]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/parser.c
* output.h (__gcc_host_wide_int__): Move to hwint.h.
[thirdparty/gcc.git] / gcc / cp / parser.c
CommitLineData
0a3b29ad 1/* C++ Parser.
7f602bca 2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
66468a32 3 2005, 2007, 2008, 2009, 2010, 2011, 2012 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"
6198e8f6 26#include "timevar.h"
0a3b29ad 27#include "cpplib.h"
28#include "tree.h"
29#include "cp-tree.h"
ca82e026 30#include "intl.h"
7bedc3a0 31#include "c-family/c-pragma.h"
0a3b29ad 32#include "decl.h"
33#include "flags.h"
852f689e 34#include "diagnostic-core.h"
4b9b2871 35#include "target.h"
56af936e 36#include "cgraph.h"
7bedc3a0 37#include "c-family/c-common.h"
79408174 38#include "c-family/c-objc.h"
9227b6fc 39#include "plugin.h"
b3145af5 40#include "tree-pretty-print.h"
41#include "parser.h"
0a3b29ad 42
43\f
44/* The lexer. */
45
00d26680 46/* The cp_lexer_* routines mediate between the lexer proper (in libcpp
47 and c-lex.c) and the C++ parser. */
0a3b29ad 48
b7bf20db 49static cp_token eof_token =
da4e72c7 50{
b3145af5 51 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
da4e72c7 52};
19273cc2 53
c247dce0 54/* The various kinds of non integral constant we encounter. */
55typedef enum non_integral_constant {
c61e1212 56 NIC_NONE,
c247dce0 57 /* floating-point literal */
58 NIC_FLOAT,
59 /* %<this%> */
60 NIC_THIS,
61 /* %<__FUNCTION__%> */
62 NIC_FUNC_NAME,
63 /* %<__PRETTY_FUNCTION__%> */
64 NIC_PRETTY_FUNC,
65 /* %<__func__%> */
66 NIC_C99_FUNC,
67 /* "%<va_arg%> */
68 NIC_VA_ARG,
69 /* a cast */
70 NIC_CAST,
71 /* %<typeid%> operator */
72 NIC_TYPEID,
73 /* non-constant compound literals */
74 NIC_NCC,
75 /* a function call */
76 NIC_FUNC_CALL,
77 /* an increment */
78 NIC_INC,
79 /* an decrement */
80 NIC_DEC,
81 /* an array reference */
82 NIC_ARRAY_REF,
83 /* %<->%> */
84 NIC_ARROW,
85 /* %<.%> */
86 NIC_POINT,
87 /* the address of a label */
88 NIC_ADDR_LABEL,
89 /* %<*%> */
90 NIC_STAR,
91 /* %<&%> */
92 NIC_ADDR,
93 /* %<++%> */
94 NIC_PREINCREMENT,
95 /* %<--%> */
96 NIC_PREDECREMENT,
97 /* %<new%> */
98 NIC_NEW,
99 /* %<delete%> */
100 NIC_DEL,
101 /* calls to overloaded operators */
102 NIC_OVERLOADED,
103 /* an assignment */
104 NIC_ASSIGNMENT,
105 /* a comma operator */
106 NIC_COMMA,
107 /* a call to a constructor */
4c0315d0 108 NIC_CONSTRUCTOR,
109 /* a transaction expression */
110 NIC_TRANSACTION
c247dce0 111} non_integral_constant;
112
113/* The various kinds of errors about name-lookup failing. */
114typedef enum name_lookup_error {
115 /* NULL */
116 NLE_NULL,
117 /* is not a type */
118 NLE_TYPE,
119 /* is not a class or namespace */
120 NLE_CXX98,
121 /* is not a class, namespace, or enumeration */
122 NLE_NOT_CXX98
123} name_lookup_error;
124
125/* The various kinds of required token */
126typedef enum required_token {
c61e1212 127 RT_NONE,
128 RT_SEMICOLON, /* ';' */
c247dce0 129 RT_OPEN_PAREN, /* '(' */
130 RT_CLOSE_BRACE, /* '}' */
131 RT_OPEN_BRACE, /* '{' */
132 RT_CLOSE_SQUARE, /* ']' */
133 RT_OPEN_SQUARE, /* '[' */
134 RT_COMMA, /* ',' */
135 RT_SCOPE, /* '::' */
136 RT_LESS, /* '<' */
137 RT_GREATER, /* '>' */
138 RT_EQ, /* '=' */
139 RT_ELLIPSIS, /* '...' */
140 RT_MULT, /* '*' */
141 RT_COMPL, /* '~' */
142 RT_COLON, /* ':' */
143 RT_COLON_SCOPE, /* ':' or '::' */
144 RT_CLOSE_PAREN, /* ')' */
145 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
146 RT_PRAGMA_EOL, /* end of line */
147 RT_NAME, /* identifier */
148
149 /* The type is CPP_KEYWORD */
150 RT_NEW, /* new */
151 RT_DELETE, /* delete */
152 RT_RETURN, /* return */
153 RT_WHILE, /* while */
154 RT_EXTERN, /* extern */
155 RT_STATIC_ASSERT, /* static_assert */
156 RT_DECLTYPE, /* decltype */
157 RT_OPERATOR, /* operator */
158 RT_CLASS, /* class */
159 RT_TEMPLATE, /* template */
160 RT_NAMESPACE, /* namespace */
161 RT_USING, /* using */
162 RT_ASM, /* asm */
163 RT_TRY, /* try */
164 RT_CATCH, /* catch */
165 RT_THROW, /* throw */
166 RT_LABEL, /* __label__ */
167 RT_AT_TRY, /* @try */
168 RT_AT_SYNCHRONIZED, /* @synchronized */
169 RT_AT_THROW, /* @throw */
170
171 RT_SELECT, /* selection-statement */
172 RT_INTERATION, /* iteration-statement */
173 RT_JUMP, /* jump-statement */
174 RT_CLASS_KEY, /* class-key */
4c0315d0 175 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
176 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
177 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
178 RT_TRANSACTION_CANCEL /* __transaction_cancel */
c247dce0 179} required_token;
180
0a3b29ad 181/* Prototypes. */
182
573aba85 183static cp_lexer *cp_lexer_new_main
45baea8b 184 (void);
0a3b29ad 185static cp_lexer *cp_lexer_new_from_tokens
00d26680 186 (cp_token_cache *tokens);
187static void cp_lexer_destroy
188 (cp_lexer *);
0a3b29ad 189static int cp_lexer_saving_tokens
45baea8b 190 (const cp_lexer *);
19273cc2 191static cp_token *cp_lexer_token_at
192 (cp_lexer *, cp_token_position);
0a3b29ad 193static void cp_lexer_get_preprocessor_token
45baea8b 194 (cp_lexer *, cp_token *);
00d26680 195static inline cp_token *cp_lexer_peek_token
196 (cp_lexer *);
0a3b29ad 197static cp_token *cp_lexer_peek_nth_token
45baea8b 198 (cp_lexer *, size_t);
2370b5bf 199static inline bool cp_lexer_next_token_is
45baea8b 200 (cp_lexer *, enum cpp_ttype);
0a3b29ad 201static bool cp_lexer_next_token_is_not
45baea8b 202 (cp_lexer *, enum cpp_ttype);
0a3b29ad 203static bool cp_lexer_next_token_is_keyword
45baea8b 204 (cp_lexer *, enum rid);
ccb84981 205static cp_token *cp_lexer_consume_token
45baea8b 206 (cp_lexer *);
0a3b29ad 207static void cp_lexer_purge_token
208 (cp_lexer *);
209static void cp_lexer_purge_tokens_after
19273cc2 210 (cp_lexer *, cp_token_position);
0a3b29ad 211static void cp_lexer_save_tokens
45baea8b 212 (cp_lexer *);
0a3b29ad 213static void cp_lexer_commit_tokens
45baea8b 214 (cp_lexer *);
0a3b29ad 215static void cp_lexer_rollback_tokens
45baea8b 216 (cp_lexer *);
0a3b29ad 217static void cp_lexer_print_token
45baea8b 218 (FILE *, cp_token *);
ccb84981 219static inline bool cp_lexer_debugging_p
45baea8b 220 (cp_lexer *);
0a3b29ad 221static void cp_lexer_start_debugging
45baea8b 222 (cp_lexer *) ATTRIBUTE_UNUSED;
0a3b29ad 223static void cp_lexer_stop_debugging
45baea8b 224 (cp_lexer *) ATTRIBUTE_UNUSED;
0a3b29ad 225
00d26680 226static cp_token_cache *cp_token_cache_new
227 (cp_token *, cp_token *);
228
b75b98aa 229static void cp_parser_initial_pragma
230 (cp_token *);
231
244db24d 232static tree cp_literal_operator_id
233 (const char *);
234
0a3b29ad 235/* Manifest constants. */
83adc189 236#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
19273cc2 237#define CP_SAVED_TOKEN_STACK 5
0a3b29ad 238
0a3b29ad 239/* Variables. */
240
241/* The stream to which debugging output should be written. */
242static FILE *cp_lexer_debug_stream;
243
48d94ede 244/* Nonzero if we are parsing an unevaluated operand: an operand to
245 sizeof, typeof, or alignof. */
246int cp_unevaluated_operand;
247
24224d3d 248/* Dump up to NUM tokens in BUFFER to FILE starting with token
249 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
250 first token in BUFFER. If NUM is 0, dump all the tokens. If
251 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
252 highlighted by surrounding it in [[ ]]. */
b3145af5 253
24224d3d 254static void
255cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
256 cp_token *start_token, unsigned num,
257 cp_token *curr_token)
b3145af5 258{
24224d3d 259 unsigned i, nprinted;
b3145af5 260 cp_token *token;
24224d3d 261 bool do_print;
b3145af5 262
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
24224d3d 265 if (buffer == NULL)
266 return;
267
b3145af5 268 if (num == 0)
269 num = VEC_length (cp_token, buffer);
270
24224d3d 271 if (start_token == NULL)
272 start_token = VEC_address (cp_token, buffer);
273
274 if (start_token > VEC_address (cp_token, buffer))
b3145af5 275 {
24224d3d 276 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
277 fprintf (file, " ... ");
278 }
279
280 do_print = false;
281 nprinted = 0;
282 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
283 {
284 if (token == start_token)
285 do_print = true;
286
287 if (!do_print)
288 continue;
289
290 nprinted++;
291 if (token == curr_token)
292 fprintf (file, "[[");
293
b3145af5 294 cp_lexer_print_token (file, token);
24224d3d 295
296 if (token == curr_token)
297 fprintf (file, "]]");
298
b3145af5 299 switch (token->type)
300 {
301 case CPP_SEMICOLON:
302 case CPP_OPEN_BRACE:
303 case CPP_CLOSE_BRACE:
304 case CPP_EOF:
305 fputc ('\n', file);
306 break;
307
308 default:
309 fputc (' ', file);
310 }
311 }
312
313 if (i == num && i < VEC_length (cp_token, buffer))
314 {
315 fprintf (file, " ... ");
316 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
317 VEC_length (cp_token, buffer) - 1));
318 }
319
320 fprintf (file, "\n");
321}
322
323
324/* Dump all tokens in BUFFER to stderr. */
325
326void
327cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
328{
24224d3d 329 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
330}
331
332
333/* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
334 description for T. */
335
336static void
337cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
338{
339 if (t)
340 {
341 fprintf (file, "%s: ", desc);
342 print_node_brief (file, "", t, 0);
343 }
344}
345
346
347/* Dump parser context C to FILE. */
348
349static void
350cp_debug_print_context (FILE *file, cp_parser_context *c)
351{
352 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
353 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
354 print_node_brief (file, "", c->object_type, 0);
355 fprintf (file, "}\n");
356}
357
358
359/* Print the stack of parsing contexts to FILE starting with FIRST. */
360
361static void
362cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
363{
364 unsigned i;
365 cp_parser_context *c;
366
367 fprintf (file, "Parsing context stack:\n");
368 for (i = 0, c = first; c; c = c->next, i++)
369 {
370 fprintf (file, "\t#%u: ", i);
371 cp_debug_print_context (file, c);
372 }
373}
374
375
376/* Print the value of FLAG to FILE. DESC is a string describing the flag. */
377
378static void
379cp_debug_print_flag (FILE *file, const char *desc, bool flag)
380{
381 if (flag)
382 fprintf (file, "%s: true\n", desc);
383}
384
385
386/* Print an unparsed function entry UF to FILE. */
387
388static void
389cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
390{
391 unsigned i;
392 cp_default_arg_entry *default_arg_fn;
393 tree fn;
394
395 fprintf (file, "\tFunctions with default args:\n");
396 for (i = 0;
397 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
398 default_arg_fn);
399 i++)
400 {
401 fprintf (file, "\t\tClass type: ");
402 print_node_brief (file, "", default_arg_fn->class_type, 0);
403 fprintf (file, "\t\tDeclaration: ");
404 print_node_brief (file, "", default_arg_fn->decl, 0);
405 fprintf (file, "\n");
406 }
407
408 fprintf (file, "\n\tFunctions with definitions that require "
409 "post-processing\n\t\t");
410 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
411 {
412 print_node_brief (file, "", fn, 0);
413 fprintf (file, " ");
414 }
415 fprintf (file, "\n");
416
417 fprintf (file, "\n\tNon-static data members with initializers that require "
418 "post-processing\n\t\t");
419 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
420 {
421 print_node_brief (file, "", fn, 0);
422 fprintf (file, " ");
423 }
424 fprintf (file, "\n");
425}
426
427
428/* Print the stack of unparsed member functions S to FILE. */
429
430static void
431cp_debug_print_unparsed_queues (FILE *file,
432 VEC(cp_unparsed_functions_entry, gc) *s)
433{
434 unsigned i;
435 cp_unparsed_functions_entry *uf;
436
437 fprintf (file, "Unparsed functions\n");
438 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
439 {
440 fprintf (file, "#%u:\n", i);
441 cp_debug_print_unparsed_function (file, uf);
442 }
443}
444
445
446/* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
447 the given PARSER. If FILE is NULL, the output is printed on stderr. */
448
449static void
450cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
451{
452 cp_token *next_token, *first_token, *start_token;
453
454 if (file == NULL)
455 file = stderr;
456
457 next_token = parser->lexer->next_token;
458 first_token = VEC_address (cp_token, parser->lexer->buffer);
459 start_token = (next_token > first_token + window_size / 2)
460 ? next_token - window_size / 2
461 : first_token;
462 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
463 next_token);
464}
465
466
467/* Dump debugging information for the given PARSER. If FILE is NULL,
468 the output is printed on stderr. */
469
470void
471cp_debug_parser (FILE *file, cp_parser *parser)
472{
473 const size_t window_size = 20;
474 cp_token *token;
475 expanded_location eloc;
476
477 if (file == NULL)
478 file = stderr;
479
480 fprintf (file, "Parser state\n\n");
481 fprintf (file, "Number of tokens: %u\n",
482 VEC_length (cp_token, parser->lexer->buffer));
483 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
484 cp_debug_print_tree_if_set (file, "Object scope",
485 parser->object_scope);
486 cp_debug_print_tree_if_set (file, "Qualifying scope",
487 parser->qualifying_scope);
488 cp_debug_print_context_stack (file, parser->context);
489 cp_debug_print_flag (file, "Allow GNU extensions",
490 parser->allow_gnu_extensions_p);
491 cp_debug_print_flag (file, "'>' token is greater-than",
492 parser->greater_than_is_operator_p);
493 cp_debug_print_flag (file, "Default args allowed in current "
494 "parameter list", parser->default_arg_ok_p);
495 cp_debug_print_flag (file, "Parsing integral constant-expression",
496 parser->integral_constant_expression_p);
497 cp_debug_print_flag (file, "Allow non-constant expression in current "
498 "constant-expression",
499 parser->allow_non_integral_constant_expression_p);
500 cp_debug_print_flag (file, "Seen non-constant expression",
501 parser->non_integral_constant_expression_p);
502 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
503 "current context",
504 parser->local_variables_forbidden_p);
505 cp_debug_print_flag (file, "In unbraced linkage specification",
506 parser->in_unbraced_linkage_specification_p);
507 cp_debug_print_flag (file, "Parsing a declarator",
508 parser->in_declarator_p);
509 cp_debug_print_flag (file, "In template argument list",
510 parser->in_template_argument_list_p);
511 cp_debug_print_flag (file, "Parsing an iteration statement",
512 parser->in_statement & IN_ITERATION_STMT);
513 cp_debug_print_flag (file, "Parsing a switch statement",
514 parser->in_statement & IN_SWITCH_STMT);
515 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
516 parser->in_statement & IN_OMP_BLOCK);
517 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
518 parser->in_statement & IN_OMP_FOR);
519 cp_debug_print_flag (file, "Parsing an if statement",
520 parser->in_statement & IN_IF_STMT);
521 cp_debug_print_flag (file, "Parsing a type-id in an expression "
522 "context", parser->in_type_id_in_expr_p);
523 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
524 parser->implicit_extern_c);
525 cp_debug_print_flag (file, "String expressions should be translated "
526 "to execution character set",
527 parser->translate_strings_p);
528 cp_debug_print_flag (file, "Parsing function body outside of a "
529 "local class", parser->in_function_body);
530 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
531 parser->colon_corrects_to_scope_p);
532 if (parser->type_definition_forbidden_message)
533 fprintf (file, "Error message for forbidden type definitions: %s\n",
534 parser->type_definition_forbidden_message);
535 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
536 fprintf (file, "Number of class definitions in progress: %u\n",
537 parser->num_classes_being_defined);
538 fprintf (file, "Number of template parameter lists for the current "
539 "declaration: %u\n", parser->num_template_parameter_lists);
540 cp_debug_parser_tokens (file, parser, window_size);
541 token = parser->lexer->next_token;
542 fprintf (file, "Next token to parse:\n");
543 fprintf (file, "\tToken: ");
544 cp_lexer_print_token (file, token);
545 eloc = expand_location (token->location);
546 fprintf (file, "\n\tFile: %s\n", eloc.file);
547 fprintf (file, "\tLine: %d\n", eloc.line);
548 fprintf (file, "\tColumn: %d\n", eloc.column);
b3145af5 549}
b3145af5 550
551
552/* Allocate memory for a new lexer object and return it. */
0a3b29ad 553
554static cp_lexer *
b3145af5 555cp_lexer_alloc (void)
0a3b29ad 556{
da4e72c7 557 cp_lexer *lexer;
00d26680 558
ddf4604f 559 c_common_no_more_pch ();
0a3b29ad 560
561 /* Allocate the memory. */
ba72912a 562 lexer = ggc_alloc_cleared_cp_lexer ();
0a3b29ad 563
00d26680 564 /* Initially we are not debugging. */
0a3b29ad 565 lexer->debugging_p = false;
24224d3d 566
046bfc77 567 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
568 CP_SAVED_TOKEN_STACK);
9031d10b 569
da4e72c7 570 /* Create the buffer. */
b3145af5 571 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
572
573 return lexer;
574}
575
576
577/* Create a new main C++ lexer, the lexer that gets tokens from the
578 preprocessor. */
579
580static cp_lexer *
581cp_lexer_new_main (void)
582{
583 cp_lexer *lexer;
584 cp_token token;
585
586 /* It's possible that parsing the first pragma will load a PCH file,
587 which is a GC collection point. So we have to do that before
588 allocating any memory. */
589 cp_parser_initial_pragma (&token);
590
591 lexer = cp_lexer_alloc ();
0a3b29ad 592
da4e72c7 593 /* Put the first token in the buffer. */
b3145af5 594 VEC_quick_push (cp_token, lexer->buffer, &token);
9031d10b 595
93523877 596 /* Get the remaining tokens from the preprocessor. */
b3145af5 597 while (token.type != CPP_EOF)
00d26680 598 {
b3145af5 599 cp_lexer_get_preprocessor_token (lexer, &token);
600 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
00d26680 601 }
b3145af5 602
603 lexer->last_token = VEC_address (cp_token, lexer->buffer)
604 + VEC_length (cp_token, lexer->buffer)
605 - 1;
606 lexer->next_token = VEC_length (cp_token, lexer->buffer)
607 ? VEC_address (cp_token, lexer->buffer)
608 : &eof_token;
00d26680 609
eb0d20b7 610 /* Subsequent preprocessor diagnostics should use compiler
611 diagnostic functions to get the compiler source location. */
7f5f3953 612 done_lexing = true;
eb0d20b7 613
b3145af5 614 gcc_assert (!lexer->next_token->purged_p);
0a3b29ad 615 return lexer;
616}
617
00d26680 618/* Create a new lexer whose token stream is primed with the tokens in
b9dd3954 619 CACHE. When these tokens are exhausted, no new tokens will be read. */
0a3b29ad 620
621static cp_lexer *
b9dd3954 622cp_lexer_new_from_tokens (cp_token_cache *cache)
0a3b29ad 623{
b9dd3954 624 cp_token *first = cache->first;
625 cp_token *last = cache->last;
ba72912a 626 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
573aba85 627
19273cc2 628 /* We do not own the buffer. */
da4e72c7 629 lexer->buffer = NULL;
b7bf20db 630 lexer->next_token = first == last ? &eof_token : first;
19273cc2 631 lexer->last_token = last;
9031d10b 632
046bfc77 633 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
634 CP_SAVED_TOKEN_STACK);
ccb84981 635
00d26680 636 /* Initially we are not debugging. */
573aba85 637 lexer->debugging_p = false;
0a3b29ad 638
b3145af5 639 gcc_assert (!lexer->next_token->purged_p);
b9dd3954 640 return lexer;
00d26680 641}
642
93523877 643/* Frees all resources associated with LEXER. */
00d26680 644
645static void
646cp_lexer_destroy (cp_lexer *lexer)
647{
b3145af5 648 VEC_free (cp_token, gc, lexer->buffer);
046bfc77 649 VEC_free (cp_token_position, heap, lexer->saved_tokens);
00d26680 650 ggc_free (lexer);
651}
652
f1d555e3 653/* Returns nonzero if debugging information should be output. */
0a3b29ad 654
2370b5bf 655static inline bool
656cp_lexer_debugging_p (cp_lexer *lexer)
0a3b29ad 657{
2370b5bf 658 return lexer->debugging_p;
659}
660
2fcca275 661
19273cc2 662static inline cp_token_position
663cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
0a3b29ad 664{
19273cc2 665 gcc_assert (!previous_p || lexer->next_token != &eof_token);
9031d10b 666
19273cc2 667 return lexer->next_token - previous_p;
0a3b29ad 668}
669
3d0f901b 670static inline cp_token *
19273cc2 671cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
3d0f901b 672{
19273cc2 673 return pos;
3d0f901b 674}
675
a9ffdd35 676static inline void
677cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
864468c7 678{
a9ffdd35 679 lexer->next_token = cp_lexer_token_at (lexer, pos);
680}
864468c7 681
a9ffdd35 682static inline cp_token_position
683cp_lexer_previous_token_position (cp_lexer *lexer)
684{
864468c7 685 if (lexer->next_token == &eof_token)
a9ffdd35 686 return lexer->last_token - 1;
864468c7 687 else
a9ffdd35 688 return cp_lexer_token_position (lexer, true);
689}
690
691static inline cp_token *
692cp_lexer_previous_token (cp_lexer *lexer)
693{
694 cp_token_position tp = cp_lexer_previous_token_position (lexer);
864468c7 695
696 return cp_lexer_token_at (lexer, tp);
697}
698
f1d555e3 699/* nonzero if we are presently saving tokens. */
2370b5bf 700
19273cc2 701static inline int
45baea8b 702cp_lexer_saving_tokens (const cp_lexer* lexer)
2370b5bf 703{
19273cc2 704 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
0a3b29ad 705}
706
da4e72c7 707/* Store the next token from the preprocessor in *TOKEN. Return true
8115b8be 708 if we reach EOF. If LEXER is NULL, assume we are handling an
709 initial #pragma pch_preprocess, and thus want the lexer to return
710 processed strings. */
0a3b29ad 711
ccb84981 712static void
8115b8be 713cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
0a3b29ad 714{
2e1f41a9 715 static int is_extern_c = 0;
0a3b29ad 716
61cc302f 717 /* Get a new token from the preprocessor. */
9d819530 718 token->type
8115b8be 719 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
538ba11a 720 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
b75b98aa 721 token->keyword = RID_MAX;
722 token->pragma_kind = PRAGMA_NONE;
b3145af5 723 token->purged_p = false;
0a3b29ad 724
9031d10b 725 /* On some systems, some header files are surrounded by an
2e1f41a9 726 implicit extern "C" block. Set a flag in the token if it
93523877 727 comes from such a header. */
2e1f41a9 728 is_extern_c += pending_lang_change;
729 pending_lang_change = 0;
730 token->implicit_extern_c = is_extern_c > 0;
731
0a3b29ad 732 /* Check to see if this token is a keyword. */
b62240d5 733 if (token->type == CPP_NAME)
0a3b29ad 734 {
3369eb76 735 if (C_IS_RESERVED_WORD (token->u.value))
b62240d5 736 {
737 /* Mark this token as a keyword. */
738 token->type = CPP_KEYWORD;
739 /* Record which keyword. */
3369eb76 740 token->keyword = C_RID_CODE (token->u.value);
b62240d5 741 }
742 else
c47b8257 743 {
ae49e4a6 744 if (warn_cxx0x_compat
745 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
746 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
747 {
748 /* Warn about the C++0x keyword (but still treat it as
749 an identifier). */
f94b7fe2 750 warning (OPT_Wc__0x_compat,
d1d11619 751 "identifier %qE is a keyword in C++11",
a608187f 752 token->u.value);
ae49e4a6 753
754 /* Clear out the C_RID_CODE so we don't warn about this
755 particular identifier-turned-keyword again. */
7d339f93 756 C_SET_RID_CODE (token->u.value, RID_MAX);
ae49e4a6 757 }
758
c47b8257 759 token->ambiguous_p = false;
760 token->keyword = RID_MAX;
761 }
0a3b29ad 762 }
7a4e126b 763 else if (token->type == CPP_AT_NAME)
764 {
e5c75ac3 765 /* This only happens in Objective-C++; it must be a keyword. */
7a4e126b 766 token->type = CPP_KEYWORD;
3369eb76 767 switch (C_RID_CODE (token->u.value))
7a4e126b 768 {
e5c75ac3 769 /* Replace 'class' with '@class', 'private' with '@private',
770 etc. This prevents confusion with the C++ keyword
771 'class', and makes the tokens consistent with other
772 Objective-C 'AT' keywords. For example '@class' is
773 reported as RID_AT_CLASS which is consistent with
774 '@synchronized', which is reported as
775 RID_AT_SYNCHRONIZED.
776 */
777 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
778 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
7a4e126b 779 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
e5c75ac3 780 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
781 case RID_THROW: token->keyword = RID_AT_THROW; break;
782 case RID_TRY: token->keyword = RID_AT_TRY; break;
783 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
784 default: token->keyword = C_RID_CODE (token->u.value);
7a4e126b 785 }
786 }
b75b98aa 787 else if (token->type == CPP_PRAGMA)
788 {
789 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
8458f4ca 790 token->pragma_kind = ((enum pragma_kind)
791 TREE_INT_CST_LOW (token->u.value));
3369eb76 792 token->u.value = NULL_TREE;
b75b98aa 793 }
0a3b29ad 794}
795
bdbc474b 796/* Update the globals input_location and the input file stack from TOKEN. */
b9dd3954 797static inline void
798cp_lexer_set_source_position_from_token (cp_token *token)
799{
800 if (token->type != CPP_EOF)
801 {
802 input_location = token->location;
b9dd3954 803 }
804}
805
0a3b29ad 806/* Return a pointer to the next token in the token stream, but do not
807 consume it. */
808
00d26680 809static inline cp_token *
810cp_lexer_peek_token (cp_lexer *lexer)
0a3b29ad 811{
0a3b29ad 812 if (cp_lexer_debugging_p (lexer))
19273cc2 813 {
814 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
815 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
816 putc ('\n', cp_lexer_debug_stream);
817 }
b9dd3954 818 return lexer->next_token;
0a3b29ad 819}
820
821/* Return true if the next token has the indicated TYPE. */
822
b9dd3954 823static inline bool
45baea8b 824cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 825{
b9dd3954 826 return cp_lexer_peek_token (lexer)->type == type;
0a3b29ad 827}
828
829/* Return true if the next token does not have the indicated TYPE. */
830
b9dd3954 831static inline bool
45baea8b 832cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
0a3b29ad 833{
834 return !cp_lexer_next_token_is (lexer, type);
835}
836
837/* Return true if the next token is the indicated KEYWORD. */
838
b9dd3954 839static inline bool
45baea8b 840cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
0a3b29ad 841{
8f399589 842 return cp_lexer_peek_token (lexer)->keyword == keyword;
0a3b29ad 843}
844
f82f1250 845/* Return true if the next token is not the indicated KEYWORD. */
846
847static inline bool
848cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
849{
850 return cp_lexer_peek_token (lexer)->keyword != keyword;
851}
852
17f99120 853/* Return true if the next token is a keyword for a decl-specifier. */
854
9a7c4b43 855static bool
856cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
857{
858 cp_token *token;
859
860 token = cp_lexer_peek_token (lexer);
861 switch (token->keyword)
862 {
45b44d0a 863 /* auto specifier: storage-class-specifier in C++,
864 simple-type-specifier in C++0x. */
9a7c4b43 865 case RID_AUTO:
45b44d0a 866 /* Storage classes. */
9a7c4b43 867 case RID_REGISTER:
868 case RID_STATIC:
869 case RID_EXTERN:
870 case RID_MUTABLE:
871 case RID_THREAD:
872 /* Elaborated type specifiers. */
873 case RID_ENUM:
874 case RID_CLASS:
875 case RID_STRUCT:
876 case RID_UNION:
877 case RID_TYPENAME:
878 /* Simple type specifiers. */
879 case RID_CHAR:
924bbf02 880 case RID_CHAR16:
881 case RID_CHAR32:
9a7c4b43 882 case RID_WCHAR:
883 case RID_BOOL:
884 case RID_SHORT:
885 case RID_INT:
886 case RID_LONG:
6388cfe2 887 case RID_INT128:
9a7c4b43 888 case RID_SIGNED:
889 case RID_UNSIGNED:
890 case RID_FLOAT:
891 case RID_DOUBLE:
892 case RID_VOID:
893 /* GNU extensions. */
894 case RID_ATTRIBUTE:
895 case RID_TYPEOF:
34da8800 896 /* C++0x extensions. */
897 case RID_DECLTYPE:
8de5c43e 898 case RID_UNDERLYING_TYPE:
9a7c4b43 899 return true;
900
901 default:
902 return false;
903 }
904}
905
07b8f133 906/* Returns TRUE iff the token T begins a decltype type. */
907
908static bool
909token_is_decltype (cp_token *t)
910{
911 return (t->keyword == RID_DECLTYPE
912 || t->type == CPP_DECLTYPE);
913}
914
915/* Returns TRUE iff the next token begins a decltype type. */
916
917static bool
918cp_lexer_next_token_is_decltype (cp_lexer *lexer)
919{
920 cp_token *t = cp_lexer_peek_token (lexer);
921 return token_is_decltype (t);
922}
923
0a3b29ad 924/* Return a pointer to the Nth token in the token stream. If N is 1,
b9dd3954 925 then this is precisely equivalent to cp_lexer_peek_token (except
926 that it is not inline). One would like to disallow that case, but
927 there is one case (cp_parser_nth_token_starts_template_id) where
928 the caller passes a variable for N and it might be 1. */
0a3b29ad 929
930static cp_token *
45baea8b 931cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
0a3b29ad 932{
933 cp_token *token;
934
935 /* N is 1-based, not zero-based. */
73cc0964 936 gcc_assert (n > 0);
074ab442 937
b9dd3954 938 if (cp_lexer_debugging_p (lexer))
939 fprintf (cp_lexer_debug_stream,
940 "cp_lexer: peeking ahead %ld at token: ", (long)n);
941
00d26680 942 --n;
0a3b29ad 943 token = lexer->next_token;
73cc0964 944 gcc_assert (!n || token != &eof_token);
00d26680 945 while (n != 0)
0a3b29ad 946 {
00d26680 947 ++token;
19273cc2 948 if (token == lexer->last_token)
949 {
b7bf20db 950 token = &eof_token;
19273cc2 951 break;
952 }
9031d10b 953
b3145af5 954 if (!token->purged_p)
00d26680 955 --n;
0a3b29ad 956 }
957
b9dd3954 958 if (cp_lexer_debugging_p (lexer))
959 {
960 cp_lexer_print_token (cp_lexer_debug_stream, token);
961 putc ('\n', cp_lexer_debug_stream);
962 }
963
0a3b29ad 964 return token;
965}
966
b9dd3954 967/* Return the next token, and advance the lexer's next_token pointer
968 to point to the next non-purged token. */
0a3b29ad 969
970static cp_token *
45baea8b 971cp_lexer_consume_token (cp_lexer* lexer)
0a3b29ad 972{
b9dd3954 973 cp_token *token = lexer->next_token;
0a3b29ad 974
19273cc2 975 gcc_assert (token != &eof_token);
b75b98aa 976 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
9031d10b 977
b9dd3954 978 do
19273cc2 979 {
980 lexer->next_token++;
981 if (lexer->next_token == lexer->last_token)
982 {
b7bf20db 983 lexer->next_token = &eof_token;
19273cc2 984 break;
985 }
9031d10b 986
19273cc2 987 }
b3145af5 988 while (lexer->next_token->purged_p);
9031d10b 989
b9dd3954 990 cp_lexer_set_source_position_from_token (token);
9031d10b 991
0a3b29ad 992 /* Provide debugging output. */
993 if (cp_lexer_debugging_p (lexer))
994 {
b9dd3954 995 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
0a3b29ad 996 cp_lexer_print_token (cp_lexer_debug_stream, token);
b9dd3954 997 putc ('\n', cp_lexer_debug_stream);
0a3b29ad 998 }
9031d10b 999
0a3b29ad 1000 return token;
1001}
1002
b9dd3954 1003/* Permanently remove the next token from the token stream, and
1004 advance the next_token pointer to refer to the next non-purged
1005 token. */
0a3b29ad 1006
1007static void
1008cp_lexer_purge_token (cp_lexer *lexer)
1009{
00d26680 1010 cp_token *tok = lexer->next_token;
9031d10b 1011
19273cc2 1012 gcc_assert (tok != &eof_token);
b3145af5 1013 tok->purged_p = true;
00d26680 1014 tok->location = UNKNOWN_LOCATION;
3369eb76 1015 tok->u.value = NULL_TREE;
00d26680 1016 tok->keyword = RID_MAX;
b9dd3954 1017
1018 do
19273cc2 1019 {
1020 tok++;
1021 if (tok == lexer->last_token)
1022 {
b7bf20db 1023 tok = &eof_token;
19273cc2 1024 break;
1025 }
1026 }
b3145af5 1027 while (tok->purged_p);
19273cc2 1028 lexer->next_token = tok;
0a3b29ad 1029}
1030
00d26680 1031/* Permanently remove all tokens after TOK, up to, but not
0a3b29ad 1032 including, the token that will be returned next by
1033 cp_lexer_peek_token. */
1034
1035static void
00d26680 1036cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
0a3b29ad 1037{
19273cc2 1038 cp_token *peek = lexer->next_token;
0a3b29ad 1039
19273cc2 1040 if (peek == &eof_token)
1041 peek = lexer->last_token;
9031d10b 1042
00d26680 1043 gcc_assert (tok < peek);
1044
1045 for ( tok += 1; tok != peek; tok += 1)
0a3b29ad 1046 {
b3145af5 1047 tok->purged_p = true;
00d26680 1048 tok->location = UNKNOWN_LOCATION;
3369eb76 1049 tok->u.value = NULL_TREE;
00d26680 1050 tok->keyword = RID_MAX;
0a3b29ad 1051 }
00d26680 1052}
1053
0a3b29ad 1054/* Begin saving tokens. All tokens consumed after this point will be
1055 preserved. */
1056
1057static void
45baea8b 1058cp_lexer_save_tokens (cp_lexer* lexer)
0a3b29ad 1059{
1060 /* Provide debugging output. */
1061 if (cp_lexer_debugging_p (lexer))
1062 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1063
046bfc77 1064 VEC_safe_push (cp_token_position, heap,
1065 lexer->saved_tokens, lexer->next_token);
0a3b29ad 1066}
1067
1068/* Commit to the portion of the token stream most recently saved. */
1069
1070static void
45baea8b 1071cp_lexer_commit_tokens (cp_lexer* lexer)
0a3b29ad 1072{
1073 /* Provide debugging output. */
1074 if (cp_lexer_debugging_p (lexer))
1075 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1076
19273cc2 1077 VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 1078}
1079
1080/* Return all tokens saved since the last call to cp_lexer_save_tokens
1081 to the token stream. Stop saving tokens. */
1082
1083static void
45baea8b 1084cp_lexer_rollback_tokens (cp_lexer* lexer)
0a3b29ad 1085{
0a3b29ad 1086 /* Provide debugging output. */
1087 if (cp_lexer_debugging_p (lexer))
1088 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1089
19273cc2 1090 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
0a3b29ad 1091}
1092
0a3b29ad 1093/* Print a representation of the TOKEN on the STREAM. */
1094
1095static void
00d26680 1096cp_lexer_print_token (FILE * stream, cp_token *token)
1097{
1098 /* We don't use cpp_type2name here because the parser defines
1099 a few tokens of its own. */
1100 static const char *const token_names[] = {
1101 /* cpplib-defined token types */
1102#define OP(e, s) #e,
1103#define TK(e, s) #e,
1104 TTYPE_TABLE
1105#undef OP
1106#undef TK
1107 /* C++ parser token types - see "Manifest constants", above. */
1108 "KEYWORD",
1109 "TEMPLATE_ID",
1110 "NESTED_NAME_SPECIFIER",
00d26680 1111 };
9031d10b 1112
00d26680 1113 /* For some tokens, print the associated data. */
0a3b29ad 1114 switch (token->type)
1115 {
00d26680 1116 case CPP_KEYWORD:
1117 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1118 For example, `struct' is mapped to an INTEGER_CST. */
3369eb76 1119 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
00d26680 1120 break;
1121 /* else fall through */
0a3b29ad 1122 case CPP_NAME:
3369eb76 1123 fputs (IDENTIFIER_POINTER (token->u.value), stream);
0a3b29ad 1124 break;
1125
00d26680 1126 case CPP_STRING:
924bbf02 1127 case CPP_STRING16:
1128 case CPP_STRING32:
00d26680 1129 case CPP_WSTRING:
538ba11a 1130 case CPP_UTF8STRING:
3369eb76 1131 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
0a3b29ad 1132 break;
1133
b3145af5 1134 case CPP_NUMBER:
1135 print_generic_expr (stream, token->u.value, 0);
1136 break;
1137
0a3b29ad 1138 default:
b3145af5 1139 /* If we have a name for the token, print it out. Otherwise, we
1140 simply give the numeric code. */
1141 if (token->type < ARRAY_SIZE(token_names))
1142 fputs (token_names[token->type], stream);
1143 else
1144 fprintf (stream, "[%d]", token->type);
0a3b29ad 1145 break;
1146 }
0a3b29ad 1147}
1148
0a3b29ad 1149/* Start emitting debugging information. */
1150
1151static void
45baea8b 1152cp_lexer_start_debugging (cp_lexer* lexer)
0a3b29ad 1153{
31cc05e3 1154 lexer->debugging_p = true;
24224d3d 1155 cp_lexer_debug_stream = stderr;
0a3b29ad 1156}
ccb84981 1157
0a3b29ad 1158/* Stop emitting debugging information. */
1159
1160static void
45baea8b 1161cp_lexer_stop_debugging (cp_lexer* lexer)
0a3b29ad 1162{
31cc05e3 1163 lexer->debugging_p = false;
24224d3d 1164 cp_lexer_debug_stream = NULL;
0a3b29ad 1165}
1166
93523877 1167/* Create a new cp_token_cache, representing a range of tokens. */
00d26680 1168
1169static cp_token_cache *
1170cp_token_cache_new (cp_token *first, cp_token *last)
1171{
ba72912a 1172 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
00d26680 1173 cache->first = first;
1174 cache->last = last;
1175 return cache;
1176}
1177
0a3b29ad 1178\f
4b9b2871 1179/* Decl-specifiers. */
1180
4b9b2871 1181/* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1182
1183static void
1184clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1185{
1186 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1187}
1188
3046c0a3 1189/* Declarators. */
1190
1191/* Nothing other than the parser should be creating declarators;
1192 declarators are a semi-syntactic representation of C++ entities.
1193 Other parts of the front end that need to create entities (like
1194 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1195
207355ad 1196static cp_declarator *make_call_declarator
ece7f9e3 1197 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
207355ad 1198static cp_declarator *make_array_declarator
3046c0a3 1199 (cp_declarator *, tree);
207355ad 1200static cp_declarator *make_pointer_declarator
2cfb6cde 1201 (cp_cv_quals, cp_declarator *);
207355ad 1202static cp_declarator *make_reference_declarator
63949b38 1203 (cp_cv_quals, cp_declarator *, bool);
207355ad 1204static cp_parameter_declarator *make_parameter_declarator
4b9b2871 1205 (cp_decl_specifier_seq *, cp_declarator *, tree);
207355ad 1206static cp_declarator *make_ptrmem_declarator
2cfb6cde 1207 (cp_cv_quals, tree, cp_declarator *);
3046c0a3 1208
197c9df7 1209/* An erroneous declarator. */
1210static cp_declarator *cp_error_declarator;
3046c0a3 1211
1212/* The obstack on which declarators and related data structures are
1213 allocated. */
1214static struct obstack declarator_obstack;
1215
1216/* Alloc BYTES from the declarator memory pool. */
1217
1218static inline void *
1219alloc_declarator (size_t bytes)
1220{
1221 return obstack_alloc (&declarator_obstack, bytes);
1222}
1223
1224/* Allocate a declarator of the indicated KIND. Clear fields that are
1225 common to all declarators. */
1226
1227static cp_declarator *
1228make_declarator (cp_declarator_kind kind)
1229{
1230 cp_declarator *declarator;
1231
1232 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1233 declarator->kind = kind;
1234 declarator->attributes = NULL_TREE;
1235 declarator->declarator = NULL;
d95d815d 1236 declarator->parameter_pack_p = false;
3c5a5d99 1237 declarator->id_loc = UNKNOWN_LOCATION;
3046c0a3 1238
1239 return declarator;
1240}
1241
2366ed31 1242/* Make a declarator for a generalized identifier. If
1243 QUALIFYING_SCOPE is non-NULL, the identifier is
1244 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1245 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1246 is, if any. */
3046c0a3 1247
2ded3667 1248static cp_declarator *
2366ed31 1249make_id_declarator (tree qualifying_scope, tree unqualified_name,
1250 special_function_kind sfk)
3046c0a3 1251{
1252 cp_declarator *declarator;
207355ad 1253
2ded3667 1254 /* It is valid to write:
1255
1256 class C { void f(); };
1257 typedef C D;
1258 void D::f();
1259
1260 The standard is not clear about whether `typedef const C D' is
1261 legal; as of 2002-09-15 the committee is considering that
1262 question. EDG 3.0 allows that syntax. Therefore, we do as
1263 well. */
1264 if (qualifying_scope && TYPE_P (qualifying_scope))
1265 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1266
2366ed31 1267 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1268 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1269 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1270
3046c0a3 1271 declarator = make_declarator (cdk_id);
2ded3667 1272 declarator->u.id.qualifying_scope = qualifying_scope;
1273 declarator->u.id.unqualified_name = unqualified_name;
2366ed31 1274 declarator->u.id.sfk = sfk;
d95d815d 1275
3046c0a3 1276 return declarator;
1277}
1278
1279/* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1280 of modifiers such as const or volatile to apply to the pointer
1281 type, represented as identifiers. */
1282
1283cp_declarator *
2cfb6cde 1284make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
3046c0a3 1285{
1286 cp_declarator *declarator;
1287
1288 declarator = make_declarator (cdk_pointer);
1289 declarator->declarator = target;
1290 declarator->u.pointer.qualifiers = cv_qualifiers;
1291 declarator->u.pointer.class_type = NULL_TREE;
d95d815d 1292 if (target)
1293 {
0a683683 1294 declarator->id_loc = target->id_loc;
d95d815d 1295 declarator->parameter_pack_p = target->parameter_pack_p;
1296 target->parameter_pack_p = false;
1297 }
1298 else
1299 declarator->parameter_pack_p = false;
3046c0a3 1300
1301 return declarator;
1302}
1303
1304/* Like make_pointer_declarator -- but for references. */
1305
1306cp_declarator *
63949b38 1307make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1308 bool rvalue_ref)
3046c0a3 1309{
1310 cp_declarator *declarator;
1311
1312 declarator = make_declarator (cdk_reference);
1313 declarator->declarator = target;
63949b38 1314 declarator->u.reference.qualifiers = cv_qualifiers;
1315 declarator->u.reference.rvalue_ref = rvalue_ref;
d95d815d 1316 if (target)
1317 {
0a683683 1318 declarator->id_loc = target->id_loc;
d95d815d 1319 declarator->parameter_pack_p = target->parameter_pack_p;
1320 target->parameter_pack_p = false;
1321 }
1322 else
1323 declarator->parameter_pack_p = false;
3046c0a3 1324
1325 return declarator;
1326}
1327
1328/* Like make_pointer_declarator -- but for a pointer to a non-static
1329 member of CLASS_TYPE. */
1330
1331cp_declarator *
2cfb6cde 1332make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
3046c0a3 1333 cp_declarator *pointee)
1334{
1335 cp_declarator *declarator;
1336
1337 declarator = make_declarator (cdk_ptrmem);
1338 declarator->declarator = pointee;
1339 declarator->u.pointer.qualifiers = cv_qualifiers;
1340 declarator->u.pointer.class_type = class_type;
1341
d95d815d 1342 if (pointee)
1343 {
1344 declarator->parameter_pack_p = pointee->parameter_pack_p;
1345 pointee->parameter_pack_p = false;
1346 }
1347 else
1348 declarator->parameter_pack_p = false;
1349
3046c0a3 1350 return declarator;
1351}
1352
1353/* Make a declarator for the function given by TARGET, with the
1354 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1355 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1356 indicates what exceptions can be thrown. */
1357
1358cp_declarator *
207355ad 1359make_call_declarator (cp_declarator *target,
34eac767 1360 tree parms,
2cfb6cde 1361 cp_cv_quals cv_qualifiers,
ece7f9e3 1362 cp_virt_specifiers virt_specifiers,
346e3a9c 1363 tree exception_specification,
1364 tree late_return_type)
3046c0a3 1365{
1366 cp_declarator *declarator;
1367
1368 declarator = make_declarator (cdk_function);
1369 declarator->declarator = target;
1370 declarator->u.function.parameters = parms;
1371 declarator->u.function.qualifiers = cv_qualifiers;
ece7f9e3 1372 declarator->u.function.virt_specifiers = virt_specifiers;
3046c0a3 1373 declarator->u.function.exception_specification = exception_specification;
346e3a9c 1374 declarator->u.function.late_return_type = late_return_type;
d95d815d 1375 if (target)
1376 {
0a683683 1377 declarator->id_loc = target->id_loc;
d95d815d 1378 declarator->parameter_pack_p = target->parameter_pack_p;
1379 target->parameter_pack_p = false;
1380 }
1381 else
1382 declarator->parameter_pack_p = false;
3046c0a3 1383
1384 return declarator;
1385}
1386
1387/* Make a declarator for an array of BOUNDS elements, each of which is
1388 defined by ELEMENT. */
1389
1390cp_declarator *
1391make_array_declarator (cp_declarator *element, tree bounds)
1392{
1393 cp_declarator *declarator;
1394
1395 declarator = make_declarator (cdk_array);
1396 declarator->declarator = element;
1397 declarator->u.array.bounds = bounds;
d95d815d 1398 if (element)
1399 {
0a683683 1400 declarator->id_loc = element->id_loc;
d95d815d 1401 declarator->parameter_pack_p = element->parameter_pack_p;
1402 element->parameter_pack_p = false;
1403 }
1404 else
1405 declarator->parameter_pack_p = false;
3046c0a3 1406
1407 return declarator;
1408}
1409
2aedc2ff 1410/* Determine whether the declarator we've seen so far can be a
1411 parameter pack, when followed by an ellipsis. */
1412static bool
1413declarator_can_be_parameter_pack (cp_declarator *declarator)
1414{
1415 /* Search for a declarator name, or any other declarator that goes
1416 after the point where the ellipsis could appear in a parameter
1417 pack. If we find any of these, then this declarator can not be
1418 made into a parameter pack. */
1419 bool found = false;
1420 while (declarator && !found)
1421 {
1422 switch ((int)declarator->kind)
1423 {
1424 case cdk_id:
2aedc2ff 1425 case cdk_array:
2aedc2ff 1426 found = true;
1427 break;
41341abd 1428
1429 case cdk_error:
1430 return true;
1431
2aedc2ff 1432 default:
1433 declarator = declarator->declarator;
1434 break;
1435 }
1436 }
1437
1438 return !found;
1439}
1440
3046c0a3 1441cp_parameter_declarator *no_parameters;
1442
1443/* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1444 DECLARATOR and DEFAULT_ARGUMENT. */
1445
1446cp_parameter_declarator *
207355ad 1447make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
3046c0a3 1448 cp_declarator *declarator,
1449 tree default_argument)
1450{
1451 cp_parameter_declarator *parameter;
1452
207355ad 1453 parameter = ((cp_parameter_declarator *)
3046c0a3 1454 alloc_declarator (sizeof (cp_parameter_declarator)));
1455 parameter->next = NULL;
4b9b2871 1456 if (decl_specifiers)
1457 parameter->decl_specifiers = *decl_specifiers;
1458 else
1459 clear_decl_specs (&parameter->decl_specifiers);
3046c0a3 1460 parameter->declarator = declarator;
1461 parameter->default_argument = default_argument;
1462 parameter->ellipsis_p = false;
1463
1464 return parameter;
1465}
1466
3a30cb7f 1467/* Returns true iff DECLARATOR is a declaration for a function. */
1468
1469static bool
1470function_declarator_p (const cp_declarator *declarator)
1471{
1472 while (declarator)
1473 {
1474 if (declarator->kind == cdk_function
1475 && declarator->declarator->kind == cdk_id)
1476 return true;
1477 if (declarator->kind == cdk_id
1478 || declarator->kind == cdk_error)
1479 return false;
1480 declarator = declarator->declarator;
1481 }
1482 return false;
1483}
1484
0a3b29ad 1485/* The parser. */
1486
1487/* Overview
1488 --------
1489
1490 A cp_parser parses the token stream as specified by the C++
1491 grammar. Its job is purely parsing, not semantic analysis. For
1492 example, the parser breaks the token stream into declarators,
1493 expressions, statements, and other similar syntactic constructs.
1494 It does not check that the types of the expressions on either side
1495 of an assignment-statement are compatible, or that a function is
1496 not declared with a parameter of type `void'.
1497
1498 The parser invokes routines elsewhere in the compiler to perform
1499 semantic analysis and to build up the abstract syntax tree for the
ccb84981 1500 code processed.
0a3b29ad 1501
1502 The parser (and the template instantiation code, which is, in a
1503 way, a close relative of parsing) are the only parts of the
1504 compiler that should be calling push_scope and pop_scope, or
1505 related functions. The parser (and template instantiation code)
1506 keeps track of what scope is presently active; everything else
1507 should simply honor that. (The code that generates static
1508 initializers may also need to set the scope, in order to check
1509 access control correctly when emitting the initializers.)
1510
1511 Methodology
1512 -----------
ccb84981 1513
0a3b29ad 1514 The parser is of the standard recursive-descent variety. Upcoming
1515 tokens in the token stream are examined in order to determine which
1516 production to use when parsing a non-terminal. Some C++ constructs
1517 require arbitrary look ahead to disambiguate. For example, it is
1518 impossible, in the general case, to tell whether a statement is an
1519 expression or declaration without scanning the entire statement.
1520 Therefore, the parser is capable of "parsing tentatively." When the
1521 parser is not sure what construct comes next, it enters this mode.
1522 Then, while we attempt to parse the construct, the parser queues up
1523 error messages, rather than issuing them immediately, and saves the
1524 tokens it consumes. If the construct is parsed successfully, the
1525 parser "commits", i.e., it issues any queued error messages and
1526 the tokens that were being preserved are permanently discarded.
1527 If, however, the construct is not parsed successfully, the parser
1528 rolls back its state completely so that it can resume parsing using
1529 a different alternative.
1530
1531 Future Improvements
1532 -------------------
ccb84981 1533
0a88af73 1534 The performance of the parser could probably be improved substantially.
1535 We could often eliminate the need to parse tentatively by looking ahead
1536 a little bit. In some places, this approach might not entirely eliminate
1537 the need to parse tentatively, but it might still speed up the average
1538 case. */
0a3b29ad 1539
1540/* Flags that are passed to some parsing functions. These values can
1541 be bitwise-ored together. */
1542
26dbec0a 1543enum
0a3b29ad 1544{
1545 /* No flags. */
1546 CP_PARSER_FLAGS_NONE = 0x0,
1547 /* The construct is optional. If it is not present, then no error
1548 should be issued. */
1549 CP_PARSER_FLAGS_OPTIONAL = 0x1,
638569c5 1550 /* When parsing a type-specifier, treat user-defined type-names
1551 as non-type identifiers. */
1552 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1553 /* When parsing a type-specifier, do not try to parse a class-specifier
1554 or enum-specifier. */
ca63c29a 1555 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1556 /* When parsing a decl-specifier-seq, only allow type-specifier or
1557 constexpr. */
1558 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
8458f4ca 1559};
1560
1561/* This type is used for parameters and variables which hold
26dbec0a 1562 combinations of the above flags. */
8458f4ca 1563typedef int cp_parser_flags;
0a3b29ad 1564
42bbd0ec 1565/* The different kinds of declarators we want to parse. */
1566
1567typedef enum cp_parser_declarator_kind
1568{
0f3ccaa3 1569 /* We want an abstract declarator. */
42bbd0ec 1570 CP_PARSER_DECLARATOR_ABSTRACT,
1571 /* We want a named declarator. */
1572 CP_PARSER_DECLARATOR_NAMED,
bd8962d5 1573 /* We don't mind, but the name must be an unqualified-id. */
42bbd0ec 1574 CP_PARSER_DECLARATOR_EITHER
1575} cp_parser_declarator_kind;
1576
0a88af73 1577/* The precedence values used to parse binary expressions. The minimum value
1578 of PREC must be 1, because zero is reserved to quickly discriminate
1579 binary operators from other tokens. */
0a3b29ad 1580
0a88af73 1581enum cp_parser_prec
0a3b29ad 1582{
0a88af73 1583 PREC_NOT_OPERATOR,
1584 PREC_LOGICAL_OR_EXPRESSION,
1585 PREC_LOGICAL_AND_EXPRESSION,
1586 PREC_INCLUSIVE_OR_EXPRESSION,
1587 PREC_EXCLUSIVE_OR_EXPRESSION,
1588 PREC_AND_EXPRESSION,
0a88af73 1589 PREC_EQUALITY_EXPRESSION,
22dc256c 1590 PREC_RELATIONAL_EXPRESSION,
0a88af73 1591 PREC_SHIFT_EXPRESSION,
1592 PREC_ADDITIVE_EXPRESSION,
1593 PREC_MULTIPLICATIVE_EXPRESSION,
1594 PREC_PM_EXPRESSION,
1595 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1596};
0a3b29ad 1597
0a88af73 1598/* A mapping from a token type to a corresponding tree node type, with a
1599 precedence value. */
0a3b29ad 1600
0a88af73 1601typedef struct cp_parser_binary_operations_map_node
1602{
1603 /* The token type. */
1604 enum cpp_ttype token_type;
1605 /* The corresponding tree code. */
1606 enum tree_code tree_type;
1607 /* The precedence of this operator. */
1608 enum cp_parser_prec prec;
1609} cp_parser_binary_operations_map_node;
0a3b29ad 1610
0a88af73 1611typedef struct cp_parser_expression_stack_entry
1612{
e534436e 1613 /* Left hand side of the binary operation we are currently
1614 parsing. */
0a88af73 1615 tree lhs;
e534436e 1616 /* Original tree code for left hand side, if it was a binary
1617 expression itself (used for -Wparentheses). */
1618 enum tree_code lhs_type;
1619 /* Tree code for the binary operation we are parsing. */
0a88af73 1620 enum tree_code tree_type;
e534436e 1621 /* Precedence of the binary operation we are parsing. */
8458f4ca 1622 enum cp_parser_prec prec;
1e6d0c16 1623 /* Location of the binary operation we are parsing. */
1624 location_t loc;
0a88af73 1625} cp_parser_expression_stack_entry;
1626
9802eea0 1627/* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1628 entries because precedence levels on the stack are monotonically
1629 increasing. */
0a88af73 1630typedef struct cp_parser_expression_stack_entry
1631 cp_parser_expression_stack[NUM_PREC_VALUES];
0a3b29ad 1632
0a3b29ad 1633/* Prototypes. */
1634
1635/* Constructors and destructors. */
1636
1637static cp_parser_context *cp_parser_context_new
45baea8b 1638 (cp_parser_context *);
0a3b29ad 1639
2c593bd0 1640/* Class variables. */
1641
7035b2ab 1642static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2c593bd0 1643
0a88af73 1644/* The operator-precedence table used by cp_parser_binary_expression.
1645 Transformed into an associative array (binops_by_token) by
1646 cp_parser_new. */
1647
1648static const cp_parser_binary_operations_map_node binops[] = {
1649 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1650 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1651
1652 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1655
1656 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1658
1659 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1661
1662 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1664 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
0a88af73 1666
1667 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1668 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1669
1670 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1671
1672 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1673
1674 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1675
1676 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1677
1678 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1679};
1680
1681/* The same as binops, but initialized by cp_parser_new so that
1682 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1683 for speed. */
1684static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1685
0a3b29ad 1686/* Constructors and destructors. */
1687
1688/* Construct a new context. The context below this one on the stack
1689 is given by NEXT. */
1690
1691static cp_parser_context *
45baea8b 1692cp_parser_context_new (cp_parser_context* next)
0a3b29ad 1693{
1694 cp_parser_context *context;
1695
1696 /* Allocate the storage. */
2c593bd0 1697 if (cp_parser_context_free_list != NULL)
1698 {
1699 /* Pull the first entry from the free list. */
1700 context = cp_parser_context_free_list;
1701 cp_parser_context_free_list = context->next;
6edf18a6 1702 memset (context, 0, sizeof (*context));
2c593bd0 1703 }
1704 else
ba72912a 1705 context = ggc_alloc_cleared_cp_parser_context ();
0a88af73 1706
0a3b29ad 1707 /* No errors have occurred yet in this context. */
1708 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
08cc44e7 1709 /* If this is not the bottommost context, copy information that we
0a3b29ad 1710 need from the previous context. */
1711 if (next)
1712 {
1713 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1714 expression, then we are parsing one in this context, too. */
1715 context->object_type = next->object_type;
0a3b29ad 1716 /* Thread the stack. */
1717 context->next = next;
1718 }
1719
1720 return context;
1721}
1722
9177da82 1723/* Managing the unparsed function queues. */
1724
1725#define unparsed_funs_with_default_args \
1726 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1727#define unparsed_funs_with_definitions \
1728 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
3ab40fb9 1729#define unparsed_nsdmis \
1730 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
9177da82 1731
1732static void
1733push_unparsed_function_queues (cp_parser *parser)
1734{
1735 VEC_safe_push (cp_unparsed_functions_entry, gc,
1736 parser->unparsed_queues, NULL);
1737 unparsed_funs_with_default_args = NULL;
1738 unparsed_funs_with_definitions = make_tree_vector ();
3ab40fb9 1739 unparsed_nsdmis = NULL;
9177da82 1740}
1741
1742static void
1743pop_unparsed_function_queues (cp_parser *parser)
1744{
1745 release_tree_vector (unparsed_funs_with_definitions);
1746 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1747}
1748
0a3b29ad 1749/* Prototypes. */
1750
1751/* Constructors and destructors. */
1752
1753static cp_parser *cp_parser_new
45baea8b 1754 (void);
0a3b29ad 1755
ccb84981 1756/* Routines to parse various constructs.
0a3b29ad 1757
1758 Those that return `tree' will return the error_mark_node (rather
1759 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1760 Sometimes, they will return an ordinary node if error-recovery was
755edffd 1761 attempted, even though a parse error occurred. So, to check
0a3b29ad 1762 whether or not a parse error occurred, you should always use
1763 cp_parser_error_occurred. If the construct is optional (indicated
1764 either by an `_opt' in the name of the function that does the
1765 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1766 the construct is not present. */
1767
1768/* Lexical conventions [gram.lex] */
1769
1770static tree cp_parser_identifier
45baea8b 1771 (cp_parser *);
00d26680 1772static tree cp_parser_string_literal
1773 (cp_parser *, bool, bool);
244db24d 1774static tree cp_parser_userdef_char_literal
1775 (cp_parser *);
1776static tree cp_parser_userdef_string_literal
1777 (cp_token *);
1778static tree cp_parser_userdef_numeric_literal
1779 (cp_parser *);
0a3b29ad 1780
1781/* Basic concepts [gram.basic] */
1782
1783static bool cp_parser_translation_unit
45baea8b 1784 (cp_parser *);
0a3b29ad 1785
1786/* Expressions [gram.expr] */
1787
1788static tree cp_parser_primary_expression
fbb01da7 1789 (cp_parser *, bool, bool, bool, cp_id_kind *);
0a3b29ad 1790static tree cp_parser_id_expression
130bb1d4 1791 (cp_parser *, bool, bool, bool *, bool, bool);
0a3b29ad 1792static tree cp_parser_unqualified_id
130bb1d4 1793 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1794static tree cp_parser_nested_name_specifier_opt
3d0f901b 1795 (cp_parser *, bool, bool, bool, bool);
0a3b29ad 1796static tree cp_parser_nested_name_specifier
0a3b29ad 1797 (cp_parser *, bool, bool, bool, bool);
3f00a6c0 1798static tree cp_parser_qualifying_entity
3d0f901b 1799 (cp_parser *, bool, bool, bool, bool, bool);
0a3b29ad 1800static tree cp_parser_postfix_expression
98b326fd 1801 (cp_parser *, bool, bool, bool, cp_id_kind *);
43bf5d72 1802static tree cp_parser_postfix_open_square_expression
1803 (cp_parser *, tree, bool);
1804static tree cp_parser_postfix_dot_deref_expression
ad9ae192 1805 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
f352a3fb 1806static VEC(tree,gc) *cp_parser_parenthesized_expression_list
33199a81 1807 (cp_parser *, int, bool, bool, bool *);
1808/* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1809enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
0a3b29ad 1810static void cp_parser_pseudo_destructor_name
45baea8b 1811 (cp_parser *, tree *, tree *);
0a3b29ad 1812static tree cp_parser_unary_expression
98b326fd 1813 (cp_parser *, bool, bool, cp_id_kind *);
0a3b29ad 1814static enum tree_code cp_parser_unary_operator
45baea8b 1815 (cp_token *);
0a3b29ad 1816static tree cp_parser_new_expression
45baea8b 1817 (cp_parser *);
f352a3fb 1818static VEC(tree,gc) *cp_parser_new_placement
45baea8b 1819 (cp_parser *);
0a3b29ad 1820static tree cp_parser_new_type_id
3046c0a3 1821 (cp_parser *, tree *);
1822static cp_declarator *cp_parser_new_declarator_opt
45baea8b 1823 (cp_parser *);
3046c0a3 1824static cp_declarator *cp_parser_direct_new_declarator
45baea8b 1825 (cp_parser *);
f352a3fb 1826static VEC(tree,gc) *cp_parser_new_initializer
45baea8b 1827 (cp_parser *);
0a3b29ad 1828static tree cp_parser_delete_expression
45baea8b 1829 (cp_parser *);
ccb84981 1830static tree cp_parser_cast_expression
98b326fd 1831 (cp_parser *, bool, bool, cp_id_kind *);
0a88af73 1832static tree cp_parser_binary_expression
4390875c 1833 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
0a3b29ad 1834static tree cp_parser_question_colon_clause
45baea8b 1835 (cp_parser *, tree);
0a3b29ad 1836static tree cp_parser_assignment_expression
98b326fd 1837 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1838static enum tree_code cp_parser_assignment_operator_opt
45baea8b 1839 (cp_parser *);
0a3b29ad 1840static tree cp_parser_expression
98b326fd 1841 (cp_parser *, bool, cp_id_kind *);
0a3b29ad 1842static tree cp_parser_constant_expression
5f6526e1 1843 (cp_parser *, bool, bool *);
43bf5d72 1844static tree cp_parser_builtin_offsetof
1845 (cp_parser *);
a8b75081 1846static tree cp_parser_lambda_expression
1847 (cp_parser *);
1848static void cp_parser_lambda_introducer
1849 (cp_parser *, tree);
3847f0aa 1850static bool cp_parser_lambda_declarator_opt
a8b75081 1851 (cp_parser *, tree);
1852static void cp_parser_lambda_body
1853 (cp_parser *, tree);
0a3b29ad 1854
1855/* Statements [gram.stmt.stmt] */
1856
1857static void cp_parser_statement
e534436e 1858 (cp_parser *, tree, bool, bool *);
17d53949 1859static void cp_parser_label_for_labeled_statement
1860 (cp_parser *);
0a3b29ad 1861static tree cp_parser_expression_statement
2363ef00 1862 (cp_parser *, tree);
0a3b29ad 1863static tree cp_parser_compound_statement
6bf5e78d 1864 (cp_parser *, tree, bool, bool);
0a3b29ad 1865static void cp_parser_statement_seq_opt
2363ef00 1866 (cp_parser *, tree);
0a3b29ad 1867static tree cp_parser_selection_statement
e534436e 1868 (cp_parser *, bool *);
0a3b29ad 1869static tree cp_parser_condition
45baea8b 1870 (cp_parser *);
0a3b29ad 1871static tree cp_parser_iteration_statement
45baea8b 1872 (cp_parser *);
fa7d5870 1873static bool cp_parser_for_init_statement
1874 (cp_parser *, tree *decl);
1875static tree cp_parser_for
9dd72ec4 1876 (cp_parser *);
fa7d5870 1877static tree cp_parser_c_for
1878 (cp_parser *, tree, tree);
1879static tree cp_parser_range_for
1880 (cp_parser *, tree, tree, tree);
4121b00f 1881static void do_range_for_auto_deduction
1882 (tree, tree);
4e81a9f3 1883static tree cp_parser_perform_range_for_lookup
1884 (tree, tree *, tree *);
1885static tree cp_parser_range_for_member_function
1886 (tree, tree);
0a3b29ad 1887static tree cp_parser_jump_statement
45baea8b 1888 (cp_parser *);
0a3b29ad 1889static void cp_parser_declaration_statement
45baea8b 1890 (cp_parser *);
0a3b29ad 1891
1892static tree cp_parser_implicitly_scoped_statement
e534436e 1893 (cp_parser *, bool *);
0a3b29ad 1894static void cp_parser_already_scoped_statement
45baea8b 1895 (cp_parser *);
0a3b29ad 1896
1897/* Declarations [gram.dcl.dcl] */
1898
1899static void cp_parser_declaration_seq_opt
45baea8b 1900 (cp_parser *);
0a3b29ad 1901static void cp_parser_declaration
45baea8b 1902 (cp_parser *);
0a3b29ad 1903static void cp_parser_block_declaration
45baea8b 1904 (cp_parser *, bool);
0a3b29ad 1905static void cp_parser_simple_declaration
fa7d5870 1906 (cp_parser *, bool, tree *);
4b9b2871 1907static void cp_parser_decl_specifier_seq
1908 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
0a3b29ad 1909static tree cp_parser_storage_class_specifier_opt
45baea8b 1910 (cp_parser *);
0a3b29ad 1911static tree cp_parser_function_specifier_opt
4b9b2871 1912 (cp_parser *, cp_decl_specifier_seq *);
0a3b29ad 1913static tree cp_parser_type_specifier
207355ad 1914 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
4b9b2871 1915 int *, bool *);
0a3b29ad 1916static tree cp_parser_simple_type_specifier
4b9b2871 1917 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
0a3b29ad 1918static tree cp_parser_type_name
45baea8b 1919 (cp_parser *);
674e90bd 1920static tree cp_parser_nonclass_name
1921 (cp_parser* parser);
0a3b29ad 1922static tree cp_parser_elaborated_type_specifier
45baea8b 1923 (cp_parser *, bool, bool);
0a3b29ad 1924static tree cp_parser_enum_specifier
45baea8b 1925 (cp_parser *);
0a3b29ad 1926static void cp_parser_enumerator_list
45baea8b 1927 (cp_parser *, tree);
ccb84981 1928static void cp_parser_enumerator_definition
45baea8b 1929 (cp_parser *, tree);
0a3b29ad 1930static tree cp_parser_namespace_name
45baea8b 1931 (cp_parser *);
0a3b29ad 1932static void cp_parser_namespace_definition
45baea8b 1933 (cp_parser *);
0a3b29ad 1934static void cp_parser_namespace_body
45baea8b 1935 (cp_parser *);
0a3b29ad 1936static tree cp_parser_qualified_namespace_specifier
45baea8b 1937 (cp_parser *);
0a3b29ad 1938static void cp_parser_namespace_alias_definition
45baea8b 1939 (cp_parser *);
da2a3271 1940static bool cp_parser_using_declaration
1941 (cp_parser *, bool);
0a3b29ad 1942static void cp_parser_using_directive
45baea8b 1943 (cp_parser *);
370478b1 1944static tree cp_parser_alias_declaration
1945 (cp_parser *);
0a3b29ad 1946static void cp_parser_asm_definition
45baea8b 1947 (cp_parser *);
0a3b29ad 1948static void cp_parser_linkage_specification
45baea8b 1949 (cp_parser *);
7a05c4b1 1950static void cp_parser_static_assert
1951 (cp_parser *, bool);
34da8800 1952static tree cp_parser_decltype
1953 (cp_parser *);
0a3b29ad 1954
1955/* Declarators [gram.dcl.decl] */
1956
1957static tree cp_parser_init_declarator
fa7d5870 1958 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
3046c0a3 1959static cp_declarator *cp_parser_declarator
08ea345c 1960 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
3046c0a3 1961static cp_declarator *cp_parser_direct_declarator
08ea345c 1962 (cp_parser *, cp_parser_declarator_kind, int *, bool);
0a3b29ad 1963static enum tree_code cp_parser_ptr_operator
2cfb6cde 1964 (cp_parser *, tree *, cp_cv_quals *);
1965static cp_cv_quals cp_parser_cv_qualifier_seq_opt
45baea8b 1966 (cp_parser *);
ece7f9e3 1967static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1968 (cp_parser *);
346e3a9c 1969static tree cp_parser_late_return_type_opt
0499ac79 1970 (cp_parser *, cp_cv_quals);
0a3b29ad 1971static tree cp_parser_declarator_id
197c9df7 1972 (cp_parser *, bool);
0a3b29ad 1973static tree cp_parser_type_id
45baea8b 1974 (cp_parser *);
75eaa947 1975static tree cp_parser_template_type_arg
1976 (cp_parser *);
638569c5 1977static tree cp_parser_trailing_type_id (cp_parser *);
75eaa947 1978static tree cp_parser_type_id_1
638569c5 1979 (cp_parser *, bool, bool);
4b9b2871 1980static void cp_parser_type_specifier_seq
638569c5 1981 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
34eac767 1982static tree cp_parser_parameter_declaration_clause
45baea8b 1983 (cp_parser *);
34eac767 1984static tree cp_parser_parameter_declaration_list
3046c0a3 1985 (cp_parser *, bool *);
1986static cp_parameter_declarator *cp_parser_parameter_declaration
92b128ed 1987 (cp_parser *, bool, bool *);
41341abd 1988static tree cp_parser_default_argument
1989 (cp_parser *, bool);
0a3b29ad 1990static void cp_parser_function_body
376a817b 1991 (cp_parser *, bool);
0a3b29ad 1992static tree cp_parser_initializer
878870b4 1993 (cp_parser *, bool *, bool *);
0a3b29ad 1994static tree cp_parser_initializer_clause
878870b4 1995 (cp_parser *, bool *);
f82f1250 1996static tree cp_parser_braced_list
1997 (cp_parser*, bool*);
c75b4594 1998static VEC(constructor_elt,gc) *cp_parser_initializer_list
878870b4 1999 (cp_parser *, bool *);
0a3b29ad 2000
2001static bool cp_parser_ctor_initializer_opt_and_function_body
376a817b 2002 (cp_parser *, bool);
0a3b29ad 2003
2004/* Classes [gram.class] */
2005
2006static tree cp_parser_class_name
e2ae55f2 2007 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
0a3b29ad 2008static tree cp_parser_class_specifier
45baea8b 2009 (cp_parser *);
0a3b29ad 2010static tree cp_parser_class_head
1a9b4c25 2011 (cp_parser *, bool *, tree *, tree *);
0a3b29ad 2012static enum tag_types cp_parser_class_key
45baea8b 2013 (cp_parser *);
0a3b29ad 2014static void cp_parser_member_specification_opt
45baea8b 2015 (cp_parser *);
0a3b29ad 2016static void cp_parser_member_declaration
45baea8b 2017 (cp_parser *);
0a3b29ad 2018static tree cp_parser_pure_specifier
45baea8b 2019 (cp_parser *);
0a3b29ad 2020static tree cp_parser_constant_initializer
45baea8b 2021 (cp_parser *);
0a3b29ad 2022
2023/* Derived classes [gram.class.derived] */
2024
2025static tree cp_parser_base_clause
45baea8b 2026 (cp_parser *);
0a3b29ad 2027static tree cp_parser_base_specifier
45baea8b 2028 (cp_parser *);
0a3b29ad 2029
2030/* Special member functions [gram.special] */
2031
2032static tree cp_parser_conversion_function_id
45baea8b 2033 (cp_parser *);
0a3b29ad 2034static tree cp_parser_conversion_type_id
45baea8b 2035 (cp_parser *);
3046c0a3 2036static cp_declarator *cp_parser_conversion_declarator_opt
45baea8b 2037 (cp_parser *);
0a3b29ad 2038static bool cp_parser_ctor_initializer_opt
45baea8b 2039 (cp_parser *);
0a3b29ad 2040static void cp_parser_mem_initializer_list
45baea8b 2041 (cp_parser *);
0a3b29ad 2042static tree cp_parser_mem_initializer
45baea8b 2043 (cp_parser *);
0a3b29ad 2044static tree cp_parser_mem_initializer_id
45baea8b 2045 (cp_parser *);
0a3b29ad 2046
2047/* Overloading [gram.over] */
2048
2049static tree cp_parser_operator_function_id
45baea8b 2050 (cp_parser *);
0a3b29ad 2051static tree cp_parser_operator
45baea8b 2052 (cp_parser *);
0a3b29ad 2053
2054/* Templates [gram.temp] */
2055
2056static void cp_parser_template_declaration
45baea8b 2057 (cp_parser *, bool);
0a3b29ad 2058static tree cp_parser_template_parameter_list
45baea8b 2059 (cp_parser *);
0a3b29ad 2060static tree cp_parser_template_parameter
d95d815d 2061 (cp_parser *, bool *, bool *);
0a3b29ad 2062static tree cp_parser_type_parameter
d95d815d 2063 (cp_parser *, bool *);
0a3b29ad 2064static tree cp_parser_template_id
3d0f901b 2065 (cp_parser *, bool, bool, bool);
0a3b29ad 2066static tree cp_parser_template_name
3d0f901b 2067 (cp_parser *, bool, bool, bool, bool *);
0a3b29ad 2068static tree cp_parser_template_argument_list
45baea8b 2069 (cp_parser *);
0a3b29ad 2070static tree cp_parser_template_argument
45baea8b 2071 (cp_parser *);
0a3b29ad 2072static void cp_parser_explicit_instantiation
45baea8b 2073 (cp_parser *);
0a3b29ad 2074static void cp_parser_explicit_specialization
45baea8b 2075 (cp_parser *);
0a3b29ad 2076
2077/* Exception handling [gram.exception] */
2078
ccb84981 2079static tree cp_parser_try_block
45baea8b 2080 (cp_parser *);
0a3b29ad 2081static bool cp_parser_function_try_block
45baea8b 2082 (cp_parser *);
0a3b29ad 2083static void cp_parser_handler_seq
45baea8b 2084 (cp_parser *);
0a3b29ad 2085static void cp_parser_handler
45baea8b 2086 (cp_parser *);
0a3b29ad 2087static tree cp_parser_exception_declaration
45baea8b 2088 (cp_parser *);
0a3b29ad 2089static tree cp_parser_throw_expression
45baea8b 2090 (cp_parser *);
0a3b29ad 2091static tree cp_parser_exception_specification_opt
45baea8b 2092 (cp_parser *);
0a3b29ad 2093static tree cp_parser_type_id_list
45baea8b 2094 (cp_parser *);
0a3b29ad 2095
2096/* GNU Extensions */
2097
2098static tree cp_parser_asm_specification_opt
45baea8b 2099 (cp_parser *);
0a3b29ad 2100static tree cp_parser_asm_operand_list
45baea8b 2101 (cp_parser *);
0a3b29ad 2102static tree cp_parser_asm_clobber_list
45baea8b 2103 (cp_parser *);
78f55ca8 2104static tree cp_parser_asm_label_list
2105 (cp_parser *);
0a3b29ad 2106static tree cp_parser_attributes_opt
45baea8b 2107 (cp_parser *);
0a3b29ad 2108static tree cp_parser_attribute_list
45baea8b 2109 (cp_parser *);
0a3b29ad 2110static bool cp_parser_extension_opt
45baea8b 2111 (cp_parser *, int *);
0a3b29ad 2112static void cp_parser_label_declaration
45baea8b 2113 (cp_parser *);
0a3b29ad 2114
4c0315d0 2115/* Transactional Memory Extensions */
2116
2117static tree cp_parser_transaction
2118 (cp_parser *, enum rid);
2119static tree cp_parser_transaction_expression
2120 (cp_parser *, enum rid);
2121static bool cp_parser_function_transaction
2122 (cp_parser *, enum rid);
2123static tree cp_parser_transaction_cancel
2124 (cp_parser *);
2125
b75b98aa 2126enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2127static bool cp_parser_pragma
2128 (cp_parser *, enum pragma_context);
2129
79408174 2130/* Objective-C++ Productions */
2131
2132static tree cp_parser_objc_message_receiver
2133 (cp_parser *);
2134static tree cp_parser_objc_message_args
2135 (cp_parser *);
2136static tree cp_parser_objc_message_expression
2137 (cp_parser *);
2138static tree cp_parser_objc_encode_expression
2139 (cp_parser *);
2140static tree cp_parser_objc_defs_expression
2141 (cp_parser *);
2142static tree cp_parser_objc_protocol_expression
2143 (cp_parser *);
2144static tree cp_parser_objc_selector_expression
2145 (cp_parser *);
2146static tree cp_parser_objc_expression
2147 (cp_parser *);
2148static bool cp_parser_objc_selector_p
2149 (enum cpp_ttype);
2150static tree cp_parser_objc_selector
2151 (cp_parser *);
2152static tree cp_parser_objc_protocol_refs_opt
2153 (cp_parser *);
2154static void cp_parser_objc_declaration
2155 (cp_parser *, tree);
2156static tree cp_parser_objc_statement
2157 (cp_parser *);
2158static bool cp_parser_objc_valid_prefix_attributes
2159 (cp_parser *, tree *);
2160static void cp_parser_objc_at_property_declaration
2161 (cp_parser *) ;
2162static void cp_parser_objc_at_synthesize_declaration
2163 (cp_parser *) ;
2164static void cp_parser_objc_at_dynamic_declaration
2165 (cp_parser *) ;
2166static tree cp_parser_objc_struct_declaration
2167 (cp_parser *) ;
7a4e126b 2168
0a3b29ad 2169/* Utility Routines */
2170
2171static tree cp_parser_lookup_name
ad9ae192 2172 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
0a3b29ad 2173static tree cp_parser_lookup_name_simple
ad9ae192 2174 (cp_parser *, tree, location_t);
0a3b29ad 2175static tree cp_parser_maybe_treat_template_as_class
2176 (tree, bool);
2177static bool cp_parser_check_declarator_template_parameters
ad9ae192 2178 (cp_parser *, cp_declarator *, location_t);
0a3b29ad 2179static bool cp_parser_check_template_parameters
7b07a15e 2180 (cp_parser *, unsigned, location_t, cp_declarator *);
a63bc44c 2181static tree cp_parser_simple_cast_expression
2182 (cp_parser *);
0a3b29ad 2183static tree cp_parser_global_scope_opt
130bb1d4 2184 (cp_parser *, bool);
0a3b29ad 2185static bool cp_parser_constructor_declarator_p
2186 (cp_parser *, bool);
2187static tree cp_parser_function_definition_from_specifiers_and_declarator
4b9b2871 2188 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
0a3b29ad 2189static tree cp_parser_function_definition_after_declarator
45baea8b 2190 (cp_parser *, bool);
0a3b29ad 2191static void cp_parser_template_declaration_after_export
45baea8b 2192 (cp_parser *, bool);
23010bc8 2193static void cp_parser_perform_template_parameter_access_checks
3369eb76 2194 (VEC (deferred_access_check,gc)*);
0a3b29ad 2195static tree cp_parser_single_declaration
0c032b46 2196 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
0a3b29ad 2197static tree cp_parser_functional_cast
45baea8b 2198 (cp_parser *, tree);
92b128ed 2199static tree cp_parser_save_member_function_body
4b9b2871 2200 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
3ab40fb9 2201static tree cp_parser_save_nsdmi
2202 (cp_parser *);
8534c3a3 2203static tree cp_parser_enclosed_template_argument_list
2204 (cp_parser *);
69b6679c 2205static void cp_parser_save_default_args
2206 (cp_parser *, tree);
0a3b29ad 2207static void cp_parser_late_parsing_for_member
45baea8b 2208 (cp_parser *, tree);
3ab40fb9 2209static tree cp_parser_late_parse_one_default_arg
2210 (cp_parser *, tree, tree, tree);
2211static void cp_parser_late_parsing_nsdmi
2212 (cp_parser *, tree);
0a3b29ad 2213static void cp_parser_late_parsing_default_args
af128372 2214 (cp_parser *, tree);
0a3b29ad 2215static tree cp_parser_sizeof_operand
45baea8b 2216 (cp_parser *, enum rid);
481451eb 2217static tree cp_parser_trait_expr
2218 (cp_parser *, enum rid);
0a3b29ad 2219static bool cp_parser_declares_only_class_p
45baea8b 2220 (cp_parser *);
4b9b2871 2221static void cp_parser_set_storage_class
ad9ae192 2222 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
207355ad 2223static void cp_parser_set_decl_spec_type
eef0ab03 2224 (cp_decl_specifier_seq *, tree, location_t, bool);
a60f3e81 2225static void set_and_check_decl_spec_loc
2226 (cp_decl_specifier_seq *decl_specs,
2227 cp_decl_spec ds, source_location location);
0a3b29ad 2228static bool cp_parser_friend_p
4b9b2871 2229 (const cp_decl_specifier_seq *);
c247dce0 2230static void cp_parser_required_error
2231 (cp_parser *, required_token, bool);
0a3b29ad 2232static cp_token *cp_parser_require
c247dce0 2233 (cp_parser *, enum cpp_ttype, required_token);
0a3b29ad 2234static cp_token *cp_parser_require_keyword
c247dce0 2235 (cp_parser *, enum rid, required_token);
ccb84981 2236static bool cp_parser_token_starts_function_definition_p
45baea8b 2237 (cp_token *);
0a3b29ad 2238static bool cp_parser_next_token_starts_class_definition_p
2239 (cp_parser *);
13795292 2240static bool cp_parser_next_token_ends_template_argument_p
2241 (cp_parser *);
c8d5ab79 2242static bool cp_parser_nth_token_starts_template_argument_list_p
2243 (cp_parser *, size_t);
0a3b29ad 2244static enum tag_types cp_parser_token_is_class_key
45baea8b 2245 (cp_token *);
0a3b29ad 2246static void cp_parser_check_class_key
2247 (enum tag_types, tree type);
7e35473e 2248static void cp_parser_check_access_in_redeclaration
ad9ae192 2249 (tree type, location_t location);
0a3b29ad 2250static bool cp_parser_optional_template_keyword
2251 (cp_parser *);
ccb84981 2252static void cp_parser_pre_parsed_nested_name_specifier
b3c48b5d 2253 (cp_parser *);
f82f1250 2254static bool cp_parser_cache_group
00d26680 2255 (cp_parser *, enum cpp_ttype, unsigned);
ce24dc8d 2256static tree cp_parser_cache_defarg
2257 (cp_parser *parser, bool nsdmi);
ccb84981 2258static void cp_parser_parse_tentatively
45baea8b 2259 (cp_parser *);
0a3b29ad 2260static void cp_parser_commit_to_tentative_parse
45baea8b 2261 (cp_parser *);
0a3b29ad 2262static void cp_parser_abort_tentative_parse
45baea8b 2263 (cp_parser *);
0a3b29ad 2264static bool cp_parser_parse_definitely
45baea8b 2265 (cp_parser *);
2370b5bf 2266static inline bool cp_parser_parsing_tentatively
45baea8b 2267 (cp_parser *);
efcbcf83 2268static bool cp_parser_uncommitted_to_tentative_parse_p
45baea8b 2269 (cp_parser *);
0a3b29ad 2270static void cp_parser_error
45baea8b 2271 (cp_parser *, const char *);
92b128ed 2272static void cp_parser_name_lookup_error
c247dce0 2273 (cp_parser *, tree, tree, name_lookup_error, location_t);
2c593bd0 2274static bool cp_parser_simulate_error
45baea8b 2275 (cp_parser *);
9a7c4b43 2276static bool cp_parser_check_type_definition
45baea8b 2277 (cp_parser *);
8172be22 2278static void cp_parser_check_for_definition_in_return_type
eef0ab03 2279 (cp_declarator *, tree, location_t type_location);
1157e9f0 2280static void cp_parser_check_for_invalid_template_id
eef0ab03 2281 (cp_parser *, tree, location_t location);
3938e0c2 2282static bool cp_parser_non_integral_constant_expression
c247dce0 2283 (cp_parser *, non_integral_constant);
e00c3963 2284static void cp_parser_diagnose_invalid_type_name
ad9ae192 2285 (cp_parser *, tree, tree, location_t);
e00c3963 2286static bool cp_parser_parse_and_diagnose_invalid_type_name
954ad420 2287 (cp_parser *);
0986fa22 2288static int cp_parser_skip_to_closing_parenthesis
3d0f901b 2289 (cp_parser *, bool, bool, bool);
0a3b29ad 2290static void cp_parser_skip_to_end_of_statement
45baea8b 2291 (cp_parser *);
cf91b86a 2292static void cp_parser_consume_semicolon_at_end_of_statement
2293 (cp_parser *);
0a3b29ad 2294static void cp_parser_skip_to_end_of_block_or_statement
45baea8b 2295 (cp_parser *);
9aad947b 2296static bool cp_parser_skip_to_closing_brace
0a3b29ad 2297 (cp_parser *);
c42e0e2d 2298static void cp_parser_skip_to_end_of_template_parameter_list
2299 (cp_parser *);
b75b98aa 2300static void cp_parser_skip_to_pragma_eol
2301 (cp_parser*, cp_token *);
0a3b29ad 2302static bool cp_parser_error_occurred
45baea8b 2303 (cp_parser *);
0a3b29ad 2304static bool cp_parser_allow_gnu_extensions_p
45baea8b 2305 (cp_parser *);
244db24d 2306static bool cp_parser_is_pure_string_literal
2307 (cp_token *);
0a3b29ad 2308static bool cp_parser_is_string_literal
45baea8b 2309 (cp_token *);
ccb84981 2310static bool cp_parser_is_keyword
45baea8b 2311 (cp_token *, enum rid);
e00c3963 2312static tree cp_parser_make_typename_type
ad9ae192 2313 (cp_parser *, tree, tree, location_t location);
63949b38 2314static cp_declarator * cp_parser_make_indirect_declarator
2315 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
0a3b29ad 2316
f1d555e3 2317/* Returns nonzero if we are parsing tentatively. */
2370b5bf 2318
2319static inline bool
45baea8b 2320cp_parser_parsing_tentatively (cp_parser* parser)
2370b5bf 2321{
2322 return parser->context->next != NULL;
2323}
2324
f1d555e3 2325/* Returns nonzero if TOKEN is a string literal. */
0a3b29ad 2326
2327static bool
244db24d 2328cp_parser_is_pure_string_literal (cp_token* token)
0a3b29ad 2329{
924bbf02 2330 return (token->type == CPP_STRING ||
2331 token->type == CPP_STRING16 ||
2332 token->type == CPP_STRING32 ||
538ba11a 2333 token->type == CPP_WSTRING ||
2334 token->type == CPP_UTF8STRING);
0a3b29ad 2335}
2336
244db24d 2337/* Returns nonzero if TOKEN is a string literal
2338 of a user-defined string literal. */
2339
2340static bool
2341cp_parser_is_string_literal (cp_token* token)
2342{
2343 return (cp_parser_is_pure_string_literal (token) ||
2344 token->type == CPP_STRING_USERDEF ||
2345 token->type == CPP_STRING16_USERDEF ||
2346 token->type == CPP_STRING32_USERDEF ||
2347 token->type == CPP_WSTRING_USERDEF ||
2348 token->type == CPP_UTF8STRING_USERDEF);
2349}
2350
f1d555e3 2351/* Returns nonzero if TOKEN is the indicated KEYWORD. */
0a3b29ad 2352
2353static bool
45baea8b 2354cp_parser_is_keyword (cp_token* token, enum rid keyword)
0a3b29ad 2355{
2356 return token->keyword == keyword;
2357}
2358
b9dd3954 2359/* If not parsing tentatively, issue a diagnostic of the form
2360 FILE:LINE: MESSAGE before TOKEN
2361 where TOKEN is the next token in the input stream. MESSAGE
2362 (specified by the caller) is usually of the form "expected
2363 OTHER-TOKEN". */
0a3b29ad 2364
2365static void
c247dce0 2366cp_parser_error (cp_parser* parser, const char* gmsgid)
0a3b29ad 2367{
2c593bd0 2368 if (!cp_parser_simulate_error (parser))
92b128ed 2369 {
b9dd3954 2370 cp_token *token = cp_lexer_peek_token (parser->lexer);
2371 /* This diagnostic makes more sense if it is tagged to the line
2372 of the token we just peeked at. */
2373 cp_lexer_set_source_position_from_token (token);
b75b98aa 2374
eb78e86f 2375 if (token->type == CPP_PRAGMA)
2376 {
ccb59bb6 2377 error_at (token->location,
2378 "%<#pragma%> is not allowed here");
b75b98aa 2379 cp_parser_skip_to_pragma_eol (parser, token);
eb78e86f 2380 return;
2381 }
b75b98aa 2382
c247dce0 2383 c_parse_error (gmsgid,
78662158 2384 /* Because c_parser_error does not understand
2385 CPP_KEYWORD, keywords are treated like
2386 identifiers. */
ccb84981 2387 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
ba99525e 2388 token->u.value, token->flags);
92b128ed 2389 }
2390}
2391
2392/* Issue an error about name-lookup failing. NAME is the
2393 IDENTIFIER_NODE DECL is the result of
2394 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2395 the thing that we hoped to find. */
2396
2397static void
2398cp_parser_name_lookup_error (cp_parser* parser,
2399 tree name,
2400 tree decl,
c247dce0 2401 name_lookup_error desired,
ad9ae192 2402 location_t location)
92b128ed 2403{
2404 /* If name lookup completely failed, tell the user that NAME was not
2405 declared. */
2406 if (decl == error_mark_node)
2407 {
2408 if (parser->scope && parser->scope != global_namespace)
ccb59bb6 2409 error_at (location, "%<%E::%E%> has not been declared",
2410 parser->scope, name);
92b128ed 2411 else if (parser->scope == global_namespace)
ccb59bb6 2412 error_at (location, "%<::%E%> has not been declared", name);
9031d10b 2413 else if (parser->object_scope
30aea172 2414 && !CLASS_TYPE_P (parser->object_scope))
ccb59bb6 2415 error_at (location, "request for member %qE in non-class type %qT",
2416 name, parser->object_scope);
30aea172 2417 else if (parser->object_scope)
ccb59bb6 2418 error_at (location, "%<%T::%E%> has not been declared",
2419 parser->object_scope, name);
92b128ed 2420 else
ccb59bb6 2421 error_at (location, "%qE has not been declared", name);
92b128ed 2422 }
2423 else if (parser->scope && parser->scope != global_namespace)
c247dce0 2424 {
2425 switch (desired)
2426 {
2427 case NLE_TYPE:
2428 error_at (location, "%<%E::%E%> is not a type",
2429 parser->scope, name);
2430 break;
2431 case NLE_CXX98:
2432 error_at (location, "%<%E::%E%> is not a class or namespace",
2433 parser->scope, name);
2434 break;
2435 case NLE_NOT_CXX98:
2436 error_at (location,
2437 "%<%E::%E%> is not a class, namespace, or enumeration",
2438 parser->scope, name);
2439 break;
2440 default:
2441 gcc_unreachable ();
2442
2443 }
2444 }
92b128ed 2445 else if (parser->scope == global_namespace)
c247dce0 2446 {
2447 switch (desired)
2448 {
2449 case NLE_TYPE:
2450 error_at (location, "%<::%E%> is not a type", name);
2451 break;
2452 case NLE_CXX98:
2453 error_at (location, "%<::%E%> is not a class or namespace", name);
2454 break;
2455 case NLE_NOT_CXX98:
2456 error_at (location,
2457 "%<::%E%> is not a class, namespace, or enumeration",
2458 name);
2459 break;
2460 default:
2461 gcc_unreachable ();
2462 }
2463 }
92b128ed 2464 else
c247dce0 2465 {
2466 switch (desired)
2467 {
2468 case NLE_TYPE:
2469 error_at (location, "%qE is not a type", name);
2470 break;
2471 case NLE_CXX98:
2472 error_at (location, "%qE is not a class or namespace", name);
2473 break;
2474 case NLE_NOT_CXX98:
2475 error_at (location,
2476 "%qE is not a class, namespace, or enumeration", name);
2477 break;
2478 default:
2479 gcc_unreachable ();
2480 }
2481 }
0a3b29ad 2482}
2483
2484/* If we are parsing tentatively, remember that an error has occurred
2c593bd0 2485 during this tentative parse. Returns true if the error was
e24657db 2486 simulated; false if a message should be issued by the caller. */
0a3b29ad 2487
2c593bd0 2488static bool
45baea8b 2489cp_parser_simulate_error (cp_parser* parser)
0a3b29ad 2490{
efcbcf83 2491 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2c593bd0 2492 {
2493 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2494 return true;
2495 }
2496 return false;
0a3b29ad 2497}
2498
2499/* This function is called when a type is defined. If type
2500 definitions are forbidden at this point, an error message is
2501 issued. */
2502
9a7c4b43 2503static bool
45baea8b 2504cp_parser_check_type_definition (cp_parser* parser)
0a3b29ad 2505{
2506 /* If types are forbidden here, issue a message. */
2507 if (parser->type_definition_forbidden_message)
9a7c4b43 2508 {
2e52ac87 2509 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2510 in the message need to be interpreted. */
2511 error (parser->type_definition_forbidden_message);
9a7c4b43 2512 return false;
2513 }
2514 return true;
0a3b29ad 2515}
2516
e2ae55f2 2517/* This function is called when the DECLARATOR is processed. The TYPE
0be5f5cc 2518 was a type defined in the decl-specifiers. If it is invalid to
e2ae55f2 2519 define a type in the decl-specifiers for DECLARATOR, an error is
eef0ab03 2520 issued. TYPE_LOCATION is the location of TYPE and is used
2521 for error reporting. */
8172be22 2522
2523static void
3046c0a3 2524cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
eef0ab03 2525 tree type, location_t type_location)
8172be22 2526{
2527 /* [dcl.fct] forbids type definitions in return types.
2528 Unfortunately, it's not easy to know whether or not we are
2529 processing a return type until after the fact. */
2530 while (declarator
3046c0a3 2531 && (declarator->kind == cdk_pointer
2532 || declarator->kind == cdk_reference
2533 || declarator->kind == cdk_ptrmem))
2534 declarator = declarator->declarator;
8172be22 2535 if (declarator
e2ae55f2 2536 && declarator->kind == cdk_function)
2537 {
ccb59bb6 2538 error_at (type_location,
2539 "new types may not be defined in a return type");
5bcc316e 2540 inform (type_location,
2541 "(perhaps a semicolon is missing after the definition of %qT)",
2542 type);
e2ae55f2 2543 }
8172be22 2544}
2545
1157e9f0 2546/* A type-specifier (TYPE) has been parsed which cannot be followed by
2547 "<" in any valid C++ program. If the next token is indeed "<",
2548 issue a message warning the user about what appears to be an
eef0ab03 2549 invalid attempt to form a template-id. LOCATION is the location
2550 of the type-specifier (TYPE) */
1157e9f0 2551
2552static void
ccb84981 2553cp_parser_check_for_invalid_template_id (cp_parser* parser,
eef0ab03 2554 tree type, location_t location)
1157e9f0 2555{
19273cc2 2556 cp_token_position start = 0;
1157e9f0 2557
2558 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2559 {
2560 if (TYPE_P (type))
ccb59bb6 2561 error_at (location, "%qT is not a template", type);
1157e9f0 2562 else if (TREE_CODE (type) == IDENTIFIER_NODE)
ccb59bb6 2563 error_at (location, "%qE is not a template", type);
1157e9f0 2564 else
ccb59bb6 2565 error_at (location, "invalid template-id");
1157e9f0 2566 /* Remember the location of the invalid "<". */
efcbcf83 2567 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 2568 start = cp_lexer_token_position (parser->lexer, true);
1157e9f0 2569 /* Consume the "<". */
2570 cp_lexer_consume_token (parser->lexer);
2571 /* Parse the template arguments. */
2572 cp_parser_enclosed_template_argument_list (parser);
a5268b2f 2573 /* Permanently remove the invalid template arguments so that
1157e9f0 2574 this error message is not issued again. */
19273cc2 2575 if (start)
2576 cp_lexer_purge_tokens_after (parser->lexer, start);
1157e9f0 2577 }
2578}
2579
3938e0c2 2580/* If parsing an integral constant-expression, issue an error message
2581 about the fact that THING appeared and return true. Otherwise,
640aa28c 2582 return false. In either case, set
9031d10b 2583 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
5f6526e1 2584
3938e0c2 2585static bool
2586cp_parser_non_integral_constant_expression (cp_parser *parser,
c247dce0 2587 non_integral_constant thing)
5f6526e1 2588{
640aa28c 2589 parser->non_integral_constant_expression_p = true;
3938e0c2 2590 if (parser->integral_constant_expression_p)
2591 {
2592 if (!parser->allow_non_integral_constant_expression_p)
2593 {
c247dce0 2594 const char *msg = NULL;
2595 switch (thing)
2596 {
2597 case NIC_FLOAT:
2598 error ("floating-point literal "
2599 "cannot appear in a constant-expression");
2600 return true;
2601 case NIC_CAST:
2602 error ("a cast to a type other than an integral or "
2603 "enumeration type cannot appear in a "
2604 "constant-expression");
2605 return true;
2606 case NIC_TYPEID:
2607 error ("%<typeid%> operator "
2608 "cannot appear in a constant-expression");
2609 return true;
2610 case NIC_NCC:
2611 error ("non-constant compound literals "
2612 "cannot appear in a constant-expression");
2613 return true;
2614 case NIC_FUNC_CALL:
2615 error ("a function call "
2616 "cannot appear in a constant-expression");
2617 return true;
2618 case NIC_INC:
2619 error ("an increment "
2620 "cannot appear in a constant-expression");
2621 return true;
2622 case NIC_DEC:
2623 error ("an decrement "
2624 "cannot appear in a constant-expression");
2625 return true;
2626 case NIC_ARRAY_REF:
2627 error ("an array reference "
2628 "cannot appear in a constant-expression");
2629 return true;
2630 case NIC_ADDR_LABEL:
2631 error ("the address of a label "
2632 "cannot appear in a constant-expression");
2633 return true;
2634 case NIC_OVERLOADED:
2635 error ("calls to overloaded operators "
2636 "cannot appear in a constant-expression");
2637 return true;
2638 case NIC_ASSIGNMENT:
2639 error ("an assignment cannot appear in a constant-expression");
2640 return true;
2641 case NIC_COMMA:
2642 error ("a comma operator "
2643 "cannot appear in a constant-expression");
2644 return true;
2645 case NIC_CONSTRUCTOR:
2646 error ("a call to a constructor "
2647 "cannot appear in a constant-expression");
2648 return true;
4c0315d0 2649 case NIC_TRANSACTION:
2650 error ("a transaction expression "
2651 "cannot appear in a constant-expression");
2652 return true;
c247dce0 2653 case NIC_THIS:
2654 msg = "this";
2655 break;
2656 case NIC_FUNC_NAME:
2657 msg = "__FUNCTION__";
2658 break;
2659 case NIC_PRETTY_FUNC:
2660 msg = "__PRETTY_FUNCTION__";
2661 break;
2662 case NIC_C99_FUNC:
2663 msg = "__func__";
2664 break;
2665 case NIC_VA_ARG:
2666 msg = "va_arg";
2667 break;
2668 case NIC_ARROW:
2669 msg = "->";
2670 break;
2671 case NIC_POINT:
2672 msg = ".";
2673 break;
2674 case NIC_STAR:
2675 msg = "*";
2676 break;
2677 case NIC_ADDR:
2678 msg = "&";
2679 break;
2680 case NIC_PREINCREMENT:
2681 msg = "++";
2682 break;
2683 case NIC_PREDECREMENT:
2684 msg = "--";
2685 break;
2686 case NIC_NEW:
2687 msg = "new";
2688 break;
2689 case NIC_DEL:
2690 msg = "delete";
2691 break;
2692 default:
2693 gcc_unreachable ();
2694 }
2695 if (msg)
2696 error ("%qs cannot appear in a constant-expression", msg);
3938e0c2 2697 return true;
2698 }
3938e0c2 2699 }
2700 return false;
5f6526e1 2701}
2702
d9da0685 2703/* Emit a diagnostic for an invalid type name. SCOPE is the
b41cf329 2704 qualifying scope (or NULL, if none) for ID. This function commits
2705 to the current active tentative parse, if any. (Otherwise, the
2706 problematic construct might be encountered again later, resulting
eef0ab03 2707 in duplicate error messages.) LOCATION is the location of ID. */
954ad420 2708
e00c3963 2709static void
ad9ae192 2710cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2711 tree scope, tree id,
eef0ab03 2712 location_t location)
bb93aad0 2713{
2714 tree decl, old_scope;
af3481d9 2715 cp_parser_commit_to_tentative_parse (parser);
e00c3963 2716 /* Try to lookup the identifier. */
2717 old_scope = parser->scope;
2718 parser->scope = scope;
eef0ab03 2719 decl = cp_parser_lookup_name_simple (parser, id, location);
e00c3963 2720 parser->scope = old_scope;
2721 /* If the lookup found a template-name, it means that the user forgot
e4bc96e2 2722 to specify an argument list. Emit a useful error message. */
e00c3963 2723 if (TREE_CODE (decl) == TEMPLATE_DECL)
ccb59bb6 2724 error_at (location,
2725 "invalid use of template-name %qE without an argument list",
2726 decl);
dea225ee 2727 else if (TREE_CODE (id) == BIT_NOT_EXPR)
ccb59bb6 2728 error_at (location, "invalid use of destructor %qD as a type", id);
2b443576 2729 else if (TREE_CODE (decl) == TYPE_DECL)
2730 /* Something like 'unsigned A a;' */
ccb59bb6 2731 error_at (location, "invalid combination of multiple type-specifiers");
b62240d5 2732 else if (!parser->scope)
954ad420 2733 {
954ad420 2734 /* Issue an error message. */
ccb59bb6 2735 error_at (location, "%qE does not name a type", id);
954ad420 2736 /* If we're in a template class, it's possible that the user was
2737 referring to a type from a base class. For example:
2738
2739 template <typename T> struct A { typedef T X; };
2740 template <typename T> struct B : public A<T> { X x; };
2741
2742 The user should have said "typename A<T>::X". */
03d0aa7e 2743 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
0d84dc2d 2744 inform (location, "C++11 %<constexpr%> only available with "
2745 "-std=c++11 or -std=gnu++11");
03d0aa7e 2746 else if (processing_template_decl && current_class_type
2747 && TYPE_BINFO (current_class_type))
954ad420 2748 {
2749 tree b;
2750
2751 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2752 b;
2753 b = TREE_CHAIN (b))
2754 {
2755 tree base_type = BINFO_TYPE (b);
ccb84981 2756 if (CLASS_TYPE_P (base_type)
7e9a6a16 2757 && dependent_type_p (base_type))
954ad420 2758 {
2759 tree field;
2760 /* Go from a particular instantiation of the
2761 template (which will have an empty TYPE_FIELDs),
2762 to the main version. */
f0a4ea5b 2763 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
954ad420 2764 for (field = TYPE_FIELDS (base_type);
2765 field;
1767a056 2766 field = DECL_CHAIN (field))
954ad420 2767 if (TREE_CODE (field) == TYPE_DECL
e00c3963 2768 && DECL_NAME (field) == id)
954ad420 2769 {
5bcc316e 2770 inform (location,
2771 "(perhaps %<typename %T::%E%> was intended)",
2772 BINFO_TYPE (b), id);
954ad420 2773 break;
2774 }
2775 if (field)
2776 break;
2777 }
2778 }
2779 }
954ad420 2780 }
e00c3963 2781 /* Here we diagnose qualified-ids where the scope is actually correct,
2782 but the identifier does not resolve to a valid type name. */
b62240d5 2783 else if (parser->scope != error_mark_node)
e00c3963 2784 {
2785 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
ccb59bb6 2786 error_at (location, "%qE in namespace %qE does not name a type",
2787 id, parser->scope);
a70e3c37 2788 else if (CLASS_TYPE_P (parser->scope)
5570fae0 2789 && constructor_name_p (id, parser->scope))
2790 {
2791 /* A<T>::A<T>() */
2792 error_at (location, "%<%T::%E%> names the constructor, not"
2793 " the type", parser->scope, id);
2794 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2795 error_at (location, "and %qT has no template constructors",
2796 parser->scope);
2797 }
5bd9bf06 2798 else if (TYPE_P (parser->scope)
2799 && dependent_scope_p (parser->scope))
67484828 2800 error_at (location, "need %<typename%> before %<%T::%E%> because "
2801 "%qT is a dependent scope",
5bd9bf06 2802 parser->scope, id, parser->scope);
e00c3963 2803 else if (TYPE_P (parser->scope))
e2f75d44 2804 error_at (location, "%qE in %q#T does not name a type",
ccb59bb6 2805 id, parser->scope);
e00c3963 2806 else
2e3e31d2 2807 gcc_unreachable ();
e00c3963 2808 }
2809}
954ad420 2810
e00c3963 2811/* Check for a common situation where a type-name should be present,
2812 but is not, and issue a sensible error message. Returns true if an
2813 invalid type-name was detected.
ccb84981 2814
e00c3963 2815 The situation handled by this function are variable declarations of the
ccb84981 2816 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2817 Usually, `ID' should name a type, but if we got here it means that it
e00c3963 2818 does not. We try to emit the best possible error message depending on
074ab442 2819 how exactly the id-expression looks like. */
e00c3963 2820
2821static bool
2822cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2823{
2824 tree id;
ad9ae192 2825 cp_token *token = cp_lexer_peek_token (parser->lexer);
e00c3963 2826
6dd3a38d 2827 /* Avoid duplicate error about ambiguous lookup. */
2828 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2829 {
2830 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2831 if (next->type == CPP_NAME && next->ambiguous_p)
2832 goto out;
2833 }
2834
e00c3963 2835 cp_parser_parse_tentatively (parser);
ccb84981 2836 id = cp_parser_id_expression (parser,
e00c3963 2837 /*template_keyword_p=*/false,
2838 /*check_dependency_p=*/true,
2839 /*template_p=*/NULL,
197c9df7 2840 /*declarator_p=*/true,
130bb1d4 2841 /*optional_p=*/false);
67484828 2842 /* If the next token is a (, this is a function with no explicit return
2843 type, i.e. constructor, destructor or conversion op. */
2844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7525f769 2845 || TREE_CODE (id) == TYPE_DECL)
e00c3963 2846 {
2847 cp_parser_abort_tentative_parse (parser);
2848 return false;
2849 }
7525f769 2850 if (!cp_parser_parse_definitely (parser))
e00c3963 2851 return false;
2852
e00c3963 2853 /* Emit a diagnostic for the invalid type. */
ad9ae192 2854 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2855 id, token->location);
6dd3a38d 2856 out:
67484828 2857 /* If we aren't in the middle of a declarator (i.e. in a
2858 parameter-declaration-clause), skip to the end of the declaration;
2859 there's no point in trying to process it. */
2860 if (!parser->in_declarator_p)
2861 cp_parser_skip_to_end_of_block_or_statement (parser);
e00c3963 2862 return true;
954ad420 2863}
2864
ccb84981 2865/* Consume tokens up to, and including, the next non-nested closing `)'.
0986fa22 2866 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2867 are doing error recovery. Returns -1 if OR_COMMA is true and we
2868 found an unnested comma. */
0a3b29ad 2869
0986fa22 2870static int
2871cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
ccb84981 2872 bool recovering,
3d0f901b 2873 bool or_comma,
2874 bool consume_paren)
0a3b29ad 2875{
0986fa22 2876 unsigned paren_depth = 0;
2877 unsigned brace_depth = 0;
a8b75081 2878 unsigned square_depth = 0;
0a3b29ad 2879
efcbcf83 2880 if (recovering && !or_comma
2881 && cp_parser_uncommitted_to_tentative_parse_p (parser))
0986fa22 2882 return 0;
ccb84981 2883
0a3b29ad 2884 while (true)
2885 {
b75b98aa 2886 cp_token * token = cp_lexer_peek_token (parser->lexer);
ccb84981 2887
b75b98aa 2888 switch (token->type)
3fe7c943 2889 {
b75b98aa 2890 case CPP_EOF:
2891 case CPP_PRAGMA_EOL:
2892 /* If we've run out of tokens, then there is no closing `)'. */
2893 return 0;
0a3b29ad 2894
a8b75081 2895 /* This is good for lambda expression capture-lists. */
2896 case CPP_OPEN_SQUARE:
2897 ++square_depth;
2898 break;
2899 case CPP_CLOSE_SQUARE:
2900 if (!square_depth--)
2901 return 0;
2902 break;
2903
b75b98aa 2904 case CPP_SEMICOLON:
2905 /* This matches the processing in skip_to_end_of_statement. */
2906 if (!brace_depth)
2907 return 0;
2908 break;
ccb84981 2909
b75b98aa 2910 case CPP_OPEN_BRACE:
2911 ++brace_depth;
3fe7c943 2912 break;
b75b98aa 2913 case CPP_CLOSE_BRACE:
3d0f901b 2914 if (!brace_depth--)
b75b98aa 2915 return 0;
3fe7c943 2916 break;
ccb84981 2917
b75b98aa 2918 case CPP_COMMA:
a8b75081 2919 if (recovering && or_comma && !brace_depth && !paren_depth
2920 && !square_depth)
b75b98aa 2921 return -1;
2922 break;
2923
2924 case CPP_OPEN_PAREN:
2925 if (!brace_depth)
0986fa22 2926 ++paren_depth;
b75b98aa 2927 break;
2928
2929 case CPP_CLOSE_PAREN:
2930 if (!brace_depth && !paren_depth--)
3d0f901b 2931 {
2932 if (consume_paren)
2933 cp_lexer_consume_token (parser->lexer);
b75b98aa 2934 return 1;
3d0f901b 2935 }
b75b98aa 2936 break;
2937
2938 default:
2939 break;
0986fa22 2940 }
ccb84981 2941
3d0f901b 2942 /* Consume the token. */
2943 cp_lexer_consume_token (parser->lexer);
0a3b29ad 2944 }
2945}
2946
2947/* Consume tokens until we reach the end of the current statement.
2948 Normally, that will be just before consuming a `;'. However, if a
2949 non-nested `}' comes first, then we stop before consuming that. */
2950
2951static void
45baea8b 2952cp_parser_skip_to_end_of_statement (cp_parser* parser)
0a3b29ad 2953{
2954 unsigned nesting_depth = 0;
2955
2956 while (true)
2957 {
b75b98aa 2958 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 2959
b75b98aa 2960 switch (token->type)
0a3b29ad 2961 {
b75b98aa 2962 case CPP_EOF:
2963 case CPP_PRAGMA_EOL:
2964 /* If we've run out of tokens, stop. */
2965 return;
2966
2967 case CPP_SEMICOLON:
2968 /* If the next token is a `;', we have reached the end of the
2969 statement. */
2970 if (!nesting_depth)
2971 return;
2972 break;
2973
2974 case CPP_CLOSE_BRACE:
2975 /* If this is a non-nested '}', stop before consuming it.
0a3b29ad 2976 That way, when confronted with something like:
2977
ccb84981 2978 { 3 + }
0a3b29ad 2979
b75b98aa 2980 we stop before consuming the closing '}', even though we
0a3b29ad 2981 have not yet reached a `;'. */
2982 if (nesting_depth == 0)
b75b98aa 2983 return;
2984
2985 /* If it is the closing '}' for a block that we have
0a3b29ad 2986 scanned, stop -- but only after consuming the token.
2987 That way given:
2988
653e5405 2989 void f g () { ... }
0a3b29ad 2990 typedef int I;
2991
2992 we will stop after the body of the erroneously declared
2993 function, but before consuming the following `typedef'
2994 declaration. */
2995 if (--nesting_depth == 0)
2996 {
2997 cp_lexer_consume_token (parser->lexer);
b75b98aa 2998 return;
0a3b29ad 2999 }
b75b98aa 3000
3001 case CPP_OPEN_BRACE:
3002 ++nesting_depth;
3003 break;
3004
3005 default:
3006 break;
0a3b29ad 3007 }
b75b98aa 3008
0a3b29ad 3009 /* Consume the token. */
3010 cp_lexer_consume_token (parser->lexer);
3011 }
3012}
3013
cf91b86a 3014/* This function is called at the end of a statement or declaration.
3015 If the next token is a semicolon, it is consumed; otherwise, error
3016 recovery is attempted. */
3017
3018static void
3019cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3020{
3021 /* Look for the trailing `;'. */
c247dce0 3022 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
cf91b86a 3023 {
3024 /* If there is additional (erroneous) input, skip to the end of
3025 the statement. */
3026 cp_parser_skip_to_end_of_statement (parser);
3027 /* If the next token is now a `;', consume it. */
3028 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3029 cp_lexer_consume_token (parser->lexer);
3030 }
3031}
3032
0a3b29ad 3033/* Skip tokens until we have consumed an entire block, or until we
3034 have consumed a non-nested `;'. */
3035
3036static void
45baea8b 3037cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
0a3b29ad 3038{
c75ae97e 3039 int nesting_depth = 0;
0a3b29ad 3040
c75ae97e 3041 while (nesting_depth >= 0)
0a3b29ad 3042 {
c75ae97e 3043 cp_token *token = cp_lexer_peek_token (parser->lexer);
9031d10b 3044
c75ae97e 3045 switch (token->type)
0a3b29ad 3046 {
c75ae97e 3047 case CPP_EOF:
b75b98aa 3048 case CPP_PRAGMA_EOL:
c75ae97e 3049 /* If we've run out of tokens, stop. */
b75b98aa 3050 return;
c75ae97e 3051
3052 case CPP_SEMICOLON:
3053 /* Stop if this is an unnested ';'. */
3054 if (!nesting_depth)
3055 nesting_depth = -1;
3056 break;
3057
3058 case CPP_CLOSE_BRACE:
3059 /* Stop if this is an unnested '}', or closes the outermost
3060 nesting level. */
3061 nesting_depth--;
926c5baf 3062 if (nesting_depth < 0)
3063 return;
c75ae97e 3064 if (!nesting_depth)
3065 nesting_depth = -1;
3066 break;
9031d10b 3067
c75ae97e 3068 case CPP_OPEN_BRACE:
3069 /* Nest. */
3070 nesting_depth++;
3071 break;
3072
3073 default:
0a3b29ad 3074 break;
3075 }
9031d10b 3076
0a3b29ad 3077 /* Consume the token. */
c75ae97e 3078 cp_lexer_consume_token (parser->lexer);
0a3b29ad 3079 }
3080}
3081
3082/* Skip tokens until a non-nested closing curly brace is the next
9aad947b 3083 token, or there are no more tokens. Return true in the first case,
3084 false otherwise. */
0a3b29ad 3085
9aad947b 3086static bool
0a3b29ad 3087cp_parser_skip_to_closing_brace (cp_parser *parser)
3088{
3089 unsigned nesting_depth = 0;
3090
3091 while (true)
3092 {
b75b98aa 3093 cp_token *token = cp_lexer_peek_token (parser->lexer);
3094
3095 switch (token->type)
3096 {
3097 case CPP_EOF:
3098 case CPP_PRAGMA_EOL:
3099 /* If we've run out of tokens, stop. */
9aad947b 3100 return false;
b75b98aa 3101
3102 case CPP_CLOSE_BRACE:
3103 /* If the next token is a non-nested `}', then we have reached
3104 the end of the current block. */
3105 if (nesting_depth-- == 0)
9aad947b 3106 return true;
b75b98aa 3107 break;
3108
3109 case CPP_OPEN_BRACE:
3110 /* If it the next token is a `{', then we are entering a new
3111 block. Consume the entire block. */
3112 ++nesting_depth;
3113 break;
3114
3115 default:
3116 break;
3117 }
0a3b29ad 3118
0a3b29ad 3119 /* Consume the token. */
3120 cp_lexer_consume_token (parser->lexer);
3121 }
3122}
3123
b75b98aa 3124/* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3125 parameter is the PRAGMA token, allowing us to purge the entire pragma
3126 sequence. */
3127
3128static void
3129cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3130{
3131 cp_token *token;
3132
3133 parser->lexer->in_pragma = false;
3134
3135 do
3136 token = cp_lexer_consume_token (parser->lexer);
3137 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3138
3139 /* Ensure that the pragma is not parsed again. */
3140 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3141}
3142
8487df40 3143/* Require pragma end of line, resyncing with it as necessary. The
3144 arguments are as for cp_parser_skip_to_pragma_eol. */
3145
3146static void
3147cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3148{
3149 parser->lexer->in_pragma = false;
c247dce0 3150 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
8487df40 3151 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3152}
3153
e00c3963 3154/* This is a simple wrapper around make_typename_type. When the id is
3155 an unresolved identifier node, we can provide a superior diagnostic
3156 using cp_parser_diagnose_invalid_type_name. */
3157
3158static tree
ad9ae192 3159cp_parser_make_typename_type (cp_parser *parser, tree scope,
3160 tree id, location_t id_location)
bb93aad0 3161{
3162 tree result;
3163 if (TREE_CODE (id) == IDENTIFIER_NODE)
3164 {
e2ae55f2 3165 result = make_typename_type (scope, id, typename_type,
074ab442 3166 /*complain=*/tf_none);
bb93aad0 3167 if (result == error_mark_node)
ad9ae192 3168 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
bb93aad0 3169 return result;
3170 }
e2ae55f2 3171 return make_typename_type (scope, id, typename_type, tf_error);
e00c3963 3172}
3173
63949b38 3174/* This is a wrapper around the
3175 make_{pointer,ptrmem,reference}_declarator functions that decides
3176 which one to call based on the CODE and CLASS_TYPE arguments. The
3177 CODE argument should be one of the values returned by
3178 cp_parser_ptr_operator. */
3179static cp_declarator *
3180cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3181 cp_cv_quals cv_qualifiers,
3182 cp_declarator *target)
3183{
416c300e 3184 if (code == ERROR_MARK)
3185 return cp_error_declarator;
3186
63949b38 3187 if (code == INDIRECT_REF)
3188 if (class_type == NULL_TREE)
3189 return make_pointer_declarator (cv_qualifiers, target);
3190 else
3191 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3192 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3193 return make_reference_declarator (cv_qualifiers, target, false);
3194 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3195 return make_reference_declarator (cv_qualifiers, target, true);
3196 gcc_unreachable ();
3197}
e00c3963 3198
0a3b29ad 3199/* Create a new C++ parser. */
3200
3201static cp_parser *
45baea8b 3202cp_parser_new (void)
0a3b29ad 3203{
3204 cp_parser *parser;
573aba85 3205 cp_lexer *lexer;
0a88af73 3206 unsigned i;
573aba85 3207
ba72912a 3208 /* cp_lexer_new_main is called before doing GC allocation because
573aba85 3209 cp_lexer_new_main might load a PCH file. */
3210 lexer = cp_lexer_new_main ();
0a3b29ad 3211
0a88af73 3212 /* Initialize the binops_by_token so that we can get the tree
3213 directly from the token. */
3214 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3215 binops_by_token[binops[i].token_type] = binops[i];
3216
ba72912a 3217 parser = ggc_alloc_cleared_cp_parser ();
573aba85 3218 parser->lexer = lexer;
0a3b29ad 3219 parser->context = cp_parser_context_new (NULL);
3220
3221 /* For now, we always accept GNU extensions. */
3222 parser->allow_gnu_extensions_p = 1;
3223
3224 /* The `>' token is a greater-than operator, not the end of a
3225 template-id. */
3226 parser->greater_than_is_operator_p = true;
3227
3228 parser->default_arg_ok_p = true;
ccb84981 3229
0a3b29ad 3230 /* We are not parsing a constant-expression. */
f47c1747 3231 parser->integral_constant_expression_p = false;
3232 parser->allow_non_integral_constant_expression_p = false;
3233 parser->non_integral_constant_expression_p = false;
0a3b29ad 3234
3235 /* Local variable names are not forbidden. */
3236 parser->local_variables_forbidden_p = false;
3237
755edffd 3238 /* We are not processing an `extern "C"' declaration. */
0a3b29ad 3239 parser->in_unbraced_linkage_specification_p = false;
3240
3241 /* We are not processing a declarator. */
3242 parser->in_declarator_p = false;
3243
92b128ed 3244 /* We are not processing a template-argument-list. */
3245 parser->in_template_argument_list_p = false;
3246
c3fbce20 3247 /* We are not in an iteration statement. */
8487df40 3248 parser->in_statement = 0;
c3fbce20 3249
3250 /* We are not in a switch statement. */
3251 parser->in_switch_statement_p = false;
3252
41f2d08e 3253 /* We are not parsing a type-id inside an expression. */
3254 parser->in_type_id_in_expr_p = false;
3255
93523877 3256 /* Declarations aren't implicitly extern "C". */
2e1f41a9 3257 parser->implicit_extern_c = false;
3258
00d26680 3259 /* String literals should be translated to the execution character set. */
3260 parser->translate_strings_p = true;
3261
0aeb1cc5 3262 /* We are not parsing a function body. */
3263 parser->in_function_body = false;
3264
1e838f42 3265 /* We can correct until told otherwise. */
3266 parser->colon_corrects_to_scope_p = true;
3267
0a3b29ad 3268 /* The unparsed function queue is empty. */
9177da82 3269 push_unparsed_function_queues (parser);
0a3b29ad 3270
3271 /* There are no classes being defined. */
3272 parser->num_classes_being_defined = 0;
3273
3274 /* No template parameters apply. */
3275 parser->num_template_parameter_lists = 0;
3276
3277 return parser;
3278}
3279
b9dd3954 3280/* Create a cp_lexer structure which will emit the tokens in CACHE
3281 and push it onto the parser's lexer stack. This is used for delayed
3282 parsing of in-class method bodies and default arguments, and should
3283 not be confused with tentative parsing. */
3284static void
3285cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3286{
3287 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3288 lexer->next = parser->lexer;
3289 parser->lexer = lexer;
3290
3291 /* Move the current source position to that of the first token in the
3292 new lexer. */
3293 cp_lexer_set_source_position_from_token (lexer->next_token);
3294}
3295
3296/* Pop the top lexer off the parser stack. This is never used for the
3297 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3298static void
3299cp_parser_pop_lexer (cp_parser *parser)
3300{
3301 cp_lexer *lexer = parser->lexer;
3302 parser->lexer = lexer->next;
3303 cp_lexer_destroy (lexer);
3304
3305 /* Put the current source position back where it was before this
3306 lexer was pushed. */
3307 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3308}
3309
0a3b29ad 3310/* Lexical conventions [gram.lex] */
3311
3312/* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3313 identifier. */
3314
ccb84981 3315static tree
45baea8b 3316cp_parser_identifier (cp_parser* parser)
0a3b29ad 3317{
3318 cp_token *token;
3319
3320 /* Look for the identifier. */
c247dce0 3321 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
0a3b29ad 3322 /* Return the value. */
3369eb76 3323 return token ? token->u.value : error_mark_node;
0a3b29ad 3324}
3325
00d26680 3326/* Parse a sequence of adjacent string constants. Returns a
3327 TREE_STRING representing the combined, nul-terminated string
3328 constant. If TRANSLATE is true, translate the string to the
3329 execution character set. If WIDE_OK is true, a wide string is
3330 invalid here.
3331
3332 C++98 [lex.string] says that if a narrow string literal token is
3333 adjacent to a wide string literal token, the behavior is undefined.
3334 However, C99 6.4.5p4 says that this results in a wide string literal.
3335 We follow C99 here, for consistency with the C front end.
3336
3337 This code is largely lifted from lex_string() in c-lex.c.
3338
3339 FUTURE: ObjC++ will need to handle @-strings here. */
3340static tree
3341cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3342{
3343 tree value;
00d26680 3344 size_t count;
3345 struct obstack str_ob;
3346 cpp_string str, istr, *strs;
3347 cp_token *tok;
244db24d 3348 enum cpp_ttype type, curr_type;
3349 int have_suffix_p = 0;
3350 tree string_tree;
3351 tree suffix_id = NULL_TREE;
3352 bool curr_tok_is_userdef_p = false;
00d26680 3353
3354 tok = cp_lexer_peek_token (parser->lexer);
3355 if (!cp_parser_is_string_literal (tok))
3356 {
3357 cp_parser_error (parser, "expected string-literal");
3358 return error_mark_node;
3359 }
3360
244db24d 3361 if (cpp_userdef_string_p (tok->type))
3362 {
3363 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3364 curr_type = cpp_userdef_string_remove_type (tok->type);
3365 curr_tok_is_userdef_p = true;
3366 }
3367 else
3368 {
3369 string_tree = tok->u.value;
3370 curr_type = tok->type;
3371 }
3372 type = curr_type;
924bbf02 3373
2e533eb1 3374 /* Try to avoid the overhead of creating and destroying an obstack
00d26680 3375 for the common case of just one string. */
b9dd3954 3376 if (!cp_parser_is_string_literal
3377 (cp_lexer_peek_nth_token (parser->lexer, 2)))
00d26680 3378 {
b9dd3954 3379 cp_lexer_consume_token (parser->lexer);
3380
244db24d 3381 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3382 str.len = TREE_STRING_LENGTH (string_tree);
00d26680 3383 count = 1;
00d26680 3384
244db24d 3385 if (curr_tok_is_userdef_p)
3386 {
3387 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3388 have_suffix_p = 1;
3389 curr_type = cpp_userdef_string_remove_type (tok->type);
3390 }
3391 else
3392 curr_type = tok->type;
3393
00d26680 3394 strs = &str;
3395 }
3396 else
3397 {
3398 gcc_obstack_init (&str_ob);
3399 count = 0;
3400
3401 do
3402 {
b9dd3954 3403 cp_lexer_consume_token (parser->lexer);
00d26680 3404 count++;
244db24d 3405 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3406 str.len = TREE_STRING_LENGTH (string_tree);
924bbf02 3407
244db24d 3408 if (curr_tok_is_userdef_p)
3409 {
3410 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3411 if (have_suffix_p == 0)
3412 {
3413 suffix_id = curr_suffix_id;
3414 have_suffix_p = 1;
3415 }
3416 else if (have_suffix_p == 1
3417 && curr_suffix_id != suffix_id)
3418 {
3419 error ("inconsistent user-defined literal suffixes"
3420 " %qD and %qD in string literal",
3421 suffix_id, curr_suffix_id);
3422 have_suffix_p = -1;
3423 }
3424 curr_type = cpp_userdef_string_remove_type (tok->type);
3425 }
3426 else
3427 curr_type = tok->type;
3428
3429 if (type != curr_type)
924bbf02 3430 {
3431 if (type == CPP_STRING)
244db24d 3432 type = curr_type;
3433 else if (curr_type != CPP_STRING)
ccb59bb6 3434 error_at (tok->location,
3435 "unsupported non-standard concatenation "
3436 "of string literals");
924bbf02 3437 }
00d26680 3438
3439 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3440
b9dd3954 3441 tok = cp_lexer_peek_token (parser->lexer);
244db24d 3442 if (cpp_userdef_string_p (tok->type))
3443 {
3444 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3445 curr_type = cpp_userdef_string_remove_type (tok->type);
3446 curr_tok_is_userdef_p = true;
3447 }
3448 else
3449 {
3450 string_tree = tok->u.value;
3451 curr_type = tok->type;
3452 curr_tok_is_userdef_p = false;
3453 }
00d26680 3454 }
3455 while (cp_parser_is_string_literal (tok));
3456
3457 strs = (cpp_string *) obstack_finish (&str_ob);
3458 }
3459
924bbf02 3460 if (type != CPP_STRING && !wide_ok)
00d26680 3461 {
3462 cp_parser_error (parser, "a wide string is invalid in this context");
924bbf02 3463 type = CPP_STRING;
00d26680 3464 }
3465
3466 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
924bbf02 3467 (parse_in, strs, count, &istr, type))
00d26680 3468 {
c21f4fcd 3469 value = build_string (istr.len, (const char *)istr.text);
e47a6f81 3470 free (CONST_CAST (unsigned char *, istr.text));
00d26680 3471
924bbf02 3472 switch (type)
3473 {
3474 default:
3475 case CPP_STRING:
538ba11a 3476 case CPP_UTF8STRING:
924bbf02 3477 TREE_TYPE (value) = char_array_type_node;
3478 break;
3479 case CPP_STRING16:
3480 TREE_TYPE (value) = char16_array_type_node;
3481 break;
3482 case CPP_STRING32:
3483 TREE_TYPE (value) = char32_array_type_node;
3484 break;
3485 case CPP_WSTRING:
3486 TREE_TYPE (value) = wchar_array_type_node;
3487 break;
3488 }
3489
00d26680 3490 value = fix_string_type (value);
244db24d 3491
3492 if (have_suffix_p)
3493 {
3494 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3495 tok->u.value = literal;
3496 return cp_parser_userdef_string_literal (tok);
3497 }
00d26680 3498 }
3499 else
3500 /* cpp_interpret_string has issued an error. */
3501 value = error_mark_node;
3502
3503 if (count > 1)
3504 obstack_free (&str_ob, 0);
3505
3506 return value;
3507}
3508
b06c89b6 3509/* Look up a literal operator with the name and the exact arguments. */
3510
3511static tree
3512lookup_literal_operator (tree name, VEC(tree,gc) *args)
3513{
3514 tree decl, fns;
3515 decl = lookup_name (name);
65d0898b 3516 if (!decl || !is_overloaded_fn (decl))
b06c89b6 3517 return error_mark_node;
3518
3519 for (fns = decl; fns; fns = OVL_NEXT (fns))
3520 {
3521 unsigned int ix;
3522 bool found = true;
3523 tree fn = OVL_CURRENT (fns);
3524 tree argtypes = NULL_TREE;
3525 argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3526 if (argtypes != NULL_TREE)
3527 {
3528 for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3529 ++ix, argtypes = TREE_CHAIN (argtypes))
3530 {
3531 tree targ = TREE_VALUE (argtypes);
3532 tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3533 bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3534 bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3535 if ((ptr || arr || !same_type_p (targ, tparm))
3536 && (!ptr || !arr
3537 || !same_type_p (TREE_TYPE (targ),
3538 TREE_TYPE (tparm))))
3539 found = false;
3540 }
66468a32 3541 if (found
3542 && ix == VEC_length (tree, args)
3543 /* May be this should be sufficient_parms_p instead,
3544 depending on how exactly should user-defined literals
3545 work in presence of default arguments on the literal
3546 operator parameters. */
3547 && argtypes == void_list_node)
b06c89b6 3548 return fn;
3549 }
3550 }
3551
3552 return error_mark_node;
3553}
3554
244db24d 3555/* Parse a user-defined char constant. Returns a call to a user-defined
3556 literal operator taking the character as an argument. */
3557
3558static tree
3559cp_parser_userdef_char_literal (cp_parser *parser)
3560{
b06c89b6 3561 cp_token *token = cp_lexer_consume_token (parser->lexer);
3562 tree literal = token->u.value;
3563 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3564 tree value = USERDEF_LITERAL_VALUE (literal);
3565 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3566 tree decl, result;
244db24d 3567
3568 /* Build up a call to the user-defined operator */
3569 /* Lookup the name we got back from the id-expression. */
b06c89b6 3570 VEC(tree,gc) *args = make_tree_vector ();
3571 VEC_safe_push (tree, gc, args, value);
3572 decl = lookup_literal_operator (name, args);
244db24d 3573 if (!decl || decl == error_mark_node)
3574 {
b06c89b6 3575 error ("unable to find character literal operator %qD with %qT argument",
3576 name, TREE_TYPE (value));
3577 release_tree_vector (args);
244db24d 3578 return error_mark_node;
3579 }
b06c89b6 3580 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3581 release_tree_vector (args);
3582 if (result != error_mark_node)
3583 return result;
244db24d 3584
b06c89b6 3585 error ("unable to find character literal operator %qD with %qT argument",
3586 name, TREE_TYPE (value));
3587 return error_mark_node;
244db24d 3588}
3589
3590/* A subroutine of cp_parser_userdef_numeric_literal to
3591 create a char... template parameter pack from a string node. */
3592
3593static tree
3594make_char_string_pack (tree value)
3595{
3596 tree charvec;
3597 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3598 const char *str = TREE_STRING_POINTER (value);
3599 int i, len = TREE_STRING_LENGTH (value) - 1;
3600 tree argvec = make_tree_vec (1);
3601
3602 /* Fill in CHARVEC with all of the parameters. */
3603 charvec = make_tree_vec (len);
3604 for (i = 0; i < len; ++i)
3605 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3606
3607 /* Build the argument packs. */
3608 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3609 TREE_TYPE (argpack) = char_type_node;
3610
3611 TREE_VEC_ELT (argvec, 0) = argpack;
3612
3613 return argvec;
3614}
3615
3616/* Parse a user-defined numeric constant. returns a call to a user-defined
3617 literal operator. */
3618
3619static tree
3620cp_parser_userdef_numeric_literal (cp_parser *parser)
3621{
b06c89b6 3622 cp_token *token = cp_lexer_consume_token (parser->lexer);
3623 tree literal = token->u.value;
3624 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3625 tree value = USERDEF_LITERAL_VALUE (literal);
3626 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3627 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3628 tree decl, result;
244db24d 3629 VEC(tree,gc) *args;
3630
b06c89b6 3631 /* Look for a literal operator taking the exact type of numeric argument
3632 as the literal value. */
244db24d 3633 args = make_tree_vector ();
3634 VEC_safe_push (tree, gc, args, value);
b06c89b6 3635 decl = lookup_literal_operator (name, args);
244db24d 3636 if (decl && decl != error_mark_node)
3637 {
3638 result = finish_call_expr (decl, &args, false, true, tf_none);
3639 if (result != error_mark_node)
3640 {
3641 release_tree_vector (args);
3642 return result;
3643 }
3644 }
3645 release_tree_vector (args);
3646
3647 /* If the numeric argument didn't work, look for a raw literal
3648 operator taking a const char* argument consisting of the number
3649 in string format. */
3650 args = make_tree_vector ();
3651 VEC_safe_push (tree, gc, args, num_string);
b06c89b6 3652 decl = lookup_literal_operator (name, args);
244db24d 3653 if (decl && decl != error_mark_node)
3654 {
3655 result = finish_call_expr (decl, &args, false, true, tf_none);
3656 if (result != error_mark_node)
3657 {
3658 release_tree_vector (args);
3659 return result;
3660 }
3661 }
3662 release_tree_vector (args);
3663
3664 /* If the raw literal didn't work, look for a non-type template
3665 function with parameter pack char.... Call the function with
3666 template parameter characters representing the number. */
3667 args = make_tree_vector ();
b06c89b6 3668 decl = lookup_literal_operator (name, args);
244db24d 3669 if (decl && decl != error_mark_node)
3670 {
3671 tree tmpl_args = make_char_string_pack (num_string);
3672 decl = lookup_template_function (decl, tmpl_args);
3673 result = finish_call_expr (decl, &args, false, true, tf_none);
3674 if (result != error_mark_node)
3675 {
3676 release_tree_vector (args);
3677 return result;
3678 }
3679 }
3680 release_tree_vector (args);
3681
b06c89b6 3682 error ("unable to find numeric literal operator %qD", name);
3683 return error_mark_node;
244db24d 3684}
3685
3686/* Parse a user-defined string constant. Returns a call to a user-defined
3687 literal operator taking a character pointer and the length of the string
3688 as arguments. */
5c1b1ffa 3689
244db24d 3690static tree
3691cp_parser_userdef_string_literal (cp_token *token)
3692{
b06c89b6 3693 tree literal = token->u.value;
3694 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3695 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3696 tree value = USERDEF_LITERAL_VALUE (literal);
3697 int len = TREE_STRING_LENGTH (value)
4d6f53f4 3698 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
b06c89b6 3699 tree decl, result;
3700
244db24d 3701 /* Build up a call to the user-defined operator */
3702 /* Lookup the name we got back from the id-expression. */
b06c89b6 3703 VEC(tree,gc) *args = make_tree_vector ();
3704 VEC_safe_push (tree, gc, args, value);
3705 VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3706 decl = lookup_name (name);
244db24d 3707 if (!decl || decl == error_mark_node)
3708 {
b06c89b6 3709 error ("unable to find string literal operator %qD", name);
3710 release_tree_vector (args);
244db24d 3711 return error_mark_node;
3712 }
b06c89b6 3713 result = finish_call_expr (decl, &args, false, true, tf_none);
3714 release_tree_vector (args);
3715 if (result != error_mark_node)
3716 return result;
244db24d 3717
b06c89b6 3718 error ("unable to find string literal operator %qD with %qT, %qT arguments",
3719 name, TREE_TYPE (value), size_type_node);
3720 return error_mark_node;
244db24d 3721}
3722
00d26680 3723
0a3b29ad 3724/* Basic concepts [gram.basic] */
3725
3726/* Parse a translation-unit.
3727
3728 translation-unit:
ccb84981 3729 declaration-seq [opt]
0a3b29ad 3730
3731 Returns TRUE if all went well. */
3732
3733static bool
45baea8b 3734cp_parser_translation_unit (cp_parser* parser)
0a3b29ad 3735{
3046c0a3 3736 /* The address of the first non-permanent object on the declarator
3737 obstack. */
3738 static void *declarator_obstack_base;
3739
3740 bool success;
207355ad 3741
3046c0a3 3742 /* Create the declarator obstack, if necessary. */
3743 if (!cp_error_declarator)
3744 {
3745 gcc_obstack_init (&declarator_obstack);
3746 /* Create the error declarator. */
3747 cp_error_declarator = make_declarator (cdk_error);
3748 /* Create the empty parameter list. */
4b9b2871 3749 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3046c0a3 3750 /* Remember where the base of the declarator obstack lies. */
3751 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3752 }
3753
d88386ce 3754 cp_parser_declaration_seq_opt (parser);
074ab442 3755
d88386ce 3756 /* If there are no tokens left then all went well. */
3757 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
0a3b29ad 3758 {
d88386ce 3759 /* Get rid of the token array; we don't need it any more. */
3760 cp_lexer_destroy (parser->lexer);
3761 parser->lexer = NULL;
074ab442 3762
d88386ce 3763 /* This file might have been a context that's implicitly extern
074ab442 3764 "C". If so, pop the lang context. (Only relevant for PCH.) */
d88386ce 3765 if (parser->implicit_extern_c)
074ab442 3766 {
3767 pop_lang_context ();
3768 parser->implicit_extern_c = false;
3769 }
3770
d88386ce 3771 /* Finish up. */
3772 finish_translation_unit ();
074ab442 3773
d88386ce 3774 success = true;
0a3b29ad 3775 }
d88386ce 3776 else
3777 {
3778 cp_parser_error (parser, "expected declaration");
3779 success = false;
3780 }
074ab442 3781
3046c0a3 3782 /* Make sure the declarator obstack was fully cleaned up. */
b4df430b 3783 gcc_assert (obstack_next_free (&declarator_obstack)
3784 == declarator_obstack_base);
0a3b29ad 3785
3786 /* All went well. */
3046c0a3 3787 return success;
0a3b29ad 3788}
3789
3790/* Expressions [gram.expr] */
3791
3792/* Parse a primary-expression.
3793
3794 primary-expression:
3795 literal
3796 this
3797 ( expression )
3798 id-expression
3799
3800 GNU Extensions:
3801
3802 primary-expression:
3803 ( compound-statement )
3804 __builtin_va_arg ( assignment-expression , type-id )
e3a7cc2b 3805 __builtin_offsetof ( type-id , offsetof-expression )
0a3b29ad 3806
481451eb 3807 C++ Extensions:
3808 __has_nothrow_assign ( type-id )
3809 __has_nothrow_constructor ( type-id )
3810 __has_nothrow_copy ( type-id )
3811 __has_trivial_assign ( type-id )
3812 __has_trivial_constructor ( type-id )
3813 __has_trivial_copy ( type-id )
3814 __has_trivial_destructor ( type-id )
3815 __has_virtual_destructor ( type-id )
3816 __is_abstract ( type-id )
3817 __is_base_of ( type-id , type-id )
3818 __is_class ( type-id )
3819 __is_convertible_to ( type-id , type-id )
3820 __is_empty ( type-id )
3821 __is_enum ( type-id )
aa4313eb 3822 __is_final ( type-id )
23407dc9 3823 __is_literal_type ( type-id )
481451eb 3824 __is_pod ( type-id )
3825 __is_polymorphic ( type-id )
23407dc9 3826 __is_std_layout ( type-id )
3827 __is_trivial ( type-id )
481451eb 3828 __is_union ( type-id )
3829
7a4e126b 3830 Objective-C++ Extension:
3831
3832 primary-expression:
3833 objc-expression
3834
0a3b29ad 3835 literal:
3836 __null
3837
fbb01da7 3838 ADDRESS_P is true iff this expression was immediately preceded by
3839 "&" and therefore might denote a pointer-to-member. CAST_P is true
3840 iff this expression is the target of a cast. TEMPLATE_ARG_P is
149fdb98 3841 true iff this expression is a template argument.
640aa28c 3842
fbb01da7 3843 Returns a representation of the expression. Upon return, *IDK
3844 indicates what kind of id-expression (if any) was present. */
0a3b29ad 3845
3846static tree
ccb84981 3847cp_parser_primary_expression (cp_parser *parser,
fbb01da7 3848 bool address_p,
640aa28c 3849 bool cast_p,
fbb01da7 3850 bool template_arg_p,
3851 cp_id_kind *idk)
0a3b29ad 3852{
ad9ae192 3853 cp_token *token = NULL;
0a3b29ad 3854
3855 /* Assume the primary expression is not an id-expression. */
0886adbc 3856 *idk = CP_ID_KIND_NONE;
0a3b29ad 3857
3858 /* Peek at the next token. */
3859 token = cp_lexer_peek_token (parser->lexer);
3860 switch (token->type)
3861 {
3862 /* literal:
3863 integer-literal
3864 character-literal
3865 floating-literal
3866 string-literal
244db24d 3867 boolean-literal
3868 pointer-literal
3869 user-defined-literal */
0a3b29ad 3870 case CPP_CHAR:
924bbf02 3871 case CPP_CHAR16:
3872 case CPP_CHAR32:
0a3b29ad 3873 case CPP_WCHAR:
0a3b29ad 3874 case CPP_NUMBER:
244db24d 3875 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3876 return cp_parser_userdef_numeric_literal (parser);
0a3b29ad 3877 token = cp_lexer_consume_token (parser->lexer);
14e882ea 3878 if (TREE_CODE (token->u.value) == FIXED_CST)
3879 {
ccb59bb6 3880 error_at (token->location,
3881 "fixed-point types not supported in C++");
14e882ea 3882 return error_mark_node;
3883 }
640aa28c 3884 /* Floating-point literals are only allowed in an integral
3885 constant expression if they are cast to an integral or
3886 enumeration type. */
3369eb76 3887 if (TREE_CODE (token->u.value) == REAL_CST
b68a9fcf 3888 && parser->integral_constant_expression_p
3889 && pedantic)
640aa28c 3890 {
3891 /* CAST_P will be set even in invalid code like "int(2.7 +
3892 ...)". Therefore, we have to check that the next token
3893 is sure to end the cast. */
3894 if (cast_p)
3895 {
3896 cp_token *next_token;
3897
3898 next_token = cp_lexer_peek_token (parser->lexer);
3899 if (/* The comma at the end of an
3900 enumerator-definition. */
3901 next_token->type != CPP_COMMA
3902 /* The curly brace at the end of an enum-specifier. */
3903 && next_token->type != CPP_CLOSE_BRACE
3904 /* The end of a statement. */
3905 && next_token->type != CPP_SEMICOLON
3906 /* The end of the cast-expression. */
3907 && next_token->type != CPP_CLOSE_PAREN
3908 /* The end of an array bound. */
c6e71399 3909 && next_token->type != CPP_CLOSE_SQUARE
3910 /* The closing ">" in a template-argument-list. */
3911 && (next_token->type != CPP_GREATER
56471494 3912 || parser->greater_than_is_operator_p)
3913 /* C++0x only: A ">>" treated like two ">" tokens,
3914 in a template-argument-list. */
3915 && (next_token->type != CPP_RSHIFT
6dcdb5de 3916 || (cxx_dialect == cxx98)
c6e71399 3917 || parser->greater_than_is_operator_p))
640aa28c 3918 cast_p = false;
3919 }
3920
3921 /* If we are within a cast, then the constraint that the
3922 cast is to an integral or enumeration type will be
3923 checked at that point. If we are not within a cast, then
3924 this code is invalid. */
3925 if (!cast_p)
c247dce0 3926 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
640aa28c 3927 }
3369eb76 3928 return token->u.value;
0a3b29ad 3929
244db24d 3930 case CPP_CHAR_USERDEF:
3931 case CPP_CHAR16_USERDEF:
3932 case CPP_CHAR32_USERDEF:
3933 case CPP_WCHAR_USERDEF:
3934 return cp_parser_userdef_char_literal (parser);
3935
3fe7c943 3936 case CPP_STRING:
924bbf02 3937 case CPP_STRING16:
3938 case CPP_STRING32:
3fe7c943 3939 case CPP_WSTRING:
538ba11a 3940 case CPP_UTF8STRING:
244db24d 3941 case CPP_STRING_USERDEF:
3942 case CPP_STRING16_USERDEF:
3943 case CPP_STRING32_USERDEF:
3944 case CPP_WSTRING_USERDEF:
3945 case CPP_UTF8STRING_USERDEF:
00d26680 3946 /* ??? Should wide strings be allowed when parser->translate_strings_p
653e5405 3947 is false (i.e. in attributes)? If not, we can kill the third
00d26680 3948 argument to cp_parser_string_literal. */
3949 return cp_parser_string_literal (parser,
3950 parser->translate_strings_p,
3951 true);
3fe7c943 3952
0a3b29ad 3953 case CPP_OPEN_PAREN:
3954 {
3955 tree expr;
3956 bool saved_greater_than_is_operator_p;
3957
3958 /* Consume the `('. */
3959 cp_lexer_consume_token (parser->lexer);
3960 /* Within a parenthesized expression, a `>' token is always
3961 the greater-than operator. */
ccb84981 3962 saved_greater_than_is_operator_p
0a3b29ad 3963 = parser->greater_than_is_operator_p;
3964 parser->greater_than_is_operator_p = true;
3965 /* If we see `( { ' then we are looking at the beginning of
3966 a GNU statement-expression. */
3967 if (cp_parser_allow_gnu_extensions_p (parser)
3968 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3969 {
3970 /* Statement-expressions are not allowed by the standard. */
29438999 3971 pedwarn (token->location, OPT_Wpedantic,
21ca8540 3972 "ISO C++ forbids braced-groups within expressions");
ccb84981 3973
0a3b29ad 3974 /* And they're not allowed outside of a function-body; you
3975 cannot, for example, write:
ccb84981 3976
653e5405 3977 int i = ({ int j = 3; j + 1; });
ccb84981 3978
0a3b29ad 3979 at class or namespace scope. */
9184d665 3980 if (!parser->in_function_body
3981 || parser->in_template_argument_list_p)
4be86438 3982 {
ccb59bb6 3983 error_at (token->location,
3984 "statement-expressions are not allowed outside "
3985 "functions nor in template-argument lists");
4be86438 3986 cp_parser_skip_to_end_of_block_or_statement (parser);
3987 expr = error_mark_node;
3988 }
3989 else
3990 {
3991 /* Start the statement-expression. */
3992 expr = begin_stmt_expr ();
3993 /* Parse the compound-statement. */
6bf5e78d 3994 cp_parser_compound_statement (parser, expr, false, false);
4be86438 3995 /* Finish up. */
3996 expr = finish_stmt_expr (expr, false);
3997 }
0a3b29ad 3998 }
3999 else
4000 {
4001 /* Parse the parenthesized expression. */
98b326fd 4002 expr = cp_parser_expression (parser, cast_p, idk);
0a3b29ad 4003 /* Let the front end know that this expression was
4004 enclosed in parentheses. This matters in case, for
4005 example, the expression is of the form `A::B', since
4006 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4007 not. */
4008 finish_parenthesized_expr (expr);
eb954b5f 4009 /* DR 705: Wrapping an unqualified name in parentheses
4010 suppresses arg-dependent lookup. We want to pass back
4011 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4012 (c++/37862), but none of the others. */
4013 if (*idk != CP_ID_KIND_QUALIFIED)
4014 *idk = CP_ID_KIND_NONE;
0a3b29ad 4015 }
4016 /* The `>' token might be the end of a template-id or
4017 template-parameter-list now. */
ccb84981 4018 parser->greater_than_is_operator_p
0a3b29ad 4019 = saved_greater_than_is_operator_p;
4020 /* Consume the `)'. */
c247dce0 4021 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
0a3b29ad 4022 cp_parser_skip_to_end_of_statement (parser);
4023
4024 return expr;
4025 }
4026
a8b75081 4027 case CPP_OPEN_SQUARE:
79408174 4028 if (c_dialect_objc ())
4029 /* We have an Objective-C++ message. */
4030 return cp_parser_objc_expression (parser);
50bd405f 4031 {
4032 tree lam = cp_parser_lambda_expression (parser);
4033 /* Don't warn about a failed tentative parse. */
4034 if (cp_parser_error_occurred (parser))
4035 return error_mark_node;
4036 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4037 return lam;
4038 }
a8b75081 4039
79408174 4040 case CPP_OBJC_STRING:
4041 if (c_dialect_objc ())
4042 /* We have an Objective-C++ string literal. */
4043 return cp_parser_objc_expression (parser);
4044 cp_parser_error (parser, "expected primary-expression");
4045 return error_mark_node;
a8b75081 4046
0a3b29ad 4047 case CPP_KEYWORD:
4048 switch (token->keyword)
4049 {
4050 /* These two are the boolean literals. */
4051 case RID_TRUE:
4052 cp_lexer_consume_token (parser->lexer);
4053 return boolean_true_node;
4054 case RID_FALSE:
4055 cp_lexer_consume_token (parser->lexer);
4056 return boolean_false_node;
ccb84981 4057
0a3b29ad 4058 /* The `__null' literal. */
4059 case RID_NULL:
4060 cp_lexer_consume_token (parser->lexer);
4061 return null_node;
4062
6fe11077 4063 /* The `nullptr' literal. */
4064 case RID_NULLPTR:
4065 cp_lexer_consume_token (parser->lexer);
4066 return nullptr_node;
4067
0a3b29ad 4068 /* Recognize the `this' keyword. */
4069 case RID_THIS:
4070 cp_lexer_consume_token (parser->lexer);
4071 if (parser->local_variables_forbidden_p)
4072 {
ccb59bb6 4073 error_at (token->location,
4074 "%<this%> may not be used in this context");
0a3b29ad 4075 return error_mark_node;
4076 }
5f6526e1 4077 /* Pointers cannot appear in constant-expressions. */
c247dce0 4078 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3938e0c2 4079 return error_mark_node;
0a3b29ad 4080 return finish_this_expr ();
4081
4082 /* The `operator' keyword can be the beginning of an
4083 id-expression. */
4084 case RID_OPERATOR:
4085 goto id_expression;
4086
4087 case RID_FUNCTION_NAME:
4088 case RID_PRETTY_FUNCTION_NAME:
4089 case RID_C99_FUNCTION_NAME:
a457170d 4090 {
c247dce0 4091 non_integral_constant name;
a457170d 4092
4093 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4094 __func__ are the names of variables -- but they are
4095 treated specially. Therefore, they are handled here,
4096 rather than relying on the generic id-expression logic
4097 below. Grammatically, these names are id-expressions.
4098
4099 Consume the token. */
4100 token = cp_lexer_consume_token (parser->lexer);
4101
4102 switch (token->keyword)
4103 {
4104 case RID_FUNCTION_NAME:
c247dce0 4105 name = NIC_FUNC_NAME;
a457170d 4106 break;
4107 case RID_PRETTY_FUNCTION_NAME:
c247dce0 4108 name = NIC_PRETTY_FUNC;
a457170d 4109 break;
4110 case RID_C99_FUNCTION_NAME:
c247dce0 4111 name = NIC_C99_FUNC;
a457170d 4112 break;
4113 default:
4114 gcc_unreachable ();
4115 }
4116
4117 if (cp_parser_non_integral_constant_expression (parser, name))
4118 return error_mark_node;
4119
4120 /* Look up the name. */
4121 return finish_fname (token->u.value);
4122 }
0a3b29ad 4123
4124 case RID_VA_ARG:
4125 {
4126 tree expression;
4127 tree type;
346b240d 4128 source_location type_location;
0a3b29ad 4129
4130 /* The `__builtin_va_arg' construct is used to handle
4131 `va_arg'. Consume the `__builtin_va_arg' token. */
4132 cp_lexer_consume_token (parser->lexer);
4133 /* Look for the opening `('. */
c247dce0 4134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 4135 /* Now, parse the assignment-expression. */
640aa28c 4136 expression = cp_parser_assignment_expression (parser,
98b326fd 4137 /*cast_p=*/false, NULL);
0a3b29ad 4138 /* Look for the `,'. */
c247dce0 4139 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
346b240d 4140 type_location = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 4141 /* Parse the type-id. */
4142 type = cp_parser_type_id (parser);
4143 /* Look for the closing `)'. */
c247dce0 4144 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5f6526e1 4145 /* Using `va_arg' in a constant-expression is not
4146 allowed. */
c61e1212 4147 if (cp_parser_non_integral_constant_expression (parser,
4148 NIC_VA_ARG))
3938e0c2 4149 return error_mark_node;
346b240d 4150 return build_x_va_arg (type_location, expression, type);
0a3b29ad 4151 }
4152
30b87eb6 4153 case RID_OFFSETOF:
43bf5d72 4154 return cp_parser_builtin_offsetof (parser);
30b87eb6 4155
481451eb 4156 case RID_HAS_NOTHROW_ASSIGN:
4157 case RID_HAS_NOTHROW_CONSTRUCTOR:
4158 case RID_HAS_NOTHROW_COPY:
4159 case RID_HAS_TRIVIAL_ASSIGN:
4160 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4161 case RID_HAS_TRIVIAL_COPY:
4162 case RID_HAS_TRIVIAL_DESTRUCTOR:
4163 case RID_HAS_VIRTUAL_DESTRUCTOR:
4164 case RID_IS_ABSTRACT:
4165 case RID_IS_BASE_OF:
4166 case RID_IS_CLASS:
4167 case RID_IS_CONVERTIBLE_TO:
4168 case RID_IS_EMPTY:
4169 case RID_IS_ENUM:
aa4313eb 4170 case RID_IS_FINAL:
23407dc9 4171 case RID_IS_LITERAL_TYPE:
481451eb 4172 case RID_IS_POD:
4173 case RID_IS_POLYMORPHIC:
c1c67b4f 4174 case RID_IS_STD_LAYOUT:
4175 case RID_IS_TRIVIAL:
481451eb 4176 case RID_IS_UNION:
4177 return cp_parser_trait_expr (parser, token->keyword);
4178
79408174 4179 /* Objective-C++ expressions. */
4180 case RID_AT_ENCODE:
4181 case RID_AT_PROTOCOL:
4182 case RID_AT_SELECTOR:
4183 return cp_parser_objc_expression (parser);
7a4e126b 4184
45a7aa40 4185 case RID_TEMPLATE:
4186 if (parser->in_function_body
4187 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4188 == CPP_LESS))
4189 {
4190 error_at (token->location,
4191 "a template declaration cannot appear at block scope");
4192 cp_parser_skip_to_end_of_block_or_statement (parser);
4193 return error_mark_node;
4194 }
0a3b29ad 4195 default:
4196 cp_parser_error (parser, "expected primary-expression");
4197 return error_mark_node;
4198 }
0a3b29ad 4199
4200 /* An id-expression can start with either an identifier, a
4201 `::' as the beginning of a qualified-id, or the "operator"
4202 keyword. */
4203 case CPP_NAME:
4204 case CPP_SCOPE:
4205 case CPP_TEMPLATE_ID:
4206 case CPP_NESTED_NAME_SPECIFIER:
4207 {
4208 tree id_expression;
4209 tree decl;
0886adbc 4210 const char *error_msg;
fbb01da7 4211 bool template_p;
4212 bool done;
ad9ae192 4213 cp_token *id_expr_token;
0a3b29ad 4214
4215 id_expression:
4216 /* Parse the id-expression. */
ccb84981 4217 id_expression
4218 = cp_parser_id_expression (parser,
0a3b29ad 4219 /*template_keyword_p=*/false,
4220 /*check_dependency_p=*/true,
fbb01da7 4221 &template_p,
197c9df7 4222 /*declarator_p=*/false,
130bb1d4 4223 /*optional_p=*/false);
0a3b29ad 4224 if (id_expression == error_mark_node)
4225 return error_mark_node;
ad9ae192 4226 id_expr_token = token;
fbb01da7 4227 token = cp_lexer_peek_token (parser->lexer);
4228 done = (token->type != CPP_OPEN_SQUARE
4229 && token->type != CPP_OPEN_PAREN
4230 && token->type != CPP_DOT
4231 && token->type != CPP_DEREF
4232 && token->type != CPP_PLUS_PLUS
4233 && token->type != CPP_MINUS_MINUS);
0a3b29ad 4234 /* If we have a template-id, then no further lookup is
4235 required. If the template-id was for a template-class, we
4236 will sometimes have a TYPE_DECL at this point. */
fbb01da7 4237 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4238 || TREE_CODE (id_expression) == TYPE_DECL)
0a3b29ad 4239 decl = id_expression;
4240 /* Look up the name. */
ccb84981 4241 else
0a3b29ad 4242 {
b62240d5 4243 tree ambiguous_decls;
2cdbcd51 4244
627895ac 4245 /* If we already know that this lookup is ambiguous, then
4246 we've already issued an error message; there's no reason
4247 to check again. */
4248 if (id_expr_token->type == CPP_NAME
4249 && id_expr_token->ambiguous_p)
4250 {
4251 cp_parser_simulate_error (parser);
4252 return error_mark_node;
4253 }
4254
2cdbcd51 4255 decl = cp_parser_lookup_name (parser, id_expression,
e2ae55f2 4256 none_type,
fbb01da7 4257 template_p,
2cdbcd51 4258 /*is_namespace=*/false,
4259 /*check_dependency=*/true,
ad9ae192 4260 &ambiguous_decls,
4261 id_expr_token->location);
2cdbcd51 4262 /* If the lookup was ambiguous, an error will already have
4263 been issued. */
b62240d5 4264 if (ambiguous_decls)
2cdbcd51 4265 return error_mark_node;
7a4e126b 4266
79408174 4267 /* In Objective-C++, we may have an Objective-C 2.0
4268 dot-syntax for classes here. */
4269 if (c_dialect_objc ()
4270 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4271 && TREE_CODE (decl) == TYPE_DECL
4272 && objc_is_class_name (decl))
4273 {
4274 tree component;
4275 cp_lexer_consume_token (parser->lexer);
4276 component = cp_parser_identifier (parser);
4277 if (component == error_mark_node)
4278 return error_mark_node;
4279
4280 return objc_build_class_component_ref (id_expression, component);
4281 }
4282
4283 /* In Objective-C++, an instance variable (ivar) may be preferred
4284 to whatever cp_parser_lookup_name() found. */
4285 decl = objc_lookup_ivar (decl, id_expression);
7a4e126b 4286
0a3b29ad 4287 /* If name lookup gives us a SCOPE_REF, then the
fbb01da7 4288 qualifying scope was dependent. */
0a3b29ad 4289 if (TREE_CODE (decl) == SCOPE_REF)
62116ec3 4290 {
4291 /* At this point, we do not know if DECL is a valid
4292 integral constant expression. We assume that it is
4293 in fact such an expression, so that code like:
4294
4295 template <int N> struct A {
4296 int a[B<N>::i];
4297 };
4298
4299 is accepted. At template-instantiation time, we
4300 will check that B<N>::i is actually a constant. */
4301 return decl;
4302 }
0a3b29ad 4303 /* Check to see if DECL is a local variable in a context
4304 where that is forbidden. */
4305 if (parser->local_variables_forbidden_p
4306 && local_variable_p (decl))
4307 {
4308 /* It might be that we only found DECL because we are
4309 trying to be generous with pre-ISO scoping rules.
4310 For example, consider:
4311
4312 int i;
4313 void g() {
4314 for (int i = 0; i < 10; ++i) {}
4315 extern void f(int j = i);
4316 }
4317
ccb84981 4318 Here, name look up will originally find the out
0a3b29ad 4319 of scope `i'. We need to issue a warning message,
4320 but then use the global `i'. */
4321 decl = check_for_out_of_scope_variable (decl);
4322 if (local_variable_p (decl))
4323 {
ccb59bb6 4324 error_at (id_expr_token->location,
4325 "local variable %qD may not appear in this context",
4326 decl);
0a3b29ad 4327 return error_mark_node;
4328 }
4329 }
f3b70d2f 4330 }
ccb84981 4331
074ab442 4332 decl = (finish_id_expression
fbb01da7 4333 (id_expression, decl, parser->scope,
4334 idk,
4335 parser->integral_constant_expression_p,
4336 parser->allow_non_integral_constant_expression_p,
4337 &parser->non_integral_constant_expression_p,
4338 template_p, done, address_p,
4339 template_arg_p,
ad9ae192 4340 &error_msg,
4341 id_expr_token->location));
0886adbc 4342 if (error_msg)
4343 cp_parser_error (parser, error_msg);
0a3b29ad 4344 return decl;
4345 }
4346
4347 /* Anything else is an error. */
4348 default:
4349 cp_parser_error (parser, "expected primary-expression");
4350 return error_mark_node;
4351 }
4352}
4353
4354/* Parse an id-expression.
4355
4356 id-expression:
4357 unqualified-id
4358 qualified-id
4359
4360 qualified-id:
4361 :: [opt] nested-name-specifier template [opt] unqualified-id
4362 :: identifier
4363 :: operator-function-id
4364 :: template-id
4365
4366 Return a representation of the unqualified portion of the
4367 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4368 a `::' or nested-name-specifier.
4369
4370 Often, if the id-expression was a qualified-id, the caller will
4371 want to make a SCOPE_REF to represent the qualified-id. This
4372 function does not do this in order to avoid wastefully creating
4373 SCOPE_REFs when they are not required.
4374
0a3b29ad 4375 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4376 `template' keyword.
4377
4378 If CHECK_DEPENDENCY_P is false, then names are looked up inside
ccb84981 4379 uninstantiated templates.
0a3b29ad 4380
a210e1ca 4381 If *TEMPLATE_P is non-NULL, it is set to true iff the
0a3b29ad 4382 `template' keyword is used to explicitly indicate that the entity
ccb84981 4383 named is a template.
899cc6e8 4384
4385 If DECLARATOR_P is true, the id-expression is appearing as part of
63eff20d 4386 a declarator, rather than as part of an expression. */
0a3b29ad 4387
4388static tree
4389cp_parser_id_expression (cp_parser *parser,
4390 bool template_keyword_p,
4391 bool check_dependency_p,
899cc6e8 4392 bool *template_p,
197c9df7 4393 bool declarator_p,
130bb1d4 4394 bool optional_p)
0a3b29ad 4395{
4396 bool global_scope_p;
4397 bool nested_name_specifier_p;
4398
4399 /* Assume the `template' keyword was not used. */
4400 if (template_p)
fbb01da7 4401 *template_p = template_keyword_p;
0a3b29ad 4402
4403 /* Look for the optional `::' operator. */
ccb84981 4404 global_scope_p
130bb1d4 4405 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
0a3b29ad 4406 != NULL_TREE);
4407 /* Look for the optional nested-name-specifier. */
ccb84981 4408 nested_name_specifier_p
0a3b29ad 4409 = (cp_parser_nested_name_specifier_opt (parser,
4410 /*typename_keyword_p=*/false,
4411 check_dependency_p,
3d0f901b 4412 /*type_p=*/false,
0078a5e8 4413 declarator_p)
0a3b29ad 4414 != NULL_TREE);
4415 /* If there is a nested-name-specifier, then we are looking at
4416 the first qualified-id production. */
4417 if (nested_name_specifier_p)
4418 {
4419 tree saved_scope;
4420 tree saved_object_scope;
4421 tree saved_qualifying_scope;
4422 tree unqualified_id;
4423 bool is_template;
4424
4425 /* See if the next token is the `template' keyword. */
4426 if (!template_p)
4427 template_p = &is_template;
4428 *template_p = cp_parser_optional_template_keyword (parser);
4429 /* Name lookup we do during the processing of the
4430 unqualified-id might obliterate SCOPE. */
4431 saved_scope = parser->scope;
4432 saved_object_scope = parser->object_scope;
4433 saved_qualifying_scope = parser->qualifying_scope;
4434 /* Process the final unqualified-id. */
4435 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
899cc6e8 4436 check_dependency_p,
197c9df7 4437 declarator_p,
130bb1d4 4438 /*optional_p=*/false);
0a3b29ad 4439 /* Restore the SAVED_SCOPE for our caller. */
4440 parser->scope = saved_scope;
4441 parser->object_scope = saved_object_scope;
4442 parser->qualifying_scope = saved_qualifying_scope;
4443
4444 return unqualified_id;
4445 }
4446 /* Otherwise, if we are in global scope, then we are looking at one
4447 of the other qualified-id productions. */
4448 else if (global_scope_p)
4449 {
4450 cp_token *token;
4451 tree id;
4452
2c593bd0 4453 /* Peek at the next token. */
4454 token = cp_lexer_peek_token (parser->lexer);
4455
4456 /* If it's an identifier, and the next token is not a "<", then
4457 we can avoid the template-id case. This is an optimization
4458 for this common case. */
ccb84981 4459 if (token->type == CPP_NAME
4460 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 4461 (parser, 2))
2c593bd0 4462 return cp_parser_identifier (parser);
4463
0a3b29ad 4464 cp_parser_parse_tentatively (parser);
4465 /* Try a template-id. */
ccb84981 4466 id = cp_parser_template_id (parser,
0a3b29ad 4467 /*template_keyword_p=*/false,
3d0f901b 4468 /*check_dependency_p=*/true,
4469 declarator_p);
0a3b29ad 4470 /* If that worked, we're done. */
4471 if (cp_parser_parse_definitely (parser))
4472 return id;
4473
2c593bd0 4474 /* Peek at the next token. (Changes in the token buffer may
4475 have invalidated the pointer obtained above.) */
0a3b29ad 4476 token = cp_lexer_peek_token (parser->lexer);
4477
4478 switch (token->type)
4479 {
4480 case CPP_NAME:
4481 return cp_parser_identifier (parser);
4482
4483 case CPP_KEYWORD:
4484 if (token->keyword == RID_OPERATOR)
4485 return cp_parser_operator_function_id (parser);
4486 /* Fall through. */
ccb84981 4487
0a3b29ad 4488 default:
4489 cp_parser_error (parser, "expected id-expression");
4490 return error_mark_node;
4491 }
4492 }
4493 else
4494 return cp_parser_unqualified_id (parser, template_keyword_p,
899cc6e8 4495 /*check_dependency_p=*/true,
130bb1d4 4496 declarator_p,
4497 optional_p);
0a3b29ad 4498}
4499
4500/* Parse an unqualified-id.
4501
4502 unqualified-id:
4503 identifier
4504 operator-function-id
4505 conversion-function-id
4506 ~ class-name
4507 template-id
4508
4509 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4510 keyword, in a construct like `A::template ...'.
4511
4512 Returns a representation of unqualified-id. For the `identifier'
4513 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4514 production a BIT_NOT_EXPR is returned; the operand of the
4515 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4516 other productions, see the documentation accompanying the
4517 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
899cc6e8 4518 names are looked up in uninstantiated templates. If DECLARATOR_P
4519 is true, the unqualified-id is appearing as part of a declarator,
4520 rather than as part of an expression. */
0a3b29ad 4521
4522static tree
ccb84981 4523cp_parser_unqualified_id (cp_parser* parser,
653e5405 4524 bool template_keyword_p,
899cc6e8 4525 bool check_dependency_p,
074ab442 4526 bool declarator_p,
130bb1d4 4527 bool optional_p)
0a3b29ad 4528{
4529 cp_token *token;
4530
4531 /* Peek at the next token. */
4532 token = cp_lexer_peek_token (parser->lexer);
ccb84981 4533
0a3b29ad 4534 switch (token->type)
4535 {
4536 case CPP_NAME:
4537 {
4538 tree id;
4539
4540 /* We don't know yet whether or not this will be a
4541 template-id. */
4542 cp_parser_parse_tentatively (parser);
4543 /* Try a template-id. */
4544 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4545 check_dependency_p,
4546 declarator_p);
0a3b29ad 4547 /* If it worked, we're done. */
4548 if (cp_parser_parse_definitely (parser))
4549 return id;
4550 /* Otherwise, it's an ordinary identifier. */
4551 return cp_parser_identifier (parser);
4552 }
4553
4554 case CPP_TEMPLATE_ID:
4555 return cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4556 check_dependency_p,
4557 declarator_p);
0a3b29ad 4558
4559 case CPP_COMPL:
4560 {
4561 tree type_decl;
4562 tree qualifying_scope;
4563 tree object_scope;
4564 tree scope;
f0d4a607 4565 bool done;
0a3b29ad 4566
4567 /* Consume the `~' token. */
4568 cp_lexer_consume_token (parser->lexer);
4569 /* Parse the class-name. The standard, as written, seems to
4570 say that:
4571
4572 template <typename T> struct S { ~S (); };
4573 template <typename T> S<T>::~S() {}
4574
653e5405 4575 is invalid, since `~' must be followed by a class-name, but
0a3b29ad 4576 `S<T>' is dependent, and so not known to be a class.
4577 That's not right; we need to look in uninstantiated
4578 templates. A further complication arises from:
4579
4580 template <typename T> void f(T t) {
4581 t.T::~T();
ccb84981 4582 }
0a3b29ad 4583
4584 Here, it is not possible to look up `T' in the scope of `T'
4585 itself. We must look in both the current scope, and the
ccb84981 4586 scope of the containing complete expression.
0a3b29ad 4587
4588 Yet another issue is:
4589
653e5405 4590 struct S {
4591 int S;
4592 ~S();
4593 };
0a3b29ad 4594
653e5405 4595 S::~S() {}
0a3b29ad 4596
653e5405 4597 The standard does not seem to say that the `S' in `~S'
0a3b29ad 4598 should refer to the type `S' and not the data member
4599 `S::S'. */
4600
4601 /* DR 244 says that we look up the name after the "~" in the
4602 same scope as we looked up the qualifying name. That idea
4603 isn't fully worked out; it's more complicated than that. */
4604 scope = parser->scope;
4605 object_scope = parser->object_scope;
4606 qualifying_scope = parser->qualifying_scope;
4607
4b2ee8a4 4608 /* Check for invalid scopes. */
4609 if (scope == error_mark_node)
4610 {
dea225ee 4611 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4612 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 4613 return error_mark_node;
4614 }
4615 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4616 {
4617 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4618 error_at (token->location,
4619 "scope %qT before %<~%> is not a class-name",
4620 scope);
dea225ee 4621 cp_parser_simulate_error (parser);
4622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4623 cp_lexer_consume_token (parser->lexer);
4b2ee8a4 4624 return error_mark_node;
4625 }
4626 gcc_assert (!scope || TYPE_P (scope));
4627
cb84e326 4628 /* If the name is of the form "X::~X" it's OK even if X is a
4629 typedef. */
ba0c587d 4630 token = cp_lexer_peek_token (parser->lexer);
4b2ee8a4 4631 if (scope
ba0c587d 4632 && token->type == CPP_NAME
ccb84981 4633 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
a70e3c37 4634 != CPP_LESS)
cb84e326 4635 && (token->u.value == TYPE_IDENTIFIER (scope)
55b1db46 4636 || (CLASS_TYPE_P (scope)
4637 && constructor_name_p (token->u.value, scope))))
0a3b29ad 4638 {
4639 cp_lexer_consume_token (parser->lexer);
4640 return build_nt (BIT_NOT_EXPR, scope);
4641 }
4642
4643 /* If there was an explicit qualification (S::~T), first look
a70e3c37 4644 in the scope given by the qualification (i.e., S).
4645
5570fae0 4646 Note: in the calls to cp_parser_class_name below we pass
4647 typename_type so that lookup finds the injected-class-name
4648 rather than the constructor. */
f0d4a607 4649 done = false;
7978bd9e 4650 type_decl = NULL_TREE;
0a3b29ad 4651 if (scope)
4652 {
4653 cp_parser_parse_tentatively (parser);
ccb84981 4654 type_decl = cp_parser_class_name (parser,
0a3b29ad 4655 /*typename_keyword_p=*/false,
4656 /*template_keyword_p=*/false,
5570fae0 4657 typename_type,
0a3b29ad 4658 /*check_dependency=*/false,
3d0f901b 4659 /*class_head_p=*/false,
4660 declarator_p);
0a3b29ad 4661 if (cp_parser_parse_definitely (parser))
f0d4a607 4662 done = true;
0a3b29ad 4663 }
4664 /* In "N::S::~S", look in "N" as well. */
f0d4a607 4665 if (!done && scope && qualifying_scope)
0a3b29ad 4666 {
4667 cp_parser_parse_tentatively (parser);
4668 parser->scope = qualifying_scope;
4669 parser->object_scope = NULL_TREE;
4670 parser->qualifying_scope = NULL_TREE;
ccb84981 4671 type_decl
4672 = cp_parser_class_name (parser,
0a3b29ad 4673 /*typename_keyword_p=*/false,
4674 /*template_keyword_p=*/false,
5570fae0 4675 typename_type,
0a3b29ad 4676 /*check_dependency=*/false,
3d0f901b 4677 /*class_head_p=*/false,
4678 declarator_p);
0a3b29ad 4679 if (cp_parser_parse_definitely (parser))
f0d4a607 4680 done = true;
0a3b29ad 4681 }
130bb1d4 4682 /* In "p->S::~T", look in the scope given by "*p" as well. */
4683 else if (!done && object_scope)
0a3b29ad 4684 {
4685 cp_parser_parse_tentatively (parser);
4686 parser->scope = object_scope;
4687 parser->object_scope = NULL_TREE;
4688 parser->qualifying_scope = NULL_TREE;
ccb84981 4689 type_decl
4690 = cp_parser_class_name (parser,
130bb1d4 4691 /*typename_keyword_p=*/false,
0a3b29ad 4692 /*template_keyword_p=*/false,
5570fae0 4693 typename_type,
0a3b29ad 4694 /*check_dependency=*/false,
3d0f901b 4695 /*class_head_p=*/false,
4696 declarator_p);
0a3b29ad 4697 if (cp_parser_parse_definitely (parser))
f0d4a607 4698 done = true;
0a3b29ad 4699 }
4700 /* Look in the surrounding context. */
f0d4a607 4701 if (!done)
4702 {
4703 parser->scope = NULL_TREE;
4704 parser->object_scope = NULL_TREE;
4705 parser->qualifying_scope = NULL_TREE;
c6cc092b 4706 if (processing_template_decl)
4707 cp_parser_parse_tentatively (parser);
f0d4a607 4708 type_decl
4709 = cp_parser_class_name (parser,
4710 /*typename_keyword_p=*/false,
4711 /*template_keyword_p=*/false,
5570fae0 4712 typename_type,
f0d4a607 4713 /*check_dependency=*/false,
4714 /*class_head_p=*/false,
4715 declarator_p);
c6cc092b 4716 if (processing_template_decl
4717 && ! cp_parser_parse_definitely (parser))
4718 {
4719 /* We couldn't find a type with this name, so just accept
4720 it and check for a match at instantiation time. */
4721 type_decl = cp_parser_identifier (parser);
92644697 4722 if (type_decl != error_mark_node)
4723 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4724 return type_decl;
c6cc092b 4725 }
f0d4a607 4726 }
0a3b29ad 4727 /* If an error occurred, assume that the name of the
4728 destructor is the same as the name of the qualifying
4729 class. That allows us to keep parsing after running
4730 into ill-formed destructor names. */
4b2ee8a4 4731 if (type_decl == error_mark_node && scope)
0a3b29ad 4732 return build_nt (BIT_NOT_EXPR, scope);
4733 else if (type_decl == error_mark_node)
4734 return error_mark_node;
4735
a32f68f5 4736 /* Check that destructor name and scope match. */
4737 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4738 {
4739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4740 error_at (token->location,
4741 "declaration of %<~%T%> as member of %qT",
4742 type_decl, scope);
dea225ee 4743 cp_parser_simulate_error (parser);
a32f68f5 4744 return error_mark_node;
4745 }
4746
899cc6e8 4747 /* [class.dtor]
4748
4749 A typedef-name that names a class shall not be used as the
4750 identifier in the declarator for a destructor declaration. */
ccb84981 4751 if (declarator_p
899cc6e8 4752 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
510a599a 4753 && !DECL_SELF_REFERENCE_P (type_decl)
4754 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 4755 error_at (token->location,
4756 "typedef-name %qD used as destructor declarator",
4757 type_decl);
899cc6e8 4758
0a3b29ad 4759 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4760 }
4761
4762 case CPP_KEYWORD:
4763 if (token->keyword == RID_OPERATOR)
4764 {
4765 tree id;
4766
4767 /* This could be a template-id, so we try that first. */
4768 cp_parser_parse_tentatively (parser);
4769 /* Try a template-id. */
4770 id = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 4771 /*check_dependency_p=*/true,
4772 declarator_p);
0a3b29ad 4773 /* If that worked, we're done. */
4774 if (cp_parser_parse_definitely (parser))
4775 return id;
4776 /* We still don't know whether we're looking at an
4777 operator-function-id or a conversion-function-id. */
4778 cp_parser_parse_tentatively (parser);
4779 /* Try an operator-function-id. */
4780 id = cp_parser_operator_function_id (parser);
4781 /* If that didn't work, try a conversion-function-id. */
4782 if (!cp_parser_parse_definitely (parser))
4783 id = cp_parser_conversion_function_id (parser);
244db24d 4784 else if (UDLIT_OPER_P (id))
4785 {
4786 /* 17.6.3.3.5 */
4787 const char *name = UDLIT_OP_SUFFIX (id);
4788 if (name[0] != '_' && !in_system_header)
4789 warning (0, "literal operator suffixes not preceded by %<_%>"
4790 " are reserved for future standardization");
4791 }
0a3b29ad 4792
4793 return id;
4794 }
4795 /* Fall through. */
4796
4797 default:
197c9df7 4798 if (optional_p)
4799 return NULL_TREE;
0a3b29ad 4800 cp_parser_error (parser, "expected unqualified-id");
4801 return error_mark_node;
4802 }
4803}
4804
4805/* Parse an (optional) nested-name-specifier.
4806
3f00a6c0 4807 nested-name-specifier: [C++98]
0a3b29ad 4808 class-or-namespace-name :: nested-name-specifier [opt]
4809 class-or-namespace-name :: template nested-name-specifier [opt]
4810
3f00a6c0 4811 nested-name-specifier: [C++0x]
4812 type-name ::
4813 namespace-name ::
4814 nested-name-specifier identifier ::
4815 nested-name-specifier template [opt] simple-template-id ::
4816
0a3b29ad 4817 PARSER->SCOPE should be set appropriately before this function is
4818 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4819 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4820 in name lookups.
4821
4822 Sets PARSER->SCOPE to the class (TYPE) or namespace
4823 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4824 it unchanged if there is no nested-name-specifier. Returns the new
ccb84981 4825 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3d0f901b 4826
4827 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4828 part of a declaration and/or decl-specifier. */
0a3b29ad 4829
4830static tree
ccb84981 4831cp_parser_nested_name_specifier_opt (cp_parser *parser,
4832 bool typename_keyword_p,
0a3b29ad 4833 bool check_dependency_p,
3d0f901b 4834 bool type_p,
4835 bool is_declaration)
0a3b29ad 4836{
4837 bool success = false;
19273cc2 4838 cp_token_position start = 0;
4839 cp_token *token;
0a3b29ad 4840
0a3b29ad 4841 /* Remember where the nested-name-specifier starts. */
efcbcf83 4842 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
d15855b2 4843 {
4844 start = cp_lexer_token_position (parser->lexer, false);
4845 push_deferring_access_checks (dk_deferred);
4846 }
9b57b06b 4847
0a3b29ad 4848 while (true)
4849 {
4850 tree new_scope;
4851 tree old_scope;
4852 tree saved_qualifying_scope;
0a3b29ad 4853 bool template_keyword_p;
4854
b3c48b5d 4855 /* Spot cases that cannot be the beginning of a
4856 nested-name-specifier. */
4857 token = cp_lexer_peek_token (parser->lexer);
4858
4859 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4860 the already parsed nested-name-specifier. */
4861 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4862 {
4863 /* Grab the nested-name-specifier and continue the loop. */
4864 cp_parser_pre_parsed_nested_name_specifier (parser);
2f7a9ec1 4865 /* If we originally encountered this nested-name-specifier
4866 with IS_DECLARATION set to false, we will not have
4867 resolved TYPENAME_TYPEs, so we must do so here. */
b8b2a426 4868 if (is_declaration
4869 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
2f7a9ec1 4870 {
4871 new_scope = resolve_typename_type (parser->scope,
4872 /*only_current_p=*/false);
8826a863 4873 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
2f7a9ec1 4874 parser->scope = new_scope;
4875 }
b3c48b5d 4876 success = true;
4877 continue;
4878 }
4879
0a3b29ad 4880 /* Spot cases that cannot be the beginning of a
4881 nested-name-specifier. On the second and subsequent times
4882 through the loop, we look for the `template' keyword. */
2370b5bf 4883 if (success && token->keyword == RID_TEMPLATE)
0a3b29ad 4884 ;
4885 /* A template-id can start a nested-name-specifier. */
2370b5bf 4886 else if (token->type == CPP_TEMPLATE_ID)
0a3b29ad 4887 ;
07b8f133 4888 /* DR 743: decltype can be used in a nested-name-specifier. */
4889 else if (token_is_decltype (token))
4890 ;
0a3b29ad 4891 else
4892 {
4893 /* If the next token is not an identifier, then it is
3f00a6c0 4894 definitely not a type-name or namespace-name. */
2370b5bf 4895 if (token->type != CPP_NAME)
0a3b29ad 4896 break;
4897 /* If the following token is neither a `<' (to begin a
4898 template-id), nor a `::', then we are not looking at a
4899 nested-name-specifier. */
4900 token = cp_lexer_peek_nth_token (parser->lexer, 2);
1e838f42 4901
4902 if (token->type == CPP_COLON
4903 && parser->colon_corrects_to_scope_p
4904 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4905 {
4906 error_at (token->location,
4907 "found %<:%> in nested-name-specifier, expected %<::%>");
4908 token->type = CPP_SCOPE;
4909 }
4910
c8d5ab79 4911 if (token->type != CPP_SCOPE
4912 && !cp_parser_nth_token_starts_template_argument_list_p
4913 (parser, 2))
0a3b29ad 4914 break;
4915 }
4916
4917 /* The nested-name-specifier is optional, so we parse
4918 tentatively. */
4919 cp_parser_parse_tentatively (parser);
4920
4921 /* Look for the optional `template' keyword, if this isn't the
4922 first time through the loop. */
4923 if (success)
4924 template_keyword_p = cp_parser_optional_template_keyword (parser);
4925 else
4926 template_keyword_p = false;
4927
4928 /* Save the old scope since the name lookup we are about to do
4929 might destroy it. */
4930 old_scope = parser->scope;
4931 saved_qualifying_scope = parser->qualifying_scope;
0078a5e8 4932 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4933 look up names in "X<T>::I" in order to determine that "Y" is
4934 a template. So, if we have a typename at this point, we make
4935 an effort to look through it. */
9031d10b 4936 if (is_declaration
dee31741 4937 && !typename_keyword_p
9031d10b 4938 && parser->scope
0078a5e8 4939 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
9031d10b 4940 parser->scope = resolve_typename_type (parser->scope,
0078a5e8 4941 /*only_current_p=*/false);
0a3b29ad 4942 /* Parse the qualifying entity. */
ccb84981 4943 new_scope
3f00a6c0 4944 = cp_parser_qualifying_entity (parser,
4945 typename_keyword_p,
4946 template_keyword_p,
4947 check_dependency_p,
4948 type_p,
4949 is_declaration);
0a3b29ad 4950 /* Look for the `::' token. */
c247dce0 4951 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 4952
4953 /* If we found what we wanted, we keep going; otherwise, we're
4954 done. */
4955 if (!cp_parser_parse_definitely (parser))
4956 {
4957 bool error_p = false;
4958
4959 /* Restore the OLD_SCOPE since it was valid before the
4960 failed attempt at finding the last
4961 class-or-namespace-name. */
4962 parser->scope = old_scope;
4963 parser->qualifying_scope = saved_qualifying_scope;
07b8f133 4964
4965 /* If the next token is a decltype, and the one after that is a
4966 `::', then the decltype has failed to resolve to a class or
4967 enumeration type. Give this error even when parsing
4968 tentatively since it can't possibly be valid--and we're going
4969 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4970 won't get another chance.*/
4971 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4973 == CPP_SCOPE))
4974 {
4975 token = cp_lexer_consume_token (parser->lexer);
4976 error_at (token->location, "decltype evaluates to %qT, "
4977 "which is not a class or enumeration type",
4978 token->u.value);
4979 parser->scope = error_mark_node;
4980 error_p = true;
4981 /* As below. */
4982 success = true;
4983 cp_lexer_consume_token (parser->lexer);
4984 }
4985
ba0c587d 4986 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4987 break;
0a3b29ad 4988 /* If the next token is an identifier, and the one after
4989 that is a `::', then any valid interpretation would have
4990 found a class-or-namespace-name. */
4991 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
ccb84981 4992 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 4993 == CPP_SCOPE)
ccb84981 4994 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
0a3b29ad 4995 != CPP_COMPL))
4996 {
4997 token = cp_lexer_consume_token (parser->lexer);
ccb84981 4998 if (!error_p)
0a3b29ad 4999 {
b62240d5 5000 if (!token->ambiguous_p)
5001 {
5002 tree decl;
5003 tree ambiguous_decls;
5004
3369eb76 5005 decl = cp_parser_lookup_name (parser, token->u.value,
b62240d5 5006 none_type,
5007 /*is_template=*/false,
5008 /*is_namespace=*/false,
5009 /*check_dependency=*/true,
ad9ae192 5010 &ambiguous_decls,
5011 token->location);
b62240d5 5012 if (TREE_CODE (decl) == TEMPLATE_DECL)
ccb59bb6 5013 error_at (token->location,
5014 "%qD used without template parameters",
5015 decl);
b62240d5 5016 else if (ambiguous_decls)
5017 {
ccb59bb6 5018 error_at (token->location,
5019 "reference to %qD is ambiguous",
5020 token->u.value);
b62240d5 5021 print_candidates (ambiguous_decls);
5022 decl = error_mark_node;
5023 }
5024 else
3f00a6c0 5025 {
3f00a6c0 5026 if (cxx_dialect != cxx98)
c247dce0 5027 cp_parser_name_lookup_error
5028 (parser, token->u.value, decl, NLE_NOT_CXX98,
3f00a6c0 5029 token->location);
c247dce0 5030 else
5031 cp_parser_name_lookup_error
5032 (parser, token->u.value, decl, NLE_CXX98,
5033 token->location);
3f00a6c0 5034 }
b62240d5 5035 }
5036 parser->scope = error_mark_node;
0a3b29ad 5037 error_p = true;
6fc758aa 5038 /* Treat this as a successful nested-name-specifier
5039 due to:
5040
5041 [basic.lookup.qual]
5042
5043 If the name found is not a class-name (clause
5044 _class_) or namespace-name (_namespace.def_), the
5045 program is ill-formed. */
5046 success = true;
0a3b29ad 5047 }
5048 cp_lexer_consume_token (parser->lexer);
5049 }
5050 break;
5051 }
0a3b29ad 5052 /* We've found one valid nested-name-specifier. */
5053 success = true;
fbb01da7 5054 /* Name lookup always gives us a DECL. */
5055 if (TREE_CODE (new_scope) == TYPE_DECL)
5056 new_scope = TREE_TYPE (new_scope);
5057 /* Uses of "template" must be followed by actual templates. */
5058 if (template_keyword_p
5059 && !(CLASS_TYPE_P (new_scope)
5060 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5061 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5062 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5063 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5064 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5065 == TEMPLATE_ID_EXPR)))
2b9e3597 5066 permerror (input_location, TYPE_P (new_scope)
ac7549c7 5067 ? G_("%qT is not a template")
5068 : G_("%qD is not a template"),
561fec9d 5069 new_scope);
0a3b29ad 5070 /* If it is a class scope, try to complete it; we are about to
5071 be looking up names inside the class. */
fbb01da7 5072 if (TYPE_P (new_scope)
954ad420 5073 /* Since checking types for dependency can be expensive,
5074 avoid doing it if the type is already complete. */
fbb01da7 5075 && !COMPLETE_TYPE_P (new_scope)
954ad420 5076 /* Do not try to complete dependent types. */
fbb01da7 5077 && !dependent_type_p (new_scope))
fd0d54e4 5078 {
5079 new_scope = complete_type (new_scope);
5080 /* If it is a typedef to current class, use the current
5081 class instead, as the typedef won't have any names inside
5082 it yet. */
5083 if (!COMPLETE_TYPE_P (new_scope)
5084 && currently_open_class (new_scope))
5085 new_scope = TYPE_MAIN_VARIANT (new_scope);
5086 }
fbb01da7 5087 /* Make sure we look in the right scope the next time through
5088 the loop. */
5089 parser->scope = new_scope;
0a3b29ad 5090 }
5091
5092 /* If parsing tentatively, replace the sequence of tokens that makes
5093 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5094 token. That way, should we re-parse the token stream, we will
5095 not have to repeat the effort required to do the parse, nor will
5096 we issue duplicate error messages. */
19273cc2 5097 if (success && start)
0a3b29ad 5098 {
d15855b2 5099 cp_token *token;
9031d10b 5100
d15855b2 5101 token = cp_lexer_token_at (parser->lexer, start);
0a3b29ad 5102 /* Reset the contents of the START token. */
5103 token->type = CPP_NESTED_NAME_SPECIFIER;
d15855b2 5104 /* Retrieve any deferred checks. Do not pop this access checks yet
5105 so the memory will not be reclaimed during token replacing below. */
ba72912a 5106 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
3369eb76 5107 token->u.tree_check_value->value = parser->scope;
5108 token->u.tree_check_value->checks = get_deferred_access_checks ();
5109 token->u.tree_check_value->qualifying_scope =
5110 parser->qualifying_scope;
0a3b29ad 5111 token->keyword = RID_MAX;
9031d10b 5112
0a3b29ad 5113 /* Purge all subsequent tokens. */
19273cc2 5114 cp_lexer_purge_tokens_after (parser->lexer, start);
0a3b29ad 5115 }
074ab442 5116
d15855b2 5117 if (start)
5118 pop_to_parent_deferring_access_checks ();
0a3b29ad 5119
5120 return success ? parser->scope : NULL_TREE;
5121}
5122
5123/* Parse a nested-name-specifier. See
5124 cp_parser_nested_name_specifier_opt for details. This function
5125 behaves identically, except that it will an issue an error if no
c75ae97e 5126 nested-name-specifier is present. */
0a3b29ad 5127
5128static tree
ccb84981 5129cp_parser_nested_name_specifier (cp_parser *parser,
5130 bool typename_keyword_p,
0a3b29ad 5131 bool check_dependency_p,
3d0f901b 5132 bool type_p,
5133 bool is_declaration)
0a3b29ad 5134{
5135 tree scope;
5136
5137 /* Look for the nested-name-specifier. */
5138 scope = cp_parser_nested_name_specifier_opt (parser,
5139 typename_keyword_p,
5140 check_dependency_p,
3d0f901b 5141 type_p,
5142 is_declaration);
0a3b29ad 5143 /* If it was not present, issue an error message. */
5144 if (!scope)
5145 {
5146 cp_parser_error (parser, "expected nested-name-specifier");
bbd3de90 5147 parser->scope = NULL_TREE;
0a3b29ad 5148 }
5149
5150 return scope;
5151}
5152
3f00a6c0 5153/* Parse the qualifying entity in a nested-name-specifier. For C++98,
5154 this is either a class-name or a namespace-name (which corresponds
5155 to the class-or-namespace-name production in the grammar). For
5156 C++0x, it can also be a type-name that refers to an enumeration
370478b1 5157 type or a simple-template-id.
0a3b29ad 5158
5159 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5160 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5161 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5162 TYPE_P is TRUE iff the next name should be taken as a class-name,
5163 even the same name is declared to be another entity in the same
5164 scope.
5165
5166 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6fc758aa 5167 specified by the class-or-namespace-name. If neither is found the
5168 ERROR_MARK_NODE is returned. */
0a3b29ad 5169
5170static tree
3f00a6c0 5171cp_parser_qualifying_entity (cp_parser *parser,
5172 bool typename_keyword_p,
5173 bool template_keyword_p,
5174 bool check_dependency_p,
5175 bool type_p,
5176 bool is_declaration)
0a3b29ad 5177{
5178 tree saved_scope;
5179 tree saved_qualifying_scope;
5180 tree saved_object_scope;
5181 tree scope;
6fc758aa 5182 bool only_class_p;
3f00a6c0 5183 bool successful_parse_p;
0a3b29ad 5184
07b8f133 5185 /* DR 743: decltype can appear in a nested-name-specifier. */
5186 if (cp_lexer_next_token_is_decltype (parser->lexer))
5187 {
5188 scope = cp_parser_decltype (parser);
5189 if (TREE_CODE (scope) != ENUMERAL_TYPE
5190 && !MAYBE_CLASS_TYPE_P (scope))
5191 {
5192 cp_parser_simulate_error (parser);
5193 return error_mark_node;
5194 }
dc0c8153 5195 if (TYPE_NAME (scope))
5196 scope = TYPE_NAME (scope);
5197 return scope;
07b8f133 5198 }
5199
0a3b29ad 5200 /* Before we try to parse the class-name, we must save away the
5201 current PARSER->SCOPE since cp_parser_class_name will destroy
5202 it. */
5203 saved_scope = parser->scope;
5204 saved_qualifying_scope = parser->qualifying_scope;
5205 saved_object_scope = parser->object_scope;
6fc758aa 5206 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5207 there is no need to look for a namespace-name. */
3f00a6c0 5208 only_class_p = template_keyword_p
5209 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6fc758aa 5210 if (!only_class_p)
5211 cp_parser_parse_tentatively (parser);
ccb84981 5212 scope = cp_parser_class_name (parser,
0a3b29ad 5213 typename_keyword_p,
5214 template_keyword_p,
e2ae55f2 5215 type_p ? class_type : none_type,
0a3b29ad 5216 check_dependency_p,
3d0f901b 5217 /*class_head_p=*/false,
5218 is_declaration);
3f00a6c0 5219 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5220 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5221 if (!only_class_p
5222 && cxx_dialect != cxx98
5223 && !successful_parse_p)
5224 {
5225 /* Restore the saved scope. */
5226 parser->scope = saved_scope;
5227 parser->qualifying_scope = saved_qualifying_scope;
5228 parser->object_scope = saved_object_scope;
5229
5230 /* Parse tentatively. */
5231 cp_parser_parse_tentatively (parser);
5232
370478b1 5233 /* Parse a type-name */
5234 scope = cp_parser_type_name (parser);
e221246b 5235
5236 /* "If the name found does not designate a namespace or a class,
5237 enumeration, or dependent type, the program is ill-formed."
5238
5239 We cover classes and dependent types above and namespaces below,
5240 so this code is only looking for enums. */
5241 if (!scope || TREE_CODE (scope) != TYPE_DECL
5242 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5243 cp_parser_simulate_error (parser);
5244
3f00a6c0 5245 successful_parse_p = cp_parser_parse_definitely (parser);
5246 }
0a3b29ad 5247 /* If that didn't work, try for a namespace-name. */
3f00a6c0 5248 if (!only_class_p && !successful_parse_p)
0a3b29ad 5249 {
5250 /* Restore the saved scope. */
5251 parser->scope = saved_scope;
5252 parser->qualifying_scope = saved_qualifying_scope;
5253 parser->object_scope = saved_object_scope;
6fc758aa 5254 /* If we are not looking at an identifier followed by the scope
5255 resolution operator, then this is not part of a
5256 nested-name-specifier. (Note that this function is only used
5257 to parse the components of a nested-name-specifier.) */
5258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5259 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5260 return error_mark_node;
0a3b29ad 5261 scope = cp_parser_namespace_name (parser);
5262 }
5263
5264 return scope;
5265}
5266
5267/* Parse a postfix-expression.
5268
5269 postfix-expression:
5270 primary-expression
5271 postfix-expression [ expression ]
5272 postfix-expression ( expression-list [opt] )
5273 simple-type-specifier ( expression-list [opt] )
ccb84981 5274 typename :: [opt] nested-name-specifier identifier
0a3b29ad 5275 ( expression-list [opt] )
5276 typename :: [opt] nested-name-specifier template [opt] template-id
5277 ( expression-list [opt] )
5278 postfix-expression . template [opt] id-expression
5279 postfix-expression -> template [opt] id-expression
5280 postfix-expression . pseudo-destructor-name
5281 postfix-expression -> pseudo-destructor-name
5282 postfix-expression ++
5283 postfix-expression --
5284 dynamic_cast < type-id > ( expression )
5285 static_cast < type-id > ( expression )
5286 reinterpret_cast < type-id > ( expression )
5287 const_cast < type-id > ( expression )
5288 typeid ( expression )
5289 typeid ( type-id )
5290
5291 GNU Extension:
ccb84981 5292
0a3b29ad 5293 postfix-expression:
5294 ( type-id ) { initializer-list , [opt] }
5295
5296 This extension is a GNU version of the C99 compound-literal
5297 construct. (The C99 grammar uses `type-name' instead of `type-id',
5298 but they are essentially the same concept.)
5299
5300 If ADDRESS_P is true, the postfix expression is the operand of the
640aa28c 5301 `&' operator. CAST_P is true if this expression is the target of a
9031d10b 5302 cast.
0a3b29ad 5303
34da8800 5304 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5305 class member access expressions [expr.ref].
5306
0a3b29ad 5307 Returns a representation of the expression. */
5308
5309static tree
34da8800 5310cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
98b326fd 5311 bool member_access_only_p,
5312 cp_id_kind * pidk_return)
0a3b29ad 5313{
5314 cp_token *token;
5315 enum rid keyword;
0886adbc 5316 cp_id_kind idk = CP_ID_KIND_NONE;
0a3b29ad 5317 tree postfix_expression = NULL_TREE;
34da8800 5318 bool is_member_access = false;
0a3b29ad 5319
5320 /* Peek at the next token. */
5321 token = cp_lexer_peek_token (parser->lexer);
5322 /* Some of the productions are determined by keywords. */
5323 keyword = token->keyword;
5324 switch (keyword)
5325 {
5326 case RID_DYNCAST:
5327 case RID_STATCAST:
5328 case RID_REINTCAST:
5329 case RID_CONSTCAST:
5330 {
5331 tree type;
5332 tree expression;
5333 const char *saved_message;
5334
5335 /* All of these can be handled in the same way from the point
5336 of view of parsing. Begin by consuming the token
5337 identifying the cast. */
5338 cp_lexer_consume_token (parser->lexer);
ccb84981 5339
0a3b29ad 5340 /* New types cannot be defined in the cast. */
5341 saved_message = parser->type_definition_forbidden_message;
5342 parser->type_definition_forbidden_message
ca82e026 5343 = G_("types may not be defined in casts");
0a3b29ad 5344
5345 /* Look for the opening `<'. */
c247dce0 5346 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 5347 /* Parse the type to which we are casting. */
5348 type = cp_parser_type_id (parser);
5349 /* Look for the closing `>'. */
c247dce0 5350 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 5351 /* Restore the old message. */
5352 parser->type_definition_forbidden_message = saved_message;
5353
5354 /* And the expression which is being cast. */
c247dce0 5355 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
98b326fd 5356 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
c247dce0 5357 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 5358
5f6526e1 5359 /* Only type conversions to integral or enumeration types
5360 can be used in constant-expressions. */
bde9ebf7 5361 if (!cast_valid_in_integral_constant_expression_p (type)
c61e1212 5362 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
3938e0c2 5363 return error_mark_node;
5f6526e1 5364
0a3b29ad 5365 switch (keyword)
5366 {
5367 case RID_DYNCAST:
5368 postfix_expression
ebd21de4 5369 = build_dynamic_cast (type, expression, tf_warning_or_error);
0a3b29ad 5370 break;
5371 case RID_STATCAST:
5372 postfix_expression
ebd21de4 5373 = build_static_cast (type, expression, tf_warning_or_error);
0a3b29ad 5374 break;
5375 case RID_REINTCAST:
5376 postfix_expression
ebd21de4 5377 = build_reinterpret_cast (type, expression,
5378 tf_warning_or_error);
0a3b29ad 5379 break;
5380 case RID_CONSTCAST:
5381 postfix_expression
ebd21de4 5382 = build_const_cast (type, expression, tf_warning_or_error);
0a3b29ad 5383 break;
5384 default:
2e3e31d2 5385 gcc_unreachable ();
0a3b29ad 5386 }
5387 }
5388 break;
5389
5390 case RID_TYPEID:
5391 {
5392 tree type;
5393 const char *saved_message;
41f2d08e 5394 bool saved_in_type_id_in_expr_p;
0a3b29ad 5395
5396 /* Consume the `typeid' token. */
5397 cp_lexer_consume_token (parser->lexer);
5398 /* Look for the `(' token. */
c247dce0 5399 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 5400 /* Types cannot be defined in a `typeid' expression. */
5401 saved_message = parser->type_definition_forbidden_message;
5402 parser->type_definition_forbidden_message
ca82e026 5403 = G_("types may not be defined in a %<typeid%> expression");
0a3b29ad 5404 /* We can't be sure yet whether we're looking at a type-id or an
5405 expression. */
5406 cp_parser_parse_tentatively (parser);
5407 /* Try a type-id first. */
41f2d08e 5408 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5409 parser->in_type_id_in_expr_p = true;
0a3b29ad 5410 type = cp_parser_type_id (parser);
41f2d08e 5411 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 5412 /* Look for the `)' token. Otherwise, we can't be sure that
5413 we're not looking at an expression: consider `typeid (int
5414 (3))', for example. */
c247dce0 5415 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 5416 /* If all went well, simply lookup the type-id. */
5417 if (cp_parser_parse_definitely (parser))
5418 postfix_expression = get_typeid (type);
5419 /* Otherwise, fall back to the expression variant. */
5420 else
5421 {
5422 tree expression;
5423
5424 /* Look for an expression. */
98b326fd 5425 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
0a3b29ad 5426 /* Compute its typeid. */
5427 postfix_expression = build_typeid (expression);
5428 /* Look for the `)' token. */
c247dce0 5429 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 5430 }
4a430fd7 5431 /* Restore the saved message. */
5432 parser->type_definition_forbidden_message = saved_message;
368ad4b9 5433 /* `typeid' may not appear in an integral constant expression. */
244db24d 5434 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
368ad4b9 5435 return error_mark_node;
0a3b29ad 5436 }
5437 break;
ccb84981 5438
0a3b29ad 5439 case RID_TYPENAME:
5440 {
0a3b29ad 5441 tree type;
93673597 5442 /* The syntax permitted here is the same permitted for an
5443 elaborated-type-specifier. */
5444 type = cp_parser_elaborated_type_specifier (parser,
5445 /*is_friend=*/false,
5446 /*is_declaration=*/false);
0a3b29ad 5447 postfix_expression = cp_parser_functional_cast (parser, type);
5448 }
5449 break;
5450
5451 default:
5452 {
5453 tree type;
5454
5455 /* If the next thing is a simple-type-specifier, we may be
5456 looking at a functional cast. We could also be looking at
5457 an id-expression. So, we try the functional cast, and if
5458 that doesn't work we fall back to the primary-expression. */
5459 cp_parser_parse_tentatively (parser);
5460 /* Look for the simple-type-specifier. */
ccb84981 5461 type = cp_parser_simple_type_specifier (parser,
4b9b2871 5462 /*decl_specs=*/NULL,
5463 CP_PARSER_FLAGS_NONE);
0a3b29ad 5464 /* Parse the cast itself. */
5465 if (!cp_parser_error_occurred (parser))
ccb84981 5466 postfix_expression
0a3b29ad 5467 = cp_parser_functional_cast (parser, type);
5468 /* If that worked, we're done. */
5469 if (cp_parser_parse_definitely (parser))
5470 break;
5471
5472 /* If the functional-cast didn't work out, try a
5473 compound-literal. */
5f6526e1 5474 if (cp_parser_allow_gnu_extensions_p (parser)
5475 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
0a3b29ad 5476 {
c75b4594 5477 VEC(constructor_elt,gc) *initializer_list = NULL;
41f2d08e 5478 bool saved_in_type_id_in_expr_p;
0a3b29ad 5479
5480 cp_parser_parse_tentatively (parser);
5f6526e1 5481 /* Consume the `('. */
5482 cp_lexer_consume_token (parser->lexer);
5483 /* Parse the type. */
41f2d08e 5484 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5485 parser->in_type_id_in_expr_p = true;
5f6526e1 5486 type = cp_parser_type_id (parser);
41f2d08e 5487 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5f6526e1 5488 /* Look for the `)'. */
c247dce0 5489 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5f6526e1 5490 /* Look for the `{'. */
c247dce0 5491 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5f6526e1 5492 /* If things aren't going well, there's no need to
5493 keep going. */
5494 if (!cp_parser_error_occurred (parser))
0a3b29ad 5495 {
878870b4 5496 bool non_constant_p;
5f6526e1 5497 /* Parse the initializer-list. */
ccb84981 5498 initializer_list
878870b4 5499 = cp_parser_initializer_list (parser, &non_constant_p);
5f6526e1 5500 /* Allow a trailing `,'. */
5501 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5502 cp_lexer_consume_token (parser->lexer);
5503 /* Look for the final `}'. */
c247dce0 5504 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 5505 }
5506 /* If that worked, we're definitely looking at a
5507 compound-literal expression. */
5508 if (cp_parser_parse_definitely (parser))
5509 {
5510 /* Warn the user that a compound literal is not
5511 allowed in standard C++. */
29438999 5512 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
93b25def 5513 /* For simplicity, we disallow compound literals in
5514 constant-expressions. We could
66a0bac0 5515 allow compound literals of integer type, whose
5516 initializer was a constant, in constant
5517 expressions. Permitting that usage, as a further
5518 extension, would not change the meaning of any
5519 currently accepted programs. (Of course, as
5520 compound literals are not part of ISO C++, the
5521 standard has nothing to say.) */
c247dce0 5522 if (cp_parser_non_integral_constant_expression (parser,
5523 NIC_NCC))
66a0bac0 5524 {
5525 postfix_expression = error_mark_node;
5526 break;
5527 }
0a3b29ad 5528 /* Form the representation of the compound-literal. */
ccb84981 5529 postfix_expression
f82f1250 5530 = (finish_compound_literal
5531 (type, build_constructor (init_list_type_node,
95034afb 5532 initializer_list),
5533 tf_warning_or_error));
0a3b29ad 5534 break;
5535 }
5536 }
5537
5538 /* It must be a primary-expression. */
074ab442 5539 postfix_expression
5540 = cp_parser_primary_expression (parser, address_p, cast_p,
fbb01da7 5541 /*template_arg_p=*/false,
5542 &idk);
0a3b29ad 5543 }
5544 break;
5545 }
5546
0a3b29ad 5547 /* Keep looping until the postfix-expression is complete. */
5548 while (true)
5549 {
c08d51be 5550 if (idk == CP_ID_KIND_UNQUALIFIED
5551 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
0a3b29ad 5552 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
0886adbc 5553 /* It is not a Koenig lookup function call. */
ccb84981 5554 postfix_expression
0886adbc 5555 = unqualified_name_lookup_error (postfix_expression);
ccb84981 5556
0a3b29ad 5557 /* Peek at the next token. */
5558 token = cp_lexer_peek_token (parser->lexer);
5559
5560 switch (token->type)
5561 {
5562 case CPP_OPEN_SQUARE:
43bf5d72 5563 postfix_expression
5564 = cp_parser_postfix_open_square_expression (parser,
5565 postfix_expression,
5566 false);
5567 idk = CP_ID_KIND_NONE;
34da8800 5568 is_member_access = false;
0a3b29ad 5569 break;
5570
5571 case CPP_OPEN_PAREN:
5572 /* postfix-expression ( expression-list [opt] ) */
5573 {
cbce34a5 5574 bool koenig_p;
0dbadc68 5575 bool is_builtin_constant_p;
5576 bool saved_integral_constant_expression_p = false;
5577 bool saved_non_integral_constant_expression_p = false;
f352a3fb 5578 VEC(tree,gc) *args;
0dbadc68 5579
34da8800 5580 is_member_access = false;
5581
9031d10b 5582 is_builtin_constant_p
0dbadc68 5583 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5584 if (is_builtin_constant_p)
5585 {
5586 /* The whole point of __builtin_constant_p is to allow
5587 non-constant expressions to appear as arguments. */
5588 saved_integral_constant_expression_p
5589 = parser->integral_constant_expression_p;
5590 saved_non_integral_constant_expression_p
5591 = parser->non_integral_constant_expression_p;
5592 parser->integral_constant_expression_p = false;
5593 }
5594 args = (cp_parser_parenthesized_expression_list
33199a81 5595 (parser, non_attr,
d95d815d 5596 /*cast_p=*/false, /*allow_expansion_p=*/true,
0dbadc68 5597 /*non_constant_p=*/NULL));
5598 if (is_builtin_constant_p)
5599 {
5600 parser->integral_constant_expression_p
5601 = saved_integral_constant_expression_p;
5602 parser->non_integral_constant_expression_p
5603 = saved_non_integral_constant_expression_p;
5604 }
0a3b29ad 5605
f352a3fb 5606 if (args == NULL)
0986fa22 5607 {
5608 postfix_expression = error_mark_node;
5609 break;
5610 }
ccb84981 5611
5f6526e1 5612 /* Function calls are not permitted in
5613 constant-expressions. */
3a88b1db 5614 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5615 && cp_parser_non_integral_constant_expression (parser,
c247dce0 5616 NIC_FUNC_CALL))
5f6526e1 5617 {
3938e0c2 5618 postfix_expression = error_mark_node;
f352a3fb 5619 release_tree_vector (args);
3938e0c2 5620 break;
5f6526e1 5621 }
0a3b29ad 5622
cbce34a5 5623 koenig_p = false;
32f71e4f 5624 if (idk == CP_ID_KIND_UNQUALIFIED
5625 || idk == CP_ID_KIND_TEMPLATE_ID)
b7edeb23 5626 {
0e43a566 5627 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5628 {
f352a3fb 5629 if (!VEC_empty (tree, args))
0e43a566 5630 {
5631 koenig_p = true;
ba026663 5632 if (!any_type_dependent_arguments_p (args))
5633 postfix_expression
9dd72ec4 5634 = perform_koenig_lookup (postfix_expression, args,
8411500a 5635 /*include_std=*/false,
5636 tf_warning_or_error);
0e43a566 5637 }
5638 else
5639 postfix_expression
5640 = unqualified_fn_lookup_error (postfix_expression);
5641 }
3ed12242 5642 /* We do not perform argument-dependent lookup if
5643 normal lookup finds a non-function, in accordance
5644 with the expected resolution of DR 218. */
f352a3fb 5645 else if (!VEC_empty (tree, args)
5646 && is_overloaded_fn (postfix_expression))
cbce34a5 5647 {
0e43a566 5648 tree fn = get_first_fn (postfix_expression);
52b882a6 5649 fn = STRIP_TEMPLATE (fn);
0e43a566 5650
52b882a6 5651 /* Do not do argument dependent lookup if regular
5652 lookup finds a member function or a block-scope
5653 function declaration. [basic.lookup.argdep]/3 */
5654 if (!DECL_FUNCTION_MEMBER_P (fn)
5655 && !DECL_LOCAL_FUNCTION_P (fn))
0e43a566 5656 {
5657 koenig_p = true;
ba026663 5658 if (!any_type_dependent_arguments_p (args))
5659 postfix_expression
9dd72ec4 5660 = perform_koenig_lookup (postfix_expression, args,
8411500a 5661 /*include_std=*/false,
5662 tf_warning_or_error);
0e43a566 5663 }
cbce34a5 5664 }
b7edeb23 5665 }
ccb84981 5666
13795292 5667 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
0a3b29ad 5668 {
13795292 5669 tree instance = TREE_OPERAND (postfix_expression, 0);
5670 tree fn = TREE_OPERAND (postfix_expression, 1);
5671
5672 if (processing_template_decl
5673 && (type_dependent_expression_p (instance)
5674 || (!BASELINK_P (fn)
5675 && TREE_CODE (fn) != FIELD_DECL)
ed45372d 5676 || type_dependent_expression_p (fn)
13795292 5677 || any_type_dependent_arguments_p (args)))
5678 {
5679 postfix_expression
f352a3fb 5680 = build_nt_call_vec (postfix_expression, args);
5681 release_tree_vector (args);
13795292 5682 break;
5683 }
3c33f9f3 5684
5685 if (BASELINK_P (fn))
98b326fd 5686 {
3c33f9f3 5687 postfix_expression
ccb84981 5688 = (build_new_method_call
f352a3fb 5689 (instance, fn, &args, NULL_TREE,
ccb84981 5690 (idk == CP_ID_KIND_QUALIFIED
884a66e4 5691 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5692 : LOOKUP_NORMAL),
ebd21de4 5693 /*fn_p=*/NULL,
5694 tf_warning_or_error));
98b326fd 5695 }
3c33f9f3 5696 else
5697 postfix_expression
f352a3fb 5698 = finish_call_expr (postfix_expression, &args,
3c33f9f3 5699 /*disallow_virtual=*/false,
ebd21de4 5700 /*koenig_p=*/false,
5701 tf_warning_or_error);
0a3b29ad 5702 }
13795292 5703 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5704 || TREE_CODE (postfix_expression) == MEMBER_REF
5705 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
0a3b29ad 5706 postfix_expression = (build_offset_ref_call_from_tree
f352a3fb 5707 (postfix_expression, &args));
0886adbc 5708 else if (idk == CP_ID_KIND_QUALIFIED)
b3c48b5d 5709 /* A call to a static class member, or a namespace-scope
5710 function. */
5711 postfix_expression
f352a3fb 5712 = finish_call_expr (postfix_expression, &args,
cbce34a5 5713 /*disallow_virtual=*/true,
ebd21de4 5714 koenig_p,
5715 tf_warning_or_error);
0a3b29ad 5716 else
b3c48b5d 5717 /* All other function calls. */
ccb84981 5718 postfix_expression
f352a3fb 5719 = finish_call_expr (postfix_expression, &args,
cbce34a5 5720 /*disallow_virtual=*/false,
ebd21de4 5721 koenig_p,
5722 tf_warning_or_error);
0a3b29ad 5723
5724 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
0886adbc 5725 idk = CP_ID_KIND_NONE;
f352a3fb 5726
5727 release_tree_vector (args);
0a3b29ad 5728 }
5729 break;
ccb84981 5730
0a3b29ad 5731 case CPP_DOT:
5732 case CPP_DEREF:
ccb84981 5733 /* postfix-expression . template [opt] id-expression
5734 postfix-expression . pseudo-destructor-name
0a3b29ad 5735 postfix-expression -> template [opt] id-expression
5736 postfix-expression -> pseudo-destructor-name */
207355ad 5737
43bf5d72 5738 /* Consume the `.' or `->' operator. */
5739 cp_lexer_consume_token (parser->lexer);
0a3b29ad 5740
43bf5d72 5741 postfix_expression
5742 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5743 postfix_expression,
ad9ae192 5744 false, &idk,
5745 token->location);
34da8800 5746
5747 is_member_access = true;
0a3b29ad 5748 break;
5749
5750 case CPP_PLUS_PLUS:
5751 /* postfix-expression ++ */
5752 /* Consume the `++' token. */
5753 cp_lexer_consume_token (parser->lexer);
364c2f43 5754 /* Generate a representation for the complete expression. */
ccb84981 5755 postfix_expression
5756 = finish_increment_expr (postfix_expression,
364c2f43 5757 POSTINCREMENT_EXPR);
5f6526e1 5758 /* Increments may not appear in constant-expressions. */
c247dce0 5759 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
3938e0c2 5760 postfix_expression = error_mark_node;
0886adbc 5761 idk = CP_ID_KIND_NONE;
34da8800 5762 is_member_access = false;
0a3b29ad 5763 break;
5764
5765 case CPP_MINUS_MINUS:
5766 /* postfix-expression -- */
5767 /* Consume the `--' token. */
5768 cp_lexer_consume_token (parser->lexer);
364c2f43 5769 /* Generate a representation for the complete expression. */
ccb84981 5770 postfix_expression
5771 = finish_increment_expr (postfix_expression,
364c2f43 5772 POSTDECREMENT_EXPR);
5f6526e1 5773 /* Decrements may not appear in constant-expressions. */
c247dce0 5774 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
3938e0c2 5775 postfix_expression = error_mark_node;
0886adbc 5776 idk = CP_ID_KIND_NONE;
34da8800 5777 is_member_access = false;
0a3b29ad 5778 break;
5779
5780 default:
98b326fd 5781 if (pidk_return != NULL)
5782 * pidk_return = idk;
34da8800 5783 if (member_access_only_p)
5784 return is_member_access? postfix_expression : error_mark_node;
5785 else
5786 return postfix_expression;
0a3b29ad 5787 }
5788 }
5789
5790 /* We should never get here. */
2e3e31d2 5791 gcc_unreachable ();
0a3b29ad 5792 return error_mark_node;
5793}
5794
43bf5d72 5795/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5796 by cp_parser_builtin_offsetof. We're looking for
5797
5798 postfix-expression [ expression ]
91b6856f 5799 postfix-expression [ braced-init-list ] (C++11)
43bf5d72 5800
5801 FOR_OFFSETOF is set if we're being called in that context, which
5802 changes how we deal with integer constant expressions. */
5803
5804static tree
5805cp_parser_postfix_open_square_expression (cp_parser *parser,
5806 tree postfix_expression,
5807 bool for_offsetof)
5808{
5809 tree index;
ef0b0c72 5810 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43bf5d72 5811
5812 /* Consume the `[' token. */
5813 cp_lexer_consume_token (parser->lexer);
5814
5815 /* Parse the index expression. */
5816 /* ??? For offsetof, there is a question of what to allow here. If
5817 offsetof is not being used in an integral constant expression context,
5818 then we *could* get the right answer by computing the value at runtime.
5819 If we are in an integral constant expression context, then we might
5820 could accept any constant expression; hard to say without analysis.
5821 Rather than open the barn door too wide right away, allow only integer
4a44ba29 5822 constant expressions here. */
43bf5d72 5823 if (for_offsetof)
5824 index = cp_parser_constant_expression (parser, false, NULL);
5825 else
91b6856f 5826 {
5827 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5828 {
5829 bool expr_nonconst_p;
5830 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5831 index = cp_parser_braced_list (parser, &expr_nonconst_p);
5832 }
5833 else
5834 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5835 }
43bf5d72 5836
5837 /* Look for the closing `]'. */
c247dce0 5838 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
43bf5d72 5839
5840 /* Build the ARRAY_REF. */
ef0b0c72 5841 postfix_expression = grok_array_decl (loc, postfix_expression, index);
43bf5d72 5842
5843 /* When not doing offsetof, array references are not permitted in
5844 constant-expressions. */
5845 if (!for_offsetof
c247dce0 5846 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
43bf5d72 5847 postfix_expression = error_mark_node;
5848
5849 return postfix_expression;
5850}
5851
5852/* A subroutine of cp_parser_postfix_expression that also gets hijacked
5853 by cp_parser_builtin_offsetof. We're looking for
5854
5855 postfix-expression . template [opt] id-expression
5856 postfix-expression . pseudo-destructor-name
5857 postfix-expression -> template [opt] id-expression
5858 postfix-expression -> pseudo-destructor-name
5859
5860 FOR_OFFSETOF is set if we're being called in that context. That sorta
5861 limits what of the above we'll actually accept, but nevermind.
5862 TOKEN_TYPE is the "." or "->" token, which will already have been
5863 removed from the stream. */
5864
5865static tree
5866cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5867 enum cpp_ttype token_type,
5868 tree postfix_expression,
ad9ae192 5869 bool for_offsetof, cp_id_kind *idk,
5870 location_t location)
43bf5d72 5871{
5872 tree name;
5873 bool dependent_p;
19efe073 5874 bool pseudo_destructor_p;
43bf5d72 5875 tree scope = NULL_TREE;
5876
5877 /* If this is a `->' operator, dereference the pointer. */
5878 if (token_type == CPP_DEREF)
ef0b0c72 5879 postfix_expression = build_x_arrow (location, postfix_expression,
2ad2700d 5880 tf_warning_or_error);
43bf5d72 5881 /* Check to see whether or not the expression is type-dependent. */
5882 dependent_p = type_dependent_expression_p (postfix_expression);
5883 /* The identifier following the `->' or `.' is not qualified. */
5884 parser->scope = NULL_TREE;
5885 parser->qualifying_scope = NULL_TREE;
5886 parser->object_scope = NULL_TREE;
5887 *idk = CP_ID_KIND_NONE;
98b326fd 5888
43bf5d72 5889 /* Enter the scope corresponding to the type of the object
5890 given by the POSTFIX_EXPRESSION. */
130bb1d4 5891 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
43bf5d72 5892 {
130bb1d4 5893 scope = TREE_TYPE (postfix_expression);
43bf5d72 5894 /* According to the standard, no expression should ever have
5895 reference type. Unfortunately, we do not currently match
5896 the standard in this respect in that our internal representation
5897 of an expression may have reference type even when the standard
5898 says it does not. Therefore, we have to manually obtain the
5899 underlying type here. */
5900 scope = non_reference (scope);
5901 /* The type of the POSTFIX_EXPRESSION must be complete. */
e3efb139 5902 if (scope == unknown_type_node)
5903 {
ccb59bb6 5904 error_at (location, "%qE does not have class type",
5905 postfix_expression);
e3efb139 5906 scope = NULL_TREE;
5907 }
56f83c40 5908 /* Unlike the object expression in other contexts, *this is not
5909 required to be of complete type for purposes of class member
5910 access (5.2.5) outside the member function body. */
5911 else if (scope != current_class_ref
5912 && !(processing_template_decl && scope == current_class_type))
e3efb139 5913 scope = complete_type_or_else (scope, NULL_TREE);
130bb1d4 5914 /* Let the name lookup machinery know that we are processing a
5915 class member access expression. */
5916 parser->context->object_type = scope;
43bf5d72 5917 /* If something went wrong, we want to be able to discern that case,
5918 as opposed to the case where there was no SCOPE due to the type
5919 of expression being dependent. */
5920 if (!scope)
5921 scope = error_mark_node;
5922 /* If the SCOPE was erroneous, make the various semantic analysis
5923 functions exit quickly -- and without issuing additional error
5924 messages. */
5925 if (scope == error_mark_node)
5926 postfix_expression = error_mark_node;
5927 }
5928
19efe073 5929 /* Assume this expression is not a pseudo-destructor access. */
5930 pseudo_destructor_p = false;
5931
5932 /* If the SCOPE is a scalar type, then, if this is a valid program,
8da7c9e9 5933 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5934 is type dependent, it can be pseudo-destructor-name or something else.
5935 Try to parse it as pseudo-destructor-name first. */
5936 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
43bf5d72 5937 {
19efe073 5938 tree s;
5939 tree type;
5940
5941 cp_parser_parse_tentatively (parser);
5942 /* Parse the pseudo-destructor-name. */
5943 s = NULL_TREE;
5944 cp_parser_pseudo_destructor_name (parser, &s, &type);
8da7c9e9 5945 if (dependent_p
5946 && (cp_parser_error_occurred (parser)
5947 || TREE_CODE (type) != TYPE_DECL
5948 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5949 cp_parser_abort_tentative_parse (parser);
5950 else if (cp_parser_parse_definitely (parser))
19efe073 5951 {
5952 pseudo_destructor_p = true;
5953 postfix_expression
5954 = finish_pseudo_destructor_expr (postfix_expression,
5955 s, TREE_TYPE (type));
5956 }
5957 }
5958
5959 if (!pseudo_destructor_p)
5960 {
5961 /* If the SCOPE is not a scalar type, we are looking at an
5962 ordinary class member access expression, rather than a
5963 pseudo-destructor-name. */
fbb01da7 5964 bool template_p;
ad9ae192 5965 cp_token *token = cp_lexer_peek_token (parser->lexer);
43bf5d72 5966 /* Parse the id-expression. */
074ab442 5967 name = (cp_parser_id_expression
5968 (parser,
fbb01da7 5969 cp_parser_optional_template_keyword (parser),
5970 /*check_dependency_p=*/true,
5971 &template_p,
197c9df7 5972 /*declarator_p=*/false,
130bb1d4 5973 /*optional_p=*/false));
43bf5d72 5974 /* In general, build a SCOPE_REF if the member name is qualified.
5975 However, if the name was not dependent and has already been
5976 resolved; there is no need to build the SCOPE_REF. For example;
5977
653e5405 5978 struct X { void f(); };
5979 template <typename T> void f(T* t) { t->X::f(); }
43bf5d72 5980
5981 Even though "t" is dependent, "X::f" is not and has been resolved
5982 to a BASELINK; there is no need to include scope information. */
5983
5984 /* But we do need to remember that there was an explicit scope for
5985 virtual function calls. */
5986 if (parser->scope)
5987 *idk = CP_ID_KIND_QUALIFIED;
5988
e2ae55f2 5989 /* If the name is a template-id that names a type, we will get a
5990 TYPE_DECL here. That is invalid code. */
5991 if (TREE_CODE (name) == TYPE_DECL)
43bf5d72 5992 {
ccb59bb6 5993 error_at (token->location, "invalid use of %qD", name);
e2ae55f2 5994 postfix_expression = error_mark_node;
5995 }
5996 else
5997 {
5998 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5999 {
4ac56fde 6000 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6001 {
6002 error_at (token->location, "%<%D::%D%> is not a class member",
6003 parser->scope, name);
6004 postfix_expression = error_mark_node;
6005 }
6006 else
6007 name = build_qualified_name (/*type=*/NULL_TREE,
6008 parser->scope,
6009 name,
6010 template_p);
e2ae55f2 6011 parser->scope = NULL_TREE;
6012 parser->qualifying_scope = NULL_TREE;
6013 parser->object_scope = NULL_TREE;
6014 }
8272c334 6015 if (parser->scope && name && BASELINK_P (name))
e2ae55f2 6016 adjust_result_of_qualified_name_lookup
8272c334 6017 (name, parser->scope, scope);
e2ae55f2 6018 postfix_expression
fbb01da7 6019 = finish_class_member_access_expr (postfix_expression, name,
ebd21de4 6020 template_p,
6021 tf_warning_or_error);
43bf5d72 6022 }
43bf5d72 6023 }
43bf5d72 6024
6025 /* We no longer need to look up names in the scope of the object on
6026 the left-hand side of the `.' or `->' operator. */
6027 parser->context->object_type = NULL_TREE;
6028
6029 /* Outside of offsetof, these operators may not appear in
6030 constant-expressions. */
6031 if (!for_offsetof
207355ad 6032 && (cp_parser_non_integral_constant_expression
c247dce0 6033 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
43bf5d72 6034 postfix_expression = error_mark_node;
6035
6036 return postfix_expression;
6037}
6038
0986fa22 6039/* Parse a parenthesized expression-list.
0a3b29ad 6040
6041 expression-list:
6042 assignment-expression
6043 expression-list, assignment-expression
6044
0986fa22 6045 attribute-list:
6046 expression-list
6047 identifier
6048 identifier, expression-list
6049
640aa28c 6050 CAST_P is true if this expression is the target of a cast.
6051
d95d815d 6052 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6053 argument pack.
6054
f352a3fb 6055 Returns a vector of trees. Each element is a representation of an
6056 assignment-expression. NULL is returned if the ( and or ) are
6057 missing. An empty, but allocated, vector is returned on no
33199a81 6058 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6059 if we are parsing an attribute list for an attribute that wants a
6060 plain identifier argument, normal_attr for an attribute that wants
6061 an expression, or non_attr if we aren't parsing an attribute list. If
f352a3fb 6062 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6063 not all of the expressions in the list were constant. */
0a3b29ad 6064
f352a3fb 6065static VEC(tree,gc) *
ccb84981 6066cp_parser_parenthesized_expression_list (cp_parser* parser,
33199a81 6067 int is_attribute_list,
640aa28c 6068 bool cast_p,
d95d815d 6069 bool allow_expansion_p,
878870b4 6070 bool *non_constant_p)
0a3b29ad 6071{
f352a3fb 6072 VEC(tree,gc) *expression_list;
33199a81 6073 bool fold_expr_p = is_attribute_list != non_attr;
0986fa22 6074 tree identifier = NULL_TREE;
9247ecc6 6075 bool saved_greater_than_is_operator_p;
878870b4 6076
6077 /* Assume all the expressions will be constant. */
6078 if (non_constant_p)
6079 *non_constant_p = false;
6080
c247dce0 6081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
f352a3fb 6082 return NULL;
6083
6084 expression_list = make_tree_vector ();
ccb84981 6085
9247ecc6 6086 /* Within a parenthesized expression, a `>' token is always
6087 the greater-than operator. */
6088 saved_greater_than_is_operator_p
6089 = parser->greater_than_is_operator_p;
6090 parser->greater_than_is_operator_p = true;
6091
0a3b29ad 6092 /* Consume expressions until there are no more. */
0986fa22 6093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6094 while (true)
6095 {
6096 tree expr;
ccb84981 6097
0986fa22 6098 /* At the beginning of attribute lists, check to see if the
6099 next token is an identifier. */
33199a81 6100 if (is_attribute_list == id_attr
0986fa22 6101 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6102 {
6103 cp_token *token;
ccb84981 6104
0986fa22 6105 /* Consume the identifier. */
6106 token = cp_lexer_consume_token (parser->lexer);
6107 /* Save the identifier. */
3369eb76 6108 identifier = token->u.value;
0986fa22 6109 }
6110 else
6111 {
f82f1250 6112 bool expr_non_constant_p;
6113
0986fa22 6114 /* Parse the next assignment-expression. */
f82f1250 6115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6116 {
6117 /* A braced-init-list. */
bf8d19fe 6118 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 6119 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6120 if (non_constant_p && expr_non_constant_p)
6121 *non_constant_p = true;
6122 }
6123 else if (non_constant_p)
878870b4 6124 {
ccb84981 6125 expr = (cp_parser_constant_expression
878870b4 6126 (parser, /*allow_non_constant_p=*/true,
6127 &expr_non_constant_p));
6128 if (expr_non_constant_p)
6129 *non_constant_p = true;
6130 }
6131 else
98b326fd 6132 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
0a3b29ad 6133
4736f3be 6134 if (fold_expr_p)
6135 expr = fold_non_dependent_expr (expr);
6136
d95d815d 6137 /* If we have an ellipsis, then this is an expression
6138 expansion. */
6139 if (allow_expansion_p
6140 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6141 {
6142 /* Consume the `...'. */
6143 cp_lexer_consume_token (parser->lexer);
6144
6145 /* Build the argument pack. */
6146 expr = make_pack_expansion (expr);
6147 }
6148
0986fa22 6149 /* Add it to the list. We add error_mark_node
6150 expressions to the list, so that we can still tell if
6151 the correct form for a parenthesized expression-list
6152 is found. That gives better errors. */
f352a3fb 6153 VEC_safe_push (tree, gc, expression_list, expr);
0a3b29ad 6154
0986fa22 6155 if (expr == error_mark_node)
6156 goto skip_comma;
6157 }
0a3b29ad 6158
0986fa22 6159 /* After the first item, attribute lists look the same as
6160 expression lists. */
33199a81 6161 is_attribute_list = non_attr;
ccb84981 6162
0986fa22 6163 get_comma:;
6164 /* If the next token isn't a `,', then we are done. */
6165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6166 break;
6167
6168 /* Otherwise, consume the `,' and keep going. */
6169 cp_lexer_consume_token (parser->lexer);
6170 }
ccb84981 6171
c247dce0 6172 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
0986fa22 6173 {
6174 int ending;
ccb84981 6175
0986fa22 6176 skip_comma:;
6177 /* We try and resync to an unnested comma, as that will give the
6178 user better diagnostics. */
ccb84981 6179 ending = cp_parser_skip_to_closing_parenthesis (parser,
6180 /*recovering=*/true,
92b128ed 6181 /*or_comma=*/true,
3d0f901b 6182 /*consume_paren=*/true);
0986fa22 6183 if (ending < 0)
6184 goto get_comma;
6185 if (!ending)
9247ecc6 6186 {
6187 parser->greater_than_is_operator_p
6188 = saved_greater_than_is_operator_p;
f352a3fb 6189 return NULL;
9247ecc6 6190 }
0a3b29ad 6191 }
6192
9247ecc6 6193 parser->greater_than_is_operator_p
6194 = saved_greater_than_is_operator_p;
6195
0986fa22 6196 if (identifier)
f352a3fb 6197 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
ccb84981 6198
0986fa22 6199 return expression_list;
0a3b29ad 6200}
6201
6202/* Parse a pseudo-destructor-name.
6203
6204 pseudo-destructor-name:
6205 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6206 :: [opt] nested-name-specifier template template-id :: ~ type-name
6207 :: [opt] nested-name-specifier [opt] ~ type-name
6208
6209 If either of the first two productions is used, sets *SCOPE to the
6210 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6211 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
60c6ea84 6212 or ERROR_MARK_NODE if the parse fails. */
0a3b29ad 6213
6214static void
ccb84981 6215cp_parser_pseudo_destructor_name (cp_parser* parser,
653e5405 6216 tree* scope,
6217 tree* type)
0a3b29ad 6218{
6219 bool nested_name_specifier_p;
6220
30aea172 6221 /* Assume that things will not work out. */
6222 *type = error_mark_node;
6223
0a3b29ad 6224 /* Look for the optional `::' operator. */
130bb1d4 6225 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
0a3b29ad 6226 /* Look for the optional nested-name-specifier. */
ccb84981 6227 nested_name_specifier_p
0a3b29ad 6228 = (cp_parser_nested_name_specifier_opt (parser,
6229 /*typename_keyword_p=*/false,
6230 /*check_dependency_p=*/true,
3d0f901b 6231 /*type_p=*/false,
d046014d 6232 /*is_declaration=*/false)
0a3b29ad 6233 != NULL_TREE);
6234 /* Now, if we saw a nested-name-specifier, we might be doing the
6235 second production. */
ccb84981 6236 if (nested_name_specifier_p
0a3b29ad 6237 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6238 {
6239 /* Consume the `template' keyword. */
6240 cp_lexer_consume_token (parser->lexer);
6241 /* Parse the template-id. */
ccb84981 6242 cp_parser_template_id (parser,
0a3b29ad 6243 /*template_keyword_p=*/true,
3d0f901b 6244 /*check_dependency_p=*/false,
6245 /*is_declaration=*/true);
0a3b29ad 6246 /* Look for the `::' token. */
c247dce0 6247 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 6248 }
6249 /* If the next token is not a `~', then there might be some
6beb3f76 6250 additional qualification. */
0a3b29ad 6251 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6252 {
d1cc3060 6253 /* At this point, we're looking for "type-name :: ~". The type-name
6254 must not be a class-name, since this is a pseudo-destructor. So,
6255 it must be either an enum-name, or a typedef-name -- both of which
6256 are just identifiers. So, we peek ahead to check that the "::"
6257 and "~" tokens are present; if they are not, then we can avoid
6258 calling type_name. */
6259 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6260 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6261 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6262 {
6263 cp_parser_error (parser, "non-scalar type");
6264 return;
6265 }
6266
0a3b29ad 6267 /* Look for the type-name. */
674e90bd 6268 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
30aea172 6269 if (*scope == error_mark_node)
6270 return;
6271
0a3b29ad 6272 /* Look for the `::' token. */
c247dce0 6273 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
0a3b29ad 6274 }
6275 else
6276 *scope = NULL_TREE;
6277
6278 /* Look for the `~'. */
c247dce0 6279 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
ef60d9db 6280
6281 /* Once we see the ~, this has to be a pseudo-destructor. */
6282 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6283 cp_parser_commit_to_tentative_parse (parser);
6284
0a3b29ad 6285 /* Look for the type-name again. We are not responsible for
6286 checking that it matches the first type-name. */
674e90bd 6287 *type = cp_parser_nonclass_name (parser);
0a3b29ad 6288}
6289
6290/* Parse a unary-expression.
6291
6292 unary-expression:
6293 postfix-expression
6294 ++ cast-expression
6295 -- cast-expression
6296 unary-operator cast-expression
6297 sizeof unary-expression
6298 sizeof ( type-id )
f07e4b1b 6299 alignof ( type-id ) [C++0x]
0a3b29ad 6300 new-expression
6301 delete-expression
6302
6303 GNU Extensions:
6304
6305 unary-expression:
6306 __extension__ cast-expression
6307 __alignof__ unary-expression
6308 __alignof__ ( type-id )
f07e4b1b 6309 alignof unary-expression [C++0x]
0a3b29ad 6310 __real__ cast-expression
6311 __imag__ cast-expression
6312 && identifier
6313
6314 ADDRESS_P is true iff the unary-expression is appearing as the
640aa28c 6315 operand of the `&' operator. CAST_P is true if this expression is
6316 the target of a cast.
0a3b29ad 6317
755edffd 6318 Returns a representation of the expression. */
0a3b29ad 6319
6320static tree
98b326fd 6321cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6322 cp_id_kind * pidk)
0a3b29ad 6323{
6324 cp_token *token;
6325 enum tree_code unary_operator;
6326
6327 /* Peek at the next token. */
6328 token = cp_lexer_peek_token (parser->lexer);
6329 /* Some keywords give away the kind of expression. */
6330 if (token->type == CPP_KEYWORD)
6331 {
6332 enum rid keyword = token->keyword;
6333
6334 switch (keyword)
6335 {
6336 case RID_ALIGNOF:
0a3b29ad 6337 case RID_SIZEOF:
6338 {
6339 tree operand;
e47f82ba 6340 enum tree_code op;
ccb84981 6341
e47f82ba 6342 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6343 /* Consume the token. */
0a3b29ad 6344 cp_lexer_consume_token (parser->lexer);
6345 /* Parse the operand. */
6346 operand = cp_parser_sizeof_operand (parser, keyword);
6347
e47f82ba 6348 if (TYPE_P (operand))
6349 return cxx_sizeof_or_alignof_type (operand, op, true);
0a3b29ad 6350 else
f07e4b1b 6351 {
6352 /* ISO C++ defines alignof only with types, not with
6353 expressions. So pedwarn if alignof is used with a non-
6354 type expression. However, __alignof__ is ok. */
cad7fbbd 6355 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
29438999 6356 pedwarn (token->location, OPT_Wpedantic,
f07e4b1b 6357 "ISO C++ does not allow %<alignof%> "
6358 "with a non-type");
6359
6360 return cxx_sizeof_or_alignof_expr (operand, op, true);
6361 }
0a3b29ad 6362 }
6363
6364 case RID_NEW:
6365 return cp_parser_new_expression (parser);
6366
6367 case RID_DELETE:
6368 return cp_parser_delete_expression (parser);
ccb84981 6369
0a3b29ad 6370 case RID_EXTENSION:
6371 {
6372 /* The saved value of the PEDANTIC flag. */
6373 int saved_pedantic;
6374 tree expr;
6375
6376 /* Save away the PEDANTIC flag. */
6377 cp_parser_extension_opt (parser, &saved_pedantic);
6378 /* Parse the cast-expression. */
a63bc44c 6379 expr = cp_parser_simple_cast_expression (parser);
0a3b29ad 6380 /* Restore the PEDANTIC flag. */
6381 pedantic = saved_pedantic;
6382
6383 return expr;
6384 }
6385
6386 case RID_REALPART:
6387 case RID_IMAGPART:
6388 {
6389 tree expression;
6390
6391 /* Consume the `__real__' or `__imag__' token. */
6392 cp_lexer_consume_token (parser->lexer);
6393 /* Parse the cast-expression. */
a63bc44c 6394 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 6395 /* Create the complete representation. */
ef0b0c72 6396 return build_x_unary_op (token->location,
6397 (keyword == RID_REALPART
0a3b29ad 6398 ? REALPART_EXPR : IMAGPART_EXPR),
ebd21de4 6399 expression,
6400 tf_warning_or_error);
0a3b29ad 6401 }
6402 break;
6403
4c0315d0 6404 case RID_TRANSACTION_ATOMIC:
6405 case RID_TRANSACTION_RELAXED:
6406 return cp_parser_transaction_expression (parser, keyword);
6407
98fe9664 6408 case RID_NOEXCEPT:
6409 {
6410 tree expr;
6411 const char *saved_message;
6412 bool saved_integral_constant_expression_p;
6413 bool saved_non_integral_constant_expression_p;
6414 bool saved_greater_than_is_operator_p;
6415
6416 cp_lexer_consume_token (parser->lexer);
6417 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6418
6419 saved_message = parser->type_definition_forbidden_message;
6420 parser->type_definition_forbidden_message
6421 = G_("types may not be defined in %<noexcept%> expressions");
6422
6423 saved_integral_constant_expression_p
6424 = parser->integral_constant_expression_p;
6425 saved_non_integral_constant_expression_p
6426 = parser->non_integral_constant_expression_p;
6427 parser->integral_constant_expression_p = false;
6428
6429 saved_greater_than_is_operator_p
6430 = parser->greater_than_is_operator_p;
6431 parser->greater_than_is_operator_p = true;
6432
6433 ++cp_unevaluated_operand;
6434 ++c_inhibit_evaluation_warnings;
6435 expr = cp_parser_expression (parser, false, NULL);
6436 --c_inhibit_evaluation_warnings;
6437 --cp_unevaluated_operand;
6438
6439 parser->greater_than_is_operator_p
6440 = saved_greater_than_is_operator_p;
6441
6442 parser->integral_constant_expression_p
6443 = saved_integral_constant_expression_p;
6444 parser->non_integral_constant_expression_p
6445 = saved_non_integral_constant_expression_p;
6446
6447 parser->type_definition_forbidden_message = saved_message;
6448
6449 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
1194d077 6450 return finish_noexcept_expr (expr, tf_warning_or_error);
98fe9664 6451 }
6452
0a3b29ad 6453 default:
6454 break;
6455 }
6456 }
6457
6458 /* Look for the `:: new' and `:: delete', which also signal the
6459 beginning of a new-expression, or delete-expression,
6460 respectively. If the next token is `::', then it might be one of
6461 these. */
6462 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6463 {
6464 enum rid keyword;
6465
6466 /* See if the token after the `::' is one of the keywords in
6467 which we're interested. */
6468 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6469 /* If it's `new', we have a new-expression. */
6470 if (keyword == RID_NEW)
6471 return cp_parser_new_expression (parser);
6472 /* Similarly, for `delete'. */
6473 else if (keyword == RID_DELETE)
6474 return cp_parser_delete_expression (parser);
6475 }
6476
6477 /* Look for a unary operator. */
6478 unary_operator = cp_parser_unary_operator (token);
6479 /* The `++' and `--' operators can be handled similarly, even though
6480 they are not technically unary-operators in the grammar. */
6481 if (unary_operator == ERROR_MARK)
6482 {
6483 if (token->type == CPP_PLUS_PLUS)
6484 unary_operator = PREINCREMENT_EXPR;
6485 else if (token->type == CPP_MINUS_MINUS)
6486 unary_operator = PREDECREMENT_EXPR;
6487 /* Handle the GNU address-of-label extension. */
6488 else if (cp_parser_allow_gnu_extensions_p (parser)
6489 && token->type == CPP_AND_AND)
6490 {
6491 tree identifier;
89966701 6492 tree expression;
ef0b0c72 6493 location_t loc = token->location;
0a3b29ad 6494
6495 /* Consume the '&&' token. */
6496 cp_lexer_consume_token (parser->lexer);
6497 /* Look for the identifier. */
6498 identifier = cp_parser_identifier (parser);
6499 /* Create an expression representing the address. */
dda49785 6500 expression = finish_label_address_expr (identifier, loc);
89966701 6501 if (cp_parser_non_integral_constant_expression (parser,
c247dce0 6502 NIC_ADDR_LABEL))
89966701 6503 expression = error_mark_node;
6504 return expression;
0a3b29ad 6505 }
6506 }
6507 if (unary_operator != ERROR_MARK)
6508 {
6509 tree cast_expression;
364c2f43 6510 tree expression = error_mark_node;
c61e1212 6511 non_integral_constant non_constant_p = NIC_NONE;
ef0b0c72 6512 location_t loc = token->location;
0a3b29ad 6513
6514 /* Consume the operator token. */
6515 token = cp_lexer_consume_token (parser->lexer);
6516 /* Parse the cast-expression. */
ccb84981 6517 cast_expression
9031d10b 6518 = cp_parser_cast_expression (parser,
640aa28c 6519 unary_operator == ADDR_EXPR,
98b326fd 6520 /*cast_p=*/false, pidk);
0a3b29ad 6521 /* Now, build an appropriate representation. */
6522 switch (unary_operator)
6523 {
6524 case INDIRECT_REF:
c247dce0 6525 non_constant_p = NIC_STAR;
ef0b0c72 6526 expression = build_x_indirect_ref (loc, cast_expression,
6527 RO_UNARY_STAR,
ebd21de4 6528 tf_warning_or_error);
364c2f43 6529 break;
6530
0a3b29ad 6531 case ADDR_EXPR:
c247dce0 6532 non_constant_p = NIC_ADDR;
364c2f43 6533 /* Fall through. */
13795292 6534 case BIT_NOT_EXPR:
ef0b0c72 6535 expression = build_x_unary_op (loc, unary_operator,
6536 cast_expression,
ebd21de4 6537 tf_warning_or_error);
364c2f43 6538 break;
6539
5f6526e1 6540 case PREINCREMENT_EXPR:
6541 case PREDECREMENT_EXPR:
c247dce0 6542 non_constant_p = unary_operator == PREINCREMENT_EXPR
6543 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5f6526e1 6544 /* Fall through. */
97d541d5 6545 case UNARY_PLUS_EXPR:
0a3b29ad 6546 case NEGATE_EXPR:
6547 case TRUTH_NOT_EXPR:
ef0b0c72 6548 expression = finish_unary_op_expr (loc, unary_operator,
6549 cast_expression);
364c2f43 6550 break;
0a3b29ad 6551
0a3b29ad 6552 default:
2e3e31d2 6553 gcc_unreachable ();
0a3b29ad 6554 }
364c2f43 6555
c61e1212 6556 if (non_constant_p != NIC_NONE
3938e0c2 6557 && cp_parser_non_integral_constant_expression (parser,
6558 non_constant_p))
6559 expression = error_mark_node;
364c2f43 6560
6561 return expression;
0a3b29ad 6562 }
6563
34da8800 6564 return cp_parser_postfix_expression (parser, address_p, cast_p,
98b326fd 6565 /*member_access_only_p=*/false,
6566 pidk);
0a3b29ad 6567}
6568
6569/* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6570 unary-operator, the corresponding tree code is returned. */
6571
6572static enum tree_code
45baea8b 6573cp_parser_unary_operator (cp_token* token)
0a3b29ad 6574{
6575 switch (token->type)
6576 {
6577 case CPP_MULT:
6578 return INDIRECT_REF;
6579
6580 case CPP_AND:
6581 return ADDR_EXPR;
6582
6583 case CPP_PLUS:
97d541d5 6584 return UNARY_PLUS_EXPR;
0a3b29ad 6585
6586 case CPP_MINUS:
6587 return NEGATE_EXPR;
6588
6589 case CPP_NOT:
6590 return TRUTH_NOT_EXPR;
ccb84981 6591
0a3b29ad 6592 case CPP_COMPL:
6593 return BIT_NOT_EXPR;
6594
6595 default:
6596 return ERROR_MARK;
6597 }
6598}
6599
6600/* Parse a new-expression.
6601
5c6faf71 6602 new-expression:
0a3b29ad 6603 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6604 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6605
6606 Returns a representation of the expression. */
6607
6608static tree
45baea8b 6609cp_parser_new_expression (cp_parser* parser)
0a3b29ad 6610{
6611 bool global_scope_p;
f352a3fb 6612 VEC(tree,gc) *placement;
0a3b29ad 6613 tree type;
f352a3fb 6614 VEC(tree,gc) *initializer;
3592befb 6615 tree nelts = NULL_TREE;
f352a3fb 6616 tree ret;
0a3b29ad 6617
6618 /* Look for the optional `::' operator. */
ccb84981 6619 global_scope_p
0a3b29ad 6620 = (cp_parser_global_scope_opt (parser,
130bb1d4 6621 /*current_scope_valid_p=*/false)
0a3b29ad 6622 != NULL_TREE);
6623 /* Look for the `new' operator. */
c247dce0 6624 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
0a3b29ad 6625 /* There's no easy way to tell a new-placement from the
6626 `( type-id )' construct. */
6627 cp_parser_parse_tentatively (parser);
6628 /* Look for a new-placement. */
6629 placement = cp_parser_new_placement (parser);
6630 /* If that didn't work out, there's no new-placement. */
6631 if (!cp_parser_parse_definitely (parser))
f352a3fb 6632 {
6633 if (placement != NULL)
6634 release_tree_vector (placement);
6635 placement = NULL;
6636 }
0a3b29ad 6637
6638 /* If the next token is a `(', then we have a parenthesized
6639 type-id. */
6640 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6641 {
ad9ae192 6642 cp_token *token;
73fcfc2c 6643 const char *saved_message = parser->type_definition_forbidden_message;
6644
0a3b29ad 6645 /* Consume the `('. */
6646 cp_lexer_consume_token (parser->lexer);
73fcfc2c 6647
0a3b29ad 6648 /* Parse the type-id. */
73fcfc2c 6649 parser->type_definition_forbidden_message
6650 = G_("types may not be defined in a new-expression");
0a3b29ad 6651 type = cp_parser_type_id (parser);
73fcfc2c 6652 parser->type_definition_forbidden_message = saved_message;
6653
0a3b29ad 6654 /* Look for the closing `)'. */
c247dce0 6655 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
ad9ae192 6656 token = cp_lexer_peek_token (parser->lexer);
207355ad 6657 /* There should not be a direct-new-declarator in this production,
653e5405 6658 but GCC used to allowed this, so we check and emit a sensible error
383da0a4 6659 message for this case. */
6660 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
f352cc1d 6661 {
ccb59bb6 6662 error_at (token->location,
6663 "array bound forbidden after parenthesized type-id");
5bcc316e 6664 inform (token->location,
6665 "try removing the parentheses around the type-id");
383da0a4 6666 cp_parser_direct_new_declarator (parser);
6667 }
0a3b29ad 6668 }
6669 /* Otherwise, there must be a new-type-id. */
6670 else
3046c0a3 6671 type = cp_parser_new_type_id (parser, &nelts);
0a3b29ad 6672
f82f1250 6673 /* If the next token is a `(' or '{', then we have a new-initializer. */
6674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6675 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 6676 initializer = cp_parser_new_initializer (parser);
6677 else
f352a3fb 6678 initializer = NULL;
0a3b29ad 6679
3938e0c2 6680 /* A new-expression may not appear in an integral constant
6681 expression. */
c247dce0 6682 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
f352a3fb 6683 ret = error_mark_node;
6684 else
6685 {
6686 /* Create a representation of the new-expression. */
6687 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6688 tf_warning_or_error);
6689 }
3938e0c2 6690
f352a3fb 6691 if (placement != NULL)
6692 release_tree_vector (placement);
6693 if (initializer != NULL)
6694 release_tree_vector (initializer);
6695
6696 return ret;
0a3b29ad 6697}
6698
6699/* Parse a new-placement.
6700
6701 new-placement:
6702 ( expression-list )
6703
6704 Returns the same representation as for an expression-list. */
6705
f352a3fb 6706static VEC(tree,gc) *
45baea8b 6707cp_parser_new_placement (cp_parser* parser)
0a3b29ad 6708{
f352a3fb 6709 VEC(tree,gc) *expression_list;
0a3b29ad 6710
0a3b29ad 6711 /* Parse the expression-list. */
ccb84981 6712 expression_list = (cp_parser_parenthesized_expression_list
33199a81 6713 (parser, non_attr, /*cast_p=*/false,
6714 /*allow_expansion_p=*/true,
640aa28c 6715 /*non_constant_p=*/NULL));
0a3b29ad 6716
6717 return expression_list;
6718}
6719
6720/* Parse a new-type-id.
6721
6722 new-type-id:
6723 type-specifier-seq new-declarator [opt]
6724
3046c0a3 6725 Returns the TYPE allocated. If the new-type-id indicates an array
6726 type, *NELTS is set to the number of elements in the last array
6727 bound; the TYPE will not include the last array bound. */
0a3b29ad 6728
6729static tree
3046c0a3 6730cp_parser_new_type_id (cp_parser* parser, tree *nelts)
0a3b29ad 6731{
4b9b2871 6732 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 6733 cp_declarator *new_declarator;
6734 cp_declarator *declarator;
6735 cp_declarator *outer_declarator;
0a3b29ad 6736 const char *saved_message;
6737
6738 /* The type-specifier sequence must not contain type definitions.
6739 (It cannot contain declarations of new types either, but if they
6740 are not definitions we will catch that because they are not
6741 complete.) */
6742 saved_message = parser->type_definition_forbidden_message;
6743 parser->type_definition_forbidden_message
ca82e026 6744 = G_("types may not be defined in a new-type-id");
0a3b29ad 6745 /* Parse the type-specifier-seq. */
c44f7faf 6746 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 6747 /*is_trailing_return=*/false,
6f74fe3c 6748 &type_specifier_seq);
0a3b29ad 6749 /* Restore the old message. */
6750 parser->type_definition_forbidden_message = saved_message;
3592befb 6751
6752 if (type_specifier_seq.type == error_mark_node)
6753 return error_mark_node;
6754
0a3b29ad 6755 /* Parse the new-declarator. */
3046c0a3 6756 new_declarator = cp_parser_new_declarator_opt (parser);
6757
6758 /* Determine the number of elements in the last array dimension, if
6759 any. */
6760 *nelts = NULL_TREE;
6761 /* Skip down to the last array dimension. */
6762 declarator = new_declarator;
6763 outer_declarator = NULL;
6764 while (declarator && (declarator->kind == cdk_pointer
6765 || declarator->kind == cdk_ptrmem))
6766 {
6767 outer_declarator = declarator;
6768 declarator = declarator->declarator;
6769 }
207355ad 6770 while (declarator
3046c0a3 6771 && declarator->kind == cdk_array
6772 && declarator->declarator
6773 && declarator->declarator->kind == cdk_array)
6774 {
6775 outer_declarator = declarator;
6776 declarator = declarator->declarator;
6777 }
207355ad 6778
3046c0a3 6779 if (declarator && declarator->kind == cdk_array)
6780 {
6781 *nelts = declarator->u.array.bounds;
6782 if (*nelts == error_mark_node)
6783 *nelts = integer_one_node;
9031d10b 6784
3046c0a3 6785 if (outer_declarator)
6786 outer_declarator->declarator = declarator->declarator;
6787 else
6788 new_declarator = NULL;
6789 }
0a3b29ad 6790
3592befb 6791 return groktypename (&type_specifier_seq, new_declarator, false);
0a3b29ad 6792}
6793
6794/* Parse an (optional) new-declarator.
6795
6796 new-declarator:
6797 ptr-operator new-declarator [opt]
6798 direct-new-declarator
6799
3046c0a3 6800 Returns the declarator. */
0a3b29ad 6801
3046c0a3 6802static cp_declarator *
45baea8b 6803cp_parser_new_declarator_opt (cp_parser* parser)
0a3b29ad 6804{
6805 enum tree_code code;
6806 tree type;
2cfb6cde 6807 cp_cv_quals cv_quals;
0a3b29ad 6808
6809 /* We don't know if there's a ptr-operator next, or not. */
6810 cp_parser_parse_tentatively (parser);
6811 /* Look for a ptr-operator. */
2cfb6cde 6812 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
0a3b29ad 6813 /* If that worked, look for more new-declarators. */
6814 if (cp_parser_parse_definitely (parser))
6815 {
3046c0a3 6816 cp_declarator *declarator;
0a3b29ad 6817
6818 /* Parse another optional declarator. */
6819 declarator = cp_parser_new_declarator_opt (parser);
6820
63949b38 6821 return cp_parser_make_indirect_declarator
6822 (code, type, cv_quals, declarator);
0a3b29ad 6823 }
6824
6825 /* If the next token is a `[', there is a direct-new-declarator. */
6826 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6827 return cp_parser_direct_new_declarator (parser);
6828
3046c0a3 6829 return NULL;
0a3b29ad 6830}
6831
6832/* Parse a direct-new-declarator.
6833
6834 direct-new-declarator:
6835 [ expression ]
ccb84981 6836 direct-new-declarator [constant-expression]
0a3b29ad 6837
3046c0a3 6838 */
0a3b29ad 6839
3046c0a3 6840static cp_declarator *
45baea8b 6841cp_parser_direct_new_declarator (cp_parser* parser)
0a3b29ad 6842{
3046c0a3 6843 cp_declarator *declarator = NULL;
0a3b29ad 6844
6845 while (true)
6846 {
6847 tree expression;
6848
6849 /* Look for the opening `['. */
c247dce0 6850 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
0a3b29ad 6851 /* The first expression is not required to be constant. */
6852 if (!declarator)
6853 {
ad9ae192 6854 cp_token *token = cp_lexer_peek_token (parser->lexer);
98b326fd 6855 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 6856 /* The standard requires that the expression have integral
6857 type. DR 74 adds enumeration types. We believe that the
6858 real intent is that these expressions be handled like the
6859 expression in a `switch' condition, which also allows
6860 classes with a single conversion to integral or
6861 enumeration type. */
6862 if (!processing_template_decl)
6863 {
ccb84981 6864 expression
0a3b29ad 6865 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6866 expression,
35771a9a 6867 /*complain=*/true);
0a3b29ad 6868 if (!expression)
6869 {
ccb59bb6 6870 error_at (token->location,
6871 "expression in new-declarator must have integral "
6872 "or enumeration type");
0a3b29ad 6873 expression = error_mark_node;
6874 }
6875 }
6876 }
6877 /* But all the other expressions must be. */
6878 else
ccb84981 6879 expression
6880 = cp_parser_constant_expression (parser,
5f6526e1 6881 /*allow_non_constant=*/false,
6882 NULL);
0a3b29ad 6883 /* Look for the closing `]'. */
c247dce0 6884 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 6885
6886 /* Add this bound to the declarator. */
3046c0a3 6887 declarator = make_array_declarator (declarator, expression);
0a3b29ad 6888
6889 /* If the next token is not a `[', then there are no more
6890 bounds. */
6891 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6892 break;
6893 }
6894
6895 return declarator;
6896}
6897
6898/* Parse a new-initializer.
6899
6900 new-initializer:
6901 ( expression-list [opt] )
f82f1250 6902 braced-init-list
0a3b29ad 6903
f352a3fb 6904 Returns a representation of the expression-list. */
0a3b29ad 6905
f352a3fb 6906static VEC(tree,gc) *
45baea8b 6907cp_parser_new_initializer (cp_parser* parser)
0a3b29ad 6908{
f352a3fb 6909 VEC(tree,gc) *expression_list;
0a3b29ad 6910
f82f1250 6911 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6912 {
f352a3fb 6913 tree t;
f82f1250 6914 bool expr_non_constant_p;
bf8d19fe 6915 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f352a3fb 6916 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6917 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6918 expression_list = make_tree_vector_single (t);
f82f1250 6919 }
6920 else
6921 expression_list = (cp_parser_parenthesized_expression_list
33199a81 6922 (parser, non_attr, /*cast_p=*/false,
6923 /*allow_expansion_p=*/true,
f82f1250 6924 /*non_constant_p=*/NULL));
0a3b29ad 6925
6926 return expression_list;
6927}
6928
6929/* Parse a delete-expression.
6930
6931 delete-expression:
6932 :: [opt] delete cast-expression
6933 :: [opt] delete [ ] cast-expression
6934
6935 Returns a representation of the expression. */
6936
6937static tree
45baea8b 6938cp_parser_delete_expression (cp_parser* parser)
0a3b29ad 6939{
6940 bool global_scope_p;
6941 bool array_p;
6942 tree expression;
6943
6944 /* Look for the optional `::' operator. */
6945 global_scope_p
6946 = (cp_parser_global_scope_opt (parser,
130bb1d4 6947 /*current_scope_valid_p=*/false)
0a3b29ad 6948 != NULL_TREE);
6949 /* Look for the `delete' keyword. */
c247dce0 6950 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
0a3b29ad 6951 /* See if the array syntax is in use. */
6952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6953 {
6954 /* Consume the `[' token. */
6955 cp_lexer_consume_token (parser->lexer);
6956 /* Look for the `]' token. */
c247dce0 6957 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 6958 /* Remember that this is the `[]' construct. */
6959 array_p = true;
6960 }
6961 else
6962 array_p = false;
6963
6964 /* Parse the cast-expression. */
a63bc44c 6965 expression = cp_parser_simple_cast_expression (parser);
0a3b29ad 6966
3938e0c2 6967 /* A delete-expression may not appear in an integral constant
6968 expression. */
c247dce0 6969 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
3938e0c2 6970 return error_mark_node;
6971
9e505437 6972 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6973 tf_warning_or_error);
0a3b29ad 6974}
6975
589d356c 6976/* Returns true if TOKEN may start a cast-expression and false
6977 otherwise. */
6978
6979static bool
6980cp_parser_token_starts_cast_expression (cp_token *token)
6981{
6982 switch (token->type)
6983 {
6984 case CPP_COMMA:
6985 case CPP_SEMICOLON:
6986 case CPP_QUERY:
6987 case CPP_COLON:
6988 case CPP_CLOSE_SQUARE:
6989 case CPP_CLOSE_PAREN:
6990 case CPP_CLOSE_BRACE:
6991 case CPP_DOT:
6992 case CPP_DOT_STAR:
6993 case CPP_DEREF:
6994 case CPP_DEREF_STAR:
6995 case CPP_DIV:
6996 case CPP_MOD:
6997 case CPP_LSHIFT:
6998 case CPP_RSHIFT:
6999 case CPP_LESS:
7000 case CPP_GREATER:
7001 case CPP_LESS_EQ:
7002 case CPP_GREATER_EQ:
7003 case CPP_EQ_EQ:
7004 case CPP_NOT_EQ:
7005 case CPP_EQ:
7006 case CPP_MULT_EQ:
7007 case CPP_DIV_EQ:
7008 case CPP_MOD_EQ:
7009 case CPP_PLUS_EQ:
7010 case CPP_MINUS_EQ:
7011 case CPP_RSHIFT_EQ:
7012 case CPP_LSHIFT_EQ:
7013 case CPP_AND_EQ:
7014 case CPP_XOR_EQ:
7015 case CPP_OR_EQ:
7016 case CPP_XOR:
7017 case CPP_OR:
7018 case CPP_OR_OR:
c19d7ab2 7019 case CPP_EOF:
589d356c 7020 return false;
7021
79408174 7022 /* '[' may start a primary-expression in obj-c++. */
589d356c 7023 case CPP_OPEN_SQUARE:
79408174 7024 return c_dialect_objc ();
589d356c 7025
7026 default:
7027 return true;
7028 }
7029}
7030
0a3b29ad 7031/* Parse a cast-expression.
7032
7033 cast-expression:
7034 unary-expression
7035 ( type-id ) cast-expression
7036
640aa28c 7037 ADDRESS_P is true iff the unary-expression is appearing as the
7038 operand of the `&' operator. CAST_P is true if this expression is
7039 the target of a cast.
7040
0a3b29ad 7041 Returns a representation of the expression. */
7042
7043static tree
98b326fd 7044cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7045 cp_id_kind * pidk)
0a3b29ad 7046{
7047 /* If it's a `(', then we might be looking at a cast. */
7048 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7049 {
7050 tree type = NULL_TREE;
7051 tree expr = NULL_TREE;
7052 bool compound_literal_p;
7053 const char *saved_message;
7054
7055 /* There's no way to know yet whether or not this is a cast.
7056 For example, `(int (3))' is a unary-expression, while `(int)
7057 3' is a cast. So, we resort to parsing tentatively. */
7058 cp_parser_parse_tentatively (parser);
7059 /* Types may not be defined in a cast. */
7060 saved_message = parser->type_definition_forbidden_message;
7061 parser->type_definition_forbidden_message
ca82e026 7062 = G_("types may not be defined in casts");
0a3b29ad 7063 /* Consume the `('. */
7064 cp_lexer_consume_token (parser->lexer);
7065 /* A very tricky bit is that `(struct S) { 3 }' is a
7066 compound-literal (which we permit in C++ as an extension).
7067 But, that construct is not a cast-expression -- it is a
7068 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7069 is legal; if the compound-literal were a cast-expression,
7070 you'd need an extra set of parentheses.) But, if we parse
7071 the type-id, and it happens to be a class-specifier, then we
7072 will commit to the parse at that point, because we cannot
7073 undo the action that is done when creating a new class. So,
ccb84981 7074 then we cannot back up and do a postfix-expression.
0a3b29ad 7075
7076 Therefore, we scan ahead to the closing `)', and check to see
7077 if the token after the `)' is a `{'. If so, we are not
ccb84981 7078 looking at a cast-expression.
0a3b29ad 7079
7080 Save tokens so that we can put them back. */
7081 cp_lexer_save_tokens (parser->lexer);
7082 /* Skip tokens until the next token is a closing parenthesis.
7083 If we find the closing `)', and the next token is a `{', then
7084 we are looking at a compound-literal. */
ccb84981 7085 compound_literal_p
3d0f901b 7086 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7087 /*consume_paren=*/true)
0a3b29ad 7088 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7089 /* Roll back the tokens we skipped. */
7090 cp_lexer_rollback_tokens (parser->lexer);
7091 /* If we were looking at a compound-literal, simulate an error
7092 so that the call to cp_parser_parse_definitely below will
7093 fail. */
7094 if (compound_literal_p)
7095 cp_parser_simulate_error (parser);
7096 else
7097 {
41f2d08e 7098 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7099 parser->in_type_id_in_expr_p = true;
0a3b29ad 7100 /* Look for the type-id. */
7101 type = cp_parser_type_id (parser);
7102 /* Look for the closing `)'. */
c247dce0 7103 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
41f2d08e 7104 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 7105 }
7106
7107 /* Restore the saved message. */
7108 parser->type_definition_forbidden_message = saved_message;
7109
589d356c 7110 /* At this point this can only be either a cast or a
7111 parenthesized ctor such as `(T ())' that looks like a cast to
7112 function returning T. */
7113 if (!cp_parser_error_occurred (parser)
7114 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7115 (parser->lexer)))
0a3b29ad 7116 {
589d356c 7117 cp_parser_parse_definitely (parser);
7118 expr = cp_parser_cast_expression (parser,
7119 /*address_p=*/false,
98b326fd 7120 /*cast_p=*/true, pidk);
589d356c 7121
0a3b29ad 7122 /* Warn about old-style casts, if so requested. */
ccb84981 7123 if (warn_old_style_cast
7124 && !in_system_header
7125 && !VOID_TYPE_P (type)
0a3b29ad 7126 && current_lang_name != lang_name_c)
ced7c954 7127 warning (OPT_Wold_style_cast, "use of old-style cast");
5f6526e1 7128
7129 /* Only type conversions to integral or enumeration types
7130 can be used in constant-expressions. */
bde9ebf7 7131 if (!cast_valid_in_integral_constant_expression_p (type)
c61e1212 7132 && cp_parser_non_integral_constant_expression (parser,
7133 NIC_CAST))
3938e0c2 7134 return error_mark_node;
7135
0a3b29ad 7136 /* Perform the cast. */
e60a6f7b 7137 expr = build_c_cast (input_location, type, expr);
d622b3bd 7138 return expr;
0a3b29ad 7139 }
589d356c 7140 else
7141 cp_parser_abort_tentative_parse (parser);
0a3b29ad 7142 }
7143
7144 /* If we get here, then it's not a cast, so it must be a
7145 unary-expression. */
98b326fd 7146 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
0a3b29ad 7147}
7148
0a88af73 7149/* Parse a binary expression of the general form:
0a3b29ad 7150
7151 pm-expression:
7152 cast-expression
7153 pm-expression .* cast-expression
7154 pm-expression ->* cast-expression
7155
e24657db 7156 multiplicative-expression:
0a3b29ad 7157 pm-expression
7158 multiplicative-expression * pm-expression
7159 multiplicative-expression / pm-expression
7160 multiplicative-expression % pm-expression
7161
0a3b29ad 7162 additive-expression:
7163 multiplicative-expression
7164 additive-expression + multiplicative-expression
7165 additive-expression - multiplicative-expression
7166
0a3b29ad 7167 shift-expression:
7168 additive-expression
7169 shift-expression << additive-expression
7170 shift-expression >> additive-expression
7171
0a3b29ad 7172 relational-expression:
7173 shift-expression
7174 relational-expression < shift-expression
7175 relational-expression > shift-expression
7176 relational-expression <= shift-expression
7177 relational-expression >= shift-expression
7178
0a88af73 7179 GNU Extension:
9031d10b 7180
0a3b29ad 7181 relational-expression:
7182 relational-expression <? shift-expression
7183 relational-expression >? shift-expression
7184
0a3b29ad 7185 equality-expression:
7186 relational-expression
7187 equality-expression == relational-expression
7188 equality-expression != relational-expression
7189
0a3b29ad 7190 and-expression:
7191 equality-expression
7192 and-expression & equality-expression
7193
0a3b29ad 7194 exclusive-or-expression:
7195 and-expression
7196 exclusive-or-expression ^ and-expression
7197
0a88af73 7198 inclusive-or-expression:
7199 exclusive-or-expression
7200 inclusive-or-expression | exclusive-or-expression
0a3b29ad 7201
0a88af73 7202 logical-and-expression:
7203 inclusive-or-expression
7204 logical-and-expression && inclusive-or-expression
0a3b29ad 7205
0a88af73 7206 logical-or-expression:
7207 logical-and-expression
7208 logical-or-expression || logical-and-expression
0a3b29ad 7209
0a88af73 7210 All these are implemented with a single function like:
0a3b29ad 7211
0a88af73 7212 binary-expression:
7213 simple-cast-expression
7214 binary-expression <token> binary-expression
0a3b29ad 7215
640aa28c 7216 CAST_P is true if this expression is the target of a cast.
7217
0a88af73 7218 The binops_by_token map is used to get the tree codes for each <token> type.
7219 binary-expressions are associated according to a precedence table. */
0a3b29ad 7220
6dcdb5de 7221#define TOKEN_PRECEDENCE(token) \
7222(((token->type == CPP_GREATER \
7223 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7224 && !parser->greater_than_is_operator_p) \
7225 ? PREC_NOT_OPERATOR \
56471494 7226 : binops_by_token[token->type].prec)
0a3b29ad 7227
7228static tree
fd6481cf 7229cp_parser_binary_expression (cp_parser* parser, bool cast_p,
4390875c 7230 bool no_toplevel_fold_p,
98b326fd 7231 enum cp_parser_prec prec,
7232 cp_id_kind * pidk)
0a3b29ad 7233{
0a88af73 7234 cp_parser_expression_stack stack;
7235 cp_parser_expression_stack_entry *sp = &stack[0];
1e6d0c16 7236 cp_parser_expression_stack_entry current;
7237 tree rhs;
0a88af73 7238 cp_token *token;
1e6d0c16 7239 enum tree_code rhs_type;
fd6481cf 7240 enum cp_parser_prec new_prec, lookahead_prec;
9951fe5d 7241 tree overload;
0a3b29ad 7242
0a88af73 7243 /* Parse the first expression. */
1e6d0c16 7244 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
7245 cast_p, pidk);
7246 current.lhs_type = ERROR_MARK;
7247 current.prec = prec;
0a3b29ad 7248
0a88af73 7249 for (;;)
7250 {
7251 /* Get an operator token. */
7252 token = cp_lexer_peek_token (parser->lexer);
d50879bc 7253
56471494 7254 if (warn_cxx0x_compat
7255 && token->type == CPP_RSHIFT
7256 && !parser->greater_than_is_operator_p)
7257 {
1e6d0c16 7258 if (warning_at (token->location, OPT_Wc__0x_compat,
7259 "%<>>%> operator is treated"
ef0b0c72 7260 " as two right angle brackets in C++11"))
1e6d0c16 7261 inform (token->location,
7262 "suggest parentheses around %<>>%> expression");
56471494 7263 }
7264
0a88af73 7265 new_prec = TOKEN_PRECEDENCE (token);
7266
7267 /* Popping an entry off the stack means we completed a subexpression:
653e5405 7268 - either we found a token which is not an operator (`>' where it is not
7269 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7270 will happen repeatedly;
7271 - or, we found an operator which has lower priority. This is the case
7272 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7273 parsing `3 * 4'. */
1e6d0c16 7274 if (new_prec <= current.prec)
653e5405 7275 {
7276 if (sp == stack)
0a88af73 7277 break;
653e5405 7278 else
0a88af73 7279 goto pop;
653e5405 7280 }
0a3b29ad 7281
0a88af73 7282 get_rhs:
1e6d0c16 7283 current.tree_type = binops_by_token[token->type].tree_type;
7284 current.loc = token->location;
0a3b29ad 7285
93523877 7286 /* We used the operator token. */
0a88af73 7287 cp_lexer_consume_token (parser->lexer);
0a3b29ad 7288
6dd536b9 7289 /* For "false && x" or "true || x", x will never be executed;
7290 disable warnings while evaluating it. */
1e6d0c16 7291 if (current.tree_type == TRUTH_ANDIF_EXPR)
7292 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
7293 else if (current.tree_type == TRUTH_ORIF_EXPR)
7294 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
6dd536b9 7295
0a88af73 7296 /* Extract another operand. It may be the RHS of this expression
653e5405 7297 or the LHS of a new, higher priority expression. */
0a88af73 7298 rhs = cp_parser_simple_cast_expression (parser);
e534436e 7299 rhs_type = ERROR_MARK;
0a3b29ad 7300
0a88af73 7301 /* Get another operator token. Look up its precedence to avoid
653e5405 7302 building a useless (immediately popped) stack entry for common
7303 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
0a88af73 7304 token = cp_lexer_peek_token (parser->lexer);
7305 lookahead_prec = TOKEN_PRECEDENCE (token);
7306 if (lookahead_prec > new_prec)
653e5405 7307 {
7308 /* ... and prepare to parse the RHS of the new, higher priority
7309 expression. Since precedence levels on the stack are
9802eea0 7310 monotonically increasing, we do not have to care about
7311 stack overflows. */
1e6d0c16 7312 *sp = current;
7313 ++sp;
7314 current.lhs = rhs;
7315 current.lhs_type = rhs_type;
7316 current.prec = new_prec;
653e5405 7317 new_prec = lookahead_prec;
7318 goto get_rhs;
7319
7320 pop:
4390875c 7321 lookahead_prec = new_prec;
653e5405 7322 /* If the stack is not empty, we have parsed into LHS the right side
0a88af73 7323 (`4' in the example above) of an expression we had suspended.
9031d10b 7324 We can use the information on the stack to recover the LHS (`3')
0a88af73 7325 from the stack together with the tree code (`MULT_EXPR'), and
7326 the precedence of the higher level subexpression
7327 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7328 which will be used to actually build the additive expression. */
1e6d0c16 7329 rhs = current.lhs;
7330 rhs_type = current.lhs_type;
653e5405 7331 --sp;
1e6d0c16 7332 current = *sp;
653e5405 7333 }
0a3b29ad 7334
6dd536b9 7335 /* Undo the disabling of warnings done above. */
1e6d0c16 7336 if (current.tree_type == TRUTH_ANDIF_EXPR)
7337 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
7338 else if (current.tree_type == TRUTH_ORIF_EXPR)
7339 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
6dd536b9 7340
9951fe5d 7341 overload = NULL;
82012ffe 7342 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7343 ERROR_MARK for everything that is not a binary expression.
7344 This makes warn_about_parentheses miss some warnings that
7345 involve unary operators. For unary expressions we should
7346 pass the correct tree_code unless the unary expression was
7347 surrounded by parentheses.
7348 */
4390875c 7349 if (no_toplevel_fold_p
1e6d0c16 7350 && lookahead_prec <= current.prec
4390875c 7351 && sp == stack
1e6d0c16 7352 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison)
7353 current.lhs = build2 (current.tree_type, boolean_type_node,
7354 current.lhs, rhs);
4390875c 7355 else
1e6d0c16 7356 current.lhs = build_x_binary_op (current.loc, current.tree_type,
7357 current.lhs, current.lhs_type,
7358 rhs, rhs_type, &overload,
7359 tf_warning_or_error);
7360 current.lhs_type = current.tree_type;
0a3b29ad 7361
0a88af73 7362 /* If the binary operator required the use of an overloaded operator,
653e5405 7363 then this expression cannot be an integral constant-expression.
7364 An overloaded operator can be used even if both operands are
7365 otherwise permissible in an integral constant-expression if at
7366 least one of the operands is of enumeration type. */
0a3b29ad 7367
9951fe5d 7368 if (overload
c61e1212 7369 && cp_parser_non_integral_constant_expression (parser,
7370 NIC_OVERLOADED))
653e5405 7371 return error_mark_node;
0a88af73 7372 }
0a3b29ad 7373
1e6d0c16 7374 return current.lhs;
0a3b29ad 7375}
7376
0a88af73 7377
0a3b29ad 7378/* Parse the `? expression : assignment-expression' part of a
7379 conditional-expression. The LOGICAL_OR_EXPR is the
7380 logical-or-expression that started the conditional-expression.
7381 Returns a representation of the entire conditional-expression.
7382
878870b4 7383 This routine is used by cp_parser_assignment_expression.
0a3b29ad 7384
7385 ? expression : assignment-expression
ccb84981 7386
0a3b29ad 7387 GNU Extensions:
ccb84981 7388
0a3b29ad 7389 ? : assignment-expression */
7390
7391static tree
45baea8b 7392cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
0a3b29ad 7393{
7394 tree expr;
7395 tree assignment_expr;
b9bdfa0b 7396 struct cp_token *token;
255b5d15 7397 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 7398
7399 /* Consume the `?' token. */
7400 cp_lexer_consume_token (parser->lexer);
b9bdfa0b 7401 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 7402 if (cp_parser_allow_gnu_extensions_p (parser)
b9bdfa0b 7403 && token->type == CPP_COLON)
48d94ede 7404 {
29438999 7405 pedwarn (token->location, OPT_Wpedantic,
b9bdfa0b 7406 "ISO C++ does not allow ?: with omitted middle operand");
48d94ede 7407 /* Implicit true clause. */
7408 expr = NULL_TREE;
7409 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
b9bdfa0b 7410 warn_for_omitted_condop (token->location, logical_or_expr);
48d94ede 7411 }
0a3b29ad 7412 else
48d94ede 7413 {
1e838f42 7414 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7415 parser->colon_corrects_to_scope_p = false;
48d94ede 7416 /* Parse the expression. */
7417 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7418 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7419 c_inhibit_evaluation_warnings +=
7420 ((logical_or_expr == truthvalue_true_node)
7421 - (logical_or_expr == truthvalue_false_node));
1e838f42 7422 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
48d94ede 7423 }
ccb84981 7424
0a3b29ad 7425 /* The next token should be a `:'. */
c247dce0 7426 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 7427 /* Parse the assignment-expression. */
98b326fd 7428 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
48d94ede 7429 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
0a3b29ad 7430
7431 /* Build the conditional-expression. */
255b5d15 7432 return build_x_conditional_expr (loc, logical_or_expr,
0a3b29ad 7433 expr,
ebd21de4 7434 assignment_expr,
7435 tf_warning_or_error);
0a3b29ad 7436}
7437
7438/* Parse an assignment-expression.
7439
7440 assignment-expression:
7441 conditional-expression
7442 logical-or-expression assignment-operator assignment_expression
7443 throw-expression
7444
640aa28c 7445 CAST_P is true if this expression is the target of a cast.
7446
0a3b29ad 7447 Returns a representation for the expression. */
7448
7449static tree
98b326fd 7450cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7451 cp_id_kind * pidk)
0a3b29ad 7452{
7453 tree expr;
7454
7455 /* If the next token is the `throw' keyword, then we're looking at
7456 a throw-expression. */
7457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7458 expr = cp_parser_throw_expression (parser);
7459 /* Otherwise, it must be that we are looking at a
7460 logical-or-expression. */
7461 else
7462 {
0a88af73 7463 /* Parse the binary expressions (logical-or-expression). */
4390875c 7464 expr = cp_parser_binary_expression (parser, cast_p, false,
7465 PREC_NOT_OPERATOR, pidk);
0a3b29ad 7466 /* If the next token is a `?' then we're actually looking at a
7467 conditional-expression. */
7468 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7469 return cp_parser_question_colon_clause (parser, expr);
ccb84981 7470 else
0a3b29ad 7471 {
255b5d15 7472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 7473
7474 /* If it's an assignment-operator, we're using the second
7475 production. */
255b5d15 7476 enum tree_code assignment_operator
0a3b29ad 7477 = cp_parser_assignment_operator_opt (parser);
7478 if (assignment_operator != ERROR_MARK)
7479 {
f82f1250 7480 bool non_constant_p;
0a3b29ad 7481
7482 /* Parse the right-hand side of the assignment. */
f82f1250 7483 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7484
7485 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
bf8d19fe 7486 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 7487
5f6526e1 7488 /* An assignment may not appear in a
7489 constant-expression. */
3938e0c2 7490 if (cp_parser_non_integral_constant_expression (parser,
c247dce0 7491 NIC_ASSIGNMENT))
3938e0c2 7492 return error_mark_node;
755edffd 7493 /* Build the assignment expression. */
255b5d15 7494 expr = build_x_modify_expr (loc, expr,
ccb84981 7495 assignment_operator,
ebd21de4 7496 rhs,
7497 tf_warning_or_error);
0a3b29ad 7498 }
7499 }
7500 }
7501
7502 return expr;
7503}
7504
7505/* Parse an (optional) assignment-operator.
7506
ccb84981 7507 assignment-operator: one of
7508 = *= /= %= += -= >>= <<= &= ^= |=
0a3b29ad 7509
7510 GNU Extension:
ccb84981 7511
0a3b29ad 7512 assignment-operator: one of
7513 <?= >?=
7514
7515 If the next token is an assignment operator, the corresponding tree
7516 code is returned, and the token is consumed. For example, for
7517 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7518 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7519 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7520 operator, ERROR_MARK is returned. */
7521
7522static enum tree_code
45baea8b 7523cp_parser_assignment_operator_opt (cp_parser* parser)
0a3b29ad 7524{
7525 enum tree_code op;
7526 cp_token *token;
7527
08cc44e7 7528 /* Peek at the next token. */
0a3b29ad 7529 token = cp_lexer_peek_token (parser->lexer);
7530
7531 switch (token->type)
7532 {
7533 case CPP_EQ:
7534 op = NOP_EXPR;
7535 break;
7536
7537 case CPP_MULT_EQ:
7538 op = MULT_EXPR;
7539 break;
7540
7541 case CPP_DIV_EQ:
7542 op = TRUNC_DIV_EXPR;
7543 break;
7544
7545 case CPP_MOD_EQ:
7546 op = TRUNC_MOD_EXPR;
7547 break;
7548
7549 case CPP_PLUS_EQ:
7550 op = PLUS_EXPR;
7551 break;
7552
7553 case CPP_MINUS_EQ:
7554 op = MINUS_EXPR;
7555 break;
7556
7557 case CPP_RSHIFT_EQ:
7558 op = RSHIFT_EXPR;
7559 break;
7560
7561 case CPP_LSHIFT_EQ:
7562 op = LSHIFT_EXPR;
7563 break;
7564
7565 case CPP_AND_EQ:
7566 op = BIT_AND_EXPR;
7567 break;
7568
7569 case CPP_XOR_EQ:
7570 op = BIT_XOR_EXPR;
7571 break;
7572
7573 case CPP_OR_EQ:
7574 op = BIT_IOR_EXPR;
7575 break;
7576
ccb84981 7577 default:
0a3b29ad 7578 /* Nothing else is an assignment operator. */
7579 op = ERROR_MARK;
7580 }
7581
7582 /* If it was an assignment operator, consume it. */
7583 if (op != ERROR_MARK)
7584 cp_lexer_consume_token (parser->lexer);
7585
7586 return op;
7587}
7588
7589/* Parse an expression.
7590
7591 expression:
7592 assignment-expression
7593 expression , assignment-expression
7594
640aa28c 7595 CAST_P is true if this expression is the target of a cast.
7596
0a3b29ad 7597 Returns a representation of the expression. */
7598
7599static tree
98b326fd 7600cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
0a3b29ad 7601{
7602 tree expression = NULL_TREE;
255b5d15 7603 location_t loc = UNKNOWN_LOCATION;
0a3b29ad 7604
7605 while (true)
7606 {
7607 tree assignment_expression;
7608
7609 /* Parse the next assignment-expression. */
ccb84981 7610 assignment_expression
98b326fd 7611 = cp_parser_assignment_expression (parser, cast_p, pidk);
0a3b29ad 7612 /* If this is the first assignment-expression, we can just
7613 save it away. */
7614 if (!expression)
7615 expression = assignment_expression;
0a3b29ad 7616 else
255b5d15 7617 expression = build_x_compound_expr (loc, expression,
ebd21de4 7618 assignment_expression,
7619 tf_warning_or_error);
0a3b29ad 7620 /* If the next token is not a comma, then we are done with the
7621 expression. */
7622 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7623 break;
7624 /* Consume the `,'. */
255b5d15 7625 loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 7626 cp_lexer_consume_token (parser->lexer);
5f6526e1 7627 /* A comma operator cannot appear in a constant-expression. */
c247dce0 7628 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
3938e0c2 7629 expression = error_mark_node;
5f6526e1 7630 }
0a3b29ad 7631
7632 return expression;
7633}
7634
ccb84981 7635/* Parse a constant-expression.
0a3b29ad 7636
7637 constant-expression:
ccb84981 7638 conditional-expression
5f6526e1 7639
7640 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
13795292 7641 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7642 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7643 is false, NON_CONSTANT_P should be NULL. */
0a3b29ad 7644
7645static tree
ccb84981 7646cp_parser_constant_expression (cp_parser* parser,
5f6526e1 7647 bool allow_non_constant_p,
7648 bool *non_constant_p)
0a3b29ad 7649{
f47c1747 7650 bool saved_integral_constant_expression_p;
7651 bool saved_allow_non_integral_constant_expression_p;
7652 bool saved_non_integral_constant_expression_p;
0a3b29ad 7653 tree expression;
7654
7655 /* It might seem that we could simply parse the
7656 conditional-expression, and then check to see if it were
7657 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7658 one that the compiler can figure out is constant, possibly after
7659 doing some simplifications or optimizations. The standard has a
7660 precise definition of constant-expression, and we must honor
7661 that, even though it is somewhat more restrictive.
7662
7663 For example:
7664
7665 int i[(2, 3)];
7666
7667 is not a legal declaration, because `(2, 3)' is not a
7668 constant-expression. The `,' operator is forbidden in a
7669 constant-expression. However, GCC's constant-folding machinery
7670 will fold this operation to an INTEGER_CST for `3'. */
7671
5f6526e1 7672 /* Save the old settings. */
f47c1747 7673 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
ccb84981 7674 saved_allow_non_integral_constant_expression_p
f47c1747 7675 = parser->allow_non_integral_constant_expression_p;
7676 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
0a3b29ad 7677 /* We are now parsing a constant-expression. */
f47c1747 7678 parser->integral_constant_expression_p = true;
ce984e5e 7679 parser->allow_non_integral_constant_expression_p
7680 = (allow_non_constant_p || cxx_dialect >= cxx0x);
f47c1747 7681 parser->non_integral_constant_expression_p = false;
878870b4 7682 /* Although the grammar says "conditional-expression", we parse an
7683 "assignment-expression", which also permits "throw-expression"
7684 and the use of assignment operators. In the case that
7685 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7686 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7687 actually essential that we look for an assignment-expression.
7688 For example, cp_parser_initializer_clauses uses this function to
7689 determine whether a particular assignment-expression is in fact
7690 constant. */
98b326fd 7691 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
5f6526e1 7692 /* Restore the old settings. */
9031d10b 7693 parser->integral_constant_expression_p
640aa28c 7694 = saved_integral_constant_expression_p;
ccb84981 7695 parser->allow_non_integral_constant_expression_p
f47c1747 7696 = saved_allow_non_integral_constant_expression_p;
cfa61f84 7697 if (cxx_dialect >= cxx0x)
7698 {
7699 /* Require an rvalue constant expression here; that's what our
7700 callers expect. Reference constant expressions are handled
7701 separately in e.g. cp_parser_template_argument. */
7702 bool is_const = potential_rvalue_constant_expression (expression);
7703 parser->non_integral_constant_expression_p = !is_const;
7704 if (!is_const && !allow_non_constant_p)
7705 require_potential_rvalue_constant_expression (expression);
7706 }
5f6526e1 7707 if (allow_non_constant_p)
f47c1747 7708 *non_constant_p = parser->non_integral_constant_expression_p;
9031d10b 7709 parser->non_integral_constant_expression_p
640aa28c 7710 = saved_non_integral_constant_expression_p;
0a3b29ad 7711
7712 return expression;
7713}
7714
43bf5d72 7715/* Parse __builtin_offsetof.
7716
7717 offsetof-expression:
7718 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7719
7720 offsetof-member-designator:
7721 id-expression
7722 | offsetof-member-designator "." id-expression
f0d0d842 7723 | offsetof-member-designator "[" expression "]"
7724 | offsetof-member-designator "->" id-expression */
43bf5d72 7725
7726static tree
7727cp_parser_builtin_offsetof (cp_parser *parser)
7728{
7729 int save_ice_p, save_non_ice_p;
7730 tree type, expr;
7731 cp_id_kind dummy;
ad9ae192 7732 cp_token *token;
43bf5d72 7733
7734 /* We're about to accept non-integral-constant things, but will
7735 definitely yield an integral constant expression. Save and
7736 restore these values around our local parsing. */
7737 save_ice_p = parser->integral_constant_expression_p;
7738 save_non_ice_p = parser->non_integral_constant_expression_p;
7739
7740 /* Consume the "__builtin_offsetof" token. */
7741 cp_lexer_consume_token (parser->lexer);
7742 /* Consume the opening `('. */
c247dce0 7743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
43bf5d72 7744 /* Parse the type-id. */
7745 type = cp_parser_type_id (parser);
7746 /* Look for the `,'. */
c247dce0 7747 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
ad9ae192 7748 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 7749
7750 /* Build the (type *)null that begins the traditional offsetof macro. */
ebd21de4 7751 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7752 tf_warning_or_error);
43bf5d72 7753
7754 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7755 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
ad9ae192 7756 true, &dummy, token->location);
43bf5d72 7757 while (true)
7758 {
ad9ae192 7759 token = cp_lexer_peek_token (parser->lexer);
43bf5d72 7760 switch (token->type)
7761 {
7762 case CPP_OPEN_SQUARE:
7763 /* offsetof-member-designator "[" expression "]" */
7764 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7765 break;
7766
f0d0d842 7767 case CPP_DEREF:
7768 /* offsetof-member-designator "->" identifier */
ef0b0c72 7769 expr = grok_array_decl (token->location, expr, integer_zero_node);
f0d0d842 7770 /* FALLTHRU */
7771
43bf5d72 7772 case CPP_DOT:
7773 /* offsetof-member-designator "." identifier */
7774 cp_lexer_consume_token (parser->lexer);
f0d0d842 7775 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7776 expr, true, &dummy,
ad9ae192 7777 token->location);
43bf5d72 7778 break;
7779
7780 case CPP_CLOSE_PAREN:
7781 /* Consume the ")" token. */
7782 cp_lexer_consume_token (parser->lexer);
7783 goto success;
7784
7785 default:
7786 /* Error. We know the following require will fail, but
7787 that gives the proper error message. */
c247dce0 7788 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
43bf5d72 7789 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7790 expr = error_mark_node;
7791 goto failure;
7792 }
7793 }
7794
7795 success:
11106b1c 7796 /* If we're processing a template, we can't finish the semantics yet.
7797 Otherwise we can fold the entire expression now. */
7798 if (processing_template_decl)
7799 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7800 else
bf75f33a 7801 expr = finish_offsetof (expr);
43bf5d72 7802
7803 failure:
7804 parser->integral_constant_expression_p = save_ice_p;
7805 parser->non_integral_constant_expression_p = save_non_ice_p;
7806
7807 return expr;
7808}
7809
8de5c43e 7810/* Parse a trait expression.
7811
7812 Returns a representation of the expression, the underlying type
7813 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
481451eb 7814
7815static tree
7816cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7817{
7818 cp_trait_kind kind;
7819 tree type1, type2 = NULL_TREE;
7820 bool binary = false;
7821 cp_decl_specifier_seq decl_specs;
7822
7823 switch (keyword)
7824 {
7825 case RID_HAS_NOTHROW_ASSIGN:
7826 kind = CPTK_HAS_NOTHROW_ASSIGN;
7827 break;
7828 case RID_HAS_NOTHROW_CONSTRUCTOR:
7829 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7830 break;
7831 case RID_HAS_NOTHROW_COPY:
7832 kind = CPTK_HAS_NOTHROW_COPY;
7833 break;
7834 case RID_HAS_TRIVIAL_ASSIGN:
7835 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7836 break;
7837 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7838 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7839 break;
7840 case RID_HAS_TRIVIAL_COPY:
7841 kind = CPTK_HAS_TRIVIAL_COPY;
7842 break;
7843 case RID_HAS_TRIVIAL_DESTRUCTOR:
7844 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7845 break;
7846 case RID_HAS_VIRTUAL_DESTRUCTOR:
7847 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7848 break;
7849 case RID_IS_ABSTRACT:
7850 kind = CPTK_IS_ABSTRACT;
7851 break;
7852 case RID_IS_BASE_OF:
7853 kind = CPTK_IS_BASE_OF;
7854 binary = true;
7855 break;
7856 case RID_IS_CLASS:
7857 kind = CPTK_IS_CLASS;
7858 break;
7859 case RID_IS_CONVERTIBLE_TO:
7860 kind = CPTK_IS_CONVERTIBLE_TO;
7861 binary = true;
7862 break;
7863 case RID_IS_EMPTY:
7864 kind = CPTK_IS_EMPTY;
7865 break;
7866 case RID_IS_ENUM:
7867 kind = CPTK_IS_ENUM;
7868 break;
aa4313eb 7869 case RID_IS_FINAL:
7870 kind = CPTK_IS_FINAL;
7871 break;
8de5c43e 7872 case RID_IS_LITERAL_TYPE:
7873 kind = CPTK_IS_LITERAL_TYPE;
7874 break;
481451eb 7875 case RID_IS_POD:
7876 kind = CPTK_IS_POD;
7877 break;
7878 case RID_IS_POLYMORPHIC:
7879 kind = CPTK_IS_POLYMORPHIC;
7880 break;
c1c67b4f 7881 case RID_IS_STD_LAYOUT:
7882 kind = CPTK_IS_STD_LAYOUT;
7883 break;
7884 case RID_IS_TRIVIAL:
7885 kind = CPTK_IS_TRIVIAL;
7886 break;
481451eb 7887 case RID_IS_UNION:
7888 kind = CPTK_IS_UNION;
7889 break;
8de5c43e 7890 case RID_UNDERLYING_TYPE:
7891 kind = CPTK_UNDERLYING_TYPE;
5290e253 7892 break;
e6014a82 7893 case RID_BASES:
7894 kind = CPTK_BASES;
7895 break;
7896 case RID_DIRECT_BASES:
7897 kind = CPTK_DIRECT_BASES;
7898 break;
481451eb 7899 default:
7900 gcc_unreachable ();
7901 }
7902
7903 /* Consume the token. */
7904 cp_lexer_consume_token (parser->lexer);
7905
c247dce0 7906 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
481451eb 7907
7908 type1 = cp_parser_type_id (parser);
7909
bcef2b12 7910 if (type1 == error_mark_node)
7911 return error_mark_node;
7912
481451eb 7913 /* Build a trivial decl-specifier-seq. */
7914 clear_decl_specs (&decl_specs);
7915 decl_specs.type = type1;
7916
7917 /* Call grokdeclarator to figure out what type this is. */
7918 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7919 /*initialized=*/0, /*attrlist=*/NULL);
7920
7921 if (binary)
7922 {
c247dce0 7923 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
481451eb 7924
7925 type2 = cp_parser_type_id (parser);
7926
bcef2b12 7927 if (type2 == error_mark_node)
7928 return error_mark_node;
7929
481451eb 7930 /* Build a trivial decl-specifier-seq. */
7931 clear_decl_specs (&decl_specs);
7932 decl_specs.type = type2;
7933
7934 /* Call grokdeclarator to figure out what type this is. */
7935 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7936 /*initialized=*/0, /*attrlist=*/NULL);
7937 }
7938
c247dce0 7939 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
481451eb 7940
bcef2b12 7941 /* Complete the trait expression, which may mean either processing
7942 the trait expr now or saving it for template instantiation. */
e6014a82 7943 switch(kind)
7944 {
7945 case CPTK_UNDERLYING_TYPE:
7946 return finish_underlying_type (type1);
7947 case CPTK_BASES:
7948 return finish_bases (type1, false);
7949 case CPTK_DIRECT_BASES:
7950 return finish_bases (type1, true);
7951 default:
7952 return finish_trait_expr (kind, type1, type2);
7953 }
481451eb 7954}
7955
a8b75081 7956/* Lambdas that appear in variable initializer or default argument scope
7957 get that in their mangling, so we need to record it. We might as well
7958 use the count for function and namespace scopes as well. */
1cf796e9 7959static GTY(()) tree lambda_scope;
7960static GTY(()) int lambda_count;
a8b75081 7961typedef struct GTY(()) tree_int
7962{
7963 tree t;
7964 int i;
7965} tree_int;
7966DEF_VEC_O(tree_int);
7967DEF_VEC_ALLOC_O(tree_int,gc);
7968static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7969
7970static void
7971start_lambda_scope (tree decl)
7972{
7973 tree_int ti;
7974 gcc_assert (decl);
7975 /* Once we're inside a function, we ignore other scopes and just push
7976 the function again so that popping works properly. */
7977 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7978 decl = current_function_decl;
7979 ti.t = lambda_scope;
7980 ti.i = lambda_count;
7981 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7982 if (lambda_scope != decl)
7983 {
7984 /* Don't reset the count if we're still in the same function. */
7985 lambda_scope = decl;
7986 lambda_count = 0;
7987 }
7988}
7989
7990static void
7991record_lambda_scope (tree lambda)
7992{
7993 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7994 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7995}
7996
7997static void
7998finish_lambda_scope (void)
7999{
8000 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8001 if (lambda_scope != p->t)
8002 {
8003 lambda_scope = p->t;
8004 lambda_count = p->i;
8005 }
8006 VEC_pop (tree_int, lambda_scope_stack);
8007}
8008
a8b75081 8009/* Parse a lambda expression.
8010
8011 lambda-expression:
8012 lambda-introducer lambda-declarator [opt] compound-statement
8013
8014 Returns a representation of the expression. */
8015
8016static tree
8017cp_parser_lambda_expression (cp_parser* parser)
8018{
8019 tree lambda_expr = build_lambda_expr ();
8020 tree type;
3847f0aa 8021 bool ok;
a8b75081 8022
8023 LAMBDA_EXPR_LOCATION (lambda_expr)
8024 = cp_lexer_peek_token (parser->lexer)->location;
8025
e8a2efc1 8026 if (cp_unevaluated_operand)
8027 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8028 "lambda-expression in unevaluated context");
8029
a8b75081 8030 /* We may be in the middle of deferred access check. Disable
8031 it now. */
8032 push_deferring_access_checks (dk_no_deferred);
8033
86b604cf 8034 cp_parser_lambda_introducer (parser, lambda_expr);
8035
a8b75081 8036 type = begin_lambda_type (lambda_expr);
69bcfc08 8037 if (type == error_mark_node)
8038 return error_mark_node;
a8b75081 8039
8040 record_lambda_scope (lambda_expr);
8041
18515bc1 8042 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8043 determine_visibility (TYPE_NAME (type));
8044
86b604cf 8045 /* Now that we've started the type, add the capture fields for any
8046 explicit captures. */
8047 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8048
a8b75081 8049 {
8050 /* Inside the class, surrounding template-parameter-lists do not apply. */
8051 unsigned int saved_num_template_parameter_lists
8052 = parser->num_template_parameter_lists;
b1bd5bd7 8053 unsigned char in_statement = parser->in_statement;
8054 bool in_switch_statement_p = parser->in_switch_statement_p;
a8b75081 8055
8056 parser->num_template_parameter_lists = 0;
b1bd5bd7 8057 parser->in_statement = 0;
8058 parser->in_switch_statement_p = false;
a8b75081 8059
a8b75081 8060 /* By virtue of defining a local class, a lambda expression has access to
8061 the private variables of enclosing classes. */
8062
3847f0aa 8063 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
a8b75081 8064
3847f0aa 8065 if (ok)
8066 cp_parser_lambda_body (parser, lambda_expr);
8067 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8068 cp_parser_skip_to_end_of_block_or_statement (parser);
a8b75081 8069
8070 /* The capture list was built up in reverse order; fix that now. */
8071 {
8072 tree newlist = NULL_TREE;
8073 tree elt, next;
8074
8075 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8076 elt; elt = next)
8077 {
9e1aa1c9 8078 next = TREE_CHAIN (elt);
8079 TREE_CHAIN (elt) = newlist;
8080 newlist = elt;
a8b75081 8081 }
8082 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8083 }
8084
3847f0aa 8085 if (ok)
8086 maybe_add_lambda_conv_op (type);
91a733f9 8087
a8b75081 8088 type = finish_struct (type, /*attributes=*/NULL_TREE);
8089
8090 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
b1bd5bd7 8091 parser->in_statement = in_statement;
8092 parser->in_switch_statement_p = in_switch_statement_p;
a8b75081 8093 }
8094
8095 pop_deferring_access_checks ();
8096
b1b48aca 8097 /* This field is only used during parsing of the lambda. */
8098 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8099
bcc4b4ea 8100 /* This lambda shouldn't have any proxies left at this point. */
8101 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8102 /* And now that we're done, push proxies for an enclosing lambda. */
8103 insert_pending_capture_proxies ();
8104
3847f0aa 8105 if (ok)
8106 return build_lambda_object (lambda_expr);
8107 else
8108 return error_mark_node;
a8b75081 8109}
8110
8111/* Parse the beginning of a lambda expression.
8112
8113 lambda-introducer:
8114 [ lambda-capture [opt] ]
8115
8116 LAMBDA_EXPR is the current representation of the lambda expression. */
8117
8118static void
8119cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8120{
8121 /* Need commas after the first capture. */
8122 bool first = true;
8123
8124 /* Eat the leading `['. */
c247dce0 8125 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
a8b75081 8126
8127 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8128 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8129 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8130 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8131 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8132 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8133
8134 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8135 {
8136 cp_lexer_consume_token (parser->lexer);
8137 first = false;
8138 }
8139
8140 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8141 {
8142 cp_token* capture_token;
8143 tree capture_id;
8144 tree capture_init_expr;
8145 cp_id_kind idk = CP_ID_KIND_NONE;
7da3c25a 8146 bool explicit_init_p = false;
a8b75081 8147
8148 enum capture_kind_type
8149 {
8150 BY_COPY,
8151 BY_REFERENCE
8152 };
8153 enum capture_kind_type capture_kind = BY_COPY;
8154
8155 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8156 {
8157 error ("expected end of capture-list");
8158 return;
8159 }
8160
8161 if (first)
8162 first = false;
8163 else
c247dce0 8164 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
a8b75081 8165
8166 /* Possibly capture `this'. */
8167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8168 {
3bd4ccf0 8169 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8170 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8171 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8172 "with by-copy capture default");
a8b75081 8173 cp_lexer_consume_token (parser->lexer);
8174 add_capture (lambda_expr,
bcc4b4ea 8175 /*id=*/this_identifier,
a8b75081 8176 /*initializer=*/finish_this_expr(),
7da3c25a 8177 /*by_reference_p=*/false,
8178 explicit_init_p);
a8b75081 8179 continue;
8180 }
8181
8182 /* Remember whether we want to capture as a reference or not. */
8183 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8184 {
8185 capture_kind = BY_REFERENCE;
8186 cp_lexer_consume_token (parser->lexer);
8187 }
8188
8189 /* Get the identifier. */
8190 capture_token = cp_lexer_peek_token (parser->lexer);
8191 capture_id = cp_parser_identifier (parser);
8192
8193 if (capture_id == error_mark_node)
8194 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8195 delimiters, but I modified this to stop on unnested ']' as well. It
8196 was already changed to stop on unnested '}', so the
8197 "closing_parenthesis" name is no more misleading with my change. */
8198 {
8199 cp_parser_skip_to_closing_parenthesis (parser,
8200 /*recovering=*/true,
8201 /*or_comma=*/true,
8202 /*consume_paren=*/true);
7271d48e 8203 break;
a8b75081 8204 }
8205
8206 /* Find the initializer for this capture. */
8207 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8208 {
8209 /* An explicit expression exists. */
8210 cp_lexer_consume_token (parser->lexer);
29438999 8211 pedwarn (input_location, OPT_Wpedantic,
a8b75081 8212 "ISO C++ does not allow initializers "
8213 "in lambda expression capture lists");
8214 capture_init_expr = cp_parser_assignment_expression (parser,
8215 /*cast_p=*/true,
8216 &idk);
7da3c25a 8217 explicit_init_p = true;
a8b75081 8218 }
8219 else
8220 {
8221 const char* error_msg;
8222
8223 /* Turn the identifier into an id-expression. */
8224 capture_init_expr
8225 = cp_parser_lookup_name
8226 (parser,
8227 capture_id,
8228 none_type,
8229 /*is_template=*/false,
8230 /*is_namespace=*/false,
8231 /*check_dependency=*/true,
8232 /*ambiguous_decls=*/NULL,
8233 capture_token->location);
8234
b9f4d0f4 8235 if (capture_init_expr == error_mark_node)
8236 {
8237 unqualified_name_lookup_error (capture_id);
8238 continue;
8239 }
8240 else if (DECL_P (capture_init_expr)
8241 && (TREE_CODE (capture_init_expr) != VAR_DECL
8242 && TREE_CODE (capture_init_expr) != PARM_DECL))
8243 {
8244 error_at (capture_token->location,
8245 "capture of non-variable %qD ",
8246 capture_init_expr);
8247 inform (0, "%q+#D declared here", capture_init_expr);
8248 continue;
8249 }
8250 if (TREE_CODE (capture_init_expr) == VAR_DECL
8251 && decl_storage_duration (capture_init_expr) != dk_auto)
8252 {
8253 pedwarn (capture_token->location, 0, "capture of variable "
8254 "%qD with non-automatic storage duration",
8255 capture_init_expr);
8256 inform (0, "%q+#D declared here", capture_init_expr);
8257 continue;
8258 }
8259
a8b75081 8260 capture_init_expr
8261 = finish_id_expression
8262 (capture_id,
8263 capture_init_expr,
8264 parser->scope,
8265 &idk,
8266 /*integral_constant_expression_p=*/false,
8267 /*allow_non_integral_constant_expression_p=*/false,
8268 /*non_integral_constant_expression_p=*/NULL,
8269 /*template_p=*/false,
8270 /*done=*/true,
8271 /*address_p=*/false,
8272 /*template_arg_p=*/false,
8273 &error_msg,
8274 capture_token->location);
8275 }
8276
3bd4ccf0 8277 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8278 && !explicit_init_p)
8279 {
8280 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8281 && capture_kind == BY_COPY)
8282 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8283 "of %qD redundant with by-copy capture default",
8284 capture_id);
8285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8286 && capture_kind == BY_REFERENCE)
8287 pedwarn (capture_token->location, 0, "explicit by-reference "
8288 "capture of %qD redundant with by-reference capture "
8289 "default", capture_id);
8290 }
8291
a8b75081 8292 add_capture (lambda_expr,
8293 capture_id,
8294 capture_init_expr,
7da3c25a 8295 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8296 explicit_init_p);
a8b75081 8297 }
8298
c247dce0 8299 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
a8b75081 8300}
8301
8302/* Parse the (optional) middle of a lambda expression.
8303
8304 lambda-declarator:
8305 ( parameter-declaration-clause [opt] )
8306 attribute-specifier [opt]
8307 mutable [opt]
8308 exception-specification [opt]
8309 lambda-return-type-clause [opt]
8310
8311 LAMBDA_EXPR is the current representation of the lambda expression. */
8312
3847f0aa 8313static bool
a8b75081 8314cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8315{
8316 /* 5.1.1.4 of the standard says:
8317 If a lambda-expression does not include a lambda-declarator, it is as if
8318 the lambda-declarator were ().
8319 This means an empty parameter list, no attributes, and no exception
8320 specification. */
8321 tree param_list = void_list_node;
8322 tree attributes = NULL_TREE;
8323 tree exception_spec = NULL_TREE;
8324 tree t;
8325
8326 /* The lambda-declarator is optional, but must begin with an opening
8327 parenthesis if present. */
8328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8329 {
8330 cp_lexer_consume_token (parser->lexer);
8331
8332 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8333
8334 /* Parse parameters. */
8335 param_list = cp_parser_parameter_declaration_clause (parser);
8336
8337 /* Default arguments shall not be specified in the
8338 parameter-declaration-clause of a lambda-declarator. */
8339 for (t = param_list; t; t = TREE_CHAIN (t))
8340 if (TREE_PURPOSE (t))
29438999 8341 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
a8b75081 8342 "default argument specified for lambda parameter");
8343
c247dce0 8344 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
a8b75081 8345
8346 attributes = cp_parser_attributes_opt (parser);
8347
8348 /* Parse optional `mutable' keyword. */
8349 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8350 {
8351 cp_lexer_consume_token (parser->lexer);
8352 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8353 }
8354
8355 /* Parse optional exception specification. */
8356 exception_spec = cp_parser_exception_specification_opt (parser);
8357
8358 /* Parse optional trailing return type. */
8359 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8360 {
8361 cp_lexer_consume_token (parser->lexer);
8362 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8363 }
8364
8365 /* The function parameters must be in scope all the way until after the
8366 trailing-return-type in case of decltype. */
1767a056 8367 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
a8b75081 8368 pop_binding (DECL_NAME (t), t);
8369
8370 leave_scope ();
8371 }
8372
8373 /* Create the function call operator.
8374
8375 Messing with declarators like this is no uglier than building up the
8376 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8377 other code. */
8378 {
8379 cp_decl_specifier_seq return_type_specs;
8380 cp_declarator* declarator;
8381 tree fco;
8382 int quals;
8383 void *p;
8384
8385 clear_decl_specs (&return_type_specs);
8386 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8387 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8388 else
86359a65 8389 /* Maybe we will deduce the return type later. */
8390 return_type_specs.type = make_auto ();
a8b75081 8391
8392 p = obstack_alloc (&declarator_obstack, 0);
8393
8394 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8395 sfk_none);
8396
43936711 8397 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8398 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
a8b75081 8399 declarator = make_call_declarator (declarator, param_list, quals,
ece7f9e3 8400 VIRT_SPEC_UNSPECIFIED,
a8b75081 8401 exception_spec,
8402 /*late_return_type=*/NULL_TREE);
3c5a5d99 8403 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
a8b75081 8404
8405 fco = grokmethod (&return_type_specs,
91a733f9 8406 declarator,
8407 attributes);
3847f0aa 8408 if (fco != error_mark_node)
8409 {
8410 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8411 DECL_ARTIFICIAL (fco) = 1;
bcc4b4ea 8412 /* Give the object parameter a different name. */
8413 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
3847f0aa 8414 }
a8b75081 8415
8416 finish_member_declaration (fco);
8417
8418 obstack_free (&declarator_obstack, p);
3847f0aa 8419
8420 return (fco != error_mark_node);
a8b75081 8421 }
8422}
8423
8424/* Parse the body of a lambda expression, which is simply
8425
8426 compound-statement
8427
8428 but which requires special handling.
8429 LAMBDA_EXPR is the current representation of the lambda expression. */
8430
8431static void
8432cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8433{
8434 bool nested = (current_function_decl != NULL_TREE);
40207b39 8435 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
a8b75081 8436 if (nested)
8437 push_function_context ();
cf797b5e 8438 else
8439 /* Still increment function_depth so that we don't GC in the
8440 middle of an expression. */
8441 ++function_depth;
40207b39 8442 /* Clear this in case we're in the middle of a default argument. */
8443 parser->local_variables_forbidden_p = false;
a8b75081 8444
8445 /* Finish the function call operator
8446 - class_specifier
8447 + late_parsing_for_member
8448 + function_definition_after_declarator
8449 + ctor_initializer_opt_and_function_body */
8450 {
8451 tree fco = lambda_function (lambda_expr);
8452 tree body;
8453 bool done = false;
9b17c849 8454 tree compound_stmt;
bcc4b4ea 8455 tree cap;
a8b75081 8456
8457 /* Let the front end know that we are going to be defining this
8458 function. */
8459 start_preparsed_function (fco,
8460 NULL_TREE,
8461 SF_PRE_PARSED | SF_INCLASS_INLINE);
8462
8463 start_lambda_scope (fco);
8464 body = begin_function_body ();
8465
9b17c849 8466 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8467 goto out;
8468
bcc4b4ea 8469 /* Push the proxies for any explicit captures. */
8470 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8471 cap = TREE_CHAIN (cap))
8472 build_capture_proxy (TREE_PURPOSE (cap));
8473
9b17c849 8474 compound_stmt = begin_compound_stmt (0);
8475
a8b75081 8476 /* 5.1.1.4 of the standard says:
8477 If a lambda-expression does not include a trailing-return-type, it
8478 is as if the trailing-return-type denotes the following type:
69df9882 8479 * if the compound-statement is of the form
a8b75081 8480 { return attribute-specifier [opt] expression ; }
8481 the type of the returned expression after lvalue-to-rvalue
8482 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8483 (_conv.array_ 4.2), and function-to-pointer conversion
8484 (_conv.func_ 4.3);
69df9882 8485 * otherwise, void. */
a8b75081 8486
8487 /* In a lambda that has neither a lambda-return-type-clause
8488 nor a deducible form, errors should be reported for return statements
8489 in the body. Since we used void as the placeholder return type, parsing
8490 the body as usual will give such desired behavior. */
8491 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9b17c849 8492 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8493 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
a8b75081 8494 {
a8b75081 8495 tree expr = NULL_TREE;
8496 cp_id_kind idk = CP_ID_KIND_NONE;
8497
8498 /* Parse tentatively in case there's more after the initial return
8499 statement. */
8500 cp_parser_parse_tentatively (parser);
8501
c247dce0 8502 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
a8b75081 8503
8504 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8505
c247dce0 8506 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8507 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
a8b75081 8508
8509 if (cp_parser_parse_definitely (parser))
8510 {
86359a65 8511 if (!processing_template_decl)
8512 apply_deduced_return_type (fco, lambda_return_type (expr));
a8b75081 8513
a8b75081 8514 /* Will get error here if type not deduced yet. */
8515 finish_return_stmt (expr);
a8b75081 8516
8517 done = true;
8518 }
8519 }
8520
8521 if (!done)
8522 {
9b17c849 8523 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8524 cp_parser_label_declaration (parser);
8525 cp_parser_statement_seq_opt (parser, NULL_TREE);
8526 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
a8b75081 8527 }
8528
9b17c849 8529 finish_compound_stmt (compound_stmt);
8530
8531 out:
a8b75081 8532 finish_function_body (body);
8533 finish_lambda_scope ();
8534
8535 /* Finish the function and generate code for it if necessary. */
8536 expand_or_defer_fn (finish_function (/*inline*/2));
8537 }
8538
40207b39 8539 parser->local_variables_forbidden_p = local_variables_forbidden_p;
a8b75081 8540 if (nested)
8541 pop_function_context();
cf797b5e 8542 else
8543 --function_depth;
a8b75081 8544}
8545
0a3b29ad 8546/* Statements [gram.stmt.stmt] */
8547
ccb84981 8548/* Parse a statement.
0a3b29ad 8549
8550 statement:
8551 labeled-statement
8552 expression-statement
8553 compound-statement
8554 selection-statement
8555 iteration-statement
8556 jump-statement
8557 declaration-statement
b75b98aa 8558 try-block
8559
4c0315d0 8560 TM Extension:
8561
8562 statement:
8563 atomic-statement
8564
074ab442 8565 IN_COMPOUND is true when the statement is nested inside a
e534436e 8566 cp_parser_compound_statement; this matters for certain pragmas.
8567
8568 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8569 is a (possibly labeled) if statement which is not enclosed in braces
8570 and has an else clause. This is used to implement -Wparentheses. */
0a3b29ad 8571
8572static void
b75b98aa 8573cp_parser_statement (cp_parser* parser, tree in_statement_expr,
e534436e 8574 bool in_compound, bool *if_p)
0a3b29ad 8575{
8576 tree statement;
8577 cp_token *token;
357f7efa 8578 location_t statement_location;
0a3b29ad 8579
b75b98aa 8580 restart:
e534436e 8581 if (if_p != NULL)
8582 *if_p = false;
0a3b29ad 8583 /* There is no statement yet. */
8584 statement = NULL_TREE;
8585 /* Peek at the next token. */
8586 token = cp_lexer_peek_token (parser->lexer);
4ee9c684 8587 /* Remember the location of the first token in the statement. */
357f7efa 8588 statement_location = token->location;
0a3b29ad 8589 /* If this is a keyword, then that will often determine what kind of
8590 statement we have. */
8591 if (token->type == CPP_KEYWORD)
8592 {
8593 enum rid keyword = token->keyword;
8594
8595 switch (keyword)
8596 {
8597 case RID_CASE:
8598 case RID_DEFAULT:
17d53949 8599 /* Looks like a labeled-statement with a case label.
8600 Parse the label, and then use tail recursion to parse
8601 the statement. */
8602 cp_parser_label_for_labeled_statement (parser);
8603 goto restart;
0a3b29ad 8604
8605 case RID_IF:
8606 case RID_SWITCH:
e534436e 8607 statement = cp_parser_selection_statement (parser, if_p);
0a3b29ad 8608 break;
8609
8610 case RID_WHILE:
8611 case RID_DO:
8612 case RID_FOR:
8613 statement = cp_parser_iteration_statement (parser);
8614 break;
8615
8616 case RID_BREAK:
8617 case RID_CONTINUE:
8618 case RID_RETURN:
8619 case RID_GOTO:
8620 statement = cp_parser_jump_statement (parser);
8621 break;
8622
79408174 8623 /* Objective-C++ exception-handling constructs. */
8624 case RID_AT_TRY:
8625 case RID_AT_CATCH:
8626 case RID_AT_FINALLY:
8627 case RID_AT_SYNCHRONIZED:
8628 case RID_AT_THROW:
8629 statement = cp_parser_objc_statement (parser);
8630 break;
7a4e126b 8631
0a3b29ad 8632 case RID_TRY:
8633 statement = cp_parser_try_block (parser);
8634 break;
8635
26d3c536 8636 case RID_NAMESPACE:
8637 /* This must be a namespace alias definition. */
8638 cp_parser_declaration_statement (parser);
8639 return;
8640
4c0315d0 8641 case RID_TRANSACTION_ATOMIC:
8642 case RID_TRANSACTION_RELAXED:
8643 statement = cp_parser_transaction (parser, keyword);
8644 break;
8645 case RID_TRANSACTION_CANCEL:
8646 statement = cp_parser_transaction_cancel (parser);
8647 break;
8648
0a3b29ad 8649 default:
8650 /* It might be a keyword like `int' that can start a
8651 declaration-statement. */
8652 break;
8653 }
8654 }
8655 else if (token->type == CPP_NAME)
8656 {
8657 /* If the next token is a `:', then we are looking at a
8658 labeled-statement. */
8659 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8660 if (token->type == CPP_COLON)
17d53949 8661 {
8662 /* Looks like a labeled-statement with an ordinary label.
8663 Parse the label, and then use tail recursion to parse
8664 the statement. */
8665 cp_parser_label_for_labeled_statement (parser);
8666 goto restart;
8667 }
0a3b29ad 8668 }
8669 /* Anything that starts with a `{' must be a compound-statement. */
8670 else if (token->type == CPP_OPEN_BRACE)
6bf5e78d 8671 statement = cp_parser_compound_statement (parser, NULL, false, false);
3986d990 8672 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8673 a statement all its own. */
8674 else if (token->type == CPP_PRAGMA)
8675 {
b75b98aa 8676 /* Only certain OpenMP pragmas are attached to statements, and thus
8677 are considered statements themselves. All others are not. In
8678 the context of a compound, accept the pragma as a "statement" and
074ab442 8679 return so that we can check for a close brace. Otherwise we
b75b98aa 8680 require a real statement and must go back and read one. */
8681 if (in_compound)
8682 cp_parser_pragma (parser, pragma_compound);
8683 else if (!cp_parser_pragma (parser, pragma_stmt))
8684 goto restart;
3986d990 8685 return;
8686 }
5f959b7d 8687 else if (token->type == CPP_EOF)
8688 {
8689 cp_parser_error (parser, "expected statement");
8690 return;
8691 }
0a3b29ad 8692
8693 /* Everything else must be a declaration-statement or an
ccb84981 8694 expression-statement. Try for the declaration-statement
0a3b29ad 8695 first, unless we are looking at a `;', in which case we know that
8696 we have an expression-statement. */
8697 if (!statement)
8698 {
8699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8700 {
8701 cp_parser_parse_tentatively (parser);
8702 /* Try to parse the declaration-statement. */
8703 cp_parser_declaration_statement (parser);
8704 /* If that worked, we're done. */
8705 if (cp_parser_parse_definitely (parser))
8706 return;
8707 }
8708 /* Look for an expression-statement instead. */
2363ef00 8709 statement = cp_parser_expression_statement (parser, in_statement_expr);
0a3b29ad 8710 }
8711
8712 /* Set the line number for the statement. */
bac62436 8713 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
357f7efa 8714 SET_EXPR_LOCATION (statement, statement_location);
0a3b29ad 8715}
8716
17d53949 8717/* Parse the label for a labeled-statement, i.e.
0a3b29ad 8718
17d53949 8719 identifier :
8720 case constant-expression :
8721 default :
260cf17d 8722
8723 GNU Extension:
17d53949 8724 case constant-expression ... constant-expression : statement
ccb84981 8725
17d53949 8726 When a label is parsed without errors, the label is added to the
8727 parse tree by the finish_* functions, so this function doesn't
8728 have to return the label. */
b75b98aa 8729
17d53949 8730static void
8731cp_parser_label_for_labeled_statement (cp_parser* parser)
0a3b29ad 8732{
8733 cp_token *token;
e1c8f1c5 8734 tree label = NULL_TREE;
1e838f42 8735 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
0a3b29ad 8736
8737 /* The next token should be an identifier. */
8738 token = cp_lexer_peek_token (parser->lexer);
8739 if (token->type != CPP_NAME
8740 && token->type != CPP_KEYWORD)
8741 {
8742 cp_parser_error (parser, "expected labeled-statement");
17d53949 8743 return;
0a3b29ad 8744 }
8745
1e838f42 8746 parser->colon_corrects_to_scope_p = false;
0a3b29ad 8747 switch (token->keyword)
8748 {
8749 case RID_CASE:
8750 {
260cf17d 8751 tree expr, expr_hi;
8752 cp_token *ellipsis;
0a3b29ad 8753
8754 /* Consume the `case' token. */
8755 cp_lexer_consume_token (parser->lexer);
8756 /* Parse the constant-expression. */
ccb84981 8757 expr = cp_parser_constant_expression (parser,
13795292 8758 /*allow_non_constant_p=*/false,
5f6526e1 8759 NULL);
260cf17d 8760
8761 ellipsis = cp_lexer_peek_token (parser->lexer);
8762 if (ellipsis->type == CPP_ELLIPSIS)
8763 {
653e5405 8764 /* Consume the `...' token. */
260cf17d 8765 cp_lexer_consume_token (parser->lexer);
8766 expr_hi =
8767 cp_parser_constant_expression (parser,
653e5405 8768 /*allow_non_constant_p=*/false,
260cf17d 8769 NULL);
8770 /* We don't need to emit warnings here, as the common code
8771 will do this for us. */
8772 }
8773 else
8774 expr_hi = NULL_TREE;
8775
b75b98aa 8776 if (parser->in_switch_statement_p)
e60a6f7b 8777 finish_case_label (token->location, expr, expr_hi);
b75b98aa 8778 else
ccb59bb6 8779 error_at (token->location,
8780 "case label %qE not within a switch statement",
8781 expr);
0a3b29ad 8782 }
8783 break;
8784
8785 case RID_DEFAULT:
8786 /* Consume the `default' token. */
8787 cp_lexer_consume_token (parser->lexer);
b75b98aa 8788
8789 if (parser->in_switch_statement_p)
e60a6f7b 8790 finish_case_label (token->location, NULL_TREE, NULL_TREE);
b75b98aa 8791 else
ccb59bb6 8792 error_at (token->location, "case label not within a switch statement");
0a3b29ad 8793 break;
8794
8795 default:
8796 /* Anything else must be an ordinary label. */
e1c8f1c5 8797 label = finish_label_stmt (cp_parser_identifier (parser));
0a3b29ad 8798 break;
8799 }
8800
8801 /* Require the `:' token. */
c247dce0 8802 cp_parser_require (parser, CPP_COLON, RT_COLON);
e1c8f1c5 8803
8804 /* An ordinary label may optionally be followed by attributes.
8805 However, this is only permitted if the attributes are then
8806 followed by a semicolon. This is because, for backward
8807 compatibility, when parsing
8808 lab: __attribute__ ((unused)) int i;
8809 we want the attribute to attach to "i", not "lab". */
8810 if (label != NULL_TREE
8811 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8812 {
8813 tree attrs;
8814
8815 cp_parser_parse_tentatively (parser);
8816 attrs = cp_parser_attributes_opt (parser);
8817 if (attrs == NULL_TREE
8818 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8819 cp_parser_abort_tentative_parse (parser);
8820 else if (!cp_parser_parse_definitely (parser))
8821 ;
8822 else
8823 cplus_decl_attributes (&label, attrs, 0);
8824 }
1e838f42 8825
8826 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
0a3b29ad 8827}
8828
8829/* Parse an expression-statement.
8830
8831 expression-statement:
8832 expression [opt] ;
8833
8834 Returns the new EXPR_STMT -- or NULL_TREE if the expression
942ab15b 8835 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8836 indicates whether this expression-statement is part of an
8837 expression statement. */
0a3b29ad 8838
8839static tree
2363ef00 8840cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
0a3b29ad 8841{
942ab15b 8842 tree statement = NULL_TREE;
5bd9bf06 8843 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 8844
942ab15b 8845 /* If the next token is a ';', then there is no expression
bd8962d5 8846 statement. */
0a3b29ad 8847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 8848 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
ccb84981 8849
5570fae0 8850 /* Give a helpful message for "A<T>::type t;" and the like. */
5bd9bf06 8851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
5570fae0 8852 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8853 {
8854 if (TREE_CODE (statement) == SCOPE_REF)
8855 error_at (token->location, "need %<typename%> before %qE because "
8856 "%qT is a dependent scope",
8857 statement, TREE_OPERAND (statement, 0));
8858 else if (is_overloaded_fn (statement)
8859 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8860 {
8861 /* A::A a; */
8862 tree fn = get_first_fn (statement);
8863 error_at (token->location,
8864 "%<%T::%D%> names the constructor, not the type",
8865 DECL_CONTEXT (fn), DECL_NAME (fn));
8866 }
8867 }
5bd9bf06 8868
0a3b29ad 8869 /* Consume the final `;'. */
cf91b86a 8870 cp_parser_consume_semicolon_at_end_of_statement (parser);
0a3b29ad 8871
2363ef00 8872 if (in_statement_expr
942ab15b 8873 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
640aa28c 8874 /* This is the final expression statement of a statement
8875 expression. */
8876 statement = finish_stmt_expr_expr (statement, in_statement_expr);
942ab15b 8877 else if (statement)
8878 statement = finish_expr_stmt (statement);
8879 else
8880 finish_stmt ();
ccb84981 8881
0a3b29ad 8882 return statement;
8883}
8884
8885/* Parse a compound-statement.
8886
8887 compound-statement:
8888 { statement-seq [opt] }
ccb84981 8889
3dac447c 8890 GNU extension:
8891
8892 compound-statement:
8893 { label-declaration-seq [opt] statement-seq [opt] }
8894
8895 label-declaration-seq:
8896 label-declaration
8897 label-declaration-seq label-declaration
8898
632f8185 8899 Returns a tree representing the statement. */
0a3b29ad 8900
8901static tree
2363ef00 8902cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6bf5e78d 8903 bool in_try, bool function_body)
0a3b29ad 8904{
8905 tree compound_stmt;
8906
8907 /* Consume the `{'. */
c247dce0 8908 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
0a3b29ad 8909 return error_mark_node;
6bf5e78d 8910 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8911 && !function_body)
29438999 8912 pedwarn (input_location, OPT_Wpedantic,
6bf5e78d 8913 "compound-statement in constexpr function");
0a3b29ad 8914 /* Begin the compound-statement. */
2363ef00 8915 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
3dac447c 8916 /* If the next keyword is `__label__' we have a label declaration. */
8917 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8918 cp_parser_label_declaration (parser);
0a3b29ad 8919 /* Parse an (optional) statement-seq. */
2363ef00 8920 cp_parser_statement_seq_opt (parser, in_statement_expr);
0a3b29ad 8921 /* Finish the compound-statement. */
68f8f8cc 8922 finish_compound_stmt (compound_stmt);
0a3b29ad 8923 /* Consume the `}'. */
c247dce0 8924 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 8925
8926 return compound_stmt;
8927}
8928
8929/* Parse an (optional) statement-seq.
8930
8931 statement-seq:
8932 statement
8933 statement-seq [opt] statement */
8934
8935static void
2363ef00 8936cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
0a3b29ad 8937{
8938 /* Scan statements until there aren't any more. */
8939 while (true)
8940 {
b75b98aa 8941 cp_token *token = cp_lexer_peek_token (parser->lexer);
8942
06517bd4 8943 /* If we are looking at a `}', then we have run out of
8944 statements; the same is true if we have reached the end
8945 of file, or have stumbled upon a stray '@end'. */
b75b98aa 8946 if (token->type == CPP_CLOSE_BRACE
8947 || token->type == CPP_EOF
06517bd4 8948 || token->type == CPP_PRAGMA_EOL
8949 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
0a3b29ad 8950 break;
2672c56c 8951
8952 /* If we are in a compound statement and find 'else' then
8953 something went wrong. */
8954 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8955 {
8956 if (parser->in_statement & IN_IF_STMT)
8957 break;
8958 else
8959 {
8960 token = cp_lexer_consume_token (parser->lexer);
ccb59bb6 8961 error_at (token->location, "%<else%> without a previous %<if%>");
2672c56c 8962 }
8963 }
0a3b29ad 8964
8965 /* Parse the statement. */
e534436e 8966 cp_parser_statement (parser, in_statement_expr, true, NULL);
0a3b29ad 8967 }
8968}
8969
8970/* Parse a selection-statement.
8971
8972 selection-statement:
8973 if ( condition ) statement
8974 if ( condition ) statement else statement
ccb84981 8975 switch ( condition ) statement
0a3b29ad 8976
e534436e 8977 Returns the new IF_STMT or SWITCH_STMT.
8978
8979 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8980 is a (possibly labeled) if statement which is not enclosed in
8981 braces and has an else clause. This is used to implement
8982 -Wparentheses. */
0a3b29ad 8983
8984static tree
e534436e 8985cp_parser_selection_statement (cp_parser* parser, bool *if_p)
0a3b29ad 8986{
8987 cp_token *token;
8988 enum rid keyword;
8989
e534436e 8990 if (if_p != NULL)
8991 *if_p = false;
8992
0a3b29ad 8993 /* Peek at the next token. */
c247dce0 8994 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
0a3b29ad 8995
8996 /* See what kind of keyword it is. */
8997 keyword = token->keyword;
8998 switch (keyword)
8999 {
9000 case RID_IF:
9001 case RID_SWITCH:
9002 {
9003 tree statement;
9004 tree condition;
9005
9006 /* Look for the `('. */
c247dce0 9007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
0a3b29ad 9008 {
9009 cp_parser_skip_to_end_of_statement (parser);
9010 return error_mark_node;
9011 }
9012
9013 /* Begin the selection-statement. */
9014 if (keyword == RID_IF)
9015 statement = begin_if_stmt ();
9016 else
9017 statement = begin_switch_stmt ();
9018
9019 /* Parse the condition. */
9020 condition = cp_parser_condition (parser);
9021 /* Look for the `)'. */
c247dce0 9022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3d0f901b 9023 cp_parser_skip_to_closing_parenthesis (parser, true, false,
9024 /*consume_paren=*/true);
0a3b29ad 9025
9026 if (keyword == RID_IF)
9027 {
e534436e 9028 bool nested_if;
2672c56c 9029 unsigned char in_statement;
e534436e 9030
0a3b29ad 9031 /* Add the condition. */
9032 finish_if_stmt_cond (condition, statement);
9033
9034 /* Parse the then-clause. */
2672c56c 9035 in_statement = parser->in_statement;
9036 parser->in_statement |= IN_IF_STMT;
c2c7830b 9037 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9038 {
9039 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
e60a6f7b 9040 add_stmt (build_empty_stmt (loc));
c2c7830b 9041 cp_lexer_consume_token (parser->lexer);
9042 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9043 warning_at (loc, OPT_Wempty_body, "suggest braces around "
9044 "empty body in an %<if%> statement");
75b32336 9045 nested_if = false;
c2c7830b 9046 }
9047 else
9048 cp_parser_implicitly_scoped_statement (parser, &nested_if);
2672c56c 9049 parser->in_statement = in_statement;
9050
0a3b29ad 9051 finish_then_clause (statement);
9052
9053 /* If the next token is `else', parse the else-clause. */
9054 if (cp_lexer_next_token_is_keyword (parser->lexer,
9055 RID_ELSE))
9056 {
0a3b29ad 9057 /* Consume the `else' keyword. */
9058 cp_lexer_consume_token (parser->lexer);
2363ef00 9059 begin_else_clause (statement);
0a3b29ad 9060 /* Parse the else-clause. */
c2c7830b 9061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9062 {
e60a6f7b 9063 location_t loc;
9064 loc = cp_lexer_peek_token (parser->lexer)->location;
9065 warning_at (loc,
c2c7830b 9066 OPT_Wempty_body, "suggest braces around "
9067 "empty body in an %<else%> statement");
e60a6f7b 9068 add_stmt (build_empty_stmt (loc));
c2c7830b 9069 cp_lexer_consume_token (parser->lexer);
9070 }
9071 else
9072 cp_parser_implicitly_scoped_statement (parser, NULL);
9073
0a3b29ad 9074 finish_else_clause (statement);
e534436e 9075
9076 /* If we are currently parsing a then-clause, then
9077 IF_P will not be NULL. We set it to true to
9078 indicate that this if statement has an else clause.
9079 This may trigger the Wparentheses warning below
9080 when we get back up to the parent if statement. */
9081 if (if_p != NULL)
9082 *if_p = true;
9083 }
9084 else
9085 {
9086 /* This if statement does not have an else clause. If
9087 NESTED_IF is true, then the then-clause is an if
9088 statement which does have an else clause. We warn
9089 about the potential ambiguity. */
9090 if (nested_if)
ccb59bb6 9091 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9092 "suggest explicit braces to avoid ambiguous"
9093 " %<else%>");
0a3b29ad 9094 }
9095
9096 /* Now we're all done with the if-statement. */
2363ef00 9097 finish_if_stmt (statement);
0a3b29ad 9098 }
9099 else
9100 {
c3fbce20 9101 bool in_switch_statement_p;
8487df40 9102 unsigned char in_statement;
0a3b29ad 9103
9104 /* Add the condition. */
9105 finish_switch_cond (condition, statement);
9106
9107 /* Parse the body of the switch-statement. */
c3fbce20 9108 in_switch_statement_p = parser->in_switch_statement_p;
8487df40 9109 in_statement = parser->in_statement;
c3fbce20 9110 parser->in_switch_statement_p = true;
8487df40 9111 parser->in_statement |= IN_SWITCH_STMT;
e534436e 9112 cp_parser_implicitly_scoped_statement (parser, NULL);
c3fbce20 9113 parser->in_switch_statement_p = in_switch_statement_p;
8487df40 9114 parser->in_statement = in_statement;
0a3b29ad 9115
9116 /* Now we're all done with the switch-statement. */
9117 finish_switch_stmt (statement);
9118 }
9119
9120 return statement;
9121 }
9122 break;
9123
9124 default:
9125 cp_parser_error (parser, "expected selection-statement");
9126 return error_mark_node;
9127 }
9128}
9129
ccb84981 9130/* Parse a condition.
0a3b29ad 9131
9132 condition:
9133 expression
f82f1250 9134 type-specifier-seq declarator = initializer-clause
9135 type-specifier-seq declarator braced-init-list
0a3b29ad 9136
9137 GNU Extension:
ccb84981 9138
0a3b29ad 9139 condition:
ccb84981 9140 type-specifier-seq declarator asm-specification [opt]
0a3b29ad 9141 attributes [opt] = assignment-expression
ccb84981 9142
0a3b29ad 9143 Returns the expression that should be tested. */
9144
9145static tree
45baea8b 9146cp_parser_condition (cp_parser* parser)
0a3b29ad 9147{
4b9b2871 9148 cp_decl_specifier_seq type_specifiers;
0a3b29ad 9149 const char *saved_message;
ca63c29a 9150 int declares_class_or_enum;
0a3b29ad 9151
9152 /* Try the declaration first. */
9153 cp_parser_parse_tentatively (parser);
9154 /* New types are not allowed in the type-specifier-seq for a
9155 condition. */
9156 saved_message = parser->type_definition_forbidden_message;
9157 parser->type_definition_forbidden_message
ca82e026 9158 = G_("types may not be defined in conditions");
0a3b29ad 9159 /* Parse the type-specifier-seq. */
ca63c29a 9160 cp_parser_decl_specifier_seq (parser,
9161 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9162 &type_specifiers,
9163 &declares_class_or_enum);
0a3b29ad 9164 /* Restore the saved message. */
9165 parser->type_definition_forbidden_message = saved_message;
9166 /* If all is well, we might be looking at a declaration. */
9167 if (!cp_parser_error_occurred (parser))
9168 {
9169 tree decl;
9170 tree asm_specification;
9171 tree attributes;
3046c0a3 9172 cp_declarator *declarator;
0a3b29ad 9173 tree initializer = NULL_TREE;
ccb84981 9174
0a3b29ad 9175 /* Parse the declarator. */
42bbd0ec 9176 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 9177 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 9178 /*parenthesized_p=*/NULL,
9179 /*member_p=*/false);
0a3b29ad 9180 /* Parse the attributes. */
9181 attributes = cp_parser_attributes_opt (parser);
9182 /* Parse the asm-specification. */
9183 asm_specification = cp_parser_asm_specification_opt (parser);
f82f1250 9184 /* If the next token is not an `=' or '{', then we might still be
0a3b29ad 9185 looking at an expression. For example:
ccb84981 9186
0a3b29ad 9187 if (A(a).x)
ccb84981 9188
0a3b29ad 9189 looks like a decl-specifier-seq and a declarator -- but then
9190 there is no `=', so this is an expression. */
f82f1250 9191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9192 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9193 cp_parser_simulate_error (parser);
9194
9195 /* If we did see an `=' or '{', then we are looking at a declaration
0a3b29ad 9196 for sure. */
9197 if (cp_parser_parse_definitely (parser))
9198 {
9031d10b 9199 tree pushed_scope;
d91303a6 9200 bool non_constant_p;
f82f1250 9201 bool flags = LOOKUP_ONLYCONVERTING;
91caa6ca 9202
0a3b29ad 9203 /* Create the declaration. */
4b9b2871 9204 decl = start_decl (declarator, &type_specifiers,
0a3b29ad 9205 /*initialized_p=*/true,
91caa6ca 9206 attributes, /*prefix_attributes=*/NULL_TREE,
7f602bca 9207 &pushed_scope);
f82f1250 9208
9209 /* Parse the initializer. */
9210 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9211 {
9212 initializer = cp_parser_braced_list (parser, &non_constant_p);
9213 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9214 flags = 0;
9215 }
9216 else
9217 {
9218 /* Consume the `='. */
c247dce0 9219 cp_parser_require (parser, CPP_EQ, RT_EQ);
f82f1250 9220 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9221 }
9222 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
bf8d19fe 9223 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 9224
0a3b29ad 9225 /* Process the initializer. */
ccb84981 9226 cp_finish_decl (decl,
074ab442 9227 initializer, !non_constant_p,
ccb84981 9228 asm_specification,
f82f1250 9229 flags);
00d26680 9230
7f602bca 9231 if (pushed_scope)
9232 pop_scope (pushed_scope);
ccb84981 9233
0a3b29ad 9234 return convert_from_reference (decl);
9235 }
9236 }
9237 /* If we didn't even get past the declarator successfully, we are
9238 definitely not looking at a declaration. */
9239 else
9240 cp_parser_abort_tentative_parse (parser);
9241
9242 /* Otherwise, we are looking at an expression. */
98b326fd 9243 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 9244}
9245
fa7d5870 9246/* Parses a for-statement or range-for-statement until the closing ')',
9247 not included. */
9dd72ec4 9248
9249static tree
fa7d5870 9250cp_parser_for (cp_parser *parser)
9dd72ec4 9251{
fa7d5870 9252 tree init, scope, decl;
9253 bool is_range_for;
9dd72ec4 9254
9255 /* Begin the for-statement. */
fa7d5870 9256 scope = begin_for_scope (&init);
9dd72ec4 9257
9258 /* Parse the initialization. */
fa7d5870 9259 is_range_for = cp_parser_for_init_statement (parser, &decl);
9260
9261 if (is_range_for)
9262 return cp_parser_range_for (parser, scope, init, decl);
9263 else
9264 return cp_parser_c_for (parser, scope, init);
9265}
9266
9267static tree
9268cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9269{
9270 /* Normal for loop */
9271 tree condition = NULL_TREE;
9272 tree expression = NULL_TREE;
9273 tree stmt;
9274
9275 stmt = begin_for_stmt (scope, init);
9276 /* The for-init-statement has already been parsed in
9277 cp_parser_for_init_statement, so no work is needed here. */
9dd72ec4 9278 finish_for_init_stmt (stmt);
9279
9280 /* If there's a condition, process it. */
9281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9282 condition = cp_parser_condition (parser);
9283 finish_for_cond (condition, stmt);
9284 /* Look for the `;'. */
9285 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9286
9287 /* If there's an expression, process it. */
9288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9289 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9290 finish_for_expr (expression, stmt);
9291
9292 return stmt;
9293}
9294
9295/* Tries to parse a range-based for-statement:
9296
9297 range-based-for:
fa7d5870 9298 decl-specifier-seq declarator : expression
9dd72ec4 9299
fa7d5870 9300 The decl-specifier-seq declarator and the `:' are already parsed by
9301 cp_parser_for_init_statement. If processing_template_decl it returns a
9302 newly created RANGE_FOR_STMT; if not, it is converted to a
9303 regular FOR_STMT. */
9dd72ec4 9304
9305static tree
fa7d5870 9306cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9dd72ec4 9307{
fa7d5870 9308 tree stmt, range_expr;
9dd72ec4 9309
9dd72ec4 9310 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9311 {
9312 bool expr_non_constant_p;
9313 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9314 }
9315 else
9316 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9317
fa7d5870 9318 /* If in template, STMT is converted to a normal for-statement
9dd72ec4 9319 at instantiation. If not, it is done just ahead. */
9320 if (processing_template_decl)
fa7d5870 9321 {
57a74da5 9322 if (check_for_bare_parameter_packs (range_expr))
9323 range_expr = error_mark_node;
fa7d5870 9324 stmt = begin_range_for_stmt (scope, init);
9325 finish_range_for_decl (stmt, range_decl, range_expr);
14034aae 9326 if (!type_dependent_expression_p (range_expr)
9327 /* do_auto_deduction doesn't mess with template init-lists. */
9328 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
14cb05a2 9329 do_range_for_auto_deduction (range_decl, range_expr);
fa7d5870 9330 }
9dd72ec4 9331 else
fa7d5870 9332 {
9333 stmt = begin_for_stmt (scope, init);
9334 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9335 }
9dd72ec4 9336 return stmt;
9337}
9338
4121b00f 9339/* Subroutine of cp_convert_range_for: given the initializer expression,
9340 builds up the range temporary. */
9341
9342static tree
9343build_range_temp (tree range_expr)
9344{
9345 tree range_type, range_temp;
9346
9347 /* Find out the type deduced by the declaration
9348 `auto &&__range = range_expr'. */
9349 range_type = cp_build_reference_type (make_auto (), true);
9350 range_type = do_auto_deduction (range_type, range_expr,
9351 type_uses_auto (range_type));
9352
9353 /* Create the __range variable. */
9354 range_temp = build_decl (input_location, VAR_DECL,
9355 get_identifier ("__for_range"), range_type);
9356 TREE_USED (range_temp) = 1;
9357 DECL_ARTIFICIAL (range_temp) = 1;
9358
9359 return range_temp;
9360}
9361
9362/* Used by cp_parser_range_for in template context: we aren't going to
9363 do a full conversion yet, but we still need to resolve auto in the
9364 type of the for-range-declaration if present. This is basically
9365 a shortcut version of cp_convert_range_for. */
9366
9367static void
9368do_range_for_auto_deduction (tree decl, tree range_expr)
9369{
9370 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9371 if (auto_node)
9372 {
9373 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9374 range_temp = convert_from_reference (build_range_temp (range_expr));
9375 iter_type = (cp_parser_perform_range_for_lookup
9376 (range_temp, &begin_dummy, &end_dummy));
9377 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
ef0b0c72 9378 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
4121b00f 9379 tf_warning_or_error);
9380 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9381 iter_decl, auto_node);
9382 }
9383}
9384
9dd72ec4 9385/* Converts a range-based for-statement into a normal
9386 for-statement, as per the definition.
9387
9388 for (RANGE_DECL : RANGE_EXPR)
9389 BLOCK
9390
9391 should be equivalent to:
9392
9393 {
9394 auto &&__range = RANGE_EXPR;
9395 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9396 __begin != __end;
9397 ++__begin)
9398 {
9399 RANGE_DECL = *__begin;
9400 BLOCK
9401 }
9402 }
9403
9404 If RANGE_EXPR is an array:
4e81a9f3 9405 BEGIN_EXPR = __range
9406 END_EXPR = __range + ARRAY_SIZE(__range)
9407 Else if RANGE_EXPR has a member 'begin' or 'end':
9408 BEGIN_EXPR = __range.begin()
9409 END_EXPR = __range.end()
9dd72ec4 9410 Else:
9411 BEGIN_EXPR = begin(__range)
9412 END_EXPR = end(__range);
9413
4e81a9f3 9414 If __range has a member 'begin' but not 'end', or vice versa, we must
9415 still use the second alternative (it will surely fail, however).
9416 When calling begin()/end() in the third alternative we must use
9417 argument dependent lookup, but always considering 'std' as an associated
9418 namespace. */
9dd72ec4 9419
9420tree
9421cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9422{
9dd72ec4 9423 tree begin, end;
9424 tree iter_type, begin_expr, end_expr;
9425 tree condition, expression;
9426
fa7d5870 9427 if (range_decl == error_mark_node || range_expr == error_mark_node)
9428 /* If an error happened previously do nothing or else a lot of
9429 unhelpful errors would be issued. */
9430 begin_expr = end_expr = iter_type = error_mark_node;
9dd72ec4 9431 else
9432 {
4121b00f 9433 tree range_temp = build_range_temp (range_expr);
fa7d5870 9434 pushdecl (range_temp);
9435 cp_finish_decl (range_temp, range_expr,
9436 /*is_constant_init*/false, NULL_TREE,
9437 LOOKUP_ONLYCONVERTING);
9438
9439 range_temp = convert_from_reference (range_temp);
4e81a9f3 9440 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9441 &begin_expr, &end_expr);
9dd72ec4 9442 }
9443
4e81a9f3 9444 /* The new for initialization statement. */
9dd72ec4 9445 begin = build_decl (input_location, VAR_DECL,
9446 get_identifier ("__for_begin"), iter_type);
9447 TREE_USED (begin) = 1;
9448 DECL_ARTIFICIAL (begin) = 1;
9449 pushdecl (begin);
936983bf 9450 cp_finish_decl (begin, begin_expr,
9451 /*is_constant_init*/false, NULL_TREE,
9452 LOOKUP_ONLYCONVERTING);
9453
9dd72ec4 9454 end = build_decl (input_location, VAR_DECL,
9455 get_identifier ("__for_end"), iter_type);
9456 TREE_USED (end) = 1;
9457 DECL_ARTIFICIAL (end) = 1;
9458 pushdecl (end);
936983bf 9459 cp_finish_decl (end, end_expr,
9460 /*is_constant_init*/false, NULL_TREE,
9461 LOOKUP_ONLYCONVERTING);
9dd72ec4 9462
9463 finish_for_init_stmt (statement);
9464
4e81a9f3 9465 /* The new for condition. */
ef0b0c72 9466 condition = build_x_binary_op (input_location, NE_EXPR,
9dd72ec4 9467 begin, ERROR_MARK,
9468 end, ERROR_MARK,
9469 NULL, tf_warning_or_error);
9470 finish_for_cond (condition, statement);
9471
4e81a9f3 9472 /* The new increment expression. */
ef0b0c72 9473 expression = finish_unary_op_expr (input_location,
9474 PREINCREMENT_EXPR, begin);
9dd72ec4 9475 finish_for_expr (expression, statement);
9476
4e81a9f3 9477 /* The declaration is initialized with *__begin inside the loop body. */
9dd72ec4 9478 cp_finish_decl (range_decl,
ef0b0c72 9479 build_x_indirect_ref (input_location, begin, RO_NULL,
9480 tf_warning_or_error),
9dd72ec4 9481 /*is_constant_init*/false, NULL_TREE,
9482 LOOKUP_ONLYCONVERTING);
9483
9484 return statement;
9485}
9486
4e81a9f3 9487/* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9488 We need to solve both at the same time because the method used
9489 depends on the existence of members begin or end.
9490 Returns the type deduced for the iterator expression. */
9491
9492static tree
9493cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9494{
8ea810e9 9495 if (error_operand_p (range))
6280641a 9496 {
9497 *begin = *end = error_mark_node;
9498 return error_mark_node;
9499 }
8ea810e9 9500
321f66b1 9501 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
4e81a9f3 9502 {
9503 error ("range-based %<for%> expression of type %qT "
9504 "has incomplete type", TREE_TYPE (range));
9505 *begin = *end = error_mark_node;
9506 return error_mark_node;
9507 }
9508 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9509 {
9510 /* If RANGE is an array, we will use pointer arithmetic. */
9511 *begin = range;
9512 *end = build_binary_op (input_location, PLUS_EXPR,
9513 range,
9514 array_type_nelts_top (TREE_TYPE (range)),
9515 0);
9516 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9517 }
9518 else
9519 {
9520 /* If it is not an array, we must do a bit of magic. */
9521 tree id_begin, id_end;
9522 tree member_begin, member_end;
9523
9524 *begin = *end = error_mark_node;
9525
9526 id_begin = get_identifier ("begin");
9527 id_end = get_identifier ("end");
9528 member_begin = lookup_member (TREE_TYPE (range), id_begin,
2cbaacd9 9529 /*protect=*/2, /*want_type=*/false,
9530 tf_warning_or_error);
4e81a9f3 9531 member_end = lookup_member (TREE_TYPE (range), id_end,
2cbaacd9 9532 /*protect=*/2, /*want_type=*/false,
9533 tf_warning_or_error);
4e81a9f3 9534
9535 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9536 {
9537 /* Use the member functions. */
9538 if (member_begin != NULL_TREE)
9539 *begin = cp_parser_range_for_member_function (range, id_begin);
9540 else
9541 error ("range-based %<for%> expression of type %qT has an "
9542 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9543
9544 if (member_end != NULL_TREE)
9545 *end = cp_parser_range_for_member_function (range, id_end);
9546 else
9547 error ("range-based %<for%> expression of type %qT has a "
9548 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9549 }
9550 else
9551 {
9552 /* Use global functions with ADL. */
9553 VEC(tree,gc) *vec;
9554 vec = make_tree_vector ();
9555
9556 VEC_safe_push (tree, gc, vec, range);
9557
9558 member_begin = perform_koenig_lookup (id_begin, vec,
8411500a 9559 /*include_std=*/true,
9560 tf_warning_or_error);
4e81a9f3 9561 *begin = finish_call_expr (member_begin, &vec, false, true,
9562 tf_warning_or_error);
9563 member_end = perform_koenig_lookup (id_end, vec,
8411500a 9564 /*include_std=*/true,
9565 tf_warning_or_error);
4e81a9f3 9566 *end = finish_call_expr (member_end, &vec, false, true,
9567 tf_warning_or_error);
9568
9569 release_tree_vector (vec);
9570 }
9571
9572 /* Last common checks. */
9573 if (*begin == error_mark_node || *end == error_mark_node)
9574 {
9575 /* If one of the expressions is an error do no more checks. */
9576 *begin = *end = error_mark_node;
9577 return error_mark_node;
9578 }
9579 else
9580 {
9581 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9582 /* The unqualified type of the __begin and __end temporaries should
9583 be the same, as required by the multiple auto declaration. */
9584 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9585 error ("inconsistent begin/end types in range-based %<for%> "
9586 "statement: %qT and %qT",
9587 TREE_TYPE (*begin), TREE_TYPE (*end));
9588 return iter_type;
9589 }
9590 }
9591}
9592
9593/* Helper function for cp_parser_perform_range_for_lookup.
9594 Builds a tree for RANGE.IDENTIFIER(). */
9595
9596static tree
9597cp_parser_range_for_member_function (tree range, tree identifier)
9598{
9599 tree member, res;
9600 VEC(tree,gc) *vec;
9601
9602 member = finish_class_member_access_expr (range, identifier,
9603 false, tf_warning_or_error);
9604 if (member == error_mark_node)
9605 return error_mark_node;
9606
9607 vec = make_tree_vector ();
9608 res = finish_call_expr (member, &vec,
9609 /*disallow_virtual=*/false,
9610 /*koenig_p=*/false,
9611 tf_warning_or_error);
9612 release_tree_vector (vec);
9613 return res;
9614}
9dd72ec4 9615
0a3b29ad 9616/* Parse an iteration-statement.
9617
9618 iteration-statement:
9619 while ( condition ) statement
9620 do statement while ( expression ) ;
9621 for ( for-init-statement condition [opt] ; expression [opt] )
9622 statement
9623
9dd72ec4 9624 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
0a3b29ad 9625
9626static tree
45baea8b 9627cp_parser_iteration_statement (cp_parser* parser)
0a3b29ad 9628{
9629 cp_token *token;
9630 enum rid keyword;
9631 tree statement;
8487df40 9632 unsigned char in_statement;
0a3b29ad 9633
9634 /* Peek at the next token. */
c247dce0 9635 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
0a3b29ad 9636 if (!token)
9637 return error_mark_node;
9638
c3fbce20 9639 /* Remember whether or not we are already within an iteration
ccb84981 9640 statement. */
8487df40 9641 in_statement = parser->in_statement;
c3fbce20 9642
0a3b29ad 9643 /* See what kind of keyword it is. */
9644 keyword = token->keyword;
9645 switch (keyword)
9646 {
9647 case RID_WHILE:
9648 {
9649 tree condition;
9650
9651 /* Begin the while-statement. */
9652 statement = begin_while_stmt ();
9653 /* Look for the `('. */
c247dce0 9654 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 9655 /* Parse the condition. */
9656 condition = cp_parser_condition (parser);
9657 finish_while_stmt_cond (condition, statement);
9658 /* Look for the `)'. */
c247dce0 9659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 9660 /* Parse the dependent statement. */
8487df40 9661 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 9662 cp_parser_already_scoped_statement (parser);
8487df40 9663 parser->in_statement = in_statement;
0a3b29ad 9664 /* We're done with the while-statement. */
9665 finish_while_stmt (statement);
9666 }
9667 break;
9668
9669 case RID_DO:
9670 {
9671 tree expression;
9672
9673 /* Begin the do-statement. */
9674 statement = begin_do_stmt ();
9675 /* Parse the body of the do-statement. */
8487df40 9676 parser->in_statement = IN_ITERATION_STMT;
e534436e 9677 cp_parser_implicitly_scoped_statement (parser, NULL);
8487df40 9678 parser->in_statement = in_statement;
0a3b29ad 9679 finish_do_body (statement);
9680 /* Look for the `while' keyword. */
c247dce0 9681 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
0a3b29ad 9682 /* Look for the `('. */
c247dce0 9683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 9684 /* Parse the expression. */
98b326fd 9685 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 9686 /* We're done with the do-statement. */
9687 finish_do_stmt (expression, statement);
9688 /* Look for the `)'. */
c247dce0 9689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 9690 /* Look for the `;'. */
c247dce0 9691 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9692 }
9693 break;
9694
9695 case RID_FOR:
9696 {
0a3b29ad 9697 /* Look for the `('. */
c247dce0 9698 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 9699
fa7d5870 9700 statement = cp_parser_for (parser);
9dd72ec4 9701
0a3b29ad 9702 /* Look for the `)'. */
c247dce0 9703 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
207355ad 9704
0a3b29ad 9705 /* Parse the body of the for-statement. */
8487df40 9706 parser->in_statement = IN_ITERATION_STMT;
0a3b29ad 9707 cp_parser_already_scoped_statement (parser);
8487df40 9708 parser->in_statement = in_statement;
0a3b29ad 9709
9710 /* We're done with the for-statement. */
9711 finish_for_stmt (statement);
9712 }
9713 break;
9714
9715 default:
9716 cp_parser_error (parser, "expected iteration-statement");
9717 statement = error_mark_node;
9718 break;
9719 }
9720
9721 return statement;
9722}
9723
fa7d5870 9724/* Parse a for-init-statement or the declarator of a range-based-for.
9725 Returns true if a range-based-for declaration is seen.
0a3b29ad 9726
9727 for-init-statement:
9728 expression-statement
9729 simple-declaration */
9730
fa7d5870 9731static bool
9732cp_parser_for_init_statement (cp_parser* parser, tree *decl)
0a3b29ad 9733{
9734 /* If the next token is a `;', then we have an empty
755edffd 9735 expression-statement. Grammatically, this is also a
0a3b29ad 9736 simple-declaration, but an invalid one, because it does not
9737 declare anything. Therefore, if we did not handle this case
9738 specially, we would issue an error message about an invalid
9739 declaration. */
9740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9741 {
fa7d5870 9742 bool is_range_for = false;
9743 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9744
9745 parser->colon_corrects_to_scope_p = false;
9746
0a3b29ad 9747 /* We're going to speculatively look for a declaration, falling back
9748 to an expression, if necessary. */
9749 cp_parser_parse_tentatively (parser);
9750 /* Parse the declaration. */
9751 cp_parser_simple_declaration (parser,
fa7d5870 9752 /*function_definition_allowed_p=*/false,
9753 decl);
9754 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9755 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9756 {
9757 /* It is a range-for, consume the ':' */
9758 cp_lexer_consume_token (parser->lexer);
9759 is_range_for = true;
9760 if (cxx_dialect < cxx0x)
9761 {
9762 error_at (cp_lexer_peek_token (parser->lexer)->location,
4e81a9f3 9763 "range-based %<for%> loops are not allowed "
fa7d5870 9764 "in C++98 mode");
9765 *decl = error_mark_node;
9766 }
9767 }
9768 else
9769 /* The ';' is not consumed yet because we told
9770 cp_parser_simple_declaration not to. */
9771 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9772
9773 if (cp_parser_parse_definitely (parser))
9774 return is_range_for;
0a3b29ad 9775 /* If the tentative parse failed, then we shall need to look for an
9776 expression-statement. */
0a3b29ad 9777 }
fa7d5870 9778 /* If we are here, it is an expression-statement. */
10b4e9cc 9779 cp_parser_expression_statement (parser, NULL_TREE);
fa7d5870 9780 return false;
0a3b29ad 9781}
9782
9783/* Parse a jump-statement.
9784
9785 jump-statement:
9786 break ;
9787 continue ;
9788 return expression [opt] ;
f82f1250 9789 return braced-init-list ;
ccb84981 9790 goto identifier ;
0a3b29ad 9791
9792 GNU extension:
9793
9794 jump-statement:
9795 goto * expression ;
9796
c857cd60 9797 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
0a3b29ad 9798
9799static tree
45baea8b 9800cp_parser_jump_statement (cp_parser* parser)
0a3b29ad 9801{
9802 tree statement = error_mark_node;
9803 cp_token *token;
9804 enum rid keyword;
2672c56c 9805 unsigned char in_statement;
0a3b29ad 9806
9807 /* Peek at the next token. */
c247dce0 9808 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
0a3b29ad 9809 if (!token)
9810 return error_mark_node;
9811
9812 /* See what kind of keyword it is. */
9813 keyword = token->keyword;
9814 switch (keyword)
9815 {
9816 case RID_BREAK:
2672c56c 9817 in_statement = parser->in_statement & ~IN_IF_STMT;
9818 switch (in_statement)
c3fbce20 9819 {
8487df40 9820 case 0:
ccb59bb6 9821 error_at (token->location, "break statement not within loop or switch");
8487df40 9822 break;
9823 default:
2672c56c 9824 gcc_assert ((in_statement & IN_SWITCH_STMT)
9825 || in_statement == IN_ITERATION_STMT);
8487df40 9826 statement = finish_break_stmt ();
9827 break;
9828 case IN_OMP_BLOCK:
ccb59bb6 9829 error_at (token->location, "invalid exit from OpenMP structured block");
8487df40 9830 break;
9831 case IN_OMP_FOR:
ccb59bb6 9832 error_at (token->location, "break statement used with OpenMP for loop");
8487df40 9833 break;
c3fbce20 9834 }
c247dce0 9835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9836 break;
9837
9838 case RID_CONTINUE:
2672c56c 9839 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
c3fbce20 9840 {
8487df40 9841 case 0:
ccb59bb6 9842 error_at (token->location, "continue statement not within a loop");
8487df40 9843 break;
9844 case IN_ITERATION_STMT:
9845 case IN_OMP_FOR:
9846 statement = finish_continue_stmt ();
9847 break;
9848 case IN_OMP_BLOCK:
ccb59bb6 9849 error_at (token->location, "invalid exit from OpenMP structured block");
8487df40 9850 break;
9851 default:
9852 gcc_unreachable ();
c3fbce20 9853 }
c247dce0 9854 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9855 break;
9856
9857 case RID_RETURN:
9858 {
9859 tree expr;
f82f1250 9860 bool expr_non_constant_p;
0a3b29ad 9861
f82f1250 9862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9863 {
bf8d19fe 9864 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 9865 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9866 }
9867 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
98b326fd 9868 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 9869 else
f82f1250 9870 /* If the next token is a `;', then there is no
9871 expression. */
0a3b29ad 9872 expr = NULL_TREE;
9873 /* Build the return-statement. */
9874 statement = finish_return_stmt (expr);
9875 /* Look for the final `;'. */
c247dce0 9876 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9877 }
9878 break;
9879
9880 case RID_GOTO:
9881 /* Create the goto-statement. */
9882 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9883 {
9884 /* Issue a warning about this use of a GNU extension. */
29438999 9885 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
0a3b29ad 9886 /* Consume the '*' token. */
9887 cp_lexer_consume_token (parser->lexer);
9888 /* Parse the dependent expression. */
98b326fd 9889 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
0a3b29ad 9890 }
9891 else
9892 finish_goto_stmt (cp_parser_identifier (parser));
9893 /* Look for the final `;'. */
c247dce0 9894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 9895 break;
9896
9897 default:
9898 cp_parser_error (parser, "expected jump-statement");
9899 break;
9900 }
9901
9902 return statement;
9903}
9904
9905/* Parse a declaration-statement.
9906
9907 declaration-statement:
9908 block-declaration */
9909
9910static void
45baea8b 9911cp_parser_declaration_statement (cp_parser* parser)
0a3b29ad 9912{
3046c0a3 9913 void *p;
9914
9915 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9916 p = obstack_alloc (&declarator_obstack, 0);
9917
9918 /* Parse the block-declaration. */
0a3b29ad 9919 cp_parser_block_declaration (parser, /*statement_p=*/true);
9920
3046c0a3 9921 /* Free any declarators allocated. */
9922 obstack_free (&declarator_obstack, p);
9923
0a3b29ad 9924 /* Finish off the statement. */
9925 finish_stmt ();
9926}
9927
9928/* Some dependent statements (like `if (cond) statement'), are
9929 implicitly in their own scope. In other words, if the statement is
9930 a single statement (as opposed to a compound-statement), it is
9931 none-the-less treated as if it were enclosed in braces. Any
9932 declarations appearing in the dependent statement are out of scope
9933 after control passes that point. This function parses a statement,
9934 but ensures that is in its own scope, even if it is not a
ccb84981 9935 compound-statement.
0a3b29ad 9936
e534436e 9937 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9938 is a (possibly labeled) if statement which is not enclosed in
9939 braces and has an else clause. This is used to implement
9940 -Wparentheses.
9941
0a3b29ad 9942 Returns the new statement. */
9943
9944static tree
e534436e 9945cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
0a3b29ad 9946{
9947 tree statement;
9948
e534436e 9949 if (if_p != NULL)
9950 *if_p = false;
9951
50247dd9 9952 /* Mark if () ; with a special NOP_EXPR. */
9953 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9954 {
e60a6f7b 9955 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
50247dd9 9956 cp_lexer_consume_token (parser->lexer);
e60a6f7b 9957 statement = add_stmt (build_empty_stmt (loc));
50247dd9 9958 }
9959 /* if a compound is opened, we simply parse the statement directly. */
9960 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6bf5e78d 9961 statement = cp_parser_compound_statement (parser, NULL, false, false);
0a3b29ad 9962 /* If the token is not a `{', then we must take special action. */
50247dd9 9963 else
0a3b29ad 9964 {
9965 /* Create a compound-statement. */
2363ef00 9966 statement = begin_compound_stmt (0);
0a3b29ad 9967 /* Parse the dependent-statement. */
e534436e 9968 cp_parser_statement (parser, NULL_TREE, false, if_p);
0a3b29ad 9969 /* Finish the dummy compound-statement. */
68f8f8cc 9970 finish_compound_stmt (statement);
0a3b29ad 9971 }
0a3b29ad 9972
9973 /* Return the statement. */
9974 return statement;
9975}
9976
9977/* For some dependent statements (like `while (cond) statement'), we
9978 have already created a scope. Therefore, even if the dependent
9979 statement is a compound-statement, we do not want to create another
9980 scope. */
9981
9982static void
45baea8b 9983cp_parser_already_scoped_statement (cp_parser* parser)
0a3b29ad 9984{
2363ef00 9985 /* If the token is a `{', then we must take special action. */
9986 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
e534436e 9987 cp_parser_statement (parser, NULL_TREE, false, NULL);
2363ef00 9988 else
0a3b29ad 9989 {
2363ef00 9990 /* Avoid calling cp_parser_compound_statement, so that we
9991 don't create a new scope. Do everything else by hand. */
c247dce0 9992 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
1f529806 9993 /* If the next keyword is `__label__' we have a label declaration. */
9994 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9995 cp_parser_label_declaration (parser);
9996 /* Parse an (optional) statement-seq. */
b75b98aa 9997 cp_parser_statement_seq_opt (parser, NULL_TREE);
c247dce0 9998 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 9999 }
0a3b29ad 10000}
10001
10002/* Declarations [gram.dcl.dcl] */
10003
10004/* Parse an optional declaration-sequence.
10005
10006 declaration-seq:
10007 declaration
10008 declaration-seq declaration */
10009
10010static void
45baea8b 10011cp_parser_declaration_seq_opt (cp_parser* parser)
0a3b29ad 10012{
10013 while (true)
10014 {
10015 cp_token *token;
10016
10017 token = cp_lexer_peek_token (parser->lexer);
10018
10019 if (token->type == CPP_CLOSE_BRACE
b75b98aa 10020 || token->type == CPP_EOF
10021 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 10022 break;
10023
ccb84981 10024 if (token->type == CPP_SEMICOLON)
0a3b29ad 10025 {
10026 /* A declaration consisting of a single semicolon is
10027 invalid. Allow it unless we're being pedantic. */
0a3b29ad 10028 cp_lexer_consume_token (parser->lexer);
8864917d 10029 if (!in_system_header)
29438999 10030 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
0a3b29ad 10031 continue;
10032 }
10033
2e1f41a9 10034 /* If we're entering or exiting a region that's implicitly
93523877 10035 extern "C", modify the lang context appropriately. */
2e1f41a9 10036 if (!parser->implicit_extern_c && token->implicit_extern_c)
10037 {
10038 push_lang_context (lang_name_c);
10039 parser->implicit_extern_c = true;
10040 }
10041 else if (parser->implicit_extern_c && !token->implicit_extern_c)
10042 {
10043 pop_lang_context ();
10044 parser->implicit_extern_c = false;
10045 }
10046
3986d990 10047 if (token->type == CPP_PRAGMA)
10048 {
10049 /* A top-level declaration can consist solely of a #pragma.
10050 A nested declaration cannot, so this is done here and not
10051 in cp_parser_declaration. (A #pragma at block scope is
10052 handled in cp_parser_statement.) */
b75b98aa 10053 cp_parser_pragma (parser, pragma_external);
3986d990 10054 continue;
10055 }
10056
313a21c0 10057 /* Parse the declaration itself. */
0a3b29ad 10058 cp_parser_declaration (parser);
10059 }
10060}
10061
10062/* Parse a declaration.
10063
10064 declaration:
10065 block-declaration
10066 function-definition
10067 template-declaration
10068 explicit-instantiation
10069 explicit-specialization
10070 linkage-specification
ccb84981 10071 namespace-definition
2c6a8806 10072
10073 GNU extension:
10074
10075 declaration:
10076 __extension__ declaration */
0a3b29ad 10077
10078static void
45baea8b 10079cp_parser_declaration (cp_parser* parser)
0a3b29ad 10080{
10081 cp_token token1;
10082 cp_token token2;
2c6a8806 10083 int saved_pedantic;
3046c0a3 10084 void *p;
8774a917 10085 tree attributes = NULL_TREE;
2c6a8806 10086
10087 /* Check for the `__extension__' keyword. */
10088 if (cp_parser_extension_opt (parser, &saved_pedantic))
10089 {
10090 /* Parse the qualified declaration. */
10091 cp_parser_declaration (parser);
10092 /* Restore the PEDANTIC flag. */
10093 pedantic = saved_pedantic;
10094
10095 return;
10096 }
0a3b29ad 10097
10098 /* Try to figure out what kind of declaration is present. */
10099 token1 = *cp_lexer_peek_token (parser->lexer);
ccb84981 10100
0a3b29ad 10101 if (token1.type != CPP_EOF)
10102 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8102ba14 10103 else
0f803187 10104 {
10105 token2.type = CPP_EOF;
10106 token2.keyword = RID_MAX;
10107 }
0a3b29ad 10108
3046c0a3 10109 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10110 p = obstack_alloc (&declarator_obstack, 0);
10111
0a3b29ad 10112 /* If the next token is `extern' and the following token is a string
10113 literal, then we have a linkage specification. */
10114 if (token1.keyword == RID_EXTERN
244db24d 10115 && cp_parser_is_pure_string_literal (&token2))
0a3b29ad 10116 cp_parser_linkage_specification (parser);
10117 /* If the next token is `template', then we have either a template
10118 declaration, an explicit instantiation, or an explicit
10119 specialization. */
10120 else if (token1.keyword == RID_TEMPLATE)
10121 {
10122 /* `template <>' indicates a template specialization. */
10123 if (token2.type == CPP_LESS
10124 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10125 cp_parser_explicit_specialization (parser);
10126 /* `template <' indicates a template declaration. */
10127 else if (token2.type == CPP_LESS)
10128 cp_parser_template_declaration (parser, /*member_p=*/false);
10129 /* Anything else must be an explicit instantiation. */
10130 else
10131 cp_parser_explicit_instantiation (parser);
10132 }
10133 /* If the next token is `export', then we have a template
10134 declaration. */
10135 else if (token1.keyword == RID_EXPORT)
10136 cp_parser_template_declaration (parser, /*member_p=*/false);
10137 /* If the next token is `extern', 'static' or 'inline' and the one
10138 after that is `template', we have a GNU extended explicit
10139 instantiation directive. */
10140 else if (cp_parser_allow_gnu_extensions_p (parser)
10141 && (token1.keyword == RID_EXTERN
10142 || token1.keyword == RID_STATIC
10143 || token1.keyword == RID_INLINE)
10144 && token2.keyword == RID_TEMPLATE)
10145 cp_parser_explicit_instantiation (parser);
10146 /* If the next token is `namespace', check for a named or unnamed
10147 namespace definition. */
10148 else if (token1.keyword == RID_NAMESPACE
10149 && (/* A named namespace definition. */
10150 (token2.type == CPP_NAME
ccb84981 10151 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
799435d8 10152 != CPP_EQ))
0a3b29ad 10153 /* An unnamed namespace definition. */
30e98711 10154 || token2.type == CPP_OPEN_BRACE
10155 || token2.keyword == RID_ATTRIBUTE))
0a3b29ad 10156 cp_parser_namespace_definition (parser);
93635a8e 10157 /* An inline (associated) namespace definition. */
10158 else if (token1.keyword == RID_INLINE
10159 && token2.keyword == RID_NAMESPACE)
10160 cp_parser_namespace_definition (parser);
79408174 10161 /* Objective-C++ declaration/definition. */
10162 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10163 cp_parser_objc_declaration (parser, NULL_TREE);
10164 else if (c_dialect_objc ()
10165 && token1.keyword == RID_ATTRIBUTE
10166 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10167 cp_parser_objc_declaration (parser, attributes);
0a3b29ad 10168 /* We must have either a block declaration or a function
10169 definition. */
10170 else
10171 /* Try to parse a block-declaration, or a function-definition. */
10172 cp_parser_block_declaration (parser, /*statement_p=*/false);
3046c0a3 10173
10174 /* Free any declarators allocated. */
10175 obstack_free (&declarator_obstack, p);
0a3b29ad 10176}
10177
ccb84981 10178/* Parse a block-declaration.
0a3b29ad 10179
10180 block-declaration:
10181 simple-declaration
10182 asm-definition
10183 namespace-alias-definition
10184 using-declaration
ccb84981 10185 using-directive
0a3b29ad 10186
10187 GNU Extension:
10188
10189 block-declaration:
ccb84981 10190 __extension__ block-declaration
0a3b29ad 10191
7a05c4b1 10192 C++0x Extension:
10193
10194 block-declaration:
10195 static_assert-declaration
10196
755edffd 10197 If STATEMENT_P is TRUE, then this block-declaration is occurring as
0a3b29ad 10198 part of a declaration-statement. */
10199
10200static void
ccb84981 10201cp_parser_block_declaration (cp_parser *parser,
0a3b29ad 10202 bool statement_p)
10203{
10204 cp_token *token1;
10205 int saved_pedantic;
10206
10207 /* Check for the `__extension__' keyword. */
10208 if (cp_parser_extension_opt (parser, &saved_pedantic))
10209 {
10210 /* Parse the qualified declaration. */
10211 cp_parser_block_declaration (parser, statement_p);
10212 /* Restore the PEDANTIC flag. */
10213 pedantic = saved_pedantic;
10214
10215 return;
10216 }
10217
10218 /* Peek at the next token to figure out which kind of declaration is
10219 present. */
10220 token1 = cp_lexer_peek_token (parser->lexer);
10221
10222 /* If the next keyword is `asm', we have an asm-definition. */
10223 if (token1->keyword == RID_ASM)
10224 {
10225 if (statement_p)
10226 cp_parser_commit_to_tentative_parse (parser);
10227 cp_parser_asm_definition (parser);
10228 }
10229 /* If the next keyword is `namespace', we have a
10230 namespace-alias-definition. */
10231 else if (token1->keyword == RID_NAMESPACE)
10232 cp_parser_namespace_alias_definition (parser);
370478b1 10233 /* If the next keyword is `using', we have a
10234 using-declaration, a using-directive, or an alias-declaration. */
0a3b29ad 10235 else if (token1->keyword == RID_USING)
10236 {
10237 cp_token *token2;
10238
10239 if (statement_p)
10240 cp_parser_commit_to_tentative_parse (parser);
10241 /* If the token after `using' is `namespace', then we have a
10242 using-directive. */
10243 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10244 if (token2->keyword == RID_NAMESPACE)
10245 cp_parser_using_directive (parser);
370478b1 10246 /* If the second token after 'using' is '=', then we have an
10247 alias-declaration. */
10248 else if (cxx_dialect >= cxx0x
10249 && token2->type == CPP_NAME
10250 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10251 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10252 == RID_ATTRIBUTE)))
10253 cp_parser_alias_declaration (parser);
0a3b29ad 10254 /* Otherwise, it's a using-declaration. */
10255 else
da2a3271 10256 cp_parser_using_declaration (parser,
10257 /*access_declaration_p=*/false);
0a3b29ad 10258 }
3dac447c 10259 /* If the next keyword is `__label__' we have a misplaced label
10260 declaration. */
0a3b29ad 10261 else if (token1->keyword == RID_LABEL)
10262 {
3dac447c 10263 cp_lexer_consume_token (parser->lexer);
ccb59bb6 10264 error_at (token1->location, "%<__label__%> not at the beginning of a block");
3dac447c 10265 cp_parser_skip_to_end_of_statement (parser);
10266 /* If the next token is now a `;', consume it. */
10267 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10268 cp_lexer_consume_token (parser->lexer);
0a3b29ad 10269 }
7a05c4b1 10270 /* If the next token is `static_assert' we have a static assertion. */
10271 else if (token1->keyword == RID_STATIC_ASSERT)
10272 cp_parser_static_assert (parser, /*member_p=*/false);
0a3b29ad 10273 /* Anything else must be a simple-declaration. */
10274 else
fa7d5870 10275 cp_parser_simple_declaration (parser, !statement_p,
10276 /*maybe_range_for_decl*/NULL);
0a3b29ad 10277}
10278
10279/* Parse a simple-declaration.
10280
10281 simple-declaration:
ccb84981 10282 decl-specifier-seq [opt] init-declarator-list [opt] ;
0a3b29ad 10283
10284 init-declarator-list:
10285 init-declarator
ccb84981 10286 init-declarator-list , init-declarator
0a3b29ad 10287
755edffd 10288 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
fa7d5870 10289 function-definition as a simple-declaration.
10290
10291 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10292 parsed declaration if it is an uninitialized single declarator not followed
10293 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10294 if present, will not be consumed. */
0a3b29ad 10295
10296static void
ccb84981 10297cp_parser_simple_declaration (cp_parser* parser,
fa7d5870 10298 bool function_definition_allowed_p,
10299 tree *maybe_range_for_decl)
0a3b29ad 10300{
4b9b2871 10301 cp_decl_specifier_seq decl_specifiers;
8172be22 10302 int declares_class_or_enum;
0a3b29ad 10303 bool saw_declarator;
10304
fa7d5870 10305 if (maybe_range_for_decl)
10306 *maybe_range_for_decl = NULL_TREE;
10307
0a3b29ad 10308 /* Defer access checks until we know what is being declared; the
10309 checks for names appearing in the decl-specifier-seq should be
10310 done as if we were in the scope of the thing being declared. */
4f62c42e 10311 push_deferring_access_checks (dk_deferred);
9b57b06b 10312
0a3b29ad 10313 /* Parse the decl-specifier-seq. We have to keep track of whether
10314 or not the decl-specifier-seq declares a named class or
10315 enumeration type, since that is the only case in which the
ccb84981 10316 init-declarator-list is allowed to be empty.
0a3b29ad 10317
10318 [dcl.dcl]
10319
10320 In a simple-declaration, the optional init-declarator-list can be
10321 omitted only when declaring a class or enumeration, that is when
10322 the decl-specifier-seq contains either a class-specifier, an
10323 elaborated-type-specifier, or an enum-specifier. */
4b9b2871 10324 cp_parser_decl_specifier_seq (parser,
10325 CP_PARSER_FLAGS_OPTIONAL,
10326 &decl_specifiers,
10327 &declares_class_or_enum);
0a3b29ad 10328 /* We no longer need to defer access checks. */
9b57b06b 10329 stop_deferring_access_checks ();
7488d745 10330
878870b4 10331 /* In a block scope, a valid declaration must always have a
10332 decl-specifier-seq. By not trying to parse declarators, we can
10333 resolve the declaration/expression ambiguity more quickly. */
207355ad 10334 if (!function_definition_allowed_p
4b9b2871 10335 && !decl_specifiers.any_specifiers_p)
878870b4 10336 {
10337 cp_parser_error (parser, "expected declaration");
10338 goto done;
10339 }
10340
954ad420 10341 /* If the next two tokens are both identifiers, the code is
10342 erroneous. The usual cause of this situation is code like:
10343
10344 T t;
10345
10346 where "T" should name a type -- but does not. */
67484828 10347 if (!decl_specifiers.any_type_specifiers_p
882d3c4f 10348 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
954ad420 10349 {
4f62c42e 10350 /* If parsing tentatively, we should commit; we really are
954ad420 10351 looking at a declaration. */
10352 cp_parser_commit_to_tentative_parse (parser);
10353 /* Give up. */
878870b4 10354 goto done;
954ad420 10355 }
9031d10b 10356
d1a64350 10357 /* If we have seen at least one decl-specifier, and the next token
10358 is not a parenthesis, then we must be looking at a declaration.
10359 (After "int (" we might be looking at a functional cast.) */
9031d10b 10360 if (decl_specifiers.any_specifiers_p
f82f1250 10361 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8659a722 10362 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10363 && !cp_parser_error_occurred (parser))
d1a64350 10364 cp_parser_commit_to_tentative_parse (parser);
954ad420 10365
0a3b29ad 10366 /* Keep going until we hit the `;' at the end of the simple
10367 declaration. */
10368 saw_declarator = false;
ccb84981 10369 while (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 10370 CPP_SEMICOLON))
10371 {
10372 cp_token *token;
10373 bool function_definition_p;
8172be22 10374 tree decl;
0a3b29ad 10375
b60e927a 10376 if (saw_declarator)
10377 {
10378 /* If we are processing next declarator, coma is expected */
10379 token = cp_lexer_peek_token (parser->lexer);
10380 gcc_assert (token->type == CPP_COMMA);
10381 cp_lexer_consume_token (parser->lexer);
fa7d5870 10382 if (maybe_range_for_decl)
10383 *maybe_range_for_decl = error_mark_node;
b60e927a 10384 }
10385 else
10386 saw_declarator = true;
10387
0a3b29ad 10388 /* Parse the init-declarator. */
4b9b2871 10389 decl = cp_parser_init_declarator (parser, &decl_specifiers,
3369eb76 10390 /*checks=*/NULL,
8172be22 10391 function_definition_allowed_p,
10392 /*member_p=*/false,
10393 declares_class_or_enum,
fa7d5870 10394 &function_definition_p,
10395 maybe_range_for_decl);
7e9a6a16 10396 /* If an error occurred while parsing tentatively, exit quickly.
10397 (That usually happens when in the body of a function; each
10398 statement is treated as a declaration-statement until proven
10399 otherwise.) */
10400 if (cp_parser_error_occurred (parser))
878870b4 10401 goto done;
0a3b29ad 10402 /* Handle function definitions specially. */
10403 if (function_definition_p)
10404 {
10405 /* If the next token is a `,', then we are probably
10406 processing something like:
10407
10408 void f() {}, *p;
10409
10410 which is erroneous. */
10411 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
ad9ae192 10412 {
10413 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 10414 error_at (token->location,
10415 "mixing"
10416 " declarations and function-definitions is forbidden");
ad9ae192 10417 }
0a3b29ad 10418 /* Otherwise, we're done with the list of declarators. */
10419 else
7488d745 10420 {
9b57b06b 10421 pop_deferring_access_checks ();
7488d745 10422 return;
10423 }
0a3b29ad 10424 }
fa7d5870 10425 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10426 *maybe_range_for_decl = decl;
0a3b29ad 10427 /* The next token should be either a `,' or a `;'. */
10428 token = cp_lexer_peek_token (parser->lexer);
10429 /* If it's a `,', there are more declarators to come. */
10430 if (token->type == CPP_COMMA)
b60e927a 10431 /* will be consumed next time around */;
0a3b29ad 10432 /* If it's a `;', we are done. */
fa7d5870 10433 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
0a3b29ad 10434 break;
10435 /* Anything else is an error. */
10436 else
10437 {
d1a64350 10438 /* If we have already issued an error message we don't need
10439 to issue another one. */
10440 if (decl != error_mark_node
efcbcf83 10441 || cp_parser_uncommitted_to_tentative_parse_p (parser))
a2c5b975 10442 cp_parser_error (parser, "expected %<,%> or %<;%>");
0a3b29ad 10443 /* Skip tokens until we reach the end of the statement. */
10444 cp_parser_skip_to_end_of_statement (parser);
2c584053 10445 /* If the next token is now a `;', consume it. */
10446 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10447 cp_lexer_consume_token (parser->lexer);
878870b4 10448 goto done;
0a3b29ad 10449 }
10450 /* After the first time around, a function-definition is not
10451 allowed -- even if it was OK at first. For example:
10452
653e5405 10453 int i, f() {}
0a3b29ad 10454
653e5405 10455 is not valid. */
0a3b29ad 10456 function_definition_allowed_p = false;
10457 }
10458
10459 /* Issue an error message if no declarators are present, and the
10460 decl-specifier-seq does not itself declare a class or
10461 enumeration. */
10462 if (!saw_declarator)
10463 {
10464 if (cp_parser_declares_only_class_p (parser))
4b9b2871 10465 shadow_tag (&decl_specifiers);
0a3b29ad 10466 /* Perform any deferred access checks. */
9b57b06b 10467 perform_deferred_access_checks ();
0a3b29ad 10468 }
10469
10470 /* Consume the `;'. */
fa7d5870 10471 if (!maybe_range_for_decl)
10472 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 10473
878870b4 10474 done:
10475 pop_deferring_access_checks ();
0a3b29ad 10476}
10477
10478/* Parse a decl-specifier-seq.
10479
10480 decl-specifier-seq:
10481 decl-specifier-seq [opt] decl-specifier
10482
10483 decl-specifier:
10484 storage-class-specifier
10485 type-specifier
10486 function-specifier
10487 friend
ccb84981 10488 typedef
0a3b29ad 10489
10490 GNU Extension:
10491
e67a67ea 10492 decl-specifier:
10493 attributes
0a3b29ad 10494
4b9b2871 10495 Set *DECL_SPECS to a representation of the decl-specifier-seq.
0a3b29ad 10496
fb871e92 10497 The parser flags FLAGS is used to control type-specifier parsing.
8172be22 10498
10499 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8e594c09 10500 flags:
8172be22 10501
10502 1: one of the decl-specifiers is an elaborated-type-specifier
653e5405 10503 (i.e., a type declaration)
8172be22 10504 2: one of the decl-specifiers is an enum-specifier or a
653e5405 10505 class-specifier (i.e., a type definition)
207355ad 10506
8172be22 10507 */
0a3b29ad 10508
4b9b2871 10509static void
ccb84981 10510cp_parser_decl_specifier_seq (cp_parser* parser,
4b9b2871 10511 cp_parser_flags flags,
10512 cp_decl_specifier_seq *decl_specs,
8172be22 10513 int* declares_class_or_enum)
0a3b29ad 10514{
87647325 10515 bool constructor_possible_p = !parser->in_declarator_p;
ad9ae192 10516 cp_token *start_token = NULL;
a60f3e81 10517 cp_decl_spec ds;
ccb84981 10518
4b9b2871 10519 /* Clear DECL_SPECS. */
10520 clear_decl_specs (decl_specs);
10521
0a3b29ad 10522 /* Assume no class or enumeration type is declared. */
8172be22 10523 *declares_class_or_enum = 0;
0a3b29ad 10524
0a3b29ad 10525 /* Keep reading specifiers until there are no more to read. */
10526 while (true)
10527 {
0a3b29ad 10528 bool constructor_p;
4b9b2871 10529 bool found_decl_spec;
0a3b29ad 10530 cp_token *token;
a60f3e81 10531 ds = ds_last;
0a3b29ad 10532
10533 /* Peek at the next token. */
10534 token = cp_lexer_peek_token (parser->lexer);
ad9ae192 10535
10536 /* Save the first token of the decl spec list for error
10537 reporting. */
10538 if (!start_token)
10539 start_token = token;
0a3b29ad 10540 /* Handle attributes. */
10541 if (token->keyword == RID_ATTRIBUTE)
10542 {
10543 /* Parse the attributes. */
207355ad 10544 decl_specs->attributes
4b9b2871 10545 = chainon (decl_specs->attributes,
10546 cp_parser_attributes_opt (parser));
a60f3e81 10547 if (decl_specs->locations[ds_attribute] == 0)
10548 decl_specs->locations[ds_attribute] = token->location;
0a3b29ad 10549 continue;
10550 }
4b9b2871 10551 /* Assume we will find a decl-specifier keyword. */
10552 found_decl_spec = true;
0a3b29ad 10553 /* If the next token is an appropriate keyword, we can simply
10554 add it to the list. */
10555 switch (token->keyword)
10556 {
0a3b29ad 10557 /* decl-specifier:
17814aca 10558 friend
10559 constexpr */
4b9b2871 10560 case RID_FRIEND:
acd412e7 10561 if (!at_class_scope_p ())
10562 {
ccb59bb6 10563 error_at (token->location, "%<friend%> used outside of class");
acd412e7 10564 cp_lexer_purge_token (parser->lexer);
10565 }
10566 else
10567 {
a60f3e81 10568 ds = ds_friend;
acd412e7 10569 /* Consume the token. */
10570 cp_lexer_consume_token (parser->lexer);
10571 }
0a3b29ad 10572 break;
10573
17814aca 10574 case RID_CONSTEXPR:
a60f3e81 10575 ds = ds_constexpr;
17814aca 10576 cp_lexer_consume_token (parser->lexer);
10577 break;
10578
0a3b29ad 10579 /* function-specifier:
10580 inline
10581 virtual
10582 explicit */
10583 case RID_INLINE:
10584 case RID_VIRTUAL:
10585 case RID_EXPLICIT:
4b9b2871 10586 cp_parser_function_specifier_opt (parser, decl_specs);
0a3b29ad 10587 break;
ccb84981 10588
0a3b29ad 10589 /* decl-specifier:
10590 typedef */
10591 case RID_TYPEDEF:
a60f3e81 10592 ds = ds_typedef;
0a3b29ad 10593 /* Consume the token. */
10594 cp_lexer_consume_token (parser->lexer);
b3c48b5d 10595 /* A constructor declarator cannot appear in a typedef. */
10596 constructor_possible_p = false;
f3b70d2f 10597 /* The "typedef" keyword can only occur in a declaration; we
10598 may as well commit at this point. */
10599 cp_parser_commit_to_tentative_parse (parser);
ceec99b9 10600
10601 if (decl_specs->storage_class != sc_none)
10602 decl_specs->conflicting_specifiers_p = true;
0a3b29ad 10603 break;
10604
10605 /* storage-class-specifier:
10606 auto
10607 register
10608 static
10609 extern
ccb84981 10610 mutable
0a3b29ad 10611
653e5405 10612 GNU Extension:
0a3b29ad 10613 thread */
10614 case RID_AUTO:
45b44d0a 10615 if (cxx_dialect == cxx98)
10616 {
46f4817e 10617 /* Consume the token. */
10618 cp_lexer_consume_token (parser->lexer);
10619
45b44d0a 10620 /* Complain about `auto' as a storage specifier, if
10621 we're complaining about C++0x compatibility. */
ccb59bb6 10622 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
d1d11619 10623 " changes meaning in C++11; please remove it");
45b44d0a 10624
10625 /* Set the storage class anyway. */
ad9ae192 10626 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10627 token->location);
45b44d0a 10628 }
46f4817e 10629 else
10630 /* C++0x auto type-specifier. */
10631 found_decl_spec = false;
45b44d0a 10632 break;
10633
0a3b29ad 10634 case RID_REGISTER:
10635 case RID_STATIC:
10636 case RID_EXTERN:
10637 case RID_MUTABLE:
4b9b2871 10638 /* Consume the token. */
10639 cp_lexer_consume_token (parser->lexer);
ad9ae192 10640 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10641 token->location);
4b9b2871 10642 break;
0a3b29ad 10643 case RID_THREAD:
4b9b2871 10644 /* Consume the token. */
a60f3e81 10645 ds = ds_thread;
4b9b2871 10646 cp_lexer_consume_token (parser->lexer);
0a3b29ad 10647 break;
ccb84981 10648
0a3b29ad 10649 default:
4b9b2871 10650 /* We did not yet find a decl-specifier yet. */
10651 found_decl_spec = false;
0a3b29ad 10652 break;
10653 }
10654
ca63c29a 10655 if (found_decl_spec
10656 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10657 && token->keyword != RID_CONSTEXPR)
10658 error ("decl-specifier invalid in condition");
10659
a60f3e81 10660 if (ds != ds_last)
10661 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
10662
0a3b29ad 10663 /* Constructors are a special case. The `S' in `S()' is not a
10664 decl-specifier; it is the beginning of the declarator. */
207355ad 10665 constructor_p
4b9b2871 10666 = (!found_decl_spec
10667 && constructor_possible_p
207355ad 10668 && (cp_parser_constructor_declarator_p
a60f3e81 10669 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
0a3b29ad 10670
10671 /* If we don't have a DECL_SPEC yet, then we must be looking at
10672 a type-specifier. */
4b9b2871 10673 if (!found_decl_spec && !constructor_p)
0a3b29ad 10674 {
8172be22 10675 int decl_spec_declares_class_or_enum;
0a3b29ad 10676 bool is_cv_qualifier;
4b9b2871 10677 tree type_spec;
0a3b29ad 10678
4b9b2871 10679 type_spec
0a3b29ad 10680 = cp_parser_type_specifier (parser, flags,
4b9b2871 10681 decl_specs,
0a3b29ad 10682 /*is_declaration=*/true,
10683 &decl_spec_declares_class_or_enum,
10684 &is_cv_qualifier);
0a3b29ad 10685 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10686
10687 /* If this type-specifier referenced a user-defined type
10688 (a typedef, class-name, etc.), then we can't allow any
10689 more such type-specifiers henceforth.
10690
10691 [dcl.spec]
10692
10693 The longest sequence of decl-specifiers that could
10694 possibly be a type name is taken as the
10695 decl-specifier-seq of a declaration. The sequence shall
10696 be self-consistent as described below.
10697
10698 [dcl.type]
10699
10700 As a general rule, at most one type-specifier is allowed
10701 in the complete decl-specifier-seq of a declaration. The
10702 only exceptions are the following:
10703
10704 -- const or volatile can be combined with any other
ccb84981 10705 type-specifier.
0a3b29ad 10706
10707 -- signed or unsigned can be combined with char, long,
10708 short, or int.
10709
10710 -- ..
10711
10712 Example:
10713
10714 typedef char* Pc;
10715 void g (const int Pc);
10716
10717 Here, Pc is *not* part of the decl-specifier seq; it's
10718 the declarator. Therefore, once we see a type-specifier
10719 (other than a cv-qualifier), we forbid any additional
10720 user-defined types. We *do* still allow things like `int
10721 int' to be considered a decl-specifier-seq, and issue the
10722 error message later. */
4b9b2871 10723 if (type_spec && !is_cv_qualifier)
0a3b29ad 10724 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
b3c48b5d 10725 /* A constructor declarator cannot follow a type-specifier. */
4b9b2871 10726 if (type_spec)
0a3b29ad 10727 {
4b9b2871 10728 constructor_possible_p = false;
10729 found_decl_spec = true;
67484828 10730 if (!is_cv_qualifier)
10731 decl_specs->any_type_specifiers_p = true;
0a3b29ad 10732 }
0a3b29ad 10733 }
10734
4b9b2871 10735 /* If we still do not have a DECL_SPEC, then there are no more
10736 decl-specifiers. */
10737 if (!found_decl_spec)
10738 break;
0a3b29ad 10739
4b9b2871 10740 decl_specs->any_specifiers_p = true;
0a3b29ad 10741 /* After we see one decl-specifier, further decl-specifiers are
10742 always optional. */
10743 flags |= CP_PARSER_FLAGS_OPTIONAL;
10744 }
10745
2822900d 10746 /* Don't allow a friend specifier with a class definition. */
a60f3e81 10747 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
4b9b2871 10748 && (*declares_class_or_enum & 2))
a60f3e81 10749 error_at (decl_specs->locations[ds_friend],
ccb59bb6 10750 "class definition may not be declared a friend");
0a3b29ad 10751}
10752
ccb84981 10753/* Parse an (optional) storage-class-specifier.
0a3b29ad 10754
10755 storage-class-specifier:
10756 auto
10757 register
10758 static
10759 extern
ccb84981 10760 mutable
0a3b29ad 10761
10762 GNU Extension:
10763
10764 storage-class-specifier:
10765 thread
10766
10767 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
ccb84981 10768
0a3b29ad 10769static tree
45baea8b 10770cp_parser_storage_class_specifier_opt (cp_parser* parser)
0a3b29ad 10771{
10772 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10773 {
10774 case RID_AUTO:
45b44d0a 10775 if (cxx_dialect != cxx98)
10776 return NULL_TREE;
10777 /* Fall through for C++98. */
10778
0a3b29ad 10779 case RID_REGISTER:
10780 case RID_STATIC:
10781 case RID_EXTERN:
10782 case RID_MUTABLE:
10783 case RID_THREAD:
10784 /* Consume the token. */
3369eb76 10785 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 10786
10787 default:
10788 return NULL_TREE;
10789 }
10790}
10791
ccb84981 10792/* Parse an (optional) function-specifier.
0a3b29ad 10793
10794 function-specifier:
10795 inline
10796 virtual
10797 explicit
10798
4b9b2871 10799 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10800 Updates DECL_SPECS, if it is non-NULL. */
ccb84981 10801
0a3b29ad 10802static tree
4b9b2871 10803cp_parser_function_specifier_opt (cp_parser* parser,
10804 cp_decl_specifier_seq *decl_specs)
0a3b29ad 10805{
ad9ae192 10806 cp_token *token = cp_lexer_peek_token (parser->lexer);
10807 switch (token->keyword)
0a3b29ad 10808 {
10809 case RID_INLINE:
a60f3e81 10810 set_and_check_decl_spec_loc (decl_specs, ds_inline, token->location);
4b9b2871 10811 break;
10812
0a3b29ad 10813 case RID_VIRTUAL:
5f69415d 10814 /* 14.5.2.3 [temp.mem]
074ab442 10815
10816 A member function template shall not be virtual. */
5f69415d 10817 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
ccb59bb6 10818 error_at (token->location, "templates may not be %<virtual%>");
a60f3e81 10819 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token->location);
4b9b2871 10820 break;
10821
0a3b29ad 10822 case RID_EXPLICIT:
a60f3e81 10823 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token->location);
4b9b2871 10824 break;
0a3b29ad 10825
10826 default:
10827 return NULL_TREE;
10828 }
4b9b2871 10829
10830 /* Consume the token. */
3369eb76 10831 return cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 10832}
10833
10834/* Parse a linkage-specification.
10835
10836 linkage-specification:
10837 extern string-literal { declaration-seq [opt] }
10838 extern string-literal declaration */
10839
10840static void
45baea8b 10841cp_parser_linkage_specification (cp_parser* parser)
0a3b29ad 10842{
0a3b29ad 10843 tree linkage;
10844
10845 /* Look for the `extern' keyword. */
c247dce0 10846 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
0a3b29ad 10847
00d26680 10848 /* Look for the string-literal. */
10849 linkage = cp_parser_string_literal (parser, false, false);
0a3b29ad 10850
10851 /* Transform the literal into an identifier. If the literal is a
10852 wide-character string, or contains embedded NULs, then we can't
10853 handle it as the user wants. */
00d26680 10854 if (strlen (TREE_STRING_POINTER (linkage))
10855 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
0a3b29ad 10856 {
10857 cp_parser_error (parser, "invalid linkage-specification");
10858 /* Assume C++ linkage. */
00d26680 10859 linkage = lang_name_cplusplus;
0a3b29ad 10860 }
0a3b29ad 10861 else
00d26680 10862 linkage = get_identifier (TREE_STRING_POINTER (linkage));
0a3b29ad 10863
10864 /* We're now using the new linkage. */
10865 push_lang_context (linkage);
10866
10867 /* If the next token is a `{', then we're using the first
10868 production. */
10869 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10870 {
10871 /* Consume the `{' token. */
10872 cp_lexer_consume_token (parser->lexer);
10873 /* Parse the declarations. */
10874 cp_parser_declaration_seq_opt (parser);
10875 /* Look for the closing `}'. */
c247dce0 10876 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 10877 }
10878 /* Otherwise, there's just one declaration. */
10879 else
10880 {
10881 bool saved_in_unbraced_linkage_specification_p;
10882
ccb84981 10883 saved_in_unbraced_linkage_specification_p
0a3b29ad 10884 = parser->in_unbraced_linkage_specification_p;
10885 parser->in_unbraced_linkage_specification_p = true;
0a3b29ad 10886 cp_parser_declaration (parser);
ccb84981 10887 parser->in_unbraced_linkage_specification_p
0a3b29ad 10888 = saved_in_unbraced_linkage_specification_p;
10889 }
10890
10891 /* We're done with the linkage-specification. */
10892 pop_lang_context ();
10893}
10894
7a05c4b1 10895/* Parse a static_assert-declaration.
10896
10897 static_assert-declaration:
10898 static_assert ( constant-expression , string-literal ) ;
10899
10900 If MEMBER_P, this static_assert is a class member. */
10901
10902static void
10903cp_parser_static_assert(cp_parser *parser, bool member_p)
10904{
10905 tree condition;
10906 tree message;
10907 cp_token *token;
10908 location_t saved_loc;
cfa61f84 10909 bool dummy;
7a05c4b1 10910
10911 /* Peek at the `static_assert' token so we can keep track of exactly
10912 where the static assertion started. */
10913 token = cp_lexer_peek_token (parser->lexer);
10914 saved_loc = token->location;
10915
10916 /* Look for the `static_assert' keyword. */
10917 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
c247dce0 10918 RT_STATIC_ASSERT))
7a05c4b1 10919 return;
10920
10921 /* We know we are in a static assertion; commit to any tentative
10922 parse. */
10923 if (cp_parser_parsing_tentatively (parser))
10924 cp_parser_commit_to_tentative_parse (parser);
10925
10926 /* Parse the `(' starting the static assertion condition. */
c247dce0 10927 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7a05c4b1 10928
cfa61f84 10929 /* Parse the constant-expression. Allow a non-constant expression
10930 here in order to give better diagnostics in finish_static_assert. */
7a05c4b1 10931 condition =
10932 cp_parser_constant_expression (parser,
cfa61f84 10933 /*allow_non_constant_p=*/true,
10934 /*non_constant_p=*/&dummy);
7a05c4b1 10935
10936 /* Parse the separating `,'. */
c247dce0 10937 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7a05c4b1 10938
10939 /* Parse the string-literal message. */
10940 message = cp_parser_string_literal (parser,
10941 /*translate=*/false,
10942 /*wide_ok=*/true);
10943
10944 /* A `)' completes the static assertion. */
c247dce0 10945 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7a05c4b1 10946 cp_parser_skip_to_closing_parenthesis (parser,
10947 /*recovering=*/true,
10948 /*or_comma=*/false,
10949 /*consume_paren=*/true);
10950
10951 /* A semicolon terminates the declaration. */
c247dce0 10952 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7a05c4b1 10953
10954 /* Complete the static assertion, which may mean either processing
10955 the static assert now or saving it for template instantiation. */
10956 finish_static_assert (condition, message, saved_loc, member_p);
10957}
10958
34da8800 10959/* Parse a `decltype' type. Returns the type.
10960
10961 simple-type-specifier:
10962 decltype ( expression ) */
10963
10964static tree
10965cp_parser_decltype (cp_parser *parser)
10966{
10967 tree expr;
10968 bool id_expression_or_member_access_p = false;
10969 const char *saved_message;
10970 bool saved_integral_constant_expression_p;
10971 bool saved_non_integral_constant_expression_p;
ad9ae192 10972 cp_token *id_expr_start_token;
07b8f133 10973 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10974
10975 if (start_token->type == CPP_DECLTYPE)
10976 {
10977 /* Already parsed. */
10978 cp_lexer_consume_token (parser->lexer);
10979 return start_token->u.value;
10980 }
34da8800 10981
10982 /* Look for the `decltype' token. */
c247dce0 10983 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
34da8800 10984 return error_mark_node;
10985
10986 /* Types cannot be defined in a `decltype' expression. Save away the
10987 old message. */
10988 saved_message = parser->type_definition_forbidden_message;
10989
10990 /* And create the new one. */
10991 parser->type_definition_forbidden_message
ca82e026 10992 = G_("types may not be defined in %<decltype%> expressions");
34da8800 10993
10994 /* The restrictions on constant-expressions do not apply inside
10995 decltype expressions. */
10996 saved_integral_constant_expression_p
10997 = parser->integral_constant_expression_p;
10998 saved_non_integral_constant_expression_p
10999 = parser->non_integral_constant_expression_p;
11000 parser->integral_constant_expression_p = false;
11001
11002 /* Do not actually evaluate the expression. */
48d94ede 11003 ++cp_unevaluated_operand;
11004
11005 /* Do not warn about problems with the expression. */
11006 ++c_inhibit_evaluation_warnings;
34da8800 11007
11008 /* Parse the opening `('. */
c247dce0 11009 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
4d3e6d58 11010 return error_mark_node;
34da8800 11011
11012 /* First, try parsing an id-expression. */
ad9ae192 11013 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
34da8800 11014 cp_parser_parse_tentatively (parser);
11015 expr = cp_parser_id_expression (parser,
11016 /*template_keyword_p=*/false,
11017 /*check_dependency_p=*/true,
11018 /*template_p=*/NULL,
11019 /*declarator_p=*/false,
11020 /*optional_p=*/false);
11021
11022 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11023 {
11024 bool non_integral_constant_expression_p = false;
11025 tree id_expression = expr;
11026 cp_id_kind idk;
11027 const char *error_msg;
11028
4d3e6d58 11029 if (TREE_CODE (expr) == IDENTIFIER_NODE)
11030 /* Lookup the name we got back from the id-expression. */
11031 expr = cp_parser_lookup_name (parser, expr,
11032 none_type,
11033 /*is_template=*/false,
11034 /*is_namespace=*/false,
11035 /*check_dependency=*/true,
ad9ae192 11036 /*ambiguous_decls=*/NULL,
11037 id_expr_start_token->location);
4d3e6d58 11038
711178fb 11039 if (expr
34da8800 11040 && expr != error_mark_node
11041 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11042 && TREE_CODE (expr) != TYPE_DECL
711178fb 11043 && (TREE_CODE (expr) != BIT_NOT_EXPR
11044 || !TYPE_P (TREE_OPERAND (expr, 0)))
34da8800 11045 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11046 {
11047 /* Complete lookup of the id-expression. */
11048 expr = (finish_id_expression
11049 (id_expression, expr, parser->scope, &idk,
11050 /*integral_constant_expression_p=*/false,
11051 /*allow_non_integral_constant_expression_p=*/true,
11052 &non_integral_constant_expression_p,
11053 /*template_p=*/false,
11054 /*done=*/true,
11055 /*address_p=*/false,
11056 /*template_arg_p=*/false,
ad9ae192 11057 &error_msg,
11058 id_expr_start_token->location));
34da8800 11059
11060 if (expr == error_mark_node)
11061 /* We found an id-expression, but it was something that we
11062 should not have found. This is an error, not something
11063 we can recover from, so note that we found an
11064 id-expression and we'll recover as gracefully as
11065 possible. */
11066 id_expression_or_member_access_p = true;
11067 }
11068
11069 if (expr
11070 && expr != error_mark_node
11071 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11072 /* We have an id-expression. */
11073 id_expression_or_member_access_p = true;
11074 }
11075
11076 if (!id_expression_or_member_access_p)
11077 {
11078 /* Abort the id-expression parse. */
11079 cp_parser_abort_tentative_parse (parser);
11080
11081 /* Parsing tentatively, again. */
11082 cp_parser_parse_tentatively (parser);
11083
11084 /* Parse a class member access. */
11085 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11086 /*cast_p=*/false,
98b326fd 11087 /*member_access_only_p=*/true, NULL);
34da8800 11088
11089 if (expr
11090 && expr != error_mark_node
11091 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11092 /* We have an id-expression. */
11093 id_expression_or_member_access_p = true;
11094 }
11095
11096 if (id_expression_or_member_access_p)
11097 /* We have parsed the complete id-expression or member access. */
11098 cp_parser_parse_definitely (parser);
11099 else
11100 {
3a39874d 11101 bool saved_greater_than_is_operator_p;
11102
34da8800 11103 /* Abort our attempt to parse an id-expression or member access
11104 expression. */
11105 cp_parser_abort_tentative_parse (parser);
11106
3a39874d 11107 /* Within a parenthesized expression, a `>' token is always
11108 the greater-than operator. */
11109 saved_greater_than_is_operator_p
11110 = parser->greater_than_is_operator_p;
11111 parser->greater_than_is_operator_p = true;
11112
34da8800 11113 /* Parse a full expression. */
98b326fd 11114 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
3a39874d 11115
11116 /* The `>' token might be the end of a template-id or
11117 template-parameter-list now. */
11118 parser->greater_than_is_operator_p
11119 = saved_greater_than_is_operator_p;
34da8800 11120 }
11121
11122 /* Go back to evaluating expressions. */
48d94ede 11123 --cp_unevaluated_operand;
11124 --c_inhibit_evaluation_warnings;
34da8800 11125
11126 /* Restore the old message and the integral constant expression
11127 flags. */
11128 parser->type_definition_forbidden_message = saved_message;
11129 parser->integral_constant_expression_p
11130 = saved_integral_constant_expression_p;
11131 parser->non_integral_constant_expression_p
11132 = saved_non_integral_constant_expression_p;
11133
34da8800 11134 /* Parse to the closing `)'. */
c247dce0 11135 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4e761042 11136 {
11137 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11138 /*consume_paren=*/true);
11139 return error_mark_node;
11140 }
34da8800 11141
07b8f133 11142 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
c2b6be66 11143 tf_warning_or_error);
07b8f133 11144
11145 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11146 it again. */
11147 start_token->type = CPP_DECLTYPE;
11148 start_token->u.value = expr;
11149 start_token->keyword = RID_MAX;
11150 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11151
11152 return expr;
34da8800 11153}
11154
0a3b29ad 11155/* Special member functions [gram.special] */
11156
11157/* Parse a conversion-function-id.
11158
11159 conversion-function-id:
ccb84981 11160 operator conversion-type-id
0a3b29ad 11161
11162 Returns an IDENTIFIER_NODE representing the operator. */
11163
ccb84981 11164static tree
45baea8b 11165cp_parser_conversion_function_id (cp_parser* parser)
0a3b29ad 11166{
11167 tree type;
11168 tree saved_scope;
11169 tree saved_qualifying_scope;
11170 tree saved_object_scope;
7f602bca 11171 tree pushed_scope = NULL_TREE;
0a3b29ad 11172
11173 /* Look for the `operator' token. */
c247dce0 11174 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
0a3b29ad 11175 return error_mark_node;
11176 /* When we parse the conversion-type-id, the current scope will be
11177 reset. However, we need that information in able to look up the
11178 conversion function later, so we save it here. */
11179 saved_scope = parser->scope;
11180 saved_qualifying_scope = parser->qualifying_scope;
11181 saved_object_scope = parser->object_scope;
11182 /* We must enter the scope of the class so that the names of
11183 entities declared within the class are available in the
11184 conversion-type-id. For example, consider:
11185
ccb84981 11186 struct S {
653e5405 11187 typedef int I;
0a3b29ad 11188 operator I();
11189 };
11190
11191 S::operator I() { ... }
11192
11193 In order to see that `I' is a type-name in the definition, we
11194 must be in the scope of `S'. */
11195 if (saved_scope)
7f602bca 11196 pushed_scope = push_scope (saved_scope);
0a3b29ad 11197 /* Parse the conversion-type-id. */
11198 type = cp_parser_conversion_type_id (parser);
11199 /* Leave the scope of the class, if any. */
7f602bca 11200 if (pushed_scope)
11201 pop_scope (pushed_scope);
0a3b29ad 11202 /* Restore the saved scope. */
11203 parser->scope = saved_scope;
11204 parser->qualifying_scope = saved_qualifying_scope;
11205 parser->object_scope = saved_object_scope;
11206 /* If the TYPE is invalid, indicate failure. */
11207 if (type == error_mark_node)
11208 return error_mark_node;
11209 return mangle_conv_op_name_for_type (type);
11210}
11211
11212/* Parse a conversion-type-id:
11213
11214 conversion-type-id:
11215 type-specifier-seq conversion-declarator [opt]
11216
11217 Returns the TYPE specified. */
11218
11219static tree
45baea8b 11220cp_parser_conversion_type_id (cp_parser* parser)
0a3b29ad 11221{
11222 tree attributes;
4b9b2871 11223 cp_decl_specifier_seq type_specifiers;
3046c0a3 11224 cp_declarator *declarator;
c37ff371 11225 tree type_specified;
0a3b29ad 11226
11227 /* Parse the attributes. */
11228 attributes = cp_parser_attributes_opt (parser);
11229 /* Parse the type-specifiers. */
c44f7faf 11230 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 11231 /*is_trailing_return=*/false,
6f74fe3c 11232 &type_specifiers);
0a3b29ad 11233 /* If that didn't work, stop. */
4b9b2871 11234 if (type_specifiers.type == error_mark_node)
0a3b29ad 11235 return error_mark_node;
11236 /* Parse the conversion-declarator. */
11237 declarator = cp_parser_conversion_declarator_opt (parser);
11238
c37ff371 11239 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
653e5405 11240 /*initialized=*/0, &attributes);
c37ff371 11241 if (attributes)
11242 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
ddcb49ca 11243
11244 /* Don't give this error when parsing tentatively. This happens to
11245 work because we always parse this definitively once. */
11246 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11247 && type_uses_auto (type_specified))
11248 {
86359a65 11249 if (cxx_dialect < cxx1y)
11250 {
11251 error ("invalid use of %<auto%> in conversion operator");
11252 return error_mark_node;
11253 }
11254 else if (template_parm_scope_p ())
11255 warning (0, "use of %<auto%> in member template "
11256 "conversion operator can never be deduced");
ddcb49ca 11257 }
11258
c37ff371 11259 return type_specified;
0a3b29ad 11260}
11261
11262/* Parse an (optional) conversion-declarator.
11263
11264 conversion-declarator:
ccb84981 11265 ptr-operator conversion-declarator [opt]
0a3b29ad 11266
3046c0a3 11267 */
0a3b29ad 11268
3046c0a3 11269static cp_declarator *
45baea8b 11270cp_parser_conversion_declarator_opt (cp_parser* parser)
0a3b29ad 11271{
11272 enum tree_code code;
11273 tree class_type;
2cfb6cde 11274 cp_cv_quals cv_quals;
0a3b29ad 11275
11276 /* We don't know if there's a ptr-operator next, or not. */
11277 cp_parser_parse_tentatively (parser);
11278 /* Try the ptr-operator. */
2cfb6cde 11279 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
0a3b29ad 11280 /* If it worked, look for more conversion-declarators. */
11281 if (cp_parser_parse_definitely (parser))
11282 {
3046c0a3 11283 cp_declarator *declarator;
207355ad 11284
3046c0a3 11285 /* Parse another optional declarator. */
11286 declarator = cp_parser_conversion_declarator_opt (parser);
207355ad 11287
63949b38 11288 return cp_parser_make_indirect_declarator
11289 (code, class_type, cv_quals, declarator);
0a3b29ad 11290 }
11291
3046c0a3 11292 return NULL;
0a3b29ad 11293}
11294
11295/* Parse an (optional) ctor-initializer.
11296
11297 ctor-initializer:
ccb84981 11298 : mem-initializer-list
0a3b29ad 11299
11300 Returns TRUE iff the ctor-initializer was actually present. */
11301
11302static bool
45baea8b 11303cp_parser_ctor_initializer_opt (cp_parser* parser)
0a3b29ad 11304{
11305 /* If the next token is not a `:', then there is no
11306 ctor-initializer. */
11307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11308 {
11309 /* Do default initialization of any bases and members. */
11310 if (DECL_CONSTRUCTOR_P (current_function_decl))
11311 finish_mem_initializers (NULL_TREE);
11312
11313 return false;
11314 }
11315
11316 /* Consume the `:' token. */
11317 cp_lexer_consume_token (parser->lexer);
11318 /* And the mem-initializer-list. */
11319 cp_parser_mem_initializer_list (parser);
11320
11321 return true;
11322}
11323
11324/* Parse a mem-initializer-list.
11325
11326 mem-initializer-list:
d95d815d 11327 mem-initializer ... [opt]
11328 mem-initializer ... [opt] , mem-initializer-list */
0a3b29ad 11329
11330static void
45baea8b 11331cp_parser_mem_initializer_list (cp_parser* parser)
0a3b29ad 11332{
11333 tree mem_initializer_list = NULL_TREE;
90510c63 11334 tree target_ctor = error_mark_node;
ad9ae192 11335 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11336
11337 /* Let the semantic analysis code know that we are starting the
11338 mem-initializer-list. */
7e5ca199 11339 if (!DECL_CONSTRUCTOR_P (current_function_decl))
ccb59bb6 11340 error_at (token->location,
d653ac31 11341 "only constructors take member initializers");
0a3b29ad 11342
11343 /* Loop through the list. */
11344 while (true)
11345 {
11346 tree mem_initializer;
11347
ad9ae192 11348 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 11349 /* Parse the mem-initializer. */
11350 mem_initializer = cp_parser_mem_initializer (parser);
d95d815d 11351 /* If the next token is a `...', we're expanding member initializers. */
11352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11353 {
11354 /* Consume the `...'. */
11355 cp_lexer_consume_token (parser->lexer);
11356
11357 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11358 can be expanded but members cannot. */
11359 if (mem_initializer != error_mark_node
11360 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11361 {
ccb59bb6 11362 error_at (token->location,
11363 "cannot expand initializer for member %<%D%>",
11364 TREE_PURPOSE (mem_initializer));
d95d815d 11365 mem_initializer = error_mark_node;
11366 }
11367
11368 /* Construct the pack expansion type. */
11369 if (mem_initializer != error_mark_node)
11370 mem_initializer = make_pack_expansion (mem_initializer);
11371 }
90510c63 11372 if (target_ctor != error_mark_node
11373 && mem_initializer != error_mark_node)
11374 {
11375 error ("mem-initializer for %qD follows constructor delegation",
11376 TREE_PURPOSE (mem_initializer));
11377 mem_initializer = error_mark_node;
11378 }
11379 /* Look for a target constructor. */
11380 if (mem_initializer != error_mark_node
11381 && TYPE_P (TREE_PURPOSE (mem_initializer))
11382 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11383 {
11384 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11385 if (mem_initializer_list)
11386 {
11387 error ("constructor delegation follows mem-initializer for %qD",
11388 TREE_PURPOSE (mem_initializer_list));
11389 mem_initializer = error_mark_node;
11390 }
11391 target_ctor = mem_initializer;
11392 }
0a3b29ad 11393 /* Add it to the list, unless it was erroneous. */
7be1bc1f 11394 if (mem_initializer != error_mark_node)
0a3b29ad 11395 {
11396 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11397 mem_initializer_list = mem_initializer;
11398 }
11399 /* If the next token is not a `,', we're done. */
11400 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11401 break;
11402 /* Consume the `,' token. */
11403 cp_lexer_consume_token (parser->lexer);
11404 }
11405
11406 /* Perform semantic analysis. */
7e5ca199 11407 if (DECL_CONSTRUCTOR_P (current_function_decl))
11408 finish_mem_initializers (mem_initializer_list);
0a3b29ad 11409}
11410
11411/* Parse a mem-initializer.
11412
11413 mem-initializer:
ccb84981 11414 mem-initializer-id ( expression-list [opt] )
f82f1250 11415 mem-initializer-id braced-init-list
0a3b29ad 11416
11417 GNU extension:
ccb84981 11418
0a3b29ad 11419 mem-initializer:
755edffd 11420 ( expression-list [opt] )
0a3b29ad 11421
11422 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11423 class) or FIELD_DECL (for a non-static data member) to initialize;
7be1bc1f 11424 the TREE_VALUE is the expression-list. An empty initialization
11425 list is represented by void_list_node. */
0a3b29ad 11426
11427static tree
45baea8b 11428cp_parser_mem_initializer (cp_parser* parser)
0a3b29ad 11429{
11430 tree mem_initializer_id;
11431 tree expression_list;
5f1653d2 11432 tree member;
ad9ae192 11433 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb84981 11434
0a3b29ad 11435 /* Find out what is being initialized. */
11436 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11437 {
2b9e3597 11438 permerror (token->location,
11439 "anachronistic old-style base class initializer");
0a3b29ad 11440 mem_initializer_id = NULL_TREE;
11441 }
11442 else
642a3054 11443 {
11444 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11445 if (mem_initializer_id == error_mark_node)
11446 return mem_initializer_id;
11447 }
5f1653d2 11448 member = expand_member_init (mem_initializer_id);
11449 if (member && !DECL_P (member))
11450 in_base_initializer = 1;
0986fa22 11451
f82f1250 11452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11453 {
11454 bool expr_non_constant_p;
bf8d19fe 11455 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 11456 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11457 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11458 expression_list = build_tree_list (NULL_TREE, expression_list);
11459 }
11460 else
f352a3fb 11461 {
11462 VEC(tree,gc)* vec;
33199a81 11463 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 11464 /*cast_p=*/false,
11465 /*allow_expansion_p=*/true,
11466 /*non_constant_p=*/NULL);
11467 if (vec == NULL)
11468 return error_mark_node;
11469 expression_list = build_tree_list_vec (vec);
11470 release_tree_vector (vec);
11471 }
11472
7be1bc1f 11473 if (expression_list == error_mark_node)
11474 return error_mark_node;
0986fa22 11475 if (!expression_list)
0a3b29ad 11476 expression_list = void_type_node;
0a3b29ad 11477
5f1653d2 11478 in_base_initializer = 0;
ccb84981 11479
7be1bc1f 11480 return member ? build_tree_list (member, expression_list) : error_mark_node;
0a3b29ad 11481}
11482
11483/* Parse a mem-initializer-id.
11484
11485 mem-initializer-id:
11486 :: [opt] nested-name-specifier [opt] class-name
ccb84981 11487 identifier
0a3b29ad 11488
11489 Returns a TYPE indicating the class to be initializer for the first
11490 production. Returns an IDENTIFIER_NODE indicating the data member
11491 to be initialized for the second production. */
11492
11493static tree
45baea8b 11494cp_parser_mem_initializer_id (cp_parser* parser)
0a3b29ad 11495{
11496 bool global_scope_p;
11497 bool nested_name_specifier_p;
0b1957b6 11498 bool template_p = false;
0a3b29ad 11499 tree id;
11500
ad9ae192 11501 cp_token *token = cp_lexer_peek_token (parser->lexer);
11502
0b1957b6 11503 /* `typename' is not allowed in this context ([temp.res]). */
11504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11505 {
ccb59bb6 11506 error_at (token->location,
11507 "keyword %<typename%> not allowed in this context (a qualified "
11508 "member initializer is implicitly a type)");
0b1957b6 11509 cp_lexer_consume_token (parser->lexer);
11510 }
0a3b29ad 11511 /* Look for the optional `::' operator. */
ccb84981 11512 global_scope_p
11513 = (cp_parser_global_scope_opt (parser,
130bb1d4 11514 /*current_scope_valid_p=*/false)
0a3b29ad 11515 != NULL_TREE);
11516 /* Look for the optional nested-name-specifier. The simplest way to
11517 implement:
11518
11519 [temp.res]
11520
11521 The keyword `typename' is not permitted in a base-specifier or
11522 mem-initializer; in these contexts a qualified name that
11523 depends on a template-parameter is implicitly assumed to be a
11524 type name.
11525
11526 is to assume that we have seen the `typename' keyword at this
11527 point. */
ccb84981 11528 nested_name_specifier_p
0a3b29ad 11529 = (cp_parser_nested_name_specifier_opt (parser,
11530 /*typename_keyword_p=*/true,
11531 /*check_dependency_p=*/true,
3d0f901b 11532 /*type_p=*/true,
11533 /*is_declaration=*/true)
0a3b29ad 11534 != NULL_TREE);
0b1957b6 11535 if (nested_name_specifier_p)
11536 template_p = cp_parser_optional_template_keyword (parser);
0a3b29ad 11537 /* If there is a `::' operator or a nested-name-specifier, then we
11538 are definitely looking for a class-name. */
11539 if (global_scope_p || nested_name_specifier_p)
11540 return cp_parser_class_name (parser,
11541 /*typename_keyword_p=*/true,
0b1957b6 11542 /*template_keyword_p=*/template_p,
5570fae0 11543 typename_type,
0a3b29ad 11544 /*check_dependency_p=*/true,
3d0f901b 11545 /*class_head_p=*/false,
11546 /*is_declaration=*/true);
0a3b29ad 11547 /* Otherwise, we could also be looking for an ordinary identifier. */
11548 cp_parser_parse_tentatively (parser);
11549 /* Try a class-name. */
ccb84981 11550 id = cp_parser_class_name (parser,
0a3b29ad 11551 /*typename_keyword_p=*/true,
11552 /*template_keyword_p=*/false,
e2ae55f2 11553 none_type,
0a3b29ad 11554 /*check_dependency_p=*/true,
3d0f901b 11555 /*class_head_p=*/false,
11556 /*is_declaration=*/true);
0a3b29ad 11557 /* If we found one, we're done. */
11558 if (cp_parser_parse_definitely (parser))
11559 return id;
11560 /* Otherwise, look for an ordinary identifier. */
11561 return cp_parser_identifier (parser);
11562}
11563
11564/* Overloading [gram.over] */
11565
11566/* Parse an operator-function-id.
11567
11568 operator-function-id:
ccb84981 11569 operator operator
0a3b29ad 11570
11571 Returns an IDENTIFIER_NODE for the operator which is a
11572 human-readable spelling of the identifier, e.g., `operator +'. */
11573
ccb84981 11574static tree
45baea8b 11575cp_parser_operator_function_id (cp_parser* parser)
0a3b29ad 11576{
11577 /* Look for the `operator' keyword. */
c247dce0 11578 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
0a3b29ad 11579 return error_mark_node;
11580 /* And then the name of the operator itself. */
11581 return cp_parser_operator (parser);
11582}
11583
244db24d 11584/* Return an identifier node for a user-defined literal operator.
11585 The suffix identifier is chained to the operator name identifier. */
11586
11587static tree
11588cp_literal_operator_id (const char* name)
11589{
11590 tree identifier;
11591 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11592 + strlen (name) + 10);
11593 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11594 identifier = get_identifier (buffer);
11595 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11596
11597 return identifier;
11598}
11599
0a3b29ad 11600/* Parse an operator.
11601
11602 operator:
11603 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11604 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11605 || ++ -- , ->* -> () []
11606
11607 GNU Extensions:
ccb84981 11608
0a3b29ad 11609 operator:
11610 <? >? <?= >?=
11611
11612 Returns an IDENTIFIER_NODE for the operator which is a
11613 human-readable spelling of the identifier, e.g., `operator +'. */
ccb84981 11614
0a3b29ad 11615static tree
45baea8b 11616cp_parser_operator (cp_parser* parser)
0a3b29ad 11617{
11618 tree id = NULL_TREE;
11619 cp_token *token;
11620
11621 /* Peek at the next token. */
11622 token = cp_lexer_peek_token (parser->lexer);
11623 /* Figure out which operator we have. */
11624 switch (token->type)
11625 {
11626 case CPP_KEYWORD:
11627 {
11628 enum tree_code op;
11629
11630 /* The keyword should be either `new' or `delete'. */
11631 if (token->keyword == RID_NEW)
11632 op = NEW_EXPR;
11633 else if (token->keyword == RID_DELETE)
11634 op = DELETE_EXPR;
11635 else
11636 break;
11637
11638 /* Consume the `new' or `delete' token. */
11639 cp_lexer_consume_token (parser->lexer);
11640
11641 /* Peek at the next token. */
11642 token = cp_lexer_peek_token (parser->lexer);
11643 /* If it's a `[' token then this is the array variant of the
11644 operator. */
11645 if (token->type == CPP_OPEN_SQUARE)
11646 {
11647 /* Consume the `[' token. */
11648 cp_lexer_consume_token (parser->lexer);
11649 /* Look for the `]' token. */
c247dce0 11650 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
ccb84981 11651 id = ansi_opname (op == NEW_EXPR
0a3b29ad 11652 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11653 }
11654 /* Otherwise, we have the non-array variant. */
11655 else
11656 id = ansi_opname (op);
11657
11658 return id;
11659 }
11660
11661 case CPP_PLUS:
11662 id = ansi_opname (PLUS_EXPR);
11663 break;
11664
11665 case CPP_MINUS:
11666 id = ansi_opname (MINUS_EXPR);
11667 break;
11668
11669 case CPP_MULT:
11670 id = ansi_opname (MULT_EXPR);
11671 break;
11672
11673 case CPP_DIV:
11674 id = ansi_opname (TRUNC_DIV_EXPR);
11675 break;
11676
11677 case CPP_MOD:
11678 id = ansi_opname (TRUNC_MOD_EXPR);
11679 break;
11680
11681 case CPP_XOR:
11682 id = ansi_opname (BIT_XOR_EXPR);
11683 break;
11684
11685 case CPP_AND:
11686 id = ansi_opname (BIT_AND_EXPR);
11687 break;
11688
11689 case CPP_OR:
11690 id = ansi_opname (BIT_IOR_EXPR);
11691 break;
11692
11693 case CPP_COMPL:
11694 id = ansi_opname (BIT_NOT_EXPR);
11695 break;
ccb84981 11696
0a3b29ad 11697 case CPP_NOT:
11698 id = ansi_opname (TRUTH_NOT_EXPR);
11699 break;
11700
11701 case CPP_EQ:
11702 id = ansi_assopname (NOP_EXPR);
11703 break;
11704
11705 case CPP_LESS:
11706 id = ansi_opname (LT_EXPR);
11707 break;
11708
11709 case CPP_GREATER:
11710 id = ansi_opname (GT_EXPR);
11711 break;
11712
11713 case CPP_PLUS_EQ:
11714 id = ansi_assopname (PLUS_EXPR);
11715 break;
11716
11717 case CPP_MINUS_EQ:
11718 id = ansi_assopname (MINUS_EXPR);
11719 break;
11720
11721 case CPP_MULT_EQ:
11722 id = ansi_assopname (MULT_EXPR);
11723 break;
11724
11725 case CPP_DIV_EQ:
11726 id = ansi_assopname (TRUNC_DIV_EXPR);
11727 break;
11728
11729 case CPP_MOD_EQ:
11730 id = ansi_assopname (TRUNC_MOD_EXPR);
11731 break;
11732
11733 case CPP_XOR_EQ:
11734 id = ansi_assopname (BIT_XOR_EXPR);
11735 break;
11736
11737 case CPP_AND_EQ:
11738 id = ansi_assopname (BIT_AND_EXPR);
11739 break;
11740
11741 case CPP_OR_EQ:
11742 id = ansi_assopname (BIT_IOR_EXPR);
11743 break;
11744
11745 case CPP_LSHIFT:
11746 id = ansi_opname (LSHIFT_EXPR);
11747 break;
11748
11749 case CPP_RSHIFT:
11750 id = ansi_opname (RSHIFT_EXPR);
11751 break;
11752
11753 case CPP_LSHIFT_EQ:
11754 id = ansi_assopname (LSHIFT_EXPR);
11755 break;
11756
11757 case CPP_RSHIFT_EQ:
11758 id = ansi_assopname (RSHIFT_EXPR);
11759 break;
11760
11761 case CPP_EQ_EQ:
11762 id = ansi_opname (EQ_EXPR);
11763 break;
11764
11765 case CPP_NOT_EQ:
11766 id = ansi_opname (NE_EXPR);
11767 break;
11768
11769 case CPP_LESS_EQ:
11770 id = ansi_opname (LE_EXPR);
11771 break;
11772
11773 case CPP_GREATER_EQ:
11774 id = ansi_opname (GE_EXPR);
11775 break;
11776
11777 case CPP_AND_AND:
11778 id = ansi_opname (TRUTH_ANDIF_EXPR);
11779 break;
11780
11781 case CPP_OR_OR:
11782 id = ansi_opname (TRUTH_ORIF_EXPR);
11783 break;
ccb84981 11784
0a3b29ad 11785 case CPP_PLUS_PLUS:
11786 id = ansi_opname (POSTINCREMENT_EXPR);
11787 break;
11788
11789 case CPP_MINUS_MINUS:
11790 id = ansi_opname (PREDECREMENT_EXPR);
11791 break;
11792
11793 case CPP_COMMA:
11794 id = ansi_opname (COMPOUND_EXPR);
11795 break;
11796
11797 case CPP_DEREF_STAR:
11798 id = ansi_opname (MEMBER_REF);
11799 break;
11800
11801 case CPP_DEREF:
11802 id = ansi_opname (COMPONENT_REF);
11803 break;
11804
11805 case CPP_OPEN_PAREN:
11806 /* Consume the `('. */
11807 cp_lexer_consume_token (parser->lexer);
11808 /* Look for the matching `)'. */
c247dce0 11809 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 11810 return ansi_opname (CALL_EXPR);
11811
11812 case CPP_OPEN_SQUARE:
11813 /* Consume the `['. */
11814 cp_lexer_consume_token (parser->lexer);
11815 /* Look for the matching `]'. */
c247dce0 11816 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 11817 return ansi_opname (ARRAY_REF);
11818
244db24d 11819 case CPP_STRING:
11820 if (cxx_dialect == cxx98)
11821 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11822 if (TREE_STRING_LENGTH (token->u.value) > 2)
11823 {
11824 error ("expected empty string after %<operator%> keyword");
11825 return error_mark_node;
11826 }
11827 /* Consume the string. */
11828 cp_lexer_consume_token (parser->lexer);
11829 /* Look for the suffix identifier. */
11830 token = cp_lexer_peek_token (parser->lexer);
11831 if (token->type == CPP_NAME)
11832 {
11833 id = cp_parser_identifier (parser);
11834 if (id != error_mark_node)
11835 {
11836 const char *name = IDENTIFIER_POINTER (id);
11837 return cp_literal_operator_id (name);
11838 }
11839 }
11840 else
11841 {
11842 error ("expected suffix identifier");
11843 return error_mark_node;
11844 }
11845
11846 case CPP_STRING_USERDEF:
11847 error ("missing space between %<\"\"%> and suffix identifier");
11848 return error_mark_node;
11849
0a3b29ad 11850 default:
11851 /* Anything else is an error. */
11852 break;
11853 }
11854
11855 /* If we have selected an identifier, we need to consume the
11856 operator token. */
11857 if (id)
11858 cp_lexer_consume_token (parser->lexer);
11859 /* Otherwise, no valid operator name was present. */
11860 else
11861 {
11862 cp_parser_error (parser, "expected operator");
11863 id = error_mark_node;
11864 }
11865
11866 return id;
11867}
11868
11869/* Parse a template-declaration.
11870
11871 template-declaration:
ccb84981 11872 export [opt] template < template-parameter-list > declaration
0a3b29ad 11873
11874 If MEMBER_P is TRUE, this template-declaration occurs within a
ccb84981 11875 class-specifier.
0a3b29ad 11876
11877 The grammar rule given by the standard isn't correct. What
11878 is really meant is:
11879
11880 template-declaration:
ccb84981 11881 export [opt] template-parameter-list-seq
0a3b29ad 11882 decl-specifier-seq [opt] init-declarator [opt] ;
ccb84981 11883 export [opt] template-parameter-list-seq
0a3b29ad 11884 function-definition
11885
11886 template-parameter-list-seq:
11887 template-parameter-list-seq [opt]
11888 template < template-parameter-list > */
11889
11890static void
45baea8b 11891cp_parser_template_declaration (cp_parser* parser, bool member_p)
0a3b29ad 11892{
11893 /* Check for `export'. */
11894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11895 {
11896 /* Consume the `export' token. */
11897 cp_lexer_consume_token (parser->lexer);
11898 /* Warn that we do not support `export'. */
c3ceba8e 11899 warning (0, "keyword %<export%> not implemented, and will be ignored");
0a3b29ad 11900 }
11901
11902 cp_parser_template_declaration_after_export (parser, member_p);
11903}
11904
11905/* Parse a template-parameter-list.
11906
11907 template-parameter-list:
11908 template-parameter
11909 template-parameter-list , template-parameter
11910
11911 Returns a TREE_LIST. Each node represents a template parameter.
11912 The nodes are connected via their TREE_CHAINs. */
11913
11914static tree
45baea8b 11915cp_parser_template_parameter_list (cp_parser* parser)
0a3b29ad 11916{
11917 tree parameter_list = NULL_TREE;
11918
7be1bc1f 11919 begin_template_parm_list ();
0d432ee0 11920
11921 /* The loop below parses the template parms. We first need to know
11922 the total number of template parms to be able to compute proper
11923 canonical types of each dependent type. So after the loop, when
11924 we know the total number of template parms,
11925 end_template_parm_list computes the proper canonical types and
11926 fixes up the dependent types accordingly. */
0a3b29ad 11927 while (true)
11928 {
11929 tree parameter;
3046c0a3 11930 bool is_non_type;
d95d815d 11931 bool is_parameter_pack;
e60a6f7b 11932 location_t parm_loc;
0a3b29ad 11933
11934 /* Parse the template-parameter. */
e60a6f7b 11935 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
d95d815d 11936 parameter = cp_parser_template_parameter (parser,
11937 &is_non_type,
11938 &is_parameter_pack);
0a3b29ad 11939 /* Add it to the list. */
8f776a97 11940 if (parameter != error_mark_node)
11941 parameter_list = process_template_parm (parameter_list,
e60a6f7b 11942 parm_loc,
8f776a97 11943 parameter,
d95d815d 11944 is_non_type,
0d432ee0 11945 is_parameter_pack,
11946 0);
e347c61e 11947 else
11948 {
11949 tree err_parm = build_tree_list (parameter, parameter);
e347c61e 11950 parameter_list = chainon (parameter_list, err_parm);
11951 }
11952
f5b66d53 11953 /* If the next token is not a `,', we're done. */
11954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 11955 break;
11956 /* Otherwise, consume the `,' token. */
11957 cp_lexer_consume_token (parser->lexer);
11958 }
11959
7be1bc1f 11960 return end_template_parm_list (parameter_list);
0a3b29ad 11961}
11962
11963/* Parse a template-parameter.
11964
11965 template-parameter:
11966 type-parameter
11967 parameter-declaration
11968
8f776a97 11969 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11970 the parameter. The TREE_PURPOSE is the default value, if any.
11971 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
d95d815d 11972 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11973 set to true iff this parameter is a parameter pack. */
0a3b29ad 11974
11975static tree
d95d815d 11976cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11977 bool *is_parameter_pack)
0a3b29ad 11978{
11979 cp_token *token;
4b9b2871 11980 cp_parameter_declarator *parameter_declarator;
4efde0d3 11981 cp_declarator *id_declarator;
8f776a97 11982 tree parm;
0a3b29ad 11983
3046c0a3 11984 /* Assume it is a type parameter or a template parameter. */
11985 *is_non_type = false;
d95d815d 11986 /* Assume it not a parameter pack. */
11987 *is_parameter_pack = false;
0a3b29ad 11988 /* Peek at the next token. */
11989 token = cp_lexer_peek_token (parser->lexer);
11990 /* If it is `class' or `template', we have a type-parameter. */
11991 if (token->keyword == RID_TEMPLATE)
d95d815d 11992 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 11993 /* If it is `class' or `typename' we do not know yet whether it is a
11994 type parameter or a non-type parameter. Consider:
11995
11996 template <typename T, typename T::X X> ...
11997
11998 or:
ccb84981 11999
0a3b29ad 12000 template <class C, class D*> ...
12001
12002 Here, the first parameter is a type parameter, and the second is
12003 a non-type parameter. We can tell by looking at the token after
12004 the identifier -- if it is a `,', `=', or `>' then we have a type
12005 parameter. */
12006 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12007 {
12008 /* Peek at the token after `class' or `typename'. */
12009 token = cp_lexer_peek_nth_token (parser->lexer, 2);
d95d815d 12010 /* If it's an ellipsis, we have a template type parameter
12011 pack. */
12012 if (token->type == CPP_ELLIPSIS)
12013 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 12014 /* If it's an identifier, skip it. */
12015 if (token->type == CPP_NAME)
12016 token = cp_lexer_peek_nth_token (parser->lexer, 3);
12017 /* Now, see if the token looks like the end of a template
12018 parameter. */
ccb84981 12019 if (token->type == CPP_COMMA
0a3b29ad 12020 || token->type == CPP_EQ
12021 || token->type == CPP_GREATER)
d95d815d 12022 return cp_parser_type_parameter (parser, is_parameter_pack);
0a3b29ad 12023 }
12024
ccb84981 12025 /* Otherwise, it is a non-type parameter.
0a3b29ad 12026
12027 [temp.param]
12028
12029 When parsing a default template-argument for a non-type
12030 template-parameter, the first non-nested `>' is taken as the end
12031 of the template parameter-list rather than a greater-than
12032 operator. */
3046c0a3 12033 *is_non_type = true;
12034 parameter_declarator
12035 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12036 /*parenthesized_p=*/NULL);
d95d815d 12037
12038 /* If the parameter declaration is marked as a parameter pack, set
12039 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12040 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12041 grokdeclarator. */
12042 if (parameter_declarator
12043 && parameter_declarator->declarator
12044 && parameter_declarator->declarator->parameter_pack_p)
12045 {
12046 *is_parameter_pack = true;
12047 parameter_declarator->declarator->parameter_pack_p = false;
12048 }
12049
12050 /* If the next token is an ellipsis, and we don't already have it
12051 marked as a parameter pack, then we have a parameter pack (that
41341abd 12052 has no declarator). */
d95d815d 12053 if (!*is_parameter_pack
2aedc2ff 12054 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12055 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
d95d815d 12056 {
41341abd 12057 /* Consume the `...'. */
d95d815d 12058 cp_lexer_consume_token (parser->lexer);
12059 maybe_warn_variadic_templates ();
12060
12061 *is_parameter_pack = true;
83b01f73 12062 }
12063 /* We might end up with a pack expansion as the type of the non-type
12064 template parameter, in which case this is a non-type template
12065 parameter pack. */
12066 else if (parameter_declarator
12067 && parameter_declarator->decl_specifiers.type
12068 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12069 {
12070 *is_parameter_pack = true;
12071 parameter_declarator->decl_specifiers.type =
12072 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12073 }
41341abd 12074
83b01f73 12075 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12076 {
41341abd 12077 /* Parameter packs cannot have default arguments. However, a
12078 user may try to do so, so we'll parse them and give an
12079 appropriate diagnostic here. */
41341abd 12080
ad9ae192 12081 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
83b01f73 12082
12083 /* Find the name of the parameter pack. */
12084 id_declarator = parameter_declarator->declarator;
12085 while (id_declarator && id_declarator->kind != cdk_id)
12086 id_declarator = id_declarator->declarator;
12087
12088 if (id_declarator && id_declarator->kind == cdk_id)
ccb59bb6 12089 error_at (start_token->location,
12090 "template parameter pack %qD cannot have a default argument",
12091 id_declarator->u.id.unqualified_name);
83b01f73 12092 else
ccb59bb6 12093 error_at (start_token->location,
12094 "template parameter pack cannot have a default argument");
83b01f73 12095
12096 /* Parse the default argument, but throw away the result. */
12097 cp_parser_default_argument (parser, /*template_parm_p=*/true);
d95d815d 12098 }
12099
8f776a97 12100 parm = grokdeclarator (parameter_declarator->declarator,
12101 &parameter_declarator->decl_specifiers,
3b901c35 12102 TPARM, /*initialized=*/0,
8f776a97 12103 /*attrlist=*/NULL);
12104 if (parm == error_mark_node)
12105 return error_mark_node;
d95d815d 12106
8f776a97 12107 return build_tree_list (parameter_declarator->default_argument, parm);
0a3b29ad 12108}
12109
12110/* Parse a type-parameter.
12111
12112 type-parameter:
12113 class identifier [opt]
12114 class identifier [opt] = type-id
12115 typename identifier [opt]
12116 typename identifier [opt] = type-id
12117 template < template-parameter-list > class identifier [opt]
ccb84981 12118 template < template-parameter-list > class identifier [opt]
12119 = id-expression
0a3b29ad 12120
d95d815d 12121 GNU Extension (variadic templates):
12122
12123 type-parameter:
12124 class ... identifier [opt]
12125 typename ... identifier [opt]
12126
0a3b29ad 12127 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12128 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
d95d815d 12129 the declaration of the parameter.
12130
12131 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
0a3b29ad 12132
12133static tree
d95d815d 12134cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
0a3b29ad 12135{
12136 cp_token *token;
12137 tree parameter;
12138
12139 /* Look for a keyword to tell us what kind of parameter this is. */
c247dce0 12140 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
0a3b29ad 12141 if (!token)
12142 return error_mark_node;
12143
12144 switch (token->keyword)
12145 {
12146 case RID_CLASS:
12147 case RID_TYPENAME:
12148 {
12149 tree identifier;
12150 tree default_argument;
12151
d95d815d 12152 /* If the next token is an ellipsis, we have a template
12153 argument pack. */
12154 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12155 {
12156 /* Consume the `...' token. */
12157 cp_lexer_consume_token (parser->lexer);
12158 maybe_warn_variadic_templates ();
12159
12160 *is_parameter_pack = true;
12161 }
12162
0a3b29ad 12163 /* If the next token is an identifier, then it names the
653e5405 12164 parameter. */
0a3b29ad 12165 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12166 identifier = cp_parser_identifier (parser);
12167 else
12168 identifier = NULL_TREE;
12169
12170 /* Create the parameter. */
12171 parameter = finish_template_type_parm (class_type_node, identifier);
12172
12173 /* If the next token is an `=', we have a default argument. */
12174 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12175 {
12176 /* Consume the `=' token. */
12177 cp_lexer_consume_token (parser->lexer);
755edffd 12178 /* Parse the default-argument. */
23010bc8 12179 push_deferring_access_checks (dk_no_deferred);
0a3b29ad 12180 default_argument = cp_parser_type_id (parser);
d95d815d 12181
12182 /* Template parameter packs cannot have default
12183 arguments. */
12184 if (*is_parameter_pack)
12185 {
12186 if (identifier)
ccb59bb6 12187 error_at (token->location,
12188 "template parameter pack %qD cannot have a "
12189 "default argument", identifier);
d95d815d 12190 else
ccb59bb6 12191 error_at (token->location,
12192 "template parameter packs cannot have "
12193 "default arguments");
d95d815d 12194 default_argument = NULL_TREE;
12195 }
23010bc8 12196 pop_deferring_access_checks ();
0a3b29ad 12197 }
12198 else
12199 default_argument = NULL_TREE;
12200
12201 /* Create the combined representation of the parameter and the
12202 default argument. */
816786ad 12203 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 12204 }
12205 break;
12206
12207 case RID_TEMPLATE:
12208 {
0a3b29ad 12209 tree identifier;
12210 tree default_argument;
12211
12212 /* Look for the `<'. */
c247dce0 12213 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 12214 /* Parse the template-parameter-list. */
8e9e8d76 12215 cp_parser_template_parameter_list (parser);
0a3b29ad 12216 /* Look for the `>'. */
c247dce0 12217 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 12218 /* Look for the `class' keyword. */
c247dce0 12219 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
d95d815d 12220 /* If the next token is an ellipsis, we have a template
12221 argument pack. */
12222 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12223 {
12224 /* Consume the `...' token. */
12225 cp_lexer_consume_token (parser->lexer);
12226 maybe_warn_variadic_templates ();
12227
12228 *is_parameter_pack = true;
12229 }
0a3b29ad 12230 /* If the next token is an `=', then there is a
12231 default-argument. If the next token is a `>', we are at
12232 the end of the parameter-list. If the next token is a `,',
12233 then we are at the end of this parameter. */
12234 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12235 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12236 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
2a03dcc3 12237 {
12238 identifier = cp_parser_identifier (parser);
93523877 12239 /* Treat invalid names as if the parameter were nameless. */
2a03dcc3 12240 if (identifier == error_mark_node)
12241 identifier = NULL_TREE;
12242 }
0a3b29ad 12243 else
12244 identifier = NULL_TREE;
2a03dcc3 12245
0a3b29ad 12246 /* Create the template parameter. */
12247 parameter = finish_template_template_parm (class_type_node,
12248 identifier);
ccb84981 12249
0a3b29ad 12250 /* If the next token is an `=', then there is a
12251 default-argument. */
12252 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12253 {
c3b9e457 12254 bool is_template;
12255
0a3b29ad 12256 /* Consume the `='. */
12257 cp_lexer_consume_token (parser->lexer);
12258 /* Parse the id-expression. */
23010bc8 12259 push_deferring_access_checks (dk_no_deferred);
ad9ae192 12260 /* save token before parsing the id-expression, for error
12261 reporting */
12262 token = cp_lexer_peek_token (parser->lexer);
ccb84981 12263 default_argument
0a3b29ad 12264 = cp_parser_id_expression (parser,
12265 /*template_keyword_p=*/false,
12266 /*check_dependency_p=*/true,
c3b9e457 12267 /*template_p=*/&is_template,
197c9df7 12268 /*declarator_p=*/false,
130bb1d4 12269 /*optional_p=*/false);
eae805b4 12270 if (TREE_CODE (default_argument) == TYPE_DECL)
12271 /* If the id-expression was a template-id that refers to
12272 a template-class, we already have the declaration here,
12273 so no further lookup is needed. */
12274 ;
12275 else
12276 /* Look up the name. */
ccb84981 12277 default_argument
eae805b4 12278 = cp_parser_lookup_name (parser, default_argument,
e2ae55f2 12279 none_type,
12280 /*is_template=*/is_template,
12281 /*is_namespace=*/false,
12282 /*check_dependency=*/true,
ad9ae192 12283 /*ambiguous_decls=*/NULL,
12284 token->location);
0a3b29ad 12285 /* See if the default argument is valid. */
12286 default_argument
12287 = check_template_template_default_arg (default_argument);
d95d815d 12288
12289 /* Template parameter packs cannot have default
12290 arguments. */
12291 if (*is_parameter_pack)
12292 {
12293 if (identifier)
ccb59bb6 12294 error_at (token->location,
12295 "template parameter pack %qD cannot "
12296 "have a default argument",
12297 identifier);
d95d815d 12298 else
ccb59bb6 12299 error_at (token->location, "template parameter packs cannot "
12300 "have default arguments");
d95d815d 12301 default_argument = NULL_TREE;
12302 }
23010bc8 12303 pop_deferring_access_checks ();
0a3b29ad 12304 }
12305 else
12306 default_argument = NULL_TREE;
12307
12308 /* Create the combined representation of the parameter and the
12309 default argument. */
2a03dcc3 12310 parameter = build_tree_list (default_argument, parameter);
0a3b29ad 12311 }
12312 break;
12313
12314 default:
2a03dcc3 12315 gcc_unreachable ();
12316 break;
0a3b29ad 12317 }
ccb84981 12318
0a3b29ad 12319 return parameter;
12320}
12321
12322/* Parse a template-id.
12323
12324 template-id:
12325 template-name < template-argument-list [opt] >
12326
12327 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12328 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12329 returned. Otherwise, if the template-name names a function, or set
12330 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
ccb84981 12331 names a class, returns a TYPE_DECL for the specialization.
0a3b29ad 12332
12333 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12334 uninstantiated templates. */
12335
12336static tree
ccb84981 12337cp_parser_template_id (cp_parser *parser,
12338 bool template_keyword_p,
3d0f901b 12339 bool check_dependency_p,
12340 bool is_declaration)
0a3b29ad 12341{
3369eb76 12342 int i;
607a5d68 12343 tree templ;
0a3b29ad 12344 tree arguments;
0a3b29ad 12345 tree template_id;
19273cc2 12346 cp_token_position start_of_id = 0;
3369eb76 12347 deferred_access_check *chk;
12348 VEC (deferred_access_check,gc) *access_check;
8e9e8d76 12349 cp_token *next_token = NULL, *next_token_2 = NULL;
3d0f901b 12350 bool is_identifier;
0a3b29ad 12351
12352 /* If the next token corresponds to a template-id, there is no need
12353 to reparse it. */
b3c48b5d 12354 next_token = cp_lexer_peek_token (parser->lexer);
12355 if (next_token->type == CPP_TEMPLATE_ID)
0a3b29ad 12356 {
3369eb76 12357 struct tree_check *check_value;
0a3b29ad 12358
12359 /* Get the stored value. */
3369eb76 12360 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
0a3b29ad 12361 /* Perform any access checks that were deferred. */
3369eb76 12362 access_check = check_value->checks;
12363 if (access_check)
12364 {
48148244 12365 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12366 perform_or_defer_access_check (chk->binfo,
12367 chk->decl,
12368 chk->diag_decl);
3369eb76 12369 }
0a3b29ad 12370 /* Return the stored value. */
3369eb76 12371 return check_value->value;
0a3b29ad 12372 }
12373
b3c48b5d 12374 /* Avoid performing name lookup if there is no possibility of
12375 finding a template-id. */
12376 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12377 || (next_token->type == CPP_NAME
ccb84981 12378 && !cp_parser_nth_token_starts_template_argument_list_p
c8d5ab79 12379 (parser, 2)))
b3c48b5d 12380 {
12381 cp_parser_error (parser, "expected template-id");
12382 return error_mark_node;
12383 }
12384
0a3b29ad 12385 /* Remember where the template-id starts. */
efcbcf83 12386 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
19273cc2 12387 start_of_id = cp_lexer_token_position (parser->lexer, false);
0a3b29ad 12388
4f62c42e 12389 push_deferring_access_checks (dk_deferred);
9b57b06b 12390
0a3b29ad 12391 /* Parse the template-name. */
3d0f901b 12392 is_identifier = false;
607a5d68 12393 templ = cp_parser_template_name (parser, template_keyword_p,
12394 check_dependency_p,
12395 is_declaration,
12396 &is_identifier);
12397 if (templ == error_mark_node || is_identifier)
9b57b06b 12398 {
12399 pop_deferring_access_checks ();
607a5d68 12400 return templ;
9b57b06b 12401 }
0a3b29ad 12402
ccb84981 12403 /* If we find the sequence `[:' after a template-name, it's probably
c8d5ab79 12404 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12405 parse correctly the argument list. */
b9dd3954 12406 next_token = cp_lexer_peek_token (parser->lexer);
c8d5ab79 12407 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
ccb84981 12408 if (next_token->type == CPP_OPEN_SQUARE
c8d5ab79 12409 && next_token->flags & DIGRAPH
ccb84981 12410 && next_token_2->type == CPP_COLON
c8d5ab79 12411 && !(next_token_2->flags & PREV_WHITE))
9b57b06b 12412 {
c8d5ab79 12413 cp_parser_parse_tentatively (parser);
12414 /* Change `:' into `::'. */
12415 next_token_2->type = CPP_SCOPE;
12416 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
653e5405 12417 CPP_LESS. */
c8d5ab79 12418 cp_lexer_consume_token (parser->lexer);
ad9ae192 12419
c8d5ab79 12420 /* Parse the arguments. */
12421 arguments = cp_parser_enclosed_template_argument_list (parser);
12422 if (!cp_parser_parse_definitely (parser))
12423 {
12424 /* If we couldn't parse an argument list, then we revert our changes
12425 and return simply an error. Maybe this is not a template-id
12426 after all. */
12427 next_token_2->type = CPP_COLON;
a2c5b975 12428 cp_parser_error (parser, "expected %<<%>");
c8d5ab79 12429 pop_deferring_access_checks ();
12430 return error_mark_node;
12431 }
12432 /* Otherwise, emit an error about the invalid digraph, but continue
653e5405 12433 parsing because we got our argument list. */
2b9e3597 12434 if (permerror (next_token->location,
12435 "%<<::%> cannot begin a template-argument list"))
a52d5726 12436 {
12437 static bool hint = false;
5bcc316e 12438 inform (next_token->location,
12439 "%<<:%> is an alternate spelling for %<[%>."
12440 " Insert whitespace between %<<%> and %<::%>");
a52d5726 12441 if (!hint && !flag_permissive)
c8d5ab79 12442 {
5bcc316e 12443 inform (next_token->location, "(if you use %<-fpermissive%>"
12444 " G++ will accept your code)");
c8d5ab79 12445 hint = true;
12446 }
12447 }
12448 }
12449 else
12450 {
12451 /* Look for the `<' that starts the template-argument-list. */
c247dce0 12452 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
c8d5ab79 12453 {
12454 pop_deferring_access_checks ();
12455 return error_mark_node;
12456 }
12457 /* Parse the arguments. */
12458 arguments = cp_parser_enclosed_template_argument_list (parser);
9b57b06b 12459 }
0a3b29ad 12460
12461 /* Build a representation of the specialization. */
607a5d68 12462 if (TREE_CODE (templ) == IDENTIFIER_NODE)
255b5d15 12463 template_id = build_min_nt_loc (next_token->location,
12464 TEMPLATE_ID_EXPR,
12465 templ, arguments);
370478b1 12466 else if (DECL_TYPE_TEMPLATE_P (templ)
607a5d68 12467 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
ba0c587d 12468 {
12469 bool entering_scope;
12470 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12471 template (rather than some instantiation thereof) only if
12472 is not nested within some other construct. For example, in
12473 "template <typename T> void f(T) { A<T>::", A<T> is just an
12474 instantiation of A. */
12475 entering_scope = (template_parm_scope_p ()
12476 && cp_lexer_next_token_is (parser->lexer,
12477 CPP_SCOPE));
12478 template_id
607a5d68 12479 = finish_template_type (templ, arguments, entering_scope);
ba0c587d 12480 }
0a3b29ad 12481 else
12482 {
12483 /* If it's not a class-template or a template-template, it should be
12484 a function-template. */
607a5d68 12485 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12486 || TREE_CODE (templ) == OVERLOAD
12487 || BASELINK_P (templ)));
ccb84981 12488
607a5d68 12489 template_id = lookup_template_function (templ, arguments);
0a3b29ad 12490 }
ccb84981 12491
0a3b29ad 12492 /* If parsing tentatively, replace the sequence of tokens that makes
12493 up the template-id with a CPP_TEMPLATE_ID token. That way,
12494 should we re-parse the token stream, we will not have to repeat
12495 the effort required to do the parse, nor will we issue duplicate
12496 error messages about problems during instantiation of the
459742a6 12497 template. */
67635103 12498 if (start_of_id)
0a3b29ad 12499 {
19273cc2 12500 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9031d10b 12501
0a3b29ad 12502 /* Reset the contents of the START_OF_ID token. */
12503 token->type = CPP_TEMPLATE_ID;
3369eb76 12504 /* Retrieve any deferred checks. Do not pop this access checks yet
12505 so the memory will not be reclaimed during token replacing below. */
ba72912a 12506 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
3369eb76 12507 token->u.tree_check_value->value = template_id;
12508 token->u.tree_check_value->checks = get_deferred_access_checks ();
0a3b29ad 12509 token->keyword = RID_MAX;
9031d10b 12510
0a3b29ad 12511 /* Purge all subsequent tokens. */
19273cc2 12512 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
67635103 12513
12514 /* ??? Can we actually assume that, if template_id ==
12515 error_mark_node, we will have issued a diagnostic to the
12516 user, as opposed to simply marking the tentative parse as
12517 failed? */
12518 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
ccb59bb6 12519 error_at (token->location, "parse error in template argument list");
0a3b29ad 12520 }
12521
9b57b06b 12522 pop_deferring_access_checks ();
0a3b29ad 12523 return template_id;
12524}
12525
12526/* Parse a template-name.
12527
12528 template-name:
12529 identifier
ccb84981 12530
0a3b29ad 12531 The standard should actually say:
12532
12533 template-name:
12534 identifier
12535 operator-function-id
0a3b29ad 12536
12537 A defect report has been filed about this issue.
12538
182d094e 12539 A conversion-function-id cannot be a template name because they cannot
12540 be part of a template-id. In fact, looking at this code:
12541
12542 a.operator K<int>()
12543
12544 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
ccb84981 12545 It is impossible to call a templated conversion-function-id with an
182d094e 12546 explicit argument list, since the only allowed template parameter is
12547 the type to which it is converting.
12548
0a3b29ad 12549 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12550 `template' keyword, in a construction like:
12551
12552 T::template f<3>()
12553
12554 In that case `f' is taken to be a template-name, even though there
12555 is no way of knowing for sure.
12556
12557 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12558 name refers to a set of overloaded functions, at least one of which
12559 is a template, or an IDENTIFIER_NODE with the name of the template,
12560 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12561 names are looked up inside uninstantiated templates. */
12562
12563static tree
ccb84981 12564cp_parser_template_name (cp_parser* parser,
653e5405 12565 bool template_keyword_p,
12566 bool check_dependency_p,
3d0f901b 12567 bool is_declaration,
12568 bool *is_identifier)
0a3b29ad 12569{
12570 tree identifier;
12571 tree decl;
12572 tree fns;
ad9ae192 12573 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 12574
12575 /* If the next token is `operator', then we have either an
12576 operator-function-id or a conversion-function-id. */
12577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12578 {
12579 /* We don't know whether we're looking at an
12580 operator-function-id or a conversion-function-id. */
12581 cp_parser_parse_tentatively (parser);
12582 /* Try an operator-function-id. */
12583 identifier = cp_parser_operator_function_id (parser);
12584 /* If that didn't work, try a conversion-function-id. */
12585 if (!cp_parser_parse_definitely (parser))
653e5405 12586 {
182d094e 12587 cp_parser_error (parser, "expected template-name");
12588 return error_mark_node;
653e5405 12589 }
0a3b29ad 12590 }
12591 /* Look for the identifier. */
12592 else
12593 identifier = cp_parser_identifier (parser);
ccb84981 12594
0a3b29ad 12595 /* If we didn't find an identifier, we don't have a template-id. */
12596 if (identifier == error_mark_node)
12597 return error_mark_node;
12598
12599 /* If the name immediately followed the `template' keyword, then it
12600 is a template-name. However, if the next token is not `<', then
12601 we do not treat it as a template-name, since it is not being used
12602 as part of a template-id. This enables us to handle constructs
12603 like:
12604
12605 template <typename T> struct S { S(); };
12606 template <typename T> S<T>::S();
12607
12608 correctly. We would treat `S' as a template -- if it were `S<T>'
12609 -- but we do not if there is no `<'. */
3d0f901b 12610
12611 if (processing_template_decl
c8d5ab79 12612 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
3d0f901b 12613 {
12614 /* In a declaration, in a dependent context, we pretend that the
12615 "template" keyword was present in order to improve error
12616 recovery. For example, given:
ccb84981 12617
3d0f901b 12618 template <typename T> void f(T::X<int>);
ccb84981 12619
3d0f901b 12620 we want to treat "X<int>" as a template-id. */
ccb84981 12621 if (is_declaration
12622 && !template_keyword_p
3d0f901b 12623 && parser->scope && TYPE_P (parser->scope)
0078a5e8 12624 && check_dependency_p
05f701e2 12625 && dependent_scope_p (parser->scope)
9ed82c65 12626 /* Do not do this for dtors (or ctors), since they never
12627 need the template keyword before their name. */
12628 && !constructor_name_p (identifier, parser->scope))
3d0f901b 12629 {
19273cc2 12630 cp_token_position start = 0;
9031d10b 12631
3d0f901b 12632 /* Explain what went wrong. */
ccb59bb6 12633 error_at (token->location, "non-template %qD used as template",
12634 identifier);
12635 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
9ed82c65 12636 parser->scope, identifier);
efcbcf83 12637 /* If parsing tentatively, find the location of the "<" token. */
12638 if (cp_parser_simulate_error (parser))
12639 start = cp_lexer_token_position (parser->lexer, true);
3d0f901b 12640 /* Parse the template arguments so that we can issue error
12641 messages about them. */
12642 cp_lexer_consume_token (parser->lexer);
12643 cp_parser_enclosed_template_argument_list (parser);
12644 /* Skip tokens until we find a good place from which to
12645 continue parsing. */
12646 cp_parser_skip_to_closing_parenthesis (parser,
12647 /*recovering=*/true,
12648 /*or_comma=*/true,
12649 /*consume_paren=*/false);
12650 /* If parsing tentatively, permanently remove the
12651 template argument list. That will prevent duplicate
12652 error messages from being issued about the missing
12653 "template" keyword. */
19273cc2 12654 if (start)
12655 cp_lexer_purge_tokens_after (parser->lexer, start);
3d0f901b 12656 if (is_identifier)
12657 *is_identifier = true;
12658 return identifier;
12659 }
3180c04a 12660
12661 /* If the "template" keyword is present, then there is generally
12662 no point in doing name-lookup, so we just return IDENTIFIER.
12663 But, if the qualifying scope is non-dependent then we can
12664 (and must) do name-lookup normally. */
12665 if (template_keyword_p
12666 && (!parser->scope
207355ad 12667 || (TYPE_P (parser->scope)
3180c04a 12668 && dependent_type_p (parser->scope))))
3d0f901b 12669 return identifier;
12670 }
0a3b29ad 12671
12672 /* Look up the name. */
12673 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 12674 none_type,
6dd3a38d 12675 /*is_template=*/true,
6fc758aa 12676 /*is_namespace=*/false,
2cdbcd51 12677 check_dependency_p,
ad9ae192 12678 /*ambiguous_decls=*/NULL,
12679 token->location);
0a3b29ad 12680
12681 /* If DECL is a template, then the name was a template-name. */
12682 if (TREE_CODE (decl) == TEMPLATE_DECL)
12683 ;
ccb84981 12684 else
0a3b29ad 12685 {
a5a297d8 12686 tree fn = NULL_TREE;
12687
0a3b29ad 12688 /* The standard does not explicitly indicate whether a name that
12689 names a set of overloaded declarations, some of which are
12690 templates, is a template-name. However, such a name should
12691 be a template-name; otherwise, there is no way to form a
12692 template-id for the overloaded templates. */
12693 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12694 if (TREE_CODE (fns) == OVERLOAD)
a5a297d8 12695 for (fn = fns; fn; fn = OVL_NEXT (fn))
12696 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12697 break;
ccb84981 12698
a5a297d8 12699 if (!fn)
0a3b29ad 12700 {
a5a297d8 12701 /* The name does not name a template. */
0a3b29ad 12702 cp_parser_error (parser, "expected template-name");
12703 return error_mark_node;
12704 }
12705 }
12706
12707 /* If DECL is dependent, and refers to a function, then just return
12708 its name; we will look it up again during template instantiation. */
12709 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12710 {
47744737 12711 tree scope = ovl_scope (decl);
7e9a6a16 12712 if (TYPE_P (scope) && dependent_type_p (scope))
0a3b29ad 12713 return identifier;
12714 }
12715
12716 return decl;
12717}
12718
12719/* Parse a template-argument-list.
12720
12721 template-argument-list:
d95d815d 12722 template-argument ... [opt]
12723 template-argument-list , template-argument ... [opt]
0a3b29ad 12724
bd8962d5 12725 Returns a TREE_VEC containing the arguments. */
0a3b29ad 12726
12727static tree
45baea8b 12728cp_parser_template_argument_list (cp_parser* parser)
0a3b29ad 12729{
b5959ba9 12730 tree fixed_args[10];
12731 unsigned n_args = 0;
12732 unsigned alloced = 10;
12733 tree *arg_ary = fixed_args;
12734 tree vec;
92b128ed 12735 bool saved_in_template_argument_list_p;
45b4e1e4 12736 bool saved_ice_p;
12737 bool saved_non_ice_p;
0a3b29ad 12738
92b128ed 12739 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12740 parser->in_template_argument_list_p = true;
45b4e1e4 12741 /* Even if the template-id appears in an integral
074ab442 12742 constant-expression, the contents of the argument list do
12743 not. */
45b4e1e4 12744 saved_ice_p = parser->integral_constant_expression_p;
12745 parser->integral_constant_expression_p = false;
12746 saved_non_ice_p = parser->non_integral_constant_expression_p;
12747 parser->non_integral_constant_expression_p = false;
e6014a82 12748
fbb01da7 12749 /* Parse the arguments. */
b5959ba9 12750 do
0a3b29ad 12751 {
12752 tree argument;
12753
b5959ba9 12754 if (n_args)
bd8962d5 12755 /* Consume the comma. */
b5959ba9 12756 cp_lexer_consume_token (parser->lexer);
ccb84981 12757
0a3b29ad 12758 /* Parse the template-argument. */
12759 argument = cp_parser_template_argument (parser);
d95d815d 12760
12761 /* If the next token is an ellipsis, we're expanding a template
12762 argument pack. */
12763 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12764 {
7cdf9e98 12765 if (argument == error_mark_node)
12766 {
12767 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 12768 error_at (token->location,
12769 "expected parameter pack before %<...%>");
7cdf9e98 12770 }
d95d815d 12771 /* Consume the `...' token. */
12772 cp_lexer_consume_token (parser->lexer);
12773
12774 /* Make the argument into a TYPE_PACK_EXPANSION or
12775 EXPR_PACK_EXPANSION. */
12776 argument = make_pack_expansion (argument);
12777 }
12778
b5959ba9 12779 if (n_args == alloced)
12780 {
12781 alloced *= 2;
ccb84981 12782
b5959ba9 12783 if (arg_ary == fixed_args)
12784 {
56e60747 12785 arg_ary = XNEWVEC (tree, alloced);
b5959ba9 12786 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12787 }
12788 else
7ea410eb 12789 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
b5959ba9 12790 }
12791 arg_ary[n_args++] = argument;
0a3b29ad 12792 }
b5959ba9 12793 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12794
12795 vec = make_tree_vec (n_args);
0a3b29ad 12796
b5959ba9 12797 while (n_args--)
12798 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
ccb84981 12799
b5959ba9 12800 if (arg_ary != fixed_args)
12801 free (arg_ary);
45b4e1e4 12802 parser->non_integral_constant_expression_p = saved_non_ice_p;
12803 parser->integral_constant_expression_p = saved_ice_p;
92b128ed 12804 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
94c17f03 12805#ifdef ENABLE_CHECKING
12806 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12807#endif
b5959ba9 12808 return vec;
0a3b29ad 12809}
12810
12811/* Parse a template-argument.
12812
12813 template-argument:
12814 assignment-expression
12815 type-id
12816 id-expression
12817
12818 The representation is that of an assignment-expression, type-id, or
12819 id-expression -- except that the qualified id-expression is
12820 evaluated, so that the value returned is either a DECL or an
ccb84981 12821 OVERLOAD.
13795292 12822
12823 Although the standard says "assignment-expression", it forbids
12824 throw-expressions or assignments in the template argument.
12825 Therefore, we use "conditional-expression" instead. */
0a3b29ad 12826
12827static tree
45baea8b 12828cp_parser_template_argument (cp_parser* parser)
0a3b29ad 12829{
12830 tree argument;
12831 bool template_p;
13795292 12832 bool address_p;
bece9ea1 12833 bool maybe_type_id = false;
ad9ae192 12834 cp_token *token = NULL, *argument_start_token = NULL;
ef0b0c72 12835 location_t loc = 0;
0886adbc 12836 cp_id_kind idk;
0a3b29ad 12837
12838 /* There's really no way to know what we're looking at, so we just
ccb84981 12839 try each alternative in order.
0a3b29ad 12840
12841 [temp.arg]
12842
12843 In a template-argument, an ambiguity between a type-id and an
12844 expression is resolved to a type-id, regardless of the form of
ccb84981 12845 the corresponding template-parameter.
0a3b29ad 12846
12847 Therefore, we try a type-id first. */
12848 cp_parser_parse_tentatively (parser);
75eaa947 12849 argument = cp_parser_template_type_arg (parser);
7d46c9d7 12850 /* If there was no error parsing the type-id but the next token is a
12851 '>>', our behavior depends on which dialect of C++ we're
12852 parsing. In C++98, we probably found a typo for '> >'. But there
12853 are type-id which are also valid expressions. For instance:
bece9ea1 12854
12855 struct X { int operator >> (int); };
12856 template <int V> struct Foo {};
12857 Foo<X () >> 5> r;
12858
12859 Here 'X()' is a valid type-id of a function type, but the user just
12860 wanted to write the expression "X() >> 5". Thus, we remember that we
12861 found a valid type-id, but we still try to parse the argument as an
7d46c9d7 12862 expression to see what happens.
12863
12864 In C++0x, the '>>' will be considered two separate '>'
12865 tokens. */
bece9ea1 12866 if (!cp_parser_error_occurred (parser)
7d46c9d7 12867 && cxx_dialect == cxx98
bece9ea1 12868 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12869 {
12870 maybe_type_id = true;
12871 cp_parser_abort_tentative_parse (parser);
12872 }
12873 else
12874 {
12875 /* If the next token isn't a `,' or a `>', then this argument wasn't
12876 really finished. This means that the argument is not a valid
12877 type-id. */
12878 if (!cp_parser_next_token_ends_template_argument_p (parser))
12879 cp_parser_error (parser, "expected template-argument");
12880 /* If that worked, we're done. */
12881 if (cp_parser_parse_definitely (parser))
12882 return argument;
12883 }
0a3b29ad 12884 /* We're still not sure what the argument will be. */
12885 cp_parser_parse_tentatively (parser);
12886 /* Try a template. */
ad9ae192 12887 argument_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 12888 argument = cp_parser_id_expression (parser,
0a3b29ad 12889 /*template_keyword_p=*/false,
12890 /*check_dependency_p=*/true,
899cc6e8 12891 &template_p,
197c9df7 12892 /*declarator_p=*/false,
130bb1d4 12893 /*optional_p=*/false);
0a3b29ad 12894 /* If the next token isn't a `,' or a `>', then this argument wasn't
12895 really finished. */
13795292 12896 if (!cp_parser_next_token_ends_template_argument_p (parser))
0a3b29ad 12897 cp_parser_error (parser, "expected template-argument");
12898 if (!cp_parser_error_occurred (parser))
12899 {
4ec378b6 12900 /* Figure out what is being referred to. If the id-expression
12901 was for a class template specialization, then we will have a
12902 TYPE_DECL at this point. There is no need to do name lookup
12903 at this point in that case. */
12904 if (TREE_CODE (argument) != TYPE_DECL)
12905 argument = cp_parser_lookup_name (parser, argument,
e2ae55f2 12906 none_type,
4ec378b6 12907 /*is_template=*/template_p,
12908 /*is_namespace=*/false,
2cdbcd51 12909 /*check_dependency=*/true,
ad9ae192 12910 /*ambiguous_decls=*/NULL,
12911 argument_start_token->location);
11c54989 12912 if (TREE_CODE (argument) != TEMPLATE_DECL
12913 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
0a3b29ad 12914 cp_parser_error (parser, "expected template-name");
12915 }
12916 if (cp_parser_parse_definitely (parser))
12917 return argument;
13795292 12918 /* It must be a non-type argument. There permitted cases are given
12919 in [temp.arg.nontype]:
12920
12921 -- an integral constant-expression of integral or enumeration
653e5405 12922 type; or
13795292 12923
12924 -- the name of a non-type template-parameter; or
12925
12926 -- the name of an object or function with external linkage...
12927
12928 -- the address of an object or function with external linkage...
12929
bd8962d5 12930 -- a pointer to member... */
13795292 12931 /* Look for a non-type template parameter. */
12932 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12933 {
12934 cp_parser_parse_tentatively (parser);
12935 argument = cp_parser_primary_expression (parser,
08cc44e7 12936 /*address_p=*/false,
640aa28c 12937 /*cast_p=*/false,
fbb01da7 12938 /*template_arg_p=*/true,
12939 &idk);
13795292 12940 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12941 || !cp_parser_next_token_ends_template_argument_p (parser))
12942 cp_parser_simulate_error (parser);
12943 if (cp_parser_parse_definitely (parser))
12944 return argument;
12945 }
729f89ff 12946
13795292 12947 /* If the next token is "&", the argument must be the address of an
12948 object or function with external linkage. */
12949 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12950 if (address_p)
ef0b0c72 12951 {
12952 loc = cp_lexer_peek_token (parser->lexer)->location;
12953 cp_lexer_consume_token (parser->lexer);
12954 }
13795292 12955 /* See if we might have an id-expression. */
12956 token = cp_lexer_peek_token (parser->lexer);
12957 if (token->type == CPP_NAME
12958 || token->keyword == RID_OPERATOR
12959 || token->type == CPP_SCOPE
12960 || token->type == CPP_TEMPLATE_ID
12961 || token->type == CPP_NESTED_NAME_SPECIFIER)
12962 {
12963 cp_parser_parse_tentatively (parser);
12964 argument = cp_parser_primary_expression (parser,
fbb01da7 12965 address_p,
640aa28c 12966 /*cast_p=*/false,
fbb01da7 12967 /*template_arg_p=*/true,
12968 &idk);
13795292 12969 if (cp_parser_error_occurred (parser)
12970 || !cp_parser_next_token_ends_template_argument_p (parser))
12971 cp_parser_abort_tentative_parse (parser);
12972 else
12973 {
11026030 12974 tree probe;
12975
729f89ff 12976 if (TREE_CODE (argument) == INDIRECT_REF)
12977 {
12978 gcc_assert (REFERENCE_REF_P (argument));
12979 argument = TREE_OPERAND (argument, 0);
12980 }
9031d10b 12981
11026030 12982 /* If we're in a template, we represent a qualified-id referring
12983 to a static data member as a SCOPE_REF even if the scope isn't
12984 dependent so that we can check access control later. */
12985 probe = argument;
12986 if (TREE_CODE (probe) == SCOPE_REF)
12987 probe = TREE_OPERAND (probe, 1);
12988 if (TREE_CODE (probe) == VAR_DECL)
13795292 12989 {
12990 /* A variable without external linkage might still be a
12991 valid constant-expression, so no error is issued here
12992 if the external-linkage check fails. */
11026030 12993 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13795292 12994 cp_parser_simulate_error (parser);
12995 }
12996 else if (is_overloaded_fn (argument))
12997 /* All overloaded functions are allowed; if the external
12998 linkage test does not pass, an error will be issued
12999 later. */
13000 ;
13001 else if (address_p
ccb84981 13002 && (TREE_CODE (argument) == OFFSET_REF
13795292 13003 || TREE_CODE (argument) == SCOPE_REF))
13004 /* A pointer-to-member. */
13005 ;
729f89ff 13006 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13007 ;
13795292 13008 else
13009 cp_parser_simulate_error (parser);
13010
13011 if (cp_parser_parse_definitely (parser))
13012 {
13013 if (address_p)
ef0b0c72 13014 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
13015 tf_warning_or_error);
13795292 13016 return argument;
13017 }
13018 }
13019 }
13020 /* If the argument started with "&", there are no other valid
13021 alternatives at this point. */
13022 if (address_p)
13023 {
13024 cp_parser_error (parser, "invalid non-type template argument");
13025 return error_mark_node;
13026 }
729f89ff 13027
bece9ea1 13028 /* If the argument wasn't successfully parsed as a type-id followed
ccb84981 13029 by '>>', the argument can only be a constant expression now.
bece9ea1 13030 Otherwise, we try parsing the constant-expression tentatively,
13031 because the argument could really be a type-id. */
13032 if (maybe_type_id)
13033 cp_parser_parse_tentatively (parser);
ccb84981 13034 argument = cp_parser_constant_expression (parser,
13795292 13035 /*allow_non_constant_p=*/false,
13036 /*non_constant_p=*/NULL);
2250d32c 13037 argument = fold_non_dependent_expr (argument);
bece9ea1 13038 if (!maybe_type_id)
13039 return argument;
13040 if (!cp_parser_next_token_ends_template_argument_p (parser))
13041 cp_parser_error (parser, "expected template-argument");
13042 if (cp_parser_parse_definitely (parser))
13043 return argument;
13044 /* We did our best to parse the argument as a non type-id, but that
13045 was the only alternative that matched (albeit with a '>' after
ccb84981 13046 it). We can assume it's just a typo from the user, and a
bece9ea1 13047 diagnostic will then be issued. */
75eaa947 13048 return cp_parser_template_type_arg (parser);
0a3b29ad 13049}
13050
13051/* Parse an explicit-instantiation.
13052
13053 explicit-instantiation:
ccb84981 13054 template declaration
0a3b29ad 13055
13056 Although the standard says `declaration', what it really means is:
13057
13058 explicit-instantiation:
ccb84981 13059 template decl-specifier-seq [opt] declarator [opt] ;
0a3b29ad 13060
13061 Things like `template int S<int>::i = 5, int S<double>::j;' are not
13062 supposed to be allowed. A defect report has been filed about this
ccb84981 13063 issue.
0a3b29ad 13064
13065 GNU Extension:
ccb84981 13066
0a3b29ad 13067 explicit-instantiation:
ccb84981 13068 storage-class-specifier template
0a3b29ad 13069 decl-specifier-seq [opt] declarator [opt] ;
ccb84981 13070 function-specifier template
0a3b29ad 13071 decl-specifier-seq [opt] declarator [opt] ; */
13072
13073static void
45baea8b 13074cp_parser_explicit_instantiation (cp_parser* parser)
0a3b29ad 13075{
8172be22 13076 int declares_class_or_enum;
4b9b2871 13077 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 13078 tree extension_specifier = NULL_TREE;
13079
6198e8f6 13080 timevar_push (TV_TEMPLATE_INST);
13081
0a3b29ad 13082 /* Look for an (optional) storage-class-specifier or
13083 function-specifier. */
13084 if (cp_parser_allow_gnu_extensions_p (parser))
13085 {
ccb84981 13086 extension_specifier
0a3b29ad 13087 = cp_parser_storage_class_specifier_opt (parser);
13088 if (!extension_specifier)
207355ad 13089 extension_specifier
4b9b2871 13090 = cp_parser_function_specifier_opt (parser,
13091 /*decl_specs=*/NULL);
0a3b29ad 13092 }
13093
13094 /* Look for the `template' keyword. */
c247dce0 13095 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
0a3b29ad 13096 /* Let the front end know that we are processing an explicit
13097 instantiation. */
13098 begin_explicit_instantiation ();
13099 /* [temp.explicit] says that we are supposed to ignore access
13100 control while processing explicit instantiation directives. */
4cab8273 13101 push_deferring_access_checks (dk_no_check);
0a3b29ad 13102 /* Parse a decl-specifier-seq. */
4b9b2871 13103 cp_parser_decl_specifier_seq (parser,
13104 CP_PARSER_FLAGS_OPTIONAL,
13105 &decl_specifiers,
13106 &declares_class_or_enum);
0a3b29ad 13107 /* If there was exactly one decl-specifier, and it declared a class,
13108 and there's no declarator, then we have an explicit type
13109 instantiation. */
13110 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13111 {
13112 tree type;
13113
4b9b2871 13114 type = check_tag_decl (&decl_specifiers);
6bdc805e 13115 /* Turn access control back on for names used during
13116 template instantiation. */
13117 pop_deferring_access_checks ();
0a3b29ad 13118 if (type)
c31fdaf6 13119 do_type_instantiation (type, extension_specifier,
074ab442 13120 /*complain=*/tf_error);
0a3b29ad 13121 }
13122 else
13123 {
3046c0a3 13124 cp_declarator *declarator;
0a3b29ad 13125 tree decl;
13126
13127 /* Parse the declarator. */
ccb84981 13128 declarator
42bbd0ec 13129 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 13130 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 13131 /*parenthesized_p=*/NULL,
13132 /*member_p=*/false);
e2ae55f2 13133 if (declares_class_or_enum & 2)
13134 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 13135 decl_specifiers.type,
a60f3e81 13136 decl_specifiers.locations[ds_type_spec]);
3046c0a3 13137 if (declarator != cp_error_declarator)
e1a6bbd7 13138 {
a60f3e81 13139 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
13140 permerror (decl_specifiers.locations[ds_inline],
13141 "explicit instantiation shall not use"
ca63c29a 13142 " %<inline%> specifier");
a60f3e81 13143 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
13144 permerror (decl_specifiers.locations[ds_constexpr],
13145 "explicit instantiation shall not use"
ca63c29a 13146 " %<constexpr%> specifier");
13147
4b9b2871 13148 decl = grokdeclarator (declarator, &decl_specifiers,
4a2849cb 13149 NORMAL, 0, &decl_specifiers.attributes);
e1a6bbd7 13150 /* Turn access control back on for names used during
13151 template instantiation. */
13152 pop_deferring_access_checks ();
13153 /* Do the explicit instantiation. */
13154 do_decl_instantiation (decl, extension_specifier);
13155 }
13156 else
13157 {
13158 pop_deferring_access_checks ();
13159 /* Skip the body of the explicit instantiation. */
13160 cp_parser_skip_to_end_of_statement (parser);
13161 }
0a3b29ad 13162 }
13163 /* We're done with the instantiation. */
13164 end_explicit_instantiation ();
0a3b29ad 13165
cf91b86a 13166 cp_parser_consume_semicolon_at_end_of_statement (parser);
6198e8f6 13167
13168 timevar_pop (TV_TEMPLATE_INST);
0a3b29ad 13169}
13170
13171/* Parse an explicit-specialization.
13172
13173 explicit-specialization:
ccb84981 13174 template < > declaration
0a3b29ad 13175
13176 Although the standard says `declaration', what it really means is:
13177
13178 explicit-specialization:
13179 template <> decl-specifier [opt] init-declarator [opt] ;
ccb84981 13180 template <> function-definition
0a3b29ad 13181 template <> explicit-specialization
13182 template <> template-declaration */
13183
13184static void
45baea8b 13185cp_parser_explicit_specialization (cp_parser* parser)
0a3b29ad 13186{
9f25cdd8 13187 bool need_lang_pop;
ad9ae192 13188 cp_token *token = cp_lexer_peek_token (parser->lexer);
13189
0a3b29ad 13190 /* Look for the `template' keyword. */
c247dce0 13191 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
0a3b29ad 13192 /* Look for the `<'. */
c247dce0 13193 cp_parser_require (parser, CPP_LESS, RT_LESS);
0a3b29ad 13194 /* Look for the `>'. */
c247dce0 13195 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
0a3b29ad 13196 /* We have processed another parameter list. */
13197 ++parser->num_template_parameter_lists;
9f25cdd8 13198 /* [temp]
074ab442 13199
9f25cdd8 13200 A template ... explicit specialization ... shall not have C
074ab442 13201 linkage. */
9f25cdd8 13202 if (current_lang_name == lang_name_c)
13203 {
ccb59bb6 13204 error_at (token->location, "template specialization with C linkage");
9f25cdd8 13205 /* Give it C++ linkage to avoid confusing other parts of the
13206 front end. */
13207 push_lang_context (lang_name_cplusplus);
13208 need_lang_pop = true;
13209 }
13210 else
13211 need_lang_pop = false;
0a3b29ad 13212 /* Let the front end know that we are beginning a specialization. */
6d9bff9f 13213 if (!begin_specialization ())
13214 {
13215 end_specialization ();
6d9bff9f 13216 return;
13217 }
13218
0a3b29ad 13219 /* If the next keyword is `template', we need to figure out whether
13220 or not we're looking a template-declaration. */
13221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13222 {
13223 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13224 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13225 cp_parser_template_declaration_after_export (parser,
13226 /*member_p=*/false);
13227 else
13228 cp_parser_explicit_specialization (parser);
13229 }
13230 else
13231 /* Parse the dependent declaration. */
ccb84981 13232 cp_parser_single_declaration (parser,
3369eb76 13233 /*checks=*/NULL,
0a3b29ad 13234 /*member_p=*/false,
0c032b46 13235 /*explicit_specialization_p=*/true,
0a3b29ad 13236 /*friend_p=*/NULL);
0a3b29ad 13237 /* We're done with the specialization. */
13238 end_specialization ();
9f25cdd8 13239 /* For the erroneous case of a template with C linkage, we pushed an
13240 implicit C++ linkage scope; exit that scope now. */
13241 if (need_lang_pop)
13242 pop_lang_context ();
0a3b29ad 13243 /* We're done with this parameter list. */
13244 --parser->num_template_parameter_lists;
13245}
13246
13247/* Parse a type-specifier.
13248
13249 type-specifier:
13250 simple-type-specifier
13251 class-specifier
13252 enum-specifier
13253 elaborated-type-specifier
13254 cv-qualifier
13255
13256 GNU Extension:
13257
13258 type-specifier:
13259 __complex__
13260
4b9b2871 13261 Returns a representation of the type-specifier. For a
13262 class-specifier, enum-specifier, or elaborated-type-specifier, a
13263 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
0a3b29ad 13264
fb871e92 13265 The parser flags FLAGS is used to control type-specifier parsing.
13266
13267 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13268 in a decl-specifier-seq.
0a3b29ad 13269
13270 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13271 class-specifier, enum-specifier, or elaborated-type-specifier, then
678704be 13272 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
8172be22 13273 if a type is declared; 2 if it is defined. Otherwise, it is set to
13274 zero.
0a3b29ad 13275
13276 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13277 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13278 is set to FALSE. */
13279
13280static tree
ccb84981 13281cp_parser_type_specifier (cp_parser* parser,
13282 cp_parser_flags flags,
4b9b2871 13283 cp_decl_specifier_seq *decl_specs,
45baea8b 13284 bool is_declaration,
8172be22 13285 int* declares_class_or_enum,
45baea8b 13286 bool* is_cv_qualifier)
0a3b29ad 13287{
13288 tree type_spec = NULL_TREE;
13289 cp_token *token;
13290 enum rid keyword;
4b9b2871 13291 cp_decl_spec ds = ds_last;
0a3b29ad 13292
13293 /* Assume this type-specifier does not declare a new type. */
13294 if (declares_class_or_enum)
8e594c09 13295 *declares_class_or_enum = 0;
0a3b29ad 13296 /* And that it does not specify a cv-qualifier. */
13297 if (is_cv_qualifier)
13298 *is_cv_qualifier = false;
13299 /* Peek at the next token. */
13300 token = cp_lexer_peek_token (parser->lexer);
13301
13302 /* If we're looking at a keyword, we can use that to guide the
13303 production we choose. */
13304 keyword = token->keyword;
13305 switch (keyword)
13306 {
637fdc50 13307 case RID_ENUM:
638569c5 13308 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13309 goto elaborated_type_specifier;
13310
4a2849cb 13311 /* Look for the enum-specifier. */
13312 type_spec = cp_parser_enum_specifier (parser);
13313 /* If that worked, we're done. */
13314 if (type_spec)
637fdc50 13315 {
637fdc50 13316 if (declares_class_or_enum)
13317 *declares_class_or_enum = 2;
13318 if (decl_specs)
13319 cp_parser_set_decl_spec_type (decl_specs,
13320 type_spec,
eef0ab03 13321 token->location,
1bc28cb0 13322 /*type_definition_p=*/true);
637fdc50 13323 return type_spec;
13324 }
13325 else
13326 goto elaborated_type_specifier;
13327
0a3b29ad 13328 /* Any of these indicate either a class-specifier, or an
13329 elaborated-type-specifier. */
13330 case RID_CLASS:
13331 case RID_STRUCT:
13332 case RID_UNION:
638569c5 13333 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13334 goto elaborated_type_specifier;
13335
0a3b29ad 13336 /* Parse tentatively so that we can back up if we don't find a
637fdc50 13337 class-specifier. */
0a3b29ad 13338 cp_parser_parse_tentatively (parser);
637fdc50 13339 /* Look for the class-specifier. */
13340 type_spec = cp_parser_class_specifier (parser);
dd2b35f1 13341 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
0a3b29ad 13342 /* If that worked, we're done. */
13343 if (cp_parser_parse_definitely (parser))
13344 {
13345 if (declares_class_or_enum)
8172be22 13346 *declares_class_or_enum = 2;
4b9b2871 13347 if (decl_specs)
13348 cp_parser_set_decl_spec_type (decl_specs,
13349 type_spec,
eef0ab03 13350 token->location,
1bc28cb0 13351 /*type_definition_p=*/true);
0a3b29ad 13352 return type_spec;
13353 }
13354
13355 /* Fall through. */
637fdc50 13356 elaborated_type_specifier:
13357 /* We're declaring (not defining) a class or enum. */
13358 if (declares_class_or_enum)
13359 *declares_class_or_enum = 1;
0a3b29ad 13360
637fdc50 13361 /* Fall through. */
0a3b29ad 13362 case RID_TYPENAME:
13363 /* Look for an elaborated-type-specifier. */
207355ad 13364 type_spec
13365 = (cp_parser_elaborated_type_specifier
4b9b2871 13366 (parser,
a60f3e81 13367 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
4b9b2871 13368 is_declaration));
4b9b2871 13369 if (decl_specs)
13370 cp_parser_set_decl_spec_type (decl_specs,
13371 type_spec,
eef0ab03 13372 token->location,
1bc28cb0 13373 /*type_definition_p=*/false);
0a3b29ad 13374 return type_spec;
13375
13376 case RID_CONST:
4b9b2871 13377 ds = ds_const;
13378 if (is_cv_qualifier)
13379 *is_cv_qualifier = true;
13380 break;
207355ad 13381
0a3b29ad 13382 case RID_VOLATILE:
4b9b2871 13383 ds = ds_volatile;
0a3b29ad 13384 if (is_cv_qualifier)
13385 *is_cv_qualifier = true;
4b9b2871 13386 break;
0a3b29ad 13387
4b9b2871 13388 case RID_RESTRICT:
13389 ds = ds_restrict;
13390 if (is_cv_qualifier)
13391 *is_cv_qualifier = true;
13392 break;
0a3b29ad 13393
13394 case RID_COMPLEX:
13395 /* The `__complex__' keyword is a GNU extension. */
4b9b2871 13396 ds = ds_complex;
13397 break;
0a3b29ad 13398
13399 default:
13400 break;
13401 }
13402
4b9b2871 13403 /* Handle simple keywords. */
13404 if (ds != ds_last)
13405 {
13406 if (decl_specs)
13407 {
a60f3e81 13408 set_and_check_decl_spec_loc (decl_specs, ds, token->location);
4b9b2871 13409 decl_specs->any_specifiers_p = true;
13410 }
3369eb76 13411 return cp_lexer_consume_token (parser->lexer)->u.value;
4b9b2871 13412 }
13413
0a3b29ad 13414 /* If we do not already have a type-specifier, assume we are looking
13415 at a simple-type-specifier. */
207355ad 13416 type_spec = cp_parser_simple_type_specifier (parser,
4b9b2871 13417 decl_specs,
13418 flags);
0a3b29ad 13419
13420 /* If we didn't find a type-specifier, and a type-specifier was not
13421 optional in this context, issue an error message. */
13422 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13423 {
13424 cp_parser_error (parser, "expected type specifier");
13425 return error_mark_node;
13426 }
13427
13428 return type_spec;
13429}
13430
13431/* Parse a simple-type-specifier.
13432
13433 simple-type-specifier:
13434 :: [opt] nested-name-specifier [opt] type-name
13435 :: [opt] nested-name-specifier template template-id
13436 char
13437 wchar_t
13438 bool
13439 short
13440 int
13441 long
13442 signed
13443 unsigned
13444 float
13445 double
ccb84981 13446 void
0a3b29ad 13447
34da8800 13448 C++0x Extension:
13449
13450 simple-type-specifier:
45b44d0a 13451 auto
34da8800 13452 decltype ( expression )
924bbf02 13453 char16_t
13454 char32_t
8de5c43e 13455 __underlying_type ( type-id )
34da8800 13456
0a3b29ad 13457 GNU Extension:
13458
13459 simple-type-specifier:
6388cfe2 13460 __int128
0a3b29ad 13461 __typeof__ unary-expression
13462 __typeof__ ( type-id )
13463
4b9b2871 13464 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13465 appropriately updated. */
0a3b29ad 13466
13467static tree
207355ad 13468cp_parser_simple_type_specifier (cp_parser* parser,
4b9b2871 13469 cp_decl_specifier_seq *decl_specs,
13470 cp_parser_flags flags)
0a3b29ad 13471{
13472 tree type = NULL_TREE;
13473 cp_token *token;
13474
13475 /* Peek at the next token. */
13476 token = cp_lexer_peek_token (parser->lexer);
13477
13478 /* If we're looking at a keyword, things are easy. */
13479 switch (token->keyword)
13480 {
13481 case RID_CHAR:
4b9b2871 13482 if (decl_specs)
13483 decl_specs->explicit_char_p = true;
b7d1e8ea 13484 type = char_type_node;
13485 break;
924bbf02 13486 case RID_CHAR16:
13487 type = char16_type_node;
13488 break;
13489 case RID_CHAR32:
13490 type = char32_type_node;
13491 break;
0a3b29ad 13492 case RID_WCHAR:
b7d1e8ea 13493 type = wchar_type_node;
13494 break;
0a3b29ad 13495 case RID_BOOL:
b7d1e8ea 13496 type = boolean_type_node;
13497 break;
0a3b29ad 13498 case RID_SHORT:
a60f3e81 13499 set_and_check_decl_spec_loc (decl_specs, ds_short, token->location);
b7d1e8ea 13500 type = short_integer_type_node;
13501 break;
0a3b29ad 13502 case RID_INT:
4b9b2871 13503 if (decl_specs)
13504 decl_specs->explicit_int_p = true;
b7d1e8ea 13505 type = integer_type_node;
13506 break;
6388cfe2 13507 case RID_INT128:
13508 if (!int128_integer_type_node)
13509 break;
13510 if (decl_specs)
13511 decl_specs->explicit_int128_p = true;
13512 type = int128_integer_type_node;
13513 break;
0a3b29ad 13514 case RID_LONG:
4b9b2871 13515 if (decl_specs)
a60f3e81 13516 set_and_check_decl_spec_loc (decl_specs, ds_long, token->location);
b7d1e8ea 13517 type = long_integer_type_node;
13518 break;
0a3b29ad 13519 case RID_SIGNED:
a60f3e81 13520 set_and_check_decl_spec_loc (decl_specs, ds_signed, token->location);
b7d1e8ea 13521 type = integer_type_node;
13522 break;
0a3b29ad 13523 case RID_UNSIGNED:
a60f3e81 13524 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token->location);
b7d1e8ea 13525 type = unsigned_type_node;
13526 break;
0a3b29ad 13527 case RID_FLOAT:
b7d1e8ea 13528 type = float_type_node;
13529 break;
0a3b29ad 13530 case RID_DOUBLE:
b7d1e8ea 13531 type = double_type_node;
13532 break;
0a3b29ad 13533 case RID_VOID:
b7d1e8ea 13534 type = void_type_node;
13535 break;
45b44d0a 13536
13537 case RID_AUTO:
bf8d19fe 13538 maybe_warn_cpp0x (CPP0X_AUTO);
46f4817e 13539 type = make_auto ();
45b44d0a 13540 break;
0a3b29ad 13541
34da8800 13542 case RID_DECLTYPE:
07b8f133 13543 /* Since DR 743, decltype can either be a simple-type-specifier by
13544 itself or begin a nested-name-specifier. Parsing it will replace
13545 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13546 handling below decide what to do. */
13547 cp_parser_decltype (parser);
13548 cp_lexer_set_token_position (parser->lexer, token);
13549 break;
34da8800 13550
0a3b29ad 13551 case RID_TYPEOF:
4b9b2871 13552 /* Consume the `typeof' token. */
13553 cp_lexer_consume_token (parser->lexer);
13554 /* Parse the operand to `typeof'. */
13555 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13556 /* If it is not already a TYPE, take its type. */
13557 if (!TYPE_P (type))
13558 type = finish_typeof (type);
13559
13560 if (decl_specs)
13561 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 13562 token->location,
1bc28cb0 13563 /*type_definition_p=*/false);
207355ad 13564
4b9b2871 13565 return type;
0a3b29ad 13566
8de5c43e 13567 case RID_UNDERLYING_TYPE:
13568 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
8de5c43e 13569 if (decl_specs)
13570 cp_parser_set_decl_spec_type (decl_specs, type,
13571 token->location,
1bc28cb0 13572 /*type_definition_p=*/false);
8de5c43e 13573
13574 return type;
13575
e6014a82 13576 case RID_BASES:
13577 case RID_DIRECT_BASES:
13578 type = cp_parser_trait_expr (parser, token->keyword);
13579 if (decl_specs)
13580 cp_parser_set_decl_spec_type (decl_specs, type,
13581 token->location,
13582 /*type_definition_p=*/false);
13583 return type;
0a3b29ad 13584 default:
13585 break;
13586 }
13587
07b8f133 13588 /* If token is an already-parsed decltype not followed by ::,
13589 it's a simple-type-specifier. */
13590 if (token->type == CPP_DECLTYPE
13591 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13592 {
13593 type = token->u.value;
13594 if (decl_specs)
13595 cp_parser_set_decl_spec_type (decl_specs, type,
13596 token->location,
1bc28cb0 13597 /*type_definition_p=*/false);
07b8f133 13598 cp_lexer_consume_token (parser->lexer);
13599 return type;
13600 }
13601
b7d1e8ea 13602 /* If the type-specifier was for a built-in type, we're done. */
13603 if (type)
13604 {
4b9b2871 13605 /* Record the type. */
13606 if (decl_specs
13607 && (token->keyword != RID_SIGNED
13608 && token->keyword != RID_UNSIGNED
13609 && token->keyword != RID_SHORT
13610 && token->keyword != RID_LONG))
207355ad 13611 cp_parser_set_decl_spec_type (decl_specs,
4b9b2871 13612 type,
eef0ab03 13613 token->location,
1bc28cb0 13614 /*type_definition_p=*/false);
4b9b2871 13615 if (decl_specs)
13616 decl_specs->any_specifiers_p = true;
13617
b7d1e8ea 13618 /* Consume the token. */
8e9e8d76 13619 cp_lexer_consume_token (parser->lexer);
182d094e 13620
13621 /* There is no valid C++ program where a non-template type is
13622 followed by a "<". That usually indicates that the user thought
13623 that the type was a template. */
eef0ab03 13624 cp_parser_check_for_invalid_template_id (parser, type, token->location);
182d094e 13625
4b9b2871 13626 return TYPE_NAME (type);
b7d1e8ea 13627 }
13628
0a3b29ad 13629 /* The type-specifier must be a user-defined type. */
ccb84981 13630 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
0a3b29ad 13631 {
a116ab20 13632 bool qualified_p;
b446079d 13633 bool global_p;
a116ab20 13634
0a3b29ad 13635 /* Don't gobble tokens or issue error messages if this is an
13636 optional type-specifier. */
13637 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13638 cp_parser_parse_tentatively (parser);
13639
13640 /* Look for the optional `::' operator. */
b446079d 13641 global_p
a619017d 13642 = (cp_parser_global_scope_opt (parser,
130bb1d4 13643 /*current_scope_valid_p=*/false)
a619017d 13644 != NULL_TREE);
0a3b29ad 13645 /* Look for the nested-name specifier. */
a116ab20 13646 qualified_p
13647 = (cp_parser_nested_name_specifier_opt (parser,
13648 /*typename_keyword_p=*/false,
13649 /*check_dependency_p=*/true,
13650 /*type_p=*/false,
382fbf3d 13651 /*is_declaration=*/false)
13652 != NULL_TREE);
eef0ab03 13653 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13654 /* If we have seen a nested-name-specifier, and the next token
13655 is `template', then we are using the template-id production. */
ccb84981 13656 if (parser->scope
0a3b29ad 13657 && cp_parser_optional_template_keyword (parser))
13658 {
13659 /* Look for the template-id. */
ccb84981 13660 type = cp_parser_template_id (parser,
0a3b29ad 13661 /*template_keyword_p=*/true,
3d0f901b 13662 /*check_dependency_p=*/true,
13663 /*is_declaration=*/false);
0a3b29ad 13664 /* If the template-id did not name a type, we are out of
13665 luck. */
13666 if (TREE_CODE (type) != TYPE_DECL)
13667 {
13668 cp_parser_error (parser, "expected template-id for type");
13669 type = NULL_TREE;
13670 }
13671 }
13672 /* Otherwise, look for a type-name. */
13673 else
92b128ed 13674 type = cp_parser_type_name (parser);
a116ab20 13675 /* Keep track of all name-lookups performed in class scopes. */
207355ad 13676 if (type
b446079d 13677 && !global_p
a116ab20 13678 && !qualified_p
207355ad 13679 && TREE_CODE (type) == TYPE_DECL
a116ab20 13680 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13681 maybe_note_name_used_in_class (DECL_NAME (type), type);
0a3b29ad 13682 /* If it didn't work out, we don't have a TYPE. */
ccb84981 13683 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
0a3b29ad 13684 && !cp_parser_parse_definitely (parser))
13685 type = NULL_TREE;
4b9b2871 13686 if (type && decl_specs)
13687 cp_parser_set_decl_spec_type (decl_specs, type,
eef0ab03 13688 token->location,
1bc28cb0 13689 /*type_definition_p=*/false);
0a3b29ad 13690 }
13691
13692 /* If we didn't get a type-name, issue an error message. */
13693 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13694 {
13695 cp_parser_error (parser, "expected type-name");
13696 return error_mark_node;
13697 }
13698
92b128ed 13699 if (type && type != error_mark_node)
7a4e126b 13700 {
79408174 13701 /* See if TYPE is an Objective-C type, and if so, parse and
13702 accept any protocol references following it. Do this before
13703 the cp_parser_check_for_invalid_template_id() call, because
13704 Objective-C types can be followed by '<...>' which would
13705 enclose protocol names rather than template arguments, and so
13706 everything is fine. */
13707 if (c_dialect_objc () && !parser->scope
13708 && (objc_is_id (type) || objc_is_class_name (type)))
13709 {
13710 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13711 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13712
13713 /* Clobber the "unqualified" type previously entered into
13714 DECL_SPECS with the new, improved protocol-qualified version. */
13715 if (decl_specs)
13716 decl_specs->type = qual_type;
13717
13718 return qual_type;
13719 }
7a4e126b 13720
b0d0931f 13721 /* There is no valid C++ program where a non-template type is
13722 followed by a "<". That usually indicates that the user
13723 thought that the type was a template. */
eef0ab03 13724 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13725 token->location);
9031d10b 13726 }
8534c3a3 13727
0a3b29ad 13728 return type;
13729}
13730
13731/* Parse a type-name.
13732
13733 type-name:
13734 class-name
13735 enum-name
ccb84981 13736 typedef-name
370478b1 13737 simple-template-id [in c++0x]
0a3b29ad 13738
13739 enum-name:
13740 identifier
13741
13742 typedef-name:
ccb84981 13743 identifier
0a3b29ad 13744
dfea972c 13745 Returns a TYPE_DECL for the type. */
0a3b29ad 13746
13747static tree
45baea8b 13748cp_parser_type_name (cp_parser* parser)
0a3b29ad 13749{
13750 tree type_decl;
0a3b29ad 13751
13752 /* We can't know yet whether it is a class-name or not. */
13753 cp_parser_parse_tentatively (parser);
13754 /* Try a class-name. */
ccb84981 13755 type_decl = cp_parser_class_name (parser,
0a3b29ad 13756 /*typename_keyword_p=*/false,
13757 /*template_keyword_p=*/false,
e2ae55f2 13758 none_type,
0a3b29ad 13759 /*check_dependency_p=*/true,
3d0f901b 13760 /*class_head_p=*/false,
13761 /*is_declaration=*/false);
0a3b29ad 13762 /* If it's not a class-name, keep looking. */
13763 if (!cp_parser_parse_definitely (parser))
13764 {
370478b1 13765 if (cxx_dialect < cxx0x)
13766 /* It must be a typedef-name or an enum-name. */
13767 return cp_parser_nonclass_name (parser);
13768
13769 cp_parser_parse_tentatively (parser);
13770 /* It is either a simple-template-id representing an
13771 instantiation of an alias template... */
13772 type_decl = cp_parser_template_id (parser,
13773 /*template_keyword_p=*/false,
13774 /*check_dependency_p=*/false,
13775 /*is_declaration=*/false);
13776 /* Note that this must be an instantiation of an alias template
13777 because [temp.names]/6 says:
13778
13779 A template-id that names an alias template specialization
13780 is a type-name.
13781
13782 Whereas [temp.names]/7 says:
13783
13784 A simple-template-id that names a class template
13785 specialization is a class-name. */
13786 if (type_decl != NULL_TREE
13787 && TREE_CODE (type_decl) == TYPE_DECL
13788 && TYPE_DECL_ALIAS_P (type_decl))
13789 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13790 else
13791 cp_parser_simulate_error (parser);
13792
13793 if (!cp_parser_parse_definitely (parser))
13794 /* ... Or a typedef-name or an enum-name. */
13795 return cp_parser_nonclass_name (parser);
674e90bd 13796 }
ccb84981 13797
674e90bd 13798 return type_decl;
13799}
7a4e126b 13800
674e90bd 13801/* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
7a4e126b 13802
674e90bd 13803 enum-name:
13804 identifier
13805
13806 typedef-name:
13807 identifier
13808
13809 Returns a TYPE_DECL for the type. */
ccb84981 13810
674e90bd 13811static tree
13812cp_parser_nonclass_name (cp_parser* parser)
13813{
13814 tree type_decl;
13815 tree identifier;
13816
ad9ae192 13817 cp_token *token = cp_lexer_peek_token (parser->lexer);
674e90bd 13818 identifier = cp_parser_identifier (parser);
13819 if (identifier == error_mark_node)
13820 return error_mark_node;
13821
13822 /* Look up the type-name. */
ad9ae192 13823 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
674e90bd 13824
3d79ce2d 13825 if (TREE_CODE (type_decl) == USING_DECL)
13826 {
13827 if (!DECL_DEPENDENT_P (type_decl))
13828 type_decl = strip_using_decl (type_decl);
13829 else if (USING_DECL_TYPENAME_P (type_decl))
13830 {
13831 /* We have found a type introduced by a using
13832 declaration at class scope that refers to a dependent
13833 type.
13834
13835 using typename :: [opt] nested-name-specifier unqualified-id ;
13836 */
13837 type_decl = make_typename_type (TREE_TYPE (type_decl),
13838 DECL_NAME (type_decl),
13839 typename_type, tf_error);
13840 if (type_decl != error_mark_node)
13841 type_decl = TYPE_NAME (type_decl);
13842 }
13843 }
13844
79408174 13845 if (TREE_CODE (type_decl) != TYPE_DECL
13846 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13847 {
13848 /* See if this is an Objective-C type. */
13849 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13850 tree type = objc_get_protocol_qualified_type (identifier, protos);
13851 if (type)
13852 type_decl = TYPE_NAME (type);
13853 }
b0d0931f 13854
674e90bd 13855 /* Issue an error if we did not find a type-name. */
b0d0931f 13856 if (TREE_CODE (type_decl) != TYPE_DECL
79408174 13857 /* In Objective-C, we have the complication that class names are
13858 normally type names and start declarations (eg, the
13859 "NSObject" in "NSObject *object;"), but can be used in an
13860 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13861 is an expression. So, a classname followed by a dot is not a
13862 valid type-name. */
13863 || (objc_is_class_name (TREE_TYPE (type_decl))
13864 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
674e90bd 13865 {
13866 if (!cp_parser_simulate_error (parser))
13867 cp_parser_name_lookup_error (parser, identifier, type_decl,
c247dce0 13868 NLE_TYPE, token->location);
674e90bd 13869 return error_mark_node;
13870 }
13871 /* Remember that the name was used in the definition of the
13872 current class so that we can check later to see if the
13873 meaning would have been different after the class was
13874 entirely defined. */
13875 else if (type_decl != error_mark_node
13876 && !parser->scope)
13877 maybe_note_name_used_in_class (identifier, type_decl);
13878
0a3b29ad 13879 return type_decl;
13880}
13881
0a3b29ad 13882/* Parse an elaborated-type-specifier. Note that the grammar given
13883 here incorporates the resolution to DR68.
13884
13885 elaborated-type-specifier:
13886 class-key :: [opt] nested-name-specifier [opt] identifier
13887 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
3f00a6c0 13888 enum-key :: [opt] nested-name-specifier [opt] identifier
0a3b29ad 13889 typename :: [opt] nested-name-specifier identifier
ccb84981 13890 typename :: [opt] nested-name-specifier template [opt]
13891 template-id
0a3b29ad 13892
c010fc5a 13893 GNU extension:
13894
13895 elaborated-type-specifier:
13896 class-key attributes :: [opt] nested-name-specifier [opt] identifier
ccb84981 13897 class-key attributes :: [opt] nested-name-specifier [opt]
653e5405 13898 template [opt] template-id
c010fc5a 13899 enum attributes :: [opt] nested-name-specifier [opt] identifier
13900
0a3b29ad 13901 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13902 declared `friend'. If IS_DECLARATION is TRUE, then this
13903 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13904 something is being declared.
13905
13906 Returns the TYPE specified. */
13907
13908static tree
ccb84981 13909cp_parser_elaborated_type_specifier (cp_parser* parser,
653e5405 13910 bool is_friend,
13911 bool is_declaration)
0a3b29ad 13912{
13913 enum tag_types tag_type;
13914 tree identifier;
13915 tree type = NULL_TREE;
c010fc5a 13916 tree attributes = NULL_TREE;
dd42b7cf 13917 tree globalscope;
ad9ae192 13918 cp_token *token = NULL;
0a3b29ad 13919
13920 /* See if we're looking at the `enum' keyword. */
13921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13922 {
13923 /* Consume the `enum' token. */
13924 cp_lexer_consume_token (parser->lexer);
13925 /* Remember that it's an enumeration type. */
13926 tag_type = enum_type;
aa290616 13927 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13928 enums) is used here. */
3f00a6c0 13929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
aa290616 13930 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13931 {
13932 pedwarn (input_location, 0, "elaborated-type-specifier "
13933 "for a scoped enum must not use the %<%D%> keyword",
13934 cp_lexer_peek_token (parser->lexer)->u.value);
13935 /* Consume the `struct' or `class' and parse it anyway. */
13936 cp_lexer_consume_token (parser->lexer);
13937 }
c010fc5a 13938 /* Parse the attributes. */
13939 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 13940 }
13941 /* Or, it might be `typename'. */
13942 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13943 RID_TYPENAME))
13944 {
13945 /* Consume the `typename' token. */
13946 cp_lexer_consume_token (parser->lexer);
13947 /* Remember that it's a `typename' type. */
13948 tag_type = typename_type;
0a3b29ad 13949 }
13950 /* Otherwise it must be a class-key. */
13951 else
13952 {
13953 tag_type = cp_parser_class_key (parser);
13954 if (tag_type == none_type)
13955 return error_mark_node;
c010fc5a 13956 /* Parse the attributes. */
13957 attributes = cp_parser_attributes_opt (parser);
0a3b29ad 13958 }
13959
13960 /* Look for the `::' operator. */
dd42b7cf 13961 globalscope = cp_parser_global_scope_opt (parser,
13962 /*current_scope_valid_p=*/false);
0a3b29ad 13963 /* Look for the nested-name-specifier. */
dd42b7cf 13964 if (tag_type == typename_type && !globalscope)
e6ec2049 13965 {
c75ae97e 13966 if (!cp_parser_nested_name_specifier (parser,
e6ec2049 13967 /*typename_keyword_p=*/true,
13968 /*check_dependency_p=*/true,
3d0f901b 13969 /*type_p=*/true,
c75ae97e 13970 is_declaration))
e6ec2049 13971 return error_mark_node;
13972 }
0a3b29ad 13973 else
13974 /* Even though `typename' is not present, the proposed resolution
13975 to Core Issue 180 says that in `class A<T>::B', `B' should be
13976 considered a type-name, even if `A<T>' is dependent. */
13977 cp_parser_nested_name_specifier_opt (parser,
13978 /*typename_keyword_p=*/true,
13979 /*check_dependency_p=*/true,
3d0f901b 13980 /*type_p=*/true,
13981 is_declaration);
6fa283db 13982 /* For everything but enumeration types, consider a template-id.
13983 For an enumeration type, consider only a plain identifier. */
0a3b29ad 13984 if (tag_type != enum_type)
13985 {
13986 bool template_p = false;
13987 tree decl;
13988
13989 /* Allow the `template' keyword. */
13990 template_p = cp_parser_optional_template_keyword (parser);
13991 /* If we didn't see `template', we don't know if there's a
653e5405 13992 template-id or not. */
0a3b29ad 13993 if (!template_p)
13994 cp_parser_parse_tentatively (parser);
13995 /* Parse the template-id. */
ad9ae192 13996 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 13997 decl = cp_parser_template_id (parser, template_p,
3d0f901b 13998 /*check_dependency_p=*/true,
13999 is_declaration);
0a3b29ad 14000 /* If we didn't find a template-id, look for an ordinary
653e5405 14001 identifier. */
0a3b29ad 14002 if (!template_p && !cp_parser_parse_definitely (parser))
14003 ;
14004 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14005 in effect, then we must assume that, upon instantiation, the
14006 template will correspond to a class. */
14007 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14008 && tag_type == typename_type)
14009 type = make_typename_type (parser->scope, decl,
e2ae55f2 14010 typename_type,
c31fdaf6 14011 /*complain=*/tf_error);
cb288599 14012 /* If the `typename' keyword is in effect and DECL is not a type
14013 decl. Then type is non existant. */
14014 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14015 type = NULL_TREE;
14016 else
a56bd057 14017 type = check_elaborated_type_specifier (tag_type, decl,
14018 /*allow_template_p=*/true);
0a3b29ad 14019 }
14020
0a3b29ad 14021 if (!type)
14022 {
ad9ae192 14023 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 14024 identifier = cp_parser_identifier (parser);
14025
14026 if (identifier == error_mark_node)
bbd3de90 14027 {
14028 parser->scope = NULL_TREE;
14029 return error_mark_node;
14030 }
0a3b29ad 14031
14032 /* For a `typename', we needn't call xref_tag. */
9031d10b 14033 if (tag_type == typename_type
d9da0685 14034 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
ccb84981 14035 return cp_parser_make_typename_type (parser, parser->scope,
ad9ae192 14036 identifier,
14037 token->location);
0a3b29ad 14038 /* Look up a qualified name in the usual way. */
14039 if (parser->scope)
14040 {
14041 tree decl;
8a58dc06 14042 tree ambiguous_decls;
0a3b29ad 14043
ccb84981 14044 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 14045 tag_type,
c3b9e457 14046 /*is_template=*/false,
6fc758aa 14047 /*is_namespace=*/false,
2cdbcd51 14048 /*check_dependency=*/true,
ad9ae192 14049 &ambiguous_decls,
14050 token->location);
8a58dc06 14051
14052 /* If the lookup was ambiguous, an error will already have been
14053 issued. */
14054 if (ambiguous_decls)
14055 return error_mark_node;
a5e50b31 14056
14057 /* If we are parsing friend declaration, DECL may be a
14058 TEMPLATE_DECL tree node here. However, we need to check
14059 whether this TEMPLATE_DECL results in valid code. Consider
14060 the following example:
14061
14062 namespace N {
14063 template <class T> class C {};
14064 }
14065 class X {
14066 template <class T> friend class N::C; // #1, valid code
14067 };
14068 template <class T> class Y {
14069 friend class N::C; // #2, invalid code
14070 };
14071
14072 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14073 name lookup of `N::C'. We see that friend declaration must
14074 be template for the code to be valid. Note that
14075 processing_template_decl does not work here since it is
14076 always 1 for the above two cases. */
14077
ccb84981 14078 decl = (cp_parser_maybe_treat_template_as_class
a5e50b31 14079 (decl, /*tag_name_p=*/is_friend
14080 && parser->num_template_parameter_lists));
0a3b29ad 14081
14082 if (TREE_CODE (decl) != TYPE_DECL)
14083 {
9031d10b 14084 cp_parser_diagnose_invalid_type_name (parser,
d9da0685 14085 parser->scope,
ad9ae192 14086 identifier,
14087 token->location);
0a3b29ad 14088 return error_mark_node;
14089 }
8172be22 14090
14091 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
539401d2 14092 {
14093 bool allow_template = (parser->num_template_parameter_lists
14094 || DECL_SELF_REFERENCE_P (decl));
14095 type = check_elaborated_type_specifier (tag_type, decl,
14096 allow_template);
14097
14098 if (type == error_mark_node)
14099 return error_mark_node;
14100 }
0a3b29ad 14101
f1f71333 14102 /* Forward declarations of nested types, such as
14103
14104 class C1::C2;
14105 class C1::C2::C3;
14106
14107 are invalid unless all components preceding the final '::'
14108 are complete. If all enclosing types are complete, these
14109 declarations become merely pointless.
14110
14111 Invalid forward declarations of nested types are errors
14112 caught elsewhere in parsing. Those that are pointless arrive
14113 here. */
14114
8d4f5f9e 14115 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
f1f71333 14116 && !is_friend && !processing_explicit_instantiation)
14117 warning (0, "declaration %qD does not declare anything", decl);
14118
0a3b29ad 14119 type = TREE_TYPE (decl);
14120 }
ccb84981 14121 else
0a3b29ad 14122 {
14123 /* An elaborated-type-specifier sometimes introduces a new type and
14124 sometimes names an existing type. Normally, the rule is that it
14125 introduces a new type only if there is not an existing type of
14126 the same name already in scope. For example, given:
14127
14128 struct S {};
14129 void f() { struct S s; }
14130
14131 the `struct S' in the body of `f' is the same `struct S' as in
14132 the global scope; the existing definition is used. However, if
ccb84981 14133 there were no global declaration, this would introduce a new
0a3b29ad 14134 local class named `S'.
14135
14136 An exception to this rule applies to the following code:
14137
14138 namespace N { struct S; }
14139
14140 Here, the elaborated-type-specifier names a new type
14141 unconditionally; even if there is already an `S' in the
14142 containing scope this declaration names a new type.
14143 This exception only applies if the elaborated-type-specifier
14144 forms the complete declaration:
14145
ccb84981 14146 [class.name]
0a3b29ad 14147
14148 A declaration consisting solely of `class-key identifier ;' is
14149 either a redeclaration of the name in the current scope or a
14150 forward declaration of the identifier as a class name. It
14151 introduces the name into the current scope.
14152
14153 We are in this situation precisely when the next token is a `;'.
14154
14155 An exception to the exception is that a `friend' declaration does
14156 *not* name a new type; i.e., given:
14157
14158 struct S { friend struct T; };
14159
ccb84981 14160 `T' is not a new type in the scope of `S'.
0a3b29ad 14161
14162 Also, `new struct S' or `sizeof (struct S)' never results in the
14163 definition of a new type; a new type can only be declared in a
6beb3f76 14164 declaration context. */
0a3b29ad 14165
1fadf2c8 14166 tag_scope ts;
4f311379 14167 bool template_p;
14168
1fadf2c8 14169 if (is_friend)
14170 /* Friends have special name lookup rules. */
14171 ts = ts_within_enclosing_non_class;
14172 else if (is_declaration
14173 && cp_lexer_next_token_is (parser->lexer,
14174 CPP_SEMICOLON))
14175 /* This is a `class-key identifier ;' */
14176 ts = ts_current;
14177 else
14178 ts = ts_global;
14179
074ab442 14180 template_p =
4f311379 14181 (parser->num_template_parameter_lists
14182 && (cp_parser_next_token_starts_class_definition_p (parser)
14183 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
7be1bc1f 14184 /* An unqualified name was used to reference this type, so
14185 there were no qualifying templates. */
074ab442 14186 if (!cp_parser_check_template_parameters (parser,
ad9ae192 14187 /*num_templates=*/0,
7b07a15e 14188 token->location,
14189 /*declarator=*/NULL))
7be1bc1f 14190 return error_mark_node;
4f311379 14191 type = xref_tag (tag_type, identifier, ts, template_p);
0a3b29ad 14192 }
14193 }
4a2849cb 14194
7b83d1d8 14195 if (type == error_mark_node)
14196 return error_mark_node;
14197
4a2849cb 14198 /* Allow attributes on forward declarations of classes. */
14199 if (attributes)
14200 {
b415ff10 14201 if (TREE_CODE (type) == TYPENAME_TYPE)
14202 warning (OPT_Wattributes,
14203 "attributes ignored on uninstantiated type");
14204 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14205 && ! processing_explicit_instantiation)
4a2849cb 14206 warning (OPT_Wattributes,
14207 "attributes ignored on template instantiation");
14208 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14209 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14210 else
14211 warning (OPT_Wattributes,
14212 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14213 }
14214
0a3b29ad 14215 if (tag_type != enum_type)
70366e83 14216 {
14217 /* Indicate whether this class was declared as a `class' or as a
14218 `struct'. */
14219 if (TREE_CODE (type) == RECORD_TYPE)
14220 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14221 cp_parser_check_class_key (tag_type, type);
14222 }
1157e9f0 14223
14224 /* A "<" cannot follow an elaborated type specifier. If that
14225 happens, the user was probably trying to form a template-id. */
eef0ab03 14226 cp_parser_check_for_invalid_template_id (parser, type, token->location);
1157e9f0 14227
0a3b29ad 14228 return type;
14229}
14230
14231/* Parse an enum-specifier.
14232
14233 enum-specifier:
aa290616 14234 enum-head { enumerator-list [opt] }
ee239d91 14235 enum-head { enumerator-list , } [C++0x]
aa290616 14236
14237 enum-head:
14238 enum-key identifier [opt] enum-base [opt]
14239 enum-key nested-name-specifier identifier enum-base [opt]
3f00a6c0 14240
14241 enum-key:
14242 enum
14243 enum class [C++0x]
14244 enum struct [C++0x]
14245
14246 enum-base: [C++0x]
14247 : type-specifier-seq
0a3b29ad 14248
aa290616 14249 opaque-enum-specifier:
14250 enum-key identifier enum-base [opt] ;
14251
fe80e237 14252 GNU Extensions:
3f00a6c0 14253 enum-key attributes[opt] identifier [opt] enum-base [opt]
14254 { enumerator-list [opt] }attributes[opt]
ee239d91 14255 enum-key attributes[opt] identifier [opt] enum-base [opt]
14256 { enumerator-list, }attributes[opt] [C++0x]
fe80e237 14257
4a2849cb 14258 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14259 if the token stream isn't an enum-specifier after all. */
0a3b29ad 14260
14261static tree
45baea8b 14262cp_parser_enum_specifier (cp_parser* parser)
0a3b29ad 14263{
637fdc50 14264 tree identifier;
aa290616 14265 tree type = NULL_TREE;
14266 tree prev_scope;
14267 tree nested_name_specifier = NULL_TREE;
4a2849cb 14268 tree attributes;
3f00a6c0 14269 bool scoped_enum_p = false;
b706ce78 14270 bool has_underlying_type = false;
aa290616 14271 bool nested_being_defined = false;
14272 bool new_value_list = false;
14273 bool is_new_type = false;
14274 bool is_anonymous = false;
3f00a6c0 14275 tree underlying_type = NULL_TREE;
aa290616 14276 cp_token *type_start_token = NULL;
1e838f42 14277 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14278
14279 parser->colon_corrects_to_scope_p = false;
4a2849cb 14280
14281 /* Parse tentatively so that we can back up if we don't find a
14282 enum-specifier. */
14283 cp_parser_parse_tentatively (parser);
0a3b29ad 14284
637fdc50 14285 /* Caller guarantees that the current token is 'enum', an identifier
14286 possibly follows, and the token after that is an opening brace.
14287 If we don't have an identifier, fabricate an anonymous name for
14288 the enumeration being defined. */
14289 cp_lexer_consume_token (parser->lexer);
0a3b29ad 14290
3f00a6c0 14291 /* Parse the "class" or "struct", which indicates a scoped
14292 enumeration type in C++0x. */
14293 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14294 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14295 {
aa290616 14296 if (cxx_dialect < cxx0x)
bf8d19fe 14297 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
3f00a6c0 14298
14299 /* Consume the `struct' or `class' token. */
14300 cp_lexer_consume_token (parser->lexer);
14301
14302 scoped_enum_p = true;
14303 }
b706ce78 14304
4a2849cb 14305 attributes = cp_parser_attributes_opt (parser);
14306
aa290616 14307 /* Clear the qualification. */
14308 parser->scope = NULL_TREE;
14309 parser->qualifying_scope = NULL_TREE;
14310 parser->object_scope = NULL_TREE;
14311
14312 /* Figure out in what scope the declaration is being placed. */
14313 prev_scope = current_scope ();
14314
14315 type_start_token = cp_lexer_peek_token (parser->lexer);
14316
14317 push_deferring_access_checks (dk_no_check);
14318 nested_name_specifier
14319 = cp_parser_nested_name_specifier_opt (parser,
14320 /*typename_keyword_p=*/true,
14321 /*check_dependency_p=*/false,
14322 /*type_p=*/false,
14323 /*is_declaration=*/false);
14324
14325 if (nested_name_specifier)
14326 {
14327 tree name;
14328
14329 identifier = cp_parser_identifier (parser);
14330 name = cp_parser_lookup_name (parser, identifier,
14331 enum_type,
14332 /*is_template=*/false,
14333 /*is_namespace=*/false,
14334 /*check_dependency=*/true,
14335 /*ambiguous_decls=*/NULL,
14336 input_location);
14337 if (name)
14338 {
14339 type = TREE_TYPE (name);
14340 if (TREE_CODE (type) == TYPENAME_TYPE)
14341 {
14342 /* Are template enums allowed in ISO? */
14343 if (template_parm_scope_p ())
29438999 14344 pedwarn (type_start_token->location, OPT_Wpedantic,
aa290616 14345 "%qD is an enumeration template", name);
14346 /* ignore a typename reference, for it will be solved by name
14347 in start_enum. */
14348 type = NULL_TREE;
14349 }
14350 }
14351 else
14352 error_at (type_start_token->location,
14353 "%qD is not an enumerator-name", identifier);
14354 }
637fdc50 14355 else
aa290616 14356 {
14357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14358 identifier = cp_parser_identifier (parser);
14359 else
14360 {
14361 identifier = make_anon_name ();
14362 is_anonymous = true;
14363 }
14364 }
14365 pop_deferring_access_checks ();
0a3b29ad 14366
e72cb433 14367 /* Check for the `:' that denotes a specified underlying type in C++0x.
14368 Note that a ':' could also indicate a bitfield width, however. */
3f00a6c0 14369 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14370 {
14371 cp_decl_specifier_seq type_specifiers;
14372
e72cb433 14373 /* Consume the `:'. */
14374 cp_lexer_consume_token (parser->lexer);
14375
14376 /* Parse the type-specifier-seq. */
c44f7faf 14377 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 14378 /*is_trailing_return=*/false,
e72cb433 14379 &type_specifiers);
14380
b706ce78 14381 /* At this point this is surely not elaborated type specifier. */
14382 if (!cp_parser_parse_definitely (parser))
14383 return NULL_TREE;
14384
aa290616 14385 if (cxx_dialect < cxx0x)
bf8d19fe 14386 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
3f00a6c0 14387
b706ce78 14388 has_underlying_type = true;
14389
3f00a6c0 14390 /* If that didn't work, stop. */
14391 if (type_specifiers.type != error_mark_node)
14392 {
14393 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14394 /*initialized=*/0, NULL);
14395 if (underlying_type == error_mark_node)
14396 underlying_type = NULL_TREE;
14397 }
3f00a6c0 14398 }
14399
4a2849cb 14400 /* Look for the `{' but don't consume it yet. */
14401 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
b706ce78 14402 {
aa290616 14403 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14404 {
14405 cp_parser_error (parser, "expected %<{%>");
14406 if (has_underlying_type)
1e838f42 14407 {
14408 type = NULL_TREE;
14409 goto out;
14410 }
aa290616 14411 }
14412 /* An opaque-enum-specifier must have a ';' here. */
14413 if ((scoped_enum_p || underlying_type)
14414 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14415 {
14416 cp_parser_error (parser, "expected %<;%> or %<{%>");
14417 if (has_underlying_type)
1e838f42 14418 {
14419 type = NULL_TREE;
14420 goto out;
14421 }
aa290616 14422 }
b706ce78 14423 }
4a2849cb 14424
b706ce78 14425 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
4a2849cb 14426 return NULL_TREE;
14427
aa290616 14428 if (nested_name_specifier)
14429 {
14430 if (CLASS_TYPE_P (nested_name_specifier))
14431 {
14432 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14433 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14434 push_scope (nested_name_specifier);
14435 }
14436 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14437 {
14438 push_nested_namespace (nested_name_specifier);
14439 }
14440 }
14441
0a3b29ad 14442 /* Issue an error message if type-definitions are forbidden here. */
9a7c4b43 14443 if (!cp_parser_check_type_definition (parser))
14444 type = error_mark_node;
14445 else
14446 /* Create the new type. We do this before consuming the opening
14447 brace so the enum will be recorded as being on the line of its
14448 tag (or the 'enum' keyword, if there is no tag). */
aa290616 14449 type = start_enum (identifier, type, underlying_type,
14450 scoped_enum_p, &is_new_type);
b9dd3954 14451
aa290616 14452 /* If the next token is not '{' it is an opaque-enum-specifier or an
14453 elaborated-type-specifier. */
14454 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4a2849cb 14455 {
6198e8f6 14456 timevar_push (TV_PARSE_ENUM);
aa290616 14457 if (nested_name_specifier)
14458 {
14459 /* The following catches invalid code such as:
14460 enum class S<int>::E { A, B, C }; */
14461 if (!processing_specialization
14462 && CLASS_TYPE_P (nested_name_specifier)
14463 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14464 error_at (type_start_token->location, "cannot add an enumerator "
14465 "list to a template instantiation");
14466
14467 /* If that scope does not contain the scope in which the
14468 class was originally declared, the program is invalid. */
14469 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14470 {
14471 if (at_namespace_scope_p ())
14472 error_at (type_start_token->location,
14473 "declaration of %qD in namespace %qD which does not "
14474 "enclose %qD",
14475 type, prev_scope, nested_name_specifier);
14476 else
14477 error_at (type_start_token->location,
14478 "declaration of %qD in %qD which does not enclose %qD",
14479 type, prev_scope, nested_name_specifier);
14480 type = error_mark_node;
14481 }
14482 }
4a2849cb 14483
aa290616 14484 if (scoped_enum_p)
14485 begin_scope (sk_scoped_enum, type);
637fdc50 14486
aa290616 14487 /* Consume the opening brace. */
14488 cp_lexer_consume_token (parser->lexer);
14489
14490 if (type == error_mark_node)
14491 ; /* Nothing to add */
14492 else if (OPAQUE_ENUM_P (type)
14493 || (cxx_dialect > cxx98 && processing_specialization))
14494 {
14495 new_value_list = true;
14496 SET_OPAQUE_ENUM_P (type, false);
14497 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14498 }
14499 else
14500 {
14501 error_at (type_start_token->location, "multiple definition of %q#T", type);
14502 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14503 "previous definition here");
14504 type = error_mark_node;
14505 }
14506
14507 if (type == error_mark_node)
14508 cp_parser_skip_to_end_of_block_or_statement (parser);
14509 /* If the next token is not '}', then there are some enumerators. */
14510 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14511 cp_parser_enumerator_list (parser, type);
14512
14513 /* Consume the final '}'. */
14514 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14515
14516 if (scoped_enum_p)
14517 finish_scope ();
6198e8f6 14518 timevar_pop (TV_PARSE_ENUM);
aa290616 14519 }
14520 else
14521 {
14522 /* If a ';' follows, then it is an opaque-enum-specifier
14523 and additional restrictions apply. */
14524 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14525 {
14526 if (is_anonymous)
14527 error_at (type_start_token->location,
14528 "opaque-enum-specifier without name");
14529 else if (nested_name_specifier)
14530 error_at (type_start_token->location,
14531 "opaque-enum-specifier must use a simple identifier");
14532 }
14533 }
0a3b29ad 14534
fe80e237 14535 /* Look for trailing attributes to apply to this enumeration, and
93523877 14536 apply them if appropriate. */
fe80e237 14537 if (cp_parser_allow_gnu_extensions_p (parser))
14538 {
14539 tree trailing_attr = cp_parser_attributes_opt (parser);
07dd1387 14540 trailing_attr = chainon (trailing_attr, attributes);
fe80e237 14541 cplus_decl_attributes (&type,
14542 trailing_attr,
14543 (int) ATTR_FLAG_TYPE_IN_PLACE);
14544 }
14545
0a3b29ad 14546 /* Finish up the enumeration. */
aa290616 14547 if (type != error_mark_node)
14548 {
14549 if (new_value_list)
14550 finish_enum_value_list (type);
14551 if (is_new_type)
14552 finish_enum (type);
14553 }
0a3b29ad 14554
aa290616 14555 if (nested_name_specifier)
14556 {
14557 if (CLASS_TYPE_P (nested_name_specifier))
14558 {
14559 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14560 pop_scope (nested_name_specifier);
14561 }
14562 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14563 {
14564 pop_nested_namespace (nested_name_specifier);
14565 }
14566 }
1e838f42 14567 out:
14568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
0a3b29ad 14569 return type;
14570}
14571
14572/* Parse an enumerator-list. The enumerators all have the indicated
ccb84981 14573 TYPE.
0a3b29ad 14574
14575 enumerator-list:
14576 enumerator-definition
14577 enumerator-list , enumerator-definition */
14578
14579static void
45baea8b 14580cp_parser_enumerator_list (cp_parser* parser, tree type)
0a3b29ad 14581{
14582 while (true)
14583 {
0a3b29ad 14584 /* Parse an enumerator-definition. */
14585 cp_parser_enumerator_definition (parser, type);
637fdc50 14586
14587 /* If the next token is not a ',', we've reached the end of
14588 the list. */
14589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
0a3b29ad 14590 break;
14591 /* Otherwise, consume the `,' and keep going. */
14592 cp_lexer_consume_token (parser->lexer);
14593 /* If the next token is a `}', there is a trailing comma. */
14594 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14595 {
ee239d91 14596 if (cxx_dialect < cxx0x && !in_system_header)
29438999 14597 pedwarn (input_location, OPT_Wpedantic,
ee239d91 14598 "comma at end of enumerator list");
0a3b29ad 14599 break;
14600 }
14601 }
14602}
14603
14604/* Parse an enumerator-definition. The enumerator has the indicated
14605 TYPE.
14606
14607 enumerator-definition:
14608 enumerator
14609 enumerator = constant-expression
ccb84981 14610
0a3b29ad 14611 enumerator:
14612 identifier */
14613
14614static void
45baea8b 14615cp_parser_enumerator_definition (cp_parser* parser, tree type)
0a3b29ad 14616{
0a3b29ad 14617 tree identifier;
14618 tree value;
cbdababb 14619 location_t loc;
14620
14621 /* Save the input location because we are interested in the location
14622 of the identifier and not the location of the explicit value. */
14623 loc = cp_lexer_peek_token (parser->lexer)->location;
0a3b29ad 14624
14625 /* Look for the identifier. */
14626 identifier = cp_parser_identifier (parser);
14627 if (identifier == error_mark_node)
14628 return;
ccb84981 14629
637fdc50 14630 /* If the next token is an '=', then there is an explicit value. */
14631 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
0a3b29ad 14632 {
14633 /* Consume the `=' token. */
14634 cp_lexer_consume_token (parser->lexer);
14635 /* Parse the value. */
ccb84981 14636 value = cp_parser_constant_expression (parser,
13795292 14637 /*allow_non_constant_p=*/false,
5f6526e1 14638 NULL);
0a3b29ad 14639 }
14640 else
14641 value = NULL_TREE;
14642
ccc5f70d 14643 /* If we are processing a template, make sure the initializer of the
14644 enumerator doesn't contain any bare template parameter pack. */
14645 if (check_for_bare_parameter_packs (value))
14646 value = error_mark_node;
14647
9aad9f90 14648 /* integral_constant_value will pull out this expression, so make sure
14649 it's folded as appropriate. */
14650 value = fold_non_dependent_expr (value);
14651
0a3b29ad 14652 /* Create the enumerator. */
cbdababb 14653 build_enumerator (identifier, value, type, loc);
0a3b29ad 14654}
14655
14656/* Parse a namespace-name.
14657
14658 namespace-name:
14659 original-namespace-name
14660 namespace-alias
14661
14662 Returns the NAMESPACE_DECL for the namespace. */
14663
14664static tree
45baea8b 14665cp_parser_namespace_name (cp_parser* parser)
0a3b29ad 14666{
14667 tree identifier;
14668 tree namespace_decl;
14669
ad9ae192 14670 cp_token *token = cp_lexer_peek_token (parser->lexer);
14671
0a3b29ad 14672 /* Get the name of the namespace. */
14673 identifier = cp_parser_identifier (parser);
14674 if (identifier == error_mark_node)
14675 return error_mark_node;
14676
6fc758aa 14677 /* Look up the identifier in the currently active scope. Look only
14678 for namespaces, due to:
14679
14680 [basic.lookup.udir]
14681
14682 When looking up a namespace-name in a using-directive or alias
ccb84981 14683 definition, only namespace names are considered.
6fc758aa 14684
14685 And:
14686
14687 [basic.lookup.qual]
14688
14689 During the lookup of a name preceding the :: scope resolution
ccb84981 14690 operator, object, function, and enumerator names are ignored.
6fc758aa 14691
3f00a6c0 14692 (Note that cp_parser_qualifying_entity only calls this
6fc758aa 14693 function if the token after the name is the scope resolution
14694 operator.) */
14695 namespace_decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 14696 none_type,
c3b9e457 14697 /*is_template=*/false,
6fc758aa 14698 /*is_namespace=*/true,
2cdbcd51 14699 /*check_dependency=*/true,
ad9ae192 14700 /*ambiguous_decls=*/NULL,
14701 token->location);
0a3b29ad 14702 /* If it's not a namespace, issue an error. */
14703 if (namespace_decl == error_mark_node
14704 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14705 {
f75d14ca 14706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb59bb6 14707 error_at (token->location, "%qD is not a namespace-name", identifier);
0a3b29ad 14708 cp_parser_error (parser, "expected namespace-name");
14709 namespace_decl = error_mark_node;
14710 }
ccb84981 14711
0a3b29ad 14712 return namespace_decl;
14713}
14714
14715/* Parse a namespace-definition.
14716
14717 namespace-definition:
14718 named-namespace-definition
ccb84981 14719 unnamed-namespace-definition
0a3b29ad 14720
14721 named-namespace-definition:
14722 original-namespace-definition
14723 extension-namespace-definition
14724
14725 original-namespace-definition:
14726 namespace identifier { namespace-body }
ccb84981 14727
0a3b29ad 14728 extension-namespace-definition:
14729 namespace original-namespace-name { namespace-body }
ccb84981 14730
0a3b29ad 14731 unnamed-namespace-definition:
14732 namespace { namespace-body } */
14733
14734static void
45baea8b 14735cp_parser_namespace_definition (cp_parser* parser)
0a3b29ad 14736{
799435d8 14737 tree identifier, attribs;
1fd77946 14738 bool has_visibility;
93635a8e 14739 bool is_inline;
14740
14741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14742 {
af28195d 14743 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
93635a8e 14744 is_inline = true;
14745 cp_lexer_consume_token (parser->lexer);
14746 }
14747 else
14748 is_inline = false;
0a3b29ad 14749
14750 /* Look for the `namespace' keyword. */
c247dce0 14751 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 14752
14753 /* Get the name of the namespace. We do not attempt to distinguish
14754 between an original-namespace-definition and an
14755 extension-namespace-definition at this point. The semantic
14756 analysis routines are responsible for that. */
14757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14758 identifier = cp_parser_identifier (parser);
14759 else
14760 identifier = NULL_TREE;
14761
799435d8 14762 /* Parse any specified attributes. */
14763 attribs = cp_parser_attributes_opt (parser);
14764
0a3b29ad 14765 /* Look for the `{' to start the namespace. */
c247dce0 14766 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
0a3b29ad 14767 /* Start the namespace. */
1fd77946 14768 push_namespace (identifier);
14769
93635a8e 14770 /* "inline namespace" is equivalent to a stub namespace definition
14771 followed by a strong using directive. */
14772 if (is_inline)
14773 {
607a5d68 14774 tree name_space = current_namespace;
93635a8e 14775 /* Set up namespace association. */
607a5d68 14776 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14777 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14778 DECL_NAMESPACE_ASSOCIATIONS (name_space));
93635a8e 14779 /* Import the contents of the inline namespace. */
14780 pop_namespace ();
607a5d68 14781 do_using_directive (name_space);
93635a8e 14782 push_namespace (identifier);
14783 }
14784
1fd77946 14785 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14786
0a3b29ad 14787 /* Parse the body of the namespace. */
14788 cp_parser_namespace_body (parser);
1fd77946 14789
1fd77946 14790 if (has_visibility)
f1658ae0 14791 pop_visibility (1);
1fd77946 14792
0a3b29ad 14793 /* Finish the namespace. */
14794 pop_namespace ();
14795 /* Look for the final `}'. */
c247dce0 14796 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
0a3b29ad 14797}
14798
14799/* Parse a namespace-body.
14800
14801 namespace-body:
14802 declaration-seq [opt] */
14803
14804static void
45baea8b 14805cp_parser_namespace_body (cp_parser* parser)
0a3b29ad 14806{
14807 cp_parser_declaration_seq_opt (parser);
14808}
14809
14810/* Parse a namespace-alias-definition.
14811
14812 namespace-alias-definition:
14813 namespace identifier = qualified-namespace-specifier ; */
14814
14815static void
45baea8b 14816cp_parser_namespace_alias_definition (cp_parser* parser)
0a3b29ad 14817{
14818 tree identifier;
14819 tree namespace_specifier;
14820
ad9ae192 14821 cp_token *token = cp_lexer_peek_token (parser->lexer);
14822
0a3b29ad 14823 /* Look for the `namespace' keyword. */
c247dce0 14824 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 14825 /* Look for the identifier. */
14826 identifier = cp_parser_identifier (parser);
14827 if (identifier == error_mark_node)
14828 return;
14829 /* Look for the `=' token. */
26d3c536 14830 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14831 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14832 {
ccb59bb6 14833 error_at (token->location, "%<namespace%> definition is not allowed here");
26d3c536 14834 /* Skip the definition. */
14835 cp_lexer_consume_token (parser->lexer);
9aad947b 14836 if (cp_parser_skip_to_closing_brace (parser))
14837 cp_lexer_consume_token (parser->lexer);
26d3c536 14838 return;
14839 }
c247dce0 14840 cp_parser_require (parser, CPP_EQ, RT_EQ);
0a3b29ad 14841 /* Look for the qualified-namespace-specifier. */
ccb84981 14842 namespace_specifier
0a3b29ad 14843 = cp_parser_qualified_namespace_specifier (parser);
14844 /* Look for the `;' token. */
c247dce0 14845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 14846
14847 /* Register the alias in the symbol table. */
14848 do_namespace_alias (identifier, namespace_specifier);
14849}
14850
14851/* Parse a qualified-namespace-specifier.
14852
14853 qualified-namespace-specifier:
14854 :: [opt] nested-name-specifier [opt] namespace-name
14855
14856 Returns a NAMESPACE_DECL corresponding to the specified
14857 namespace. */
14858
14859static tree
45baea8b 14860cp_parser_qualified_namespace_specifier (cp_parser* parser)
0a3b29ad 14861{
14862 /* Look for the optional `::'. */
ccb84981 14863 cp_parser_global_scope_opt (parser,
130bb1d4 14864 /*current_scope_valid_p=*/false);
0a3b29ad 14865
14866 /* Look for the optional nested-name-specifier. */
14867 cp_parser_nested_name_specifier_opt (parser,
14868 /*typename_keyword_p=*/false,
14869 /*check_dependency_p=*/true,
3d0f901b 14870 /*type_p=*/false,
14871 /*is_declaration=*/true);
0a3b29ad 14872
14873 return cp_parser_namespace_name (parser);
14874}
14875
da2a3271 14876/* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14877 access declaration.
0a3b29ad 14878
14879 using-declaration:
14880 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
da2a3271 14881 using :: unqualified-id ;
0a3b29ad 14882
da2a3271 14883 access-declaration:
14884 qualified-id ;
14885
14886 */
14887
14888static bool
14889cp_parser_using_declaration (cp_parser* parser,
14890 bool access_declaration_p)
0a3b29ad 14891{
14892 cp_token *token;
14893 bool typename_p = false;
14894 bool global_scope_p;
14895 tree decl;
14896 tree identifier;
2c47ecdb 14897 tree qscope;
3239a030 14898 int oldcount = errorcount;
14899 cp_token *diag_token = NULL;
0a3b29ad 14900
da2a3271 14901 if (access_declaration_p)
3239a030 14902 {
14903 diag_token = cp_lexer_peek_token (parser->lexer);
14904 cp_parser_parse_tentatively (parser);
14905 }
da2a3271 14906 else
0a3b29ad 14907 {
da2a3271 14908 /* Look for the `using' keyword. */
c247dce0 14909 cp_parser_require_keyword (parser, RID_USING, RT_USING);
da2a3271 14910
14911 /* Peek at the next token. */
14912 token = cp_lexer_peek_token (parser->lexer);
14913 /* See if it's `typename'. */
14914 if (token->keyword == RID_TYPENAME)
14915 {
14916 /* Remember that we've seen it. */
14917 typename_p = true;
14918 /* Consume the `typename' token. */
14919 cp_lexer_consume_token (parser->lexer);
14920 }
0a3b29ad 14921 }
14922
14923 /* Look for the optional global scope qualification. */
ccb84981 14924 global_scope_p
0a3b29ad 14925 = (cp_parser_global_scope_opt (parser,
130bb1d4 14926 /*current_scope_valid_p=*/false)
0a3b29ad 14927 != NULL_TREE);
14928
14929 /* If we saw `typename', or didn't see `::', then there must be a
14930 nested-name-specifier present. */
14931 if (typename_p || !global_scope_p)
ccb84981 14932 qscope = cp_parser_nested_name_specifier (parser, typename_p,
2c47ecdb 14933 /*check_dependency_p=*/true,
14934 /*type_p=*/false,
14935 /*is_declaration=*/true);
0a3b29ad 14936 /* Otherwise, we could be in either of the two productions. In that
14937 case, treat the nested-name-specifier as optional. */
14938 else
2c47ecdb 14939 qscope = cp_parser_nested_name_specifier_opt (parser,
14940 /*typename_keyword_p=*/false,
14941 /*check_dependency_p=*/true,
14942 /*type_p=*/false,
14943 /*is_declaration=*/true);
14944 if (!qscope)
14945 qscope = global_namespace;
0a3b29ad 14946
7638e10c 14947 if (access_declaration_p && cp_parser_error_occurred (parser))
14948 /* Something has already gone wrong; there's no need to parse
14949 further. Since an error has occurred, the return value of
14950 cp_parser_parse_definitely will be false, as required. */
14951 return cp_parser_parse_definitely (parser);
14952
ad9ae192 14953 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 14954 /* Parse the unqualified-id. */
ccb84981 14955 identifier = cp_parser_unqualified_id (parser,
0a3b29ad 14956 /*template_keyword_p=*/false,
899cc6e8 14957 /*check_dependency_p=*/true,
197c9df7 14958 /*declarator_p=*/true,
130bb1d4 14959 /*optional_p=*/false);
0a3b29ad 14960
da2a3271 14961 if (access_declaration_p)
14962 {
14963 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14964 cp_parser_simulate_error (parser);
14965 if (!cp_parser_parse_definitely (parser))
14966 return false;
14967 }
14968
0a3b29ad 14969 /* The function we call to handle a using-declaration is different
14970 depending on what scope we are in. */
c91d0d7d 14971 if (qscope == error_mark_node || identifier == error_mark_node)
899cc6e8 14972 ;
14973 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14974 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14975 /* [namespace.udecl]
14976
14977 A using declaration shall not name a template-id. */
ccb59bb6 14978 error_at (token->location,
14979 "a template-id may not appear in a using-declaration");
0a3b29ad 14980 else
14981 {
46f43a6b 14982 if (at_class_scope_p ())
642a03ff 14983 {
899cc6e8 14984 /* Create the USING_DECL. */
2ded3667 14985 decl = do_class_using_decl (parser->scope, identifier);
613687b1 14986
9e92dfe5 14987 if (decl && typename_p)
3d79ce2d 14988 USING_DECL_TYPENAME_P (decl) = 1;
14989
830a6615 14990 if (check_for_bare_parameter_packs (decl))
613687b1 14991 return false;
9e92dfe5 14992 else
613687b1 14993 /* Add it to the list of members in this class. */
14994 finish_member_declaration (decl);
642a03ff 14995 }
0a3b29ad 14996 else
899cc6e8 14997 {
ad9ae192 14998 decl = cp_parser_lookup_name_simple (parser,
14999 identifier,
15000 token->location);
899cc6e8 15001 if (decl == error_mark_node)
ad9ae192 15002 cp_parser_name_lookup_error (parser, identifier,
c247dce0 15003 decl, NLE_NULL,
ad9ae192 15004 token->location);
830a6615 15005 else if (check_for_bare_parameter_packs (decl))
613687b1 15006 return false;
46f43a6b 15007 else if (!at_namespace_scope_p ())
2c47ecdb 15008 do_local_using_decl (decl, qscope, identifier);
899cc6e8 15009 else
2c47ecdb 15010 do_toplevel_using_decl (decl, qscope, identifier);
899cc6e8 15011 }
0a3b29ad 15012 }
15013
15014 /* Look for the final `;'. */
c247dce0 15015 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
3239a030 15016
15017 if (access_declaration_p && errorcount == oldcount)
15018 warning_at (diag_token->location, OPT_Wdeprecated,
15019 "access declarations are deprecated "
15020 "in favour of using-declarations; "
15021 "suggestion: add the %<using%> keyword");
15022
da2a3271 15023 return true;
0a3b29ad 15024}
15025
370478b1 15026/* Parse an alias-declaration.
15027
15028 alias-declaration:
15029 using identifier attribute-specifier-seq [opt] = type-id */
15030
15031static tree
15032cp_parser_alias_declaration (cp_parser* parser)
15033{
7961105f 15034 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
a60f3e81 15035 location_t id_location, using_location, attrs_location = 0;
370478b1 15036 cp_declarator *declarator;
15037 cp_decl_specifier_seq decl_specs;
6ffe24ee 15038 bool member_p;
341dcc7d 15039 const char *saved_message = NULL;
370478b1 15040
15041 /* Look for the `using' keyword. */
a60f3e81 15042 using_location = cp_lexer_peek_token (parser->lexer)->location;
370478b1 15043 cp_parser_require_keyword (parser, RID_USING, RT_USING);
15044 id_location = cp_lexer_peek_token (parser->lexer)->location;
15045 id = cp_parser_identifier (parser);
f9d09581 15046 if (id == error_mark_node)
15047 return error_mark_node;
15048
a60f3e81 15049 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
370478b1 15050 attributes = cp_parser_attributes_opt (parser);
f9d09581 15051 if (attributes == error_mark_node)
15052 return error_mark_node;
15053
370478b1 15054 cp_parser_require (parser, CPP_EQ, RT_EQ);
15055
341dcc7d 15056 /* Now we are going to parse the type-id of the declaration. */
15057
15058 /*
15059 [dcl.type]/3 says:
15060
15061 "A type-specifier-seq shall not define a class or enumeration
15062 unless it appears in the type-id of an alias-declaration (7.1.3) that
15063 is not the declaration of a template-declaration."
15064
15065 In other words, if we currently are in an alias template, the
15066 type-id should not define a type.
15067
15068 So let's set parser->type_definition_forbidden_message in that
15069 case; cp_parser_check_type_definition (called by
15070 cp_parser_class_specifier) will then emit an error if a type is
15071 defined in the type-id. */
15072 if (parser->num_template_parameter_lists)
15073 {
15074 saved_message = parser->type_definition_forbidden_message;
15075 parser->type_definition_forbidden_message =
15076 G_("types may not be defined in alias template declarations");
15077 }
15078
370478b1 15079 type = cp_parser_type_id (parser);
341dcc7d 15080
15081 /* Restore the error message if need be. */
15082 if (parser->num_template_parameter_lists)
15083 parser->type_definition_forbidden_message = saved_message;
15084
cc69d18d 15085 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
370478b1 15086
982abe7b 15087 if (cp_parser_error_occurred (parser))
15088 return error_mark_node;
15089
370478b1 15090 /* A typedef-name can also be introduced by an alias-declaration. The
15091 identifier following the using keyword becomes a typedef-name. It has
15092 the same semantics as if it were introduced by the typedef
15093 specifier. In particular, it does not define a new type and it shall
15094 not appear in the type-id. */
15095
15096 clear_decl_specs (&decl_specs);
15097 decl_specs.type = type;
a60f3e81 15098 if (attributes != NULL_TREE)
15099 {
15100 decl_specs.attributes = attributes;
15101 set_and_check_decl_spec_loc (&decl_specs,
15102 ds_attribute,
15103 attrs_location);
15104 }
15105 set_and_check_decl_spec_loc (&decl_specs,
15106 ds_typedef,
15107 using_location);
15108 set_and_check_decl_spec_loc (&decl_specs,
15109 ds_alias,
15110 using_location);
370478b1 15111
15112 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15113 declarator->id_loc = id_location;
15114
6ffe24ee 15115 member_p = at_class_scope_p ();
15116 if (member_p)
370478b1 15117 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15118 NULL_TREE, attributes);
15119 else
15120 decl = start_decl (declarator, &decl_specs, 0,
7961105f 15121 attributes, NULL_TREE, &pushed_scope);
370478b1 15122 if (decl == error_mark_node)
15123 return decl;
15124
15125 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15126
7961105f 15127 if (pushed_scope)
15128 pop_scope (pushed_scope);
15129
370478b1 15130 /* If decl is a template, return its TEMPLATE_DECL so that it gets
15131 added into the symbol table; otherwise, return the TYPE_DECL. */
15132 if (DECL_LANG_SPECIFIC (decl)
15133 && DECL_TEMPLATE_INFO (decl)
15134 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
6ffe24ee 15135 {
15136 decl = DECL_TI_TEMPLATE (decl);
15137 if (member_p)
15138 check_member_template (decl);
15139 }
15140
370478b1 15141 return decl;
15142}
15143
ccb84981 15144/* Parse a using-directive.
15145
0a3b29ad 15146 using-directive:
15147 using namespace :: [opt] nested-name-specifier [opt]
15148 namespace-name ; */
15149
15150static void
45baea8b 15151cp_parser_using_directive (cp_parser* parser)
0a3b29ad 15152{
15153 tree namespace_decl;
a5ed46c9 15154 tree attribs;
0a3b29ad 15155
15156 /* Look for the `using' keyword. */
c247dce0 15157 cp_parser_require_keyword (parser, RID_USING, RT_USING);
0a3b29ad 15158 /* And the `namespace' keyword. */
c247dce0 15159 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
0a3b29ad 15160 /* Look for the optional `::' operator. */
130bb1d4 15161 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
755edffd 15162 /* And the optional nested-name-specifier. */
0a3b29ad 15163 cp_parser_nested_name_specifier_opt (parser,
15164 /*typename_keyword_p=*/false,
15165 /*check_dependency_p=*/true,
3d0f901b 15166 /*type_p=*/false,
15167 /*is_declaration=*/true);
0a3b29ad 15168 /* Get the namespace being used. */
15169 namespace_decl = cp_parser_namespace_name (parser);
a5ed46c9 15170 /* And any specified attributes. */
15171 attribs = cp_parser_attributes_opt (parser);
0a3b29ad 15172 /* Update the symbol table. */
a5ed46c9 15173 parse_using_directive (namespace_decl, attribs);
0a3b29ad 15174 /* Look for the final `;'. */
c247dce0 15175 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 15176}
15177
15178/* Parse an asm-definition.
15179
15180 asm-definition:
ccb84981 15181 asm ( string-literal ) ;
0a3b29ad 15182
15183 GNU Extension:
15184
15185 asm-definition:
15186 asm volatile [opt] ( string-literal ) ;
15187 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15188 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 15189 : asm-operand-list [opt] ) ;
ccb84981 15190 asm volatile [opt] ( string-literal : asm-operand-list [opt]
653e5405 15191 : asm-operand-list [opt]
78f55ca8 15192 : asm-clobber-list [opt] ) ;
15193 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15194 : asm-clobber-list [opt]
15195 : asm-goto-list ) ; */
0a3b29ad 15196
15197static void
45baea8b 15198cp_parser_asm_definition (cp_parser* parser)
0a3b29ad 15199{
0a3b29ad 15200 tree string;
15201 tree outputs = NULL_TREE;
15202 tree inputs = NULL_TREE;
15203 tree clobbers = NULL_TREE;
78f55ca8 15204 tree labels = NULL_TREE;
0a3b29ad 15205 tree asm_stmt;
15206 bool volatile_p = false;
15207 bool extended_p = false;
a40e70de 15208 bool invalid_inputs_p = false;
15209 bool invalid_outputs_p = false;
78f55ca8 15210 bool goto_p = false;
c61e1212 15211 required_token missing = RT_NONE;
0a3b29ad 15212
15213 /* Look for the `asm' keyword. */
c247dce0 15214 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
0a3b29ad 15215 /* See if the next token is `volatile'. */
15216 if (cp_parser_allow_gnu_extensions_p (parser)
15217 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15218 {
15219 /* Remember that we saw the `volatile' keyword. */
15220 volatile_p = true;
15221 /* Consume the token. */
15222 cp_lexer_consume_token (parser->lexer);
15223 }
78f55ca8 15224 if (cp_parser_allow_gnu_extensions_p (parser)
15225 && parser->in_function_body
15226 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15227 {
15228 /* Remember that we saw the `goto' keyword. */
15229 goto_p = true;
15230 /* Consume the token. */
15231 cp_lexer_consume_token (parser->lexer);
15232 }
0a3b29ad 15233 /* Look for the opening `('. */
c247dce0 15234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
00d26680 15235 return;
0a3b29ad 15236 /* Look for the string. */
00d26680 15237 string = cp_parser_string_literal (parser, false, false);
15238 if (string == error_mark_node)
15239 {
15240 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15241 /*consume_paren=*/true);
15242 return;
15243 }
15244
0a3b29ad 15245 /* If we're allowing GNU extensions, check for the extended assembly
ccb84981 15246 syntax. Unfortunately, the `:' tokens need not be separated by
0a3b29ad 15247 a space in C, and so, for compatibility, we tolerate that here
15248 too. Doing that means that we have to treat the `::' operator as
15249 two `:' tokens. */
15250 if (cp_parser_allow_gnu_extensions_p (parser)
0aeb1cc5 15251 && parser->in_function_body
0a3b29ad 15252 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15253 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15254 {
15255 bool inputs_p = false;
15256 bool clobbers_p = false;
78f55ca8 15257 bool labels_p = false;
0a3b29ad 15258
15259 /* The extended syntax was used. */
15260 extended_p = true;
15261
15262 /* Look for outputs. */
15263 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15264 {
15265 /* Consume the `:'. */
15266 cp_lexer_consume_token (parser->lexer);
15267 /* Parse the output-operands. */
ccb84981 15268 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 15269 CPP_COLON)
15270 && cp_lexer_next_token_is_not (parser->lexer,
f2050a0c 15271 CPP_SCOPE)
15272 && cp_lexer_next_token_is_not (parser->lexer,
78f55ca8 15273 CPP_CLOSE_PAREN)
15274 && !goto_p)
0a3b29ad 15275 outputs = cp_parser_asm_operand_list (parser);
a40e70de 15276
15277 if (outputs == error_mark_node)
15278 invalid_outputs_p = true;
0a3b29ad 15279 }
15280 /* If the next token is `::', there are no outputs, and the
15281 next token is the beginning of the inputs. */
15282 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8f26d225 15283 /* The inputs are coming next. */
15284 inputs_p = true;
0a3b29ad 15285
15286 /* Look for inputs. */
15287 if (inputs_p
15288 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15289 {
8f26d225 15290 /* Consume the `:' or `::'. */
15291 cp_lexer_consume_token (parser->lexer);
0a3b29ad 15292 /* Parse the output-operands. */
ccb84981 15293 if (cp_lexer_next_token_is_not (parser->lexer,
0a3b29ad 15294 CPP_COLON)
78f55ca8 15295 && cp_lexer_next_token_is_not (parser->lexer,
15296 CPP_SCOPE)
f2050a0c 15297 && cp_lexer_next_token_is_not (parser->lexer,
15298 CPP_CLOSE_PAREN))
0a3b29ad 15299 inputs = cp_parser_asm_operand_list (parser);
a40e70de 15300
15301 if (inputs == error_mark_node)
15302 invalid_inputs_p = true;
0a3b29ad 15303 }
15304 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15305 /* The clobbers are coming next. */
15306 clobbers_p = true;
15307
15308 /* Look for clobbers. */
ccb84981 15309 if (clobbers_p
0a3b29ad 15310 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15311 {
78f55ca8 15312 clobbers_p = true;
8f26d225 15313 /* Consume the `:' or `::'. */
15314 cp_lexer_consume_token (parser->lexer);
0a3b29ad 15315 /* Parse the clobbers. */
f2050a0c 15316 if (cp_lexer_next_token_is_not (parser->lexer,
78f55ca8 15317 CPP_COLON)
15318 && cp_lexer_next_token_is_not (parser->lexer,
15319 CPP_CLOSE_PAREN))
f2050a0c 15320 clobbers = cp_parser_asm_clobber_list (parser);
0a3b29ad 15321 }
78f55ca8 15322 else if (goto_p
15323 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15324 /* The labels are coming next. */
15325 labels_p = true;
15326
15327 /* Look for labels. */
15328 if (labels_p
15329 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15330 {
15331 labels_p = true;
15332 /* Consume the `:' or `::'. */
15333 cp_lexer_consume_token (parser->lexer);
15334 /* Parse the labels. */
15335 labels = cp_parser_asm_label_list (parser);
15336 }
15337
15338 if (goto_p && !labels_p)
c247dce0 15339 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
0a3b29ad 15340 }
78f55ca8 15341 else if (goto_p)
c247dce0 15342 missing = RT_COLON_SCOPE;
78f55ca8 15343
0a3b29ad 15344 /* Look for the closing `)'. */
78f55ca8 15345 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
c247dce0 15346 missing ? missing : RT_CLOSE_PAREN))
3d0f901b 15347 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15348 /*consume_paren=*/true);
c247dce0 15349 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 15350
a40e70de 15351 if (!invalid_inputs_p && !invalid_outputs_p)
0a3b29ad 15352 {
a40e70de 15353 /* Create the ASM_EXPR. */
15354 if (parser->in_function_body)
36d35193 15355 {
a40e70de 15356 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
78f55ca8 15357 inputs, clobbers, labels);
a40e70de 15358 /* If the extended syntax was not used, mark the ASM_EXPR. */
15359 if (!extended_p)
15360 {
15361 tree temp = asm_stmt;
15362 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15363 temp = TREE_OPERAND (temp, 0);
9031d10b 15364
a40e70de 15365 ASM_INPUT_P (temp) = 1;
15366 }
36d35193 15367 }
a40e70de 15368 else
cf951b1a 15369 add_asm_node (string);
0a3b29ad 15370 }
0a3b29ad 15371}
15372
15373/* Declarators [gram.dcl.decl] */
15374
15375/* Parse an init-declarator.
15376
15377 init-declarator:
15378 declarator initializer [opt]
15379
15380 GNU Extension:
15381
15382 init-declarator:
15383 declarator asm-specification [opt] attributes [opt] initializer [opt]
15384
92b128ed 15385 function-definition:
15386 decl-specifier-seq [opt] declarator ctor-initializer [opt]
ccb84981 15387 function-body
15388 decl-specifier-seq [opt] declarator function-try-block
92b128ed 15389
15390 GNU Extension:
15391
15392 function-definition:
ccb84981 15393 __extension__ function-definition
92b128ed 15394
4c0315d0 15395 TM Extension:
15396
15397 function-definition:
15398 decl-specifier-seq [opt] declarator function-transaction-block
15399
23010bc8 15400 The DECL_SPECIFIERS apply to this declarator. Returns a
15401 representation of the entity declared. If MEMBER_P is TRUE, then
15402 this declarator appears in a class scope. The new DECL created by
15403 this declarator is returned.
15404
15405 The CHECKS are access checks that should be performed once we know
15406 what entity is being declared (and, therefore, what classes have
15407 befriended it).
0a3b29ad 15408
15409 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15410 for a function-definition here as well. If the declarator is a
15411 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15412 be TRUE upon return. By that point, the function-definition will
15413 have been completely parsed.
15414
15415 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
fa7d5870 15416 is FALSE.
15417
15418 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15419 parsed declaration if it is an uninitialized single declarator not followed
15420 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15421 if present, will not be consumed. If returned, this declarator will be
15422 created with SD_INITIALIZED but will not call cp_finish_decl. */
0a3b29ad 15423
15424static tree
ccb84981 15425cp_parser_init_declarator (cp_parser* parser,
4b9b2871 15426 cp_decl_specifier_seq *decl_specifiers,
3369eb76 15427 VEC (deferred_access_check,gc)* checks,
45baea8b 15428 bool function_definition_allowed_p,
15429 bool member_p,
8172be22 15430 int declares_class_or_enum,
fa7d5870 15431 bool* function_definition_p,
15432 tree* maybe_range_for_decl)
0a3b29ad 15433{
ad9ae192 15434 cp_token *token = NULL, *asm_spec_start_token = NULL,
15435 *attributes_start_token = NULL;
3046c0a3 15436 cp_declarator *declarator;
4b9b2871 15437 tree prefix_attributes;
0a3b29ad 15438 tree attributes;
15439 tree asm_specification;
15440 tree initializer;
15441 tree decl = NULL_TREE;
15442 tree scope;
2336da2a 15443 int is_initialized;
393f878f 15444 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15445 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15446 "(...)". */
15447 enum cpp_ttype initialization_kind;
f82f1250 15448 bool is_direct_init = false;
878870b4 15449 bool is_non_constant_init;
0986fa22 15450 int ctor_dtor_or_conv_p;
0a3b29ad 15451 bool friend_p;
a356adfb 15452 tree pushed_scope = NULL_TREE;
fa7d5870 15453 bool range_for_decl_p = false;
0a3b29ad 15454
4b9b2871 15455 /* Gather the attributes that were provided with the
15456 decl-specifiers. */
15457 prefix_attributes = decl_specifiers->attributes;
4b9b2871 15458
0a3b29ad 15459 /* Assume that this is not the declarator for a function
15460 definition. */
15461 if (function_definition_p)
15462 *function_definition_p = false;
15463
15464 /* Defer access checks while parsing the declarator; we cannot know
ccb84981 15465 what names are accessible until we know what is being
0a3b29ad 15466 declared. */
9b57b06b 15467 resume_deferring_access_checks ();
15468
0a3b29ad 15469 /* Parse the declarator. */
ad9ae192 15470 token = cp_lexer_peek_token (parser->lexer);
ccb84981 15471 declarator
42bbd0ec 15472 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 15473 &ctor_dtor_or_conv_p,
08ea345c 15474 /*parenthesized_p=*/NULL,
a526875a 15475 member_p);
0a3b29ad 15476 /* Gather up the deferred checks. */
9b57b06b 15477 stop_deferring_access_checks ();
7488d745 15478
0a3b29ad 15479 /* If the DECLARATOR was erroneous, there's no need to go
15480 further. */
3046c0a3 15481 if (declarator == cp_error_declarator)
9b57b06b 15482 return error_mark_node;
0a3b29ad 15483
04ef83b7 15484 /* Check that the number of template-parameter-lists is OK. */
ad9ae192 15485 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15486 token->location))
04ef83b7 15487 return error_mark_node;
15488
e2ae55f2 15489 if (declares_class_or_enum & 2)
15490 cp_parser_check_for_definition_in_return_type (declarator,
eef0ab03 15491 decl_specifiers->type,
a60f3e81 15492 decl_specifiers->locations[ds_type_spec]);
8172be22 15493
0a3b29ad 15494 /* Figure out what scope the entity declared by the DECLARATOR is
15495 located in. `grokdeclarator' sometimes changes the scope, so
15496 we compute it now. */
15497 scope = get_scope_of_declarator (declarator);
15498
8d2e1854 15499 /* Perform any lookups in the declared type which were thought to be
15500 dependent, but are not in the scope of the declarator. */
15501 decl_specifiers->type
15502 = maybe_update_decl_type (decl_specifiers->type, scope);
15503
0a3b29ad 15504 /* If we're allowing GNU extensions, look for an asm-specification
15505 and attributes. */
15506 if (cp_parser_allow_gnu_extensions_p (parser))
15507 {
15508 /* Look for an asm-specification. */
ad9ae192 15509 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15510 asm_specification = cp_parser_asm_specification_opt (parser);
15511 /* And attributes. */
ad9ae192 15512 attributes_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 15513 attributes = cp_parser_attributes_opt (parser);
15514 }
15515 else
15516 {
15517 asm_specification = NULL_TREE;
15518 attributes = NULL_TREE;
15519 }
15520
15521 /* Peek at the next token. */
15522 token = cp_lexer_peek_token (parser->lexer);
15523 /* Check to see if the token indicates the start of a
15524 function-definition. */
f82f1250 15525 if (function_declarator_p (declarator)
15526 && cp_parser_token_starts_function_definition_p (token))
0a3b29ad 15527 {
15528 if (!function_definition_allowed_p)
15529 {
15530 /* If a function-definition should not appear here, issue an
15531 error message. */
15532 cp_parser_error (parser,
15533 "a function-definition is not allowed here");
15534 return error_mark_node;
15535 }
15536 else
15537 {
d19f0a18 15538 location_t func_brace_location
15539 = cp_lexer_peek_token (parser->lexer)->location;
15540
0a3b29ad 15541 /* Neither attributes nor an asm-specification are allowed
15542 on a function-definition. */
15543 if (asm_specification)
ccb59bb6 15544 error_at (asm_spec_start_token->location,
15545 "an asm-specification is not allowed "
15546 "on a function-definition");
0a3b29ad 15547 if (attributes)
ccb59bb6 15548 error_at (attributes_start_token->location,
15549 "attributes are not allowed on a function-definition");
0a3b29ad 15550 /* This is a function-definition. */
15551 *function_definition_p = true;
15552
0a3b29ad 15553 /* Parse the function definition. */
92b128ed 15554 if (member_p)
15555 decl = cp_parser_save_member_function_body (parser,
15556 decl_specifiers,
15557 declarator,
15558 prefix_attributes);
15559 else
ccb84981 15560 decl
92b128ed 15561 = (cp_parser_function_definition_from_specifiers_and_declarator
15562 (parser, decl_specifiers, prefix_attributes, declarator));
7488d745 15563
d19f0a18 15564 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15565 {
15566 /* This is where the prologue starts... */
15567 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15568 = func_brace_location;
15569 }
15570
0a3b29ad 15571 return decl;
15572 }
15573 }
15574
15575 /* [dcl.dcl]
15576
15577 Only in function declarations for constructors, destructors, and
ccb84981 15578 type conversions can the decl-specifier-seq be omitted.
0a3b29ad 15579
15580 We explicitly postpone this check past the point where we handle
15581 function-definitions because we tolerate function-definitions
15582 that are missing their return types in some modes. */
4b9b2871 15583 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
0a3b29ad 15584 {
ccb84981 15585 cp_parser_error (parser,
0a3b29ad 15586 "expected constructor, destructor, or type conversion");
15587 return error_mark_node;
15588 }
15589
f82f1250 15590 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
393f878f 15591 if (token->type == CPP_EQ
f82f1250 15592 || token->type == CPP_OPEN_PAREN
15593 || token->type == CPP_OPEN_BRACE)
0a3b29ad 15594 {
16f0449a 15595 is_initialized = SD_INITIALIZED;
393f878f 15596 initialization_kind = token->type;
fa7d5870 15597 if (maybe_range_for_decl)
15598 *maybe_range_for_decl = error_mark_node;
2336da2a 15599
15600 if (token->type == CPP_EQ
15601 && function_declarator_p (declarator))
15602 {
15603 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15604 if (t2->keyword == RID_DEFAULT)
16f0449a 15605 is_initialized = SD_DEFAULTED;
2336da2a 15606 else if (t2->keyword == RID_DELETE)
16f0449a 15607 is_initialized = SD_DELETED;
2336da2a 15608 }
393f878f 15609 }
15610 else
15611 {
15612 /* If the init-declarator isn't initialized and isn't followed by a
15613 `,' or `;', it's not a valid init-declarator. */
15614 if (token->type != CPP_COMMA
15615 && token->type != CPP_SEMICOLON)
15616 {
fa7d5870 15617 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15618 range_for_decl_p = true;
15619 else
15620 {
15621 cp_parser_error (parser, "expected initializer");
15622 return error_mark_node;
15623 }
393f878f 15624 }
16f0449a 15625 is_initialized = SD_UNINITIALIZED;
393f878f 15626 initialization_kind = CPP_EOF;
0a3b29ad 15627 }
15628
15629 /* Because start_decl has side-effects, we should only call it if we
15630 know we're going ahead. By this point, we know that we cannot
15631 possibly be looking at any other construct. */
15632 cp_parser_commit_to_tentative_parse (parser);
15633
396d2731 15634 /* If the decl specifiers were bad, issue an error now that we're
15635 sure this was intended to be a declarator. Then continue
15636 declaring the variable(s), as int, to try to cut down on further
15637 errors. */
4b9b2871 15638 if (decl_specifiers->any_specifiers_p
15639 && decl_specifiers->type == error_mark_node)
396d2731 15640 {
15641 cp_parser_error (parser, "invalid type in declaration");
4b9b2871 15642 decl_specifiers->type = integer_type_node;
396d2731 15643 }
15644
0a3b29ad 15645 /* Check to see whether or not this declaration is a friend. */
15646 friend_p = cp_parser_friend_p (decl_specifiers);
15647
0a3b29ad 15648 /* Enter the newly declared entry in the symbol table. If we're
15649 processing a declaration in a class-specifier, we wait until
15650 after processing the initializer. */
15651 if (!member_p)
15652 {
15653 if (parser->in_unbraced_linkage_specification_p)
3b289c9c 15654 decl_specifiers->storage_class = sc_extern;
8512e308 15655 decl = start_decl (declarator, decl_specifiers,
fa7d5870 15656 range_for_decl_p? SD_INITIALIZED : is_initialized,
15657 attributes, prefix_attributes,
7f602bca 15658 &pushed_scope);
0a683683 15659 /* Adjust location of decl if declarator->id_loc is more appropriate:
15660 set, and decl wasn't merged with another decl, in which case its
15661 location would be different from input_location, and more accurate. */
15662 if (DECL_P (decl)
15663 && declarator->id_loc != UNKNOWN_LOCATION
15664 && DECL_SOURCE_LOCATION (decl) == input_location)
15665 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
0a3b29ad 15666 }
91caa6ca 15667 else if (scope)
15668 /* Enter the SCOPE. That way unqualified names appearing in the
15669 initializer will be looked up in SCOPE. */
7f602bca 15670 pushed_scope = push_scope (scope);
0a3b29ad 15671
15672 /* Perform deferred access control checks, now that we know in which
15673 SCOPE the declared entity resides. */
ccb84981 15674 if (!member_p && decl)
0a3b29ad 15675 {
15676 tree saved_current_function_decl = NULL_TREE;
15677
15678 /* If the entity being declared is a function, pretend that we
15679 are in its scope. If it is a `friend', it may have access to
6beb3f76 15680 things that would not otherwise be accessible. */
0a3b29ad 15681 if (TREE_CODE (decl) == FUNCTION_DECL)
15682 {
15683 saved_current_function_decl = current_function_decl;
15684 current_function_decl = decl;
15685 }
ccb84981 15686
23010bc8 15687 /* Perform access checks for template parameters. */
15688 cp_parser_perform_template_parameter_access_checks (checks);
15689
9b57b06b 15690 /* Perform the access control checks for the declarator and the
08cc44e7 15691 decl-specifiers. */
9b57b06b 15692 perform_deferred_access_checks ();
0a3b29ad 15693
15694 /* Restore the saved value. */
15695 if (TREE_CODE (decl) == FUNCTION_DECL)
15696 current_function_decl = saved_current_function_decl;
15697 }
15698
15699 /* Parse the initializer. */
9bbeacce 15700 initializer = NULL_TREE;
f82f1250 15701 is_direct_init = false;
9bbeacce 15702 is_non_constant_init = true;
0a3b29ad 15703 if (is_initialized)
393f878f 15704 {
95f80464 15705 if (function_declarator_p (declarator))
15706 {
ad9ae192 15707 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
95f80464 15708 if (initialization_kind == CPP_EQ)
15709 initializer = cp_parser_pure_specifier (parser);
15710 else
15711 {
15712 /* If the declaration was erroneous, we don't really
15713 know what the user intended, so just silently
15714 consume the initializer. */
15715 if (decl != error_mark_node)
ccb59bb6 15716 error_at (initializer_start_token->location,
15717 "initializer provided for function");
95f80464 15718 cp_parser_skip_to_closing_parenthesis (parser,
15719 /*recovering=*/true,
15720 /*or_comma=*/false,
15721 /*consume_paren=*/true);
15722 }
15723 }
393f878f 15724 else
a8b75081 15725 {
15726 /* We want to record the extra mangling scope for in-class
15727 initializers of class members and initializers of static data
4d9250cf 15728 member templates. The former involves deferring
a8b75081 15729 parsing of the initializer until end of class as with default
15730 arguments. So right here we only handle the latter. */
15731 if (!member_p && processing_template_decl)
15732 start_lambda_scope (decl);
15733 initializer = cp_parser_initializer (parser,
15734 &is_direct_init,
15735 &is_non_constant_init);
15736 if (!member_p && processing_template_decl)
15737 finish_lambda_scope ();
3592befb 15738 if (initializer == error_mark_node)
15739 cp_parser_skip_to_end_of_statement (parser);
a8b75081 15740 }
393f878f 15741 }
0a3b29ad 15742
15743 /* The old parser allows attributes to appear after a parenthesized
15744 initializer. Mark Mitchell proposed removing this functionality
15745 on the GCC mailing lists on 2002-08-13. This parser accepts the
15746 attributes -- but ignores them. */
f82f1250 15747 if (cp_parser_allow_gnu_extensions_p (parser)
15748 && initialization_kind == CPP_OPEN_PAREN)
0a3b29ad 15749 if (cp_parser_attributes_opt (parser))
9b2d6d13 15750 warning (OPT_Wattributes,
15751 "attributes after parenthesized initializer ignored");
0a3b29ad 15752
0a3b29ad 15753 /* For an in-class declaration, use `grokfield' to create the
15754 declaration. */
15755 if (member_p)
69b6679c 15756 {
7f602bca 15757 if (pushed_scope)
f6781b9a 15758 {
7f602bca 15759 pop_scope (pushed_scope);
a356adfb 15760 pushed_scope = NULL_TREE;
f6781b9a 15761 }
69b6679c 15762 decl = grokfield (declarator, decl_specifiers,
d91303a6 15763 initializer, !is_non_constant_init,
15764 /*asmspec=*/NULL_TREE,
d246ef36 15765 prefix_attributes);
69b6679c 15766 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15767 cp_parser_save_default_args (parser, decl);
15768 }
ccb84981 15769
ff764080 15770 /* Finish processing the declaration. But, skip member
0a3b29ad 15771 declarations. */
ff764080 15772 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
91caa6ca 15773 {
15774 cp_finish_decl (decl,
d91303a6 15775 initializer, !is_non_constant_init,
91caa6ca 15776 asm_specification,
15777 /* If the initializer is in parentheses, then this is
15778 a direct-initialization, which means that an
15779 `explicit' constructor is OK. Otherwise, an
15780 `explicit' constructor cannot be used. */
f82f1250 15781 ((is_direct_init || !is_initialized)
a06e700f 15782 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
91caa6ca 15783 }
6dcdb5de 15784 else if ((cxx_dialect != cxx98) && friend_p
15785 && decl && TREE_CODE (decl) == FUNCTION_DECL)
82d31768 15786 /* Core issue #226 (C++0x only): A default template-argument
15787 shall not be specified in a friend class template
15788 declaration. */
15789 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15790 /*is_partial=*/0, /*is_friend_decl=*/1);
15791
7f602bca 15792 if (!friend_p && pushed_scope)
15793 pop_scope (pushed_scope);
0a3b29ad 15794
15795 return decl;
15796}
15797
15798/* Parse a declarator.
ccb84981 15799
0a3b29ad 15800 declarator:
15801 direct-declarator
ccb84981 15802 ptr-operator declarator
0a3b29ad 15803
15804 abstract-declarator:
15805 ptr-operator abstract-declarator [opt]
15806 direct-abstract-declarator
15807
15808 GNU Extensions:
15809
15810 declarator:
15811 attributes [opt] direct-declarator
ccb84981 15812 attributes [opt] ptr-operator declarator
0a3b29ad 15813
15814 abstract-declarator:
15815 attributes [opt] ptr-operator abstract-declarator [opt]
15816 attributes [opt] direct-abstract-declarator
ccb84981 15817
0986fa22 15818 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15819 detect constructor, destructor or conversion operators. It is set
15820 to -1 if the declarator is a name, and +1 if it is a
15821 function. Otherwise it is set to zero. Usually you just want to
15822 test for >0, but internally the negative value is used.
ccb84981 15823
0a3b29ad 15824 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15825 a decl-specifier-seq unless it declares a constructor, destructor,
15826 or conversion. It might seem that we could check this condition in
15827 semantic analysis, rather than parsing, but that makes it difficult
15828 to handle something like `f()'. We want to notice that there are
15829 no decl-specifiers, and therefore realize that this is an
ccb84981 15830 expression, not a declaration.)
15831
92b128ed 15832 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
9031d10b 15833 the declarator is a direct-declarator of the form "(...)".
08ea345c 15834
15835 MEMBER_P is true iff this declarator is a member-declarator. */
0a3b29ad 15836
3046c0a3 15837static cp_declarator *
ccb84981 15838cp_parser_declarator (cp_parser* parser,
653e5405 15839 cp_parser_declarator_kind dcl_kind,
15840 int* ctor_dtor_or_conv_p,
08ea345c 15841 bool* parenthesized_p,
15842 bool member_p)
0a3b29ad 15843{
3046c0a3 15844 cp_declarator *declarator;
0a3b29ad 15845 enum tree_code code;
2cfb6cde 15846 cp_cv_quals cv_quals;
0a3b29ad 15847 tree class_type;
15848 tree attributes = NULL_TREE;
15849
15850 /* Assume this is not a constructor, destructor, or type-conversion
15851 operator. */
15852 if (ctor_dtor_or_conv_p)
0986fa22 15853 *ctor_dtor_or_conv_p = 0;
0a3b29ad 15854
15855 if (cp_parser_allow_gnu_extensions_p (parser))
15856 attributes = cp_parser_attributes_opt (parser);
ccb84981 15857
0a3b29ad 15858 /* Check for the ptr-operator production. */
15859 cp_parser_parse_tentatively (parser);
15860 /* Parse the ptr-operator. */
ccb84981 15861 code = cp_parser_ptr_operator (parser,
15862 &class_type,
2cfb6cde 15863 &cv_quals);
0a3b29ad 15864 /* If that worked, then we have a ptr-operator. */
15865 if (cp_parser_parse_definitely (parser))
15866 {
92b128ed 15867 /* If a ptr-operator was found, then this declarator was not
15868 parenthesized. */
15869 if (parenthesized_p)
15870 *parenthesized_p = true;
0a3b29ad 15871 /* The dependent declarator is optional if we are parsing an
15872 abstract-declarator. */
42bbd0ec 15873 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 15874 cp_parser_parse_tentatively (parser);
15875
15876 /* Parse the dependent declarator. */
42bbd0ec 15877 declarator = cp_parser_declarator (parser, dcl_kind,
92b128ed 15878 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 15879 /*parenthesized_p=*/NULL,
15880 /*member_p=*/false);
0a3b29ad 15881
15882 /* If we are parsing an abstract-declarator, we must handle the
15883 case where the dependent declarator is absent. */
42bbd0ec 15884 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15885 && !cp_parser_parse_definitely (parser))
3046c0a3 15886 declarator = NULL;
ccb84981 15887
63949b38 15888 declarator = cp_parser_make_indirect_declarator
15889 (code, class_type, cv_quals, declarator);
0a3b29ad 15890 }
15891 /* Everything else is a direct-declarator. */
15892 else
92b128ed 15893 {
15894 if (parenthesized_p)
15895 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15896 CPP_OPEN_PAREN);
15897 declarator = cp_parser_direct_declarator (parser, dcl_kind,
08ea345c 15898 ctor_dtor_or_conv_p,
15899 member_p);
92b128ed 15900 }
0a3b29ad 15901
ba0c587d 15902 if (attributes && declarator && declarator != cp_error_declarator)
3046c0a3 15903 declarator->attributes = attributes;
ccb84981 15904
0a3b29ad 15905 return declarator;
15906}
15907
15908/* Parse a direct-declarator or direct-abstract-declarator.
15909
15910 direct-declarator:
15911 declarator-id
15912 direct-declarator ( parameter-declaration-clause )
ccb84981 15913 cv-qualifier-seq [opt]
0a3b29ad 15914 exception-specification [opt]
15915 direct-declarator [ constant-expression [opt] ]
ccb84981 15916 ( declarator )
0a3b29ad 15917
15918 direct-abstract-declarator:
15919 direct-abstract-declarator [opt]
ccb84981 15920 ( parameter-declaration-clause )
0a3b29ad 15921 cv-qualifier-seq [opt]
15922 exception-specification [opt]
15923 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15924 ( abstract-declarator )
15925
42bbd0ec 15926 Returns a representation of the declarator. DCL_KIND is
15927 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15928 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15929 we are parsing a direct-declarator. It is
15930 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15931 of ambiguity we prefer an abstract declarator, as per
08ea345c 15932 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
3046c0a3 15933 cp_parser_declarator. */
0a3b29ad 15934
3046c0a3 15935static cp_declarator *
45baea8b 15936cp_parser_direct_declarator (cp_parser* parser,
653e5405 15937 cp_parser_declarator_kind dcl_kind,
15938 int* ctor_dtor_or_conv_p,
08ea345c 15939 bool member_p)
0a3b29ad 15940{
15941 cp_token *token;
3046c0a3 15942 cp_declarator *declarator = NULL;
0a3b29ad 15943 tree scope = NULL_TREE;
15944 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15945 bool saved_in_declarator_p = parser->in_declarator_p;
42bbd0ec 15946 bool first = true;
7f602bca 15947 tree pushed_scope = NULL_TREE;
ccb84981 15948
42bbd0ec 15949 while (true)
0a3b29ad 15950 {
42bbd0ec 15951 /* Peek at the next token. */
15952 token = cp_lexer_peek_token (parser->lexer);
15953 if (token->type == CPP_OPEN_PAREN)
0a3b29ad 15954 {
42bbd0ec 15955 /* This is either a parameter-declaration-clause, or a
653e5405 15956 parenthesized declarator. When we know we are parsing a
15957 named declarator, it must be a parenthesized declarator
15958 if FIRST is true. For instance, `(int)' is a
15959 parameter-declaration-clause, with an omitted
15960 direct-abstract-declarator. But `((*))', is a
15961 parenthesized abstract declarator. Finally, when T is a
15962 template parameter `(T)' is a
15963 parameter-declaration-clause, and not a parenthesized
15964 named declarator.
ccb84981 15965
42bbd0ec 15966 We first try and parse a parameter-declaration-clause,
15967 and then try a nested declarator (if FIRST is true).
0a3b29ad 15968
42bbd0ec 15969 It is not an error for it not to be a
15970 parameter-declaration-clause, even when FIRST is
15971 false. Consider,
15972
15973 int i (int);
15974 int i (3);
15975
15976 The first is the declaration of a function while the
08cc44e7 15977 second is the definition of a variable, including its
42bbd0ec 15978 initializer.
15979
15980 Having seen only the parenthesis, we cannot know which of
15981 these two alternatives should be selected. Even more
15982 complex are examples like:
15983
653e5405 15984 int i (int (a));
42bbd0ec 15985 int i (int (3));
15986
15987 The former is a function-declaration; the latter is a
ccb84981 15988 variable initialization.
42bbd0ec 15989
755edffd 15990 Thus again, we try a parameter-declaration-clause, and if
42bbd0ec 15991 that fails, we back out and return. */
15992
15993 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
0a3b29ad 15994 {
34eac767 15995 tree params;
bb91f165 15996 unsigned saved_num_template_parameter_lists;
34eac767 15997 bool is_declarator = false;
15998 tree t;
ccb84981 15999
08ea345c 16000 /* In a member-declarator, the only valid interpretation
16001 of a parenthesis is the start of a
16002 parameter-declaration-clause. (It is invalid to
16003 initialize a static data member with a parenthesized
16004 initializer; only the "=" form of initialization is
16005 permitted.) */
16006 if (!member_p)
16007 cp_parser_parse_tentatively (parser);
0a3b29ad 16008
42bbd0ec 16009 /* Consume the `('. */
16010 cp_lexer_consume_token (parser->lexer);
16011 if (first)
16012 {
16013 /* If this is going to be an abstract declarator, we're
16014 in a declarator and we can't have default args. */
16015 parser->default_arg_ok_p = false;
16016 parser->in_declarator_p = true;
16017 }
ccb84981 16018
bb91f165 16019 /* Inside the function parameter list, surrounding
16020 template-parameter-lists do not apply. */
16021 saved_num_template_parameter_lists
16022 = parser->num_template_parameter_lists;
16023 parser->num_template_parameter_lists = 0;
16024
34eac767 16025 begin_scope (sk_function_parms, NULL_TREE);
16026
42bbd0ec 16027 /* Parse the parameter-declaration-clause. */
16028 params = cp_parser_parameter_declaration_clause (parser);
16029
bb91f165 16030 parser->num_template_parameter_lists
16031 = saved_num_template_parameter_lists;
16032
b25cc8c2 16033 /* Consume the `)'. */
16034 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16035
42bbd0ec 16036 /* If all went well, parse the cv-qualifier-seq and the
653e5405 16037 exception-specification. */
08ea345c 16038 if (member_p || cp_parser_parse_definitely (parser))
42bbd0ec 16039 {
2cfb6cde 16040 cp_cv_quals cv_quals;
ece7f9e3 16041 cp_virt_specifiers virt_specifiers;
42bbd0ec 16042 tree exception_specification;
346e3a9c 16043 tree late_return;
0986fa22 16044
34eac767 16045 is_declarator = true;
16046
0986fa22 16047 if (ctor_dtor_or_conv_p)
16048 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
42bbd0ec 16049 first = false;
42bbd0ec 16050
16051 /* Parse the cv-qualifier-seq. */
2cfb6cde 16052 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
42bbd0ec 16053 /* And the exception-specification. */
ccb84981 16054 exception_specification
42bbd0ec 16055 = cp_parser_exception_specification_opt (parser);
ece7f9e3 16056 /* Parse the virt-specifier-seq. */
16057 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
42bbd0ec 16058
a526875a 16059 late_return = (cp_parser_late_return_type_opt
16060 (parser, member_p ? cv_quals : -1));
346e3a9c 16061
42bbd0ec 16062 /* Create the function-declarator. */
16063 declarator = make_call_declarator (declarator,
16064 params,
2cfb6cde 16065 cv_quals,
ece7f9e3 16066 virt_specifiers,
346e3a9c 16067 exception_specification,
16068 late_return);
42bbd0ec 16069 /* Any subsequent parameter lists are to do with
653e5405 16070 return type, so are not those of the declared
16071 function. */
42bbd0ec 16072 parser->default_arg_ok_p = false;
42bbd0ec 16073 }
34eac767 16074
16075 /* Remove the function parms from scope. */
1767a056 16076 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
34eac767 16077 pop_binding (DECL_NAME (t), t);
16078 leave_scope();
16079
16080 if (is_declarator)
16081 /* Repeat the main loop. */
16082 continue;
42bbd0ec 16083 }
ccb84981 16084
42bbd0ec 16085 /* If this is the first, we can try a parenthesized
16086 declarator. */
16087 if (first)
0a3b29ad 16088 {
91f31809 16089 bool saved_in_type_id_in_expr_p;
16090
0a3b29ad 16091 parser->default_arg_ok_p = saved_default_arg_ok_p;
42bbd0ec 16092 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 16093
42bbd0ec 16094 /* Consume the `('. */
16095 cp_lexer_consume_token (parser->lexer);
16096 /* Parse the nested declarator. */
91f31809 16097 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16098 parser->in_type_id_in_expr_p = true;
ccb84981 16099 declarator
92b128ed 16100 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
08ea345c 16101 /*parenthesized_p=*/NULL,
16102 member_p);
91f31809 16103 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
42bbd0ec 16104 first = false;
16105 /* Expect a `)'. */
c247dce0 16106 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3046c0a3 16107 declarator = cp_error_declarator;
16108 if (declarator == cp_error_declarator)
42bbd0ec 16109 break;
ccb84981 16110
42bbd0ec 16111 goto handle_declarator;
0a3b29ad 16112 }
6beb3f76 16113 /* Otherwise, we must be done. */
42bbd0ec 16114 else
16115 break;
0a3b29ad 16116 }
42bbd0ec 16117 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16118 && token->type == CPP_OPEN_SQUARE)
0a3b29ad 16119 {
42bbd0ec 16120 /* Parse an array-declarator. */
0a3b29ad 16121 tree bounds;
16122
0986fa22 16123 if (ctor_dtor_or_conv_p)
16124 *ctor_dtor_or_conv_p = 0;
ccb84981 16125
42bbd0ec 16126 first = false;
16127 parser->default_arg_ok_p = false;
16128 parser->in_declarator_p = true;
0a3b29ad 16129 /* Consume the `['. */
16130 cp_lexer_consume_token (parser->lexer);
16131 /* Peek at the next token. */
16132 token = cp_lexer_peek_token (parser->lexer);
16133 /* If the next token is `]', then there is no
16134 constant-expression. */
16135 if (token->type != CPP_CLOSE_SQUARE)
5f6526e1 16136 {
16137 bool non_constant_p;
16138
ccb84981 16139 bounds
5f6526e1 16140 = cp_parser_constant_expression (parser,
16141 /*allow_non_constant=*/true,
16142 &non_constant_p);
cfa61f84 16143 if (!non_constant_p)
ce984e5e 16144 /* OK */;
d3ab3840 16145 else if (error_operand_p (bounds))
16146 /* Already gave an error. */;
27176e7c 16147 else if (!parser->in_function_body
16148 || current_binding_level->kind == sk_function_parms)
8b652e89 16149 {
d3ab3840 16150 /* Normally, the array bound must be an integral constant
16151 expression. However, as an extension, we allow VLAs
16152 in function scopes as long as they aren't part of a
16153 parameter declaration. */
27176e7c 16154 cp_parser_error (parser,
16155 "array bound is not an integer constant");
8b652e89 16156 bounds = error_mark_node;
16157 }
d3ab3840 16158 else if (processing_template_decl)
d1564595 16159 {
16160 /* Remember this wasn't a constant-expression. */
16161 bounds = build_nop (TREE_TYPE (bounds), bounds);
16162 TREE_SIDE_EFFECTS (bounds) = 1;
16163 }
5f6526e1 16164 }
0a3b29ad 16165 else
16166 bounds = NULL_TREE;
16167 /* Look for the closing `]'. */
c247dce0 16168 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
42bbd0ec 16169 {
3046c0a3 16170 declarator = cp_error_declarator;
42bbd0ec 16171 break;
16172 }
0a3b29ad 16173
3046c0a3 16174 declarator = make_array_declarator (declarator, bounds);
0a3b29ad 16175 }
42bbd0ec 16176 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
0a3b29ad 16177 {
f805d53d 16178 {
16179 tree qualifying_scope;
16180 tree unqualified_name;
16181 special_function_kind sfk;
16182 bool abstract_ok;
16183 bool pack_expansion_p = false;
16184 cp_token *declarator_id_start_token;
16185
16186 /* Parse a declarator-id */
16187 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16188 if (abstract_ok)
16189 {
16190 cp_parser_parse_tentatively (parser);
d95d815d 16191
f805d53d 16192 /* If we see an ellipsis, we should be looking at a
16193 parameter pack. */
16194 if (token->type == CPP_ELLIPSIS)
16195 {
16196 /* Consume the `...' */
16197 cp_lexer_consume_token (parser->lexer);
d95d815d 16198
f805d53d 16199 pack_expansion_p = true;
16200 }
16201 }
d95d815d 16202
f805d53d 16203 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16204 unqualified_name
16205 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16206 qualifying_scope = parser->scope;
16207 if (abstract_ok)
16208 {
16209 bool okay = false;
ccb84981 16210
f805d53d 16211 if (!unqualified_name && pack_expansion_p)
16212 {
16213 /* Check whether an error occurred. */
16214 okay = !cp_parser_error_occurred (parser);
16215
16216 /* We already consumed the ellipsis to mark a
16217 parameter pack, but we have no way to report it,
16218 so abort the tentative parse. We will be exiting
16219 immediately anyway. */
16220 cp_parser_abort_tentative_parse (parser);
16221 }
16222 else
16223 okay = cp_parser_parse_definitely (parser);
ccb84981 16224
f805d53d 16225 if (!okay)
16226 unqualified_name = error_mark_node;
16227 else if (unqualified_name
16228 && (qualifying_scope
16229 || (TREE_CODE (unqualified_name)
16230 != IDENTIFIER_NODE)))
16231 {
16232 cp_parser_error (parser, "expected unqualified-id");
16233 unqualified_name = error_mark_node;
16234 }
16235 }
ccb84981 16236
f805d53d 16237 if (!unqualified_name)
16238 return NULL;
16239 if (unqualified_name == error_mark_node)
16240 {
16241 declarator = cp_error_declarator;
16242 pack_expansion_p = false;
16243 declarator->parameter_pack_p = false;
16244 break;
16245 }
d95d815d 16246
f805d53d 16247 if (qualifying_scope && at_namespace_scope_p ()
16248 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16249 {
16250 /* In the declaration of a member of a template class
16251 outside of the class itself, the SCOPE will sometimes
16252 be a TYPENAME_TYPE. For example, given:
16253
16254 template <typename T>
16255 int S<T>::R::i = 3;
16256
16257 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16258 this context, we must resolve S<T>::R to an ordinary
16259 type, rather than a typename type.
16260
16261 The reason we normally avoid resolving TYPENAME_TYPEs
16262 is that a specialization of `S' might render
16263 `S<T>::R' not a type. However, if `S' is
16264 specialized, then this `i' will not be used, so there
16265 is no harm in resolving the types here. */
16266 tree type;
16267
16268 /* Resolve the TYPENAME_TYPE. */
16269 type = resolve_typename_type (qualifying_scope,
16270 /*only_current_p=*/false);
16271 /* If that failed, the declarator is invalid. */
16272 if (TREE_CODE (type) == TYPENAME_TYPE)
2e6a4932 16273 {
16274 if (typedef_variant_p (type))
16275 error_at (declarator_id_start_token->location,
16276 "cannot define member of dependent typedef "
16277 "%qT", type);
16278 else
16279 error_at (declarator_id_start_token->location,
16280 "%<%T::%E%> is not a type",
16281 TYPE_CONTEXT (qualifying_scope),
16282 TYPE_IDENTIFIER (qualifying_scope));
16283 }
f805d53d 16284 qualifying_scope = type;
16285 }
42bbd0ec 16286
f805d53d 16287 sfk = sfk_none;
42bbd0ec 16288
f805d53d 16289 if (unqualified_name)
16290 {
16291 tree class_type;
2366ed31 16292
f805d53d 16293 if (qualifying_scope
16294 && CLASS_TYPE_P (qualifying_scope))
16295 class_type = qualifying_scope;
16296 else
16297 class_type = current_class_type;
2366ed31 16298
f805d53d 16299 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16300 {
16301 tree name_type = TREE_TYPE (unqualified_name);
16302 if (class_type && same_type_p (name_type, class_type))
16303 {
16304 if (qualifying_scope
16305 && CLASSTYPE_USE_TEMPLATE (name_type))
16306 {
ccb59bb6 16307 error_at (declarator_id_start_token->location,
16308 "invalid use of constructor as a template");
16309 inform (declarator_id_start_token->location,
16310 "use %<%T::%D%> instead of %<%T::%D%> to "
f805d53d 16311 "name the constructor in a qualified name",
16312 class_type,
16313 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16314 class_type, name_type);
16315 declarator = cp_error_declarator;
16316 break;
16317 }
16318 else
16319 unqualified_name = constructor_name (class_type);
16320 }
16321 else
16322 {
16323 /* We do not attempt to print the declarator
16324 here because we do not have enough
16325 information about its original syntactic
16326 form. */
16327 cp_parser_error (parser, "invalid declarator");
16328 declarator = cp_error_declarator;
16329 break;
16330 }
16331 }
d95d815d 16332
f805d53d 16333 if (class_type)
16334 {
16335 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16336 sfk = sfk_destructor;
16337 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16338 sfk = sfk_conversion;
16339 else if (/* There's no way to declare a constructor
16340 for an anonymous type, even if the type
16341 got a name for linkage purposes. */
16342 !TYPE_WAS_ANONYMOUS (class_type)
16343 && constructor_name_p (unqualified_name,
16344 class_type))
16345 {
16346 unqualified_name = constructor_name (class_type);
16347 sfk = sfk_constructor;
16348 }
a70e3c37 16349 else if (is_overloaded_fn (unqualified_name)
16350 && DECL_CONSTRUCTOR_P (get_first_fn
16351 (unqualified_name)))
16352 sfk = sfk_constructor;
f805d53d 16353
16354 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16355 *ctor_dtor_or_conv_p = -1;
16356 }
16357 }
16358 declarator = make_id_declarator (qualifying_scope,
16359 unqualified_name,
16360 sfk);
16361 declarator->id_loc = token->location;
16362 declarator->parameter_pack_p = pack_expansion_p;
16363
16364 if (pack_expansion_p)
16365 maybe_warn_variadic_templates ();
16366 }
42bbd0ec 16367
16368 handle_declarator:;
16369 scope = get_scope_of_declarator (declarator);
16370 if (scope)
1cbda81f 16371 /* Any names that appear after the declarator-id for a
16372 member are looked up in the containing scope. */
7f602bca 16373 pushed_scope = push_scope (scope);
42bbd0ec 16374 parser->in_declarator_p = true;
16375 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
3046c0a3 16376 || (declarator && declarator->kind == cdk_id))
42bbd0ec 16377 /* Default args are only allowed on function
16378 declarations. */
16379 parser->default_arg_ok_p = saved_default_arg_ok_p;
0a3b29ad 16380 else
42bbd0ec 16381 parser->default_arg_ok_p = false;
16382
16383 first = false;
0a3b29ad 16384 }
42bbd0ec 16385 /* We're done. */
0a3b29ad 16386 else
16387 break;
0a3b29ad 16388 }
16389
16390 /* For an abstract declarator, we might wind up with nothing at this
16391 point. That's an error; the declarator is not optional. */
16392 if (!declarator)
16393 cp_parser_error (parser, "expected declarator");
16394
16395 /* If we entered a scope, we must exit it now. */
7f602bca 16396 if (pushed_scope)
16397 pop_scope (pushed_scope);
0a3b29ad 16398
16399 parser->default_arg_ok_p = saved_default_arg_ok_p;
16400 parser->in_declarator_p = saved_in_declarator_p;
ccb84981 16401
0a3b29ad 16402 return declarator;
16403}
16404
ccb84981 16405/* Parse a ptr-operator.
0a3b29ad 16406
16407 ptr-operator:
16408 * cv-qualifier-seq [opt]
16409 &
16410 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16411
16412 GNU Extension:
16413
16414 ptr-operator:
16415 & cv-qualifier-seq [opt]
16416
2cfb6cde 16417 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
63949b38 16418 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16419 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16420 filled in with the TYPE containing the member. *CV_QUALS is
16421 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16422 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16423 Note that the tree codes returned by this function have nothing
16424 to do with the types of trees that will be eventually be created
16425 to represent the pointer or reference type being parsed. They are
16426 just constants with suggestive names. */
0a3b29ad 16427static enum tree_code
ccb84981 16428cp_parser_ptr_operator (cp_parser* parser,
653e5405 16429 tree* type,
2cfb6cde 16430 cp_cv_quals *cv_quals)
0a3b29ad 16431{
16432 enum tree_code code = ERROR_MARK;
16433 cp_token *token;
16434
16435 /* Assume that it's not a pointer-to-member. */
16436 *type = NULL_TREE;
16437 /* And that there are no cv-qualifiers. */
2cfb6cde 16438 *cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 16439
16440 /* Peek at the next token. */
16441 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 16442
63949b38 16443 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16444 if (token->type == CPP_MULT)
16445 code = INDIRECT_REF;
16446 else if (token->type == CPP_AND)
16447 code = ADDR_EXPR;
6dcdb5de 16448 else if ((cxx_dialect != cxx98) &&
16449 token->type == CPP_AND_AND) /* C++0x only */
63949b38 16450 code = NON_LVALUE_EXPR;
16451
16452 if (code != ERROR_MARK)
16453 {
16454 /* Consume the `*', `&' or `&&'. */
0a3b29ad 16455 cp_lexer_consume_token (parser->lexer);
16456
16457 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16458 `&', if we are allowing GNU extensions. (The only qualifier
16459 that can legally appear after `&' is `restrict', but that is
16460 enforced during semantic analysis. */
ccb84981 16461 if (code == INDIRECT_REF
0a3b29ad 16462 || cp_parser_allow_gnu_extensions_p (parser))
2cfb6cde 16463 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
0a3b29ad 16464 }
16465 else
16466 {
16467 /* Try the pointer-to-member case. */
16468 cp_parser_parse_tentatively (parser);
16469 /* Look for the optional `::' operator. */
16470 cp_parser_global_scope_opt (parser,
130bb1d4 16471 /*current_scope_valid_p=*/false);
0a3b29ad 16472 /* Look for the nested-name specifier. */
ad9ae192 16473 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 16474 cp_parser_nested_name_specifier (parser,
16475 /*typename_keyword_p=*/false,
16476 /*check_dependency_p=*/true,
3d0f901b 16477 /*type_p=*/false,
16478 /*is_declaration=*/false);
0a3b29ad 16479 /* If we found it, and the next token is a `*', then we are
16480 indeed looking at a pointer-to-member operator. */
16481 if (!cp_parser_error_occurred (parser)
c247dce0 16482 && cp_parser_require (parser, CPP_MULT, RT_MULT))
0a3b29ad 16483 {
0a3b29ad 16484 /* Indicate that the `*' operator was used. */
16485 code = INDIRECT_REF;
393f878f 16486
16487 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
ccb59bb6 16488 error_at (token->location, "%qD is a namespace", parser->scope);
f979c460 16489 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16490 error_at (token->location, "cannot form pointer to member of "
16491 "non-class %q#T", parser->scope);
393f878f 16492 else
16493 {
16494 /* The type of which the member is a member is given by the
16495 current SCOPE. */
16496 *type = parser->scope;
16497 /* The next name will not be qualified. */
16498 parser->scope = NULL_TREE;
16499 parser->qualifying_scope = NULL_TREE;
16500 parser->object_scope = NULL_TREE;
16501 /* Look for the optional cv-qualifier-seq. */
16502 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16503 }
0a3b29ad 16504 }
16505 /* If that didn't work we don't have a ptr-operator. */
16506 if (!cp_parser_parse_definitely (parser))
16507 cp_parser_error (parser, "expected ptr-operator");
16508 }
16509
16510 return code;
16511}
16512
16513/* Parse an (optional) cv-qualifier-seq.
16514
16515 cv-qualifier-seq:
ccb84981 16516 cv-qualifier cv-qualifier-seq [opt]
0a3b29ad 16517
0a3b29ad 16518 cv-qualifier:
16519 const
ccb84981 16520 volatile
0a3b29ad 16521
16522 GNU Extension:
16523
16524 cv-qualifier:
207355ad 16525 __restrict__
0a3b29ad 16526
2cfb6cde 16527 Returns a bitmask representing the cv-qualifiers. */
16528
16529static cp_cv_quals
16530cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
0a3b29ad 16531{
2cfb6cde 16532 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
0a3b29ad 16533
2cfb6cde 16534 while (true)
0a3b29ad 16535 {
2cfb6cde 16536 cp_token *token;
16537 cp_cv_quals cv_qualifier;
207355ad 16538
2cfb6cde 16539 /* Peek at the next token. */
16540 token = cp_lexer_peek_token (parser->lexer);
16541 /* See if it's a cv-qualifier. */
16542 switch (token->keyword)
16543 {
16544 case RID_CONST:
16545 cv_qualifier = TYPE_QUAL_CONST;
16546 break;
207355ad 16547
2cfb6cde 16548 case RID_VOLATILE:
16549 cv_qualifier = TYPE_QUAL_VOLATILE;
16550 break;
207355ad 16551
2cfb6cde 16552 case RID_RESTRICT:
16553 cv_qualifier = TYPE_QUAL_RESTRICT;
16554 break;
207355ad 16555
2cfb6cde 16556 default:
16557 cv_qualifier = TYPE_UNQUALIFIED;
16558 break;
16559 }
207355ad 16560
2cfb6cde 16561 if (!cv_qualifier)
16562 break;
0a3b29ad 16563
2cfb6cde 16564 if (cv_quals & cv_qualifier)
16565 {
ccb59bb6 16566 error_at (token->location, "duplicate cv-qualifier");
2cfb6cde 16567 cp_lexer_purge_token (parser->lexer);
16568 }
16569 else
16570 {
16571 cp_lexer_consume_token (parser->lexer);
16572 cv_quals |= cv_qualifier;
16573 }
0a3b29ad 16574 }
16575
2cfb6cde 16576 return cv_quals;
0a3b29ad 16577}
16578
ece7f9e3 16579/* Parse an (optional) virt-specifier-seq.
16580
16581 virt-specifier-seq:
16582 virt-specifier virt-specifier-seq [opt]
16583
16584 virt-specifier:
16585 override
16586 final
16587
16588 Returns a bitmask representing the virt-specifiers. */
16589
16590static cp_virt_specifiers
16591cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16592{
16593 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16594
16595 while (true)
16596 {
16597 cp_token *token;
16598 cp_virt_specifiers virt_specifier;
16599
16600 /* Peek at the next token. */
16601 token = cp_lexer_peek_token (parser->lexer);
16602 /* See if it's a virt-specifier-qualifier. */
16603 if (token->type != CPP_NAME)
16604 break;
16605 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
dcdaa0e3 16606 {
16607 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16608 virt_specifier = VIRT_SPEC_OVERRIDE;
16609 }
ece7f9e3 16610 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
dcdaa0e3 16611 {
16612 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16613 virt_specifier = VIRT_SPEC_FINAL;
16614 }
16615 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16616 {
16617 virt_specifier = VIRT_SPEC_FINAL;
16618 }
ece7f9e3 16619 else
16620 break;
16621
16622 if (virt_specifiers & virt_specifier)
16623 {
16624 error_at (token->location, "duplicate virt-specifier");
16625 cp_lexer_purge_token (parser->lexer);
16626 }
16627 else
16628 {
16629 cp_lexer_consume_token (parser->lexer);
16630 virt_specifiers |= virt_specifier;
16631 }
16632 }
16633 return virt_specifiers;
16634}
16635
aa4a12a7 16636/* Used by handling of trailing-return-types and NSDMI, in which 'this'
16637 is in scope even though it isn't real. */
16638
16639static void
16640inject_this_parameter (tree ctype, cp_cv_quals quals)
16641{
16642 tree this_parm;
16643
16644 if (current_class_ptr)
16645 {
16646 /* We don't clear this between NSDMIs. Is it already what we want? */
16647 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16648 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16649 && cp_type_quals (type) == quals)
16650 return;
16651 }
16652
16653 this_parm = build_this_parm (ctype, quals);
16654 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16655 current_class_ptr = NULL_TREE;
16656 current_class_ref
16657 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16658 current_class_ptr = this_parm;
16659}
16660
346e3a9c 16661/* Parse a late-specified return type, if any. This is not a separate
16662 non-terminal, but part of a function declarator, which looks like
16663
638569c5 16664 -> trailing-type-specifier-seq abstract-declarator(opt)
346e3a9c 16665
a526875a 16666 Returns the type indicated by the type-id.
16667
16668 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16669 function. */
346e3a9c 16670
16671static tree
0499ac79 16672cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
346e3a9c 16673{
16674 cp_token *token;
0499ac79 16675 tree type;
346e3a9c 16676
16677 /* Peek at the next token. */
16678 token = cp_lexer_peek_token (parser->lexer);
16679 /* A late-specified return type is indicated by an initial '->'. */
16680 if (token->type != CPP_DEREF)
16681 return NULL_TREE;
16682
16683 /* Consume the ->. */
16684 cp_lexer_consume_token (parser->lexer);
16685
a526875a 16686 if (quals >= 0)
0499ac79 16687 {
16688 /* DR 1207: 'this' is in scope in the trailing return type. */
0499ac79 16689 gcc_assert (current_class_ptr == NULL_TREE);
aa4a12a7 16690 inject_this_parameter (current_class_type, quals);
0499ac79 16691 }
16692
16693 type = cp_parser_trailing_type_id (parser);
16694
e14c7944 16695 if (quals >= 0)
0499ac79 16696 current_class_ptr = current_class_ref = NULL_TREE;
16697
16698 return type;
346e3a9c 16699}
16700
0a3b29ad 16701/* Parse a declarator-id.
16702
16703 declarator-id:
16704 id-expression
ccb84981 16705 :: [opt] nested-name-specifier [opt] type-name
0a3b29ad 16706
16707 In the `id-expression' case, the value returned is as for
16708 cp_parser_id_expression if the id-expression was an unqualified-id.
16709 If the id-expression was a qualified-id, then a SCOPE_REF is
16710 returned. The first operand is the scope (either a NAMESPACE_DECL
16711 or TREE_TYPE), but the second is still just a representation of an
16712 unqualified-id. */
16713
16714static tree
197c9df7 16715cp_parser_declarator_id (cp_parser* parser, bool optional_p)
0a3b29ad 16716{
2366ed31 16717 tree id;
0a3b29ad 16718 /* The expression must be an id-expression. Assume that qualified
16719 names are the names of types so that:
16720
16721 template <class T>
16722 int S<T>::R::i = 3;
16723
16724 will work; we must treat `S<T>::R' as the name of a type.
16725 Similarly, assume that qualified names are templates, where
16726 required, so that:
16727
16728 template <class T>
16729 int S<T>::R<T>::i = 3;
16730
16731 will work, too. */
2366ed31 16732 id = cp_parser_id_expression (parser,
16733 /*template_keyword_p=*/false,
16734 /*check_dependency_p=*/false,
16735 /*template_p=*/NULL,
197c9df7 16736 /*declarator_p=*/true,
130bb1d4 16737 optional_p);
197c9df7 16738 if (id && BASELINK_P (id))
2366ed31 16739 id = BASELINK_FUNCTIONS (id);
16740 return id;
0a3b29ad 16741}
16742
16743/* Parse a type-id.
16744
16745 type-id:
16746 type-specifier-seq abstract-declarator [opt]
16747
16748 Returns the TYPE specified. */
16749
16750static tree
638569c5 16751cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16752 bool is_trailing_return)
0a3b29ad 16753{
4b9b2871 16754 cp_decl_specifier_seq type_specifier_seq;
3046c0a3 16755 cp_declarator *abstract_declarator;
0a3b29ad 16756
16757 /* Parse the type-specifier-seq. */
c44f7faf 16758 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
638569c5 16759 is_trailing_return,
6f74fe3c 16760 &type_specifier_seq);
4b9b2871 16761 if (type_specifier_seq.type == error_mark_node)
0a3b29ad 16762 return error_mark_node;
16763
16764 /* There might or might not be an abstract declarator. */
16765 cp_parser_parse_tentatively (parser);
16766 /* Look for the declarator. */
ccb84981 16767 abstract_declarator
92b128ed 16768 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
08ea345c 16769 /*parenthesized_p=*/NULL,
16770 /*member_p=*/false);
0a3b29ad 16771 /* Check to see if there really was a declarator. */
16772 if (!cp_parser_parse_definitely (parser))
3046c0a3 16773 abstract_declarator = NULL;
0a3b29ad 16774
475fd34e 16775 if (type_specifier_seq.type
16776 && type_uses_auto (type_specifier_seq.type))
16777 {
e439140e 16778 /* A type-id with type 'auto' is only ok if the abstract declarator
16779 is a function declarator with a late-specified return type. */
16780 if (abstract_declarator
16781 && abstract_declarator->kind == cdk_function
16782 && abstract_declarator->u.function.late_return_type)
16783 /* OK */;
16784 else
16785 {
16786 error ("invalid use of %<auto%>");
16787 return error_mark_node;
16788 }
475fd34e 16789 }
16790
75eaa947 16791 return groktypename (&type_specifier_seq, abstract_declarator,
16792 is_template_arg);
16793}
16794
16795static tree cp_parser_type_id (cp_parser *parser)
16796{
638569c5 16797 return cp_parser_type_id_1 (parser, false, false);
75eaa947 16798}
16799
16800static tree cp_parser_template_type_arg (cp_parser *parser)
16801{
1961ca68 16802 tree r;
16803 const char *saved_message = parser->type_definition_forbidden_message;
16804 parser->type_definition_forbidden_message
16805 = G_("types may not be defined in template arguments");
16806 r = cp_parser_type_id_1 (parser, true, false);
16807 parser->type_definition_forbidden_message = saved_message;
16808 return r;
638569c5 16809}
16810
16811static tree cp_parser_trailing_type_id (cp_parser *parser)
16812{
16813 return cp_parser_type_id_1 (parser, false, true);
0a3b29ad 16814}
16815
16816/* Parse a type-specifier-seq.
16817
16818 type-specifier-seq:
16819 type-specifier type-specifier-seq [opt]
16820
16821 GNU extension:
16822
16823 type-specifier-seq:
16824 attributes type-specifier-seq [opt]
16825
c44f7faf 16826 If IS_DECLARATION is true, we are at the start of a "condition" or
16827 exception-declaration, so we might be followed by a declarator-id.
6f74fe3c 16828
638569c5 16829 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16830 i.e. we've just seen "->".
16831
4b9b2871 16832 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
0a3b29ad 16833
4b9b2871 16834static void
16835cp_parser_type_specifier_seq (cp_parser* parser,
c44f7faf 16836 bool is_declaration,
638569c5 16837 bool is_trailing_return,
4b9b2871 16838 cp_decl_specifier_seq *type_specifier_seq)
0a3b29ad 16839{
16840 bool seen_type_specifier = false;
6f74fe3c 16841 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
ad9ae192 16842 cp_token *start_token = NULL;
4b9b2871 16843
16844 /* Clear the TYPE_SPECIFIER_SEQ. */
16845 clear_decl_specs (type_specifier_seq);
0a3b29ad 16846
638569c5 16847 /* In the context of a trailing return type, enum E { } is an
16848 elaborated-type-specifier followed by a function-body, not an
16849 enum-specifier. */
16850 if (is_trailing_return)
16851 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16852
0a3b29ad 16853 /* Parse the type-specifiers and attributes. */
16854 while (true)
16855 {
16856 tree type_specifier;
6f74fe3c 16857 bool is_cv_qualifier;
0a3b29ad 16858
16859 /* Check for attributes first. */
16860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16861 {
207355ad 16862 type_specifier_seq->attributes =
4b9b2871 16863 chainon (type_specifier_seq->attributes,
16864 cp_parser_attributes_opt (parser));
0a3b29ad 16865 continue;
16866 }
16867
ad9ae192 16868 /* record the token of the beginning of the type specifier seq,
16869 for error reporting purposes*/
16870 if (!start_token)
16871 start_token = cp_lexer_peek_token (parser->lexer);
16872
0a3b29ad 16873 /* Look for the type-specifier. */
ccb84981 16874 type_specifier = cp_parser_type_specifier (parser,
6f74fe3c 16875 flags,
4b9b2871 16876 type_specifier_seq,
0a3b29ad 16877 /*is_declaration=*/false,
16878 NULL,
6f74fe3c 16879 &is_cv_qualifier);
16880 if (!type_specifier)
4b9b2871 16881 {
6f74fe3c 16882 /* If the first type-specifier could not be found, this is not a
16883 type-specifier-seq at all. */
16884 if (!seen_type_specifier)
16885 {
16886 cp_parser_error (parser, "expected type-specifier");
16887 type_specifier_seq->type = error_mark_node;
16888 return;
16889 }
16890 /* If subsequent type-specifiers could not be found, the
16891 type-specifier-seq is complete. */
16892 break;
4b9b2871 16893 }
0a3b29ad 16894
0a3b29ad 16895 seen_type_specifier = true;
6f74fe3c 16896 /* The standard says that a condition can be:
16897
653e5405 16898 type-specifier-seq declarator = assignment-expression
9031d10b 16899
6f74fe3c 16900 However, given:
16901
16902 struct S {};
16903 if (int S = ...)
16904
653e5405 16905 we should treat the "S" as a declarator, not as a
16906 type-specifier. The standard doesn't say that explicitly for
16907 type-specifier-seq, but it does say that for
16908 decl-specifier-seq in an ordinary declaration. Perhaps it
16909 would be clearer just to allow a decl-specifier-seq here, and
16910 then add a semantic restriction that if any decl-specifiers
16911 that are not type-specifiers appear, the program is invalid. */
c44f7faf 16912 if (is_declaration && !is_cv_qualifier)
9031d10b 16913 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
0a3b29ad 16914 }
0a3b29ad 16915}
16916
16917/* Parse a parameter-declaration-clause.
16918
16919 parameter-declaration-clause:
16920 parameter-declaration-list [opt] ... [opt]
16921 parameter-declaration-list , ...
16922
3046c0a3 16923 Returns a representation for the parameter declarations. A return
16924 value of NULL indicates a parameter-declaration-clause consisting
16925 only of an ellipsis. */
0a3b29ad 16926
34eac767 16927static tree
45baea8b 16928cp_parser_parameter_declaration_clause (cp_parser* parser)
0a3b29ad 16929{
34eac767 16930 tree parameters;
0a3b29ad 16931 cp_token *token;
16932 bool ellipsis_p;
3046c0a3 16933 bool is_error;
0a3b29ad 16934
16935 /* Peek at the next token. */
16936 token = cp_lexer_peek_token (parser->lexer);
16937 /* Check for trivial parameter-declaration-clauses. */
16938 if (token->type == CPP_ELLIPSIS)
16939 {
16940 /* Consume the `...' token. */
16941 cp_lexer_consume_token (parser->lexer);
34eac767 16942 return NULL_TREE;
0a3b29ad 16943 }
16944 else if (token->type == CPP_CLOSE_PAREN)
16945 /* There are no parameters. */
2bd78947 16946 {
16947#ifndef NO_IMPLICIT_EXTERN_C
16948 if (in_system_header && current_class_type == NULL
16949 && current_lang_name == lang_name_c)
34eac767 16950 return NULL_TREE;
2bd78947 16951 else
16952#endif
34eac767 16953 return void_list_node;
2bd78947 16954 }
0a3b29ad 16955 /* Check for `(void)', too, which is a special case. */
16956 else if (token->keyword == RID_VOID
ccb84981 16957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 16958 == CPP_CLOSE_PAREN))
16959 {
16960 /* Consume the `void' token. */
16961 cp_lexer_consume_token (parser->lexer);
16962 /* There are no parameters. */
34eac767 16963 return void_list_node;
0a3b29ad 16964 }
ccb84981 16965
0a3b29ad 16966 /* Parse the parameter-declaration-list. */
3046c0a3 16967 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
0a3b29ad 16968 /* If a parse error occurred while parsing the
16969 parameter-declaration-list, then the entire
16970 parameter-declaration-clause is erroneous. */
3046c0a3 16971 if (is_error)
16972 return NULL;
0a3b29ad 16973
16974 /* Peek at the next token. */
16975 token = cp_lexer_peek_token (parser->lexer);
16976 /* If it's a `,', the clause should terminate with an ellipsis. */
16977 if (token->type == CPP_COMMA)
16978 {
16979 /* Consume the `,'. */
16980 cp_lexer_consume_token (parser->lexer);
16981 /* Expect an ellipsis. */
ccb84981 16982 ellipsis_p
c247dce0 16983 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
0a3b29ad 16984 }
ccb84981 16985 /* It might also be `...' if the optional trailing `,' was
0a3b29ad 16986 omitted. */
16987 else if (token->type == CPP_ELLIPSIS)
16988 {
16989 /* Consume the `...' token. */
16990 cp_lexer_consume_token (parser->lexer);
16991 /* And remember that we saw it. */
16992 ellipsis_p = true;
16993 }
16994 else
16995 ellipsis_p = false;
16996
16997 /* Finish the parameter list. */
34eac767 16998 if (!ellipsis_p)
16999 parameters = chainon (parameters, void_list_node);
207355ad 17000
3046c0a3 17001 return parameters;
0a3b29ad 17002}
17003
17004/* Parse a parameter-declaration-list.
17005
17006 parameter-declaration-list:
17007 parameter-declaration
17008 parameter-declaration-list , parameter-declaration
17009
17010 Returns a representation of the parameter-declaration-list, as for
17011 cp_parser_parameter_declaration_clause. However, the
3046c0a3 17012 `void_list_node' is never appended to the list. Upon return,
17013 *IS_ERROR will be true iff an error occurred. */
0a3b29ad 17014
34eac767 17015static tree
3046c0a3 17016cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
0a3b29ad 17017{
34eac767 17018 tree parameters = NULL_TREE;
17019 tree *tail = &parameters;
3b289c9c 17020 bool saved_in_unbraced_linkage_specification_p;
32d008d9 17021 int index = 0;
3046c0a3 17022
17023 /* Assume all will go well. */
17024 *is_error = false;
3b289c9c 17025 /* The special considerations that apply to a function within an
17026 unbraced linkage specifications do not apply to the parameters
17027 to the function. */
17028 saved_in_unbraced_linkage_specification_p
17029 = parser->in_unbraced_linkage_specification_p;
17030 parser->in_unbraced_linkage_specification_p = false;
0a3b29ad 17031
17032 /* Look for more parameters. */
17033 while (true)
17034 {
3046c0a3 17035 cp_parameter_declarator *parameter;
34eac767 17036 tree decl = error_mark_node;
b6396a96 17037 bool parenthesized_p = false;
0a3b29ad 17038 /* Parse the parameter. */
ccb84981 17039 parameter
17040 = cp_parser_parameter_declaration (parser,
92b128ed 17041 /*template_parm_p=*/false,
17042 &parenthesized_p);
759fa9c9 17043
34eac767 17044 /* We don't know yet if the enclosing context is deprecated, so wait
17045 and warn in grokparms if appropriate. */
17046 deprecated_state = DEPRECATED_SUPPRESS;
17047
17048 if (parameter)
17049 decl = grokdeclarator (parameter->declarator,
17050 &parameter->decl_specifiers,
17051 PARM,
17052 parameter->default_argument != NULL_TREE,
17053 &parameter->decl_specifiers.attributes);
17054
17055 deprecated_state = DEPRECATED_NORMAL;
17056
755edffd 17057 /* If a parse error occurred parsing the parameter declaration,
0a3b29ad 17058 then the entire parameter-declaration-list is erroneous. */
34eac767 17059 if (decl == error_mark_node)
0a3b29ad 17060 {
3046c0a3 17061 *is_error = true;
34eac767 17062 parameters = error_mark_node;
0a3b29ad 17063 break;
17064 }
34eac767 17065
17066 if (parameter->decl_specifiers.attributes)
17067 cplus_decl_attributes (&decl,
17068 parameter->decl_specifiers.attributes,
17069 0);
17070 if (DECL_NAME (decl))
17071 decl = pushdecl (decl);
17072
32d008d9 17073 if (decl != error_mark_node)
17074 {
17075 retrofit_lang_decl (decl);
17076 DECL_PARM_INDEX (decl) = ++index;
4d7aaf8e 17077 DECL_PARM_LEVEL (decl) = function_parm_depth ();
32d008d9 17078 }
17079
0a3b29ad 17080 /* Add the new parameter to the list. */
34eac767 17081 *tail = build_tree_list (parameter->default_argument, decl);
17082 tail = &TREE_CHAIN (*tail);
0a3b29ad 17083
17084 /* Peek at the next token. */
17085 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
7a4e126b 17086 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17087 /* These are for Objective-C++ */
17088 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17089 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
0a3b29ad 17090 /* The parameter-declaration-list is complete. */
17091 break;
17092 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17093 {
17094 cp_token *token;
17095
17096 /* Peek at the next token. */
17097 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17098 /* If it's an ellipsis, then the list is complete. */
17099 if (token->type == CPP_ELLIPSIS)
17100 break;
17101 /* Otherwise, there must be more parameters. Consume the
17102 `,'. */
17103 cp_lexer_consume_token (parser->lexer);
92b128ed 17104 /* When parsing something like:
17105
653e5405 17106 int i(float f, double d)
ccb84981 17107
653e5405 17108 we can tell after seeing the declaration for "f" that we
92b128ed 17109 are not looking at an initialization of a variable "i",
ccb84981 17110 but rather at the declaration of a function "i".
92b128ed 17111
17112 Due to the fact that the parsing of template arguments
17113 (as specified to a template-id) requires backtracking we
17114 cannot use this technique when inside a template argument
17115 list. */
17116 if (!parser->in_template_argument_list_p
6006bfb6 17117 && !parser->in_type_id_in_expr_p
efcbcf83 17118 && cp_parser_uncommitted_to_tentative_parse_p (parser)
92b128ed 17119 /* However, a parameter-declaration of the form
17120 "foat(f)" (which is a valid declaration of a
17121 parameter "f") can also be interpreted as an
17122 expression (the conversion of "f" to "float"). */
17123 && !parenthesized_p)
17124 cp_parser_commit_to_tentative_parse (parser);
0a3b29ad 17125 }
17126 else
17127 {
a2c5b975 17128 cp_parser_error (parser, "expected %<,%> or %<...%>");
efcbcf83 17129 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
ccb84981 17130 cp_parser_skip_to_closing_parenthesis (parser,
92b128ed 17131 /*recovering=*/true,
78662158 17132 /*or_comma=*/false,
92b128ed 17133 /*consume_paren=*/false);
0a3b29ad 17134 break;
17135 }
17136 }
17137
3b289c9c 17138 parser->in_unbraced_linkage_specification_p
17139 = saved_in_unbraced_linkage_specification_p;
17140
3046c0a3 17141 return parameters;
0a3b29ad 17142}
17143
17144/* Parse a parameter declaration.
17145
17146 parameter-declaration:
d95d815d 17147 decl-specifier-seq ... [opt] declarator
0a3b29ad 17148 decl-specifier-seq declarator = assignment-expression
d95d815d 17149 decl-specifier-seq ... [opt] abstract-declarator [opt]
0a3b29ad 17150 decl-specifier-seq abstract-declarator [opt] = assignment-expression
17151
759fa9c9 17152 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17153 declares a template parameter. (In that case, a non-nested `>'
17154 token encountered during the parsing of the assignment-expression
17155 is not interpreted as a greater-than operator.)
0a3b29ad 17156
3046c0a3 17157 Returns a representation of the parameter, or NULL if an error
17158 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17159 true iff the declarator is of the form "(p)". */
0a3b29ad 17160
3046c0a3 17161static cp_parameter_declarator *
ccb84981 17162cp_parser_parameter_declaration (cp_parser *parser,
92b128ed 17163 bool template_parm_p,
17164 bool *parenthesized_p)
0a3b29ad 17165{
8172be22 17166 int declares_class_or_enum;
4b9b2871 17167 cp_decl_specifier_seq decl_specifiers;
3046c0a3 17168 cp_declarator *declarator;
0a3b29ad 17169 tree default_argument;
ad9ae192 17170 cp_token *token = NULL, *declarator_token_start = NULL;
0a3b29ad 17171 const char *saved_message;
17172
759fa9c9 17173 /* In a template parameter, `>' is not an operator.
17174
17175 [temp.param]
17176
17177 When parsing a default template-argument for a non-type
17178 template-parameter, the first non-nested `>' is taken as the end
17179 of the template parameter-list rather than a greater-than
17180 operator. */
759fa9c9 17181
0a3b29ad 17182 /* Type definitions may not appear in parameter types. */
17183 saved_message = parser->type_definition_forbidden_message;
ccb84981 17184 parser->type_definition_forbidden_message
ca82e026 17185 = G_("types may not be defined in parameter types");
0a3b29ad 17186
17187 /* Parse the declaration-specifiers. */
4b9b2871 17188 cp_parser_decl_specifier_seq (parser,
17189 CP_PARSER_FLAGS_NONE,
17190 &decl_specifiers,
17191 &declares_class_or_enum);
67484828 17192
17193 /* Complain about missing 'typename' or other invalid type names. */
17194 if (!decl_specifiers.any_type_specifiers_p)
17195 cp_parser_parse_and_diagnose_invalid_type_name (parser);
17196
0a3b29ad 17197 /* If an error occurred, there's no reason to attempt to parse the
17198 rest of the declaration. */
17199 if (cp_parser_error_occurred (parser))
17200 {
17201 parser->type_definition_forbidden_message = saved_message;
3046c0a3 17202 return NULL;
0a3b29ad 17203 }
17204
17205 /* Peek at the next token. */
17206 token = cp_lexer_peek_token (parser->lexer);
d95d815d 17207
0a3b29ad 17208 /* If the next token is a `)', `,', `=', `>', or `...', then there
d95d815d 17209 is no declarator. However, when variadic templates are enabled,
17210 there may be a declarator following `...'. */
ccb84981 17211 if (token->type == CPP_CLOSE_PAREN
0a3b29ad 17212 || token->type == CPP_COMMA
17213 || token->type == CPP_EQ
0a3b29ad 17214 || token->type == CPP_GREATER)
92b128ed 17215 {
3046c0a3 17216 declarator = NULL;
92b128ed 17217 if (parenthesized_p)
17218 *parenthesized_p = false;
17219 }
0a3b29ad 17220 /* Otherwise, there should be a declarator. */
17221 else
17222 {
17223 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17224 parser->default_arg_ok_p = false;
ccb84981 17225
78662158 17226 /* After seeing a decl-specifier-seq, if the next token is not a
17227 "(", there is no possibility that the code is a valid
41f2d08e 17228 expression. Therefore, if parsing tentatively, we commit at
17229 this point. */
78662158 17230 if (!parser->in_template_argument_list_p
461ec6e9 17231 /* In an expression context, having seen:
41f2d08e 17232
91f31809 17233 (int((char ...
41f2d08e 17234
17235 we cannot be sure whether we are looking at a
91f31809 17236 function-type (taking a "char" as a parameter) or a cast
17237 of some object of type "char" to "int". */
41f2d08e 17238 && !parser->in_type_id_in_expr_p
efcbcf83 17239 && cp_parser_uncommitted_to_tentative_parse_p (parser)
b25cc8c2 17240 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
78662158 17241 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17242 cp_parser_commit_to_tentative_parse (parser);
17243 /* Parse the declarator. */
ad9ae192 17244 declarator_token_start = token;
0a3b29ad 17245 declarator = cp_parser_declarator (parser,
42bbd0ec 17246 CP_PARSER_DECLARATOR_EITHER,
92b128ed 17247 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 17248 parenthesized_p,
17249 /*member_p=*/false);
0a3b29ad 17250 parser->default_arg_ok_p = saved_default_arg_ok_p;
b5002156 17251 /* After the declarator, allow more attributes. */
4b9b2871 17252 decl_specifiers.attributes
207355ad 17253 = chainon (decl_specifiers.attributes,
4b9b2871 17254 cp_parser_attributes_opt (parser));
0a3b29ad 17255 }
17256
2aedc2ff 17257 /* If the next token is an ellipsis, and we have not seen a
17258 declarator name, and the type of the declarator contains parameter
17259 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17260 a parameter pack expansion expression. Otherwise, leave the
17261 ellipsis for a C-style variadic function. */
d95d815d 17262 token = cp_lexer_peek_token (parser->lexer);
17263 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17264 {
17265 tree type = decl_specifiers.type;
17266
2aedc2ff 17267 if (type && DECL_P (type))
d95d815d 17268 type = TREE_TYPE (type);
17269
2aedc2ff 17270 if (type
17271 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17272 && declarator_can_be_parameter_pack (declarator)
d95d815d 17273 && (!declarator || !declarator->parameter_pack_p)
17274 && uses_parameter_packs (type))
17275 {
2aedc2ff 17276 /* Consume the `...'. */
17277 cp_lexer_consume_token (parser->lexer);
17278 maybe_warn_variadic_templates ();
17279
17280 /* Build a pack expansion type */
17281 if (declarator)
17282 declarator->parameter_pack_p = true;
17283 else
17284 decl_specifiers.type = make_pack_expansion (type);
17285 }
d95d815d 17286 }
17287
42bbd0ec 17288 /* The restriction on defining new types applies only to the type
0a3b29ad 17289 of the parameter, not to the default argument. */
17290 parser->type_definition_forbidden_message = saved_message;
17291
17292 /* If the next token is `=', then process a default argument. */
17293 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17294 {
ce24dc8d 17295 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 17296 /* If we are defining a class, then the tokens that make up the
17297 default argument must be saved and processed later. */
ccb84981 17298 if (!template_parm_p && at_class_scope_p ()
a8b75081 17299 && TYPE_BEING_DEFINED (current_class_type)
17300 && !LAMBDA_TYPE_P (current_class_type))
ce24dc8d 17301 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
0a3b29ad 17302 /* Outside of a class definition, we can just parse the
653e5405 17303 assignment-expression. */
0a3b29ad 17304 else
ce24dc8d 17305 default_argument
17306 = cp_parser_default_argument (parser, template_parm_p);
41341abd 17307
0a3b29ad 17308 if (!parser->default_arg_ok_p)
17309 {
561fec9d 17310 if (flag_permissive)
c3ceba8e 17311 warning (0, "deprecated use of default argument for parameter of non-function");
816786ad 17312 else
17313 {
ccb59bb6 17314 error_at (token->location,
17315 "default arguments are only "
17316 "permitted for function parameters");
816786ad 17317 default_argument = NULL_TREE;
17318 }
0a3b29ad 17319 }
41341abd 17320 else if ((declarator && declarator->parameter_pack_p)
17321 || (decl_specifiers.type
17322 && PACK_EXPANSION_P (decl_specifiers.type)))
17323 {
41341abd 17324 /* Find the name of the parameter pack. */
17325 cp_declarator *id_declarator = declarator;
17326 while (id_declarator && id_declarator->kind != cdk_id)
17327 id_declarator = id_declarator->declarator;
17328
17329 if (id_declarator && id_declarator->kind == cdk_id)
ccb59bb6 17330 error_at (declarator_token_start->location,
ac7549c7 17331 template_parm_p
17332 ? G_("template parameter pack %qD "
17333 "cannot have a default argument")
17334 : G_("parameter pack %qD cannot have "
17335 "a default argument"),
ccb59bb6 17336 id_declarator->u.id.unqualified_name);
41341abd 17337 else
ccb59bb6 17338 error_at (declarator_token_start->location,
ac7549c7 17339 template_parm_p
17340 ? G_("template parameter pack cannot have "
17341 "a default argument")
17342 : G_("parameter pack cannot have a "
17343 "default argument"));
17344
41341abd 17345 default_argument = NULL_TREE;
17346 }
0a3b29ad 17347 }
17348 else
17349 default_argument = NULL_TREE;
ccb84981 17350
4b9b2871 17351 return make_parameter_declarator (&decl_specifiers,
3046c0a3 17352 declarator,
17353 default_argument);
0a3b29ad 17354}
17355
41341abd 17356/* Parse a default argument and return it.
17357
17358 TEMPLATE_PARM_P is true if this is a default argument for a
17359 non-type template parameter. */
17360static tree
17361cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17362{
17363 tree default_argument = NULL_TREE;
17364 bool saved_greater_than_is_operator_p;
17365 bool saved_local_variables_forbidden_p;
3ab40fb9 17366 bool non_constant_p, is_direct_init;
41341abd 17367
17368 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17369 set correctly. */
17370 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17371 parser->greater_than_is_operator_p = !template_parm_p;
17372 /* Local variable names (and the `this' keyword) may not
17373 appear in a default argument. */
17374 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17375 parser->local_variables_forbidden_p = true;
41341abd 17376 /* Parse the assignment-expression. */
17377 if (template_parm_p)
17378 push_deferring_access_checks (dk_no_deferred);
17379 default_argument
3ab40fb9 17380 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
200e747f 17381 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17382 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
41341abd 17383 if (template_parm_p)
17384 pop_deferring_access_checks ();
41341abd 17385 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17386 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17387
17388 return default_argument;
17389}
17390
0a3b29ad 17391/* Parse a function-body.
17392
17393 function-body:
17394 compound_statement */
17395
17396static void
376a817b 17397cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
0a3b29ad 17398{
376a817b 17399 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
0a3b29ad 17400}
17401
17402/* Parse a ctor-initializer-opt followed by a function-body. Return
376a817b 17403 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
17404 is true we are parsing a function-try-block. */
0a3b29ad 17405
17406static bool
376a817b 17407cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
17408 bool in_function_try_block)
0a3b29ad 17409{
ca63c29a 17410 tree body, list;
0a3b29ad 17411 bool ctor_initializer_p;
ca63c29a 17412 const bool check_body_p =
17413 DECL_CONSTRUCTOR_P (current_function_decl)
17414 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17415 tree last = NULL;
0a3b29ad 17416
17417 /* Begin the function body. */
17418 body = begin_function_body ();
17419 /* Parse the optional ctor-initializer. */
17420 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
ca63c29a 17421
17422 /* If we're parsing a constexpr constructor definition, we need
17423 to check that the constructor body is indeed empty. However,
17424 before we get to cp_parser_function_body lot of junk has been
17425 generated, so we can't just check that we have an empty block.
17426 Rather we take a snapshot of the outermost block, and check whether
17427 cp_parser_function_body changed its state. */
17428 if (check_body_p)
17429 {
5770f123 17430 list = cur_stmt_list;
17431 if (STATEMENT_LIST_TAIL (list))
ca63c29a 17432 last = STATEMENT_LIST_TAIL (list)->stmt;
17433 }
0a3b29ad 17434 /* Parse the function-body. */
376a817b 17435 cp_parser_function_body (parser, in_function_try_block);
fdf548d1 17436 if (check_body_p)
17437 check_constexpr_ctor_body (last, list);
0a3b29ad 17438 /* Finish the function body. */
17439 finish_function_body (body);
17440
17441 return ctor_initializer_p;
17442}
17443
17444/* Parse an initializer.
17445
17446 initializer:
17447 = initializer-clause
ccb84981 17448 ( expression-list )
0a3b29ad 17449
e4bc96e2 17450 Returns an expression representing the initializer. If no
ccb84981 17451 initializer is present, NULL_TREE is returned.
0a3b29ad 17452
f82f1250 17453 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17454 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17455 set to TRUE if there is no initializer present. If there is an
878870b4 17456 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17457 is set to true; otherwise it is set to false. */
0a3b29ad 17458
17459static tree
f82f1250 17460cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
878870b4 17461 bool* non_constant_p)
0a3b29ad 17462{
17463 cp_token *token;
17464 tree init;
17465
17466 /* Peek at the next token. */
17467 token = cp_lexer_peek_token (parser->lexer);
17468
17469 /* Let our caller know whether or not this initializer was
17470 parenthesized. */
f82f1250 17471 *is_direct_init = (token->type != CPP_EQ);
878870b4 17472 /* Assume that the initializer is constant. */
17473 *non_constant_p = false;
0a3b29ad 17474
17475 if (token->type == CPP_EQ)
17476 {
17477 /* Consume the `='. */
17478 cp_lexer_consume_token (parser->lexer);
17479 /* Parse the initializer-clause. */
878870b4 17480 init = cp_parser_initializer_clause (parser, non_constant_p);
0a3b29ad 17481 }
17482 else if (token->type == CPP_OPEN_PAREN)
f352a3fb 17483 {
17484 VEC(tree,gc) *vec;
33199a81 17485 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 17486 /*cast_p=*/false,
17487 /*allow_expansion_p=*/true,
17488 non_constant_p);
17489 if (vec == NULL)
17490 return error_mark_node;
17491 init = build_tree_list_vec (vec);
17492 release_tree_vector (vec);
17493 }
f82f1250 17494 else if (token->type == CPP_OPEN_BRACE)
17495 {
bf8d19fe 17496 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 17497 init = cp_parser_braced_list (parser, non_constant_p);
17498 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17499 }
0a3b29ad 17500 else
17501 {
17502 /* Anything else is an error. */
17503 cp_parser_error (parser, "expected initializer");
17504 init = error_mark_node;
17505 }
17506
17507 return init;
17508}
17509
ccb84981 17510/* Parse an initializer-clause.
0a3b29ad 17511
17512 initializer-clause:
17513 assignment-expression
f82f1250 17514 braced-init-list
0a3b29ad 17515
ccb84981 17516 Returns an expression representing the initializer.
0a3b29ad 17517
17518 If the `assignment-expression' production is used the value
ccb84981 17519 returned is simply a representation for the expression.
0a3b29ad 17520
f82f1250 17521 Otherwise, calls cp_parser_braced_list. */
0a3b29ad 17522
17523static tree
878870b4 17524cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
0a3b29ad 17525{
17526 tree initializer;
17527
53073dc9 17528 /* Assume the expression is constant. */
17529 *non_constant_p = false;
17530
0a3b29ad 17531 /* If it is not a `{', then we are looking at an
17532 assignment-expression. */
17533 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
f352cc1d 17534 {
207355ad 17535 initializer
f352cc1d 17536 = cp_parser_constant_expression (parser,
17537 /*allow_non_constant_p=*/true,
17538 non_constant_p);
f352cc1d 17539 }
0a3b29ad 17540 else
f82f1250 17541 initializer = cp_parser_braced_list (parser, non_constant_p);
17542
17543 return initializer;
17544}
17545
17546/* Parse a brace-enclosed initializer list.
17547
17548 braced-init-list:
17549 { initializer-list , [opt] }
17550 { }
17551
17552 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17553 the elements of the initializer-list (or NULL, if the last
17554 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17555 NULL_TREE. There is no way to detect whether or not the optional
17556 trailing `,' was provided. NON_CONSTANT_P is as for
17557 cp_parser_initializer. */
17558
17559static tree
17560cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17561{
17562 tree initializer;
17563
17564 /* Consume the `{' token. */
17565 cp_lexer_consume_token (parser->lexer);
17566 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17567 initializer = make_node (CONSTRUCTOR);
17568 /* If it's not a `}', then there is a non-trivial initializer. */
17569 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
0a3b29ad 17570 {
f82f1250 17571 /* Parse the initializer list. */
17572 CONSTRUCTOR_ELTS (initializer)
17573 = cp_parser_initializer_list (parser, non_constant_p);
17574 /* A trailing `,' token is allowed. */
17575 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17576 cp_lexer_consume_token (parser->lexer);
0a3b29ad 17577 }
f82f1250 17578 /* Now, there should be a trailing `}'. */
c247dce0 17579 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
f82f1250 17580 TREE_TYPE (initializer) = init_list_type_node;
0a3b29ad 17581 return initializer;
17582}
17583
17584/* Parse an initializer-list.
17585
17586 initializer-list:
d95d815d 17587 initializer-clause ... [opt]
17588 initializer-list , initializer-clause ... [opt]
0a3b29ad 17589
17590 GNU Extension:
ccb84981 17591
0a3b29ad 17592 initializer-list:
79e5afae 17593 designation initializer-clause ...[opt]
17594 initializer-list , designation initializer-clause ...[opt]
17595
17596 designation:
17597 . identifier =
17598 identifier :
17599 [ constant-expression ] =
0a3b29ad 17600
c75b4594 17601 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17602 for the initializer. If the INDEX of the elt is non-NULL, it is the
878870b4 17603 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17604 as for cp_parser_initializer. */
0a3b29ad 17605
c75b4594 17606static VEC(constructor_elt,gc) *
878870b4 17607cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
0a3b29ad 17608{
c75b4594 17609 VEC(constructor_elt,gc) *v = NULL;
0a3b29ad 17610
878870b4 17611 /* Assume all of the expressions are constant. */
17612 *non_constant_p = false;
17613
0a3b29ad 17614 /* Parse the rest of the list. */
17615 while (true)
17616 {
17617 cp_token *token;
79e5afae 17618 tree designator;
0a3b29ad 17619 tree initializer;
878870b4 17620 bool clause_non_constant_p;
17621
0a3b29ad 17622 /* If the next token is an identifier and the following one is a
17623 colon, we are looking at the GNU designated-initializer
17624 syntax. */
17625 if (cp_parser_allow_gnu_extensions_p (parser)
17626 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17627 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17628 {
1d8baa0e 17629 /* Warn the user that they are using an extension. */
29438999 17630 pedwarn (input_location, OPT_Wpedantic,
8864917d 17631 "ISO C++ does not allow designated initializers");
0a3b29ad 17632 /* Consume the identifier. */
79e5afae 17633 designator = cp_lexer_consume_token (parser->lexer)->u.value;
0a3b29ad 17634 /* Consume the `:'. */
17635 cp_lexer_consume_token (parser->lexer);
17636 }
79e5afae 17637 /* Also handle the C99 syntax, '. id ='. */
17638 else if (cp_parser_allow_gnu_extensions_p (parser)
17639 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17640 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17641 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17642 {
17643 /* Warn the user that they are using an extension. */
29438999 17644 pedwarn (input_location, OPT_Wpedantic,
79e5afae 17645 "ISO C++ does not allow C99 designated initializers");
17646 /* Consume the `.'. */
17647 cp_lexer_consume_token (parser->lexer);
17648 /* Consume the identifier. */
17649 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17650 /* Consume the `='. */
17651 cp_lexer_consume_token (parser->lexer);
17652 }
17653 /* Also handle C99 array designators, '[ const ] ='. */
17654 else if (cp_parser_allow_gnu_extensions_p (parser)
17655 && !c_dialect_objc ()
17656 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17657 {
1d422816 17658 /* In C++11, [ could start a lambda-introducer. */
17659 cp_parser_parse_tentatively (parser);
79e5afae 17660 cp_lexer_consume_token (parser->lexer);
17661 designator = cp_parser_constant_expression (parser, false, NULL);
17662 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17663 cp_parser_require (parser, CPP_EQ, RT_EQ);
222ad45b 17664 if (!cp_parser_parse_definitely (parser))
17665 designator = NULL_TREE;
79e5afae 17666 }
0a3b29ad 17667 else
79e5afae 17668 designator = NULL_TREE;
0a3b29ad 17669
17670 /* Parse the initializer. */
ccb84981 17671 initializer = cp_parser_initializer_clause (parser,
878870b4 17672 &clause_non_constant_p);
17673 /* If any clause is non-constant, so is the entire initializer. */
17674 if (clause_non_constant_p)
17675 *non_constant_p = true;
c75b4594 17676
d95d815d 17677 /* If we have an ellipsis, this is an initializer pack
17678 expansion. */
17679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17680 {
17681 /* Consume the `...'. */
17682 cp_lexer_consume_token (parser->lexer);
17683
17684 /* Turn the initializer into an initializer expansion. */
17685 initializer = make_pack_expansion (initializer);
17686 }
17687
c75b4594 17688 /* Add it to the vector. */
79e5afae 17689 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
0a3b29ad 17690
17691 /* If the next token is not a comma, we have reached the end of
17692 the list. */
17693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17694 break;
17695
17696 /* Peek at the next token. */
17697 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17698 /* If the next token is a `}', then we're still done. An
17699 initializer-clause can have a trailing `,' after the
17700 initializer-list and before the closing `}'. */
17701 if (token->type == CPP_CLOSE_BRACE)
17702 break;
17703
17704 /* Consume the `,' token. */
17705 cp_lexer_consume_token (parser->lexer);
17706 }
17707
c75b4594 17708 return v;
0a3b29ad 17709}
17710
17711/* Classes [gram.class] */
17712
17713/* Parse a class-name.
17714
17715 class-name:
17716 identifier
17717 template-id
17718
17719 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17720 to indicate that names looked up in dependent types should be
17721 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17722 keyword has been used to indicate that the name that appears next
e2ae55f2 17723 is a template. TAG_TYPE indicates the explicit tag given before
17724 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17725 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17726 is the class being defined in a class-head.
0a3b29ad 17727
17728 Returns the TYPE_DECL representing the class. */
17729
17730static tree
ccb84981 17731cp_parser_class_name (cp_parser *parser,
17732 bool typename_keyword_p,
17733 bool template_keyword_p,
e2ae55f2 17734 enum tag_types tag_type,
0a3b29ad 17735 bool check_dependency_p,
3d0f901b 17736 bool class_head_p,
17737 bool is_declaration)
0a3b29ad 17738{
17739 tree decl;
17740 tree scope;
17741 bool typename_p;
2c593bd0 17742 cp_token *token;
a6c68ee8 17743 tree identifier = NULL_TREE;
2c593bd0 17744
17745 /* All class-names start with an identifier. */
17746 token = cp_lexer_peek_token (parser->lexer);
17747 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17748 {
17749 cp_parser_error (parser, "expected class-name");
17750 return error_mark_node;
17751 }
ccb84981 17752
0a3b29ad 17753 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17754 to a template-id, so we save it here. */
17755 scope = parser->scope;
1e9847d8 17756 if (scope == error_mark_node)
17757 return error_mark_node;
ccb84981 17758
0a3b29ad 17759 /* Any name names a type if we're following the `typename' keyword
17760 in a qualified name where the enclosing scope is type-dependent. */
17761 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
7e9a6a16 17762 && dependent_type_p (scope));
2c593bd0 17763 /* Handle the common case (an identifier, but not a template-id)
17764 efficiently. */
ccb84981 17765 if (token->type == CPP_NAME
c8d5ab79 17766 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
0a3b29ad 17767 {
b62240d5 17768 cp_token *identifier_token;
b62240d5 17769 bool ambiguous_p;
0a3b29ad 17770
17771 /* Look for the identifier. */
b62240d5 17772 identifier_token = cp_lexer_peek_token (parser->lexer);
17773 ambiguous_p = identifier_token->ambiguous_p;
0a3b29ad 17774 identifier = cp_parser_identifier (parser);
17775 /* If the next token isn't an identifier, we are certainly not
17776 looking at a class-name. */
17777 if (identifier == error_mark_node)
17778 decl = error_mark_node;
17779 /* If we know this is a type-name, there's no need to look it
17780 up. */
17781 else if (typename_p)
17782 decl = identifier;
17783 else
17784 {
b62240d5 17785 tree ambiguous_decls;
17786 /* If we already know that this lookup is ambiguous, then
17787 we've already issued an error message; there's no reason
17788 to check again. */
17789 if (ambiguous_p)
17790 {
17791 cp_parser_simulate_error (parser);
17792 return error_mark_node;
17793 }
0a3b29ad 17794 /* If the next token is a `::', then the name must be a type
17795 name.
17796
17797 [basic.lookup.qual]
17798
17799 During the lookup for a name preceding the :: scope
17800 resolution operator, object, function, and enumerator
17801 names are ignored. */
17802 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
e2ae55f2 17803 tag_type = typename_type;
0a3b29ad 17804 /* Look up the name. */
ccb84981 17805 decl = cp_parser_lookup_name (parser, identifier,
e2ae55f2 17806 tag_type,
c3b9e457 17807 /*is_template=*/false,
6fc758aa 17808 /*is_namespace=*/false,
2cdbcd51 17809 check_dependency_p,
ad9ae192 17810 &ambiguous_decls,
17811 identifier_token->location);
b62240d5 17812 if (ambiguous_decls)
17813 {
b62240d5 17814 if (cp_parser_parsing_tentatively (parser))
4272a2e8 17815 cp_parser_simulate_error (parser);
b62240d5 17816 return error_mark_node;
17817 }
0a3b29ad 17818 }
17819 }
2c593bd0 17820 else
17821 {
17822 /* Try a template-id. */
17823 decl = cp_parser_template_id (parser, template_keyword_p,
3d0f901b 17824 check_dependency_p,
17825 is_declaration);
2c593bd0 17826 if (decl == error_mark_node)
17827 return error_mark_node;
17828 }
0a3b29ad 17829
17830 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17831
17832 /* If this is a typename, create a TYPENAME_TYPE. */
17833 if (typename_p && decl != error_mark_node)
50dad21f 17834 {
c31fdaf6 17835 decl = make_typename_type (scope, decl, typename_type,
074ab442 17836 /*complain=*/tf_error);
50dad21f 17837 if (decl != error_mark_node)
17838 decl = TYPE_NAME (decl);
17839 }
0a3b29ad 17840
83ae9f05 17841 decl = strip_using_decl (decl);
17842
0a3b29ad 17843 /* Check to see that it is really the name of a class. */
ccb84981 17844 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
0a3b29ad 17845 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17846 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17847 /* Situations like this:
17848
17849 template <typename T> struct A {
ccb84981 17850 typename T::template X<int>::I i;
0a3b29ad 17851 };
17852
17853 are problematic. Is `T::template X<int>' a class-name? The
17854 standard does not seem to be definitive, but there is no other
17855 valid interpretation of the following `::'. Therefore, those
17856 names are considered class-names. */
0a3b29ad 17857 {
64b06433 17858 decl = make_typename_type (scope, decl, tag_type, tf_error);
17859 if (decl != error_mark_node)
17860 decl = TYPE_NAME (decl);
0a3b29ad 17861 }
64b06433 17862 else if (TREE_CODE (decl) != TYPE_DECL
17863 || TREE_TYPE (decl) == error_mark_node
b0d0931f 17864 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
79408174 17865 /* In Objective-C 2.0, a classname followed by '.' starts a
17866 dot-syntax expression, and it's not a type-name. */
17867 || (c_dialect_objc ()
17868 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17869 && objc_is_class_name (decl)))
64b06433 17870 decl = error_mark_node;
17871
17872 if (decl == error_mark_node)
17873 cp_parser_error (parser, "expected class-name");
a6c68ee8 17874 else if (identifier && !parser->scope)
17875 maybe_note_name_used_in_class (identifier, decl);
0a3b29ad 17876
17877 return decl;
17878}
17879
17880/* Parse a class-specifier.
17881
17882 class-specifier:
17883 class-head { member-specification [opt] }
17884
17885 Returns the TREE_TYPE representing the class. */
17886
17887static tree
6198e8f6 17888cp_parser_class_specifier_1 (cp_parser* parser)
0a3b29ad 17889{
0a3b29ad 17890 tree type;
4ee9c684 17891 tree attributes = NULL_TREE;
0a3b29ad 17892 bool nested_name_specifier_p;
0a3b29ad 17893 unsigned saved_num_template_parameter_lists;
0aeb1cc5 17894 bool saved_in_function_body;
b1bd5bd7 17895 unsigned char in_statement;
17896 bool in_switch_statement_p;
f0475530 17897 bool saved_in_unbraced_linkage_specification_p;
352baa70 17898 tree old_scope = NULL_TREE;
534d03ee 17899 tree scope = NULL_TREE;
1a9b4c25 17900 tree bases;
a0499cf2 17901 cp_token *closing_brace;
0a3b29ad 17902
4f62c42e 17903 push_deferring_access_checks (dk_no_deferred);
9b57b06b 17904
0a3b29ad 17905 /* Parse the class-head. */
17906 type = cp_parser_class_head (parser,
fb3e3237 17907 &nested_name_specifier_p,
1a9b4c25 17908 &attributes,
17909 &bases);
0a3b29ad 17910 /* If the class-head was a semantic disaster, skip the entire body
17911 of the class. */
17912 if (!type)
17913 {
17914 cp_parser_skip_to_end_of_block_or_statement (parser);
9b57b06b 17915 pop_deferring_access_checks ();
0a3b29ad 17916 return error_mark_node;
17917 }
9b57b06b 17918
0a3b29ad 17919 /* Look for the `{'. */
c247dce0 17920 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9b57b06b 17921 {
17922 pop_deferring_access_checks ();
17923 return error_mark_node;
17924 }
17925
1a9b4c25 17926 /* Process the base classes. If they're invalid, skip the
17927 entire class body. */
17928 if (!xref_basetypes (type, bases))
17929 {
1a9b4c25 17930 /* Consuming the closing brace yields better error messages
17931 later on. */
9aad947b 17932 if (cp_parser_skip_to_closing_brace (parser))
17933 cp_lexer_consume_token (parser->lexer);
1a9b4c25 17934 pop_deferring_access_checks ();
17935 return error_mark_node;
17936 }
17937
0a3b29ad 17938 /* Issue an error message if type-definitions are forbidden here. */
17939 cp_parser_check_type_definition (parser);
17940 /* Remember that we are defining one more class. */
17941 ++parser->num_classes_being_defined;
17942 /* Inside the class, surrounding template-parameter-lists do not
17943 apply. */
ccb84981 17944 saved_num_template_parameter_lists
17945 = parser->num_template_parameter_lists;
0a3b29ad 17946 parser->num_template_parameter_lists = 0;
0aeb1cc5 17947 /* We are not in a function body. */
17948 saved_in_function_body = parser->in_function_body;
17949 parser->in_function_body = false;
b1bd5bd7 17950 /* Or in a loop. */
17951 in_statement = parser->in_statement;
17952 parser->in_statement = 0;
17953 /* Or in a switch. */
17954 in_switch_statement_p = parser->in_switch_statement_p;
17955 parser->in_switch_statement_p = false;
f0475530 17956 /* We are not immediately inside an extern "lang" block. */
17957 saved_in_unbraced_linkage_specification_p
17958 = parser->in_unbraced_linkage_specification_p;
17959 parser->in_unbraced_linkage_specification_p = false;
4cab8273 17960
0a3b29ad 17961 /* Start the class. */
4ccffa39 17962 if (nested_name_specifier_p)
534d03ee 17963 {
17964 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
352baa70 17965 old_scope = push_inner_scope (scope);
534d03ee 17966 }
4a2849cb 17967 type = begin_class_definition (type, attributes);
207355ad 17968
0a3b29ad 17969 if (type == error_mark_node)
6beb3f76 17970 /* If the type is erroneous, skip the entire body of the class. */
0a3b29ad 17971 cp_parser_skip_to_closing_brace (parser);
17972 else
17973 /* Parse the member-specification. */
17974 cp_parser_member_specification_opt (parser);
207355ad 17975
0a3b29ad 17976 /* Look for the trailing `}'. */
a0499cf2 17977 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
fb3e3237 17978 /* Look for trailing attributes to apply to this class. */
0a3b29ad 17979 if (cp_parser_allow_gnu_extensions_p (parser))
4a2849cb 17980 attributes = cp_parser_attributes_opt (parser);
fb3e3237 17981 if (type != error_mark_node)
17982 type = finish_struct (type, attributes);
352baa70 17983 if (nested_name_specifier_p)
17984 pop_inner_scope (old_scope, scope);
a9ffdd35 17985
17986 /* We've finished a type definition. Check for the common syntax
17987 error of forgetting a semicolon after the definition. We need to
17988 be careful, as we can't just check for not-a-semicolon and be done
17989 with it; the user might have typed:
17990
17991 class X { } c = ...;
17992 class X { } *p = ...;
17993
17994 and so forth. Instead, enumerate all the possible tokens that
17995 might follow this production; if we don't see one of them, then
17996 complain and silently insert the semicolon. */
17997 {
17998 cp_token *token = cp_lexer_peek_token (parser->lexer);
17999 bool want_semicolon = true;
18000
18001 switch (token->type)
18002 {
18003 case CPP_NAME:
18004 case CPP_SEMICOLON:
18005 case CPP_MULT:
18006 case CPP_AND:
18007 case CPP_OPEN_PAREN:
18008 case CPP_CLOSE_PAREN:
18009 case CPP_COMMA:
18010 want_semicolon = false;
18011 break;
18012
18013 /* While it's legal for type qualifiers and storage class
18014 specifiers to follow type definitions in the grammar, only
18015 compiler testsuites contain code like that. Assume that if
18016 we see such code, then what we're really seeing is a case
18017 like:
18018
18019 class X { }
18020 const <type> var = ...;
18021
18022 or
18023
18024 class Y { }
18025 static <type> func (...) ...
18026
18027 i.e. the qualifier or specifier applies to the next
18028 declaration. To do so, however, we need to look ahead one
18029 more token to see if *that* token is a type specifier.
18030
18031 This code could be improved to handle:
18032
18033 class Z { }
18034 static const <type> var = ...; */
18035 case CPP_KEYWORD:
fad3f658 18036 if (keyword_is_decl_specifier (token->keyword))
a9ffdd35 18037 {
18038 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18039
fad3f658 18040 /* Handling user-defined types here would be nice, but very
18041 tricky. */
18042 want_semicolon
18043 = (lookahead->type == CPP_KEYWORD
18044 && keyword_begins_type_specifier (lookahead->keyword));
a9ffdd35 18045 }
18046 break;
18047 default:
18048 break;
18049 }
18050
18143be0 18051 /* If we don't have a type, then something is very wrong and we
a0499cf2 18052 shouldn't try to do anything clever. Likewise for not seeing the
18053 closing brace. */
18054 if (closing_brace && TYPE_P (type) && want_semicolon)
a9ffdd35 18055 {
18056 cp_token_position prev
18057 = cp_lexer_previous_token_position (parser->lexer);
18058 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18059 location_t loc = prev_token->location;
18060
18061 if (CLASSTYPE_DECLARED_CLASS (type))
18062 error_at (loc, "expected %<;%> after class definition");
18063 else if (TREE_CODE (type) == RECORD_TYPE)
18064 error_at (loc, "expected %<;%> after struct definition");
18065 else if (TREE_CODE (type) == UNION_TYPE)
18066 error_at (loc, "expected %<;%> after union definition");
18067 else
18068 gcc_unreachable ();
18069
18070 /* Unget one token and smash it to look as though we encountered
18071 a semicolon in the input stream. */
18072 cp_lexer_set_token_position (parser->lexer, prev);
18073 token = cp_lexer_peek_token (parser->lexer);
18074 token->type = CPP_SEMICOLON;
18075 token->keyword = RID_MAX;
18076 }
18077 }
18078
0a3b29ad 18079 /* If this class is not itself within the scope of another class,
18080 then we need to parse the bodies of all of the queued function
18081 definitions. Note that the queued functions defined in a class
18082 are not always processed immediately following the
18083 class-specifier for that class. Consider:
18084
18085 struct A {
653e5405 18086 struct B { void f() { sizeof (A); } };
0a3b29ad 18087 };
18088
18089 If `f' were processed before the processing of `A' were
18090 completed, there would be no way to compute the size of `A'.
18091 Note that the nesting we are interested in here is lexical --
18092 not the semantic nesting given by TYPE_CONTEXT. In particular,
18093 for:
18094
18095 struct A { struct B; };
18096 struct A::B { void f() { } };
18097
18098 there is no need to delay the parsing of `A::B::f'. */
ccb84981 18099 if (--parser->num_classes_being_defined == 0)
0a3b29ad 18100 {
3ab40fb9 18101 tree decl;
7f602bca 18102 tree class_type = NULL_TREE;
18103 tree pushed_scope = NULL_TREE;
9177da82 18104 unsigned ix;
18105 cp_default_arg_entry *e;
aa4a12a7 18106 tree save_ccp, save_ccr;
074ab442 18107
af128372 18108 /* In a first pass, parse default arguments to the functions.
18109 Then, in a second pass, parse the bodies of the functions.
18110 This two-phased approach handles cases like:
ccb84981 18111
18112 struct S {
653e5405 18113 void f() { g(); }
18114 void g(int i = 3);
18115 };
af128372 18116
653e5405 18117 */
48148244 18118 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18119 ix, e)
af128372 18120 {
3ab40fb9 18121 decl = e->decl;
af128372 18122 /* If there are default arguments that have not yet been processed,
18123 take care of them now. */
9177da82 18124 if (class_type != e->class_type)
93c149df 18125 {
7f602bca 18126 if (pushed_scope)
18127 pop_scope (pushed_scope);
9177da82 18128 class_type = e->class_type;
7f602bca 18129 pushed_scope = push_scope (class_type);
93c149df 18130 }
18131 /* Make sure that any template parameters are in scope. */
3ab40fb9 18132 maybe_begin_member_template_processing (decl);
93c149df 18133 /* Parse the default argument expressions. */
3ab40fb9 18134 cp_parser_late_parsing_default_args (parser, decl);
af128372 18135 /* Remove any template parameters from the symbol table. */
18136 maybe_end_member_template_processing ();
18137 }
3ab40fb9 18138 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18139 /* Now parse any NSDMIs. */
aa4a12a7 18140 save_ccp = current_class_ptr;
18141 save_ccr = current_class_ref;
3ab40fb9 18142 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18143 {
18144 if (class_type != DECL_CONTEXT (decl))
18145 {
18146 if (pushed_scope)
18147 pop_scope (pushed_scope);
18148 class_type = DECL_CONTEXT (decl);
18149 pushed_scope = push_scope (class_type);
18150 }
aa4a12a7 18151 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
3ab40fb9 18152 cp_parser_late_parsing_nsdmi (parser, decl);
18153 }
18154 VEC_truncate (tree, unparsed_nsdmis, 0);
aa4a12a7 18155 current_class_ptr = save_ccp;
18156 current_class_ref = save_ccr;
7f602bca 18157 if (pushed_scope)
18158 pop_scope (pushed_scope);
af128372 18159 /* Now parse the body of the functions. */
3ab40fb9 18160 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18161 cp_parser_late_parsing_for_member (parser, decl);
9177da82 18162 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
0a3b29ad 18163 }
18164
18165 /* Put back any saved access checks. */
9b57b06b 18166 pop_deferring_access_checks ();
0a3b29ad 18167
0aeb1cc5 18168 /* Restore saved state. */
b1bd5bd7 18169 parser->in_switch_statement_p = in_switch_statement_p;
18170 parser->in_statement = in_statement;
0aeb1cc5 18171 parser->in_function_body = saved_in_function_body;
0a3b29ad 18172 parser->num_template_parameter_lists
18173 = saved_num_template_parameter_lists;
f0475530 18174 parser->in_unbraced_linkage_specification_p
18175 = saved_in_unbraced_linkage_specification_p;
0a3b29ad 18176
18177 return type;
18178}
18179
6198e8f6 18180static tree
18181cp_parser_class_specifier (cp_parser* parser)
18182{
18183 tree ret;
18184 timevar_push (TV_PARSE_STRUCT);
18185 ret = cp_parser_class_specifier_1 (parser);
18186 timevar_pop (TV_PARSE_STRUCT);
18187 return ret;
18188}
18189
0a3b29ad 18190/* Parse a class-head.
18191
18192 class-head:
18193 class-key identifier [opt] base-clause [opt]
6a8b7c0d 18194 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
ccb84981 18195 class-key nested-name-specifier [opt] template-id
18196 base-clause [opt]
0a3b29ad 18197
6a8b7c0d 18198 class-virt-specifier:
18199 final
18200
0a3b29ad 18201 GNU Extensions:
18202 class-key attributes identifier [opt] base-clause [opt]
18203 class-key attributes nested-name-specifier identifier base-clause [opt]
ccb84981 18204 class-key attributes nested-name-specifier [opt] template-id
18205 base-clause [opt]
0a3b29ad 18206
4595a3b3 18207 Upon return BASES is initialized to the list of base classes (or
18208 NULL, if there are none) in the same form returned by
18209 cp_parser_base_clause.
18210
0a3b29ad 18211 Returns the TYPE of the indicated class. Sets
18212 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18213 involving a nested-name-specifier was used, and FALSE otherwise.
0a3b29ad 18214
8d52e9a6 18215 Returns error_mark_node if this is not a class-head.
9031d10b 18216
0a3b29ad 18217 Returns NULL_TREE if the class-head is syntactically valid, but
18218 semantically invalid in a way that means we should skip the entire
18219 body of the class. */
18220
18221static tree
ccb84981 18222cp_parser_class_head (cp_parser* parser,
fb3e3237 18223 bool* nested_name_specifier_p,
1a9b4c25 18224 tree *attributes_p,
18225 tree *bases)
0a3b29ad 18226{
0a3b29ad 18227 tree nested_name_specifier;
18228 enum tag_types class_key;
18229 tree id = NULL_TREE;
18230 tree type = NULL_TREE;
18231 tree attributes;
6a8b7c0d 18232 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
0a3b29ad 18233 bool template_id_p = false;
18234 bool qualified_p = false;
18235 bool invalid_nested_name_p = false;
e16b1a13 18236 bool invalid_explicit_specialization_p = false;
1e838f42 18237 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7f602bca 18238 tree pushed_scope = NULL_TREE;
0a3b29ad 18239 unsigned num_templates;
ad9ae192 18240 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
0a3b29ad 18241 /* Assume no nested-name-specifier will be present. */
18242 *nested_name_specifier_p = false;
18243 /* Assume no template parameter lists will be used in defining the
18244 type. */
18245 num_templates = 0;
1e838f42 18246 parser->colon_corrects_to_scope_p = false;
0a3b29ad 18247
4595a3b3 18248 *bases = NULL_TREE;
18249
0a3b29ad 18250 /* Look for the class-key. */
18251 class_key = cp_parser_class_key (parser);
18252 if (class_key == none_type)
18253 return error_mark_node;
18254
18255 /* Parse the attributes. */
18256 attributes = cp_parser_attributes_opt (parser);
18257
18258 /* If the next token is `::', that is invalid -- but sometimes
18259 people do try to write:
18260
ccb84981 18261 struct ::S {};
0a3b29ad 18262
18263 Handle this gracefully by accepting the extra qualifier, and then
18264 issuing an error about it later if this really is a
b3c48b5d 18265 class-head. If it turns out just to be an elaborated type
0a3b29ad 18266 specifier, remain silent. */
130bb1d4 18267 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
0a3b29ad 18268 qualified_p = true;
18269
4f62c42e 18270 push_deferring_access_checks (dk_no_check);
18271
0a3b29ad 18272 /* Determine the name of the class. Begin by looking for an
18273 optional nested-name-specifier. */
ad9ae192 18274 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
ccb84981 18275 nested_name_specifier
0a3b29ad 18276 = cp_parser_nested_name_specifier_opt (parser,
18277 /*typename_keyword_p=*/false,
1bd3a9f9 18278 /*check_dependency_p=*/false,
3d0f901b 18279 /*type_p=*/false,
18280 /*is_declaration=*/false);
0a3b29ad 18281 /* If there was a nested-name-specifier, then there *must* be an
18282 identifier. */
18283 if (nested_name_specifier)
18284 {
ad9ae192 18285 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 18286 /* Although the grammar says `identifier', it really means
18287 `class-name' or `template-name'. You are only allowed to
18288 define a class that has already been declared with this
ccb84981 18289 syntax.
0a3b29ad 18290
b4f2a576 18291 The proposed resolution for Core Issue 180 says that wherever
0a3b29ad 18292 you see `class T::X' you should treat `X' as a type-name.
ccb84981 18293
0a3b29ad 18294 It is OK to define an inaccessible class; for example:
ccb84981 18295
653e5405 18296 class A { class B; };
18297 class A::B {};
ccb84981 18298
653e5405 18299 We do not know if we will see a class-name, or a
0a3b29ad 18300 template-name. We look for a class-name first, in case the
18301 class-name is a template-id; if we looked for the
18302 template-name first we would stop after the template-name. */
18303 cp_parser_parse_tentatively (parser);
18304 type = cp_parser_class_name (parser,
18305 /*typename_keyword_p=*/false,
18306 /*template_keyword_p=*/false,
e2ae55f2 18307 class_type,
0a3b29ad 18308 /*check_dependency_p=*/false,
3d0f901b 18309 /*class_head_p=*/true,
18310 /*is_declaration=*/false);
0a3b29ad 18311 /* If that didn't work, ignore the nested-name-specifier. */
18312 if (!cp_parser_parse_definitely (parser))
18313 {
18314 invalid_nested_name_p = true;
eef0ab03 18315 type_start_token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 18316 id = cp_parser_identifier (parser);
18317 if (id == error_mark_node)
18318 id = NULL_TREE;
18319 }
18320 /* If we could not find a corresponding TYPE, treat this
18321 declaration like an unqualified declaration. */
18322 if (type == error_mark_node)
18323 nested_name_specifier = NULL_TREE;
18324 /* Otherwise, count the number of templates used in TYPE and its
18325 containing scopes. */
ccb84981 18326 else
0a3b29ad 18327 {
18328 tree scope;
18329
ccb84981 18330 for (scope = TREE_TYPE (type);
0a3b29ad 18331 scope && TREE_CODE (scope) != NAMESPACE_DECL;
ccb84981 18332 scope = (TYPE_P (scope)
0a3b29ad 18333 ? TYPE_CONTEXT (scope)
ccb84981 18334 : DECL_CONTEXT (scope)))
18335 if (TYPE_P (scope)
0a3b29ad 18336 && CLASS_TYPE_P (scope)
18337 && CLASSTYPE_TEMPLATE_INFO (scope)
b3c48b5d 18338 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18339 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
0a3b29ad 18340 ++num_templates;
18341 }
18342 }
18343 /* Otherwise, the identifier is optional. */
18344 else
18345 {
18346 /* We don't know whether what comes next is a template-id,
18347 an identifier, or nothing at all. */
18348 cp_parser_parse_tentatively (parser);
18349 /* Check for a template-id. */
ad9ae192 18350 type_start_token = cp_lexer_peek_token (parser->lexer);
ccb84981 18351 id = cp_parser_template_id (parser,
0a3b29ad 18352 /*template_keyword_p=*/false,
3d0f901b 18353 /*check_dependency_p=*/true,
18354 /*is_declaration=*/true);
0a3b29ad 18355 /* If that didn't work, it could still be an identifier. */
18356 if (!cp_parser_parse_definitely (parser))
18357 {
18358 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
eef0ab03 18359 {
18360 type_start_token = cp_lexer_peek_token (parser->lexer);
18361 id = cp_parser_identifier (parser);
18362 }
0a3b29ad 18363 else
18364 id = NULL_TREE;
18365 }
18366 else
18367 {
18368 template_id_p = true;
18369 ++num_templates;
18370 }
18371 }
18372
4f62c42e 18373 pop_deferring_access_checks ();
18374
e67a67ea 18375 if (id)
6a8b7c0d 18376 {
18377 cp_parser_check_for_invalid_template_id (parser, id,
18378 type_start_token->location);
6a8b7c0d 18379 }
285f2354 18380 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
1157e9f0 18381
0a3b29ad 18382 /* If it's not a `:' or a `{' then we can't really be looking at a
18383 class-head, since a class-head only appears as part of a
18384 class-specifier. We have to detect this situation before calling
18385 xref_tag, since that has irreversible side-effects. */
18386 if (!cp_parser_next_token_starts_class_definition_p (parser))
18387 {
a2c5b975 18388 cp_parser_error (parser, "expected %<{%> or %<:%>");
1e838f42 18389 type = error_mark_node;
18390 goto out;
0a3b29ad 18391 }
18392
18393 /* At this point, we're going ahead with the class-specifier, even
18394 if some other problem occurs. */
18395 cp_parser_commit_to_tentative_parse (parser);
6a8b7c0d 18396 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18397 {
18398 cp_parser_error (parser,
18399 "cannot specify %<override%> for a class");
18400 type = error_mark_node;
18401 goto out;
18402 }
0a3b29ad 18403 /* Issue the error about the overly-qualified name now. */
18404 if (qualified_p)
71b7ff50 18405 {
18406 cp_parser_error (parser,
18407 "global qualification of class name is invalid");
1e838f42 18408 type = error_mark_node;
18409 goto out;
71b7ff50 18410 }
0a3b29ad 18411 else if (invalid_nested_name_p)
71b7ff50 18412 {
18413 cp_parser_error (parser,
18414 "qualified name does not name a class");
1e838f42 18415 type = error_mark_node;
18416 goto out;
71b7ff50 18417 }
600be0e5 18418 else if (nested_name_specifier)
18419 {
18420 tree scope;
b0ab2aae 18421
18422 /* Reject typedef-names in class heads. */
18423 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18424 {
ccb59bb6 18425 error_at (type_start_token->location,
18426 "invalid class name in declaration of %qD",
18427 type);
b0ab2aae 18428 type = NULL_TREE;
18429 goto done;
18430 }
18431
600be0e5 18432 /* Figure out in what scope the declaration is being placed. */
18433 scope = current_scope ();
600be0e5 18434 /* If that scope does not contain the scope in which the
18435 class was originally declared, the program is invalid. */
18436 if (scope && !is_ancestor (scope, nested_name_specifier))
18437 {
b2df4109 18438 if (at_namespace_scope_p ())
ccb59bb6 18439 error_at (type_start_token->location,
18440 "declaration of %qD in namespace %qD which does not "
18441 "enclose %qD",
18442 type, scope, nested_name_specifier);
b2df4109 18443 else
ccb59bb6 18444 error_at (type_start_token->location,
18445 "declaration of %qD in %qD which does not enclose %qD",
18446 type, scope, nested_name_specifier);
600be0e5 18447 type = NULL_TREE;
18448 goto done;
18449 }
18450 /* [dcl.meaning]
18451
08cc44e7 18452 A declarator-id shall not be qualified except for the
600be0e5 18453 definition of a ... nested class outside of its class
08cc44e7 18454 ... [or] the definition or explicit instantiation of a
600be0e5 18455 class member of a namespace outside of its namespace. */
18456 if (scope == nested_name_specifier)
18457 {
ccb59bb6 18458 permerror (nested_name_specifier_token_start->location,
18459 "extra qualification not allowed");
600be0e5 18460 nested_name_specifier = NULL_TREE;
18461 num_templates = 0;
18462 }
18463 }
e16b1a13 18464 /* An explicit-specialization must be preceded by "template <>". If
18465 it is not, try to recover gracefully. */
ccb84981 18466 if (at_namespace_scope_p ()
e16b1a13 18467 && parser->num_template_parameter_lists == 0
4ccffa39 18468 && template_id_p)
e16b1a13 18469 {
ccb59bb6 18470 error_at (type_start_token->location,
18471 "an explicit specialization must be preceded by %<template <>%>");
e16b1a13 18472 invalid_explicit_specialization_p = true;
18473 /* Take the same action that would have been taken by
18474 cp_parser_explicit_specialization. */
18475 ++parser->num_template_parameter_lists;
18476 begin_specialization ();
18477 }
18478 /* There must be no "return" statements between this point and the
18479 end of this function; set "type "to the correct return value and
18480 use "goto done;" to return. */
0a3b29ad 18481 /* Make sure that the right number of template parameters were
18482 present. */
ad9ae192 18483 if (!cp_parser_check_template_parameters (parser, num_templates,
7b07a15e 18484 type_start_token->location,
18485 /*declarator=*/NULL))
e16b1a13 18486 {
18487 /* If something went wrong, there is no point in even trying to
18488 process the class-definition. */
18489 type = NULL_TREE;
18490 goto done;
18491 }
0a3b29ad 18492
0a3b29ad 18493 /* Look up the type. */
18494 if (template_id_p)
18495 {
04a3504e 18496 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18497 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18498 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18499 {
ccb59bb6 18500 error_at (type_start_token->location,
18501 "function template %qD redeclared as a class template", id);
04a3504e 18502 type = error_mark_node;
18503 }
18504 else
18505 {
18506 type = TREE_TYPE (id);
18507 type = maybe_process_partial_specialization (type);
18508 }
7f602bca 18509 if (nested_name_specifier)
18510 pushed_scope = push_scope (nested_name_specifier);
0a3b29ad 18511 }
7f602bca 18512 else if (nested_name_specifier)
0a3b29ad 18513 {
0a3b29ad 18514 tree class_type;
18515
18516 /* Given:
18517
18518 template <typename T> struct S { struct T };
5f6526e1 18519 template <typename T> struct S<T>::T { };
0a3b29ad 18520
18521 we will get a TYPENAME_TYPE when processing the definition of
18522 `S::T'. We need to resolve it to the actual type before we
18523 try to define it. */
18524 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18525 {
5f6526e1 18526 class_type = resolve_typename_type (TREE_TYPE (type),
18527 /*only_current_p=*/false);
8826a863 18528 if (TREE_CODE (class_type) != TYPENAME_TYPE)
5f6526e1 18529 type = TYPE_NAME (class_type);
18530 else
18531 {
18532 cp_parser_error (parser, "could not resolve typename type");
18533 type = error_mark_node;
18534 }
0a3b29ad 18535 }
18536
e01a7685 18537 if (maybe_process_partial_specialization (TREE_TYPE (type))
18538 == error_mark_node)
18539 {
18540 type = NULL_TREE;
18541 goto done;
18542 }
18543
8172be22 18544 class_type = current_class_type;
18545 /* Enter the scope indicated by the nested-name-specifier. */
7f602bca 18546 pushed_scope = push_scope (nested_name_specifier);
8172be22 18547 /* Get the canonical version of this type. */
18548 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18549 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18550 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
8d52e9a6 18551 {
18552 type = push_template_decl (type);
18553 if (type == error_mark_node)
18554 {
18555 type = NULL_TREE;
18556 goto done;
18557 }
18558 }
9031d10b 18559
8172be22 18560 type = TREE_TYPE (type);
7f602bca 18561 *nested_name_specifier_p = true;
0a3b29ad 18562 }
7f602bca 18563 else /* The name is not a nested name. */
18564 {
18565 /* If the class was unnamed, create a dummy name. */
18566 if (!id)
18567 id = make_anon_name ();
18568 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18569 parser->num_template_parameter_lists);
18570 }
18571
0a3b29ad 18572 /* Indicate whether this class was declared as a `class' or as a
18573 `struct'. */
18574 if (TREE_CODE (type) == RECORD_TYPE)
18575 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18576 cp_parser_check_class_key (class_key, type);
18577
4cbba981 18578 /* If this type was already complete, and we see another definition,
18579 that's an error. */
18580 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18581 {
ccb59bb6 18582 error_at (type_start_token->location, "redefinition of %q#T",
18583 type);
18584 error_at (type_start_token->location, "previous definition of %q+#T",
18585 type);
94637815 18586 type = NULL_TREE;
18587 goto done;
4cbba981 18588 }
1a9b4c25 18589 else if (type == error_mark_node)
18590 type = NULL_TREE;
4cbba981 18591
7f602bca 18592 /* We will have entered the scope containing the class; the names of
4cbba981 18593 base classes should be looked up in that context. For example:
0a3b29ad 18594
18595 struct A { struct B {}; struct C; };
18596 struct A::C : B {};
18597
18598 is valid. */
207355ad 18599
a6460bf1 18600 /* Get the list of base-classes, if there is one. */
18601 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
1a9b4c25 18602 *bases = cp_parser_base_clause (parser);
0a3b29ad 18603
7f602bca 18604 done:
0a3b29ad 18605 /* Leave the scope given by the nested-name-specifier. We will
18606 enter the class scope itself while processing the members. */
7f602bca 18607 if (pushed_scope)
18608 pop_scope (pushed_scope);
0a3b29ad 18609
e16b1a13 18610 if (invalid_explicit_specialization_p)
18611 {
18612 end_specialization ();
18613 --parser->num_template_parameter_lists;
18614 }
aad3e1b3 18615
18616 if (type)
18617 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
fb3e3237 18618 *attributes_p = attributes;
6a8b7c0d 18619 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18620 CLASSTYPE_FINAL (type) = 1;
1e838f42 18621 out:
18622 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
0a3b29ad 18623 return type;
18624}
18625
18626/* Parse a class-key.
18627
18628 class-key:
18629 class
18630 struct
18631 union
18632
18633 Returns the kind of class-key specified, or none_type to indicate
18634 error. */
18635
18636static enum tag_types
45baea8b 18637cp_parser_class_key (cp_parser* parser)
0a3b29ad 18638{
18639 cp_token *token;
18640 enum tag_types tag_type;
18641
18642 /* Look for the class-key. */
c247dce0 18643 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
0a3b29ad 18644 if (!token)
18645 return none_type;
18646
18647 /* Check to see if the TOKEN is a class-key. */
18648 tag_type = cp_parser_token_is_class_key (token);
18649 if (!tag_type)
18650 cp_parser_error (parser, "expected class-key");
18651 return tag_type;
18652}
18653
18654/* Parse an (optional) member-specification.
18655
18656 member-specification:
18657 member-declaration member-specification [opt]
18658 access-specifier : member-specification [opt] */
18659
18660static void
45baea8b 18661cp_parser_member_specification_opt (cp_parser* parser)
0a3b29ad 18662{
18663 while (true)
18664 {
18665 cp_token *token;
18666 enum rid keyword;
18667
18668 /* Peek at the next token. */
18669 token = cp_lexer_peek_token (parser->lexer);
18670 /* If it's a `}', or EOF then we've seen all the members. */
b75b98aa 18671 if (token->type == CPP_CLOSE_BRACE
18672 || token->type == CPP_EOF
18673 || token->type == CPP_PRAGMA_EOL)
0a3b29ad 18674 break;
18675
18676 /* See if this token is a keyword. */
18677 keyword = token->keyword;
18678 switch (keyword)
18679 {
18680 case RID_PUBLIC:
18681 case RID_PROTECTED:
18682 case RID_PRIVATE:
18683 /* Consume the access-specifier. */
18684 cp_lexer_consume_token (parser->lexer);
18685 /* Remember which access-specifier is active. */
3369eb76 18686 current_access_specifier = token->u.value;
0a3b29ad 18687 /* Look for the `:'. */
c247dce0 18688 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 18689 break;
18690
18691 default:
882d3c4f 18692 /* Accept #pragmas at class scope. */
18693 if (token->type == CPP_PRAGMA)
18694 {
b75b98aa 18695 cp_parser_pragma (parser, pragma_external);
882d3c4f 18696 break;
18697 }
18698
0a3b29ad 18699 /* Otherwise, the next construction must be a
18700 member-declaration. */
18701 cp_parser_member_declaration (parser);
0a3b29ad 18702 }
18703 }
18704}
18705
ccb84981 18706/* Parse a member-declaration.
0a3b29ad 18707
18708 member-declaration:
18709 decl-specifier-seq [opt] member-declarator-list [opt] ;
18710 function-definition ; [opt]
18711 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18712 using-declaration
ccb84981 18713 template-declaration
370478b1 18714 alias-declaration
0a3b29ad 18715
18716 member-declarator-list:
18717 member-declarator
18718 member-declarator-list , member-declarator
18719
18720 member-declarator:
ccb84981 18721 declarator pure-specifier [opt]
0a3b29ad 18722 declarator constant-initializer [opt]
ccb84981 18723 identifier [opt] : constant-expression
0a3b29ad 18724
18725 GNU Extensions:
18726
18727 member-declaration:
18728 __extension__ member-declaration
18729
18730 member-declarator:
18731 declarator attributes [opt] pure-specifier [opt]
18732 declarator attributes [opt] constant-initializer [opt]
7a05c4b1 18733 identifier [opt] attributes [opt] : constant-expression
18734
18735 C++0x Extensions:
18736
18737 member-declaration:
18738 static_assert-declaration */
0a3b29ad 18739
18740static void
45baea8b 18741cp_parser_member_declaration (cp_parser* parser)
0a3b29ad 18742{
4b9b2871 18743 cp_decl_specifier_seq decl_specifiers;
0a3b29ad 18744 tree prefix_attributes;
18745 tree decl;
8172be22 18746 int declares_class_or_enum;
0a3b29ad 18747 bool friend_p;
ad9ae192 18748 cp_token *token = NULL;
18749 cp_token *decl_spec_token_start = NULL;
18750 cp_token *initializer_token_start = NULL;
0a3b29ad 18751 int saved_pedantic;
1e838f42 18752 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
0a3b29ad 18753
18754 /* Check for the `__extension__' keyword. */
18755 if (cp_parser_extension_opt (parser, &saved_pedantic))
18756 {
18757 /* Recurse. */
18758 cp_parser_member_declaration (parser);
18759 /* Restore the old value of the PEDANTIC flag. */
18760 pedantic = saved_pedantic;
18761
18762 return;
18763 }
18764
18765 /* Check for a template-declaration. */
18766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18767 {
81ef2172 18768 /* An explicit specialization here is an error condition, and we
18769 expect the specialization handler to detect and report this. */
18770 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18771 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18772 cp_parser_explicit_specialization (parser);
18773 else
18774 cp_parser_template_declaration (parser, /*member_p=*/true);
0a3b29ad 18775
18776 return;
18777 }
18778
18779 /* Check for a using-declaration. */
18780 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18781 {
370478b1 18782 if (cxx_dialect < cxx0x)
18783 {
18784 /* Parse the using-declaration. */
18785 cp_parser_using_declaration (parser,
18786 /*access_declaration_p=*/false);
18787 return;
18788 }
18789 else
18790 {
18791 tree decl;
18792 cp_parser_parse_tentatively (parser);
18793 decl = cp_parser_alias_declaration (parser);
18794 if (cp_parser_parse_definitely (parser))
18795 finish_member_declaration (decl);
18796 else
18797 cp_parser_using_declaration (parser,
18798 /*access_declaration_p=*/false);
18799 return;
18800 }
0a3b29ad 18801 }
ccb84981 18802
79408174 18803 /* Check for @defs. */
18804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18805 {
18806 tree ivar, member;
18807 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18808 ivar = ivar_chains;
18809 while (ivar)
18810 {
18811 member = ivar;
18812 ivar = TREE_CHAIN (member);
18813 TREE_CHAIN (member) = NULL_TREE;
18814 finish_member_declaration (member);
18815 }
18816 return;
18817 }
7a4e126b 18818
7a05c4b1 18819 /* If the next token is `static_assert' we have a static assertion. */
18820 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18821 {
18822 cp_parser_static_assert (parser, /*member_p=*/true);
18823 return;
18824 }
18825
1e838f42 18826 parser->colon_corrects_to_scope_p = false;
18827
da2a3271 18828 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
3d79ce2d 18829 goto out;
da2a3271 18830
0a3b29ad 18831 /* Parse the decl-specifier-seq. */
ad9ae192 18832 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 18833 cp_parser_decl_specifier_seq (parser,
18834 CP_PARSER_FLAGS_OPTIONAL,
18835 &decl_specifiers,
18836 &declares_class_or_enum);
18837 prefix_attributes = decl_specifiers.attributes;
18838 decl_specifiers.attributes = NULL_TREE;
954ad420 18839 /* Check for an invalid type-name. */
67484828 18840 if (!decl_specifiers.any_type_specifiers_p
882d3c4f 18841 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
1e838f42 18842 goto out;
0a3b29ad 18843 /* If there is no declarator, then the decl-specifier-seq should
18844 specify a type. */
18845 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18846 {
18847 /* If there was no decl-specifier-seq, and the next token is a
18848 `;', then we have something like:
18849
18850 struct S { ; };
18851
18852 [class.mem]
18853
18854 Each member-declaration shall declare at least one member
18855 name of the class. */
4b9b2871 18856 if (!decl_specifiers.any_specifiers_p)
0a3b29ad 18857 {
b9dd3954 18858 cp_token *token = cp_lexer_peek_token (parser->lexer);
8864917d 18859 if (!in_system_header_at (token->location))
29438999 18860 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
0a3b29ad 18861 }
ccb84981 18862 else
0a3b29ad 18863 {
18864 tree type;
ccb84981 18865
0a3b29ad 18866 /* See if this declaration is a friend. */
4b9b2871 18867 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 18868 /* If there were decl-specifiers, check to see if there was
18869 a class-declaration. */
4b9b2871 18870 type = check_tag_decl (&decl_specifiers);
0a3b29ad 18871 /* Nested classes have already been added to the class, but
18872 a `friend' needs to be explicitly registered. */
18873 if (friend_p)
18874 {
18875 /* If the `friend' keyword was present, the friend must
18876 be introduced with a class-key. */
145c81fd 18877 if (!declares_class_or_enum && cxx_dialect < cxx0x)
29438999 18878 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
145c81fd 18879 "in C++03 a class-key must be used "
18880 "when declaring a friend");
0a3b29ad 18881 /* In this case:
18882
ccb84981 18883 template <typename T> struct A {
653e5405 18884 friend struct A<T>::B;
18885 };
ccb84981 18886
0a3b29ad 18887 A<T>::B will be represented by a TYPENAME_TYPE, and
18888 therefore not recognized by check_tag_decl. */
145c81fd 18889 if (!type)
18890 {
18891 type = decl_specifiers.type;
18892 if (type && TREE_CODE (type) == TYPE_DECL)
18893 type = TREE_TYPE (type);
18894 }
069754af 18895 if (!type || !TYPE_P (type))
ccb59bb6 18896 error_at (decl_spec_token_start->location,
18897 "friend declaration does not name a class or "
18898 "function");
0a3b29ad 18899 else
b123b79d 18900 make_friend_class (current_class_type, type,
18901 /*complain=*/true);
0a3b29ad 18902 }
18903 /* If there is no TYPE, an error message will already have
18904 been issued. */
4b9b2871 18905 else if (!type || type == error_mark_node)
0a3b29ad 18906 ;
18907 /* An anonymous aggregate has to be handled specially; such
18908 a declaration really declares a data member (with a
18909 particular type), as opposed to a nested class. */
18910 else if (ANON_AGGR_TYPE_P (type))
18911 {
97abbe27 18912 /* C++11 9.5/6. */
18913 if (decl_specifiers.storage_class != sc_none)
18914 error_at (decl_spec_token_start->location,
18915 "a storage class on an anonymous aggregate "
18916 "in class scope is not allowed");
18917
0a3b29ad 18918 /* Remove constructors and such from TYPE, now that we
755edffd 18919 know it is an anonymous aggregate. */
0a3b29ad 18920 fixup_anonymous_aggr (type);
18921 /* And make the corresponding data member. */
e60a6f7b 18922 decl = build_decl (decl_spec_token_start->location,
18923 FIELD_DECL, NULL_TREE, type);
0a3b29ad 18924 /* Add it to the class. */
18925 finish_member_declaration (decl);
18926 }
7e35473e 18927 else
ad9ae192 18928 cp_parser_check_access_in_redeclaration
18929 (TYPE_NAME (type),
18930 decl_spec_token_start->location);
0a3b29ad 18931 }
18932 }
18933 else
18934 {
864468c7 18935 bool assume_semicolon = false;
18936
0a3b29ad 18937 /* See if these declarations will be friends. */
4b9b2871 18938 friend_p = cp_parser_friend_p (&decl_specifiers);
0a3b29ad 18939
ccb84981 18940 /* Keep going until we hit the `;' at the end of the
0a3b29ad 18941 declaration. */
18942 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18943 {
18944 tree attributes = NULL_TREE;
18945 tree first_attribute;
18946
18947 /* Peek at the next token. */
18948 token = cp_lexer_peek_token (parser->lexer);
18949
18950 /* Check for a bitfield declaration. */
18951 if (token->type == CPP_COLON
18952 || (token->type == CPP_NAME
ccb84981 18953 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
0a3b29ad 18954 == CPP_COLON))
18955 {
18956 tree identifier;
18957 tree width;
18958
18959 /* Get the name of the bitfield. Note that we cannot just
18960 check TOKEN here because it may have been invalidated by
18961 the call to cp_lexer_peek_nth_token above. */
18962 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18963 identifier = cp_parser_identifier (parser);
18964 else
18965 identifier = NULL_TREE;
18966
18967 /* Consume the `:' token. */
18968 cp_lexer_consume_token (parser->lexer);
18969 /* Get the width of the bitfield. */
ccb84981 18970 width
5f6526e1 18971 = cp_parser_constant_expression (parser,
18972 /*allow_non_constant=*/false,
18973 NULL);
0a3b29ad 18974
18975 /* Look for attributes that apply to the bitfield. */
18976 attributes = cp_parser_attributes_opt (parser);
18977 /* Remember which attributes are prefix attributes and
18978 which are not. */
18979 first_attribute = attributes;
18980 /* Combine the attributes. */
18981 attributes = chainon (prefix_attributes, attributes);
18982
18983 /* Create the bitfield declaration. */
207355ad 18984 decl = grokbitfield (identifier
2ded3667 18985 ? make_id_declarator (NULL_TREE,
2366ed31 18986 identifier,
18987 sfk_none)
3046c0a3 18988 : NULL,
4b9b2871 18989 &decl_specifiers,
f922e029 18990 width,
18991 attributes);
0a3b29ad 18992 }
18993 else
18994 {
3046c0a3 18995 cp_declarator *declarator;
0a3b29ad 18996 tree initializer;
18997 tree asm_specification;
0986fa22 18998 int ctor_dtor_or_conv_p;
0a3b29ad 18999
19000 /* Parse the declarator. */
ccb84981 19001 declarator
42bbd0ec 19002 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
92b128ed 19003 &ctor_dtor_or_conv_p,
08ea345c 19004 /*parenthesized_p=*/NULL,
19005 /*member_p=*/true);
0a3b29ad 19006
19007 /* If something went wrong parsing the declarator, make sure
19008 that we at least consume some tokens. */
3046c0a3 19009 if (declarator == cp_error_declarator)
0a3b29ad 19010 {
19011 /* Skip to the end of the statement. */
19012 cp_parser_skip_to_end_of_statement (parser);
92b128ed 19013 /* If the next token is not a semicolon, that is
19014 probably because we just skipped over the body of
19015 a function. So, we consume a semicolon if
19016 present, but do not issue an error message if it
19017 is not present. */
19018 if (cp_lexer_next_token_is (parser->lexer,
19019 CPP_SEMICOLON))
19020 cp_lexer_consume_token (parser->lexer);
1e838f42 19021 goto out;
0a3b29ad 19022 }
19023
e2ae55f2 19024 if (declares_class_or_enum & 2)
19025 cp_parser_check_for_definition_in_return_type
eef0ab03 19026 (declarator, decl_specifiers.type,
a60f3e81 19027 decl_specifiers.locations[ds_type_spec]);
8172be22 19028
0a3b29ad 19029 /* Look for an asm-specification. */
19030 asm_specification = cp_parser_asm_specification_opt (parser);
19031 /* Look for attributes that apply to the declaration. */
19032 attributes = cp_parser_attributes_opt (parser);
19033 /* Remember which attributes are prefix attributes and
19034 which are not. */
19035 first_attribute = attributes;
19036 /* Combine the attributes. */
19037 attributes = chainon (prefix_attributes, attributes);
19038
19039 /* If it's an `=', then we have a constant-initializer or a
19040 pure-specifier. It is not correct to parse the
19041 initializer before registering the member declaration
19042 since the member declaration should be in scope while
19043 its initializer is processed. However, the rest of the
19044 front end does not yet provide an interface that allows
19045 us to handle this correctly. */
19046 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19047 {
19048 /* In [class.mem]:
19049
19050 A pure-specifier shall be used only in the declaration of
ccb84981 19051 a virtual function.
0a3b29ad 19052
19053 A member-declarator can contain a constant-initializer
19054 only if it declares a static member of integral or
ccb84981 19055 enumeration type.
0a3b29ad 19056
19057 Therefore, if the DECLARATOR is for a function, we look
19058 for a pure-specifier; otherwise, we look for a
19059 constant-initializer. When we call `grokfield', it will
19060 perform more stringent semantics checks. */
ad9ae192 19061 initializer_token_start = cp_lexer_peek_token (parser->lexer);
3ab40fb9 19062 if (function_declarator_p (declarator)
19063 || (decl_specifiers.type
19064 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19065 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19066 == FUNCTION_TYPE)))
0a3b29ad 19067 initializer = cp_parser_pure_specifier (parser);
3ab40fb9 19068 else if (decl_specifiers.storage_class != sc_static)
19069 initializer = cp_parser_save_nsdmi (parser);
0ecfcc92 19070 else if (cxx_dialect >= cxx0x)
19071 {
19072 bool nonconst;
19073 /* Don't require a constant rvalue in C++11, since we
19074 might want a reference constant. We'll enforce
19075 constancy later. */
19076 cp_lexer_consume_token (parser->lexer);
19077 /* Parse the initializer. */
19078 initializer = cp_parser_initializer_clause (parser,
19079 &nonconst);
19080 }
0a3b29ad 19081 else
92b128ed 19082 /* Parse the initializer. */
19083 initializer = cp_parser_constant_initializer (parser);
0a3b29ad 19084 }
d9c249a4 19085 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19086 && !function_declarator_p (declarator))
19087 {
19088 bool x;
3ab40fb9 19089 if (decl_specifiers.storage_class != sc_static)
19090 initializer = cp_parser_save_nsdmi (parser);
19091 else
19092 initializer = cp_parser_initializer (parser, &x, &x);
d9c249a4 19093 }
0a3b29ad 19094 /* Otherwise, there is no initializer. */
19095 else
19096 initializer = NULL_TREE;
19097
19098 /* See if we are probably looking at a function
f29f4570 19099 definition. We are certainly not looking at a
0a3b29ad 19100 member-declarator. Calling `grokfield' has
19101 side-effects, so we must not do it unless we are sure
19102 that we are looking at a member-declarator. */
ccb84981 19103 if (cp_parser_token_starts_function_definition_p
0a3b29ad 19104 (cp_lexer_peek_token (parser->lexer)))
92b128ed 19105 {
19106 /* The grammar does not allow a pure-specifier to be
19107 used when a member function is defined. (It is
19108 possible that this fact is an oversight in the
19109 standard, since a pure function may be defined
19110 outside of the class-specifier. */
a2748d43 19111 if (initializer && initializer_token_start)
ccb59bb6 19112 error_at (initializer_token_start->location,
19113 "pure-specifier on function-definition");
92b128ed 19114 decl = cp_parser_save_member_function_body (parser,
4b9b2871 19115 &decl_specifiers,
92b128ed 19116 declarator,
19117 attributes);
19118 /* If the member was not a friend, declare it here. */
19119 if (!friend_p)
19120 finish_member_declaration (decl);
19121 /* Peek at the next token. */
19122 token = cp_lexer_peek_token (parser->lexer);
19123 /* If the next token is a semicolon, consume it. */
19124 if (token->type == CPP_SEMICOLON)
e6ae3136 19125 cp_lexer_consume_token (parser->lexer);
1e838f42 19126 goto out;
92b128ed 19127 }
0a3b29ad 19128 else
d19f0a18 19129 if (declarator->kind == cdk_function)
19130 declarator->id_loc = token->location;
d91303a6 19131 /* Create the declaration. */
19132 decl = grokfield (declarator, &decl_specifiers,
19133 initializer, /*init_const_expr_p=*/true,
19134 asm_specification,
19135 attributes);
0a3b29ad 19136 }
19137
19138 /* Reset PREFIX_ATTRIBUTES. */
19139 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19140 attributes = TREE_CHAIN (attributes);
19141 if (attributes)
19142 TREE_CHAIN (attributes) = NULL_TREE;
19143
19144 /* If there is any qualification still in effect, clear it
19145 now; we will be starting fresh with the next declarator. */
19146 parser->scope = NULL_TREE;
19147 parser->qualifying_scope = NULL_TREE;
19148 parser->object_scope = NULL_TREE;
19149 /* If it's a `,', then there are more declarators. */
19150 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19151 cp_lexer_consume_token (parser->lexer);
19152 /* If the next token isn't a `;', then we have a parse error. */
19153 else if (cp_lexer_next_token_is_not (parser->lexer,
19154 CPP_SEMICOLON))
19155 {
864468c7 19156 /* The next token might be a ways away from where the
19157 actual semicolon is missing. Find the previous token
19158 and use that for our error position. */
19159 cp_token *token = cp_lexer_previous_token (parser->lexer);
19160 error_at (token->location,
19161 "expected %<;%> at end of member declaration");
0a3b29ad 19162
864468c7 19163 /* Assume that the user meant to provide a semicolon. If
19164 we were to cp_parser_skip_to_end_of_statement, we might
19165 skip to a semicolon inside a member function definition
19166 and issue nonsensical error messages. */
19167 assume_semicolon = true;
0a3b29ad 19168 }
19169
19170 if (decl)
19171 {
19172 /* Add DECL to the list of members. */
19173 if (!friend_p)
19174 finish_member_declaration (decl);
19175
0a3b29ad 19176 if (TREE_CODE (decl) == FUNCTION_DECL)
69b6679c 19177 cp_parser_save_default_args (parser, decl);
3ab40fb9 19178 else if (TREE_CODE (decl) == FIELD_DECL
19179 && !DECL_C_BIT_FIELD (decl)
19180 && DECL_INITIAL (decl))
19181 /* Add DECL to the queue of NSDMI to be parsed later. */
19182 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
0a3b29ad 19183 }
864468c7 19184
19185 if (assume_semicolon)
1e838f42 19186 goto out;
0a3b29ad 19187 }
19188 }
19189
c247dce0 19190 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
1e838f42 19191 out:
19192 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
0a3b29ad 19193}
19194
19195/* Parse a pure-specifier.
19196
19197 pure-specifier:
19198 = 0
19199
19200 Returns INTEGER_ZERO_NODE if a pure specifier is found.
63eff20d 19201 Otherwise, ERROR_MARK_NODE is returned. */
0a3b29ad 19202
19203static tree
45baea8b 19204cp_parser_pure_specifier (cp_parser* parser)
0a3b29ad 19205{
19206 cp_token *token;
19207
19208 /* Look for the `=' token. */
c247dce0 19209 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
0a3b29ad 19210 return error_mark_node;
19211 /* Look for the `0' token. */
d8acda95 19212 token = cp_lexer_peek_token (parser->lexer);
19213
19214 if (token->type == CPP_EOF
19215 || token->type == CPP_PRAGMA_EOL)
19216 return error_mark_node;
19217
19218 cp_lexer_consume_token (parser->lexer);
2336da2a 19219
19220 /* Accept = default or = delete in c++0x mode. */
19221 if (token->keyword == RID_DEFAULT
19222 || token->keyword == RID_DELETE)
19223 {
bf8d19fe 19224 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
2336da2a 19225 return token->u.value;
19226 }
19227
8dba02f7 19228 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
5f69415d 19229 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19230 {
074ab442 19231 cp_parser_error (parser,
640710cf 19232 "invalid pure specifier (only %<= 0%> is allowed)");
5f69415d 19233 cp_parser_skip_to_end_of_statement (parser);
19234 return error_mark_node;
19235 }
19236 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19237 {
ccb59bb6 19238 error_at (token->location, "templates may not be %<virtual%>");
5f69415d 19239 return error_mark_node;
19240 }
0a3b29ad 19241
5f69415d 19242 return integer_zero_node;
0a3b29ad 19243}
19244
19245/* Parse a constant-initializer.
19246
19247 constant-initializer:
19248 = constant-expression
19249
19250 Returns a representation of the constant-expression. */
19251
19252static tree
45baea8b 19253cp_parser_constant_initializer (cp_parser* parser)
0a3b29ad 19254{
19255 /* Look for the `=' token. */
c247dce0 19256 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
0a3b29ad 19257 return error_mark_node;
19258
19259 /* It is invalid to write:
19260
19261 struct S { static const int i = { 7 }; };
19262
19263 */
19264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19265 {
19266 cp_parser_error (parser,
19267 "a brace-enclosed initializer is not allowed here");
19268 /* Consume the opening brace. */
19269 cp_lexer_consume_token (parser->lexer);
19270 /* Skip the initializer. */
19271 cp_parser_skip_to_closing_brace (parser);
19272 /* Look for the trailing `}'. */
c247dce0 19273 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
ccb84981 19274
0a3b29ad 19275 return error_mark_node;
19276 }
19277
ccb84981 19278 return cp_parser_constant_expression (parser,
5f6526e1 19279 /*allow_non_constant=*/false,
19280 NULL);
0a3b29ad 19281}
19282
19283/* Derived classes [gram.class.derived] */
19284
19285/* Parse a base-clause.
19286
19287 base-clause:
ccb84981 19288 : base-specifier-list
0a3b29ad 19289
19290 base-specifier-list:
d95d815d 19291 base-specifier ... [opt]
19292 base-specifier-list , base-specifier ... [opt]
0a3b29ad 19293
19294 Returns a TREE_LIST representing the base-classes, in the order in
19295 which they were declared. The representation of each node is as
ccb84981 19296 described by cp_parser_base_specifier.
0a3b29ad 19297
19298 In the case that no bases are specified, this function will return
19299 NULL_TREE, not ERROR_MARK_NODE. */
19300
19301static tree
45baea8b 19302cp_parser_base_clause (cp_parser* parser)
0a3b29ad 19303{
19304 tree bases = NULL_TREE;
19305
19306 /* Look for the `:' that begins the list. */
c247dce0 19307 cp_parser_require (parser, CPP_COLON, RT_COLON);
0a3b29ad 19308
19309 /* Scan the base-specifier-list. */
19310 while (true)
19311 {
19312 cp_token *token;
19313 tree base;
d95d815d 19314 bool pack_expansion_p = false;
0a3b29ad 19315
19316 /* Look for the base-specifier. */
19317 base = cp_parser_base_specifier (parser);
d95d815d 19318 /* Look for the (optional) ellipsis. */
19319 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19320 {
19321 /* Consume the `...'. */
19322 cp_lexer_consume_token (parser->lexer);
19323
19324 pack_expansion_p = true;
19325 }
19326
0a3b29ad 19327 /* Add BASE to the front of the list. */
07b8f133 19328 if (base && base != error_mark_node)
0a3b29ad 19329 {
d95d815d 19330 if (pack_expansion_p)
19331 /* Make this a pack expansion type. */
19332 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
d95d815d 19333
830a6615 19334 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
613687b1 19335 {
19336 TREE_CHAIN (base) = bases;
19337 bases = base;
19338 }
0a3b29ad 19339 }
19340 /* Peek at the next token. */
19341 token = cp_lexer_peek_token (parser->lexer);
19342 /* If it's not a comma, then the list is complete. */
19343 if (token->type != CPP_COMMA)
19344 break;
19345 /* Consume the `,'. */
19346 cp_lexer_consume_token (parser->lexer);
19347 }
19348
19349 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19350 base class had a qualified name. However, the next name that
19351 appears is certainly not qualified. */
19352 parser->scope = NULL_TREE;
19353 parser->qualifying_scope = NULL_TREE;
19354 parser->object_scope = NULL_TREE;
19355
19356 return nreverse (bases);
19357}
19358
19359/* Parse a base-specifier.
19360
19361 base-specifier:
19362 :: [opt] nested-name-specifier [opt] class-name
19363 virtual access-specifier [opt] :: [opt] nested-name-specifier
19364 [opt] class-name
19365 access-specifier virtual [opt] :: [opt] nested-name-specifier
19366 [opt] class-name
19367
19368 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19369 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19370 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19371 (or the ERROR_MARK_NODE) indicating the type that was specified. */
ccb84981 19372
0a3b29ad 19373static tree
45baea8b 19374cp_parser_base_specifier (cp_parser* parser)
0a3b29ad 19375{
19376 cp_token *token;
19377 bool done = false;
19378 bool virtual_p = false;
19379 bool duplicate_virtual_error_issued_p = false;
19380 bool duplicate_access_error_issued_p = false;
d622b3bd 19381 bool class_scope_p, template_p;
95f3173a 19382 tree access = access_default_node;
0a3b29ad 19383 tree type;
19384
19385 /* Process the optional `virtual' and `access-specifier'. */
19386 while (!done)
19387 {
19388 /* Peek at the next token. */
19389 token = cp_lexer_peek_token (parser->lexer);
19390 /* Process `virtual'. */
19391 switch (token->keyword)
19392 {
19393 case RID_VIRTUAL:
19394 /* If `virtual' appears more than once, issue an error. */
19395 if (virtual_p && !duplicate_virtual_error_issued_p)
19396 {
19397 cp_parser_error (parser,
a2c5b975 19398 "%<virtual%> specified more than once in base-specified");
0a3b29ad 19399 duplicate_virtual_error_issued_p = true;
19400 }
19401
19402 virtual_p = true;
19403
19404 /* Consume the `virtual' token. */
19405 cp_lexer_consume_token (parser->lexer);
19406
19407 break;
19408
19409 case RID_PUBLIC:
19410 case RID_PROTECTED:
19411 case RID_PRIVATE:
19412 /* If more than one access specifier appears, issue an
19413 error. */
95f3173a 19414 if (access != access_default_node
19415 && !duplicate_access_error_issued_p)
0a3b29ad 19416 {
19417 cp_parser_error (parser,
19418 "more than one access specifier in base-specified");
19419 duplicate_access_error_issued_p = true;
19420 }
19421
95f3173a 19422 access = ridpointers[(int) token->keyword];
0a3b29ad 19423
19424 /* Consume the access-specifier. */
19425 cp_lexer_consume_token (parser->lexer);
19426
19427 break;
19428
19429 default:
19430 done = true;
19431 break;
19432 }
19433 }
0f3ccaa3 19434 /* It is not uncommon to see programs mechanically, erroneously, use
eae805b4 19435 the 'typename' keyword to denote (dependent) qualified types
9591b260 19436 as base classes. */
19437 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19438 {
ad9ae192 19439 token = cp_lexer_peek_token (parser->lexer);
9591b260 19440 if (!processing_template_decl)
ccb59bb6 19441 error_at (token->location,
19442 "keyword %<typename%> not allowed outside of templates");
9591b260 19443 else
ccb59bb6 19444 error_at (token->location,
19445 "keyword %<typename%> not allowed in this context "
19446 "(the base class is implicitly a type)");
9591b260 19447 cp_lexer_consume_token (parser->lexer);
19448 }
0a3b29ad 19449
0a3b29ad 19450 /* Look for the optional `::' operator. */
130bb1d4 19451 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
0a3b29ad 19452 /* Look for the nested-name-specifier. The simplest way to
19453 implement:
19454
19455 [temp.res]
19456
19457 The keyword `typename' is not permitted in a base-specifier or
19458 mem-initializer; in these contexts a qualified name that
19459 depends on a template-parameter is implicitly assumed to be a
19460 type name.
19461
19462 is to pretend that we have seen the `typename' keyword at this
ccb84981 19463 point. */
0a3b29ad 19464 cp_parser_nested_name_specifier_opt (parser,
19465 /*typename_keyword_p=*/true,
19466 /*check_dependency_p=*/true,
e2ae55f2 19467 typename_type,
3d0f901b 19468 /*is_declaration=*/true);
0a3b29ad 19469 /* If the base class is given by a qualified name, assume that names
19470 we see are type names or templates, as appropriate. */
19471 class_scope_p = (parser->scope && TYPE_P (parser->scope));
d622b3bd 19472 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
ccb84981 19473
07b8f133 19474 if (!parser->scope
19475 && cp_lexer_next_token_is_decltype (parser->lexer))
19476 /* DR 950 allows decltype as a base-specifier. */
19477 type = cp_parser_decltype (parser);
19478 else
19479 {
19480 /* Otherwise, look for the class-name. */
19481 type = cp_parser_class_name (parser,
19482 class_scope_p,
19483 template_p,
19484 typename_type,
19485 /*check_dependency_p=*/true,
19486 /*class_head_p=*/false,
19487 /*is_declaration=*/true);
19488 type = TREE_TYPE (type);
19489 }
0a3b29ad 19490
19491 if (type == error_mark_node)
19492 return error_mark_node;
19493
07b8f133 19494 return finish_base_specifier (type, access, virtual_p);
0a3b29ad 19495}
19496
19497/* Exception handling [gram.exception] */
19498
f770bf53 19499/* Parse an (optional) noexcept-specification.
0a3b29ad 19500
f770bf53 19501 noexcept-specification:
19502 noexcept ( constant-expression ) [opt]
0a3b29ad 19503
f770bf53 19504 If no noexcept-specification is present, returns NULL_TREE.
19505 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19506 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19507 there are no parentheses. CONSUMED_EXPR will be set accordingly.
19508 Otherwise, returns a noexcept specification unless RETURN_COND is true,
19509 in which case a boolean condition is returned instead. */
0a3b29ad 19510
19511static tree
f770bf53 19512cp_parser_noexcept_specification_opt (cp_parser* parser,
19513 bool require_constexpr,
19514 bool* consumed_expr,
19515 bool return_cond)
0a3b29ad 19516{
19517 cp_token *token;
3644efa5 19518 const char *saved_message;
0a3b29ad 19519
19520 /* Peek at the next token. */
19521 token = cp_lexer_peek_token (parser->lexer);
3644efa5 19522
19523 /* Is it a noexcept-specification? */
19524 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19525 {
19526 tree expr;
19527 cp_lexer_consume_token (parser->lexer);
19528
19529 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19530 {
19531 cp_lexer_consume_token (parser->lexer);
19532
f770bf53 19533 if (require_constexpr)
19534 {
19535 /* Types may not be defined in an exception-specification. */
19536 saved_message = parser->type_definition_forbidden_message;
19537 parser->type_definition_forbidden_message
19538 = G_("types may not be defined in an exception-specification");
3644efa5 19539
f770bf53 19540 expr = cp_parser_constant_expression (parser, false, NULL);
3644efa5 19541
f770bf53 19542 /* Restore the saved message. */
19543 parser->type_definition_forbidden_message = saved_message;
19544 }
19545 else
19546 {
19547 expr = cp_parser_expression (parser, false, NULL);
19548 *consumed_expr = true;
19549 }
3644efa5 19550
19551 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19552 }
19553 else
f770bf53 19554 {
19555 expr = boolean_true_node;
19556 if (!require_constexpr)
19557 *consumed_expr = false;
19558 }
3644efa5 19559
f770bf53 19560 /* We cannot build a noexcept-spec right away because this will check
19561 that expr is a constexpr. */
19562 if (!return_cond)
19563 return build_noexcept_spec (expr, tf_warning_or_error);
19564 else
19565 return expr;
3644efa5 19566 }
f770bf53 19567 else
19568 return NULL_TREE;
19569}
19570
19571/* Parse an (optional) exception-specification.
19572
19573 exception-specification:
19574 throw ( type-id-list [opt] )
19575
19576 Returns a TREE_LIST representing the exception-specification. The
19577 TREE_VALUE of each node is a type. */
19578
19579static tree
19580cp_parser_exception_specification_opt (cp_parser* parser)
19581{
19582 cp_token *token;
19583 tree type_id_list;
19584 const char *saved_message;
19585
19586 /* Peek at the next token. */
19587 token = cp_lexer_peek_token (parser->lexer);
19588
19589 /* Is it a noexcept-specification? */
19590 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19591 false);
19592 if (type_id_list != NULL_TREE)
19593 return type_id_list;
3644efa5 19594
0a3b29ad 19595 /* If it's not `throw', then there's no exception-specification. */
19596 if (!cp_parser_is_keyword (token, RID_THROW))
19597 return NULL_TREE;
19598
3644efa5 19599#if 0
19600 /* Enable this once a lot of code has transitioned to noexcept? */
543efdbe 19601 if (cxx_dialect >= cxx0x && !in_system_header)
3644efa5 19602 warning (OPT_Wdeprecated, "dynamic exception specifications are "
bf776685 19603 "deprecated in C++0x; use %<noexcept%> instead");
3644efa5 19604#endif
19605
0a3b29ad 19606 /* Consume the `throw'. */
19607 cp_lexer_consume_token (parser->lexer);
19608
19609 /* Look for the `('. */
c247dce0 19610 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 19611
19612 /* Peek at the next token. */
19613 token = cp_lexer_peek_token (parser->lexer);
19614 /* If it's not a `)', then there is a type-id-list. */
19615 if (token->type != CPP_CLOSE_PAREN)
19616 {
0a3b29ad 19617 /* Types may not be defined in an exception-specification. */
19618 saved_message = parser->type_definition_forbidden_message;
19619 parser->type_definition_forbidden_message
ca82e026 19620 = G_("types may not be defined in an exception-specification");
0a3b29ad 19621 /* Parse the type-id-list. */
19622 type_id_list = cp_parser_type_id_list (parser);
19623 /* Restore the saved message. */
19624 parser->type_definition_forbidden_message = saved_message;
19625 }
19626 else
19627 type_id_list = empty_except_spec;
19628
19629 /* Look for the `)'. */
c247dce0 19630 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 19631
19632 return type_id_list;
19633}
19634
19635/* Parse an (optional) type-id-list.
19636
19637 type-id-list:
d95d815d 19638 type-id ... [opt]
19639 type-id-list , type-id ... [opt]
0a3b29ad 19640
19641 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19642 in the order that the types were presented. */
19643
19644static tree
45baea8b 19645cp_parser_type_id_list (cp_parser* parser)
0a3b29ad 19646{
19647 tree types = NULL_TREE;
19648
19649 while (true)
19650 {
19651 cp_token *token;
19652 tree type;
19653
19654 /* Get the next type-id. */
19655 type = cp_parser_type_id (parser);
d95d815d 19656 /* Parse the optional ellipsis. */
19657 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19658 {
19659 /* Consume the `...'. */
19660 cp_lexer_consume_token (parser->lexer);
19661
19662 /* Turn the type into a pack expansion expression. */
19663 type = make_pack_expansion (type);
19664 }
0a3b29ad 19665 /* Add it to the list. */
19666 types = add_exception_specifier (types, type, /*complain=*/1);
19667 /* Peek at the next token. */
19668 token = cp_lexer_peek_token (parser->lexer);
19669 /* If it is not a `,', we are done. */
19670 if (token->type != CPP_COMMA)
19671 break;
19672 /* Consume the `,'. */
19673 cp_lexer_consume_token (parser->lexer);
19674 }
19675
19676 return nreverse (types);
19677}
19678
19679/* Parse a try-block.
19680
19681 try-block:
19682 try compound-statement handler-seq */
19683
19684static tree
45baea8b 19685cp_parser_try_block (cp_parser* parser)
0a3b29ad 19686{
19687 tree try_block;
19688
c247dce0 19689 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
0a3b29ad 19690 try_block = begin_try_block ();
6bf5e78d 19691 cp_parser_compound_statement (parser, NULL, true, false);
0a3b29ad 19692 finish_try_block (try_block);
19693 cp_parser_handler_seq (parser);
19694 finish_handler_sequence (try_block);
19695
19696 return try_block;
19697}
19698
19699/* Parse a function-try-block.
19700
19701 function-try-block:
19702 try ctor-initializer [opt] function-body handler-seq */
19703
19704static bool
45baea8b 19705cp_parser_function_try_block (cp_parser* parser)
0a3b29ad 19706{
78f7169a 19707 tree compound_stmt;
0a3b29ad 19708 tree try_block;
19709 bool ctor_initializer_p;
19710
19711 /* Look for the `try' keyword. */
c247dce0 19712 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
0a3b29ad 19713 return false;
a17c2a3a 19714 /* Let the rest of the front end know where we are. */
78f7169a 19715 try_block = begin_function_try_block (&compound_stmt);
0a3b29ad 19716 /* Parse the function-body. */
376a817b 19717 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
19718 (parser, /*in_function_try_block=*/true);
0a3b29ad 19719 /* We're done with the `try' part. */
19720 finish_function_try_block (try_block);
19721 /* Parse the handlers. */
19722 cp_parser_handler_seq (parser);
19723 /* We're done with the handlers. */
78f7169a 19724 finish_function_handler_sequence (try_block, compound_stmt);
0a3b29ad 19725
19726 return ctor_initializer_p;
19727}
19728
19729/* Parse a handler-seq.
19730
19731 handler-seq:
19732 handler handler-seq [opt] */
19733
19734static void
45baea8b 19735cp_parser_handler_seq (cp_parser* parser)
0a3b29ad 19736{
19737 while (true)
19738 {
19739 cp_token *token;
19740
19741 /* Parse the handler. */
19742 cp_parser_handler (parser);
19743 /* Peek at the next token. */
19744 token = cp_lexer_peek_token (parser->lexer);
19745 /* If it's not `catch' then there are no more handlers. */
19746 if (!cp_parser_is_keyword (token, RID_CATCH))
19747 break;
19748 }
19749}
19750
19751/* Parse a handler.
19752
19753 handler:
19754 catch ( exception-declaration ) compound-statement */
19755
19756static void
45baea8b 19757cp_parser_handler (cp_parser* parser)
0a3b29ad 19758{
19759 tree handler;
19760 tree declaration;
19761
c247dce0 19762 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
0a3b29ad 19763 handler = begin_handler ();
c247dce0 19764 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 19765 declaration = cp_parser_exception_declaration (parser);
19766 finish_handler_parms (declaration, handler);
c247dce0 19767 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6bf5e78d 19768 cp_parser_compound_statement (parser, NULL, false, false);
0a3b29ad 19769 finish_handler (handler);
19770}
19771
19772/* Parse an exception-declaration.
19773
19774 exception-declaration:
19775 type-specifier-seq declarator
19776 type-specifier-seq abstract-declarator
19777 type-specifier-seq
ccb84981 19778 ...
0a3b29ad 19779
19780 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19781 ellipsis variant is used. */
19782
19783static tree
45baea8b 19784cp_parser_exception_declaration (cp_parser* parser)
0a3b29ad 19785{
4b9b2871 19786 cp_decl_specifier_seq type_specifiers;
3046c0a3 19787 cp_declarator *declarator;
0a3b29ad 19788 const char *saved_message;
19789
19790 /* If it's an ellipsis, it's easy to handle. */
19791 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19792 {
19793 /* Consume the `...' token. */
19794 cp_lexer_consume_token (parser->lexer);
19795 return NULL_TREE;
19796 }
19797
19798 /* Types may not be defined in exception-declarations. */
19799 saved_message = parser->type_definition_forbidden_message;
19800 parser->type_definition_forbidden_message
ca82e026 19801 = G_("types may not be defined in exception-declarations");
0a3b29ad 19802
19803 /* Parse the type-specifier-seq. */
c44f7faf 19804 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
638569c5 19805 /*is_trailing_return=*/false,
6f74fe3c 19806 &type_specifiers);
0a3b29ad 19807 /* If it's a `)', then there is no declarator. */
19808 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
3046c0a3 19809 declarator = NULL;
0a3b29ad 19810 else
42bbd0ec 19811 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
92b128ed 19812 /*ctor_dtor_or_conv_p=*/NULL,
08ea345c 19813 /*parenthesized_p=*/NULL,
19814 /*member_p=*/false);
0a3b29ad 19815
19816 /* Restore the saved message. */
19817 parser->type_definition_forbidden_message = saved_message;
19818
68023786 19819 if (!type_specifiers.any_specifiers_p)
19820 return error_mark_node;
3046c0a3 19821
68023786 19822 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
0a3b29ad 19823}
19824
ccb84981 19825/* Parse a throw-expression.
0a3b29ad 19826
19827 throw-expression:
755edffd 19828 throw assignment-expression [opt]
0a3b29ad 19829
19830 Returns a THROW_EXPR representing the throw-expression. */
19831
19832static tree
45baea8b 19833cp_parser_throw_expression (cp_parser* parser)
0a3b29ad 19834{
19835 tree expression;
fe338aca 19836 cp_token* token;
0a3b29ad 19837
c247dce0 19838 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
fe338aca 19839 token = cp_lexer_peek_token (parser->lexer);
19840 /* Figure out whether or not there is an assignment-expression
19841 following the "throw" keyword. */
19842 if (token->type == CPP_COMMA
19843 || token->type == CPP_SEMICOLON
19844 || token->type == CPP_CLOSE_PAREN
19845 || token->type == CPP_CLOSE_SQUARE
19846 || token->type == CPP_CLOSE_BRACE
19847 || token->type == CPP_COLON)
0a3b29ad 19848 expression = NULL_TREE;
fe338aca 19849 else
640aa28c 19850 expression = cp_parser_assignment_expression (parser,
98b326fd 19851 /*cast_p=*/false, NULL);
0a3b29ad 19852
19853 return build_throw (expression);
19854}
19855
19856/* GNU Extensions */
19857
19858/* Parse an (optional) asm-specification.
19859
19860 asm-specification:
19861 asm ( string-literal )
19862
19863 If the asm-specification is present, returns a STRING_CST
19864 corresponding to the string-literal. Otherwise, returns
19865 NULL_TREE. */
19866
19867static tree
45baea8b 19868cp_parser_asm_specification_opt (cp_parser* parser)
0a3b29ad 19869{
19870 cp_token *token;
19871 tree asm_specification;
19872
19873 /* Peek at the next token. */
19874 token = cp_lexer_peek_token (parser->lexer);
ccb84981 19875 /* If the next token isn't the `asm' keyword, then there's no
0a3b29ad 19876 asm-specification. */
19877 if (!cp_parser_is_keyword (token, RID_ASM))
19878 return NULL_TREE;
19879
19880 /* Consume the `asm' token. */
19881 cp_lexer_consume_token (parser->lexer);
19882 /* Look for the `('. */
c247dce0 19883 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 19884
19885 /* Look for the string-literal. */
00d26680 19886 asm_specification = cp_parser_string_literal (parser, false, false);
0a3b29ad 19887
19888 /* Look for the `)'. */
c247dce0 19889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 19890
19891 return asm_specification;
19892}
19893
ccb84981 19894/* Parse an asm-operand-list.
0a3b29ad 19895
19896 asm-operand-list:
19897 asm-operand
19898 asm-operand-list , asm-operand
ccb84981 19899
0a3b29ad 19900 asm-operand:
ccb84981 19901 string-literal ( expression )
0a3b29ad 19902 [ string-literal ] string-literal ( expression )
19903
19904 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19905 each node is the expression. The TREE_PURPOSE is itself a
19906 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19907 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
a40e70de 19908 is a STRING_CST for the string literal before the parenthesis. Returns
19909 ERROR_MARK_NODE if any of the operands are invalid. */
0a3b29ad 19910
19911static tree
45baea8b 19912cp_parser_asm_operand_list (cp_parser* parser)
0a3b29ad 19913{
19914 tree asm_operands = NULL_TREE;
a40e70de 19915 bool invalid_operands = false;
0a3b29ad 19916
19917 while (true)
19918 {
19919 tree string_literal;
19920 tree expression;
19921 tree name;
ccb84981 19922
ccb84981 19923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
0a3b29ad 19924 {
19925 /* Consume the `[' token. */
19926 cp_lexer_consume_token (parser->lexer);
19927 /* Read the operand name. */
19928 name = cp_parser_identifier (parser);
ccb84981 19929 if (name != error_mark_node)
0a3b29ad 19930 name = build_string (IDENTIFIER_LENGTH (name),
19931 IDENTIFIER_POINTER (name));
19932 /* Look for the closing `]'. */
c247dce0 19933 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
0a3b29ad 19934 }
19935 else
19936 name = NULL_TREE;
19937 /* Look for the string-literal. */
00d26680 19938 string_literal = cp_parser_string_literal (parser, false, false);
19939
0a3b29ad 19940 /* Look for the `('. */
c247dce0 19941 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 19942 /* Parse the expression. */
98b326fd 19943 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
0a3b29ad 19944 /* Look for the `)'. */
c247dce0 19945 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
00d26680 19946
a40e70de 19947 if (name == error_mark_node
19948 || string_literal == error_mark_node
19949 || expression == error_mark_node)
19950 invalid_operands = true;
19951
0a3b29ad 19952 /* Add this operand to the list. */
19953 asm_operands = tree_cons (build_tree_list (name, string_literal),
ccb84981 19954 expression,
0a3b29ad 19955 asm_operands);
ccb84981 19956 /* If the next token is not a `,', there are no more
0a3b29ad 19957 operands. */
19958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19959 break;
19960 /* Consume the `,'. */
19961 cp_lexer_consume_token (parser->lexer);
19962 }
19963
a40e70de 19964 return invalid_operands ? error_mark_node : nreverse (asm_operands);
0a3b29ad 19965}
19966
ccb84981 19967/* Parse an asm-clobber-list.
0a3b29ad 19968
19969 asm-clobber-list:
19970 string-literal
ccb84981 19971 asm-clobber-list , string-literal
0a3b29ad 19972
19973 Returns a TREE_LIST, indicating the clobbers in the order that they
19974 appeared. The TREE_VALUE of each node is a STRING_CST. */
19975
19976static tree
45baea8b 19977cp_parser_asm_clobber_list (cp_parser* parser)
0a3b29ad 19978{
19979 tree clobbers = NULL_TREE;
19980
19981 while (true)
19982 {
0a3b29ad 19983 tree string_literal;
19984
19985 /* Look for the string literal. */
00d26680 19986 string_literal = cp_parser_string_literal (parser, false, false);
0a3b29ad 19987 /* Add it to the list. */
19988 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
ccb84981 19989 /* If the next token is not a `,', then the list is
0a3b29ad 19990 complete. */
19991 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19992 break;
19993 /* Consume the `,' token. */
19994 cp_lexer_consume_token (parser->lexer);
19995 }
19996
19997 return clobbers;
19998}
19999
78f55ca8 20000/* Parse an asm-label-list.
20001
20002 asm-label-list:
20003 identifier
20004 asm-label-list , identifier
20005
20006 Returns a TREE_LIST, indicating the labels in the order that they
20007 appeared. The TREE_VALUE of each node is a label. */
20008
20009static tree
20010cp_parser_asm_label_list (cp_parser* parser)
20011{
20012 tree labels = NULL_TREE;
20013
20014 while (true)
20015 {
20016 tree identifier, label, name;
20017
20018 /* Look for the identifier. */
20019 identifier = cp_parser_identifier (parser);
20020 if (!error_operand_p (identifier))
20021 {
20022 label = lookup_label (identifier);
20023 if (TREE_CODE (label) == LABEL_DECL)
20024 {
20025 TREE_USED (label) = 1;
20026 check_goto (label);
20027 name = build_string (IDENTIFIER_LENGTH (identifier),
20028 IDENTIFIER_POINTER (identifier));
20029 labels = tree_cons (name, label, labels);
20030 }
20031 }
20032 /* If the next token is not a `,', then the list is
20033 complete. */
20034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20035 break;
20036 /* Consume the `,' token. */
20037 cp_lexer_consume_token (parser->lexer);
20038 }
20039
20040 return nreverse (labels);
20041}
20042
0a3b29ad 20043/* Parse an (optional) series of attributes.
20044
20045 attributes:
20046 attributes attribute
20047
20048 attribute:
ccb84981 20049 __attribute__ (( attribute-list [opt] ))
0a3b29ad 20050
20051 The return value is as for cp_parser_attribute_list. */
ccb84981 20052
0a3b29ad 20053static tree
45baea8b 20054cp_parser_attributes_opt (cp_parser* parser)
0a3b29ad 20055{
20056 tree attributes = NULL_TREE;
20057
20058 while (true)
20059 {
20060 cp_token *token;
20061 tree attribute_list;
793fb8f3 20062 bool ok = true;
0a3b29ad 20063
20064 /* Peek at the next token. */
20065 token = cp_lexer_peek_token (parser->lexer);
20066 /* If it's not `__attribute__', then we're done. */
20067 if (token->keyword != RID_ATTRIBUTE)
20068 break;
20069
20070 /* Consume the `__attribute__' keyword. */
20071 cp_lexer_consume_token (parser->lexer);
20072 /* Look for the two `(' tokens. */
c247dce0 20073 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20074 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
0a3b29ad 20075
20076 /* Peek at the next token. */
20077 token = cp_lexer_peek_token (parser->lexer);
20078 if (token->type != CPP_CLOSE_PAREN)
20079 /* Parse the attribute-list. */
20080 attribute_list = cp_parser_attribute_list (parser);
20081 else
20082 /* If the next token is a `)', then there is no attribute
20083 list. */
20084 attribute_list = NULL;
20085
20086 /* Look for the two `)' tokens. */
793fb8f3 20087 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20088 ok = false;
20089 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
20090 ok = false;
20091 if (!ok)
20092 cp_parser_skip_to_end_of_statement (parser);
0a3b29ad 20093
20094 /* Add these new attributes to the list. */
20095 attributes = chainon (attributes, attribute_list);
20096 }
20097
20098 return attributes;
20099}
20100
ccb84981 20101/* Parse an attribute-list.
0a3b29ad 20102
ccb84981 20103 attribute-list:
20104 attribute
0a3b29ad 20105 attribute-list , attribute
20106
20107 attribute:
ccb84981 20108 identifier
0a3b29ad 20109 identifier ( identifier )
20110 identifier ( identifier , expression-list )
ccb84981 20111 identifier ( expression-list )
0a3b29ad 20112
f0d4a607 20113 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
20114 to an attribute. The TREE_PURPOSE of each node is the identifier
20115 indicating which attribute is in use. The TREE_VALUE represents
20116 the arguments, if any. */
0a3b29ad 20117
20118static tree
45baea8b 20119cp_parser_attribute_list (cp_parser* parser)
0a3b29ad 20120{
20121 tree attribute_list = NULL_TREE;
00d26680 20122 bool save_translate_strings_p = parser->translate_strings_p;
0a3b29ad 20123
00d26680 20124 parser->translate_strings_p = false;
0a3b29ad 20125 while (true)
20126 {
20127 cp_token *token;
20128 tree identifier;
20129 tree attribute;
20130
20131 /* Look for the identifier. We also allow keywords here; for
20132 example `__attribute__ ((const))' is legal. */
20133 token = cp_lexer_peek_token (parser->lexer);
f0d4a607 20134 if (token->type == CPP_NAME
20135 || token->type == CPP_KEYWORD)
20136 {
ce601f92 20137 tree arguments = NULL_TREE;
20138
f0d4a607 20139 /* Consume the token. */
20140 token = cp_lexer_consume_token (parser->lexer);
ccb84981 20141
f0d4a607 20142 /* Save away the identifier that indicates which attribute
9031d10b 20143 this is. */
d3ca4ed0 20144 identifier = (token->type == CPP_KEYWORD)
20145 /* For keywords, use the canonical spelling, not the
20146 parsed identifier. */
20147 ? ridpointers[(int) token->keyword]
20148 : token->u.value;
20149
f0d4a607 20150 attribute = build_tree_list (identifier, NULL_TREE);
0a3b29ad 20151
f0d4a607 20152 /* Peek at the next token. */
20153 token = cp_lexer_peek_token (parser->lexer);
20154 /* If it's an `(', then parse the attribute arguments. */
20155 if (token->type == CPP_OPEN_PAREN)
20156 {
f352a3fb 20157 VEC(tree,gc) *vec;
33199a81 20158 int attr_flag = (attribute_takes_identifier_p (identifier)
20159 ? id_attr : normal_attr);
f352a3fb 20160 vec = cp_parser_parenthesized_expression_list
33199a81 20161 (parser, attr_flag, /*cast_p=*/false,
f352a3fb 20162 /*allow_expansion_p=*/false,
20163 /*non_constant_p=*/NULL);
20164 if (vec == NULL)
20165 arguments = error_mark_node;
20166 else
20167 {
20168 arguments = build_tree_list_vec (vec);
20169 release_tree_vector (vec);
20170 }
ce601f92 20171 /* Save the arguments away. */
f0d4a607 20172 TREE_VALUE (attribute) = arguments;
20173 }
0a3b29ad 20174
ce601f92 20175 if (arguments != error_mark_node)
20176 {
20177 /* Add this attribute to the list. */
20178 TREE_CHAIN (attribute) = attribute_list;
20179 attribute_list = attribute;
20180 }
0a3b29ad 20181
f0d4a607 20182 token = cp_lexer_peek_token (parser->lexer);
20183 }
20184 /* Now, look for more attributes. If the next token isn't a
20185 `,', we're done. */
0a3b29ad 20186 if (token->type != CPP_COMMA)
20187 break;
20188
63eff20d 20189 /* Consume the comma and keep going. */
0a3b29ad 20190 cp_lexer_consume_token (parser->lexer);
20191 }
00d26680 20192 parser->translate_strings_p = save_translate_strings_p;
0a3b29ad 20193
20194 /* We built up the list in reverse order. */
20195 return nreverse (attribute_list);
20196}
20197
20198/* Parse an optional `__extension__' keyword. Returns TRUE if it is
20199 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20200 current value of the PEDANTIC flag, regardless of whether or not
20201 the `__extension__' keyword is present. The caller is responsible
20202 for restoring the value of the PEDANTIC flag. */
20203
20204static bool
45baea8b 20205cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
0a3b29ad 20206{
20207 /* Save the old value of the PEDANTIC flag. */
20208 *saved_pedantic = pedantic;
20209
20210 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20211 {
20212 /* Consume the `__extension__' token. */
20213 cp_lexer_consume_token (parser->lexer);
20214 /* We're not being pedantic while the `__extension__' keyword is
20215 in effect. */
20216 pedantic = 0;
20217
20218 return true;
20219 }
20220
20221 return false;
20222}
20223
20224/* Parse a label declaration.
20225
20226 label-declaration:
20227 __label__ label-declarator-seq ;
20228
20229 label-declarator-seq:
20230 identifier , label-declarator-seq
20231 identifier */
20232
20233static void
45baea8b 20234cp_parser_label_declaration (cp_parser* parser)
0a3b29ad 20235{
20236 /* Look for the `__label__' keyword. */
c247dce0 20237 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
0a3b29ad 20238
20239 while (true)
20240 {
20241 tree identifier;
20242
20243 /* Look for an identifier. */
20244 identifier = cp_parser_identifier (parser);
f7d1c2ea 20245 /* If we failed, stop. */
20246 if (identifier == error_mark_node)
20247 break;
20248 /* Declare it as a label. */
0a3b29ad 20249 finish_label_decl (identifier);
20250 /* If the next token is a `;', stop. */
20251 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20252 break;
20253 /* Look for the `,' separating the label declarations. */
c247dce0 20254 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
0a3b29ad 20255 }
20256
20257 /* Look for the final `;'. */
c247dce0 20258 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
0a3b29ad 20259}
20260
20261/* Support Functions */
20262
20263/* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20264 NAME should have one of the representations used for an
20265 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20266 is returned. If PARSER->SCOPE is a dependent type, then a
20267 SCOPE_REF is returned.
20268
20269 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20270 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20271 was formed. Abstractly, such entities should not be passed to this
20272 function, because they do not need to be looked up, but it is
20273 simpler to check for this special case here, rather than at the
20274 call-sites.
20275
20276 In cases not explicitly covered above, this function returns a
20277 DECL, OVERLOAD, or baselink representing the result of the lookup.
20278 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20279 is returned.
20280
0be5f5cc 20281 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
e2ae55f2 20282 (e.g., "struct") that was used. In that case bindings that do not
20283 refer to types are ignored.
0a3b29ad 20284
c3b9e457 20285 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20286 ignored.
20287
6fc758aa 20288 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20289 are ignored.
20290
0a3b29ad 20291 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
9031d10b 20292 types.
2cdbcd51 20293
b62240d5 20294 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
149fdb98 20295 TREE_LIST of candidates if name-lookup results in an ambiguity, and
074ab442 20296 NULL_TREE otherwise. */
0a3b29ad 20297
20298static tree
ccb84981 20299cp_parser_lookup_name (cp_parser *parser, tree name,
e2ae55f2 20300 enum tag_types tag_type,
074ab442 20301 bool is_template,
fbb01da7 20302 bool is_namespace,
2cdbcd51 20303 bool check_dependency,
ad9ae192 20304 tree *ambiguous_decls,
20305 location_t name_location)
0a3b29ad 20306{
0e8ce9be 20307 int flags = 0;
0a3b29ad 20308 tree decl;
20309 tree object_type = parser->context->object_type;
20310
0e8ce9be 20311 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20312 flags |= LOOKUP_COMPLAIN;
20313
2cdbcd51 20314 /* Assume that the lookup will be unambiguous. */
b62240d5 20315 if (ambiguous_decls)
20316 *ambiguous_decls = NULL_TREE;
2cdbcd51 20317
0a3b29ad 20318 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20319 no longer valid. Note that if we are parsing tentatively, and
20320 the parse fails, OBJECT_TYPE will be automatically restored. */
20321 parser->context->object_type = NULL_TREE;
20322
20323 if (name == error_mark_node)
20324 return error_mark_node;
20325
20326 /* A template-id has already been resolved; there is no lookup to
20327 do. */
20328 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20329 return name;
20330 if (BASELINK_P (name))
20331 {
b4df430b 20332 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20333 == TEMPLATE_ID_EXPR);
0a3b29ad 20334 return name;
20335 }
20336
20337 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20338 it should already have been checked to make sure that the name
20339 used matches the type being destroyed. */
20340 if (TREE_CODE (name) == BIT_NOT_EXPR)
20341 {
20342 tree type;
20343
20344 /* Figure out to which type this destructor applies. */
20345 if (parser->scope)
20346 type = parser->scope;
20347 else if (object_type)
20348 type = object_type;
20349 else
20350 type = current_class_type;
20351 /* If that's not a class type, there is no destructor. */
20352 if (!type || !CLASS_TYPE_P (type))
20353 return error_mark_node;
ed36f1cf 20354 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20355 lazily_declare_fn (sfk_destructor, type);
2b40dfce 20356 if (!CLASSTYPE_DESTRUCTORS (type))
20357 return error_mark_node;
0a3b29ad 20358 /* If it was a class type, return the destructor. */
20359 return CLASSTYPE_DESTRUCTORS (type);
20360 }
20361
20362 /* By this point, the NAME should be an ordinary identifier. If
20363 the id-expression was a qualified name, the qualifying scope is
20364 stored in PARSER->SCOPE at this point. */
b4df430b 20365 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
ccb84981 20366
0a3b29ad 20367 /* Perform the lookup. */
20368 if (parser->scope)
ccb84981 20369 {
7e9a6a16 20370 bool dependent_p;
0a3b29ad 20371
20372 if (parser->scope == error_mark_node)
20373 return error_mark_node;
20374
20375 /* If the SCOPE is dependent, the lookup must be deferred until
20376 the template is instantiated -- unless we are explicitly
20377 looking up names in uninstantiated templates. Even then, we
20378 cannot look up the name if the scope is not a class type; it
20379 might, for example, be a template type parameter. */
7e9a6a16 20380 dependent_p = (TYPE_P (parser->scope)
05f701e2 20381 && dependent_scope_p (parser->scope));
0a3b29ad 20382 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
05f701e2 20383 && dependent_p)
20384 /* Defer lookup. */
20385 decl = error_mark_node;
0a3b29ad 20386 else
20387 {
7f602bca 20388 tree pushed_scope = NULL_TREE;
1cbda81f 20389
0a3b29ad 20390 /* If PARSER->SCOPE is a dependent type, then it must be a
20391 class type, and we must not be checking dependencies;
20392 otherwise, we would have processed this lookup above. So
20393 that PARSER->SCOPE is not considered a dependent base by
20394 lookup_member, we must enter the scope here. */
7e9a6a16 20395 if (dependent_p)
7f602bca 20396 pushed_scope = push_scope (parser->scope);
a70e3c37 20397
49c8ed9f 20398 /* If the PARSER->SCOPE is a template specialization, it
20399 may be instantiated during name lookup. In that case,
20400 errors may be issued. Even if we rollback the current
20401 tentative parse, those errors are valid. */
20402 decl = lookup_qualified_name (parser->scope, name,
20403 tag_type != none_type,
20404 /*complain=*/true);
20405
a70e3c37 20406 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20407 lookup result and the nested-name-specifier nominates a class C:
20408 * if the name specified after the nested-name-specifier, when
20409 looked up in C, is the injected-class-name of C (Clause 9), or
20410 * if the name specified after the nested-name-specifier is the
20411 same as the identifier or the simple-template-id's template-
20412 name in the last component of the nested-name-specifier,
20413 the name is instead considered to name the constructor of
20414 class C. [ Note: for example, the constructor is not an
20415 acceptable lookup result in an elaborated-type-specifier so
20416 the constructor would not be used in place of the
20417 injected-class-name. --end note ] Such a constructor name
20418 shall be used only in the declarator-id of a declaration that
20419 names a constructor or in a using-declaration. */
20420 if (tag_type == none_type
49c8ed9f 20421 && DECL_SELF_REFERENCE_P (decl)
20422 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20423 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20424 tag_type != none_type,
20425 /*complain=*/true);
93a3c02a 20426
20427 /* If we have a single function from a using decl, pull it out. */
05f701e2 20428 if (TREE_CODE (decl) == OVERLOAD
93a3c02a 20429 && !really_overloaded_fn (decl))
20430 decl = OVL_FUNCTION (decl);
20431
7f602bca 20432 if (pushed_scope)
20433 pop_scope (pushed_scope);
0a3b29ad 20434 }
05f701e2 20435
20436 /* If the scope is a dependent type and either we deferred lookup or
20437 we did lookup but didn't find the name, rememeber the name. */
20438 if (decl == error_mark_node && TYPE_P (parser->scope)
20439 && dependent_type_p (parser->scope))
20440 {
20441 if (tag_type)
20442 {
20443 tree type;
20444
20445 /* The resolution to Core Issue 180 says that `struct
20446 A::B' should be considered a type-name, even if `A'
20447 is dependent. */
20448 type = make_typename_type (parser->scope, name, tag_type,
20449 /*complain=*/tf_error);
20450 decl = TYPE_NAME (type);
20451 }
20452 else if (is_template
20453 && (cp_parser_next_token_ends_template_argument_p (parser)
20454 || cp_lexer_next_token_is (parser->lexer,
20455 CPP_CLOSE_PAREN)))
20456 decl = make_unbound_class_template (parser->scope,
20457 name, NULL_TREE,
20458 /*complain=*/tf_error);
20459 else
20460 decl = build_qualified_name (/*type=*/NULL_TREE,
20461 parser->scope, name,
20462 is_template);
20463 }
0a3b29ad 20464 parser->qualifying_scope = parser->scope;
20465 parser->object_scope = NULL_TREE;
20466 }
20467 else if (object_type)
20468 {
20469 tree object_decl = NULL_TREE;
20470 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20471 OBJECT_TYPE is not a class. */
20472 if (CLASS_TYPE_P (object_type))
20473 /* If the OBJECT_TYPE is a template specialization, it may
20474 be instantiated during name lookup. In that case, errors
20475 may be issued. Even if we rollback the current tentative
20476 parse, those errors are valid. */
20477 object_decl = lookup_member (object_type,
20478 name,
9031d10b 20479 /*protect=*/0,
2cbaacd9 20480 tag_type != none_type,
20481 tf_warning_or_error);
0a3b29ad 20482 /* Look it up in the enclosing context, too. */
9031d10b 20483 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 20484 /*nonclass=*/0,
0e8ce9be 20485 /*block_p=*/true, is_namespace, flags);
0a3b29ad 20486 parser->object_scope = object_type;
20487 parser->qualifying_scope = NULL_TREE;
20488 if (object_decl)
20489 decl = object_decl;
20490 }
20491 else
20492 {
9031d10b 20493 decl = lookup_name_real (name, tag_type != none_type,
e2ae55f2 20494 /*nonclass=*/0,
0e8ce9be 20495 /*block_p=*/true, is_namespace, flags);
0a3b29ad 20496 parser->qualifying_scope = NULL_TREE;
20497 parser->object_scope = NULL_TREE;
20498 }
20499
20500 /* If the lookup failed, let our caller know. */
3f3fa556 20501 if (!decl || decl == error_mark_node)
0a3b29ad 20502 return error_mark_node;
20503
6dd3a38d 20504 /* Pull out the template from an injected-class-name (or multiple). */
20505 if (is_template)
20506 decl = maybe_get_template_decl_from_type_decl (decl);
20507
0a3b29ad 20508 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20509 if (TREE_CODE (decl) == TREE_LIST)
20510 {
b62240d5 20511 if (ambiguous_decls)
20512 *ambiguous_decls = decl;
0a3b29ad 20513 /* The error message we have to print is too complicated for
20514 cp_parser_error, so we incorporate its actions directly. */
2c593bd0 20515 if (!cp_parser_simulate_error (parser))
0a3b29ad 20516 {
ccb59bb6 20517 error_at (name_location, "reference to %qD is ambiguous",
20518 name);
0a3b29ad 20519 print_candidates (decl);
20520 }
20521 return error_mark_node;
20522 }
20523
b4df430b 20524 gcc_assert (DECL_P (decl)
20525 || TREE_CODE (decl) == OVERLOAD
20526 || TREE_CODE (decl) == SCOPE_REF
20527 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20528 || BASELINK_P (decl));
0a3b29ad 20529
20530 /* If we have resolved the name of a member declaration, check to
20531 see if the declaration is accessible. When the name resolves to
755edffd 20532 set of overloaded functions, accessibility is checked when
ccb84981 20533 overload resolution is done.
0a3b29ad 20534
20535 During an explicit instantiation, access is not checked at all,
20536 as per [temp.explicit]. */
4f62c42e 20537 if (DECL_P (decl))
ef4534a3 20538 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
0a3b29ad 20539
a4e3ffad 20540 maybe_record_typedef_use (decl);
20541
0a3b29ad 20542 return decl;
20543}
20544
20545/* Like cp_parser_lookup_name, but for use in the typical case where
c3b9e457 20546 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20547 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
0a3b29ad 20548
20549static tree
ad9ae192 20550cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
0a3b29ad 20551{
ccb84981 20552 return cp_parser_lookup_name (parser, name,
e2ae55f2 20553 none_type,
c3b9e457 20554 /*is_template=*/false,
6fc758aa 20555 /*is_namespace=*/false,
2cdbcd51 20556 /*check_dependency=*/true,
ad9ae192 20557 /*ambiguous_decls=*/NULL,
20558 location);
0a3b29ad 20559}
20560
0a3b29ad 20561/* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20562 the current context, return the TYPE_DECL. If TAG_NAME_P is
20563 true, the DECL indicates the class being defined in a class-head,
20564 or declared in an elaborated-type-specifier.
20565
20566 Otherwise, return DECL. */
20567
20568static tree
20569cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20570{
a5e50b31 20571 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20572 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
0a3b29ad 20573
ccb84981 20574 struct A {
653e5405 20575 template <typename T> struct B;
0a3b29ad 20576 };
20577
ccb84981 20578 template <typename T> struct A::B {};
20579
e4bc96e2 20580 Similarly, in an elaborated-type-specifier:
0a3b29ad 20581
20582 namespace N { struct X{}; }
20583
20584 struct A {
653e5405 20585 template <typename T> friend struct N::X;
0a3b29ad 20586 };
20587
a5e50b31 20588 However, if the DECL refers to a class type, and we are in
20589 the scope of the class, then the name lookup automatically
20590 finds the TYPE_DECL created by build_self_reference rather
20591 than a TEMPLATE_DECL. For example, in:
20592
20593 template <class T> struct S {
653e5405 20594 S s;
a5e50b31 20595 };
20596
20597 there is no need to handle such case. */
20598
20599 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
0a3b29ad 20600 return DECL_TEMPLATE_RESULT (decl);
20601
20602 return decl;
20603}
20604
20605/* If too many, or too few, template-parameter lists apply to the
20606 declarator, issue an error message. Returns TRUE if all went well,
20607 and FALSE otherwise. */
20608
20609static bool
ccb84981 20610cp_parser_check_declarator_template_parameters (cp_parser* parser,
ad9ae192 20611 cp_declarator *declarator,
20612 location_t declarator_location)
0a3b29ad 20613{
20614 unsigned num_templates;
20615
20616 /* We haven't seen any classes that involve template parameters yet. */
20617 num_templates = 0;
20618
3046c0a3 20619 switch (declarator->kind)
0a3b29ad 20620 {
3046c0a3 20621 case cdk_id:
2ded3667 20622 if (declarator->u.id.qualifying_scope)
3046c0a3 20623 {
20624 tree scope;
0a3b29ad 20625
2ded3667 20626 scope = declarator->u.id.qualifying_scope;
0a3b29ad 20627
3046c0a3 20628 while (scope && CLASS_TYPE_P (scope))
20629 {
20630 /* You're supposed to have one `template <...>'
20631 for every template class, but you don't need one
20632 for a full specialization. For example:
20633
20634 template <class T> struct S{};
20635 template <> struct S<int> { void f(); };
20636 void S<int>::f () {}
20637
20638 is correct; there shouldn't be a `template <>' for
20639 the definition of `S<int>::f'. */
04ef83b7 20640 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20641 /* If SCOPE does not have template information of any
20642 kind, then it is not a template, nor is it nested
20643 within a template. */
20644 break;
20645 if (explicit_class_specialization_p (scope))
20646 break;
20647 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
3046c0a3 20648 ++num_templates;
20649
20650 scope = TYPE_CONTEXT (scope);
20651 }
20652 }
9031d10b 20653 else if (TREE_CODE (declarator->u.id.unqualified_name)
2ded3667 20654 == TEMPLATE_ID_EXPR)
20655 /* If the DECLARATOR has the form `X<y>' then it uses one
20656 additional level of template parameters. */
0a3b29ad 20657 ++num_templates;
20658
7b07a15e 20659 return cp_parser_check_template_parameters
20660 (parser, num_templates, declarator_location, declarator);
20661
3046c0a3 20662
20663 case cdk_function:
20664 case cdk_array:
20665 case cdk_pointer:
20666 case cdk_reference:
20667 case cdk_ptrmem:
207355ad 20668 return (cp_parser_check_declarator_template_parameters
ad9ae192 20669 (parser, declarator->declarator, declarator_location));
3046c0a3 20670
20671 case cdk_error:
20672 return true;
20673
20674 default:
2e3e31d2 20675 gcc_unreachable ();
0a3b29ad 20676 }
2e3e31d2 20677 return false;
0a3b29ad 20678}
20679
20680/* NUM_TEMPLATES were used in the current declaration. If that is
20681 invalid, return FALSE and issue an error messages. Otherwise,
7b07a15e 20682 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20683 declarator and we can print more accurate diagnostics. */
0a3b29ad 20684
20685static bool
45baea8b 20686cp_parser_check_template_parameters (cp_parser* parser,
ad9ae192 20687 unsigned num_templates,
7b07a15e 20688 location_t location,
20689 cp_declarator *declarator)
0a3b29ad 20690{
7b07a15e 20691 /* If there are the same number of template classes and parameter
20692 lists, that's OK. */
20693 if (parser->num_template_parameter_lists == num_templates)
20694 return true;
20695 /* If there are more, but only one more, then we are referring to a
20696 member template. That's OK too. */
20697 if (parser->num_template_parameter_lists == num_templates + 1)
20698 return true;
0a3b29ad 20699 /* If there are more template classes than parameter lists, we have
20700 something like:
ccb84981 20701
0a3b29ad 20702 template <class T> void S<T>::R<T>::f (); */
20703 if (parser->num_template_parameter_lists < num_templates)
20704 {
5bd9bf06 20705 if (declarator && !current_function_decl)
7b07a15e 20706 error_at (location, "specializing member %<%T::%E%> "
20707 "requires %<template<>%> syntax",
20708 declarator->u.id.qualifying_scope,
20709 declarator->u.id.unqualified_name);
5bd9bf06 20710 else if (declarator)
20711 error_at (location, "invalid declaration of %<%T::%E%>",
20712 declarator->u.id.qualifying_scope,
20713 declarator->u.id.unqualified_name);
7b07a15e 20714 else
20715 error_at (location, "too few template-parameter-lists");
0a3b29ad 20716 return false;
20717 }
0a3b29ad 20718 /* Otherwise, there are too many template parameter lists. We have
20719 something like:
20720
20721 template <class T> template <class U> void S::f(); */
ccb59bb6 20722 error_at (location, "too many template-parameter-lists");
0a3b29ad 20723 return false;
20724}
20725
0a3b29ad 20726/* Parse an optional `::' token indicating that the following name is
20727 from the global namespace. If so, PARSER->SCOPE is set to the
20728 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20729 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20730 Returns the new value of PARSER->SCOPE, if the `::' token is
20731 present, and NULL_TREE otherwise. */
20732
20733static tree
130bb1d4 20734cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
0a3b29ad 20735{
20736 cp_token *token;
20737
20738 /* Peek at the next token. */
20739 token = cp_lexer_peek_token (parser->lexer);
20740 /* If we're looking at a `::' token then we're starting from the
20741 global namespace, not our current location. */
20742 if (token->type == CPP_SCOPE)
20743 {
20744 /* Consume the `::' token. */
20745 cp_lexer_consume_token (parser->lexer);
20746 /* Set the SCOPE so that we know where to start the lookup. */
20747 parser->scope = global_namespace;
20748 parser->qualifying_scope = global_namespace;
20749 parser->object_scope = NULL_TREE;
20750
20751 return parser->scope;
20752 }
130bb1d4 20753 else if (!current_scope_valid_p)
0a3b29ad 20754 {
20755 parser->scope = NULL_TREE;
20756 parser->qualifying_scope = NULL_TREE;
130bb1d4 20757 parser->object_scope = NULL_TREE;
0a3b29ad 20758 }
20759
20760 return NULL_TREE;
20761}
20762
20763/* Returns TRUE if the upcoming token sequence is the start of a
20764 constructor declarator. If FRIEND_P is true, the declarator is
20765 preceded by the `friend' specifier. */
20766
20767static bool
20768cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20769{
20770 bool constructor_p;
a70e3c37 20771 tree nested_name_specifier;
b3c48b5d 20772 cp_token *next_token;
20773
20774 /* The common case is that this is not a constructor declarator, so
954ad420 20775 try to avoid doing lots of work if at all possible. It's not
20776 valid declare a constructor at function scope. */
0aeb1cc5 20777 if (parser->in_function_body)
954ad420 20778 return false;
20779 /* And only certain tokens can begin a constructor declarator. */
b3c48b5d 20780 next_token = cp_lexer_peek_token (parser->lexer);
20781 if (next_token->type != CPP_NAME
20782 && next_token->type != CPP_SCOPE
20783 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20784 && next_token->type != CPP_TEMPLATE_ID)
20785 return false;
0a3b29ad 20786
20787 /* Parse tentatively; we are going to roll back all of the tokens
20788 consumed here. */
20789 cp_parser_parse_tentatively (parser);
20790 /* Assume that we are looking at a constructor declarator. */
20791 constructor_p = true;
4f62c42e 20792
0a3b29ad 20793 /* Look for the optional `::' operator. */
20794 cp_parser_global_scope_opt (parser,
130bb1d4 20795 /*current_scope_valid_p=*/false);
0a3b29ad 20796 /* Look for the nested-name-specifier. */
a70e3c37 20797 nested_name_specifier
0a3b29ad 20798 = (cp_parser_nested_name_specifier_opt (parser,
20799 /*typename_keyword_p=*/false,
20800 /*check_dependency_p=*/false,
3d0f901b 20801 /*type_p=*/false,
a70e3c37 20802 /*is_declaration=*/false));
0a3b29ad 20803 /* Outside of a class-specifier, there must be a
20804 nested-name-specifier. */
a70e3c37 20805 if (!nested_name_specifier &&
0a3b29ad 20806 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20807 || friend_p))
20808 constructor_p = false;
a70e3c37 20809 else if (nested_name_specifier == error_mark_node)
20810 constructor_p = false;
20811
20812 /* If we have a class scope, this is easy; DR 147 says that S::S always
20813 names the constructor, and no other qualified name could. */
20814 if (constructor_p && nested_name_specifier
e2f75d44 20815 && CLASS_TYPE_P (nested_name_specifier))
a70e3c37 20816 {
20817 tree id = cp_parser_unqualified_id (parser,
20818 /*template_keyword_p=*/false,
20819 /*check_dependency_p=*/false,
20820 /*declarator_p=*/true,
20821 /*optional_p=*/false);
20822 if (is_overloaded_fn (id))
20823 id = DECL_NAME (get_first_fn (id));
20824 if (!constructor_name_p (id, nested_name_specifier))
20825 constructor_p = false;
20826 }
0a3b29ad 20827 /* If we still think that this might be a constructor-declarator,
20828 look for a class-name. */
a70e3c37 20829 else if (constructor_p)
0a3b29ad 20830 {
20831 /* If we have:
20832
a70e3c37 20833 template <typename T> struct S {
20834 S();
20835 };
0a3b29ad 20836
a70e3c37 20837 we must recognize that the nested `S' names a class. */
20838 tree type_decl;
0a3b29ad 20839 type_decl = cp_parser_class_name (parser,
20840 /*typename_keyword_p=*/false,
20841 /*template_keyword_p=*/false,
e2ae55f2 20842 none_type,
0a3b29ad 20843 /*check_dependency_p=*/false,
3d0f901b 20844 /*class_head_p=*/false,
20845 /*is_declaration=*/false);
0a3b29ad 20846 /* If there was no class-name, then this is not a constructor. */
20847 constructor_p = !cp_parser_error_occurred (parser);
4f62c42e 20848
a70e3c37 20849 /* If we're still considering a constructor, we have to see a `(',
20850 to begin the parameter-declaration-clause, followed by either a
20851 `)', an `...', or a decl-specifier. We need to check for a
20852 type-specifier to avoid being fooled into thinking that:
0a3b29ad 20853
a70e3c37 20854 S (f) (int);
0a3b29ad 20855
a70e3c37 20856 is a constructor. (It is actually a function named `f' that
20857 takes one parameter (of type `int') and returns a value of type
20858 `S'. */
20859 if (constructor_p
c247dce0 20860 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
a70e3c37 20861 constructor_p = false;
20862
20863 if (constructor_p
20864 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
0a3b29ad 20865 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
e67a67ea 20866 /* A parameter declaration begins with a decl-specifier,
20867 which is either the "attribute" keyword, a storage class
20868 specifier, or (usually) a type-specifier. */
9a7c4b43 20869 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
0a3b29ad 20870 {
8c1f65e6 20871 tree type;
7f602bca 20872 tree pushed_scope = NULL_TREE;
bb91f165 20873 unsigned saved_num_template_parameter_lists;
8c1f65e6 20874
20875 /* Names appearing in the type-specifier should be looked up
20876 in the scope of the class. */
20877 if (current_class_type)
20878 type = NULL_TREE;
0a3b29ad 20879 else
20880 {
8c1f65e6 20881 type = TREE_TYPE (type_decl);
20882 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 20883 {
ccb84981 20884 type = resolve_typename_type (type,
5f6526e1 20885 /*only_current_p=*/false);
8826a863 20886 if (TREE_CODE (type) == TYPENAME_TYPE)
5f6526e1 20887 {
20888 cp_parser_abort_tentative_parse (parser);
20889 return false;
20890 }
20891 }
7f602bca 20892 pushed_scope = push_scope (type);
0a3b29ad 20893 }
bb91f165 20894
20895 /* Inside the constructor parameter list, surrounding
20896 template-parameter-lists do not apply. */
20897 saved_num_template_parameter_lists
20898 = parser->num_template_parameter_lists;
20899 parser->num_template_parameter_lists = 0;
20900
8c1f65e6 20901 /* Look for the type-specifier. */
20902 cp_parser_type_specifier (parser,
20903 CP_PARSER_FLAGS_NONE,
4b9b2871 20904 /*decl_specs=*/NULL,
8c1f65e6 20905 /*is_declarator=*/true,
20906 /*declares_class_or_enum=*/NULL,
20907 /*is_cv_qualifier=*/NULL);
bb91f165 20908
20909 parser->num_template_parameter_lists
20910 = saved_num_template_parameter_lists;
20911
8c1f65e6 20912 /* Leave the scope of the class. */
7f602bca 20913 if (pushed_scope)
20914 pop_scope (pushed_scope);
8c1f65e6 20915
20916 constructor_p = !cp_parser_error_occurred (parser);
0a3b29ad 20917 }
20918 }
a70e3c37 20919
0a3b29ad 20920 /* We did not really want to consume any tokens. */
20921 cp_parser_abort_tentative_parse (parser);
20922
20923 return constructor_p;
20924}
20925
20926/* Parse the definition of the function given by the DECL_SPECIFIERS,
9b57b06b 20927 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
0a3b29ad 20928 they must be performed once we are in the scope of the function.
20929
20930 Returns the function defined. */
20931
20932static tree
20933cp_parser_function_definition_from_specifiers_and_declarator
45baea8b 20934 (cp_parser* parser,
4b9b2871 20935 cp_decl_specifier_seq *decl_specifiers,
45baea8b 20936 tree attributes,
3046c0a3 20937 const cp_declarator *declarator)
0a3b29ad 20938{
20939 tree fn;
20940 bool success_p;
20941
20942 /* Begin the function-definition. */
3046c0a3 20943 success_p = start_function (decl_specifiers, declarator, attributes);
20944
20945 /* The things we're about to see are not directly qualified by any
20946 template headers we've seen thus far. */
20947 reset_specialization ();
0a3b29ad 20948
20949 /* If there were names looked up in the decl-specifier-seq that we
20950 did not check, check them now. We must wait until we are in the
20951 scope of the function to perform the checks, since the function
20952 might be a friend. */
9b57b06b 20953 perform_deferred_access_checks ();
0a3b29ad 20954
20955 if (!success_p)
20956 {
3046c0a3 20957 /* Skip the entire function. */
0a3b29ad 20958 cp_parser_skip_to_end_of_block_or_statement (parser);
20959 fn = error_mark_node;
20960 }
ddb7a3b0 20961 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20962 {
20963 /* Seen already, skip it. An error message has already been output. */
20964 cp_parser_skip_to_end_of_block_or_statement (parser);
20965 fn = current_function_decl;
20966 current_function_decl = NULL_TREE;
20967 /* If this is a function from a class, pop the nested class. */
20968 if (current_class_name)
20969 pop_nested_class ();
20970 }
0a3b29ad 20971 else
6198e8f6 20972 {
20973 timevar_id_t tv;
20974 if (DECL_DECLARED_INLINE_P (current_function_decl))
20975 tv = TV_PARSE_INLINE;
20976 else
20977 tv = TV_PARSE_FUNC;
20978 timevar_push (tv);
20979 fn = cp_parser_function_definition_after_declarator (parser,
0a3b29ad 20980 /*inline_p=*/false);
6198e8f6 20981 timevar_pop (tv);
20982 }
0a3b29ad 20983
20984 return fn;
20985}
20986
20987/* Parse the part of a function-definition that follows the
20988 declarator. INLINE_P is TRUE iff this function is an inline
a8b75081 20989 function defined within a class-specifier.
0a3b29ad 20990
20991 Returns the function defined. */
20992
ccb84981 20993static tree
20994cp_parser_function_definition_after_declarator (cp_parser* parser,
45baea8b 20995 bool inline_p)
0a3b29ad 20996{
20997 tree fn;
20998 bool ctor_initializer_p = false;
20999 bool saved_in_unbraced_linkage_specification_p;
0aeb1cc5 21000 bool saved_in_function_body;
0a3b29ad 21001 unsigned saved_num_template_parameter_lists;
ad9ae192 21002 cp_token *token;
0a3b29ad 21003
0aeb1cc5 21004 saved_in_function_body = parser->in_function_body;
21005 parser->in_function_body = true;
0a3b29ad 21006 /* If the next token is `return', then the code may be trying to
21007 make use of the "named return value" extension that G++ used to
21008 support. */
ad9ae192 21009 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 21010 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21011 {
21012 /* Consume the `return' keyword. */
21013 cp_lexer_consume_token (parser->lexer);
21014 /* Look for the identifier that indicates what value is to be
21015 returned. */
21016 cp_parser_identifier (parser);
21017 /* Issue an error message. */
ccb59bb6 21018 error_at (token->location,
21019 "named return values are no longer supported");
0a3b29ad 21020 /* Skip tokens until we reach the start of the function body. */
b75b98aa 21021 while (true)
21022 {
21023 cp_token *token = cp_lexer_peek_token (parser->lexer);
21024 if (token->type == CPP_OPEN_BRACE
21025 || token->type == CPP_EOF
21026 || token->type == CPP_PRAGMA_EOL)
21027 break;
21028 cp_lexer_consume_token (parser->lexer);
21029 }
0a3b29ad 21030 }
21031 /* The `extern' in `extern "C" void f () { ... }' does not apply to
21032 anything declared inside `f'. */
ccb84981 21033 saved_in_unbraced_linkage_specification_p
0a3b29ad 21034 = parser->in_unbraced_linkage_specification_p;
21035 parser->in_unbraced_linkage_specification_p = false;
21036 /* Inside the function, surrounding template-parameter-lists do not
21037 apply. */
ccb84981 21038 saved_num_template_parameter_lists
21039 = parser->num_template_parameter_lists;
0a3b29ad 21040 parser->num_template_parameter_lists = 0;
a8b75081 21041
21042 start_lambda_scope (current_function_decl);
21043
4c0315d0 21044 /* If the next token is `try', `__transaction_atomic', or
21045 `__transaction_relaxed`, then we are looking at either function-try-block
21046 or function-transaction-block. Note that all of these include the
21047 function-body. */
21048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21049 ctor_initializer_p = cp_parser_function_transaction (parser,
21050 RID_TRANSACTION_ATOMIC);
21051 else if (cp_lexer_next_token_is_keyword (parser->lexer,
21052 RID_TRANSACTION_RELAXED))
21053 ctor_initializer_p = cp_parser_function_transaction (parser,
21054 RID_TRANSACTION_RELAXED);
21055 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
0a3b29ad 21056 ctor_initializer_p = cp_parser_function_try_block (parser);
0a3b29ad 21057 else
376a817b 21058 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21059 (parser, /*in_function_try_block=*/false);
0a3b29ad 21060
a8b75081 21061 finish_lambda_scope ();
21062
0a3b29ad 21063 /* Finish the function. */
ccb84981 21064 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
0a3b29ad 21065 (inline_p ? 2 : 0));
21066 /* Generate code for it, if necessary. */
6cb758f0 21067 expand_or_defer_fn (fn);
0a3b29ad 21068 /* Restore the saved values. */
ccb84981 21069 parser->in_unbraced_linkage_specification_p
0a3b29ad 21070 = saved_in_unbraced_linkage_specification_p;
ccb84981 21071 parser->num_template_parameter_lists
0a3b29ad 21072 = saved_num_template_parameter_lists;
0aeb1cc5 21073 parser->in_function_body = saved_in_function_body;
0a3b29ad 21074
21075 return fn;
21076}
21077
21078/* Parse a template-declaration, assuming that the `export' (and
21079 `extern') keywords, if present, has already been scanned. MEMBER_P
21080 is as for cp_parser_template_declaration. */
21081
21082static void
45baea8b 21083cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
0a3b29ad 21084{
21085 tree decl = NULL_TREE;
3369eb76 21086 VEC (deferred_access_check,gc) *checks;
0a3b29ad 21087 tree parameter_list;
21088 bool friend_p = false;
9f25cdd8 21089 bool need_lang_pop;
ad9ae192 21090 cp_token *token;
0a3b29ad 21091
21092 /* Look for the `template' keyword. */
ad9ae192 21093 token = cp_lexer_peek_token (parser->lexer);
c247dce0 21094 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
0a3b29ad 21095 return;
ccb84981 21096
0a3b29ad 21097 /* And the `<'. */
c247dce0 21098 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
0a3b29ad 21099 return;
a34824c0 21100 if (at_class_scope_p () && current_function_decl)
21101 {
21102 /* 14.5.2.2 [temp.mem]
21103
21104 A local class shall not have member templates. */
ccb59bb6 21105 error_at (token->location,
21106 "invalid declaration of member template in local class");
a34824c0 21107 cp_parser_skip_to_end_of_block_or_statement (parser);
21108 return;
21109 }
9f25cdd8 21110 /* [temp]
074ab442 21111
9f25cdd8 21112 A template ... shall not have C linkage. */
21113 if (current_lang_name == lang_name_c)
21114 {
ccb59bb6 21115 error_at (token->location, "template with C linkage");
9f25cdd8 21116 /* Give it C++ linkage to avoid confusing other parts of the
21117 front end. */
21118 push_lang_context (lang_name_cplusplus);
21119 need_lang_pop = true;
21120 }
21121 else
21122 need_lang_pop = false;
23010bc8 21123
21124 /* We cannot perform access checks on the template parameter
21125 declarations until we know what is being declared, just as we
21126 cannot check the decl-specifier list. */
21127 push_deferring_access_checks (dk_deferred);
21128
0a3b29ad 21129 /* If the next token is `>', then we have an invalid
21130 specialization. Rather than complain about an invalid template
21131 parameter, issue an error message here. */
21132 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21133 {
21134 cp_parser_error (parser, "invalid explicit specialization");
3398499c 21135 begin_specialization ();
0a3b29ad 21136 parameter_list = NULL_TREE;
21137 }
21138 else
71c5bb94 21139 {
21140 /* Parse the template parameters. */
21141 parameter_list = cp_parser_template_parameter_list (parser);
21142 fixup_template_parms ();
21143 }
3398499c 21144
23010bc8 21145 /* Get the deferred access checks from the parameter list. These
21146 will be checked once we know what is being declared, as for a
21147 member template the checks must be performed in the scope of the
21148 class containing the member. */
21149 checks = get_deferred_access_checks ();
21150
0a3b29ad 21151 /* Look for the `>'. */
c42e0e2d 21152 cp_parser_skip_to_end_of_template_parameter_list (parser);
0a3b29ad 21153 /* We just processed one more parameter list. */
21154 ++parser->num_template_parameter_lists;
21155 /* If the next token is `template', there are more template
21156 parameters. */
ccb84981 21157 if (cp_lexer_next_token_is_keyword (parser->lexer,
0a3b29ad 21158 RID_TEMPLATE))
21159 cp_parser_template_declaration_after_export (parser, member_p);
370478b1 21160 else if (cxx_dialect >= cxx0x
21161 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21162 decl = cp_parser_alias_declaration (parser);
0a3b29ad 21163 else
21164 {
a8ff8672 21165 /* There are no access checks when parsing a template, as we do not
653e5405 21166 know if a specialization will be a friend. */
a8ff8672 21167 push_deferring_access_checks (dk_no_check);
ad9ae192 21168 token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 21169 decl = cp_parser_single_declaration (parser,
23010bc8 21170 checks,
0a3b29ad 21171 member_p,
0c032b46 21172 /*explicit_specialization_p=*/false,
0a3b29ad 21173 &friend_p);
a8ff8672 21174 pop_deferring_access_checks ();
207355ad 21175
0a3b29ad 21176 /* If this is a member template declaration, let the front
21177 end know. */
21178 if (member_p && !friend_p && decl)
7e35473e 21179 {
21180 if (TREE_CODE (decl) == TYPE_DECL)
ad9ae192 21181 cp_parser_check_access_in_redeclaration (decl, token->location);
7e35473e 21182
21183 decl = finish_member_template_decl (decl);
21184 }
0a3b29ad 21185 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
b123b79d 21186 make_friend_class (current_class_type, TREE_TYPE (decl),
21187 /*complain=*/true);
0a3b29ad 21188 }
21189 /* We are done with the current parameter list. */
21190 --parser->num_template_parameter_lists;
21191
23010bc8 21192 pop_deferring_access_checks ();
21193
0a3b29ad 21194 /* Finish up. */
21195 finish_template_decl (parameter_list);
21196
244db24d 21197 /* Check the template arguments for a literal operator template. */
21198 if (decl
21199 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21200 && UDLIT_OPER_P (DECL_NAME (decl)))
21201 {
21202 bool ok = true;
21203 if (parameter_list == NULL_TREE)
21204 ok = false;
21205 else
21206 {
21207 int num_parms = TREE_VEC_LENGTH (parameter_list);
21208 if (num_parms != 1)
21209 ok = false;
21210 else
21211 {
21212 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21213 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21214 if (TREE_TYPE (parm) != char_type_node
21215 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21216 ok = false;
21217 }
21218 }
21219 if (!ok)
21220 error ("literal operator template %qD has invalid parameter list."
21221 " Expected non-type template argument pack <char...>",
21222 decl);
21223 }
0a3b29ad 21224 /* Register member declarations. */
21225 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21226 finish_member_declaration (decl);
9f25cdd8 21227 /* For the erroneous case of a template with C linkage, we pushed an
21228 implicit C++ linkage scope; exit that scope now. */
21229 if (need_lang_pop)
21230 pop_lang_context ();
0a3b29ad 21231 /* If DECL is a function template, we must return to parse it later.
21232 (Even though there is no definition, there might be default
21233 arguments that need handling.) */
ccb84981 21234 if (member_p && decl
0a3b29ad 21235 && (TREE_CODE (decl) == FUNCTION_DECL
21236 || DECL_FUNCTION_TEMPLATE_P (decl)))
9177da82 21237 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
0a3b29ad 21238}
21239
23010bc8 21240/* Perform the deferred access checks from a template-parameter-list.
21241 CHECKS is a TREE_LIST of access checks, as returned by
21242 get_deferred_access_checks. */
21243
21244static void
3369eb76 21245cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
23010bc8 21246{
21247 ++processing_template_parmlist;
21248 perform_access_checks (checks);
21249 --processing_template_parmlist;
21250}
21251
0a3b29ad 21252/* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21253 `function-definition' sequence. MEMBER_P is true, this declaration
21254 appears in a class scope.
21255
21256 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21257 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21258
21259static tree
ccb84981 21260cp_parser_single_declaration (cp_parser* parser,
3369eb76 21261 VEC (deferred_access_check,gc)* checks,
45baea8b 21262 bool member_p,
0c032b46 21263 bool explicit_specialization_p,
45baea8b 21264 bool* friend_p)
0a3b29ad 21265{
8172be22 21266 int declares_class_or_enum;
0a3b29ad 21267 tree decl = NULL_TREE;
4b9b2871 21268 cp_decl_specifier_seq decl_specifiers;
92b128ed 21269 bool function_definition_p = false;
ad9ae192 21270 cp_token *decl_spec_token_start;
0a3b29ad 21271
2a03dcc3 21272 /* This function is only used when processing a template
21273 declaration. */
21274 gcc_assert (innermost_scope_kind () == sk_template_parms
21275 || innermost_scope_kind () == sk_template_spec);
21276
0a3b29ad 21277 /* Defer access checks until we know what is being declared. */
4f62c42e 21278 push_deferring_access_checks (dk_deferred);
9b57b06b 21279
0a3b29ad 21280 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21281 alternative. */
ad9ae192 21282 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
4b9b2871 21283 cp_parser_decl_specifier_seq (parser,
21284 CP_PARSER_FLAGS_OPTIONAL,
21285 &decl_specifiers,
21286 &declares_class_or_enum);
92b128ed 21287 if (friend_p)
4b9b2871 21288 *friend_p = cp_parser_friend_p (&decl_specifiers);
2a03dcc3 21289
21290 /* There are no template typedefs. */
a60f3e81 21291 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
2a03dcc3 21292 {
ccb59bb6 21293 error_at (decl_spec_token_start->location,
21294 "template declaration of %<typedef%>");
2a03dcc3 21295 decl = error_mark_node;
21296 }
21297
0a3b29ad 21298 /* Gather up the access checks that occurred the
21299 decl-specifier-seq. */
9b57b06b 21300 stop_deferring_access_checks ();
21301
0a3b29ad 21302 /* Check for the declaration of a template class. */
21303 if (declares_class_or_enum)
21304 {
21305 if (cp_parser_declares_only_class_p (parser))
21306 {
4b9b2871 21307 decl = shadow_tag (&decl_specifiers);
f95fba26 21308
21309 /* In this case:
21310
21311 struct C {
21312 friend template <typename T> struct A<T>::B;
21313 };
21314
21315 A<T>::B will be represented by a TYPENAME_TYPE, and
21316 therefore not recognized by shadow_tag. */
21317 if (friend_p && *friend_p
21318 && !decl
21319 && decl_specifiers.type
21320 && TYPE_P (decl_specifiers.type))
21321 decl = decl_specifiers.type;
21322
4b9b2871 21323 if (decl && decl != error_mark_node)
0a3b29ad 21324 decl = TYPE_NAME (decl);
21325 else
21326 decl = error_mark_node;
23010bc8 21327
21328 /* Perform access checks for template parameters. */
21329 cp_parser_perform_template_parameter_access_checks (checks);
0a3b29ad 21330 }
21331 }
67484828 21332
21333 /* Complain about missing 'typename' or other invalid type names. */
3504ce7d 21334 if (!decl_specifiers.any_type_specifiers_p
21335 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21336 {
21337 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21338 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21339 the rest of this declaration. */
21340 decl = error_mark_node;
21341 goto out;
21342 }
67484828 21343
0a3b29ad 21344 /* If it's not a template class, try for a template function. If
21345 the next token is a `;', then this declaration does not declare
21346 anything. But, if there were errors in the decl-specifiers, then
21347 the error might well have come from an attempted class-specifier.
21348 In that case, there's no need to warn about a missing declarator. */
21349 if (!decl
21350 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
4b9b2871 21351 || decl_specifiers.type != error_mark_node))
0c032b46 21352 {
21353 decl = cp_parser_init_declarator (parser,
21354 &decl_specifiers,
21355 checks,
21356 /*function_definition_allowed_p=*/true,
21357 member_p,
21358 declares_class_or_enum,
fa7d5870 21359 &function_definition_p,
21360 NULL);
0c032b46 21361
21362 /* 7.1.1-1 [dcl.stc]
21363
21364 A storage-class-specifier shall not be specified in an explicit
21365 specialization... */
21366 if (decl
21367 && explicit_specialization_p
21368 && decl_specifiers.storage_class != sc_none)
21369 {
ccb59bb6 21370 error_at (decl_spec_token_start->location,
21371 "explicit template specialization cannot have a storage class");
0c032b46 21372 decl = error_mark_node;
21373 }
21374 }
9b57b06b 21375
3504ce7d 21376 /* Look for a trailing `;' after the declaration. */
21377 if (!function_definition_p
21378 && (decl == error_mark_node
21379 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21380 cp_parser_skip_to_end_of_block_or_statement (parser);
21381
21382 out:
9b57b06b 21383 pop_deferring_access_checks ();
21384
0a3b29ad 21385 /* Clear any current qualification; whatever comes next is the start
21386 of something new. */
21387 parser->scope = NULL_TREE;
21388 parser->qualifying_scope = NULL_TREE;
21389 parser->object_scope = NULL_TREE;
0a3b29ad 21390
21391 return decl;
21392}
21393
a63bc44c 21394/* Parse a cast-expression that is not the operand of a unary "&". */
21395
21396static tree
21397cp_parser_simple_cast_expression (cp_parser *parser)
21398{
640aa28c 21399 return cp_parser_cast_expression (parser, /*address_p=*/false,
98b326fd 21400 /*cast_p=*/false, NULL);
a63bc44c 21401}
21402
0a3b29ad 21403/* Parse a functional cast to TYPE. Returns an expression
21404 representing the cast. */
21405
21406static tree
45baea8b 21407cp_parser_functional_cast (cp_parser* parser, tree type)
0a3b29ad 21408{
f352a3fb 21409 VEC(tree,gc) *vec;
0a3b29ad 21410 tree expression_list;
9ee4e816 21411 tree cast;
f82f1250 21412 bool nonconst_p;
21413
21414 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21415 {
bf8d19fe 21416 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
f82f1250 21417 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21418 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21419 if (TREE_CODE (type) == TYPE_DECL)
21420 type = TREE_TYPE (type);
95034afb 21421 return finish_compound_literal (type, expression_list,
21422 tf_warning_or_error);
f82f1250 21423 }
0a3b29ad 21424
f352a3fb 21425
33199a81 21426 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
f352a3fb 21427 /*cast_p=*/true,
21428 /*allow_expansion_p=*/true,
21429 /*non_constant_p=*/NULL);
21430 if (vec == NULL)
21431 expression_list = error_mark_node;
21432 else
21433 {
21434 expression_list = build_tree_list_vec (vec);
21435 release_tree_vector (vec);
21436 }
0a3b29ad 21437
ebd21de4 21438 cast = build_functional_cast (type, expression_list,
21439 tf_warning_or_error);
9ee4e816 21440 /* [expr.const]/1: In an integral constant expression "only type
21441 conversions to integral or enumeration type can be used". */
673b95fd 21442 if (TREE_CODE (type) == TYPE_DECL)
21443 type = TREE_TYPE (type);
bde9ebf7 21444 if (cast != error_mark_node
21445 && !cast_valid_in_integral_constant_expression_p (type)
c61e1212 21446 && cp_parser_non_integral_constant_expression (parser,
21447 NIC_CONSTRUCTOR))
bde9ebf7 21448 return error_mark_node;
9ee4e816 21449 return cast;
0a3b29ad 21450}
21451
92b128ed 21452/* Save the tokens that make up the body of a member function defined
21453 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21454 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21455 specifiers applied to the declaration. Returns the FUNCTION_DECL
21456 for the member function. */
21457
817adc77 21458static tree
92b128ed 21459cp_parser_save_member_function_body (cp_parser* parser,
4b9b2871 21460 cp_decl_specifier_seq *decl_specifiers,
3046c0a3 21461 cp_declarator *declarator,
92b128ed 21462 tree attributes)
21463{
00d26680 21464 cp_token *first;
21465 cp_token *last;
92b128ed 21466 tree fn;
21467
23a78a18 21468 /* Create the FUNCTION_DECL. */
21469 fn = grokmethod (decl_specifiers, declarator, attributes);
92b128ed 21470 /* If something went badly wrong, bail out now. */
21471 if (fn == error_mark_node)
21472 {
21473 /* If there's a function-body, skip it. */
ccb84981 21474 if (cp_parser_token_starts_function_definition_p
92b128ed 21475 (cp_lexer_peek_token (parser->lexer)))
21476 cp_parser_skip_to_end_of_block_or_statement (parser);
21477 return error_mark_node;
21478 }
21479
21480 /* Remember it, if there default args to post process. */
21481 cp_parser_save_default_args (parser, fn);
21482
ccb84981 21483 /* Save away the tokens that make up the body of the
92b128ed 21484 function. */
00d26680 21485 first = parser->lexer->next_token;
f82f1250 21486 /* We can have braced-init-list mem-initializers before the fn body. */
21487 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21488 {
21489 cp_lexer_consume_token (parser->lexer);
21490 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21491 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21492 {
21493 /* cache_group will stop after an un-nested { } pair, too. */
21494 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21495 break;
21496
21497 /* variadic mem-inits have ... after the ')'. */
21498 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21499 cp_lexer_consume_token (parser->lexer);
21500 }
21501 }
00d26680 21502 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
92b128ed 21503 /* Handle function try blocks. */
21504 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
00d26680 21505 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21506 last = parser->lexer->next_token;
92b128ed 21507
21508 /* Save away the inline definition; we will process it when the
21509 class is complete. */
00d26680 21510 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
92b128ed 21511 DECL_PENDING_INLINE_P (fn) = 1;
21512
21513 /* We need to know that this was defined in the class, so that
21514 friend templates are handled correctly. */
21515 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21516
92b128ed 21517 /* Add FN to the queue of functions to be parsed later. */
9177da82 21518 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
92b128ed 21519
21520 return fn;
21521}
21522
3ab40fb9 21523/* Save the tokens that make up the in-class initializer for a non-static
21524 data member. Returns a DEFAULT_ARG. */
21525
21526static tree
21527cp_parser_save_nsdmi (cp_parser* parser)
21528{
ce24dc8d 21529 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
3ab40fb9 21530}
21531
8534c3a3 21532/* Parse a template-argument-list, as well as the trailing ">" (but
e6014a82 21533 not the opening "<"). See cp_parser_template_argument_list for the
8534c3a3 21534 return value. */
21535
21536static tree
21537cp_parser_enclosed_template_argument_list (cp_parser* parser)
21538{
21539 tree arguments;
21540 tree saved_scope;
21541 tree saved_qualifying_scope;
21542 tree saved_object_scope;
21543 bool saved_greater_than_is_operator_p;
48d94ede 21544 int saved_unevaluated_operand;
21545 int saved_inhibit_evaluation_warnings;
8534c3a3 21546
21547 /* [temp.names]
21548
21549 When parsing a template-id, the first non-nested `>' is taken as
21550 the end of the template-argument-list rather than a greater-than
21551 operator. */
ccb84981 21552 saved_greater_than_is_operator_p
8534c3a3 21553 = parser->greater_than_is_operator_p;
21554 parser->greater_than_is_operator_p = false;
21555 /* Parsing the argument list may modify SCOPE, so we save it
21556 here. */
21557 saved_scope = parser->scope;
21558 saved_qualifying_scope = parser->qualifying_scope;
21559 saved_object_scope = parser->object_scope;
ef792945 21560 /* We need to evaluate the template arguments, even though this
21561 template-id may be nested within a "sizeof". */
48d94ede 21562 saved_unevaluated_operand = cp_unevaluated_operand;
21563 cp_unevaluated_operand = 0;
21564 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21565 c_inhibit_evaluation_warnings = 0;
8534c3a3 21566 /* Parse the template-argument-list itself. */
56471494 21567 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21568 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8534c3a3 21569 arguments = NULL_TREE;
21570 else
21571 arguments = cp_parser_template_argument_list (parser);
bece9ea1 21572 /* Look for the `>' that ends the template-argument-list. If we find
21573 a '>>' instead, it's probably just a typo. */
21574 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21575 {
6dcdb5de 21576 if (cxx_dialect != cxx98)
56471494 21577 {
21578 /* In C++0x, a `>>' in a template argument list or cast
21579 expression is considered to be two separate `>'
21580 tokens. So, change the current token to a `>', but don't
21581 consume it: it will be consumed later when the outer
21582 template argument list (or cast expression) is parsed.
21583 Note that this replacement of `>' for `>>' is necessary
21584 even if we are parsing tentatively: in the tentative
21585 case, after calling
21586 cp_parser_enclosed_template_argument_list we will always
21587 throw away all of the template arguments and the first
21588 closing `>', either because the template argument list
21589 was erroneous or because we are replacing those tokens
21590 with a CPP_TEMPLATE_ID token. The second `>' (which will
21591 not have been thrown away) is needed either to close an
21592 outer template argument list or to complete a new-style
21593 cast. */
21594 cp_token *token = cp_lexer_peek_token (parser->lexer);
21595 token->type = CPP_GREATER;
21596 }
21597 else if (!saved_greater_than_is_operator_p)
bece9ea1 21598 {
b9dd3954 21599 /* If we're in a nested template argument list, the '>>' has
21600 to be a typo for '> >'. We emit the error message, but we
21601 continue parsing and we push a '>' as next token, so that
21602 the argument list will be parsed correctly. Note that the
21603 global source location is still on the token before the
21604 '>>', so we need to say explicitly where we want it. */
21605 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 21606 error_at (token->location, "%<>>%> should be %<> >%> "
21607 "within a nested template argument list");
b9dd3954 21608
bece9ea1 21609 token->type = CPP_GREATER;
21610 }
21611 else
21612 {
b9dd3954 21613 /* If this is not a nested template argument list, the '>>'
21614 is a typo for '>'. Emit an error message and continue.
21615 Same deal about the token location, but here we can get it
21616 right by consuming the '>>' before issuing the diagnostic. */
ad9ae192 21617 cp_token *token = cp_lexer_consume_token (parser->lexer);
ccb59bb6 21618 error_at (token->location,
21619 "spurious %<>>%>, use %<>%> to terminate "
21620 "a template argument list");
bece9ea1 21621 }
21622 }
b9dd3954 21623 else
c42e0e2d 21624 cp_parser_skip_to_end_of_template_parameter_list (parser);
8534c3a3 21625 /* The `>' token might be a greater-than operator again now. */
ccb84981 21626 parser->greater_than_is_operator_p
8534c3a3 21627 = saved_greater_than_is_operator_p;
21628 /* Restore the SAVED_SCOPE. */
21629 parser->scope = saved_scope;
21630 parser->qualifying_scope = saved_qualifying_scope;
21631 parser->object_scope = saved_object_scope;
48d94ede 21632 cp_unevaluated_operand = saved_unevaluated_operand;
21633 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8534c3a3 21634
21635 return arguments;
21636}
21637
0a3b29ad 21638/* MEMBER_FUNCTION is a member function, or a friend. If default
21639 arguments, or the body of the function have not yet been parsed,
21640 parse them now. */
21641
21642static void
45baea8b 21643cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
0a3b29ad 21644{
6198e8f6 21645 timevar_push (TV_PARSE_INMETH);
0a3b29ad 21646 /* If this member is a template, get the underlying
21647 FUNCTION_DECL. */
21648 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21649 member_function = DECL_TEMPLATE_RESULT (member_function);
21650
21651 /* There should not be any class definitions in progress at this
21652 point; the bodies of members are only parsed outside of all class
21653 definitions. */
b4df430b 21654 gcc_assert (parser->num_classes_being_defined == 0);
0a3b29ad 21655 /* While we're parsing the member functions we might encounter more
21656 classes. We want to handle them right away, but we don't want
21657 them getting mixed up with functions that are currently in the
21658 queue. */
9177da82 21659 push_unparsed_function_queues (parser);
0a3b29ad 21660
21661 /* Make sure that any template parameters are in scope. */
21662 maybe_begin_member_template_processing (member_function);
21663
0a3b29ad 21664 /* If the body of the function has not yet been parsed, parse it
21665 now. */
21666 if (DECL_PENDING_INLINE_P (member_function))
21667 {
21668 tree function_scope;
21669 cp_token_cache *tokens;
21670
21671 /* The function is no longer pending; we are processing it. */
21672 tokens = DECL_PENDING_INLINE_INFO (member_function);
21673 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21674 DECL_PENDING_INLINE_P (member_function) = 0;
9031d10b 21675
7ff3aeed 21676 /* If this is a local class, enter the scope of the containing
21677 function. */
21678 function_scope = current_function_decl;
0a3b29ad 21679 if (function_scope)
d2764e2d 21680 push_function_context ();
9031d10b 21681
b9dd3954 21682 /* Push the body of the function onto the lexer stack. */
21683 cp_parser_push_lexer_for_tokens (parser, tokens);
ccb84981 21684
0a3b29ad 21685 /* Let the front end know that we going to be defining this
21686 function. */
3046c0a3 21687 start_preparsed_function (member_function, NULL_TREE,
21688 SF_PRE_PARSED | SF_INCLASS_INLINE);
ccb84981 21689
69ebef96 21690 /* Don't do access checking if it is a templated function. */
21691 if (processing_template_decl)
21692 push_deferring_access_checks (dk_no_check);
9031d10b 21693
0a3b29ad 21694 /* Now, parse the body of the function. */
21695 cp_parser_function_definition_after_declarator (parser,
21696 /*inline_p=*/true);
ccb84981 21697
69ebef96 21698 if (processing_template_decl)
21699 pop_deferring_access_checks ();
9031d10b 21700
0a3b29ad 21701 /* Leave the scope of the containing function. */
21702 if (function_scope)
d2764e2d 21703 pop_function_context ();
b9dd3954 21704 cp_parser_pop_lexer (parser);
0a3b29ad 21705 }
21706
21707 /* Remove any template parameters from the symbol table. */
21708 maybe_end_member_template_processing ();
21709
21710 /* Restore the queue. */
9177da82 21711 pop_unparsed_function_queues (parser);
6198e8f6 21712 timevar_pop (TV_PARSE_INMETH);
0a3b29ad 21713}
21714
63eff20d 21715/* If DECL contains any default args, remember it on the unparsed
69b6679c 21716 functions queue. */
21717
21718static void
21719cp_parser_save_default_args (cp_parser* parser, tree decl)
21720{
21721 tree probe;
21722
21723 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21724 probe;
21725 probe = TREE_CHAIN (probe))
21726 if (TREE_PURPOSE (probe))
21727 {
9177da82 21728 cp_default_arg_entry *entry
21729 = VEC_safe_push (cp_default_arg_entry, gc,
21730 unparsed_funs_with_default_args, NULL);
21731 entry->class_type = current_class_type;
21732 entry->decl = decl;
69b6679c 21733 break;
21734 }
69b6679c 21735}
21736
3ab40fb9 21737/* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21738 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21739 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21740 from the parameter-type-list. */
21741
21742static tree
21743cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21744 tree default_arg, tree parmtype)
21745{
21746 cp_token_cache *tokens;
21747 tree parsed_arg;
21748 bool dummy;
21749
5c52c178 21750 if (default_arg == error_mark_node)
21751 return error_mark_node;
21752
3ab40fb9 21753 /* Push the saved tokens for the default argument onto the parser's
21754 lexer stack. */
21755 tokens = DEFARG_TOKENS (default_arg);
21756 cp_parser_push_lexer_for_tokens (parser, tokens);
21757
21758 start_lambda_scope (decl);
21759
21760 /* Parse the default argument. */
21761 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21762 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21763 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21764
21765 finish_lambda_scope ();
21766
3592befb 21767 if (parsed_arg == error_mark_node)
21768 cp_parser_skip_to_end_of_statement (parser);
21769
3ab40fb9 21770 if (!processing_template_decl)
21771 {
21772 /* In a non-template class, check conversions now. In a template,
21773 we'll wait and instantiate these as needed. */
21774 if (TREE_CODE (decl) == PARM_DECL)
21775 parsed_arg = check_default_argument (parmtype, parsed_arg);
21776 else
21777 {
21778 int flags = LOOKUP_IMPLICIT;
21779 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21780 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21781 flags = LOOKUP_NORMAL;
21782 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21783 }
21784 }
21785
21786 /* If the token stream has not been completely used up, then
21787 there was extra junk after the end of the default
21788 argument. */
21789 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21790 {
21791 if (TREE_CODE (decl) == PARM_DECL)
21792 cp_parser_error (parser, "expected %<,%>");
21793 else
21794 cp_parser_error (parser, "expected %<;%>");
21795 }
21796
21797 /* Revert to the main lexer. */
21798 cp_parser_pop_lexer (parser);
21799
21800 return parsed_arg;
21801}
21802
21803/* FIELD is a non-static data member with an initializer which we saved for
21804 later; parse it now. */
21805
21806static void
21807cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21808{
21809 tree def;
21810
21811 push_unparsed_function_queues (parser);
21812 def = cp_parser_late_parse_one_default_arg (parser, field,
21813 DECL_INITIAL (field),
21814 NULL_TREE);
21815 pop_unparsed_function_queues (parser);
21816
21817 DECL_INITIAL (field) = def;
21818}
21819
af128372 21820/* FN is a FUNCTION_DECL which may contains a parameter with an
93c149df 21821 unparsed DEFAULT_ARG. Parse the default args now. This function
21822 assumes that the current scope is the scope in which the default
21823 argument should be processed. */
0a3b29ad 21824
21825static void
af128372 21826cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
0a3b29ad 21827{
0a3b29ad 21828 bool saved_local_variables_forbidden_p;
a8b75081 21829 tree parm, parmdecl;
af128372 21830
e6021728 21831 /* While we're parsing the default args, we might (due to the
21832 statement expression extension) encounter more classes. We want
21833 to handle them right away, but we don't want them getting mixed
21834 up with default args that are currently in the queue. */
9177da82 21835 push_unparsed_function_queues (parser);
e6021728 21836
b9dd3954 21837 /* Local variable names (and the `this' keyword) may not appear
21838 in a default argument. */
21839 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21840 parser->local_variables_forbidden_p = true;
21841
4439eed9 21842 push_defarg_context (fn);
21843
a8b75081 21844 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21845 parmdecl = DECL_ARGUMENTS (fn);
21846 parm && parm != void_list_node;
21847 parm = TREE_CHAIN (parm),
1767a056 21848 parmdecl = DECL_CHAIN (parmdecl))
0a3b29ad 21849 {
29081c08 21850 tree default_arg = TREE_PURPOSE (parm);
21851 tree parsed_arg;
f51f5e0b 21852 VEC(tree,gc) *insts;
21853 tree copy;
21854 unsigned ix;
9031d10b 21855
29081c08 21856 if (!default_arg)
b9dd3954 21857 continue;
0a3b29ad 21858
f6219e82 21859 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21860 /* This can happen for a friend declaration for a function
21861 already declared with default arguments. */
21862 continue;
29081c08 21863
3ab40fb9 21864 parsed_arg
21865 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21866 default_arg,
21867 TREE_VALUE (parm));
027f3cbf 21868 if (parsed_arg == error_mark_node)
21869 {
027f3cbf 21870 continue;
21871 }
074ab442 21872
29081c08 21873 TREE_PURPOSE (parm) = parsed_arg;
21874
21875 /* Update any instantiations we've already created. */
f51f5e0b 21876 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21877 VEC_iterate (tree, insts, ix, copy); ix++)
21878 TREE_PURPOSE (copy) = parsed_arg;
0a3b29ad 21879 }
e6021728 21880
4439eed9 21881 pop_defarg_context ();
21882
917e3348 21883 /* Make sure no default arg is missing. */
21884 check_default_args (fn);
21885
b9dd3954 21886 /* Restore the state of local_variables_forbidden_p. */
21887 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21888
e6021728 21889 /* Restore the queue. */
9177da82 21890 pop_unparsed_function_queues (parser);
0a3b29ad 21891}
21892
21893/* Parse the operand of `sizeof' (or a similar operator). Returns
21894 either a TYPE or an expression, depending on the form of the
21895 input. The KEYWORD indicates which kind of expression we have
21896 encountered. */
21897
21898static tree
45baea8b 21899cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
0a3b29ad 21900{
0a3b29ad 21901 tree expr = NULL_TREE;
21902 const char *saved_message;
dea3189b 21903 char *tmp;
f47c1747 21904 bool saved_integral_constant_expression_p;
640aa28c 21905 bool saved_non_integral_constant_expression_p;
d95d815d 21906 bool pack_expansion_p = false;
0a3b29ad 21907
0a3b29ad 21908 /* Types cannot be defined in a `sizeof' expression. Save away the
21909 old message. */
21910 saved_message = parser->type_definition_forbidden_message;
21911 /* And create the new one. */
2e52ac87 21912 tmp = concat ("types may not be defined in %<",
21913 IDENTIFIER_POINTER (ridpointers[keyword]),
21914 "%> expressions", NULL);
21915 parser->type_definition_forbidden_message = tmp;
0a3b29ad 21916
21917 /* The restrictions on constant-expressions do not apply inside
21918 sizeof expressions. */
9031d10b 21919 saved_integral_constant_expression_p
640aa28c 21920 = parser->integral_constant_expression_p;
21921 saved_non_integral_constant_expression_p
21922 = parser->non_integral_constant_expression_p;
f47c1747 21923 parser->integral_constant_expression_p = false;
0a3b29ad 21924
d95d815d 21925 /* If it's a `...', then we are computing the length of a parameter
21926 pack. */
21927 if (keyword == RID_SIZEOF
21928 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21929 {
21930 /* Consume the `...'. */
21931 cp_lexer_consume_token (parser->lexer);
21932 maybe_warn_variadic_templates ();
21933
21934 /* Note that this is an expansion. */
21935 pack_expansion_p = true;
21936 }
21937
4c99a080 21938 /* Do not actually evaluate the expression. */
48d94ede 21939 ++cp_unevaluated_operand;
21940 ++c_inhibit_evaluation_warnings;
0a3b29ad 21941 /* If it's a `(', then we might be looking at the type-id
21942 construction. */
21943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21944 {
21945 tree type;
41f2d08e 21946 bool saved_in_type_id_in_expr_p;
0a3b29ad 21947
21948 /* We can't be sure yet whether we're looking at a type-id or an
21949 expression. */
21950 cp_parser_parse_tentatively (parser);
21951 /* Consume the `('. */
21952 cp_lexer_consume_token (parser->lexer);
21953 /* Parse the type-id. */
41f2d08e 21954 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21955 parser->in_type_id_in_expr_p = true;
0a3b29ad 21956 type = cp_parser_type_id (parser);
41f2d08e 21957 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
0a3b29ad 21958 /* Now, look for the trailing `)'. */
c247dce0 21959 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
0a3b29ad 21960 /* If all went well, then we're done. */
21961 if (cp_parser_parse_definitely (parser))
21962 {
4b9b2871 21963 cp_decl_specifier_seq decl_specs;
21964
21965 /* Build a trivial decl-specifier-seq. */
21966 clear_decl_specs (&decl_specs);
21967 decl_specs.type = type;
0a3b29ad 21968
21969 /* Call grokdeclarator to figure out what type this is. */
3046c0a3 21970 expr = grokdeclarator (NULL,
4b9b2871 21971 &decl_specs,
0a3b29ad 21972 TYPENAME,
21973 /*initialized=*/0,
21974 /*attrlist=*/NULL);
21975 }
21976 }
f3186cfc 21977 else if (pack_expansion_p)
21978 permerror (cp_lexer_peek_token (parser->lexer)->location,
21979 "%<sizeof...%> argument must be surrounded by parentheses");
0a3b29ad 21980
21981 /* If the type-id production did not work out, then we must be
21982 looking at the unary-expression production. */
21983 if (!expr)
640aa28c 21984 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 21985 /*cast_p=*/false, NULL);
d95d815d 21986
21987 if (pack_expansion_p)
21988 /* Build a pack expansion. */
21989 expr = make_pack_expansion (expr);
21990
4c99a080 21991 /* Go back to evaluating expressions. */
48d94ede 21992 --cp_unevaluated_operand;
21993 --c_inhibit_evaluation_warnings;
0a3b29ad 21994
21995 /* Free the message we created. */
dea3189b 21996 free (tmp);
0a3b29ad 21997 /* And restore the old one. */
21998 parser->type_definition_forbidden_message = saved_message;
9031d10b 21999 parser->integral_constant_expression_p
640aa28c 22000 = saved_integral_constant_expression_p;
22001 parser->non_integral_constant_expression_p
22002 = saved_non_integral_constant_expression_p;
0a3b29ad 22003
22004 return expr;
22005}
22006
22007/* If the current declaration has no declarator, return true. */
22008
22009static bool
22010cp_parser_declares_only_class_p (cp_parser *parser)
22011{
ccb84981 22012 /* If the next token is a `;' or a `,' then there is no
0a3b29ad 22013 declarator. */
22014 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22015 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22016}
22017
78e0edfb 22018/* Update the DECL_SPECS to reflect the storage class indicated by
22019 KEYWORD. */
0a3b29ad 22020
4b9b2871 22021static void
78e0edfb 22022cp_parser_set_storage_class (cp_parser *parser,
22023 cp_decl_specifier_seq *decl_specs,
ad9ae192 22024 enum rid keyword,
22025 location_t location)
0a3b29ad 22026{
78e0edfb 22027 cp_storage_class storage_class;
22028
22029 if (parser->in_unbraced_linkage_specification_p)
22030 {
ccb59bb6 22031 error_at (location, "invalid use of %qD in linkage specification",
22032 ridpointers[keyword]);
78e0edfb 22033 return;
22034 }
22035 else if (decl_specs->storage_class != sc_none)
22036 {
ceec99b9 22037 decl_specs->conflicting_specifiers_p = true;
78e0edfb 22038 return;
22039 }
22040
22041 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
a60f3e81 22042 && decl_spec_seq_has_spec_p (decl_specs, ds_thread))
78e0edfb 22043 {
a60f3e81 22044 error_at (decl_specs->locations[ds_thread],
22045 "%<__thread%> before %qD", ridpointers[keyword]);
22046 decl_specs->locations[ds_thread] = 0;
78e0edfb 22047 }
22048
074ab442 22049 switch (keyword)
78e0edfb 22050 {
22051 case RID_AUTO:
22052 storage_class = sc_auto;
22053 break;
22054 case RID_REGISTER:
22055 storage_class = sc_register;
22056 break;
22057 case RID_STATIC:
22058 storage_class = sc_static;
22059 break;
22060 case RID_EXTERN:
22061 storage_class = sc_extern;
22062 break;
22063 case RID_MUTABLE:
22064 storage_class = sc_mutable;
22065 break;
22066 default:
22067 gcc_unreachable ();
22068 }
22069 decl_specs->storage_class = storage_class;
a60f3e81 22070 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, location);
ceec99b9 22071
22072 /* A storage class specifier cannot be applied alongside a typedef
22073 specifier. If there is a typedef specifier present then set
22074 conflicting_specifiers_p which will trigger an error later
22075 on in grokdeclarator. */
a60f3e81 22076 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
ceec99b9 22077 decl_specs->conflicting_specifiers_p = true;
4b9b2871 22078}
22079
1bc28cb0 22080/* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
22081 is true, the type is a class or enum definition. */
0a3b29ad 22082
4b9b2871 22083static void
22084cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22085 tree type_spec,
eef0ab03 22086 location_t location,
1bc28cb0 22087 bool type_definition_p)
4b9b2871 22088{
22089 decl_specs->any_specifiers_p = true;
207355ad 22090
924bbf02 22091 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22092 (with, for example, in "typedef int wchar_t;") we remember that
22093 this is what happened. In system headers, we ignore these
22094 declarations so that G++ can work with system headers that are not
22095 C++-safe. */
a60f3e81 22096 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
1bc28cb0 22097 && !type_definition_p
263df831 22098 && (type_spec == boolean_type_node
924bbf02 22099 || type_spec == char16_type_node
22100 || type_spec == char32_type_node
263df831 22101 || type_spec == wchar_type_node)
dd9b4af4 22102 && (decl_specs->type
a60f3e81 22103 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
22104 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
22105 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
22106 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
fc1ad922 22107 {
22108 decl_specs->redefined_builtin_type = type_spec;
a60f3e81 22109 set_and_check_decl_spec_loc (decl_specs,
22110 ds_redefined_builtin_type_spec,
22111 location);
fc1ad922 22112 if (!decl_specs->type)
22113 {
22114 decl_specs->type = type_spec;
1bc28cb0 22115 decl_specs->type_definition_p = false;
a60f3e81 22116 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, location);
fc1ad922 22117 }
22118 }
dd9b4af4 22119 else if (decl_specs->type)
22120 decl_specs->multiple_types_p = true;
4b9b2871 22121 else
22122 {
22123 decl_specs->type = type_spec;
1bc28cb0 22124 decl_specs->type_definition_p = type_definition_p;
fc1ad922 22125 decl_specs->redefined_builtin_type = NULL_TREE;
a60f3e81 22126 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, location);
0a3b29ad 22127 }
4b9b2871 22128}
0a3b29ad 22129
a60f3e81 22130/* Set the location for a declarator specifier and check if it is
22131 duplicated.
22132
22133 DECL_SPECS is the sequence of declarator specifiers onto which to
22134 set the location.
22135
22136 DS is the single declarator specifier to set which location is to
22137 be set onto the existing sequence of declarators.
22138
22139 LOCATION is the location for the declarator specifier to
22140 consider. */
22141
22142static void
22143set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
22144 cp_decl_spec ds, source_location location)
22145{
22146 gcc_assert (ds < ds_last);
22147
22148 if (decl_specs == NULL)
22149 return;
22150
22151 if (decl_specs->locations[ds] == 0)
22152 decl_specs->locations[ds] = location;
22153 else
22154 {
22155 if (ds == ds_long)
22156 {
22157 if (decl_specs->locations[ds_long_long] != 0)
22158 error_at (location,
22159 "%<long long long%> is too long for GCC");
22160 else
22161 {
22162 decl_specs->locations[ds_long_long] = location;
22163 pedwarn_cxx98 (location,
22164 OPT_Wlong_long,
22165 "ISO C++ 1998 does not support %<long long%>");
22166 }
22167 }
22168 else
22169 {
22170 static const char *const decl_spec_names[] = {
22171 "signed",
22172 "unsigned",
22173 "short",
22174 "long",
22175 "const",
22176 "volatile",
22177 "restrict",
22178 "inline",
22179 "virtual",
22180 "explicit",
22181 "friend",
22182 "typedef",
22183 "using",
22184 "constexpr",
22185 "__complex",
22186 "__thread"
22187 };
22188 error_at (location,
22189 "duplicate %qs", decl_spec_names[ds]);
22190 }
22191 }
22192}
22193
22194/* Return true iff the declarator specifier DS is present in the
22195 sequence of declarator specifiers DECL_SPECS. */
22196
22197bool
22198decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
22199 cp_decl_spec ds)
22200{
22201 gcc_assert (ds < ds_last);
22202
22203 if (decl_specs == NULL)
22204 return false;
22205
22206 return decl_specs->locations[ds] != 0;
22207}
22208
4b9b2871 22209/* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22210 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
22211
22212static bool
22213cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22214{
a60f3e81 22215 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
0a3b29ad 22216}
22217
c247dce0 22218/* Issue an error message indicating that TOKEN_DESC was expected.
22219 If KEYWORD is true, it indicated this function is called by
22220 cp_parser_require_keword and the required token can only be
22221 a indicated keyword. */
22222
22223static void
22224cp_parser_required_error (cp_parser *parser,
22225 required_token token_desc,
22226 bool keyword)
22227{
22228 switch (token_desc)
22229 {
22230 case RT_NEW:
22231 cp_parser_error (parser, "expected %<new%>");
22232 return;
22233 case RT_DELETE:
22234 cp_parser_error (parser, "expected %<delete%>");
22235 return;
22236 case RT_RETURN:
22237 cp_parser_error (parser, "expected %<return%>");
22238 return;
22239 case RT_WHILE:
22240 cp_parser_error (parser, "expected %<while%>");
22241 return;
22242 case RT_EXTERN:
22243 cp_parser_error (parser, "expected %<extern%>");
22244 return;
22245 case RT_STATIC_ASSERT:
22246 cp_parser_error (parser, "expected %<static_assert%>");
22247 return;
22248 case RT_DECLTYPE:
22249 cp_parser_error (parser, "expected %<decltype%>");
22250 return;
22251 case RT_OPERATOR:
22252 cp_parser_error (parser, "expected %<operator%>");
22253 return;
22254 case RT_CLASS:
22255 cp_parser_error (parser, "expected %<class%>");
22256 return;
22257 case RT_TEMPLATE:
22258 cp_parser_error (parser, "expected %<template%>");
22259 return;
22260 case RT_NAMESPACE:
22261 cp_parser_error (parser, "expected %<namespace%>");
22262 return;
22263 case RT_USING:
22264 cp_parser_error (parser, "expected %<using%>");
22265 return;
22266 case RT_ASM:
22267 cp_parser_error (parser, "expected %<asm%>");
22268 return;
22269 case RT_TRY:
22270 cp_parser_error (parser, "expected %<try%>");
22271 return;
22272 case RT_CATCH:
22273 cp_parser_error (parser, "expected %<catch%>");
22274 return;
22275 case RT_THROW:
22276 cp_parser_error (parser, "expected %<throw%>");
22277 return;
22278 case RT_LABEL:
22279 cp_parser_error (parser, "expected %<__label__%>");
22280 return;
22281 case RT_AT_TRY:
22282 cp_parser_error (parser, "expected %<@try%>");
22283 return;
22284 case RT_AT_SYNCHRONIZED:
22285 cp_parser_error (parser, "expected %<@synchronized%>");
22286 return;
22287 case RT_AT_THROW:
22288 cp_parser_error (parser, "expected %<@throw%>");
22289 return;
4c0315d0 22290 case RT_TRANSACTION_ATOMIC:
22291 cp_parser_error (parser, "expected %<__transaction_atomic%>");
22292 return;
22293 case RT_TRANSACTION_RELAXED:
22294 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22295 return;
c247dce0 22296 default:
22297 break;
22298 }
22299 if (!keyword)
22300 {
22301 switch (token_desc)
22302 {
22303 case RT_SEMICOLON:
22304 cp_parser_error (parser, "expected %<;%>");
22305 return;
22306 case RT_OPEN_PAREN:
22307 cp_parser_error (parser, "expected %<(%>");
22308 return;
22309 case RT_CLOSE_BRACE:
22310 cp_parser_error (parser, "expected %<}%>");
22311 return;
22312 case RT_OPEN_BRACE:
22313 cp_parser_error (parser, "expected %<{%>");
22314 return;
22315 case RT_CLOSE_SQUARE:
22316 cp_parser_error (parser, "expected %<]%>");
22317 return;
22318 case RT_OPEN_SQUARE:
22319 cp_parser_error (parser, "expected %<[%>");
22320 return;
22321 case RT_COMMA:
22322 cp_parser_error (parser, "expected %<,%>");
22323 return;
22324 case RT_SCOPE:
22325 cp_parser_error (parser, "expected %<::%>");
22326 return;
22327 case RT_LESS:
22328 cp_parser_error (parser, "expected %<<%>");
22329 return;
22330 case RT_GREATER:
22331 cp_parser_error (parser, "expected %<>%>");
22332 return;
22333 case RT_EQ:
22334 cp_parser_error (parser, "expected %<=%>");
22335 return;
22336 case RT_ELLIPSIS:
22337 cp_parser_error (parser, "expected %<...%>");
22338 return;
22339 case RT_MULT:
22340 cp_parser_error (parser, "expected %<*%>");
22341 return;
22342 case RT_COMPL:
22343 cp_parser_error (parser, "expected %<~%>");
22344 return;
22345 case RT_COLON:
22346 cp_parser_error (parser, "expected %<:%>");
22347 return;
22348 case RT_COLON_SCOPE:
22349 cp_parser_error (parser, "expected %<:%> or %<::%>");
22350 return;
22351 case RT_CLOSE_PAREN:
22352 cp_parser_error (parser, "expected %<)%>");
22353 return;
22354 case RT_COMMA_CLOSE_PAREN:
22355 cp_parser_error (parser, "expected %<,%> or %<)%>");
22356 return;
22357 case RT_PRAGMA_EOL:
22358 cp_parser_error (parser, "expected end of line");
22359 return;
22360 case RT_NAME:
22361 cp_parser_error (parser, "expected identifier");
22362 return;
22363 case RT_SELECT:
22364 cp_parser_error (parser, "expected selection-statement");
22365 return;
22366 case RT_INTERATION:
22367 cp_parser_error (parser, "expected iteration-statement");
22368 return;
22369 case RT_JUMP:
22370 cp_parser_error (parser, "expected jump-statement");
22371 return;
22372 case RT_CLASS_KEY:
22373 cp_parser_error (parser, "expected class-key");
22374 return;
22375 case RT_CLASS_TYPENAME_TEMPLATE:
22376 cp_parser_error (parser,
22377 "expected %<class%>, %<typename%>, or %<template%>");
22378 return;
22379 default:
22380 gcc_unreachable ();
22381 }
22382 }
22383 else
22384 gcc_unreachable ();
22385}
22386
22387
22388
0a3b29ad 22389/* If the next token is of the indicated TYPE, consume it. Otherwise,
22390 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 22391
0a3b29ad 22392 Returns the token consumed, if the token had the appropriate type.
22393 Otherwise, returns NULL. */
22394
22395static cp_token *
45baea8b 22396cp_parser_require (cp_parser* parser,
653e5405 22397 enum cpp_ttype type,
c247dce0 22398 required_token token_desc)
0a3b29ad 22399{
22400 if (cp_lexer_next_token_is (parser->lexer, type))
22401 return cp_lexer_consume_token (parser->lexer);
22402 else
22403 {
2c593bd0 22404 /* Output the MESSAGE -- unless we're parsing tentatively. */
22405 if (!cp_parser_simulate_error (parser))
c247dce0 22406 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
0a3b29ad 22407 return NULL;
22408 }
22409}
22410
c42e0e2d 22411/* An error message is produced if the next token is not '>'.
22412 All further tokens are skipped until the desired token is
22413 found or '{', '}', ';' or an unbalanced ')' or ']'. */
0a3b29ad 22414
22415static void
c42e0e2d 22416cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
0a3b29ad 22417{
c42e0e2d 22418 /* Current level of '< ... >'. */
22419 unsigned level = 0;
22420 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
0a3b29ad 22421 unsigned nesting_depth = 0;
22422
c42e0e2d 22423 /* Are we ready, yet? If not, issue error message. */
c247dce0 22424 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
0a3b29ad 22425 return;
22426
22427 /* Skip tokens until the desired token is found. */
22428 while (true)
22429 {
22430 /* Peek at the next token. */
c42e0e2d 22431 switch (cp_lexer_peek_token (parser->lexer)->type)
0a3b29ad 22432 {
c42e0e2d 22433 case CPP_LESS:
22434 if (!nesting_depth)
22435 ++level;
22436 break;
b75b98aa 22437
56471494 22438 case CPP_RSHIFT:
6dcdb5de 22439 if (cxx_dialect == cxx98)
56471494 22440 /* C++0x views the `>>' operator as two `>' tokens, but
22441 C++98 does not. */
22442 break;
22443 else if (!nesting_depth && level-- == 0)
22444 {
22445 /* We've hit a `>>' where the first `>' closes the
22446 template argument list, and the second `>' is
22447 spurious. Just consume the `>>' and stop; we've
22448 already produced at least one error. */
22449 cp_lexer_consume_token (parser->lexer);
22450 return;
22451 }
22452 /* Fall through for C++0x, so we handle the second `>' in
22453 the `>>'. */
22454
c42e0e2d 22455 case CPP_GREATER:
22456 if (!nesting_depth && level-- == 0)
22457 {
22458 /* We've reached the token we want, consume it and stop. */
22459 cp_lexer_consume_token (parser->lexer);
22460 return;
22461 }
22462 break;
b75b98aa 22463
b75b98aa 22464 case CPP_OPEN_PAREN:
22465 case CPP_OPEN_SQUARE:
22466 ++nesting_depth;
22467 break;
22468
b75b98aa 22469 case CPP_CLOSE_PAREN:
22470 case CPP_CLOSE_SQUARE:
0a3b29ad 22471 if (nesting_depth-- == 0)
22472 return;
b75b98aa 22473 break;
22474
c42e0e2d 22475 case CPP_EOF:
22476 case CPP_PRAGMA_EOL:
22477 case CPP_SEMICOLON:
22478 case CPP_OPEN_BRACE:
22479 case CPP_CLOSE_BRACE:
22480 /* The '>' was probably forgotten, don't look further. */
22481 return;
22482
b75b98aa 22483 default:
22484 break;
0a3b29ad 22485 }
b75b98aa 22486
0a3b29ad 22487 /* Consume this token. */
22488 cp_lexer_consume_token (parser->lexer);
22489 }
22490}
22491
22492/* If the next token is the indicated keyword, consume it. Otherwise,
22493 issue an error message indicating that TOKEN_DESC was expected.
ccb84981 22494
0a3b29ad 22495 Returns the token consumed, if the token had the appropriate type.
22496 Otherwise, returns NULL. */
22497
22498static cp_token *
45baea8b 22499cp_parser_require_keyword (cp_parser* parser,
653e5405 22500 enum rid keyword,
c247dce0 22501 required_token token_desc)
0a3b29ad 22502{
22503 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22504
22505 if (token && token->keyword != keyword)
22506 {
c247dce0 22507 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
0a3b29ad 22508 return NULL;
22509 }
22510
22511 return token;
22512}
22513
22514/* Returns TRUE iff TOKEN is a token that can begin the body of a
22515 function-definition. */
22516
ccb84981 22517static bool
45baea8b 22518cp_parser_token_starts_function_definition_p (cp_token* token)
0a3b29ad 22519{
22520 return (/* An ordinary function-body begins with an `{'. */
22521 token->type == CPP_OPEN_BRACE
22522 /* A ctor-initializer begins with a `:'. */
22523 || token->type == CPP_COLON
22524 /* A function-try-block begins with `try'. */
22525 || token->keyword == RID_TRY
4c0315d0 22526 /* A function-transaction-block begins with `__transaction_atomic'
22527 or `__transaction_relaxed'. */
22528 || token->keyword == RID_TRANSACTION_ATOMIC
22529 || token->keyword == RID_TRANSACTION_RELAXED
0a3b29ad 22530 /* The named return value extension begins with `return'. */
22531 || token->keyword == RID_RETURN);
22532}
22533
22534/* Returns TRUE iff the next token is the ":" or "{" beginning a class
22535 definition. */
22536
22537static bool
22538cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22539{
22540 cp_token *token;
22541
22542 token = cp_lexer_peek_token (parser->lexer);
22543 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22544}
22545
56471494 22546/* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22547 C++0x) ending a template-argument. */
13795292 22548
22549static bool
22550cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22551{
22552 cp_token *token;
22553
22554 token = cp_lexer_peek_token (parser->lexer);
d95d815d 22555 return (token->type == CPP_COMMA
22556 || token->type == CPP_GREATER
56471494 22557 || token->type == CPP_ELLIPSIS
6dcdb5de 22558 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
13795292 22559}
c8d5ab79 22560
945b33d4 22561/* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
c8d5ab79 22562 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22563
22564static bool
ccb84981 22565cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
c8d5ab79 22566 size_t n)
22567{
22568 cp_token *token;
22569
22570 token = cp_lexer_peek_nth_token (parser->lexer, n);
22571 if (token->type == CPP_LESS)
22572 return true;
22573 /* Check for the sequence `<::' in the original code. It would be lexed as
22574 `[:', where `[' is a digraph, and there is no whitespace before
22575 `:'. */
22576 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22577 {
22578 cp_token *token2;
22579 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22580 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22581 return true;
22582 }
22583 return false;
22584}
ccb84981 22585
0a3b29ad 22586/* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22587 or none_type otherwise. */
22588
22589static enum tag_types
45baea8b 22590cp_parser_token_is_class_key (cp_token* token)
0a3b29ad 22591{
22592 switch (token->keyword)
22593 {
22594 case RID_CLASS:
22595 return class_type;
22596 case RID_STRUCT:
22597 return record_type;
22598 case RID_UNION:
22599 return union_type;
ccb84981 22600
0a3b29ad 22601 default:
22602 return none_type;
22603 }
22604}
22605
22606/* Issue an error message if the CLASS_KEY does not match the TYPE. */
22607
22608static void
22609cp_parser_check_class_key (enum tag_types class_key, tree type)
22610{
49014ba6 22611 if (type == error_mark_node)
22612 return;
0a3b29ad 22613 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
5fe6835d 22614 {
22615 permerror (input_location, "%qs tag used in naming %q#T",
22616 class_key == union_type ? "union"
22617 : class_key == record_type ? "struct" : "class",
22618 type);
22619 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22620 "%q#T was previously declared here", type);
22621 }
0a3b29ad 22622}
ccb84981 22623
63eff20d 22624/* Issue an error message if DECL is redeclared with different
7e35473e 22625 access than its original declaration [class.access.spec/3].
22626 This applies to nested classes and nested class templates.
22627 [class.mem/1]. */
22628
a2c5b975 22629static void
ad9ae192 22630cp_parser_check_access_in_redeclaration (tree decl, location_t location)
7e35473e 22631{
a7905afa 22632 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
7e35473e 22633 return;
22634
22635 if ((TREE_PRIVATE (decl)
22636 != (current_access_specifier == access_private_node))
22637 || (TREE_PROTECTED (decl)
22638 != (current_access_specifier == access_protected_node)))
ccb59bb6 22639 error_at (location, "%qD redeclared with different access", decl);
7e35473e 22640}
22641
0a3b29ad 22642/* Look for the `template' keyword, as a syntactic disambiguator.
ccb84981 22643 Return TRUE iff it is present, in which case it will be
0a3b29ad 22644 consumed. */
22645
22646static bool
22647cp_parser_optional_template_keyword (cp_parser *parser)
22648{
22649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22650 {
22651 /* The `template' keyword can only be used within templates;
22652 outside templates the parser can always figure out what is a
22653 template and what is not. */
22654 if (!processing_template_decl)
22655 {
ad9ae192 22656 cp_token *token = cp_lexer_peek_token (parser->lexer);
ccb59bb6 22657 error_at (token->location,
22658 "%<template%> (as a disambiguator) is only allowed "
22659 "within templates");
0a3b29ad 22660 /* If this part of the token stream is rescanned, the same
22661 error message would be generated. So, we purge the token
22662 from the stream. */
22663 cp_lexer_purge_token (parser->lexer);
22664 return false;
22665 }
22666 else
22667 {
22668 /* Consume the `template' keyword. */
22669 cp_lexer_consume_token (parser->lexer);
22670 return true;
22671 }
22672 }
22673
22674 return false;
22675}
22676
b3c48b5d 22677/* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22678 set PARSER->SCOPE, and perform other related actions. */
22679
22680static void
22681cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22682{
3369eb76 22683 int i;
22684 struct tree_check *check_value;
22685 deferred_access_check *chk;
22686 VEC (deferred_access_check,gc) *checks;
b3c48b5d 22687
22688 /* Get the stored value. */
3369eb76 22689 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
b3c48b5d 22690 /* Perform any access checks that were deferred. */
3369eb76 22691 checks = check_value->checks;
22692 if (checks)
22693 {
48148244 22694 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22695 perform_or_defer_access_check (chk->binfo,
22696 chk->decl,
22697 chk->diag_decl);
3369eb76 22698 }
b3c48b5d 22699 /* Set the scope from the stored value. */
3369eb76 22700 parser->scope = check_value->value;
22701 parser->qualifying_scope = check_value->qualifying_scope;
b3c48b5d 22702 parser->object_scope = NULL_TREE;
22703}
22704
f82f1250 22705/* Consume tokens up through a non-nested END token. Returns TRUE if we
22706 encounter the end of a block before what we were looking for. */
0a3b29ad 22707
f82f1250 22708static bool
00d26680 22709cp_parser_cache_group (cp_parser *parser,
22710 enum cpp_ttype end,
22711 unsigned depth)
0a3b29ad 22712{
22713 while (true)
22714 {
f82f1250 22715 cp_token *token = cp_lexer_peek_token (parser->lexer);
0a3b29ad 22716
f82f1250 22717 /* Abort a parenthesized expression if we encounter a semicolon. */
0a3b29ad 22718 if ((end == CPP_CLOSE_PAREN || depth == 0)
f82f1250 22719 && token->type == CPP_SEMICOLON)
22720 return true;
0a3b29ad 22721 /* If we've reached the end of the file, stop. */
f82f1250 22722 if (token->type == CPP_EOF
b75b98aa 22723 || (end != CPP_PRAGMA_EOL
f82f1250 22724 && token->type == CPP_PRAGMA_EOL))
22725 return true;
22726 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22727 /* We've hit the end of an enclosing block, so there's been some
22728 kind of syntax error. */
22729 return true;
22730
22731 /* Consume the token. */
22732 cp_lexer_consume_token (parser->lexer);
0a3b29ad 22733 /* See if it starts a new group. */
22734 if (token->type == CPP_OPEN_BRACE)
22735 {
00d26680 22736 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
f82f1250 22737 /* In theory this should probably check end == '}', but
22738 cp_parser_save_member_function_body needs it to exit
22739 after either '}' or ')' when called with ')'. */
0a3b29ad 22740 if (depth == 0)
f82f1250 22741 return false;
0a3b29ad 22742 }
22743 else if (token->type == CPP_OPEN_PAREN)
f82f1250 22744 {
22745 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22746 if (depth == 0 && end == CPP_CLOSE_PAREN)
22747 return false;
22748 }
b75b98aa 22749 else if (token->type == CPP_PRAGMA)
22750 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
0a3b29ad 22751 else if (token->type == end)
f82f1250 22752 return false;
0a3b29ad 22753 }
22754}
22755
ce24dc8d 22756/* Like above, for caching a default argument or NSDMI. Both of these are
22757 terminated by a non-nested comma, but it can be unclear whether or not a
22758 comma is nested in a template argument list unless we do more parsing.
22759 In order to handle this ambiguity, when we encounter a ',' after a '<'
22760 we try to parse what follows as a parameter-declaration-list (in the
22761 case of a default argument) or a member-declarator (in the case of an
22762 NSDMI). If that succeeds, then we stop caching. */
22763
22764static tree
22765cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22766{
22767 unsigned depth = 0;
22768 int maybe_template_id = 0;
22769 cp_token *first_token;
22770 cp_token *token;
22771 tree default_argument;
22772
22773 /* Add tokens until we have processed the entire default
22774 argument. We add the range [first_token, token). */
22775 first_token = cp_lexer_peek_token (parser->lexer);
22776 if (first_token->type == CPP_OPEN_BRACE)
22777 {
22778 /* For list-initialization, this is straightforward. */
22779 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22780 token = cp_lexer_peek_token (parser->lexer);
22781 }
22782 else while (true)
22783 {
22784 bool done = false;
22785
22786 /* Peek at the next token. */
22787 token = cp_lexer_peek_token (parser->lexer);
22788 /* What we do depends on what token we have. */
22789 switch (token->type)
22790 {
22791 /* In valid code, a default argument must be
22792 immediately followed by a `,' `)', or `...'. */
22793 case CPP_COMMA:
22794 if (depth == 0 && maybe_template_id)
22795 {
22796 /* If we've seen a '<', we might be in a
22797 template-argument-list. Until Core issue 325 is
22798 resolved, we don't know how this situation ought
22799 to be handled, so try to DTRT. We check whether
22800 what comes after the comma is a valid parameter
22801 declaration list. If it is, then the comma ends
22802 the default argument; otherwise the default
22803 argument continues. */
22804 bool error = false;
22805 tree t;
22806
22807 /* Set ITALP so cp_parser_parameter_declaration_list
22808 doesn't decide to commit to this parse. */
22809 bool saved_italp = parser->in_template_argument_list_p;
22810 parser->in_template_argument_list_p = true;
22811
22812 cp_parser_parse_tentatively (parser);
22813 cp_lexer_consume_token (parser->lexer);
22814
22815 if (nsdmi)
22816 {
22817 int ctor_dtor_or_conv_p;
22818 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22819 &ctor_dtor_or_conv_p,
22820 /*parenthesized_p=*/NULL,
22821 /*member_p=*/true);
22822 }
22823 else
22824 {
22825 begin_scope (sk_function_parms, NULL_TREE);
22826 cp_parser_parameter_declaration_list (parser, &error);
22827 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22828 pop_binding (DECL_NAME (t), t);
22829 leave_scope ();
22830 }
22831 if (!cp_parser_error_occurred (parser) && !error)
22832 done = true;
22833 cp_parser_abort_tentative_parse (parser);
22834
22835 parser->in_template_argument_list_p = saved_italp;
22836 break;
22837 }
22838 case CPP_CLOSE_PAREN:
22839 case CPP_ELLIPSIS:
22840 /* If we run into a non-nested `;', `}', or `]',
22841 then the code is invalid -- but the default
22842 argument is certainly over. */
22843 case CPP_SEMICOLON:
22844 case CPP_CLOSE_BRACE:
22845 case CPP_CLOSE_SQUARE:
22846 if (depth == 0)
22847 done = true;
22848 /* Update DEPTH, if necessary. */
22849 else if (token->type == CPP_CLOSE_PAREN
22850 || token->type == CPP_CLOSE_BRACE
22851 || token->type == CPP_CLOSE_SQUARE)
22852 --depth;
22853 break;
22854
22855 case CPP_OPEN_PAREN:
22856 case CPP_OPEN_SQUARE:
22857 case CPP_OPEN_BRACE:
22858 ++depth;
22859 break;
22860
22861 case CPP_LESS:
22862 if (depth == 0)
22863 /* This might be the comparison operator, or it might
22864 start a template argument list. */
22865 ++maybe_template_id;
22866 break;
22867
22868 case CPP_RSHIFT:
22869 if (cxx_dialect == cxx98)
22870 break;
22871 /* Fall through for C++0x, which treats the `>>'
22872 operator like two `>' tokens in certain
22873 cases. */
22874
22875 case CPP_GREATER:
22876 if (depth == 0)
22877 {
22878 /* This might be an operator, or it might close a
22879 template argument list. But if a previous '<'
22880 started a template argument list, this will have
22881 closed it, so we can't be in one anymore. */
22882 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22883 if (maybe_template_id < 0)
22884 maybe_template_id = 0;
22885 }
22886 break;
22887
22888 /* If we run out of tokens, issue an error message. */
22889 case CPP_EOF:
22890 case CPP_PRAGMA_EOL:
22891 error_at (token->location, "file ends in default argument");
22892 done = true;
22893 break;
22894
22895 case CPP_NAME:
22896 case CPP_SCOPE:
22897 /* In these cases, we should look for template-ids.
22898 For example, if the default argument is
22899 `X<int, double>()', we need to do name lookup to
22900 figure out whether or not `X' is a template; if
22901 so, the `,' does not end the default argument.
22902
22903 That is not yet done. */
22904 break;
22905
22906 default:
22907 break;
22908 }
22909
22910 /* If we've reached the end, stop. */
22911 if (done)
22912 break;
22913
22914 /* Add the token to the token block. */
22915 token = cp_lexer_consume_token (parser->lexer);
22916 }
22917
22918 /* Create a DEFAULT_ARG to represent the unparsed default
22919 argument. */
22920 default_argument = make_node (DEFAULT_ARG);
22921 DEFARG_TOKENS (default_argument)
22922 = cp_token_cache_new (first_token, token);
22923 DEFARG_INSTANTIATIONS (default_argument) = NULL;
22924
22925 return default_argument;
22926}
22927
0a3b29ad 22928/* Begin parsing tentatively. We always save tokens while parsing
22929 tentatively so that if the tentative parsing fails we can restore the
22930 tokens. */
22931
22932static void
45baea8b 22933cp_parser_parse_tentatively (cp_parser* parser)
0a3b29ad 22934{
22935 /* Enter a new parsing context. */
22936 parser->context = cp_parser_context_new (parser->context);
22937 /* Begin saving tokens. */
22938 cp_lexer_save_tokens (parser->lexer);
22939 /* In order to avoid repetitive access control error messages,
22940 access checks are queued up until we are no longer parsing
22941 tentatively. */
4f62c42e 22942 push_deferring_access_checks (dk_deferred);
0a3b29ad 22943}
22944
22945/* Commit to the currently active tentative parse. */
22946
22947static void
45baea8b 22948cp_parser_commit_to_tentative_parse (cp_parser* parser)
0a3b29ad 22949{
22950 cp_parser_context *context;
22951 cp_lexer *lexer;
22952
22953 /* Mark all of the levels as committed. */
22954 lexer = parser->lexer;
22955 for (context = parser->context; context->next; context = context->next)
22956 {
22957 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22958 break;
22959 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22960 while (!cp_lexer_saving_tokens (lexer))
22961 lexer = lexer->next;
22962 cp_lexer_commit_tokens (lexer);
22963 }
22964}
22965
22966/* Abort the currently active tentative parse. All consumed tokens
22967 will be rolled back, and no diagnostics will be issued. */
22968
22969static void
45baea8b 22970cp_parser_abort_tentative_parse (cp_parser* parser)
0a3b29ad 22971{
1f595a02 22972 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22973 || errorcount > 0);
0a3b29ad 22974 cp_parser_simulate_error (parser);
22975 /* Now, pretend that we want to see if the construct was
22976 successfully parsed. */
22977 cp_parser_parse_definitely (parser);
22978}
22979
755edffd 22980/* Stop parsing tentatively. If a parse error has occurred, restore the
0a3b29ad 22981 token stream. Otherwise, commit to the tokens we have consumed.
22982 Returns true if no error occurred; false otherwise. */
22983
22984static bool
45baea8b 22985cp_parser_parse_definitely (cp_parser* parser)
0a3b29ad 22986{
22987 bool error_occurred;
22988 cp_parser_context *context;
22989
755edffd 22990 /* Remember whether or not an error occurred, since we are about to
0a3b29ad 22991 destroy that information. */
22992 error_occurred = cp_parser_error_occurred (parser);
22993 /* Remove the topmost context from the stack. */
22994 context = parser->context;
22995 parser->context = context->next;
22996 /* If no parse errors occurred, commit to the tentative parse. */
22997 if (!error_occurred)
22998 {
22999 /* Commit to the tokens read tentatively, unless that was
23000 already done. */
23001 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
23002 cp_lexer_commit_tokens (parser->lexer);
9b57b06b 23003
23004 pop_to_parent_deferring_access_checks ();
0a3b29ad 23005 }
23006 /* Otherwise, if errors occurred, roll back our state so that things
23007 are just as they were before we began the tentative parse. */
23008 else
9b57b06b 23009 {
23010 cp_lexer_rollback_tokens (parser->lexer);
23011 pop_deferring_access_checks ();
23012 }
2c593bd0 23013 /* Add the context to the front of the free list. */
23014 context->next = cp_parser_context_free_list;
23015 cp_parser_context_free_list = context;
23016
23017 return !error_occurred;
0a3b29ad 23018}
23019
efcbcf83 23020/* Returns true if we are parsing tentatively and are not committed to
23021 this tentative parse. */
0a3b29ad 23022
23023static bool
efcbcf83 23024cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
0a3b29ad 23025{
23026 return (cp_parser_parsing_tentatively (parser)
efcbcf83 23027 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
0a3b29ad 23028}
23029
f1d555e3 23030/* Returns nonzero iff an error has occurred during the most recent
0a3b29ad 23031 tentative parse. */
ccb84981 23032
0a3b29ad 23033static bool
45baea8b 23034cp_parser_error_occurred (cp_parser* parser)
0a3b29ad 23035{
23036 return (cp_parser_parsing_tentatively (parser)
23037 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
23038}
23039
f1d555e3 23040/* Returns nonzero if GNU extensions are allowed. */
0a3b29ad 23041
23042static bool
45baea8b 23043cp_parser_allow_gnu_extensions_p (cp_parser* parser)
0a3b29ad 23044{
23045 return parser->allow_gnu_extensions_p;
23046}
7a4e126b 23047\f
23048/* Objective-C++ Productions */
23049
79408174 23050
23051/* Parse an Objective-C expression, which feeds into a primary-expression
23052 above.
23053
23054 objc-expression:
23055 objc-message-expression
23056 objc-string-literal
23057 objc-encode-expression
23058 objc-protocol-expression
23059 objc-selector-expression
23060
23061 Returns a tree representation of the expression. */
23062
23063static tree
23064cp_parser_objc_expression (cp_parser* parser)
23065{
23066 /* Try to figure out what kind of declaration is present. */
23067 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23068
23069 switch (kwd->type)
23070 {
23071 case CPP_OPEN_SQUARE:
23072 return cp_parser_objc_message_expression (parser);
23073
23074 case CPP_OBJC_STRING:
23075 kwd = cp_lexer_consume_token (parser->lexer);
23076 return objc_build_string_object (kwd->u.value);
23077
23078 case CPP_KEYWORD:
23079 switch (kwd->keyword)
23080 {
23081 case RID_AT_ENCODE:
23082 return cp_parser_objc_encode_expression (parser);
23083
23084 case RID_AT_PROTOCOL:
23085 return cp_parser_objc_protocol_expression (parser);
23086
23087 case RID_AT_SELECTOR:
23088 return cp_parser_objc_selector_expression (parser);
23089
23090 default:
23091 break;
23092 }
23093 default:
23094 error_at (kwd->location,
23095 "misplaced %<@%D%> Objective-C++ construct",
23096 kwd->u.value);
23097 cp_parser_skip_to_end_of_block_or_statement (parser);
23098 }
23099
23100 return error_mark_node;
23101}
23102
23103/* Parse an Objective-C message expression.
23104
23105 objc-message-expression:
23106 [ objc-message-receiver objc-message-args ]
23107
23108 Returns a representation of an Objective-C message. */
23109
23110static tree
23111cp_parser_objc_message_expression (cp_parser* parser)
23112{
23113 tree receiver, messageargs;
23114
23115 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
23116 receiver = cp_parser_objc_message_receiver (parser);
23117 messageargs = cp_parser_objc_message_args (parser);
23118 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23119
4185cf58 23120 return objc_build_message_expr (receiver, messageargs);
79408174 23121}
23122
23123/* Parse an objc-message-receiver.
23124
23125 objc-message-receiver:
23126 expression
23127 simple-type-specifier
23128
23129 Returns a representation of the type or expression. */
23130
23131static tree
23132cp_parser_objc_message_receiver (cp_parser* parser)
23133{
23134 tree rcv;
23135
23136 /* An Objective-C message receiver may be either (1) a type
23137 or (2) an expression. */
23138 cp_parser_parse_tentatively (parser);
23139 rcv = cp_parser_expression (parser, false, NULL);
23140
23141 if (cp_parser_parse_definitely (parser))
23142 return rcv;
23143
23144 rcv = cp_parser_simple_type_specifier (parser,
23145 /*decl_specs=*/NULL,
23146 CP_PARSER_FLAGS_NONE);
23147
23148 return objc_get_class_reference (rcv);
23149}
23150
23151/* Parse the arguments and selectors comprising an Objective-C message.
23152
23153 objc-message-args:
23154 objc-selector
23155 objc-selector-args
23156 objc-selector-args , objc-comma-args
23157
23158 objc-selector-args:
23159 objc-selector [opt] : assignment-expression
23160 objc-selector-args objc-selector [opt] : assignment-expression
23161
23162 objc-comma-args:
23163 assignment-expression
23164 objc-comma-args , assignment-expression
23165
23166 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23167 selector arguments and TREE_VALUE containing a list of comma
23168 arguments. */
23169
23170static tree
23171cp_parser_objc_message_args (cp_parser* parser)
23172{
23173 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23174 bool maybe_unary_selector_p = true;
23175 cp_token *token = cp_lexer_peek_token (parser->lexer);
23176
23177 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23178 {
23179 tree selector = NULL_TREE, arg;
23180
23181 if (token->type != CPP_COLON)
23182 selector = cp_parser_objc_selector (parser);
23183
23184 /* Detect if we have a unary selector. */
23185 if (maybe_unary_selector_p
23186 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23187 return build_tree_list (selector, NULL_TREE);
23188
23189 maybe_unary_selector_p = false;
23190 cp_parser_require (parser, CPP_COLON, RT_COLON);
23191 arg = cp_parser_assignment_expression (parser, false, NULL);
23192
23193 sel_args
23194 = chainon (sel_args,
23195 build_tree_list (selector, arg));
23196
23197 token = cp_lexer_peek_token (parser->lexer);
23198 }
23199
23200 /* Handle non-selector arguments, if any. */
23201 while (token->type == CPP_COMMA)
23202 {
23203 tree arg;
23204
23205 cp_lexer_consume_token (parser->lexer);
23206 arg = cp_parser_assignment_expression (parser, false, NULL);
23207
23208 addl_args
23209 = chainon (addl_args,
23210 build_tree_list (NULL_TREE, arg));
23211
23212 token = cp_lexer_peek_token (parser->lexer);
23213 }
23214
23215 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23216 {
23217 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23218 return build_tree_list (error_mark_node, error_mark_node);
23219 }
23220
23221 return build_tree_list (sel_args, addl_args);
23222}
23223
23224/* Parse an Objective-C encode expression.
23225
23226 objc-encode-expression:
23227 @encode objc-typename
23228
23229 Returns an encoded representation of the type argument. */
23230
23231static tree
23232cp_parser_objc_encode_expression (cp_parser* parser)
23233{
23234 tree type;
23235 cp_token *token;
23236
23237 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
23238 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23239 token = cp_lexer_peek_token (parser->lexer);
23240 type = complete_type (cp_parser_type_id (parser));
23241 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23242
23243 if (!type)
23244 {
23245 error_at (token->location,
23246 "%<@encode%> must specify a type as an argument");
23247 return error_mark_node;
23248 }
23249
23250 /* This happens if we find @encode(T) (where T is a template
23251 typename or something dependent on a template typename) when
23252 parsing a template. In that case, we can't compile it
23253 immediately, but we rather create an AT_ENCODE_EXPR which will
23254 need to be instantiated when the template is used.
23255 */
23256 if (dependent_type_p (type))
23257 {
23258 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23259 TREE_READONLY (value) = 1;
23260 return value;
23261 }
23262
23263 return objc_build_encode_expr (type);
23264}
23265
23266/* Parse an Objective-C @defs expression. */
23267
23268static tree
23269cp_parser_objc_defs_expression (cp_parser *parser)
23270{
23271 tree name;
23272
23273 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
23274 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23275 name = cp_parser_identifier (parser);
23276 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23277
23278 return objc_get_class_ivars (name);
23279}
23280
23281/* Parse an Objective-C protocol expression.
23282
23283 objc-protocol-expression:
23284 @protocol ( identifier )
23285
23286 Returns a representation of the protocol expression. */
23287
23288static tree
23289cp_parser_objc_protocol_expression (cp_parser* parser)
23290{
23291 tree proto;
23292
23293 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23294 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23295 proto = cp_parser_identifier (parser);
23296 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23297
23298 return objc_build_protocol_expr (proto);
23299}
23300
23301/* Parse an Objective-C selector expression.
23302
23303 objc-selector-expression:
23304 @selector ( objc-method-signature )
23305
23306 objc-method-signature:
23307 objc-selector
23308 objc-selector-seq
23309
23310 objc-selector-seq:
23311 objc-selector :
23312 objc-selector-seq objc-selector :
23313
23314 Returns a representation of the method selector. */
23315
23316static tree
23317cp_parser_objc_selector_expression (cp_parser* parser)
23318{
23319 tree sel_seq = NULL_TREE;
23320 bool maybe_unary_selector_p = true;
23321 cp_token *token;
23322 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23323
23324 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
23325 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23326 token = cp_lexer_peek_token (parser->lexer);
23327
23328 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23329 || token->type == CPP_SCOPE)
23330 {
23331 tree selector = NULL_TREE;
23332
23333 if (token->type != CPP_COLON
23334 || token->type == CPP_SCOPE)
23335 selector = cp_parser_objc_selector (parser);
23336
23337 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23338 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23339 {
23340 /* Detect if we have a unary selector. */
23341 if (maybe_unary_selector_p)
23342 {
23343 sel_seq = selector;
23344 goto finish_selector;
23345 }
23346 else
23347 {
23348 cp_parser_error (parser, "expected %<:%>");
23349 }
23350 }
23351 maybe_unary_selector_p = false;
23352 token = cp_lexer_consume_token (parser->lexer);
23353
23354 if (token->type == CPP_SCOPE)
23355 {
23356 sel_seq
23357 = chainon (sel_seq,
23358 build_tree_list (selector, NULL_TREE));
23359 sel_seq
23360 = chainon (sel_seq,
23361 build_tree_list (NULL_TREE, NULL_TREE));
23362 }
23363 else
23364 sel_seq
23365 = chainon (sel_seq,
23366 build_tree_list (selector, NULL_TREE));
23367
23368 token = cp_lexer_peek_token (parser->lexer);
23369 }
23370
23371 finish_selector:
23372 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23373
23374 return objc_build_selector_expr (loc, sel_seq);
23375}
23376
23377/* Parse a list of identifiers.
23378
23379 objc-identifier-list:
23380 identifier
23381 objc-identifier-list , identifier
23382
23383 Returns a TREE_LIST of identifier nodes. */
23384
23385static tree
23386cp_parser_objc_identifier_list (cp_parser* parser)
23387{
23388 tree identifier;
23389 tree list;
23390 cp_token *sep;
23391
23392 identifier = cp_parser_identifier (parser);
23393 if (identifier == error_mark_node)
23394 return error_mark_node;
23395
23396 list = build_tree_list (NULL_TREE, identifier);
23397 sep = cp_lexer_peek_token (parser->lexer);
23398
23399 while (sep->type == CPP_COMMA)
23400 {
23401 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23402 identifier = cp_parser_identifier (parser);
23403 if (identifier == error_mark_node)
23404 return list;
23405
23406 list = chainon (list, build_tree_list (NULL_TREE,
23407 identifier));
23408 sep = cp_lexer_peek_token (parser->lexer);
23409 }
23410
23411 return list;
23412}
23413
23414/* Parse an Objective-C alias declaration.
23415
23416 objc-alias-declaration:
23417 @compatibility_alias identifier identifier ;
23418
23419 This function registers the alias mapping with the Objective-C front end.
23420 It returns nothing. */
23421
23422static void
23423cp_parser_objc_alias_declaration (cp_parser* parser)
23424{
23425 tree alias, orig;
23426
23427 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23428 alias = cp_parser_identifier (parser);
23429 orig = cp_parser_identifier (parser);
23430 objc_declare_alias (alias, orig);
23431 cp_parser_consume_semicolon_at_end_of_statement (parser);
23432}
23433
23434/* Parse an Objective-C class forward-declaration.
23435
23436 objc-class-declaration:
23437 @class objc-identifier-list ;
23438
23439 The function registers the forward declarations with the Objective-C
23440 front end. It returns nothing. */
23441
23442static void
23443cp_parser_objc_class_declaration (cp_parser* parser)
23444{
23445 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29d7200d 23446 while (true)
23447 {
23448 tree id;
23449
23450 id = cp_parser_identifier (parser);
23451 if (id == error_mark_node)
23452 break;
23453
23454 objc_declare_class (id);
23455
23456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23457 cp_lexer_consume_token (parser->lexer);
23458 else
23459 break;
23460 }
79408174 23461 cp_parser_consume_semicolon_at_end_of_statement (parser);
23462}
23463
23464/* Parse a list of Objective-C protocol references.
23465
23466 objc-protocol-refs-opt:
23467 objc-protocol-refs [opt]
23468
23469 objc-protocol-refs:
23470 < objc-identifier-list >
23471
23472 Returns a TREE_LIST of identifiers, if any. */
23473
23474static tree
23475cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23476{
23477 tree protorefs = NULL_TREE;
23478
23479 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23480 {
23481 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23482 protorefs = cp_parser_objc_identifier_list (parser);
23483 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23484 }
23485
23486 return protorefs;
23487}
23488
23489/* Parse a Objective-C visibility specification. */
23490
23491static void
23492cp_parser_objc_visibility_spec (cp_parser* parser)
23493{
23494 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23495
23496 switch (vis->keyword)
23497 {
23498 case RID_AT_PRIVATE:
23499 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23500 break;
23501 case RID_AT_PROTECTED:
23502 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23503 break;
23504 case RID_AT_PUBLIC:
23505 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23506 break;
23507 case RID_AT_PACKAGE:
23508 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23509 break;
23510 default:
23511 return;
23512 }
23513
23514 /* Eat '@private'/'@protected'/'@public'. */
23515 cp_lexer_consume_token (parser->lexer);
23516}
23517
23518/* Parse an Objective-C method type. Return 'true' if it is a class
23519 (+) method, and 'false' if it is an instance (-) method. */
23520
23521static inline bool
23522cp_parser_objc_method_type (cp_parser* parser)
23523{
23524 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23525 return true;
23526 else
23527 return false;
23528}
23529
23530/* Parse an Objective-C protocol qualifier. */
23531
23532static tree
23533cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23534{
23535 tree quals = NULL_TREE, node;
23536 cp_token *token = cp_lexer_peek_token (parser->lexer);
23537
23538 node = token->u.value;
23539
23540 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23541 && (node == ridpointers [(int) RID_IN]
23542 || node == ridpointers [(int) RID_OUT]
23543 || node == ridpointers [(int) RID_INOUT]
23544 || node == ridpointers [(int) RID_BYCOPY]
23545 || node == ridpointers [(int) RID_BYREF]
23546 || node == ridpointers [(int) RID_ONEWAY]))
23547 {
23548 quals = tree_cons (NULL_TREE, node, quals);
23549 cp_lexer_consume_token (parser->lexer);
23550 token = cp_lexer_peek_token (parser->lexer);
23551 node = token->u.value;
23552 }
23553
23554 return quals;
23555}
23556
23557/* Parse an Objective-C typename. */
23558
23559static tree
23560cp_parser_objc_typename (cp_parser* parser)
23561{
23562 tree type_name = NULL_TREE;
23563
23564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23565 {
23566 tree proto_quals, cp_type = NULL_TREE;
23567
23568 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23569 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23570
23571 /* An ObjC type name may consist of just protocol qualifiers, in which
23572 case the type shall default to 'id'. */
23573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23574 {
23575 cp_type = cp_parser_type_id (parser);
23576
23577 /* If the type could not be parsed, an error has already
23578 been produced. For error recovery, behave as if it had
23579 not been specified, which will use the default type
23580 'id'. */
23581 if (cp_type == error_mark_node)
23582 {
23583 cp_type = NULL_TREE;
23584 /* We need to skip to the closing parenthesis as
23585 cp_parser_type_id() does not seem to do it for
23586 us. */
23587 cp_parser_skip_to_closing_parenthesis (parser,
23588 /*recovering=*/true,
23589 /*or_comma=*/false,
23590 /*consume_paren=*/false);
23591 }
23592 }
23593
23594 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23595 type_name = build_tree_list (proto_quals, cp_type);
23596 }
23597
23598 return type_name;
23599}
23600
23601/* Check to see if TYPE refers to an Objective-C selector name. */
23602
23603static bool
23604cp_parser_objc_selector_p (enum cpp_ttype type)
23605{
23606 return (type == CPP_NAME || type == CPP_KEYWORD
23607 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23608 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23609 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23610 || type == CPP_XOR || type == CPP_XOR_EQ);
23611}
23612
23613/* Parse an Objective-C selector. */
23614
23615static tree
23616cp_parser_objc_selector (cp_parser* parser)
23617{
23618 cp_token *token = cp_lexer_consume_token (parser->lexer);
23619
23620 if (!cp_parser_objc_selector_p (token->type))
23621 {
23622 error_at (token->location, "invalid Objective-C++ selector name");
23623 return error_mark_node;
23624 }
23625
23626 /* C++ operator names are allowed to appear in ObjC selectors. */
23627 switch (token->type)
23628 {
23629 case CPP_AND_AND: return get_identifier ("and");
23630 case CPP_AND_EQ: return get_identifier ("and_eq");
23631 case CPP_AND: return get_identifier ("bitand");
23632 case CPP_OR: return get_identifier ("bitor");
23633 case CPP_COMPL: return get_identifier ("compl");
23634 case CPP_NOT: return get_identifier ("not");
23635 case CPP_NOT_EQ: return get_identifier ("not_eq");
23636 case CPP_OR_OR: return get_identifier ("or");
23637 case CPP_OR_EQ: return get_identifier ("or_eq");
23638 case CPP_XOR: return get_identifier ("xor");
23639 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23640 default: return token->u.value;
23641 }
23642}
23643
23644/* Parse an Objective-C params list. */
23645
23646static tree
23647cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23648{
23649 tree params = NULL_TREE;
23650 bool maybe_unary_selector_p = true;
23651 cp_token *token = cp_lexer_peek_token (parser->lexer);
23652
23653 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23654 {
23655 tree selector = NULL_TREE, type_name, identifier;
23656 tree parm_attr = NULL_TREE;
23657
23658 if (token->keyword == RID_ATTRIBUTE)
23659 break;
23660
23661 if (token->type != CPP_COLON)
23662 selector = cp_parser_objc_selector (parser);
23663
23664 /* Detect if we have a unary selector. */
23665 if (maybe_unary_selector_p
23666 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23667 {
23668 params = selector; /* Might be followed by attributes. */
23669 break;
23670 }
23671
23672 maybe_unary_selector_p = false;
23673 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23674 {
23675 /* Something went quite wrong. There should be a colon
23676 here, but there is not. Stop parsing parameters. */
23677 break;
23678 }
23679 type_name = cp_parser_objc_typename (parser);
23680 /* New ObjC allows attributes on parameters too. */
23681 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23682 parm_attr = cp_parser_attributes_opt (parser);
23683 identifier = cp_parser_identifier (parser);
23684
23685 params
23686 = chainon (params,
23687 objc_build_keyword_decl (selector,
23688 type_name,
23689 identifier,
23690 parm_attr));
23691
23692 token = cp_lexer_peek_token (parser->lexer);
23693 }
23694
23695 if (params == NULL_TREE)
23696 {
23697 cp_parser_error (parser, "objective-c++ method declaration is expected");
23698 return error_mark_node;
23699 }
23700
23701 /* We allow tail attributes for the method. */
23702 if (token->keyword == RID_ATTRIBUTE)
23703 {
23704 *attributes = cp_parser_attributes_opt (parser);
23705 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23706 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23707 return params;
23708 cp_parser_error (parser,
23709 "method attributes must be specified at the end");
23710 return error_mark_node;
23711 }
23712
23713 if (params == NULL_TREE)
23714 {
23715 cp_parser_error (parser, "objective-c++ method declaration is expected");
23716 return error_mark_node;
23717 }
23718 return params;
23719}
23720
23721/* Parse the non-keyword Objective-C params. */
23722
23723static tree
23724cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23725 tree* attributes)
23726{
23727 tree params = make_node (TREE_LIST);
23728 cp_token *token = cp_lexer_peek_token (parser->lexer);
23729 *ellipsisp = false; /* Initially, assume no ellipsis. */
23730
23731 while (token->type == CPP_COMMA)
23732 {
23733 cp_parameter_declarator *parmdecl;
23734 tree parm;
23735
23736 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23737 token = cp_lexer_peek_token (parser->lexer);
23738
23739 if (token->type == CPP_ELLIPSIS)
23740 {
23741 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23742 *ellipsisp = true;
23743 token = cp_lexer_peek_token (parser->lexer);
23744 break;
23745 }
23746
23747 /* TODO: parse attributes for tail parameters. */
23748 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23749 parm = grokdeclarator (parmdecl->declarator,
23750 &parmdecl->decl_specifiers,
23751 PARM, /*initialized=*/0,
23752 /*attrlist=*/NULL);
23753
23754 chainon (params, build_tree_list (NULL_TREE, parm));
23755 token = cp_lexer_peek_token (parser->lexer);
23756 }
23757
23758 /* We allow tail attributes for the method. */
23759 if (token->keyword == RID_ATTRIBUTE)
23760 {
23761 if (*attributes == NULL_TREE)
23762 {
23763 *attributes = cp_parser_attributes_opt (parser);
23764 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23765 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23766 return params;
23767 }
23768 else
23769 /* We have an error, but parse the attributes, so that we can
23770 carry on. */
23771 *attributes = cp_parser_attributes_opt (parser);
23772
23773 cp_parser_error (parser,
23774 "method attributes must be specified at the end");
23775 return error_mark_node;
23776 }
23777
23778 return params;
23779}
23780
23781/* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23782
23783static void
23784cp_parser_objc_interstitial_code (cp_parser* parser)
23785{
23786 cp_token *token = cp_lexer_peek_token (parser->lexer);
23787
23788 /* If the next token is `extern' and the following token is a string
23789 literal, then we have a linkage specification. */
23790 if (token->keyword == RID_EXTERN
244db24d 23791 && cp_parser_is_pure_string_literal
23792 (cp_lexer_peek_nth_token (parser->lexer, 2)))
79408174 23793 cp_parser_linkage_specification (parser);
23794 /* Handle #pragma, if any. */
23795 else if (token->type == CPP_PRAGMA)
23796 cp_parser_pragma (parser, pragma_external);
23797 /* Allow stray semicolons. */
23798 else if (token->type == CPP_SEMICOLON)
23799 cp_lexer_consume_token (parser->lexer);
23800 /* Mark methods as optional or required, when building protocols. */
23801 else if (token->keyword == RID_AT_OPTIONAL)
23802 {
23803 cp_lexer_consume_token (parser->lexer);
23804 objc_set_method_opt (true);
23805 }
23806 else if (token->keyword == RID_AT_REQUIRED)
23807 {
23808 cp_lexer_consume_token (parser->lexer);
23809 objc_set_method_opt (false);
23810 }
23811 else if (token->keyword == RID_NAMESPACE)
23812 cp_parser_namespace_definition (parser);
23813 /* Other stray characters must generate errors. */
23814 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23815 {
23816 cp_lexer_consume_token (parser->lexer);
23817 error ("stray %qs between Objective-C++ methods",
23818 token->type == CPP_OPEN_BRACE ? "{" : "}");
23819 }
23820 /* Finally, try to parse a block-declaration, or a function-definition. */
23821 else
23822 cp_parser_block_declaration (parser, /*statement_p=*/false);
23823}
23824
23825/* Parse a method signature. */
23826
23827static tree
23828cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23829{
23830 tree rettype, kwdparms, optparms;
23831 bool ellipsis = false;
23832 bool is_class_method;
23833
23834 is_class_method = cp_parser_objc_method_type (parser);
23835 rettype = cp_parser_objc_typename (parser);
23836 *attributes = NULL_TREE;
23837 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23838 if (kwdparms == error_mark_node)
23839 return error_mark_node;
23840 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23841 if (optparms == error_mark_node)
23842 return error_mark_node;
23843
23844 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23845}
23846
23847static bool
23848cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23849{
23850 tree tattr;
23851 cp_lexer_save_tokens (parser->lexer);
23852 tattr = cp_parser_attributes_opt (parser);
23853 gcc_assert (tattr) ;
23854
23855 /* If the attributes are followed by a method introducer, this is not allowed.
23856 Dump the attributes and flag the situation. */
23857 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23858 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23859 return true;
23860
23861 /* Otherwise, the attributes introduce some interstitial code, possibly so
23862 rewind to allow that check. */
23863 cp_lexer_rollback_tokens (parser->lexer);
23864 return false;
23865}
23866
23867/* Parse an Objective-C method prototype list. */
23868
23869static void
23870cp_parser_objc_method_prototype_list (cp_parser* parser)
23871{
23872 cp_token *token = cp_lexer_peek_token (parser->lexer);
23873
23874 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23875 {
23876 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23877 {
23878 tree attributes, sig;
23879 bool is_class_method;
23880 if (token->type == CPP_PLUS)
23881 is_class_method = true;
23882 else
23883 is_class_method = false;
23884 sig = cp_parser_objc_method_signature (parser, &attributes);
23885 if (sig == error_mark_node)
23886 {
23887 cp_parser_skip_to_end_of_block_or_statement (parser);
23888 token = cp_lexer_peek_token (parser->lexer);
23889 continue;
23890 }
23891 objc_add_method_declaration (is_class_method, sig, attributes);
23892 cp_parser_consume_semicolon_at_end_of_statement (parser);
23893 }
23894 else if (token->keyword == RID_AT_PROPERTY)
23895 cp_parser_objc_at_property_declaration (parser);
23896 else if (token->keyword == RID_ATTRIBUTE
23897 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23898 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23899 OPT_Wattributes,
23900 "prefix attributes are ignored for methods");
23901 else
23902 /* Allow for interspersed non-ObjC++ code. */
23903 cp_parser_objc_interstitial_code (parser);
23904
23905 token = cp_lexer_peek_token (parser->lexer);
23906 }
23907
23908 if (token->type != CPP_EOF)
23909 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23910 else
23911 cp_parser_error (parser, "expected %<@end%>");
23912
23913 objc_finish_interface ();
23914}
23915
23916/* Parse an Objective-C method definition list. */
23917
23918static void
23919cp_parser_objc_method_definition_list (cp_parser* parser)
23920{
23921 cp_token *token = cp_lexer_peek_token (parser->lexer);
23922
23923 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23924 {
23925 tree meth;
23926
23927 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23928 {
23929 cp_token *ptk;
23930 tree sig, attribute;
23931 bool is_class_method;
23932 if (token->type == CPP_PLUS)
23933 is_class_method = true;
23934 else
23935 is_class_method = false;
23936 push_deferring_access_checks (dk_deferred);
23937 sig = cp_parser_objc_method_signature (parser, &attribute);
23938 if (sig == error_mark_node)
23939 {
23940 cp_parser_skip_to_end_of_block_or_statement (parser);
23941 token = cp_lexer_peek_token (parser->lexer);
23942 continue;
23943 }
4232a958 23944 objc_start_method_definition (is_class_method, sig, attribute,
23945 NULL_TREE);
79408174 23946
23947 /* For historical reasons, we accept an optional semicolon. */
23948 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23949 cp_lexer_consume_token (parser->lexer);
23950
23951 ptk = cp_lexer_peek_token (parser->lexer);
23952 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23953 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23954 {
23955 perform_deferred_access_checks ();
23956 stop_deferring_access_checks ();
23957 meth = cp_parser_function_definition_after_declarator (parser,
23958 false);
23959 pop_deferring_access_checks ();
23960 objc_finish_method_definition (meth);
23961 }
23962 }
23963 /* The following case will be removed once @synthesize is
23964 completely implemented. */
23965 else if (token->keyword == RID_AT_PROPERTY)
23966 cp_parser_objc_at_property_declaration (parser);
23967 else if (token->keyword == RID_AT_SYNTHESIZE)
23968 cp_parser_objc_at_synthesize_declaration (parser);
23969 else if (token->keyword == RID_AT_DYNAMIC)
23970 cp_parser_objc_at_dynamic_declaration (parser);
23971 else if (token->keyword == RID_ATTRIBUTE
23972 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23973 warning_at (token->location, OPT_Wattributes,
23974 "prefix attributes are ignored for methods");
23975 else
23976 /* Allow for interspersed non-ObjC++ code. */
23977 cp_parser_objc_interstitial_code (parser);
23978
23979 token = cp_lexer_peek_token (parser->lexer);
23980 }
23981
23982 if (token->type != CPP_EOF)
23983 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23984 else
23985 cp_parser_error (parser, "expected %<@end%>");
23986
23987 objc_finish_implementation ();
23988}
23989
23990/* Parse Objective-C ivars. */
23991
23992static void
23993cp_parser_objc_class_ivars (cp_parser* parser)
23994{
23995 cp_token *token = cp_lexer_peek_token (parser->lexer);
23996
23997 if (token->type != CPP_OPEN_BRACE)
23998 return; /* No ivars specified. */
23999
24000 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
24001 token = cp_lexer_peek_token (parser->lexer);
24002
24003 while (token->type != CPP_CLOSE_BRACE
24004 && token->keyword != RID_AT_END && token->type != CPP_EOF)
24005 {
24006 cp_decl_specifier_seq declspecs;
24007 int decl_class_or_enum_p;
24008 tree prefix_attributes;
24009
24010 cp_parser_objc_visibility_spec (parser);
24011
24012 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24013 break;
24014
24015 cp_parser_decl_specifier_seq (parser,
24016 CP_PARSER_FLAGS_OPTIONAL,
24017 &declspecs,
24018 &decl_class_or_enum_p);
24019
24020 /* auto, register, static, extern, mutable. */
24021 if (declspecs.storage_class != sc_none)
24022 {
24023 cp_parser_error (parser, "invalid type for instance variable");
24024 declspecs.storage_class = sc_none;
24025 }
24026
24027 /* __thread. */
a60f3e81 24028 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
79408174 24029 {
24030 cp_parser_error (parser, "invalid type for instance variable");
a60f3e81 24031 declspecs.locations[ds_thread] = 0;
79408174 24032 }
24033
24034 /* typedef. */
a60f3e81 24035 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
79408174 24036 {
24037 cp_parser_error (parser, "invalid type for instance variable");
a60f3e81 24038 declspecs.locations[ds_thread] = 0;
79408174 24039 }
24040
24041 prefix_attributes = declspecs.attributes;
24042 declspecs.attributes = NULL_TREE;
24043
24044 /* Keep going until we hit the `;' at the end of the
24045 declaration. */
24046 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24047 {
24048 tree width = NULL_TREE, attributes, first_attribute, decl;
24049 cp_declarator *declarator = NULL;
24050 int ctor_dtor_or_conv_p;
24051
24052 /* Check for a (possibly unnamed) bitfield declaration. */
24053 token = cp_lexer_peek_token (parser->lexer);
24054 if (token->type == CPP_COLON)
24055 goto eat_colon;
24056
24057 if (token->type == CPP_NAME
24058 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24059 == CPP_COLON))
24060 {
24061 /* Get the name of the bitfield. */
24062 declarator = make_id_declarator (NULL_TREE,
24063 cp_parser_identifier (parser),
24064 sfk_none);
24065
24066 eat_colon:
24067 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24068 /* Get the width of the bitfield. */
24069 width
24070 = cp_parser_constant_expression (parser,
24071 /*allow_non_constant=*/false,
24072 NULL);
24073 }
24074 else
24075 {
24076 /* Parse the declarator. */
24077 declarator
24078 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24079 &ctor_dtor_or_conv_p,
24080 /*parenthesized_p=*/NULL,
24081 /*member_p=*/false);
24082 }
24083
24084 /* Look for attributes that apply to the ivar. */
24085 attributes = cp_parser_attributes_opt (parser);
24086 /* Remember which attributes are prefix attributes and
24087 which are not. */
24088 first_attribute = attributes;
24089 /* Combine the attributes. */
24090 attributes = chainon (prefix_attributes, attributes);
24091
24092 if (width)
24093 /* Create the bitfield declaration. */
24094 decl = grokbitfield (declarator, &declspecs,
24095 width,
24096 attributes);
24097 else
24098 decl = grokfield (declarator, &declspecs,
24099 NULL_TREE, /*init_const_expr_p=*/false,
24100 NULL_TREE, attributes);
24101
24102 /* Add the instance variable. */
7ef1fa91 24103 if (decl != error_mark_node && decl != NULL_TREE)
24104 objc_add_instance_variable (decl);
79408174 24105
24106 /* Reset PREFIX_ATTRIBUTES. */
24107 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24108 attributes = TREE_CHAIN (attributes);
24109 if (attributes)
24110 TREE_CHAIN (attributes) = NULL_TREE;
24111
24112 token = cp_lexer_peek_token (parser->lexer);
24113
24114 if (token->type == CPP_COMMA)
24115 {
24116 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24117 continue;
24118 }
24119 break;
24120 }
24121
24122 cp_parser_consume_semicolon_at_end_of_statement (parser);
24123 token = cp_lexer_peek_token (parser->lexer);
24124 }
24125
24126 if (token->keyword == RID_AT_END)
24127 cp_parser_error (parser, "expected %<}%>");
24128
24129 /* Do not consume the RID_AT_END, so it will be read again as terminating
24130 the @interface of @implementation. */
24131 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24132 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
24133
24134 /* For historical reasons, we accept an optional semicolon. */
24135 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24136 cp_lexer_consume_token (parser->lexer);
24137}
24138
24139/* Parse an Objective-C protocol declaration. */
24140
24141static void
24142cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24143{
24144 tree proto, protorefs;
24145 cp_token *tok;
24146
24147 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
24148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24149 {
24150 tok = cp_lexer_peek_token (parser->lexer);
24151 error_at (tok->location, "identifier expected after %<@protocol%>");
a758bf7d 24152 cp_parser_consume_semicolon_at_end_of_statement (parser);
24153 return;
79408174 24154 }
24155
24156 /* See if we have a forward declaration or a definition. */
24157 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24158
24159 /* Try a forward declaration first. */
24160 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24161 {
a758bf7d 24162 while (true)
24163 {
24164 tree id;
24165
24166 id = cp_parser_identifier (parser);
24167 if (id == error_mark_node)
24168 break;
24169
24170 objc_declare_protocol (id, attributes);
24171
24172 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24173 cp_lexer_consume_token (parser->lexer);
24174 else
24175 break;
24176 }
79408174 24177 cp_parser_consume_semicolon_at_end_of_statement (parser);
24178 }
24179
24180 /* Ok, we got a full-fledged definition (or at least should). */
24181 else
24182 {
24183 proto = cp_parser_identifier (parser);
24184 protorefs = cp_parser_objc_protocol_refs_opt (parser);
24185 objc_start_protocol (proto, protorefs, attributes);
24186 cp_parser_objc_method_prototype_list (parser);
24187 }
24188}
24189
24190/* Parse an Objective-C superclass or category. */
24191
24192static void
24193cp_parser_objc_superclass_or_category (cp_parser *parser,
24194 bool iface_p,
24195 tree *super,
24196 tree *categ, bool *is_class_extension)
24197{
24198 cp_token *next = cp_lexer_peek_token (parser->lexer);
24199
24200 *super = *categ = NULL_TREE;
24201 *is_class_extension = false;
24202 if (next->type == CPP_COLON)
24203 {
24204 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
24205 *super = cp_parser_identifier (parser);
24206 }
24207 else if (next->type == CPP_OPEN_PAREN)
24208 {
24209 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
24210
24211 /* If there is no category name, and this is an @interface, we
24212 have a class extension. */
24213 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24214 {
24215 *categ = NULL_TREE;
24216 *is_class_extension = true;
24217 }
24218 else
24219 *categ = cp_parser_identifier (parser);
24220
24221 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24222 }
24223}
24224
24225/* Parse an Objective-C class interface. */
24226
24227static void
24228cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24229{
24230 tree name, super, categ, protos;
24231 bool is_class_extension;
24232
24233 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
24234 name = cp_parser_identifier (parser);
24235 if (name == error_mark_node)
24236 {
24237 /* It's hard to recover because even if valid @interface stuff
24238 is to follow, we can't compile it (or validate it) if we
24239 don't even know which class it refers to. Let's assume this
24240 was a stray '@interface' token in the stream and skip it.
24241 */
24242 return;
24243 }
24244 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24245 &is_class_extension);
24246 protos = cp_parser_objc_protocol_refs_opt (parser);
24247
24248 /* We have either a class or a category on our hands. */
24249 if (categ || is_class_extension)
24250 objc_start_category_interface (name, categ, protos, attributes);
24251 else
24252 {
24253 objc_start_class_interface (name, super, protos, attributes);
24254 /* Handle instance variable declarations, if any. */
24255 cp_parser_objc_class_ivars (parser);
24256 objc_continue_interface ();
24257 }
24258
24259 cp_parser_objc_method_prototype_list (parser);
24260}
24261
24262/* Parse an Objective-C class implementation. */
24263
24264static void
24265cp_parser_objc_class_implementation (cp_parser* parser)
24266{
24267 tree name, super, categ;
24268 bool is_class_extension;
24269
24270 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
24271 name = cp_parser_identifier (parser);
24272 if (name == error_mark_node)
24273 {
24274 /* It's hard to recover because even if valid @implementation
24275 stuff is to follow, we can't compile it (or validate it) if
24276 we don't even know which class it refers to. Let's assume
24277 this was a stray '@implementation' token in the stream and
24278 skip it.
24279 */
24280 return;
24281 }
24282 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24283 &is_class_extension);
24284
24285 /* We have either a class or a category on our hands. */
24286 if (categ)
24287 objc_start_category_implementation (name, categ);
24288 else
24289 {
24290 objc_start_class_implementation (name, super);
24291 /* Handle instance variable declarations, if any. */
24292 cp_parser_objc_class_ivars (parser);
24293 objc_continue_implementation ();
24294 }
24295
24296 cp_parser_objc_method_definition_list (parser);
24297}
24298
24299/* Consume the @end token and finish off the implementation. */
24300
24301static void
24302cp_parser_objc_end_implementation (cp_parser* parser)
24303{
24304 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
24305 objc_finish_implementation ();
24306}
24307
24308/* Parse an Objective-C declaration. */
24309
24310static void
24311cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24312{
24313 /* Try to figure out what kind of declaration is present. */
24314 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24315
24316 if (attributes)
24317 switch (kwd->keyword)
24318 {
24319 case RID_AT_ALIAS:
24320 case RID_AT_CLASS:
24321 case RID_AT_END:
24322 error_at (kwd->location, "attributes may not be specified before"
24323 " the %<@%D%> Objective-C++ keyword",
24324 kwd->u.value);
24325 attributes = NULL;
24326 break;
24327 case RID_AT_IMPLEMENTATION:
24328 warning_at (kwd->location, OPT_Wattributes,
24329 "prefix attributes are ignored before %<@%D%>",
24330 kwd->u.value);
24331 attributes = NULL;
24332 default:
24333 break;
24334 }
24335
24336 switch (kwd->keyword)
24337 {
24338 case RID_AT_ALIAS:
24339 cp_parser_objc_alias_declaration (parser);
24340 break;
24341 case RID_AT_CLASS:
24342 cp_parser_objc_class_declaration (parser);
24343 break;
24344 case RID_AT_PROTOCOL:
24345 cp_parser_objc_protocol_declaration (parser, attributes);
24346 break;
24347 case RID_AT_INTERFACE:
24348 cp_parser_objc_class_interface (parser, attributes);
24349 break;
24350 case RID_AT_IMPLEMENTATION:
24351 cp_parser_objc_class_implementation (parser);
24352 break;
24353 case RID_AT_END:
24354 cp_parser_objc_end_implementation (parser);
24355 break;
24356 default:
24357 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24358 kwd->u.value);
24359 cp_parser_skip_to_end_of_block_or_statement (parser);
24360 }
24361}
24362
24363/* Parse an Objective-C try-catch-finally statement.
24364
24365 objc-try-catch-finally-stmt:
24366 @try compound-statement objc-catch-clause-seq [opt]
24367 objc-finally-clause [opt]
24368
24369 objc-catch-clause-seq:
24370 objc-catch-clause objc-catch-clause-seq [opt]
24371
24372 objc-catch-clause:
24373 @catch ( objc-exception-declaration ) compound-statement
24374
24375 objc-finally-clause:
24376 @finally compound-statement
24377
24378 objc-exception-declaration:
24379 parameter-declaration
24380 '...'
24381
24382 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24383
24384 Returns NULL_TREE.
24385
24386 PS: This function is identical to c_parser_objc_try_catch_finally_statement
24387 for C. Keep them in sync. */
24388
24389static tree
24390cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24391{
24392 location_t location;
24393 tree stmt;
24394
24395 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24396 location = cp_lexer_peek_token (parser->lexer)->location;
24397 objc_maybe_warn_exceptions (location);
24398 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24399 node, lest it get absorbed into the surrounding block. */
24400 stmt = push_stmt_list ();
6bf5e78d 24401 cp_parser_compound_statement (parser, NULL, false, false);
79408174 24402 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24403
24404 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24405 {
24406 cp_parameter_declarator *parm;
24407 tree parameter_declaration = error_mark_node;
24408 bool seen_open_paren = false;
24409
24410 cp_lexer_consume_token (parser->lexer);
24411 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24412 seen_open_paren = true;
24413 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24414 {
24415 /* We have "@catch (...)" (where the '...' are literally
24416 what is in the code). Skip the '...'.
24417 parameter_declaration is set to NULL_TREE, and
24418 objc_being_catch_clauses() knows that that means
24419 '...'. */
24420 cp_lexer_consume_token (parser->lexer);
24421 parameter_declaration = NULL_TREE;
24422 }
24423 else
24424 {
24425 /* We have "@catch (NSException *exception)" or something
24426 like that. Parse the parameter declaration. */
24427 parm = cp_parser_parameter_declaration (parser, false, NULL);
24428 if (parm == NULL)
24429 parameter_declaration = error_mark_node;
24430 else
24431 parameter_declaration = grokdeclarator (parm->declarator,
24432 &parm->decl_specifiers,
24433 PARM, /*initialized=*/0,
24434 /*attrlist=*/NULL);
24435 }
24436 if (seen_open_paren)
24437 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24438 else
24439 {
24440 /* If there was no open parenthesis, we are recovering from
24441 an error, and we are trying to figure out what mistake
24442 the user has made. */
24443
24444 /* If there is an immediate closing parenthesis, the user
24445 probably forgot the opening one (ie, they typed "@catch
24446 NSException *e)". Parse the closing parenthesis and keep
24447 going. */
24448 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24449 cp_lexer_consume_token (parser->lexer);
24450
24451 /* If these is no immediate closing parenthesis, the user
24452 probably doesn't know that parenthesis are required at
24453 all (ie, they typed "@catch NSException *e"). So, just
24454 forget about the closing parenthesis and keep going. */
24455 }
24456 objc_begin_catch_clause (parameter_declaration);
6bf5e78d 24457 cp_parser_compound_statement (parser, NULL, false, false);
79408174 24458 objc_finish_catch_clause ();
24459 }
24460 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24461 {
24462 cp_lexer_consume_token (parser->lexer);
24463 location = cp_lexer_peek_token (parser->lexer)->location;
24464 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24465 node, lest it get absorbed into the surrounding block. */
24466 stmt = push_stmt_list ();
6bf5e78d 24467 cp_parser_compound_statement (parser, NULL, false, false);
79408174 24468 objc_build_finally_clause (location, pop_stmt_list (stmt));
24469 }
24470
24471 return objc_finish_try_stmt ();
24472}
24473
24474/* Parse an Objective-C synchronized statement.
24475
24476 objc-synchronized-stmt:
24477 @synchronized ( expression ) compound-statement
24478
24479 Returns NULL_TREE. */
24480
24481static tree
24482cp_parser_objc_synchronized_statement (cp_parser *parser)
24483{
24484 location_t location;
24485 tree lock, stmt;
24486
24487 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24488
24489 location = cp_lexer_peek_token (parser->lexer)->location;
24490 objc_maybe_warn_exceptions (location);
24491 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24492 lock = cp_parser_expression (parser, false, NULL);
24493 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24494
24495 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24496 node, lest it get absorbed into the surrounding block. */
24497 stmt = push_stmt_list ();
6bf5e78d 24498 cp_parser_compound_statement (parser, NULL, false, false);
79408174 24499
24500 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24501}
24502
24503/* Parse an Objective-C throw statement.
24504
24505 objc-throw-stmt:
24506 @throw assignment-expression [opt] ;
24507
24508 Returns a constructed '@throw' statement. */
24509
24510static tree
24511cp_parser_objc_throw_statement (cp_parser *parser)
24512{
24513 tree expr = NULL_TREE;
24514 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24515
24516 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24517
24518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24519 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24520
24521 cp_parser_consume_semicolon_at_end_of_statement (parser);
24522
24523 return objc_build_throw_stmt (loc, expr);
24524}
24525
24526/* Parse an Objective-C statement. */
24527
24528static tree
24529cp_parser_objc_statement (cp_parser * parser)
24530{
24531 /* Try to figure out what kind of declaration is present. */
24532 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24533
24534 switch (kwd->keyword)
24535 {
24536 case RID_AT_TRY:
24537 return cp_parser_objc_try_catch_finally_statement (parser);
24538 case RID_AT_SYNCHRONIZED:
24539 return cp_parser_objc_synchronized_statement (parser);
24540 case RID_AT_THROW:
24541 return cp_parser_objc_throw_statement (parser);
24542 default:
24543 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24544 kwd->u.value);
24545 cp_parser_skip_to_end_of_block_or_statement (parser);
24546 }
24547
24548 return error_mark_node;
24549}
24550
24551/* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24552 look ahead to see if an objc keyword follows the attributes. This
24553 is to detect the use of prefix attributes on ObjC @interface and
24554 @protocol. */
24555
24556static bool
24557cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24558{
24559 cp_lexer_save_tokens (parser->lexer);
24560 *attrib = cp_parser_attributes_opt (parser);
24561 gcc_assert (*attrib);
24562 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24563 {
24564 cp_lexer_commit_tokens (parser->lexer);
24565 return true;
24566 }
24567 cp_lexer_rollback_tokens (parser->lexer);
24568 return false;
24569}
24570
24571/* This routine is a minimal replacement for
24572 c_parser_struct_declaration () used when parsing the list of
24573 types/names or ObjC++ properties. For example, when parsing the
24574 code
24575
24576 @property (readonly) int a, b, c;
24577
24578 this function is responsible for parsing "int a, int b, int c" and
24579 returning the declarations as CHAIN of DECLs.
24580
24581 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24582 similar parsing. */
24583static tree
24584cp_parser_objc_struct_declaration (cp_parser *parser)
24585{
24586 tree decls = NULL_TREE;
24587 cp_decl_specifier_seq declspecs;
24588 int decl_class_or_enum_p;
24589 tree prefix_attributes;
24590
24591 cp_parser_decl_specifier_seq (parser,
24592 CP_PARSER_FLAGS_NONE,
24593 &declspecs,
24594 &decl_class_or_enum_p);
24595
24596 if (declspecs.type == error_mark_node)
24597 return error_mark_node;
24598
24599 /* auto, register, static, extern, mutable. */
24600 if (declspecs.storage_class != sc_none)
24601 {
24602 cp_parser_error (parser, "invalid type for property");
24603 declspecs.storage_class = sc_none;
24604 }
24605
24606 /* __thread. */
a60f3e81 24607 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
79408174 24608 {
24609 cp_parser_error (parser, "invalid type for property");
a60f3e81 24610 declspecs.locations[ds_thread] = 0;
79408174 24611 }
24612
24613 /* typedef. */
a60f3e81 24614 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
79408174 24615 {
24616 cp_parser_error (parser, "invalid type for property");
a60f3e81 24617 declspecs.locations[ds_typedef] = 0;
79408174 24618 }
24619
24620 prefix_attributes = declspecs.attributes;
24621 declspecs.attributes = NULL_TREE;
24622
24623 /* Keep going until we hit the `;' at the end of the declaration. */
24624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24625 {
24626 tree attributes, first_attribute, decl;
24627 cp_declarator *declarator;
24628 cp_token *token;
24629
24630 /* Parse the declarator. */
24631 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24632 NULL, NULL, false);
24633
24634 /* Look for attributes that apply to the ivar. */
24635 attributes = cp_parser_attributes_opt (parser);
24636 /* Remember which attributes are prefix attributes and
24637 which are not. */
24638 first_attribute = attributes;
24639 /* Combine the attributes. */
24640 attributes = chainon (prefix_attributes, attributes);
24641
24642 decl = grokfield (declarator, &declspecs,
24643 NULL_TREE, /*init_const_expr_p=*/false,
24644 NULL_TREE, attributes);
24645
24646 if (decl == error_mark_node || decl == NULL_TREE)
24647 return error_mark_node;
24648
24649 /* Reset PREFIX_ATTRIBUTES. */
24650 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24651 attributes = TREE_CHAIN (attributes);
24652 if (attributes)
24653 TREE_CHAIN (attributes) = NULL_TREE;
24654
24655 DECL_CHAIN (decl) = decls;
24656 decls = decl;
24657
24658 token = cp_lexer_peek_token (parser->lexer);
24659 if (token->type == CPP_COMMA)
24660 {
24661 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24662 continue;
24663 }
24664 else
24665 break;
24666 }
24667 return decls;
24668}
24669
24670/* Parse an Objective-C @property declaration. The syntax is:
24671
24672 objc-property-declaration:
24673 '@property' objc-property-attributes[opt] struct-declaration ;
24674
24675 objc-property-attributes:
24676 '(' objc-property-attribute-list ')'
24677
24678 objc-property-attribute-list:
24679 objc-property-attribute
24680 objc-property-attribute-list, objc-property-attribute
24681
24682 objc-property-attribute
24683 'getter' = identifier
24684 'setter' = identifier
24685 'readonly'
24686 'readwrite'
24687 'assign'
24688 'retain'
24689 'copy'
24690 'nonatomic'
24691
24692 For example:
24693 @property NSString *name;
24694 @property (readonly) id object;
24695 @property (retain, nonatomic, getter=getTheName) id name;
24696 @property int a, b, c;
24697
24698 PS: This function is identical to
24699 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24700static void
24701cp_parser_objc_at_property_declaration (cp_parser *parser)
24702{
24703 /* The following variables hold the attributes of the properties as
24704 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24705 seen. When we see an attribute, we set them to 'true' (if they
24706 are boolean properties) or to the identifier (if they have an
24707 argument, ie, for getter and setter). Note that here we only
24708 parse the list of attributes, check the syntax and accumulate the
24709 attributes that we find. objc_add_property_declaration() will
24710 then process the information. */
24711 bool property_assign = false;
24712 bool property_copy = false;
24713 tree property_getter_ident = NULL_TREE;
24714 bool property_nonatomic = false;
24715 bool property_readonly = false;
24716 bool property_readwrite = false;
24717 bool property_retain = false;
24718 tree property_setter_ident = NULL_TREE;
24719
24720 /* 'properties' is the list of properties that we read. Usually a
24721 single one, but maybe more (eg, in "@property int a, b, c;" there
24722 are three). */
24723 tree properties;
24724 location_t loc;
24725
24726 loc = cp_lexer_peek_token (parser->lexer)->location;
24727
24728 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24729
24730 /* Parse the optional attribute list... */
24731 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24732 {
24733 /* Eat the '('. */
24734 cp_lexer_consume_token (parser->lexer);
24735
24736 while (true)
24737 {
24738 bool syntax_error = false;
24739 cp_token *token = cp_lexer_peek_token (parser->lexer);
24740 enum rid keyword;
24741
24742 if (token->type != CPP_NAME)
24743 {
24744 cp_parser_error (parser, "expected identifier");
24745 break;
24746 }
24747 keyword = C_RID_CODE (token->u.value);
24748 cp_lexer_consume_token (parser->lexer);
24749 switch (keyword)
24750 {
24751 case RID_ASSIGN: property_assign = true; break;
24752 case RID_COPY: property_copy = true; break;
24753 case RID_NONATOMIC: property_nonatomic = true; break;
24754 case RID_READONLY: property_readonly = true; break;
24755 case RID_READWRITE: property_readwrite = true; break;
24756 case RID_RETAIN: property_retain = true; break;
24757
24758 case RID_GETTER:
24759 case RID_SETTER:
24760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24761 {
24762 if (keyword == RID_GETTER)
24763 cp_parser_error (parser,
24764 "missing %<=%> (after %<getter%> attribute)");
24765 else
24766 cp_parser_error (parser,
24767 "missing %<=%> (after %<setter%> attribute)");
24768 syntax_error = true;
24769 break;
24770 }
24771 cp_lexer_consume_token (parser->lexer); /* eat the = */
d0b0b694 24772 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
79408174 24773 {
24774 cp_parser_error (parser, "expected identifier");
24775 syntax_error = true;
24776 break;
24777 }
24778 if (keyword == RID_SETTER)
24779 {
24780 if (property_setter_ident != NULL_TREE)
d0b0b694 24781 {
24782 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24783 cp_lexer_consume_token (parser->lexer);
24784 }
79408174 24785 else
d0b0b694 24786 property_setter_ident = cp_parser_objc_selector (parser);
79408174 24787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24788 cp_parser_error (parser, "setter name must terminate with %<:%>");
24789 else
24790 cp_lexer_consume_token (parser->lexer);
24791 }
24792 else
24793 {
24794 if (property_getter_ident != NULL_TREE)
d0b0b694 24795 {
24796 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24797 cp_lexer_consume_token (parser->lexer);
24798 }
79408174 24799 else
d0b0b694 24800 property_getter_ident = cp_parser_objc_selector (parser);
79408174 24801 }
24802 break;
24803 default:
24804 cp_parser_error (parser, "unknown property attribute");
24805 syntax_error = true;
24806 break;
24807 }
24808
24809 if (syntax_error)
24810 break;
24811
24812 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24813 cp_lexer_consume_token (parser->lexer);
24814 else
24815 break;
24816 }
24817
24818 /* FIXME: "@property (setter, assign);" will generate a spurious
24819 "error: expected ‘)’ before ‘,’ token". This is because
24820 cp_parser_require, unlike the C counterpart, will produce an
24821 error even if we are in error recovery. */
24822 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24823 {
24824 cp_parser_skip_to_closing_parenthesis (parser,
24825 /*recovering=*/true,
24826 /*or_comma=*/false,
24827 /*consume_paren=*/true);
24828 }
24829 }
24830
24831 /* ... and the property declaration(s). */
24832 properties = cp_parser_objc_struct_declaration (parser);
24833
24834 if (properties == error_mark_node)
24835 {
24836 cp_parser_skip_to_end_of_statement (parser);
24837 /* If the next token is now a `;', consume it. */
24838 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24839 cp_lexer_consume_token (parser->lexer);
24840 return;
24841 }
24842
24843 if (properties == NULL_TREE)
24844 cp_parser_error (parser, "expected identifier");
24845 else
24846 {
24847 /* Comma-separated properties are chained together in
24848 reverse order; add them one by one. */
24849 properties = nreverse (properties);
24850
24851 for (; properties; properties = TREE_CHAIN (properties))
24852 objc_add_property_declaration (loc, copy_node (properties),
24853 property_readonly, property_readwrite,
24854 property_assign, property_retain,
24855 property_copy, property_nonatomic,
24856 property_getter_ident, property_setter_ident);
24857 }
24858
24859 cp_parser_consume_semicolon_at_end_of_statement (parser);
24860}
24861
24862/* Parse an Objective-C++ @synthesize declaration. The syntax is:
24863
24864 objc-synthesize-declaration:
24865 @synthesize objc-synthesize-identifier-list ;
24866
24867 objc-synthesize-identifier-list:
24868 objc-synthesize-identifier
24869 objc-synthesize-identifier-list, objc-synthesize-identifier
24870
24871 objc-synthesize-identifier
24872 identifier
24873 identifier = identifier
24874
24875 For example:
24876 @synthesize MyProperty;
24877 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24878
24879 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24880 for C. Keep them in sync.
24881*/
24882static void
24883cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24884{
24885 tree list = NULL_TREE;
24886 location_t loc;
24887 loc = cp_lexer_peek_token (parser->lexer)->location;
24888
24889 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24890 while (true)
24891 {
24892 tree property, ivar;
24893 property = cp_parser_identifier (parser);
24894 if (property == error_mark_node)
24895 {
24896 cp_parser_consume_semicolon_at_end_of_statement (parser);
24897 return;
24898 }
24899 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24900 {
24901 cp_lexer_consume_token (parser->lexer);
24902 ivar = cp_parser_identifier (parser);
24903 if (ivar == error_mark_node)
24904 {
24905 cp_parser_consume_semicolon_at_end_of_statement (parser);
24906 return;
24907 }
24908 }
24909 else
24910 ivar = NULL_TREE;
24911 list = chainon (list, build_tree_list (ivar, property));
24912 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24913 cp_lexer_consume_token (parser->lexer);
24914 else
24915 break;
24916 }
24917 cp_parser_consume_semicolon_at_end_of_statement (parser);
24918 objc_add_synthesize_declaration (loc, list);
24919}
24920
24921/* Parse an Objective-C++ @dynamic declaration. The syntax is:
24922
24923 objc-dynamic-declaration:
24924 @dynamic identifier-list ;
24925
24926 For example:
24927 @dynamic MyProperty;
24928 @dynamic MyProperty, AnotherProperty;
24929
24930 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24931 for C. Keep them in sync.
24932*/
24933static void
24934cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24935{
24936 tree list = NULL_TREE;
24937 location_t loc;
24938 loc = cp_lexer_peek_token (parser->lexer)->location;
24939
24940 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24941 while (true)
24942 {
24943 tree property;
24944 property = cp_parser_identifier (parser);
24945 if (property == error_mark_node)
24946 {
24947 cp_parser_consume_semicolon_at_end_of_statement (parser);
24948 return;
24949 }
24950 list = chainon (list, build_tree_list (NULL, property));
24951 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24952 cp_lexer_consume_token (parser->lexer);
24953 else
24954 break;
24955 }
24956 cp_parser_consume_semicolon_at_end_of_statement (parser);
24957 objc_add_dynamic_declaration (loc, list);
24958}
e1f293c0 24959
8487df40 24960\f
24961/* OpenMP 2.5 parsing routines. */
24962
8487df40 24963/* Returns name of the next clause.
24964 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24965 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24966 returned and the token is consumed. */
24967
24968static pragma_omp_clause
24969cp_parser_omp_clause_name (cp_parser *parser)
24970{
24971 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24972
24973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24974 result = PRAGMA_OMP_CLAUSE_IF;
24975 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24976 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24977 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24978 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24979 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24980 {
3369eb76 24981 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 24982 const char *p = IDENTIFIER_POINTER (id);
24983
24984 switch (p[0])
24985 {
24986 case 'c':
fd6481cf 24987 if (!strcmp ("collapse", p))
24988 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24989 else if (!strcmp ("copyin", p))
8487df40 24990 result = PRAGMA_OMP_CLAUSE_COPYIN;
074ab442 24991 else if (!strcmp ("copyprivate", p))
8487df40 24992 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24993 break;
24994 case 'f':
2169f33b 24995 if (!strcmp ("final", p))
24996 result = PRAGMA_OMP_CLAUSE_FINAL;
24997 else if (!strcmp ("firstprivate", p))
8487df40 24998 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24999 break;
25000 case 'l':
25001 if (!strcmp ("lastprivate", p))
25002 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
25003 break;
2169f33b 25004 case 'm':
25005 if (!strcmp ("mergeable", p))
25006 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
25007 break;
8487df40 25008 case 'n':
25009 if (!strcmp ("nowait", p))
25010 result = PRAGMA_OMP_CLAUSE_NOWAIT;
25011 else if (!strcmp ("num_threads", p))
25012 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
25013 break;
25014 case 'o':
25015 if (!strcmp ("ordered", p))
25016 result = PRAGMA_OMP_CLAUSE_ORDERED;
25017 break;
25018 case 'r':
25019 if (!strcmp ("reduction", p))
25020 result = PRAGMA_OMP_CLAUSE_REDUCTION;
25021 break;
25022 case 's':
25023 if (!strcmp ("schedule", p))
25024 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
25025 else if (!strcmp ("shared", p))
25026 result = PRAGMA_OMP_CLAUSE_SHARED;
25027 break;
fd6481cf 25028 case 'u':
25029 if (!strcmp ("untied", p))
25030 result = PRAGMA_OMP_CLAUSE_UNTIED;
25031 break;
8487df40 25032 }
25033 }
0a3b29ad 25034
8487df40 25035 if (result != PRAGMA_OMP_CLAUSE_NONE)
25036 cp_lexer_consume_token (parser->lexer);
0a3b29ad 25037
8487df40 25038 return result;
25039}
b75b98aa 25040
8487df40 25041/* Validate that a clause of the given type does not already exist. */
b75b98aa 25042
25043static void
590c3166 25044check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
ad9ae192 25045 const char *name, location_t location)
b75b98aa 25046{
8487df40 25047 tree c;
b75b98aa 25048
8487df40 25049 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25050 if (OMP_CLAUSE_CODE (c) == code)
25051 {
ccb59bb6 25052 error_at (location, "too many %qs clauses", name);
8487df40 25053 break;
25054 }
25055}
b75b98aa 25056
8487df40 25057/* OpenMP 2.5:
25058 variable-list:
25059 identifier
25060 variable-list , identifier
25061
25062 In addition, we match a closing parenthesis. An opening parenthesis
25063 will have been consumed by the caller.
25064
25065 If KIND is nonzero, create the appropriate node and install the decl
25066 in OMP_CLAUSE_DECL and add the node to the head of the list.
25067
25068 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
25069 return the list created. */
25070
25071static tree
25072cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
25073 tree list)
25074{
ad9ae192 25075 cp_token *token;
8487df40 25076 while (1)
b75b98aa 25077 {
8487df40 25078 tree name, decl;
b75b98aa 25079
ad9ae192 25080 token = cp_lexer_peek_token (parser->lexer);
8487df40 25081 name = cp_parser_id_expression (parser, /*template_p=*/false,
25082 /*check_dependency_p=*/true,
25083 /*template_p=*/NULL,
197c9df7 25084 /*declarator_p=*/false,
130bb1d4 25085 /*optional_p=*/false);
8487df40 25086 if (name == error_mark_node)
25087 goto skip_comma;
25088
ad9ae192 25089 decl = cp_parser_lookup_name_simple (parser, name, token->location);
8487df40 25090 if (decl == error_mark_node)
c247dce0 25091 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25092 token->location);
8487df40 25093 else if (kind != 0)
25094 {
e60a6f7b 25095 tree u = build_omp_clause (token->location, kind);
8487df40 25096 OMP_CLAUSE_DECL (u) = decl;
25097 OMP_CLAUSE_CHAIN (u) = list;
25098 list = u;
25099 }
25100 else
25101 list = tree_cons (decl, NULL_TREE, list);
25102
25103 get_comma:
25104 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25105 break;
25106 cp_lexer_consume_token (parser->lexer);
25107 }
25108
c247dce0 25109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 25110 {
25111 int ending;
25112
25113 /* Try to resync to an unnested comma. Copied from
25114 cp_parser_parenthesized_expression_list. */
25115 skip_comma:
25116 ending = cp_parser_skip_to_closing_parenthesis (parser,
25117 /*recovering=*/true,
25118 /*or_comma=*/true,
25119 /*consume_paren=*/true);
25120 if (ending < 0)
25121 goto get_comma;
25122 }
25123
25124 return list;
25125}
25126
25127/* Similarly, but expect leading and trailing parenthesis. This is a very
25128 common case for omp clauses. */
25129
25130static tree
25131cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25132{
c247dce0 25133 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25134 return cp_parser_omp_var_list_no_open (parser, kind, list);
25135 return list;
25136}
25137
fd6481cf 25138/* OpenMP 3.0:
25139 collapse ( constant-expression ) */
25140
25141static tree
ad9ae192 25142cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
fd6481cf 25143{
25144 tree c, num;
25145 location_t loc;
25146 HOST_WIDE_INT n;
25147
25148 loc = cp_lexer_peek_token (parser->lexer)->location;
c247dce0 25149 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
fd6481cf 25150 return list;
25151
25152 num = cp_parser_constant_expression (parser, false, NULL);
25153
c247dce0 25154 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
fd6481cf 25155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25156 /*or_comma=*/false,
25157 /*consume_paren=*/true);
25158
25159 if (num == error_mark_node)
25160 return list;
25161 num = fold_non_dependent_expr (num);
25162 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25163 || !host_integerp (num, 0)
25164 || (n = tree_low_cst (num, 0)) <= 0
25165 || (int) n != n)
25166 {
ccb59bb6 25167 error_at (loc, "collapse argument needs positive constant integer expression");
fd6481cf 25168 return list;
25169 }
25170
ad9ae192 25171 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
e60a6f7b 25172 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
fd6481cf 25173 OMP_CLAUSE_CHAIN (c) = list;
25174 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25175
25176 return c;
25177}
25178
8487df40 25179/* OpenMP 2.5:
25180 default ( shared | none ) */
25181
25182static tree
ad9ae192 25183cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
8487df40 25184{
25185 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25186 tree c;
25187
c247dce0 25188 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25189 return list;
25190 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25191 {
3369eb76 25192 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 25193 const char *p = IDENTIFIER_POINTER (id);
25194
25195 switch (p[0])
25196 {
25197 case 'n':
25198 if (strcmp ("none", p) != 0)
25199 goto invalid_kind;
25200 kind = OMP_CLAUSE_DEFAULT_NONE;
25201 break;
25202
25203 case 's':
25204 if (strcmp ("shared", p) != 0)
25205 goto invalid_kind;
25206 kind = OMP_CLAUSE_DEFAULT_SHARED;
25207 break;
25208
25209 default:
25210 goto invalid_kind;
25211 }
25212
25213 cp_lexer_consume_token (parser->lexer);
b75b98aa 25214 }
25215 else
8487df40 25216 {
25217 invalid_kind:
25218 cp_parser_error (parser, "expected %<none%> or %<shared%>");
25219 }
b75b98aa 25220
c247dce0 25221 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 25222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25223 /*or_comma=*/false,
25224 /*consume_paren=*/true);
074ab442 25225
8487df40 25226 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25227 return list;
b75b98aa 25228
ad9ae192 25229 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
e60a6f7b 25230 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
8487df40 25231 OMP_CLAUSE_CHAIN (c) = list;
25232 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
b75b98aa 25233
8487df40 25234 return c;
b75b98aa 25235}
25236
2169f33b 25237/* OpenMP 3.1:
25238 final ( expression ) */
25239
25240static tree
25241cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25242{
25243 tree t, c;
25244
25245 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25246 return list;
25247
25248 t = cp_parser_condition (parser);
25249
25250 if (t == error_mark_node
25251 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25252 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25253 /*or_comma=*/false,
25254 /*consume_paren=*/true);
25255
25256 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25257
25258 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25259 OMP_CLAUSE_FINAL_EXPR (c) = t;
25260 OMP_CLAUSE_CHAIN (c) = list;
25261
25262 return c;
25263}
25264
8487df40 25265/* OpenMP 2.5:
25266 if ( expression ) */
b75b98aa 25267
8487df40 25268static tree
ad9ae192 25269cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
b75b98aa 25270{
8487df40 25271 tree t, c;
b75b98aa 25272
c247dce0 25273 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25274 return list;
b75b98aa 25275
8487df40 25276 t = cp_parser_condition (parser);
25277
25278 if (t == error_mark_node
c247dce0 25279 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 25280 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25281 /*or_comma=*/false,
25282 /*consume_paren=*/true);
25283
ad9ae192 25284 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
8487df40 25285
e60a6f7b 25286 c = build_omp_clause (location, OMP_CLAUSE_IF);
8487df40 25287 OMP_CLAUSE_IF_EXPR (c) = t;
25288 OMP_CLAUSE_CHAIN (c) = list;
25289
25290 return c;
25291}
25292
2169f33b 25293/* OpenMP 3.1:
25294 mergeable */
25295
25296static tree
25297cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25298 tree list, location_t location)
25299{
25300 tree c;
25301
25302 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25303 location);
25304
25305 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25306 OMP_CLAUSE_CHAIN (c) = list;
25307 return c;
25308}
25309
8487df40 25310/* OpenMP 2.5:
25311 nowait */
25312
25313static tree
ad9ae192 25314cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25315 tree list, location_t location)
8487df40 25316{
25317 tree c;
25318
ad9ae192 25319 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
8487df40 25320
e60a6f7b 25321 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
8487df40 25322 OMP_CLAUSE_CHAIN (c) = list;
25323 return c;
25324}
25325
25326/* OpenMP 2.5:
25327 num_threads ( expression ) */
25328
25329static tree
ad9ae192 25330cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25331 location_t location)
8487df40 25332{
25333 tree t, c;
25334
c247dce0 25335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25336 return list;
25337
98b326fd 25338 t = cp_parser_expression (parser, false, NULL);
8487df40 25339
25340 if (t == error_mark_node
c247dce0 25341 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 25342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25343 /*or_comma=*/false,
25344 /*consume_paren=*/true);
25345
ad9ae192 25346 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25347 "num_threads", location);
8487df40 25348
e60a6f7b 25349 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
8487df40 25350 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25351 OMP_CLAUSE_CHAIN (c) = list;
25352
25353 return c;
25354}
25355
25356/* OpenMP 2.5:
25357 ordered */
25358
25359static tree
ad9ae192 25360cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25361 tree list, location_t location)
8487df40 25362{
25363 tree c;
25364
ad9ae192 25365 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25366 "ordered", location);
8487df40 25367
e60a6f7b 25368 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
8487df40 25369 OMP_CLAUSE_CHAIN (c) = list;
25370 return c;
25371}
25372
25373/* OpenMP 2.5:
25374 reduction ( reduction-operator : variable-list )
25375
25376 reduction-operator:
2169f33b 25377 One of: + * - & ^ | && ||
25378
25379 OpenMP 3.1:
25380
25381 reduction-operator:
25382 One of: + * - & ^ | && || min max */
8487df40 25383
25384static tree
25385cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25386{
25387 enum tree_code code;
25388 tree nlist, c;
25389
c247dce0 25390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25391 return list;
25392
25393 switch (cp_lexer_peek_token (parser->lexer)->type)
b75b98aa 25394 {
8487df40 25395 case CPP_PLUS:
25396 code = PLUS_EXPR;
25397 break;
25398 case CPP_MULT:
25399 code = MULT_EXPR;
25400 break;
25401 case CPP_MINUS:
25402 code = MINUS_EXPR;
25403 break;
25404 case CPP_AND:
25405 code = BIT_AND_EXPR;
25406 break;
25407 case CPP_XOR:
25408 code = BIT_XOR_EXPR;
25409 break;
25410 case CPP_OR:
25411 code = BIT_IOR_EXPR;
25412 break;
25413 case CPP_AND_AND:
25414 code = TRUTH_ANDIF_EXPR;
25415 break;
25416 case CPP_OR_OR:
25417 code = TRUTH_ORIF_EXPR;
b75b98aa 25418 break;
2169f33b 25419 case CPP_NAME:
25420 {
25421 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25422 const char *p = IDENTIFIER_POINTER (id);
25423
25424 if (strcmp (p, "min") == 0)
25425 {
25426 code = MIN_EXPR;
25427 break;
25428 }
25429 if (strcmp (p, "max") == 0)
25430 {
25431 code = MAX_EXPR;
25432 break;
25433 }
25434 }
25435 /* FALLTHROUGH */
b75b98aa 25436 default:
361cc318 25437 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
2169f33b 25438 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
8487df40 25439 resync_fail:
25440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25441 /*or_comma=*/false,
25442 /*consume_paren=*/true);
25443 return list;
25444 }
25445 cp_lexer_consume_token (parser->lexer);
25446
c247dce0 25447 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
8487df40 25448 goto resync_fail;
25449
25450 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25451 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25452 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25453
25454 return nlist;
25455}
25456
25457/* OpenMP 2.5:
25458 schedule ( schedule-kind )
25459 schedule ( schedule-kind , expression )
25460
25461 schedule-kind:
fd6481cf 25462 static | dynamic | guided | runtime | auto */
8487df40 25463
25464static tree
ad9ae192 25465cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
8487df40 25466{
25467 tree c, t;
25468
c247dce0 25469 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8487df40 25470 return list;
25471
e60a6f7b 25472 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
8487df40 25473
25474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25475 {
3369eb76 25476 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 25477 const char *p = IDENTIFIER_POINTER (id);
25478
25479 switch (p[0])
25480 {
25481 case 'd':
25482 if (strcmp ("dynamic", p) != 0)
25483 goto invalid_kind;
25484 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25485 break;
25486
074ab442 25487 case 'g':
8487df40 25488 if (strcmp ("guided", p) != 0)
25489 goto invalid_kind;
25490 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25491 break;
25492
25493 case 'r':
25494 if (strcmp ("runtime", p) != 0)
25495 goto invalid_kind;
25496 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25497 break;
25498
25499 default:
25500 goto invalid_kind;
25501 }
25502 }
25503 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25504 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
fd6481cf 25505 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25506 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
8487df40 25507 else
25508 goto invalid_kind;
25509 cp_lexer_consume_token (parser->lexer);
25510
25511 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25512 {
ad9ae192 25513 cp_token *token;
8487df40 25514 cp_lexer_consume_token (parser->lexer);
25515
ad9ae192 25516 token = cp_lexer_peek_token (parser->lexer);
98b326fd 25517 t = cp_parser_assignment_expression (parser, false, NULL);
8487df40 25518
25519 if (t == error_mark_node)
25520 goto resync_fail;
25521 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
ccb59bb6 25522 error_at (token->location, "schedule %<runtime%> does not take "
25523 "a %<chunk_size%> parameter");
fd6481cf 25524 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
ccb59bb6 25525 error_at (token->location, "schedule %<auto%> does not take "
25526 "a %<chunk_size%> parameter");
8487df40 25527 else
25528 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25529
c247dce0 25530 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 25531 goto resync_fail;
25532 }
c247dce0 25533 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
8487df40 25534 goto resync_fail;
25535
ad9ae192 25536 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
8487df40 25537 OMP_CLAUSE_CHAIN (c) = list;
25538 return c;
25539
25540 invalid_kind:
25541 cp_parser_error (parser, "invalid schedule kind");
25542 resync_fail:
25543 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25544 /*or_comma=*/false,
25545 /*consume_paren=*/true);
25546 return list;
25547}
25548
fd6481cf 25549/* OpenMP 3.0:
25550 untied */
25551
25552static tree
ad9ae192 25553cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25554 tree list, location_t location)
fd6481cf 25555{
25556 tree c;
25557
ad9ae192 25558 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
fd6481cf 25559
e60a6f7b 25560 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
fd6481cf 25561 OMP_CLAUSE_CHAIN (c) = list;
25562 return c;
25563}
25564
8487df40 25565/* Parse all OpenMP clauses. The set clauses allowed by the directive
25566 is a bitmask in MASK. Return the list of clauses found; the result
25567 of clause default goes in *pdefault. */
25568
25569static tree
25570cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25571 const char *where, cp_token *pragma_tok)
25572{
25573 tree clauses = NULL;
77c8bed2 25574 bool first = true;
ad9ae192 25575 cp_token *token = NULL;
8487df40 25576
25577 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25578 {
77c8bed2 25579 pragma_omp_clause c_kind;
8487df40 25580 const char *c_name;
25581 tree prev = clauses;
25582
77c8bed2 25583 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25584 cp_lexer_consume_token (parser->lexer);
25585
ad9ae192 25586 token = cp_lexer_peek_token (parser->lexer);
77c8bed2 25587 c_kind = cp_parser_omp_clause_name (parser);
25588 first = false;
25589
8487df40 25590 switch (c_kind)
25591 {
fd6481cf 25592 case PRAGMA_OMP_CLAUSE_COLLAPSE:
ad9ae192 25593 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25594 token->location);
fd6481cf 25595 c_name = "collapse";
25596 break;
8487df40 25597 case PRAGMA_OMP_CLAUSE_COPYIN:
25598 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25599 c_name = "copyin";
25600 break;
25601 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25602 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25603 clauses);
25604 c_name = "copyprivate";
25605 break;
25606 case PRAGMA_OMP_CLAUSE_DEFAULT:
ad9ae192 25607 clauses = cp_parser_omp_clause_default (parser, clauses,
25608 token->location);
8487df40 25609 c_name = "default";
25610 break;
2169f33b 25611 case PRAGMA_OMP_CLAUSE_FINAL:
25612 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25613 c_name = "final";
25614 break;
8487df40 25615 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25616 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25617 clauses);
25618 c_name = "firstprivate";
25619 break;
25620 case PRAGMA_OMP_CLAUSE_IF:
ad9ae192 25621 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
8487df40 25622 c_name = "if";
25623 break;
25624 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25625 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25626 clauses);
25627 c_name = "lastprivate";
25628 break;
2169f33b 25629 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25630 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25631 token->location);
25632 c_name = "mergeable";
25633 break;
8487df40 25634 case PRAGMA_OMP_CLAUSE_NOWAIT:
ad9ae192 25635 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
8487df40 25636 c_name = "nowait";
25637 break;
25638 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
ad9ae192 25639 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25640 token->location);
8487df40 25641 c_name = "num_threads";
25642 break;
25643 case PRAGMA_OMP_CLAUSE_ORDERED:
ad9ae192 25644 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25645 token->location);
8487df40 25646 c_name = "ordered";
25647 break;
25648 case PRAGMA_OMP_CLAUSE_PRIVATE:
25649 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25650 clauses);
25651 c_name = "private";
25652 break;
25653 case PRAGMA_OMP_CLAUSE_REDUCTION:
25654 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25655 c_name = "reduction";
25656 break;
25657 case PRAGMA_OMP_CLAUSE_SCHEDULE:
ad9ae192 25658 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25659 token->location);
8487df40 25660 c_name = "schedule";
25661 break;
25662 case PRAGMA_OMP_CLAUSE_SHARED:
25663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25664 clauses);
25665 c_name = "shared";
25666 break;
fd6481cf 25667 case PRAGMA_OMP_CLAUSE_UNTIED:
ad9ae192 25668 clauses = cp_parser_omp_clause_untied (parser, clauses,
25669 token->location);
fd6481cf 25670 c_name = "nowait";
25671 break;
8487df40 25672 default:
25673 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25674 goto saw_error;
25675 }
25676
25677 if (((mask >> c_kind) & 1) == 0)
25678 {
25679 /* Remove the invalid clause(s) from the list to avoid
25680 confusing the rest of the compiler. */
25681 clauses = prev;
ccb59bb6 25682 error_at (token->location, "%qs is not valid for %qs", c_name, where);
8487df40 25683 }
25684 }
25685 saw_error:
25686 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25687 return finish_omp_clauses (clauses);
25688}
25689
25690/* OpenMP 2.5:
25691 structured-block:
25692 statement
25693
25694 In practice, we're also interested in adding the statement to an
25695 outer node. So it is convenient if we work around the fact that
25696 cp_parser_statement calls add_stmt. */
25697
25698static unsigned
25699cp_parser_begin_omp_structured_block (cp_parser *parser)
25700{
25701 unsigned save = parser->in_statement;
25702
25703 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25704 This preserves the "not within loop or switch" style error messages
25705 for nonsense cases like
25706 void foo() {
25707 #pragma omp single
25708 break;
25709 }
25710 */
25711 if (parser->in_statement)
25712 parser->in_statement = IN_OMP_BLOCK;
25713
25714 return save;
25715}
25716
25717static void
25718cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25719{
25720 parser->in_statement = save;
25721}
25722
25723static tree
25724cp_parser_omp_structured_block (cp_parser *parser)
25725{
25726 tree stmt = begin_omp_structured_block ();
25727 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25728
e534436e 25729 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 25730
25731 cp_parser_end_omp_structured_block (parser, save);
25732 return finish_omp_structured_block (stmt);
25733}
25734
25735/* OpenMP 2.5:
25736 # pragma omp atomic new-line
25737 expression-stmt
25738
25739 expression-stmt:
25740 x binop= expr | x++ | ++x | x-- | --x
25741 binop:
25742 +, *, -, /, &, ^, |, <<, >>
25743
2169f33b 25744 where x is an lvalue expression with scalar type.
25745
25746 OpenMP 3.1:
25747 # pragma omp atomic new-line
25748 update-stmt
25749
25750 # pragma omp atomic read new-line
25751 read-stmt
25752
25753 # pragma omp atomic write new-line
25754 write-stmt
25755
25756 # pragma omp atomic update new-line
25757 update-stmt
25758
25759 # pragma omp atomic capture new-line
25760 capture-stmt
25761
25762 # pragma omp atomic capture new-line
25763 capture-block
25764
25765 read-stmt:
25766 v = x
25767 write-stmt:
25768 x = expr
25769 update-stmt:
25770 expression-stmt | x = x binop expr
25771 capture-stmt:
25772 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25773 capture-block:
25774 { v = x; update-stmt; } | { update-stmt; v = x; }
25775
25776 where x and v are lvalue expressions with scalar type. */
8487df40 25777
25778static void
25779cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25780{
2169f33b 25781 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25782 tree rhs1 = NULL_TREE, orig_lhs;
25783 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25784 bool structured_block = false;
25785
25786 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25787 {
25788 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25789 const char *p = IDENTIFIER_POINTER (id);
8487df40 25790
2169f33b 25791 if (!strcmp (p, "read"))
25792 code = OMP_ATOMIC_READ;
25793 else if (!strcmp (p, "write"))
25794 code = NOP_EXPR;
25795 else if (!strcmp (p, "update"))
25796 code = OMP_ATOMIC;
25797 else if (!strcmp (p, "capture"))
25798 code = OMP_ATOMIC_CAPTURE_NEW;
25799 else
25800 p = NULL;
25801 if (p)
25802 cp_lexer_consume_token (parser->lexer);
25803 }
8487df40 25804 cp_parser_require_pragma_eol (parser, pragma_tok);
25805
2169f33b 25806 switch (code)
25807 {
25808 case OMP_ATOMIC_READ:
25809 case NOP_EXPR: /* atomic write */
25810 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25811 /*cast_p=*/false, NULL);
25812 if (v == error_mark_node)
25813 goto saw_error;
25814 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25815 goto saw_error;
25816 if (code == NOP_EXPR)
25817 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25818 else
25819 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25820 /*cast_p=*/false, NULL);
25821 if (lhs == error_mark_node)
25822 goto saw_error;
25823 if (code == NOP_EXPR)
25824 {
25825 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25826 opcode. */
25827 code = OMP_ATOMIC;
25828 rhs = lhs;
25829 lhs = v;
25830 v = NULL_TREE;
25831 }
25832 goto done;
25833 case OMP_ATOMIC_CAPTURE_NEW:
25834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25835 {
25836 cp_lexer_consume_token (parser->lexer);
25837 structured_block = true;
25838 }
25839 else
25840 {
25841 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25842 /*cast_p=*/false, NULL);
25843 if (v == error_mark_node)
25844 goto saw_error;
25845 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25846 goto saw_error;
25847 }
25848 default:
25849 break;
25850 }
25851
25852restart:
8487df40 25853 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
98b326fd 25854 /*cast_p=*/false, NULL);
2169f33b 25855 orig_lhs = lhs;
8487df40 25856 switch (TREE_CODE (lhs))
25857 {
25858 case ERROR_MARK:
25859 goto saw_error;
25860
8487df40 25861 case POSTINCREMENT_EXPR:
2169f33b 25862 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25863 code = OMP_ATOMIC_CAPTURE_OLD;
25864 /* FALLTHROUGH */
25865 case PREINCREMENT_EXPR:
8487df40 25866 lhs = TREE_OPERAND (lhs, 0);
2169f33b 25867 opcode = PLUS_EXPR;
8487df40 25868 rhs = integer_one_node;
25869 break;
25870
8487df40 25871 case POSTDECREMENT_EXPR:
2169f33b 25872 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25873 code = OMP_ATOMIC_CAPTURE_OLD;
25874 /* FALLTHROUGH */
25875 case PREDECREMENT_EXPR:
8487df40 25876 lhs = TREE_OPERAND (lhs, 0);
2169f33b 25877 opcode = MINUS_EXPR;
8487df40 25878 rhs = integer_one_node;
25879 break;
25880
db28a91b 25881 case COMPOUND_EXPR:
25882 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25883 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25884 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25885 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25886 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25887 (TREE_OPERAND (lhs, 1), 0), 0)))
25888 == BOOLEAN_TYPE)
25889 /* Undo effects of boolean_increment for post {in,de}crement. */
25890 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25891 /* FALLTHRU */
25892 case MODIFY_EXPR:
25893 if (TREE_CODE (lhs) == MODIFY_EXPR
25894 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
2169f33b 25895 {
25896 /* Undo effects of boolean_increment. */
25897 if (integer_onep (TREE_OPERAND (lhs, 1)))
25898 {
25899 /* This is pre or post increment. */
25900 rhs = TREE_OPERAND (lhs, 1);
25901 lhs = TREE_OPERAND (lhs, 0);
25902 opcode = NOP_EXPR;
25903 if (code == OMP_ATOMIC_CAPTURE_NEW
25904 && !structured_block
25905 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25906 code = OMP_ATOMIC_CAPTURE_OLD;
25907 break;
25908 }
25909 }
db28a91b 25910 /* FALLTHRU */
8487df40 25911 default:
25912 switch (cp_lexer_peek_token (parser->lexer)->type)
25913 {
25914 case CPP_MULT_EQ:
2169f33b 25915 opcode = MULT_EXPR;
8487df40 25916 break;
25917 case CPP_DIV_EQ:
2169f33b 25918 opcode = TRUNC_DIV_EXPR;
8487df40 25919 break;
25920 case CPP_PLUS_EQ:
2169f33b 25921 opcode = PLUS_EXPR;
8487df40 25922 break;
25923 case CPP_MINUS_EQ:
2169f33b 25924 opcode = MINUS_EXPR;
8487df40 25925 break;
25926 case CPP_LSHIFT_EQ:
2169f33b 25927 opcode = LSHIFT_EXPR;
8487df40 25928 break;
25929 case CPP_RSHIFT_EQ:
2169f33b 25930 opcode = RSHIFT_EXPR;
8487df40 25931 break;
25932 case CPP_AND_EQ:
2169f33b 25933 opcode = BIT_AND_EXPR;
8487df40 25934 break;
25935 case CPP_OR_EQ:
2169f33b 25936 opcode = BIT_IOR_EXPR;
8487df40 25937 break;
25938 case CPP_XOR_EQ:
2169f33b 25939 opcode = BIT_XOR_EXPR;
8487df40 25940 break;
2169f33b 25941 case CPP_EQ:
25942 if (structured_block || code == OMP_ATOMIC)
25943 {
25944 enum cp_parser_prec oprec;
25945 cp_token *token;
25946 cp_lexer_consume_token (parser->lexer);
25947 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25948 /*cast_p=*/false, NULL);
25949 if (rhs1 == error_mark_node)
25950 goto saw_error;
25951 token = cp_lexer_peek_token (parser->lexer);
25952 switch (token->type)
25953 {
25954 case CPP_SEMICOLON:
25955 if (code == OMP_ATOMIC_CAPTURE_NEW)
25956 {
25957 code = OMP_ATOMIC_CAPTURE_OLD;
25958 v = lhs;
25959 lhs = NULL_TREE;
25960 lhs1 = rhs1;
25961 rhs1 = NULL_TREE;
25962 cp_lexer_consume_token (parser->lexer);
25963 goto restart;
25964 }
25965 cp_parser_error (parser,
25966 "invalid form of %<#pragma omp atomic%>");
25967 goto saw_error;
25968 case CPP_MULT:
25969 opcode = MULT_EXPR;
25970 break;
25971 case CPP_DIV:
25972 opcode = TRUNC_DIV_EXPR;
25973 break;
25974 case CPP_PLUS:
25975 opcode = PLUS_EXPR;
25976 break;
25977 case CPP_MINUS:
25978 opcode = MINUS_EXPR;
25979 break;
25980 case CPP_LSHIFT:
25981 opcode = LSHIFT_EXPR;
25982 break;
25983 case CPP_RSHIFT:
25984 opcode = RSHIFT_EXPR;
25985 break;
25986 case CPP_AND:
25987 opcode = BIT_AND_EXPR;
25988 break;
25989 case CPP_OR:
25990 opcode = BIT_IOR_EXPR;
25991 break;
25992 case CPP_XOR:
25993 opcode = BIT_XOR_EXPR;
25994 break;
25995 default:
25996 cp_parser_error (parser,
25997 "invalid operator for %<#pragma omp atomic%>");
25998 goto saw_error;
25999 }
26000 oprec = TOKEN_PRECEDENCE (token);
26001 gcc_assert (oprec != PREC_NOT_OPERATOR);
26002 if (commutative_tree_code (opcode))
26003 oprec = (enum cp_parser_prec) (oprec - 1);
26004 cp_lexer_consume_token (parser->lexer);
26005 rhs = cp_parser_binary_expression (parser, false, false,
26006 oprec, NULL);
26007 if (rhs == error_mark_node)
26008 goto saw_error;
26009 goto stmt_done;
26010 }
26011 /* FALLTHROUGH */
8487df40 26012 default:
26013 cp_parser_error (parser,
26014 "invalid operator for %<#pragma omp atomic%>");
26015 goto saw_error;
26016 }
26017 cp_lexer_consume_token (parser->lexer);
26018
98b326fd 26019 rhs = cp_parser_expression (parser, false, NULL);
8487df40 26020 if (rhs == error_mark_node)
26021 goto saw_error;
26022 break;
26023 }
2169f33b 26024stmt_done:
26025 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
26026 {
26027 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
26028 goto saw_error;
26029 v = cp_parser_unary_expression (parser, /*address_p=*/false,
26030 /*cast_p=*/false, NULL);
26031 if (v == error_mark_node)
26032 goto saw_error;
26033 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
26034 goto saw_error;
26035 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
26036 /*cast_p=*/false, NULL);
26037 if (lhs1 == error_mark_node)
26038 goto saw_error;
26039 }
26040 if (structured_block)
26041 {
26042 cp_parser_consume_semicolon_at_end_of_statement (parser);
26043 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26044 }
26045done:
26046 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
26047 if (!structured_block)
26048 cp_parser_consume_semicolon_at_end_of_statement (parser);
8487df40 26049 return;
26050
26051 saw_error:
26052 cp_parser_skip_to_end_of_block_or_statement (parser);
2169f33b 26053 if (structured_block)
26054 {
26055 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26056 cp_lexer_consume_token (parser->lexer);
26057 else if (code == OMP_ATOMIC_CAPTURE_NEW)
26058 {
26059 cp_parser_skip_to_end_of_block_or_statement (parser);
26060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26061 cp_lexer_consume_token (parser->lexer);
26062 }
26063 }
8487df40 26064}
26065
26066
26067/* OpenMP 2.5:
074ab442 26068 # pragma omp barrier new-line */
8487df40 26069
26070static void
26071cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
26072{
26073 cp_parser_require_pragma_eol (parser, pragma_tok);
26074 finish_omp_barrier ();
26075}
26076
26077/* OpenMP 2.5:
26078 # pragma omp critical [(name)] new-line
074ab442 26079 structured-block */
8487df40 26080
26081static tree
26082cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
26083{
26084 tree stmt, name = NULL;
26085
26086 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26087 {
26088 cp_lexer_consume_token (parser->lexer);
26089
26090 name = cp_parser_identifier (parser);
074ab442 26091
8487df40 26092 if (name == error_mark_node
c247dce0 26093 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8487df40 26094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26095 /*or_comma=*/false,
26096 /*consume_paren=*/true);
26097 if (name == error_mark_node)
26098 name = NULL;
26099 }
26100 cp_parser_require_pragma_eol (parser, pragma_tok);
26101
26102 stmt = cp_parser_omp_structured_block (parser);
e60a6f7b 26103 return c_finish_omp_critical (input_location, stmt, name);
8487df40 26104}
26105
26106/* OpenMP 2.5:
26107 # pragma omp flush flush-vars[opt] new-line
26108
26109 flush-vars:
26110 ( variable-list ) */
26111
26112static void
26113cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26114{
26115 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
a52f99a9 26116 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 26117 cp_parser_require_pragma_eol (parser, pragma_tok);
26118
26119 finish_omp_flush ();
26120}
26121
fd6481cf 26122/* Helper function, to parse omp for increment expression. */
8487df40 26123
26124static tree
fd6481cf 26125cp_parser_omp_for_cond (cp_parser *parser, tree decl)
8487df40 26126{
4390875c 26127 tree cond = cp_parser_binary_expression (parser, false, true,
26128 PREC_NOT_OPERATOR, NULL);
4390875c 26129 if (cond == error_mark_node
26130 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8487df40 26131 {
fd6481cf 26132 cp_parser_skip_to_end_of_statement (parser);
26133 return error_mark_node;
8487df40 26134 }
8487df40 26135
4390875c 26136 switch (TREE_CODE (cond))
fd6481cf 26137 {
fd6481cf 26138 case GT_EXPR:
26139 case GE_EXPR:
4390875c 26140 case LT_EXPR:
26141 case LE_EXPR:
fd6481cf 26142 break;
26143 default:
fd6481cf 26144 return error_mark_node;
26145 }
26146
4390875c 26147 /* If decl is an iterator, preserve LHS and RHS of the relational
26148 expr until finish_omp_for. */
26149 if (decl
26150 && (type_dependent_expression_p (decl)
26151 || CLASS_TYPE_P (TREE_TYPE (decl))))
26152 return cond;
8487df40 26153
ef0b0c72 26154 return build_x_binary_op (input_location, TREE_CODE (cond),
4390875c 26155 TREE_OPERAND (cond, 0), ERROR_MARK,
26156 TREE_OPERAND (cond, 1), ERROR_MARK,
9951fe5d 26157 /*overload=*/NULL, tf_warning_or_error);
fd6481cf 26158}
8487df40 26159
fd6481cf 26160/* Helper function, to parse omp for increment expression. */
26161
26162static tree
26163cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26164{
26165 cp_token *token = cp_lexer_peek_token (parser->lexer);
26166 enum tree_code op;
26167 tree lhs, rhs;
26168 cp_id_kind idk;
26169 bool decl_first;
26170
26171 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26172 {
26173 op = (token->type == CPP_PLUS_PLUS
26174 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26175 cp_lexer_consume_token (parser->lexer);
98b326fd 26176 lhs = cp_parser_cast_expression (parser, false, false, NULL);
fd6481cf 26177 if (lhs != decl)
26178 return error_mark_node;
26179 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26180 }
26181
26182 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26183 if (lhs != decl)
26184 return error_mark_node;
26185
26186 token = cp_lexer_peek_token (parser->lexer);
26187 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26188 {
26189 op = (token->type == CPP_PLUS_PLUS
26190 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26191 cp_lexer_consume_token (parser->lexer);
26192 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26193 }
26194
26195 op = cp_parser_assignment_operator_opt (parser);
26196 if (op == ERROR_MARK)
26197 return error_mark_node;
26198
26199 if (op != NOP_EXPR)
26200 {
98b326fd 26201 rhs = cp_parser_assignment_expression (parser, false, NULL);
fd6481cf 26202 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26203 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26204 }
26205
4390875c 26206 lhs = cp_parser_binary_expression (parser, false, false,
98b326fd 26207 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 26208 token = cp_lexer_peek_token (parser->lexer);
26209 decl_first = lhs == decl;
26210 if (decl_first)
26211 lhs = NULL_TREE;
26212 if (token->type != CPP_PLUS
26213 && token->type != CPP_MINUS)
26214 return error_mark_node;
26215
26216 do
26217 {
26218 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26219 cp_lexer_consume_token (parser->lexer);
4390875c 26220 rhs = cp_parser_binary_expression (parser, false, false,
98b326fd 26221 PREC_ADDITIVE_EXPRESSION, NULL);
fd6481cf 26222 token = cp_lexer_peek_token (parser->lexer);
26223 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
8487df40 26224 {
fd6481cf 26225 if (lhs == NULL_TREE)
26226 {
26227 if (op == PLUS_EXPR)
26228 lhs = rhs;
26229 else
ef0b0c72 26230 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
26231 tf_warning_or_error);
fd6481cf 26232 }
26233 else
ef0b0c72 26234 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
26235 ERROR_MARK, NULL, tf_warning_or_error);
fd6481cf 26236 }
26237 }
26238 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
8487df40 26239
fd6481cf 26240 if (!decl_first)
26241 {
26242 if (rhs != decl || op == MINUS_EXPR)
26243 return error_mark_node;
26244 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26245 }
26246 else
26247 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26248
26249 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26250}
26251
bde357c8 26252/* Parse the restricted form of the for statement allowed by OpenMP. */
fd6481cf 26253
26254static tree
26255cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26256{
26257 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
9bc1bf88 26258 tree real_decl, initv, condv, incrv, declv;
fd6481cf 26259 tree this_pre_body, cl;
26260 location_t loc_first;
26261 bool collapse_err = false;
26262 int i, collapse = 1, nbraces = 0;
9bc1bf88 26263 VEC(tree,gc) *for_block = make_tree_vector ();
fd6481cf 26264
26265 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26266 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26267 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26268
26269 gcc_assert (collapse >= 1);
26270
26271 declv = make_tree_vec (collapse);
26272 initv = make_tree_vec (collapse);
26273 condv = make_tree_vec (collapse);
26274 incrv = make_tree_vec (collapse);
26275
26276 loc_first = cp_lexer_peek_token (parser->lexer)->location;
26277
26278 for (i = 0; i < collapse; i++)
26279 {
26280 int bracecount = 0;
26281 bool add_private_clause = false;
26282 location_t loc;
26283
26284 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26285 {
26286 cp_parser_error (parser, "for statement expected");
26287 return NULL;
26288 }
26289 loc = cp_lexer_consume_token (parser->lexer)->location;
26290
c247dce0 26291 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
fd6481cf 26292 return NULL;
26293
26294 init = decl = real_decl = NULL;
26295 this_pre_body = push_stmt_list ();
26296 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26297 {
507c9a59 26298 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26299
26300 init-expr:
26301 var = lb
26302 integer-type var = lb
26303 random-access-iterator-type var = lb
26304 pointer-type var = lb
26305 */
fd6481cf 26306 cp_decl_specifier_seq type_specifiers;
26307
26308 /* First, try to parse as an initialized declaration. See
26309 cp_parser_condition, from whence the bulk of this is copied. */
26310
26311 cp_parser_parse_tentatively (parser);
c44f7faf 26312 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
638569c5 26313 /*is_trailing_return=*/false,
fd6481cf 26314 &type_specifiers);
507c9a59 26315 if (cp_parser_parse_definitely (parser))
8487df40 26316 {
507c9a59 26317 /* If parsing a type specifier seq succeeded, then this
26318 MUST be a initialized declaration. */
fd6481cf 26319 tree asm_specification, attributes;
26320 cp_declarator *declarator;
26321
26322 declarator = cp_parser_declarator (parser,
26323 CP_PARSER_DECLARATOR_NAMED,
26324 /*ctor_dtor_or_conv_p=*/NULL,
26325 /*parenthesized_p=*/NULL,
26326 /*member_p=*/false);
26327 attributes = cp_parser_attributes_opt (parser);
26328 asm_specification = cp_parser_asm_specification_opt (parser);
26329
507c9a59 26330 if (declarator == cp_error_declarator)
26331 cp_parser_skip_to_end_of_statement (parser);
26332
26333 else
fd6481cf 26334 {
304ac225 26335 tree pushed_scope, auto_node;
fd6481cf 26336
26337 decl = start_decl (declarator, &type_specifiers,
304ac225 26338 SD_INITIALIZED, attributes,
fd6481cf 26339 /*prefix_attributes=*/NULL_TREE,
26340 &pushed_scope);
26341
304ac225 26342 auto_node = type_uses_auto (TREE_TYPE (decl));
507c9a59 26343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26344 {
26345 if (cp_lexer_next_token_is (parser->lexer,
26346 CPP_OPEN_PAREN))
26347 error ("parenthesized initialization is not allowed in "
26348 "OpenMP %<for%> loop");
26349 else
26350 /* Trigger an error. */
c247dce0 26351 cp_parser_require (parser, CPP_EQ, RT_EQ);
507c9a59 26352
26353 init = error_mark_node;
26354 cp_parser_skip_to_end_of_statement (parser);
26355 }
26356 else if (CLASS_TYPE_P (TREE_TYPE (decl))
304ac225 26357 || type_dependent_expression_p (decl)
26358 || auto_node)
fd6481cf 26359 {
f82f1250 26360 bool is_direct_init, is_non_constant_init;
fd6481cf 26361
26362 init = cp_parser_initializer (parser,
f82f1250 26363 &is_direct_init,
fd6481cf 26364 &is_non_constant_init);
26365
cb542a2d 26366 if (auto_node)
304ac225 26367 {
26368 TREE_TYPE (decl)
26369 = do_auto_deduction (TREE_TYPE (decl), init,
26370 auto_node);
26371
26372 if (!CLASS_TYPE_P (TREE_TYPE (decl))
26373 && !type_dependent_expression_p (decl))
26374 goto non_class;
26375 }
26376
fd6481cf 26377 cp_finish_decl (decl, init, !is_non_constant_init,
26378 asm_specification,
26379 LOOKUP_ONLYCONVERTING);
26380 if (CLASS_TYPE_P (TREE_TYPE (decl)))
26381 {
9bc1bf88 26382 VEC_safe_push (tree, gc, for_block, this_pre_body);
fd6481cf 26383 init = NULL_TREE;
26384 }
26385 else
26386 init = pop_stmt_list (this_pre_body);
26387 this_pre_body = NULL_TREE;
26388 }
26389 else
26390 {
507c9a59 26391 /* Consume '='. */
26392 cp_lexer_consume_token (parser->lexer);
98b326fd 26393 init = cp_parser_assignment_expression (parser, false, NULL);
8487df40 26394
304ac225 26395 non_class:
fd6481cf 26396 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26397 init = error_mark_node;
26398 else
26399 cp_finish_decl (decl, NULL_TREE,
26400 /*init_const_expr_p=*/false,
26401 asm_specification,
26402 LOOKUP_ONLYCONVERTING);
26403 }
8487df40 26404
fd6481cf 26405 if (pushed_scope)
26406 pop_scope (pushed_scope);
26407 }
26408 }
507c9a59 26409 else
fd6481cf 26410 {
26411 cp_id_kind idk;
507c9a59 26412 /* If parsing a type specifier sequence failed, then
26413 this MUST be a simple expression. */
fd6481cf 26414 cp_parser_parse_tentatively (parser);
26415 decl = cp_parser_primary_expression (parser, false, false,
26416 false, &idk);
26417 if (!cp_parser_error_occurred (parser)
26418 && decl
26419 && DECL_P (decl)
26420 && CLASS_TYPE_P (TREE_TYPE (decl)))
26421 {
26422 tree rhs;
26423
26424 cp_parser_parse_definitely (parser);
c247dce0 26425 cp_parser_require (parser, CPP_EQ, RT_EQ);
98b326fd 26426 rhs = cp_parser_assignment_expression (parser, false, NULL);
255b5d15 26427 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
26428 decl, NOP_EXPR,
fd6481cf 26429 rhs,
26430 tf_warning_or_error));
26431 add_private_clause = true;
26432 }
a46a7e42 26433 else
fd6481cf 26434 {
26435 decl = NULL;
26436 cp_parser_abort_tentative_parse (parser);
98b326fd 26437 init = cp_parser_expression (parser, false, NULL);
fd6481cf 26438 if (init)
26439 {
26440 if (TREE_CODE (init) == MODIFY_EXPR
26441 || TREE_CODE (init) == MODOP_EXPR)
26442 real_decl = TREE_OPERAND (init, 0);
26443 }
26444 }
26445 }
26446 }
c247dce0 26447 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
fd6481cf 26448 if (this_pre_body)
26449 {
26450 this_pre_body = pop_stmt_list (this_pre_body);
26451 if (pre_body)
26452 {
26453 tree t = pre_body;
26454 pre_body = push_stmt_list ();
26455 add_stmt (t);
26456 add_stmt (this_pre_body);
26457 pre_body = pop_stmt_list (pre_body);
26458 }
26459 else
26460 pre_body = this_pre_body;
26461 }
8487df40 26462
fd6481cf 26463 if (decl)
26464 real_decl = decl;
26465 if (par_clauses != NULL && real_decl != NULL_TREE)
26466 {
26467 tree *c;
26468 for (c = par_clauses; *c ; )
26469 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26470 && OMP_CLAUSE_DECL (*c) == real_decl)
26471 {
ccb59bb6 26472 error_at (loc, "iteration variable %qD"
26473 " should not be firstprivate", real_decl);
fd6481cf 26474 *c = OMP_CLAUSE_CHAIN (*c);
26475 }
26476 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26477 && OMP_CLAUSE_DECL (*c) == real_decl)
26478 {
26479 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26480 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
e60a6f7b 26481 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
fd6481cf 26482 OMP_CLAUSE_DECL (l) = real_decl;
26483 OMP_CLAUSE_CHAIN (l) = clauses;
26484 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26485 clauses = l;
26486 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26487 CP_OMP_CLAUSE_INFO (*c) = NULL;
26488 add_private_clause = false;
26489 }
26490 else
26491 {
26492 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26493 && OMP_CLAUSE_DECL (*c) == real_decl)
26494 add_private_clause = false;
26495 c = &OMP_CLAUSE_CHAIN (*c);
26496 }
26497 }
26498
26499 if (add_private_clause)
26500 {
26501 tree c;
26502 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26503 {
26504 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26505 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26506 && OMP_CLAUSE_DECL (c) == decl)
26507 break;
26508 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26509 && OMP_CLAUSE_DECL (c) == decl)
ccb59bb6 26510 error_at (loc, "iteration variable %qD "
26511 "should not be firstprivate",
26512 decl);
fd6481cf 26513 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26514 && OMP_CLAUSE_DECL (c) == decl)
ccb59bb6 26515 error_at (loc, "iteration variable %qD should not be reduction",
26516 decl);
fd6481cf 26517 }
26518 if (c == NULL)
26519 {
e60a6f7b 26520 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
fd6481cf 26521 OMP_CLAUSE_DECL (c) = decl;
26522 c = finish_omp_clauses (c);
26523 if (c)
26524 {
26525 OMP_CLAUSE_CHAIN (c) = clauses;
26526 clauses = c;
26527 }
8487df40 26528 }
26529 }
26530
fd6481cf 26531 cond = NULL;
26532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
e060ba36 26533 cond = cp_parser_omp_for_cond (parser, decl);
c247dce0 26534 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8487df40 26535
fd6481cf 26536 incr = NULL;
26537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26538 {
26539 /* If decl is an iterator, preserve the operator on decl
26540 until finish_omp_for. */
ef12a5ef 26541 if (real_decl
995fad40 26542 && ((processing_template_decl
ef12a5ef 26543 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26544 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26545 incr = cp_parser_omp_for_incr (parser, real_decl);
fd6481cf 26546 else
98b326fd 26547 incr = cp_parser_expression (parser, false, NULL);
fd6481cf 26548 }
8487df40 26549
c247dce0 26550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
fd6481cf 26551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26552 /*or_comma=*/false,
26553 /*consume_paren=*/true);
8487df40 26554
fd6481cf 26555 TREE_VEC_ELT (declv, i) = decl;
26556 TREE_VEC_ELT (initv, i) = init;
26557 TREE_VEC_ELT (condv, i) = cond;
26558 TREE_VEC_ELT (incrv, i) = incr;
26559
26560 if (i == collapse - 1)
26561 break;
26562
26563 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26564 in between the collapsed for loops to be still considered perfectly
26565 nested. Hopefully the final version clarifies this.
26566 For now handle (multiple) {'s and empty statements. */
26567 cp_parser_parse_tentatively (parser);
26568 do
26569 {
26570 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26571 break;
26572 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26573 {
26574 cp_lexer_consume_token (parser->lexer);
26575 bracecount++;
26576 }
26577 else if (bracecount
26578 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26579 cp_lexer_consume_token (parser->lexer);
26580 else
26581 {
26582 loc = cp_lexer_peek_token (parser->lexer)->location;
ccb59bb6 26583 error_at (loc, "not enough collapsed for loops");
fd6481cf 26584 collapse_err = true;
26585 cp_parser_abort_tentative_parse (parser);
26586 declv = NULL_TREE;
26587 break;
26588 }
26589 }
26590 while (1);
26591
26592 if (declv)
26593 {
26594 cp_parser_parse_definitely (parser);
26595 nbraces += bracecount;
26596 }
26597 }
8487df40 26598
26599 /* Note that we saved the original contents of this flag when we entered
26600 the structured block, and so we don't need to re-save it here. */
26601 parser->in_statement = IN_OMP_FOR;
26602
26603 /* Note that the grammar doesn't call for a structured block here,
26604 though the loop as a whole is a structured block. */
26605 body = push_stmt_list ();
e534436e 26606 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 26607 body = pop_stmt_list (body);
26608
fd6481cf 26609 if (declv == NULL_TREE)
26610 ret = NULL_TREE;
26611 else
26612 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26613 pre_body, clauses);
26614
26615 while (nbraces)
26616 {
26617 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26618 {
26619 cp_lexer_consume_token (parser->lexer);
26620 nbraces--;
26621 }
26622 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26623 cp_lexer_consume_token (parser->lexer);
26624 else
26625 {
26626 if (!collapse_err)
eef0ab03 26627 {
ccb59bb6 26628 error_at (cp_lexer_peek_token (parser->lexer)->location,
26629 "collapsed loops not perfectly nested");
eef0ab03 26630 }
fd6481cf 26631 collapse_err = true;
26632 cp_parser_statement_seq_opt (parser, NULL);
1c1f5172 26633 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26634 break;
fd6481cf 26635 }
26636 }
26637
9bc1bf88 26638 while (!VEC_empty (tree, for_block))
26639 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26640 release_tree_vector (for_block);
fd6481cf 26641
26642 return ret;
8487df40 26643}
26644
26645/* OpenMP 2.5:
26646 #pragma omp for for-clause[optseq] new-line
074ab442 26647 for-loop */
8487df40 26648
26649#define OMP_FOR_CLAUSE_MASK \
26650 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26651 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26652 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26653 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26654 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26655 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
fd6481cf 26656 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26657 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
8487df40 26658
26659static tree
26660cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26661{
26662 tree clauses, sb, ret;
26663 unsigned int save;
26664
26665 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26666 "#pragma omp for", pragma_tok);
26667
26668 sb = begin_omp_structured_block ();
26669 save = cp_parser_begin_omp_structured_block (parser);
26670
fd6481cf 26671 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
8487df40 26672
26673 cp_parser_end_omp_structured_block (parser, save);
26674 add_stmt (finish_omp_structured_block (sb));
26675
26676 return ret;
26677}
26678
26679/* OpenMP 2.5:
26680 # pragma omp master new-line
074ab442 26681 structured-block */
8487df40 26682
26683static tree
26684cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26685{
26686 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 26687 return c_finish_omp_master (input_location,
26688 cp_parser_omp_structured_block (parser));
8487df40 26689}
26690
26691/* OpenMP 2.5:
26692 # pragma omp ordered new-line
074ab442 26693 structured-block */
8487df40 26694
26695static tree
26696cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26697{
e60a6f7b 26698 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 26699 cp_parser_require_pragma_eol (parser, pragma_tok);
e60a6f7b 26700 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
8487df40 26701}
26702
26703/* OpenMP 2.5:
26704
26705 section-scope:
26706 { section-sequence }
26707
26708 section-sequence:
26709 section-directive[opt] structured-block
26710 section-sequence section-directive structured-block */
26711
26712static tree
26713cp_parser_omp_sections_scope (cp_parser *parser)
26714{
26715 tree stmt, substmt;
26716 bool error_suppress = false;
26717 cp_token *tok;
26718
c247dce0 26719 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8487df40 26720 return NULL_TREE;
26721
26722 stmt = push_stmt_list ();
26723
26724 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26725 {
26726 unsigned save;
26727
26728 substmt = begin_omp_structured_block ();
26729 save = cp_parser_begin_omp_structured_block (parser);
26730
26731 while (1)
26732 {
e534436e 26733 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 26734
26735 tok = cp_lexer_peek_token (parser->lexer);
26736 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26737 break;
26738 if (tok->type == CPP_CLOSE_BRACE)
26739 break;
26740 if (tok->type == CPP_EOF)
26741 break;
26742 }
26743
26744 cp_parser_end_omp_structured_block (parser, save);
26745 substmt = finish_omp_structured_block (substmt);
26746 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26747 add_stmt (substmt);
26748 }
26749
26750 while (1)
26751 {
26752 tok = cp_lexer_peek_token (parser->lexer);
26753 if (tok->type == CPP_CLOSE_BRACE)
26754 break;
26755 if (tok->type == CPP_EOF)
26756 break;
26757
26758 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26759 {
26760 cp_lexer_consume_token (parser->lexer);
26761 cp_parser_require_pragma_eol (parser, tok);
26762 error_suppress = false;
26763 }
26764 else if (!error_suppress)
26765 {
26766 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26767 error_suppress = true;
26768 }
26769
26770 substmt = cp_parser_omp_structured_block (parser);
26771 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26772 add_stmt (substmt);
26773 }
c247dce0 26774 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8487df40 26775
26776 substmt = pop_stmt_list (stmt);
26777
26778 stmt = make_node (OMP_SECTIONS);
26779 TREE_TYPE (stmt) = void_type_node;
26780 OMP_SECTIONS_BODY (stmt) = substmt;
26781
26782 add_stmt (stmt);
26783 return stmt;
26784}
26785
26786/* OpenMP 2.5:
26787 # pragma omp sections sections-clause[optseq] newline
074ab442 26788 sections-scope */
8487df40 26789
26790#define OMP_SECTIONS_CLAUSE_MASK \
26791 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26792 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26793 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26794 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26795 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26796
26797static tree
26798cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26799{
26800 tree clauses, ret;
26801
26802 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26803 "#pragma omp sections", pragma_tok);
26804
26805 ret = cp_parser_omp_sections_scope (parser);
26806 if (ret)
26807 OMP_SECTIONS_CLAUSES (ret) = clauses;
26808
26809 return ret;
26810}
26811
26812/* OpenMP 2.5:
26813 # pragma parallel parallel-clause new-line
26814 # pragma parallel for parallel-for-clause new-line
074ab442 26815 # pragma parallel sections parallel-sections-clause new-line */
8487df40 26816
26817#define OMP_PARALLEL_CLAUSE_MASK \
26818 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26819 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26820 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26821 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26822 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26823 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26824 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26825 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26826
26827static tree
26828cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26829{
26830 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26831 const char *p_name = "#pragma omp parallel";
26832 tree stmt, clauses, par_clause, ws_clause, block;
26833 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26834 unsigned int save;
e60a6f7b 26835 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8487df40 26836
26837 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26838 {
26839 cp_lexer_consume_token (parser->lexer);
26840 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26841 p_name = "#pragma omp parallel for";
26842 mask |= OMP_FOR_CLAUSE_MASK;
26843 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26844 }
26845 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26846 {
3369eb76 26847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
8487df40 26848 const char *p = IDENTIFIER_POINTER (id);
26849 if (strcmp (p, "sections") == 0)
26850 {
26851 cp_lexer_consume_token (parser->lexer);
26852 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26853 p_name = "#pragma omp parallel sections";
26854 mask |= OMP_SECTIONS_CLAUSE_MASK;
26855 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26856 }
26857 }
26858
26859 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26860 block = begin_omp_parallel ();
26861 save = cp_parser_begin_omp_structured_block (parser);
26862
26863 switch (p_kind)
26864 {
26865 case PRAGMA_OMP_PARALLEL:
355efab1 26866 cp_parser_statement (parser, NULL_TREE, false, NULL);
8487df40 26867 par_clause = clauses;
26868 break;
26869
26870 case PRAGMA_OMP_PARALLEL_FOR:
e60a6f7b 26871 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
fd6481cf 26872 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
8487df40 26873 break;
26874
26875 case PRAGMA_OMP_PARALLEL_SECTIONS:
e60a6f7b 26876 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
8487df40 26877 stmt = cp_parser_omp_sections_scope (parser);
26878 if (stmt)
26879 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26880 break;
26881
26882 default:
26883 gcc_unreachable ();
26884 }
26885
26886 cp_parser_end_omp_structured_block (parser, save);
87f7c31e 26887 stmt = finish_omp_parallel (par_clause, block);
26888 if (p_kind != PRAGMA_OMP_PARALLEL)
26889 OMP_PARALLEL_COMBINED (stmt) = 1;
26890 return stmt;
8487df40 26891}
26892
26893/* OpenMP 2.5:
26894 # pragma omp single single-clause[optseq] new-line
074ab442 26895 structured-block */
8487df40 26896
26897#define OMP_SINGLE_CLAUSE_MASK \
26898 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26899 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26900 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26901 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26902
26903static tree
26904cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26905{
26906 tree stmt = make_node (OMP_SINGLE);
26907 TREE_TYPE (stmt) = void_type_node;
26908
26909 OMP_SINGLE_CLAUSES (stmt)
26910 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26911 "#pragma omp single", pragma_tok);
26912 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26913
26914 return add_stmt (stmt);
26915}
26916
fd6481cf 26917/* OpenMP 3.0:
26918 # pragma omp task task-clause[optseq] new-line
26919 structured-block */
26920
26921#define OMP_TASK_CLAUSE_MASK \
26922 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26923 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26924 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26925 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26926 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
2169f33b 26927 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26928 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26929 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
fd6481cf 26930
26931static tree
26932cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26933{
26934 tree clauses, block;
26935 unsigned int save;
26936
26937 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26938 "#pragma omp task", pragma_tok);
26939 block = begin_omp_task ();
26940 save = cp_parser_begin_omp_structured_block (parser);
26941 cp_parser_statement (parser, NULL_TREE, false, NULL);
26942 cp_parser_end_omp_structured_block (parser, save);
26943 return finish_omp_task (clauses, block);
26944}
26945
26946/* OpenMP 3.0:
26947 # pragma omp taskwait new-line */
26948
26949static void
26950cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26951{
26952 cp_parser_require_pragma_eol (parser, pragma_tok);
26953 finish_omp_taskwait ();
26954}
26955
2169f33b 26956/* OpenMP 3.1:
26957 # pragma omp taskyield new-line */
26958
26959static void
26960cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26961{
26962 cp_parser_require_pragma_eol (parser, pragma_tok);
26963 finish_omp_taskyield ();
26964}
26965
8487df40 26966/* OpenMP 2.5:
26967 # pragma omp threadprivate (variable-list) */
26968
26969static void
26970cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26971{
26972 tree vars;
26973
a52f99a9 26974 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
8487df40 26975 cp_parser_require_pragma_eol (parser, pragma_tok);
26976
8487df40 26977 finish_omp_threadprivate (vars);
26978}
26979
26980/* Main entry point to OpenMP statement pragmas. */
26981
26982static void
26983cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26984{
26985 tree stmt;
26986
26987 switch (pragma_tok->pragma_kind)
26988 {
26989 case PRAGMA_OMP_ATOMIC:
26990 cp_parser_omp_atomic (parser, pragma_tok);
26991 return;
26992 case PRAGMA_OMP_CRITICAL:
26993 stmt = cp_parser_omp_critical (parser, pragma_tok);
26994 break;
26995 case PRAGMA_OMP_FOR:
26996 stmt = cp_parser_omp_for (parser, pragma_tok);
26997 break;
26998 case PRAGMA_OMP_MASTER:
26999 stmt = cp_parser_omp_master (parser, pragma_tok);
27000 break;
27001 case PRAGMA_OMP_ORDERED:
27002 stmt = cp_parser_omp_ordered (parser, pragma_tok);
27003 break;
27004 case PRAGMA_OMP_PARALLEL:
27005 stmt = cp_parser_omp_parallel (parser, pragma_tok);
27006 break;
27007 case PRAGMA_OMP_SECTIONS:
27008 stmt = cp_parser_omp_sections (parser, pragma_tok);
27009 break;
27010 case PRAGMA_OMP_SINGLE:
27011 stmt = cp_parser_omp_single (parser, pragma_tok);
27012 break;
fd6481cf 27013 case PRAGMA_OMP_TASK:
27014 stmt = cp_parser_omp_task (parser, pragma_tok);
27015 break;
8487df40 27016 default:
27017 gcc_unreachable ();
27018 }
27019
27020 if (stmt)
27021 SET_EXPR_LOCATION (stmt, pragma_tok->location);
27022}
27023\f
4c0315d0 27024/* Transactional Memory parsing routines. */
27025
27026/* Parse a transaction attribute.
27027
27028 txn-attribute:
27029 attribute
27030 [ [ identifier ] ]
27031
27032 ??? Simplify this when C++0x bracket attributes are
27033 implemented properly. */
27034
27035static tree
27036cp_parser_txn_attribute_opt (cp_parser *parser)
27037{
27038 cp_token *token;
27039 tree attr_name, attr = NULL;
27040
27041 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
27042 return cp_parser_attributes_opt (parser);
27043
27044 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
27045 return NULL_TREE;
27046 cp_lexer_consume_token (parser->lexer);
27047 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
27048 goto error1;
27049
27050 token = cp_lexer_peek_token (parser->lexer);
27051 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
27052 {
27053 token = cp_lexer_consume_token (parser->lexer);
27054
27055 attr_name = (token->type == CPP_KEYWORD
27056 /* For keywords, use the canonical spelling,
27057 not the parsed identifier. */
27058 ? ridpointers[(int) token->keyword]
27059 : token->u.value);
27060 attr = build_tree_list (attr_name, NULL_TREE);
27061 }
27062 else
27063 cp_parser_error (parser, "expected identifier");
27064
27065 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27066 error1:
27067 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27068 return attr;
27069}
27070
27071/* Parse a __transaction_atomic or __transaction_relaxed statement.
27072
27073 transaction-statement:
f770bf53 27074 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
4c0315d0 27075 compound-statement
f770bf53 27076 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
4c0315d0 27077*/
27078
27079static tree
27080cp_parser_transaction (cp_parser *parser, enum rid keyword)
27081{
27082 unsigned char old_in = parser->in_transaction;
27083 unsigned char this_in = 1, new_in;
27084 cp_token *token;
f770bf53 27085 tree stmt, attrs, noex;
4c0315d0 27086
27087 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27088 || keyword == RID_TRANSACTION_RELAXED);
27089 token = cp_parser_require_keyword (parser, keyword,
27090 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27091 : RT_TRANSACTION_RELAXED));
27092 gcc_assert (token != NULL);
27093
27094 if (keyword == RID_TRANSACTION_RELAXED)
27095 this_in |= TM_STMT_ATTR_RELAXED;
27096 else
27097 {
27098 attrs = cp_parser_txn_attribute_opt (parser);
27099 if (attrs)
27100 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27101 }
27102
f770bf53 27103 /* Parse a noexcept specification. */
27104 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27105
4c0315d0 27106 /* Keep track if we're in the lexical scope of an outer transaction. */
27107 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27108
27109 stmt = begin_transaction_stmt (token->location, NULL, this_in);
27110
27111 parser->in_transaction = new_in;
27112 cp_parser_compound_statement (parser, NULL, false, false);
27113 parser->in_transaction = old_in;
27114
f770bf53 27115 finish_transaction_stmt (stmt, NULL, this_in, noex);
4c0315d0 27116
27117 return stmt;
27118}
27119
27120/* Parse a __transaction_atomic or __transaction_relaxed expression.
27121
27122 transaction-expression:
f770bf53 27123 __transaction_atomic txn-noexcept-spec[opt] ( expression )
27124 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
4c0315d0 27125*/
27126
27127static tree
27128cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27129{
27130 unsigned char old_in = parser->in_transaction;
27131 unsigned char this_in = 1;
27132 cp_token *token;
f770bf53 27133 tree expr, noex;
27134 bool noex_expr;
4c0315d0 27135
27136 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27137 || keyword == RID_TRANSACTION_RELAXED);
27138
27139 if (!flag_tm)
27140 error (keyword == RID_TRANSACTION_RELAXED
27141 ? G_("%<__transaction_relaxed%> without transactional memory "
27142 "support enabled")
27143 : G_("%<__transaction_atomic%> without transactional memory "
27144 "support enabled"));
27145
27146 token = cp_parser_require_keyword (parser, keyword,
27147 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27148 : RT_TRANSACTION_RELAXED));
27149 gcc_assert (token != NULL);
27150
27151 if (keyword == RID_TRANSACTION_RELAXED)
27152 this_in |= TM_STMT_ATTR_RELAXED;
27153
f770bf53 27154 /* Set this early. This might mean that we allow transaction_cancel in
27155 an expression that we find out later actually has to be a constexpr.
27156 However, we expect that cxx_constant_value will be able to deal with
27157 this; also, if the noexcept has no constexpr, then what we parse next
27158 really is a transaction's body. */
4c0315d0 27159 parser->in_transaction = this_in;
bdd07b44 27160
f770bf53 27161 /* Parse a noexcept specification. */
27162 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27163 true);
bdd07b44 27164
f770bf53 27165 if (!noex || !noex_expr
27166 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27167 {
27168 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27169
27170 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27171 finish_parenthesized_expr (expr);
27172
27173 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27174 }
27175 else
27176 {
27177 /* The only expression that is available got parsed for the noexcept
27178 already. noexcept is true then. */
27179 expr = noex;
27180 noex = boolean_true_node;
27181 }
27182
27183 expr = build_transaction_expr (token->location, expr, this_in, noex);
4c0315d0 27184 parser->in_transaction = old_in;
27185
27186 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27187 return error_mark_node;
27188
bdd07b44 27189 return (flag_tm ? expr : error_mark_node);
4c0315d0 27190}
27191
27192/* Parse a function-transaction-block.
27193
27194 function-transaction-block:
27195 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27196 function-body
27197 __transaction_atomic txn-attribute[opt] function-try-block
27198 __transaction_relaxed ctor-initializer[opt] function-body
27199 __transaction_relaxed function-try-block
27200*/
27201
27202static bool
27203cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27204{
27205 unsigned char old_in = parser->in_transaction;
27206 unsigned char new_in = 1;
27207 tree compound_stmt, stmt, attrs;
27208 bool ctor_initializer_p;
27209 cp_token *token;
27210
27211 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27212 || keyword == RID_TRANSACTION_RELAXED);
27213 token = cp_parser_require_keyword (parser, keyword,
27214 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27215 : RT_TRANSACTION_RELAXED));
27216 gcc_assert (token != NULL);
27217
27218 if (keyword == RID_TRANSACTION_RELAXED)
27219 new_in |= TM_STMT_ATTR_RELAXED;
27220 else
27221 {
27222 attrs = cp_parser_txn_attribute_opt (parser);
27223 if (attrs)
27224 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27225 }
27226
27227 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27228
27229 parser->in_transaction = new_in;
27230
27231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27232 ctor_initializer_p = cp_parser_function_try_block (parser);
27233 else
376a817b 27234 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
27235 (parser, /*in_function_try_block=*/false);
4c0315d0 27236
27237 parser->in_transaction = old_in;
27238
f770bf53 27239 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
4c0315d0 27240
27241 return ctor_initializer_p;
27242}
27243
27244/* Parse a __transaction_cancel statement.
27245
27246 cancel-statement:
27247 __transaction_cancel txn-attribute[opt] ;
27248 __transaction_cancel txn-attribute[opt] throw-expression ;
27249
27250 ??? Cancel and throw is not yet implemented. */
27251
27252static tree
27253cp_parser_transaction_cancel (cp_parser *parser)
27254{
27255 cp_token *token;
27256 bool is_outer = false;
27257 tree stmt, attrs;
27258
27259 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27260 RT_TRANSACTION_CANCEL);
27261 gcc_assert (token != NULL);
27262
27263 attrs = cp_parser_txn_attribute_opt (parser);
27264 if (attrs)
27265 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27266
27267 /* ??? Parse cancel-and-throw here. */
27268
27269 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27270
27271 if (!flag_tm)
27272 {
27273 error_at (token->location, "%<__transaction_cancel%> without "
27274 "transactional memory support enabled");
27275 return error_mark_node;
27276 }
27277 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27278 {
27279 error_at (token->location, "%<__transaction_cancel%> within a "
27280 "%<__transaction_relaxed%>");
27281 return error_mark_node;
27282 }
27283 else if (is_outer)
27284 {
27285 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27286 && !is_tm_may_cancel_outer (current_function_decl))
27287 {
27288 error_at (token->location, "outer %<__transaction_cancel%> not "
27289 "within outer %<__transaction_atomic%>");
27290 error_at (token->location,
27291 " or a %<transaction_may_cancel_outer%> function");
27292 return error_mark_node;
27293 }
27294 }
27295 else if (parser->in_transaction == 0)
27296 {
27297 error_at (token->location, "%<__transaction_cancel%> not within "
27298 "%<__transaction_atomic%>");
27299 return error_mark_node;
27300 }
27301
27302 stmt = build_tm_abort_call (token->location, is_outer);
27303 add_stmt (stmt);
27304 finish_stmt ();
27305
27306 return stmt;
27307}
27308\f
8487df40 27309/* The parser. */
27310
27311static GTY (()) cp_parser *the_parser;
27312
27313\f
27314/* Special handling for the first token or line in the file. The first
27315 thing in the file might be #pragma GCC pch_preprocess, which loads a
27316 PCH file, which is a GC collection point. So we need to handle this
27317 first pragma without benefit of an existing lexer structure.
27318
074ab442 27319 Always returns one token to the caller in *FIRST_TOKEN. This is
8487df40 27320 either the true first token of the file, or the first token after
27321 the initial pragma. */
27322
27323static void
27324cp_parser_initial_pragma (cp_token *first_token)
27325{
27326 tree name = NULL;
27327
27328 cp_lexer_get_preprocessor_token (NULL, first_token);
27329 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27330 return;
27331
27332 cp_lexer_get_preprocessor_token (NULL, first_token);
27333 if (first_token->type == CPP_STRING)
27334 {
3369eb76 27335 name = first_token->u.value;
8487df40 27336
27337 cp_lexer_get_preprocessor_token (NULL, first_token);
27338 if (first_token->type != CPP_PRAGMA_EOL)
ccb59bb6 27339 error_at (first_token->location,
27340 "junk at end of %<#pragma GCC pch_preprocess%>");
8487df40 27341 }
27342 else
ccb59bb6 27343 error_at (first_token->location, "expected string literal");
8487df40 27344
27345 /* Skip to the end of the pragma. */
27346 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27347 cp_lexer_get_preprocessor_token (NULL, first_token);
27348
8487df40 27349 /* Now actually load the PCH file. */
27350 if (name)
27351 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
b5262297 27352
27353 /* Read one more token to return to our caller. We have to do this
27354 after reading the PCH file in, since its pointers have to be
27355 live. */
27356 cp_lexer_get_preprocessor_token (NULL, first_token);
8487df40 27357}
27358
27359/* Normal parsing of a pragma token. Here we can (and must) use the
27360 regular lexer. */
27361
27362static bool
27363cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27364{
27365 cp_token *pragma_tok;
27366 unsigned int id;
27367
27368 pragma_tok = cp_lexer_consume_token (parser->lexer);
27369 gcc_assert (pragma_tok->type == CPP_PRAGMA);
27370 parser->lexer->in_pragma = true;
27371
27372 id = pragma_tok->pragma_kind;
27373 switch (id)
27374 {
27375 case PRAGMA_GCC_PCH_PREPROCESS:
ccb59bb6 27376 error_at (pragma_tok->location,
27377 "%<#pragma GCC pch_preprocess%> must be first");
8487df40 27378 break;
27379
27380 case PRAGMA_OMP_BARRIER:
27381 switch (context)
27382 {
27383 case pragma_compound:
27384 cp_parser_omp_barrier (parser, pragma_tok);
27385 return false;
27386 case pragma_stmt:
ccb59bb6 27387 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27388 "used in compound statements");
8487df40 27389 break;
27390 default:
27391 goto bad_stmt;
27392 }
27393 break;
27394
27395 case PRAGMA_OMP_FLUSH:
27396 switch (context)
27397 {
27398 case pragma_compound:
27399 cp_parser_omp_flush (parser, pragma_tok);
27400 return false;
27401 case pragma_stmt:
ccb59bb6 27402 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27403 "used in compound statements");
8487df40 27404 break;
27405 default:
27406 goto bad_stmt;
27407 }
27408 break;
27409
fd6481cf 27410 case PRAGMA_OMP_TASKWAIT:
27411 switch (context)
27412 {
27413 case pragma_compound:
27414 cp_parser_omp_taskwait (parser, pragma_tok);
27415 return false;
27416 case pragma_stmt:
ccb59bb6 27417 error_at (pragma_tok->location,
27418 "%<#pragma omp taskwait%> may only be "
27419 "used in compound statements");
2169f33b 27420 break;
27421 default:
27422 goto bad_stmt;
27423 }
27424 break;
27425
27426 case PRAGMA_OMP_TASKYIELD:
27427 switch (context)
27428 {
27429 case pragma_compound:
27430 cp_parser_omp_taskyield (parser, pragma_tok);
27431 return false;
27432 case pragma_stmt:
27433 error_at (pragma_tok->location,
27434 "%<#pragma omp taskyield%> may only be "
27435 "used in compound statements");
fd6481cf 27436 break;
27437 default:
27438 goto bad_stmt;
27439 }
27440 break;
27441
8487df40 27442 case PRAGMA_OMP_THREADPRIVATE:
27443 cp_parser_omp_threadprivate (parser, pragma_tok);
27444 return false;
27445
27446 case PRAGMA_OMP_ATOMIC:
27447 case PRAGMA_OMP_CRITICAL:
27448 case PRAGMA_OMP_FOR:
27449 case PRAGMA_OMP_MASTER:
27450 case PRAGMA_OMP_ORDERED:
27451 case PRAGMA_OMP_PARALLEL:
27452 case PRAGMA_OMP_SECTIONS:
27453 case PRAGMA_OMP_SINGLE:
fd6481cf 27454 case PRAGMA_OMP_TASK:
8487df40 27455 if (context == pragma_external)
27456 goto bad_stmt;
27457 cp_parser_omp_construct (parser, pragma_tok);
27458 return true;
27459
27460 case PRAGMA_OMP_SECTION:
ccb59bb6 27461 error_at (pragma_tok->location,
27462 "%<#pragma omp section%> may only be used in "
27463 "%<#pragma omp sections%> construct");
8487df40 27464 break;
27465
27466 default:
27467 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27468 c_invoke_pragma_handler (id);
27469 break;
27470
27471 bad_stmt:
27472 cp_parser_error (parser, "expected declaration specifiers");
b75b98aa 27473 break;
27474 }
27475
27476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27477 return false;
27478}
27479
27480/* The interface the pragma parsers have to the lexer. */
27481
27482enum cpp_ttype
27483pragma_lex (tree *value)
27484{
27485 cp_token *tok;
27486 enum cpp_ttype ret;
27487
27488 tok = cp_lexer_peek_token (the_parser->lexer);
27489
27490 ret = tok->type;
3369eb76 27491 *value = tok->u.value;
b75b98aa 27492
27493 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27494 ret = CPP_EOF;
27495 else if (ret == CPP_STRING)
27496 *value = cp_parser_string_literal (the_parser, false, false);
27497 else
27498 {
27499 cp_lexer_consume_token (the_parser->lexer);
27500 if (ret == CPP_KEYWORD)
27501 ret = CPP_NAME;
27502 }
27503
27504 return ret;
27505}
27506
27507\f
0a3b29ad 27508/* External interface. */
27509
40109983 27510/* Parse one entire translation unit. */
0a3b29ad 27511
40109983 27512void
27513c_parse_file (void)
0a3b29ad 27514{
393b349a 27515 static bool already_called = false;
27516
27517 if (already_called)
27518 {
27519 sorry ("inter-module optimizations not implemented for C++");
27520 return;
27521 }
27522 already_called = true;
0a3b29ad 27523
27524 the_parser = cp_parser_new ();
4cab8273 27525 push_deferring_access_checks (flag_access_control
27526 ? dk_no_deferred : dk_no_check);
8e9e8d76 27527 cp_parser_translation_unit (the_parser);
0a3b29ad 27528 the_parser = NULL;
0a3b29ad 27529}
27530
0a3b29ad 27531#include "gt-cp-parser.h"