]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
* diagnostic-core.h: Include bversion.h.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
0a3b29ad 1/* C++ Parser.
7f602bca 2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
c61e1212 3 2005, 2007, 2008, 2009, 2010 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"
0a3b29ad 26#include "cpplib.h"
27#include "tree.h"
28#include "cp-tree.h"
ca82e026 29#include "intl.h"
7bedc3a0 30#include "c-family/c-pragma.h"
0a3b29ad 31#include "decl.h"
32#include "flags.h"
852f689e 33#include "diagnostic-core.h"
0a3b29ad 34#include "output.h"
4b9b2871 35#include "target.h"
56af936e 36#include "cgraph.h"
7bedc3a0 37#include "c-family/c-common.h"
9227b6fc 38#include "plugin.h"
0a3b29ad 39
40\f
41/* The lexer. */
42
00d26680 43/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
44 and c-lex.c) and the C++ parser. */
0a3b29ad 45
3369eb76 46/* A token's value and its associated deferred access checks and
47 qualifying scope. */
48
fb1e4f4a 49struct GTY(()) tree_check {
3369eb76 50 /* The value associated with the token. */
51 tree value;
52 /* The checks that have been associated with value. */
53 VEC (deferred_access_check, gc)* checks;
54 /* The token's qualifying scope (used when it is a
55 CPP_NESTED_NAME_SPECIFIER). */
56 tree qualifying_scope;
57};
58
0a3b29ad 59/* A C++ token. */
60
fb1e4f4a 61typedef struct GTY (()) cp_token {
0a3b29ad 62 /* The kind of token. */
07e4a80b 63 ENUM_BITFIELD (cpp_ttype) type : 8;
0a3b29ad 64 /* If this token is a keyword, this value indicates which keyword.
65 Otherwise, this value is RID_MAX. */
07e4a80b 66 ENUM_BITFIELD (rid) keyword : 8;
c8d5ab79 67 /* Token flags. */
68 unsigned char flags;
b75b98aa 69 /* Identifier for the pragma. */
70 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
2e1f41a9 71 /* True if this token is from a context where it is implicitly extern "C" */
72 BOOL_BITFIELD implicit_extern_c : 1;
b62240d5 73 /* True for a CPP_NAME token that is not a keyword (i.e., for which
74 KEYWORD is RID_MAX) iff this name was looked up and found to be
75 ambiguous. An error has already been reported. */
76 BOOL_BITFIELD ambiguous_p : 1;
0ac758f7 77 /* The location at which this token was found. */
78 location_t location;
63d4e07c 79 /* The value associated with this token, if any. */
3369eb76 80 union cp_token_value {
81 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
82 struct tree_check* GTY((tag ("1"))) tree_check_value;
83 /* Use for all other tokens. */
84 tree GTY((tag ("0"))) value;
85 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
0a3b29ad 86} cp_token;
87
19273cc2 88/* We use a stack of token pointer for saving token sets. */
89typedef struct cp_token *cp_token_position;
046bfc77 90DEF_VEC_P (cp_token_position);
91DEF_VEC_ALLOC_P (cp_token_position,heap);
19273cc2 92
b7bf20db 93static cp_token eof_token =
da4e72c7 94{
0ac758f7 95 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
da4e72c7 96};
19273cc2 97
0a3b29ad 98/* The cp_lexer structure represents the C++ lexer. It is responsible
99 for managing the token stream from the preprocessor and supplying
00d26680 100 it to the parser. Tokens are never added to the cp_lexer after
93523877 101 it is created. */
0a3b29ad 102
fb1e4f4a 103typedef struct GTY (()) cp_lexer {
19273cc2 104 /* The memory allocated for the buffer. NULL if this lexer does not
105 own the token buffer. */
da4e72c7 106 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
107 /* If the lexer owns the buffer, this is the number of tokens in the
108 buffer. */
109 size_t buffer_length;
9031d10b 110
00d26680 111 /* A pointer just past the last available token. The tokens
93523877 112 in this lexer are [buffer, last_token). */
19273cc2 113 cp_token_position GTY ((skip)) last_token;
00d26680 114
19273cc2 115 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
0a3b29ad 116 no more available tokens. */
19273cc2 117 cp_token_position GTY ((skip)) next_token;
0a3b29ad 118
119 /* A stack indicating positions at which cp_lexer_save_tokens was
120 called. The top entry is the most recent position at which we
19273cc2 121 began saving tokens. If the stack is non-empty, we are saving
122 tokens. */
046bfc77 123 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
0a3b29ad 124
b75b98aa 125 /* The next lexer in a linked list of lexers. */
126 struct cp_lexer *next;
127
0a3b29ad 128 /* True if we should output debugging information. */
129 bool debugging_p;
130
b75b98aa 131 /* True if we're in the context of parsing a pragma, and should not
132 increment past the end-of-line marker. */
133 bool in_pragma;
0a3b29ad 134} cp_lexer;
135
00d26680 136/* cp_token_cache is a range of tokens. There is no need to represent
137 allocate heap memory for it, since tokens are never removed from the
138 lexer's array. There is also no need for the GC to walk through
139 a cp_token_cache, since everything in here is referenced through
93523877 140 a lexer. */
00d26680 141
fb1e4f4a 142typedef struct GTY(()) cp_token_cache {
93523877 143 /* The beginning of the token range. */
00d26680 144 cp_token * GTY((skip)) first;
145
93523877 146 /* Points immediately after the last token in the range. */
00d26680 147 cp_token * GTY ((skip)) last;
148} cp_token_cache;
149
c247dce0 150/* The various kinds of non integral constant we encounter. */
151typedef enum non_integral_constant {
c61e1212 152 NIC_NONE,
c247dce0 153 /* floating-point literal */
154 NIC_FLOAT,
155 /* %<this%> */
156 NIC_THIS,
157 /* %<__FUNCTION__%> */
158 NIC_FUNC_NAME,
159 /* %<__PRETTY_FUNCTION__%> */
160 NIC_PRETTY_FUNC,
161 /* %<__func__%> */
162 NIC_C99_FUNC,
163 /* "%<va_arg%> */
164 NIC_VA_ARG,
165 /* a cast */
166 NIC_CAST,
167 /* %<typeid%> operator */
168 NIC_TYPEID,
169 /* non-constant compound literals */
170 NIC_NCC,
171 /* a function call */
172 NIC_FUNC_CALL,
173 /* an increment */
174 NIC_INC,
175 /* an decrement */
176 NIC_DEC,
177 /* an array reference */
178 NIC_ARRAY_REF,
179 /* %<->%> */
180 NIC_ARROW,
181 /* %<.%> */
182 NIC_POINT,
183 /* the address of a label */
184 NIC_ADDR_LABEL,
185 /* %<*%> */
186 NIC_STAR,
187 /* %<&%> */
188 NIC_ADDR,
189 /* %<++%> */
190 NIC_PREINCREMENT,
191 /* %<--%> */
192 NIC_PREDECREMENT,
193 /* %<new%> */
194 NIC_NEW,
195 /* %<delete%> */
196 NIC_DEL,
197 /* calls to overloaded operators */
198 NIC_OVERLOADED,
199 /* an assignment */
200 NIC_ASSIGNMENT,
201 /* a comma operator */
202 NIC_COMMA,
203 /* a call to a constructor */
204 NIC_CONSTRUCTOR
205} non_integral_constant;
206
207/* The various kinds of errors about name-lookup failing. */
208typedef enum name_lookup_error {
209 /* NULL */
210 NLE_NULL,
211 /* is not a type */
212 NLE_TYPE,
213 /* is not a class or namespace */
214 NLE_CXX98,
215 /* is not a class, namespace, or enumeration */
216 NLE_NOT_CXX98
217} name_lookup_error;
218
219/* The various kinds of required token */
220typedef enum required_token {
c61e1212 221 RT_NONE,
222 RT_SEMICOLON, /* ';' */
c247dce0 223 RT_OPEN_PAREN, /* '(' */
224 RT_CLOSE_BRACE, /* '}' */
225 RT_OPEN_BRACE, /* '{' */
226 RT_CLOSE_SQUARE, /* ']' */
227 RT_OPEN_SQUARE, /* '[' */
228 RT_COMMA, /* ',' */
229 RT_SCOPE, /* '::' */
230 RT_LESS, /* '<' */
231 RT_GREATER, /* '>' */
232 RT_EQ, /* '=' */
233 RT_ELLIPSIS, /* '...' */
234 RT_MULT, /* '*' */
235 RT_COMPL, /* '~' */
236 RT_COLON, /* ':' */
237 RT_COLON_SCOPE, /* ':' or '::' */
238 RT_CLOSE_PAREN, /* ')' */
239 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
240 RT_PRAGMA_EOL, /* end of line */
241 RT_NAME, /* identifier */
242
243 /* The type is CPP_KEYWORD */
244 RT_NEW, /* new */
245 RT_DELETE, /* delete */
246 RT_RETURN, /* return */
247 RT_WHILE, /* while */
248 RT_EXTERN, /* extern */
249 RT_STATIC_ASSERT, /* static_assert */
250 RT_DECLTYPE, /* decltype */
251 RT_OPERATOR, /* operator */
252 RT_CLASS, /* class */
253 RT_TEMPLATE, /* template */
254 RT_NAMESPACE, /* namespace */
255 RT_USING, /* using */
256 RT_ASM, /* asm */
257 RT_TRY, /* try */
258 RT_CATCH, /* catch */
259 RT_THROW, /* throw */
260 RT_LABEL, /* __label__ */
261 RT_AT_TRY, /* @try */
262 RT_AT_SYNCHRONIZED, /* @synchronized */
263 RT_AT_THROW, /* @throw */
264
265 RT_SELECT, /* selection-statement */
266 RT_INTERATION, /* iteration-statement */
267 RT_JUMP, /* jump-statement */
268 RT_CLASS_KEY, /* class-key */
269 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
270} required_token;
271
0a3b29ad 272/* Prototypes. */
273
573aba85 274static cp_lexer *cp_lexer_new_main
45baea8b 275 (void);
0a3b29ad 276static cp_lexer *cp_lexer_new_from_tokens
00d26680 277 (cp_token_cache *tokens);
278static void cp_lexer_destroy
279 (cp_lexer *);
0a3b29ad 280static int cp_lexer_saving_tokens
45baea8b 281 (const cp_lexer *);
19273cc2 282static cp_token_position cp_lexer_token_position
283 (cp_lexer *, bool);
284static cp_token *cp_lexer_token_at
285 (cp_lexer *, cp_token_position);
0a3b29ad 286static void cp_lexer_get_preprocessor_token
45baea8b 287 (cp_lexer *, cp_token *);
00d26680 288static inline cp_token *cp_lexer_peek_token
289 (cp_lexer *);
0a3b29ad 290static cp_token *cp_lexer_peek_nth_token
45baea8b 291 (cp_lexer *, size_t);
2370b5bf 292static inline bool cp_lexer_next_token_is
45baea8b 293 (cp_lexer *, enum cpp_ttype);
0a3b29ad 294static bool cp_lexer_next_token_is_not
45baea8b 295 (cp_lexer *, enum cpp_ttype);
0a3b29ad 296static bool cp_lexer_next_token_is_keyword
45baea8b 297 (cp_lexer *, enum rid);
ccb84981 298static cp_token *cp_lexer_consume_token
45baea8b 299 (cp_lexer *);
0a3b29ad 300static void cp_lexer_purge_token
301 (cp_lexer *);
302static void cp_lexer_purge_tokens_after
19273cc2 303 (cp_lexer *, cp_token_position);
0a3b29ad 304static void cp_lexer_save_tokens
45baea8b 305 (cp_lexer *);
0a3b29ad 306static void cp_lexer_commit_tokens
45baea8b 307 (cp_lexer *);
0a3b29ad 308static void cp_lexer_rollback_tokens
45baea8b 309 (cp_lexer *);
2fcca275 310#ifdef ENABLE_CHECKING
0a3b29ad 311static void cp_lexer_print_token
45baea8b 312 (FILE *, cp_token *);
ccb84981 313static inline bool cp_lexer_debugging_p
45baea8b 314 (cp_lexer *);
0a3b29ad 315static void cp_lexer_start_debugging
45baea8b 316 (cp_lexer *) ATTRIBUTE_UNUSED;
0a3b29ad 317static void cp_lexer_stop_debugging
45baea8b 318 (cp_lexer *) ATTRIBUTE_UNUSED;
2fcca275 319#else
b9dd3954 320/* If we define cp_lexer_debug_stream to NULL it will provoke warnings
321 about passing NULL to functions that require non-NULL arguments
322 (fputs, fprintf). It will never be used, so all we need is a value
323 of the right type that's guaranteed not to be NULL. */
324#define cp_lexer_debug_stream stdout
325#define cp_lexer_print_token(str, tok) (void) 0
2fcca275 326#define cp_lexer_debugging_p(lexer) 0
327#endif /* ENABLE_CHECKING */
0a3b29ad 328
00d26680 329static cp_token_cache *cp_token_cache_new
330 (cp_token *, cp_token *);
331
b75b98aa 332static void cp_parser_initial_pragma
333 (cp_token *);
334
0a3b29ad 335/* Manifest constants. */
83adc189 336#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
19273cc2 337#define CP_SAVED_TOKEN_STACK 5
0a3b29ad 338
339/* A token type for keywords, as opposed to ordinary identifiers. */
340#define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
341
342/* A token type for template-ids. If a template-id is processed while
343 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
344 the value of the CPP_TEMPLATE_ID is whatever was returned by
345 cp_parser_template_id. */
346#define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
347
348/* A token type for nested-name-specifiers. If a
349 nested-name-specifier is processed while parsing tentatively, it is
350 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
351 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
352 cp_parser_nested_name_specifier_opt. */
353#define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
354
355/* A token type for tokens that are not tokens at all; these are used
00d26680 356 to represent slots in the array where there used to be a token
93523877 357 that has now been deleted. */
0a88af73 358#define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
359
360/* The number of token types, including C++-specific ones. */
361#define N_CP_TTYPES ((int) (CPP_PURGED + 1))
0a3b29ad 362
363/* Variables. */
364
2fcca275 365#ifdef ENABLE_CHECKING
0a3b29ad 366/* The stream to which debugging output should be written. */
367static FILE *cp_lexer_debug_stream;
2fcca275 368#endif /* ENABLE_CHECKING */
0a3b29ad 369
48d94ede 370/* Nonzero if we are parsing an unevaluated operand: an operand to
371 sizeof, typeof, or alignof. */
372int cp_unevaluated_operand;
373
573aba85 374/* Create a new main C++ lexer, the lexer that gets tokens from the
375 preprocessor. */
0a3b29ad 376
377static cp_lexer *
573aba85 378cp_lexer_new_main (void)
0a3b29ad 379{
573aba85 380 cp_token first_token;
da4e72c7 381 cp_lexer *lexer;
382 cp_token *pos;
383 size_t alloc;
384 size_t space;
385 cp_token *buffer;
573aba85 386
b75b98aa 387 /* It's possible that parsing the first pragma will load a PCH file,
388 which is a GC collection point. So we have to do that before
389 allocating any memory. */
390 cp_parser_initial_pragma (&first_token);
00d26680 391
ddf4604f 392 c_common_no_more_pch ();
0a3b29ad 393
394 /* Allocate the memory. */
ba72912a 395 lexer = ggc_alloc_cleared_cp_lexer ();
0a3b29ad 396
9031d10b 397#ifdef ENABLE_CHECKING
00d26680 398 /* Initially we are not debugging. */
0a3b29ad 399 lexer->debugging_p = false;
2fcca275 400#endif /* ENABLE_CHECKING */
046bfc77 401 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
402 CP_SAVED_TOKEN_STACK);
9031d10b 403
da4e72c7 404 /* Create the buffer. */
405 alloc = CP_LEXER_BUFFER_SIZE;
ba72912a 406 buffer = ggc_alloc_vec_cp_token (alloc);
0a3b29ad 407
da4e72c7 408 /* Put the first token in the buffer. */
409 space = alloc;
410 pos = buffer;
411 *pos = first_token;
9031d10b 412
93523877 413 /* Get the remaining tokens from the preprocessor. */
da4e72c7 414 while (pos->type != CPP_EOF)
00d26680 415 {
da4e72c7 416 pos++;
417 if (!--space)
418 {
419 space = alloc;
420 alloc *= 2;
7ea410eb 421 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
da4e72c7 422 pos = buffer + space;
423 }
424 cp_lexer_get_preprocessor_token (lexer, pos);
00d26680 425 }
da4e72c7 426 lexer->buffer = buffer;
427 lexer->buffer_length = alloc - space;
428 lexer->last_token = pos;
b7bf20db 429 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
00d26680 430
eb0d20b7 431 /* Subsequent preprocessor diagnostics should use compiler
432 diagnostic functions to get the compiler source location. */
7f5f3953 433 done_lexing = true;
eb0d20b7 434
b9dd3954 435 gcc_assert (lexer->next_token->type != CPP_PURGED);
0a3b29ad 436 return lexer;
437}
438
00d26680 439/* Create a new lexer whose token stream is primed with the tokens in
b9dd3954 440 CACHE. When these tokens are exhausted, no new tokens will be read. */
0a3b29ad 441
442static cp_lexer *
b9dd3954 443cp_lexer_new_from_tokens (cp_token_cache *cache)
0a3b29ad 444{
b9dd3954 445 cp_token *first = cache->first;
446 cp_token *last = cache->last;
ba72912a 447 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
573aba85 448
19273cc2 449 /* We do not own the buffer. */
da4e72c7 450 lexer->buffer = NULL;
451 lexer->buffer_length = 0;
b7bf20db 452 lexer->next_token = first == last ? &eof_token : first;
19273cc2 453 lexer->last_token = last;
9031d10b 454
046bfc77 455 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
456 CP_SAVED_TOKEN_STACK);
ccb84981 457
2fcca275 458#ifdef ENABLE_CHECKING
00d26680 459 /* Initially we are not debugging. */
573aba85 460 lexer->debugging_p = false;
00d26680 461#endif
0a3b29ad 462
b9dd3954 463 gcc_assert (lexer->next_token->type != CPP_PURGED);
464 return lexer;
00d26680 465}
466
93523877 467/* Frees all resources associated with LEXER. */
00d26680 468
469static void
470cp_lexer_destroy (cp_lexer *lexer)
471{
19273cc2 472 if (lexer->buffer)
473 ggc_free (lexer->buffer);
046bfc77 474 VEC_free (cp_token_position, heap, lexer->saved_tokens);
00d26680 475 ggc_free (lexer);
476}
477
f1d555e3 478/* Returns nonzero if debugging information should be output. */
0a3b29ad 479
2fcca275 480#ifdef ENABLE_CHECKING
481
2370b5bf 482static inline bool
483cp_lexer_debugging_p (cp_lexer *lexer)
0a3b29ad 484{
2370b5bf 485 return lexer->debugging_p;
486}
487
2fcca275 488#endif /* ENABLE_CHECKING */
489
19273cc2 490static inline cp_token_position
491cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
0a3b29ad 492{
19273cc2 493 gcc_assert (!previous_p || lexer->next_token != &eof_token);
9031d10b 494
19273cc2 495 return lexer->next_token - previous_p;
0a3b29ad 496}
497
3d0f901b 498static inline cp_token *
19273cc2 499cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
3d0f901b 500{
19273cc2 501 return pos;
3d0f901b 502}
503
a9ffdd35 504static inline void
505cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
864468c7 506{
a9ffdd35 507 lexer->next_token = cp_lexer_token_at (lexer, pos);
508}
864468c7 509
a9ffdd35 510static inline cp_token_position
511cp_lexer_previous_token_position (cp_lexer *lexer)
512{
864468c7 513 if (lexer->next_token == &eof_token)
a9ffdd35 514 return lexer->last_token - 1;
864468c7 515 else
a9ffdd35 516 return cp_lexer_token_position (lexer, true);
517}
518
519static inline cp_token *
520cp_lexer_previous_token (cp_lexer *lexer)
521{
522 cp_token_position tp = cp_lexer_previous_token_position (lexer);
864468c7 523
524 return cp_lexer_token_at (lexer, tp);
525}
526
f1d555e3 527/* nonzero if we are presently saving tokens. */
2370b5bf 528
19273cc2 529static inline int
45baea8b 530cp_lexer_saving_tokens (const cp_lexer* lexer)
2370b5bf 531{
19273cc2 532 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
0a3b29ad 533}
534
da4e72c7 535/* Store the next token from the preprocessor in *TOKEN. Return true
8115b8be 536 if we reach EOF. If LEXER is NULL, assume we are handling an
537 initial #pragma pch_preprocess, and thus want the lexer to return
538 processed strings. */
0a3b29ad 539
ccb84981 540static void
8115b8be 541cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
0a3b29ad 542{
2e1f41a9 543 static int is_extern_c = 0;
0a3b29ad 544
61cc302f 545 /* Get a new token from the preprocessor. */
9d819530 546 token->type
8115b8be 547 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
538ba11a 548 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
b75b98aa 549 token->keyword = RID_MAX;
550 token->pragma_kind = PRAGMA_NONE;
0a3b29ad 551
9031d10b 552 /* On some systems, some header files are surrounded by an
2e1f41a9 553 implicit extern "C" block. Set a flag in the token if it
93523877 554 comes from such a header. */
2e1f41a9 555 is_extern_c += pending_lang_change;
556 pending_lang_change = 0;
557 token->implicit_extern_c = is_extern_c > 0;
558
0a3b29ad 559 /* Check to see if this token is a keyword. */
b62240d5 560 if (token->type == CPP_NAME)
0a3b29ad 561 {
3369eb76 562 if (C_IS_RESERVED_WORD (token->u.value))
b62240d5 563 {
564 /* Mark this token as a keyword. */
565 token->type = CPP_KEYWORD;
566 /* Record which keyword. */
3369eb76 567 token->keyword = C_RID_CODE (token->u.value);
b62240d5 568 }
569 else
c47b8257 570 {
ae49e4a6 571 if (warn_cxx0x_compat
572 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
573 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
574 {
575 /* Warn about the C++0x keyword (but still treat it as
576 an identifier). */
f94b7fe2 577 warning (OPT_Wc__0x_compat,
a608187f 578 "identifier %qE will become a keyword in C++0x",
579 token->u.value);
ae49e4a6 580
581 /* Clear out the C_RID_CODE so we don't warn about this
582 particular identifier-turned-keyword again. */
7d339f93 583 C_SET_RID_CODE (token->u.value, RID_MAX);
ae49e4a6 584 }
585
c47b8257 586 token->ambiguous_p = false;
587 token->keyword = RID_MAX;
588 }
0a3b29ad 589 }
7a4e126b 590 else if (token->type == CPP_AT_NAME)
591 {
e5c75ac3 592 /* This only happens in Objective-C++; it must be a keyword. */
7a4e126b 593 token->type = CPP_KEYWORD;
3369eb76 594 switch (C_RID_CODE (token->u.value))
7a4e126b 595 {
e5c75ac3 596 /* Replace 'class' with '@class', 'private' with '@private',
597 etc. This prevents confusion with the C++ keyword
598 'class', and makes the tokens consistent with other
599 Objective-C 'AT' keywords. For example '@class' is
600 reported as RID_AT_CLASS which is consistent with
601 '@synchronized', which is reported as
602 RID_AT_SYNCHRONIZED.
603 */
604 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
605 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
7a4e126b 606 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
e5c75ac3 607 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
608 case RID_THROW: token->keyword = RID_AT_THROW; break;
609 case RID_TRY: token->keyword = RID_AT_TRY; break;
610 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
611 default: token->keyword = C_RID_CODE (token->u.value);
7a4e126b 612 }
613 }
b75b98aa 614 else if (token->type == CPP_PRAGMA)
615 {
616 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
8458f4ca 617 token->pragma_kind = ((enum pragma_kind)
618 TREE_INT_CST_LOW (token->u.value));
3369eb76 619 token->u.value = NULL_TREE;
b75b98aa 620 }
0a3b29ad 621}
622
bdbc474b 623/* Update the globals input_location and the input file stack from TOKEN. */
b9dd3954 624static inline void
625cp_lexer_set_source_position_from_token (cp_token *token)
626{
627 if (token->type != CPP_EOF)
628 {
629 input_location = token->location;
b9dd3954 630 }
631}
632
0a3b29ad 633/* Return a pointer to the next token in the token stream, but do not
634 consume it. */
635
00d26680 636static inline cp_token *
637cp_lexer_peek_token (cp_lexer *lexer)
0a3b29ad 638{
0a3b29ad 639 if (cp_lexer_debugging_p (lexer))
19273cc2 640 {
641 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
642 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
643 putc ('\n', cp_lexer_debug_stream);
644 }
b9dd3954 645 return lexer->next_token;
0a3b29ad 646}
647
648/* Return true if the next token has the indicated TYPE. */
649
b9dd3954 650static inline bool
45baea8b 651cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 652{
b9dd3954 653 return cp_lexer_peek_token (lexer)->type == type;
0a3b29ad 654}
655
656/* Return true if the next token does not have the indicated TYPE. */
657
b9dd3954 658static inline bool
45baea8b 659cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 660{
661 return !cp_lexer_next_token_is (lexer, type);
662}
663
664/* Return true if the next token is the indicated KEYWORD. */
665
b9dd3954 666static inline bool
45baea8b 667cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
0a3b29ad 668{
8f399589 669 return cp_lexer_peek_token (lexer)->keyword == keyword;
0a3b29ad 670}
671
f82f1250 672/* Return true if the next token is not the indicated KEYWORD. */
673
674static inline bool
675cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
676{
677 return cp_lexer_peek_token (lexer)->keyword != keyword;
678}
679
17f99120 680/* Return true if the next token is a keyword for a decl-specifier. */
681
9a7c4b43 682static bool
683cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
684{
685 cp_token *token;
686
687 token = cp_lexer_peek_token (lexer);
688 switch (token->keyword)
689 {
45b44d0a 690 /* auto specifier: storage-class-specifier in C++,
691 simple-type-specifier in C++0x. */
9a7c4b43 692 case RID_AUTO:
45b44d0a 693 /* Storage classes. */
9a7c4b43 694 case RID_REGISTER:
695 case RID_STATIC:
696 case RID_EXTERN:
697 case RID_MUTABLE:
698 case RID_THREAD:
699 /* Elaborated type specifiers. */
700 case RID_ENUM:
701 case RID_CLASS:
702 case RID_STRUCT:
703 case RID_UNION:
704 case RID_TYPENAME:
705 /* Simple type specifiers. */
706 case RID_CHAR:
924bbf02 707 case RID_CHAR16:
708 case RID_CHAR32:
9a7c4b43 709 case RID_WCHAR:
710 case RID_BOOL:
711 case RID_SHORT:
712 case RID_INT:
713 case RID_LONG:
6388cfe2 714 case RID_INT128:
9a7c4b43 715 case RID_SIGNED:
716 case RID_UNSIGNED:
717 case RID_FLOAT:
718 case RID_DOUBLE:
719 case RID_VOID:
720 /* GNU extensions. */
721 case RID_ATTRIBUTE:
722 case RID_TYPEOF:
34da8800 723 /* C++0x extensions. */
724 case RID_DECLTYPE:
9a7c4b43 725 return true;
726
727 default:
728 return false;
729 }
730}
731
0a3b29ad 732/* Return a pointer to the Nth token in the token stream. If N is 1,
b9dd3954 733 then this is precisely equivalent to cp_lexer_peek_token (except
734 that it is not inline). One would like to disallow that case, but
735 there is one case (cp_parser_nth_token_starts_template_id) where
736 the caller passes a variable for N and it might be 1. */
0a3b29ad 737
738static cp_token *
45baea8b 739cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
0a3b29ad 740{
741 cp_token *token;
742
743 /* N is 1-based, not zero-based. */
73cc0964 744 gcc_assert (n > 0);
074ab442 745
b9dd3954 746 if (cp_lexer_debugging_p (lexer))
747 fprintf (cp_lexer_debug_stream,
748 "cp_lexer: peeking ahead %ld at token: ", (long)n);
749
00d26680 750 --n;
0a3b29ad 751 token = lexer->next_token;
73cc0964 752 gcc_assert (!n || token != &eof_token);
00d26680 753 while (n != 0)
0a3b29ad 754 {
00d26680 755 ++token;
19273cc2 756 if (token == lexer->last_token)
757 {
b7bf20db 758 token = &eof_token;
19273cc2 759 break;
760 }
9031d10b 761
00d26680 762 if (token->type != CPP_PURGED)
763 --n;
0a3b29ad 764 }
765
b9dd3954 766 if (cp_lexer_debugging_p (lexer))
767 {
768 cp_lexer_print_token (cp_lexer_debug_stream, token);
769 putc ('\n', cp_lexer_debug_stream);
770 }
771
0a3b29ad 772 return token;
773}
774
b9dd3954 775/* Return the next token, and advance the lexer's next_token pointer
776 to point to the next non-purged token. */
0a3b29ad 777
778static cp_token *
45baea8b 779cp_lexer_consume_token (cp_lexer* lexer)
0a3b29ad 780{
b9dd3954 781 cp_token *token = lexer->next_token;
0a3b29ad 782
19273cc2 783 gcc_assert (token != &eof_token);
b75b98aa 784 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
9031d10b 785
b9dd3954 786 do
19273cc2 787 {
788 lexer->next_token++;
789 if (lexer->next_token == lexer->last_token)
790 {
b7bf20db 791 lexer->next_token = &eof_token;
19273cc2 792 break;
793 }
9031d10b 794
19273cc2 795 }
b9dd3954 796 while (lexer->next_token->type == CPP_PURGED);
9031d10b 797
b9dd3954 798 cp_lexer_set_source_position_from_token (token);
9031d10b 799
0a3b29ad 800 /* Provide debugging output. */
801 if (cp_lexer_debugging_p (lexer))
802 {
b9dd3954 803 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
0a3b29ad 804 cp_lexer_print_token (cp_lexer_debug_stream, token);
b9dd3954 805 putc ('\n', cp_lexer_debug_stream);
0a3b29ad 806 }
9031d10b 807
0a3b29ad 808 return token;
809}
810
b9dd3954 811/* Permanently remove the next token from the token stream, and
812 advance the next_token pointer to refer to the next non-purged
813 token. */
0a3b29ad 814
815static void
816cp_lexer_purge_token (cp_lexer *lexer)
817{
00d26680 818 cp_token *tok = lexer->next_token;
9031d10b 819
19273cc2 820 gcc_assert (tok != &eof_token);
00d26680 821 tok->type = CPP_PURGED;
822 tok->location = UNKNOWN_LOCATION;
3369eb76 823 tok->u.value = NULL_TREE;
00d26680 824 tok->keyword = RID_MAX;
b9dd3954 825
826 do
19273cc2 827 {
828 tok++;
829 if (tok == lexer->last_token)
830 {
b7bf20db 831 tok = &eof_token;
19273cc2 832 break;
833 }
834 }
835 while (tok->type == CPP_PURGED);
836 lexer->next_token = tok;
0a3b29ad 837}
838
00d26680 839/* Permanently remove all tokens after TOK, up to, but not
0a3b29ad 840 including, the token that will be returned next by
841 cp_lexer_peek_token. */
842
843static void
00d26680 844cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
0a3b29ad 845{
19273cc2 846 cp_token *peek = lexer->next_token;
0a3b29ad 847
19273cc2 848 if (peek == &eof_token)
849 peek = lexer->last_token;
9031d10b 850
00d26680 851 gcc_assert (tok < peek);
852
853 for ( tok += 1; tok != peek; tok += 1)
0a3b29ad 854 {
00d26680 855 tok->type = CPP_PURGED;
856 tok->location = UNKNOWN_LOCATION;
3369eb76 857 tok->u.value = NULL_TREE;
00d26680 858 tok->keyword = RID_MAX;
0a3b29ad 859 }
00d26680 860}
861
0a3b29ad 862/* Begin saving tokens. All tokens consumed after this point will be
863 preserved. */
864
865static void
45baea8b 866cp_lexer_save_tokens (cp_lexer* lexer)
0a3b29ad 867{
868 /* Provide debugging output. */
869 if (cp_lexer_debugging_p (lexer))
870 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
871
046bfc77 872 VEC_safe_push (cp_token_position, heap,
873 lexer->saved_tokens, lexer->next_token);
0a3b29ad 874}
875
876/* Commit to the portion of the token stream most recently saved. */
877
878static void
45baea8b 879cp_lexer_commit_tokens (cp_lexer* lexer)
0a3b29ad 880{
881 /* Provide debugging output. */
882 if (cp_lexer_debugging_p (lexer))
883 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
884
19273cc2 885 VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 886}
887
888/* Return all tokens saved since the last call to cp_lexer_save_tokens
889 to the token stream. Stop saving tokens. */
890
891static void
45baea8b 892cp_lexer_rollback_tokens (cp_lexer* lexer)
0a3b29ad 893{
0a3b29ad 894 /* Provide debugging output. */
895 if (cp_lexer_debugging_p (lexer))
896 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
897
19273cc2 898 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 899}
900
0a3b29ad 901/* Print a representation of the TOKEN on the STREAM. */
902
2fcca275 903#ifdef ENABLE_CHECKING
904
0a3b29ad 905static void
00d26680 906cp_lexer_print_token (FILE * stream, cp_token *token)
907{
908 /* We don't use cpp_type2name here because the parser defines
909 a few tokens of its own. */
910 static const char *const token_names[] = {
911 /* cpplib-defined token types */
912#define OP(e, s) #e,
913#define TK(e, s) #e,
914 TTYPE_TABLE
915#undef OP
916#undef TK
917 /* C++ parser token types - see "Manifest constants", above. */
918 "KEYWORD",
919 "TEMPLATE_ID",
920 "NESTED_NAME_SPECIFIER",
921 "PURGED"
922 };
9031d10b 923
00d26680 924 /* If we have a name for the token, print it out. Otherwise, we
925 simply give the numeric code. */
926 gcc_assert (token->type < ARRAY_SIZE(token_names));
927 fputs (token_names[token->type], stream);
0a3b29ad 928
00d26680 929 /* For some tokens, print the associated data. */
0a3b29ad 930 switch (token->type)
931 {
00d26680 932 case CPP_KEYWORD:
933 /* Some keywords have a value that is not an IDENTIFIER_NODE.
934 For example, `struct' is mapped to an INTEGER_CST. */
3369eb76 935 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
00d26680 936 break;
937 /* else fall through */
0a3b29ad 938 case CPP_NAME:
3369eb76 939 fputs (IDENTIFIER_POINTER (token->u.value), stream);
0a3b29ad 940 break;
941
00d26680 942 case CPP_STRING:
924bbf02 943 case CPP_STRING16:
944 case CPP_STRING32:
00d26680 945 case CPP_WSTRING:
538ba11a 946 case CPP_UTF8STRING:
3369eb76 947 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
0a3b29ad 948 break;
949
0a3b29ad 950 default:
951 break;
952 }
0a3b29ad 953}
954
0a3b29ad 955/* Start emitting debugging information. */
956
957static void
45baea8b 958cp_lexer_start_debugging (cp_lexer* lexer)
0a3b29ad 959{
31cc05e3 960 lexer->debugging_p = true;
0a3b29ad 961}
ccb84981 962
0a3b29ad 963/* Stop emitting debugging information. */
964
965static void
45baea8b 966cp_lexer_stop_debugging (cp_lexer* lexer)
0a3b29ad 967{
31cc05e3 968 lexer->debugging_p = false;
0a3b29ad 969}
970
2fcca275 971#endif /* ENABLE_CHECKING */
972
93523877 973/* Create a new cp_token_cache, representing a range of tokens. */
00d26680 974
975static cp_token_cache *
976cp_token_cache_new (cp_token *first, cp_token *last)
977{
ba72912a 978 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
00d26680 979 cache->first = first;
980 cache->last = last;
981 return cache;
982}
983
0a3b29ad 984\f
4b9b2871 985/* Decl-specifiers. */
986
4b9b2871 987/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
988
989static void
990clear_decl_specs (cp_decl_specifier_seq *decl_specs)
991{
992 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
993}
994
3046c0a3 995/* Declarators. */
996
997/* Nothing other than the parser should be creating declarators;
998 declarators are a semi-syntactic representation of C++ entities.
999 Other parts of the front end that need to create entities (like
1000 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1001
207355ad 1002static cp_declarator *make_call_declarator
34eac767 1003 (cp_declarator *, tree, cp_cv_quals, tree, tree);
207355ad 1004static cp_declarator *make_array_declarator
3046c0a3 1005 (cp_declarator *, tree);
207355ad 1006static cp_declarator *make_pointer_declarator
2cfb6cde 1007 (cp_cv_quals, cp_declarator *);
207355ad 1008static cp_declarator *make_reference_declarator
63949b38 1009 (cp_cv_quals, cp_declarator *, bool);
207355ad 1010static cp_parameter_declarator *make_parameter_declarator
4b9b2871 1011 (cp_decl_specifier_seq *, cp_declarator *, tree);
207355ad 1012static cp_declarator *make_ptrmem_declarator
2cfb6cde 1013 (cp_cv_quals, tree, cp_declarator *);
3046c0a3 1014
197c9df7 1015/* An erroneous declarator. */
1016static cp_declarator *cp_error_declarator;
3046c0a3 1017
1018/* The obstack on which declarators and related data structures are
1019 allocated. */
1020static struct obstack declarator_obstack;
1021
1022/* Alloc BYTES from the declarator memory pool. */
1023
1024static inline void *
1025alloc_declarator (size_t bytes)
1026{
1027 return obstack_alloc (&declarator_obstack, bytes);
1028}
1029
1030/* Allocate a declarator of the indicated KIND. Clear fields that are
1031 common to all declarators. */
1032
1033static cp_declarator *
1034make_declarator (cp_declarator_kind kind)
1035{
1036 cp_declarator *declarator;
1037
1038 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1039 declarator->kind = kind;
1040 declarator->attributes = NULL_TREE;
1041 declarator->declarator = NULL;
d95d815d 1042 declarator->parameter_pack_p = false;
3c5a5d99 1043 declarator->id_loc = UNKNOWN_LOCATION;
3046c0a3 1044
1045 return declarator;
1046}
1047
2366ed31 1048/* Make a declarator for a generalized identifier. If
1049 QUALIFYING_SCOPE is non-NULL, the identifier is
1050 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1051 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1052 is, if any. */
3046c0a3 1053
2ded3667 1054static cp_declarator *
2366ed31 1055make_id_declarator (tree qualifying_scope, tree unqualified_name,
1056 special_function_kind sfk)
3046c0a3 1057{
1058 cp_declarator *declarator;
207355ad 1059
2ded3667 1060 /* It is valid to write:
1061
1062 class C { void f(); };
1063 typedef C D;
1064 void D::f();
1065
1066 The standard is not clear about whether `typedef const C D' is
1067 legal; as of 2002-09-15 the committee is considering that
1068 question. EDG 3.0 allows that syntax. Therefore, we do as
1069 well. */
1070 if (qualifying_scope && TYPE_P (qualifying_scope))
1071 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1072
2366ed31 1073 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1074 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1075 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1076
3046c0a3 1077 declarator = make_declarator (cdk_id);
2ded3667 1078 declarator->u.id.qualifying_scope = qualifying_scope;
1079 declarator->u.id.unqualified_name = unqualified_name;
2366ed31 1080 declarator->u.id.sfk = sfk;
d95d815d 1081
3046c0a3 1082 return declarator;
1083}
1084
1085/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1086 of modifiers such as const or volatile to apply to the pointer
1087 type, represented as identifiers. */
1088
1089cp_declarator *
2cfb6cde 1090make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
3046c0a3 1091{
1092 cp_declarator *declarator;
1093
1094 declarator = make_declarator (cdk_pointer);
1095 declarator->declarator = target;
1096 declarator->u.pointer.qualifiers = cv_qualifiers;
1097 declarator->u.pointer.class_type = NULL_TREE;
d95d815d 1098 if (target)
1099 {
0a683683 1100 declarator->id_loc = target->id_loc;
d95d815d 1101 declarator->parameter_pack_p = target->parameter_pack_p;
1102 target->parameter_pack_p = false;
1103 }
1104 else
1105 declarator->parameter_pack_p = false;
3046c0a3 1106
1107 return declarator;
1108}
1109
1110/* Like make_pointer_declarator -- but for references. */
1111
1112cp_declarator *
63949b38 1113make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1114 bool rvalue_ref)
3046c0a3 1115{
1116 cp_declarator *declarator;
1117
1118 declarator = make_declarator (cdk_reference);
1119 declarator->declarator = target;
63949b38 1120 declarator->u.reference.qualifiers = cv_qualifiers;
1121 declarator->u.reference.rvalue_ref = rvalue_ref;
d95d815d 1122 if (target)
1123 {
0a683683 1124 declarator->id_loc = target->id_loc;
d95d815d 1125 declarator->parameter_pack_p = target->parameter_pack_p;
1126 target->parameter_pack_p = false;
1127 }
1128 else
1129 declarator->parameter_pack_p = false;
3046c0a3 1130
1131 return declarator;
1132}
1133
1134/* Like make_pointer_declarator -- but for a pointer to a non-static
1135 member of CLASS_TYPE. */
1136
1137cp_declarator *
2cfb6cde 1138make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
3046c0a3 1139 cp_declarator *pointee)
1140{
1141 cp_declarator *declarator;
1142
1143 declarator = make_declarator (cdk_ptrmem);
1144 declarator->declarator = pointee;
1145 declarator->u.pointer.qualifiers = cv_qualifiers;
1146 declarator->u.pointer.class_type = class_type;
1147
d95d815d 1148 if (pointee)
1149 {
1150 declarator->parameter_pack_p = pointee->parameter_pack_p;
1151 pointee->parameter_pack_p = false;
1152 }
1153 else
1154 declarator->parameter_pack_p = false;
1155
3046c0a3 1156 return declarator;
1157}
1158
1159/* Make a declarator for the function given by TARGET, with the
1160 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1161 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1162 indicates what exceptions can be thrown. */
1163
1164cp_declarator *
207355ad 1165make_call_declarator (cp_declarator *target,
34eac767 1166 tree parms,
2cfb6cde 1167 cp_cv_quals cv_qualifiers,
346e3a9c 1168 tree exception_specification,
1169 tree late_return_type)
3046c0a3 1170{
1171 cp_declarator *declarator;
1172
1173 declarator = make_declarator (cdk_function);
1174 declarator->declarator = target;
1175 declarator->u.function.parameters = parms;
1176 declarator->u.function.qualifiers = cv_qualifiers;
1177 declarator->u.function.exception_specification = exception_specification;
346e3a9c 1178 declarator->u.function.late_return_type = late_return_type;
d95d815d 1179 if (target)
1180 {
0a683683 1181 declarator->id_loc = target->id_loc;
d95d815d 1182 declarator->parameter_pack_p = target->parameter_pack_p;
1183 target->parameter_pack_p = false;
1184 }
1185 else
1186 declarator->parameter_pack_p = false;
3046c0a3 1187
1188 return declarator;
1189}
1190
1191/* Make a declarator for an array of BOUNDS elements, each of which is
1192 defined by ELEMENT. */
1193
1194cp_declarator *
1195make_array_declarator (cp_declarator *element, tree bounds)
1196{
1197 cp_declarator *declarator;
1198
1199 declarator = make_declarator (cdk_array);
1200 declarator->declarator = element;
1201 declarator->u.array.bounds = bounds;
d95d815d 1202 if (element)
1203 {
0a683683 1204 declarator->id_loc = element->id_loc;
d95d815d 1205 declarator->parameter_pack_p = element->parameter_pack_p;
1206 element->parameter_pack_p = false;
1207 }
1208 else
1209 declarator->parameter_pack_p = false;
3046c0a3 1210
1211 return declarator;
1212}
1213
2aedc2ff 1214/* Determine whether the declarator we've seen so far can be a
1215 parameter pack, when followed by an ellipsis. */
1216static bool
1217declarator_can_be_parameter_pack (cp_declarator *declarator)
1218{
1219 /* Search for a declarator name, or any other declarator that goes
1220 after the point where the ellipsis could appear in a parameter
1221 pack. If we find any of these, then this declarator can not be
1222 made into a parameter pack. */
1223 bool found = false;
1224 while (declarator && !found)
1225 {
1226 switch ((int)declarator->kind)
1227 {
1228 case cdk_id:
2aedc2ff 1229 case cdk_array:
2aedc2ff 1230 found = true;
1231 break;
41341abd 1232
1233 case cdk_error:
1234 return true;
1235
2aedc2ff 1236 default:
1237 declarator = declarator->declarator;
1238 break;
1239 }
1240 }
1241
1242 return !found;
1243}
1244
3046c0a3 1245cp_parameter_declarator *no_parameters;
1246
1247/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1248 DECLARATOR and DEFAULT_ARGUMENT. */
1249
1250cp_parameter_declarator *
207355ad 1251make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
3046c0a3 1252 cp_declarator *declarator,
1253 tree default_argument)
1254{
1255 cp_parameter_declarator *parameter;
1256
207355ad 1257 parameter = ((cp_parameter_declarator *)
3046c0a3 1258 alloc_declarator (sizeof (cp_parameter_declarator)));
1259 parameter->next = NULL;
4b9b2871 1260 if (decl_specifiers)
1261 parameter->decl_specifiers = *decl_specifiers;
1262 else
1263 clear_decl_specs (&parameter->decl_specifiers);
3046c0a3 1264 parameter->declarator = declarator;
1265 parameter->default_argument = default_argument;
1266 parameter->ellipsis_p = false;
1267
1268 return parameter;
1269}
1270
3a30cb7f 1271/* Returns true iff DECLARATOR is a declaration for a function. */
1272
1273static bool
1274function_declarator_p (const cp_declarator *declarator)
1275{
1276 while (declarator)
1277 {
1278 if (declarator->kind == cdk_function
1279 && declarator->declarator->kind == cdk_id)
1280 return true;
1281 if (declarator->kind == cdk_id
1282 || declarator->kind == cdk_error)
1283 return false;
1284 declarator = declarator->declarator;
1285 }
1286 return false;
1287}
1288
0a3b29ad 1289/* The parser. */
1290
1291/* Overview
1292 --------
1293
1294 A cp_parser parses the token stream as specified by the C++
1295 grammar. Its job is purely parsing, not semantic analysis. For
1296 example, the parser breaks the token stream into declarators,
1297 expressions, statements, and other similar syntactic constructs.
1298 It does not check that the types of the expressions on either side
1299 of an assignment-statement are compatible, or that a function is
1300 not declared with a parameter of type `void'.
1301
1302 The parser invokes routines elsewhere in the compiler to perform
1303 semantic analysis and to build up the abstract syntax tree for the
ccb84981 1304 code processed.
0a3b29ad 1305
1306 The parser (and the template instantiation code, which is, in a
1307 way, a close relative of parsing) are the only parts of the
1308 compiler that should be calling push_scope and pop_scope, or
1309 related functions. The parser (and template instantiation code)
1310 keeps track of what scope is presently active; everything else
1311 should simply honor that. (The code that generates static
1312 initializers may also need to set the scope, in order to check
1313 access control correctly when emitting the initializers.)
1314
1315 Methodology
1316 -----------
ccb84981 1317
0a3b29ad 1318 The parser is of the standard recursive-descent variety. Upcoming
1319 tokens in the token stream are examined in order to determine which
1320 production to use when parsing a non-terminal. Some C++ constructs
1321 require arbitrary look ahead to disambiguate. For example, it is
1322 impossible, in the general case, to tell whether a statement is an
1323 expression or declaration without scanning the entire statement.
1324 Therefore, the parser is capable of "parsing tentatively." When the
1325 parser is not sure what construct comes next, it enters this mode.
1326 Then, while we attempt to parse the construct, the parser queues up
1327 error messages, rather than issuing them immediately, and saves the
1328 tokens it consumes. If the construct is parsed successfully, the
1329 parser "commits", i.e., it issues any queued error messages and
1330 the tokens that were being preserved are permanently discarded.
1331 If, however, the construct is not parsed successfully, the parser
1332 rolls back its state completely so that it can resume parsing using
1333 a different alternative.
1334
1335 Future Improvements
1336 -------------------
ccb84981 1337
0a88af73 1338 The performance of the parser could probably be improved substantially.
1339 We could often eliminate the need to parse tentatively by looking ahead
1340 a little bit. In some places, this approach might not entirely eliminate
1341 the need to parse tentatively, but it might still speed up the average
1342 case. */
0a3b29ad 1343
1344/* Flags that are passed to some parsing functions. These values can
1345 be bitwise-ored together. */
1346
26dbec0a 1347enum
0a3b29ad 1348{
1349 /* No flags. */
1350 CP_PARSER_FLAGS_NONE = 0x0,
1351 /* The construct is optional. If it is not present, then no error
1352 should be issued. */
1353 CP_PARSER_FLAGS_OPTIONAL = 0x1,
638569c5 1354 /* When parsing a type-specifier, treat user-defined type-names
1355 as non-type identifiers. */
1356 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1357 /* When parsing a type-specifier, do not try to parse a class-specifier
1358 or enum-specifier. */
ca63c29a 1359 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1360 /* When parsing a decl-specifier-seq, only allow type-specifier or
1361 constexpr. */
1362 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
8458f4ca 1363};
1364
1365/* This type is used for parameters and variables which hold
26dbec0a 1366 combinations of the above flags. */
8458f4ca 1367typedef int cp_parser_flags;
0a3b29ad 1368
42bbd0ec 1369/* The different kinds of declarators we want to parse. */
1370
1371typedef enum cp_parser_declarator_kind
1372{
0f3ccaa3 1373 /* We want an abstract declarator. */
42bbd0ec 1374 CP_PARSER_DECLARATOR_ABSTRACT,
1375 /* We want a named declarator. */
1376 CP_PARSER_DECLARATOR_NAMED,
bd8962d5 1377 /* We don't mind, but the name must be an unqualified-id. */
42bbd0ec 1378 CP_PARSER_DECLARATOR_EITHER
1379} cp_parser_declarator_kind;
1380
0a88af73 1381/* The precedence values used to parse binary expressions. The minimum value
1382 of PREC must be 1, because zero is reserved to quickly discriminate
1383 binary operators from other tokens. */
0a3b29ad 1384
0a88af73 1385enum cp_parser_prec
0a3b29ad 1386{
0a88af73 1387 PREC_NOT_OPERATOR,
1388 PREC_LOGICAL_OR_EXPRESSION,
1389 PREC_LOGICAL_AND_EXPRESSION,
1390 PREC_INCLUSIVE_OR_EXPRESSION,
1391 PREC_EXCLUSIVE_OR_EXPRESSION,
1392 PREC_AND_EXPRESSION,
0a88af73 1393 PREC_EQUALITY_EXPRESSION,
22dc256c 1394 PREC_RELATIONAL_EXPRESSION,
0a88af73 1395 PREC_SHIFT_EXPRESSION,
1396 PREC_ADDITIVE_EXPRESSION,
1397 PREC_MULTIPLICATIVE_EXPRESSION,
1398 PREC_PM_EXPRESSION,
1399 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1400};
0a3b29ad 1401
0a88af73 1402/* A mapping from a token type to a corresponding tree node type, with a
1403 precedence value. */
0a3b29ad 1404
0a88af73 1405typedef struct cp_parser_binary_operations_map_node
1406{
1407 /* The token type. */
1408 enum cpp_ttype token_type;
1409 /* The corresponding tree code. */
1410 enum tree_code tree_type;
1411 /* The precedence of this operator. */
1412 enum cp_parser_prec prec;
1413} cp_parser_binary_operations_map_node;
0a3b29ad 1414
1415/* The status of a tentative parse. */
1416
1417typedef enum cp_parser_status_kind
1418{
1419 /* No errors have occurred. */
1420 CP_PARSER_STATUS_KIND_NO_ERROR,
1421 /* An error has occurred. */
1422 CP_PARSER_STATUS_KIND_ERROR,
1423 /* We are committed to this tentative parse, whether or not an error
1424 has occurred. */
1425 CP_PARSER_STATUS_KIND_COMMITTED
1426} cp_parser_status_kind;
1427
0a88af73 1428typedef struct cp_parser_expression_stack_entry
1429{
e534436e 1430 /* Left hand side of the binary operation we are currently
1431 parsing. */
0a88af73 1432 tree lhs;
e534436e 1433 /* Original tree code for left hand side, if it was a binary
1434 expression itself (used for -Wparentheses). */
1435 enum tree_code lhs_type;
1436 /* Tree code for the binary operation we are parsing. */
0a88af73 1437 enum tree_code tree_type;
e534436e 1438 /* Precedence of the binary operation we are parsing. */
8458f4ca 1439 enum cp_parser_prec prec;
0a88af73 1440} cp_parser_expression_stack_entry;
1441
9802eea0 1442/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1443 entries because precedence levels on the stack are monotonically
1444 increasing. */
0a88af73 1445typedef struct cp_parser_expression_stack_entry
1446 cp_parser_expression_stack[NUM_PREC_VALUES];
0a3b29ad 1447
0a88af73 1448/* Context that is saved and restored when parsing tentatively. */
fb1e4f4a 1449typedef struct GTY (()) cp_parser_context {
0a3b29ad 1450 /* If this is a tentative parsing context, the status of the
1451 tentative parse. */
1452 enum cp_parser_status_kind status;
1453 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1454 that are looked up in this context must be looked up both in the
1455 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1456 the context of the containing expression. */
1457 tree object_type;
0a88af73 1458
0a3b29ad 1459 /* The next parsing context in the stack. */
1460 struct cp_parser_context *next;
1461} cp_parser_context;
1462
1463/* Prototypes. */
1464
1465/* Constructors and destructors. */
1466
1467static cp_parser_context *cp_parser_context_new
45baea8b 1468 (cp_parser_context *);
0a3b29ad 1469
2c593bd0 1470/* Class variables. */
1471
7035b2ab 1472static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2c593bd0 1473
0a88af73 1474/* The operator-precedence table used by cp_parser_binary_expression.
1475 Transformed into an associative array (binops_by_token) by
1476 cp_parser_new. */
1477
1478static const cp_parser_binary_operations_map_node binops[] = {
1479 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1480 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1481
1482 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1483 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1484 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1485
1486 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1487 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1488
1489 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1490 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1491
1492 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1493 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1494 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1495 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
0a88af73 1496
1497 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1498 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1499
1500 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1501
1502 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1503
1504 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1505
1506 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1507
1508 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1509};
1510
1511/* The same as binops, but initialized by cp_parser_new so that
1512 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1513 for speed. */
1514static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1515
0a3b29ad 1516/* Constructors and destructors. */
1517
1518/* Construct a new context. The context below this one on the stack
1519 is given by NEXT. */
1520
1521static cp_parser_context *
45baea8b 1522cp_parser_context_new (cp_parser_context* next)
0a3b29ad 1523{
1524 cp_parser_context *context;
1525
1526 /* Allocate the storage. */
2c593bd0 1527 if (cp_parser_context_free_list != NULL)
1528 {
1529 /* Pull the first entry from the free list. */
1530 context = cp_parser_context_free_list;
1531 cp_parser_context_free_list = context->next;
6edf18a6 1532 memset (context, 0, sizeof (*context));
2c593bd0 1533 }
1534 else
ba72912a 1535 context = ggc_alloc_cleared_cp_parser_context ();
0a88af73 1536
0a3b29ad 1537 /* No errors have occurred yet in this context. */
1538 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
08cc44e7 1539 /* If this is not the bottommost context, copy information that we
0a3b29ad 1540 need from the previous context. */
1541 if (next)
1542 {
1543 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1544 expression, then we are parsing one in this context, too. */
1545 context->object_type = next->object_type;
0a3b29ad 1546 /* Thread the stack. */
1547 context->next = next;
1548 }
1549
1550 return context;
1551}
1552
9177da82 1553/* An entry in a queue of function arguments that require post-processing. */
1554
1555typedef struct GTY(()) cp_default_arg_entry_d {
1556 /* The current_class_type when we parsed this arg. */
1557 tree class_type;
1558
1559 /* The function decl itself. */
1560 tree decl;
1561} cp_default_arg_entry;
1562
1563DEF_VEC_O(cp_default_arg_entry);
1564DEF_VEC_ALLOC_O(cp_default_arg_entry,gc);
1565
1566/* An entry in a stack for member functions of local classes. */
1567
1568typedef struct GTY(()) cp_unparsed_functions_entry_d {
1569 /* Functions with default arguments that require post-processing.
1570 Functions appear in this list in declaration order. */
1571 VEC(cp_default_arg_entry,gc) *funs_with_default_args;
1572
1573 /* Functions with defintions that require post-processing. Functions
1574 appear in this list in declaration order. */
1575 VEC(tree,gc) *funs_with_definitions;
1576} cp_unparsed_functions_entry;
1577
1578DEF_VEC_O(cp_unparsed_functions_entry);
1579DEF_VEC_ALLOC_O(cp_unparsed_functions_entry,gc);
1580
0a3b29ad 1581/* The cp_parser structure represents the C++ parser. */
1582
fb1e4f4a 1583typedef struct GTY(()) cp_parser {
0a3b29ad 1584 /* The lexer from which we are obtaining tokens. */
1585 cp_lexer *lexer;
1586
1587 /* The scope in which names should be looked up. If NULL_TREE, then
1588 we look up names in the scope that is currently open in the
1589 source program. If non-NULL, this is either a TYPE or
c75ae97e 1590 NAMESPACE_DECL for the scope in which we should look. It can
1591 also be ERROR_MARK, when we've parsed a bogus scope.
0a3b29ad 1592
1593 This value is not cleared automatically after a name is looked
1594 up, so we must be careful to clear it before starting a new look
1595 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1596 will look up `Z' in the scope of `X', rather than the current
1597 scope.) Unfortunately, it is difficult to tell when name lookup
1598 is complete, because we sometimes peek at a token, look it up,
c75ae97e 1599 and then decide not to consume it. */
0a3b29ad 1600 tree scope;
1601
1602 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1603 last lookup took place. OBJECT_SCOPE is used if an expression
1604 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
ccb84981 1605 respectively. QUALIFYING_SCOPE is used for an expression of the
0a3b29ad 1606 form "X::Y"; it refers to X. */
1607 tree object_scope;
1608 tree qualifying_scope;
1609
1610 /* A stack of parsing contexts. All but the bottom entry on the
1611 stack will be tentative contexts.
1612
1613 We parse tentatively in order to determine which construct is in
1614 use in some situations. For example, in order to determine
1615 whether a statement is an expression-statement or a
1616 declaration-statement we parse it tentatively as a
1617 declaration-statement. If that fails, we then reparse the same
1618 token stream as an expression-statement. */
1619 cp_parser_context *context;
1620
1621 /* True if we are parsing GNU C++. If this flag is not set, then
1622 GNU extensions are not recognized. */
1623 bool allow_gnu_extensions_p;
1624
1625 /* TRUE if the `>' token should be interpreted as the greater-than
1626 operator. FALSE if it is the end of a template-id or
56471494 1627 template-parameter-list. In C++0x mode, this flag also applies to
1628 `>>' tokens, which are viewed as two consecutive `>' tokens when
1629 this flag is FALSE. */
0a3b29ad 1630 bool greater_than_is_operator_p;
1631
1632 /* TRUE if default arguments are allowed within a parameter list
1633 that starts at this point. FALSE if only a gnu extension makes
63eff20d 1634 them permissible. */
0a3b29ad 1635 bool default_arg_ok_p;
ccb84981 1636
0a3b29ad 1637 /* TRUE if we are parsing an integral constant-expression. See
1638 [expr.const] for a precise definition. */
f47c1747 1639 bool integral_constant_expression_p;
0a3b29ad 1640
5f6526e1 1641 /* TRUE if we are parsing an integral constant-expression -- but a
1642 non-constant expression should be permitted as well. This flag
1643 is used when parsing an array bound so that GNU variable-length
1644 arrays are tolerated. */
f47c1747 1645 bool allow_non_integral_constant_expression_p;
5f6526e1 1646
1647 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1648 been seen that makes the expression non-constant. */
f47c1747 1649 bool non_integral_constant_expression_p;
5f6526e1 1650
0a3b29ad 1651 /* TRUE if local variable names and `this' are forbidden in the
1652 current context. */
1653 bool local_variables_forbidden_p;
1654
1655 /* TRUE if the declaration we are parsing is part of a
1656 linkage-specification of the form `extern string-literal
1657 declaration'. */
1658 bool in_unbraced_linkage_specification_p;
1659
1660 /* TRUE if we are presently parsing a declarator, after the
1661 direct-declarator. */
1662 bool in_declarator_p;
1663
92b128ed 1664 /* TRUE if we are presently parsing a template-argument-list. */
1665 bool in_template_argument_list_p;
1666
8487df40 1667 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1668 to IN_OMP_BLOCK if parsing OpenMP structured block and
1669 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1670 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1671 iteration-statement, OpenMP block or loop within that switch. */
1672#define IN_SWITCH_STMT 1
1673#define IN_ITERATION_STMT 2
1674#define IN_OMP_BLOCK 4
1675#define IN_OMP_FOR 8
2672c56c 1676#define IN_IF_STMT 16
8487df40 1677 unsigned char in_statement;
1678
1679 /* TRUE if we are presently parsing the body of a switch statement.
1680 Note that this doesn't quite overlap with in_statement above.
1681 The difference relates to giving the right sets of error messages:
1682 "case not in switch" vs "break statement used with OpenMP...". */
c3fbce20 1683 bool in_switch_statement_p;
1684
41f2d08e 1685 /* TRUE if we are parsing a type-id in an expression context. In
1686 such a situation, both "type (expr)" and "type (type)" are valid
1687 alternatives. */
1688 bool in_type_id_in_expr_p;
1689
2e1f41a9 1690 /* TRUE if we are currently in a header file where declarations are
93523877 1691 implicitly extern "C". */
2e1f41a9 1692 bool implicit_extern_c;
1693
00d26680 1694 /* TRUE if strings in expressions should be translated to the execution
1695 character set. */
1696 bool translate_strings_p;
1697
0aeb1cc5 1698 /* TRUE if we are presently parsing the body of a function, but not
1699 a local class. */
1700 bool in_function_body;
1701
0a3b29ad 1702 /* If non-NULL, then we are parsing a construct where new type
1703 definitions are not permitted. The string stored here will be
1704 issued as an error message if a type is defined. */
1705 const char *type_definition_forbidden_message;
1706
9177da82 1707 /* A stack used for member functions of local classes. The lists
1708 contained in an individual entry can only be processed once the
1709 outermost class being defined is complete. */
1710 VEC(cp_unparsed_functions_entry,gc) *unparsed_queues;
0a3b29ad 1711
1712 /* The number of classes whose definitions are currently in
1713 progress. */
1714 unsigned num_classes_being_defined;
1715
1716 /* The number of template parameter lists that apply directly to the
1717 current declaration. */
1718 unsigned num_template_parameter_lists;
1719} cp_parser;
1720
9177da82 1721/* Managing the unparsed function queues. */
1722
1723#define unparsed_funs_with_default_args \
1724 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1725#define unparsed_funs_with_definitions \
1726 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1727
1728static void
1729push_unparsed_function_queues (cp_parser *parser)
1730{
1731 VEC_safe_push (cp_unparsed_functions_entry, gc,
1732 parser->unparsed_queues, NULL);
1733 unparsed_funs_with_default_args = NULL;
1734 unparsed_funs_with_definitions = make_tree_vector ();
1735}
1736
1737static void
1738pop_unparsed_function_queues (cp_parser *parser)
1739{
1740 release_tree_vector (unparsed_funs_with_definitions);
1741 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1742}
1743
0a3b29ad 1744/* Prototypes. */
1745
1746/* Constructors and destructors. */
1747
1748static cp_parser *cp_parser_new
45baea8b 1749 (void);
0a3b29ad 1750
ccb84981 1751/* Routines to parse various constructs.
0a3b29ad 1752
1753 Those that return `tree' will return the error_mark_node (rather
1754 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1755 Sometimes, they will return an ordinary node if error-recovery was
755edffd 1756 attempted, even though a parse error occurred. So, to check
0a3b29ad 1757 whether or not a parse error occurred, you should always use
1758 cp_parser_error_occurred. If the construct is optional (indicated
1759 either by an `_opt' in the name of the function that does the
1760 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1761 the construct is not present. */
1762
1763/* Lexical conventions [gram.lex] */
1764
1765static tree cp_parser_identifier
45baea8b 1766 (cp_parser *);
00d26680 1767static tree cp_parser_string_literal
1768 (cp_parser *, bool, bool);
0a3b29ad 1769
1770/* Basic concepts [gram.basic] */
1771
1772static bool cp_parser_translation_unit
45baea8b 1773 (cp_parser *);
0a3b29ad 1774
1775/* Expressions [gram.expr] */
1776
1777static tree cp_parser_primary_expression
fbb01da7 1778 (cp_parser *, bool, bool, bool, cp_id_kind *);
0a3b29ad 1779static tree cp_parser_id_expression
130bb1d4 1780 (cp_parser *, bool, bool, bool *, bool, bool);
0a3b29ad 1781static tree cp_parser_unqualified_id
130bb1d4 1782 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1783static tree cp_parser_nested_name_specifier_opt
3d0f901b 1784 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1785static tree cp_parser_nested_name_specifier
0a3b29ad 1786 (cp_parser *, bool, bool, bool, bool);
3f00a6c0 1787static tree cp_parser_qualifying_entity
3d0f901b 1788 (cp_parser *, bool, bool, bool, bool, bool);
0a3b29ad 1789static tree cp_parser_postfix_expression
98b326fd 1790 (cp_parser *, bool, bool, bool, cp_id_kind *);
43bf5d72 1791static tree cp_parser_postfix_open_square_expression
1792 (cp_parser *, tree, bool);
1793static tree cp_parser_postfix_dot_deref_expression
ad9ae192 1794 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
f352a3fb 1795static VEC(tree,gc) *cp_parser_parenthesized_expression_list
33199a81 1796 (cp_parser *, int, bool, bool, bool *);
1797/* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1798enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
0a3b29ad 1799static void cp_parser_pseudo_destructor_name
45baea8b 1800 (cp_parser *, tree *, tree *);
0a3b29ad 1801static tree cp_parser_unary_expression
98b326fd 1802 (cp_parser *, bool, bool, cp_id_kind *);
0a3b29ad 1803static enum tree_code cp_parser_unary_operator
45baea8b 1804 (cp_token *);
0a3b29ad 1805static tree cp_parser_new_expression
45baea8b 1806 (cp_parser *);
f352a3fb 1807static VEC(tree,gc) *cp_parser_new_placement
45baea8b 1808 (cp_parser *);
0a3b29ad 1809static tree cp_parser_new_type_id
3046c0a3 1810 (cp_parser *, tree *);
1811static cp_declarator *cp_parser_new_declarator_opt
45baea8b 1812 (cp_parser *);
3046c0a3 1813static cp_declarator *cp_parser_direct_new_declarator
45baea8b 1814 (cp_parser *);
f352a3fb 1815static VEC(tree,gc) *cp_parser_new_initializer
45baea8b 1816 (cp_parser *);
0a3b29ad 1817static tree cp_parser_delete_expression
45baea8b 1818 (cp_parser *);
ccb84981 1819static tree cp_parser_cast_expression
98b326fd 1820 (cp_parser *, bool, bool, cp_id_kind *);
0a88af73 1821static tree cp_parser_binary_expression
4390875c 1822 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
0a3b29ad 1823static tree cp_parser_question_colon_clause
45baea8b 1824 (cp_parser *, tree);
0a3b29ad 1825static tree cp_parser_assignment_expression
98b326fd 1826 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1827static enum tree_code cp_parser_assignment_operator_opt
45baea8b 1828 (cp_parser *);
0a3b29ad 1829static tree cp_parser_expression
98b326fd 1830 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1831static tree cp_parser_constant_expression
5f6526e1 1832 (cp_parser *, bool, bool *);
43bf5d72 1833static tree cp_parser_builtin_offsetof
1834 (cp_parser *);
a8b75081 1835static tree cp_parser_lambda_expression
1836 (cp_parser *);
1837static void cp_parser_lambda_introducer
1838 (cp_parser *, tree);
1839static void cp_parser_lambda_declarator_opt
1840 (cp_parser *, tree);
1841static void cp_parser_lambda_body
1842 (cp_parser *, tree);
0a3b29ad 1843
1844/* Statements [gram.stmt.stmt] */
1845
1846static void cp_parser_statement
e534436e 1847 (cp_parser *, tree, bool, bool *);
17d53949 1848static void cp_parser_label_for_labeled_statement
1849 (cp_parser *);
0a3b29ad 1850static tree cp_parser_expression_statement
2363ef00 1851 (cp_parser *, tree);
0a3b29ad 1852static tree cp_parser_compound_statement
2363ef00 1853 (cp_parser *, tree, bool);
0a3b29ad 1854static void cp_parser_statement_seq_opt
2363ef00 1855 (cp_parser *, tree);
0a3b29ad 1856static tree cp_parser_selection_statement
e534436e 1857 (cp_parser *, bool *);
0a3b29ad 1858static tree cp_parser_condition
45baea8b 1859 (cp_parser *);
0a3b29ad 1860static tree cp_parser_iteration_statement
45baea8b 1861 (cp_parser *);
0a3b29ad 1862static void cp_parser_for_init_statement
45baea8b 1863 (cp_parser *);
9dd72ec4 1864static tree cp_parser_c_for
1865 (cp_parser *);
1866static tree cp_parser_range_for
1867 (cp_parser *);
0a3b29ad 1868static tree cp_parser_jump_statement
45baea8b 1869 (cp_parser *);
0a3b29ad 1870static void cp_parser_declaration_statement
45baea8b 1871 (cp_parser *);
0a3b29ad 1872
1873static tree cp_parser_implicitly_scoped_statement
e534436e 1874 (cp_parser *, bool *);
0a3b29ad 1875static void cp_parser_already_scoped_statement
45baea8b 1876 (cp_parser *);
0a3b29ad 1877
1878/* Declarations [gram.dcl.dcl] */
1879
1880static void cp_parser_declaration_seq_opt
45baea8b 1881 (cp_parser *);
0a3b29ad 1882static void cp_parser_declaration
45baea8b 1883 (cp_parser *);
0a3b29ad 1884static void cp_parser_block_declaration
45baea8b 1885 (cp_parser *, bool);
0a3b29ad 1886static void cp_parser_simple_declaration
45baea8b 1887 (cp_parser *, bool);
4b9b2871 1888static void cp_parser_decl_specifier_seq
1889 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
0a3b29ad 1890static tree cp_parser_storage_class_specifier_opt
45baea8b 1891 (cp_parser *);
0a3b29ad 1892static tree cp_parser_function_specifier_opt
4b9b2871 1893 (cp_parser *, cp_decl_specifier_seq *);
0a3b29ad 1894static tree cp_parser_type_specifier
207355ad 1895 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
4b9b2871 1896 int *, bool *);
0a3b29ad 1897static tree cp_parser_simple_type_specifier
4b9b2871 1898 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
0a3b29ad 1899static tree cp_parser_type_name
45baea8b 1900 (cp_parser *);
674e90bd 1901static tree cp_parser_nonclass_name
1902 (cp_parser* parser);
0a3b29ad 1903static tree cp_parser_elaborated_type_specifier
45baea8b 1904 (cp_parser *, bool, bool);
0a3b29ad 1905static tree cp_parser_enum_specifier
45baea8b 1906 (cp_parser *);
0a3b29ad 1907static void cp_parser_enumerator_list
45baea8b 1908 (cp_parser *, tree);
ccb84981 1909static void cp_parser_enumerator_definition
45baea8b 1910 (cp_parser *, tree);
0a3b29ad 1911static tree cp_parser_namespace_name
45baea8b 1912 (cp_parser *);
0a3b29ad 1913static void cp_parser_namespace_definition
45baea8b 1914 (cp_parser *);
0a3b29ad 1915static void cp_parser_namespace_body
45baea8b 1916 (cp_parser *);
0a3b29ad 1917static tree cp_parser_qualified_namespace_specifier
45baea8b 1918 (cp_parser *);
0a3b29ad 1919static void cp_parser_namespace_alias_definition
45baea8b 1920 (cp_parser *);
da2a3271 1921static bool cp_parser_using_declaration
1922 (cp_parser *, bool);
0a3b29ad 1923static void cp_parser_using_directive
45baea8b 1924 (cp_parser *);
0a3b29ad 1925static void cp_parser_asm_definition
45baea8b 1926 (cp_parser *);
0a3b29ad 1927static void cp_parser_linkage_specification
45baea8b 1928 (cp_parser *);
7a05c4b1 1929static void cp_parser_static_assert
1930 (cp_parser *, bool);
34da8800 1931static tree cp_parser_decltype
1932 (cp_parser *);
0a3b29ad 1933
1934/* Declarators [gram.dcl.decl] */
1935
1936static tree cp_parser_init_declarator
3369eb76 1937 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
3046c0a3 1938static cp_declarator *cp_parser_declarator
08ea345c 1939 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
3046c0a3 1940static cp_declarator *cp_parser_direct_declarator
08ea345c 1941 (cp_parser *, cp_parser_declarator_kind, int *, bool);
0a3b29ad 1942static enum tree_code cp_parser_ptr_operator
2cfb6cde 1943 (cp_parser *, tree *, cp_cv_quals *);
1944static cp_cv_quals cp_parser_cv_qualifier_seq_opt
45baea8b 1945 (cp_parser *);
346e3a9c 1946static tree cp_parser_late_return_type_opt
1947 (cp_parser *);
0a3b29ad 1948static tree cp_parser_declarator_id
197c9df7 1949 (cp_parser *, bool);
0a3b29ad 1950static tree cp_parser_type_id
45baea8b 1951 (cp_parser *);
75eaa947 1952static tree cp_parser_template_type_arg
1953 (cp_parser *);
638569c5 1954static tree cp_parser_trailing_type_id (cp_parser *);
75eaa947 1955static tree cp_parser_type_id_1
638569c5 1956 (cp_parser *, bool, bool);
4b9b2871 1957static void cp_parser_type_specifier_seq
638569c5 1958 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
34eac767 1959static tree cp_parser_parameter_declaration_clause
45baea8b 1960 (cp_parser *);
34eac767 1961static tree cp_parser_parameter_declaration_list
3046c0a3 1962 (cp_parser *, bool *);
1963static cp_parameter_declarator *cp_parser_parameter_declaration
92b128ed 1964 (cp_parser *, bool, bool *);
41341abd 1965static tree cp_parser_default_argument
1966 (cp_parser *, bool);
0a3b29ad 1967static void cp_parser_function_body
1968 (cp_parser *);
1969static tree cp_parser_initializer
878870b4 1970 (cp_parser *, bool *, bool *);
0a3b29ad 1971static tree cp_parser_initializer_clause
878870b4 1972 (cp_parser *, bool *);
f82f1250 1973static tree cp_parser_braced_list
1974 (cp_parser*, bool*);
c75b4594 1975static VEC(constructor_elt,gc) *cp_parser_initializer_list
878870b4 1976 (cp_parser *, bool *);
0a3b29ad 1977
1978static bool cp_parser_ctor_initializer_opt_and_function_body
1979 (cp_parser *);
1980
1981/* Classes [gram.class] */
1982
1983static tree cp_parser_class_name
e2ae55f2 1984 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
0a3b29ad 1985static tree cp_parser_class_specifier
45baea8b 1986 (cp_parser *);
0a3b29ad 1987static tree cp_parser_class_head
1a9b4c25 1988 (cp_parser *, bool *, tree *, tree *);
0a3b29ad 1989static enum tag_types cp_parser_class_key
45baea8b 1990 (cp_parser *);
0a3b29ad 1991static void cp_parser_member_specification_opt
45baea8b 1992 (cp_parser *);
0a3b29ad 1993static void cp_parser_member_declaration
45baea8b 1994 (cp_parser *);
0a3b29ad 1995static tree cp_parser_pure_specifier
45baea8b 1996 (cp_parser *);
0a3b29ad 1997static tree cp_parser_constant_initializer
45baea8b 1998 (cp_parser *);
0a3b29ad 1999
2000/* Derived classes [gram.class.derived] */
2001
2002static tree cp_parser_base_clause
45baea8b 2003 (cp_parser *);
0a3b29ad 2004static tree cp_parser_base_specifier
45baea8b 2005 (cp_parser *);
0a3b29ad 2006
2007/* Special member functions [gram.special] */
2008
2009static tree cp_parser_conversion_function_id
45baea8b 2010 (cp_parser *);
0a3b29ad 2011static tree cp_parser_conversion_type_id
45baea8b 2012 (cp_parser *);
3046c0a3 2013static cp_declarator *cp_parser_conversion_declarator_opt
45baea8b 2014 (cp_parser *);
0a3b29ad 2015static bool cp_parser_ctor_initializer_opt
45baea8b 2016 (cp_parser *);
0a3b29ad 2017static void cp_parser_mem_initializer_list
45baea8b 2018 (cp_parser *);
0a3b29ad 2019static tree cp_parser_mem_initializer
45baea8b 2020 (cp_parser *);
0a3b29ad 2021static tree cp_parser_mem_initializer_id
45baea8b 2022 (cp_parser *);
0a3b29ad 2023
2024/* Overloading [gram.over] */
2025
2026static tree cp_parser_operator_function_id
45baea8b 2027 (cp_parser *);
0a3b29ad 2028static tree cp_parser_operator
45baea8b 2029 (cp_parser *);
0a3b29ad 2030
2031/* Templates [gram.temp] */
2032
2033static void cp_parser_template_declaration
45baea8b 2034 (cp_parser *, bool);
0a3b29ad 2035static tree cp_parser_template_parameter_list
45baea8b 2036 (cp_parser *);
0a3b29ad 2037static tree cp_parser_template_parameter
d95d815d 2038 (cp_parser *, bool *, bool *);
0a3b29ad 2039static tree cp_parser_type_parameter
d95d815d 2040 (cp_parser *, bool *);
0a3b29ad 2041static tree cp_parser_template_id
3d0f901b 2042 (cp_parser *, bool, bool, bool);
0a3b29ad 2043static tree cp_parser_template_name
3d0f901b 2044 (cp_parser *, bool, bool, bool, bool *);
0a3b29ad 2045static tree cp_parser_template_argument_list
45baea8b 2046 (cp_parser *);
0a3b29ad 2047static tree cp_parser_template_argument
45baea8b 2048 (cp_parser *);
0a3b29ad 2049static void cp_parser_explicit_instantiation
45baea8b 2050 (cp_parser *);
0a3b29ad 2051static void cp_parser_explicit_specialization
45baea8b 2052 (cp_parser *);
0a3b29ad 2053
2054/* Exception handling [gram.exception] */
2055
ccb84981 2056static tree cp_parser_try_block
45baea8b 2057 (cp_parser *);
0a3b29ad 2058static bool cp_parser_function_try_block
45baea8b 2059 (cp_parser *);
0a3b29ad 2060static void cp_parser_handler_seq
45baea8b 2061 (cp_parser *);
0a3b29ad 2062static void cp_parser_handler
45baea8b 2063 (cp_parser *);
0a3b29ad 2064static tree cp_parser_exception_declaration
45baea8b 2065 (cp_parser *);
0a3b29ad 2066static tree cp_parser_throw_expression
45baea8b 2067 (cp_parser *);
0a3b29ad 2068static tree cp_parser_exception_specification_opt
45baea8b 2069 (cp_parser *);
0a3b29ad 2070static tree cp_parser_type_id_list
45baea8b 2071 (cp_parser *);
0a3b29ad 2072
2073/* GNU Extensions */
2074
2075static tree cp_parser_asm_specification_opt
45baea8b 2076 (cp_parser *);
0a3b29ad 2077static tree cp_parser_asm_operand_list
45baea8b 2078 (cp_parser *);
0a3b29ad 2079static tree cp_parser_asm_clobber_list
45baea8b 2080 (cp_parser *);
78f55ca8 2081static tree cp_parser_asm_label_list
2082 (cp_parser *);
0a3b29ad 2083static tree cp_parser_attributes_opt
45baea8b 2084 (cp_parser *);
0a3b29ad 2085static tree cp_parser_attribute_list
45baea8b 2086 (cp_parser *);
0a3b29ad 2087static bool cp_parser_extension_opt
45baea8b 2088 (cp_parser *, int *);
0a3b29ad 2089static void cp_parser_label_declaration
45baea8b 2090 (cp_parser *);
0a3b29ad 2091
b75b98aa 2092enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2093static bool cp_parser_pragma
2094 (cp_parser *, enum pragma_context);
2095
7a4e126b 2096/* Objective-C++ Productions */
2097
2098static tree cp_parser_objc_message_receiver
2099 (cp_parser *);
2100static tree cp_parser_objc_message_args
2101 (cp_parser *);
2102static tree cp_parser_objc_message_expression
2103 (cp_parser *);
2104static tree cp_parser_objc_encode_expression
2105 (cp_parser *);
9031d10b 2106static tree cp_parser_objc_defs_expression
7a4e126b 2107 (cp_parser *);
2108static tree cp_parser_objc_protocol_expression
2109 (cp_parser *);
2110static tree cp_parser_objc_selector_expression
2111 (cp_parser *);
2112static tree cp_parser_objc_expression
2113 (cp_parser *);
2114static bool cp_parser_objc_selector_p
2115 (enum cpp_ttype);
2116static tree cp_parser_objc_selector
2117 (cp_parser *);
2118static tree cp_parser_objc_protocol_refs_opt
2119 (cp_parser *);
2120static void cp_parser_objc_declaration
a336eb4b 2121 (cp_parser *, tree);
7a4e126b 2122static tree cp_parser_objc_statement
2123 (cp_parser *);
a336eb4b 2124static bool cp_parser_objc_valid_prefix_attributes
86c110ac 2125 (cp_parser *, tree *);
1d894bcf 2126static void cp_parser_objc_at_property_declaration
86c110ac 2127 (cp_parser *) ;
e1f293c0 2128static void cp_parser_objc_at_synthesize_declaration
2129 (cp_parser *) ;
2130static void cp_parser_objc_at_dynamic_declaration
2131 (cp_parser *) ;
1d894bcf 2132static tree cp_parser_objc_struct_declaration
86c110ac 2133 (cp_parser *) ;
7a4e126b 2134
0a3b29ad 2135/* Utility Routines */
2136
2137static tree cp_parser_lookup_name
ad9ae192 2138 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
0a3b29ad 2139static tree cp_parser_lookup_name_simple
ad9ae192 2140 (cp_parser *, tree, location_t);
0a3b29ad 2141static tree cp_parser_maybe_treat_template_as_class
2142 (tree, bool);
2143static bool cp_parser_check_declarator_template_parameters
ad9ae192 2144 (cp_parser *, cp_declarator *, location_t);
0a3b29ad 2145static bool cp_parser_check_template_parameters
7b07a15e 2146 (cp_parser *, unsigned, location_t, cp_declarator *);
a63bc44c 2147static tree cp_parser_simple_cast_expression
2148 (cp_parser *);
0a3b29ad 2149static tree cp_parser_global_scope_opt
130bb1d4 2150 (cp_parser *, bool);
0a3b29ad 2151static bool cp_parser_constructor_declarator_p
2152 (cp_parser *, bool);
2153static tree cp_parser_function_definition_from_specifiers_and_declarator
4b9b2871 2154 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
0a3b29ad 2155static tree cp_parser_function_definition_after_declarator
45baea8b 2156 (cp_parser *, bool);
0a3b29ad 2157static void cp_parser_template_declaration_after_export
45baea8b 2158 (cp_parser *, bool);
23010bc8 2159static void cp_parser_perform_template_parameter_access_checks
3369eb76 2160 (VEC (deferred_access_check,gc)*);
0a3b29ad 2161static tree cp_parser_single_declaration
0c032b46 2162 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
0a3b29ad 2163static tree cp_parser_functional_cast
45baea8b 2164 (cp_parser *, tree);
92b128ed 2165static tree cp_parser_save_member_function_body
4b9b2871 2166 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
8534c3a3 2167static tree cp_parser_enclosed_template_argument_list
2168 (cp_parser *);
69b6679c 2169static void cp_parser_save_default_args
2170 (cp_parser *, tree);
0a3b29ad 2171static void cp_parser_late_parsing_for_member
45baea8b 2172 (cp_parser *, tree);
0a3b29ad 2173static void cp_parser_late_parsing_default_args
af128372 2174 (cp_parser *, tree);
0a3b29ad 2175static tree cp_parser_sizeof_operand
45baea8b 2176 (cp_parser *, enum rid);
481451eb 2177static tree cp_parser_trait_expr
2178 (cp_parser *, enum rid);
0a3b29ad 2179static bool cp_parser_declares_only_class_p
45baea8b 2180 (cp_parser *);
4b9b2871 2181static void cp_parser_set_storage_class
ad9ae192 2182 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
207355ad 2183static void cp_parser_set_decl_spec_type
eef0ab03 2184 (cp_decl_specifier_seq *, tree, location_t, bool);
0a3b29ad 2185static bool cp_parser_friend_p
4b9b2871 2186 (const cp_decl_specifier_seq *);
c247dce0 2187static void cp_parser_required_error
2188 (cp_parser *, required_token, bool);
0a3b29ad 2189static cp_token *cp_parser_require
c247dce0 2190 (cp_parser *, enum cpp_ttype, required_token);
0a3b29ad 2191static cp_token *cp_parser_require_keyword
c247dce0 2192 (cp_parser *, enum rid, required_token);
ccb84981 2193static bool cp_parser_token_starts_function_definition_p
45baea8b 2194 (cp_token *);
0a3b29ad 2195static bool cp_parser_next_token_starts_class_definition_p
2196 (cp_parser *);
13795292 2197static bool cp_parser_next_token_ends_template_argument_p
2198 (cp_parser *);
c8d5ab79 2199static bool cp_parser_nth_token_starts_template_argument_list_p
2200 (cp_parser *, size_t);
0a3b29ad 2201static enum tag_types cp_parser_token_is_class_key
45baea8b 2202 (cp_token *);
0a3b29ad 2203static void cp_parser_check_class_key
2204 (enum tag_types, tree type);
7e35473e 2205static void cp_parser_check_access_in_redeclaration
ad9ae192 2206 (tree type, location_t location);
0a3b29ad 2207static bool cp_parser_optional_template_keyword
2208 (cp_parser *);
ccb84981 2209static void cp_parser_pre_parsed_nested_name_specifier
b3c48b5d 2210 (cp_parser *);
f82f1250 2211static bool cp_parser_cache_group
00d26680 2212 (cp_parser *, enum cpp_ttype, unsigned);
ccb84981 2213static void cp_parser_parse_tentatively
45baea8b 2214 (cp_parser *);
0a3b29ad 2215static void cp_parser_commit_to_tentative_parse
45baea8b 2216 (cp_parser *);
0a3b29ad 2217static void cp_parser_abort_tentative_parse
45baea8b 2218 (cp_parser *);
0a3b29ad 2219static bool cp_parser_parse_definitely
45baea8b 2220 (cp_parser *);
2370b5bf 2221static inline bool cp_parser_parsing_tentatively
45baea8b 2222 (cp_parser *);
efcbcf83 2223static bool cp_parser_uncommitted_to_tentative_parse_p
45baea8b 2224 (cp_parser *);
0a3b29ad 2225static void cp_parser_error
45baea8b 2226 (cp_parser *, const char *);
92b128ed 2227static void cp_parser_name_lookup_error
c247dce0 2228 (cp_parser *, tree, tree, name_lookup_error, location_t);
2c593bd0 2229static bool cp_parser_simulate_error
45baea8b 2230 (cp_parser *);
9a7c4b43 2231static bool cp_parser_check_type_definition
45baea8b 2232 (cp_parser *);
8172be22 2233static void cp_parser_check_for_definition_in_return_type
eef0ab03 2234 (cp_declarator *, tree, location_t type_location);
1157e9f0 2235static void cp_parser_check_for_invalid_template_id
eef0ab03 2236 (cp_parser *, tree, location_t location);
3938e0c2 2237static bool cp_parser_non_integral_constant_expression
c247dce0 2238 (cp_parser *, non_integral_constant);
e00c3963 2239static void cp_parser_diagnose_invalid_type_name
ad9ae192 2240 (cp_parser *, tree, tree, location_t);
e00c3963 2241static bool cp_parser_parse_and_diagnose_invalid_type_name
954ad420 2242 (cp_parser *);
0986fa22 2243static int cp_parser_skip_to_closing_parenthesis
3d0f901b 2244 (cp_parser *, bool, bool, bool);
0a3b29ad 2245static void cp_parser_skip_to_end_of_statement
45baea8b 2246 (cp_parser *);
cf91b86a 2247static void cp_parser_consume_semicolon_at_end_of_statement
2248 (cp_parser *);
0a3b29ad 2249static void cp_parser_skip_to_end_of_block_or_statement
45baea8b 2250 (cp_parser *);
9aad947b 2251static bool cp_parser_skip_to_closing_brace
0a3b29ad 2252 (cp_parser *);
c42e0e2d 2253static void cp_parser_skip_to_end_of_template_parameter_list
2254 (cp_parser *);
b75b98aa 2255static void cp_parser_skip_to_pragma_eol
2256 (cp_parser*, cp_token *);
0a3b29ad 2257static bool cp_parser_error_occurred
45baea8b 2258 (cp_parser *);
0a3b29ad 2259static bool cp_parser_allow_gnu_extensions_p
45baea8b 2260 (cp_parser *);
0a3b29ad 2261static bool cp_parser_is_string_literal
45baea8b 2262 (cp_token *);
ccb84981 2263static bool cp_parser_is_keyword
45baea8b 2264 (cp_token *, enum rid);
e00c3963 2265static tree cp_parser_make_typename_type
ad9ae192 2266 (cp_parser *, tree, tree, location_t location);
63949b38 2267static cp_declarator * cp_parser_make_indirect_declarator
2268 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
0a3b29ad 2269
f1d555e3 2270/* Returns nonzero if we are parsing tentatively. */
2370b5bf 2271
2272static inline bool
45baea8b 2273cp_parser_parsing_tentatively (cp_parser* parser)
2370b5bf 2274{
2275 return parser->context->next != NULL;
2276}
2277
f1d555e3 2278/* Returns nonzero if TOKEN is a string literal. */
0a3b29ad 2279
2280static bool
45baea8b 2281cp_parser_is_string_literal (cp_token* token)
0a3b29ad 2282{
924bbf02 2283 return (token->type == CPP_STRING ||
2284 token->type == CPP_STRING16 ||
2285 token->type == CPP_STRING32 ||
538ba11a 2286 token->type == CPP_WSTRING ||
2287 token->type == CPP_UTF8STRING);
0a3b29ad 2288}
2289
f1d555e3 2290/* Returns nonzero if TOKEN is the indicated KEYWORD. */
0a3b29ad 2291
2292static bool
45baea8b 2293cp_parser_is_keyword (cp_token* token, enum rid keyword)
0a3b29ad 2294{
2295 return token->keyword == keyword;
2296}
2297
b9dd3954 2298/* If not parsing tentatively, issue a diagnostic of the form
2299 FILE:LINE: MESSAGE before TOKEN
2300 where TOKEN is the next token in the input stream. MESSAGE
2301 (specified by the caller) is usually of the form "expected
2302 OTHER-TOKEN". */
0a3b29ad 2303
2304static void
c247dce0 2305cp_parser_error (cp_parser* parser, const char* gmsgid)
0a3b29ad 2306{
2c593bd0 2307 if (!cp_parser_simulate_error (parser))
92b128ed 2308 {
b9dd3954 2309 cp_token *token = cp_lexer_peek_token (parser->lexer);
2310 /* This diagnostic makes more sense if it is tagged to the line
2311 of the token we just peeked at. */
2312 cp_lexer_set_source_position_from_token (token);
b75b98aa 2313
eb78e86f 2314 if (token->type == CPP_PRAGMA)
2315 {
ccb59bb6 2316 error_at (token->location,
2317 "%<#pragma%> is not allowed here");
b75b98aa 2318 cp_parser_skip_to_pragma_eol (parser, token);
eb78e86f 2319 return;
2320 }
b75b98aa 2321
c247dce0 2322 c_parse_error (gmsgid,
78662158 2323 /* Because c_parser_error does not understand
2324 CPP_KEYWORD, keywords are treated like
2325 identifiers. */
ccb84981 2326 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
ba99525e 2327 token->u.value, token->flags);
92b128ed 2328 }
2329}
2330
2331/* Issue an error about name-lookup failing. NAME is the
2332 IDENTIFIER_NODE DECL is the result of
2333 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2334 the thing that we hoped to find. */
2335
2336static void
2337cp_parser_name_lookup_error (cp_parser* parser,
2338 tree name,
2339 tree decl,
c247dce0 2340 name_lookup_error desired,
ad9ae192 2341 location_t location)
92b128ed 2342{
2343 /* If name lookup completely failed, tell the user that NAME was not
2344 declared. */
2345 if (decl == error_mark_node)
2346 {
2347 if (parser->scope && parser->scope != global_namespace)
ccb59bb6 2348 error_at (location, "%<%E::%E%> has not been declared",
2349 parser->scope, name);
92b128ed 2350 else if (parser->scope == global_namespace)
ccb59bb6 2351 error_at (location, "%<::%E%> has not been declared", name);
9031d10b 2352 else if (parser->object_scope
30aea172 2353 && !CLASS_TYPE_P (parser->object_scope))
ccb59bb6 2354 error_at (location, "request for member %qE in non-class type %qT",
2355 name, parser->object_scope);
30aea172 2356 else if (parser->object_scope)
ccb59bb6 2357 error_at (location, "%<%T::%E%> has not been declared",
2358 parser->object_scope, name);
92b128ed 2359 else
ccb59bb6 2360 error_at (location, "%qE has not been declared", name);
92b128ed 2361 }
2362 else if (parser->scope && parser->scope != global_namespace)
c247dce0 2363 {
2364 switch (desired)
2365 {
2366 case NLE_TYPE:
2367 error_at (location, "%<%E::%E%> is not a type",
2368 parser->scope, name);
2369 break;
2370 case NLE_CXX98:
2371 error_at (location, "%<%E::%E%> is not a class or namespace",
2372 parser->scope, name);
2373 break;
2374 case NLE_NOT_CXX98:
2375 error_at (location,
2376 "%<%E::%E%> is not a class, namespace, or enumeration",
2377 parser->scope, name);
2378 break;
2379 default:
2380 gcc_unreachable ();
2381
2382 }
2383 }
92b128ed 2384 else if (parser->scope == global_namespace)
c247dce0 2385 {
2386 switch (desired)
2387 {
2388 case NLE_TYPE:
2389 error_at (location, "%<::%E%> is not a type", name);
2390 break;
2391 case NLE_CXX98:
2392 error_at (location, "%<::%E%> is not a class or namespace", name);
2393 break;
2394 case NLE_NOT_CXX98:
2395 error_at (location,
2396 "%<::%E%> is not a class, namespace, or enumeration",
2397 name);
2398 break;
2399 default:
2400 gcc_unreachable ();
2401 }
2402 }
92b128ed 2403 else
c247dce0 2404 {
2405 switch (desired)
2406 {
2407 case NLE_TYPE:
2408 error_at (location, "%qE is not a type", name);
2409 break;
2410 case NLE_CXX98:
2411 error_at (location, "%qE is not a class or namespace", name);
2412 break;
2413 case NLE_NOT_CXX98:
2414 error_at (location,
2415 "%qE is not a class, namespace, or enumeration", name);
2416 break;
2417 default:
2418 gcc_unreachable ();
2419 }
2420 }
0a3b29ad 2421}
2422
2423/* If we are parsing tentatively, remember that an error has occurred
2c593bd0 2424 during this tentative parse. Returns true if the error was
e24657db 2425 simulated; false if a message should be issued by the caller. */
0a3b29ad 2426
2c593bd0 2427static bool
45baea8b 2428cp_parser_simulate_error (cp_parser* parser)
0a3b29ad 2429{
efcbcf83 2430 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2c593bd0 2431 {
2432 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2433 return true;
2434 }
2435 return false;
0a3b29ad 2436}
2437
639b2fed 2438/* Check for repeated decl-specifiers. */
2439
2440static void
ad9ae192 2441cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2442 location_t location)
639b2fed 2443{
9f1b7d17 2444 int ds;
639b2fed 2445
2446 for (ds = ds_first; ds != ds_last; ++ds)
2447 {
9f1b7d17 2448 unsigned count = decl_specs->specs[ds];
639b2fed 2449 if (count < 2)
2450 continue;
2451 /* The "long" specifier is a special case because of "long long". */
2452 if (ds == ds_long)
2453 {
2454 if (count > 2)
ccb59bb6 2455 error_at (location, "%<long long long%> is too long for GCC");
9ab71c6b 2456 else
2457 pedwarn_cxx98 (location, OPT_Wlong_long,
2458 "ISO C++ 1998 does not support %<long long%>");
639b2fed 2459 }
2460 else if (count > 1)
2461 {
2462 static const char *const decl_spec_names[] = {
2463 "signed",
2464 "unsigned",
2465 "short",
2466 "long",
2467 "const",
2468 "volatile",
2469 "restrict",
2470 "inline",
2471 "virtual",
2472 "explicit",
2473 "friend",
2474 "typedef",
17814aca 2475 "constexpr",
639b2fed 2476 "__complex",
2477 "__thread"
2478 };
ccb59bb6 2479 error_at (location, "duplicate %qs", decl_spec_names[ds]);
639b2fed 2480 }
2481 }
2482}
2483
0a3b29ad 2484/* This function is called when a type is defined. If type
2485 definitions are forbidden at this point, an error message is
2486 issued. */
2487
9a7c4b43 2488static bool
45baea8b 2489cp_parser_check_type_definition (cp_parser* parser)
0a3b29ad 2490{
2491 /* If types are forbidden here, issue a message. */
2492 if (parser->type_definition_forbidden_message)
9a7c4b43 2493 {
2e52ac87 2494 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2495 in the message need to be interpreted. */
2496 error (parser->type_definition_forbidden_message);
9a7c4b43 2497 return false;
2498 }
2499 return true;
0a3b29ad 2500}
2501
e2ae55f2 2502/* This function is called when the DECLARATOR is processed. The TYPE
0be5f5cc 2503 was a type defined in the decl-specifiers. If it is invalid to
e2ae55f2 2504 define a type in the decl-specifiers for DECLARATOR, an error is
eef0ab03 2505 issued. TYPE_LOCATION is the location of TYPE and is used
2506 for error reporting. */
8172be22 2507
2508static void
3046c0a3 2509cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
eef0ab03 2510 tree type, location_t type_location)
8172be22 2511{
2512 /* [dcl.fct] forbids type definitions in return types.
2513 Unfortunately, it's not easy to know whether or not we are
2514 processing a return type until after the fact. */
2515 while (declarator
3046c0a3 2516 && (declarator->kind == cdk_pointer
2517 || declarator->kind == cdk_reference
2518 || declarator->kind == cdk_ptrmem))
2519 declarator = declarator->declarator;
8172be22 2520 if (declarator
e2ae55f2 2521 && declarator->kind == cdk_function)
2522 {
ccb59bb6 2523 error_at (type_location,
2524 "new types may not be defined in a return type");
5bcc316e 2525 inform (type_location,
2526 "(perhaps a semicolon is missing after the definition of %qT)",
2527 type);
e2ae55f2 2528 }
8172be22 2529}
2530
1157e9f0 2531/* A type-specifier (TYPE) has been parsed which cannot be followed by
2532 "<" in any valid C++ program. If the next token is indeed "<",
2533 issue a message warning the user about what appears to be an
eef0ab03 2534 invalid attempt to form a template-id. LOCATION is the location
2535 of the type-specifier (TYPE) */
1157e9f0 2536
2537static void
ccb84981 2538cp_parser_check_for_invalid_template_id (cp_parser* parser,
eef0ab03 2539 tree type, location_t location)
1157e9f0 2540{
19273cc2 2541 cp_token_position start = 0;
1157e9f0 2542
2543 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2544 {
2545 if (TYPE_P (type))
ccb59bb6 2546 error_at (location, "%qT is not a template", type);
1157e9f0 2547 else if (TREE_CODE (type) == IDENTIFIER_NODE)
ccb59bb6 2548 error_at (location, "%qE is not a template", type);
1157e9f0 2549 else
ccb59bb6 2550 error_at (location, "invalid template-id");
1157e9f0 2551 /* Remember the location of the invalid "<". */
efcbcf83 2552 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 2553 start = cp_lexer_token_position (parser->lexer, true);
1157e9f0 2554 /* Consume the "<". */
2555 cp_lexer_consume_token (parser->lexer);
2556 /* Parse the template arguments. */
2557 cp_parser_enclosed_template_argument_list (parser);
a5268b2f 2558 /* Permanently remove the invalid template arguments so that
1157e9f0 2559 this error message is not issued again. */
19273cc2 2560 if (start)
2561 cp_lexer_purge_tokens_after (parser->lexer, start);
1157e9f0 2562 }
2563}
2564
3938e0c2 2565/* If parsing an integral constant-expression, issue an error message
2566 about the fact that THING appeared and return true. Otherwise,
640aa28c 2567 return false. In either case, set
9031d10b 2568 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
5f6526e1 2569
3938e0c2 2570static bool
2571cp_parser_non_integral_constant_expression (cp_parser *parser,
c247dce0 2572 non_integral_constant thing)
5f6526e1 2573{
640aa28c 2574 parser->non_integral_constant_expression_p = true;
3938e0c2 2575 if (parser->integral_constant_expression_p)
2576 {
2577 if (!parser->allow_non_integral_constant_expression_p)
2578 {
c247dce0 2579 const char *msg = NULL;
2580 switch (thing)
2581 {
2582 case NIC_FLOAT:
2583 error ("floating-point literal "
2584 "cannot appear in a constant-expression");
2585 return true;
2586 case NIC_CAST:
2587 error ("a cast to a type other than an integral or "
2588 "enumeration type cannot appear in a "
2589 "constant-expression");
2590 return true;
2591 case NIC_TYPEID:
2592 error ("%<typeid%> operator "
2593 "cannot appear in a constant-expression");
2594 return true;
2595 case NIC_NCC:
2596 error ("non-constant compound literals "
2597 "cannot appear in a constant-expression");
2598 return true;
2599 case NIC_FUNC_CALL:
2600 error ("a function call "
2601 "cannot appear in a constant-expression");
2602 return true;
2603 case NIC_INC:
2604 error ("an increment "
2605 "cannot appear in a constant-expression");
2606 return true;
2607 case NIC_DEC:
2608 error ("an decrement "
2609 "cannot appear in a constant-expression");
2610 return true;
2611 case NIC_ARRAY_REF:
2612 error ("an array reference "
2613 "cannot appear in a constant-expression");
2614 return true;
2615 case NIC_ADDR_LABEL:
2616 error ("the address of a label "
2617 "cannot appear in a constant-expression");
2618 return true;
2619 case NIC_OVERLOADED:
2620 error ("calls to overloaded operators "
2621 "cannot appear in a constant-expression");
2622 return true;
2623 case NIC_ASSIGNMENT:
2624 error ("an assignment cannot appear in a constant-expression");
2625 return true;
2626 case NIC_COMMA:
2627 error ("a comma operator "
2628 "cannot appear in a constant-expression");
2629 return true;
2630 case NIC_CONSTRUCTOR:
2631 error ("a call to a constructor "
2632 "cannot appear in a constant-expression");
2633 return true;
2634 case NIC_THIS:
2635 msg = "this";
2636 break;
2637 case NIC_FUNC_NAME:
2638 msg = "__FUNCTION__";
2639 break;
2640 case NIC_PRETTY_FUNC:
2641 msg = "__PRETTY_FUNCTION__";
2642 break;
2643 case NIC_C99_FUNC:
2644 msg = "__func__";
2645 break;
2646 case NIC_VA_ARG:
2647 msg = "va_arg";
2648 break;
2649 case NIC_ARROW:
2650 msg = "->";
2651 break;
2652 case NIC_POINT:
2653 msg = ".";
2654 break;
2655 case NIC_STAR:
2656 msg = "*";
2657 break;
2658 case NIC_ADDR:
2659 msg = "&";
2660 break;
2661 case NIC_PREINCREMENT:
2662 msg = "++";
2663 break;
2664 case NIC_PREDECREMENT:
2665 msg = "--";
2666 break;
2667 case NIC_NEW:
2668 msg = "new";
2669 break;
2670 case NIC_DEL:
2671 msg = "delete";
2672 break;
2673 default:
2674 gcc_unreachable ();
2675 }
2676 if (msg)
2677 error ("%qs cannot appear in a constant-expression", msg);
3938e0c2 2678 return true;
2679 }
3938e0c2 2680 }
2681 return false;
5f6526e1 2682}
2683
d9da0685 2684/* Emit a diagnostic for an invalid type name. SCOPE is the
b41cf329 2685 qualifying scope (or NULL, if none) for ID. This function commits
2686 to the current active tentative parse, if any. (Otherwise, the
2687 problematic construct might be encountered again later, resulting
eef0ab03 2688 in duplicate error messages.) LOCATION is the location of ID. */
954ad420 2689
e00c3963 2690static void
ad9ae192 2691cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2692 tree scope, tree id,
eef0ab03 2693 location_t location)
bb93aad0 2694{
2695 tree decl, old_scope;
e00c3963 2696 /* Try to lookup the identifier. */
2697 old_scope = parser->scope;
2698 parser->scope = scope;
eef0ab03 2699 decl = cp_parser_lookup_name_simple (parser, id, location);
e00c3963 2700 parser->scope = old_scope;
2701 /* If the lookup found a template-name, it means that the user forgot
e4bc96e2 2702 to specify an argument list. Emit a useful error message. */
e00c3963 2703 if (TREE_CODE (decl) == TEMPLATE_DECL)
ccb59bb6 2704 error_at (location,
2705 "invalid use of template-name %qE without an argument list",
2706 decl);
dea225ee 2707 else if (TREE_CODE (id) == BIT_NOT_EXPR)
ccb59bb6 2708 error_at (location, "invalid use of destructor %qD as a type", id);
2b443576 2709 else if (TREE_CODE (decl) == TYPE_DECL)
2710 /* Something like 'unsigned A a;' */
ccb59bb6 2711 error_at (location, "invalid combination of multiple type-specifiers");
b62240d5 2712 else if (!parser->scope)
954ad420 2713 {
954ad420 2714 /* Issue an error message. */
ccb59bb6 2715 error_at (location, "%qE does not name a type", id);
954ad420 2716 /* If we're in a template class, it's possible that the user was
2717 referring to a type from a base class. For example:
2718
2719 template <typename T> struct A { typedef T X; };
2720 template <typename T> struct B : public A<T> { X x; };
2721
2722 The user should have said "typename A<T>::X". */
03d0aa7e 2723 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2724 inform (location, "C++0x %<constexpr%> only available with "
2725 "-std=c++0x or -std=gnu++0x");
2726 else if (processing_template_decl && current_class_type
2727 && TYPE_BINFO (current_class_type))
954ad420 2728 {
2729 tree b;
2730
2731 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2732 b;
2733 b = TREE_CHAIN (b))
2734 {
2735 tree base_type = BINFO_TYPE (b);
ccb84981 2736 if (CLASS_TYPE_P (base_type)
7e9a6a16 2737 && dependent_type_p (base_type))
954ad420 2738 {
2739 tree field;
2740 /* Go from a particular instantiation of the
2741 template (which will have an empty TYPE_FIELDs),
2742 to the main version. */
f0a4ea5b 2743 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
954ad420 2744 for (field = TYPE_FIELDS (base_type);
2745 field;
1767a056 2746 field = DECL_CHAIN (field))
954ad420 2747 if (TREE_CODE (field) == TYPE_DECL
e00c3963 2748 && DECL_NAME (field) == id)
954ad420 2749 {
5bcc316e 2750 inform (location,
2751 "(perhaps %<typename %T::%E%> was intended)",
2752 BINFO_TYPE (b), id);
954ad420 2753 break;
2754 }
2755 if (field)
2756 break;
2757 }
2758 }
2759 }
954ad420 2760 }
e00c3963 2761 /* Here we diagnose qualified-ids where the scope is actually correct,
2762 but the identifier does not resolve to a valid type name. */
b62240d5 2763 else if (parser->scope != error_mark_node)
e00c3963 2764 {
2765 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
ccb59bb6 2766 error_at (location, "%qE in namespace %qE does not name a type",
2767 id, parser->scope);
a70e3c37 2768 else if (CLASS_TYPE_P (parser->scope)
5570fae0 2769 && constructor_name_p (id, parser->scope))
2770 {
2771 /* A<T>::A<T>() */
2772 error_at (location, "%<%T::%E%> names the constructor, not"
2773 " the type", parser->scope, id);
2774 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2775 error_at (location, "and %qT has no template constructors",
2776 parser->scope);
2777 }
5bd9bf06 2778 else if (TYPE_P (parser->scope)
2779 && dependent_scope_p (parser->scope))
67484828 2780 error_at (location, "need %<typename%> before %<%T::%E%> because "
2781 "%qT is a dependent scope",
5bd9bf06 2782 parser->scope, id, parser->scope);
e00c3963 2783 else if (TYPE_P (parser->scope))
ccb59bb6 2784 error_at (location, "%qE in class %qT does not name a type",
2785 id, parser->scope);
e00c3963 2786 else
2e3e31d2 2787 gcc_unreachable ();
e00c3963 2788 }
b41cf329 2789 cp_parser_commit_to_tentative_parse (parser);
e00c3963 2790}
954ad420 2791
e00c3963 2792/* Check for a common situation where a type-name should be present,
2793 but is not, and issue a sensible error message. Returns true if an
2794 invalid type-name was detected.
ccb84981 2795
e00c3963 2796 The situation handled by this function are variable declarations of the
ccb84981 2797 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2798 Usually, `ID' should name a type, but if we got here it means that it
e00c3963 2799 does not. We try to emit the best possible error message depending on
074ab442 2800 how exactly the id-expression looks like. */
e00c3963 2801
2802static bool
2803cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2804{
2805 tree id;
ad9ae192 2806 cp_token *token = cp_lexer_peek_token (parser->lexer);
e00c3963 2807
6dd3a38d 2808 /* Avoid duplicate error about ambiguous lookup. */
2809 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2810 {
2811 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2812 if (next->type == CPP_NAME && next->ambiguous_p)
2813 goto out;
2814 }
2815
e00c3963 2816 cp_parser_parse_tentatively (parser);
ccb84981 2817 id = cp_parser_id_expression (parser,
e00c3963 2818 /*template_keyword_p=*/false,
2819 /*check_dependency_p=*/true,
2820 /*template_p=*/NULL,
197c9df7 2821 /*declarator_p=*/true,
130bb1d4 2822 /*optional_p=*/false);
67484828 2823 /* If the next token is a (, this is a function with no explicit return
2824 type, i.e. constructor, destructor or conversion op. */
2825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7525f769 2826 || TREE_CODE (id) == TYPE_DECL)
e00c3963 2827 {
2828 cp_parser_abort_tentative_parse (parser);
2829 return false;
2830 }
7525f769 2831 if (!cp_parser_parse_definitely (parser))
e00c3963 2832 return false;
2833
e00c3963 2834 /* Emit a diagnostic for the invalid type. */
ad9ae192 2835 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2836 id, token->location);
6dd3a38d 2837 out:
67484828 2838 /* If we aren't in the middle of a declarator (i.e. in a
2839 parameter-declaration-clause), skip to the end of the declaration;
2840 there's no point in trying to process it. */
2841 if (!parser->in_declarator_p)
2842 cp_parser_skip_to_end_of_block_or_statement (parser);
e00c3963 2843 return true;
954ad420 2844}
2845
ccb84981 2846/* Consume tokens up to, and including, the next non-nested closing `)'.
0986fa22 2847 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2848 are doing error recovery. Returns -1 if OR_COMMA is true and we
2849 found an unnested comma. */
0a3b29ad 2850
0986fa22 2851static int
2852cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
ccb84981 2853 bool recovering,
3d0f901b 2854 bool or_comma,
2855 bool consume_paren)
0a3b29ad 2856{
0986fa22 2857 unsigned paren_depth = 0;
2858 unsigned brace_depth = 0;
a8b75081 2859 unsigned square_depth = 0;
0a3b29ad 2860
efcbcf83 2861 if (recovering && !or_comma
2862 && cp_parser_uncommitted_to_tentative_parse_p (parser))
0986fa22 2863 return 0;
ccb84981 2864
0a3b29ad 2865 while (true)
2866 {
b75b98aa 2867 cp_token * token = cp_lexer_peek_token (parser->lexer);
ccb84981 2868
b75b98aa 2869 switch (token->type)
3fe7c943 2870 {
b75b98aa 2871 case CPP_EOF:
2872 case CPP_PRAGMA_EOL:
2873 /* If we've run out of tokens, then there is no closing `)'. */
2874 return 0;
0a3b29ad 2875
a8b75081 2876 /* This is good for lambda expression capture-lists. */
2877 case CPP_OPEN_SQUARE:
2878 ++square_depth;
2879 break;
2880 case CPP_CLOSE_SQUARE:
2881 if (!square_depth--)
2882 return 0;
2883 break;
2884
b75b98aa 2885 case CPP_SEMICOLON:
2886 /* This matches the processing in skip_to_end_of_statement. */
2887 if (!brace_depth)
2888 return 0;
2889 break;
ccb84981 2890
b75b98aa 2891 case CPP_OPEN_BRACE:
2892 ++brace_depth;
3fe7c943 2893 break;
b75b98aa 2894 case CPP_CLOSE_BRACE:
3d0f901b 2895 if (!brace_depth--)
b75b98aa 2896 return 0;
3fe7c943 2897 break;
ccb84981 2898
b75b98aa 2899 case CPP_COMMA:
a8b75081 2900 if (recovering && or_comma && !brace_depth && !paren_depth
2901 && !square_depth)
b75b98aa 2902 return -1;
2903 break;
2904
2905 case CPP_OPEN_PAREN:
2906 if (!brace_depth)
0986fa22 2907 ++paren_depth;
b75b98aa 2908 break;
2909
2910 case CPP_CLOSE_PAREN:
2911 if (!brace_depth && !paren_depth--)
3d0f901b 2912 {
2913 if (consume_paren)
2914 cp_lexer_consume_token (parser->lexer);
b75b98aa 2915 return 1;
3d0f901b 2916 }
b75b98aa 2917 break;
2918
2919 default:
2920 break;
0986fa22 2921 }
ccb84981 2922
3d0f901b 2923 /* Consume the token. */
2924 cp_lexer_consume_token (parser->lexer);
0a3b29ad 2925 }
2926}
2927
2928/* Consume tokens until we reach the end of the current statement.
2929 Normally, that will be just before consuming a `;'. However, if a
2930 non-nested `}' comes first, then we stop before consuming that. */
2931
2932static void
45baea8b 2933cp_parser_skip_to_end_of_statement (cp_parser* parser)
0a3b29ad 2934{
2935 unsigned nesting_depth = 0;
2936
2937 while (true)
2938 {
b75b98aa 2939 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 2940
b75b98aa 2941 switch (token->type)
0a3b29ad 2942 {
b75b98aa 2943 case CPP_EOF:
2944 case CPP_PRAGMA_EOL:
2945 /* If we've run out of tokens, stop. */
2946 return;
2947
2948 case CPP_SEMICOLON:
2949 /* If the next token is a `;', we have reached the end of the
2950 statement. */
2951 if (!nesting_depth)
2952 return;
2953 break;
2954
2955 case CPP_CLOSE_BRACE:
2956 /* If this is a non-nested '}', stop before consuming it.
0a3b29ad 2957 That way, when confronted with something like:
2958
ccb84981 2959 { 3 + }
0a3b29ad 2960
b75b98aa 2961 we stop before consuming the closing '}', even though we
0a3b29ad 2962 have not yet reached a `;'. */
2963 if (nesting_depth == 0)
b75b98aa 2964 return;
2965
2966 /* If it is the closing '}' for a block that we have
0a3b29ad 2967 scanned, stop -- but only after consuming the token.
2968 That way given:
2969
653e5405 2970 void f g () { ... }
0a3b29ad 2971 typedef int I;
2972
2973 we will stop after the body of the erroneously declared
2974 function, but before consuming the following `typedef'
2975 declaration. */
2976 if (--nesting_depth == 0)
2977 {
2978 cp_lexer_consume_token (parser->lexer);
b75b98aa 2979 return;
0a3b29ad 2980 }
b75b98aa 2981
2982 case CPP_OPEN_BRACE:
2983 ++nesting_depth;
2984 break;
2985
2986 default:
2987 break;
0a3b29ad 2988 }
b75b98aa 2989
0a3b29ad 2990 /* Consume the token. */
2991 cp_lexer_consume_token (parser->lexer);
2992 }
2993}
2994
cf91b86a 2995/* This function is called at the end of a statement or declaration.
2996 If the next token is a semicolon, it is consumed; otherwise, error
2997 recovery is attempted. */
2998
2999static void
3000cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3001{
3002 /* Look for the trailing `;'. */
c247dce0 3003 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
cf91b86a 3004 {
3005 /* If there is additional (erroneous) input, skip to the end of
3006 the statement. */
3007 cp_parser_skip_to_end_of_statement (parser);
3008 /* If the next token is now a `;', consume it. */
3009 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3010 cp_lexer_consume_token (parser->lexer);
3011 }
3012}
3013
0a3b29ad 3014/* Skip tokens until we have consumed an entire block, or until we
3015 have consumed a non-nested `;'. */
3016
3017static void
45baea8b 3018cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
0a3b29ad 3019{
c75ae97e 3020 int nesting_depth = 0;
0a3b29ad 3021
c75ae97e 3022 while (nesting_depth >= 0)
0a3b29ad 3023 {
c75ae97e 3024 cp_token *token = cp_lexer_peek_token (parser->lexer);
9031d10b 3025
c75ae97e 3026 switch (token->type)
0a3b29ad 3027 {
c75ae97e 3028 case CPP_EOF:
b75b98aa 3029 case CPP_PRAGMA_EOL:
c75ae97e 3030 /* If we've run out of tokens, stop. */
b75b98aa 3031 return;
c75ae97e 3032
3033 case CPP_SEMICOLON:
3034 /* Stop if this is an unnested ';'. */
3035 if (!nesting_depth)
3036 nesting_depth = -1;
3037 break;
3038
3039 case CPP_CLOSE_BRACE:
3040 /* Stop if this is an unnested '}', or closes the outermost
3041 nesting level. */
3042 nesting_depth--;
926c5baf 3043 if (nesting_depth < 0)
3044 return;
c75ae97e 3045 if (!nesting_depth)
3046 nesting_depth = -1;
3047 break;
9031d10b 3048
c75ae97e 3049 case CPP_OPEN_BRACE:
3050 /* Nest. */
3051 nesting_depth++;
3052 break;
3053
3054 default:
0a3b29ad 3055 break;
3056 }
9031d10b 3057
0a3b29ad 3058 /* Consume the token. */
c75ae97e 3059 cp_lexer_consume_token (parser->lexer);
0a3b29ad 3060 }
3061}
3062
3063/* Skip tokens until a non-nested closing curly brace is the next
9aad947b 3064 token, or there are no more tokens. Return true in the first case,
3065 false otherwise. */
0a3b29ad 3066
9aad947b 3067static bool
0a3b29ad 3068cp_parser_skip_to_closing_brace (cp_parser *parser)
3069{
3070 unsigned nesting_depth = 0;
3071
3072 while (true)
3073 {
b75b98aa 3074 cp_token *token = cp_lexer_peek_token (parser->lexer);
3075
3076 switch (token->type)
3077 {
3078 case CPP_EOF:
3079 case CPP_PRAGMA_EOL:
3080 /* If we've run out of tokens, stop. */
9aad947b 3081 return false;
b75b98aa 3082
3083 case CPP_CLOSE_BRACE:
3084 /* If the next token is a non-nested `}', then we have reached
3085 the end of the current block. */
3086 if (nesting_depth-- == 0)
9aad947b 3087 return true;
b75b98aa 3088 break;
3089
3090 case CPP_OPEN_BRACE:
3091 /* If it the next token is a `{', then we are entering a new
3092 block. Consume the entire block. */
3093 ++nesting_depth;
3094 break;
3095
3096 default:
3097 break;
3098 }
0a3b29ad 3099
0a3b29ad 3100 /* Consume the token. */
3101 cp_lexer_consume_token (parser->lexer);
3102 }
3103}
3104
b75b98aa 3105/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3106 parameter is the PRAGMA token, allowing us to purge the entire pragma
3107 sequence. */
3108
3109static void
3110cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3111{
3112 cp_token *token;
3113
3114 parser->lexer->in_pragma = false;
3115
3116 do
3117 token = cp_lexer_consume_token (parser->lexer);
3118 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3119
3120 /* Ensure that the pragma is not parsed again. */
3121 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3122}
3123
8487df40 3124/* Require pragma end of line, resyncing with it as necessary. The
3125 arguments are as for cp_parser_skip_to_pragma_eol. */
3126
3127static void
3128cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3129{
3130 parser->lexer->in_pragma = false;
c247dce0 3131 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
8487df40 3132 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3133}
3134
e00c3963 3135/* This is a simple wrapper around make_typename_type. When the id is
3136 an unresolved identifier node, we can provide a superior diagnostic
3137 using cp_parser_diagnose_invalid_type_name. */
3138
3139static tree
ad9ae192 3140cp_parser_make_typename_type (cp_parser *parser, tree scope,
3141 tree id, location_t id_location)
bb93aad0 3142{
3143 tree result;
3144 if (TREE_CODE (id) == IDENTIFIER_NODE)
3145 {
e2ae55f2 3146 result = make_typename_type (scope, id, typename_type,
074ab442 3147 /*complain=*/tf_none);
bb93aad0 3148 if (result == error_mark_node)
ad9ae192 3149 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
bb93aad0 3150 return result;
3151 }
e2ae55f2 3152 return make_typename_type (scope, id, typename_type, tf_error);
e00c3963 3153}
3154
63949b38 3155/* This is a wrapper around the
3156 make_{pointer,ptrmem,reference}_declarator functions that decides
3157 which one to call based on the CODE and CLASS_TYPE arguments. The
3158 CODE argument should be one of the values returned by
3159 cp_parser_ptr_operator. */
3160static cp_declarator *
3161cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3162 cp_cv_quals cv_qualifiers,
3163 cp_declarator *target)
3164{
416c300e 3165 if (code == ERROR_MARK)
3166 return cp_error_declarator;
3167
63949b38 3168 if (code == INDIRECT_REF)
3169 if (class_type == NULL_TREE)
3170 return make_pointer_declarator (cv_qualifiers, target);
3171 else
3172 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3173 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3174 return make_reference_declarator (cv_qualifiers, target, false);
3175 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3176 return make_reference_declarator (cv_qualifiers, target, true);
3177 gcc_unreachable ();
3178}
e00c3963 3179
0a3b29ad 3180/* Create a new C++ parser. */
3181
3182static cp_parser *
45baea8b 3183cp_parser_new (void)
0a3b29ad 3184{
3185 cp_parser *parser;
573aba85 3186 cp_lexer *lexer;
0a88af73 3187 unsigned i;
573aba85 3188
ba72912a 3189 /* cp_lexer_new_main is called before doing GC allocation because
573aba85 3190 cp_lexer_new_main might load a PCH file. */
3191 lexer = cp_lexer_new_main ();
0a3b29ad 3192
0a88af73 3193 /* Initialize the binops_by_token so that we can get the tree
3194 directly from the token. */
3195 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3196 binops_by_token[binops[i].token_type] = binops[i];
3197
ba72912a 3198 parser = ggc_alloc_cleared_cp_parser ();
573aba85 3199 parser->lexer = lexer;
0a3b29ad 3200 parser->context = cp_parser_context_new (NULL);
3201
3202 /* For now, we always accept GNU extensions. */
3203 parser->allow_gnu_extensions_p = 1;
3204
3205 /* The `>' token is a greater-than operator, not the end of a
3206 template-id. */
3207 parser->greater_than_is_operator_p = true;
3208
3209 parser->default_arg_ok_p = true;
ccb84981 3210
0a3b29ad 3211 /* We are not parsing a constant-expression. */
f47c1747 3212 parser->integral_constant_expression_p = false;
3213 parser->allow_non_integral_constant_expression_p = false;
3214 parser->non_integral_constant_expression_p = false;
0a3b29ad 3215
3216 /* Local variable names are not forbidden. */
3217 parser->local_variables_forbidden_p = false;
3218
755edffd 3219 /* We are not processing an `extern "C"' declaration. */
0a3b29ad 3220 parser->in_unbraced_linkage_specification_p = false;
3221
3222 /* We are not processing a declarator. */
3223 parser->in_declarator_p = false;
3224
92b128ed 3225 /* We are not processing a template-argument-list. */
3226 parser->in_template_argument_list_p = false;
3227
c3fbce20 3228 /* We are not in an iteration statement. */
8487df40 3229 parser->in_statement = 0;
c3fbce20 3230
3231 /* We are not in a switch statement. */
3232 parser->in_switch_statement_p = false;
3233
41f2d08e 3234 /* We are not parsing a type-id inside an expression. */
3235 parser->in_type_id_in_expr_p = false;
3236
93523877 3237 /* Declarations aren't implicitly extern "C". */
2e1f41a9 3238 parser->implicit_extern_c = false;
3239
00d26680 3240 /* String literals should be translated to the execution character set. */
3241 parser->translate_strings_p = true;
3242
0aeb1cc5 3243 /* We are not parsing a function body. */
3244 parser->in_function_body = false;
3245
0a3b29ad 3246 /* The unparsed function queue is empty. */
9177da82 3247 push_unparsed_function_queues (parser);
0a3b29ad 3248
3249 /* There are no classes being defined. */
3250 parser->num_classes_being_defined = 0;
3251
3252 /* No template parameters apply. */
3253 parser->num_template_parameter_lists = 0;
3254
3255 return parser;
3256}
3257
b9dd3954 3258/* Create a cp_lexer structure which will emit the tokens in CACHE
3259 and push it onto the parser's lexer stack. This is used for delayed
3260 parsing of in-class method bodies and default arguments, and should
3261 not be confused with tentative parsing. */
3262static void
3263cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3264{
3265 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3266 lexer->next = parser->lexer;
3267 parser->lexer = lexer;
3268
3269 /* Move the current source position to that of the first token in the
3270 new lexer. */
3271 cp_lexer_set_source_position_from_token (lexer->next_token);
3272}
3273
3274/* Pop the top lexer off the parser stack. This is never used for the
3275 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3276static void
3277cp_parser_pop_lexer (cp_parser *parser)
3278{
3279 cp_lexer *lexer = parser->lexer;
3280 parser->lexer = lexer->next;
3281 cp_lexer_destroy (lexer);
3282
3283 /* Put the current source position back where it was before this
3284 lexer was pushed. */
3285 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3286}
3287
0a3b29ad 3288/* Lexical conventions [gram.lex] */
3289
3290/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3291 identifier. */
3292
ccb84981 3293static tree
45baea8b 3294cp_parser_identifier (cp_parser* parser)
0a3b29ad 3295{
3296 cp_token *token;
3297
3298 /* Look for the identifier. */
c247dce0 3299 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
0a3b29ad 3300 /* Return the value. */
3369eb76 3301 return token ? token->u.value : error_mark_node;
0a3b29ad 3302}
3303
00d26680 3304/* Parse a sequence of adjacent string constants. Returns a
3305 TREE_STRING representing the combined, nul-terminated string
3306 constant. If TRANSLATE is true, translate the string to the
3307 execution character set. If WIDE_OK is true, a wide string is
3308 invalid here.
3309
3310 C++98 [lex.string] says that if a narrow string literal token is
3311 adjacent to a wide string literal token, the behavior is undefined.
3312 However, C99 6.4.5p4 says that this results in a wide string literal.
3313 We follow C99 here, for consistency with the C front end.
3314
3315 This code is largely lifted from lex_string() in c-lex.c.
3316
3317 FUTURE: ObjC++ will need to handle @-strings here. */
3318static tree
3319cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3320{
3321 tree value;
00d26680 3322 size_t count;
3323 struct obstack str_ob;
3324 cpp_string str, istr, *strs;
3325 cp_token *tok;
924bbf02 3326 enum cpp_ttype type;
00d26680 3327
3328 tok = cp_lexer_peek_token (parser->lexer);
3329 if (!cp_parser_is_string_literal (tok))
3330 {
3331 cp_parser_error (parser, "expected string-literal");
3332 return error_mark_node;
3333 }
3334
924bbf02 3335 type = tok->type;
3336
2e533eb1 3337 /* Try to avoid the overhead of creating and destroying an obstack
00d26680 3338 for the common case of just one string. */
b9dd3954 3339 if (!cp_parser_is_string_literal
3340 (cp_lexer_peek_nth_token (parser->lexer, 2)))
00d26680 3341 {
b9dd3954 3342 cp_lexer_consume_token (parser->lexer);
3343
3369eb76 3344 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3345 str.len = TREE_STRING_LENGTH (tok->u.value);
00d26680 3346 count = 1;
00d26680 3347
3348 strs = &str;
3349 }
3350 else
3351 {
3352 gcc_obstack_init (&str_ob);
3353 count = 0;
3354
3355 do
3356 {
b9dd3954 3357 cp_lexer_consume_token (parser->lexer);
00d26680 3358 count++;
dea3189b 3359 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3369eb76 3360 str.len = TREE_STRING_LENGTH (tok->u.value);
924bbf02 3361
3362 if (type != tok->type)
3363 {
3364 if (type == CPP_STRING)
3365 type = tok->type;
3366 else if (tok->type != CPP_STRING)
ccb59bb6 3367 error_at (tok->location,
3368 "unsupported non-standard concatenation "
3369 "of string literals");
924bbf02 3370 }
00d26680 3371
3372 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3373
b9dd3954 3374 tok = cp_lexer_peek_token (parser->lexer);
00d26680 3375 }
3376 while (cp_parser_is_string_literal (tok));
3377
3378 strs = (cpp_string *) obstack_finish (&str_ob);
3379 }
3380
924bbf02 3381 if (type != CPP_STRING && !wide_ok)
00d26680 3382 {
3383 cp_parser_error (parser, "a wide string is invalid in this context");
924bbf02 3384 type = CPP_STRING;
00d26680 3385 }
3386
3387 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
924bbf02 3388 (parse_in, strs, count, &istr, type))
00d26680 3389 {
c21f4fcd 3390 value = build_string (istr.len, (const char *)istr.text);
e47a6f81 3391 free (CONST_CAST (unsigned char *, istr.text));
00d26680 3392
924bbf02 3393 switch (type)
3394 {
3395 default:
3396 case CPP_STRING:
538ba11a 3397 case CPP_UTF8STRING:
924bbf02 3398 TREE_TYPE (value) = char_array_type_node;
3399 break;
3400 case CPP_STRING16:
3401 TREE_TYPE (value) = char16_array_type_node;
3402 break;
3403 case CPP_STRING32:
3404 TREE_TYPE (value) = char32_array_type_node;
3405 break;
3406 case CPP_WSTRING:
3407 TREE_TYPE (value) = wchar_array_type_node;
3408 break;
3409 }
3410
00d26680 3411 value = fix_string_type (value);
3412 }
3413 else
3414 /* cpp_interpret_string has issued an error. */
3415 value = error_mark_node;
3416
3417 if (count > 1)
3418 obstack_free (&str_ob, 0);
3419
3420 return value;
3421}
3422
3423
0a3b29ad 3424/* Basic concepts [gram.basic] */
3425
3426/* Parse a translation-unit.
3427
3428 translation-unit:
ccb84981 3429 declaration-seq [opt]
0a3b29ad 3430
3431 Returns TRUE if all went well. */
3432
3433static bool
45baea8b 3434cp_parser_translation_unit (cp_parser* parser)
0a3b29ad 3435{
3046c0a3 3436 /* The address of the first non-permanent object on the declarator
3437 obstack. */
3438 static void *declarator_obstack_base;
3439
3440 bool success;
207355ad 3441
3046c0a3 3442 /* Create the declarator obstack, if necessary. */
3443 if (!cp_error_declarator)
3444 {
3445 gcc_obstack_init (&declarator_obstack);
3446 /* Create the error declarator. */
3447 cp_error_declarator = make_declarator (cdk_error);
3448 /* Create the empty parameter list. */
4b9b2871 3449 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3046c0a3 3450 /* Remember where the base of the declarator obstack lies. */
3451 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3452 }
3453
d88386ce 3454 cp_parser_declaration_seq_opt (parser);
074ab442 3455
d88386ce 3456 /* If there are no tokens left then all went well. */
3457 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0a3b29ad 3458 {
d88386ce 3459 /* Get rid of the token array; we don't need it any more. */
3460 cp_lexer_destroy (parser->lexer);
3461 parser->lexer = NULL;
074ab442 3462
d88386ce 3463 /* This file might have been a context that's implicitly extern
074ab442 3464 "C". If so, pop the lang context. (Only relevant for PCH.) */
d88386ce 3465 if (parser->implicit_extern_c)
074ab442 3466 {
3467 pop_lang_context ();
3468 parser->implicit_extern_c = false;
3469 }
3470
d88386ce 3471 /* Finish up. */
3472 finish_translation_unit ();
074ab442 3473
d88386ce 3474 success = true;
0a3b29ad 3475 }
d88386ce 3476 else
3477 {
3478 cp_parser_error (parser, "expected declaration");
3479 success = false;
3480 }
074ab442 3481
3046c0a3 3482 /* Make sure the declarator obstack was fully cleaned up. */
b4df430b 3483 gcc_assert (obstack_next_free (&declarator_obstack)
3484 == declarator_obstack_base);
0a3b29ad 3485
3486 /* All went well. */
3046c0a3 3487 return success;
0a3b29ad 3488}
3489
3490/* Expressions [gram.expr] */
3491
3492/* Parse a primary-expression.
3493
3494 primary-expression:
3495 literal
3496 this
3497 ( expression )
3498 id-expression
3499
3500 GNU Extensions:
3501
3502 primary-expression:
3503 ( compound-statement )
3504 __builtin_va_arg ( assignment-expression , type-id )
e3a7cc2b 3505 __builtin_offsetof ( type-id , offsetof-expression )
0a3b29ad 3506
481451eb 3507 C++ Extensions:
3508 __has_nothrow_assign ( type-id )
3509 __has_nothrow_constructor ( type-id )
3510 __has_nothrow_copy ( type-id )
3511 __has_trivial_assign ( type-id )
3512 __has_trivial_constructor ( type-id )
3513 __has_trivial_copy ( type-id )
3514 __has_trivial_destructor ( type-id )
3515 __has_virtual_destructor ( type-id )
3516 __is_abstract ( type-id )
3517 __is_base_of ( type-id , type-id )
3518 __is_class ( type-id )
3519 __is_convertible_to ( type-id , type-id )
3520 __is_empty ( type-id )
3521 __is_enum ( type-id )
3522 __is_pod ( type-id )
3523 __is_polymorphic ( type-id )
3524 __is_union ( type-id )
3525
7a4e126b 3526 Objective-C++ Extension:
3527
3528 primary-expression:
3529 objc-expression
3530
0a3b29ad 3531 literal:
3532 __null
3533
fbb01da7 3534 ADDRESS_P is true iff this expression was immediately preceded by
3535 "&" and therefore might denote a pointer-to-member. CAST_P is true
3536 iff this expression is the target of a cast. TEMPLATE_ARG_P is
149fdb98 3537 true iff this expression is a template argument.
640aa28c 3538
fbb01da7 3539 Returns a representation of the expression. Upon return, *IDK
3540 indicates what kind of id-expression (if any) was present. */
0a3b29ad 3541
3542static tree
ccb84981 3543cp_parser_primary_expression (cp_parser *parser,
fbb01da7 3544 bool address_p,
640aa28c 3545 bool cast_p,
fbb01da7 3546 bool template_arg_p,
3547 cp_id_kind *idk)
0a3b29ad 3548{
ad9ae192 3549 cp_token *token = NULL;
0a3b29ad 3550
3551 /* Assume the primary expression is not an id-expression. */
0886adbc 3552 *idk = CP_ID_KIND_NONE;
0a3b29ad 3553
3554 /* Peek at the next token. */
3555 token = cp_lexer_peek_token (parser->lexer);
3556 switch (token->type)
3557 {
3558 /* literal:
3559 integer-literal
3560 character-literal
3561 floating-literal
3562 string-literal
3563 boolean-literal */
3564 case CPP_CHAR:
924bbf02 3565 case CPP_CHAR16:
3566 case CPP_CHAR32:
0a3b29ad 3567 case CPP_WCHAR:
0a3b29ad 3568 case CPP_NUMBER:
3569 token = cp_lexer_consume_token (parser->lexer);
14e882ea 3570 if (TREE_CODE (token->u.value) == FIXED_CST)
3571 {
ccb59bb6 3572 error_at (token->location,
3573 "fixed-point types not supported in C++");
14e882ea 3574 return error_mark_node;
3575 }
640aa28c 3576 /* Floating-point literals are only allowed in an integral
3577 constant expression if they are cast to an integral or
3578 enumeration type. */
3369eb76 3579 if (TREE_CODE (token->u.value) == REAL_CST
b68a9fcf 3580 && parser->integral_constant_expression_p
3581 && pedantic)
640aa28c 3582 {
3583 /* CAST_P will be set even in invalid code like "int(2.7 +
3584 ...)". Therefore, we have to check that the next token
3585 is sure to end the cast. */
3586 if (cast_p)
3587 {
3588 cp_token *next_token;
3589
3590 next_token = cp_lexer_peek_token (parser->lexer);
3591 if (/* The comma at the end of an
3592 enumerator-definition. */
3593 next_token->type != CPP_COMMA
3594 /* The curly brace at the end of an enum-specifier. */
3595 && next_token->type != CPP_CLOSE_BRACE
3596 /* The end of a statement. */
3597 && next_token->type != CPP_SEMICOLON
3598 /* The end of the cast-expression. */
3599 && next_token->type != CPP_CLOSE_PAREN
3600 /* The end of an array bound. */
c6e71399 3601 && next_token->type != CPP_CLOSE_SQUARE
3602 /* The closing ">" in a template-argument-list. */
3603 && (next_token->type != CPP_GREATER
56471494 3604 || parser->greater_than_is_operator_p)
3605 /* C++0x only: A ">>" treated like two ">" tokens,
3606 in a template-argument-list. */
3607 && (next_token->type != CPP_RSHIFT
6dcdb5de 3608 || (cxx_dialect == cxx98)
c6e71399 3609 || parser->greater_than_is_operator_p))
640aa28c 3610 cast_p = false;
3611 }
3612
3613 /* If we are within a cast, then the constraint that the
3614 cast is to an integral or enumeration type will be
3615 checked at that point. If we are not within a cast, then
3616 this code is invalid. */
3617 if (!cast_p)
c247dce0 3618 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
640aa28c 3619 }
3369eb76 3620 return token->u.value;
0a3b29ad 3621
3fe7c943 3622 case CPP_STRING:
924bbf02 3623 case CPP_STRING16:
3624 case CPP_STRING32:
3fe7c943 3625 case CPP_WSTRING:
538ba11a 3626 case CPP_UTF8STRING:
00d26680 3627 /* ??? Should wide strings be allowed when parser->translate_strings_p
653e5405 3628 is false (i.e. in attributes)? If not, we can kill the third
00d26680 3629 argument to cp_parser_string_literal. */
3630 return cp_parser_string_literal (parser,
3631 parser->translate_strings_p,
3632 true);
3fe7c943 3633
0a3b29ad 3634 case CPP_OPEN_PAREN:
3635 {
3636 tree expr;
3637 bool saved_greater_than_is_operator_p;
3638
3639 /* Consume the `('. */
3640 cp_lexer_consume_token (parser->lexer);
3641 /* Within a parenthesized expression, a `>' token is always
3642 the greater-than operator. */
ccb84981 3643 saved_greater_than_is_operator_p
0a3b29ad 3644 = parser->greater_than_is_operator_p;
3645 parser->greater_than_is_operator_p = true;
3646 /* If we see `( { ' then we are looking at the beginning of
3647 a GNU statement-expression. */
3648 if (cp_parser_allow_gnu_extensions_p (parser)
3649 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3650 {
3651 /* Statement-expressions are not allowed by the standard. */
21ca8540 3652 pedwarn (token->location, OPT_pedantic,
3653 "ISO C++ forbids braced-groups within expressions");
ccb84981 3654
0a3b29ad 3655 /* And they're not allowed outside of a function-body; you
3656 cannot, for example, write:
ccb84981 3657
653e5405 3658 int i = ({ int j = 3; j + 1; });
ccb84981 3659
0a3b29ad 3660 at class or namespace scope. */
9184d665 3661 if (!parser->in_function_body
3662 || parser->in_template_argument_list_p)
4be86438 3663 {
ccb59bb6 3664 error_at (token->location,
3665 "statement-expressions are not allowed outside "
3666 "functions nor in template-argument lists");
4be86438 3667 cp_parser_skip_to_end_of_block_or_statement (parser);
3668 expr = error_mark_node;
3669 }
3670 else
3671 {
3672 /* Start the statement-expression. */
3673 expr = begin_stmt_expr ();
3674 /* Parse the compound-statement. */
3675 cp_parser_compound_statement (parser, expr, false);
3676 /* Finish up. */
3677 expr = finish_stmt_expr (expr, false);
3678 }
0a3b29ad 3679 }
3680 else
3681 {
3682 /* Parse the parenthesized expression. */
98b326fd 3683 expr = cp_parser_expression (parser, cast_p, idk);
0a3b29ad 3684 /* Let the front end know that this expression was
3685 enclosed in parentheses. This matters in case, for
3686 example, the expression is of the form `A::B', since
3687 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3688 not. */
3689 finish_parenthesized_expr (expr);
3690 }
3691 /* The `>' token might be the end of a template-id or
3692 template-parameter-list now. */
ccb84981 3693 parser->greater_than_is_operator_p
0a3b29ad 3694 = saved_greater_than_is_operator_p;
3695 /* Consume the `)'. */
c247dce0 3696 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
0a3b29ad 3697 cp_parser_skip_to_end_of_statement (parser);
3698
3699 return expr;
3700 }
3701
a8b75081 3702 case CPP_OPEN_SQUARE:
3703 if (c_dialect_objc ())
3704 /* We have an Objective-C++ message. */
3705 return cp_parser_objc_expression (parser);
bf8d19fe 3706 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
a8b75081 3707 return cp_parser_lambda_expression (parser);
3708
3709 case CPP_OBJC_STRING:
3710 if (c_dialect_objc ())
3711 /* We have an Objective-C++ string literal. */
3712 return cp_parser_objc_expression (parser);
3713 cp_parser_error (parser, "expected primary-expression");
3714 return error_mark_node;
3715
0a3b29ad 3716 case CPP_KEYWORD:
3717 switch (token->keyword)
3718 {
3719 /* These two are the boolean literals. */
3720 case RID_TRUE:
3721 cp_lexer_consume_token (parser->lexer);
3722 return boolean_true_node;
3723 case RID_FALSE:
3724 cp_lexer_consume_token (parser->lexer);
3725 return boolean_false_node;
ccb84981 3726
0a3b29ad 3727 /* The `__null' literal. */
3728 case RID_NULL:
3729 cp_lexer_consume_token (parser->lexer);
3730 return null_node;
3731
6fe11077 3732 /* The `nullptr' literal. */
3733 case RID_NULLPTR:
3734 cp_lexer_consume_token (parser->lexer);
3735 return nullptr_node;
3736
0a3b29ad 3737 /* Recognize the `this' keyword. */
3738 case RID_THIS:
3739 cp_lexer_consume_token (parser->lexer);
3740 if (parser->local_variables_forbidden_p)
3741 {
ccb59bb6 3742 error_at (token->location,
3743 "%<this%> may not be used in this context");
0a3b29ad 3744 return error_mark_node;
3745 }
5f6526e1 3746 /* Pointers cannot appear in constant-expressions. */
c247dce0 3747 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3938e0c2 3748 return error_mark_node;
0a3b29ad 3749 return finish_this_expr ();
3750
3751 /* The `operator' keyword can be the beginning of an
3752 id-expression. */
3753 case RID_OPERATOR:
3754 goto id_expression;
3755
3756 case RID_FUNCTION_NAME:
3757 case RID_PRETTY_FUNCTION_NAME:
3758 case RID_C99_FUNCTION_NAME:
a457170d 3759 {
c247dce0 3760 non_integral_constant name;
a457170d 3761
3762 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3763 __func__ are the names of variables -- but they are
3764 treated specially. Therefore, they are handled here,
3765 rather than relying on the generic id-expression logic
3766 below. Grammatically, these names are id-expressions.
3767
3768 Consume the token. */
3769 token = cp_lexer_consume_token (parser->lexer);
3770
3771 switch (token->keyword)
3772 {
3773 case RID_FUNCTION_NAME:
c247dce0 3774 name = NIC_FUNC_NAME;
a457170d 3775 break;
3776 case RID_PRETTY_FUNCTION_NAME:
c247dce0 3777 name = NIC_PRETTY_FUNC;
a457170d 3778 break;
3779 case RID_C99_FUNCTION_NAME:
c247dce0 3780 name = NIC_C99_FUNC;
a457170d 3781 break;
3782 default:
3783 gcc_unreachable ();
3784 }
3785
3786 if (cp_parser_non_integral_constant_expression (parser, name))
3787 return error_mark_node;
3788
3789 /* Look up the name. */
3790 return finish_fname (token->u.value);
3791 }
0a3b29ad 3792
3793 case RID_VA_ARG:
3794 {
3795 tree expression;
3796 tree type;
3797
3798 /* The `__builtin_va_arg' construct is used to handle
3799 `va_arg'. Consume the `__builtin_va_arg' token. */
3800 cp_lexer_consume_token (parser->lexer);
3801 /* Look for the opening `('. */
c247dce0 3802 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 3803 /* Now, parse the assignment-expression. */
640aa28c 3804 expression = cp_parser_assignment_expression (parser,
98b326fd 3805 /*cast_p=*/false, NULL);
0a3b29ad 3806 /* Look for the `,'. */
c247dce0 3807 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
0a3b29ad 3808 /* Parse the type-id. */
3809 type = cp_parser_type_id (parser);
3810 /* Look for the closing `)'. */
c247dce0 3811 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5f6526e1 3812 /* Using `va_arg' in a constant-expression is not
3813 allowed. */
c61e1212 3814 if (cp_parser_non_integral_constant_expression (parser,
3815 NIC_VA_ARG))
3938e0c2 3816 return error_mark_node;
0a3b29ad 3817 return build_x_va_arg (expression, type);
3818 }
3819
30b87eb6 3820 case RID_OFFSETOF:
43bf5d72 3821 return cp_parser_builtin_offsetof (parser);
30b87eb6 3822
481451eb 3823 case RID_HAS_NOTHROW_ASSIGN:
3824 case RID_HAS_NOTHROW_CONSTRUCTOR:
3825 case RID_HAS_NOTHROW_COPY:
3826 case RID_HAS_TRIVIAL_ASSIGN:
3827 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3828 case RID_HAS_TRIVIAL_COPY:
3829 case RID_HAS_TRIVIAL_DESTRUCTOR:
3830 case RID_HAS_VIRTUAL_DESTRUCTOR:
3831 case RID_IS_ABSTRACT:
3832 case RID_IS_BASE_OF:
3833 case RID_IS_CLASS:
3834 case RID_IS_CONVERTIBLE_TO:
3835 case RID_IS_EMPTY:
3836 case RID_IS_ENUM:
3837 case RID_IS_POD:
3838 case RID_IS_POLYMORPHIC:
c1c67b4f 3839 case RID_IS_STD_LAYOUT:
3840 case RID_IS_TRIVIAL:
481451eb 3841 case RID_IS_UNION:
5290e253 3842 case RID_IS_LITERAL_TYPE:
481451eb 3843 return cp_parser_trait_expr (parser, token->keyword);
3844
3845 /* Objective-C++ expressions. */
7a4e126b 3846 case RID_AT_ENCODE:
3847 case RID_AT_PROTOCOL:
3848 case RID_AT_SELECTOR:
3849 return cp_parser_objc_expression (parser);
3850
45a7aa40 3851 case RID_TEMPLATE:
3852 if (parser->in_function_body
3853 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3854 == CPP_LESS))
3855 {
3856 error_at (token->location,
3857 "a template declaration cannot appear at block scope");
3858 cp_parser_skip_to_end_of_block_or_statement (parser);
3859 return error_mark_node;
3860 }
0a3b29ad 3861 default:
3862 cp_parser_error (parser, "expected primary-expression");
3863 return error_mark_node;
3864 }
0a3b29ad 3865
3866 /* An id-expression can start with either an identifier, a
3867 `::' as the beginning of a qualified-id, or the "operator"
3868 keyword. */
3869 case CPP_NAME:
3870 case CPP_SCOPE:
3871 case CPP_TEMPLATE_ID:
3872 case CPP_NESTED_NAME_SPECIFIER:
3873 {
3874 tree id_expression;
3875 tree decl;
0886adbc 3876 const char *error_msg;
fbb01da7 3877 bool template_p;
3878 bool done;
ad9ae192 3879 cp_token *id_expr_token;
0a3b29ad 3880
3881 id_expression:
3882 /* Parse the id-expression. */
ccb84981 3883 id_expression
3884 = cp_parser_id_expression (parser,
0a3b29ad 3885 /*template_keyword_p=*/false,
3886 /*check_dependency_p=*/true,
fbb01da7 3887 &template_p,
197c9df7 3888 /*declarator_p=*/false,
130bb1d4 3889 /*optional_p=*/false);
0a3b29ad 3890 if (id_expression == error_mark_node)
3891 return error_mark_node;
ad9ae192 3892 id_expr_token = token;
fbb01da7 3893 token = cp_lexer_peek_token (parser->lexer);
3894 done = (token->type != CPP_OPEN_SQUARE
3895 && token->type != CPP_OPEN_PAREN
3896 && token->type != CPP_DOT
3897 && token->type != CPP_DEREF
3898 && token->type != CPP_PLUS_PLUS
3899 && token->type != CPP_MINUS_MINUS);
0a3b29ad 3900 /* If we have a template-id, then no further lookup is
3901 required. If the template-id was for a template-class, we
3902 will sometimes have a TYPE_DECL at this point. */
fbb01da7 3903 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3904 || TREE_CODE (id_expression) == TYPE_DECL)
0a3b29ad 3905 decl = id_expression;
3906 /* Look up the name. */
ccb84981 3907 else
0a3b29ad 3908 {
b62240d5 3909 tree ambiguous_decls;
2cdbcd51 3910
627895ac 3911 /* If we already know that this lookup is ambiguous, then
3912 we've already issued an error message; there's no reason
3913 to check again. */
3914 if (id_expr_token->type == CPP_NAME
3915 && id_expr_token->ambiguous_p)
3916 {
3917 cp_parser_simulate_error (parser);
3918 return error_mark_node;
3919 }
3920
2cdbcd51 3921 decl = cp_parser_lookup_name (parser, id_expression,
e2ae55f2 3922 none_type,
fbb01da7 3923 template_p,
2cdbcd51 3924 /*is_namespace=*/false,
3925 /*check_dependency=*/true,
ad9ae192 3926 &ambiguous_decls,
3927 id_expr_token->location);
2cdbcd51 3928 /* If the lookup was ambiguous, an error will already have
3929 been issued. */
b62240d5 3930 if (ambiguous_decls)
2cdbcd51 3931 return error_mark_node;
7a4e126b 3932
b0d0931f 3933 /* In Objective-C++, we may have an Objective-C 2.0
3934 dot-syntax for classes here. */
3935 if (c_dialect_objc ()
3936 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3937 && TREE_CODE (decl) == TYPE_DECL
3938 && objc_is_class_name (decl))
3939 {
3940 tree component;
3941 cp_lexer_consume_token (parser->lexer);
3942 component = cp_parser_identifier (parser);
3943 if (component == error_mark_node)
3944 return error_mark_node;
3945
3946 return objc_build_class_component_ref (id_expression, component);
3947 }
3948
7a4e126b 3949 /* In Objective-C++, an instance variable (ivar) may be preferred
3950 to whatever cp_parser_lookup_name() found. */
3951 decl = objc_lookup_ivar (decl, id_expression);
3952
0a3b29ad 3953 /* If name lookup gives us a SCOPE_REF, then the
fbb01da7 3954 qualifying scope was dependent. */
0a3b29ad 3955 if (TREE_CODE (decl) == SCOPE_REF)
62116ec3 3956 {
3957 /* At this point, we do not know if DECL is a valid
3958 integral constant expression. We assume that it is
3959 in fact such an expression, so that code like:
3960
3961 template <int N> struct A {
3962 int a[B<N>::i];
3963 };
3964
3965 is accepted. At template-instantiation time, we
3966 will check that B<N>::i is actually a constant. */
3967 return decl;
3968 }
0a3b29ad 3969 /* Check to see if DECL is a local variable in a context
3970 where that is forbidden. */
3971 if (parser->local_variables_forbidden_p
3972 && local_variable_p (decl))
3973 {
3974 /* It might be that we only found DECL because we are
3975 trying to be generous with pre-ISO scoping rules.
3976 For example, consider:
3977
3978 int i;
3979 void g() {
3980 for (int i = 0; i < 10; ++i) {}
3981 extern void f(int j = i);
3982 }
3983
ccb84981 3984 Here, name look up will originally find the out
0a3b29ad 3985 of scope `i'. We need to issue a warning message,
3986 but then use the global `i'. */
3987 decl = check_for_out_of_scope_variable (decl);
3988 if (local_variable_p (decl))
3989 {
ccb59bb6 3990 error_at (id_expr_token->location,
3991 "local variable %qD may not appear in this context",
3992 decl);
0a3b29ad 3993 return error_mark_node;
3994 }
3995 }
f3b70d2f 3996 }
ccb84981 3997
074ab442 3998 decl = (finish_id_expression
fbb01da7 3999 (id_expression, decl, parser->scope,
4000 idk,
4001 parser->integral_constant_expression_p,
4002 parser->allow_non_integral_constant_expression_p,
4003 &parser->non_integral_constant_expression_p,
4004 template_p, done, address_p,
4005 template_arg_p,
ad9ae192 4006 &error_msg,
4007 id_expr_token->location));
0886adbc 4008 if (error_msg)
4009 cp_parser_error (parser, error_msg);
0a3b29ad 4010 return decl;
4011 }
4012
4013 /* Anything else is an error. */
4014 default:
4015 cp_parser_error (parser, "expected primary-expression");
4016 return error_mark_node;
4017 }
4018}
4019
4020/* Parse an id-expression.
4021
4022 id-expression:
4023 unqualified-id
4024 qualified-id
4025
4026 qualified-id:
4027 :: [opt] nested-name-specifier template [opt] unqualified-id
4028 :: identifier
4029 :: operator-function-id
4030 :: template-id
4031
4032 Return a representation of the unqualified portion of the
4033 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4034 a `::' or nested-name-specifier.
4035
4036 Often, if the id-expression was a qualified-id, the caller will
4037 want to make a SCOPE_REF to represent the qualified-id. This
4038 function does not do this in order to avoid wastefully creating
4039 SCOPE_REFs when they are not required.
4040
0a3b29ad 4041 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4042 `template' keyword.
4043
4044 If CHECK_DEPENDENCY_P is false, then names are looked up inside
ccb84981 4045 uninstantiated templates.
0a3b29ad 4046
a210e1ca 4047 If *TEMPLATE_P is non-NULL, it is set to true iff the
0a3b29ad 4048 `template' keyword is used to explicitly indicate that the entity
ccb84981 4049 named is a template.
899cc6e8 4050
4051 If DECLARATOR_P is true, the id-expression is appearing as part of
63eff20d 4052 a declarator, rather than as part of an expression. */
0a3b29ad 4053
4054static tree
4055cp_parser_id_expression (cp_parser *parser,
4056 bool template_keyword_p,
4057 bool check_dependency_p,
899cc6e8 4058 bool *template_p,
197c9df7 4059 bool declarator_p,
130bb1d4 4060 bool optional_p)
0a3b29ad 4061{
4062 bool global_scope_p;
4063 bool nested_name_specifier_p;
4064
4065 /* Assume the `template' keyword was not used. */
4066 if (template_p)
fbb01da7 4067 *template_p = template_keyword_p;
0a3b29ad 4068
4069 /* Look for the optional `::' operator. */
ccb84981 4070 global_scope_p
130bb1d4 4071 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
0a3b29ad 4072 != NULL_TREE);
4073 /* Look for the optional nested-name-specifier. */
ccb84981 4074 nested_name_specifier_p
0a3b29ad 4075 = (cp_parser_nested_name_specifier_opt (parser,
4076 /*typename_keyword_p=*/false,
4077 check_dependency_p,
3d0f901b 4078 /*type_p=*/false,
0078a5e8 4079 declarator_p)
0a3b29ad 4080 != NULL_TREE);
4081 /* If there is a nested-name-specifier, then we are looking at
4082 the first qualified-id production. */
4083 if (nested_name_specifier_p)
4084 {
4085 tree saved_scope;
4086 tree saved_object_scope;
4087 tree saved_qualifying_scope;
4088 tree unqualified_id;
4089 bool is_template;
4090
4091 /* See if the next token is the `template' keyword. */
4092 if (!template_p)
4093 template_p = &is_template;
4094 *template_p = cp_parser_optional_template_keyword (parser);
4095 /* Name lookup we do during the processing of the
4096 unqualified-id might obliterate SCOPE. */
4097 saved_scope = parser->scope;
4098 saved_object_scope = parser->object_scope;
4099 saved_qualifying_scope = parser->qualifying_scope;
4100 /* Process the final unqualified-id. */
4101 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
899cc6e8 4102 check_dependency_p,
197c9df7 4103 declarator_p,
130bb1d4 4104 /*optional_p=*/false);
0a3b29ad 4105 /* Restore the SAVED_SCOPE for our caller. */
4106 parser->scope = saved_scope;
4107 parser->object_scope = saved_object_scope;
4108 parser->qualifying_scope = saved_qualifying_scope;
4109
4110 return unqualified_id;
4111 }
4112 /* Otherwise, if we are in global scope, then we are looking at one
4113 of the other qualified-id productions. */
4114 else if (global_scope_p)
4115 {
4116 cp_token *token;
4117 tree id;
4118
2c593bd0 4119 /* Peek at the next token. */
4120 token = cp_lexer_peek_token (parser->lexer);
4121
4122 /* If it's an identifier, and the next token is not a "<", then
4123 we can avoid the template-id case. This is an optimization
4124 for this common case. */
ccb84981 4125 if (token->type == CPP_NAME
4126 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 4127 (parser, 2))
2c593bd0 4128 return cp_parser_identifier (parser);
4129
0a3b29ad 4130 cp_parser_parse_tentatively (parser);
4131 /* Try a template-id. */
ccb84981 4132 id = cp_parser_template_id (parser,
0a3b29ad 4133 /*template_keyword_p=*/false,
3d0f901b 4134 /*check_dependency_p=*/true,
4135 declarator_p);
0a3b29ad 4136 /* If that worked, we're done. */
4137 if (cp_parser_parse_definitely (parser))
4138 return id;
4139
2c593bd0 4140 /* Peek at the next token. (Changes in the token buffer may
4141 have invalidated the pointer obtained above.) */
0a3b29ad 4142 token = cp_lexer_peek_token (parser->lexer);
4143
4144 switch (token->type)
4145 {
4146 case CPP_NAME:
4147 return cp_parser_identifier (parser);
4148
4149 case CPP_KEYWORD:
4150 if (token->keyword == RID_OPERATOR)
4151 return cp_parser_operator_function_id (parser);
4152 /* Fall through. */
ccb84981 4153
0a3b29ad 4154 default:
4155 cp_parser_error (parser, "expected id-expression");
4156 return error_mark_node;
4157 }
4158 }
4159 else
4160 return cp_parser_unqualified_id (parser, template_keyword_p,
899cc6e8 4161 /*check_dependency_p=*/true,
130bb1d4 4162 declarator_p,
4163 optional_p);
0a3b29ad 4164}
4165
4166/* Parse an unqualified-id.
4167
4168 unqualified-id:
4169 identifier
4170 operator-function-id
4171 conversion-function-id
4172 ~ class-name
4173 template-id
4174
4175 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4176 keyword, in a construct like `A::template ...'.
4177
4178 Returns a representation of unqualified-id. For the `identifier'
4179 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4180 production a BIT_NOT_EXPR is returned; the operand of the
4181 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4182 other productions, see the documentation accompanying the
4183 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
899cc6e8 4184 names are looked up in uninstantiated templates. If DECLARATOR_P
4185 is true, the unqualified-id is appearing as part of a declarator,
4186 rather than as part of an expression. */
0a3b29ad 4187
4188static tree
ccb84981 4189cp_parser_unqualified_id (cp_parser* parser,
653e5405 4190 bool template_keyword_p,
899cc6e8 4191 bool check_dependency_p,
074ab442 4192 bool declarator_p,
130bb1d4 4193 bool optional_p)
0a3b29ad 4194{
4195 cp_token *token;
4196
4197 /* Peek at the next token. */
4198 token = cp_lexer_peek_token (parser->lexer);
ccb84981 4199
0a3b29ad 4200 switch (token->type)
4201 {
4202 case CPP_NAME:
4203 {
4204 tree id;
4205
4206 /* We don't know yet whether or not this will be a
4207 template-id. */
4208 cp_parser_parse_tentatively (parser);
4209 /* Try a template-id. */
4210 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4211 check_dependency_p,
4212 declarator_p);
0a3b29ad 4213 /* If it worked, we're done. */
4214 if (cp_parser_parse_definitely (parser))
4215 return id;
4216 /* Otherwise, it's an ordinary identifier. */
4217 return cp_parser_identifier (parser);
4218 }
4219
4220 case CPP_TEMPLATE_ID:
4221 return cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4222 check_dependency_p,
4223 declarator_p);
0a3b29ad 4224
4225 case CPP_COMPL:
4226 {
4227 tree type_decl;
4228 tree qualifying_scope;
4229 tree object_scope;
4230 tree scope;
f0d4a607 4231 bool done;
0a3b29ad 4232
4233 /* Consume the `~' token. */
4234 cp_lexer_consume_token (parser->lexer);
4235 /* Parse the class-name. The standard, as written, seems to
4236 say that:
4237
4238 template <typename T> struct S { ~S (); };
4239 template <typename T> S<T>::~S() {}
4240
653e5405 4241 is invalid, since `~' must be followed by a class-name, but
0a3b29ad 4242 `S<T>' is dependent, and so not known to be a class.
4243 That's not right; we need to look in uninstantiated
4244 templates. A further complication arises from:
4245
4246 template <typename T> void f(T t) {
4247 t.T::~T();
ccb84981 4248 }
0a3b29ad 4249
4250 Here, it is not possible to look up `T' in the scope of `T'
4251 itself. We must look in both the current scope, and the
ccb84981 4252 scope of the containing complete expression.
0a3b29ad 4253
4254 Yet another issue is:
4255
653e5405 4256 struct S {
4257 int S;
4258 ~S();
4259 };
0a3b29ad 4260
653e5405 4261 S::~S() {}
0a3b29ad 4262
653e5405 4263 The standard does not seem to say that the `S' in `~S'
0a3b29ad 4264 should refer to the type `S' and not the data member
4265 `S::S'. */
4266
4267 /* DR 244 says that we look up the name after the "~" in the
4268 same scope as we looked up the qualifying name. That idea
4269 isn't fully worked out; it's more complicated than that. */
4270 scope = parser->scope;
4271 object_scope = parser->object_scope;
4272 qualifying_scope = parser->qualifying_scope;
4273
4b2ee8a4 4274 /* Check for invalid scopes. */
4275 if (scope == error_mark_node)
4276 {
dea225ee 4277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4278 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 4279 return error_mark_node;
4280 }
4281 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4282 {
4283 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4284 error_at (token->location,
4285 "scope %qT before %<~%> is not a class-name",
4286 scope);
dea225ee 4287 cp_parser_simulate_error (parser);
4288 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4289 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 4290 return error_mark_node;
4291 }
4292 gcc_assert (!scope || TYPE_P (scope));
4293
cb84e326 4294 /* If the name is of the form "X::~X" it's OK even if X is a
4295 typedef. */
ba0c587d 4296 token = cp_lexer_peek_token (parser->lexer);
4b2ee8a4 4297 if (scope
ba0c587d 4298 && token->type == CPP_NAME
ccb84981 4299 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a70e3c37 4300 != CPP_LESS)
cb84e326 4301 && (token->u.value == TYPE_IDENTIFIER (scope)
4302 || constructor_name_p (token->u.value, scope)))
0a3b29ad 4303 {
4304 cp_lexer_consume_token (parser->lexer);
4305 return build_nt (BIT_NOT_EXPR, scope);
4306 }
4307
4308 /* If there was an explicit qualification (S::~T), first look
a70e3c37 4309 in the scope given by the qualification (i.e., S).
4310
5570fae0 4311 Note: in the calls to cp_parser_class_name below we pass
4312 typename_type so that lookup finds the injected-class-name
4313 rather than the constructor. */
f0d4a607 4314 done = false;
7978bd9e 4315 type_decl = NULL_TREE;
0a3b29ad 4316 if (scope)
4317 {
4318 cp_parser_parse_tentatively (parser);
ccb84981 4319 type_decl = cp_parser_class_name (parser,
0a3b29ad 4320 /*typename_keyword_p=*/false,
4321 /*template_keyword_p=*/false,
5570fae0 4322 typename_type,
0a3b29ad 4323 /*check_dependency=*/false,
3d0f901b 4324 /*class_head_p=*/false,
4325 declarator_p);
0a3b29ad 4326 if (cp_parser_parse_definitely (parser))
f0d4a607 4327 done = true;
0a3b29ad 4328 }
4329 /* In "N::S::~S", look in "N" as well. */
f0d4a607 4330 if (!done && scope && qualifying_scope)
0a3b29ad 4331 {
4332 cp_parser_parse_tentatively (parser);
4333 parser->scope = qualifying_scope;
4334 parser->object_scope = NULL_TREE;
4335 parser->qualifying_scope = NULL_TREE;
ccb84981 4336 type_decl
4337 = cp_parser_class_name (parser,
0a3b29ad 4338 /*typename_keyword_p=*/false,
4339 /*template_keyword_p=*/false,
5570fae0 4340 typename_type,
0a3b29ad 4341 /*check_dependency=*/false,
3d0f901b 4342 /*class_head_p=*/false,
4343 declarator_p);
0a3b29ad 4344 if (cp_parser_parse_definitely (parser))
f0d4a607 4345 done = true;
0a3b29ad 4346 }
130bb1d4 4347 /* In "p->S::~T", look in the scope given by "*p" as well. */
4348 else if (!done && object_scope)
0a3b29ad 4349 {
4350 cp_parser_parse_tentatively (parser);
4351 parser->scope = object_scope;
4352 parser->object_scope = NULL_TREE;
4353 parser->qualifying_scope = NULL_TREE;
ccb84981 4354 type_decl
4355 = cp_parser_class_name (parser,
130bb1d4 4356 /*typename_keyword_p=*/false,
0a3b29ad 4357 /*template_keyword_p=*/false,
5570fae0 4358 typename_type,
0a3b29ad 4359 /*check_dependency=*/false,
3d0f901b 4360 /*class_head_p=*/false,
4361 declarator_p);
0a3b29ad 4362 if (cp_parser_parse_definitely (parser))
f0d4a607 4363 done = true;
0a3b29ad 4364 }
4365 /* Look in the surrounding context. */
f0d4a607 4366 if (!done)
4367 {
4368 parser->scope = NULL_TREE;
4369 parser->object_scope = NULL_TREE;
4370 parser->qualifying_scope = NULL_TREE;
c6cc092b 4371 if (processing_template_decl)
4372 cp_parser_parse_tentatively (parser);
f0d4a607 4373 type_decl
4374 = cp_parser_class_name (parser,
4375 /*typename_keyword_p=*/false,
4376 /*template_keyword_p=*/false,
5570fae0 4377 typename_type,
f0d4a607 4378 /*check_dependency=*/false,
4379 /*class_head_p=*/false,
4380 declarator_p);
c6cc092b 4381 if (processing_template_decl
4382 && ! cp_parser_parse_definitely (parser))
4383 {
4384 /* We couldn't find a type with this name, so just accept
4385 it and check for a match at instantiation time. */
4386 type_decl = cp_parser_identifier (parser);
92644697 4387 if (type_decl != error_mark_node)
4388 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4389 return type_decl;
c6cc092b 4390 }
f0d4a607 4391 }
0a3b29ad 4392 /* If an error occurred, assume that the name of the
4393 destructor is the same as the name of the qualifying
4394 class. That allows us to keep parsing after running
4395 into ill-formed destructor names. */
4b2ee8a4 4396 if (type_decl == error_mark_node && scope)
0a3b29ad 4397 return build_nt (BIT_NOT_EXPR, scope);
4398 else if (type_decl == error_mark_node)
4399 return error_mark_node;
4400
a32f68f5 4401 /* Check that destructor name and scope match. */
4402 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4403 {
4404 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4405 error_at (token->location,
4406 "declaration of %<~%T%> as member of %qT",
4407 type_decl, scope);
dea225ee 4408 cp_parser_simulate_error (parser);
a32f68f5 4409 return error_mark_node;
4410 }
4411
899cc6e8 4412 /* [class.dtor]
4413
4414 A typedef-name that names a class shall not be used as the
4415 identifier in the declarator for a destructor declaration. */
ccb84981 4416 if (declarator_p
899cc6e8 4417 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
510a599a 4418 && !DECL_SELF_REFERENCE_P (type_decl)
4419 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4420 error_at (token->location,
4421 "typedef-name %qD used as destructor declarator",
4422 type_decl);
899cc6e8 4423
0a3b29ad 4424 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4425 }
4426
4427 case CPP_KEYWORD:
4428 if (token->keyword == RID_OPERATOR)
4429 {
4430 tree id;
4431
4432 /* This could be a template-id, so we try that first. */
4433 cp_parser_parse_tentatively (parser);
4434 /* Try a template-id. */
4435 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4436 /*check_dependency_p=*/true,
4437 declarator_p);
0a3b29ad 4438 /* If that worked, we're done. */
4439 if (cp_parser_parse_definitely (parser))
4440 return id;
4441 /* We still don't know whether we're looking at an
4442 operator-function-id or a conversion-function-id. */
4443 cp_parser_parse_tentatively (parser);
4444 /* Try an operator-function-id. */
4445 id = cp_parser_operator_function_id (parser);
4446 /* If that didn't work, try a conversion-function-id. */
4447 if (!cp_parser_parse_definitely (parser))
4448 id = cp_parser_conversion_function_id (parser);
4449
4450 return id;
4451 }
4452 /* Fall through. */
4453
4454 default:
197c9df7 4455 if (optional_p)
4456 return NULL_TREE;
0a3b29ad 4457 cp_parser_error (parser, "expected unqualified-id");
4458 return error_mark_node;
4459 }
4460}
4461
4462/* Parse an (optional) nested-name-specifier.
4463
3f00a6c0 4464 nested-name-specifier: [C++98]
0a3b29ad 4465 class-or-namespace-name :: nested-name-specifier [opt]
4466 class-or-namespace-name :: template nested-name-specifier [opt]
4467
3f00a6c0 4468 nested-name-specifier: [C++0x]
4469 type-name ::
4470 namespace-name ::
4471 nested-name-specifier identifier ::
4472 nested-name-specifier template [opt] simple-template-id ::
4473
0a3b29ad 4474 PARSER->SCOPE should be set appropriately before this function is
4475 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4476 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4477 in name lookups.
4478
4479 Sets PARSER->SCOPE to the class (TYPE) or namespace
4480 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4481 it unchanged if there is no nested-name-specifier. Returns the new
ccb84981 4482 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3d0f901b 4483
4484 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4485 part of a declaration and/or decl-specifier. */
0a3b29ad 4486
4487static tree
ccb84981 4488cp_parser_nested_name_specifier_opt (cp_parser *parser,
4489 bool typename_keyword_p,
0a3b29ad 4490 bool check_dependency_p,
3d0f901b 4491 bool type_p,
4492 bool is_declaration)
0a3b29ad 4493{
4494 bool success = false;
19273cc2 4495 cp_token_position start = 0;
4496 cp_token *token;
0a3b29ad 4497
0a3b29ad 4498 /* Remember where the nested-name-specifier starts. */
efcbcf83 4499 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
d15855b2 4500 {
4501 start = cp_lexer_token_position (parser->lexer, false);
4502 push_deferring_access_checks (dk_deferred);
4503 }
9b57b06b 4504
0a3b29ad 4505 while (true)
4506 {
4507 tree new_scope;
4508 tree old_scope;
4509 tree saved_qualifying_scope;
0a3b29ad 4510 bool template_keyword_p;
4511
b3c48b5d 4512 /* Spot cases that cannot be the beginning of a
4513 nested-name-specifier. */
4514 token = cp_lexer_peek_token (parser->lexer);
4515
4516 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4517 the already parsed nested-name-specifier. */
4518 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4519 {
4520 /* Grab the nested-name-specifier and continue the loop. */
4521 cp_parser_pre_parsed_nested_name_specifier (parser);
2f7a9ec1 4522 /* If we originally encountered this nested-name-specifier
4523 with IS_DECLARATION set to false, we will not have
4524 resolved TYPENAME_TYPEs, so we must do so here. */
b8b2a426 4525 if (is_declaration
4526 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
2f7a9ec1 4527 {
4528 new_scope = resolve_typename_type (parser->scope,
4529 /*only_current_p=*/false);
8826a863 4530 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
2f7a9ec1 4531 parser->scope = new_scope;
4532 }
b3c48b5d 4533 success = true;
4534 continue;
4535 }
4536
0a3b29ad 4537 /* Spot cases that cannot be the beginning of a
4538 nested-name-specifier. On the second and subsequent times
4539 through the loop, we look for the `template' keyword. */
2370b5bf 4540 if (success && token->keyword == RID_TEMPLATE)
0a3b29ad 4541 ;
4542 /* A template-id can start a nested-name-specifier. */
2370b5bf 4543 else if (token->type == CPP_TEMPLATE_ID)
0a3b29ad 4544 ;
4545 else
4546 {
4547 /* If the next token is not an identifier, then it is
3f00a6c0 4548 definitely not a type-name or namespace-name. */
2370b5bf 4549 if (token->type != CPP_NAME)
0a3b29ad 4550 break;
4551 /* If the following token is neither a `<' (to begin a
4552 template-id), nor a `::', then we are not looking at a
4553 nested-name-specifier. */
4554 token = cp_lexer_peek_nth_token (parser->lexer, 2);
c8d5ab79 4555 if (token->type != CPP_SCOPE
4556 && !cp_parser_nth_token_starts_template_argument_list_p
4557 (parser, 2))
0a3b29ad 4558 break;
4559 }
4560
4561 /* The nested-name-specifier is optional, so we parse
4562 tentatively. */
4563 cp_parser_parse_tentatively (parser);
4564
4565 /* Look for the optional `template' keyword, if this isn't the
4566 first time through the loop. */
4567 if (success)
4568 template_keyword_p = cp_parser_optional_template_keyword (parser);
4569 else
4570 template_keyword_p = false;
4571
4572 /* Save the old scope since the name lookup we are about to do
4573 might destroy it. */
4574 old_scope = parser->scope;
4575 saved_qualifying_scope = parser->qualifying_scope;
0078a5e8 4576 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4577 look up names in "X<T>::I" in order to determine that "Y" is
4578 a template. So, if we have a typename at this point, we make
4579 an effort to look through it. */
9031d10b 4580 if (is_declaration
dee31741 4581 && !typename_keyword_p
9031d10b 4582 && parser->scope
0078a5e8 4583 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
9031d10b 4584 parser->scope = resolve_typename_type (parser->scope,
0078a5e8 4585 /*only_current_p=*/false);
0a3b29ad 4586 /* Parse the qualifying entity. */
ccb84981 4587 new_scope
3f00a6c0 4588 = cp_parser_qualifying_entity (parser,
4589 typename_keyword_p,
4590 template_keyword_p,
4591 check_dependency_p,
4592 type_p,
4593 is_declaration);
0a3b29ad 4594 /* Look for the `::' token. */
c247dce0 4595 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 4596
4597 /* If we found what we wanted, we keep going; otherwise, we're
4598 done. */
4599 if (!cp_parser_parse_definitely (parser))
4600 {
4601 bool error_p = false;
4602
4603 /* Restore the OLD_SCOPE since it was valid before the
4604 failed attempt at finding the last
4605 class-or-namespace-name. */
4606 parser->scope = old_scope;
4607 parser->qualifying_scope = saved_qualifying_scope;
ba0c587d 4608 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4609 break;
0a3b29ad 4610 /* If the next token is an identifier, and the one after
4611 that is a `::', then any valid interpretation would have
4612 found a class-or-namespace-name. */
4613 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
ccb84981 4614 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 4615 == CPP_SCOPE)
ccb84981 4616 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
0a3b29ad 4617 != CPP_COMPL))
4618 {
4619 token = cp_lexer_consume_token (parser->lexer);
ccb84981 4620 if (!error_p)
0a3b29ad 4621 {
b62240d5 4622 if (!token->ambiguous_p)
4623 {
4624 tree decl;
4625 tree ambiguous_decls;
4626
3369eb76 4627 decl = cp_parser_lookup_name (parser, token->u.value,
b62240d5 4628 none_type,
4629 /*is_template=*/false,
4630 /*is_namespace=*/false,
4631 /*check_dependency=*/true,
ad9ae192 4632 &ambiguous_decls,
4633 token->location);
b62240d5 4634 if (TREE_CODE (decl) == TEMPLATE_DECL)
ccb59bb6 4635 error_at (token->location,
4636 "%qD used without template parameters",
4637 decl);
b62240d5 4638 else if (ambiguous_decls)
4639 {
ccb59bb6 4640 error_at (token->location,
4641 "reference to %qD is ambiguous",
4642 token->u.value);
b62240d5 4643 print_candidates (ambiguous_decls);
4644 decl = error_mark_node;
4645 }
4646 else
3f00a6c0 4647 {
3f00a6c0 4648 if (cxx_dialect != cxx98)
c247dce0 4649 cp_parser_name_lookup_error
4650 (parser, token->u.value, decl, NLE_NOT_CXX98,
3f00a6c0 4651 token->location);
c247dce0 4652 else
4653 cp_parser_name_lookup_error
4654 (parser, token->u.value, decl, NLE_CXX98,
4655 token->location);
3f00a6c0 4656 }
b62240d5 4657 }
4658 parser->scope = error_mark_node;
0a3b29ad 4659 error_p = true;
6fc758aa 4660 /* Treat this as a successful nested-name-specifier
4661 due to:
4662
4663 [basic.lookup.qual]
4664
4665 If the name found is not a class-name (clause
4666 _class_) or namespace-name (_namespace.def_), the
4667 program is ill-formed. */
4668 success = true;
0a3b29ad 4669 }
4670 cp_lexer_consume_token (parser->lexer);
4671 }
4672 break;
4673 }
0a3b29ad 4674 /* We've found one valid nested-name-specifier. */
4675 success = true;
fbb01da7 4676 /* Name lookup always gives us a DECL. */
4677 if (TREE_CODE (new_scope) == TYPE_DECL)
4678 new_scope = TREE_TYPE (new_scope);
4679 /* Uses of "template" must be followed by actual templates. */
4680 if (template_keyword_p
4681 && !(CLASS_TYPE_P (new_scope)
4682 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4683 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4684 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4685 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4686 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4687 == TEMPLATE_ID_EXPR)))
2b9e3597 4688 permerror (input_location, TYPE_P (new_scope)
561fec9d 4689 ? "%qT is not a template"
4690 : "%qD is not a template",
4691 new_scope);
0a3b29ad 4692 /* If it is a class scope, try to complete it; we are about to
4693 be looking up names inside the class. */
fbb01da7 4694 if (TYPE_P (new_scope)
954ad420 4695 /* Since checking types for dependency can be expensive,
4696 avoid doing it if the type is already complete. */
fbb01da7 4697 && !COMPLETE_TYPE_P (new_scope)
954ad420 4698 /* Do not try to complete dependent types. */
fbb01da7 4699 && !dependent_type_p (new_scope))
fd0d54e4 4700 {
4701 new_scope = complete_type (new_scope);
4702 /* If it is a typedef to current class, use the current
4703 class instead, as the typedef won't have any names inside
4704 it yet. */
4705 if (!COMPLETE_TYPE_P (new_scope)
4706 && currently_open_class (new_scope))
4707 new_scope = TYPE_MAIN_VARIANT (new_scope);
4708 }
fbb01da7 4709 /* Make sure we look in the right scope the next time through
4710 the loop. */
4711 parser->scope = new_scope;
0a3b29ad 4712 }
4713
4714 /* If parsing tentatively, replace the sequence of tokens that makes
4715 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4716 token. That way, should we re-parse the token stream, we will
4717 not have to repeat the effort required to do the parse, nor will
4718 we issue duplicate error messages. */
19273cc2 4719 if (success && start)
0a3b29ad 4720 {
d15855b2 4721 cp_token *token;
9031d10b 4722
d15855b2 4723 token = cp_lexer_token_at (parser->lexer, start);
0a3b29ad 4724 /* Reset the contents of the START token. */
4725 token->type = CPP_NESTED_NAME_SPECIFIER;
d15855b2 4726 /* Retrieve any deferred checks. Do not pop this access checks yet
4727 so the memory will not be reclaimed during token replacing below. */
ba72912a 4728 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
3369eb76 4729 token->u.tree_check_value->value = parser->scope;
4730 token->u.tree_check_value->checks = get_deferred_access_checks ();
4731 token->u.tree_check_value->qualifying_scope =
4732 parser->qualifying_scope;
0a3b29ad 4733 token->keyword = RID_MAX;
9031d10b 4734
0a3b29ad 4735 /* Purge all subsequent tokens. */
19273cc2 4736 cp_lexer_purge_tokens_after (parser->lexer, start);
0a3b29ad 4737 }
074ab442 4738
d15855b2 4739 if (start)
4740 pop_to_parent_deferring_access_checks ();
0a3b29ad 4741
4742 return success ? parser->scope : NULL_TREE;
4743}
4744
4745/* Parse a nested-name-specifier. See
4746 cp_parser_nested_name_specifier_opt for details. This function
4747 behaves identically, except that it will an issue an error if no
c75ae97e 4748 nested-name-specifier is present. */
0a3b29ad 4749
4750static tree
ccb84981 4751cp_parser_nested_name_specifier (cp_parser *parser,
4752 bool typename_keyword_p,
0a3b29ad 4753 bool check_dependency_p,
3d0f901b 4754 bool type_p,
4755 bool is_declaration)
0a3b29ad 4756{
4757 tree scope;
4758
4759 /* Look for the nested-name-specifier. */
4760 scope = cp_parser_nested_name_specifier_opt (parser,
4761 typename_keyword_p,
4762 check_dependency_p,
3d0f901b 4763 type_p,
4764 is_declaration);
0a3b29ad 4765 /* If it was not present, issue an error message. */
4766 if (!scope)
4767 {
4768 cp_parser_error (parser, "expected nested-name-specifier");
bbd3de90 4769 parser->scope = NULL_TREE;
0a3b29ad 4770 }
4771
4772 return scope;
4773}
4774
3f00a6c0 4775/* Parse the qualifying entity in a nested-name-specifier. For C++98,
4776 this is either a class-name or a namespace-name (which corresponds
4777 to the class-or-namespace-name production in the grammar). For
4778 C++0x, it can also be a type-name that refers to an enumeration
4779 type.
0a3b29ad 4780
4781 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4782 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4783 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4784 TYPE_P is TRUE iff the next name should be taken as a class-name,
4785 even the same name is declared to be another entity in the same
4786 scope.
4787
4788 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6fc758aa 4789 specified by the class-or-namespace-name. If neither is found the
4790 ERROR_MARK_NODE is returned. */
0a3b29ad 4791
4792static tree
3f00a6c0 4793cp_parser_qualifying_entity (cp_parser *parser,
4794 bool typename_keyword_p,
4795 bool template_keyword_p,
4796 bool check_dependency_p,
4797 bool type_p,
4798 bool is_declaration)
0a3b29ad 4799{
4800 tree saved_scope;
4801 tree saved_qualifying_scope;
4802 tree saved_object_scope;
4803 tree scope;
6fc758aa 4804 bool only_class_p;
3f00a6c0 4805 bool successful_parse_p;
0a3b29ad 4806
0a3b29ad 4807 /* Before we try to parse the class-name, we must save away the
4808 current PARSER->SCOPE since cp_parser_class_name will destroy
4809 it. */
4810 saved_scope = parser->scope;
4811 saved_qualifying_scope = parser->qualifying_scope;
4812 saved_object_scope = parser->object_scope;
6fc758aa 4813 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4814 there is no need to look for a namespace-name. */
3f00a6c0 4815 only_class_p = template_keyword_p
4816 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6fc758aa 4817 if (!only_class_p)
4818 cp_parser_parse_tentatively (parser);
ccb84981 4819 scope = cp_parser_class_name (parser,
0a3b29ad 4820 typename_keyword_p,
4821 template_keyword_p,
e2ae55f2 4822 type_p ? class_type : none_type,
0a3b29ad 4823 check_dependency_p,
3d0f901b 4824 /*class_head_p=*/false,
4825 is_declaration);
3f00a6c0 4826 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4827 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4828 if (!only_class_p
4829 && cxx_dialect != cxx98
4830 && !successful_parse_p)
4831 {
4832 /* Restore the saved scope. */
4833 parser->scope = saved_scope;
4834 parser->qualifying_scope = saved_qualifying_scope;
4835 parser->object_scope = saved_object_scope;
4836
4837 /* Parse tentatively. */
4838 cp_parser_parse_tentatively (parser);
4839
4840 /* Parse a typedef-name or enum-name. */
4841 scope = cp_parser_nonclass_name (parser);
e221246b 4842
4843 /* "If the name found does not designate a namespace or a class,
4844 enumeration, or dependent type, the program is ill-formed."
4845
4846 We cover classes and dependent types above and namespaces below,
4847 so this code is only looking for enums. */
4848 if (!scope || TREE_CODE (scope) != TYPE_DECL
4849 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4850 cp_parser_simulate_error (parser);
4851
3f00a6c0 4852 successful_parse_p = cp_parser_parse_definitely (parser);
4853 }
0a3b29ad 4854 /* If that didn't work, try for a namespace-name. */
3f00a6c0 4855 if (!only_class_p && !successful_parse_p)
0a3b29ad 4856 {
4857 /* Restore the saved scope. */
4858 parser->scope = saved_scope;
4859 parser->qualifying_scope = saved_qualifying_scope;
4860 parser->object_scope = saved_object_scope;
6fc758aa 4861 /* If we are not looking at an identifier followed by the scope
4862 resolution operator, then this is not part of a
4863 nested-name-specifier. (Note that this function is only used
4864 to parse the components of a nested-name-specifier.) */
4865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4866 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4867 return error_mark_node;
0a3b29ad 4868 scope = cp_parser_namespace_name (parser);
4869 }
4870
4871 return scope;
4872}
4873
4874/* Parse a postfix-expression.
4875
4876 postfix-expression:
4877 primary-expression
4878 postfix-expression [ expression ]
4879 postfix-expression ( expression-list [opt] )
4880 simple-type-specifier ( expression-list [opt] )
ccb84981 4881 typename :: [opt] nested-name-specifier identifier
0a3b29ad 4882 ( expression-list [opt] )
4883 typename :: [opt] nested-name-specifier template [opt] template-id
4884 ( expression-list [opt] )
4885 postfix-expression . template [opt] id-expression
4886 postfix-expression -> template [opt] id-expression
4887 postfix-expression . pseudo-destructor-name
4888 postfix-expression -> pseudo-destructor-name
4889 postfix-expression ++
4890 postfix-expression --
4891 dynamic_cast < type-id > ( expression )
4892 static_cast < type-id > ( expression )
4893 reinterpret_cast < type-id > ( expression )
4894 const_cast < type-id > ( expression )
4895 typeid ( expression )
4896 typeid ( type-id )
4897
4898 GNU Extension:
ccb84981 4899
0a3b29ad 4900 postfix-expression:
4901 ( type-id ) { initializer-list , [opt] }
4902
4903 This extension is a GNU version of the C99 compound-literal
4904 construct. (The C99 grammar uses `type-name' instead of `type-id',
4905 but they are essentially the same concept.)
4906
4907 If ADDRESS_P is true, the postfix expression is the operand of the
640aa28c 4908 `&' operator. CAST_P is true if this expression is the target of a
9031d10b 4909 cast.
0a3b29ad 4910
34da8800 4911 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4912 class member access expressions [expr.ref].
4913
0a3b29ad 4914 Returns a representation of the expression. */
4915
4916static tree
34da8800 4917cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
98b326fd 4918 bool member_access_only_p,
4919 cp_id_kind * pidk_return)
0a3b29ad 4920{
4921 cp_token *token;
4922 enum rid keyword;
0886adbc 4923 cp_id_kind idk = CP_ID_KIND_NONE;
0a3b29ad 4924 tree postfix_expression = NULL_TREE;
34da8800 4925 bool is_member_access = false;
0a3b29ad 4926
4927 /* Peek at the next token. */
4928 token = cp_lexer_peek_token (parser->lexer);
4929 /* Some of the productions are determined by keywords. */
4930 keyword = token->keyword;
4931 switch (keyword)
4932 {
4933 case RID_DYNCAST:
4934 case RID_STATCAST:
4935 case RID_REINTCAST:
4936 case RID_CONSTCAST:
4937 {
4938 tree type;
4939 tree expression;
4940 const char *saved_message;
4941
4942 /* All of these can be handled in the same way from the point
4943 of view of parsing. Begin by consuming the token
4944 identifying the cast. */
4945 cp_lexer_consume_token (parser->lexer);
ccb84981 4946
0a3b29ad 4947 /* New types cannot be defined in the cast. */
4948 saved_message = parser->type_definition_forbidden_message;
4949 parser->type_definition_forbidden_message
ca82e026 4950 = G_("types may not be defined in casts");
0a3b29ad 4951
4952 /* Look for the opening `<'. */
c247dce0 4953 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 4954 /* Parse the type to which we are casting. */
4955 type = cp_parser_type_id (parser);
4956 /* Look for the closing `>'. */
c247dce0 4957 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 4958 /* Restore the old message. */
4959 parser->type_definition_forbidden_message = saved_message;
4960
4961 /* And the expression which is being cast. */
c247dce0 4962 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
98b326fd 4963 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
c247dce0 4964 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 4965
5f6526e1 4966 /* Only type conversions to integral or enumeration types
4967 can be used in constant-expressions. */
bde9ebf7 4968 if (!cast_valid_in_integral_constant_expression_p (type)
c61e1212 4969 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
3938e0c2 4970 return error_mark_node;
5f6526e1 4971
0a3b29ad 4972 switch (keyword)
4973 {
4974 case RID_DYNCAST:
4975 postfix_expression
ebd21de4 4976 = build_dynamic_cast (type, expression, tf_warning_or_error);
0a3b29ad 4977 break;
4978 case RID_STATCAST:
4979 postfix_expression
ebd21de4 4980 = build_static_cast (type, expression, tf_warning_or_error);
0a3b29ad 4981 break;
4982 case RID_REINTCAST:
4983 postfix_expression
ebd21de4 4984 = build_reinterpret_cast (type, expression,
4985 tf_warning_or_error);
0a3b29ad 4986 break;
4987 case RID_CONSTCAST:
4988 postfix_expression
ebd21de4 4989 = build_const_cast (type, expression, tf_warning_or_error);
0a3b29ad 4990 break;
4991 default:
2e3e31d2 4992 gcc_unreachable ();
0a3b29ad 4993 }
4994 }
4995 break;
4996
4997 case RID_TYPEID:
4998 {
4999 tree type;
5000 const char *saved_message;
41f2d08e 5001 bool saved_in_type_id_in_expr_p;
0a3b29ad 5002
5003 /* Consume the `typeid' token. */
5004 cp_lexer_consume_token (parser->lexer);
5005 /* Look for the `(' token. */
c247dce0 5006 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 5007 /* Types cannot be defined in a `typeid' expression. */
5008 saved_message = parser->type_definition_forbidden_message;
5009 parser->type_definition_forbidden_message
ca82e026 5010 = G_("types may not be defined in a %<typeid%> expression");
0a3b29ad 5011 /* We can't be sure yet whether we're looking at a type-id or an
5012 expression. */
5013 cp_parser_parse_tentatively (parser);
5014 /* Try a type-id first. */
41f2d08e 5015 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5016 parser->in_type_id_in_expr_p = true;
0a3b29ad 5017 type = cp_parser_type_id (parser);
41f2d08e 5018 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 5019 /* Look for the `)' token. Otherwise, we can't be sure that
5020 we're not looking at an expression: consider `typeid (int
5021 (3))', for example. */
c247dce0 5022 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 5023 /* If all went well, simply lookup the type-id. */
5024 if (cp_parser_parse_definitely (parser))
5025 postfix_expression = get_typeid (type);
5026 /* Otherwise, fall back to the expression variant. */
5027 else
5028 {
5029 tree expression;
5030
5031 /* Look for an expression. */
98b326fd 5032 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
0a3b29ad 5033 /* Compute its typeid. */
5034 postfix_expression = build_typeid (expression);
5035 /* Look for the `)' token. */
c247dce0 5036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 5037 }
4a430fd7 5038 /* Restore the saved message. */
5039 parser->type_definition_forbidden_message = saved_message;
368ad4b9 5040 /* `typeid' may not appear in an integral constant expression. */
c247dce0 5041 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
368ad4b9 5042 return error_mark_node;
0a3b29ad 5043 }
5044 break;
ccb84981 5045
0a3b29ad 5046 case RID_TYPENAME:
5047 {
0a3b29ad 5048 tree type;
93673597 5049 /* The syntax permitted here is the same permitted for an
5050 elaborated-type-specifier. */
5051 type = cp_parser_elaborated_type_specifier (parser,
5052 /*is_friend=*/false,
5053 /*is_declaration=*/false);
0a3b29ad 5054 postfix_expression = cp_parser_functional_cast (parser, type);
5055 }
5056 break;
5057
5058 default:
5059 {
5060 tree type;
5061
5062 /* If the next thing is a simple-type-specifier, we may be
5063 looking at a functional cast. We could also be looking at
5064 an id-expression. So, we try the functional cast, and if
5065 that doesn't work we fall back to the primary-expression. */
5066 cp_parser_parse_tentatively (parser);
5067 /* Look for the simple-type-specifier. */
ccb84981 5068 type = cp_parser_simple_type_specifier (parser,
4b9b2871 5069 /*decl_specs=*/NULL,
5070 CP_PARSER_FLAGS_NONE);
0a3b29ad 5071 /* Parse the cast itself. */
5072 if (!cp_parser_error_occurred (parser))
ccb84981 5073 postfix_expression
0a3b29ad 5074 = cp_parser_functional_cast (parser, type);
5075 /* If that worked, we're done. */
5076 if (cp_parser_parse_definitely (parser))
5077 break;
5078
5079 /* If the functional-cast didn't work out, try a
5080 compound-literal. */
5f6526e1 5081 if (cp_parser_allow_gnu_extensions_p (parser)
5082 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
0a3b29ad 5083 {
c75b4594 5084 VEC(constructor_elt,gc) *initializer_list = NULL;
41f2d08e 5085 bool saved_in_type_id_in_expr_p;
0a3b29ad 5086
5087 cp_parser_parse_tentatively (parser);
5f6526e1 5088 /* Consume the `('. */
5089 cp_lexer_consume_token (parser->lexer);
5090 /* Parse the type. */
41f2d08e 5091 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5092 parser->in_type_id_in_expr_p = true;
5f6526e1 5093 type = cp_parser_type_id (parser);
41f2d08e 5094 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5f6526e1 5095 /* Look for the `)'. */
c247dce0 5096 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5f6526e1 5097 /* Look for the `{'. */
c247dce0 5098 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5f6526e1 5099 /* If things aren't going well, there's no need to
5100 keep going. */
5101 if (!cp_parser_error_occurred (parser))
0a3b29ad 5102 {
878870b4 5103 bool non_constant_p;
5f6526e1 5104 /* Parse the initializer-list. */
ccb84981 5105 initializer_list
878870b4 5106 = cp_parser_initializer_list (parser, &non_constant_p);
5f6526e1 5107 /* Allow a trailing `,'. */
5108 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5109 cp_lexer_consume_token (parser->lexer);
5110 /* Look for the final `}'. */
c247dce0 5111 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 5112 }
5113 /* If that worked, we're definitely looking at a
5114 compound-literal expression. */
5115 if (cp_parser_parse_definitely (parser))
5116 {
5117 /* Warn the user that a compound literal is not
5118 allowed in standard C++. */
21ca8540 5119 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
93b25def 5120 /* For simplicity, we disallow compound literals in
5121 constant-expressions. We could
66a0bac0 5122 allow compound literals of integer type, whose
5123 initializer was a constant, in constant
5124 expressions. Permitting that usage, as a further
5125 extension, would not change the meaning of any
5126 currently accepted programs. (Of course, as
5127 compound literals are not part of ISO C++, the
5128 standard has nothing to say.) */
c247dce0 5129 if (cp_parser_non_integral_constant_expression (parser,
5130 NIC_NCC))
66a0bac0 5131 {
5132 postfix_expression = error_mark_node;
5133 break;
5134 }
0a3b29ad 5135 /* Form the representation of the compound-literal. */
ccb84981 5136 postfix_expression
f82f1250 5137 = (finish_compound_literal
5138 (type, build_constructor (init_list_type_node,
5139 initializer_list)));
0a3b29ad 5140 break;
5141 }
5142 }
5143
5144 /* It must be a primary-expression. */
074ab442 5145 postfix_expression
5146 = cp_parser_primary_expression (parser, address_p, cast_p,
fbb01da7 5147 /*template_arg_p=*/false,
5148 &idk);
0a3b29ad 5149 }
5150 break;
5151 }
5152
0a3b29ad 5153 /* Keep looping until the postfix-expression is complete. */
5154 while (true)
5155 {
c08d51be 5156 if (idk == CP_ID_KIND_UNQUALIFIED
5157 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
0a3b29ad 5158 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
0886adbc 5159 /* It is not a Koenig lookup function call. */
ccb84981 5160 postfix_expression
0886adbc 5161 = unqualified_name_lookup_error (postfix_expression);
ccb84981 5162
0a3b29ad 5163 /* Peek at the next token. */
5164 token = cp_lexer_peek_token (parser->lexer);
5165
5166 switch (token->type)
5167 {
5168 case CPP_OPEN_SQUARE:
43bf5d72 5169 postfix_expression
5170 = cp_parser_postfix_open_square_expression (parser,
5171 postfix_expression,
5172 false);
5173 idk = CP_ID_KIND_NONE;
34da8800 5174 is_member_access = false;
0a3b29ad 5175 break;
5176
5177 case CPP_OPEN_PAREN:
5178 /* postfix-expression ( expression-list [opt] ) */
5179 {
cbce34a5 5180 bool koenig_p;
0dbadc68 5181 bool is_builtin_constant_p;
5182 bool saved_integral_constant_expression_p = false;
5183 bool saved_non_integral_constant_expression_p = false;
f352a3fb 5184 VEC(tree,gc) *args;
0dbadc68 5185
34da8800 5186 is_member_access = false;
5187
9031d10b 5188 is_builtin_constant_p
0dbadc68 5189 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5190 if (is_builtin_constant_p)
5191 {
5192 /* The whole point of __builtin_constant_p is to allow
5193 non-constant expressions to appear as arguments. */
5194 saved_integral_constant_expression_p
5195 = parser->integral_constant_expression_p;
5196 saved_non_integral_constant_expression_p
5197 = parser->non_integral_constant_expression_p;
5198 parser->integral_constant_expression_p = false;
5199 }
5200 args = (cp_parser_parenthesized_expression_list
33199a81 5201 (parser, non_attr,
d95d815d 5202 /*cast_p=*/false, /*allow_expansion_p=*/true,
0dbadc68 5203 /*non_constant_p=*/NULL));
5204 if (is_builtin_constant_p)
5205 {
5206 parser->integral_constant_expression_p
5207 = saved_integral_constant_expression_p;
5208 parser->non_integral_constant_expression_p
5209 = saved_non_integral_constant_expression_p;
5210 }
0a3b29ad 5211
f352a3fb 5212 if (args == NULL)
0986fa22 5213 {
5214 postfix_expression = error_mark_node;
5215 break;
5216 }
ccb84981 5217
5f6526e1 5218 /* Function calls are not permitted in
5219 constant-expressions. */
3a88b1db 5220 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5221 && cp_parser_non_integral_constant_expression (parser,
c247dce0 5222 NIC_FUNC_CALL))
5f6526e1 5223 {
3938e0c2 5224 postfix_expression = error_mark_node;
f352a3fb 5225 release_tree_vector (args);
3938e0c2 5226 break;
5f6526e1 5227 }
0a3b29ad 5228
cbce34a5 5229 koenig_p = false;
32f71e4f 5230 if (idk == CP_ID_KIND_UNQUALIFIED
5231 || idk == CP_ID_KIND_TEMPLATE_ID)
b7edeb23 5232 {
0e43a566 5233 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5234 {
f352a3fb 5235 if (!VEC_empty (tree, args))
0e43a566 5236 {
5237 koenig_p = true;
ba026663 5238 if (!any_type_dependent_arguments_p (args))
5239 postfix_expression
9dd72ec4 5240 = perform_koenig_lookup (postfix_expression, args,
5241 /*include_std=*/false);
0e43a566 5242 }
5243 else
5244 postfix_expression
5245 = unqualified_fn_lookup_error (postfix_expression);
5246 }
3ed12242 5247 /* We do not perform argument-dependent lookup if
5248 normal lookup finds a non-function, in accordance
5249 with the expected resolution of DR 218. */
f352a3fb 5250 else if (!VEC_empty (tree, args)
5251 && is_overloaded_fn (postfix_expression))
cbce34a5 5252 {
0e43a566 5253 tree fn = get_first_fn (postfix_expression);
52b882a6 5254 fn = STRIP_TEMPLATE (fn);
0e43a566 5255
52b882a6 5256 /* Do not do argument dependent lookup if regular
5257 lookup finds a member function or a block-scope
5258 function declaration. [basic.lookup.argdep]/3 */
5259 if (!DECL_FUNCTION_MEMBER_P (fn)
5260 && !DECL_LOCAL_FUNCTION_P (fn))
0e43a566 5261 {
5262 koenig_p = true;
ba026663 5263 if (!any_type_dependent_arguments_p (args))
5264 postfix_expression
9dd72ec4 5265 = perform_koenig_lookup (postfix_expression, args,
5266 /*include_std=*/false);
0e43a566 5267 }
cbce34a5 5268 }
b7edeb23 5269 }
ccb84981 5270
13795292 5271 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
0a3b29ad 5272 {
13795292 5273 tree instance = TREE_OPERAND (postfix_expression, 0);
5274 tree fn = TREE_OPERAND (postfix_expression, 1);
5275
5276 if (processing_template_decl
5277 && (type_dependent_expression_p (instance)
5278 || (!BASELINK_P (fn)
5279 && TREE_CODE (fn) != FIELD_DECL)
ed45372d 5280 || type_dependent_expression_p (fn)
13795292 5281 || any_type_dependent_arguments_p (args)))
5282 {
5283 postfix_expression
f352a3fb 5284 = build_nt_call_vec (postfix_expression, args);
5285 release_tree_vector (args);
13795292 5286 break;
5287 }
3c33f9f3 5288
5289 if (BASELINK_P (fn))
98b326fd 5290 {
3c33f9f3 5291 postfix_expression
ccb84981 5292 = (build_new_method_call
f352a3fb 5293 (instance, fn, &args, NULL_TREE,
ccb84981 5294 (idk == CP_ID_KIND_QUALIFIED
393f878f 5295 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
ebd21de4 5296 /*fn_p=*/NULL,
5297 tf_warning_or_error));
98b326fd 5298 }
3c33f9f3 5299 else
5300 postfix_expression
f352a3fb 5301 = finish_call_expr (postfix_expression, &args,
3c33f9f3 5302 /*disallow_virtual=*/false,
ebd21de4 5303 /*koenig_p=*/false,
5304 tf_warning_or_error);
0a3b29ad 5305 }
13795292 5306 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5307 || TREE_CODE (postfix_expression) == MEMBER_REF
5308 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
0a3b29ad 5309 postfix_expression = (build_offset_ref_call_from_tree
f352a3fb 5310 (postfix_expression, &args));
0886adbc 5311 else if (idk == CP_ID_KIND_QUALIFIED)
b3c48b5d 5312 /* A call to a static class member, or a namespace-scope
5313 function. */
5314 postfix_expression
f352a3fb 5315 = finish_call_expr (postfix_expression, &args,
cbce34a5 5316 /*disallow_virtual=*/true,
ebd21de4 5317 koenig_p,
5318 tf_warning_or_error);
0a3b29ad 5319 else
b3c48b5d 5320 /* All other function calls. */
ccb84981 5321 postfix_expression
f352a3fb 5322 = finish_call_expr (postfix_expression, &args,
cbce34a5 5323 /*disallow_virtual=*/false,
ebd21de4 5324 koenig_p,
5325 tf_warning_or_error);
0a3b29ad 5326
5327 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
0886adbc 5328 idk = CP_ID_KIND_NONE;
f352a3fb 5329
5330 release_tree_vector (args);
0a3b29ad 5331 }
5332 break;
ccb84981 5333
0a3b29ad 5334 case CPP_DOT:
5335 case CPP_DEREF:
ccb84981 5336 /* postfix-expression . template [opt] id-expression
5337 postfix-expression . pseudo-destructor-name
0a3b29ad 5338 postfix-expression -> template [opt] id-expression
5339 postfix-expression -> pseudo-destructor-name */
207355ad 5340
43bf5d72 5341 /* Consume the `.' or `->' operator. */
5342 cp_lexer_consume_token (parser->lexer);
0a3b29ad 5343
43bf5d72 5344 postfix_expression
5345 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5346 postfix_expression,
ad9ae192 5347 false, &idk,
5348 token->location);
34da8800 5349
5350 is_member_access = true;
0a3b29ad 5351 break;
5352
5353 case CPP_PLUS_PLUS:
5354 /* postfix-expression ++ */
5355 /* Consume the `++' token. */
5356 cp_lexer_consume_token (parser->lexer);
364c2f43 5357 /* Generate a representation for the complete expression. */
ccb84981 5358 postfix_expression
5359 = finish_increment_expr (postfix_expression,
364c2f43 5360 POSTINCREMENT_EXPR);
5f6526e1 5361 /* Increments may not appear in constant-expressions. */
c247dce0 5362 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
3938e0c2 5363 postfix_expression = error_mark_node;
0886adbc 5364 idk = CP_ID_KIND_NONE;
34da8800 5365 is_member_access = false;
0a3b29ad 5366 break;
5367
5368 case CPP_MINUS_MINUS:
5369 /* postfix-expression -- */
5370 /* Consume the `--' token. */
5371 cp_lexer_consume_token (parser->lexer);
364c2f43 5372 /* Generate a representation for the complete expression. */
ccb84981 5373 postfix_expression
5374 = finish_increment_expr (postfix_expression,
364c2f43 5375 POSTDECREMENT_EXPR);
5f6526e1 5376 /* Decrements may not appear in constant-expressions. */
c247dce0 5377 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
3938e0c2 5378 postfix_expression = error_mark_node;
0886adbc 5379 idk = CP_ID_KIND_NONE;
34da8800 5380 is_member_access = false;
0a3b29ad 5381 break;
5382
5383 default:
98b326fd 5384 if (pidk_return != NULL)
5385 * pidk_return = idk;
34da8800 5386 if (member_access_only_p)
5387 return is_member_access? postfix_expression : error_mark_node;
5388 else
5389 return postfix_expression;
0a3b29ad 5390 }
5391 }
5392
5393 /* We should never get here. */
2e3e31d2 5394 gcc_unreachable ();
0a3b29ad 5395 return error_mark_node;
5396}
5397
43bf5d72 5398/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5399 by cp_parser_builtin_offsetof. We're looking for
5400
5401 postfix-expression [ expression ]
5402
5403 FOR_OFFSETOF is set if we're being called in that context, which
5404 changes how we deal with integer constant expressions. */
5405
5406static tree
5407cp_parser_postfix_open_square_expression (cp_parser *parser,
5408 tree postfix_expression,
5409 bool for_offsetof)
5410{
5411 tree index;
5412
5413 /* Consume the `[' token. */
5414 cp_lexer_consume_token (parser->lexer);
5415
5416 /* Parse the index expression. */
5417 /* ??? For offsetof, there is a question of what to allow here. If
5418 offsetof is not being used in an integral constant expression context,
5419 then we *could* get the right answer by computing the value at runtime.
5420 If we are in an integral constant expression context, then we might
5421 could accept any constant expression; hard to say without analysis.
5422 Rather than open the barn door too wide right away, allow only integer
4a44ba29 5423 constant expressions here. */
43bf5d72 5424 if (for_offsetof)
5425 index = cp_parser_constant_expression (parser, false, NULL);
5426 else
98b326fd 5427 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
43bf5d72 5428
5429 /* Look for the closing `]'. */
c247dce0 5430 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43bf5d72 5431
5432 /* Build the ARRAY_REF. */
5433 postfix_expression = grok_array_decl (postfix_expression, index);
5434
5435 /* When not doing offsetof, array references are not permitted in
5436 constant-expressions. */
5437 if (!for_offsetof
c247dce0 5438 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
43bf5d72 5439 postfix_expression = error_mark_node;
5440
5441 return postfix_expression;
5442}
5443
5444/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5445 by cp_parser_builtin_offsetof. We're looking for
5446
5447 postfix-expression . template [opt] id-expression
5448 postfix-expression . pseudo-destructor-name
5449 postfix-expression -> template [opt] id-expression
5450 postfix-expression -> pseudo-destructor-name
5451
5452 FOR_OFFSETOF is set if we're being called in that context. That sorta
5453 limits what of the above we'll actually accept, but nevermind.
5454 TOKEN_TYPE is the "." or "->" token, which will already have been
5455 removed from the stream. */
5456
5457static tree
5458cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5459 enum cpp_ttype token_type,
5460 tree postfix_expression,
ad9ae192 5461 bool for_offsetof, cp_id_kind *idk,
5462 location_t location)
43bf5d72 5463{
5464 tree name;
5465 bool dependent_p;
19efe073 5466 bool pseudo_destructor_p;
43bf5d72 5467 tree scope = NULL_TREE;
5468
5469 /* If this is a `->' operator, dereference the pointer. */
5470 if (token_type == CPP_DEREF)
5471 postfix_expression = build_x_arrow (postfix_expression);
5472 /* Check to see whether or not the expression is type-dependent. */
5473 dependent_p = type_dependent_expression_p (postfix_expression);
5474 /* The identifier following the `->' or `.' is not qualified. */
5475 parser->scope = NULL_TREE;
5476 parser->qualifying_scope = NULL_TREE;
5477 parser->object_scope = NULL_TREE;
5478 *idk = CP_ID_KIND_NONE;
98b326fd 5479
43bf5d72 5480 /* Enter the scope corresponding to the type of the object
5481 given by the POSTFIX_EXPRESSION. */
130bb1d4 5482 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
43bf5d72 5483 {
130bb1d4 5484 scope = TREE_TYPE (postfix_expression);
43bf5d72 5485 /* According to the standard, no expression should ever have
5486 reference type. Unfortunately, we do not currently match
5487 the standard in this respect in that our internal representation
5488 of an expression may have reference type even when the standard
5489 says it does not. Therefore, we have to manually obtain the
5490 underlying type here. */
5491 scope = non_reference (scope);
5492 /* The type of the POSTFIX_EXPRESSION must be complete. */
e3efb139 5493 if (scope == unknown_type_node)
5494 {
ccb59bb6 5495 error_at (location, "%qE does not have class type",
5496 postfix_expression);
e3efb139 5497 scope = NULL_TREE;
5498 }
130bb1d4 5499 else
e3efb139 5500 scope = complete_type_or_else (scope, NULL_TREE);
130bb1d4 5501 /* Let the name lookup machinery know that we are processing a
5502 class member access expression. */
5503 parser->context->object_type = scope;
43bf5d72 5504 /* If something went wrong, we want to be able to discern that case,
5505 as opposed to the case where there was no SCOPE due to the type
5506 of expression being dependent. */
5507 if (!scope)
5508 scope = error_mark_node;
5509 /* If the SCOPE was erroneous, make the various semantic analysis
5510 functions exit quickly -- and without issuing additional error
5511 messages. */
5512 if (scope == error_mark_node)
5513 postfix_expression = error_mark_node;
5514 }
5515
19efe073 5516 /* Assume this expression is not a pseudo-destructor access. */
5517 pseudo_destructor_p = false;
5518
5519 /* If the SCOPE is a scalar type, then, if this is a valid program,
8da7c9e9 5520 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5521 is type dependent, it can be pseudo-destructor-name or something else.
5522 Try to parse it as pseudo-destructor-name first. */
5523 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
43bf5d72 5524 {
19efe073 5525 tree s;
5526 tree type;
5527
5528 cp_parser_parse_tentatively (parser);
5529 /* Parse the pseudo-destructor-name. */
5530 s = NULL_TREE;
5531 cp_parser_pseudo_destructor_name (parser, &s, &type);
8da7c9e9 5532 if (dependent_p
5533 && (cp_parser_error_occurred (parser)
5534 || TREE_CODE (type) != TYPE_DECL
5535 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5536 cp_parser_abort_tentative_parse (parser);
5537 else if (cp_parser_parse_definitely (parser))
19efe073 5538 {
5539 pseudo_destructor_p = true;
5540 postfix_expression
5541 = finish_pseudo_destructor_expr (postfix_expression,
5542 s, TREE_TYPE (type));
5543 }
5544 }
5545
5546 if (!pseudo_destructor_p)
5547 {
5548 /* If the SCOPE is not a scalar type, we are looking at an
5549 ordinary class member access expression, rather than a
5550 pseudo-destructor-name. */
fbb01da7 5551 bool template_p;
ad9ae192 5552 cp_token *token = cp_lexer_peek_token (parser->lexer);
43bf5d72 5553 /* Parse the id-expression. */
074ab442 5554 name = (cp_parser_id_expression
5555 (parser,
fbb01da7 5556 cp_parser_optional_template_keyword (parser),
5557 /*check_dependency_p=*/true,
5558 &template_p,
197c9df7 5559 /*declarator_p=*/false,
130bb1d4 5560 /*optional_p=*/false));
43bf5d72 5561 /* In general, build a SCOPE_REF if the member name is qualified.
5562 However, if the name was not dependent and has already been
5563 resolved; there is no need to build the SCOPE_REF. For example;
5564
653e5405 5565 struct X { void f(); };
5566 template <typename T> void f(T* t) { t->X::f(); }
43bf5d72 5567
5568 Even though "t" is dependent, "X::f" is not and has been resolved
5569 to a BASELINK; there is no need to include scope information. */
5570
5571 /* But we do need to remember that there was an explicit scope for
5572 virtual function calls. */
5573 if (parser->scope)
5574 *idk = CP_ID_KIND_QUALIFIED;
5575
e2ae55f2 5576 /* If the name is a template-id that names a type, we will get a
5577 TYPE_DECL here. That is invalid code. */
5578 if (TREE_CODE (name) == TYPE_DECL)
43bf5d72 5579 {
ccb59bb6 5580 error_at (token->location, "invalid use of %qD", name);
e2ae55f2 5581 postfix_expression = error_mark_node;
5582 }
5583 else
5584 {
5585 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5586 {
fbb01da7 5587 name = build_qualified_name (/*type=*/NULL_TREE,
5588 parser->scope,
5589 name,
5590 template_p);
e2ae55f2 5591 parser->scope = NULL_TREE;
5592 parser->qualifying_scope = NULL_TREE;
5593 parser->object_scope = NULL_TREE;
5594 }
5595 if (scope && name && BASELINK_P (name))
5596 adjust_result_of_qualified_name_lookup
c784dc88 5597 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
e2ae55f2 5598 postfix_expression
fbb01da7 5599 = finish_class_member_access_expr (postfix_expression, name,
ebd21de4 5600 template_p,
5601 tf_warning_or_error);
43bf5d72 5602 }
43bf5d72 5603 }
43bf5d72 5604
5605 /* We no longer need to look up names in the scope of the object on
5606 the left-hand side of the `.' or `->' operator. */
5607 parser->context->object_type = NULL_TREE;
5608
5609 /* Outside of offsetof, these operators may not appear in
5610 constant-expressions. */
5611 if (!for_offsetof
207355ad 5612 && (cp_parser_non_integral_constant_expression
c247dce0 5613 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
43bf5d72 5614 postfix_expression = error_mark_node;
5615
5616 return postfix_expression;
5617}
5618
0986fa22 5619/* Parse a parenthesized expression-list.
0a3b29ad 5620
5621 expression-list:
5622 assignment-expression
5623 expression-list, assignment-expression
5624
0986fa22 5625 attribute-list:
5626 expression-list
5627 identifier
5628 identifier, expression-list
5629
640aa28c 5630 CAST_P is true if this expression is the target of a cast.
5631
d95d815d 5632 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5633 argument pack.
5634
f352a3fb 5635 Returns a vector of trees. Each element is a representation of an
5636 assignment-expression. NULL is returned if the ( and or ) are
5637 missing. An empty, but allocated, vector is returned on no
33199a81 5638 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5639 if we are parsing an attribute list for an attribute that wants a
5640 plain identifier argument, normal_attr for an attribute that wants
5641 an expression, or non_attr if we aren't parsing an attribute list. If
f352a3fb 5642 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5643 not all of the expressions in the list were constant. */
0a3b29ad 5644
f352a3fb 5645static VEC(tree,gc) *
ccb84981 5646cp_parser_parenthesized_expression_list (cp_parser* parser,
33199a81 5647 int is_attribute_list,
640aa28c 5648 bool cast_p,
d95d815d 5649 bool allow_expansion_p,
878870b4 5650 bool *non_constant_p)
0a3b29ad 5651{
f352a3fb 5652 VEC(tree,gc) *expression_list;
33199a81 5653 bool fold_expr_p = is_attribute_list != non_attr;
0986fa22 5654 tree identifier = NULL_TREE;
9247ecc6 5655 bool saved_greater_than_is_operator_p;
878870b4 5656
5657 /* Assume all the expressions will be constant. */
5658 if (non_constant_p)
5659 *non_constant_p = false;
5660
c247dce0 5661 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
f352a3fb 5662 return NULL;
5663
5664 expression_list = make_tree_vector ();
ccb84981 5665
9247ecc6 5666 /* Within a parenthesized expression, a `>' token is always
5667 the greater-than operator. */
5668 saved_greater_than_is_operator_p
5669 = parser->greater_than_is_operator_p;
5670 parser->greater_than_is_operator_p = true;
5671
0a3b29ad 5672 /* Consume expressions until there are no more. */
0986fa22 5673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5674 while (true)
5675 {
5676 tree expr;
ccb84981 5677
0986fa22 5678 /* At the beginning of attribute lists, check to see if the
5679 next token is an identifier. */
33199a81 5680 if (is_attribute_list == id_attr
0986fa22 5681 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5682 {
5683 cp_token *token;
ccb84981 5684
0986fa22 5685 /* Consume the identifier. */
5686 token = cp_lexer_consume_token (parser->lexer);
5687 /* Save the identifier. */
3369eb76 5688 identifier = token->u.value;
0986fa22 5689 }
5690 else
5691 {
f82f1250 5692 bool expr_non_constant_p;
5693
0986fa22 5694 /* Parse the next assignment-expression. */
f82f1250 5695 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5696 {
5697 /* A braced-init-list. */
bf8d19fe 5698 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 5699 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5700 if (non_constant_p && expr_non_constant_p)
5701 *non_constant_p = true;
5702 }
5703 else if (non_constant_p)
878870b4 5704 {
ccb84981 5705 expr = (cp_parser_constant_expression
878870b4 5706 (parser, /*allow_non_constant_p=*/true,
5707 &expr_non_constant_p));
5708 if (expr_non_constant_p)
5709 *non_constant_p = true;
5710 }
5711 else
98b326fd 5712 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
0a3b29ad 5713
4736f3be 5714 if (fold_expr_p)
5715 expr = fold_non_dependent_expr (expr);
5716
d95d815d 5717 /* If we have an ellipsis, then this is an expression
5718 expansion. */
5719 if (allow_expansion_p
5720 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5721 {
5722 /* Consume the `...'. */
5723 cp_lexer_consume_token (parser->lexer);
5724
5725 /* Build the argument pack. */
5726 expr = make_pack_expansion (expr);
5727 }
5728
0986fa22 5729 /* Add it to the list. We add error_mark_node
5730 expressions to the list, so that we can still tell if
5731 the correct form for a parenthesized expression-list
5732 is found. That gives better errors. */
f352a3fb 5733 VEC_safe_push (tree, gc, expression_list, expr);
0a3b29ad 5734
0986fa22 5735 if (expr == error_mark_node)
5736 goto skip_comma;
5737 }
0a3b29ad 5738
0986fa22 5739 /* After the first item, attribute lists look the same as
5740 expression lists. */
33199a81 5741 is_attribute_list = non_attr;
ccb84981 5742
0986fa22 5743 get_comma:;
5744 /* If the next token isn't a `,', then we are done. */
5745 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5746 break;
5747
5748 /* Otherwise, consume the `,' and keep going. */
5749 cp_lexer_consume_token (parser->lexer);
5750 }
ccb84981 5751
c247dce0 5752 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
0986fa22 5753 {
5754 int ending;
ccb84981 5755
0986fa22 5756 skip_comma:;
5757 /* We try and resync to an unnested comma, as that will give the
5758 user better diagnostics. */
ccb84981 5759 ending = cp_parser_skip_to_closing_parenthesis (parser,
5760 /*recovering=*/true,
92b128ed 5761 /*or_comma=*/true,
3d0f901b 5762 /*consume_paren=*/true);
0986fa22 5763 if (ending < 0)
5764 goto get_comma;
5765 if (!ending)
9247ecc6 5766 {
5767 parser->greater_than_is_operator_p
5768 = saved_greater_than_is_operator_p;
f352a3fb 5769 return NULL;
9247ecc6 5770 }
0a3b29ad 5771 }
5772
9247ecc6 5773 parser->greater_than_is_operator_p
5774 = saved_greater_than_is_operator_p;
5775
0986fa22 5776 if (identifier)
f352a3fb 5777 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
ccb84981 5778
0986fa22 5779 return expression_list;
0a3b29ad 5780}
5781
5782/* Parse a pseudo-destructor-name.
5783
5784 pseudo-destructor-name:
5785 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5786 :: [opt] nested-name-specifier template template-id :: ~ type-name
5787 :: [opt] nested-name-specifier [opt] ~ type-name
5788
5789 If either of the first two productions is used, sets *SCOPE to the
5790 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5791 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
60c6ea84 5792 or ERROR_MARK_NODE if the parse fails. */
0a3b29ad 5793
5794static void
ccb84981 5795cp_parser_pseudo_destructor_name (cp_parser* parser,
653e5405 5796 tree* scope,
5797 tree* type)
0a3b29ad 5798{
5799 bool nested_name_specifier_p;
5800
30aea172 5801 /* Assume that things will not work out. */
5802 *type = error_mark_node;
5803
0a3b29ad 5804 /* Look for the optional `::' operator. */
130bb1d4 5805 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
0a3b29ad 5806 /* Look for the optional nested-name-specifier. */
ccb84981 5807 nested_name_specifier_p
0a3b29ad 5808 = (cp_parser_nested_name_specifier_opt (parser,
5809 /*typename_keyword_p=*/false,
5810 /*check_dependency_p=*/true,
3d0f901b 5811 /*type_p=*/false,
d046014d 5812 /*is_declaration=*/false)
0a3b29ad 5813 != NULL_TREE);
5814 /* Now, if we saw a nested-name-specifier, we might be doing the
5815 second production. */
ccb84981 5816 if (nested_name_specifier_p
0a3b29ad 5817 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5818 {
5819 /* Consume the `template' keyword. */
5820 cp_lexer_consume_token (parser->lexer);
5821 /* Parse the template-id. */
ccb84981 5822 cp_parser_template_id (parser,
0a3b29ad 5823 /*template_keyword_p=*/true,
3d0f901b 5824 /*check_dependency_p=*/false,
5825 /*is_declaration=*/true);
0a3b29ad 5826 /* Look for the `::' token. */
c247dce0 5827 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 5828 }
5829 /* If the next token is not a `~', then there might be some
6beb3f76 5830 additional qualification. */
0a3b29ad 5831 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5832 {
d1cc3060 5833 /* At this point, we're looking for "type-name :: ~". The type-name
5834 must not be a class-name, since this is a pseudo-destructor. So,
5835 it must be either an enum-name, or a typedef-name -- both of which
5836 are just identifiers. So, we peek ahead to check that the "::"
5837 and "~" tokens are present; if they are not, then we can avoid
5838 calling type_name. */
5839 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5840 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5841 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5842 {
5843 cp_parser_error (parser, "non-scalar type");
5844 return;
5845 }
5846
0a3b29ad 5847 /* Look for the type-name. */
674e90bd 5848 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
30aea172 5849 if (*scope == error_mark_node)
5850 return;
5851
0a3b29ad 5852 /* Look for the `::' token. */
c247dce0 5853 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 5854 }
5855 else
5856 *scope = NULL_TREE;
5857
5858 /* Look for the `~'. */
c247dce0 5859 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
0a3b29ad 5860 /* Look for the type-name again. We are not responsible for
5861 checking that it matches the first type-name. */
674e90bd 5862 *type = cp_parser_nonclass_name (parser);
0a3b29ad 5863}
5864
5865/* Parse a unary-expression.
5866
5867 unary-expression:
5868 postfix-expression
5869 ++ cast-expression
5870 -- cast-expression
5871 unary-operator cast-expression
5872 sizeof unary-expression
5873 sizeof ( type-id )
5874 new-expression
5875 delete-expression
5876
5877 GNU Extensions:
5878
5879 unary-expression:
5880 __extension__ cast-expression
5881 __alignof__ unary-expression
5882 __alignof__ ( type-id )
5883 __real__ cast-expression
5884 __imag__ cast-expression
5885 && identifier
5886
5887 ADDRESS_P is true iff the unary-expression is appearing as the
640aa28c 5888 operand of the `&' operator. CAST_P is true if this expression is
5889 the target of a cast.
0a3b29ad 5890
755edffd 5891 Returns a representation of the expression. */
0a3b29ad 5892
5893static tree
98b326fd 5894cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5895 cp_id_kind * pidk)
0a3b29ad 5896{
5897 cp_token *token;
5898 enum tree_code unary_operator;
5899
5900 /* Peek at the next token. */
5901 token = cp_lexer_peek_token (parser->lexer);
5902 /* Some keywords give away the kind of expression. */
5903 if (token->type == CPP_KEYWORD)
5904 {
5905 enum rid keyword = token->keyword;
5906
5907 switch (keyword)
5908 {
5909 case RID_ALIGNOF:
0a3b29ad 5910 case RID_SIZEOF:
5911 {
5912 tree operand;
e47f82ba 5913 enum tree_code op;
ccb84981 5914
e47f82ba 5915 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5916 /* Consume the token. */
0a3b29ad 5917 cp_lexer_consume_token (parser->lexer);
5918 /* Parse the operand. */
5919 operand = cp_parser_sizeof_operand (parser, keyword);
5920
e47f82ba 5921 if (TYPE_P (operand))
5922 return cxx_sizeof_or_alignof_type (operand, op, true);
0a3b29ad 5923 else
ebd21de4 5924 return cxx_sizeof_or_alignof_expr (operand, op, true);
0a3b29ad 5925 }
5926
5927 case RID_NEW:
5928 return cp_parser_new_expression (parser);
5929
5930 case RID_DELETE:
5931 return cp_parser_delete_expression (parser);
ccb84981 5932
0a3b29ad 5933 case RID_EXTENSION:
5934 {
5935 /* The saved value of the PEDANTIC flag. */
5936 int saved_pedantic;
5937 tree expr;
5938
5939 /* Save away the PEDANTIC flag. */
5940 cp_parser_extension_opt (parser, &saved_pedantic);
5941 /* Parse the cast-expression. */
a63bc44c 5942 expr = cp_parser_simple_cast_expression (parser);
0a3b29ad 5943 /* Restore the PEDANTIC flag. */
5944 pedantic = saved_pedantic;
5945
5946 return expr;
5947 }
5948
5949 case RID_REALPART:
5950 case RID_IMAGPART:
5951 {
5952 tree expression;
5953
5954 /* Consume the `__real__' or `__imag__' token. */
5955 cp_lexer_consume_token (parser->lexer);
5956 /* Parse the cast-expression. */
a63bc44c 5957 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 5958 /* Create the complete representation. */
5959 return build_x_unary_op ((keyword == RID_REALPART
5960 ? REALPART_EXPR : IMAGPART_EXPR),
ebd21de4 5961 expression,
5962 tf_warning_or_error);
0a3b29ad 5963 }
5964 break;
5965
98fe9664 5966 case RID_NOEXCEPT:
5967 {
5968 tree expr;
5969 const char *saved_message;
5970 bool saved_integral_constant_expression_p;
5971 bool saved_non_integral_constant_expression_p;
5972 bool saved_greater_than_is_operator_p;
5973
5974 cp_lexer_consume_token (parser->lexer);
5975 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5976
5977 saved_message = parser->type_definition_forbidden_message;
5978 parser->type_definition_forbidden_message
5979 = G_("types may not be defined in %<noexcept%> expressions");
5980
5981 saved_integral_constant_expression_p
5982 = parser->integral_constant_expression_p;
5983 saved_non_integral_constant_expression_p
5984 = parser->non_integral_constant_expression_p;
5985 parser->integral_constant_expression_p = false;
5986
5987 saved_greater_than_is_operator_p
5988 = parser->greater_than_is_operator_p;
5989 parser->greater_than_is_operator_p = true;
5990
5991 ++cp_unevaluated_operand;
5992 ++c_inhibit_evaluation_warnings;
5993 expr = cp_parser_expression (parser, false, NULL);
5994 --c_inhibit_evaluation_warnings;
5995 --cp_unevaluated_operand;
5996
5997 parser->greater_than_is_operator_p
5998 = saved_greater_than_is_operator_p;
5999
6000 parser->integral_constant_expression_p
6001 = saved_integral_constant_expression_p;
6002 parser->non_integral_constant_expression_p
6003 = saved_non_integral_constant_expression_p;
6004
6005 parser->type_definition_forbidden_message = saved_message;
6006
6007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
1194d077 6008 return finish_noexcept_expr (expr, tf_warning_or_error);
98fe9664 6009 }
6010
0a3b29ad 6011 default:
6012 break;
6013 }
6014 }
6015
6016 /* Look for the `:: new' and `:: delete', which also signal the
6017 beginning of a new-expression, or delete-expression,
6018 respectively. If the next token is `::', then it might be one of
6019 these. */
6020 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6021 {
6022 enum rid keyword;
6023
6024 /* See if the token after the `::' is one of the keywords in
6025 which we're interested. */
6026 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6027 /* If it's `new', we have a new-expression. */
6028 if (keyword == RID_NEW)
6029 return cp_parser_new_expression (parser);
6030 /* Similarly, for `delete'. */
6031 else if (keyword == RID_DELETE)
6032 return cp_parser_delete_expression (parser);
6033 }
6034
6035 /* Look for a unary operator. */
6036 unary_operator = cp_parser_unary_operator (token);
6037 /* The `++' and `--' operators can be handled similarly, even though
6038 they are not technically unary-operators in the grammar. */
6039 if (unary_operator == ERROR_MARK)
6040 {
6041 if (token->type == CPP_PLUS_PLUS)
6042 unary_operator = PREINCREMENT_EXPR;
6043 else if (token->type == CPP_MINUS_MINUS)
6044 unary_operator = PREDECREMENT_EXPR;
6045 /* Handle the GNU address-of-label extension. */
6046 else if (cp_parser_allow_gnu_extensions_p (parser)
6047 && token->type == CPP_AND_AND)
6048 {
6049 tree identifier;
89966701 6050 tree expression;
dda49785 6051 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 6052
6053 /* Consume the '&&' token. */
6054 cp_lexer_consume_token (parser->lexer);
6055 /* Look for the identifier. */
6056 identifier = cp_parser_identifier (parser);
6057 /* Create an expression representing the address. */
dda49785 6058 expression = finish_label_address_expr (identifier, loc);
89966701 6059 if (cp_parser_non_integral_constant_expression (parser,
c247dce0 6060 NIC_ADDR_LABEL))
89966701 6061 expression = error_mark_node;
6062 return expression;
0a3b29ad 6063 }
6064 }
6065 if (unary_operator != ERROR_MARK)
6066 {
6067 tree cast_expression;
364c2f43 6068 tree expression = error_mark_node;
c61e1212 6069 non_integral_constant non_constant_p = NIC_NONE;
0a3b29ad 6070
6071 /* Consume the operator token. */
6072 token = cp_lexer_consume_token (parser->lexer);
6073 /* Parse the cast-expression. */
ccb84981 6074 cast_expression
9031d10b 6075 = cp_parser_cast_expression (parser,
640aa28c 6076 unary_operator == ADDR_EXPR,
98b326fd 6077 /*cast_p=*/false, pidk);
0a3b29ad 6078 /* Now, build an appropriate representation. */
6079 switch (unary_operator)
6080 {
6081 case INDIRECT_REF:
c247dce0 6082 non_constant_p = NIC_STAR;
f08923b3 6083 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
ebd21de4 6084 tf_warning_or_error);
364c2f43 6085 break;
6086
0a3b29ad 6087 case ADDR_EXPR:
c247dce0 6088 non_constant_p = NIC_ADDR;
364c2f43 6089 /* Fall through. */
13795292 6090 case BIT_NOT_EXPR:
ebd21de4 6091 expression = build_x_unary_op (unary_operator, cast_expression,
6092 tf_warning_or_error);
364c2f43 6093 break;
6094
5f6526e1 6095 case PREINCREMENT_EXPR:
6096 case PREDECREMENT_EXPR:
c247dce0 6097 non_constant_p = unary_operator == PREINCREMENT_EXPR
6098 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5f6526e1 6099 /* Fall through. */
97d541d5 6100 case UNARY_PLUS_EXPR:
0a3b29ad 6101 case NEGATE_EXPR:
6102 case TRUTH_NOT_EXPR:
364c2f43 6103 expression = finish_unary_op_expr (unary_operator, cast_expression);
6104 break;
0a3b29ad 6105
0a3b29ad 6106 default:
2e3e31d2 6107 gcc_unreachable ();
0a3b29ad 6108 }
364c2f43 6109
c61e1212 6110 if (non_constant_p != NIC_NONE
3938e0c2 6111 && cp_parser_non_integral_constant_expression (parser,
6112 non_constant_p))
6113 expression = error_mark_node;
364c2f43 6114
6115 return expression;
0a3b29ad 6116 }
6117
34da8800 6118 return cp_parser_postfix_expression (parser, address_p, cast_p,
98b326fd 6119 /*member_access_only_p=*/false,
6120 pidk);
0a3b29ad 6121}
6122
6123/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6124 unary-operator, the corresponding tree code is returned. */
6125
6126static enum tree_code
45baea8b 6127cp_parser_unary_operator (cp_token* token)
0a3b29ad 6128{
6129 switch (token->type)
6130 {
6131 case CPP_MULT:
6132 return INDIRECT_REF;
6133
6134 case CPP_AND:
6135 return ADDR_EXPR;
6136
6137 case CPP_PLUS:
97d541d5 6138 return UNARY_PLUS_EXPR;
0a3b29ad 6139
6140 case CPP_MINUS:
6141 return NEGATE_EXPR;
6142
6143 case CPP_NOT:
6144 return TRUTH_NOT_EXPR;
ccb84981 6145
0a3b29ad 6146 case CPP_COMPL:
6147 return BIT_NOT_EXPR;
6148
6149 default:
6150 return ERROR_MARK;
6151 }
6152}
6153
6154/* Parse a new-expression.
6155
5c6faf71 6156 new-expression:
0a3b29ad 6157 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6158 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6159
6160 Returns a representation of the expression. */
6161
6162static tree
45baea8b 6163cp_parser_new_expression (cp_parser* parser)
0a3b29ad 6164{
6165 bool global_scope_p;
f352a3fb 6166 VEC(tree,gc) *placement;
0a3b29ad 6167 tree type;
f352a3fb 6168 VEC(tree,gc) *initializer;
3046c0a3 6169 tree nelts;
f352a3fb 6170 tree ret;
0a3b29ad 6171
6172 /* Look for the optional `::' operator. */
ccb84981 6173 global_scope_p
0a3b29ad 6174 = (cp_parser_global_scope_opt (parser,
130bb1d4 6175 /*current_scope_valid_p=*/false)
0a3b29ad 6176 != NULL_TREE);
6177 /* Look for the `new' operator. */
c247dce0 6178 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
0a3b29ad 6179 /* There's no easy way to tell a new-placement from the
6180 `( type-id )' construct. */
6181 cp_parser_parse_tentatively (parser);
6182 /* Look for a new-placement. */
6183 placement = cp_parser_new_placement (parser);
6184 /* If that didn't work out, there's no new-placement. */
6185 if (!cp_parser_parse_definitely (parser))
f352a3fb 6186 {
6187 if (placement != NULL)
6188 release_tree_vector (placement);
6189 placement = NULL;
6190 }
0a3b29ad 6191
6192 /* If the next token is a `(', then we have a parenthesized
6193 type-id. */
6194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6195 {
ad9ae192 6196 cp_token *token;
0a3b29ad 6197 /* Consume the `('. */
6198 cp_lexer_consume_token (parser->lexer);
6199 /* Parse the type-id. */
6200 type = cp_parser_type_id (parser);
6201 /* Look for the closing `)'. */
c247dce0 6202 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
ad9ae192 6203 token = cp_lexer_peek_token (parser->lexer);
207355ad 6204 /* There should not be a direct-new-declarator in this production,
653e5405 6205 but GCC used to allowed this, so we check and emit a sensible error
383da0a4 6206 message for this case. */
6207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
f352cc1d 6208 {
ccb59bb6 6209 error_at (token->location,
6210 "array bound forbidden after parenthesized type-id");
5bcc316e 6211 inform (token->location,
6212 "try removing the parentheses around the type-id");
383da0a4 6213 cp_parser_direct_new_declarator (parser);
6214 }
19efe073 6215 nelts = NULL_TREE;
0a3b29ad 6216 }
6217 /* Otherwise, there must be a new-type-id. */
6218 else
3046c0a3 6219 type = cp_parser_new_type_id (parser, &nelts);
0a3b29ad 6220
f82f1250 6221 /* If the next token is a `(' or '{', then we have a new-initializer. */
6222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6223 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 6224 initializer = cp_parser_new_initializer (parser);
6225 else
f352a3fb 6226 initializer = NULL;
0a3b29ad 6227
3938e0c2 6228 /* A new-expression may not appear in an integral constant
6229 expression. */
c247dce0 6230 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
f352a3fb 6231 ret = error_mark_node;
6232 else
6233 {
6234 /* Create a representation of the new-expression. */
6235 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6236 tf_warning_or_error);
6237 }
3938e0c2 6238
f352a3fb 6239 if (placement != NULL)
6240 release_tree_vector (placement);
6241 if (initializer != NULL)
6242 release_tree_vector (initializer);
6243
6244 return ret;
0a3b29ad 6245}
6246
6247/* Parse a new-placement.
6248
6249 new-placement:
6250 ( expression-list )
6251
6252 Returns the same representation as for an expression-list. */
6253
f352a3fb 6254static VEC(tree,gc) *
45baea8b 6255cp_parser_new_placement (cp_parser* parser)
0a3b29ad 6256{
f352a3fb 6257 VEC(tree,gc) *expression_list;
0a3b29ad 6258
0a3b29ad 6259 /* Parse the expression-list. */
ccb84981 6260 expression_list = (cp_parser_parenthesized_expression_list
33199a81 6261 (parser, non_attr, /*cast_p=*/false,
6262 /*allow_expansion_p=*/true,
640aa28c 6263 /*non_constant_p=*/NULL));
0a3b29ad 6264
6265 return expression_list;
6266}
6267
6268/* Parse a new-type-id.
6269
6270 new-type-id:
6271 type-specifier-seq new-declarator [opt]
6272
3046c0a3 6273 Returns the TYPE allocated. If the new-type-id indicates an array
6274 type, *NELTS is set to the number of elements in the last array
6275 bound; the TYPE will not include the last array bound. */
0a3b29ad 6276
6277static tree
3046c0a3 6278cp_parser_new_type_id (cp_parser* parser, tree *nelts)
0a3b29ad 6279{
4b9b2871 6280 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 6281 cp_declarator *new_declarator;
6282 cp_declarator *declarator;
6283 cp_declarator *outer_declarator;
0a3b29ad 6284 const char *saved_message;
3046c0a3 6285 tree type;
0a3b29ad 6286
6287 /* The type-specifier sequence must not contain type definitions.
6288 (It cannot contain declarations of new types either, but if they
6289 are not definitions we will catch that because they are not
6290 complete.) */
6291 saved_message = parser->type_definition_forbidden_message;
6292 parser->type_definition_forbidden_message
ca82e026 6293 = G_("types may not be defined in a new-type-id");
0a3b29ad 6294 /* Parse the type-specifier-seq. */
c44f7faf 6295 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 6296 /*is_trailing_return=*/false,
6f74fe3c 6297 &type_specifier_seq);
0a3b29ad 6298 /* Restore the old message. */
6299 parser->type_definition_forbidden_message = saved_message;
6300 /* Parse the new-declarator. */
3046c0a3 6301 new_declarator = cp_parser_new_declarator_opt (parser);
6302
6303 /* Determine the number of elements in the last array dimension, if
6304 any. */
6305 *nelts = NULL_TREE;
6306 /* Skip down to the last array dimension. */
6307 declarator = new_declarator;
6308 outer_declarator = NULL;
6309 while (declarator && (declarator->kind == cdk_pointer
6310 || declarator->kind == cdk_ptrmem))
6311 {
6312 outer_declarator = declarator;
6313 declarator = declarator->declarator;
6314 }
207355ad 6315 while (declarator
3046c0a3 6316 && declarator->kind == cdk_array
6317 && declarator->declarator
6318 && declarator->declarator->kind == cdk_array)
6319 {
6320 outer_declarator = declarator;
6321 declarator = declarator->declarator;
6322 }
207355ad 6323
3046c0a3 6324 if (declarator && declarator->kind == cdk_array)
6325 {
6326 *nelts = declarator->u.array.bounds;
6327 if (*nelts == error_mark_node)
6328 *nelts = integer_one_node;
9031d10b 6329
3046c0a3 6330 if (outer_declarator)
6331 outer_declarator->declarator = declarator->declarator;
6332 else
6333 new_declarator = NULL;
6334 }
0a3b29ad 6335
75eaa947 6336 type = groktypename (&type_specifier_seq, new_declarator, false);
3046c0a3 6337 return type;
0a3b29ad 6338}
6339
6340/* Parse an (optional) new-declarator.
6341
6342 new-declarator:
6343 ptr-operator new-declarator [opt]
6344 direct-new-declarator
6345
3046c0a3 6346 Returns the declarator. */
0a3b29ad 6347
3046c0a3 6348static cp_declarator *
45baea8b 6349cp_parser_new_declarator_opt (cp_parser* parser)
0a3b29ad 6350{
6351 enum tree_code code;
6352 tree type;
2cfb6cde 6353 cp_cv_quals cv_quals;
0a3b29ad 6354
6355 /* We don't know if there's a ptr-operator next, or not. */
6356 cp_parser_parse_tentatively (parser);
6357 /* Look for a ptr-operator. */
2cfb6cde 6358 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
0a3b29ad 6359 /* If that worked, look for more new-declarators. */
6360 if (cp_parser_parse_definitely (parser))
6361 {
3046c0a3 6362 cp_declarator *declarator;
0a3b29ad 6363
6364 /* Parse another optional declarator. */
6365 declarator = cp_parser_new_declarator_opt (parser);
6366
63949b38 6367 return cp_parser_make_indirect_declarator
6368 (code, type, cv_quals, declarator);
0a3b29ad 6369 }
6370
6371 /* If the next token is a `[', there is a direct-new-declarator. */
6372 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6373 return cp_parser_direct_new_declarator (parser);
6374
3046c0a3 6375 return NULL;
0a3b29ad 6376}
6377
6378/* Parse a direct-new-declarator.
6379
6380 direct-new-declarator:
6381 [ expression ]
ccb84981 6382 direct-new-declarator [constant-expression]
0a3b29ad 6383
3046c0a3 6384 */
0a3b29ad 6385
3046c0a3 6386static cp_declarator *
45baea8b 6387cp_parser_direct_new_declarator (cp_parser* parser)
0a3b29ad 6388{
3046c0a3 6389 cp_declarator *declarator = NULL;
0a3b29ad 6390
6391 while (true)
6392 {
6393 tree expression;
6394
6395 /* Look for the opening `['. */
c247dce0 6396 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
0a3b29ad 6397 /* The first expression is not required to be constant. */
6398 if (!declarator)
6399 {
ad9ae192 6400 cp_token *token = cp_lexer_peek_token (parser->lexer);
98b326fd 6401 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 6402 /* The standard requires that the expression have integral
6403 type. DR 74 adds enumeration types. We believe that the
6404 real intent is that these expressions be handled like the
6405 expression in a `switch' condition, which also allows
6406 classes with a single conversion to integral or
6407 enumeration type. */
6408 if (!processing_template_decl)
6409 {
ccb84981 6410 expression
0a3b29ad 6411 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6412 expression,
35771a9a 6413 /*complain=*/true);
0a3b29ad 6414 if (!expression)
6415 {
ccb59bb6 6416 error_at (token->location,
6417 "expression in new-declarator must have integral "
6418 "or enumeration type");
0a3b29ad 6419 expression = error_mark_node;
6420 }
6421 }
6422 }
6423 /* But all the other expressions must be. */
6424 else
ccb84981 6425 expression
6426 = cp_parser_constant_expression (parser,
5f6526e1 6427 /*allow_non_constant=*/false,
6428 NULL);
0a3b29ad 6429 /* Look for the closing `]'. */
c247dce0 6430 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 6431
6432 /* Add this bound to the declarator. */
3046c0a3 6433 declarator = make_array_declarator (declarator, expression);
0a3b29ad 6434
6435 /* If the next token is not a `[', then there are no more
6436 bounds. */
6437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6438 break;
6439 }
6440
6441 return declarator;
6442}
6443
6444/* Parse a new-initializer.
6445
6446 new-initializer:
6447 ( expression-list [opt] )
f82f1250 6448 braced-init-list
0a3b29ad 6449
f352a3fb 6450 Returns a representation of the expression-list. */
0a3b29ad 6451
f352a3fb 6452static VEC(tree,gc) *
45baea8b 6453cp_parser_new_initializer (cp_parser* parser)
0a3b29ad 6454{
f352a3fb 6455 VEC(tree,gc) *expression_list;
0a3b29ad 6456
f82f1250 6457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6458 {
f352a3fb 6459 tree t;
f82f1250 6460 bool expr_non_constant_p;
bf8d19fe 6461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f352a3fb 6462 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6463 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6464 expression_list = make_tree_vector_single (t);
f82f1250 6465 }
6466 else
6467 expression_list = (cp_parser_parenthesized_expression_list
33199a81 6468 (parser, non_attr, /*cast_p=*/false,
6469 /*allow_expansion_p=*/true,
f82f1250 6470 /*non_constant_p=*/NULL));
0a3b29ad 6471
6472 return expression_list;
6473}
6474
6475/* Parse a delete-expression.
6476
6477 delete-expression:
6478 :: [opt] delete cast-expression
6479 :: [opt] delete [ ] cast-expression
6480
6481 Returns a representation of the expression. */
6482
6483static tree
45baea8b 6484cp_parser_delete_expression (cp_parser* parser)
0a3b29ad 6485{
6486 bool global_scope_p;
6487 bool array_p;
6488 tree expression;
6489
6490 /* Look for the optional `::' operator. */
6491 global_scope_p
6492 = (cp_parser_global_scope_opt (parser,
130bb1d4 6493 /*current_scope_valid_p=*/false)
0a3b29ad 6494 != NULL_TREE);
6495 /* Look for the `delete' keyword. */
c247dce0 6496 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
0a3b29ad 6497 /* See if the array syntax is in use. */
6498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6499 {
6500 /* Consume the `[' token. */
6501 cp_lexer_consume_token (parser->lexer);
6502 /* Look for the `]' token. */
c247dce0 6503 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 6504 /* Remember that this is the `[]' construct. */
6505 array_p = true;
6506 }
6507 else
6508 array_p = false;
6509
6510 /* Parse the cast-expression. */
a63bc44c 6511 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 6512
3938e0c2 6513 /* A delete-expression may not appear in an integral constant
6514 expression. */
c247dce0 6515 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
3938e0c2 6516 return error_mark_node;
6517
0a3b29ad 6518 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6519}
6520
589d356c 6521/* Returns true if TOKEN may start a cast-expression and false
6522 otherwise. */
6523
6524static bool
6525cp_parser_token_starts_cast_expression (cp_token *token)
6526{
6527 switch (token->type)
6528 {
6529 case CPP_COMMA:
6530 case CPP_SEMICOLON:
6531 case CPP_QUERY:
6532 case CPP_COLON:
6533 case CPP_CLOSE_SQUARE:
6534 case CPP_CLOSE_PAREN:
6535 case CPP_CLOSE_BRACE:
6536 case CPP_DOT:
6537 case CPP_DOT_STAR:
6538 case CPP_DEREF:
6539 case CPP_DEREF_STAR:
6540 case CPP_DIV:
6541 case CPP_MOD:
6542 case CPP_LSHIFT:
6543 case CPP_RSHIFT:
6544 case CPP_LESS:
6545 case CPP_GREATER:
6546 case CPP_LESS_EQ:
6547 case CPP_GREATER_EQ:
6548 case CPP_EQ_EQ:
6549 case CPP_NOT_EQ:
6550 case CPP_EQ:
6551 case CPP_MULT_EQ:
6552 case CPP_DIV_EQ:
6553 case CPP_MOD_EQ:
6554 case CPP_PLUS_EQ:
6555 case CPP_MINUS_EQ:
6556 case CPP_RSHIFT_EQ:
6557 case CPP_LSHIFT_EQ:
6558 case CPP_AND_EQ:
6559 case CPP_XOR_EQ:
6560 case CPP_OR_EQ:
6561 case CPP_XOR:
6562 case CPP_OR:
6563 case CPP_OR_OR:
c19d7ab2 6564 case CPP_EOF:
589d356c 6565 return false;
6566
6567 /* '[' may start a primary-expression in obj-c++. */
6568 case CPP_OPEN_SQUARE:
6569 return c_dialect_objc ();
6570
6571 default:
6572 return true;
6573 }
6574}
6575
0a3b29ad 6576/* Parse a cast-expression.
6577
6578 cast-expression:
6579 unary-expression
6580 ( type-id ) cast-expression
6581
640aa28c 6582 ADDRESS_P is true iff the unary-expression is appearing as the
6583 operand of the `&' operator. CAST_P is true if this expression is
6584 the target of a cast.
6585
0a3b29ad 6586 Returns a representation of the expression. */
6587
6588static tree
98b326fd 6589cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6590 cp_id_kind * pidk)
0a3b29ad 6591{
6592 /* If it's a `(', then we might be looking at a cast. */
6593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6594 {
6595 tree type = NULL_TREE;
6596 tree expr = NULL_TREE;
6597 bool compound_literal_p;
6598 const char *saved_message;
6599
6600 /* There's no way to know yet whether or not this is a cast.
6601 For example, `(int (3))' is a unary-expression, while `(int)
6602 3' is a cast. So, we resort to parsing tentatively. */
6603 cp_parser_parse_tentatively (parser);
6604 /* Types may not be defined in a cast. */
6605 saved_message = parser->type_definition_forbidden_message;
6606 parser->type_definition_forbidden_message
ca82e026 6607 = G_("types may not be defined in casts");
0a3b29ad 6608 /* Consume the `('. */
6609 cp_lexer_consume_token (parser->lexer);
6610 /* A very tricky bit is that `(struct S) { 3 }' is a
6611 compound-literal (which we permit in C++ as an extension).
6612 But, that construct is not a cast-expression -- it is a
6613 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6614 is legal; if the compound-literal were a cast-expression,
6615 you'd need an extra set of parentheses.) But, if we parse
6616 the type-id, and it happens to be a class-specifier, then we
6617 will commit to the parse at that point, because we cannot
6618 undo the action that is done when creating a new class. So,
ccb84981 6619 then we cannot back up and do a postfix-expression.
0a3b29ad 6620
6621 Therefore, we scan ahead to the closing `)', and check to see
6622 if the token after the `)' is a `{'. If so, we are not
ccb84981 6623 looking at a cast-expression.
0a3b29ad 6624
6625 Save tokens so that we can put them back. */
6626 cp_lexer_save_tokens (parser->lexer);
6627 /* Skip tokens until the next token is a closing parenthesis.
6628 If we find the closing `)', and the next token is a `{', then
6629 we are looking at a compound-literal. */
ccb84981 6630 compound_literal_p
3d0f901b 6631 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6632 /*consume_paren=*/true)
0a3b29ad 6633 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6634 /* Roll back the tokens we skipped. */
6635 cp_lexer_rollback_tokens (parser->lexer);
6636 /* If we were looking at a compound-literal, simulate an error
6637 so that the call to cp_parser_parse_definitely below will
6638 fail. */
6639 if (compound_literal_p)
6640 cp_parser_simulate_error (parser);
6641 else
6642 {
41f2d08e 6643 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6644 parser->in_type_id_in_expr_p = true;
0a3b29ad 6645 /* Look for the type-id. */
6646 type = cp_parser_type_id (parser);
6647 /* Look for the closing `)'. */
c247dce0 6648 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
41f2d08e 6649 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 6650 }
6651
6652 /* Restore the saved message. */
6653 parser->type_definition_forbidden_message = saved_message;
6654
589d356c 6655 /* At this point this can only be either a cast or a
6656 parenthesized ctor such as `(T ())' that looks like a cast to
6657 function returning T. */
6658 if (!cp_parser_error_occurred (parser)
6659 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6660 (parser->lexer)))
0a3b29ad 6661 {
589d356c 6662 cp_parser_parse_definitely (parser);
6663 expr = cp_parser_cast_expression (parser,
6664 /*address_p=*/false,
98b326fd 6665 /*cast_p=*/true, pidk);
589d356c 6666
0a3b29ad 6667 /* Warn about old-style casts, if so requested. */
ccb84981 6668 if (warn_old_style_cast
6669 && !in_system_header
6670 && !VOID_TYPE_P (type)
0a3b29ad 6671 && current_lang_name != lang_name_c)
ced7c954 6672 warning (OPT_Wold_style_cast, "use of old-style cast");
5f6526e1 6673
6674 /* Only type conversions to integral or enumeration types
6675 can be used in constant-expressions. */
bde9ebf7 6676 if (!cast_valid_in_integral_constant_expression_p (type)
c61e1212 6677 && cp_parser_non_integral_constant_expression (parser,
6678 NIC_CAST))
3938e0c2 6679 return error_mark_node;
6680
0a3b29ad 6681 /* Perform the cast. */
e60a6f7b 6682 expr = build_c_cast (input_location, type, expr);
d622b3bd 6683 return expr;
0a3b29ad 6684 }
589d356c 6685 else
6686 cp_parser_abort_tentative_parse (parser);
0a3b29ad 6687 }
6688
6689 /* If we get here, then it's not a cast, so it must be a
6690 unary-expression. */
98b326fd 6691 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
0a3b29ad 6692}
6693
0a88af73 6694/* Parse a binary expression of the general form:
0a3b29ad 6695
6696 pm-expression:
6697 cast-expression
6698 pm-expression .* cast-expression
6699 pm-expression ->* cast-expression
6700
e24657db 6701 multiplicative-expression:
0a3b29ad 6702 pm-expression
6703 multiplicative-expression * pm-expression
6704 multiplicative-expression / pm-expression
6705 multiplicative-expression % pm-expression
6706
0a3b29ad 6707 additive-expression:
6708 multiplicative-expression
6709 additive-expression + multiplicative-expression
6710 additive-expression - multiplicative-expression
6711
0a3b29ad 6712 shift-expression:
6713 additive-expression
6714 shift-expression << additive-expression
6715 shift-expression >> additive-expression
6716
0a3b29ad 6717 relational-expression:
6718 shift-expression
6719 relational-expression < shift-expression
6720 relational-expression > shift-expression
6721 relational-expression <= shift-expression
6722 relational-expression >= shift-expression
6723
0a88af73 6724 GNU Extension:
9031d10b 6725
0a3b29ad 6726 relational-expression:
6727 relational-expression <? shift-expression
6728 relational-expression >? shift-expression
6729
0a3b29ad 6730 equality-expression:
6731 relational-expression
6732 equality-expression == relational-expression
6733 equality-expression != relational-expression
6734
0a3b29ad 6735 and-expression:
6736 equality-expression
6737 and-expression & equality-expression
6738
0a3b29ad 6739 exclusive-or-expression:
6740 and-expression
6741 exclusive-or-expression ^ and-expression
6742
0a88af73 6743 inclusive-or-expression:
6744 exclusive-or-expression
6745 inclusive-or-expression | exclusive-or-expression
0a3b29ad 6746
0a88af73 6747 logical-and-expression:
6748 inclusive-or-expression
6749 logical-and-expression && inclusive-or-expression
0a3b29ad 6750
0a88af73 6751 logical-or-expression:
6752 logical-and-expression
6753 logical-or-expression || logical-and-expression
0a3b29ad 6754
0a88af73 6755 All these are implemented with a single function like:
0a3b29ad 6756
0a88af73 6757 binary-expression:
6758 simple-cast-expression
6759 binary-expression <token> binary-expression
0a3b29ad 6760
640aa28c 6761 CAST_P is true if this expression is the target of a cast.
6762
0a88af73 6763 The binops_by_token map is used to get the tree codes for each <token> type.
6764 binary-expressions are associated according to a precedence table. */
0a3b29ad 6765
6dcdb5de 6766#define TOKEN_PRECEDENCE(token) \
6767(((token->type == CPP_GREATER \
6768 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6769 && !parser->greater_than_is_operator_p) \
6770 ? PREC_NOT_OPERATOR \
56471494 6771 : binops_by_token[token->type].prec)
0a3b29ad 6772
6773static tree
fd6481cf 6774cp_parser_binary_expression (cp_parser* parser, bool cast_p,
4390875c 6775 bool no_toplevel_fold_p,
98b326fd 6776 enum cp_parser_prec prec,
6777 cp_id_kind * pidk)
0a3b29ad 6778{
0a88af73 6779 cp_parser_expression_stack stack;
6780 cp_parser_expression_stack_entry *sp = &stack[0];
6781 tree lhs, rhs;
6782 cp_token *token;
e534436e 6783 enum tree_code tree_type, lhs_type, rhs_type;
fd6481cf 6784 enum cp_parser_prec new_prec, lookahead_prec;
0a88af73 6785 bool overloaded_p;
0a3b29ad 6786
0a88af73 6787 /* Parse the first expression. */
98b326fd 6788 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
e534436e 6789 lhs_type = ERROR_MARK;
0a3b29ad 6790
0a88af73 6791 for (;;)
6792 {
6793 /* Get an operator token. */
6794 token = cp_lexer_peek_token (parser->lexer);
d50879bc 6795
56471494 6796 if (warn_cxx0x_compat
6797 && token->type == CPP_RSHIFT
6798 && !parser->greater_than_is_operator_p)
6799 {
ccb59bb6 6800 if (warning_at (token->location, OPT_Wc__0x_compat,
6801 "%<>>%> operator will be treated as"
6802 " two right angle brackets in C++0x"))
6803 inform (token->location,
6804 "suggest parentheses around %<>>%> expression");
56471494 6805 }
6806
0a88af73 6807 new_prec = TOKEN_PRECEDENCE (token);
6808
6809 /* Popping an entry off the stack means we completed a subexpression:
653e5405 6810 - either we found a token which is not an operator (`>' where it is not
6811 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6812 will happen repeatedly;
6813 - or, we found an operator which has lower priority. This is the case
6814 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6815 parsing `3 * 4'. */
0a88af73 6816 if (new_prec <= prec)
653e5405 6817 {
6818 if (sp == stack)
0a88af73 6819 break;
653e5405 6820 else
0a88af73 6821 goto pop;
653e5405 6822 }
0a3b29ad 6823
0a88af73 6824 get_rhs:
6825 tree_type = binops_by_token[token->type].tree_type;
0a3b29ad 6826
93523877 6827 /* We used the operator token. */
0a88af73 6828 cp_lexer_consume_token (parser->lexer);
0a3b29ad 6829
6dd536b9 6830 /* For "false && x" or "true || x", x will never be executed;
6831 disable warnings while evaluating it. */
6832 if (tree_type == TRUTH_ANDIF_EXPR)
6833 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6834 else if (tree_type == TRUTH_ORIF_EXPR)
6835 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6836
0a88af73 6837 /* Extract another operand. It may be the RHS of this expression
653e5405 6838 or the LHS of a new, higher priority expression. */
0a88af73 6839 rhs = cp_parser_simple_cast_expression (parser);
e534436e 6840 rhs_type = ERROR_MARK;
0a3b29ad 6841
0a88af73 6842 /* Get another operator token. Look up its precedence to avoid
653e5405 6843 building a useless (immediately popped) stack entry for common
6844 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
0a88af73 6845 token = cp_lexer_peek_token (parser->lexer);
6846 lookahead_prec = TOKEN_PRECEDENCE (token);
6847 if (lookahead_prec > new_prec)
653e5405 6848 {
6849 /* ... and prepare to parse the RHS of the new, higher priority
6850 expression. Since precedence levels on the stack are
9802eea0 6851 monotonically increasing, we do not have to care about
6852 stack overflows. */
653e5405 6853 sp->prec = prec;
6854 sp->tree_type = tree_type;
6855 sp->lhs = lhs;
e534436e 6856 sp->lhs_type = lhs_type;
653e5405 6857 sp++;
6858 lhs = rhs;
e534436e 6859 lhs_type = rhs_type;
653e5405 6860 prec = new_prec;
6861 new_prec = lookahead_prec;
6862 goto get_rhs;
6863
6864 pop:
4390875c 6865 lookahead_prec = new_prec;
653e5405 6866 /* If the stack is not empty, we have parsed into LHS the right side
0a88af73 6867 (`4' in the example above) of an expression we had suspended.
9031d10b 6868 We can use the information on the stack to recover the LHS (`3')
0a88af73 6869 from the stack together with the tree code (`MULT_EXPR'), and
6870 the precedence of the higher level subexpression
6871 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6872 which will be used to actually build the additive expression. */
653e5405 6873 --sp;
0a88af73 6874 prec = sp->prec;
653e5405 6875 tree_type = sp->tree_type;
6876 rhs = lhs;
e534436e 6877 rhs_type = lhs_type;
653e5405 6878 lhs = sp->lhs;
e534436e 6879 lhs_type = sp->lhs_type;
653e5405 6880 }
0a3b29ad 6881
6dd536b9 6882 /* Undo the disabling of warnings done above. */
6883 if (tree_type == TRUTH_ANDIF_EXPR)
6884 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6885 else if (tree_type == TRUTH_ORIF_EXPR)
6886 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6887
0a88af73 6888 overloaded_p = false;
82012ffe 6889 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6890 ERROR_MARK for everything that is not a binary expression.
6891 This makes warn_about_parentheses miss some warnings that
6892 involve unary operators. For unary expressions we should
6893 pass the correct tree_code unless the unary expression was
6894 surrounded by parentheses.
6895 */
4390875c 6896 if (no_toplevel_fold_p
6897 && lookahead_prec <= prec
6898 && sp == stack
6899 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6900 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6901 else
6902 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6903 &overloaded_p, tf_warning_or_error);
e534436e 6904 lhs_type = tree_type;
0a3b29ad 6905
0a88af73 6906 /* If the binary operator required the use of an overloaded operator,
653e5405 6907 then this expression cannot be an integral constant-expression.
6908 An overloaded operator can be used even if both operands are
6909 otherwise permissible in an integral constant-expression if at
6910 least one of the operands is of enumeration type. */
0a3b29ad 6911
0a88af73 6912 if (overloaded_p
c61e1212 6913 && cp_parser_non_integral_constant_expression (parser,
6914 NIC_OVERLOADED))
653e5405 6915 return error_mark_node;
0a88af73 6916 }
0a3b29ad 6917
0a88af73 6918 return lhs;
0a3b29ad 6919}
6920
0a88af73 6921
0a3b29ad 6922/* Parse the `? expression : assignment-expression' part of a
6923 conditional-expression. The LOGICAL_OR_EXPR is the
6924 logical-or-expression that started the conditional-expression.
6925 Returns a representation of the entire conditional-expression.
6926
878870b4 6927 This routine is used by cp_parser_assignment_expression.
0a3b29ad 6928
6929 ? expression : assignment-expression
ccb84981 6930
0a3b29ad 6931 GNU Extensions:
ccb84981 6932
0a3b29ad 6933 ? : assignment-expression */
6934
6935static tree
45baea8b 6936cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
0a3b29ad 6937{
6938 tree expr;
6939 tree assignment_expr;
b9bdfa0b 6940 struct cp_token *token;
0a3b29ad 6941
6942 /* Consume the `?' token. */
6943 cp_lexer_consume_token (parser->lexer);
b9bdfa0b 6944 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 6945 if (cp_parser_allow_gnu_extensions_p (parser)
b9bdfa0b 6946 && token->type == CPP_COLON)
48d94ede 6947 {
b9bdfa0b 6948 pedwarn (token->location, OPT_pedantic,
6949 "ISO C++ does not allow ?: with omitted middle operand");
48d94ede 6950 /* Implicit true clause. */
6951 expr = NULL_TREE;
6952 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
b9bdfa0b 6953 warn_for_omitted_condop (token->location, logical_or_expr);
48d94ede 6954 }
0a3b29ad 6955 else
48d94ede 6956 {
6957 /* Parse the expression. */
6958 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6959 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6960 c_inhibit_evaluation_warnings +=
6961 ((logical_or_expr == truthvalue_true_node)
6962 - (logical_or_expr == truthvalue_false_node));
6963 }
ccb84981 6964
0a3b29ad 6965 /* The next token should be a `:'. */
c247dce0 6966 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 6967 /* Parse the assignment-expression. */
98b326fd 6968 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
48d94ede 6969 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
0a3b29ad 6970
6971 /* Build the conditional-expression. */
6972 return build_x_conditional_expr (logical_or_expr,
6973 expr,
ebd21de4 6974 assignment_expr,
6975 tf_warning_or_error);
0a3b29ad 6976}
6977
6978/* Parse an assignment-expression.
6979
6980 assignment-expression:
6981 conditional-expression
6982 logical-or-expression assignment-operator assignment_expression
6983 throw-expression
6984
640aa28c 6985 CAST_P is true if this expression is the target of a cast.
6986
0a3b29ad 6987 Returns a representation for the expression. */
6988
6989static tree
98b326fd 6990cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6991 cp_id_kind * pidk)
0a3b29ad 6992{
6993 tree expr;
6994
6995 /* If the next token is the `throw' keyword, then we're looking at
6996 a throw-expression. */
6997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6998 expr = cp_parser_throw_expression (parser);
6999 /* Otherwise, it must be that we are looking at a
7000 logical-or-expression. */
7001 else
7002 {
0a88af73 7003 /* Parse the binary expressions (logical-or-expression). */
4390875c 7004 expr = cp_parser_binary_expression (parser, cast_p, false,
7005 PREC_NOT_OPERATOR, pidk);
0a3b29ad 7006 /* If the next token is a `?' then we're actually looking at a
7007 conditional-expression. */
7008 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7009 return cp_parser_question_colon_clause (parser, expr);
ccb84981 7010 else
0a3b29ad 7011 {
7012 enum tree_code assignment_operator;
7013
7014 /* If it's an assignment-operator, we're using the second
7015 production. */
ccb84981 7016 assignment_operator
0a3b29ad 7017 = cp_parser_assignment_operator_opt (parser);
7018 if (assignment_operator != ERROR_MARK)
7019 {
f82f1250 7020 bool non_constant_p;
0a3b29ad 7021
7022 /* Parse the right-hand side of the assignment. */
f82f1250 7023 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7024
7025 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
bf8d19fe 7026 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 7027
5f6526e1 7028 /* An assignment may not appear in a
7029 constant-expression. */
3938e0c2 7030 if (cp_parser_non_integral_constant_expression (parser,
c247dce0 7031 NIC_ASSIGNMENT))
3938e0c2 7032 return error_mark_node;
755edffd 7033 /* Build the assignment expression. */
ccb84981 7034 expr = build_x_modify_expr (expr,
7035 assignment_operator,
ebd21de4 7036 rhs,
7037 tf_warning_or_error);
0a3b29ad 7038 }
7039 }
7040 }
7041
7042 return expr;
7043}
7044
7045/* Parse an (optional) assignment-operator.
7046
ccb84981 7047 assignment-operator: one of
7048 = *= /= %= += -= >>= <<= &= ^= |=
0a3b29ad 7049
7050 GNU Extension:
ccb84981 7051
0a3b29ad 7052 assignment-operator: one of
7053 <?= >?=
7054
7055 If the next token is an assignment operator, the corresponding tree
7056 code is returned, and the token is consumed. For example, for
7057 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7058 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7059 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7060 operator, ERROR_MARK is returned. */
7061
7062static enum tree_code
45baea8b 7063cp_parser_assignment_operator_opt (cp_parser* parser)
0a3b29ad 7064{
7065 enum tree_code op;
7066 cp_token *token;
7067
08cc44e7 7068 /* Peek at the next token. */
0a3b29ad 7069 token = cp_lexer_peek_token (parser->lexer);
7070
7071 switch (token->type)
7072 {
7073 case CPP_EQ:
7074 op = NOP_EXPR;
7075 break;
7076
7077 case CPP_MULT_EQ:
7078 op = MULT_EXPR;
7079 break;
7080
7081 case CPP_DIV_EQ:
7082 op = TRUNC_DIV_EXPR;
7083 break;
7084
7085 case CPP_MOD_EQ:
7086 op = TRUNC_MOD_EXPR;
7087 break;
7088
7089 case CPP_PLUS_EQ:
7090 op = PLUS_EXPR;
7091 break;
7092
7093 case CPP_MINUS_EQ:
7094 op = MINUS_EXPR;
7095 break;
7096
7097 case CPP_RSHIFT_EQ:
7098 op = RSHIFT_EXPR;
7099 break;
7100
7101 case CPP_LSHIFT_EQ:
7102 op = LSHIFT_EXPR;
7103 break;
7104
7105 case CPP_AND_EQ:
7106 op = BIT_AND_EXPR;
7107 break;
7108
7109 case CPP_XOR_EQ:
7110 op = BIT_XOR_EXPR;
7111 break;
7112
7113 case CPP_OR_EQ:
7114 op = BIT_IOR_EXPR;
7115 break;
7116
ccb84981 7117 default:
0a3b29ad 7118 /* Nothing else is an assignment operator. */
7119 op = ERROR_MARK;
7120 }
7121
7122 /* If it was an assignment operator, consume it. */
7123 if (op != ERROR_MARK)
7124 cp_lexer_consume_token (parser->lexer);
7125
7126 return op;
7127}
7128
7129/* Parse an expression.
7130
7131 expression:
7132 assignment-expression
7133 expression , assignment-expression
7134
640aa28c 7135 CAST_P is true if this expression is the target of a cast.
7136
0a3b29ad 7137 Returns a representation of the expression. */
7138
7139static tree
98b326fd 7140cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
0a3b29ad 7141{
7142 tree expression = NULL_TREE;
0a3b29ad 7143
7144 while (true)
7145 {
7146 tree assignment_expression;
7147
7148 /* Parse the next assignment-expression. */
ccb84981 7149 assignment_expression
98b326fd 7150 = cp_parser_assignment_expression (parser, cast_p, pidk);
0a3b29ad 7151 /* If this is the first assignment-expression, we can just
7152 save it away. */
7153 if (!expression)
7154 expression = assignment_expression;
0a3b29ad 7155 else
13795292 7156 expression = build_x_compound_expr (expression,
ebd21de4 7157 assignment_expression,
7158 tf_warning_or_error);
0a3b29ad 7159 /* If the next token is not a comma, then we are done with the
7160 expression. */
7161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7162 break;
7163 /* Consume the `,'. */
7164 cp_lexer_consume_token (parser->lexer);
5f6526e1 7165 /* A comma operator cannot appear in a constant-expression. */
c247dce0 7166 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
3938e0c2 7167 expression = error_mark_node;
5f6526e1 7168 }
0a3b29ad 7169
7170 return expression;
7171}
7172
ccb84981 7173/* Parse a constant-expression.
0a3b29ad 7174
7175 constant-expression:
ccb84981 7176 conditional-expression
5f6526e1 7177
7178 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
13795292 7179 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7180 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7181 is false, NON_CONSTANT_P should be NULL. */
0a3b29ad 7182
7183static tree
ccb84981 7184cp_parser_constant_expression (cp_parser* parser,
5f6526e1 7185 bool allow_non_constant_p,
7186 bool *non_constant_p)
0a3b29ad 7187{
f47c1747 7188 bool saved_integral_constant_expression_p;
7189 bool saved_allow_non_integral_constant_expression_p;
7190 bool saved_non_integral_constant_expression_p;
0a3b29ad 7191 tree expression;
7192
7193 /* It might seem that we could simply parse the
7194 conditional-expression, and then check to see if it were
7195 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7196 one that the compiler can figure out is constant, possibly after
7197 doing some simplifications or optimizations. The standard has a
7198 precise definition of constant-expression, and we must honor
7199 that, even though it is somewhat more restrictive.
7200
7201 For example:
7202
7203 int i[(2, 3)];
7204
7205 is not a legal declaration, because `(2, 3)' is not a
7206 constant-expression. The `,' operator is forbidden in a
7207 constant-expression. However, GCC's constant-folding machinery
7208 will fold this operation to an INTEGER_CST for `3'. */
7209
5f6526e1 7210 /* Save the old settings. */
f47c1747 7211 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
ccb84981 7212 saved_allow_non_integral_constant_expression_p
f47c1747 7213 = parser->allow_non_integral_constant_expression_p;
7214 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
0a3b29ad 7215 /* We are now parsing a constant-expression. */
f47c1747 7216 parser->integral_constant_expression_p = true;
ce984e5e 7217 parser->allow_non_integral_constant_expression_p
7218 = (allow_non_constant_p || cxx_dialect >= cxx0x);
f47c1747 7219 parser->non_integral_constant_expression_p = false;
878870b4 7220 /* Although the grammar says "conditional-expression", we parse an
7221 "assignment-expression", which also permits "throw-expression"
7222 and the use of assignment operators. In the case that
7223 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7224 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7225 actually essential that we look for an assignment-expression.
7226 For example, cp_parser_initializer_clauses uses this function to
7227 determine whether a particular assignment-expression is in fact
7228 constant. */
98b326fd 7229 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
5f6526e1 7230 /* Restore the old settings. */
9031d10b 7231 parser->integral_constant_expression_p
640aa28c 7232 = saved_integral_constant_expression_p;
ccb84981 7233 parser->allow_non_integral_constant_expression_p
f47c1747 7234 = saved_allow_non_integral_constant_expression_p;
5f6526e1 7235 if (allow_non_constant_p)
f47c1747 7236 *non_constant_p = parser->non_integral_constant_expression_p;
ce984e5e 7237 else if (parser->non_integral_constant_expression_p
7238 && cxx_dialect < cxx0x)
640aa28c 7239 expression = error_mark_node;
9031d10b 7240 parser->non_integral_constant_expression_p
640aa28c 7241 = saved_non_integral_constant_expression_p;
0a3b29ad 7242
7243 return expression;
7244}
7245
43bf5d72 7246/* Parse __builtin_offsetof.
7247
7248 offsetof-expression:
7249 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7250
7251 offsetof-member-designator:
7252 id-expression
7253 | offsetof-member-designator "." id-expression
f0d0d842 7254 | offsetof-member-designator "[" expression "]"
7255 | offsetof-member-designator "->" id-expression */
43bf5d72 7256
7257static tree
7258cp_parser_builtin_offsetof (cp_parser *parser)
7259{
7260 int save_ice_p, save_non_ice_p;
7261 tree type, expr;
7262 cp_id_kind dummy;
ad9ae192 7263 cp_token *token;
43bf5d72 7264
7265 /* We're about to accept non-integral-constant things, but will
7266 definitely yield an integral constant expression. Save and
7267 restore these values around our local parsing. */
7268 save_ice_p = parser->integral_constant_expression_p;
7269 save_non_ice_p = parser->non_integral_constant_expression_p;
7270
7271 /* Consume the "__builtin_offsetof" token. */
7272 cp_lexer_consume_token (parser->lexer);
7273 /* Consume the opening `('. */
c247dce0 7274 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
43bf5d72 7275 /* Parse the type-id. */
7276 type = cp_parser_type_id (parser);
7277 /* Look for the `,'. */
c247dce0 7278 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
ad9ae192 7279 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 7280
7281 /* Build the (type *)null that begins the traditional offsetof macro. */
ebd21de4 7282 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7283 tf_warning_or_error);
43bf5d72 7284
7285 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7286 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
ad9ae192 7287 true, &dummy, token->location);
43bf5d72 7288 while (true)
7289 {
ad9ae192 7290 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 7291 switch (token->type)
7292 {
7293 case CPP_OPEN_SQUARE:
7294 /* offsetof-member-designator "[" expression "]" */
7295 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7296 break;
7297
f0d0d842 7298 case CPP_DEREF:
7299 /* offsetof-member-designator "->" identifier */
7300 expr = grok_array_decl (expr, integer_zero_node);
7301 /* FALLTHRU */
7302
43bf5d72 7303 case CPP_DOT:
7304 /* offsetof-member-designator "." identifier */
7305 cp_lexer_consume_token (parser->lexer);
f0d0d842 7306 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7307 expr, true, &dummy,
ad9ae192 7308 token->location);
43bf5d72 7309 break;
7310
7311 case CPP_CLOSE_PAREN:
7312 /* Consume the ")" token. */
7313 cp_lexer_consume_token (parser->lexer);
7314 goto success;
7315
7316 default:
7317 /* Error. We know the following require will fail, but
7318 that gives the proper error message. */
c247dce0 7319 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
43bf5d72 7320 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7321 expr = error_mark_node;
7322 goto failure;
7323 }
7324 }
7325
7326 success:
11106b1c 7327 /* If we're processing a template, we can't finish the semantics yet.
7328 Otherwise we can fold the entire expression now. */
7329 if (processing_template_decl)
7330 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7331 else
bf75f33a 7332 expr = finish_offsetof (expr);
43bf5d72 7333
7334 failure:
7335 parser->integral_constant_expression_p = save_ice_p;
7336 parser->non_integral_constant_expression_p = save_non_ice_p;
7337
7338 return expr;
7339}
7340
481451eb 7341/* Parse a trait expression. */
7342
7343static tree
7344cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7345{
7346 cp_trait_kind kind;
7347 tree type1, type2 = NULL_TREE;
7348 bool binary = false;
7349 cp_decl_specifier_seq decl_specs;
7350
7351 switch (keyword)
7352 {
7353 case RID_HAS_NOTHROW_ASSIGN:
7354 kind = CPTK_HAS_NOTHROW_ASSIGN;
7355 break;
7356 case RID_HAS_NOTHROW_CONSTRUCTOR:
7357 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7358 break;
7359 case RID_HAS_NOTHROW_COPY:
7360 kind = CPTK_HAS_NOTHROW_COPY;
7361 break;
7362 case RID_HAS_TRIVIAL_ASSIGN:
7363 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7364 break;
7365 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7366 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7367 break;
7368 case RID_HAS_TRIVIAL_COPY:
7369 kind = CPTK_HAS_TRIVIAL_COPY;
7370 break;
7371 case RID_HAS_TRIVIAL_DESTRUCTOR:
7372 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7373 break;
7374 case RID_HAS_VIRTUAL_DESTRUCTOR:
7375 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7376 break;
7377 case RID_IS_ABSTRACT:
7378 kind = CPTK_IS_ABSTRACT;
7379 break;
7380 case RID_IS_BASE_OF:
7381 kind = CPTK_IS_BASE_OF;
7382 binary = true;
7383 break;
7384 case RID_IS_CLASS:
7385 kind = CPTK_IS_CLASS;
7386 break;
7387 case RID_IS_CONVERTIBLE_TO:
7388 kind = CPTK_IS_CONVERTIBLE_TO;
7389 binary = true;
7390 break;
7391 case RID_IS_EMPTY:
7392 kind = CPTK_IS_EMPTY;
7393 break;
7394 case RID_IS_ENUM:
7395 kind = CPTK_IS_ENUM;
7396 break;
7397 case RID_IS_POD:
7398 kind = CPTK_IS_POD;
7399 break;
7400 case RID_IS_POLYMORPHIC:
7401 kind = CPTK_IS_POLYMORPHIC;
7402 break;
c1c67b4f 7403 case RID_IS_STD_LAYOUT:
7404 kind = CPTK_IS_STD_LAYOUT;
7405 break;
7406 case RID_IS_TRIVIAL:
7407 kind = CPTK_IS_TRIVIAL;
7408 break;
481451eb 7409 case RID_IS_UNION:
7410 kind = CPTK_IS_UNION;
7411 break;
5290e253 7412 case RID_IS_LITERAL_TYPE:
7413 kind = CPTK_IS_LITERAL_TYPE;
7414 break;
481451eb 7415 default:
7416 gcc_unreachable ();
7417 }
7418
7419 /* Consume the token. */
7420 cp_lexer_consume_token (parser->lexer);
7421
c247dce0 7422 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
481451eb 7423
7424 type1 = cp_parser_type_id (parser);
7425
bcef2b12 7426 if (type1 == error_mark_node)
7427 return error_mark_node;
7428
481451eb 7429 /* Build a trivial decl-specifier-seq. */
7430 clear_decl_specs (&decl_specs);
7431 decl_specs.type = type1;
7432
7433 /* Call grokdeclarator to figure out what type this is. */
7434 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7435 /*initialized=*/0, /*attrlist=*/NULL);
7436
7437 if (binary)
7438 {
c247dce0 7439 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
481451eb 7440
7441 type2 = cp_parser_type_id (parser);
7442
bcef2b12 7443 if (type2 == error_mark_node)
7444 return error_mark_node;
7445
481451eb 7446 /* Build a trivial decl-specifier-seq. */
7447 clear_decl_specs (&decl_specs);
7448 decl_specs.type = type2;
7449
7450 /* Call grokdeclarator to figure out what type this is. */
7451 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7452 /*initialized=*/0, /*attrlist=*/NULL);
7453 }
7454
c247dce0 7455 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
481451eb 7456
bcef2b12 7457 /* Complete the trait expression, which may mean either processing
7458 the trait expr now or saving it for template instantiation. */
481451eb 7459 return finish_trait_expr (kind, type1, type2);
7460}
7461
a8b75081 7462/* Lambdas that appear in variable initializer or default argument scope
7463 get that in their mangling, so we need to record it. We might as well
7464 use the count for function and namespace scopes as well. */
1cf796e9 7465static GTY(()) tree lambda_scope;
7466static GTY(()) int lambda_count;
a8b75081 7467typedef struct GTY(()) tree_int
7468{
7469 tree t;
7470 int i;
7471} tree_int;
7472DEF_VEC_O(tree_int);
7473DEF_VEC_ALLOC_O(tree_int,gc);
7474static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7475
7476static void
7477start_lambda_scope (tree decl)
7478{
7479 tree_int ti;
7480 gcc_assert (decl);
7481 /* Once we're inside a function, we ignore other scopes and just push
7482 the function again so that popping works properly. */
7483 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7484 decl = current_function_decl;
7485 ti.t = lambda_scope;
7486 ti.i = lambda_count;
7487 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7488 if (lambda_scope != decl)
7489 {
7490 /* Don't reset the count if we're still in the same function. */
7491 lambda_scope = decl;
7492 lambda_count = 0;
7493 }
7494}
7495
7496static void
7497record_lambda_scope (tree lambda)
7498{
7499 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7500 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7501}
7502
7503static void
7504finish_lambda_scope (void)
7505{
7506 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7507 if (lambda_scope != p->t)
7508 {
7509 lambda_scope = p->t;
7510 lambda_count = p->i;
7511 }
7512 VEC_pop (tree_int, lambda_scope_stack);
7513}
7514
a8b75081 7515/* Parse a lambda expression.
7516
7517 lambda-expression:
7518 lambda-introducer lambda-declarator [opt] compound-statement
7519
7520 Returns a representation of the expression. */
7521
7522static tree
7523cp_parser_lambda_expression (cp_parser* parser)
7524{
7525 tree lambda_expr = build_lambda_expr ();
7526 tree type;
7527
7528 LAMBDA_EXPR_LOCATION (lambda_expr)
7529 = cp_lexer_peek_token (parser->lexer)->location;
7530
e8a2efc1 7531 if (cp_unevaluated_operand)
7532 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7533 "lambda-expression in unevaluated context");
7534
a8b75081 7535 /* We may be in the middle of deferred access check. Disable
7536 it now. */
7537 push_deferring_access_checks (dk_no_deferred);
7538
86b604cf 7539 cp_parser_lambda_introducer (parser, lambda_expr);
7540
a8b75081 7541 type = begin_lambda_type (lambda_expr);
7542
7543 record_lambda_scope (lambda_expr);
7544
18515bc1 7545 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7546 determine_visibility (TYPE_NAME (type));
7547
86b604cf 7548 /* Now that we've started the type, add the capture fields for any
7549 explicit captures. */
7550 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7551
a8b75081 7552 {
7553 /* Inside the class, surrounding template-parameter-lists do not apply. */
7554 unsigned int saved_num_template_parameter_lists
7555 = parser->num_template_parameter_lists;
7556
7557 parser->num_template_parameter_lists = 0;
7558
a8b75081 7559 /* By virtue of defining a local class, a lambda expression has access to
7560 the private variables of enclosing classes. */
7561
7562 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7563
7564 cp_parser_lambda_body (parser, lambda_expr);
7565
7566 /* The capture list was built up in reverse order; fix that now. */
7567 {
7568 tree newlist = NULL_TREE;
7569 tree elt, next;
7570
7571 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7572 elt; elt = next)
7573 {
9e1aa1c9 7574 tree field = TREE_PURPOSE (elt);
7575 char *buf;
7576
7577 next = TREE_CHAIN (elt);
7578 TREE_CHAIN (elt) = newlist;
7579 newlist = elt;
7580
a8b75081 7581 /* Also add __ to the beginning of the field name so that code
7582 outside the lambda body can't see the captured name. We could
7583 just remove the name entirely, but this is more useful for
7584 debugging. */
9e1aa1c9 7585 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7586 /* The 'this' capture already starts with __. */
7587 continue;
7588
7589 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
a8b75081 7590 buf[1] = buf[0] = '_';
7591 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7592 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7593 DECL_NAME (field) = get_identifier (buf);
a8b75081 7594 }
7595 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7596 }
7597
91a733f9 7598 maybe_add_lambda_conv_op (type);
7599
a8b75081 7600 type = finish_struct (type, /*attributes=*/NULL_TREE);
7601
7602 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7603 }
7604
7605 pop_deferring_access_checks ();
7606
7607 return build_lambda_object (lambda_expr);
7608}
7609
7610/* Parse the beginning of a lambda expression.
7611
7612 lambda-introducer:
7613 [ lambda-capture [opt] ]
7614
7615 LAMBDA_EXPR is the current representation of the lambda expression. */
7616
7617static void
7618cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7619{
7620 /* Need commas after the first capture. */
7621 bool first = true;
7622
7623 /* Eat the leading `['. */
c247dce0 7624 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
a8b75081 7625
7626 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7627 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7628 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7629 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7630 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7631 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7632
7633 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7634 {
7635 cp_lexer_consume_token (parser->lexer);
7636 first = false;
7637 }
7638
7639 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7640 {
7641 cp_token* capture_token;
7642 tree capture_id;
7643 tree capture_init_expr;
7644 cp_id_kind idk = CP_ID_KIND_NONE;
7da3c25a 7645 bool explicit_init_p = false;
a8b75081 7646
7647 enum capture_kind_type
7648 {
7649 BY_COPY,
7650 BY_REFERENCE
7651 };
7652 enum capture_kind_type capture_kind = BY_COPY;
7653
7654 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7655 {
7656 error ("expected end of capture-list");
7657 return;
7658 }
7659
7660 if (first)
7661 first = false;
7662 else
c247dce0 7663 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
a8b75081 7664
7665 /* Possibly capture `this'. */
7666 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7667 {
7668 cp_lexer_consume_token (parser->lexer);
7669 add_capture (lambda_expr,
7670 /*id=*/get_identifier ("__this"),
7671 /*initializer=*/finish_this_expr(),
7da3c25a 7672 /*by_reference_p=*/false,
7673 explicit_init_p);
a8b75081 7674 continue;
7675 }
7676
7677 /* Remember whether we want to capture as a reference or not. */
7678 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7679 {
7680 capture_kind = BY_REFERENCE;
7681 cp_lexer_consume_token (parser->lexer);
7682 }
7683
7684 /* Get the identifier. */
7685 capture_token = cp_lexer_peek_token (parser->lexer);
7686 capture_id = cp_parser_identifier (parser);
7687
7688 if (capture_id == error_mark_node)
7689 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7690 delimiters, but I modified this to stop on unnested ']' as well. It
7691 was already changed to stop on unnested '}', so the
7692 "closing_parenthesis" name is no more misleading with my change. */
7693 {
7694 cp_parser_skip_to_closing_parenthesis (parser,
7695 /*recovering=*/true,
7696 /*or_comma=*/true,
7697 /*consume_paren=*/true);
7271d48e 7698 break;
a8b75081 7699 }
7700
7701 /* Find the initializer for this capture. */
7702 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7703 {
7704 /* An explicit expression exists. */
7705 cp_lexer_consume_token (parser->lexer);
7706 pedwarn (input_location, OPT_pedantic,
7707 "ISO C++ does not allow initializers "
7708 "in lambda expression capture lists");
7709 capture_init_expr = cp_parser_assignment_expression (parser,
7710 /*cast_p=*/true,
7711 &idk);
7da3c25a 7712 explicit_init_p = true;
a8b75081 7713 }
7714 else
7715 {
7716 const char* error_msg;
7717
7718 /* Turn the identifier into an id-expression. */
7719 capture_init_expr
7720 = cp_parser_lookup_name
7721 (parser,
7722 capture_id,
7723 none_type,
7724 /*is_template=*/false,
7725 /*is_namespace=*/false,
7726 /*check_dependency=*/true,
7727 /*ambiguous_decls=*/NULL,
7728 capture_token->location);
7729
7730 capture_init_expr
7731 = finish_id_expression
7732 (capture_id,
7733 capture_init_expr,
7734 parser->scope,
7735 &idk,
7736 /*integral_constant_expression_p=*/false,
7737 /*allow_non_integral_constant_expression_p=*/false,
7738 /*non_integral_constant_expression_p=*/NULL,
7739 /*template_p=*/false,
7740 /*done=*/true,
7741 /*address_p=*/false,
7742 /*template_arg_p=*/false,
7743 &error_msg,
7744 capture_token->location);
7745 }
7746
7747 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7748 capture_init_expr
7749 = unqualified_name_lookup_error (capture_init_expr);
7750
7751 add_capture (lambda_expr,
7752 capture_id,
7753 capture_init_expr,
7da3c25a 7754 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7755 explicit_init_p);
a8b75081 7756 }
7757
c247dce0 7758 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
a8b75081 7759}
7760
7761/* Parse the (optional) middle of a lambda expression.
7762
7763 lambda-declarator:
7764 ( parameter-declaration-clause [opt] )
7765 attribute-specifier [opt]
7766 mutable [opt]
7767 exception-specification [opt]
7768 lambda-return-type-clause [opt]
7769
7770 LAMBDA_EXPR is the current representation of the lambda expression. */
7771
7772static void
7773cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7774{
7775 /* 5.1.1.4 of the standard says:
7776 If a lambda-expression does not include a lambda-declarator, it is as if
7777 the lambda-declarator were ().
7778 This means an empty parameter list, no attributes, and no exception
7779 specification. */
7780 tree param_list = void_list_node;
7781 tree attributes = NULL_TREE;
7782 tree exception_spec = NULL_TREE;
7783 tree t;
7784
7785 /* The lambda-declarator is optional, but must begin with an opening
7786 parenthesis if present. */
7787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7788 {
7789 cp_lexer_consume_token (parser->lexer);
7790
7791 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7792
7793 /* Parse parameters. */
7794 param_list = cp_parser_parameter_declaration_clause (parser);
7795
7796 /* Default arguments shall not be specified in the
7797 parameter-declaration-clause of a lambda-declarator. */
7798 for (t = param_list; t; t = TREE_CHAIN (t))
7799 if (TREE_PURPOSE (t))
7800 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7801 "default argument specified for lambda parameter");
7802
c247dce0 7803 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
a8b75081 7804
7805 attributes = cp_parser_attributes_opt (parser);
7806
7807 /* Parse optional `mutable' keyword. */
7808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7809 {
7810 cp_lexer_consume_token (parser->lexer);
7811 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7812 }
7813
7814 /* Parse optional exception specification. */
7815 exception_spec = cp_parser_exception_specification_opt (parser);
7816
7817 /* Parse optional trailing return type. */
7818 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7819 {
7820 cp_lexer_consume_token (parser->lexer);
7821 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7822 }
7823
7824 /* The function parameters must be in scope all the way until after the
7825 trailing-return-type in case of decltype. */
1767a056 7826 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
a8b75081 7827 pop_binding (DECL_NAME (t), t);
7828
7829 leave_scope ();
7830 }
7831
7832 /* Create the function call operator.
7833
7834 Messing with declarators like this is no uglier than building up the
7835 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7836 other code. */
7837 {
7838 cp_decl_specifier_seq return_type_specs;
7839 cp_declarator* declarator;
7840 tree fco;
7841 int quals;
7842 void *p;
7843
7844 clear_decl_specs (&return_type_specs);
7845 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7846 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7847 else
7848 /* Maybe we will deduce the return type later, but we can use void
7849 as a placeholder return type anyways. */
7850 return_type_specs.type = void_type_node;
7851
7852 p = obstack_alloc (&declarator_obstack, 0);
7853
7854 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7855 sfk_none);
7856
43936711 7857 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7858 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
a8b75081 7859 declarator = make_call_declarator (declarator, param_list, quals,
7860 exception_spec,
7861 /*late_return_type=*/NULL_TREE);
3c5a5d99 7862 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
a8b75081 7863
7864 fco = grokmethod (&return_type_specs,
91a733f9 7865 declarator,
7866 attributes);
a8b75081 7867 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7868 DECL_ARTIFICIAL (fco) = 1;
7869
7870 finish_member_declaration (fco);
7871
7872 obstack_free (&declarator_obstack, p);
7873 }
7874}
7875
7876/* Parse the body of a lambda expression, which is simply
7877
7878 compound-statement
7879
7880 but which requires special handling.
7881 LAMBDA_EXPR is the current representation of the lambda expression. */
7882
7883static void
7884cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7885{
7886 bool nested = (current_function_decl != NULL_TREE);
7887 if (nested)
7888 push_function_context ();
7889
7890 /* Finish the function call operator
7891 - class_specifier
7892 + late_parsing_for_member
7893 + function_definition_after_declarator
7894 + ctor_initializer_opt_and_function_body */
7895 {
7896 tree fco = lambda_function (lambda_expr);
7897 tree body;
7898 bool done = false;
7899
7900 /* Let the front end know that we are going to be defining this
7901 function. */
7902 start_preparsed_function (fco,
7903 NULL_TREE,
7904 SF_PRE_PARSED | SF_INCLASS_INLINE);
7905
7906 start_lambda_scope (fco);
7907 body = begin_function_body ();
7908
7909 /* 5.1.1.4 of the standard says:
7910 If a lambda-expression does not include a trailing-return-type, it
7911 is as if the trailing-return-type denotes the following type:
69df9882 7912 * if the compound-statement is of the form
a8b75081 7913 { return attribute-specifier [opt] expression ; }
7914 the type of the returned expression after lvalue-to-rvalue
7915 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7916 (_conv.array_ 4.2), and function-to-pointer conversion
7917 (_conv.func_ 4.3);
69df9882 7918 * otherwise, void. */
a8b75081 7919
7920 /* In a lambda that has neither a lambda-return-type-clause
7921 nor a deducible form, errors should be reported for return statements
7922 in the body. Since we used void as the placeholder return type, parsing
7923 the body as usual will give such desired behavior. */
7924 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7925 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7926 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7927 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7928 {
7929 tree compound_stmt;
7930 tree expr = NULL_TREE;
7931 cp_id_kind idk = CP_ID_KIND_NONE;
7932
7933 /* Parse tentatively in case there's more after the initial return
7934 statement. */
7935 cp_parser_parse_tentatively (parser);
7936
c247dce0 7937 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7938 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
a8b75081 7939
7940 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7941
c247dce0 7942 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7943 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
a8b75081 7944
7945 if (cp_parser_parse_definitely (parser))
7946 {
7947 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7948
7949 compound_stmt = begin_compound_stmt (0);
7950 /* Will get error here if type not deduced yet. */
7951 finish_return_stmt (expr);
7952 finish_compound_stmt (compound_stmt);
7953
7954 done = true;
7955 }
7956 }
7957
7958 if (!done)
7959 {
7960 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7961 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7962 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7963 cp_parser_compound_stmt does not pass it. */
7964 cp_parser_function_body (parser);
7965 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7966 }
7967
7968 finish_function_body (body);
7969 finish_lambda_scope ();
7970
7971 /* Finish the function and generate code for it if necessary. */
7972 expand_or_defer_fn (finish_function (/*inline*/2));
7973 }
7974
7975 if (nested)
7976 pop_function_context();
7977}
7978
0a3b29ad 7979/* Statements [gram.stmt.stmt] */
7980
ccb84981 7981/* Parse a statement.
0a3b29ad 7982
7983 statement:
7984 labeled-statement
7985 expression-statement
7986 compound-statement
7987 selection-statement
7988 iteration-statement
7989 jump-statement
7990 declaration-statement
b75b98aa 7991 try-block
7992
074ab442 7993 IN_COMPOUND is true when the statement is nested inside a
e534436e 7994 cp_parser_compound_statement; this matters for certain pragmas.
7995
7996 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7997 is a (possibly labeled) if statement which is not enclosed in braces
7998 and has an else clause. This is used to implement -Wparentheses. */
0a3b29ad 7999
8000static void
b75b98aa 8001cp_parser_statement (cp_parser* parser, tree in_statement_expr,
e534436e 8002 bool in_compound, bool *if_p)
0a3b29ad 8003{
8004 tree statement;
8005 cp_token *token;
357f7efa 8006 location_t statement_location;
0a3b29ad 8007
b75b98aa 8008 restart:
e534436e 8009 if (if_p != NULL)
8010 *if_p = false;
0a3b29ad 8011 /* There is no statement yet. */
8012 statement = NULL_TREE;
8013 /* Peek at the next token. */
8014 token = cp_lexer_peek_token (parser->lexer);
4ee9c684 8015 /* Remember the location of the first token in the statement. */
357f7efa 8016 statement_location = token->location;
0a3b29ad 8017 /* If this is a keyword, then that will often determine what kind of
8018 statement we have. */
8019 if (token->type == CPP_KEYWORD)
8020 {
8021 enum rid keyword = token->keyword;
8022
8023 switch (keyword)
8024 {
8025 case RID_CASE:
8026 case RID_DEFAULT:
17d53949 8027 /* Looks like a labeled-statement with a case label.
8028 Parse the label, and then use tail recursion to parse
8029 the statement. */
8030 cp_parser_label_for_labeled_statement (parser);
8031 goto restart;
0a3b29ad 8032
8033 case RID_IF:
8034 case RID_SWITCH:
e534436e 8035 statement = cp_parser_selection_statement (parser, if_p);
0a3b29ad 8036 break;
8037
8038 case RID_WHILE:
8039 case RID_DO:
8040 case RID_FOR:
8041 statement = cp_parser_iteration_statement (parser);
8042 break;
8043
8044 case RID_BREAK:
8045 case RID_CONTINUE:
8046 case RID_RETURN:
8047 case RID_GOTO:
8048 statement = cp_parser_jump_statement (parser);
8049 break;
8050
7a4e126b 8051 /* Objective-C++ exception-handling constructs. */
8052 case RID_AT_TRY:
8053 case RID_AT_CATCH:
8054 case RID_AT_FINALLY:
8055 case RID_AT_SYNCHRONIZED:
8056 case RID_AT_THROW:
8057 statement = cp_parser_objc_statement (parser);
8058 break;
8059
0a3b29ad 8060 case RID_TRY:
8061 statement = cp_parser_try_block (parser);
8062 break;
8063
26d3c536 8064 case RID_NAMESPACE:
8065 /* This must be a namespace alias definition. */
8066 cp_parser_declaration_statement (parser);
8067 return;
8068
0a3b29ad 8069 default:
8070 /* It might be a keyword like `int' that can start a
8071 declaration-statement. */
8072 break;
8073 }
8074 }
8075 else if (token->type == CPP_NAME)
8076 {
8077 /* If the next token is a `:', then we are looking at a
8078 labeled-statement. */
8079 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8080 if (token->type == CPP_COLON)
17d53949 8081 {
8082 /* Looks like a labeled-statement with an ordinary label.
8083 Parse the label, and then use tail recursion to parse
8084 the statement. */
8085 cp_parser_label_for_labeled_statement (parser);
8086 goto restart;
8087 }
0a3b29ad 8088 }
8089 /* Anything that starts with a `{' must be a compound-statement. */
8090 else if (token->type == CPP_OPEN_BRACE)
2363ef00 8091 statement = cp_parser_compound_statement (parser, NULL, false);
3986d990 8092 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8093 a statement all its own. */
8094 else if (token->type == CPP_PRAGMA)
8095 {
b75b98aa 8096 /* Only certain OpenMP pragmas are attached to statements, and thus
8097 are considered statements themselves. All others are not. In
8098 the context of a compound, accept the pragma as a "statement" and
074ab442 8099 return so that we can check for a close brace. Otherwise we
b75b98aa 8100 require a real statement and must go back and read one. */
8101 if (in_compound)
8102 cp_parser_pragma (parser, pragma_compound);
8103 else if (!cp_parser_pragma (parser, pragma_stmt))
8104 goto restart;
3986d990 8105 return;
8106 }
5f959b7d 8107 else if (token->type == CPP_EOF)
8108 {
8109 cp_parser_error (parser, "expected statement");
8110 return;
8111 }
0a3b29ad 8112
8113 /* Everything else must be a declaration-statement or an
ccb84981 8114 expression-statement. Try for the declaration-statement
0a3b29ad 8115 first, unless we are looking at a `;', in which case we know that
8116 we have an expression-statement. */
8117 if (!statement)
8118 {
8119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8120 {
8121 cp_parser_parse_tentatively (parser);
8122 /* Try to parse the declaration-statement. */
8123 cp_parser_declaration_statement (parser);
8124 /* If that worked, we're done. */
8125 if (cp_parser_parse_definitely (parser))
8126 return;
8127 }
8128 /* Look for an expression-statement instead. */
2363ef00 8129 statement = cp_parser_expression_statement (parser, in_statement_expr);
0a3b29ad 8130 }
8131
8132 /* Set the line number for the statement. */
bac62436 8133 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
357f7efa 8134 SET_EXPR_LOCATION (statement, statement_location);
0a3b29ad 8135}
8136
17d53949 8137/* Parse the label for a labeled-statement, i.e.
0a3b29ad 8138
17d53949 8139 identifier :
8140 case constant-expression :
8141 default :
260cf17d 8142
8143 GNU Extension:
17d53949 8144 case constant-expression ... constant-expression : statement
ccb84981 8145
17d53949 8146 When a label is parsed without errors, the label is added to the
8147 parse tree by the finish_* functions, so this function doesn't
8148 have to return the label. */
b75b98aa 8149
17d53949 8150static void
8151cp_parser_label_for_labeled_statement (cp_parser* parser)
0a3b29ad 8152{
8153 cp_token *token;
e1c8f1c5 8154 tree label = NULL_TREE;
0a3b29ad 8155
8156 /* The next token should be an identifier. */
8157 token = cp_lexer_peek_token (parser->lexer);
8158 if (token->type != CPP_NAME
8159 && token->type != CPP_KEYWORD)
8160 {
8161 cp_parser_error (parser, "expected labeled-statement");
17d53949 8162 return;
0a3b29ad 8163 }
8164
8165 switch (token->keyword)
8166 {
8167 case RID_CASE:
8168 {
260cf17d 8169 tree expr, expr_hi;
8170 cp_token *ellipsis;
0a3b29ad 8171
8172 /* Consume the `case' token. */
8173 cp_lexer_consume_token (parser->lexer);
8174 /* Parse the constant-expression. */
ccb84981 8175 expr = cp_parser_constant_expression (parser,
13795292 8176 /*allow_non_constant_p=*/false,
5f6526e1 8177 NULL);
260cf17d 8178
8179 ellipsis = cp_lexer_peek_token (parser->lexer);
8180 if (ellipsis->type == CPP_ELLIPSIS)
8181 {
653e5405 8182 /* Consume the `...' token. */
260cf17d 8183 cp_lexer_consume_token (parser->lexer);
8184 expr_hi =
8185 cp_parser_constant_expression (parser,
653e5405 8186 /*allow_non_constant_p=*/false,
260cf17d 8187 NULL);
8188 /* We don't need to emit warnings here, as the common code
8189 will do this for us. */
8190 }
8191 else
8192 expr_hi = NULL_TREE;
8193
b75b98aa 8194 if (parser->in_switch_statement_p)
e60a6f7b 8195 finish_case_label (token->location, expr, expr_hi);
b75b98aa 8196 else
ccb59bb6 8197 error_at (token->location,
8198 "case label %qE not within a switch statement",
8199 expr);
0a3b29ad 8200 }
8201 break;
8202
8203 case RID_DEFAULT:
8204 /* Consume the `default' token. */
8205 cp_lexer_consume_token (parser->lexer);
b75b98aa 8206
8207 if (parser->in_switch_statement_p)
e60a6f7b 8208 finish_case_label (token->location, NULL_TREE, NULL_TREE);
b75b98aa 8209 else
ccb59bb6 8210 error_at (token->location, "case label not within a switch statement");
0a3b29ad 8211 break;
8212
8213 default:
8214 /* Anything else must be an ordinary label. */
e1c8f1c5 8215 label = finish_label_stmt (cp_parser_identifier (parser));
0a3b29ad 8216 break;
8217 }
8218
8219 /* Require the `:' token. */
c247dce0 8220 cp_parser_require (parser, CPP_COLON, RT_COLON);
e1c8f1c5 8221
8222 /* An ordinary label may optionally be followed by attributes.
8223 However, this is only permitted if the attributes are then
8224 followed by a semicolon. This is because, for backward
8225 compatibility, when parsing
8226 lab: __attribute__ ((unused)) int i;
8227 we want the attribute to attach to "i", not "lab". */
8228 if (label != NULL_TREE
8229 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8230 {
8231 tree attrs;
8232
8233 cp_parser_parse_tentatively (parser);
8234 attrs = cp_parser_attributes_opt (parser);
8235 if (attrs == NULL_TREE
8236 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8237 cp_parser_abort_tentative_parse (parser);
8238 else if (!cp_parser_parse_definitely (parser))
8239 ;
8240 else
8241 cplus_decl_attributes (&label, attrs, 0);
8242 }
0a3b29ad 8243}
8244
8245/* Parse an expression-statement.
8246
8247 expression-statement:
8248 expression [opt] ;
8249
8250 Returns the new EXPR_STMT -- or NULL_TREE if the expression
942ab15b 8251 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8252 indicates whether this expression-statement is part of an
8253 expression statement. */
0a3b29ad 8254
8255static tree
2363ef00 8256cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
0a3b29ad 8257{
942ab15b 8258 tree statement = NULL_TREE;
5bd9bf06 8259 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 8260
942ab15b 8261 /* If the next token is a ';', then there is no expression
bd8962d5 8262 statement. */
0a3b29ad 8263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 8264 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
ccb84981 8265
5570fae0 8266 /* Give a helpful message for "A<T>::type t;" and the like. */
5bd9bf06 8267 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
5570fae0 8268 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8269 {
8270 if (TREE_CODE (statement) == SCOPE_REF)
8271 error_at (token->location, "need %<typename%> before %qE because "
8272 "%qT is a dependent scope",
8273 statement, TREE_OPERAND (statement, 0));
8274 else if (is_overloaded_fn (statement)
8275 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8276 {
8277 /* A::A a; */
8278 tree fn = get_first_fn (statement);
8279 error_at (token->location,
8280 "%<%T::%D%> names the constructor, not the type",
8281 DECL_CONTEXT (fn), DECL_NAME (fn));
8282 }
8283 }
5bd9bf06 8284
0a3b29ad 8285 /* Consume the final `;'. */
cf91b86a 8286 cp_parser_consume_semicolon_at_end_of_statement (parser);
0a3b29ad 8287
2363ef00 8288 if (in_statement_expr
942ab15b 8289 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
640aa28c 8290 /* This is the final expression statement of a statement
8291 expression. */
8292 statement = finish_stmt_expr_expr (statement, in_statement_expr);
942ab15b 8293 else if (statement)
8294 statement = finish_expr_stmt (statement);
8295 else
8296 finish_stmt ();
ccb84981 8297
0a3b29ad 8298 return statement;
8299}
8300
8301/* Parse a compound-statement.
8302
8303 compound-statement:
8304 { statement-seq [opt] }
ccb84981 8305
3dac447c 8306 GNU extension:
8307
8308 compound-statement:
8309 { label-declaration-seq [opt] statement-seq [opt] }
8310
8311 label-declaration-seq:
8312 label-declaration
8313 label-declaration-seq label-declaration
8314
632f8185 8315 Returns a tree representing the statement. */
0a3b29ad 8316
8317static tree
2363ef00 8318cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8319 bool in_try)
0a3b29ad 8320{
8321 tree compound_stmt;
8322
8323 /* Consume the `{'. */
c247dce0 8324 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
0a3b29ad 8325 return error_mark_node;
8326 /* Begin the compound-statement. */
2363ef00 8327 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
3dac447c 8328 /* If the next keyword is `__label__' we have a label declaration. */
8329 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8330 cp_parser_label_declaration (parser);
0a3b29ad 8331 /* Parse an (optional) statement-seq. */
2363ef00 8332 cp_parser_statement_seq_opt (parser, in_statement_expr);
0a3b29ad 8333 /* Finish the compound-statement. */
68f8f8cc 8334 finish_compound_stmt (compound_stmt);
0a3b29ad 8335 /* Consume the `}'. */
c247dce0 8336 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 8337
8338 return compound_stmt;
8339}
8340
8341/* Parse an (optional) statement-seq.
8342
8343 statement-seq:
8344 statement
8345 statement-seq [opt] statement */
8346
8347static void
2363ef00 8348cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
0a3b29ad 8349{
8350 /* Scan statements until there aren't any more. */
8351 while (true)
8352 {
b75b98aa 8353 cp_token *token = cp_lexer_peek_token (parser->lexer);
8354
06517bd4 8355 /* If we are looking at a `}', then we have run out of
8356 statements; the same is true if we have reached the end
8357 of file, or have stumbled upon a stray '@end'. */
b75b98aa 8358 if (token->type == CPP_CLOSE_BRACE
8359 || token->type == CPP_EOF
06517bd4 8360 || token->type == CPP_PRAGMA_EOL
8361 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
0a3b29ad 8362 break;
2672c56c 8363
8364 /* If we are in a compound statement and find 'else' then
8365 something went wrong. */
8366 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8367 {
8368 if (parser->in_statement & IN_IF_STMT)
8369 break;
8370 else
8371 {
8372 token = cp_lexer_consume_token (parser->lexer);
ccb59bb6 8373 error_at (token->location, "%<else%> without a previous %<if%>");
2672c56c 8374 }
8375 }
0a3b29ad 8376
8377 /* Parse the statement. */
e534436e 8378 cp_parser_statement (parser, in_statement_expr, true, NULL);
0a3b29ad 8379 }
8380}
8381
8382/* Parse a selection-statement.
8383
8384 selection-statement:
8385 if ( condition ) statement
8386 if ( condition ) statement else statement
ccb84981 8387 switch ( condition ) statement
0a3b29ad 8388
e534436e 8389 Returns the new IF_STMT or SWITCH_STMT.
8390
8391 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8392 is a (possibly labeled) if statement which is not enclosed in
8393 braces and has an else clause. This is used to implement
8394 -Wparentheses. */
0a3b29ad 8395
8396static tree
e534436e 8397cp_parser_selection_statement (cp_parser* parser, bool *if_p)
0a3b29ad 8398{
8399 cp_token *token;
8400 enum rid keyword;
8401
e534436e 8402 if (if_p != NULL)
8403 *if_p = false;
8404
0a3b29ad 8405 /* Peek at the next token. */
c247dce0 8406 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
0a3b29ad 8407
8408 /* See what kind of keyword it is. */
8409 keyword = token->keyword;
8410 switch (keyword)
8411 {
8412 case RID_IF:
8413 case RID_SWITCH:
8414 {
8415 tree statement;
8416 tree condition;
8417
8418 /* Look for the `('. */
c247dce0 8419 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
0a3b29ad 8420 {
8421 cp_parser_skip_to_end_of_statement (parser);
8422 return error_mark_node;
8423 }
8424
8425 /* Begin the selection-statement. */
8426 if (keyword == RID_IF)
8427 statement = begin_if_stmt ();
8428 else
8429 statement = begin_switch_stmt ();
8430
8431 /* Parse the condition. */
8432 condition = cp_parser_condition (parser);
8433 /* Look for the `)'. */
c247dce0 8434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3d0f901b 8435 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8436 /*consume_paren=*/true);
0a3b29ad 8437
8438 if (keyword == RID_IF)
8439 {
e534436e 8440 bool nested_if;
2672c56c 8441 unsigned char in_statement;
e534436e 8442
0a3b29ad 8443 /* Add the condition. */
8444 finish_if_stmt_cond (condition, statement);
8445
8446 /* Parse the then-clause. */
2672c56c 8447 in_statement = parser->in_statement;
8448 parser->in_statement |= IN_IF_STMT;
c2c7830b 8449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8450 {
8451 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
e60a6f7b 8452 add_stmt (build_empty_stmt (loc));
c2c7830b 8453 cp_lexer_consume_token (parser->lexer);
8454 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8455 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8456 "empty body in an %<if%> statement");
75b32336 8457 nested_if = false;
c2c7830b 8458 }
8459 else
8460 cp_parser_implicitly_scoped_statement (parser, &nested_if);
2672c56c 8461 parser->in_statement = in_statement;
8462
0a3b29ad 8463 finish_then_clause (statement);
8464
8465 /* If the next token is `else', parse the else-clause. */
8466 if (cp_lexer_next_token_is_keyword (parser->lexer,
8467 RID_ELSE))
8468 {
0a3b29ad 8469 /* Consume the `else' keyword. */
8470 cp_lexer_consume_token (parser->lexer);
2363ef00 8471 begin_else_clause (statement);
0a3b29ad 8472 /* Parse the else-clause. */
c2c7830b 8473 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8474 {
e60a6f7b 8475 location_t loc;
8476 loc = cp_lexer_peek_token (parser->lexer)->location;
8477 warning_at (loc,
c2c7830b 8478 OPT_Wempty_body, "suggest braces around "
8479 "empty body in an %<else%> statement");
e60a6f7b 8480 add_stmt (build_empty_stmt (loc));
c2c7830b 8481 cp_lexer_consume_token (parser->lexer);
8482 }
8483 else
8484 cp_parser_implicitly_scoped_statement (parser, NULL);
8485
0a3b29ad 8486 finish_else_clause (statement);
e534436e 8487
8488 /* If we are currently parsing a then-clause, then
8489 IF_P will not be NULL. We set it to true to
8490 indicate that this if statement has an else clause.
8491 This may trigger the Wparentheses warning below
8492 when we get back up to the parent if statement. */
8493 if (if_p != NULL)
8494 *if_p = true;
8495 }
8496 else
8497 {
8498 /* This if statement does not have an else clause. If
8499 NESTED_IF is true, then the then-clause is an if
8500 statement which does have an else clause. We warn
8501 about the potential ambiguity. */
8502 if (nested_if)
ccb59bb6 8503 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8504 "suggest explicit braces to avoid ambiguous"
8505 " %<else%>");
0a3b29ad 8506 }
8507
8508 /* Now we're all done with the if-statement. */
2363ef00 8509 finish_if_stmt (statement);
0a3b29ad 8510 }
8511 else
8512 {
c3fbce20 8513 bool in_switch_statement_p;
8487df40 8514 unsigned char in_statement;
0a3b29ad 8515
8516 /* Add the condition. */
8517 finish_switch_cond (condition, statement);
8518
8519 /* Parse the body of the switch-statement. */
c3fbce20 8520 in_switch_statement_p = parser->in_switch_statement_p;
8487df40 8521 in_statement = parser->in_statement;
c3fbce20 8522 parser->in_switch_statement_p = true;
8487df40 8523 parser->in_statement |= IN_SWITCH_STMT;
e534436e 8524 cp_parser_implicitly_scoped_statement (parser, NULL);
c3fbce20 8525 parser->in_switch_statement_p = in_switch_statement_p;
8487df40 8526 parser->in_statement = in_statement;
0a3b29ad 8527
8528 /* Now we're all done with the switch-statement. */
8529 finish_switch_stmt (statement);
8530 }
8531
8532 return statement;
8533 }
8534 break;
8535
8536 default:
8537 cp_parser_error (parser, "expected selection-statement");
8538 return error_mark_node;
8539 }
8540}
8541
ccb84981 8542/* Parse a condition.
0a3b29ad 8543
8544 condition:
8545 expression
f82f1250 8546 type-specifier-seq declarator = initializer-clause
8547 type-specifier-seq declarator braced-init-list
0a3b29ad 8548
8549 GNU Extension:
ccb84981 8550
0a3b29ad 8551 condition:
ccb84981 8552 type-specifier-seq declarator asm-specification [opt]
0a3b29ad 8553 attributes [opt] = assignment-expression
ccb84981 8554
0a3b29ad 8555 Returns the expression that should be tested. */
8556
8557static tree
45baea8b 8558cp_parser_condition (cp_parser* parser)
0a3b29ad 8559{
4b9b2871 8560 cp_decl_specifier_seq type_specifiers;
0a3b29ad 8561 const char *saved_message;
ca63c29a 8562 int declares_class_or_enum;
0a3b29ad 8563
8564 /* Try the declaration first. */
8565 cp_parser_parse_tentatively (parser);
8566 /* New types are not allowed in the type-specifier-seq for a
8567 condition. */
8568 saved_message = parser->type_definition_forbidden_message;
8569 parser->type_definition_forbidden_message
ca82e026 8570 = G_("types may not be defined in conditions");
0a3b29ad 8571 /* Parse the type-specifier-seq. */
ca63c29a 8572 cp_parser_decl_specifier_seq (parser,
8573 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8574 &type_specifiers,
8575 &declares_class_or_enum);
0a3b29ad 8576 /* Restore the saved message. */
8577 parser->type_definition_forbidden_message = saved_message;
8578 /* If all is well, we might be looking at a declaration. */
8579 if (!cp_parser_error_occurred (parser))
8580 {
8581 tree decl;
8582 tree asm_specification;
8583 tree attributes;
3046c0a3 8584 cp_declarator *declarator;
0a3b29ad 8585 tree initializer = NULL_TREE;
ccb84981 8586
0a3b29ad 8587 /* Parse the declarator. */
42bbd0ec 8588 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 8589 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 8590 /*parenthesized_p=*/NULL,
8591 /*member_p=*/false);
0a3b29ad 8592 /* Parse the attributes. */
8593 attributes = cp_parser_attributes_opt (parser);
8594 /* Parse the asm-specification. */
8595 asm_specification = cp_parser_asm_specification_opt (parser);
f82f1250 8596 /* If the next token is not an `=' or '{', then we might still be
0a3b29ad 8597 looking at an expression. For example:
ccb84981 8598
0a3b29ad 8599 if (A(a).x)
ccb84981 8600
0a3b29ad 8601 looks like a decl-specifier-seq and a declarator -- but then
8602 there is no `=', so this is an expression. */
f82f1250 8603 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8604 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8605 cp_parser_simulate_error (parser);
8606
8607 /* If we did see an `=' or '{', then we are looking at a declaration
0a3b29ad 8608 for sure. */
8609 if (cp_parser_parse_definitely (parser))
8610 {
9031d10b 8611 tree pushed_scope;
d91303a6 8612 bool non_constant_p;
f82f1250 8613 bool flags = LOOKUP_ONLYCONVERTING;
91caa6ca 8614
0a3b29ad 8615 /* Create the declaration. */
4b9b2871 8616 decl = start_decl (declarator, &type_specifiers,
0a3b29ad 8617 /*initialized_p=*/true,
91caa6ca 8618 attributes, /*prefix_attributes=*/NULL_TREE,
7f602bca 8619 &pushed_scope);
f82f1250 8620
8621 /* Parse the initializer. */
8622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8623 {
8624 initializer = cp_parser_braced_list (parser, &non_constant_p);
8625 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8626 flags = 0;
8627 }
8628 else
8629 {
8630 /* Consume the `='. */
c247dce0 8631 cp_parser_require (parser, CPP_EQ, RT_EQ);
f82f1250 8632 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8633 }
8634 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
bf8d19fe 8635 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 8636
d91303a6 8637 if (!non_constant_p)
8638 initializer = fold_non_dependent_expr (initializer);
ccb84981 8639
0a3b29ad 8640 /* Process the initializer. */
ccb84981 8641 cp_finish_decl (decl,
074ab442 8642 initializer, !non_constant_p,
ccb84981 8643 asm_specification,
f82f1250 8644 flags);
00d26680 8645
7f602bca 8646 if (pushed_scope)
8647 pop_scope (pushed_scope);
ccb84981 8648
0a3b29ad 8649 return convert_from_reference (decl);
8650 }
8651 }
8652 /* If we didn't even get past the declarator successfully, we are
8653 definitely not looking at a declaration. */
8654 else
8655 cp_parser_abort_tentative_parse (parser);
8656
8657 /* Otherwise, we are looking at an expression. */
98b326fd 8658 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 8659}
8660
9dd72ec4 8661/* Parses a traditional for-statement until the closing ')', not included. */
8662
8663static tree
8664cp_parser_c_for (cp_parser *parser)
8665{
8666 /* Normal for loop */
8667 tree stmt;
8668 tree condition = NULL_TREE;
8669 tree expression = NULL_TREE;
8670
8671 /* Begin the for-statement. */
8672 stmt = begin_for_stmt ();
8673
8674 /* Parse the initialization. */
8675 cp_parser_for_init_statement (parser);
8676 finish_for_init_stmt (stmt);
8677
8678 /* If there's a condition, process it. */
8679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8680 condition = cp_parser_condition (parser);
8681 finish_for_cond (condition, stmt);
8682 /* Look for the `;'. */
8683 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8684
8685 /* If there's an expression, process it. */
8686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8687 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8688 finish_for_expr (expression, stmt);
8689
8690 return stmt;
8691}
8692
8693/* Tries to parse a range-based for-statement:
8694
8695 range-based-for:
8696 type-specifier-seq declarator : expression
8697
8698 If succesful, assigns to *DECL the DECLARATOR and to *EXPR the
8699 expression. Note that the *DECL is returned unfinished, so
8700 later you should call cp_finish_decl().
8701
8702 Returns TRUE iff a range-based for is parsed. */
8703
8704static tree
8705cp_parser_range_for (cp_parser *parser)
8706{
8707 tree stmt, range_decl, range_expr;
8708 cp_decl_specifier_seq type_specifiers;
8709 cp_declarator *declarator;
8710 const char *saved_message;
8711 tree attributes, pushed_scope;
8712
8713 cp_parser_parse_tentatively (parser);
8714 /* New types are not allowed in the type-specifier-seq for a
8715 range-based for loop. */
8716 saved_message = parser->type_definition_forbidden_message;
8717 parser->type_definition_forbidden_message
8718 = G_("types may not be defined in range-based for loops");
8719 /* Parse the type-specifier-seq. */
8720 cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8721 /*is_trailing_return=*/false,
8722 &type_specifiers);
8723 /* Restore the saved message. */
8724 parser->type_definition_forbidden_message = saved_message;
8725 /* If all is well, we might be looking at a declaration. */
8726 if (cp_parser_error_occurred (parser))
8727 {
8728 cp_parser_abort_tentative_parse (parser);
8729 return NULL_TREE;
8730 }
8731 /* Parse the declarator. */
8732 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8733 /*ctor_dtor_or_conv_p=*/NULL,
8734 /*parenthesized_p=*/NULL,
8735 /*member_p=*/false);
8736 /* Parse the attributes. */
8737 attributes = cp_parser_attributes_opt (parser);
8738 /* The next token should be `:'. */
8739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8740 cp_parser_simulate_error (parser);
8741
8742 /* Check if it is a range-based for */
8743 if (!cp_parser_parse_definitely (parser))
8744 return NULL_TREE;
8745
8746 cp_parser_require (parser, CPP_COLON, RT_COLON);
8747 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8748 {
8749 bool expr_non_constant_p;
8750 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8751 }
8752 else
8753 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8754
8755 /* If in template, STMT is converted to a normal for-statements
8756 at instantiation. If not, it is done just ahead. */
8757 if (processing_template_decl)
8758 stmt = begin_range_for_stmt ();
8759 else
8760 stmt = begin_for_stmt ();
8761
8762 /* Create the declaration. It must be after begin{,_range}_for_stmt(). */
8763 range_decl = start_decl (declarator, &type_specifiers,
8764 /*initialized_p=*/SD_INITIALIZED,
8765 attributes, /*prefix_attributes=*/NULL_TREE,
8766 &pushed_scope);
8767 /* No scope allowed here */
8768 pop_scope (pushed_scope);
8769
8770 if (TREE_CODE (stmt) == RANGE_FOR_STMT)
8771 finish_range_for_decl (stmt, range_decl, range_expr);
8772 else
8773 /* Convert the range-based for loop into a normal for-statement. */
8774 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8775
8776 return stmt;
8777}
8778
8779/* Converts a range-based for-statement into a normal
8780 for-statement, as per the definition.
8781
8782 for (RANGE_DECL : RANGE_EXPR)
8783 BLOCK
8784
8785 should be equivalent to:
8786
8787 {
8788 auto &&__range = RANGE_EXPR;
8789 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8790 __begin != __end;
8791 ++__begin)
8792 {
8793 RANGE_DECL = *__begin;
8794 BLOCK
8795 }
8796 }
8797
8798 If RANGE_EXPR is an array:
8799 BEGIN_EXPR = __range
8800 END_EXPR = __range + ARRAY_SIZE(__range)
8801 Else:
8802 BEGIN_EXPR = begin(__range)
8803 END_EXPR = end(__range);
8804
8805 When calling begin()/end() we must use argument dependent
8806 lookup, but always considering 'std' as an associated namespace. */
8807
8808tree
8809cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8810{
8811 tree range_type, range_temp;
8812 tree begin, end;
8813 tree iter_type, begin_expr, end_expr;
8814 tree condition, expression;
8815
8816 /* Find out the type deduced by the declaration
8817 * `auto &&__range = range_expr' */
8818 range_type = cp_build_reference_type (make_auto (), true);
8819 range_type = do_auto_deduction (range_type, range_expr,
8820 type_uses_auto (range_type));
8821
8822 /* Create the __range variable */
8823 range_temp = build_decl (input_location, VAR_DECL,
8824 get_identifier ("__for_range"), range_type);
8825 TREE_USED (range_temp) = 1;
8826 DECL_ARTIFICIAL (range_temp) = 1;
8827 pushdecl (range_temp);
936983bf 8828 cp_finish_decl (range_temp, range_expr,
8829 /*is_constant_init*/false, NULL_TREE,
8830 LOOKUP_ONLYCONVERTING);
8831
9dd72ec4 8832 range_temp = convert_from_reference (range_temp);
8833
8834 if (TREE_CODE (TREE_TYPE (range_temp)) == ARRAY_TYPE)
8835 {
8836 /* If RANGE_TEMP is an array we will use pointer arithmetic */
8837 iter_type = build_pointer_type (TREE_TYPE (TREE_TYPE (range_temp)));
8838 begin_expr = range_temp;
8839 end_expr
8840 = build_binary_op (input_location, PLUS_EXPR,
8841 range_temp,
8842 array_type_nelts_top (TREE_TYPE (range_temp)), 0);
8843 }
8844 else
8845 {
8846 /* If it is not an array, we must call begin(__range)/end__range() */
8847 VEC(tree,gc) *vec;
8848
8849 begin_expr = get_identifier ("begin");
8850 vec = make_tree_vector ();
8851 VEC_safe_push (tree, gc, vec, range_temp);
8852 begin_expr = perform_koenig_lookup (begin_expr, vec,
8853 /*include_std=*/true);
8854 begin_expr = finish_call_expr (begin_expr, &vec, false, true,
8855 tf_warning_or_error);
8856 release_tree_vector (vec);
8857
8858 end_expr = get_identifier ("end");
8859 vec = make_tree_vector ();
8860 VEC_safe_push (tree, gc, vec, range_temp);
8861 end_expr = perform_koenig_lookup (end_expr, vec,
8862 /*include_std=*/true);
8863 end_expr = finish_call_expr (end_expr, &vec, false, true,
8864 tf_warning_or_error);
8865 release_tree_vector (vec);
8866
8867 /* The unqualified type of the __begin and __end temporaries should
8868 * be the same as required by the multiple auto declaration */
8869 iter_type = cv_unqualified (TREE_TYPE (begin_expr));
8870 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (end_expr))))
8871 error ("inconsistent begin/end types in range-based for: %qT and %qT",
8872 TREE_TYPE (begin_expr), TREE_TYPE (end_expr));
8873 }
8874
8875 /* The new for initialization statement */
8876 begin = build_decl (input_location, VAR_DECL,
8877 get_identifier ("__for_begin"), iter_type);
8878 TREE_USED (begin) = 1;
8879 DECL_ARTIFICIAL (begin) = 1;
8880 pushdecl (begin);
936983bf 8881 cp_finish_decl (begin, begin_expr,
8882 /*is_constant_init*/false, NULL_TREE,
8883 LOOKUP_ONLYCONVERTING);
8884
9dd72ec4 8885 end = build_decl (input_location, VAR_DECL,
8886 get_identifier ("__for_end"), iter_type);
8887 TREE_USED (end) = 1;
8888 DECL_ARTIFICIAL (end) = 1;
8889 pushdecl (end);
936983bf 8890 cp_finish_decl (end, end_expr,
8891 /*is_constant_init*/false, NULL_TREE,
8892 LOOKUP_ONLYCONVERTING);
9dd72ec4 8893
8894 finish_for_init_stmt (statement);
8895
8896/* The new for condition */
8897 condition = build_x_binary_op (NE_EXPR,
8898 begin, ERROR_MARK,
8899 end, ERROR_MARK,
8900 NULL, tf_warning_or_error);
8901 finish_for_cond (condition, statement);
8902
8903 /* The new increment expression */
8904 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8905 finish_for_expr (expression, statement);
8906
8907 /* The declaration is initialized with *__begin inside the loop body */
8908 cp_finish_decl (range_decl,
8909 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8910 /*is_constant_init*/false, NULL_TREE,
8911 LOOKUP_ONLYCONVERTING);
8912
8913 return statement;
8914}
8915
8916
0a3b29ad 8917/* Parse an iteration-statement.
8918
8919 iteration-statement:
8920 while ( condition ) statement
8921 do statement while ( expression ) ;
8922 for ( for-init-statement condition [opt] ; expression [opt] )
8923 statement
8924
9dd72ec4 8925 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
0a3b29ad 8926
8927static tree
45baea8b 8928cp_parser_iteration_statement (cp_parser* parser)
0a3b29ad 8929{
8930 cp_token *token;
8931 enum rid keyword;
8932 tree statement;
8487df40 8933 unsigned char in_statement;
0a3b29ad 8934
8935 /* Peek at the next token. */
c247dce0 8936 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
0a3b29ad 8937 if (!token)
8938 return error_mark_node;
8939
c3fbce20 8940 /* Remember whether or not we are already within an iteration
ccb84981 8941 statement. */
8487df40 8942 in_statement = parser->in_statement;
c3fbce20 8943
0a3b29ad 8944 /* See what kind of keyword it is. */
8945 keyword = token->keyword;
8946 switch (keyword)
8947 {
8948 case RID_WHILE:
8949 {
8950 tree condition;
8951
8952 /* Begin the while-statement. */
8953 statement = begin_while_stmt ();
8954 /* Look for the `('. */
c247dce0 8955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 8956 /* Parse the condition. */
8957 condition = cp_parser_condition (parser);
8958 finish_while_stmt_cond (condition, statement);
8959 /* Look for the `)'. */
c247dce0 8960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 8961 /* Parse the dependent statement. */
8487df40 8962 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 8963 cp_parser_already_scoped_statement (parser);
8487df40 8964 parser->in_statement = in_statement;
0a3b29ad 8965 /* We're done with the while-statement. */
8966 finish_while_stmt (statement);
8967 }
8968 break;
8969
8970 case RID_DO:
8971 {
8972 tree expression;
8973
8974 /* Begin the do-statement. */
8975 statement = begin_do_stmt ();
8976 /* Parse the body of the do-statement. */
8487df40 8977 parser->in_statement = IN_ITERATION_STMT;
e534436e 8978 cp_parser_implicitly_scoped_statement (parser, NULL);
8487df40 8979 parser->in_statement = in_statement;
0a3b29ad 8980 finish_do_body (statement);
8981 /* Look for the `while' keyword. */
c247dce0 8982 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
0a3b29ad 8983 /* Look for the `('. */
c247dce0 8984 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 8985 /* Parse the expression. */
98b326fd 8986 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 8987 /* We're done with the do-statement. */
8988 finish_do_stmt (expression, statement);
8989 /* Look for the `)'. */
c247dce0 8990 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 8991 /* Look for the `;'. */
c247dce0 8992 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 8993 }
8994 break;
8995
8996 case RID_FOR:
8997 {
0a3b29ad 8998 /* Look for the `('. */
c247dce0 8999 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 9000
9dd72ec4 9001 if (cxx_dialect == cxx0x)
9002 statement = cp_parser_range_for (parser);
9003 else
9004 statement = NULL_TREE;
9005 if (statement == NULL_TREE)
9006 statement = cp_parser_c_for (parser);
9007
0a3b29ad 9008 /* Look for the `)'. */
c247dce0 9009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
207355ad 9010
0a3b29ad 9011 /* Parse the body of the for-statement. */
8487df40 9012 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 9013 cp_parser_already_scoped_statement (parser);
8487df40 9014 parser->in_statement = in_statement;
0a3b29ad 9015
9016 /* We're done with the for-statement. */
9017 finish_for_stmt (statement);
9018 }
9019 break;
9020
9021 default:
9022 cp_parser_error (parser, "expected iteration-statement");
9023 statement = error_mark_node;
9024 break;
9025 }
9026
9027 return statement;
9028}
9029
9030/* Parse a for-init-statement.
9031
9032 for-init-statement:
9033 expression-statement
9034 simple-declaration */
9035
9036static void
45baea8b 9037cp_parser_for_init_statement (cp_parser* parser)
0a3b29ad 9038{
9039 /* If the next token is a `;', then we have an empty
755edffd 9040 expression-statement. Grammatically, this is also a
0a3b29ad 9041 simple-declaration, but an invalid one, because it does not
9042 declare anything. Therefore, if we did not handle this case
9043 specially, we would issue an error message about an invalid
9044 declaration. */
9045 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9046 {
9047 /* We're going to speculatively look for a declaration, falling back
9048 to an expression, if necessary. */
9049 cp_parser_parse_tentatively (parser);
9050 /* Parse the declaration. */
9051 cp_parser_simple_declaration (parser,
9052 /*function_definition_allowed_p=*/false);
9053 /* If the tentative parse failed, then we shall need to look for an
9054 expression-statement. */
9055 if (cp_parser_parse_definitely (parser))
9056 return;
9057 }
9058
10b4e9cc 9059 cp_parser_expression_statement (parser, NULL_TREE);
0a3b29ad 9060}
9061
9062/* Parse a jump-statement.
9063
9064 jump-statement:
9065 break ;
9066 continue ;
9067 return expression [opt] ;
f82f1250 9068 return braced-init-list ;
ccb84981 9069 goto identifier ;
0a3b29ad 9070
9071 GNU extension:
9072
9073 jump-statement:
9074 goto * expression ;
9075
c857cd60 9076 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
0a3b29ad 9077
9078static tree
45baea8b 9079cp_parser_jump_statement (cp_parser* parser)
0a3b29ad 9080{
9081 tree statement = error_mark_node;
9082 cp_token *token;
9083 enum rid keyword;
2672c56c 9084 unsigned char in_statement;
0a3b29ad 9085
9086 /* Peek at the next token. */
c247dce0 9087 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
0a3b29ad 9088 if (!token)
9089 return error_mark_node;
9090
9091 /* See what kind of keyword it is. */
9092 keyword = token->keyword;
9093 switch (keyword)
9094 {
9095 case RID_BREAK:
2672c56c 9096 in_statement = parser->in_statement & ~IN_IF_STMT;
9097 switch (in_statement)
c3fbce20 9098 {
8487df40 9099 case 0:
ccb59bb6 9100 error_at (token->location, "break statement not within loop or switch");
8487df40 9101 break;
9102 default:
2672c56c 9103 gcc_assert ((in_statement & IN_SWITCH_STMT)
9104 || in_statement == IN_ITERATION_STMT);
8487df40 9105 statement = finish_break_stmt ();
9106 break;
9107 case IN_OMP_BLOCK:
ccb59bb6 9108 error_at (token->location, "invalid exit from OpenMP structured block");
8487df40 9109 break;
9110 case IN_OMP_FOR:
ccb59bb6 9111 error_at (token->location, "break statement used with OpenMP for loop");
8487df40 9112 break;
c3fbce20 9113 }
c247dce0 9114 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9115 break;
9116
9117 case RID_CONTINUE:
2672c56c 9118 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
c3fbce20 9119 {
8487df40 9120 case 0:
ccb59bb6 9121 error_at (token->location, "continue statement not within a loop");
8487df40 9122 break;
9123 case IN_ITERATION_STMT:
9124 case IN_OMP_FOR:
9125 statement = finish_continue_stmt ();
9126 break;
9127 case IN_OMP_BLOCK:
ccb59bb6 9128 error_at (token->location, "invalid exit from OpenMP structured block");
8487df40 9129 break;
9130 default:
9131 gcc_unreachable ();
c3fbce20 9132 }
c247dce0 9133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9134 break;
9135
9136 case RID_RETURN:
9137 {
9138 tree expr;
f82f1250 9139 bool expr_non_constant_p;
0a3b29ad 9140
f82f1250 9141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9142 {
bf8d19fe 9143 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 9144 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9145 }
9146 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 9147 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 9148 else
f82f1250 9149 /* If the next token is a `;', then there is no
9150 expression. */
0a3b29ad 9151 expr = NULL_TREE;
9152 /* Build the return-statement. */
9153 statement = finish_return_stmt (expr);
9154 /* Look for the final `;'. */
c247dce0 9155 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9156 }
9157 break;
9158
9159 case RID_GOTO:
9160 /* Create the goto-statement. */
9161 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9162 {
9163 /* Issue a warning about this use of a GNU extension. */
21ca8540 9164 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
0a3b29ad 9165 /* Consume the '*' token. */
9166 cp_lexer_consume_token (parser->lexer);
9167 /* Parse the dependent expression. */
98b326fd 9168 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
0a3b29ad 9169 }
9170 else
9171 finish_goto_stmt (cp_parser_identifier (parser));
9172 /* Look for the final `;'. */
c247dce0 9173 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9174 break;
9175
9176 default:
9177 cp_parser_error (parser, "expected jump-statement");
9178 break;
9179 }
9180
9181 return statement;
9182}
9183
9184/* Parse a declaration-statement.
9185
9186 declaration-statement:
9187 block-declaration */
9188
9189static void
45baea8b 9190cp_parser_declaration_statement (cp_parser* parser)
0a3b29ad 9191{
3046c0a3 9192 void *p;
9193
9194 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9195 p = obstack_alloc (&declarator_obstack, 0);
9196
9197 /* Parse the block-declaration. */
0a3b29ad 9198 cp_parser_block_declaration (parser, /*statement_p=*/true);
9199
3046c0a3 9200 /* Free any declarators allocated. */
9201 obstack_free (&declarator_obstack, p);
9202
0a3b29ad 9203 /* Finish off the statement. */
9204 finish_stmt ();
9205}
9206
9207/* Some dependent statements (like `if (cond) statement'), are
9208 implicitly in their own scope. In other words, if the statement is
9209 a single statement (as opposed to a compound-statement), it is
9210 none-the-less treated as if it were enclosed in braces. Any
9211 declarations appearing in the dependent statement are out of scope
9212 after control passes that point. This function parses a statement,
9213 but ensures that is in its own scope, even if it is not a
ccb84981 9214 compound-statement.
0a3b29ad 9215
e534436e 9216 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9217 is a (possibly labeled) if statement which is not enclosed in
9218 braces and has an else clause. This is used to implement
9219 -Wparentheses.
9220
0a3b29ad 9221 Returns the new statement. */
9222
9223static tree
e534436e 9224cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
0a3b29ad 9225{
9226 tree statement;
9227
e534436e 9228 if (if_p != NULL)
9229 *if_p = false;
9230
50247dd9 9231 /* Mark if () ; with a special NOP_EXPR. */
9232 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9233 {
e60a6f7b 9234 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50247dd9 9235 cp_lexer_consume_token (parser->lexer);
e60a6f7b 9236 statement = add_stmt (build_empty_stmt (loc));
50247dd9 9237 }
9238 /* if a compound is opened, we simply parse the statement directly. */
9239 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9240 statement = cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 9241 /* If the token is not a `{', then we must take special action. */
50247dd9 9242 else
0a3b29ad 9243 {
9244 /* Create a compound-statement. */
2363ef00 9245 statement = begin_compound_stmt (0);
0a3b29ad 9246 /* Parse the dependent-statement. */
e534436e 9247 cp_parser_statement (parser, NULL_TREE, false, if_p);
0a3b29ad 9248 /* Finish the dummy compound-statement. */
68f8f8cc 9249 finish_compound_stmt (statement);
0a3b29ad 9250 }
0a3b29ad 9251
9252 /* Return the statement. */
9253 return statement;
9254}
9255
9256/* For some dependent statements (like `while (cond) statement'), we
9257 have already created a scope. Therefore, even if the dependent
9258 statement is a compound-statement, we do not want to create another
9259 scope. */
9260
9261static void
45baea8b 9262cp_parser_already_scoped_statement (cp_parser* parser)
0a3b29ad 9263{
2363ef00 9264 /* If the token is a `{', then we must take special action. */
9265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
e534436e 9266 cp_parser_statement (parser, NULL_TREE, false, NULL);
2363ef00 9267 else
0a3b29ad 9268 {
2363ef00 9269 /* Avoid calling cp_parser_compound_statement, so that we
9270 don't create a new scope. Do everything else by hand. */
c247dce0 9271 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
1f529806 9272 /* If the next keyword is `__label__' we have a label declaration. */
9273 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9274 cp_parser_label_declaration (parser);
9275 /* Parse an (optional) statement-seq. */
b75b98aa 9276 cp_parser_statement_seq_opt (parser, NULL_TREE);
c247dce0 9277 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 9278 }
0a3b29ad 9279}
9280
9281/* Declarations [gram.dcl.dcl] */
9282
9283/* Parse an optional declaration-sequence.
9284
9285 declaration-seq:
9286 declaration
9287 declaration-seq declaration */
9288
9289static void
45baea8b 9290cp_parser_declaration_seq_opt (cp_parser* parser)
0a3b29ad 9291{
9292 while (true)
9293 {
9294 cp_token *token;
9295
9296 token = cp_lexer_peek_token (parser->lexer);
9297
9298 if (token->type == CPP_CLOSE_BRACE
b75b98aa 9299 || token->type == CPP_EOF
9300 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 9301 break;
9302
ccb84981 9303 if (token->type == CPP_SEMICOLON)
0a3b29ad 9304 {
9305 /* A declaration consisting of a single semicolon is
9306 invalid. Allow it unless we're being pedantic. */
0a3b29ad 9307 cp_lexer_consume_token (parser->lexer);
8864917d 9308 if (!in_system_header)
21ca8540 9309 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
0a3b29ad 9310 continue;
9311 }
9312
2e1f41a9 9313 /* If we're entering or exiting a region that's implicitly
93523877 9314 extern "C", modify the lang context appropriately. */
2e1f41a9 9315 if (!parser->implicit_extern_c && token->implicit_extern_c)
9316 {
9317 push_lang_context (lang_name_c);
9318 parser->implicit_extern_c = true;
9319 }
9320 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9321 {
9322 pop_lang_context ();
9323 parser->implicit_extern_c = false;
9324 }
9325
3986d990 9326 if (token->type == CPP_PRAGMA)
9327 {
9328 /* A top-level declaration can consist solely of a #pragma.
9329 A nested declaration cannot, so this is done here and not
9330 in cp_parser_declaration. (A #pragma at block scope is
9331 handled in cp_parser_statement.) */
b75b98aa 9332 cp_parser_pragma (parser, pragma_external);
3986d990 9333 continue;
9334 }
9335
313a21c0 9336 /* Parse the declaration itself. */
0a3b29ad 9337 cp_parser_declaration (parser);
9338 }
9339}
9340
9341/* Parse a declaration.
9342
9343 declaration:
9344 block-declaration
9345 function-definition
9346 template-declaration
9347 explicit-instantiation
9348 explicit-specialization
9349 linkage-specification
ccb84981 9350 namespace-definition
2c6a8806 9351
9352 GNU extension:
9353
9354 declaration:
9355 __extension__ declaration */
0a3b29ad 9356
9357static void
45baea8b 9358cp_parser_declaration (cp_parser* parser)
0a3b29ad 9359{
9360 cp_token token1;
9361 cp_token token2;
2c6a8806 9362 int saved_pedantic;
3046c0a3 9363 void *p;
a336eb4b 9364 tree attributes = NULL_TREE;
2c6a8806 9365
9366 /* Check for the `__extension__' keyword. */
9367 if (cp_parser_extension_opt (parser, &saved_pedantic))
9368 {
9369 /* Parse the qualified declaration. */
9370 cp_parser_declaration (parser);
9371 /* Restore the PEDANTIC flag. */
9372 pedantic = saved_pedantic;
9373
9374 return;
9375 }
0a3b29ad 9376
9377 /* Try to figure out what kind of declaration is present. */
9378 token1 = *cp_lexer_peek_token (parser->lexer);
ccb84981 9379
0a3b29ad 9380 if (token1.type != CPP_EOF)
9381 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8102ba14 9382 else
0f803187 9383 {
9384 token2.type = CPP_EOF;
9385 token2.keyword = RID_MAX;
9386 }
0a3b29ad 9387
3046c0a3 9388 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9389 p = obstack_alloc (&declarator_obstack, 0);
9390
0a3b29ad 9391 /* If the next token is `extern' and the following token is a string
9392 literal, then we have a linkage specification. */
9393 if (token1.keyword == RID_EXTERN
9394 && cp_parser_is_string_literal (&token2))
9395 cp_parser_linkage_specification (parser);
9396 /* If the next token is `template', then we have either a template
9397 declaration, an explicit instantiation, or an explicit
9398 specialization. */
9399 else if (token1.keyword == RID_TEMPLATE)
9400 {
9401 /* `template <>' indicates a template specialization. */
9402 if (token2.type == CPP_LESS
9403 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9404 cp_parser_explicit_specialization (parser);
9405 /* `template <' indicates a template declaration. */
9406 else if (token2.type == CPP_LESS)
9407 cp_parser_template_declaration (parser, /*member_p=*/false);
9408 /* Anything else must be an explicit instantiation. */
9409 else
9410 cp_parser_explicit_instantiation (parser);
9411 }
9412 /* If the next token is `export', then we have a template
9413 declaration. */
9414 else if (token1.keyword == RID_EXPORT)
9415 cp_parser_template_declaration (parser, /*member_p=*/false);
9416 /* If the next token is `extern', 'static' or 'inline' and the one
9417 after that is `template', we have a GNU extended explicit
9418 instantiation directive. */
9419 else if (cp_parser_allow_gnu_extensions_p (parser)
9420 && (token1.keyword == RID_EXTERN
9421 || token1.keyword == RID_STATIC
9422 || token1.keyword == RID_INLINE)
9423 && token2.keyword == RID_TEMPLATE)
9424 cp_parser_explicit_instantiation (parser);
9425 /* If the next token is `namespace', check for a named or unnamed
9426 namespace definition. */
9427 else if (token1.keyword == RID_NAMESPACE
9428 && (/* A named namespace definition. */
9429 (token2.type == CPP_NAME
ccb84981 9430 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
799435d8 9431 != CPP_EQ))
0a3b29ad 9432 /* An unnamed namespace definition. */
30e98711 9433 || token2.type == CPP_OPEN_BRACE
9434 || token2.keyword == RID_ATTRIBUTE))
0a3b29ad 9435 cp_parser_namespace_definition (parser);
93635a8e 9436 /* An inline (associated) namespace definition. */
9437 else if (token1.keyword == RID_INLINE
9438 && token2.keyword == RID_NAMESPACE)
9439 cp_parser_namespace_definition (parser);
7a4e126b 9440 /* Objective-C++ declaration/definition. */
9441 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
a336eb4b 9442 cp_parser_objc_declaration (parser, NULL_TREE);
9443 else if (c_dialect_objc ()
9444 && token1.keyword == RID_ATTRIBUTE
9445 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9446 cp_parser_objc_declaration (parser, attributes);
0a3b29ad 9447 /* We must have either a block declaration or a function
9448 definition. */
9449 else
9450 /* Try to parse a block-declaration, or a function-definition. */
9451 cp_parser_block_declaration (parser, /*statement_p=*/false);
3046c0a3 9452
9453 /* Free any declarators allocated. */
9454 obstack_free (&declarator_obstack, p);
0a3b29ad 9455}
9456
ccb84981 9457/* Parse a block-declaration.
0a3b29ad 9458
9459 block-declaration:
9460 simple-declaration
9461 asm-definition
9462 namespace-alias-definition
9463 using-declaration
ccb84981 9464 using-directive
0a3b29ad 9465
9466 GNU Extension:
9467
9468 block-declaration:
ccb84981 9469 __extension__ block-declaration
0a3b29ad 9470
7a05c4b1 9471 C++0x Extension:
9472
9473 block-declaration:
9474 static_assert-declaration
9475
755edffd 9476 If STATEMENT_P is TRUE, then this block-declaration is occurring as
0a3b29ad 9477 part of a declaration-statement. */
9478
9479static void
ccb84981 9480cp_parser_block_declaration (cp_parser *parser,
0a3b29ad 9481 bool statement_p)
9482{
9483 cp_token *token1;
9484 int saved_pedantic;
9485
9486 /* Check for the `__extension__' keyword. */
9487 if (cp_parser_extension_opt (parser, &saved_pedantic))
9488 {
9489 /* Parse the qualified declaration. */
9490 cp_parser_block_declaration (parser, statement_p);
9491 /* Restore the PEDANTIC flag. */
9492 pedantic = saved_pedantic;
9493
9494 return;
9495 }
9496
9497 /* Peek at the next token to figure out which kind of declaration is
9498 present. */
9499 token1 = cp_lexer_peek_token (parser->lexer);
9500
9501 /* If the next keyword is `asm', we have an asm-definition. */
9502 if (token1->keyword == RID_ASM)
9503 {
9504 if (statement_p)
9505 cp_parser_commit_to_tentative_parse (parser);
9506 cp_parser_asm_definition (parser);
9507 }
9508 /* If the next keyword is `namespace', we have a
9509 namespace-alias-definition. */
9510 else if (token1->keyword == RID_NAMESPACE)
9511 cp_parser_namespace_alias_definition (parser);
9512 /* If the next keyword is `using', we have either a
9513 using-declaration or a using-directive. */
9514 else if (token1->keyword == RID_USING)
9515 {
9516 cp_token *token2;
9517
9518 if (statement_p)
9519 cp_parser_commit_to_tentative_parse (parser);
9520 /* If the token after `using' is `namespace', then we have a
9521 using-directive. */
9522 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9523 if (token2->keyword == RID_NAMESPACE)
9524 cp_parser_using_directive (parser);
9525 /* Otherwise, it's a using-declaration. */
9526 else
da2a3271 9527 cp_parser_using_declaration (parser,
9528 /*access_declaration_p=*/false);
0a3b29ad 9529 }
3dac447c 9530 /* If the next keyword is `__label__' we have a misplaced label
9531 declaration. */
0a3b29ad 9532 else if (token1->keyword == RID_LABEL)
9533 {
3dac447c 9534 cp_lexer_consume_token (parser->lexer);
ccb59bb6 9535 error_at (token1->location, "%<__label__%> not at the beginning of a block");
3dac447c 9536 cp_parser_skip_to_end_of_statement (parser);
9537 /* If the next token is now a `;', consume it. */
9538 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9539 cp_lexer_consume_token (parser->lexer);
0a3b29ad 9540 }
7a05c4b1 9541 /* If the next token is `static_assert' we have a static assertion. */
9542 else if (token1->keyword == RID_STATIC_ASSERT)
9543 cp_parser_static_assert (parser, /*member_p=*/false);
0a3b29ad 9544 /* Anything else must be a simple-declaration. */
9545 else
9546 cp_parser_simple_declaration (parser, !statement_p);
9547}
9548
9549/* Parse a simple-declaration.
9550
9551 simple-declaration:
ccb84981 9552 decl-specifier-seq [opt] init-declarator-list [opt] ;
0a3b29ad 9553
9554 init-declarator-list:
9555 init-declarator
ccb84981 9556 init-declarator-list , init-declarator
0a3b29ad 9557
755edffd 9558 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6beb3f76 9559 function-definition as a simple-declaration. */
0a3b29ad 9560
9561static void
ccb84981 9562cp_parser_simple_declaration (cp_parser* parser,
653e5405 9563 bool function_definition_allowed_p)
0a3b29ad 9564{
4b9b2871 9565 cp_decl_specifier_seq decl_specifiers;
8172be22 9566 int declares_class_or_enum;
0a3b29ad 9567 bool saw_declarator;
9568
9569 /* Defer access checks until we know what is being declared; the
9570 checks for names appearing in the decl-specifier-seq should be
9571 done as if we were in the scope of the thing being declared. */
4f62c42e 9572 push_deferring_access_checks (dk_deferred);
9b57b06b 9573
0a3b29ad 9574 /* Parse the decl-specifier-seq. We have to keep track of whether
9575 or not the decl-specifier-seq declares a named class or
9576 enumeration type, since that is the only case in which the
ccb84981 9577 init-declarator-list is allowed to be empty.
0a3b29ad 9578
9579 [dcl.dcl]
9580
9581 In a simple-declaration, the optional init-declarator-list can be
9582 omitted only when declaring a class or enumeration, that is when
9583 the decl-specifier-seq contains either a class-specifier, an
9584 elaborated-type-specifier, or an enum-specifier. */
4b9b2871 9585 cp_parser_decl_specifier_seq (parser,
9586 CP_PARSER_FLAGS_OPTIONAL,
9587 &decl_specifiers,
9588 &declares_class_or_enum);
0a3b29ad 9589 /* We no longer need to defer access checks. */
9b57b06b 9590 stop_deferring_access_checks ();
7488d745 9591
878870b4 9592 /* In a block scope, a valid declaration must always have a
9593 decl-specifier-seq. By not trying to parse declarators, we can
9594 resolve the declaration/expression ambiguity more quickly. */
207355ad 9595 if (!function_definition_allowed_p
4b9b2871 9596 && !decl_specifiers.any_specifiers_p)
878870b4 9597 {
9598 cp_parser_error (parser, "expected declaration");
9599 goto done;
9600 }
9601
954ad420 9602 /* If the next two tokens are both identifiers, the code is
9603 erroneous. The usual cause of this situation is code like:
9604
9605 T t;
9606
9607 where "T" should name a type -- but does not. */
67484828 9608 if (!decl_specifiers.any_type_specifiers_p
882d3c4f 9609 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
954ad420 9610 {
4f62c42e 9611 /* If parsing tentatively, we should commit; we really are
954ad420 9612 looking at a declaration. */
9613 cp_parser_commit_to_tentative_parse (parser);
9614 /* Give up. */
878870b4 9615 goto done;
954ad420 9616 }
9031d10b 9617
d1a64350 9618 /* If we have seen at least one decl-specifier, and the next token
9619 is not a parenthesis, then we must be looking at a declaration.
9620 (After "int (" we might be looking at a functional cast.) */
9031d10b 9621 if (decl_specifiers.any_specifiers_p
f82f1250 9622 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8659a722 9623 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9624 && !cp_parser_error_occurred (parser))
d1a64350 9625 cp_parser_commit_to_tentative_parse (parser);
954ad420 9626
0a3b29ad 9627 /* Keep going until we hit the `;' at the end of the simple
9628 declaration. */
9629 saw_declarator = false;
ccb84981 9630 while (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 9631 CPP_SEMICOLON))
9632 {
9633 cp_token *token;
9634 bool function_definition_p;
8172be22 9635 tree decl;
0a3b29ad 9636
b60e927a 9637 if (saw_declarator)
9638 {
9639 /* If we are processing next declarator, coma is expected */
9640 token = cp_lexer_peek_token (parser->lexer);
9641 gcc_assert (token->type == CPP_COMMA);
9642 cp_lexer_consume_token (parser->lexer);
9643 }
9644 else
9645 saw_declarator = true;
9646
0a3b29ad 9647 /* Parse the init-declarator. */
4b9b2871 9648 decl = cp_parser_init_declarator (parser, &decl_specifiers,
3369eb76 9649 /*checks=*/NULL,
8172be22 9650 function_definition_allowed_p,
9651 /*member_p=*/false,
9652 declares_class_or_enum,
9653 &function_definition_p);
7e9a6a16 9654 /* If an error occurred while parsing tentatively, exit quickly.
9655 (That usually happens when in the body of a function; each
9656 statement is treated as a declaration-statement until proven
9657 otherwise.) */
9658 if (cp_parser_error_occurred (parser))
878870b4 9659 goto done;
0a3b29ad 9660 /* Handle function definitions specially. */
9661 if (function_definition_p)
9662 {
9663 /* If the next token is a `,', then we are probably
9664 processing something like:
9665
9666 void f() {}, *p;
9667
9668 which is erroneous. */
9669 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
ad9ae192 9670 {
9671 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 9672 error_at (token->location,
9673 "mixing"
9674 " declarations and function-definitions is forbidden");
ad9ae192 9675 }
0a3b29ad 9676 /* Otherwise, we're done with the list of declarators. */
9677 else
7488d745 9678 {
9b57b06b 9679 pop_deferring_access_checks ();
7488d745 9680 return;
9681 }
0a3b29ad 9682 }
9683 /* The next token should be either a `,' or a `;'. */
9684 token = cp_lexer_peek_token (parser->lexer);
9685 /* If it's a `,', there are more declarators to come. */
9686 if (token->type == CPP_COMMA)
b60e927a 9687 /* will be consumed next time around */;
0a3b29ad 9688 /* If it's a `;', we are done. */
9689 else if (token->type == CPP_SEMICOLON)
9690 break;
9691 /* Anything else is an error. */
9692 else
9693 {
d1a64350 9694 /* If we have already issued an error message we don't need
9695 to issue another one. */
9696 if (decl != error_mark_node
efcbcf83 9697 || cp_parser_uncommitted_to_tentative_parse_p (parser))
a2c5b975 9698 cp_parser_error (parser, "expected %<,%> or %<;%>");
0a3b29ad 9699 /* Skip tokens until we reach the end of the statement. */
9700 cp_parser_skip_to_end_of_statement (parser);
2c584053 9701 /* If the next token is now a `;', consume it. */
9702 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9703 cp_lexer_consume_token (parser->lexer);
878870b4 9704 goto done;
0a3b29ad 9705 }
9706 /* After the first time around, a function-definition is not
9707 allowed -- even if it was OK at first. For example:
9708
653e5405 9709 int i, f() {}
0a3b29ad 9710
653e5405 9711 is not valid. */
0a3b29ad 9712 function_definition_allowed_p = false;
9713 }
9714
9715 /* Issue an error message if no declarators are present, and the
9716 decl-specifier-seq does not itself declare a class or
9717 enumeration. */
9718 if (!saw_declarator)
9719 {
9720 if (cp_parser_declares_only_class_p (parser))
4b9b2871 9721 shadow_tag (&decl_specifiers);
0a3b29ad 9722 /* Perform any deferred access checks. */
9b57b06b 9723 perform_deferred_access_checks ();
0a3b29ad 9724 }
9725
9726 /* Consume the `;'. */
c247dce0 9727 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9728
878870b4 9729 done:
9730 pop_deferring_access_checks ();
0a3b29ad 9731}
9732
9733/* Parse a decl-specifier-seq.
9734
9735 decl-specifier-seq:
9736 decl-specifier-seq [opt] decl-specifier
9737
9738 decl-specifier:
9739 storage-class-specifier
9740 type-specifier
9741 function-specifier
9742 friend
ccb84981 9743 typedef
0a3b29ad 9744
9745 GNU Extension:
9746
e67a67ea 9747 decl-specifier:
9748 attributes
0a3b29ad 9749
4b9b2871 9750 Set *DECL_SPECS to a representation of the decl-specifier-seq.
0a3b29ad 9751
fb871e92 9752 The parser flags FLAGS is used to control type-specifier parsing.
8172be22 9753
9754 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8e594c09 9755 flags:
8172be22 9756
9757 1: one of the decl-specifiers is an elaborated-type-specifier
653e5405 9758 (i.e., a type declaration)
8172be22 9759 2: one of the decl-specifiers is an enum-specifier or a
653e5405 9760 class-specifier (i.e., a type definition)
207355ad 9761
8172be22 9762 */
0a3b29ad 9763
4b9b2871 9764static void
ccb84981 9765cp_parser_decl_specifier_seq (cp_parser* parser,
4b9b2871 9766 cp_parser_flags flags,
9767 cp_decl_specifier_seq *decl_specs,
8172be22 9768 int* declares_class_or_enum)
0a3b29ad 9769{
87647325 9770 bool constructor_possible_p = !parser->in_declarator_p;
ad9ae192 9771 cp_token *start_token = NULL;
ccb84981 9772
4b9b2871 9773 /* Clear DECL_SPECS. */
9774 clear_decl_specs (decl_specs);
9775
0a3b29ad 9776 /* Assume no class or enumeration type is declared. */
8172be22 9777 *declares_class_or_enum = 0;
0a3b29ad 9778
0a3b29ad 9779 /* Keep reading specifiers until there are no more to read. */
9780 while (true)
9781 {
0a3b29ad 9782 bool constructor_p;
4b9b2871 9783 bool found_decl_spec;
0a3b29ad 9784 cp_token *token;
9785
9786 /* Peek at the next token. */
9787 token = cp_lexer_peek_token (parser->lexer);
ad9ae192 9788
9789 /* Save the first token of the decl spec list for error
9790 reporting. */
9791 if (!start_token)
9792 start_token = token;
0a3b29ad 9793 /* Handle attributes. */
9794 if (token->keyword == RID_ATTRIBUTE)
9795 {
9796 /* Parse the attributes. */
207355ad 9797 decl_specs->attributes
4b9b2871 9798 = chainon (decl_specs->attributes,
9799 cp_parser_attributes_opt (parser));
0a3b29ad 9800 continue;
9801 }
4b9b2871 9802 /* Assume we will find a decl-specifier keyword. */
9803 found_decl_spec = true;
0a3b29ad 9804 /* If the next token is an appropriate keyword, we can simply
9805 add it to the list. */
9806 switch (token->keyword)
9807 {
0a3b29ad 9808 /* decl-specifier:
17814aca 9809 friend
9810 constexpr */
4b9b2871 9811 case RID_FRIEND:
acd412e7 9812 if (!at_class_scope_p ())
9813 {
ccb59bb6 9814 error_at (token->location, "%<friend%> used outside of class");
acd412e7 9815 cp_lexer_purge_token (parser->lexer);
9816 }
9817 else
9818 {
9819 ++decl_specs->specs[(int) ds_friend];
9820 /* Consume the token. */
9821 cp_lexer_consume_token (parser->lexer);
9822 }
0a3b29ad 9823 break;
9824
17814aca 9825 case RID_CONSTEXPR:
9826 ++decl_specs->specs[(int) ds_constexpr];
9827 cp_lexer_consume_token (parser->lexer);
9828 break;
9829
0a3b29ad 9830 /* function-specifier:
9831 inline
9832 virtual
9833 explicit */
9834 case RID_INLINE:
9835 case RID_VIRTUAL:
9836 case RID_EXPLICIT:
4b9b2871 9837 cp_parser_function_specifier_opt (parser, decl_specs);
0a3b29ad 9838 break;
ccb84981 9839
0a3b29ad 9840 /* decl-specifier:
9841 typedef */
9842 case RID_TYPEDEF:
4b9b2871 9843 ++decl_specs->specs[(int) ds_typedef];
0a3b29ad 9844 /* Consume the token. */
9845 cp_lexer_consume_token (parser->lexer);
b3c48b5d 9846 /* A constructor declarator cannot appear in a typedef. */
9847 constructor_possible_p = false;
f3b70d2f 9848 /* The "typedef" keyword can only occur in a declaration; we
9849 may as well commit at this point. */
9850 cp_parser_commit_to_tentative_parse (parser);
ceec99b9 9851
9852 if (decl_specs->storage_class != sc_none)
9853 decl_specs->conflicting_specifiers_p = true;
0a3b29ad 9854 break;
9855
9856 /* storage-class-specifier:
9857 auto
9858 register
9859 static
9860 extern
ccb84981 9861 mutable
0a3b29ad 9862
653e5405 9863 GNU Extension:
0a3b29ad 9864 thread */
9865 case RID_AUTO:
45b44d0a 9866 if (cxx_dialect == cxx98)
9867 {
46f4817e 9868 /* Consume the token. */
9869 cp_lexer_consume_token (parser->lexer);
9870
45b44d0a 9871 /* Complain about `auto' as a storage specifier, if
9872 we're complaining about C++0x compatibility. */
ccb59bb6 9873 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9874 " will change meaning in C++0x; please remove it");
45b44d0a 9875
9876 /* Set the storage class anyway. */
ad9ae192 9877 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9878 token->location);
45b44d0a 9879 }
46f4817e 9880 else
9881 /* C++0x auto type-specifier. */
9882 found_decl_spec = false;
45b44d0a 9883 break;
9884
0a3b29ad 9885 case RID_REGISTER:
9886 case RID_STATIC:
9887 case RID_EXTERN:
9888 case RID_MUTABLE:
4b9b2871 9889 /* Consume the token. */
9890 cp_lexer_consume_token (parser->lexer);
ad9ae192 9891 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9892 token->location);
4b9b2871 9893 break;
0a3b29ad 9894 case RID_THREAD:
4b9b2871 9895 /* Consume the token. */
9896 cp_lexer_consume_token (parser->lexer);
9897 ++decl_specs->specs[(int) ds_thread];
0a3b29ad 9898 break;
ccb84981 9899
0a3b29ad 9900 default:
4b9b2871 9901 /* We did not yet find a decl-specifier yet. */
9902 found_decl_spec = false;
0a3b29ad 9903 break;
9904 }
9905
ca63c29a 9906 if (found_decl_spec
9907 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9908 && token->keyword != RID_CONSTEXPR)
9909 error ("decl-specifier invalid in condition");
9910
0a3b29ad 9911 /* Constructors are a special case. The `S' in `S()' is not a
9912 decl-specifier; it is the beginning of the declarator. */
207355ad 9913 constructor_p
4b9b2871 9914 = (!found_decl_spec
9915 && constructor_possible_p
207355ad 9916 && (cp_parser_constructor_declarator_p
4b9b2871 9917 (parser, decl_specs->specs[(int) ds_friend] != 0)));
0a3b29ad 9918
9919 /* If we don't have a DECL_SPEC yet, then we must be looking at
9920 a type-specifier. */
4b9b2871 9921 if (!found_decl_spec && !constructor_p)
0a3b29ad 9922 {
8172be22 9923 int decl_spec_declares_class_or_enum;
0a3b29ad 9924 bool is_cv_qualifier;
4b9b2871 9925 tree type_spec;
0a3b29ad 9926
4b9b2871 9927 type_spec
0a3b29ad 9928 = cp_parser_type_specifier (parser, flags,
4b9b2871 9929 decl_specs,
0a3b29ad 9930 /*is_declaration=*/true,
9931 &decl_spec_declares_class_or_enum,
9932 &is_cv_qualifier);
0a3b29ad 9933 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9934
9935 /* If this type-specifier referenced a user-defined type
9936 (a typedef, class-name, etc.), then we can't allow any
9937 more such type-specifiers henceforth.
9938
9939 [dcl.spec]
9940
9941 The longest sequence of decl-specifiers that could
9942 possibly be a type name is taken as the
9943 decl-specifier-seq of a declaration. The sequence shall
9944 be self-consistent as described below.
9945
9946 [dcl.type]
9947
9948 As a general rule, at most one type-specifier is allowed
9949 in the complete decl-specifier-seq of a declaration. The
9950 only exceptions are the following:
9951
9952 -- const or volatile can be combined with any other
ccb84981 9953 type-specifier.
0a3b29ad 9954
9955 -- signed or unsigned can be combined with char, long,
9956 short, or int.
9957
9958 -- ..
9959
9960 Example:
9961
9962 typedef char* Pc;
9963 void g (const int Pc);
9964
9965 Here, Pc is *not* part of the decl-specifier seq; it's
9966 the declarator. Therefore, once we see a type-specifier
9967 (other than a cv-qualifier), we forbid any additional
9968 user-defined types. We *do* still allow things like `int
9969 int' to be considered a decl-specifier-seq, and issue the
9970 error message later. */
4b9b2871 9971 if (type_spec && !is_cv_qualifier)
0a3b29ad 9972 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
b3c48b5d 9973 /* A constructor declarator cannot follow a type-specifier. */
4b9b2871 9974 if (type_spec)
0a3b29ad 9975 {
4b9b2871 9976 constructor_possible_p = false;
9977 found_decl_spec = true;
67484828 9978 if (!is_cv_qualifier)
9979 decl_specs->any_type_specifiers_p = true;
0a3b29ad 9980 }
0a3b29ad 9981 }
9982
4b9b2871 9983 /* If we still do not have a DECL_SPEC, then there are no more
9984 decl-specifiers. */
9985 if (!found_decl_spec)
9986 break;
0a3b29ad 9987
4b9b2871 9988 decl_specs->any_specifiers_p = true;
0a3b29ad 9989 /* After we see one decl-specifier, further decl-specifiers are
9990 always optional. */
9991 flags |= CP_PARSER_FLAGS_OPTIONAL;
9992 }
9993
ad9ae192 9994 cp_parser_check_decl_spec (decl_specs, start_token->location);
36c61b55 9995
2822900d 9996 /* Don't allow a friend specifier with a class definition. */
4b9b2871 9997 if (decl_specs->specs[(int) ds_friend] != 0
9998 && (*declares_class_or_enum & 2))
ccb59bb6 9999 error_at (start_token->location,
10000 "class definition may not be declared a friend");
0a3b29ad 10001}
10002
ccb84981 10003/* Parse an (optional) storage-class-specifier.
0a3b29ad 10004
10005 storage-class-specifier:
10006 auto
10007 register
10008 static
10009 extern
ccb84981 10010 mutable
0a3b29ad 10011
10012 GNU Extension:
10013
10014 storage-class-specifier:
10015 thread
10016
10017 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
ccb84981 10018
0a3b29ad 10019static tree
45baea8b 10020cp_parser_storage_class_specifier_opt (cp_parser* parser)
0a3b29ad 10021{
10022 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10023 {
10024 case RID_AUTO:
45b44d0a 10025 if (cxx_dialect != cxx98)
10026 return NULL_TREE;
10027 /* Fall through for C++98. */
10028
0a3b29ad 10029 case RID_REGISTER:
10030 case RID_STATIC:
10031 case RID_EXTERN:
10032 case RID_MUTABLE:
10033 case RID_THREAD:
10034 /* Consume the token. */
3369eb76 10035 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 10036
10037 default:
10038 return NULL_TREE;
10039 }
10040}
10041
ccb84981 10042/* Parse an (optional) function-specifier.
0a3b29ad 10043
10044 function-specifier:
10045 inline
10046 virtual
10047 explicit
10048
4b9b2871 10049 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10050 Updates DECL_SPECS, if it is non-NULL. */
ccb84981 10051
0a3b29ad 10052static tree
4b9b2871 10053cp_parser_function_specifier_opt (cp_parser* parser,
10054 cp_decl_specifier_seq *decl_specs)
0a3b29ad 10055{
ad9ae192 10056 cp_token *token = cp_lexer_peek_token (parser->lexer);
10057 switch (token->keyword)
0a3b29ad 10058 {
10059 case RID_INLINE:
4b9b2871 10060 if (decl_specs)
10061 ++decl_specs->specs[(int) ds_inline];
10062 break;
10063
0a3b29ad 10064 case RID_VIRTUAL:
5f69415d 10065 /* 14.5.2.3 [temp.mem]
074ab442 10066
10067 A member function template shall not be virtual. */
5f69415d 10068 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
ccb59bb6 10069 error_at (token->location, "templates may not be %<virtual%>");
5f69415d 10070 else if (decl_specs)
4b9b2871 10071 ++decl_specs->specs[(int) ds_virtual];
10072 break;
10073
0a3b29ad 10074 case RID_EXPLICIT:
4b9b2871 10075 if (decl_specs)
10076 ++decl_specs->specs[(int) ds_explicit];
10077 break;
0a3b29ad 10078
10079 default:
10080 return NULL_TREE;
10081 }
4b9b2871 10082
10083 /* Consume the token. */
3369eb76 10084 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 10085}
10086
10087/* Parse a linkage-specification.
10088
10089 linkage-specification:
10090 extern string-literal { declaration-seq [opt] }
10091 extern string-literal declaration */
10092
10093static void
45baea8b 10094cp_parser_linkage_specification (cp_parser* parser)
0a3b29ad 10095{
0a3b29ad 10096 tree linkage;
10097
10098 /* Look for the `extern' keyword. */
c247dce0 10099 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
0a3b29ad 10100
00d26680 10101 /* Look for the string-literal. */
10102 linkage = cp_parser_string_literal (parser, false, false);
0a3b29ad 10103
10104 /* Transform the literal into an identifier. If the literal is a
10105 wide-character string, or contains embedded NULs, then we can't
10106 handle it as the user wants. */
00d26680 10107 if (strlen (TREE_STRING_POINTER (linkage))
10108 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
0a3b29ad 10109 {
10110 cp_parser_error (parser, "invalid linkage-specification");
10111 /* Assume C++ linkage. */
00d26680 10112 linkage = lang_name_cplusplus;
0a3b29ad 10113 }
0a3b29ad 10114 else
00d26680 10115 linkage = get_identifier (TREE_STRING_POINTER (linkage));
0a3b29ad 10116
10117 /* We're now using the new linkage. */
10118 push_lang_context (linkage);
10119
10120 /* If the next token is a `{', then we're using the first
10121 production. */
10122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10123 {
10124 /* Consume the `{' token. */
10125 cp_lexer_consume_token (parser->lexer);
10126 /* Parse the declarations. */
10127 cp_parser_declaration_seq_opt (parser);
10128 /* Look for the closing `}'. */
c247dce0 10129 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 10130 }
10131 /* Otherwise, there's just one declaration. */
10132 else
10133 {
10134 bool saved_in_unbraced_linkage_specification_p;
10135
ccb84981 10136 saved_in_unbraced_linkage_specification_p
0a3b29ad 10137 = parser->in_unbraced_linkage_specification_p;
10138 parser->in_unbraced_linkage_specification_p = true;
0a3b29ad 10139 cp_parser_declaration (parser);
ccb84981 10140 parser->in_unbraced_linkage_specification_p
0a3b29ad 10141 = saved_in_unbraced_linkage_specification_p;
10142 }
10143
10144 /* We're done with the linkage-specification. */
10145 pop_lang_context ();
10146}
10147
7a05c4b1 10148/* Parse a static_assert-declaration.
10149
10150 static_assert-declaration:
10151 static_assert ( constant-expression , string-literal ) ;
10152
10153 If MEMBER_P, this static_assert is a class member. */
10154
10155static void
10156cp_parser_static_assert(cp_parser *parser, bool member_p)
10157{
10158 tree condition;
10159 tree message;
10160 cp_token *token;
10161 location_t saved_loc;
10162
10163 /* Peek at the `static_assert' token so we can keep track of exactly
10164 where the static assertion started. */
10165 token = cp_lexer_peek_token (parser->lexer);
10166 saved_loc = token->location;
10167
10168 /* Look for the `static_assert' keyword. */
10169 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
c247dce0 10170 RT_STATIC_ASSERT))
7a05c4b1 10171 return;
10172
10173 /* We know we are in a static assertion; commit to any tentative
10174 parse. */
10175 if (cp_parser_parsing_tentatively (parser))
10176 cp_parser_commit_to_tentative_parse (parser);
10177
10178 /* Parse the `(' starting the static assertion condition. */
c247dce0 10179 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7a05c4b1 10180
10181 /* Parse the constant-expression. */
10182 condition =
10183 cp_parser_constant_expression (parser,
10184 /*allow_non_constant_p=*/false,
10185 /*non_constant_p=*/NULL);
10186
10187 /* Parse the separating `,'. */
c247dce0 10188 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7a05c4b1 10189
10190 /* Parse the string-literal message. */
10191 message = cp_parser_string_literal (parser,
10192 /*translate=*/false,
10193 /*wide_ok=*/true);
10194
10195 /* A `)' completes the static assertion. */
c247dce0 10196 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7a05c4b1 10197 cp_parser_skip_to_closing_parenthesis (parser,
10198 /*recovering=*/true,
10199 /*or_comma=*/false,
10200 /*consume_paren=*/true);
10201
10202 /* A semicolon terminates the declaration. */
c247dce0 10203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7a05c4b1 10204
10205 /* Complete the static assertion, which may mean either processing
10206 the static assert now or saving it for template instantiation. */
10207 finish_static_assert (condition, message, saved_loc, member_p);
10208}
10209
34da8800 10210/* Parse a `decltype' type. Returns the type.
10211
10212 simple-type-specifier:
10213 decltype ( expression ) */
10214
10215static tree
10216cp_parser_decltype (cp_parser *parser)
10217{
10218 tree expr;
10219 bool id_expression_or_member_access_p = false;
10220 const char *saved_message;
10221 bool saved_integral_constant_expression_p;
10222 bool saved_non_integral_constant_expression_p;
ad9ae192 10223 cp_token *id_expr_start_token;
34da8800 10224
10225 /* Look for the `decltype' token. */
c247dce0 10226 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
34da8800 10227 return error_mark_node;
10228
10229 /* Types cannot be defined in a `decltype' expression. Save away the
10230 old message. */
10231 saved_message = parser->type_definition_forbidden_message;
10232
10233 /* And create the new one. */
10234 parser->type_definition_forbidden_message
ca82e026 10235 = G_("types may not be defined in %<decltype%> expressions");
34da8800 10236
10237 /* The restrictions on constant-expressions do not apply inside
10238 decltype expressions. */
10239 saved_integral_constant_expression_p
10240 = parser->integral_constant_expression_p;
10241 saved_non_integral_constant_expression_p
10242 = parser->non_integral_constant_expression_p;
10243 parser->integral_constant_expression_p = false;
10244
10245 /* Do not actually evaluate the expression. */
48d94ede 10246 ++cp_unevaluated_operand;
10247
10248 /* Do not warn about problems with the expression. */
10249 ++c_inhibit_evaluation_warnings;
34da8800 10250
10251 /* Parse the opening `('. */
c247dce0 10252 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
4d3e6d58 10253 return error_mark_node;
34da8800 10254
10255 /* First, try parsing an id-expression. */
ad9ae192 10256 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
34da8800 10257 cp_parser_parse_tentatively (parser);
10258 expr = cp_parser_id_expression (parser,
10259 /*template_keyword_p=*/false,
10260 /*check_dependency_p=*/true,
10261 /*template_p=*/NULL,
10262 /*declarator_p=*/false,
10263 /*optional_p=*/false);
10264
10265 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10266 {
10267 bool non_integral_constant_expression_p = false;
10268 tree id_expression = expr;
10269 cp_id_kind idk;
10270 const char *error_msg;
10271
4d3e6d58 10272 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10273 /* Lookup the name we got back from the id-expression. */
10274 expr = cp_parser_lookup_name (parser, expr,
10275 none_type,
10276 /*is_template=*/false,
10277 /*is_namespace=*/false,
10278 /*check_dependency=*/true,
ad9ae192 10279 /*ambiguous_decls=*/NULL,
10280 id_expr_start_token->location);
4d3e6d58 10281
711178fb 10282 if (expr
34da8800 10283 && expr != error_mark_node
10284 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10285 && TREE_CODE (expr) != TYPE_DECL
711178fb 10286 && (TREE_CODE (expr) != BIT_NOT_EXPR
10287 || !TYPE_P (TREE_OPERAND (expr, 0)))
34da8800 10288 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10289 {
10290 /* Complete lookup of the id-expression. */
10291 expr = (finish_id_expression
10292 (id_expression, expr, parser->scope, &idk,
10293 /*integral_constant_expression_p=*/false,
10294 /*allow_non_integral_constant_expression_p=*/true,
10295 &non_integral_constant_expression_p,
10296 /*template_p=*/false,
10297 /*done=*/true,
10298 /*address_p=*/false,
10299 /*template_arg_p=*/false,
ad9ae192 10300 &error_msg,
10301 id_expr_start_token->location));
34da8800 10302
10303 if (expr == error_mark_node)
10304 /* We found an id-expression, but it was something that we
10305 should not have found. This is an error, not something
10306 we can recover from, so note that we found an
10307 id-expression and we'll recover as gracefully as
10308 possible. */
10309 id_expression_or_member_access_p = true;
10310 }
10311
10312 if (expr
10313 && expr != error_mark_node
10314 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10315 /* We have an id-expression. */
10316 id_expression_or_member_access_p = true;
10317 }
10318
10319 if (!id_expression_or_member_access_p)
10320 {
10321 /* Abort the id-expression parse. */
10322 cp_parser_abort_tentative_parse (parser);
10323
10324 /* Parsing tentatively, again. */
10325 cp_parser_parse_tentatively (parser);
10326
10327 /* Parse a class member access. */
10328 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10329 /*cast_p=*/false,
98b326fd 10330 /*member_access_only_p=*/true, NULL);
34da8800 10331
10332 if (expr
10333 && expr != error_mark_node
10334 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10335 /* We have an id-expression. */
10336 id_expression_or_member_access_p = true;
10337 }
10338
10339 if (id_expression_or_member_access_p)
10340 /* We have parsed the complete id-expression or member access. */
10341 cp_parser_parse_definitely (parser);
10342 else
10343 {
3a39874d 10344 bool saved_greater_than_is_operator_p;
10345
34da8800 10346 /* Abort our attempt to parse an id-expression or member access
10347 expression. */
10348 cp_parser_abort_tentative_parse (parser);
10349
3a39874d 10350 /* Within a parenthesized expression, a `>' token is always
10351 the greater-than operator. */
10352 saved_greater_than_is_operator_p
10353 = parser->greater_than_is_operator_p;
10354 parser->greater_than_is_operator_p = true;
10355
34da8800 10356 /* Parse a full expression. */
98b326fd 10357 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
3a39874d 10358
10359 /* The `>' token might be the end of a template-id or
10360 template-parameter-list now. */
10361 parser->greater_than_is_operator_p
10362 = saved_greater_than_is_operator_p;
34da8800 10363 }
10364
10365 /* Go back to evaluating expressions. */
48d94ede 10366 --cp_unevaluated_operand;
10367 --c_inhibit_evaluation_warnings;
34da8800 10368
10369 /* Restore the old message and the integral constant expression
10370 flags. */
10371 parser->type_definition_forbidden_message = saved_message;
10372 parser->integral_constant_expression_p
10373 = saved_integral_constant_expression_p;
10374 parser->non_integral_constant_expression_p
10375 = saved_non_integral_constant_expression_p;
10376
10377 if (expr == error_mark_node)
10378 {
10379 /* Skip everything up to the closing `)'. */
10380 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10381 /*consume_paren=*/true);
10382 return error_mark_node;
10383 }
10384
10385 /* Parse to the closing `)'. */
c247dce0 10386 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4e761042 10387 {
10388 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10389 /*consume_paren=*/true);
10390 return error_mark_node;
10391 }
34da8800 10392
10393 return finish_decltype_type (expr, id_expression_or_member_access_p);
10394}
10395
0a3b29ad 10396/* Special member functions [gram.special] */
10397
10398/* Parse a conversion-function-id.
10399
10400 conversion-function-id:
ccb84981 10401 operator conversion-type-id
0a3b29ad 10402
10403 Returns an IDENTIFIER_NODE representing the operator. */
10404
ccb84981 10405static tree
45baea8b 10406cp_parser_conversion_function_id (cp_parser* parser)
0a3b29ad 10407{
10408 tree type;
10409 tree saved_scope;
10410 tree saved_qualifying_scope;
10411 tree saved_object_scope;
7f602bca 10412 tree pushed_scope = NULL_TREE;
0a3b29ad 10413
10414 /* Look for the `operator' token. */
c247dce0 10415 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
0a3b29ad 10416 return error_mark_node;
10417 /* When we parse the conversion-type-id, the current scope will be
10418 reset. However, we need that information in able to look up the
10419 conversion function later, so we save it here. */
10420 saved_scope = parser->scope;
10421 saved_qualifying_scope = parser->qualifying_scope;
10422 saved_object_scope = parser->object_scope;
10423 /* We must enter the scope of the class so that the names of
10424 entities declared within the class are available in the
10425 conversion-type-id. For example, consider:
10426
ccb84981 10427 struct S {
653e5405 10428 typedef int I;
0a3b29ad 10429 operator I();
10430 };
10431
10432 S::operator I() { ... }
10433
10434 In order to see that `I' is a type-name in the definition, we
10435 must be in the scope of `S'. */
10436 if (saved_scope)
7f602bca 10437 pushed_scope = push_scope (saved_scope);
0a3b29ad 10438 /* Parse the conversion-type-id. */
10439 type = cp_parser_conversion_type_id (parser);
10440 /* Leave the scope of the class, if any. */
7f602bca 10441 if (pushed_scope)
10442 pop_scope (pushed_scope);
0a3b29ad 10443 /* Restore the saved scope. */
10444 parser->scope = saved_scope;
10445 parser->qualifying_scope = saved_qualifying_scope;
10446 parser->object_scope = saved_object_scope;
10447 /* If the TYPE is invalid, indicate failure. */
10448 if (type == error_mark_node)
10449 return error_mark_node;
10450 return mangle_conv_op_name_for_type (type);
10451}
10452
10453/* Parse a conversion-type-id:
10454
10455 conversion-type-id:
10456 type-specifier-seq conversion-declarator [opt]
10457
10458 Returns the TYPE specified. */
10459
10460static tree
45baea8b 10461cp_parser_conversion_type_id (cp_parser* parser)
0a3b29ad 10462{
10463 tree attributes;
4b9b2871 10464 cp_decl_specifier_seq type_specifiers;
3046c0a3 10465 cp_declarator *declarator;
c37ff371 10466 tree type_specified;
0a3b29ad 10467
10468 /* Parse the attributes. */
10469 attributes = cp_parser_attributes_opt (parser);
10470 /* Parse the type-specifiers. */
c44f7faf 10471 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 10472 /*is_trailing_return=*/false,
6f74fe3c 10473 &type_specifiers);
0a3b29ad 10474 /* If that didn't work, stop. */
4b9b2871 10475 if (type_specifiers.type == error_mark_node)
0a3b29ad 10476 return error_mark_node;
10477 /* Parse the conversion-declarator. */
10478 declarator = cp_parser_conversion_declarator_opt (parser);
10479
c37ff371 10480 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
653e5405 10481 /*initialized=*/0, &attributes);
c37ff371 10482 if (attributes)
10483 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
ddcb49ca 10484
10485 /* Don't give this error when parsing tentatively. This happens to
10486 work because we always parse this definitively once. */
10487 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10488 && type_uses_auto (type_specified))
10489 {
10490 error ("invalid use of %<auto%> in conversion operator");
10491 return error_mark_node;
10492 }
10493
c37ff371 10494 return type_specified;
0a3b29ad 10495}
10496
10497/* Parse an (optional) conversion-declarator.
10498
10499 conversion-declarator:
ccb84981 10500 ptr-operator conversion-declarator [opt]
0a3b29ad 10501
3046c0a3 10502 */
0a3b29ad 10503
3046c0a3 10504static cp_declarator *
45baea8b 10505cp_parser_conversion_declarator_opt (cp_parser* parser)
0a3b29ad 10506{
10507 enum tree_code code;
10508 tree class_type;
2cfb6cde 10509 cp_cv_quals cv_quals;
0a3b29ad 10510
10511 /* We don't know if there's a ptr-operator next, or not. */
10512 cp_parser_parse_tentatively (parser);
10513 /* Try the ptr-operator. */
2cfb6cde 10514 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
0a3b29ad 10515 /* If it worked, look for more conversion-declarators. */
10516 if (cp_parser_parse_definitely (parser))
10517 {
3046c0a3 10518 cp_declarator *declarator;
207355ad 10519
3046c0a3 10520 /* Parse another optional declarator. */
10521 declarator = cp_parser_conversion_declarator_opt (parser);
207355ad 10522
63949b38 10523 return cp_parser_make_indirect_declarator
10524 (code, class_type, cv_quals, declarator);
0a3b29ad 10525 }
10526
3046c0a3 10527 return NULL;
0a3b29ad 10528}
10529
10530/* Parse an (optional) ctor-initializer.
10531
10532 ctor-initializer:
ccb84981 10533 : mem-initializer-list
0a3b29ad 10534
10535 Returns TRUE iff the ctor-initializer was actually present. */
10536
10537static bool
45baea8b 10538cp_parser_ctor_initializer_opt (cp_parser* parser)
0a3b29ad 10539{
10540 /* If the next token is not a `:', then there is no
10541 ctor-initializer. */
10542 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10543 {
10544 /* Do default initialization of any bases and members. */
10545 if (DECL_CONSTRUCTOR_P (current_function_decl))
10546 finish_mem_initializers (NULL_TREE);
10547
10548 return false;
10549 }
10550
10551 /* Consume the `:' token. */
10552 cp_lexer_consume_token (parser->lexer);
10553 /* And the mem-initializer-list. */
10554 cp_parser_mem_initializer_list (parser);
10555
10556 return true;
10557}
10558
10559/* Parse a mem-initializer-list.
10560
10561 mem-initializer-list:
d95d815d 10562 mem-initializer ... [opt]
10563 mem-initializer ... [opt] , mem-initializer-list */
0a3b29ad 10564
10565static void
45baea8b 10566cp_parser_mem_initializer_list (cp_parser* parser)
0a3b29ad 10567{
10568 tree mem_initializer_list = NULL_TREE;
ad9ae192 10569 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 10570
10571 /* Let the semantic analysis code know that we are starting the
10572 mem-initializer-list. */
7e5ca199 10573 if (!DECL_CONSTRUCTOR_P (current_function_decl))
ccb59bb6 10574 error_at (token->location,
d653ac31 10575 "only constructors take member initializers");
0a3b29ad 10576
10577 /* Loop through the list. */
10578 while (true)
10579 {
10580 tree mem_initializer;
10581
ad9ae192 10582 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 10583 /* Parse the mem-initializer. */
10584 mem_initializer = cp_parser_mem_initializer (parser);
d95d815d 10585 /* If the next token is a `...', we're expanding member initializers. */
10586 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10587 {
10588 /* Consume the `...'. */
10589 cp_lexer_consume_token (parser->lexer);
10590
10591 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10592 can be expanded but members cannot. */
10593 if (mem_initializer != error_mark_node
10594 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10595 {
ccb59bb6 10596 error_at (token->location,
10597 "cannot expand initializer for member %<%D%>",
10598 TREE_PURPOSE (mem_initializer));
d95d815d 10599 mem_initializer = error_mark_node;
10600 }
10601
10602 /* Construct the pack expansion type. */
10603 if (mem_initializer != error_mark_node)
10604 mem_initializer = make_pack_expansion (mem_initializer);
10605 }
0a3b29ad 10606 /* Add it to the list, unless it was erroneous. */
7be1bc1f 10607 if (mem_initializer != error_mark_node)
0a3b29ad 10608 {
10609 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10610 mem_initializer_list = mem_initializer;
10611 }
10612 /* If the next token is not a `,', we're done. */
10613 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10614 break;
10615 /* Consume the `,' token. */
10616 cp_lexer_consume_token (parser->lexer);
10617 }
10618
10619 /* Perform semantic analysis. */
7e5ca199 10620 if (DECL_CONSTRUCTOR_P (current_function_decl))
10621 finish_mem_initializers (mem_initializer_list);
0a3b29ad 10622}
10623
10624/* Parse a mem-initializer.
10625
10626 mem-initializer:
ccb84981 10627 mem-initializer-id ( expression-list [opt] )
f82f1250 10628 mem-initializer-id braced-init-list
0a3b29ad 10629
10630 GNU extension:
ccb84981 10631
0a3b29ad 10632 mem-initializer:
755edffd 10633 ( expression-list [opt] )
0a3b29ad 10634
10635 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10636 class) or FIELD_DECL (for a non-static data member) to initialize;
7be1bc1f 10637 the TREE_VALUE is the expression-list. An empty initialization
10638 list is represented by void_list_node. */
0a3b29ad 10639
10640static tree
45baea8b 10641cp_parser_mem_initializer (cp_parser* parser)
0a3b29ad 10642{
10643 tree mem_initializer_id;
10644 tree expression_list;
5f1653d2 10645 tree member;
ad9ae192 10646 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb84981 10647
0a3b29ad 10648 /* Find out what is being initialized. */
10649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10650 {
2b9e3597 10651 permerror (token->location,
10652 "anachronistic old-style base class initializer");
0a3b29ad 10653 mem_initializer_id = NULL_TREE;
10654 }
10655 else
642a3054 10656 {
10657 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10658 if (mem_initializer_id == error_mark_node)
10659 return mem_initializer_id;
10660 }
5f1653d2 10661 member = expand_member_init (mem_initializer_id);
10662 if (member && !DECL_P (member))
10663 in_base_initializer = 1;
0986fa22 10664
f82f1250 10665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10666 {
10667 bool expr_non_constant_p;
bf8d19fe 10668 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 10669 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10670 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10671 expression_list = build_tree_list (NULL_TREE, expression_list);
10672 }
10673 else
f352a3fb 10674 {
10675 VEC(tree,gc)* vec;
33199a81 10676 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 10677 /*cast_p=*/false,
10678 /*allow_expansion_p=*/true,
10679 /*non_constant_p=*/NULL);
10680 if (vec == NULL)
10681 return error_mark_node;
10682 expression_list = build_tree_list_vec (vec);
10683 release_tree_vector (vec);
10684 }
10685
7be1bc1f 10686 if (expression_list == error_mark_node)
10687 return error_mark_node;
0986fa22 10688 if (!expression_list)
0a3b29ad 10689 expression_list = void_type_node;
0a3b29ad 10690
5f1653d2 10691 in_base_initializer = 0;
ccb84981 10692
7be1bc1f 10693 return member ? build_tree_list (member, expression_list) : error_mark_node;
0a3b29ad 10694}
10695
10696/* Parse a mem-initializer-id.
10697
10698 mem-initializer-id:
10699 :: [opt] nested-name-specifier [opt] class-name
ccb84981 10700 identifier
0a3b29ad 10701
10702 Returns a TYPE indicating the class to be initializer for the first
10703 production. Returns an IDENTIFIER_NODE indicating the data member
10704 to be initialized for the second production. */
10705
10706static tree
45baea8b 10707cp_parser_mem_initializer_id (cp_parser* parser)
0a3b29ad 10708{
10709 bool global_scope_p;
10710 bool nested_name_specifier_p;
0b1957b6 10711 bool template_p = false;
0a3b29ad 10712 tree id;
10713
ad9ae192 10714 cp_token *token = cp_lexer_peek_token (parser->lexer);
10715
0b1957b6 10716 /* `typename' is not allowed in this context ([temp.res]). */
10717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10718 {
ccb59bb6 10719 error_at (token->location,
10720 "keyword %<typename%> not allowed in this context (a qualified "
10721 "member initializer is implicitly a type)");
0b1957b6 10722 cp_lexer_consume_token (parser->lexer);
10723 }
0a3b29ad 10724 /* Look for the optional `::' operator. */
ccb84981 10725 global_scope_p
10726 = (cp_parser_global_scope_opt (parser,
130bb1d4 10727 /*current_scope_valid_p=*/false)
0a3b29ad 10728 != NULL_TREE);
10729 /* Look for the optional nested-name-specifier. The simplest way to
10730 implement:
10731
10732 [temp.res]
10733
10734 The keyword `typename' is not permitted in a base-specifier or
10735 mem-initializer; in these contexts a qualified name that
10736 depends on a template-parameter is implicitly assumed to be a
10737 type name.
10738
10739 is to assume that we have seen the `typename' keyword at this
10740 point. */
ccb84981 10741 nested_name_specifier_p
0a3b29ad 10742 = (cp_parser_nested_name_specifier_opt (parser,
10743 /*typename_keyword_p=*/true,
10744 /*check_dependency_p=*/true,
3d0f901b 10745 /*type_p=*/true,
10746 /*is_declaration=*/true)
0a3b29ad 10747 != NULL_TREE);
0b1957b6 10748 if (nested_name_specifier_p)
10749 template_p = cp_parser_optional_template_keyword (parser);
0a3b29ad 10750 /* If there is a `::' operator or a nested-name-specifier, then we
10751 are definitely looking for a class-name. */
10752 if (global_scope_p || nested_name_specifier_p)
10753 return cp_parser_class_name (parser,
10754 /*typename_keyword_p=*/true,
0b1957b6 10755 /*template_keyword_p=*/template_p,
5570fae0 10756 typename_type,
0a3b29ad 10757 /*check_dependency_p=*/true,
3d0f901b 10758 /*class_head_p=*/false,
10759 /*is_declaration=*/true);
0a3b29ad 10760 /* Otherwise, we could also be looking for an ordinary identifier. */
10761 cp_parser_parse_tentatively (parser);
10762 /* Try a class-name. */
ccb84981 10763 id = cp_parser_class_name (parser,
0a3b29ad 10764 /*typename_keyword_p=*/true,
10765 /*template_keyword_p=*/false,
e2ae55f2 10766 none_type,
0a3b29ad 10767 /*check_dependency_p=*/true,
3d0f901b 10768 /*class_head_p=*/false,
10769 /*is_declaration=*/true);
0a3b29ad 10770 /* If we found one, we're done. */
10771 if (cp_parser_parse_definitely (parser))
10772 return id;
10773 /* Otherwise, look for an ordinary identifier. */
10774 return cp_parser_identifier (parser);
10775}
10776
10777/* Overloading [gram.over] */
10778
10779/* Parse an operator-function-id.
10780
10781 operator-function-id:
ccb84981 10782 operator operator
0a3b29ad 10783
10784 Returns an IDENTIFIER_NODE for the operator which is a
10785 human-readable spelling of the identifier, e.g., `operator +'. */
10786
ccb84981 10787static tree
45baea8b 10788cp_parser_operator_function_id (cp_parser* parser)
0a3b29ad 10789{
10790 /* Look for the `operator' keyword. */
c247dce0 10791 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
0a3b29ad 10792 return error_mark_node;
10793 /* And then the name of the operator itself. */
10794 return cp_parser_operator (parser);
10795}
10796
10797/* Parse an operator.
10798
10799 operator:
10800 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10801 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10802 || ++ -- , ->* -> () []
10803
10804 GNU Extensions:
ccb84981 10805
0a3b29ad 10806 operator:
10807 <? >? <?= >?=
10808
10809 Returns an IDENTIFIER_NODE for the operator which is a
10810 human-readable spelling of the identifier, e.g., `operator +'. */
ccb84981 10811
0a3b29ad 10812static tree
45baea8b 10813cp_parser_operator (cp_parser* parser)
0a3b29ad 10814{
10815 tree id = NULL_TREE;
10816 cp_token *token;
10817
10818 /* Peek at the next token. */
10819 token = cp_lexer_peek_token (parser->lexer);
10820 /* Figure out which operator we have. */
10821 switch (token->type)
10822 {
10823 case CPP_KEYWORD:
10824 {
10825 enum tree_code op;
10826
10827 /* The keyword should be either `new' or `delete'. */
10828 if (token->keyword == RID_NEW)
10829 op = NEW_EXPR;
10830 else if (token->keyword == RID_DELETE)
10831 op = DELETE_EXPR;
10832 else
10833 break;
10834
10835 /* Consume the `new' or `delete' token. */
10836 cp_lexer_consume_token (parser->lexer);
10837
10838 /* Peek at the next token. */
10839 token = cp_lexer_peek_token (parser->lexer);
10840 /* If it's a `[' token then this is the array variant of the
10841 operator. */
10842 if (token->type == CPP_OPEN_SQUARE)
10843 {
10844 /* Consume the `[' token. */
10845 cp_lexer_consume_token (parser->lexer);
10846 /* Look for the `]' token. */
c247dce0 10847 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
ccb84981 10848 id = ansi_opname (op == NEW_EXPR
0a3b29ad 10849 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10850 }
10851 /* Otherwise, we have the non-array variant. */
10852 else
10853 id = ansi_opname (op);
10854
10855 return id;
10856 }
10857
10858 case CPP_PLUS:
10859 id = ansi_opname (PLUS_EXPR);
10860 break;
10861
10862 case CPP_MINUS:
10863 id = ansi_opname (MINUS_EXPR);
10864 break;
10865
10866 case CPP_MULT:
10867 id = ansi_opname (MULT_EXPR);
10868 break;
10869
10870 case CPP_DIV:
10871 id = ansi_opname (TRUNC_DIV_EXPR);
10872 break;
10873
10874 case CPP_MOD:
10875 id = ansi_opname (TRUNC_MOD_EXPR);
10876 break;
10877
10878 case CPP_XOR:
10879 id = ansi_opname (BIT_XOR_EXPR);
10880 break;
10881
10882 case CPP_AND:
10883 id = ansi_opname (BIT_AND_EXPR);
10884 break;
10885
10886 case CPP_OR:
10887 id = ansi_opname (BIT_IOR_EXPR);
10888 break;
10889
10890 case CPP_COMPL:
10891 id = ansi_opname (BIT_NOT_EXPR);
10892 break;
ccb84981 10893
0a3b29ad 10894 case CPP_NOT:
10895 id = ansi_opname (TRUTH_NOT_EXPR);
10896 break;
10897
10898 case CPP_EQ:
10899 id = ansi_assopname (NOP_EXPR);
10900 break;
10901
10902 case CPP_LESS:
10903 id = ansi_opname (LT_EXPR);
10904 break;
10905
10906 case CPP_GREATER:
10907 id = ansi_opname (GT_EXPR);
10908 break;
10909
10910 case CPP_PLUS_EQ:
10911 id = ansi_assopname (PLUS_EXPR);
10912 break;
10913
10914 case CPP_MINUS_EQ:
10915 id = ansi_assopname (MINUS_EXPR);
10916 break;
10917
10918 case CPP_MULT_EQ:
10919 id = ansi_assopname (MULT_EXPR);
10920 break;
10921
10922 case CPP_DIV_EQ:
10923 id = ansi_assopname (TRUNC_DIV_EXPR);
10924 break;
10925
10926 case CPP_MOD_EQ:
10927 id = ansi_assopname (TRUNC_MOD_EXPR);
10928 break;
10929
10930 case CPP_XOR_EQ:
10931 id = ansi_assopname (BIT_XOR_EXPR);
10932 break;
10933
10934 case CPP_AND_EQ:
10935 id = ansi_assopname (BIT_AND_EXPR);
10936 break;
10937
10938 case CPP_OR_EQ:
10939 id = ansi_assopname (BIT_IOR_EXPR);
10940 break;
10941
10942 case CPP_LSHIFT:
10943 id = ansi_opname (LSHIFT_EXPR);
10944 break;
10945
10946 case CPP_RSHIFT:
10947 id = ansi_opname (RSHIFT_EXPR);
10948 break;
10949
10950 case CPP_LSHIFT_EQ:
10951 id = ansi_assopname (LSHIFT_EXPR);
10952 break;
10953
10954 case CPP_RSHIFT_EQ:
10955 id = ansi_assopname (RSHIFT_EXPR);
10956 break;
10957
10958 case CPP_EQ_EQ:
10959 id = ansi_opname (EQ_EXPR);
10960 break;
10961
10962 case CPP_NOT_EQ:
10963 id = ansi_opname (NE_EXPR);
10964 break;
10965
10966 case CPP_LESS_EQ:
10967 id = ansi_opname (LE_EXPR);
10968 break;
10969
10970 case CPP_GREATER_EQ:
10971 id = ansi_opname (GE_EXPR);
10972 break;
10973
10974 case CPP_AND_AND:
10975 id = ansi_opname (TRUTH_ANDIF_EXPR);
10976 break;
10977
10978 case CPP_OR_OR:
10979 id = ansi_opname (TRUTH_ORIF_EXPR);
10980 break;
ccb84981 10981
0a3b29ad 10982 case CPP_PLUS_PLUS:
10983 id = ansi_opname (POSTINCREMENT_EXPR);
10984 break;
10985
10986 case CPP_MINUS_MINUS:
10987 id = ansi_opname (PREDECREMENT_EXPR);
10988 break;
10989
10990 case CPP_COMMA:
10991 id = ansi_opname (COMPOUND_EXPR);
10992 break;
10993
10994 case CPP_DEREF_STAR:
10995 id = ansi_opname (MEMBER_REF);
10996 break;
10997
10998 case CPP_DEREF:
10999 id = ansi_opname (COMPONENT_REF);
11000 break;
11001
11002 case CPP_OPEN_PAREN:
11003 /* Consume the `('. */
11004 cp_lexer_consume_token (parser->lexer);
11005 /* Look for the matching `)'. */
c247dce0 11006 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 11007 return ansi_opname (CALL_EXPR);
11008
11009 case CPP_OPEN_SQUARE:
11010 /* Consume the `['. */
11011 cp_lexer_consume_token (parser->lexer);
11012 /* Look for the matching `]'. */
c247dce0 11013 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 11014 return ansi_opname (ARRAY_REF);
11015
0a3b29ad 11016 default:
11017 /* Anything else is an error. */
11018 break;
11019 }
11020
11021 /* If we have selected an identifier, we need to consume the
11022 operator token. */
11023 if (id)
11024 cp_lexer_consume_token (parser->lexer);
11025 /* Otherwise, no valid operator name was present. */
11026 else
11027 {
11028 cp_parser_error (parser, "expected operator");
11029 id = error_mark_node;
11030 }
11031
11032 return id;
11033}
11034
11035/* Parse a template-declaration.
11036
11037 template-declaration:
ccb84981 11038 export [opt] template < template-parameter-list > declaration
0a3b29ad 11039
11040 If MEMBER_P is TRUE, this template-declaration occurs within a
ccb84981 11041 class-specifier.
0a3b29ad 11042
11043 The grammar rule given by the standard isn't correct. What
11044 is really meant is:
11045
11046 template-declaration:
ccb84981 11047 export [opt] template-parameter-list-seq
0a3b29ad 11048 decl-specifier-seq [opt] init-declarator [opt] ;
ccb84981 11049 export [opt] template-parameter-list-seq
0a3b29ad 11050 function-definition
11051
11052 template-parameter-list-seq:
11053 template-parameter-list-seq [opt]
11054 template < template-parameter-list > */
11055
11056static void
45baea8b 11057cp_parser_template_declaration (cp_parser* parser, bool member_p)
0a3b29ad 11058{
11059 /* Check for `export'. */
11060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11061 {
11062 /* Consume the `export' token. */
11063 cp_lexer_consume_token (parser->lexer);
11064 /* Warn that we do not support `export'. */
c3ceba8e 11065 warning (0, "keyword %<export%> not implemented, and will be ignored");
0a3b29ad 11066 }
11067
11068 cp_parser_template_declaration_after_export (parser, member_p);
11069}
11070
11071/* Parse a template-parameter-list.
11072
11073 template-parameter-list:
11074 template-parameter
11075 template-parameter-list , template-parameter
11076
11077 Returns a TREE_LIST. Each node represents a template parameter.
11078 The nodes are connected via their TREE_CHAINs. */
11079
11080static tree
45baea8b 11081cp_parser_template_parameter_list (cp_parser* parser)
0a3b29ad 11082{
11083 tree parameter_list = NULL_TREE;
11084
7be1bc1f 11085 begin_template_parm_list ();
0d432ee0 11086
11087 /* The loop below parses the template parms. We first need to know
11088 the total number of template parms to be able to compute proper
11089 canonical types of each dependent type. So after the loop, when
11090 we know the total number of template parms,
11091 end_template_parm_list computes the proper canonical types and
11092 fixes up the dependent types accordingly. */
0a3b29ad 11093 while (true)
11094 {
11095 tree parameter;
3046c0a3 11096 bool is_non_type;
d95d815d 11097 bool is_parameter_pack;
e60a6f7b 11098 location_t parm_loc;
0a3b29ad 11099
11100 /* Parse the template-parameter. */
e60a6f7b 11101 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
d95d815d 11102 parameter = cp_parser_template_parameter (parser,
11103 &is_non_type,
11104 &is_parameter_pack);
0a3b29ad 11105 /* Add it to the list. */
8f776a97 11106 if (parameter != error_mark_node)
11107 parameter_list = process_template_parm (parameter_list,
e60a6f7b 11108 parm_loc,
8f776a97 11109 parameter,
d95d815d 11110 is_non_type,
0d432ee0 11111 is_parameter_pack,
11112 0);
e347c61e 11113 else
11114 {
11115 tree err_parm = build_tree_list (parameter, parameter);
e347c61e 11116 parameter_list = chainon (parameter_list, err_parm);
11117 }
11118
f5b66d53 11119 /* If the next token is not a `,', we're done. */
11120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 11121 break;
11122 /* Otherwise, consume the `,' token. */
11123 cp_lexer_consume_token (parser->lexer);
11124 }
11125
7be1bc1f 11126 return end_template_parm_list (parameter_list);
0a3b29ad 11127}
11128
11129/* Parse a template-parameter.
11130
11131 template-parameter:
11132 type-parameter
11133 parameter-declaration
11134
8f776a97 11135 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11136 the parameter. The TREE_PURPOSE is the default value, if any.
11137 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
d95d815d 11138 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11139 set to true iff this parameter is a parameter pack. */
0a3b29ad 11140
11141static tree
d95d815d 11142cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11143 bool *is_parameter_pack)
0a3b29ad 11144{
11145 cp_token *token;
4b9b2871 11146 cp_parameter_declarator *parameter_declarator;
4efde0d3 11147 cp_declarator *id_declarator;
8f776a97 11148 tree parm;
0a3b29ad 11149
3046c0a3 11150 /* Assume it is a type parameter or a template parameter. */
11151 *is_non_type = false;
d95d815d 11152 /* Assume it not a parameter pack. */
11153 *is_parameter_pack = false;
0a3b29ad 11154 /* Peek at the next token. */
11155 token = cp_lexer_peek_token (parser->lexer);
11156 /* If it is `class' or `template', we have a type-parameter. */
11157 if (token->keyword == RID_TEMPLATE)
d95d815d 11158 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 11159 /* If it is `class' or `typename' we do not know yet whether it is a
11160 type parameter or a non-type parameter. Consider:
11161
11162 template <typename T, typename T::X X> ...
11163
11164 or:
ccb84981 11165
0a3b29ad 11166 template <class C, class D*> ...
11167
11168 Here, the first parameter is a type parameter, and the second is
11169 a non-type parameter. We can tell by looking at the token after
11170 the identifier -- if it is a `,', `=', or `>' then we have a type
11171 parameter. */
11172 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11173 {
11174 /* Peek at the token after `class' or `typename'. */
11175 token = cp_lexer_peek_nth_token (parser->lexer, 2);
d95d815d 11176 /* If it's an ellipsis, we have a template type parameter
11177 pack. */
11178 if (token->type == CPP_ELLIPSIS)
11179 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 11180 /* If it's an identifier, skip it. */
11181 if (token->type == CPP_NAME)
11182 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11183 /* Now, see if the token looks like the end of a template
11184 parameter. */
ccb84981 11185 if (token->type == CPP_COMMA
0a3b29ad 11186 || token->type == CPP_EQ
11187 || token->type == CPP_GREATER)
d95d815d 11188 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 11189 }
11190
ccb84981 11191 /* Otherwise, it is a non-type parameter.
0a3b29ad 11192
11193 [temp.param]
11194
11195 When parsing a default template-argument for a non-type
11196 template-parameter, the first non-nested `>' is taken as the end
11197 of the template parameter-list rather than a greater-than
11198 operator. */
3046c0a3 11199 *is_non_type = true;
11200 parameter_declarator
11201 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11202 /*parenthesized_p=*/NULL);
d95d815d 11203
11204 /* If the parameter declaration is marked as a parameter pack, set
11205 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11206 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11207 grokdeclarator. */
11208 if (parameter_declarator
11209 && parameter_declarator->declarator
11210 && parameter_declarator->declarator->parameter_pack_p)
11211 {
11212 *is_parameter_pack = true;
11213 parameter_declarator->declarator->parameter_pack_p = false;
11214 }
11215
11216 /* If the next token is an ellipsis, and we don't already have it
11217 marked as a parameter pack, then we have a parameter pack (that
41341abd 11218 has no declarator). */
d95d815d 11219 if (!*is_parameter_pack
2aedc2ff 11220 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11221 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
d95d815d 11222 {
41341abd 11223 /* Consume the `...'. */
d95d815d 11224 cp_lexer_consume_token (parser->lexer);
11225 maybe_warn_variadic_templates ();
11226
11227 *is_parameter_pack = true;
83b01f73 11228 }
11229 /* We might end up with a pack expansion as the type of the non-type
11230 template parameter, in which case this is a non-type template
11231 parameter pack. */
11232 else if (parameter_declarator
11233 && parameter_declarator->decl_specifiers.type
11234 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11235 {
11236 *is_parameter_pack = true;
11237 parameter_declarator->decl_specifiers.type =
11238 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11239 }
41341abd 11240
83b01f73 11241 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11242 {
41341abd 11243 /* Parameter packs cannot have default arguments. However, a
11244 user may try to do so, so we'll parse them and give an
11245 appropriate diagnostic here. */
41341abd 11246
83b01f73 11247 /* Consume the `='. */
ad9ae192 11248 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
83b01f73 11249 cp_lexer_consume_token (parser->lexer);
11250
11251 /* Find the name of the parameter pack. */
11252 id_declarator = parameter_declarator->declarator;
11253 while (id_declarator && id_declarator->kind != cdk_id)
11254 id_declarator = id_declarator->declarator;
11255
11256 if (id_declarator && id_declarator->kind == cdk_id)
ccb59bb6 11257 error_at (start_token->location,
11258 "template parameter pack %qD cannot have a default argument",
11259 id_declarator->u.id.unqualified_name);
83b01f73 11260 else
ccb59bb6 11261 error_at (start_token->location,
11262 "template parameter pack cannot have a default argument");
83b01f73 11263
11264 /* Parse the default argument, but throw away the result. */
11265 cp_parser_default_argument (parser, /*template_parm_p=*/true);
d95d815d 11266 }
11267
8f776a97 11268 parm = grokdeclarator (parameter_declarator->declarator,
11269 &parameter_declarator->decl_specifiers,
3b901c35 11270 TPARM, /*initialized=*/0,
8f776a97 11271 /*attrlist=*/NULL);
11272 if (parm == error_mark_node)
11273 return error_mark_node;
d95d815d 11274
8f776a97 11275 return build_tree_list (parameter_declarator->default_argument, parm);
0a3b29ad 11276}
11277
11278/* Parse a type-parameter.
11279
11280 type-parameter:
11281 class identifier [opt]
11282 class identifier [opt] = type-id
11283 typename identifier [opt]
11284 typename identifier [opt] = type-id
11285 template < template-parameter-list > class identifier [opt]
ccb84981 11286 template < template-parameter-list > class identifier [opt]
11287 = id-expression
0a3b29ad 11288
d95d815d 11289 GNU Extension (variadic templates):
11290
11291 type-parameter:
11292 class ... identifier [opt]
11293 typename ... identifier [opt]
11294
0a3b29ad 11295 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11296 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
d95d815d 11297 the declaration of the parameter.
11298
11299 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
0a3b29ad 11300
11301static tree
d95d815d 11302cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
0a3b29ad 11303{
11304 cp_token *token;
11305 tree parameter;
11306
11307 /* Look for a keyword to tell us what kind of parameter this is. */
c247dce0 11308 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
0a3b29ad 11309 if (!token)
11310 return error_mark_node;
11311
11312 switch (token->keyword)
11313 {
11314 case RID_CLASS:
11315 case RID_TYPENAME:
11316 {
11317 tree identifier;
11318 tree default_argument;
11319
d95d815d 11320 /* If the next token is an ellipsis, we have a template
11321 argument pack. */
11322 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11323 {
11324 /* Consume the `...' token. */
11325 cp_lexer_consume_token (parser->lexer);
11326 maybe_warn_variadic_templates ();
11327
11328 *is_parameter_pack = true;
11329 }
11330
0a3b29ad 11331 /* If the next token is an identifier, then it names the
653e5405 11332 parameter. */
0a3b29ad 11333 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11334 identifier = cp_parser_identifier (parser);
11335 else
11336 identifier = NULL_TREE;
11337
11338 /* Create the parameter. */
11339 parameter = finish_template_type_parm (class_type_node, identifier);
11340
11341 /* If the next token is an `=', we have a default argument. */
11342 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11343 {
11344 /* Consume the `=' token. */
11345 cp_lexer_consume_token (parser->lexer);
755edffd 11346 /* Parse the default-argument. */
23010bc8 11347 push_deferring_access_checks (dk_no_deferred);
0a3b29ad 11348 default_argument = cp_parser_type_id (parser);
d95d815d 11349
11350 /* Template parameter packs cannot have default
11351 arguments. */
11352 if (*is_parameter_pack)
11353 {
11354 if (identifier)
ccb59bb6 11355 error_at (token->location,
11356 "template parameter pack %qD cannot have a "
11357 "default argument", identifier);
d95d815d 11358 else
ccb59bb6 11359 error_at (token->location,
11360 "template parameter packs cannot have "
11361 "default arguments");
d95d815d 11362 default_argument = NULL_TREE;
11363 }
23010bc8 11364 pop_deferring_access_checks ();
0a3b29ad 11365 }
11366 else
11367 default_argument = NULL_TREE;
11368
11369 /* Create the combined representation of the parameter and the
11370 default argument. */
816786ad 11371 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 11372 }
11373 break;
11374
11375 case RID_TEMPLATE:
11376 {
0a3b29ad 11377 tree identifier;
11378 tree default_argument;
11379
11380 /* Look for the `<'. */
c247dce0 11381 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 11382 /* Parse the template-parameter-list. */
8e9e8d76 11383 cp_parser_template_parameter_list (parser);
0a3b29ad 11384 /* Look for the `>'. */
c247dce0 11385 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 11386 /* Look for the `class' keyword. */
c247dce0 11387 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
d95d815d 11388 /* If the next token is an ellipsis, we have a template
11389 argument pack. */
11390 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11391 {
11392 /* Consume the `...' token. */
11393 cp_lexer_consume_token (parser->lexer);
11394 maybe_warn_variadic_templates ();
11395
11396 *is_parameter_pack = true;
11397 }
0a3b29ad 11398 /* If the next token is an `=', then there is a
11399 default-argument. If the next token is a `>', we are at
11400 the end of the parameter-list. If the next token is a `,',
11401 then we are at the end of this parameter. */
11402 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11403 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11404 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
2a03dcc3 11405 {
11406 identifier = cp_parser_identifier (parser);
93523877 11407 /* Treat invalid names as if the parameter were nameless. */
2a03dcc3 11408 if (identifier == error_mark_node)
11409 identifier = NULL_TREE;
11410 }
0a3b29ad 11411 else
11412 identifier = NULL_TREE;
2a03dcc3 11413
0a3b29ad 11414 /* Create the template parameter. */
11415 parameter = finish_template_template_parm (class_type_node,
11416 identifier);
ccb84981 11417
0a3b29ad 11418 /* If the next token is an `=', then there is a
11419 default-argument. */
11420 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11421 {
c3b9e457 11422 bool is_template;
11423
0a3b29ad 11424 /* Consume the `='. */
11425 cp_lexer_consume_token (parser->lexer);
11426 /* Parse the id-expression. */
23010bc8 11427 push_deferring_access_checks (dk_no_deferred);
ad9ae192 11428 /* save token before parsing the id-expression, for error
11429 reporting */
11430 token = cp_lexer_peek_token (parser->lexer);
ccb84981 11431 default_argument
0a3b29ad 11432 = cp_parser_id_expression (parser,
11433 /*template_keyword_p=*/false,
11434 /*check_dependency_p=*/true,
c3b9e457 11435 /*template_p=*/&is_template,
197c9df7 11436 /*declarator_p=*/false,
130bb1d4 11437 /*optional_p=*/false);
eae805b4 11438 if (TREE_CODE (default_argument) == TYPE_DECL)
11439 /* If the id-expression was a template-id that refers to
11440 a template-class, we already have the declaration here,
11441 so no further lookup is needed. */
11442 ;
11443 else
11444 /* Look up the name. */
ccb84981 11445 default_argument
eae805b4 11446 = cp_parser_lookup_name (parser, default_argument,
e2ae55f2 11447 none_type,
11448 /*is_template=*/is_template,
11449 /*is_namespace=*/false,
11450 /*check_dependency=*/true,
ad9ae192 11451 /*ambiguous_decls=*/NULL,
11452 token->location);
0a3b29ad 11453 /* See if the default argument is valid. */
11454 default_argument
11455 = check_template_template_default_arg (default_argument);
d95d815d 11456
11457 /* Template parameter packs cannot have default
11458 arguments. */
11459 if (*is_parameter_pack)
11460 {
11461 if (identifier)
ccb59bb6 11462 error_at (token->location,
11463 "template parameter pack %qD cannot "
11464 "have a default argument",
11465 identifier);
d95d815d 11466 else
ccb59bb6 11467 error_at (token->location, "template parameter packs cannot "
11468 "have default arguments");
d95d815d 11469 default_argument = NULL_TREE;
11470 }
23010bc8 11471 pop_deferring_access_checks ();
0a3b29ad 11472 }
11473 else
11474 default_argument = NULL_TREE;
11475
11476 /* Create the combined representation of the parameter and the
11477 default argument. */
2a03dcc3 11478 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 11479 }
11480 break;
11481
11482 default:
2a03dcc3 11483 gcc_unreachable ();
11484 break;
0a3b29ad 11485 }
ccb84981 11486
0a3b29ad 11487 return parameter;
11488}
11489
11490/* Parse a template-id.
11491
11492 template-id:
11493 template-name < template-argument-list [opt] >
11494
11495 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11496 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11497 returned. Otherwise, if the template-name names a function, or set
11498 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
ccb84981 11499 names a class, returns a TYPE_DECL for the specialization.
0a3b29ad 11500
11501 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11502 uninstantiated templates. */
11503
11504static tree
ccb84981 11505cp_parser_template_id (cp_parser *parser,
11506 bool template_keyword_p,
3d0f901b 11507 bool check_dependency_p,
11508 bool is_declaration)
0a3b29ad 11509{
3369eb76 11510 int i;
607a5d68 11511 tree templ;
0a3b29ad 11512 tree arguments;
0a3b29ad 11513 tree template_id;
19273cc2 11514 cp_token_position start_of_id = 0;
3369eb76 11515 deferred_access_check *chk;
11516 VEC (deferred_access_check,gc) *access_check;
8e9e8d76 11517 cp_token *next_token = NULL, *next_token_2 = NULL;
3d0f901b 11518 bool is_identifier;
0a3b29ad 11519
11520 /* If the next token corresponds to a template-id, there is no need
11521 to reparse it. */
b3c48b5d 11522 next_token = cp_lexer_peek_token (parser->lexer);
11523 if (next_token->type == CPP_TEMPLATE_ID)
0a3b29ad 11524 {
3369eb76 11525 struct tree_check *check_value;
0a3b29ad 11526
11527 /* Get the stored value. */
3369eb76 11528 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
0a3b29ad 11529 /* Perform any access checks that were deferred. */
3369eb76 11530 access_check = check_value->checks;
11531 if (access_check)
11532 {
48148244 11533 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11534 perform_or_defer_access_check (chk->binfo,
11535 chk->decl,
11536 chk->diag_decl);
3369eb76 11537 }
0a3b29ad 11538 /* Return the stored value. */
3369eb76 11539 return check_value->value;
0a3b29ad 11540 }
11541
b3c48b5d 11542 /* Avoid performing name lookup if there is no possibility of
11543 finding a template-id. */
11544 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11545 || (next_token->type == CPP_NAME
ccb84981 11546 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 11547 (parser, 2)))
b3c48b5d 11548 {
11549 cp_parser_error (parser, "expected template-id");
11550 return error_mark_node;
11551 }
11552
0a3b29ad 11553 /* Remember where the template-id starts. */
efcbcf83 11554 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 11555 start_of_id = cp_lexer_token_position (parser->lexer, false);
0a3b29ad 11556
4f62c42e 11557 push_deferring_access_checks (dk_deferred);
9b57b06b 11558
0a3b29ad 11559 /* Parse the template-name. */
3d0f901b 11560 is_identifier = false;
607a5d68 11561 templ = cp_parser_template_name (parser, template_keyword_p,
11562 check_dependency_p,
11563 is_declaration,
11564 &is_identifier);
11565 if (templ == error_mark_node || is_identifier)
9b57b06b 11566 {
11567 pop_deferring_access_checks ();
607a5d68 11568 return templ;
9b57b06b 11569 }
0a3b29ad 11570
ccb84981 11571 /* If we find the sequence `[:' after a template-name, it's probably
c8d5ab79 11572 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11573 parse correctly the argument list. */
b9dd3954 11574 next_token = cp_lexer_peek_token (parser->lexer);
c8d5ab79 11575 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
ccb84981 11576 if (next_token->type == CPP_OPEN_SQUARE
c8d5ab79 11577 && next_token->flags & DIGRAPH
ccb84981 11578 && next_token_2->type == CPP_COLON
c8d5ab79 11579 && !(next_token_2->flags & PREV_WHITE))
9b57b06b 11580 {
c8d5ab79 11581 cp_parser_parse_tentatively (parser);
11582 /* Change `:' into `::'. */
11583 next_token_2->type = CPP_SCOPE;
11584 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
653e5405 11585 CPP_LESS. */
c8d5ab79 11586 cp_lexer_consume_token (parser->lexer);
ad9ae192 11587
c8d5ab79 11588 /* Parse the arguments. */
11589 arguments = cp_parser_enclosed_template_argument_list (parser);
11590 if (!cp_parser_parse_definitely (parser))
11591 {
11592 /* If we couldn't parse an argument list, then we revert our changes
11593 and return simply an error. Maybe this is not a template-id
11594 after all. */
11595 next_token_2->type = CPP_COLON;
a2c5b975 11596 cp_parser_error (parser, "expected %<<%>");
c8d5ab79 11597 pop_deferring_access_checks ();
11598 return error_mark_node;
11599 }
11600 /* Otherwise, emit an error about the invalid digraph, but continue
653e5405 11601 parsing because we got our argument list. */
2b9e3597 11602 if (permerror (next_token->location,
11603 "%<<::%> cannot begin a template-argument list"))
a52d5726 11604 {
11605 static bool hint = false;
5bcc316e 11606 inform (next_token->location,
11607 "%<<:%> is an alternate spelling for %<[%>."
11608 " Insert whitespace between %<<%> and %<::%>");
a52d5726 11609 if (!hint && !flag_permissive)
c8d5ab79 11610 {
5bcc316e 11611 inform (next_token->location, "(if you use %<-fpermissive%>"
11612 " G++ will accept your code)");
c8d5ab79 11613 hint = true;
11614 }
11615 }
11616 }
11617 else
11618 {
11619 /* Look for the `<' that starts the template-argument-list. */
c247dce0 11620 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
c8d5ab79 11621 {
11622 pop_deferring_access_checks ();
11623 return error_mark_node;
11624 }
11625 /* Parse the arguments. */
11626 arguments = cp_parser_enclosed_template_argument_list (parser);
9b57b06b 11627 }
0a3b29ad 11628
11629 /* Build a representation of the specialization. */
607a5d68 11630 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11631 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11632 else if (DECL_CLASS_TEMPLATE_P (templ)
11633 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
ba0c587d 11634 {
11635 bool entering_scope;
11636 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11637 template (rather than some instantiation thereof) only if
11638 is not nested within some other construct. For example, in
11639 "template <typename T> void f(T) { A<T>::", A<T> is just an
11640 instantiation of A. */
11641 entering_scope = (template_parm_scope_p ()
11642 && cp_lexer_next_token_is (parser->lexer,
11643 CPP_SCOPE));
11644 template_id
607a5d68 11645 = finish_template_type (templ, arguments, entering_scope);
ba0c587d 11646 }
0a3b29ad 11647 else
11648 {
11649 /* If it's not a class-template or a template-template, it should be
11650 a function-template. */
607a5d68 11651 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11652 || TREE_CODE (templ) == OVERLOAD
11653 || BASELINK_P (templ)));
ccb84981 11654
607a5d68 11655 template_id = lookup_template_function (templ, arguments);
0a3b29ad 11656 }
ccb84981 11657
0a3b29ad 11658 /* If parsing tentatively, replace the sequence of tokens that makes
11659 up the template-id with a CPP_TEMPLATE_ID token. That way,
11660 should we re-parse the token stream, we will not have to repeat
11661 the effort required to do the parse, nor will we issue duplicate
11662 error messages about problems during instantiation of the
459742a6 11663 template. */
67635103 11664 if (start_of_id)
0a3b29ad 11665 {
19273cc2 11666 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9031d10b 11667
0a3b29ad 11668 /* Reset the contents of the START_OF_ID token. */
11669 token->type = CPP_TEMPLATE_ID;
3369eb76 11670 /* Retrieve any deferred checks. Do not pop this access checks yet
11671 so the memory will not be reclaimed during token replacing below. */
ba72912a 11672 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
3369eb76 11673 token->u.tree_check_value->value = template_id;
11674 token->u.tree_check_value->checks = get_deferred_access_checks ();
0a3b29ad 11675 token->keyword = RID_MAX;
9031d10b 11676
0a3b29ad 11677 /* Purge all subsequent tokens. */
19273cc2 11678 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
67635103 11679
11680 /* ??? Can we actually assume that, if template_id ==
11681 error_mark_node, we will have issued a diagnostic to the
11682 user, as opposed to simply marking the tentative parse as
11683 failed? */
11684 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
ccb59bb6 11685 error_at (token->location, "parse error in template argument list");
0a3b29ad 11686 }
11687
9b57b06b 11688 pop_deferring_access_checks ();
0a3b29ad 11689 return template_id;
11690}
11691
11692/* Parse a template-name.
11693
11694 template-name:
11695 identifier
ccb84981 11696
0a3b29ad 11697 The standard should actually say:
11698
11699 template-name:
11700 identifier
11701 operator-function-id
0a3b29ad 11702
11703 A defect report has been filed about this issue.
11704
182d094e 11705 A conversion-function-id cannot be a template name because they cannot
11706 be part of a template-id. In fact, looking at this code:
11707
11708 a.operator K<int>()
11709
11710 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
ccb84981 11711 It is impossible to call a templated conversion-function-id with an
182d094e 11712 explicit argument list, since the only allowed template parameter is
11713 the type to which it is converting.
11714
0a3b29ad 11715 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11716 `template' keyword, in a construction like:
11717
11718 T::template f<3>()
11719
11720 In that case `f' is taken to be a template-name, even though there
11721 is no way of knowing for sure.
11722
11723 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11724 name refers to a set of overloaded functions, at least one of which
11725 is a template, or an IDENTIFIER_NODE with the name of the template,
11726 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11727 names are looked up inside uninstantiated templates. */
11728
11729static tree
ccb84981 11730cp_parser_template_name (cp_parser* parser,
653e5405 11731 bool template_keyword_p,
11732 bool check_dependency_p,
3d0f901b 11733 bool is_declaration,
11734 bool *is_identifier)
0a3b29ad 11735{
11736 tree identifier;
11737 tree decl;
11738 tree fns;
ad9ae192 11739 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11740
11741 /* If the next token is `operator', then we have either an
11742 operator-function-id or a conversion-function-id. */
11743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11744 {
11745 /* We don't know whether we're looking at an
11746 operator-function-id or a conversion-function-id. */
11747 cp_parser_parse_tentatively (parser);
11748 /* Try an operator-function-id. */
11749 identifier = cp_parser_operator_function_id (parser);
11750 /* If that didn't work, try a conversion-function-id. */
11751 if (!cp_parser_parse_definitely (parser))
653e5405 11752 {
182d094e 11753 cp_parser_error (parser, "expected template-name");
11754 return error_mark_node;
653e5405 11755 }
0a3b29ad 11756 }
11757 /* Look for the identifier. */
11758 else
11759 identifier = cp_parser_identifier (parser);
ccb84981 11760
0a3b29ad 11761 /* If we didn't find an identifier, we don't have a template-id. */
11762 if (identifier == error_mark_node)
11763 return error_mark_node;
11764
11765 /* If the name immediately followed the `template' keyword, then it
11766 is a template-name. However, if the next token is not `<', then
11767 we do not treat it as a template-name, since it is not being used
11768 as part of a template-id. This enables us to handle constructs
11769 like:
11770
11771 template <typename T> struct S { S(); };
11772 template <typename T> S<T>::S();
11773
11774 correctly. We would treat `S' as a template -- if it were `S<T>'
11775 -- but we do not if there is no `<'. */
3d0f901b 11776
11777 if (processing_template_decl
c8d5ab79 11778 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
3d0f901b 11779 {
11780 /* In a declaration, in a dependent context, we pretend that the
11781 "template" keyword was present in order to improve error
11782 recovery. For example, given:
ccb84981 11783
3d0f901b 11784 template <typename T> void f(T::X<int>);
ccb84981 11785
3d0f901b 11786 we want to treat "X<int>" as a template-id. */
ccb84981 11787 if (is_declaration
11788 && !template_keyword_p
3d0f901b 11789 && parser->scope && TYPE_P (parser->scope)
0078a5e8 11790 && check_dependency_p
05f701e2 11791 && dependent_scope_p (parser->scope)
9ed82c65 11792 /* Do not do this for dtors (or ctors), since they never
11793 need the template keyword before their name. */
11794 && !constructor_name_p (identifier, parser->scope))
3d0f901b 11795 {
19273cc2 11796 cp_token_position start = 0;
9031d10b 11797
3d0f901b 11798 /* Explain what went wrong. */
ccb59bb6 11799 error_at (token->location, "non-template %qD used as template",
11800 identifier);
11801 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
9ed82c65 11802 parser->scope, identifier);
efcbcf83 11803 /* If parsing tentatively, find the location of the "<" token. */
11804 if (cp_parser_simulate_error (parser))
11805 start = cp_lexer_token_position (parser->lexer, true);
3d0f901b 11806 /* Parse the template arguments so that we can issue error
11807 messages about them. */
11808 cp_lexer_consume_token (parser->lexer);
11809 cp_parser_enclosed_template_argument_list (parser);
11810 /* Skip tokens until we find a good place from which to
11811 continue parsing. */
11812 cp_parser_skip_to_closing_parenthesis (parser,
11813 /*recovering=*/true,
11814 /*or_comma=*/true,
11815 /*consume_paren=*/false);
11816 /* If parsing tentatively, permanently remove the
11817 template argument list. That will prevent duplicate
11818 error messages from being issued about the missing
11819 "template" keyword. */
19273cc2 11820 if (start)
11821 cp_lexer_purge_tokens_after (parser->lexer, start);
3d0f901b 11822 if (is_identifier)
11823 *is_identifier = true;
11824 return identifier;
11825 }
3180c04a 11826
11827 /* If the "template" keyword is present, then there is generally
11828 no point in doing name-lookup, so we just return IDENTIFIER.
11829 But, if the qualifying scope is non-dependent then we can
11830 (and must) do name-lookup normally. */
11831 if (template_keyword_p
11832 && (!parser->scope
207355ad 11833 || (TYPE_P (parser->scope)
3180c04a 11834 && dependent_type_p (parser->scope))))
3d0f901b 11835 return identifier;
11836 }
0a3b29ad 11837
11838 /* Look up the name. */
11839 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 11840 none_type,
6dd3a38d 11841 /*is_template=*/true,
6fc758aa 11842 /*is_namespace=*/false,
2cdbcd51 11843 check_dependency_p,
ad9ae192 11844 /*ambiguous_decls=*/NULL,
11845 token->location);
0a3b29ad 11846
11847 /* If DECL is a template, then the name was a template-name. */
11848 if (TREE_CODE (decl) == TEMPLATE_DECL)
11849 ;
ccb84981 11850 else
0a3b29ad 11851 {
a5a297d8 11852 tree fn = NULL_TREE;
11853
0a3b29ad 11854 /* The standard does not explicitly indicate whether a name that
11855 names a set of overloaded declarations, some of which are
11856 templates, is a template-name. However, such a name should
11857 be a template-name; otherwise, there is no way to form a
11858 template-id for the overloaded templates. */
11859 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11860 if (TREE_CODE (fns) == OVERLOAD)
a5a297d8 11861 for (fn = fns; fn; fn = OVL_NEXT (fn))
11862 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11863 break;
ccb84981 11864
a5a297d8 11865 if (!fn)
0a3b29ad 11866 {
a5a297d8 11867 /* The name does not name a template. */
0a3b29ad 11868 cp_parser_error (parser, "expected template-name");
11869 return error_mark_node;
11870 }
11871 }
11872
11873 /* If DECL is dependent, and refers to a function, then just return
11874 its name; we will look it up again during template instantiation. */
11875 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11876 {
11877 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
7e9a6a16 11878 if (TYPE_P (scope) && dependent_type_p (scope))
0a3b29ad 11879 return identifier;
11880 }
11881
11882 return decl;
11883}
11884
11885/* Parse a template-argument-list.
11886
11887 template-argument-list:
d95d815d 11888 template-argument ... [opt]
11889 template-argument-list , template-argument ... [opt]
0a3b29ad 11890
bd8962d5 11891 Returns a TREE_VEC containing the arguments. */
0a3b29ad 11892
11893static tree
45baea8b 11894cp_parser_template_argument_list (cp_parser* parser)
0a3b29ad 11895{
b5959ba9 11896 tree fixed_args[10];
11897 unsigned n_args = 0;
11898 unsigned alloced = 10;
11899 tree *arg_ary = fixed_args;
11900 tree vec;
92b128ed 11901 bool saved_in_template_argument_list_p;
45b4e1e4 11902 bool saved_ice_p;
11903 bool saved_non_ice_p;
0a3b29ad 11904
92b128ed 11905 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11906 parser->in_template_argument_list_p = true;
45b4e1e4 11907 /* Even if the template-id appears in an integral
074ab442 11908 constant-expression, the contents of the argument list do
11909 not. */
45b4e1e4 11910 saved_ice_p = parser->integral_constant_expression_p;
11911 parser->integral_constant_expression_p = false;
11912 saved_non_ice_p = parser->non_integral_constant_expression_p;
11913 parser->non_integral_constant_expression_p = false;
fbb01da7 11914 /* Parse the arguments. */
b5959ba9 11915 do
0a3b29ad 11916 {
11917 tree argument;
11918
b5959ba9 11919 if (n_args)
bd8962d5 11920 /* Consume the comma. */
b5959ba9 11921 cp_lexer_consume_token (parser->lexer);
ccb84981 11922
0a3b29ad 11923 /* Parse the template-argument. */
11924 argument = cp_parser_template_argument (parser);
d95d815d 11925
11926 /* If the next token is an ellipsis, we're expanding a template
11927 argument pack. */
11928 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11929 {
7cdf9e98 11930 if (argument == error_mark_node)
11931 {
11932 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 11933 error_at (token->location,
11934 "expected parameter pack before %<...%>");
7cdf9e98 11935 }
d95d815d 11936 /* Consume the `...' token. */
11937 cp_lexer_consume_token (parser->lexer);
11938
11939 /* Make the argument into a TYPE_PACK_EXPANSION or
11940 EXPR_PACK_EXPANSION. */
11941 argument = make_pack_expansion (argument);
11942 }
11943
b5959ba9 11944 if (n_args == alloced)
11945 {
11946 alloced *= 2;
ccb84981 11947
b5959ba9 11948 if (arg_ary == fixed_args)
11949 {
56e60747 11950 arg_ary = XNEWVEC (tree, alloced);
b5959ba9 11951 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11952 }
11953 else
7ea410eb 11954 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
b5959ba9 11955 }
11956 arg_ary[n_args++] = argument;
0a3b29ad 11957 }
b5959ba9 11958 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11959
11960 vec = make_tree_vec (n_args);
0a3b29ad 11961
b5959ba9 11962 while (n_args--)
11963 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
ccb84981 11964
b5959ba9 11965 if (arg_ary != fixed_args)
11966 free (arg_ary);
45b4e1e4 11967 parser->non_integral_constant_expression_p = saved_non_ice_p;
11968 parser->integral_constant_expression_p = saved_ice_p;
92b128ed 11969 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
94c17f03 11970#ifdef ENABLE_CHECKING
11971 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11972#endif
b5959ba9 11973 return vec;
0a3b29ad 11974}
11975
11976/* Parse a template-argument.
11977
11978 template-argument:
11979 assignment-expression
11980 type-id
11981 id-expression
11982
11983 The representation is that of an assignment-expression, type-id, or
11984 id-expression -- except that the qualified id-expression is
11985 evaluated, so that the value returned is either a DECL or an
ccb84981 11986 OVERLOAD.
13795292 11987
11988 Although the standard says "assignment-expression", it forbids
11989 throw-expressions or assignments in the template argument.
11990 Therefore, we use "conditional-expression" instead. */
0a3b29ad 11991
11992static tree
45baea8b 11993cp_parser_template_argument (cp_parser* parser)
0a3b29ad 11994{
11995 tree argument;
11996 bool template_p;
13795292 11997 bool address_p;
bece9ea1 11998 bool maybe_type_id = false;
ad9ae192 11999 cp_token *token = NULL, *argument_start_token = NULL;
0886adbc 12000 cp_id_kind idk;
0a3b29ad 12001
12002 /* There's really no way to know what we're looking at, so we just
ccb84981 12003 try each alternative in order.
0a3b29ad 12004
12005 [temp.arg]
12006
12007 In a template-argument, an ambiguity between a type-id and an
12008 expression is resolved to a type-id, regardless of the form of
ccb84981 12009 the corresponding template-parameter.
0a3b29ad 12010
12011 Therefore, we try a type-id first. */
12012 cp_parser_parse_tentatively (parser);
75eaa947 12013 argument = cp_parser_template_type_arg (parser);
7d46c9d7 12014 /* If there was no error parsing the type-id but the next token is a
12015 '>>', our behavior depends on which dialect of C++ we're
12016 parsing. In C++98, we probably found a typo for '> >'. But there
12017 are type-id which are also valid expressions. For instance:
bece9ea1 12018
12019 struct X { int operator >> (int); };
12020 template <int V> struct Foo {};
12021 Foo<X () >> 5> r;
12022
12023 Here 'X()' is a valid type-id of a function type, but the user just
12024 wanted to write the expression "X() >> 5". Thus, we remember that we
12025 found a valid type-id, but we still try to parse the argument as an
7d46c9d7 12026 expression to see what happens.
12027
12028 In C++0x, the '>>' will be considered two separate '>'
12029 tokens. */
bece9ea1 12030 if (!cp_parser_error_occurred (parser)
7d46c9d7 12031 && cxx_dialect == cxx98
bece9ea1 12032 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12033 {
12034 maybe_type_id = true;
12035 cp_parser_abort_tentative_parse (parser);
12036 }
12037 else
12038 {
12039 /* If the next token isn't a `,' or a `>', then this argument wasn't
12040 really finished. This means that the argument is not a valid
12041 type-id. */
12042 if (!cp_parser_next_token_ends_template_argument_p (parser))
12043 cp_parser_error (parser, "expected template-argument");
12044 /* If that worked, we're done. */
12045 if (cp_parser_parse_definitely (parser))
12046 return argument;
12047 }
0a3b29ad 12048 /* We're still not sure what the argument will be. */
12049 cp_parser_parse_tentatively (parser);
12050 /* Try a template. */
ad9ae192 12051 argument_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 12052 argument = cp_parser_id_expression (parser,
0a3b29ad 12053 /*template_keyword_p=*/false,
12054 /*check_dependency_p=*/true,
899cc6e8 12055 &template_p,
197c9df7 12056 /*declarator_p=*/false,
130bb1d4 12057 /*optional_p=*/false);
0a3b29ad 12058 /* If the next token isn't a `,' or a `>', then this argument wasn't
12059 really finished. */
13795292 12060 if (!cp_parser_next_token_ends_template_argument_p (parser))
0a3b29ad 12061 cp_parser_error (parser, "expected template-argument");
12062 if (!cp_parser_error_occurred (parser))
12063 {
4ec378b6 12064 /* Figure out what is being referred to. If the id-expression
12065 was for a class template specialization, then we will have a
12066 TYPE_DECL at this point. There is no need to do name lookup
12067 at this point in that case. */
12068 if (TREE_CODE (argument) != TYPE_DECL)
12069 argument = cp_parser_lookup_name (parser, argument,
e2ae55f2 12070 none_type,
4ec378b6 12071 /*is_template=*/template_p,
12072 /*is_namespace=*/false,
2cdbcd51 12073 /*check_dependency=*/true,
ad9ae192 12074 /*ambiguous_decls=*/NULL,
12075 argument_start_token->location);
11c54989 12076 if (TREE_CODE (argument) != TEMPLATE_DECL
12077 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
0a3b29ad 12078 cp_parser_error (parser, "expected template-name");
12079 }
12080 if (cp_parser_parse_definitely (parser))
12081 return argument;
13795292 12082 /* It must be a non-type argument. There permitted cases are given
12083 in [temp.arg.nontype]:
12084
12085 -- an integral constant-expression of integral or enumeration
653e5405 12086 type; or
13795292 12087
12088 -- the name of a non-type template-parameter; or
12089
12090 -- the name of an object or function with external linkage...
12091
12092 -- the address of an object or function with external linkage...
12093
bd8962d5 12094 -- a pointer to member... */
13795292 12095 /* Look for a non-type template parameter. */
12096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12097 {
12098 cp_parser_parse_tentatively (parser);
12099 argument = cp_parser_primary_expression (parser,
08cc44e7 12100 /*address_p=*/false,
640aa28c 12101 /*cast_p=*/false,
fbb01da7 12102 /*template_arg_p=*/true,
12103 &idk);
13795292 12104 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12105 || !cp_parser_next_token_ends_template_argument_p (parser))
12106 cp_parser_simulate_error (parser);
12107 if (cp_parser_parse_definitely (parser))
12108 return argument;
12109 }
729f89ff 12110
13795292 12111 /* If the next token is "&", the argument must be the address of an
12112 object or function with external linkage. */
12113 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12114 if (address_p)
12115 cp_lexer_consume_token (parser->lexer);
12116 /* See if we might have an id-expression. */
12117 token = cp_lexer_peek_token (parser->lexer);
12118 if (token->type == CPP_NAME
12119 || token->keyword == RID_OPERATOR
12120 || token->type == CPP_SCOPE
12121 || token->type == CPP_TEMPLATE_ID
12122 || token->type == CPP_NESTED_NAME_SPECIFIER)
12123 {
12124 cp_parser_parse_tentatively (parser);
12125 argument = cp_parser_primary_expression (parser,
fbb01da7 12126 address_p,
640aa28c 12127 /*cast_p=*/false,
fbb01da7 12128 /*template_arg_p=*/true,
12129 &idk);
13795292 12130 if (cp_parser_error_occurred (parser)
12131 || !cp_parser_next_token_ends_template_argument_p (parser))
12132 cp_parser_abort_tentative_parse (parser);
12133 else
12134 {
11026030 12135 tree probe;
12136
729f89ff 12137 if (TREE_CODE (argument) == INDIRECT_REF)
12138 {
12139 gcc_assert (REFERENCE_REF_P (argument));
12140 argument = TREE_OPERAND (argument, 0);
12141 }
9031d10b 12142
11026030 12143 /* If we're in a template, we represent a qualified-id referring
12144 to a static data member as a SCOPE_REF even if the scope isn't
12145 dependent so that we can check access control later. */
12146 probe = argument;
12147 if (TREE_CODE (probe) == SCOPE_REF)
12148 probe = TREE_OPERAND (probe, 1);
12149 if (TREE_CODE (probe) == VAR_DECL)
13795292 12150 {
12151 /* A variable without external linkage might still be a
12152 valid constant-expression, so no error is issued here
12153 if the external-linkage check fails. */
11026030 12154 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13795292 12155 cp_parser_simulate_error (parser);
12156 }
12157 else if (is_overloaded_fn (argument))
12158 /* All overloaded functions are allowed; if the external
12159 linkage test does not pass, an error will be issued
12160 later. */
12161 ;
12162 else if (address_p
ccb84981 12163 && (TREE_CODE (argument) == OFFSET_REF
13795292 12164 || TREE_CODE (argument) == SCOPE_REF))
12165 /* A pointer-to-member. */
12166 ;
729f89ff 12167 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12168 ;
13795292 12169 else
12170 cp_parser_simulate_error (parser);
12171
12172 if (cp_parser_parse_definitely (parser))
12173 {
12174 if (address_p)
ebd21de4 12175 argument = build_x_unary_op (ADDR_EXPR, argument,
12176 tf_warning_or_error);
13795292 12177 return argument;
12178 }
12179 }
12180 }
12181 /* If the argument started with "&", there are no other valid
12182 alternatives at this point. */
12183 if (address_p)
12184 {
12185 cp_parser_error (parser, "invalid non-type template argument");
12186 return error_mark_node;
12187 }
729f89ff 12188
bece9ea1 12189 /* If the argument wasn't successfully parsed as a type-id followed
ccb84981 12190 by '>>', the argument can only be a constant expression now.
bece9ea1 12191 Otherwise, we try parsing the constant-expression tentatively,
12192 because the argument could really be a type-id. */
12193 if (maybe_type_id)
12194 cp_parser_parse_tentatively (parser);
ccb84981 12195 argument = cp_parser_constant_expression (parser,
13795292 12196 /*allow_non_constant_p=*/false,
12197 /*non_constant_p=*/NULL);
2250d32c 12198 argument = fold_non_dependent_expr (argument);
bece9ea1 12199 if (!maybe_type_id)
12200 return argument;
12201 if (!cp_parser_next_token_ends_template_argument_p (parser))
12202 cp_parser_error (parser, "expected template-argument");
12203 if (cp_parser_parse_definitely (parser))
12204 return argument;
12205 /* We did our best to parse the argument as a non type-id, but that
12206 was the only alternative that matched (albeit with a '>' after
ccb84981 12207 it). We can assume it's just a typo from the user, and a
bece9ea1 12208 diagnostic will then be issued. */
75eaa947 12209 return cp_parser_template_type_arg (parser);
0a3b29ad 12210}
12211
12212/* Parse an explicit-instantiation.
12213
12214 explicit-instantiation:
ccb84981 12215 template declaration
0a3b29ad 12216
12217 Although the standard says `declaration', what it really means is:
12218
12219 explicit-instantiation:
ccb84981 12220 template decl-specifier-seq [opt] declarator [opt] ;
0a3b29ad 12221
12222 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12223 supposed to be allowed. A defect report has been filed about this
ccb84981 12224 issue.
0a3b29ad 12225
12226 GNU Extension:
ccb84981 12227
0a3b29ad 12228 explicit-instantiation:
ccb84981 12229 storage-class-specifier template
0a3b29ad 12230 decl-specifier-seq [opt] declarator [opt] ;
ccb84981 12231 function-specifier template
0a3b29ad 12232 decl-specifier-seq [opt] declarator [opt] ; */
12233
12234static void
45baea8b 12235cp_parser_explicit_instantiation (cp_parser* parser)
0a3b29ad 12236{
8172be22 12237 int declares_class_or_enum;
4b9b2871 12238 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 12239 tree extension_specifier = NULL_TREE;
12240
12241 /* Look for an (optional) storage-class-specifier or
12242 function-specifier. */
12243 if (cp_parser_allow_gnu_extensions_p (parser))
12244 {
ccb84981 12245 extension_specifier
0a3b29ad 12246 = cp_parser_storage_class_specifier_opt (parser);
12247 if (!extension_specifier)
207355ad 12248 extension_specifier
4b9b2871 12249 = cp_parser_function_specifier_opt (parser,
12250 /*decl_specs=*/NULL);
0a3b29ad 12251 }
12252
12253 /* Look for the `template' keyword. */
c247dce0 12254 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
0a3b29ad 12255 /* Let the front end know that we are processing an explicit
12256 instantiation. */
12257 begin_explicit_instantiation ();
12258 /* [temp.explicit] says that we are supposed to ignore access
12259 control while processing explicit instantiation directives. */
4cab8273 12260 push_deferring_access_checks (dk_no_check);
0a3b29ad 12261 /* Parse a decl-specifier-seq. */
4b9b2871 12262 cp_parser_decl_specifier_seq (parser,
12263 CP_PARSER_FLAGS_OPTIONAL,
12264 &decl_specifiers,
12265 &declares_class_or_enum);
0a3b29ad 12266 /* If there was exactly one decl-specifier, and it declared a class,
12267 and there's no declarator, then we have an explicit type
12268 instantiation. */
12269 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12270 {
12271 tree type;
12272
4b9b2871 12273 type = check_tag_decl (&decl_specifiers);
6bdc805e 12274 /* Turn access control back on for names used during
12275 template instantiation. */
12276 pop_deferring_access_checks ();
0a3b29ad 12277 if (type)
c31fdaf6 12278 do_type_instantiation (type, extension_specifier,
074ab442 12279 /*complain=*/tf_error);
0a3b29ad 12280 }
12281 else
12282 {
3046c0a3 12283 cp_declarator *declarator;
0a3b29ad 12284 tree decl;
12285
12286 /* Parse the declarator. */
ccb84981 12287 declarator
42bbd0ec 12288 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 12289 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 12290 /*parenthesized_p=*/NULL,
12291 /*member_p=*/false);
e2ae55f2 12292 if (declares_class_or_enum & 2)
12293 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 12294 decl_specifiers.type,
12295 decl_specifiers.type_location);
3046c0a3 12296 if (declarator != cp_error_declarator)
e1a6bbd7 12297 {
ca63c29a 12298 if (decl_specifiers.specs[(int)ds_inline])
12299 permerror (input_location, "explicit instantiation shall not use"
12300 " %<inline%> specifier");
12301 if (decl_specifiers.specs[(int)ds_constexpr])
12302 permerror (input_location, "explicit instantiation shall not use"
12303 " %<constexpr%> specifier");
12304
4b9b2871 12305 decl = grokdeclarator (declarator, &decl_specifiers,
4a2849cb 12306 NORMAL, 0, &decl_specifiers.attributes);
e1a6bbd7 12307 /* Turn access control back on for names used during
12308 template instantiation. */
12309 pop_deferring_access_checks ();
12310 /* Do the explicit instantiation. */
12311 do_decl_instantiation (decl, extension_specifier);
12312 }
12313 else
12314 {
12315 pop_deferring_access_checks ();
12316 /* Skip the body of the explicit instantiation. */
12317 cp_parser_skip_to_end_of_statement (parser);
12318 }
0a3b29ad 12319 }
12320 /* We're done with the instantiation. */
12321 end_explicit_instantiation ();
0a3b29ad 12322
cf91b86a 12323 cp_parser_consume_semicolon_at_end_of_statement (parser);
0a3b29ad 12324}
12325
12326/* Parse an explicit-specialization.
12327
12328 explicit-specialization:
ccb84981 12329 template < > declaration
0a3b29ad 12330
12331 Although the standard says `declaration', what it really means is:
12332
12333 explicit-specialization:
12334 template <> decl-specifier [opt] init-declarator [opt] ;
ccb84981 12335 template <> function-definition
0a3b29ad 12336 template <> explicit-specialization
12337 template <> template-declaration */
12338
12339static void
45baea8b 12340cp_parser_explicit_specialization (cp_parser* parser)
0a3b29ad 12341{
9f25cdd8 12342 bool need_lang_pop;
ad9ae192 12343 cp_token *token = cp_lexer_peek_token (parser->lexer);
12344
0a3b29ad 12345 /* Look for the `template' keyword. */
c247dce0 12346 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
0a3b29ad 12347 /* Look for the `<'. */
c247dce0 12348 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 12349 /* Look for the `>'. */
c247dce0 12350 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 12351 /* We have processed another parameter list. */
12352 ++parser->num_template_parameter_lists;
9f25cdd8 12353 /* [temp]
074ab442 12354
9f25cdd8 12355 A template ... explicit specialization ... shall not have C
074ab442 12356 linkage. */
9f25cdd8 12357 if (current_lang_name == lang_name_c)
12358 {
ccb59bb6 12359 error_at (token->location, "template specialization with C linkage");
9f25cdd8 12360 /* Give it C++ linkage to avoid confusing other parts of the
12361 front end. */
12362 push_lang_context (lang_name_cplusplus);
12363 need_lang_pop = true;
12364 }
12365 else
12366 need_lang_pop = false;
0a3b29ad 12367 /* Let the front end know that we are beginning a specialization. */
6d9bff9f 12368 if (!begin_specialization ())
12369 {
12370 end_specialization ();
6d9bff9f 12371 return;
12372 }
12373
0a3b29ad 12374 /* If the next keyword is `template', we need to figure out whether
12375 or not we're looking a template-declaration. */
12376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12377 {
12378 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12379 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12380 cp_parser_template_declaration_after_export (parser,
12381 /*member_p=*/false);
12382 else
12383 cp_parser_explicit_specialization (parser);
12384 }
12385 else
12386 /* Parse the dependent declaration. */
ccb84981 12387 cp_parser_single_declaration (parser,
3369eb76 12388 /*checks=*/NULL,
0a3b29ad 12389 /*member_p=*/false,
0c032b46 12390 /*explicit_specialization_p=*/true,
0a3b29ad 12391 /*friend_p=*/NULL);
0a3b29ad 12392 /* We're done with the specialization. */
12393 end_specialization ();
9f25cdd8 12394 /* For the erroneous case of a template with C linkage, we pushed an
12395 implicit C++ linkage scope; exit that scope now. */
12396 if (need_lang_pop)
12397 pop_lang_context ();
0a3b29ad 12398 /* We're done with this parameter list. */
12399 --parser->num_template_parameter_lists;
12400}
12401
12402/* Parse a type-specifier.
12403
12404 type-specifier:
12405 simple-type-specifier
12406 class-specifier
12407 enum-specifier
12408 elaborated-type-specifier
12409 cv-qualifier
12410
12411 GNU Extension:
12412
12413 type-specifier:
12414 __complex__
12415
4b9b2871 12416 Returns a representation of the type-specifier. For a
12417 class-specifier, enum-specifier, or elaborated-type-specifier, a
12418 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
0a3b29ad 12419
fb871e92 12420 The parser flags FLAGS is used to control type-specifier parsing.
12421
12422 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12423 in a decl-specifier-seq.
0a3b29ad 12424
12425 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12426 class-specifier, enum-specifier, or elaborated-type-specifier, then
678704be 12427 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8172be22 12428 if a type is declared; 2 if it is defined. Otherwise, it is set to
12429 zero.
0a3b29ad 12430
12431 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12432 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12433 is set to FALSE. */
12434
12435static tree
ccb84981 12436cp_parser_type_specifier (cp_parser* parser,
12437 cp_parser_flags flags,
4b9b2871 12438 cp_decl_specifier_seq *decl_specs,
45baea8b 12439 bool is_declaration,
8172be22 12440 int* declares_class_or_enum,
45baea8b 12441 bool* is_cv_qualifier)
0a3b29ad 12442{
12443 tree type_spec = NULL_TREE;
12444 cp_token *token;
12445 enum rid keyword;
4b9b2871 12446 cp_decl_spec ds = ds_last;
0a3b29ad 12447
12448 /* Assume this type-specifier does not declare a new type. */
12449 if (declares_class_or_enum)
8e594c09 12450 *declares_class_or_enum = 0;
0a3b29ad 12451 /* And that it does not specify a cv-qualifier. */
12452 if (is_cv_qualifier)
12453 *is_cv_qualifier = false;
12454 /* Peek at the next token. */
12455 token = cp_lexer_peek_token (parser->lexer);
12456
12457 /* If we're looking at a keyword, we can use that to guide the
12458 production we choose. */
12459 keyword = token->keyword;
12460 switch (keyword)
12461 {
637fdc50 12462 case RID_ENUM:
638569c5 12463 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12464 goto elaborated_type_specifier;
12465
4a2849cb 12466 /* Look for the enum-specifier. */
12467 type_spec = cp_parser_enum_specifier (parser);
12468 /* If that worked, we're done. */
12469 if (type_spec)
637fdc50 12470 {
637fdc50 12471 if (declares_class_or_enum)
12472 *declares_class_or_enum = 2;
12473 if (decl_specs)
12474 cp_parser_set_decl_spec_type (decl_specs,
12475 type_spec,
eef0ab03 12476 token->location,
637fdc50 12477 /*user_defined_p=*/true);
12478 return type_spec;
12479 }
12480 else
12481 goto elaborated_type_specifier;
12482
0a3b29ad 12483 /* Any of these indicate either a class-specifier, or an
12484 elaborated-type-specifier. */
12485 case RID_CLASS:
12486 case RID_STRUCT:
12487 case RID_UNION:
638569c5 12488 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12489 goto elaborated_type_specifier;
12490
0a3b29ad 12491 /* Parse tentatively so that we can back up if we don't find a
637fdc50 12492 class-specifier. */
0a3b29ad 12493 cp_parser_parse_tentatively (parser);
637fdc50 12494 /* Look for the class-specifier. */
12495 type_spec = cp_parser_class_specifier (parser);
dd2b35f1 12496 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
0a3b29ad 12497 /* If that worked, we're done. */
12498 if (cp_parser_parse_definitely (parser))
12499 {
12500 if (declares_class_or_enum)
8172be22 12501 *declares_class_or_enum = 2;
4b9b2871 12502 if (decl_specs)
12503 cp_parser_set_decl_spec_type (decl_specs,
12504 type_spec,
eef0ab03 12505 token->location,
4b9b2871 12506 /*user_defined_p=*/true);
0a3b29ad 12507 return type_spec;
12508 }
12509
12510 /* Fall through. */
637fdc50 12511 elaborated_type_specifier:
12512 /* We're declaring (not defining) a class or enum. */
12513 if (declares_class_or_enum)
12514 *declares_class_or_enum = 1;
0a3b29ad 12515
637fdc50 12516 /* Fall through. */
0a3b29ad 12517 case RID_TYPENAME:
12518 /* Look for an elaborated-type-specifier. */
207355ad 12519 type_spec
12520 = (cp_parser_elaborated_type_specifier
4b9b2871 12521 (parser,
12522 decl_specs && decl_specs->specs[(int) ds_friend],
12523 is_declaration));
4b9b2871 12524 if (decl_specs)
12525 cp_parser_set_decl_spec_type (decl_specs,
12526 type_spec,
eef0ab03 12527 token->location,
4b9b2871 12528 /*user_defined_p=*/true);
0a3b29ad 12529 return type_spec;
12530
12531 case RID_CONST:
4b9b2871 12532 ds = ds_const;
12533 if (is_cv_qualifier)
12534 *is_cv_qualifier = true;
12535 break;
207355ad 12536
0a3b29ad 12537 case RID_VOLATILE:
4b9b2871 12538 ds = ds_volatile;
0a3b29ad 12539 if (is_cv_qualifier)
12540 *is_cv_qualifier = true;
4b9b2871 12541 break;
0a3b29ad 12542
4b9b2871 12543 case RID_RESTRICT:
12544 ds = ds_restrict;
12545 if (is_cv_qualifier)
12546 *is_cv_qualifier = true;
12547 break;
0a3b29ad 12548
12549 case RID_COMPLEX:
12550 /* The `__complex__' keyword is a GNU extension. */
4b9b2871 12551 ds = ds_complex;
12552 break;
0a3b29ad 12553
12554 default:
12555 break;
12556 }
12557
4b9b2871 12558 /* Handle simple keywords. */
12559 if (ds != ds_last)
12560 {
12561 if (decl_specs)
12562 {
12563 ++decl_specs->specs[(int)ds];
12564 decl_specs->any_specifiers_p = true;
12565 }
3369eb76 12566 return cp_lexer_consume_token (parser->lexer)->u.value;
4b9b2871 12567 }
12568
0a3b29ad 12569 /* If we do not already have a type-specifier, assume we are looking
12570 at a simple-type-specifier. */
207355ad 12571 type_spec = cp_parser_simple_type_specifier (parser,
4b9b2871 12572 decl_specs,
12573 flags);
0a3b29ad 12574
12575 /* If we didn't find a type-specifier, and a type-specifier was not
12576 optional in this context, issue an error message. */
12577 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12578 {
12579 cp_parser_error (parser, "expected type specifier");
12580 return error_mark_node;
12581 }
12582
12583 return type_spec;
12584}
12585
12586/* Parse a simple-type-specifier.
12587
12588 simple-type-specifier:
12589 :: [opt] nested-name-specifier [opt] type-name
12590 :: [opt] nested-name-specifier template template-id
12591 char
12592 wchar_t
12593 bool
12594 short
12595 int
12596 long
12597 signed
12598 unsigned
12599 float
12600 double
ccb84981 12601 void
0a3b29ad 12602
34da8800 12603 C++0x Extension:
12604
12605 simple-type-specifier:
45b44d0a 12606 auto
34da8800 12607 decltype ( expression )
924bbf02 12608 char16_t
12609 char32_t
34da8800 12610
0a3b29ad 12611 GNU Extension:
12612
12613 simple-type-specifier:
6388cfe2 12614 __int128
0a3b29ad 12615 __typeof__ unary-expression
12616 __typeof__ ( type-id )
12617
4b9b2871 12618 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12619 appropriately updated. */
0a3b29ad 12620
12621static tree
207355ad 12622cp_parser_simple_type_specifier (cp_parser* parser,
4b9b2871 12623 cp_decl_specifier_seq *decl_specs,
12624 cp_parser_flags flags)
0a3b29ad 12625{
12626 tree type = NULL_TREE;
12627 cp_token *token;
12628
12629 /* Peek at the next token. */
12630 token = cp_lexer_peek_token (parser->lexer);
12631
12632 /* If we're looking at a keyword, things are easy. */
12633 switch (token->keyword)
12634 {
12635 case RID_CHAR:
4b9b2871 12636 if (decl_specs)
12637 decl_specs->explicit_char_p = true;
b7d1e8ea 12638 type = char_type_node;
12639 break;
924bbf02 12640 case RID_CHAR16:
12641 type = char16_type_node;
12642 break;
12643 case RID_CHAR32:
12644 type = char32_type_node;
12645 break;
0a3b29ad 12646 case RID_WCHAR:
b7d1e8ea 12647 type = wchar_type_node;
12648 break;
0a3b29ad 12649 case RID_BOOL:
b7d1e8ea 12650 type = boolean_type_node;
12651 break;
0a3b29ad 12652 case RID_SHORT:
4b9b2871 12653 if (decl_specs)
12654 ++decl_specs->specs[(int) ds_short];
b7d1e8ea 12655 type = short_integer_type_node;
12656 break;
0a3b29ad 12657 case RID_INT:
4b9b2871 12658 if (decl_specs)
12659 decl_specs->explicit_int_p = true;
b7d1e8ea 12660 type = integer_type_node;
12661 break;
6388cfe2 12662 case RID_INT128:
12663 if (!int128_integer_type_node)
12664 break;
12665 if (decl_specs)
12666 decl_specs->explicit_int128_p = true;
12667 type = int128_integer_type_node;
12668 break;
0a3b29ad 12669 case RID_LONG:
4b9b2871 12670 if (decl_specs)
12671 ++decl_specs->specs[(int) ds_long];
b7d1e8ea 12672 type = long_integer_type_node;
12673 break;
0a3b29ad 12674 case RID_SIGNED:
4b9b2871 12675 if (decl_specs)
12676 ++decl_specs->specs[(int) ds_signed];
b7d1e8ea 12677 type = integer_type_node;
12678 break;
0a3b29ad 12679 case RID_UNSIGNED:
4b9b2871 12680 if (decl_specs)
12681 ++decl_specs->specs[(int) ds_unsigned];
b7d1e8ea 12682 type = unsigned_type_node;
12683 break;
0a3b29ad 12684 case RID_FLOAT:
b7d1e8ea 12685 type = float_type_node;
12686 break;
0a3b29ad 12687 case RID_DOUBLE:
b7d1e8ea 12688 type = double_type_node;
12689 break;
0a3b29ad 12690 case RID_VOID:
b7d1e8ea 12691 type = void_type_node;
12692 break;
45b44d0a 12693
12694 case RID_AUTO:
bf8d19fe 12695 maybe_warn_cpp0x (CPP0X_AUTO);
46f4817e 12696 type = make_auto ();
45b44d0a 12697 break;
0a3b29ad 12698
34da8800 12699 case RID_DECLTYPE:
12700 /* Parse the `decltype' type. */
12701 type = cp_parser_decltype (parser);
12702
12703 if (decl_specs)
12704 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 12705 token->location,
34da8800 12706 /*user_defined_p=*/true);
12707
12708 return type;
12709
0a3b29ad 12710 case RID_TYPEOF:
4b9b2871 12711 /* Consume the `typeof' token. */
12712 cp_lexer_consume_token (parser->lexer);
12713 /* Parse the operand to `typeof'. */
12714 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12715 /* If it is not already a TYPE, take its type. */
12716 if (!TYPE_P (type))
12717 type = finish_typeof (type);
12718
12719 if (decl_specs)
12720 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 12721 token->location,
4b9b2871 12722 /*user_defined_p=*/true);
207355ad 12723
4b9b2871 12724 return type;
0a3b29ad 12725
12726 default:
12727 break;
12728 }
12729
b7d1e8ea 12730 /* If the type-specifier was for a built-in type, we're done. */
12731 if (type)
12732 {
4b9b2871 12733 /* Record the type. */
12734 if (decl_specs
12735 && (token->keyword != RID_SIGNED
12736 && token->keyword != RID_UNSIGNED
12737 && token->keyword != RID_SHORT
12738 && token->keyword != RID_LONG))
207355ad 12739 cp_parser_set_decl_spec_type (decl_specs,
4b9b2871 12740 type,
eef0ab03 12741 token->location,
4b9b2871 12742 /*user_defined=*/false);
12743 if (decl_specs)
12744 decl_specs->any_specifiers_p = true;
12745
b7d1e8ea 12746 /* Consume the token. */
8e9e8d76 12747 cp_lexer_consume_token (parser->lexer);
182d094e 12748
12749 /* There is no valid C++ program where a non-template type is
12750 followed by a "<". That usually indicates that the user thought
12751 that the type was a template. */
eef0ab03 12752 cp_parser_check_for_invalid_template_id (parser, type, token->location);
182d094e 12753
4b9b2871 12754 return TYPE_NAME (type);
b7d1e8ea 12755 }
12756
0a3b29ad 12757 /* The type-specifier must be a user-defined type. */
ccb84981 12758 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
0a3b29ad 12759 {
a116ab20 12760 bool qualified_p;
b446079d 12761 bool global_p;
a116ab20 12762
0a3b29ad 12763 /* Don't gobble tokens or issue error messages if this is an
12764 optional type-specifier. */
12765 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12766 cp_parser_parse_tentatively (parser);
12767
12768 /* Look for the optional `::' operator. */
b446079d 12769 global_p
a619017d 12770 = (cp_parser_global_scope_opt (parser,
130bb1d4 12771 /*current_scope_valid_p=*/false)
a619017d 12772 != NULL_TREE);
0a3b29ad 12773 /* Look for the nested-name specifier. */
a116ab20 12774 qualified_p
12775 = (cp_parser_nested_name_specifier_opt (parser,
12776 /*typename_keyword_p=*/false,
12777 /*check_dependency_p=*/true,
12778 /*type_p=*/false,
382fbf3d 12779 /*is_declaration=*/false)
12780 != NULL_TREE);
eef0ab03 12781 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 12782 /* If we have seen a nested-name-specifier, and the next token
12783 is `template', then we are using the template-id production. */
ccb84981 12784 if (parser->scope
0a3b29ad 12785 && cp_parser_optional_template_keyword (parser))
12786 {
12787 /* Look for the template-id. */
ccb84981 12788 type = cp_parser_template_id (parser,
0a3b29ad 12789 /*template_keyword_p=*/true,
3d0f901b 12790 /*check_dependency_p=*/true,
12791 /*is_declaration=*/false);
0a3b29ad 12792 /* If the template-id did not name a type, we are out of
12793 luck. */
12794 if (TREE_CODE (type) != TYPE_DECL)
12795 {
12796 cp_parser_error (parser, "expected template-id for type");
12797 type = NULL_TREE;
12798 }
12799 }
12800 /* Otherwise, look for a type-name. */
12801 else
92b128ed 12802 type = cp_parser_type_name (parser);
a116ab20 12803 /* Keep track of all name-lookups performed in class scopes. */
207355ad 12804 if (type
b446079d 12805 && !global_p
a116ab20 12806 && !qualified_p
207355ad 12807 && TREE_CODE (type) == TYPE_DECL
a116ab20 12808 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12809 maybe_note_name_used_in_class (DECL_NAME (type), type);
0a3b29ad 12810 /* If it didn't work out, we don't have a TYPE. */
ccb84981 12811 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
0a3b29ad 12812 && !cp_parser_parse_definitely (parser))
12813 type = NULL_TREE;
4b9b2871 12814 if (type && decl_specs)
12815 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 12816 token->location,
4b9b2871 12817 /*user_defined=*/true);
0a3b29ad 12818 }
12819
12820 /* If we didn't get a type-name, issue an error message. */
12821 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12822 {
12823 cp_parser_error (parser, "expected type-name");
12824 return error_mark_node;
12825 }
12826
92b128ed 12827 if (type && type != error_mark_node)
7a4e126b 12828 {
b0d0931f 12829 /* See if TYPE is an Objective-C type, and if so, parse and
12830 accept any protocol references following it. Do this before
12831 the cp_parser_check_for_invalid_template_id() call, because
12832 Objective-C types can be followed by '<...>' which would
12833 enclose protocol names rather than template arguments, and so
12834 everything is fine. */
260681ca 12835 if (c_dialect_objc () && !parser->scope
7a4e126b 12836 && (objc_is_id (type) || objc_is_class_name (type)))
12837 {
12838 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12839 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12840
12841 /* Clobber the "unqualified" type previously entered into
c78cbec8 12842 DECL_SPECS with the new, improved protocol-qualified version. */
7a4e126b 12843 if (decl_specs)
12844 decl_specs->type = qual_type;
12845
12846 return qual_type;
12847 }
12848
b0d0931f 12849 /* There is no valid C++ program where a non-template type is
12850 followed by a "<". That usually indicates that the user
12851 thought that the type was a template. */
eef0ab03 12852 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12853 token->location);
9031d10b 12854 }
8534c3a3 12855
0a3b29ad 12856 return type;
12857}
12858
12859/* Parse a type-name.
12860
12861 type-name:
12862 class-name
12863 enum-name
ccb84981 12864 typedef-name
0a3b29ad 12865
12866 enum-name:
12867 identifier
12868
12869 typedef-name:
ccb84981 12870 identifier
0a3b29ad 12871
dfea972c 12872 Returns a TYPE_DECL for the type. */
0a3b29ad 12873
12874static tree
45baea8b 12875cp_parser_type_name (cp_parser* parser)
0a3b29ad 12876{
12877 tree type_decl;
0a3b29ad 12878
12879 /* We can't know yet whether it is a class-name or not. */
12880 cp_parser_parse_tentatively (parser);
12881 /* Try a class-name. */
ccb84981 12882 type_decl = cp_parser_class_name (parser,
0a3b29ad 12883 /*typename_keyword_p=*/false,
12884 /*template_keyword_p=*/false,
e2ae55f2 12885 none_type,
0a3b29ad 12886 /*check_dependency_p=*/true,
3d0f901b 12887 /*class_head_p=*/false,
12888 /*is_declaration=*/false);
0a3b29ad 12889 /* If it's not a class-name, keep looking. */
12890 if (!cp_parser_parse_definitely (parser))
12891 {
12892 /* It must be a typedef-name or an enum-name. */
674e90bd 12893 return cp_parser_nonclass_name (parser);
12894 }
ccb84981 12895
674e90bd 12896 return type_decl;
12897}
7a4e126b 12898
674e90bd 12899/* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
7a4e126b 12900
674e90bd 12901 enum-name:
12902 identifier
12903
12904 typedef-name:
12905 identifier
12906
12907 Returns a TYPE_DECL for the type. */
ccb84981 12908
674e90bd 12909static tree
12910cp_parser_nonclass_name (cp_parser* parser)
12911{
12912 tree type_decl;
12913 tree identifier;
12914
ad9ae192 12915 cp_token *token = cp_lexer_peek_token (parser->lexer);
674e90bd 12916 identifier = cp_parser_identifier (parser);
12917 if (identifier == error_mark_node)
12918 return error_mark_node;
12919
12920 /* Look up the type-name. */
ad9ae192 12921 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
674e90bd 12922
12923 if (TREE_CODE (type_decl) != TYPE_DECL
12924 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12925 {
12926 /* See if this is an Objective-C type. */
12927 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12928 tree type = objc_get_protocol_qualified_type (identifier, protos);
12929 if (type)
12930 type_decl = TYPE_NAME (type);
12931 }
b0d0931f 12932
674e90bd 12933 /* Issue an error if we did not find a type-name. */
b0d0931f 12934 if (TREE_CODE (type_decl) != TYPE_DECL
12935 /* In Objective-C, we have the complication that class names are
12936 normally type names and start declarations (eg, the
12937 "NSObject" in "NSObject *object;"), but can be used in an
12938 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12939 is an expression. So, a classname followed by a dot is not a
12940 valid type-name. */
12941 || (objc_is_class_name (TREE_TYPE (type_decl))
12942 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
674e90bd 12943 {
12944 if (!cp_parser_simulate_error (parser))
12945 cp_parser_name_lookup_error (parser, identifier, type_decl,
c247dce0 12946 NLE_TYPE, token->location);
674e90bd 12947 return error_mark_node;
12948 }
12949 /* Remember that the name was used in the definition of the
12950 current class so that we can check later to see if the
12951 meaning would have been different after the class was
12952 entirely defined. */
12953 else if (type_decl != error_mark_node
12954 && !parser->scope)
12955 maybe_note_name_used_in_class (identifier, type_decl);
12956
0a3b29ad 12957 return type_decl;
12958}
12959
0a3b29ad 12960/* Parse an elaborated-type-specifier. Note that the grammar given
12961 here incorporates the resolution to DR68.
12962
12963 elaborated-type-specifier:
12964 class-key :: [opt] nested-name-specifier [opt] identifier
12965 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
3f00a6c0 12966 enum-key :: [opt] nested-name-specifier [opt] identifier
0a3b29ad 12967 typename :: [opt] nested-name-specifier identifier
ccb84981 12968 typename :: [opt] nested-name-specifier template [opt]
12969 template-id
0a3b29ad 12970
c010fc5a 12971 GNU extension:
12972
12973 elaborated-type-specifier:
12974 class-key attributes :: [opt] nested-name-specifier [opt] identifier
ccb84981 12975 class-key attributes :: [opt] nested-name-specifier [opt]
653e5405 12976 template [opt] template-id
c010fc5a 12977 enum attributes :: [opt] nested-name-specifier [opt] identifier
12978
0a3b29ad 12979 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12980 declared `friend'. If IS_DECLARATION is TRUE, then this
12981 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12982 something is being declared.
12983
12984 Returns the TYPE specified. */
12985
12986static tree
ccb84981 12987cp_parser_elaborated_type_specifier (cp_parser* parser,
653e5405 12988 bool is_friend,
12989 bool is_declaration)
0a3b29ad 12990{
12991 enum tag_types tag_type;
12992 tree identifier;
12993 tree type = NULL_TREE;
c010fc5a 12994 tree attributes = NULL_TREE;
dd42b7cf 12995 tree globalscope;
ad9ae192 12996 cp_token *token = NULL;
0a3b29ad 12997
12998 /* See if we're looking at the `enum' keyword. */
12999 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13000 {
13001 /* Consume the `enum' token. */
13002 cp_lexer_consume_token (parser->lexer);
13003 /* Remember that it's an enumeration type. */
13004 tag_type = enum_type;
aa290616 13005 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13006 enums) is used here. */
3f00a6c0 13007 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
aa290616 13008 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13009 {
13010 pedwarn (input_location, 0, "elaborated-type-specifier "
13011 "for a scoped enum must not use the %<%D%> keyword",
13012 cp_lexer_peek_token (parser->lexer)->u.value);
13013 /* Consume the `struct' or `class' and parse it anyway. */
13014 cp_lexer_consume_token (parser->lexer);
13015 }
c010fc5a 13016 /* Parse the attributes. */
13017 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 13018 }
13019 /* Or, it might be `typename'. */
13020 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13021 RID_TYPENAME))
13022 {
13023 /* Consume the `typename' token. */
13024 cp_lexer_consume_token (parser->lexer);
13025 /* Remember that it's a `typename' type. */
13026 tag_type = typename_type;
0a3b29ad 13027 }
13028 /* Otherwise it must be a class-key. */
13029 else
13030 {
13031 tag_type = cp_parser_class_key (parser);
13032 if (tag_type == none_type)
13033 return error_mark_node;
c010fc5a 13034 /* Parse the attributes. */
13035 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 13036 }
13037
13038 /* Look for the `::' operator. */
dd42b7cf 13039 globalscope = cp_parser_global_scope_opt (parser,
13040 /*current_scope_valid_p=*/false);
0a3b29ad 13041 /* Look for the nested-name-specifier. */
dd42b7cf 13042 if (tag_type == typename_type && !globalscope)
e6ec2049 13043 {
c75ae97e 13044 if (!cp_parser_nested_name_specifier (parser,
e6ec2049 13045 /*typename_keyword_p=*/true,
13046 /*check_dependency_p=*/true,
3d0f901b 13047 /*type_p=*/true,
c75ae97e 13048 is_declaration))
e6ec2049 13049 return error_mark_node;
13050 }
0a3b29ad 13051 else
13052 /* Even though `typename' is not present, the proposed resolution
13053 to Core Issue 180 says that in `class A<T>::B', `B' should be
13054 considered a type-name, even if `A<T>' is dependent. */
13055 cp_parser_nested_name_specifier_opt (parser,
13056 /*typename_keyword_p=*/true,
13057 /*check_dependency_p=*/true,
3d0f901b 13058 /*type_p=*/true,
13059 is_declaration);
6fa283db 13060 /* For everything but enumeration types, consider a template-id.
13061 For an enumeration type, consider only a plain identifier. */
0a3b29ad 13062 if (tag_type != enum_type)
13063 {
13064 bool template_p = false;
13065 tree decl;
13066
13067 /* Allow the `template' keyword. */
13068 template_p = cp_parser_optional_template_keyword (parser);
13069 /* If we didn't see `template', we don't know if there's a
653e5405 13070 template-id or not. */
0a3b29ad 13071 if (!template_p)
13072 cp_parser_parse_tentatively (parser);
13073 /* Parse the template-id. */
ad9ae192 13074 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13075 decl = cp_parser_template_id (parser, template_p,
3d0f901b 13076 /*check_dependency_p=*/true,
13077 is_declaration);
0a3b29ad 13078 /* If we didn't find a template-id, look for an ordinary
653e5405 13079 identifier. */
0a3b29ad 13080 if (!template_p && !cp_parser_parse_definitely (parser))
13081 ;
13082 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13083 in effect, then we must assume that, upon instantiation, the
13084 template will correspond to a class. */
13085 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13086 && tag_type == typename_type)
13087 type = make_typename_type (parser->scope, decl,
e2ae55f2 13088 typename_type,
c31fdaf6 13089 /*complain=*/tf_error);
cb288599 13090 /* If the `typename' keyword is in effect and DECL is not a type
13091 decl. Then type is non existant. */
13092 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13093 type = NULL_TREE;
13094 else
0a3b29ad 13095 type = TREE_TYPE (decl);
13096 }
13097
0a3b29ad 13098 if (!type)
13099 {
ad9ae192 13100 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13101 identifier = cp_parser_identifier (parser);
13102
13103 if (identifier == error_mark_node)
bbd3de90 13104 {
13105 parser->scope = NULL_TREE;
13106 return error_mark_node;
13107 }
0a3b29ad 13108
13109 /* For a `typename', we needn't call xref_tag. */
9031d10b 13110 if (tag_type == typename_type
d9da0685 13111 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
ccb84981 13112 return cp_parser_make_typename_type (parser, parser->scope,
ad9ae192 13113 identifier,
13114 token->location);
0a3b29ad 13115 /* Look up a qualified name in the usual way. */
13116 if (parser->scope)
13117 {
13118 tree decl;
8a58dc06 13119 tree ambiguous_decls;
0a3b29ad 13120
ccb84981 13121 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 13122 tag_type,
c3b9e457 13123 /*is_template=*/false,
6fc758aa 13124 /*is_namespace=*/false,
2cdbcd51 13125 /*check_dependency=*/true,
ad9ae192 13126 &ambiguous_decls,
13127 token->location);
8a58dc06 13128
13129 /* If the lookup was ambiguous, an error will already have been
13130 issued. */
13131 if (ambiguous_decls)
13132 return error_mark_node;
a5e50b31 13133
13134 /* If we are parsing friend declaration, DECL may be a
13135 TEMPLATE_DECL tree node here. However, we need to check
13136 whether this TEMPLATE_DECL results in valid code. Consider
13137 the following example:
13138
13139 namespace N {
13140 template <class T> class C {};
13141 }
13142 class X {
13143 template <class T> friend class N::C; // #1, valid code
13144 };
13145 template <class T> class Y {
13146 friend class N::C; // #2, invalid code
13147 };
13148
13149 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13150 name lookup of `N::C'. We see that friend declaration must
13151 be template for the code to be valid. Note that
13152 processing_template_decl does not work here since it is
13153 always 1 for the above two cases. */
13154
ccb84981 13155 decl = (cp_parser_maybe_treat_template_as_class
a5e50b31 13156 (decl, /*tag_name_p=*/is_friend
13157 && parser->num_template_parameter_lists));
0a3b29ad 13158
13159 if (TREE_CODE (decl) != TYPE_DECL)
13160 {
9031d10b 13161 cp_parser_diagnose_invalid_type_name (parser,
d9da0685 13162 parser->scope,
ad9ae192 13163 identifier,
13164 token->location);
0a3b29ad 13165 return error_mark_node;
13166 }
8172be22 13167
13168 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
539401d2 13169 {
13170 bool allow_template = (parser->num_template_parameter_lists
13171 || DECL_SELF_REFERENCE_P (decl));
13172 type = check_elaborated_type_specifier (tag_type, decl,
13173 allow_template);
13174
13175 if (type == error_mark_node)
13176 return error_mark_node;
13177 }
0a3b29ad 13178
f1f71333 13179 /* Forward declarations of nested types, such as
13180
13181 class C1::C2;
13182 class C1::C2::C3;
13183
13184 are invalid unless all components preceding the final '::'
13185 are complete. If all enclosing types are complete, these
13186 declarations become merely pointless.
13187
13188 Invalid forward declarations of nested types are errors
13189 caught elsewhere in parsing. Those that are pointless arrive
13190 here. */
13191
8d4f5f9e 13192 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
f1f71333 13193 && !is_friend && !processing_explicit_instantiation)
13194 warning (0, "declaration %qD does not declare anything", decl);
13195
0a3b29ad 13196 type = TREE_TYPE (decl);
13197 }
ccb84981 13198 else
0a3b29ad 13199 {
13200 /* An elaborated-type-specifier sometimes introduces a new type and
13201 sometimes names an existing type. Normally, the rule is that it
13202 introduces a new type only if there is not an existing type of
13203 the same name already in scope. For example, given:
13204
13205 struct S {};
13206 void f() { struct S s; }
13207
13208 the `struct S' in the body of `f' is the same `struct S' as in
13209 the global scope; the existing definition is used. However, if
ccb84981 13210 there were no global declaration, this would introduce a new
0a3b29ad 13211 local class named `S'.
13212
13213 An exception to this rule applies to the following code:
13214
13215 namespace N { struct S; }
13216
13217 Here, the elaborated-type-specifier names a new type
13218 unconditionally; even if there is already an `S' in the
13219 containing scope this declaration names a new type.
13220 This exception only applies if the elaborated-type-specifier
13221 forms the complete declaration:
13222
ccb84981 13223 [class.name]
0a3b29ad 13224
13225 A declaration consisting solely of `class-key identifier ;' is
13226 either a redeclaration of the name in the current scope or a
13227 forward declaration of the identifier as a class name. It
13228 introduces the name into the current scope.
13229
13230 We are in this situation precisely when the next token is a `;'.
13231
13232 An exception to the exception is that a `friend' declaration does
13233 *not* name a new type; i.e., given:
13234
13235 struct S { friend struct T; };
13236
ccb84981 13237 `T' is not a new type in the scope of `S'.
0a3b29ad 13238
13239 Also, `new struct S' or `sizeof (struct S)' never results in the
13240 definition of a new type; a new type can only be declared in a
6beb3f76 13241 declaration context. */
0a3b29ad 13242
1fadf2c8 13243 tag_scope ts;
4f311379 13244 bool template_p;
13245
1fadf2c8 13246 if (is_friend)
13247 /* Friends have special name lookup rules. */
13248 ts = ts_within_enclosing_non_class;
13249 else if (is_declaration
13250 && cp_lexer_next_token_is (parser->lexer,
13251 CPP_SEMICOLON))
13252 /* This is a `class-key identifier ;' */
13253 ts = ts_current;
13254 else
13255 ts = ts_global;
13256
074ab442 13257 template_p =
4f311379 13258 (parser->num_template_parameter_lists
13259 && (cp_parser_next_token_starts_class_definition_p (parser)
13260 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
7be1bc1f 13261 /* An unqualified name was used to reference this type, so
13262 there were no qualifying templates. */
074ab442 13263 if (!cp_parser_check_template_parameters (parser,
ad9ae192 13264 /*num_templates=*/0,
7b07a15e 13265 token->location,
13266 /*declarator=*/NULL))
7be1bc1f 13267 return error_mark_node;
4f311379 13268 type = xref_tag (tag_type, identifier, ts, template_p);
0a3b29ad 13269 }
13270 }
4a2849cb 13271
7b83d1d8 13272 if (type == error_mark_node)
13273 return error_mark_node;
13274
4a2849cb 13275 /* Allow attributes on forward declarations of classes. */
13276 if (attributes)
13277 {
b415ff10 13278 if (TREE_CODE (type) == TYPENAME_TYPE)
13279 warning (OPT_Wattributes,
13280 "attributes ignored on uninstantiated type");
13281 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13282 && ! processing_explicit_instantiation)
4a2849cb 13283 warning (OPT_Wattributes,
13284 "attributes ignored on template instantiation");
13285 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13286 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13287 else
13288 warning (OPT_Wattributes,
13289 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13290 }
13291
0a3b29ad 13292 if (tag_type != enum_type)
13293 cp_parser_check_class_key (tag_type, type);
1157e9f0 13294
13295 /* A "<" cannot follow an elaborated type specifier. If that
13296 happens, the user was probably trying to form a template-id. */
eef0ab03 13297 cp_parser_check_for_invalid_template_id (parser, type, token->location);
1157e9f0 13298
0a3b29ad 13299 return type;
13300}
13301
13302/* Parse an enum-specifier.
13303
13304 enum-specifier:
aa290616 13305 enum-head { enumerator-list [opt] }
13306
13307 enum-head:
13308 enum-key identifier [opt] enum-base [opt]
13309 enum-key nested-name-specifier identifier enum-base [opt]
3f00a6c0 13310
13311 enum-key:
13312 enum
13313 enum class [C++0x]
13314 enum struct [C++0x]
13315
13316 enum-base: [C++0x]
13317 : type-specifier-seq
0a3b29ad 13318
aa290616 13319 opaque-enum-specifier:
13320 enum-key identifier enum-base [opt] ;
13321
fe80e237 13322 GNU Extensions:
3f00a6c0 13323 enum-key attributes[opt] identifier [opt] enum-base [opt]
13324 { enumerator-list [opt] }attributes[opt]
fe80e237 13325
4a2849cb 13326 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13327 if the token stream isn't an enum-specifier after all. */
0a3b29ad 13328
13329static tree
45baea8b 13330cp_parser_enum_specifier (cp_parser* parser)
0a3b29ad 13331{
637fdc50 13332 tree identifier;
aa290616 13333 tree type = NULL_TREE;
13334 tree prev_scope;
13335 tree nested_name_specifier = NULL_TREE;
4a2849cb 13336 tree attributes;
3f00a6c0 13337 bool scoped_enum_p = false;
b706ce78 13338 bool has_underlying_type = false;
aa290616 13339 bool nested_being_defined = false;
13340 bool new_value_list = false;
13341 bool is_new_type = false;
13342 bool is_anonymous = false;
3f00a6c0 13343 tree underlying_type = NULL_TREE;
aa290616 13344 cp_token *type_start_token = NULL;
4a2849cb 13345
13346 /* Parse tentatively so that we can back up if we don't find a
13347 enum-specifier. */
13348 cp_parser_parse_tentatively (parser);
0a3b29ad 13349
637fdc50 13350 /* Caller guarantees that the current token is 'enum', an identifier
13351 possibly follows, and the token after that is an opening brace.
13352 If we don't have an identifier, fabricate an anonymous name for
13353 the enumeration being defined. */
13354 cp_lexer_consume_token (parser->lexer);
0a3b29ad 13355
3f00a6c0 13356 /* Parse the "class" or "struct", which indicates a scoped
13357 enumeration type in C++0x. */
13358 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13359 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13360 {
aa290616 13361 if (cxx_dialect < cxx0x)
bf8d19fe 13362 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
3f00a6c0 13363
13364 /* Consume the `struct' or `class' token. */
13365 cp_lexer_consume_token (parser->lexer);
13366
13367 scoped_enum_p = true;
13368 }
b706ce78 13369
4a2849cb 13370 attributes = cp_parser_attributes_opt (parser);
13371
aa290616 13372 /* Clear the qualification. */
13373 parser->scope = NULL_TREE;
13374 parser->qualifying_scope = NULL_TREE;
13375 parser->object_scope = NULL_TREE;
13376
13377 /* Figure out in what scope the declaration is being placed. */
13378 prev_scope = current_scope ();
13379
13380 type_start_token = cp_lexer_peek_token (parser->lexer);
13381
13382 push_deferring_access_checks (dk_no_check);
13383 nested_name_specifier
13384 = cp_parser_nested_name_specifier_opt (parser,
13385 /*typename_keyword_p=*/true,
13386 /*check_dependency_p=*/false,
13387 /*type_p=*/false,
13388 /*is_declaration=*/false);
13389
13390 if (nested_name_specifier)
13391 {
13392 tree name;
13393
13394 identifier = cp_parser_identifier (parser);
13395 name = cp_parser_lookup_name (parser, identifier,
13396 enum_type,
13397 /*is_template=*/false,
13398 /*is_namespace=*/false,
13399 /*check_dependency=*/true,
13400 /*ambiguous_decls=*/NULL,
13401 input_location);
13402 if (name)
13403 {
13404 type = TREE_TYPE (name);
13405 if (TREE_CODE (type) == TYPENAME_TYPE)
13406 {
13407 /* Are template enums allowed in ISO? */
13408 if (template_parm_scope_p ())
13409 pedwarn (type_start_token->location, OPT_pedantic,
13410 "%qD is an enumeration template", name);
13411 /* ignore a typename reference, for it will be solved by name
13412 in start_enum. */
13413 type = NULL_TREE;
13414 }
13415 }
13416 else
13417 error_at (type_start_token->location,
13418 "%qD is not an enumerator-name", identifier);
13419 }
637fdc50 13420 else
aa290616 13421 {
13422 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13423 identifier = cp_parser_identifier (parser);
13424 else
13425 {
13426 identifier = make_anon_name ();
13427 is_anonymous = true;
13428 }
13429 }
13430 pop_deferring_access_checks ();
0a3b29ad 13431
e72cb433 13432 /* Check for the `:' that denotes a specified underlying type in C++0x.
13433 Note that a ':' could also indicate a bitfield width, however. */
3f00a6c0 13434 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13435 {
13436 cp_decl_specifier_seq type_specifiers;
13437
e72cb433 13438 /* Consume the `:'. */
13439 cp_lexer_consume_token (parser->lexer);
13440
13441 /* Parse the type-specifier-seq. */
c44f7faf 13442 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 13443 /*is_trailing_return=*/false,
e72cb433 13444 &type_specifiers);
13445
b706ce78 13446 /* At this point this is surely not elaborated type specifier. */
13447 if (!cp_parser_parse_definitely (parser))
13448 return NULL_TREE;
13449
aa290616 13450 if (cxx_dialect < cxx0x)
bf8d19fe 13451 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
3f00a6c0 13452
b706ce78 13453 has_underlying_type = true;
13454
3f00a6c0 13455 /* If that didn't work, stop. */
13456 if (type_specifiers.type != error_mark_node)
13457 {
13458 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13459 /*initialized=*/0, NULL);
13460 if (underlying_type == error_mark_node)
13461 underlying_type = NULL_TREE;
13462 }
3f00a6c0 13463 }
13464
4a2849cb 13465 /* Look for the `{' but don't consume it yet. */
13466 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
b706ce78 13467 {
aa290616 13468 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13469 {
13470 cp_parser_error (parser, "expected %<{%>");
13471 if (has_underlying_type)
13472 return NULL_TREE;
13473 }
13474 /* An opaque-enum-specifier must have a ';' here. */
13475 if ((scoped_enum_p || underlying_type)
13476 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13477 {
13478 cp_parser_error (parser, "expected %<;%> or %<{%>");
13479 if (has_underlying_type)
13480 return NULL_TREE;
13481 }
b706ce78 13482 }
4a2849cb 13483
b706ce78 13484 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
4a2849cb 13485 return NULL_TREE;
13486
aa290616 13487 if (nested_name_specifier)
13488 {
13489 if (CLASS_TYPE_P (nested_name_specifier))
13490 {
13491 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13492 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13493 push_scope (nested_name_specifier);
13494 }
13495 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13496 {
13497 push_nested_namespace (nested_name_specifier);
13498 }
13499 }
13500
0a3b29ad 13501 /* Issue an error message if type-definitions are forbidden here. */
9a7c4b43 13502 if (!cp_parser_check_type_definition (parser))
13503 type = error_mark_node;
13504 else
13505 /* Create the new type. We do this before consuming the opening
13506 brace so the enum will be recorded as being on the line of its
13507 tag (or the 'enum' keyword, if there is no tag). */
aa290616 13508 type = start_enum (identifier, type, underlying_type,
13509 scoped_enum_p, &is_new_type);
b9dd3954 13510
aa290616 13511 /* If the next token is not '{' it is an opaque-enum-specifier or an
13512 elaborated-type-specifier. */
13513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4a2849cb 13514 {
aa290616 13515 if (nested_name_specifier)
13516 {
13517 /* The following catches invalid code such as:
13518 enum class S<int>::E { A, B, C }; */
13519 if (!processing_specialization
13520 && CLASS_TYPE_P (nested_name_specifier)
13521 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13522 error_at (type_start_token->location, "cannot add an enumerator "
13523 "list to a template instantiation");
13524
13525 /* If that scope does not contain the scope in which the
13526 class was originally declared, the program is invalid. */
13527 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13528 {
13529 if (at_namespace_scope_p ())
13530 error_at (type_start_token->location,
13531 "declaration of %qD in namespace %qD which does not "
13532 "enclose %qD",
13533 type, prev_scope, nested_name_specifier);
13534 else
13535 error_at (type_start_token->location,
13536 "declaration of %qD in %qD which does not enclose %qD",
13537 type, prev_scope, nested_name_specifier);
13538 type = error_mark_node;
13539 }
13540 }
4a2849cb 13541
aa290616 13542 if (scoped_enum_p)
13543 begin_scope (sk_scoped_enum, type);
637fdc50 13544
aa290616 13545 /* Consume the opening brace. */
13546 cp_lexer_consume_token (parser->lexer);
13547
13548 if (type == error_mark_node)
13549 ; /* Nothing to add */
13550 else if (OPAQUE_ENUM_P (type)
13551 || (cxx_dialect > cxx98 && processing_specialization))
13552 {
13553 new_value_list = true;
13554 SET_OPAQUE_ENUM_P (type, false);
13555 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13556 }
13557 else
13558 {
13559 error_at (type_start_token->location, "multiple definition of %q#T", type);
13560 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13561 "previous definition here");
13562 type = error_mark_node;
13563 }
13564
13565 if (type == error_mark_node)
13566 cp_parser_skip_to_end_of_block_or_statement (parser);
13567 /* If the next token is not '}', then there are some enumerators. */
13568 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13569 cp_parser_enumerator_list (parser, type);
13570
13571 /* Consume the final '}'. */
13572 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13573
13574 if (scoped_enum_p)
13575 finish_scope ();
13576 }
13577 else
13578 {
13579 /* If a ';' follows, then it is an opaque-enum-specifier
13580 and additional restrictions apply. */
13581 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13582 {
13583 if (is_anonymous)
13584 error_at (type_start_token->location,
13585 "opaque-enum-specifier without name");
13586 else if (nested_name_specifier)
13587 error_at (type_start_token->location,
13588 "opaque-enum-specifier must use a simple identifier");
13589 }
13590 }
0a3b29ad 13591
fe80e237 13592 /* Look for trailing attributes to apply to this enumeration, and
93523877 13593 apply them if appropriate. */
fe80e237 13594 if (cp_parser_allow_gnu_extensions_p (parser))
13595 {
13596 tree trailing_attr = cp_parser_attributes_opt (parser);
07dd1387 13597 trailing_attr = chainon (trailing_attr, attributes);
fe80e237 13598 cplus_decl_attributes (&type,
13599 trailing_attr,
13600 (int) ATTR_FLAG_TYPE_IN_PLACE);
13601 }
13602
0a3b29ad 13603 /* Finish up the enumeration. */
aa290616 13604 if (type != error_mark_node)
13605 {
13606 if (new_value_list)
13607 finish_enum_value_list (type);
13608 if (is_new_type)
13609 finish_enum (type);
13610 }
0a3b29ad 13611
aa290616 13612 if (nested_name_specifier)
13613 {
13614 if (CLASS_TYPE_P (nested_name_specifier))
13615 {
13616 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13617 pop_scope (nested_name_specifier);
13618 }
13619 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13620 {
13621 pop_nested_namespace (nested_name_specifier);
13622 }
13623 }
0a3b29ad 13624 return type;
13625}
13626
13627/* Parse an enumerator-list. The enumerators all have the indicated
ccb84981 13628 TYPE.
0a3b29ad 13629
13630 enumerator-list:
13631 enumerator-definition
13632 enumerator-list , enumerator-definition */
13633
13634static void
45baea8b 13635cp_parser_enumerator_list (cp_parser* parser, tree type)
0a3b29ad 13636{
13637 while (true)
13638 {
0a3b29ad 13639 /* Parse an enumerator-definition. */
13640 cp_parser_enumerator_definition (parser, type);
637fdc50 13641
13642 /* If the next token is not a ',', we've reached the end of
13643 the list. */
13644 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 13645 break;
13646 /* Otherwise, consume the `,' and keep going. */
13647 cp_lexer_consume_token (parser->lexer);
13648 /* If the next token is a `}', there is a trailing comma. */
13649 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13650 {
8864917d 13651 if (!in_system_header)
21ca8540 13652 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
0a3b29ad 13653 break;
13654 }
13655 }
13656}
13657
13658/* Parse an enumerator-definition. The enumerator has the indicated
13659 TYPE.
13660
13661 enumerator-definition:
13662 enumerator
13663 enumerator = constant-expression
ccb84981 13664
0a3b29ad 13665 enumerator:
13666 identifier */
13667
13668static void
45baea8b 13669cp_parser_enumerator_definition (cp_parser* parser, tree type)
0a3b29ad 13670{
0a3b29ad 13671 tree identifier;
13672 tree value;
cbdababb 13673 location_t loc;
13674
13675 /* Save the input location because we are interested in the location
13676 of the identifier and not the location of the explicit value. */
13677 loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 13678
13679 /* Look for the identifier. */
13680 identifier = cp_parser_identifier (parser);
13681 if (identifier == error_mark_node)
13682 return;
ccb84981 13683
637fdc50 13684 /* If the next token is an '=', then there is an explicit value. */
13685 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
0a3b29ad 13686 {
13687 /* Consume the `=' token. */
13688 cp_lexer_consume_token (parser->lexer);
13689 /* Parse the value. */
ccb84981 13690 value = cp_parser_constant_expression (parser,
13795292 13691 /*allow_non_constant_p=*/false,
5f6526e1 13692 NULL);
0a3b29ad 13693 }
13694 else
13695 value = NULL_TREE;
13696
ccc5f70d 13697 /* If we are processing a template, make sure the initializer of the
13698 enumerator doesn't contain any bare template parameter pack. */
13699 if (check_for_bare_parameter_packs (value))
13700 value = error_mark_node;
13701
0a3b29ad 13702 /* Create the enumerator. */
cbdababb 13703 build_enumerator (identifier, value, type, loc);
0a3b29ad 13704}
13705
13706/* Parse a namespace-name.
13707
13708 namespace-name:
13709 original-namespace-name
13710 namespace-alias
13711
13712 Returns the NAMESPACE_DECL for the namespace. */
13713
13714static tree
45baea8b 13715cp_parser_namespace_name (cp_parser* parser)
0a3b29ad 13716{
13717 tree identifier;
13718 tree namespace_decl;
13719
ad9ae192 13720 cp_token *token = cp_lexer_peek_token (parser->lexer);
13721
0a3b29ad 13722 /* Get the name of the namespace. */
13723 identifier = cp_parser_identifier (parser);
13724 if (identifier == error_mark_node)
13725 return error_mark_node;
13726
6fc758aa 13727 /* Look up the identifier in the currently active scope. Look only
13728 for namespaces, due to:
13729
13730 [basic.lookup.udir]
13731
13732 When looking up a namespace-name in a using-directive or alias
ccb84981 13733 definition, only namespace names are considered.
6fc758aa 13734
13735 And:
13736
13737 [basic.lookup.qual]
13738
13739 During the lookup of a name preceding the :: scope resolution
ccb84981 13740 operator, object, function, and enumerator names are ignored.
6fc758aa 13741
3f00a6c0 13742 (Note that cp_parser_qualifying_entity only calls this
6fc758aa 13743 function if the token after the name is the scope resolution
13744 operator.) */
13745 namespace_decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 13746 none_type,
c3b9e457 13747 /*is_template=*/false,
6fc758aa 13748 /*is_namespace=*/true,
2cdbcd51 13749 /*check_dependency=*/true,
ad9ae192 13750 /*ambiguous_decls=*/NULL,
13751 token->location);
0a3b29ad 13752 /* If it's not a namespace, issue an error. */
13753 if (namespace_decl == error_mark_node
13754 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13755 {
f75d14ca 13756 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 13757 error_at (token->location, "%qD is not a namespace-name", identifier);
0a3b29ad 13758 cp_parser_error (parser, "expected namespace-name");
13759 namespace_decl = error_mark_node;
13760 }
ccb84981 13761
0a3b29ad 13762 return namespace_decl;
13763}
13764
13765/* Parse a namespace-definition.
13766
13767 namespace-definition:
13768 named-namespace-definition
ccb84981 13769 unnamed-namespace-definition
0a3b29ad 13770
13771 named-namespace-definition:
13772 original-namespace-definition
13773 extension-namespace-definition
13774
13775 original-namespace-definition:
13776 namespace identifier { namespace-body }
ccb84981 13777
0a3b29ad 13778 extension-namespace-definition:
13779 namespace original-namespace-name { namespace-body }
ccb84981 13780
0a3b29ad 13781 unnamed-namespace-definition:
13782 namespace { namespace-body } */
13783
13784static void
45baea8b 13785cp_parser_namespace_definition (cp_parser* parser)
0a3b29ad 13786{
799435d8 13787 tree identifier, attribs;
1fd77946 13788 bool has_visibility;
93635a8e 13789 bool is_inline;
13790
13791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13792 {
af28195d 13793 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
93635a8e 13794 is_inline = true;
13795 cp_lexer_consume_token (parser->lexer);
13796 }
13797 else
13798 is_inline = false;
0a3b29ad 13799
13800 /* Look for the `namespace' keyword. */
c247dce0 13801 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 13802
13803 /* Get the name of the namespace. We do not attempt to distinguish
13804 between an original-namespace-definition and an
13805 extension-namespace-definition at this point. The semantic
13806 analysis routines are responsible for that. */
13807 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13808 identifier = cp_parser_identifier (parser);
13809 else
13810 identifier = NULL_TREE;
13811
799435d8 13812 /* Parse any specified attributes. */
13813 attribs = cp_parser_attributes_opt (parser);
13814
0a3b29ad 13815 /* Look for the `{' to start the namespace. */
c247dce0 13816 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
0a3b29ad 13817 /* Start the namespace. */
1fd77946 13818 push_namespace (identifier);
13819
93635a8e 13820 /* "inline namespace" is equivalent to a stub namespace definition
13821 followed by a strong using directive. */
13822 if (is_inline)
13823 {
607a5d68 13824 tree name_space = current_namespace;
93635a8e 13825 /* Set up namespace association. */
607a5d68 13826 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13827 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13828 DECL_NAMESPACE_ASSOCIATIONS (name_space));
93635a8e 13829 /* Import the contents of the inline namespace. */
13830 pop_namespace ();
607a5d68 13831 do_using_directive (name_space);
93635a8e 13832 push_namespace (identifier);
13833 }
13834
1fd77946 13835 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13836
0a3b29ad 13837 /* Parse the body of the namespace. */
13838 cp_parser_namespace_body (parser);
1fd77946 13839
1fd77946 13840 if (has_visibility)
f1658ae0 13841 pop_visibility (1);
1fd77946 13842
0a3b29ad 13843 /* Finish the namespace. */
13844 pop_namespace ();
13845 /* Look for the final `}'. */
c247dce0 13846 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 13847}
13848
13849/* Parse a namespace-body.
13850
13851 namespace-body:
13852 declaration-seq [opt] */
13853
13854static void
45baea8b 13855cp_parser_namespace_body (cp_parser* parser)
0a3b29ad 13856{
13857 cp_parser_declaration_seq_opt (parser);
13858}
13859
13860/* Parse a namespace-alias-definition.
13861
13862 namespace-alias-definition:
13863 namespace identifier = qualified-namespace-specifier ; */
13864
13865static void
45baea8b 13866cp_parser_namespace_alias_definition (cp_parser* parser)
0a3b29ad 13867{
13868 tree identifier;
13869 tree namespace_specifier;
13870
ad9ae192 13871 cp_token *token = cp_lexer_peek_token (parser->lexer);
13872
0a3b29ad 13873 /* Look for the `namespace' keyword. */
c247dce0 13874 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 13875 /* Look for the identifier. */
13876 identifier = cp_parser_identifier (parser);
13877 if (identifier == error_mark_node)
13878 return;
13879 /* Look for the `=' token. */
26d3c536 13880 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13881 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13882 {
ccb59bb6 13883 error_at (token->location, "%<namespace%> definition is not allowed here");
26d3c536 13884 /* Skip the definition. */
13885 cp_lexer_consume_token (parser->lexer);
9aad947b 13886 if (cp_parser_skip_to_closing_brace (parser))
13887 cp_lexer_consume_token (parser->lexer);
26d3c536 13888 return;
13889 }
c247dce0 13890 cp_parser_require (parser, CPP_EQ, RT_EQ);
0a3b29ad 13891 /* Look for the qualified-namespace-specifier. */
ccb84981 13892 namespace_specifier
0a3b29ad 13893 = cp_parser_qualified_namespace_specifier (parser);
13894 /* Look for the `;' token. */
c247dce0 13895 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 13896
13897 /* Register the alias in the symbol table. */
13898 do_namespace_alias (identifier, namespace_specifier);
13899}
13900
13901/* Parse a qualified-namespace-specifier.
13902
13903 qualified-namespace-specifier:
13904 :: [opt] nested-name-specifier [opt] namespace-name
13905
13906 Returns a NAMESPACE_DECL corresponding to the specified
13907 namespace. */
13908
13909static tree
45baea8b 13910cp_parser_qualified_namespace_specifier (cp_parser* parser)
0a3b29ad 13911{
13912 /* Look for the optional `::'. */
ccb84981 13913 cp_parser_global_scope_opt (parser,
130bb1d4 13914 /*current_scope_valid_p=*/false);
0a3b29ad 13915
13916 /* Look for the optional nested-name-specifier. */
13917 cp_parser_nested_name_specifier_opt (parser,
13918 /*typename_keyword_p=*/false,
13919 /*check_dependency_p=*/true,
3d0f901b 13920 /*type_p=*/false,
13921 /*is_declaration=*/true);
0a3b29ad 13922
13923 return cp_parser_namespace_name (parser);
13924}
13925
da2a3271 13926/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13927 access declaration.
0a3b29ad 13928
13929 using-declaration:
13930 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
da2a3271 13931 using :: unqualified-id ;
0a3b29ad 13932
da2a3271 13933 access-declaration:
13934 qualified-id ;
13935
13936 */
13937
13938static bool
13939cp_parser_using_declaration (cp_parser* parser,
13940 bool access_declaration_p)
0a3b29ad 13941{
13942 cp_token *token;
13943 bool typename_p = false;
13944 bool global_scope_p;
13945 tree decl;
13946 tree identifier;
2c47ecdb 13947 tree qscope;
0a3b29ad 13948
da2a3271 13949 if (access_declaration_p)
13950 cp_parser_parse_tentatively (parser);
13951 else
0a3b29ad 13952 {
da2a3271 13953 /* Look for the `using' keyword. */
c247dce0 13954 cp_parser_require_keyword (parser, RID_USING, RT_USING);
da2a3271 13955
13956 /* Peek at the next token. */
13957 token = cp_lexer_peek_token (parser->lexer);
13958 /* See if it's `typename'. */
13959 if (token->keyword == RID_TYPENAME)
13960 {
13961 /* Remember that we've seen it. */
13962 typename_p = true;
13963 /* Consume the `typename' token. */
13964 cp_lexer_consume_token (parser->lexer);
13965 }
0a3b29ad 13966 }
13967
13968 /* Look for the optional global scope qualification. */
ccb84981 13969 global_scope_p
0a3b29ad 13970 = (cp_parser_global_scope_opt (parser,
130bb1d4 13971 /*current_scope_valid_p=*/false)
0a3b29ad 13972 != NULL_TREE);
13973
13974 /* If we saw `typename', or didn't see `::', then there must be a
13975 nested-name-specifier present. */
13976 if (typename_p || !global_scope_p)
ccb84981 13977 qscope = cp_parser_nested_name_specifier (parser, typename_p,
2c47ecdb 13978 /*check_dependency_p=*/true,
13979 /*type_p=*/false,
13980 /*is_declaration=*/true);
0a3b29ad 13981 /* Otherwise, we could be in either of the two productions. In that
13982 case, treat the nested-name-specifier as optional. */
13983 else
2c47ecdb 13984 qscope = cp_parser_nested_name_specifier_opt (parser,
13985 /*typename_keyword_p=*/false,
13986 /*check_dependency_p=*/true,
13987 /*type_p=*/false,
13988 /*is_declaration=*/true);
13989 if (!qscope)
13990 qscope = global_namespace;
0a3b29ad 13991
7638e10c 13992 if (access_declaration_p && cp_parser_error_occurred (parser))
13993 /* Something has already gone wrong; there's no need to parse
13994 further. Since an error has occurred, the return value of
13995 cp_parser_parse_definitely will be false, as required. */
13996 return cp_parser_parse_definitely (parser);
13997
ad9ae192 13998 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13999 /* Parse the unqualified-id. */
ccb84981 14000 identifier = cp_parser_unqualified_id (parser,
0a3b29ad 14001 /*template_keyword_p=*/false,
899cc6e8 14002 /*check_dependency_p=*/true,
197c9df7 14003 /*declarator_p=*/true,
130bb1d4 14004 /*optional_p=*/false);
0a3b29ad 14005
da2a3271 14006 if (access_declaration_p)
14007 {
14008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14009 cp_parser_simulate_error (parser);
14010 if (!cp_parser_parse_definitely (parser))
14011 return false;
14012 }
14013
0a3b29ad 14014 /* The function we call to handle a using-declaration is different
14015 depending on what scope we are in. */
c91d0d7d 14016 if (qscope == error_mark_node || identifier == error_mark_node)
899cc6e8 14017 ;
14018 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14019 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14020 /* [namespace.udecl]
14021
14022 A using declaration shall not name a template-id. */
ccb59bb6 14023 error_at (token->location,
14024 "a template-id may not appear in a using-declaration");
0a3b29ad 14025 else
14026 {
46f43a6b 14027 if (at_class_scope_p ())
642a03ff 14028 {
899cc6e8 14029 /* Create the USING_DECL. */
2ded3667 14030 decl = do_class_using_decl (parser->scope, identifier);
613687b1 14031
830a6615 14032 if (check_for_bare_parameter_packs (decl))
613687b1 14033 return false;
14034 else
14035 /* Add it to the list of members in this class. */
14036 finish_member_declaration (decl);
642a03ff 14037 }
0a3b29ad 14038 else
899cc6e8 14039 {
ad9ae192 14040 decl = cp_parser_lookup_name_simple (parser,
14041 identifier,
14042 token->location);
899cc6e8 14043 if (decl == error_mark_node)
ad9ae192 14044 cp_parser_name_lookup_error (parser, identifier,
c247dce0 14045 decl, NLE_NULL,
ad9ae192 14046 token->location);
830a6615 14047 else if (check_for_bare_parameter_packs (decl))
613687b1 14048 return false;
46f43a6b 14049 else if (!at_namespace_scope_p ())
2c47ecdb 14050 do_local_using_decl (decl, qscope, identifier);
899cc6e8 14051 else
2c47ecdb 14052 do_toplevel_using_decl (decl, qscope, identifier);
899cc6e8 14053 }
0a3b29ad 14054 }
14055
14056 /* Look for the final `;'. */
c247dce0 14057 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
da2a3271 14058
14059 return true;
0a3b29ad 14060}
14061
ccb84981 14062/* Parse a using-directive.
14063
0a3b29ad 14064 using-directive:
14065 using namespace :: [opt] nested-name-specifier [opt]
14066 namespace-name ; */
14067
14068static void
45baea8b 14069cp_parser_using_directive (cp_parser* parser)
0a3b29ad 14070{
14071 tree namespace_decl;
a5ed46c9 14072 tree attribs;
0a3b29ad 14073
14074 /* Look for the `using' keyword. */
c247dce0 14075 cp_parser_require_keyword (parser, RID_USING, RT_USING);
0a3b29ad 14076 /* And the `namespace' keyword. */
c247dce0 14077 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 14078 /* Look for the optional `::' operator. */
130bb1d4 14079 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
755edffd 14080 /* And the optional nested-name-specifier. */
0a3b29ad 14081 cp_parser_nested_name_specifier_opt (parser,
14082 /*typename_keyword_p=*/false,
14083 /*check_dependency_p=*/true,
3d0f901b 14084 /*type_p=*/false,
14085 /*is_declaration=*/true);
0a3b29ad 14086 /* Get the namespace being used. */
14087 namespace_decl = cp_parser_namespace_name (parser);
a5ed46c9 14088 /* And any specified attributes. */
14089 attribs = cp_parser_attributes_opt (parser);
0a3b29ad 14090 /* Update the symbol table. */
a5ed46c9 14091 parse_using_directive (namespace_decl, attribs);
0a3b29ad 14092 /* Look for the final `;'. */
c247dce0 14093 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 14094}
14095
14096/* Parse an asm-definition.
14097
14098 asm-definition:
ccb84981 14099 asm ( string-literal ) ;
0a3b29ad 14100
14101 GNU Extension:
14102
14103 asm-definition:
14104 asm volatile [opt] ( string-literal ) ;
14105 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14106 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 14107 : asm-operand-list [opt] ) ;
ccb84981 14108 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 14109 : asm-operand-list [opt]
78f55ca8 14110 : asm-clobber-list [opt] ) ;
14111 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14112 : asm-clobber-list [opt]
14113 : asm-goto-list ) ; */
0a3b29ad 14114
14115static void
45baea8b 14116cp_parser_asm_definition (cp_parser* parser)
0a3b29ad 14117{
0a3b29ad 14118 tree string;
14119 tree outputs = NULL_TREE;
14120 tree inputs = NULL_TREE;
14121 tree clobbers = NULL_TREE;
78f55ca8 14122 tree labels = NULL_TREE;
0a3b29ad 14123 tree asm_stmt;
14124 bool volatile_p = false;
14125 bool extended_p = false;
a40e70de 14126 bool invalid_inputs_p = false;
14127 bool invalid_outputs_p = false;
78f55ca8 14128 bool goto_p = false;
c61e1212 14129 required_token missing = RT_NONE;
0a3b29ad 14130
14131 /* Look for the `asm' keyword. */
c247dce0 14132 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
0a3b29ad 14133 /* See if the next token is `volatile'. */
14134 if (cp_parser_allow_gnu_extensions_p (parser)
14135 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14136 {
14137 /* Remember that we saw the `volatile' keyword. */
14138 volatile_p = true;
14139 /* Consume the token. */
14140 cp_lexer_consume_token (parser->lexer);
14141 }
78f55ca8 14142 if (cp_parser_allow_gnu_extensions_p (parser)
14143 && parser->in_function_body
14144 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14145 {
14146 /* Remember that we saw the `goto' keyword. */
14147 goto_p = true;
14148 /* Consume the token. */
14149 cp_lexer_consume_token (parser->lexer);
14150 }
0a3b29ad 14151 /* Look for the opening `('. */
c247dce0 14152 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
00d26680 14153 return;
0a3b29ad 14154 /* Look for the string. */
00d26680 14155 string = cp_parser_string_literal (parser, false, false);
14156 if (string == error_mark_node)
14157 {
14158 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14159 /*consume_paren=*/true);
14160 return;
14161 }
14162
0a3b29ad 14163 /* If we're allowing GNU extensions, check for the extended assembly
ccb84981 14164 syntax. Unfortunately, the `:' tokens need not be separated by
0a3b29ad 14165 a space in C, and so, for compatibility, we tolerate that here
14166 too. Doing that means that we have to treat the `::' operator as
14167 two `:' tokens. */
14168 if (cp_parser_allow_gnu_extensions_p (parser)
0aeb1cc5 14169 && parser->in_function_body
0a3b29ad 14170 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14171 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14172 {
14173 bool inputs_p = false;
14174 bool clobbers_p = false;
78f55ca8 14175 bool labels_p = false;
0a3b29ad 14176
14177 /* The extended syntax was used. */
14178 extended_p = true;
14179
14180 /* Look for outputs. */
14181 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14182 {
14183 /* Consume the `:'. */
14184 cp_lexer_consume_token (parser->lexer);
14185 /* Parse the output-operands. */
ccb84981 14186 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 14187 CPP_COLON)
14188 && cp_lexer_next_token_is_not (parser->lexer,
f2050a0c 14189 CPP_SCOPE)
14190 && cp_lexer_next_token_is_not (parser->lexer,
78f55ca8 14191 CPP_CLOSE_PAREN)
14192 && !goto_p)
0a3b29ad 14193 outputs = cp_parser_asm_operand_list (parser);
a40e70de 14194
14195 if (outputs == error_mark_node)
14196 invalid_outputs_p = true;
0a3b29ad 14197 }
14198 /* If the next token is `::', there are no outputs, and the
14199 next token is the beginning of the inputs. */
14200 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8f26d225 14201 /* The inputs are coming next. */
14202 inputs_p = true;
0a3b29ad 14203
14204 /* Look for inputs. */
14205 if (inputs_p
14206 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14207 {
8f26d225 14208 /* Consume the `:' or `::'. */
14209 cp_lexer_consume_token (parser->lexer);
0a3b29ad 14210 /* Parse the output-operands. */
ccb84981 14211 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 14212 CPP_COLON)
78f55ca8 14213 && cp_lexer_next_token_is_not (parser->lexer,
14214 CPP_SCOPE)
f2050a0c 14215 && cp_lexer_next_token_is_not (parser->lexer,
14216 CPP_CLOSE_PAREN))
0a3b29ad 14217 inputs = cp_parser_asm_operand_list (parser);
a40e70de 14218
14219 if (inputs == error_mark_node)
14220 invalid_inputs_p = true;
0a3b29ad 14221 }
14222 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14223 /* The clobbers are coming next. */
14224 clobbers_p = true;
14225
14226 /* Look for clobbers. */
ccb84981 14227 if (clobbers_p
0a3b29ad 14228 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14229 {
78f55ca8 14230 clobbers_p = true;
8f26d225 14231 /* Consume the `:' or `::'. */
14232 cp_lexer_consume_token (parser->lexer);
0a3b29ad 14233 /* Parse the clobbers. */
f2050a0c 14234 if (cp_lexer_next_token_is_not (parser->lexer,
78f55ca8 14235 CPP_COLON)
14236 && cp_lexer_next_token_is_not (parser->lexer,
14237 CPP_CLOSE_PAREN))
f2050a0c 14238 clobbers = cp_parser_asm_clobber_list (parser);
0a3b29ad 14239 }
78f55ca8 14240 else if (goto_p
14241 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14242 /* The labels are coming next. */
14243 labels_p = true;
14244
14245 /* Look for labels. */
14246 if (labels_p
14247 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14248 {
14249 labels_p = true;
14250 /* Consume the `:' or `::'. */
14251 cp_lexer_consume_token (parser->lexer);
14252 /* Parse the labels. */
14253 labels = cp_parser_asm_label_list (parser);
14254 }
14255
14256 if (goto_p && !labels_p)
c247dce0 14257 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
0a3b29ad 14258 }
78f55ca8 14259 else if (goto_p)
c247dce0 14260 missing = RT_COLON_SCOPE;
78f55ca8 14261
0a3b29ad 14262 /* Look for the closing `)'. */
78f55ca8 14263 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
c247dce0 14264 missing ? missing : RT_CLOSE_PAREN))
3d0f901b 14265 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14266 /*consume_paren=*/true);
c247dce0 14267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 14268
a40e70de 14269 if (!invalid_inputs_p && !invalid_outputs_p)
0a3b29ad 14270 {
a40e70de 14271 /* Create the ASM_EXPR. */
14272 if (parser->in_function_body)
36d35193 14273 {
a40e70de 14274 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
78f55ca8 14275 inputs, clobbers, labels);
a40e70de 14276 /* If the extended syntax was not used, mark the ASM_EXPR. */
14277 if (!extended_p)
14278 {
14279 tree temp = asm_stmt;
14280 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14281 temp = TREE_OPERAND (temp, 0);
9031d10b 14282
a40e70de 14283 ASM_INPUT_P (temp) = 1;
14284 }
36d35193 14285 }
a40e70de 14286 else
14287 cgraph_add_asm_node (string);
0a3b29ad 14288 }
0a3b29ad 14289}
14290
14291/* Declarators [gram.dcl.decl] */
14292
14293/* Parse an init-declarator.
14294
14295 init-declarator:
14296 declarator initializer [opt]
14297
14298 GNU Extension:
14299
14300 init-declarator:
14301 declarator asm-specification [opt] attributes [opt] initializer [opt]
14302
92b128ed 14303 function-definition:
14304 decl-specifier-seq [opt] declarator ctor-initializer [opt]
ccb84981 14305 function-body
14306 decl-specifier-seq [opt] declarator function-try-block
92b128ed 14307
14308 GNU Extension:
14309
14310 function-definition:
ccb84981 14311 __extension__ function-definition
92b128ed 14312
23010bc8 14313 The DECL_SPECIFIERS apply to this declarator. Returns a
14314 representation of the entity declared. If MEMBER_P is TRUE, then
14315 this declarator appears in a class scope. The new DECL created by
14316 this declarator is returned.
14317
14318 The CHECKS are access checks that should be performed once we know
14319 what entity is being declared (and, therefore, what classes have
14320 befriended it).
0a3b29ad 14321
14322 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14323 for a function-definition here as well. If the declarator is a
14324 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14325 be TRUE upon return. By that point, the function-definition will
14326 have been completely parsed.
14327
14328 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14329 is FALSE. */
14330
14331static tree
ccb84981 14332cp_parser_init_declarator (cp_parser* parser,
4b9b2871 14333 cp_decl_specifier_seq *decl_specifiers,
3369eb76 14334 VEC (deferred_access_check,gc)* checks,
45baea8b 14335 bool function_definition_allowed_p,
14336 bool member_p,
8172be22 14337 int declares_class_or_enum,
45baea8b 14338 bool* function_definition_p)
0a3b29ad 14339{
ad9ae192 14340 cp_token *token = NULL, *asm_spec_start_token = NULL,
14341 *attributes_start_token = NULL;
3046c0a3 14342 cp_declarator *declarator;
4b9b2871 14343 tree prefix_attributes;
0a3b29ad 14344 tree attributes;
14345 tree asm_specification;
14346 tree initializer;
14347 tree decl = NULL_TREE;
14348 tree scope;
2336da2a 14349 int is_initialized;
393f878f 14350 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14351 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14352 "(...)". */
14353 enum cpp_ttype initialization_kind;
f82f1250 14354 bool is_direct_init = false;
878870b4 14355 bool is_non_constant_init;
0986fa22 14356 int ctor_dtor_or_conv_p;
0a3b29ad 14357 bool friend_p;
7f602bca 14358 tree pushed_scope = NULL;
0a3b29ad 14359
4b9b2871 14360 /* Gather the attributes that were provided with the
14361 decl-specifiers. */
14362 prefix_attributes = decl_specifiers->attributes;
4b9b2871 14363
0a3b29ad 14364 /* Assume that this is not the declarator for a function
14365 definition. */
14366 if (function_definition_p)
14367 *function_definition_p = false;
14368
14369 /* Defer access checks while parsing the declarator; we cannot know
ccb84981 14370 what names are accessible until we know what is being
0a3b29ad 14371 declared. */
9b57b06b 14372 resume_deferring_access_checks ();
14373
0a3b29ad 14374 /* Parse the declarator. */
ad9ae192 14375 token = cp_lexer_peek_token (parser->lexer);
ccb84981 14376 declarator
42bbd0ec 14377 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 14378 &ctor_dtor_or_conv_p,
08ea345c 14379 /*parenthesized_p=*/NULL,
14380 /*member_p=*/false);
0a3b29ad 14381 /* Gather up the deferred checks. */
9b57b06b 14382 stop_deferring_access_checks ();
7488d745 14383
0a3b29ad 14384 /* If the DECLARATOR was erroneous, there's no need to go
14385 further. */
3046c0a3 14386 if (declarator == cp_error_declarator)
9b57b06b 14387 return error_mark_node;
0a3b29ad 14388
04ef83b7 14389 /* Check that the number of template-parameter-lists is OK. */
ad9ae192 14390 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14391 token->location))
04ef83b7 14392 return error_mark_node;
14393
e2ae55f2 14394 if (declares_class_or_enum & 2)
14395 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 14396 decl_specifiers->type,
14397 decl_specifiers->type_location);
8172be22 14398
0a3b29ad 14399 /* Figure out what scope the entity declared by the DECLARATOR is
14400 located in. `grokdeclarator' sometimes changes the scope, so
14401 we compute it now. */
14402 scope = get_scope_of_declarator (declarator);
14403
8d2e1854 14404 /* Perform any lookups in the declared type which were thought to be
14405 dependent, but are not in the scope of the declarator. */
14406 decl_specifiers->type
14407 = maybe_update_decl_type (decl_specifiers->type, scope);
14408
0a3b29ad 14409 /* If we're allowing GNU extensions, look for an asm-specification
14410 and attributes. */
14411 if (cp_parser_allow_gnu_extensions_p (parser))
14412 {
14413 /* Look for an asm-specification. */
ad9ae192 14414 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 14415 asm_specification = cp_parser_asm_specification_opt (parser);
14416 /* And attributes. */
ad9ae192 14417 attributes_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 14418 attributes = cp_parser_attributes_opt (parser);
14419 }
14420 else
14421 {
14422 asm_specification = NULL_TREE;
14423 attributes = NULL_TREE;
14424 }
14425
14426 /* Peek at the next token. */
14427 token = cp_lexer_peek_token (parser->lexer);
14428 /* Check to see if the token indicates the start of a
14429 function-definition. */
f82f1250 14430 if (function_declarator_p (declarator)
14431 && cp_parser_token_starts_function_definition_p (token))
0a3b29ad 14432 {
14433 if (!function_definition_allowed_p)
14434 {
14435 /* If a function-definition should not appear here, issue an
14436 error message. */
14437 cp_parser_error (parser,
14438 "a function-definition is not allowed here");
14439 return error_mark_node;
14440 }
14441 else
14442 {
d19f0a18 14443 location_t func_brace_location
14444 = cp_lexer_peek_token (parser->lexer)->location;
14445
0a3b29ad 14446 /* Neither attributes nor an asm-specification are allowed
14447 on a function-definition. */
14448 if (asm_specification)
ccb59bb6 14449 error_at (asm_spec_start_token->location,
14450 "an asm-specification is not allowed "
14451 "on a function-definition");
0a3b29ad 14452 if (attributes)
ccb59bb6 14453 error_at (attributes_start_token->location,
14454 "attributes are not allowed on a function-definition");
0a3b29ad 14455 /* This is a function-definition. */
14456 *function_definition_p = true;
14457
0a3b29ad 14458 /* Parse the function definition. */
92b128ed 14459 if (member_p)
14460 decl = cp_parser_save_member_function_body (parser,
14461 decl_specifiers,
14462 declarator,
14463 prefix_attributes);
14464 else
ccb84981 14465 decl
92b128ed 14466 = (cp_parser_function_definition_from_specifiers_and_declarator
14467 (parser, decl_specifiers, prefix_attributes, declarator));
7488d745 14468
d19f0a18 14469 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14470 {
14471 /* This is where the prologue starts... */
14472 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14473 = func_brace_location;
14474 }
14475
0a3b29ad 14476 return decl;
14477 }
14478 }
14479
14480 /* [dcl.dcl]
14481
14482 Only in function declarations for constructors, destructors, and
ccb84981 14483 type conversions can the decl-specifier-seq be omitted.
0a3b29ad 14484
14485 We explicitly postpone this check past the point where we handle
14486 function-definitions because we tolerate function-definitions
14487 that are missing their return types in some modes. */
4b9b2871 14488 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
0a3b29ad 14489 {
ccb84981 14490 cp_parser_error (parser,
0a3b29ad 14491 "expected constructor, destructor, or type conversion");
14492 return error_mark_node;
14493 }
14494
f82f1250 14495 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
393f878f 14496 if (token->type == CPP_EQ
f82f1250 14497 || token->type == CPP_OPEN_PAREN
14498 || token->type == CPP_OPEN_BRACE)
0a3b29ad 14499 {
16f0449a 14500 is_initialized = SD_INITIALIZED;
393f878f 14501 initialization_kind = token->type;
2336da2a 14502
14503 if (token->type == CPP_EQ
14504 && function_declarator_p (declarator))
14505 {
14506 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14507 if (t2->keyword == RID_DEFAULT)
16f0449a 14508 is_initialized = SD_DEFAULTED;
2336da2a 14509 else if (t2->keyword == RID_DELETE)
16f0449a 14510 is_initialized = SD_DELETED;
2336da2a 14511 }
393f878f 14512 }
14513 else
14514 {
14515 /* If the init-declarator isn't initialized and isn't followed by a
14516 `,' or `;', it's not a valid init-declarator. */
14517 if (token->type != CPP_COMMA
14518 && token->type != CPP_SEMICOLON)
14519 {
14520 cp_parser_error (parser, "expected initializer");
14521 return error_mark_node;
14522 }
16f0449a 14523 is_initialized = SD_UNINITIALIZED;
393f878f 14524 initialization_kind = CPP_EOF;
0a3b29ad 14525 }
14526
14527 /* Because start_decl has side-effects, we should only call it if we
14528 know we're going ahead. By this point, we know that we cannot
14529 possibly be looking at any other construct. */
14530 cp_parser_commit_to_tentative_parse (parser);
14531
396d2731 14532 /* If the decl specifiers were bad, issue an error now that we're
14533 sure this was intended to be a declarator. Then continue
14534 declaring the variable(s), as int, to try to cut down on further
14535 errors. */
4b9b2871 14536 if (decl_specifiers->any_specifiers_p
14537 && decl_specifiers->type == error_mark_node)
396d2731 14538 {
14539 cp_parser_error (parser, "invalid type in declaration");
4b9b2871 14540 decl_specifiers->type = integer_type_node;
396d2731 14541 }
14542
0a3b29ad 14543 /* Check to see whether or not this declaration is a friend. */
14544 friend_p = cp_parser_friend_p (decl_specifiers);
14545
0a3b29ad 14546 /* Enter the newly declared entry in the symbol table. If we're
14547 processing a declaration in a class-specifier, we wait until
14548 after processing the initializer. */
14549 if (!member_p)
14550 {
14551 if (parser->in_unbraced_linkage_specification_p)
3b289c9c 14552 decl_specifiers->storage_class = sc_extern;
8512e308 14553 decl = start_decl (declarator, decl_specifiers,
91caa6ca 14554 is_initialized, attributes, prefix_attributes,
7f602bca 14555 &pushed_scope);
0a683683 14556 /* Adjust location of decl if declarator->id_loc is more appropriate:
14557 set, and decl wasn't merged with another decl, in which case its
14558 location would be different from input_location, and more accurate. */
14559 if (DECL_P (decl)
14560 && declarator->id_loc != UNKNOWN_LOCATION
14561 && DECL_SOURCE_LOCATION (decl) == input_location)
14562 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
0a3b29ad 14563 }
91caa6ca 14564 else if (scope)
14565 /* Enter the SCOPE. That way unqualified names appearing in the
14566 initializer will be looked up in SCOPE. */
7f602bca 14567 pushed_scope = push_scope (scope);
0a3b29ad 14568
14569 /* Perform deferred access control checks, now that we know in which
14570 SCOPE the declared entity resides. */
ccb84981 14571 if (!member_p && decl)
0a3b29ad 14572 {
14573 tree saved_current_function_decl = NULL_TREE;
14574
14575 /* If the entity being declared is a function, pretend that we
14576 are in its scope. If it is a `friend', it may have access to
6beb3f76 14577 things that would not otherwise be accessible. */
0a3b29ad 14578 if (TREE_CODE (decl) == FUNCTION_DECL)
14579 {
14580 saved_current_function_decl = current_function_decl;
14581 current_function_decl = decl;
14582 }
ccb84981 14583
23010bc8 14584 /* Perform access checks for template parameters. */
14585 cp_parser_perform_template_parameter_access_checks (checks);
14586
9b57b06b 14587 /* Perform the access control checks for the declarator and the
08cc44e7 14588 decl-specifiers. */
9b57b06b 14589 perform_deferred_access_checks ();
0a3b29ad 14590
14591 /* Restore the saved value. */
14592 if (TREE_CODE (decl) == FUNCTION_DECL)
14593 current_function_decl = saved_current_function_decl;
14594 }
14595
14596 /* Parse the initializer. */
9bbeacce 14597 initializer = NULL_TREE;
f82f1250 14598 is_direct_init = false;
9bbeacce 14599 is_non_constant_init = true;
0a3b29ad 14600 if (is_initialized)
393f878f 14601 {
95f80464 14602 if (function_declarator_p (declarator))
14603 {
ad9ae192 14604 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
95f80464 14605 if (initialization_kind == CPP_EQ)
14606 initializer = cp_parser_pure_specifier (parser);
14607 else
14608 {
14609 /* If the declaration was erroneous, we don't really
14610 know what the user intended, so just silently
14611 consume the initializer. */
14612 if (decl != error_mark_node)
ccb59bb6 14613 error_at (initializer_start_token->location,
14614 "initializer provided for function");
95f80464 14615 cp_parser_skip_to_closing_parenthesis (parser,
14616 /*recovering=*/true,
14617 /*or_comma=*/false,
14618 /*consume_paren=*/true);
14619 }
14620 }
393f878f 14621 else
a8b75081 14622 {
14623 /* We want to record the extra mangling scope for in-class
14624 initializers of class members and initializers of static data
14625 member templates. The former is a C++0x feature which isn't
14626 implemented yet, and I expect it will involve deferring
14627 parsing of the initializer until end of class as with default
14628 arguments. So right here we only handle the latter. */
14629 if (!member_p && processing_template_decl)
14630 start_lambda_scope (decl);
14631 initializer = cp_parser_initializer (parser,
14632 &is_direct_init,
14633 &is_non_constant_init);
14634 if (!member_p && processing_template_decl)
14635 finish_lambda_scope ();
14636 }
393f878f 14637 }
0a3b29ad 14638
14639 /* The old parser allows attributes to appear after a parenthesized
14640 initializer. Mark Mitchell proposed removing this functionality
14641 on the GCC mailing lists on 2002-08-13. This parser accepts the
14642 attributes -- but ignores them. */
f82f1250 14643 if (cp_parser_allow_gnu_extensions_p (parser)
14644 && initialization_kind == CPP_OPEN_PAREN)
0a3b29ad 14645 if (cp_parser_attributes_opt (parser))
9b2d6d13 14646 warning (OPT_Wattributes,
14647 "attributes after parenthesized initializer ignored");
0a3b29ad 14648
0a3b29ad 14649 /* For an in-class declaration, use `grokfield' to create the
14650 declaration. */
14651 if (member_p)
69b6679c 14652 {
7f602bca 14653 if (pushed_scope)
f6781b9a 14654 {
7f602bca 14655 pop_scope (pushed_scope);
14656 pushed_scope = false;
f6781b9a 14657 }
69b6679c 14658 decl = grokfield (declarator, decl_specifiers,
d91303a6 14659 initializer, !is_non_constant_init,
14660 /*asmspec=*/NULL_TREE,
d246ef36 14661 prefix_attributes);
69b6679c 14662 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14663 cp_parser_save_default_args (parser, decl);
14664 }
ccb84981 14665
0a3b29ad 14666 /* Finish processing the declaration. But, skip friend
14667 declarations. */
d1cd2603 14668 if (!friend_p && decl && decl != error_mark_node)
91caa6ca 14669 {
14670 cp_finish_decl (decl,
d91303a6 14671 initializer, !is_non_constant_init,
91caa6ca 14672 asm_specification,
14673 /* If the initializer is in parentheses, then this is
14674 a direct-initialization, which means that an
14675 `explicit' constructor is OK. Otherwise, an
14676 `explicit' constructor cannot be used. */
f82f1250 14677 ((is_direct_init || !is_initialized)
a06e700f 14678 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
91caa6ca 14679 }
6dcdb5de 14680 else if ((cxx_dialect != cxx98) && friend_p
14681 && decl && TREE_CODE (decl) == FUNCTION_DECL)
82d31768 14682 /* Core issue #226 (C++0x only): A default template-argument
14683 shall not be specified in a friend class template
14684 declaration. */
14685 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14686 /*is_partial=*/0, /*is_friend_decl=*/1);
14687
7f602bca 14688 if (!friend_p && pushed_scope)
14689 pop_scope (pushed_scope);
0a3b29ad 14690
14691 return decl;
14692}
14693
14694/* Parse a declarator.
ccb84981 14695
0a3b29ad 14696 declarator:
14697 direct-declarator
ccb84981 14698 ptr-operator declarator
0a3b29ad 14699
14700 abstract-declarator:
14701 ptr-operator abstract-declarator [opt]
14702 direct-abstract-declarator
14703
14704 GNU Extensions:
14705
14706 declarator:
14707 attributes [opt] direct-declarator
ccb84981 14708 attributes [opt] ptr-operator declarator
0a3b29ad 14709
14710 abstract-declarator:
14711 attributes [opt] ptr-operator abstract-declarator [opt]
14712 attributes [opt] direct-abstract-declarator
ccb84981 14713
0986fa22 14714 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14715 detect constructor, destructor or conversion operators. It is set
14716 to -1 if the declarator is a name, and +1 if it is a
14717 function. Otherwise it is set to zero. Usually you just want to
14718 test for >0, but internally the negative value is used.
ccb84981 14719
0a3b29ad 14720 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14721 a decl-specifier-seq unless it declares a constructor, destructor,
14722 or conversion. It might seem that we could check this condition in
14723 semantic analysis, rather than parsing, but that makes it difficult
14724 to handle something like `f()'. We want to notice that there are
14725 no decl-specifiers, and therefore realize that this is an
ccb84981 14726 expression, not a declaration.)
14727
92b128ed 14728 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9031d10b 14729 the declarator is a direct-declarator of the form "(...)".
08ea345c 14730
14731 MEMBER_P is true iff this declarator is a member-declarator. */
0a3b29ad 14732
3046c0a3 14733static cp_declarator *
ccb84981 14734cp_parser_declarator (cp_parser* parser,
653e5405 14735 cp_parser_declarator_kind dcl_kind,
14736 int* ctor_dtor_or_conv_p,
08ea345c 14737 bool* parenthesized_p,
14738 bool member_p)
0a3b29ad 14739{
3046c0a3 14740 cp_declarator *declarator;
0a3b29ad 14741 enum tree_code code;
2cfb6cde 14742 cp_cv_quals cv_quals;
0a3b29ad 14743 tree class_type;
14744 tree attributes = NULL_TREE;
14745
14746 /* Assume this is not a constructor, destructor, or type-conversion
14747 operator. */
14748 if (ctor_dtor_or_conv_p)
0986fa22 14749 *ctor_dtor_or_conv_p = 0;
0a3b29ad 14750
14751 if (cp_parser_allow_gnu_extensions_p (parser))
14752 attributes = cp_parser_attributes_opt (parser);
ccb84981 14753
0a3b29ad 14754 /* Check for the ptr-operator production. */
14755 cp_parser_parse_tentatively (parser);
14756 /* Parse the ptr-operator. */
ccb84981 14757 code = cp_parser_ptr_operator (parser,
14758 &class_type,
2cfb6cde 14759 &cv_quals);
0a3b29ad 14760 /* If that worked, then we have a ptr-operator. */
14761 if (cp_parser_parse_definitely (parser))
14762 {
92b128ed 14763 /* If a ptr-operator was found, then this declarator was not
14764 parenthesized. */
14765 if (parenthesized_p)
14766 *parenthesized_p = true;
0a3b29ad 14767 /* The dependent declarator is optional if we are parsing an
14768 abstract-declarator. */
42bbd0ec 14769 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 14770 cp_parser_parse_tentatively (parser);
14771
14772 /* Parse the dependent declarator. */
42bbd0ec 14773 declarator = cp_parser_declarator (parser, dcl_kind,
92b128ed 14774 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 14775 /*parenthesized_p=*/NULL,
14776 /*member_p=*/false);
0a3b29ad 14777
14778 /* If we are parsing an abstract-declarator, we must handle the
14779 case where the dependent declarator is absent. */
42bbd0ec 14780 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14781 && !cp_parser_parse_definitely (parser))
3046c0a3 14782 declarator = NULL;
ccb84981 14783
63949b38 14784 declarator = cp_parser_make_indirect_declarator
14785 (code, class_type, cv_quals, declarator);
0a3b29ad 14786 }
14787 /* Everything else is a direct-declarator. */
14788 else
92b128ed 14789 {
14790 if (parenthesized_p)
14791 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14792 CPP_OPEN_PAREN);
14793 declarator = cp_parser_direct_declarator (parser, dcl_kind,
08ea345c 14794 ctor_dtor_or_conv_p,
14795 member_p);
92b128ed 14796 }
0a3b29ad 14797
ba0c587d 14798 if (attributes && declarator && declarator != cp_error_declarator)
3046c0a3 14799 declarator->attributes = attributes;
ccb84981 14800
0a3b29ad 14801 return declarator;
14802}
14803
14804/* Parse a direct-declarator or direct-abstract-declarator.
14805
14806 direct-declarator:
14807 declarator-id
14808 direct-declarator ( parameter-declaration-clause )
ccb84981 14809 cv-qualifier-seq [opt]
0a3b29ad 14810 exception-specification [opt]
14811 direct-declarator [ constant-expression [opt] ]
ccb84981 14812 ( declarator )
0a3b29ad 14813
14814 direct-abstract-declarator:
14815 direct-abstract-declarator [opt]
ccb84981 14816 ( parameter-declaration-clause )
0a3b29ad 14817 cv-qualifier-seq [opt]
14818 exception-specification [opt]
14819 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14820 ( abstract-declarator )
14821
42bbd0ec 14822 Returns a representation of the declarator. DCL_KIND is
14823 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14824 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14825 we are parsing a direct-declarator. It is
14826 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14827 of ambiguity we prefer an abstract declarator, as per
08ea345c 14828 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
3046c0a3 14829 cp_parser_declarator. */
0a3b29ad 14830
3046c0a3 14831static cp_declarator *
45baea8b 14832cp_parser_direct_declarator (cp_parser* parser,
653e5405 14833 cp_parser_declarator_kind dcl_kind,
14834 int* ctor_dtor_or_conv_p,
08ea345c 14835 bool member_p)
0a3b29ad 14836{
14837 cp_token *token;
3046c0a3 14838 cp_declarator *declarator = NULL;
0a3b29ad 14839 tree scope = NULL_TREE;
14840 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14841 bool saved_in_declarator_p = parser->in_declarator_p;
42bbd0ec 14842 bool first = true;
7f602bca 14843 tree pushed_scope = NULL_TREE;
ccb84981 14844
42bbd0ec 14845 while (true)
0a3b29ad 14846 {
42bbd0ec 14847 /* Peek at the next token. */
14848 token = cp_lexer_peek_token (parser->lexer);
14849 if (token->type == CPP_OPEN_PAREN)
0a3b29ad 14850 {
42bbd0ec 14851 /* This is either a parameter-declaration-clause, or a
653e5405 14852 parenthesized declarator. When we know we are parsing a
14853 named declarator, it must be a parenthesized declarator
14854 if FIRST is true. For instance, `(int)' is a
14855 parameter-declaration-clause, with an omitted
14856 direct-abstract-declarator. But `((*))', is a
14857 parenthesized abstract declarator. Finally, when T is a
14858 template parameter `(T)' is a
14859 parameter-declaration-clause, and not a parenthesized
14860 named declarator.
ccb84981 14861
42bbd0ec 14862 We first try and parse a parameter-declaration-clause,
14863 and then try a nested declarator (if FIRST is true).
0a3b29ad 14864
42bbd0ec 14865 It is not an error for it not to be a
14866 parameter-declaration-clause, even when FIRST is
14867 false. Consider,
14868
14869 int i (int);
14870 int i (3);
14871
14872 The first is the declaration of a function while the
08cc44e7 14873 second is the definition of a variable, including its
42bbd0ec 14874 initializer.
14875
14876 Having seen only the parenthesis, we cannot know which of
14877 these two alternatives should be selected. Even more
14878 complex are examples like:
14879
653e5405 14880 int i (int (a));
42bbd0ec 14881 int i (int (3));
14882
14883 The former is a function-declaration; the latter is a
ccb84981 14884 variable initialization.
42bbd0ec 14885
755edffd 14886 Thus again, we try a parameter-declaration-clause, and if
42bbd0ec 14887 that fails, we back out and return. */
14888
14889 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 14890 {
34eac767 14891 tree params;
bb91f165 14892 unsigned saved_num_template_parameter_lists;
34eac767 14893 bool is_declarator = false;
14894 tree t;
ccb84981 14895
08ea345c 14896 /* In a member-declarator, the only valid interpretation
14897 of a parenthesis is the start of a
14898 parameter-declaration-clause. (It is invalid to
14899 initialize a static data member with a parenthesized
14900 initializer; only the "=" form of initialization is
14901 permitted.) */
14902 if (!member_p)
14903 cp_parser_parse_tentatively (parser);
0a3b29ad 14904
42bbd0ec 14905 /* Consume the `('. */
14906 cp_lexer_consume_token (parser->lexer);
14907 if (first)
14908 {
14909 /* If this is going to be an abstract declarator, we're
14910 in a declarator and we can't have default args. */
14911 parser->default_arg_ok_p = false;
14912 parser->in_declarator_p = true;
14913 }
ccb84981 14914
bb91f165 14915 /* Inside the function parameter list, surrounding
14916 template-parameter-lists do not apply. */
14917 saved_num_template_parameter_lists
14918 = parser->num_template_parameter_lists;
14919 parser->num_template_parameter_lists = 0;
14920
34eac767 14921 begin_scope (sk_function_parms, NULL_TREE);
14922
42bbd0ec 14923 /* Parse the parameter-declaration-clause. */
14924 params = cp_parser_parameter_declaration_clause (parser);
14925
bb91f165 14926 parser->num_template_parameter_lists
14927 = saved_num_template_parameter_lists;
14928
42bbd0ec 14929 /* If all went well, parse the cv-qualifier-seq and the
653e5405 14930 exception-specification. */
08ea345c 14931 if (member_p || cp_parser_parse_definitely (parser))
42bbd0ec 14932 {
2cfb6cde 14933 cp_cv_quals cv_quals;
42bbd0ec 14934 tree exception_specification;
346e3a9c 14935 tree late_return;
0986fa22 14936
34eac767 14937 is_declarator = true;
14938
0986fa22 14939 if (ctor_dtor_or_conv_p)
14940 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
42bbd0ec 14941 first = false;
14942 /* Consume the `)'. */
c247dce0 14943 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
42bbd0ec 14944
14945 /* Parse the cv-qualifier-seq. */
2cfb6cde 14946 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
42bbd0ec 14947 /* And the exception-specification. */
ccb84981 14948 exception_specification
42bbd0ec 14949 = cp_parser_exception_specification_opt (parser);
14950
346e3a9c 14951 late_return
14952 = cp_parser_late_return_type_opt (parser);
14953
42bbd0ec 14954 /* Create the function-declarator. */
14955 declarator = make_call_declarator (declarator,
14956 params,
2cfb6cde 14957 cv_quals,
346e3a9c 14958 exception_specification,
14959 late_return);
42bbd0ec 14960 /* Any subsequent parameter lists are to do with
653e5405 14961 return type, so are not those of the declared
14962 function. */
42bbd0ec 14963 parser->default_arg_ok_p = false;
42bbd0ec 14964 }
34eac767 14965
14966 /* Remove the function parms from scope. */
1767a056 14967 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
34eac767 14968 pop_binding (DECL_NAME (t), t);
14969 leave_scope();
14970
14971 if (is_declarator)
14972 /* Repeat the main loop. */
14973 continue;
42bbd0ec 14974 }
ccb84981 14975
42bbd0ec 14976 /* If this is the first, we can try a parenthesized
14977 declarator. */
14978 if (first)
0a3b29ad 14979 {
91f31809 14980 bool saved_in_type_id_in_expr_p;
14981
0a3b29ad 14982 parser->default_arg_ok_p = saved_default_arg_ok_p;
42bbd0ec 14983 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 14984
42bbd0ec 14985 /* Consume the `('. */
14986 cp_lexer_consume_token (parser->lexer);
14987 /* Parse the nested declarator. */
91f31809 14988 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14989 parser->in_type_id_in_expr_p = true;
ccb84981 14990 declarator
92b128ed 14991 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
08ea345c 14992 /*parenthesized_p=*/NULL,
14993 member_p);
91f31809 14994 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
42bbd0ec 14995 first = false;
14996 /* Expect a `)'. */
c247dce0 14997 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3046c0a3 14998 declarator = cp_error_declarator;
14999 if (declarator == cp_error_declarator)
42bbd0ec 15000 break;
ccb84981 15001
42bbd0ec 15002 goto handle_declarator;
0a3b29ad 15003 }
6beb3f76 15004 /* Otherwise, we must be done. */
42bbd0ec 15005 else
15006 break;
0a3b29ad 15007 }
42bbd0ec 15008 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15009 && token->type == CPP_OPEN_SQUARE)
0a3b29ad 15010 {
42bbd0ec 15011 /* Parse an array-declarator. */
0a3b29ad 15012 tree bounds;
15013
0986fa22 15014 if (ctor_dtor_or_conv_p)
15015 *ctor_dtor_or_conv_p = 0;
ccb84981 15016
42bbd0ec 15017 first = false;
15018 parser->default_arg_ok_p = false;
15019 parser->in_declarator_p = true;
0a3b29ad 15020 /* Consume the `['. */
15021 cp_lexer_consume_token (parser->lexer);
15022 /* Peek at the next token. */
15023 token = cp_lexer_peek_token (parser->lexer);
15024 /* If the next token is `]', then there is no
15025 constant-expression. */
15026 if (token->type != CPP_CLOSE_SQUARE)
5f6526e1 15027 {
15028 bool non_constant_p;
15029
ccb84981 15030 bounds
5f6526e1 15031 = cp_parser_constant_expression (parser,
15032 /*allow_non_constant=*/true,
15033 &non_constant_p);
ce984e5e 15034 if (!non_constant_p || cxx_dialect >= cxx0x)
15035 /* OK */;
f0d4a607 15036 /* Normally, the array bound must be an integral constant
15037 expression. However, as an extension, we allow VLAs
27176e7c 15038 in function scopes as long as they aren't part of a
15039 parameter declaration. */
15040 else if (!parser->in_function_body
15041 || current_binding_level->kind == sk_function_parms)
8b652e89 15042 {
27176e7c 15043 cp_parser_error (parser,
15044 "array bound is not an integer constant");
8b652e89 15045 bounds = error_mark_node;
15046 }
d1564595 15047 else if (processing_template_decl && !error_operand_p (bounds))
15048 {
15049 /* Remember this wasn't a constant-expression. */
15050 bounds = build_nop (TREE_TYPE (bounds), bounds);
15051 TREE_SIDE_EFFECTS (bounds) = 1;
15052 }
5f6526e1 15053 }
0a3b29ad 15054 else
15055 bounds = NULL_TREE;
15056 /* Look for the closing `]'. */
c247dce0 15057 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
42bbd0ec 15058 {
3046c0a3 15059 declarator = cp_error_declarator;
42bbd0ec 15060 break;
15061 }
0a3b29ad 15062
3046c0a3 15063 declarator = make_array_declarator (declarator, bounds);
0a3b29ad 15064 }
42bbd0ec 15065 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
0a3b29ad 15066 {
f805d53d 15067 {
15068 tree qualifying_scope;
15069 tree unqualified_name;
15070 special_function_kind sfk;
15071 bool abstract_ok;
15072 bool pack_expansion_p = false;
15073 cp_token *declarator_id_start_token;
15074
15075 /* Parse a declarator-id */
15076 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15077 if (abstract_ok)
15078 {
15079 cp_parser_parse_tentatively (parser);
d95d815d 15080
f805d53d 15081 /* If we see an ellipsis, we should be looking at a
15082 parameter pack. */
15083 if (token->type == CPP_ELLIPSIS)
15084 {
15085 /* Consume the `...' */
15086 cp_lexer_consume_token (parser->lexer);
d95d815d 15087
f805d53d 15088 pack_expansion_p = true;
15089 }
15090 }
d95d815d 15091
f805d53d 15092 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15093 unqualified_name
15094 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15095 qualifying_scope = parser->scope;
15096 if (abstract_ok)
15097 {
15098 bool okay = false;
ccb84981 15099
f805d53d 15100 if (!unqualified_name && pack_expansion_p)
15101 {
15102 /* Check whether an error occurred. */
15103 okay = !cp_parser_error_occurred (parser);
15104
15105 /* We already consumed the ellipsis to mark a
15106 parameter pack, but we have no way to report it,
15107 so abort the tentative parse. We will be exiting
15108 immediately anyway. */
15109 cp_parser_abort_tentative_parse (parser);
15110 }
15111 else
15112 okay = cp_parser_parse_definitely (parser);
ccb84981 15113
f805d53d 15114 if (!okay)
15115 unqualified_name = error_mark_node;
15116 else if (unqualified_name
15117 && (qualifying_scope
15118 || (TREE_CODE (unqualified_name)
15119 != IDENTIFIER_NODE)))
15120 {
15121 cp_parser_error (parser, "expected unqualified-id");
15122 unqualified_name = error_mark_node;
15123 }
15124 }
ccb84981 15125
f805d53d 15126 if (!unqualified_name)
15127 return NULL;
15128 if (unqualified_name == error_mark_node)
15129 {
15130 declarator = cp_error_declarator;
15131 pack_expansion_p = false;
15132 declarator->parameter_pack_p = false;
15133 break;
15134 }
d95d815d 15135
f805d53d 15136 if (qualifying_scope && at_namespace_scope_p ()
15137 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15138 {
15139 /* In the declaration of a member of a template class
15140 outside of the class itself, the SCOPE will sometimes
15141 be a TYPENAME_TYPE. For example, given:
15142
15143 template <typename T>
15144 int S<T>::R::i = 3;
15145
15146 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15147 this context, we must resolve S<T>::R to an ordinary
15148 type, rather than a typename type.
15149
15150 The reason we normally avoid resolving TYPENAME_TYPEs
15151 is that a specialization of `S' might render
15152 `S<T>::R' not a type. However, if `S' is
15153 specialized, then this `i' will not be used, so there
15154 is no harm in resolving the types here. */
15155 tree type;
15156
15157 /* Resolve the TYPENAME_TYPE. */
15158 type = resolve_typename_type (qualifying_scope,
15159 /*only_current_p=*/false);
15160 /* If that failed, the declarator is invalid. */
15161 if (TREE_CODE (type) == TYPENAME_TYPE)
2e6a4932 15162 {
15163 if (typedef_variant_p (type))
15164 error_at (declarator_id_start_token->location,
15165 "cannot define member of dependent typedef "
15166 "%qT", type);
15167 else
15168 error_at (declarator_id_start_token->location,
15169 "%<%T::%E%> is not a type",
15170 TYPE_CONTEXT (qualifying_scope),
15171 TYPE_IDENTIFIER (qualifying_scope));
15172 }
f805d53d 15173 qualifying_scope = type;
15174 }
42bbd0ec 15175
f805d53d 15176 sfk = sfk_none;
42bbd0ec 15177
f805d53d 15178 if (unqualified_name)
15179 {
15180 tree class_type;
2366ed31 15181
f805d53d 15182 if (qualifying_scope
15183 && CLASS_TYPE_P (qualifying_scope))
15184 class_type = qualifying_scope;
15185 else
15186 class_type = current_class_type;
2366ed31 15187
f805d53d 15188 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15189 {
15190 tree name_type = TREE_TYPE (unqualified_name);
15191 if (class_type && same_type_p (name_type, class_type))
15192 {
15193 if (qualifying_scope
15194 && CLASSTYPE_USE_TEMPLATE (name_type))
15195 {
ccb59bb6 15196 error_at (declarator_id_start_token->location,
15197 "invalid use of constructor as a template");
15198 inform (declarator_id_start_token->location,
15199 "use %<%T::%D%> instead of %<%T::%D%> to "
f805d53d 15200 "name the constructor in a qualified name",
15201 class_type,
15202 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15203 class_type, name_type);
15204 declarator = cp_error_declarator;
15205 break;
15206 }
15207 else
15208 unqualified_name = constructor_name (class_type);
15209 }
15210 else
15211 {
15212 /* We do not attempt to print the declarator
15213 here because we do not have enough
15214 information about its original syntactic
15215 form. */
15216 cp_parser_error (parser, "invalid declarator");
15217 declarator = cp_error_declarator;
15218 break;
15219 }
15220 }
d95d815d 15221
f805d53d 15222 if (class_type)
15223 {
15224 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15225 sfk = sfk_destructor;
15226 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15227 sfk = sfk_conversion;
15228 else if (/* There's no way to declare a constructor
15229 for an anonymous type, even if the type
15230 got a name for linkage purposes. */
15231 !TYPE_WAS_ANONYMOUS (class_type)
15232 && constructor_name_p (unqualified_name,
15233 class_type))
15234 {
15235 unqualified_name = constructor_name (class_type);
15236 sfk = sfk_constructor;
15237 }
a70e3c37 15238 else if (is_overloaded_fn (unqualified_name)
15239 && DECL_CONSTRUCTOR_P (get_first_fn
15240 (unqualified_name)))
15241 sfk = sfk_constructor;
f805d53d 15242
15243 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15244 *ctor_dtor_or_conv_p = -1;
15245 }
15246 }
15247 declarator = make_id_declarator (qualifying_scope,
15248 unqualified_name,
15249 sfk);
15250 declarator->id_loc = token->location;
15251 declarator->parameter_pack_p = pack_expansion_p;
15252
15253 if (pack_expansion_p)
15254 maybe_warn_variadic_templates ();
15255 }
42bbd0ec 15256
15257 handle_declarator:;
15258 scope = get_scope_of_declarator (declarator);
15259 if (scope)
1cbda81f 15260 /* Any names that appear after the declarator-id for a
15261 member are looked up in the containing scope. */
7f602bca 15262 pushed_scope = push_scope (scope);
42bbd0ec 15263 parser->in_declarator_p = true;
15264 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
3046c0a3 15265 || (declarator && declarator->kind == cdk_id))
42bbd0ec 15266 /* Default args are only allowed on function
15267 declarations. */
15268 parser->default_arg_ok_p = saved_default_arg_ok_p;
0a3b29ad 15269 else
42bbd0ec 15270 parser->default_arg_ok_p = false;
15271
15272 first = false;
0a3b29ad 15273 }
42bbd0ec 15274 /* We're done. */
0a3b29ad 15275 else
15276 break;
0a3b29ad 15277 }
15278
15279 /* For an abstract declarator, we might wind up with nothing at this
15280 point. That's an error; the declarator is not optional. */
15281 if (!declarator)
15282 cp_parser_error (parser, "expected declarator");
15283
15284 /* If we entered a scope, we must exit it now. */
7f602bca 15285 if (pushed_scope)
15286 pop_scope (pushed_scope);
0a3b29ad 15287
15288 parser->default_arg_ok_p = saved_default_arg_ok_p;
15289 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 15290
0a3b29ad 15291 return declarator;
15292}
15293
ccb84981 15294/* Parse a ptr-operator.
0a3b29ad 15295
15296 ptr-operator:
15297 * cv-qualifier-seq [opt]
15298 &
15299 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15300
15301 GNU Extension:
15302
15303 ptr-operator:
15304 & cv-qualifier-seq [opt]
15305
2cfb6cde 15306 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
63949b38 15307 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15308 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15309 filled in with the TYPE containing the member. *CV_QUALS is
15310 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15311 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15312 Note that the tree codes returned by this function have nothing
15313 to do with the types of trees that will be eventually be created
15314 to represent the pointer or reference type being parsed. They are
15315 just constants with suggestive names. */
0a3b29ad 15316static enum tree_code
ccb84981 15317cp_parser_ptr_operator (cp_parser* parser,
653e5405 15318 tree* type,
2cfb6cde 15319 cp_cv_quals *cv_quals)
0a3b29ad 15320{
15321 enum tree_code code = ERROR_MARK;
15322 cp_token *token;
15323
15324 /* Assume that it's not a pointer-to-member. */
15325 *type = NULL_TREE;
15326 /* And that there are no cv-qualifiers. */
2cfb6cde 15327 *cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 15328
15329 /* Peek at the next token. */
15330 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15331
63949b38 15332 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15333 if (token->type == CPP_MULT)
15334 code = INDIRECT_REF;
15335 else if (token->type == CPP_AND)
15336 code = ADDR_EXPR;
6dcdb5de 15337 else if ((cxx_dialect != cxx98) &&
15338 token->type == CPP_AND_AND) /* C++0x only */
63949b38 15339 code = NON_LVALUE_EXPR;
15340
15341 if (code != ERROR_MARK)
15342 {
15343 /* Consume the `*', `&' or `&&'. */
0a3b29ad 15344 cp_lexer_consume_token (parser->lexer);
15345
15346 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15347 `&', if we are allowing GNU extensions. (The only qualifier
15348 that can legally appear after `&' is `restrict', but that is
15349 enforced during semantic analysis. */
ccb84981 15350 if (code == INDIRECT_REF
0a3b29ad 15351 || cp_parser_allow_gnu_extensions_p (parser))
2cfb6cde 15352 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
0a3b29ad 15353 }
15354 else
15355 {
15356 /* Try the pointer-to-member case. */
15357 cp_parser_parse_tentatively (parser);
15358 /* Look for the optional `::' operator. */
15359 cp_parser_global_scope_opt (parser,
130bb1d4 15360 /*current_scope_valid_p=*/false);
0a3b29ad 15361 /* Look for the nested-name specifier. */
ad9ae192 15362 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15363 cp_parser_nested_name_specifier (parser,
15364 /*typename_keyword_p=*/false,
15365 /*check_dependency_p=*/true,
3d0f901b 15366 /*type_p=*/false,
15367 /*is_declaration=*/false);
0a3b29ad 15368 /* If we found it, and the next token is a `*', then we are
15369 indeed looking at a pointer-to-member operator. */
15370 if (!cp_parser_error_occurred (parser)
c247dce0 15371 && cp_parser_require (parser, CPP_MULT, RT_MULT))
0a3b29ad 15372 {
0a3b29ad 15373 /* Indicate that the `*' operator was used. */
15374 code = INDIRECT_REF;
393f878f 15375
15376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
ccb59bb6 15377 error_at (token->location, "%qD is a namespace", parser->scope);
393f878f 15378 else
15379 {
15380 /* The type of which the member is a member is given by the
15381 current SCOPE. */
15382 *type = parser->scope;
15383 /* The next name will not be qualified. */
15384 parser->scope = NULL_TREE;
15385 parser->qualifying_scope = NULL_TREE;
15386 parser->object_scope = NULL_TREE;
15387 /* Look for the optional cv-qualifier-seq. */
15388 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15389 }
0a3b29ad 15390 }
15391 /* If that didn't work we don't have a ptr-operator. */
15392 if (!cp_parser_parse_definitely (parser))
15393 cp_parser_error (parser, "expected ptr-operator");
15394 }
15395
15396 return code;
15397}
15398
15399/* Parse an (optional) cv-qualifier-seq.
15400
15401 cv-qualifier-seq:
ccb84981 15402 cv-qualifier cv-qualifier-seq [opt]
0a3b29ad 15403
0a3b29ad 15404 cv-qualifier:
15405 const
ccb84981 15406 volatile
0a3b29ad 15407
15408 GNU Extension:
15409
15410 cv-qualifier:
207355ad 15411 __restrict__
0a3b29ad 15412
2cfb6cde 15413 Returns a bitmask representing the cv-qualifiers. */
15414
15415static cp_cv_quals
15416cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
0a3b29ad 15417{
2cfb6cde 15418 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 15419
2cfb6cde 15420 while (true)
0a3b29ad 15421 {
2cfb6cde 15422 cp_token *token;
15423 cp_cv_quals cv_qualifier;
207355ad 15424
2cfb6cde 15425 /* Peek at the next token. */
15426 token = cp_lexer_peek_token (parser->lexer);
15427 /* See if it's a cv-qualifier. */
15428 switch (token->keyword)
15429 {
15430 case RID_CONST:
15431 cv_qualifier = TYPE_QUAL_CONST;
15432 break;
207355ad 15433
2cfb6cde 15434 case RID_VOLATILE:
15435 cv_qualifier = TYPE_QUAL_VOLATILE;
15436 break;
207355ad 15437
2cfb6cde 15438 case RID_RESTRICT:
15439 cv_qualifier = TYPE_QUAL_RESTRICT;
15440 break;
207355ad 15441
2cfb6cde 15442 default:
15443 cv_qualifier = TYPE_UNQUALIFIED;
15444 break;
15445 }
207355ad 15446
2cfb6cde 15447 if (!cv_qualifier)
15448 break;
0a3b29ad 15449
2cfb6cde 15450 if (cv_quals & cv_qualifier)
15451 {
ccb59bb6 15452 error_at (token->location, "duplicate cv-qualifier");
2cfb6cde 15453 cp_lexer_purge_token (parser->lexer);
15454 }
15455 else
15456 {
15457 cp_lexer_consume_token (parser->lexer);
15458 cv_quals |= cv_qualifier;
15459 }
0a3b29ad 15460 }
15461
2cfb6cde 15462 return cv_quals;
0a3b29ad 15463}
15464
346e3a9c 15465/* Parse a late-specified return type, if any. This is not a separate
15466 non-terminal, but part of a function declarator, which looks like
15467
638569c5 15468 -> trailing-type-specifier-seq abstract-declarator(opt)
346e3a9c 15469
15470 Returns the type indicated by the type-id. */
15471
15472static tree
15473cp_parser_late_return_type_opt (cp_parser* parser)
15474{
15475 cp_token *token;
15476
15477 /* Peek at the next token. */
15478 token = cp_lexer_peek_token (parser->lexer);
15479 /* A late-specified return type is indicated by an initial '->'. */
15480 if (token->type != CPP_DEREF)
15481 return NULL_TREE;
15482
15483 /* Consume the ->. */
15484 cp_lexer_consume_token (parser->lexer);
15485
638569c5 15486 return cp_parser_trailing_type_id (parser);
346e3a9c 15487}
15488
0a3b29ad 15489/* Parse a declarator-id.
15490
15491 declarator-id:
15492 id-expression
ccb84981 15493 :: [opt] nested-name-specifier [opt] type-name
0a3b29ad 15494
15495 In the `id-expression' case, the value returned is as for
15496 cp_parser_id_expression if the id-expression was an unqualified-id.
15497 If the id-expression was a qualified-id, then a SCOPE_REF is
15498 returned. The first operand is the scope (either a NAMESPACE_DECL
15499 or TREE_TYPE), but the second is still just a representation of an
15500 unqualified-id. */
15501
15502static tree
197c9df7 15503cp_parser_declarator_id (cp_parser* parser, bool optional_p)
0a3b29ad 15504{
2366ed31 15505 tree id;
0a3b29ad 15506 /* The expression must be an id-expression. Assume that qualified
15507 names are the names of types so that:
15508
15509 template <class T>
15510 int S<T>::R::i = 3;
15511
15512 will work; we must treat `S<T>::R' as the name of a type.
15513 Similarly, assume that qualified names are templates, where
15514 required, so that:
15515
15516 template <class T>
15517 int S<T>::R<T>::i = 3;
15518
15519 will work, too. */
2366ed31 15520 id = cp_parser_id_expression (parser,
15521 /*template_keyword_p=*/false,
15522 /*check_dependency_p=*/false,
15523 /*template_p=*/NULL,
197c9df7 15524 /*declarator_p=*/true,
130bb1d4 15525 optional_p);
197c9df7 15526 if (id && BASELINK_P (id))
2366ed31 15527 id = BASELINK_FUNCTIONS (id);
15528 return id;
0a3b29ad 15529}
15530
15531/* Parse a type-id.
15532
15533 type-id:
15534 type-specifier-seq abstract-declarator [opt]
15535
15536 Returns the TYPE specified. */
15537
15538static tree
638569c5 15539cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15540 bool is_trailing_return)
0a3b29ad 15541{
4b9b2871 15542 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 15543 cp_declarator *abstract_declarator;
0a3b29ad 15544
15545 /* Parse the type-specifier-seq. */
c44f7faf 15546 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 15547 is_trailing_return,
6f74fe3c 15548 &type_specifier_seq);
4b9b2871 15549 if (type_specifier_seq.type == error_mark_node)
0a3b29ad 15550 return error_mark_node;
15551
15552 /* There might or might not be an abstract declarator. */
15553 cp_parser_parse_tentatively (parser);
15554 /* Look for the declarator. */
ccb84981 15555 abstract_declarator
92b128ed 15556 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
08ea345c 15557 /*parenthesized_p=*/NULL,
15558 /*member_p=*/false);
0a3b29ad 15559 /* Check to see if there really was a declarator. */
15560 if (!cp_parser_parse_definitely (parser))
3046c0a3 15561 abstract_declarator = NULL;
0a3b29ad 15562
475fd34e 15563 if (type_specifier_seq.type
15564 && type_uses_auto (type_specifier_seq.type))
15565 {
e439140e 15566 /* A type-id with type 'auto' is only ok if the abstract declarator
15567 is a function declarator with a late-specified return type. */
15568 if (abstract_declarator
15569 && abstract_declarator->kind == cdk_function
15570 && abstract_declarator->u.function.late_return_type)
15571 /* OK */;
15572 else
15573 {
15574 error ("invalid use of %<auto%>");
15575 return error_mark_node;
15576 }
475fd34e 15577 }
15578
75eaa947 15579 return groktypename (&type_specifier_seq, abstract_declarator,
15580 is_template_arg);
15581}
15582
15583static tree cp_parser_type_id (cp_parser *parser)
15584{
638569c5 15585 return cp_parser_type_id_1 (parser, false, false);
75eaa947 15586}
15587
15588static tree cp_parser_template_type_arg (cp_parser *parser)
15589{
638569c5 15590 return cp_parser_type_id_1 (parser, true, false);
15591}
15592
15593static tree cp_parser_trailing_type_id (cp_parser *parser)
15594{
15595 return cp_parser_type_id_1 (parser, false, true);
0a3b29ad 15596}
15597
15598/* Parse a type-specifier-seq.
15599
15600 type-specifier-seq:
15601 type-specifier type-specifier-seq [opt]
15602
15603 GNU extension:
15604
15605 type-specifier-seq:
15606 attributes type-specifier-seq [opt]
15607
c44f7faf 15608 If IS_DECLARATION is true, we are at the start of a "condition" or
15609 exception-declaration, so we might be followed by a declarator-id.
6f74fe3c 15610
638569c5 15611 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15612 i.e. we've just seen "->".
15613
4b9b2871 15614 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
0a3b29ad 15615
4b9b2871 15616static void
15617cp_parser_type_specifier_seq (cp_parser* parser,
c44f7faf 15618 bool is_declaration,
638569c5 15619 bool is_trailing_return,
4b9b2871 15620 cp_decl_specifier_seq *type_specifier_seq)
0a3b29ad 15621{
15622 bool seen_type_specifier = false;
6f74fe3c 15623 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
ad9ae192 15624 cp_token *start_token = NULL;
4b9b2871 15625
15626 /* Clear the TYPE_SPECIFIER_SEQ. */
15627 clear_decl_specs (type_specifier_seq);
0a3b29ad 15628
638569c5 15629 /* In the context of a trailing return type, enum E { } is an
15630 elaborated-type-specifier followed by a function-body, not an
15631 enum-specifier. */
15632 if (is_trailing_return)
15633 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15634
0a3b29ad 15635 /* Parse the type-specifiers and attributes. */
15636 while (true)
15637 {
15638 tree type_specifier;
6f74fe3c 15639 bool is_cv_qualifier;
0a3b29ad 15640
15641 /* Check for attributes first. */
15642 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15643 {
207355ad 15644 type_specifier_seq->attributes =
4b9b2871 15645 chainon (type_specifier_seq->attributes,
15646 cp_parser_attributes_opt (parser));
0a3b29ad 15647 continue;
15648 }
15649
ad9ae192 15650 /* record the token of the beginning of the type specifier seq,
15651 for error reporting purposes*/
15652 if (!start_token)
15653 start_token = cp_lexer_peek_token (parser->lexer);
15654
0a3b29ad 15655 /* Look for the type-specifier. */
ccb84981 15656 type_specifier = cp_parser_type_specifier (parser,
6f74fe3c 15657 flags,
4b9b2871 15658 type_specifier_seq,
0a3b29ad 15659 /*is_declaration=*/false,
15660 NULL,
6f74fe3c 15661 &is_cv_qualifier);
15662 if (!type_specifier)
4b9b2871 15663 {
6f74fe3c 15664 /* If the first type-specifier could not be found, this is not a
15665 type-specifier-seq at all. */
15666 if (!seen_type_specifier)
15667 {
15668 cp_parser_error (parser, "expected type-specifier");
15669 type_specifier_seq->type = error_mark_node;
15670 return;
15671 }
15672 /* If subsequent type-specifiers could not be found, the
15673 type-specifier-seq is complete. */
15674 break;
4b9b2871 15675 }
0a3b29ad 15676
0a3b29ad 15677 seen_type_specifier = true;
6f74fe3c 15678 /* The standard says that a condition can be:
15679
653e5405 15680 type-specifier-seq declarator = assignment-expression
9031d10b 15681
6f74fe3c 15682 However, given:
15683
15684 struct S {};
15685 if (int S = ...)
15686
653e5405 15687 we should treat the "S" as a declarator, not as a
15688 type-specifier. The standard doesn't say that explicitly for
15689 type-specifier-seq, but it does say that for
15690 decl-specifier-seq in an ordinary declaration. Perhaps it
15691 would be clearer just to allow a decl-specifier-seq here, and
15692 then add a semantic restriction that if any decl-specifiers
15693 that are not type-specifiers appear, the program is invalid. */
c44f7faf 15694 if (is_declaration && !is_cv_qualifier)
9031d10b 15695 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
0a3b29ad 15696 }
639b2fed 15697
ad9ae192 15698 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
0a3b29ad 15699}
15700
15701/* Parse a parameter-declaration-clause.
15702
15703 parameter-declaration-clause:
15704 parameter-declaration-list [opt] ... [opt]
15705 parameter-declaration-list , ...
15706
3046c0a3 15707 Returns a representation for the parameter declarations. A return
15708 value of NULL indicates a parameter-declaration-clause consisting
15709 only of an ellipsis. */
0a3b29ad 15710
34eac767 15711static tree
45baea8b 15712cp_parser_parameter_declaration_clause (cp_parser* parser)
0a3b29ad 15713{
34eac767 15714 tree parameters;
0a3b29ad 15715 cp_token *token;
15716 bool ellipsis_p;
3046c0a3 15717 bool is_error;
0a3b29ad 15718
15719 /* Peek at the next token. */
15720 token = cp_lexer_peek_token (parser->lexer);
15721 /* Check for trivial parameter-declaration-clauses. */
15722 if (token->type == CPP_ELLIPSIS)
15723 {
15724 /* Consume the `...' token. */
15725 cp_lexer_consume_token (parser->lexer);
34eac767 15726 return NULL_TREE;
0a3b29ad 15727 }
15728 else if (token->type == CPP_CLOSE_PAREN)
15729 /* There are no parameters. */
2bd78947 15730 {
15731#ifndef NO_IMPLICIT_EXTERN_C
15732 if (in_system_header && current_class_type == NULL
15733 && current_lang_name == lang_name_c)
34eac767 15734 return NULL_TREE;
2bd78947 15735 else
15736#endif
34eac767 15737 return void_list_node;
2bd78947 15738 }
0a3b29ad 15739 /* Check for `(void)', too, which is a special case. */
15740 else if (token->keyword == RID_VOID
ccb84981 15741 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 15742 == CPP_CLOSE_PAREN))
15743 {
15744 /* Consume the `void' token. */
15745 cp_lexer_consume_token (parser->lexer);
15746 /* There are no parameters. */
34eac767 15747 return void_list_node;
0a3b29ad 15748 }
ccb84981 15749
0a3b29ad 15750 /* Parse the parameter-declaration-list. */
3046c0a3 15751 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
0a3b29ad 15752 /* If a parse error occurred while parsing the
15753 parameter-declaration-list, then the entire
15754 parameter-declaration-clause is erroneous. */
3046c0a3 15755 if (is_error)
15756 return NULL;
0a3b29ad 15757
15758 /* Peek at the next token. */
15759 token = cp_lexer_peek_token (parser->lexer);
15760 /* If it's a `,', the clause should terminate with an ellipsis. */
15761 if (token->type == CPP_COMMA)
15762 {
15763 /* Consume the `,'. */
15764 cp_lexer_consume_token (parser->lexer);
15765 /* Expect an ellipsis. */
ccb84981 15766 ellipsis_p
c247dce0 15767 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
0a3b29ad 15768 }
ccb84981 15769 /* It might also be `...' if the optional trailing `,' was
0a3b29ad 15770 omitted. */
15771 else if (token->type == CPP_ELLIPSIS)
15772 {
15773 /* Consume the `...' token. */
15774 cp_lexer_consume_token (parser->lexer);
15775 /* And remember that we saw it. */
15776 ellipsis_p = true;
15777 }
15778 else
15779 ellipsis_p = false;
15780
15781 /* Finish the parameter list. */
34eac767 15782 if (!ellipsis_p)
15783 parameters = chainon (parameters, void_list_node);
207355ad 15784
3046c0a3 15785 return parameters;
0a3b29ad 15786}
15787
15788/* Parse a parameter-declaration-list.
15789
15790 parameter-declaration-list:
15791 parameter-declaration
15792 parameter-declaration-list , parameter-declaration
15793
15794 Returns a representation of the parameter-declaration-list, as for
15795 cp_parser_parameter_declaration_clause. However, the
3046c0a3 15796 `void_list_node' is never appended to the list. Upon return,
15797 *IS_ERROR will be true iff an error occurred. */
0a3b29ad 15798
34eac767 15799static tree
3046c0a3 15800cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
0a3b29ad 15801{
34eac767 15802 tree parameters = NULL_TREE;
15803 tree *tail = &parameters;
3b289c9c 15804 bool saved_in_unbraced_linkage_specification_p;
32d008d9 15805 int index = 0;
3046c0a3 15806
15807 /* Assume all will go well. */
15808 *is_error = false;
3b289c9c 15809 /* The special considerations that apply to a function within an
15810 unbraced linkage specifications do not apply to the parameters
15811 to the function. */
15812 saved_in_unbraced_linkage_specification_p
15813 = parser->in_unbraced_linkage_specification_p;
15814 parser->in_unbraced_linkage_specification_p = false;
0a3b29ad 15815
15816 /* Look for more parameters. */
15817 while (true)
15818 {
3046c0a3 15819 cp_parameter_declarator *parameter;
34eac767 15820 tree decl = error_mark_node;
92b128ed 15821 bool parenthesized_p;
0a3b29ad 15822 /* Parse the parameter. */
ccb84981 15823 parameter
15824 = cp_parser_parameter_declaration (parser,
92b128ed 15825 /*template_parm_p=*/false,
15826 &parenthesized_p);
759fa9c9 15827
34eac767 15828 /* We don't know yet if the enclosing context is deprecated, so wait
15829 and warn in grokparms if appropriate. */
15830 deprecated_state = DEPRECATED_SUPPRESS;
15831
15832 if (parameter)
15833 decl = grokdeclarator (parameter->declarator,
15834 &parameter->decl_specifiers,
15835 PARM,
15836 parameter->default_argument != NULL_TREE,
15837 &parameter->decl_specifiers.attributes);
15838
15839 deprecated_state = DEPRECATED_NORMAL;
15840
755edffd 15841 /* If a parse error occurred parsing the parameter declaration,
0a3b29ad 15842 then the entire parameter-declaration-list is erroneous. */
34eac767 15843 if (decl == error_mark_node)
0a3b29ad 15844 {
3046c0a3 15845 *is_error = true;
34eac767 15846 parameters = error_mark_node;
0a3b29ad 15847 break;
15848 }
34eac767 15849
15850 if (parameter->decl_specifiers.attributes)
15851 cplus_decl_attributes (&decl,
15852 parameter->decl_specifiers.attributes,
15853 0);
15854 if (DECL_NAME (decl))
15855 decl = pushdecl (decl);
15856
32d008d9 15857 if (decl != error_mark_node)
15858 {
15859 retrofit_lang_decl (decl);
15860 DECL_PARM_INDEX (decl) = ++index;
15861 }
15862
0a3b29ad 15863 /* Add the new parameter to the list. */
34eac767 15864 *tail = build_tree_list (parameter->default_argument, decl);
15865 tail = &TREE_CHAIN (*tail);
0a3b29ad 15866
15867 /* Peek at the next token. */
15868 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
7a4e126b 15869 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15870 /* These are for Objective-C++ */
15871 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15872 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 15873 /* The parameter-declaration-list is complete. */
15874 break;
15875 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15876 {
15877 cp_token *token;
15878
15879 /* Peek at the next token. */
15880 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15881 /* If it's an ellipsis, then the list is complete. */
15882 if (token->type == CPP_ELLIPSIS)
15883 break;
15884 /* Otherwise, there must be more parameters. Consume the
15885 `,'. */
15886 cp_lexer_consume_token (parser->lexer);
92b128ed 15887 /* When parsing something like:
15888
653e5405 15889 int i(float f, double d)
ccb84981 15890
653e5405 15891 we can tell after seeing the declaration for "f" that we
92b128ed 15892 are not looking at an initialization of a variable "i",
ccb84981 15893 but rather at the declaration of a function "i".
92b128ed 15894
15895 Due to the fact that the parsing of template arguments
15896 (as specified to a template-id) requires backtracking we
15897 cannot use this technique when inside a template argument
15898 list. */
15899 if (!parser->in_template_argument_list_p
6006bfb6 15900 && !parser->in_type_id_in_expr_p
efcbcf83 15901 && cp_parser_uncommitted_to_tentative_parse_p (parser)
92b128ed 15902 /* However, a parameter-declaration of the form
15903 "foat(f)" (which is a valid declaration of a
15904 parameter "f") can also be interpreted as an
15905 expression (the conversion of "f" to "float"). */
15906 && !parenthesized_p)
15907 cp_parser_commit_to_tentative_parse (parser);
0a3b29ad 15908 }
15909 else
15910 {
a2c5b975 15911 cp_parser_error (parser, "expected %<,%> or %<...%>");
efcbcf83 15912 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb84981 15913 cp_parser_skip_to_closing_parenthesis (parser,
92b128ed 15914 /*recovering=*/true,
78662158 15915 /*or_comma=*/false,
92b128ed 15916 /*consume_paren=*/false);
0a3b29ad 15917 break;
15918 }
15919 }
15920
3b289c9c 15921 parser->in_unbraced_linkage_specification_p
15922 = saved_in_unbraced_linkage_specification_p;
15923
3046c0a3 15924 return parameters;
0a3b29ad 15925}
15926
15927/* Parse a parameter declaration.
15928
15929 parameter-declaration:
d95d815d 15930 decl-specifier-seq ... [opt] declarator
0a3b29ad 15931 decl-specifier-seq declarator = assignment-expression
d95d815d 15932 decl-specifier-seq ... [opt] abstract-declarator [opt]
0a3b29ad 15933 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15934
759fa9c9 15935 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15936 declares a template parameter. (In that case, a non-nested `>'
15937 token encountered during the parsing of the assignment-expression
15938 is not interpreted as a greater-than operator.)
0a3b29ad 15939
3046c0a3 15940 Returns a representation of the parameter, or NULL if an error
15941 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15942 true iff the declarator is of the form "(p)". */
0a3b29ad 15943
3046c0a3 15944static cp_parameter_declarator *
ccb84981 15945cp_parser_parameter_declaration (cp_parser *parser,
92b128ed 15946 bool template_parm_p,
15947 bool *parenthesized_p)
0a3b29ad 15948{
8172be22 15949 int declares_class_or_enum;
4b9b2871 15950 cp_decl_specifier_seq decl_specifiers;
3046c0a3 15951 cp_declarator *declarator;
0a3b29ad 15952 tree default_argument;
ad9ae192 15953 cp_token *token = NULL, *declarator_token_start = NULL;
0a3b29ad 15954 const char *saved_message;
15955
759fa9c9 15956 /* In a template parameter, `>' is not an operator.
15957
15958 [temp.param]
15959
15960 When parsing a default template-argument for a non-type
15961 template-parameter, the first non-nested `>' is taken as the end
15962 of the template parameter-list rather than a greater-than
15963 operator. */
759fa9c9 15964
0a3b29ad 15965 /* Type definitions may not appear in parameter types. */
15966 saved_message = parser->type_definition_forbidden_message;
ccb84981 15967 parser->type_definition_forbidden_message
ca82e026 15968 = G_("types may not be defined in parameter types");
0a3b29ad 15969
15970 /* Parse the declaration-specifiers. */
4b9b2871 15971 cp_parser_decl_specifier_seq (parser,
15972 CP_PARSER_FLAGS_NONE,
15973 &decl_specifiers,
15974 &declares_class_or_enum);
67484828 15975
15976 /* Complain about missing 'typename' or other invalid type names. */
15977 if (!decl_specifiers.any_type_specifiers_p)
15978 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15979
0a3b29ad 15980 /* If an error occurred, there's no reason to attempt to parse the
15981 rest of the declaration. */
15982 if (cp_parser_error_occurred (parser))
15983 {
15984 parser->type_definition_forbidden_message = saved_message;
3046c0a3 15985 return NULL;
0a3b29ad 15986 }
15987
15988 /* Peek at the next token. */
15989 token = cp_lexer_peek_token (parser->lexer);
d95d815d 15990
0a3b29ad 15991 /* If the next token is a `)', `,', `=', `>', or `...', then there
d95d815d 15992 is no declarator. However, when variadic templates are enabled,
15993 there may be a declarator following `...'. */
ccb84981 15994 if (token->type == CPP_CLOSE_PAREN
0a3b29ad 15995 || token->type == CPP_COMMA
15996 || token->type == CPP_EQ
0a3b29ad 15997 || token->type == CPP_GREATER)
92b128ed 15998 {
3046c0a3 15999 declarator = NULL;
92b128ed 16000 if (parenthesized_p)
16001 *parenthesized_p = false;
16002 }
0a3b29ad 16003 /* Otherwise, there should be a declarator. */
16004 else
16005 {
16006 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16007 parser->default_arg_ok_p = false;
ccb84981 16008
78662158 16009 /* After seeing a decl-specifier-seq, if the next token is not a
16010 "(", there is no possibility that the code is a valid
41f2d08e 16011 expression. Therefore, if parsing tentatively, we commit at
16012 this point. */
78662158 16013 if (!parser->in_template_argument_list_p
461ec6e9 16014 /* In an expression context, having seen:
41f2d08e 16015
91f31809 16016 (int((char ...
41f2d08e 16017
16018 we cannot be sure whether we are looking at a
91f31809 16019 function-type (taking a "char" as a parameter) or a cast
16020 of some object of type "char" to "int". */
41f2d08e 16021 && !parser->in_type_id_in_expr_p
efcbcf83 16022 && cp_parser_uncommitted_to_tentative_parse_p (parser)
78662158 16023 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16024 cp_parser_commit_to_tentative_parse (parser);
16025 /* Parse the declarator. */
ad9ae192 16026 declarator_token_start = token;
0a3b29ad 16027 declarator = cp_parser_declarator (parser,
42bbd0ec 16028 CP_PARSER_DECLARATOR_EITHER,
92b128ed 16029 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 16030 parenthesized_p,
16031 /*member_p=*/false);
0a3b29ad 16032 parser->default_arg_ok_p = saved_default_arg_ok_p;
b5002156 16033 /* After the declarator, allow more attributes. */
4b9b2871 16034 decl_specifiers.attributes
207355ad 16035 = chainon (decl_specifiers.attributes,
4b9b2871 16036 cp_parser_attributes_opt (parser));
0a3b29ad 16037 }
16038
2aedc2ff 16039 /* If the next token is an ellipsis, and we have not seen a
16040 declarator name, and the type of the declarator contains parameter
16041 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16042 a parameter pack expansion expression. Otherwise, leave the
16043 ellipsis for a C-style variadic function. */
d95d815d 16044 token = cp_lexer_peek_token (parser->lexer);
16045 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16046 {
16047 tree type = decl_specifiers.type;
16048
2aedc2ff 16049 if (type && DECL_P (type))
d95d815d 16050 type = TREE_TYPE (type);
16051
2aedc2ff 16052 if (type
16053 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16054 && declarator_can_be_parameter_pack (declarator)
d95d815d 16055 && (!declarator || !declarator->parameter_pack_p)
16056 && uses_parameter_packs (type))
16057 {
2aedc2ff 16058 /* Consume the `...'. */
16059 cp_lexer_consume_token (parser->lexer);
16060 maybe_warn_variadic_templates ();
16061
16062 /* Build a pack expansion type */
16063 if (declarator)
16064 declarator->parameter_pack_p = true;
16065 else
16066 decl_specifiers.type = make_pack_expansion (type);
16067 }
d95d815d 16068 }
16069
42bbd0ec 16070 /* The restriction on defining new types applies only to the type
0a3b29ad 16071 of the parameter, not to the default argument. */
16072 parser->type_definition_forbidden_message = saved_message;
16073
16074 /* If the next token is `=', then process a default argument. */
16075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16076 {
0a3b29ad 16077 /* Consume the `='. */
16078 cp_lexer_consume_token (parser->lexer);
16079
16080 /* If we are defining a class, then the tokens that make up the
16081 default argument must be saved and processed later. */
ccb84981 16082 if (!template_parm_p && at_class_scope_p ()
a8b75081 16083 && TYPE_BEING_DEFINED (current_class_type)
16084 && !LAMBDA_TYPE_P (current_class_type))
0a3b29ad 16085 {
16086 unsigned depth = 0;
eef53511 16087 int maybe_template_id = 0;
00d26680 16088 cp_token *first_token;
16089 cp_token *token;
0a3b29ad 16090
16091 /* Add tokens until we have processed the entire default
93523877 16092 argument. We add the range [first_token, token). */
00d26680 16093 first_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 16094 while (true)
16095 {
16096 bool done = false;
0a3b29ad 16097
16098 /* Peek at the next token. */
16099 token = cp_lexer_peek_token (parser->lexer);
16100 /* What we do depends on what token we have. */
16101 switch (token->type)
16102 {
16103 /* In valid code, a default argument must be
16104 immediately followed by a `,' `)', or `...'. */
16105 case CPP_COMMA:
eef53511 16106 if (depth == 0 && maybe_template_id)
16107 {
16108 /* If we've seen a '<', we might be in a
16109 template-argument-list. Until Core issue 325 is
16110 resolved, we don't know how this situation ought
16111 to be handled, so try to DTRT. We check whether
16112 what comes after the comma is a valid parameter
16113 declaration list. If it is, then the comma ends
16114 the default argument; otherwise the default
16115 argument continues. */
16116 bool error = false;
57b09892 16117 tree t;
eef53511 16118
16119 /* Set ITALP so cp_parser_parameter_declaration_list
16120 doesn't decide to commit to this parse. */
16121 bool saved_italp = parser->in_template_argument_list_p;
16122 parser->in_template_argument_list_p = true;
16123
16124 cp_parser_parse_tentatively (parser);
16125 cp_lexer_consume_token (parser->lexer);
57b09892 16126 begin_scope (sk_function_parms, NULL_TREE);
eef53511 16127 cp_parser_parameter_declaration_list (parser, &error);
57b09892 16128 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16129 pop_binding (DECL_NAME (t), t);
16130 leave_scope ();
eef53511 16131 if (!cp_parser_error_occurred (parser) && !error)
16132 done = true;
16133 cp_parser_abort_tentative_parse (parser);
16134
16135 parser->in_template_argument_list_p = saved_italp;
16136 break;
16137 }
0a3b29ad 16138 case CPP_CLOSE_PAREN:
16139 case CPP_ELLIPSIS:
16140 /* If we run into a non-nested `;', `}', or `]',
16141 then the code is invalid -- but the default
16142 argument is certainly over. */
16143 case CPP_SEMICOLON:
16144 case CPP_CLOSE_BRACE:
16145 case CPP_CLOSE_SQUARE:
16146 if (depth == 0)
16147 done = true;
16148 /* Update DEPTH, if necessary. */
16149 else if (token->type == CPP_CLOSE_PAREN
16150 || token->type == CPP_CLOSE_BRACE
16151 || token->type == CPP_CLOSE_SQUARE)
16152 --depth;
16153 break;
16154
16155 case CPP_OPEN_PAREN:
16156 case CPP_OPEN_SQUARE:
16157 case CPP_OPEN_BRACE:
16158 ++depth;
16159 break;
16160
eef53511 16161 case CPP_LESS:
16162 if (depth == 0)
16163 /* This might be the comparison operator, or it might
16164 start a template argument list. */
16165 ++maybe_template_id;
16166 break;
16167
56471494 16168 case CPP_RSHIFT:
6dcdb5de 16169 if (cxx_dialect == cxx98)
56471494 16170 break;
16171 /* Fall through for C++0x, which treats the `>>'
16172 operator like two `>' tokens in certain
16173 cases. */
16174
0a3b29ad 16175 case CPP_GREATER:
eef53511 16176 if (depth == 0)
16177 {
16178 /* This might be an operator, or it might close a
16179 template argument list. But if a previous '<'
16180 started a template argument list, this will have
16181 closed it, so we can't be in one anymore. */
16182 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16183 if (maybe_template_id < 0)
16184 maybe_template_id = 0;
16185 }
0a3b29ad 16186 break;
16187
16188 /* If we run out of tokens, issue an error message. */
16189 case CPP_EOF:
b75b98aa 16190 case CPP_PRAGMA_EOL:
ccb59bb6 16191 error_at (token->location, "file ends in default argument");
0a3b29ad 16192 done = true;
16193 break;
16194
16195 case CPP_NAME:
16196 case CPP_SCOPE:
16197 /* In these cases, we should look for template-ids.
ccb84981 16198 For example, if the default argument is
0a3b29ad 16199 `X<int, double>()', we need to do name lookup to
16200 figure out whether or not `X' is a template; if
755edffd 16201 so, the `,' does not end the default argument.
0a3b29ad 16202
16203 That is not yet done. */
16204 break;
16205
16206 default:
16207 break;
16208 }
16209
16210 /* If we've reached the end, stop. */
16211 if (done)
16212 break;
ccb84981 16213
0a3b29ad 16214 /* Add the token to the token block. */
16215 token = cp_lexer_consume_token (parser->lexer);
0a3b29ad 16216 }
00d26680 16217
648a0f57 16218 /* Create a DEFAULT_ARG to represent the unparsed default
653e5405 16219 argument. */
00d26680 16220 default_argument = make_node (DEFAULT_ARG);
16221 DEFARG_TOKENS (default_argument)
f51f5e0b 16222 = cp_token_cache_new (first_token, token);
16223 DEFARG_INSTANTIATIONS (default_argument) = NULL;
0a3b29ad 16224 }
16225 /* Outside of a class definition, we can just parse the
653e5405 16226 assignment-expression. */
0a3b29ad 16227 else
ad9ae192 16228 {
16229 token = cp_lexer_peek_token (parser->lexer);
16230 default_argument
16231 = cp_parser_default_argument (parser, template_parm_p);
16232 }
41341abd 16233
0a3b29ad 16234 if (!parser->default_arg_ok_p)
16235 {
561fec9d 16236 if (flag_permissive)
c3ceba8e 16237 warning (0, "deprecated use of default argument for parameter of non-function");
816786ad 16238 else
16239 {
ccb59bb6 16240 error_at (token->location,
16241 "default arguments are only "
16242 "permitted for function parameters");
816786ad 16243 default_argument = NULL_TREE;
16244 }
0a3b29ad 16245 }
41341abd 16246 else if ((declarator && declarator->parameter_pack_p)
16247 || (decl_specifiers.type
16248 && PACK_EXPANSION_P (decl_specifiers.type)))
16249 {
41341abd 16250 /* Find the name of the parameter pack. */
16251 cp_declarator *id_declarator = declarator;
16252 while (id_declarator && id_declarator->kind != cdk_id)
16253 id_declarator = id_declarator->declarator;
16254
16255 if (id_declarator && id_declarator->kind == cdk_id)
ccb59bb6 16256 error_at (declarator_token_start->location,
16257 template_parm_p
16258 ? "template parameter pack %qD"
16259 " cannot have a default argument"
16260 : "parameter pack %qD cannot have a default argument",
16261 id_declarator->u.id.unqualified_name);
41341abd 16262 else
ccb59bb6 16263 error_at (declarator_token_start->location,
16264 template_parm_p
16265 ? "template parameter pack cannot have a default argument"
16266 : "parameter pack cannot have a default argument");
41341abd 16267
16268 default_argument = NULL_TREE;
16269 }
0a3b29ad 16270 }
16271 else
16272 default_argument = NULL_TREE;
ccb84981 16273
4b9b2871 16274 return make_parameter_declarator (&decl_specifiers,
3046c0a3 16275 declarator,
16276 default_argument);
0a3b29ad 16277}
16278
41341abd 16279/* Parse a default argument and return it.
16280
16281 TEMPLATE_PARM_P is true if this is a default argument for a
16282 non-type template parameter. */
16283static tree
16284cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16285{
16286 tree default_argument = NULL_TREE;
16287 bool saved_greater_than_is_operator_p;
16288 bool saved_local_variables_forbidden_p;
16289
16290 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16291 set correctly. */
16292 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16293 parser->greater_than_is_operator_p = !template_parm_p;
16294 /* Local variable names (and the `this' keyword) may not
16295 appear in a default argument. */
16296 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16297 parser->local_variables_forbidden_p = true;
41341abd 16298 /* Parse the assignment-expression. */
16299 if (template_parm_p)
16300 push_deferring_access_checks (dk_no_deferred);
16301 default_argument
98b326fd 16302 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
41341abd 16303 if (template_parm_p)
16304 pop_deferring_access_checks ();
41341abd 16305 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16306 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16307
16308 return default_argument;
16309}
16310
0a3b29ad 16311/* Parse a function-body.
16312
16313 function-body:
16314 compound_statement */
16315
16316static void
16317cp_parser_function_body (cp_parser *parser)
16318{
2363ef00 16319 cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 16320}
16321
16322/* Parse a ctor-initializer-opt followed by a function-body. Return
16323 true if a ctor-initializer was present. */
16324
16325static bool
16326cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16327{
ca63c29a 16328 tree body, list;
0a3b29ad 16329 bool ctor_initializer_p;
ca63c29a 16330 const bool check_body_p =
16331 DECL_CONSTRUCTOR_P (current_function_decl)
16332 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16333 tree last = NULL;
0a3b29ad 16334
16335 /* Begin the function body. */
16336 body = begin_function_body ();
16337 /* Parse the optional ctor-initializer. */
16338 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
ca63c29a 16339
16340 /* If we're parsing a constexpr constructor definition, we need
16341 to check that the constructor body is indeed empty. However,
16342 before we get to cp_parser_function_body lot of junk has been
16343 generated, so we can't just check that we have an empty block.
16344 Rather we take a snapshot of the outermost block, and check whether
16345 cp_parser_function_body changed its state. */
16346 if (check_body_p)
16347 {
16348 list = body;
16349 if (TREE_CODE (list) == BIND_EXPR)
16350 list = BIND_EXPR_BODY (list);
16351 if (TREE_CODE (list) == STATEMENT_LIST
16352 && STATEMENT_LIST_TAIL (list) != NULL)
16353 last = STATEMENT_LIST_TAIL (list)->stmt;
16354 }
0a3b29ad 16355 /* Parse the function-body. */
16356 cp_parser_function_body (parser);
fdf548d1 16357 if (check_body_p)
16358 check_constexpr_ctor_body (last, list);
0a3b29ad 16359 /* Finish the function body. */
16360 finish_function_body (body);
16361
16362 return ctor_initializer_p;
16363}
16364
16365/* Parse an initializer.
16366
16367 initializer:
16368 = initializer-clause
ccb84981 16369 ( expression-list )
0a3b29ad 16370
e4bc96e2 16371 Returns an expression representing the initializer. If no
ccb84981 16372 initializer is present, NULL_TREE is returned.
0a3b29ad 16373
f82f1250 16374 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16375 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16376 set to TRUE if there is no initializer present. If there is an
878870b4 16377 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16378 is set to true; otherwise it is set to false. */
0a3b29ad 16379
16380static tree
f82f1250 16381cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
878870b4 16382 bool* non_constant_p)
0a3b29ad 16383{
16384 cp_token *token;
16385 tree init;
16386
16387 /* Peek at the next token. */
16388 token = cp_lexer_peek_token (parser->lexer);
16389
16390 /* Let our caller know whether or not this initializer was
16391 parenthesized. */
f82f1250 16392 *is_direct_init = (token->type != CPP_EQ);
878870b4 16393 /* Assume that the initializer is constant. */
16394 *non_constant_p = false;
0a3b29ad 16395
16396 if (token->type == CPP_EQ)
16397 {
16398 /* Consume the `='. */
16399 cp_lexer_consume_token (parser->lexer);
16400 /* Parse the initializer-clause. */
878870b4 16401 init = cp_parser_initializer_clause (parser, non_constant_p);
0a3b29ad 16402 }
16403 else if (token->type == CPP_OPEN_PAREN)
f352a3fb 16404 {
16405 VEC(tree,gc) *vec;
33199a81 16406 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 16407 /*cast_p=*/false,
16408 /*allow_expansion_p=*/true,
16409 non_constant_p);
16410 if (vec == NULL)
16411 return error_mark_node;
16412 init = build_tree_list_vec (vec);
16413 release_tree_vector (vec);
16414 }
f82f1250 16415 else if (token->type == CPP_OPEN_BRACE)
16416 {
bf8d19fe 16417 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 16418 init = cp_parser_braced_list (parser, non_constant_p);
16419 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16420 }
0a3b29ad 16421 else
16422 {
16423 /* Anything else is an error. */
16424 cp_parser_error (parser, "expected initializer");
16425 init = error_mark_node;
16426 }
16427
16428 return init;
16429}
16430
ccb84981 16431/* Parse an initializer-clause.
0a3b29ad 16432
16433 initializer-clause:
16434 assignment-expression
f82f1250 16435 braced-init-list
0a3b29ad 16436
ccb84981 16437 Returns an expression representing the initializer.
0a3b29ad 16438
16439 If the `assignment-expression' production is used the value
ccb84981 16440 returned is simply a representation for the expression.
0a3b29ad 16441
f82f1250 16442 Otherwise, calls cp_parser_braced_list. */
0a3b29ad 16443
16444static tree
878870b4 16445cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
0a3b29ad 16446{
16447 tree initializer;
16448
53073dc9 16449 /* Assume the expression is constant. */
16450 *non_constant_p = false;
16451
0a3b29ad 16452 /* If it is not a `{', then we are looking at an
16453 assignment-expression. */
16454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
f352cc1d 16455 {
207355ad 16456 initializer
f352cc1d 16457 = cp_parser_constant_expression (parser,
16458 /*allow_non_constant_p=*/true,
16459 non_constant_p);
16460 if (!*non_constant_p)
ce984e5e 16461 {
16462 /* We only want to fold if this is really a constant
16463 expression. FIXME Actually, we don't want to fold here, but in
16464 cp_finish_decl. */
16465 tree folded = fold_non_dependent_expr (initializer);
16466 folded = maybe_constant_value (folded);
16467 if (TREE_CONSTANT (folded))
16468 initializer = folded;
16469 }
f352cc1d 16470 }
0a3b29ad 16471 else
f82f1250 16472 initializer = cp_parser_braced_list (parser, non_constant_p);
16473
16474 return initializer;
16475}
16476
16477/* Parse a brace-enclosed initializer list.
16478
16479 braced-init-list:
16480 { initializer-list , [opt] }
16481 { }
16482
16483 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16484 the elements of the initializer-list (or NULL, if the last
16485 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16486 NULL_TREE. There is no way to detect whether or not the optional
16487 trailing `,' was provided. NON_CONSTANT_P is as for
16488 cp_parser_initializer. */
16489
16490static tree
16491cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16492{
16493 tree initializer;
16494
16495 /* Consume the `{' token. */
16496 cp_lexer_consume_token (parser->lexer);
16497 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16498 initializer = make_node (CONSTRUCTOR);
16499 /* If it's not a `}', then there is a non-trivial initializer. */
16500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
0a3b29ad 16501 {
f82f1250 16502 /* Parse the initializer list. */
16503 CONSTRUCTOR_ELTS (initializer)
16504 = cp_parser_initializer_list (parser, non_constant_p);
16505 /* A trailing `,' token is allowed. */
16506 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16507 cp_lexer_consume_token (parser->lexer);
0a3b29ad 16508 }
f82f1250 16509 /* Now, there should be a trailing `}'. */
c247dce0 16510 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
f82f1250 16511 TREE_TYPE (initializer) = init_list_type_node;
0a3b29ad 16512 return initializer;
16513}
16514
16515/* Parse an initializer-list.
16516
16517 initializer-list:
d95d815d 16518 initializer-clause ... [opt]
16519 initializer-list , initializer-clause ... [opt]
0a3b29ad 16520
16521 GNU Extension:
ccb84981 16522
0a3b29ad 16523 initializer-list:
16524 identifier : initializer-clause
16525 initializer-list, identifier : initializer-clause
16526
c75b4594 16527 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16528 for the initializer. If the INDEX of the elt is non-NULL, it is the
878870b4 16529 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16530 as for cp_parser_initializer. */
0a3b29ad 16531
c75b4594 16532static VEC(constructor_elt,gc) *
878870b4 16533cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
0a3b29ad 16534{
c75b4594 16535 VEC(constructor_elt,gc) *v = NULL;
0a3b29ad 16536
878870b4 16537 /* Assume all of the expressions are constant. */
16538 *non_constant_p = false;
16539
0a3b29ad 16540 /* Parse the rest of the list. */
16541 while (true)
16542 {
16543 cp_token *token;
16544 tree identifier;
16545 tree initializer;
878870b4 16546 bool clause_non_constant_p;
16547
0a3b29ad 16548 /* If the next token is an identifier and the following one is a
16549 colon, we are looking at the GNU designated-initializer
16550 syntax. */
16551 if (cp_parser_allow_gnu_extensions_p (parser)
16552 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16553 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16554 {
1d8baa0e 16555 /* Warn the user that they are using an extension. */
21ca8540 16556 pedwarn (input_location, OPT_pedantic,
8864917d 16557 "ISO C++ does not allow designated initializers");
0a3b29ad 16558 /* Consume the identifier. */
3369eb76 16559 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 16560 /* Consume the `:'. */
16561 cp_lexer_consume_token (parser->lexer);
16562 }
16563 else
16564 identifier = NULL_TREE;
16565
16566 /* Parse the initializer. */
ccb84981 16567 initializer = cp_parser_initializer_clause (parser,
878870b4 16568 &clause_non_constant_p);
16569 /* If any clause is non-constant, so is the entire initializer. */
16570 if (clause_non_constant_p)
16571 *non_constant_p = true;
c75b4594 16572
d95d815d 16573 /* If we have an ellipsis, this is an initializer pack
16574 expansion. */
16575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16576 {
16577 /* Consume the `...'. */
16578 cp_lexer_consume_token (parser->lexer);
16579
16580 /* Turn the initializer into an initializer expansion. */
16581 initializer = make_pack_expansion (initializer);
16582 }
16583
c75b4594 16584 /* Add it to the vector. */
16585 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
0a3b29ad 16586
16587 /* If the next token is not a comma, we have reached the end of
16588 the list. */
16589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16590 break;
16591
16592 /* Peek at the next token. */
16593 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16594 /* If the next token is a `}', then we're still done. An
16595 initializer-clause can have a trailing `,' after the
16596 initializer-list and before the closing `}'. */
16597 if (token->type == CPP_CLOSE_BRACE)
16598 break;
16599
16600 /* Consume the `,' token. */
16601 cp_lexer_consume_token (parser->lexer);
16602 }
16603
c75b4594 16604 return v;
0a3b29ad 16605}
16606
16607/* Classes [gram.class] */
16608
16609/* Parse a class-name.
16610
16611 class-name:
16612 identifier
16613 template-id
16614
16615 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16616 to indicate that names looked up in dependent types should be
16617 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16618 keyword has been used to indicate that the name that appears next
e2ae55f2 16619 is a template. TAG_TYPE indicates the explicit tag given before
16620 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16621 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16622 is the class being defined in a class-head.
0a3b29ad 16623
16624 Returns the TYPE_DECL representing the class. */
16625
16626static tree
ccb84981 16627cp_parser_class_name (cp_parser *parser,
16628 bool typename_keyword_p,
16629 bool template_keyword_p,
e2ae55f2 16630 enum tag_types tag_type,
0a3b29ad 16631 bool check_dependency_p,
3d0f901b 16632 bool class_head_p,
16633 bool is_declaration)
0a3b29ad 16634{
16635 tree decl;
16636 tree scope;
16637 bool typename_p;
2c593bd0 16638 cp_token *token;
a6c68ee8 16639 tree identifier = NULL_TREE;
2c593bd0 16640
16641 /* All class-names start with an identifier. */
16642 token = cp_lexer_peek_token (parser->lexer);
16643 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16644 {
16645 cp_parser_error (parser, "expected class-name");
16646 return error_mark_node;
16647 }
ccb84981 16648
0a3b29ad 16649 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16650 to a template-id, so we save it here. */
16651 scope = parser->scope;
1e9847d8 16652 if (scope == error_mark_node)
16653 return error_mark_node;
ccb84981 16654
0a3b29ad 16655 /* Any name names a type if we're following the `typename' keyword
16656 in a qualified name where the enclosing scope is type-dependent. */
16657 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
7e9a6a16 16658 && dependent_type_p (scope));
2c593bd0 16659 /* Handle the common case (an identifier, but not a template-id)
16660 efficiently. */
ccb84981 16661 if (token->type == CPP_NAME
c8d5ab79 16662 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
0a3b29ad 16663 {
b62240d5 16664 cp_token *identifier_token;
b62240d5 16665 bool ambiguous_p;
0a3b29ad 16666
16667 /* Look for the identifier. */
b62240d5 16668 identifier_token = cp_lexer_peek_token (parser->lexer);
16669 ambiguous_p = identifier_token->ambiguous_p;
0a3b29ad 16670 identifier = cp_parser_identifier (parser);
16671 /* If the next token isn't an identifier, we are certainly not
16672 looking at a class-name. */
16673 if (identifier == error_mark_node)
16674 decl = error_mark_node;
16675 /* If we know this is a type-name, there's no need to look it
16676 up. */
16677 else if (typename_p)
16678 decl = identifier;
16679 else
16680 {
b62240d5 16681 tree ambiguous_decls;
16682 /* If we already know that this lookup is ambiguous, then
16683 we've already issued an error message; there's no reason
16684 to check again. */
16685 if (ambiguous_p)
16686 {
16687 cp_parser_simulate_error (parser);
16688 return error_mark_node;
16689 }
0a3b29ad 16690 /* If the next token is a `::', then the name must be a type
16691 name.
16692
16693 [basic.lookup.qual]
16694
16695 During the lookup for a name preceding the :: scope
16696 resolution operator, object, function, and enumerator
16697 names are ignored. */
16698 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
e2ae55f2 16699 tag_type = typename_type;
0a3b29ad 16700 /* Look up the name. */
ccb84981 16701 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 16702 tag_type,
c3b9e457 16703 /*is_template=*/false,
6fc758aa 16704 /*is_namespace=*/false,
2cdbcd51 16705 check_dependency_p,
ad9ae192 16706 &ambiguous_decls,
16707 identifier_token->location);
b62240d5 16708 if (ambiguous_decls)
16709 {
b62240d5 16710 if (cp_parser_parsing_tentatively (parser))
4272a2e8 16711 cp_parser_simulate_error (parser);
b62240d5 16712 return error_mark_node;
16713 }
0a3b29ad 16714 }
16715 }
2c593bd0 16716 else
16717 {
16718 /* Try a template-id. */
16719 decl = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 16720 check_dependency_p,
16721 is_declaration);
2c593bd0 16722 if (decl == error_mark_node)
16723 return error_mark_node;
16724 }
0a3b29ad 16725
16726 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16727
16728 /* If this is a typename, create a TYPENAME_TYPE. */
16729 if (typename_p && decl != error_mark_node)
50dad21f 16730 {
c31fdaf6 16731 decl = make_typename_type (scope, decl, typename_type,
074ab442 16732 /*complain=*/tf_error);
50dad21f 16733 if (decl != error_mark_node)
16734 decl = TYPE_NAME (decl);
16735 }
0a3b29ad 16736
16737 /* Check to see that it is really the name of a class. */
ccb84981 16738 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
0a3b29ad 16739 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16740 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16741 /* Situations like this:
16742
16743 template <typename T> struct A {
ccb84981 16744 typename T::template X<int>::I i;
0a3b29ad 16745 };
16746
16747 are problematic. Is `T::template X<int>' a class-name? The
16748 standard does not seem to be definitive, but there is no other
16749 valid interpretation of the following `::'. Therefore, those
16750 names are considered class-names. */
0a3b29ad 16751 {
64b06433 16752 decl = make_typename_type (scope, decl, tag_type, tf_error);
16753 if (decl != error_mark_node)
16754 decl = TYPE_NAME (decl);
0a3b29ad 16755 }
64b06433 16756 else if (TREE_CODE (decl) != TYPE_DECL
16757 || TREE_TYPE (decl) == error_mark_node
b0d0931f 16758 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16759 /* In Objective-C 2.0, a classname followed by '.' starts a
16760 dot-syntax expression, and it's not a type-name. */
16761 || (c_dialect_objc ()
16762 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16763 && objc_is_class_name (decl)))
64b06433 16764 decl = error_mark_node;
16765
16766 if (decl == error_mark_node)
16767 cp_parser_error (parser, "expected class-name");
a6c68ee8 16768 else if (identifier && !parser->scope)
16769 maybe_note_name_used_in_class (identifier, decl);
0a3b29ad 16770
16771 return decl;
16772}
16773
16774/* Parse a class-specifier.
16775
16776 class-specifier:
16777 class-head { member-specification [opt] }
16778
16779 Returns the TREE_TYPE representing the class. */
16780
16781static tree
45baea8b 16782cp_parser_class_specifier (cp_parser* parser)
0a3b29ad 16783{
0a3b29ad 16784 tree type;
4ee9c684 16785 tree attributes = NULL_TREE;
0a3b29ad 16786 bool nested_name_specifier_p;
0a3b29ad 16787 unsigned saved_num_template_parameter_lists;
0aeb1cc5 16788 bool saved_in_function_body;
f0475530 16789 bool saved_in_unbraced_linkage_specification_p;
352baa70 16790 tree old_scope = NULL_TREE;
534d03ee 16791 tree scope = NULL_TREE;
1a9b4c25 16792 tree bases;
0a3b29ad 16793
4f62c42e 16794 push_deferring_access_checks (dk_no_deferred);
9b57b06b 16795
0a3b29ad 16796 /* Parse the class-head. */
16797 type = cp_parser_class_head (parser,
fb3e3237 16798 &nested_name_specifier_p,
1a9b4c25 16799 &attributes,
16800 &bases);
0a3b29ad 16801 /* If the class-head was a semantic disaster, skip the entire body
16802 of the class. */
16803 if (!type)
16804 {
16805 cp_parser_skip_to_end_of_block_or_statement (parser);
9b57b06b 16806 pop_deferring_access_checks ();
0a3b29ad 16807 return error_mark_node;
16808 }
9b57b06b 16809
0a3b29ad 16810 /* Look for the `{'. */
c247dce0 16811 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9b57b06b 16812 {
16813 pop_deferring_access_checks ();
16814 return error_mark_node;
16815 }
16816
1a9b4c25 16817 /* Process the base classes. If they're invalid, skip the
16818 entire class body. */
16819 if (!xref_basetypes (type, bases))
16820 {
1a9b4c25 16821 /* Consuming the closing brace yields better error messages
16822 later on. */
9aad947b 16823 if (cp_parser_skip_to_closing_brace (parser))
16824 cp_lexer_consume_token (parser->lexer);
1a9b4c25 16825 pop_deferring_access_checks ();
16826 return error_mark_node;
16827 }
16828
0a3b29ad 16829 /* Issue an error message if type-definitions are forbidden here. */
16830 cp_parser_check_type_definition (parser);
16831 /* Remember that we are defining one more class. */
16832 ++parser->num_classes_being_defined;
16833 /* Inside the class, surrounding template-parameter-lists do not
16834 apply. */
ccb84981 16835 saved_num_template_parameter_lists
16836 = parser->num_template_parameter_lists;
0a3b29ad 16837 parser->num_template_parameter_lists = 0;
0aeb1cc5 16838 /* We are not in a function body. */
16839 saved_in_function_body = parser->in_function_body;
16840 parser->in_function_body = false;
f0475530 16841 /* We are not immediately inside an extern "lang" block. */
16842 saved_in_unbraced_linkage_specification_p
16843 = parser->in_unbraced_linkage_specification_p;
16844 parser->in_unbraced_linkage_specification_p = false;
4cab8273 16845
0a3b29ad 16846 /* Start the class. */
4ccffa39 16847 if (nested_name_specifier_p)
534d03ee 16848 {
16849 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
352baa70 16850 old_scope = push_inner_scope (scope);
534d03ee 16851 }
4a2849cb 16852 type = begin_class_definition (type, attributes);
207355ad 16853
0a3b29ad 16854 if (type == error_mark_node)
6beb3f76 16855 /* If the type is erroneous, skip the entire body of the class. */
0a3b29ad 16856 cp_parser_skip_to_closing_brace (parser);
16857 else
16858 /* Parse the member-specification. */
16859 cp_parser_member_specification_opt (parser);
207355ad 16860
0a3b29ad 16861 /* Look for the trailing `}'. */
c247dce0 16862 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
fb3e3237 16863 /* Look for trailing attributes to apply to this class. */
0a3b29ad 16864 if (cp_parser_allow_gnu_extensions_p (parser))
4a2849cb 16865 attributes = cp_parser_attributes_opt (parser);
fb3e3237 16866 if (type != error_mark_node)
16867 type = finish_struct (type, attributes);
352baa70 16868 if (nested_name_specifier_p)
16869 pop_inner_scope (old_scope, scope);
a9ffdd35 16870
16871 /* We've finished a type definition. Check for the common syntax
16872 error of forgetting a semicolon after the definition. We need to
16873 be careful, as we can't just check for not-a-semicolon and be done
16874 with it; the user might have typed:
16875
16876 class X { } c = ...;
16877 class X { } *p = ...;
16878
16879 and so forth. Instead, enumerate all the possible tokens that
16880 might follow this production; if we don't see one of them, then
16881 complain and silently insert the semicolon. */
16882 {
16883 cp_token *token = cp_lexer_peek_token (parser->lexer);
16884 bool want_semicolon = true;
16885
16886 switch (token->type)
16887 {
16888 case CPP_NAME:
16889 case CPP_SEMICOLON:
16890 case CPP_MULT:
16891 case CPP_AND:
16892 case CPP_OPEN_PAREN:
16893 case CPP_CLOSE_PAREN:
16894 case CPP_COMMA:
16895 want_semicolon = false;
16896 break;
16897
16898 /* While it's legal for type qualifiers and storage class
16899 specifiers to follow type definitions in the grammar, only
16900 compiler testsuites contain code like that. Assume that if
16901 we see such code, then what we're really seeing is a case
16902 like:
16903
16904 class X { }
16905 const <type> var = ...;
16906
16907 or
16908
16909 class Y { }
16910 static <type> func (...) ...
16911
16912 i.e. the qualifier or specifier applies to the next
16913 declaration. To do so, however, we need to look ahead one
16914 more token to see if *that* token is a type specifier.
16915
16916 This code could be improved to handle:
16917
16918 class Z { }
16919 static const <type> var = ...; */
16920 case CPP_KEYWORD:
16921 if (keyword_is_storage_class_specifier (token->keyword)
16922 || keyword_is_type_qualifier (token->keyword))
16923 {
16924 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16925
16926 if (lookahead->type == CPP_KEYWORD
16927 && !keyword_begins_type_specifier (lookahead->keyword))
16928 want_semicolon = false;
16929 else if (lookahead->type == CPP_NAME)
16930 /* Handling user-defined types here would be nice, but
16931 very tricky. */
16932 want_semicolon = false;
16933 }
16934 break;
16935 default:
16936 break;
16937 }
16938
16939 if (want_semicolon)
16940 {
16941 cp_token_position prev
16942 = cp_lexer_previous_token_position (parser->lexer);
16943 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16944 location_t loc = prev_token->location;
16945
16946 if (CLASSTYPE_DECLARED_CLASS (type))
16947 error_at (loc, "expected %<;%> after class definition");
16948 else if (TREE_CODE (type) == RECORD_TYPE)
16949 error_at (loc, "expected %<;%> after struct definition");
16950 else if (TREE_CODE (type) == UNION_TYPE)
16951 error_at (loc, "expected %<;%> after union definition");
16952 else
16953 gcc_unreachable ();
16954
16955 /* Unget one token and smash it to look as though we encountered
16956 a semicolon in the input stream. */
16957 cp_lexer_set_token_position (parser->lexer, prev);
16958 token = cp_lexer_peek_token (parser->lexer);
16959 token->type = CPP_SEMICOLON;
16960 token->keyword = RID_MAX;
16961 }
16962 }
16963
0a3b29ad 16964 /* If this class is not itself within the scope of another class,
16965 then we need to parse the bodies of all of the queued function
16966 definitions. Note that the queued functions defined in a class
16967 are not always processed immediately following the
16968 class-specifier for that class. Consider:
16969
16970 struct A {
653e5405 16971 struct B { void f() { sizeof (A); } };
0a3b29ad 16972 };
16973
16974 If `f' were processed before the processing of `A' were
16975 completed, there would be no way to compute the size of `A'.
16976 Note that the nesting we are interested in here is lexical --
16977 not the semantic nesting given by TYPE_CONTEXT. In particular,
16978 for:
16979
16980 struct A { struct B; };
16981 struct A::B { void f() { } };
16982
16983 there is no need to delay the parsing of `A::B::f'. */
ccb84981 16984 if (--parser->num_classes_being_defined == 0)
0a3b29ad 16985 {
af128372 16986 tree fn;
7f602bca 16987 tree class_type = NULL_TREE;
16988 tree pushed_scope = NULL_TREE;
9177da82 16989 unsigned ix;
16990 cp_default_arg_entry *e;
074ab442 16991
af128372 16992 /* In a first pass, parse default arguments to the functions.
16993 Then, in a second pass, parse the bodies of the functions.
16994 This two-phased approach handles cases like:
ccb84981 16995
16996 struct S {
653e5405 16997 void f() { g(); }
16998 void g(int i = 3);
16999 };
af128372 17000
653e5405 17001 */
48148244 17002 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17003 ix, e)
af128372 17004 {
9177da82 17005 fn = e->decl;
af128372 17006 /* If there are default arguments that have not yet been processed,
17007 take care of them now. */
9177da82 17008 if (class_type != e->class_type)
93c149df 17009 {
7f602bca 17010 if (pushed_scope)
17011 pop_scope (pushed_scope);
9177da82 17012 class_type = e->class_type;
7f602bca 17013 pushed_scope = push_scope (class_type);
93c149df 17014 }
17015 /* Make sure that any template parameters are in scope. */
17016 maybe_begin_member_template_processing (fn);
17017 /* Parse the default argument expressions. */
af128372 17018 cp_parser_late_parsing_default_args (parser, fn);
17019 /* Remove any template parameters from the symbol table. */
17020 maybe_end_member_template_processing ();
17021 }
7f602bca 17022 if (pushed_scope)
17023 pop_scope (pushed_scope);
9177da82 17024 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
af128372 17025 /* Now parse the body of the functions. */
48148244 17026 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
9177da82 17027 cp_parser_late_parsing_for_member (parser, fn);
17028 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
0a3b29ad 17029 }
17030
17031 /* Put back any saved access checks. */
9b57b06b 17032 pop_deferring_access_checks ();
0a3b29ad 17033
0aeb1cc5 17034 /* Restore saved state. */
17035 parser->in_function_body = saved_in_function_body;
0a3b29ad 17036 parser->num_template_parameter_lists
17037 = saved_num_template_parameter_lists;
f0475530 17038 parser->in_unbraced_linkage_specification_p
17039 = saved_in_unbraced_linkage_specification_p;
0a3b29ad 17040
17041 return type;
17042}
17043
17044/* Parse a class-head.
17045
17046 class-head:
17047 class-key identifier [opt] base-clause [opt]
17048 class-key nested-name-specifier identifier base-clause [opt]
ccb84981 17049 class-key nested-name-specifier [opt] template-id
17050 base-clause [opt]
0a3b29ad 17051
17052 GNU Extensions:
17053 class-key attributes identifier [opt] base-clause [opt]
17054 class-key attributes nested-name-specifier identifier base-clause [opt]
ccb84981 17055 class-key attributes nested-name-specifier [opt] template-id
17056 base-clause [opt]
0a3b29ad 17057
4595a3b3 17058 Upon return BASES is initialized to the list of base classes (or
17059 NULL, if there are none) in the same form returned by
17060 cp_parser_base_clause.
17061
0a3b29ad 17062 Returns the TYPE of the indicated class. Sets
17063 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17064 involving a nested-name-specifier was used, and FALSE otherwise.
0a3b29ad 17065
8d52e9a6 17066 Returns error_mark_node if this is not a class-head.
9031d10b 17067
0a3b29ad 17068 Returns NULL_TREE if the class-head is syntactically valid, but
17069 semantically invalid in a way that means we should skip the entire
17070 body of the class. */
17071
17072static tree
ccb84981 17073cp_parser_class_head (cp_parser* parser,
fb3e3237 17074 bool* nested_name_specifier_p,
1a9b4c25 17075 tree *attributes_p,
17076 tree *bases)
0a3b29ad 17077{
0a3b29ad 17078 tree nested_name_specifier;
17079 enum tag_types class_key;
17080 tree id = NULL_TREE;
17081 tree type = NULL_TREE;
17082 tree attributes;
17083 bool template_id_p = false;
17084 bool qualified_p = false;
17085 bool invalid_nested_name_p = false;
e16b1a13 17086 bool invalid_explicit_specialization_p = false;
7f602bca 17087 tree pushed_scope = NULL_TREE;
0a3b29ad 17088 unsigned num_templates;
ad9ae192 17089 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
0a3b29ad 17090 /* Assume no nested-name-specifier will be present. */
17091 *nested_name_specifier_p = false;
17092 /* Assume no template parameter lists will be used in defining the
17093 type. */
17094 num_templates = 0;
17095
4595a3b3 17096 *bases = NULL_TREE;
17097
0a3b29ad 17098 /* Look for the class-key. */
17099 class_key = cp_parser_class_key (parser);
17100 if (class_key == none_type)
17101 return error_mark_node;
17102
17103 /* Parse the attributes. */
17104 attributes = cp_parser_attributes_opt (parser);
17105
17106 /* If the next token is `::', that is invalid -- but sometimes
17107 people do try to write:
17108
ccb84981 17109 struct ::S {};
0a3b29ad 17110
17111 Handle this gracefully by accepting the extra qualifier, and then
17112 issuing an error about it later if this really is a
b3c48b5d 17113 class-head. If it turns out just to be an elaborated type
0a3b29ad 17114 specifier, remain silent. */
130bb1d4 17115 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
0a3b29ad 17116 qualified_p = true;
17117
4f62c42e 17118 push_deferring_access_checks (dk_no_check);
17119
0a3b29ad 17120 /* Determine the name of the class. Begin by looking for an
17121 optional nested-name-specifier. */
ad9ae192 17122 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
ccb84981 17123 nested_name_specifier
0a3b29ad 17124 = cp_parser_nested_name_specifier_opt (parser,
17125 /*typename_keyword_p=*/false,
1bd3a9f9 17126 /*check_dependency_p=*/false,
3d0f901b 17127 /*type_p=*/false,
17128 /*is_declaration=*/false);
0a3b29ad 17129 /* If there was a nested-name-specifier, then there *must* be an
17130 identifier. */
17131 if (nested_name_specifier)
17132 {
ad9ae192 17133 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 17134 /* Although the grammar says `identifier', it really means
17135 `class-name' or `template-name'. You are only allowed to
17136 define a class that has already been declared with this
ccb84981 17137 syntax.
0a3b29ad 17138
b4f2a576 17139 The proposed resolution for Core Issue 180 says that wherever
0a3b29ad 17140 you see `class T::X' you should treat `X' as a type-name.
ccb84981 17141
0a3b29ad 17142 It is OK to define an inaccessible class; for example:
ccb84981 17143
653e5405 17144 class A { class B; };
17145 class A::B {};
ccb84981 17146
653e5405 17147 We do not know if we will see a class-name, or a
0a3b29ad 17148 template-name. We look for a class-name first, in case the
17149 class-name is a template-id; if we looked for the
17150 template-name first we would stop after the template-name. */
17151 cp_parser_parse_tentatively (parser);
17152 type = cp_parser_class_name (parser,
17153 /*typename_keyword_p=*/false,
17154 /*template_keyword_p=*/false,
e2ae55f2 17155 class_type,
0a3b29ad 17156 /*check_dependency_p=*/false,
3d0f901b 17157 /*class_head_p=*/true,
17158 /*is_declaration=*/false);
0a3b29ad 17159 /* If that didn't work, ignore the nested-name-specifier. */
17160 if (!cp_parser_parse_definitely (parser))
17161 {
17162 invalid_nested_name_p = true;
eef0ab03 17163 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 17164 id = cp_parser_identifier (parser);
17165 if (id == error_mark_node)
17166 id = NULL_TREE;
17167 }
17168 /* If we could not find a corresponding TYPE, treat this
17169 declaration like an unqualified declaration. */
17170 if (type == error_mark_node)
17171 nested_name_specifier = NULL_TREE;
17172 /* Otherwise, count the number of templates used in TYPE and its
17173 containing scopes. */
ccb84981 17174 else
0a3b29ad 17175 {
17176 tree scope;
17177
ccb84981 17178 for (scope = TREE_TYPE (type);
0a3b29ad 17179 scope && TREE_CODE (scope) != NAMESPACE_DECL;
ccb84981 17180 scope = (TYPE_P (scope)
0a3b29ad 17181 ? TYPE_CONTEXT (scope)
ccb84981 17182 : DECL_CONTEXT (scope)))
17183 if (TYPE_P (scope)
0a3b29ad 17184 && CLASS_TYPE_P (scope)
17185 && CLASSTYPE_TEMPLATE_INFO (scope)
b3c48b5d 17186 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17187 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
0a3b29ad 17188 ++num_templates;
17189 }
17190 }
17191 /* Otherwise, the identifier is optional. */
17192 else
17193 {
17194 /* We don't know whether what comes next is a template-id,
17195 an identifier, or nothing at all. */
17196 cp_parser_parse_tentatively (parser);
17197 /* Check for a template-id. */
ad9ae192 17198 type_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 17199 id = cp_parser_template_id (parser,
0a3b29ad 17200 /*template_keyword_p=*/false,
3d0f901b 17201 /*check_dependency_p=*/true,
17202 /*is_declaration=*/true);
0a3b29ad 17203 /* If that didn't work, it could still be an identifier. */
17204 if (!cp_parser_parse_definitely (parser))
17205 {
17206 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
eef0ab03 17207 {
17208 type_start_token = cp_lexer_peek_token (parser->lexer);
17209 id = cp_parser_identifier (parser);
17210 }
0a3b29ad 17211 else
17212 id = NULL_TREE;
17213 }
17214 else
17215 {
17216 template_id_p = true;
17217 ++num_templates;
17218 }
17219 }
17220
4f62c42e 17221 pop_deferring_access_checks ();
17222
e67a67ea 17223 if (id)
eef0ab03 17224 cp_parser_check_for_invalid_template_id (parser, id,
17225 type_start_token->location);
1157e9f0 17226
0a3b29ad 17227 /* If it's not a `:' or a `{' then we can't really be looking at a
17228 class-head, since a class-head only appears as part of a
17229 class-specifier. We have to detect this situation before calling
17230 xref_tag, since that has irreversible side-effects. */
17231 if (!cp_parser_next_token_starts_class_definition_p (parser))
17232 {
a2c5b975 17233 cp_parser_error (parser, "expected %<{%> or %<:%>");
0a3b29ad 17234 return error_mark_node;
17235 }
17236
17237 /* At this point, we're going ahead with the class-specifier, even
17238 if some other problem occurs. */
17239 cp_parser_commit_to_tentative_parse (parser);
17240 /* Issue the error about the overly-qualified name now. */
17241 if (qualified_p)
71b7ff50 17242 {
17243 cp_parser_error (parser,
17244 "global qualification of class name is invalid");
17245 return error_mark_node;
17246 }
0a3b29ad 17247 else if (invalid_nested_name_p)
71b7ff50 17248 {
17249 cp_parser_error (parser,
17250 "qualified name does not name a class");
17251 return error_mark_node;
17252 }
600be0e5 17253 else if (nested_name_specifier)
17254 {
17255 tree scope;
b0ab2aae 17256
17257 /* Reject typedef-names in class heads. */
17258 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17259 {
ccb59bb6 17260 error_at (type_start_token->location,
17261 "invalid class name in declaration of %qD",
17262 type);
b0ab2aae 17263 type = NULL_TREE;
17264 goto done;
17265 }
17266
600be0e5 17267 /* Figure out in what scope the declaration is being placed. */
17268 scope = current_scope ();
600be0e5 17269 /* If that scope does not contain the scope in which the
17270 class was originally declared, the program is invalid. */
17271 if (scope && !is_ancestor (scope, nested_name_specifier))
17272 {
b2df4109 17273 if (at_namespace_scope_p ())
ccb59bb6 17274 error_at (type_start_token->location,
17275 "declaration of %qD in namespace %qD which does not "
17276 "enclose %qD",
17277 type, scope, nested_name_specifier);
b2df4109 17278 else
ccb59bb6 17279 error_at (type_start_token->location,
17280 "declaration of %qD in %qD which does not enclose %qD",
17281 type, scope, nested_name_specifier);
600be0e5 17282 type = NULL_TREE;
17283 goto done;
17284 }
17285 /* [dcl.meaning]
17286
08cc44e7 17287 A declarator-id shall not be qualified except for the
600be0e5 17288 definition of a ... nested class outside of its class
08cc44e7 17289 ... [or] the definition or explicit instantiation of a
600be0e5 17290 class member of a namespace outside of its namespace. */
17291 if (scope == nested_name_specifier)
17292 {
ccb59bb6 17293 permerror (nested_name_specifier_token_start->location,
17294 "extra qualification not allowed");
600be0e5 17295 nested_name_specifier = NULL_TREE;
17296 num_templates = 0;
17297 }
17298 }
e16b1a13 17299 /* An explicit-specialization must be preceded by "template <>". If
17300 it is not, try to recover gracefully. */
ccb84981 17301 if (at_namespace_scope_p ()
e16b1a13 17302 && parser->num_template_parameter_lists == 0
4ccffa39 17303 && template_id_p)
e16b1a13 17304 {
ccb59bb6 17305 error_at (type_start_token->location,
17306 "an explicit specialization must be preceded by %<template <>%>");
e16b1a13 17307 invalid_explicit_specialization_p = true;
17308 /* Take the same action that would have been taken by
17309 cp_parser_explicit_specialization. */
17310 ++parser->num_template_parameter_lists;
17311 begin_specialization ();
17312 }
17313 /* There must be no "return" statements between this point and the
17314 end of this function; set "type "to the correct return value and
17315 use "goto done;" to return. */
0a3b29ad 17316 /* Make sure that the right number of template parameters were
17317 present. */
ad9ae192 17318 if (!cp_parser_check_template_parameters (parser, num_templates,
7b07a15e 17319 type_start_token->location,
17320 /*declarator=*/NULL))
e16b1a13 17321 {
17322 /* If something went wrong, there is no point in even trying to
17323 process the class-definition. */
17324 type = NULL_TREE;
17325 goto done;
17326 }
0a3b29ad 17327
0a3b29ad 17328 /* Look up the type. */
17329 if (template_id_p)
17330 {
04a3504e 17331 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17332 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17333 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17334 {
ccb59bb6 17335 error_at (type_start_token->location,
17336 "function template %qD redeclared as a class template", id);
04a3504e 17337 type = error_mark_node;
17338 }
17339 else
17340 {
17341 type = TREE_TYPE (id);
17342 type = maybe_process_partial_specialization (type);
17343 }
7f602bca 17344 if (nested_name_specifier)
17345 pushed_scope = push_scope (nested_name_specifier);
0a3b29ad 17346 }
7f602bca 17347 else if (nested_name_specifier)
0a3b29ad 17348 {
0a3b29ad 17349 tree class_type;
17350
17351 /* Given:
17352
17353 template <typename T> struct S { struct T };
5f6526e1 17354 template <typename T> struct S<T>::T { };
0a3b29ad 17355
17356 we will get a TYPENAME_TYPE when processing the definition of
17357 `S::T'. We need to resolve it to the actual type before we
17358 try to define it. */
17359 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17360 {
5f6526e1 17361 class_type = resolve_typename_type (TREE_TYPE (type),
17362 /*only_current_p=*/false);
8826a863 17363 if (TREE_CODE (class_type) != TYPENAME_TYPE)
5f6526e1 17364 type = TYPE_NAME (class_type);
17365 else
17366 {
17367 cp_parser_error (parser, "could not resolve typename type");
17368 type = error_mark_node;
17369 }
0a3b29ad 17370 }
17371
e01a7685 17372 if (maybe_process_partial_specialization (TREE_TYPE (type))
17373 == error_mark_node)
17374 {
17375 type = NULL_TREE;
17376 goto done;
17377 }
17378
8172be22 17379 class_type = current_class_type;
17380 /* Enter the scope indicated by the nested-name-specifier. */
7f602bca 17381 pushed_scope = push_scope (nested_name_specifier);
8172be22 17382 /* Get the canonical version of this type. */
17383 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17384 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17385 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
8d52e9a6 17386 {
17387 type = push_template_decl (type);
17388 if (type == error_mark_node)
17389 {
17390 type = NULL_TREE;
17391 goto done;
17392 }
17393 }
9031d10b 17394
8172be22 17395 type = TREE_TYPE (type);
7f602bca 17396 *nested_name_specifier_p = true;
0a3b29ad 17397 }
7f602bca 17398 else /* The name is not a nested name. */
17399 {
17400 /* If the class was unnamed, create a dummy name. */
17401 if (!id)
17402 id = make_anon_name ();
17403 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17404 parser->num_template_parameter_lists);
17405 }
17406
0a3b29ad 17407 /* Indicate whether this class was declared as a `class' or as a
17408 `struct'. */
17409 if (TREE_CODE (type) == RECORD_TYPE)
17410 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17411 cp_parser_check_class_key (class_key, type);
17412
4cbba981 17413 /* If this type was already complete, and we see another definition,
17414 that's an error. */
17415 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17416 {
ccb59bb6 17417 error_at (type_start_token->location, "redefinition of %q#T",
17418 type);
17419 error_at (type_start_token->location, "previous definition of %q+#T",
17420 type);
94637815 17421 type = NULL_TREE;
17422 goto done;
4cbba981 17423 }
1a9b4c25 17424 else if (type == error_mark_node)
17425 type = NULL_TREE;
4cbba981 17426
7f602bca 17427 /* We will have entered the scope containing the class; the names of
4cbba981 17428 base classes should be looked up in that context. For example:
0a3b29ad 17429
17430 struct A { struct B {}; struct C; };
17431 struct A::C : B {};
17432
17433 is valid. */
207355ad 17434
a6460bf1 17435 /* Get the list of base-classes, if there is one. */
17436 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
1a9b4c25 17437 *bases = cp_parser_base_clause (parser);
0a3b29ad 17438
7f602bca 17439 done:
0a3b29ad 17440 /* Leave the scope given by the nested-name-specifier. We will
17441 enter the class scope itself while processing the members. */
7f602bca 17442 if (pushed_scope)
17443 pop_scope (pushed_scope);
0a3b29ad 17444
e16b1a13 17445 if (invalid_explicit_specialization_p)
17446 {
17447 end_specialization ();
17448 --parser->num_template_parameter_lists;
17449 }
aad3e1b3 17450
17451 if (type)
17452 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
fb3e3237 17453 *attributes_p = attributes;
0a3b29ad 17454 return type;
17455}
17456
17457/* Parse a class-key.
17458
17459 class-key:
17460 class
17461 struct
17462 union
17463
17464 Returns the kind of class-key specified, or none_type to indicate
17465 error. */
17466
17467static enum tag_types
45baea8b 17468cp_parser_class_key (cp_parser* parser)
0a3b29ad 17469{
17470 cp_token *token;
17471 enum tag_types tag_type;
17472
17473 /* Look for the class-key. */
c247dce0 17474 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
0a3b29ad 17475 if (!token)
17476 return none_type;
17477
17478 /* Check to see if the TOKEN is a class-key. */
17479 tag_type = cp_parser_token_is_class_key (token);
17480 if (!tag_type)
17481 cp_parser_error (parser, "expected class-key");
17482 return tag_type;
17483}
17484
17485/* Parse an (optional) member-specification.
17486
17487 member-specification:
17488 member-declaration member-specification [opt]
17489 access-specifier : member-specification [opt] */
17490
17491static void
45baea8b 17492cp_parser_member_specification_opt (cp_parser* parser)
0a3b29ad 17493{
17494 while (true)
17495 {
17496 cp_token *token;
17497 enum rid keyword;
17498
17499 /* Peek at the next token. */
17500 token = cp_lexer_peek_token (parser->lexer);
17501 /* If it's a `}', or EOF then we've seen all the members. */
b75b98aa 17502 if (token->type == CPP_CLOSE_BRACE
17503 || token->type == CPP_EOF
17504 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 17505 break;
17506
17507 /* See if this token is a keyword. */
17508 keyword = token->keyword;
17509 switch (keyword)
17510 {
17511 case RID_PUBLIC:
17512 case RID_PROTECTED:
17513 case RID_PRIVATE:
17514 /* Consume the access-specifier. */
17515 cp_lexer_consume_token (parser->lexer);
17516 /* Remember which access-specifier is active. */
3369eb76 17517 current_access_specifier = token->u.value;
0a3b29ad 17518 /* Look for the `:'. */
c247dce0 17519 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 17520 break;
17521
17522 default:
882d3c4f 17523 /* Accept #pragmas at class scope. */
17524 if (token->type == CPP_PRAGMA)
17525 {
b75b98aa 17526 cp_parser_pragma (parser, pragma_external);
882d3c4f 17527 break;
17528 }
17529
0a3b29ad 17530 /* Otherwise, the next construction must be a
17531 member-declaration. */
17532 cp_parser_member_declaration (parser);
0a3b29ad 17533 }
17534 }
17535}
17536
ccb84981 17537/* Parse a member-declaration.
0a3b29ad 17538
17539 member-declaration:
17540 decl-specifier-seq [opt] member-declarator-list [opt] ;
17541 function-definition ; [opt]
17542 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17543 using-declaration
ccb84981 17544 template-declaration
0a3b29ad 17545
17546 member-declarator-list:
17547 member-declarator
17548 member-declarator-list , member-declarator
17549
17550 member-declarator:
ccb84981 17551 declarator pure-specifier [opt]
0a3b29ad 17552 declarator constant-initializer [opt]
ccb84981 17553 identifier [opt] : constant-expression
0a3b29ad 17554
17555 GNU Extensions:
17556
17557 member-declaration:
17558 __extension__ member-declaration
17559
17560 member-declarator:
17561 declarator attributes [opt] pure-specifier [opt]
17562 declarator attributes [opt] constant-initializer [opt]
7a05c4b1 17563 identifier [opt] attributes [opt] : constant-expression
17564
17565 C++0x Extensions:
17566
17567 member-declaration:
17568 static_assert-declaration */
0a3b29ad 17569
17570static void
45baea8b 17571cp_parser_member_declaration (cp_parser* parser)
0a3b29ad 17572{
4b9b2871 17573 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 17574 tree prefix_attributes;
17575 tree decl;
8172be22 17576 int declares_class_or_enum;
0a3b29ad 17577 bool friend_p;
ad9ae192 17578 cp_token *token = NULL;
17579 cp_token *decl_spec_token_start = NULL;
17580 cp_token *initializer_token_start = NULL;
0a3b29ad 17581 int saved_pedantic;
17582
17583 /* Check for the `__extension__' keyword. */
17584 if (cp_parser_extension_opt (parser, &saved_pedantic))
17585 {
17586 /* Recurse. */
17587 cp_parser_member_declaration (parser);
17588 /* Restore the old value of the PEDANTIC flag. */
17589 pedantic = saved_pedantic;
17590
17591 return;
17592 }
17593
17594 /* Check for a template-declaration. */
17595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17596 {
81ef2172 17597 /* An explicit specialization here is an error condition, and we
17598 expect the specialization handler to detect and report this. */
17599 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17600 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17601 cp_parser_explicit_specialization (parser);
17602 else
17603 cp_parser_template_declaration (parser, /*member_p=*/true);
0a3b29ad 17604
17605 return;
17606 }
17607
17608 /* Check for a using-declaration. */
17609 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17610 {
17611 /* Parse the using-declaration. */
da2a3271 17612 cp_parser_using_declaration (parser,
17613 /*access_declaration_p=*/false);
0a3b29ad 17614 return;
17615 }
ccb84981 17616
7a4e126b 17617 /* Check for @defs. */
17618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17619 {
17620 tree ivar, member;
17621 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17622 ivar = ivar_chains;
17623 while (ivar)
17624 {
17625 member = ivar;
17626 ivar = TREE_CHAIN (member);
17627 TREE_CHAIN (member) = NULL_TREE;
17628 finish_member_declaration (member);
17629 }
17630 return;
17631 }
17632
7a05c4b1 17633 /* If the next token is `static_assert' we have a static assertion. */
17634 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17635 {
17636 cp_parser_static_assert (parser, /*member_p=*/true);
17637 return;
17638 }
17639
da2a3271 17640 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17641 return;
17642
0a3b29ad 17643 /* Parse the decl-specifier-seq. */
ad9ae192 17644 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 17645 cp_parser_decl_specifier_seq (parser,
17646 CP_PARSER_FLAGS_OPTIONAL,
17647 &decl_specifiers,
17648 &declares_class_or_enum);
17649 prefix_attributes = decl_specifiers.attributes;
17650 decl_specifiers.attributes = NULL_TREE;
954ad420 17651 /* Check for an invalid type-name. */
67484828 17652 if (!decl_specifiers.any_type_specifiers_p
882d3c4f 17653 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
954ad420 17654 return;
0a3b29ad 17655 /* If there is no declarator, then the decl-specifier-seq should
17656 specify a type. */
17657 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17658 {
17659 /* If there was no decl-specifier-seq, and the next token is a
17660 `;', then we have something like:
17661
17662 struct S { ; };
17663
17664 [class.mem]
17665
17666 Each member-declaration shall declare at least one member
17667 name of the class. */
4b9b2871 17668 if (!decl_specifiers.any_specifiers_p)
0a3b29ad 17669 {
b9dd3954 17670 cp_token *token = cp_lexer_peek_token (parser->lexer);
8864917d 17671 if (!in_system_header_at (token->location))
21ca8540 17672 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
0a3b29ad 17673 }
ccb84981 17674 else
0a3b29ad 17675 {
17676 tree type;
ccb84981 17677
0a3b29ad 17678 /* See if this declaration is a friend. */
4b9b2871 17679 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 17680 /* If there were decl-specifiers, check to see if there was
17681 a class-declaration. */
4b9b2871 17682 type = check_tag_decl (&decl_specifiers);
0a3b29ad 17683 /* Nested classes have already been added to the class, but
17684 a `friend' needs to be explicitly registered. */
17685 if (friend_p)
17686 {
17687 /* If the `friend' keyword was present, the friend must
17688 be introduced with a class-key. */
17689 if (!declares_class_or_enum)
ccb59bb6 17690 error_at (decl_spec_token_start->location,
17691 "a class-key must be used when declaring a friend");
0a3b29ad 17692 /* In this case:
17693
ccb84981 17694 template <typename T> struct A {
653e5405 17695 friend struct A<T>::B;
17696 };
ccb84981 17697
0a3b29ad 17698 A<T>::B will be represented by a TYPENAME_TYPE, and
17699 therefore not recognized by check_tag_decl. */
207355ad 17700 if (!type
4b9b2871 17701 && decl_specifiers.type
17702 && TYPE_P (decl_specifiers.type))
17703 type = decl_specifiers.type;
069754af 17704 if (!type || !TYPE_P (type))
ccb59bb6 17705 error_at (decl_spec_token_start->location,
17706 "friend declaration does not name a class or "
17707 "function");
0a3b29ad 17708 else
b123b79d 17709 make_friend_class (current_class_type, type,
17710 /*complain=*/true);
0a3b29ad 17711 }
17712 /* If there is no TYPE, an error message will already have
17713 been issued. */
4b9b2871 17714 else if (!type || type == error_mark_node)
0a3b29ad 17715 ;
17716 /* An anonymous aggregate has to be handled specially; such
17717 a declaration really declares a data member (with a
17718 particular type), as opposed to a nested class. */
17719 else if (ANON_AGGR_TYPE_P (type))
17720 {
17721 /* Remove constructors and such from TYPE, now that we
755edffd 17722 know it is an anonymous aggregate. */
0a3b29ad 17723 fixup_anonymous_aggr (type);
17724 /* And make the corresponding data member. */
e60a6f7b 17725 decl = build_decl (decl_spec_token_start->location,
17726 FIELD_DECL, NULL_TREE, type);
0a3b29ad 17727 /* Add it to the class. */
17728 finish_member_declaration (decl);
17729 }
7e35473e 17730 else
ad9ae192 17731 cp_parser_check_access_in_redeclaration
17732 (TYPE_NAME (type),
17733 decl_spec_token_start->location);
0a3b29ad 17734 }
17735 }
17736 else
17737 {
864468c7 17738 bool assume_semicolon = false;
17739
0a3b29ad 17740 /* See if these declarations will be friends. */
4b9b2871 17741 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 17742
ccb84981 17743 /* Keep going until we hit the `;' at the end of the
0a3b29ad 17744 declaration. */
17745 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17746 {
17747 tree attributes = NULL_TREE;
17748 tree first_attribute;
17749
17750 /* Peek at the next token. */
17751 token = cp_lexer_peek_token (parser->lexer);
17752
17753 /* Check for a bitfield declaration. */
17754 if (token->type == CPP_COLON
17755 || (token->type == CPP_NAME
ccb84981 17756 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 17757 == CPP_COLON))
17758 {
17759 tree identifier;
17760 tree width;
17761
17762 /* Get the name of the bitfield. Note that we cannot just
17763 check TOKEN here because it may have been invalidated by
17764 the call to cp_lexer_peek_nth_token above. */
17765 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17766 identifier = cp_parser_identifier (parser);
17767 else
17768 identifier = NULL_TREE;
17769
17770 /* Consume the `:' token. */
17771 cp_lexer_consume_token (parser->lexer);
17772 /* Get the width of the bitfield. */
ccb84981 17773 width
5f6526e1 17774 = cp_parser_constant_expression (parser,
17775 /*allow_non_constant=*/false,
17776 NULL);
0a3b29ad 17777
17778 /* Look for attributes that apply to the bitfield. */
17779 attributes = cp_parser_attributes_opt (parser);
17780 /* Remember which attributes are prefix attributes and
17781 which are not. */
17782 first_attribute = attributes;
17783 /* Combine the attributes. */
17784 attributes = chainon (prefix_attributes, attributes);
17785
17786 /* Create the bitfield declaration. */
207355ad 17787 decl = grokbitfield (identifier
2ded3667 17788 ? make_id_declarator (NULL_TREE,
2366ed31 17789 identifier,
17790 sfk_none)
3046c0a3 17791 : NULL,
4b9b2871 17792 &decl_specifiers,
f922e029 17793 width,
17794 attributes);
0a3b29ad 17795 }
17796 else
17797 {
3046c0a3 17798 cp_declarator *declarator;
0a3b29ad 17799 tree initializer;
17800 tree asm_specification;
0986fa22 17801 int ctor_dtor_or_conv_p;
0a3b29ad 17802
17803 /* Parse the declarator. */
ccb84981 17804 declarator
42bbd0ec 17805 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 17806 &ctor_dtor_or_conv_p,
08ea345c 17807 /*parenthesized_p=*/NULL,
17808 /*member_p=*/true);
0a3b29ad 17809
17810 /* If something went wrong parsing the declarator, make sure
17811 that we at least consume some tokens. */
3046c0a3 17812 if (declarator == cp_error_declarator)
0a3b29ad 17813 {
17814 /* Skip to the end of the statement. */
17815 cp_parser_skip_to_end_of_statement (parser);
92b128ed 17816 /* If the next token is not a semicolon, that is
17817 probably because we just skipped over the body of
17818 a function. So, we consume a semicolon if
17819 present, but do not issue an error message if it
17820 is not present. */
17821 if (cp_lexer_next_token_is (parser->lexer,
17822 CPP_SEMICOLON))
17823 cp_lexer_consume_token (parser->lexer);
17824 return;
0a3b29ad 17825 }
17826
e2ae55f2 17827 if (declares_class_or_enum & 2)
17828 cp_parser_check_for_definition_in_return_type
eef0ab03 17829 (declarator, decl_specifiers.type,
17830 decl_specifiers.type_location);
8172be22 17831
0a3b29ad 17832 /* Look for an asm-specification. */
17833 asm_specification = cp_parser_asm_specification_opt (parser);
17834 /* Look for attributes that apply to the declaration. */
17835 attributes = cp_parser_attributes_opt (parser);
17836 /* Remember which attributes are prefix attributes and
17837 which are not. */
17838 first_attribute = attributes;
17839 /* Combine the attributes. */
17840 attributes = chainon (prefix_attributes, attributes);
17841
17842 /* If it's an `=', then we have a constant-initializer or a
17843 pure-specifier. It is not correct to parse the
17844 initializer before registering the member declaration
17845 since the member declaration should be in scope while
17846 its initializer is processed. However, the rest of the
17847 front end does not yet provide an interface that allows
17848 us to handle this correctly. */
17849 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17850 {
17851 /* In [class.mem]:
17852
17853 A pure-specifier shall be used only in the declaration of
ccb84981 17854 a virtual function.
0a3b29ad 17855
17856 A member-declarator can contain a constant-initializer
17857 only if it declares a static member of integral or
ccb84981 17858 enumeration type.
0a3b29ad 17859
17860 Therefore, if the DECLARATOR is for a function, we look
17861 for a pure-specifier; otherwise, we look for a
17862 constant-initializer. When we call `grokfield', it will
17863 perform more stringent semantics checks. */
ad9ae192 17864 initializer_token_start = cp_lexer_peek_token (parser->lexer);
3a30cb7f 17865 if (function_declarator_p (declarator))
0a3b29ad 17866 initializer = cp_parser_pure_specifier (parser);
17867 else
92b128ed 17868 /* Parse the initializer. */
17869 initializer = cp_parser_constant_initializer (parser);
0a3b29ad 17870 }
17871 /* Otherwise, there is no initializer. */
17872 else
17873 initializer = NULL_TREE;
17874
17875 /* See if we are probably looking at a function
f29f4570 17876 definition. We are certainly not looking at a
0a3b29ad 17877 member-declarator. Calling `grokfield' has
17878 side-effects, so we must not do it unless we are sure
17879 that we are looking at a member-declarator. */
ccb84981 17880 if (cp_parser_token_starts_function_definition_p
0a3b29ad 17881 (cp_lexer_peek_token (parser->lexer)))
92b128ed 17882 {
17883 /* The grammar does not allow a pure-specifier to be
17884 used when a member function is defined. (It is
17885 possible that this fact is an oversight in the
17886 standard, since a pure function may be defined
17887 outside of the class-specifier. */
17888 if (initializer)
ccb59bb6 17889 error_at (initializer_token_start->location,
17890 "pure-specifier on function-definition");
92b128ed 17891 decl = cp_parser_save_member_function_body (parser,
4b9b2871 17892 &decl_specifiers,
92b128ed 17893 declarator,
17894 attributes);
17895 /* If the member was not a friend, declare it here. */
17896 if (!friend_p)
17897 finish_member_declaration (decl);
17898 /* Peek at the next token. */
17899 token = cp_lexer_peek_token (parser->lexer);
17900 /* If the next token is a semicolon, consume it. */
17901 if (token->type == CPP_SEMICOLON)
e6ae3136 17902 cp_lexer_consume_token (parser->lexer);
92b128ed 17903 return;
17904 }
0a3b29ad 17905 else
d19f0a18 17906 if (declarator->kind == cdk_function)
17907 declarator->id_loc = token->location;
d91303a6 17908 /* Create the declaration. */
17909 decl = grokfield (declarator, &decl_specifiers,
17910 initializer, /*init_const_expr_p=*/true,
17911 asm_specification,
17912 attributes);
0a3b29ad 17913 }
17914
17915 /* Reset PREFIX_ATTRIBUTES. */
17916 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17917 attributes = TREE_CHAIN (attributes);
17918 if (attributes)
17919 TREE_CHAIN (attributes) = NULL_TREE;
17920
17921 /* If there is any qualification still in effect, clear it
17922 now; we will be starting fresh with the next declarator. */
17923 parser->scope = NULL_TREE;
17924 parser->qualifying_scope = NULL_TREE;
17925 parser->object_scope = NULL_TREE;
17926 /* If it's a `,', then there are more declarators. */
17927 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17928 cp_lexer_consume_token (parser->lexer);
17929 /* If the next token isn't a `;', then we have a parse error. */
17930 else if (cp_lexer_next_token_is_not (parser->lexer,
17931 CPP_SEMICOLON))
17932 {
864468c7 17933 /* The next token might be a ways away from where the
17934 actual semicolon is missing. Find the previous token
17935 and use that for our error position. */
17936 cp_token *token = cp_lexer_previous_token (parser->lexer);
17937 error_at (token->location,
17938 "expected %<;%> at end of member declaration");
0a3b29ad 17939
864468c7 17940 /* Assume that the user meant to provide a semicolon. If
17941 we were to cp_parser_skip_to_end_of_statement, we might
17942 skip to a semicolon inside a member function definition
17943 and issue nonsensical error messages. */
17944 assume_semicolon = true;
0a3b29ad 17945 }
17946
17947 if (decl)
17948 {
17949 /* Add DECL to the list of members. */
17950 if (!friend_p)
17951 finish_member_declaration (decl);
17952
0a3b29ad 17953 if (TREE_CODE (decl) == FUNCTION_DECL)
69b6679c 17954 cp_parser_save_default_args (parser, decl);
0a3b29ad 17955 }
864468c7 17956
17957 if (assume_semicolon)
17958 return;
0a3b29ad 17959 }
17960 }
17961
c247dce0 17962 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 17963}
17964
17965/* Parse a pure-specifier.
17966
17967 pure-specifier:
17968 = 0
17969
17970 Returns INTEGER_ZERO_NODE if a pure specifier is found.
63eff20d 17971 Otherwise, ERROR_MARK_NODE is returned. */
0a3b29ad 17972
17973static tree
45baea8b 17974cp_parser_pure_specifier (cp_parser* parser)
0a3b29ad 17975{
17976 cp_token *token;
17977
17978 /* Look for the `=' token. */
c247dce0 17979 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
0a3b29ad 17980 return error_mark_node;
17981 /* Look for the `0' token. */
d8acda95 17982 token = cp_lexer_peek_token (parser->lexer);
17983
17984 if (token->type == CPP_EOF
17985 || token->type == CPP_PRAGMA_EOL)
17986 return error_mark_node;
17987
17988 cp_lexer_consume_token (parser->lexer);
2336da2a 17989
17990 /* Accept = default or = delete in c++0x mode. */
17991 if (token->keyword == RID_DEFAULT
17992 || token->keyword == RID_DELETE)
17993 {
bf8d19fe 17994 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
2336da2a 17995 return token->u.value;
17996 }
17997
8dba02f7 17998 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
5f69415d 17999 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18000 {
074ab442 18001 cp_parser_error (parser,
640710cf 18002 "invalid pure specifier (only %<= 0%> is allowed)");
5f69415d 18003 cp_parser_skip_to_end_of_statement (parser);
18004 return error_mark_node;
18005 }
18006 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18007 {
ccb59bb6 18008 error_at (token->location, "templates may not be %<virtual%>");
5f69415d 18009 return error_mark_node;
18010 }
0a3b29ad 18011
5f69415d 18012 return integer_zero_node;
0a3b29ad 18013}
18014
18015/* Parse a constant-initializer.
18016
18017 constant-initializer:
18018 = constant-expression
18019
18020 Returns a representation of the constant-expression. */
18021
18022static tree
45baea8b 18023cp_parser_constant_initializer (cp_parser* parser)
0a3b29ad 18024{
18025 /* Look for the `=' token. */
c247dce0 18026 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
0a3b29ad 18027 return error_mark_node;
18028
18029 /* It is invalid to write:
18030
18031 struct S { static const int i = { 7 }; };
18032
18033 */
18034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18035 {
18036 cp_parser_error (parser,
18037 "a brace-enclosed initializer is not allowed here");
18038 /* Consume the opening brace. */
18039 cp_lexer_consume_token (parser->lexer);
18040 /* Skip the initializer. */
18041 cp_parser_skip_to_closing_brace (parser);
18042 /* Look for the trailing `}'. */
c247dce0 18043 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
ccb84981 18044
0a3b29ad 18045 return error_mark_node;
18046 }
18047
ccb84981 18048 return cp_parser_constant_expression (parser,
5f6526e1 18049 /*allow_non_constant=*/false,
18050 NULL);
0a3b29ad 18051}
18052
18053/* Derived classes [gram.class.derived] */
18054
18055/* Parse a base-clause.
18056
18057 base-clause:
ccb84981 18058 : base-specifier-list
0a3b29ad 18059
18060 base-specifier-list:
d95d815d 18061 base-specifier ... [opt]
18062 base-specifier-list , base-specifier ... [opt]
0a3b29ad 18063
18064 Returns a TREE_LIST representing the base-classes, in the order in
18065 which they were declared. The representation of each node is as
ccb84981 18066 described by cp_parser_base_specifier.
0a3b29ad 18067
18068 In the case that no bases are specified, this function will return
18069 NULL_TREE, not ERROR_MARK_NODE. */
18070
18071static tree
45baea8b 18072cp_parser_base_clause (cp_parser* parser)
0a3b29ad 18073{
18074 tree bases = NULL_TREE;
18075
18076 /* Look for the `:' that begins the list. */
c247dce0 18077 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 18078
18079 /* Scan the base-specifier-list. */
18080 while (true)
18081 {
18082 cp_token *token;
18083 tree base;
d95d815d 18084 bool pack_expansion_p = false;
0a3b29ad 18085
18086 /* Look for the base-specifier. */
18087 base = cp_parser_base_specifier (parser);
d95d815d 18088 /* Look for the (optional) ellipsis. */
18089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18090 {
18091 /* Consume the `...'. */
18092 cp_lexer_consume_token (parser->lexer);
18093
18094 pack_expansion_p = true;
18095 }
18096
0a3b29ad 18097 /* Add BASE to the front of the list. */
18098 if (base != error_mark_node)
18099 {
d95d815d 18100 if (pack_expansion_p)
18101 /* Make this a pack expansion type. */
18102 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
613687b1 18103
d95d815d 18104
830a6615 18105 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
613687b1 18106 {
18107 TREE_CHAIN (base) = bases;
18108 bases = base;
18109 }
0a3b29ad 18110 }
18111 /* Peek at the next token. */
18112 token = cp_lexer_peek_token (parser->lexer);
18113 /* If it's not a comma, then the list is complete. */
18114 if (token->type != CPP_COMMA)
18115 break;
18116 /* Consume the `,'. */
18117 cp_lexer_consume_token (parser->lexer);
18118 }
18119
18120 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18121 base class had a qualified name. However, the next name that
18122 appears is certainly not qualified. */
18123 parser->scope = NULL_TREE;
18124 parser->qualifying_scope = NULL_TREE;
18125 parser->object_scope = NULL_TREE;
18126
18127 return nreverse (bases);
18128}
18129
18130/* Parse a base-specifier.
18131
18132 base-specifier:
18133 :: [opt] nested-name-specifier [opt] class-name
18134 virtual access-specifier [opt] :: [opt] nested-name-specifier
18135 [opt] class-name
18136 access-specifier virtual [opt] :: [opt] nested-name-specifier
18137 [opt] class-name
18138
18139 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18140 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18141 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18142 (or the ERROR_MARK_NODE) indicating the type that was specified. */
ccb84981 18143
0a3b29ad 18144static tree
45baea8b 18145cp_parser_base_specifier (cp_parser* parser)
0a3b29ad 18146{
18147 cp_token *token;
18148 bool done = false;
18149 bool virtual_p = false;
18150 bool duplicate_virtual_error_issued_p = false;
18151 bool duplicate_access_error_issued_p = false;
d622b3bd 18152 bool class_scope_p, template_p;
95f3173a 18153 tree access = access_default_node;
0a3b29ad 18154 tree type;
18155
18156 /* Process the optional `virtual' and `access-specifier'. */
18157 while (!done)
18158 {
18159 /* Peek at the next token. */
18160 token = cp_lexer_peek_token (parser->lexer);
18161 /* Process `virtual'. */
18162 switch (token->keyword)
18163 {
18164 case RID_VIRTUAL:
18165 /* If `virtual' appears more than once, issue an error. */
18166 if (virtual_p && !duplicate_virtual_error_issued_p)
18167 {
18168 cp_parser_error (parser,
a2c5b975 18169 "%<virtual%> specified more than once in base-specified");
0a3b29ad 18170 duplicate_virtual_error_issued_p = true;
18171 }
18172
18173 virtual_p = true;
18174
18175 /* Consume the `virtual' token. */
18176 cp_lexer_consume_token (parser->lexer);
18177
18178 break;
18179
18180 case RID_PUBLIC:
18181 case RID_PROTECTED:
18182 case RID_PRIVATE:
18183 /* If more than one access specifier appears, issue an
18184 error. */
95f3173a 18185 if (access != access_default_node
18186 && !duplicate_access_error_issued_p)
0a3b29ad 18187 {
18188 cp_parser_error (parser,
18189 "more than one access specifier in base-specified");
18190 duplicate_access_error_issued_p = true;
18191 }
18192
95f3173a 18193 access = ridpointers[(int) token->keyword];
0a3b29ad 18194
18195 /* Consume the access-specifier. */
18196 cp_lexer_consume_token (parser->lexer);
18197
18198 break;
18199
18200 default:
18201 done = true;
18202 break;
18203 }
18204 }
0f3ccaa3 18205 /* It is not uncommon to see programs mechanically, erroneously, use
eae805b4 18206 the 'typename' keyword to denote (dependent) qualified types
9591b260 18207 as base classes. */
18208 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18209 {
ad9ae192 18210 token = cp_lexer_peek_token (parser->lexer);
9591b260 18211 if (!processing_template_decl)
ccb59bb6 18212 error_at (token->location,
18213 "keyword %<typename%> not allowed outside of templates");
9591b260 18214 else
ccb59bb6 18215 error_at (token->location,
18216 "keyword %<typename%> not allowed in this context "
18217 "(the base class is implicitly a type)");
9591b260 18218 cp_lexer_consume_token (parser->lexer);
18219 }
0a3b29ad 18220
0a3b29ad 18221 /* Look for the optional `::' operator. */
130bb1d4 18222 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
0a3b29ad 18223 /* Look for the nested-name-specifier. The simplest way to
18224 implement:
18225
18226 [temp.res]
18227
18228 The keyword `typename' is not permitted in a base-specifier or
18229 mem-initializer; in these contexts a qualified name that
18230 depends on a template-parameter is implicitly assumed to be a
18231 type name.
18232
18233 is to pretend that we have seen the `typename' keyword at this
ccb84981 18234 point. */
0a3b29ad 18235 cp_parser_nested_name_specifier_opt (parser,
18236 /*typename_keyword_p=*/true,
18237 /*check_dependency_p=*/true,
e2ae55f2 18238 typename_type,
3d0f901b 18239 /*is_declaration=*/true);
0a3b29ad 18240 /* If the base class is given by a qualified name, assume that names
18241 we see are type names or templates, as appropriate. */
18242 class_scope_p = (parser->scope && TYPE_P (parser->scope));
d622b3bd 18243 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
ccb84981 18244
0a3b29ad 18245 /* Finally, look for the class-name. */
ccb84981 18246 type = cp_parser_class_name (parser,
0a3b29ad 18247 class_scope_p,
d622b3bd 18248 template_p,
e2ae55f2 18249 typename_type,
0a3b29ad 18250 /*check_dependency_p=*/true,
3d0f901b 18251 /*class_head_p=*/false,
18252 /*is_declaration=*/true);
0a3b29ad 18253
18254 if (type == error_mark_node)
18255 return error_mark_node;
18256
95f3173a 18257 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
0a3b29ad 18258}
18259
18260/* Exception handling [gram.exception] */
18261
18262/* Parse an (optional) exception-specification.
18263
18264 exception-specification:
18265 throw ( type-id-list [opt] )
18266
18267 Returns a TREE_LIST representing the exception-specification. The
18268 TREE_VALUE of each node is a type. */
18269
18270static tree
45baea8b 18271cp_parser_exception_specification_opt (cp_parser* parser)
0a3b29ad 18272{
18273 cp_token *token;
18274 tree type_id_list;
3644efa5 18275 const char *saved_message;
0a3b29ad 18276
18277 /* Peek at the next token. */
18278 token = cp_lexer_peek_token (parser->lexer);
3644efa5 18279
18280 /* Is it a noexcept-specification? */
18281 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18282 {
18283 tree expr;
18284 cp_lexer_consume_token (parser->lexer);
18285
18286 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18287 {
18288 cp_lexer_consume_token (parser->lexer);
18289
18290 /* Types may not be defined in an exception-specification. */
18291 saved_message = parser->type_definition_forbidden_message;
18292 parser->type_definition_forbidden_message
18293 = G_("types may not be defined in an exception-specification");
18294
18295 expr = cp_parser_constant_expression (parser, false, NULL);
18296
18297 /* Restore the saved message. */
18298 parser->type_definition_forbidden_message = saved_message;
18299
18300 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18301 }
18302 else
18303 expr = boolean_true_node;
18304
18305 return build_noexcept_spec (expr, tf_warning_or_error);
18306 }
18307
0a3b29ad 18308 /* If it's not `throw', then there's no exception-specification. */
18309 if (!cp_parser_is_keyword (token, RID_THROW))
18310 return NULL_TREE;
18311
3644efa5 18312#if 0
18313 /* Enable this once a lot of code has transitioned to noexcept? */
18314 if (cxx_dialect == cxx0x && !in_system_header)
18315 warning (OPT_Wdeprecated, "dynamic exception specifications are "
bf776685 18316 "deprecated in C++0x; use %<noexcept%> instead");
3644efa5 18317#endif
18318
0a3b29ad 18319 /* Consume the `throw'. */
18320 cp_lexer_consume_token (parser->lexer);
18321
18322 /* Look for the `('. */
c247dce0 18323 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 18324
18325 /* Peek at the next token. */
18326 token = cp_lexer_peek_token (parser->lexer);
18327 /* If it's not a `)', then there is a type-id-list. */
18328 if (token->type != CPP_CLOSE_PAREN)
18329 {
0a3b29ad 18330 /* Types may not be defined in an exception-specification. */
18331 saved_message = parser->type_definition_forbidden_message;
18332 parser->type_definition_forbidden_message
ca82e026 18333 = G_("types may not be defined in an exception-specification");
0a3b29ad 18334 /* Parse the type-id-list. */
18335 type_id_list = cp_parser_type_id_list (parser);
18336 /* Restore the saved message. */
18337 parser->type_definition_forbidden_message = saved_message;
18338 }
18339 else
18340 type_id_list = empty_except_spec;
18341
18342 /* Look for the `)'. */
c247dce0 18343 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 18344
18345 return type_id_list;
18346}
18347
18348/* Parse an (optional) type-id-list.
18349
18350 type-id-list:
d95d815d 18351 type-id ... [opt]
18352 type-id-list , type-id ... [opt]
0a3b29ad 18353
18354 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18355 in the order that the types were presented. */
18356
18357static tree
45baea8b 18358cp_parser_type_id_list (cp_parser* parser)
0a3b29ad 18359{
18360 tree types = NULL_TREE;
18361
18362 while (true)
18363 {
18364 cp_token *token;
18365 tree type;
18366
18367 /* Get the next type-id. */
18368 type = cp_parser_type_id (parser);
d95d815d 18369 /* Parse the optional ellipsis. */
18370 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18371 {
18372 /* Consume the `...'. */
18373 cp_lexer_consume_token (parser->lexer);
18374
18375 /* Turn the type into a pack expansion expression. */
18376 type = make_pack_expansion (type);
18377 }
0a3b29ad 18378 /* Add it to the list. */
18379 types = add_exception_specifier (types, type, /*complain=*/1);
18380 /* Peek at the next token. */
18381 token = cp_lexer_peek_token (parser->lexer);
18382 /* If it is not a `,', we are done. */
18383 if (token->type != CPP_COMMA)
18384 break;
18385 /* Consume the `,'. */
18386 cp_lexer_consume_token (parser->lexer);
18387 }
18388
18389 return nreverse (types);
18390}
18391
18392/* Parse a try-block.
18393
18394 try-block:
18395 try compound-statement handler-seq */
18396
18397static tree
45baea8b 18398cp_parser_try_block (cp_parser* parser)
0a3b29ad 18399{
18400 tree try_block;
18401
c247dce0 18402 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
0a3b29ad 18403 try_block = begin_try_block ();
2363ef00 18404 cp_parser_compound_statement (parser, NULL, true);
0a3b29ad 18405 finish_try_block (try_block);
18406 cp_parser_handler_seq (parser);
18407 finish_handler_sequence (try_block);
18408
18409 return try_block;
18410}
18411
18412/* Parse a function-try-block.
18413
18414 function-try-block:
18415 try ctor-initializer [opt] function-body handler-seq */
18416
18417static bool
45baea8b 18418cp_parser_function_try_block (cp_parser* parser)
0a3b29ad 18419{
78f7169a 18420 tree compound_stmt;
0a3b29ad 18421 tree try_block;
18422 bool ctor_initializer_p;
18423
18424 /* Look for the `try' keyword. */
c247dce0 18425 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
0a3b29ad 18426 return false;
a17c2a3a 18427 /* Let the rest of the front end know where we are. */
78f7169a 18428 try_block = begin_function_try_block (&compound_stmt);
0a3b29ad 18429 /* Parse the function-body. */
ccb84981 18430 ctor_initializer_p
0a3b29ad 18431 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18432 /* We're done with the `try' part. */
18433 finish_function_try_block (try_block);
18434 /* Parse the handlers. */
18435 cp_parser_handler_seq (parser);
18436 /* We're done with the handlers. */
78f7169a 18437 finish_function_handler_sequence (try_block, compound_stmt);
0a3b29ad 18438
18439 return ctor_initializer_p;
18440}
18441
18442/* Parse a handler-seq.
18443
18444 handler-seq:
18445 handler handler-seq [opt] */
18446
18447static void
45baea8b 18448cp_parser_handler_seq (cp_parser* parser)
0a3b29ad 18449{
18450 while (true)
18451 {
18452 cp_token *token;
18453
18454 /* Parse the handler. */
18455 cp_parser_handler (parser);
18456 /* Peek at the next token. */
18457 token = cp_lexer_peek_token (parser->lexer);
18458 /* If it's not `catch' then there are no more handlers. */
18459 if (!cp_parser_is_keyword (token, RID_CATCH))
18460 break;
18461 }
18462}
18463
18464/* Parse a handler.
18465
18466 handler:
18467 catch ( exception-declaration ) compound-statement */
18468
18469static void
45baea8b 18470cp_parser_handler (cp_parser* parser)
0a3b29ad 18471{
18472 tree handler;
18473 tree declaration;
18474
c247dce0 18475 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
0a3b29ad 18476 handler = begin_handler ();
c247dce0 18477 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 18478 declaration = cp_parser_exception_declaration (parser);
18479 finish_handler_parms (declaration, handler);
c247dce0 18480 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
2363ef00 18481 cp_parser_compound_statement (parser, NULL, false);
0a3b29ad 18482 finish_handler (handler);
18483}
18484
18485/* Parse an exception-declaration.
18486
18487 exception-declaration:
18488 type-specifier-seq declarator
18489 type-specifier-seq abstract-declarator
18490 type-specifier-seq
ccb84981 18491 ...
0a3b29ad 18492
18493 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18494 ellipsis variant is used. */
18495
18496static tree
45baea8b 18497cp_parser_exception_declaration (cp_parser* parser)
0a3b29ad 18498{
4b9b2871 18499 cp_decl_specifier_seq type_specifiers;
3046c0a3 18500 cp_declarator *declarator;
0a3b29ad 18501 const char *saved_message;
18502
18503 /* If it's an ellipsis, it's easy to handle. */
18504 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18505 {
18506 /* Consume the `...' token. */
18507 cp_lexer_consume_token (parser->lexer);
18508 return NULL_TREE;
18509 }
18510
18511 /* Types may not be defined in exception-declarations. */
18512 saved_message = parser->type_definition_forbidden_message;
18513 parser->type_definition_forbidden_message
ca82e026 18514 = G_("types may not be defined in exception-declarations");
0a3b29ad 18515
18516 /* Parse the type-specifier-seq. */
c44f7faf 18517 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
638569c5 18518 /*is_trailing_return=*/false,
6f74fe3c 18519 &type_specifiers);
0a3b29ad 18520 /* If it's a `)', then there is no declarator. */
18521 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
3046c0a3 18522 declarator = NULL;
0a3b29ad 18523 else
42bbd0ec 18524 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
92b128ed 18525 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 18526 /*parenthesized_p=*/NULL,
18527 /*member_p=*/false);
0a3b29ad 18528
18529 /* Restore the saved message. */
18530 parser->type_definition_forbidden_message = saved_message;
18531
68023786 18532 if (!type_specifiers.any_specifiers_p)
18533 return error_mark_node;
3046c0a3 18534
68023786 18535 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
0a3b29ad 18536}
18537
ccb84981 18538/* Parse a throw-expression.
0a3b29ad 18539
18540 throw-expression:
755edffd 18541 throw assignment-expression [opt]
0a3b29ad 18542
18543 Returns a THROW_EXPR representing the throw-expression. */
18544
18545static tree
45baea8b 18546cp_parser_throw_expression (cp_parser* parser)
0a3b29ad 18547{
18548 tree expression;
fe338aca 18549 cp_token* token;
0a3b29ad 18550
c247dce0 18551 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
fe338aca 18552 token = cp_lexer_peek_token (parser->lexer);
18553 /* Figure out whether or not there is an assignment-expression
18554 following the "throw" keyword. */
18555 if (token->type == CPP_COMMA
18556 || token->type == CPP_SEMICOLON
18557 || token->type == CPP_CLOSE_PAREN
18558 || token->type == CPP_CLOSE_SQUARE
18559 || token->type == CPP_CLOSE_BRACE
18560 || token->type == CPP_COLON)
0a3b29ad 18561 expression = NULL_TREE;
fe338aca 18562 else
640aa28c 18563 expression = cp_parser_assignment_expression (parser,
98b326fd 18564 /*cast_p=*/false, NULL);
0a3b29ad 18565
18566 return build_throw (expression);
18567}
18568
18569/* GNU Extensions */
18570
18571/* Parse an (optional) asm-specification.
18572
18573 asm-specification:
18574 asm ( string-literal )
18575
18576 If the asm-specification is present, returns a STRING_CST
18577 corresponding to the string-literal. Otherwise, returns
18578 NULL_TREE. */
18579
18580static tree
45baea8b 18581cp_parser_asm_specification_opt (cp_parser* parser)
0a3b29ad 18582{
18583 cp_token *token;
18584 tree asm_specification;
18585
18586 /* Peek at the next token. */
18587 token = cp_lexer_peek_token (parser->lexer);
ccb84981 18588 /* If the next token isn't the `asm' keyword, then there's no
0a3b29ad 18589 asm-specification. */
18590 if (!cp_parser_is_keyword (token, RID_ASM))
18591 return NULL_TREE;
18592
18593 /* Consume the `asm' token. */
18594 cp_lexer_consume_token (parser->lexer);
18595 /* Look for the `('. */
c247dce0 18596 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 18597
18598 /* Look for the string-literal. */
00d26680 18599 asm_specification = cp_parser_string_literal (parser, false, false);
0a3b29ad 18600
18601 /* Look for the `)'. */
c247dce0 18602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 18603
18604 return asm_specification;
18605}
18606
ccb84981 18607/* Parse an asm-operand-list.
0a3b29ad 18608
18609 asm-operand-list:
18610 asm-operand
18611 asm-operand-list , asm-operand
ccb84981 18612
0a3b29ad 18613 asm-operand:
ccb84981 18614 string-literal ( expression )
0a3b29ad 18615 [ string-literal ] string-literal ( expression )
18616
18617 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18618 each node is the expression. The TREE_PURPOSE is itself a
18619 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18620 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
a40e70de 18621 is a STRING_CST for the string literal before the parenthesis. Returns
18622 ERROR_MARK_NODE if any of the operands are invalid. */
0a3b29ad 18623
18624static tree
45baea8b 18625cp_parser_asm_operand_list (cp_parser* parser)
0a3b29ad 18626{
18627 tree asm_operands = NULL_TREE;
a40e70de 18628 bool invalid_operands = false;
0a3b29ad 18629
18630 while (true)
18631 {
18632 tree string_literal;
18633 tree expression;
18634 tree name;
ccb84981 18635
ccb84981 18636 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0a3b29ad 18637 {
18638 /* Consume the `[' token. */
18639 cp_lexer_consume_token (parser->lexer);
18640 /* Read the operand name. */
18641 name = cp_parser_identifier (parser);
ccb84981 18642 if (name != error_mark_node)
0a3b29ad 18643 name = build_string (IDENTIFIER_LENGTH (name),
18644 IDENTIFIER_POINTER (name));
18645 /* Look for the closing `]'. */
c247dce0 18646 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 18647 }
18648 else
18649 name = NULL_TREE;
18650 /* Look for the string-literal. */
00d26680 18651 string_literal = cp_parser_string_literal (parser, false, false);
18652
0a3b29ad 18653 /* Look for the `('. */
c247dce0 18654 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 18655 /* Parse the expression. */
98b326fd 18656 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 18657 /* Look for the `)'. */
c247dce0 18658 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
00d26680 18659
a40e70de 18660 if (name == error_mark_node
18661 || string_literal == error_mark_node
18662 || expression == error_mark_node)
18663 invalid_operands = true;
18664
0a3b29ad 18665 /* Add this operand to the list. */
18666 asm_operands = tree_cons (build_tree_list (name, string_literal),
ccb84981 18667 expression,
0a3b29ad 18668 asm_operands);
ccb84981 18669 /* If the next token is not a `,', there are no more
0a3b29ad 18670 operands. */
18671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18672 break;
18673 /* Consume the `,'. */
18674 cp_lexer_consume_token (parser->lexer);
18675 }
18676
a40e70de 18677 return invalid_operands ? error_mark_node : nreverse (asm_operands);
0a3b29ad 18678}
18679
ccb84981 18680/* Parse an asm-clobber-list.
0a3b29ad 18681
18682 asm-clobber-list:
18683 string-literal
ccb84981 18684 asm-clobber-list , string-literal
0a3b29ad 18685
18686 Returns a TREE_LIST, indicating the clobbers in the order that they
18687 appeared. The TREE_VALUE of each node is a STRING_CST. */
18688
18689static tree
45baea8b 18690cp_parser_asm_clobber_list (cp_parser* parser)
0a3b29ad 18691{
18692 tree clobbers = NULL_TREE;
18693
18694 while (true)
18695 {
0a3b29ad 18696 tree string_literal;
18697
18698 /* Look for the string literal. */
00d26680 18699 string_literal = cp_parser_string_literal (parser, false, false);
0a3b29ad 18700 /* Add it to the list. */
18701 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
ccb84981 18702 /* If the next token is not a `,', then the list is
0a3b29ad 18703 complete. */
18704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18705 break;
18706 /* Consume the `,' token. */
18707 cp_lexer_consume_token (parser->lexer);
18708 }
18709
18710 return clobbers;
18711}
18712
78f55ca8 18713/* Parse an asm-label-list.
18714
18715 asm-label-list:
18716 identifier
18717 asm-label-list , identifier
18718
18719 Returns a TREE_LIST, indicating the labels in the order that they
18720 appeared. The TREE_VALUE of each node is a label. */
18721
18722static tree
18723cp_parser_asm_label_list (cp_parser* parser)
18724{
18725 tree labels = NULL_TREE;
18726
18727 while (true)
18728 {
18729 tree identifier, label, name;
18730
18731 /* Look for the identifier. */
18732 identifier = cp_parser_identifier (parser);
18733 if (!error_operand_p (identifier))
18734 {
18735 label = lookup_label (identifier);
18736 if (TREE_CODE (label) == LABEL_DECL)
18737 {
18738 TREE_USED (label) = 1;
18739 check_goto (label);
18740 name = build_string (IDENTIFIER_LENGTH (identifier),
18741 IDENTIFIER_POINTER (identifier));
18742 labels = tree_cons (name, label, labels);
18743 }
18744 }
18745 /* If the next token is not a `,', then the list is
18746 complete. */
18747 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18748 break;
18749 /* Consume the `,' token. */
18750 cp_lexer_consume_token (parser->lexer);
18751 }
18752
18753 return nreverse (labels);
18754}
18755
0a3b29ad 18756/* Parse an (optional) series of attributes.
18757
18758 attributes:
18759 attributes attribute
18760
18761 attribute:
ccb84981 18762 __attribute__ (( attribute-list [opt] ))
0a3b29ad 18763
18764 The return value is as for cp_parser_attribute_list. */
ccb84981 18765
0a3b29ad 18766static tree
45baea8b 18767cp_parser_attributes_opt (cp_parser* parser)
0a3b29ad 18768{
18769 tree attributes = NULL_TREE;
18770
18771 while (true)
18772 {
18773 cp_token *token;
18774 tree attribute_list;
18775
18776 /* Peek at the next token. */
18777 token = cp_lexer_peek_token (parser->lexer);
18778 /* If it's not `__attribute__', then we're done. */
18779 if (token->keyword != RID_ATTRIBUTE)
18780 break;
18781
18782 /* Consume the `__attribute__' keyword. */
18783 cp_lexer_consume_token (parser->lexer);
18784 /* Look for the two `(' tokens. */
c247dce0 18785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18786 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 18787
18788 /* Peek at the next token. */
18789 token = cp_lexer_peek_token (parser->lexer);
18790 if (token->type != CPP_CLOSE_PAREN)
18791 /* Parse the attribute-list. */
18792 attribute_list = cp_parser_attribute_list (parser);
18793 else
18794 /* If the next token is a `)', then there is no attribute
18795 list. */
18796 attribute_list = NULL;
18797
18798 /* Look for the two `)' tokens. */
c247dce0 18799 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18800 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 18801
18802 /* Add these new attributes to the list. */
18803 attributes = chainon (attributes, attribute_list);
18804 }
18805
18806 return attributes;
18807}
18808
ccb84981 18809/* Parse an attribute-list.
0a3b29ad 18810
ccb84981 18811 attribute-list:
18812 attribute
0a3b29ad 18813 attribute-list , attribute
18814
18815 attribute:
ccb84981 18816 identifier
0a3b29ad 18817 identifier ( identifier )
18818 identifier ( identifier , expression-list )
ccb84981 18819 identifier ( expression-list )
0a3b29ad 18820
f0d4a607 18821 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18822 to an attribute. The TREE_PURPOSE of each node is the identifier
18823 indicating which attribute is in use. The TREE_VALUE represents
18824 the arguments, if any. */
0a3b29ad 18825
18826static tree
45baea8b 18827cp_parser_attribute_list (cp_parser* parser)
0a3b29ad 18828{
18829 tree attribute_list = NULL_TREE;
00d26680 18830 bool save_translate_strings_p = parser->translate_strings_p;
0a3b29ad 18831
00d26680 18832 parser->translate_strings_p = false;
0a3b29ad 18833 while (true)
18834 {
18835 cp_token *token;
18836 tree identifier;
18837 tree attribute;
18838
18839 /* Look for the identifier. We also allow keywords here; for
18840 example `__attribute__ ((const))' is legal. */
18841 token = cp_lexer_peek_token (parser->lexer);
f0d4a607 18842 if (token->type == CPP_NAME
18843 || token->type == CPP_KEYWORD)
18844 {
ce601f92 18845 tree arguments = NULL_TREE;
18846
f0d4a607 18847 /* Consume the token. */
18848 token = cp_lexer_consume_token (parser->lexer);
ccb84981 18849
f0d4a607 18850 /* Save away the identifier that indicates which attribute
9031d10b 18851 this is. */
d3ca4ed0 18852 identifier = (token->type == CPP_KEYWORD)
18853 /* For keywords, use the canonical spelling, not the
18854 parsed identifier. */
18855 ? ridpointers[(int) token->keyword]
18856 : token->u.value;
18857
f0d4a607 18858 attribute = build_tree_list (identifier, NULL_TREE);
0a3b29ad 18859
f0d4a607 18860 /* Peek at the next token. */
18861 token = cp_lexer_peek_token (parser->lexer);
18862 /* If it's an `(', then parse the attribute arguments. */
18863 if (token->type == CPP_OPEN_PAREN)
18864 {
f352a3fb 18865 VEC(tree,gc) *vec;
33199a81 18866 int attr_flag = (attribute_takes_identifier_p (identifier)
18867 ? id_attr : normal_attr);
f352a3fb 18868 vec = cp_parser_parenthesized_expression_list
33199a81 18869 (parser, attr_flag, /*cast_p=*/false,
f352a3fb 18870 /*allow_expansion_p=*/false,
18871 /*non_constant_p=*/NULL);
18872 if (vec == NULL)
18873 arguments = error_mark_node;
18874 else
18875 {
18876 arguments = build_tree_list_vec (vec);
18877 release_tree_vector (vec);
18878 }
ce601f92 18879 /* Save the arguments away. */
f0d4a607 18880 TREE_VALUE (attribute) = arguments;
18881 }
0a3b29ad 18882
ce601f92 18883 if (arguments != error_mark_node)
18884 {
18885 /* Add this attribute to the list. */
18886 TREE_CHAIN (attribute) = attribute_list;
18887 attribute_list = attribute;
18888 }
0a3b29ad 18889
f0d4a607 18890 token = cp_lexer_peek_token (parser->lexer);
18891 }
18892 /* Now, look for more attributes. If the next token isn't a
18893 `,', we're done. */
0a3b29ad 18894 if (token->type != CPP_COMMA)
18895 break;
18896
63eff20d 18897 /* Consume the comma and keep going. */
0a3b29ad 18898 cp_lexer_consume_token (parser->lexer);
18899 }
00d26680 18900 parser->translate_strings_p = save_translate_strings_p;
0a3b29ad 18901
18902 /* We built up the list in reverse order. */
18903 return nreverse (attribute_list);
18904}
18905
18906/* Parse an optional `__extension__' keyword. Returns TRUE if it is
18907 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18908 current value of the PEDANTIC flag, regardless of whether or not
18909 the `__extension__' keyword is present. The caller is responsible
18910 for restoring the value of the PEDANTIC flag. */
18911
18912static bool
45baea8b 18913cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
0a3b29ad 18914{
18915 /* Save the old value of the PEDANTIC flag. */
18916 *saved_pedantic = pedantic;
18917
18918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18919 {
18920 /* Consume the `__extension__' token. */
18921 cp_lexer_consume_token (parser->lexer);
18922 /* We're not being pedantic while the `__extension__' keyword is
18923 in effect. */
18924 pedantic = 0;
18925
18926 return true;
18927 }
18928
18929 return false;
18930}
18931
18932/* Parse a label declaration.
18933
18934 label-declaration:
18935 __label__ label-declarator-seq ;
18936
18937 label-declarator-seq:
18938 identifier , label-declarator-seq
18939 identifier */
18940
18941static void
45baea8b 18942cp_parser_label_declaration (cp_parser* parser)
0a3b29ad 18943{
18944 /* Look for the `__label__' keyword. */
c247dce0 18945 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
0a3b29ad 18946
18947 while (true)
18948 {
18949 tree identifier;
18950
18951 /* Look for an identifier. */
18952 identifier = cp_parser_identifier (parser);
f7d1c2ea 18953 /* If we failed, stop. */
18954 if (identifier == error_mark_node)
18955 break;
18956 /* Declare it as a label. */
0a3b29ad 18957 finish_label_decl (identifier);
18958 /* If the next token is a `;', stop. */
18959 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18960 break;
18961 /* Look for the `,' separating the label declarations. */
c247dce0 18962 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
0a3b29ad 18963 }
18964
18965 /* Look for the final `;'. */
c247dce0 18966 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 18967}
18968
18969/* Support Functions */
18970
18971/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18972 NAME should have one of the representations used for an
18973 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18974 is returned. If PARSER->SCOPE is a dependent type, then a
18975 SCOPE_REF is returned.
18976
18977 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18978 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18979 was formed. Abstractly, such entities should not be passed to this
18980 function, because they do not need to be looked up, but it is
18981 simpler to check for this special case here, rather than at the
18982 call-sites.
18983
18984 In cases not explicitly covered above, this function returns a
18985 DECL, OVERLOAD, or baselink representing the result of the lookup.
18986 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18987 is returned.
18988
0be5f5cc 18989 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
e2ae55f2 18990 (e.g., "struct") that was used. In that case bindings that do not
18991 refer to types are ignored.
0a3b29ad 18992
c3b9e457 18993 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18994 ignored.
18995
6fc758aa 18996 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18997 are ignored.
18998
0a3b29ad 18999 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
9031d10b 19000 types.
2cdbcd51 19001
b62240d5 19002 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
149fdb98 19003 TREE_LIST of candidates if name-lookup results in an ambiguity, and
074ab442 19004 NULL_TREE otherwise. */
0a3b29ad 19005
19006static tree
ccb84981 19007cp_parser_lookup_name (cp_parser *parser, tree name,
e2ae55f2 19008 enum tag_types tag_type,
074ab442 19009 bool is_template,
fbb01da7 19010 bool is_namespace,
2cdbcd51 19011 bool check_dependency,
ad9ae192 19012 tree *ambiguous_decls,
19013 location_t name_location)
0a3b29ad 19014{
0e8ce9be 19015 int flags = 0;
0a3b29ad 19016 tree decl;
19017 tree object_type = parser->context->object_type;
19018
0e8ce9be 19019 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19020 flags |= LOOKUP_COMPLAIN;
19021
2cdbcd51 19022 /* Assume that the lookup will be unambiguous. */
b62240d5 19023 if (ambiguous_decls)
19024 *ambiguous_decls = NULL_TREE;
2cdbcd51 19025
0a3b29ad 19026 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19027 no longer valid. Note that if we are parsing tentatively, and
19028 the parse fails, OBJECT_TYPE will be automatically restored. */
19029 parser->context->object_type = NULL_TREE;
19030
19031 if (name == error_mark_node)
19032 return error_mark_node;
19033
19034 /* A template-id has already been resolved; there is no lookup to
19035 do. */
19036 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19037 return name;
19038 if (BASELINK_P (name))
19039 {
b4df430b 19040 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19041 == TEMPLATE_ID_EXPR);
0a3b29ad 19042 return name;
19043 }
19044
19045 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19046 it should already have been checked to make sure that the name
19047 used matches the type being destroyed. */
19048 if (TREE_CODE (name) == BIT_NOT_EXPR)
19049 {
19050 tree type;
19051
19052 /* Figure out to which type this destructor applies. */
19053 if (parser->scope)
19054 type = parser->scope;
19055 else if (object_type)
19056 type = object_type;
19057 else
19058 type = current_class_type;
19059 /* If that's not a class type, there is no destructor. */
19060 if (!type || !CLASS_TYPE_P (type))
19061 return error_mark_node;
ed36f1cf 19062 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19063 lazily_declare_fn (sfk_destructor, type);
2b40dfce 19064 if (!CLASSTYPE_DESTRUCTORS (type))
19065 return error_mark_node;
0a3b29ad 19066 /* If it was a class type, return the destructor. */
19067 return CLASSTYPE_DESTRUCTORS (type);
19068 }
19069
19070 /* By this point, the NAME should be an ordinary identifier. If
19071 the id-expression was a qualified name, the qualifying scope is
19072 stored in PARSER->SCOPE at this point. */
b4df430b 19073 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
ccb84981 19074
0a3b29ad 19075 /* Perform the lookup. */
19076 if (parser->scope)
ccb84981 19077 {
7e9a6a16 19078 bool dependent_p;
0a3b29ad 19079
19080 if (parser->scope == error_mark_node)
19081 return error_mark_node;
19082
19083 /* If the SCOPE is dependent, the lookup must be deferred until
19084 the template is instantiated -- unless we are explicitly
19085 looking up names in uninstantiated templates. Even then, we
19086 cannot look up the name if the scope is not a class type; it
19087 might, for example, be a template type parameter. */
7e9a6a16 19088 dependent_p = (TYPE_P (parser->scope)
05f701e2 19089 && dependent_scope_p (parser->scope));
0a3b29ad 19090 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
05f701e2 19091 && dependent_p)
19092 /* Defer lookup. */
19093 decl = error_mark_node;
0a3b29ad 19094 else
19095 {
7f602bca 19096 tree pushed_scope = NULL_TREE;
1cbda81f 19097
0a3b29ad 19098 /* If PARSER->SCOPE is a dependent type, then it must be a
19099 class type, and we must not be checking dependencies;
19100 otherwise, we would have processed this lookup above. So
19101 that PARSER->SCOPE is not considered a dependent base by
19102 lookup_member, we must enter the scope here. */
7e9a6a16 19103 if (dependent_p)
7f602bca 19104 pushed_scope = push_scope (parser->scope);
a70e3c37 19105
49c8ed9f 19106 /* If the PARSER->SCOPE is a template specialization, it
19107 may be instantiated during name lookup. In that case,
19108 errors may be issued. Even if we rollback the current
19109 tentative parse, those errors are valid. */
19110 decl = lookup_qualified_name (parser->scope, name,
19111 tag_type != none_type,
19112 /*complain=*/true);
19113
a70e3c37 19114 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19115 lookup result and the nested-name-specifier nominates a class C:
19116 * if the name specified after the nested-name-specifier, when
19117 looked up in C, is the injected-class-name of C (Clause 9), or
19118 * if the name specified after the nested-name-specifier is the
19119 same as the identifier or the simple-template-id's template-
19120 name in the last component of the nested-name-specifier,
19121 the name is instead considered to name the constructor of
19122 class C. [ Note: for example, the constructor is not an
19123 acceptable lookup result in an elaborated-type-specifier so
19124 the constructor would not be used in place of the
19125 injected-class-name. --end note ] Such a constructor name
19126 shall be used only in the declarator-id of a declaration that
19127 names a constructor or in a using-declaration. */
19128 if (tag_type == none_type
49c8ed9f 19129 && DECL_SELF_REFERENCE_P (decl)
19130 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19131 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19132 tag_type != none_type,
19133 /*complain=*/true);
93a3c02a 19134
19135 /* If we have a single function from a using decl, pull it out. */
05f701e2 19136 if (TREE_CODE (decl) == OVERLOAD
93a3c02a 19137 && !really_overloaded_fn (decl))
19138 decl = OVL_FUNCTION (decl);
19139
7f602bca 19140 if (pushed_scope)
19141 pop_scope (pushed_scope);
0a3b29ad 19142 }
05f701e2 19143
19144 /* If the scope is a dependent type and either we deferred lookup or
19145 we did lookup but didn't find the name, rememeber the name. */
19146 if (decl == error_mark_node && TYPE_P (parser->scope)
19147 && dependent_type_p (parser->scope))
19148 {
19149 if (tag_type)
19150 {
19151 tree type;
19152
19153 /* The resolution to Core Issue 180 says that `struct
19154 A::B' should be considered a type-name, even if `A'
19155 is dependent. */
19156 type = make_typename_type (parser->scope, name, tag_type,
19157 /*complain=*/tf_error);
19158 decl = TYPE_NAME (type);
19159 }
19160 else if (is_template
19161 && (cp_parser_next_token_ends_template_argument_p (parser)
19162 || cp_lexer_next_token_is (parser->lexer,
19163 CPP_CLOSE_PAREN)))
19164 decl = make_unbound_class_template (parser->scope,
19165 name, NULL_TREE,
19166 /*complain=*/tf_error);
19167 else
19168 decl = build_qualified_name (/*type=*/NULL_TREE,
19169 parser->scope, name,
19170 is_template);
19171 }
0a3b29ad 19172 parser->qualifying_scope = parser->scope;
19173 parser->object_scope = NULL_TREE;
19174 }
19175 else if (object_type)
19176 {
19177 tree object_decl = NULL_TREE;
19178 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19179 OBJECT_TYPE is not a class. */
19180 if (CLASS_TYPE_P (object_type))
19181 /* If the OBJECT_TYPE is a template specialization, it may
19182 be instantiated during name lookup. In that case, errors
19183 may be issued. Even if we rollback the current tentative
19184 parse, those errors are valid. */
19185 object_decl = lookup_member (object_type,
19186 name,
9031d10b 19187 /*protect=*/0,
e2ae55f2 19188 tag_type != none_type);
0a3b29ad 19189 /* Look it up in the enclosing context, too. */
9031d10b 19190 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 19191 /*nonclass=*/0,
0e8ce9be 19192 /*block_p=*/true, is_namespace, flags);
0a3b29ad 19193 parser->object_scope = object_type;
19194 parser->qualifying_scope = NULL_TREE;
19195 if (object_decl)
19196 decl = object_decl;
19197 }
19198 else
19199 {
9031d10b 19200 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 19201 /*nonclass=*/0,
0e8ce9be 19202 /*block_p=*/true, is_namespace, flags);
0a3b29ad 19203 parser->qualifying_scope = NULL_TREE;
19204 parser->object_scope = NULL_TREE;
19205 }
19206
19207 /* If the lookup failed, let our caller know. */
3f3fa556 19208 if (!decl || decl == error_mark_node)
0a3b29ad 19209 return error_mark_node;
19210
6dd3a38d 19211 /* Pull out the template from an injected-class-name (or multiple). */
19212 if (is_template)
19213 decl = maybe_get_template_decl_from_type_decl (decl);
19214
0a3b29ad 19215 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19216 if (TREE_CODE (decl) == TREE_LIST)
19217 {
b62240d5 19218 if (ambiguous_decls)
19219 *ambiguous_decls = decl;
0a3b29ad 19220 /* The error message we have to print is too complicated for
19221 cp_parser_error, so we incorporate its actions directly. */
2c593bd0 19222 if (!cp_parser_simulate_error (parser))
0a3b29ad 19223 {
ccb59bb6 19224 error_at (name_location, "reference to %qD is ambiguous",
19225 name);
0a3b29ad 19226 print_candidates (decl);
19227 }
19228 return error_mark_node;
19229 }
19230
b4df430b 19231 gcc_assert (DECL_P (decl)
19232 || TREE_CODE (decl) == OVERLOAD
19233 || TREE_CODE (decl) == SCOPE_REF
19234 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19235 || BASELINK_P (decl));
0a3b29ad 19236
19237 /* If we have resolved the name of a member declaration, check to
19238 see if the declaration is accessible. When the name resolves to
755edffd 19239 set of overloaded functions, accessibility is checked when
ccb84981 19240 overload resolution is done.
0a3b29ad 19241
19242 During an explicit instantiation, access is not checked at all,
19243 as per [temp.explicit]. */
4f62c42e 19244 if (DECL_P (decl))
ef4534a3 19245 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
0a3b29ad 19246
19247 return decl;
19248}
19249
19250/* Like cp_parser_lookup_name, but for use in the typical case where
c3b9e457 19251 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19252 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
0a3b29ad 19253
19254static tree
ad9ae192 19255cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
0a3b29ad 19256{
ccb84981 19257 return cp_parser_lookup_name (parser, name,
e2ae55f2 19258 none_type,
c3b9e457 19259 /*is_template=*/false,
6fc758aa 19260 /*is_namespace=*/false,
2cdbcd51 19261 /*check_dependency=*/true,
ad9ae192 19262 /*ambiguous_decls=*/NULL,
19263 location);
0a3b29ad 19264}
19265
0a3b29ad 19266/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19267 the current context, return the TYPE_DECL. If TAG_NAME_P is
19268 true, the DECL indicates the class being defined in a class-head,
19269 or declared in an elaborated-type-specifier.
19270
19271 Otherwise, return DECL. */
19272
19273static tree
19274cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19275{
a5e50b31 19276 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19277 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
0a3b29ad 19278
ccb84981 19279 struct A {
653e5405 19280 template <typename T> struct B;
0a3b29ad 19281 };
19282
ccb84981 19283 template <typename T> struct A::B {};
19284
e4bc96e2 19285 Similarly, in an elaborated-type-specifier:
0a3b29ad 19286
19287 namespace N { struct X{}; }
19288
19289 struct A {
653e5405 19290 template <typename T> friend struct N::X;
0a3b29ad 19291 };
19292
a5e50b31 19293 However, if the DECL refers to a class type, and we are in
19294 the scope of the class, then the name lookup automatically
19295 finds the TYPE_DECL created by build_self_reference rather
19296 than a TEMPLATE_DECL. For example, in:
19297
19298 template <class T> struct S {
653e5405 19299 S s;
a5e50b31 19300 };
19301
19302 there is no need to handle such case. */
19303
19304 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
0a3b29ad 19305 return DECL_TEMPLATE_RESULT (decl);
19306
19307 return decl;
19308}
19309
19310/* If too many, or too few, template-parameter lists apply to the
19311 declarator, issue an error message. Returns TRUE if all went well,
19312 and FALSE otherwise. */
19313
19314static bool
ccb84981 19315cp_parser_check_declarator_template_parameters (cp_parser* parser,
ad9ae192 19316 cp_declarator *declarator,
19317 location_t declarator_location)
0a3b29ad 19318{
19319 unsigned num_templates;
19320
19321 /* We haven't seen any classes that involve template parameters yet. */
19322 num_templates = 0;
19323
3046c0a3 19324 switch (declarator->kind)
0a3b29ad 19325 {
3046c0a3 19326 case cdk_id:
2ded3667 19327 if (declarator->u.id.qualifying_scope)
3046c0a3 19328 {
19329 tree scope;
0a3b29ad 19330
2ded3667 19331 scope = declarator->u.id.qualifying_scope;
0a3b29ad 19332
3046c0a3 19333 while (scope && CLASS_TYPE_P (scope))
19334 {
19335 /* You're supposed to have one `template <...>'
19336 for every template class, but you don't need one
19337 for a full specialization. For example:
19338
19339 template <class T> struct S{};
19340 template <> struct S<int> { void f(); };
19341 void S<int>::f () {}
19342
19343 is correct; there shouldn't be a `template <>' for
19344 the definition of `S<int>::f'. */
04ef83b7 19345 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19346 /* If SCOPE does not have template information of any
19347 kind, then it is not a template, nor is it nested
19348 within a template. */
19349 break;
19350 if (explicit_class_specialization_p (scope))
19351 break;
19352 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
3046c0a3 19353 ++num_templates;
19354
19355 scope = TYPE_CONTEXT (scope);
19356 }
19357 }
9031d10b 19358 else if (TREE_CODE (declarator->u.id.unqualified_name)
2ded3667 19359 == TEMPLATE_ID_EXPR)
19360 /* If the DECLARATOR has the form `X<y>' then it uses one
19361 additional level of template parameters. */
0a3b29ad 19362 ++num_templates;
19363
7b07a15e 19364 return cp_parser_check_template_parameters
19365 (parser, num_templates, declarator_location, declarator);
19366
3046c0a3 19367
19368 case cdk_function:
19369 case cdk_array:
19370 case cdk_pointer:
19371 case cdk_reference:
19372 case cdk_ptrmem:
207355ad 19373 return (cp_parser_check_declarator_template_parameters
ad9ae192 19374 (parser, declarator->declarator, declarator_location));
3046c0a3 19375
19376 case cdk_error:
19377 return true;
19378
19379 default:
2e3e31d2 19380 gcc_unreachable ();
0a3b29ad 19381 }
2e3e31d2 19382 return false;
0a3b29ad 19383}
19384
19385/* NUM_TEMPLATES were used in the current declaration. If that is
19386 invalid, return FALSE and issue an error messages. Otherwise,
7b07a15e 19387 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19388 declarator and we can print more accurate diagnostics. */
0a3b29ad 19389
19390static bool
45baea8b 19391cp_parser_check_template_parameters (cp_parser* parser,
ad9ae192 19392 unsigned num_templates,
7b07a15e 19393 location_t location,
19394 cp_declarator *declarator)
0a3b29ad 19395{
7b07a15e 19396 /* If there are the same number of template classes and parameter
19397 lists, that's OK. */
19398 if (parser->num_template_parameter_lists == num_templates)
19399 return true;
19400 /* If there are more, but only one more, then we are referring to a
19401 member template. That's OK too. */
19402 if (parser->num_template_parameter_lists == num_templates + 1)
19403 return true;
0a3b29ad 19404 /* If there are more template classes than parameter lists, we have
19405 something like:
ccb84981 19406
0a3b29ad 19407 template <class T> void S<T>::R<T>::f (); */
19408 if (parser->num_template_parameter_lists < num_templates)
19409 {
5bd9bf06 19410 if (declarator && !current_function_decl)
7b07a15e 19411 error_at (location, "specializing member %<%T::%E%> "
19412 "requires %<template<>%> syntax",
19413 declarator->u.id.qualifying_scope,
19414 declarator->u.id.unqualified_name);
5bd9bf06 19415 else if (declarator)
19416 error_at (location, "invalid declaration of %<%T::%E%>",
19417 declarator->u.id.qualifying_scope,
19418 declarator->u.id.unqualified_name);
7b07a15e 19419 else
19420 error_at (location, "too few template-parameter-lists");
0a3b29ad 19421 return false;
19422 }
0a3b29ad 19423 /* Otherwise, there are too many template parameter lists. We have
19424 something like:
19425
19426 template <class T> template <class U> void S::f(); */
ccb59bb6 19427 error_at (location, "too many template-parameter-lists");
0a3b29ad 19428 return false;
19429}
19430
0a3b29ad 19431/* Parse an optional `::' token indicating that the following name is
19432 from the global namespace. If so, PARSER->SCOPE is set to the
19433 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19434 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19435 Returns the new value of PARSER->SCOPE, if the `::' token is
19436 present, and NULL_TREE otherwise. */
19437
19438static tree
130bb1d4 19439cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
0a3b29ad 19440{
19441 cp_token *token;
19442
19443 /* Peek at the next token. */
19444 token = cp_lexer_peek_token (parser->lexer);
19445 /* If we're looking at a `::' token then we're starting from the
19446 global namespace, not our current location. */
19447 if (token->type == CPP_SCOPE)
19448 {
19449 /* Consume the `::' token. */
19450 cp_lexer_consume_token (parser->lexer);
19451 /* Set the SCOPE so that we know where to start the lookup. */
19452 parser->scope = global_namespace;
19453 parser->qualifying_scope = global_namespace;
19454 parser->object_scope = NULL_TREE;
19455
19456 return parser->scope;
19457 }
130bb1d4 19458 else if (!current_scope_valid_p)
0a3b29ad 19459 {
19460 parser->scope = NULL_TREE;
19461 parser->qualifying_scope = NULL_TREE;
130bb1d4 19462 parser->object_scope = NULL_TREE;
0a3b29ad 19463 }
19464
19465 return NULL_TREE;
19466}
19467
19468/* Returns TRUE if the upcoming token sequence is the start of a
19469 constructor declarator. If FRIEND_P is true, the declarator is
19470 preceded by the `friend' specifier. */
19471
19472static bool
19473cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19474{
19475 bool constructor_p;
a70e3c37 19476 tree nested_name_specifier;
b3c48b5d 19477 cp_token *next_token;
19478
19479 /* The common case is that this is not a constructor declarator, so
954ad420 19480 try to avoid doing lots of work if at all possible. It's not
19481 valid declare a constructor at function scope. */
0aeb1cc5 19482 if (parser->in_function_body)
954ad420 19483 return false;
19484 /* And only certain tokens can begin a constructor declarator. */
b3c48b5d 19485 next_token = cp_lexer_peek_token (parser->lexer);
19486 if (next_token->type != CPP_NAME
19487 && next_token->type != CPP_SCOPE
19488 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19489 && next_token->type != CPP_TEMPLATE_ID)
19490 return false;
0a3b29ad 19491
19492 /* Parse tentatively; we are going to roll back all of the tokens
19493 consumed here. */
19494 cp_parser_parse_tentatively (parser);
19495 /* Assume that we are looking at a constructor declarator. */
19496 constructor_p = true;
4f62c42e 19497
0a3b29ad 19498 /* Look for the optional `::' operator. */
19499 cp_parser_global_scope_opt (parser,
130bb1d4 19500 /*current_scope_valid_p=*/false);
0a3b29ad 19501 /* Look for the nested-name-specifier. */
a70e3c37 19502 nested_name_specifier
0a3b29ad 19503 = (cp_parser_nested_name_specifier_opt (parser,
19504 /*typename_keyword_p=*/false,
19505 /*check_dependency_p=*/false,
3d0f901b 19506 /*type_p=*/false,
a70e3c37 19507 /*is_declaration=*/false));
0a3b29ad 19508 /* Outside of a class-specifier, there must be a
19509 nested-name-specifier. */
a70e3c37 19510 if (!nested_name_specifier &&
0a3b29ad 19511 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19512 || friend_p))
19513 constructor_p = false;
a70e3c37 19514 else if (nested_name_specifier == error_mark_node)
19515 constructor_p = false;
19516
19517 /* If we have a class scope, this is easy; DR 147 says that S::S always
19518 names the constructor, and no other qualified name could. */
19519 if (constructor_p && nested_name_specifier
19520 && TYPE_P (nested_name_specifier))
19521 {
19522 tree id = cp_parser_unqualified_id (parser,
19523 /*template_keyword_p=*/false,
19524 /*check_dependency_p=*/false,
19525 /*declarator_p=*/true,
19526 /*optional_p=*/false);
19527 if (is_overloaded_fn (id))
19528 id = DECL_NAME (get_first_fn (id));
19529 if (!constructor_name_p (id, nested_name_specifier))
19530 constructor_p = false;
19531 }
0a3b29ad 19532 /* If we still think that this might be a constructor-declarator,
19533 look for a class-name. */
a70e3c37 19534 else if (constructor_p)
0a3b29ad 19535 {
19536 /* If we have:
19537
a70e3c37 19538 template <typename T> struct S {
19539 S();
19540 };
0a3b29ad 19541
a70e3c37 19542 we must recognize that the nested `S' names a class. */
19543 tree type_decl;
0a3b29ad 19544 type_decl = cp_parser_class_name (parser,
19545 /*typename_keyword_p=*/false,
19546 /*template_keyword_p=*/false,
e2ae55f2 19547 none_type,
0a3b29ad 19548 /*check_dependency_p=*/false,
3d0f901b 19549 /*class_head_p=*/false,
19550 /*is_declaration=*/false);
0a3b29ad 19551 /* If there was no class-name, then this is not a constructor. */
19552 constructor_p = !cp_parser_error_occurred (parser);
4f62c42e 19553
a70e3c37 19554 /* If we're still considering a constructor, we have to see a `(',
19555 to begin the parameter-declaration-clause, followed by either a
19556 `)', an `...', or a decl-specifier. We need to check for a
19557 type-specifier to avoid being fooled into thinking that:
0a3b29ad 19558
a70e3c37 19559 S (f) (int);
0a3b29ad 19560
a70e3c37 19561 is a constructor. (It is actually a function named `f' that
19562 takes one parameter (of type `int') and returns a value of type
19563 `S'. */
19564 if (constructor_p
c247dce0 19565 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
a70e3c37 19566 constructor_p = false;
19567
19568 if (constructor_p
19569 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
0a3b29ad 19570 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
e67a67ea 19571 /* A parameter declaration begins with a decl-specifier,
19572 which is either the "attribute" keyword, a storage class
19573 specifier, or (usually) a type-specifier. */
9a7c4b43 19574 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
0a3b29ad 19575 {
8c1f65e6 19576 tree type;
7f602bca 19577 tree pushed_scope = NULL_TREE;
bb91f165 19578 unsigned saved_num_template_parameter_lists;
8c1f65e6 19579
19580 /* Names appearing in the type-specifier should be looked up
19581 in the scope of the class. */
19582 if (current_class_type)
19583 type = NULL_TREE;
0a3b29ad 19584 else
19585 {
8c1f65e6 19586 type = TREE_TYPE (type_decl);
19587 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 19588 {
ccb84981 19589 type = resolve_typename_type (type,
5f6526e1 19590 /*only_current_p=*/false);
8826a863 19591 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 19592 {
19593 cp_parser_abort_tentative_parse (parser);
19594 return false;
19595 }
19596 }
7f602bca 19597 pushed_scope = push_scope (type);
0a3b29ad 19598 }
bb91f165 19599
19600 /* Inside the constructor parameter list, surrounding
19601 template-parameter-lists do not apply. */
19602 saved_num_template_parameter_lists
19603 = parser->num_template_parameter_lists;
19604 parser->num_template_parameter_lists = 0;
19605
8c1f65e6 19606 /* Look for the type-specifier. */
19607 cp_parser_type_specifier (parser,
19608 CP_PARSER_FLAGS_NONE,
4b9b2871 19609 /*decl_specs=*/NULL,
8c1f65e6 19610 /*is_declarator=*/true,
19611 /*declares_class_or_enum=*/NULL,
19612 /*is_cv_qualifier=*/NULL);
bb91f165 19613
19614 parser->num_template_parameter_lists
19615 = saved_num_template_parameter_lists;
19616
8c1f65e6 19617 /* Leave the scope of the class. */
7f602bca 19618 if (pushed_scope)
19619 pop_scope (pushed_scope);
8c1f65e6 19620
19621 constructor_p = !cp_parser_error_occurred (parser);
0a3b29ad 19622 }
19623 }
a70e3c37 19624
0a3b29ad 19625 /* We did not really want to consume any tokens. */
19626 cp_parser_abort_tentative_parse (parser);
19627
19628 return constructor_p;
19629}
19630
19631/* Parse the definition of the function given by the DECL_SPECIFIERS,
9b57b06b 19632 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
0a3b29ad 19633 they must be performed once we are in the scope of the function.
19634
19635 Returns the function defined. */
19636
19637static tree
19638cp_parser_function_definition_from_specifiers_and_declarator
45baea8b 19639 (cp_parser* parser,
4b9b2871 19640 cp_decl_specifier_seq *decl_specifiers,
45baea8b 19641 tree attributes,
3046c0a3 19642 const cp_declarator *declarator)
0a3b29ad 19643{
19644 tree fn;
19645 bool success_p;
19646
19647 /* Begin the function-definition. */
3046c0a3 19648 success_p = start_function (decl_specifiers, declarator, attributes);
19649
19650 /* The things we're about to see are not directly qualified by any
19651 template headers we've seen thus far. */
19652 reset_specialization ();
0a3b29ad 19653
19654 /* If there were names looked up in the decl-specifier-seq that we
19655 did not check, check them now. We must wait until we are in the
19656 scope of the function to perform the checks, since the function
19657 might be a friend. */
9b57b06b 19658 perform_deferred_access_checks ();
0a3b29ad 19659
19660 if (!success_p)
19661 {
3046c0a3 19662 /* Skip the entire function. */
0a3b29ad 19663 cp_parser_skip_to_end_of_block_or_statement (parser);
19664 fn = error_mark_node;
19665 }
ddb7a3b0 19666 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19667 {
19668 /* Seen already, skip it. An error message has already been output. */
19669 cp_parser_skip_to_end_of_block_or_statement (parser);
19670 fn = current_function_decl;
19671 current_function_decl = NULL_TREE;
19672 /* If this is a function from a class, pop the nested class. */
19673 if (current_class_name)
19674 pop_nested_class ();
19675 }
0a3b29ad 19676 else
19677 fn = cp_parser_function_definition_after_declarator (parser,
19678 /*inline_p=*/false);
19679
19680 return fn;
19681}
19682
19683/* Parse the part of a function-definition that follows the
19684 declarator. INLINE_P is TRUE iff this function is an inline
a8b75081 19685 function defined within a class-specifier.
0a3b29ad 19686
19687 Returns the function defined. */
19688
ccb84981 19689static tree
19690cp_parser_function_definition_after_declarator (cp_parser* parser,
45baea8b 19691 bool inline_p)
0a3b29ad 19692{
19693 tree fn;
19694 bool ctor_initializer_p = false;
19695 bool saved_in_unbraced_linkage_specification_p;
0aeb1cc5 19696 bool saved_in_function_body;
0a3b29ad 19697 unsigned saved_num_template_parameter_lists;
ad9ae192 19698 cp_token *token;
0a3b29ad 19699
0aeb1cc5 19700 saved_in_function_body = parser->in_function_body;
19701 parser->in_function_body = true;
0a3b29ad 19702 /* If the next token is `return', then the code may be trying to
19703 make use of the "named return value" extension that G++ used to
19704 support. */
ad9ae192 19705 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 19706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19707 {
19708 /* Consume the `return' keyword. */
19709 cp_lexer_consume_token (parser->lexer);
19710 /* Look for the identifier that indicates what value is to be
19711 returned. */
19712 cp_parser_identifier (parser);
19713 /* Issue an error message. */
ccb59bb6 19714 error_at (token->location,
19715 "named return values are no longer supported");
0a3b29ad 19716 /* Skip tokens until we reach the start of the function body. */
b75b98aa 19717 while (true)
19718 {
19719 cp_token *token = cp_lexer_peek_token (parser->lexer);
19720 if (token->type == CPP_OPEN_BRACE
19721 || token->type == CPP_EOF
19722 || token->type == CPP_PRAGMA_EOL)
19723 break;
19724 cp_lexer_consume_token (parser->lexer);
19725 }
0a3b29ad 19726 }
19727 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19728 anything declared inside `f'. */
ccb84981 19729 saved_in_unbraced_linkage_specification_p
0a3b29ad 19730 = parser->in_unbraced_linkage_specification_p;
19731 parser->in_unbraced_linkage_specification_p = false;
19732 /* Inside the function, surrounding template-parameter-lists do not
19733 apply. */
ccb84981 19734 saved_num_template_parameter_lists
19735 = parser->num_template_parameter_lists;
0a3b29ad 19736 parser->num_template_parameter_lists = 0;
a8b75081 19737
19738 start_lambda_scope (current_function_decl);
19739
0a3b29ad 19740 /* If the next token is `try', then we are looking at a
19741 function-try-block. */
19742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19743 ctor_initializer_p = cp_parser_function_try_block (parser);
19744 /* A function-try-block includes the function-body, so we only do
19745 this next part if we're not processing a function-try-block. */
19746 else
ccb84981 19747 ctor_initializer_p
0a3b29ad 19748 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19749
a8b75081 19750 finish_lambda_scope ();
19751
0a3b29ad 19752 /* Finish the function. */
ccb84981 19753 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
0a3b29ad 19754 (inline_p ? 2 : 0));
19755 /* Generate code for it, if necessary. */
6cb758f0 19756 expand_or_defer_fn (fn);
0a3b29ad 19757 /* Restore the saved values. */
ccb84981 19758 parser->in_unbraced_linkage_specification_p
0a3b29ad 19759 = saved_in_unbraced_linkage_specification_p;
ccb84981 19760 parser->num_template_parameter_lists
0a3b29ad 19761 = saved_num_template_parameter_lists;
0aeb1cc5 19762 parser->in_function_body = saved_in_function_body;
0a3b29ad 19763
19764 return fn;
19765}
19766
19767/* Parse a template-declaration, assuming that the `export' (and
19768 `extern') keywords, if present, has already been scanned. MEMBER_P
19769 is as for cp_parser_template_declaration. */
19770
19771static void
45baea8b 19772cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
0a3b29ad 19773{
19774 tree decl = NULL_TREE;
3369eb76 19775 VEC (deferred_access_check,gc) *checks;
0a3b29ad 19776 tree parameter_list;
19777 bool friend_p = false;
9f25cdd8 19778 bool need_lang_pop;
ad9ae192 19779 cp_token *token;
0a3b29ad 19780
19781 /* Look for the `template' keyword. */
ad9ae192 19782 token = cp_lexer_peek_token (parser->lexer);
c247dce0 19783 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
0a3b29ad 19784 return;
ccb84981 19785
0a3b29ad 19786 /* And the `<'. */
c247dce0 19787 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
0a3b29ad 19788 return;
a34824c0 19789 if (at_class_scope_p () && current_function_decl)
19790 {
19791 /* 14.5.2.2 [temp.mem]
19792
19793 A local class shall not have member templates. */
ccb59bb6 19794 error_at (token->location,
19795 "invalid declaration of member template in local class");
a34824c0 19796 cp_parser_skip_to_end_of_block_or_statement (parser);
19797 return;
19798 }
9f25cdd8 19799 /* [temp]
074ab442 19800
9f25cdd8 19801 A template ... shall not have C linkage. */
19802 if (current_lang_name == lang_name_c)
19803 {
ccb59bb6 19804 error_at (token->location, "template with C linkage");
9f25cdd8 19805 /* Give it C++ linkage to avoid confusing other parts of the
19806 front end. */
19807 push_lang_context (lang_name_cplusplus);
19808 need_lang_pop = true;
19809 }
19810 else
19811 need_lang_pop = false;
23010bc8 19812
19813 /* We cannot perform access checks on the template parameter
19814 declarations until we know what is being declared, just as we
19815 cannot check the decl-specifier list. */
19816 push_deferring_access_checks (dk_deferred);
19817
0a3b29ad 19818 /* If the next token is `>', then we have an invalid
19819 specialization. Rather than complain about an invalid template
19820 parameter, issue an error message here. */
19821 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19822 {
19823 cp_parser_error (parser, "invalid explicit specialization");
3398499c 19824 begin_specialization ();
0a3b29ad 19825 parameter_list = NULL_TREE;
19826 }
19827 else
7be1bc1f 19828 /* Parse the template parameters. */
19829 parameter_list = cp_parser_template_parameter_list (parser);
3398499c 19830
23010bc8 19831 /* Get the deferred access checks from the parameter list. These
19832 will be checked once we know what is being declared, as for a
19833 member template the checks must be performed in the scope of the
19834 class containing the member. */
19835 checks = get_deferred_access_checks ();
19836
0a3b29ad 19837 /* Look for the `>'. */
c42e0e2d 19838 cp_parser_skip_to_end_of_template_parameter_list (parser);
0a3b29ad 19839 /* We just processed one more parameter list. */
19840 ++parser->num_template_parameter_lists;
19841 /* If the next token is `template', there are more template
19842 parameters. */
ccb84981 19843 if (cp_lexer_next_token_is_keyword (parser->lexer,
0a3b29ad 19844 RID_TEMPLATE))
19845 cp_parser_template_declaration_after_export (parser, member_p);
19846 else
19847 {
a8ff8672 19848 /* There are no access checks when parsing a template, as we do not
653e5405 19849 know if a specialization will be a friend. */
a8ff8672 19850 push_deferring_access_checks (dk_no_check);
ad9ae192 19851 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 19852 decl = cp_parser_single_declaration (parser,
23010bc8 19853 checks,
0a3b29ad 19854 member_p,
0c032b46 19855 /*explicit_specialization_p=*/false,
0a3b29ad 19856 &friend_p);
a8ff8672 19857 pop_deferring_access_checks ();
207355ad 19858
0a3b29ad 19859 /* If this is a member template declaration, let the front
19860 end know. */
19861 if (member_p && !friend_p && decl)
7e35473e 19862 {
19863 if (TREE_CODE (decl) == TYPE_DECL)
ad9ae192 19864 cp_parser_check_access_in_redeclaration (decl, token->location);
7e35473e 19865
19866 decl = finish_member_template_decl (decl);
19867 }
0a3b29ad 19868 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
b123b79d 19869 make_friend_class (current_class_type, TREE_TYPE (decl),
19870 /*complain=*/true);
0a3b29ad 19871 }
19872 /* We are done with the current parameter list. */
19873 --parser->num_template_parameter_lists;
19874
23010bc8 19875 pop_deferring_access_checks ();
19876
0a3b29ad 19877 /* Finish up. */
19878 finish_template_decl (parameter_list);
19879
19880 /* Register member declarations. */
19881 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19882 finish_member_declaration (decl);
9f25cdd8 19883 /* For the erroneous case of a template with C linkage, we pushed an
19884 implicit C++ linkage scope; exit that scope now. */
19885 if (need_lang_pop)
19886 pop_lang_context ();
0a3b29ad 19887 /* If DECL is a function template, we must return to parse it later.
19888 (Even though there is no definition, there might be default
19889 arguments that need handling.) */
ccb84981 19890 if (member_p && decl
0a3b29ad 19891 && (TREE_CODE (decl) == FUNCTION_DECL
19892 || DECL_FUNCTION_TEMPLATE_P (decl)))
9177da82 19893 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
0a3b29ad 19894}
19895
23010bc8 19896/* Perform the deferred access checks from a template-parameter-list.
19897 CHECKS is a TREE_LIST of access checks, as returned by
19898 get_deferred_access_checks. */
19899
19900static void
3369eb76 19901cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
23010bc8 19902{
19903 ++processing_template_parmlist;
19904 perform_access_checks (checks);
19905 --processing_template_parmlist;
19906}
19907
0a3b29ad 19908/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19909 `function-definition' sequence. MEMBER_P is true, this declaration
19910 appears in a class scope.
19911
19912 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19913 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19914
19915static tree
ccb84981 19916cp_parser_single_declaration (cp_parser* parser,
3369eb76 19917 VEC (deferred_access_check,gc)* checks,
45baea8b 19918 bool member_p,
0c032b46 19919 bool explicit_specialization_p,
45baea8b 19920 bool* friend_p)
0a3b29ad 19921{
8172be22 19922 int declares_class_or_enum;
0a3b29ad 19923 tree decl = NULL_TREE;
4b9b2871 19924 cp_decl_specifier_seq decl_specifiers;
92b128ed 19925 bool function_definition_p = false;
ad9ae192 19926 cp_token *decl_spec_token_start;
0a3b29ad 19927
2a03dcc3 19928 /* This function is only used when processing a template
19929 declaration. */
19930 gcc_assert (innermost_scope_kind () == sk_template_parms
19931 || innermost_scope_kind () == sk_template_spec);
19932
0a3b29ad 19933 /* Defer access checks until we know what is being declared. */
4f62c42e 19934 push_deferring_access_checks (dk_deferred);
9b57b06b 19935
0a3b29ad 19936 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19937 alternative. */
ad9ae192 19938 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 19939 cp_parser_decl_specifier_seq (parser,
19940 CP_PARSER_FLAGS_OPTIONAL,
19941 &decl_specifiers,
19942 &declares_class_or_enum);
92b128ed 19943 if (friend_p)
4b9b2871 19944 *friend_p = cp_parser_friend_p (&decl_specifiers);
2a03dcc3 19945
19946 /* There are no template typedefs. */
19947 if (decl_specifiers.specs[(int) ds_typedef])
19948 {
ccb59bb6 19949 error_at (decl_spec_token_start->location,
19950 "template declaration of %<typedef%>");
2a03dcc3 19951 decl = error_mark_node;
19952 }
19953
0a3b29ad 19954 /* Gather up the access checks that occurred the
19955 decl-specifier-seq. */
9b57b06b 19956 stop_deferring_access_checks ();
19957
0a3b29ad 19958 /* Check for the declaration of a template class. */
19959 if (declares_class_or_enum)
19960 {
19961 if (cp_parser_declares_only_class_p (parser))
19962 {
4b9b2871 19963 decl = shadow_tag (&decl_specifiers);
f95fba26 19964
19965 /* In this case:
19966
19967 struct C {
19968 friend template <typename T> struct A<T>::B;
19969 };
19970
19971 A<T>::B will be represented by a TYPENAME_TYPE, and
19972 therefore not recognized by shadow_tag. */
19973 if (friend_p && *friend_p
19974 && !decl
19975 && decl_specifiers.type
19976 && TYPE_P (decl_specifiers.type))
19977 decl = decl_specifiers.type;
19978
4b9b2871 19979 if (decl && decl != error_mark_node)
0a3b29ad 19980 decl = TYPE_NAME (decl);
19981 else
19982 decl = error_mark_node;
23010bc8 19983
19984 /* Perform access checks for template parameters. */
19985 cp_parser_perform_template_parameter_access_checks (checks);
0a3b29ad 19986 }
19987 }
67484828 19988
19989 /* Complain about missing 'typename' or other invalid type names. */
19990 if (!decl_specifiers.any_type_specifiers_p)
19991 cp_parser_parse_and_diagnose_invalid_type_name (parser);
19992
0a3b29ad 19993 /* If it's not a template class, try for a template function. If
19994 the next token is a `;', then this declaration does not declare
19995 anything. But, if there were errors in the decl-specifiers, then
19996 the error might well have come from an attempted class-specifier.
19997 In that case, there's no need to warn about a missing declarator. */
19998 if (!decl
19999 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
4b9b2871 20000 || decl_specifiers.type != error_mark_node))
0c032b46 20001 {
20002 decl = cp_parser_init_declarator (parser,
20003 &decl_specifiers,
20004 checks,
20005 /*function_definition_allowed_p=*/true,
20006 member_p,
20007 declares_class_or_enum,
20008 &function_definition_p);
20009
20010 /* 7.1.1-1 [dcl.stc]
20011
20012 A storage-class-specifier shall not be specified in an explicit
20013 specialization... */
20014 if (decl
20015 && explicit_specialization_p
20016 && decl_specifiers.storage_class != sc_none)
20017 {
ccb59bb6 20018 error_at (decl_spec_token_start->location,
20019 "explicit template specialization cannot have a storage class");
0c032b46 20020 decl = error_mark_node;
20021 }
20022 }
9b57b06b 20023
20024 pop_deferring_access_checks ();
20025
0a3b29ad 20026 /* Clear any current qualification; whatever comes next is the start
20027 of something new. */
20028 parser->scope = NULL_TREE;
20029 parser->qualifying_scope = NULL_TREE;
20030 parser->object_scope = NULL_TREE;
20031 /* Look for a trailing `;' after the declaration. */
92b128ed 20032 if (!function_definition_p
2a03dcc3 20033 && (decl == error_mark_node
c247dce0 20034 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
0a3b29ad 20035 cp_parser_skip_to_end_of_block_or_statement (parser);
0a3b29ad 20036
20037 return decl;
20038}
20039
a63bc44c 20040/* Parse a cast-expression that is not the operand of a unary "&". */
20041
20042static tree
20043cp_parser_simple_cast_expression (cp_parser *parser)
20044{
640aa28c 20045 return cp_parser_cast_expression (parser, /*address_p=*/false,
98b326fd 20046 /*cast_p=*/false, NULL);
a63bc44c 20047}
20048
0a3b29ad 20049/* Parse a functional cast to TYPE. Returns an expression
20050 representing the cast. */
20051
20052static tree
45baea8b 20053cp_parser_functional_cast (cp_parser* parser, tree type)
0a3b29ad 20054{
f352a3fb 20055 VEC(tree,gc) *vec;
0a3b29ad 20056 tree expression_list;
9ee4e816 20057 tree cast;
f82f1250 20058 bool nonconst_p;
20059
20060 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20061 {
bf8d19fe 20062 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 20063 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20064 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20065 if (TREE_CODE (type) == TYPE_DECL)
20066 type = TREE_TYPE (type);
20067 return finish_compound_literal (type, expression_list);
20068 }
0a3b29ad 20069
f352a3fb 20070
33199a81 20071 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 20072 /*cast_p=*/true,
20073 /*allow_expansion_p=*/true,
20074 /*non_constant_p=*/NULL);
20075 if (vec == NULL)
20076 expression_list = error_mark_node;
20077 else
20078 {
20079 expression_list = build_tree_list_vec (vec);
20080 release_tree_vector (vec);
20081 }
0a3b29ad 20082
ebd21de4 20083 cast = build_functional_cast (type, expression_list,
20084 tf_warning_or_error);
9ee4e816 20085 /* [expr.const]/1: In an integral constant expression "only type
20086 conversions to integral or enumeration type can be used". */
673b95fd 20087 if (TREE_CODE (type) == TYPE_DECL)
20088 type = TREE_TYPE (type);
bde9ebf7 20089 if (cast != error_mark_node
20090 && !cast_valid_in_integral_constant_expression_p (type)
c61e1212 20091 && cp_parser_non_integral_constant_expression (parser,
20092 NIC_CONSTRUCTOR))
bde9ebf7 20093 return error_mark_node;
9ee4e816 20094 return cast;
0a3b29ad 20095}
20096
92b128ed 20097/* Save the tokens that make up the body of a member function defined
20098 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20099 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20100 specifiers applied to the declaration. Returns the FUNCTION_DECL
20101 for the member function. */
20102
817adc77 20103static tree
92b128ed 20104cp_parser_save_member_function_body (cp_parser* parser,
4b9b2871 20105 cp_decl_specifier_seq *decl_specifiers,
3046c0a3 20106 cp_declarator *declarator,
92b128ed 20107 tree attributes)
20108{
00d26680 20109 cp_token *first;
20110 cp_token *last;
92b128ed 20111 tree fn;
20112
23a78a18 20113 /* Create the FUNCTION_DECL. */
20114 fn = grokmethod (decl_specifiers, declarator, attributes);
92b128ed 20115 /* If something went badly wrong, bail out now. */
20116 if (fn == error_mark_node)
20117 {
20118 /* If there's a function-body, skip it. */
ccb84981 20119 if (cp_parser_token_starts_function_definition_p
92b128ed 20120 (cp_lexer_peek_token (parser->lexer)))
20121 cp_parser_skip_to_end_of_block_or_statement (parser);
20122 return error_mark_node;
20123 }
20124
20125 /* Remember it, if there default args to post process. */
20126 cp_parser_save_default_args (parser, fn);
20127
ccb84981 20128 /* Save away the tokens that make up the body of the
92b128ed 20129 function. */
00d26680 20130 first = parser->lexer->next_token;
f82f1250 20131 /* We can have braced-init-list mem-initializers before the fn body. */
20132 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20133 {
20134 cp_lexer_consume_token (parser->lexer);
20135 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20136 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20137 {
20138 /* cache_group will stop after an un-nested { } pair, too. */
20139 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20140 break;
20141
20142 /* variadic mem-inits have ... after the ')'. */
20143 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20144 cp_lexer_consume_token (parser->lexer);
20145 }
20146 }
00d26680 20147 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
92b128ed 20148 /* Handle function try blocks. */
20149 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
00d26680 20150 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20151 last = parser->lexer->next_token;
92b128ed 20152
20153 /* Save away the inline definition; we will process it when the
20154 class is complete. */
00d26680 20155 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
92b128ed 20156 DECL_PENDING_INLINE_P (fn) = 1;
20157
20158 /* We need to know that this was defined in the class, so that
20159 friend templates are handled correctly. */
20160 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20161
92b128ed 20162 /* Add FN to the queue of functions to be parsed later. */
9177da82 20163 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
92b128ed 20164
20165 return fn;
20166}
20167
8534c3a3 20168/* Parse a template-argument-list, as well as the trailing ">" (but
20169 not the opening ">"). See cp_parser_template_argument_list for the
20170 return value. */
20171
20172static tree
20173cp_parser_enclosed_template_argument_list (cp_parser* parser)
20174{
20175 tree arguments;
20176 tree saved_scope;
20177 tree saved_qualifying_scope;
20178 tree saved_object_scope;
20179 bool saved_greater_than_is_operator_p;
48d94ede 20180 int saved_unevaluated_operand;
20181 int saved_inhibit_evaluation_warnings;
8534c3a3 20182
20183 /* [temp.names]
20184
20185 When parsing a template-id, the first non-nested `>' is taken as
20186 the end of the template-argument-list rather than a greater-than
20187 operator. */
ccb84981 20188 saved_greater_than_is_operator_p
8534c3a3 20189 = parser->greater_than_is_operator_p;
20190 parser->greater_than_is_operator_p = false;
20191 /* Parsing the argument list may modify SCOPE, so we save it
20192 here. */
20193 saved_scope = parser->scope;
20194 saved_qualifying_scope = parser->qualifying_scope;
20195 saved_object_scope = parser->object_scope;
ef792945 20196 /* We need to evaluate the template arguments, even though this
20197 template-id may be nested within a "sizeof". */
48d94ede 20198 saved_unevaluated_operand = cp_unevaluated_operand;
20199 cp_unevaluated_operand = 0;
20200 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20201 c_inhibit_evaluation_warnings = 0;
8534c3a3 20202 /* Parse the template-argument-list itself. */
56471494 20203 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20204 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8534c3a3 20205 arguments = NULL_TREE;
20206 else
20207 arguments = cp_parser_template_argument_list (parser);
bece9ea1 20208 /* Look for the `>' that ends the template-argument-list. If we find
20209 a '>>' instead, it's probably just a typo. */
20210 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20211 {
6dcdb5de 20212 if (cxx_dialect != cxx98)
56471494 20213 {
20214 /* In C++0x, a `>>' in a template argument list or cast
20215 expression is considered to be two separate `>'
20216 tokens. So, change the current token to a `>', but don't
20217 consume it: it will be consumed later when the outer
20218 template argument list (or cast expression) is parsed.
20219 Note that this replacement of `>' for `>>' is necessary
20220 even if we are parsing tentatively: in the tentative
20221 case, after calling
20222 cp_parser_enclosed_template_argument_list we will always
20223 throw away all of the template arguments and the first
20224 closing `>', either because the template argument list
20225 was erroneous or because we are replacing those tokens
20226 with a CPP_TEMPLATE_ID token. The second `>' (which will
20227 not have been thrown away) is needed either to close an
20228 outer template argument list or to complete a new-style
20229 cast. */
20230 cp_token *token = cp_lexer_peek_token (parser->lexer);
20231 token->type = CPP_GREATER;
20232 }
20233 else if (!saved_greater_than_is_operator_p)
bece9ea1 20234 {
b9dd3954 20235 /* If we're in a nested template argument list, the '>>' has
20236 to be a typo for '> >'. We emit the error message, but we
20237 continue parsing and we push a '>' as next token, so that
20238 the argument list will be parsed correctly. Note that the
20239 global source location is still on the token before the
20240 '>>', so we need to say explicitly where we want it. */
20241 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 20242 error_at (token->location, "%<>>%> should be %<> >%> "
20243 "within a nested template argument list");
b9dd3954 20244
bece9ea1 20245 token->type = CPP_GREATER;
20246 }
20247 else
20248 {
b9dd3954 20249 /* If this is not a nested template argument list, the '>>'
20250 is a typo for '>'. Emit an error message and continue.
20251 Same deal about the token location, but here we can get it
20252 right by consuming the '>>' before issuing the diagnostic. */
ad9ae192 20253 cp_token *token = cp_lexer_consume_token (parser->lexer);
ccb59bb6 20254 error_at (token->location,
20255 "spurious %<>>%>, use %<>%> to terminate "
20256 "a template argument list");
bece9ea1 20257 }
20258 }
b9dd3954 20259 else
c42e0e2d 20260 cp_parser_skip_to_end_of_template_parameter_list (parser);
8534c3a3 20261 /* The `>' token might be a greater-than operator again now. */
ccb84981 20262 parser->greater_than_is_operator_p
8534c3a3 20263 = saved_greater_than_is_operator_p;
20264 /* Restore the SAVED_SCOPE. */
20265 parser->scope = saved_scope;
20266 parser->qualifying_scope = saved_qualifying_scope;
20267 parser->object_scope = saved_object_scope;
48d94ede 20268 cp_unevaluated_operand = saved_unevaluated_operand;
20269 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8534c3a3 20270
20271 return arguments;
20272}
20273
0a3b29ad 20274/* MEMBER_FUNCTION is a member function, or a friend. If default
20275 arguments, or the body of the function have not yet been parsed,
20276 parse them now. */
20277
20278static void
45baea8b 20279cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
0a3b29ad 20280{
0a3b29ad 20281 /* If this member is a template, get the underlying
20282 FUNCTION_DECL. */
20283 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20284 member_function = DECL_TEMPLATE_RESULT (member_function);
20285
20286 /* There should not be any class definitions in progress at this
20287 point; the bodies of members are only parsed outside of all class
20288 definitions. */
b4df430b 20289 gcc_assert (parser->num_classes_being_defined == 0);
0a3b29ad 20290 /* While we're parsing the member functions we might encounter more
20291 classes. We want to handle them right away, but we don't want
20292 them getting mixed up with functions that are currently in the
20293 queue. */
9177da82 20294 push_unparsed_function_queues (parser);
0a3b29ad 20295
20296 /* Make sure that any template parameters are in scope. */
20297 maybe_begin_member_template_processing (member_function);
20298
0a3b29ad 20299 /* If the body of the function has not yet been parsed, parse it
20300 now. */
20301 if (DECL_PENDING_INLINE_P (member_function))
20302 {
20303 tree function_scope;
20304 cp_token_cache *tokens;
20305
20306 /* The function is no longer pending; we are processing it. */
20307 tokens = DECL_PENDING_INLINE_INFO (member_function);
20308 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20309 DECL_PENDING_INLINE_P (member_function) = 0;
9031d10b 20310
7ff3aeed 20311 /* If this is a local class, enter the scope of the containing
20312 function. */
20313 function_scope = current_function_decl;
0a3b29ad 20314 if (function_scope)
d2764e2d 20315 push_function_context ();
9031d10b 20316
b9dd3954 20317 /* Push the body of the function onto the lexer stack. */
20318 cp_parser_push_lexer_for_tokens (parser, tokens);
ccb84981 20319
0a3b29ad 20320 /* Let the front end know that we going to be defining this
20321 function. */
3046c0a3 20322 start_preparsed_function (member_function, NULL_TREE,
20323 SF_PRE_PARSED | SF_INCLASS_INLINE);
ccb84981 20324
69ebef96 20325 /* Don't do access checking if it is a templated function. */
20326 if (processing_template_decl)
20327 push_deferring_access_checks (dk_no_check);
9031d10b 20328
0a3b29ad 20329 /* Now, parse the body of the function. */
20330 cp_parser_function_definition_after_declarator (parser,
20331 /*inline_p=*/true);
ccb84981 20332
69ebef96 20333 if (processing_template_decl)
20334 pop_deferring_access_checks ();
9031d10b 20335
0a3b29ad 20336 /* Leave the scope of the containing function. */
20337 if (function_scope)
d2764e2d 20338 pop_function_context ();
b9dd3954 20339 cp_parser_pop_lexer (parser);
0a3b29ad 20340 }
20341
20342 /* Remove any template parameters from the symbol table. */
20343 maybe_end_member_template_processing ();
20344
20345 /* Restore the queue. */
9177da82 20346 pop_unparsed_function_queues (parser);
0a3b29ad 20347}
20348
63eff20d 20349/* If DECL contains any default args, remember it on the unparsed
69b6679c 20350 functions queue. */
20351
20352static void
20353cp_parser_save_default_args (cp_parser* parser, tree decl)
20354{
20355 tree probe;
20356
20357 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20358 probe;
20359 probe = TREE_CHAIN (probe))
20360 if (TREE_PURPOSE (probe))
20361 {
9177da82 20362 cp_default_arg_entry *entry
20363 = VEC_safe_push (cp_default_arg_entry, gc,
20364 unparsed_funs_with_default_args, NULL);
20365 entry->class_type = current_class_type;
20366 entry->decl = decl;
69b6679c 20367 break;
20368 }
69b6679c 20369}
20370
af128372 20371/* FN is a FUNCTION_DECL which may contains a parameter with an
93c149df 20372 unparsed DEFAULT_ARG. Parse the default args now. This function
20373 assumes that the current scope is the scope in which the default
20374 argument should be processed. */
0a3b29ad 20375
20376static void
af128372 20377cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
0a3b29ad 20378{
0a3b29ad 20379 bool saved_local_variables_forbidden_p;
a8b75081 20380 tree parm, parmdecl;
af128372 20381
e6021728 20382 /* While we're parsing the default args, we might (due to the
20383 statement expression extension) encounter more classes. We want
20384 to handle them right away, but we don't want them getting mixed
20385 up with default args that are currently in the queue. */
9177da82 20386 push_unparsed_function_queues (parser);
e6021728 20387
b9dd3954 20388 /* Local variable names (and the `this' keyword) may not appear
20389 in a default argument. */
20390 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20391 parser->local_variables_forbidden_p = true;
20392
a8b75081 20393 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20394 parmdecl = DECL_ARGUMENTS (fn);
20395 parm && parm != void_list_node;
20396 parm = TREE_CHAIN (parm),
1767a056 20397 parmdecl = DECL_CHAIN (parmdecl))
0a3b29ad 20398 {
b9dd3954 20399 cp_token_cache *tokens;
29081c08 20400 tree default_arg = TREE_PURPOSE (parm);
20401 tree parsed_arg;
f51f5e0b 20402 VEC(tree,gc) *insts;
20403 tree copy;
20404 unsigned ix;
9031d10b 20405
29081c08 20406 if (!default_arg)
b9dd3954 20407 continue;
0a3b29ad 20408
f6219e82 20409 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20410 /* This can happen for a friend declaration for a function
20411 already declared with default arguments. */
20412 continue;
29081c08 20413
b9dd3954 20414 /* Push the saved tokens for the default argument onto the parser's
20415 lexer stack. */
29081c08 20416 tokens = DEFARG_TOKENS (default_arg);
b9dd3954 20417 cp_parser_push_lexer_for_tokens (parser, tokens);
0a3b29ad 20418
a8b75081 20419 start_lambda_scope (parmdecl);
20420
b9dd3954 20421 /* Parse the assignment-expression. */
98b326fd 20422 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
027f3cbf 20423 if (parsed_arg == error_mark_node)
20424 {
20425 cp_parser_pop_lexer (parser);
20426 continue;
20427 }
29081c08 20428
6d5a06c3 20429 if (!processing_template_decl)
20430 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
074ab442 20431
29081c08 20432 TREE_PURPOSE (parm) = parsed_arg;
20433
20434 /* Update any instantiations we've already created. */
f51f5e0b 20435 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20436 VEC_iterate (tree, insts, ix, copy); ix++)
20437 TREE_PURPOSE (copy) = parsed_arg;
0a3b29ad 20438
a8b75081 20439 finish_lambda_scope ();
20440
3ed12242 20441 /* If the token stream has not been completely used up, then
20442 there was extra junk after the end of the default
20443 argument. */
20444 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
a2c5b975 20445 cp_parser_error (parser, "expected %<,%>");
3ed12242 20446
b9dd3954 20447 /* Revert to the main lexer. */
20448 cp_parser_pop_lexer (parser);
0a3b29ad 20449 }
e6021728 20450
917e3348 20451 /* Make sure no default arg is missing. */
20452 check_default_args (fn);
20453
b9dd3954 20454 /* Restore the state of local_variables_forbidden_p. */
20455 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20456
e6021728 20457 /* Restore the queue. */
9177da82 20458 pop_unparsed_function_queues (parser);
0a3b29ad 20459}
20460
20461/* Parse the operand of `sizeof' (or a similar operator). Returns
20462 either a TYPE or an expression, depending on the form of the
20463 input. The KEYWORD indicates which kind of expression we have
20464 encountered. */
20465
20466static tree
45baea8b 20467cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
0a3b29ad 20468{
0a3b29ad 20469 tree expr = NULL_TREE;
20470 const char *saved_message;
dea3189b 20471 char *tmp;
f47c1747 20472 bool saved_integral_constant_expression_p;
640aa28c 20473 bool saved_non_integral_constant_expression_p;
d95d815d 20474 bool pack_expansion_p = false;
0a3b29ad 20475
0a3b29ad 20476 /* Types cannot be defined in a `sizeof' expression. Save away the
20477 old message. */
20478 saved_message = parser->type_definition_forbidden_message;
20479 /* And create the new one. */
2e52ac87 20480 tmp = concat ("types may not be defined in %<",
20481 IDENTIFIER_POINTER (ridpointers[keyword]),
20482 "%> expressions", NULL);
20483 parser->type_definition_forbidden_message = tmp;
0a3b29ad 20484
20485 /* The restrictions on constant-expressions do not apply inside
20486 sizeof expressions. */
9031d10b 20487 saved_integral_constant_expression_p
640aa28c 20488 = parser->integral_constant_expression_p;
20489 saved_non_integral_constant_expression_p
20490 = parser->non_integral_constant_expression_p;
f47c1747 20491 parser->integral_constant_expression_p = false;
0a3b29ad 20492
d95d815d 20493 /* If it's a `...', then we are computing the length of a parameter
20494 pack. */
20495 if (keyword == RID_SIZEOF
20496 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20497 {
20498 /* Consume the `...'. */
20499 cp_lexer_consume_token (parser->lexer);
20500 maybe_warn_variadic_templates ();
20501
20502 /* Note that this is an expansion. */
20503 pack_expansion_p = true;
20504 }
20505
4c99a080 20506 /* Do not actually evaluate the expression. */
48d94ede 20507 ++cp_unevaluated_operand;
20508 ++c_inhibit_evaluation_warnings;
0a3b29ad 20509 /* If it's a `(', then we might be looking at the type-id
20510 construction. */
20511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20512 {
20513 tree type;
41f2d08e 20514 bool saved_in_type_id_in_expr_p;
0a3b29ad 20515
20516 /* We can't be sure yet whether we're looking at a type-id or an
20517 expression. */
20518 cp_parser_parse_tentatively (parser);
20519 /* Consume the `('. */
20520 cp_lexer_consume_token (parser->lexer);
20521 /* Parse the type-id. */
41f2d08e 20522 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20523 parser->in_type_id_in_expr_p = true;
0a3b29ad 20524 type = cp_parser_type_id (parser);
41f2d08e 20525 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 20526 /* Now, look for the trailing `)'. */
c247dce0 20527 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 20528 /* If all went well, then we're done. */
20529 if (cp_parser_parse_definitely (parser))
20530 {
4b9b2871 20531 cp_decl_specifier_seq decl_specs;
20532
20533 /* Build a trivial decl-specifier-seq. */
20534 clear_decl_specs (&decl_specs);
20535 decl_specs.type = type;
0a3b29ad 20536
20537 /* Call grokdeclarator to figure out what type this is. */
3046c0a3 20538 expr = grokdeclarator (NULL,
4b9b2871 20539 &decl_specs,
0a3b29ad 20540 TYPENAME,
20541 /*initialized=*/0,
20542 /*attrlist=*/NULL);
20543 }
20544 }
20545
20546 /* If the type-id production did not work out, then we must be
20547 looking at the unary-expression production. */
20548 if (!expr)
640aa28c 20549 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 20550 /*cast_p=*/false, NULL);
d95d815d 20551
20552 if (pack_expansion_p)
20553 /* Build a pack expansion. */
20554 expr = make_pack_expansion (expr);
20555
4c99a080 20556 /* Go back to evaluating expressions. */
48d94ede 20557 --cp_unevaluated_operand;
20558 --c_inhibit_evaluation_warnings;
0a3b29ad 20559
20560 /* Free the message we created. */
dea3189b 20561 free (tmp);
0a3b29ad 20562 /* And restore the old one. */
20563 parser->type_definition_forbidden_message = saved_message;
9031d10b 20564 parser->integral_constant_expression_p
640aa28c 20565 = saved_integral_constant_expression_p;
20566 parser->non_integral_constant_expression_p
20567 = saved_non_integral_constant_expression_p;
0a3b29ad 20568
20569 return expr;
20570}
20571
20572/* If the current declaration has no declarator, return true. */
20573
20574static bool
20575cp_parser_declares_only_class_p (cp_parser *parser)
20576{
ccb84981 20577 /* If the next token is a `;' or a `,' then there is no
0a3b29ad 20578 declarator. */
20579 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20580 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20581}
20582
78e0edfb 20583/* Update the DECL_SPECS to reflect the storage class indicated by
20584 KEYWORD. */
0a3b29ad 20585
4b9b2871 20586static void
78e0edfb 20587cp_parser_set_storage_class (cp_parser *parser,
20588 cp_decl_specifier_seq *decl_specs,
ad9ae192 20589 enum rid keyword,
20590 location_t location)
0a3b29ad 20591{
78e0edfb 20592 cp_storage_class storage_class;
20593
20594 if (parser->in_unbraced_linkage_specification_p)
20595 {
ccb59bb6 20596 error_at (location, "invalid use of %qD in linkage specification",
20597 ridpointers[keyword]);
78e0edfb 20598 return;
20599 }
20600 else if (decl_specs->storage_class != sc_none)
20601 {
ceec99b9 20602 decl_specs->conflicting_specifiers_p = true;
78e0edfb 20603 return;
20604 }
20605
20606 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20607 && decl_specs->specs[(int) ds_thread])
20608 {
ccb59bb6 20609 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
78e0edfb 20610 decl_specs->specs[(int) ds_thread] = 0;
20611 }
20612
074ab442 20613 switch (keyword)
78e0edfb 20614 {
20615 case RID_AUTO:
20616 storage_class = sc_auto;
20617 break;
20618 case RID_REGISTER:
20619 storage_class = sc_register;
20620 break;
20621 case RID_STATIC:
20622 storage_class = sc_static;
20623 break;
20624 case RID_EXTERN:
20625 storage_class = sc_extern;
20626 break;
20627 case RID_MUTABLE:
20628 storage_class = sc_mutable;
20629 break;
20630 default:
20631 gcc_unreachable ();
20632 }
20633 decl_specs->storage_class = storage_class;
ceec99b9 20634
20635 /* A storage class specifier cannot be applied alongside a typedef
20636 specifier. If there is a typedef specifier present then set
20637 conflicting_specifiers_p which will trigger an error later
20638 on in grokdeclarator. */
20639 if (decl_specs->specs[(int)ds_typedef])
20640 decl_specs->conflicting_specifiers_p = true;
4b9b2871 20641}
20642
20643/* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20644 is true, the type is a user-defined type; otherwise it is a
20645 built-in type specified by a keyword. */
0a3b29ad 20646
4b9b2871 20647static void
20648cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20649 tree type_spec,
eef0ab03 20650 location_t location,
4b9b2871 20651 bool user_defined_p)
20652{
20653 decl_specs->any_specifiers_p = true;
207355ad 20654
924bbf02 20655 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20656 (with, for example, in "typedef int wchar_t;") we remember that
20657 this is what happened. In system headers, we ignore these
20658 declarations so that G++ can work with system headers that are not
20659 C++-safe. */
207355ad 20660 if (decl_specs->specs[(int) ds_typedef]
dd9b4af4 20661 && !user_defined_p
263df831 20662 && (type_spec == boolean_type_node
924bbf02 20663 || type_spec == char16_type_node
20664 || type_spec == char32_type_node
263df831 20665 || type_spec == wchar_type_node)
dd9b4af4 20666 && (decl_specs->type
20667 || decl_specs->specs[(int) ds_long]
20668 || decl_specs->specs[(int) ds_short]
20669 || decl_specs->specs[(int) ds_unsigned]
20670 || decl_specs->specs[(int) ds_signed]))
fc1ad922 20671 {
20672 decl_specs->redefined_builtin_type = type_spec;
20673 if (!decl_specs->type)
20674 {
20675 decl_specs->type = type_spec;
20676 decl_specs->user_defined_type_p = false;
eef0ab03 20677 decl_specs->type_location = location;
fc1ad922 20678 }
20679 }
dd9b4af4 20680 else if (decl_specs->type)
20681 decl_specs->multiple_types_p = true;
4b9b2871 20682 else
20683 {
20684 decl_specs->type = type_spec;
20685 decl_specs->user_defined_type_p = user_defined_p;
fc1ad922 20686 decl_specs->redefined_builtin_type = NULL_TREE;
eef0ab03 20687 decl_specs->type_location = location;
0a3b29ad 20688 }
4b9b2871 20689}
0a3b29ad 20690
4b9b2871 20691/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20692 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20693
20694static bool
20695cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20696{
20697 return decl_specifiers->specs[(int) ds_friend] != 0;
0a3b29ad 20698}
20699
c247dce0 20700/* Issue an error message indicating that TOKEN_DESC was expected.
20701 If KEYWORD is true, it indicated this function is called by
20702 cp_parser_require_keword and the required token can only be
20703 a indicated keyword. */
20704
20705static void
20706cp_parser_required_error (cp_parser *parser,
20707 required_token token_desc,
20708 bool keyword)
20709{
20710 switch (token_desc)
20711 {
20712 case RT_NEW:
20713 cp_parser_error (parser, "expected %<new%>");
20714 return;
20715 case RT_DELETE:
20716 cp_parser_error (parser, "expected %<delete%>");
20717 return;
20718 case RT_RETURN:
20719 cp_parser_error (parser, "expected %<return%>");
20720 return;
20721 case RT_WHILE:
20722 cp_parser_error (parser, "expected %<while%>");
20723 return;
20724 case RT_EXTERN:
20725 cp_parser_error (parser, "expected %<extern%>");
20726 return;
20727 case RT_STATIC_ASSERT:
20728 cp_parser_error (parser, "expected %<static_assert%>");
20729 return;
20730 case RT_DECLTYPE:
20731 cp_parser_error (parser, "expected %<decltype%>");
20732 return;
20733 case RT_OPERATOR:
20734 cp_parser_error (parser, "expected %<operator%>");
20735 return;
20736 case RT_CLASS:
20737 cp_parser_error (parser, "expected %<class%>");
20738 return;
20739 case RT_TEMPLATE:
20740 cp_parser_error (parser, "expected %<template%>");
20741 return;
20742 case RT_NAMESPACE:
20743 cp_parser_error (parser, "expected %<namespace%>");
20744 return;
20745 case RT_USING:
20746 cp_parser_error (parser, "expected %<using%>");
20747 return;
20748 case RT_ASM:
20749 cp_parser_error (parser, "expected %<asm%>");
20750 return;
20751 case RT_TRY:
20752 cp_parser_error (parser, "expected %<try%>");
20753 return;
20754 case RT_CATCH:
20755 cp_parser_error (parser, "expected %<catch%>");
20756 return;
20757 case RT_THROW:
20758 cp_parser_error (parser, "expected %<throw%>");
20759 return;
20760 case RT_LABEL:
20761 cp_parser_error (parser, "expected %<__label__%>");
20762 return;
20763 case RT_AT_TRY:
20764 cp_parser_error (parser, "expected %<@try%>");
20765 return;
20766 case RT_AT_SYNCHRONIZED:
20767 cp_parser_error (parser, "expected %<@synchronized%>");
20768 return;
20769 case RT_AT_THROW:
20770 cp_parser_error (parser, "expected %<@throw%>");
20771 return;
20772 default:
20773 break;
20774 }
20775 if (!keyword)
20776 {
20777 switch (token_desc)
20778 {
20779 case RT_SEMICOLON:
20780 cp_parser_error (parser, "expected %<;%>");
20781 return;
20782 case RT_OPEN_PAREN:
20783 cp_parser_error (parser, "expected %<(%>");
20784 return;
20785 case RT_CLOSE_BRACE:
20786 cp_parser_error (parser, "expected %<}%>");
20787 return;
20788 case RT_OPEN_BRACE:
20789 cp_parser_error (parser, "expected %<{%>");
20790 return;
20791 case RT_CLOSE_SQUARE:
20792 cp_parser_error (parser, "expected %<]%>");
20793 return;
20794 case RT_OPEN_SQUARE:
20795 cp_parser_error (parser, "expected %<[%>");
20796 return;
20797 case RT_COMMA:
20798 cp_parser_error (parser, "expected %<,%>");
20799 return;
20800 case RT_SCOPE:
20801 cp_parser_error (parser, "expected %<::%>");
20802 return;
20803 case RT_LESS:
20804 cp_parser_error (parser, "expected %<<%>");
20805 return;
20806 case RT_GREATER:
20807 cp_parser_error (parser, "expected %<>%>");
20808 return;
20809 case RT_EQ:
20810 cp_parser_error (parser, "expected %<=%>");
20811 return;
20812 case RT_ELLIPSIS:
20813 cp_parser_error (parser, "expected %<...%>");
20814 return;
20815 case RT_MULT:
20816 cp_parser_error (parser, "expected %<*%>");
20817 return;
20818 case RT_COMPL:
20819 cp_parser_error (parser, "expected %<~%>");
20820 return;
20821 case RT_COLON:
20822 cp_parser_error (parser, "expected %<:%>");
20823 return;
20824 case RT_COLON_SCOPE:
20825 cp_parser_error (parser, "expected %<:%> or %<::%>");
20826 return;
20827 case RT_CLOSE_PAREN:
20828 cp_parser_error (parser, "expected %<)%>");
20829 return;
20830 case RT_COMMA_CLOSE_PAREN:
20831 cp_parser_error (parser, "expected %<,%> or %<)%>");
20832 return;
20833 case RT_PRAGMA_EOL:
20834 cp_parser_error (parser, "expected end of line");
20835 return;
20836 case RT_NAME:
20837 cp_parser_error (parser, "expected identifier");
20838 return;
20839 case RT_SELECT:
20840 cp_parser_error (parser, "expected selection-statement");
20841 return;
20842 case RT_INTERATION:
20843 cp_parser_error (parser, "expected iteration-statement");
20844 return;
20845 case RT_JUMP:
20846 cp_parser_error (parser, "expected jump-statement");
20847 return;
20848 case RT_CLASS_KEY:
20849 cp_parser_error (parser, "expected class-key");
20850 return;
20851 case RT_CLASS_TYPENAME_TEMPLATE:
20852 cp_parser_error (parser,
20853 "expected %<class%>, %<typename%>, or %<template%>");
20854 return;
20855 default:
20856 gcc_unreachable ();
20857 }
20858 }
20859 else
20860 gcc_unreachable ();
20861}
20862
20863
20864
0a3b29ad 20865/* If the next token is of the indicated TYPE, consume it. Otherwise,
20866 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 20867
0a3b29ad 20868 Returns the token consumed, if the token had the appropriate type.
20869 Otherwise, returns NULL. */
20870
20871static cp_token *
45baea8b 20872cp_parser_require (cp_parser* parser,
653e5405 20873 enum cpp_ttype type,
c247dce0 20874 required_token token_desc)
0a3b29ad 20875{
20876 if (cp_lexer_next_token_is (parser->lexer, type))
20877 return cp_lexer_consume_token (parser->lexer);
20878 else
20879 {
2c593bd0 20880 /* Output the MESSAGE -- unless we're parsing tentatively. */
20881 if (!cp_parser_simulate_error (parser))
c247dce0 20882 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
0a3b29ad 20883 return NULL;
20884 }
20885}
20886
c42e0e2d 20887/* An error message is produced if the next token is not '>'.
20888 All further tokens are skipped until the desired token is
20889 found or '{', '}', ';' or an unbalanced ')' or ']'. */
0a3b29ad 20890
20891static void
c42e0e2d 20892cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
0a3b29ad 20893{
c42e0e2d 20894 /* Current level of '< ... >'. */
20895 unsigned level = 0;
20896 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
0a3b29ad 20897 unsigned nesting_depth = 0;
20898
c42e0e2d 20899 /* Are we ready, yet? If not, issue error message. */
c247dce0 20900 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
0a3b29ad 20901 return;
20902
20903 /* Skip tokens until the desired token is found. */
20904 while (true)
20905 {
20906 /* Peek at the next token. */
c42e0e2d 20907 switch (cp_lexer_peek_token (parser->lexer)->type)
0a3b29ad 20908 {
c42e0e2d 20909 case CPP_LESS:
20910 if (!nesting_depth)
20911 ++level;
20912 break;
b75b98aa 20913
56471494 20914 case CPP_RSHIFT:
6dcdb5de 20915 if (cxx_dialect == cxx98)
56471494 20916 /* C++0x views the `>>' operator as two `>' tokens, but
20917 C++98 does not. */
20918 break;
20919 else if (!nesting_depth && level-- == 0)
20920 {
20921 /* We've hit a `>>' where the first `>' closes the
20922 template argument list, and the second `>' is
20923 spurious. Just consume the `>>' and stop; we've
20924 already produced at least one error. */
20925 cp_lexer_consume_token (parser->lexer);
20926 return;
20927 }
20928 /* Fall through for C++0x, so we handle the second `>' in
20929 the `>>'. */
20930
c42e0e2d 20931 case CPP_GREATER:
20932 if (!nesting_depth && level-- == 0)
20933 {
20934 /* We've reached the token we want, consume it and stop. */
20935 cp_lexer_consume_token (parser->lexer);
20936 return;
20937 }
20938 break;
b75b98aa 20939
b75b98aa 20940 case CPP_OPEN_PAREN:
20941 case CPP_OPEN_SQUARE:
20942 ++nesting_depth;
20943 break;
20944
b75b98aa 20945 case CPP_CLOSE_PAREN:
20946 case CPP_CLOSE_SQUARE:
0a3b29ad 20947 if (nesting_depth-- == 0)
20948 return;
b75b98aa 20949 break;
20950
c42e0e2d 20951 case CPP_EOF:
20952 case CPP_PRAGMA_EOL:
20953 case CPP_SEMICOLON:
20954 case CPP_OPEN_BRACE:
20955 case CPP_CLOSE_BRACE:
20956 /* The '>' was probably forgotten, don't look further. */
20957 return;
20958
b75b98aa 20959 default:
20960 break;
0a3b29ad 20961 }
b75b98aa 20962
0a3b29ad 20963 /* Consume this token. */
20964 cp_lexer_consume_token (parser->lexer);
20965 }
20966}
20967
20968/* If the next token is the indicated keyword, consume it. Otherwise,
20969 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 20970
0a3b29ad 20971 Returns the token consumed, if the token had the appropriate type.
20972 Otherwise, returns NULL. */
20973
20974static cp_token *
45baea8b 20975cp_parser_require_keyword (cp_parser* parser,
653e5405 20976 enum rid keyword,
c247dce0 20977 required_token token_desc)
0a3b29ad 20978{
20979 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
20980
20981 if (token && token->keyword != keyword)
20982 {
c247dce0 20983 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
0a3b29ad 20984 return NULL;
20985 }
20986
20987 return token;
20988}
20989
20990/* Returns TRUE iff TOKEN is a token that can begin the body of a
20991 function-definition. */
20992
ccb84981 20993static bool
45baea8b 20994cp_parser_token_starts_function_definition_p (cp_token* token)
0a3b29ad 20995{
20996 return (/* An ordinary function-body begins with an `{'. */
20997 token->type == CPP_OPEN_BRACE
20998 /* A ctor-initializer begins with a `:'. */
20999 || token->type == CPP_COLON
21000 /* A function-try-block begins with `try'. */
21001 || token->keyword == RID_TRY
21002 /* The named return value extension begins with `return'. */
21003 || token->keyword == RID_RETURN);
21004}
21005
21006/* Returns TRUE iff the next token is the ":" or "{" beginning a class
21007 definition. */
21008
21009static bool
21010cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21011{
21012 cp_token *token;
21013
21014 token = cp_lexer_peek_token (parser->lexer);
21015 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21016}
21017
56471494 21018/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21019 C++0x) ending a template-argument. */
13795292 21020
21021static bool
21022cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21023{
21024 cp_token *token;
21025
21026 token = cp_lexer_peek_token (parser->lexer);
d95d815d 21027 return (token->type == CPP_COMMA
21028 || token->type == CPP_GREATER
56471494 21029 || token->type == CPP_ELLIPSIS
6dcdb5de 21030 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
13795292 21031}
c8d5ab79 21032
945b33d4 21033/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
c8d5ab79 21034 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21035
21036static bool
ccb84981 21037cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
c8d5ab79 21038 size_t n)
21039{
21040 cp_token *token;
21041
21042 token = cp_lexer_peek_nth_token (parser->lexer, n);
21043 if (token->type == CPP_LESS)
21044 return true;
21045 /* Check for the sequence `<::' in the original code. It would be lexed as
21046 `[:', where `[' is a digraph, and there is no whitespace before
21047 `:'. */
21048 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21049 {
21050 cp_token *token2;
21051 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21052 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21053 return true;
21054 }
21055 return false;
21056}
ccb84981 21057
0a3b29ad 21058/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21059 or none_type otherwise. */
21060
21061static enum tag_types
45baea8b 21062cp_parser_token_is_class_key (cp_token* token)
0a3b29ad 21063{
21064 switch (token->keyword)
21065 {
21066 case RID_CLASS:
21067 return class_type;
21068 case RID_STRUCT:
21069 return record_type;
21070 case RID_UNION:
21071 return union_type;
ccb84981 21072
0a3b29ad 21073 default:
21074 return none_type;
21075 }
21076}
21077
21078/* Issue an error message if the CLASS_KEY does not match the TYPE. */
21079
21080static void
21081cp_parser_check_class_key (enum tag_types class_key, tree type)
21082{
21083 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
2b9e3597 21084 permerror (input_location, "%qs tag used in naming %q#T",
0a3b29ad 21085 class_key == union_type ? "union"
ccb84981 21086 : class_key == record_type ? "struct" : "class",
0a3b29ad 21087 type);
21088}
ccb84981 21089
63eff20d 21090/* Issue an error message if DECL is redeclared with different
7e35473e 21091 access than its original declaration [class.access.spec/3].
21092 This applies to nested classes and nested class templates.
21093 [class.mem/1]. */
21094
a2c5b975 21095static void
ad9ae192 21096cp_parser_check_access_in_redeclaration (tree decl, location_t location)
7e35473e 21097{
a7905afa 21098 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
7e35473e 21099 return;
21100
21101 if ((TREE_PRIVATE (decl)
21102 != (current_access_specifier == access_private_node))
21103 || (TREE_PROTECTED (decl)
21104 != (current_access_specifier == access_protected_node)))
ccb59bb6 21105 error_at (location, "%qD redeclared with different access", decl);
7e35473e 21106}
21107
0a3b29ad 21108/* Look for the `template' keyword, as a syntactic disambiguator.
ccb84981 21109 Return TRUE iff it is present, in which case it will be
0a3b29ad 21110 consumed. */
21111
21112static bool
21113cp_parser_optional_template_keyword (cp_parser *parser)
21114{
21115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21116 {
21117 /* The `template' keyword can only be used within templates;
21118 outside templates the parser can always figure out what is a
21119 template and what is not. */
21120 if (!processing_template_decl)
21121 {
ad9ae192 21122 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 21123 error_at (token->location,
21124 "%<template%> (as a disambiguator) is only allowed "
21125 "within templates");
0a3b29ad 21126 /* If this part of the token stream is rescanned, the same
21127 error message would be generated. So, we purge the token
21128 from the stream. */
21129 cp_lexer_purge_token (parser->lexer);
21130 return false;
21131 }
21132 else
21133 {
21134 /* Consume the `template' keyword. */
21135 cp_lexer_consume_token (parser->lexer);
21136 return true;
21137 }
21138 }
21139
21140 return false;
21141}
21142
b3c48b5d 21143/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21144 set PARSER->SCOPE, and perform other related actions. */
21145
21146static void
21147cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21148{
3369eb76 21149 int i;
21150 struct tree_check *check_value;
21151 deferred_access_check *chk;
21152 VEC (deferred_access_check,gc) *checks;
b3c48b5d 21153
21154 /* Get the stored value. */
3369eb76 21155 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
b3c48b5d 21156 /* Perform any access checks that were deferred. */
3369eb76 21157 checks = check_value->checks;
21158 if (checks)
21159 {
48148244 21160 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21161 perform_or_defer_access_check (chk->binfo,
21162 chk->decl,
21163 chk->diag_decl);
3369eb76 21164 }
b3c48b5d 21165 /* Set the scope from the stored value. */
3369eb76 21166 parser->scope = check_value->value;
21167 parser->qualifying_scope = check_value->qualifying_scope;
b3c48b5d 21168 parser->object_scope = NULL_TREE;
21169}
21170
f82f1250 21171/* Consume tokens up through a non-nested END token. Returns TRUE if we
21172 encounter the end of a block before what we were looking for. */
0a3b29ad 21173
f82f1250 21174static bool
00d26680 21175cp_parser_cache_group (cp_parser *parser,
21176 enum cpp_ttype end,
21177 unsigned depth)
0a3b29ad 21178{
21179 while (true)
21180 {
f82f1250 21181 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 21182
f82f1250 21183 /* Abort a parenthesized expression if we encounter a semicolon. */
0a3b29ad 21184 if ((end == CPP_CLOSE_PAREN || depth == 0)
f82f1250 21185 && token->type == CPP_SEMICOLON)
21186 return true;
0a3b29ad 21187 /* If we've reached the end of the file, stop. */
f82f1250 21188 if (token->type == CPP_EOF
b75b98aa 21189 || (end != CPP_PRAGMA_EOL
f82f1250 21190 && token->type == CPP_PRAGMA_EOL))
21191 return true;
21192 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21193 /* We've hit the end of an enclosing block, so there's been some
21194 kind of syntax error. */
21195 return true;
21196
21197 /* Consume the token. */
21198 cp_lexer_consume_token (parser->lexer);
0a3b29ad 21199 /* See if it starts a new group. */
21200 if (token->type == CPP_OPEN_BRACE)
21201 {
00d26680 21202 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
f82f1250 21203 /* In theory this should probably check end == '}', but
21204 cp_parser_save_member_function_body needs it to exit
21205 after either '}' or ')' when called with ')'. */
0a3b29ad 21206 if (depth == 0)
f82f1250 21207 return false;
0a3b29ad 21208 }
21209 else if (token->type == CPP_OPEN_PAREN)
f82f1250 21210 {
21211 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21212 if (depth == 0 && end == CPP_CLOSE_PAREN)
21213 return false;
21214 }
b75b98aa 21215 else if (token->type == CPP_PRAGMA)
21216 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
0a3b29ad 21217 else if (token->type == end)
f82f1250 21218 return false;
0a3b29ad 21219 }
21220}
21221
21222/* Begin parsing tentatively. We always save tokens while parsing
21223 tentatively so that if the tentative parsing fails we can restore the
21224 tokens. */
21225
21226static void
45baea8b 21227cp_parser_parse_tentatively (cp_parser* parser)
0a3b29ad 21228{
21229 /* Enter a new parsing context. */
21230 parser->context = cp_parser_context_new (parser->context);
21231 /* Begin saving tokens. */
21232 cp_lexer_save_tokens (parser->lexer);
21233 /* In order to avoid repetitive access control error messages,
21234 access checks are queued up until we are no longer parsing
21235 tentatively. */
4f62c42e 21236 push_deferring_access_checks (dk_deferred);
0a3b29ad 21237}
21238
21239/* Commit to the currently active tentative parse. */
21240
21241static void
45baea8b 21242cp_parser_commit_to_tentative_parse (cp_parser* parser)
0a3b29ad 21243{
21244 cp_parser_context *context;
21245 cp_lexer *lexer;
21246
21247 /* Mark all of the levels as committed. */
21248 lexer = parser->lexer;
21249 for (context = parser->context; context->next; context = context->next)
21250 {
21251 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21252 break;
21253 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21254 while (!cp_lexer_saving_tokens (lexer))
21255 lexer = lexer->next;
21256 cp_lexer_commit_tokens (lexer);
21257 }
21258}
21259
21260/* Abort the currently active tentative parse. All consumed tokens
21261 will be rolled back, and no diagnostics will be issued. */
21262
21263static void
45baea8b 21264cp_parser_abort_tentative_parse (cp_parser* parser)
0a3b29ad 21265{
21266 cp_parser_simulate_error (parser);
21267 /* Now, pretend that we want to see if the construct was
21268 successfully parsed. */
21269 cp_parser_parse_definitely (parser);
21270}
21271
755edffd 21272/* Stop parsing tentatively. If a parse error has occurred, restore the
0a3b29ad 21273 token stream. Otherwise, commit to the tokens we have consumed.
21274 Returns true if no error occurred; false otherwise. */
21275
21276static bool
45baea8b 21277cp_parser_parse_definitely (cp_parser* parser)
0a3b29ad 21278{
21279 bool error_occurred;
21280 cp_parser_context *context;
21281
755edffd 21282 /* Remember whether or not an error occurred, since we are about to
0a3b29ad 21283 destroy that information. */
21284 error_occurred = cp_parser_error_occurred (parser);
21285 /* Remove the topmost context from the stack. */
21286 context = parser->context;
21287 parser->context = context->next;
21288 /* If no parse errors occurred, commit to the tentative parse. */
21289 if (!error_occurred)
21290 {
21291 /* Commit to the tokens read tentatively, unless that was
21292 already done. */
21293 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21294 cp_lexer_commit_tokens (parser->lexer);
9b57b06b 21295
21296 pop_to_parent_deferring_access_checks ();
0a3b29ad 21297 }
21298 /* Otherwise, if errors occurred, roll back our state so that things
21299 are just as they were before we began the tentative parse. */
21300 else
9b57b06b 21301 {
21302 cp_lexer_rollback_tokens (parser->lexer);
21303 pop_deferring_access_checks ();
21304 }
2c593bd0 21305 /* Add the context to the front of the free list. */
21306 context->next = cp_parser_context_free_list;
21307 cp_parser_context_free_list = context;
21308
21309 return !error_occurred;
0a3b29ad 21310}
21311
efcbcf83 21312/* Returns true if we are parsing tentatively and are not committed to
21313 this tentative parse. */
0a3b29ad 21314
21315static bool
efcbcf83 21316cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
0a3b29ad 21317{
21318 return (cp_parser_parsing_tentatively (parser)
efcbcf83 21319 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
0a3b29ad 21320}
21321
f1d555e3 21322/* Returns nonzero iff an error has occurred during the most recent
0a3b29ad 21323 tentative parse. */
ccb84981 21324
0a3b29ad 21325static bool
45baea8b 21326cp_parser_error_occurred (cp_parser* parser)
0a3b29ad 21327{
21328 return (cp_parser_parsing_tentatively (parser)
21329 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21330}
21331
f1d555e3 21332/* Returns nonzero if GNU extensions are allowed. */
0a3b29ad 21333
21334static bool
45baea8b 21335cp_parser_allow_gnu_extensions_p (cp_parser* parser)
0a3b29ad 21336{
21337 return parser->allow_gnu_extensions_p;
21338}
7a4e126b 21339\f
21340/* Objective-C++ Productions */
21341
21342
21343/* Parse an Objective-C expression, which feeds into a primary-expression
21344 above.
21345
21346 objc-expression:
21347 objc-message-expression
21348 objc-string-literal
21349 objc-encode-expression
21350 objc-protocol-expression
21351 objc-selector-expression
21352
21353 Returns a tree representation of the expression. */
21354
21355static tree
21356cp_parser_objc_expression (cp_parser* parser)
21357{
21358 /* Try to figure out what kind of declaration is present. */
21359 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21360
21361 switch (kwd->type)
21362 {
21363 case CPP_OPEN_SQUARE:
21364 return cp_parser_objc_message_expression (parser);
21365
21366 case CPP_OBJC_STRING:
21367 kwd = cp_lexer_consume_token (parser->lexer);
3369eb76 21368 return objc_build_string_object (kwd->u.value);
7a4e126b 21369
21370 case CPP_KEYWORD:
21371 switch (kwd->keyword)
21372 {
21373 case RID_AT_ENCODE:
21374 return cp_parser_objc_encode_expression (parser);
21375
21376 case RID_AT_PROTOCOL:
21377 return cp_parser_objc_protocol_expression (parser);
21378
21379 case RID_AT_SELECTOR:
21380 return cp_parser_objc_selector_expression (parser);
21381
21382 default:
21383 break;
21384 }
21385 default:
ccb59bb6 21386 error_at (kwd->location,
21387 "misplaced %<@%D%> Objective-C++ construct",
21388 kwd->u.value);
7a4e126b 21389 cp_parser_skip_to_end_of_block_or_statement (parser);
21390 }
21391
21392 return error_mark_node;
21393}
21394
21395/* Parse an Objective-C message expression.
21396
21397 objc-message-expression:
21398 [ objc-message-receiver objc-message-args ]
21399
21400 Returns a representation of an Objective-C message. */
21401
21402static tree
21403cp_parser_objc_message_expression (cp_parser* parser)
21404{
21405 tree receiver, messageargs;
21406
21407 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21408 receiver = cp_parser_objc_message_receiver (parser);
21409 messageargs = cp_parser_objc_message_args (parser);
c247dce0 21410 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7a4e126b 21411
21412 return objc_build_message_expr (build_tree_list (receiver, messageargs));
21413}
21414
21415/* Parse an objc-message-receiver.
21416
21417 objc-message-receiver:
7a4e126b 21418 expression
aa796005 21419 simple-type-specifier
7a4e126b 21420
21421 Returns a representation of the type or expression. */
21422
21423static tree
21424cp_parser_objc_message_receiver (cp_parser* parser)
21425{
21426 tree rcv;
7a4e126b 21427
21428 /* An Objective-C message receiver may be either (1) a type
21429 or (2) an expression. */
21430 cp_parser_parse_tentatively (parser);
98b326fd 21431 rcv = cp_parser_expression (parser, false, NULL);
7a4e126b 21432
21433 if (cp_parser_parse_definitely (parser))
21434 return rcv;
21435
aa796005 21436 rcv = cp_parser_simple_type_specifier (parser,
21437 /*decl_specs=*/NULL,
21438 CP_PARSER_FLAGS_NONE);
7a4e126b 21439
21440 return objc_get_class_reference (rcv);
21441}
21442
21443/* Parse the arguments and selectors comprising an Objective-C message.
21444
21445 objc-message-args:
21446 objc-selector
21447 objc-selector-args
21448 objc-selector-args , objc-comma-args
21449
21450 objc-selector-args:
21451 objc-selector [opt] : assignment-expression
21452 objc-selector-args objc-selector [opt] : assignment-expression
21453
21454 objc-comma-args:
21455 assignment-expression
21456 objc-comma-args , assignment-expression
21457
21458 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21459 selector arguments and TREE_VALUE containing a list of comma
21460 arguments. */
21461
21462static tree
21463cp_parser_objc_message_args (cp_parser* parser)
21464{
21465 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21466 bool maybe_unary_selector_p = true;
21467 cp_token *token = cp_lexer_peek_token (parser->lexer);
21468
21469 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21470 {
21471 tree selector = NULL_TREE, arg;
21472
21473 if (token->type != CPP_COLON)
21474 selector = cp_parser_objc_selector (parser);
21475
21476 /* Detect if we have a unary selector. */
21477 if (maybe_unary_selector_p
21478 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21479 return build_tree_list (selector, NULL_TREE);
21480
21481 maybe_unary_selector_p = false;
c247dce0 21482 cp_parser_require (parser, CPP_COLON, RT_COLON);
98b326fd 21483 arg = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 21484
21485 sel_args
21486 = chainon (sel_args,
21487 build_tree_list (selector, arg));
21488
21489 token = cp_lexer_peek_token (parser->lexer);
21490 }
21491
21492 /* Handle non-selector arguments, if any. */
21493 while (token->type == CPP_COMMA)
21494 {
21495 tree arg;
21496
21497 cp_lexer_consume_token (parser->lexer);
98b326fd 21498 arg = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 21499
21500 addl_args
21501 = chainon (addl_args,
21502 build_tree_list (NULL_TREE, arg));
21503
21504 token = cp_lexer_peek_token (parser->lexer);
21505 }
21506
06517bd4 21507 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21508 {
21509 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21510 return build_tree_list (error_mark_node, error_mark_node);
21511 }
21512
7a4e126b 21513 return build_tree_list (sel_args, addl_args);
21514}
21515
21516/* Parse an Objective-C encode expression.
21517
21518 objc-encode-expression:
21519 @encode objc-typename
9031d10b 21520
7a4e126b 21521 Returns an encoded representation of the type argument. */
21522
21523static tree
21524cp_parser_objc_encode_expression (cp_parser* parser)
21525{
21526 tree type;
ad9ae192 21527 cp_token *token;
7a4e126b 21528
21529 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
c247dce0 21530 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
ad9ae192 21531 token = cp_lexer_peek_token (parser->lexer);
7a4e126b 21532 type = complete_type (cp_parser_type_id (parser));
c247dce0 21533 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 21534
21535 if (!type)
21536 {
ccb59bb6 21537 error_at (token->location,
21538 "%<@encode%> must specify a type as an argument");
7a4e126b 21539 return error_mark_node;
21540 }
21541
f4babcfa 21542 /* This happens if we find @encode(T) (where T is a template
21543 typename or something dependent on a template typename) when
21544 parsing a template. In that case, we can't compile it
21545 immediately, but we rather create an AT_ENCODE_EXPR which will
21546 need to be instantiated when the template is used.
21547 */
01d43738 21548 if (dependent_type_p (type))
21549 {
21550 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21551 TREE_READONLY (value) = 1;
21552 return value;
21553 }
21554
7a4e126b 21555 return objc_build_encode_expr (type);
21556}
21557
21558/* Parse an Objective-C @defs expression. */
21559
21560static tree
21561cp_parser_objc_defs_expression (cp_parser *parser)
21562{
21563 tree name;
21564
21565 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
c247dce0 21566 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7a4e126b 21567 name = cp_parser_identifier (parser);
c247dce0 21568 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 21569
21570 return objc_get_class_ivars (name);
21571}
21572
21573/* Parse an Objective-C protocol expression.
21574
21575 objc-protocol-expression:
21576 @protocol ( identifier )
21577
21578 Returns a representation of the protocol expression. */
21579
21580static tree
21581cp_parser_objc_protocol_expression (cp_parser* parser)
21582{
21583 tree proto;
21584
21585 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
c247dce0 21586 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7a4e126b 21587 proto = cp_parser_identifier (parser);
c247dce0 21588 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 21589
21590 return objc_build_protocol_expr (proto);
21591}
21592
21593/* Parse an Objective-C selector expression.
21594
21595 objc-selector-expression:
21596 @selector ( objc-method-signature )
21597
21598 objc-method-signature:
21599 objc-selector
21600 objc-selector-seq
21601
21602 objc-selector-seq:
21603 objc-selector :
21604 objc-selector-seq objc-selector :
21605
21606 Returns a representation of the method selector. */
21607
21608static tree
21609cp_parser_objc_selector_expression (cp_parser* parser)
21610{
21611 tree sel_seq = NULL_TREE;
21612 bool maybe_unary_selector_p = true;
21613 cp_token *token;
e60a6f7b 21614 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7a4e126b 21615
21616 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
c247dce0 21617 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7a4e126b 21618 token = cp_lexer_peek_token (parser->lexer);
21619
ca7aef9f 21620 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
074ab442 21621 || token->type == CPP_SCOPE)
7a4e126b 21622 {
21623 tree selector = NULL_TREE;
21624
ca7aef9f 21625 if (token->type != CPP_COLON
21626 || token->type == CPP_SCOPE)
7a4e126b 21627 selector = cp_parser_objc_selector (parser);
21628
ca7aef9f 21629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
074ab442 21630 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
7a4e126b 21631 {
ca7aef9f 21632 /* Detect if we have a unary selector. */
21633 if (maybe_unary_selector_p)
21634 {
21635 sel_seq = selector;
21636 goto finish_selector;
21637 }
21638 else
21639 {
21640 cp_parser_error (parser, "expected %<:%>");
21641 }
7a4e126b 21642 }
7a4e126b 21643 maybe_unary_selector_p = false;
ca7aef9f 21644 token = cp_lexer_consume_token (parser->lexer);
074ab442 21645
ca7aef9f 21646 if (token->type == CPP_SCOPE)
074ab442 21647 {
ca7aef9f 21648 sel_seq
21649 = chainon (sel_seq,
21650 build_tree_list (selector, NULL_TREE));
21651 sel_seq
21652 = chainon (sel_seq,
21653 build_tree_list (NULL_TREE, NULL_TREE));
21654 }
21655 else
21656 sel_seq
21657 = chainon (sel_seq,
21658 build_tree_list (selector, NULL_TREE));
7a4e126b 21659
21660 token = cp_lexer_peek_token (parser->lexer);
21661 }
21662
21663 finish_selector:
c247dce0 21664 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 21665
e60a6f7b 21666 return objc_build_selector_expr (loc, sel_seq);
7a4e126b 21667}
21668
21669/* Parse a list of identifiers.
21670
21671 objc-identifier-list:
21672 identifier
21673 objc-identifier-list , identifier
21674
21675 Returns a TREE_LIST of identifier nodes. */
21676
21677static tree
21678cp_parser_objc_identifier_list (cp_parser* parser)
21679{
e1f293c0 21680 tree identifier;
21681 tree list;
21682 cp_token *sep;
21683
21684 identifier = cp_parser_identifier (parser);
21685 if (identifier == error_mark_node)
21686 return error_mark_node;
21687
21688 list = build_tree_list (NULL_TREE, identifier);
21689 sep = cp_lexer_peek_token (parser->lexer);
7a4e126b 21690
21691 while (sep->type == CPP_COMMA)
21692 {
21693 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
e1f293c0 21694 identifier = cp_parser_identifier (parser);
21695 if (identifier == error_mark_node)
21696 return list;
21697
21698 list = chainon (list, build_tree_list (NULL_TREE,
21699 identifier));
7a4e126b 21700 sep = cp_lexer_peek_token (parser->lexer);
21701 }
e1f293c0 21702
7a4e126b 21703 return list;
21704}
21705
21706/* Parse an Objective-C alias declaration.
21707
21708 objc-alias-declaration:
21709 @compatibility_alias identifier identifier ;
21710
a17c2a3a 21711 This function registers the alias mapping with the Objective-C front end.
7a4e126b 21712 It returns nothing. */
21713
21714static void
21715cp_parser_objc_alias_declaration (cp_parser* parser)
21716{
21717 tree alias, orig;
21718
21719 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21720 alias = cp_parser_identifier (parser);
21721 orig = cp_parser_identifier (parser);
21722 objc_declare_alias (alias, orig);
21723 cp_parser_consume_semicolon_at_end_of_statement (parser);
21724}
21725
21726/* Parse an Objective-C class forward-declaration.
21727
21728 objc-class-declaration:
21729 @class objc-identifier-list ;
21730
21731 The function registers the forward declarations with the Objective-C
a17c2a3a 21732 front end. It returns nothing. */
7a4e126b 21733
21734static void
21735cp_parser_objc_class_declaration (cp_parser* parser)
21736{
21737 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21738 objc_declare_class (cp_parser_objc_identifier_list (parser));
21739 cp_parser_consume_semicolon_at_end_of_statement (parser);
21740}
21741
21742/* Parse a list of Objective-C protocol references.
21743
21744 objc-protocol-refs-opt:
21745 objc-protocol-refs [opt]
21746
21747 objc-protocol-refs:
21748 < objc-identifier-list >
21749
21750 Returns a TREE_LIST of identifiers, if any. */
21751
21752static tree
21753cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21754{
21755 tree protorefs = NULL_TREE;
21756
21757 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21758 {
21759 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21760 protorefs = cp_parser_objc_identifier_list (parser);
c247dce0 21761 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7a4e126b 21762 }
21763
21764 return protorefs;
21765}
21766
21767/* Parse a Objective-C visibility specification. */
21768
21769static void
21770cp_parser_objc_visibility_spec (cp_parser* parser)
21771{
21772 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21773
21774 switch (vis->keyword)
21775 {
21776 case RID_AT_PRIVATE:
4a8875ed 21777 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
7a4e126b 21778 break;
21779 case RID_AT_PROTECTED:
4a8875ed 21780 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
7a4e126b 21781 break;
21782 case RID_AT_PUBLIC:
4a8875ed 21783 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21784 break;
21785 case RID_AT_PACKAGE:
21786 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
7a4e126b 21787 break;
21788 default:
21789 return;
21790 }
0a3b29ad 21791
7a4e126b 21792 /* Eat '@private'/'@protected'/'@public'. */
21793 cp_lexer_consume_token (parser->lexer);
21794}
21795
45b2b110 21796/* Parse an Objective-C method type. Return 'true' if it is a class
21797 (+) method, and 'false' if it is an instance (-) method. */
7a4e126b 21798
45b2b110 21799static inline bool
7a4e126b 21800cp_parser_objc_method_type (cp_parser* parser)
21801{
45b2b110 21802 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21803 return true;
21804 else
21805 return false;
7a4e126b 21806}
21807
21808/* Parse an Objective-C protocol qualifier. */
21809
21810static tree
21811cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21812{
21813 tree quals = NULL_TREE, node;
21814 cp_token *token = cp_lexer_peek_token (parser->lexer);
21815
3369eb76 21816 node = token->u.value;
7a4e126b 21817
21818 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21819 && (node == ridpointers [(int) RID_IN]
21820 || node == ridpointers [(int) RID_OUT]
21821 || node == ridpointers [(int) RID_INOUT]
21822 || node == ridpointers [(int) RID_BYCOPY]
653e5405 21823 || node == ridpointers [(int) RID_BYREF]
7a4e126b 21824 || node == ridpointers [(int) RID_ONEWAY]))
21825 {
21826 quals = tree_cons (NULL_TREE, node, quals);
21827 cp_lexer_consume_token (parser->lexer);
21828 token = cp_lexer_peek_token (parser->lexer);
3369eb76 21829 node = token->u.value;
7a4e126b 21830 }
21831
21832 return quals;
21833}
21834
21835/* Parse an Objective-C typename. */
21836
21837static tree
21838cp_parser_objc_typename (cp_parser* parser)
21839{
607a5d68 21840 tree type_name = NULL_TREE;
7a4e126b 21841
21842 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21843 {
21844 tree proto_quals, cp_type = NULL_TREE;
21845
21846 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21847 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21848
21849 /* An ObjC type name may consist of just protocol qualifiers, in which
21850 case the type shall default to 'id'. */
21851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21852 cp_type = cp_parser_type_id (parser);
21853
c247dce0 21854 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
607a5d68 21855 type_name = build_tree_list (proto_quals, cp_type);
7a4e126b 21856 }
21857
607a5d68 21858 return type_name;
7a4e126b 21859}
21860
21861/* Check to see if TYPE refers to an Objective-C selector name. */
21862
21863static bool
21864cp_parser_objc_selector_p (enum cpp_ttype type)
21865{
21866 return (type == CPP_NAME || type == CPP_KEYWORD
21867 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21868 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21869 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21870 || type == CPP_XOR || type == CPP_XOR_EQ);
21871}
21872
21873/* Parse an Objective-C selector. */
21874
21875static tree
21876cp_parser_objc_selector (cp_parser* parser)
21877{
21878 cp_token *token = cp_lexer_consume_token (parser->lexer);
9031d10b 21879
7a4e126b 21880 if (!cp_parser_objc_selector_p (token->type))
21881 {
ccb59bb6 21882 error_at (token->location, "invalid Objective-C++ selector name");
7a4e126b 21883 return error_mark_node;
21884 }
21885
21886 /* C++ operator names are allowed to appear in ObjC selectors. */
21887 switch (token->type)
21888 {
21889 case CPP_AND_AND: return get_identifier ("and");
21890 case CPP_AND_EQ: return get_identifier ("and_eq");
21891 case CPP_AND: return get_identifier ("bitand");
21892 case CPP_OR: return get_identifier ("bitor");
21893 case CPP_COMPL: return get_identifier ("compl");
21894 case CPP_NOT: return get_identifier ("not");
21895 case CPP_NOT_EQ: return get_identifier ("not_eq");
21896 case CPP_OR_OR: return get_identifier ("or");
21897 case CPP_OR_EQ: return get_identifier ("or_eq");
21898 case CPP_XOR: return get_identifier ("xor");
21899 case CPP_XOR_EQ: return get_identifier ("xor_eq");
3369eb76 21900 default: return token->u.value;
7a4e126b 21901 }
21902}
21903
21904/* Parse an Objective-C params list. */
21905
21906static tree
03fc2271 21907cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
7a4e126b 21908{
21909 tree params = NULL_TREE;
21910 bool maybe_unary_selector_p = true;
21911 cp_token *token = cp_lexer_peek_token (parser->lexer);
21912
21913 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21914 {
607a5d68 21915 tree selector = NULL_TREE, type_name, identifier;
03fc2271 21916 tree parm_attr = NULL_TREE;
21917
21918 if (token->keyword == RID_ATTRIBUTE)
21919 break;
7a4e126b 21920
21921 if (token->type != CPP_COLON)
21922 selector = cp_parser_objc_selector (parser);
21923
21924 /* Detect if we have a unary selector. */
21925 if (maybe_unary_selector_p
21926 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
03fc2271 21927 {
21928 params = selector; /* Might be followed by attributes. */
21929 break;
21930 }
7a4e126b 21931
21932 maybe_unary_selector_p = false;
06517bd4 21933 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21934 {
21935 /* Something went quite wrong. There should be a colon
21936 here, but there is not. Stop parsing parameters. */
21937 break;
21938 }
607a5d68 21939 type_name = cp_parser_objc_typename (parser);
03fc2271 21940 /* New ObjC allows attributes on parameters too. */
21941 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21942 parm_attr = cp_parser_attributes_opt (parser);
7a4e126b 21943 identifier = cp_parser_identifier (parser);
21944
21945 params
21946 = chainon (params,
9031d10b 21947 objc_build_keyword_decl (selector,
607a5d68 21948 type_name,
03fc2271 21949 identifier,
21950 parm_attr));
7a4e126b 21951
21952 token = cp_lexer_peek_token (parser->lexer);
21953 }
21954
03fc2271 21955 if (params == NULL_TREE)
21956 {
21957 cp_parser_error (parser, "objective-c++ method declaration is expected");
21958 return error_mark_node;
21959 }
21960
21961 /* We allow tail attributes for the method. */
21962 if (token->keyword == RID_ATTRIBUTE)
21963 {
21964 *attributes = cp_parser_attributes_opt (parser);
21965 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21966 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21967 return params;
21968 cp_parser_error (parser,
21969 "method attributes must be specified at the end");
21970 return error_mark_node;
21971 }
21972
06517bd4 21973 if (params == NULL_TREE)
21974 {
21975 cp_parser_error (parser, "objective-c++ method declaration is expected");
21976 return error_mark_node;
21977 }
7a4e126b 21978 return params;
21979}
21980
21981/* Parse the non-keyword Objective-C params. */
21982
21983static tree
03fc2271 21984cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
21985 tree* attributes)
7a4e126b 21986{
21987 tree params = make_node (TREE_LIST);
21988 cp_token *token = cp_lexer_peek_token (parser->lexer);
21989 *ellipsisp = false; /* Initially, assume no ellipsis. */
21990
21991 while (token->type == CPP_COMMA)
21992 {
21993 cp_parameter_declarator *parmdecl;
21994 tree parm;
21995
21996 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21997 token = cp_lexer_peek_token (parser->lexer);
21998
21999 if (token->type == CPP_ELLIPSIS)
22000 {
22001 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22002 *ellipsisp = true;
cabf6150 22003 token = cp_lexer_peek_token (parser->lexer);
7a4e126b 22004 break;
22005 }
22006
03fc2271 22007 /* TODO: parse attributes for tail parameters. */
7a4e126b 22008 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22009 parm = grokdeclarator (parmdecl->declarator,
22010 &parmdecl->decl_specifiers,
9031d10b 22011 PARM, /*initialized=*/0,
7a4e126b 22012 /*attrlist=*/NULL);
22013
22014 chainon (params, build_tree_list (NULL_TREE, parm));
22015 token = cp_lexer_peek_token (parser->lexer);
22016 }
22017
03fc2271 22018 /* We allow tail attributes for the method. */
22019 if (token->keyword == RID_ATTRIBUTE)
22020 {
22021 if (*attributes == NULL_TREE)
22022 {
22023 *attributes = cp_parser_attributes_opt (parser);
22024 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22025 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22026 return params;
22027 }
22028 else
22029 /* We have an error, but parse the attributes, so that we can
22030 carry on. */
22031 *attributes = cp_parser_attributes_opt (parser);
22032
22033 cp_parser_error (parser,
22034 "method attributes must be specified at the end");
22035 return error_mark_node;
22036 }
22037
7a4e126b 22038 return params;
22039}
22040
22041/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22042
22043static void
22044cp_parser_objc_interstitial_code (cp_parser* parser)
22045{
22046 cp_token *token = cp_lexer_peek_token (parser->lexer);
22047
22048 /* If the next token is `extern' and the following token is a string
22049 literal, then we have a linkage specification. */
22050 if (token->keyword == RID_EXTERN
22051 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22052 cp_parser_linkage_specification (parser);
22053 /* Handle #pragma, if any. */
22054 else if (token->type == CPP_PRAGMA)
b75b98aa 22055 cp_parser_pragma (parser, pragma_external);
7a4e126b 22056 /* Allow stray semicolons. */
22057 else if (token->type == CPP_SEMICOLON)
22058 cp_lexer_consume_token (parser->lexer);
069761fb 22059 /* Mark methods as optional or required, when building protocols. */
22060 else if (token->keyword == RID_AT_OPTIONAL)
22061 {
22062 cp_lexer_consume_token (parser->lexer);
22063 objc_set_method_opt (true);
22064 }
22065 else if (token->keyword == RID_AT_REQUIRED)
22066 {
22067 cp_lexer_consume_token (parser->lexer);
22068 objc_set_method_opt (false);
22069 }
456d230e 22070 else if (token->keyword == RID_NAMESPACE)
22071 cp_parser_namespace_definition (parser);
06517bd4 22072 /* Other stray characters must generate errors. */
22073 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22074 {
22075 cp_lexer_consume_token (parser->lexer);
bf776685 22076 error ("stray %qs between Objective-C++ methods",
06517bd4 22077 token->type == CPP_OPEN_BRACE ? "{" : "}");
22078 }
7a4e126b 22079 /* Finally, try to parse a block-declaration, or a function-definition. */
22080 else
22081 cp_parser_block_declaration (parser, /*statement_p=*/false);
22082}
22083
22084/* Parse a method signature. */
22085
22086static tree
03fc2271 22087cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
7a4e126b 22088{
22089 tree rettype, kwdparms, optparms;
22090 bool ellipsis = false;
45b2b110 22091 bool is_class_method;
7a4e126b 22092
45b2b110 22093 is_class_method = cp_parser_objc_method_type (parser);
7a4e126b 22094 rettype = cp_parser_objc_typename (parser);
03fc2271 22095 *attributes = NULL_TREE;
22096 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22097 if (kwdparms == error_mark_node)
22098 return error_mark_node;
22099 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22100 if (optparms == error_mark_node)
22101 return error_mark_node;
7a4e126b 22102
45b2b110 22103 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
7a4e126b 22104}
22105
03fc2271 22106static bool
22107cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22108{
22109 tree tattr;
22110 cp_lexer_save_tokens (parser->lexer);
22111 tattr = cp_parser_attributes_opt (parser);
22112 gcc_assert (tattr) ;
22113
22114 /* If the attributes are followed by a method introducer, this is not allowed.
22115 Dump the attributes and flag the situation. */
22116 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22117 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22118 return true;
22119
22120 /* Otherwise, the attributes introduce some interstitial code, possibly so
22121 rewind to allow that check. */
22122 cp_lexer_rollback_tokens (parser->lexer);
22123 return false;
22124}
22125
22126/* Parse an Objective-C method prototype list. */
7a4e126b 22127
22128static void
22129cp_parser_objc_method_prototype_list (cp_parser* parser)
22130{
22131 cp_token *token = cp_lexer_peek_token (parser->lexer);
22132
03fc2271 22133 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
7a4e126b 22134 {
22135 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22136 {
03fc2271 22137 tree attributes, sig;
45b2b110 22138 bool is_class_method;
22139 if (token->type == CPP_PLUS)
22140 is_class_method = true;
22141 else
22142 is_class_method = false;
03fc2271 22143 sig = cp_parser_objc_method_signature (parser, &attributes);
22144 if (sig == error_mark_node)
22145 {
22146 cp_parser_skip_to_end_of_block_or_statement (parser);
06517bd4 22147 token = cp_lexer_peek_token (parser->lexer);
03fc2271 22148 continue;
22149 }
45b2b110 22150 objc_add_method_declaration (is_class_method, sig, attributes);
7a4e126b 22151 cp_parser_consume_semicolon_at_end_of_statement (parser);
22152 }
86c110ac 22153 else if (token->keyword == RID_AT_PROPERTY)
1d894bcf 22154 cp_parser_objc_at_property_declaration (parser);
03fc2271 22155 else if (token->keyword == RID_ATTRIBUTE
22156 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22157 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22158 OPT_Wattributes,
22159 "prefix attributes are ignored for methods");
7a4e126b 22160 else
22161 /* Allow for interspersed non-ObjC++ code. */
22162 cp_parser_objc_interstitial_code (parser);
22163
22164 token = cp_lexer_peek_token (parser->lexer);
22165 }
22166
06517bd4 22167 if (token->type != CPP_EOF)
22168 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22169 else
22170 cp_parser_error (parser, "expected %<@end%>");
22171
7a4e126b 22172 objc_finish_interface ();
22173}
22174
22175/* Parse an Objective-C method definition list. */
22176
22177static void
22178cp_parser_objc_method_definition_list (cp_parser* parser)
22179{
22180 cp_token *token = cp_lexer_peek_token (parser->lexer);
22181
03fc2271 22182 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
7a4e126b 22183 {
22184 tree meth;
22185
22186 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22187 {
03fc2271 22188 cp_token *ptk;
22189 tree sig, attribute;
45b2b110 22190 bool is_class_method;
22191 if (token->type == CPP_PLUS)
22192 is_class_method = true;
22193 else
22194 is_class_method = false;
7a4e126b 22195 push_deferring_access_checks (dk_deferred);
03fc2271 22196 sig = cp_parser_objc_method_signature (parser, &attribute);
22197 if (sig == error_mark_node)
22198 {
22199 cp_parser_skip_to_end_of_block_or_statement (parser);
06517bd4 22200 token = cp_lexer_peek_token (parser->lexer);
03fc2271 22201 continue;
22202 }
45b2b110 22203 objc_start_method_definition (is_class_method, sig, attribute);
7a4e126b 22204
22205 /* For historical reasons, we accept an optional semicolon. */
22206 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22207 cp_lexer_consume_token (parser->lexer);
22208
03fc2271 22209 ptk = cp_lexer_peek_token (parser->lexer);
22210 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22211 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22212 {
22213 perform_deferred_access_checks ();
22214 stop_deferring_access_checks ();
22215 meth = cp_parser_function_definition_after_declarator (parser,
06517bd4 22216 false);
03fc2271 22217 pop_deferring_access_checks ();
22218 objc_finish_method_definition (meth);
22219 }
7a4e126b 22220 }
1d894bcf 22221 /* The following case will be removed once @synthesize is
22222 completely implemented. */
86c110ac 22223 else if (token->keyword == RID_AT_PROPERTY)
1d894bcf 22224 cp_parser_objc_at_property_declaration (parser);
e1f293c0 22225 else if (token->keyword == RID_AT_SYNTHESIZE)
22226 cp_parser_objc_at_synthesize_declaration (parser);
22227 else if (token->keyword == RID_AT_DYNAMIC)
22228 cp_parser_objc_at_dynamic_declaration (parser);
03fc2271 22229 else if (token->keyword == RID_ATTRIBUTE
22230 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22231 warning_at (token->location, OPT_Wattributes,
22232 "prefix attributes are ignored for methods");
7a4e126b 22233 else
22234 /* Allow for interspersed non-ObjC++ code. */
22235 cp_parser_objc_interstitial_code (parser);
22236
22237 token = cp_lexer_peek_token (parser->lexer);
22238 }
22239
06517bd4 22240 if (token->type != CPP_EOF)
22241 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22242 else
22243 cp_parser_error (parser, "expected %<@end%>");
22244
7a4e126b 22245 objc_finish_implementation ();
22246}
22247
22248/* Parse Objective-C ivars. */
22249
22250static void
22251cp_parser_objc_class_ivars (cp_parser* parser)
22252{
22253 cp_token *token = cp_lexer_peek_token (parser->lexer);
22254
22255 if (token->type != CPP_OPEN_BRACE)
22256 return; /* No ivars specified. */
22257
22258 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22259 token = cp_lexer_peek_token (parser->lexer);
22260
06517bd4 22261 while (token->type != CPP_CLOSE_BRACE
22262 && token->keyword != RID_AT_END && token->type != CPP_EOF)
7a4e126b 22263 {
22264 cp_decl_specifier_seq declspecs;
22265 int decl_class_or_enum_p;
22266 tree prefix_attributes;
22267
22268 cp_parser_objc_visibility_spec (parser);
22269
22270 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22271 break;
22272
22273 cp_parser_decl_specifier_seq (parser,
22274 CP_PARSER_FLAGS_OPTIONAL,
22275 &declspecs,
22276 &decl_class_or_enum_p);
1d894bcf 22277
22278 /* auto, register, static, extern, mutable. */
22279 if (declspecs.storage_class != sc_none)
22280 {
22281 cp_parser_error (parser, "invalid type for instance variable");
22282 declspecs.storage_class = sc_none;
22283 }
22284
22285 /* __thread. */
22286 if (declspecs.specs[(int) ds_thread])
22287 {
22288 cp_parser_error (parser, "invalid type for instance variable");
22289 declspecs.specs[(int) ds_thread] = 0;
22290 }
22291
22292 /* typedef. */
22293 if (declspecs.specs[(int) ds_typedef])
22294 {
22295 cp_parser_error (parser, "invalid type for instance variable");
22296 declspecs.specs[(int) ds_typedef] = 0;
22297 }
22298
7a4e126b 22299 prefix_attributes = declspecs.attributes;
22300 declspecs.attributes = NULL_TREE;
22301
22302 /* Keep going until we hit the `;' at the end of the
22303 declaration. */
22304 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22305 {
22306 tree width = NULL_TREE, attributes, first_attribute, decl;
22307 cp_declarator *declarator = NULL;
22308 int ctor_dtor_or_conv_p;
22309
22310 /* Check for a (possibly unnamed) bitfield declaration. */
22311 token = cp_lexer_peek_token (parser->lexer);
22312 if (token->type == CPP_COLON)
22313 goto eat_colon;
22314
22315 if (token->type == CPP_NAME
22316 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22317 == CPP_COLON))
22318 {
22319 /* Get the name of the bitfield. */
22320 declarator = make_id_declarator (NULL_TREE,
2366ed31 22321 cp_parser_identifier (parser),
22322 sfk_none);
7a4e126b 22323
22324 eat_colon:
22325 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22326 /* Get the width of the bitfield. */
22327 width
22328 = cp_parser_constant_expression (parser,
22329 /*allow_non_constant=*/false,
22330 NULL);
22331 }
22332 else
22333 {
22334 /* Parse the declarator. */
9031d10b 22335 declarator
7a4e126b 22336 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22337 &ctor_dtor_or_conv_p,
22338 /*parenthesized_p=*/NULL,
22339 /*member_p=*/false);
22340 }
22341
22342 /* Look for attributes that apply to the ivar. */
22343 attributes = cp_parser_attributes_opt (parser);
22344 /* Remember which attributes are prefix attributes and
22345 which are not. */
22346 first_attribute = attributes;
22347 /* Combine the attributes. */
22348 attributes = chainon (prefix_attributes, attributes);
22349
22350 if (width)
7a4e126b 22351 /* Create the bitfield declaration. */
f922e029 22352 decl = grokbitfield (declarator, &declspecs,
22353 width,
22354 attributes);
7a4e126b 22355 else
074ab442 22356 decl = grokfield (declarator, &declspecs,
d91303a6 22357 NULL_TREE, /*init_const_expr_p=*/false,
7a4e126b 22358 NULL_TREE, attributes);
9031d10b 22359
7a4e126b 22360 /* Add the instance variable. */
22361 objc_add_instance_variable (decl);
22362
22363 /* Reset PREFIX_ATTRIBUTES. */
22364 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22365 attributes = TREE_CHAIN (attributes);
22366 if (attributes)
22367 TREE_CHAIN (attributes) = NULL_TREE;
22368
22369 token = cp_lexer_peek_token (parser->lexer);
22370
22371 if (token->type == CPP_COMMA)
22372 {
22373 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22374 continue;
22375 }
22376 break;
22377 }
22378
22379 cp_parser_consume_semicolon_at_end_of_statement (parser);
22380 token = cp_lexer_peek_token (parser->lexer);
22381 }
22382
06517bd4 22383 if (token->keyword == RID_AT_END)
22384 cp_parser_error (parser, "expected %<}%>");
22385
22386 /* Do not consume the RID_AT_END, so it will be read again as terminating
22387 the @interface of @implementation. */
22388 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22389 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22390
7a4e126b 22391 /* For historical reasons, we accept an optional semicolon. */
22392 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22393 cp_lexer_consume_token (parser->lexer);
22394}
22395
22396/* Parse an Objective-C protocol declaration. */
22397
22398static void
a336eb4b 22399cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
7a4e126b 22400{
22401 tree proto, protorefs;
22402 cp_token *tok;
22403
22404 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22406 {
ad9ae192 22407 tok = cp_lexer_peek_token (parser->lexer);
ccb59bb6 22408 error_at (tok->location, "identifier expected after %<@protocol%>");
7a4e126b 22409 goto finish;
22410 }
22411
c78cbec8 22412 /* See if we have a forward declaration or a definition. */
7a4e126b 22413 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
9031d10b 22414
7a4e126b 22415 /* Try a forward declaration first. */
22416 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22417 {
c213e196 22418 objc_declare_protocols (cp_parser_objc_identifier_list (parser),
22419 attributes);
9031d10b 22420 finish:
7a4e126b 22421 cp_parser_consume_semicolon_at_end_of_statement (parser);
9031d10b 22422 }
7a4e126b 22423
22424 /* Ok, we got a full-fledged definition (or at least should). */
22425 else
22426 {
22427 proto = cp_parser_identifier (parser);
22428 protorefs = cp_parser_objc_protocol_refs_opt (parser);
a336eb4b 22429 objc_start_protocol (proto, protorefs, attributes);
7a4e126b 22430 cp_parser_objc_method_prototype_list (parser);
22431 }
22432}
22433
22434/* Parse an Objective-C superclass or category. */
22435
22436static void
22437cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
22438 tree *categ)
22439{
22440 cp_token *next = cp_lexer_peek_token (parser->lexer);
22441
22442 *super = *categ = NULL_TREE;
22443 if (next->type == CPP_COLON)
22444 {
22445 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22446 *super = cp_parser_identifier (parser);
22447 }
22448 else if (next->type == CPP_OPEN_PAREN)
22449 {
22450 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22451 *categ = cp_parser_identifier (parser);
c247dce0 22452 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 22453 }
22454}
22455
22456/* Parse an Objective-C class interface. */
22457
22458static void
a336eb4b 22459cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
7a4e126b 22460{
22461 tree name, super, categ, protos;
22462
22463 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22464 name = cp_parser_identifier (parser);
06517bd4 22465 if (name == error_mark_node)
22466 {
22467 /* It's hard to recover because even if valid @interface stuff
22468 is to follow, we can't compile it (or validate it) if we
22469 don't even know which class it refers to. Let's assume this
22470 was a stray '@interface' token in the stream and skip it.
22471 */
22472 return;
22473 }
7a4e126b 22474 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22475 protos = cp_parser_objc_protocol_refs_opt (parser);
22476
22477 /* We have either a class or a category on our hands. */
22478 if (categ)
a336eb4b 22479 objc_start_category_interface (name, categ, protos, attributes);
7a4e126b 22480 else
22481 {
a336eb4b 22482 objc_start_class_interface (name, super, protos, attributes);
7a4e126b 22483 /* Handle instance variable declarations, if any. */
22484 cp_parser_objc_class_ivars (parser);
22485 objc_continue_interface ();
22486 }
22487
22488 cp_parser_objc_method_prototype_list (parser);
22489}
22490
22491/* Parse an Objective-C class implementation. */
22492
22493static void
22494cp_parser_objc_class_implementation (cp_parser* parser)
22495{
22496 tree name, super, categ;
22497
22498 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22499 name = cp_parser_identifier (parser);
06517bd4 22500 if (name == error_mark_node)
22501 {
22502 /* It's hard to recover because even if valid @implementation
22503 stuff is to follow, we can't compile it (or validate it) if
22504 we don't even know which class it refers to. Let's assume
22505 this was a stray '@implementation' token in the stream and
22506 skip it.
22507 */
22508 return;
22509 }
7a4e126b 22510 cp_parser_objc_superclass_or_category (parser, &super, &categ);
22511
22512 /* We have either a class or a category on our hands. */
22513 if (categ)
22514 objc_start_category_implementation (name, categ);
22515 else
22516 {
22517 objc_start_class_implementation (name, super);
22518 /* Handle instance variable declarations, if any. */
22519 cp_parser_objc_class_ivars (parser);
22520 objc_continue_implementation ();
22521 }
22522
22523 cp_parser_objc_method_definition_list (parser);
22524}
22525
22526/* Consume the @end token and finish off the implementation. */
22527
22528static void
22529cp_parser_objc_end_implementation (cp_parser* parser)
22530{
22531 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22532 objc_finish_implementation ();
22533}
22534
22535/* Parse an Objective-C declaration. */
22536
22537static void
a336eb4b 22538cp_parser_objc_declaration (cp_parser* parser, tree attributes)
7a4e126b 22539{
22540 /* Try to figure out what kind of declaration is present. */
22541 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22542
a336eb4b 22543 if (attributes)
22544 switch (kwd->keyword)
22545 {
22546 case RID_AT_ALIAS:
22547 case RID_AT_CLASS:
22548 case RID_AT_END:
22549 error_at (kwd->location, "attributes may not be specified before"
22550 " the %<@%D%> Objective-C++ keyword",
22551 kwd->u.value);
22552 attributes = NULL;
22553 break;
22554 case RID_AT_IMPLEMENTATION:
22555 warning_at (kwd->location, OPT_Wattributes,
22556 "prefix attributes are ignored before %<@%D%>",
22557 kwd->u.value);
22558 attributes = NULL;
22559 default:
22560 break;
22561 }
22562
7a4e126b 22563 switch (kwd->keyword)
22564 {
22565 case RID_AT_ALIAS:
22566 cp_parser_objc_alias_declaration (parser);
22567 break;
22568 case RID_AT_CLASS:
22569 cp_parser_objc_class_declaration (parser);
22570 break;
22571 case RID_AT_PROTOCOL:
a336eb4b 22572 cp_parser_objc_protocol_declaration (parser, attributes);
7a4e126b 22573 break;
22574 case RID_AT_INTERFACE:
a336eb4b 22575 cp_parser_objc_class_interface (parser, attributes);
7a4e126b 22576 break;
22577 case RID_AT_IMPLEMENTATION:
22578 cp_parser_objc_class_implementation (parser);
22579 break;
22580 case RID_AT_END:
22581 cp_parser_objc_end_implementation (parser);
22582 break;
22583 default:
ccb59bb6 22584 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22585 kwd->u.value);
7a4e126b 22586 cp_parser_skip_to_end_of_block_or_statement (parser);
22587 }
22588}
22589
22590/* Parse an Objective-C try-catch-finally statement.
22591
22592 objc-try-catch-finally-stmt:
22593 @try compound-statement objc-catch-clause-seq [opt]
22594 objc-finally-clause [opt]
22595
22596 objc-catch-clause-seq:
22597 objc-catch-clause objc-catch-clause-seq [opt]
22598
22599 objc-catch-clause:
b3d2d312 22600 @catch ( objc-exception-declaration ) compound-statement
7a4e126b 22601
b3d2d312 22602 objc-finally-clause:
7a4e126b 22603 @finally compound-statement
22604
b3d2d312 22605 objc-exception-declaration:
22606 parameter-declaration
22607 '...'
22608
22609 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22610
22611 Returns NULL_TREE.
22612
22613 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22614 for C. Keep them in sync. */
7a4e126b 22615
22616static tree
b3d2d312 22617cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22618{
7a4e126b 22619 location_t location;
22620 tree stmt;
22621
c247dce0 22622 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
7a4e126b 22623 location = cp_lexer_peek_token (parser->lexer)->location;
22624 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22625 node, lest it get absorbed into the surrounding block. */
22626 stmt = push_stmt_list ();
22627 cp_parser_compound_statement (parser, NULL, false);
22628 objc_begin_try_stmt (location, pop_stmt_list (stmt));
9031d10b 22629
7a4e126b 22630 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22631 {
b3d2d312 22632 cp_parameter_declarator *parm;
22633 tree parameter_declaration = error_mark_node;
22634 bool seen_open_paren = false;
7a4e126b 22635
22636 cp_lexer_consume_token (parser->lexer);
b3d2d312 22637 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22638 seen_open_paren = true;
22639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22640 {
22641 /* We have "@catch (...)" (where the '...' are literally
22642 what is in the code). Skip the '...'.
22643 parameter_declaration is set to NULL_TREE, and
22644 objc_being_catch_clauses() knows that that means
22645 '...'. */
22646 cp_lexer_consume_token (parser->lexer);
22647 parameter_declaration = NULL_TREE;
22648 }
22649 else
22650 {
22651 /* We have "@catch (NSException *exception)" or something
22652 like that. Parse the parameter declaration. */
22653 parm = cp_parser_parameter_declaration (parser, false, NULL);
22654 if (parm == NULL)
22655 parameter_declaration = error_mark_node;
22656 else
22657 parameter_declaration = grokdeclarator (parm->declarator,
22658 &parm->decl_specifiers,
22659 PARM, /*initialized=*/0,
22660 /*attrlist=*/NULL);
22661 }
22662 if (seen_open_paren)
22663 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22664 else
22665 {
22666 /* If there was no open parenthesis, we are recovering from
22667 an error, and we are trying to figure out what mistake
22668 the user has made. */
22669
22670 /* If there is an immediate closing parenthesis, the user
22671 probably forgot the opening one (ie, they typed "@catch
22672 NSException *e)". Parse the closing parenthesis and keep
22673 going. */
22674 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22675 cp_lexer_consume_token (parser->lexer);
22676
22677 /* If these is no immediate closing parenthesis, the user
22678 probably doesn't know that parenthesis are required at
22679 all (ie, they typed "@catch NSException *e"). So, just
22680 forget about the closing parenthesis and keep going. */
22681 }
22682 objc_begin_catch_clause (parameter_declaration);
7a4e126b 22683 cp_parser_compound_statement (parser, NULL, false);
22684 objc_finish_catch_clause ();
22685 }
7a4e126b 22686 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22687 {
22688 cp_lexer_consume_token (parser->lexer);
22689 location = cp_lexer_peek_token (parser->lexer)->location;
22690 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22691 node, lest it get absorbed into the surrounding block. */
22692 stmt = push_stmt_list ();
22693 cp_parser_compound_statement (parser, NULL, false);
22694 objc_build_finally_clause (location, pop_stmt_list (stmt));
22695 }
22696
22697 return objc_finish_try_stmt ();
22698}
22699
22700/* Parse an Objective-C synchronized statement.
22701
22702 objc-synchronized-stmt:
22703 @synchronized ( expression ) compound-statement
22704
22705 Returns NULL_TREE. */
22706
22707static tree
22708cp_parser_objc_synchronized_statement (cp_parser *parser) {
22709 location_t location;
22710 tree lock, stmt;
22711
c247dce0 22712 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
7a4e126b 22713
22714 location = cp_lexer_peek_token (parser->lexer)->location;
c247dce0 22715 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
98b326fd 22716 lock = cp_parser_expression (parser, false, NULL);
c247dce0 22717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7a4e126b 22718
22719 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22720 node, lest it get absorbed into the surrounding block. */
22721 stmt = push_stmt_list ();
22722 cp_parser_compound_statement (parser, NULL, false);
22723
22724 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22725}
22726
22727/* Parse an Objective-C throw statement.
22728
22729 objc-throw-stmt:
22730 @throw assignment-expression [opt] ;
22731
22732 Returns a constructed '@throw' statement. */
22733
22734static tree
22735cp_parser_objc_throw_statement (cp_parser *parser) {
22736 tree expr = NULL_TREE;
e60a6f7b 22737 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7a4e126b 22738
c247dce0 22739 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
7a4e126b 22740
22741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 22742 expr = cp_parser_assignment_expression (parser, false, NULL);
7a4e126b 22743
22744 cp_parser_consume_semicolon_at_end_of_statement (parser);
22745
e60a6f7b 22746 return objc_build_throw_stmt (loc, expr);
7a4e126b 22747}
22748
22749/* Parse an Objective-C statement. */
22750
22751static tree
22752cp_parser_objc_statement (cp_parser * parser) {
22753 /* Try to figure out what kind of declaration is present. */
22754 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22755
22756 switch (kwd->keyword)
22757 {
22758 case RID_AT_TRY:
22759 return cp_parser_objc_try_catch_finally_statement (parser);
22760 case RID_AT_SYNCHRONIZED:
22761 return cp_parser_objc_synchronized_statement (parser);
22762 case RID_AT_THROW:
22763 return cp_parser_objc_throw_statement (parser);
22764 default:
ccb59bb6 22765 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22766 kwd->u.value);
7a4e126b 22767 cp_parser_skip_to_end_of_block_or_statement (parser);
22768 }
22769
22770 return error_mark_node;
22771}
a336eb4b 22772
22773/* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22774 look ahead to see if an objc keyword follows the attributes. This
22775 is to detect the use of prefix attributes on ObjC @interface and
22776 @protocol. */
22777
22778static bool
22779cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22780{
22781 cp_lexer_save_tokens (parser->lexer);
22782 *attrib = cp_parser_attributes_opt (parser);
22783 gcc_assert (*attrib);
22784 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22785 {
22786 cp_lexer_commit_tokens (parser->lexer);
22787 return true;
22788 }
22789 cp_lexer_rollback_tokens (parser->lexer);
22790 return false;
22791}
86c110ac 22792
1d894bcf 22793/* This routine is a minimal replacement for
22794 c_parser_struct_declaration () used when parsing the list of
22795 types/names or ObjC++ properties. For example, when parsing the
22796 code
86c110ac 22797
1d894bcf 22798 @property (readonly) int a, b, c;
22799
22800 this function is responsible for parsing "int a, int b, int c" and
22801 returning the declarations as CHAIN of DECLs.
22802
22803 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22804 similar parsing. */
22805static tree
22806cp_parser_objc_struct_declaration (cp_parser *parser)
86c110ac 22807{
1d894bcf 22808 tree decls = NULL_TREE;
86c110ac 22809 cp_decl_specifier_seq declspecs;
1d894bcf 22810 int decl_class_or_enum_p;
22811 tree prefix_attributes;
86c110ac 22812
22813 cp_parser_decl_specifier_seq (parser,
1d894bcf 22814 CP_PARSER_FLAGS_NONE,
22815 &declspecs,
22816 &decl_class_or_enum_p);
22817
22818 if (declspecs.type == error_mark_node)
22819 return error_mark_node;
22820
22821 /* auto, register, static, extern, mutable. */
22822 if (declspecs.storage_class != sc_none)
22823 {
22824 cp_parser_error (parser, "invalid type for property");
22825 declspecs.storage_class = sc_none;
22826 }
22827
22828 /* __thread. */
22829 if (declspecs.specs[(int) ds_thread])
22830 {
22831 cp_parser_error (parser, "invalid type for property");
22832 declspecs.specs[(int) ds_thread] = 0;
22833 }
22834
22835 /* typedef. */
22836 if (declspecs.specs[(int) ds_typedef])
22837 {
22838 cp_parser_error (parser, "invalid type for property");
22839 declspecs.specs[(int) ds_typedef] = 0;
22840 }
22841
22842 prefix_attributes = declspecs.attributes;
22843 declspecs.attributes = NULL_TREE;
22844
86c110ac 22845 /* Keep going until we hit the `;' at the end of the declaration. */
22846 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22847 {
1d894bcf 22848 tree attributes, first_attribute, decl;
22849 cp_declarator *declarator;
86c110ac 22850 cp_token *token;
86c110ac 22851
1d894bcf 22852 /* Parse the declarator. */
22853 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22854 NULL, NULL, false);
22855
22856 /* Look for attributes that apply to the ivar. */
22857 attributes = cp_parser_attributes_opt (parser);
22858 /* Remember which attributes are prefix attributes and
22859 which are not. */
22860 first_attribute = attributes;
22861 /* Combine the attributes. */
22862 attributes = chainon (prefix_attributes, attributes);
22863
22864 decl = grokfield (declarator, &declspecs,
22865 NULL_TREE, /*init_const_expr_p=*/false,
22866 NULL_TREE, attributes);
22867
22868 if (decl == error_mark_node || decl == NULL_TREE)
22869 return error_mark_node;
22870
22871 /* Reset PREFIX_ATTRIBUTES. */
22872 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22873 attributes = TREE_CHAIN (attributes);
22874 if (attributes)
22875 TREE_CHAIN (attributes) = NULL_TREE;
22876
22877 DECL_CHAIN (decl) = decls;
22878 decls = decl;
22879
86c110ac 22880 token = cp_lexer_peek_token (parser->lexer);
22881 if (token->type == CPP_COMMA)
22882 {
22883 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22884 continue;
22885 }
1d894bcf 22886 else
86c110ac 22887 break;
22888 }
1d894bcf 22889 return decls;
86c110ac 22890}
22891
1d894bcf 22892/* Parse an Objective-C @property declaration. The syntax is:
22893
22894 objc-property-declaration:
22895 '@property' objc-property-attributes[opt] struct-declaration ;
22896
22897 objc-property-attributes:
22898 '(' objc-property-attribute-list ')'
22899
22900 objc-property-attribute-list:
22901 objc-property-attribute
22902 objc-property-attribute-list, objc-property-attribute
22903
22904 objc-property-attribute
22905 'getter' = identifier
22906 'setter' = identifier
22907 'readonly'
22908 'readwrite'
22909 'assign'
22910 'retain'
22911 'copy'
22912 'nonatomic'
22913
22914 For example:
22915 @property NSString *name;
22916 @property (readonly) id object;
22917 @property (retain, nonatomic, getter=getTheName) id name;
22918 @property int a, b, c;
22919
22920 PS: This function is identical to
7590f0e5 22921 c_parser_objc_at_property_declaration for C. Keep them in sync. */
86c110ac 22922static void
1d894bcf 22923cp_parser_objc_at_property_declaration (cp_parser *parser)
86c110ac 22924{
7590f0e5 22925 /* The following variables hold the attributes of the properties as
22926 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
22927 seen. When we see an attribute, we set them to 'true' (if they
22928 are boolean properties) or to the identifier (if they have an
22929 argument, ie, for getter and setter). Note that here we only
22930 parse the list of attributes, check the syntax and accumulate the
22931 attributes that we find. objc_add_property_declaration() will
22932 then process the information. */
22933 bool property_assign = false;
22934 bool property_copy = false;
22935 tree property_getter_ident = NULL_TREE;
22936 bool property_nonatomic = false;
22937 bool property_readonly = false;
22938 bool property_readwrite = false;
22939 bool property_retain = false;
22940 tree property_setter_ident = NULL_TREE;
7590f0e5 22941
22942 /* 'properties' is the list of properties that we read. Usually a
22943 single one, but maybe more (eg, in "@property int a, b, c;" there
22944 are three). */
1d894bcf 22945 tree properties;
22946 location_t loc;
7590f0e5 22947
1d894bcf 22948 loc = cp_lexer_peek_token (parser->lexer)->location;
86c110ac 22949
1d894bcf 22950 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
86c110ac 22951
1d894bcf 22952 /* Parse the optional attribute list... */
22953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22954 {
22955 /* Eat the '('. */
22956 cp_lexer_consume_token (parser->lexer);
22957
22958 while (true)
86c110ac 22959 {
1d894bcf 22960 bool syntax_error = false;
22961 cp_token *token = cp_lexer_peek_token (parser->lexer);
22962 enum rid keyword;
22963
22964 if (token->type != CPP_NAME)
22965 {
22966 cp_parser_error (parser, "expected identifier");
22967 break;
22968 }
22969 keyword = C_RID_CODE (token->u.value);
7590f0e5 22970 cp_lexer_consume_token (parser->lexer);
1d894bcf 22971 switch (keyword)
86c110ac 22972 {
7590f0e5 22973 case RID_ASSIGN: property_assign = true; break;
7590f0e5 22974 case RID_COPY: property_copy = true; break;
22975 case RID_NONATOMIC: property_nonatomic = true; break;
22976 case RID_READONLY: property_readonly = true; break;
22977 case RID_READWRITE: property_readwrite = true; break;
22978 case RID_RETAIN: property_retain = true; break;
22979
1d894bcf 22980 case RID_GETTER:
22981 case RID_SETTER:
1d894bcf 22982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22983 {
22984 cp_parser_error (parser,
22985 "getter/setter/ivar attribute must be followed by %<=%>");
22986 syntax_error = true;
22987 break;
22988 }
22989 cp_lexer_consume_token (parser->lexer); /* eat the = */
22990 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22991 {
22992 cp_parser_error (parser, "expected identifier");
22993 syntax_error = true;
22994 break;
22995 }
1d894bcf 22996 if (keyword == RID_SETTER)
86c110ac 22997 {
7590f0e5 22998 if (property_setter_ident != NULL_TREE)
22999 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23000 else
23001 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
1d894bcf 23002 cp_lexer_consume_token (parser->lexer);
7590f0e5 23003 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23004 cp_parser_error (parser, "setter name must terminate with %<:%>");
23005 else
23006 cp_lexer_consume_token (parser->lexer);
86c110ac 23007 }
9d9f5bb3 23008 else
1d894bcf 23009 {
7590f0e5 23010 if (property_getter_ident != NULL_TREE)
23011 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23012 else
23013 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
1d894bcf 23014 cp_lexer_consume_token (parser->lexer);
1d894bcf 23015 }
7590f0e5 23016 break;
23017 default:
23018 cp_parser_error (parser, "unknown property attribute");
1d894bcf 23019 syntax_error = true;
86c110ac 23020 break;
23021 }
1d894bcf 23022
23023 if (syntax_error)
23024 break;
23025
23026 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23027 cp_lexer_consume_token (parser->lexer);
23028 else
23029 break;
86c110ac 23030 }
1d894bcf 23031
23032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
86c110ac 23033 {
1d894bcf 23034 cp_parser_skip_to_closing_parenthesis (parser,
23035 /*recovering=*/true,
23036 /*or_comma=*/false,
23037 /*consume_paren=*/true);
86c110ac 23038 }
86c110ac 23039 }
23040
1d894bcf 23041 /* ... and the property declaration(s). */
23042 properties = cp_parser_objc_struct_declaration (parser);
86c110ac 23043
1d894bcf 23044 if (properties == error_mark_node)
23045 {
23046 cp_parser_skip_to_end_of_statement (parser);
23047 /* If the next token is now a `;', consume it. */
23048 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23049 cp_lexer_consume_token (parser->lexer);
23050 return;
23051 }
86c110ac 23052
1d894bcf 23053 if (properties == NULL_TREE)
23054 cp_parser_error (parser, "expected identifier");
23055 else
23056 {
23057 /* Comma-separated properties are chained together in
23058 reverse order; add them one by one. */
23059 properties = nreverse (properties);
23060
23061 for (; properties; properties = TREE_CHAIN (properties))
7590f0e5 23062 objc_add_property_declaration (loc, copy_node (properties),
23063 property_readonly, property_readwrite,
23064 property_assign, property_retain,
23065 property_copy, property_nonatomic,
9d9f5bb3 23066 property_getter_ident, property_setter_ident);
1d894bcf 23067 }
23068
23069 cp_parser_consume_semicolon_at_end_of_statement (parser);
86c110ac 23070}
e1f293c0 23071
23072/* Parse an Objective-C++ @synthesize declaration. The syntax is:
23073
23074 objc-synthesize-declaration:
23075 @synthesize objc-synthesize-identifier-list ;
23076
23077 objc-synthesize-identifier-list:
23078 objc-synthesize-identifier
23079 objc-synthesize-identifier-list, objc-synthesize-identifier
23080
23081 objc-synthesize-identifier
23082 identifier
23083 identifier = identifier
23084
23085 For example:
23086 @synthesize MyProperty;
23087 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23088
23089 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23090 for C. Keep them in sync.
23091*/
23092static void
23093cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23094{
23095 tree list = NULL_TREE;
23096 location_t loc;
23097 loc = cp_lexer_peek_token (parser->lexer)->location;
23098
23099 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23100 while (true)
23101 {
23102 tree property, ivar;
23103 property = cp_parser_identifier (parser);
23104 if (property == error_mark_node)
23105 {
23106 cp_parser_consume_semicolon_at_end_of_statement (parser);
23107 return;
23108 }
23109 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23110 {
23111 cp_lexer_consume_token (parser->lexer);
23112 ivar = cp_parser_identifier (parser);
23113 if (ivar == error_mark_node)
23114 {
23115 cp_parser_consume_semicolon_at_end_of_statement (parser);
23116 return;
23117 }
23118 }
23119 else
23120 ivar = NULL_TREE;
23121 list = chainon (list, build_tree_list (ivar, property));
23122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23123 cp_lexer_consume_token (parser->lexer);
23124 else
23125 break;
23126 }
23127 cp_parser_consume_semicolon_at_end_of_statement (parser);
23128 objc_add_synthesize_declaration (loc, list);
23129}
23130
23131/* Parse an Objective-C++ @dynamic declaration. The syntax is:
23132
23133 objc-dynamic-declaration:
23134 @dynamic identifier-list ;
23135
23136 For example:
23137 @dynamic MyProperty;
23138 @dynamic MyProperty, AnotherProperty;
23139
23140 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23141 for C. Keep them in sync.
23142*/
23143static void
23144cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23145{
23146 tree list = NULL_TREE;
23147 location_t loc;
23148 loc = cp_lexer_peek_token (parser->lexer)->location;
23149
23150 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23151 while (true)
23152 {
23153 tree property;
23154 property = cp_parser_identifier (parser);
23155 if (property == error_mark_node)
23156 {
23157 cp_parser_consume_semicolon_at_end_of_statement (parser);
23158 return;
23159 }
23160 list = chainon (list, build_tree_list (NULL, property));
23161 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23162 cp_lexer_consume_token (parser->lexer);
23163 else
23164 break;
23165 }
23166 cp_parser_consume_semicolon_at_end_of_statement (parser);
23167 objc_add_dynamic_declaration (loc, list);
23168}
23169
8487df40 23170\f
23171/* OpenMP 2.5 parsing routines. */
23172
8487df40 23173/* Returns name of the next clause.
23174 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23175 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23176 returned and the token is consumed. */
23177
23178static pragma_omp_clause
23179cp_parser_omp_clause_name (cp_parser *parser)
23180{
23181 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23182
23183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23184 result = PRAGMA_OMP_CLAUSE_IF;
23185 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23186 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23187 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23188 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23189 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23190 {
3369eb76 23191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 23192 const char *p = IDENTIFIER_POINTER (id);
23193
23194 switch (p[0])
23195 {
23196 case 'c':
fd6481cf 23197 if (!strcmp ("collapse", p))
23198 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23199 else if (!strcmp ("copyin", p))
8487df40 23200 result = PRAGMA_OMP_CLAUSE_COPYIN;
074ab442 23201 else if (!strcmp ("copyprivate", p))
8487df40 23202 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23203 break;
23204 case 'f':
23205 if (!strcmp ("firstprivate", p))
23206 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23207 break;
23208 case 'l':
23209 if (!strcmp ("lastprivate", p))
23210 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23211 break;
23212 case 'n':
23213 if (!strcmp ("nowait", p))
23214 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23215 else if (!strcmp ("num_threads", p))
23216 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23217 break;
23218 case 'o':
23219 if (!strcmp ("ordered", p))
23220 result = PRAGMA_OMP_CLAUSE_ORDERED;
23221 break;
23222 case 'r':
23223 if (!strcmp ("reduction", p))
23224 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23225 break;
23226 case 's':
23227 if (!strcmp ("schedule", p))
23228 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23229 else if (!strcmp ("shared", p))
23230 result = PRAGMA_OMP_CLAUSE_SHARED;
23231 break;
fd6481cf 23232 case 'u':
23233 if (!strcmp ("untied", p))
23234 result = PRAGMA_OMP_CLAUSE_UNTIED;
23235 break;
8487df40 23236 }
23237 }
0a3b29ad 23238
8487df40 23239 if (result != PRAGMA_OMP_CLAUSE_NONE)
23240 cp_lexer_consume_token (parser->lexer);
0a3b29ad 23241
8487df40 23242 return result;
23243}
b75b98aa 23244
8487df40 23245/* Validate that a clause of the given type does not already exist. */
b75b98aa 23246
23247static void
590c3166 23248check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
ad9ae192 23249 const char *name, location_t location)
b75b98aa 23250{
8487df40 23251 tree c;
b75b98aa 23252
8487df40 23253 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23254 if (OMP_CLAUSE_CODE (c) == code)
23255 {
ccb59bb6 23256 error_at (location, "too many %qs clauses", name);
8487df40 23257 break;
23258 }
23259}
b75b98aa 23260
8487df40 23261/* OpenMP 2.5:
23262 variable-list:
23263 identifier
23264 variable-list , identifier
23265
23266 In addition, we match a closing parenthesis. An opening parenthesis
23267 will have been consumed by the caller.
23268
23269 If KIND is nonzero, create the appropriate node and install the decl
23270 in OMP_CLAUSE_DECL and add the node to the head of the list.
23271
23272 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23273 return the list created. */
23274
23275static tree
23276cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23277 tree list)
23278{
ad9ae192 23279 cp_token *token;
8487df40 23280 while (1)
b75b98aa 23281 {
8487df40 23282 tree name, decl;
b75b98aa 23283
ad9ae192 23284 token = cp_lexer_peek_token (parser->lexer);
8487df40 23285 name = cp_parser_id_expression (parser, /*template_p=*/false,
23286 /*check_dependency_p=*/true,
23287 /*template_p=*/NULL,
197c9df7 23288 /*declarator_p=*/false,
130bb1d4 23289 /*optional_p=*/false);
8487df40 23290 if (name == error_mark_node)
23291 goto skip_comma;
23292
ad9ae192 23293 decl = cp_parser_lookup_name_simple (parser, name, token->location);
8487df40 23294 if (decl == error_mark_node)
c247dce0 23295 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23296 token->location);
8487df40 23297 else if (kind != 0)
23298 {
e60a6f7b 23299 tree u = build_omp_clause (token->location, kind);
8487df40 23300 OMP_CLAUSE_DECL (u) = decl;
23301 OMP_CLAUSE_CHAIN (u) = list;
23302 list = u;
23303 }
23304 else
23305 list = tree_cons (decl, NULL_TREE, list);
23306
23307 get_comma:
23308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23309 break;
23310 cp_lexer_consume_token (parser->lexer);
23311 }
23312
c247dce0 23313 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 23314 {
23315 int ending;
23316
23317 /* Try to resync to an unnested comma. Copied from
23318 cp_parser_parenthesized_expression_list. */
23319 skip_comma:
23320 ending = cp_parser_skip_to_closing_parenthesis (parser,
23321 /*recovering=*/true,
23322 /*or_comma=*/true,
23323 /*consume_paren=*/true);
23324 if (ending < 0)
23325 goto get_comma;
23326 }
23327
23328 return list;
23329}
23330
23331/* Similarly, but expect leading and trailing parenthesis. This is a very
23332 common case for omp clauses. */
23333
23334static tree
23335cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23336{
c247dce0 23337 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23338 return cp_parser_omp_var_list_no_open (parser, kind, list);
23339 return list;
23340}
23341
fd6481cf 23342/* OpenMP 3.0:
23343 collapse ( constant-expression ) */
23344
23345static tree
ad9ae192 23346cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
fd6481cf 23347{
23348 tree c, num;
23349 location_t loc;
23350 HOST_WIDE_INT n;
23351
23352 loc = cp_lexer_peek_token (parser->lexer)->location;
c247dce0 23353 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
fd6481cf 23354 return list;
23355
23356 num = cp_parser_constant_expression (parser, false, NULL);
23357
c247dce0 23358 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
fd6481cf 23359 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23360 /*or_comma=*/false,
23361 /*consume_paren=*/true);
23362
23363 if (num == error_mark_node)
23364 return list;
23365 num = fold_non_dependent_expr (num);
23366 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23367 || !host_integerp (num, 0)
23368 || (n = tree_low_cst (num, 0)) <= 0
23369 || (int) n != n)
23370 {
ccb59bb6 23371 error_at (loc, "collapse argument needs positive constant integer expression");
fd6481cf 23372 return list;
23373 }
23374
ad9ae192 23375 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
e60a6f7b 23376 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
fd6481cf 23377 OMP_CLAUSE_CHAIN (c) = list;
23378 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23379
23380 return c;
23381}
23382
8487df40 23383/* OpenMP 2.5:
23384 default ( shared | none ) */
23385
23386static tree
ad9ae192 23387cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
8487df40 23388{
23389 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23390 tree c;
23391
c247dce0 23392 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23393 return list;
23394 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23395 {
3369eb76 23396 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 23397 const char *p = IDENTIFIER_POINTER (id);
23398
23399 switch (p[0])
23400 {
23401 case 'n':
23402 if (strcmp ("none", p) != 0)
23403 goto invalid_kind;
23404 kind = OMP_CLAUSE_DEFAULT_NONE;
23405 break;
23406
23407 case 's':
23408 if (strcmp ("shared", p) != 0)
23409 goto invalid_kind;
23410 kind = OMP_CLAUSE_DEFAULT_SHARED;
23411 break;
23412
23413 default:
23414 goto invalid_kind;
23415 }
23416
23417 cp_lexer_consume_token (parser->lexer);
b75b98aa 23418 }
23419 else
8487df40 23420 {
23421 invalid_kind:
23422 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23423 }
b75b98aa 23424
c247dce0 23425 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 23426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23427 /*or_comma=*/false,
23428 /*consume_paren=*/true);
074ab442 23429
8487df40 23430 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23431 return list;
b75b98aa 23432
ad9ae192 23433 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
e60a6f7b 23434 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
8487df40 23435 OMP_CLAUSE_CHAIN (c) = list;
23436 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
b75b98aa 23437
8487df40 23438 return c;
b75b98aa 23439}
23440
8487df40 23441/* OpenMP 2.5:
23442 if ( expression ) */
b75b98aa 23443
8487df40 23444static tree
ad9ae192 23445cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
b75b98aa 23446{
8487df40 23447 tree t, c;
b75b98aa 23448
c247dce0 23449 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23450 return list;
b75b98aa 23451
8487df40 23452 t = cp_parser_condition (parser);
23453
23454 if (t == error_mark_node
c247dce0 23455 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 23456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23457 /*or_comma=*/false,
23458 /*consume_paren=*/true);
23459
ad9ae192 23460 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
8487df40 23461
e60a6f7b 23462 c = build_omp_clause (location, OMP_CLAUSE_IF);
8487df40 23463 OMP_CLAUSE_IF_EXPR (c) = t;
23464 OMP_CLAUSE_CHAIN (c) = list;
23465
23466 return c;
23467}
23468
23469/* OpenMP 2.5:
23470 nowait */
23471
23472static tree
ad9ae192 23473cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23474 tree list, location_t location)
8487df40 23475{
23476 tree c;
23477
ad9ae192 23478 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
8487df40 23479
e60a6f7b 23480 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
8487df40 23481 OMP_CLAUSE_CHAIN (c) = list;
23482 return c;
23483}
23484
23485/* OpenMP 2.5:
23486 num_threads ( expression ) */
23487
23488static tree
ad9ae192 23489cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23490 location_t location)
8487df40 23491{
23492 tree t, c;
23493
c247dce0 23494 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23495 return list;
23496
98b326fd 23497 t = cp_parser_expression (parser, false, NULL);
8487df40 23498
23499 if (t == error_mark_node
c247dce0 23500 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 23501 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23502 /*or_comma=*/false,
23503 /*consume_paren=*/true);
23504
ad9ae192 23505 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23506 "num_threads", location);
8487df40 23507
e60a6f7b 23508 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
8487df40 23509 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23510 OMP_CLAUSE_CHAIN (c) = list;
23511
23512 return c;
23513}
23514
23515/* OpenMP 2.5:
23516 ordered */
23517
23518static tree
ad9ae192 23519cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23520 tree list, location_t location)
8487df40 23521{
23522 tree c;
23523
ad9ae192 23524 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23525 "ordered", location);
8487df40 23526
e60a6f7b 23527 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
8487df40 23528 OMP_CLAUSE_CHAIN (c) = list;
23529 return c;
23530}
23531
23532/* OpenMP 2.5:
23533 reduction ( reduction-operator : variable-list )
23534
23535 reduction-operator:
23536 One of: + * - & ^ | && || */
23537
23538static tree
23539cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23540{
23541 enum tree_code code;
23542 tree nlist, c;
23543
c247dce0 23544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23545 return list;
23546
23547 switch (cp_lexer_peek_token (parser->lexer)->type)
b75b98aa 23548 {
8487df40 23549 case CPP_PLUS:
23550 code = PLUS_EXPR;
23551 break;
23552 case CPP_MULT:
23553 code = MULT_EXPR;
23554 break;
23555 case CPP_MINUS:
23556 code = MINUS_EXPR;
23557 break;
23558 case CPP_AND:
23559 code = BIT_AND_EXPR;
23560 break;
23561 case CPP_XOR:
23562 code = BIT_XOR_EXPR;
23563 break;
23564 case CPP_OR:
23565 code = BIT_IOR_EXPR;
23566 break;
23567 case CPP_AND_AND:
23568 code = TRUTH_ANDIF_EXPR;
23569 break;
23570 case CPP_OR_OR:
23571 code = TRUTH_ORIF_EXPR;
b75b98aa 23572 break;
b75b98aa 23573 default:
361cc318 23574 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23575 "%<|%>, %<&&%>, or %<||%>");
8487df40 23576 resync_fail:
23577 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23578 /*or_comma=*/false,
23579 /*consume_paren=*/true);
23580 return list;
23581 }
23582 cp_lexer_consume_token (parser->lexer);
23583
c247dce0 23584 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
8487df40 23585 goto resync_fail;
23586
23587 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23588 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23589 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23590
23591 return nlist;
23592}
23593
23594/* OpenMP 2.5:
23595 schedule ( schedule-kind )
23596 schedule ( schedule-kind , expression )
23597
23598 schedule-kind:
fd6481cf 23599 static | dynamic | guided | runtime | auto */
8487df40 23600
23601static tree
ad9ae192 23602cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
8487df40 23603{
23604 tree c, t;
23605
c247dce0 23606 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 23607 return list;
23608
e60a6f7b 23609 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
8487df40 23610
23611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23612 {
3369eb76 23613 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 23614 const char *p = IDENTIFIER_POINTER (id);
23615
23616 switch (p[0])
23617 {
23618 case 'd':
23619 if (strcmp ("dynamic", p) != 0)
23620 goto invalid_kind;
23621 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23622 break;
23623
074ab442 23624 case 'g':
8487df40 23625 if (strcmp ("guided", p) != 0)
23626 goto invalid_kind;
23627 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23628 break;
23629
23630 case 'r':
23631 if (strcmp ("runtime", p) != 0)
23632 goto invalid_kind;
23633 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23634 break;
23635
23636 default:
23637 goto invalid_kind;
23638 }
23639 }
23640 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23641 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
fd6481cf 23642 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23643 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8487df40 23644 else
23645 goto invalid_kind;
23646 cp_lexer_consume_token (parser->lexer);
23647
23648 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23649 {
ad9ae192 23650 cp_token *token;
8487df40 23651 cp_lexer_consume_token (parser->lexer);
23652
ad9ae192 23653 token = cp_lexer_peek_token (parser->lexer);
98b326fd 23654 t = cp_parser_assignment_expression (parser, false, NULL);
8487df40 23655
23656 if (t == error_mark_node)
23657 goto resync_fail;
23658 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
ccb59bb6 23659 error_at (token->location, "schedule %<runtime%> does not take "
23660 "a %<chunk_size%> parameter");
fd6481cf 23661 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
ccb59bb6 23662 error_at (token->location, "schedule %<auto%> does not take "
23663 "a %<chunk_size%> parameter");
8487df40 23664 else
23665 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23666
c247dce0 23667 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 23668 goto resync_fail;
23669 }
c247dce0 23670 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
8487df40 23671 goto resync_fail;
23672
ad9ae192 23673 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
8487df40 23674 OMP_CLAUSE_CHAIN (c) = list;
23675 return c;
23676
23677 invalid_kind:
23678 cp_parser_error (parser, "invalid schedule kind");
23679 resync_fail:
23680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23681 /*or_comma=*/false,
23682 /*consume_paren=*/true);
23683 return list;
23684}
23685
fd6481cf 23686/* OpenMP 3.0:
23687 untied */
23688
23689static tree
ad9ae192 23690cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23691 tree list, location_t location)
fd6481cf 23692{
23693 tree c;
23694
ad9ae192 23695 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
fd6481cf 23696
e60a6f7b 23697 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
fd6481cf 23698 OMP_CLAUSE_CHAIN (c) = list;
23699 return c;
23700}
23701
8487df40 23702/* Parse all OpenMP clauses. The set clauses allowed by the directive
23703 is a bitmask in MASK. Return the list of clauses found; the result
23704 of clause default goes in *pdefault. */
23705
23706static tree
23707cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23708 const char *where, cp_token *pragma_tok)
23709{
23710 tree clauses = NULL;
77c8bed2 23711 bool first = true;
ad9ae192 23712 cp_token *token = NULL;
8487df40 23713
23714 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23715 {
77c8bed2 23716 pragma_omp_clause c_kind;
8487df40 23717 const char *c_name;
23718 tree prev = clauses;
23719
77c8bed2 23720 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23721 cp_lexer_consume_token (parser->lexer);
23722
ad9ae192 23723 token = cp_lexer_peek_token (parser->lexer);
77c8bed2 23724 c_kind = cp_parser_omp_clause_name (parser);
23725 first = false;
23726
8487df40 23727 switch (c_kind)
23728 {
fd6481cf 23729 case PRAGMA_OMP_CLAUSE_COLLAPSE:
ad9ae192 23730 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23731 token->location);
fd6481cf 23732 c_name = "collapse";
23733 break;
8487df40 23734 case PRAGMA_OMP_CLAUSE_COPYIN:
23735 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23736 c_name = "copyin";
23737 break;
23738 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23739 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23740 clauses);
23741 c_name = "copyprivate";
23742 break;
23743 case PRAGMA_OMP_CLAUSE_DEFAULT:
ad9ae192 23744 clauses = cp_parser_omp_clause_default (parser, clauses,
23745 token->location);
8487df40 23746 c_name = "default";
23747 break;
23748 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23749 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23750 clauses);
23751 c_name = "firstprivate";
23752 break;
23753 case PRAGMA_OMP_CLAUSE_IF:
ad9ae192 23754 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
8487df40 23755 c_name = "if";
23756 break;
23757 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23758 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23759 clauses);
23760 c_name = "lastprivate";
23761 break;
23762 case PRAGMA_OMP_CLAUSE_NOWAIT:
ad9ae192 23763 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
8487df40 23764 c_name = "nowait";
23765 break;
23766 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
ad9ae192 23767 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23768 token->location);
8487df40 23769 c_name = "num_threads";
23770 break;
23771 case PRAGMA_OMP_CLAUSE_ORDERED:
ad9ae192 23772 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23773 token->location);
8487df40 23774 c_name = "ordered";
23775 break;
23776 case PRAGMA_OMP_CLAUSE_PRIVATE:
23777 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23778 clauses);
23779 c_name = "private";
23780 break;
23781 case PRAGMA_OMP_CLAUSE_REDUCTION:
23782 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23783 c_name = "reduction";
23784 break;
23785 case PRAGMA_OMP_CLAUSE_SCHEDULE:
ad9ae192 23786 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23787 token->location);
8487df40 23788 c_name = "schedule";
23789 break;
23790 case PRAGMA_OMP_CLAUSE_SHARED:
23791 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23792 clauses);
23793 c_name = "shared";
23794 break;
fd6481cf 23795 case PRAGMA_OMP_CLAUSE_UNTIED:
ad9ae192 23796 clauses = cp_parser_omp_clause_untied (parser, clauses,
23797 token->location);
fd6481cf 23798 c_name = "nowait";
23799 break;
8487df40 23800 default:
23801 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23802 goto saw_error;
23803 }
23804
23805 if (((mask >> c_kind) & 1) == 0)
23806 {
23807 /* Remove the invalid clause(s) from the list to avoid
23808 confusing the rest of the compiler. */
23809 clauses = prev;
ccb59bb6 23810 error_at (token->location, "%qs is not valid for %qs", c_name, where);
8487df40 23811 }
23812 }
23813 saw_error:
23814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23815 return finish_omp_clauses (clauses);
23816}
23817
23818/* OpenMP 2.5:
23819 structured-block:
23820 statement
23821
23822 In practice, we're also interested in adding the statement to an
23823 outer node. So it is convenient if we work around the fact that
23824 cp_parser_statement calls add_stmt. */
23825
23826static unsigned
23827cp_parser_begin_omp_structured_block (cp_parser *parser)
23828{
23829 unsigned save = parser->in_statement;
23830
23831 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23832 This preserves the "not within loop or switch" style error messages
23833 for nonsense cases like
23834 void foo() {
23835 #pragma omp single
23836 break;
23837 }
23838 */
23839 if (parser->in_statement)
23840 parser->in_statement = IN_OMP_BLOCK;
23841
23842 return save;
23843}
23844
23845static void
23846cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23847{
23848 parser->in_statement = save;
23849}
23850
23851static tree
23852cp_parser_omp_structured_block (cp_parser *parser)
23853{
23854 tree stmt = begin_omp_structured_block ();
23855 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23856
e534436e 23857 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 23858
23859 cp_parser_end_omp_structured_block (parser, save);
23860 return finish_omp_structured_block (stmt);
23861}
23862
23863/* OpenMP 2.5:
23864 # pragma omp atomic new-line
23865 expression-stmt
23866
23867 expression-stmt:
23868 x binop= expr | x++ | ++x | x-- | --x
23869 binop:
23870 +, *, -, /, &, ^, |, <<, >>
23871
23872 where x is an lvalue expression with scalar type. */
23873
23874static void
23875cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23876{
23877 tree lhs, rhs;
23878 enum tree_code code;
23879
23880 cp_parser_require_pragma_eol (parser, pragma_tok);
23881
23882 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 23883 /*cast_p=*/false, NULL);
8487df40 23884 switch (TREE_CODE (lhs))
23885 {
23886 case ERROR_MARK:
23887 goto saw_error;
23888
23889 case PREINCREMENT_EXPR:
23890 case POSTINCREMENT_EXPR:
23891 lhs = TREE_OPERAND (lhs, 0);
23892 code = PLUS_EXPR;
23893 rhs = integer_one_node;
23894 break;
23895
23896 case PREDECREMENT_EXPR:
23897 case POSTDECREMENT_EXPR:
23898 lhs = TREE_OPERAND (lhs, 0);
23899 code = MINUS_EXPR;
23900 rhs = integer_one_node;
23901 break;
23902
db28a91b 23903 case COMPOUND_EXPR:
23904 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
23905 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
23906 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
23907 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
23908 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
23909 (TREE_OPERAND (lhs, 1), 0), 0)))
23910 == BOOLEAN_TYPE)
23911 /* Undo effects of boolean_increment for post {in,de}crement. */
23912 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
23913 /* FALLTHRU */
23914 case MODIFY_EXPR:
23915 if (TREE_CODE (lhs) == MODIFY_EXPR
23916 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
23917 {
23918 /* Undo effects of boolean_increment. */
23919 if (integer_onep (TREE_OPERAND (lhs, 1)))
23920 {
23921 /* This is pre or post increment. */
23922 rhs = TREE_OPERAND (lhs, 1);
23923 lhs = TREE_OPERAND (lhs, 0);
23924 code = NOP_EXPR;
23925 break;
23926 }
23927 }
23928 /* FALLTHRU */
8487df40 23929 default:
23930 switch (cp_lexer_peek_token (parser->lexer)->type)
23931 {
23932 case CPP_MULT_EQ:
23933 code = MULT_EXPR;
23934 break;
23935 case CPP_DIV_EQ:
23936 code = TRUNC_DIV_EXPR;
23937 break;
23938 case CPP_PLUS_EQ:
23939 code = PLUS_EXPR;
23940 break;
23941 case CPP_MINUS_EQ:
23942 code = MINUS_EXPR;
23943 break;
23944 case CPP_LSHIFT_EQ:
23945 code = LSHIFT_EXPR;
23946 break;
23947 case CPP_RSHIFT_EQ:
23948 code = RSHIFT_EXPR;
23949 break;
23950 case CPP_AND_EQ:
23951 code = BIT_AND_EXPR;
23952 break;
23953 case CPP_OR_EQ:
23954 code = BIT_IOR_EXPR;
23955 break;
23956 case CPP_XOR_EQ:
23957 code = BIT_XOR_EXPR;
23958 break;
23959 default:
23960 cp_parser_error (parser,
23961 "invalid operator for %<#pragma omp atomic%>");
23962 goto saw_error;
23963 }
23964 cp_lexer_consume_token (parser->lexer);
23965
98b326fd 23966 rhs = cp_parser_expression (parser, false, NULL);
8487df40 23967 if (rhs == error_mark_node)
23968 goto saw_error;
23969 break;
23970 }
23971 finish_omp_atomic (code, lhs, rhs);
23972 cp_parser_consume_semicolon_at_end_of_statement (parser);
23973 return;
23974
23975 saw_error:
23976 cp_parser_skip_to_end_of_block_or_statement (parser);
23977}
23978
23979
23980/* OpenMP 2.5:
074ab442 23981 # pragma omp barrier new-line */
8487df40 23982
23983static void
23984cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
23985{
23986 cp_parser_require_pragma_eol (parser, pragma_tok);
23987 finish_omp_barrier ();
23988}
23989
23990/* OpenMP 2.5:
23991 # pragma omp critical [(name)] new-line
074ab442 23992 structured-block */
8487df40 23993
23994static tree
23995cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
23996{
23997 tree stmt, name = NULL;
23998
23999 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24000 {
24001 cp_lexer_consume_token (parser->lexer);
24002
24003 name = cp_parser_identifier (parser);
074ab442 24004
8487df40 24005 if (name == error_mark_node
c247dce0 24006 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 24007 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24008 /*or_comma=*/false,
24009 /*consume_paren=*/true);
24010 if (name == error_mark_node)
24011 name = NULL;
24012 }
24013 cp_parser_require_pragma_eol (parser, pragma_tok);
24014
24015 stmt = cp_parser_omp_structured_block (parser);
e60a6f7b 24016 return c_finish_omp_critical (input_location, stmt, name);
8487df40 24017}
24018
24019/* OpenMP 2.5:
24020 # pragma omp flush flush-vars[opt] new-line
24021
24022 flush-vars:
24023 ( variable-list ) */
24024
24025static void
24026cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24027{
24028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a52f99a9 24029 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 24030 cp_parser_require_pragma_eol (parser, pragma_tok);
24031
24032 finish_omp_flush ();
24033}
24034
fd6481cf 24035/* Helper function, to parse omp for increment expression. */
8487df40 24036
24037static tree
fd6481cf 24038cp_parser_omp_for_cond (cp_parser *parser, tree decl)
8487df40 24039{
4390875c 24040 tree cond = cp_parser_binary_expression (parser, false, true,
24041 PREC_NOT_OPERATOR, NULL);
24042 bool overloaded_p;
8487df40 24043
4390875c 24044 if (cond == error_mark_node
24045 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8487df40 24046 {
fd6481cf 24047 cp_parser_skip_to_end_of_statement (parser);
24048 return error_mark_node;
8487df40 24049 }
8487df40 24050
4390875c 24051 switch (TREE_CODE (cond))
fd6481cf 24052 {
fd6481cf 24053 case GT_EXPR:
24054 case GE_EXPR:
4390875c 24055 case LT_EXPR:
24056 case LE_EXPR:
fd6481cf 24057 break;
24058 default:
fd6481cf 24059 return error_mark_node;
24060 }
24061
4390875c 24062 /* If decl is an iterator, preserve LHS and RHS of the relational
24063 expr until finish_omp_for. */
24064 if (decl
24065 && (type_dependent_expression_p (decl)
24066 || CLASS_TYPE_P (TREE_TYPE (decl))))
24067 return cond;
8487df40 24068
4390875c 24069 return build_x_binary_op (TREE_CODE (cond),
24070 TREE_OPERAND (cond, 0), ERROR_MARK,
24071 TREE_OPERAND (cond, 1), ERROR_MARK,
24072 &overloaded_p, tf_warning_or_error);
fd6481cf 24073}
8487df40 24074
fd6481cf 24075/* Helper function, to parse omp for increment expression. */
24076
24077static tree
24078cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24079{
24080 cp_token *token = cp_lexer_peek_token (parser->lexer);
24081 enum tree_code op;
24082 tree lhs, rhs;
24083 cp_id_kind idk;
24084 bool decl_first;
24085
24086 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24087 {
24088 op = (token->type == CPP_PLUS_PLUS
24089 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24090 cp_lexer_consume_token (parser->lexer);
98b326fd 24091 lhs = cp_parser_cast_expression (parser, false, false, NULL);
fd6481cf 24092 if (lhs != decl)
24093 return error_mark_node;
24094 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24095 }
24096
24097 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24098 if (lhs != decl)
24099 return error_mark_node;
24100
24101 token = cp_lexer_peek_token (parser->lexer);
24102 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24103 {
24104 op = (token->type == CPP_PLUS_PLUS
24105 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24106 cp_lexer_consume_token (parser->lexer);
24107 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24108 }
24109
24110 op = cp_parser_assignment_operator_opt (parser);
24111 if (op == ERROR_MARK)
24112 return error_mark_node;
24113
24114 if (op != NOP_EXPR)
24115 {
98b326fd 24116 rhs = cp_parser_assignment_expression (parser, false, NULL);
fd6481cf 24117 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24118 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24119 }
24120
4390875c 24121 lhs = cp_parser_binary_expression (parser, false, false,
98b326fd 24122 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 24123 token = cp_lexer_peek_token (parser->lexer);
24124 decl_first = lhs == decl;
24125 if (decl_first)
24126 lhs = NULL_TREE;
24127 if (token->type != CPP_PLUS
24128 && token->type != CPP_MINUS)
24129 return error_mark_node;
24130
24131 do
24132 {
24133 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24134 cp_lexer_consume_token (parser->lexer);
4390875c 24135 rhs = cp_parser_binary_expression (parser, false, false,
98b326fd 24136 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 24137 token = cp_lexer_peek_token (parser->lexer);
24138 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
8487df40 24139 {
fd6481cf 24140 if (lhs == NULL_TREE)
24141 {
24142 if (op == PLUS_EXPR)
24143 lhs = rhs;
24144 else
24145 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24146 }
24147 else
24148 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24149 NULL, tf_warning_or_error);
24150 }
24151 }
24152 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
8487df40 24153
fd6481cf 24154 if (!decl_first)
24155 {
24156 if (rhs != decl || op == MINUS_EXPR)
24157 return error_mark_node;
24158 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24159 }
24160 else
24161 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24162
24163 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24164}
24165
bde357c8 24166/* Parse the restricted form of the for statement allowed by OpenMP. */
fd6481cf 24167
24168static tree
24169cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24170{
24171 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
9bc1bf88 24172 tree real_decl, initv, condv, incrv, declv;
fd6481cf 24173 tree this_pre_body, cl;
24174 location_t loc_first;
24175 bool collapse_err = false;
24176 int i, collapse = 1, nbraces = 0;
9bc1bf88 24177 VEC(tree,gc) *for_block = make_tree_vector ();
fd6481cf 24178
24179 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24180 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24181 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24182
24183 gcc_assert (collapse >= 1);
24184
24185 declv = make_tree_vec (collapse);
24186 initv = make_tree_vec (collapse);
24187 condv = make_tree_vec (collapse);
24188 incrv = make_tree_vec (collapse);
24189
24190 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24191
24192 for (i = 0; i < collapse; i++)
24193 {
24194 int bracecount = 0;
24195 bool add_private_clause = false;
24196 location_t loc;
24197
24198 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24199 {
24200 cp_parser_error (parser, "for statement expected");
24201 return NULL;
24202 }
24203 loc = cp_lexer_consume_token (parser->lexer)->location;
24204
c247dce0 24205 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
fd6481cf 24206 return NULL;
24207
24208 init = decl = real_decl = NULL;
24209 this_pre_body = push_stmt_list ();
24210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24211 {
507c9a59 24212 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24213
24214 init-expr:
24215 var = lb
24216 integer-type var = lb
24217 random-access-iterator-type var = lb
24218 pointer-type var = lb
24219 */
fd6481cf 24220 cp_decl_specifier_seq type_specifiers;
24221
24222 /* First, try to parse as an initialized declaration. See
24223 cp_parser_condition, from whence the bulk of this is copied. */
24224
24225 cp_parser_parse_tentatively (parser);
c44f7faf 24226 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
638569c5 24227 /*is_trailing_return=*/false,
fd6481cf 24228 &type_specifiers);
507c9a59 24229 if (cp_parser_parse_definitely (parser))
8487df40 24230 {
507c9a59 24231 /* If parsing a type specifier seq succeeded, then this
24232 MUST be a initialized declaration. */
fd6481cf 24233 tree asm_specification, attributes;
24234 cp_declarator *declarator;
24235
24236 declarator = cp_parser_declarator (parser,
24237 CP_PARSER_DECLARATOR_NAMED,
24238 /*ctor_dtor_or_conv_p=*/NULL,
24239 /*parenthesized_p=*/NULL,
24240 /*member_p=*/false);
24241 attributes = cp_parser_attributes_opt (parser);
24242 asm_specification = cp_parser_asm_specification_opt (parser);
24243
507c9a59 24244 if (declarator == cp_error_declarator)
24245 cp_parser_skip_to_end_of_statement (parser);
24246
24247 else
fd6481cf 24248 {
304ac225 24249 tree pushed_scope, auto_node;
fd6481cf 24250
24251 decl = start_decl (declarator, &type_specifiers,
304ac225 24252 SD_INITIALIZED, attributes,
fd6481cf 24253 /*prefix_attributes=*/NULL_TREE,
24254 &pushed_scope);
24255
304ac225 24256 auto_node = type_uses_auto (TREE_TYPE (decl));
507c9a59 24257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24258 {
24259 if (cp_lexer_next_token_is (parser->lexer,
24260 CPP_OPEN_PAREN))
24261 error ("parenthesized initialization is not allowed in "
24262 "OpenMP %<for%> loop");
24263 else
24264 /* Trigger an error. */
c247dce0 24265 cp_parser_require (parser, CPP_EQ, RT_EQ);
507c9a59 24266
24267 init = error_mark_node;
24268 cp_parser_skip_to_end_of_statement (parser);
24269 }
24270 else if (CLASS_TYPE_P (TREE_TYPE (decl))
304ac225 24271 || type_dependent_expression_p (decl)
24272 || auto_node)
fd6481cf 24273 {
f82f1250 24274 bool is_direct_init, is_non_constant_init;
fd6481cf 24275
24276 init = cp_parser_initializer (parser,
f82f1250 24277 &is_direct_init,
fd6481cf 24278 &is_non_constant_init);
24279
3986b463 24280 if (auto_node && describable_type (init))
304ac225 24281 {
24282 TREE_TYPE (decl)
24283 = do_auto_deduction (TREE_TYPE (decl), init,
24284 auto_node);
24285
24286 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24287 && !type_dependent_expression_p (decl))
24288 goto non_class;
24289 }
24290
fd6481cf 24291 cp_finish_decl (decl, init, !is_non_constant_init,
24292 asm_specification,
24293 LOOKUP_ONLYCONVERTING);
24294 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24295 {
9bc1bf88 24296 VEC_safe_push (tree, gc, for_block, this_pre_body);
fd6481cf 24297 init = NULL_TREE;
24298 }
24299 else
24300 init = pop_stmt_list (this_pre_body);
24301 this_pre_body = NULL_TREE;
24302 }
24303 else
24304 {
507c9a59 24305 /* Consume '='. */
24306 cp_lexer_consume_token (parser->lexer);
98b326fd 24307 init = cp_parser_assignment_expression (parser, false, NULL);
8487df40 24308
304ac225 24309 non_class:
fd6481cf 24310 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24311 init = error_mark_node;
24312 else
24313 cp_finish_decl (decl, NULL_TREE,
24314 /*init_const_expr_p=*/false,
24315 asm_specification,
24316 LOOKUP_ONLYCONVERTING);
24317 }
8487df40 24318
fd6481cf 24319 if (pushed_scope)
24320 pop_scope (pushed_scope);
24321 }
24322 }
507c9a59 24323 else
fd6481cf 24324 {
24325 cp_id_kind idk;
507c9a59 24326 /* If parsing a type specifier sequence failed, then
24327 this MUST be a simple expression. */
fd6481cf 24328 cp_parser_parse_tentatively (parser);
24329 decl = cp_parser_primary_expression (parser, false, false,
24330 false, &idk);
24331 if (!cp_parser_error_occurred (parser)
24332 && decl
24333 && DECL_P (decl)
24334 && CLASS_TYPE_P (TREE_TYPE (decl)))
24335 {
24336 tree rhs;
24337
24338 cp_parser_parse_definitely (parser);
c247dce0 24339 cp_parser_require (parser, CPP_EQ, RT_EQ);
98b326fd 24340 rhs = cp_parser_assignment_expression (parser, false, NULL);
fd6481cf 24341 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24342 rhs,
24343 tf_warning_or_error));
24344 add_private_clause = true;
24345 }
a46a7e42 24346 else
fd6481cf 24347 {
24348 decl = NULL;
24349 cp_parser_abort_tentative_parse (parser);
98b326fd 24350 init = cp_parser_expression (parser, false, NULL);
fd6481cf 24351 if (init)
24352 {
24353 if (TREE_CODE (init) == MODIFY_EXPR
24354 || TREE_CODE (init) == MODOP_EXPR)
24355 real_decl = TREE_OPERAND (init, 0);
24356 }
24357 }
24358 }
24359 }
c247dce0 24360 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
fd6481cf 24361 if (this_pre_body)
24362 {
24363 this_pre_body = pop_stmt_list (this_pre_body);
24364 if (pre_body)
24365 {
24366 tree t = pre_body;
24367 pre_body = push_stmt_list ();
24368 add_stmt (t);
24369 add_stmt (this_pre_body);
24370 pre_body = pop_stmt_list (pre_body);
24371 }
24372 else
24373 pre_body = this_pre_body;
24374 }
8487df40 24375
fd6481cf 24376 if (decl)
24377 real_decl = decl;
24378 if (par_clauses != NULL && real_decl != NULL_TREE)
24379 {
24380 tree *c;
24381 for (c = par_clauses; *c ; )
24382 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24383 && OMP_CLAUSE_DECL (*c) == real_decl)
24384 {
ccb59bb6 24385 error_at (loc, "iteration variable %qD"
24386 " should not be firstprivate", real_decl);
fd6481cf 24387 *c = OMP_CLAUSE_CHAIN (*c);
24388 }
24389 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24390 && OMP_CLAUSE_DECL (*c) == real_decl)
24391 {
24392 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24393 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
e60a6f7b 24394 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
fd6481cf 24395 OMP_CLAUSE_DECL (l) = real_decl;
24396 OMP_CLAUSE_CHAIN (l) = clauses;
24397 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24398 clauses = l;
24399 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24400 CP_OMP_CLAUSE_INFO (*c) = NULL;
24401 add_private_clause = false;
24402 }
24403 else
24404 {
24405 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24406 && OMP_CLAUSE_DECL (*c) == real_decl)
24407 add_private_clause = false;
24408 c = &OMP_CLAUSE_CHAIN (*c);
24409 }
24410 }
24411
24412 if (add_private_clause)
24413 {
24414 tree c;
24415 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24416 {
24417 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24418 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24419 && OMP_CLAUSE_DECL (c) == decl)
24420 break;
24421 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24422 && OMP_CLAUSE_DECL (c) == decl)
ccb59bb6 24423 error_at (loc, "iteration variable %qD "
24424 "should not be firstprivate",
24425 decl);
fd6481cf 24426 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24427 && OMP_CLAUSE_DECL (c) == decl)
ccb59bb6 24428 error_at (loc, "iteration variable %qD should not be reduction",
24429 decl);
fd6481cf 24430 }
24431 if (c == NULL)
24432 {
e60a6f7b 24433 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
fd6481cf 24434 OMP_CLAUSE_DECL (c) = decl;
24435 c = finish_omp_clauses (c);
24436 if (c)
24437 {
24438 OMP_CLAUSE_CHAIN (c) = clauses;
24439 clauses = c;
24440 }
8487df40 24441 }
24442 }
24443
fd6481cf 24444 cond = NULL;
24445 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
e060ba36 24446 cond = cp_parser_omp_for_cond (parser, decl);
c247dce0 24447 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8487df40 24448
fd6481cf 24449 incr = NULL;
24450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24451 {
24452 /* If decl is an iterator, preserve the operator on decl
24453 until finish_omp_for. */
24454 if (decl
24455 && (type_dependent_expression_p (decl)
24456 || CLASS_TYPE_P (TREE_TYPE (decl))))
24457 incr = cp_parser_omp_for_incr (parser, decl);
24458 else
98b326fd 24459 incr = cp_parser_expression (parser, false, NULL);
fd6481cf 24460 }
8487df40 24461
c247dce0 24462 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
fd6481cf 24463 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24464 /*or_comma=*/false,
24465 /*consume_paren=*/true);
8487df40 24466
fd6481cf 24467 TREE_VEC_ELT (declv, i) = decl;
24468 TREE_VEC_ELT (initv, i) = init;
24469 TREE_VEC_ELT (condv, i) = cond;
24470 TREE_VEC_ELT (incrv, i) = incr;
24471
24472 if (i == collapse - 1)
24473 break;
24474
24475 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24476 in between the collapsed for loops to be still considered perfectly
24477 nested. Hopefully the final version clarifies this.
24478 For now handle (multiple) {'s and empty statements. */
24479 cp_parser_parse_tentatively (parser);
24480 do
24481 {
24482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24483 break;
24484 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24485 {
24486 cp_lexer_consume_token (parser->lexer);
24487 bracecount++;
24488 }
24489 else if (bracecount
24490 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24491 cp_lexer_consume_token (parser->lexer);
24492 else
24493 {
24494 loc = cp_lexer_peek_token (parser->lexer)->location;
ccb59bb6 24495 error_at (loc, "not enough collapsed for loops");
fd6481cf 24496 collapse_err = true;
24497 cp_parser_abort_tentative_parse (parser);
24498 declv = NULL_TREE;
24499 break;
24500 }
24501 }
24502 while (1);
24503
24504 if (declv)
24505 {
24506 cp_parser_parse_definitely (parser);
24507 nbraces += bracecount;
24508 }
24509 }
8487df40 24510
24511 /* Note that we saved the original contents of this flag when we entered
24512 the structured block, and so we don't need to re-save it here. */
24513 parser->in_statement = IN_OMP_FOR;
24514
24515 /* Note that the grammar doesn't call for a structured block here,
24516 though the loop as a whole is a structured block. */
24517 body = push_stmt_list ();
e534436e 24518 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 24519 body = pop_stmt_list (body);
24520
fd6481cf 24521 if (declv == NULL_TREE)
24522 ret = NULL_TREE;
24523 else
24524 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24525 pre_body, clauses);
24526
24527 while (nbraces)
24528 {
24529 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24530 {
24531 cp_lexer_consume_token (parser->lexer);
24532 nbraces--;
24533 }
24534 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24535 cp_lexer_consume_token (parser->lexer);
24536 else
24537 {
24538 if (!collapse_err)
eef0ab03 24539 {
ccb59bb6 24540 error_at (cp_lexer_peek_token (parser->lexer)->location,
24541 "collapsed loops not perfectly nested");
eef0ab03 24542 }
fd6481cf 24543 collapse_err = true;
24544 cp_parser_statement_seq_opt (parser, NULL);
1c1f5172 24545 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24546 break;
fd6481cf 24547 }
24548 }
24549
9bc1bf88 24550 while (!VEC_empty (tree, for_block))
24551 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24552 release_tree_vector (for_block);
fd6481cf 24553
24554 return ret;
8487df40 24555}
24556
24557/* OpenMP 2.5:
24558 #pragma omp for for-clause[optseq] new-line
074ab442 24559 for-loop */
8487df40 24560
24561#define OMP_FOR_CLAUSE_MASK \
24562 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24563 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24564 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24565 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24566 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24567 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
fd6481cf 24568 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24569 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
8487df40 24570
24571static tree
24572cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24573{
24574 tree clauses, sb, ret;
24575 unsigned int save;
24576
24577 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24578 "#pragma omp for", pragma_tok);
24579
24580 sb = begin_omp_structured_block ();
24581 save = cp_parser_begin_omp_structured_block (parser);
24582
fd6481cf 24583 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
8487df40 24584
24585 cp_parser_end_omp_structured_block (parser, save);
24586 add_stmt (finish_omp_structured_block (sb));
24587
24588 return ret;
24589}
24590
24591/* OpenMP 2.5:
24592 # pragma omp master new-line
074ab442 24593 structured-block */
8487df40 24594
24595static tree
24596cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24597{
24598 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 24599 return c_finish_omp_master (input_location,
24600 cp_parser_omp_structured_block (parser));
8487df40 24601}
24602
24603/* OpenMP 2.5:
24604 # pragma omp ordered new-line
074ab442 24605 structured-block */
8487df40 24606
24607static tree
24608cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24609{
e60a6f7b 24610 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 24611 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 24612 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
8487df40 24613}
24614
24615/* OpenMP 2.5:
24616
24617 section-scope:
24618 { section-sequence }
24619
24620 section-sequence:
24621 section-directive[opt] structured-block
24622 section-sequence section-directive structured-block */
24623
24624static tree
24625cp_parser_omp_sections_scope (cp_parser *parser)
24626{
24627 tree stmt, substmt;
24628 bool error_suppress = false;
24629 cp_token *tok;
24630
c247dce0 24631 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8487df40 24632 return NULL_TREE;
24633
24634 stmt = push_stmt_list ();
24635
24636 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24637 {
24638 unsigned save;
24639
24640 substmt = begin_omp_structured_block ();
24641 save = cp_parser_begin_omp_structured_block (parser);
24642
24643 while (1)
24644 {
e534436e 24645 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 24646
24647 tok = cp_lexer_peek_token (parser->lexer);
24648 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24649 break;
24650 if (tok->type == CPP_CLOSE_BRACE)
24651 break;
24652 if (tok->type == CPP_EOF)
24653 break;
24654 }
24655
24656 cp_parser_end_omp_structured_block (parser, save);
24657 substmt = finish_omp_structured_block (substmt);
24658 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24659 add_stmt (substmt);
24660 }
24661
24662 while (1)
24663 {
24664 tok = cp_lexer_peek_token (parser->lexer);
24665 if (tok->type == CPP_CLOSE_BRACE)
24666 break;
24667 if (tok->type == CPP_EOF)
24668 break;
24669
24670 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24671 {
24672 cp_lexer_consume_token (parser->lexer);
24673 cp_parser_require_pragma_eol (parser, tok);
24674 error_suppress = false;
24675 }
24676 else if (!error_suppress)
24677 {
24678 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24679 error_suppress = true;
24680 }
24681
24682 substmt = cp_parser_omp_structured_block (parser);
24683 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24684 add_stmt (substmt);
24685 }
c247dce0 24686 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8487df40 24687
24688 substmt = pop_stmt_list (stmt);
24689
24690 stmt = make_node (OMP_SECTIONS);
24691 TREE_TYPE (stmt) = void_type_node;
24692 OMP_SECTIONS_BODY (stmt) = substmt;
24693
24694 add_stmt (stmt);
24695 return stmt;
24696}
24697
24698/* OpenMP 2.5:
24699 # pragma omp sections sections-clause[optseq] newline
074ab442 24700 sections-scope */
8487df40 24701
24702#define OMP_SECTIONS_CLAUSE_MASK \
24703 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24704 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24705 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24706 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24707 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24708
24709static tree
24710cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24711{
24712 tree clauses, ret;
24713
24714 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24715 "#pragma omp sections", pragma_tok);
24716
24717 ret = cp_parser_omp_sections_scope (parser);
24718 if (ret)
24719 OMP_SECTIONS_CLAUSES (ret) = clauses;
24720
24721 return ret;
24722}
24723
24724/* OpenMP 2.5:
24725 # pragma parallel parallel-clause new-line
24726 # pragma parallel for parallel-for-clause new-line
074ab442 24727 # pragma parallel sections parallel-sections-clause new-line */
8487df40 24728
24729#define OMP_PARALLEL_CLAUSE_MASK \
24730 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24731 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24732 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24733 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24734 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24735 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24736 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24737 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24738
24739static tree
24740cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24741{
24742 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24743 const char *p_name = "#pragma omp parallel";
24744 tree stmt, clauses, par_clause, ws_clause, block;
24745 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24746 unsigned int save;
e60a6f7b 24747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 24748
24749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24750 {
24751 cp_lexer_consume_token (parser->lexer);
24752 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24753 p_name = "#pragma omp parallel for";
24754 mask |= OMP_FOR_CLAUSE_MASK;
24755 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24756 }
24757 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24758 {
3369eb76 24759 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 24760 const char *p = IDENTIFIER_POINTER (id);
24761 if (strcmp (p, "sections") == 0)
24762 {
24763 cp_lexer_consume_token (parser->lexer);
24764 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24765 p_name = "#pragma omp parallel sections";
24766 mask |= OMP_SECTIONS_CLAUSE_MASK;
24767 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24768 }
24769 }
24770
24771 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24772 block = begin_omp_parallel ();
24773 save = cp_parser_begin_omp_structured_block (parser);
24774
24775 switch (p_kind)
24776 {
24777 case PRAGMA_OMP_PARALLEL:
355efab1 24778 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 24779 par_clause = clauses;
24780 break;
24781
24782 case PRAGMA_OMP_PARALLEL_FOR:
e60a6f7b 24783 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
fd6481cf 24784 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
8487df40 24785 break;
24786
24787 case PRAGMA_OMP_PARALLEL_SECTIONS:
e60a6f7b 24788 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8487df40 24789 stmt = cp_parser_omp_sections_scope (parser);
24790 if (stmt)
24791 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24792 break;
24793
24794 default:
24795 gcc_unreachable ();
24796 }
24797
24798 cp_parser_end_omp_structured_block (parser, save);
87f7c31e 24799 stmt = finish_omp_parallel (par_clause, block);
24800 if (p_kind != PRAGMA_OMP_PARALLEL)
24801 OMP_PARALLEL_COMBINED (stmt) = 1;
24802 return stmt;
8487df40 24803}
24804
24805/* OpenMP 2.5:
24806 # pragma omp single single-clause[optseq] new-line
074ab442 24807 structured-block */
8487df40 24808
24809#define OMP_SINGLE_CLAUSE_MASK \
24810 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24811 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24812 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24813 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24814
24815static tree
24816cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24817{
24818 tree stmt = make_node (OMP_SINGLE);
24819 TREE_TYPE (stmt) = void_type_node;
24820
24821 OMP_SINGLE_CLAUSES (stmt)
24822 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24823 "#pragma omp single", pragma_tok);
24824 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24825
24826 return add_stmt (stmt);
24827}
24828
fd6481cf 24829/* OpenMP 3.0:
24830 # pragma omp task task-clause[optseq] new-line
24831 structured-block */
24832
24833#define OMP_TASK_CLAUSE_MASK \
24834 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24835 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24836 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24837 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24838 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24839 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24840
24841static tree
24842cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24843{
24844 tree clauses, block;
24845 unsigned int save;
24846
24847 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24848 "#pragma omp task", pragma_tok);
24849 block = begin_omp_task ();
24850 save = cp_parser_begin_omp_structured_block (parser);
24851 cp_parser_statement (parser, NULL_TREE, false, NULL);
24852 cp_parser_end_omp_structured_block (parser, save);
24853 return finish_omp_task (clauses, block);
24854}
24855
24856/* OpenMP 3.0:
24857 # pragma omp taskwait new-line */
24858
24859static void
24860cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24861{
24862 cp_parser_require_pragma_eol (parser, pragma_tok);
24863 finish_omp_taskwait ();
24864}
24865
8487df40 24866/* OpenMP 2.5:
24867 # pragma omp threadprivate (variable-list) */
24868
24869static void
24870cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24871{
24872 tree vars;
24873
a52f99a9 24874 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 24875 cp_parser_require_pragma_eol (parser, pragma_tok);
24876
8487df40 24877 finish_omp_threadprivate (vars);
24878}
24879
24880/* Main entry point to OpenMP statement pragmas. */
24881
24882static void
24883cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24884{
24885 tree stmt;
24886
24887 switch (pragma_tok->pragma_kind)
24888 {
24889 case PRAGMA_OMP_ATOMIC:
24890 cp_parser_omp_atomic (parser, pragma_tok);
24891 return;
24892 case PRAGMA_OMP_CRITICAL:
24893 stmt = cp_parser_omp_critical (parser, pragma_tok);
24894 break;
24895 case PRAGMA_OMP_FOR:
24896 stmt = cp_parser_omp_for (parser, pragma_tok);
24897 break;
24898 case PRAGMA_OMP_MASTER:
24899 stmt = cp_parser_omp_master (parser, pragma_tok);
24900 break;
24901 case PRAGMA_OMP_ORDERED:
24902 stmt = cp_parser_omp_ordered (parser, pragma_tok);
24903 break;
24904 case PRAGMA_OMP_PARALLEL:
24905 stmt = cp_parser_omp_parallel (parser, pragma_tok);
24906 break;
24907 case PRAGMA_OMP_SECTIONS:
24908 stmt = cp_parser_omp_sections (parser, pragma_tok);
24909 break;
24910 case PRAGMA_OMP_SINGLE:
24911 stmt = cp_parser_omp_single (parser, pragma_tok);
24912 break;
fd6481cf 24913 case PRAGMA_OMP_TASK:
24914 stmt = cp_parser_omp_task (parser, pragma_tok);
24915 break;
8487df40 24916 default:
24917 gcc_unreachable ();
24918 }
24919
24920 if (stmt)
24921 SET_EXPR_LOCATION (stmt, pragma_tok->location);
24922}
24923\f
24924/* The parser. */
24925
24926static GTY (()) cp_parser *the_parser;
24927
24928\f
24929/* Special handling for the first token or line in the file. The first
24930 thing in the file might be #pragma GCC pch_preprocess, which loads a
24931 PCH file, which is a GC collection point. So we need to handle this
24932 first pragma without benefit of an existing lexer structure.
24933
074ab442 24934 Always returns one token to the caller in *FIRST_TOKEN. This is
8487df40 24935 either the true first token of the file, or the first token after
24936 the initial pragma. */
24937
24938static void
24939cp_parser_initial_pragma (cp_token *first_token)
24940{
24941 tree name = NULL;
24942
24943 cp_lexer_get_preprocessor_token (NULL, first_token);
24944 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
24945 return;
24946
24947 cp_lexer_get_preprocessor_token (NULL, first_token);
24948 if (first_token->type == CPP_STRING)
24949 {
3369eb76 24950 name = first_token->u.value;
8487df40 24951
24952 cp_lexer_get_preprocessor_token (NULL, first_token);
24953 if (first_token->type != CPP_PRAGMA_EOL)
ccb59bb6 24954 error_at (first_token->location,
24955 "junk at end of %<#pragma GCC pch_preprocess%>");
8487df40 24956 }
24957 else
ccb59bb6 24958 error_at (first_token->location, "expected string literal");
8487df40 24959
24960 /* Skip to the end of the pragma. */
24961 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
24962 cp_lexer_get_preprocessor_token (NULL, first_token);
24963
8487df40 24964 /* Now actually load the PCH file. */
24965 if (name)
24966 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
b5262297 24967
24968 /* Read one more token to return to our caller. We have to do this
24969 after reading the PCH file in, since its pointers have to be
24970 live. */
24971 cp_lexer_get_preprocessor_token (NULL, first_token);
8487df40 24972}
24973
24974/* Normal parsing of a pragma token. Here we can (and must) use the
24975 regular lexer. */
24976
24977static bool
24978cp_parser_pragma (cp_parser *parser, enum pragma_context context)
24979{
24980 cp_token *pragma_tok;
24981 unsigned int id;
24982
24983 pragma_tok = cp_lexer_consume_token (parser->lexer);
24984 gcc_assert (pragma_tok->type == CPP_PRAGMA);
24985 parser->lexer->in_pragma = true;
24986
24987 id = pragma_tok->pragma_kind;
24988 switch (id)
24989 {
24990 case PRAGMA_GCC_PCH_PREPROCESS:
ccb59bb6 24991 error_at (pragma_tok->location,
24992 "%<#pragma GCC pch_preprocess%> must be first");
8487df40 24993 break;
24994
24995 case PRAGMA_OMP_BARRIER:
24996 switch (context)
24997 {
24998 case pragma_compound:
24999 cp_parser_omp_barrier (parser, pragma_tok);
25000 return false;
25001 case pragma_stmt:
ccb59bb6 25002 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25003 "used in compound statements");
8487df40 25004 break;
25005 default:
25006 goto bad_stmt;
25007 }
25008 break;
25009
25010 case PRAGMA_OMP_FLUSH:
25011 switch (context)
25012 {
25013 case pragma_compound:
25014 cp_parser_omp_flush (parser, pragma_tok);
25015 return false;
25016 case pragma_stmt:
ccb59bb6 25017 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25018 "used in compound statements");
8487df40 25019 break;
25020 default:
25021 goto bad_stmt;
25022 }
25023 break;
25024
fd6481cf 25025 case PRAGMA_OMP_TASKWAIT:
25026 switch (context)
25027 {
25028 case pragma_compound:
25029 cp_parser_omp_taskwait (parser, pragma_tok);
25030 return false;
25031 case pragma_stmt:
ccb59bb6 25032 error_at (pragma_tok->location,
25033 "%<#pragma omp taskwait%> may only be "
25034 "used in compound statements");
fd6481cf 25035 break;
25036 default:
25037 goto bad_stmt;
25038 }
25039 break;
25040
8487df40 25041 case PRAGMA_OMP_THREADPRIVATE:
25042 cp_parser_omp_threadprivate (parser, pragma_tok);
25043 return false;
25044
25045 case PRAGMA_OMP_ATOMIC:
25046 case PRAGMA_OMP_CRITICAL:
25047 case PRAGMA_OMP_FOR:
25048 case PRAGMA_OMP_MASTER:
25049 case PRAGMA_OMP_ORDERED:
25050 case PRAGMA_OMP_PARALLEL:
25051 case PRAGMA_OMP_SECTIONS:
25052 case PRAGMA_OMP_SINGLE:
fd6481cf 25053 case PRAGMA_OMP_TASK:
8487df40 25054 if (context == pragma_external)
25055 goto bad_stmt;
25056 cp_parser_omp_construct (parser, pragma_tok);
25057 return true;
25058
25059 case PRAGMA_OMP_SECTION:
ccb59bb6 25060 error_at (pragma_tok->location,
25061 "%<#pragma omp section%> may only be used in "
25062 "%<#pragma omp sections%> construct");
8487df40 25063 break;
25064
25065 default:
25066 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25067 c_invoke_pragma_handler (id);
25068 break;
25069
25070 bad_stmt:
25071 cp_parser_error (parser, "expected declaration specifiers");
b75b98aa 25072 break;
25073 }
25074
25075 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25076 return false;
25077}
25078
25079/* The interface the pragma parsers have to the lexer. */
25080
25081enum cpp_ttype
25082pragma_lex (tree *value)
25083{
25084 cp_token *tok;
25085 enum cpp_ttype ret;
25086
25087 tok = cp_lexer_peek_token (the_parser->lexer);
25088
25089 ret = tok->type;
3369eb76 25090 *value = tok->u.value;
b75b98aa 25091
25092 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25093 ret = CPP_EOF;
25094 else if (ret == CPP_STRING)
25095 *value = cp_parser_string_literal (the_parser, false, false);
25096 else
25097 {
25098 cp_lexer_consume_token (the_parser->lexer);
25099 if (ret == CPP_KEYWORD)
25100 ret = CPP_NAME;
25101 }
25102
25103 return ret;
25104}
25105
25106\f
0a3b29ad 25107/* External interface. */
25108
40109983 25109/* Parse one entire translation unit. */
0a3b29ad 25110
40109983 25111void
25112c_parse_file (void)
0a3b29ad 25113{
393b349a 25114 static bool already_called = false;
25115
25116 if (already_called)
25117 {
25118 sorry ("inter-module optimizations not implemented for C++");
25119 return;
25120 }
25121 already_called = true;
0a3b29ad 25122
25123 the_parser = cp_parser_new ();
4cab8273 25124 push_deferring_access_checks (flag_access_control
25125 ? dk_no_deferred : dk_no_check);
8e9e8d76 25126 cp_parser_translation_unit (the_parser);
0a3b29ad 25127 the_parser = NULL;
0a3b29ad 25128}
25129
0a3b29ad 25130#include "gt-cp-parser.h"