]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
gcc/java/
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
0a3b29ad 1/* C++ Parser.
7f602bca 2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
f0d0d842 3 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
0a3b29ad 4 Written by Mark Mitchell <mark@codesourcery.com>.
5
6f0d25a6 6 This file is part of GCC.
0a3b29ad 7
6f0d25a6 8 GCC is free software; you can redistribute it and/or modify it
0a3b29ad 9 under the terms of the GNU General Public License as published by
aa139c3f 10 the Free Software Foundation; either version 3, or (at your option)
0a3b29ad 11 any later version.
12
6f0d25a6 13 GCC is distributed in the hope that it will be useful, but
0a3b29ad 14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
aa139c3f 18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
0a3b29ad 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"
0a3b29ad 35#include "toplev.h"
36#include "output.h"
4b9b2871 37#include "target.h"
56af936e 38#include "cgraph.h"
7a4e126b 39#include "c-common.h"
9227b6fc 40#include "plugin.h"
0a3b29ad 41
42\f
43/* The lexer. */
44
00d26680 45/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
0a3b29ad 47
3369eb76 48/* A token's value and its associated deferred access checks and
49 qualifying scope. */
50
fb1e4f4a 51struct GTY(()) tree_check {
3369eb76 52 /* The value associated with the token. */
53 tree value;
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
59};
60
0a3b29ad 61/* A C++ token. */
62
fb1e4f4a 63typedef struct GTY (()) cp_token {
0a3b29ad 64 /* The kind of token. */
07e4a80b 65 ENUM_BITFIELD (cpp_ttype) type : 8;
0a3b29ad 66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
07e4a80b 68 ENUM_BITFIELD (rid) keyword : 8;
c8d5ab79 69 /* Token flags. */
70 unsigned char flags;
b75b98aa 71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
2e1f41a9 73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
b62240d5 75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
0ac758f7 79 /* The location at which this token was found. */
80 location_t location;
63d4e07c 81 /* The value associated with this token, if any. */
3369eb76 82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
0a3b29ad 88} cp_token;
89
19273cc2 90/* We use a stack of token pointer for saving token sets. */
91typedef struct cp_token *cp_token_position;
046bfc77 92DEF_VEC_P (cp_token_position);
93DEF_VEC_ALLOC_P (cp_token_position,heap);
19273cc2 94
b7bf20db 95static cp_token eof_token =
da4e72c7 96{
0ac758f7 97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
da4e72c7 98};
19273cc2 99
0a3b29ad 100/* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
00d26680 102 it to the parser. Tokens are never added to the cp_lexer after
93523877 103 it is created. */
0a3b29ad 104
fb1e4f4a 105typedef struct GTY (()) cp_lexer {
19273cc2 106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
da4e72c7 108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
110 buffer. */
111 size_t buffer_length;
9031d10b 112
00d26680 113 /* A pointer just past the last available token. The tokens
93523877 114 in this lexer are [buffer, last_token). */
19273cc2 115 cp_token_position GTY ((skip)) last_token;
00d26680 116
19273cc2 117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
0a3b29ad 118 no more available tokens. */
19273cc2 119 cp_token_position GTY ((skip)) next_token;
0a3b29ad 120
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
19273cc2 123 began saving tokens. If the stack is non-empty, we are saving
124 tokens. */
046bfc77 125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
0a3b29ad 126
b75b98aa 127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
129
0a3b29ad 130 /* True if we should output debugging information. */
131 bool debugging_p;
132
b75b98aa 133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
135 bool in_pragma;
0a3b29ad 136} cp_lexer;
137
00d26680 138/* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
93523877 142 a lexer. */
00d26680 143
fb1e4f4a 144typedef struct GTY(()) cp_token_cache {
93523877 145 /* The beginning of the token range. */
00d26680 146 cp_token * GTY((skip)) first;
147
93523877 148 /* Points immediately after the last token in the range. */
00d26680 149 cp_token * GTY ((skip)) last;
150} cp_token_cache;
151
0a3b29ad 152/* Prototypes. */
153
573aba85 154static cp_lexer *cp_lexer_new_main
45baea8b 155 (void);
0a3b29ad 156static cp_lexer *cp_lexer_new_from_tokens
00d26680 157 (cp_token_cache *tokens);
158static void cp_lexer_destroy
159 (cp_lexer *);
0a3b29ad 160static int cp_lexer_saving_tokens
45baea8b 161 (const cp_lexer *);
19273cc2 162static cp_token_position cp_lexer_token_position
163 (cp_lexer *, bool);
164static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
0a3b29ad 166static void cp_lexer_get_preprocessor_token
45baea8b 167 (cp_lexer *, cp_token *);
00d26680 168static inline cp_token *cp_lexer_peek_token
169 (cp_lexer *);
0a3b29ad 170static cp_token *cp_lexer_peek_nth_token
45baea8b 171 (cp_lexer *, size_t);
2370b5bf 172static inline bool cp_lexer_next_token_is
45baea8b 173 (cp_lexer *, enum cpp_ttype);
0a3b29ad 174static bool cp_lexer_next_token_is_not
45baea8b 175 (cp_lexer *, enum cpp_ttype);
0a3b29ad 176static bool cp_lexer_next_token_is_keyword
45baea8b 177 (cp_lexer *, enum rid);
ccb84981 178static cp_token *cp_lexer_consume_token
45baea8b 179 (cp_lexer *);
0a3b29ad 180static void cp_lexer_purge_token
181 (cp_lexer *);
182static void cp_lexer_purge_tokens_after
19273cc2 183 (cp_lexer *, cp_token_position);
0a3b29ad 184static void cp_lexer_save_tokens
45baea8b 185 (cp_lexer *);
0a3b29ad 186static void cp_lexer_commit_tokens
45baea8b 187 (cp_lexer *);
0a3b29ad 188static void cp_lexer_rollback_tokens
45baea8b 189 (cp_lexer *);
2fcca275 190#ifdef ENABLE_CHECKING
0a3b29ad 191static void cp_lexer_print_token
45baea8b 192 (FILE *, cp_token *);
ccb84981 193static inline bool cp_lexer_debugging_p
45baea8b 194 (cp_lexer *);
0a3b29ad 195static void cp_lexer_start_debugging
45baea8b 196 (cp_lexer *) ATTRIBUTE_UNUSED;
0a3b29ad 197static void cp_lexer_stop_debugging
45baea8b 198 (cp_lexer *) ATTRIBUTE_UNUSED;
2fcca275 199#else
b9dd3954 200/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204#define cp_lexer_debug_stream stdout
205#define cp_lexer_print_token(str, tok) (void) 0
2fcca275 206#define cp_lexer_debugging_p(lexer) 0
207#endif /* ENABLE_CHECKING */
0a3b29ad 208
00d26680 209static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
211
b75b98aa 212static void cp_parser_initial_pragma
213 (cp_token *);
214
0a3b29ad 215/* Manifest constants. */
83adc189 216#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
19273cc2 217#define CP_SAVED_TOKEN_STACK 5
0a3b29ad 218
219/* A token type for keywords, as opposed to ordinary identifiers. */
220#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222/* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228/* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235/* A token type for tokens that are not tokens at all; these are used
00d26680 236 to represent slots in the array where there used to be a token
93523877 237 that has now been deleted. */
0a88af73 238#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240/* The number of token types, including C++-specific ones. */
241#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
0a3b29ad 242
243/* Variables. */
244
2fcca275 245#ifdef ENABLE_CHECKING
0a3b29ad 246/* The stream to which debugging output should be written. */
247static FILE *cp_lexer_debug_stream;
2fcca275 248#endif /* ENABLE_CHECKING */
0a3b29ad 249
573aba85 250/* Create a new main C++ lexer, the lexer that gets tokens from the
251 preprocessor. */
0a3b29ad 252
253static cp_lexer *
573aba85 254cp_lexer_new_main (void)
0a3b29ad 255{
573aba85 256 cp_token first_token;
da4e72c7 257 cp_lexer *lexer;
258 cp_token *pos;
259 size_t alloc;
260 size_t space;
261 cp_token *buffer;
573aba85 262
b75b98aa 263 /* It's possible that parsing the first pragma will load a PCH file,
264 which is a GC collection point. So we have to do that before
265 allocating any memory. */
266 cp_parser_initial_pragma (&first_token);
00d26680 267
ddf4604f 268 c_common_no_more_pch ();
0a3b29ad 269
270 /* Allocate the memory. */
a33db04a 271 lexer = GGC_CNEW (cp_lexer);
0a3b29ad 272
9031d10b 273#ifdef ENABLE_CHECKING
00d26680 274 /* Initially we are not debugging. */
0a3b29ad 275 lexer->debugging_p = false;
2fcca275 276#endif /* ENABLE_CHECKING */
046bfc77 277 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
278 CP_SAVED_TOKEN_STACK);
9031d10b 279
da4e72c7 280 /* Create the buffer. */
281 alloc = CP_LEXER_BUFFER_SIZE;
56e60747 282 buffer = GGC_NEWVEC (cp_token, alloc);
0a3b29ad 283
da4e72c7 284 /* Put the first token in the buffer. */
285 space = alloc;
286 pos = buffer;
287 *pos = first_token;
9031d10b 288
93523877 289 /* Get the remaining tokens from the preprocessor. */
da4e72c7 290 while (pos->type != CPP_EOF)
00d26680 291 {
da4e72c7 292 pos++;
293 if (!--space)
294 {
295 space = alloc;
296 alloc *= 2;
7ea410eb 297 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
da4e72c7 298 pos = buffer + space;
299 }
300 cp_lexer_get_preprocessor_token (lexer, pos);
00d26680 301 }
da4e72c7 302 lexer->buffer = buffer;
303 lexer->buffer_length = alloc - space;
304 lexer->last_token = pos;
b7bf20db 305 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
00d26680 306
eb0d20b7 307 /* Subsequent preprocessor diagnostics should use compiler
308 diagnostic functions to get the compiler source location. */
7f5f3953 309 done_lexing = true;
eb0d20b7 310
b9dd3954 311 gcc_assert (lexer->next_token->type != CPP_PURGED);
0a3b29ad 312 return lexer;
313}
314
00d26680 315/* Create a new lexer whose token stream is primed with the tokens in
b9dd3954 316 CACHE. When these tokens are exhausted, no new tokens will be read. */
0a3b29ad 317
318static cp_lexer *
b9dd3954 319cp_lexer_new_from_tokens (cp_token_cache *cache)
0a3b29ad 320{
b9dd3954 321 cp_token *first = cache->first;
322 cp_token *last = cache->last;
00d26680 323 cp_lexer *lexer = GGC_CNEW (cp_lexer);
573aba85 324
19273cc2 325 /* We do not own the buffer. */
da4e72c7 326 lexer->buffer = NULL;
327 lexer->buffer_length = 0;
b7bf20db 328 lexer->next_token = first == last ? &eof_token : first;
19273cc2 329 lexer->last_token = last;
9031d10b 330
046bfc77 331 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
332 CP_SAVED_TOKEN_STACK);
ccb84981 333
2fcca275 334#ifdef ENABLE_CHECKING
00d26680 335 /* Initially we are not debugging. */
573aba85 336 lexer->debugging_p = false;
00d26680 337#endif
0a3b29ad 338
b9dd3954 339 gcc_assert (lexer->next_token->type != CPP_PURGED);
340 return lexer;
00d26680 341}
342
93523877 343/* Frees all resources associated with LEXER. */
00d26680 344
345static void
346cp_lexer_destroy (cp_lexer *lexer)
347{
19273cc2 348 if (lexer->buffer)
349 ggc_free (lexer->buffer);
046bfc77 350 VEC_free (cp_token_position, heap, lexer->saved_tokens);
00d26680 351 ggc_free (lexer);
352}
353
f1d555e3 354/* Returns nonzero if debugging information should be output. */
0a3b29ad 355
2fcca275 356#ifdef ENABLE_CHECKING
357
2370b5bf 358static inline bool
359cp_lexer_debugging_p (cp_lexer *lexer)
0a3b29ad 360{
2370b5bf 361 return lexer->debugging_p;
362}
363
2fcca275 364#endif /* ENABLE_CHECKING */
365
19273cc2 366static inline cp_token_position
367cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
0a3b29ad 368{
19273cc2 369 gcc_assert (!previous_p || lexer->next_token != &eof_token);
9031d10b 370
19273cc2 371 return lexer->next_token - previous_p;
0a3b29ad 372}
373
3d0f901b 374static inline cp_token *
19273cc2 375cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
3d0f901b 376{
19273cc2 377 return pos;
3d0f901b 378}
379
f1d555e3 380/* nonzero if we are presently saving tokens. */
2370b5bf 381
19273cc2 382static inline int
45baea8b 383cp_lexer_saving_tokens (const cp_lexer* lexer)
2370b5bf 384{
19273cc2 385 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
0a3b29ad 386}
387
da4e72c7 388/* Store the next token from the preprocessor in *TOKEN. Return true
8115b8be 389 if we reach EOF. If LEXER is NULL, assume we are handling an
390 initial #pragma pch_preprocess, and thus want the lexer to return
391 processed strings. */
0a3b29ad 392
ccb84981 393static void
8115b8be 394cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
0a3b29ad 395{
2e1f41a9 396 static int is_extern_c = 0;
0a3b29ad 397
61cc302f 398 /* Get a new token from the preprocessor. */
9d819530 399 token->type
8115b8be 400 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
401 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
b75b98aa 402 token->keyword = RID_MAX;
403 token->pragma_kind = PRAGMA_NONE;
0a3b29ad 404
9031d10b 405 /* On some systems, some header files are surrounded by an
2e1f41a9 406 implicit extern "C" block. Set a flag in the token if it
93523877 407 comes from such a header. */
2e1f41a9 408 is_extern_c += pending_lang_change;
409 pending_lang_change = 0;
410 token->implicit_extern_c = is_extern_c > 0;
411
0a3b29ad 412 /* Check to see if this token is a keyword. */
b62240d5 413 if (token->type == CPP_NAME)
0a3b29ad 414 {
3369eb76 415 if (C_IS_RESERVED_WORD (token->u.value))
b62240d5 416 {
417 /* Mark this token as a keyword. */
418 token->type = CPP_KEYWORD;
419 /* Record which keyword. */
3369eb76 420 token->keyword = C_RID_CODE (token->u.value);
b62240d5 421 }
422 else
c47b8257 423 {
ae49e4a6 424 if (warn_cxx0x_compat
425 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
426 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
427 {
428 /* Warn about the C++0x keyword (but still treat it as
429 an identifier). */
f94b7fe2 430 warning (OPT_Wc__0x_compat,
a608187f 431 "identifier %qE will become a keyword in C++0x",
432 token->u.value);
ae49e4a6 433
434 /* Clear out the C_RID_CODE so we don't warn about this
435 particular identifier-turned-keyword again. */
7d339f93 436 C_SET_RID_CODE (token->u.value, RID_MAX);
ae49e4a6 437 }
438
c47b8257 439 token->ambiguous_p = false;
440 token->keyword = RID_MAX;
441 }
0a3b29ad 442 }
7a4e126b 443 /* Handle Objective-C++ keywords. */
444 else if (token->type == CPP_AT_NAME)
445 {
446 token->type = CPP_KEYWORD;
3369eb76 447 switch (C_RID_CODE (token->u.value))
7a4e126b 448 {
449 /* Map 'class' to '@class', 'private' to '@private', etc. */
450 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
451 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
452 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
453 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
454 case RID_THROW: token->keyword = RID_AT_THROW; break;
455 case RID_TRY: token->keyword = RID_AT_TRY; break;
456 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
3369eb76 457 default: token->keyword = C_RID_CODE (token->u.value);
7a4e126b 458 }
459 }
b75b98aa 460 else if (token->type == CPP_PRAGMA)
461 {
462 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
8458f4ca 463 token->pragma_kind = ((enum pragma_kind)
464 TREE_INT_CST_LOW (token->u.value));
3369eb76 465 token->u.value = NULL_TREE;
b75b98aa 466 }
0a3b29ad 467}
468
bdbc474b 469/* Update the globals input_location and the input file stack from TOKEN. */
b9dd3954 470static inline void
471cp_lexer_set_source_position_from_token (cp_token *token)
472{
473 if (token->type != CPP_EOF)
474 {
475 input_location = token->location;
b9dd3954 476 }
477}
478
0a3b29ad 479/* Return a pointer to the next token in the token stream, but do not
480 consume it. */
481
00d26680 482static inline cp_token *
483cp_lexer_peek_token (cp_lexer *lexer)
0a3b29ad 484{
0a3b29ad 485 if (cp_lexer_debugging_p (lexer))
19273cc2 486 {
487 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
488 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
489 putc ('\n', cp_lexer_debug_stream);
490 }
b9dd3954 491 return lexer->next_token;
0a3b29ad 492}
493
494/* Return true if the next token has the indicated TYPE. */
495
b9dd3954 496static inline bool
45baea8b 497cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 498{
b9dd3954 499 return cp_lexer_peek_token (lexer)->type == type;
0a3b29ad 500}
501
502/* Return true if the next token does not have the indicated TYPE. */
503
b9dd3954 504static inline bool
45baea8b 505cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 506{
507 return !cp_lexer_next_token_is (lexer, type);
508}
509
510/* Return true if the next token is the indicated KEYWORD. */
511
b9dd3954 512static inline bool
45baea8b 513cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
0a3b29ad 514{
8f399589 515 return cp_lexer_peek_token (lexer)->keyword == keyword;
0a3b29ad 516}
517
f82f1250 518/* Return true if the next token is not the indicated KEYWORD. */
519
520static inline bool
521cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
522{
523 return cp_lexer_peek_token (lexer)->keyword != keyword;
524}
525
17f99120 526/* Return true if the next token is a keyword for a decl-specifier. */
527
9a7c4b43 528static bool
529cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
530{
531 cp_token *token;
532
533 token = cp_lexer_peek_token (lexer);
534 switch (token->keyword)
535 {
45b44d0a 536 /* auto specifier: storage-class-specifier in C++,
537 simple-type-specifier in C++0x. */
9a7c4b43 538 case RID_AUTO:
45b44d0a 539 /* Storage classes. */
9a7c4b43 540 case RID_REGISTER:
541 case RID_STATIC:
542 case RID_EXTERN:
543 case RID_MUTABLE:
544 case RID_THREAD:
545 /* Elaborated type specifiers. */
546 case RID_ENUM:
547 case RID_CLASS:
548 case RID_STRUCT:
549 case RID_UNION:
550 case RID_TYPENAME:
551 /* Simple type specifiers. */
552 case RID_CHAR:
924bbf02 553 case RID_CHAR16:
554 case RID_CHAR32:
9a7c4b43 555 case RID_WCHAR:
556 case RID_BOOL:
557 case RID_SHORT:
558 case RID_INT:
559 case RID_LONG:
560 case RID_SIGNED:
561 case RID_UNSIGNED:
562 case RID_FLOAT:
563 case RID_DOUBLE:
564 case RID_VOID:
565 /* GNU extensions. */
566 case RID_ATTRIBUTE:
567 case RID_TYPEOF:
34da8800 568 /* C++0x extensions. */
569 case RID_DECLTYPE:
9a7c4b43 570 return true;
571
572 default:
573 return false;
574 }
575}
576
0a3b29ad 577/* Return a pointer to the Nth token in the token stream. If N is 1,
b9dd3954 578 then this is precisely equivalent to cp_lexer_peek_token (except
579 that it is not inline). One would like to disallow that case, but
580 there is one case (cp_parser_nth_token_starts_template_id) where
581 the caller passes a variable for N and it might be 1. */
0a3b29ad 582
583static cp_token *
45baea8b 584cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
0a3b29ad 585{
586 cp_token *token;
587
588 /* N is 1-based, not zero-based. */
73cc0964 589 gcc_assert (n > 0);
074ab442 590
b9dd3954 591 if (cp_lexer_debugging_p (lexer))
592 fprintf (cp_lexer_debug_stream,
593 "cp_lexer: peeking ahead %ld at token: ", (long)n);
594
00d26680 595 --n;
0a3b29ad 596 token = lexer->next_token;
73cc0964 597 gcc_assert (!n || token != &eof_token);
00d26680 598 while (n != 0)
0a3b29ad 599 {
00d26680 600 ++token;
19273cc2 601 if (token == lexer->last_token)
602 {
b7bf20db 603 token = &eof_token;
19273cc2 604 break;
605 }
9031d10b 606
00d26680 607 if (token->type != CPP_PURGED)
608 --n;
0a3b29ad 609 }
610
b9dd3954 611 if (cp_lexer_debugging_p (lexer))
612 {
613 cp_lexer_print_token (cp_lexer_debug_stream, token);
614 putc ('\n', cp_lexer_debug_stream);
615 }
616
0a3b29ad 617 return token;
618}
619
b9dd3954 620/* Return the next token, and advance the lexer's next_token pointer
621 to point to the next non-purged token. */
0a3b29ad 622
623static cp_token *
45baea8b 624cp_lexer_consume_token (cp_lexer* lexer)
0a3b29ad 625{
b9dd3954 626 cp_token *token = lexer->next_token;
0a3b29ad 627
19273cc2 628 gcc_assert (token != &eof_token);
b75b98aa 629 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
9031d10b 630
b9dd3954 631 do
19273cc2 632 {
633 lexer->next_token++;
634 if (lexer->next_token == lexer->last_token)
635 {
b7bf20db 636 lexer->next_token = &eof_token;
19273cc2 637 break;
638 }
9031d10b 639
19273cc2 640 }
b9dd3954 641 while (lexer->next_token->type == CPP_PURGED);
9031d10b 642
b9dd3954 643 cp_lexer_set_source_position_from_token (token);
9031d10b 644
0a3b29ad 645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
647 {
b9dd3954 648 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
0a3b29ad 649 cp_lexer_print_token (cp_lexer_debug_stream, token);
b9dd3954 650 putc ('\n', cp_lexer_debug_stream);
0a3b29ad 651 }
9031d10b 652
0a3b29ad 653 return token;
654}
655
b9dd3954 656/* Permanently remove the next token from the token stream, and
657 advance the next_token pointer to refer to the next non-purged
658 token. */
0a3b29ad 659
660static void
661cp_lexer_purge_token (cp_lexer *lexer)
662{
00d26680 663 cp_token *tok = lexer->next_token;
9031d10b 664
19273cc2 665 gcc_assert (tok != &eof_token);
00d26680 666 tok->type = CPP_PURGED;
667 tok->location = UNKNOWN_LOCATION;
3369eb76 668 tok->u.value = NULL_TREE;
00d26680 669 tok->keyword = RID_MAX;
b9dd3954 670
671 do
19273cc2 672 {
673 tok++;
674 if (tok == lexer->last_token)
675 {
b7bf20db 676 tok = &eof_token;
19273cc2 677 break;
678 }
679 }
680 while (tok->type == CPP_PURGED);
681 lexer->next_token = tok;
0a3b29ad 682}
683
00d26680 684/* Permanently remove all tokens after TOK, up to, but not
0a3b29ad 685 including, the token that will be returned next by
686 cp_lexer_peek_token. */
687
688static void
00d26680 689cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
0a3b29ad 690{
19273cc2 691 cp_token *peek = lexer->next_token;
0a3b29ad 692
19273cc2 693 if (peek == &eof_token)
694 peek = lexer->last_token;
9031d10b 695
00d26680 696 gcc_assert (tok < peek);
697
698 for ( tok += 1; tok != peek; tok += 1)
0a3b29ad 699 {
00d26680 700 tok->type = CPP_PURGED;
701 tok->location = UNKNOWN_LOCATION;
3369eb76 702 tok->u.value = NULL_TREE;
00d26680 703 tok->keyword = RID_MAX;
0a3b29ad 704 }
00d26680 705}
706
0a3b29ad 707/* Begin saving tokens. All tokens consumed after this point will be
708 preserved. */
709
710static void
45baea8b 711cp_lexer_save_tokens (cp_lexer* lexer)
0a3b29ad 712{
713 /* Provide debugging output. */
714 if (cp_lexer_debugging_p (lexer))
715 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
716
046bfc77 717 VEC_safe_push (cp_token_position, heap,
718 lexer->saved_tokens, lexer->next_token);
0a3b29ad 719}
720
721/* Commit to the portion of the token stream most recently saved. */
722
723static void
45baea8b 724cp_lexer_commit_tokens (cp_lexer* lexer)
0a3b29ad 725{
726 /* Provide debugging output. */
727 if (cp_lexer_debugging_p (lexer))
728 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
729
19273cc2 730 VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 731}
732
733/* Return all tokens saved since the last call to cp_lexer_save_tokens
734 to the token stream. Stop saving tokens. */
735
736static void
45baea8b 737cp_lexer_rollback_tokens (cp_lexer* lexer)
0a3b29ad 738{
0a3b29ad 739 /* Provide debugging output. */
740 if (cp_lexer_debugging_p (lexer))
741 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
742
19273cc2 743 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 744}
745
0a3b29ad 746/* Print a representation of the TOKEN on the STREAM. */
747
2fcca275 748#ifdef ENABLE_CHECKING
749
0a3b29ad 750static void
00d26680 751cp_lexer_print_token (FILE * stream, cp_token *token)
752{
753 /* We don't use cpp_type2name here because the parser defines
754 a few tokens of its own. */
755 static const char *const token_names[] = {
756 /* cpplib-defined token types */
757#define OP(e, s) #e,
758#define TK(e, s) #e,
759 TTYPE_TABLE
760#undef OP
761#undef TK
762 /* C++ parser token types - see "Manifest constants", above. */
763 "KEYWORD",
764 "TEMPLATE_ID",
765 "NESTED_NAME_SPECIFIER",
766 "PURGED"
767 };
9031d10b 768
00d26680 769 /* If we have a name for the token, print it out. Otherwise, we
770 simply give the numeric code. */
771 gcc_assert (token->type < ARRAY_SIZE(token_names));
772 fputs (token_names[token->type], stream);
0a3b29ad 773
00d26680 774 /* For some tokens, print the associated data. */
0a3b29ad 775 switch (token->type)
776 {
00d26680 777 case CPP_KEYWORD:
778 /* Some keywords have a value that is not an IDENTIFIER_NODE.
779 For example, `struct' is mapped to an INTEGER_CST. */
3369eb76 780 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
00d26680 781 break;
782 /* else fall through */
0a3b29ad 783 case CPP_NAME:
3369eb76 784 fputs (IDENTIFIER_POINTER (token->u.value), stream);
0a3b29ad 785 break;
786
00d26680 787 case CPP_STRING:
924bbf02 788 case CPP_STRING16:
789 case CPP_STRING32:
00d26680 790 case CPP_WSTRING:
3369eb76 791 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
0a3b29ad 792 break;
793
0a3b29ad 794 default:
795 break;
796 }
0a3b29ad 797}
798
0a3b29ad 799/* Start emitting debugging information. */
800
801static void
45baea8b 802cp_lexer_start_debugging (cp_lexer* lexer)
0a3b29ad 803{
31cc05e3 804 lexer->debugging_p = true;
0a3b29ad 805}
ccb84981 806
0a3b29ad 807/* Stop emitting debugging information. */
808
809static void
45baea8b 810cp_lexer_stop_debugging (cp_lexer* lexer)
0a3b29ad 811{
31cc05e3 812 lexer->debugging_p = false;
0a3b29ad 813}
814
2fcca275 815#endif /* ENABLE_CHECKING */
816
93523877 817/* Create a new cp_token_cache, representing a range of tokens. */
00d26680 818
819static cp_token_cache *
820cp_token_cache_new (cp_token *first, cp_token *last)
821{
822 cp_token_cache *cache = GGC_NEW (cp_token_cache);
823 cache->first = first;
824 cache->last = last;
825 return cache;
826}
827
0a3b29ad 828\f
4b9b2871 829/* Decl-specifiers. */
830
4b9b2871 831/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
832
833static void
834clear_decl_specs (cp_decl_specifier_seq *decl_specs)
835{
836 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
837}
838
3046c0a3 839/* Declarators. */
840
841/* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
845
207355ad 846static cp_declarator *make_call_declarator
34eac767 847 (cp_declarator *, tree, cp_cv_quals, tree, tree);
207355ad 848static cp_declarator *make_array_declarator
3046c0a3 849 (cp_declarator *, tree);
207355ad 850static cp_declarator *make_pointer_declarator
2cfb6cde 851 (cp_cv_quals, cp_declarator *);
207355ad 852static cp_declarator *make_reference_declarator
63949b38 853 (cp_cv_quals, cp_declarator *, bool);
207355ad 854static cp_parameter_declarator *make_parameter_declarator
4b9b2871 855 (cp_decl_specifier_seq *, cp_declarator *, tree);
207355ad 856static cp_declarator *make_ptrmem_declarator
2cfb6cde 857 (cp_cv_quals, tree, cp_declarator *);
3046c0a3 858
197c9df7 859/* An erroneous declarator. */
860static cp_declarator *cp_error_declarator;
3046c0a3 861
862/* The obstack on which declarators and related data structures are
863 allocated. */
864static struct obstack declarator_obstack;
865
866/* Alloc BYTES from the declarator memory pool. */
867
868static inline void *
869alloc_declarator (size_t bytes)
870{
871 return obstack_alloc (&declarator_obstack, bytes);
872}
873
874/* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
876
877static cp_declarator *
878make_declarator (cp_declarator_kind kind)
879{
880 cp_declarator *declarator;
881
882 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883 declarator->kind = kind;
884 declarator->attributes = NULL_TREE;
885 declarator->declarator = NULL;
d95d815d 886 declarator->parameter_pack_p = false;
3046c0a3 887
888 return declarator;
889}
890
2366ed31 891/* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
895 is, if any. */
3046c0a3 896
2ded3667 897static cp_declarator *
2366ed31 898make_id_declarator (tree qualifying_scope, tree unqualified_name,
899 special_function_kind sfk)
3046c0a3 900{
901 cp_declarator *declarator;
207355ad 902
2ded3667 903 /* It is valid to write:
904
905 class C { void f(); };
906 typedef C D;
907 void D::f();
908
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
912 well. */
913 if (qualifying_scope && TYPE_P (qualifying_scope))
914 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
915
2366ed31 916 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
919
3046c0a3 920 declarator = make_declarator (cdk_id);
2ded3667 921 declarator->u.id.qualifying_scope = qualifying_scope;
922 declarator->u.id.unqualified_name = unqualified_name;
2366ed31 923 declarator->u.id.sfk = sfk;
d95d815d 924
3046c0a3 925 return declarator;
926}
927
928/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
931
932cp_declarator *
2cfb6cde 933make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
3046c0a3 934{
935 cp_declarator *declarator;
936
937 declarator = make_declarator (cdk_pointer);
938 declarator->declarator = target;
939 declarator->u.pointer.qualifiers = cv_qualifiers;
940 declarator->u.pointer.class_type = NULL_TREE;
d95d815d 941 if (target)
942 {
943 declarator->parameter_pack_p = target->parameter_pack_p;
944 target->parameter_pack_p = false;
945 }
946 else
947 declarator->parameter_pack_p = false;
3046c0a3 948
949 return declarator;
950}
951
952/* Like make_pointer_declarator -- but for references. */
953
954cp_declarator *
63949b38 955make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
956 bool rvalue_ref)
3046c0a3 957{
958 cp_declarator *declarator;
959
960 declarator = make_declarator (cdk_reference);
961 declarator->declarator = target;
63949b38 962 declarator->u.reference.qualifiers = cv_qualifiers;
963 declarator->u.reference.rvalue_ref = rvalue_ref;
d95d815d 964 if (target)
965 {
966 declarator->parameter_pack_p = target->parameter_pack_p;
967 target->parameter_pack_p = false;
968 }
969 else
970 declarator->parameter_pack_p = false;
3046c0a3 971
972 return declarator;
973}
974
975/* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
977
978cp_declarator *
2cfb6cde 979make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
3046c0a3 980 cp_declarator *pointee)
981{
982 cp_declarator *declarator;
983
984 declarator = make_declarator (cdk_ptrmem);
985 declarator->declarator = pointee;
986 declarator->u.pointer.qualifiers = cv_qualifiers;
987 declarator->u.pointer.class_type = class_type;
988
d95d815d 989 if (pointee)
990 {
991 declarator->parameter_pack_p = pointee->parameter_pack_p;
992 pointee->parameter_pack_p = false;
993 }
994 else
995 declarator->parameter_pack_p = false;
996
3046c0a3 997 return declarator;
998}
999
1000/* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1004
1005cp_declarator *
207355ad 1006make_call_declarator (cp_declarator *target,
34eac767 1007 tree parms,
2cfb6cde 1008 cp_cv_quals cv_qualifiers,
346e3a9c 1009 tree exception_specification,
1010 tree late_return_type)
3046c0a3 1011{
1012 cp_declarator *declarator;
1013
1014 declarator = make_declarator (cdk_function);
1015 declarator->declarator = target;
1016 declarator->u.function.parameters = parms;
1017 declarator->u.function.qualifiers = cv_qualifiers;
1018 declarator->u.function.exception_specification = exception_specification;
346e3a9c 1019 declarator->u.function.late_return_type = late_return_type;
d95d815d 1020 if (target)
1021 {
1022 declarator->parameter_pack_p = target->parameter_pack_p;
1023 target->parameter_pack_p = false;
1024 }
1025 else
1026 declarator->parameter_pack_p = false;
3046c0a3 1027
1028 return declarator;
1029}
1030
1031/* Make a declarator for an array of BOUNDS elements, each of which is
1032 defined by ELEMENT. */
1033
1034cp_declarator *
1035make_array_declarator (cp_declarator *element, tree bounds)
1036{
1037 cp_declarator *declarator;
1038
1039 declarator = make_declarator (cdk_array);
1040 declarator->declarator = element;
1041 declarator->u.array.bounds = bounds;
d95d815d 1042 if (element)
1043 {
1044 declarator->parameter_pack_p = element->parameter_pack_p;
1045 element->parameter_pack_p = false;
1046 }
1047 else
1048 declarator->parameter_pack_p = false;
3046c0a3 1049
1050 return declarator;
1051}
1052
2aedc2ff 1053/* Determine whether the declarator we've seen so far can be a
1054 parameter pack, when followed by an ellipsis. */
1055static bool
1056declarator_can_be_parameter_pack (cp_declarator *declarator)
1057{
1058 /* Search for a declarator name, or any other declarator that goes
1059 after the point where the ellipsis could appear in a parameter
1060 pack. If we find any of these, then this declarator can not be
1061 made into a parameter pack. */
1062 bool found = false;
1063 while (declarator && !found)
1064 {
1065 switch ((int)declarator->kind)
1066 {
1067 case cdk_id:
2aedc2ff 1068 case cdk_array:
2aedc2ff 1069 found = true;
1070 break;
41341abd 1071
1072 case cdk_error:
1073 return true;
1074
2aedc2ff 1075 default:
1076 declarator = declarator->declarator;
1077 break;
1078 }
1079 }
1080
1081 return !found;
1082}
1083
3046c0a3 1084cp_parameter_declarator *no_parameters;
1085
1086/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087 DECLARATOR and DEFAULT_ARGUMENT. */
1088
1089cp_parameter_declarator *
207355ad 1090make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
3046c0a3 1091 cp_declarator *declarator,
1092 tree default_argument)
1093{
1094 cp_parameter_declarator *parameter;
1095
207355ad 1096 parameter = ((cp_parameter_declarator *)
3046c0a3 1097 alloc_declarator (sizeof (cp_parameter_declarator)));
1098 parameter->next = NULL;
4b9b2871 1099 if (decl_specifiers)
1100 parameter->decl_specifiers = *decl_specifiers;
1101 else
1102 clear_decl_specs (&parameter->decl_specifiers);
3046c0a3 1103 parameter->declarator = declarator;
1104 parameter->default_argument = default_argument;
1105 parameter->ellipsis_p = false;
1106
1107 return parameter;
1108}
1109
3a30cb7f 1110/* Returns true iff DECLARATOR is a declaration for a function. */
1111
1112static bool
1113function_declarator_p (const cp_declarator *declarator)
1114{
1115 while (declarator)
1116 {
1117 if (declarator->kind == cdk_function
1118 && declarator->declarator->kind == cdk_id)
1119 return true;
1120 if (declarator->kind == cdk_id
1121 || declarator->kind == cdk_error)
1122 return false;
1123 declarator = declarator->declarator;
1124 }
1125 return false;
1126}
1127
0a3b29ad 1128/* The parser. */
1129
1130/* Overview
1131 --------
1132
1133 A cp_parser parses the token stream as specified by the C++
1134 grammar. Its job is purely parsing, not semantic analysis. For
1135 example, the parser breaks the token stream into declarators,
1136 expressions, statements, and other similar syntactic constructs.
1137 It does not check that the types of the expressions on either side
1138 of an assignment-statement are compatible, or that a function is
1139 not declared with a parameter of type `void'.
1140
1141 The parser invokes routines elsewhere in the compiler to perform
1142 semantic analysis and to build up the abstract syntax tree for the
ccb84981 1143 code processed.
0a3b29ad 1144
1145 The parser (and the template instantiation code, which is, in a
1146 way, a close relative of parsing) are the only parts of the
1147 compiler that should be calling push_scope and pop_scope, or
1148 related functions. The parser (and template instantiation code)
1149 keeps track of what scope is presently active; everything else
1150 should simply honor that. (The code that generates static
1151 initializers may also need to set the scope, in order to check
1152 access control correctly when emitting the initializers.)
1153
1154 Methodology
1155 -----------
ccb84981 1156
0a3b29ad 1157 The parser is of the standard recursive-descent variety. Upcoming
1158 tokens in the token stream are examined in order to determine which
1159 production to use when parsing a non-terminal. Some C++ constructs
1160 require arbitrary look ahead to disambiguate. For example, it is
1161 impossible, in the general case, to tell whether a statement is an
1162 expression or declaration without scanning the entire statement.
1163 Therefore, the parser is capable of "parsing tentatively." When the
1164 parser is not sure what construct comes next, it enters this mode.
1165 Then, while we attempt to parse the construct, the parser queues up
1166 error messages, rather than issuing them immediately, and saves the
1167 tokens it consumes. If the construct is parsed successfully, the
1168 parser "commits", i.e., it issues any queued error messages and
1169 the tokens that were being preserved are permanently discarded.
1170 If, however, the construct is not parsed successfully, the parser
1171 rolls back its state completely so that it can resume parsing using
1172 a different alternative.
1173
1174 Future Improvements
1175 -------------------
ccb84981 1176
0a88af73 1177 The performance of the parser could probably be improved substantially.
1178 We could often eliminate the need to parse tentatively by looking ahead
1179 a little bit. In some places, this approach might not entirely eliminate
1180 the need to parse tentatively, but it might still speed up the average
1181 case. */
0a3b29ad 1182
1183/* Flags that are passed to some parsing functions. These values can
1184 be bitwise-ored together. */
1185
26dbec0a 1186enum
0a3b29ad 1187{
1188 /* No flags. */
1189 CP_PARSER_FLAGS_NONE = 0x0,
1190 /* The construct is optional. If it is not present, then no error
1191 should be issued. */
1192 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193 /* When parsing a type-specifier, do not allow user-defined types. */
1194 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
8458f4ca 1195};
1196
1197/* This type is used for parameters and variables which hold
26dbec0a 1198 combinations of the above flags. */
8458f4ca 1199typedef int cp_parser_flags;
0a3b29ad 1200
42bbd0ec 1201/* The different kinds of declarators we want to parse. */
1202
1203typedef enum cp_parser_declarator_kind
1204{
0f3ccaa3 1205 /* We want an abstract declarator. */
42bbd0ec 1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
bd8962d5 1209 /* We don't mind, but the name must be an unqualified-id. */
42bbd0ec 1210 CP_PARSER_DECLARATOR_EITHER
1211} cp_parser_declarator_kind;
1212
0a88af73 1213/* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
0a3b29ad 1216
0a88af73 1217enum cp_parser_prec
0a3b29ad 1218{
0a88af73 1219 PREC_NOT_OPERATOR,
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
0a88af73 1225 PREC_EQUALITY_EXPRESSION,
22dc256c 1226 PREC_RELATIONAL_EXPRESSION,
0a88af73 1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1230 PREC_PM_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232};
0a3b29ad 1233
0a88af73 1234/* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
0a3b29ad 1236
0a88af73 1237typedef struct cp_parser_binary_operations_map_node
1238{
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245} cp_parser_binary_operations_map_node;
0a3b29ad 1246
1247/* The status of a tentative parse. */
1248
1249typedef enum cp_parser_status_kind
1250{
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1256 has occurred. */
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258} cp_parser_status_kind;
1259
0a88af73 1260typedef struct cp_parser_expression_stack_entry
1261{
e534436e 1262 /* Left hand side of the binary operation we are currently
1263 parsing. */
0a88af73 1264 tree lhs;
e534436e 1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
0a88af73 1269 enum tree_code tree_type;
e534436e 1270 /* Precedence of the binary operation we are parsing. */
8458f4ca 1271 enum cp_parser_prec prec;
0a88af73 1272} cp_parser_expression_stack_entry;
1273
9802eea0 1274/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1276 increasing. */
0a88af73 1277typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
0a3b29ad 1279
0a88af73 1280/* Context that is saved and restored when parsing tentatively. */
fb1e4f4a 1281typedef struct GTY (()) cp_parser_context {
0a3b29ad 1282 /* If this is a tentative parsing context, the status of the
1283 tentative parse. */
1284 enum cp_parser_status_kind status;
1285 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1286 that are looked up in this context must be looked up both in the
1287 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1288 the context of the containing expression. */
1289 tree object_type;
0a88af73 1290
0a3b29ad 1291 /* The next parsing context in the stack. */
1292 struct cp_parser_context *next;
1293} cp_parser_context;
1294
1295/* Prototypes. */
1296
1297/* Constructors and destructors. */
1298
1299static cp_parser_context *cp_parser_context_new
45baea8b 1300 (cp_parser_context *);
0a3b29ad 1301
2c593bd0 1302/* Class variables. */
1303
7035b2ab 1304static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2c593bd0 1305
0a88af73 1306/* The operator-precedence table used by cp_parser_binary_expression.
1307 Transformed into an associative array (binops_by_token) by
1308 cp_parser_new. */
1309
1310static const cp_parser_binary_operations_map_node binops[] = {
1311 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1312 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1313
1314 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317
1318 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320
1321 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323
1324 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
0a88af73 1328
1329 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1330 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1331
1332 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1333
1334 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1335
1336 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1337
1338 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1339
1340 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1341};
1342
1343/* The same as binops, but initialized by cp_parser_new so that
1344 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1345 for speed. */
1346static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1347
0a3b29ad 1348/* Constructors and destructors. */
1349
1350/* Construct a new context. The context below this one on the stack
1351 is given by NEXT. */
1352
1353static cp_parser_context *
45baea8b 1354cp_parser_context_new (cp_parser_context* next)
0a3b29ad 1355{
1356 cp_parser_context *context;
1357
1358 /* Allocate the storage. */
2c593bd0 1359 if (cp_parser_context_free_list != NULL)
1360 {
1361 /* Pull the first entry from the free list. */
1362 context = cp_parser_context_free_list;
1363 cp_parser_context_free_list = context->next;
6edf18a6 1364 memset (context, 0, sizeof (*context));
2c593bd0 1365 }
1366 else
a33db04a 1367 context = GGC_CNEW (cp_parser_context);
0a88af73 1368
0a3b29ad 1369 /* No errors have occurred yet in this context. */
1370 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
08cc44e7 1371 /* If this is not the bottommost context, copy information that we
0a3b29ad 1372 need from the previous context. */
1373 if (next)
1374 {
1375 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1376 expression, then we are parsing one in this context, too. */
1377 context->object_type = next->object_type;
0a3b29ad 1378 /* Thread the stack. */
1379 context->next = next;
1380 }
1381
1382 return context;
1383}
1384
1385/* The cp_parser structure represents the C++ parser. */
1386
fb1e4f4a 1387typedef struct GTY(()) cp_parser {
0a3b29ad 1388 /* The lexer from which we are obtaining tokens. */
1389 cp_lexer *lexer;
1390
1391 /* The scope in which names should be looked up. If NULL_TREE, then
1392 we look up names in the scope that is currently open in the
1393 source program. If non-NULL, this is either a TYPE or
c75ae97e 1394 NAMESPACE_DECL for the scope in which we should look. It can
1395 also be ERROR_MARK, when we've parsed a bogus scope.
0a3b29ad 1396
1397 This value is not cleared automatically after a name is looked
1398 up, so we must be careful to clear it before starting a new look
1399 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1400 will look up `Z' in the scope of `X', rather than the current
1401 scope.) Unfortunately, it is difficult to tell when name lookup
1402 is complete, because we sometimes peek at a token, look it up,
c75ae97e 1403 and then decide not to consume it. */
0a3b29ad 1404 tree scope;
1405
1406 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407 last lookup took place. OBJECT_SCOPE is used if an expression
1408 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
ccb84981 1409 respectively. QUALIFYING_SCOPE is used for an expression of the
0a3b29ad 1410 form "X::Y"; it refers to X. */
1411 tree object_scope;
1412 tree qualifying_scope;
1413
1414 /* A stack of parsing contexts. All but the bottom entry on the
1415 stack will be tentative contexts.
1416
1417 We parse tentatively in order to determine which construct is in
1418 use in some situations. For example, in order to determine
1419 whether a statement is an expression-statement or a
1420 declaration-statement we parse it tentatively as a
1421 declaration-statement. If that fails, we then reparse the same
1422 token stream as an expression-statement. */
1423 cp_parser_context *context;
1424
1425 /* True if we are parsing GNU C++. If this flag is not set, then
1426 GNU extensions are not recognized. */
1427 bool allow_gnu_extensions_p;
1428
1429 /* TRUE if the `>' token should be interpreted as the greater-than
1430 operator. FALSE if it is the end of a template-id or
56471494 1431 template-parameter-list. In C++0x mode, this flag also applies to
1432 `>>' tokens, which are viewed as two consecutive `>' tokens when
1433 this flag is FALSE. */
0a3b29ad 1434 bool greater_than_is_operator_p;
1435
1436 /* TRUE if default arguments are allowed within a parameter list
1437 that starts at this point. FALSE if only a gnu extension makes
63eff20d 1438 them permissible. */
0a3b29ad 1439 bool default_arg_ok_p;
ccb84981 1440
0a3b29ad 1441 /* TRUE if we are parsing an integral constant-expression. See
1442 [expr.const] for a precise definition. */
f47c1747 1443 bool integral_constant_expression_p;
0a3b29ad 1444
5f6526e1 1445 /* TRUE if we are parsing an integral constant-expression -- but a
1446 non-constant expression should be permitted as well. This flag
1447 is used when parsing an array bound so that GNU variable-length
1448 arrays are tolerated. */
f47c1747 1449 bool allow_non_integral_constant_expression_p;
5f6526e1 1450
1451 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452 been seen that makes the expression non-constant. */
f47c1747 1453 bool non_integral_constant_expression_p;
5f6526e1 1454
0a3b29ad 1455 /* TRUE if local variable names and `this' are forbidden in the
1456 current context. */
1457 bool local_variables_forbidden_p;
1458
1459 /* TRUE if the declaration we are parsing is part of a
1460 linkage-specification of the form `extern string-literal
1461 declaration'. */
1462 bool in_unbraced_linkage_specification_p;
1463
1464 /* TRUE if we are presently parsing a declarator, after the
1465 direct-declarator. */
1466 bool in_declarator_p;
1467
92b128ed 1468 /* TRUE if we are presently parsing a template-argument-list. */
1469 bool in_template_argument_list_p;
1470
8487df40 1471 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472 to IN_OMP_BLOCK if parsing OpenMP structured block and
1473 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1474 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475 iteration-statement, OpenMP block or loop within that switch. */
1476#define IN_SWITCH_STMT 1
1477#define IN_ITERATION_STMT 2
1478#define IN_OMP_BLOCK 4
1479#define IN_OMP_FOR 8
2672c56c 1480#define IN_IF_STMT 16
8487df40 1481 unsigned char in_statement;
1482
1483 /* TRUE if we are presently parsing the body of a switch statement.
1484 Note that this doesn't quite overlap with in_statement above.
1485 The difference relates to giving the right sets of error messages:
1486 "case not in switch" vs "break statement used with OpenMP...". */
c3fbce20 1487 bool in_switch_statement_p;
1488
41f2d08e 1489 /* TRUE if we are parsing a type-id in an expression context. In
1490 such a situation, both "type (expr)" and "type (type)" are valid
1491 alternatives. */
1492 bool in_type_id_in_expr_p;
1493
2e1f41a9 1494 /* TRUE if we are currently in a header file where declarations are
93523877 1495 implicitly extern "C". */
2e1f41a9 1496 bool implicit_extern_c;
1497
00d26680 1498 /* TRUE if strings in expressions should be translated to the execution
1499 character set. */
1500 bool translate_strings_p;
1501
0aeb1cc5 1502 /* TRUE if we are presently parsing the body of a function, but not
1503 a local class. */
1504 bool in_function_body;
1505
0a3b29ad 1506 /* If non-NULL, then we are parsing a construct where new type
1507 definitions are not permitted. The string stored here will be
1508 issued as an error message if a type is defined. */
1509 const char *type_definition_forbidden_message;
1510
69b6679c 1511 /* A list of lists. The outer list is a stack, used for member
1512 functions of local classes. At each level there are two sub-list,
1513 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515 TREE_VALUE's. The functions are chained in reverse declaration
1516 order.
1517
1518 The TREE_PURPOSE sublist contains those functions with default
1519 arguments that need post processing, and the TREE_VALUE sublist
1520 contains those functions with definitions that need post
1521 processing.
1522
1523 These lists can only be processed once the outermost class being
6beb3f76 1524 defined is complete. */
0a3b29ad 1525 tree unparsed_functions_queues;
1526
1527 /* The number of classes whose definitions are currently in
1528 progress. */
1529 unsigned num_classes_being_defined;
1530
1531 /* The number of template parameter lists that apply directly to the
1532 current declaration. */
1533 unsigned num_template_parameter_lists;
1534} cp_parser;
1535
0a3b29ad 1536/* Prototypes. */
1537
1538/* Constructors and destructors. */
1539
1540static cp_parser *cp_parser_new
45baea8b 1541 (void);
0a3b29ad 1542
ccb84981 1543/* Routines to parse various constructs.
0a3b29ad 1544
1545 Those that return `tree' will return the error_mark_node (rather
1546 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547 Sometimes, they will return an ordinary node if error-recovery was
755edffd 1548 attempted, even though a parse error occurred. So, to check
0a3b29ad 1549 whether or not a parse error occurred, you should always use
1550 cp_parser_error_occurred. If the construct is optional (indicated
1551 either by an `_opt' in the name of the function that does the
1552 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553 the construct is not present. */
1554
1555/* Lexical conventions [gram.lex] */
1556
1557static tree cp_parser_identifier
45baea8b 1558 (cp_parser *);
00d26680 1559static tree cp_parser_string_literal
1560 (cp_parser *, bool, bool);
0a3b29ad 1561
1562/* Basic concepts [gram.basic] */
1563
1564static bool cp_parser_translation_unit
45baea8b 1565 (cp_parser *);
0a3b29ad 1566
1567/* Expressions [gram.expr] */
1568
1569static tree cp_parser_primary_expression
fbb01da7 1570 (cp_parser *, bool, bool, bool, cp_id_kind *);
0a3b29ad 1571static tree cp_parser_id_expression
130bb1d4 1572 (cp_parser *, bool, bool, bool *, bool, bool);
0a3b29ad 1573static tree cp_parser_unqualified_id
130bb1d4 1574 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1575static tree cp_parser_nested_name_specifier_opt
3d0f901b 1576 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1577static tree cp_parser_nested_name_specifier
0a3b29ad 1578 (cp_parser *, bool, bool, bool, bool);
3f00a6c0 1579static tree cp_parser_qualifying_entity
3d0f901b 1580 (cp_parser *, bool, bool, bool, bool, bool);
0a3b29ad 1581static tree cp_parser_postfix_expression
98b326fd 1582 (cp_parser *, bool, bool, bool, cp_id_kind *);
43bf5d72 1583static tree cp_parser_postfix_open_square_expression
1584 (cp_parser *, tree, bool);
1585static tree cp_parser_postfix_dot_deref_expression
ad9ae192 1586 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
f352a3fb 1587static VEC(tree,gc) *cp_parser_parenthesized_expression_list
d95d815d 1588 (cp_parser *, bool, bool, bool, bool *);
0a3b29ad 1589static void cp_parser_pseudo_destructor_name
45baea8b 1590 (cp_parser *, tree *, tree *);
0a3b29ad 1591static tree cp_parser_unary_expression
98b326fd 1592 (cp_parser *, bool, bool, cp_id_kind *);
0a3b29ad 1593static enum tree_code cp_parser_unary_operator
45baea8b 1594 (cp_token *);
0a3b29ad 1595static tree cp_parser_new_expression
45baea8b 1596 (cp_parser *);
f352a3fb 1597static VEC(tree,gc) *cp_parser_new_placement
45baea8b 1598 (cp_parser *);
0a3b29ad 1599static tree cp_parser_new_type_id
3046c0a3 1600 (cp_parser *, tree *);
1601static cp_declarator *cp_parser_new_declarator_opt
45baea8b 1602 (cp_parser *);
3046c0a3 1603static cp_declarator *cp_parser_direct_new_declarator
45baea8b 1604 (cp_parser *);
f352a3fb 1605static VEC(tree,gc) *cp_parser_new_initializer
45baea8b 1606 (cp_parser *);
0a3b29ad 1607static tree cp_parser_delete_expression
45baea8b 1608 (cp_parser *);
ccb84981 1609static tree cp_parser_cast_expression
98b326fd 1610 (cp_parser *, bool, bool, cp_id_kind *);
0a88af73 1611static tree cp_parser_binary_expression
4390875c 1612 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
0a3b29ad 1613static tree cp_parser_question_colon_clause
45baea8b 1614 (cp_parser *, tree);
0a3b29ad 1615static tree cp_parser_assignment_expression
98b326fd 1616 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1617static enum tree_code cp_parser_assignment_operator_opt
45baea8b 1618 (cp_parser *);
0a3b29ad 1619static tree cp_parser_expression
98b326fd 1620 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1621static tree cp_parser_constant_expression
5f6526e1 1622 (cp_parser *, bool, bool *);
43bf5d72 1623static tree cp_parser_builtin_offsetof
1624 (cp_parser *);
0a3b29ad 1625
1626/* Statements [gram.stmt.stmt] */
1627
1628static void cp_parser_statement
e534436e 1629 (cp_parser *, tree, bool, bool *);
17d53949 1630static void cp_parser_label_for_labeled_statement
1631 (cp_parser *);
0a3b29ad 1632static tree cp_parser_expression_statement
2363ef00 1633 (cp_parser *, tree);
0a3b29ad 1634static tree cp_parser_compound_statement
2363ef00 1635 (cp_parser *, tree, bool);
0a3b29ad 1636static void cp_parser_statement_seq_opt
2363ef00 1637 (cp_parser *, tree);
0a3b29ad 1638static tree cp_parser_selection_statement
e534436e 1639 (cp_parser *, bool *);
0a3b29ad 1640static tree cp_parser_condition
45baea8b 1641 (cp_parser *);
0a3b29ad 1642static tree cp_parser_iteration_statement
45baea8b 1643 (cp_parser *);
0a3b29ad 1644static void cp_parser_for_init_statement
45baea8b 1645 (cp_parser *);
0a3b29ad 1646static tree cp_parser_jump_statement
45baea8b 1647 (cp_parser *);
0a3b29ad 1648static void cp_parser_declaration_statement
45baea8b 1649 (cp_parser *);
0a3b29ad 1650
1651static tree cp_parser_implicitly_scoped_statement
e534436e 1652 (cp_parser *, bool *);
0a3b29ad 1653static void cp_parser_already_scoped_statement
45baea8b 1654 (cp_parser *);
0a3b29ad 1655
1656/* Declarations [gram.dcl.dcl] */
1657
1658static void cp_parser_declaration_seq_opt
45baea8b 1659 (cp_parser *);
0a3b29ad 1660static void cp_parser_declaration
45baea8b 1661 (cp_parser *);
0a3b29ad 1662static void cp_parser_block_declaration
45baea8b 1663 (cp_parser *, bool);
0a3b29ad 1664static void cp_parser_simple_declaration
45baea8b 1665 (cp_parser *, bool);
4b9b2871 1666static void cp_parser_decl_specifier_seq
1667 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
0a3b29ad 1668static tree cp_parser_storage_class_specifier_opt
45baea8b 1669 (cp_parser *);
0a3b29ad 1670static tree cp_parser_function_specifier_opt
4b9b2871 1671 (cp_parser *, cp_decl_specifier_seq *);
0a3b29ad 1672static tree cp_parser_type_specifier
207355ad 1673 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
4b9b2871 1674 int *, bool *);
0a3b29ad 1675static tree cp_parser_simple_type_specifier
4b9b2871 1676 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
0a3b29ad 1677static tree cp_parser_type_name
45baea8b 1678 (cp_parser *);
674e90bd 1679static tree cp_parser_nonclass_name
1680 (cp_parser* parser);
0a3b29ad 1681static tree cp_parser_elaborated_type_specifier
45baea8b 1682 (cp_parser *, bool, bool);
0a3b29ad 1683static tree cp_parser_enum_specifier
45baea8b 1684 (cp_parser *);
0a3b29ad 1685static void cp_parser_enumerator_list
45baea8b 1686 (cp_parser *, tree);
ccb84981 1687static void cp_parser_enumerator_definition
45baea8b 1688 (cp_parser *, tree);
0a3b29ad 1689static tree cp_parser_namespace_name
45baea8b 1690 (cp_parser *);
0a3b29ad 1691static void cp_parser_namespace_definition
45baea8b 1692 (cp_parser *);
0a3b29ad 1693static void cp_parser_namespace_body
45baea8b 1694 (cp_parser *);
0a3b29ad 1695static tree cp_parser_qualified_namespace_specifier
45baea8b 1696 (cp_parser *);
0a3b29ad 1697static void cp_parser_namespace_alias_definition
45baea8b 1698 (cp_parser *);
da2a3271 1699static bool cp_parser_using_declaration
1700 (cp_parser *, bool);
0a3b29ad 1701static void cp_parser_using_directive
45baea8b 1702 (cp_parser *);
0a3b29ad 1703static void cp_parser_asm_definition
45baea8b 1704 (cp_parser *);
0a3b29ad 1705static void cp_parser_linkage_specification
45baea8b 1706 (cp_parser *);
7a05c4b1 1707static void cp_parser_static_assert
1708 (cp_parser *, bool);
34da8800 1709static tree cp_parser_decltype
1710 (cp_parser *);
0a3b29ad 1711
1712/* Declarators [gram.dcl.decl] */
1713
1714static tree cp_parser_init_declarator
3369eb76 1715 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
3046c0a3 1716static cp_declarator *cp_parser_declarator
08ea345c 1717 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
3046c0a3 1718static cp_declarator *cp_parser_direct_declarator
08ea345c 1719 (cp_parser *, cp_parser_declarator_kind, int *, bool);
0a3b29ad 1720static enum tree_code cp_parser_ptr_operator
2cfb6cde 1721 (cp_parser *, tree *, cp_cv_quals *);
1722static cp_cv_quals cp_parser_cv_qualifier_seq_opt
45baea8b 1723 (cp_parser *);
346e3a9c 1724static tree cp_parser_late_return_type_opt
1725 (cp_parser *);
0a3b29ad 1726static tree cp_parser_declarator_id
197c9df7 1727 (cp_parser *, bool);
0a3b29ad 1728static tree cp_parser_type_id
45baea8b 1729 (cp_parser *);
75eaa947 1730static tree cp_parser_template_type_arg
1731 (cp_parser *);
1732static tree cp_parser_type_id_1
1733 (cp_parser *, bool);
4b9b2871 1734static void cp_parser_type_specifier_seq
6f74fe3c 1735 (cp_parser *, bool, cp_decl_specifier_seq *);
34eac767 1736static tree cp_parser_parameter_declaration_clause
45baea8b 1737 (cp_parser *);
34eac767 1738static tree cp_parser_parameter_declaration_list
3046c0a3 1739 (cp_parser *, bool *);
1740static cp_parameter_declarator *cp_parser_parameter_declaration
92b128ed 1741 (cp_parser *, bool, bool *);
41341abd 1742static tree cp_parser_default_argument
1743 (cp_parser *, bool);
0a3b29ad 1744static void cp_parser_function_body
1745 (cp_parser *);
1746static tree cp_parser_initializer
878870b4 1747 (cp_parser *, bool *, bool *);
0a3b29ad 1748static tree cp_parser_initializer_clause
878870b4 1749 (cp_parser *, bool *);
f82f1250 1750static tree cp_parser_braced_list
1751 (cp_parser*, bool*);
c75b4594 1752static VEC(constructor_elt,gc) *cp_parser_initializer_list
878870b4 1753 (cp_parser *, bool *);
0a3b29ad 1754
1755static bool cp_parser_ctor_initializer_opt_and_function_body
1756 (cp_parser *);
1757
1758/* Classes [gram.class] */
1759
1760static tree cp_parser_class_name
e2ae55f2 1761 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
0a3b29ad 1762static tree cp_parser_class_specifier
45baea8b 1763 (cp_parser *);
0a3b29ad 1764static tree cp_parser_class_head
1a9b4c25 1765 (cp_parser *, bool *, tree *, tree *);
0a3b29ad 1766static enum tag_types cp_parser_class_key
45baea8b 1767 (cp_parser *);
0a3b29ad 1768static void cp_parser_member_specification_opt
45baea8b 1769 (cp_parser *);
0a3b29ad 1770static void cp_parser_member_declaration
45baea8b 1771 (cp_parser *);
0a3b29ad 1772static tree cp_parser_pure_specifier
45baea8b 1773 (cp_parser *);
0a3b29ad 1774static tree cp_parser_constant_initializer
45baea8b 1775 (cp_parser *);
0a3b29ad 1776
1777/* Derived classes [gram.class.derived] */
1778
1779static tree cp_parser_base_clause
45baea8b 1780 (cp_parser *);
0a3b29ad 1781static tree cp_parser_base_specifier
45baea8b 1782 (cp_parser *);
0a3b29ad 1783
1784/* Special member functions [gram.special] */
1785
1786static tree cp_parser_conversion_function_id
45baea8b 1787 (cp_parser *);
0a3b29ad 1788static tree cp_parser_conversion_type_id
45baea8b 1789 (cp_parser *);
3046c0a3 1790static cp_declarator *cp_parser_conversion_declarator_opt
45baea8b 1791 (cp_parser *);
0a3b29ad 1792static bool cp_parser_ctor_initializer_opt
45baea8b 1793 (cp_parser *);
0a3b29ad 1794static void cp_parser_mem_initializer_list
45baea8b 1795 (cp_parser *);
0a3b29ad 1796static tree cp_parser_mem_initializer
45baea8b 1797 (cp_parser *);
0a3b29ad 1798static tree cp_parser_mem_initializer_id
45baea8b 1799 (cp_parser *);
0a3b29ad 1800
1801/* Overloading [gram.over] */
1802
1803static tree cp_parser_operator_function_id
45baea8b 1804 (cp_parser *);
0a3b29ad 1805static tree cp_parser_operator
45baea8b 1806 (cp_parser *);
0a3b29ad 1807
1808/* Templates [gram.temp] */
1809
1810static void cp_parser_template_declaration
45baea8b 1811 (cp_parser *, bool);
0a3b29ad 1812static tree cp_parser_template_parameter_list
45baea8b 1813 (cp_parser *);
0a3b29ad 1814static tree cp_parser_template_parameter
d95d815d 1815 (cp_parser *, bool *, bool *);
0a3b29ad 1816static tree cp_parser_type_parameter
d95d815d 1817 (cp_parser *, bool *);
0a3b29ad 1818static tree cp_parser_template_id
3d0f901b 1819 (cp_parser *, bool, bool, bool);
0a3b29ad 1820static tree cp_parser_template_name
3d0f901b 1821 (cp_parser *, bool, bool, bool, bool *);
0a3b29ad 1822static tree cp_parser_template_argument_list
45baea8b 1823 (cp_parser *);
0a3b29ad 1824static tree cp_parser_template_argument
45baea8b 1825 (cp_parser *);
0a3b29ad 1826static void cp_parser_explicit_instantiation
45baea8b 1827 (cp_parser *);
0a3b29ad 1828static void cp_parser_explicit_specialization
45baea8b 1829 (cp_parser *);
0a3b29ad 1830
1831/* Exception handling [gram.exception] */
1832
ccb84981 1833static tree cp_parser_try_block
45baea8b 1834 (cp_parser *);
0a3b29ad 1835static bool cp_parser_function_try_block
45baea8b 1836 (cp_parser *);
0a3b29ad 1837static void cp_parser_handler_seq
45baea8b 1838 (cp_parser *);
0a3b29ad 1839static void cp_parser_handler
45baea8b 1840 (cp_parser *);
0a3b29ad 1841static tree cp_parser_exception_declaration
45baea8b 1842 (cp_parser *);
0a3b29ad 1843static tree cp_parser_throw_expression
45baea8b 1844 (cp_parser *);
0a3b29ad 1845static tree cp_parser_exception_specification_opt
45baea8b 1846 (cp_parser *);
0a3b29ad 1847static tree cp_parser_type_id_list
45baea8b 1848 (cp_parser *);
0a3b29ad 1849
1850/* GNU Extensions */
1851
1852static tree cp_parser_asm_specification_opt
45baea8b 1853 (cp_parser *);
0a3b29ad 1854static tree cp_parser_asm_operand_list
45baea8b 1855 (cp_parser *);
0a3b29ad 1856static tree cp_parser_asm_clobber_list
45baea8b 1857 (cp_parser *);
0a3b29ad 1858static tree cp_parser_attributes_opt
45baea8b 1859 (cp_parser *);
0a3b29ad 1860static tree cp_parser_attribute_list
45baea8b 1861 (cp_parser *);
0a3b29ad 1862static bool cp_parser_extension_opt
45baea8b 1863 (cp_parser *, int *);
0a3b29ad 1864static void cp_parser_label_declaration
45baea8b 1865 (cp_parser *);
0a3b29ad 1866
b75b98aa 1867enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868static bool cp_parser_pragma
1869 (cp_parser *, enum pragma_context);
1870
7a4e126b 1871/* Objective-C++ Productions */
1872
1873static tree cp_parser_objc_message_receiver
1874 (cp_parser *);
1875static tree cp_parser_objc_message_args
1876 (cp_parser *);
1877static tree cp_parser_objc_message_expression
1878 (cp_parser *);
1879static tree cp_parser_objc_encode_expression
1880 (cp_parser *);
9031d10b 1881static tree cp_parser_objc_defs_expression
7a4e126b 1882 (cp_parser *);
1883static tree cp_parser_objc_protocol_expression
1884 (cp_parser *);
1885static tree cp_parser_objc_selector_expression
1886 (cp_parser *);
1887static tree cp_parser_objc_expression
1888 (cp_parser *);
1889static bool cp_parser_objc_selector_p
1890 (enum cpp_ttype);
1891static tree cp_parser_objc_selector
1892 (cp_parser *);
1893static tree cp_parser_objc_protocol_refs_opt
1894 (cp_parser *);
1895static void cp_parser_objc_declaration
1896 (cp_parser *);
1897static tree cp_parser_objc_statement
1898 (cp_parser *);
1899
0a3b29ad 1900/* Utility Routines */
1901
1902static tree cp_parser_lookup_name
ad9ae192 1903 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
0a3b29ad 1904static tree cp_parser_lookup_name_simple
ad9ae192 1905 (cp_parser *, tree, location_t);
0a3b29ad 1906static tree cp_parser_maybe_treat_template_as_class
1907 (tree, bool);
1908static bool cp_parser_check_declarator_template_parameters
ad9ae192 1909 (cp_parser *, cp_declarator *, location_t);
0a3b29ad 1910static bool cp_parser_check_template_parameters
7b07a15e 1911 (cp_parser *, unsigned, location_t, cp_declarator *);
a63bc44c 1912static tree cp_parser_simple_cast_expression
1913 (cp_parser *);
0a3b29ad 1914static tree cp_parser_global_scope_opt
130bb1d4 1915 (cp_parser *, bool);
0a3b29ad 1916static bool cp_parser_constructor_declarator_p
1917 (cp_parser *, bool);
1918static tree cp_parser_function_definition_from_specifiers_and_declarator
4b9b2871 1919 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
0a3b29ad 1920static tree cp_parser_function_definition_after_declarator
45baea8b 1921 (cp_parser *, bool);
0a3b29ad 1922static void cp_parser_template_declaration_after_export
45baea8b 1923 (cp_parser *, bool);
23010bc8 1924static void cp_parser_perform_template_parameter_access_checks
3369eb76 1925 (VEC (deferred_access_check,gc)*);
0a3b29ad 1926static tree cp_parser_single_declaration
0c032b46 1927 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
0a3b29ad 1928static tree cp_parser_functional_cast
45baea8b 1929 (cp_parser *, tree);
92b128ed 1930static tree cp_parser_save_member_function_body
4b9b2871 1931 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
8534c3a3 1932static tree cp_parser_enclosed_template_argument_list
1933 (cp_parser *);
69b6679c 1934static void cp_parser_save_default_args
1935 (cp_parser *, tree);
0a3b29ad 1936static void cp_parser_late_parsing_for_member
45baea8b 1937 (cp_parser *, tree);
0a3b29ad 1938static void cp_parser_late_parsing_default_args
af128372 1939 (cp_parser *, tree);
0a3b29ad 1940static tree cp_parser_sizeof_operand
45baea8b 1941 (cp_parser *, enum rid);
481451eb 1942static tree cp_parser_trait_expr
1943 (cp_parser *, enum rid);
0a3b29ad 1944static bool cp_parser_declares_only_class_p
45baea8b 1945 (cp_parser *);
4b9b2871 1946static void cp_parser_set_storage_class
ad9ae192 1947 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
207355ad 1948static void cp_parser_set_decl_spec_type
eef0ab03 1949 (cp_decl_specifier_seq *, tree, location_t, bool);
0a3b29ad 1950static bool cp_parser_friend_p
4b9b2871 1951 (const cp_decl_specifier_seq *);
0a3b29ad 1952static cp_token *cp_parser_require
45baea8b 1953 (cp_parser *, enum cpp_ttype, const char *);
0a3b29ad 1954static cp_token *cp_parser_require_keyword
45baea8b 1955 (cp_parser *, enum rid, const char *);
ccb84981 1956static bool cp_parser_token_starts_function_definition_p
45baea8b 1957 (cp_token *);
0a3b29ad 1958static bool cp_parser_next_token_starts_class_definition_p
1959 (cp_parser *);
13795292 1960static bool cp_parser_next_token_ends_template_argument_p
1961 (cp_parser *);
c8d5ab79 1962static bool cp_parser_nth_token_starts_template_argument_list_p
1963 (cp_parser *, size_t);
0a3b29ad 1964static enum tag_types cp_parser_token_is_class_key
45baea8b 1965 (cp_token *);
0a3b29ad 1966static void cp_parser_check_class_key
1967 (enum tag_types, tree type);
7e35473e 1968static void cp_parser_check_access_in_redeclaration
ad9ae192 1969 (tree type, location_t location);
0a3b29ad 1970static bool cp_parser_optional_template_keyword
1971 (cp_parser *);
ccb84981 1972static void cp_parser_pre_parsed_nested_name_specifier
b3c48b5d 1973 (cp_parser *);
f82f1250 1974static bool cp_parser_cache_group
00d26680 1975 (cp_parser *, enum cpp_ttype, unsigned);
ccb84981 1976static void cp_parser_parse_tentatively
45baea8b 1977 (cp_parser *);
0a3b29ad 1978static void cp_parser_commit_to_tentative_parse
45baea8b 1979 (cp_parser *);
0a3b29ad 1980static void cp_parser_abort_tentative_parse
45baea8b 1981 (cp_parser *);
0a3b29ad 1982static bool cp_parser_parse_definitely
45baea8b 1983 (cp_parser *);
2370b5bf 1984static inline bool cp_parser_parsing_tentatively
45baea8b 1985 (cp_parser *);
efcbcf83 1986static bool cp_parser_uncommitted_to_tentative_parse_p
45baea8b 1987 (cp_parser *);
0a3b29ad 1988static void cp_parser_error
45baea8b 1989 (cp_parser *, const char *);
92b128ed 1990static void cp_parser_name_lookup_error
ad9ae192 1991 (cp_parser *, tree, tree, const char *, location_t);
2c593bd0 1992static bool cp_parser_simulate_error
45baea8b 1993 (cp_parser *);
9a7c4b43 1994static bool cp_parser_check_type_definition
45baea8b 1995 (cp_parser *);
8172be22 1996static void cp_parser_check_for_definition_in_return_type
eef0ab03 1997 (cp_declarator *, tree, location_t type_location);
1157e9f0 1998static void cp_parser_check_for_invalid_template_id
eef0ab03 1999 (cp_parser *, tree, location_t location);
3938e0c2 2000static bool cp_parser_non_integral_constant_expression
2001 (cp_parser *, const char *);
e00c3963 2002static void cp_parser_diagnose_invalid_type_name
ad9ae192 2003 (cp_parser *, tree, tree, location_t);
e00c3963 2004static bool cp_parser_parse_and_diagnose_invalid_type_name
954ad420 2005 (cp_parser *);
0986fa22 2006static int cp_parser_skip_to_closing_parenthesis
3d0f901b 2007 (cp_parser *, bool, bool, bool);
0a3b29ad 2008static void cp_parser_skip_to_end_of_statement
45baea8b 2009 (cp_parser *);
cf91b86a 2010static void cp_parser_consume_semicolon_at_end_of_statement
2011 (cp_parser *);
0a3b29ad 2012static void cp_parser_skip_to_end_of_block_or_statement
45baea8b 2013 (cp_parser *);
9aad947b 2014static bool cp_parser_skip_to_closing_brace
0a3b29ad 2015 (cp_parser *);
c42e0e2d 2016static void cp_parser_skip_to_end_of_template_parameter_list
2017 (cp_parser *);
b75b98aa 2018static void cp_parser_skip_to_pragma_eol
2019 (cp_parser*, cp_token *);
0a3b29ad 2020static bool cp_parser_error_occurred
45baea8b 2021 (cp_parser *);
0a3b29ad 2022static bool cp_parser_allow_gnu_extensions_p
45baea8b 2023 (cp_parser *);
0a3b29ad 2024static bool cp_parser_is_string_literal
45baea8b 2025 (cp_token *);
ccb84981 2026static bool cp_parser_is_keyword
45baea8b 2027 (cp_token *, enum rid);
e00c3963 2028static tree cp_parser_make_typename_type
ad9ae192 2029 (cp_parser *, tree, tree, location_t location);
63949b38 2030static cp_declarator * cp_parser_make_indirect_declarator
2031 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
0a3b29ad 2032
f1d555e3 2033/* Returns nonzero if we are parsing tentatively. */
2370b5bf 2034
2035static inline bool
45baea8b 2036cp_parser_parsing_tentatively (cp_parser* parser)
2370b5bf 2037{
2038 return parser->context->next != NULL;
2039}
2040
f1d555e3 2041/* Returns nonzero if TOKEN is a string literal. */
0a3b29ad 2042
2043static bool
45baea8b 2044cp_parser_is_string_literal (cp_token* token)
0a3b29ad 2045{
924bbf02 2046 return (token->type == CPP_STRING ||
2047 token->type == CPP_STRING16 ||
2048 token->type == CPP_STRING32 ||
2049 token->type == CPP_WSTRING);
0a3b29ad 2050}
2051
f1d555e3 2052/* Returns nonzero if TOKEN is the indicated KEYWORD. */
0a3b29ad 2053
2054static bool
45baea8b 2055cp_parser_is_keyword (cp_token* token, enum rid keyword)
0a3b29ad 2056{
2057 return token->keyword == keyword;
2058}
2059
b9dd3954 2060/* If not parsing tentatively, issue a diagnostic of the form
2061 FILE:LINE: MESSAGE before TOKEN
2062 where TOKEN is the next token in the input stream. MESSAGE
2063 (specified by the caller) is usually of the form "expected
2064 OTHER-TOKEN". */
0a3b29ad 2065
2066static void
45baea8b 2067cp_parser_error (cp_parser* parser, const char* message)
0a3b29ad 2068{
2c593bd0 2069 if (!cp_parser_simulate_error (parser))
92b128ed 2070 {
b9dd3954 2071 cp_token *token = cp_lexer_peek_token (parser->lexer);
2072 /* This diagnostic makes more sense if it is tagged to the line
2073 of the token we just peeked at. */
2074 cp_lexer_set_source_position_from_token (token);
b75b98aa 2075
eb78e86f 2076 if (token->type == CPP_PRAGMA)
2077 {
ad9ae192 2078 error ("%H%<#pragma%> is not allowed here", &token->location);
b75b98aa 2079 cp_parser_skip_to_pragma_eol (parser, token);
eb78e86f 2080 return;
2081 }
b75b98aa 2082
ccb84981 2083 c_parse_error (message,
78662158 2084 /* Because c_parser_error does not understand
2085 CPP_KEYWORD, keywords are treated like
2086 identifiers. */
ccb84981 2087 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
ba99525e 2088 token->u.value, token->flags);
92b128ed 2089 }
2090}
2091
2092/* Issue an error about name-lookup failing. NAME is the
2093 IDENTIFIER_NODE DECL is the result of
2094 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2095 the thing that we hoped to find. */
2096
2097static void
2098cp_parser_name_lookup_error (cp_parser* parser,
2099 tree name,
2100 tree decl,
ad9ae192 2101 const char* desired,
2102 location_t location)
92b128ed 2103{
2104 /* If name lookup completely failed, tell the user that NAME was not
2105 declared. */
2106 if (decl == error_mark_node)
2107 {
2108 if (parser->scope && parser->scope != global_namespace)
ad9ae192 2109 error ("%H%<%E::%E%> has not been declared",
2110 &location, parser->scope, name);
92b128ed 2111 else if (parser->scope == global_namespace)
ad9ae192 2112 error ("%H%<::%E%> has not been declared", &location, name);
9031d10b 2113 else if (parser->object_scope
30aea172 2114 && !CLASS_TYPE_P (parser->object_scope))
ad9ae192 2115 error ("%Hrequest for member %qE in non-class type %qT",
2116 &location, name, parser->object_scope);
30aea172 2117 else if (parser->object_scope)
ad9ae192 2118 error ("%H%<%T::%E%> has not been declared",
2119 &location, parser->object_scope, name);
92b128ed 2120 else
ad9ae192 2121 error ("%H%qE has not been declared", &location, name);
92b128ed 2122 }
2123 else if (parser->scope && parser->scope != global_namespace)
ad9ae192 2124 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
92b128ed 2125 else if (parser->scope == global_namespace)
ad9ae192 2126 error ("%H%<::%E%> %s", &location, name, desired);
92b128ed 2127 else
ad9ae192 2128 error ("%H%qE %s", &location, name, desired);
0a3b29ad 2129}
2130
2131/* If we are parsing tentatively, remember that an error has occurred
2c593bd0 2132 during this tentative parse. Returns true if the error was
e24657db 2133 simulated; false if a message should be issued by the caller. */
0a3b29ad 2134
2c593bd0 2135static bool
45baea8b 2136cp_parser_simulate_error (cp_parser* parser)
0a3b29ad 2137{
efcbcf83 2138 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2c593bd0 2139 {
2140 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2141 return true;
2142 }
2143 return false;
0a3b29ad 2144}
2145
639b2fed 2146/* Check for repeated decl-specifiers. */
2147
2148static void
ad9ae192 2149cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2150 location_t location)
639b2fed 2151{
9f1b7d17 2152 int ds;
639b2fed 2153
2154 for (ds = ds_first; ds != ds_last; ++ds)
2155 {
9f1b7d17 2156 unsigned count = decl_specs->specs[ds];
639b2fed 2157 if (count < 2)
2158 continue;
2159 /* The "long" specifier is a special case because of "long long". */
2160 if (ds == ds_long)
2161 {
2162 if (count > 2)
ad9ae192 2163 error ("%H%<long long long%> is too long for GCC", &location);
9ab71c6b 2164 else
2165 pedwarn_cxx98 (location, OPT_Wlong_long,
2166 "ISO C++ 1998 does not support %<long long%>");
639b2fed 2167 }
2168 else if (count > 1)
2169 {
2170 static const char *const decl_spec_names[] = {
2171 "signed",
2172 "unsigned",
2173 "short",
2174 "long",
2175 "const",
2176 "volatile",
2177 "restrict",
2178 "inline",
2179 "virtual",
2180 "explicit",
2181 "friend",
2182 "typedef",
2183 "__complex",
2184 "__thread"
2185 };
9f1b7d17 2186 error ("%Hduplicate %qs", &location, decl_spec_names[ds]);
639b2fed 2187 }
2188 }
2189}
2190
0a3b29ad 2191/* This function is called when a type is defined. If type
2192 definitions are forbidden at this point, an error message is
2193 issued. */
2194
9a7c4b43 2195static bool
45baea8b 2196cp_parser_check_type_definition (cp_parser* parser)
0a3b29ad 2197{
2198 /* If types are forbidden here, issue a message. */
2199 if (parser->type_definition_forbidden_message)
9a7c4b43 2200 {
2e52ac87 2201 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202 in the message need to be interpreted. */
2203 error (parser->type_definition_forbidden_message);
9a7c4b43 2204 return false;
2205 }
2206 return true;
0a3b29ad 2207}
2208
e2ae55f2 2209/* This function is called when the DECLARATOR is processed. The TYPE
0be5f5cc 2210 was a type defined in the decl-specifiers. If it is invalid to
e2ae55f2 2211 define a type in the decl-specifiers for DECLARATOR, an error is
eef0ab03 2212 issued. TYPE_LOCATION is the location of TYPE and is used
2213 for error reporting. */
8172be22 2214
2215static void
3046c0a3 2216cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
eef0ab03 2217 tree type, location_t type_location)
8172be22 2218{
2219 /* [dcl.fct] forbids type definitions in return types.
2220 Unfortunately, it's not easy to know whether or not we are
2221 processing a return type until after the fact. */
2222 while (declarator
3046c0a3 2223 && (declarator->kind == cdk_pointer
2224 || declarator->kind == cdk_reference
2225 || declarator->kind == cdk_ptrmem))
2226 declarator = declarator->declarator;
8172be22 2227 if (declarator
e2ae55f2 2228 && declarator->kind == cdk_function)
2229 {
eef0ab03 2230 error ("%Hnew types may not be defined in a return type", &type_location);
5bcc316e 2231 inform (type_location,
2232 "(perhaps a semicolon is missing after the definition of %qT)",
2233 type);
e2ae55f2 2234 }
8172be22 2235}
2236
1157e9f0 2237/* A type-specifier (TYPE) has been parsed which cannot be followed by
2238 "<" in any valid C++ program. If the next token is indeed "<",
2239 issue a message warning the user about what appears to be an
eef0ab03 2240 invalid attempt to form a template-id. LOCATION is the location
2241 of the type-specifier (TYPE) */
1157e9f0 2242
2243static void
ccb84981 2244cp_parser_check_for_invalid_template_id (cp_parser* parser,
eef0ab03 2245 tree type, location_t location)
1157e9f0 2246{
19273cc2 2247 cp_token_position start = 0;
1157e9f0 2248
2249 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2250 {
2251 if (TYPE_P (type))
eef0ab03 2252 error ("%H%qT is not a template", &location, type);
1157e9f0 2253 else if (TREE_CODE (type) == IDENTIFIER_NODE)
eef0ab03 2254 error ("%H%qE is not a template", &location, type);
1157e9f0 2255 else
eef0ab03 2256 error ("%Hinvalid template-id", &location);
1157e9f0 2257 /* Remember the location of the invalid "<". */
efcbcf83 2258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 2259 start = cp_lexer_token_position (parser->lexer, true);
1157e9f0 2260 /* Consume the "<". */
2261 cp_lexer_consume_token (parser->lexer);
2262 /* Parse the template arguments. */
2263 cp_parser_enclosed_template_argument_list (parser);
a5268b2f 2264 /* Permanently remove the invalid template arguments so that
1157e9f0 2265 this error message is not issued again. */
19273cc2 2266 if (start)
2267 cp_lexer_purge_tokens_after (parser->lexer, start);
1157e9f0 2268 }
2269}
2270
3938e0c2 2271/* If parsing an integral constant-expression, issue an error message
2272 about the fact that THING appeared and return true. Otherwise,
640aa28c 2273 return false. In either case, set
9031d10b 2274 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
5f6526e1 2275
3938e0c2 2276static bool
2277cp_parser_non_integral_constant_expression (cp_parser *parser,
2278 const char *thing)
5f6526e1 2279{
640aa28c 2280 parser->non_integral_constant_expression_p = true;
3938e0c2 2281 if (parser->integral_constant_expression_p)
2282 {
2283 if (!parser->allow_non_integral_constant_expression_p)
2284 {
7222be86 2285 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286 in the message need to be interpreted. */
2287 char *message = concat (thing,
2288 " cannot appear in a constant-expression",
2289 NULL);
2290 error (message);
2291 free (message);
3938e0c2 2292 return true;
2293 }
3938e0c2 2294 }
2295 return false;
5f6526e1 2296}
2297
d9da0685 2298/* Emit a diagnostic for an invalid type name. SCOPE is the
b41cf329 2299 qualifying scope (or NULL, if none) for ID. This function commits
2300 to the current active tentative parse, if any. (Otherwise, the
2301 problematic construct might be encountered again later, resulting
eef0ab03 2302 in duplicate error messages.) LOCATION is the location of ID. */
954ad420 2303
e00c3963 2304static void
ad9ae192 2305cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306 tree scope, tree id,
eef0ab03 2307 location_t location)
bb93aad0 2308{
2309 tree decl, old_scope;
e00c3963 2310 /* Try to lookup the identifier. */
2311 old_scope = parser->scope;
2312 parser->scope = scope;
eef0ab03 2313 decl = cp_parser_lookup_name_simple (parser, id, location);
e00c3963 2314 parser->scope = old_scope;
2315 /* If the lookup found a template-name, it means that the user forgot
e4bc96e2 2316 to specify an argument list. Emit a useful error message. */
e00c3963 2317 if (TREE_CODE (decl) == TEMPLATE_DECL)
eef0ab03 2318 error ("%Hinvalid use of template-name %qE without an argument list",
2319 &location, decl);
dea225ee 2320 else if (TREE_CODE (id) == BIT_NOT_EXPR)
eef0ab03 2321 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2b443576 2322 else if (TREE_CODE (decl) == TYPE_DECL)
2323 /* Something like 'unsigned A a;' */
eef0ab03 2324 error ("%Hinvalid combination of multiple type-specifiers",
2325 &location);
b62240d5 2326 else if (!parser->scope)
954ad420 2327 {
954ad420 2328 /* Issue an error message. */
eef0ab03 2329 error ("%H%qE does not name a type", &location, id);
954ad420 2330 /* If we're in a template class, it's possible that the user was
2331 referring to a type from a base class. For example:
2332
2333 template <typename T> struct A { typedef T X; };
2334 template <typename T> struct B : public A<T> { X x; };
2335
2336 The user should have said "typename A<T>::X". */
236d1d50 2337 if (processing_template_decl && current_class_type
2338 && TYPE_BINFO (current_class_type))
954ad420 2339 {
2340 tree b;
2341
2342 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2343 b;
2344 b = TREE_CHAIN (b))
2345 {
2346 tree base_type = BINFO_TYPE (b);
ccb84981 2347 if (CLASS_TYPE_P (base_type)
7e9a6a16 2348 && dependent_type_p (base_type))
954ad420 2349 {
2350 tree field;
2351 /* Go from a particular instantiation of the
2352 template (which will have an empty TYPE_FIELDs),
2353 to the main version. */
f0a4ea5b 2354 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
954ad420 2355 for (field = TYPE_FIELDS (base_type);
2356 field;
2357 field = TREE_CHAIN (field))
2358 if (TREE_CODE (field) == TYPE_DECL
e00c3963 2359 && DECL_NAME (field) == id)
954ad420 2360 {
5bcc316e 2361 inform (location,
2362 "(perhaps %<typename %T::%E%> was intended)",
2363 BINFO_TYPE (b), id);
954ad420 2364 break;
2365 }
2366 if (field)
2367 break;
2368 }
2369 }
2370 }
954ad420 2371 }
e00c3963 2372 /* Here we diagnose qualified-ids where the scope is actually correct,
2373 but the identifier does not resolve to a valid type name. */
b62240d5 2374 else if (parser->scope != error_mark_node)
e00c3963 2375 {
2376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
eef0ab03 2377 error ("%H%qE in namespace %qE does not name a type",
2378 &location, id, parser->scope);
e00c3963 2379 else if (TYPE_P (parser->scope))
eef0ab03 2380 error ("%H%qE in class %qT does not name a type",
2381 &location, id, parser->scope);
e00c3963 2382 else
2e3e31d2 2383 gcc_unreachable ();
e00c3963 2384 }
b41cf329 2385 cp_parser_commit_to_tentative_parse (parser);
e00c3963 2386}
954ad420 2387
e00c3963 2388/* Check for a common situation where a type-name should be present,
2389 but is not, and issue a sensible error message. Returns true if an
2390 invalid type-name was detected.
ccb84981 2391
e00c3963 2392 The situation handled by this function are variable declarations of the
ccb84981 2393 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394 Usually, `ID' should name a type, but if we got here it means that it
e00c3963 2395 does not. We try to emit the best possible error message depending on
074ab442 2396 how exactly the id-expression looks like. */
e00c3963 2397
2398static bool
2399cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2400{
2401 tree id;
ad9ae192 2402 cp_token *token = cp_lexer_peek_token (parser->lexer);
e00c3963 2403
2404 cp_parser_parse_tentatively (parser);
ccb84981 2405 id = cp_parser_id_expression (parser,
e00c3963 2406 /*template_keyword_p=*/false,
2407 /*check_dependency_p=*/true,
2408 /*template_p=*/NULL,
197c9df7 2409 /*declarator_p=*/true,
130bb1d4 2410 /*optional_p=*/false);
e00c3963 2411 /* After the id-expression, there should be a plain identifier,
2412 otherwise this is not a simple variable declaration. Also, if
2413 the scope is dependent, we cannot do much. */
2414 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
ccb84981 2415 || (parser->scope && TYPE_P (parser->scope)
7525f769 2416 && dependent_type_p (parser->scope))
2417 || TREE_CODE (id) == TYPE_DECL)
e00c3963 2418 {
2419 cp_parser_abort_tentative_parse (parser);
2420 return false;
2421 }
7525f769 2422 if (!cp_parser_parse_definitely (parser))
e00c3963 2423 return false;
2424
e00c3963 2425 /* Emit a diagnostic for the invalid type. */
ad9ae192 2426 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427 id, token->location);
e00c3963 2428 /* Skip to the end of the declaration; there's no point in
2429 trying to process it. */
2430 cp_parser_skip_to_end_of_block_or_statement (parser);
2431 return true;
954ad420 2432}
2433
ccb84981 2434/* Consume tokens up to, and including, the next non-nested closing `)'.
0986fa22 2435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2436 are doing error recovery. Returns -1 if OR_COMMA is true and we
2437 found an unnested comma. */
0a3b29ad 2438
0986fa22 2439static int
2440cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
ccb84981 2441 bool recovering,
3d0f901b 2442 bool or_comma,
2443 bool consume_paren)
0a3b29ad 2444{
0986fa22 2445 unsigned paren_depth = 0;
2446 unsigned brace_depth = 0;
0a3b29ad 2447
efcbcf83 2448 if (recovering && !or_comma
2449 && cp_parser_uncommitted_to_tentative_parse_p (parser))
0986fa22 2450 return 0;
ccb84981 2451
0a3b29ad 2452 while (true)
2453 {
b75b98aa 2454 cp_token * token = cp_lexer_peek_token (parser->lexer);
ccb84981 2455
b75b98aa 2456 switch (token->type)
3fe7c943 2457 {
b75b98aa 2458 case CPP_EOF:
2459 case CPP_PRAGMA_EOL:
2460 /* If we've run out of tokens, then there is no closing `)'. */
2461 return 0;
0a3b29ad 2462
b75b98aa 2463 case CPP_SEMICOLON:
2464 /* This matches the processing in skip_to_end_of_statement. */
2465 if (!brace_depth)
2466 return 0;
2467 break;
ccb84981 2468
b75b98aa 2469 case CPP_OPEN_BRACE:
2470 ++brace_depth;
3fe7c943 2471 break;
b75b98aa 2472 case CPP_CLOSE_BRACE:
3d0f901b 2473 if (!brace_depth--)
b75b98aa 2474 return 0;
3fe7c943 2475 break;
ccb84981 2476
b75b98aa 2477 case CPP_COMMA:
2478 if (recovering && or_comma && !brace_depth && !paren_depth)
2479 return -1;
2480 break;
2481
2482 case CPP_OPEN_PAREN:
2483 if (!brace_depth)
0986fa22 2484 ++paren_depth;
b75b98aa 2485 break;
2486
2487 case CPP_CLOSE_PAREN:
2488 if (!brace_depth && !paren_depth--)
3d0f901b 2489 {
2490 if (consume_paren)
2491 cp_lexer_consume_token (parser->lexer);
b75b98aa 2492 return 1;
3d0f901b 2493 }
b75b98aa 2494 break;
2495
2496 default:
2497 break;
0986fa22 2498 }
ccb84981 2499
3d0f901b 2500 /* Consume the token. */
2501 cp_lexer_consume_token (parser->lexer);
0a3b29ad 2502 }
2503}
2504
2505/* Consume tokens until we reach the end of the current statement.
2506 Normally, that will be just before consuming a `;'. However, if a
2507 non-nested `}' comes first, then we stop before consuming that. */
2508
2509static void
45baea8b 2510cp_parser_skip_to_end_of_statement (cp_parser* parser)
0a3b29ad 2511{
2512 unsigned nesting_depth = 0;
2513
2514 while (true)
2515 {
b75b98aa 2516 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 2517
b75b98aa 2518 switch (token->type)
0a3b29ad 2519 {
b75b98aa 2520 case CPP_EOF:
2521 case CPP_PRAGMA_EOL:
2522 /* If we've run out of tokens, stop. */
2523 return;
2524
2525 case CPP_SEMICOLON:
2526 /* If the next token is a `;', we have reached the end of the
2527 statement. */
2528 if (!nesting_depth)
2529 return;
2530 break;
2531
2532 case CPP_CLOSE_BRACE:
2533 /* If this is a non-nested '}', stop before consuming it.
0a3b29ad 2534 That way, when confronted with something like:
2535
ccb84981 2536 { 3 + }
0a3b29ad 2537
b75b98aa 2538 we stop before consuming the closing '}', even though we
0a3b29ad 2539 have not yet reached a `;'. */
2540 if (nesting_depth == 0)
b75b98aa 2541 return;
2542
2543 /* If it is the closing '}' for a block that we have
0a3b29ad 2544 scanned, stop -- but only after consuming the token.
2545 That way given:
2546
653e5405 2547 void f g () { ... }
0a3b29ad 2548 typedef int I;
2549
2550 we will stop after the body of the erroneously declared
2551 function, but before consuming the following `typedef'
2552 declaration. */
2553 if (--nesting_depth == 0)
2554 {
2555 cp_lexer_consume_token (parser->lexer);
b75b98aa 2556 return;
0a3b29ad 2557 }
b75b98aa 2558
2559 case CPP_OPEN_BRACE:
2560 ++nesting_depth;
2561 break;
2562
2563 default:
2564 break;
0a3b29ad 2565 }
b75b98aa 2566
0a3b29ad 2567 /* Consume the token. */
2568 cp_lexer_consume_token (parser->lexer);
2569 }
2570}
2571
cf91b86a 2572/* This function is called at the end of a statement or declaration.
2573 If the next token is a semicolon, it is consumed; otherwise, error
2574 recovery is attempted. */
2575
2576static void
2577cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2578{
2579 /* Look for the trailing `;'. */
640710cf 2580 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
cf91b86a 2581 {
2582 /* If there is additional (erroneous) input, skip to the end of
2583 the statement. */
2584 cp_parser_skip_to_end_of_statement (parser);
2585 /* If the next token is now a `;', consume it. */
2586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587 cp_lexer_consume_token (parser->lexer);
2588 }
2589}
2590
0a3b29ad 2591/* Skip tokens until we have consumed an entire block, or until we
2592 have consumed a non-nested `;'. */
2593
2594static void
45baea8b 2595cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
0a3b29ad 2596{
c75ae97e 2597 int nesting_depth = 0;
0a3b29ad 2598
c75ae97e 2599 while (nesting_depth >= 0)
0a3b29ad 2600 {
c75ae97e 2601 cp_token *token = cp_lexer_peek_token (parser->lexer);
9031d10b 2602
c75ae97e 2603 switch (token->type)
0a3b29ad 2604 {
c75ae97e 2605 case CPP_EOF:
b75b98aa 2606 case CPP_PRAGMA_EOL:
c75ae97e 2607 /* If we've run out of tokens, stop. */
b75b98aa 2608 return;
c75ae97e 2609
2610 case CPP_SEMICOLON:
2611 /* Stop if this is an unnested ';'. */
2612 if (!nesting_depth)
2613 nesting_depth = -1;
2614 break;
2615
2616 case CPP_CLOSE_BRACE:
2617 /* Stop if this is an unnested '}', or closes the outermost
2618 nesting level. */
2619 nesting_depth--;
926c5baf 2620 if (nesting_depth < 0)
2621 return;
c75ae97e 2622 if (!nesting_depth)
2623 nesting_depth = -1;
2624 break;
9031d10b 2625
c75ae97e 2626 case CPP_OPEN_BRACE:
2627 /* Nest. */
2628 nesting_depth++;
2629 break;
2630
2631 default:
0a3b29ad 2632 break;
2633 }
9031d10b 2634
0a3b29ad 2635 /* Consume the token. */
c75ae97e 2636 cp_lexer_consume_token (parser->lexer);
0a3b29ad 2637 }
2638}
2639
2640/* Skip tokens until a non-nested closing curly brace is the next
9aad947b 2641 token, or there are no more tokens. Return true in the first case,
2642 false otherwise. */
0a3b29ad 2643
9aad947b 2644static bool
0a3b29ad 2645cp_parser_skip_to_closing_brace (cp_parser *parser)
2646{
2647 unsigned nesting_depth = 0;
2648
2649 while (true)
2650 {
b75b98aa 2651 cp_token *token = cp_lexer_peek_token (parser->lexer);
2652
2653 switch (token->type)
2654 {
2655 case CPP_EOF:
2656 case CPP_PRAGMA_EOL:
2657 /* If we've run out of tokens, stop. */
9aad947b 2658 return false;
b75b98aa 2659
2660 case CPP_CLOSE_BRACE:
2661 /* If the next token is a non-nested `}', then we have reached
2662 the end of the current block. */
2663 if (nesting_depth-- == 0)
9aad947b 2664 return true;
b75b98aa 2665 break;
2666
2667 case CPP_OPEN_BRACE:
2668 /* If it the next token is a `{', then we are entering a new
2669 block. Consume the entire block. */
2670 ++nesting_depth;
2671 break;
2672
2673 default:
2674 break;
2675 }
0a3b29ad 2676
0a3b29ad 2677 /* Consume the token. */
2678 cp_lexer_consume_token (parser->lexer);
2679 }
2680}
2681
b75b98aa 2682/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2683 parameter is the PRAGMA token, allowing us to purge the entire pragma
2684 sequence. */
2685
2686static void
2687cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2688{
2689 cp_token *token;
2690
2691 parser->lexer->in_pragma = false;
2692
2693 do
2694 token = cp_lexer_consume_token (parser->lexer);
2695 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2696
2697 /* Ensure that the pragma is not parsed again. */
2698 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2699}
2700
8487df40 2701/* Require pragma end of line, resyncing with it as necessary. The
2702 arguments are as for cp_parser_skip_to_pragma_eol. */
2703
2704static void
2705cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2706{
2707 parser->lexer->in_pragma = false;
2708 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2710}
2711
e00c3963 2712/* This is a simple wrapper around make_typename_type. When the id is
2713 an unresolved identifier node, we can provide a superior diagnostic
2714 using cp_parser_diagnose_invalid_type_name. */
2715
2716static tree
ad9ae192 2717cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718 tree id, location_t id_location)
bb93aad0 2719{
2720 tree result;
2721 if (TREE_CODE (id) == IDENTIFIER_NODE)
2722 {
e2ae55f2 2723 result = make_typename_type (scope, id, typename_type,
074ab442 2724 /*complain=*/tf_none);
bb93aad0 2725 if (result == error_mark_node)
ad9ae192 2726 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
bb93aad0 2727 return result;
2728 }
e2ae55f2 2729 return make_typename_type (scope, id, typename_type, tf_error);
e00c3963 2730}
2731
63949b38 2732/* This is a wrapper around the
2733 make_{pointer,ptrmem,reference}_declarator functions that decides
2734 which one to call based on the CODE and CLASS_TYPE arguments. The
2735 CODE argument should be one of the values returned by
2736 cp_parser_ptr_operator. */
2737static cp_declarator *
2738cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739 cp_cv_quals cv_qualifiers,
2740 cp_declarator *target)
2741{
416c300e 2742 if (code == ERROR_MARK)
2743 return cp_error_declarator;
2744
63949b38 2745 if (code == INDIRECT_REF)
2746 if (class_type == NULL_TREE)
2747 return make_pointer_declarator (cv_qualifiers, target);
2748 else
2749 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751 return make_reference_declarator (cv_qualifiers, target, false);
2752 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, true);
2754 gcc_unreachable ();
2755}
e00c3963 2756
0a3b29ad 2757/* Create a new C++ parser. */
2758
2759static cp_parser *
45baea8b 2760cp_parser_new (void)
0a3b29ad 2761{
2762 cp_parser *parser;
573aba85 2763 cp_lexer *lexer;
0a88af73 2764 unsigned i;
573aba85 2765
2766 /* cp_lexer_new_main is called before calling ggc_alloc because
2767 cp_lexer_new_main might load a PCH file. */
2768 lexer = cp_lexer_new_main ();
0a3b29ad 2769
0a88af73 2770 /* Initialize the binops_by_token so that we can get the tree
2771 directly from the token. */
2772 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773 binops_by_token[binops[i].token_type] = binops[i];
2774
a33db04a 2775 parser = GGC_CNEW (cp_parser);
573aba85 2776 parser->lexer = lexer;
0a3b29ad 2777 parser->context = cp_parser_context_new (NULL);
2778
2779 /* For now, we always accept GNU extensions. */
2780 parser->allow_gnu_extensions_p = 1;
2781
2782 /* The `>' token is a greater-than operator, not the end of a
2783 template-id. */
2784 parser->greater_than_is_operator_p = true;
2785
2786 parser->default_arg_ok_p = true;
ccb84981 2787
0a3b29ad 2788 /* We are not parsing a constant-expression. */
f47c1747 2789 parser->integral_constant_expression_p = false;
2790 parser->allow_non_integral_constant_expression_p = false;
2791 parser->non_integral_constant_expression_p = false;
0a3b29ad 2792
2793 /* Local variable names are not forbidden. */
2794 parser->local_variables_forbidden_p = false;
2795
755edffd 2796 /* We are not processing an `extern "C"' declaration. */
0a3b29ad 2797 parser->in_unbraced_linkage_specification_p = false;
2798
2799 /* We are not processing a declarator. */
2800 parser->in_declarator_p = false;
2801
92b128ed 2802 /* We are not processing a template-argument-list. */
2803 parser->in_template_argument_list_p = false;
2804
c3fbce20 2805 /* We are not in an iteration statement. */
8487df40 2806 parser->in_statement = 0;
c3fbce20 2807
2808 /* We are not in a switch statement. */
2809 parser->in_switch_statement_p = false;
2810
41f2d08e 2811 /* We are not parsing a type-id inside an expression. */
2812 parser->in_type_id_in_expr_p = false;
2813
93523877 2814 /* Declarations aren't implicitly extern "C". */
2e1f41a9 2815 parser->implicit_extern_c = false;
2816
00d26680 2817 /* String literals should be translated to the execution character set. */
2818 parser->translate_strings_p = true;
2819
0aeb1cc5 2820 /* We are not parsing a function body. */
2821 parser->in_function_body = false;
2822
0a3b29ad 2823 /* The unparsed function queue is empty. */
2824 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2825
2826 /* There are no classes being defined. */
2827 parser->num_classes_being_defined = 0;
2828
2829 /* No template parameters apply. */
2830 parser->num_template_parameter_lists = 0;
2831
2832 return parser;
2833}
2834
b9dd3954 2835/* Create a cp_lexer structure which will emit the tokens in CACHE
2836 and push it onto the parser's lexer stack. This is used for delayed
2837 parsing of in-class method bodies and default arguments, and should
2838 not be confused with tentative parsing. */
2839static void
2840cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2841{
2842 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843 lexer->next = parser->lexer;
2844 parser->lexer = lexer;
2845
2846 /* Move the current source position to that of the first token in the
2847 new lexer. */
2848 cp_lexer_set_source_position_from_token (lexer->next_token);
2849}
2850
2851/* Pop the top lexer off the parser stack. This is never used for the
2852 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2853static void
2854cp_parser_pop_lexer (cp_parser *parser)
2855{
2856 cp_lexer *lexer = parser->lexer;
2857 parser->lexer = lexer->next;
2858 cp_lexer_destroy (lexer);
2859
2860 /* Put the current source position back where it was before this
2861 lexer was pushed. */
2862 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2863}
2864
0a3b29ad 2865/* Lexical conventions [gram.lex] */
2866
2867/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2868 identifier. */
2869
ccb84981 2870static tree
45baea8b 2871cp_parser_identifier (cp_parser* parser)
0a3b29ad 2872{
2873 cp_token *token;
2874
2875 /* Look for the identifier. */
2876 token = cp_parser_require (parser, CPP_NAME, "identifier");
2877 /* Return the value. */
3369eb76 2878 return token ? token->u.value : error_mark_node;
0a3b29ad 2879}
2880
00d26680 2881/* Parse a sequence of adjacent string constants. Returns a
2882 TREE_STRING representing the combined, nul-terminated string
2883 constant. If TRANSLATE is true, translate the string to the
2884 execution character set. If WIDE_OK is true, a wide string is
2885 invalid here.
2886
2887 C++98 [lex.string] says that if a narrow string literal token is
2888 adjacent to a wide string literal token, the behavior is undefined.
2889 However, C99 6.4.5p4 says that this results in a wide string literal.
2890 We follow C99 here, for consistency with the C front end.
2891
2892 This code is largely lifted from lex_string() in c-lex.c.
2893
2894 FUTURE: ObjC++ will need to handle @-strings here. */
2895static tree
2896cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2897{
2898 tree value;
00d26680 2899 size_t count;
2900 struct obstack str_ob;
2901 cpp_string str, istr, *strs;
2902 cp_token *tok;
924bbf02 2903 enum cpp_ttype type;
00d26680 2904
2905 tok = cp_lexer_peek_token (parser->lexer);
2906 if (!cp_parser_is_string_literal (tok))
2907 {
2908 cp_parser_error (parser, "expected string-literal");
2909 return error_mark_node;
2910 }
2911
924bbf02 2912 type = tok->type;
2913
2e533eb1 2914 /* Try to avoid the overhead of creating and destroying an obstack
00d26680 2915 for the common case of just one string. */
b9dd3954 2916 if (!cp_parser_is_string_literal
2917 (cp_lexer_peek_nth_token (parser->lexer, 2)))
00d26680 2918 {
b9dd3954 2919 cp_lexer_consume_token (parser->lexer);
2920
3369eb76 2921 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922 str.len = TREE_STRING_LENGTH (tok->u.value);
00d26680 2923 count = 1;
00d26680 2924
2925 strs = &str;
2926 }
2927 else
2928 {
2929 gcc_obstack_init (&str_ob);
2930 count = 0;
2931
2932 do
2933 {
b9dd3954 2934 cp_lexer_consume_token (parser->lexer);
00d26680 2935 count++;
dea3189b 2936 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3369eb76 2937 str.len = TREE_STRING_LENGTH (tok->u.value);
924bbf02 2938
2939 if (type != tok->type)
2940 {
2941 if (type == CPP_STRING)
2942 type = tok->type;
2943 else if (tok->type != CPP_STRING)
ad9ae192 2944 error ("%Hunsupported non-standard concatenation "
2945 "of string literals", &tok->location);
924bbf02 2946 }
00d26680 2947
2948 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2949
b9dd3954 2950 tok = cp_lexer_peek_token (parser->lexer);
00d26680 2951 }
2952 while (cp_parser_is_string_literal (tok));
2953
2954 strs = (cpp_string *) obstack_finish (&str_ob);
2955 }
2956
924bbf02 2957 if (type != CPP_STRING && !wide_ok)
00d26680 2958 {
2959 cp_parser_error (parser, "a wide string is invalid in this context");
924bbf02 2960 type = CPP_STRING;
00d26680 2961 }
2962
2963 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
924bbf02 2964 (parse_in, strs, count, &istr, type))
00d26680 2965 {
c21f4fcd 2966 value = build_string (istr.len, (const char *)istr.text);
e47a6f81 2967 free (CONST_CAST (unsigned char *, istr.text));
00d26680 2968
924bbf02 2969 switch (type)
2970 {
2971 default:
2972 case CPP_STRING:
2973 TREE_TYPE (value) = char_array_type_node;
2974 break;
2975 case CPP_STRING16:
2976 TREE_TYPE (value) = char16_array_type_node;
2977 break;
2978 case CPP_STRING32:
2979 TREE_TYPE (value) = char32_array_type_node;
2980 break;
2981 case CPP_WSTRING:
2982 TREE_TYPE (value) = wchar_array_type_node;
2983 break;
2984 }
2985
00d26680 2986 value = fix_string_type (value);
2987 }
2988 else
2989 /* cpp_interpret_string has issued an error. */
2990 value = error_mark_node;
2991
2992 if (count > 1)
2993 obstack_free (&str_ob, 0);
2994
2995 return value;
2996}
2997
2998
0a3b29ad 2999/* Basic concepts [gram.basic] */
3000
3001/* Parse a translation-unit.
3002
3003 translation-unit:
ccb84981 3004 declaration-seq [opt]
0a3b29ad 3005
3006 Returns TRUE if all went well. */
3007
3008static bool
45baea8b 3009cp_parser_translation_unit (cp_parser* parser)
0a3b29ad 3010{
3046c0a3 3011 /* The address of the first non-permanent object on the declarator
3012 obstack. */
3013 static void *declarator_obstack_base;
3014
3015 bool success;
207355ad 3016
3046c0a3 3017 /* Create the declarator obstack, if necessary. */
3018 if (!cp_error_declarator)
3019 {
3020 gcc_obstack_init (&declarator_obstack);
3021 /* Create the error declarator. */
3022 cp_error_declarator = make_declarator (cdk_error);
3023 /* Create the empty parameter list. */
4b9b2871 3024 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3046c0a3 3025 /* Remember where the base of the declarator obstack lies. */
3026 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3027 }
3028
d88386ce 3029 cp_parser_declaration_seq_opt (parser);
074ab442 3030
d88386ce 3031 /* If there are no tokens left then all went well. */
3032 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0a3b29ad 3033 {
d88386ce 3034 /* Get rid of the token array; we don't need it any more. */
3035 cp_lexer_destroy (parser->lexer);
3036 parser->lexer = NULL;
074ab442 3037
d88386ce 3038 /* This file might have been a context that's implicitly extern
074ab442 3039 "C". If so, pop the lang context. (Only relevant for PCH.) */
d88386ce 3040 if (parser->implicit_extern_c)
074ab442 3041 {
3042 pop_lang_context ();
3043 parser->implicit_extern_c = false;
3044 }
3045
d88386ce 3046 /* Finish up. */
3047 finish_translation_unit ();
074ab442 3048
d88386ce 3049 success = true;
0a3b29ad 3050 }
d88386ce 3051 else
3052 {
3053 cp_parser_error (parser, "expected declaration");
3054 success = false;
3055 }
074ab442 3056
3046c0a3 3057 /* Make sure the declarator obstack was fully cleaned up. */
b4df430b 3058 gcc_assert (obstack_next_free (&declarator_obstack)
3059 == declarator_obstack_base);
0a3b29ad 3060
3061 /* All went well. */
3046c0a3 3062 return success;
0a3b29ad 3063}
3064
3065/* Expressions [gram.expr] */
3066
3067/* Parse a primary-expression.
3068
3069 primary-expression:
3070 literal
3071 this
3072 ( expression )
3073 id-expression
3074
3075 GNU Extensions:
3076
3077 primary-expression:
3078 ( compound-statement )
3079 __builtin_va_arg ( assignment-expression , type-id )
e3a7cc2b 3080 __builtin_offsetof ( type-id , offsetof-expression )
0a3b29ad 3081
481451eb 3082 C++ Extensions:
3083 __has_nothrow_assign ( type-id )
3084 __has_nothrow_constructor ( type-id )
3085 __has_nothrow_copy ( type-id )
3086 __has_trivial_assign ( type-id )
3087 __has_trivial_constructor ( type-id )
3088 __has_trivial_copy ( type-id )
3089 __has_trivial_destructor ( type-id )
3090 __has_virtual_destructor ( type-id )
3091 __is_abstract ( type-id )
3092 __is_base_of ( type-id , type-id )
3093 __is_class ( type-id )
3094 __is_convertible_to ( type-id , type-id )
3095 __is_empty ( type-id )
3096 __is_enum ( type-id )
3097 __is_pod ( type-id )
3098 __is_polymorphic ( type-id )
3099 __is_union ( type-id )
3100
7a4e126b 3101 Objective-C++ Extension:
3102
3103 primary-expression:
3104 objc-expression
3105
0a3b29ad 3106 literal:
3107 __null
3108
fbb01da7 3109 ADDRESS_P is true iff this expression was immediately preceded by
3110 "&" and therefore might denote a pointer-to-member. CAST_P is true
3111 iff this expression is the target of a cast. TEMPLATE_ARG_P is
149fdb98 3112 true iff this expression is a template argument.
640aa28c 3113
fbb01da7 3114 Returns a representation of the expression. Upon return, *IDK
3115 indicates what kind of id-expression (if any) was present. */
0a3b29ad 3116
3117static tree
ccb84981 3118cp_parser_primary_expression (cp_parser *parser,
fbb01da7 3119 bool address_p,
640aa28c 3120 bool cast_p,
fbb01da7 3121 bool template_arg_p,
3122 cp_id_kind *idk)
0a3b29ad 3123{
ad9ae192 3124 cp_token *token = NULL;
0a3b29ad 3125
3126 /* Assume the primary expression is not an id-expression. */
0886adbc 3127 *idk = CP_ID_KIND_NONE;
0a3b29ad 3128
3129 /* Peek at the next token. */
3130 token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3132 {
3133 /* literal:
3134 integer-literal
3135 character-literal
3136 floating-literal
3137 string-literal
3138 boolean-literal */
3139 case CPP_CHAR:
924bbf02 3140 case CPP_CHAR16:
3141 case CPP_CHAR32:
0a3b29ad 3142 case CPP_WCHAR:
0a3b29ad 3143 case CPP_NUMBER:
3144 token = cp_lexer_consume_token (parser->lexer);
14e882ea 3145 if (TREE_CODE (token->u.value) == FIXED_CST)
3146 {
3147 error ("%Hfixed-point types not supported in C++",
3148 &token->location);
3149 return error_mark_node;
3150 }
640aa28c 3151 /* Floating-point literals are only allowed in an integral
3152 constant expression if they are cast to an integral or
3153 enumeration type. */
3369eb76 3154 if (TREE_CODE (token->u.value) == REAL_CST
b68a9fcf 3155 && parser->integral_constant_expression_p
3156 && pedantic)
640aa28c 3157 {
3158 /* CAST_P will be set even in invalid code like "int(2.7 +
3159 ...)". Therefore, we have to check that the next token
3160 is sure to end the cast. */
3161 if (cast_p)
3162 {
3163 cp_token *next_token;
3164
3165 next_token = cp_lexer_peek_token (parser->lexer);
3166 if (/* The comma at the end of an
3167 enumerator-definition. */
3168 next_token->type != CPP_COMMA
3169 /* The curly brace at the end of an enum-specifier. */
3170 && next_token->type != CPP_CLOSE_BRACE
3171 /* The end of a statement. */
3172 && next_token->type != CPP_SEMICOLON
3173 /* The end of the cast-expression. */
3174 && next_token->type != CPP_CLOSE_PAREN
3175 /* The end of an array bound. */
c6e71399 3176 && next_token->type != CPP_CLOSE_SQUARE
3177 /* The closing ">" in a template-argument-list. */
3178 && (next_token->type != CPP_GREATER
56471494 3179 || parser->greater_than_is_operator_p)
3180 /* C++0x only: A ">>" treated like two ">" tokens,
3181 in a template-argument-list. */
3182 && (next_token->type != CPP_RSHIFT
6dcdb5de 3183 || (cxx_dialect == cxx98)
c6e71399 3184 || parser->greater_than_is_operator_p))
640aa28c 3185 cast_p = false;
3186 }
3187
3188 /* If we are within a cast, then the constraint that the
3189 cast is to an integral or enumeration type will be
3190 checked at that point. If we are not within a cast, then
3191 this code is invalid. */
3192 if (!cast_p)
9031d10b 3193 cp_parser_non_integral_constant_expression
640aa28c 3194 (parser, "floating-point literal");
3195 }
3369eb76 3196 return token->u.value;
0a3b29ad 3197
3fe7c943 3198 case CPP_STRING:
924bbf02 3199 case CPP_STRING16:
3200 case CPP_STRING32:
3fe7c943 3201 case CPP_WSTRING:
00d26680 3202 /* ??? Should wide strings be allowed when parser->translate_strings_p
653e5405 3203 is false (i.e. in attributes)? If not, we can kill the third
00d26680 3204 argument to cp_parser_string_literal. */
3205 return cp_parser_string_literal (parser,
3206 parser->translate_strings_p,
3207 true);
3fe7c943 3208
0a3b29ad 3209 case CPP_OPEN_PAREN:
3210 {
3211 tree expr;
3212 bool saved_greater_than_is_operator_p;
3213
3214 /* Consume the `('. */
3215 cp_lexer_consume_token (parser->lexer);
3216 /* Within a parenthesized expression, a `>' token is always
3217 the greater-than operator. */
ccb84981 3218 saved_greater_than_is_operator_p
0a3b29ad 3219 = parser->greater_than_is_operator_p;
3220 parser->greater_than_is_operator_p = true;
3221 /* If we see `( { ' then we are looking at the beginning of
3222 a GNU statement-expression. */
3223 if (cp_parser_allow_gnu_extensions_p (parser)
3224 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3225 {
3226 /* Statement-expressions are not allowed by the standard. */
21ca8540 3227 pedwarn (token->location, OPT_pedantic,
3228 "ISO C++ forbids braced-groups within expressions");
ccb84981 3229
0a3b29ad 3230 /* And they're not allowed outside of a function-body; you
3231 cannot, for example, write:
ccb84981 3232
653e5405 3233 int i = ({ int j = 3; j + 1; });
ccb84981 3234
0a3b29ad 3235 at class or namespace scope. */
9184d665 3236 if (!parser->in_function_body
3237 || parser->in_template_argument_list_p)
4be86438 3238 {
ad9ae192 3239 error ("%Hstatement-expressions are not allowed outside "
3240 "functions nor in template-argument lists",
3241 &token->location);
4be86438 3242 cp_parser_skip_to_end_of_block_or_statement (parser);
3243 expr = error_mark_node;
3244 }
3245 else
3246 {
3247 /* Start the statement-expression. */
3248 expr = begin_stmt_expr ();
3249 /* Parse the compound-statement. */
3250 cp_parser_compound_statement (parser, expr, false);
3251 /* Finish up. */
3252 expr = finish_stmt_expr (expr, false);
3253 }
0a3b29ad 3254 }
3255 else
3256 {
3257 /* Parse the parenthesized expression. */
98b326fd 3258 expr = cp_parser_expression (parser, cast_p, idk);
0a3b29ad 3259 /* Let the front end know that this expression was
3260 enclosed in parentheses. This matters in case, for
3261 example, the expression is of the form `A::B', since
3262 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3263 not. */
3264 finish_parenthesized_expr (expr);
3265 }
3266 /* The `>' token might be the end of a template-id or
3267 template-parameter-list now. */
ccb84981 3268 parser->greater_than_is_operator_p
0a3b29ad 3269 = saved_greater_than_is_operator_p;
3270 /* Consume the `)'. */
640710cf 3271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
0a3b29ad 3272 cp_parser_skip_to_end_of_statement (parser);
3273
3274 return expr;
3275 }
3276
3277 case CPP_KEYWORD:
3278 switch (token->keyword)
3279 {
3280 /* These two are the boolean literals. */
3281 case RID_TRUE:
3282 cp_lexer_consume_token (parser->lexer);
3283 return boolean_true_node;
3284 case RID_FALSE:
3285 cp_lexer_consume_token (parser->lexer);
3286 return boolean_false_node;
ccb84981 3287
0a3b29ad 3288 /* The `__null' literal. */
3289 case RID_NULL:
3290 cp_lexer_consume_token (parser->lexer);
3291 return null_node;
3292
3293 /* Recognize the `this' keyword. */
3294 case RID_THIS:
3295 cp_lexer_consume_token (parser->lexer);
3296 if (parser->local_variables_forbidden_p)
3297 {
ad9ae192 3298 error ("%H%<this%> may not be used in this context",
3299 &token->location);
0a3b29ad 3300 return error_mark_node;
3301 }
5f6526e1 3302 /* Pointers cannot appear in constant-expressions. */
7222be86 3303 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3938e0c2 3304 return error_mark_node;
0a3b29ad 3305 return finish_this_expr ();
3306
3307 /* The `operator' keyword can be the beginning of an
3308 id-expression. */
3309 case RID_OPERATOR:
3310 goto id_expression;
3311
3312 case RID_FUNCTION_NAME:
3313 case RID_PRETTY_FUNCTION_NAME:
3314 case RID_C99_FUNCTION_NAME:
a457170d 3315 {
3316 const char *name;
3317
3318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319 __func__ are the names of variables -- but they are
3320 treated specially. Therefore, they are handled here,
3321 rather than relying on the generic id-expression logic
3322 below. Grammatically, these names are id-expressions.
3323
3324 Consume the token. */
3325 token = cp_lexer_consume_token (parser->lexer);
3326
3327 switch (token->keyword)
3328 {
3329 case RID_FUNCTION_NAME:
3330 name = "%<__FUNCTION__%>";
3331 break;
3332 case RID_PRETTY_FUNCTION_NAME:
3333 name = "%<__PRETTY_FUNCTION__%>";
3334 break;
3335 case RID_C99_FUNCTION_NAME:
3336 name = "%<__func__%>";
3337 break;
3338 default:
3339 gcc_unreachable ();
3340 }
3341
3342 if (cp_parser_non_integral_constant_expression (parser, name))
3343 return error_mark_node;
3344
3345 /* Look up the name. */
3346 return finish_fname (token->u.value);
3347 }
0a3b29ad 3348
3349 case RID_VA_ARG:
3350 {
3351 tree expression;
3352 tree type;
3353
3354 /* The `__builtin_va_arg' construct is used to handle
3355 `va_arg'. Consume the `__builtin_va_arg' token. */
3356 cp_lexer_consume_token (parser->lexer);
3357 /* Look for the opening `('. */
640710cf 3358 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 3359 /* Now, parse the assignment-expression. */
640aa28c 3360 expression = cp_parser_assignment_expression (parser,
98b326fd 3361 /*cast_p=*/false, NULL);
0a3b29ad 3362 /* Look for the `,'. */
640710cf 3363 cp_parser_require (parser, CPP_COMMA, "%<,%>");
0a3b29ad 3364 /* Parse the type-id. */
3365 type = cp_parser_type_id (parser);
3366 /* Look for the closing `)'. */
640710cf 3367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5f6526e1 3368 /* Using `va_arg' in a constant-expression is not
3369 allowed. */
3938e0c2 3370 if (cp_parser_non_integral_constant_expression (parser,
7222be86 3371 "%<va_arg%>"))
3938e0c2 3372 return error_mark_node;
0a3b29ad 3373 return build_x_va_arg (expression, type);
3374 }
3375
30b87eb6 3376 case RID_OFFSETOF:
43bf5d72 3377 return cp_parser_builtin_offsetof (parser);
30b87eb6 3378
481451eb 3379 case RID_HAS_NOTHROW_ASSIGN:
3380 case RID_HAS_NOTHROW_CONSTRUCTOR:
3381 case RID_HAS_NOTHROW_COPY:
3382 case RID_HAS_TRIVIAL_ASSIGN:
3383 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384 case RID_HAS_TRIVIAL_COPY:
3385 case RID_HAS_TRIVIAL_DESTRUCTOR:
3386 case RID_HAS_VIRTUAL_DESTRUCTOR:
3387 case RID_IS_ABSTRACT:
3388 case RID_IS_BASE_OF:
3389 case RID_IS_CLASS:
3390 case RID_IS_CONVERTIBLE_TO:
3391 case RID_IS_EMPTY:
3392 case RID_IS_ENUM:
3393 case RID_IS_POD:
3394 case RID_IS_POLYMORPHIC:
3395 case RID_IS_UNION:
3396 return cp_parser_trait_expr (parser, token->keyword);
3397
3398 /* Objective-C++ expressions. */
7a4e126b 3399 case RID_AT_ENCODE:
3400 case RID_AT_PROTOCOL:
3401 case RID_AT_SELECTOR:
3402 return cp_parser_objc_expression (parser);
3403
0a3b29ad 3404 default:
3405 cp_parser_error (parser, "expected primary-expression");
3406 return error_mark_node;
3407 }
0a3b29ad 3408
3409 /* An id-expression can start with either an identifier, a
3410 `::' as the beginning of a qualified-id, or the "operator"
3411 keyword. */
3412 case CPP_NAME:
3413 case CPP_SCOPE:
3414 case CPP_TEMPLATE_ID:
3415 case CPP_NESTED_NAME_SPECIFIER:
3416 {
3417 tree id_expression;
3418 tree decl;
0886adbc 3419 const char *error_msg;
fbb01da7 3420 bool template_p;
3421 bool done;
ad9ae192 3422 cp_token *id_expr_token;
0a3b29ad 3423
3424 id_expression:
3425 /* Parse the id-expression. */
ccb84981 3426 id_expression
3427 = cp_parser_id_expression (parser,
0a3b29ad 3428 /*template_keyword_p=*/false,
3429 /*check_dependency_p=*/true,
fbb01da7 3430 &template_p,
197c9df7 3431 /*declarator_p=*/false,
130bb1d4 3432 /*optional_p=*/false);
0a3b29ad 3433 if (id_expression == error_mark_node)
3434 return error_mark_node;
ad9ae192 3435 id_expr_token = token;
fbb01da7 3436 token = cp_lexer_peek_token (parser->lexer);
3437 done = (token->type != CPP_OPEN_SQUARE
3438 && token->type != CPP_OPEN_PAREN
3439 && token->type != CPP_DOT
3440 && token->type != CPP_DEREF
3441 && token->type != CPP_PLUS_PLUS
3442 && token->type != CPP_MINUS_MINUS);
0a3b29ad 3443 /* If we have a template-id, then no further lookup is
3444 required. If the template-id was for a template-class, we
3445 will sometimes have a TYPE_DECL at this point. */
fbb01da7 3446 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447 || TREE_CODE (id_expression) == TYPE_DECL)
0a3b29ad 3448 decl = id_expression;
3449 /* Look up the name. */
ccb84981 3450 else
0a3b29ad 3451 {
b62240d5 3452 tree ambiguous_decls;
2cdbcd51 3453
3454 decl = cp_parser_lookup_name (parser, id_expression,
e2ae55f2 3455 none_type,
fbb01da7 3456 template_p,
2cdbcd51 3457 /*is_namespace=*/false,
3458 /*check_dependency=*/true,
ad9ae192 3459 &ambiguous_decls,
3460 id_expr_token->location);
2cdbcd51 3461 /* If the lookup was ambiguous, an error will already have
3462 been issued. */
b62240d5 3463 if (ambiguous_decls)
2cdbcd51 3464 return error_mark_node;
7a4e126b 3465
3466 /* In Objective-C++, an instance variable (ivar) may be preferred
3467 to whatever cp_parser_lookup_name() found. */
3468 decl = objc_lookup_ivar (decl, id_expression);
3469
0a3b29ad 3470 /* If name lookup gives us a SCOPE_REF, then the
fbb01da7 3471 qualifying scope was dependent. */
0a3b29ad 3472 if (TREE_CODE (decl) == SCOPE_REF)
62116ec3 3473 {
3474 /* At this point, we do not know if DECL is a valid
3475 integral constant expression. We assume that it is
3476 in fact such an expression, so that code like:
3477
3478 template <int N> struct A {
3479 int a[B<N>::i];
3480 };
3481
3482 is accepted. At template-instantiation time, we
3483 will check that B<N>::i is actually a constant. */
3484 return decl;
3485 }
0a3b29ad 3486 /* Check to see if DECL is a local variable in a context
3487 where that is forbidden. */
3488 if (parser->local_variables_forbidden_p
3489 && local_variable_p (decl))
3490 {
3491 /* It might be that we only found DECL because we are
3492 trying to be generous with pre-ISO scoping rules.
3493 For example, consider:
3494
3495 int i;
3496 void g() {
3497 for (int i = 0; i < 10; ++i) {}
3498 extern void f(int j = i);
3499 }
3500
ccb84981 3501 Here, name look up will originally find the out
0a3b29ad 3502 of scope `i'. We need to issue a warning message,
3503 but then use the global `i'. */
3504 decl = check_for_out_of_scope_variable (decl);
3505 if (local_variable_p (decl))
3506 {
ad9ae192 3507 error ("%Hlocal variable %qD may not appear in this context",
3508 &id_expr_token->location, decl);
0a3b29ad 3509 return error_mark_node;
3510 }
3511 }
f3b70d2f 3512 }
ccb84981 3513
074ab442 3514 decl = (finish_id_expression
fbb01da7 3515 (id_expression, decl, parser->scope,
3516 idk,
3517 parser->integral_constant_expression_p,
3518 parser->allow_non_integral_constant_expression_p,
3519 &parser->non_integral_constant_expression_p,
3520 template_p, done, address_p,
3521 template_arg_p,
ad9ae192 3522 &error_msg,
3523 id_expr_token->location));
0886adbc 3524 if (error_msg)
3525 cp_parser_error (parser, error_msg);
0a3b29ad 3526 return decl;
3527 }
3528
3529 /* Anything else is an error. */
3530 default:
dfbfbb43 3531 /* ...unless we have an Objective-C++ message or string literal,
3532 that is. */
9031d10b 3533 if (c_dialect_objc ()
dfbfbb43 3534 && (token->type == CPP_OPEN_SQUARE
3535 || token->type == CPP_OBJC_STRING))
7a4e126b 3536 return cp_parser_objc_expression (parser);
3537
0a3b29ad 3538 cp_parser_error (parser, "expected primary-expression");
3539 return error_mark_node;
3540 }
3541}
3542
3543/* Parse an id-expression.
3544
3545 id-expression:
3546 unqualified-id
3547 qualified-id
3548
3549 qualified-id:
3550 :: [opt] nested-name-specifier template [opt] unqualified-id
3551 :: identifier
3552 :: operator-function-id
3553 :: template-id
3554
3555 Return a representation of the unqualified portion of the
3556 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3557 a `::' or nested-name-specifier.
3558
3559 Often, if the id-expression was a qualified-id, the caller will
3560 want to make a SCOPE_REF to represent the qualified-id. This
3561 function does not do this in order to avoid wastefully creating
3562 SCOPE_REFs when they are not required.
3563
0a3b29ad 3564 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3565 `template' keyword.
3566
3567 If CHECK_DEPENDENCY_P is false, then names are looked up inside
ccb84981 3568 uninstantiated templates.
0a3b29ad 3569
a210e1ca 3570 If *TEMPLATE_P is non-NULL, it is set to true iff the
0a3b29ad 3571 `template' keyword is used to explicitly indicate that the entity
ccb84981 3572 named is a template.
899cc6e8 3573
3574 If DECLARATOR_P is true, the id-expression is appearing as part of
63eff20d 3575 a declarator, rather than as part of an expression. */
0a3b29ad 3576
3577static tree
3578cp_parser_id_expression (cp_parser *parser,
3579 bool template_keyword_p,
3580 bool check_dependency_p,
899cc6e8 3581 bool *template_p,
197c9df7 3582 bool declarator_p,
130bb1d4 3583 bool optional_p)
0a3b29ad 3584{
3585 bool global_scope_p;
3586 bool nested_name_specifier_p;
3587
3588 /* Assume the `template' keyword was not used. */
3589 if (template_p)
fbb01da7 3590 *template_p = template_keyword_p;
0a3b29ad 3591
3592 /* Look for the optional `::' operator. */
ccb84981 3593 global_scope_p
130bb1d4 3594 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
0a3b29ad 3595 != NULL_TREE);
3596 /* Look for the optional nested-name-specifier. */
ccb84981 3597 nested_name_specifier_p
0a3b29ad 3598 = (cp_parser_nested_name_specifier_opt (parser,
3599 /*typename_keyword_p=*/false,
3600 check_dependency_p,
3d0f901b 3601 /*type_p=*/false,
0078a5e8 3602 declarator_p)
0a3b29ad 3603 != NULL_TREE);
3604 /* If there is a nested-name-specifier, then we are looking at
3605 the first qualified-id production. */
3606 if (nested_name_specifier_p)
3607 {
3608 tree saved_scope;
3609 tree saved_object_scope;
3610 tree saved_qualifying_scope;
3611 tree unqualified_id;
3612 bool is_template;
3613
3614 /* See if the next token is the `template' keyword. */
3615 if (!template_p)
3616 template_p = &is_template;
3617 *template_p = cp_parser_optional_template_keyword (parser);
3618 /* Name lookup we do during the processing of the
3619 unqualified-id might obliterate SCOPE. */
3620 saved_scope = parser->scope;
3621 saved_object_scope = parser->object_scope;
3622 saved_qualifying_scope = parser->qualifying_scope;
3623 /* Process the final unqualified-id. */
3624 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
899cc6e8 3625 check_dependency_p,
197c9df7 3626 declarator_p,
130bb1d4 3627 /*optional_p=*/false);
0a3b29ad 3628 /* Restore the SAVED_SCOPE for our caller. */
3629 parser->scope = saved_scope;
3630 parser->object_scope = saved_object_scope;
3631 parser->qualifying_scope = saved_qualifying_scope;
3632
3633 return unqualified_id;
3634 }
3635 /* Otherwise, if we are in global scope, then we are looking at one
3636 of the other qualified-id productions. */
3637 else if (global_scope_p)
3638 {
3639 cp_token *token;
3640 tree id;
3641
2c593bd0 3642 /* Peek at the next token. */
3643 token = cp_lexer_peek_token (parser->lexer);
3644
3645 /* If it's an identifier, and the next token is not a "<", then
3646 we can avoid the template-id case. This is an optimization
3647 for this common case. */
ccb84981 3648 if (token->type == CPP_NAME
3649 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 3650 (parser, 2))
2c593bd0 3651 return cp_parser_identifier (parser);
3652
0a3b29ad 3653 cp_parser_parse_tentatively (parser);
3654 /* Try a template-id. */
ccb84981 3655 id = cp_parser_template_id (parser,
0a3b29ad 3656 /*template_keyword_p=*/false,
3d0f901b 3657 /*check_dependency_p=*/true,
3658 declarator_p);
0a3b29ad 3659 /* If that worked, we're done. */
3660 if (cp_parser_parse_definitely (parser))
3661 return id;
3662
2c593bd0 3663 /* Peek at the next token. (Changes in the token buffer may
3664 have invalidated the pointer obtained above.) */
0a3b29ad 3665 token = cp_lexer_peek_token (parser->lexer);
3666
3667 switch (token->type)
3668 {
3669 case CPP_NAME:
3670 return cp_parser_identifier (parser);
3671
3672 case CPP_KEYWORD:
3673 if (token->keyword == RID_OPERATOR)
3674 return cp_parser_operator_function_id (parser);
3675 /* Fall through. */
ccb84981 3676
0a3b29ad 3677 default:
3678 cp_parser_error (parser, "expected id-expression");
3679 return error_mark_node;
3680 }
3681 }
3682 else
3683 return cp_parser_unqualified_id (parser, template_keyword_p,
899cc6e8 3684 /*check_dependency_p=*/true,
130bb1d4 3685 declarator_p,
3686 optional_p);
0a3b29ad 3687}
3688
3689/* Parse an unqualified-id.
3690
3691 unqualified-id:
3692 identifier
3693 operator-function-id
3694 conversion-function-id
3695 ~ class-name
3696 template-id
3697
3698 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699 keyword, in a construct like `A::template ...'.
3700
3701 Returns a representation of unqualified-id. For the `identifier'
3702 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3703 production a BIT_NOT_EXPR is returned; the operand of the
3704 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3705 other productions, see the documentation accompanying the
3706 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
899cc6e8 3707 names are looked up in uninstantiated templates. If DECLARATOR_P
3708 is true, the unqualified-id is appearing as part of a declarator,
3709 rather than as part of an expression. */
0a3b29ad 3710
3711static tree
ccb84981 3712cp_parser_unqualified_id (cp_parser* parser,
653e5405 3713 bool template_keyword_p,
899cc6e8 3714 bool check_dependency_p,
074ab442 3715 bool declarator_p,
130bb1d4 3716 bool optional_p)
0a3b29ad 3717{
3718 cp_token *token;
3719
3720 /* Peek at the next token. */
3721 token = cp_lexer_peek_token (parser->lexer);
ccb84981 3722
0a3b29ad 3723 switch (token->type)
3724 {
3725 case CPP_NAME:
3726 {
3727 tree id;
3728
3729 /* We don't know yet whether or not this will be a
3730 template-id. */
3731 cp_parser_parse_tentatively (parser);
3732 /* Try a template-id. */
3733 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 3734 check_dependency_p,
3735 declarator_p);
0a3b29ad 3736 /* If it worked, we're done. */
3737 if (cp_parser_parse_definitely (parser))
3738 return id;
3739 /* Otherwise, it's an ordinary identifier. */
3740 return cp_parser_identifier (parser);
3741 }
3742
3743 case CPP_TEMPLATE_ID:
3744 return cp_parser_template_id (parser, template_keyword_p,
3d0f901b 3745 check_dependency_p,
3746 declarator_p);
0a3b29ad 3747
3748 case CPP_COMPL:
3749 {
3750 tree type_decl;
3751 tree qualifying_scope;
3752 tree object_scope;
3753 tree scope;
f0d4a607 3754 bool done;
0a3b29ad 3755
3756 /* Consume the `~' token. */
3757 cp_lexer_consume_token (parser->lexer);
3758 /* Parse the class-name. The standard, as written, seems to
3759 say that:
3760
3761 template <typename T> struct S { ~S (); };
3762 template <typename T> S<T>::~S() {}
3763
653e5405 3764 is invalid, since `~' must be followed by a class-name, but
0a3b29ad 3765 `S<T>' is dependent, and so not known to be a class.
3766 That's not right; we need to look in uninstantiated
3767 templates. A further complication arises from:
3768
3769 template <typename T> void f(T t) {
3770 t.T::~T();
ccb84981 3771 }
0a3b29ad 3772
3773 Here, it is not possible to look up `T' in the scope of `T'
3774 itself. We must look in both the current scope, and the
ccb84981 3775 scope of the containing complete expression.
0a3b29ad 3776
3777 Yet another issue is:
3778
653e5405 3779 struct S {
3780 int S;
3781 ~S();
3782 };
0a3b29ad 3783
653e5405 3784 S::~S() {}
0a3b29ad 3785
653e5405 3786 The standard does not seem to say that the `S' in `~S'
0a3b29ad 3787 should refer to the type `S' and not the data member
3788 `S::S'. */
3789
3790 /* DR 244 says that we look up the name after the "~" in the
3791 same scope as we looked up the qualifying name. That idea
3792 isn't fully worked out; it's more complicated than that. */
3793 scope = parser->scope;
3794 object_scope = parser->object_scope;
3795 qualifying_scope = parser->qualifying_scope;
3796
4b2ee8a4 3797 /* Check for invalid scopes. */
3798 if (scope == error_mark_node)
3799 {
dea225ee 3800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 3802 return error_mark_node;
3803 }
3804 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3805 {
3806 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ad9ae192 3807 error ("%Hscope %qT before %<~%> is not a class-name",
3808 &token->location, scope);
dea225ee 3809 cp_parser_simulate_error (parser);
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 3812 return error_mark_node;
3813 }
3814 gcc_assert (!scope || TYPE_P (scope));
3815
0a3b29ad 3816 /* If the name is of the form "X::~X" it's OK. */
ba0c587d 3817 token = cp_lexer_peek_token (parser->lexer);
4b2ee8a4 3818 if (scope
ba0c587d 3819 && token->type == CPP_NAME
ccb84981 3820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 3821 == CPP_OPEN_PAREN)
3369eb76 3822 && constructor_name_p (token->u.value, scope))
0a3b29ad 3823 {
3824 cp_lexer_consume_token (parser->lexer);
3825 return build_nt (BIT_NOT_EXPR, scope);
3826 }
3827
3828 /* If there was an explicit qualification (S::~T), first look
3829 in the scope given by the qualification (i.e., S). */
f0d4a607 3830 done = false;
7978bd9e 3831 type_decl = NULL_TREE;
0a3b29ad 3832 if (scope)
3833 {
3834 cp_parser_parse_tentatively (parser);
ccb84981 3835 type_decl = cp_parser_class_name (parser,
0a3b29ad 3836 /*typename_keyword_p=*/false,
3837 /*template_keyword_p=*/false,
e2ae55f2 3838 none_type,
0a3b29ad 3839 /*check_dependency=*/false,
3d0f901b 3840 /*class_head_p=*/false,
3841 declarator_p);
0a3b29ad 3842 if (cp_parser_parse_definitely (parser))
f0d4a607 3843 done = true;
0a3b29ad 3844 }
3845 /* In "N::S::~S", look in "N" as well. */
f0d4a607 3846 if (!done && scope && qualifying_scope)
0a3b29ad 3847 {
3848 cp_parser_parse_tentatively (parser);
3849 parser->scope = qualifying_scope;
3850 parser->object_scope = NULL_TREE;
3851 parser->qualifying_scope = NULL_TREE;
ccb84981 3852 type_decl
3853 = cp_parser_class_name (parser,
0a3b29ad 3854 /*typename_keyword_p=*/false,
3855 /*template_keyword_p=*/false,
e2ae55f2 3856 none_type,
0a3b29ad 3857 /*check_dependency=*/false,
3d0f901b 3858 /*class_head_p=*/false,
3859 declarator_p);
0a3b29ad 3860 if (cp_parser_parse_definitely (parser))
f0d4a607 3861 done = true;
0a3b29ad 3862 }
130bb1d4 3863 /* In "p->S::~T", look in the scope given by "*p" as well. */
3864 else if (!done && object_scope)
0a3b29ad 3865 {
3866 cp_parser_parse_tentatively (parser);
3867 parser->scope = object_scope;
3868 parser->object_scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
ccb84981 3870 type_decl
3871 = cp_parser_class_name (parser,
130bb1d4 3872 /*typename_keyword_p=*/false,
0a3b29ad 3873 /*template_keyword_p=*/false,
e2ae55f2 3874 none_type,
0a3b29ad 3875 /*check_dependency=*/false,
3d0f901b 3876 /*class_head_p=*/false,
3877 declarator_p);
0a3b29ad 3878 if (cp_parser_parse_definitely (parser))
f0d4a607 3879 done = true;
0a3b29ad 3880 }
3881 /* Look in the surrounding context. */
f0d4a607 3882 if (!done)
3883 {
3884 parser->scope = NULL_TREE;
3885 parser->object_scope = NULL_TREE;
3886 parser->qualifying_scope = NULL_TREE;
c6cc092b 3887 if (processing_template_decl)
3888 cp_parser_parse_tentatively (parser);
f0d4a607 3889 type_decl
3890 = cp_parser_class_name (parser,
3891 /*typename_keyword_p=*/false,
3892 /*template_keyword_p=*/false,
3893 none_type,
3894 /*check_dependency=*/false,
3895 /*class_head_p=*/false,
3896 declarator_p);
c6cc092b 3897 if (processing_template_decl
3898 && ! cp_parser_parse_definitely (parser))
3899 {
3900 /* We couldn't find a type with this name, so just accept
3901 it and check for a match at instantiation time. */
3902 type_decl = cp_parser_identifier (parser);
92644697 3903 if (type_decl != error_mark_node)
3904 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3905 return type_decl;
c6cc092b 3906 }
f0d4a607 3907 }
0a3b29ad 3908 /* If an error occurred, assume that the name of the
3909 destructor is the same as the name of the qualifying
3910 class. That allows us to keep parsing after running
3911 into ill-formed destructor names. */
4b2ee8a4 3912 if (type_decl == error_mark_node && scope)
0a3b29ad 3913 return build_nt (BIT_NOT_EXPR, scope);
3914 else if (type_decl == error_mark_node)
3915 return error_mark_node;
3916
a32f68f5 3917 /* Check that destructor name and scope match. */
3918 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3919 {
3920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ad9ae192 3921 error ("%Hdeclaration of %<~%T%> as member of %qT",
3922 &token->location, type_decl, scope);
dea225ee 3923 cp_parser_simulate_error (parser);
a32f68f5 3924 return error_mark_node;
3925 }
3926
899cc6e8 3927 /* [class.dtor]
3928
3929 A typedef-name that names a class shall not be used as the
3930 identifier in the declarator for a destructor declaration. */
ccb84981 3931 if (declarator_p
899cc6e8 3932 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
510a599a 3933 && !DECL_SELF_REFERENCE_P (type_decl)
3934 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
ad9ae192 3935 error ("%Htypedef-name %qD used as destructor declarator",
3936 &token->location, type_decl);
899cc6e8 3937
0a3b29ad 3938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3939 }
3940
3941 case CPP_KEYWORD:
3942 if (token->keyword == RID_OPERATOR)
3943 {
3944 tree id;
3945
3946 /* This could be a template-id, so we try that first. */
3947 cp_parser_parse_tentatively (parser);
3948 /* Try a template-id. */
3949 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 3950 /*check_dependency_p=*/true,
3951 declarator_p);
0a3b29ad 3952 /* If that worked, we're done. */
3953 if (cp_parser_parse_definitely (parser))
3954 return id;
3955 /* We still don't know whether we're looking at an
3956 operator-function-id or a conversion-function-id. */
3957 cp_parser_parse_tentatively (parser);
3958 /* Try an operator-function-id. */
3959 id = cp_parser_operator_function_id (parser);
3960 /* If that didn't work, try a conversion-function-id. */
3961 if (!cp_parser_parse_definitely (parser))
3962 id = cp_parser_conversion_function_id (parser);
3963
3964 return id;
3965 }
3966 /* Fall through. */
3967
3968 default:
197c9df7 3969 if (optional_p)
3970 return NULL_TREE;
0a3b29ad 3971 cp_parser_error (parser, "expected unqualified-id");
3972 return error_mark_node;
3973 }
3974}
3975
3976/* Parse an (optional) nested-name-specifier.
3977
3f00a6c0 3978 nested-name-specifier: [C++98]
0a3b29ad 3979 class-or-namespace-name :: nested-name-specifier [opt]
3980 class-or-namespace-name :: template nested-name-specifier [opt]
3981
3f00a6c0 3982 nested-name-specifier: [C++0x]
3983 type-name ::
3984 namespace-name ::
3985 nested-name-specifier identifier ::
3986 nested-name-specifier template [opt] simple-template-id ::
3987
0a3b29ad 3988 PARSER->SCOPE should be set appropriately before this function is
3989 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3991 in name lookups.
3992
3993 Sets PARSER->SCOPE to the class (TYPE) or namespace
3994 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995 it unchanged if there is no nested-name-specifier. Returns the new
ccb84981 3996 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3d0f901b 3997
3998 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999 part of a declaration and/or decl-specifier. */
0a3b29ad 4000
4001static tree
ccb84981 4002cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003 bool typename_keyword_p,
0a3b29ad 4004 bool check_dependency_p,
3d0f901b 4005 bool type_p,
4006 bool is_declaration)
0a3b29ad 4007{
4008 bool success = false;
19273cc2 4009 cp_token_position start = 0;
4010 cp_token *token;
0a3b29ad 4011
0a3b29ad 4012 /* Remember where the nested-name-specifier starts. */
efcbcf83 4013 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
d15855b2 4014 {
4015 start = cp_lexer_token_position (parser->lexer, false);
4016 push_deferring_access_checks (dk_deferred);
4017 }
9b57b06b 4018
0a3b29ad 4019 while (true)
4020 {
4021 tree new_scope;
4022 tree old_scope;
4023 tree saved_qualifying_scope;
0a3b29ad 4024 bool template_keyword_p;
4025
b3c48b5d 4026 /* Spot cases that cannot be the beginning of a
4027 nested-name-specifier. */
4028 token = cp_lexer_peek_token (parser->lexer);
4029
4030 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031 the already parsed nested-name-specifier. */
4032 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4033 {
4034 /* Grab the nested-name-specifier and continue the loop. */
4035 cp_parser_pre_parsed_nested_name_specifier (parser);
2f7a9ec1 4036 /* If we originally encountered this nested-name-specifier
4037 with IS_DECLARATION set to false, we will not have
4038 resolved TYPENAME_TYPEs, so we must do so here. */
b8b2a426 4039 if (is_declaration
4040 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
2f7a9ec1 4041 {
4042 new_scope = resolve_typename_type (parser->scope,
4043 /*only_current_p=*/false);
8826a863 4044 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
2f7a9ec1 4045 parser->scope = new_scope;
4046 }
b3c48b5d 4047 success = true;
4048 continue;
4049 }
4050
0a3b29ad 4051 /* Spot cases that cannot be the beginning of a
4052 nested-name-specifier. On the second and subsequent times
4053 through the loop, we look for the `template' keyword. */
2370b5bf 4054 if (success && token->keyword == RID_TEMPLATE)
0a3b29ad 4055 ;
4056 /* A template-id can start a nested-name-specifier. */
2370b5bf 4057 else if (token->type == CPP_TEMPLATE_ID)
0a3b29ad 4058 ;
4059 else
4060 {
4061 /* If the next token is not an identifier, then it is
3f00a6c0 4062 definitely not a type-name or namespace-name. */
2370b5bf 4063 if (token->type != CPP_NAME)
0a3b29ad 4064 break;
4065 /* If the following token is neither a `<' (to begin a
4066 template-id), nor a `::', then we are not looking at a
4067 nested-name-specifier. */
4068 token = cp_lexer_peek_nth_token (parser->lexer, 2);
c8d5ab79 4069 if (token->type != CPP_SCOPE
4070 && !cp_parser_nth_token_starts_template_argument_list_p
4071 (parser, 2))
0a3b29ad 4072 break;
4073 }
4074
4075 /* The nested-name-specifier is optional, so we parse
4076 tentatively. */
4077 cp_parser_parse_tentatively (parser);
4078
4079 /* Look for the optional `template' keyword, if this isn't the
4080 first time through the loop. */
4081 if (success)
4082 template_keyword_p = cp_parser_optional_template_keyword (parser);
4083 else
4084 template_keyword_p = false;
4085
4086 /* Save the old scope since the name lookup we are about to do
4087 might destroy it. */
4088 old_scope = parser->scope;
4089 saved_qualifying_scope = parser->qualifying_scope;
0078a5e8 4090 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091 look up names in "X<T>::I" in order to determine that "Y" is
4092 a template. So, if we have a typename at this point, we make
4093 an effort to look through it. */
9031d10b 4094 if (is_declaration
dee31741 4095 && !typename_keyword_p
9031d10b 4096 && parser->scope
0078a5e8 4097 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
9031d10b 4098 parser->scope = resolve_typename_type (parser->scope,
0078a5e8 4099 /*only_current_p=*/false);
0a3b29ad 4100 /* Parse the qualifying entity. */
ccb84981 4101 new_scope
3f00a6c0 4102 = cp_parser_qualifying_entity (parser,
4103 typename_keyword_p,
4104 template_keyword_p,
4105 check_dependency_p,
4106 type_p,
4107 is_declaration);
0a3b29ad 4108 /* Look for the `::' token. */
640710cf 4109 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
0a3b29ad 4110
4111 /* If we found what we wanted, we keep going; otherwise, we're
4112 done. */
4113 if (!cp_parser_parse_definitely (parser))
4114 {
4115 bool error_p = false;
4116
4117 /* Restore the OLD_SCOPE since it was valid before the
4118 failed attempt at finding the last
4119 class-or-namespace-name. */
4120 parser->scope = old_scope;
4121 parser->qualifying_scope = saved_qualifying_scope;
ba0c587d 4122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4123 break;
0a3b29ad 4124 /* If the next token is an identifier, and the one after
4125 that is a `::', then any valid interpretation would have
4126 found a class-or-namespace-name. */
4127 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
ccb84981 4128 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 4129 == CPP_SCOPE)
ccb84981 4130 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
0a3b29ad 4131 != CPP_COMPL))
4132 {
4133 token = cp_lexer_consume_token (parser->lexer);
ccb84981 4134 if (!error_p)
0a3b29ad 4135 {
b62240d5 4136 if (!token->ambiguous_p)
4137 {
4138 tree decl;
4139 tree ambiguous_decls;
4140
3369eb76 4141 decl = cp_parser_lookup_name (parser, token->u.value,
b62240d5 4142 none_type,
4143 /*is_template=*/false,
4144 /*is_namespace=*/false,
4145 /*check_dependency=*/true,
ad9ae192 4146 &ambiguous_decls,
4147 token->location);
b62240d5 4148 if (TREE_CODE (decl) == TEMPLATE_DECL)
ad9ae192 4149 error ("%H%qD used without template parameters",
4150 &token->location, decl);
b62240d5 4151 else if (ambiguous_decls)
4152 {
ad9ae192 4153 error ("%Hreference to %qD is ambiguous",
4154 &token->location, token->u.value);
b62240d5 4155 print_candidates (ambiguous_decls);
4156 decl = error_mark_node;
4157 }
4158 else
3f00a6c0 4159 {
4160 const char* msg = "is not a class or namespace";
4161 if (cxx_dialect != cxx98)
4162 msg = "is not a class, namespace, or enumeration";
4163 cp_parser_name_lookup_error
4164 (parser, token->u.value, decl, msg,
4165 token->location);
4166 }
b62240d5 4167 }
4168 parser->scope = error_mark_node;
0a3b29ad 4169 error_p = true;
6fc758aa 4170 /* Treat this as a successful nested-name-specifier
4171 due to:
4172
4173 [basic.lookup.qual]
4174
4175 If the name found is not a class-name (clause
4176 _class_) or namespace-name (_namespace.def_), the
4177 program is ill-formed. */
4178 success = true;
0a3b29ad 4179 }
4180 cp_lexer_consume_token (parser->lexer);
4181 }
4182 break;
4183 }
0a3b29ad 4184 /* We've found one valid nested-name-specifier. */
4185 success = true;
fbb01da7 4186 /* Name lookup always gives us a DECL. */
4187 if (TREE_CODE (new_scope) == TYPE_DECL)
4188 new_scope = TREE_TYPE (new_scope);
4189 /* Uses of "template" must be followed by actual templates. */
4190 if (template_keyword_p
4191 && !(CLASS_TYPE_P (new_scope)
4192 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197 == TEMPLATE_ID_EXPR)))
2b9e3597 4198 permerror (input_location, TYPE_P (new_scope)
561fec9d 4199 ? "%qT is not a template"
4200 : "%qD is not a template",
4201 new_scope);
0a3b29ad 4202 /* If it is a class scope, try to complete it; we are about to
4203 be looking up names inside the class. */
fbb01da7 4204 if (TYPE_P (new_scope)
954ad420 4205 /* Since checking types for dependency can be expensive,
4206 avoid doing it if the type is already complete. */
fbb01da7 4207 && !COMPLETE_TYPE_P (new_scope)
954ad420 4208 /* Do not try to complete dependent types. */
fbb01da7 4209 && !dependent_type_p (new_scope))
fd0d54e4 4210 {
4211 new_scope = complete_type (new_scope);
4212 /* If it is a typedef to current class, use the current
4213 class instead, as the typedef won't have any names inside
4214 it yet. */
4215 if (!COMPLETE_TYPE_P (new_scope)
4216 && currently_open_class (new_scope))
4217 new_scope = TYPE_MAIN_VARIANT (new_scope);
4218 }
fbb01da7 4219 /* Make sure we look in the right scope the next time through
4220 the loop. */
4221 parser->scope = new_scope;
0a3b29ad 4222 }
4223
4224 /* If parsing tentatively, replace the sequence of tokens that makes
4225 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226 token. That way, should we re-parse the token stream, we will
4227 not have to repeat the effort required to do the parse, nor will
4228 we issue duplicate error messages. */
19273cc2 4229 if (success && start)
0a3b29ad 4230 {
d15855b2 4231 cp_token *token;
9031d10b 4232
d15855b2 4233 token = cp_lexer_token_at (parser->lexer, start);
0a3b29ad 4234 /* Reset the contents of the START token. */
4235 token->type = CPP_NESTED_NAME_SPECIFIER;
d15855b2 4236 /* Retrieve any deferred checks. Do not pop this access checks yet
4237 so the memory will not be reclaimed during token replacing below. */
3369eb76 4238 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239 token->u.tree_check_value->value = parser->scope;
4240 token->u.tree_check_value->checks = get_deferred_access_checks ();
4241 token->u.tree_check_value->qualifying_scope =
4242 parser->qualifying_scope;
0a3b29ad 4243 token->keyword = RID_MAX;
9031d10b 4244
0a3b29ad 4245 /* Purge all subsequent tokens. */
19273cc2 4246 cp_lexer_purge_tokens_after (parser->lexer, start);
0a3b29ad 4247 }
074ab442 4248
d15855b2 4249 if (start)
4250 pop_to_parent_deferring_access_checks ();
0a3b29ad 4251
4252 return success ? parser->scope : NULL_TREE;
4253}
4254
4255/* Parse a nested-name-specifier. See
4256 cp_parser_nested_name_specifier_opt for details. This function
4257 behaves identically, except that it will an issue an error if no
c75ae97e 4258 nested-name-specifier is present. */
0a3b29ad 4259
4260static tree
ccb84981 4261cp_parser_nested_name_specifier (cp_parser *parser,
4262 bool typename_keyword_p,
0a3b29ad 4263 bool check_dependency_p,
3d0f901b 4264 bool type_p,
4265 bool is_declaration)
0a3b29ad 4266{
4267 tree scope;
4268
4269 /* Look for the nested-name-specifier. */
4270 scope = cp_parser_nested_name_specifier_opt (parser,
4271 typename_keyword_p,
4272 check_dependency_p,
3d0f901b 4273 type_p,
4274 is_declaration);
0a3b29ad 4275 /* If it was not present, issue an error message. */
4276 if (!scope)
4277 {
4278 cp_parser_error (parser, "expected nested-name-specifier");
bbd3de90 4279 parser->scope = NULL_TREE;
0a3b29ad 4280 }
4281
4282 return scope;
4283}
4284
3f00a6c0 4285/* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286 this is either a class-name or a namespace-name (which corresponds
4287 to the class-or-namespace-name production in the grammar). For
4288 C++0x, it can also be a type-name that refers to an enumeration
4289 type.
0a3b29ad 4290
4291 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294 TYPE_P is TRUE iff the next name should be taken as a class-name,
4295 even the same name is declared to be another entity in the same
4296 scope.
4297
4298 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6fc758aa 4299 specified by the class-or-namespace-name. If neither is found the
4300 ERROR_MARK_NODE is returned. */
0a3b29ad 4301
4302static tree
3f00a6c0 4303cp_parser_qualifying_entity (cp_parser *parser,
4304 bool typename_keyword_p,
4305 bool template_keyword_p,
4306 bool check_dependency_p,
4307 bool type_p,
4308 bool is_declaration)
0a3b29ad 4309{
4310 tree saved_scope;
4311 tree saved_qualifying_scope;
4312 tree saved_object_scope;
4313 tree scope;
6fc758aa 4314 bool only_class_p;
3f00a6c0 4315 bool successful_parse_p;
0a3b29ad 4316
0a3b29ad 4317 /* Before we try to parse the class-name, we must save away the
4318 current PARSER->SCOPE since cp_parser_class_name will destroy
4319 it. */
4320 saved_scope = parser->scope;
4321 saved_qualifying_scope = parser->qualifying_scope;
4322 saved_object_scope = parser->object_scope;
6fc758aa 4323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4324 there is no need to look for a namespace-name. */
3f00a6c0 4325 only_class_p = template_keyword_p
4326 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6fc758aa 4327 if (!only_class_p)
4328 cp_parser_parse_tentatively (parser);
ccb84981 4329 scope = cp_parser_class_name (parser,
0a3b29ad 4330 typename_keyword_p,
4331 template_keyword_p,
e2ae55f2 4332 type_p ? class_type : none_type,
0a3b29ad 4333 check_dependency_p,
3d0f901b 4334 /*class_head_p=*/false,
4335 is_declaration);
3f00a6c0 4336 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4338 if (!only_class_p
4339 && cxx_dialect != cxx98
4340 && !successful_parse_p)
4341 {
4342 /* Restore the saved scope. */
4343 parser->scope = saved_scope;
4344 parser->qualifying_scope = saved_qualifying_scope;
4345 parser->object_scope = saved_object_scope;
4346
4347 /* Parse tentatively. */
4348 cp_parser_parse_tentatively (parser);
4349
4350 /* Parse a typedef-name or enum-name. */
4351 scope = cp_parser_nonclass_name (parser);
4352 successful_parse_p = cp_parser_parse_definitely (parser);
4353 }
0a3b29ad 4354 /* If that didn't work, try for a namespace-name. */
3f00a6c0 4355 if (!only_class_p && !successful_parse_p)
0a3b29ad 4356 {
4357 /* Restore the saved scope. */
4358 parser->scope = saved_scope;
4359 parser->qualifying_scope = saved_qualifying_scope;
4360 parser->object_scope = saved_object_scope;
6fc758aa 4361 /* If we are not looking at an identifier followed by the scope
4362 resolution operator, then this is not part of a
4363 nested-name-specifier. (Note that this function is only used
4364 to parse the components of a nested-name-specifier.) */
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367 return error_mark_node;
0a3b29ad 4368 scope = cp_parser_namespace_name (parser);
4369 }
4370
4371 return scope;
4372}
4373
4374/* Parse a postfix-expression.
4375
4376 postfix-expression:
4377 primary-expression
4378 postfix-expression [ expression ]
4379 postfix-expression ( expression-list [opt] )
4380 simple-type-specifier ( expression-list [opt] )
ccb84981 4381 typename :: [opt] nested-name-specifier identifier
0a3b29ad 4382 ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier template [opt] template-id
4384 ( expression-list [opt] )
4385 postfix-expression . template [opt] id-expression
4386 postfix-expression -> template [opt] id-expression
4387 postfix-expression . pseudo-destructor-name
4388 postfix-expression -> pseudo-destructor-name
4389 postfix-expression ++
4390 postfix-expression --
4391 dynamic_cast < type-id > ( expression )
4392 static_cast < type-id > ( expression )
4393 reinterpret_cast < type-id > ( expression )
4394 const_cast < type-id > ( expression )
4395 typeid ( expression )
4396 typeid ( type-id )
4397
4398 GNU Extension:
ccb84981 4399
0a3b29ad 4400 postfix-expression:
4401 ( type-id ) { initializer-list , [opt] }
4402
4403 This extension is a GNU version of the C99 compound-literal
4404 construct. (The C99 grammar uses `type-name' instead of `type-id',
4405 but they are essentially the same concept.)
4406
4407 If ADDRESS_P is true, the postfix expression is the operand of the
640aa28c 4408 `&' operator. CAST_P is true if this expression is the target of a
9031d10b 4409 cast.
0a3b29ad 4410
34da8800 4411 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412 class member access expressions [expr.ref].
4413
0a3b29ad 4414 Returns a representation of the expression. */
4415
4416static tree
34da8800 4417cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
98b326fd 4418 bool member_access_only_p,
4419 cp_id_kind * pidk_return)
0a3b29ad 4420{
4421 cp_token *token;
4422 enum rid keyword;
0886adbc 4423 cp_id_kind idk = CP_ID_KIND_NONE;
0a3b29ad 4424 tree postfix_expression = NULL_TREE;
34da8800 4425 bool is_member_access = false;
0a3b29ad 4426
4427 /* Peek at the next token. */
4428 token = cp_lexer_peek_token (parser->lexer);
4429 /* Some of the productions are determined by keywords. */
4430 keyword = token->keyword;
4431 switch (keyword)
4432 {
4433 case RID_DYNCAST:
4434 case RID_STATCAST:
4435 case RID_REINTCAST:
4436 case RID_CONSTCAST:
4437 {
4438 tree type;
4439 tree expression;
4440 const char *saved_message;
4441
4442 /* All of these can be handled in the same way from the point
4443 of view of parsing. Begin by consuming the token
4444 identifying the cast. */
4445 cp_lexer_consume_token (parser->lexer);
ccb84981 4446
0a3b29ad 4447 /* New types cannot be defined in the cast. */
4448 saved_message = parser->type_definition_forbidden_message;
4449 parser->type_definition_forbidden_message
4450 = "types may not be defined in casts";
4451
4452 /* Look for the opening `<'. */
640710cf 4453 cp_parser_require (parser, CPP_LESS, "%<<%>");
0a3b29ad 4454 /* Parse the type to which we are casting. */
4455 type = cp_parser_type_id (parser);
4456 /* Look for the closing `>'. */
640710cf 4457 cp_parser_require (parser, CPP_GREATER, "%<>%>");
0a3b29ad 4458 /* Restore the old message. */
4459 parser->type_definition_forbidden_message = saved_message;
4460
4461 /* And the expression which is being cast. */
640710cf 4462 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
98b326fd 4463 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
640710cf 4464 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 4465
5f6526e1 4466 /* Only type conversions to integral or enumeration types
4467 can be used in constant-expressions. */
bde9ebf7 4468 if (!cast_valid_in_integral_constant_expression_p (type)
207355ad 4469 && (cp_parser_non_integral_constant_expression
3938e0c2 4470 (parser,
4471 "a cast to a type other than an integral or "
4472 "enumeration type")))
4473 return error_mark_node;
5f6526e1 4474
0a3b29ad 4475 switch (keyword)
4476 {
4477 case RID_DYNCAST:
4478 postfix_expression
ebd21de4 4479 = build_dynamic_cast (type, expression, tf_warning_or_error);
0a3b29ad 4480 break;
4481 case RID_STATCAST:
4482 postfix_expression
ebd21de4 4483 = build_static_cast (type, expression, tf_warning_or_error);
0a3b29ad 4484 break;
4485 case RID_REINTCAST:
4486 postfix_expression
ebd21de4 4487 = build_reinterpret_cast (type, expression,
4488 tf_warning_or_error);
0a3b29ad 4489 break;
4490 case RID_CONSTCAST:
4491 postfix_expression
ebd21de4 4492 = build_const_cast (type, expression, tf_warning_or_error);
0a3b29ad 4493 break;
4494 default:
2e3e31d2 4495 gcc_unreachable ();
0a3b29ad 4496 }
4497 }
4498 break;
4499
4500 case RID_TYPEID:
4501 {
4502 tree type;
4503 const char *saved_message;
41f2d08e 4504 bool saved_in_type_id_in_expr_p;
0a3b29ad 4505
4506 /* Consume the `typeid' token. */
4507 cp_lexer_consume_token (parser->lexer);
4508 /* Look for the `(' token. */
640710cf 4509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 4510 /* Types cannot be defined in a `typeid' expression. */
4511 saved_message = parser->type_definition_forbidden_message;
4512 parser->type_definition_forbidden_message
2e52ac87 4513 = "types may not be defined in a %<typeid%> expression";
0a3b29ad 4514 /* We can't be sure yet whether we're looking at a type-id or an
4515 expression. */
4516 cp_parser_parse_tentatively (parser);
4517 /* Try a type-id first. */
41f2d08e 4518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519 parser->in_type_id_in_expr_p = true;
0a3b29ad 4520 type = cp_parser_type_id (parser);
41f2d08e 4521 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 4522 /* Look for the `)' token. Otherwise, we can't be sure that
4523 we're not looking at an expression: consider `typeid (int
4524 (3))', for example. */
640710cf 4525 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 4526 /* If all went well, simply lookup the type-id. */
4527 if (cp_parser_parse_definitely (parser))
4528 postfix_expression = get_typeid (type);
4529 /* Otherwise, fall back to the expression variant. */
4530 else
4531 {
4532 tree expression;
4533
4534 /* Look for an expression. */
98b326fd 4535 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
0a3b29ad 4536 /* Compute its typeid. */
4537 postfix_expression = build_typeid (expression);
4538 /* Look for the `)' token. */
640710cf 4539 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 4540 }
4a430fd7 4541 /* Restore the saved message. */
4542 parser->type_definition_forbidden_message = saved_message;
368ad4b9 4543 /* `typeid' may not appear in an integral constant expression. */
207355ad 4544 if (cp_parser_non_integral_constant_expression(parser,
7222be86 4545 "%<typeid%> operator"))
368ad4b9 4546 return error_mark_node;
0a3b29ad 4547 }
4548 break;
ccb84981 4549
0a3b29ad 4550 case RID_TYPENAME:
4551 {
0a3b29ad 4552 tree type;
93673597 4553 /* The syntax permitted here is the same permitted for an
4554 elaborated-type-specifier. */
4555 type = cp_parser_elaborated_type_specifier (parser,
4556 /*is_friend=*/false,
4557 /*is_declaration=*/false);
0a3b29ad 4558 postfix_expression = cp_parser_functional_cast (parser, type);
4559 }
4560 break;
4561
4562 default:
4563 {
4564 tree type;
4565
4566 /* If the next thing is a simple-type-specifier, we may be
4567 looking at a functional cast. We could also be looking at
4568 an id-expression. So, we try the functional cast, and if
4569 that doesn't work we fall back to the primary-expression. */
4570 cp_parser_parse_tentatively (parser);
4571 /* Look for the simple-type-specifier. */
ccb84981 4572 type = cp_parser_simple_type_specifier (parser,
4b9b2871 4573 /*decl_specs=*/NULL,
4574 CP_PARSER_FLAGS_NONE);
0a3b29ad 4575 /* Parse the cast itself. */
4576 if (!cp_parser_error_occurred (parser))
ccb84981 4577 postfix_expression
0a3b29ad 4578 = cp_parser_functional_cast (parser, type);
4579 /* If that worked, we're done. */
4580 if (cp_parser_parse_definitely (parser))
4581 break;
4582
4583 /* If the functional-cast didn't work out, try a
4584 compound-literal. */
5f6526e1 4585 if (cp_parser_allow_gnu_extensions_p (parser)
4586 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
0a3b29ad 4587 {
c75b4594 4588 VEC(constructor_elt,gc) *initializer_list = NULL;
41f2d08e 4589 bool saved_in_type_id_in_expr_p;
0a3b29ad 4590
4591 cp_parser_parse_tentatively (parser);
5f6526e1 4592 /* Consume the `('. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Parse the type. */
41f2d08e 4595 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596 parser->in_type_id_in_expr_p = true;
5f6526e1 4597 type = cp_parser_type_id (parser);
41f2d08e 4598 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5f6526e1 4599 /* Look for the `)'. */
640710cf 4600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5f6526e1 4601 /* Look for the `{'. */
640710cf 4602 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
5f6526e1 4603 /* If things aren't going well, there's no need to
4604 keep going. */
4605 if (!cp_parser_error_occurred (parser))
0a3b29ad 4606 {
878870b4 4607 bool non_constant_p;
5f6526e1 4608 /* Parse the initializer-list. */
ccb84981 4609 initializer_list
878870b4 4610 = cp_parser_initializer_list (parser, &non_constant_p);
5f6526e1 4611 /* Allow a trailing `,'. */
4612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the final `}'. */
640710cf 4615 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 4616 }
4617 /* If that worked, we're definitely looking at a
4618 compound-literal expression. */
4619 if (cp_parser_parse_definitely (parser))
4620 {
4621 /* Warn the user that a compound literal is not
4622 allowed in standard C++. */
21ca8540 4623 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
93b25def 4624 /* For simplicity, we disallow compound literals in
4625 constant-expressions. We could
66a0bac0 4626 allow compound literals of integer type, whose
4627 initializer was a constant, in constant
4628 expressions. Permitting that usage, as a further
4629 extension, would not change the meaning of any
4630 currently accepted programs. (Of course, as
4631 compound literals are not part of ISO C++, the
4632 standard has nothing to say.) */
4633 if (cp_parser_non_integral_constant_expression
4634 (parser, "non-constant compound literals"))
4635 {
4636 postfix_expression = error_mark_node;
4637 break;
4638 }
0a3b29ad 4639 /* Form the representation of the compound-literal. */
ccb84981 4640 postfix_expression
f82f1250 4641 = (finish_compound_literal
4642 (type, build_constructor (init_list_type_node,
4643 initializer_list)));
0a3b29ad 4644 break;
4645 }
4646 }
4647
4648 /* It must be a primary-expression. */
074ab442 4649 postfix_expression
4650 = cp_parser_primary_expression (parser, address_p, cast_p,
fbb01da7 4651 /*template_arg_p=*/false,
4652 &idk);
0a3b29ad 4653 }
4654 break;
4655 }
4656
0a3b29ad 4657 /* Keep looping until the postfix-expression is complete. */
4658 while (true)
4659 {
c08d51be 4660 if (idk == CP_ID_KIND_UNQUALIFIED
4661 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
0a3b29ad 4662 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
0886adbc 4663 /* It is not a Koenig lookup function call. */
ccb84981 4664 postfix_expression
0886adbc 4665 = unqualified_name_lookup_error (postfix_expression);
ccb84981 4666
0a3b29ad 4667 /* Peek at the next token. */
4668 token = cp_lexer_peek_token (parser->lexer);
4669
4670 switch (token->type)
4671 {
4672 case CPP_OPEN_SQUARE:
43bf5d72 4673 postfix_expression
4674 = cp_parser_postfix_open_square_expression (parser,
4675 postfix_expression,
4676 false);
4677 idk = CP_ID_KIND_NONE;
34da8800 4678 is_member_access = false;
0a3b29ad 4679 break;
4680
4681 case CPP_OPEN_PAREN:
4682 /* postfix-expression ( expression-list [opt] ) */
4683 {
cbce34a5 4684 bool koenig_p;
0dbadc68 4685 bool is_builtin_constant_p;
4686 bool saved_integral_constant_expression_p = false;
4687 bool saved_non_integral_constant_expression_p = false;
f352a3fb 4688 VEC(tree,gc) *args;
0dbadc68 4689
34da8800 4690 is_member_access = false;
4691
9031d10b 4692 is_builtin_constant_p
0dbadc68 4693 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694 if (is_builtin_constant_p)
4695 {
4696 /* The whole point of __builtin_constant_p is to allow
4697 non-constant expressions to appear as arguments. */
4698 saved_integral_constant_expression_p
4699 = parser->integral_constant_expression_p;
4700 saved_non_integral_constant_expression_p
4701 = parser->non_integral_constant_expression_p;
4702 parser->integral_constant_expression_p = false;
4703 }
4704 args = (cp_parser_parenthesized_expression_list
9031d10b 4705 (parser, /*is_attribute_list=*/false,
d95d815d 4706 /*cast_p=*/false, /*allow_expansion_p=*/true,
0dbadc68 4707 /*non_constant_p=*/NULL));
4708 if (is_builtin_constant_p)
4709 {
4710 parser->integral_constant_expression_p
4711 = saved_integral_constant_expression_p;
4712 parser->non_integral_constant_expression_p
4713 = saved_non_integral_constant_expression_p;
4714 }
0a3b29ad 4715
f352a3fb 4716 if (args == NULL)
0986fa22 4717 {
4718 postfix_expression = error_mark_node;
4719 break;
4720 }
ccb84981 4721
5f6526e1 4722 /* Function calls are not permitted in
4723 constant-expressions. */
3a88b1db 4724 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725 && cp_parser_non_integral_constant_expression (parser,
4726 "a function call"))
5f6526e1 4727 {
3938e0c2 4728 postfix_expression = error_mark_node;
f352a3fb 4729 release_tree_vector (args);
3938e0c2 4730 break;
5f6526e1 4731 }
0a3b29ad 4732
cbce34a5 4733 koenig_p = false;
32f71e4f 4734 if (idk == CP_ID_KIND_UNQUALIFIED
4735 || idk == CP_ID_KIND_TEMPLATE_ID)
b7edeb23 4736 {
0e43a566 4737 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4738 {
f352a3fb 4739 if (!VEC_empty (tree, args))
0e43a566 4740 {
4741 koenig_p = true;
ba026663 4742 if (!any_type_dependent_arguments_p (args))
4743 postfix_expression
4744 = perform_koenig_lookup (postfix_expression, args);
0e43a566 4745 }
4746 else
4747 postfix_expression
4748 = unqualified_fn_lookup_error (postfix_expression);
4749 }
3ed12242 4750 /* We do not perform argument-dependent lookup if
4751 normal lookup finds a non-function, in accordance
4752 with the expected resolution of DR 218. */
f352a3fb 4753 else if (!VEC_empty (tree, args)
4754 && is_overloaded_fn (postfix_expression))
cbce34a5 4755 {
0e43a566 4756 tree fn = get_first_fn (postfix_expression);
4757
4758 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4759 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4760
4761 /* Only do argument dependent lookup if regular
4762 lookup does not find a set of member functions.
4763 [basic.lookup.koenig]/2a */
4764 if (!DECL_FUNCTION_MEMBER_P (fn))
4765 {
4766 koenig_p = true;
ba026663 4767 if (!any_type_dependent_arguments_p (args))
4768 postfix_expression
4769 = perform_koenig_lookup (postfix_expression, args);
0e43a566 4770 }
cbce34a5 4771 }
b7edeb23 4772 }
ccb84981 4773
13795292 4774 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
0a3b29ad 4775 {
13795292 4776 tree instance = TREE_OPERAND (postfix_expression, 0);
4777 tree fn = TREE_OPERAND (postfix_expression, 1);
4778
4779 if (processing_template_decl
4780 && (type_dependent_expression_p (instance)
4781 || (!BASELINK_P (fn)
4782 && TREE_CODE (fn) != FIELD_DECL)
ed45372d 4783 || type_dependent_expression_p (fn)
13795292 4784 || any_type_dependent_arguments_p (args)))
4785 {
4786 postfix_expression
f352a3fb 4787 = build_nt_call_vec (postfix_expression, args);
4788 release_tree_vector (args);
13795292 4789 break;
4790 }
3c33f9f3 4791
4792 if (BASELINK_P (fn))
98b326fd 4793 {
3c33f9f3 4794 postfix_expression
ccb84981 4795 = (build_new_method_call
f352a3fb 4796 (instance, fn, &args, NULL_TREE,
ccb84981 4797 (idk == CP_ID_KIND_QUALIFIED
393f878f 4798 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
ebd21de4 4799 /*fn_p=*/NULL,
4800 tf_warning_or_error));
98b326fd 4801 }
3c33f9f3 4802 else
4803 postfix_expression
f352a3fb 4804 = finish_call_expr (postfix_expression, &args,
3c33f9f3 4805 /*disallow_virtual=*/false,
ebd21de4 4806 /*koenig_p=*/false,
4807 tf_warning_or_error);
0a3b29ad 4808 }
13795292 4809 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4810 || TREE_CODE (postfix_expression) == MEMBER_REF
4811 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
0a3b29ad 4812 postfix_expression = (build_offset_ref_call_from_tree
f352a3fb 4813 (postfix_expression, &args));
0886adbc 4814 else if (idk == CP_ID_KIND_QUALIFIED)
b3c48b5d 4815 /* A call to a static class member, or a namespace-scope
4816 function. */
4817 postfix_expression
f352a3fb 4818 = finish_call_expr (postfix_expression, &args,
cbce34a5 4819 /*disallow_virtual=*/true,
ebd21de4 4820 koenig_p,
4821 tf_warning_or_error);
0a3b29ad 4822 else
b3c48b5d 4823 /* All other function calls. */
ccb84981 4824 postfix_expression
f352a3fb 4825 = finish_call_expr (postfix_expression, &args,
cbce34a5 4826 /*disallow_virtual=*/false,
ebd21de4 4827 koenig_p,
4828 tf_warning_or_error);
0a3b29ad 4829
4830 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
0886adbc 4831 idk = CP_ID_KIND_NONE;
f352a3fb 4832
4833 release_tree_vector (args);
0a3b29ad 4834 }
4835 break;
ccb84981 4836
0a3b29ad 4837 case CPP_DOT:
4838 case CPP_DEREF:
ccb84981 4839 /* postfix-expression . template [opt] id-expression
4840 postfix-expression . pseudo-destructor-name
0a3b29ad 4841 postfix-expression -> template [opt] id-expression
4842 postfix-expression -> pseudo-destructor-name */
207355ad 4843
43bf5d72 4844 /* Consume the `.' or `->' operator. */
4845 cp_lexer_consume_token (parser->lexer);
0a3b29ad 4846
43bf5d72 4847 postfix_expression
4848 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4849 postfix_expression,
ad9ae192 4850 false, &idk,
4851 token->location);
34da8800 4852
4853 is_member_access = true;
0a3b29ad 4854 break;
4855
4856 case CPP_PLUS_PLUS:
4857 /* postfix-expression ++ */
4858 /* Consume the `++' token. */
4859 cp_lexer_consume_token (parser->lexer);
364c2f43 4860 /* Generate a representation for the complete expression. */
ccb84981 4861 postfix_expression
4862 = finish_increment_expr (postfix_expression,
364c2f43 4863 POSTINCREMENT_EXPR);
5f6526e1 4864 /* Increments may not appear in constant-expressions. */
3938e0c2 4865 if (cp_parser_non_integral_constant_expression (parser,
4866 "an increment"))
4867 postfix_expression = error_mark_node;
0886adbc 4868 idk = CP_ID_KIND_NONE;
34da8800 4869 is_member_access = false;
0a3b29ad 4870 break;
4871
4872 case CPP_MINUS_MINUS:
4873 /* postfix-expression -- */
4874 /* Consume the `--' token. */
4875 cp_lexer_consume_token (parser->lexer);
364c2f43 4876 /* Generate a representation for the complete expression. */
ccb84981 4877 postfix_expression
4878 = finish_increment_expr (postfix_expression,
364c2f43 4879 POSTDECREMENT_EXPR);
5f6526e1 4880 /* Decrements may not appear in constant-expressions. */
3938e0c2 4881 if (cp_parser_non_integral_constant_expression (parser,
4882 "a decrement"))
4883 postfix_expression = error_mark_node;
0886adbc 4884 idk = CP_ID_KIND_NONE;
34da8800 4885 is_member_access = false;
0a3b29ad 4886 break;
4887
4888 default:
98b326fd 4889 if (pidk_return != NULL)
4890 * pidk_return = idk;
34da8800 4891 if (member_access_only_p)
4892 return is_member_access? postfix_expression : error_mark_node;
4893 else
4894 return postfix_expression;
0a3b29ad 4895 }
4896 }
4897
4898 /* We should never get here. */
2e3e31d2 4899 gcc_unreachable ();
0a3b29ad 4900 return error_mark_node;
4901}
4902
43bf5d72 4903/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4904 by cp_parser_builtin_offsetof. We're looking for
4905
4906 postfix-expression [ expression ]
4907
4908 FOR_OFFSETOF is set if we're being called in that context, which
4909 changes how we deal with integer constant expressions. */
4910
4911static tree
4912cp_parser_postfix_open_square_expression (cp_parser *parser,
4913 tree postfix_expression,
4914 bool for_offsetof)
4915{
4916 tree index;
4917
4918 /* Consume the `[' token. */
4919 cp_lexer_consume_token (parser->lexer);
4920
4921 /* Parse the index expression. */
4922 /* ??? For offsetof, there is a question of what to allow here. If
4923 offsetof is not being used in an integral constant expression context,
4924 then we *could* get the right answer by computing the value at runtime.
4925 If we are in an integral constant expression context, then we might
4926 could accept any constant expression; hard to say without analysis.
4927 Rather than open the barn door too wide right away, allow only integer
4a44ba29 4928 constant expressions here. */
43bf5d72 4929 if (for_offsetof)
4930 index = cp_parser_constant_expression (parser, false, NULL);
4931 else
98b326fd 4932 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
43bf5d72 4933
4934 /* Look for the closing `]'. */
640710cf 4935 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
43bf5d72 4936
4937 /* Build the ARRAY_REF. */
4938 postfix_expression = grok_array_decl (postfix_expression, index);
4939
4940 /* When not doing offsetof, array references are not permitted in
4941 constant-expressions. */
4942 if (!for_offsetof
4943 && (cp_parser_non_integral_constant_expression
4944 (parser, "an array reference")))
4945 postfix_expression = error_mark_node;
4946
4947 return postfix_expression;
4948}
4949
4950/* A subroutine of cp_parser_postfix_expression that also gets hijacked
4951 by cp_parser_builtin_offsetof. We're looking for
4952
4953 postfix-expression . template [opt] id-expression
4954 postfix-expression . pseudo-destructor-name
4955 postfix-expression -> template [opt] id-expression
4956 postfix-expression -> pseudo-destructor-name
4957
4958 FOR_OFFSETOF is set if we're being called in that context. That sorta
4959 limits what of the above we'll actually accept, but nevermind.
4960 TOKEN_TYPE is the "." or "->" token, which will already have been
4961 removed from the stream. */
4962
4963static tree
4964cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4965 enum cpp_ttype token_type,
4966 tree postfix_expression,
ad9ae192 4967 bool for_offsetof, cp_id_kind *idk,
4968 location_t location)
43bf5d72 4969{
4970 tree name;
4971 bool dependent_p;
19efe073 4972 bool pseudo_destructor_p;
43bf5d72 4973 tree scope = NULL_TREE;
4974
4975 /* If this is a `->' operator, dereference the pointer. */
4976 if (token_type == CPP_DEREF)
4977 postfix_expression = build_x_arrow (postfix_expression);
4978 /* Check to see whether or not the expression is type-dependent. */
4979 dependent_p = type_dependent_expression_p (postfix_expression);
4980 /* The identifier following the `->' or `.' is not qualified. */
4981 parser->scope = NULL_TREE;
4982 parser->qualifying_scope = NULL_TREE;
4983 parser->object_scope = NULL_TREE;
4984 *idk = CP_ID_KIND_NONE;
98b326fd 4985
43bf5d72 4986 /* Enter the scope corresponding to the type of the object
4987 given by the POSTFIX_EXPRESSION. */
130bb1d4 4988 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
43bf5d72 4989 {
130bb1d4 4990 scope = TREE_TYPE (postfix_expression);
43bf5d72 4991 /* According to the standard, no expression should ever have
4992 reference type. Unfortunately, we do not currently match
4993 the standard in this respect in that our internal representation
4994 of an expression may have reference type even when the standard
4995 says it does not. Therefore, we have to manually obtain the
4996 underlying type here. */
4997 scope = non_reference (scope);
4998 /* The type of the POSTFIX_EXPRESSION must be complete. */
e3efb139 4999 if (scope == unknown_type_node)
5000 {
ad9ae192 5001 error ("%H%qE does not have class type", &location, postfix_expression);
e3efb139 5002 scope = NULL_TREE;
5003 }
130bb1d4 5004 else
e3efb139 5005 scope = complete_type_or_else (scope, NULL_TREE);
130bb1d4 5006 /* Let the name lookup machinery know that we are processing a
5007 class member access expression. */
5008 parser->context->object_type = scope;
43bf5d72 5009 /* If something went wrong, we want to be able to discern that case,
5010 as opposed to the case where there was no SCOPE due to the type
5011 of expression being dependent. */
5012 if (!scope)
5013 scope = error_mark_node;
5014 /* If the SCOPE was erroneous, make the various semantic analysis
5015 functions exit quickly -- and without issuing additional error
5016 messages. */
5017 if (scope == error_mark_node)
5018 postfix_expression = error_mark_node;
5019 }
5020
19efe073 5021 /* Assume this expression is not a pseudo-destructor access. */
5022 pseudo_destructor_p = false;
5023
5024 /* If the SCOPE is a scalar type, then, if this is a valid program,
8da7c9e9 5025 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5026 is type dependent, it can be pseudo-destructor-name or something else.
5027 Try to parse it as pseudo-destructor-name first. */
5028 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
43bf5d72 5029 {
19efe073 5030 tree s;
5031 tree type;
5032
5033 cp_parser_parse_tentatively (parser);
5034 /* Parse the pseudo-destructor-name. */
5035 s = NULL_TREE;
5036 cp_parser_pseudo_destructor_name (parser, &s, &type);
8da7c9e9 5037 if (dependent_p
5038 && (cp_parser_error_occurred (parser)
5039 || TREE_CODE (type) != TYPE_DECL
5040 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5041 cp_parser_abort_tentative_parse (parser);
5042 else if (cp_parser_parse_definitely (parser))
19efe073 5043 {
5044 pseudo_destructor_p = true;
5045 postfix_expression
5046 = finish_pseudo_destructor_expr (postfix_expression,
5047 s, TREE_TYPE (type));
5048 }
5049 }
5050
5051 if (!pseudo_destructor_p)
5052 {
5053 /* If the SCOPE is not a scalar type, we are looking at an
5054 ordinary class member access expression, rather than a
5055 pseudo-destructor-name. */
fbb01da7 5056 bool template_p;
ad9ae192 5057 cp_token *token = cp_lexer_peek_token (parser->lexer);
43bf5d72 5058 /* Parse the id-expression. */
074ab442 5059 name = (cp_parser_id_expression
5060 (parser,
fbb01da7 5061 cp_parser_optional_template_keyword (parser),
5062 /*check_dependency_p=*/true,
5063 &template_p,
197c9df7 5064 /*declarator_p=*/false,
130bb1d4 5065 /*optional_p=*/false));
43bf5d72 5066 /* In general, build a SCOPE_REF if the member name is qualified.
5067 However, if the name was not dependent and has already been
5068 resolved; there is no need to build the SCOPE_REF. For example;
5069
653e5405 5070 struct X { void f(); };
5071 template <typename T> void f(T* t) { t->X::f(); }
43bf5d72 5072
5073 Even though "t" is dependent, "X::f" is not and has been resolved
5074 to a BASELINK; there is no need to include scope information. */
5075
5076 /* But we do need to remember that there was an explicit scope for
5077 virtual function calls. */
5078 if (parser->scope)
5079 *idk = CP_ID_KIND_QUALIFIED;
5080
e2ae55f2 5081 /* If the name is a template-id that names a type, we will get a
5082 TYPE_DECL here. That is invalid code. */
5083 if (TREE_CODE (name) == TYPE_DECL)
43bf5d72 5084 {
ad9ae192 5085 error ("%Hinvalid use of %qD", &token->location, name);
e2ae55f2 5086 postfix_expression = error_mark_node;
5087 }
5088 else
5089 {
5090 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5091 {
fbb01da7 5092 name = build_qualified_name (/*type=*/NULL_TREE,
5093 parser->scope,
5094 name,
5095 template_p);
e2ae55f2 5096 parser->scope = NULL_TREE;
5097 parser->qualifying_scope = NULL_TREE;
5098 parser->object_scope = NULL_TREE;
5099 }
5100 if (scope && name && BASELINK_P (name))
5101 adjust_result_of_qualified_name_lookup
c784dc88 5102 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
e2ae55f2 5103 postfix_expression
fbb01da7 5104 = finish_class_member_access_expr (postfix_expression, name,
ebd21de4 5105 template_p,
5106 tf_warning_or_error);
43bf5d72 5107 }
43bf5d72 5108 }
43bf5d72 5109
5110 /* We no longer need to look up names in the scope of the object on
5111 the left-hand side of the `.' or `->' operator. */
5112 parser->context->object_type = NULL_TREE;
5113
5114 /* Outside of offsetof, these operators may not appear in
5115 constant-expressions. */
5116 if (!for_offsetof
207355ad 5117 && (cp_parser_non_integral_constant_expression
7222be86 5118 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
43bf5d72 5119 postfix_expression = error_mark_node;
5120
5121 return postfix_expression;
5122}
5123
0986fa22 5124/* Parse a parenthesized expression-list.
0a3b29ad 5125
5126 expression-list:
5127 assignment-expression
5128 expression-list, assignment-expression
5129
0986fa22 5130 attribute-list:
5131 expression-list
5132 identifier
5133 identifier, expression-list
5134
640aa28c 5135 CAST_P is true if this expression is the target of a cast.
5136
d95d815d 5137 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5138 argument pack.
5139
f352a3fb 5140 Returns a vector of trees. Each element is a representation of an
5141 assignment-expression. NULL is returned if the ( and or ) are
5142 missing. An empty, but allocated, vector is returned on no
5143 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5144 if this is really an attribute list being parsed. If
5145 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5146 not all of the expressions in the list were constant. */
0a3b29ad 5147
f352a3fb 5148static VEC(tree,gc) *
ccb84981 5149cp_parser_parenthesized_expression_list (cp_parser* parser,
878870b4 5150 bool is_attribute_list,
640aa28c 5151 bool cast_p,
d95d815d 5152 bool allow_expansion_p,
878870b4 5153 bool *non_constant_p)
0a3b29ad 5154{
f352a3fb 5155 VEC(tree,gc) *expression_list;
4736f3be 5156 bool fold_expr_p = is_attribute_list;
0986fa22 5157 tree identifier = NULL_TREE;
9247ecc6 5158 bool saved_greater_than_is_operator_p;
878870b4 5159
5160 /* Assume all the expressions will be constant. */
5161 if (non_constant_p)
5162 *non_constant_p = false;
5163
640710cf 5164 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
f352a3fb 5165 return NULL;
5166
5167 expression_list = make_tree_vector ();
ccb84981 5168
9247ecc6 5169 /* Within a parenthesized expression, a `>' token is always
5170 the greater-than operator. */
5171 saved_greater_than_is_operator_p
5172 = parser->greater_than_is_operator_p;
5173 parser->greater_than_is_operator_p = true;
5174
0a3b29ad 5175 /* Consume expressions until there are no more. */
0986fa22 5176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5177 while (true)
5178 {
5179 tree expr;
ccb84981 5180
0986fa22 5181 /* At the beginning of attribute lists, check to see if the
5182 next token is an identifier. */
5183 if (is_attribute_list
5184 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5185 {
5186 cp_token *token;
ccb84981 5187
0986fa22 5188 /* Consume the identifier. */
5189 token = cp_lexer_consume_token (parser->lexer);
5190 /* Save the identifier. */
3369eb76 5191 identifier = token->u.value;
0986fa22 5192 }
5193 else
5194 {
f82f1250 5195 bool expr_non_constant_p;
5196
0986fa22 5197 /* Parse the next assignment-expression. */
f82f1250 5198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5199 {
5200 /* A braced-init-list. */
5201 maybe_warn_cpp0x ("extended initializer lists");
5202 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5203 if (non_constant_p && expr_non_constant_p)
5204 *non_constant_p = true;
5205 }
5206 else if (non_constant_p)
878870b4 5207 {
ccb84981 5208 expr = (cp_parser_constant_expression
878870b4 5209 (parser, /*allow_non_constant_p=*/true,
5210 &expr_non_constant_p));
5211 if (expr_non_constant_p)
5212 *non_constant_p = true;
5213 }
5214 else
98b326fd 5215 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
0a3b29ad 5216
4736f3be 5217 if (fold_expr_p)
5218 expr = fold_non_dependent_expr (expr);
5219
d95d815d 5220 /* If we have an ellipsis, then this is an expression
5221 expansion. */
5222 if (allow_expansion_p
5223 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5224 {
5225 /* Consume the `...'. */
5226 cp_lexer_consume_token (parser->lexer);
5227
5228 /* Build the argument pack. */
5229 expr = make_pack_expansion (expr);
5230 }
5231
0986fa22 5232 /* Add it to the list. We add error_mark_node
5233 expressions to the list, so that we can still tell if
5234 the correct form for a parenthesized expression-list
5235 is found. That gives better errors. */
f352a3fb 5236 VEC_safe_push (tree, gc, expression_list, expr);
0a3b29ad 5237
0986fa22 5238 if (expr == error_mark_node)
5239 goto skip_comma;
5240 }
0a3b29ad 5241
0986fa22 5242 /* After the first item, attribute lists look the same as
5243 expression lists. */
5244 is_attribute_list = false;
ccb84981 5245
0986fa22 5246 get_comma:;
5247 /* If the next token isn't a `,', then we are done. */
5248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5249 break;
5250
5251 /* Otherwise, consume the `,' and keep going. */
5252 cp_lexer_consume_token (parser->lexer);
5253 }
ccb84981 5254
640710cf 5255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
0986fa22 5256 {
5257 int ending;
ccb84981 5258
0986fa22 5259 skip_comma:;
5260 /* We try and resync to an unnested comma, as that will give the
5261 user better diagnostics. */
ccb84981 5262 ending = cp_parser_skip_to_closing_parenthesis (parser,
5263 /*recovering=*/true,
92b128ed 5264 /*or_comma=*/true,
3d0f901b 5265 /*consume_paren=*/true);
0986fa22 5266 if (ending < 0)
5267 goto get_comma;
5268 if (!ending)
9247ecc6 5269 {
5270 parser->greater_than_is_operator_p
5271 = saved_greater_than_is_operator_p;
f352a3fb 5272 return NULL;
9247ecc6 5273 }
0a3b29ad 5274 }
5275
9247ecc6 5276 parser->greater_than_is_operator_p
5277 = saved_greater_than_is_operator_p;
5278
0986fa22 5279 if (identifier)
f352a3fb 5280 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
ccb84981 5281
0986fa22 5282 return expression_list;
0a3b29ad 5283}
5284
5285/* Parse a pseudo-destructor-name.
5286
5287 pseudo-destructor-name:
5288 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5289 :: [opt] nested-name-specifier template template-id :: ~ type-name
5290 :: [opt] nested-name-specifier [opt] ~ type-name
5291
5292 If either of the first two productions is used, sets *SCOPE to the
5293 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5294 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
60c6ea84 5295 or ERROR_MARK_NODE if the parse fails. */
0a3b29ad 5296
5297static void
ccb84981 5298cp_parser_pseudo_destructor_name (cp_parser* parser,
653e5405 5299 tree* scope,
5300 tree* type)
0a3b29ad 5301{
5302 bool nested_name_specifier_p;
5303
30aea172 5304 /* Assume that things will not work out. */
5305 *type = error_mark_node;
5306
0a3b29ad 5307 /* Look for the optional `::' operator. */
130bb1d4 5308 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
0a3b29ad 5309 /* Look for the optional nested-name-specifier. */
ccb84981 5310 nested_name_specifier_p
0a3b29ad 5311 = (cp_parser_nested_name_specifier_opt (parser,
5312 /*typename_keyword_p=*/false,
5313 /*check_dependency_p=*/true,
3d0f901b 5314 /*type_p=*/false,
d046014d 5315 /*is_declaration=*/false)
0a3b29ad 5316 != NULL_TREE);
5317 /* Now, if we saw a nested-name-specifier, we might be doing the
5318 second production. */
ccb84981 5319 if (nested_name_specifier_p
0a3b29ad 5320 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5321 {
5322 /* Consume the `template' keyword. */
5323 cp_lexer_consume_token (parser->lexer);
5324 /* Parse the template-id. */
ccb84981 5325 cp_parser_template_id (parser,
0a3b29ad 5326 /*template_keyword_p=*/true,
3d0f901b 5327 /*check_dependency_p=*/false,
5328 /*is_declaration=*/true);
0a3b29ad 5329 /* Look for the `::' token. */
640710cf 5330 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
0a3b29ad 5331 }
5332 /* If the next token is not a `~', then there might be some
6beb3f76 5333 additional qualification. */
0a3b29ad 5334 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5335 {
d1cc3060 5336 /* At this point, we're looking for "type-name :: ~". The type-name
5337 must not be a class-name, since this is a pseudo-destructor. So,
5338 it must be either an enum-name, or a typedef-name -- both of which
5339 are just identifiers. So, we peek ahead to check that the "::"
5340 and "~" tokens are present; if they are not, then we can avoid
5341 calling type_name. */
5342 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5343 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5344 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5345 {
5346 cp_parser_error (parser, "non-scalar type");
5347 return;
5348 }
5349
0a3b29ad 5350 /* Look for the type-name. */
674e90bd 5351 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
30aea172 5352 if (*scope == error_mark_node)
5353 return;
5354
0a3b29ad 5355 /* Look for the `::' token. */
640710cf 5356 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
0a3b29ad 5357 }
5358 else
5359 *scope = NULL_TREE;
5360
5361 /* Look for the `~'. */
640710cf 5362 cp_parser_require (parser, CPP_COMPL, "%<~%>");
0a3b29ad 5363 /* Look for the type-name again. We are not responsible for
5364 checking that it matches the first type-name. */
674e90bd 5365 *type = cp_parser_nonclass_name (parser);
0a3b29ad 5366}
5367
5368/* Parse a unary-expression.
5369
5370 unary-expression:
5371 postfix-expression
5372 ++ cast-expression
5373 -- cast-expression
5374 unary-operator cast-expression
5375 sizeof unary-expression
5376 sizeof ( type-id )
5377 new-expression
5378 delete-expression
5379
5380 GNU Extensions:
5381
5382 unary-expression:
5383 __extension__ cast-expression
5384 __alignof__ unary-expression
5385 __alignof__ ( type-id )
5386 __real__ cast-expression
5387 __imag__ cast-expression
5388 && identifier
5389
5390 ADDRESS_P is true iff the unary-expression is appearing as the
640aa28c 5391 operand of the `&' operator. CAST_P is true if this expression is
5392 the target of a cast.
0a3b29ad 5393
755edffd 5394 Returns a representation of the expression. */
0a3b29ad 5395
5396static tree
98b326fd 5397cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5398 cp_id_kind * pidk)
0a3b29ad 5399{
5400 cp_token *token;
5401 enum tree_code unary_operator;
5402
5403 /* Peek at the next token. */
5404 token = cp_lexer_peek_token (parser->lexer);
5405 /* Some keywords give away the kind of expression. */
5406 if (token->type == CPP_KEYWORD)
5407 {
5408 enum rid keyword = token->keyword;
5409
5410 switch (keyword)
5411 {
5412 case RID_ALIGNOF:
0a3b29ad 5413 case RID_SIZEOF:
5414 {
5415 tree operand;
e47f82ba 5416 enum tree_code op;
ccb84981 5417
e47f82ba 5418 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5419 /* Consume the token. */
0a3b29ad 5420 cp_lexer_consume_token (parser->lexer);
5421 /* Parse the operand. */
5422 operand = cp_parser_sizeof_operand (parser, keyword);
5423
e47f82ba 5424 if (TYPE_P (operand))
5425 return cxx_sizeof_or_alignof_type (operand, op, true);
0a3b29ad 5426 else
ebd21de4 5427 return cxx_sizeof_or_alignof_expr (operand, op, true);
0a3b29ad 5428 }
5429
5430 case RID_NEW:
5431 return cp_parser_new_expression (parser);
5432
5433 case RID_DELETE:
5434 return cp_parser_delete_expression (parser);
ccb84981 5435
0a3b29ad 5436 case RID_EXTENSION:
5437 {
5438 /* The saved value of the PEDANTIC flag. */
5439 int saved_pedantic;
5440 tree expr;
5441
5442 /* Save away the PEDANTIC flag. */
5443 cp_parser_extension_opt (parser, &saved_pedantic);
5444 /* Parse the cast-expression. */
a63bc44c 5445 expr = cp_parser_simple_cast_expression (parser);
0a3b29ad 5446 /* Restore the PEDANTIC flag. */
5447 pedantic = saved_pedantic;
5448
5449 return expr;
5450 }
5451
5452 case RID_REALPART:
5453 case RID_IMAGPART:
5454 {
5455 tree expression;
5456
5457 /* Consume the `__real__' or `__imag__' token. */
5458 cp_lexer_consume_token (parser->lexer);
5459 /* Parse the cast-expression. */
a63bc44c 5460 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 5461 /* Create the complete representation. */
5462 return build_x_unary_op ((keyword == RID_REALPART
5463 ? REALPART_EXPR : IMAGPART_EXPR),
ebd21de4 5464 expression,
5465 tf_warning_or_error);
0a3b29ad 5466 }
5467 break;
5468
5469 default:
5470 break;
5471 }
5472 }
5473
5474 /* Look for the `:: new' and `:: delete', which also signal the
5475 beginning of a new-expression, or delete-expression,
5476 respectively. If the next token is `::', then it might be one of
5477 these. */
5478 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5479 {
5480 enum rid keyword;
5481
5482 /* See if the token after the `::' is one of the keywords in
5483 which we're interested. */
5484 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5485 /* If it's `new', we have a new-expression. */
5486 if (keyword == RID_NEW)
5487 return cp_parser_new_expression (parser);
5488 /* Similarly, for `delete'. */
5489 else if (keyword == RID_DELETE)
5490 return cp_parser_delete_expression (parser);
5491 }
5492
5493 /* Look for a unary operator. */
5494 unary_operator = cp_parser_unary_operator (token);
5495 /* The `++' and `--' operators can be handled similarly, even though
5496 they are not technically unary-operators in the grammar. */
5497 if (unary_operator == ERROR_MARK)
5498 {
5499 if (token->type == CPP_PLUS_PLUS)
5500 unary_operator = PREINCREMENT_EXPR;
5501 else if (token->type == CPP_MINUS_MINUS)
5502 unary_operator = PREDECREMENT_EXPR;
5503 /* Handle the GNU address-of-label extension. */
5504 else if (cp_parser_allow_gnu_extensions_p (parser)
5505 && token->type == CPP_AND_AND)
5506 {
5507 tree identifier;
89966701 5508 tree expression;
dda49785 5509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 5510
5511 /* Consume the '&&' token. */
5512 cp_lexer_consume_token (parser->lexer);
5513 /* Look for the identifier. */
5514 identifier = cp_parser_identifier (parser);
5515 /* Create an expression representing the address. */
dda49785 5516 expression = finish_label_address_expr (identifier, loc);
89966701 5517 if (cp_parser_non_integral_constant_expression (parser,
5518 "the address of a label"))
5519 expression = error_mark_node;
5520 return expression;
0a3b29ad 5521 }
5522 }
5523 if (unary_operator != ERROR_MARK)
5524 {
5525 tree cast_expression;
364c2f43 5526 tree expression = error_mark_node;
5527 const char *non_constant_p = NULL;
0a3b29ad 5528
5529 /* Consume the operator token. */
5530 token = cp_lexer_consume_token (parser->lexer);
5531 /* Parse the cast-expression. */
ccb84981 5532 cast_expression
9031d10b 5533 = cp_parser_cast_expression (parser,
640aa28c 5534 unary_operator == ADDR_EXPR,
98b326fd 5535 /*cast_p=*/false, pidk);
0a3b29ad 5536 /* Now, build an appropriate representation. */
5537 switch (unary_operator)
5538 {
5539 case INDIRECT_REF:
7222be86 5540 non_constant_p = "%<*%>";
ebd21de4 5541 expression = build_x_indirect_ref (cast_expression, "unary *",
5542 tf_warning_or_error);
364c2f43 5543 break;
5544
0a3b29ad 5545 case ADDR_EXPR:
7222be86 5546 non_constant_p = "%<&%>";
364c2f43 5547 /* Fall through. */
13795292 5548 case BIT_NOT_EXPR:
ebd21de4 5549 expression = build_x_unary_op (unary_operator, cast_expression,
5550 tf_warning_or_error);
364c2f43 5551 break;
5552
5f6526e1 5553 case PREINCREMENT_EXPR:
5554 case PREDECREMENT_EXPR:
364c2f43 5555 non_constant_p = (unary_operator == PREINCREMENT_EXPR
7222be86 5556 ? "%<++%>" : "%<--%>");
5f6526e1 5557 /* Fall through. */
97d541d5 5558 case UNARY_PLUS_EXPR:
0a3b29ad 5559 case NEGATE_EXPR:
5560 case TRUTH_NOT_EXPR:
364c2f43 5561 expression = finish_unary_op_expr (unary_operator, cast_expression);
5562 break;
0a3b29ad 5563
0a3b29ad 5564 default:
2e3e31d2 5565 gcc_unreachable ();
0a3b29ad 5566 }
364c2f43 5567
207355ad 5568 if (non_constant_p
3938e0c2 5569 && cp_parser_non_integral_constant_expression (parser,
5570 non_constant_p))
5571 expression = error_mark_node;
364c2f43 5572
5573 return expression;
0a3b29ad 5574 }
5575
34da8800 5576 return cp_parser_postfix_expression (parser, address_p, cast_p,
98b326fd 5577 /*member_access_only_p=*/false,
5578 pidk);
0a3b29ad 5579}
5580
5581/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5582 unary-operator, the corresponding tree code is returned. */
5583
5584static enum tree_code
45baea8b 5585cp_parser_unary_operator (cp_token* token)
0a3b29ad 5586{
5587 switch (token->type)
5588 {
5589 case CPP_MULT:
5590 return INDIRECT_REF;
5591
5592 case CPP_AND:
5593 return ADDR_EXPR;
5594
5595 case CPP_PLUS:
97d541d5 5596 return UNARY_PLUS_EXPR;
0a3b29ad 5597
5598 case CPP_MINUS:
5599 return NEGATE_EXPR;
5600
5601 case CPP_NOT:
5602 return TRUTH_NOT_EXPR;
ccb84981 5603
0a3b29ad 5604 case CPP_COMPL:
5605 return BIT_NOT_EXPR;
5606
5607 default:
5608 return ERROR_MARK;
5609 }
5610}
5611
5612/* Parse a new-expression.
5613
5c6faf71 5614 new-expression:
0a3b29ad 5615 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5616 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5617
5618 Returns a representation of the expression. */
5619
5620static tree
45baea8b 5621cp_parser_new_expression (cp_parser* parser)
0a3b29ad 5622{
5623 bool global_scope_p;
f352a3fb 5624 VEC(tree,gc) *placement;
0a3b29ad 5625 tree type;
f352a3fb 5626 VEC(tree,gc) *initializer;
3046c0a3 5627 tree nelts;
f352a3fb 5628 tree ret;
0a3b29ad 5629
5630 /* Look for the optional `::' operator. */
ccb84981 5631 global_scope_p
0a3b29ad 5632 = (cp_parser_global_scope_opt (parser,
130bb1d4 5633 /*current_scope_valid_p=*/false)
0a3b29ad 5634 != NULL_TREE);
5635 /* Look for the `new' operator. */
640710cf 5636 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
0a3b29ad 5637 /* There's no easy way to tell a new-placement from the
5638 `( type-id )' construct. */
5639 cp_parser_parse_tentatively (parser);
5640 /* Look for a new-placement. */
5641 placement = cp_parser_new_placement (parser);
5642 /* If that didn't work out, there's no new-placement. */
5643 if (!cp_parser_parse_definitely (parser))
f352a3fb 5644 {
5645 if (placement != NULL)
5646 release_tree_vector (placement);
5647 placement = NULL;
5648 }
0a3b29ad 5649
5650 /* If the next token is a `(', then we have a parenthesized
5651 type-id. */
5652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5653 {
ad9ae192 5654 cp_token *token;
0a3b29ad 5655 /* Consume the `('. */
5656 cp_lexer_consume_token (parser->lexer);
5657 /* Parse the type-id. */
5658 type = cp_parser_type_id (parser);
5659 /* Look for the closing `)'. */
640710cf 5660 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
ad9ae192 5661 token = cp_lexer_peek_token (parser->lexer);
207355ad 5662 /* There should not be a direct-new-declarator in this production,
653e5405 5663 but GCC used to allowed this, so we check and emit a sensible error
383da0a4 5664 message for this case. */
5665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
f352cc1d 5666 {
ad9ae192 5667 error ("%Harray bound forbidden after parenthesized type-id",
5668 &token->location);
5bcc316e 5669 inform (token->location,
5670 "try removing the parentheses around the type-id");
383da0a4 5671 cp_parser_direct_new_declarator (parser);
5672 }
19efe073 5673 nelts = NULL_TREE;
0a3b29ad 5674 }
5675 /* Otherwise, there must be a new-type-id. */
5676 else
3046c0a3 5677 type = cp_parser_new_type_id (parser, &nelts);
0a3b29ad 5678
f82f1250 5679 /* If the next token is a `(' or '{', then we have a new-initializer. */
5680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5681 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 5682 initializer = cp_parser_new_initializer (parser);
5683 else
f352a3fb 5684 initializer = NULL;
0a3b29ad 5685
3938e0c2 5686 /* A new-expression may not appear in an integral constant
5687 expression. */
7222be86 5688 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
f352a3fb 5689 ret = error_mark_node;
5690 else
5691 {
5692 /* Create a representation of the new-expression. */
5693 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5694 tf_warning_or_error);
5695 }
3938e0c2 5696
f352a3fb 5697 if (placement != NULL)
5698 release_tree_vector (placement);
5699 if (initializer != NULL)
5700 release_tree_vector (initializer);
5701
5702 return ret;
0a3b29ad 5703}
5704
5705/* Parse a new-placement.
5706
5707 new-placement:
5708 ( expression-list )
5709
5710 Returns the same representation as for an expression-list. */
5711
f352a3fb 5712static VEC(tree,gc) *
45baea8b 5713cp_parser_new_placement (cp_parser* parser)
0a3b29ad 5714{
f352a3fb 5715 VEC(tree,gc) *expression_list;
0a3b29ad 5716
0a3b29ad 5717 /* Parse the expression-list. */
ccb84981 5718 expression_list = (cp_parser_parenthesized_expression_list
d95d815d 5719 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
640aa28c 5720 /*non_constant_p=*/NULL));
0a3b29ad 5721
5722 return expression_list;
5723}
5724
5725/* Parse a new-type-id.
5726
5727 new-type-id:
5728 type-specifier-seq new-declarator [opt]
5729
3046c0a3 5730 Returns the TYPE allocated. If the new-type-id indicates an array
5731 type, *NELTS is set to the number of elements in the last array
5732 bound; the TYPE will not include the last array bound. */
0a3b29ad 5733
5734static tree
3046c0a3 5735cp_parser_new_type_id (cp_parser* parser, tree *nelts)
0a3b29ad 5736{
4b9b2871 5737 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 5738 cp_declarator *new_declarator;
5739 cp_declarator *declarator;
5740 cp_declarator *outer_declarator;
0a3b29ad 5741 const char *saved_message;
3046c0a3 5742 tree type;
0a3b29ad 5743
5744 /* The type-specifier sequence must not contain type definitions.
5745 (It cannot contain declarations of new types either, but if they
5746 are not definitions we will catch that because they are not
5747 complete.) */
5748 saved_message = parser->type_definition_forbidden_message;
5749 parser->type_definition_forbidden_message
5750 = "types may not be defined in a new-type-id";
5751 /* Parse the type-specifier-seq. */
6f74fe3c 5752 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5753 &type_specifier_seq);
0a3b29ad 5754 /* Restore the old message. */
5755 parser->type_definition_forbidden_message = saved_message;
5756 /* Parse the new-declarator. */
3046c0a3 5757 new_declarator = cp_parser_new_declarator_opt (parser);
5758
5759 /* Determine the number of elements in the last array dimension, if
5760 any. */
5761 *nelts = NULL_TREE;
5762 /* Skip down to the last array dimension. */
5763 declarator = new_declarator;
5764 outer_declarator = NULL;
5765 while (declarator && (declarator->kind == cdk_pointer
5766 || declarator->kind == cdk_ptrmem))
5767 {
5768 outer_declarator = declarator;
5769 declarator = declarator->declarator;
5770 }
207355ad 5771 while (declarator
3046c0a3 5772 && declarator->kind == cdk_array
5773 && declarator->declarator
5774 && declarator->declarator->kind == cdk_array)
5775 {
5776 outer_declarator = declarator;
5777 declarator = declarator->declarator;
5778 }
207355ad 5779
3046c0a3 5780 if (declarator && declarator->kind == cdk_array)
5781 {
5782 *nelts = declarator->u.array.bounds;
5783 if (*nelts == error_mark_node)
5784 *nelts = integer_one_node;
9031d10b 5785
3046c0a3 5786 if (outer_declarator)
5787 outer_declarator->declarator = declarator->declarator;
5788 else
5789 new_declarator = NULL;
5790 }
0a3b29ad 5791
75eaa947 5792 type = groktypename (&type_specifier_seq, new_declarator, false);
3046c0a3 5793 return type;
0a3b29ad 5794}
5795
5796/* Parse an (optional) new-declarator.
5797
5798 new-declarator:
5799 ptr-operator new-declarator [opt]
5800 direct-new-declarator
5801
3046c0a3 5802 Returns the declarator. */
0a3b29ad 5803
3046c0a3 5804static cp_declarator *
45baea8b 5805cp_parser_new_declarator_opt (cp_parser* parser)
0a3b29ad 5806{
5807 enum tree_code code;
5808 tree type;
2cfb6cde 5809 cp_cv_quals cv_quals;
0a3b29ad 5810
5811 /* We don't know if there's a ptr-operator next, or not. */
5812 cp_parser_parse_tentatively (parser);
5813 /* Look for a ptr-operator. */
2cfb6cde 5814 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
0a3b29ad 5815 /* If that worked, look for more new-declarators. */
5816 if (cp_parser_parse_definitely (parser))
5817 {
3046c0a3 5818 cp_declarator *declarator;
0a3b29ad 5819
5820 /* Parse another optional declarator. */
5821 declarator = cp_parser_new_declarator_opt (parser);
5822
63949b38 5823 return cp_parser_make_indirect_declarator
5824 (code, type, cv_quals, declarator);
0a3b29ad 5825 }
5826
5827 /* If the next token is a `[', there is a direct-new-declarator. */
5828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5829 return cp_parser_direct_new_declarator (parser);
5830
3046c0a3 5831 return NULL;
0a3b29ad 5832}
5833
5834/* Parse a direct-new-declarator.
5835
5836 direct-new-declarator:
5837 [ expression ]
ccb84981 5838 direct-new-declarator [constant-expression]
0a3b29ad 5839
3046c0a3 5840 */
0a3b29ad 5841
3046c0a3 5842static cp_declarator *
45baea8b 5843cp_parser_direct_new_declarator (cp_parser* parser)
0a3b29ad 5844{
3046c0a3 5845 cp_declarator *declarator = NULL;
0a3b29ad 5846
5847 while (true)
5848 {
5849 tree expression;
5850
5851 /* Look for the opening `['. */
640710cf 5852 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
0a3b29ad 5853 /* The first expression is not required to be constant. */
5854 if (!declarator)
5855 {
ad9ae192 5856 cp_token *token = cp_lexer_peek_token (parser->lexer);
98b326fd 5857 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 5858 /* The standard requires that the expression have integral
5859 type. DR 74 adds enumeration types. We believe that the
5860 real intent is that these expressions be handled like the
5861 expression in a `switch' condition, which also allows
5862 classes with a single conversion to integral or
5863 enumeration type. */
5864 if (!processing_template_decl)
5865 {
ccb84981 5866 expression
0a3b29ad 5867 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5868 expression,
35771a9a 5869 /*complain=*/true);
0a3b29ad 5870 if (!expression)
5871 {
ad9ae192 5872 error ("%Hexpression in new-declarator must have integral "
5873 "or enumeration type", &token->location);
0a3b29ad 5874 expression = error_mark_node;
5875 }
5876 }
5877 }
5878 /* But all the other expressions must be. */
5879 else
ccb84981 5880 expression
5881 = cp_parser_constant_expression (parser,
5f6526e1 5882 /*allow_non_constant=*/false,
5883 NULL);
0a3b29ad 5884 /* Look for the closing `]'. */
640710cf 5885 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
0a3b29ad 5886
5887 /* Add this bound to the declarator. */
3046c0a3 5888 declarator = make_array_declarator (declarator, expression);
0a3b29ad 5889
5890 /* If the next token is not a `[', then there are no more
5891 bounds. */
5892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5893 break;
5894 }
5895
5896 return declarator;
5897}
5898
5899/* Parse a new-initializer.
5900
5901 new-initializer:
5902 ( expression-list [opt] )
f82f1250 5903 braced-init-list
0a3b29ad 5904
f352a3fb 5905 Returns a representation of the expression-list. */
0a3b29ad 5906
f352a3fb 5907static VEC(tree,gc) *
45baea8b 5908cp_parser_new_initializer (cp_parser* parser)
0a3b29ad 5909{
f352a3fb 5910 VEC(tree,gc) *expression_list;
0a3b29ad 5911
f82f1250 5912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5913 {
f352a3fb 5914 tree t;
f82f1250 5915 bool expr_non_constant_p;
5916 maybe_warn_cpp0x ("extended initializer lists");
f352a3fb 5917 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5918 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5919 expression_list = make_tree_vector_single (t);
f82f1250 5920 }
5921 else
5922 expression_list = (cp_parser_parenthesized_expression_list
5923 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5924 /*non_constant_p=*/NULL));
0a3b29ad 5925
5926 return expression_list;
5927}
5928
5929/* Parse a delete-expression.
5930
5931 delete-expression:
5932 :: [opt] delete cast-expression
5933 :: [opt] delete [ ] cast-expression
5934
5935 Returns a representation of the expression. */
5936
5937static tree
45baea8b 5938cp_parser_delete_expression (cp_parser* parser)
0a3b29ad 5939{
5940 bool global_scope_p;
5941 bool array_p;
5942 tree expression;
5943
5944 /* Look for the optional `::' operator. */
5945 global_scope_p
5946 = (cp_parser_global_scope_opt (parser,
130bb1d4 5947 /*current_scope_valid_p=*/false)
0a3b29ad 5948 != NULL_TREE);
5949 /* Look for the `delete' keyword. */
640710cf 5950 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
0a3b29ad 5951 /* See if the array syntax is in use. */
5952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5953 {
5954 /* Consume the `[' token. */
5955 cp_lexer_consume_token (parser->lexer);
5956 /* Look for the `]' token. */
640710cf 5957 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
0a3b29ad 5958 /* Remember that this is the `[]' construct. */
5959 array_p = true;
5960 }
5961 else
5962 array_p = false;
5963
5964 /* Parse the cast-expression. */
a63bc44c 5965 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 5966
3938e0c2 5967 /* A delete-expression may not appear in an integral constant
5968 expression. */
7222be86 5969 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
3938e0c2 5970 return error_mark_node;
5971
0a3b29ad 5972 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5973}
5974
589d356c 5975/* Returns true if TOKEN may start a cast-expression and false
5976 otherwise. */
5977
5978static bool
5979cp_parser_token_starts_cast_expression (cp_token *token)
5980{
5981 switch (token->type)
5982 {
5983 case CPP_COMMA:
5984 case CPP_SEMICOLON:
5985 case CPP_QUERY:
5986 case CPP_COLON:
5987 case CPP_CLOSE_SQUARE:
5988 case CPP_CLOSE_PAREN:
5989 case CPP_CLOSE_BRACE:
5990 case CPP_DOT:
5991 case CPP_DOT_STAR:
5992 case CPP_DEREF:
5993 case CPP_DEREF_STAR:
5994 case CPP_DIV:
5995 case CPP_MOD:
5996 case CPP_LSHIFT:
5997 case CPP_RSHIFT:
5998 case CPP_LESS:
5999 case CPP_GREATER:
6000 case CPP_LESS_EQ:
6001 case CPP_GREATER_EQ:
6002 case CPP_EQ_EQ:
6003 case CPP_NOT_EQ:
6004 case CPP_EQ:
6005 case CPP_MULT_EQ:
6006 case CPP_DIV_EQ:
6007 case CPP_MOD_EQ:
6008 case CPP_PLUS_EQ:
6009 case CPP_MINUS_EQ:
6010 case CPP_RSHIFT_EQ:
6011 case CPP_LSHIFT_EQ:
6012 case CPP_AND_EQ:
6013 case CPP_XOR_EQ:
6014 case CPP_OR_EQ:
6015 case CPP_XOR:
6016 case CPP_OR:
6017 case CPP_OR_OR:
c19d7ab2 6018 case CPP_EOF:
589d356c 6019 return false;
6020
6021 /* '[' may start a primary-expression in obj-c++. */
6022 case CPP_OPEN_SQUARE:
6023 return c_dialect_objc ();
6024
6025 default:
6026 return true;
6027 }
6028}
6029
0a3b29ad 6030/* Parse a cast-expression.
6031
6032 cast-expression:
6033 unary-expression
6034 ( type-id ) cast-expression
6035
640aa28c 6036 ADDRESS_P is true iff the unary-expression is appearing as the
6037 operand of the `&' operator. CAST_P is true if this expression is
6038 the target of a cast.
6039
0a3b29ad 6040 Returns a representation of the expression. */
6041
6042static tree
98b326fd 6043cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6044 cp_id_kind * pidk)
0a3b29ad 6045{
6046 /* If it's a `(', then we might be looking at a cast. */
6047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6048 {
6049 tree type = NULL_TREE;
6050 tree expr = NULL_TREE;
6051 bool compound_literal_p;
6052 const char *saved_message;
6053
6054 /* There's no way to know yet whether or not this is a cast.
6055 For example, `(int (3))' is a unary-expression, while `(int)
6056 3' is a cast. So, we resort to parsing tentatively. */
6057 cp_parser_parse_tentatively (parser);
6058 /* Types may not be defined in a cast. */
6059 saved_message = parser->type_definition_forbidden_message;
6060 parser->type_definition_forbidden_message
6061 = "types may not be defined in casts";
6062 /* Consume the `('. */
6063 cp_lexer_consume_token (parser->lexer);
6064 /* A very tricky bit is that `(struct S) { 3 }' is a
6065 compound-literal (which we permit in C++ as an extension).
6066 But, that construct is not a cast-expression -- it is a
6067 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6068 is legal; if the compound-literal were a cast-expression,
6069 you'd need an extra set of parentheses.) But, if we parse
6070 the type-id, and it happens to be a class-specifier, then we
6071 will commit to the parse at that point, because we cannot
6072 undo the action that is done when creating a new class. So,
ccb84981 6073 then we cannot back up and do a postfix-expression.
0a3b29ad 6074
6075 Therefore, we scan ahead to the closing `)', and check to see
6076 if the token after the `)' is a `{'. If so, we are not
ccb84981 6077 looking at a cast-expression.
0a3b29ad 6078
6079 Save tokens so that we can put them back. */
6080 cp_lexer_save_tokens (parser->lexer);
6081 /* Skip tokens until the next token is a closing parenthesis.
6082 If we find the closing `)', and the next token is a `{', then
6083 we are looking at a compound-literal. */
ccb84981 6084 compound_literal_p
3d0f901b 6085 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6086 /*consume_paren=*/true)
0a3b29ad 6087 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6088 /* Roll back the tokens we skipped. */
6089 cp_lexer_rollback_tokens (parser->lexer);
6090 /* If we were looking at a compound-literal, simulate an error
6091 so that the call to cp_parser_parse_definitely below will
6092 fail. */
6093 if (compound_literal_p)
6094 cp_parser_simulate_error (parser);
6095 else
6096 {
41f2d08e 6097 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6098 parser->in_type_id_in_expr_p = true;
0a3b29ad 6099 /* Look for the type-id. */
6100 type = cp_parser_type_id (parser);
6101 /* Look for the closing `)'. */
640710cf 6102 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
41f2d08e 6103 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 6104 }
6105
6106 /* Restore the saved message. */
6107 parser->type_definition_forbidden_message = saved_message;
6108
589d356c 6109 /* At this point this can only be either a cast or a
6110 parenthesized ctor such as `(T ())' that looks like a cast to
6111 function returning T. */
6112 if (!cp_parser_error_occurred (parser)
6113 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6114 (parser->lexer)))
0a3b29ad 6115 {
589d356c 6116 cp_parser_parse_definitely (parser);
6117 expr = cp_parser_cast_expression (parser,
6118 /*address_p=*/false,
98b326fd 6119 /*cast_p=*/true, pidk);
589d356c 6120
0a3b29ad 6121 /* Warn about old-style casts, if so requested. */
ccb84981 6122 if (warn_old_style_cast
6123 && !in_system_header
6124 && !VOID_TYPE_P (type)
0a3b29ad 6125 && current_lang_name != lang_name_c)
ced7c954 6126 warning (OPT_Wold_style_cast, "use of old-style cast");
5f6526e1 6127
6128 /* Only type conversions to integral or enumeration types
6129 can be used in constant-expressions. */
bde9ebf7 6130 if (!cast_valid_in_integral_constant_expression_p (type)
207355ad 6131 && (cp_parser_non_integral_constant_expression
3938e0c2 6132 (parser,
6133 "a cast to a type other than an integral or "
6134 "enumeration type")))
6135 return error_mark_node;
6136
0a3b29ad 6137 /* Perform the cast. */
e60a6f7b 6138 expr = build_c_cast (input_location, type, expr);
d622b3bd 6139 return expr;
0a3b29ad 6140 }
589d356c 6141 else
6142 cp_parser_abort_tentative_parse (parser);
0a3b29ad 6143 }
6144
6145 /* If we get here, then it's not a cast, so it must be a
6146 unary-expression. */
98b326fd 6147 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
0a3b29ad 6148}
6149
0a88af73 6150/* Parse a binary expression of the general form:
0a3b29ad 6151
6152 pm-expression:
6153 cast-expression
6154 pm-expression .* cast-expression
6155 pm-expression ->* cast-expression
6156
e24657db 6157 multiplicative-expression:
0a3b29ad 6158 pm-expression
6159 multiplicative-expression * pm-expression
6160 multiplicative-expression / pm-expression
6161 multiplicative-expression % pm-expression
6162
0a3b29ad 6163 additive-expression:
6164 multiplicative-expression
6165 additive-expression + multiplicative-expression
6166 additive-expression - multiplicative-expression
6167
0a3b29ad 6168 shift-expression:
6169 additive-expression
6170 shift-expression << additive-expression
6171 shift-expression >> additive-expression
6172
0a3b29ad 6173 relational-expression:
6174 shift-expression
6175 relational-expression < shift-expression
6176 relational-expression > shift-expression
6177 relational-expression <= shift-expression
6178 relational-expression >= shift-expression
6179
0a88af73 6180 GNU Extension:
9031d10b 6181
0a3b29ad 6182 relational-expression:
6183 relational-expression <? shift-expression
6184 relational-expression >? shift-expression
6185
0a3b29ad 6186 equality-expression:
6187 relational-expression
6188 equality-expression == relational-expression
6189 equality-expression != relational-expression
6190
0a3b29ad 6191 and-expression:
6192 equality-expression
6193 and-expression & equality-expression
6194
0a3b29ad 6195 exclusive-or-expression:
6196 and-expression
6197 exclusive-or-expression ^ and-expression
6198
0a88af73 6199 inclusive-or-expression:
6200 exclusive-or-expression
6201 inclusive-or-expression | exclusive-or-expression
0a3b29ad 6202
0a88af73 6203 logical-and-expression:
6204 inclusive-or-expression
6205 logical-and-expression && inclusive-or-expression
0a3b29ad 6206
0a88af73 6207 logical-or-expression:
6208 logical-and-expression
6209 logical-or-expression || logical-and-expression
0a3b29ad 6210
0a88af73 6211 All these are implemented with a single function like:
0a3b29ad 6212
0a88af73 6213 binary-expression:
6214 simple-cast-expression
6215 binary-expression <token> binary-expression
0a3b29ad 6216
640aa28c 6217 CAST_P is true if this expression is the target of a cast.
6218
0a88af73 6219 The binops_by_token map is used to get the tree codes for each <token> type.
6220 binary-expressions are associated according to a precedence table. */
0a3b29ad 6221
6dcdb5de 6222#define TOKEN_PRECEDENCE(token) \
6223(((token->type == CPP_GREATER \
6224 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6225 && !parser->greater_than_is_operator_p) \
6226 ? PREC_NOT_OPERATOR \
56471494 6227 : binops_by_token[token->type].prec)
0a3b29ad 6228
6229static tree
fd6481cf 6230cp_parser_binary_expression (cp_parser* parser, bool cast_p,
4390875c 6231 bool no_toplevel_fold_p,
98b326fd 6232 enum cp_parser_prec prec,
6233 cp_id_kind * pidk)
0a3b29ad 6234{
0a88af73 6235 cp_parser_expression_stack stack;
6236 cp_parser_expression_stack_entry *sp = &stack[0];
6237 tree lhs, rhs;
6238 cp_token *token;
e534436e 6239 enum tree_code tree_type, lhs_type, rhs_type;
fd6481cf 6240 enum cp_parser_prec new_prec, lookahead_prec;
0a88af73 6241 bool overloaded_p;
0a3b29ad 6242
0a88af73 6243 /* Parse the first expression. */
98b326fd 6244 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
e534436e 6245 lhs_type = ERROR_MARK;
0a3b29ad 6246
0a88af73 6247 for (;;)
6248 {
6249 /* Get an operator token. */
6250 token = cp_lexer_peek_token (parser->lexer);
d50879bc 6251
56471494 6252 if (warn_cxx0x_compat
6253 && token->type == CPP_RSHIFT
6254 && !parser->greater_than_is_operator_p)
6255 {
6256 warning (OPT_Wc__0x_compat,
6257 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6258 &token->location);
6259 warning (OPT_Wc__0x_compat,
6260 "suggest parentheses around %<>>%> expression");
6261 }
6262
0a88af73 6263 new_prec = TOKEN_PRECEDENCE (token);
6264
6265 /* Popping an entry off the stack means we completed a subexpression:
653e5405 6266 - either we found a token which is not an operator (`>' where it is not
6267 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6268 will happen repeatedly;
6269 - or, we found an operator which has lower priority. This is the case
6270 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6271 parsing `3 * 4'. */
0a88af73 6272 if (new_prec <= prec)
653e5405 6273 {
6274 if (sp == stack)
0a88af73 6275 break;
653e5405 6276 else
0a88af73 6277 goto pop;
653e5405 6278 }
0a3b29ad 6279
0a88af73 6280 get_rhs:
6281 tree_type = binops_by_token[token->type].tree_type;
0a3b29ad 6282
93523877 6283 /* We used the operator token. */
0a88af73 6284 cp_lexer_consume_token (parser->lexer);
0a3b29ad 6285
0a88af73 6286 /* Extract another operand. It may be the RHS of this expression
653e5405 6287 or the LHS of a new, higher priority expression. */
0a88af73 6288 rhs = cp_parser_simple_cast_expression (parser);
e534436e 6289 rhs_type = ERROR_MARK;
0a3b29ad 6290
0a88af73 6291 /* Get another operator token. Look up its precedence to avoid
653e5405 6292 building a useless (immediately popped) stack entry for common
6293 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
0a88af73 6294 token = cp_lexer_peek_token (parser->lexer);
6295 lookahead_prec = TOKEN_PRECEDENCE (token);
6296 if (lookahead_prec > new_prec)
653e5405 6297 {
6298 /* ... and prepare to parse the RHS of the new, higher priority
6299 expression. Since precedence levels on the stack are
9802eea0 6300 monotonically increasing, we do not have to care about
6301 stack overflows. */
653e5405 6302 sp->prec = prec;
6303 sp->tree_type = tree_type;
6304 sp->lhs = lhs;
e534436e 6305 sp->lhs_type = lhs_type;
653e5405 6306 sp++;
6307 lhs = rhs;
e534436e 6308 lhs_type = rhs_type;
653e5405 6309 prec = new_prec;
6310 new_prec = lookahead_prec;
6311 goto get_rhs;
6312
6313 pop:
4390875c 6314 lookahead_prec = new_prec;
653e5405 6315 /* If the stack is not empty, we have parsed into LHS the right side
0a88af73 6316 (`4' in the example above) of an expression we had suspended.
9031d10b 6317 We can use the information on the stack to recover the LHS (`3')
0a88af73 6318 from the stack together with the tree code (`MULT_EXPR'), and
6319 the precedence of the higher level subexpression
6320 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6321 which will be used to actually build the additive expression. */
653e5405 6322 --sp;
0a88af73 6323 prec = sp->prec;
653e5405 6324 tree_type = sp->tree_type;
6325 rhs = lhs;
e534436e 6326 rhs_type = lhs_type;
653e5405 6327 lhs = sp->lhs;
e534436e 6328 lhs_type = sp->lhs_type;
653e5405 6329 }
0a3b29ad 6330
0a88af73 6331 overloaded_p = false;
82012ffe 6332 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6333 ERROR_MARK for everything that is not a binary expression.
6334 This makes warn_about_parentheses miss some warnings that
6335 involve unary operators. For unary expressions we should
6336 pass the correct tree_code unless the unary expression was
6337 surrounded by parentheses.
6338 */
4390875c 6339 if (no_toplevel_fold_p
6340 && lookahead_prec <= prec
6341 && sp == stack
6342 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6343 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6344 else
6345 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6346 &overloaded_p, tf_warning_or_error);
e534436e 6347 lhs_type = tree_type;
0a3b29ad 6348
0a88af73 6349 /* If the binary operator required the use of an overloaded operator,
653e5405 6350 then this expression cannot be an integral constant-expression.
6351 An overloaded operator can be used even if both operands are
6352 otherwise permissible in an integral constant-expression if at
6353 least one of the operands is of enumeration type. */
0a3b29ad 6354
0a88af73 6355 if (overloaded_p
653e5405 6356 && (cp_parser_non_integral_constant_expression
6357 (parser, "calls to overloaded operators")))
6358 return error_mark_node;
0a88af73 6359 }
0a3b29ad 6360
0a88af73 6361 return lhs;
0a3b29ad 6362}
6363
0a88af73 6364
0a3b29ad 6365/* Parse the `? expression : assignment-expression' part of a
6366 conditional-expression. The LOGICAL_OR_EXPR is the
6367 logical-or-expression that started the conditional-expression.
6368 Returns a representation of the entire conditional-expression.
6369
878870b4 6370 This routine is used by cp_parser_assignment_expression.
0a3b29ad 6371
6372 ? expression : assignment-expression
ccb84981 6373
0a3b29ad 6374 GNU Extensions:
ccb84981 6375
0a3b29ad 6376 ? : assignment-expression */
6377
6378static tree
45baea8b 6379cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
0a3b29ad 6380{
6381 tree expr;
6382 tree assignment_expr;
6383
6384 /* Consume the `?' token. */
6385 cp_lexer_consume_token (parser->lexer);
6386 if (cp_parser_allow_gnu_extensions_p (parser)
6387 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6388 /* Implicit true clause. */
6389 expr = NULL_TREE;
6390 else
6391 /* Parse the expression. */
98b326fd 6392 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
ccb84981 6393
0a3b29ad 6394 /* The next token should be a `:'. */
640710cf 6395 cp_parser_require (parser, CPP_COLON, "%<:%>");
0a3b29ad 6396 /* Parse the assignment-expression. */
98b326fd 6397 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 6398
6399 /* Build the conditional-expression. */
6400 return build_x_conditional_expr (logical_or_expr,
6401 expr,
ebd21de4 6402 assignment_expr,
6403 tf_warning_or_error);
0a3b29ad 6404}
6405
6406/* Parse an assignment-expression.
6407
6408 assignment-expression:
6409 conditional-expression
6410 logical-or-expression assignment-operator assignment_expression
6411 throw-expression
6412
640aa28c 6413 CAST_P is true if this expression is the target of a cast.
6414
0a3b29ad 6415 Returns a representation for the expression. */
6416
6417static tree
98b326fd 6418cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6419 cp_id_kind * pidk)
0a3b29ad 6420{
6421 tree expr;
6422
6423 /* If the next token is the `throw' keyword, then we're looking at
6424 a throw-expression. */
6425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6426 expr = cp_parser_throw_expression (parser);
6427 /* Otherwise, it must be that we are looking at a
6428 logical-or-expression. */
6429 else
6430 {
0a88af73 6431 /* Parse the binary expressions (logical-or-expression). */
4390875c 6432 expr = cp_parser_binary_expression (parser, cast_p, false,
6433 PREC_NOT_OPERATOR, pidk);
0a3b29ad 6434 /* If the next token is a `?' then we're actually looking at a
6435 conditional-expression. */
6436 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6437 return cp_parser_question_colon_clause (parser, expr);
ccb84981 6438 else
0a3b29ad 6439 {
6440 enum tree_code assignment_operator;
6441
6442 /* If it's an assignment-operator, we're using the second
6443 production. */
ccb84981 6444 assignment_operator
0a3b29ad 6445 = cp_parser_assignment_operator_opt (parser);
6446 if (assignment_operator != ERROR_MARK)
6447 {
f82f1250 6448 bool non_constant_p;
0a3b29ad 6449
6450 /* Parse the right-hand side of the assignment. */
f82f1250 6451 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6452
6453 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6454 maybe_warn_cpp0x ("extended initializer lists");
6455
5f6526e1 6456 /* An assignment may not appear in a
6457 constant-expression. */
3938e0c2 6458 if (cp_parser_non_integral_constant_expression (parser,
6459 "an assignment"))
6460 return error_mark_node;
755edffd 6461 /* Build the assignment expression. */
ccb84981 6462 expr = build_x_modify_expr (expr,
6463 assignment_operator,
ebd21de4 6464 rhs,
6465 tf_warning_or_error);
0a3b29ad 6466 }
6467 }
6468 }
6469
6470 return expr;
6471}
6472
6473/* Parse an (optional) assignment-operator.
6474
ccb84981 6475 assignment-operator: one of
6476 = *= /= %= += -= >>= <<= &= ^= |=
0a3b29ad 6477
6478 GNU Extension:
ccb84981 6479
0a3b29ad 6480 assignment-operator: one of
6481 <?= >?=
6482
6483 If the next token is an assignment operator, the corresponding tree
6484 code is returned, and the token is consumed. For example, for
6485 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6486 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6487 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6488 operator, ERROR_MARK is returned. */
6489
6490static enum tree_code
45baea8b 6491cp_parser_assignment_operator_opt (cp_parser* parser)
0a3b29ad 6492{
6493 enum tree_code op;
6494 cp_token *token;
6495
08cc44e7 6496 /* Peek at the next token. */
0a3b29ad 6497 token = cp_lexer_peek_token (parser->lexer);
6498
6499 switch (token->type)
6500 {
6501 case CPP_EQ:
6502 op = NOP_EXPR;
6503 break;
6504
6505 case CPP_MULT_EQ:
6506 op = MULT_EXPR;
6507 break;
6508
6509 case CPP_DIV_EQ:
6510 op = TRUNC_DIV_EXPR;
6511 break;
6512
6513 case CPP_MOD_EQ:
6514 op = TRUNC_MOD_EXPR;
6515 break;
6516
6517 case CPP_PLUS_EQ:
6518 op = PLUS_EXPR;
6519 break;
6520
6521 case CPP_MINUS_EQ:
6522 op = MINUS_EXPR;
6523 break;
6524
6525 case CPP_RSHIFT_EQ:
6526 op = RSHIFT_EXPR;
6527 break;
6528
6529 case CPP_LSHIFT_EQ:
6530 op = LSHIFT_EXPR;
6531 break;
6532
6533 case CPP_AND_EQ:
6534 op = BIT_AND_EXPR;
6535 break;
6536
6537 case CPP_XOR_EQ:
6538 op = BIT_XOR_EXPR;
6539 break;
6540
6541 case CPP_OR_EQ:
6542 op = BIT_IOR_EXPR;
6543 break;
6544
ccb84981 6545 default:
0a3b29ad 6546 /* Nothing else is an assignment operator. */
6547 op = ERROR_MARK;
6548 }
6549
6550 /* If it was an assignment operator, consume it. */
6551 if (op != ERROR_MARK)
6552 cp_lexer_consume_token (parser->lexer);
6553
6554 return op;
6555}
6556
6557/* Parse an expression.
6558
6559 expression:
6560 assignment-expression
6561 expression , assignment-expression
6562
640aa28c 6563 CAST_P is true if this expression is the target of a cast.
6564
0a3b29ad 6565 Returns a representation of the expression. */
6566
6567static tree
98b326fd 6568cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
0a3b29ad 6569{
6570 tree expression = NULL_TREE;
0a3b29ad 6571
6572 while (true)
6573 {
6574 tree assignment_expression;
6575
6576 /* Parse the next assignment-expression. */
ccb84981 6577 assignment_expression
98b326fd 6578 = cp_parser_assignment_expression (parser, cast_p, pidk);
0a3b29ad 6579 /* If this is the first assignment-expression, we can just
6580 save it away. */
6581 if (!expression)
6582 expression = assignment_expression;
0a3b29ad 6583 else
13795292 6584 expression = build_x_compound_expr (expression,
ebd21de4 6585 assignment_expression,
6586 tf_warning_or_error);
0a3b29ad 6587 /* If the next token is not a comma, then we are done with the
6588 expression. */
6589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6590 break;
6591 /* Consume the `,'. */
6592 cp_lexer_consume_token (parser->lexer);
5f6526e1 6593 /* A comma operator cannot appear in a constant-expression. */
3938e0c2 6594 if (cp_parser_non_integral_constant_expression (parser,
6595 "a comma operator"))
6596 expression = error_mark_node;
5f6526e1 6597 }
0a3b29ad 6598
6599 return expression;
6600}
6601
ccb84981 6602/* Parse a constant-expression.
0a3b29ad 6603
6604 constant-expression:
ccb84981 6605 conditional-expression
5f6526e1 6606
6607 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
13795292 6608 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6609 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6610 is false, NON_CONSTANT_P should be NULL. */
0a3b29ad 6611
6612static tree
ccb84981 6613cp_parser_constant_expression (cp_parser* parser,
5f6526e1 6614 bool allow_non_constant_p,
6615 bool *non_constant_p)
0a3b29ad 6616{
f47c1747 6617 bool saved_integral_constant_expression_p;
6618 bool saved_allow_non_integral_constant_expression_p;
6619 bool saved_non_integral_constant_expression_p;
0a3b29ad 6620 tree expression;
6621
6622 /* It might seem that we could simply parse the
6623 conditional-expression, and then check to see if it were
6624 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6625 one that the compiler can figure out is constant, possibly after
6626 doing some simplifications or optimizations. The standard has a
6627 precise definition of constant-expression, and we must honor
6628 that, even though it is somewhat more restrictive.
6629
6630 For example:
6631
6632 int i[(2, 3)];
6633
6634 is not a legal declaration, because `(2, 3)' is not a
6635 constant-expression. The `,' operator is forbidden in a
6636 constant-expression. However, GCC's constant-folding machinery
6637 will fold this operation to an INTEGER_CST for `3'. */
6638
5f6526e1 6639 /* Save the old settings. */
f47c1747 6640 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
ccb84981 6641 saved_allow_non_integral_constant_expression_p
f47c1747 6642 = parser->allow_non_integral_constant_expression_p;
6643 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
0a3b29ad 6644 /* We are now parsing a constant-expression. */
f47c1747 6645 parser->integral_constant_expression_p = true;
6646 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6647 parser->non_integral_constant_expression_p = false;
878870b4 6648 /* Although the grammar says "conditional-expression", we parse an
6649 "assignment-expression", which also permits "throw-expression"
6650 and the use of assignment operators. In the case that
6651 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6652 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6653 actually essential that we look for an assignment-expression.
6654 For example, cp_parser_initializer_clauses uses this function to
6655 determine whether a particular assignment-expression is in fact
6656 constant. */
98b326fd 6657 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
5f6526e1 6658 /* Restore the old settings. */
9031d10b 6659 parser->integral_constant_expression_p
640aa28c 6660 = saved_integral_constant_expression_p;
ccb84981 6661 parser->allow_non_integral_constant_expression_p
f47c1747 6662 = saved_allow_non_integral_constant_expression_p;
5f6526e1 6663 if (allow_non_constant_p)
f47c1747 6664 *non_constant_p = parser->non_integral_constant_expression_p;
640aa28c 6665 else if (parser->non_integral_constant_expression_p)
6666 expression = error_mark_node;
9031d10b 6667 parser->non_integral_constant_expression_p
640aa28c 6668 = saved_non_integral_constant_expression_p;
0a3b29ad 6669
6670 return expression;
6671}
6672
43bf5d72 6673/* Parse __builtin_offsetof.
6674
6675 offsetof-expression:
6676 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6677
6678 offsetof-member-designator:
6679 id-expression
6680 | offsetof-member-designator "." id-expression
f0d0d842 6681 | offsetof-member-designator "[" expression "]"
6682 | offsetof-member-designator "->" id-expression */
43bf5d72 6683
6684static tree
6685cp_parser_builtin_offsetof (cp_parser *parser)
6686{
6687 int save_ice_p, save_non_ice_p;
6688 tree type, expr;
6689 cp_id_kind dummy;
ad9ae192 6690 cp_token *token;
43bf5d72 6691
6692 /* We're about to accept non-integral-constant things, but will
6693 definitely yield an integral constant expression. Save and
6694 restore these values around our local parsing. */
6695 save_ice_p = parser->integral_constant_expression_p;
6696 save_non_ice_p = parser->non_integral_constant_expression_p;
6697
6698 /* Consume the "__builtin_offsetof" token. */
6699 cp_lexer_consume_token (parser->lexer);
6700 /* Consume the opening `('. */
640710cf 6701 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
43bf5d72 6702 /* Parse the type-id. */
6703 type = cp_parser_type_id (parser);
6704 /* Look for the `,'. */
640710cf 6705 cp_parser_require (parser, CPP_COMMA, "%<,%>");
ad9ae192 6706 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 6707
6708 /* Build the (type *)null that begins the traditional offsetof macro. */
ebd21de4 6709 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6710 tf_warning_or_error);
43bf5d72 6711
6712 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6713 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
ad9ae192 6714 true, &dummy, token->location);
43bf5d72 6715 while (true)
6716 {
ad9ae192 6717 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 6718 switch (token->type)
6719 {
6720 case CPP_OPEN_SQUARE:
6721 /* offsetof-member-designator "[" expression "]" */
6722 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6723 break;
6724
f0d0d842 6725 case CPP_DEREF:
6726 /* offsetof-member-designator "->" identifier */
6727 expr = grok_array_decl (expr, integer_zero_node);
6728 /* FALLTHRU */
6729
43bf5d72 6730 case CPP_DOT:
6731 /* offsetof-member-designator "." identifier */
6732 cp_lexer_consume_token (parser->lexer);
f0d0d842 6733 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6734 expr, true, &dummy,
ad9ae192 6735 token->location);
43bf5d72 6736 break;
6737
6738 case CPP_CLOSE_PAREN:
6739 /* Consume the ")" token. */
6740 cp_lexer_consume_token (parser->lexer);
6741 goto success;
6742
6743 default:
6744 /* Error. We know the following require will fail, but
6745 that gives the proper error message. */
640710cf 6746 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
43bf5d72 6747 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6748 expr = error_mark_node;
6749 goto failure;
6750 }
6751 }
6752
6753 success:
11106b1c 6754 /* If we're processing a template, we can't finish the semantics yet.
6755 Otherwise we can fold the entire expression now. */
6756 if (processing_template_decl)
6757 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6758 else
bf75f33a 6759 expr = finish_offsetof (expr);
43bf5d72 6760
6761 failure:
6762 parser->integral_constant_expression_p = save_ice_p;
6763 parser->non_integral_constant_expression_p = save_non_ice_p;
6764
6765 return expr;
6766}
6767
481451eb 6768/* Parse a trait expression. */
6769
6770static tree
6771cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6772{
6773 cp_trait_kind kind;
6774 tree type1, type2 = NULL_TREE;
6775 bool binary = false;
6776 cp_decl_specifier_seq decl_specs;
6777
6778 switch (keyword)
6779 {
6780 case RID_HAS_NOTHROW_ASSIGN:
6781 kind = CPTK_HAS_NOTHROW_ASSIGN;
6782 break;
6783 case RID_HAS_NOTHROW_CONSTRUCTOR:
6784 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6785 break;
6786 case RID_HAS_NOTHROW_COPY:
6787 kind = CPTK_HAS_NOTHROW_COPY;
6788 break;
6789 case RID_HAS_TRIVIAL_ASSIGN:
6790 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6791 break;
6792 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6793 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6794 break;
6795 case RID_HAS_TRIVIAL_COPY:
6796 kind = CPTK_HAS_TRIVIAL_COPY;
6797 break;
6798 case RID_HAS_TRIVIAL_DESTRUCTOR:
6799 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6800 break;
6801 case RID_HAS_VIRTUAL_DESTRUCTOR:
6802 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6803 break;
6804 case RID_IS_ABSTRACT:
6805 kind = CPTK_IS_ABSTRACT;
6806 break;
6807 case RID_IS_BASE_OF:
6808 kind = CPTK_IS_BASE_OF;
6809 binary = true;
6810 break;
6811 case RID_IS_CLASS:
6812 kind = CPTK_IS_CLASS;
6813 break;
6814 case RID_IS_CONVERTIBLE_TO:
6815 kind = CPTK_IS_CONVERTIBLE_TO;
6816 binary = true;
6817 break;
6818 case RID_IS_EMPTY:
6819 kind = CPTK_IS_EMPTY;
6820 break;
6821 case RID_IS_ENUM:
6822 kind = CPTK_IS_ENUM;
6823 break;
6824 case RID_IS_POD:
6825 kind = CPTK_IS_POD;
6826 break;
6827 case RID_IS_POLYMORPHIC:
6828 kind = CPTK_IS_POLYMORPHIC;
6829 break;
6830 case RID_IS_UNION:
6831 kind = CPTK_IS_UNION;
6832 break;
6833 default:
6834 gcc_unreachable ();
6835 }
6836
6837 /* Consume the token. */
6838 cp_lexer_consume_token (parser->lexer);
6839
640710cf 6840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
481451eb 6841
6842 type1 = cp_parser_type_id (parser);
6843
bcef2b12 6844 if (type1 == error_mark_node)
6845 return error_mark_node;
6846
481451eb 6847 /* Build a trivial decl-specifier-seq. */
6848 clear_decl_specs (&decl_specs);
6849 decl_specs.type = type1;
6850
6851 /* Call grokdeclarator to figure out what type this is. */
6852 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6853 /*initialized=*/0, /*attrlist=*/NULL);
6854
6855 if (binary)
6856 {
640710cf 6857 cp_parser_require (parser, CPP_COMMA, "%<,%>");
481451eb 6858
6859 type2 = cp_parser_type_id (parser);
6860
bcef2b12 6861 if (type2 == error_mark_node)
6862 return error_mark_node;
6863
481451eb 6864 /* Build a trivial decl-specifier-seq. */
6865 clear_decl_specs (&decl_specs);
6866 decl_specs.type = type2;
6867
6868 /* Call grokdeclarator to figure out what type this is. */
6869 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6870 /*initialized=*/0, /*attrlist=*/NULL);
6871 }
6872
640710cf 6873 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
481451eb 6874
bcef2b12 6875 /* Complete the trait expression, which may mean either processing
6876 the trait expr now or saving it for template instantiation. */
481451eb 6877 return finish_trait_expr (kind, type1, type2);
6878}
6879
0a3b29ad 6880/* Statements [gram.stmt.stmt] */
6881
ccb84981 6882/* Parse a statement.
0a3b29ad 6883
6884 statement:
6885 labeled-statement
6886 expression-statement
6887 compound-statement
6888 selection-statement
6889 iteration-statement
6890 jump-statement
6891 declaration-statement
b75b98aa 6892 try-block
6893
074ab442 6894 IN_COMPOUND is true when the statement is nested inside a
e534436e 6895 cp_parser_compound_statement; this matters for certain pragmas.
6896
6897 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6898 is a (possibly labeled) if statement which is not enclosed in braces
6899 and has an else clause. This is used to implement -Wparentheses. */
0a3b29ad 6900
6901static void
b75b98aa 6902cp_parser_statement (cp_parser* parser, tree in_statement_expr,
e534436e 6903 bool in_compound, bool *if_p)
0a3b29ad 6904{
6905 tree statement;
6906 cp_token *token;
357f7efa 6907 location_t statement_location;
0a3b29ad 6908
b75b98aa 6909 restart:
e534436e 6910 if (if_p != NULL)
6911 *if_p = false;
0a3b29ad 6912 /* There is no statement yet. */
6913 statement = NULL_TREE;
6914 /* Peek at the next token. */
6915 token = cp_lexer_peek_token (parser->lexer);
4ee9c684 6916 /* Remember the location of the first token in the statement. */
357f7efa 6917 statement_location = token->location;
0a3b29ad 6918 /* If this is a keyword, then that will often determine what kind of
6919 statement we have. */
6920 if (token->type == CPP_KEYWORD)
6921 {
6922 enum rid keyword = token->keyword;
6923
6924 switch (keyword)
6925 {
6926 case RID_CASE:
6927 case RID_DEFAULT:
17d53949 6928 /* Looks like a labeled-statement with a case label.
6929 Parse the label, and then use tail recursion to parse
6930 the statement. */
6931 cp_parser_label_for_labeled_statement (parser);
6932 goto restart;
0a3b29ad 6933
6934 case RID_IF:
6935 case RID_SWITCH:
e534436e 6936 statement = cp_parser_selection_statement (parser, if_p);
0a3b29ad 6937 break;
6938
6939 case RID_WHILE:
6940 case RID_DO:
6941 case RID_FOR:
6942 statement = cp_parser_iteration_statement (parser);
6943 break;
6944
6945 case RID_BREAK:
6946 case RID_CONTINUE:
6947 case RID_RETURN:
6948 case RID_GOTO:
6949 statement = cp_parser_jump_statement (parser);
6950 break;
6951
7a4e126b 6952 /* Objective-C++ exception-handling constructs. */
6953 case RID_AT_TRY:
6954 case RID_AT_CATCH:
6955 case RID_AT_FINALLY:
6956 case RID_AT_SYNCHRONIZED:
6957 case RID_AT_THROW:
6958 statement = cp_parser_objc_statement (parser);
6959 break;
6960
0a3b29ad 6961 case RID_TRY:
6962 statement = cp_parser_try_block (parser);
6963 break;
6964
26d3c536 6965 case RID_NAMESPACE:
6966 /* This must be a namespace alias definition. */
6967 cp_parser_declaration_statement (parser);
6968 return;
6969
0a3b29ad 6970 default:
6971 /* It might be a keyword like `int' that can start a
6972 declaration-statement. */
6973 break;
6974 }
6975 }
6976 else if (token->type == CPP_NAME)
6977 {
6978 /* If the next token is a `:', then we are looking at a
6979 labeled-statement. */
6980 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6981 if (token->type == CPP_COLON)
17d53949 6982 {
6983 /* Looks like a labeled-statement with an ordinary label.
6984 Parse the label, and then use tail recursion to parse
6985 the statement. */
6986 cp_parser_label_for_labeled_statement (parser);
6987 goto restart;
6988 }
0a3b29ad 6989 }
6990 /* Anything that starts with a `{' must be a compound-statement. */
6991 else if (token->type == CPP_OPEN_BRACE)
2363ef00 6992 statement = cp_parser_compound_statement (parser, NULL, false);
3986d990 6993 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6994 a statement all its own. */
6995 else if (token->type == CPP_PRAGMA)
6996 {
b75b98aa 6997 /* Only certain OpenMP pragmas are attached to statements, and thus
6998 are considered statements themselves. All others are not. In
6999 the context of a compound, accept the pragma as a "statement" and
074ab442 7000 return so that we can check for a close brace. Otherwise we
b75b98aa 7001 require a real statement and must go back and read one. */
7002 if (in_compound)
7003 cp_parser_pragma (parser, pragma_compound);
7004 else if (!cp_parser_pragma (parser, pragma_stmt))
7005 goto restart;
3986d990 7006 return;
7007 }
5f959b7d 7008 else if (token->type == CPP_EOF)
7009 {
7010 cp_parser_error (parser, "expected statement");
7011 return;
7012 }
0a3b29ad 7013
7014 /* Everything else must be a declaration-statement or an
ccb84981 7015 expression-statement. Try for the declaration-statement
0a3b29ad 7016 first, unless we are looking at a `;', in which case we know that
7017 we have an expression-statement. */
7018 if (!statement)
7019 {
7020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7021 {
7022 cp_parser_parse_tentatively (parser);
7023 /* Try to parse the declaration-statement. */
7024 cp_parser_declaration_statement (parser);
7025 /* If that worked, we're done. */
7026 if (cp_parser_parse_definitely (parser))
7027 return;
7028 }
7029 /* Look for an expression-statement instead. */
2363ef00 7030 statement = cp_parser_expression_statement (parser, in_statement_expr);
0a3b29ad 7031 }
7032
7033 /* Set the line number for the statement. */
bac62436 7034 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
357f7efa 7035 SET_EXPR_LOCATION (statement, statement_location);
0a3b29ad 7036}
7037
17d53949 7038/* Parse the label for a labeled-statement, i.e.
0a3b29ad 7039
17d53949 7040 identifier :
7041 case constant-expression :
7042 default :
260cf17d 7043
7044 GNU Extension:
17d53949 7045 case constant-expression ... constant-expression : statement
ccb84981 7046
17d53949 7047 When a label is parsed without errors, the label is added to the
7048 parse tree by the finish_* functions, so this function doesn't
7049 have to return the label. */
b75b98aa 7050
17d53949 7051static void
7052cp_parser_label_for_labeled_statement (cp_parser* parser)
0a3b29ad 7053{
7054 cp_token *token;
e1c8f1c5 7055 tree label = NULL_TREE;
0a3b29ad 7056
7057 /* The next token should be an identifier. */
7058 token = cp_lexer_peek_token (parser->lexer);
7059 if (token->type != CPP_NAME
7060 && token->type != CPP_KEYWORD)
7061 {
7062 cp_parser_error (parser, "expected labeled-statement");
17d53949 7063 return;
0a3b29ad 7064 }
7065
7066 switch (token->keyword)
7067 {
7068 case RID_CASE:
7069 {
260cf17d 7070 tree expr, expr_hi;
7071 cp_token *ellipsis;
0a3b29ad 7072
7073 /* Consume the `case' token. */
7074 cp_lexer_consume_token (parser->lexer);
7075 /* Parse the constant-expression. */
ccb84981 7076 expr = cp_parser_constant_expression (parser,
13795292 7077 /*allow_non_constant_p=*/false,
5f6526e1 7078 NULL);
260cf17d 7079
7080 ellipsis = cp_lexer_peek_token (parser->lexer);
7081 if (ellipsis->type == CPP_ELLIPSIS)
7082 {
653e5405 7083 /* Consume the `...' token. */
260cf17d 7084 cp_lexer_consume_token (parser->lexer);
7085 expr_hi =
7086 cp_parser_constant_expression (parser,
653e5405 7087 /*allow_non_constant_p=*/false,
260cf17d 7088 NULL);
7089 /* We don't need to emit warnings here, as the common code
7090 will do this for us. */
7091 }
7092 else
7093 expr_hi = NULL_TREE;
7094
b75b98aa 7095 if (parser->in_switch_statement_p)
e60a6f7b 7096 finish_case_label (token->location, expr, expr_hi);
b75b98aa 7097 else
ad9ae192 7098 error ("%Hcase label %qE not within a switch statement",
7099 &token->location, expr);
0a3b29ad 7100 }
7101 break;
7102
7103 case RID_DEFAULT:
7104 /* Consume the `default' token. */
7105 cp_lexer_consume_token (parser->lexer);
b75b98aa 7106
7107 if (parser->in_switch_statement_p)
e60a6f7b 7108 finish_case_label (token->location, NULL_TREE, NULL_TREE);
b75b98aa 7109 else
ad9ae192 7110 error ("%Hcase label not within a switch statement", &token->location);
0a3b29ad 7111 break;
7112
7113 default:
7114 /* Anything else must be an ordinary label. */
e1c8f1c5 7115 label = finish_label_stmt (cp_parser_identifier (parser));
0a3b29ad 7116 break;
7117 }
7118
7119 /* Require the `:' token. */
640710cf 7120 cp_parser_require (parser, CPP_COLON, "%<:%>");
e1c8f1c5 7121
7122 /* An ordinary label may optionally be followed by attributes.
7123 However, this is only permitted if the attributes are then
7124 followed by a semicolon. This is because, for backward
7125 compatibility, when parsing
7126 lab: __attribute__ ((unused)) int i;
7127 we want the attribute to attach to "i", not "lab". */
7128 if (label != NULL_TREE
7129 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7130 {
7131 tree attrs;
7132
7133 cp_parser_parse_tentatively (parser);
7134 attrs = cp_parser_attributes_opt (parser);
7135 if (attrs == NULL_TREE
7136 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7137 cp_parser_abort_tentative_parse (parser);
7138 else if (!cp_parser_parse_definitely (parser))
7139 ;
7140 else
7141 cplus_decl_attributes (&label, attrs, 0);
7142 }
0a3b29ad 7143}
7144
7145/* Parse an expression-statement.
7146
7147 expression-statement:
7148 expression [opt] ;
7149
7150 Returns the new EXPR_STMT -- or NULL_TREE if the expression
942ab15b 7151 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7152 indicates whether this expression-statement is part of an
7153 expression statement. */
0a3b29ad 7154
7155static tree
2363ef00 7156cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
0a3b29ad 7157{
942ab15b 7158 tree statement = NULL_TREE;
0a3b29ad 7159
942ab15b 7160 /* If the next token is a ';', then there is no expression
bd8962d5 7161 statement. */
0a3b29ad 7162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 7163 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
ccb84981 7164
0a3b29ad 7165 /* Consume the final `;'. */
cf91b86a 7166 cp_parser_consume_semicolon_at_end_of_statement (parser);
0a3b29ad 7167
2363ef00 7168 if (in_statement_expr
942ab15b 7169 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
640aa28c 7170 /* This is the final expression statement of a statement
7171 expression. */
7172 statement = finish_stmt_expr_expr (statement, in_statement_expr);
942ab15b 7173 else if (statement)
7174 statement = finish_expr_stmt (statement);
7175 else
7176 finish_stmt ();
ccb84981 7177
0a3b29ad 7178 return statement;
7179}
7180
7181/* Parse a compound-statement.
7182
7183 compound-statement:
7184 { statement-seq [opt] }
ccb84981 7185
3dac447c 7186 GNU extension:
7187
7188 compound-statement:
7189 { label-declaration-seq [opt] statement-seq [opt] }
7190
7191 label-declaration-seq:
7192 label-declaration
7193 label-declaration-seq label-declaration
7194
632f8185 7195 Returns a tree representing the statement. */
0a3b29ad 7196
7197static tree
2363ef00 7198cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7199 bool in_try)
0a3b29ad 7200{
7201 tree compound_stmt;
7202
7203 /* Consume the `{'. */
640710cf 7204 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
0a3b29ad 7205 return error_mark_node;
7206 /* Begin the compound-statement. */
2363ef00 7207 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
3dac447c 7208 /* If the next keyword is `__label__' we have a label declaration. */
7209 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7210 cp_parser_label_declaration (parser);
0a3b29ad 7211 /* Parse an (optional) statement-seq. */
2363ef00 7212 cp_parser_statement_seq_opt (parser, in_statement_expr);
0a3b29ad 7213 /* Finish the compound-statement. */
68f8f8cc 7214 finish_compound_stmt (compound_stmt);
0a3b29ad 7215 /* Consume the `}'. */
640710cf 7216 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 7217
7218 return compound_stmt;
7219}
7220
7221/* Parse an (optional) statement-seq.
7222
7223 statement-seq:
7224 statement
7225 statement-seq [opt] statement */
7226
7227static void
2363ef00 7228cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
0a3b29ad 7229{
7230 /* Scan statements until there aren't any more. */
7231 while (true)
7232 {
b75b98aa 7233 cp_token *token = cp_lexer_peek_token (parser->lexer);
7234
0a3b29ad 7235 /* If we're looking at a `}', then we've run out of statements. */
b75b98aa 7236 if (token->type == CPP_CLOSE_BRACE
7237 || token->type == CPP_EOF
7238 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 7239 break;
2672c56c 7240
7241 /* If we are in a compound statement and find 'else' then
7242 something went wrong. */
7243 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7244 {
7245 if (parser->in_statement & IN_IF_STMT)
7246 break;
7247 else
7248 {
7249 token = cp_lexer_consume_token (parser->lexer);
ad9ae192 7250 error ("%H%<else%> without a previous %<if%>", &token->location);
2672c56c 7251 }
7252 }
0a3b29ad 7253
7254 /* Parse the statement. */
e534436e 7255 cp_parser_statement (parser, in_statement_expr, true, NULL);
0a3b29ad 7256 }
7257}
7258
7259/* Parse a selection-statement.
7260
7261 selection-statement:
7262 if ( condition ) statement
7263 if ( condition ) statement else statement
ccb84981 7264 switch ( condition ) statement
0a3b29ad 7265
e534436e 7266 Returns the new IF_STMT or SWITCH_STMT.
7267
7268 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7269 is a (possibly labeled) if statement which is not enclosed in
7270 braces and has an else clause. This is used to implement
7271 -Wparentheses. */
0a3b29ad 7272
7273static tree
e534436e 7274cp_parser_selection_statement (cp_parser* parser, bool *if_p)
0a3b29ad 7275{
7276 cp_token *token;
7277 enum rid keyword;
7278
e534436e 7279 if (if_p != NULL)
7280 *if_p = false;
7281
0a3b29ad 7282 /* Peek at the next token. */
7283 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7284
7285 /* See what kind of keyword it is. */
7286 keyword = token->keyword;
7287 switch (keyword)
7288 {
7289 case RID_IF:
7290 case RID_SWITCH:
7291 {
7292 tree statement;
7293 tree condition;
7294
7295 /* Look for the `('. */
640710cf 7296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
0a3b29ad 7297 {
7298 cp_parser_skip_to_end_of_statement (parser);
7299 return error_mark_node;
7300 }
7301
7302 /* Begin the selection-statement. */
7303 if (keyword == RID_IF)
7304 statement = begin_if_stmt ();
7305 else
7306 statement = begin_switch_stmt ();
7307
7308 /* Parse the condition. */
7309 condition = cp_parser_condition (parser);
7310 /* Look for the `)'. */
640710cf 7311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3d0f901b 7312 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7313 /*consume_paren=*/true);
0a3b29ad 7314
7315 if (keyword == RID_IF)
7316 {
e534436e 7317 bool nested_if;
2672c56c 7318 unsigned char in_statement;
e534436e 7319
0a3b29ad 7320 /* Add the condition. */
7321 finish_if_stmt_cond (condition, statement);
7322
7323 /* Parse the then-clause. */
2672c56c 7324 in_statement = parser->in_statement;
7325 parser->in_statement |= IN_IF_STMT;
c2c7830b 7326 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7327 {
7328 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
e60a6f7b 7329 add_stmt (build_empty_stmt (loc));
c2c7830b 7330 cp_lexer_consume_token (parser->lexer);
7331 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7332 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7333 "empty body in an %<if%> statement");
75b32336 7334 nested_if = false;
c2c7830b 7335 }
7336 else
7337 cp_parser_implicitly_scoped_statement (parser, &nested_if);
2672c56c 7338 parser->in_statement = in_statement;
7339
0a3b29ad 7340 finish_then_clause (statement);
7341
7342 /* If the next token is `else', parse the else-clause. */
7343 if (cp_lexer_next_token_is_keyword (parser->lexer,
7344 RID_ELSE))
7345 {
0a3b29ad 7346 /* Consume the `else' keyword. */
7347 cp_lexer_consume_token (parser->lexer);
2363ef00 7348 begin_else_clause (statement);
0a3b29ad 7349 /* Parse the else-clause. */
c2c7830b 7350 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7351 {
e60a6f7b 7352 location_t loc;
7353 loc = cp_lexer_peek_token (parser->lexer)->location;
7354 warning_at (loc,
c2c7830b 7355 OPT_Wempty_body, "suggest braces around "
7356 "empty body in an %<else%> statement");
e60a6f7b 7357 add_stmt (build_empty_stmt (loc));
c2c7830b 7358 cp_lexer_consume_token (parser->lexer);
7359 }
7360 else
7361 cp_parser_implicitly_scoped_statement (parser, NULL);
7362
0a3b29ad 7363 finish_else_clause (statement);
e534436e 7364
7365 /* If we are currently parsing a then-clause, then
7366 IF_P will not be NULL. We set it to true to
7367 indicate that this if statement has an else clause.
7368 This may trigger the Wparentheses warning below
7369 when we get back up to the parent if statement. */
7370 if (if_p != NULL)
7371 *if_p = true;
7372 }
7373 else
7374 {
7375 /* This if statement does not have an else clause. If
7376 NESTED_IF is true, then the then-clause is an if
7377 statement which does have an else clause. We warn
7378 about the potential ambiguity. */
7379 if (nested_if)
7380 warning (OPT_Wparentheses,
7381 ("%Hsuggest explicit braces "
7382 "to avoid ambiguous %<else%>"),
7383 EXPR_LOCUS (statement));
0a3b29ad 7384 }
7385
7386 /* Now we're all done with the if-statement. */
2363ef00 7387 finish_if_stmt (statement);
0a3b29ad 7388 }
7389 else
7390 {
c3fbce20 7391 bool in_switch_statement_p;
8487df40 7392 unsigned char in_statement;
0a3b29ad 7393
7394 /* Add the condition. */
7395 finish_switch_cond (condition, statement);
7396
7397 /* Parse the body of the switch-statement. */
c3fbce20 7398 in_switch_statement_p = parser->in_switch_statement_p;
8487df40 7399 in_statement = parser->in_statement;
c3fbce20 7400 parser->in_switch_statement_p = true;
8487df40 7401 parser->in_statement |= IN_SWITCH_STMT;
e534436e 7402 cp_parser_implicitly_scoped_statement (parser, NULL);
c3fbce20 7403 parser->in_switch_statement_p = in_switch_statement_p;
8487df40 7404 parser->in_statement = in_statement;
0a3b29ad 7405
7406 /* Now we're all done with the switch-statement. */
7407 finish_switch_stmt (statement);
7408 }
7409
7410 return statement;
7411 }
7412 break;
7413
7414 default:
7415 cp_parser_error (parser, "expected selection-statement");
7416 return error_mark_node;
7417 }
7418}
7419
ccb84981 7420/* Parse a condition.
0a3b29ad 7421
7422 condition:
7423 expression
f82f1250 7424 type-specifier-seq declarator = initializer-clause
7425 type-specifier-seq declarator braced-init-list
0a3b29ad 7426
7427 GNU Extension:
ccb84981 7428
0a3b29ad 7429 condition:
ccb84981 7430 type-specifier-seq declarator asm-specification [opt]
0a3b29ad 7431 attributes [opt] = assignment-expression
ccb84981 7432
0a3b29ad 7433 Returns the expression that should be tested. */
7434
7435static tree
45baea8b 7436cp_parser_condition (cp_parser* parser)
0a3b29ad 7437{
4b9b2871 7438 cp_decl_specifier_seq type_specifiers;
0a3b29ad 7439 const char *saved_message;
7440
7441 /* Try the declaration first. */
7442 cp_parser_parse_tentatively (parser);
7443 /* New types are not allowed in the type-specifier-seq for a
7444 condition. */
7445 saved_message = parser->type_definition_forbidden_message;
7446 parser->type_definition_forbidden_message
7447 = "types may not be defined in conditions";
7448 /* Parse the type-specifier-seq. */
6f74fe3c 7449 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7450 &type_specifiers);
0a3b29ad 7451 /* Restore the saved message. */
7452 parser->type_definition_forbidden_message = saved_message;
7453 /* If all is well, we might be looking at a declaration. */
7454 if (!cp_parser_error_occurred (parser))
7455 {
7456 tree decl;
7457 tree asm_specification;
7458 tree attributes;
3046c0a3 7459 cp_declarator *declarator;
0a3b29ad 7460 tree initializer = NULL_TREE;
ccb84981 7461
0a3b29ad 7462 /* Parse the declarator. */
42bbd0ec 7463 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 7464 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 7465 /*parenthesized_p=*/NULL,
7466 /*member_p=*/false);
0a3b29ad 7467 /* Parse the attributes. */
7468 attributes = cp_parser_attributes_opt (parser);
7469 /* Parse the asm-specification. */
7470 asm_specification = cp_parser_asm_specification_opt (parser);
f82f1250 7471 /* If the next token is not an `=' or '{', then we might still be
0a3b29ad 7472 looking at an expression. For example:
ccb84981 7473
0a3b29ad 7474 if (A(a).x)
ccb84981 7475
0a3b29ad 7476 looks like a decl-specifier-seq and a declarator -- but then
7477 there is no `=', so this is an expression. */
f82f1250 7478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7479 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7480 cp_parser_simulate_error (parser);
7481
7482 /* If we did see an `=' or '{', then we are looking at a declaration
0a3b29ad 7483 for sure. */
7484 if (cp_parser_parse_definitely (parser))
7485 {
9031d10b 7486 tree pushed_scope;
d91303a6 7487 bool non_constant_p;
f82f1250 7488 bool flags = LOOKUP_ONLYCONVERTING;
91caa6ca 7489
0a3b29ad 7490 /* Create the declaration. */
4b9b2871 7491 decl = start_decl (declarator, &type_specifiers,
0a3b29ad 7492 /*initialized_p=*/true,
91caa6ca 7493 attributes, /*prefix_attributes=*/NULL_TREE,
7f602bca 7494 &pushed_scope);
f82f1250 7495
7496 /* Parse the initializer. */
7497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7498 {
7499 initializer = cp_parser_braced_list (parser, &non_constant_p);
7500 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7501 flags = 0;
7502 }
7503 else
7504 {
7505 /* Consume the `='. */
905380a8 7506 cp_parser_require (parser, CPP_EQ, "%<=%>");
f82f1250 7507 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7508 }
7509 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7510 maybe_warn_cpp0x ("extended initializer lists");
7511
d91303a6 7512 if (!non_constant_p)
7513 initializer = fold_non_dependent_expr (initializer);
ccb84981 7514
0a3b29ad 7515 /* Process the initializer. */
ccb84981 7516 cp_finish_decl (decl,
074ab442 7517 initializer, !non_constant_p,
ccb84981 7518 asm_specification,
f82f1250 7519 flags);
00d26680 7520
7f602bca 7521 if (pushed_scope)
7522 pop_scope (pushed_scope);
ccb84981 7523
0a3b29ad 7524 return convert_from_reference (decl);
7525 }
7526 }
7527 /* If we didn't even get past the declarator successfully, we are
7528 definitely not looking at a declaration. */
7529 else
7530 cp_parser_abort_tentative_parse (parser);
7531
7532 /* Otherwise, we are looking at an expression. */
98b326fd 7533 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 7534}
7535
7536/* Parse an iteration-statement.
7537
7538 iteration-statement:
7539 while ( condition ) statement
7540 do statement while ( expression ) ;
7541 for ( for-init-statement condition [opt] ; expression [opt] )
7542 statement
7543
7544 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7545
7546static tree
45baea8b 7547cp_parser_iteration_statement (cp_parser* parser)
0a3b29ad 7548{
7549 cp_token *token;
7550 enum rid keyword;
7551 tree statement;
8487df40 7552 unsigned char in_statement;
0a3b29ad 7553
7554 /* Peek at the next token. */
7555 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7556 if (!token)
7557 return error_mark_node;
7558
c3fbce20 7559 /* Remember whether or not we are already within an iteration
ccb84981 7560 statement. */
8487df40 7561 in_statement = parser->in_statement;
c3fbce20 7562
0a3b29ad 7563 /* See what kind of keyword it is. */
7564 keyword = token->keyword;
7565 switch (keyword)
7566 {
7567 case RID_WHILE:
7568 {
7569 tree condition;
7570
7571 /* Begin the while-statement. */
7572 statement = begin_while_stmt ();
7573 /* Look for the `('. */
640710cf 7574 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 7575 /* Parse the condition. */
7576 condition = cp_parser_condition (parser);
7577 finish_while_stmt_cond (condition, statement);
7578 /* Look for the `)'. */
640710cf 7579 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 7580 /* Parse the dependent statement. */
8487df40 7581 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 7582 cp_parser_already_scoped_statement (parser);
8487df40 7583 parser->in_statement = in_statement;
0a3b29ad 7584 /* We're done with the while-statement. */
7585 finish_while_stmt (statement);
7586 }
7587 break;
7588
7589 case RID_DO:
7590 {
7591 tree expression;
7592
7593 /* Begin the do-statement. */
7594 statement = begin_do_stmt ();
7595 /* Parse the body of the do-statement. */
8487df40 7596 parser->in_statement = IN_ITERATION_STMT;
e534436e 7597 cp_parser_implicitly_scoped_statement (parser, NULL);
8487df40 7598 parser->in_statement = in_statement;
0a3b29ad 7599 finish_do_body (statement);
7600 /* Look for the `while' keyword. */
640710cf 7601 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
0a3b29ad 7602 /* Look for the `('. */
640710cf 7603 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 7604 /* Parse the expression. */
98b326fd 7605 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 7606 /* We're done with the do-statement. */
7607 finish_do_stmt (expression, statement);
7608 /* Look for the `)'. */
640710cf 7609 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 7610 /* Look for the `;'. */
640710cf 7611 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7612 }
7613 break;
7614
7615 case RID_FOR:
7616 {
7617 tree condition = NULL_TREE;
7618 tree expression = NULL_TREE;
7619
7620 /* Begin the for-statement. */
7621 statement = begin_for_stmt ();
7622 /* Look for the `('. */
640710cf 7623 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 7624 /* Parse the initialization. */
7625 cp_parser_for_init_statement (parser);
7626 finish_for_init_stmt (statement);
7627
7628 /* If there's a condition, process it. */
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7630 condition = cp_parser_condition (parser);
7631 finish_for_cond (condition, statement);
7632 /* Look for the `;'. */
640710cf 7633 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7634
7635 /* If there's an expression, process it. */
7636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
98b326fd 7637 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 7638 finish_for_expr (expression, statement);
7639 /* Look for the `)'. */
640710cf 7640 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
207355ad 7641
0a3b29ad 7642 /* Parse the body of the for-statement. */
8487df40 7643 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 7644 cp_parser_already_scoped_statement (parser);
8487df40 7645 parser->in_statement = in_statement;
0a3b29ad 7646
7647 /* We're done with the for-statement. */
7648 finish_for_stmt (statement);
7649 }
7650 break;
7651
7652 default:
7653 cp_parser_error (parser, "expected iteration-statement");
7654 statement = error_mark_node;
7655 break;
7656 }
7657
7658 return statement;
7659}
7660
7661/* Parse a for-init-statement.
7662
7663 for-init-statement:
7664 expression-statement
7665 simple-declaration */
7666
7667static void
45baea8b 7668cp_parser_for_init_statement (cp_parser* parser)
0a3b29ad 7669{
7670 /* If the next token is a `;', then we have an empty
755edffd 7671 expression-statement. Grammatically, this is also a
0a3b29ad 7672 simple-declaration, but an invalid one, because it does not
7673 declare anything. Therefore, if we did not handle this case
7674 specially, we would issue an error message about an invalid
7675 declaration. */
7676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7677 {
7678 /* We're going to speculatively look for a declaration, falling back
7679 to an expression, if necessary. */
7680 cp_parser_parse_tentatively (parser);
7681 /* Parse the declaration. */
7682 cp_parser_simple_declaration (parser,
7683 /*function_definition_allowed_p=*/false);
7684 /* If the tentative parse failed, then we shall need to look for an
7685 expression-statement. */
7686 if (cp_parser_parse_definitely (parser))
7687 return;
7688 }
7689
942ab15b 7690 cp_parser_expression_statement (parser, false);
0a3b29ad 7691}
7692
7693/* Parse a jump-statement.
7694
7695 jump-statement:
7696 break ;
7697 continue ;
7698 return expression [opt] ;
f82f1250 7699 return braced-init-list ;
ccb84981 7700 goto identifier ;
0a3b29ad 7701
7702 GNU extension:
7703
7704 jump-statement:
7705 goto * expression ;
7706
c857cd60 7707 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
0a3b29ad 7708
7709static tree
45baea8b 7710cp_parser_jump_statement (cp_parser* parser)
0a3b29ad 7711{
7712 tree statement = error_mark_node;
7713 cp_token *token;
7714 enum rid keyword;
2672c56c 7715 unsigned char in_statement;
0a3b29ad 7716
7717 /* Peek at the next token. */
7718 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7719 if (!token)
7720 return error_mark_node;
7721
7722 /* See what kind of keyword it is. */
7723 keyword = token->keyword;
7724 switch (keyword)
7725 {
7726 case RID_BREAK:
2672c56c 7727 in_statement = parser->in_statement & ~IN_IF_STMT;
7728 switch (in_statement)
c3fbce20 7729 {
8487df40 7730 case 0:
ad9ae192 7731 error ("%Hbreak statement not within loop or switch", &token->location);
8487df40 7732 break;
7733 default:
2672c56c 7734 gcc_assert ((in_statement & IN_SWITCH_STMT)
7735 || in_statement == IN_ITERATION_STMT);
8487df40 7736 statement = finish_break_stmt ();
7737 break;
7738 case IN_OMP_BLOCK:
ad9ae192 7739 error ("%Hinvalid exit from OpenMP structured block", &token->location);
8487df40 7740 break;
7741 case IN_OMP_FOR:
ad9ae192 7742 error ("%Hbreak statement used with OpenMP for loop", &token->location);
8487df40 7743 break;
c3fbce20 7744 }
a2c5b975 7745 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7746 break;
7747
7748 case RID_CONTINUE:
2672c56c 7749 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
c3fbce20 7750 {
8487df40 7751 case 0:
ad9ae192 7752 error ("%Hcontinue statement not within a loop", &token->location);
8487df40 7753 break;
7754 case IN_ITERATION_STMT:
7755 case IN_OMP_FOR:
7756 statement = finish_continue_stmt ();
7757 break;
7758 case IN_OMP_BLOCK:
ad9ae192 7759 error ("%Hinvalid exit from OpenMP structured block", &token->location);
8487df40 7760 break;
7761 default:
7762 gcc_unreachable ();
c3fbce20 7763 }
a2c5b975 7764 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7765 break;
7766
7767 case RID_RETURN:
7768 {
7769 tree expr;
f82f1250 7770 bool expr_non_constant_p;
0a3b29ad 7771
f82f1250 7772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7773 {
7774 maybe_warn_cpp0x ("extended initializer lists");
7775 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7776 }
7777 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 7778 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 7779 else
f82f1250 7780 /* If the next token is a `;', then there is no
7781 expression. */
0a3b29ad 7782 expr = NULL_TREE;
7783 /* Build the return-statement. */
7784 statement = finish_return_stmt (expr);
7785 /* Look for the final `;'. */
a2c5b975 7786 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7787 }
7788 break;
7789
7790 case RID_GOTO:
7791 /* Create the goto-statement. */
7792 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7793 {
7794 /* Issue a warning about this use of a GNU extension. */
21ca8540 7795 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
0a3b29ad 7796 /* Consume the '*' token. */
7797 cp_lexer_consume_token (parser->lexer);
7798 /* Parse the dependent expression. */
98b326fd 7799 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
0a3b29ad 7800 }
7801 else
7802 finish_goto_stmt (cp_parser_identifier (parser));
7803 /* Look for the final `;'. */
a2c5b975 7804 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 7805 break;
7806
7807 default:
7808 cp_parser_error (parser, "expected jump-statement");
7809 break;
7810 }
7811
7812 return statement;
7813}
7814
7815/* Parse a declaration-statement.
7816
7817 declaration-statement:
7818 block-declaration */
7819
7820static void
45baea8b 7821cp_parser_declaration_statement (cp_parser* parser)
0a3b29ad 7822{
3046c0a3 7823 void *p;
7824
7825 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7826 p = obstack_alloc (&declarator_obstack, 0);
7827
7828 /* Parse the block-declaration. */
0a3b29ad 7829 cp_parser_block_declaration (parser, /*statement_p=*/true);
7830
3046c0a3 7831 /* Free any declarators allocated. */
7832 obstack_free (&declarator_obstack, p);
7833
0a3b29ad 7834 /* Finish off the statement. */
7835 finish_stmt ();
7836}
7837
7838/* Some dependent statements (like `if (cond) statement'), are
7839 implicitly in their own scope. In other words, if the statement is
7840 a single statement (as opposed to a compound-statement), it is
7841 none-the-less treated as if it were enclosed in braces. Any
7842 declarations appearing in the dependent statement are out of scope
7843 after control passes that point. This function parses a statement,
7844 but ensures that is in its own scope, even if it is not a
ccb84981 7845 compound-statement.
0a3b29ad 7846
e534436e 7847 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7848 is a (possibly labeled) if statement which is not enclosed in
7849 braces and has an else clause. This is used to implement
7850 -Wparentheses.
7851
0a3b29ad 7852 Returns the new statement. */
7853
7854static tree
e534436e 7855cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
0a3b29ad 7856{
7857 tree statement;
7858
e534436e 7859 if (if_p != NULL)
7860 *if_p = false;
7861
50247dd9 7862 /* Mark if () ; with a special NOP_EXPR. */
7863 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7864 {
e60a6f7b 7865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50247dd9 7866 cp_lexer_consume_token (parser->lexer);
e60a6f7b 7867 statement = add_stmt (build_empty_stmt (loc));
50247dd9 7868 }
7869 /* if a compound is opened, we simply parse the statement directly. */
7870 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7871 statement = cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 7872 /* If the token is not a `{', then we must take special action. */
50247dd9 7873 else
0a3b29ad 7874 {
7875 /* Create a compound-statement. */
2363ef00 7876 statement = begin_compound_stmt (0);
0a3b29ad 7877 /* Parse the dependent-statement. */
e534436e 7878 cp_parser_statement (parser, NULL_TREE, false, if_p);
0a3b29ad 7879 /* Finish the dummy compound-statement. */
68f8f8cc 7880 finish_compound_stmt (statement);
0a3b29ad 7881 }
0a3b29ad 7882
7883 /* Return the statement. */
7884 return statement;
7885}
7886
7887/* For some dependent statements (like `while (cond) statement'), we
7888 have already created a scope. Therefore, even if the dependent
7889 statement is a compound-statement, we do not want to create another
7890 scope. */
7891
7892static void
45baea8b 7893cp_parser_already_scoped_statement (cp_parser* parser)
0a3b29ad 7894{
2363ef00 7895 /* If the token is a `{', then we must take special action. */
7896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
e534436e 7897 cp_parser_statement (parser, NULL_TREE, false, NULL);
2363ef00 7898 else
0a3b29ad 7899 {
2363ef00 7900 /* Avoid calling cp_parser_compound_statement, so that we
7901 don't create a new scope. Do everything else by hand. */
640710cf 7902 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
1f529806 7903 /* If the next keyword is `__label__' we have a label declaration. */
7904 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7905 cp_parser_label_declaration (parser);
7906 /* Parse an (optional) statement-seq. */
b75b98aa 7907 cp_parser_statement_seq_opt (parser, NULL_TREE);
640710cf 7908 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 7909 }
0a3b29ad 7910}
7911
7912/* Declarations [gram.dcl.dcl] */
7913
7914/* Parse an optional declaration-sequence.
7915
7916 declaration-seq:
7917 declaration
7918 declaration-seq declaration */
7919
7920static void
45baea8b 7921cp_parser_declaration_seq_opt (cp_parser* parser)
0a3b29ad 7922{
7923 while (true)
7924 {
7925 cp_token *token;
7926
7927 token = cp_lexer_peek_token (parser->lexer);
7928
7929 if (token->type == CPP_CLOSE_BRACE
b75b98aa 7930 || token->type == CPP_EOF
7931 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 7932 break;
7933
ccb84981 7934 if (token->type == CPP_SEMICOLON)
0a3b29ad 7935 {
7936 /* A declaration consisting of a single semicolon is
7937 invalid. Allow it unless we're being pedantic. */
0a3b29ad 7938 cp_lexer_consume_token (parser->lexer);
8864917d 7939 if (!in_system_header)
21ca8540 7940 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
0a3b29ad 7941 continue;
7942 }
7943
2e1f41a9 7944 /* If we're entering or exiting a region that's implicitly
93523877 7945 extern "C", modify the lang context appropriately. */
2e1f41a9 7946 if (!parser->implicit_extern_c && token->implicit_extern_c)
7947 {
7948 push_lang_context (lang_name_c);
7949 parser->implicit_extern_c = true;
7950 }
7951 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7952 {
7953 pop_lang_context ();
7954 parser->implicit_extern_c = false;
7955 }
7956
3986d990 7957 if (token->type == CPP_PRAGMA)
7958 {
7959 /* A top-level declaration can consist solely of a #pragma.
7960 A nested declaration cannot, so this is done here and not
7961 in cp_parser_declaration. (A #pragma at block scope is
7962 handled in cp_parser_statement.) */
b75b98aa 7963 cp_parser_pragma (parser, pragma_external);
3986d990 7964 continue;
7965 }
7966
313a21c0 7967 /* Parse the declaration itself. */
0a3b29ad 7968 cp_parser_declaration (parser);
7969 }
7970}
7971
7972/* Parse a declaration.
7973
7974 declaration:
7975 block-declaration
7976 function-definition
7977 template-declaration
7978 explicit-instantiation
7979 explicit-specialization
7980 linkage-specification
ccb84981 7981 namespace-definition
2c6a8806 7982
7983 GNU extension:
7984
7985 declaration:
7986 __extension__ declaration */
0a3b29ad 7987
7988static void
45baea8b 7989cp_parser_declaration (cp_parser* parser)
0a3b29ad 7990{
7991 cp_token token1;
7992 cp_token token2;
2c6a8806 7993 int saved_pedantic;
3046c0a3 7994 void *p;
2c6a8806 7995
7996 /* Check for the `__extension__' keyword. */
7997 if (cp_parser_extension_opt (parser, &saved_pedantic))
7998 {
7999 /* Parse the qualified declaration. */
8000 cp_parser_declaration (parser);
8001 /* Restore the PEDANTIC flag. */
8002 pedantic = saved_pedantic;
8003
8004 return;
8005 }
0a3b29ad 8006
8007 /* Try to figure out what kind of declaration is present. */
8008 token1 = *cp_lexer_peek_token (parser->lexer);
ccb84981 8009
0a3b29ad 8010 if (token1.type != CPP_EOF)
8011 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8102ba14 8012 else
0f803187 8013 {
8014 token2.type = CPP_EOF;
8015 token2.keyword = RID_MAX;
8016 }
0a3b29ad 8017
3046c0a3 8018 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8019 p = obstack_alloc (&declarator_obstack, 0);
8020
0a3b29ad 8021 /* If the next token is `extern' and the following token is a string
8022 literal, then we have a linkage specification. */
8023 if (token1.keyword == RID_EXTERN
8024 && cp_parser_is_string_literal (&token2))
8025 cp_parser_linkage_specification (parser);
8026 /* If the next token is `template', then we have either a template
8027 declaration, an explicit instantiation, or an explicit
8028 specialization. */
8029 else if (token1.keyword == RID_TEMPLATE)
8030 {
8031 /* `template <>' indicates a template specialization. */
8032 if (token2.type == CPP_LESS
8033 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8034 cp_parser_explicit_specialization (parser);
8035 /* `template <' indicates a template declaration. */
8036 else if (token2.type == CPP_LESS)
8037 cp_parser_template_declaration (parser, /*member_p=*/false);
8038 /* Anything else must be an explicit instantiation. */
8039 else
8040 cp_parser_explicit_instantiation (parser);
8041 }
8042 /* If the next token is `export', then we have a template
8043 declaration. */
8044 else if (token1.keyword == RID_EXPORT)
8045 cp_parser_template_declaration (parser, /*member_p=*/false);
8046 /* If the next token is `extern', 'static' or 'inline' and the one
8047 after that is `template', we have a GNU extended explicit
8048 instantiation directive. */
8049 else if (cp_parser_allow_gnu_extensions_p (parser)
8050 && (token1.keyword == RID_EXTERN
8051 || token1.keyword == RID_STATIC
8052 || token1.keyword == RID_INLINE)
8053 && token2.keyword == RID_TEMPLATE)
8054 cp_parser_explicit_instantiation (parser);
8055 /* If the next token is `namespace', check for a named or unnamed
8056 namespace definition. */
8057 else if (token1.keyword == RID_NAMESPACE
8058 && (/* A named namespace definition. */
8059 (token2.type == CPP_NAME
ccb84981 8060 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
799435d8 8061 != CPP_EQ))
0a3b29ad 8062 /* An unnamed namespace definition. */
30e98711 8063 || token2.type == CPP_OPEN_BRACE
8064 || token2.keyword == RID_ATTRIBUTE))
0a3b29ad 8065 cp_parser_namespace_definition (parser);
93635a8e 8066 /* An inline (associated) namespace definition. */
8067 else if (token1.keyword == RID_INLINE
8068 && token2.keyword == RID_NAMESPACE)
8069 cp_parser_namespace_definition (parser);
7a4e126b 8070 /* Objective-C++ declaration/definition. */
8071 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8072 cp_parser_objc_declaration (parser);
0a3b29ad 8073 /* We must have either a block declaration or a function
8074 definition. */
8075 else
8076 /* Try to parse a block-declaration, or a function-definition. */
8077 cp_parser_block_declaration (parser, /*statement_p=*/false);
3046c0a3 8078
8079 /* Free any declarators allocated. */
8080 obstack_free (&declarator_obstack, p);
0a3b29ad 8081}
8082
ccb84981 8083/* Parse a block-declaration.
0a3b29ad 8084
8085 block-declaration:
8086 simple-declaration
8087 asm-definition
8088 namespace-alias-definition
8089 using-declaration
ccb84981 8090 using-directive
0a3b29ad 8091
8092 GNU Extension:
8093
8094 block-declaration:
ccb84981 8095 __extension__ block-declaration
0a3b29ad 8096
7a05c4b1 8097 C++0x Extension:
8098
8099 block-declaration:
8100 static_assert-declaration
8101
755edffd 8102 If STATEMENT_P is TRUE, then this block-declaration is occurring as
0a3b29ad 8103 part of a declaration-statement. */
8104
8105static void
ccb84981 8106cp_parser_block_declaration (cp_parser *parser,
0a3b29ad 8107 bool statement_p)
8108{
8109 cp_token *token1;
8110 int saved_pedantic;
8111
8112 /* Check for the `__extension__' keyword. */
8113 if (cp_parser_extension_opt (parser, &saved_pedantic))
8114 {
8115 /* Parse the qualified declaration. */
8116 cp_parser_block_declaration (parser, statement_p);
8117 /* Restore the PEDANTIC flag. */
8118 pedantic = saved_pedantic;
8119
8120 return;
8121 }
8122
8123 /* Peek at the next token to figure out which kind of declaration is
8124 present. */
8125 token1 = cp_lexer_peek_token (parser->lexer);
8126
8127 /* If the next keyword is `asm', we have an asm-definition. */
8128 if (token1->keyword == RID_ASM)
8129 {
8130 if (statement_p)
8131 cp_parser_commit_to_tentative_parse (parser);
8132 cp_parser_asm_definition (parser);
8133 }
8134 /* If the next keyword is `namespace', we have a
8135 namespace-alias-definition. */
8136 else if (token1->keyword == RID_NAMESPACE)
8137 cp_parser_namespace_alias_definition (parser);
8138 /* If the next keyword is `using', we have either a
8139 using-declaration or a using-directive. */
8140 else if (token1->keyword == RID_USING)
8141 {
8142 cp_token *token2;
8143
8144 if (statement_p)
8145 cp_parser_commit_to_tentative_parse (parser);
8146 /* If the token after `using' is `namespace', then we have a
8147 using-directive. */
8148 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8149 if (token2->keyword == RID_NAMESPACE)
8150 cp_parser_using_directive (parser);
8151 /* Otherwise, it's a using-declaration. */
8152 else
da2a3271 8153 cp_parser_using_declaration (parser,
8154 /*access_declaration_p=*/false);
0a3b29ad 8155 }
3dac447c 8156 /* If the next keyword is `__label__' we have a misplaced label
8157 declaration. */
0a3b29ad 8158 else if (token1->keyword == RID_LABEL)
8159 {
3dac447c 8160 cp_lexer_consume_token (parser->lexer);
ad9ae192 8161 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
3dac447c 8162 cp_parser_skip_to_end_of_statement (parser);
8163 /* If the next token is now a `;', consume it. */
8164 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8165 cp_lexer_consume_token (parser->lexer);
0a3b29ad 8166 }
7a05c4b1 8167 /* If the next token is `static_assert' we have a static assertion. */
8168 else if (token1->keyword == RID_STATIC_ASSERT)
8169 cp_parser_static_assert (parser, /*member_p=*/false);
0a3b29ad 8170 /* Anything else must be a simple-declaration. */
8171 else
8172 cp_parser_simple_declaration (parser, !statement_p);
8173}
8174
8175/* Parse a simple-declaration.
8176
8177 simple-declaration:
ccb84981 8178 decl-specifier-seq [opt] init-declarator-list [opt] ;
0a3b29ad 8179
8180 init-declarator-list:
8181 init-declarator
ccb84981 8182 init-declarator-list , init-declarator
0a3b29ad 8183
755edffd 8184 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6beb3f76 8185 function-definition as a simple-declaration. */
0a3b29ad 8186
8187static void
ccb84981 8188cp_parser_simple_declaration (cp_parser* parser,
653e5405 8189 bool function_definition_allowed_p)
0a3b29ad 8190{
4b9b2871 8191 cp_decl_specifier_seq decl_specifiers;
8172be22 8192 int declares_class_or_enum;
0a3b29ad 8193 bool saw_declarator;
8194
8195 /* Defer access checks until we know what is being declared; the
8196 checks for names appearing in the decl-specifier-seq should be
8197 done as if we were in the scope of the thing being declared. */
4f62c42e 8198 push_deferring_access_checks (dk_deferred);
9b57b06b 8199
0a3b29ad 8200 /* Parse the decl-specifier-seq. We have to keep track of whether
8201 or not the decl-specifier-seq declares a named class or
8202 enumeration type, since that is the only case in which the
ccb84981 8203 init-declarator-list is allowed to be empty.
0a3b29ad 8204
8205 [dcl.dcl]
8206
8207 In a simple-declaration, the optional init-declarator-list can be
8208 omitted only when declaring a class or enumeration, that is when
8209 the decl-specifier-seq contains either a class-specifier, an
8210 elaborated-type-specifier, or an enum-specifier. */
4b9b2871 8211 cp_parser_decl_specifier_seq (parser,
8212 CP_PARSER_FLAGS_OPTIONAL,
8213 &decl_specifiers,
8214 &declares_class_or_enum);
0a3b29ad 8215 /* We no longer need to defer access checks. */
9b57b06b 8216 stop_deferring_access_checks ();
7488d745 8217
878870b4 8218 /* In a block scope, a valid declaration must always have a
8219 decl-specifier-seq. By not trying to parse declarators, we can
8220 resolve the declaration/expression ambiguity more quickly. */
207355ad 8221 if (!function_definition_allowed_p
4b9b2871 8222 && !decl_specifiers.any_specifiers_p)
878870b4 8223 {
8224 cp_parser_error (parser, "expected declaration");
8225 goto done;
8226 }
8227
954ad420 8228 /* If the next two tokens are both identifiers, the code is
8229 erroneous. The usual cause of this situation is code like:
8230
8231 T t;
8232
8233 where "T" should name a type -- but does not. */
882d3c4f 8234 if (!decl_specifiers.type
8235 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
954ad420 8236 {
4f62c42e 8237 /* If parsing tentatively, we should commit; we really are
954ad420 8238 looking at a declaration. */
8239 cp_parser_commit_to_tentative_parse (parser);
8240 /* Give up. */
878870b4 8241 goto done;
954ad420 8242 }
9031d10b 8243
d1a64350 8244 /* If we have seen at least one decl-specifier, and the next token
8245 is not a parenthesis, then we must be looking at a declaration.
8246 (After "int (" we might be looking at a functional cast.) */
9031d10b 8247 if (decl_specifiers.any_specifiers_p
f82f1250 8248 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8659a722 8249 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8250 && !cp_parser_error_occurred (parser))
d1a64350 8251 cp_parser_commit_to_tentative_parse (parser);
954ad420 8252
0a3b29ad 8253 /* Keep going until we hit the `;' at the end of the simple
8254 declaration. */
8255 saw_declarator = false;
ccb84981 8256 while (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 8257 CPP_SEMICOLON))
8258 {
8259 cp_token *token;
8260 bool function_definition_p;
8172be22 8261 tree decl;
0a3b29ad 8262
b60e927a 8263 if (saw_declarator)
8264 {
8265 /* If we are processing next declarator, coma is expected */
8266 token = cp_lexer_peek_token (parser->lexer);
8267 gcc_assert (token->type == CPP_COMMA);
8268 cp_lexer_consume_token (parser->lexer);
8269 }
8270 else
8271 saw_declarator = true;
8272
0a3b29ad 8273 /* Parse the init-declarator. */
4b9b2871 8274 decl = cp_parser_init_declarator (parser, &decl_specifiers,
3369eb76 8275 /*checks=*/NULL,
8172be22 8276 function_definition_allowed_p,
8277 /*member_p=*/false,
8278 declares_class_or_enum,
8279 &function_definition_p);
7e9a6a16 8280 /* If an error occurred while parsing tentatively, exit quickly.
8281 (That usually happens when in the body of a function; each
8282 statement is treated as a declaration-statement until proven
8283 otherwise.) */
8284 if (cp_parser_error_occurred (parser))
878870b4 8285 goto done;
0a3b29ad 8286 /* Handle function definitions specially. */
8287 if (function_definition_p)
8288 {
8289 /* If the next token is a `,', then we are probably
8290 processing something like:
8291
8292 void f() {}, *p;
8293
8294 which is erroneous. */
8295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
ad9ae192 8296 {
8297 cp_token *token = cp_lexer_peek_token (parser->lexer);
8298 error ("%Hmixing declarations and function-definitions is forbidden",
8299 &token->location);
8300 }
0a3b29ad 8301 /* Otherwise, we're done with the list of declarators. */
8302 else
7488d745 8303 {
9b57b06b 8304 pop_deferring_access_checks ();
7488d745 8305 return;
8306 }
0a3b29ad 8307 }
8308 /* The next token should be either a `,' or a `;'. */
8309 token = cp_lexer_peek_token (parser->lexer);
8310 /* If it's a `,', there are more declarators to come. */
8311 if (token->type == CPP_COMMA)
b60e927a 8312 /* will be consumed next time around */;
0a3b29ad 8313 /* If it's a `;', we are done. */
8314 else if (token->type == CPP_SEMICOLON)
8315 break;
8316 /* Anything else is an error. */
8317 else
8318 {
d1a64350 8319 /* If we have already issued an error message we don't need
8320 to issue another one. */
8321 if (decl != error_mark_node
efcbcf83 8322 || cp_parser_uncommitted_to_tentative_parse_p (parser))
a2c5b975 8323 cp_parser_error (parser, "expected %<,%> or %<;%>");
0a3b29ad 8324 /* Skip tokens until we reach the end of the statement. */
8325 cp_parser_skip_to_end_of_statement (parser);
2c584053 8326 /* If the next token is now a `;', consume it. */
8327 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8328 cp_lexer_consume_token (parser->lexer);
878870b4 8329 goto done;
0a3b29ad 8330 }
8331 /* After the first time around, a function-definition is not
8332 allowed -- even if it was OK at first. For example:
8333
653e5405 8334 int i, f() {}
0a3b29ad 8335
653e5405 8336 is not valid. */
0a3b29ad 8337 function_definition_allowed_p = false;
8338 }
8339
8340 /* Issue an error message if no declarators are present, and the
8341 decl-specifier-seq does not itself declare a class or
8342 enumeration. */
8343 if (!saw_declarator)
8344 {
8345 if (cp_parser_declares_only_class_p (parser))
4b9b2871 8346 shadow_tag (&decl_specifiers);
0a3b29ad 8347 /* Perform any deferred access checks. */
9b57b06b 8348 perform_deferred_access_checks ();
0a3b29ad 8349 }
8350
8351 /* Consume the `;'. */
640710cf 8352 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 8353
878870b4 8354 done:
8355 pop_deferring_access_checks ();
0a3b29ad 8356}
8357
8358/* Parse a decl-specifier-seq.
8359
8360 decl-specifier-seq:
8361 decl-specifier-seq [opt] decl-specifier
8362
8363 decl-specifier:
8364 storage-class-specifier
8365 type-specifier
8366 function-specifier
8367 friend
ccb84981 8368 typedef
0a3b29ad 8369
8370 GNU Extension:
8371
e67a67ea 8372 decl-specifier:
8373 attributes
0a3b29ad 8374
4b9b2871 8375 Set *DECL_SPECS to a representation of the decl-specifier-seq.
0a3b29ad 8376
fb871e92 8377 The parser flags FLAGS is used to control type-specifier parsing.
8172be22 8378
8379 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8e594c09 8380 flags:
8172be22 8381
8382 1: one of the decl-specifiers is an elaborated-type-specifier
653e5405 8383 (i.e., a type declaration)
8172be22 8384 2: one of the decl-specifiers is an enum-specifier or a
653e5405 8385 class-specifier (i.e., a type definition)
207355ad 8386
8172be22 8387 */
0a3b29ad 8388
4b9b2871 8389static void
ccb84981 8390cp_parser_decl_specifier_seq (cp_parser* parser,
4b9b2871 8391 cp_parser_flags flags,
8392 cp_decl_specifier_seq *decl_specs,
8172be22 8393 int* declares_class_or_enum)
0a3b29ad 8394{
87647325 8395 bool constructor_possible_p = !parser->in_declarator_p;
ad9ae192 8396 cp_token *start_token = NULL;
ccb84981 8397
4b9b2871 8398 /* Clear DECL_SPECS. */
8399 clear_decl_specs (decl_specs);
8400
0a3b29ad 8401 /* Assume no class or enumeration type is declared. */
8172be22 8402 *declares_class_or_enum = 0;
0a3b29ad 8403
0a3b29ad 8404 /* Keep reading specifiers until there are no more to read. */
8405 while (true)
8406 {
0a3b29ad 8407 bool constructor_p;
4b9b2871 8408 bool found_decl_spec;
0a3b29ad 8409 cp_token *token;
8410
8411 /* Peek at the next token. */
8412 token = cp_lexer_peek_token (parser->lexer);
ad9ae192 8413
8414 /* Save the first token of the decl spec list for error
8415 reporting. */
8416 if (!start_token)
8417 start_token = token;
0a3b29ad 8418 /* Handle attributes. */
8419 if (token->keyword == RID_ATTRIBUTE)
8420 {
8421 /* Parse the attributes. */
207355ad 8422 decl_specs->attributes
4b9b2871 8423 = chainon (decl_specs->attributes,
8424 cp_parser_attributes_opt (parser));
0a3b29ad 8425 continue;
8426 }
4b9b2871 8427 /* Assume we will find a decl-specifier keyword. */
8428 found_decl_spec = true;
0a3b29ad 8429 /* If the next token is an appropriate keyword, we can simply
8430 add it to the list. */
8431 switch (token->keyword)
8432 {
0a3b29ad 8433 /* decl-specifier:
8434 friend */
4b9b2871 8435 case RID_FRIEND:
acd412e7 8436 if (!at_class_scope_p ())
8437 {
3d0e0017 8438 error ("%H%<friend%> used outside of class", &token->location);
acd412e7 8439 cp_lexer_purge_token (parser->lexer);
8440 }
8441 else
8442 {
8443 ++decl_specs->specs[(int) ds_friend];
8444 /* Consume the token. */
8445 cp_lexer_consume_token (parser->lexer);
8446 }
0a3b29ad 8447 break;
8448
8449 /* function-specifier:
8450 inline
8451 virtual
8452 explicit */
8453 case RID_INLINE:
8454 case RID_VIRTUAL:
8455 case RID_EXPLICIT:
4b9b2871 8456 cp_parser_function_specifier_opt (parser, decl_specs);
0a3b29ad 8457 break;
ccb84981 8458
0a3b29ad 8459 /* decl-specifier:
8460 typedef */
8461 case RID_TYPEDEF:
4b9b2871 8462 ++decl_specs->specs[(int) ds_typedef];
0a3b29ad 8463 /* Consume the token. */
8464 cp_lexer_consume_token (parser->lexer);
b3c48b5d 8465 /* A constructor declarator cannot appear in a typedef. */
8466 constructor_possible_p = false;
f3b70d2f 8467 /* The "typedef" keyword can only occur in a declaration; we
8468 may as well commit at this point. */
8469 cp_parser_commit_to_tentative_parse (parser);
ceec99b9 8470
8471 if (decl_specs->storage_class != sc_none)
8472 decl_specs->conflicting_specifiers_p = true;
0a3b29ad 8473 break;
8474
8475 /* storage-class-specifier:
8476 auto
8477 register
8478 static
8479 extern
ccb84981 8480 mutable
0a3b29ad 8481
653e5405 8482 GNU Extension:
0a3b29ad 8483 thread */
8484 case RID_AUTO:
45b44d0a 8485 if (cxx_dialect == cxx98)
8486 {
46f4817e 8487 /* Consume the token. */
8488 cp_lexer_consume_token (parser->lexer);
8489
45b44d0a 8490 /* Complain about `auto' as a storage specifier, if
8491 we're complaining about C++0x compatibility. */
8492 warning
8493 (OPT_Wc__0x_compat,
ad9ae192 8494 "%H%<auto%> will change meaning in C++0x; please remove it",
8495 &token->location);
45b44d0a 8496
8497 /* Set the storage class anyway. */
ad9ae192 8498 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8499 token->location);
45b44d0a 8500 }
46f4817e 8501 else
8502 /* C++0x auto type-specifier. */
8503 found_decl_spec = false;
45b44d0a 8504 break;
8505
0a3b29ad 8506 case RID_REGISTER:
8507 case RID_STATIC:
8508 case RID_EXTERN:
8509 case RID_MUTABLE:
4b9b2871 8510 /* Consume the token. */
8511 cp_lexer_consume_token (parser->lexer);
ad9ae192 8512 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8513 token->location);
4b9b2871 8514 break;
0a3b29ad 8515 case RID_THREAD:
4b9b2871 8516 /* Consume the token. */
8517 cp_lexer_consume_token (parser->lexer);
8518 ++decl_specs->specs[(int) ds_thread];
0a3b29ad 8519 break;
ccb84981 8520
0a3b29ad 8521 default:
4b9b2871 8522 /* We did not yet find a decl-specifier yet. */
8523 found_decl_spec = false;
0a3b29ad 8524 break;
8525 }
8526
8527 /* Constructors are a special case. The `S' in `S()' is not a
8528 decl-specifier; it is the beginning of the declarator. */
207355ad 8529 constructor_p
4b9b2871 8530 = (!found_decl_spec
8531 && constructor_possible_p
207355ad 8532 && (cp_parser_constructor_declarator_p
4b9b2871 8533 (parser, decl_specs->specs[(int) ds_friend] != 0)));
0a3b29ad 8534
8535 /* If we don't have a DECL_SPEC yet, then we must be looking at
8536 a type-specifier. */
4b9b2871 8537 if (!found_decl_spec && !constructor_p)
0a3b29ad 8538 {
8172be22 8539 int decl_spec_declares_class_or_enum;
0a3b29ad 8540 bool is_cv_qualifier;
4b9b2871 8541 tree type_spec;
0a3b29ad 8542
4b9b2871 8543 type_spec
0a3b29ad 8544 = cp_parser_type_specifier (parser, flags,
4b9b2871 8545 decl_specs,
0a3b29ad 8546 /*is_declaration=*/true,
8547 &decl_spec_declares_class_or_enum,
8548 &is_cv_qualifier);
0a3b29ad 8549 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8550
8551 /* If this type-specifier referenced a user-defined type
8552 (a typedef, class-name, etc.), then we can't allow any
8553 more such type-specifiers henceforth.
8554
8555 [dcl.spec]
8556
8557 The longest sequence of decl-specifiers that could
8558 possibly be a type name is taken as the
8559 decl-specifier-seq of a declaration. The sequence shall
8560 be self-consistent as described below.
8561
8562 [dcl.type]
8563
8564 As a general rule, at most one type-specifier is allowed
8565 in the complete decl-specifier-seq of a declaration. The
8566 only exceptions are the following:
8567
8568 -- const or volatile can be combined with any other
ccb84981 8569 type-specifier.
0a3b29ad 8570
8571 -- signed or unsigned can be combined with char, long,
8572 short, or int.
8573
8574 -- ..
8575
8576 Example:
8577
8578 typedef char* Pc;
8579 void g (const int Pc);
8580
8581 Here, Pc is *not* part of the decl-specifier seq; it's
8582 the declarator. Therefore, once we see a type-specifier
8583 (other than a cv-qualifier), we forbid any additional
8584 user-defined types. We *do* still allow things like `int
8585 int' to be considered a decl-specifier-seq, and issue the
8586 error message later. */
4b9b2871 8587 if (type_spec && !is_cv_qualifier)
0a3b29ad 8588 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
b3c48b5d 8589 /* A constructor declarator cannot follow a type-specifier. */
4b9b2871 8590 if (type_spec)
0a3b29ad 8591 {
4b9b2871 8592 constructor_possible_p = false;
8593 found_decl_spec = true;
0a3b29ad 8594 }
0a3b29ad 8595 }
8596
4b9b2871 8597 /* If we still do not have a DECL_SPEC, then there are no more
8598 decl-specifiers. */
8599 if (!found_decl_spec)
8600 break;
0a3b29ad 8601
4b9b2871 8602 decl_specs->any_specifiers_p = true;
0a3b29ad 8603 /* After we see one decl-specifier, further decl-specifiers are
8604 always optional. */
8605 flags |= CP_PARSER_FLAGS_OPTIONAL;
8606 }
8607
ad9ae192 8608 cp_parser_check_decl_spec (decl_specs, start_token->location);
36c61b55 8609
2822900d 8610 /* Don't allow a friend specifier with a class definition. */
4b9b2871 8611 if (decl_specs->specs[(int) ds_friend] != 0
8612 && (*declares_class_or_enum & 2))
ad9ae192 8613 error ("%Hclass definition may not be declared a friend",
8614 &start_token->location);
0a3b29ad 8615}
8616
ccb84981 8617/* Parse an (optional) storage-class-specifier.
0a3b29ad 8618
8619 storage-class-specifier:
8620 auto
8621 register
8622 static
8623 extern
ccb84981 8624 mutable
0a3b29ad 8625
8626 GNU Extension:
8627
8628 storage-class-specifier:
8629 thread
8630
8631 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
ccb84981 8632
0a3b29ad 8633static tree
45baea8b 8634cp_parser_storage_class_specifier_opt (cp_parser* parser)
0a3b29ad 8635{
8636 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8637 {
8638 case RID_AUTO:
45b44d0a 8639 if (cxx_dialect != cxx98)
8640 return NULL_TREE;
8641 /* Fall through for C++98. */
8642
0a3b29ad 8643 case RID_REGISTER:
8644 case RID_STATIC:
8645 case RID_EXTERN:
8646 case RID_MUTABLE:
8647 case RID_THREAD:
8648 /* Consume the token. */
3369eb76 8649 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 8650
8651 default:
8652 return NULL_TREE;
8653 }
8654}
8655
ccb84981 8656/* Parse an (optional) function-specifier.
0a3b29ad 8657
8658 function-specifier:
8659 inline
8660 virtual
8661 explicit
8662
4b9b2871 8663 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8664 Updates DECL_SPECS, if it is non-NULL. */
ccb84981 8665
0a3b29ad 8666static tree
4b9b2871 8667cp_parser_function_specifier_opt (cp_parser* parser,
8668 cp_decl_specifier_seq *decl_specs)
0a3b29ad 8669{
ad9ae192 8670 cp_token *token = cp_lexer_peek_token (parser->lexer);
8671 switch (token->keyword)
0a3b29ad 8672 {
8673 case RID_INLINE:
4b9b2871 8674 if (decl_specs)
8675 ++decl_specs->specs[(int) ds_inline];
8676 break;
8677
0a3b29ad 8678 case RID_VIRTUAL:
5f69415d 8679 /* 14.5.2.3 [temp.mem]
074ab442 8680
8681 A member function template shall not be virtual. */
5f69415d 8682 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
ad9ae192 8683 error ("%Htemplates may not be %<virtual%>", &token->location);
5f69415d 8684 else if (decl_specs)
4b9b2871 8685 ++decl_specs->specs[(int) ds_virtual];
8686 break;
8687
0a3b29ad 8688 case RID_EXPLICIT:
4b9b2871 8689 if (decl_specs)
8690 ++decl_specs->specs[(int) ds_explicit];
8691 break;
0a3b29ad 8692
8693 default:
8694 return NULL_TREE;
8695 }
4b9b2871 8696
8697 /* Consume the token. */
3369eb76 8698 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 8699}
8700
8701/* Parse a linkage-specification.
8702
8703 linkage-specification:
8704 extern string-literal { declaration-seq [opt] }
8705 extern string-literal declaration */
8706
8707static void
45baea8b 8708cp_parser_linkage_specification (cp_parser* parser)
0a3b29ad 8709{
0a3b29ad 8710 tree linkage;
8711
8712 /* Look for the `extern' keyword. */
640710cf 8713 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
0a3b29ad 8714
00d26680 8715 /* Look for the string-literal. */
8716 linkage = cp_parser_string_literal (parser, false, false);
0a3b29ad 8717
8718 /* Transform the literal into an identifier. If the literal is a
8719 wide-character string, or contains embedded NULs, then we can't
8720 handle it as the user wants. */
00d26680 8721 if (strlen (TREE_STRING_POINTER (linkage))
8722 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
0a3b29ad 8723 {
8724 cp_parser_error (parser, "invalid linkage-specification");
8725 /* Assume C++ linkage. */
00d26680 8726 linkage = lang_name_cplusplus;
0a3b29ad 8727 }
0a3b29ad 8728 else
00d26680 8729 linkage = get_identifier (TREE_STRING_POINTER (linkage));
0a3b29ad 8730
8731 /* We're now using the new linkage. */
8732 push_lang_context (linkage);
8733
8734 /* If the next token is a `{', then we're using the first
8735 production. */
8736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8737 {
8738 /* Consume the `{' token. */
8739 cp_lexer_consume_token (parser->lexer);
8740 /* Parse the declarations. */
8741 cp_parser_declaration_seq_opt (parser);
8742 /* Look for the closing `}'. */
640710cf 8743 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 8744 }
8745 /* Otherwise, there's just one declaration. */
8746 else
8747 {
8748 bool saved_in_unbraced_linkage_specification_p;
8749
ccb84981 8750 saved_in_unbraced_linkage_specification_p
0a3b29ad 8751 = parser->in_unbraced_linkage_specification_p;
8752 parser->in_unbraced_linkage_specification_p = true;
0a3b29ad 8753 cp_parser_declaration (parser);
ccb84981 8754 parser->in_unbraced_linkage_specification_p
0a3b29ad 8755 = saved_in_unbraced_linkage_specification_p;
8756 }
8757
8758 /* We're done with the linkage-specification. */
8759 pop_lang_context ();
8760}
8761
7a05c4b1 8762/* Parse a static_assert-declaration.
8763
8764 static_assert-declaration:
8765 static_assert ( constant-expression , string-literal ) ;
8766
8767 If MEMBER_P, this static_assert is a class member. */
8768
8769static void
8770cp_parser_static_assert(cp_parser *parser, bool member_p)
8771{
8772 tree condition;
8773 tree message;
8774 cp_token *token;
8775 location_t saved_loc;
8776
8777 /* Peek at the `static_assert' token so we can keep track of exactly
8778 where the static assertion started. */
8779 token = cp_lexer_peek_token (parser->lexer);
8780 saved_loc = token->location;
8781
8782 /* Look for the `static_assert' keyword. */
8783 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
640710cf 8784 "%<static_assert%>"))
7a05c4b1 8785 return;
8786
8787 /* We know we are in a static assertion; commit to any tentative
8788 parse. */
8789 if (cp_parser_parsing_tentatively (parser))
8790 cp_parser_commit_to_tentative_parse (parser);
8791
8792 /* Parse the `(' starting the static assertion condition. */
640710cf 8793 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7a05c4b1 8794
8795 /* Parse the constant-expression. */
8796 condition =
8797 cp_parser_constant_expression (parser,
8798 /*allow_non_constant_p=*/false,
8799 /*non_constant_p=*/NULL);
8800
8801 /* Parse the separating `,'. */
640710cf 8802 cp_parser_require (parser, CPP_COMMA, "%<,%>");
7a05c4b1 8803
8804 /* Parse the string-literal message. */
8805 message = cp_parser_string_literal (parser,
8806 /*translate=*/false,
8807 /*wide_ok=*/true);
8808
8809 /* A `)' completes the static assertion. */
640710cf 8810 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7a05c4b1 8811 cp_parser_skip_to_closing_parenthesis (parser,
8812 /*recovering=*/true,
8813 /*or_comma=*/false,
8814 /*consume_paren=*/true);
8815
8816 /* A semicolon terminates the declaration. */
640710cf 8817 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7a05c4b1 8818
8819 /* Complete the static assertion, which may mean either processing
8820 the static assert now or saving it for template instantiation. */
8821 finish_static_assert (condition, message, saved_loc, member_p);
8822}
8823
34da8800 8824/* Parse a `decltype' type. Returns the type.
8825
8826 simple-type-specifier:
8827 decltype ( expression ) */
8828
8829static tree
8830cp_parser_decltype (cp_parser *parser)
8831{
8832 tree expr;
8833 bool id_expression_or_member_access_p = false;
8834 const char *saved_message;
8835 bool saved_integral_constant_expression_p;
8836 bool saved_non_integral_constant_expression_p;
ad9ae192 8837 cp_token *id_expr_start_token;
34da8800 8838
8839 /* Look for the `decltype' token. */
640710cf 8840 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
34da8800 8841 return error_mark_node;
8842
8843 /* Types cannot be defined in a `decltype' expression. Save away the
8844 old message. */
8845 saved_message = parser->type_definition_forbidden_message;
8846
8847 /* And create the new one. */
8848 parser->type_definition_forbidden_message
2e52ac87 8849 = "types may not be defined in %<decltype%> expressions";
34da8800 8850
8851 /* The restrictions on constant-expressions do not apply inside
8852 decltype expressions. */
8853 saved_integral_constant_expression_p
8854 = parser->integral_constant_expression_p;
8855 saved_non_integral_constant_expression_p
8856 = parser->non_integral_constant_expression_p;
8857 parser->integral_constant_expression_p = false;
8858
8859 /* Do not actually evaluate the expression. */
8860 ++skip_evaluation;
8861
8862 /* Parse the opening `('. */
640710cf 8863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
4d3e6d58 8864 return error_mark_node;
34da8800 8865
8866 /* First, try parsing an id-expression. */
ad9ae192 8867 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
34da8800 8868 cp_parser_parse_tentatively (parser);
8869 expr = cp_parser_id_expression (parser,
8870 /*template_keyword_p=*/false,
8871 /*check_dependency_p=*/true,
8872 /*template_p=*/NULL,
8873 /*declarator_p=*/false,
8874 /*optional_p=*/false);
8875
8876 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8877 {
8878 bool non_integral_constant_expression_p = false;
8879 tree id_expression = expr;
8880 cp_id_kind idk;
8881 const char *error_msg;
8882
4d3e6d58 8883 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8884 /* Lookup the name we got back from the id-expression. */
8885 expr = cp_parser_lookup_name (parser, expr,
8886 none_type,
8887 /*is_template=*/false,
8888 /*is_namespace=*/false,
8889 /*check_dependency=*/true,
ad9ae192 8890 /*ambiguous_decls=*/NULL,
8891 id_expr_start_token->location);
4d3e6d58 8892
711178fb 8893 if (expr
34da8800 8894 && expr != error_mark_node
8895 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8896 && TREE_CODE (expr) != TYPE_DECL
711178fb 8897 && (TREE_CODE (expr) != BIT_NOT_EXPR
8898 || !TYPE_P (TREE_OPERAND (expr, 0)))
34da8800 8899 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8900 {
8901 /* Complete lookup of the id-expression. */
8902 expr = (finish_id_expression
8903 (id_expression, expr, parser->scope, &idk,
8904 /*integral_constant_expression_p=*/false,
8905 /*allow_non_integral_constant_expression_p=*/true,
8906 &non_integral_constant_expression_p,
8907 /*template_p=*/false,
8908 /*done=*/true,
8909 /*address_p=*/false,
8910 /*template_arg_p=*/false,
ad9ae192 8911 &error_msg,
8912 id_expr_start_token->location));
34da8800 8913
8914 if (expr == error_mark_node)
8915 /* We found an id-expression, but it was something that we
8916 should not have found. This is an error, not something
8917 we can recover from, so note that we found an
8918 id-expression and we'll recover as gracefully as
8919 possible. */
8920 id_expression_or_member_access_p = true;
8921 }
8922
8923 if (expr
8924 && expr != error_mark_node
8925 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8926 /* We have an id-expression. */
8927 id_expression_or_member_access_p = true;
8928 }
8929
8930 if (!id_expression_or_member_access_p)
8931 {
8932 /* Abort the id-expression parse. */
8933 cp_parser_abort_tentative_parse (parser);
8934
8935 /* Parsing tentatively, again. */
8936 cp_parser_parse_tentatively (parser);
8937
8938 /* Parse a class member access. */
8939 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8940 /*cast_p=*/false,
98b326fd 8941 /*member_access_only_p=*/true, NULL);
34da8800 8942
8943 if (expr
8944 && expr != error_mark_node
8945 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8946 /* We have an id-expression. */
8947 id_expression_or_member_access_p = true;
8948 }
8949
8950 if (id_expression_or_member_access_p)
8951 /* We have parsed the complete id-expression or member access. */
8952 cp_parser_parse_definitely (parser);
8953 else
8954 {
8955 /* Abort our attempt to parse an id-expression or member access
8956 expression. */
8957 cp_parser_abort_tentative_parse (parser);
8958
8959 /* Parse a full expression. */
98b326fd 8960 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
34da8800 8961 }
8962
8963 /* Go back to evaluating expressions. */
8964 --skip_evaluation;
8965
8966 /* Restore the old message and the integral constant expression
8967 flags. */
8968 parser->type_definition_forbidden_message = saved_message;
8969 parser->integral_constant_expression_p
8970 = saved_integral_constant_expression_p;
8971 parser->non_integral_constant_expression_p
8972 = saved_non_integral_constant_expression_p;
8973
8974 if (expr == error_mark_node)
8975 {
8976 /* Skip everything up to the closing `)'. */
8977 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8978 /*consume_paren=*/true);
8979 return error_mark_node;
8980 }
8981
8982 /* Parse to the closing `)'. */
640710cf 8983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
4e761042 8984 {
8985 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8986 /*consume_paren=*/true);
8987 return error_mark_node;
8988 }
34da8800 8989
8990 return finish_decltype_type (expr, id_expression_or_member_access_p);
8991}
8992
0a3b29ad 8993/* Special member functions [gram.special] */
8994
8995/* Parse a conversion-function-id.
8996
8997 conversion-function-id:
ccb84981 8998 operator conversion-type-id
0a3b29ad 8999
9000 Returns an IDENTIFIER_NODE representing the operator. */
9001
ccb84981 9002static tree
45baea8b 9003cp_parser_conversion_function_id (cp_parser* parser)
0a3b29ad 9004{
9005 tree type;
9006 tree saved_scope;
9007 tree saved_qualifying_scope;
9008 tree saved_object_scope;
7f602bca 9009 tree pushed_scope = NULL_TREE;
0a3b29ad 9010
9011 /* Look for the `operator' token. */
640710cf 9012 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
0a3b29ad 9013 return error_mark_node;
9014 /* When we parse the conversion-type-id, the current scope will be
9015 reset. However, we need that information in able to look up the
9016 conversion function later, so we save it here. */
9017 saved_scope = parser->scope;
9018 saved_qualifying_scope = parser->qualifying_scope;
9019 saved_object_scope = parser->object_scope;
9020 /* We must enter the scope of the class so that the names of
9021 entities declared within the class are available in the
9022 conversion-type-id. For example, consider:
9023
ccb84981 9024 struct S {
653e5405 9025 typedef int I;
0a3b29ad 9026 operator I();
9027 };
9028
9029 S::operator I() { ... }
9030
9031 In order to see that `I' is a type-name in the definition, we
9032 must be in the scope of `S'. */
9033 if (saved_scope)
7f602bca 9034 pushed_scope = push_scope (saved_scope);
0a3b29ad 9035 /* Parse the conversion-type-id. */
9036 type = cp_parser_conversion_type_id (parser);
9037 /* Leave the scope of the class, if any. */
7f602bca 9038 if (pushed_scope)
9039 pop_scope (pushed_scope);
0a3b29ad 9040 /* Restore the saved scope. */
9041 parser->scope = saved_scope;
9042 parser->qualifying_scope = saved_qualifying_scope;
9043 parser->object_scope = saved_object_scope;
9044 /* If the TYPE is invalid, indicate failure. */
9045 if (type == error_mark_node)
9046 return error_mark_node;
9047 return mangle_conv_op_name_for_type (type);
9048}
9049
9050/* Parse a conversion-type-id:
9051
9052 conversion-type-id:
9053 type-specifier-seq conversion-declarator [opt]
9054
9055 Returns the TYPE specified. */
9056
9057static tree
45baea8b 9058cp_parser_conversion_type_id (cp_parser* parser)
0a3b29ad 9059{
9060 tree attributes;
4b9b2871 9061 cp_decl_specifier_seq type_specifiers;
3046c0a3 9062 cp_declarator *declarator;
c37ff371 9063 tree type_specified;
0a3b29ad 9064
9065 /* Parse the attributes. */
9066 attributes = cp_parser_attributes_opt (parser);
9067 /* Parse the type-specifiers. */
6f74fe3c 9068 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9069 &type_specifiers);
0a3b29ad 9070 /* If that didn't work, stop. */
4b9b2871 9071 if (type_specifiers.type == error_mark_node)
0a3b29ad 9072 return error_mark_node;
9073 /* Parse the conversion-declarator. */
9074 declarator = cp_parser_conversion_declarator_opt (parser);
9075
c37ff371 9076 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
653e5405 9077 /*initialized=*/0, &attributes);
c37ff371 9078 if (attributes)
9079 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
ddcb49ca 9080
9081 /* Don't give this error when parsing tentatively. This happens to
9082 work because we always parse this definitively once. */
9083 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9084 && type_uses_auto (type_specified))
9085 {
9086 error ("invalid use of %<auto%> in conversion operator");
9087 return error_mark_node;
9088 }
9089
c37ff371 9090 return type_specified;
0a3b29ad 9091}
9092
9093/* Parse an (optional) conversion-declarator.
9094
9095 conversion-declarator:
ccb84981 9096 ptr-operator conversion-declarator [opt]
0a3b29ad 9097
3046c0a3 9098 */
0a3b29ad 9099
3046c0a3 9100static cp_declarator *
45baea8b 9101cp_parser_conversion_declarator_opt (cp_parser* parser)
0a3b29ad 9102{
9103 enum tree_code code;
9104 tree class_type;
2cfb6cde 9105 cp_cv_quals cv_quals;
0a3b29ad 9106
9107 /* We don't know if there's a ptr-operator next, or not. */
9108 cp_parser_parse_tentatively (parser);
9109 /* Try the ptr-operator. */
2cfb6cde 9110 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
0a3b29ad 9111 /* If it worked, look for more conversion-declarators. */
9112 if (cp_parser_parse_definitely (parser))
9113 {
3046c0a3 9114 cp_declarator *declarator;
207355ad 9115
3046c0a3 9116 /* Parse another optional declarator. */
9117 declarator = cp_parser_conversion_declarator_opt (parser);
207355ad 9118
63949b38 9119 return cp_parser_make_indirect_declarator
9120 (code, class_type, cv_quals, declarator);
0a3b29ad 9121 }
9122
3046c0a3 9123 return NULL;
0a3b29ad 9124}
9125
9126/* Parse an (optional) ctor-initializer.
9127
9128 ctor-initializer:
ccb84981 9129 : mem-initializer-list
0a3b29ad 9130
9131 Returns TRUE iff the ctor-initializer was actually present. */
9132
9133static bool
45baea8b 9134cp_parser_ctor_initializer_opt (cp_parser* parser)
0a3b29ad 9135{
9136 /* If the next token is not a `:', then there is no
9137 ctor-initializer. */
9138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9139 {
9140 /* Do default initialization of any bases and members. */
9141 if (DECL_CONSTRUCTOR_P (current_function_decl))
9142 finish_mem_initializers (NULL_TREE);
9143
9144 return false;
9145 }
9146
9147 /* Consume the `:' token. */
9148 cp_lexer_consume_token (parser->lexer);
9149 /* And the mem-initializer-list. */
9150 cp_parser_mem_initializer_list (parser);
9151
9152 return true;
9153}
9154
9155/* Parse a mem-initializer-list.
9156
9157 mem-initializer-list:
d95d815d 9158 mem-initializer ... [opt]
9159 mem-initializer ... [opt] , mem-initializer-list */
0a3b29ad 9160
9161static void
45baea8b 9162cp_parser_mem_initializer_list (cp_parser* parser)
0a3b29ad 9163{
9164 tree mem_initializer_list = NULL_TREE;
ad9ae192 9165 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 9166
9167 /* Let the semantic analysis code know that we are starting the
9168 mem-initializer-list. */
7e5ca199 9169 if (!DECL_CONSTRUCTOR_P (current_function_decl))
ad9ae192 9170 error ("%Honly constructors take base initializers",
9171 &token->location);
0a3b29ad 9172
9173 /* Loop through the list. */
9174 while (true)
9175 {
9176 tree mem_initializer;
9177
ad9ae192 9178 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 9179 /* Parse the mem-initializer. */
9180 mem_initializer = cp_parser_mem_initializer (parser);
d95d815d 9181 /* If the next token is a `...', we're expanding member initializers. */
9182 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9183 {
9184 /* Consume the `...'. */
9185 cp_lexer_consume_token (parser->lexer);
9186
9187 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9188 can be expanded but members cannot. */
9189 if (mem_initializer != error_mark_node
9190 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9191 {
ad9ae192 9192 error ("%Hcannot expand initializer for member %<%D%>",
9193 &token->location, TREE_PURPOSE (mem_initializer));
d95d815d 9194 mem_initializer = error_mark_node;
9195 }
9196
9197 /* Construct the pack expansion type. */
9198 if (mem_initializer != error_mark_node)
9199 mem_initializer = make_pack_expansion (mem_initializer);
9200 }
0a3b29ad 9201 /* Add it to the list, unless it was erroneous. */
7be1bc1f 9202 if (mem_initializer != error_mark_node)
0a3b29ad 9203 {
9204 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9205 mem_initializer_list = mem_initializer;
9206 }
9207 /* If the next token is not a `,', we're done. */
9208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9209 break;
9210 /* Consume the `,' token. */
9211 cp_lexer_consume_token (parser->lexer);
9212 }
9213
9214 /* Perform semantic analysis. */
7e5ca199 9215 if (DECL_CONSTRUCTOR_P (current_function_decl))
9216 finish_mem_initializers (mem_initializer_list);
0a3b29ad 9217}
9218
9219/* Parse a mem-initializer.
9220
9221 mem-initializer:
ccb84981 9222 mem-initializer-id ( expression-list [opt] )
f82f1250 9223 mem-initializer-id braced-init-list
0a3b29ad 9224
9225 GNU extension:
ccb84981 9226
0a3b29ad 9227 mem-initializer:
755edffd 9228 ( expression-list [opt] )
0a3b29ad 9229
9230 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9231 class) or FIELD_DECL (for a non-static data member) to initialize;
7be1bc1f 9232 the TREE_VALUE is the expression-list. An empty initialization
9233 list is represented by void_list_node. */
0a3b29ad 9234
9235static tree
45baea8b 9236cp_parser_mem_initializer (cp_parser* parser)
0a3b29ad 9237{
9238 tree mem_initializer_id;
9239 tree expression_list;
5f1653d2 9240 tree member;
ad9ae192 9241 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb84981 9242
0a3b29ad 9243 /* Find out what is being initialized. */
9244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9245 {
2b9e3597 9246 permerror (token->location,
9247 "anachronistic old-style base class initializer");
0a3b29ad 9248 mem_initializer_id = NULL_TREE;
9249 }
9250 else
642a3054 9251 {
9252 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9253 if (mem_initializer_id == error_mark_node)
9254 return mem_initializer_id;
9255 }
5f1653d2 9256 member = expand_member_init (mem_initializer_id);
9257 if (member && !DECL_P (member))
9258 in_base_initializer = 1;
0986fa22 9259
f82f1250 9260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9261 {
9262 bool expr_non_constant_p;
9263 maybe_warn_cpp0x ("extended initializer lists");
9264 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9265 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9266 expression_list = build_tree_list (NULL_TREE, expression_list);
9267 }
9268 else
f352a3fb 9269 {
9270 VEC(tree,gc)* vec;
9271 vec = cp_parser_parenthesized_expression_list (parser, false,
9272 /*cast_p=*/false,
9273 /*allow_expansion_p=*/true,
9274 /*non_constant_p=*/NULL);
9275 if (vec == NULL)
9276 return error_mark_node;
9277 expression_list = build_tree_list_vec (vec);
9278 release_tree_vector (vec);
9279 }
9280
7be1bc1f 9281 if (expression_list == error_mark_node)
9282 return error_mark_node;
0986fa22 9283 if (!expression_list)
0a3b29ad 9284 expression_list = void_type_node;
0a3b29ad 9285
5f1653d2 9286 in_base_initializer = 0;
ccb84981 9287
7be1bc1f 9288 return member ? build_tree_list (member, expression_list) : error_mark_node;
0a3b29ad 9289}
9290
9291/* Parse a mem-initializer-id.
9292
9293 mem-initializer-id:
9294 :: [opt] nested-name-specifier [opt] class-name
ccb84981 9295 identifier
0a3b29ad 9296
9297 Returns a TYPE indicating the class to be initializer for the first
9298 production. Returns an IDENTIFIER_NODE indicating the data member
9299 to be initialized for the second production. */
9300
9301static tree
45baea8b 9302cp_parser_mem_initializer_id (cp_parser* parser)
0a3b29ad 9303{
9304 bool global_scope_p;
9305 bool nested_name_specifier_p;
0b1957b6 9306 bool template_p = false;
0a3b29ad 9307 tree id;
9308
ad9ae192 9309 cp_token *token = cp_lexer_peek_token (parser->lexer);
9310
0b1957b6 9311 /* `typename' is not allowed in this context ([temp.res]). */
9312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9313 {
ad9ae192 9314 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9315 "member initializer is implicitly a type)",
9316 &token->location);
0b1957b6 9317 cp_lexer_consume_token (parser->lexer);
9318 }
0a3b29ad 9319 /* Look for the optional `::' operator. */
ccb84981 9320 global_scope_p
9321 = (cp_parser_global_scope_opt (parser,
130bb1d4 9322 /*current_scope_valid_p=*/false)
0a3b29ad 9323 != NULL_TREE);
9324 /* Look for the optional nested-name-specifier. The simplest way to
9325 implement:
9326
9327 [temp.res]
9328
9329 The keyword `typename' is not permitted in a base-specifier or
9330 mem-initializer; in these contexts a qualified name that
9331 depends on a template-parameter is implicitly assumed to be a
9332 type name.
9333
9334 is to assume that we have seen the `typename' keyword at this
9335 point. */
ccb84981 9336 nested_name_specifier_p
0a3b29ad 9337 = (cp_parser_nested_name_specifier_opt (parser,
9338 /*typename_keyword_p=*/true,
9339 /*check_dependency_p=*/true,
3d0f901b 9340 /*type_p=*/true,
9341 /*is_declaration=*/true)
0a3b29ad 9342 != NULL_TREE);
0b1957b6 9343 if (nested_name_specifier_p)
9344 template_p = cp_parser_optional_template_keyword (parser);
0a3b29ad 9345 /* If there is a `::' operator or a nested-name-specifier, then we
9346 are definitely looking for a class-name. */
9347 if (global_scope_p || nested_name_specifier_p)
9348 return cp_parser_class_name (parser,
9349 /*typename_keyword_p=*/true,
0b1957b6 9350 /*template_keyword_p=*/template_p,
e2ae55f2 9351 none_type,
0a3b29ad 9352 /*check_dependency_p=*/true,
3d0f901b 9353 /*class_head_p=*/false,
9354 /*is_declaration=*/true);
0a3b29ad 9355 /* Otherwise, we could also be looking for an ordinary identifier. */
9356 cp_parser_parse_tentatively (parser);
9357 /* Try a class-name. */
ccb84981 9358 id = cp_parser_class_name (parser,
0a3b29ad 9359 /*typename_keyword_p=*/true,
9360 /*template_keyword_p=*/false,
e2ae55f2 9361 none_type,
0a3b29ad 9362 /*check_dependency_p=*/true,
3d0f901b 9363 /*class_head_p=*/false,
9364 /*is_declaration=*/true);
0a3b29ad 9365 /* If we found one, we're done. */
9366 if (cp_parser_parse_definitely (parser))
9367 return id;
9368 /* Otherwise, look for an ordinary identifier. */
9369 return cp_parser_identifier (parser);
9370}
9371
9372/* Overloading [gram.over] */
9373
9374/* Parse an operator-function-id.
9375
9376 operator-function-id:
ccb84981 9377 operator operator
0a3b29ad 9378
9379 Returns an IDENTIFIER_NODE for the operator which is a
9380 human-readable spelling of the identifier, e.g., `operator +'. */
9381
ccb84981 9382static tree
45baea8b 9383cp_parser_operator_function_id (cp_parser* parser)
0a3b29ad 9384{
9385 /* Look for the `operator' keyword. */
640710cf 9386 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
0a3b29ad 9387 return error_mark_node;
9388 /* And then the name of the operator itself. */
9389 return cp_parser_operator (parser);
9390}
9391
9392/* Parse an operator.
9393
9394 operator:
9395 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9396 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9397 || ++ -- , ->* -> () []
9398
9399 GNU Extensions:
ccb84981 9400
0a3b29ad 9401 operator:
9402 <? >? <?= >?=
9403
9404 Returns an IDENTIFIER_NODE for the operator which is a
9405 human-readable spelling of the identifier, e.g., `operator +'. */
ccb84981 9406
0a3b29ad 9407static tree
45baea8b 9408cp_parser_operator (cp_parser* parser)
0a3b29ad 9409{
9410 tree id = NULL_TREE;
9411 cp_token *token;
9412
9413 /* Peek at the next token. */
9414 token = cp_lexer_peek_token (parser->lexer);
9415 /* Figure out which operator we have. */
9416 switch (token->type)
9417 {
9418 case CPP_KEYWORD:
9419 {
9420 enum tree_code op;
9421
9422 /* The keyword should be either `new' or `delete'. */
9423 if (token->keyword == RID_NEW)
9424 op = NEW_EXPR;
9425 else if (token->keyword == RID_DELETE)
9426 op = DELETE_EXPR;
9427 else
9428 break;
9429
9430 /* Consume the `new' or `delete' token. */
9431 cp_lexer_consume_token (parser->lexer);
9432
9433 /* Peek at the next token. */
9434 token = cp_lexer_peek_token (parser->lexer);
9435 /* If it's a `[' token then this is the array variant of the
9436 operator. */
9437 if (token->type == CPP_OPEN_SQUARE)
9438 {
9439 /* Consume the `[' token. */
9440 cp_lexer_consume_token (parser->lexer);
9441 /* Look for the `]' token. */
640710cf 9442 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
ccb84981 9443 id = ansi_opname (op == NEW_EXPR
0a3b29ad 9444 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9445 }
9446 /* Otherwise, we have the non-array variant. */
9447 else
9448 id = ansi_opname (op);
9449
9450 return id;
9451 }
9452
9453 case CPP_PLUS:
9454 id = ansi_opname (PLUS_EXPR);
9455 break;
9456
9457 case CPP_MINUS:
9458 id = ansi_opname (MINUS_EXPR);
9459 break;
9460
9461 case CPP_MULT:
9462 id = ansi_opname (MULT_EXPR);
9463 break;
9464
9465 case CPP_DIV:
9466 id = ansi_opname (TRUNC_DIV_EXPR);
9467 break;
9468
9469 case CPP_MOD:
9470 id = ansi_opname (TRUNC_MOD_EXPR);
9471 break;
9472
9473 case CPP_XOR:
9474 id = ansi_opname (BIT_XOR_EXPR);
9475 break;
9476
9477 case CPP_AND:
9478 id = ansi_opname (BIT_AND_EXPR);
9479 break;
9480
9481 case CPP_OR:
9482 id = ansi_opname (BIT_IOR_EXPR);
9483 break;
9484
9485 case CPP_COMPL:
9486 id = ansi_opname (BIT_NOT_EXPR);
9487 break;
ccb84981 9488
0a3b29ad 9489 case CPP_NOT:
9490 id = ansi_opname (TRUTH_NOT_EXPR);
9491 break;
9492
9493 case CPP_EQ:
9494 id = ansi_assopname (NOP_EXPR);
9495 break;
9496
9497 case CPP_LESS:
9498 id = ansi_opname (LT_EXPR);
9499 break;
9500
9501 case CPP_GREATER:
9502 id = ansi_opname (GT_EXPR);
9503 break;
9504
9505 case CPP_PLUS_EQ:
9506 id = ansi_assopname (PLUS_EXPR);
9507 break;
9508
9509 case CPP_MINUS_EQ:
9510 id = ansi_assopname (MINUS_EXPR);
9511 break;
9512
9513 case CPP_MULT_EQ:
9514 id = ansi_assopname (MULT_EXPR);
9515 break;
9516
9517 case CPP_DIV_EQ:
9518 id = ansi_assopname (TRUNC_DIV_EXPR);
9519 break;
9520
9521 case CPP_MOD_EQ:
9522 id = ansi_assopname (TRUNC_MOD_EXPR);
9523 break;
9524
9525 case CPP_XOR_EQ:
9526 id = ansi_assopname (BIT_XOR_EXPR);
9527 break;
9528
9529 case CPP_AND_EQ:
9530 id = ansi_assopname (BIT_AND_EXPR);
9531 break;
9532
9533 case CPP_OR_EQ:
9534 id = ansi_assopname (BIT_IOR_EXPR);
9535 break;
9536
9537 case CPP_LSHIFT:
9538 id = ansi_opname (LSHIFT_EXPR);
9539 break;
9540
9541 case CPP_RSHIFT:
9542 id = ansi_opname (RSHIFT_EXPR);
9543 break;
9544
9545 case CPP_LSHIFT_EQ:
9546 id = ansi_assopname (LSHIFT_EXPR);
9547 break;
9548
9549 case CPP_RSHIFT_EQ:
9550 id = ansi_assopname (RSHIFT_EXPR);
9551 break;
9552
9553 case CPP_EQ_EQ:
9554 id = ansi_opname (EQ_EXPR);
9555 break;
9556
9557 case CPP_NOT_EQ:
9558 id = ansi_opname (NE_EXPR);
9559 break;
9560
9561 case CPP_LESS_EQ:
9562 id = ansi_opname (LE_EXPR);
9563 break;
9564
9565 case CPP_GREATER_EQ:
9566 id = ansi_opname (GE_EXPR);
9567 break;
9568
9569 case CPP_AND_AND:
9570 id = ansi_opname (TRUTH_ANDIF_EXPR);
9571 break;
9572
9573 case CPP_OR_OR:
9574 id = ansi_opname (TRUTH_ORIF_EXPR);
9575 break;
ccb84981 9576
0a3b29ad 9577 case CPP_PLUS_PLUS:
9578 id = ansi_opname (POSTINCREMENT_EXPR);
9579 break;
9580
9581 case CPP_MINUS_MINUS:
9582 id = ansi_opname (PREDECREMENT_EXPR);
9583 break;
9584
9585 case CPP_COMMA:
9586 id = ansi_opname (COMPOUND_EXPR);
9587 break;
9588
9589 case CPP_DEREF_STAR:
9590 id = ansi_opname (MEMBER_REF);
9591 break;
9592
9593 case CPP_DEREF:
9594 id = ansi_opname (COMPONENT_REF);
9595 break;
9596
9597 case CPP_OPEN_PAREN:
9598 /* Consume the `('. */
9599 cp_lexer_consume_token (parser->lexer);
9600 /* Look for the matching `)'. */
640710cf 9601 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 9602 return ansi_opname (CALL_EXPR);
9603
9604 case CPP_OPEN_SQUARE:
9605 /* Consume the `['. */
9606 cp_lexer_consume_token (parser->lexer);
9607 /* Look for the matching `]'. */
640710cf 9608 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
0a3b29ad 9609 return ansi_opname (ARRAY_REF);
9610
0a3b29ad 9611 default:
9612 /* Anything else is an error. */
9613 break;
9614 }
9615
9616 /* If we have selected an identifier, we need to consume the
9617 operator token. */
9618 if (id)
9619 cp_lexer_consume_token (parser->lexer);
9620 /* Otherwise, no valid operator name was present. */
9621 else
9622 {
9623 cp_parser_error (parser, "expected operator");
9624 id = error_mark_node;
9625 }
9626
9627 return id;
9628}
9629
9630/* Parse a template-declaration.
9631
9632 template-declaration:
ccb84981 9633 export [opt] template < template-parameter-list > declaration
0a3b29ad 9634
9635 If MEMBER_P is TRUE, this template-declaration occurs within a
ccb84981 9636 class-specifier.
0a3b29ad 9637
9638 The grammar rule given by the standard isn't correct. What
9639 is really meant is:
9640
9641 template-declaration:
ccb84981 9642 export [opt] template-parameter-list-seq
0a3b29ad 9643 decl-specifier-seq [opt] init-declarator [opt] ;
ccb84981 9644 export [opt] template-parameter-list-seq
0a3b29ad 9645 function-definition
9646
9647 template-parameter-list-seq:
9648 template-parameter-list-seq [opt]
9649 template < template-parameter-list > */
9650
9651static void
45baea8b 9652cp_parser_template_declaration (cp_parser* parser, bool member_p)
0a3b29ad 9653{
9654 /* Check for `export'. */
9655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9656 {
9657 /* Consume the `export' token. */
9658 cp_lexer_consume_token (parser->lexer);
9659 /* Warn that we do not support `export'. */
c3ceba8e 9660 warning (0, "keyword %<export%> not implemented, and will be ignored");
0a3b29ad 9661 }
9662
9663 cp_parser_template_declaration_after_export (parser, member_p);
9664}
9665
9666/* Parse a template-parameter-list.
9667
9668 template-parameter-list:
9669 template-parameter
9670 template-parameter-list , template-parameter
9671
9672 Returns a TREE_LIST. Each node represents a template parameter.
9673 The nodes are connected via their TREE_CHAINs. */
9674
9675static tree
45baea8b 9676cp_parser_template_parameter_list (cp_parser* parser)
0a3b29ad 9677{
9678 tree parameter_list = NULL_TREE;
9679
7be1bc1f 9680 begin_template_parm_list ();
0a3b29ad 9681 while (true)
9682 {
9683 tree parameter;
3046c0a3 9684 bool is_non_type;
d95d815d 9685 bool is_parameter_pack;
e60a6f7b 9686 location_t parm_loc;
0a3b29ad 9687
9688 /* Parse the template-parameter. */
e60a6f7b 9689 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
d95d815d 9690 parameter = cp_parser_template_parameter (parser,
9691 &is_non_type,
9692 &is_parameter_pack);
0a3b29ad 9693 /* Add it to the list. */
8f776a97 9694 if (parameter != error_mark_node)
9695 parameter_list = process_template_parm (parameter_list,
e60a6f7b 9696 parm_loc,
8f776a97 9697 parameter,
d95d815d 9698 is_non_type,
9699 is_parameter_pack);
e347c61e 9700 else
9701 {
9702 tree err_parm = build_tree_list (parameter, parameter);
9703 TREE_VALUE (err_parm) = error_mark_node;
9704 parameter_list = chainon (parameter_list, err_parm);
9705 }
9706
f5b66d53 9707 /* If the next token is not a `,', we're done. */
9708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 9709 break;
9710 /* Otherwise, consume the `,' token. */
9711 cp_lexer_consume_token (parser->lexer);
9712 }
9713
7be1bc1f 9714 return end_template_parm_list (parameter_list);
0a3b29ad 9715}
9716
9717/* Parse a template-parameter.
9718
9719 template-parameter:
9720 type-parameter
9721 parameter-declaration
9722
8f776a97 9723 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9724 the parameter. The TREE_PURPOSE is the default value, if any.
9725 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
d95d815d 9726 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9727 set to true iff this parameter is a parameter pack. */
0a3b29ad 9728
9729static tree
d95d815d 9730cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9731 bool *is_parameter_pack)
0a3b29ad 9732{
9733 cp_token *token;
4b9b2871 9734 cp_parameter_declarator *parameter_declarator;
4efde0d3 9735 cp_declarator *id_declarator;
8f776a97 9736 tree parm;
0a3b29ad 9737
3046c0a3 9738 /* Assume it is a type parameter or a template parameter. */
9739 *is_non_type = false;
d95d815d 9740 /* Assume it not a parameter pack. */
9741 *is_parameter_pack = false;
0a3b29ad 9742 /* Peek at the next token. */
9743 token = cp_lexer_peek_token (parser->lexer);
9744 /* If it is `class' or `template', we have a type-parameter. */
9745 if (token->keyword == RID_TEMPLATE)
d95d815d 9746 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 9747 /* If it is `class' or `typename' we do not know yet whether it is a
9748 type parameter or a non-type parameter. Consider:
9749
9750 template <typename T, typename T::X X> ...
9751
9752 or:
ccb84981 9753
0a3b29ad 9754 template <class C, class D*> ...
9755
9756 Here, the first parameter is a type parameter, and the second is
9757 a non-type parameter. We can tell by looking at the token after
9758 the identifier -- if it is a `,', `=', or `>' then we have a type
9759 parameter. */
9760 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9761 {
9762 /* Peek at the token after `class' or `typename'. */
9763 token = cp_lexer_peek_nth_token (parser->lexer, 2);
d95d815d 9764 /* If it's an ellipsis, we have a template type parameter
9765 pack. */
9766 if (token->type == CPP_ELLIPSIS)
9767 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 9768 /* If it's an identifier, skip it. */
9769 if (token->type == CPP_NAME)
9770 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9771 /* Now, see if the token looks like the end of a template
9772 parameter. */
ccb84981 9773 if (token->type == CPP_COMMA
0a3b29ad 9774 || token->type == CPP_EQ
9775 || token->type == CPP_GREATER)
d95d815d 9776 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 9777 }
9778
ccb84981 9779 /* Otherwise, it is a non-type parameter.
0a3b29ad 9780
9781 [temp.param]
9782
9783 When parsing a default template-argument for a non-type
9784 template-parameter, the first non-nested `>' is taken as the end
9785 of the template parameter-list rather than a greater-than
9786 operator. */
3046c0a3 9787 *is_non_type = true;
9788 parameter_declarator
9789 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9790 /*parenthesized_p=*/NULL);
d95d815d 9791
9792 /* If the parameter declaration is marked as a parameter pack, set
9793 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9794 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9795 grokdeclarator. */
9796 if (parameter_declarator
9797 && parameter_declarator->declarator
9798 && parameter_declarator->declarator->parameter_pack_p)
9799 {
9800 *is_parameter_pack = true;
9801 parameter_declarator->declarator->parameter_pack_p = false;
9802 }
9803
9804 /* If the next token is an ellipsis, and we don't already have it
9805 marked as a parameter pack, then we have a parameter pack (that
41341abd 9806 has no declarator). */
d95d815d 9807 if (!*is_parameter_pack
2aedc2ff 9808 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9809 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
d95d815d 9810 {
41341abd 9811 /* Consume the `...'. */
d95d815d 9812 cp_lexer_consume_token (parser->lexer);
9813 maybe_warn_variadic_templates ();
9814
9815 *is_parameter_pack = true;
83b01f73 9816 }
9817 /* We might end up with a pack expansion as the type of the non-type
9818 template parameter, in which case this is a non-type template
9819 parameter pack. */
9820 else if (parameter_declarator
9821 && parameter_declarator->decl_specifiers.type
9822 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9823 {
9824 *is_parameter_pack = true;
9825 parameter_declarator->decl_specifiers.type =
9826 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9827 }
41341abd 9828
83b01f73 9829 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9830 {
41341abd 9831 /* Parameter packs cannot have default arguments. However, a
9832 user may try to do so, so we'll parse them and give an
9833 appropriate diagnostic here. */
41341abd 9834
83b01f73 9835 /* Consume the `='. */
ad9ae192 9836 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
83b01f73 9837 cp_lexer_consume_token (parser->lexer);
9838
9839 /* Find the name of the parameter pack. */
9840 id_declarator = parameter_declarator->declarator;
9841 while (id_declarator && id_declarator->kind != cdk_id)
9842 id_declarator = id_declarator->declarator;
9843
9844 if (id_declarator && id_declarator->kind == cdk_id)
ad9ae192 9845 error ("%Htemplate parameter pack %qD cannot have a default argument",
9846 &start_token->location, id_declarator->u.id.unqualified_name);
83b01f73 9847 else
ad9ae192 9848 error ("%Htemplate parameter pack cannot have a default argument",
9849 &start_token->location);
83b01f73 9850
9851 /* Parse the default argument, but throw away the result. */
9852 cp_parser_default_argument (parser, /*template_parm_p=*/true);
d95d815d 9853 }
9854
8f776a97 9855 parm = grokdeclarator (parameter_declarator->declarator,
9856 &parameter_declarator->decl_specifiers,
9857 PARM, /*initialized=*/0,
9858 /*attrlist=*/NULL);
9859 if (parm == error_mark_node)
9860 return error_mark_node;
d95d815d 9861
8f776a97 9862 return build_tree_list (parameter_declarator->default_argument, parm);
0a3b29ad 9863}
9864
9865/* Parse a type-parameter.
9866
9867 type-parameter:
9868 class identifier [opt]
9869 class identifier [opt] = type-id
9870 typename identifier [opt]
9871 typename identifier [opt] = type-id
9872 template < template-parameter-list > class identifier [opt]
ccb84981 9873 template < template-parameter-list > class identifier [opt]
9874 = id-expression
0a3b29ad 9875
d95d815d 9876 GNU Extension (variadic templates):
9877
9878 type-parameter:
9879 class ... identifier [opt]
9880 typename ... identifier [opt]
9881
0a3b29ad 9882 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9883 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
d95d815d 9884 the declaration of the parameter.
9885
9886 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
0a3b29ad 9887
9888static tree
d95d815d 9889cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
0a3b29ad 9890{
9891 cp_token *token;
9892 tree parameter;
9893
9894 /* Look for a keyword to tell us what kind of parameter this is. */
ccb84981 9895 token = cp_parser_require (parser, CPP_KEYWORD,
640710cf 9896 "%<class%>, %<typename%>, or %<template%>");
0a3b29ad 9897 if (!token)
9898 return error_mark_node;
9899
9900 switch (token->keyword)
9901 {
9902 case RID_CLASS:
9903 case RID_TYPENAME:
9904 {
9905 tree identifier;
9906 tree default_argument;
9907
d95d815d 9908 /* If the next token is an ellipsis, we have a template
9909 argument pack. */
9910 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9911 {
9912 /* Consume the `...' token. */
9913 cp_lexer_consume_token (parser->lexer);
9914 maybe_warn_variadic_templates ();
9915
9916 *is_parameter_pack = true;
9917 }
9918
0a3b29ad 9919 /* If the next token is an identifier, then it names the
653e5405 9920 parameter. */
0a3b29ad 9921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9922 identifier = cp_parser_identifier (parser);
9923 else
9924 identifier = NULL_TREE;
9925
9926 /* Create the parameter. */
9927 parameter = finish_template_type_parm (class_type_node, identifier);
9928
9929 /* If the next token is an `=', we have a default argument. */
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9931 {
9932 /* Consume the `=' token. */
9933 cp_lexer_consume_token (parser->lexer);
755edffd 9934 /* Parse the default-argument. */
23010bc8 9935 push_deferring_access_checks (dk_no_deferred);
0a3b29ad 9936 default_argument = cp_parser_type_id (parser);
d95d815d 9937
9938 /* Template parameter packs cannot have default
9939 arguments. */
9940 if (*is_parameter_pack)
9941 {
9942 if (identifier)
ad9ae192 9943 error ("%Htemplate parameter pack %qD cannot have a "
9944 "default argument", &token->location, identifier);
d95d815d 9945 else
ad9ae192 9946 error ("%Htemplate parameter packs cannot have "
9947 "default arguments", &token->location);
d95d815d 9948 default_argument = NULL_TREE;
9949 }
23010bc8 9950 pop_deferring_access_checks ();
0a3b29ad 9951 }
9952 else
9953 default_argument = NULL_TREE;
9954
9955 /* Create the combined representation of the parameter and the
9956 default argument. */
816786ad 9957 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 9958 }
9959 break;
9960
9961 case RID_TEMPLATE:
9962 {
9963 tree parameter_list;
9964 tree identifier;
9965 tree default_argument;
9966
9967 /* Look for the `<'. */
640710cf 9968 cp_parser_require (parser, CPP_LESS, "%<<%>");
0a3b29ad 9969 /* Parse the template-parameter-list. */
7be1bc1f 9970 parameter_list = cp_parser_template_parameter_list (parser);
0a3b29ad 9971 /* Look for the `>'. */
640710cf 9972 cp_parser_require (parser, CPP_GREATER, "%<>%>");
0a3b29ad 9973 /* Look for the `class' keyword. */
640710cf 9974 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
d95d815d 9975 /* If the next token is an ellipsis, we have a template
9976 argument pack. */
9977 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9978 {
9979 /* Consume the `...' token. */
9980 cp_lexer_consume_token (parser->lexer);
9981 maybe_warn_variadic_templates ();
9982
9983 *is_parameter_pack = true;
9984 }
0a3b29ad 9985 /* If the next token is an `=', then there is a
9986 default-argument. If the next token is a `>', we are at
9987 the end of the parameter-list. If the next token is a `,',
9988 then we are at the end of this parameter. */
9989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9990 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9991 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
2a03dcc3 9992 {
9993 identifier = cp_parser_identifier (parser);
93523877 9994 /* Treat invalid names as if the parameter were nameless. */
2a03dcc3 9995 if (identifier == error_mark_node)
9996 identifier = NULL_TREE;
9997 }
0a3b29ad 9998 else
9999 identifier = NULL_TREE;
2a03dcc3 10000
0a3b29ad 10001 /* Create the template parameter. */
10002 parameter = finish_template_template_parm (class_type_node,
10003 identifier);
ccb84981 10004
0a3b29ad 10005 /* If the next token is an `=', then there is a
10006 default-argument. */
10007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10008 {
c3b9e457 10009 bool is_template;
10010
0a3b29ad 10011 /* Consume the `='. */
10012 cp_lexer_consume_token (parser->lexer);
10013 /* Parse the id-expression. */
23010bc8 10014 push_deferring_access_checks (dk_no_deferred);
ad9ae192 10015 /* save token before parsing the id-expression, for error
10016 reporting */
10017 token = cp_lexer_peek_token (parser->lexer);
ccb84981 10018 default_argument
0a3b29ad 10019 = cp_parser_id_expression (parser,
10020 /*template_keyword_p=*/false,
10021 /*check_dependency_p=*/true,
c3b9e457 10022 /*template_p=*/&is_template,
197c9df7 10023 /*declarator_p=*/false,
130bb1d4 10024 /*optional_p=*/false);
eae805b4 10025 if (TREE_CODE (default_argument) == TYPE_DECL)
10026 /* If the id-expression was a template-id that refers to
10027 a template-class, we already have the declaration here,
10028 so no further lookup is needed. */
10029 ;
10030 else
10031 /* Look up the name. */
ccb84981 10032 default_argument
eae805b4 10033 = cp_parser_lookup_name (parser, default_argument,
e2ae55f2 10034 none_type,
10035 /*is_template=*/is_template,
10036 /*is_namespace=*/false,
10037 /*check_dependency=*/true,
ad9ae192 10038 /*ambiguous_decls=*/NULL,
10039 token->location);
0a3b29ad 10040 /* See if the default argument is valid. */
10041 default_argument
10042 = check_template_template_default_arg (default_argument);
d95d815d 10043
10044 /* Template parameter packs cannot have default
10045 arguments. */
10046 if (*is_parameter_pack)
10047 {
10048 if (identifier)
ad9ae192 10049 error ("%Htemplate parameter pack %qD cannot "
10050 "have a default argument",
10051 &token->location, identifier);
d95d815d 10052 else
ad9ae192 10053 error ("%Htemplate parameter packs cannot "
10054 "have default arguments",
10055 &token->location);
d95d815d 10056 default_argument = NULL_TREE;
10057 }
23010bc8 10058 pop_deferring_access_checks ();
0a3b29ad 10059 }
10060 else
10061 default_argument = NULL_TREE;
10062
10063 /* Create the combined representation of the parameter and the
10064 default argument. */
2a03dcc3 10065 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 10066 }
10067 break;
10068
10069 default:
2a03dcc3 10070 gcc_unreachable ();
10071 break;
0a3b29ad 10072 }
ccb84981 10073
0a3b29ad 10074 return parameter;
10075}
10076
10077/* Parse a template-id.
10078
10079 template-id:
10080 template-name < template-argument-list [opt] >
10081
10082 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10083 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10084 returned. Otherwise, if the template-name names a function, or set
10085 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
ccb84981 10086 names a class, returns a TYPE_DECL for the specialization.
0a3b29ad 10087
10088 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10089 uninstantiated templates. */
10090
10091static tree
ccb84981 10092cp_parser_template_id (cp_parser *parser,
10093 bool template_keyword_p,
3d0f901b 10094 bool check_dependency_p,
10095 bool is_declaration)
0a3b29ad 10096{
3369eb76 10097 int i;
607a5d68 10098 tree templ;
0a3b29ad 10099 tree arguments;
0a3b29ad 10100 tree template_id;
19273cc2 10101 cp_token_position start_of_id = 0;
3369eb76 10102 deferred_access_check *chk;
10103 VEC (deferred_access_check,gc) *access_check;
ad9ae192 10104 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
3d0f901b 10105 bool is_identifier;
0a3b29ad 10106
10107 /* If the next token corresponds to a template-id, there is no need
10108 to reparse it. */
b3c48b5d 10109 next_token = cp_lexer_peek_token (parser->lexer);
10110 if (next_token->type == CPP_TEMPLATE_ID)
0a3b29ad 10111 {
3369eb76 10112 struct tree_check *check_value;
0a3b29ad 10113
10114 /* Get the stored value. */
3369eb76 10115 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
0a3b29ad 10116 /* Perform any access checks that were deferred. */
3369eb76 10117 access_check = check_value->checks;
10118 if (access_check)
10119 {
10120 for (i = 0 ;
10121 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10122 ++i)
10123 {
10124 perform_or_defer_access_check (chk->binfo,
10125 chk->decl,
10126 chk->diag_decl);
10127 }
10128 }
0a3b29ad 10129 /* Return the stored value. */
3369eb76 10130 return check_value->value;
0a3b29ad 10131 }
10132
b3c48b5d 10133 /* Avoid performing name lookup if there is no possibility of
10134 finding a template-id. */
10135 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10136 || (next_token->type == CPP_NAME
ccb84981 10137 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 10138 (parser, 2)))
b3c48b5d 10139 {
10140 cp_parser_error (parser, "expected template-id");
10141 return error_mark_node;
10142 }
10143
0a3b29ad 10144 /* Remember where the template-id starts. */
efcbcf83 10145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 10146 start_of_id = cp_lexer_token_position (parser->lexer, false);
0a3b29ad 10147
4f62c42e 10148 push_deferring_access_checks (dk_deferred);
9b57b06b 10149
0a3b29ad 10150 /* Parse the template-name. */
3d0f901b 10151 is_identifier = false;
ad9ae192 10152 token = cp_lexer_peek_token (parser->lexer);
607a5d68 10153 templ = cp_parser_template_name (parser, template_keyword_p,
10154 check_dependency_p,
10155 is_declaration,
10156 &is_identifier);
10157 if (templ == error_mark_node || is_identifier)
9b57b06b 10158 {
10159 pop_deferring_access_checks ();
607a5d68 10160 return templ;
9b57b06b 10161 }
0a3b29ad 10162
ccb84981 10163 /* If we find the sequence `[:' after a template-name, it's probably
c8d5ab79 10164 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10165 parse correctly the argument list. */
b9dd3954 10166 next_token = cp_lexer_peek_token (parser->lexer);
c8d5ab79 10167 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
ccb84981 10168 if (next_token->type == CPP_OPEN_SQUARE
c8d5ab79 10169 && next_token->flags & DIGRAPH
ccb84981 10170 && next_token_2->type == CPP_COLON
c8d5ab79 10171 && !(next_token_2->flags & PREV_WHITE))
9b57b06b 10172 {
c8d5ab79 10173 cp_parser_parse_tentatively (parser);
10174 /* Change `:' into `::'. */
10175 next_token_2->type = CPP_SCOPE;
10176 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
653e5405 10177 CPP_LESS. */
c8d5ab79 10178 cp_lexer_consume_token (parser->lexer);
ad9ae192 10179
c8d5ab79 10180 /* Parse the arguments. */
10181 arguments = cp_parser_enclosed_template_argument_list (parser);
10182 if (!cp_parser_parse_definitely (parser))
10183 {
10184 /* If we couldn't parse an argument list, then we revert our changes
10185 and return simply an error. Maybe this is not a template-id
10186 after all. */
10187 next_token_2->type = CPP_COLON;
a2c5b975 10188 cp_parser_error (parser, "expected %<<%>");
c8d5ab79 10189 pop_deferring_access_checks ();
10190 return error_mark_node;
10191 }
10192 /* Otherwise, emit an error about the invalid digraph, but continue
653e5405 10193 parsing because we got our argument list. */
2b9e3597 10194 if (permerror (next_token->location,
10195 "%<<::%> cannot begin a template-argument list"))
a52d5726 10196 {
10197 static bool hint = false;
5bcc316e 10198 inform (next_token->location,
10199 "%<<:%> is an alternate spelling for %<[%>."
10200 " Insert whitespace between %<<%> and %<::%>");
a52d5726 10201 if (!hint && !flag_permissive)
c8d5ab79 10202 {
5bcc316e 10203 inform (next_token->location, "(if you use %<-fpermissive%>"
10204 " G++ will accept your code)");
c8d5ab79 10205 hint = true;
10206 }
10207 }
10208 }
10209 else
10210 {
10211 /* Look for the `<' that starts the template-argument-list. */
640710cf 10212 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
c8d5ab79 10213 {
10214 pop_deferring_access_checks ();
10215 return error_mark_node;
10216 }
10217 /* Parse the arguments. */
10218 arguments = cp_parser_enclosed_template_argument_list (parser);
9b57b06b 10219 }
0a3b29ad 10220
10221 /* Build a representation of the specialization. */
607a5d68 10222 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10223 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10224 else if (DECL_CLASS_TEMPLATE_P (templ)
10225 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
ba0c587d 10226 {
10227 bool entering_scope;
10228 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10229 template (rather than some instantiation thereof) only if
10230 is not nested within some other construct. For example, in
10231 "template <typename T> void f(T) { A<T>::", A<T> is just an
10232 instantiation of A. */
10233 entering_scope = (template_parm_scope_p ()
10234 && cp_lexer_next_token_is (parser->lexer,
10235 CPP_SCOPE));
10236 template_id
607a5d68 10237 = finish_template_type (templ, arguments, entering_scope);
ba0c587d 10238 }
0a3b29ad 10239 else
10240 {
10241 /* If it's not a class-template or a template-template, it should be
10242 a function-template. */
607a5d68 10243 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10244 || TREE_CODE (templ) == OVERLOAD
10245 || BASELINK_P (templ)));
ccb84981 10246
607a5d68 10247 template_id = lookup_template_function (templ, arguments);
0a3b29ad 10248 }
ccb84981 10249
0a3b29ad 10250 /* If parsing tentatively, replace the sequence of tokens that makes
10251 up the template-id with a CPP_TEMPLATE_ID token. That way,
10252 should we re-parse the token stream, we will not have to repeat
10253 the effort required to do the parse, nor will we issue duplicate
10254 error messages about problems during instantiation of the
459742a6 10255 template. */
67635103 10256 if (start_of_id)
0a3b29ad 10257 {
19273cc2 10258 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9031d10b 10259
0a3b29ad 10260 /* Reset the contents of the START_OF_ID token. */
10261 token->type = CPP_TEMPLATE_ID;
3369eb76 10262 /* Retrieve any deferred checks. Do not pop this access checks yet
10263 so the memory will not be reclaimed during token replacing below. */
10264 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10265 token->u.tree_check_value->value = template_id;
10266 token->u.tree_check_value->checks = get_deferred_access_checks ();
0a3b29ad 10267 token->keyword = RID_MAX;
9031d10b 10268
0a3b29ad 10269 /* Purge all subsequent tokens. */
19273cc2 10270 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
67635103 10271
10272 /* ??? Can we actually assume that, if template_id ==
10273 error_mark_node, we will have issued a diagnostic to the
10274 user, as opposed to simply marking the tentative parse as
10275 failed? */
10276 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
ad9ae192 10277 error ("%Hparse error in template argument list",
10278 &token->location);
0a3b29ad 10279 }
10280
9b57b06b 10281 pop_deferring_access_checks ();
0a3b29ad 10282 return template_id;
10283}
10284
10285/* Parse a template-name.
10286
10287 template-name:
10288 identifier
ccb84981 10289
0a3b29ad 10290 The standard should actually say:
10291
10292 template-name:
10293 identifier
10294 operator-function-id
0a3b29ad 10295
10296 A defect report has been filed about this issue.
10297
182d094e 10298 A conversion-function-id cannot be a template name because they cannot
10299 be part of a template-id. In fact, looking at this code:
10300
10301 a.operator K<int>()
10302
10303 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
ccb84981 10304 It is impossible to call a templated conversion-function-id with an
182d094e 10305 explicit argument list, since the only allowed template parameter is
10306 the type to which it is converting.
10307
0a3b29ad 10308 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10309 `template' keyword, in a construction like:
10310
10311 T::template f<3>()
10312
10313 In that case `f' is taken to be a template-name, even though there
10314 is no way of knowing for sure.
10315
10316 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10317 name refers to a set of overloaded functions, at least one of which
10318 is a template, or an IDENTIFIER_NODE with the name of the template,
10319 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10320 names are looked up inside uninstantiated templates. */
10321
10322static tree
ccb84981 10323cp_parser_template_name (cp_parser* parser,
653e5405 10324 bool template_keyword_p,
10325 bool check_dependency_p,
3d0f901b 10326 bool is_declaration,
10327 bool *is_identifier)
0a3b29ad 10328{
10329 tree identifier;
10330 tree decl;
10331 tree fns;
ad9ae192 10332 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 10333
10334 /* If the next token is `operator', then we have either an
10335 operator-function-id or a conversion-function-id. */
10336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10337 {
10338 /* We don't know whether we're looking at an
10339 operator-function-id or a conversion-function-id. */
10340 cp_parser_parse_tentatively (parser);
10341 /* Try an operator-function-id. */
10342 identifier = cp_parser_operator_function_id (parser);
10343 /* If that didn't work, try a conversion-function-id. */
10344 if (!cp_parser_parse_definitely (parser))
653e5405 10345 {
182d094e 10346 cp_parser_error (parser, "expected template-name");
10347 return error_mark_node;
653e5405 10348 }
0a3b29ad 10349 }
10350 /* Look for the identifier. */
10351 else
10352 identifier = cp_parser_identifier (parser);
ccb84981 10353
0a3b29ad 10354 /* If we didn't find an identifier, we don't have a template-id. */
10355 if (identifier == error_mark_node)
10356 return error_mark_node;
10357
10358 /* If the name immediately followed the `template' keyword, then it
10359 is a template-name. However, if the next token is not `<', then
10360 we do not treat it as a template-name, since it is not being used
10361 as part of a template-id. This enables us to handle constructs
10362 like:
10363
10364 template <typename T> struct S { S(); };
10365 template <typename T> S<T>::S();
10366
10367 correctly. We would treat `S' as a template -- if it were `S<T>'
10368 -- but we do not if there is no `<'. */
3d0f901b 10369
10370 if (processing_template_decl
c8d5ab79 10371 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
3d0f901b 10372 {
10373 /* In a declaration, in a dependent context, we pretend that the
10374 "template" keyword was present in order to improve error
10375 recovery. For example, given:
ccb84981 10376
3d0f901b 10377 template <typename T> void f(T::X<int>);
ccb84981 10378
3d0f901b 10379 we want to treat "X<int>" as a template-id. */
ccb84981 10380 if (is_declaration
10381 && !template_keyword_p
3d0f901b 10382 && parser->scope && TYPE_P (parser->scope)
0078a5e8 10383 && check_dependency_p
05f701e2 10384 && dependent_scope_p (parser->scope)
9ed82c65 10385 /* Do not do this for dtors (or ctors), since they never
10386 need the template keyword before their name. */
10387 && !constructor_name_p (identifier, parser->scope))
3d0f901b 10388 {
19273cc2 10389 cp_token_position start = 0;
9031d10b 10390
3d0f901b 10391 /* Explain what went wrong. */
ad9ae192 10392 error ("%Hnon-template %qD used as template",
10393 &token->location, identifier);
5bcc316e 10394 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
9ed82c65 10395 parser->scope, identifier);
efcbcf83 10396 /* If parsing tentatively, find the location of the "<" token. */
10397 if (cp_parser_simulate_error (parser))
10398 start = cp_lexer_token_position (parser->lexer, true);
3d0f901b 10399 /* Parse the template arguments so that we can issue error
10400 messages about them. */
10401 cp_lexer_consume_token (parser->lexer);
10402 cp_parser_enclosed_template_argument_list (parser);
10403 /* Skip tokens until we find a good place from which to
10404 continue parsing. */
10405 cp_parser_skip_to_closing_parenthesis (parser,
10406 /*recovering=*/true,
10407 /*or_comma=*/true,
10408 /*consume_paren=*/false);
10409 /* If parsing tentatively, permanently remove the
10410 template argument list. That will prevent duplicate
10411 error messages from being issued about the missing
10412 "template" keyword. */
19273cc2 10413 if (start)
10414 cp_lexer_purge_tokens_after (parser->lexer, start);
3d0f901b 10415 if (is_identifier)
10416 *is_identifier = true;
10417 return identifier;
10418 }
3180c04a 10419
10420 /* If the "template" keyword is present, then there is generally
10421 no point in doing name-lookup, so we just return IDENTIFIER.
10422 But, if the qualifying scope is non-dependent then we can
10423 (and must) do name-lookup normally. */
10424 if (template_keyword_p
10425 && (!parser->scope
207355ad 10426 || (TYPE_P (parser->scope)
3180c04a 10427 && dependent_type_p (parser->scope))))
3d0f901b 10428 return identifier;
10429 }
0a3b29ad 10430
10431 /* Look up the name. */
10432 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 10433 none_type,
c3b9e457 10434 /*is_template=*/false,
6fc758aa 10435 /*is_namespace=*/false,
2cdbcd51 10436 check_dependency_p,
ad9ae192 10437 /*ambiguous_decls=*/NULL,
10438 token->location);
0a3b29ad 10439 decl = maybe_get_template_decl_from_type_decl (decl);
10440
10441 /* If DECL is a template, then the name was a template-name. */
10442 if (TREE_CODE (decl) == TEMPLATE_DECL)
10443 ;
ccb84981 10444 else
0a3b29ad 10445 {
a5a297d8 10446 tree fn = NULL_TREE;
10447
0a3b29ad 10448 /* The standard does not explicitly indicate whether a name that
10449 names a set of overloaded declarations, some of which are
10450 templates, is a template-name. However, such a name should
10451 be a template-name; otherwise, there is no way to form a
10452 template-id for the overloaded templates. */
10453 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10454 if (TREE_CODE (fns) == OVERLOAD)
a5a297d8 10455 for (fn = fns; fn; fn = OVL_NEXT (fn))
10456 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10457 break;
ccb84981 10458
a5a297d8 10459 if (!fn)
0a3b29ad 10460 {
a5a297d8 10461 /* The name does not name a template. */
0a3b29ad 10462 cp_parser_error (parser, "expected template-name");
10463 return error_mark_node;
10464 }
10465 }
10466
10467 /* If DECL is dependent, and refers to a function, then just return
10468 its name; we will look it up again during template instantiation. */
10469 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10470 {
10471 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7e9a6a16 10472 if (TYPE_P (scope) && dependent_type_p (scope))
0a3b29ad 10473 return identifier;
10474 }
10475
10476 return decl;
10477}
10478
10479/* Parse a template-argument-list.
10480
10481 template-argument-list:
d95d815d 10482 template-argument ... [opt]
10483 template-argument-list , template-argument ... [opt]
0a3b29ad 10484
bd8962d5 10485 Returns a TREE_VEC containing the arguments. */
0a3b29ad 10486
10487static tree
45baea8b 10488cp_parser_template_argument_list (cp_parser* parser)
0a3b29ad 10489{
b5959ba9 10490 tree fixed_args[10];
10491 unsigned n_args = 0;
10492 unsigned alloced = 10;
10493 tree *arg_ary = fixed_args;
10494 tree vec;
92b128ed 10495 bool saved_in_template_argument_list_p;
45b4e1e4 10496 bool saved_ice_p;
10497 bool saved_non_ice_p;
0a3b29ad 10498
92b128ed 10499 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10500 parser->in_template_argument_list_p = true;
45b4e1e4 10501 /* Even if the template-id appears in an integral
074ab442 10502 constant-expression, the contents of the argument list do
10503 not. */
45b4e1e4 10504 saved_ice_p = parser->integral_constant_expression_p;
10505 parser->integral_constant_expression_p = false;
10506 saved_non_ice_p = parser->non_integral_constant_expression_p;
10507 parser->non_integral_constant_expression_p = false;
fbb01da7 10508 /* Parse the arguments. */
b5959ba9 10509 do
0a3b29ad 10510 {
10511 tree argument;
10512
b5959ba9 10513 if (n_args)
bd8962d5 10514 /* Consume the comma. */
b5959ba9 10515 cp_lexer_consume_token (parser->lexer);
ccb84981 10516
0a3b29ad 10517 /* Parse the template-argument. */
10518 argument = cp_parser_template_argument (parser);
d95d815d 10519
10520 /* If the next token is an ellipsis, we're expanding a template
10521 argument pack. */
10522 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10523 {
7cdf9e98 10524 if (argument == error_mark_node)
10525 {
10526 cp_token *token = cp_lexer_peek_token (parser->lexer);
10527 error ("%Hexpected parameter pack before %<...%>",
10528 &token->location);
10529 }
d95d815d 10530 /* Consume the `...' token. */
10531 cp_lexer_consume_token (parser->lexer);
10532
10533 /* Make the argument into a TYPE_PACK_EXPANSION or
10534 EXPR_PACK_EXPANSION. */
10535 argument = make_pack_expansion (argument);
10536 }
10537
b5959ba9 10538 if (n_args == alloced)
10539 {
10540 alloced *= 2;
ccb84981 10541
b5959ba9 10542 if (arg_ary == fixed_args)
10543 {
56e60747 10544 arg_ary = XNEWVEC (tree, alloced);
b5959ba9 10545 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10546 }
10547 else
7ea410eb 10548 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
b5959ba9 10549 }
10550 arg_ary[n_args++] = argument;
0a3b29ad 10551 }
b5959ba9 10552 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10553
10554 vec = make_tree_vec (n_args);
0a3b29ad 10555
b5959ba9 10556 while (n_args--)
10557 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
ccb84981 10558
b5959ba9 10559 if (arg_ary != fixed_args)
10560 free (arg_ary);
45b4e1e4 10561 parser->non_integral_constant_expression_p = saved_non_ice_p;
10562 parser->integral_constant_expression_p = saved_ice_p;
92b128ed 10563 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
b5959ba9 10564 return vec;
0a3b29ad 10565}
10566
10567/* Parse a template-argument.
10568
10569 template-argument:
10570 assignment-expression
10571 type-id
10572 id-expression
10573
10574 The representation is that of an assignment-expression, type-id, or
10575 id-expression -- except that the qualified id-expression is
10576 evaluated, so that the value returned is either a DECL or an
ccb84981 10577 OVERLOAD.
13795292 10578
10579 Although the standard says "assignment-expression", it forbids
10580 throw-expressions or assignments in the template argument.
10581 Therefore, we use "conditional-expression" instead. */
0a3b29ad 10582
10583static tree
45baea8b 10584cp_parser_template_argument (cp_parser* parser)
0a3b29ad 10585{
10586 tree argument;
10587 bool template_p;
13795292 10588 bool address_p;
bece9ea1 10589 bool maybe_type_id = false;
ad9ae192 10590 cp_token *token = NULL, *argument_start_token = NULL;
0886adbc 10591 cp_id_kind idk;
0a3b29ad 10592
10593 /* There's really no way to know what we're looking at, so we just
ccb84981 10594 try each alternative in order.
0a3b29ad 10595
10596 [temp.arg]
10597
10598 In a template-argument, an ambiguity between a type-id and an
10599 expression is resolved to a type-id, regardless of the form of
ccb84981 10600 the corresponding template-parameter.
0a3b29ad 10601
10602 Therefore, we try a type-id first. */
10603 cp_parser_parse_tentatively (parser);
75eaa947 10604 argument = cp_parser_template_type_arg (parser);
7d46c9d7 10605 /* If there was no error parsing the type-id but the next token is a
10606 '>>', our behavior depends on which dialect of C++ we're
10607 parsing. In C++98, we probably found a typo for '> >'. But there
10608 are type-id which are also valid expressions. For instance:
bece9ea1 10609
10610 struct X { int operator >> (int); };
10611 template <int V> struct Foo {};
10612 Foo<X () >> 5> r;
10613
10614 Here 'X()' is a valid type-id of a function type, but the user just
10615 wanted to write the expression "X() >> 5". Thus, we remember that we
10616 found a valid type-id, but we still try to parse the argument as an
7d46c9d7 10617 expression to see what happens.
10618
10619 In C++0x, the '>>' will be considered two separate '>'
10620 tokens. */
bece9ea1 10621 if (!cp_parser_error_occurred (parser)
7d46c9d7 10622 && cxx_dialect == cxx98
bece9ea1 10623 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10624 {
10625 maybe_type_id = true;
10626 cp_parser_abort_tentative_parse (parser);
10627 }
10628 else
10629 {
10630 /* If the next token isn't a `,' or a `>', then this argument wasn't
10631 really finished. This means that the argument is not a valid
10632 type-id. */
10633 if (!cp_parser_next_token_ends_template_argument_p (parser))
10634 cp_parser_error (parser, "expected template-argument");
10635 /* If that worked, we're done. */
10636 if (cp_parser_parse_definitely (parser))
10637 return argument;
10638 }
0a3b29ad 10639 /* We're still not sure what the argument will be. */
10640 cp_parser_parse_tentatively (parser);
10641 /* Try a template. */
ad9ae192 10642 argument_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 10643 argument = cp_parser_id_expression (parser,
0a3b29ad 10644 /*template_keyword_p=*/false,
10645 /*check_dependency_p=*/true,
899cc6e8 10646 &template_p,
197c9df7 10647 /*declarator_p=*/false,
130bb1d4 10648 /*optional_p=*/false);
0a3b29ad 10649 /* If the next token isn't a `,' or a `>', then this argument wasn't
10650 really finished. */
13795292 10651 if (!cp_parser_next_token_ends_template_argument_p (parser))
0a3b29ad 10652 cp_parser_error (parser, "expected template-argument");
10653 if (!cp_parser_error_occurred (parser))
10654 {
4ec378b6 10655 /* Figure out what is being referred to. If the id-expression
10656 was for a class template specialization, then we will have a
10657 TYPE_DECL at this point. There is no need to do name lookup
10658 at this point in that case. */
10659 if (TREE_CODE (argument) != TYPE_DECL)
10660 argument = cp_parser_lookup_name (parser, argument,
e2ae55f2 10661 none_type,
4ec378b6 10662 /*is_template=*/template_p,
10663 /*is_namespace=*/false,
2cdbcd51 10664 /*check_dependency=*/true,
ad9ae192 10665 /*ambiguous_decls=*/NULL,
10666 argument_start_token->location);
11c54989 10667 if (TREE_CODE (argument) != TEMPLATE_DECL
10668 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
0a3b29ad 10669 cp_parser_error (parser, "expected template-name");
10670 }
10671 if (cp_parser_parse_definitely (parser))
10672 return argument;
13795292 10673 /* It must be a non-type argument. There permitted cases are given
10674 in [temp.arg.nontype]:
10675
10676 -- an integral constant-expression of integral or enumeration
653e5405 10677 type; or
13795292 10678
10679 -- the name of a non-type template-parameter; or
10680
10681 -- the name of an object or function with external linkage...
10682
10683 -- the address of an object or function with external linkage...
10684
bd8962d5 10685 -- a pointer to member... */
13795292 10686 /* Look for a non-type template parameter. */
10687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10688 {
10689 cp_parser_parse_tentatively (parser);
10690 argument = cp_parser_primary_expression (parser,
08cc44e7 10691 /*address_p=*/false,
640aa28c 10692 /*cast_p=*/false,
fbb01da7 10693 /*template_arg_p=*/true,
10694 &idk);
13795292 10695 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10696 || !cp_parser_next_token_ends_template_argument_p (parser))
10697 cp_parser_simulate_error (parser);
10698 if (cp_parser_parse_definitely (parser))
10699 return argument;
10700 }
729f89ff 10701
13795292 10702 /* If the next token is "&", the argument must be the address of an
10703 object or function with external linkage. */
10704 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10705 if (address_p)
10706 cp_lexer_consume_token (parser->lexer);
10707 /* See if we might have an id-expression. */
10708 token = cp_lexer_peek_token (parser->lexer);
10709 if (token->type == CPP_NAME
10710 || token->keyword == RID_OPERATOR
10711 || token->type == CPP_SCOPE
10712 || token->type == CPP_TEMPLATE_ID
10713 || token->type == CPP_NESTED_NAME_SPECIFIER)
10714 {
10715 cp_parser_parse_tentatively (parser);
10716 argument = cp_parser_primary_expression (parser,
fbb01da7 10717 address_p,
640aa28c 10718 /*cast_p=*/false,
fbb01da7 10719 /*template_arg_p=*/true,
10720 &idk);
13795292 10721 if (cp_parser_error_occurred (parser)
10722 || !cp_parser_next_token_ends_template_argument_p (parser))
10723 cp_parser_abort_tentative_parse (parser);
10724 else
10725 {
729f89ff 10726 if (TREE_CODE (argument) == INDIRECT_REF)
10727 {
10728 gcc_assert (REFERENCE_REF_P (argument));
10729 argument = TREE_OPERAND (argument, 0);
10730 }
9031d10b 10731
13795292 10732 if (TREE_CODE (argument) == VAR_DECL)
10733 {
10734 /* A variable without external linkage might still be a
10735 valid constant-expression, so no error is issued here
10736 if the external-linkage check fails. */
33c1a964 10737 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
13795292 10738 cp_parser_simulate_error (parser);
10739 }
10740 else if (is_overloaded_fn (argument))
10741 /* All overloaded functions are allowed; if the external
10742 linkage test does not pass, an error will be issued
10743 later. */
10744 ;
10745 else if (address_p
ccb84981 10746 && (TREE_CODE (argument) == OFFSET_REF
13795292 10747 || TREE_CODE (argument) == SCOPE_REF))
10748 /* A pointer-to-member. */
10749 ;
729f89ff 10750 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10751 ;
13795292 10752 else
10753 cp_parser_simulate_error (parser);
10754
10755 if (cp_parser_parse_definitely (parser))
10756 {
10757 if (address_p)
ebd21de4 10758 argument = build_x_unary_op (ADDR_EXPR, argument,
10759 tf_warning_or_error);
13795292 10760 return argument;
10761 }
10762 }
10763 }
10764 /* If the argument started with "&", there are no other valid
10765 alternatives at this point. */
10766 if (address_p)
10767 {
10768 cp_parser_error (parser, "invalid non-type template argument");
10769 return error_mark_node;
10770 }
729f89ff 10771
bece9ea1 10772 /* If the argument wasn't successfully parsed as a type-id followed
ccb84981 10773 by '>>', the argument can only be a constant expression now.
bece9ea1 10774 Otherwise, we try parsing the constant-expression tentatively,
10775 because the argument could really be a type-id. */
10776 if (maybe_type_id)
10777 cp_parser_parse_tentatively (parser);
ccb84981 10778 argument = cp_parser_constant_expression (parser,
13795292 10779 /*allow_non_constant_p=*/false,
10780 /*non_constant_p=*/NULL);
2250d32c 10781 argument = fold_non_dependent_expr (argument);
bece9ea1 10782 if (!maybe_type_id)
10783 return argument;
10784 if (!cp_parser_next_token_ends_template_argument_p (parser))
10785 cp_parser_error (parser, "expected template-argument");
10786 if (cp_parser_parse_definitely (parser))
10787 return argument;
10788 /* We did our best to parse the argument as a non type-id, but that
10789 was the only alternative that matched (albeit with a '>' after
ccb84981 10790 it). We can assume it's just a typo from the user, and a
bece9ea1 10791 diagnostic will then be issued. */
75eaa947 10792 return cp_parser_template_type_arg (parser);
0a3b29ad 10793}
10794
10795/* Parse an explicit-instantiation.
10796
10797 explicit-instantiation:
ccb84981 10798 template declaration
0a3b29ad 10799
10800 Although the standard says `declaration', what it really means is:
10801
10802 explicit-instantiation:
ccb84981 10803 template decl-specifier-seq [opt] declarator [opt] ;
0a3b29ad 10804
10805 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10806 supposed to be allowed. A defect report has been filed about this
ccb84981 10807 issue.
0a3b29ad 10808
10809 GNU Extension:
ccb84981 10810
0a3b29ad 10811 explicit-instantiation:
ccb84981 10812 storage-class-specifier template
0a3b29ad 10813 decl-specifier-seq [opt] declarator [opt] ;
ccb84981 10814 function-specifier template
0a3b29ad 10815 decl-specifier-seq [opt] declarator [opt] ; */
10816
10817static void
45baea8b 10818cp_parser_explicit_instantiation (cp_parser* parser)
0a3b29ad 10819{
8172be22 10820 int declares_class_or_enum;
4b9b2871 10821 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 10822 tree extension_specifier = NULL_TREE;
eef0ab03 10823 cp_token *token;
0a3b29ad 10824
10825 /* Look for an (optional) storage-class-specifier or
10826 function-specifier. */
10827 if (cp_parser_allow_gnu_extensions_p (parser))
10828 {
ccb84981 10829 extension_specifier
0a3b29ad 10830 = cp_parser_storage_class_specifier_opt (parser);
10831 if (!extension_specifier)
207355ad 10832 extension_specifier
4b9b2871 10833 = cp_parser_function_specifier_opt (parser,
10834 /*decl_specs=*/NULL);
0a3b29ad 10835 }
10836
10837 /* Look for the `template' keyword. */
640710cf 10838 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
0a3b29ad 10839 /* Let the front end know that we are processing an explicit
10840 instantiation. */
10841 begin_explicit_instantiation ();
10842 /* [temp.explicit] says that we are supposed to ignore access
10843 control while processing explicit instantiation directives. */
4cab8273 10844 push_deferring_access_checks (dk_no_check);
0a3b29ad 10845 /* Parse a decl-specifier-seq. */
eef0ab03 10846 token = cp_lexer_peek_token (parser->lexer);
4b9b2871 10847 cp_parser_decl_specifier_seq (parser,
10848 CP_PARSER_FLAGS_OPTIONAL,
10849 &decl_specifiers,
10850 &declares_class_or_enum);
0a3b29ad 10851 /* If there was exactly one decl-specifier, and it declared a class,
10852 and there's no declarator, then we have an explicit type
10853 instantiation. */
10854 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10855 {
10856 tree type;
10857
4b9b2871 10858 type = check_tag_decl (&decl_specifiers);
6bdc805e 10859 /* Turn access control back on for names used during
10860 template instantiation. */
10861 pop_deferring_access_checks ();
0a3b29ad 10862 if (type)
c31fdaf6 10863 do_type_instantiation (type, extension_specifier,
074ab442 10864 /*complain=*/tf_error);
0a3b29ad 10865 }
10866 else
10867 {
3046c0a3 10868 cp_declarator *declarator;
0a3b29ad 10869 tree decl;
10870
10871 /* Parse the declarator. */
ccb84981 10872 declarator
42bbd0ec 10873 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 10874 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 10875 /*parenthesized_p=*/NULL,
10876 /*member_p=*/false);
e2ae55f2 10877 if (declares_class_or_enum & 2)
10878 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 10879 decl_specifiers.type,
10880 decl_specifiers.type_location);
3046c0a3 10881 if (declarator != cp_error_declarator)
e1a6bbd7 10882 {
4b9b2871 10883 decl = grokdeclarator (declarator, &decl_specifiers,
4a2849cb 10884 NORMAL, 0, &decl_specifiers.attributes);
e1a6bbd7 10885 /* Turn access control back on for names used during
10886 template instantiation. */
10887 pop_deferring_access_checks ();
10888 /* Do the explicit instantiation. */
10889 do_decl_instantiation (decl, extension_specifier);
10890 }
10891 else
10892 {
10893 pop_deferring_access_checks ();
10894 /* Skip the body of the explicit instantiation. */
10895 cp_parser_skip_to_end_of_statement (parser);
10896 }
0a3b29ad 10897 }
10898 /* We're done with the instantiation. */
10899 end_explicit_instantiation ();
0a3b29ad 10900
cf91b86a 10901 cp_parser_consume_semicolon_at_end_of_statement (parser);
0a3b29ad 10902}
10903
10904/* Parse an explicit-specialization.
10905
10906 explicit-specialization:
ccb84981 10907 template < > declaration
0a3b29ad 10908
10909 Although the standard says `declaration', what it really means is:
10910
10911 explicit-specialization:
10912 template <> decl-specifier [opt] init-declarator [opt] ;
ccb84981 10913 template <> function-definition
0a3b29ad 10914 template <> explicit-specialization
10915 template <> template-declaration */
10916
10917static void
45baea8b 10918cp_parser_explicit_specialization (cp_parser* parser)
0a3b29ad 10919{
9f25cdd8 10920 bool need_lang_pop;
ad9ae192 10921 cp_token *token = cp_lexer_peek_token (parser->lexer);
10922
0a3b29ad 10923 /* Look for the `template' keyword. */
640710cf 10924 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
0a3b29ad 10925 /* Look for the `<'. */
640710cf 10926 cp_parser_require (parser, CPP_LESS, "%<<%>");
0a3b29ad 10927 /* Look for the `>'. */
640710cf 10928 cp_parser_require (parser, CPP_GREATER, "%<>%>");
0a3b29ad 10929 /* We have processed another parameter list. */
10930 ++parser->num_template_parameter_lists;
9f25cdd8 10931 /* [temp]
074ab442 10932
9f25cdd8 10933 A template ... explicit specialization ... shall not have C
074ab442 10934 linkage. */
9f25cdd8 10935 if (current_lang_name == lang_name_c)
10936 {
ad9ae192 10937 error ("%Htemplate specialization with C linkage", &token->location);
9f25cdd8 10938 /* Give it C++ linkage to avoid confusing other parts of the
10939 front end. */
10940 push_lang_context (lang_name_cplusplus);
10941 need_lang_pop = true;
10942 }
10943 else
10944 need_lang_pop = false;
0a3b29ad 10945 /* Let the front end know that we are beginning a specialization. */
6d9bff9f 10946 if (!begin_specialization ())
10947 {
10948 end_specialization ();
6d9bff9f 10949 return;
10950 }
10951
0a3b29ad 10952 /* If the next keyword is `template', we need to figure out whether
10953 or not we're looking a template-declaration. */
10954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10955 {
10956 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10957 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10958 cp_parser_template_declaration_after_export (parser,
10959 /*member_p=*/false);
10960 else
10961 cp_parser_explicit_specialization (parser);
10962 }
10963 else
10964 /* Parse the dependent declaration. */
ccb84981 10965 cp_parser_single_declaration (parser,
3369eb76 10966 /*checks=*/NULL,
0a3b29ad 10967 /*member_p=*/false,
0c032b46 10968 /*explicit_specialization_p=*/true,
0a3b29ad 10969 /*friend_p=*/NULL);
0a3b29ad 10970 /* We're done with the specialization. */
10971 end_specialization ();
9f25cdd8 10972 /* For the erroneous case of a template with C linkage, we pushed an
10973 implicit C++ linkage scope; exit that scope now. */
10974 if (need_lang_pop)
10975 pop_lang_context ();
0a3b29ad 10976 /* We're done with this parameter list. */
10977 --parser->num_template_parameter_lists;
10978}
10979
10980/* Parse a type-specifier.
10981
10982 type-specifier:
10983 simple-type-specifier
10984 class-specifier
10985 enum-specifier
10986 elaborated-type-specifier
10987 cv-qualifier
10988
10989 GNU Extension:
10990
10991 type-specifier:
10992 __complex__
10993
4b9b2871 10994 Returns a representation of the type-specifier. For a
10995 class-specifier, enum-specifier, or elaborated-type-specifier, a
10996 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
0a3b29ad 10997
fb871e92 10998 The parser flags FLAGS is used to control type-specifier parsing.
10999
11000 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11001 in a decl-specifier-seq.
0a3b29ad 11002
11003 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11004 class-specifier, enum-specifier, or elaborated-type-specifier, then
678704be 11005 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8172be22 11006 if a type is declared; 2 if it is defined. Otherwise, it is set to
11007 zero.
0a3b29ad 11008
11009 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11010 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11011 is set to FALSE. */
11012
11013static tree
ccb84981 11014cp_parser_type_specifier (cp_parser* parser,
11015 cp_parser_flags flags,
4b9b2871 11016 cp_decl_specifier_seq *decl_specs,
45baea8b 11017 bool is_declaration,
8172be22 11018 int* declares_class_or_enum,
45baea8b 11019 bool* is_cv_qualifier)
0a3b29ad 11020{
11021 tree type_spec = NULL_TREE;
11022 cp_token *token;
11023 enum rid keyword;
4b9b2871 11024 cp_decl_spec ds = ds_last;
0a3b29ad 11025
11026 /* Assume this type-specifier does not declare a new type. */
11027 if (declares_class_or_enum)
8e594c09 11028 *declares_class_or_enum = 0;
0a3b29ad 11029 /* And that it does not specify a cv-qualifier. */
11030 if (is_cv_qualifier)
11031 *is_cv_qualifier = false;
11032 /* Peek at the next token. */
11033 token = cp_lexer_peek_token (parser->lexer);
11034
11035 /* If we're looking at a keyword, we can use that to guide the
11036 production we choose. */
11037 keyword = token->keyword;
11038 switch (keyword)
11039 {
637fdc50 11040 case RID_ENUM:
4a2849cb 11041 /* Look for the enum-specifier. */
11042 type_spec = cp_parser_enum_specifier (parser);
11043 /* If that worked, we're done. */
11044 if (type_spec)
637fdc50 11045 {
637fdc50 11046 if (declares_class_or_enum)
11047 *declares_class_or_enum = 2;
11048 if (decl_specs)
11049 cp_parser_set_decl_spec_type (decl_specs,
11050 type_spec,
eef0ab03 11051 token->location,
637fdc50 11052 /*user_defined_p=*/true);
11053 return type_spec;
11054 }
11055 else
11056 goto elaborated_type_specifier;
11057
0a3b29ad 11058 /* Any of these indicate either a class-specifier, or an
11059 elaborated-type-specifier. */
11060 case RID_CLASS:
11061 case RID_STRUCT:
11062 case RID_UNION:
0a3b29ad 11063 /* Parse tentatively so that we can back up if we don't find a
637fdc50 11064 class-specifier. */
0a3b29ad 11065 cp_parser_parse_tentatively (parser);
637fdc50 11066 /* Look for the class-specifier. */
11067 type_spec = cp_parser_class_specifier (parser);
dd2b35f1 11068 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
0a3b29ad 11069 /* If that worked, we're done. */
11070 if (cp_parser_parse_definitely (parser))
11071 {
11072 if (declares_class_or_enum)
8172be22 11073 *declares_class_or_enum = 2;
4b9b2871 11074 if (decl_specs)
11075 cp_parser_set_decl_spec_type (decl_specs,
11076 type_spec,
eef0ab03 11077 token->location,
4b9b2871 11078 /*user_defined_p=*/true);
0a3b29ad 11079 return type_spec;
11080 }
11081
11082 /* Fall through. */
637fdc50 11083 elaborated_type_specifier:
11084 /* We're declaring (not defining) a class or enum. */
11085 if (declares_class_or_enum)
11086 *declares_class_or_enum = 1;
0a3b29ad 11087
637fdc50 11088 /* Fall through. */
0a3b29ad 11089 case RID_TYPENAME:
11090 /* Look for an elaborated-type-specifier. */
207355ad 11091 type_spec
11092 = (cp_parser_elaborated_type_specifier
4b9b2871 11093 (parser,
11094 decl_specs && decl_specs->specs[(int) ds_friend],
11095 is_declaration));
4b9b2871 11096 if (decl_specs)
11097 cp_parser_set_decl_spec_type (decl_specs,
11098 type_spec,
eef0ab03 11099 token->location,
4b9b2871 11100 /*user_defined_p=*/true);
0a3b29ad 11101 return type_spec;
11102
11103 case RID_CONST:
4b9b2871 11104 ds = ds_const;
11105 if (is_cv_qualifier)
11106 *is_cv_qualifier = true;
11107 break;
207355ad 11108
0a3b29ad 11109 case RID_VOLATILE:
4b9b2871 11110 ds = ds_volatile;
0a3b29ad 11111 if (is_cv_qualifier)
11112 *is_cv_qualifier = true;
4b9b2871 11113 break;
0a3b29ad 11114
4b9b2871 11115 case RID_RESTRICT:
11116 ds = ds_restrict;
11117 if (is_cv_qualifier)
11118 *is_cv_qualifier = true;
11119 break;
0a3b29ad 11120
11121 case RID_COMPLEX:
11122 /* The `__complex__' keyword is a GNU extension. */
4b9b2871 11123 ds = ds_complex;
11124 break;
0a3b29ad 11125
11126 default:
11127 break;
11128 }
11129
4b9b2871 11130 /* Handle simple keywords. */
11131 if (ds != ds_last)
11132 {
11133 if (decl_specs)
11134 {
11135 ++decl_specs->specs[(int)ds];
11136 decl_specs->any_specifiers_p = true;
11137 }
3369eb76 11138 return cp_lexer_consume_token (parser->lexer)->u.value;
4b9b2871 11139 }
11140
0a3b29ad 11141 /* If we do not already have a type-specifier, assume we are looking
11142 at a simple-type-specifier. */
207355ad 11143 type_spec = cp_parser_simple_type_specifier (parser,
4b9b2871 11144 decl_specs,
11145 flags);
0a3b29ad 11146
11147 /* If we didn't find a type-specifier, and a type-specifier was not
11148 optional in this context, issue an error message. */
11149 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11150 {
11151 cp_parser_error (parser, "expected type specifier");
11152 return error_mark_node;
11153 }
11154
11155 return type_spec;
11156}
11157
11158/* Parse a simple-type-specifier.
11159
11160 simple-type-specifier:
11161 :: [opt] nested-name-specifier [opt] type-name
11162 :: [opt] nested-name-specifier template template-id
11163 char
11164 wchar_t
11165 bool
11166 short
11167 int
11168 long
11169 signed
11170 unsigned
11171 float
11172 double
ccb84981 11173 void
0a3b29ad 11174
34da8800 11175 C++0x Extension:
11176
11177 simple-type-specifier:
45b44d0a 11178 auto
34da8800 11179 decltype ( expression )
924bbf02 11180 char16_t
11181 char32_t
34da8800 11182
0a3b29ad 11183 GNU Extension:
11184
11185 simple-type-specifier:
11186 __typeof__ unary-expression
11187 __typeof__ ( type-id )
11188
4b9b2871 11189 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11190 appropriately updated. */
0a3b29ad 11191
11192static tree
207355ad 11193cp_parser_simple_type_specifier (cp_parser* parser,
4b9b2871 11194 cp_decl_specifier_seq *decl_specs,
11195 cp_parser_flags flags)
0a3b29ad 11196{
11197 tree type = NULL_TREE;
11198 cp_token *token;
11199
11200 /* Peek at the next token. */
11201 token = cp_lexer_peek_token (parser->lexer);
11202
11203 /* If we're looking at a keyword, things are easy. */
11204 switch (token->keyword)
11205 {
11206 case RID_CHAR:
4b9b2871 11207 if (decl_specs)
11208 decl_specs->explicit_char_p = true;
b7d1e8ea 11209 type = char_type_node;
11210 break;
924bbf02 11211 case RID_CHAR16:
11212 type = char16_type_node;
11213 break;
11214 case RID_CHAR32:
11215 type = char32_type_node;
11216 break;
0a3b29ad 11217 case RID_WCHAR:
b7d1e8ea 11218 type = wchar_type_node;
11219 break;
0a3b29ad 11220 case RID_BOOL:
b7d1e8ea 11221 type = boolean_type_node;
11222 break;
0a3b29ad 11223 case RID_SHORT:
4b9b2871 11224 if (decl_specs)
11225 ++decl_specs->specs[(int) ds_short];
b7d1e8ea 11226 type = short_integer_type_node;
11227 break;
0a3b29ad 11228 case RID_INT:
4b9b2871 11229 if (decl_specs)
11230 decl_specs->explicit_int_p = true;
b7d1e8ea 11231 type = integer_type_node;
11232 break;
0a3b29ad 11233 case RID_LONG:
4b9b2871 11234 if (decl_specs)
11235 ++decl_specs->specs[(int) ds_long];
b7d1e8ea 11236 type = long_integer_type_node;
11237 break;
0a3b29ad 11238 case RID_SIGNED:
4b9b2871 11239 if (decl_specs)
11240 ++decl_specs->specs[(int) ds_signed];
b7d1e8ea 11241 type = integer_type_node;
11242 break;
0a3b29ad 11243 case RID_UNSIGNED:
4b9b2871 11244 if (decl_specs)
11245 ++decl_specs->specs[(int) ds_unsigned];
b7d1e8ea 11246 type = unsigned_type_node;
11247 break;
0a3b29ad 11248 case RID_FLOAT:
b7d1e8ea 11249 type = float_type_node;
11250 break;
0a3b29ad 11251 case RID_DOUBLE:
b7d1e8ea 11252 type = double_type_node;
11253 break;
0a3b29ad 11254 case RID_VOID:
b7d1e8ea 11255 type = void_type_node;
11256 break;
45b44d0a 11257
11258 case RID_AUTO:
46f4817e 11259 maybe_warn_cpp0x ("C++0x auto");
11260 type = make_auto ();
45b44d0a 11261 break;
0a3b29ad 11262
34da8800 11263 case RID_DECLTYPE:
11264 /* Parse the `decltype' type. */
11265 type = cp_parser_decltype (parser);
11266
11267 if (decl_specs)
11268 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 11269 token->location,
34da8800 11270 /*user_defined_p=*/true);
11271
11272 return type;
11273
0a3b29ad 11274 case RID_TYPEOF:
4b9b2871 11275 /* Consume the `typeof' token. */
11276 cp_lexer_consume_token (parser->lexer);
11277 /* Parse the operand to `typeof'. */
11278 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11279 /* If it is not already a TYPE, take its type. */
11280 if (!TYPE_P (type))
11281 type = finish_typeof (type);
11282
11283 if (decl_specs)
11284 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 11285 token->location,
4b9b2871 11286 /*user_defined_p=*/true);
207355ad 11287
4b9b2871 11288 return type;
0a3b29ad 11289
11290 default:
11291 break;
11292 }
11293
b7d1e8ea 11294 /* If the type-specifier was for a built-in type, we're done. */
11295 if (type)
11296 {
11297 tree id;
11298
4b9b2871 11299 /* Record the type. */
11300 if (decl_specs
11301 && (token->keyword != RID_SIGNED
11302 && token->keyword != RID_UNSIGNED
11303 && token->keyword != RID_SHORT
11304 && token->keyword != RID_LONG))
207355ad 11305 cp_parser_set_decl_spec_type (decl_specs,
4b9b2871 11306 type,
eef0ab03 11307 token->location,
4b9b2871 11308 /*user_defined=*/false);
11309 if (decl_specs)
11310 decl_specs->any_specifiers_p = true;
11311
b7d1e8ea 11312 /* Consume the token. */
3369eb76 11313 id = cp_lexer_consume_token (parser->lexer)->u.value;
182d094e 11314
11315 /* There is no valid C++ program where a non-template type is
11316 followed by a "<". That usually indicates that the user thought
11317 that the type was a template. */
eef0ab03 11318 cp_parser_check_for_invalid_template_id (parser, type, token->location);
182d094e 11319
4b9b2871 11320 return TYPE_NAME (type);
b7d1e8ea 11321 }
11322
0a3b29ad 11323 /* The type-specifier must be a user-defined type. */
ccb84981 11324 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
0a3b29ad 11325 {
a116ab20 11326 bool qualified_p;
b446079d 11327 bool global_p;
a116ab20 11328
0a3b29ad 11329 /* Don't gobble tokens or issue error messages if this is an
11330 optional type-specifier. */
11331 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11332 cp_parser_parse_tentatively (parser);
11333
11334 /* Look for the optional `::' operator. */
b446079d 11335 global_p
a619017d 11336 = (cp_parser_global_scope_opt (parser,
130bb1d4 11337 /*current_scope_valid_p=*/false)
a619017d 11338 != NULL_TREE);
0a3b29ad 11339 /* Look for the nested-name specifier. */
a116ab20 11340 qualified_p
11341 = (cp_parser_nested_name_specifier_opt (parser,
11342 /*typename_keyword_p=*/false,
11343 /*check_dependency_p=*/true,
11344 /*type_p=*/false,
382fbf3d 11345 /*is_declaration=*/false)
11346 != NULL_TREE);
eef0ab03 11347 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11348 /* If we have seen a nested-name-specifier, and the next token
11349 is `template', then we are using the template-id production. */
ccb84981 11350 if (parser->scope
0a3b29ad 11351 && cp_parser_optional_template_keyword (parser))
11352 {
11353 /* Look for the template-id. */
ccb84981 11354 type = cp_parser_template_id (parser,
0a3b29ad 11355 /*template_keyword_p=*/true,
3d0f901b 11356 /*check_dependency_p=*/true,
11357 /*is_declaration=*/false);
0a3b29ad 11358 /* If the template-id did not name a type, we are out of
11359 luck. */
11360 if (TREE_CODE (type) != TYPE_DECL)
11361 {
11362 cp_parser_error (parser, "expected template-id for type");
11363 type = NULL_TREE;
11364 }
11365 }
11366 /* Otherwise, look for a type-name. */
11367 else
92b128ed 11368 type = cp_parser_type_name (parser);
a116ab20 11369 /* Keep track of all name-lookups performed in class scopes. */
207355ad 11370 if (type
b446079d 11371 && !global_p
a116ab20 11372 && !qualified_p
207355ad 11373 && TREE_CODE (type) == TYPE_DECL
a116ab20 11374 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11375 maybe_note_name_used_in_class (DECL_NAME (type), type);
0a3b29ad 11376 /* If it didn't work out, we don't have a TYPE. */
ccb84981 11377 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
0a3b29ad 11378 && !cp_parser_parse_definitely (parser))
11379 type = NULL_TREE;
4b9b2871 11380 if (type && decl_specs)
11381 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 11382 token->location,
4b9b2871 11383 /*user_defined=*/true);
0a3b29ad 11384 }
11385
11386 /* If we didn't get a type-name, issue an error message. */
11387 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11388 {
11389 cp_parser_error (parser, "expected type-name");
11390 return error_mark_node;
11391 }
11392
3d0f901b 11393 /* There is no valid C++ program where a non-template type is
11394 followed by a "<". That usually indicates that the user thought
11395 that the type was a template. */
92b128ed 11396 if (type && type != error_mark_node)
7a4e126b 11397 {
11398 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11399 If it is, then the '<'...'>' enclose protocol names rather than
11400 template arguments, and so everything is fine. */
11401 if (c_dialect_objc ()
11402 && (objc_is_id (type) || objc_is_class_name (type)))
11403 {
11404 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11405 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11406
11407 /* Clobber the "unqualified" type previously entered into
c78cbec8 11408 DECL_SPECS with the new, improved protocol-qualified version. */
7a4e126b 11409 if (decl_specs)
11410 decl_specs->type = qual_type;
11411
11412 return qual_type;
11413 }
11414
eef0ab03 11415 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11416 token->location);
9031d10b 11417 }
8534c3a3 11418
0a3b29ad 11419 return type;
11420}
11421
11422/* Parse a type-name.
11423
11424 type-name:
11425 class-name
11426 enum-name
ccb84981 11427 typedef-name
0a3b29ad 11428
11429 enum-name:
11430 identifier
11431
11432 typedef-name:
ccb84981 11433 identifier
0a3b29ad 11434
dfea972c 11435 Returns a TYPE_DECL for the type. */
0a3b29ad 11436
11437static tree
45baea8b 11438cp_parser_type_name (cp_parser* parser)
0a3b29ad 11439{
11440 tree type_decl;
0a3b29ad 11441
11442 /* We can't know yet whether it is a class-name or not. */
11443 cp_parser_parse_tentatively (parser);
11444 /* Try a class-name. */
ccb84981 11445 type_decl = cp_parser_class_name (parser,
0a3b29ad 11446 /*typename_keyword_p=*/false,
11447 /*template_keyword_p=*/false,
e2ae55f2 11448 none_type,
0a3b29ad 11449 /*check_dependency_p=*/true,
3d0f901b 11450 /*class_head_p=*/false,
11451 /*is_declaration=*/false);
0a3b29ad 11452 /* If it's not a class-name, keep looking. */
11453 if (!cp_parser_parse_definitely (parser))
11454 {
11455 /* It must be a typedef-name or an enum-name. */
674e90bd 11456 return cp_parser_nonclass_name (parser);
11457 }
ccb84981 11458
674e90bd 11459 return type_decl;
11460}
7a4e126b 11461
674e90bd 11462/* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
7a4e126b 11463
674e90bd 11464 enum-name:
11465 identifier
11466
11467 typedef-name:
11468 identifier
11469
11470 Returns a TYPE_DECL for the type. */
ccb84981 11471
674e90bd 11472static tree
11473cp_parser_nonclass_name (cp_parser* parser)
11474{
11475 tree type_decl;
11476 tree identifier;
11477
ad9ae192 11478 cp_token *token = cp_lexer_peek_token (parser->lexer);
674e90bd 11479 identifier = cp_parser_identifier (parser);
11480 if (identifier == error_mark_node)
11481 return error_mark_node;
11482
11483 /* Look up the type-name. */
ad9ae192 11484 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
674e90bd 11485
11486 if (TREE_CODE (type_decl) != TYPE_DECL
11487 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11488 {
11489 /* See if this is an Objective-C type. */
11490 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11491 tree type = objc_get_protocol_qualified_type (identifier, protos);
11492 if (type)
11493 type_decl = TYPE_NAME (type);
11494 }
11495
11496 /* Issue an error if we did not find a type-name. */
11497 if (TREE_CODE (type_decl) != TYPE_DECL)
11498 {
11499 if (!cp_parser_simulate_error (parser))
11500 cp_parser_name_lookup_error (parser, identifier, type_decl,
ad9ae192 11501 "is not a type", token->location);
674e90bd 11502 return error_mark_node;
11503 }
11504 /* Remember that the name was used in the definition of the
11505 current class so that we can check later to see if the
11506 meaning would have been different after the class was
11507 entirely defined. */
11508 else if (type_decl != error_mark_node
11509 && !parser->scope)
11510 maybe_note_name_used_in_class (identifier, type_decl);
11511
0a3b29ad 11512 return type_decl;
11513}
11514
0a3b29ad 11515/* Parse an elaborated-type-specifier. Note that the grammar given
11516 here incorporates the resolution to DR68.
11517
11518 elaborated-type-specifier:
11519 class-key :: [opt] nested-name-specifier [opt] identifier
11520 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
3f00a6c0 11521 enum-key :: [opt] nested-name-specifier [opt] identifier
0a3b29ad 11522 typename :: [opt] nested-name-specifier identifier
ccb84981 11523 typename :: [opt] nested-name-specifier template [opt]
11524 template-id
0a3b29ad 11525
c010fc5a 11526 GNU extension:
11527
11528 elaborated-type-specifier:
11529 class-key attributes :: [opt] nested-name-specifier [opt] identifier
ccb84981 11530 class-key attributes :: [opt] nested-name-specifier [opt]
653e5405 11531 template [opt] template-id
c010fc5a 11532 enum attributes :: [opt] nested-name-specifier [opt] identifier
11533
0a3b29ad 11534 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11535 declared `friend'. If IS_DECLARATION is TRUE, then this
11536 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11537 something is being declared.
11538
11539 Returns the TYPE specified. */
11540
11541static tree
ccb84981 11542cp_parser_elaborated_type_specifier (cp_parser* parser,
653e5405 11543 bool is_friend,
11544 bool is_declaration)
0a3b29ad 11545{
11546 enum tag_types tag_type;
11547 tree identifier;
11548 tree type = NULL_TREE;
c010fc5a 11549 tree attributes = NULL_TREE;
ad9ae192 11550 cp_token *token = NULL;
0a3b29ad 11551
11552 /* See if we're looking at the `enum' keyword. */
11553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11554 {
11555 /* Consume the `enum' token. */
11556 cp_lexer_consume_token (parser->lexer);
11557 /* Remember that it's an enumeration type. */
11558 tag_type = enum_type;
3f00a6c0 11559 /* Parse the optional `struct' or `class' key (for C++0x scoped
11560 enums). */
11561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11562 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11563 {
11564 if (cxx_dialect == cxx98)
11565 maybe_warn_cpp0x ("scoped enums");
11566
11567 /* Consume the `struct' or `class'. */
11568 cp_lexer_consume_token (parser->lexer);
11569 }
c010fc5a 11570 /* Parse the attributes. */
11571 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 11572 }
11573 /* Or, it might be `typename'. */
11574 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11575 RID_TYPENAME))
11576 {
11577 /* Consume the `typename' token. */
11578 cp_lexer_consume_token (parser->lexer);
11579 /* Remember that it's a `typename' type. */
11580 tag_type = typename_type;
11581 /* The `typename' keyword is only allowed in templates. */
11582 if (!processing_template_decl)
2b9e3597 11583 permerror (input_location, "using %<typename%> outside of template");
0a3b29ad 11584 }
11585 /* Otherwise it must be a class-key. */
11586 else
11587 {
11588 tag_type = cp_parser_class_key (parser);
11589 if (tag_type == none_type)
11590 return error_mark_node;
c010fc5a 11591 /* Parse the attributes. */
11592 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 11593 }
11594
11595 /* Look for the `::' operator. */
ccb84981 11596 cp_parser_global_scope_opt (parser,
130bb1d4 11597 /*current_scope_valid_p=*/false);
0a3b29ad 11598 /* Look for the nested-name-specifier. */
11599 if (tag_type == typename_type)
e6ec2049 11600 {
c75ae97e 11601 if (!cp_parser_nested_name_specifier (parser,
e6ec2049 11602 /*typename_keyword_p=*/true,
11603 /*check_dependency_p=*/true,
3d0f901b 11604 /*type_p=*/true,
c75ae97e 11605 is_declaration))
e6ec2049 11606 return error_mark_node;
11607 }
0a3b29ad 11608 else
11609 /* Even though `typename' is not present, the proposed resolution
11610 to Core Issue 180 says that in `class A<T>::B', `B' should be
11611 considered a type-name, even if `A<T>' is dependent. */
11612 cp_parser_nested_name_specifier_opt (parser,
11613 /*typename_keyword_p=*/true,
11614 /*check_dependency_p=*/true,
3d0f901b 11615 /*type_p=*/true,
11616 is_declaration);
6fa283db 11617 /* For everything but enumeration types, consider a template-id.
11618 For an enumeration type, consider only a plain identifier. */
0a3b29ad 11619 if (tag_type != enum_type)
11620 {
11621 bool template_p = false;
11622 tree decl;
11623
11624 /* Allow the `template' keyword. */
11625 template_p = cp_parser_optional_template_keyword (parser);
11626 /* If we didn't see `template', we don't know if there's a
653e5405 11627 template-id or not. */
0a3b29ad 11628 if (!template_p)
11629 cp_parser_parse_tentatively (parser);
11630 /* Parse the template-id. */
ad9ae192 11631 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11632 decl = cp_parser_template_id (parser, template_p,
3d0f901b 11633 /*check_dependency_p=*/true,
11634 is_declaration);
0a3b29ad 11635 /* If we didn't find a template-id, look for an ordinary
653e5405 11636 identifier. */
0a3b29ad 11637 if (!template_p && !cp_parser_parse_definitely (parser))
11638 ;
11639 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11640 in effect, then we must assume that, upon instantiation, the
11641 template will correspond to a class. */
11642 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11643 && tag_type == typename_type)
11644 type = make_typename_type (parser->scope, decl,
e2ae55f2 11645 typename_type,
c31fdaf6 11646 /*complain=*/tf_error);
cb288599 11647 /* If the `typename' keyword is in effect and DECL is not a type
11648 decl. Then type is non existant. */
11649 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11650 type = NULL_TREE;
11651 else
0a3b29ad 11652 type = TREE_TYPE (decl);
11653 }
11654
0a3b29ad 11655 if (!type)
11656 {
ad9ae192 11657 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11658 identifier = cp_parser_identifier (parser);
11659
11660 if (identifier == error_mark_node)
bbd3de90 11661 {
11662 parser->scope = NULL_TREE;
11663 return error_mark_node;
11664 }
0a3b29ad 11665
11666 /* For a `typename', we needn't call xref_tag. */
9031d10b 11667 if (tag_type == typename_type
d9da0685 11668 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
ccb84981 11669 return cp_parser_make_typename_type (parser, parser->scope,
ad9ae192 11670 identifier,
11671 token->location);
0a3b29ad 11672 /* Look up a qualified name in the usual way. */
11673 if (parser->scope)
11674 {
11675 tree decl;
8a58dc06 11676 tree ambiguous_decls;
0a3b29ad 11677
ccb84981 11678 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 11679 tag_type,
c3b9e457 11680 /*is_template=*/false,
6fc758aa 11681 /*is_namespace=*/false,
2cdbcd51 11682 /*check_dependency=*/true,
ad9ae192 11683 &ambiguous_decls,
11684 token->location);
8a58dc06 11685
11686 /* If the lookup was ambiguous, an error will already have been
11687 issued. */
11688 if (ambiguous_decls)
11689 return error_mark_node;
a5e50b31 11690
11691 /* If we are parsing friend declaration, DECL may be a
11692 TEMPLATE_DECL tree node here. However, we need to check
11693 whether this TEMPLATE_DECL results in valid code. Consider
11694 the following example:
11695
11696 namespace N {
11697 template <class T> class C {};
11698 }
11699 class X {
11700 template <class T> friend class N::C; // #1, valid code
11701 };
11702 template <class T> class Y {
11703 friend class N::C; // #2, invalid code
11704 };
11705
11706 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11707 name lookup of `N::C'. We see that friend declaration must
11708 be template for the code to be valid. Note that
11709 processing_template_decl does not work here since it is
11710 always 1 for the above two cases. */
11711
ccb84981 11712 decl = (cp_parser_maybe_treat_template_as_class
a5e50b31 11713 (decl, /*tag_name_p=*/is_friend
11714 && parser->num_template_parameter_lists));
0a3b29ad 11715
11716 if (TREE_CODE (decl) != TYPE_DECL)
11717 {
9031d10b 11718 cp_parser_diagnose_invalid_type_name (parser,
d9da0685 11719 parser->scope,
ad9ae192 11720 identifier,
11721 token->location);
0a3b29ad 11722 return error_mark_node;
11723 }
8172be22 11724
11725 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
539401d2 11726 {
11727 bool allow_template = (parser->num_template_parameter_lists
11728 || DECL_SELF_REFERENCE_P (decl));
11729 type = check_elaborated_type_specifier (tag_type, decl,
11730 allow_template);
11731
11732 if (type == error_mark_node)
11733 return error_mark_node;
11734 }
0a3b29ad 11735
f1f71333 11736 /* Forward declarations of nested types, such as
11737
11738 class C1::C2;
11739 class C1::C2::C3;
11740
11741 are invalid unless all components preceding the final '::'
11742 are complete. If all enclosing types are complete, these
11743 declarations become merely pointless.
11744
11745 Invalid forward declarations of nested types are errors
11746 caught elsewhere in parsing. Those that are pointless arrive
11747 here. */
11748
8d4f5f9e 11749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
f1f71333 11750 && !is_friend && !processing_explicit_instantiation)
11751 warning (0, "declaration %qD does not declare anything", decl);
11752
0a3b29ad 11753 type = TREE_TYPE (decl);
11754 }
ccb84981 11755 else
0a3b29ad 11756 {
11757 /* An elaborated-type-specifier sometimes introduces a new type and
11758 sometimes names an existing type. Normally, the rule is that it
11759 introduces a new type only if there is not an existing type of
11760 the same name already in scope. For example, given:
11761
11762 struct S {};
11763 void f() { struct S s; }
11764
11765 the `struct S' in the body of `f' is the same `struct S' as in
11766 the global scope; the existing definition is used. However, if
ccb84981 11767 there were no global declaration, this would introduce a new
0a3b29ad 11768 local class named `S'.
11769
11770 An exception to this rule applies to the following code:
11771
11772 namespace N { struct S; }
11773
11774 Here, the elaborated-type-specifier names a new type
11775 unconditionally; even if there is already an `S' in the
11776 containing scope this declaration names a new type.
11777 This exception only applies if the elaborated-type-specifier
11778 forms the complete declaration:
11779
ccb84981 11780 [class.name]
0a3b29ad 11781
11782 A declaration consisting solely of `class-key identifier ;' is
11783 either a redeclaration of the name in the current scope or a
11784 forward declaration of the identifier as a class name. It
11785 introduces the name into the current scope.
11786
11787 We are in this situation precisely when the next token is a `;'.
11788
11789 An exception to the exception is that a `friend' declaration does
11790 *not* name a new type; i.e., given:
11791
11792 struct S { friend struct T; };
11793
ccb84981 11794 `T' is not a new type in the scope of `S'.
0a3b29ad 11795
11796 Also, `new struct S' or `sizeof (struct S)' never results in the
11797 definition of a new type; a new type can only be declared in a
6beb3f76 11798 declaration context. */
0a3b29ad 11799
1fadf2c8 11800 tag_scope ts;
4f311379 11801 bool template_p;
11802
1fadf2c8 11803 if (is_friend)
11804 /* Friends have special name lookup rules. */
11805 ts = ts_within_enclosing_non_class;
11806 else if (is_declaration
11807 && cp_lexer_next_token_is (parser->lexer,
11808 CPP_SEMICOLON))
11809 /* This is a `class-key identifier ;' */
11810 ts = ts_current;
11811 else
11812 ts = ts_global;
11813
074ab442 11814 template_p =
4f311379 11815 (parser->num_template_parameter_lists
11816 && (cp_parser_next_token_starts_class_definition_p (parser)
11817 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
7be1bc1f 11818 /* An unqualified name was used to reference this type, so
11819 there were no qualifying templates. */
074ab442 11820 if (!cp_parser_check_template_parameters (parser,
ad9ae192 11821 /*num_templates=*/0,
7b07a15e 11822 token->location,
11823 /*declarator=*/NULL))
7be1bc1f 11824 return error_mark_node;
4f311379 11825 type = xref_tag (tag_type, identifier, ts, template_p);
0a3b29ad 11826 }
11827 }
4a2849cb 11828
7b83d1d8 11829 if (type == error_mark_node)
11830 return error_mark_node;
11831
4a2849cb 11832 /* Allow attributes on forward declarations of classes. */
11833 if (attributes)
11834 {
b415ff10 11835 if (TREE_CODE (type) == TYPENAME_TYPE)
11836 warning (OPT_Wattributes,
11837 "attributes ignored on uninstantiated type");
11838 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11839 && ! processing_explicit_instantiation)
4a2849cb 11840 warning (OPT_Wattributes,
11841 "attributes ignored on template instantiation");
11842 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11843 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11844 else
11845 warning (OPT_Wattributes,
11846 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11847 }
11848
0a3b29ad 11849 if (tag_type != enum_type)
11850 cp_parser_check_class_key (tag_type, type);
1157e9f0 11851
11852 /* A "<" cannot follow an elaborated type specifier. If that
11853 happens, the user was probably trying to form a template-id. */
eef0ab03 11854 cp_parser_check_for_invalid_template_id (parser, type, token->location);
1157e9f0 11855
0a3b29ad 11856 return type;
11857}
11858
11859/* Parse an enum-specifier.
11860
11861 enum-specifier:
3f00a6c0 11862 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11863
11864 enum-key:
11865 enum
11866 enum class [C++0x]
11867 enum struct [C++0x]
11868
11869 enum-base: [C++0x]
11870 : type-specifier-seq
0a3b29ad 11871
fe80e237 11872 GNU Extensions:
3f00a6c0 11873 enum-key attributes[opt] identifier [opt] enum-base [opt]
11874 { enumerator-list [opt] }attributes[opt]
fe80e237 11875
4a2849cb 11876 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11877 if the token stream isn't an enum-specifier after all. */
0a3b29ad 11878
11879static tree
45baea8b 11880cp_parser_enum_specifier (cp_parser* parser)
0a3b29ad 11881{
637fdc50 11882 tree identifier;
0a3b29ad 11883 tree type;
4a2849cb 11884 tree attributes;
3f00a6c0 11885 bool scoped_enum_p = false;
b706ce78 11886 bool has_underlying_type = false;
3f00a6c0 11887 tree underlying_type = NULL_TREE;
4a2849cb 11888
11889 /* Parse tentatively so that we can back up if we don't find a
11890 enum-specifier. */
11891 cp_parser_parse_tentatively (parser);
0a3b29ad 11892
637fdc50 11893 /* Caller guarantees that the current token is 'enum', an identifier
11894 possibly follows, and the token after that is an opening brace.
11895 If we don't have an identifier, fabricate an anonymous name for
11896 the enumeration being defined. */
11897 cp_lexer_consume_token (parser->lexer);
0a3b29ad 11898
3f00a6c0 11899 /* Parse the "class" or "struct", which indicates a scoped
11900 enumeration type in C++0x. */
11901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11902 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11903 {
11904 if (cxx_dialect == cxx98)
11905 maybe_warn_cpp0x ("scoped enums");
11906
11907 /* Consume the `struct' or `class' token. */
11908 cp_lexer_consume_token (parser->lexer);
11909
11910 scoped_enum_p = true;
11911 }
b706ce78 11912
4a2849cb 11913 attributes = cp_parser_attributes_opt (parser);
11914
637fdc50 11915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
0a3b29ad 11916 identifier = cp_parser_identifier (parser);
637fdc50 11917 else
11918 identifier = make_anon_name ();
0a3b29ad 11919
3f00a6c0 11920 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11921 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11922 {
11923 cp_decl_specifier_seq type_specifiers;
11924
b706ce78 11925 /* At this point this is surely not elaborated type specifier. */
11926 if (!cp_parser_parse_definitely (parser))
11927 return NULL_TREE;
11928
3f00a6c0 11929 if (cxx_dialect == cxx98)
11930 maybe_warn_cpp0x ("scoped enums");
11931
11932 /* Consume the `:'. */
11933 cp_lexer_consume_token (parser->lexer);
11934
b706ce78 11935 has_underlying_type = true;
11936
3f00a6c0 11937 /* Parse the type-specifier-seq. */
11938 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11939 &type_specifiers);
b706ce78 11940
3f00a6c0 11941 /* If that didn't work, stop. */
11942 if (type_specifiers.type != error_mark_node)
11943 {
11944 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11945 /*initialized=*/0, NULL);
11946 if (underlying_type == error_mark_node)
11947 underlying_type = NULL_TREE;
11948 }
3f00a6c0 11949 }
11950
4a2849cb 11951 /* Look for the `{' but don't consume it yet. */
11952 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
b706ce78 11953 {
11954 cp_parser_error (parser, "expected %<{%>");
11955 if (has_underlying_type)
11956 return NULL_TREE;
11957 }
4a2849cb 11958
b706ce78 11959 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
4a2849cb 11960 return NULL_TREE;
11961
0a3b29ad 11962 /* Issue an error message if type-definitions are forbidden here. */
9a7c4b43 11963 if (!cp_parser_check_type_definition (parser))
11964 type = error_mark_node;
11965 else
11966 /* Create the new type. We do this before consuming the opening
11967 brace so the enum will be recorded as being on the line of its
11968 tag (or the 'enum' keyword, if there is no tag). */
3f00a6c0 11969 type = start_enum (identifier, underlying_type, scoped_enum_p);
9a7c4b43 11970
b9dd3954 11971 /* Consume the opening brace. */
11972 cp_lexer_consume_token (parser->lexer);
11973
4a2849cb 11974 if (type == error_mark_node)
11975 {
11976 cp_parser_skip_to_end_of_block_or_statement (parser);
11977 return error_mark_node;
11978 }
11979
637fdc50 11980 /* If the next token is not '}', then there are some enumerators. */
11981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
0a3b29ad 11982 cp_parser_enumerator_list (parser, type);
637fdc50 11983
11984 /* Consume the final '}'. */
640710cf 11985 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 11986
fe80e237 11987 /* Look for trailing attributes to apply to this enumeration, and
93523877 11988 apply them if appropriate. */
fe80e237 11989 if (cp_parser_allow_gnu_extensions_p (parser))
11990 {
11991 tree trailing_attr = cp_parser_attributes_opt (parser);
07dd1387 11992 trailing_attr = chainon (trailing_attr, attributes);
fe80e237 11993 cplus_decl_attributes (&type,
11994 trailing_attr,
11995 (int) ATTR_FLAG_TYPE_IN_PLACE);
11996 }
11997
0a3b29ad 11998 /* Finish up the enumeration. */
11999 finish_enum (type);
12000
12001 return type;
12002}
12003
12004/* Parse an enumerator-list. The enumerators all have the indicated
ccb84981 12005 TYPE.
0a3b29ad 12006
12007 enumerator-list:
12008 enumerator-definition
12009 enumerator-list , enumerator-definition */
12010
12011static void
45baea8b 12012cp_parser_enumerator_list (cp_parser* parser, tree type)
0a3b29ad 12013{
12014 while (true)
12015 {
0a3b29ad 12016 /* Parse an enumerator-definition. */
12017 cp_parser_enumerator_definition (parser, type);
637fdc50 12018
12019 /* If the next token is not a ',', we've reached the end of
12020 the list. */
12021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 12022 break;
12023 /* Otherwise, consume the `,' and keep going. */
12024 cp_lexer_consume_token (parser->lexer);
12025 /* If the next token is a `}', there is a trailing comma. */
12026 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12027 {
8864917d 12028 if (!in_system_header)
21ca8540 12029 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
0a3b29ad 12030 break;
12031 }
12032 }
12033}
12034
12035/* Parse an enumerator-definition. The enumerator has the indicated
12036 TYPE.
12037
12038 enumerator-definition:
12039 enumerator
12040 enumerator = constant-expression
ccb84981 12041
0a3b29ad 12042 enumerator:
12043 identifier */
12044
12045static void
45baea8b 12046cp_parser_enumerator_definition (cp_parser* parser, tree type)
0a3b29ad 12047{
0a3b29ad 12048 tree identifier;
12049 tree value;
12050
12051 /* Look for the identifier. */
12052 identifier = cp_parser_identifier (parser);
12053 if (identifier == error_mark_node)
12054 return;
ccb84981 12055
637fdc50 12056 /* If the next token is an '=', then there is an explicit value. */
12057 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
0a3b29ad 12058 {
12059 /* Consume the `=' token. */
12060 cp_lexer_consume_token (parser->lexer);
12061 /* Parse the value. */
ccb84981 12062 value = cp_parser_constant_expression (parser,
13795292 12063 /*allow_non_constant_p=*/false,
5f6526e1 12064 NULL);
0a3b29ad 12065 }
12066 else
12067 value = NULL_TREE;
12068
ccc5f70d 12069 /* If we are processing a template, make sure the initializer of the
12070 enumerator doesn't contain any bare template parameter pack. */
12071 if (check_for_bare_parameter_packs (value))
12072 value = error_mark_node;
12073
0a3b29ad 12074 /* Create the enumerator. */
12075 build_enumerator (identifier, value, type);
12076}
12077
12078/* Parse a namespace-name.
12079
12080 namespace-name:
12081 original-namespace-name
12082 namespace-alias
12083
12084 Returns the NAMESPACE_DECL for the namespace. */
12085
12086static tree
45baea8b 12087cp_parser_namespace_name (cp_parser* parser)
0a3b29ad 12088{
12089 tree identifier;
12090 tree namespace_decl;
12091
ad9ae192 12092 cp_token *token = cp_lexer_peek_token (parser->lexer);
12093
0a3b29ad 12094 /* Get the name of the namespace. */
12095 identifier = cp_parser_identifier (parser);
12096 if (identifier == error_mark_node)
12097 return error_mark_node;
12098
6fc758aa 12099 /* Look up the identifier in the currently active scope. Look only
12100 for namespaces, due to:
12101
12102 [basic.lookup.udir]
12103
12104 When looking up a namespace-name in a using-directive or alias
ccb84981 12105 definition, only namespace names are considered.
6fc758aa 12106
12107 And:
12108
12109 [basic.lookup.qual]
12110
12111 During the lookup of a name preceding the :: scope resolution
ccb84981 12112 operator, object, function, and enumerator names are ignored.
6fc758aa 12113
3f00a6c0 12114 (Note that cp_parser_qualifying_entity only calls this
6fc758aa 12115 function if the token after the name is the scope resolution
12116 operator.) */
12117 namespace_decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 12118 none_type,
c3b9e457 12119 /*is_template=*/false,
6fc758aa 12120 /*is_namespace=*/true,
2cdbcd51 12121 /*check_dependency=*/true,
ad9ae192 12122 /*ambiguous_decls=*/NULL,
12123 token->location);
0a3b29ad 12124 /* If it's not a namespace, issue an error. */
12125 if (namespace_decl == error_mark_node
12126 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12127 {
f75d14ca 12128 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ad9ae192 12129 error ("%H%qD is not a namespace-name", &token->location, identifier);
0a3b29ad 12130 cp_parser_error (parser, "expected namespace-name");
12131 namespace_decl = error_mark_node;
12132 }
ccb84981 12133
0a3b29ad 12134 return namespace_decl;
12135}
12136
12137/* Parse a namespace-definition.
12138
12139 namespace-definition:
12140 named-namespace-definition
ccb84981 12141 unnamed-namespace-definition
0a3b29ad 12142
12143 named-namespace-definition:
12144 original-namespace-definition
12145 extension-namespace-definition
12146
12147 original-namespace-definition:
12148 namespace identifier { namespace-body }
ccb84981 12149
0a3b29ad 12150 extension-namespace-definition:
12151 namespace original-namespace-name { namespace-body }
ccb84981 12152
0a3b29ad 12153 unnamed-namespace-definition:
12154 namespace { namespace-body } */
12155
12156static void
45baea8b 12157cp_parser_namespace_definition (cp_parser* parser)
0a3b29ad 12158{
799435d8 12159 tree identifier, attribs;
1fd77946 12160 bool has_visibility;
93635a8e 12161 bool is_inline;
12162
12163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12164 {
12165 is_inline = true;
12166 cp_lexer_consume_token (parser->lexer);
12167 }
12168 else
12169 is_inline = false;
0a3b29ad 12170
12171 /* Look for the `namespace' keyword. */
640710cf 12172 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
0a3b29ad 12173
12174 /* Get the name of the namespace. We do not attempt to distinguish
12175 between an original-namespace-definition and an
12176 extension-namespace-definition at this point. The semantic
12177 analysis routines are responsible for that. */
12178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12179 identifier = cp_parser_identifier (parser);
12180 else
12181 identifier = NULL_TREE;
12182
799435d8 12183 /* Parse any specified attributes. */
12184 attribs = cp_parser_attributes_opt (parser);
12185
0a3b29ad 12186 /* Look for the `{' to start the namespace. */
640710cf 12187 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
0a3b29ad 12188 /* Start the namespace. */
1fd77946 12189 push_namespace (identifier);
12190
93635a8e 12191 /* "inline namespace" is equivalent to a stub namespace definition
12192 followed by a strong using directive. */
12193 if (is_inline)
12194 {
607a5d68 12195 tree name_space = current_namespace;
93635a8e 12196 /* Set up namespace association. */
607a5d68 12197 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12198 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12199 DECL_NAMESPACE_ASSOCIATIONS (name_space));
93635a8e 12200 /* Import the contents of the inline namespace. */
12201 pop_namespace ();
607a5d68 12202 do_using_directive (name_space);
93635a8e 12203 push_namespace (identifier);
12204 }
12205
1fd77946 12206 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12207
0a3b29ad 12208 /* Parse the body of the namespace. */
12209 cp_parser_namespace_body (parser);
1fd77946 12210
12211#ifdef HANDLE_PRAGMA_VISIBILITY
12212 if (has_visibility)
12213 pop_visibility ();
12214#endif
12215
0a3b29ad 12216 /* Finish the namespace. */
12217 pop_namespace ();
12218 /* Look for the final `}'. */
640710cf 12219 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
0a3b29ad 12220}
12221
12222/* Parse a namespace-body.
12223
12224 namespace-body:
12225 declaration-seq [opt] */
12226
12227static void
45baea8b 12228cp_parser_namespace_body (cp_parser* parser)
0a3b29ad 12229{
12230 cp_parser_declaration_seq_opt (parser);
12231}
12232
12233/* Parse a namespace-alias-definition.
12234
12235 namespace-alias-definition:
12236 namespace identifier = qualified-namespace-specifier ; */
12237
12238static void
45baea8b 12239cp_parser_namespace_alias_definition (cp_parser* parser)
0a3b29ad 12240{
12241 tree identifier;
12242 tree namespace_specifier;
12243
ad9ae192 12244 cp_token *token = cp_lexer_peek_token (parser->lexer);
12245
0a3b29ad 12246 /* Look for the `namespace' keyword. */
640710cf 12247 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
0a3b29ad 12248 /* Look for the identifier. */
12249 identifier = cp_parser_identifier (parser);
12250 if (identifier == error_mark_node)
12251 return;
12252 /* Look for the `=' token. */
26d3c536 12253 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12254 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12255 {
ad9ae192 12256 error ("%H%<namespace%> definition is not allowed here", &token->location);
26d3c536 12257 /* Skip the definition. */
12258 cp_lexer_consume_token (parser->lexer);
9aad947b 12259 if (cp_parser_skip_to_closing_brace (parser))
12260 cp_lexer_consume_token (parser->lexer);
26d3c536 12261 return;
12262 }
640710cf 12263 cp_parser_require (parser, CPP_EQ, "%<=%>");
0a3b29ad 12264 /* Look for the qualified-namespace-specifier. */
ccb84981 12265 namespace_specifier
0a3b29ad 12266 = cp_parser_qualified_namespace_specifier (parser);
12267 /* Look for the `;' token. */
640710cf 12268 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 12269
12270 /* Register the alias in the symbol table. */
12271 do_namespace_alias (identifier, namespace_specifier);
12272}
12273
12274/* Parse a qualified-namespace-specifier.
12275
12276 qualified-namespace-specifier:
12277 :: [opt] nested-name-specifier [opt] namespace-name
12278
12279 Returns a NAMESPACE_DECL corresponding to the specified
12280 namespace. */
12281
12282static tree
45baea8b 12283cp_parser_qualified_namespace_specifier (cp_parser* parser)
0a3b29ad 12284{
12285 /* Look for the optional `::'. */
ccb84981 12286 cp_parser_global_scope_opt (parser,
130bb1d4 12287 /*current_scope_valid_p=*/false);
0a3b29ad 12288
12289 /* Look for the optional nested-name-specifier. */
12290 cp_parser_nested_name_specifier_opt (parser,
12291 /*typename_keyword_p=*/false,
12292 /*check_dependency_p=*/true,
3d0f901b 12293 /*type_p=*/false,
12294 /*is_declaration=*/true);
0a3b29ad 12295
12296 return cp_parser_namespace_name (parser);
12297}
12298
da2a3271 12299/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12300 access declaration.
0a3b29ad 12301
12302 using-declaration:
12303 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
da2a3271 12304 using :: unqualified-id ;
0a3b29ad 12305
da2a3271 12306 access-declaration:
12307 qualified-id ;
12308
12309 */
12310
12311static bool
12312cp_parser_using_declaration (cp_parser* parser,
12313 bool access_declaration_p)
0a3b29ad 12314{
12315 cp_token *token;
12316 bool typename_p = false;
12317 bool global_scope_p;
12318 tree decl;
12319 tree identifier;
2c47ecdb 12320 tree qscope;
0a3b29ad 12321
da2a3271 12322 if (access_declaration_p)
12323 cp_parser_parse_tentatively (parser);
12324 else
0a3b29ad 12325 {
da2a3271 12326 /* Look for the `using' keyword. */
640710cf 12327 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
da2a3271 12328
12329 /* Peek at the next token. */
12330 token = cp_lexer_peek_token (parser->lexer);
12331 /* See if it's `typename'. */
12332 if (token->keyword == RID_TYPENAME)
12333 {
12334 /* Remember that we've seen it. */
12335 typename_p = true;
12336 /* Consume the `typename' token. */
12337 cp_lexer_consume_token (parser->lexer);
12338 }
0a3b29ad 12339 }
12340
12341 /* Look for the optional global scope qualification. */
ccb84981 12342 global_scope_p
0a3b29ad 12343 = (cp_parser_global_scope_opt (parser,
130bb1d4 12344 /*current_scope_valid_p=*/false)
0a3b29ad 12345 != NULL_TREE);
12346
12347 /* If we saw `typename', or didn't see `::', then there must be a
12348 nested-name-specifier present. */
12349 if (typename_p || !global_scope_p)
ccb84981 12350 qscope = cp_parser_nested_name_specifier (parser, typename_p,
2c47ecdb 12351 /*check_dependency_p=*/true,
12352 /*type_p=*/false,
12353 /*is_declaration=*/true);
0a3b29ad 12354 /* Otherwise, we could be in either of the two productions. In that
12355 case, treat the nested-name-specifier as optional. */
12356 else
2c47ecdb 12357 qscope = cp_parser_nested_name_specifier_opt (parser,
12358 /*typename_keyword_p=*/false,
12359 /*check_dependency_p=*/true,
12360 /*type_p=*/false,
12361 /*is_declaration=*/true);
12362 if (!qscope)
12363 qscope = global_namespace;
0a3b29ad 12364
7638e10c 12365 if (access_declaration_p && cp_parser_error_occurred (parser))
12366 /* Something has already gone wrong; there's no need to parse
12367 further. Since an error has occurred, the return value of
12368 cp_parser_parse_definitely will be false, as required. */
12369 return cp_parser_parse_definitely (parser);
12370
ad9ae192 12371 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 12372 /* Parse the unqualified-id. */
ccb84981 12373 identifier = cp_parser_unqualified_id (parser,
0a3b29ad 12374 /*template_keyword_p=*/false,
899cc6e8 12375 /*check_dependency_p=*/true,
197c9df7 12376 /*declarator_p=*/true,
130bb1d4 12377 /*optional_p=*/false);
0a3b29ad 12378
da2a3271 12379 if (access_declaration_p)
12380 {
12381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12382 cp_parser_simulate_error (parser);
12383 if (!cp_parser_parse_definitely (parser))
12384 return false;
12385 }
12386
0a3b29ad 12387 /* The function we call to handle a using-declaration is different
12388 depending on what scope we are in. */
c91d0d7d 12389 if (qscope == error_mark_node || identifier == error_mark_node)
899cc6e8 12390 ;
12391 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12392 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12393 /* [namespace.udecl]
12394
12395 A using declaration shall not name a template-id. */
ad9ae192 12396 error ("%Ha template-id may not appear in a using-declaration",
12397 &token->location);
0a3b29ad 12398 else
12399 {
46f43a6b 12400 if (at_class_scope_p ())
642a03ff 12401 {
899cc6e8 12402 /* Create the USING_DECL. */
2ded3667 12403 decl = do_class_using_decl (parser->scope, identifier);
613687b1 12404
830a6615 12405 if (check_for_bare_parameter_packs (decl))
613687b1 12406 return false;
12407 else
12408 /* Add it to the list of members in this class. */
12409 finish_member_declaration (decl);
642a03ff 12410 }
0a3b29ad 12411 else
899cc6e8 12412 {
ad9ae192 12413 decl = cp_parser_lookup_name_simple (parser,
12414 identifier,
12415 token->location);
899cc6e8 12416 if (decl == error_mark_node)
ad9ae192 12417 cp_parser_name_lookup_error (parser, identifier,
12418 decl, NULL,
12419 token->location);
830a6615 12420 else if (check_for_bare_parameter_packs (decl))
613687b1 12421 return false;
46f43a6b 12422 else if (!at_namespace_scope_p ())
2c47ecdb 12423 do_local_using_decl (decl, qscope, identifier);
899cc6e8 12424 else
2c47ecdb 12425 do_toplevel_using_decl (decl, qscope, identifier);
899cc6e8 12426 }
0a3b29ad 12427 }
12428
12429 /* Look for the final `;'. */
640710cf 12430 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
da2a3271 12431
12432 return true;
0a3b29ad 12433}
12434
ccb84981 12435/* Parse a using-directive.
12436
0a3b29ad 12437 using-directive:
12438 using namespace :: [opt] nested-name-specifier [opt]
12439 namespace-name ; */
12440
12441static void
45baea8b 12442cp_parser_using_directive (cp_parser* parser)
0a3b29ad 12443{
12444 tree namespace_decl;
a5ed46c9 12445 tree attribs;
0a3b29ad 12446
12447 /* Look for the `using' keyword. */
640710cf 12448 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
0a3b29ad 12449 /* And the `namespace' keyword. */
640710cf 12450 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
0a3b29ad 12451 /* Look for the optional `::' operator. */
130bb1d4 12452 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
755edffd 12453 /* And the optional nested-name-specifier. */
0a3b29ad 12454 cp_parser_nested_name_specifier_opt (parser,
12455 /*typename_keyword_p=*/false,
12456 /*check_dependency_p=*/true,
3d0f901b 12457 /*type_p=*/false,
12458 /*is_declaration=*/true);
0a3b29ad 12459 /* Get the namespace being used. */
12460 namespace_decl = cp_parser_namespace_name (parser);
a5ed46c9 12461 /* And any specified attributes. */
12462 attribs = cp_parser_attributes_opt (parser);
0a3b29ad 12463 /* Update the symbol table. */
a5ed46c9 12464 parse_using_directive (namespace_decl, attribs);
0a3b29ad 12465 /* Look for the final `;'. */
640710cf 12466 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 12467}
12468
12469/* Parse an asm-definition.
12470
12471 asm-definition:
ccb84981 12472 asm ( string-literal ) ;
0a3b29ad 12473
12474 GNU Extension:
12475
12476 asm-definition:
12477 asm volatile [opt] ( string-literal ) ;
12478 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12479 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 12480 : asm-operand-list [opt] ) ;
ccb84981 12481 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 12482 : asm-operand-list [opt]
12483 : asm-operand-list [opt] ) ; */
0a3b29ad 12484
12485static void
45baea8b 12486cp_parser_asm_definition (cp_parser* parser)
0a3b29ad 12487{
0a3b29ad 12488 tree string;
12489 tree outputs = NULL_TREE;
12490 tree inputs = NULL_TREE;
12491 tree clobbers = NULL_TREE;
12492 tree asm_stmt;
12493 bool volatile_p = false;
12494 bool extended_p = false;
a40e70de 12495 bool invalid_inputs_p = false;
12496 bool invalid_outputs_p = false;
0a3b29ad 12497
12498 /* Look for the `asm' keyword. */
640710cf 12499 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
0a3b29ad 12500 /* See if the next token is `volatile'. */
12501 if (cp_parser_allow_gnu_extensions_p (parser)
12502 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12503 {
12504 /* Remember that we saw the `volatile' keyword. */
12505 volatile_p = true;
12506 /* Consume the token. */
12507 cp_lexer_consume_token (parser->lexer);
12508 }
12509 /* Look for the opening `('. */
640710cf 12510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
00d26680 12511 return;
0a3b29ad 12512 /* Look for the string. */
00d26680 12513 string = cp_parser_string_literal (parser, false, false);
12514 if (string == error_mark_node)
12515 {
12516 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12517 /*consume_paren=*/true);
12518 return;
12519 }
12520
0a3b29ad 12521 /* If we're allowing GNU extensions, check for the extended assembly
ccb84981 12522 syntax. Unfortunately, the `:' tokens need not be separated by
0a3b29ad 12523 a space in C, and so, for compatibility, we tolerate that here
12524 too. Doing that means that we have to treat the `::' operator as
12525 two `:' tokens. */
12526 if (cp_parser_allow_gnu_extensions_p (parser)
0aeb1cc5 12527 && parser->in_function_body
0a3b29ad 12528 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12529 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12530 {
12531 bool inputs_p = false;
12532 bool clobbers_p = false;
12533
12534 /* The extended syntax was used. */
12535 extended_p = true;
12536
12537 /* Look for outputs. */
12538 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12539 {
12540 /* Consume the `:'. */
12541 cp_lexer_consume_token (parser->lexer);
12542 /* Parse the output-operands. */
ccb84981 12543 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 12544 CPP_COLON)
12545 && cp_lexer_next_token_is_not (parser->lexer,
f2050a0c 12546 CPP_SCOPE)
12547 && cp_lexer_next_token_is_not (parser->lexer,
12548 CPP_CLOSE_PAREN))
0a3b29ad 12549 outputs = cp_parser_asm_operand_list (parser);
a40e70de 12550
12551 if (outputs == error_mark_node)
12552 invalid_outputs_p = true;
0a3b29ad 12553 }
12554 /* If the next token is `::', there are no outputs, and the
12555 next token is the beginning of the inputs. */
12556 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8f26d225 12557 /* The inputs are coming next. */
12558 inputs_p = true;
0a3b29ad 12559
12560 /* Look for inputs. */
12561 if (inputs_p
12562 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12563 {
8f26d225 12564 /* Consume the `:' or `::'. */
12565 cp_lexer_consume_token (parser->lexer);
0a3b29ad 12566 /* Parse the output-operands. */
ccb84981 12567 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 12568 CPP_COLON)
f2050a0c 12569 && cp_lexer_next_token_is_not (parser->lexer,
12570 CPP_CLOSE_PAREN))
0a3b29ad 12571 inputs = cp_parser_asm_operand_list (parser);
a40e70de 12572
12573 if (inputs == error_mark_node)
12574 invalid_inputs_p = true;
0a3b29ad 12575 }
12576 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12577 /* The clobbers are coming next. */
12578 clobbers_p = true;
12579
12580 /* Look for clobbers. */
ccb84981 12581 if (clobbers_p
0a3b29ad 12582 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12583 {
8f26d225 12584 /* Consume the `:' or `::'. */
12585 cp_lexer_consume_token (parser->lexer);
0a3b29ad 12586 /* Parse the clobbers. */
f2050a0c 12587 if (cp_lexer_next_token_is_not (parser->lexer,
12588 CPP_CLOSE_PAREN))
12589 clobbers = cp_parser_asm_clobber_list (parser);
0a3b29ad 12590 }
12591 }
12592 /* Look for the closing `)'. */
640710cf 12593 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3d0f901b 12594 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12595 /*consume_paren=*/true);
640710cf 12596 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 12597
a40e70de 12598 if (!invalid_inputs_p && !invalid_outputs_p)
0a3b29ad 12599 {
a40e70de 12600 /* Create the ASM_EXPR. */
12601 if (parser->in_function_body)
36d35193 12602 {
a40e70de 12603 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12604 inputs, clobbers);
12605 /* If the extended syntax was not used, mark the ASM_EXPR. */
12606 if (!extended_p)
12607 {
12608 tree temp = asm_stmt;
12609 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12610 temp = TREE_OPERAND (temp, 0);
9031d10b 12611
a40e70de 12612 ASM_INPUT_P (temp) = 1;
12613 }
36d35193 12614 }
a40e70de 12615 else
12616 cgraph_add_asm_node (string);
0a3b29ad 12617 }
0a3b29ad 12618}
12619
12620/* Declarators [gram.dcl.decl] */
12621
12622/* Parse an init-declarator.
12623
12624 init-declarator:
12625 declarator initializer [opt]
12626
12627 GNU Extension:
12628
12629 init-declarator:
12630 declarator asm-specification [opt] attributes [opt] initializer [opt]
12631
92b128ed 12632 function-definition:
12633 decl-specifier-seq [opt] declarator ctor-initializer [opt]
ccb84981 12634 function-body
12635 decl-specifier-seq [opt] declarator function-try-block
92b128ed 12636
12637 GNU Extension:
12638
12639 function-definition:
ccb84981 12640 __extension__ function-definition
92b128ed 12641
23010bc8 12642 The DECL_SPECIFIERS apply to this declarator. Returns a
12643 representation of the entity declared. If MEMBER_P is TRUE, then
12644 this declarator appears in a class scope. The new DECL created by
12645 this declarator is returned.
12646
12647 The CHECKS are access checks that should be performed once we know
12648 what entity is being declared (and, therefore, what classes have
12649 befriended it).
0a3b29ad 12650
12651 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12652 for a function-definition here as well. If the declarator is a
12653 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12654 be TRUE upon return. By that point, the function-definition will
12655 have been completely parsed.
12656
12657 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12658 is FALSE. */
12659
12660static tree
ccb84981 12661cp_parser_init_declarator (cp_parser* parser,
4b9b2871 12662 cp_decl_specifier_seq *decl_specifiers,
3369eb76 12663 VEC (deferred_access_check,gc)* checks,
45baea8b 12664 bool function_definition_allowed_p,
12665 bool member_p,
8172be22 12666 int declares_class_or_enum,
45baea8b 12667 bool* function_definition_p)
0a3b29ad 12668{
ad9ae192 12669 cp_token *token = NULL, *asm_spec_start_token = NULL,
12670 *attributes_start_token = NULL;
3046c0a3 12671 cp_declarator *declarator;
4b9b2871 12672 tree prefix_attributes;
0a3b29ad 12673 tree attributes;
12674 tree asm_specification;
12675 tree initializer;
12676 tree decl = NULL_TREE;
12677 tree scope;
2336da2a 12678 int is_initialized;
393f878f 12679 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12680 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12681 "(...)". */
12682 enum cpp_ttype initialization_kind;
f82f1250 12683 bool is_direct_init = false;
878870b4 12684 bool is_non_constant_init;
0986fa22 12685 int ctor_dtor_or_conv_p;
0a3b29ad 12686 bool friend_p;
7f602bca 12687 tree pushed_scope = NULL;
0a3b29ad 12688
4b9b2871 12689 /* Gather the attributes that were provided with the
12690 decl-specifiers. */
12691 prefix_attributes = decl_specifiers->attributes;
4b9b2871 12692
0a3b29ad 12693 /* Assume that this is not the declarator for a function
12694 definition. */
12695 if (function_definition_p)
12696 *function_definition_p = false;
12697
12698 /* Defer access checks while parsing the declarator; we cannot know
ccb84981 12699 what names are accessible until we know what is being
0a3b29ad 12700 declared. */
9b57b06b 12701 resume_deferring_access_checks ();
12702
0a3b29ad 12703 /* Parse the declarator. */
ad9ae192 12704 token = cp_lexer_peek_token (parser->lexer);
ccb84981 12705 declarator
42bbd0ec 12706 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 12707 &ctor_dtor_or_conv_p,
08ea345c 12708 /*parenthesized_p=*/NULL,
12709 /*member_p=*/false);
0a3b29ad 12710 /* Gather up the deferred checks. */
9b57b06b 12711 stop_deferring_access_checks ();
7488d745 12712
0a3b29ad 12713 /* If the DECLARATOR was erroneous, there's no need to go
12714 further. */
3046c0a3 12715 if (declarator == cp_error_declarator)
9b57b06b 12716 return error_mark_node;
0a3b29ad 12717
04ef83b7 12718 /* Check that the number of template-parameter-lists is OK. */
ad9ae192 12719 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12720 token->location))
04ef83b7 12721 return error_mark_node;
12722
e2ae55f2 12723 if (declares_class_or_enum & 2)
12724 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 12725 decl_specifiers->type,
12726 decl_specifiers->type_location);
8172be22 12727
0a3b29ad 12728 /* Figure out what scope the entity declared by the DECLARATOR is
12729 located in. `grokdeclarator' sometimes changes the scope, so
12730 we compute it now. */
12731 scope = get_scope_of_declarator (declarator);
12732
12733 /* If we're allowing GNU extensions, look for an asm-specification
12734 and attributes. */
12735 if (cp_parser_allow_gnu_extensions_p (parser))
12736 {
12737 /* Look for an asm-specification. */
ad9ae192 12738 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 12739 asm_specification = cp_parser_asm_specification_opt (parser);
12740 /* And attributes. */
ad9ae192 12741 attributes_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 12742 attributes = cp_parser_attributes_opt (parser);
12743 }
12744 else
12745 {
12746 asm_specification = NULL_TREE;
12747 attributes = NULL_TREE;
12748 }
12749
12750 /* Peek at the next token. */
12751 token = cp_lexer_peek_token (parser->lexer);
12752 /* Check to see if the token indicates the start of a
12753 function-definition. */
f82f1250 12754 if (function_declarator_p (declarator)
12755 && cp_parser_token_starts_function_definition_p (token))
0a3b29ad 12756 {
12757 if (!function_definition_allowed_p)
12758 {
12759 /* If a function-definition should not appear here, issue an
12760 error message. */
12761 cp_parser_error (parser,
12762 "a function-definition is not allowed here");
12763 return error_mark_node;
12764 }
12765 else
12766 {
d19f0a18 12767 location_t func_brace_location
12768 = cp_lexer_peek_token (parser->lexer)->location;
12769
0a3b29ad 12770 /* Neither attributes nor an asm-specification are allowed
12771 on a function-definition. */
12772 if (asm_specification)
ad9ae192 12773 error ("%Han asm-specification is not allowed "
12774 "on a function-definition",
12775 &asm_spec_start_token->location);
0a3b29ad 12776 if (attributes)
ad9ae192 12777 error ("%Hattributes are not allowed on a function-definition",
12778 &attributes_start_token->location);
0a3b29ad 12779 /* This is a function-definition. */
12780 *function_definition_p = true;
12781
0a3b29ad 12782 /* Parse the function definition. */
92b128ed 12783 if (member_p)
12784 decl = cp_parser_save_member_function_body (parser,
12785 decl_specifiers,
12786 declarator,
12787 prefix_attributes);
12788 else
ccb84981 12789 decl
92b128ed 12790 = (cp_parser_function_definition_from_specifiers_and_declarator
12791 (parser, decl_specifiers, prefix_attributes, declarator));
7488d745 12792
d19f0a18 12793 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12794 {
12795 /* This is where the prologue starts... */
12796 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12797 = func_brace_location;
12798 }
12799
0a3b29ad 12800 return decl;
12801 }
12802 }
12803
12804 /* [dcl.dcl]
12805
12806 Only in function declarations for constructors, destructors, and
ccb84981 12807 type conversions can the decl-specifier-seq be omitted.
0a3b29ad 12808
12809 We explicitly postpone this check past the point where we handle
12810 function-definitions because we tolerate function-definitions
12811 that are missing their return types in some modes. */
4b9b2871 12812 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
0a3b29ad 12813 {
ccb84981 12814 cp_parser_error (parser,
0a3b29ad 12815 "expected constructor, destructor, or type conversion");
12816 return error_mark_node;
12817 }
12818
f82f1250 12819 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
393f878f 12820 if (token->type == CPP_EQ
f82f1250 12821 || token->type == CPP_OPEN_PAREN
12822 || token->type == CPP_OPEN_BRACE)
0a3b29ad 12823 {
16f0449a 12824 is_initialized = SD_INITIALIZED;
393f878f 12825 initialization_kind = token->type;
2336da2a 12826
12827 if (token->type == CPP_EQ
12828 && function_declarator_p (declarator))
12829 {
12830 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12831 if (t2->keyword == RID_DEFAULT)
16f0449a 12832 is_initialized = SD_DEFAULTED;
2336da2a 12833 else if (t2->keyword == RID_DELETE)
16f0449a 12834 is_initialized = SD_DELETED;
2336da2a 12835 }
393f878f 12836 }
12837 else
12838 {
12839 /* If the init-declarator isn't initialized and isn't followed by a
12840 `,' or `;', it's not a valid init-declarator. */
12841 if (token->type != CPP_COMMA
12842 && token->type != CPP_SEMICOLON)
12843 {
12844 cp_parser_error (parser, "expected initializer");
12845 return error_mark_node;
12846 }
16f0449a 12847 is_initialized = SD_UNINITIALIZED;
393f878f 12848 initialization_kind = CPP_EOF;
0a3b29ad 12849 }
12850
12851 /* Because start_decl has side-effects, we should only call it if we
12852 know we're going ahead. By this point, we know that we cannot
12853 possibly be looking at any other construct. */
12854 cp_parser_commit_to_tentative_parse (parser);
12855
396d2731 12856 /* If the decl specifiers were bad, issue an error now that we're
12857 sure this was intended to be a declarator. Then continue
12858 declaring the variable(s), as int, to try to cut down on further
12859 errors. */
4b9b2871 12860 if (decl_specifiers->any_specifiers_p
12861 && decl_specifiers->type == error_mark_node)
396d2731 12862 {
12863 cp_parser_error (parser, "invalid type in declaration");
4b9b2871 12864 decl_specifiers->type = integer_type_node;
396d2731 12865 }
12866
0a3b29ad 12867 /* Check to see whether or not this declaration is a friend. */
12868 friend_p = cp_parser_friend_p (decl_specifiers);
12869
0a3b29ad 12870 /* Enter the newly declared entry in the symbol table. If we're
12871 processing a declaration in a class-specifier, we wait until
12872 after processing the initializer. */
12873 if (!member_p)
12874 {
12875 if (parser->in_unbraced_linkage_specification_p)
3b289c9c 12876 decl_specifiers->storage_class = sc_extern;
8512e308 12877 decl = start_decl (declarator, decl_specifiers,
91caa6ca 12878 is_initialized, attributes, prefix_attributes,
7f602bca 12879 &pushed_scope);
0a3b29ad 12880 }
91caa6ca 12881 else if (scope)
12882 /* Enter the SCOPE. That way unqualified names appearing in the
12883 initializer will be looked up in SCOPE. */
7f602bca 12884 pushed_scope = push_scope (scope);
0a3b29ad 12885
12886 /* Perform deferred access control checks, now that we know in which
12887 SCOPE the declared entity resides. */
ccb84981 12888 if (!member_p && decl)
0a3b29ad 12889 {
12890 tree saved_current_function_decl = NULL_TREE;
12891
12892 /* If the entity being declared is a function, pretend that we
12893 are in its scope. If it is a `friend', it may have access to
6beb3f76 12894 things that would not otherwise be accessible. */
0a3b29ad 12895 if (TREE_CODE (decl) == FUNCTION_DECL)
12896 {
12897 saved_current_function_decl = current_function_decl;
12898 current_function_decl = decl;
12899 }
ccb84981 12900
23010bc8 12901 /* Perform access checks for template parameters. */
12902 cp_parser_perform_template_parameter_access_checks (checks);
12903
9b57b06b 12904 /* Perform the access control checks for the declarator and the
08cc44e7 12905 decl-specifiers. */
9b57b06b 12906 perform_deferred_access_checks ();
0a3b29ad 12907
12908 /* Restore the saved value. */
12909 if (TREE_CODE (decl) == FUNCTION_DECL)
12910 current_function_decl = saved_current_function_decl;
12911 }
12912
12913 /* Parse the initializer. */
9bbeacce 12914 initializer = NULL_TREE;
f82f1250 12915 is_direct_init = false;
9bbeacce 12916 is_non_constant_init = true;
0a3b29ad 12917 if (is_initialized)
393f878f 12918 {
95f80464 12919 if (function_declarator_p (declarator))
12920 {
ad9ae192 12921 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
95f80464 12922 if (initialization_kind == CPP_EQ)
12923 initializer = cp_parser_pure_specifier (parser);
12924 else
12925 {
12926 /* If the declaration was erroneous, we don't really
12927 know what the user intended, so just silently
12928 consume the initializer. */
12929 if (decl != error_mark_node)
ad9ae192 12930 error ("%Hinitializer provided for function",
12931 &initializer_start_token->location);
95f80464 12932 cp_parser_skip_to_closing_parenthesis (parser,
12933 /*recovering=*/true,
12934 /*or_comma=*/false,
12935 /*consume_paren=*/true);
12936 }
12937 }
393f878f 12938 else
12939 initializer = cp_parser_initializer (parser,
f82f1250 12940 &is_direct_init,
393f878f 12941 &is_non_constant_init);
12942 }
0a3b29ad 12943
12944 /* The old parser allows attributes to appear after a parenthesized
12945 initializer. Mark Mitchell proposed removing this functionality
12946 on the GCC mailing lists on 2002-08-13. This parser accepts the
12947 attributes -- but ignores them. */
f82f1250 12948 if (cp_parser_allow_gnu_extensions_p (parser)
12949 && initialization_kind == CPP_OPEN_PAREN)
0a3b29ad 12950 if (cp_parser_attributes_opt (parser))
9b2d6d13 12951 warning (OPT_Wattributes,
12952 "attributes after parenthesized initializer ignored");
0a3b29ad 12953
0a3b29ad 12954 /* For an in-class declaration, use `grokfield' to create the
12955 declaration. */
12956 if (member_p)
69b6679c 12957 {
7f602bca 12958 if (pushed_scope)
f6781b9a 12959 {
7f602bca 12960 pop_scope (pushed_scope);
12961 pushed_scope = false;
f6781b9a 12962 }
69b6679c 12963 decl = grokfield (declarator, decl_specifiers,
d91303a6 12964 initializer, !is_non_constant_init,
12965 /*asmspec=*/NULL_TREE,
d246ef36 12966 prefix_attributes);
69b6679c 12967 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12968 cp_parser_save_default_args (parser, decl);
12969 }
ccb84981 12970
0a3b29ad 12971 /* Finish processing the declaration. But, skip friend
12972 declarations. */
d1cd2603 12973 if (!friend_p && decl && decl != error_mark_node)
91caa6ca 12974 {
12975 cp_finish_decl (decl,
d91303a6 12976 initializer, !is_non_constant_init,
91caa6ca 12977 asm_specification,
12978 /* If the initializer is in parentheses, then this is
12979 a direct-initialization, which means that an
12980 `explicit' constructor is OK. Otherwise, an
12981 `explicit' constructor cannot be used. */
f82f1250 12982 ((is_direct_init || !is_initialized)
12983 ? 0 : LOOKUP_ONLYCONVERTING));
91caa6ca 12984 }
6dcdb5de 12985 else if ((cxx_dialect != cxx98) && friend_p
12986 && decl && TREE_CODE (decl) == FUNCTION_DECL)
82d31768 12987 /* Core issue #226 (C++0x only): A default template-argument
12988 shall not be specified in a friend class template
12989 declaration. */
12990 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12991 /*is_partial=*/0, /*is_friend_decl=*/1);
12992
7f602bca 12993 if (!friend_p && pushed_scope)
12994 pop_scope (pushed_scope);
0a3b29ad 12995
12996 return decl;
12997}
12998
12999/* Parse a declarator.
ccb84981 13000
0a3b29ad 13001 declarator:
13002 direct-declarator
ccb84981 13003 ptr-operator declarator
0a3b29ad 13004
13005 abstract-declarator:
13006 ptr-operator abstract-declarator [opt]
13007 direct-abstract-declarator
13008
13009 GNU Extensions:
13010
13011 declarator:
13012 attributes [opt] direct-declarator
ccb84981 13013 attributes [opt] ptr-operator declarator
0a3b29ad 13014
13015 abstract-declarator:
13016 attributes [opt] ptr-operator abstract-declarator [opt]
13017 attributes [opt] direct-abstract-declarator
ccb84981 13018
0986fa22 13019 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13020 detect constructor, destructor or conversion operators. It is set
13021 to -1 if the declarator is a name, and +1 if it is a
13022 function. Otherwise it is set to zero. Usually you just want to
13023 test for >0, but internally the negative value is used.
ccb84981 13024
0a3b29ad 13025 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13026 a decl-specifier-seq unless it declares a constructor, destructor,
13027 or conversion. It might seem that we could check this condition in
13028 semantic analysis, rather than parsing, but that makes it difficult
13029 to handle something like `f()'. We want to notice that there are
13030 no decl-specifiers, and therefore realize that this is an
ccb84981 13031 expression, not a declaration.)
13032
92b128ed 13033 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9031d10b 13034 the declarator is a direct-declarator of the form "(...)".
08ea345c 13035
13036 MEMBER_P is true iff this declarator is a member-declarator. */
0a3b29ad 13037
3046c0a3 13038static cp_declarator *
ccb84981 13039cp_parser_declarator (cp_parser* parser,
653e5405 13040 cp_parser_declarator_kind dcl_kind,
13041 int* ctor_dtor_or_conv_p,
08ea345c 13042 bool* parenthesized_p,
13043 bool member_p)
0a3b29ad 13044{
13045 cp_token *token;
3046c0a3 13046 cp_declarator *declarator;
0a3b29ad 13047 enum tree_code code;
2cfb6cde 13048 cp_cv_quals cv_quals;
0a3b29ad 13049 tree class_type;
13050 tree attributes = NULL_TREE;
13051
13052 /* Assume this is not a constructor, destructor, or type-conversion
13053 operator. */
13054 if (ctor_dtor_or_conv_p)
0986fa22 13055 *ctor_dtor_or_conv_p = 0;
0a3b29ad 13056
13057 if (cp_parser_allow_gnu_extensions_p (parser))
13058 attributes = cp_parser_attributes_opt (parser);
ccb84981 13059
0a3b29ad 13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
ccb84981 13062
0a3b29ad 13063 /* Check for the ptr-operator production. */
13064 cp_parser_parse_tentatively (parser);
13065 /* Parse the ptr-operator. */
ccb84981 13066 code = cp_parser_ptr_operator (parser,
13067 &class_type,
2cfb6cde 13068 &cv_quals);
0a3b29ad 13069 /* If that worked, then we have a ptr-operator. */
13070 if (cp_parser_parse_definitely (parser))
13071 {
92b128ed 13072 /* If a ptr-operator was found, then this declarator was not
13073 parenthesized. */
13074 if (parenthesized_p)
13075 *parenthesized_p = true;
0a3b29ad 13076 /* The dependent declarator is optional if we are parsing an
13077 abstract-declarator. */
42bbd0ec 13078 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 13079 cp_parser_parse_tentatively (parser);
13080
13081 /* Parse the dependent declarator. */
42bbd0ec 13082 declarator = cp_parser_declarator (parser, dcl_kind,
92b128ed 13083 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 13084 /*parenthesized_p=*/NULL,
13085 /*member_p=*/false);
0a3b29ad 13086
13087 /* If we are parsing an abstract-declarator, we must handle the
13088 case where the dependent declarator is absent. */
42bbd0ec 13089 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13090 && !cp_parser_parse_definitely (parser))
3046c0a3 13091 declarator = NULL;
ccb84981 13092
63949b38 13093 declarator = cp_parser_make_indirect_declarator
13094 (code, class_type, cv_quals, declarator);
0a3b29ad 13095 }
13096 /* Everything else is a direct-declarator. */
13097 else
92b128ed 13098 {
13099 if (parenthesized_p)
13100 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13101 CPP_OPEN_PAREN);
13102 declarator = cp_parser_direct_declarator (parser, dcl_kind,
08ea345c 13103 ctor_dtor_or_conv_p,
13104 member_p);
92b128ed 13105 }
0a3b29ad 13106
ba0c587d 13107 if (attributes && declarator && declarator != cp_error_declarator)
3046c0a3 13108 declarator->attributes = attributes;
ccb84981 13109
0a3b29ad 13110 return declarator;
13111}
13112
13113/* Parse a direct-declarator or direct-abstract-declarator.
13114
13115 direct-declarator:
13116 declarator-id
13117 direct-declarator ( parameter-declaration-clause )
ccb84981 13118 cv-qualifier-seq [opt]
0a3b29ad 13119 exception-specification [opt]
13120 direct-declarator [ constant-expression [opt] ]
ccb84981 13121 ( declarator )
0a3b29ad 13122
13123 direct-abstract-declarator:
13124 direct-abstract-declarator [opt]
ccb84981 13125 ( parameter-declaration-clause )
0a3b29ad 13126 cv-qualifier-seq [opt]
13127 exception-specification [opt]
13128 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13129 ( abstract-declarator )
13130
42bbd0ec 13131 Returns a representation of the declarator. DCL_KIND is
13132 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13133 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13134 we are parsing a direct-declarator. It is
13135 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13136 of ambiguity we prefer an abstract declarator, as per
08ea345c 13137 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
3046c0a3 13138 cp_parser_declarator. */
0a3b29ad 13139
3046c0a3 13140static cp_declarator *
45baea8b 13141cp_parser_direct_declarator (cp_parser* parser,
653e5405 13142 cp_parser_declarator_kind dcl_kind,
13143 int* ctor_dtor_or_conv_p,
08ea345c 13144 bool member_p)
0a3b29ad 13145{
13146 cp_token *token;
3046c0a3 13147 cp_declarator *declarator = NULL;
0a3b29ad 13148 tree scope = NULL_TREE;
13149 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13150 bool saved_in_declarator_p = parser->in_declarator_p;
42bbd0ec 13151 bool first = true;
7f602bca 13152 tree pushed_scope = NULL_TREE;
ccb84981 13153
42bbd0ec 13154 while (true)
0a3b29ad 13155 {
42bbd0ec 13156 /* Peek at the next token. */
13157 token = cp_lexer_peek_token (parser->lexer);
13158 if (token->type == CPP_OPEN_PAREN)
0a3b29ad 13159 {
42bbd0ec 13160 /* This is either a parameter-declaration-clause, or a
653e5405 13161 parenthesized declarator. When we know we are parsing a
13162 named declarator, it must be a parenthesized declarator
13163 if FIRST is true. For instance, `(int)' is a
13164 parameter-declaration-clause, with an omitted
13165 direct-abstract-declarator. But `((*))', is a
13166 parenthesized abstract declarator. Finally, when T is a
13167 template parameter `(T)' is a
13168 parameter-declaration-clause, and not a parenthesized
13169 named declarator.
ccb84981 13170
42bbd0ec 13171 We first try and parse a parameter-declaration-clause,
13172 and then try a nested declarator (if FIRST is true).
0a3b29ad 13173
42bbd0ec 13174 It is not an error for it not to be a
13175 parameter-declaration-clause, even when FIRST is
13176 false. Consider,
13177
13178 int i (int);
13179 int i (3);
13180
13181 The first is the declaration of a function while the
08cc44e7 13182 second is the definition of a variable, including its
42bbd0ec 13183 initializer.
13184
13185 Having seen only the parenthesis, we cannot know which of
13186 these two alternatives should be selected. Even more
13187 complex are examples like:
13188
653e5405 13189 int i (int (a));
42bbd0ec 13190 int i (int (3));
13191
13192 The former is a function-declaration; the latter is a
ccb84981 13193 variable initialization.
42bbd0ec 13194
755edffd 13195 Thus again, we try a parameter-declaration-clause, and if
42bbd0ec 13196 that fails, we back out and return. */
13197
13198 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 13199 {
34eac767 13200 tree params;
bb91f165 13201 unsigned saved_num_template_parameter_lists;
34eac767 13202 bool is_declarator = false;
13203 tree t;
ccb84981 13204
08ea345c 13205 /* In a member-declarator, the only valid interpretation
13206 of a parenthesis is the start of a
13207 parameter-declaration-clause. (It is invalid to
13208 initialize a static data member with a parenthesized
13209 initializer; only the "=" form of initialization is
13210 permitted.) */
13211 if (!member_p)
13212 cp_parser_parse_tentatively (parser);
0a3b29ad 13213
42bbd0ec 13214 /* Consume the `('. */
13215 cp_lexer_consume_token (parser->lexer);
13216 if (first)
13217 {
13218 /* If this is going to be an abstract declarator, we're
13219 in a declarator and we can't have default args. */
13220 parser->default_arg_ok_p = false;
13221 parser->in_declarator_p = true;
13222 }
ccb84981 13223
bb91f165 13224 /* Inside the function parameter list, surrounding
13225 template-parameter-lists do not apply. */
13226 saved_num_template_parameter_lists
13227 = parser->num_template_parameter_lists;
13228 parser->num_template_parameter_lists = 0;
13229
34eac767 13230 begin_scope (sk_function_parms, NULL_TREE);
13231
42bbd0ec 13232 /* Parse the parameter-declaration-clause. */
13233 params = cp_parser_parameter_declaration_clause (parser);
13234
bb91f165 13235 parser->num_template_parameter_lists
13236 = saved_num_template_parameter_lists;
13237
42bbd0ec 13238 /* If all went well, parse the cv-qualifier-seq and the
653e5405 13239 exception-specification. */
08ea345c 13240 if (member_p || cp_parser_parse_definitely (parser))
42bbd0ec 13241 {
2cfb6cde 13242 cp_cv_quals cv_quals;
42bbd0ec 13243 tree exception_specification;
346e3a9c 13244 tree late_return;
0986fa22 13245
34eac767 13246 is_declarator = true;
13247
0986fa22 13248 if (ctor_dtor_or_conv_p)
13249 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
42bbd0ec 13250 first = false;
13251 /* Consume the `)'. */
640710cf 13252 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
42bbd0ec 13253
13254 /* Parse the cv-qualifier-seq. */
2cfb6cde 13255 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
42bbd0ec 13256 /* And the exception-specification. */
ccb84981 13257 exception_specification
42bbd0ec 13258 = cp_parser_exception_specification_opt (parser);
13259
346e3a9c 13260 late_return
13261 = cp_parser_late_return_type_opt (parser);
13262
42bbd0ec 13263 /* Create the function-declarator. */
13264 declarator = make_call_declarator (declarator,
13265 params,
2cfb6cde 13266 cv_quals,
346e3a9c 13267 exception_specification,
13268 late_return);
42bbd0ec 13269 /* Any subsequent parameter lists are to do with
653e5405 13270 return type, so are not those of the declared
13271 function. */
42bbd0ec 13272 parser->default_arg_ok_p = false;
42bbd0ec 13273 }
34eac767 13274
13275 /* Remove the function parms from scope. */
13276 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13277 pop_binding (DECL_NAME (t), t);
13278 leave_scope();
13279
13280 if (is_declarator)
13281 /* Repeat the main loop. */
13282 continue;
42bbd0ec 13283 }
ccb84981 13284
42bbd0ec 13285 /* If this is the first, we can try a parenthesized
13286 declarator. */
13287 if (first)
0a3b29ad 13288 {
91f31809 13289 bool saved_in_type_id_in_expr_p;
13290
0a3b29ad 13291 parser->default_arg_ok_p = saved_default_arg_ok_p;
42bbd0ec 13292 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 13293
42bbd0ec 13294 /* Consume the `('. */
13295 cp_lexer_consume_token (parser->lexer);
13296 /* Parse the nested declarator. */
91f31809 13297 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13298 parser->in_type_id_in_expr_p = true;
ccb84981 13299 declarator
92b128ed 13300 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
08ea345c 13301 /*parenthesized_p=*/NULL,
13302 member_p);
91f31809 13303 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
42bbd0ec 13304 first = false;
13305 /* Expect a `)'. */
640710cf 13306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3046c0a3 13307 declarator = cp_error_declarator;
13308 if (declarator == cp_error_declarator)
42bbd0ec 13309 break;
ccb84981 13310
42bbd0ec 13311 goto handle_declarator;
0a3b29ad 13312 }
6beb3f76 13313 /* Otherwise, we must be done. */
42bbd0ec 13314 else
13315 break;
0a3b29ad 13316 }
42bbd0ec 13317 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13318 && token->type == CPP_OPEN_SQUARE)
0a3b29ad 13319 {
42bbd0ec 13320 /* Parse an array-declarator. */
0a3b29ad 13321 tree bounds;
13322
0986fa22 13323 if (ctor_dtor_or_conv_p)
13324 *ctor_dtor_or_conv_p = 0;
ccb84981 13325
42bbd0ec 13326 first = false;
13327 parser->default_arg_ok_p = false;
13328 parser->in_declarator_p = true;
0a3b29ad 13329 /* Consume the `['. */
13330 cp_lexer_consume_token (parser->lexer);
13331 /* Peek at the next token. */
13332 token = cp_lexer_peek_token (parser->lexer);
13333 /* If the next token is `]', then there is no
13334 constant-expression. */
13335 if (token->type != CPP_CLOSE_SQUARE)
5f6526e1 13336 {
13337 bool non_constant_p;
13338
ccb84981 13339 bounds
5f6526e1 13340 = cp_parser_constant_expression (parser,
13341 /*allow_non_constant=*/true,
13342 &non_constant_p);
13795292 13343 if (!non_constant_p)
2250d32c 13344 bounds = fold_non_dependent_expr (bounds);
f0d4a607 13345 /* Normally, the array bound must be an integral constant
13346 expression. However, as an extension, we allow VLAs
9031d10b 13347 in function scopes. */
0aeb1cc5 13348 else if (!parser->in_function_body)
8b652e89 13349 {
ad9ae192 13350 error ("%Harray bound is not an integer constant",
13351 &token->location);
8b652e89 13352 bounds = error_mark_node;
13353 }
d1564595 13354 else if (processing_template_decl && !error_operand_p (bounds))
13355 {
13356 /* Remember this wasn't a constant-expression. */
13357 bounds = build_nop (TREE_TYPE (bounds), bounds);
13358 TREE_SIDE_EFFECTS (bounds) = 1;
13359 }
5f6526e1 13360 }
0a3b29ad 13361 else
13362 bounds = NULL_TREE;
13363 /* Look for the closing `]'. */
640710cf 13364 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
42bbd0ec 13365 {
3046c0a3 13366 declarator = cp_error_declarator;
42bbd0ec 13367 break;
13368 }
0a3b29ad 13369
3046c0a3 13370 declarator = make_array_declarator (declarator, bounds);
0a3b29ad 13371 }
42bbd0ec 13372 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
0a3b29ad 13373 {
2ded3667 13374 tree qualifying_scope;
13375 tree unqualified_name;
2366ed31 13376 special_function_kind sfk;
197c9df7 13377 bool abstract_ok;
d95d815d 13378 bool pack_expansion_p = false;
ad9ae192 13379 cp_token *declarator_id_start_token;
3046c0a3 13380
3d0f901b 13381 /* Parse a declarator-id */
197c9df7 13382 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13383 if (abstract_ok)
d95d815d 13384 {
13385 cp_parser_parse_tentatively (parser);
13386
13387 /* If we see an ellipsis, we should be looking at a
13388 parameter pack. */
13389 if (token->type == CPP_ELLIPSIS)
13390 {
13391 /* Consume the `...' */
13392 cp_lexer_consume_token (parser->lexer);
13393
13394 pack_expansion_p = true;
13395 }
13396 }
13397
ad9ae192 13398 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
074ab442 13399 unqualified_name
197c9df7 13400 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
2ded3667 13401 qualifying_scope = parser->scope;
197c9df7 13402 if (abstract_ok)
5ea97769 13403 {
d95d815d 13404 bool okay = false;
13405
13406 if (!unqualified_name && pack_expansion_p)
13407 {
13408 /* Check whether an error occurred. */
13409 okay = !cp_parser_error_occurred (parser);
13410
13411 /* We already consumed the ellipsis to mark a
13412 parameter pack, but we have no way to report it,
13413 so abort the tentative parse. We will be exiting
13414 immediately anyway. */
13415 cp_parser_abort_tentative_parse (parser);
13416 }
13417 else
13418 okay = cp_parser_parse_definitely (parser);
13419
13420 if (!okay)
2ded3667 13421 unqualified_name = error_mark_node;
197c9df7 13422 else if (unqualified_name
13423 && (qualifying_scope
13424 || (TREE_CODE (unqualified_name)
13425 != IDENTIFIER_NODE)))
5ea97769 13426 {
13427 cp_parser_error (parser, "expected unqualified-id");
2ded3667 13428 unqualified_name = error_mark_node;
5ea97769 13429 }
13430 }
ccb84981 13431
197c9df7 13432 if (!unqualified_name)
13433 return NULL;
2ded3667 13434 if (unqualified_name == error_mark_node)
3046c0a3 13435 {
13436 declarator = cp_error_declarator;
d95d815d 13437 pack_expansion_p = false;
13438 declarator->parameter_pack_p = false;
3046c0a3 13439 break;
13440 }
ccb84981 13441
2ded3667 13442 if (qualifying_scope && at_namespace_scope_p ()
13443 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
42bbd0ec 13444 {
42bbd0ec 13445 /* In the declaration of a member of a template class
653e5405 13446 outside of the class itself, the SCOPE will sometimes
13447 be a TYPENAME_TYPE. For example, given:
ccb84981 13448
653e5405 13449 template <typename T>
13450 int S<T>::R::i = 3;
ccb84981 13451
653e5405 13452 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13453 this context, we must resolve S<T>::R to an ordinary
13454 type, rather than a typename type.
ccb84981 13455
653e5405 13456 The reason we normally avoid resolving TYPENAME_TYPEs
13457 is that a specialization of `S' might render
13458 `S<T>::R' not a type. However, if `S' is
13459 specialized, then this `i' will not be used, so there
13460 is no harm in resolving the types here. */
2ded3667 13461 tree type;
9031d10b 13462
2ded3667 13463 /* Resolve the TYPENAME_TYPE. */
13464 type = resolve_typename_type (qualifying_scope,
13465 /*only_current_p=*/false);
13466 /* If that failed, the declarator is invalid. */
8826a863 13467 if (TREE_CODE (type) == TYPENAME_TYPE)
ad9ae192 13468 error ("%H%<%T::%E%> is not a type",
13469 &declarator_id_start_token->location,
2ded3667 13470 TYPE_CONTEXT (qualifying_scope),
13471 TYPE_IDENTIFIER (qualifying_scope));
13472 qualifying_scope = type;
42bbd0ec 13473 }
ccb84981 13474
2366ed31 13475 sfk = sfk_none;
d95d815d 13476
2ded3667 13477 if (unqualified_name)
0a3b29ad 13478 {
42bbd0ec 13479 tree class_type;
13480
2ded3667 13481 if (qualifying_scope
13482 && CLASS_TYPE_P (qualifying_scope))
13483 class_type = qualifying_scope;
42bbd0ec 13484 else
2ded3667 13485 class_type = current_class_type;
42bbd0ec 13486
2366ed31 13487 if (TREE_CODE (unqualified_name) == TYPE_DECL)
3046c0a3 13488 {
e1b0710d 13489 tree name_type = TREE_TYPE (unqualified_name);
13490 if (class_type && same_type_p (name_type, class_type))
3046c0a3 13491 {
e1b0710d 13492 if (qualifying_scope
13493 && CLASSTYPE_USE_TEMPLATE (name_type))
13494 {
ad9ae192 13495 error ("%Hinvalid use of constructor as a template",
13496 &declarator_id_start_token->location);
5bcc316e 13497 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
e1b0710d 13498 "name the constructor in a qualified name",
13499 class_type,
13500 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13501 class_type, name_type);
13502 declarator = cp_error_declarator;
13503 break;
13504 }
13505 else
13506 unqualified_name = constructor_name (class_type);
2366ed31 13507 }
2366ed31 13508 else
13509 {
13510 /* We do not attempt to print the declarator
13511 here because we do not have enough
13512 information about its original syntactic
13513 form. */
a286098e 13514 cp_parser_error (parser, "invalid declarator");
2366ed31 13515 declarator = cp_error_declarator;
13516 break;
3046c0a3 13517 }
13518 }
2366ed31 13519
13520 if (class_type)
13521 {
13522 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13523 sfk = sfk_destructor;
13524 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13525 sfk = sfk_conversion;
13526 else if (/* There's no way to declare a constructor
13527 for an anonymous type, even if the type
13528 got a name for linkage purposes. */
13529 !TYPE_WAS_ANONYMOUS (class_type)
13530 && constructor_name_p (unqualified_name,
13531 class_type))
13532 {
13533 unqualified_name = constructor_name (class_type);
13534 sfk = sfk_constructor;
13535 }
13536
13537 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13538 *ctor_dtor_or_conv_p = -1;
13539 }
0a3b29ad 13540 }
074ab442 13541 declarator = make_id_declarator (qualifying_scope,
2366ed31 13542 unqualified_name,
13543 sfk);
13544 declarator->id_loc = token->location;
d95d815d 13545 declarator->parameter_pack_p = pack_expansion_p;
13546
13547 if (pack_expansion_p)
13548 maybe_warn_variadic_templates ();
42bbd0ec 13549
13550 handle_declarator:;
13551 scope = get_scope_of_declarator (declarator);
13552 if (scope)
1cbda81f 13553 /* Any names that appear after the declarator-id for a
13554 member are looked up in the containing scope. */
7f602bca 13555 pushed_scope = push_scope (scope);
42bbd0ec 13556 parser->in_declarator_p = true;
13557 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
3046c0a3 13558 || (declarator && declarator->kind == cdk_id))
42bbd0ec 13559 /* Default args are only allowed on function
13560 declarations. */
13561 parser->default_arg_ok_p = saved_default_arg_ok_p;
0a3b29ad 13562 else
42bbd0ec 13563 parser->default_arg_ok_p = false;
13564
13565 first = false;
0a3b29ad 13566 }
42bbd0ec 13567 /* We're done. */
0a3b29ad 13568 else
13569 break;
0a3b29ad 13570 }
13571
13572 /* For an abstract declarator, we might wind up with nothing at this
13573 point. That's an error; the declarator is not optional. */
13574 if (!declarator)
13575 cp_parser_error (parser, "expected declarator");
13576
13577 /* If we entered a scope, we must exit it now. */
7f602bca 13578 if (pushed_scope)
13579 pop_scope (pushed_scope);
0a3b29ad 13580
13581 parser->default_arg_ok_p = saved_default_arg_ok_p;
13582 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 13583
0a3b29ad 13584 return declarator;
13585}
13586
ccb84981 13587/* Parse a ptr-operator.
0a3b29ad 13588
13589 ptr-operator:
13590 * cv-qualifier-seq [opt]
13591 &
13592 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13593
13594 GNU Extension:
13595
13596 ptr-operator:
13597 & cv-qualifier-seq [opt]
13598
2cfb6cde 13599 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
63949b38 13600 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13601 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13602 filled in with the TYPE containing the member. *CV_QUALS is
13603 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13604 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13605 Note that the tree codes returned by this function have nothing
13606 to do with the types of trees that will be eventually be created
13607 to represent the pointer or reference type being parsed. They are
13608 just constants with suggestive names. */
0a3b29ad 13609static enum tree_code
ccb84981 13610cp_parser_ptr_operator (cp_parser* parser,
653e5405 13611 tree* type,
2cfb6cde 13612 cp_cv_quals *cv_quals)
0a3b29ad 13613{
13614 enum tree_code code = ERROR_MARK;
13615 cp_token *token;
13616
13617 /* Assume that it's not a pointer-to-member. */
13618 *type = NULL_TREE;
13619 /* And that there are no cv-qualifiers. */
2cfb6cde 13620 *cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 13621
13622 /* Peek at the next token. */
13623 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13624
63949b38 13625 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13626 if (token->type == CPP_MULT)
13627 code = INDIRECT_REF;
13628 else if (token->type == CPP_AND)
13629 code = ADDR_EXPR;
6dcdb5de 13630 else if ((cxx_dialect != cxx98) &&
13631 token->type == CPP_AND_AND) /* C++0x only */
63949b38 13632 code = NON_LVALUE_EXPR;
13633
13634 if (code != ERROR_MARK)
13635 {
13636 /* Consume the `*', `&' or `&&'. */
0a3b29ad 13637 cp_lexer_consume_token (parser->lexer);
13638
13639 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13640 `&', if we are allowing GNU extensions. (The only qualifier
13641 that can legally appear after `&' is `restrict', but that is
13642 enforced during semantic analysis. */
ccb84981 13643 if (code == INDIRECT_REF
0a3b29ad 13644 || cp_parser_allow_gnu_extensions_p (parser))
2cfb6cde 13645 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
0a3b29ad 13646 }
13647 else
13648 {
13649 /* Try the pointer-to-member case. */
13650 cp_parser_parse_tentatively (parser);
13651 /* Look for the optional `::' operator. */
13652 cp_parser_global_scope_opt (parser,
130bb1d4 13653 /*current_scope_valid_p=*/false);
0a3b29ad 13654 /* Look for the nested-name specifier. */
ad9ae192 13655 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13656 cp_parser_nested_name_specifier (parser,
13657 /*typename_keyword_p=*/false,
13658 /*check_dependency_p=*/true,
3d0f901b 13659 /*type_p=*/false,
13660 /*is_declaration=*/false);
0a3b29ad 13661 /* If we found it, and the next token is a `*', then we are
13662 indeed looking at a pointer-to-member operator. */
13663 if (!cp_parser_error_occurred (parser)
640710cf 13664 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
0a3b29ad 13665 {
0a3b29ad 13666 /* Indicate that the `*' operator was used. */
13667 code = INDIRECT_REF;
393f878f 13668
13669 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
ad9ae192 13670 error ("%H%qD is a namespace", &token->location, parser->scope);
393f878f 13671 else
13672 {
13673 /* The type of which the member is a member is given by the
13674 current SCOPE. */
13675 *type = parser->scope;
13676 /* The next name will not be qualified. */
13677 parser->scope = NULL_TREE;
13678 parser->qualifying_scope = NULL_TREE;
13679 parser->object_scope = NULL_TREE;
13680 /* Look for the optional cv-qualifier-seq. */
13681 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13682 }
0a3b29ad 13683 }
13684 /* If that didn't work we don't have a ptr-operator. */
13685 if (!cp_parser_parse_definitely (parser))
13686 cp_parser_error (parser, "expected ptr-operator");
13687 }
13688
13689 return code;
13690}
13691
13692/* Parse an (optional) cv-qualifier-seq.
13693
13694 cv-qualifier-seq:
ccb84981 13695 cv-qualifier cv-qualifier-seq [opt]
0a3b29ad 13696
0a3b29ad 13697 cv-qualifier:
13698 const
ccb84981 13699 volatile
0a3b29ad 13700
13701 GNU Extension:
13702
13703 cv-qualifier:
207355ad 13704 __restrict__
0a3b29ad 13705
2cfb6cde 13706 Returns a bitmask representing the cv-qualifiers. */
13707
13708static cp_cv_quals
13709cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
0a3b29ad 13710{
2cfb6cde 13711 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 13712
2cfb6cde 13713 while (true)
0a3b29ad 13714 {
2cfb6cde 13715 cp_token *token;
13716 cp_cv_quals cv_qualifier;
207355ad 13717
2cfb6cde 13718 /* Peek at the next token. */
13719 token = cp_lexer_peek_token (parser->lexer);
13720 /* See if it's a cv-qualifier. */
13721 switch (token->keyword)
13722 {
13723 case RID_CONST:
13724 cv_qualifier = TYPE_QUAL_CONST;
13725 break;
207355ad 13726
2cfb6cde 13727 case RID_VOLATILE:
13728 cv_qualifier = TYPE_QUAL_VOLATILE;
13729 break;
207355ad 13730
2cfb6cde 13731 case RID_RESTRICT:
13732 cv_qualifier = TYPE_QUAL_RESTRICT;
13733 break;
207355ad 13734
2cfb6cde 13735 default:
13736 cv_qualifier = TYPE_UNQUALIFIED;
13737 break;
13738 }
207355ad 13739
2cfb6cde 13740 if (!cv_qualifier)
13741 break;
0a3b29ad 13742
2cfb6cde 13743 if (cv_quals & cv_qualifier)
13744 {
ad9ae192 13745 error ("%Hduplicate cv-qualifier", &token->location);
2cfb6cde 13746 cp_lexer_purge_token (parser->lexer);
13747 }
13748 else
13749 {
13750 cp_lexer_consume_token (parser->lexer);
13751 cv_quals |= cv_qualifier;
13752 }
0a3b29ad 13753 }
13754
2cfb6cde 13755 return cv_quals;
0a3b29ad 13756}
13757
346e3a9c 13758/* Parse a late-specified return type, if any. This is not a separate
13759 non-terminal, but part of a function declarator, which looks like
13760
13761 -> type-id
13762
13763 Returns the type indicated by the type-id. */
13764
13765static tree
13766cp_parser_late_return_type_opt (cp_parser* parser)
13767{
13768 cp_token *token;
13769
13770 /* Peek at the next token. */
13771 token = cp_lexer_peek_token (parser->lexer);
13772 /* A late-specified return type is indicated by an initial '->'. */
13773 if (token->type != CPP_DEREF)
13774 return NULL_TREE;
13775
13776 /* Consume the ->. */
13777 cp_lexer_consume_token (parser->lexer);
13778
13779 return cp_parser_type_id (parser);
13780}
13781
0a3b29ad 13782/* Parse a declarator-id.
13783
13784 declarator-id:
13785 id-expression
ccb84981 13786 :: [opt] nested-name-specifier [opt] type-name
0a3b29ad 13787
13788 In the `id-expression' case, the value returned is as for
13789 cp_parser_id_expression if the id-expression was an unqualified-id.
13790 If the id-expression was a qualified-id, then a SCOPE_REF is
13791 returned. The first operand is the scope (either a NAMESPACE_DECL
13792 or TREE_TYPE), but the second is still just a representation of an
13793 unqualified-id. */
13794
13795static tree
197c9df7 13796cp_parser_declarator_id (cp_parser* parser, bool optional_p)
0a3b29ad 13797{
2366ed31 13798 tree id;
0a3b29ad 13799 /* The expression must be an id-expression. Assume that qualified
13800 names are the names of types so that:
13801
13802 template <class T>
13803 int S<T>::R::i = 3;
13804
13805 will work; we must treat `S<T>::R' as the name of a type.
13806 Similarly, assume that qualified names are templates, where
13807 required, so that:
13808
13809 template <class T>
13810 int S<T>::R<T>::i = 3;
13811
13812 will work, too. */
2366ed31 13813 id = cp_parser_id_expression (parser,
13814 /*template_keyword_p=*/false,
13815 /*check_dependency_p=*/false,
13816 /*template_p=*/NULL,
197c9df7 13817 /*declarator_p=*/true,
130bb1d4 13818 optional_p);
197c9df7 13819 if (id && BASELINK_P (id))
2366ed31 13820 id = BASELINK_FUNCTIONS (id);
13821 return id;
0a3b29ad 13822}
13823
13824/* Parse a type-id.
13825
13826 type-id:
13827 type-specifier-seq abstract-declarator [opt]
13828
13829 Returns the TYPE specified. */
13830
13831static tree
75eaa947 13832cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
0a3b29ad 13833{
4b9b2871 13834 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 13835 cp_declarator *abstract_declarator;
0a3b29ad 13836
13837 /* Parse the type-specifier-seq. */
6f74fe3c 13838 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13839 &type_specifier_seq);
4b9b2871 13840 if (type_specifier_seq.type == error_mark_node)
0a3b29ad 13841 return error_mark_node;
13842
13843 /* There might or might not be an abstract declarator. */
13844 cp_parser_parse_tentatively (parser);
13845 /* Look for the declarator. */
ccb84981 13846 abstract_declarator
92b128ed 13847 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
08ea345c 13848 /*parenthesized_p=*/NULL,
13849 /*member_p=*/false);
0a3b29ad 13850 /* Check to see if there really was a declarator. */
13851 if (!cp_parser_parse_definitely (parser))
3046c0a3 13852 abstract_declarator = NULL;
0a3b29ad 13853
475fd34e 13854 if (type_specifier_seq.type
13855 && type_uses_auto (type_specifier_seq.type))
13856 {
e439140e 13857 /* A type-id with type 'auto' is only ok if the abstract declarator
13858 is a function declarator with a late-specified return type. */
13859 if (abstract_declarator
13860 && abstract_declarator->kind == cdk_function
13861 && abstract_declarator->u.function.late_return_type)
13862 /* OK */;
13863 else
13864 {
13865 error ("invalid use of %<auto%>");
13866 return error_mark_node;
13867 }
475fd34e 13868 }
13869
75eaa947 13870 return groktypename (&type_specifier_seq, abstract_declarator,
13871 is_template_arg);
13872}
13873
13874static tree cp_parser_type_id (cp_parser *parser)
13875{
13876 return cp_parser_type_id_1 (parser, false);
13877}
13878
13879static tree cp_parser_template_type_arg (cp_parser *parser)
13880{
13881 return cp_parser_type_id_1 (parser, true);
0a3b29ad 13882}
13883
13884/* Parse a type-specifier-seq.
13885
13886 type-specifier-seq:
13887 type-specifier type-specifier-seq [opt]
13888
13889 GNU extension:
13890
13891 type-specifier-seq:
13892 attributes type-specifier-seq [opt]
13893
6f74fe3c 13894 If IS_CONDITION is true, we are at the start of a "condition",
13895 e.g., we've just seen "if (".
13896
4b9b2871 13897 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
0a3b29ad 13898
4b9b2871 13899static void
13900cp_parser_type_specifier_seq (cp_parser* parser,
6f74fe3c 13901 bool is_condition,
4b9b2871 13902 cp_decl_specifier_seq *type_specifier_seq)
0a3b29ad 13903{
13904 bool seen_type_specifier = false;
6f74fe3c 13905 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
ad9ae192 13906 cp_token *start_token = NULL;
4b9b2871 13907
13908 /* Clear the TYPE_SPECIFIER_SEQ. */
13909 clear_decl_specs (type_specifier_seq);
0a3b29ad 13910
13911 /* Parse the type-specifiers and attributes. */
13912 while (true)
13913 {
13914 tree type_specifier;
6f74fe3c 13915 bool is_cv_qualifier;
0a3b29ad 13916
13917 /* Check for attributes first. */
13918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13919 {
207355ad 13920 type_specifier_seq->attributes =
4b9b2871 13921 chainon (type_specifier_seq->attributes,
13922 cp_parser_attributes_opt (parser));
0a3b29ad 13923 continue;
13924 }
13925
ad9ae192 13926 /* record the token of the beginning of the type specifier seq,
13927 for error reporting purposes*/
13928 if (!start_token)
13929 start_token = cp_lexer_peek_token (parser->lexer);
13930
0a3b29ad 13931 /* Look for the type-specifier. */
ccb84981 13932 type_specifier = cp_parser_type_specifier (parser,
6f74fe3c 13933 flags,
4b9b2871 13934 type_specifier_seq,
0a3b29ad 13935 /*is_declaration=*/false,
13936 NULL,
6f74fe3c 13937 &is_cv_qualifier);
13938 if (!type_specifier)
4b9b2871 13939 {
6f74fe3c 13940 /* If the first type-specifier could not be found, this is not a
13941 type-specifier-seq at all. */
13942 if (!seen_type_specifier)
13943 {
13944 cp_parser_error (parser, "expected type-specifier");
13945 type_specifier_seq->type = error_mark_node;
13946 return;
13947 }
13948 /* If subsequent type-specifiers could not be found, the
13949 type-specifier-seq is complete. */
13950 break;
4b9b2871 13951 }
0a3b29ad 13952
0a3b29ad 13953 seen_type_specifier = true;
6f74fe3c 13954 /* The standard says that a condition can be:
13955
653e5405 13956 type-specifier-seq declarator = assignment-expression
9031d10b 13957
6f74fe3c 13958 However, given:
13959
13960 struct S {};
13961 if (int S = ...)
13962
653e5405 13963 we should treat the "S" as a declarator, not as a
13964 type-specifier. The standard doesn't say that explicitly for
13965 type-specifier-seq, but it does say that for
13966 decl-specifier-seq in an ordinary declaration. Perhaps it
13967 would be clearer just to allow a decl-specifier-seq here, and
13968 then add a semantic restriction that if any decl-specifiers
13969 that are not type-specifiers appear, the program is invalid. */
6f74fe3c 13970 if (is_condition && !is_cv_qualifier)
9031d10b 13971 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
0a3b29ad 13972 }
639b2fed 13973
ad9ae192 13974 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
0a3b29ad 13975}
13976
13977/* Parse a parameter-declaration-clause.
13978
13979 parameter-declaration-clause:
13980 parameter-declaration-list [opt] ... [opt]
13981 parameter-declaration-list , ...
13982
3046c0a3 13983 Returns a representation for the parameter declarations. A return
13984 value of NULL indicates a parameter-declaration-clause consisting
13985 only of an ellipsis. */
0a3b29ad 13986
34eac767 13987static tree
45baea8b 13988cp_parser_parameter_declaration_clause (cp_parser* parser)
0a3b29ad 13989{
34eac767 13990 tree parameters;
0a3b29ad 13991 cp_token *token;
13992 bool ellipsis_p;
3046c0a3 13993 bool is_error;
0a3b29ad 13994
13995 /* Peek at the next token. */
13996 token = cp_lexer_peek_token (parser->lexer);
13997 /* Check for trivial parameter-declaration-clauses. */
13998 if (token->type == CPP_ELLIPSIS)
13999 {
14000 /* Consume the `...' token. */
14001 cp_lexer_consume_token (parser->lexer);
34eac767 14002 return NULL_TREE;
0a3b29ad 14003 }
14004 else if (token->type == CPP_CLOSE_PAREN)
14005 /* There are no parameters. */
2bd78947 14006 {
14007#ifndef NO_IMPLICIT_EXTERN_C
14008 if (in_system_header && current_class_type == NULL
14009 && current_lang_name == lang_name_c)
34eac767 14010 return NULL_TREE;
2bd78947 14011 else
14012#endif
34eac767 14013 return void_list_node;
2bd78947 14014 }
0a3b29ad 14015 /* Check for `(void)', too, which is a special case. */
14016 else if (token->keyword == RID_VOID
ccb84981 14017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 14018 == CPP_CLOSE_PAREN))
14019 {
14020 /* Consume the `void' token. */
14021 cp_lexer_consume_token (parser->lexer);
14022 /* There are no parameters. */
34eac767 14023 return void_list_node;
0a3b29ad 14024 }
ccb84981 14025
0a3b29ad 14026 /* Parse the parameter-declaration-list. */
3046c0a3 14027 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
0a3b29ad 14028 /* If a parse error occurred while parsing the
14029 parameter-declaration-list, then the entire
14030 parameter-declaration-clause is erroneous. */
3046c0a3 14031 if (is_error)
14032 return NULL;
0a3b29ad 14033
14034 /* Peek at the next token. */
14035 token = cp_lexer_peek_token (parser->lexer);
14036 /* If it's a `,', the clause should terminate with an ellipsis. */
14037 if (token->type == CPP_COMMA)
14038 {
14039 /* Consume the `,'. */
14040 cp_lexer_consume_token (parser->lexer);
14041 /* Expect an ellipsis. */
ccb84981 14042 ellipsis_p
640710cf 14043 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
0a3b29ad 14044 }
ccb84981 14045 /* It might also be `...' if the optional trailing `,' was
0a3b29ad 14046 omitted. */
14047 else if (token->type == CPP_ELLIPSIS)
14048 {
14049 /* Consume the `...' token. */
14050 cp_lexer_consume_token (parser->lexer);
14051 /* And remember that we saw it. */
14052 ellipsis_p = true;
14053 }
14054 else
14055 ellipsis_p = false;
14056
14057 /* Finish the parameter list. */
34eac767 14058 if (!ellipsis_p)
14059 parameters = chainon (parameters, void_list_node);
207355ad 14060
3046c0a3 14061 return parameters;
0a3b29ad 14062}
14063
14064/* Parse a parameter-declaration-list.
14065
14066 parameter-declaration-list:
14067 parameter-declaration
14068 parameter-declaration-list , parameter-declaration
14069
14070 Returns a representation of the parameter-declaration-list, as for
14071 cp_parser_parameter_declaration_clause. However, the
3046c0a3 14072 `void_list_node' is never appended to the list. Upon return,
14073 *IS_ERROR will be true iff an error occurred. */
0a3b29ad 14074
34eac767 14075static tree
3046c0a3 14076cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
0a3b29ad 14077{
34eac767 14078 tree parameters = NULL_TREE;
14079 tree *tail = &parameters;
3b289c9c 14080 bool saved_in_unbraced_linkage_specification_p;
3046c0a3 14081
14082 /* Assume all will go well. */
14083 *is_error = false;
3b289c9c 14084 /* The special considerations that apply to a function within an
14085 unbraced linkage specifications do not apply to the parameters
14086 to the function. */
14087 saved_in_unbraced_linkage_specification_p
14088 = parser->in_unbraced_linkage_specification_p;
14089 parser->in_unbraced_linkage_specification_p = false;
0a3b29ad 14090
14091 /* Look for more parameters. */
14092 while (true)
14093 {
3046c0a3 14094 cp_parameter_declarator *parameter;
34eac767 14095 tree decl = error_mark_node;
92b128ed 14096 bool parenthesized_p;
0a3b29ad 14097 /* Parse the parameter. */
ccb84981 14098 parameter
14099 = cp_parser_parameter_declaration (parser,
92b128ed 14100 /*template_parm_p=*/false,
14101 &parenthesized_p);
759fa9c9 14102
34eac767 14103 /* We don't know yet if the enclosing context is deprecated, so wait
14104 and warn in grokparms if appropriate. */
14105 deprecated_state = DEPRECATED_SUPPRESS;
14106
14107 if (parameter)
14108 decl = grokdeclarator (parameter->declarator,
14109 &parameter->decl_specifiers,
14110 PARM,
14111 parameter->default_argument != NULL_TREE,
14112 &parameter->decl_specifiers.attributes);
14113
14114 deprecated_state = DEPRECATED_NORMAL;
14115
755edffd 14116 /* If a parse error occurred parsing the parameter declaration,
0a3b29ad 14117 then the entire parameter-declaration-list is erroneous. */
34eac767 14118 if (decl == error_mark_node)
0a3b29ad 14119 {
3046c0a3 14120 *is_error = true;
34eac767 14121 parameters = error_mark_node;
0a3b29ad 14122 break;
14123 }
34eac767 14124
14125 if (parameter->decl_specifiers.attributes)
14126 cplus_decl_attributes (&decl,
14127 parameter->decl_specifiers.attributes,
14128 0);
14129 if (DECL_NAME (decl))
14130 decl = pushdecl (decl);
14131
0a3b29ad 14132 /* Add the new parameter to the list. */
34eac767 14133 *tail = build_tree_list (parameter->default_argument, decl);
14134 tail = &TREE_CHAIN (*tail);
0a3b29ad 14135
14136 /* Peek at the next token. */
14137 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
7a4e126b 14138 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14139 /* These are for Objective-C++ */
14140 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14141 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 14142 /* The parameter-declaration-list is complete. */
14143 break;
14144 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14145 {
14146 cp_token *token;
14147
14148 /* Peek at the next token. */
14149 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14150 /* If it's an ellipsis, then the list is complete. */
14151 if (token->type == CPP_ELLIPSIS)
14152 break;
14153 /* Otherwise, there must be more parameters. Consume the
14154 `,'. */
14155 cp_lexer_consume_token (parser->lexer);
92b128ed 14156 /* When parsing something like:
14157
653e5405 14158 int i(float f, double d)
ccb84981 14159
653e5405 14160 we can tell after seeing the declaration for "f" that we
92b128ed 14161 are not looking at an initialization of a variable "i",
ccb84981 14162 but rather at the declaration of a function "i".
92b128ed 14163
14164 Due to the fact that the parsing of template arguments
14165 (as specified to a template-id) requires backtracking we
14166 cannot use this technique when inside a template argument
14167 list. */
14168 if (!parser->in_template_argument_list_p
6006bfb6 14169 && !parser->in_type_id_in_expr_p
efcbcf83 14170 && cp_parser_uncommitted_to_tentative_parse_p (parser)
92b128ed 14171 /* However, a parameter-declaration of the form
14172 "foat(f)" (which is a valid declaration of a
14173 parameter "f") can also be interpreted as an
14174 expression (the conversion of "f" to "float"). */
14175 && !parenthesized_p)
14176 cp_parser_commit_to_tentative_parse (parser);
0a3b29ad 14177 }
14178 else
14179 {
a2c5b975 14180 cp_parser_error (parser, "expected %<,%> or %<...%>");
efcbcf83 14181 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb84981 14182 cp_parser_skip_to_closing_parenthesis (parser,
92b128ed 14183 /*recovering=*/true,
78662158 14184 /*or_comma=*/false,
92b128ed 14185 /*consume_paren=*/false);
0a3b29ad 14186 break;
14187 }
14188 }
14189
3b289c9c 14190 parser->in_unbraced_linkage_specification_p
14191 = saved_in_unbraced_linkage_specification_p;
14192
3046c0a3 14193 return parameters;
0a3b29ad 14194}
14195
14196/* Parse a parameter declaration.
14197
14198 parameter-declaration:
d95d815d 14199 decl-specifier-seq ... [opt] declarator
0a3b29ad 14200 decl-specifier-seq declarator = assignment-expression
d95d815d 14201 decl-specifier-seq ... [opt] abstract-declarator [opt]
0a3b29ad 14202 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14203
759fa9c9 14204 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14205 declares a template parameter. (In that case, a non-nested `>'
14206 token encountered during the parsing of the assignment-expression
14207 is not interpreted as a greater-than operator.)
0a3b29ad 14208
3046c0a3 14209 Returns a representation of the parameter, or NULL if an error
14210 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14211 true iff the declarator is of the form "(p)". */
0a3b29ad 14212
3046c0a3 14213static cp_parameter_declarator *
ccb84981 14214cp_parser_parameter_declaration (cp_parser *parser,
92b128ed 14215 bool template_parm_p,
14216 bool *parenthesized_p)
0a3b29ad 14217{
8172be22 14218 int declares_class_or_enum;
759fa9c9 14219 bool greater_than_is_operator_p;
4b9b2871 14220 cp_decl_specifier_seq decl_specifiers;
3046c0a3 14221 cp_declarator *declarator;
0a3b29ad 14222 tree default_argument;
ad9ae192 14223 cp_token *token = NULL, *declarator_token_start = NULL;
0a3b29ad 14224 const char *saved_message;
14225
759fa9c9 14226 /* In a template parameter, `>' is not an operator.
14227
14228 [temp.param]
14229
14230 When parsing a default template-argument for a non-type
14231 template-parameter, the first non-nested `>' is taken as the end
14232 of the template parameter-list rather than a greater-than
14233 operator. */
14234 greater_than_is_operator_p = !template_parm_p;
14235
0a3b29ad 14236 /* Type definitions may not appear in parameter types. */
14237 saved_message = parser->type_definition_forbidden_message;
ccb84981 14238 parser->type_definition_forbidden_message
0a3b29ad 14239 = "types may not be defined in parameter types";
14240
14241 /* Parse the declaration-specifiers. */
4b9b2871 14242 cp_parser_decl_specifier_seq (parser,
14243 CP_PARSER_FLAGS_NONE,
14244 &decl_specifiers,
14245 &declares_class_or_enum);
0a3b29ad 14246 /* If an error occurred, there's no reason to attempt to parse the
14247 rest of the declaration. */
14248 if (cp_parser_error_occurred (parser))
14249 {
14250 parser->type_definition_forbidden_message = saved_message;
3046c0a3 14251 return NULL;
0a3b29ad 14252 }
14253
14254 /* Peek at the next token. */
14255 token = cp_lexer_peek_token (parser->lexer);
d95d815d 14256
0a3b29ad 14257 /* If the next token is a `)', `,', `=', `>', or `...', then there
d95d815d 14258 is no declarator. However, when variadic templates are enabled,
14259 there may be a declarator following `...'. */
ccb84981 14260 if (token->type == CPP_CLOSE_PAREN
0a3b29ad 14261 || token->type == CPP_COMMA
14262 || token->type == CPP_EQ
0a3b29ad 14263 || token->type == CPP_GREATER)
92b128ed 14264 {
3046c0a3 14265 declarator = NULL;
92b128ed 14266 if (parenthesized_p)
14267 *parenthesized_p = false;
14268 }
0a3b29ad 14269 /* Otherwise, there should be a declarator. */
14270 else
14271 {
14272 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14273 parser->default_arg_ok_p = false;
ccb84981 14274
78662158 14275 /* After seeing a decl-specifier-seq, if the next token is not a
14276 "(", there is no possibility that the code is a valid
41f2d08e 14277 expression. Therefore, if parsing tentatively, we commit at
14278 this point. */
78662158 14279 if (!parser->in_template_argument_list_p
461ec6e9 14280 /* In an expression context, having seen:
41f2d08e 14281
91f31809 14282 (int((char ...
41f2d08e 14283
14284 we cannot be sure whether we are looking at a
91f31809 14285 function-type (taking a "char" as a parameter) or a cast
14286 of some object of type "char" to "int". */
41f2d08e 14287 && !parser->in_type_id_in_expr_p
efcbcf83 14288 && cp_parser_uncommitted_to_tentative_parse_p (parser)
78662158 14289 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14290 cp_parser_commit_to_tentative_parse (parser);
14291 /* Parse the declarator. */
ad9ae192 14292 declarator_token_start = token;
0a3b29ad 14293 declarator = cp_parser_declarator (parser,
42bbd0ec 14294 CP_PARSER_DECLARATOR_EITHER,
92b128ed 14295 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 14296 parenthesized_p,
14297 /*member_p=*/false);
0a3b29ad 14298 parser->default_arg_ok_p = saved_default_arg_ok_p;
b5002156 14299 /* After the declarator, allow more attributes. */
4b9b2871 14300 decl_specifiers.attributes
207355ad 14301 = chainon (decl_specifiers.attributes,
4b9b2871 14302 cp_parser_attributes_opt (parser));
0a3b29ad 14303 }
14304
2aedc2ff 14305 /* If the next token is an ellipsis, and we have not seen a
14306 declarator name, and the type of the declarator contains parameter
14307 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14308 a parameter pack expansion expression. Otherwise, leave the
14309 ellipsis for a C-style variadic function. */
d95d815d 14310 token = cp_lexer_peek_token (parser->lexer);
14311 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14312 {
14313 tree type = decl_specifiers.type;
14314
2aedc2ff 14315 if (type && DECL_P (type))
d95d815d 14316 type = TREE_TYPE (type);
14317
2aedc2ff 14318 if (type
14319 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14320 && declarator_can_be_parameter_pack (declarator)
d95d815d 14321 && (!declarator || !declarator->parameter_pack_p)
14322 && uses_parameter_packs (type))
14323 {
2aedc2ff 14324 /* Consume the `...'. */
14325 cp_lexer_consume_token (parser->lexer);
14326 maybe_warn_variadic_templates ();
14327
14328 /* Build a pack expansion type */
14329 if (declarator)
14330 declarator->parameter_pack_p = true;
14331 else
14332 decl_specifiers.type = make_pack_expansion (type);
14333 }
d95d815d 14334 }
14335
42bbd0ec 14336 /* The restriction on defining new types applies only to the type
0a3b29ad 14337 of the parameter, not to the default argument. */
14338 parser->type_definition_forbidden_message = saved_message;
14339
14340 /* If the next token is `=', then process a default argument. */
14341 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14342 {
0a3b29ad 14343 /* Consume the `='. */
14344 cp_lexer_consume_token (parser->lexer);
14345
14346 /* If we are defining a class, then the tokens that make up the
14347 default argument must be saved and processed later. */
ccb84981 14348 if (!template_parm_p && at_class_scope_p ()
759fa9c9 14349 && TYPE_BEING_DEFINED (current_class_type))
0a3b29ad 14350 {
14351 unsigned depth = 0;
eef53511 14352 int maybe_template_id = 0;
00d26680 14353 cp_token *first_token;
14354 cp_token *token;
0a3b29ad 14355
14356 /* Add tokens until we have processed the entire default
93523877 14357 argument. We add the range [first_token, token). */
00d26680 14358 first_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 14359 while (true)
14360 {
14361 bool done = false;
0a3b29ad 14362
14363 /* Peek at the next token. */
14364 token = cp_lexer_peek_token (parser->lexer);
14365 /* What we do depends on what token we have. */
14366 switch (token->type)
14367 {
14368 /* In valid code, a default argument must be
14369 immediately followed by a `,' `)', or `...'. */
14370 case CPP_COMMA:
eef53511 14371 if (depth == 0 && maybe_template_id)
14372 {
14373 /* If we've seen a '<', we might be in a
14374 template-argument-list. Until Core issue 325 is
14375 resolved, we don't know how this situation ought
14376 to be handled, so try to DTRT. We check whether
14377 what comes after the comma is a valid parameter
14378 declaration list. If it is, then the comma ends
14379 the default argument; otherwise the default
14380 argument continues. */
14381 bool error = false;
14382
14383 /* Set ITALP so cp_parser_parameter_declaration_list
14384 doesn't decide to commit to this parse. */
14385 bool saved_italp = parser->in_template_argument_list_p;
14386 parser->in_template_argument_list_p = true;
14387
14388 cp_parser_parse_tentatively (parser);
14389 cp_lexer_consume_token (parser->lexer);
14390 cp_parser_parameter_declaration_list (parser, &error);
14391 if (!cp_parser_error_occurred (parser) && !error)
14392 done = true;
14393 cp_parser_abort_tentative_parse (parser);
14394
14395 parser->in_template_argument_list_p = saved_italp;
14396 break;
14397 }
0a3b29ad 14398 case CPP_CLOSE_PAREN:
14399 case CPP_ELLIPSIS:
14400 /* If we run into a non-nested `;', `}', or `]',
14401 then the code is invalid -- but the default
14402 argument is certainly over. */
14403 case CPP_SEMICOLON:
14404 case CPP_CLOSE_BRACE:
14405 case CPP_CLOSE_SQUARE:
14406 if (depth == 0)
14407 done = true;
14408 /* Update DEPTH, if necessary. */
14409 else if (token->type == CPP_CLOSE_PAREN
14410 || token->type == CPP_CLOSE_BRACE
14411 || token->type == CPP_CLOSE_SQUARE)
14412 --depth;
14413 break;
14414
14415 case CPP_OPEN_PAREN:
14416 case CPP_OPEN_SQUARE:
14417 case CPP_OPEN_BRACE:
14418 ++depth;
14419 break;
14420
eef53511 14421 case CPP_LESS:
14422 if (depth == 0)
14423 /* This might be the comparison operator, or it might
14424 start a template argument list. */
14425 ++maybe_template_id;
14426 break;
14427
56471494 14428 case CPP_RSHIFT:
6dcdb5de 14429 if (cxx_dialect == cxx98)
56471494 14430 break;
14431 /* Fall through for C++0x, which treats the `>>'
14432 operator like two `>' tokens in certain
14433 cases. */
14434
0a3b29ad 14435 case CPP_GREATER:
eef53511 14436 if (depth == 0)
14437 {
14438 /* This might be an operator, or it might close a
14439 template argument list. But if a previous '<'
14440 started a template argument list, this will have
14441 closed it, so we can't be in one anymore. */
14442 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14443 if (maybe_template_id < 0)
14444 maybe_template_id = 0;
14445 }
0a3b29ad 14446 break;
14447
14448 /* If we run out of tokens, issue an error message. */
14449 case CPP_EOF:
b75b98aa 14450 case CPP_PRAGMA_EOL:
ad9ae192 14451 error ("%Hfile ends in default argument", &token->location);
0a3b29ad 14452 done = true;
14453 break;
14454
14455 case CPP_NAME:
14456 case CPP_SCOPE:
14457 /* In these cases, we should look for template-ids.
ccb84981 14458 For example, if the default argument is
0a3b29ad 14459 `X<int, double>()', we need to do name lookup to
14460 figure out whether or not `X' is a template; if
755edffd 14461 so, the `,' does not end the default argument.
0a3b29ad 14462
14463 That is not yet done. */
14464 break;
14465
14466 default:
14467 break;
14468 }
14469
14470 /* If we've reached the end, stop. */
14471 if (done)
14472 break;
ccb84981 14473
0a3b29ad 14474 /* Add the token to the token block. */
14475 token = cp_lexer_consume_token (parser->lexer);
0a3b29ad 14476 }
00d26680 14477
648a0f57 14478 /* Create a DEFAULT_ARG to represent the unparsed default
653e5405 14479 argument. */
00d26680 14480 default_argument = make_node (DEFAULT_ARG);
14481 DEFARG_TOKENS (default_argument)
f51f5e0b 14482 = cp_token_cache_new (first_token, token);
14483 DEFARG_INSTANTIATIONS (default_argument) = NULL;
0a3b29ad 14484 }
14485 /* Outside of a class definition, we can just parse the
653e5405 14486 assignment-expression. */
0a3b29ad 14487 else
ad9ae192 14488 {
14489 token = cp_lexer_peek_token (parser->lexer);
14490 default_argument
14491 = cp_parser_default_argument (parser, template_parm_p);
14492 }
41341abd 14493
0a3b29ad 14494 if (!parser->default_arg_ok_p)
14495 {
561fec9d 14496 if (flag_permissive)
c3ceba8e 14497 warning (0, "deprecated use of default argument for parameter of non-function");
816786ad 14498 else
14499 {
ad9ae192 14500 error ("%Hdefault arguments are only "
14501 "permitted for function parameters",
14502 &token->location);
816786ad 14503 default_argument = NULL_TREE;
14504 }
0a3b29ad 14505 }
41341abd 14506 else if ((declarator && declarator->parameter_pack_p)
14507 || (decl_specifiers.type
14508 && PACK_EXPANSION_P (decl_specifiers.type)))
14509 {
14510 const char* kind = template_parm_p? "template " : "";
14511
14512 /* Find the name of the parameter pack. */
14513 cp_declarator *id_declarator = declarator;
14514 while (id_declarator && id_declarator->kind != cdk_id)
14515 id_declarator = id_declarator->declarator;
14516
14517 if (id_declarator && id_declarator->kind == cdk_id)
ad9ae192 14518 error ("%H%sparameter pack %qD cannot have a default argument",
14519 &declarator_token_start->location,
41341abd 14520 kind, id_declarator->u.id.unqualified_name);
14521 else
ad9ae192 14522 error ("%H%sparameter pack cannot have a default argument",
14523 &declarator_token_start->location, kind);
41341abd 14524
14525 default_argument = NULL_TREE;
14526 }
0a3b29ad 14527 }
14528 else
14529 default_argument = NULL_TREE;
ccb84981 14530
4b9b2871 14531 return make_parameter_declarator (&decl_specifiers,
3046c0a3 14532 declarator,
14533 default_argument);
0a3b29ad 14534}
14535
41341abd 14536/* Parse a default argument and return it.
14537
14538 TEMPLATE_PARM_P is true if this is a default argument for a
14539 non-type template parameter. */
14540static tree
14541cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14542{
14543 tree default_argument = NULL_TREE;
14544 bool saved_greater_than_is_operator_p;
14545 bool saved_local_variables_forbidden_p;
14546
14547 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14548 set correctly. */
14549 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14550 parser->greater_than_is_operator_p = !template_parm_p;
14551 /* Local variable names (and the `this' keyword) may not
14552 appear in a default argument. */
14553 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14554 parser->local_variables_forbidden_p = true;
14555 /* The default argument expression may cause implicitly
14556 defined member functions to be synthesized, which will
14557 result in garbage collection. We must treat this
14558 situation as if we were within the body of function so as
14559 to avoid collecting live data on the stack. */
14560 ++function_depth;
14561 /* Parse the assignment-expression. */
14562 if (template_parm_p)
14563 push_deferring_access_checks (dk_no_deferred);
14564 default_argument
98b326fd 14565 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
41341abd 14566 if (template_parm_p)
14567 pop_deferring_access_checks ();
14568 /* Restore saved state. */
14569 --function_depth;
14570 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14571 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14572
14573 return default_argument;
14574}
14575
0a3b29ad 14576/* Parse a function-body.
14577
14578 function-body:
14579 compound_statement */
14580
14581static void
14582cp_parser_function_body (cp_parser *parser)
14583{
2363ef00 14584 cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 14585}
14586
14587/* Parse a ctor-initializer-opt followed by a function-body. Return
14588 true if a ctor-initializer was present. */
14589
14590static bool
14591cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14592{
14593 tree body;
14594 bool ctor_initializer_p;
14595
14596 /* Begin the function body. */
14597 body = begin_function_body ();
14598 /* Parse the optional ctor-initializer. */
14599 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14600 /* Parse the function-body. */
14601 cp_parser_function_body (parser);
14602 /* Finish the function body. */
14603 finish_function_body (body);
14604
14605 return ctor_initializer_p;
14606}
14607
14608/* Parse an initializer.
14609
14610 initializer:
14611 = initializer-clause
ccb84981 14612 ( expression-list )
0a3b29ad 14613
e4bc96e2 14614 Returns an expression representing the initializer. If no
ccb84981 14615 initializer is present, NULL_TREE is returned.
0a3b29ad 14616
f82f1250 14617 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14618 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14619 set to TRUE if there is no initializer present. If there is an
878870b4 14620 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14621 is set to true; otherwise it is set to false. */
0a3b29ad 14622
14623static tree
f82f1250 14624cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
878870b4 14625 bool* non_constant_p)
0a3b29ad 14626{
14627 cp_token *token;
14628 tree init;
14629
14630 /* Peek at the next token. */
14631 token = cp_lexer_peek_token (parser->lexer);
14632
14633 /* Let our caller know whether or not this initializer was
14634 parenthesized. */
f82f1250 14635 *is_direct_init = (token->type != CPP_EQ);
878870b4 14636 /* Assume that the initializer is constant. */
14637 *non_constant_p = false;
0a3b29ad 14638
14639 if (token->type == CPP_EQ)
14640 {
14641 /* Consume the `='. */
14642 cp_lexer_consume_token (parser->lexer);
14643 /* Parse the initializer-clause. */
878870b4 14644 init = cp_parser_initializer_clause (parser, non_constant_p);
0a3b29ad 14645 }
14646 else if (token->type == CPP_OPEN_PAREN)
f352a3fb 14647 {
14648 VEC(tree,gc) *vec;
14649 vec = cp_parser_parenthesized_expression_list (parser, false,
14650 /*cast_p=*/false,
14651 /*allow_expansion_p=*/true,
14652 non_constant_p);
14653 if (vec == NULL)
14654 return error_mark_node;
14655 init = build_tree_list_vec (vec);
14656 release_tree_vector (vec);
14657 }
f82f1250 14658 else if (token->type == CPP_OPEN_BRACE)
14659 {
14660 maybe_warn_cpp0x ("extended initializer lists");
14661 init = cp_parser_braced_list (parser, non_constant_p);
14662 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14663 }
0a3b29ad 14664 else
14665 {
14666 /* Anything else is an error. */
14667 cp_parser_error (parser, "expected initializer");
14668 init = error_mark_node;
14669 }
14670
14671 return init;
14672}
14673
ccb84981 14674/* Parse an initializer-clause.
0a3b29ad 14675
14676 initializer-clause:
14677 assignment-expression
f82f1250 14678 braced-init-list
0a3b29ad 14679
ccb84981 14680 Returns an expression representing the initializer.
0a3b29ad 14681
14682 If the `assignment-expression' production is used the value
ccb84981 14683 returned is simply a representation for the expression.
0a3b29ad 14684
f82f1250 14685 Otherwise, calls cp_parser_braced_list. */
0a3b29ad 14686
14687static tree
878870b4 14688cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
0a3b29ad 14689{
14690 tree initializer;
14691
53073dc9 14692 /* Assume the expression is constant. */
14693 *non_constant_p = false;
14694
0a3b29ad 14695 /* If it is not a `{', then we are looking at an
14696 assignment-expression. */
14697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
f352cc1d 14698 {
207355ad 14699 initializer
f352cc1d 14700 = cp_parser_constant_expression (parser,
14701 /*allow_non_constant_p=*/true,
14702 non_constant_p);
14703 if (!*non_constant_p)
14704 initializer = fold_non_dependent_expr (initializer);
14705 }
0a3b29ad 14706 else
f82f1250 14707 initializer = cp_parser_braced_list (parser, non_constant_p);
14708
14709 return initializer;
14710}
14711
14712/* Parse a brace-enclosed initializer list.
14713
14714 braced-init-list:
14715 { initializer-list , [opt] }
14716 { }
14717
14718 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14719 the elements of the initializer-list (or NULL, if the last
14720 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14721 NULL_TREE. There is no way to detect whether or not the optional
14722 trailing `,' was provided. NON_CONSTANT_P is as for
14723 cp_parser_initializer. */
14724
14725static tree
14726cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14727{
14728 tree initializer;
14729
14730 /* Consume the `{' token. */
14731 cp_lexer_consume_token (parser->lexer);
14732 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14733 initializer = make_node (CONSTRUCTOR);
14734 /* If it's not a `}', then there is a non-trivial initializer. */
14735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
0a3b29ad 14736 {
f82f1250 14737 /* Parse the initializer list. */
14738 CONSTRUCTOR_ELTS (initializer)
14739 = cp_parser_initializer_list (parser, non_constant_p);
14740 /* A trailing `,' token is allowed. */
14741 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14742 cp_lexer_consume_token (parser->lexer);
0a3b29ad 14743 }
f82f1250 14744 /* Now, there should be a trailing `}'. */
14745 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14746 TREE_TYPE (initializer) = init_list_type_node;
0a3b29ad 14747 return initializer;
14748}
14749
14750/* Parse an initializer-list.
14751
14752 initializer-list:
d95d815d 14753 initializer-clause ... [opt]
14754 initializer-list , initializer-clause ... [opt]
0a3b29ad 14755
14756 GNU Extension:
ccb84981 14757
0a3b29ad 14758 initializer-list:
14759 identifier : initializer-clause
14760 initializer-list, identifier : initializer-clause
14761
c75b4594 14762 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14763 for the initializer. If the INDEX of the elt is non-NULL, it is the
878870b4 14764 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14765 as for cp_parser_initializer. */
0a3b29ad 14766
c75b4594 14767static VEC(constructor_elt,gc) *
878870b4 14768cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
0a3b29ad 14769{
c75b4594 14770 VEC(constructor_elt,gc) *v = NULL;
0a3b29ad 14771
878870b4 14772 /* Assume all of the expressions are constant. */
14773 *non_constant_p = false;
14774
0a3b29ad 14775 /* Parse the rest of the list. */
14776 while (true)
14777 {
14778 cp_token *token;
14779 tree identifier;
14780 tree initializer;
878870b4 14781 bool clause_non_constant_p;
14782
0a3b29ad 14783 /* If the next token is an identifier and the following one is a
14784 colon, we are looking at the GNU designated-initializer
14785 syntax. */
14786 if (cp_parser_allow_gnu_extensions_p (parser)
14787 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14788 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14789 {
1d8baa0e 14790 /* Warn the user that they are using an extension. */
21ca8540 14791 pedwarn (input_location, OPT_pedantic,
8864917d 14792 "ISO C++ does not allow designated initializers");
0a3b29ad 14793 /* Consume the identifier. */
3369eb76 14794 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 14795 /* Consume the `:'. */
14796 cp_lexer_consume_token (parser->lexer);
14797 }
14798 else
14799 identifier = NULL_TREE;
14800
14801 /* Parse the initializer. */
ccb84981 14802 initializer = cp_parser_initializer_clause (parser,
878870b4 14803 &clause_non_constant_p);
14804 /* If any clause is non-constant, so is the entire initializer. */
14805 if (clause_non_constant_p)
14806 *non_constant_p = true;
c75b4594 14807
d95d815d 14808 /* If we have an ellipsis, this is an initializer pack
14809 expansion. */
14810 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14811 {
14812 /* Consume the `...'. */
14813 cp_lexer_consume_token (parser->lexer);
14814
14815 /* Turn the initializer into an initializer expansion. */
14816 initializer = make_pack_expansion (initializer);
14817 }
14818
c75b4594 14819 /* Add it to the vector. */
14820 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
0a3b29ad 14821
14822 /* If the next token is not a comma, we have reached the end of
14823 the list. */
14824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14825 break;
14826
14827 /* Peek at the next token. */
14828 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14829 /* If the next token is a `}', then we're still done. An
14830 initializer-clause can have a trailing `,' after the
14831 initializer-list and before the closing `}'. */
14832 if (token->type == CPP_CLOSE_BRACE)
14833 break;
14834
14835 /* Consume the `,' token. */
14836 cp_lexer_consume_token (parser->lexer);
14837 }
14838
c75b4594 14839 return v;
0a3b29ad 14840}
14841
14842/* Classes [gram.class] */
14843
14844/* Parse a class-name.
14845
14846 class-name:
14847 identifier
14848 template-id
14849
14850 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14851 to indicate that names looked up in dependent types should be
14852 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14853 keyword has been used to indicate that the name that appears next
e2ae55f2 14854 is a template. TAG_TYPE indicates the explicit tag given before
14855 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14856 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14857 is the class being defined in a class-head.
0a3b29ad 14858
14859 Returns the TYPE_DECL representing the class. */
14860
14861static tree
ccb84981 14862cp_parser_class_name (cp_parser *parser,
14863 bool typename_keyword_p,
14864 bool template_keyword_p,
e2ae55f2 14865 enum tag_types tag_type,
0a3b29ad 14866 bool check_dependency_p,
3d0f901b 14867 bool class_head_p,
14868 bool is_declaration)
0a3b29ad 14869{
14870 tree decl;
14871 tree scope;
14872 bool typename_p;
2c593bd0 14873 cp_token *token;
a6c68ee8 14874 tree identifier = NULL_TREE;
2c593bd0 14875
14876 /* All class-names start with an identifier. */
14877 token = cp_lexer_peek_token (parser->lexer);
14878 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14879 {
14880 cp_parser_error (parser, "expected class-name");
14881 return error_mark_node;
14882 }
ccb84981 14883
0a3b29ad 14884 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14885 to a template-id, so we save it here. */
14886 scope = parser->scope;
1e9847d8 14887 if (scope == error_mark_node)
14888 return error_mark_node;
ccb84981 14889
0a3b29ad 14890 /* Any name names a type if we're following the `typename' keyword
14891 in a qualified name where the enclosing scope is type-dependent. */
14892 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
7e9a6a16 14893 && dependent_type_p (scope));
2c593bd0 14894 /* Handle the common case (an identifier, but not a template-id)
14895 efficiently. */
ccb84981 14896 if (token->type == CPP_NAME
c8d5ab79 14897 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
0a3b29ad 14898 {
b62240d5 14899 cp_token *identifier_token;
b62240d5 14900 bool ambiguous_p;
0a3b29ad 14901
14902 /* Look for the identifier. */
b62240d5 14903 identifier_token = cp_lexer_peek_token (parser->lexer);
14904 ambiguous_p = identifier_token->ambiguous_p;
0a3b29ad 14905 identifier = cp_parser_identifier (parser);
14906 /* If the next token isn't an identifier, we are certainly not
14907 looking at a class-name. */
14908 if (identifier == error_mark_node)
14909 decl = error_mark_node;
14910 /* If we know this is a type-name, there's no need to look it
14911 up. */
14912 else if (typename_p)
14913 decl = identifier;
14914 else
14915 {
b62240d5 14916 tree ambiguous_decls;
14917 /* If we already know that this lookup is ambiguous, then
14918 we've already issued an error message; there's no reason
14919 to check again. */
14920 if (ambiguous_p)
14921 {
14922 cp_parser_simulate_error (parser);
14923 return error_mark_node;
14924 }
0a3b29ad 14925 /* If the next token is a `::', then the name must be a type
14926 name.
14927
14928 [basic.lookup.qual]
14929
14930 During the lookup for a name preceding the :: scope
14931 resolution operator, object, function, and enumerator
14932 names are ignored. */
14933 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
e2ae55f2 14934 tag_type = typename_type;
0a3b29ad 14935 /* Look up the name. */
ccb84981 14936 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 14937 tag_type,
c3b9e457 14938 /*is_template=*/false,
6fc758aa 14939 /*is_namespace=*/false,
2cdbcd51 14940 check_dependency_p,
ad9ae192 14941 &ambiguous_decls,
14942 identifier_token->location);
b62240d5 14943 if (ambiguous_decls)
14944 {
ad9ae192 14945 error ("%Hreference to %qD is ambiguous",
14946 &identifier_token->location, identifier);
b62240d5 14947 print_candidates (ambiguous_decls);
14948 if (cp_parser_parsing_tentatively (parser))
14949 {
14950 identifier_token->ambiguous_p = true;
14951 cp_parser_simulate_error (parser);
14952 }
14953 return error_mark_node;
14954 }
0a3b29ad 14955 }
14956 }
2c593bd0 14957 else
14958 {
14959 /* Try a template-id. */
14960 decl = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 14961 check_dependency_p,
14962 is_declaration);
2c593bd0 14963 if (decl == error_mark_node)
14964 return error_mark_node;
14965 }
0a3b29ad 14966
14967 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14968
14969 /* If this is a typename, create a TYPENAME_TYPE. */
14970 if (typename_p && decl != error_mark_node)
50dad21f 14971 {
c31fdaf6 14972 decl = make_typename_type (scope, decl, typename_type,
074ab442 14973 /*complain=*/tf_error);
50dad21f 14974 if (decl != error_mark_node)
14975 decl = TYPE_NAME (decl);
14976 }
0a3b29ad 14977
14978 /* Check to see that it is really the name of a class. */
ccb84981 14979 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
0a3b29ad 14980 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14981 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14982 /* Situations like this:
14983
14984 template <typename T> struct A {
ccb84981 14985 typename T::template X<int>::I i;
0a3b29ad 14986 };
14987
14988 are problematic. Is `T::template X<int>' a class-name? The
14989 standard does not seem to be definitive, but there is no other
14990 valid interpretation of the following `::'. Therefore, those
14991 names are considered class-names. */
0a3b29ad 14992 {
64b06433 14993 decl = make_typename_type (scope, decl, tag_type, tf_error);
14994 if (decl != error_mark_node)
14995 decl = TYPE_NAME (decl);
0a3b29ad 14996 }
64b06433 14997 else if (TREE_CODE (decl) != TYPE_DECL
14998 || TREE_TYPE (decl) == error_mark_node
95397ff9 14999 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
64b06433 15000 decl = error_mark_node;
15001
15002 if (decl == error_mark_node)
15003 cp_parser_error (parser, "expected class-name");
a6c68ee8 15004 else if (identifier && !parser->scope)
15005 maybe_note_name_used_in_class (identifier, decl);
0a3b29ad 15006
15007 return decl;
15008}
15009
15010/* Parse a class-specifier.
15011
15012 class-specifier:
15013 class-head { member-specification [opt] }
15014
15015 Returns the TREE_TYPE representing the class. */
15016
15017static tree
45baea8b 15018cp_parser_class_specifier (cp_parser* parser)
0a3b29ad 15019{
0a3b29ad 15020 tree type;
4ee9c684 15021 tree attributes = NULL_TREE;
0a3b29ad 15022 bool nested_name_specifier_p;
0a3b29ad 15023 unsigned saved_num_template_parameter_lists;
0aeb1cc5 15024 bool saved_in_function_body;
f0475530 15025 bool saved_in_unbraced_linkage_specification_p;
352baa70 15026 tree old_scope = NULL_TREE;
534d03ee 15027 tree scope = NULL_TREE;
1a9b4c25 15028 tree bases;
0a3b29ad 15029
4f62c42e 15030 push_deferring_access_checks (dk_no_deferred);
9b57b06b 15031
0a3b29ad 15032 /* Parse the class-head. */
15033 type = cp_parser_class_head (parser,
fb3e3237 15034 &nested_name_specifier_p,
1a9b4c25 15035 &attributes,
15036 &bases);
0a3b29ad 15037 /* If the class-head was a semantic disaster, skip the entire body
15038 of the class. */
15039 if (!type)
15040 {
15041 cp_parser_skip_to_end_of_block_or_statement (parser);
9b57b06b 15042 pop_deferring_access_checks ();
0a3b29ad 15043 return error_mark_node;
15044 }
9b57b06b 15045
0a3b29ad 15046 /* Look for the `{'. */
640710cf 15047 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
9b57b06b 15048 {
15049 pop_deferring_access_checks ();
15050 return error_mark_node;
15051 }
15052
1a9b4c25 15053 /* Process the base classes. If they're invalid, skip the
15054 entire class body. */
15055 if (!xref_basetypes (type, bases))
15056 {
1a9b4c25 15057 /* Consuming the closing brace yields better error messages
15058 later on. */
9aad947b 15059 if (cp_parser_skip_to_closing_brace (parser))
15060 cp_lexer_consume_token (parser->lexer);
1a9b4c25 15061 pop_deferring_access_checks ();
15062 return error_mark_node;
15063 }
15064
0a3b29ad 15065 /* Issue an error message if type-definitions are forbidden here. */
15066 cp_parser_check_type_definition (parser);
15067 /* Remember that we are defining one more class. */
15068 ++parser->num_classes_being_defined;
15069 /* Inside the class, surrounding template-parameter-lists do not
15070 apply. */
ccb84981 15071 saved_num_template_parameter_lists
15072 = parser->num_template_parameter_lists;
0a3b29ad 15073 parser->num_template_parameter_lists = 0;
0aeb1cc5 15074 /* We are not in a function body. */
15075 saved_in_function_body = parser->in_function_body;
15076 parser->in_function_body = false;
f0475530 15077 /* We are not immediately inside an extern "lang" block. */
15078 saved_in_unbraced_linkage_specification_p
15079 = parser->in_unbraced_linkage_specification_p;
15080 parser->in_unbraced_linkage_specification_p = false;
4cab8273 15081
0a3b29ad 15082 /* Start the class. */
4ccffa39 15083 if (nested_name_specifier_p)
534d03ee 15084 {
15085 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
352baa70 15086 old_scope = push_inner_scope (scope);
534d03ee 15087 }
4a2849cb 15088 type = begin_class_definition (type, attributes);
207355ad 15089
0a3b29ad 15090 if (type == error_mark_node)
6beb3f76 15091 /* If the type is erroneous, skip the entire body of the class. */
0a3b29ad 15092 cp_parser_skip_to_closing_brace (parser);
15093 else
15094 /* Parse the member-specification. */
15095 cp_parser_member_specification_opt (parser);
207355ad 15096
0a3b29ad 15097 /* Look for the trailing `}'. */
640710cf 15098 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
fb3e3237 15099 /* Look for trailing attributes to apply to this class. */
0a3b29ad 15100 if (cp_parser_allow_gnu_extensions_p (parser))
4a2849cb 15101 attributes = cp_parser_attributes_opt (parser);
fb3e3237 15102 if (type != error_mark_node)
15103 type = finish_struct (type, attributes);
352baa70 15104 if (nested_name_specifier_p)
15105 pop_inner_scope (old_scope, scope);
0a3b29ad 15106 /* If this class is not itself within the scope of another class,
15107 then we need to parse the bodies of all of the queued function
15108 definitions. Note that the queued functions defined in a class
15109 are not always processed immediately following the
15110 class-specifier for that class. Consider:
15111
15112 struct A {
653e5405 15113 struct B { void f() { sizeof (A); } };
0a3b29ad 15114 };
15115
15116 If `f' were processed before the processing of `A' were
15117 completed, there would be no way to compute the size of `A'.
15118 Note that the nesting we are interested in here is lexical --
15119 not the semantic nesting given by TYPE_CONTEXT. In particular,
15120 for:
15121
15122 struct A { struct B; };
15123 struct A::B { void f() { } };
15124
15125 there is no need to delay the parsing of `A::B::f'. */
ccb84981 15126 if (--parser->num_classes_being_defined == 0)
0a3b29ad 15127 {
af128372 15128 tree queue_entry;
15129 tree fn;
7f602bca 15130 tree class_type = NULL_TREE;
15131 tree pushed_scope = NULL_TREE;
074ab442 15132
af128372 15133 /* In a first pass, parse default arguments to the functions.
15134 Then, in a second pass, parse the bodies of the functions.
15135 This two-phased approach handles cases like:
ccb84981 15136
15137 struct S {
653e5405 15138 void f() { g(); }
15139 void g(int i = 3);
15140 };
af128372 15141
653e5405 15142 */
69b6679c 15143 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15144 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15145 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15146 TREE_PURPOSE (parser->unparsed_functions_queues)
15147 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
af128372 15148 {
15149 fn = TREE_VALUE (queue_entry);
af128372 15150 /* If there are default arguments that have not yet been processed,
15151 take care of them now. */
93c149df 15152 if (class_type != TREE_PURPOSE (queue_entry))
15153 {
7f602bca 15154 if (pushed_scope)
15155 pop_scope (pushed_scope);
93c149df 15156 class_type = TREE_PURPOSE (queue_entry);
7f602bca 15157 pushed_scope = push_scope (class_type);
93c149df 15158 }
15159 /* Make sure that any template parameters are in scope. */
15160 maybe_begin_member_template_processing (fn);
15161 /* Parse the default argument expressions. */
af128372 15162 cp_parser_late_parsing_default_args (parser, fn);
15163 /* Remove any template parameters from the symbol table. */
15164 maybe_end_member_template_processing ();
15165 }
7f602bca 15166 if (pushed_scope)
15167 pop_scope (pushed_scope);
af128372 15168 /* Now parse the body of the functions. */
69b6679c 15169 for (TREE_VALUE (parser->unparsed_functions_queues)
15170 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15171 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15172 TREE_VALUE (parser->unparsed_functions_queues)
15173 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
0a3b29ad 15174 {
0a3b29ad 15175 /* Figure out which function we need to process. */
0a3b29ad 15176 fn = TREE_VALUE (queue_entry);
0a3b29ad 15177 /* Parse the function. */
15178 cp_parser_late_parsing_for_member (parser, fn);
0a3b29ad 15179 }
0a3b29ad 15180 }
15181
15182 /* Put back any saved access checks. */
9b57b06b 15183 pop_deferring_access_checks ();
0a3b29ad 15184
0aeb1cc5 15185 /* Restore saved state. */
15186 parser->in_function_body = saved_in_function_body;
0a3b29ad 15187 parser->num_template_parameter_lists
15188 = saved_num_template_parameter_lists;
f0475530 15189 parser->in_unbraced_linkage_specification_p
15190 = saved_in_unbraced_linkage_specification_p;
0a3b29ad 15191
15192 return type;
15193}
15194
15195/* Parse a class-head.
15196
15197 class-head:
15198 class-key identifier [opt] base-clause [opt]
15199 class-key nested-name-specifier identifier base-clause [opt]
ccb84981 15200 class-key nested-name-specifier [opt] template-id
15201 base-clause [opt]
0a3b29ad 15202
15203 GNU Extensions:
15204 class-key attributes identifier [opt] base-clause [opt]
15205 class-key attributes nested-name-specifier identifier base-clause [opt]
ccb84981 15206 class-key attributes nested-name-specifier [opt] template-id
15207 base-clause [opt]
0a3b29ad 15208
4595a3b3 15209 Upon return BASES is initialized to the list of base classes (or
15210 NULL, if there are none) in the same form returned by
15211 cp_parser_base_clause.
15212
0a3b29ad 15213 Returns the TYPE of the indicated class. Sets
15214 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15215 involving a nested-name-specifier was used, and FALSE otherwise.
0a3b29ad 15216
8d52e9a6 15217 Returns error_mark_node if this is not a class-head.
9031d10b 15218
0a3b29ad 15219 Returns NULL_TREE if the class-head is syntactically valid, but
15220 semantically invalid in a way that means we should skip the entire
15221 body of the class. */
15222
15223static tree
ccb84981 15224cp_parser_class_head (cp_parser* parser,
fb3e3237 15225 bool* nested_name_specifier_p,
1a9b4c25 15226 tree *attributes_p,
15227 tree *bases)
0a3b29ad 15228{
0a3b29ad 15229 tree nested_name_specifier;
15230 enum tag_types class_key;
15231 tree id = NULL_TREE;
15232 tree type = NULL_TREE;
15233 tree attributes;
15234 bool template_id_p = false;
15235 bool qualified_p = false;
15236 bool invalid_nested_name_p = false;
e16b1a13 15237 bool invalid_explicit_specialization_p = false;
7f602bca 15238 tree pushed_scope = NULL_TREE;
0a3b29ad 15239 unsigned num_templates;
ad9ae192 15240 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
0a3b29ad 15241 /* Assume no nested-name-specifier will be present. */
15242 *nested_name_specifier_p = false;
15243 /* Assume no template parameter lists will be used in defining the
15244 type. */
15245 num_templates = 0;
15246
4595a3b3 15247 *bases = NULL_TREE;
15248
0a3b29ad 15249 /* Look for the class-key. */
15250 class_key = cp_parser_class_key (parser);
15251 if (class_key == none_type)
15252 return error_mark_node;
15253
15254 /* Parse the attributes. */
15255 attributes = cp_parser_attributes_opt (parser);
15256
15257 /* If the next token is `::', that is invalid -- but sometimes
15258 people do try to write:
15259
ccb84981 15260 struct ::S {};
0a3b29ad 15261
15262 Handle this gracefully by accepting the extra qualifier, and then
15263 issuing an error about it later if this really is a
b3c48b5d 15264 class-head. If it turns out just to be an elaborated type
0a3b29ad 15265 specifier, remain silent. */
130bb1d4 15266 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
0a3b29ad 15267 qualified_p = true;
15268
4f62c42e 15269 push_deferring_access_checks (dk_no_check);
15270
0a3b29ad 15271 /* Determine the name of the class. Begin by looking for an
15272 optional nested-name-specifier. */
ad9ae192 15273 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
ccb84981 15274 nested_name_specifier
0a3b29ad 15275 = cp_parser_nested_name_specifier_opt (parser,
15276 /*typename_keyword_p=*/false,
1bd3a9f9 15277 /*check_dependency_p=*/false,
3d0f901b 15278 /*type_p=*/false,
15279 /*is_declaration=*/false);
0a3b29ad 15280 /* If there was a nested-name-specifier, then there *must* be an
15281 identifier. */
15282 if (nested_name_specifier)
15283 {
ad9ae192 15284 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15285 /* Although the grammar says `identifier', it really means
15286 `class-name' or `template-name'. You are only allowed to
15287 define a class that has already been declared with this
ccb84981 15288 syntax.
0a3b29ad 15289
b4f2a576 15290 The proposed resolution for Core Issue 180 says that wherever
0a3b29ad 15291 you see `class T::X' you should treat `X' as a type-name.
ccb84981 15292
0a3b29ad 15293 It is OK to define an inaccessible class; for example:
ccb84981 15294
653e5405 15295 class A { class B; };
15296 class A::B {};
ccb84981 15297
653e5405 15298 We do not know if we will see a class-name, or a
0a3b29ad 15299 template-name. We look for a class-name first, in case the
15300 class-name is a template-id; if we looked for the
15301 template-name first we would stop after the template-name. */
15302 cp_parser_parse_tentatively (parser);
15303 type = cp_parser_class_name (parser,
15304 /*typename_keyword_p=*/false,
15305 /*template_keyword_p=*/false,
e2ae55f2 15306 class_type,
0a3b29ad 15307 /*check_dependency_p=*/false,
3d0f901b 15308 /*class_head_p=*/true,
15309 /*is_declaration=*/false);
0a3b29ad 15310 /* If that didn't work, ignore the nested-name-specifier. */
15311 if (!cp_parser_parse_definitely (parser))
15312 {
15313 invalid_nested_name_p = true;
eef0ab03 15314 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15315 id = cp_parser_identifier (parser);
15316 if (id == error_mark_node)
15317 id = NULL_TREE;
15318 }
15319 /* If we could not find a corresponding TYPE, treat this
15320 declaration like an unqualified declaration. */
15321 if (type == error_mark_node)
15322 nested_name_specifier = NULL_TREE;
15323 /* Otherwise, count the number of templates used in TYPE and its
15324 containing scopes. */
ccb84981 15325 else
0a3b29ad 15326 {
15327 tree scope;
15328
ccb84981 15329 for (scope = TREE_TYPE (type);
0a3b29ad 15330 scope && TREE_CODE (scope) != NAMESPACE_DECL;
ccb84981 15331 scope = (TYPE_P (scope)
0a3b29ad 15332 ? TYPE_CONTEXT (scope)
ccb84981 15333 : DECL_CONTEXT (scope)))
15334 if (TYPE_P (scope)
0a3b29ad 15335 && CLASS_TYPE_P (scope)
15336 && CLASSTYPE_TEMPLATE_INFO (scope)
b3c48b5d 15337 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15338 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
0a3b29ad 15339 ++num_templates;
15340 }
15341 }
15342 /* Otherwise, the identifier is optional. */
15343 else
15344 {
15345 /* We don't know whether what comes next is a template-id,
15346 an identifier, or nothing at all. */
15347 cp_parser_parse_tentatively (parser);
15348 /* Check for a template-id. */
ad9ae192 15349 type_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 15350 id = cp_parser_template_id (parser,
0a3b29ad 15351 /*template_keyword_p=*/false,
3d0f901b 15352 /*check_dependency_p=*/true,
15353 /*is_declaration=*/true);
0a3b29ad 15354 /* If that didn't work, it could still be an identifier. */
15355 if (!cp_parser_parse_definitely (parser))
15356 {
15357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
eef0ab03 15358 {
15359 type_start_token = cp_lexer_peek_token (parser->lexer);
15360 id = cp_parser_identifier (parser);
15361 }
0a3b29ad 15362 else
15363 id = NULL_TREE;
15364 }
15365 else
15366 {
15367 template_id_p = true;
15368 ++num_templates;
15369 }
15370 }
15371
4f62c42e 15372 pop_deferring_access_checks ();
15373
e67a67ea 15374 if (id)
eef0ab03 15375 cp_parser_check_for_invalid_template_id (parser, id,
15376 type_start_token->location);
1157e9f0 15377
0a3b29ad 15378 /* If it's not a `:' or a `{' then we can't really be looking at a
15379 class-head, since a class-head only appears as part of a
15380 class-specifier. We have to detect this situation before calling
15381 xref_tag, since that has irreversible side-effects. */
15382 if (!cp_parser_next_token_starts_class_definition_p (parser))
15383 {
a2c5b975 15384 cp_parser_error (parser, "expected %<{%> or %<:%>");
0a3b29ad 15385 return error_mark_node;
15386 }
15387
15388 /* At this point, we're going ahead with the class-specifier, even
15389 if some other problem occurs. */
15390 cp_parser_commit_to_tentative_parse (parser);
15391 /* Issue the error about the overly-qualified name now. */
15392 if (qualified_p)
71b7ff50 15393 {
15394 cp_parser_error (parser,
15395 "global qualification of class name is invalid");
15396 return error_mark_node;
15397 }
0a3b29ad 15398 else if (invalid_nested_name_p)
71b7ff50 15399 {
15400 cp_parser_error (parser,
15401 "qualified name does not name a class");
15402 return error_mark_node;
15403 }
600be0e5 15404 else if (nested_name_specifier)
15405 {
15406 tree scope;
b0ab2aae 15407
15408 /* Reject typedef-names in class heads. */
15409 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15410 {
ad9ae192 15411 error ("%Hinvalid class name in declaration of %qD",
15412 &type_start_token->location, type);
b0ab2aae 15413 type = NULL_TREE;
15414 goto done;
15415 }
15416
600be0e5 15417 /* Figure out in what scope the declaration is being placed. */
15418 scope = current_scope ();
600be0e5 15419 /* If that scope does not contain the scope in which the
15420 class was originally declared, the program is invalid. */
15421 if (scope && !is_ancestor (scope, nested_name_specifier))
15422 {
b2df4109 15423 if (at_namespace_scope_p ())
ad9ae192 15424 error ("%Hdeclaration of %qD in namespace %qD which does not "
15425 "enclose %qD",
15426 &type_start_token->location,
15427 type, scope, nested_name_specifier);
b2df4109 15428 else
ad9ae192 15429 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15430 &type_start_token->location,
b2df4109 15431 type, scope, nested_name_specifier);
600be0e5 15432 type = NULL_TREE;
15433 goto done;
15434 }
15435 /* [dcl.meaning]
15436
08cc44e7 15437 A declarator-id shall not be qualified except for the
600be0e5 15438 definition of a ... nested class outside of its class
08cc44e7 15439 ... [or] the definition or explicit instantiation of a
600be0e5 15440 class member of a namespace outside of its namespace. */
15441 if (scope == nested_name_specifier)
15442 {
2b9e3597 15443 permerror (input_location, "%Hextra qualification not allowed",
ad9ae192 15444 &nested_name_specifier_token_start->location);
600be0e5 15445 nested_name_specifier = NULL_TREE;
15446 num_templates = 0;
15447 }
15448 }
e16b1a13 15449 /* An explicit-specialization must be preceded by "template <>". If
15450 it is not, try to recover gracefully. */
ccb84981 15451 if (at_namespace_scope_p ()
e16b1a13 15452 && parser->num_template_parameter_lists == 0
4ccffa39 15453 && template_id_p)
e16b1a13 15454 {
ad9ae192 15455 error ("%Han explicit specialization must be preceded by %<template <>%>",
15456 &type_start_token->location);
e16b1a13 15457 invalid_explicit_specialization_p = true;
15458 /* Take the same action that would have been taken by
15459 cp_parser_explicit_specialization. */
15460 ++parser->num_template_parameter_lists;
15461 begin_specialization ();
15462 }
15463 /* There must be no "return" statements between this point and the
15464 end of this function; set "type "to the correct return value and
15465 use "goto done;" to return. */
0a3b29ad 15466 /* Make sure that the right number of template parameters were
15467 present. */
ad9ae192 15468 if (!cp_parser_check_template_parameters (parser, num_templates,
7b07a15e 15469 type_start_token->location,
15470 /*declarator=*/NULL))
e16b1a13 15471 {
15472 /* If something went wrong, there is no point in even trying to
15473 process the class-definition. */
15474 type = NULL_TREE;
15475 goto done;
15476 }
0a3b29ad 15477
0a3b29ad 15478 /* Look up the type. */
15479 if (template_id_p)
15480 {
04a3504e 15481 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15482 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15483 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15484 {
ad9ae192 15485 error ("%Hfunction template %qD redeclared as a class template",
15486 &type_start_token->location, id);
04a3504e 15487 type = error_mark_node;
15488 }
15489 else
15490 {
15491 type = TREE_TYPE (id);
15492 type = maybe_process_partial_specialization (type);
15493 }
7f602bca 15494 if (nested_name_specifier)
15495 pushed_scope = push_scope (nested_name_specifier);
0a3b29ad 15496 }
7f602bca 15497 else if (nested_name_specifier)
0a3b29ad 15498 {
0a3b29ad 15499 tree class_type;
15500
15501 /* Given:
15502
15503 template <typename T> struct S { struct T };
5f6526e1 15504 template <typename T> struct S<T>::T { };
0a3b29ad 15505
15506 we will get a TYPENAME_TYPE when processing the definition of
15507 `S::T'. We need to resolve it to the actual type before we
15508 try to define it. */
15509 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15510 {
5f6526e1 15511 class_type = resolve_typename_type (TREE_TYPE (type),
15512 /*only_current_p=*/false);
8826a863 15513 if (TREE_CODE (class_type) != TYPENAME_TYPE)
5f6526e1 15514 type = TYPE_NAME (class_type);
15515 else
15516 {
15517 cp_parser_error (parser, "could not resolve typename type");
15518 type = error_mark_node;
15519 }
0a3b29ad 15520 }
15521
e01a7685 15522 if (maybe_process_partial_specialization (TREE_TYPE (type))
15523 == error_mark_node)
15524 {
15525 type = NULL_TREE;
15526 goto done;
15527 }
15528
8172be22 15529 class_type = current_class_type;
15530 /* Enter the scope indicated by the nested-name-specifier. */
7f602bca 15531 pushed_scope = push_scope (nested_name_specifier);
8172be22 15532 /* Get the canonical version of this type. */
15533 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15534 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15535 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
8d52e9a6 15536 {
15537 type = push_template_decl (type);
15538 if (type == error_mark_node)
15539 {
15540 type = NULL_TREE;
15541 goto done;
15542 }
15543 }
9031d10b 15544
8172be22 15545 type = TREE_TYPE (type);
7f602bca 15546 *nested_name_specifier_p = true;
0a3b29ad 15547 }
7f602bca 15548 else /* The name is not a nested name. */
15549 {
15550 /* If the class was unnamed, create a dummy name. */
15551 if (!id)
15552 id = make_anon_name ();
15553 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15554 parser->num_template_parameter_lists);
15555 }
15556
0a3b29ad 15557 /* Indicate whether this class was declared as a `class' or as a
15558 `struct'. */
15559 if (TREE_CODE (type) == RECORD_TYPE)
15560 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15561 cp_parser_check_class_key (class_key, type);
15562
4cbba981 15563 /* If this type was already complete, and we see another definition,
15564 that's an error. */
15565 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15566 {
ad9ae192 15567 error ("%Hredefinition of %q#T",
15568 &type_start_token->location, type);
15569 error ("%Hprevious definition of %q+#T",
15570 &type_start_token->location, type);
94637815 15571 type = NULL_TREE;
15572 goto done;
4cbba981 15573 }
1a9b4c25 15574 else if (type == error_mark_node)
15575 type = NULL_TREE;
4cbba981 15576
7f602bca 15577 /* We will have entered the scope containing the class; the names of
4cbba981 15578 base classes should be looked up in that context. For example:
0a3b29ad 15579
15580 struct A { struct B {}; struct C; };
15581 struct A::C : B {};
15582
15583 is valid. */
207355ad 15584
a6460bf1 15585 /* Get the list of base-classes, if there is one. */
15586 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
1a9b4c25 15587 *bases = cp_parser_base_clause (parser);
0a3b29ad 15588
7f602bca 15589 done:
0a3b29ad 15590 /* Leave the scope given by the nested-name-specifier. We will
15591 enter the class scope itself while processing the members. */
7f602bca 15592 if (pushed_scope)
15593 pop_scope (pushed_scope);
0a3b29ad 15594
e16b1a13 15595 if (invalid_explicit_specialization_p)
15596 {
15597 end_specialization ();
15598 --parser->num_template_parameter_lists;
15599 }
fb3e3237 15600 *attributes_p = attributes;
0a3b29ad 15601 return type;
15602}
15603
15604/* Parse a class-key.
15605
15606 class-key:
15607 class
15608 struct
15609 union
15610
15611 Returns the kind of class-key specified, or none_type to indicate
15612 error. */
15613
15614static enum tag_types
45baea8b 15615cp_parser_class_key (cp_parser* parser)
0a3b29ad 15616{
15617 cp_token *token;
15618 enum tag_types tag_type;
15619
15620 /* Look for the class-key. */
15621 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15622 if (!token)
15623 return none_type;
15624
15625 /* Check to see if the TOKEN is a class-key. */
15626 tag_type = cp_parser_token_is_class_key (token);
15627 if (!tag_type)
15628 cp_parser_error (parser, "expected class-key");
15629 return tag_type;
15630}
15631
15632/* Parse an (optional) member-specification.
15633
15634 member-specification:
15635 member-declaration member-specification [opt]
15636 access-specifier : member-specification [opt] */
15637
15638static void
45baea8b 15639cp_parser_member_specification_opt (cp_parser* parser)
0a3b29ad 15640{
15641 while (true)
15642 {
15643 cp_token *token;
15644 enum rid keyword;
15645
15646 /* Peek at the next token. */
15647 token = cp_lexer_peek_token (parser->lexer);
15648 /* If it's a `}', or EOF then we've seen all the members. */
b75b98aa 15649 if (token->type == CPP_CLOSE_BRACE
15650 || token->type == CPP_EOF
15651 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 15652 break;
15653
15654 /* See if this token is a keyword. */
15655 keyword = token->keyword;
15656 switch (keyword)
15657 {
15658 case RID_PUBLIC:
15659 case RID_PROTECTED:
15660 case RID_PRIVATE:
15661 /* Consume the access-specifier. */
15662 cp_lexer_consume_token (parser->lexer);
15663 /* Remember which access-specifier is active. */
3369eb76 15664 current_access_specifier = token->u.value;
0a3b29ad 15665 /* Look for the `:'. */
640710cf 15666 cp_parser_require (parser, CPP_COLON, "%<:%>");
0a3b29ad 15667 break;
15668
15669 default:
882d3c4f 15670 /* Accept #pragmas at class scope. */
15671 if (token->type == CPP_PRAGMA)
15672 {
b75b98aa 15673 cp_parser_pragma (parser, pragma_external);
882d3c4f 15674 break;
15675 }
15676
0a3b29ad 15677 /* Otherwise, the next construction must be a
15678 member-declaration. */
15679 cp_parser_member_declaration (parser);
0a3b29ad 15680 }
15681 }
15682}
15683
ccb84981 15684/* Parse a member-declaration.
0a3b29ad 15685
15686 member-declaration:
15687 decl-specifier-seq [opt] member-declarator-list [opt] ;
15688 function-definition ; [opt]
15689 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15690 using-declaration
ccb84981 15691 template-declaration
0a3b29ad 15692
15693 member-declarator-list:
15694 member-declarator
15695 member-declarator-list , member-declarator
15696
15697 member-declarator:
ccb84981 15698 declarator pure-specifier [opt]
0a3b29ad 15699 declarator constant-initializer [opt]
ccb84981 15700 identifier [opt] : constant-expression
0a3b29ad 15701
15702 GNU Extensions:
15703
15704 member-declaration:
15705 __extension__ member-declaration
15706
15707 member-declarator:
15708 declarator attributes [opt] pure-specifier [opt]
15709 declarator attributes [opt] constant-initializer [opt]
7a05c4b1 15710 identifier [opt] attributes [opt] : constant-expression
15711
15712 C++0x Extensions:
15713
15714 member-declaration:
15715 static_assert-declaration */
0a3b29ad 15716
15717static void
45baea8b 15718cp_parser_member_declaration (cp_parser* parser)
0a3b29ad 15719{
4b9b2871 15720 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 15721 tree prefix_attributes;
15722 tree decl;
8172be22 15723 int declares_class_or_enum;
0a3b29ad 15724 bool friend_p;
ad9ae192 15725 cp_token *token = NULL;
15726 cp_token *decl_spec_token_start = NULL;
15727 cp_token *initializer_token_start = NULL;
0a3b29ad 15728 int saved_pedantic;
15729
15730 /* Check for the `__extension__' keyword. */
15731 if (cp_parser_extension_opt (parser, &saved_pedantic))
15732 {
15733 /* Recurse. */
15734 cp_parser_member_declaration (parser);
15735 /* Restore the old value of the PEDANTIC flag. */
15736 pedantic = saved_pedantic;
15737
15738 return;
15739 }
15740
15741 /* Check for a template-declaration. */
15742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15743 {
81ef2172 15744 /* An explicit specialization here is an error condition, and we
15745 expect the specialization handler to detect and report this. */
15746 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15747 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15748 cp_parser_explicit_specialization (parser);
15749 else
15750 cp_parser_template_declaration (parser, /*member_p=*/true);
0a3b29ad 15751
15752 return;
15753 }
15754
15755 /* Check for a using-declaration. */
15756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15757 {
15758 /* Parse the using-declaration. */
da2a3271 15759 cp_parser_using_declaration (parser,
15760 /*access_declaration_p=*/false);
0a3b29ad 15761 return;
15762 }
ccb84981 15763
7a4e126b 15764 /* Check for @defs. */
15765 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15766 {
15767 tree ivar, member;
15768 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15769 ivar = ivar_chains;
15770 while (ivar)
15771 {
15772 member = ivar;
15773 ivar = TREE_CHAIN (member);
15774 TREE_CHAIN (member) = NULL_TREE;
15775 finish_member_declaration (member);
15776 }
15777 return;
15778 }
15779
7a05c4b1 15780 /* If the next token is `static_assert' we have a static assertion. */
15781 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15782 {
15783 cp_parser_static_assert (parser, /*member_p=*/true);
15784 return;
15785 }
15786
da2a3271 15787 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15788 return;
15789
0a3b29ad 15790 /* Parse the decl-specifier-seq. */
ad9ae192 15791 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 15792 cp_parser_decl_specifier_seq (parser,
15793 CP_PARSER_FLAGS_OPTIONAL,
15794 &decl_specifiers,
15795 &declares_class_or_enum);
15796 prefix_attributes = decl_specifiers.attributes;
15797 decl_specifiers.attributes = NULL_TREE;
954ad420 15798 /* Check for an invalid type-name. */
882d3c4f 15799 if (!decl_specifiers.type
15800 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
954ad420 15801 return;
0a3b29ad 15802 /* If there is no declarator, then the decl-specifier-seq should
15803 specify a type. */
15804 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15805 {
15806 /* If there was no decl-specifier-seq, and the next token is a
15807 `;', then we have something like:
15808
15809 struct S { ; };
15810
15811 [class.mem]
15812
15813 Each member-declaration shall declare at least one member
15814 name of the class. */
4b9b2871 15815 if (!decl_specifiers.any_specifiers_p)
0a3b29ad 15816 {
b9dd3954 15817 cp_token *token = cp_lexer_peek_token (parser->lexer);
8864917d 15818 if (!in_system_header_at (token->location))
21ca8540 15819 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
0a3b29ad 15820 }
ccb84981 15821 else
0a3b29ad 15822 {
15823 tree type;
ccb84981 15824
0a3b29ad 15825 /* See if this declaration is a friend. */
4b9b2871 15826 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 15827 /* If there were decl-specifiers, check to see if there was
15828 a class-declaration. */
4b9b2871 15829 type = check_tag_decl (&decl_specifiers);
0a3b29ad 15830 /* Nested classes have already been added to the class, but
15831 a `friend' needs to be explicitly registered. */
15832 if (friend_p)
15833 {
15834 /* If the `friend' keyword was present, the friend must
15835 be introduced with a class-key. */
15836 if (!declares_class_or_enum)
ad9ae192 15837 error ("%Ha class-key must be used when declaring a friend",
15838 &decl_spec_token_start->location);
0a3b29ad 15839 /* In this case:
15840
ccb84981 15841 template <typename T> struct A {
653e5405 15842 friend struct A<T>::B;
15843 };
ccb84981 15844
0a3b29ad 15845 A<T>::B will be represented by a TYPENAME_TYPE, and
15846 therefore not recognized by check_tag_decl. */
207355ad 15847 if (!type
4b9b2871 15848 && decl_specifiers.type
15849 && TYPE_P (decl_specifiers.type))
15850 type = decl_specifiers.type;
069754af 15851 if (!type || !TYPE_P (type))
ad9ae192 15852 error ("%Hfriend declaration does not name a class or "
15853 "function", &decl_spec_token_start->location);
0a3b29ad 15854 else
b123b79d 15855 make_friend_class (current_class_type, type,
15856 /*complain=*/true);
0a3b29ad 15857 }
15858 /* If there is no TYPE, an error message will already have
15859 been issued. */
4b9b2871 15860 else if (!type || type == error_mark_node)
0a3b29ad 15861 ;
15862 /* An anonymous aggregate has to be handled specially; such
15863 a declaration really declares a data member (with a
15864 particular type), as opposed to a nested class. */
15865 else if (ANON_AGGR_TYPE_P (type))
15866 {
15867 /* Remove constructors and such from TYPE, now that we
755edffd 15868 know it is an anonymous aggregate. */
0a3b29ad 15869 fixup_anonymous_aggr (type);
15870 /* And make the corresponding data member. */
e60a6f7b 15871 decl = build_decl (decl_spec_token_start->location,
15872 FIELD_DECL, NULL_TREE, type);
0a3b29ad 15873 /* Add it to the class. */
15874 finish_member_declaration (decl);
15875 }
7e35473e 15876 else
ad9ae192 15877 cp_parser_check_access_in_redeclaration
15878 (TYPE_NAME (type),
15879 decl_spec_token_start->location);
0a3b29ad 15880 }
15881 }
15882 else
15883 {
15884 /* See if these declarations will be friends. */
4b9b2871 15885 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 15886
ccb84981 15887 /* Keep going until we hit the `;' at the end of the
0a3b29ad 15888 declaration. */
15889 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15890 {
15891 tree attributes = NULL_TREE;
15892 tree first_attribute;
15893
15894 /* Peek at the next token. */
15895 token = cp_lexer_peek_token (parser->lexer);
15896
15897 /* Check for a bitfield declaration. */
15898 if (token->type == CPP_COLON
15899 || (token->type == CPP_NAME
ccb84981 15900 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 15901 == CPP_COLON))
15902 {
15903 tree identifier;
15904 tree width;
15905
15906 /* Get the name of the bitfield. Note that we cannot just
15907 check TOKEN here because it may have been invalidated by
15908 the call to cp_lexer_peek_nth_token above. */
15909 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15910 identifier = cp_parser_identifier (parser);
15911 else
15912 identifier = NULL_TREE;
15913
15914 /* Consume the `:' token. */
15915 cp_lexer_consume_token (parser->lexer);
15916 /* Get the width of the bitfield. */
ccb84981 15917 width
5f6526e1 15918 = cp_parser_constant_expression (parser,
15919 /*allow_non_constant=*/false,
15920 NULL);
0a3b29ad 15921
15922 /* Look for attributes that apply to the bitfield. */
15923 attributes = cp_parser_attributes_opt (parser);
15924 /* Remember which attributes are prefix attributes and
15925 which are not. */
15926 first_attribute = attributes;
15927 /* Combine the attributes. */
15928 attributes = chainon (prefix_attributes, attributes);
15929
15930 /* Create the bitfield declaration. */
207355ad 15931 decl = grokbitfield (identifier
2ded3667 15932 ? make_id_declarator (NULL_TREE,
2366ed31 15933 identifier,
15934 sfk_none)
3046c0a3 15935 : NULL,
4b9b2871 15936 &decl_specifiers,
f922e029 15937 width,
15938 attributes);
0a3b29ad 15939 }
15940 else
15941 {
3046c0a3 15942 cp_declarator *declarator;
0a3b29ad 15943 tree initializer;
15944 tree asm_specification;
0986fa22 15945 int ctor_dtor_or_conv_p;
0a3b29ad 15946
15947 /* Parse the declarator. */
ccb84981 15948 declarator
42bbd0ec 15949 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 15950 &ctor_dtor_or_conv_p,
08ea345c 15951 /*parenthesized_p=*/NULL,
15952 /*member_p=*/true);
0a3b29ad 15953
15954 /* If something went wrong parsing the declarator, make sure
15955 that we at least consume some tokens. */
3046c0a3 15956 if (declarator == cp_error_declarator)
0a3b29ad 15957 {
15958 /* Skip to the end of the statement. */
15959 cp_parser_skip_to_end_of_statement (parser);
92b128ed 15960 /* If the next token is not a semicolon, that is
15961 probably because we just skipped over the body of
15962 a function. So, we consume a semicolon if
15963 present, but do not issue an error message if it
15964 is not present. */
15965 if (cp_lexer_next_token_is (parser->lexer,
15966 CPP_SEMICOLON))
15967 cp_lexer_consume_token (parser->lexer);
15968 return;
0a3b29ad 15969 }
15970
e2ae55f2 15971 if (declares_class_or_enum & 2)
15972 cp_parser_check_for_definition_in_return_type
eef0ab03 15973 (declarator, decl_specifiers.type,
15974 decl_specifiers.type_location);
8172be22 15975
0a3b29ad 15976 /* Look for an asm-specification. */
15977 asm_specification = cp_parser_asm_specification_opt (parser);
15978 /* Look for attributes that apply to the declaration. */
15979 attributes = cp_parser_attributes_opt (parser);
15980 /* Remember which attributes are prefix attributes and
15981 which are not. */
15982 first_attribute = attributes;
15983 /* Combine the attributes. */
15984 attributes = chainon (prefix_attributes, attributes);
15985
15986 /* If it's an `=', then we have a constant-initializer or a
15987 pure-specifier. It is not correct to parse the
15988 initializer before registering the member declaration
15989 since the member declaration should be in scope while
15990 its initializer is processed. However, the rest of the
15991 front end does not yet provide an interface that allows
15992 us to handle this correctly. */
15993 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15994 {
15995 /* In [class.mem]:
15996
15997 A pure-specifier shall be used only in the declaration of
ccb84981 15998 a virtual function.
0a3b29ad 15999
16000 A member-declarator can contain a constant-initializer
16001 only if it declares a static member of integral or
ccb84981 16002 enumeration type.
0a3b29ad 16003
16004 Therefore, if the DECLARATOR is for a function, we look
16005 for a pure-specifier; otherwise, we look for a
16006 constant-initializer. When we call `grokfield', it will
16007 perform more stringent semantics checks. */
ad9ae192 16008 initializer_token_start = cp_lexer_peek_token (parser->lexer);
3a30cb7f 16009 if (function_declarator_p (declarator))
0a3b29ad 16010 initializer = cp_parser_pure_specifier (parser);
16011 else
92b128ed 16012 /* Parse the initializer. */
16013 initializer = cp_parser_constant_initializer (parser);
0a3b29ad 16014 }
16015 /* Otherwise, there is no initializer. */
16016 else
16017 initializer = NULL_TREE;
16018
16019 /* See if we are probably looking at a function
f29f4570 16020 definition. We are certainly not looking at a
0a3b29ad 16021 member-declarator. Calling `grokfield' has
16022 side-effects, so we must not do it unless we are sure
16023 that we are looking at a member-declarator. */
ccb84981 16024 if (cp_parser_token_starts_function_definition_p
0a3b29ad 16025 (cp_lexer_peek_token (parser->lexer)))
92b128ed 16026 {
16027 /* The grammar does not allow a pure-specifier to be
16028 used when a member function is defined. (It is
16029 possible that this fact is an oversight in the
16030 standard, since a pure function may be defined
16031 outside of the class-specifier. */
16032 if (initializer)
ad9ae192 16033 error ("%Hpure-specifier on function-definition",
16034 &initializer_token_start->location);
92b128ed 16035 decl = cp_parser_save_member_function_body (parser,
4b9b2871 16036 &decl_specifiers,
92b128ed 16037 declarator,
16038 attributes);
16039 /* If the member was not a friend, declare it here. */
16040 if (!friend_p)
16041 finish_member_declaration (decl);
16042 /* Peek at the next token. */
16043 token = cp_lexer_peek_token (parser->lexer);
16044 /* If the next token is a semicolon, consume it. */
16045 if (token->type == CPP_SEMICOLON)
e6ae3136 16046 cp_lexer_consume_token (parser->lexer);
92b128ed 16047 return;
16048 }
0a3b29ad 16049 else
d19f0a18 16050 if (declarator->kind == cdk_function)
16051 declarator->id_loc = token->location;
d91303a6 16052 /* Create the declaration. */
16053 decl = grokfield (declarator, &decl_specifiers,
16054 initializer, /*init_const_expr_p=*/true,
16055 asm_specification,
16056 attributes);
0a3b29ad 16057 }
16058
16059 /* Reset PREFIX_ATTRIBUTES. */
16060 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16061 attributes = TREE_CHAIN (attributes);
16062 if (attributes)
16063 TREE_CHAIN (attributes) = NULL_TREE;
16064
16065 /* If there is any qualification still in effect, clear it
16066 now; we will be starting fresh with the next declarator. */
16067 parser->scope = NULL_TREE;
16068 parser->qualifying_scope = NULL_TREE;
16069 parser->object_scope = NULL_TREE;
16070 /* If it's a `,', then there are more declarators. */
16071 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16072 cp_lexer_consume_token (parser->lexer);
16073 /* If the next token isn't a `;', then we have a parse error. */
16074 else if (cp_lexer_next_token_is_not (parser->lexer,
16075 CPP_SEMICOLON))
16076 {
a2c5b975 16077 cp_parser_error (parser, "expected %<;%>");
bd8962d5 16078 /* Skip tokens until we find a `;'. */
0a3b29ad 16079 cp_parser_skip_to_end_of_statement (parser);
16080
16081 break;
16082 }
16083
16084 if (decl)
16085 {
16086 /* Add DECL to the list of members. */
16087 if (!friend_p)
16088 finish_member_declaration (decl);
16089
0a3b29ad 16090 if (TREE_CODE (decl) == FUNCTION_DECL)
69b6679c 16091 cp_parser_save_default_args (parser, decl);
0a3b29ad 16092 }
16093 }
16094 }
16095
640710cf 16096 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 16097}
16098
16099/* Parse a pure-specifier.
16100
16101 pure-specifier:
16102 = 0
16103
16104 Returns INTEGER_ZERO_NODE if a pure specifier is found.
63eff20d 16105 Otherwise, ERROR_MARK_NODE is returned. */
0a3b29ad 16106
16107static tree
45baea8b 16108cp_parser_pure_specifier (cp_parser* parser)
0a3b29ad 16109{
16110 cp_token *token;
16111
16112 /* Look for the `=' token. */
640710cf 16113 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
0a3b29ad 16114 return error_mark_node;
16115 /* Look for the `0' token. */
d8acda95 16116 token = cp_lexer_peek_token (parser->lexer);
16117
16118 if (token->type == CPP_EOF
16119 || token->type == CPP_PRAGMA_EOL)
16120 return error_mark_node;
16121
16122 cp_lexer_consume_token (parser->lexer);
2336da2a 16123
16124 /* Accept = default or = delete in c++0x mode. */
16125 if (token->keyword == RID_DEFAULT
16126 || token->keyword == RID_DELETE)
16127 {
16128 maybe_warn_cpp0x ("defaulted and deleted functions");
16129 return token->u.value;
16130 }
16131
8dba02f7 16132 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
5f69415d 16133 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16134 {
074ab442 16135 cp_parser_error (parser,
640710cf 16136 "invalid pure specifier (only %<= 0%> is allowed)");
5f69415d 16137 cp_parser_skip_to_end_of_statement (parser);
16138 return error_mark_node;
16139 }
16140 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16141 {
ad9ae192 16142 error ("%Htemplates may not be %<virtual%>", &token->location);
5f69415d 16143 return error_mark_node;
16144 }
0a3b29ad 16145
5f69415d 16146 return integer_zero_node;
0a3b29ad 16147}
16148
16149/* Parse a constant-initializer.
16150
16151 constant-initializer:
16152 = constant-expression
16153
16154 Returns a representation of the constant-expression. */
16155
16156static tree
45baea8b 16157cp_parser_constant_initializer (cp_parser* parser)
0a3b29ad 16158{
16159 /* Look for the `=' token. */
640710cf 16160 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
0a3b29ad 16161 return error_mark_node;
16162
16163 /* It is invalid to write:
16164
16165 struct S { static const int i = { 7 }; };
16166
16167 */
16168 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16169 {
16170 cp_parser_error (parser,
16171 "a brace-enclosed initializer is not allowed here");
16172 /* Consume the opening brace. */
16173 cp_lexer_consume_token (parser->lexer);
16174 /* Skip the initializer. */
16175 cp_parser_skip_to_closing_brace (parser);
16176 /* Look for the trailing `}'. */
640710cf 16177 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
ccb84981 16178
0a3b29ad 16179 return error_mark_node;
16180 }
16181
ccb84981 16182 return cp_parser_constant_expression (parser,
5f6526e1 16183 /*allow_non_constant=*/false,
16184 NULL);
0a3b29ad 16185}
16186
16187/* Derived classes [gram.class.derived] */
16188
16189/* Parse a base-clause.
16190
16191 base-clause:
ccb84981 16192 : base-specifier-list
0a3b29ad 16193
16194 base-specifier-list:
d95d815d 16195 base-specifier ... [opt]
16196 base-specifier-list , base-specifier ... [opt]
0a3b29ad 16197
16198 Returns a TREE_LIST representing the base-classes, in the order in
16199 which they were declared. The representation of each node is as
ccb84981 16200 described by cp_parser_base_specifier.
0a3b29ad 16201
16202 In the case that no bases are specified, this function will return
16203 NULL_TREE, not ERROR_MARK_NODE. */
16204
16205static tree
45baea8b 16206cp_parser_base_clause (cp_parser* parser)
0a3b29ad 16207{
16208 tree bases = NULL_TREE;
16209
16210 /* Look for the `:' that begins the list. */
640710cf 16211 cp_parser_require (parser, CPP_COLON, "%<:%>");
0a3b29ad 16212
16213 /* Scan the base-specifier-list. */
16214 while (true)
16215 {
16216 cp_token *token;
16217 tree base;
d95d815d 16218 bool pack_expansion_p = false;
0a3b29ad 16219
16220 /* Look for the base-specifier. */
16221 base = cp_parser_base_specifier (parser);
d95d815d 16222 /* Look for the (optional) ellipsis. */
16223 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16224 {
16225 /* Consume the `...'. */
16226 cp_lexer_consume_token (parser->lexer);
16227
16228 pack_expansion_p = true;
16229 }
16230
0a3b29ad 16231 /* Add BASE to the front of the list. */
16232 if (base != error_mark_node)
16233 {
d95d815d 16234 if (pack_expansion_p)
16235 /* Make this a pack expansion type. */
16236 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
613687b1 16237
d95d815d 16238
830a6615 16239 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
613687b1 16240 {
16241 TREE_CHAIN (base) = bases;
16242 bases = base;
16243 }
0a3b29ad 16244 }
16245 /* Peek at the next token. */
16246 token = cp_lexer_peek_token (parser->lexer);
16247 /* If it's not a comma, then the list is complete. */
16248 if (token->type != CPP_COMMA)
16249 break;
16250 /* Consume the `,'. */
16251 cp_lexer_consume_token (parser->lexer);
16252 }
16253
16254 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16255 base class had a qualified name. However, the next name that
16256 appears is certainly not qualified. */
16257 parser->scope = NULL_TREE;
16258 parser->qualifying_scope = NULL_TREE;
16259 parser->object_scope = NULL_TREE;
16260
16261 return nreverse (bases);
16262}
16263
16264/* Parse a base-specifier.
16265
16266 base-specifier:
16267 :: [opt] nested-name-specifier [opt] class-name
16268 virtual access-specifier [opt] :: [opt] nested-name-specifier
16269 [opt] class-name
16270 access-specifier virtual [opt] :: [opt] nested-name-specifier
16271 [opt] class-name
16272
16273 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16274 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16275 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16276 (or the ERROR_MARK_NODE) indicating the type that was specified. */
ccb84981 16277
0a3b29ad 16278static tree
45baea8b 16279cp_parser_base_specifier (cp_parser* parser)
0a3b29ad 16280{
16281 cp_token *token;
16282 bool done = false;
16283 bool virtual_p = false;
16284 bool duplicate_virtual_error_issued_p = false;
16285 bool duplicate_access_error_issued_p = false;
d622b3bd 16286 bool class_scope_p, template_p;
95f3173a 16287 tree access = access_default_node;
0a3b29ad 16288 tree type;
16289
16290 /* Process the optional `virtual' and `access-specifier'. */
16291 while (!done)
16292 {
16293 /* Peek at the next token. */
16294 token = cp_lexer_peek_token (parser->lexer);
16295 /* Process `virtual'. */
16296 switch (token->keyword)
16297 {
16298 case RID_VIRTUAL:
16299 /* If `virtual' appears more than once, issue an error. */
16300 if (virtual_p && !duplicate_virtual_error_issued_p)
16301 {
16302 cp_parser_error (parser,
a2c5b975 16303 "%<virtual%> specified more than once in base-specified");
0a3b29ad 16304 duplicate_virtual_error_issued_p = true;
16305 }
16306
16307 virtual_p = true;
16308
16309 /* Consume the `virtual' token. */
16310 cp_lexer_consume_token (parser->lexer);
16311
16312 break;
16313
16314 case RID_PUBLIC:
16315 case RID_PROTECTED:
16316 case RID_PRIVATE:
16317 /* If more than one access specifier appears, issue an
16318 error. */
95f3173a 16319 if (access != access_default_node
16320 && !duplicate_access_error_issued_p)
0a3b29ad 16321 {
16322 cp_parser_error (parser,
16323 "more than one access specifier in base-specified");
16324 duplicate_access_error_issued_p = true;
16325 }
16326
95f3173a 16327 access = ridpointers[(int) token->keyword];
0a3b29ad 16328
16329 /* Consume the access-specifier. */
16330 cp_lexer_consume_token (parser->lexer);
16331
16332 break;
16333
16334 default:
16335 done = true;
16336 break;
16337 }
16338 }
0f3ccaa3 16339 /* It is not uncommon to see programs mechanically, erroneously, use
eae805b4 16340 the 'typename' keyword to denote (dependent) qualified types
9591b260 16341 as base classes. */
16342 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16343 {
ad9ae192 16344 token = cp_lexer_peek_token (parser->lexer);
9591b260 16345 if (!processing_template_decl)
ad9ae192 16346 error ("%Hkeyword %<typename%> not allowed outside of templates",
16347 &token->location);
9591b260 16348 else
ad9ae192 16349 error ("%Hkeyword %<typename%> not allowed in this context "
16350 "(the base class is implicitly a type)",
16351 &token->location);
9591b260 16352 cp_lexer_consume_token (parser->lexer);
16353 }
0a3b29ad 16354
0a3b29ad 16355 /* Look for the optional `::' operator. */
130bb1d4 16356 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
0a3b29ad 16357 /* Look for the nested-name-specifier. The simplest way to
16358 implement:
16359
16360 [temp.res]
16361
16362 The keyword `typename' is not permitted in a base-specifier or
16363 mem-initializer; in these contexts a qualified name that
16364 depends on a template-parameter is implicitly assumed to be a
16365 type name.
16366
16367 is to pretend that we have seen the `typename' keyword at this
ccb84981 16368 point. */
0a3b29ad 16369 cp_parser_nested_name_specifier_opt (parser,
16370 /*typename_keyword_p=*/true,
16371 /*check_dependency_p=*/true,
e2ae55f2 16372 typename_type,
3d0f901b 16373 /*is_declaration=*/true);
0a3b29ad 16374 /* If the base class is given by a qualified name, assume that names
16375 we see are type names or templates, as appropriate. */
16376 class_scope_p = (parser->scope && TYPE_P (parser->scope));
d622b3bd 16377 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
ccb84981 16378
0a3b29ad 16379 /* Finally, look for the class-name. */
ccb84981 16380 type = cp_parser_class_name (parser,
0a3b29ad 16381 class_scope_p,
d622b3bd 16382 template_p,
e2ae55f2 16383 typename_type,
0a3b29ad 16384 /*check_dependency_p=*/true,
3d0f901b 16385 /*class_head_p=*/false,
16386 /*is_declaration=*/true);
0a3b29ad 16387
16388 if (type == error_mark_node)
16389 return error_mark_node;
16390
95f3173a 16391 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
0a3b29ad 16392}
16393
16394/* Exception handling [gram.exception] */
16395
16396/* Parse an (optional) exception-specification.
16397
16398 exception-specification:
16399 throw ( type-id-list [opt] )
16400
16401 Returns a TREE_LIST representing the exception-specification. The
16402 TREE_VALUE of each node is a type. */
16403
16404static tree
45baea8b 16405cp_parser_exception_specification_opt (cp_parser* parser)
0a3b29ad 16406{
16407 cp_token *token;
16408 tree type_id_list;
16409
16410 /* Peek at the next token. */
16411 token = cp_lexer_peek_token (parser->lexer);
16412 /* If it's not `throw', then there's no exception-specification. */
16413 if (!cp_parser_is_keyword (token, RID_THROW))
16414 return NULL_TREE;
16415
16416 /* Consume the `throw'. */
16417 cp_lexer_consume_token (parser->lexer);
16418
16419 /* Look for the `('. */
640710cf 16420 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 16421
16422 /* Peek at the next token. */
16423 token = cp_lexer_peek_token (parser->lexer);
16424 /* If it's not a `)', then there is a type-id-list. */
16425 if (token->type != CPP_CLOSE_PAREN)
16426 {
16427 const char *saved_message;
16428
16429 /* Types may not be defined in an exception-specification. */
16430 saved_message = parser->type_definition_forbidden_message;
16431 parser->type_definition_forbidden_message
16432 = "types may not be defined in an exception-specification";
16433 /* Parse the type-id-list. */
16434 type_id_list = cp_parser_type_id_list (parser);
16435 /* Restore the saved message. */
16436 parser->type_definition_forbidden_message = saved_message;
16437 }
16438 else
16439 type_id_list = empty_except_spec;
16440
16441 /* Look for the `)'. */
640710cf 16442 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 16443
16444 return type_id_list;
16445}
16446
16447/* Parse an (optional) type-id-list.
16448
16449 type-id-list:
d95d815d 16450 type-id ... [opt]
16451 type-id-list , type-id ... [opt]
0a3b29ad 16452
16453 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16454 in the order that the types were presented. */
16455
16456static tree
45baea8b 16457cp_parser_type_id_list (cp_parser* parser)
0a3b29ad 16458{
16459 tree types = NULL_TREE;
16460
16461 while (true)
16462 {
16463 cp_token *token;
16464 tree type;
16465
16466 /* Get the next type-id. */
16467 type = cp_parser_type_id (parser);
d95d815d 16468 /* Parse the optional ellipsis. */
16469 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16470 {
16471 /* Consume the `...'. */
16472 cp_lexer_consume_token (parser->lexer);
16473
16474 /* Turn the type into a pack expansion expression. */
16475 type = make_pack_expansion (type);
16476 }
0a3b29ad 16477 /* Add it to the list. */
16478 types = add_exception_specifier (types, type, /*complain=*/1);
16479 /* Peek at the next token. */
16480 token = cp_lexer_peek_token (parser->lexer);
16481 /* If it is not a `,', we are done. */
16482 if (token->type != CPP_COMMA)
16483 break;
16484 /* Consume the `,'. */
16485 cp_lexer_consume_token (parser->lexer);
16486 }
16487
16488 return nreverse (types);
16489}
16490
16491/* Parse a try-block.
16492
16493 try-block:
16494 try compound-statement handler-seq */
16495
16496static tree
45baea8b 16497cp_parser_try_block (cp_parser* parser)
0a3b29ad 16498{
16499 tree try_block;
16500
640710cf 16501 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
0a3b29ad 16502 try_block = begin_try_block ();
2363ef00 16503 cp_parser_compound_statement (parser, NULL, true);
0a3b29ad 16504 finish_try_block (try_block);
16505 cp_parser_handler_seq (parser);
16506 finish_handler_sequence (try_block);
16507
16508 return try_block;
16509}
16510
16511/* Parse a function-try-block.
16512
16513 function-try-block:
16514 try ctor-initializer [opt] function-body handler-seq */
16515
16516static bool
45baea8b 16517cp_parser_function_try_block (cp_parser* parser)
0a3b29ad 16518{
78f7169a 16519 tree compound_stmt;
0a3b29ad 16520 tree try_block;
16521 bool ctor_initializer_p;
16522
16523 /* Look for the `try' keyword. */
640710cf 16524 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
0a3b29ad 16525 return false;
a17c2a3a 16526 /* Let the rest of the front end know where we are. */
78f7169a 16527 try_block = begin_function_try_block (&compound_stmt);
0a3b29ad 16528 /* Parse the function-body. */
ccb84981 16529 ctor_initializer_p
0a3b29ad 16530 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16531 /* We're done with the `try' part. */
16532 finish_function_try_block (try_block);
16533 /* Parse the handlers. */
16534 cp_parser_handler_seq (parser);
16535 /* We're done with the handlers. */
78f7169a 16536 finish_function_handler_sequence (try_block, compound_stmt);
0a3b29ad 16537
16538 return ctor_initializer_p;
16539}
16540
16541/* Parse a handler-seq.
16542
16543 handler-seq:
16544 handler handler-seq [opt] */
16545
16546static void
45baea8b 16547cp_parser_handler_seq (cp_parser* parser)
0a3b29ad 16548{
16549 while (true)
16550 {
16551 cp_token *token;
16552
16553 /* Parse the handler. */
16554 cp_parser_handler (parser);
16555 /* Peek at the next token. */
16556 token = cp_lexer_peek_token (parser->lexer);
16557 /* If it's not `catch' then there are no more handlers. */
16558 if (!cp_parser_is_keyword (token, RID_CATCH))
16559 break;
16560 }
16561}
16562
16563/* Parse a handler.
16564
16565 handler:
16566 catch ( exception-declaration ) compound-statement */
16567
16568static void
45baea8b 16569cp_parser_handler (cp_parser* parser)
0a3b29ad 16570{
16571 tree handler;
16572 tree declaration;
16573
640710cf 16574 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
0a3b29ad 16575 handler = begin_handler ();
640710cf 16576 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 16577 declaration = cp_parser_exception_declaration (parser);
16578 finish_handler_parms (declaration, handler);
640710cf 16579 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
2363ef00 16580 cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 16581 finish_handler (handler);
16582}
16583
16584/* Parse an exception-declaration.
16585
16586 exception-declaration:
16587 type-specifier-seq declarator
16588 type-specifier-seq abstract-declarator
16589 type-specifier-seq
ccb84981 16590 ...
0a3b29ad 16591
16592 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16593 ellipsis variant is used. */
16594
16595static tree
45baea8b 16596cp_parser_exception_declaration (cp_parser* parser)
0a3b29ad 16597{
4b9b2871 16598 cp_decl_specifier_seq type_specifiers;
3046c0a3 16599 cp_declarator *declarator;
0a3b29ad 16600 const char *saved_message;
16601
16602 /* If it's an ellipsis, it's easy to handle. */
16603 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16604 {
16605 /* Consume the `...' token. */
16606 cp_lexer_consume_token (parser->lexer);
16607 return NULL_TREE;
16608 }
16609
16610 /* Types may not be defined in exception-declarations. */
16611 saved_message = parser->type_definition_forbidden_message;
16612 parser->type_definition_forbidden_message
16613 = "types may not be defined in exception-declarations";
16614
16615 /* Parse the type-specifier-seq. */
6f74fe3c 16616 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16617 &type_specifiers);
0a3b29ad 16618 /* If it's a `)', then there is no declarator. */
16619 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
3046c0a3 16620 declarator = NULL;
0a3b29ad 16621 else
42bbd0ec 16622 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
92b128ed 16623 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 16624 /*parenthesized_p=*/NULL,
16625 /*member_p=*/false);
0a3b29ad 16626
16627 /* Restore the saved message. */
16628 parser->type_definition_forbidden_message = saved_message;
16629
68023786 16630 if (!type_specifiers.any_specifiers_p)
16631 return error_mark_node;
3046c0a3 16632
68023786 16633 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
0a3b29ad 16634}
16635
ccb84981 16636/* Parse a throw-expression.
0a3b29ad 16637
16638 throw-expression:
755edffd 16639 throw assignment-expression [opt]
0a3b29ad 16640
16641 Returns a THROW_EXPR representing the throw-expression. */
16642
16643static tree
45baea8b 16644cp_parser_throw_expression (cp_parser* parser)
0a3b29ad 16645{
16646 tree expression;
fe338aca 16647 cp_token* token;
0a3b29ad 16648
640710cf 16649 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
fe338aca 16650 token = cp_lexer_peek_token (parser->lexer);
16651 /* Figure out whether or not there is an assignment-expression
16652 following the "throw" keyword. */
16653 if (token->type == CPP_COMMA
16654 || token->type == CPP_SEMICOLON
16655 || token->type == CPP_CLOSE_PAREN
16656 || token->type == CPP_CLOSE_SQUARE
16657 || token->type == CPP_CLOSE_BRACE
16658 || token->type == CPP_COLON)
0a3b29ad 16659 expression = NULL_TREE;
fe338aca 16660 else
640aa28c 16661 expression = cp_parser_assignment_expression (parser,
98b326fd 16662 /*cast_p=*/false, NULL);
0a3b29ad 16663
16664 return build_throw (expression);
16665}
16666
16667/* GNU Extensions */
16668
16669/* Parse an (optional) asm-specification.
16670
16671 asm-specification:
16672 asm ( string-literal )
16673
16674 If the asm-specification is present, returns a STRING_CST
16675 corresponding to the string-literal. Otherwise, returns
16676 NULL_TREE. */
16677
16678static tree
45baea8b 16679cp_parser_asm_specification_opt (cp_parser* parser)
0a3b29ad 16680{
16681 cp_token *token;
16682 tree asm_specification;
16683
16684 /* Peek at the next token. */
16685 token = cp_lexer_peek_token (parser->lexer);
ccb84981 16686 /* If the next token isn't the `asm' keyword, then there's no
0a3b29ad 16687 asm-specification. */
16688 if (!cp_parser_is_keyword (token, RID_ASM))
16689 return NULL_TREE;
16690
16691 /* Consume the `asm' token. */
16692 cp_lexer_consume_token (parser->lexer);
16693 /* Look for the `('. */
640710cf 16694 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 16695
16696 /* Look for the string-literal. */
00d26680 16697 asm_specification = cp_parser_string_literal (parser, false, false);
0a3b29ad 16698
16699 /* Look for the `)'. */
640710cf 16700 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 16701
16702 return asm_specification;
16703}
16704
ccb84981 16705/* Parse an asm-operand-list.
0a3b29ad 16706
16707 asm-operand-list:
16708 asm-operand
16709 asm-operand-list , asm-operand
ccb84981 16710
0a3b29ad 16711 asm-operand:
ccb84981 16712 string-literal ( expression )
0a3b29ad 16713 [ string-literal ] string-literal ( expression )
16714
16715 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16716 each node is the expression. The TREE_PURPOSE is itself a
16717 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16718 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
a40e70de 16719 is a STRING_CST for the string literal before the parenthesis. Returns
16720 ERROR_MARK_NODE if any of the operands are invalid. */
0a3b29ad 16721
16722static tree
45baea8b 16723cp_parser_asm_operand_list (cp_parser* parser)
0a3b29ad 16724{
16725 tree asm_operands = NULL_TREE;
a40e70de 16726 bool invalid_operands = false;
0a3b29ad 16727
16728 while (true)
16729 {
16730 tree string_literal;
16731 tree expression;
16732 tree name;
ccb84981 16733
ccb84981 16734 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0a3b29ad 16735 {
16736 /* Consume the `[' token. */
16737 cp_lexer_consume_token (parser->lexer);
16738 /* Read the operand name. */
16739 name = cp_parser_identifier (parser);
ccb84981 16740 if (name != error_mark_node)
0a3b29ad 16741 name = build_string (IDENTIFIER_LENGTH (name),
16742 IDENTIFIER_POINTER (name));
16743 /* Look for the closing `]'. */
640710cf 16744 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
0a3b29ad 16745 }
16746 else
16747 name = NULL_TREE;
16748 /* Look for the string-literal. */
00d26680 16749 string_literal = cp_parser_string_literal (parser, false, false);
16750
0a3b29ad 16751 /* Look for the `('. */
640710cf 16752 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 16753 /* Parse the expression. */
98b326fd 16754 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 16755 /* Look for the `)'. */
640710cf 16756 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
00d26680 16757
a40e70de 16758 if (name == error_mark_node
16759 || string_literal == error_mark_node
16760 || expression == error_mark_node)
16761 invalid_operands = true;
16762
0a3b29ad 16763 /* Add this operand to the list. */
16764 asm_operands = tree_cons (build_tree_list (name, string_literal),
ccb84981 16765 expression,
0a3b29ad 16766 asm_operands);
ccb84981 16767 /* If the next token is not a `,', there are no more
0a3b29ad 16768 operands. */
16769 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16770 break;
16771 /* Consume the `,'. */
16772 cp_lexer_consume_token (parser->lexer);
16773 }
16774
a40e70de 16775 return invalid_operands ? error_mark_node : nreverse (asm_operands);
0a3b29ad 16776}
16777
ccb84981 16778/* Parse an asm-clobber-list.
0a3b29ad 16779
16780 asm-clobber-list:
16781 string-literal
ccb84981 16782 asm-clobber-list , string-literal
0a3b29ad 16783
16784 Returns a TREE_LIST, indicating the clobbers in the order that they
16785 appeared. The TREE_VALUE of each node is a STRING_CST. */
16786
16787static tree
45baea8b 16788cp_parser_asm_clobber_list (cp_parser* parser)
0a3b29ad 16789{
16790 tree clobbers = NULL_TREE;
16791
16792 while (true)
16793 {
0a3b29ad 16794 tree string_literal;
16795
16796 /* Look for the string literal. */
00d26680 16797 string_literal = cp_parser_string_literal (parser, false, false);
0a3b29ad 16798 /* Add it to the list. */
16799 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
ccb84981 16800 /* If the next token is not a `,', then the list is
0a3b29ad 16801 complete. */
16802 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16803 break;
16804 /* Consume the `,' token. */
16805 cp_lexer_consume_token (parser->lexer);
16806 }
16807
16808 return clobbers;
16809}
16810
16811/* Parse an (optional) series of attributes.
16812
16813 attributes:
16814 attributes attribute
16815
16816 attribute:
ccb84981 16817 __attribute__ (( attribute-list [opt] ))
0a3b29ad 16818
16819 The return value is as for cp_parser_attribute_list. */
ccb84981 16820
0a3b29ad 16821static tree
45baea8b 16822cp_parser_attributes_opt (cp_parser* parser)
0a3b29ad 16823{
16824 tree attributes = NULL_TREE;
16825
16826 while (true)
16827 {
16828 cp_token *token;
16829 tree attribute_list;
16830
16831 /* Peek at the next token. */
16832 token = cp_lexer_peek_token (parser->lexer);
16833 /* If it's not `__attribute__', then we're done. */
16834 if (token->keyword != RID_ATTRIBUTE)
16835 break;
16836
16837 /* Consume the `__attribute__' keyword. */
16838 cp_lexer_consume_token (parser->lexer);
16839 /* Look for the two `(' tokens. */
640710cf 16840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16841 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
0a3b29ad 16842
16843 /* Peek at the next token. */
16844 token = cp_lexer_peek_token (parser->lexer);
16845 if (token->type != CPP_CLOSE_PAREN)
16846 /* Parse the attribute-list. */
16847 attribute_list = cp_parser_attribute_list (parser);
16848 else
16849 /* If the next token is a `)', then there is no attribute
16850 list. */
16851 attribute_list = NULL;
16852
16853 /* Look for the two `)' tokens. */
640710cf 16854 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16855 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 16856
16857 /* Add these new attributes to the list. */
16858 attributes = chainon (attributes, attribute_list);
16859 }
16860
16861 return attributes;
16862}
16863
ccb84981 16864/* Parse an attribute-list.
0a3b29ad 16865
ccb84981 16866 attribute-list:
16867 attribute
0a3b29ad 16868 attribute-list , attribute
16869
16870 attribute:
ccb84981 16871 identifier
0a3b29ad 16872 identifier ( identifier )
16873 identifier ( identifier , expression-list )
ccb84981 16874 identifier ( expression-list )
0a3b29ad 16875
f0d4a607 16876 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16877 to an attribute. The TREE_PURPOSE of each node is the identifier
16878 indicating which attribute is in use. The TREE_VALUE represents
16879 the arguments, if any. */
0a3b29ad 16880
16881static tree
45baea8b 16882cp_parser_attribute_list (cp_parser* parser)
0a3b29ad 16883{
16884 tree attribute_list = NULL_TREE;
00d26680 16885 bool save_translate_strings_p = parser->translate_strings_p;
0a3b29ad 16886
00d26680 16887 parser->translate_strings_p = false;
0a3b29ad 16888 while (true)
16889 {
16890 cp_token *token;
16891 tree identifier;
16892 tree attribute;
16893
16894 /* Look for the identifier. We also allow keywords here; for
16895 example `__attribute__ ((const))' is legal. */
16896 token = cp_lexer_peek_token (parser->lexer);
f0d4a607 16897 if (token->type == CPP_NAME
16898 || token->type == CPP_KEYWORD)
16899 {
ce601f92 16900 tree arguments = NULL_TREE;
16901
f0d4a607 16902 /* Consume the token. */
16903 token = cp_lexer_consume_token (parser->lexer);
ccb84981 16904
f0d4a607 16905 /* Save away the identifier that indicates which attribute
9031d10b 16906 this is. */
d3ca4ed0 16907 identifier = (token->type == CPP_KEYWORD)
16908 /* For keywords, use the canonical spelling, not the
16909 parsed identifier. */
16910 ? ridpointers[(int) token->keyword]
16911 : token->u.value;
16912
f0d4a607 16913 attribute = build_tree_list (identifier, NULL_TREE);
0a3b29ad 16914
f0d4a607 16915 /* Peek at the next token. */
16916 token = cp_lexer_peek_token (parser->lexer);
16917 /* If it's an `(', then parse the attribute arguments. */
16918 if (token->type == CPP_OPEN_PAREN)
16919 {
f352a3fb 16920 VEC(tree,gc) *vec;
16921 vec = cp_parser_parenthesized_expression_list
16922 (parser, true, /*cast_p=*/false,
16923 /*allow_expansion_p=*/false,
16924 /*non_constant_p=*/NULL);
16925 if (vec == NULL)
16926 arguments = error_mark_node;
16927 else
16928 {
16929 arguments = build_tree_list_vec (vec);
16930 release_tree_vector (vec);
16931 }
ce601f92 16932 /* Save the arguments away. */
f0d4a607 16933 TREE_VALUE (attribute) = arguments;
16934 }
0a3b29ad 16935
ce601f92 16936 if (arguments != error_mark_node)
16937 {
16938 /* Add this attribute to the list. */
16939 TREE_CHAIN (attribute) = attribute_list;
16940 attribute_list = attribute;
16941 }
0a3b29ad 16942
f0d4a607 16943 token = cp_lexer_peek_token (parser->lexer);
16944 }
16945 /* Now, look for more attributes. If the next token isn't a
16946 `,', we're done. */
0a3b29ad 16947 if (token->type != CPP_COMMA)
16948 break;
16949
63eff20d 16950 /* Consume the comma and keep going. */
0a3b29ad 16951 cp_lexer_consume_token (parser->lexer);
16952 }
00d26680 16953 parser->translate_strings_p = save_translate_strings_p;
0a3b29ad 16954
16955 /* We built up the list in reverse order. */
16956 return nreverse (attribute_list);
16957}
16958
16959/* Parse an optional `__extension__' keyword. Returns TRUE if it is
16960 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16961 current value of the PEDANTIC flag, regardless of whether or not
16962 the `__extension__' keyword is present. The caller is responsible
16963 for restoring the value of the PEDANTIC flag. */
16964
16965static bool
45baea8b 16966cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
0a3b29ad 16967{
16968 /* Save the old value of the PEDANTIC flag. */
16969 *saved_pedantic = pedantic;
16970
16971 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16972 {
16973 /* Consume the `__extension__' token. */
16974 cp_lexer_consume_token (parser->lexer);
16975 /* We're not being pedantic while the `__extension__' keyword is
16976 in effect. */
16977 pedantic = 0;
16978
16979 return true;
16980 }
16981
16982 return false;
16983}
16984
16985/* Parse a label declaration.
16986
16987 label-declaration:
16988 __label__ label-declarator-seq ;
16989
16990 label-declarator-seq:
16991 identifier , label-declarator-seq
16992 identifier */
16993
16994static void
45baea8b 16995cp_parser_label_declaration (cp_parser* parser)
0a3b29ad 16996{
16997 /* Look for the `__label__' keyword. */
640710cf 16998 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
0a3b29ad 16999
17000 while (true)
17001 {
17002 tree identifier;
17003
17004 /* Look for an identifier. */
17005 identifier = cp_parser_identifier (parser);
f7d1c2ea 17006 /* If we failed, stop. */
17007 if (identifier == error_mark_node)
17008 break;
17009 /* Declare it as a label. */
0a3b29ad 17010 finish_label_decl (identifier);
17011 /* If the next token is a `;', stop. */
17012 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17013 break;
17014 /* Look for the `,' separating the label declarations. */
640710cf 17015 cp_parser_require (parser, CPP_COMMA, "%<,%>");
0a3b29ad 17016 }
17017
17018 /* Look for the final `;'. */
640710cf 17019 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
0a3b29ad 17020}
17021
17022/* Support Functions */
17023
17024/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17025 NAME should have one of the representations used for an
17026 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17027 is returned. If PARSER->SCOPE is a dependent type, then a
17028 SCOPE_REF is returned.
17029
17030 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17031 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17032 was formed. Abstractly, such entities should not be passed to this
17033 function, because they do not need to be looked up, but it is
17034 simpler to check for this special case here, rather than at the
17035 call-sites.
17036
17037 In cases not explicitly covered above, this function returns a
17038 DECL, OVERLOAD, or baselink representing the result of the lookup.
17039 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17040 is returned.
17041
0be5f5cc 17042 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
e2ae55f2 17043 (e.g., "struct") that was used. In that case bindings that do not
17044 refer to types are ignored.
0a3b29ad 17045
c3b9e457 17046 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17047 ignored.
17048
6fc758aa 17049 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17050 are ignored.
17051
0a3b29ad 17052 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
9031d10b 17053 types.
2cdbcd51 17054
b62240d5 17055 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
149fdb98 17056 TREE_LIST of candidates if name-lookup results in an ambiguity, and
074ab442 17057 NULL_TREE otherwise. */
0a3b29ad 17058
17059static tree
ccb84981 17060cp_parser_lookup_name (cp_parser *parser, tree name,
e2ae55f2 17061 enum tag_types tag_type,
074ab442 17062 bool is_template,
fbb01da7 17063 bool is_namespace,
2cdbcd51 17064 bool check_dependency,
ad9ae192 17065 tree *ambiguous_decls,
17066 location_t name_location)
0a3b29ad 17067{
0e8ce9be 17068 int flags = 0;
0a3b29ad 17069 tree decl;
17070 tree object_type = parser->context->object_type;
17071
0e8ce9be 17072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17073 flags |= LOOKUP_COMPLAIN;
17074
2cdbcd51 17075 /* Assume that the lookup will be unambiguous. */
b62240d5 17076 if (ambiguous_decls)
17077 *ambiguous_decls = NULL_TREE;
2cdbcd51 17078
0a3b29ad 17079 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17080 no longer valid. Note that if we are parsing tentatively, and
17081 the parse fails, OBJECT_TYPE will be automatically restored. */
17082 parser->context->object_type = NULL_TREE;
17083
17084 if (name == error_mark_node)
17085 return error_mark_node;
17086
17087 /* A template-id has already been resolved; there is no lookup to
17088 do. */
17089 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17090 return name;
17091 if (BASELINK_P (name))
17092 {
b4df430b 17093 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17094 == TEMPLATE_ID_EXPR);
0a3b29ad 17095 return name;
17096 }
17097
17098 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17099 it should already have been checked to make sure that the name
17100 used matches the type being destroyed. */
17101 if (TREE_CODE (name) == BIT_NOT_EXPR)
17102 {
17103 tree type;
17104
17105 /* Figure out to which type this destructor applies. */
17106 if (parser->scope)
17107 type = parser->scope;
17108 else if (object_type)
17109 type = object_type;
17110 else
17111 type = current_class_type;
17112 /* If that's not a class type, there is no destructor. */
17113 if (!type || !CLASS_TYPE_P (type))
17114 return error_mark_node;
ed36f1cf 17115 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17116 lazily_declare_fn (sfk_destructor, type);
2b40dfce 17117 if (!CLASSTYPE_DESTRUCTORS (type))
17118 return error_mark_node;
0a3b29ad 17119 /* If it was a class type, return the destructor. */
17120 return CLASSTYPE_DESTRUCTORS (type);
17121 }
17122
17123 /* By this point, the NAME should be an ordinary identifier. If
17124 the id-expression was a qualified name, the qualifying scope is
17125 stored in PARSER->SCOPE at this point. */
b4df430b 17126 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
ccb84981 17127
0a3b29ad 17128 /* Perform the lookup. */
17129 if (parser->scope)
ccb84981 17130 {
7e9a6a16 17131 bool dependent_p;
0a3b29ad 17132
17133 if (parser->scope == error_mark_node)
17134 return error_mark_node;
17135
17136 /* If the SCOPE is dependent, the lookup must be deferred until
17137 the template is instantiated -- unless we are explicitly
17138 looking up names in uninstantiated templates. Even then, we
17139 cannot look up the name if the scope is not a class type; it
17140 might, for example, be a template type parameter. */
7e9a6a16 17141 dependent_p = (TYPE_P (parser->scope)
05f701e2 17142 && dependent_scope_p (parser->scope));
0a3b29ad 17143 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
05f701e2 17144 && dependent_p)
17145 /* Defer lookup. */
17146 decl = error_mark_node;
0a3b29ad 17147 else
17148 {
7f602bca 17149 tree pushed_scope = NULL_TREE;
1cbda81f 17150
0a3b29ad 17151 /* If PARSER->SCOPE is a dependent type, then it must be a
17152 class type, and we must not be checking dependencies;
17153 otherwise, we would have processed this lookup above. So
17154 that PARSER->SCOPE is not considered a dependent base by
17155 lookup_member, we must enter the scope here. */
7e9a6a16 17156 if (dependent_p)
7f602bca 17157 pushed_scope = push_scope (parser->scope);
dfea972c 17158 /* If the PARSER->SCOPE is a template specialization, it
0a3b29ad 17159 may be instantiated during name lookup. In that case,
17160 errors may be issued. Even if we rollback the current
17161 tentative parse, those errors are valid. */
9031d10b 17162 decl = lookup_qualified_name (parser->scope, name,
17163 tag_type != none_type,
2be3e2ee 17164 /*complain=*/true);
93a3c02a 17165
17166 /* If we have a single function from a using decl, pull it out. */
05f701e2 17167 if (TREE_CODE (decl) == OVERLOAD
93a3c02a 17168 && !really_overloaded_fn (decl))
17169 decl = OVL_FUNCTION (decl);
17170
7f602bca 17171 if (pushed_scope)
17172 pop_scope (pushed_scope);
0a3b29ad 17173 }
05f701e2 17174
17175 /* If the scope is a dependent type and either we deferred lookup or
17176 we did lookup but didn't find the name, rememeber the name. */
17177 if (decl == error_mark_node && TYPE_P (parser->scope)
17178 && dependent_type_p (parser->scope))
17179 {
17180 if (tag_type)
17181 {
17182 tree type;
17183
17184 /* The resolution to Core Issue 180 says that `struct
17185 A::B' should be considered a type-name, even if `A'
17186 is dependent. */
17187 type = make_typename_type (parser->scope, name, tag_type,
17188 /*complain=*/tf_error);
17189 decl = TYPE_NAME (type);
17190 }
17191 else if (is_template
17192 && (cp_parser_next_token_ends_template_argument_p (parser)
17193 || cp_lexer_next_token_is (parser->lexer,
17194 CPP_CLOSE_PAREN)))
17195 decl = make_unbound_class_template (parser->scope,
17196 name, NULL_TREE,
17197 /*complain=*/tf_error);
17198 else
17199 decl = build_qualified_name (/*type=*/NULL_TREE,
17200 parser->scope, name,
17201 is_template);
17202 }
0a3b29ad 17203 parser->qualifying_scope = parser->scope;
17204 parser->object_scope = NULL_TREE;
17205 }
17206 else if (object_type)
17207 {
17208 tree object_decl = NULL_TREE;
17209 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17210 OBJECT_TYPE is not a class. */
17211 if (CLASS_TYPE_P (object_type))
17212 /* If the OBJECT_TYPE is a template specialization, it may
17213 be instantiated during name lookup. In that case, errors
17214 may be issued. Even if we rollback the current tentative
17215 parse, those errors are valid. */
17216 object_decl = lookup_member (object_type,
17217 name,
9031d10b 17218 /*protect=*/0,
e2ae55f2 17219 tag_type != none_type);
0a3b29ad 17220 /* Look it up in the enclosing context, too. */
9031d10b 17221 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 17222 /*nonclass=*/0,
0e8ce9be 17223 /*block_p=*/true, is_namespace, flags);
0a3b29ad 17224 parser->object_scope = object_type;
17225 parser->qualifying_scope = NULL_TREE;
17226 if (object_decl)
17227 decl = object_decl;
17228 }
17229 else
17230 {
9031d10b 17231 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 17232 /*nonclass=*/0,
0e8ce9be 17233 /*block_p=*/true, is_namespace, flags);
0a3b29ad 17234 parser->qualifying_scope = NULL_TREE;
17235 parser->object_scope = NULL_TREE;
17236 }
17237
17238 /* If the lookup failed, let our caller know. */
3f3fa556 17239 if (!decl || decl == error_mark_node)
0a3b29ad 17240 return error_mark_node;
17241
17242 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17243 if (TREE_CODE (decl) == TREE_LIST)
17244 {
b62240d5 17245 if (ambiguous_decls)
17246 *ambiguous_decls = decl;
0a3b29ad 17247 /* The error message we have to print is too complicated for
17248 cp_parser_error, so we incorporate its actions directly. */
2c593bd0 17249 if (!cp_parser_simulate_error (parser))
0a3b29ad 17250 {
ad9ae192 17251 error ("%Hreference to %qD is ambiguous",
17252 &name_location, name);
0a3b29ad 17253 print_candidates (decl);
17254 }
17255 return error_mark_node;
17256 }
17257
b4df430b 17258 gcc_assert (DECL_P (decl)
17259 || TREE_CODE (decl) == OVERLOAD
17260 || TREE_CODE (decl) == SCOPE_REF
17261 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17262 || BASELINK_P (decl));
0a3b29ad 17263
17264 /* If we have resolved the name of a member declaration, check to
17265 see if the declaration is accessible. When the name resolves to
755edffd 17266 set of overloaded functions, accessibility is checked when
ccb84981 17267 overload resolution is done.
0a3b29ad 17268
17269 During an explicit instantiation, access is not checked at all,
17270 as per [temp.explicit]. */
4f62c42e 17271 if (DECL_P (decl))
ef4534a3 17272 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
0a3b29ad 17273
17274 return decl;
17275}
17276
17277/* Like cp_parser_lookup_name, but for use in the typical case where
c3b9e457 17278 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17279 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
0a3b29ad 17280
17281static tree
ad9ae192 17282cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
0a3b29ad 17283{
ccb84981 17284 return cp_parser_lookup_name (parser, name,
e2ae55f2 17285 none_type,
c3b9e457 17286 /*is_template=*/false,
6fc758aa 17287 /*is_namespace=*/false,
2cdbcd51 17288 /*check_dependency=*/true,
ad9ae192 17289 /*ambiguous_decls=*/NULL,
17290 location);
0a3b29ad 17291}
17292
0a3b29ad 17293/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17294 the current context, return the TYPE_DECL. If TAG_NAME_P is
17295 true, the DECL indicates the class being defined in a class-head,
17296 or declared in an elaborated-type-specifier.
17297
17298 Otherwise, return DECL. */
17299
17300static tree
17301cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17302{
a5e50b31 17303 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17304 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
0a3b29ad 17305
ccb84981 17306 struct A {
653e5405 17307 template <typename T> struct B;
0a3b29ad 17308 };
17309
ccb84981 17310 template <typename T> struct A::B {};
17311
e4bc96e2 17312 Similarly, in an elaborated-type-specifier:
0a3b29ad 17313
17314 namespace N { struct X{}; }
17315
17316 struct A {
653e5405 17317 template <typename T> friend struct N::X;
0a3b29ad 17318 };
17319
a5e50b31 17320 However, if the DECL refers to a class type, and we are in
17321 the scope of the class, then the name lookup automatically
17322 finds the TYPE_DECL created by build_self_reference rather
17323 than a TEMPLATE_DECL. For example, in:
17324
17325 template <class T> struct S {
653e5405 17326 S s;
a5e50b31 17327 };
17328
17329 there is no need to handle such case. */
17330
17331 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
0a3b29ad 17332 return DECL_TEMPLATE_RESULT (decl);
17333
17334 return decl;
17335}
17336
17337/* If too many, or too few, template-parameter lists apply to the
17338 declarator, issue an error message. Returns TRUE if all went well,
17339 and FALSE otherwise. */
17340
17341static bool
ccb84981 17342cp_parser_check_declarator_template_parameters (cp_parser* parser,
ad9ae192 17343 cp_declarator *declarator,
17344 location_t declarator_location)
0a3b29ad 17345{
17346 unsigned num_templates;
17347
17348 /* We haven't seen any classes that involve template parameters yet. */
17349 num_templates = 0;
17350
3046c0a3 17351 switch (declarator->kind)
0a3b29ad 17352 {
3046c0a3 17353 case cdk_id:
2ded3667 17354 if (declarator->u.id.qualifying_scope)
3046c0a3 17355 {
17356 tree scope;
17357 tree member;
0a3b29ad 17358
2ded3667 17359 scope = declarator->u.id.qualifying_scope;
17360 member = declarator->u.id.unqualified_name;
0a3b29ad 17361
3046c0a3 17362 while (scope && CLASS_TYPE_P (scope))
17363 {
17364 /* You're supposed to have one `template <...>'
17365 for every template class, but you don't need one
17366 for a full specialization. For example:
17367
17368 template <class T> struct S{};
17369 template <> struct S<int> { void f(); };
17370 void S<int>::f () {}
17371
17372 is correct; there shouldn't be a `template <>' for
17373 the definition of `S<int>::f'. */
04ef83b7 17374 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17375 /* If SCOPE does not have template information of any
17376 kind, then it is not a template, nor is it nested
17377 within a template. */
17378 break;
17379 if (explicit_class_specialization_p (scope))
17380 break;
17381 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
3046c0a3 17382 ++num_templates;
17383
17384 scope = TYPE_CONTEXT (scope);
17385 }
17386 }
9031d10b 17387 else if (TREE_CODE (declarator->u.id.unqualified_name)
2ded3667 17388 == TEMPLATE_ID_EXPR)
17389 /* If the DECLARATOR has the form `X<y>' then it uses one
17390 additional level of template parameters. */
0a3b29ad 17391 ++num_templates;
17392
7b07a15e 17393 return cp_parser_check_template_parameters
17394 (parser, num_templates, declarator_location, declarator);
17395
3046c0a3 17396
17397 case cdk_function:
17398 case cdk_array:
17399 case cdk_pointer:
17400 case cdk_reference:
17401 case cdk_ptrmem:
207355ad 17402 return (cp_parser_check_declarator_template_parameters
ad9ae192 17403 (parser, declarator->declarator, declarator_location));
3046c0a3 17404
17405 case cdk_error:
17406 return true;
17407
17408 default:
2e3e31d2 17409 gcc_unreachable ();
0a3b29ad 17410 }
2e3e31d2 17411 return false;
0a3b29ad 17412}
17413
17414/* NUM_TEMPLATES were used in the current declaration. If that is
17415 invalid, return FALSE and issue an error messages. Otherwise,
7b07a15e 17416 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17417 declarator and we can print more accurate diagnostics. */
0a3b29ad 17418
17419static bool
45baea8b 17420cp_parser_check_template_parameters (cp_parser* parser,
ad9ae192 17421 unsigned num_templates,
7b07a15e 17422 location_t location,
17423 cp_declarator *declarator)
0a3b29ad 17424{
7b07a15e 17425 /* If there are the same number of template classes and parameter
17426 lists, that's OK. */
17427 if (parser->num_template_parameter_lists == num_templates)
17428 return true;
17429 /* If there are more, but only one more, then we are referring to a
17430 member template. That's OK too. */
17431 if (parser->num_template_parameter_lists == num_templates + 1)
17432 return true;
0a3b29ad 17433 /* If there are more template classes than parameter lists, we have
17434 something like:
ccb84981 17435
0a3b29ad 17436 template <class T> void S<T>::R<T>::f (); */
17437 if (parser->num_template_parameter_lists < num_templates)
17438 {
7b07a15e 17439 if (declarator)
17440 error_at (location, "specializing member %<%T::%E%> "
17441 "requires %<template<>%> syntax",
17442 declarator->u.id.qualifying_scope,
17443 declarator->u.id.unqualified_name);
17444 else
17445 error_at (location, "too few template-parameter-lists");
0a3b29ad 17446 return false;
17447 }
0a3b29ad 17448 /* Otherwise, there are too many template parameter lists. We have
17449 something like:
17450
17451 template <class T> template <class U> void S::f(); */
ad9ae192 17452 error ("%Htoo many template-parameter-lists", &location);
0a3b29ad 17453 return false;
17454}
17455
0a3b29ad 17456/* Parse an optional `::' token indicating that the following name is
17457 from the global namespace. If so, PARSER->SCOPE is set to the
17458 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17459 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17460 Returns the new value of PARSER->SCOPE, if the `::' token is
17461 present, and NULL_TREE otherwise. */
17462
17463static tree
130bb1d4 17464cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
0a3b29ad 17465{
17466 cp_token *token;
17467
17468 /* Peek at the next token. */
17469 token = cp_lexer_peek_token (parser->lexer);
17470 /* If we're looking at a `::' token then we're starting from the
17471 global namespace, not our current location. */
17472 if (token->type == CPP_SCOPE)
17473 {
17474 /* Consume the `::' token. */
17475 cp_lexer_consume_token (parser->lexer);
17476 /* Set the SCOPE so that we know where to start the lookup. */
17477 parser->scope = global_namespace;
17478 parser->qualifying_scope = global_namespace;
17479 parser->object_scope = NULL_TREE;
17480
17481 return parser->scope;
17482 }
130bb1d4 17483 else if (!current_scope_valid_p)
0a3b29ad 17484 {
17485 parser->scope = NULL_TREE;
17486 parser->qualifying_scope = NULL_TREE;
130bb1d4 17487 parser->object_scope = NULL_TREE;
0a3b29ad 17488 }
17489
17490 return NULL_TREE;
17491}
17492
17493/* Returns TRUE if the upcoming token sequence is the start of a
17494 constructor declarator. If FRIEND_P is true, the declarator is
17495 preceded by the `friend' specifier. */
17496
17497static bool
17498cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17499{
17500 bool constructor_p;
17501 tree type_decl = NULL_TREE;
17502 bool nested_name_p;
b3c48b5d 17503 cp_token *next_token;
17504
17505 /* The common case is that this is not a constructor declarator, so
954ad420 17506 try to avoid doing lots of work if at all possible. It's not
17507 valid declare a constructor at function scope. */
0aeb1cc5 17508 if (parser->in_function_body)
954ad420 17509 return false;
17510 /* And only certain tokens can begin a constructor declarator. */
b3c48b5d 17511 next_token = cp_lexer_peek_token (parser->lexer);
17512 if (next_token->type != CPP_NAME
17513 && next_token->type != CPP_SCOPE
17514 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17515 && next_token->type != CPP_TEMPLATE_ID)
17516 return false;
0a3b29ad 17517
17518 /* Parse tentatively; we are going to roll back all of the tokens
17519 consumed here. */
17520 cp_parser_parse_tentatively (parser);
17521 /* Assume that we are looking at a constructor declarator. */
17522 constructor_p = true;
4f62c42e 17523
0a3b29ad 17524 /* Look for the optional `::' operator. */
17525 cp_parser_global_scope_opt (parser,
130bb1d4 17526 /*current_scope_valid_p=*/false);
0a3b29ad 17527 /* Look for the nested-name-specifier. */
ccb84981 17528 nested_name_p
0a3b29ad 17529 = (cp_parser_nested_name_specifier_opt (parser,
17530 /*typename_keyword_p=*/false,
17531 /*check_dependency_p=*/false,
3d0f901b 17532 /*type_p=*/false,
17533 /*is_declaration=*/false)
0a3b29ad 17534 != NULL_TREE);
17535 /* Outside of a class-specifier, there must be a
17536 nested-name-specifier. */
ccb84981 17537 if (!nested_name_p &&
0a3b29ad 17538 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17539 || friend_p))
17540 constructor_p = false;
17541 /* If we still think that this might be a constructor-declarator,
17542 look for a class-name. */
17543 if (constructor_p)
17544 {
17545 /* If we have:
17546
954ad420 17547 template <typename T> struct S { S(); };
0a3b29ad 17548 template <typename T> S<T>::S ();
17549
17550 we must recognize that the nested `S' names a class.
17551 Similarly, for:
17552
17553 template <typename T> S<T>::S<T> ();
17554
17555 we must recognize that the nested `S' names a template. */
17556 type_decl = cp_parser_class_name (parser,
17557 /*typename_keyword_p=*/false,
17558 /*template_keyword_p=*/false,
e2ae55f2 17559 none_type,
0a3b29ad 17560 /*check_dependency_p=*/false,
3d0f901b 17561 /*class_head_p=*/false,
17562 /*is_declaration=*/false);
0a3b29ad 17563 /* If there was no class-name, then this is not a constructor. */
17564 constructor_p = !cp_parser_error_occurred (parser);
17565 }
4f62c42e 17566
0a3b29ad 17567 /* If we're still considering a constructor, we have to see a `(',
17568 to begin the parameter-declaration-clause, followed by either a
17569 `)', an `...', or a decl-specifier. We need to check for a
17570 type-specifier to avoid being fooled into thinking that:
17571
17572 S::S (f) (int);
17573
17574 is a constructor. (It is actually a function named `f' that
17575 takes one parameter (of type `int') and returns a value of type
17576 `S::S'. */
ccb84981 17577 if (constructor_p
640710cf 17578 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
0a3b29ad 17579 {
17580 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17581 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
e67a67ea 17582 /* A parameter declaration begins with a decl-specifier,
17583 which is either the "attribute" keyword, a storage class
17584 specifier, or (usually) a type-specifier. */
9a7c4b43 17585 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
0a3b29ad 17586 {
8c1f65e6 17587 tree type;
7f602bca 17588 tree pushed_scope = NULL_TREE;
bb91f165 17589 unsigned saved_num_template_parameter_lists;
8c1f65e6 17590
17591 /* Names appearing in the type-specifier should be looked up
17592 in the scope of the class. */
17593 if (current_class_type)
17594 type = NULL_TREE;
0a3b29ad 17595 else
17596 {
8c1f65e6 17597 type = TREE_TYPE (type_decl);
17598 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 17599 {
ccb84981 17600 type = resolve_typename_type (type,
5f6526e1 17601 /*only_current_p=*/false);
8826a863 17602 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 17603 {
17604 cp_parser_abort_tentative_parse (parser);
17605 return false;
17606 }
17607 }
7f602bca 17608 pushed_scope = push_scope (type);
0a3b29ad 17609 }
bb91f165 17610
17611 /* Inside the constructor parameter list, surrounding
17612 template-parameter-lists do not apply. */
17613 saved_num_template_parameter_lists
17614 = parser->num_template_parameter_lists;
17615 parser->num_template_parameter_lists = 0;
17616
8c1f65e6 17617 /* Look for the type-specifier. */
17618 cp_parser_type_specifier (parser,
17619 CP_PARSER_FLAGS_NONE,
4b9b2871 17620 /*decl_specs=*/NULL,
8c1f65e6 17621 /*is_declarator=*/true,
17622 /*declares_class_or_enum=*/NULL,
17623 /*is_cv_qualifier=*/NULL);
bb91f165 17624
17625 parser->num_template_parameter_lists
17626 = saved_num_template_parameter_lists;
17627
8c1f65e6 17628 /* Leave the scope of the class. */
7f602bca 17629 if (pushed_scope)
17630 pop_scope (pushed_scope);
8c1f65e6 17631
17632 constructor_p = !cp_parser_error_occurred (parser);
0a3b29ad 17633 }
17634 }
17635 else
17636 constructor_p = false;
17637 /* We did not really want to consume any tokens. */
17638 cp_parser_abort_tentative_parse (parser);
17639
17640 return constructor_p;
17641}
17642
17643/* Parse the definition of the function given by the DECL_SPECIFIERS,
9b57b06b 17644 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
0a3b29ad 17645 they must be performed once we are in the scope of the function.
17646
17647 Returns the function defined. */
17648
17649static tree
17650cp_parser_function_definition_from_specifiers_and_declarator
45baea8b 17651 (cp_parser* parser,
4b9b2871 17652 cp_decl_specifier_seq *decl_specifiers,
45baea8b 17653 tree attributes,
3046c0a3 17654 const cp_declarator *declarator)
0a3b29ad 17655{
17656 tree fn;
17657 bool success_p;
17658
17659 /* Begin the function-definition. */
3046c0a3 17660 success_p = start_function (decl_specifiers, declarator, attributes);
17661
17662 /* The things we're about to see are not directly qualified by any
17663 template headers we've seen thus far. */
17664 reset_specialization ();
0a3b29ad 17665
17666 /* If there were names looked up in the decl-specifier-seq that we
17667 did not check, check them now. We must wait until we are in the
17668 scope of the function to perform the checks, since the function
17669 might be a friend. */
9b57b06b 17670 perform_deferred_access_checks ();
0a3b29ad 17671
17672 if (!success_p)
17673 {
3046c0a3 17674 /* Skip the entire function. */
0a3b29ad 17675 cp_parser_skip_to_end_of_block_or_statement (parser);
17676 fn = error_mark_node;
17677 }
ddb7a3b0 17678 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17679 {
17680 /* Seen already, skip it. An error message has already been output. */
17681 cp_parser_skip_to_end_of_block_or_statement (parser);
17682 fn = current_function_decl;
17683 current_function_decl = NULL_TREE;
17684 /* If this is a function from a class, pop the nested class. */
17685 if (current_class_name)
17686 pop_nested_class ();
17687 }
0a3b29ad 17688 else
17689 fn = cp_parser_function_definition_after_declarator (parser,
17690 /*inline_p=*/false);
17691
17692 return fn;
17693}
17694
17695/* Parse the part of a function-definition that follows the
17696 declarator. INLINE_P is TRUE iff this function is an inline
17697 function defined with a class-specifier.
17698
17699 Returns the function defined. */
17700
ccb84981 17701static tree
17702cp_parser_function_definition_after_declarator (cp_parser* parser,
45baea8b 17703 bool inline_p)
0a3b29ad 17704{
17705 tree fn;
17706 bool ctor_initializer_p = false;
17707 bool saved_in_unbraced_linkage_specification_p;
0aeb1cc5 17708 bool saved_in_function_body;
0a3b29ad 17709 unsigned saved_num_template_parameter_lists;
ad9ae192 17710 cp_token *token;
0a3b29ad 17711
0aeb1cc5 17712 saved_in_function_body = parser->in_function_body;
17713 parser->in_function_body = true;
0a3b29ad 17714 /* If the next token is `return', then the code may be trying to
17715 make use of the "named return value" extension that G++ used to
17716 support. */
ad9ae192 17717 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 17718 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17719 {
17720 /* Consume the `return' keyword. */
17721 cp_lexer_consume_token (parser->lexer);
17722 /* Look for the identifier that indicates what value is to be
17723 returned. */
17724 cp_parser_identifier (parser);
17725 /* Issue an error message. */
ad9ae192 17726 error ("%Hnamed return values are no longer supported",
17727 &token->location);
0a3b29ad 17728 /* Skip tokens until we reach the start of the function body. */
b75b98aa 17729 while (true)
17730 {
17731 cp_token *token = cp_lexer_peek_token (parser->lexer);
17732 if (token->type == CPP_OPEN_BRACE
17733 || token->type == CPP_EOF
17734 || token->type == CPP_PRAGMA_EOL)
17735 break;
17736 cp_lexer_consume_token (parser->lexer);
17737 }
0a3b29ad 17738 }
17739 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17740 anything declared inside `f'. */
ccb84981 17741 saved_in_unbraced_linkage_specification_p
0a3b29ad 17742 = parser->in_unbraced_linkage_specification_p;
17743 parser->in_unbraced_linkage_specification_p = false;
17744 /* Inside the function, surrounding template-parameter-lists do not
17745 apply. */
ccb84981 17746 saved_num_template_parameter_lists
17747 = parser->num_template_parameter_lists;
0a3b29ad 17748 parser->num_template_parameter_lists = 0;
17749 /* If the next token is `try', then we are looking at a
17750 function-try-block. */
17751 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17752 ctor_initializer_p = cp_parser_function_try_block (parser);
17753 /* A function-try-block includes the function-body, so we only do
17754 this next part if we're not processing a function-try-block. */
17755 else
ccb84981 17756 ctor_initializer_p
0a3b29ad 17757 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17758
17759 /* Finish the function. */
ccb84981 17760 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
0a3b29ad 17761 (inline_p ? 2 : 0));
17762 /* Generate code for it, if necessary. */
6cb758f0 17763 expand_or_defer_fn (fn);
0a3b29ad 17764 /* Restore the saved values. */
ccb84981 17765 parser->in_unbraced_linkage_specification_p
0a3b29ad 17766 = saved_in_unbraced_linkage_specification_p;
ccb84981 17767 parser->num_template_parameter_lists
0a3b29ad 17768 = saved_num_template_parameter_lists;
0aeb1cc5 17769 parser->in_function_body = saved_in_function_body;
0a3b29ad 17770
17771 return fn;
17772}
17773
17774/* Parse a template-declaration, assuming that the `export' (and
17775 `extern') keywords, if present, has already been scanned. MEMBER_P
17776 is as for cp_parser_template_declaration. */
17777
17778static void
45baea8b 17779cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
0a3b29ad 17780{
17781 tree decl = NULL_TREE;
3369eb76 17782 VEC (deferred_access_check,gc) *checks;
0a3b29ad 17783 tree parameter_list;
17784 bool friend_p = false;
9f25cdd8 17785 bool need_lang_pop;
ad9ae192 17786 cp_token *token;
0a3b29ad 17787
17788 /* Look for the `template' keyword. */
ad9ae192 17789 token = cp_lexer_peek_token (parser->lexer);
640710cf 17790 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
0a3b29ad 17791 return;
ccb84981 17792
0a3b29ad 17793 /* And the `<'. */
640710cf 17794 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
0a3b29ad 17795 return;
a34824c0 17796 if (at_class_scope_p () && current_function_decl)
17797 {
17798 /* 14.5.2.2 [temp.mem]
17799
17800 A local class shall not have member templates. */
ad9ae192 17801 error ("%Hinvalid declaration of member template in local class",
17802 &token->location);
a34824c0 17803 cp_parser_skip_to_end_of_block_or_statement (parser);
17804 return;
17805 }
9f25cdd8 17806 /* [temp]
074ab442 17807
9f25cdd8 17808 A template ... shall not have C linkage. */
17809 if (current_lang_name == lang_name_c)
17810 {
ad9ae192 17811 error ("%Htemplate with C linkage", &token->location);
9f25cdd8 17812 /* Give it C++ linkage to avoid confusing other parts of the
17813 front end. */
17814 push_lang_context (lang_name_cplusplus);
17815 need_lang_pop = true;
17816 }
17817 else
17818 need_lang_pop = false;
23010bc8 17819
17820 /* We cannot perform access checks on the template parameter
17821 declarations until we know what is being declared, just as we
17822 cannot check the decl-specifier list. */
17823 push_deferring_access_checks (dk_deferred);
17824
0a3b29ad 17825 /* If the next token is `>', then we have an invalid
17826 specialization. Rather than complain about an invalid template
17827 parameter, issue an error message here. */
17828 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17829 {
17830 cp_parser_error (parser, "invalid explicit specialization");
3398499c 17831 begin_specialization ();
0a3b29ad 17832 parameter_list = NULL_TREE;
17833 }
17834 else
7be1bc1f 17835 /* Parse the template parameters. */
17836 parameter_list = cp_parser_template_parameter_list (parser);
3398499c 17837
23010bc8 17838 /* Get the deferred access checks from the parameter list. These
17839 will be checked once we know what is being declared, as for a
17840 member template the checks must be performed in the scope of the
17841 class containing the member. */
17842 checks = get_deferred_access_checks ();
17843
0a3b29ad 17844 /* Look for the `>'. */
c42e0e2d 17845 cp_parser_skip_to_end_of_template_parameter_list (parser);
0a3b29ad 17846 /* We just processed one more parameter list. */
17847 ++parser->num_template_parameter_lists;
17848 /* If the next token is `template', there are more template
17849 parameters. */
ccb84981 17850 if (cp_lexer_next_token_is_keyword (parser->lexer,
0a3b29ad 17851 RID_TEMPLATE))
17852 cp_parser_template_declaration_after_export (parser, member_p);
17853 else
17854 {
a8ff8672 17855 /* There are no access checks when parsing a template, as we do not
653e5405 17856 know if a specialization will be a friend. */
a8ff8672 17857 push_deferring_access_checks (dk_no_check);
ad9ae192 17858 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 17859 decl = cp_parser_single_declaration (parser,
23010bc8 17860 checks,
0a3b29ad 17861 member_p,
0c032b46 17862 /*explicit_specialization_p=*/false,
0a3b29ad 17863 &friend_p);
a8ff8672 17864 pop_deferring_access_checks ();
207355ad 17865
0a3b29ad 17866 /* If this is a member template declaration, let the front
17867 end know. */
17868 if (member_p && !friend_p && decl)
7e35473e 17869 {
17870 if (TREE_CODE (decl) == TYPE_DECL)
ad9ae192 17871 cp_parser_check_access_in_redeclaration (decl, token->location);
7e35473e 17872
17873 decl = finish_member_template_decl (decl);
17874 }
0a3b29ad 17875 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
b123b79d 17876 make_friend_class (current_class_type, TREE_TYPE (decl),
17877 /*complain=*/true);
0a3b29ad 17878 }
17879 /* We are done with the current parameter list. */
17880 --parser->num_template_parameter_lists;
17881
23010bc8 17882 pop_deferring_access_checks ();
17883
0a3b29ad 17884 /* Finish up. */
17885 finish_template_decl (parameter_list);
17886
17887 /* Register member declarations. */
17888 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17889 finish_member_declaration (decl);
9f25cdd8 17890 /* For the erroneous case of a template with C linkage, we pushed an
17891 implicit C++ linkage scope; exit that scope now. */
17892 if (need_lang_pop)
17893 pop_lang_context ();
0a3b29ad 17894 /* If DECL is a function template, we must return to parse it later.
17895 (Even though there is no definition, there might be default
17896 arguments that need handling.) */
ccb84981 17897 if (member_p && decl
0a3b29ad 17898 && (TREE_CODE (decl) == FUNCTION_DECL
17899 || DECL_FUNCTION_TEMPLATE_P (decl)))
17900 TREE_VALUE (parser->unparsed_functions_queues)
ccb84981 17901 = tree_cons (NULL_TREE, decl,
0a3b29ad 17902 TREE_VALUE (parser->unparsed_functions_queues));
17903}
17904
23010bc8 17905/* Perform the deferred access checks from a template-parameter-list.
17906 CHECKS is a TREE_LIST of access checks, as returned by
17907 get_deferred_access_checks. */
17908
17909static void
3369eb76 17910cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
23010bc8 17911{
17912 ++processing_template_parmlist;
17913 perform_access_checks (checks);
17914 --processing_template_parmlist;
17915}
17916
0a3b29ad 17917/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17918 `function-definition' sequence. MEMBER_P is true, this declaration
17919 appears in a class scope.
17920
17921 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17922 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17923
17924static tree
ccb84981 17925cp_parser_single_declaration (cp_parser* parser,
3369eb76 17926 VEC (deferred_access_check,gc)* checks,
45baea8b 17927 bool member_p,
0c032b46 17928 bool explicit_specialization_p,
45baea8b 17929 bool* friend_p)
0a3b29ad 17930{
8172be22 17931 int declares_class_or_enum;
0a3b29ad 17932 tree decl = NULL_TREE;
4b9b2871 17933 cp_decl_specifier_seq decl_specifiers;
92b128ed 17934 bool function_definition_p = false;
ad9ae192 17935 cp_token *decl_spec_token_start;
0a3b29ad 17936
2a03dcc3 17937 /* This function is only used when processing a template
17938 declaration. */
17939 gcc_assert (innermost_scope_kind () == sk_template_parms
17940 || innermost_scope_kind () == sk_template_spec);
17941
0a3b29ad 17942 /* Defer access checks until we know what is being declared. */
4f62c42e 17943 push_deferring_access_checks (dk_deferred);
9b57b06b 17944
0a3b29ad 17945 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17946 alternative. */
ad9ae192 17947 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 17948 cp_parser_decl_specifier_seq (parser,
17949 CP_PARSER_FLAGS_OPTIONAL,
17950 &decl_specifiers,
17951 &declares_class_or_enum);
92b128ed 17952 if (friend_p)
4b9b2871 17953 *friend_p = cp_parser_friend_p (&decl_specifiers);
2a03dcc3 17954
17955 /* There are no template typedefs. */
17956 if (decl_specifiers.specs[(int) ds_typedef])
17957 {
ad9ae192 17958 error ("%Htemplate declaration of %qs",
17959 &decl_spec_token_start->location, "typedef");
2a03dcc3 17960 decl = error_mark_node;
17961 }
17962
0a3b29ad 17963 /* Gather up the access checks that occurred the
17964 decl-specifier-seq. */
9b57b06b 17965 stop_deferring_access_checks ();
17966
0a3b29ad 17967 /* Check for the declaration of a template class. */
17968 if (declares_class_or_enum)
17969 {
17970 if (cp_parser_declares_only_class_p (parser))
17971 {
4b9b2871 17972 decl = shadow_tag (&decl_specifiers);
f95fba26 17973
17974 /* In this case:
17975
17976 struct C {
17977 friend template <typename T> struct A<T>::B;
17978 };
17979
17980 A<T>::B will be represented by a TYPENAME_TYPE, and
17981 therefore not recognized by shadow_tag. */
17982 if (friend_p && *friend_p
17983 && !decl
17984 && decl_specifiers.type
17985 && TYPE_P (decl_specifiers.type))
17986 decl = decl_specifiers.type;
17987
4b9b2871 17988 if (decl && decl != error_mark_node)
0a3b29ad 17989 decl = TYPE_NAME (decl);
17990 else
17991 decl = error_mark_node;
23010bc8 17992
17993 /* Perform access checks for template parameters. */
17994 cp_parser_perform_template_parameter_access_checks (checks);
0a3b29ad 17995 }
17996 }
0a3b29ad 17997 /* If it's not a template class, try for a template function. If
17998 the next token is a `;', then this declaration does not declare
17999 anything. But, if there were errors in the decl-specifiers, then
18000 the error might well have come from an attempted class-specifier.
18001 In that case, there's no need to warn about a missing declarator. */
18002 if (!decl
18003 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
4b9b2871 18004 || decl_specifiers.type != error_mark_node))
0c032b46 18005 {
18006 decl = cp_parser_init_declarator (parser,
18007 &decl_specifiers,
18008 checks,
18009 /*function_definition_allowed_p=*/true,
18010 member_p,
18011 declares_class_or_enum,
18012 &function_definition_p);
18013
18014 /* 7.1.1-1 [dcl.stc]
18015
18016 A storage-class-specifier shall not be specified in an explicit
18017 specialization... */
18018 if (decl
18019 && explicit_specialization_p
18020 && decl_specifiers.storage_class != sc_none)
18021 {
ad9ae192 18022 error ("%Hexplicit template specialization cannot have a storage class",
18023 &decl_spec_token_start->location);
0c032b46 18024 decl = error_mark_node;
18025 }
18026 }
9b57b06b 18027
18028 pop_deferring_access_checks ();
18029
0a3b29ad 18030 /* Clear any current qualification; whatever comes next is the start
18031 of something new. */
18032 parser->scope = NULL_TREE;
18033 parser->qualifying_scope = NULL_TREE;
18034 parser->object_scope = NULL_TREE;
18035 /* Look for a trailing `;' after the declaration. */
92b128ed 18036 if (!function_definition_p
2a03dcc3 18037 && (decl == error_mark_node
640710cf 18038 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
0a3b29ad 18039 cp_parser_skip_to_end_of_block_or_statement (parser);
0a3b29ad 18040
18041 return decl;
18042}
18043
a63bc44c 18044/* Parse a cast-expression that is not the operand of a unary "&". */
18045
18046static tree
18047cp_parser_simple_cast_expression (cp_parser *parser)
18048{
640aa28c 18049 return cp_parser_cast_expression (parser, /*address_p=*/false,
98b326fd 18050 /*cast_p=*/false, NULL);
a63bc44c 18051}
18052
0a3b29ad 18053/* Parse a functional cast to TYPE. Returns an expression
18054 representing the cast. */
18055
18056static tree
45baea8b 18057cp_parser_functional_cast (cp_parser* parser, tree type)
0a3b29ad 18058{
f352a3fb 18059 VEC(tree,gc) *vec;
0a3b29ad 18060 tree expression_list;
9ee4e816 18061 tree cast;
f82f1250 18062 bool nonconst_p;
18063
18064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18065 {
18066 maybe_warn_cpp0x ("extended initializer lists");
18067 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18068 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18069 if (TREE_CODE (type) == TYPE_DECL)
18070 type = TREE_TYPE (type);
18071 return finish_compound_literal (type, expression_list);
18072 }
0a3b29ad 18073
f352a3fb 18074
18075 vec = cp_parser_parenthesized_expression_list (parser, false,
18076 /*cast_p=*/true,
18077 /*allow_expansion_p=*/true,
18078 /*non_constant_p=*/NULL);
18079 if (vec == NULL)
18080 expression_list = error_mark_node;
18081 else
18082 {
18083 expression_list = build_tree_list_vec (vec);
18084 release_tree_vector (vec);
18085 }
0a3b29ad 18086
ebd21de4 18087 cast = build_functional_cast (type, expression_list,
18088 tf_warning_or_error);
9ee4e816 18089 /* [expr.const]/1: In an integral constant expression "only type
18090 conversions to integral or enumeration type can be used". */
673b95fd 18091 if (TREE_CODE (type) == TYPE_DECL)
18092 type = TREE_TYPE (type);
bde9ebf7 18093 if (cast != error_mark_node
18094 && !cast_valid_in_integral_constant_expression_p (type)
18095 && (cp_parser_non_integral_constant_expression
18096 (parser, "a call to a constructor")))
18097 return error_mark_node;
9ee4e816 18098 return cast;
0a3b29ad 18099}
18100
92b128ed 18101/* Save the tokens that make up the body of a member function defined
18102 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18103 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18104 specifiers applied to the declaration. Returns the FUNCTION_DECL
18105 for the member function. */
18106
817adc77 18107static tree
92b128ed 18108cp_parser_save_member_function_body (cp_parser* parser,
4b9b2871 18109 cp_decl_specifier_seq *decl_specifiers,
3046c0a3 18110 cp_declarator *declarator,
92b128ed 18111 tree attributes)
18112{
00d26680 18113 cp_token *first;
18114 cp_token *last;
92b128ed 18115 tree fn;
18116
18117 /* Create the function-declaration. */
18118 fn = start_method (decl_specifiers, declarator, attributes);
18119 /* If something went badly wrong, bail out now. */
18120 if (fn == error_mark_node)
18121 {
18122 /* If there's a function-body, skip it. */
ccb84981 18123 if (cp_parser_token_starts_function_definition_p
92b128ed 18124 (cp_lexer_peek_token (parser->lexer)))
18125 cp_parser_skip_to_end_of_block_or_statement (parser);
18126 return error_mark_node;
18127 }
18128
18129 /* Remember it, if there default args to post process. */
18130 cp_parser_save_default_args (parser, fn);
18131
ccb84981 18132 /* Save away the tokens that make up the body of the
92b128ed 18133 function. */
00d26680 18134 first = parser->lexer->next_token;
f82f1250 18135 /* We can have braced-init-list mem-initializers before the fn body. */
18136 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18137 {
18138 cp_lexer_consume_token (parser->lexer);
18139 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18140 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18141 {
18142 /* cache_group will stop after an un-nested { } pair, too. */
18143 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18144 break;
18145
18146 /* variadic mem-inits have ... after the ')'. */
18147 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18148 cp_lexer_consume_token (parser->lexer);
18149 }
18150 }
00d26680 18151 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
92b128ed 18152 /* Handle function try blocks. */
18153 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
00d26680 18154 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18155 last = parser->lexer->next_token;
92b128ed 18156
18157 /* Save away the inline definition; we will process it when the
18158 class is complete. */
00d26680 18159 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
92b128ed 18160 DECL_PENDING_INLINE_P (fn) = 1;
18161
18162 /* We need to know that this was defined in the class, so that
18163 friend templates are handled correctly. */
18164 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18165
18166 /* We're done with the inline definition. */
18167 finish_method (fn);
18168
18169 /* Add FN to the queue of functions to be parsed later. */
18170 TREE_VALUE (parser->unparsed_functions_queues)
ccb84981 18171 = tree_cons (NULL_TREE, fn,
92b128ed 18172 TREE_VALUE (parser->unparsed_functions_queues));
18173
18174 return fn;
18175}
18176
8534c3a3 18177/* Parse a template-argument-list, as well as the trailing ">" (but
18178 not the opening ">"). See cp_parser_template_argument_list for the
18179 return value. */
18180
18181static tree
18182cp_parser_enclosed_template_argument_list (cp_parser* parser)
18183{
18184 tree arguments;
18185 tree saved_scope;
18186 tree saved_qualifying_scope;
18187 tree saved_object_scope;
18188 bool saved_greater_than_is_operator_p;
ef792945 18189 bool saved_skip_evaluation;
8534c3a3 18190
18191 /* [temp.names]
18192
18193 When parsing a template-id, the first non-nested `>' is taken as
18194 the end of the template-argument-list rather than a greater-than
18195 operator. */
ccb84981 18196 saved_greater_than_is_operator_p
8534c3a3 18197 = parser->greater_than_is_operator_p;
18198 parser->greater_than_is_operator_p = false;
18199 /* Parsing the argument list may modify SCOPE, so we save it
18200 here. */
18201 saved_scope = parser->scope;
18202 saved_qualifying_scope = parser->qualifying_scope;
18203 saved_object_scope = parser->object_scope;
ef792945 18204 /* We need to evaluate the template arguments, even though this
18205 template-id may be nested within a "sizeof". */
18206 saved_skip_evaluation = skip_evaluation;
18207 skip_evaluation = false;
8534c3a3 18208 /* Parse the template-argument-list itself. */
56471494 18209 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18210 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8534c3a3 18211 arguments = NULL_TREE;
18212 else
18213 arguments = cp_parser_template_argument_list (parser);
bece9ea1 18214 /* Look for the `>' that ends the template-argument-list. If we find
18215 a '>>' instead, it's probably just a typo. */
18216 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18217 {
6dcdb5de 18218 if (cxx_dialect != cxx98)
56471494 18219 {
18220 /* In C++0x, a `>>' in a template argument list or cast
18221 expression is considered to be two separate `>'
18222 tokens. So, change the current token to a `>', but don't
18223 consume it: it will be consumed later when the outer
18224 template argument list (or cast expression) is parsed.
18225 Note that this replacement of `>' for `>>' is necessary
18226 even if we are parsing tentatively: in the tentative
18227 case, after calling
18228 cp_parser_enclosed_template_argument_list we will always
18229 throw away all of the template arguments and the first
18230 closing `>', either because the template argument list
18231 was erroneous or because we are replacing those tokens
18232 with a CPP_TEMPLATE_ID token. The second `>' (which will
18233 not have been thrown away) is needed either to close an
18234 outer template argument list or to complete a new-style
18235 cast. */
18236 cp_token *token = cp_lexer_peek_token (parser->lexer);
18237 token->type = CPP_GREATER;
18238 }
18239 else if (!saved_greater_than_is_operator_p)
bece9ea1 18240 {
b9dd3954 18241 /* If we're in a nested template argument list, the '>>' has
18242 to be a typo for '> >'. We emit the error message, but we
18243 continue parsing and we push a '>' as next token, so that
18244 the argument list will be parsed correctly. Note that the
18245 global source location is still on the token before the
18246 '>>', so we need to say explicitly where we want it. */
18247 cp_token *token = cp_lexer_peek_token (parser->lexer);
18248 error ("%H%<>>%> should be %<> >%> "
18249 "within a nested template argument list",
18250 &token->location);
18251
bece9ea1 18252 token->type = CPP_GREATER;
18253 }
18254 else
18255 {
b9dd3954 18256 /* If this is not a nested template argument list, the '>>'
18257 is a typo for '>'. Emit an error message and continue.
18258 Same deal about the token location, but here we can get it
18259 right by consuming the '>>' before issuing the diagnostic. */
ad9ae192 18260 cp_token *token = cp_lexer_consume_token (parser->lexer);
18261 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18262 "a template argument list", &token->location);
bece9ea1 18263 }
18264 }
b9dd3954 18265 else
c42e0e2d 18266 cp_parser_skip_to_end_of_template_parameter_list (parser);
8534c3a3 18267 /* The `>' token might be a greater-than operator again now. */
ccb84981 18268 parser->greater_than_is_operator_p
8534c3a3 18269 = saved_greater_than_is_operator_p;
18270 /* Restore the SAVED_SCOPE. */
18271 parser->scope = saved_scope;
18272 parser->qualifying_scope = saved_qualifying_scope;
18273 parser->object_scope = saved_object_scope;
ef792945 18274 skip_evaluation = saved_skip_evaluation;
8534c3a3 18275
18276 return arguments;
18277}
18278
0a3b29ad 18279/* MEMBER_FUNCTION is a member function, or a friend. If default
18280 arguments, or the body of the function have not yet been parsed,
18281 parse them now. */
18282
18283static void
45baea8b 18284cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
0a3b29ad 18285{
0a3b29ad 18286 /* If this member is a template, get the underlying
18287 FUNCTION_DECL. */
18288 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18289 member_function = DECL_TEMPLATE_RESULT (member_function);
18290
18291 /* There should not be any class definitions in progress at this
18292 point; the bodies of members are only parsed outside of all class
18293 definitions. */
b4df430b 18294 gcc_assert (parser->num_classes_being_defined == 0);
0a3b29ad 18295 /* While we're parsing the member functions we might encounter more
18296 classes. We want to handle them right away, but we don't want
18297 them getting mixed up with functions that are currently in the
18298 queue. */
18299 parser->unparsed_functions_queues
18300 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18301
18302 /* Make sure that any template parameters are in scope. */
18303 maybe_begin_member_template_processing (member_function);
18304
0a3b29ad 18305 /* If the body of the function has not yet been parsed, parse it
18306 now. */
18307 if (DECL_PENDING_INLINE_P (member_function))
18308 {
18309 tree function_scope;
18310 cp_token_cache *tokens;
18311
18312 /* The function is no longer pending; we are processing it. */
18313 tokens = DECL_PENDING_INLINE_INFO (member_function);
18314 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18315 DECL_PENDING_INLINE_P (member_function) = 0;
9031d10b 18316
7ff3aeed 18317 /* If this is a local class, enter the scope of the containing
18318 function. */
18319 function_scope = current_function_decl;
0a3b29ad 18320 if (function_scope)
d2764e2d 18321 push_function_context ();
9031d10b 18322
b9dd3954 18323 /* Push the body of the function onto the lexer stack. */
18324 cp_parser_push_lexer_for_tokens (parser, tokens);
ccb84981 18325
0a3b29ad 18326 /* Let the front end know that we going to be defining this
18327 function. */
3046c0a3 18328 start_preparsed_function (member_function, NULL_TREE,
18329 SF_PRE_PARSED | SF_INCLASS_INLINE);
ccb84981 18330
69ebef96 18331 /* Don't do access checking if it is a templated function. */
18332 if (processing_template_decl)
18333 push_deferring_access_checks (dk_no_check);
9031d10b 18334
0a3b29ad 18335 /* Now, parse the body of the function. */
18336 cp_parser_function_definition_after_declarator (parser,
18337 /*inline_p=*/true);
ccb84981 18338
69ebef96 18339 if (processing_template_decl)
18340 pop_deferring_access_checks ();
9031d10b 18341
0a3b29ad 18342 /* Leave the scope of the containing function. */
18343 if (function_scope)
d2764e2d 18344 pop_function_context ();
b9dd3954 18345 cp_parser_pop_lexer (parser);
0a3b29ad 18346 }
18347
18348 /* Remove any template parameters from the symbol table. */
18349 maybe_end_member_template_processing ();
18350
18351 /* Restore the queue. */
ccb84981 18352 parser->unparsed_functions_queues
0a3b29ad 18353 = TREE_CHAIN (parser->unparsed_functions_queues);
18354}
18355
63eff20d 18356/* If DECL contains any default args, remember it on the unparsed
69b6679c 18357 functions queue. */
18358
18359static void
18360cp_parser_save_default_args (cp_parser* parser, tree decl)
18361{
18362 tree probe;
18363
18364 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18365 probe;
18366 probe = TREE_CHAIN (probe))
18367 if (TREE_PURPOSE (probe))
18368 {
18369 TREE_PURPOSE (parser->unparsed_functions_queues)
93c149df 18370 = tree_cons (current_class_type, decl,
69b6679c 18371 TREE_PURPOSE (parser->unparsed_functions_queues));
18372 break;
18373 }
69b6679c 18374}
18375
af128372 18376/* FN is a FUNCTION_DECL which may contains a parameter with an
93c149df 18377 unparsed DEFAULT_ARG. Parse the default args now. This function
18378 assumes that the current scope is the scope in which the default
18379 argument should be processed. */
0a3b29ad 18380
18381static void
af128372 18382cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
0a3b29ad 18383{
0a3b29ad 18384 bool saved_local_variables_forbidden_p;
b9dd3954 18385 tree parm;
af128372 18386
e6021728 18387 /* While we're parsing the default args, we might (due to the
18388 statement expression extension) encounter more classes. We want
18389 to handle them right away, but we don't want them getting mixed
18390 up with default args that are currently in the queue. */
18391 parser->unparsed_functions_queues
18392 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18393
b9dd3954 18394 /* Local variable names (and the `this' keyword) may not appear
18395 in a default argument. */
18396 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18397 parser->local_variables_forbidden_p = true;
18398
18399 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18400 parm;
18401 parm = TREE_CHAIN (parm))
0a3b29ad 18402 {
b9dd3954 18403 cp_token_cache *tokens;
29081c08 18404 tree default_arg = TREE_PURPOSE (parm);
18405 tree parsed_arg;
f51f5e0b 18406 VEC(tree,gc) *insts;
18407 tree copy;
18408 unsigned ix;
9031d10b 18409
29081c08 18410 if (!default_arg)
b9dd3954 18411 continue;
0a3b29ad 18412
f6219e82 18413 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18414 /* This can happen for a friend declaration for a function
18415 already declared with default arguments. */
18416 continue;
29081c08 18417
b9dd3954 18418 /* Push the saved tokens for the default argument onto the parser's
18419 lexer stack. */
29081c08 18420 tokens = DEFARG_TOKENS (default_arg);
b9dd3954 18421 cp_parser_push_lexer_for_tokens (parser, tokens);
0a3b29ad 18422
b9dd3954 18423 /* Parse the assignment-expression. */
98b326fd 18424 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
027f3cbf 18425 if (parsed_arg == error_mark_node)
18426 {
18427 cp_parser_pop_lexer (parser);
18428 continue;
18429 }
29081c08 18430
6d5a06c3 18431 if (!processing_template_decl)
18432 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
074ab442 18433
29081c08 18434 TREE_PURPOSE (parm) = parsed_arg;
18435
18436 /* Update any instantiations we've already created. */
f51f5e0b 18437 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18438 VEC_iterate (tree, insts, ix, copy); ix++)
18439 TREE_PURPOSE (copy) = parsed_arg;
0a3b29ad 18440
3ed12242 18441 /* If the token stream has not been completely used up, then
18442 there was extra junk after the end of the default
18443 argument. */
18444 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a2c5b975 18445 cp_parser_error (parser, "expected %<,%>");
3ed12242 18446
b9dd3954 18447 /* Revert to the main lexer. */
18448 cp_parser_pop_lexer (parser);
0a3b29ad 18449 }
e6021728 18450
917e3348 18451 /* Make sure no default arg is missing. */
18452 check_default_args (fn);
18453
b9dd3954 18454 /* Restore the state of local_variables_forbidden_p. */
18455 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18456
e6021728 18457 /* Restore the queue. */
ccb84981 18458 parser->unparsed_functions_queues
e6021728 18459 = TREE_CHAIN (parser->unparsed_functions_queues);
0a3b29ad 18460}
18461
18462/* Parse the operand of `sizeof' (or a similar operator). Returns
18463 either a TYPE or an expression, depending on the form of the
18464 input. The KEYWORD indicates which kind of expression we have
18465 encountered. */
18466
18467static tree
45baea8b 18468cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
0a3b29ad 18469{
0a3b29ad 18470 tree expr = NULL_TREE;
18471 const char *saved_message;
dea3189b 18472 char *tmp;
f47c1747 18473 bool saved_integral_constant_expression_p;
640aa28c 18474 bool saved_non_integral_constant_expression_p;
d95d815d 18475 bool pack_expansion_p = false;
0a3b29ad 18476
0a3b29ad 18477 /* Types cannot be defined in a `sizeof' expression. Save away the
18478 old message. */
18479 saved_message = parser->type_definition_forbidden_message;
18480 /* And create the new one. */
2e52ac87 18481 tmp = concat ("types may not be defined in %<",
18482 IDENTIFIER_POINTER (ridpointers[keyword]),
18483 "%> expressions", NULL);
18484 parser->type_definition_forbidden_message = tmp;
0a3b29ad 18485
18486 /* The restrictions on constant-expressions do not apply inside
18487 sizeof expressions. */
9031d10b 18488 saved_integral_constant_expression_p
640aa28c 18489 = parser->integral_constant_expression_p;
18490 saved_non_integral_constant_expression_p
18491 = parser->non_integral_constant_expression_p;
f47c1747 18492 parser->integral_constant_expression_p = false;
0a3b29ad 18493
d95d815d 18494 /* If it's a `...', then we are computing the length of a parameter
18495 pack. */
18496 if (keyword == RID_SIZEOF
18497 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18498 {
18499 /* Consume the `...'. */
18500 cp_lexer_consume_token (parser->lexer);
18501 maybe_warn_variadic_templates ();
18502
18503 /* Note that this is an expansion. */
18504 pack_expansion_p = true;
18505 }
18506
4c99a080 18507 /* Do not actually evaluate the expression. */
18508 ++skip_evaluation;
0a3b29ad 18509 /* If it's a `(', then we might be looking at the type-id
18510 construction. */
18511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18512 {
18513 tree type;
41f2d08e 18514 bool saved_in_type_id_in_expr_p;
0a3b29ad 18515
18516 /* We can't be sure yet whether we're looking at a type-id or an
18517 expression. */
18518 cp_parser_parse_tentatively (parser);
18519 /* Consume the `('. */
18520 cp_lexer_consume_token (parser->lexer);
18521 /* Parse the type-id. */
41f2d08e 18522 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18523 parser->in_type_id_in_expr_p = true;
0a3b29ad 18524 type = cp_parser_type_id (parser);
41f2d08e 18525 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 18526 /* Now, look for the trailing `)'. */
1e5fcbe2 18527 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
0a3b29ad 18528 /* If all went well, then we're done. */
18529 if (cp_parser_parse_definitely (parser))
18530 {
4b9b2871 18531 cp_decl_specifier_seq decl_specs;
18532
18533 /* Build a trivial decl-specifier-seq. */
18534 clear_decl_specs (&decl_specs);
18535 decl_specs.type = type;
0a3b29ad 18536
18537 /* Call grokdeclarator to figure out what type this is. */
3046c0a3 18538 expr = grokdeclarator (NULL,
4b9b2871 18539 &decl_specs,
0a3b29ad 18540 TYPENAME,
18541 /*initialized=*/0,
18542 /*attrlist=*/NULL);
18543 }
18544 }
18545
18546 /* If the type-id production did not work out, then we must be
18547 looking at the unary-expression production. */
18548 if (!expr)
640aa28c 18549 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 18550 /*cast_p=*/false, NULL);
d95d815d 18551
18552 if (pack_expansion_p)
18553 /* Build a pack expansion. */
18554 expr = make_pack_expansion (expr);
18555
4c99a080 18556 /* Go back to evaluating expressions. */
18557 --skip_evaluation;
0a3b29ad 18558
18559 /* Free the message we created. */
dea3189b 18560 free (tmp);
0a3b29ad 18561 /* And restore the old one. */
18562 parser->type_definition_forbidden_message = saved_message;
9031d10b 18563 parser->integral_constant_expression_p
640aa28c 18564 = saved_integral_constant_expression_p;
18565 parser->non_integral_constant_expression_p
18566 = saved_non_integral_constant_expression_p;
0a3b29ad 18567
18568 return expr;
18569}
18570
18571/* If the current declaration has no declarator, return true. */
18572
18573static bool
18574cp_parser_declares_only_class_p (cp_parser *parser)
18575{
ccb84981 18576 /* If the next token is a `;' or a `,' then there is no
0a3b29ad 18577 declarator. */
18578 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18579 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18580}
18581
78e0edfb 18582/* Update the DECL_SPECS to reflect the storage class indicated by
18583 KEYWORD. */
0a3b29ad 18584
4b9b2871 18585static void
78e0edfb 18586cp_parser_set_storage_class (cp_parser *parser,
18587 cp_decl_specifier_seq *decl_specs,
ad9ae192 18588 enum rid keyword,
18589 location_t location)
0a3b29ad 18590{
78e0edfb 18591 cp_storage_class storage_class;
18592
18593 if (parser->in_unbraced_linkage_specification_p)
18594 {
ad9ae192 18595 error ("%Hinvalid use of %qD in linkage specification",
18596 &location, ridpointers[keyword]);
78e0edfb 18597 return;
18598 }
18599 else if (decl_specs->storage_class != sc_none)
18600 {
ceec99b9 18601 decl_specs->conflicting_specifiers_p = true;
78e0edfb 18602 return;
18603 }
18604
18605 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18606 && decl_specs->specs[(int) ds_thread])
18607 {
ad9ae192 18608 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
78e0edfb 18609 decl_specs->specs[(int) ds_thread] = 0;
18610 }
18611
074ab442 18612 switch (keyword)
78e0edfb 18613 {
18614 case RID_AUTO:
18615 storage_class = sc_auto;
18616 break;
18617 case RID_REGISTER:
18618 storage_class = sc_register;
18619 break;
18620 case RID_STATIC:
18621 storage_class = sc_static;
18622 break;
18623 case RID_EXTERN:
18624 storage_class = sc_extern;
18625 break;
18626 case RID_MUTABLE:
18627 storage_class = sc_mutable;
18628 break;
18629 default:
18630 gcc_unreachable ();
18631 }
18632 decl_specs->storage_class = storage_class;
ceec99b9 18633
18634 /* A storage class specifier cannot be applied alongside a typedef
18635 specifier. If there is a typedef specifier present then set
18636 conflicting_specifiers_p which will trigger an error later
18637 on in grokdeclarator. */
18638 if (decl_specs->specs[(int)ds_typedef])
18639 decl_specs->conflicting_specifiers_p = true;
4b9b2871 18640}
18641
18642/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18643 is true, the type is a user-defined type; otherwise it is a
18644 built-in type specified by a keyword. */
0a3b29ad 18645
4b9b2871 18646static void
18647cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18648 tree type_spec,
eef0ab03 18649 location_t location,
4b9b2871 18650 bool user_defined_p)
18651{
18652 decl_specs->any_specifiers_p = true;
207355ad 18653
924bbf02 18654 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18655 (with, for example, in "typedef int wchar_t;") we remember that
18656 this is what happened. In system headers, we ignore these
18657 declarations so that G++ can work with system headers that are not
18658 C++-safe. */
207355ad 18659 if (decl_specs->specs[(int) ds_typedef]
dd9b4af4 18660 && !user_defined_p
263df831 18661 && (type_spec == boolean_type_node
924bbf02 18662 || type_spec == char16_type_node
18663 || type_spec == char32_type_node
263df831 18664 || type_spec == wchar_type_node)
dd9b4af4 18665 && (decl_specs->type
18666 || decl_specs->specs[(int) ds_long]
18667 || decl_specs->specs[(int) ds_short]
18668 || decl_specs->specs[(int) ds_unsigned]
18669 || decl_specs->specs[(int) ds_signed]))
fc1ad922 18670 {
18671 decl_specs->redefined_builtin_type = type_spec;
18672 if (!decl_specs->type)
18673 {
18674 decl_specs->type = type_spec;
18675 decl_specs->user_defined_type_p = false;
eef0ab03 18676 decl_specs->type_location = location;
fc1ad922 18677 }
18678 }
dd9b4af4 18679 else if (decl_specs->type)
18680 decl_specs->multiple_types_p = true;
4b9b2871 18681 else
18682 {
18683 decl_specs->type = type_spec;
18684 decl_specs->user_defined_type_p = user_defined_p;
fc1ad922 18685 decl_specs->redefined_builtin_type = NULL_TREE;
eef0ab03 18686 decl_specs->type_location = location;
0a3b29ad 18687 }
4b9b2871 18688}
0a3b29ad 18689
4b9b2871 18690/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18691 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18692
18693static bool
18694cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18695{
18696 return decl_specifiers->specs[(int) ds_friend] != 0;
0a3b29ad 18697}
18698
18699/* If the next token is of the indicated TYPE, consume it. Otherwise,
18700 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 18701
0a3b29ad 18702 Returns the token consumed, if the token had the appropriate type.
18703 Otherwise, returns NULL. */
18704
18705static cp_token *
45baea8b 18706cp_parser_require (cp_parser* parser,
653e5405 18707 enum cpp_ttype type,
18708 const char* token_desc)
0a3b29ad 18709{
18710 if (cp_lexer_next_token_is (parser->lexer, type))
18711 return cp_lexer_consume_token (parser->lexer);
18712 else
18713 {
2c593bd0 18714 /* Output the MESSAGE -- unless we're parsing tentatively. */
18715 if (!cp_parser_simulate_error (parser))
e1a6bbd7 18716 {
18717 char *message = concat ("expected ", token_desc, NULL);
18718 cp_parser_error (parser, message);
18719 free (message);
18720 }
0a3b29ad 18721 return NULL;
18722 }
18723}
18724
c42e0e2d 18725/* An error message is produced if the next token is not '>'.
18726 All further tokens are skipped until the desired token is
18727 found or '{', '}', ';' or an unbalanced ')' or ']'. */
0a3b29ad 18728
18729static void
c42e0e2d 18730cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
0a3b29ad 18731{
c42e0e2d 18732 /* Current level of '< ... >'. */
18733 unsigned level = 0;
18734 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
0a3b29ad 18735 unsigned nesting_depth = 0;
18736
c42e0e2d 18737 /* Are we ready, yet? If not, issue error message. */
18738 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
0a3b29ad 18739 return;
18740
18741 /* Skip tokens until the desired token is found. */
18742 while (true)
18743 {
18744 /* Peek at the next token. */
c42e0e2d 18745 switch (cp_lexer_peek_token (parser->lexer)->type)
0a3b29ad 18746 {
c42e0e2d 18747 case CPP_LESS:
18748 if (!nesting_depth)
18749 ++level;
18750 break;
b75b98aa 18751
56471494 18752 case CPP_RSHIFT:
6dcdb5de 18753 if (cxx_dialect == cxx98)
56471494 18754 /* C++0x views the `>>' operator as two `>' tokens, but
18755 C++98 does not. */
18756 break;
18757 else if (!nesting_depth && level-- == 0)
18758 {
18759 /* We've hit a `>>' where the first `>' closes the
18760 template argument list, and the second `>' is
18761 spurious. Just consume the `>>' and stop; we've
18762 already produced at least one error. */
18763 cp_lexer_consume_token (parser->lexer);
18764 return;
18765 }
18766 /* Fall through for C++0x, so we handle the second `>' in
18767 the `>>'. */
18768
c42e0e2d 18769 case CPP_GREATER:
18770 if (!nesting_depth && level-- == 0)
18771 {
18772 /* We've reached the token we want, consume it and stop. */
18773 cp_lexer_consume_token (parser->lexer);
18774 return;
18775 }
18776 break;
b75b98aa 18777
b75b98aa 18778 case CPP_OPEN_PAREN:
18779 case CPP_OPEN_SQUARE:
18780 ++nesting_depth;
18781 break;
18782
b75b98aa 18783 case CPP_CLOSE_PAREN:
18784 case CPP_CLOSE_SQUARE:
0a3b29ad 18785 if (nesting_depth-- == 0)
18786 return;
b75b98aa 18787 break;
18788
c42e0e2d 18789 case CPP_EOF:
18790 case CPP_PRAGMA_EOL:
18791 case CPP_SEMICOLON:
18792 case CPP_OPEN_BRACE:
18793 case CPP_CLOSE_BRACE:
18794 /* The '>' was probably forgotten, don't look further. */
18795 return;
18796
b75b98aa 18797 default:
18798 break;
0a3b29ad 18799 }
b75b98aa 18800
0a3b29ad 18801 /* Consume this token. */
18802 cp_lexer_consume_token (parser->lexer);
18803 }
18804}
18805
18806/* If the next token is the indicated keyword, consume it. Otherwise,
18807 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 18808
0a3b29ad 18809 Returns the token consumed, if the token had the appropriate type.
18810 Otherwise, returns NULL. */
18811
18812static cp_token *
45baea8b 18813cp_parser_require_keyword (cp_parser* parser,
653e5405 18814 enum rid keyword,
18815 const char* token_desc)
0a3b29ad 18816{
18817 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18818
18819 if (token && token->keyword != keyword)
18820 {
18821 dyn_string_t error_msg;
18822
18823 /* Format the error message. */
18824 error_msg = dyn_string_new (0);
18825 dyn_string_append_cstr (error_msg, "expected ");
18826 dyn_string_append_cstr (error_msg, token_desc);
18827 cp_parser_error (parser, error_msg->s);
18828 dyn_string_delete (error_msg);
18829 return NULL;
18830 }
18831
18832 return token;
18833}
18834
18835/* Returns TRUE iff TOKEN is a token that can begin the body of a
18836 function-definition. */
18837
ccb84981 18838static bool
45baea8b 18839cp_parser_token_starts_function_definition_p (cp_token* token)
0a3b29ad 18840{
18841 return (/* An ordinary function-body begins with an `{'. */
18842 token->type == CPP_OPEN_BRACE
18843 /* A ctor-initializer begins with a `:'. */
18844 || token->type == CPP_COLON
18845 /* A function-try-block begins with `try'. */
18846 || token->keyword == RID_TRY
18847 /* The named return value extension begins with `return'. */
18848 || token->keyword == RID_RETURN);
18849}
18850
18851/* Returns TRUE iff the next token is the ":" or "{" beginning a class
18852 definition. */
18853
18854static bool
18855cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18856{
18857 cp_token *token;
18858
18859 token = cp_lexer_peek_token (parser->lexer);
18860 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18861}
18862
56471494 18863/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18864 C++0x) ending a template-argument. */
13795292 18865
18866static bool
18867cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18868{
18869 cp_token *token;
18870
18871 token = cp_lexer_peek_token (parser->lexer);
d95d815d 18872 return (token->type == CPP_COMMA
18873 || token->type == CPP_GREATER
56471494 18874 || token->type == CPP_ELLIPSIS
6dcdb5de 18875 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
13795292 18876}
c8d5ab79 18877
945b33d4 18878/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
c8d5ab79 18879 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18880
18881static bool
ccb84981 18882cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
c8d5ab79 18883 size_t n)
18884{
18885 cp_token *token;
18886
18887 token = cp_lexer_peek_nth_token (parser->lexer, n);
18888 if (token->type == CPP_LESS)
18889 return true;
18890 /* Check for the sequence `<::' in the original code. It would be lexed as
18891 `[:', where `[' is a digraph, and there is no whitespace before
18892 `:'. */
18893 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18894 {
18895 cp_token *token2;
18896 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18897 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18898 return true;
18899 }
18900 return false;
18901}
ccb84981 18902
0a3b29ad 18903/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18904 or none_type otherwise. */
18905
18906static enum tag_types
45baea8b 18907cp_parser_token_is_class_key (cp_token* token)
0a3b29ad 18908{
18909 switch (token->keyword)
18910 {
18911 case RID_CLASS:
18912 return class_type;
18913 case RID_STRUCT:
18914 return record_type;
18915 case RID_UNION:
18916 return union_type;
ccb84981 18917
0a3b29ad 18918 default:
18919 return none_type;
18920 }
18921}
18922
18923/* Issue an error message if the CLASS_KEY does not match the TYPE. */
18924
18925static void
18926cp_parser_check_class_key (enum tag_types class_key, tree type)
18927{
18928 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2b9e3597 18929 permerror (input_location, "%qs tag used in naming %q#T",
0a3b29ad 18930 class_key == union_type ? "union"
ccb84981 18931 : class_key == record_type ? "struct" : "class",
0a3b29ad 18932 type);
18933}
ccb84981 18934
63eff20d 18935/* Issue an error message if DECL is redeclared with different
7e35473e 18936 access than its original declaration [class.access.spec/3].
18937 This applies to nested classes and nested class templates.
18938 [class.mem/1]. */
18939
a2c5b975 18940static void
ad9ae192 18941cp_parser_check_access_in_redeclaration (tree decl, location_t location)
7e35473e 18942{
a7905afa 18943 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
7e35473e 18944 return;
18945
18946 if ((TREE_PRIVATE (decl)
18947 != (current_access_specifier == access_private_node))
18948 || (TREE_PROTECTED (decl)
18949 != (current_access_specifier == access_protected_node)))
ad9ae192 18950 error ("%H%qD redeclared with different access", &location, decl);
7e35473e 18951}
18952
0a3b29ad 18953/* Look for the `template' keyword, as a syntactic disambiguator.
ccb84981 18954 Return TRUE iff it is present, in which case it will be
0a3b29ad 18955 consumed. */
18956
18957static bool
18958cp_parser_optional_template_keyword (cp_parser *parser)
18959{
18960 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18961 {
18962 /* The `template' keyword can only be used within templates;
18963 outside templates the parser can always figure out what is a
18964 template and what is not. */
18965 if (!processing_template_decl)
18966 {
ad9ae192 18967 cp_token *token = cp_lexer_peek_token (parser->lexer);
18968 error ("%H%<template%> (as a disambiguator) is only allowed "
18969 "within templates", &token->location);
0a3b29ad 18970 /* If this part of the token stream is rescanned, the same
18971 error message would be generated. So, we purge the token
18972 from the stream. */
18973 cp_lexer_purge_token (parser->lexer);
18974 return false;
18975 }
18976 else
18977 {
18978 /* Consume the `template' keyword. */
18979 cp_lexer_consume_token (parser->lexer);
18980 return true;
18981 }
18982 }
18983
18984 return false;
18985}
18986
b3c48b5d 18987/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18988 set PARSER->SCOPE, and perform other related actions. */
18989
18990static void
18991cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18992{
3369eb76 18993 int i;
18994 struct tree_check *check_value;
18995 deferred_access_check *chk;
18996 VEC (deferred_access_check,gc) *checks;
b3c48b5d 18997
18998 /* Get the stored value. */
3369eb76 18999 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
b3c48b5d 19000 /* Perform any access checks that were deferred. */
3369eb76 19001 checks = check_value->checks;
19002 if (checks)
19003 {
19004 for (i = 0 ;
19005 VEC_iterate (deferred_access_check, checks, i, chk) ;
19006 ++i)
19007 {
19008 perform_or_defer_access_check (chk->binfo,
19009 chk->decl,
19010 chk->diag_decl);
19011 }
19012 }
b3c48b5d 19013 /* Set the scope from the stored value. */
3369eb76 19014 parser->scope = check_value->value;
19015 parser->qualifying_scope = check_value->qualifying_scope;
b3c48b5d 19016 parser->object_scope = NULL_TREE;
19017}
19018
f82f1250 19019/* Consume tokens up through a non-nested END token. Returns TRUE if we
19020 encounter the end of a block before what we were looking for. */
0a3b29ad 19021
f82f1250 19022static bool
00d26680 19023cp_parser_cache_group (cp_parser *parser,
19024 enum cpp_ttype end,
19025 unsigned depth)
0a3b29ad 19026{
19027 while (true)
19028 {
f82f1250 19029 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 19030
f82f1250 19031 /* Abort a parenthesized expression if we encounter a semicolon. */
0a3b29ad 19032 if ((end == CPP_CLOSE_PAREN || depth == 0)
f82f1250 19033 && token->type == CPP_SEMICOLON)
19034 return true;
0a3b29ad 19035 /* If we've reached the end of the file, stop. */
f82f1250 19036 if (token->type == CPP_EOF
b75b98aa 19037 || (end != CPP_PRAGMA_EOL
f82f1250 19038 && token->type == CPP_PRAGMA_EOL))
19039 return true;
19040 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19041 /* We've hit the end of an enclosing block, so there's been some
19042 kind of syntax error. */
19043 return true;
19044
19045 /* Consume the token. */
19046 cp_lexer_consume_token (parser->lexer);
0a3b29ad 19047 /* See if it starts a new group. */
19048 if (token->type == CPP_OPEN_BRACE)
19049 {
00d26680 19050 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
f82f1250 19051 /* In theory this should probably check end == '}', but
19052 cp_parser_save_member_function_body needs it to exit
19053 after either '}' or ')' when called with ')'. */
0a3b29ad 19054 if (depth == 0)
f82f1250 19055 return false;
0a3b29ad 19056 }
19057 else if (token->type == CPP_OPEN_PAREN)
f82f1250 19058 {
19059 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19060 if (depth == 0 && end == CPP_CLOSE_PAREN)
19061 return false;
19062 }
b75b98aa 19063 else if (token->type == CPP_PRAGMA)
19064 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
0a3b29ad 19065 else if (token->type == end)
f82f1250 19066 return false;
0a3b29ad 19067 }
19068}
19069
19070/* Begin parsing tentatively. We always save tokens while parsing
19071 tentatively so that if the tentative parsing fails we can restore the
19072 tokens. */
19073
19074static void
45baea8b 19075cp_parser_parse_tentatively (cp_parser* parser)
0a3b29ad 19076{
19077 /* Enter a new parsing context. */
19078 parser->context = cp_parser_context_new (parser->context);
19079 /* Begin saving tokens. */
19080 cp_lexer_save_tokens (parser->lexer);
19081 /* In order to avoid repetitive access control error messages,
19082 access checks are queued up until we are no longer parsing
19083 tentatively. */
4f62c42e 19084 push_deferring_access_checks (dk_deferred);
0a3b29ad 19085}
19086
19087/* Commit to the currently active tentative parse. */
19088
19089static void
45baea8b 19090cp_parser_commit_to_tentative_parse (cp_parser* parser)
0a3b29ad 19091{
19092 cp_parser_context *context;
19093 cp_lexer *lexer;
19094
19095 /* Mark all of the levels as committed. */
19096 lexer = parser->lexer;
19097 for (context = parser->context; context->next; context = context->next)
19098 {
19099 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19100 break;
19101 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19102 while (!cp_lexer_saving_tokens (lexer))
19103 lexer = lexer->next;
19104 cp_lexer_commit_tokens (lexer);
19105 }
19106}
19107
19108/* Abort the currently active tentative parse. All consumed tokens
19109 will be rolled back, and no diagnostics will be issued. */
19110
19111static void
45baea8b 19112cp_parser_abort_tentative_parse (cp_parser* parser)
0a3b29ad 19113{
19114 cp_parser_simulate_error (parser);
19115 /* Now, pretend that we want to see if the construct was
19116 successfully parsed. */
19117 cp_parser_parse_definitely (parser);
19118}
19119
755edffd 19120/* Stop parsing tentatively. If a parse error has occurred, restore the
0a3b29ad 19121 token stream. Otherwise, commit to the tokens we have consumed.
19122 Returns true if no error occurred; false otherwise. */
19123
19124static bool
45baea8b 19125cp_parser_parse_definitely (cp_parser* parser)
0a3b29ad 19126{
19127 bool error_occurred;
19128 cp_parser_context *context;
19129
755edffd 19130 /* Remember whether or not an error occurred, since we are about to
0a3b29ad 19131 destroy that information. */
19132 error_occurred = cp_parser_error_occurred (parser);
19133 /* Remove the topmost context from the stack. */
19134 context = parser->context;
19135 parser->context = context->next;
19136 /* If no parse errors occurred, commit to the tentative parse. */
19137 if (!error_occurred)
19138 {
19139 /* Commit to the tokens read tentatively, unless that was
19140 already done. */
19141 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19142 cp_lexer_commit_tokens (parser->lexer);
9b57b06b 19143
19144 pop_to_parent_deferring_access_checks ();
0a3b29ad 19145 }
19146 /* Otherwise, if errors occurred, roll back our state so that things
19147 are just as they were before we began the tentative parse. */
19148 else
9b57b06b 19149 {
19150 cp_lexer_rollback_tokens (parser->lexer);
19151 pop_deferring_access_checks ();
19152 }
2c593bd0 19153 /* Add the context to the front of the free list. */
19154 context->next = cp_parser_context_free_list;
19155 cp_parser_context_free_list = context;
19156
19157 return !error_occurred;
0a3b29ad 19158}
19159
efcbcf83 19160/* Returns true if we are parsing tentatively and are not committed to
19161 this tentative parse. */
0a3b29ad 19162
19163static bool
efcbcf83 19164cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
0a3b29ad 19165{
19166 return (cp_parser_parsing_tentatively (parser)
efcbcf83 19167 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
0a3b29ad 19168}
19169
f1d555e3 19170/* Returns nonzero iff an error has occurred during the most recent
0a3b29ad 19171 tentative parse. */
ccb84981 19172
0a3b29ad 19173static bool
45baea8b 19174cp_parser_error_occurred (cp_parser* parser)
0a3b29ad 19175{
19176 return (cp_parser_parsing_tentatively (parser)
19177 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19178}
19179
f1d555e3 19180/* Returns nonzero if GNU extensions are allowed. */
0a3b29ad 19181
19182static bool
45baea8b 19183cp_parser_allow_gnu_extensions_p (cp_parser* parser)
0a3b29ad 19184{
19185 return parser->allow_gnu_extensions_p;
19186}
7a4e126b 19187\f
19188/* Objective-C++ Productions */
19189
19190
19191/* Parse an Objective-C expression, which feeds into a primary-expression
19192 above.
19193
19194 objc-expression:
19195 objc-message-expression
19196 objc-string-literal
19197 objc-encode-expression
19198 objc-protocol-expression
19199 objc-selector-expression
19200
19201 Returns a tree representation of the expression. */
19202
19203static tree
19204cp_parser_objc_expression (cp_parser* parser)
19205{
19206 /* Try to figure out what kind of declaration is present. */
19207 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19208
19209 switch (kwd->type)
19210 {
19211 case CPP_OPEN_SQUARE:
19212 return cp_parser_objc_message_expression (parser);
19213
19214 case CPP_OBJC_STRING:
19215 kwd = cp_lexer_consume_token (parser->lexer);
3369eb76 19216 return objc_build_string_object (kwd->u.value);
7a4e126b 19217
19218 case CPP_KEYWORD:
19219 switch (kwd->keyword)
19220 {
19221 case RID_AT_ENCODE:
19222 return cp_parser_objc_encode_expression (parser);
19223
19224 case RID_AT_PROTOCOL:
19225 return cp_parser_objc_protocol_expression (parser);
19226
19227 case RID_AT_SELECTOR:
19228 return cp_parser_objc_selector_expression (parser);
19229
19230 default:
19231 break;
19232 }
19233 default:
ad9ae192 19234 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19235 &kwd->location, kwd->u.value);
7a4e126b 19236 cp_parser_skip_to_end_of_block_or_statement (parser);
19237 }
19238
19239 return error_mark_node;
19240}
19241
19242/* Parse an Objective-C message expression.
19243
19244 objc-message-expression:
19245 [ objc-message-receiver objc-message-args ]
19246
19247 Returns a representation of an Objective-C message. */
19248
19249static tree
19250cp_parser_objc_message_expression (cp_parser* parser)
19251{
19252 tree receiver, messageargs;
19253
19254 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19255 receiver = cp_parser_objc_message_receiver (parser);
19256 messageargs = cp_parser_objc_message_args (parser);
640710cf 19257 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7a4e126b 19258
19259 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19260}
19261
19262/* Parse an objc-message-receiver.
19263
19264 objc-message-receiver:
7a4e126b 19265 expression
aa796005 19266 simple-type-specifier
7a4e126b 19267
19268 Returns a representation of the type or expression. */
19269
19270static tree
19271cp_parser_objc_message_receiver (cp_parser* parser)
19272{
19273 tree rcv;
7a4e126b 19274
19275 /* An Objective-C message receiver may be either (1) a type
19276 or (2) an expression. */
19277 cp_parser_parse_tentatively (parser);
98b326fd 19278 rcv = cp_parser_expression (parser, false, NULL);
7a4e126b 19279
19280 if (cp_parser_parse_definitely (parser))
19281 return rcv;
19282
aa796005 19283 rcv = cp_parser_simple_type_specifier (parser,
19284 /*decl_specs=*/NULL,
19285 CP_PARSER_FLAGS_NONE);
7a4e126b 19286
19287 return objc_get_class_reference (rcv);
19288}
19289
19290/* Parse the arguments and selectors comprising an Objective-C message.
19291
19292 objc-message-args:
19293 objc-selector
19294 objc-selector-args
19295 objc-selector-args , objc-comma-args
19296
19297 objc-selector-args:
19298 objc-selector [opt] : assignment-expression
19299 objc-selector-args objc-selector [opt] : assignment-expression
19300
19301 objc-comma-args:
19302 assignment-expression
19303 objc-comma-args , assignment-expression
19304
19305 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19306 selector arguments and TREE_VALUE containing a list of comma
19307 arguments. */
19308
19309static tree
19310cp_parser_objc_message_args (cp_parser* parser)
19311{
19312 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19313 bool maybe_unary_selector_p = true;
19314 cp_token *token = cp_lexer_peek_token (parser->lexer);
19315
19316 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19317 {
19318 tree selector = NULL_TREE, arg;
19319
19320 if (token->type != CPP_COLON)
19321 selector = cp_parser_objc_selector (parser);
19322
19323 /* Detect if we have a unary selector. */
19324 if (maybe_unary_selector_p
19325 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19326 return build_tree_list (selector, NULL_TREE);
19327
19328 maybe_unary_selector_p = false;
640710cf 19329 cp_parser_require (parser, CPP_COLON, "%<:%>");
98b326fd 19330 arg = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 19331
19332 sel_args
19333 = chainon (sel_args,
19334 build_tree_list (selector, arg));
19335
19336 token = cp_lexer_peek_token (parser->lexer);
19337 }
19338
19339 /* Handle non-selector arguments, if any. */
19340 while (token->type == CPP_COMMA)
19341 {
19342 tree arg;
19343
19344 cp_lexer_consume_token (parser->lexer);
98b326fd 19345 arg = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 19346
19347 addl_args
19348 = chainon (addl_args,
19349 build_tree_list (NULL_TREE, arg));
19350
19351 token = cp_lexer_peek_token (parser->lexer);
19352 }
19353
19354 return build_tree_list (sel_args, addl_args);
19355}
19356
19357/* Parse an Objective-C encode expression.
19358
19359 objc-encode-expression:
19360 @encode objc-typename
9031d10b 19361
7a4e126b 19362 Returns an encoded representation of the type argument. */
19363
19364static tree
19365cp_parser_objc_encode_expression (cp_parser* parser)
19366{
19367 tree type;
ad9ae192 19368 cp_token *token;
7a4e126b 19369
19370 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
640710cf 19371 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
ad9ae192 19372 token = cp_lexer_peek_token (parser->lexer);
7a4e126b 19373 type = complete_type (cp_parser_type_id (parser));
640710cf 19374 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 19375
19376 if (!type)
19377 {
ad9ae192 19378 error ("%H%<@encode%> must specify a type as an argument",
19379 &token->location);
7a4e126b 19380 return error_mark_node;
19381 }
19382
19383 return objc_build_encode_expr (type);
19384}
19385
19386/* Parse an Objective-C @defs expression. */
19387
19388static tree
19389cp_parser_objc_defs_expression (cp_parser *parser)
19390{
19391 tree name;
19392
19393 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
640710cf 19394 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7a4e126b 19395 name = cp_parser_identifier (parser);
640710cf 19396 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 19397
19398 return objc_get_class_ivars (name);
19399}
19400
19401/* Parse an Objective-C protocol expression.
19402
19403 objc-protocol-expression:
19404 @protocol ( identifier )
19405
19406 Returns a representation of the protocol expression. */
19407
19408static tree
19409cp_parser_objc_protocol_expression (cp_parser* parser)
19410{
19411 tree proto;
19412
19413 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
640710cf 19414 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7a4e126b 19415 proto = cp_parser_identifier (parser);
640710cf 19416 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 19417
19418 return objc_build_protocol_expr (proto);
19419}
19420
19421/* Parse an Objective-C selector expression.
19422
19423 objc-selector-expression:
19424 @selector ( objc-method-signature )
19425
19426 objc-method-signature:
19427 objc-selector
19428 objc-selector-seq
19429
19430 objc-selector-seq:
19431 objc-selector :
19432 objc-selector-seq objc-selector :
19433
19434 Returns a representation of the method selector. */
19435
19436static tree
19437cp_parser_objc_selector_expression (cp_parser* parser)
19438{
19439 tree sel_seq = NULL_TREE;
19440 bool maybe_unary_selector_p = true;
19441 cp_token *token;
e60a6f7b 19442 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7a4e126b 19443
19444 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
640710cf 19445 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7a4e126b 19446 token = cp_lexer_peek_token (parser->lexer);
19447
ca7aef9f 19448 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
074ab442 19449 || token->type == CPP_SCOPE)
7a4e126b 19450 {
19451 tree selector = NULL_TREE;
19452
ca7aef9f 19453 if (token->type != CPP_COLON
19454 || token->type == CPP_SCOPE)
7a4e126b 19455 selector = cp_parser_objc_selector (parser);
19456
ca7aef9f 19457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
074ab442 19458 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
7a4e126b 19459 {
ca7aef9f 19460 /* Detect if we have a unary selector. */
19461 if (maybe_unary_selector_p)
19462 {
19463 sel_seq = selector;
19464 goto finish_selector;
19465 }
19466 else
19467 {
19468 cp_parser_error (parser, "expected %<:%>");
19469 }
7a4e126b 19470 }
7a4e126b 19471 maybe_unary_selector_p = false;
ca7aef9f 19472 token = cp_lexer_consume_token (parser->lexer);
074ab442 19473
ca7aef9f 19474 if (token->type == CPP_SCOPE)
074ab442 19475 {
ca7aef9f 19476 sel_seq
19477 = chainon (sel_seq,
19478 build_tree_list (selector, NULL_TREE));
19479 sel_seq
19480 = chainon (sel_seq,
19481 build_tree_list (NULL_TREE, NULL_TREE));
19482 }
19483 else
19484 sel_seq
19485 = chainon (sel_seq,
19486 build_tree_list (selector, NULL_TREE));
7a4e126b 19487
19488 token = cp_lexer_peek_token (parser->lexer);
19489 }
19490
19491 finish_selector:
640710cf 19492 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 19493
e60a6f7b 19494 return objc_build_selector_expr (loc, sel_seq);
7a4e126b 19495}
19496
19497/* Parse a list of identifiers.
19498
19499 objc-identifier-list:
19500 identifier
19501 objc-identifier-list , identifier
19502
19503 Returns a TREE_LIST of identifier nodes. */
19504
19505static tree
19506cp_parser_objc_identifier_list (cp_parser* parser)
19507{
19508 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19509 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19510
19511 while (sep->type == CPP_COMMA)
19512 {
19513 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
9031d10b 19514 list = chainon (list,
7a4e126b 19515 build_tree_list (NULL_TREE,
19516 cp_parser_identifier (parser)));
19517 sep = cp_lexer_peek_token (parser->lexer);
19518 }
9031d10b 19519
7a4e126b 19520 return list;
19521}
19522
19523/* Parse an Objective-C alias declaration.
19524
19525 objc-alias-declaration:
19526 @compatibility_alias identifier identifier ;
19527
a17c2a3a 19528 This function registers the alias mapping with the Objective-C front end.
7a4e126b 19529 It returns nothing. */
19530
19531static void
19532cp_parser_objc_alias_declaration (cp_parser* parser)
19533{
19534 tree alias, orig;
19535
19536 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19537 alias = cp_parser_identifier (parser);
19538 orig = cp_parser_identifier (parser);
19539 objc_declare_alias (alias, orig);
19540 cp_parser_consume_semicolon_at_end_of_statement (parser);
19541}
19542
19543/* Parse an Objective-C class forward-declaration.
19544
19545 objc-class-declaration:
19546 @class objc-identifier-list ;
19547
19548 The function registers the forward declarations with the Objective-C
a17c2a3a 19549 front end. It returns nothing. */
7a4e126b 19550
19551static void
19552cp_parser_objc_class_declaration (cp_parser* parser)
19553{
19554 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19555 objc_declare_class (cp_parser_objc_identifier_list (parser));
19556 cp_parser_consume_semicolon_at_end_of_statement (parser);
19557}
19558
19559/* Parse a list of Objective-C protocol references.
19560
19561 objc-protocol-refs-opt:
19562 objc-protocol-refs [opt]
19563
19564 objc-protocol-refs:
19565 < objc-identifier-list >
19566
19567 Returns a TREE_LIST of identifiers, if any. */
19568
19569static tree
19570cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19571{
19572 tree protorefs = NULL_TREE;
19573
19574 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19575 {
19576 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19577 protorefs = cp_parser_objc_identifier_list (parser);
640710cf 19578 cp_parser_require (parser, CPP_GREATER, "%<>%>");
7a4e126b 19579 }
19580
19581 return protorefs;
19582}
19583
19584/* Parse a Objective-C visibility specification. */
19585
19586static void
19587cp_parser_objc_visibility_spec (cp_parser* parser)
19588{
19589 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19590
19591 switch (vis->keyword)
19592 {
19593 case RID_AT_PRIVATE:
19594 objc_set_visibility (2);
19595 break;
19596 case RID_AT_PROTECTED:
19597 objc_set_visibility (0);
19598 break;
19599 case RID_AT_PUBLIC:
19600 objc_set_visibility (1);
19601 break;
19602 default:
19603 return;
19604 }
0a3b29ad 19605
7a4e126b 19606 /* Eat '@private'/'@protected'/'@public'. */
19607 cp_lexer_consume_token (parser->lexer);
19608}
19609
19610/* Parse an Objective-C method type. */
19611
19612static void
19613cp_parser_objc_method_type (cp_parser* parser)
19614{
19615 objc_set_method_type
19616 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19617 ? PLUS_EXPR
19618 : MINUS_EXPR);
19619}
19620
19621/* Parse an Objective-C protocol qualifier. */
19622
19623static tree
19624cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19625{
19626 tree quals = NULL_TREE, node;
19627 cp_token *token = cp_lexer_peek_token (parser->lexer);
19628
3369eb76 19629 node = token->u.value;
7a4e126b 19630
19631 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19632 && (node == ridpointers [(int) RID_IN]
19633 || node == ridpointers [(int) RID_OUT]
19634 || node == ridpointers [(int) RID_INOUT]
19635 || node == ridpointers [(int) RID_BYCOPY]
653e5405 19636 || node == ridpointers [(int) RID_BYREF]
7a4e126b 19637 || node == ridpointers [(int) RID_ONEWAY]))
19638 {
19639 quals = tree_cons (NULL_TREE, node, quals);
19640 cp_lexer_consume_token (parser->lexer);
19641 token = cp_lexer_peek_token (parser->lexer);
3369eb76 19642 node = token->u.value;
7a4e126b 19643 }
19644
19645 return quals;
19646}
19647
19648/* Parse an Objective-C typename. */
19649
19650static tree
19651cp_parser_objc_typename (cp_parser* parser)
19652{
607a5d68 19653 tree type_name = NULL_TREE;
7a4e126b 19654
19655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19656 {
19657 tree proto_quals, cp_type = NULL_TREE;
19658
19659 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19660 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19661
19662 /* An ObjC type name may consist of just protocol qualifiers, in which
19663 case the type shall default to 'id'. */
19664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19665 cp_type = cp_parser_type_id (parser);
19666
640710cf 19667 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
607a5d68 19668 type_name = build_tree_list (proto_quals, cp_type);
7a4e126b 19669 }
19670
607a5d68 19671 return type_name;
7a4e126b 19672}
19673
19674/* Check to see if TYPE refers to an Objective-C selector name. */
19675
19676static bool
19677cp_parser_objc_selector_p (enum cpp_ttype type)
19678{
19679 return (type == CPP_NAME || type == CPP_KEYWORD
19680 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19681 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19682 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19683 || type == CPP_XOR || type == CPP_XOR_EQ);
19684}
19685
19686/* Parse an Objective-C selector. */
19687
19688static tree
19689cp_parser_objc_selector (cp_parser* parser)
19690{
19691 cp_token *token = cp_lexer_consume_token (parser->lexer);
9031d10b 19692
7a4e126b 19693 if (!cp_parser_objc_selector_p (token->type))
19694 {
ad9ae192 19695 error ("%Hinvalid Objective-C++ selector name", &token->location);
7a4e126b 19696 return error_mark_node;
19697 }
19698
19699 /* C++ operator names are allowed to appear in ObjC selectors. */
19700 switch (token->type)
19701 {
19702 case CPP_AND_AND: return get_identifier ("and");
19703 case CPP_AND_EQ: return get_identifier ("and_eq");
19704 case CPP_AND: return get_identifier ("bitand");
19705 case CPP_OR: return get_identifier ("bitor");
19706 case CPP_COMPL: return get_identifier ("compl");
19707 case CPP_NOT: return get_identifier ("not");
19708 case CPP_NOT_EQ: return get_identifier ("not_eq");
19709 case CPP_OR_OR: return get_identifier ("or");
19710 case CPP_OR_EQ: return get_identifier ("or_eq");
19711 case CPP_XOR: return get_identifier ("xor");
19712 case CPP_XOR_EQ: return get_identifier ("xor_eq");
3369eb76 19713 default: return token->u.value;
7a4e126b 19714 }
19715}
19716
19717/* Parse an Objective-C params list. */
19718
19719static tree
19720cp_parser_objc_method_keyword_params (cp_parser* parser)
19721{
19722 tree params = NULL_TREE;
19723 bool maybe_unary_selector_p = true;
19724 cp_token *token = cp_lexer_peek_token (parser->lexer);
19725
19726 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19727 {
607a5d68 19728 tree selector = NULL_TREE, type_name, identifier;
7a4e126b 19729
19730 if (token->type != CPP_COLON)
19731 selector = cp_parser_objc_selector (parser);
19732
19733 /* Detect if we have a unary selector. */
19734 if (maybe_unary_selector_p
19735 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19736 return selector;
19737
19738 maybe_unary_selector_p = false;
640710cf 19739 cp_parser_require (parser, CPP_COLON, "%<:%>");
607a5d68 19740 type_name = cp_parser_objc_typename (parser);
7a4e126b 19741 identifier = cp_parser_identifier (parser);
19742
19743 params
19744 = chainon (params,
9031d10b 19745 objc_build_keyword_decl (selector,
607a5d68 19746 type_name,
7a4e126b 19747 identifier));
19748
19749 token = cp_lexer_peek_token (parser->lexer);
19750 }
19751
19752 return params;
19753}
19754
19755/* Parse the non-keyword Objective-C params. */
19756
19757static tree
19758cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19759{
19760 tree params = make_node (TREE_LIST);
19761 cp_token *token = cp_lexer_peek_token (parser->lexer);
19762 *ellipsisp = false; /* Initially, assume no ellipsis. */
19763
19764 while (token->type == CPP_COMMA)
19765 {
19766 cp_parameter_declarator *parmdecl;
19767 tree parm;
19768
19769 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19770 token = cp_lexer_peek_token (parser->lexer);
19771
19772 if (token->type == CPP_ELLIPSIS)
19773 {
19774 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19775 *ellipsisp = true;
19776 break;
19777 }
19778
19779 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19780 parm = grokdeclarator (parmdecl->declarator,
19781 &parmdecl->decl_specifiers,
9031d10b 19782 PARM, /*initialized=*/0,
7a4e126b 19783 /*attrlist=*/NULL);
19784
19785 chainon (params, build_tree_list (NULL_TREE, parm));
19786 token = cp_lexer_peek_token (parser->lexer);
19787 }
19788
19789 return params;
19790}
19791
19792/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19793
19794static void
19795cp_parser_objc_interstitial_code (cp_parser* parser)
19796{
19797 cp_token *token = cp_lexer_peek_token (parser->lexer);
19798
19799 /* If the next token is `extern' and the following token is a string
19800 literal, then we have a linkage specification. */
19801 if (token->keyword == RID_EXTERN
19802 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19803 cp_parser_linkage_specification (parser);
19804 /* Handle #pragma, if any. */
19805 else if (token->type == CPP_PRAGMA)
b75b98aa 19806 cp_parser_pragma (parser, pragma_external);
7a4e126b 19807 /* Allow stray semicolons. */
19808 else if (token->type == CPP_SEMICOLON)
19809 cp_lexer_consume_token (parser->lexer);
19810 /* Finally, try to parse a block-declaration, or a function-definition. */
19811 else
19812 cp_parser_block_declaration (parser, /*statement_p=*/false);
19813}
19814
19815/* Parse a method signature. */
19816
19817static tree
19818cp_parser_objc_method_signature (cp_parser* parser)
19819{
19820 tree rettype, kwdparms, optparms;
19821 bool ellipsis = false;
19822
19823 cp_parser_objc_method_type (parser);
19824 rettype = cp_parser_objc_typename (parser);
19825 kwdparms = cp_parser_objc_method_keyword_params (parser);
19826 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19827
19828 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19829}
19830
19831/* Pars an Objective-C method prototype list. */
19832
19833static void
19834cp_parser_objc_method_prototype_list (cp_parser* parser)
19835{
19836 cp_token *token = cp_lexer_peek_token (parser->lexer);
19837
19838 while (token->keyword != RID_AT_END)
19839 {
19840 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19841 {
19842 objc_add_method_declaration
19843 (cp_parser_objc_method_signature (parser));
19844 cp_parser_consume_semicolon_at_end_of_statement (parser);
19845 }
19846 else
19847 /* Allow for interspersed non-ObjC++ code. */
19848 cp_parser_objc_interstitial_code (parser);
19849
19850 token = cp_lexer_peek_token (parser->lexer);
19851 }
19852
19853 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19854 objc_finish_interface ();
19855}
19856
19857/* Parse an Objective-C method definition list. */
19858
19859static void
19860cp_parser_objc_method_definition_list (cp_parser* parser)
19861{
19862 cp_token *token = cp_lexer_peek_token (parser->lexer);
19863
19864 while (token->keyword != RID_AT_END)
19865 {
19866 tree meth;
19867
19868 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19869 {
19870 push_deferring_access_checks (dk_deferred);
19871 objc_start_method_definition
19872 (cp_parser_objc_method_signature (parser));
19873
19874 /* For historical reasons, we accept an optional semicolon. */
19875 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19876 cp_lexer_consume_token (parser->lexer);
19877
19878 perform_deferred_access_checks ();
19879 stop_deferring_access_checks ();
19880 meth = cp_parser_function_definition_after_declarator (parser,
19881 false);
19882 pop_deferring_access_checks ();
19883 objc_finish_method_definition (meth);
19884 }
19885 else
19886 /* Allow for interspersed non-ObjC++ code. */
19887 cp_parser_objc_interstitial_code (parser);
19888
19889 token = cp_lexer_peek_token (parser->lexer);
19890 }
19891
19892 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19893 objc_finish_implementation ();
19894}
19895
19896/* Parse Objective-C ivars. */
19897
19898static void
19899cp_parser_objc_class_ivars (cp_parser* parser)
19900{
19901 cp_token *token = cp_lexer_peek_token (parser->lexer);
19902
19903 if (token->type != CPP_OPEN_BRACE)
19904 return; /* No ivars specified. */
19905
19906 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19907 token = cp_lexer_peek_token (parser->lexer);
19908
19909 while (token->type != CPP_CLOSE_BRACE)
19910 {
19911 cp_decl_specifier_seq declspecs;
19912 int decl_class_or_enum_p;
19913 tree prefix_attributes;
19914
19915 cp_parser_objc_visibility_spec (parser);
19916
19917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19918 break;
19919
19920 cp_parser_decl_specifier_seq (parser,
19921 CP_PARSER_FLAGS_OPTIONAL,
19922 &declspecs,
19923 &decl_class_or_enum_p);
19924 prefix_attributes = declspecs.attributes;
19925 declspecs.attributes = NULL_TREE;
19926
19927 /* Keep going until we hit the `;' at the end of the
19928 declaration. */
19929 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19930 {
19931 tree width = NULL_TREE, attributes, first_attribute, decl;
19932 cp_declarator *declarator = NULL;
19933 int ctor_dtor_or_conv_p;
19934
19935 /* Check for a (possibly unnamed) bitfield declaration. */
19936 token = cp_lexer_peek_token (parser->lexer);
19937 if (token->type == CPP_COLON)
19938 goto eat_colon;
19939
19940 if (token->type == CPP_NAME
19941 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19942 == CPP_COLON))
19943 {
19944 /* Get the name of the bitfield. */
19945 declarator = make_id_declarator (NULL_TREE,
2366ed31 19946 cp_parser_identifier (parser),
19947 sfk_none);
7a4e126b 19948
19949 eat_colon:
19950 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19951 /* Get the width of the bitfield. */
19952 width
19953 = cp_parser_constant_expression (parser,
19954 /*allow_non_constant=*/false,
19955 NULL);
19956 }
19957 else
19958 {
19959 /* Parse the declarator. */
9031d10b 19960 declarator
7a4e126b 19961 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19962 &ctor_dtor_or_conv_p,
19963 /*parenthesized_p=*/NULL,
19964 /*member_p=*/false);
19965 }
19966
19967 /* Look for attributes that apply to the ivar. */
19968 attributes = cp_parser_attributes_opt (parser);
19969 /* Remember which attributes are prefix attributes and
19970 which are not. */
19971 first_attribute = attributes;
19972 /* Combine the attributes. */
19973 attributes = chainon (prefix_attributes, attributes);
19974
19975 if (width)
7a4e126b 19976 /* Create the bitfield declaration. */
f922e029 19977 decl = grokbitfield (declarator, &declspecs,
19978 width,
19979 attributes);
7a4e126b 19980 else
074ab442 19981 decl = grokfield (declarator, &declspecs,
d91303a6 19982 NULL_TREE, /*init_const_expr_p=*/false,
7a4e126b 19983 NULL_TREE, attributes);
9031d10b 19984
7a4e126b 19985 /* Add the instance variable. */
19986 objc_add_instance_variable (decl);
19987
19988 /* Reset PREFIX_ATTRIBUTES. */
19989 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19990 attributes = TREE_CHAIN (attributes);
19991 if (attributes)
19992 TREE_CHAIN (attributes) = NULL_TREE;
19993
19994 token = cp_lexer_peek_token (parser->lexer);
19995
19996 if (token->type == CPP_COMMA)
19997 {
19998 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19999 continue;
20000 }
20001 break;
20002 }
20003
20004 cp_parser_consume_semicolon_at_end_of_statement (parser);
20005 token = cp_lexer_peek_token (parser->lexer);
20006 }
20007
20008 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20009 /* For historical reasons, we accept an optional semicolon. */
20010 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20011 cp_lexer_consume_token (parser->lexer);
20012}
20013
20014/* Parse an Objective-C protocol declaration. */
20015
20016static void
20017cp_parser_objc_protocol_declaration (cp_parser* parser)
20018{
20019 tree proto, protorefs;
20020 cp_token *tok;
20021
20022 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20024 {
ad9ae192 20025 tok = cp_lexer_peek_token (parser->lexer);
20026 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
7a4e126b 20027 goto finish;
20028 }
20029
c78cbec8 20030 /* See if we have a forward declaration or a definition. */
7a4e126b 20031 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
9031d10b 20032
7a4e126b 20033 /* Try a forward declaration first. */
20034 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20035 {
20036 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
9031d10b 20037 finish:
7a4e126b 20038 cp_parser_consume_semicolon_at_end_of_statement (parser);
9031d10b 20039 }
7a4e126b 20040
20041 /* Ok, we got a full-fledged definition (or at least should). */
20042 else
20043 {
20044 proto = cp_parser_identifier (parser);
20045 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20046 objc_start_protocol (proto, protorefs);
20047 cp_parser_objc_method_prototype_list (parser);
20048 }
20049}
20050
20051/* Parse an Objective-C superclass or category. */
20052
20053static void
20054cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20055 tree *categ)
20056{
20057 cp_token *next = cp_lexer_peek_token (parser->lexer);
20058
20059 *super = *categ = NULL_TREE;
20060 if (next->type == CPP_COLON)
20061 {
20062 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20063 *super = cp_parser_identifier (parser);
20064 }
20065 else if (next->type == CPP_OPEN_PAREN)
20066 {
20067 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20068 *categ = cp_parser_identifier (parser);
640710cf 20069 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 20070 }
20071}
20072
20073/* Parse an Objective-C class interface. */
20074
20075static void
20076cp_parser_objc_class_interface (cp_parser* parser)
20077{
20078 tree name, super, categ, protos;
20079
20080 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20081 name = cp_parser_identifier (parser);
20082 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20083 protos = cp_parser_objc_protocol_refs_opt (parser);
20084
20085 /* We have either a class or a category on our hands. */
20086 if (categ)
20087 objc_start_category_interface (name, categ, protos);
20088 else
20089 {
20090 objc_start_class_interface (name, super, protos);
20091 /* Handle instance variable declarations, if any. */
20092 cp_parser_objc_class_ivars (parser);
20093 objc_continue_interface ();
20094 }
20095
20096 cp_parser_objc_method_prototype_list (parser);
20097}
20098
20099/* Parse an Objective-C class implementation. */
20100
20101static void
20102cp_parser_objc_class_implementation (cp_parser* parser)
20103{
20104 tree name, super, categ;
20105
20106 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20107 name = cp_parser_identifier (parser);
20108 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20109
20110 /* We have either a class or a category on our hands. */
20111 if (categ)
20112 objc_start_category_implementation (name, categ);
20113 else
20114 {
20115 objc_start_class_implementation (name, super);
20116 /* Handle instance variable declarations, if any. */
20117 cp_parser_objc_class_ivars (parser);
20118 objc_continue_implementation ();
20119 }
20120
20121 cp_parser_objc_method_definition_list (parser);
20122}
20123
20124/* Consume the @end token and finish off the implementation. */
20125
20126static void
20127cp_parser_objc_end_implementation (cp_parser* parser)
20128{
20129 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20130 objc_finish_implementation ();
20131}
20132
20133/* Parse an Objective-C declaration. */
20134
20135static void
20136cp_parser_objc_declaration (cp_parser* parser)
20137{
20138 /* Try to figure out what kind of declaration is present. */
20139 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20140
20141 switch (kwd->keyword)
20142 {
20143 case RID_AT_ALIAS:
20144 cp_parser_objc_alias_declaration (parser);
20145 break;
20146 case RID_AT_CLASS:
20147 cp_parser_objc_class_declaration (parser);
20148 break;
20149 case RID_AT_PROTOCOL:
20150 cp_parser_objc_protocol_declaration (parser);
20151 break;
20152 case RID_AT_INTERFACE:
20153 cp_parser_objc_class_interface (parser);
20154 break;
20155 case RID_AT_IMPLEMENTATION:
20156 cp_parser_objc_class_implementation (parser);
20157 break;
20158 case RID_AT_END:
20159 cp_parser_objc_end_implementation (parser);
20160 break;
20161 default:
ad9ae192 20162 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20163 &kwd->location, kwd->u.value);
7a4e126b 20164 cp_parser_skip_to_end_of_block_or_statement (parser);
20165 }
20166}
20167
20168/* Parse an Objective-C try-catch-finally statement.
20169
20170 objc-try-catch-finally-stmt:
20171 @try compound-statement objc-catch-clause-seq [opt]
20172 objc-finally-clause [opt]
20173
20174 objc-catch-clause-seq:
20175 objc-catch-clause objc-catch-clause-seq [opt]
20176
20177 objc-catch-clause:
20178 @catch ( exception-declaration ) compound-statement
20179
20180 objc-finally-clause
20181 @finally compound-statement
20182
20183 Returns NULL_TREE. */
20184
20185static tree
20186cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20187 location_t location;
20188 tree stmt;
20189
640710cf 20190 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
7a4e126b 20191 location = cp_lexer_peek_token (parser->lexer)->location;
20192 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20193 node, lest it get absorbed into the surrounding block. */
20194 stmt = push_stmt_list ();
20195 cp_parser_compound_statement (parser, NULL, false);
20196 objc_begin_try_stmt (location, pop_stmt_list (stmt));
9031d10b 20197
7a4e126b 20198 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20199 {
20200 cp_parameter_declarator *parmdecl;
20201 tree parm;
20202
20203 cp_lexer_consume_token (parser->lexer);
640710cf 20204 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7a4e126b 20205 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20206 parm = grokdeclarator (parmdecl->declarator,
20207 &parmdecl->decl_specifiers,
9031d10b 20208 PARM, /*initialized=*/0,
7a4e126b 20209 /*attrlist=*/NULL);
640710cf 20210 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 20211 objc_begin_catch_clause (parm);
20212 cp_parser_compound_statement (parser, NULL, false);
20213 objc_finish_catch_clause ();
20214 }
20215
20216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20217 {
20218 cp_lexer_consume_token (parser->lexer);
20219 location = cp_lexer_peek_token (parser->lexer)->location;
20220 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20221 node, lest it get absorbed into the surrounding block. */
20222 stmt = push_stmt_list ();
20223 cp_parser_compound_statement (parser, NULL, false);
20224 objc_build_finally_clause (location, pop_stmt_list (stmt));
20225 }
20226
20227 return objc_finish_try_stmt ();
20228}
20229
20230/* Parse an Objective-C synchronized statement.
20231
20232 objc-synchronized-stmt:
20233 @synchronized ( expression ) compound-statement
20234
20235 Returns NULL_TREE. */
20236
20237static tree
20238cp_parser_objc_synchronized_statement (cp_parser *parser) {
20239 location_t location;
20240 tree lock, stmt;
20241
640710cf 20242 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
7a4e126b 20243
20244 location = cp_lexer_peek_token (parser->lexer)->location;
640710cf 20245 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
98b326fd 20246 lock = cp_parser_expression (parser, false, NULL);
640710cf 20247 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7a4e126b 20248
20249 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20250 node, lest it get absorbed into the surrounding block. */
20251 stmt = push_stmt_list ();
20252 cp_parser_compound_statement (parser, NULL, false);
20253
20254 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20255}
20256
20257/* Parse an Objective-C throw statement.
20258
20259 objc-throw-stmt:
20260 @throw assignment-expression [opt] ;
20261
20262 Returns a constructed '@throw' statement. */
20263
20264static tree
20265cp_parser_objc_throw_statement (cp_parser *parser) {
20266 tree expr = NULL_TREE;
e60a6f7b 20267 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7a4e126b 20268
640710cf 20269 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
7a4e126b 20270
20271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 20272 expr = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 20273
20274 cp_parser_consume_semicolon_at_end_of_statement (parser);
20275
e60a6f7b 20276 return objc_build_throw_stmt (loc, expr);
7a4e126b 20277}
20278
20279/* Parse an Objective-C statement. */
20280
20281static tree
20282cp_parser_objc_statement (cp_parser * parser) {
20283 /* Try to figure out what kind of declaration is present. */
20284 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20285
20286 switch (kwd->keyword)
20287 {
20288 case RID_AT_TRY:
20289 return cp_parser_objc_try_catch_finally_statement (parser);
20290 case RID_AT_SYNCHRONIZED:
20291 return cp_parser_objc_synchronized_statement (parser);
20292 case RID_AT_THROW:
20293 return cp_parser_objc_throw_statement (parser);
20294 default:
ad9ae192 20295 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20296 &kwd->location, kwd->u.value);
7a4e126b 20297 cp_parser_skip_to_end_of_block_or_statement (parser);
20298 }
20299
20300 return error_mark_node;
20301}
8487df40 20302\f
20303/* OpenMP 2.5 parsing routines. */
20304
8487df40 20305/* Returns name of the next clause.
20306 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20307 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20308 returned and the token is consumed. */
20309
20310static pragma_omp_clause
20311cp_parser_omp_clause_name (cp_parser *parser)
20312{
20313 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20314
20315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20316 result = PRAGMA_OMP_CLAUSE_IF;
20317 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20318 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20319 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20320 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20321 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20322 {
3369eb76 20323 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 20324 const char *p = IDENTIFIER_POINTER (id);
20325
20326 switch (p[0])
20327 {
20328 case 'c':
fd6481cf 20329 if (!strcmp ("collapse", p))
20330 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20331 else if (!strcmp ("copyin", p))
8487df40 20332 result = PRAGMA_OMP_CLAUSE_COPYIN;
074ab442 20333 else if (!strcmp ("copyprivate", p))
8487df40 20334 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20335 break;
20336 case 'f':
20337 if (!strcmp ("firstprivate", p))
20338 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20339 break;
20340 case 'l':
20341 if (!strcmp ("lastprivate", p))
20342 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20343 break;
20344 case 'n':
20345 if (!strcmp ("nowait", p))
20346 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20347 else if (!strcmp ("num_threads", p))
20348 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20349 break;
20350 case 'o':
20351 if (!strcmp ("ordered", p))
20352 result = PRAGMA_OMP_CLAUSE_ORDERED;
20353 break;
20354 case 'r':
20355 if (!strcmp ("reduction", p))
20356 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20357 break;
20358 case 's':
20359 if (!strcmp ("schedule", p))
20360 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20361 else if (!strcmp ("shared", p))
20362 result = PRAGMA_OMP_CLAUSE_SHARED;
20363 break;
fd6481cf 20364 case 'u':
20365 if (!strcmp ("untied", p))
20366 result = PRAGMA_OMP_CLAUSE_UNTIED;
20367 break;
8487df40 20368 }
20369 }
0a3b29ad 20370
8487df40 20371 if (result != PRAGMA_OMP_CLAUSE_NONE)
20372 cp_lexer_consume_token (parser->lexer);
0a3b29ad 20373
8487df40 20374 return result;
20375}
b75b98aa 20376
8487df40 20377/* Validate that a clause of the given type does not already exist. */
b75b98aa 20378
20379static void
590c3166 20380check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
ad9ae192 20381 const char *name, location_t location)
b75b98aa 20382{
8487df40 20383 tree c;
b75b98aa 20384
8487df40 20385 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20386 if (OMP_CLAUSE_CODE (c) == code)
20387 {
ad9ae192 20388 error ("%Htoo many %qs clauses", &location, name);
8487df40 20389 break;
20390 }
20391}
b75b98aa 20392
8487df40 20393/* OpenMP 2.5:
20394 variable-list:
20395 identifier
20396 variable-list , identifier
20397
20398 In addition, we match a closing parenthesis. An opening parenthesis
20399 will have been consumed by the caller.
20400
20401 If KIND is nonzero, create the appropriate node and install the decl
20402 in OMP_CLAUSE_DECL and add the node to the head of the list.
20403
20404 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20405 return the list created. */
20406
20407static tree
20408cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20409 tree list)
20410{
ad9ae192 20411 cp_token *token;
8487df40 20412 while (1)
b75b98aa 20413 {
8487df40 20414 tree name, decl;
b75b98aa 20415
ad9ae192 20416 token = cp_lexer_peek_token (parser->lexer);
8487df40 20417 name = cp_parser_id_expression (parser, /*template_p=*/false,
20418 /*check_dependency_p=*/true,
20419 /*template_p=*/NULL,
197c9df7 20420 /*declarator_p=*/false,
130bb1d4 20421 /*optional_p=*/false);
8487df40 20422 if (name == error_mark_node)
20423 goto skip_comma;
20424
ad9ae192 20425 decl = cp_parser_lookup_name_simple (parser, name, token->location);
8487df40 20426 if (decl == error_mark_node)
ad9ae192 20427 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
8487df40 20428 else if (kind != 0)
20429 {
e60a6f7b 20430 tree u = build_omp_clause (token->location, kind);
8487df40 20431 OMP_CLAUSE_DECL (u) = decl;
20432 OMP_CLAUSE_CHAIN (u) = list;
20433 list = u;
20434 }
20435 else
20436 list = tree_cons (decl, NULL_TREE, list);
20437
20438 get_comma:
20439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20440 break;
20441 cp_lexer_consume_token (parser->lexer);
20442 }
20443
640710cf 20444 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 20445 {
20446 int ending;
20447
20448 /* Try to resync to an unnested comma. Copied from
20449 cp_parser_parenthesized_expression_list. */
20450 skip_comma:
20451 ending = cp_parser_skip_to_closing_parenthesis (parser,
20452 /*recovering=*/true,
20453 /*or_comma=*/true,
20454 /*consume_paren=*/true);
20455 if (ending < 0)
20456 goto get_comma;
20457 }
20458
20459 return list;
20460}
20461
20462/* Similarly, but expect leading and trailing parenthesis. This is a very
20463 common case for omp clauses. */
20464
20465static tree
20466cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20467{
640710cf 20468 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20469 return cp_parser_omp_var_list_no_open (parser, kind, list);
20470 return list;
20471}
20472
fd6481cf 20473/* OpenMP 3.0:
20474 collapse ( constant-expression ) */
20475
20476static tree
ad9ae192 20477cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
fd6481cf 20478{
20479 tree c, num;
20480 location_t loc;
20481 HOST_WIDE_INT n;
20482
20483 loc = cp_lexer_peek_token (parser->lexer)->location;
20484 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20485 return list;
20486
20487 num = cp_parser_constant_expression (parser, false, NULL);
20488
20489 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20491 /*or_comma=*/false,
20492 /*consume_paren=*/true);
20493
20494 if (num == error_mark_node)
20495 return list;
20496 num = fold_non_dependent_expr (num);
20497 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20498 || !host_integerp (num, 0)
20499 || (n = tree_low_cst (num, 0)) <= 0
20500 || (int) n != n)
20501 {
ad9ae192 20502 error ("%Hcollapse argument needs positive constant integer expression",
20503 &loc);
fd6481cf 20504 return list;
20505 }
20506
ad9ae192 20507 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
e60a6f7b 20508 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
fd6481cf 20509 OMP_CLAUSE_CHAIN (c) = list;
20510 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20511
20512 return c;
20513}
20514
8487df40 20515/* OpenMP 2.5:
20516 default ( shared | none ) */
20517
20518static tree
ad9ae192 20519cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
8487df40 20520{
20521 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20522 tree c;
20523
640710cf 20524 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20525 return list;
20526 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20527 {
3369eb76 20528 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 20529 const char *p = IDENTIFIER_POINTER (id);
20530
20531 switch (p[0])
20532 {
20533 case 'n':
20534 if (strcmp ("none", p) != 0)
20535 goto invalid_kind;
20536 kind = OMP_CLAUSE_DEFAULT_NONE;
20537 break;
20538
20539 case 's':
20540 if (strcmp ("shared", p) != 0)
20541 goto invalid_kind;
20542 kind = OMP_CLAUSE_DEFAULT_SHARED;
20543 break;
20544
20545 default:
20546 goto invalid_kind;
20547 }
20548
20549 cp_lexer_consume_token (parser->lexer);
b75b98aa 20550 }
20551 else
8487df40 20552 {
20553 invalid_kind:
20554 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20555 }
b75b98aa 20556
640710cf 20557 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 20558 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20559 /*or_comma=*/false,
20560 /*consume_paren=*/true);
074ab442 20561
8487df40 20562 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20563 return list;
b75b98aa 20564
ad9ae192 20565 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
e60a6f7b 20566 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
8487df40 20567 OMP_CLAUSE_CHAIN (c) = list;
20568 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
b75b98aa 20569
8487df40 20570 return c;
b75b98aa 20571}
20572
8487df40 20573/* OpenMP 2.5:
20574 if ( expression ) */
b75b98aa 20575
8487df40 20576static tree
ad9ae192 20577cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
b75b98aa 20578{
8487df40 20579 tree t, c;
b75b98aa 20580
640710cf 20581 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20582 return list;
b75b98aa 20583
8487df40 20584 t = cp_parser_condition (parser);
20585
20586 if (t == error_mark_node
640710cf 20587 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 20588 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20589 /*or_comma=*/false,
20590 /*consume_paren=*/true);
20591
ad9ae192 20592 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
8487df40 20593
e60a6f7b 20594 c = build_omp_clause (location, OMP_CLAUSE_IF);
8487df40 20595 OMP_CLAUSE_IF_EXPR (c) = t;
20596 OMP_CLAUSE_CHAIN (c) = list;
20597
20598 return c;
20599}
20600
20601/* OpenMP 2.5:
20602 nowait */
20603
20604static tree
ad9ae192 20605cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20606 tree list, location_t location)
8487df40 20607{
20608 tree c;
20609
ad9ae192 20610 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
8487df40 20611
e60a6f7b 20612 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
8487df40 20613 OMP_CLAUSE_CHAIN (c) = list;
20614 return c;
20615}
20616
20617/* OpenMP 2.5:
20618 num_threads ( expression ) */
20619
20620static tree
ad9ae192 20621cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20622 location_t location)
8487df40 20623{
20624 tree t, c;
20625
640710cf 20626 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20627 return list;
20628
98b326fd 20629 t = cp_parser_expression (parser, false, NULL);
8487df40 20630
20631 if (t == error_mark_node
640710cf 20632 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 20633 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20634 /*or_comma=*/false,
20635 /*consume_paren=*/true);
20636
ad9ae192 20637 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20638 "num_threads", location);
8487df40 20639
e60a6f7b 20640 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
8487df40 20641 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20642 OMP_CLAUSE_CHAIN (c) = list;
20643
20644 return c;
20645}
20646
20647/* OpenMP 2.5:
20648 ordered */
20649
20650static tree
ad9ae192 20651cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20652 tree list, location_t location)
8487df40 20653{
20654 tree c;
20655
ad9ae192 20656 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20657 "ordered", location);
8487df40 20658
e60a6f7b 20659 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
8487df40 20660 OMP_CLAUSE_CHAIN (c) = list;
20661 return c;
20662}
20663
20664/* OpenMP 2.5:
20665 reduction ( reduction-operator : variable-list )
20666
20667 reduction-operator:
20668 One of: + * - & ^ | && || */
20669
20670static tree
20671cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20672{
20673 enum tree_code code;
20674 tree nlist, c;
20675
640710cf 20676 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20677 return list;
20678
20679 switch (cp_lexer_peek_token (parser->lexer)->type)
b75b98aa 20680 {
8487df40 20681 case CPP_PLUS:
20682 code = PLUS_EXPR;
20683 break;
20684 case CPP_MULT:
20685 code = MULT_EXPR;
20686 break;
20687 case CPP_MINUS:
20688 code = MINUS_EXPR;
20689 break;
20690 case CPP_AND:
20691 code = BIT_AND_EXPR;
20692 break;
20693 case CPP_XOR:
20694 code = BIT_XOR_EXPR;
20695 break;
20696 case CPP_OR:
20697 code = BIT_IOR_EXPR;
20698 break;
20699 case CPP_AND_AND:
20700 code = TRUTH_ANDIF_EXPR;
20701 break;
20702 case CPP_OR_OR:
20703 code = TRUTH_ORIF_EXPR;
b75b98aa 20704 break;
b75b98aa 20705 default:
361cc318 20706 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20707 "%<|%>, %<&&%>, or %<||%>");
8487df40 20708 resync_fail:
20709 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20710 /*or_comma=*/false,
20711 /*consume_paren=*/true);
20712 return list;
20713 }
20714 cp_lexer_consume_token (parser->lexer);
20715
640710cf 20716 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
8487df40 20717 goto resync_fail;
20718
20719 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20720 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20721 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20722
20723 return nlist;
20724}
20725
20726/* OpenMP 2.5:
20727 schedule ( schedule-kind )
20728 schedule ( schedule-kind , expression )
20729
20730 schedule-kind:
fd6481cf 20731 static | dynamic | guided | runtime | auto */
8487df40 20732
20733static tree
ad9ae192 20734cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
8487df40 20735{
20736 tree c, t;
20737
361cc318 20738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8487df40 20739 return list;
20740
e60a6f7b 20741 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
8487df40 20742
20743 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20744 {
3369eb76 20745 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 20746 const char *p = IDENTIFIER_POINTER (id);
20747
20748 switch (p[0])
20749 {
20750 case 'd':
20751 if (strcmp ("dynamic", p) != 0)
20752 goto invalid_kind;
20753 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20754 break;
20755
074ab442 20756 case 'g':
8487df40 20757 if (strcmp ("guided", p) != 0)
20758 goto invalid_kind;
20759 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20760 break;
20761
20762 case 'r':
20763 if (strcmp ("runtime", p) != 0)
20764 goto invalid_kind;
20765 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20766 break;
20767
20768 default:
20769 goto invalid_kind;
20770 }
20771 }
20772 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20773 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
fd6481cf 20774 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20775 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8487df40 20776 else
20777 goto invalid_kind;
20778 cp_lexer_consume_token (parser->lexer);
20779
20780 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20781 {
ad9ae192 20782 cp_token *token;
8487df40 20783 cp_lexer_consume_token (parser->lexer);
20784
ad9ae192 20785 token = cp_lexer_peek_token (parser->lexer);
98b326fd 20786 t = cp_parser_assignment_expression (parser, false, NULL);
8487df40 20787
20788 if (t == error_mark_node)
20789 goto resync_fail;
20790 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
ad9ae192 20791 error ("%Hschedule %<runtime%> does not take "
20792 "a %<chunk_size%> parameter", &token->location);
fd6481cf 20793 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
ad9ae192 20794 error ("%Hschedule %<auto%> does not take "
20795 "a %<chunk_size%> parameter", &token->location);
8487df40 20796 else
20797 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20798
640710cf 20799 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 20800 goto resync_fail;
20801 }
640710cf 20802 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
8487df40 20803 goto resync_fail;
20804
ad9ae192 20805 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
8487df40 20806 OMP_CLAUSE_CHAIN (c) = list;
20807 return c;
20808
20809 invalid_kind:
20810 cp_parser_error (parser, "invalid schedule kind");
20811 resync_fail:
20812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20813 /*or_comma=*/false,
20814 /*consume_paren=*/true);
20815 return list;
20816}
20817
fd6481cf 20818/* OpenMP 3.0:
20819 untied */
20820
20821static tree
ad9ae192 20822cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20823 tree list, location_t location)
fd6481cf 20824{
20825 tree c;
20826
ad9ae192 20827 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
fd6481cf 20828
e60a6f7b 20829 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
fd6481cf 20830 OMP_CLAUSE_CHAIN (c) = list;
20831 return c;
20832}
20833
8487df40 20834/* Parse all OpenMP clauses. The set clauses allowed by the directive
20835 is a bitmask in MASK. Return the list of clauses found; the result
20836 of clause default goes in *pdefault. */
20837
20838static tree
20839cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20840 const char *where, cp_token *pragma_tok)
20841{
20842 tree clauses = NULL;
77c8bed2 20843 bool first = true;
ad9ae192 20844 cp_token *token = NULL;
8487df40 20845
20846 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20847 {
77c8bed2 20848 pragma_omp_clause c_kind;
8487df40 20849 const char *c_name;
20850 tree prev = clauses;
20851
77c8bed2 20852 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20853 cp_lexer_consume_token (parser->lexer);
20854
ad9ae192 20855 token = cp_lexer_peek_token (parser->lexer);
77c8bed2 20856 c_kind = cp_parser_omp_clause_name (parser);
20857 first = false;
20858
8487df40 20859 switch (c_kind)
20860 {
fd6481cf 20861 case PRAGMA_OMP_CLAUSE_COLLAPSE:
ad9ae192 20862 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20863 token->location);
fd6481cf 20864 c_name = "collapse";
20865 break;
8487df40 20866 case PRAGMA_OMP_CLAUSE_COPYIN:
20867 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20868 c_name = "copyin";
20869 break;
20870 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20871 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20872 clauses);
20873 c_name = "copyprivate";
20874 break;
20875 case PRAGMA_OMP_CLAUSE_DEFAULT:
ad9ae192 20876 clauses = cp_parser_omp_clause_default (parser, clauses,
20877 token->location);
8487df40 20878 c_name = "default";
20879 break;
20880 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20881 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20882 clauses);
20883 c_name = "firstprivate";
20884 break;
20885 case PRAGMA_OMP_CLAUSE_IF:
ad9ae192 20886 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
8487df40 20887 c_name = "if";
20888 break;
20889 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20890 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20891 clauses);
20892 c_name = "lastprivate";
20893 break;
20894 case PRAGMA_OMP_CLAUSE_NOWAIT:
ad9ae192 20895 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
8487df40 20896 c_name = "nowait";
20897 break;
20898 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
ad9ae192 20899 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20900 token->location);
8487df40 20901 c_name = "num_threads";
20902 break;
20903 case PRAGMA_OMP_CLAUSE_ORDERED:
ad9ae192 20904 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20905 token->location);
8487df40 20906 c_name = "ordered";
20907 break;
20908 case PRAGMA_OMP_CLAUSE_PRIVATE:
20909 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20910 clauses);
20911 c_name = "private";
20912 break;
20913 case PRAGMA_OMP_CLAUSE_REDUCTION:
20914 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20915 c_name = "reduction";
20916 break;
20917 case PRAGMA_OMP_CLAUSE_SCHEDULE:
ad9ae192 20918 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20919 token->location);
8487df40 20920 c_name = "schedule";
20921 break;
20922 case PRAGMA_OMP_CLAUSE_SHARED:
20923 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20924 clauses);
20925 c_name = "shared";
20926 break;
fd6481cf 20927 case PRAGMA_OMP_CLAUSE_UNTIED:
ad9ae192 20928 clauses = cp_parser_omp_clause_untied (parser, clauses,
20929 token->location);
fd6481cf 20930 c_name = "nowait";
20931 break;
8487df40 20932 default:
20933 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20934 goto saw_error;
20935 }
20936
20937 if (((mask >> c_kind) & 1) == 0)
20938 {
20939 /* Remove the invalid clause(s) from the list to avoid
20940 confusing the rest of the compiler. */
20941 clauses = prev;
ad9ae192 20942 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
8487df40 20943 }
20944 }
20945 saw_error:
20946 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20947 return finish_omp_clauses (clauses);
20948}
20949
20950/* OpenMP 2.5:
20951 structured-block:
20952 statement
20953
20954 In practice, we're also interested in adding the statement to an
20955 outer node. So it is convenient if we work around the fact that
20956 cp_parser_statement calls add_stmt. */
20957
20958static unsigned
20959cp_parser_begin_omp_structured_block (cp_parser *parser)
20960{
20961 unsigned save = parser->in_statement;
20962
20963 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20964 This preserves the "not within loop or switch" style error messages
20965 for nonsense cases like
20966 void foo() {
20967 #pragma omp single
20968 break;
20969 }
20970 */
20971 if (parser->in_statement)
20972 parser->in_statement = IN_OMP_BLOCK;
20973
20974 return save;
20975}
20976
20977static void
20978cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20979{
20980 parser->in_statement = save;
20981}
20982
20983static tree
20984cp_parser_omp_structured_block (cp_parser *parser)
20985{
20986 tree stmt = begin_omp_structured_block ();
20987 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20988
e534436e 20989 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 20990
20991 cp_parser_end_omp_structured_block (parser, save);
20992 return finish_omp_structured_block (stmt);
20993}
20994
20995/* OpenMP 2.5:
20996 # pragma omp atomic new-line
20997 expression-stmt
20998
20999 expression-stmt:
21000 x binop= expr | x++ | ++x | x-- | --x
21001 binop:
21002 +, *, -, /, &, ^, |, <<, >>
21003
21004 where x is an lvalue expression with scalar type. */
21005
21006static void
21007cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21008{
21009 tree lhs, rhs;
21010 enum tree_code code;
21011
21012 cp_parser_require_pragma_eol (parser, pragma_tok);
21013
21014 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 21015 /*cast_p=*/false, NULL);
8487df40 21016 switch (TREE_CODE (lhs))
21017 {
21018 case ERROR_MARK:
21019 goto saw_error;
21020
21021 case PREINCREMENT_EXPR:
21022 case POSTINCREMENT_EXPR:
21023 lhs = TREE_OPERAND (lhs, 0);
21024 code = PLUS_EXPR;
21025 rhs = integer_one_node;
21026 break;
21027
21028 case PREDECREMENT_EXPR:
21029 case POSTDECREMENT_EXPR:
21030 lhs = TREE_OPERAND (lhs, 0);
21031 code = MINUS_EXPR;
21032 rhs = integer_one_node;
21033 break;
21034
21035 default:
21036 switch (cp_lexer_peek_token (parser->lexer)->type)
21037 {
21038 case CPP_MULT_EQ:
21039 code = MULT_EXPR;
21040 break;
21041 case CPP_DIV_EQ:
21042 code = TRUNC_DIV_EXPR;
21043 break;
21044 case CPP_PLUS_EQ:
21045 code = PLUS_EXPR;
21046 break;
21047 case CPP_MINUS_EQ:
21048 code = MINUS_EXPR;
21049 break;
21050 case CPP_LSHIFT_EQ:
21051 code = LSHIFT_EXPR;
21052 break;
21053 case CPP_RSHIFT_EQ:
21054 code = RSHIFT_EXPR;
21055 break;
21056 case CPP_AND_EQ:
21057 code = BIT_AND_EXPR;
21058 break;
21059 case CPP_OR_EQ:
21060 code = BIT_IOR_EXPR;
21061 break;
21062 case CPP_XOR_EQ:
21063 code = BIT_XOR_EXPR;
21064 break;
21065 default:
21066 cp_parser_error (parser,
21067 "invalid operator for %<#pragma omp atomic%>");
21068 goto saw_error;
21069 }
21070 cp_lexer_consume_token (parser->lexer);
21071
98b326fd 21072 rhs = cp_parser_expression (parser, false, NULL);
8487df40 21073 if (rhs == error_mark_node)
21074 goto saw_error;
21075 break;
21076 }
21077 finish_omp_atomic (code, lhs, rhs);
21078 cp_parser_consume_semicolon_at_end_of_statement (parser);
21079 return;
21080
21081 saw_error:
21082 cp_parser_skip_to_end_of_block_or_statement (parser);
21083}
21084
21085
21086/* OpenMP 2.5:
074ab442 21087 # pragma omp barrier new-line */
8487df40 21088
21089static void
21090cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21091{
21092 cp_parser_require_pragma_eol (parser, pragma_tok);
21093 finish_omp_barrier ();
21094}
21095
21096/* OpenMP 2.5:
21097 # pragma omp critical [(name)] new-line
074ab442 21098 structured-block */
8487df40 21099
21100static tree
21101cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21102{
21103 tree stmt, name = NULL;
21104
21105 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21106 {
21107 cp_lexer_consume_token (parser->lexer);
21108
21109 name = cp_parser_identifier (parser);
074ab442 21110
8487df40 21111 if (name == error_mark_node
640710cf 21112 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8487df40 21113 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21114 /*or_comma=*/false,
21115 /*consume_paren=*/true);
21116 if (name == error_mark_node)
21117 name = NULL;
21118 }
21119 cp_parser_require_pragma_eol (parser, pragma_tok);
21120
21121 stmt = cp_parser_omp_structured_block (parser);
e60a6f7b 21122 return c_finish_omp_critical (input_location, stmt, name);
8487df40 21123}
21124
21125/* OpenMP 2.5:
21126 # pragma omp flush flush-vars[opt] new-line
21127
21128 flush-vars:
21129 ( variable-list ) */
21130
21131static void
21132cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21133{
21134 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a52f99a9 21135 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 21136 cp_parser_require_pragma_eol (parser, pragma_tok);
21137
21138 finish_omp_flush ();
21139}
21140
fd6481cf 21141/* Helper function, to parse omp for increment expression. */
8487df40 21142
21143static tree
fd6481cf 21144cp_parser_omp_for_cond (cp_parser *parser, tree decl)
8487df40 21145{
4390875c 21146 tree cond = cp_parser_binary_expression (parser, false, true,
21147 PREC_NOT_OPERATOR, NULL);
21148 bool overloaded_p;
8487df40 21149
4390875c 21150 if (cond == error_mark_node
21151 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8487df40 21152 {
fd6481cf 21153 cp_parser_skip_to_end_of_statement (parser);
21154 return error_mark_node;
8487df40 21155 }
8487df40 21156
4390875c 21157 switch (TREE_CODE (cond))
fd6481cf 21158 {
fd6481cf 21159 case GT_EXPR:
21160 case GE_EXPR:
4390875c 21161 case LT_EXPR:
21162 case LE_EXPR:
fd6481cf 21163 break;
21164 default:
fd6481cf 21165 return error_mark_node;
21166 }
21167
4390875c 21168 /* If decl is an iterator, preserve LHS and RHS of the relational
21169 expr until finish_omp_for. */
21170 if (decl
21171 && (type_dependent_expression_p (decl)
21172 || CLASS_TYPE_P (TREE_TYPE (decl))))
21173 return cond;
8487df40 21174
4390875c 21175 return build_x_binary_op (TREE_CODE (cond),
21176 TREE_OPERAND (cond, 0), ERROR_MARK,
21177 TREE_OPERAND (cond, 1), ERROR_MARK,
21178 &overloaded_p, tf_warning_or_error);
fd6481cf 21179}
8487df40 21180
fd6481cf 21181/* Helper function, to parse omp for increment expression. */
21182
21183static tree
21184cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21185{
21186 cp_token *token = cp_lexer_peek_token (parser->lexer);
21187 enum tree_code op;
21188 tree lhs, rhs;
21189 cp_id_kind idk;
21190 bool decl_first;
21191
21192 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21193 {
21194 op = (token->type == CPP_PLUS_PLUS
21195 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21196 cp_lexer_consume_token (parser->lexer);
98b326fd 21197 lhs = cp_parser_cast_expression (parser, false, false, NULL);
fd6481cf 21198 if (lhs != decl)
21199 return error_mark_node;
21200 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21201 }
21202
21203 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21204 if (lhs != decl)
21205 return error_mark_node;
21206
21207 token = cp_lexer_peek_token (parser->lexer);
21208 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21209 {
21210 op = (token->type == CPP_PLUS_PLUS
21211 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21212 cp_lexer_consume_token (parser->lexer);
21213 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21214 }
21215
21216 op = cp_parser_assignment_operator_opt (parser);
21217 if (op == ERROR_MARK)
21218 return error_mark_node;
21219
21220 if (op != NOP_EXPR)
21221 {
98b326fd 21222 rhs = cp_parser_assignment_expression (parser, false, NULL);
fd6481cf 21223 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21224 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21225 }
21226
4390875c 21227 lhs = cp_parser_binary_expression (parser, false, false,
98b326fd 21228 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 21229 token = cp_lexer_peek_token (parser->lexer);
21230 decl_first = lhs == decl;
21231 if (decl_first)
21232 lhs = NULL_TREE;
21233 if (token->type != CPP_PLUS
21234 && token->type != CPP_MINUS)
21235 return error_mark_node;
21236
21237 do
21238 {
21239 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21240 cp_lexer_consume_token (parser->lexer);
4390875c 21241 rhs = cp_parser_binary_expression (parser, false, false,
98b326fd 21242 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 21243 token = cp_lexer_peek_token (parser->lexer);
21244 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
8487df40 21245 {
fd6481cf 21246 if (lhs == NULL_TREE)
21247 {
21248 if (op == PLUS_EXPR)
21249 lhs = rhs;
21250 else
21251 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21252 }
21253 else
21254 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21255 NULL, tf_warning_or_error);
21256 }
21257 }
21258 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
8487df40 21259
fd6481cf 21260 if (!decl_first)
21261 {
21262 if (rhs != decl || op == MINUS_EXPR)
21263 return error_mark_node;
21264 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21265 }
21266 else
21267 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21268
21269 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21270}
21271
bde357c8 21272/* Parse the restricted form of the for statement allowed by OpenMP. */
fd6481cf 21273
21274static tree
21275cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21276{
21277 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21278 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21279 tree this_pre_body, cl;
21280 location_t loc_first;
21281 bool collapse_err = false;
21282 int i, collapse = 1, nbraces = 0;
21283
21284 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21285 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21286 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21287
21288 gcc_assert (collapse >= 1);
21289
21290 declv = make_tree_vec (collapse);
21291 initv = make_tree_vec (collapse);
21292 condv = make_tree_vec (collapse);
21293 incrv = make_tree_vec (collapse);
21294
21295 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21296
21297 for (i = 0; i < collapse; i++)
21298 {
21299 int bracecount = 0;
21300 bool add_private_clause = false;
21301 location_t loc;
21302
21303 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21304 {
21305 cp_parser_error (parser, "for statement expected");
21306 return NULL;
21307 }
21308 loc = cp_lexer_consume_token (parser->lexer)->location;
21309
21310 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21311 return NULL;
21312
21313 init = decl = real_decl = NULL;
21314 this_pre_body = push_stmt_list ();
21315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21316 {
507c9a59 21317 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21318
21319 init-expr:
21320 var = lb
21321 integer-type var = lb
21322 random-access-iterator-type var = lb
21323 pointer-type var = lb
21324 */
fd6481cf 21325 cp_decl_specifier_seq type_specifiers;
21326
21327 /* First, try to parse as an initialized declaration. See
21328 cp_parser_condition, from whence the bulk of this is copied. */
21329
21330 cp_parser_parse_tentatively (parser);
21331 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21332 &type_specifiers);
507c9a59 21333 if (cp_parser_parse_definitely (parser))
8487df40 21334 {
507c9a59 21335 /* If parsing a type specifier seq succeeded, then this
21336 MUST be a initialized declaration. */
fd6481cf 21337 tree asm_specification, attributes;
21338 cp_declarator *declarator;
21339
21340 declarator = cp_parser_declarator (parser,
21341 CP_PARSER_DECLARATOR_NAMED,
21342 /*ctor_dtor_or_conv_p=*/NULL,
21343 /*parenthesized_p=*/NULL,
21344 /*member_p=*/false);
21345 attributes = cp_parser_attributes_opt (parser);
21346 asm_specification = cp_parser_asm_specification_opt (parser);
21347
507c9a59 21348 if (declarator == cp_error_declarator)
21349 cp_parser_skip_to_end_of_statement (parser);
21350
21351 else
fd6481cf 21352 {
304ac225 21353 tree pushed_scope, auto_node;
fd6481cf 21354
21355 decl = start_decl (declarator, &type_specifiers,
304ac225 21356 SD_INITIALIZED, attributes,
fd6481cf 21357 /*prefix_attributes=*/NULL_TREE,
21358 &pushed_scope);
21359
304ac225 21360 auto_node = type_uses_auto (TREE_TYPE (decl));
507c9a59 21361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21362 {
21363 if (cp_lexer_next_token_is (parser->lexer,
21364 CPP_OPEN_PAREN))
21365 error ("parenthesized initialization is not allowed in "
21366 "OpenMP %<for%> loop");
21367 else
21368 /* Trigger an error. */
21369 cp_parser_require (parser, CPP_EQ, "%<=%>");
21370
21371 init = error_mark_node;
21372 cp_parser_skip_to_end_of_statement (parser);
21373 }
21374 else if (CLASS_TYPE_P (TREE_TYPE (decl))
304ac225 21375 || type_dependent_expression_p (decl)
21376 || auto_node)
fd6481cf 21377 {
f82f1250 21378 bool is_direct_init, is_non_constant_init;
fd6481cf 21379
21380 init = cp_parser_initializer (parser,
f82f1250 21381 &is_direct_init,
fd6481cf 21382 &is_non_constant_init);
21383
3986b463 21384 if (auto_node && describable_type (init))
304ac225 21385 {
21386 TREE_TYPE (decl)
21387 = do_auto_deduction (TREE_TYPE (decl), init,
21388 auto_node);
21389
21390 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21391 && !type_dependent_expression_p (decl))
21392 goto non_class;
21393 }
21394
fd6481cf 21395 cp_finish_decl (decl, init, !is_non_constant_init,
21396 asm_specification,
21397 LOOKUP_ONLYCONVERTING);
21398 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21399 {
21400 for_block
21401 = tree_cons (NULL, this_pre_body, for_block);
21402 init = NULL_TREE;
21403 }
21404 else
21405 init = pop_stmt_list (this_pre_body);
21406 this_pre_body = NULL_TREE;
21407 }
21408 else
21409 {
507c9a59 21410 /* Consume '='. */
21411 cp_lexer_consume_token (parser->lexer);
98b326fd 21412 init = cp_parser_assignment_expression (parser, false, NULL);
8487df40 21413
304ac225 21414 non_class:
fd6481cf 21415 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21416 init = error_mark_node;
21417 else
21418 cp_finish_decl (decl, NULL_TREE,
21419 /*init_const_expr_p=*/false,
21420 asm_specification,
21421 LOOKUP_ONLYCONVERTING);
21422 }
8487df40 21423
fd6481cf 21424 if (pushed_scope)
21425 pop_scope (pushed_scope);
21426 }
21427 }
507c9a59 21428 else
fd6481cf 21429 {
21430 cp_id_kind idk;
507c9a59 21431 /* If parsing a type specifier sequence failed, then
21432 this MUST be a simple expression. */
fd6481cf 21433 cp_parser_parse_tentatively (parser);
21434 decl = cp_parser_primary_expression (parser, false, false,
21435 false, &idk);
21436 if (!cp_parser_error_occurred (parser)
21437 && decl
21438 && DECL_P (decl)
21439 && CLASS_TYPE_P (TREE_TYPE (decl)))
21440 {
21441 tree rhs;
21442
21443 cp_parser_parse_definitely (parser);
21444 cp_parser_require (parser, CPP_EQ, "%<=%>");
98b326fd 21445 rhs = cp_parser_assignment_expression (parser, false, NULL);
fd6481cf 21446 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21447 rhs,
21448 tf_warning_or_error));
21449 add_private_clause = true;
21450 }
a46a7e42 21451 else
fd6481cf 21452 {
21453 decl = NULL;
21454 cp_parser_abort_tentative_parse (parser);
98b326fd 21455 init = cp_parser_expression (parser, false, NULL);
fd6481cf 21456 if (init)
21457 {
21458 if (TREE_CODE (init) == MODIFY_EXPR
21459 || TREE_CODE (init) == MODOP_EXPR)
21460 real_decl = TREE_OPERAND (init, 0);
21461 }
21462 }
21463 }
21464 }
21465 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21466 if (this_pre_body)
21467 {
21468 this_pre_body = pop_stmt_list (this_pre_body);
21469 if (pre_body)
21470 {
21471 tree t = pre_body;
21472 pre_body = push_stmt_list ();
21473 add_stmt (t);
21474 add_stmt (this_pre_body);
21475 pre_body = pop_stmt_list (pre_body);
21476 }
21477 else
21478 pre_body = this_pre_body;
21479 }
8487df40 21480
fd6481cf 21481 if (decl)
21482 real_decl = decl;
21483 if (par_clauses != NULL && real_decl != NULL_TREE)
21484 {
21485 tree *c;
21486 for (c = par_clauses; *c ; )
21487 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21488 && OMP_CLAUSE_DECL (*c) == real_decl)
21489 {
21490 error ("%Hiteration variable %qD should not be firstprivate",
21491 &loc, real_decl);
21492 *c = OMP_CLAUSE_CHAIN (*c);
21493 }
21494 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21495 && OMP_CLAUSE_DECL (*c) == real_decl)
21496 {
21497 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21498 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
e60a6f7b 21499 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
fd6481cf 21500 OMP_CLAUSE_DECL (l) = real_decl;
21501 OMP_CLAUSE_CHAIN (l) = clauses;
21502 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21503 clauses = l;
21504 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21505 CP_OMP_CLAUSE_INFO (*c) = NULL;
21506 add_private_clause = false;
21507 }
21508 else
21509 {
21510 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21511 && OMP_CLAUSE_DECL (*c) == real_decl)
21512 add_private_clause = false;
21513 c = &OMP_CLAUSE_CHAIN (*c);
21514 }
21515 }
21516
21517 if (add_private_clause)
21518 {
21519 tree c;
21520 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21521 {
21522 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21523 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21524 && OMP_CLAUSE_DECL (c) == decl)
21525 break;
21526 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21527 && OMP_CLAUSE_DECL (c) == decl)
21528 error ("%Hiteration variable %qD should not be firstprivate",
21529 &loc, decl);
21530 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21531 && OMP_CLAUSE_DECL (c) == decl)
21532 error ("%Hiteration variable %qD should not be reduction",
21533 &loc, decl);
21534 }
21535 if (c == NULL)
21536 {
e60a6f7b 21537 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
fd6481cf 21538 OMP_CLAUSE_DECL (c) = decl;
21539 c = finish_omp_clauses (c);
21540 if (c)
21541 {
21542 OMP_CLAUSE_CHAIN (c) = clauses;
21543 clauses = c;
21544 }
8487df40 21545 }
21546 }
21547
fd6481cf 21548 cond = NULL;
21549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
e060ba36 21550 cond = cp_parser_omp_for_cond (parser, decl);
fd6481cf 21551 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8487df40 21552
fd6481cf 21553 incr = NULL;
21554 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21555 {
21556 /* If decl is an iterator, preserve the operator on decl
21557 until finish_omp_for. */
21558 if (decl
21559 && (type_dependent_expression_p (decl)
21560 || CLASS_TYPE_P (TREE_TYPE (decl))))
21561 incr = cp_parser_omp_for_incr (parser, decl);
21562 else
98b326fd 21563 incr = cp_parser_expression (parser, false, NULL);
fd6481cf 21564 }
8487df40 21565
fd6481cf 21566 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21568 /*or_comma=*/false,
21569 /*consume_paren=*/true);
8487df40 21570
fd6481cf 21571 TREE_VEC_ELT (declv, i) = decl;
21572 TREE_VEC_ELT (initv, i) = init;
21573 TREE_VEC_ELT (condv, i) = cond;
21574 TREE_VEC_ELT (incrv, i) = incr;
21575
21576 if (i == collapse - 1)
21577 break;
21578
21579 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21580 in between the collapsed for loops to be still considered perfectly
21581 nested. Hopefully the final version clarifies this.
21582 For now handle (multiple) {'s and empty statements. */
21583 cp_parser_parse_tentatively (parser);
21584 do
21585 {
21586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21587 break;
21588 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21589 {
21590 cp_lexer_consume_token (parser->lexer);
21591 bracecount++;
21592 }
21593 else if (bracecount
21594 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21595 cp_lexer_consume_token (parser->lexer);
21596 else
21597 {
21598 loc = cp_lexer_peek_token (parser->lexer)->location;
21599 error ("%Hnot enough collapsed for loops", &loc);
21600 collapse_err = true;
21601 cp_parser_abort_tentative_parse (parser);
21602 declv = NULL_TREE;
21603 break;
21604 }
21605 }
21606 while (1);
21607
21608 if (declv)
21609 {
21610 cp_parser_parse_definitely (parser);
21611 nbraces += bracecount;
21612 }
21613 }
8487df40 21614
21615 /* Note that we saved the original contents of this flag when we entered
21616 the structured block, and so we don't need to re-save it here. */
21617 parser->in_statement = IN_OMP_FOR;
21618
21619 /* Note that the grammar doesn't call for a structured block here,
21620 though the loop as a whole is a structured block. */
21621 body = push_stmt_list ();
e534436e 21622 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 21623 body = pop_stmt_list (body);
21624
fd6481cf 21625 if (declv == NULL_TREE)
21626 ret = NULL_TREE;
21627 else
21628 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21629 pre_body, clauses);
21630
21631 while (nbraces)
21632 {
21633 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21634 {
21635 cp_lexer_consume_token (parser->lexer);
21636 nbraces--;
21637 }
21638 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21639 cp_lexer_consume_token (parser->lexer);
21640 else
21641 {
21642 if (!collapse_err)
eef0ab03 21643 {
21644 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21645 error ("%Hcollapsed loops not perfectly nested", &loc);
21646 }
fd6481cf 21647 collapse_err = true;
21648 cp_parser_statement_seq_opt (parser, NULL);
21649 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21650 }
21651 }
21652
21653 while (for_block)
21654 {
21655 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21656 for_block = TREE_CHAIN (for_block);
21657 }
21658
21659 return ret;
8487df40 21660}
21661
21662/* OpenMP 2.5:
21663 #pragma omp for for-clause[optseq] new-line
074ab442 21664 for-loop */
8487df40 21665
21666#define OMP_FOR_CLAUSE_MASK \
21667 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21668 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21669 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21670 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21671 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21672 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
fd6481cf 21673 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21674 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
8487df40 21675
21676static tree
21677cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21678{
21679 tree clauses, sb, ret;
21680 unsigned int save;
21681
21682 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21683 "#pragma omp for", pragma_tok);
21684
21685 sb = begin_omp_structured_block ();
21686 save = cp_parser_begin_omp_structured_block (parser);
21687
fd6481cf 21688 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
8487df40 21689
21690 cp_parser_end_omp_structured_block (parser, save);
21691 add_stmt (finish_omp_structured_block (sb));
21692
21693 return ret;
21694}
21695
21696/* OpenMP 2.5:
21697 # pragma omp master new-line
074ab442 21698 structured-block */
8487df40 21699
21700static tree
21701cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21702{
21703 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 21704 return c_finish_omp_master (input_location,
21705 cp_parser_omp_structured_block (parser));
8487df40 21706}
21707
21708/* OpenMP 2.5:
21709 # pragma omp ordered new-line
074ab442 21710 structured-block */
8487df40 21711
21712static tree
21713cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21714{
e60a6f7b 21715 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 21716 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 21717 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
8487df40 21718}
21719
21720/* OpenMP 2.5:
21721
21722 section-scope:
21723 { section-sequence }
21724
21725 section-sequence:
21726 section-directive[opt] structured-block
21727 section-sequence section-directive structured-block */
21728
21729static tree
21730cp_parser_omp_sections_scope (cp_parser *parser)
21731{
21732 tree stmt, substmt;
21733 bool error_suppress = false;
21734 cp_token *tok;
21735
640710cf 21736 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
8487df40 21737 return NULL_TREE;
21738
21739 stmt = push_stmt_list ();
21740
21741 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21742 {
21743 unsigned save;
21744
21745 substmt = begin_omp_structured_block ();
21746 save = cp_parser_begin_omp_structured_block (parser);
21747
21748 while (1)
21749 {
e534436e 21750 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 21751
21752 tok = cp_lexer_peek_token (parser->lexer);
21753 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21754 break;
21755 if (tok->type == CPP_CLOSE_BRACE)
21756 break;
21757 if (tok->type == CPP_EOF)
21758 break;
21759 }
21760
21761 cp_parser_end_omp_structured_block (parser, save);
21762 substmt = finish_omp_structured_block (substmt);
21763 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21764 add_stmt (substmt);
21765 }
21766
21767 while (1)
21768 {
21769 tok = cp_lexer_peek_token (parser->lexer);
21770 if (tok->type == CPP_CLOSE_BRACE)
21771 break;
21772 if (tok->type == CPP_EOF)
21773 break;
21774
21775 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21776 {
21777 cp_lexer_consume_token (parser->lexer);
21778 cp_parser_require_pragma_eol (parser, tok);
21779 error_suppress = false;
21780 }
21781 else if (!error_suppress)
21782 {
21783 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21784 error_suppress = true;
21785 }
21786
21787 substmt = cp_parser_omp_structured_block (parser);
21788 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21789 add_stmt (substmt);
21790 }
640710cf 21791 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8487df40 21792
21793 substmt = pop_stmt_list (stmt);
21794
21795 stmt = make_node (OMP_SECTIONS);
21796 TREE_TYPE (stmt) = void_type_node;
21797 OMP_SECTIONS_BODY (stmt) = substmt;
21798
21799 add_stmt (stmt);
21800 return stmt;
21801}
21802
21803/* OpenMP 2.5:
21804 # pragma omp sections sections-clause[optseq] newline
074ab442 21805 sections-scope */
8487df40 21806
21807#define OMP_SECTIONS_CLAUSE_MASK \
21808 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21809 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21810 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21811 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21812 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21813
21814static tree
21815cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21816{
21817 tree clauses, ret;
21818
21819 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21820 "#pragma omp sections", pragma_tok);
21821
21822 ret = cp_parser_omp_sections_scope (parser);
21823 if (ret)
21824 OMP_SECTIONS_CLAUSES (ret) = clauses;
21825
21826 return ret;
21827}
21828
21829/* OpenMP 2.5:
21830 # pragma parallel parallel-clause new-line
21831 # pragma parallel for parallel-for-clause new-line
074ab442 21832 # pragma parallel sections parallel-sections-clause new-line */
8487df40 21833
21834#define OMP_PARALLEL_CLAUSE_MASK \
21835 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21836 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21837 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21838 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21839 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21840 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21841 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21842 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21843
21844static tree
21845cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21846{
21847 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21848 const char *p_name = "#pragma omp parallel";
21849 tree stmt, clauses, par_clause, ws_clause, block;
21850 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21851 unsigned int save;
e60a6f7b 21852 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 21853
21854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21855 {
21856 cp_lexer_consume_token (parser->lexer);
21857 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21858 p_name = "#pragma omp parallel for";
21859 mask |= OMP_FOR_CLAUSE_MASK;
21860 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21861 }
21862 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21863 {
3369eb76 21864 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 21865 const char *p = IDENTIFIER_POINTER (id);
21866 if (strcmp (p, "sections") == 0)
21867 {
21868 cp_lexer_consume_token (parser->lexer);
21869 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21870 p_name = "#pragma omp parallel sections";
21871 mask |= OMP_SECTIONS_CLAUSE_MASK;
21872 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21873 }
21874 }
21875
21876 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21877 block = begin_omp_parallel ();
21878 save = cp_parser_begin_omp_structured_block (parser);
21879
21880 switch (p_kind)
21881 {
21882 case PRAGMA_OMP_PARALLEL:
355efab1 21883 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 21884 par_clause = clauses;
21885 break;
21886
21887 case PRAGMA_OMP_PARALLEL_FOR:
e60a6f7b 21888 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
fd6481cf 21889 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
8487df40 21890 break;
21891
21892 case PRAGMA_OMP_PARALLEL_SECTIONS:
e60a6f7b 21893 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8487df40 21894 stmt = cp_parser_omp_sections_scope (parser);
21895 if (stmt)
21896 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21897 break;
21898
21899 default:
21900 gcc_unreachable ();
21901 }
21902
21903 cp_parser_end_omp_structured_block (parser, save);
87f7c31e 21904 stmt = finish_omp_parallel (par_clause, block);
21905 if (p_kind != PRAGMA_OMP_PARALLEL)
21906 OMP_PARALLEL_COMBINED (stmt) = 1;
21907 return stmt;
8487df40 21908}
21909
21910/* OpenMP 2.5:
21911 # pragma omp single single-clause[optseq] new-line
074ab442 21912 structured-block */
8487df40 21913
21914#define OMP_SINGLE_CLAUSE_MASK \
21915 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21916 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21917 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21918 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21919
21920static tree
21921cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21922{
21923 tree stmt = make_node (OMP_SINGLE);
21924 TREE_TYPE (stmt) = void_type_node;
21925
21926 OMP_SINGLE_CLAUSES (stmt)
21927 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21928 "#pragma omp single", pragma_tok);
21929 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21930
21931 return add_stmt (stmt);
21932}
21933
fd6481cf 21934/* OpenMP 3.0:
21935 # pragma omp task task-clause[optseq] new-line
21936 structured-block */
21937
21938#define OMP_TASK_CLAUSE_MASK \
21939 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21940 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21941 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21942 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21943 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21944 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21945
21946static tree
21947cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21948{
21949 tree clauses, block;
21950 unsigned int save;
21951
21952 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21953 "#pragma omp task", pragma_tok);
21954 block = begin_omp_task ();
21955 save = cp_parser_begin_omp_structured_block (parser);
21956 cp_parser_statement (parser, NULL_TREE, false, NULL);
21957 cp_parser_end_omp_structured_block (parser, save);
21958 return finish_omp_task (clauses, block);
21959}
21960
21961/* OpenMP 3.0:
21962 # pragma omp taskwait new-line */
21963
21964static void
21965cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21966{
21967 cp_parser_require_pragma_eol (parser, pragma_tok);
21968 finish_omp_taskwait ();
21969}
21970
8487df40 21971/* OpenMP 2.5:
21972 # pragma omp threadprivate (variable-list) */
21973
21974static void
21975cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21976{
21977 tree vars;
21978
a52f99a9 21979 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 21980 cp_parser_require_pragma_eol (parser, pragma_tok);
21981
8487df40 21982 finish_omp_threadprivate (vars);
21983}
21984
21985/* Main entry point to OpenMP statement pragmas. */
21986
21987static void
21988cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21989{
21990 tree stmt;
21991
21992 switch (pragma_tok->pragma_kind)
21993 {
21994 case PRAGMA_OMP_ATOMIC:
21995 cp_parser_omp_atomic (parser, pragma_tok);
21996 return;
21997 case PRAGMA_OMP_CRITICAL:
21998 stmt = cp_parser_omp_critical (parser, pragma_tok);
21999 break;
22000 case PRAGMA_OMP_FOR:
22001 stmt = cp_parser_omp_for (parser, pragma_tok);
22002 break;
22003 case PRAGMA_OMP_MASTER:
22004 stmt = cp_parser_omp_master (parser, pragma_tok);
22005 break;
22006 case PRAGMA_OMP_ORDERED:
22007 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22008 break;
22009 case PRAGMA_OMP_PARALLEL:
22010 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22011 break;
22012 case PRAGMA_OMP_SECTIONS:
22013 stmt = cp_parser_omp_sections (parser, pragma_tok);
22014 break;
22015 case PRAGMA_OMP_SINGLE:
22016 stmt = cp_parser_omp_single (parser, pragma_tok);
22017 break;
fd6481cf 22018 case PRAGMA_OMP_TASK:
22019 stmt = cp_parser_omp_task (parser, pragma_tok);
22020 break;
8487df40 22021 default:
22022 gcc_unreachable ();
22023 }
22024
22025 if (stmt)
22026 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22027}
22028\f
22029/* The parser. */
22030
22031static GTY (()) cp_parser *the_parser;
22032
22033\f
22034/* Special handling for the first token or line in the file. The first
22035 thing in the file might be #pragma GCC pch_preprocess, which loads a
22036 PCH file, which is a GC collection point. So we need to handle this
22037 first pragma without benefit of an existing lexer structure.
22038
074ab442 22039 Always returns one token to the caller in *FIRST_TOKEN. This is
8487df40 22040 either the true first token of the file, or the first token after
22041 the initial pragma. */
22042
22043static void
22044cp_parser_initial_pragma (cp_token *first_token)
22045{
22046 tree name = NULL;
22047
22048 cp_lexer_get_preprocessor_token (NULL, first_token);
22049 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22050 return;
22051
22052 cp_lexer_get_preprocessor_token (NULL, first_token);
22053 if (first_token->type == CPP_STRING)
22054 {
3369eb76 22055 name = first_token->u.value;
8487df40 22056
22057 cp_lexer_get_preprocessor_token (NULL, first_token);
22058 if (first_token->type != CPP_PRAGMA_EOL)
ad9ae192 22059 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22060 &first_token->location);
8487df40 22061 }
22062 else
ad9ae192 22063 error ("%Hexpected string literal", &first_token->location);
8487df40 22064
22065 /* Skip to the end of the pragma. */
22066 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22067 cp_lexer_get_preprocessor_token (NULL, first_token);
22068
8487df40 22069 /* Now actually load the PCH file. */
22070 if (name)
22071 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
b5262297 22072
22073 /* Read one more token to return to our caller. We have to do this
22074 after reading the PCH file in, since its pointers have to be
22075 live. */
22076 cp_lexer_get_preprocessor_token (NULL, first_token);
8487df40 22077}
22078
22079/* Normal parsing of a pragma token. Here we can (and must) use the
22080 regular lexer. */
22081
22082static bool
22083cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22084{
22085 cp_token *pragma_tok;
22086 unsigned int id;
22087
22088 pragma_tok = cp_lexer_consume_token (parser->lexer);
22089 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22090 parser->lexer->in_pragma = true;
22091
22092 id = pragma_tok->pragma_kind;
22093 switch (id)
22094 {
22095 case PRAGMA_GCC_PCH_PREPROCESS:
ad9ae192 22096 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22097 &pragma_tok->location);
8487df40 22098 break;
22099
22100 case PRAGMA_OMP_BARRIER:
22101 switch (context)
22102 {
22103 case pragma_compound:
22104 cp_parser_omp_barrier (parser, pragma_tok);
22105 return false;
22106 case pragma_stmt:
ad9ae192 22107 error ("%H%<#pragma omp barrier%> may only be "
22108 "used in compound statements", &pragma_tok->location);
8487df40 22109 break;
22110 default:
22111 goto bad_stmt;
22112 }
22113 break;
22114
22115 case PRAGMA_OMP_FLUSH:
22116 switch (context)
22117 {
22118 case pragma_compound:
22119 cp_parser_omp_flush (parser, pragma_tok);
22120 return false;
22121 case pragma_stmt:
ad9ae192 22122 error ("%H%<#pragma omp flush%> may only be "
22123 "used in compound statements", &pragma_tok->location);
8487df40 22124 break;
22125 default:
22126 goto bad_stmt;
22127 }
22128 break;
22129
fd6481cf 22130 case PRAGMA_OMP_TASKWAIT:
22131 switch (context)
22132 {
22133 case pragma_compound:
22134 cp_parser_omp_taskwait (parser, pragma_tok);
22135 return false;
22136 case pragma_stmt:
eef0ab03 22137 error ("%H%<#pragma omp taskwait%> may only be "
22138 "used in compound statements",
22139 &pragma_tok->location);
fd6481cf 22140 break;
22141 default:
22142 goto bad_stmt;
22143 }
22144 break;
22145
8487df40 22146 case PRAGMA_OMP_THREADPRIVATE:
22147 cp_parser_omp_threadprivate (parser, pragma_tok);
22148 return false;
22149
22150 case PRAGMA_OMP_ATOMIC:
22151 case PRAGMA_OMP_CRITICAL:
22152 case PRAGMA_OMP_FOR:
22153 case PRAGMA_OMP_MASTER:
22154 case PRAGMA_OMP_ORDERED:
22155 case PRAGMA_OMP_PARALLEL:
22156 case PRAGMA_OMP_SECTIONS:
22157 case PRAGMA_OMP_SINGLE:
fd6481cf 22158 case PRAGMA_OMP_TASK:
8487df40 22159 if (context == pragma_external)
22160 goto bad_stmt;
22161 cp_parser_omp_construct (parser, pragma_tok);
22162 return true;
22163
22164 case PRAGMA_OMP_SECTION:
ad9ae192 22165 error ("%H%<#pragma omp section%> may only be used in "
22166 "%<#pragma omp sections%> construct", &pragma_tok->location);
8487df40 22167 break;
22168
22169 default:
22170 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22171 c_invoke_pragma_handler (id);
22172 break;
22173
22174 bad_stmt:
22175 cp_parser_error (parser, "expected declaration specifiers");
b75b98aa 22176 break;
22177 }
22178
22179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22180 return false;
22181}
22182
22183/* The interface the pragma parsers have to the lexer. */
22184
22185enum cpp_ttype
22186pragma_lex (tree *value)
22187{
22188 cp_token *tok;
22189 enum cpp_ttype ret;
22190
22191 tok = cp_lexer_peek_token (the_parser->lexer);
22192
22193 ret = tok->type;
3369eb76 22194 *value = tok->u.value;
b75b98aa 22195
22196 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22197 ret = CPP_EOF;
22198 else if (ret == CPP_STRING)
22199 *value = cp_parser_string_literal (the_parser, false, false);
22200 else
22201 {
22202 cp_lexer_consume_token (the_parser->lexer);
22203 if (ret == CPP_KEYWORD)
22204 ret = CPP_NAME;
22205 }
22206
22207 return ret;
22208}
22209
22210\f
0a3b29ad 22211/* External interface. */
22212
40109983 22213/* Parse one entire translation unit. */
0a3b29ad 22214
40109983 22215void
22216c_parse_file (void)
0a3b29ad 22217{
22218 bool error_occurred;
393b349a 22219 static bool already_called = false;
22220
22221 if (already_called)
22222 {
22223 sorry ("inter-module optimizations not implemented for C++");
22224 return;
22225 }
22226 already_called = true;
0a3b29ad 22227
22228 the_parser = cp_parser_new ();
4cab8273 22229 push_deferring_access_checks (flag_access_control
22230 ? dk_no_deferred : dk_no_check);
0a3b29ad 22231 error_occurred = cp_parser_translation_unit (the_parser);
22232 the_parser = NULL;
0a3b29ad 22233}
22234
0a3b29ad 22235#include "gt-cp-parser.h"