PR c++/60862
[official-gcc.git] / gcc / cp / parser.c
bloba3c947a9a6908d5a987e5847a9124467eda82398
1 /* C++ Parser.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "timevar.h"
26 #include "cpplib.h"
27 #include "tree.h"
28 #include "print-tree.h"
29 #include "stringpool.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "cp-tree.h"
33 #include "intl.h"
34 #include "c-family/c-pragma.h"
35 #include "decl.h"
36 #include "flags.h"
37 #include "diagnostic-core.h"
38 #include "target.h"
39 #include "cgraph.h"
40 #include "c-family/c-common.h"
41 #include "c-family/c-objc.h"
42 #include "plugin.h"
43 #include "tree-pretty-print.h"
44 #include "parser.h"
45 #include "type-utils.h"
46 #include "omp-low.h"
49 /* The lexer. */
51 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
52 and c-lex.c) and the C++ parser. */
54 static cp_token eof_token =
56 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
59 /* The various kinds of non integral constant we encounter. */
60 typedef enum non_integral_constant {
61 NIC_NONE,
62 /* floating-point literal */
63 NIC_FLOAT,
64 /* %<this%> */
65 NIC_THIS,
66 /* %<__FUNCTION__%> */
67 NIC_FUNC_NAME,
68 /* %<__PRETTY_FUNCTION__%> */
69 NIC_PRETTY_FUNC,
70 /* %<__func__%> */
71 NIC_C99_FUNC,
72 /* "%<va_arg%> */
73 NIC_VA_ARG,
74 /* a cast */
75 NIC_CAST,
76 /* %<typeid%> operator */
77 NIC_TYPEID,
78 /* non-constant compound literals */
79 NIC_NCC,
80 /* a function call */
81 NIC_FUNC_CALL,
82 /* an increment */
83 NIC_INC,
84 /* an decrement */
85 NIC_DEC,
86 /* an array reference */
87 NIC_ARRAY_REF,
88 /* %<->%> */
89 NIC_ARROW,
90 /* %<.%> */
91 NIC_POINT,
92 /* the address of a label */
93 NIC_ADDR_LABEL,
94 /* %<*%> */
95 NIC_STAR,
96 /* %<&%> */
97 NIC_ADDR,
98 /* %<++%> */
99 NIC_PREINCREMENT,
100 /* %<--%> */
101 NIC_PREDECREMENT,
102 /* %<new%> */
103 NIC_NEW,
104 /* %<delete%> */
105 NIC_DEL,
106 /* calls to overloaded operators */
107 NIC_OVERLOADED,
108 /* an assignment */
109 NIC_ASSIGNMENT,
110 /* a comma operator */
111 NIC_COMMA,
112 /* a call to a constructor */
113 NIC_CONSTRUCTOR,
114 /* a transaction expression */
115 NIC_TRANSACTION
116 } non_integral_constant;
118 /* The various kinds of errors about name-lookup failing. */
119 typedef enum name_lookup_error {
120 /* NULL */
121 NLE_NULL,
122 /* is not a type */
123 NLE_TYPE,
124 /* is not a class or namespace */
125 NLE_CXX98,
126 /* is not a class, namespace, or enumeration */
127 NLE_NOT_CXX98
128 } name_lookup_error;
130 /* The various kinds of required token */
131 typedef enum required_token {
132 RT_NONE,
133 RT_SEMICOLON, /* ';' */
134 RT_OPEN_PAREN, /* '(' */
135 RT_CLOSE_BRACE, /* '}' */
136 RT_OPEN_BRACE, /* '{' */
137 RT_CLOSE_SQUARE, /* ']' */
138 RT_OPEN_SQUARE, /* '[' */
139 RT_COMMA, /* ',' */
140 RT_SCOPE, /* '::' */
141 RT_LESS, /* '<' */
142 RT_GREATER, /* '>' */
143 RT_EQ, /* '=' */
144 RT_ELLIPSIS, /* '...' */
145 RT_MULT, /* '*' */
146 RT_COMPL, /* '~' */
147 RT_COLON, /* ':' */
148 RT_COLON_SCOPE, /* ':' or '::' */
149 RT_CLOSE_PAREN, /* ')' */
150 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
151 RT_PRAGMA_EOL, /* end of line */
152 RT_NAME, /* identifier */
154 /* The type is CPP_KEYWORD */
155 RT_NEW, /* new */
156 RT_DELETE, /* delete */
157 RT_RETURN, /* return */
158 RT_WHILE, /* while */
159 RT_EXTERN, /* extern */
160 RT_STATIC_ASSERT, /* static_assert */
161 RT_DECLTYPE, /* decltype */
162 RT_OPERATOR, /* operator */
163 RT_CLASS, /* class */
164 RT_TEMPLATE, /* template */
165 RT_NAMESPACE, /* namespace */
166 RT_USING, /* using */
167 RT_ASM, /* asm */
168 RT_TRY, /* try */
169 RT_CATCH, /* catch */
170 RT_THROW, /* throw */
171 RT_LABEL, /* __label__ */
172 RT_AT_TRY, /* @try */
173 RT_AT_SYNCHRONIZED, /* @synchronized */
174 RT_AT_THROW, /* @throw */
176 RT_SELECT, /* selection-statement */
177 RT_INTERATION, /* iteration-statement */
178 RT_JUMP, /* jump-statement */
179 RT_CLASS_KEY, /* class-key */
180 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
181 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
182 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
183 RT_TRANSACTION_CANCEL /* __transaction_cancel */
184 } required_token;
186 /* Prototypes. */
188 static cp_lexer *cp_lexer_new_main
189 (void);
190 static cp_lexer *cp_lexer_new_from_tokens
191 (cp_token_cache *tokens);
192 static void cp_lexer_destroy
193 (cp_lexer *);
194 static int cp_lexer_saving_tokens
195 (const cp_lexer *);
196 static cp_token *cp_lexer_token_at
197 (cp_lexer *, cp_token_position);
198 static void cp_lexer_get_preprocessor_token
199 (cp_lexer *, cp_token *);
200 static inline cp_token *cp_lexer_peek_token
201 (cp_lexer *);
202 static cp_token *cp_lexer_peek_nth_token
203 (cp_lexer *, size_t);
204 static inline bool cp_lexer_next_token_is
205 (cp_lexer *, enum cpp_ttype);
206 static bool cp_lexer_next_token_is_not
207 (cp_lexer *, enum cpp_ttype);
208 static bool cp_lexer_next_token_is_keyword
209 (cp_lexer *, enum rid);
210 static cp_token *cp_lexer_consume_token
211 (cp_lexer *);
212 static void cp_lexer_purge_token
213 (cp_lexer *);
214 static void cp_lexer_purge_tokens_after
215 (cp_lexer *, cp_token_position);
216 static void cp_lexer_save_tokens
217 (cp_lexer *);
218 static void cp_lexer_commit_tokens
219 (cp_lexer *);
220 static void cp_lexer_rollback_tokens
221 (cp_lexer *);
222 static void cp_lexer_print_token
223 (FILE *, cp_token *);
224 static inline bool cp_lexer_debugging_p
225 (cp_lexer *);
226 static void cp_lexer_start_debugging
227 (cp_lexer *) ATTRIBUTE_UNUSED;
228 static void cp_lexer_stop_debugging
229 (cp_lexer *) ATTRIBUTE_UNUSED;
231 static cp_token_cache *cp_token_cache_new
232 (cp_token *, cp_token *);
234 static void cp_parser_initial_pragma
235 (cp_token *);
237 static tree cp_literal_operator_id
238 (const char *);
240 static void cp_parser_cilk_simd
241 (cp_parser *, cp_token *);
242 static tree cp_parser_cilk_for
243 (cp_parser *, tree);
244 static bool cp_parser_omp_declare_reduction_exprs
245 (tree, cp_parser *);
246 static tree cp_parser_cilk_simd_vectorlength
247 (cp_parser *, tree, bool);
249 /* Manifest constants. */
250 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
251 #define CP_SAVED_TOKEN_STACK 5
253 /* Variables. */
255 /* The stream to which debugging output should be written. */
256 static FILE *cp_lexer_debug_stream;
258 /* Nonzero if we are parsing an unevaluated operand: an operand to
259 sizeof, typeof, or alignof. */
260 int cp_unevaluated_operand;
262 /* Dump up to NUM tokens in BUFFER to FILE starting with token
263 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
264 first token in BUFFER. If NUM is 0, dump all the tokens. If
265 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
266 highlighted by surrounding it in [[ ]]. */
268 static void
269 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
270 cp_token *start_token, unsigned num,
271 cp_token *curr_token)
273 unsigned i, nprinted;
274 cp_token *token;
275 bool do_print;
277 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
279 if (buffer == NULL)
280 return;
282 if (num == 0)
283 num = buffer->length ();
285 if (start_token == NULL)
286 start_token = buffer->address ();
288 if (start_token > buffer->address ())
290 cp_lexer_print_token (file, &(*buffer)[0]);
291 fprintf (file, " ... ");
294 do_print = false;
295 nprinted = 0;
296 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
298 if (token == start_token)
299 do_print = true;
301 if (!do_print)
302 continue;
304 nprinted++;
305 if (token == curr_token)
306 fprintf (file, "[[");
308 cp_lexer_print_token (file, token);
310 if (token == curr_token)
311 fprintf (file, "]]");
313 switch (token->type)
315 case CPP_SEMICOLON:
316 case CPP_OPEN_BRACE:
317 case CPP_CLOSE_BRACE:
318 case CPP_EOF:
319 fputc ('\n', file);
320 break;
322 default:
323 fputc (' ', file);
327 if (i == num && i < buffer->length ())
329 fprintf (file, " ... ");
330 cp_lexer_print_token (file, &buffer->last ());
333 fprintf (file, "\n");
337 /* Dump all tokens in BUFFER to stderr. */
339 void
340 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
342 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
345 DEBUG_FUNCTION void
346 debug (vec<cp_token, va_gc> &ref)
348 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
351 DEBUG_FUNCTION void
352 debug (vec<cp_token, va_gc> *ptr)
354 if (ptr)
355 debug (*ptr);
356 else
357 fprintf (stderr, "<nil>\n");
361 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
362 description for T. */
364 static void
365 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
367 if (t)
369 fprintf (file, "%s: ", desc);
370 print_node_brief (file, "", t, 0);
375 /* Dump parser context C to FILE. */
377 static void
378 cp_debug_print_context (FILE *file, cp_parser_context *c)
380 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
381 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
382 print_node_brief (file, "", c->object_type, 0);
383 fprintf (file, "}\n");
387 /* Print the stack of parsing contexts to FILE starting with FIRST. */
389 static void
390 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
392 unsigned i;
393 cp_parser_context *c;
395 fprintf (file, "Parsing context stack:\n");
396 for (i = 0, c = first; c; c = c->next, i++)
398 fprintf (file, "\t#%u: ", i);
399 cp_debug_print_context (file, c);
404 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
406 static void
407 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
409 if (flag)
410 fprintf (file, "%s: true\n", desc);
414 /* Print an unparsed function entry UF to FILE. */
416 static void
417 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
419 unsigned i;
420 cp_default_arg_entry *default_arg_fn;
421 tree fn;
423 fprintf (file, "\tFunctions with default args:\n");
424 for (i = 0;
425 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
426 i++)
428 fprintf (file, "\t\tClass type: ");
429 print_node_brief (file, "", default_arg_fn->class_type, 0);
430 fprintf (file, "\t\tDeclaration: ");
431 print_node_brief (file, "", default_arg_fn->decl, 0);
432 fprintf (file, "\n");
435 fprintf (file, "\n\tFunctions with definitions that require "
436 "post-processing\n\t\t");
437 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
439 print_node_brief (file, "", fn, 0);
440 fprintf (file, " ");
442 fprintf (file, "\n");
444 fprintf (file, "\n\tNon-static data members with initializers that require "
445 "post-processing\n\t\t");
446 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
448 print_node_brief (file, "", fn, 0);
449 fprintf (file, " ");
451 fprintf (file, "\n");
455 /* Print the stack of unparsed member functions S to FILE. */
457 static void
458 cp_debug_print_unparsed_queues (FILE *file,
459 vec<cp_unparsed_functions_entry, va_gc> *s)
461 unsigned i;
462 cp_unparsed_functions_entry *uf;
464 fprintf (file, "Unparsed functions\n");
465 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
467 fprintf (file, "#%u:\n", i);
468 cp_debug_print_unparsed_function (file, uf);
473 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
474 the given PARSER. If FILE is NULL, the output is printed on stderr. */
476 static void
477 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
479 cp_token *next_token, *first_token, *start_token;
481 if (file == NULL)
482 file = stderr;
484 next_token = parser->lexer->next_token;
485 first_token = parser->lexer->buffer->address ();
486 start_token = (next_token > first_token + window_size / 2)
487 ? next_token - window_size / 2
488 : first_token;
489 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
490 next_token);
494 /* Dump debugging information for the given PARSER. If FILE is NULL,
495 the output is printed on stderr. */
497 void
498 cp_debug_parser (FILE *file, cp_parser *parser)
500 const size_t window_size = 20;
501 cp_token *token;
502 expanded_location eloc;
504 if (file == NULL)
505 file = stderr;
507 fprintf (file, "Parser state\n\n");
508 fprintf (file, "Number of tokens: %u\n",
509 vec_safe_length (parser->lexer->buffer));
510 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
511 cp_debug_print_tree_if_set (file, "Object scope",
512 parser->object_scope);
513 cp_debug_print_tree_if_set (file, "Qualifying scope",
514 parser->qualifying_scope);
515 cp_debug_print_context_stack (file, parser->context);
516 cp_debug_print_flag (file, "Allow GNU extensions",
517 parser->allow_gnu_extensions_p);
518 cp_debug_print_flag (file, "'>' token is greater-than",
519 parser->greater_than_is_operator_p);
520 cp_debug_print_flag (file, "Default args allowed in current "
521 "parameter list", parser->default_arg_ok_p);
522 cp_debug_print_flag (file, "Parsing integral constant-expression",
523 parser->integral_constant_expression_p);
524 cp_debug_print_flag (file, "Allow non-constant expression in current "
525 "constant-expression",
526 parser->allow_non_integral_constant_expression_p);
527 cp_debug_print_flag (file, "Seen non-constant expression",
528 parser->non_integral_constant_expression_p);
529 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
530 "current context",
531 parser->local_variables_forbidden_p);
532 cp_debug_print_flag (file, "In unbraced linkage specification",
533 parser->in_unbraced_linkage_specification_p);
534 cp_debug_print_flag (file, "Parsing a declarator",
535 parser->in_declarator_p);
536 cp_debug_print_flag (file, "In template argument list",
537 parser->in_template_argument_list_p);
538 cp_debug_print_flag (file, "Parsing an iteration statement",
539 parser->in_statement & IN_ITERATION_STMT);
540 cp_debug_print_flag (file, "Parsing a switch statement",
541 parser->in_statement & IN_SWITCH_STMT);
542 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
543 parser->in_statement & IN_OMP_BLOCK);
544 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
545 parser->in_statement & IN_CILK_SIMD_FOR);
546 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
547 parser->in_statement & IN_OMP_FOR);
548 cp_debug_print_flag (file, "Parsing an if statement",
549 parser->in_statement & IN_IF_STMT);
550 cp_debug_print_flag (file, "Parsing a type-id in an expression "
551 "context", parser->in_type_id_in_expr_p);
552 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
553 parser->implicit_extern_c);
554 cp_debug_print_flag (file, "String expressions should be translated "
555 "to execution character set",
556 parser->translate_strings_p);
557 cp_debug_print_flag (file, "Parsing function body outside of a "
558 "local class", parser->in_function_body);
559 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
560 parser->colon_corrects_to_scope_p);
561 cp_debug_print_flag (file, "Colon doesn't start a class definition",
562 parser->colon_doesnt_start_class_def_p);
563 if (parser->type_definition_forbidden_message)
564 fprintf (file, "Error message for forbidden type definitions: %s\n",
565 parser->type_definition_forbidden_message);
566 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
567 fprintf (file, "Number of class definitions in progress: %u\n",
568 parser->num_classes_being_defined);
569 fprintf (file, "Number of template parameter lists for the current "
570 "declaration: %u\n", parser->num_template_parameter_lists);
571 cp_debug_parser_tokens (file, parser, window_size);
572 token = parser->lexer->next_token;
573 fprintf (file, "Next token to parse:\n");
574 fprintf (file, "\tToken: ");
575 cp_lexer_print_token (file, token);
576 eloc = expand_location (token->location);
577 fprintf (file, "\n\tFile: %s\n", eloc.file);
578 fprintf (file, "\tLine: %d\n", eloc.line);
579 fprintf (file, "\tColumn: %d\n", eloc.column);
582 DEBUG_FUNCTION void
583 debug (cp_parser &ref)
585 cp_debug_parser (stderr, &ref);
588 DEBUG_FUNCTION void
589 debug (cp_parser *ptr)
591 if (ptr)
592 debug (*ptr);
593 else
594 fprintf (stderr, "<nil>\n");
597 /* Allocate memory for a new lexer object and return it. */
599 static cp_lexer *
600 cp_lexer_alloc (void)
602 cp_lexer *lexer;
604 c_common_no_more_pch ();
606 /* Allocate the memory. */
607 lexer = ggc_cleared_alloc<cp_lexer> ();
609 /* Initially we are not debugging. */
610 lexer->debugging_p = false;
612 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
614 /* Create the buffer. */
615 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
617 return lexer;
621 /* Create a new main C++ lexer, the lexer that gets tokens from the
622 preprocessor. */
624 static cp_lexer *
625 cp_lexer_new_main (void)
627 cp_lexer *lexer;
628 cp_token token;
630 /* It's possible that parsing the first pragma will load a PCH file,
631 which is a GC collection point. So we have to do that before
632 allocating any memory. */
633 cp_parser_initial_pragma (&token);
635 lexer = cp_lexer_alloc ();
637 /* Put the first token in the buffer. */
638 lexer->buffer->quick_push (token);
640 /* Get the remaining tokens from the preprocessor. */
641 while (token.type != CPP_EOF)
643 cp_lexer_get_preprocessor_token (lexer, &token);
644 vec_safe_push (lexer->buffer, token);
647 lexer->last_token = lexer->buffer->address ()
648 + lexer->buffer->length ()
649 - 1;
650 lexer->next_token = lexer->buffer->length ()
651 ? lexer->buffer->address ()
652 : &eof_token;
654 /* Subsequent preprocessor diagnostics should use compiler
655 diagnostic functions to get the compiler source location. */
656 done_lexing = true;
658 gcc_assert (!lexer->next_token->purged_p);
659 return lexer;
662 /* Create a new lexer whose token stream is primed with the tokens in
663 CACHE. When these tokens are exhausted, no new tokens will be read. */
665 static cp_lexer *
666 cp_lexer_new_from_tokens (cp_token_cache *cache)
668 cp_token *first = cache->first;
669 cp_token *last = cache->last;
670 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
672 /* We do not own the buffer. */
673 lexer->buffer = NULL;
674 lexer->next_token = first == last ? &eof_token : first;
675 lexer->last_token = last;
677 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
679 /* Initially we are not debugging. */
680 lexer->debugging_p = false;
682 gcc_assert (!lexer->next_token->purged_p);
683 return lexer;
686 /* Frees all resources associated with LEXER. */
688 static void
689 cp_lexer_destroy (cp_lexer *lexer)
691 vec_free (lexer->buffer);
692 lexer->saved_tokens.release ();
693 ggc_free (lexer);
696 /* Returns nonzero if debugging information should be output. */
698 static inline bool
699 cp_lexer_debugging_p (cp_lexer *lexer)
701 return lexer->debugging_p;
705 static inline cp_token_position
706 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
708 gcc_assert (!previous_p || lexer->next_token != &eof_token);
710 return lexer->next_token - previous_p;
713 static inline cp_token *
714 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
716 return pos;
719 static inline void
720 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
722 lexer->next_token = cp_lexer_token_at (lexer, pos);
725 static inline cp_token_position
726 cp_lexer_previous_token_position (cp_lexer *lexer)
728 if (lexer->next_token == &eof_token)
729 return lexer->last_token - 1;
730 else
731 return cp_lexer_token_position (lexer, true);
734 static inline cp_token *
735 cp_lexer_previous_token (cp_lexer *lexer)
737 cp_token_position tp = cp_lexer_previous_token_position (lexer);
739 return cp_lexer_token_at (lexer, tp);
742 /* nonzero if we are presently saving tokens. */
744 static inline int
745 cp_lexer_saving_tokens (const cp_lexer* lexer)
747 return lexer->saved_tokens.length () != 0;
750 /* Store the next token from the preprocessor in *TOKEN. Return true
751 if we reach EOF. If LEXER is NULL, assume we are handling an
752 initial #pragma pch_preprocess, and thus want the lexer to return
753 processed strings. */
755 static void
756 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
758 static int is_extern_c = 0;
760 /* Get a new token from the preprocessor. */
761 token->type
762 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
763 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
764 token->keyword = RID_MAX;
765 token->pragma_kind = PRAGMA_NONE;
766 token->purged_p = false;
767 token->error_reported = false;
769 /* On some systems, some header files are surrounded by an
770 implicit extern "C" block. Set a flag in the token if it
771 comes from such a header. */
772 is_extern_c += pending_lang_change;
773 pending_lang_change = 0;
774 token->implicit_extern_c = is_extern_c > 0;
776 /* Check to see if this token is a keyword. */
777 if (token->type == CPP_NAME)
779 if (C_IS_RESERVED_WORD (token->u.value))
781 /* Mark this token as a keyword. */
782 token->type = CPP_KEYWORD;
783 /* Record which keyword. */
784 token->keyword = C_RID_CODE (token->u.value);
786 else
788 if (warn_cxx0x_compat
789 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
790 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
792 /* Warn about the C++0x keyword (but still treat it as
793 an identifier). */
794 warning (OPT_Wc__0x_compat,
795 "identifier %qE is a keyword in C++11",
796 token->u.value);
798 /* Clear out the C_RID_CODE so we don't warn about this
799 particular identifier-turned-keyword again. */
800 C_SET_RID_CODE (token->u.value, RID_MAX);
803 token->keyword = RID_MAX;
806 else if (token->type == CPP_AT_NAME)
808 /* This only happens in Objective-C++; it must be a keyword. */
809 token->type = CPP_KEYWORD;
810 switch (C_RID_CODE (token->u.value))
812 /* Replace 'class' with '@class', 'private' with '@private',
813 etc. This prevents confusion with the C++ keyword
814 'class', and makes the tokens consistent with other
815 Objective-C 'AT' keywords. For example '@class' is
816 reported as RID_AT_CLASS which is consistent with
817 '@synchronized', which is reported as
818 RID_AT_SYNCHRONIZED.
820 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
821 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
822 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
823 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
824 case RID_THROW: token->keyword = RID_AT_THROW; break;
825 case RID_TRY: token->keyword = RID_AT_TRY; break;
826 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
827 default: token->keyword = C_RID_CODE (token->u.value);
830 else if (token->type == CPP_PRAGMA)
832 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
833 token->pragma_kind = ((enum pragma_kind)
834 TREE_INT_CST_LOW (token->u.value));
835 token->u.value = NULL_TREE;
839 /* Update the globals input_location and the input file stack from TOKEN. */
840 static inline void
841 cp_lexer_set_source_position_from_token (cp_token *token)
843 if (token->type != CPP_EOF)
845 input_location = token->location;
849 /* Update the globals input_location and the input file stack from LEXER. */
850 static inline void
851 cp_lexer_set_source_position (cp_lexer *lexer)
853 cp_token *token = cp_lexer_peek_token (lexer);
854 cp_lexer_set_source_position_from_token (token);
857 /* Return a pointer to the next token in the token stream, but do not
858 consume it. */
860 static inline cp_token *
861 cp_lexer_peek_token (cp_lexer *lexer)
863 if (cp_lexer_debugging_p (lexer))
865 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
866 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
867 putc ('\n', cp_lexer_debug_stream);
869 return lexer->next_token;
872 /* Return true if the next token has the indicated TYPE. */
874 static inline bool
875 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
877 return cp_lexer_peek_token (lexer)->type == type;
880 /* Return true if the next token does not have the indicated TYPE. */
882 static inline bool
883 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
885 return !cp_lexer_next_token_is (lexer, type);
888 /* Return true if the next token is the indicated KEYWORD. */
890 static inline bool
891 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
893 return cp_lexer_peek_token (lexer)->keyword == keyword;
896 static inline bool
897 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
899 return cp_lexer_peek_nth_token (lexer, n)->type == type;
902 static inline bool
903 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
905 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
908 /* Return true if the next token is not the indicated KEYWORD. */
910 static inline bool
911 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
913 return cp_lexer_peek_token (lexer)->keyword != keyword;
916 /* Return true if the next token is a keyword for a decl-specifier. */
918 static bool
919 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
921 cp_token *token;
923 token = cp_lexer_peek_token (lexer);
924 switch (token->keyword)
926 /* auto specifier: storage-class-specifier in C++,
927 simple-type-specifier in C++0x. */
928 case RID_AUTO:
929 /* Storage classes. */
930 case RID_REGISTER:
931 case RID_STATIC:
932 case RID_EXTERN:
933 case RID_MUTABLE:
934 case RID_THREAD:
935 /* Elaborated type specifiers. */
936 case RID_ENUM:
937 case RID_CLASS:
938 case RID_STRUCT:
939 case RID_UNION:
940 case RID_TYPENAME:
941 /* Simple type specifiers. */
942 case RID_CHAR:
943 case RID_CHAR16:
944 case RID_CHAR32:
945 case RID_WCHAR:
946 case RID_BOOL:
947 case RID_SHORT:
948 case RID_INT:
949 case RID_LONG:
950 case RID_INT128:
951 case RID_SIGNED:
952 case RID_UNSIGNED:
953 case RID_FLOAT:
954 case RID_DOUBLE:
955 case RID_VOID:
956 /* GNU extensions. */
957 case RID_ATTRIBUTE:
958 case RID_TYPEOF:
959 /* C++0x extensions. */
960 case RID_DECLTYPE:
961 case RID_UNDERLYING_TYPE:
962 return true;
964 default:
965 return false;
969 /* Returns TRUE iff the token T begins a decltype type. */
971 static bool
972 token_is_decltype (cp_token *t)
974 return (t->keyword == RID_DECLTYPE
975 || t->type == CPP_DECLTYPE);
978 /* Returns TRUE iff the next token begins a decltype type. */
980 static bool
981 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
983 cp_token *t = cp_lexer_peek_token (lexer);
984 return token_is_decltype (t);
987 /* Return a pointer to the Nth token in the token stream. If N is 1,
988 then this is precisely equivalent to cp_lexer_peek_token (except
989 that it is not inline). One would like to disallow that case, but
990 there is one case (cp_parser_nth_token_starts_template_id) where
991 the caller passes a variable for N and it might be 1. */
993 static cp_token *
994 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
996 cp_token *token;
998 /* N is 1-based, not zero-based. */
999 gcc_assert (n > 0);
1001 if (cp_lexer_debugging_p (lexer))
1002 fprintf (cp_lexer_debug_stream,
1003 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1005 --n;
1006 token = lexer->next_token;
1007 gcc_assert (!n || token != &eof_token);
1008 while (n != 0)
1010 ++token;
1011 if (token == lexer->last_token)
1013 token = &eof_token;
1014 break;
1017 if (!token->purged_p)
1018 --n;
1021 if (cp_lexer_debugging_p (lexer))
1023 cp_lexer_print_token (cp_lexer_debug_stream, token);
1024 putc ('\n', cp_lexer_debug_stream);
1027 return token;
1030 /* Return the next token, and advance the lexer's next_token pointer
1031 to point to the next non-purged token. */
1033 static cp_token *
1034 cp_lexer_consume_token (cp_lexer* lexer)
1036 cp_token *token = lexer->next_token;
1038 gcc_assert (token != &eof_token);
1039 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1043 lexer->next_token++;
1044 if (lexer->next_token == lexer->last_token)
1046 lexer->next_token = &eof_token;
1047 break;
1051 while (lexer->next_token->purged_p);
1053 cp_lexer_set_source_position_from_token (token);
1055 /* Provide debugging output. */
1056 if (cp_lexer_debugging_p (lexer))
1058 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1059 cp_lexer_print_token (cp_lexer_debug_stream, token);
1060 putc ('\n', cp_lexer_debug_stream);
1063 return token;
1066 /* Permanently remove the next token from the token stream, and
1067 advance the next_token pointer to refer to the next non-purged
1068 token. */
1070 static void
1071 cp_lexer_purge_token (cp_lexer *lexer)
1073 cp_token *tok = lexer->next_token;
1075 gcc_assert (tok != &eof_token);
1076 tok->purged_p = true;
1077 tok->location = UNKNOWN_LOCATION;
1078 tok->u.value = NULL_TREE;
1079 tok->keyword = RID_MAX;
1083 tok++;
1084 if (tok == lexer->last_token)
1086 tok = &eof_token;
1087 break;
1090 while (tok->purged_p);
1091 lexer->next_token = tok;
1094 /* Permanently remove all tokens after TOK, up to, but not
1095 including, the token that will be returned next by
1096 cp_lexer_peek_token. */
1098 static void
1099 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1101 cp_token *peek = lexer->next_token;
1103 if (peek == &eof_token)
1104 peek = lexer->last_token;
1106 gcc_assert (tok < peek);
1108 for ( tok += 1; tok != peek; tok += 1)
1110 tok->purged_p = true;
1111 tok->location = UNKNOWN_LOCATION;
1112 tok->u.value = NULL_TREE;
1113 tok->keyword = RID_MAX;
1117 /* Begin saving tokens. All tokens consumed after this point will be
1118 preserved. */
1120 static void
1121 cp_lexer_save_tokens (cp_lexer* lexer)
1123 /* Provide debugging output. */
1124 if (cp_lexer_debugging_p (lexer))
1125 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1127 lexer->saved_tokens.safe_push (lexer->next_token);
1130 /* Commit to the portion of the token stream most recently saved. */
1132 static void
1133 cp_lexer_commit_tokens (cp_lexer* lexer)
1135 /* Provide debugging output. */
1136 if (cp_lexer_debugging_p (lexer))
1137 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1139 lexer->saved_tokens.pop ();
1142 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1143 to the token stream. Stop saving tokens. */
1145 static void
1146 cp_lexer_rollback_tokens (cp_lexer* lexer)
1148 /* Provide debugging output. */
1149 if (cp_lexer_debugging_p (lexer))
1150 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1152 lexer->next_token = lexer->saved_tokens.pop ();
1155 /* Print a representation of the TOKEN on the STREAM. */
1157 static void
1158 cp_lexer_print_token (FILE * stream, cp_token *token)
1160 /* We don't use cpp_type2name here because the parser defines
1161 a few tokens of its own. */
1162 static const char *const token_names[] = {
1163 /* cpplib-defined token types */
1164 #define OP(e, s) #e,
1165 #define TK(e, s) #e,
1166 TTYPE_TABLE
1167 #undef OP
1168 #undef TK
1169 /* C++ parser token types - see "Manifest constants", above. */
1170 "KEYWORD",
1171 "TEMPLATE_ID",
1172 "NESTED_NAME_SPECIFIER",
1175 /* For some tokens, print the associated data. */
1176 switch (token->type)
1178 case CPP_KEYWORD:
1179 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1180 For example, `struct' is mapped to an INTEGER_CST. */
1181 if (!identifier_p (token->u.value))
1182 break;
1183 /* else fall through */
1184 case CPP_NAME:
1185 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1186 break;
1188 case CPP_STRING:
1189 case CPP_STRING16:
1190 case CPP_STRING32:
1191 case CPP_WSTRING:
1192 case CPP_UTF8STRING:
1193 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1194 break;
1196 case CPP_NUMBER:
1197 print_generic_expr (stream, token->u.value, 0);
1198 break;
1200 default:
1201 /* If we have a name for the token, print it out. Otherwise, we
1202 simply give the numeric code. */
1203 if (token->type < ARRAY_SIZE(token_names))
1204 fputs (token_names[token->type], stream);
1205 else
1206 fprintf (stream, "[%d]", token->type);
1207 break;
1211 DEBUG_FUNCTION void
1212 debug (cp_token &ref)
1214 cp_lexer_print_token (stderr, &ref);
1215 fprintf (stderr, "\n");
1218 DEBUG_FUNCTION void
1219 debug (cp_token *ptr)
1221 if (ptr)
1222 debug (*ptr);
1223 else
1224 fprintf (stderr, "<nil>\n");
1228 /* Start emitting debugging information. */
1230 static void
1231 cp_lexer_start_debugging (cp_lexer* lexer)
1233 lexer->debugging_p = true;
1234 cp_lexer_debug_stream = stderr;
1237 /* Stop emitting debugging information. */
1239 static void
1240 cp_lexer_stop_debugging (cp_lexer* lexer)
1242 lexer->debugging_p = false;
1243 cp_lexer_debug_stream = NULL;
1246 /* Create a new cp_token_cache, representing a range of tokens. */
1248 static cp_token_cache *
1249 cp_token_cache_new (cp_token *first, cp_token *last)
1251 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1252 cache->first = first;
1253 cache->last = last;
1254 return cache;
1257 /* Diagnose if #pragma omp declare simd isn't followed immediately
1258 by function declaration or definition. */
1260 static inline void
1261 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1263 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1265 error ("%<#pragma omp declare simd%> not immediately followed by "
1266 "function declaration or definition");
1267 parser->omp_declare_simd = NULL;
1271 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1272 and put that into "omp declare simd" attribute. */
1274 static inline void
1275 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1277 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1279 if (fndecl == error_mark_node)
1281 parser->omp_declare_simd = NULL;
1282 return;
1284 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1286 cp_ensure_no_omp_declare_simd (parser);
1287 return;
1292 /* Decl-specifiers. */
1294 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1296 static void
1297 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1299 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1302 /* Declarators. */
1304 /* Nothing other than the parser should be creating declarators;
1305 declarators are a semi-syntactic representation of C++ entities.
1306 Other parts of the front end that need to create entities (like
1307 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1309 static cp_declarator *make_call_declarator
1310 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree);
1311 static cp_declarator *make_array_declarator
1312 (cp_declarator *, tree);
1313 static cp_declarator *make_pointer_declarator
1314 (cp_cv_quals, cp_declarator *, tree);
1315 static cp_declarator *make_reference_declarator
1316 (cp_cv_quals, cp_declarator *, bool, tree);
1317 static cp_parameter_declarator *make_parameter_declarator
1318 (cp_decl_specifier_seq *, cp_declarator *, tree);
1319 static cp_declarator *make_ptrmem_declarator
1320 (cp_cv_quals, tree, cp_declarator *, tree);
1322 /* An erroneous declarator. */
1323 static cp_declarator *cp_error_declarator;
1325 /* The obstack on which declarators and related data structures are
1326 allocated. */
1327 static struct obstack declarator_obstack;
1329 /* Alloc BYTES from the declarator memory pool. */
1331 static inline void *
1332 alloc_declarator (size_t bytes)
1334 return obstack_alloc (&declarator_obstack, bytes);
1337 /* Allocate a declarator of the indicated KIND. Clear fields that are
1338 common to all declarators. */
1340 static cp_declarator *
1341 make_declarator (cp_declarator_kind kind)
1343 cp_declarator *declarator;
1345 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1346 declarator->kind = kind;
1347 declarator->attributes = NULL_TREE;
1348 declarator->std_attributes = NULL_TREE;
1349 declarator->declarator = NULL;
1350 declarator->parameter_pack_p = false;
1351 declarator->id_loc = UNKNOWN_LOCATION;
1353 return declarator;
1356 /* Make a declarator for a generalized identifier. If
1357 QUALIFYING_SCOPE is non-NULL, the identifier is
1358 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1359 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1360 is, if any. */
1362 static cp_declarator *
1363 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1364 special_function_kind sfk)
1366 cp_declarator *declarator;
1368 /* It is valid to write:
1370 class C { void f(); };
1371 typedef C D;
1372 void D::f();
1374 The standard is not clear about whether `typedef const C D' is
1375 legal; as of 2002-09-15 the committee is considering that
1376 question. EDG 3.0 allows that syntax. Therefore, we do as
1377 well. */
1378 if (qualifying_scope && TYPE_P (qualifying_scope))
1379 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1381 gcc_assert (identifier_p (unqualified_name)
1382 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1383 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1385 declarator = make_declarator (cdk_id);
1386 declarator->u.id.qualifying_scope = qualifying_scope;
1387 declarator->u.id.unqualified_name = unqualified_name;
1388 declarator->u.id.sfk = sfk;
1390 return declarator;
1393 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1394 of modifiers such as const or volatile to apply to the pointer
1395 type, represented as identifiers. ATTRIBUTES represent the attributes that
1396 appertain to the pointer or reference. */
1398 cp_declarator *
1399 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1400 tree attributes)
1402 cp_declarator *declarator;
1404 declarator = make_declarator (cdk_pointer);
1405 declarator->declarator = target;
1406 declarator->u.pointer.qualifiers = cv_qualifiers;
1407 declarator->u.pointer.class_type = NULL_TREE;
1408 if (target)
1410 declarator->id_loc = target->id_loc;
1411 declarator->parameter_pack_p = target->parameter_pack_p;
1412 target->parameter_pack_p = false;
1414 else
1415 declarator->parameter_pack_p = false;
1417 declarator->std_attributes = attributes;
1419 return declarator;
1422 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1423 represent the attributes that appertain to the pointer or
1424 reference. */
1426 cp_declarator *
1427 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1428 bool rvalue_ref, tree attributes)
1430 cp_declarator *declarator;
1432 declarator = make_declarator (cdk_reference);
1433 declarator->declarator = target;
1434 declarator->u.reference.qualifiers = cv_qualifiers;
1435 declarator->u.reference.rvalue_ref = rvalue_ref;
1436 if (target)
1438 declarator->id_loc = target->id_loc;
1439 declarator->parameter_pack_p = target->parameter_pack_p;
1440 target->parameter_pack_p = false;
1442 else
1443 declarator->parameter_pack_p = false;
1445 declarator->std_attributes = attributes;
1447 return declarator;
1450 /* Like make_pointer_declarator -- but for a pointer to a non-static
1451 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1452 appertain to the pointer or reference. */
1454 cp_declarator *
1455 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1456 cp_declarator *pointee,
1457 tree attributes)
1459 cp_declarator *declarator;
1461 declarator = make_declarator (cdk_ptrmem);
1462 declarator->declarator = pointee;
1463 declarator->u.pointer.qualifiers = cv_qualifiers;
1464 declarator->u.pointer.class_type = class_type;
1466 if (pointee)
1468 declarator->parameter_pack_p = pointee->parameter_pack_p;
1469 pointee->parameter_pack_p = false;
1471 else
1472 declarator->parameter_pack_p = false;
1474 declarator->std_attributes = attributes;
1476 return declarator;
1479 /* Make a declarator for the function given by TARGET, with the
1480 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1481 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1482 indicates what exceptions can be thrown. */
1484 cp_declarator *
1485 make_call_declarator (cp_declarator *target,
1486 tree parms,
1487 cp_cv_quals cv_qualifiers,
1488 cp_virt_specifiers virt_specifiers,
1489 cp_ref_qualifier ref_qualifier,
1490 tree exception_specification,
1491 tree late_return_type)
1493 cp_declarator *declarator;
1495 declarator = make_declarator (cdk_function);
1496 declarator->declarator = target;
1497 declarator->u.function.parameters = parms;
1498 declarator->u.function.qualifiers = cv_qualifiers;
1499 declarator->u.function.virt_specifiers = virt_specifiers;
1500 declarator->u.function.ref_qualifier = ref_qualifier;
1501 declarator->u.function.exception_specification = exception_specification;
1502 declarator->u.function.late_return_type = late_return_type;
1503 if (target)
1505 declarator->id_loc = target->id_loc;
1506 declarator->parameter_pack_p = target->parameter_pack_p;
1507 target->parameter_pack_p = false;
1509 else
1510 declarator->parameter_pack_p = false;
1512 return declarator;
1515 /* Make a declarator for an array of BOUNDS elements, each of which is
1516 defined by ELEMENT. */
1518 cp_declarator *
1519 make_array_declarator (cp_declarator *element, tree bounds)
1521 cp_declarator *declarator;
1523 declarator = make_declarator (cdk_array);
1524 declarator->declarator = element;
1525 declarator->u.array.bounds = bounds;
1526 if (element)
1528 declarator->id_loc = element->id_loc;
1529 declarator->parameter_pack_p = element->parameter_pack_p;
1530 element->parameter_pack_p = false;
1532 else
1533 declarator->parameter_pack_p = false;
1535 return declarator;
1538 /* Determine whether the declarator we've seen so far can be a
1539 parameter pack, when followed by an ellipsis. */
1540 static bool
1541 declarator_can_be_parameter_pack (cp_declarator *declarator)
1543 /* Search for a declarator name, or any other declarator that goes
1544 after the point where the ellipsis could appear in a parameter
1545 pack. If we find any of these, then this declarator can not be
1546 made into a parameter pack. */
1547 bool found = false;
1548 while (declarator && !found)
1550 switch ((int)declarator->kind)
1552 case cdk_id:
1553 case cdk_array:
1554 found = true;
1555 break;
1557 case cdk_error:
1558 return true;
1560 default:
1561 declarator = declarator->declarator;
1562 break;
1566 return !found;
1569 cp_parameter_declarator *no_parameters;
1571 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1572 DECLARATOR and DEFAULT_ARGUMENT. */
1574 cp_parameter_declarator *
1575 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1576 cp_declarator *declarator,
1577 tree default_argument)
1579 cp_parameter_declarator *parameter;
1581 parameter = ((cp_parameter_declarator *)
1582 alloc_declarator (sizeof (cp_parameter_declarator)));
1583 parameter->next = NULL;
1584 if (decl_specifiers)
1585 parameter->decl_specifiers = *decl_specifiers;
1586 else
1587 clear_decl_specs (&parameter->decl_specifiers);
1588 parameter->declarator = declarator;
1589 parameter->default_argument = default_argument;
1590 parameter->ellipsis_p = false;
1592 return parameter;
1595 /* Returns true iff DECLARATOR is a declaration for a function. */
1597 static bool
1598 function_declarator_p (const cp_declarator *declarator)
1600 while (declarator)
1602 if (declarator->kind == cdk_function
1603 && declarator->declarator->kind == cdk_id)
1604 return true;
1605 if (declarator->kind == cdk_id
1606 || declarator->kind == cdk_error)
1607 return false;
1608 declarator = declarator->declarator;
1610 return false;
1613 /* The parser. */
1615 /* Overview
1616 --------
1618 A cp_parser parses the token stream as specified by the C++
1619 grammar. Its job is purely parsing, not semantic analysis. For
1620 example, the parser breaks the token stream into declarators,
1621 expressions, statements, and other similar syntactic constructs.
1622 It does not check that the types of the expressions on either side
1623 of an assignment-statement are compatible, or that a function is
1624 not declared with a parameter of type `void'.
1626 The parser invokes routines elsewhere in the compiler to perform
1627 semantic analysis and to build up the abstract syntax tree for the
1628 code processed.
1630 The parser (and the template instantiation code, which is, in a
1631 way, a close relative of parsing) are the only parts of the
1632 compiler that should be calling push_scope and pop_scope, or
1633 related functions. The parser (and template instantiation code)
1634 keeps track of what scope is presently active; everything else
1635 should simply honor that. (The code that generates static
1636 initializers may also need to set the scope, in order to check
1637 access control correctly when emitting the initializers.)
1639 Methodology
1640 -----------
1642 The parser is of the standard recursive-descent variety. Upcoming
1643 tokens in the token stream are examined in order to determine which
1644 production to use when parsing a non-terminal. Some C++ constructs
1645 require arbitrary look ahead to disambiguate. For example, it is
1646 impossible, in the general case, to tell whether a statement is an
1647 expression or declaration without scanning the entire statement.
1648 Therefore, the parser is capable of "parsing tentatively." When the
1649 parser is not sure what construct comes next, it enters this mode.
1650 Then, while we attempt to parse the construct, the parser queues up
1651 error messages, rather than issuing them immediately, and saves the
1652 tokens it consumes. If the construct is parsed successfully, the
1653 parser "commits", i.e., it issues any queued error messages and
1654 the tokens that were being preserved are permanently discarded.
1655 If, however, the construct is not parsed successfully, the parser
1656 rolls back its state completely so that it can resume parsing using
1657 a different alternative.
1659 Future Improvements
1660 -------------------
1662 The performance of the parser could probably be improved substantially.
1663 We could often eliminate the need to parse tentatively by looking ahead
1664 a little bit. In some places, this approach might not entirely eliminate
1665 the need to parse tentatively, but it might still speed up the average
1666 case. */
1668 /* Flags that are passed to some parsing functions. These values can
1669 be bitwise-ored together. */
1671 enum
1673 /* No flags. */
1674 CP_PARSER_FLAGS_NONE = 0x0,
1675 /* The construct is optional. If it is not present, then no error
1676 should be issued. */
1677 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1678 /* When parsing a type-specifier, treat user-defined type-names
1679 as non-type identifiers. */
1680 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1681 /* When parsing a type-specifier, do not try to parse a class-specifier
1682 or enum-specifier. */
1683 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1684 /* When parsing a decl-specifier-seq, only allow type-specifier or
1685 constexpr. */
1686 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1689 /* This type is used for parameters and variables which hold
1690 combinations of the above flags. */
1691 typedef int cp_parser_flags;
1693 /* The different kinds of declarators we want to parse. */
1695 typedef enum cp_parser_declarator_kind
1697 /* We want an abstract declarator. */
1698 CP_PARSER_DECLARATOR_ABSTRACT,
1699 /* We want a named declarator. */
1700 CP_PARSER_DECLARATOR_NAMED,
1701 /* We don't mind, but the name must be an unqualified-id. */
1702 CP_PARSER_DECLARATOR_EITHER
1703 } cp_parser_declarator_kind;
1705 /* The precedence values used to parse binary expressions. The minimum value
1706 of PREC must be 1, because zero is reserved to quickly discriminate
1707 binary operators from other tokens. */
1709 enum cp_parser_prec
1711 PREC_NOT_OPERATOR,
1712 PREC_LOGICAL_OR_EXPRESSION,
1713 PREC_LOGICAL_AND_EXPRESSION,
1714 PREC_INCLUSIVE_OR_EXPRESSION,
1715 PREC_EXCLUSIVE_OR_EXPRESSION,
1716 PREC_AND_EXPRESSION,
1717 PREC_EQUALITY_EXPRESSION,
1718 PREC_RELATIONAL_EXPRESSION,
1719 PREC_SHIFT_EXPRESSION,
1720 PREC_ADDITIVE_EXPRESSION,
1721 PREC_MULTIPLICATIVE_EXPRESSION,
1722 PREC_PM_EXPRESSION,
1723 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1726 /* A mapping from a token type to a corresponding tree node type, with a
1727 precedence value. */
1729 typedef struct cp_parser_binary_operations_map_node
1731 /* The token type. */
1732 enum cpp_ttype token_type;
1733 /* The corresponding tree code. */
1734 enum tree_code tree_type;
1735 /* The precedence of this operator. */
1736 enum cp_parser_prec prec;
1737 } cp_parser_binary_operations_map_node;
1739 typedef struct cp_parser_expression_stack_entry
1741 /* Left hand side of the binary operation we are currently
1742 parsing. */
1743 tree lhs;
1744 /* Original tree code for left hand side, if it was a binary
1745 expression itself (used for -Wparentheses). */
1746 enum tree_code lhs_type;
1747 /* Tree code for the binary operation we are parsing. */
1748 enum tree_code tree_type;
1749 /* Precedence of the binary operation we are parsing. */
1750 enum cp_parser_prec prec;
1751 /* Location of the binary operation we are parsing. */
1752 location_t loc;
1753 } cp_parser_expression_stack_entry;
1755 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1756 entries because precedence levels on the stack are monotonically
1757 increasing. */
1758 typedef struct cp_parser_expression_stack_entry
1759 cp_parser_expression_stack[NUM_PREC_VALUES];
1761 /* Prototypes. */
1763 /* Constructors and destructors. */
1765 static cp_parser_context *cp_parser_context_new
1766 (cp_parser_context *);
1768 /* Class variables. */
1770 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1772 /* The operator-precedence table used by cp_parser_binary_expression.
1773 Transformed into an associative array (binops_by_token) by
1774 cp_parser_new. */
1776 static const cp_parser_binary_operations_map_node binops[] = {
1777 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1778 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1780 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1781 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1782 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1784 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1785 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1787 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1788 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1790 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1791 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1792 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1793 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1795 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1796 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1798 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1800 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1802 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1804 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1806 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1809 /* The same as binops, but initialized by cp_parser_new so that
1810 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1811 for speed. */
1812 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1814 /* Constructors and destructors. */
1816 /* Construct a new context. The context below this one on the stack
1817 is given by NEXT. */
1819 static cp_parser_context *
1820 cp_parser_context_new (cp_parser_context* next)
1822 cp_parser_context *context;
1824 /* Allocate the storage. */
1825 if (cp_parser_context_free_list != NULL)
1827 /* Pull the first entry from the free list. */
1828 context = cp_parser_context_free_list;
1829 cp_parser_context_free_list = context->next;
1830 memset (context, 0, sizeof (*context));
1832 else
1833 context = ggc_cleared_alloc<cp_parser_context> ();
1835 /* No errors have occurred yet in this context. */
1836 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1837 /* If this is not the bottommost context, copy information that we
1838 need from the previous context. */
1839 if (next)
1841 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1842 expression, then we are parsing one in this context, too. */
1843 context->object_type = next->object_type;
1844 /* Thread the stack. */
1845 context->next = next;
1848 return context;
1851 /* Managing the unparsed function queues. */
1853 #define unparsed_funs_with_default_args \
1854 parser->unparsed_queues->last ().funs_with_default_args
1855 #define unparsed_funs_with_definitions \
1856 parser->unparsed_queues->last ().funs_with_definitions
1857 #define unparsed_nsdmis \
1858 parser->unparsed_queues->last ().nsdmis
1859 #define unparsed_classes \
1860 parser->unparsed_queues->last ().classes
1862 static void
1863 push_unparsed_function_queues (cp_parser *parser)
1865 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1866 vec_safe_push (parser->unparsed_queues, e);
1869 static void
1870 pop_unparsed_function_queues (cp_parser *parser)
1872 release_tree_vector (unparsed_funs_with_definitions);
1873 parser->unparsed_queues->pop ();
1876 /* Prototypes. */
1878 /* Constructors and destructors. */
1880 static cp_parser *cp_parser_new
1881 (void);
1883 /* Routines to parse various constructs.
1885 Those that return `tree' will return the error_mark_node (rather
1886 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1887 Sometimes, they will return an ordinary node if error-recovery was
1888 attempted, even though a parse error occurred. So, to check
1889 whether or not a parse error occurred, you should always use
1890 cp_parser_error_occurred. If the construct is optional (indicated
1891 either by an `_opt' in the name of the function that does the
1892 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1893 the construct is not present. */
1895 /* Lexical conventions [gram.lex] */
1897 static tree cp_parser_identifier
1898 (cp_parser *);
1899 static tree cp_parser_string_literal
1900 (cp_parser *, bool, bool, bool);
1901 static tree cp_parser_userdef_char_literal
1902 (cp_parser *);
1903 static tree cp_parser_userdef_string_literal
1904 (tree);
1905 static tree cp_parser_userdef_numeric_literal
1906 (cp_parser *);
1908 /* Basic concepts [gram.basic] */
1910 static bool cp_parser_translation_unit
1911 (cp_parser *);
1913 /* Expressions [gram.expr] */
1915 static tree cp_parser_primary_expression
1916 (cp_parser *, bool, bool, bool, cp_id_kind *);
1917 static tree cp_parser_id_expression
1918 (cp_parser *, bool, bool, bool *, bool, bool);
1919 static tree cp_parser_unqualified_id
1920 (cp_parser *, bool, bool, bool, bool);
1921 static tree cp_parser_nested_name_specifier_opt
1922 (cp_parser *, bool, bool, bool, bool);
1923 static tree cp_parser_nested_name_specifier
1924 (cp_parser *, bool, bool, bool, bool);
1925 static tree cp_parser_qualifying_entity
1926 (cp_parser *, bool, bool, bool, bool, bool);
1927 static tree cp_parser_postfix_expression
1928 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
1929 static tree cp_parser_postfix_open_square_expression
1930 (cp_parser *, tree, bool, bool);
1931 static tree cp_parser_postfix_dot_deref_expression
1932 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1933 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
1934 (cp_parser *, int, bool, bool, bool *, bool = false);
1935 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1936 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1937 static void cp_parser_pseudo_destructor_name
1938 (cp_parser *, tree, tree *, tree *);
1939 static tree cp_parser_unary_expression
1940 (cp_parser *, bool, bool, cp_id_kind *);
1941 static enum tree_code cp_parser_unary_operator
1942 (cp_token *);
1943 static tree cp_parser_new_expression
1944 (cp_parser *);
1945 static vec<tree, va_gc> *cp_parser_new_placement
1946 (cp_parser *);
1947 static tree cp_parser_new_type_id
1948 (cp_parser *, tree *);
1949 static cp_declarator *cp_parser_new_declarator_opt
1950 (cp_parser *);
1951 static cp_declarator *cp_parser_direct_new_declarator
1952 (cp_parser *);
1953 static vec<tree, va_gc> *cp_parser_new_initializer
1954 (cp_parser *);
1955 static tree cp_parser_delete_expression
1956 (cp_parser *);
1957 static tree cp_parser_cast_expression
1958 (cp_parser *, bool, bool, bool, cp_id_kind *);
1959 static tree cp_parser_binary_expression
1960 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1961 static tree cp_parser_question_colon_clause
1962 (cp_parser *, tree);
1963 static tree cp_parser_assignment_expression
1964 (cp_parser *, bool, cp_id_kind *);
1965 static enum tree_code cp_parser_assignment_operator_opt
1966 (cp_parser *);
1967 static tree cp_parser_expression
1968 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
1969 static tree cp_parser_constant_expression
1970 (cp_parser *, bool, bool *);
1971 static tree cp_parser_builtin_offsetof
1972 (cp_parser *);
1973 static tree cp_parser_lambda_expression
1974 (cp_parser *);
1975 static void cp_parser_lambda_introducer
1976 (cp_parser *, tree);
1977 static bool cp_parser_lambda_declarator_opt
1978 (cp_parser *, tree);
1979 static void cp_parser_lambda_body
1980 (cp_parser *, tree);
1982 /* Statements [gram.stmt.stmt] */
1984 static void cp_parser_statement
1985 (cp_parser *, tree, bool, bool *);
1986 static void cp_parser_label_for_labeled_statement
1987 (cp_parser *, tree);
1988 static tree cp_parser_expression_statement
1989 (cp_parser *, tree);
1990 static tree cp_parser_compound_statement
1991 (cp_parser *, tree, bool, bool);
1992 static void cp_parser_statement_seq_opt
1993 (cp_parser *, tree);
1994 static tree cp_parser_selection_statement
1995 (cp_parser *, bool *);
1996 static tree cp_parser_condition
1997 (cp_parser *);
1998 static tree cp_parser_iteration_statement
1999 (cp_parser *, bool);
2000 static bool cp_parser_for_init_statement
2001 (cp_parser *, tree *decl);
2002 static tree cp_parser_for
2003 (cp_parser *, bool);
2004 static tree cp_parser_c_for
2005 (cp_parser *, tree, tree, bool);
2006 static tree cp_parser_range_for
2007 (cp_parser *, tree, tree, tree, bool);
2008 static void do_range_for_auto_deduction
2009 (tree, tree);
2010 static tree cp_parser_perform_range_for_lookup
2011 (tree, tree *, tree *);
2012 static tree cp_parser_range_for_member_function
2013 (tree, tree);
2014 static tree cp_parser_jump_statement
2015 (cp_parser *);
2016 static void cp_parser_declaration_statement
2017 (cp_parser *);
2019 static tree cp_parser_implicitly_scoped_statement
2020 (cp_parser *, bool *);
2021 static void cp_parser_already_scoped_statement
2022 (cp_parser *);
2024 /* Declarations [gram.dcl.dcl] */
2026 static void cp_parser_declaration_seq_opt
2027 (cp_parser *);
2028 static void cp_parser_declaration
2029 (cp_parser *);
2030 static void cp_parser_block_declaration
2031 (cp_parser *, bool);
2032 static void cp_parser_simple_declaration
2033 (cp_parser *, bool, tree *);
2034 static void cp_parser_decl_specifier_seq
2035 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2036 static tree cp_parser_storage_class_specifier_opt
2037 (cp_parser *);
2038 static tree cp_parser_function_specifier_opt
2039 (cp_parser *, cp_decl_specifier_seq *);
2040 static tree cp_parser_type_specifier
2041 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2042 int *, bool *);
2043 static tree cp_parser_simple_type_specifier
2044 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2045 static tree cp_parser_type_name
2046 (cp_parser *);
2047 static tree cp_parser_nonclass_name
2048 (cp_parser* parser);
2049 static tree cp_parser_elaborated_type_specifier
2050 (cp_parser *, bool, bool);
2051 static tree cp_parser_enum_specifier
2052 (cp_parser *);
2053 static void cp_parser_enumerator_list
2054 (cp_parser *, tree);
2055 static void cp_parser_enumerator_definition
2056 (cp_parser *, tree);
2057 static tree cp_parser_namespace_name
2058 (cp_parser *);
2059 static void cp_parser_namespace_definition
2060 (cp_parser *);
2061 static void cp_parser_namespace_body
2062 (cp_parser *);
2063 static tree cp_parser_qualified_namespace_specifier
2064 (cp_parser *);
2065 static void cp_parser_namespace_alias_definition
2066 (cp_parser *);
2067 static bool cp_parser_using_declaration
2068 (cp_parser *, bool);
2069 static void cp_parser_using_directive
2070 (cp_parser *);
2071 static tree cp_parser_alias_declaration
2072 (cp_parser *);
2073 static void cp_parser_asm_definition
2074 (cp_parser *);
2075 static void cp_parser_linkage_specification
2076 (cp_parser *);
2077 static void cp_parser_static_assert
2078 (cp_parser *, bool);
2079 static tree cp_parser_decltype
2080 (cp_parser *);
2082 /* Declarators [gram.dcl.decl] */
2084 static tree cp_parser_init_declarator
2085 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *);
2086 static cp_declarator *cp_parser_declarator
2087 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2088 static cp_declarator *cp_parser_direct_declarator
2089 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2090 static enum tree_code cp_parser_ptr_operator
2091 (cp_parser *, tree *, cp_cv_quals *, tree *);
2092 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2093 (cp_parser *);
2094 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2095 (cp_parser *);
2096 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2097 (cp_parser *);
2098 static tree cp_parser_late_return_type_opt
2099 (cp_parser *, cp_declarator *, cp_cv_quals);
2100 static tree cp_parser_declarator_id
2101 (cp_parser *, bool);
2102 static tree cp_parser_type_id
2103 (cp_parser *);
2104 static tree cp_parser_template_type_arg
2105 (cp_parser *);
2106 static tree cp_parser_trailing_type_id (cp_parser *);
2107 static tree cp_parser_type_id_1
2108 (cp_parser *, bool, bool);
2109 static void cp_parser_type_specifier_seq
2110 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2111 static tree cp_parser_parameter_declaration_clause
2112 (cp_parser *);
2113 static tree cp_parser_parameter_declaration_list
2114 (cp_parser *, bool *);
2115 static cp_parameter_declarator *cp_parser_parameter_declaration
2116 (cp_parser *, bool, bool *);
2117 static tree cp_parser_default_argument
2118 (cp_parser *, bool);
2119 static void cp_parser_function_body
2120 (cp_parser *, bool);
2121 static tree cp_parser_initializer
2122 (cp_parser *, bool *, bool *);
2123 static tree cp_parser_initializer_clause
2124 (cp_parser *, bool *);
2125 static tree cp_parser_braced_list
2126 (cp_parser*, bool*);
2127 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2128 (cp_parser *, bool *);
2130 static bool cp_parser_ctor_initializer_opt_and_function_body
2131 (cp_parser *, bool);
2133 static tree cp_parser_late_parsing_omp_declare_simd
2134 (cp_parser *, tree);
2136 static tree cp_parser_late_parsing_cilk_simd_fn_info
2137 (cp_parser *, tree);
2139 static tree synthesize_implicit_template_parm
2140 (cp_parser *);
2141 static tree finish_fully_implicit_template
2142 (cp_parser *, tree);
2144 /* Classes [gram.class] */
2146 static tree cp_parser_class_name
2147 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2148 static tree cp_parser_class_specifier
2149 (cp_parser *);
2150 static tree cp_parser_class_head
2151 (cp_parser *, bool *);
2152 static enum tag_types cp_parser_class_key
2153 (cp_parser *);
2154 static void cp_parser_type_parameter_key
2155 (cp_parser* parser);
2156 static void cp_parser_member_specification_opt
2157 (cp_parser *);
2158 static void cp_parser_member_declaration
2159 (cp_parser *);
2160 static tree cp_parser_pure_specifier
2161 (cp_parser *);
2162 static tree cp_parser_constant_initializer
2163 (cp_parser *);
2165 /* Derived classes [gram.class.derived] */
2167 static tree cp_parser_base_clause
2168 (cp_parser *);
2169 static tree cp_parser_base_specifier
2170 (cp_parser *);
2172 /* Special member functions [gram.special] */
2174 static tree cp_parser_conversion_function_id
2175 (cp_parser *);
2176 static tree cp_parser_conversion_type_id
2177 (cp_parser *);
2178 static cp_declarator *cp_parser_conversion_declarator_opt
2179 (cp_parser *);
2180 static bool cp_parser_ctor_initializer_opt
2181 (cp_parser *);
2182 static void cp_parser_mem_initializer_list
2183 (cp_parser *);
2184 static tree cp_parser_mem_initializer
2185 (cp_parser *);
2186 static tree cp_parser_mem_initializer_id
2187 (cp_parser *);
2189 /* Overloading [gram.over] */
2191 static tree cp_parser_operator_function_id
2192 (cp_parser *);
2193 static tree cp_parser_operator
2194 (cp_parser *);
2196 /* Templates [gram.temp] */
2198 static void cp_parser_template_declaration
2199 (cp_parser *, bool);
2200 static tree cp_parser_template_parameter_list
2201 (cp_parser *);
2202 static tree cp_parser_template_parameter
2203 (cp_parser *, bool *, bool *);
2204 static tree cp_parser_type_parameter
2205 (cp_parser *, bool *);
2206 static tree cp_parser_template_id
2207 (cp_parser *, bool, bool, enum tag_types, bool);
2208 static tree cp_parser_template_name
2209 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2210 static tree cp_parser_template_argument_list
2211 (cp_parser *);
2212 static tree cp_parser_template_argument
2213 (cp_parser *);
2214 static void cp_parser_explicit_instantiation
2215 (cp_parser *);
2216 static void cp_parser_explicit_specialization
2217 (cp_parser *);
2219 /* Exception handling [gram.exception] */
2221 static tree cp_parser_try_block
2222 (cp_parser *);
2223 static bool cp_parser_function_try_block
2224 (cp_parser *);
2225 static void cp_parser_handler_seq
2226 (cp_parser *);
2227 static void cp_parser_handler
2228 (cp_parser *);
2229 static tree cp_parser_exception_declaration
2230 (cp_parser *);
2231 static tree cp_parser_throw_expression
2232 (cp_parser *);
2233 static tree cp_parser_exception_specification_opt
2234 (cp_parser *);
2235 static tree cp_parser_type_id_list
2236 (cp_parser *);
2238 /* GNU Extensions */
2240 static tree cp_parser_asm_specification_opt
2241 (cp_parser *);
2242 static tree cp_parser_asm_operand_list
2243 (cp_parser *);
2244 static tree cp_parser_asm_clobber_list
2245 (cp_parser *);
2246 static tree cp_parser_asm_label_list
2247 (cp_parser *);
2248 static bool cp_next_tokens_can_be_attribute_p
2249 (cp_parser *);
2250 static bool cp_next_tokens_can_be_gnu_attribute_p
2251 (cp_parser *);
2252 static bool cp_next_tokens_can_be_std_attribute_p
2253 (cp_parser *);
2254 static bool cp_nth_tokens_can_be_std_attribute_p
2255 (cp_parser *, size_t);
2256 static bool cp_nth_tokens_can_be_gnu_attribute_p
2257 (cp_parser *, size_t);
2258 static bool cp_nth_tokens_can_be_attribute_p
2259 (cp_parser *, size_t);
2260 static tree cp_parser_attributes_opt
2261 (cp_parser *);
2262 static tree cp_parser_gnu_attributes_opt
2263 (cp_parser *);
2264 static tree cp_parser_gnu_attribute_list
2265 (cp_parser *);
2266 static tree cp_parser_std_attribute
2267 (cp_parser *);
2268 static tree cp_parser_std_attribute_spec
2269 (cp_parser *);
2270 static tree cp_parser_std_attribute_spec_seq
2271 (cp_parser *);
2272 static bool cp_parser_extension_opt
2273 (cp_parser *, int *);
2274 static void cp_parser_label_declaration
2275 (cp_parser *);
2277 /* Transactional Memory Extensions */
2279 static tree cp_parser_transaction
2280 (cp_parser *, enum rid);
2281 static tree cp_parser_transaction_expression
2282 (cp_parser *, enum rid);
2283 static bool cp_parser_function_transaction
2284 (cp_parser *, enum rid);
2285 static tree cp_parser_transaction_cancel
2286 (cp_parser *);
2288 enum pragma_context {
2289 pragma_external,
2290 pragma_member,
2291 pragma_objc_icode,
2292 pragma_stmt,
2293 pragma_compound
2295 static bool cp_parser_pragma
2296 (cp_parser *, enum pragma_context);
2298 /* Objective-C++ Productions */
2300 static tree cp_parser_objc_message_receiver
2301 (cp_parser *);
2302 static tree cp_parser_objc_message_args
2303 (cp_parser *);
2304 static tree cp_parser_objc_message_expression
2305 (cp_parser *);
2306 static tree cp_parser_objc_encode_expression
2307 (cp_parser *);
2308 static tree cp_parser_objc_defs_expression
2309 (cp_parser *);
2310 static tree cp_parser_objc_protocol_expression
2311 (cp_parser *);
2312 static tree cp_parser_objc_selector_expression
2313 (cp_parser *);
2314 static tree cp_parser_objc_expression
2315 (cp_parser *);
2316 static bool cp_parser_objc_selector_p
2317 (enum cpp_ttype);
2318 static tree cp_parser_objc_selector
2319 (cp_parser *);
2320 static tree cp_parser_objc_protocol_refs_opt
2321 (cp_parser *);
2322 static void cp_parser_objc_declaration
2323 (cp_parser *, tree);
2324 static tree cp_parser_objc_statement
2325 (cp_parser *);
2326 static bool cp_parser_objc_valid_prefix_attributes
2327 (cp_parser *, tree *);
2328 static void cp_parser_objc_at_property_declaration
2329 (cp_parser *) ;
2330 static void cp_parser_objc_at_synthesize_declaration
2331 (cp_parser *) ;
2332 static void cp_parser_objc_at_dynamic_declaration
2333 (cp_parser *) ;
2334 static tree cp_parser_objc_struct_declaration
2335 (cp_parser *) ;
2337 /* Utility Routines */
2339 static tree cp_parser_lookup_name
2340 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2341 static tree cp_parser_lookup_name_simple
2342 (cp_parser *, tree, location_t);
2343 static tree cp_parser_maybe_treat_template_as_class
2344 (tree, bool);
2345 static bool cp_parser_check_declarator_template_parameters
2346 (cp_parser *, cp_declarator *, location_t);
2347 static bool cp_parser_check_template_parameters
2348 (cp_parser *, unsigned, location_t, cp_declarator *);
2349 static tree cp_parser_simple_cast_expression
2350 (cp_parser *);
2351 static tree cp_parser_global_scope_opt
2352 (cp_parser *, bool);
2353 static bool cp_parser_constructor_declarator_p
2354 (cp_parser *, bool);
2355 static tree cp_parser_function_definition_from_specifiers_and_declarator
2356 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2357 static tree cp_parser_function_definition_after_declarator
2358 (cp_parser *, bool);
2359 static void cp_parser_template_declaration_after_export
2360 (cp_parser *, bool);
2361 static void cp_parser_perform_template_parameter_access_checks
2362 (vec<deferred_access_check, va_gc> *);
2363 static tree cp_parser_single_declaration
2364 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2365 static tree cp_parser_functional_cast
2366 (cp_parser *, tree);
2367 static tree cp_parser_save_member_function_body
2368 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2369 static tree cp_parser_save_nsdmi
2370 (cp_parser *);
2371 static tree cp_parser_enclosed_template_argument_list
2372 (cp_parser *);
2373 static void cp_parser_save_default_args
2374 (cp_parser *, tree);
2375 static void cp_parser_late_parsing_for_member
2376 (cp_parser *, tree);
2377 static tree cp_parser_late_parse_one_default_arg
2378 (cp_parser *, tree, tree, tree);
2379 static void cp_parser_late_parsing_nsdmi
2380 (cp_parser *, tree);
2381 static void cp_parser_late_parsing_default_args
2382 (cp_parser *, tree);
2383 static tree cp_parser_sizeof_operand
2384 (cp_parser *, enum rid);
2385 static tree cp_parser_trait_expr
2386 (cp_parser *, enum rid);
2387 static bool cp_parser_declares_only_class_p
2388 (cp_parser *);
2389 static void cp_parser_set_storage_class
2390 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2391 static void cp_parser_set_decl_spec_type
2392 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2393 static void set_and_check_decl_spec_loc
2394 (cp_decl_specifier_seq *decl_specs,
2395 cp_decl_spec ds, cp_token *);
2396 static bool cp_parser_friend_p
2397 (const cp_decl_specifier_seq *);
2398 static void cp_parser_required_error
2399 (cp_parser *, required_token, bool);
2400 static cp_token *cp_parser_require
2401 (cp_parser *, enum cpp_ttype, required_token);
2402 static cp_token *cp_parser_require_keyword
2403 (cp_parser *, enum rid, required_token);
2404 static bool cp_parser_token_starts_function_definition_p
2405 (cp_token *);
2406 static bool cp_parser_next_token_starts_class_definition_p
2407 (cp_parser *);
2408 static bool cp_parser_next_token_ends_template_argument_p
2409 (cp_parser *);
2410 static bool cp_parser_nth_token_starts_template_argument_list_p
2411 (cp_parser *, size_t);
2412 static enum tag_types cp_parser_token_is_class_key
2413 (cp_token *);
2414 static enum tag_types cp_parser_token_is_type_parameter_key
2415 (cp_token *);
2416 static void cp_parser_check_class_key
2417 (enum tag_types, tree type);
2418 static void cp_parser_check_access_in_redeclaration
2419 (tree type, location_t location);
2420 static bool cp_parser_optional_template_keyword
2421 (cp_parser *);
2422 static void cp_parser_pre_parsed_nested_name_specifier
2423 (cp_parser *);
2424 static bool cp_parser_cache_group
2425 (cp_parser *, enum cpp_ttype, unsigned);
2426 static tree cp_parser_cache_defarg
2427 (cp_parser *parser, bool nsdmi);
2428 static void cp_parser_parse_tentatively
2429 (cp_parser *);
2430 static void cp_parser_commit_to_tentative_parse
2431 (cp_parser *);
2432 static void cp_parser_commit_to_topmost_tentative_parse
2433 (cp_parser *);
2434 static void cp_parser_abort_tentative_parse
2435 (cp_parser *);
2436 static bool cp_parser_parse_definitely
2437 (cp_parser *);
2438 static inline bool cp_parser_parsing_tentatively
2439 (cp_parser *);
2440 static bool cp_parser_uncommitted_to_tentative_parse_p
2441 (cp_parser *);
2442 static void cp_parser_error
2443 (cp_parser *, const char *);
2444 static void cp_parser_name_lookup_error
2445 (cp_parser *, tree, tree, name_lookup_error, location_t);
2446 static bool cp_parser_simulate_error
2447 (cp_parser *);
2448 static bool cp_parser_check_type_definition
2449 (cp_parser *);
2450 static void cp_parser_check_for_definition_in_return_type
2451 (cp_declarator *, tree, location_t type_location);
2452 static void cp_parser_check_for_invalid_template_id
2453 (cp_parser *, tree, enum tag_types, location_t location);
2454 static bool cp_parser_non_integral_constant_expression
2455 (cp_parser *, non_integral_constant);
2456 static void cp_parser_diagnose_invalid_type_name
2457 (cp_parser *, tree, location_t);
2458 static bool cp_parser_parse_and_diagnose_invalid_type_name
2459 (cp_parser *);
2460 static int cp_parser_skip_to_closing_parenthesis
2461 (cp_parser *, bool, bool, bool);
2462 static void cp_parser_skip_to_end_of_statement
2463 (cp_parser *);
2464 static void cp_parser_consume_semicolon_at_end_of_statement
2465 (cp_parser *);
2466 static void cp_parser_skip_to_end_of_block_or_statement
2467 (cp_parser *);
2468 static bool cp_parser_skip_to_closing_brace
2469 (cp_parser *);
2470 static void cp_parser_skip_to_end_of_template_parameter_list
2471 (cp_parser *);
2472 static void cp_parser_skip_to_pragma_eol
2473 (cp_parser*, cp_token *);
2474 static bool cp_parser_error_occurred
2475 (cp_parser *);
2476 static bool cp_parser_allow_gnu_extensions_p
2477 (cp_parser *);
2478 static bool cp_parser_is_pure_string_literal
2479 (cp_token *);
2480 static bool cp_parser_is_string_literal
2481 (cp_token *);
2482 static bool cp_parser_is_keyword
2483 (cp_token *, enum rid);
2484 static tree cp_parser_make_typename_type
2485 (cp_parser *, tree, location_t location);
2486 static cp_declarator * cp_parser_make_indirect_declarator
2487 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2488 static bool cp_parser_compound_literal_p
2489 (cp_parser *);
2491 /* Returns nonzero if we are parsing tentatively. */
2493 static inline bool
2494 cp_parser_parsing_tentatively (cp_parser* parser)
2496 return parser->context->next != NULL;
2499 /* Returns nonzero if TOKEN is a string literal. */
2501 static bool
2502 cp_parser_is_pure_string_literal (cp_token* token)
2504 return (token->type == CPP_STRING ||
2505 token->type == CPP_STRING16 ||
2506 token->type == CPP_STRING32 ||
2507 token->type == CPP_WSTRING ||
2508 token->type == CPP_UTF8STRING);
2511 /* Returns nonzero if TOKEN is a string literal
2512 of a user-defined string literal. */
2514 static bool
2515 cp_parser_is_string_literal (cp_token* token)
2517 return (cp_parser_is_pure_string_literal (token) ||
2518 token->type == CPP_STRING_USERDEF ||
2519 token->type == CPP_STRING16_USERDEF ||
2520 token->type == CPP_STRING32_USERDEF ||
2521 token->type == CPP_WSTRING_USERDEF ||
2522 token->type == CPP_UTF8STRING_USERDEF);
2525 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2527 static bool
2528 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2530 return token->keyword == keyword;
2533 /* If not parsing tentatively, issue a diagnostic of the form
2534 FILE:LINE: MESSAGE before TOKEN
2535 where TOKEN is the next token in the input stream. MESSAGE
2536 (specified by the caller) is usually of the form "expected
2537 OTHER-TOKEN". */
2539 static void
2540 cp_parser_error (cp_parser* parser, const char* gmsgid)
2542 if (!cp_parser_simulate_error (parser))
2544 cp_token *token = cp_lexer_peek_token (parser->lexer);
2545 /* This diagnostic makes more sense if it is tagged to the line
2546 of the token we just peeked at. */
2547 cp_lexer_set_source_position_from_token (token);
2549 if (token->type == CPP_PRAGMA)
2551 error_at (token->location,
2552 "%<#pragma%> is not allowed here");
2553 cp_parser_skip_to_pragma_eol (parser, token);
2554 return;
2557 c_parse_error (gmsgid,
2558 /* Because c_parser_error does not understand
2559 CPP_KEYWORD, keywords are treated like
2560 identifiers. */
2561 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2562 token->u.value, token->flags);
2566 /* Issue an error about name-lookup failing. NAME is the
2567 IDENTIFIER_NODE DECL is the result of
2568 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2569 the thing that we hoped to find. */
2571 static void
2572 cp_parser_name_lookup_error (cp_parser* parser,
2573 tree name,
2574 tree decl,
2575 name_lookup_error desired,
2576 location_t location)
2578 /* If name lookup completely failed, tell the user that NAME was not
2579 declared. */
2580 if (decl == error_mark_node)
2582 if (parser->scope && parser->scope != global_namespace)
2583 error_at (location, "%<%E::%E%> has not been declared",
2584 parser->scope, name);
2585 else if (parser->scope == global_namespace)
2586 error_at (location, "%<::%E%> has not been declared", name);
2587 else if (parser->object_scope
2588 && !CLASS_TYPE_P (parser->object_scope))
2589 error_at (location, "request for member %qE in non-class type %qT",
2590 name, parser->object_scope);
2591 else if (parser->object_scope)
2592 error_at (location, "%<%T::%E%> has not been declared",
2593 parser->object_scope, name);
2594 else
2595 error_at (location, "%qE has not been declared", name);
2597 else if (parser->scope && parser->scope != global_namespace)
2599 switch (desired)
2601 case NLE_TYPE:
2602 error_at (location, "%<%E::%E%> is not a type",
2603 parser->scope, name);
2604 break;
2605 case NLE_CXX98:
2606 error_at (location, "%<%E::%E%> is not a class or namespace",
2607 parser->scope, name);
2608 break;
2609 case NLE_NOT_CXX98:
2610 error_at (location,
2611 "%<%E::%E%> is not a class, namespace, or enumeration",
2612 parser->scope, name);
2613 break;
2614 default:
2615 gcc_unreachable ();
2619 else if (parser->scope == global_namespace)
2621 switch (desired)
2623 case NLE_TYPE:
2624 error_at (location, "%<::%E%> is not a type", name);
2625 break;
2626 case NLE_CXX98:
2627 error_at (location, "%<::%E%> is not a class or namespace", name);
2628 break;
2629 case NLE_NOT_CXX98:
2630 error_at (location,
2631 "%<::%E%> is not a class, namespace, or enumeration",
2632 name);
2633 break;
2634 default:
2635 gcc_unreachable ();
2638 else
2640 switch (desired)
2642 case NLE_TYPE:
2643 error_at (location, "%qE is not a type", name);
2644 break;
2645 case NLE_CXX98:
2646 error_at (location, "%qE is not a class or namespace", name);
2647 break;
2648 case NLE_NOT_CXX98:
2649 error_at (location,
2650 "%qE is not a class, namespace, or enumeration", name);
2651 break;
2652 default:
2653 gcc_unreachable ();
2658 /* If we are parsing tentatively, remember that an error has occurred
2659 during this tentative parse. Returns true if the error was
2660 simulated; false if a message should be issued by the caller. */
2662 static bool
2663 cp_parser_simulate_error (cp_parser* parser)
2665 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2667 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2668 return true;
2670 return false;
2673 /* This function is called when a type is defined. If type
2674 definitions are forbidden at this point, an error message is
2675 issued. */
2677 static bool
2678 cp_parser_check_type_definition (cp_parser* parser)
2680 /* If types are forbidden here, issue a message. */
2681 if (parser->type_definition_forbidden_message)
2683 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2684 in the message need to be interpreted. */
2685 error (parser->type_definition_forbidden_message);
2686 return false;
2688 return true;
2691 /* This function is called when the DECLARATOR is processed. The TYPE
2692 was a type defined in the decl-specifiers. If it is invalid to
2693 define a type in the decl-specifiers for DECLARATOR, an error is
2694 issued. TYPE_LOCATION is the location of TYPE and is used
2695 for error reporting. */
2697 static void
2698 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2699 tree type, location_t type_location)
2701 /* [dcl.fct] forbids type definitions in return types.
2702 Unfortunately, it's not easy to know whether or not we are
2703 processing a return type until after the fact. */
2704 while (declarator
2705 && (declarator->kind == cdk_pointer
2706 || declarator->kind == cdk_reference
2707 || declarator->kind == cdk_ptrmem))
2708 declarator = declarator->declarator;
2709 if (declarator
2710 && declarator->kind == cdk_function)
2712 error_at (type_location,
2713 "new types may not be defined in a return type");
2714 inform (type_location,
2715 "(perhaps a semicolon is missing after the definition of %qT)",
2716 type);
2720 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2721 "<" in any valid C++ program. If the next token is indeed "<",
2722 issue a message warning the user about what appears to be an
2723 invalid attempt to form a template-id. LOCATION is the location
2724 of the type-specifier (TYPE) */
2726 static void
2727 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2728 tree type,
2729 enum tag_types tag_type,
2730 location_t location)
2732 cp_token_position start = 0;
2734 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2736 if (TYPE_P (type))
2737 error_at (location, "%qT is not a template", type);
2738 else if (identifier_p (type))
2740 if (tag_type != none_type)
2741 error_at (location, "%qE is not a class template", type);
2742 else
2743 error_at (location, "%qE is not a template", type);
2745 else
2746 error_at (location, "invalid template-id");
2747 /* Remember the location of the invalid "<". */
2748 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2749 start = cp_lexer_token_position (parser->lexer, true);
2750 /* Consume the "<". */
2751 cp_lexer_consume_token (parser->lexer);
2752 /* Parse the template arguments. */
2753 cp_parser_enclosed_template_argument_list (parser);
2754 /* Permanently remove the invalid template arguments so that
2755 this error message is not issued again. */
2756 if (start)
2757 cp_lexer_purge_tokens_after (parser->lexer, start);
2761 /* If parsing an integral constant-expression, issue an error message
2762 about the fact that THING appeared and return true. Otherwise,
2763 return false. In either case, set
2764 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2766 static bool
2767 cp_parser_non_integral_constant_expression (cp_parser *parser,
2768 non_integral_constant thing)
2770 parser->non_integral_constant_expression_p = true;
2771 if (parser->integral_constant_expression_p)
2773 if (!parser->allow_non_integral_constant_expression_p)
2775 const char *msg = NULL;
2776 switch (thing)
2778 case NIC_FLOAT:
2779 error ("floating-point literal "
2780 "cannot appear in a constant-expression");
2781 return true;
2782 case NIC_CAST:
2783 error ("a cast to a type other than an integral or "
2784 "enumeration type cannot appear in a "
2785 "constant-expression");
2786 return true;
2787 case NIC_TYPEID:
2788 error ("%<typeid%> operator "
2789 "cannot appear in a constant-expression");
2790 return true;
2791 case NIC_NCC:
2792 error ("non-constant compound literals "
2793 "cannot appear in a constant-expression");
2794 return true;
2795 case NIC_FUNC_CALL:
2796 error ("a function call "
2797 "cannot appear in a constant-expression");
2798 return true;
2799 case NIC_INC:
2800 error ("an increment "
2801 "cannot appear in a constant-expression");
2802 return true;
2803 case NIC_DEC:
2804 error ("an decrement "
2805 "cannot appear in a constant-expression");
2806 return true;
2807 case NIC_ARRAY_REF:
2808 error ("an array reference "
2809 "cannot appear in a constant-expression");
2810 return true;
2811 case NIC_ADDR_LABEL:
2812 error ("the address of a label "
2813 "cannot appear in a constant-expression");
2814 return true;
2815 case NIC_OVERLOADED:
2816 error ("calls to overloaded operators "
2817 "cannot appear in a constant-expression");
2818 return true;
2819 case NIC_ASSIGNMENT:
2820 error ("an assignment cannot appear in a constant-expression");
2821 return true;
2822 case NIC_COMMA:
2823 error ("a comma operator "
2824 "cannot appear in a constant-expression");
2825 return true;
2826 case NIC_CONSTRUCTOR:
2827 error ("a call to a constructor "
2828 "cannot appear in a constant-expression");
2829 return true;
2830 case NIC_TRANSACTION:
2831 error ("a transaction expression "
2832 "cannot appear in a constant-expression");
2833 return true;
2834 case NIC_THIS:
2835 msg = "this";
2836 break;
2837 case NIC_FUNC_NAME:
2838 msg = "__FUNCTION__";
2839 break;
2840 case NIC_PRETTY_FUNC:
2841 msg = "__PRETTY_FUNCTION__";
2842 break;
2843 case NIC_C99_FUNC:
2844 msg = "__func__";
2845 break;
2846 case NIC_VA_ARG:
2847 msg = "va_arg";
2848 break;
2849 case NIC_ARROW:
2850 msg = "->";
2851 break;
2852 case NIC_POINT:
2853 msg = ".";
2854 break;
2855 case NIC_STAR:
2856 msg = "*";
2857 break;
2858 case NIC_ADDR:
2859 msg = "&";
2860 break;
2861 case NIC_PREINCREMENT:
2862 msg = "++";
2863 break;
2864 case NIC_PREDECREMENT:
2865 msg = "--";
2866 break;
2867 case NIC_NEW:
2868 msg = "new";
2869 break;
2870 case NIC_DEL:
2871 msg = "delete";
2872 break;
2873 default:
2874 gcc_unreachable ();
2876 if (msg)
2877 error ("%qs cannot appear in a constant-expression", msg);
2878 return true;
2881 return false;
2884 /* Emit a diagnostic for an invalid type name. This function commits
2885 to the current active tentative parse, if any. (Otherwise, the
2886 problematic construct might be encountered again later, resulting
2887 in duplicate error messages.) LOCATION is the location of ID. */
2889 static void
2890 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
2891 location_t location)
2893 tree decl, ambiguous_decls;
2894 cp_parser_commit_to_tentative_parse (parser);
2895 /* Try to lookup the identifier. */
2896 decl = cp_parser_lookup_name (parser, id, none_type,
2897 /*is_template=*/false,
2898 /*is_namespace=*/false,
2899 /*check_dependency=*/true,
2900 &ambiguous_decls, location);
2901 if (ambiguous_decls)
2902 /* If the lookup was ambiguous, an error will already have
2903 been issued. */
2904 return;
2905 /* If the lookup found a template-name, it means that the user forgot
2906 to specify an argument list. Emit a useful error message. */
2907 if (TREE_CODE (decl) == TEMPLATE_DECL)
2908 error_at (location,
2909 "invalid use of template-name %qE without an argument list",
2910 decl);
2911 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2912 error_at (location, "invalid use of destructor %qD as a type", id);
2913 else if (TREE_CODE (decl) == TYPE_DECL)
2914 /* Something like 'unsigned A a;' */
2915 error_at (location, "invalid combination of multiple type-specifiers");
2916 else if (!parser->scope)
2918 /* Issue an error message. */
2919 error_at (location, "%qE does not name a type", id);
2920 /* If we're in a template class, it's possible that the user was
2921 referring to a type from a base class. For example:
2923 template <typename T> struct A { typedef T X; };
2924 template <typename T> struct B : public A<T> { X x; };
2926 The user should have said "typename A<T>::X". */
2927 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
2928 inform (location, "C++11 %<constexpr%> only available with "
2929 "-std=c++11 or -std=gnu++11");
2930 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
2931 inform (location, "C++11 %<noexcept%> only available with "
2932 "-std=c++11 or -std=gnu++11");
2933 else if (cxx_dialect < cxx11
2934 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
2935 inform (location, "C++11 %<thread_local%> only available with "
2936 "-std=c++11 or -std=gnu++11");
2937 else if (processing_template_decl && current_class_type
2938 && TYPE_BINFO (current_class_type))
2940 tree b;
2942 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2944 b = TREE_CHAIN (b))
2946 tree base_type = BINFO_TYPE (b);
2947 if (CLASS_TYPE_P (base_type)
2948 && dependent_type_p (base_type))
2950 tree field;
2951 /* Go from a particular instantiation of the
2952 template (which will have an empty TYPE_FIELDs),
2953 to the main version. */
2954 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2955 for (field = TYPE_FIELDS (base_type);
2956 field;
2957 field = DECL_CHAIN (field))
2958 if (TREE_CODE (field) == TYPE_DECL
2959 && DECL_NAME (field) == id)
2961 inform (location,
2962 "(perhaps %<typename %T::%E%> was intended)",
2963 BINFO_TYPE (b), id);
2964 break;
2966 if (field)
2967 break;
2972 /* Here we diagnose qualified-ids where the scope is actually correct,
2973 but the identifier does not resolve to a valid type name. */
2974 else if (parser->scope != error_mark_node)
2976 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2978 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2979 error_at (location_of (id),
2980 "%qE in namespace %qE does not name a template type",
2981 id, parser->scope);
2982 else
2983 error_at (location_of (id),
2984 "%qE in namespace %qE does not name a type",
2985 id, parser->scope);
2987 else if (CLASS_TYPE_P (parser->scope)
2988 && constructor_name_p (id, parser->scope))
2990 /* A<T>::A<T>() */
2991 error_at (location, "%<%T::%E%> names the constructor, not"
2992 " the type", parser->scope, id);
2993 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2994 error_at (location, "and %qT has no template constructors",
2995 parser->scope);
2997 else if (TYPE_P (parser->scope)
2998 && dependent_scope_p (parser->scope))
2999 error_at (location, "need %<typename%> before %<%T::%E%> because "
3000 "%qT is a dependent scope",
3001 parser->scope, id, parser->scope);
3002 else if (TYPE_P (parser->scope))
3004 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3005 error_at (location_of (id),
3006 "%qE in %q#T does not name a template type",
3007 id, parser->scope);
3008 else
3009 error_at (location_of (id),
3010 "%qE in %q#T does not name a type",
3011 id, parser->scope);
3013 else
3014 gcc_unreachable ();
3018 /* Check for a common situation where a type-name should be present,
3019 but is not, and issue a sensible error message. Returns true if an
3020 invalid type-name was detected.
3022 The situation handled by this function are variable declarations of the
3023 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3024 Usually, `ID' should name a type, but if we got here it means that it
3025 does not. We try to emit the best possible error message depending on
3026 how exactly the id-expression looks like. */
3028 static bool
3029 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3031 tree id;
3032 cp_token *token = cp_lexer_peek_token (parser->lexer);
3034 /* Avoid duplicate error about ambiguous lookup. */
3035 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3037 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3038 if (next->type == CPP_NAME && next->error_reported)
3039 goto out;
3042 cp_parser_parse_tentatively (parser);
3043 id = cp_parser_id_expression (parser,
3044 /*template_keyword_p=*/false,
3045 /*check_dependency_p=*/true,
3046 /*template_p=*/NULL,
3047 /*declarator_p=*/true,
3048 /*optional_p=*/false);
3049 /* If the next token is a (, this is a function with no explicit return
3050 type, i.e. constructor, destructor or conversion op. */
3051 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3052 || TREE_CODE (id) == TYPE_DECL)
3054 cp_parser_abort_tentative_parse (parser);
3055 return false;
3057 if (!cp_parser_parse_definitely (parser))
3058 return false;
3060 /* Emit a diagnostic for the invalid type. */
3061 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3062 out:
3063 /* If we aren't in the middle of a declarator (i.e. in a
3064 parameter-declaration-clause), skip to the end of the declaration;
3065 there's no point in trying to process it. */
3066 if (!parser->in_declarator_p)
3067 cp_parser_skip_to_end_of_block_or_statement (parser);
3068 return true;
3071 /* Consume tokens up to, and including, the next non-nested closing `)'.
3072 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3073 are doing error recovery. Returns -1 if OR_COMMA is true and we
3074 found an unnested comma. */
3076 static int
3077 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3078 bool recovering,
3079 bool or_comma,
3080 bool consume_paren)
3082 unsigned paren_depth = 0;
3083 unsigned brace_depth = 0;
3084 unsigned square_depth = 0;
3086 if (recovering && !or_comma
3087 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3088 return 0;
3090 while (true)
3092 cp_token * token = cp_lexer_peek_token (parser->lexer);
3094 switch (token->type)
3096 case CPP_EOF:
3097 case CPP_PRAGMA_EOL:
3098 /* If we've run out of tokens, then there is no closing `)'. */
3099 return 0;
3101 /* This is good for lambda expression capture-lists. */
3102 case CPP_OPEN_SQUARE:
3103 ++square_depth;
3104 break;
3105 case CPP_CLOSE_SQUARE:
3106 if (!square_depth--)
3107 return 0;
3108 break;
3110 case CPP_SEMICOLON:
3111 /* This matches the processing in skip_to_end_of_statement. */
3112 if (!brace_depth)
3113 return 0;
3114 break;
3116 case CPP_OPEN_BRACE:
3117 ++brace_depth;
3118 break;
3119 case CPP_CLOSE_BRACE:
3120 if (!brace_depth--)
3121 return 0;
3122 break;
3124 case CPP_COMMA:
3125 if (recovering && or_comma && !brace_depth && !paren_depth
3126 && !square_depth)
3127 return -1;
3128 break;
3130 case CPP_OPEN_PAREN:
3131 if (!brace_depth)
3132 ++paren_depth;
3133 break;
3135 case CPP_CLOSE_PAREN:
3136 if (!brace_depth && !paren_depth--)
3138 if (consume_paren)
3139 cp_lexer_consume_token (parser->lexer);
3140 return 1;
3142 break;
3144 default:
3145 break;
3148 /* Consume the token. */
3149 cp_lexer_consume_token (parser->lexer);
3153 /* Consume tokens until we reach the end of the current statement.
3154 Normally, that will be just before consuming a `;'. However, if a
3155 non-nested `}' comes first, then we stop before consuming that. */
3157 static void
3158 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3160 unsigned nesting_depth = 0;
3162 /* Unwind generic function template scope if necessary. */
3163 if (parser->fully_implicit_function_template_p)
3164 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3166 while (true)
3168 cp_token *token = cp_lexer_peek_token (parser->lexer);
3170 switch (token->type)
3172 case CPP_EOF:
3173 case CPP_PRAGMA_EOL:
3174 /* If we've run out of tokens, stop. */
3175 return;
3177 case CPP_SEMICOLON:
3178 /* If the next token is a `;', we have reached the end of the
3179 statement. */
3180 if (!nesting_depth)
3181 return;
3182 break;
3184 case CPP_CLOSE_BRACE:
3185 /* If this is a non-nested '}', stop before consuming it.
3186 That way, when confronted with something like:
3188 { 3 + }
3190 we stop before consuming the closing '}', even though we
3191 have not yet reached a `;'. */
3192 if (nesting_depth == 0)
3193 return;
3195 /* If it is the closing '}' for a block that we have
3196 scanned, stop -- but only after consuming the token.
3197 That way given:
3199 void f g () { ... }
3200 typedef int I;
3202 we will stop after the body of the erroneously declared
3203 function, but before consuming the following `typedef'
3204 declaration. */
3205 if (--nesting_depth == 0)
3207 cp_lexer_consume_token (parser->lexer);
3208 return;
3211 case CPP_OPEN_BRACE:
3212 ++nesting_depth;
3213 break;
3215 default:
3216 break;
3219 /* Consume the token. */
3220 cp_lexer_consume_token (parser->lexer);
3224 /* This function is called at the end of a statement or declaration.
3225 If the next token is a semicolon, it is consumed; otherwise, error
3226 recovery is attempted. */
3228 static void
3229 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3231 /* Look for the trailing `;'. */
3232 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3234 /* If there is additional (erroneous) input, skip to the end of
3235 the statement. */
3236 cp_parser_skip_to_end_of_statement (parser);
3237 /* If the next token is now a `;', consume it. */
3238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3239 cp_lexer_consume_token (parser->lexer);
3243 /* Skip tokens until we have consumed an entire block, or until we
3244 have consumed a non-nested `;'. */
3246 static void
3247 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3249 int nesting_depth = 0;
3251 /* Unwind generic function template scope if necessary. */
3252 if (parser->fully_implicit_function_template_p)
3253 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3255 while (nesting_depth >= 0)
3257 cp_token *token = cp_lexer_peek_token (parser->lexer);
3259 switch (token->type)
3261 case CPP_EOF:
3262 case CPP_PRAGMA_EOL:
3263 /* If we've run out of tokens, stop. */
3264 return;
3266 case CPP_SEMICOLON:
3267 /* Stop if this is an unnested ';'. */
3268 if (!nesting_depth)
3269 nesting_depth = -1;
3270 break;
3272 case CPP_CLOSE_BRACE:
3273 /* Stop if this is an unnested '}', or closes the outermost
3274 nesting level. */
3275 nesting_depth--;
3276 if (nesting_depth < 0)
3277 return;
3278 if (!nesting_depth)
3279 nesting_depth = -1;
3280 break;
3282 case CPP_OPEN_BRACE:
3283 /* Nest. */
3284 nesting_depth++;
3285 break;
3287 default:
3288 break;
3291 /* Consume the token. */
3292 cp_lexer_consume_token (parser->lexer);
3296 /* Skip tokens until a non-nested closing curly brace is the next
3297 token, or there are no more tokens. Return true in the first case,
3298 false otherwise. */
3300 static bool
3301 cp_parser_skip_to_closing_brace (cp_parser *parser)
3303 unsigned nesting_depth = 0;
3305 while (true)
3307 cp_token *token = cp_lexer_peek_token (parser->lexer);
3309 switch (token->type)
3311 case CPP_EOF:
3312 case CPP_PRAGMA_EOL:
3313 /* If we've run out of tokens, stop. */
3314 return false;
3316 case CPP_CLOSE_BRACE:
3317 /* If the next token is a non-nested `}', then we have reached
3318 the end of the current block. */
3319 if (nesting_depth-- == 0)
3320 return true;
3321 break;
3323 case CPP_OPEN_BRACE:
3324 /* If it the next token is a `{', then we are entering a new
3325 block. Consume the entire block. */
3326 ++nesting_depth;
3327 break;
3329 default:
3330 break;
3333 /* Consume the token. */
3334 cp_lexer_consume_token (parser->lexer);
3338 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3339 parameter is the PRAGMA token, allowing us to purge the entire pragma
3340 sequence. */
3342 static void
3343 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3345 cp_token *token;
3347 parser->lexer->in_pragma = false;
3350 token = cp_lexer_consume_token (parser->lexer);
3351 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3353 /* Ensure that the pragma is not parsed again. */
3354 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3357 /* Require pragma end of line, resyncing with it as necessary. The
3358 arguments are as for cp_parser_skip_to_pragma_eol. */
3360 static void
3361 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3363 parser->lexer->in_pragma = false;
3364 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3368 /* This is a simple wrapper around make_typename_type. When the id is
3369 an unresolved identifier node, we can provide a superior diagnostic
3370 using cp_parser_diagnose_invalid_type_name. */
3372 static tree
3373 cp_parser_make_typename_type (cp_parser *parser, tree id,
3374 location_t id_location)
3376 tree result;
3377 if (identifier_p (id))
3379 result = make_typename_type (parser->scope, id, typename_type,
3380 /*complain=*/tf_none);
3381 if (result == error_mark_node)
3382 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3383 return result;
3385 return make_typename_type (parser->scope, id, typename_type, tf_error);
3388 /* This is a wrapper around the
3389 make_{pointer,ptrmem,reference}_declarator functions that decides
3390 which one to call based on the CODE and CLASS_TYPE arguments. The
3391 CODE argument should be one of the values returned by
3392 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3393 appertain to the pointer or reference. */
3395 static cp_declarator *
3396 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3397 cp_cv_quals cv_qualifiers,
3398 cp_declarator *target,
3399 tree attributes)
3401 if (code == ERROR_MARK)
3402 return cp_error_declarator;
3404 if (code == INDIRECT_REF)
3405 if (class_type == NULL_TREE)
3406 return make_pointer_declarator (cv_qualifiers, target, attributes);
3407 else
3408 return make_ptrmem_declarator (cv_qualifiers, class_type,
3409 target, attributes);
3410 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3411 return make_reference_declarator (cv_qualifiers, target,
3412 false, attributes);
3413 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3414 return make_reference_declarator (cv_qualifiers, target,
3415 true, attributes);
3416 gcc_unreachable ();
3419 /* Create a new C++ parser. */
3421 static cp_parser *
3422 cp_parser_new (void)
3424 cp_parser *parser;
3425 cp_lexer *lexer;
3426 unsigned i;
3428 /* cp_lexer_new_main is called before doing GC allocation because
3429 cp_lexer_new_main might load a PCH file. */
3430 lexer = cp_lexer_new_main ();
3432 /* Initialize the binops_by_token so that we can get the tree
3433 directly from the token. */
3434 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3435 binops_by_token[binops[i].token_type] = binops[i];
3437 parser = ggc_cleared_alloc<cp_parser> ();
3438 parser->lexer = lexer;
3439 parser->context = cp_parser_context_new (NULL);
3441 /* For now, we always accept GNU extensions. */
3442 parser->allow_gnu_extensions_p = 1;
3444 /* The `>' token is a greater-than operator, not the end of a
3445 template-id. */
3446 parser->greater_than_is_operator_p = true;
3448 parser->default_arg_ok_p = true;
3450 /* We are not parsing a constant-expression. */
3451 parser->integral_constant_expression_p = false;
3452 parser->allow_non_integral_constant_expression_p = false;
3453 parser->non_integral_constant_expression_p = false;
3455 /* Local variable names are not forbidden. */
3456 parser->local_variables_forbidden_p = false;
3458 /* We are not processing an `extern "C"' declaration. */
3459 parser->in_unbraced_linkage_specification_p = false;
3461 /* We are not processing a declarator. */
3462 parser->in_declarator_p = false;
3464 /* We are not processing a template-argument-list. */
3465 parser->in_template_argument_list_p = false;
3467 /* We are not in an iteration statement. */
3468 parser->in_statement = 0;
3470 /* We are not in a switch statement. */
3471 parser->in_switch_statement_p = false;
3473 /* We are not parsing a type-id inside an expression. */
3474 parser->in_type_id_in_expr_p = false;
3476 /* Declarations aren't implicitly extern "C". */
3477 parser->implicit_extern_c = false;
3479 /* String literals should be translated to the execution character set. */
3480 parser->translate_strings_p = true;
3482 /* We are not parsing a function body. */
3483 parser->in_function_body = false;
3485 /* We can correct until told otherwise. */
3486 parser->colon_corrects_to_scope_p = true;
3488 /* The unparsed function queue is empty. */
3489 push_unparsed_function_queues (parser);
3491 /* There are no classes being defined. */
3492 parser->num_classes_being_defined = 0;
3494 /* No template parameters apply. */
3495 parser->num_template_parameter_lists = 0;
3497 /* Not declaring an implicit function template. */
3498 parser->auto_is_implicit_function_template_parm_p = false;
3499 parser->fully_implicit_function_template_p = false;
3500 parser->implicit_template_parms = 0;
3501 parser->implicit_template_scope = 0;
3503 return parser;
3506 /* Create a cp_lexer structure which will emit the tokens in CACHE
3507 and push it onto the parser's lexer stack. This is used for delayed
3508 parsing of in-class method bodies and default arguments, and should
3509 not be confused with tentative parsing. */
3510 static void
3511 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3513 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3514 lexer->next = parser->lexer;
3515 parser->lexer = lexer;
3517 /* Move the current source position to that of the first token in the
3518 new lexer. */
3519 cp_lexer_set_source_position_from_token (lexer->next_token);
3522 /* Pop the top lexer off the parser stack. This is never used for the
3523 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3524 static void
3525 cp_parser_pop_lexer (cp_parser *parser)
3527 cp_lexer *lexer = parser->lexer;
3528 parser->lexer = lexer->next;
3529 cp_lexer_destroy (lexer);
3531 /* Put the current source position back where it was before this
3532 lexer was pushed. */
3533 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3536 /* Lexical conventions [gram.lex] */
3538 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3539 identifier. */
3541 static tree
3542 cp_parser_identifier (cp_parser* parser)
3544 cp_token *token;
3546 /* Look for the identifier. */
3547 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3548 /* Return the value. */
3549 return token ? token->u.value : error_mark_node;
3552 /* Parse a sequence of adjacent string constants. Returns a
3553 TREE_STRING representing the combined, nul-terminated string
3554 constant. If TRANSLATE is true, translate the string to the
3555 execution character set. If WIDE_OK is true, a wide string is
3556 invalid here.
3558 C++98 [lex.string] says that if a narrow string literal token is
3559 adjacent to a wide string literal token, the behavior is undefined.
3560 However, C99 6.4.5p4 says that this results in a wide string literal.
3561 We follow C99 here, for consistency with the C front end.
3563 This code is largely lifted from lex_string() in c-lex.c.
3565 FUTURE: ObjC++ will need to handle @-strings here. */
3566 static tree
3567 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3568 bool lookup_udlit = true)
3570 tree value;
3571 size_t count;
3572 struct obstack str_ob;
3573 cpp_string str, istr, *strs;
3574 cp_token *tok;
3575 enum cpp_ttype type, curr_type;
3576 int have_suffix_p = 0;
3577 tree string_tree;
3578 tree suffix_id = NULL_TREE;
3579 bool curr_tok_is_userdef_p = false;
3581 tok = cp_lexer_peek_token (parser->lexer);
3582 if (!cp_parser_is_string_literal (tok))
3584 cp_parser_error (parser, "expected string-literal");
3585 return error_mark_node;
3588 if (cpp_userdef_string_p (tok->type))
3590 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3591 curr_type = cpp_userdef_string_remove_type (tok->type);
3592 curr_tok_is_userdef_p = true;
3594 else
3596 string_tree = tok->u.value;
3597 curr_type = tok->type;
3599 type = curr_type;
3601 /* Try to avoid the overhead of creating and destroying an obstack
3602 for the common case of just one string. */
3603 if (!cp_parser_is_string_literal
3604 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3606 cp_lexer_consume_token (parser->lexer);
3608 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3609 str.len = TREE_STRING_LENGTH (string_tree);
3610 count = 1;
3612 if (curr_tok_is_userdef_p)
3614 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3615 have_suffix_p = 1;
3616 curr_type = cpp_userdef_string_remove_type (tok->type);
3618 else
3619 curr_type = tok->type;
3621 strs = &str;
3623 else
3625 gcc_obstack_init (&str_ob);
3626 count = 0;
3630 cp_lexer_consume_token (parser->lexer);
3631 count++;
3632 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3633 str.len = TREE_STRING_LENGTH (string_tree);
3635 if (curr_tok_is_userdef_p)
3637 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3638 if (have_suffix_p == 0)
3640 suffix_id = curr_suffix_id;
3641 have_suffix_p = 1;
3643 else if (have_suffix_p == 1
3644 && curr_suffix_id != suffix_id)
3646 error ("inconsistent user-defined literal suffixes"
3647 " %qD and %qD in string literal",
3648 suffix_id, curr_suffix_id);
3649 have_suffix_p = -1;
3651 curr_type = cpp_userdef_string_remove_type (tok->type);
3653 else
3654 curr_type = tok->type;
3656 if (type != curr_type)
3658 if (type == CPP_STRING)
3659 type = curr_type;
3660 else if (curr_type != CPP_STRING)
3661 error_at (tok->location,
3662 "unsupported non-standard concatenation "
3663 "of string literals");
3666 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3668 tok = cp_lexer_peek_token (parser->lexer);
3669 if (cpp_userdef_string_p (tok->type))
3671 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3672 curr_type = cpp_userdef_string_remove_type (tok->type);
3673 curr_tok_is_userdef_p = true;
3675 else
3677 string_tree = tok->u.value;
3678 curr_type = tok->type;
3679 curr_tok_is_userdef_p = false;
3682 while (cp_parser_is_string_literal (tok));
3684 strs = (cpp_string *) obstack_finish (&str_ob);
3687 if (type != CPP_STRING && !wide_ok)
3689 cp_parser_error (parser, "a wide string is invalid in this context");
3690 type = CPP_STRING;
3693 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3694 (parse_in, strs, count, &istr, type))
3696 value = build_string (istr.len, (const char *)istr.text);
3697 free (CONST_CAST (unsigned char *, istr.text));
3699 switch (type)
3701 default:
3702 case CPP_STRING:
3703 case CPP_UTF8STRING:
3704 TREE_TYPE (value) = char_array_type_node;
3705 break;
3706 case CPP_STRING16:
3707 TREE_TYPE (value) = char16_array_type_node;
3708 break;
3709 case CPP_STRING32:
3710 TREE_TYPE (value) = char32_array_type_node;
3711 break;
3712 case CPP_WSTRING:
3713 TREE_TYPE (value) = wchar_array_type_node;
3714 break;
3717 value = fix_string_type (value);
3719 if (have_suffix_p)
3721 tree literal = build_userdef_literal (suffix_id, value,
3722 OT_NONE, NULL_TREE);
3723 if (lookup_udlit)
3724 value = cp_parser_userdef_string_literal (literal);
3725 else
3726 value = literal;
3729 else
3730 /* cpp_interpret_string has issued an error. */
3731 value = error_mark_node;
3733 if (count > 1)
3734 obstack_free (&str_ob, 0);
3736 return value;
3739 /* Look up a literal operator with the name and the exact arguments. */
3741 static tree
3742 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
3744 tree decl, fns;
3745 decl = lookup_name (name);
3746 if (!decl || !is_overloaded_fn (decl))
3747 return error_mark_node;
3749 for (fns = decl; fns; fns = OVL_NEXT (fns))
3751 unsigned int ix;
3752 bool found = true;
3753 tree fn = OVL_CURRENT (fns);
3754 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3755 if (parmtypes != NULL_TREE)
3757 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
3758 ++ix, parmtypes = TREE_CHAIN (parmtypes))
3760 tree tparm = TREE_VALUE (parmtypes);
3761 tree targ = TREE_TYPE ((*args)[ix]);
3762 bool ptr = TYPE_PTR_P (tparm);
3763 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
3764 if ((ptr || arr || !same_type_p (tparm, targ))
3765 && (!ptr || !arr
3766 || !same_type_p (TREE_TYPE (tparm),
3767 TREE_TYPE (targ))))
3768 found = false;
3770 if (found
3771 && ix == vec_safe_length (args)
3772 /* May be this should be sufficient_parms_p instead,
3773 depending on how exactly should user-defined literals
3774 work in presence of default arguments on the literal
3775 operator parameters. */
3776 && parmtypes == void_list_node)
3777 return fn;
3781 return error_mark_node;
3784 /* Parse a user-defined char constant. Returns a call to a user-defined
3785 literal operator taking the character as an argument. */
3787 static tree
3788 cp_parser_userdef_char_literal (cp_parser *parser)
3790 cp_token *token = cp_lexer_consume_token (parser->lexer);
3791 tree literal = token->u.value;
3792 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3793 tree value = USERDEF_LITERAL_VALUE (literal);
3794 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3795 tree decl, result;
3797 /* Build up a call to the user-defined operator */
3798 /* Lookup the name we got back from the id-expression. */
3799 vec<tree, va_gc> *args = make_tree_vector ();
3800 vec_safe_push (args, value);
3801 decl = lookup_literal_operator (name, args);
3802 if (!decl || decl == error_mark_node)
3804 error ("unable to find character literal operator %qD with %qT argument",
3805 name, TREE_TYPE (value));
3806 release_tree_vector (args);
3807 return error_mark_node;
3809 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3810 release_tree_vector (args);
3811 if (result != error_mark_node)
3812 return result;
3814 error ("unable to find character literal operator %qD with %qT argument",
3815 name, TREE_TYPE (value));
3816 return error_mark_node;
3819 /* A subroutine of cp_parser_userdef_numeric_literal to
3820 create a char... template parameter pack from a string node. */
3822 static tree
3823 make_char_string_pack (tree value)
3825 tree charvec;
3826 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3827 const char *str = TREE_STRING_POINTER (value);
3828 int i, len = TREE_STRING_LENGTH (value) - 1;
3829 tree argvec = make_tree_vec (1);
3831 /* Fill in CHARVEC with all of the parameters. */
3832 charvec = make_tree_vec (len);
3833 for (i = 0; i < len; ++i)
3834 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3836 /* Build the argument packs. */
3837 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3838 TREE_TYPE (argpack) = char_type_node;
3840 TREE_VEC_ELT (argvec, 0) = argpack;
3842 return argvec;
3845 /* A subroutine of cp_parser_userdef_numeric_literal to
3846 create a char... template parameter pack from a string node. */
3848 static tree
3849 make_string_pack (tree value)
3851 tree charvec;
3852 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3853 const unsigned char *str
3854 = (const unsigned char *) TREE_STRING_POINTER (value);
3855 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
3856 int len = TREE_STRING_LENGTH (value) / sz - 1;
3857 tree argvec = make_tree_vec (2);
3859 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
3860 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
3862 /* First template parm is character type. */
3863 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
3865 /* Fill in CHARVEC with all of the parameters. */
3866 charvec = make_tree_vec (len);
3867 for (int i = 0; i < len; ++i)
3868 TREE_VEC_ELT (charvec, i)
3869 = double_int_to_tree (str_char_type_node,
3870 double_int::from_buffer (str + i * sz, sz));
3872 /* Build the argument packs. */
3873 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3874 TREE_TYPE (argpack) = str_char_type_node;
3876 TREE_VEC_ELT (argvec, 1) = argpack;
3878 return argvec;
3881 /* Parse a user-defined numeric constant. returns a call to a user-defined
3882 literal operator. */
3884 static tree
3885 cp_parser_userdef_numeric_literal (cp_parser *parser)
3887 cp_token *token = cp_lexer_consume_token (parser->lexer);
3888 tree literal = token->u.value;
3889 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3890 tree value = USERDEF_LITERAL_VALUE (literal);
3891 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
3892 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3893 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3894 tree decl, result;
3895 vec<tree, va_gc> *args;
3897 /* Look for a literal operator taking the exact type of numeric argument
3898 as the literal value. */
3899 args = make_tree_vector ();
3900 vec_safe_push (args, value);
3901 decl = lookup_literal_operator (name, args);
3902 if (decl && decl != error_mark_node)
3904 result = finish_call_expr (decl, &args, false, true, tf_none);
3905 if (result != error_mark_node)
3907 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
3908 warning_at (token->location, OPT_Woverflow,
3909 "integer literal exceeds range of %qT type",
3910 long_long_unsigned_type_node);
3911 else
3913 if (overflow > 0)
3914 warning_at (token->location, OPT_Woverflow,
3915 "floating literal exceeds range of %qT type",
3916 long_double_type_node);
3917 else if (overflow < 0)
3918 warning_at (token->location, OPT_Woverflow,
3919 "floating literal truncated to zero");
3921 release_tree_vector (args);
3922 return result;
3925 release_tree_vector (args);
3927 /* If the numeric argument didn't work, look for a raw literal
3928 operator taking a const char* argument consisting of the number
3929 in string format. */
3930 args = make_tree_vector ();
3931 vec_safe_push (args, num_string);
3932 decl = lookup_literal_operator (name, args);
3933 if (decl && decl != error_mark_node)
3935 result = finish_call_expr (decl, &args, false, true, tf_none);
3936 if (result != error_mark_node)
3938 release_tree_vector (args);
3939 return result;
3942 release_tree_vector (args);
3944 /* If the raw literal didn't work, look for a non-type template
3945 function with parameter pack char.... Call the function with
3946 template parameter characters representing the number. */
3947 args = make_tree_vector ();
3948 decl = lookup_literal_operator (name, args);
3949 if (decl && decl != error_mark_node)
3951 tree tmpl_args = make_char_string_pack (num_string);
3952 decl = lookup_template_function (decl, tmpl_args);
3953 result = finish_call_expr (decl, &args, false, true, tf_none);
3954 if (result != error_mark_node)
3956 release_tree_vector (args);
3957 return result;
3960 release_tree_vector (args);
3962 error ("unable to find numeric literal operator %qD", name);
3963 if (!cpp_get_options (parse_in)->ext_numeric_literals)
3964 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
3965 "to enable more built-in suffixes");
3966 return error_mark_node;
3969 /* Parse a user-defined string constant. Returns a call to a user-defined
3970 literal operator taking a character pointer and the length of the string
3971 as arguments. */
3973 static tree
3974 cp_parser_userdef_string_literal (tree literal)
3976 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3977 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3978 tree value = USERDEF_LITERAL_VALUE (literal);
3979 int len = TREE_STRING_LENGTH (value)
3980 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3981 tree decl, result;
3982 vec<tree, va_gc> *args;
3984 /* Look for a template function with typename parameter CharT
3985 and parameter pack CharT... Call the function with
3986 template parameter characters representing the string. */
3987 args = make_tree_vector ();
3988 decl = lookup_literal_operator (name, args);
3989 if (decl && decl != error_mark_node)
3991 tree tmpl_args = make_string_pack (value);
3992 decl = lookup_template_function (decl, tmpl_args);
3993 result = finish_call_expr (decl, &args, false, true, tf_none);
3994 if (result != error_mark_node)
3996 release_tree_vector (args);
3997 return result;
4000 release_tree_vector (args);
4002 /* Build up a call to the user-defined operator */
4003 /* Lookup the name we got back from the id-expression. */
4004 args = make_tree_vector ();
4005 vec_safe_push (args, value);
4006 vec_safe_push (args, build_int_cst (size_type_node, len));
4007 decl = lookup_name (name);
4008 if (!decl || decl == error_mark_node)
4010 error ("unable to find string literal operator %qD", name);
4011 release_tree_vector (args);
4012 return error_mark_node;
4014 result = finish_call_expr (decl, &args, false, true, tf_none);
4015 release_tree_vector (args);
4016 if (result != error_mark_node)
4017 return result;
4019 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4020 name, TREE_TYPE (value), size_type_node);
4021 return error_mark_node;
4025 /* Basic concepts [gram.basic] */
4027 /* Parse a translation-unit.
4029 translation-unit:
4030 declaration-seq [opt]
4032 Returns TRUE if all went well. */
4034 static bool
4035 cp_parser_translation_unit (cp_parser* parser)
4037 /* The address of the first non-permanent object on the declarator
4038 obstack. */
4039 static void *declarator_obstack_base;
4041 bool success;
4043 /* Create the declarator obstack, if necessary. */
4044 if (!cp_error_declarator)
4046 gcc_obstack_init (&declarator_obstack);
4047 /* Create the error declarator. */
4048 cp_error_declarator = make_declarator (cdk_error);
4049 /* Create the empty parameter list. */
4050 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4051 /* Remember where the base of the declarator obstack lies. */
4052 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4055 cp_parser_declaration_seq_opt (parser);
4057 /* If there are no tokens left then all went well. */
4058 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4060 /* Get rid of the token array; we don't need it any more. */
4061 cp_lexer_destroy (parser->lexer);
4062 parser->lexer = NULL;
4064 /* This file might have been a context that's implicitly extern
4065 "C". If so, pop the lang context. (Only relevant for PCH.) */
4066 if (parser->implicit_extern_c)
4068 pop_lang_context ();
4069 parser->implicit_extern_c = false;
4072 /* Finish up. */
4073 finish_translation_unit ();
4075 success = true;
4077 else
4079 cp_parser_error (parser, "expected declaration");
4080 success = false;
4083 /* Make sure the declarator obstack was fully cleaned up. */
4084 gcc_assert (obstack_next_free (&declarator_obstack)
4085 == declarator_obstack_base);
4087 /* All went well. */
4088 return success;
4091 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4092 decltype context. */
4094 static inline tsubst_flags_t
4095 complain_flags (bool decltype_p)
4097 tsubst_flags_t complain = tf_warning_or_error;
4098 if (decltype_p)
4099 complain |= tf_decltype;
4100 return complain;
4103 /* Expressions [gram.expr] */
4105 /* Parse a primary-expression.
4107 primary-expression:
4108 literal
4109 this
4110 ( expression )
4111 id-expression
4112 lambda-expression (C++11)
4114 GNU Extensions:
4116 primary-expression:
4117 ( compound-statement )
4118 __builtin_va_arg ( assignment-expression , type-id )
4119 __builtin_offsetof ( type-id , offsetof-expression )
4121 C++ Extensions:
4122 __has_nothrow_assign ( type-id )
4123 __has_nothrow_constructor ( type-id )
4124 __has_nothrow_copy ( type-id )
4125 __has_trivial_assign ( type-id )
4126 __has_trivial_constructor ( type-id )
4127 __has_trivial_copy ( type-id )
4128 __has_trivial_destructor ( type-id )
4129 __has_virtual_destructor ( type-id )
4130 __is_abstract ( type-id )
4131 __is_base_of ( type-id , type-id )
4132 __is_class ( type-id )
4133 __is_convertible_to ( type-id , type-id )
4134 __is_empty ( type-id )
4135 __is_enum ( type-id )
4136 __is_final ( type-id )
4137 __is_literal_type ( type-id )
4138 __is_pod ( type-id )
4139 __is_polymorphic ( type-id )
4140 __is_std_layout ( type-id )
4141 __is_trivial ( type-id )
4142 __is_union ( type-id )
4144 Objective-C++ Extension:
4146 primary-expression:
4147 objc-expression
4149 literal:
4150 __null
4152 ADDRESS_P is true iff this expression was immediately preceded by
4153 "&" and therefore might denote a pointer-to-member. CAST_P is true
4154 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4155 true iff this expression is a template argument.
4157 Returns a representation of the expression. Upon return, *IDK
4158 indicates what kind of id-expression (if any) was present. */
4160 static tree
4161 cp_parser_primary_expression (cp_parser *parser,
4162 bool address_p,
4163 bool cast_p,
4164 bool template_arg_p,
4165 bool decltype_p,
4166 cp_id_kind *idk)
4168 cp_token *token = NULL;
4170 /* Assume the primary expression is not an id-expression. */
4171 *idk = CP_ID_KIND_NONE;
4173 /* Peek at the next token. */
4174 token = cp_lexer_peek_token (parser->lexer);
4175 switch (token->type)
4177 /* literal:
4178 integer-literal
4179 character-literal
4180 floating-literal
4181 string-literal
4182 boolean-literal
4183 pointer-literal
4184 user-defined-literal */
4185 case CPP_CHAR:
4186 case CPP_CHAR16:
4187 case CPP_CHAR32:
4188 case CPP_WCHAR:
4189 case CPP_NUMBER:
4190 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4191 return cp_parser_userdef_numeric_literal (parser);
4192 token = cp_lexer_consume_token (parser->lexer);
4193 if (TREE_CODE (token->u.value) == FIXED_CST)
4195 error_at (token->location,
4196 "fixed-point types not supported in C++");
4197 return error_mark_node;
4199 /* Floating-point literals are only allowed in an integral
4200 constant expression if they are cast to an integral or
4201 enumeration type. */
4202 if (TREE_CODE (token->u.value) == REAL_CST
4203 && parser->integral_constant_expression_p
4204 && pedantic)
4206 /* CAST_P will be set even in invalid code like "int(2.7 +
4207 ...)". Therefore, we have to check that the next token
4208 is sure to end the cast. */
4209 if (cast_p)
4211 cp_token *next_token;
4213 next_token = cp_lexer_peek_token (parser->lexer);
4214 if (/* The comma at the end of an
4215 enumerator-definition. */
4216 next_token->type != CPP_COMMA
4217 /* The curly brace at the end of an enum-specifier. */
4218 && next_token->type != CPP_CLOSE_BRACE
4219 /* The end of a statement. */
4220 && next_token->type != CPP_SEMICOLON
4221 /* The end of the cast-expression. */
4222 && next_token->type != CPP_CLOSE_PAREN
4223 /* The end of an array bound. */
4224 && next_token->type != CPP_CLOSE_SQUARE
4225 /* The closing ">" in a template-argument-list. */
4226 && (next_token->type != CPP_GREATER
4227 || parser->greater_than_is_operator_p)
4228 /* C++0x only: A ">>" treated like two ">" tokens,
4229 in a template-argument-list. */
4230 && (next_token->type != CPP_RSHIFT
4231 || (cxx_dialect == cxx98)
4232 || parser->greater_than_is_operator_p))
4233 cast_p = false;
4236 /* If we are within a cast, then the constraint that the
4237 cast is to an integral or enumeration type will be
4238 checked at that point. If we are not within a cast, then
4239 this code is invalid. */
4240 if (!cast_p)
4241 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4243 return token->u.value;
4245 case CPP_CHAR_USERDEF:
4246 case CPP_CHAR16_USERDEF:
4247 case CPP_CHAR32_USERDEF:
4248 case CPP_WCHAR_USERDEF:
4249 return cp_parser_userdef_char_literal (parser);
4251 case CPP_STRING:
4252 case CPP_STRING16:
4253 case CPP_STRING32:
4254 case CPP_WSTRING:
4255 case CPP_UTF8STRING:
4256 case CPP_STRING_USERDEF:
4257 case CPP_STRING16_USERDEF:
4258 case CPP_STRING32_USERDEF:
4259 case CPP_WSTRING_USERDEF:
4260 case CPP_UTF8STRING_USERDEF:
4261 /* ??? Should wide strings be allowed when parser->translate_strings_p
4262 is false (i.e. in attributes)? If not, we can kill the third
4263 argument to cp_parser_string_literal. */
4264 return cp_parser_string_literal (parser,
4265 parser->translate_strings_p,
4266 true);
4268 case CPP_OPEN_PAREN:
4270 tree expr;
4271 bool saved_greater_than_is_operator_p;
4273 /* Consume the `('. */
4274 cp_lexer_consume_token (parser->lexer);
4275 /* Within a parenthesized expression, a `>' token is always
4276 the greater-than operator. */
4277 saved_greater_than_is_operator_p
4278 = parser->greater_than_is_operator_p;
4279 parser->greater_than_is_operator_p = true;
4280 /* If we see `( { ' then we are looking at the beginning of
4281 a GNU statement-expression. */
4282 if (cp_parser_allow_gnu_extensions_p (parser)
4283 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4285 /* Statement-expressions are not allowed by the standard. */
4286 pedwarn (token->location, OPT_Wpedantic,
4287 "ISO C++ forbids braced-groups within expressions");
4289 /* And they're not allowed outside of a function-body; you
4290 cannot, for example, write:
4292 int i = ({ int j = 3; j + 1; });
4294 at class or namespace scope. */
4295 if (!parser->in_function_body
4296 || parser->in_template_argument_list_p)
4298 error_at (token->location,
4299 "statement-expressions are not allowed outside "
4300 "functions nor in template-argument lists");
4301 cp_parser_skip_to_end_of_block_or_statement (parser);
4302 expr = error_mark_node;
4304 else
4306 /* Start the statement-expression. */
4307 expr = begin_stmt_expr ();
4308 /* Parse the compound-statement. */
4309 cp_parser_compound_statement (parser, expr, false, false);
4310 /* Finish up. */
4311 expr = finish_stmt_expr (expr, false);
4314 else
4316 /* Parse the parenthesized expression. */
4317 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4318 /* Let the front end know that this expression was
4319 enclosed in parentheses. This matters in case, for
4320 example, the expression is of the form `A::B', since
4321 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4322 not. */
4323 expr = finish_parenthesized_expr (expr);
4324 /* DR 705: Wrapping an unqualified name in parentheses
4325 suppresses arg-dependent lookup. We want to pass back
4326 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4327 (c++/37862), but none of the others. */
4328 if (*idk != CP_ID_KIND_QUALIFIED)
4329 *idk = CP_ID_KIND_NONE;
4331 /* The `>' token might be the end of a template-id or
4332 template-parameter-list now. */
4333 parser->greater_than_is_operator_p
4334 = saved_greater_than_is_operator_p;
4335 /* Consume the `)'. */
4336 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4337 cp_parser_skip_to_end_of_statement (parser);
4339 return expr;
4342 case CPP_OPEN_SQUARE:
4343 if (c_dialect_objc ())
4344 /* We have an Objective-C++ message. */
4345 return cp_parser_objc_expression (parser);
4347 tree lam = cp_parser_lambda_expression (parser);
4348 /* Don't warn about a failed tentative parse. */
4349 if (cp_parser_error_occurred (parser))
4350 return error_mark_node;
4351 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4352 return lam;
4355 case CPP_OBJC_STRING:
4356 if (c_dialect_objc ())
4357 /* We have an Objective-C++ string literal. */
4358 return cp_parser_objc_expression (parser);
4359 cp_parser_error (parser, "expected primary-expression");
4360 return error_mark_node;
4362 case CPP_KEYWORD:
4363 switch (token->keyword)
4365 /* These two are the boolean literals. */
4366 case RID_TRUE:
4367 cp_lexer_consume_token (parser->lexer);
4368 return boolean_true_node;
4369 case RID_FALSE:
4370 cp_lexer_consume_token (parser->lexer);
4371 return boolean_false_node;
4373 /* The `__null' literal. */
4374 case RID_NULL:
4375 cp_lexer_consume_token (parser->lexer);
4376 return null_node;
4378 /* The `nullptr' literal. */
4379 case RID_NULLPTR:
4380 cp_lexer_consume_token (parser->lexer);
4381 return nullptr_node;
4383 /* Recognize the `this' keyword. */
4384 case RID_THIS:
4385 cp_lexer_consume_token (parser->lexer);
4386 if (parser->local_variables_forbidden_p)
4388 error_at (token->location,
4389 "%<this%> may not be used in this context");
4390 return error_mark_node;
4392 /* Pointers cannot appear in constant-expressions. */
4393 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4394 return error_mark_node;
4395 return finish_this_expr ();
4397 /* The `operator' keyword can be the beginning of an
4398 id-expression. */
4399 case RID_OPERATOR:
4400 goto id_expression;
4402 case RID_FUNCTION_NAME:
4403 case RID_PRETTY_FUNCTION_NAME:
4404 case RID_C99_FUNCTION_NAME:
4406 non_integral_constant name;
4408 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4409 __func__ are the names of variables -- but they are
4410 treated specially. Therefore, they are handled here,
4411 rather than relying on the generic id-expression logic
4412 below. Grammatically, these names are id-expressions.
4414 Consume the token. */
4415 token = cp_lexer_consume_token (parser->lexer);
4417 switch (token->keyword)
4419 case RID_FUNCTION_NAME:
4420 name = NIC_FUNC_NAME;
4421 break;
4422 case RID_PRETTY_FUNCTION_NAME:
4423 name = NIC_PRETTY_FUNC;
4424 break;
4425 case RID_C99_FUNCTION_NAME:
4426 name = NIC_C99_FUNC;
4427 break;
4428 default:
4429 gcc_unreachable ();
4432 if (cp_parser_non_integral_constant_expression (parser, name))
4433 return error_mark_node;
4435 /* Look up the name. */
4436 return finish_fname (token->u.value);
4439 case RID_VA_ARG:
4441 tree expression;
4442 tree type;
4443 source_location type_location;
4445 /* The `__builtin_va_arg' construct is used to handle
4446 `va_arg'. Consume the `__builtin_va_arg' token. */
4447 cp_lexer_consume_token (parser->lexer);
4448 /* Look for the opening `('. */
4449 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4450 /* Now, parse the assignment-expression. */
4451 expression = cp_parser_assignment_expression (parser,
4452 /*cast_p=*/false, NULL);
4453 /* Look for the `,'. */
4454 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4455 type_location = cp_lexer_peek_token (parser->lexer)->location;
4456 /* Parse the type-id. */
4457 type = cp_parser_type_id (parser);
4458 /* Look for the closing `)'. */
4459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4460 /* Using `va_arg' in a constant-expression is not
4461 allowed. */
4462 if (cp_parser_non_integral_constant_expression (parser,
4463 NIC_VA_ARG))
4464 return error_mark_node;
4465 return build_x_va_arg (type_location, expression, type);
4468 case RID_OFFSETOF:
4469 return cp_parser_builtin_offsetof (parser);
4471 case RID_HAS_NOTHROW_ASSIGN:
4472 case RID_HAS_NOTHROW_CONSTRUCTOR:
4473 case RID_HAS_NOTHROW_COPY:
4474 case RID_HAS_TRIVIAL_ASSIGN:
4475 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4476 case RID_HAS_TRIVIAL_COPY:
4477 case RID_HAS_TRIVIAL_DESTRUCTOR:
4478 case RID_HAS_VIRTUAL_DESTRUCTOR:
4479 case RID_IS_ABSTRACT:
4480 case RID_IS_BASE_OF:
4481 case RID_IS_CLASS:
4482 case RID_IS_CONVERTIBLE_TO:
4483 case RID_IS_EMPTY:
4484 case RID_IS_ENUM:
4485 case RID_IS_FINAL:
4486 case RID_IS_LITERAL_TYPE:
4487 case RID_IS_POD:
4488 case RID_IS_POLYMORPHIC:
4489 case RID_IS_STD_LAYOUT:
4490 case RID_IS_TRIVIAL:
4491 case RID_IS_UNION:
4492 return cp_parser_trait_expr (parser, token->keyword);
4494 /* Objective-C++ expressions. */
4495 case RID_AT_ENCODE:
4496 case RID_AT_PROTOCOL:
4497 case RID_AT_SELECTOR:
4498 return cp_parser_objc_expression (parser);
4500 case RID_TEMPLATE:
4501 if (parser->in_function_body
4502 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4503 == CPP_LESS))
4505 error_at (token->location,
4506 "a template declaration cannot appear at block scope");
4507 cp_parser_skip_to_end_of_block_or_statement (parser);
4508 return error_mark_node;
4510 default:
4511 cp_parser_error (parser, "expected primary-expression");
4512 return error_mark_node;
4515 /* An id-expression can start with either an identifier, a
4516 `::' as the beginning of a qualified-id, or the "operator"
4517 keyword. */
4518 case CPP_NAME:
4519 case CPP_SCOPE:
4520 case CPP_TEMPLATE_ID:
4521 case CPP_NESTED_NAME_SPECIFIER:
4523 tree id_expression;
4524 tree decl;
4525 const char *error_msg;
4526 bool template_p;
4527 bool done;
4528 cp_token *id_expr_token;
4530 id_expression:
4531 /* Parse the id-expression. */
4532 id_expression
4533 = cp_parser_id_expression (parser,
4534 /*template_keyword_p=*/false,
4535 /*check_dependency_p=*/true,
4536 &template_p,
4537 /*declarator_p=*/false,
4538 /*optional_p=*/false);
4539 if (id_expression == error_mark_node)
4540 return error_mark_node;
4541 id_expr_token = token;
4542 token = cp_lexer_peek_token (parser->lexer);
4543 done = (token->type != CPP_OPEN_SQUARE
4544 && token->type != CPP_OPEN_PAREN
4545 && token->type != CPP_DOT
4546 && token->type != CPP_DEREF
4547 && token->type != CPP_PLUS_PLUS
4548 && token->type != CPP_MINUS_MINUS);
4549 /* If we have a template-id, then no further lookup is
4550 required. If the template-id was for a template-class, we
4551 will sometimes have a TYPE_DECL at this point. */
4552 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4553 || TREE_CODE (id_expression) == TYPE_DECL)
4554 decl = id_expression;
4555 /* Look up the name. */
4556 else
4558 tree ambiguous_decls;
4560 /* If we already know that this lookup is ambiguous, then
4561 we've already issued an error message; there's no reason
4562 to check again. */
4563 if (id_expr_token->type == CPP_NAME
4564 && id_expr_token->error_reported)
4566 cp_parser_simulate_error (parser);
4567 return error_mark_node;
4570 decl = cp_parser_lookup_name (parser, id_expression,
4571 none_type,
4572 template_p,
4573 /*is_namespace=*/false,
4574 /*check_dependency=*/true,
4575 &ambiguous_decls,
4576 id_expr_token->location);
4577 /* If the lookup was ambiguous, an error will already have
4578 been issued. */
4579 if (ambiguous_decls)
4580 return error_mark_node;
4582 /* In Objective-C++, we may have an Objective-C 2.0
4583 dot-syntax for classes here. */
4584 if (c_dialect_objc ()
4585 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4586 && TREE_CODE (decl) == TYPE_DECL
4587 && objc_is_class_name (decl))
4589 tree component;
4590 cp_lexer_consume_token (parser->lexer);
4591 component = cp_parser_identifier (parser);
4592 if (component == error_mark_node)
4593 return error_mark_node;
4595 return objc_build_class_component_ref (id_expression, component);
4598 /* In Objective-C++, an instance variable (ivar) may be preferred
4599 to whatever cp_parser_lookup_name() found. */
4600 decl = objc_lookup_ivar (decl, id_expression);
4602 /* If name lookup gives us a SCOPE_REF, then the
4603 qualifying scope was dependent. */
4604 if (TREE_CODE (decl) == SCOPE_REF)
4606 /* At this point, we do not know if DECL is a valid
4607 integral constant expression. We assume that it is
4608 in fact such an expression, so that code like:
4610 template <int N> struct A {
4611 int a[B<N>::i];
4614 is accepted. At template-instantiation time, we
4615 will check that B<N>::i is actually a constant. */
4616 return decl;
4618 /* Check to see if DECL is a local variable in a context
4619 where that is forbidden. */
4620 if (parser->local_variables_forbidden_p
4621 && local_variable_p (decl))
4623 /* It might be that we only found DECL because we are
4624 trying to be generous with pre-ISO scoping rules.
4625 For example, consider:
4627 int i;
4628 void g() {
4629 for (int i = 0; i < 10; ++i) {}
4630 extern void f(int j = i);
4633 Here, name look up will originally find the out
4634 of scope `i'. We need to issue a warning message,
4635 but then use the global `i'. */
4636 decl = check_for_out_of_scope_variable (decl);
4637 if (local_variable_p (decl))
4639 error_at (id_expr_token->location,
4640 "local variable %qD may not appear in this context",
4641 decl);
4642 return error_mark_node;
4647 decl = (finish_id_expression
4648 (id_expression, decl, parser->scope,
4649 idk,
4650 parser->integral_constant_expression_p,
4651 parser->allow_non_integral_constant_expression_p,
4652 &parser->non_integral_constant_expression_p,
4653 template_p, done, address_p,
4654 template_arg_p,
4655 &error_msg,
4656 id_expr_token->location));
4657 if (error_msg)
4658 cp_parser_error (parser, error_msg);
4659 return decl;
4662 /* Anything else is an error. */
4663 default:
4664 cp_parser_error (parser, "expected primary-expression");
4665 return error_mark_node;
4669 static inline tree
4670 cp_parser_primary_expression (cp_parser *parser,
4671 bool address_p,
4672 bool cast_p,
4673 bool template_arg_p,
4674 cp_id_kind *idk)
4676 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
4677 /*decltype*/false, idk);
4680 /* Parse an id-expression.
4682 id-expression:
4683 unqualified-id
4684 qualified-id
4686 qualified-id:
4687 :: [opt] nested-name-specifier template [opt] unqualified-id
4688 :: identifier
4689 :: operator-function-id
4690 :: template-id
4692 Return a representation of the unqualified portion of the
4693 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4694 a `::' or nested-name-specifier.
4696 Often, if the id-expression was a qualified-id, the caller will
4697 want to make a SCOPE_REF to represent the qualified-id. This
4698 function does not do this in order to avoid wastefully creating
4699 SCOPE_REFs when they are not required.
4701 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4702 `template' keyword.
4704 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4705 uninstantiated templates.
4707 If *TEMPLATE_P is non-NULL, it is set to true iff the
4708 `template' keyword is used to explicitly indicate that the entity
4709 named is a template.
4711 If DECLARATOR_P is true, the id-expression is appearing as part of
4712 a declarator, rather than as part of an expression. */
4714 static tree
4715 cp_parser_id_expression (cp_parser *parser,
4716 bool template_keyword_p,
4717 bool check_dependency_p,
4718 bool *template_p,
4719 bool declarator_p,
4720 bool optional_p)
4722 bool global_scope_p;
4723 bool nested_name_specifier_p;
4725 /* Assume the `template' keyword was not used. */
4726 if (template_p)
4727 *template_p = template_keyword_p;
4729 /* Look for the optional `::' operator. */
4730 global_scope_p
4731 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4732 != NULL_TREE);
4733 /* Look for the optional nested-name-specifier. */
4734 nested_name_specifier_p
4735 = (cp_parser_nested_name_specifier_opt (parser,
4736 /*typename_keyword_p=*/false,
4737 check_dependency_p,
4738 /*type_p=*/false,
4739 declarator_p)
4740 != NULL_TREE);
4741 /* If there is a nested-name-specifier, then we are looking at
4742 the first qualified-id production. */
4743 if (nested_name_specifier_p)
4745 tree saved_scope;
4746 tree saved_object_scope;
4747 tree saved_qualifying_scope;
4748 tree unqualified_id;
4749 bool is_template;
4751 /* See if the next token is the `template' keyword. */
4752 if (!template_p)
4753 template_p = &is_template;
4754 *template_p = cp_parser_optional_template_keyword (parser);
4755 /* Name lookup we do during the processing of the
4756 unqualified-id might obliterate SCOPE. */
4757 saved_scope = parser->scope;
4758 saved_object_scope = parser->object_scope;
4759 saved_qualifying_scope = parser->qualifying_scope;
4760 /* Process the final unqualified-id. */
4761 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4762 check_dependency_p,
4763 declarator_p,
4764 /*optional_p=*/false);
4765 /* Restore the SAVED_SCOPE for our caller. */
4766 parser->scope = saved_scope;
4767 parser->object_scope = saved_object_scope;
4768 parser->qualifying_scope = saved_qualifying_scope;
4770 return unqualified_id;
4772 /* Otherwise, if we are in global scope, then we are looking at one
4773 of the other qualified-id productions. */
4774 else if (global_scope_p)
4776 cp_token *token;
4777 tree id;
4779 /* Peek at the next token. */
4780 token = cp_lexer_peek_token (parser->lexer);
4782 /* If it's an identifier, and the next token is not a "<", then
4783 we can avoid the template-id case. This is an optimization
4784 for this common case. */
4785 if (token->type == CPP_NAME
4786 && !cp_parser_nth_token_starts_template_argument_list_p
4787 (parser, 2))
4788 return cp_parser_identifier (parser);
4790 cp_parser_parse_tentatively (parser);
4791 /* Try a template-id. */
4792 id = cp_parser_template_id (parser,
4793 /*template_keyword_p=*/false,
4794 /*check_dependency_p=*/true,
4795 none_type,
4796 declarator_p);
4797 /* If that worked, we're done. */
4798 if (cp_parser_parse_definitely (parser))
4799 return id;
4801 /* Peek at the next token. (Changes in the token buffer may
4802 have invalidated the pointer obtained above.) */
4803 token = cp_lexer_peek_token (parser->lexer);
4805 switch (token->type)
4807 case CPP_NAME:
4808 return cp_parser_identifier (parser);
4810 case CPP_KEYWORD:
4811 if (token->keyword == RID_OPERATOR)
4812 return cp_parser_operator_function_id (parser);
4813 /* Fall through. */
4815 default:
4816 cp_parser_error (parser, "expected id-expression");
4817 return error_mark_node;
4820 else
4821 return cp_parser_unqualified_id (parser, template_keyword_p,
4822 /*check_dependency_p=*/true,
4823 declarator_p,
4824 optional_p);
4827 /* Parse an unqualified-id.
4829 unqualified-id:
4830 identifier
4831 operator-function-id
4832 conversion-function-id
4833 ~ class-name
4834 template-id
4836 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4837 keyword, in a construct like `A::template ...'.
4839 Returns a representation of unqualified-id. For the `identifier'
4840 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4841 production a BIT_NOT_EXPR is returned; the operand of the
4842 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4843 other productions, see the documentation accompanying the
4844 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4845 names are looked up in uninstantiated templates. If DECLARATOR_P
4846 is true, the unqualified-id is appearing as part of a declarator,
4847 rather than as part of an expression. */
4849 static tree
4850 cp_parser_unqualified_id (cp_parser* parser,
4851 bool template_keyword_p,
4852 bool check_dependency_p,
4853 bool declarator_p,
4854 bool optional_p)
4856 cp_token *token;
4858 /* Peek at the next token. */
4859 token = cp_lexer_peek_token (parser->lexer);
4861 switch (token->type)
4863 case CPP_NAME:
4865 tree id;
4867 /* We don't know yet whether or not this will be a
4868 template-id. */
4869 cp_parser_parse_tentatively (parser);
4870 /* Try a template-id. */
4871 id = cp_parser_template_id (parser, template_keyword_p,
4872 check_dependency_p,
4873 none_type,
4874 declarator_p);
4875 /* If it worked, we're done. */
4876 if (cp_parser_parse_definitely (parser))
4877 return id;
4878 /* Otherwise, it's an ordinary identifier. */
4879 return cp_parser_identifier (parser);
4882 case CPP_TEMPLATE_ID:
4883 return cp_parser_template_id (parser, template_keyword_p,
4884 check_dependency_p,
4885 none_type,
4886 declarator_p);
4888 case CPP_COMPL:
4890 tree type_decl;
4891 tree qualifying_scope;
4892 tree object_scope;
4893 tree scope;
4894 bool done;
4896 /* Consume the `~' token. */
4897 cp_lexer_consume_token (parser->lexer);
4898 /* Parse the class-name. The standard, as written, seems to
4899 say that:
4901 template <typename T> struct S { ~S (); };
4902 template <typename T> S<T>::~S() {}
4904 is invalid, since `~' must be followed by a class-name, but
4905 `S<T>' is dependent, and so not known to be a class.
4906 That's not right; we need to look in uninstantiated
4907 templates. A further complication arises from:
4909 template <typename T> void f(T t) {
4910 t.T::~T();
4913 Here, it is not possible to look up `T' in the scope of `T'
4914 itself. We must look in both the current scope, and the
4915 scope of the containing complete expression.
4917 Yet another issue is:
4919 struct S {
4920 int S;
4921 ~S();
4924 S::~S() {}
4926 The standard does not seem to say that the `S' in `~S'
4927 should refer to the type `S' and not the data member
4928 `S::S'. */
4930 /* DR 244 says that we look up the name after the "~" in the
4931 same scope as we looked up the qualifying name. That idea
4932 isn't fully worked out; it's more complicated than that. */
4933 scope = parser->scope;
4934 object_scope = parser->object_scope;
4935 qualifying_scope = parser->qualifying_scope;
4937 /* Check for invalid scopes. */
4938 if (scope == error_mark_node)
4940 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4941 cp_lexer_consume_token (parser->lexer);
4942 return error_mark_node;
4944 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4946 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4947 error_at (token->location,
4948 "scope %qT before %<~%> is not a class-name",
4949 scope);
4950 cp_parser_simulate_error (parser);
4951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4952 cp_lexer_consume_token (parser->lexer);
4953 return error_mark_node;
4955 gcc_assert (!scope || TYPE_P (scope));
4957 /* If the name is of the form "X::~X" it's OK even if X is a
4958 typedef. */
4959 token = cp_lexer_peek_token (parser->lexer);
4960 if (scope
4961 && token->type == CPP_NAME
4962 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4963 != CPP_LESS)
4964 && (token->u.value == TYPE_IDENTIFIER (scope)
4965 || (CLASS_TYPE_P (scope)
4966 && constructor_name_p (token->u.value, scope))))
4968 cp_lexer_consume_token (parser->lexer);
4969 return build_nt (BIT_NOT_EXPR, scope);
4972 /* ~auto means the destructor of whatever the object is. */
4973 if (cp_parser_is_keyword (token, RID_AUTO))
4975 if (cxx_dialect < cxx14)
4976 pedwarn (input_location, 0,
4977 "%<~auto%> only available with "
4978 "-std=c++14 or -std=gnu++14");
4979 cp_lexer_consume_token (parser->lexer);
4980 return build_nt (BIT_NOT_EXPR, make_auto ());
4983 /* If there was an explicit qualification (S::~T), first look
4984 in the scope given by the qualification (i.e., S).
4986 Note: in the calls to cp_parser_class_name below we pass
4987 typename_type so that lookup finds the injected-class-name
4988 rather than the constructor. */
4989 done = false;
4990 type_decl = NULL_TREE;
4991 if (scope)
4993 cp_parser_parse_tentatively (parser);
4994 type_decl = cp_parser_class_name (parser,
4995 /*typename_keyword_p=*/false,
4996 /*template_keyword_p=*/false,
4997 typename_type,
4998 /*check_dependency=*/false,
4999 /*class_head_p=*/false,
5000 declarator_p);
5001 if (cp_parser_parse_definitely (parser))
5002 done = true;
5004 /* In "N::S::~S", look in "N" as well. */
5005 if (!done && scope && qualifying_scope)
5007 cp_parser_parse_tentatively (parser);
5008 parser->scope = qualifying_scope;
5009 parser->object_scope = NULL_TREE;
5010 parser->qualifying_scope = NULL_TREE;
5011 type_decl
5012 = cp_parser_class_name (parser,
5013 /*typename_keyword_p=*/false,
5014 /*template_keyword_p=*/false,
5015 typename_type,
5016 /*check_dependency=*/false,
5017 /*class_head_p=*/false,
5018 declarator_p);
5019 if (cp_parser_parse_definitely (parser))
5020 done = true;
5022 /* In "p->S::~T", look in the scope given by "*p" as well. */
5023 else if (!done && object_scope)
5025 cp_parser_parse_tentatively (parser);
5026 parser->scope = object_scope;
5027 parser->object_scope = NULL_TREE;
5028 parser->qualifying_scope = NULL_TREE;
5029 type_decl
5030 = cp_parser_class_name (parser,
5031 /*typename_keyword_p=*/false,
5032 /*template_keyword_p=*/false,
5033 typename_type,
5034 /*check_dependency=*/false,
5035 /*class_head_p=*/false,
5036 declarator_p);
5037 if (cp_parser_parse_definitely (parser))
5038 done = true;
5040 /* Look in the surrounding context. */
5041 if (!done)
5043 parser->scope = NULL_TREE;
5044 parser->object_scope = NULL_TREE;
5045 parser->qualifying_scope = NULL_TREE;
5046 if (processing_template_decl)
5047 cp_parser_parse_tentatively (parser);
5048 type_decl
5049 = cp_parser_class_name (parser,
5050 /*typename_keyword_p=*/false,
5051 /*template_keyword_p=*/false,
5052 typename_type,
5053 /*check_dependency=*/false,
5054 /*class_head_p=*/false,
5055 declarator_p);
5056 if (processing_template_decl
5057 && ! cp_parser_parse_definitely (parser))
5059 /* We couldn't find a type with this name, so just accept
5060 it and check for a match at instantiation time. */
5061 type_decl = cp_parser_identifier (parser);
5062 if (type_decl != error_mark_node)
5063 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5064 return type_decl;
5067 /* If an error occurred, assume that the name of the
5068 destructor is the same as the name of the qualifying
5069 class. That allows us to keep parsing after running
5070 into ill-formed destructor names. */
5071 if (type_decl == error_mark_node && scope)
5072 return build_nt (BIT_NOT_EXPR, scope);
5073 else if (type_decl == error_mark_node)
5074 return error_mark_node;
5076 /* Check that destructor name and scope match. */
5077 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5079 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5080 error_at (token->location,
5081 "declaration of %<~%T%> as member of %qT",
5082 type_decl, scope);
5083 cp_parser_simulate_error (parser);
5084 return error_mark_node;
5087 /* [class.dtor]
5089 A typedef-name that names a class shall not be used as the
5090 identifier in the declarator for a destructor declaration. */
5091 if (declarator_p
5092 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5093 && !DECL_SELF_REFERENCE_P (type_decl)
5094 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5095 error_at (token->location,
5096 "typedef-name %qD used as destructor declarator",
5097 type_decl);
5099 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5102 case CPP_KEYWORD:
5103 if (token->keyword == RID_OPERATOR)
5105 tree id;
5107 /* This could be a template-id, so we try that first. */
5108 cp_parser_parse_tentatively (parser);
5109 /* Try a template-id. */
5110 id = cp_parser_template_id (parser, template_keyword_p,
5111 /*check_dependency_p=*/true,
5112 none_type,
5113 declarator_p);
5114 /* If that worked, we're done. */
5115 if (cp_parser_parse_definitely (parser))
5116 return id;
5117 /* We still don't know whether we're looking at an
5118 operator-function-id or a conversion-function-id. */
5119 cp_parser_parse_tentatively (parser);
5120 /* Try an operator-function-id. */
5121 id = cp_parser_operator_function_id (parser);
5122 /* If that didn't work, try a conversion-function-id. */
5123 if (!cp_parser_parse_definitely (parser))
5124 id = cp_parser_conversion_function_id (parser);
5125 else if (UDLIT_OPER_P (id))
5127 /* 17.6.3.3.5 */
5128 const char *name = UDLIT_OP_SUFFIX (id);
5129 if (name[0] != '_' && !in_system_header_at (input_location)
5130 && declarator_p)
5131 warning (0, "literal operator suffixes not preceded by %<_%>"
5132 " are reserved for future standardization");
5135 return id;
5137 /* Fall through. */
5139 default:
5140 if (optional_p)
5141 return NULL_TREE;
5142 cp_parser_error (parser, "expected unqualified-id");
5143 return error_mark_node;
5147 /* Parse an (optional) nested-name-specifier.
5149 nested-name-specifier: [C++98]
5150 class-or-namespace-name :: nested-name-specifier [opt]
5151 class-or-namespace-name :: template nested-name-specifier [opt]
5153 nested-name-specifier: [C++0x]
5154 type-name ::
5155 namespace-name ::
5156 nested-name-specifier identifier ::
5157 nested-name-specifier template [opt] simple-template-id ::
5159 PARSER->SCOPE should be set appropriately before this function is
5160 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5161 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5162 in name lookups.
5164 Sets PARSER->SCOPE to the class (TYPE) or namespace
5165 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5166 it unchanged if there is no nested-name-specifier. Returns the new
5167 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5169 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5170 part of a declaration and/or decl-specifier. */
5172 static tree
5173 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5174 bool typename_keyword_p,
5175 bool check_dependency_p,
5176 bool type_p,
5177 bool is_declaration)
5179 bool success = false;
5180 cp_token_position start = 0;
5181 cp_token *token;
5183 /* Remember where the nested-name-specifier starts. */
5184 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5186 start = cp_lexer_token_position (parser->lexer, false);
5187 push_deferring_access_checks (dk_deferred);
5190 while (true)
5192 tree new_scope;
5193 tree old_scope;
5194 tree saved_qualifying_scope;
5195 bool template_keyword_p;
5197 /* Spot cases that cannot be the beginning of a
5198 nested-name-specifier. */
5199 token = cp_lexer_peek_token (parser->lexer);
5201 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5202 the already parsed nested-name-specifier. */
5203 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5205 /* Grab the nested-name-specifier and continue the loop. */
5206 cp_parser_pre_parsed_nested_name_specifier (parser);
5207 /* If we originally encountered this nested-name-specifier
5208 with IS_DECLARATION set to false, we will not have
5209 resolved TYPENAME_TYPEs, so we must do so here. */
5210 if (is_declaration
5211 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5213 new_scope = resolve_typename_type (parser->scope,
5214 /*only_current_p=*/false);
5215 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5216 parser->scope = new_scope;
5218 success = true;
5219 continue;
5222 /* Spot cases that cannot be the beginning of a
5223 nested-name-specifier. On the second and subsequent times
5224 through the loop, we look for the `template' keyword. */
5225 if (success && token->keyword == RID_TEMPLATE)
5227 /* A template-id can start a nested-name-specifier. */
5228 else if (token->type == CPP_TEMPLATE_ID)
5230 /* DR 743: decltype can be used in a nested-name-specifier. */
5231 else if (token_is_decltype (token))
5233 else
5235 /* If the next token is not an identifier, then it is
5236 definitely not a type-name or namespace-name. */
5237 if (token->type != CPP_NAME)
5238 break;
5239 /* If the following token is neither a `<' (to begin a
5240 template-id), nor a `::', then we are not looking at a
5241 nested-name-specifier. */
5242 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5244 if (token->type == CPP_COLON
5245 && parser->colon_corrects_to_scope_p
5246 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5248 error_at (token->location,
5249 "found %<:%> in nested-name-specifier, expected %<::%>");
5250 token->type = CPP_SCOPE;
5253 if (token->type != CPP_SCOPE
5254 && !cp_parser_nth_token_starts_template_argument_list_p
5255 (parser, 2))
5256 break;
5259 /* The nested-name-specifier is optional, so we parse
5260 tentatively. */
5261 cp_parser_parse_tentatively (parser);
5263 /* Look for the optional `template' keyword, if this isn't the
5264 first time through the loop. */
5265 if (success)
5266 template_keyword_p = cp_parser_optional_template_keyword (parser);
5267 else
5268 template_keyword_p = false;
5270 /* Save the old scope since the name lookup we are about to do
5271 might destroy it. */
5272 old_scope = parser->scope;
5273 saved_qualifying_scope = parser->qualifying_scope;
5274 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5275 look up names in "X<T>::I" in order to determine that "Y" is
5276 a template. So, if we have a typename at this point, we make
5277 an effort to look through it. */
5278 if (is_declaration
5279 && !typename_keyword_p
5280 && parser->scope
5281 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5282 parser->scope = resolve_typename_type (parser->scope,
5283 /*only_current_p=*/false);
5284 /* Parse the qualifying entity. */
5285 new_scope
5286 = cp_parser_qualifying_entity (parser,
5287 typename_keyword_p,
5288 template_keyword_p,
5289 check_dependency_p,
5290 type_p,
5291 is_declaration);
5292 /* Look for the `::' token. */
5293 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5295 /* If we found what we wanted, we keep going; otherwise, we're
5296 done. */
5297 if (!cp_parser_parse_definitely (parser))
5299 bool error_p = false;
5301 /* Restore the OLD_SCOPE since it was valid before the
5302 failed attempt at finding the last
5303 class-or-namespace-name. */
5304 parser->scope = old_scope;
5305 parser->qualifying_scope = saved_qualifying_scope;
5307 /* If the next token is a decltype, and the one after that is a
5308 `::', then the decltype has failed to resolve to a class or
5309 enumeration type. Give this error even when parsing
5310 tentatively since it can't possibly be valid--and we're going
5311 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5312 won't get another chance.*/
5313 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5314 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5315 == CPP_SCOPE))
5317 token = cp_lexer_consume_token (parser->lexer);
5318 error_at (token->location, "decltype evaluates to %qT, "
5319 "which is not a class or enumeration type",
5320 token->u.value);
5321 parser->scope = error_mark_node;
5322 error_p = true;
5323 /* As below. */
5324 success = true;
5325 cp_lexer_consume_token (parser->lexer);
5328 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5329 break;
5330 /* If the next token is an identifier, and the one after
5331 that is a `::', then any valid interpretation would have
5332 found a class-or-namespace-name. */
5333 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5334 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5335 == CPP_SCOPE)
5336 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5337 != CPP_COMPL))
5339 token = cp_lexer_consume_token (parser->lexer);
5340 if (!error_p)
5342 if (!token->error_reported)
5344 tree decl;
5345 tree ambiguous_decls;
5347 decl = cp_parser_lookup_name (parser, token->u.value,
5348 none_type,
5349 /*is_template=*/false,
5350 /*is_namespace=*/false,
5351 /*check_dependency=*/true,
5352 &ambiguous_decls,
5353 token->location);
5354 if (TREE_CODE (decl) == TEMPLATE_DECL)
5355 error_at (token->location,
5356 "%qD used without template parameters",
5357 decl);
5358 else if (ambiguous_decls)
5360 // cp_parser_lookup_name has the same diagnostic,
5361 // thus make sure to emit it at most once.
5362 if (cp_parser_uncommitted_to_tentative_parse_p
5363 (parser))
5365 error_at (token->location,
5366 "reference to %qD is ambiguous",
5367 token->u.value);
5368 print_candidates (ambiguous_decls);
5370 decl = error_mark_node;
5372 else
5374 if (cxx_dialect != cxx98)
5375 cp_parser_name_lookup_error
5376 (parser, token->u.value, decl, NLE_NOT_CXX98,
5377 token->location);
5378 else
5379 cp_parser_name_lookup_error
5380 (parser, token->u.value, decl, NLE_CXX98,
5381 token->location);
5384 parser->scope = error_mark_node;
5385 error_p = true;
5386 /* Treat this as a successful nested-name-specifier
5387 due to:
5389 [basic.lookup.qual]
5391 If the name found is not a class-name (clause
5392 _class_) or namespace-name (_namespace.def_), the
5393 program is ill-formed. */
5394 success = true;
5396 cp_lexer_consume_token (parser->lexer);
5398 break;
5400 /* We've found one valid nested-name-specifier. */
5401 success = true;
5402 /* Name lookup always gives us a DECL. */
5403 if (TREE_CODE (new_scope) == TYPE_DECL)
5404 new_scope = TREE_TYPE (new_scope);
5405 /* Uses of "template" must be followed by actual templates. */
5406 if (template_keyword_p
5407 && !(CLASS_TYPE_P (new_scope)
5408 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5409 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5410 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5411 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5412 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5413 == TEMPLATE_ID_EXPR)))
5414 permerror (input_location, TYPE_P (new_scope)
5415 ? G_("%qT is not a template")
5416 : G_("%qD is not a template"),
5417 new_scope);
5418 /* If it is a class scope, try to complete it; we are about to
5419 be looking up names inside the class. */
5420 if (TYPE_P (new_scope)
5421 /* Since checking types for dependency can be expensive,
5422 avoid doing it if the type is already complete. */
5423 && !COMPLETE_TYPE_P (new_scope)
5424 /* Do not try to complete dependent types. */
5425 && !dependent_type_p (new_scope))
5427 new_scope = complete_type (new_scope);
5428 /* If it is a typedef to current class, use the current
5429 class instead, as the typedef won't have any names inside
5430 it yet. */
5431 if (!COMPLETE_TYPE_P (new_scope)
5432 && currently_open_class (new_scope))
5433 new_scope = TYPE_MAIN_VARIANT (new_scope);
5435 /* Make sure we look in the right scope the next time through
5436 the loop. */
5437 parser->scope = new_scope;
5440 /* If parsing tentatively, replace the sequence of tokens that makes
5441 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5442 token. That way, should we re-parse the token stream, we will
5443 not have to repeat the effort required to do the parse, nor will
5444 we issue duplicate error messages. */
5445 if (success && start)
5447 cp_token *token;
5449 token = cp_lexer_token_at (parser->lexer, start);
5450 /* Reset the contents of the START token. */
5451 token->type = CPP_NESTED_NAME_SPECIFIER;
5452 /* Retrieve any deferred checks. Do not pop this access checks yet
5453 so the memory will not be reclaimed during token replacing below. */
5454 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
5455 token->u.tree_check_value->value = parser->scope;
5456 token->u.tree_check_value->checks = get_deferred_access_checks ();
5457 token->u.tree_check_value->qualifying_scope =
5458 parser->qualifying_scope;
5459 token->keyword = RID_MAX;
5461 /* Purge all subsequent tokens. */
5462 cp_lexer_purge_tokens_after (parser->lexer, start);
5465 if (start)
5466 pop_to_parent_deferring_access_checks ();
5468 return success ? parser->scope : NULL_TREE;
5471 /* Parse a nested-name-specifier. See
5472 cp_parser_nested_name_specifier_opt for details. This function
5473 behaves identically, except that it will an issue an error if no
5474 nested-name-specifier is present. */
5476 static tree
5477 cp_parser_nested_name_specifier (cp_parser *parser,
5478 bool typename_keyword_p,
5479 bool check_dependency_p,
5480 bool type_p,
5481 bool is_declaration)
5483 tree scope;
5485 /* Look for the nested-name-specifier. */
5486 scope = cp_parser_nested_name_specifier_opt (parser,
5487 typename_keyword_p,
5488 check_dependency_p,
5489 type_p,
5490 is_declaration);
5491 /* If it was not present, issue an error message. */
5492 if (!scope)
5494 cp_parser_error (parser, "expected nested-name-specifier");
5495 parser->scope = NULL_TREE;
5498 return scope;
5501 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5502 this is either a class-name or a namespace-name (which corresponds
5503 to the class-or-namespace-name production in the grammar). For
5504 C++0x, it can also be a type-name that refers to an enumeration
5505 type or a simple-template-id.
5507 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5508 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5509 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5510 TYPE_P is TRUE iff the next name should be taken as a class-name,
5511 even the same name is declared to be another entity in the same
5512 scope.
5514 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5515 specified by the class-or-namespace-name. If neither is found the
5516 ERROR_MARK_NODE is returned. */
5518 static tree
5519 cp_parser_qualifying_entity (cp_parser *parser,
5520 bool typename_keyword_p,
5521 bool template_keyword_p,
5522 bool check_dependency_p,
5523 bool type_p,
5524 bool is_declaration)
5526 tree saved_scope;
5527 tree saved_qualifying_scope;
5528 tree saved_object_scope;
5529 tree scope;
5530 bool only_class_p;
5531 bool successful_parse_p;
5533 /* DR 743: decltype can appear in a nested-name-specifier. */
5534 if (cp_lexer_next_token_is_decltype (parser->lexer))
5536 scope = cp_parser_decltype (parser);
5537 if (TREE_CODE (scope) != ENUMERAL_TYPE
5538 && !MAYBE_CLASS_TYPE_P (scope))
5540 cp_parser_simulate_error (parser);
5541 return error_mark_node;
5543 if (TYPE_NAME (scope))
5544 scope = TYPE_NAME (scope);
5545 return scope;
5548 /* Before we try to parse the class-name, we must save away the
5549 current PARSER->SCOPE since cp_parser_class_name will destroy
5550 it. */
5551 saved_scope = parser->scope;
5552 saved_qualifying_scope = parser->qualifying_scope;
5553 saved_object_scope = parser->object_scope;
5554 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5555 there is no need to look for a namespace-name. */
5556 only_class_p = template_keyword_p
5557 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5558 if (!only_class_p)
5559 cp_parser_parse_tentatively (parser);
5560 scope = cp_parser_class_name (parser,
5561 typename_keyword_p,
5562 template_keyword_p,
5563 type_p ? class_type : none_type,
5564 check_dependency_p,
5565 /*class_head_p=*/false,
5566 is_declaration);
5567 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5568 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5569 if (!only_class_p
5570 && cxx_dialect != cxx98
5571 && !successful_parse_p)
5573 /* Restore the saved scope. */
5574 parser->scope = saved_scope;
5575 parser->qualifying_scope = saved_qualifying_scope;
5576 parser->object_scope = saved_object_scope;
5578 /* Parse tentatively. */
5579 cp_parser_parse_tentatively (parser);
5581 /* Parse a type-name */
5582 scope = cp_parser_type_name (parser);
5584 /* "If the name found does not designate a namespace or a class,
5585 enumeration, or dependent type, the program is ill-formed."
5587 We cover classes and dependent types above and namespaces below,
5588 so this code is only looking for enums. */
5589 if (!scope || TREE_CODE (scope) != TYPE_DECL
5590 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5591 cp_parser_simulate_error (parser);
5593 successful_parse_p = cp_parser_parse_definitely (parser);
5595 /* If that didn't work, try for a namespace-name. */
5596 if (!only_class_p && !successful_parse_p)
5598 /* Restore the saved scope. */
5599 parser->scope = saved_scope;
5600 parser->qualifying_scope = saved_qualifying_scope;
5601 parser->object_scope = saved_object_scope;
5602 /* If we are not looking at an identifier followed by the scope
5603 resolution operator, then this is not part of a
5604 nested-name-specifier. (Note that this function is only used
5605 to parse the components of a nested-name-specifier.) */
5606 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5607 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5608 return error_mark_node;
5609 scope = cp_parser_namespace_name (parser);
5612 return scope;
5615 /* Return true if we are looking at a compound-literal, false otherwise. */
5617 static bool
5618 cp_parser_compound_literal_p (cp_parser *parser)
5620 /* Consume the `('. */
5621 cp_lexer_consume_token (parser->lexer);
5623 cp_lexer_save_tokens (parser->lexer);
5625 /* Skip tokens until the next token is a closing parenthesis.
5626 If we find the closing `)', and the next token is a `{', then
5627 we are looking at a compound-literal. */
5628 bool compound_literal_p
5629 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5630 /*consume_paren=*/true)
5631 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5633 /* Roll back the tokens we skipped. */
5634 cp_lexer_rollback_tokens (parser->lexer);
5636 return compound_literal_p;
5639 /* Parse a postfix-expression.
5641 postfix-expression:
5642 primary-expression
5643 postfix-expression [ expression ]
5644 postfix-expression ( expression-list [opt] )
5645 simple-type-specifier ( expression-list [opt] )
5646 typename :: [opt] nested-name-specifier identifier
5647 ( expression-list [opt] )
5648 typename :: [opt] nested-name-specifier template [opt] template-id
5649 ( expression-list [opt] )
5650 postfix-expression . template [opt] id-expression
5651 postfix-expression -> template [opt] id-expression
5652 postfix-expression . pseudo-destructor-name
5653 postfix-expression -> pseudo-destructor-name
5654 postfix-expression ++
5655 postfix-expression --
5656 dynamic_cast < type-id > ( expression )
5657 static_cast < type-id > ( expression )
5658 reinterpret_cast < type-id > ( expression )
5659 const_cast < type-id > ( expression )
5660 typeid ( expression )
5661 typeid ( type-id )
5663 GNU Extension:
5665 postfix-expression:
5666 ( type-id ) { initializer-list , [opt] }
5668 This extension is a GNU version of the C99 compound-literal
5669 construct. (The C99 grammar uses `type-name' instead of `type-id',
5670 but they are essentially the same concept.)
5672 If ADDRESS_P is true, the postfix expression is the operand of the
5673 `&' operator. CAST_P is true if this expression is the target of a
5674 cast.
5676 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5677 class member access expressions [expr.ref].
5679 Returns a representation of the expression. */
5681 static tree
5682 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5683 bool member_access_only_p, bool decltype_p,
5684 cp_id_kind * pidk_return)
5686 cp_token *token;
5687 location_t loc;
5688 enum rid keyword;
5689 cp_id_kind idk = CP_ID_KIND_NONE;
5690 tree postfix_expression = NULL_TREE;
5691 bool is_member_access = false;
5692 int saved_in_statement = -1;
5694 /* Peek at the next token. */
5695 token = cp_lexer_peek_token (parser->lexer);
5696 loc = token->location;
5697 /* Some of the productions are determined by keywords. */
5698 keyword = token->keyword;
5699 switch (keyword)
5701 case RID_DYNCAST:
5702 case RID_STATCAST:
5703 case RID_REINTCAST:
5704 case RID_CONSTCAST:
5706 tree type;
5707 tree expression;
5708 const char *saved_message;
5709 bool saved_in_type_id_in_expr_p;
5711 /* All of these can be handled in the same way from the point
5712 of view of parsing. Begin by consuming the token
5713 identifying the cast. */
5714 cp_lexer_consume_token (parser->lexer);
5716 /* New types cannot be defined in the cast. */
5717 saved_message = parser->type_definition_forbidden_message;
5718 parser->type_definition_forbidden_message
5719 = G_("types may not be defined in casts");
5721 /* Look for the opening `<'. */
5722 cp_parser_require (parser, CPP_LESS, RT_LESS);
5723 /* Parse the type to which we are casting. */
5724 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5725 parser->in_type_id_in_expr_p = true;
5726 type = cp_parser_type_id (parser);
5727 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5728 /* Look for the closing `>'. */
5729 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5730 /* Restore the old message. */
5731 parser->type_definition_forbidden_message = saved_message;
5733 bool saved_greater_than_is_operator_p
5734 = parser->greater_than_is_operator_p;
5735 parser->greater_than_is_operator_p = true;
5737 /* And the expression which is being cast. */
5738 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5739 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
5740 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5742 parser->greater_than_is_operator_p
5743 = saved_greater_than_is_operator_p;
5745 /* Only type conversions to integral or enumeration types
5746 can be used in constant-expressions. */
5747 if (!cast_valid_in_integral_constant_expression_p (type)
5748 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5749 return error_mark_node;
5751 switch (keyword)
5753 case RID_DYNCAST:
5754 postfix_expression
5755 = build_dynamic_cast (type, expression, tf_warning_or_error);
5756 break;
5757 case RID_STATCAST:
5758 postfix_expression
5759 = build_static_cast (type, expression, tf_warning_or_error);
5760 break;
5761 case RID_REINTCAST:
5762 postfix_expression
5763 = build_reinterpret_cast (type, expression,
5764 tf_warning_or_error);
5765 break;
5766 case RID_CONSTCAST:
5767 postfix_expression
5768 = build_const_cast (type, expression, tf_warning_or_error);
5769 break;
5770 default:
5771 gcc_unreachable ();
5774 break;
5776 case RID_TYPEID:
5778 tree type;
5779 const char *saved_message;
5780 bool saved_in_type_id_in_expr_p;
5782 /* Consume the `typeid' token. */
5783 cp_lexer_consume_token (parser->lexer);
5784 /* Look for the `(' token. */
5785 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5786 /* Types cannot be defined in a `typeid' expression. */
5787 saved_message = parser->type_definition_forbidden_message;
5788 parser->type_definition_forbidden_message
5789 = G_("types may not be defined in a %<typeid%> expression");
5790 /* We can't be sure yet whether we're looking at a type-id or an
5791 expression. */
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a type-id first. */
5794 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5795 parser->in_type_id_in_expr_p = true;
5796 type = cp_parser_type_id (parser);
5797 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5798 /* Look for the `)' token. Otherwise, we can't be sure that
5799 we're not looking at an expression: consider `typeid (int
5800 (3))', for example. */
5801 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5802 /* If all went well, simply lookup the type-id. */
5803 if (cp_parser_parse_definitely (parser))
5804 postfix_expression = get_typeid (type, tf_warning_or_error);
5805 /* Otherwise, fall back to the expression variant. */
5806 else
5808 tree expression;
5810 /* Look for an expression. */
5811 expression = cp_parser_expression (parser, & idk);
5812 /* Compute its typeid. */
5813 postfix_expression = build_typeid (expression, tf_warning_or_error);
5814 /* Look for the `)' token. */
5815 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5817 /* Restore the saved message. */
5818 parser->type_definition_forbidden_message = saved_message;
5819 /* `typeid' may not appear in an integral constant expression. */
5820 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5821 return error_mark_node;
5823 break;
5825 case RID_TYPENAME:
5827 tree type;
5828 /* The syntax permitted here is the same permitted for an
5829 elaborated-type-specifier. */
5830 type = cp_parser_elaborated_type_specifier (parser,
5831 /*is_friend=*/false,
5832 /*is_declaration=*/false);
5833 postfix_expression = cp_parser_functional_cast (parser, type);
5835 break;
5837 case RID_CILK_SPAWN:
5839 cp_lexer_consume_token (parser->lexer);
5840 token = cp_lexer_peek_token (parser->lexer);
5841 if (token->type == CPP_SEMICOLON)
5843 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
5844 "an expression");
5845 postfix_expression = error_mark_node;
5846 break;
5848 else if (!current_function_decl)
5850 error_at (token->location, "%<_Cilk_spawn%> may only be used "
5851 "inside a function");
5852 postfix_expression = error_mark_node;
5853 break;
5855 else
5857 /* Consecutive _Cilk_spawns are not allowed in a statement. */
5858 saved_in_statement = parser->in_statement;
5859 parser->in_statement |= IN_CILK_SPAWN;
5861 cfun->calls_cilk_spawn = 1;
5862 postfix_expression =
5863 cp_parser_postfix_expression (parser, false, false,
5864 false, false, &idk);
5865 if (!flag_cilkplus)
5867 error_at (token->location, "-fcilkplus must be enabled to use"
5868 " %<_Cilk_spawn%>");
5869 cfun->calls_cilk_spawn = 0;
5871 else if (saved_in_statement & IN_CILK_SPAWN)
5873 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
5874 "are not permitted");
5875 postfix_expression = error_mark_node;
5876 cfun->calls_cilk_spawn = 0;
5878 else
5880 postfix_expression = build_cilk_spawn (token->location,
5881 postfix_expression);
5882 if (postfix_expression != error_mark_node)
5883 SET_EXPR_LOCATION (postfix_expression, input_location);
5884 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
5886 break;
5889 case RID_BUILTIN_SHUFFLE:
5891 vec<tree, va_gc> *vec;
5892 unsigned int i;
5893 tree p;
5895 cp_lexer_consume_token (parser->lexer);
5896 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
5897 /*cast_p=*/false, /*allow_expansion_p=*/true,
5898 /*non_constant_p=*/NULL);
5899 if (vec == NULL)
5900 return error_mark_node;
5902 FOR_EACH_VEC_ELT (*vec, i, p)
5903 mark_exp_read (p);
5905 if (vec->length () == 2)
5906 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
5907 tf_warning_or_error);
5908 else if (vec->length () == 3)
5909 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
5910 tf_warning_or_error);
5911 else
5913 error_at (loc, "wrong number of arguments to "
5914 "%<__builtin_shuffle%>");
5915 return error_mark_node;
5917 break;
5920 default:
5922 tree type;
5924 /* If the next thing is a simple-type-specifier, we may be
5925 looking at a functional cast. We could also be looking at
5926 an id-expression. So, we try the functional cast, and if
5927 that doesn't work we fall back to the primary-expression. */
5928 cp_parser_parse_tentatively (parser);
5929 /* Look for the simple-type-specifier. */
5930 type = cp_parser_simple_type_specifier (parser,
5931 /*decl_specs=*/NULL,
5932 CP_PARSER_FLAGS_NONE);
5933 /* Parse the cast itself. */
5934 if (!cp_parser_error_occurred (parser))
5935 postfix_expression
5936 = cp_parser_functional_cast (parser, type);
5937 /* If that worked, we're done. */
5938 if (cp_parser_parse_definitely (parser))
5939 break;
5941 /* If the functional-cast didn't work out, try a
5942 compound-literal. */
5943 if (cp_parser_allow_gnu_extensions_p (parser)
5944 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5946 tree initializer = NULL_TREE;
5948 cp_parser_parse_tentatively (parser);
5950 /* Avoid calling cp_parser_type_id pointlessly, see comment
5951 in cp_parser_cast_expression about c++/29234. */
5952 if (!cp_parser_compound_literal_p (parser))
5953 cp_parser_simulate_error (parser);
5954 else
5956 /* Parse the type. */
5957 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5958 parser->in_type_id_in_expr_p = true;
5959 type = cp_parser_type_id (parser);
5960 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5961 /* Look for the `)'. */
5962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5965 /* If things aren't going well, there's no need to
5966 keep going. */
5967 if (!cp_parser_error_occurred (parser))
5969 bool non_constant_p;
5970 /* Parse the brace-enclosed initializer list. */
5971 initializer = cp_parser_braced_list (parser,
5972 &non_constant_p);
5974 /* If that worked, we're definitely looking at a
5975 compound-literal expression. */
5976 if (cp_parser_parse_definitely (parser))
5978 /* Warn the user that a compound literal is not
5979 allowed in standard C++. */
5980 pedwarn (input_location, OPT_Wpedantic,
5981 "ISO C++ forbids compound-literals");
5982 /* For simplicity, we disallow compound literals in
5983 constant-expressions. We could
5984 allow compound literals of integer type, whose
5985 initializer was a constant, in constant
5986 expressions. Permitting that usage, as a further
5987 extension, would not change the meaning of any
5988 currently accepted programs. (Of course, as
5989 compound literals are not part of ISO C++, the
5990 standard has nothing to say.) */
5991 if (cp_parser_non_integral_constant_expression (parser,
5992 NIC_NCC))
5994 postfix_expression = error_mark_node;
5995 break;
5997 /* Form the representation of the compound-literal. */
5998 postfix_expression
5999 = finish_compound_literal (type, initializer,
6000 tf_warning_or_error);
6001 break;
6005 /* It must be a primary-expression. */
6006 postfix_expression
6007 = cp_parser_primary_expression (parser, address_p, cast_p,
6008 /*template_arg_p=*/false,
6009 decltype_p,
6010 &idk);
6012 break;
6015 /* Note that we don't need to worry about calling build_cplus_new on a
6016 class-valued CALL_EXPR in decltype when it isn't the end of the
6017 postfix-expression; unary_complex_lvalue will take care of that for
6018 all these cases. */
6020 /* Keep looping until the postfix-expression is complete. */
6021 while (true)
6023 if (idk == CP_ID_KIND_UNQUALIFIED
6024 && identifier_p (postfix_expression)
6025 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6026 /* It is not a Koenig lookup function call. */
6027 postfix_expression
6028 = unqualified_name_lookup_error (postfix_expression);
6030 /* Peek at the next token. */
6031 token = cp_lexer_peek_token (parser->lexer);
6033 switch (token->type)
6035 case CPP_OPEN_SQUARE:
6036 if (cp_next_tokens_can_be_std_attribute_p (parser))
6038 cp_parser_error (parser,
6039 "two consecutive %<[%> shall "
6040 "only introduce an attribute");
6041 return error_mark_node;
6043 postfix_expression
6044 = cp_parser_postfix_open_square_expression (parser,
6045 postfix_expression,
6046 false,
6047 decltype_p);
6048 idk = CP_ID_KIND_NONE;
6049 is_member_access = false;
6050 break;
6052 case CPP_OPEN_PAREN:
6053 /* postfix-expression ( expression-list [opt] ) */
6055 bool koenig_p;
6056 bool is_builtin_constant_p;
6057 bool saved_integral_constant_expression_p = false;
6058 bool saved_non_integral_constant_expression_p = false;
6059 tsubst_flags_t complain = complain_flags (decltype_p);
6060 vec<tree, va_gc> *args;
6062 is_member_access = false;
6064 is_builtin_constant_p
6065 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6066 if (is_builtin_constant_p)
6068 /* The whole point of __builtin_constant_p is to allow
6069 non-constant expressions to appear as arguments. */
6070 saved_integral_constant_expression_p
6071 = parser->integral_constant_expression_p;
6072 saved_non_integral_constant_expression_p
6073 = parser->non_integral_constant_expression_p;
6074 parser->integral_constant_expression_p = false;
6076 args = (cp_parser_parenthesized_expression_list
6077 (parser, non_attr,
6078 /*cast_p=*/false, /*allow_expansion_p=*/true,
6079 /*non_constant_p=*/NULL,
6080 /*want_literal_zero_p=*/warn_memset_transposed_args));
6081 if (is_builtin_constant_p)
6083 parser->integral_constant_expression_p
6084 = saved_integral_constant_expression_p;
6085 parser->non_integral_constant_expression_p
6086 = saved_non_integral_constant_expression_p;
6089 if (args == NULL)
6091 postfix_expression = error_mark_node;
6092 break;
6095 /* Function calls are not permitted in
6096 constant-expressions. */
6097 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6098 && cp_parser_non_integral_constant_expression (parser,
6099 NIC_FUNC_CALL))
6101 postfix_expression = error_mark_node;
6102 release_tree_vector (args);
6103 break;
6106 koenig_p = false;
6107 if (idk == CP_ID_KIND_UNQUALIFIED
6108 || idk == CP_ID_KIND_TEMPLATE_ID)
6110 if (identifier_p (postfix_expression))
6112 if (!args->is_empty ())
6114 koenig_p = true;
6115 if (!any_type_dependent_arguments_p (args))
6116 postfix_expression
6117 = perform_koenig_lookup (postfix_expression, args,
6118 complain);
6120 else
6121 postfix_expression
6122 = unqualified_fn_lookup_error (postfix_expression);
6124 /* We do not perform argument-dependent lookup if
6125 normal lookup finds a non-function, in accordance
6126 with the expected resolution of DR 218. */
6127 else if (!args->is_empty ()
6128 && is_overloaded_fn (postfix_expression))
6130 tree fn = get_first_fn (postfix_expression);
6131 fn = STRIP_TEMPLATE (fn);
6133 /* Do not do argument dependent lookup if regular
6134 lookup finds a member function or a block-scope
6135 function declaration. [basic.lookup.argdep]/3 */
6136 if (!DECL_FUNCTION_MEMBER_P (fn)
6137 && !DECL_LOCAL_FUNCTION_P (fn))
6139 koenig_p = true;
6140 if (!any_type_dependent_arguments_p (args))
6141 postfix_expression
6142 = perform_koenig_lookup (postfix_expression, args,
6143 complain);
6148 if (warn_memset_transposed_args)
6150 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6151 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6152 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6153 && vec_safe_length (args) == 3
6154 && integer_zerop ((*args)[2])
6155 && LITERAL_ZERO_P ((*args)[2])
6156 && !(integer_zerop ((*args)[1])
6157 && LITERAL_ZERO_P ((*args)[1])))
6158 warning (OPT_Wmemset_transposed_args,
6159 "%<memset%> used with constant zero length "
6160 "parameter; this could be due to transposed "
6161 "parameters");
6163 /* Replace LITERAL_ZERO_P INTEGER_CSTs with normal ones
6164 to avoid leaking those into folder and middle-end. */
6165 unsigned int i;
6166 tree arg;
6167 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6168 if (TREE_CODE (arg) == INTEGER_CST && LITERAL_ZERO_P (arg))
6169 (*args)[i] = build_int_cst (TREE_TYPE (arg), 0);
6172 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6174 tree instance = TREE_OPERAND (postfix_expression, 0);
6175 tree fn = TREE_OPERAND (postfix_expression, 1);
6177 if (processing_template_decl
6178 && (type_dependent_expression_p (instance)
6179 || (!BASELINK_P (fn)
6180 && TREE_CODE (fn) != FIELD_DECL)
6181 || type_dependent_expression_p (fn)
6182 || any_type_dependent_arguments_p (args)))
6184 postfix_expression
6185 = build_nt_call_vec (postfix_expression, args);
6186 release_tree_vector (args);
6187 break;
6190 if (BASELINK_P (fn))
6192 postfix_expression
6193 = (build_new_method_call
6194 (instance, fn, &args, NULL_TREE,
6195 (idk == CP_ID_KIND_QUALIFIED
6196 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6197 : LOOKUP_NORMAL),
6198 /*fn_p=*/NULL,
6199 complain));
6201 else
6202 postfix_expression
6203 = finish_call_expr (postfix_expression, &args,
6204 /*disallow_virtual=*/false,
6205 /*koenig_p=*/false,
6206 complain);
6208 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6209 || TREE_CODE (postfix_expression) == MEMBER_REF
6210 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6211 postfix_expression = (build_offset_ref_call_from_tree
6212 (postfix_expression, &args,
6213 complain));
6214 else if (idk == CP_ID_KIND_QUALIFIED)
6215 /* A call to a static class member, or a namespace-scope
6216 function. */
6217 postfix_expression
6218 = finish_call_expr (postfix_expression, &args,
6219 /*disallow_virtual=*/true,
6220 koenig_p,
6221 complain);
6222 else
6223 /* All other function calls. */
6224 postfix_expression
6225 = finish_call_expr (postfix_expression, &args,
6226 /*disallow_virtual=*/false,
6227 koenig_p,
6228 complain);
6230 protected_set_expr_location (postfix_expression, token->location);
6232 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6233 idk = CP_ID_KIND_NONE;
6235 release_tree_vector (args);
6237 break;
6239 case CPP_DOT:
6240 case CPP_DEREF:
6241 /* postfix-expression . template [opt] id-expression
6242 postfix-expression . pseudo-destructor-name
6243 postfix-expression -> template [opt] id-expression
6244 postfix-expression -> pseudo-destructor-name */
6246 /* Consume the `.' or `->' operator. */
6247 cp_lexer_consume_token (parser->lexer);
6249 postfix_expression
6250 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6251 postfix_expression,
6252 false, &idk, loc);
6254 is_member_access = true;
6255 break;
6257 case CPP_PLUS_PLUS:
6258 /* postfix-expression ++ */
6259 /* Consume the `++' token. */
6260 cp_lexer_consume_token (parser->lexer);
6261 /* Generate a representation for the complete expression. */
6262 postfix_expression
6263 = finish_increment_expr (postfix_expression,
6264 POSTINCREMENT_EXPR);
6265 /* Increments may not appear in constant-expressions. */
6266 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6267 postfix_expression = error_mark_node;
6268 idk = CP_ID_KIND_NONE;
6269 is_member_access = false;
6270 break;
6272 case CPP_MINUS_MINUS:
6273 /* postfix-expression -- */
6274 /* Consume the `--' token. */
6275 cp_lexer_consume_token (parser->lexer);
6276 /* Generate a representation for the complete expression. */
6277 postfix_expression
6278 = finish_increment_expr (postfix_expression,
6279 POSTDECREMENT_EXPR);
6280 /* Decrements may not appear in constant-expressions. */
6281 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6282 postfix_expression = error_mark_node;
6283 idk = CP_ID_KIND_NONE;
6284 is_member_access = false;
6285 break;
6287 default:
6288 if (pidk_return != NULL)
6289 * pidk_return = idk;
6290 if (member_access_only_p)
6291 return is_member_access? postfix_expression : error_mark_node;
6292 else
6293 return postfix_expression;
6297 /* We should never get here. */
6298 gcc_unreachable ();
6299 return error_mark_node;
6302 /* This function parses Cilk Plus array notations. If a normal array expr. is
6303 parsed then the array index is passed back to the caller through *INIT_INDEX
6304 and the function returns a NULL_TREE. If array notation expr. is parsed,
6305 then *INIT_INDEX is ignored by the caller and the function returns
6306 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6307 error_mark_node. */
6309 static tree
6310 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6311 tree array_value)
6313 cp_token *token = NULL;
6314 tree length_index, stride = NULL_TREE, value_tree, array_type;
6315 if (!array_value || array_value == error_mark_node)
6317 cp_parser_skip_to_end_of_statement (parser);
6318 return error_mark_node;
6321 array_type = TREE_TYPE (array_value);
6323 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
6324 parser->colon_corrects_to_scope_p = false;
6325 token = cp_lexer_peek_token (parser->lexer);
6327 if (!token)
6329 cp_parser_error (parser, "expected %<:%> or numeral");
6330 return error_mark_node;
6332 else if (token->type == CPP_COLON)
6334 /* Consume the ':'. */
6335 cp_lexer_consume_token (parser->lexer);
6337 /* If we are here, then we have a case like this A[:]. */
6338 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
6340 cp_parser_error (parser, "expected %<]%>");
6341 cp_parser_skip_to_end_of_statement (parser);
6342 return error_mark_node;
6344 *init_index = NULL_TREE;
6345 stride = NULL_TREE;
6346 length_index = NULL_TREE;
6348 else
6350 /* If we are here, then there are three valid possibilities:
6351 1. ARRAY [ EXP ]
6352 2. ARRAY [ EXP : EXP ]
6353 3. ARRAY [ EXP : EXP : EXP ] */
6355 *init_index = cp_parser_expression (parser);
6356 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
6358 /* This indicates that we have a normal array expression. */
6359 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6360 return NULL_TREE;
6363 /* Consume the ':'. */
6364 cp_lexer_consume_token (parser->lexer);
6365 length_index = cp_parser_expression (parser);
6366 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6368 cp_lexer_consume_token (parser->lexer);
6369 stride = cp_parser_expression (parser);
6372 parser->colon_corrects_to_scope_p = saved_colon_corrects;
6374 if (*init_index == error_mark_node || length_index == error_mark_node
6375 || stride == error_mark_node || array_type == error_mark_node)
6377 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
6378 cp_lexer_consume_token (parser->lexer);
6379 return error_mark_node;
6381 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6383 value_tree = build_array_notation_ref (loc, array_value, *init_index,
6384 length_index, stride, array_type);
6385 return value_tree;
6388 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6389 by cp_parser_builtin_offsetof. We're looking for
6391 postfix-expression [ expression ]
6392 postfix-expression [ braced-init-list ] (C++11)
6394 FOR_OFFSETOF is set if we're being called in that context, which
6395 changes how we deal with integer constant expressions. */
6397 static tree
6398 cp_parser_postfix_open_square_expression (cp_parser *parser,
6399 tree postfix_expression,
6400 bool for_offsetof,
6401 bool decltype_p)
6403 tree index = NULL_TREE;
6404 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6405 bool saved_greater_than_is_operator_p;
6407 /* Consume the `[' token. */
6408 cp_lexer_consume_token (parser->lexer);
6410 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
6411 parser->greater_than_is_operator_p = true;
6413 /* Parse the index expression. */
6414 /* ??? For offsetof, there is a question of what to allow here. If
6415 offsetof is not being used in an integral constant expression context,
6416 then we *could* get the right answer by computing the value at runtime.
6417 If we are in an integral constant expression context, then we might
6418 could accept any constant expression; hard to say without analysis.
6419 Rather than open the barn door too wide right away, allow only integer
6420 constant expressions here. */
6421 if (for_offsetof)
6422 index = cp_parser_constant_expression (parser, false, NULL);
6423 else
6425 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6427 bool expr_nonconst_p;
6428 cp_lexer_set_source_position (parser->lexer);
6429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6430 index = cp_parser_braced_list (parser, &expr_nonconst_p);
6431 if (flag_cilkplus
6432 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
6434 error_at (cp_lexer_peek_token (parser->lexer)->location,
6435 "braced list index is not allowed with array "
6436 "notation");
6437 cp_parser_skip_to_end_of_statement (parser);
6438 return error_mark_node;
6441 else if (flag_cilkplus)
6443 /* Here are have these two options:
6444 ARRAY[EXP : EXP] - Array notation expr with default
6445 stride of 1.
6446 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
6447 stride. */
6448 tree an_exp = cp_parser_array_notation (loc, parser, &index,
6449 postfix_expression);
6450 if (an_exp)
6451 return an_exp;
6453 else
6454 index = cp_parser_expression (parser);
6457 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
6459 /* Look for the closing `]'. */
6460 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6462 /* Build the ARRAY_REF. */
6463 postfix_expression = grok_array_decl (loc, postfix_expression,
6464 index, decltype_p);
6466 /* When not doing offsetof, array references are not permitted in
6467 constant-expressions. */
6468 if (!for_offsetof
6469 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
6470 postfix_expression = error_mark_node;
6472 return postfix_expression;
6475 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
6476 by cp_parser_builtin_offsetof. We're looking for
6478 postfix-expression . template [opt] id-expression
6479 postfix-expression . pseudo-destructor-name
6480 postfix-expression -> template [opt] id-expression
6481 postfix-expression -> pseudo-destructor-name
6483 FOR_OFFSETOF is set if we're being called in that context. That sorta
6484 limits what of the above we'll actually accept, but nevermind.
6485 TOKEN_TYPE is the "." or "->" token, which will already have been
6486 removed from the stream. */
6488 static tree
6489 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
6490 enum cpp_ttype token_type,
6491 tree postfix_expression,
6492 bool for_offsetof, cp_id_kind *idk,
6493 location_t location)
6495 tree name;
6496 bool dependent_p;
6497 bool pseudo_destructor_p;
6498 tree scope = NULL_TREE;
6500 /* If this is a `->' operator, dereference the pointer. */
6501 if (token_type == CPP_DEREF)
6502 postfix_expression = build_x_arrow (location, postfix_expression,
6503 tf_warning_or_error);
6504 /* Check to see whether or not the expression is type-dependent. */
6505 dependent_p = type_dependent_expression_p (postfix_expression);
6506 /* The identifier following the `->' or `.' is not qualified. */
6507 parser->scope = NULL_TREE;
6508 parser->qualifying_scope = NULL_TREE;
6509 parser->object_scope = NULL_TREE;
6510 *idk = CP_ID_KIND_NONE;
6512 /* Enter the scope corresponding to the type of the object
6513 given by the POSTFIX_EXPRESSION. */
6514 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
6516 scope = TREE_TYPE (postfix_expression);
6517 /* According to the standard, no expression should ever have
6518 reference type. Unfortunately, we do not currently match
6519 the standard in this respect in that our internal representation
6520 of an expression may have reference type even when the standard
6521 says it does not. Therefore, we have to manually obtain the
6522 underlying type here. */
6523 scope = non_reference (scope);
6524 /* The type of the POSTFIX_EXPRESSION must be complete. */
6525 if (scope == unknown_type_node)
6527 error_at (location, "%qE does not have class type",
6528 postfix_expression);
6529 scope = NULL_TREE;
6531 /* Unlike the object expression in other contexts, *this is not
6532 required to be of complete type for purposes of class member
6533 access (5.2.5) outside the member function body. */
6534 else if (postfix_expression != current_class_ref
6535 && !(processing_template_decl && scope == current_class_type))
6536 scope = complete_type_or_else (scope, NULL_TREE);
6537 /* Let the name lookup machinery know that we are processing a
6538 class member access expression. */
6539 parser->context->object_type = scope;
6540 /* If something went wrong, we want to be able to discern that case,
6541 as opposed to the case where there was no SCOPE due to the type
6542 of expression being dependent. */
6543 if (!scope)
6544 scope = error_mark_node;
6545 /* If the SCOPE was erroneous, make the various semantic analysis
6546 functions exit quickly -- and without issuing additional error
6547 messages. */
6548 if (scope == error_mark_node)
6549 postfix_expression = error_mark_node;
6552 /* Assume this expression is not a pseudo-destructor access. */
6553 pseudo_destructor_p = false;
6555 /* If the SCOPE is a scalar type, then, if this is a valid program,
6556 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
6557 is type dependent, it can be pseudo-destructor-name or something else.
6558 Try to parse it as pseudo-destructor-name first. */
6559 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
6561 tree s;
6562 tree type;
6564 cp_parser_parse_tentatively (parser);
6565 /* Parse the pseudo-destructor-name. */
6566 s = NULL_TREE;
6567 cp_parser_pseudo_destructor_name (parser, postfix_expression,
6568 &s, &type);
6569 if (dependent_p
6570 && (cp_parser_error_occurred (parser)
6571 || !SCALAR_TYPE_P (type)))
6572 cp_parser_abort_tentative_parse (parser);
6573 else if (cp_parser_parse_definitely (parser))
6575 pseudo_destructor_p = true;
6576 postfix_expression
6577 = finish_pseudo_destructor_expr (postfix_expression,
6578 s, type, location);
6582 if (!pseudo_destructor_p)
6584 /* If the SCOPE is not a scalar type, we are looking at an
6585 ordinary class member access expression, rather than a
6586 pseudo-destructor-name. */
6587 bool template_p;
6588 cp_token *token = cp_lexer_peek_token (parser->lexer);
6589 /* Parse the id-expression. */
6590 name = (cp_parser_id_expression
6591 (parser,
6592 cp_parser_optional_template_keyword (parser),
6593 /*check_dependency_p=*/true,
6594 &template_p,
6595 /*declarator_p=*/false,
6596 /*optional_p=*/false));
6597 /* In general, build a SCOPE_REF if the member name is qualified.
6598 However, if the name was not dependent and has already been
6599 resolved; there is no need to build the SCOPE_REF. For example;
6601 struct X { void f(); };
6602 template <typename T> void f(T* t) { t->X::f(); }
6604 Even though "t" is dependent, "X::f" is not and has been resolved
6605 to a BASELINK; there is no need to include scope information. */
6607 /* But we do need to remember that there was an explicit scope for
6608 virtual function calls. */
6609 if (parser->scope)
6610 *idk = CP_ID_KIND_QUALIFIED;
6612 /* If the name is a template-id that names a type, we will get a
6613 TYPE_DECL here. That is invalid code. */
6614 if (TREE_CODE (name) == TYPE_DECL)
6616 error_at (token->location, "invalid use of %qD", name);
6617 postfix_expression = error_mark_node;
6619 else
6621 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6623 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6625 error_at (token->location, "%<%D::%D%> is not a class member",
6626 parser->scope, name);
6627 postfix_expression = error_mark_node;
6629 else
6630 name = build_qualified_name (/*type=*/NULL_TREE,
6631 parser->scope,
6632 name,
6633 template_p);
6634 parser->scope = NULL_TREE;
6635 parser->qualifying_scope = NULL_TREE;
6636 parser->object_scope = NULL_TREE;
6638 if (parser->scope && name && BASELINK_P (name))
6639 adjust_result_of_qualified_name_lookup
6640 (name, parser->scope, scope);
6641 postfix_expression
6642 = finish_class_member_access_expr (postfix_expression, name,
6643 template_p,
6644 tf_warning_or_error);
6648 /* We no longer need to look up names in the scope of the object on
6649 the left-hand side of the `.' or `->' operator. */
6650 parser->context->object_type = NULL_TREE;
6652 /* Outside of offsetof, these operators may not appear in
6653 constant-expressions. */
6654 if (!for_offsetof
6655 && (cp_parser_non_integral_constant_expression
6656 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6657 postfix_expression = error_mark_node;
6659 return postfix_expression;
6662 /* Cache of LITERAL_ZERO_P constants. */
6664 static GTY(()) tree literal_zeros[itk_none];
6666 /* Parse a parenthesized expression-list.
6668 expression-list:
6669 assignment-expression
6670 expression-list, assignment-expression
6672 attribute-list:
6673 expression-list
6674 identifier
6675 identifier, expression-list
6677 CAST_P is true if this expression is the target of a cast.
6679 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6680 argument pack.
6682 Returns a vector of trees. Each element is a representation of an
6683 assignment-expression. NULL is returned if the ( and or ) are
6684 missing. An empty, but allocated, vector is returned on no
6685 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6686 if we are parsing an attribute list for an attribute that wants a
6687 plain identifier argument, normal_attr for an attribute that wants
6688 an expression, or non_attr if we aren't parsing an attribute list. If
6689 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6690 not all of the expressions in the list were constant.
6691 WANT_LITERAL_ZERO_P is true if the caller is interested in
6692 LITERAL_ZERO_P INTEGER_CSTs. FIXME: once we don't fold everything
6693 immediately, this can be removed. */
6695 static vec<tree, va_gc> *
6696 cp_parser_parenthesized_expression_list (cp_parser* parser,
6697 int is_attribute_list,
6698 bool cast_p,
6699 bool allow_expansion_p,
6700 bool *non_constant_p,
6701 bool want_literal_zero_p)
6703 vec<tree, va_gc> *expression_list;
6704 bool fold_expr_p = is_attribute_list != non_attr;
6705 tree identifier = NULL_TREE;
6706 bool saved_greater_than_is_operator_p;
6708 /* Assume all the expressions will be constant. */
6709 if (non_constant_p)
6710 *non_constant_p = false;
6712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6713 return NULL;
6715 expression_list = make_tree_vector ();
6717 /* Within a parenthesized expression, a `>' token is always
6718 the greater-than operator. */
6719 saved_greater_than_is_operator_p
6720 = parser->greater_than_is_operator_p;
6721 parser->greater_than_is_operator_p = true;
6723 /* Consume expressions until there are no more. */
6724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6725 while (true)
6727 tree expr;
6729 /* At the beginning of attribute lists, check to see if the
6730 next token is an identifier. */
6731 if (is_attribute_list == id_attr
6732 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6734 cp_token *token;
6736 /* Consume the identifier. */
6737 token = cp_lexer_consume_token (parser->lexer);
6738 /* Save the identifier. */
6739 identifier = token->u.value;
6741 else
6743 bool expr_non_constant_p;
6745 /* Parse the next assignment-expression. */
6746 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6748 /* A braced-init-list. */
6749 cp_lexer_set_source_position (parser->lexer);
6750 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6751 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6752 if (non_constant_p && expr_non_constant_p)
6753 *non_constant_p = true;
6755 else if (non_constant_p)
6757 expr = (cp_parser_constant_expression
6758 (parser, /*allow_non_constant_p=*/true,
6759 &expr_non_constant_p));
6760 if (expr_non_constant_p)
6761 *non_constant_p = true;
6763 else
6765 expr = NULL_TREE;
6766 cp_token *tok = cp_lexer_peek_token (parser->lexer);
6767 switch (tok->type)
6769 case CPP_NUMBER:
6770 case CPP_CHAR:
6771 case CPP_WCHAR:
6772 case CPP_CHAR16:
6773 case CPP_CHAR32:
6774 /* If a parameter is literal zero alone, remember it
6775 for -Wmemset-transposed-args warning. */
6776 if (integer_zerop (tok->u.value)
6777 && !TREE_OVERFLOW (tok->u.value)
6778 && want_literal_zero_p
6779 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6780 == CPP_COMMA
6781 || cp_lexer_peek_nth_token (parser->lexer, 2)->type
6782 == CPP_CLOSE_PAREN))
6784 unsigned int i;
6785 for (i = 0; i < itk_none; ++i)
6786 if (TREE_TYPE (tok->u.value) == integer_types[i])
6787 break;
6788 if (i < itk_none && literal_zeros[i])
6789 expr = literal_zeros[i];
6790 else
6792 expr = copy_node (tok->u.value);
6793 LITERAL_ZERO_P (expr) = 1;
6794 if (i < itk_none)
6795 literal_zeros[i] = expr;
6797 /* Consume the 0 token (or '\0', 0LL etc.). */
6798 cp_lexer_consume_token (parser->lexer);
6800 break;
6801 default:
6802 break;
6804 if (expr == NULL_TREE)
6805 expr = cp_parser_assignment_expression (parser, cast_p,
6806 NULL);
6809 if (fold_expr_p)
6810 expr = fold_non_dependent_expr (expr);
6812 /* If we have an ellipsis, then this is an expression
6813 expansion. */
6814 if (allow_expansion_p
6815 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6817 /* Consume the `...'. */
6818 cp_lexer_consume_token (parser->lexer);
6820 /* Build the argument pack. */
6821 expr = make_pack_expansion (expr);
6824 /* Add it to the list. We add error_mark_node
6825 expressions to the list, so that we can still tell if
6826 the correct form for a parenthesized expression-list
6827 is found. That gives better errors. */
6828 vec_safe_push (expression_list, expr);
6830 if (expr == error_mark_node)
6831 goto skip_comma;
6834 /* After the first item, attribute lists look the same as
6835 expression lists. */
6836 is_attribute_list = non_attr;
6838 get_comma:;
6839 /* If the next token isn't a `,', then we are done. */
6840 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6841 break;
6843 /* Otherwise, consume the `,' and keep going. */
6844 cp_lexer_consume_token (parser->lexer);
6847 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6849 int ending;
6851 skip_comma:;
6852 /* We try and resync to an unnested comma, as that will give the
6853 user better diagnostics. */
6854 ending = cp_parser_skip_to_closing_parenthesis (parser,
6855 /*recovering=*/true,
6856 /*or_comma=*/true,
6857 /*consume_paren=*/true);
6858 if (ending < 0)
6859 goto get_comma;
6860 if (!ending)
6862 parser->greater_than_is_operator_p
6863 = saved_greater_than_is_operator_p;
6864 return NULL;
6868 parser->greater_than_is_operator_p
6869 = saved_greater_than_is_operator_p;
6871 if (identifier)
6872 vec_safe_insert (expression_list, 0, identifier);
6874 return expression_list;
6877 /* Parse a pseudo-destructor-name.
6879 pseudo-destructor-name:
6880 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6881 :: [opt] nested-name-specifier template template-id :: ~ type-name
6882 :: [opt] nested-name-specifier [opt] ~ type-name
6884 If either of the first two productions is used, sets *SCOPE to the
6885 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6886 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6887 or ERROR_MARK_NODE if the parse fails. */
6889 static void
6890 cp_parser_pseudo_destructor_name (cp_parser* parser,
6891 tree object,
6892 tree* scope,
6893 tree* type)
6895 bool nested_name_specifier_p;
6897 /* Handle ~auto. */
6898 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
6899 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
6900 && !type_dependent_expression_p (object))
6902 if (cxx_dialect < cxx14)
6903 pedwarn (input_location, 0,
6904 "%<~auto%> only available with "
6905 "-std=c++14 or -std=gnu++14");
6906 cp_lexer_consume_token (parser->lexer);
6907 cp_lexer_consume_token (parser->lexer);
6908 *scope = NULL_TREE;
6909 *type = TREE_TYPE (object);
6910 return;
6913 /* Assume that things will not work out. */
6914 *type = error_mark_node;
6916 /* Look for the optional `::' operator. */
6917 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6918 /* Look for the optional nested-name-specifier. */
6919 nested_name_specifier_p
6920 = (cp_parser_nested_name_specifier_opt (parser,
6921 /*typename_keyword_p=*/false,
6922 /*check_dependency_p=*/true,
6923 /*type_p=*/false,
6924 /*is_declaration=*/false)
6925 != NULL_TREE);
6926 /* Now, if we saw a nested-name-specifier, we might be doing the
6927 second production. */
6928 if (nested_name_specifier_p
6929 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6931 /* Consume the `template' keyword. */
6932 cp_lexer_consume_token (parser->lexer);
6933 /* Parse the template-id. */
6934 cp_parser_template_id (parser,
6935 /*template_keyword_p=*/true,
6936 /*check_dependency_p=*/false,
6937 class_type,
6938 /*is_declaration=*/true);
6939 /* Look for the `::' token. */
6940 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6942 /* If the next token is not a `~', then there might be some
6943 additional qualification. */
6944 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6946 /* At this point, we're looking for "type-name :: ~". The type-name
6947 must not be a class-name, since this is a pseudo-destructor. So,
6948 it must be either an enum-name, or a typedef-name -- both of which
6949 are just identifiers. So, we peek ahead to check that the "::"
6950 and "~" tokens are present; if they are not, then we can avoid
6951 calling type_name. */
6952 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6953 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6954 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6956 cp_parser_error (parser, "non-scalar type");
6957 return;
6960 /* Look for the type-name. */
6961 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6962 if (*scope == error_mark_node)
6963 return;
6965 /* Look for the `::' token. */
6966 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6968 else
6969 *scope = NULL_TREE;
6971 /* Look for the `~'. */
6972 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6974 /* Once we see the ~, this has to be a pseudo-destructor. */
6975 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6976 cp_parser_commit_to_topmost_tentative_parse (parser);
6978 /* Look for the type-name again. We are not responsible for
6979 checking that it matches the first type-name. */
6980 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
6983 /* Parse a unary-expression.
6985 unary-expression:
6986 postfix-expression
6987 ++ cast-expression
6988 -- cast-expression
6989 unary-operator cast-expression
6990 sizeof unary-expression
6991 sizeof ( type-id )
6992 alignof ( type-id ) [C++0x]
6993 new-expression
6994 delete-expression
6996 GNU Extensions:
6998 unary-expression:
6999 __extension__ cast-expression
7000 __alignof__ unary-expression
7001 __alignof__ ( type-id )
7002 alignof unary-expression [C++0x]
7003 __real__ cast-expression
7004 __imag__ cast-expression
7005 && identifier
7006 sizeof ( type-id ) { initializer-list , [opt] }
7007 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7008 __alignof__ ( type-id ) { initializer-list , [opt] }
7010 ADDRESS_P is true iff the unary-expression is appearing as the
7011 operand of the `&' operator. CAST_P is true if this expression is
7012 the target of a cast.
7014 Returns a representation of the expression. */
7016 static tree
7017 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7018 bool decltype_p, cp_id_kind * pidk)
7020 cp_token *token;
7021 enum tree_code unary_operator;
7023 /* Peek at the next token. */
7024 token = cp_lexer_peek_token (parser->lexer);
7025 /* Some keywords give away the kind of expression. */
7026 if (token->type == CPP_KEYWORD)
7028 enum rid keyword = token->keyword;
7030 switch (keyword)
7032 case RID_ALIGNOF:
7033 case RID_SIZEOF:
7035 tree operand, ret;
7036 enum tree_code op;
7037 location_t first_loc;
7039 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7040 /* Consume the token. */
7041 cp_lexer_consume_token (parser->lexer);
7042 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7043 /* Parse the operand. */
7044 operand = cp_parser_sizeof_operand (parser, keyword);
7046 if (TYPE_P (operand))
7047 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7048 else
7050 /* ISO C++ defines alignof only with types, not with
7051 expressions. So pedwarn if alignof is used with a non-
7052 type expression. However, __alignof__ is ok. */
7053 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7054 pedwarn (token->location, OPT_Wpedantic,
7055 "ISO C++ does not allow %<alignof%> "
7056 "with a non-type");
7058 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7060 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7061 SIZEOF_EXPR with the original operand. */
7062 if (op == SIZEOF_EXPR && ret != error_mark_node)
7064 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7066 if (!processing_template_decl && TYPE_P (operand))
7068 ret = build_min (SIZEOF_EXPR, size_type_node,
7069 build1 (NOP_EXPR, operand,
7070 error_mark_node));
7071 SIZEOF_EXPR_TYPE_P (ret) = 1;
7073 else
7074 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7075 TREE_SIDE_EFFECTS (ret) = 0;
7076 TREE_READONLY (ret) = 1;
7078 SET_EXPR_LOCATION (ret, first_loc);
7080 return ret;
7083 case RID_NEW:
7084 return cp_parser_new_expression (parser);
7086 case RID_DELETE:
7087 return cp_parser_delete_expression (parser);
7089 case RID_EXTENSION:
7091 /* The saved value of the PEDANTIC flag. */
7092 int saved_pedantic;
7093 tree expr;
7095 /* Save away the PEDANTIC flag. */
7096 cp_parser_extension_opt (parser, &saved_pedantic);
7097 /* Parse the cast-expression. */
7098 expr = cp_parser_simple_cast_expression (parser);
7099 /* Restore the PEDANTIC flag. */
7100 pedantic = saved_pedantic;
7102 return expr;
7105 case RID_REALPART:
7106 case RID_IMAGPART:
7108 tree expression;
7110 /* Consume the `__real__' or `__imag__' token. */
7111 cp_lexer_consume_token (parser->lexer);
7112 /* Parse the cast-expression. */
7113 expression = cp_parser_simple_cast_expression (parser);
7114 /* Create the complete representation. */
7115 return build_x_unary_op (token->location,
7116 (keyword == RID_REALPART
7117 ? REALPART_EXPR : IMAGPART_EXPR),
7118 expression,
7119 tf_warning_or_error);
7121 break;
7123 case RID_TRANSACTION_ATOMIC:
7124 case RID_TRANSACTION_RELAXED:
7125 return cp_parser_transaction_expression (parser, keyword);
7127 case RID_NOEXCEPT:
7129 tree expr;
7130 const char *saved_message;
7131 bool saved_integral_constant_expression_p;
7132 bool saved_non_integral_constant_expression_p;
7133 bool saved_greater_than_is_operator_p;
7135 cp_lexer_consume_token (parser->lexer);
7136 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7138 saved_message = parser->type_definition_forbidden_message;
7139 parser->type_definition_forbidden_message
7140 = G_("types may not be defined in %<noexcept%> expressions");
7142 saved_integral_constant_expression_p
7143 = parser->integral_constant_expression_p;
7144 saved_non_integral_constant_expression_p
7145 = parser->non_integral_constant_expression_p;
7146 parser->integral_constant_expression_p = false;
7148 saved_greater_than_is_operator_p
7149 = parser->greater_than_is_operator_p;
7150 parser->greater_than_is_operator_p = true;
7152 ++cp_unevaluated_operand;
7153 ++c_inhibit_evaluation_warnings;
7154 expr = cp_parser_expression (parser);
7155 --c_inhibit_evaluation_warnings;
7156 --cp_unevaluated_operand;
7158 parser->greater_than_is_operator_p
7159 = saved_greater_than_is_operator_p;
7161 parser->integral_constant_expression_p
7162 = saved_integral_constant_expression_p;
7163 parser->non_integral_constant_expression_p
7164 = saved_non_integral_constant_expression_p;
7166 parser->type_definition_forbidden_message = saved_message;
7168 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7169 return finish_noexcept_expr (expr, tf_warning_or_error);
7172 default:
7173 break;
7177 /* Look for the `:: new' and `:: delete', which also signal the
7178 beginning of a new-expression, or delete-expression,
7179 respectively. If the next token is `::', then it might be one of
7180 these. */
7181 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7183 enum rid keyword;
7185 /* See if the token after the `::' is one of the keywords in
7186 which we're interested. */
7187 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7188 /* If it's `new', we have a new-expression. */
7189 if (keyword == RID_NEW)
7190 return cp_parser_new_expression (parser);
7191 /* Similarly, for `delete'. */
7192 else if (keyword == RID_DELETE)
7193 return cp_parser_delete_expression (parser);
7196 /* Look for a unary operator. */
7197 unary_operator = cp_parser_unary_operator (token);
7198 /* The `++' and `--' operators can be handled similarly, even though
7199 they are not technically unary-operators in the grammar. */
7200 if (unary_operator == ERROR_MARK)
7202 if (token->type == CPP_PLUS_PLUS)
7203 unary_operator = PREINCREMENT_EXPR;
7204 else if (token->type == CPP_MINUS_MINUS)
7205 unary_operator = PREDECREMENT_EXPR;
7206 /* Handle the GNU address-of-label extension. */
7207 else if (cp_parser_allow_gnu_extensions_p (parser)
7208 && token->type == CPP_AND_AND)
7210 tree identifier;
7211 tree expression;
7212 location_t loc = token->location;
7214 /* Consume the '&&' token. */
7215 cp_lexer_consume_token (parser->lexer);
7216 /* Look for the identifier. */
7217 identifier = cp_parser_identifier (parser);
7218 /* Create an expression representing the address. */
7219 expression = finish_label_address_expr (identifier, loc);
7220 if (cp_parser_non_integral_constant_expression (parser,
7221 NIC_ADDR_LABEL))
7222 expression = error_mark_node;
7223 return expression;
7226 if (unary_operator != ERROR_MARK)
7228 tree cast_expression;
7229 tree expression = error_mark_node;
7230 non_integral_constant non_constant_p = NIC_NONE;
7231 location_t loc = token->location;
7232 tsubst_flags_t complain = complain_flags (decltype_p);
7234 /* Consume the operator token. */
7235 token = cp_lexer_consume_token (parser->lexer);
7236 /* Parse the cast-expression. */
7237 cast_expression
7238 = cp_parser_cast_expression (parser,
7239 unary_operator == ADDR_EXPR,
7240 /*cast_p=*/false,
7241 /*decltype*/false,
7242 pidk);
7243 /* Now, build an appropriate representation. */
7244 switch (unary_operator)
7246 case INDIRECT_REF:
7247 non_constant_p = NIC_STAR;
7248 expression = build_x_indirect_ref (loc, cast_expression,
7249 RO_UNARY_STAR,
7250 complain);
7251 break;
7253 case ADDR_EXPR:
7254 non_constant_p = NIC_ADDR;
7255 /* Fall through. */
7256 case BIT_NOT_EXPR:
7257 expression = build_x_unary_op (loc, unary_operator,
7258 cast_expression,
7259 complain);
7260 break;
7262 case PREINCREMENT_EXPR:
7263 case PREDECREMENT_EXPR:
7264 non_constant_p = unary_operator == PREINCREMENT_EXPR
7265 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7266 /* Fall through. */
7267 case UNARY_PLUS_EXPR:
7268 case NEGATE_EXPR:
7269 case TRUTH_NOT_EXPR:
7270 expression = finish_unary_op_expr (loc, unary_operator,
7271 cast_expression, complain);
7272 break;
7274 default:
7275 gcc_unreachable ();
7278 if (non_constant_p != NIC_NONE
7279 && cp_parser_non_integral_constant_expression (parser,
7280 non_constant_p))
7281 expression = error_mark_node;
7283 return expression;
7286 return cp_parser_postfix_expression (parser, address_p, cast_p,
7287 /*member_access_only_p=*/false,
7288 decltype_p,
7289 pidk);
7292 static inline tree
7293 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
7294 cp_id_kind * pidk)
7296 return cp_parser_unary_expression (parser, address_p, cast_p,
7297 /*decltype*/false, pidk);
7300 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7301 unary-operator, the corresponding tree code is returned. */
7303 static enum tree_code
7304 cp_parser_unary_operator (cp_token* token)
7306 switch (token->type)
7308 case CPP_MULT:
7309 return INDIRECT_REF;
7311 case CPP_AND:
7312 return ADDR_EXPR;
7314 case CPP_PLUS:
7315 return UNARY_PLUS_EXPR;
7317 case CPP_MINUS:
7318 return NEGATE_EXPR;
7320 case CPP_NOT:
7321 return TRUTH_NOT_EXPR;
7323 case CPP_COMPL:
7324 return BIT_NOT_EXPR;
7326 default:
7327 return ERROR_MARK;
7331 /* Parse a new-expression.
7333 new-expression:
7334 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
7335 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
7337 Returns a representation of the expression. */
7339 static tree
7340 cp_parser_new_expression (cp_parser* parser)
7342 bool global_scope_p;
7343 vec<tree, va_gc> *placement;
7344 tree type;
7345 vec<tree, va_gc> *initializer;
7346 tree nelts = NULL_TREE;
7347 tree ret;
7349 /* Look for the optional `::' operator. */
7350 global_scope_p
7351 = (cp_parser_global_scope_opt (parser,
7352 /*current_scope_valid_p=*/false)
7353 != NULL_TREE);
7354 /* Look for the `new' operator. */
7355 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
7356 /* There's no easy way to tell a new-placement from the
7357 `( type-id )' construct. */
7358 cp_parser_parse_tentatively (parser);
7359 /* Look for a new-placement. */
7360 placement = cp_parser_new_placement (parser);
7361 /* If that didn't work out, there's no new-placement. */
7362 if (!cp_parser_parse_definitely (parser))
7364 if (placement != NULL)
7365 release_tree_vector (placement);
7366 placement = NULL;
7369 /* If the next token is a `(', then we have a parenthesized
7370 type-id. */
7371 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7373 cp_token *token;
7374 const char *saved_message = parser->type_definition_forbidden_message;
7376 /* Consume the `('. */
7377 cp_lexer_consume_token (parser->lexer);
7379 /* Parse the type-id. */
7380 parser->type_definition_forbidden_message
7381 = G_("types may not be defined in a new-expression");
7382 type = cp_parser_type_id (parser);
7383 parser->type_definition_forbidden_message = saved_message;
7385 /* Look for the closing `)'. */
7386 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7387 token = cp_lexer_peek_token (parser->lexer);
7388 /* There should not be a direct-new-declarator in this production,
7389 but GCC used to allowed this, so we check and emit a sensible error
7390 message for this case. */
7391 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7393 error_at (token->location,
7394 "array bound forbidden after parenthesized type-id");
7395 inform (token->location,
7396 "try removing the parentheses around the type-id");
7397 cp_parser_direct_new_declarator (parser);
7400 /* Otherwise, there must be a new-type-id. */
7401 else
7402 type = cp_parser_new_type_id (parser, &nelts);
7404 /* If the next token is a `(' or '{', then we have a new-initializer. */
7405 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
7406 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7407 initializer = cp_parser_new_initializer (parser);
7408 else
7409 initializer = NULL;
7411 /* A new-expression may not appear in an integral constant
7412 expression. */
7413 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
7414 ret = error_mark_node;
7415 else
7417 /* Create a representation of the new-expression. */
7418 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
7419 tf_warning_or_error);
7422 if (placement != NULL)
7423 release_tree_vector (placement);
7424 if (initializer != NULL)
7425 release_tree_vector (initializer);
7427 return ret;
7430 /* Parse a new-placement.
7432 new-placement:
7433 ( expression-list )
7435 Returns the same representation as for an expression-list. */
7437 static vec<tree, va_gc> *
7438 cp_parser_new_placement (cp_parser* parser)
7440 vec<tree, va_gc> *expression_list;
7442 /* Parse the expression-list. */
7443 expression_list = (cp_parser_parenthesized_expression_list
7444 (parser, non_attr, /*cast_p=*/false,
7445 /*allow_expansion_p=*/true,
7446 /*non_constant_p=*/NULL));
7448 return expression_list;
7451 /* Parse a new-type-id.
7453 new-type-id:
7454 type-specifier-seq new-declarator [opt]
7456 Returns the TYPE allocated. If the new-type-id indicates an array
7457 type, *NELTS is set to the number of elements in the last array
7458 bound; the TYPE will not include the last array bound. */
7460 static tree
7461 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
7463 cp_decl_specifier_seq type_specifier_seq;
7464 cp_declarator *new_declarator;
7465 cp_declarator *declarator;
7466 cp_declarator *outer_declarator;
7467 const char *saved_message;
7469 /* The type-specifier sequence must not contain type definitions.
7470 (It cannot contain declarations of new types either, but if they
7471 are not definitions we will catch that because they are not
7472 complete.) */
7473 saved_message = parser->type_definition_forbidden_message;
7474 parser->type_definition_forbidden_message
7475 = G_("types may not be defined in a new-type-id");
7476 /* Parse the type-specifier-seq. */
7477 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
7478 /*is_trailing_return=*/false,
7479 &type_specifier_seq);
7480 /* Restore the old message. */
7481 parser->type_definition_forbidden_message = saved_message;
7483 if (type_specifier_seq.type == error_mark_node)
7484 return error_mark_node;
7486 /* Parse the new-declarator. */
7487 new_declarator = cp_parser_new_declarator_opt (parser);
7489 /* Determine the number of elements in the last array dimension, if
7490 any. */
7491 *nelts = NULL_TREE;
7492 /* Skip down to the last array dimension. */
7493 declarator = new_declarator;
7494 outer_declarator = NULL;
7495 while (declarator && (declarator->kind == cdk_pointer
7496 || declarator->kind == cdk_ptrmem))
7498 outer_declarator = declarator;
7499 declarator = declarator->declarator;
7501 while (declarator
7502 && declarator->kind == cdk_array
7503 && declarator->declarator
7504 && declarator->declarator->kind == cdk_array)
7506 outer_declarator = declarator;
7507 declarator = declarator->declarator;
7510 if (declarator && declarator->kind == cdk_array)
7512 *nelts = declarator->u.array.bounds;
7513 if (*nelts == error_mark_node)
7514 *nelts = integer_one_node;
7516 if (outer_declarator)
7517 outer_declarator->declarator = declarator->declarator;
7518 else
7519 new_declarator = NULL;
7522 return groktypename (&type_specifier_seq, new_declarator, false);
7525 /* Parse an (optional) new-declarator.
7527 new-declarator:
7528 ptr-operator new-declarator [opt]
7529 direct-new-declarator
7531 Returns the declarator. */
7533 static cp_declarator *
7534 cp_parser_new_declarator_opt (cp_parser* parser)
7536 enum tree_code code;
7537 tree type, std_attributes = NULL_TREE;
7538 cp_cv_quals cv_quals;
7540 /* We don't know if there's a ptr-operator next, or not. */
7541 cp_parser_parse_tentatively (parser);
7542 /* Look for a ptr-operator. */
7543 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
7544 /* If that worked, look for more new-declarators. */
7545 if (cp_parser_parse_definitely (parser))
7547 cp_declarator *declarator;
7549 /* Parse another optional declarator. */
7550 declarator = cp_parser_new_declarator_opt (parser);
7552 declarator = cp_parser_make_indirect_declarator
7553 (code, type, cv_quals, declarator, std_attributes);
7555 return declarator;
7558 /* If the next token is a `[', there is a direct-new-declarator. */
7559 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7560 return cp_parser_direct_new_declarator (parser);
7562 return NULL;
7565 /* Parse a direct-new-declarator.
7567 direct-new-declarator:
7568 [ expression ]
7569 direct-new-declarator [constant-expression]
7573 static cp_declarator *
7574 cp_parser_direct_new_declarator (cp_parser* parser)
7576 cp_declarator *declarator = NULL;
7578 while (true)
7580 tree expression;
7581 cp_token *token;
7583 /* Look for the opening `['. */
7584 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7586 token = cp_lexer_peek_token (parser->lexer);
7587 expression = cp_parser_expression (parser);
7588 /* The standard requires that the expression have integral
7589 type. DR 74 adds enumeration types. We believe that the
7590 real intent is that these expressions be handled like the
7591 expression in a `switch' condition, which also allows
7592 classes with a single conversion to integral or
7593 enumeration type. */
7594 if (!processing_template_decl)
7596 expression
7597 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
7598 expression,
7599 /*complain=*/true);
7600 if (!expression)
7602 error_at (token->location,
7603 "expression in new-declarator must have integral "
7604 "or enumeration type");
7605 expression = error_mark_node;
7609 /* Look for the closing `]'. */
7610 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7612 /* Add this bound to the declarator. */
7613 declarator = make_array_declarator (declarator, expression);
7615 /* If the next token is not a `[', then there are no more
7616 bounds. */
7617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
7618 break;
7621 return declarator;
7624 /* Parse a new-initializer.
7626 new-initializer:
7627 ( expression-list [opt] )
7628 braced-init-list
7630 Returns a representation of the expression-list. */
7632 static vec<tree, va_gc> *
7633 cp_parser_new_initializer (cp_parser* parser)
7635 vec<tree, va_gc> *expression_list;
7637 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7639 tree t;
7640 bool expr_non_constant_p;
7641 cp_lexer_set_source_position (parser->lexer);
7642 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7643 t = cp_parser_braced_list (parser, &expr_non_constant_p);
7644 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
7645 expression_list = make_tree_vector_single (t);
7647 else
7648 expression_list = (cp_parser_parenthesized_expression_list
7649 (parser, non_attr, /*cast_p=*/false,
7650 /*allow_expansion_p=*/true,
7651 /*non_constant_p=*/NULL));
7653 return expression_list;
7656 /* Parse a delete-expression.
7658 delete-expression:
7659 :: [opt] delete cast-expression
7660 :: [opt] delete [ ] cast-expression
7662 Returns a representation of the expression. */
7664 static tree
7665 cp_parser_delete_expression (cp_parser* parser)
7667 bool global_scope_p;
7668 bool array_p;
7669 tree expression;
7671 /* Look for the optional `::' operator. */
7672 global_scope_p
7673 = (cp_parser_global_scope_opt (parser,
7674 /*current_scope_valid_p=*/false)
7675 != NULL_TREE);
7676 /* Look for the `delete' keyword. */
7677 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
7678 /* See if the array syntax is in use. */
7679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
7681 /* Consume the `[' token. */
7682 cp_lexer_consume_token (parser->lexer);
7683 /* Look for the `]' token. */
7684 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7685 /* Remember that this is the `[]' construct. */
7686 array_p = true;
7688 else
7689 array_p = false;
7691 /* Parse the cast-expression. */
7692 expression = cp_parser_simple_cast_expression (parser);
7694 /* A delete-expression may not appear in an integral constant
7695 expression. */
7696 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7697 return error_mark_node;
7699 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7700 tf_warning_or_error);
7703 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
7704 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
7705 0 otherwise. */
7707 static int
7708 cp_parser_tokens_start_cast_expression (cp_parser *parser)
7710 cp_token *token = cp_lexer_peek_token (parser->lexer);
7711 switch (token->type)
7713 case CPP_COMMA:
7714 case CPP_SEMICOLON:
7715 case CPP_QUERY:
7716 case CPP_COLON:
7717 case CPP_CLOSE_SQUARE:
7718 case CPP_CLOSE_PAREN:
7719 case CPP_CLOSE_BRACE:
7720 case CPP_OPEN_BRACE:
7721 case CPP_DOT:
7722 case CPP_DOT_STAR:
7723 case CPP_DEREF:
7724 case CPP_DEREF_STAR:
7725 case CPP_DIV:
7726 case CPP_MOD:
7727 case CPP_LSHIFT:
7728 case CPP_RSHIFT:
7729 case CPP_LESS:
7730 case CPP_GREATER:
7731 case CPP_LESS_EQ:
7732 case CPP_GREATER_EQ:
7733 case CPP_EQ_EQ:
7734 case CPP_NOT_EQ:
7735 case CPP_EQ:
7736 case CPP_MULT_EQ:
7737 case CPP_DIV_EQ:
7738 case CPP_MOD_EQ:
7739 case CPP_PLUS_EQ:
7740 case CPP_MINUS_EQ:
7741 case CPP_RSHIFT_EQ:
7742 case CPP_LSHIFT_EQ:
7743 case CPP_AND_EQ:
7744 case CPP_XOR_EQ:
7745 case CPP_OR_EQ:
7746 case CPP_XOR:
7747 case CPP_OR:
7748 case CPP_OR_OR:
7749 case CPP_EOF:
7750 case CPP_ELLIPSIS:
7751 return 0;
7753 case CPP_OPEN_PAREN:
7754 /* In ((type ()) () the last () isn't a valid cast-expression,
7755 so the whole must be parsed as postfix-expression. */
7756 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
7757 != CPP_CLOSE_PAREN;
7759 case CPP_OPEN_SQUARE:
7760 /* '[' may start a primary-expression in obj-c++ and in C++11,
7761 as a lambda-expression, eg, '(void)[]{}'. */
7762 if (cxx_dialect >= cxx11)
7763 return -1;
7764 return c_dialect_objc ();
7766 case CPP_PLUS_PLUS:
7767 case CPP_MINUS_MINUS:
7768 /* '++' and '--' may or may not start a cast-expression:
7770 struct T { void operator++(int); };
7771 void f() { (T())++; }
7775 int a;
7776 (int)++a; */
7777 return -1;
7779 default:
7780 return 1;
7784 /* Parse a cast-expression.
7786 cast-expression:
7787 unary-expression
7788 ( type-id ) cast-expression
7790 ADDRESS_P is true iff the unary-expression is appearing as the
7791 operand of the `&' operator. CAST_P is true if this expression is
7792 the target of a cast.
7794 Returns a representation of the expression. */
7796 static tree
7797 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7798 bool decltype_p, cp_id_kind * pidk)
7800 /* If it's a `(', then we might be looking at a cast. */
7801 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7803 tree type = NULL_TREE;
7804 tree expr = NULL_TREE;
7805 int cast_expression = 0;
7806 const char *saved_message;
7808 /* There's no way to know yet whether or not this is a cast.
7809 For example, `(int (3))' is a unary-expression, while `(int)
7810 3' is a cast. So, we resort to parsing tentatively. */
7811 cp_parser_parse_tentatively (parser);
7812 /* Types may not be defined in a cast. */
7813 saved_message = parser->type_definition_forbidden_message;
7814 parser->type_definition_forbidden_message
7815 = G_("types may not be defined in casts");
7816 /* Consume the `('. */
7817 cp_lexer_consume_token (parser->lexer);
7818 /* A very tricky bit is that `(struct S) { 3 }' is a
7819 compound-literal (which we permit in C++ as an extension).
7820 But, that construct is not a cast-expression -- it is a
7821 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7822 is legal; if the compound-literal were a cast-expression,
7823 you'd need an extra set of parentheses.) But, if we parse
7824 the type-id, and it happens to be a class-specifier, then we
7825 will commit to the parse at that point, because we cannot
7826 undo the action that is done when creating a new class. So,
7827 then we cannot back up and do a postfix-expression.
7829 Another tricky case is the following (c++/29234):
7831 struct S { void operator () (); };
7833 void foo ()
7835 ( S()() );
7838 As a type-id we parse the parenthesized S()() as a function
7839 returning a function, groktypename complains and we cannot
7840 back up in this case either.
7842 Therefore, we scan ahead to the closing `)', and check to see
7843 if the tokens after the `)' can start a cast-expression. Otherwise
7844 we are dealing with an unary-expression, a postfix-expression
7845 or something else.
7847 Yet another tricky case, in C++11, is the following (c++/54891):
7849 (void)[]{};
7851 The issue is that usually, besides the case of lambda-expressions,
7852 the parenthesized type-id cannot be followed by '[', and, eg, we
7853 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
7854 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
7855 we don't commit, we try a cast-expression, then an unary-expression.
7857 Save tokens so that we can put them back. */
7858 cp_lexer_save_tokens (parser->lexer);
7860 /* We may be looking at a cast-expression. */
7861 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7862 /*consume_paren=*/true))
7863 cast_expression
7864 = cp_parser_tokens_start_cast_expression (parser);
7866 /* Roll back the tokens we skipped. */
7867 cp_lexer_rollback_tokens (parser->lexer);
7868 /* If we aren't looking at a cast-expression, simulate an error so
7869 that the call to cp_parser_error_occurred below returns true. */
7870 if (!cast_expression)
7871 cp_parser_simulate_error (parser);
7872 else
7874 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7875 parser->in_type_id_in_expr_p = true;
7876 /* Look for the type-id. */
7877 type = cp_parser_type_id (parser);
7878 /* Look for the closing `)'. */
7879 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7880 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7883 /* Restore the saved message. */
7884 parser->type_definition_forbidden_message = saved_message;
7886 /* At this point this can only be either a cast or a
7887 parenthesized ctor such as `(T ())' that looks like a cast to
7888 function returning T. */
7889 if (!cp_parser_error_occurred (parser))
7891 /* Only commit if the cast-expression doesn't start with
7892 '++', '--', or '[' in C++11. */
7893 if (cast_expression > 0)
7894 cp_parser_commit_to_topmost_tentative_parse (parser);
7896 expr = cp_parser_cast_expression (parser,
7897 /*address_p=*/false,
7898 /*cast_p=*/true,
7899 /*decltype_p=*/false,
7900 pidk);
7902 if (cp_parser_parse_definitely (parser))
7904 /* Warn about old-style casts, if so requested. */
7905 if (warn_old_style_cast
7906 && !in_system_header_at (input_location)
7907 && !VOID_TYPE_P (type)
7908 && current_lang_name != lang_name_c)
7909 warning (OPT_Wold_style_cast, "use of old-style cast");
7911 /* Only type conversions to integral or enumeration types
7912 can be used in constant-expressions. */
7913 if (!cast_valid_in_integral_constant_expression_p (type)
7914 && cp_parser_non_integral_constant_expression (parser,
7915 NIC_CAST))
7916 return error_mark_node;
7918 /* Perform the cast. */
7919 expr = build_c_cast (input_location, type, expr);
7920 return expr;
7923 else
7924 cp_parser_abort_tentative_parse (parser);
7927 /* If we get here, then it's not a cast, so it must be a
7928 unary-expression. */
7929 return cp_parser_unary_expression (parser, address_p, cast_p,
7930 decltype_p, pidk);
7933 /* Parse a binary expression of the general form:
7935 pm-expression:
7936 cast-expression
7937 pm-expression .* cast-expression
7938 pm-expression ->* cast-expression
7940 multiplicative-expression:
7941 pm-expression
7942 multiplicative-expression * pm-expression
7943 multiplicative-expression / pm-expression
7944 multiplicative-expression % pm-expression
7946 additive-expression:
7947 multiplicative-expression
7948 additive-expression + multiplicative-expression
7949 additive-expression - multiplicative-expression
7951 shift-expression:
7952 additive-expression
7953 shift-expression << additive-expression
7954 shift-expression >> additive-expression
7956 relational-expression:
7957 shift-expression
7958 relational-expression < shift-expression
7959 relational-expression > shift-expression
7960 relational-expression <= shift-expression
7961 relational-expression >= shift-expression
7963 GNU Extension:
7965 relational-expression:
7966 relational-expression <? shift-expression
7967 relational-expression >? shift-expression
7969 equality-expression:
7970 relational-expression
7971 equality-expression == relational-expression
7972 equality-expression != relational-expression
7974 and-expression:
7975 equality-expression
7976 and-expression & equality-expression
7978 exclusive-or-expression:
7979 and-expression
7980 exclusive-or-expression ^ and-expression
7982 inclusive-or-expression:
7983 exclusive-or-expression
7984 inclusive-or-expression | exclusive-or-expression
7986 logical-and-expression:
7987 inclusive-or-expression
7988 logical-and-expression && inclusive-or-expression
7990 logical-or-expression:
7991 logical-and-expression
7992 logical-or-expression || logical-and-expression
7994 All these are implemented with a single function like:
7996 binary-expression:
7997 simple-cast-expression
7998 binary-expression <token> binary-expression
8000 CAST_P is true if this expression is the target of a cast.
8002 The binops_by_token map is used to get the tree codes for each <token> type.
8003 binary-expressions are associated according to a precedence table. */
8005 #define TOKEN_PRECEDENCE(token) \
8006 (((token->type == CPP_GREATER \
8007 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8008 && !parser->greater_than_is_operator_p) \
8009 ? PREC_NOT_OPERATOR \
8010 : binops_by_token[token->type].prec)
8012 static tree
8013 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8014 bool no_toplevel_fold_p,
8015 bool decltype_p,
8016 enum cp_parser_prec prec,
8017 cp_id_kind * pidk)
8019 cp_parser_expression_stack stack;
8020 cp_parser_expression_stack_entry *sp = &stack[0];
8021 cp_parser_expression_stack_entry current;
8022 tree rhs;
8023 cp_token *token;
8024 enum tree_code rhs_type;
8025 enum cp_parser_prec new_prec, lookahead_prec;
8026 tree overload;
8028 /* Parse the first expression. */
8029 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8030 ? TRUTH_NOT_EXPR : ERROR_MARK);
8031 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8032 cast_p, decltype_p, pidk);
8033 current.prec = prec;
8035 if (cp_parser_error_occurred (parser))
8036 return error_mark_node;
8038 for (;;)
8040 /* Get an operator token. */
8041 token = cp_lexer_peek_token (parser->lexer);
8043 if (warn_cxx0x_compat
8044 && token->type == CPP_RSHIFT
8045 && !parser->greater_than_is_operator_p)
8047 if (warning_at (token->location, OPT_Wc__0x_compat,
8048 "%<>>%> operator is treated"
8049 " as two right angle brackets in C++11"))
8050 inform (token->location,
8051 "suggest parentheses around %<>>%> expression");
8054 new_prec = TOKEN_PRECEDENCE (token);
8056 /* Popping an entry off the stack means we completed a subexpression:
8057 - either we found a token which is not an operator (`>' where it is not
8058 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8059 will happen repeatedly;
8060 - or, we found an operator which has lower priority. This is the case
8061 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8062 parsing `3 * 4'. */
8063 if (new_prec <= current.prec)
8065 if (sp == stack)
8066 break;
8067 else
8068 goto pop;
8071 get_rhs:
8072 current.tree_type = binops_by_token[token->type].tree_type;
8073 current.loc = token->location;
8075 /* We used the operator token. */
8076 cp_lexer_consume_token (parser->lexer);
8078 /* For "false && x" or "true || x", x will never be executed;
8079 disable warnings while evaluating it. */
8080 if (current.tree_type == TRUTH_ANDIF_EXPR)
8081 c_inhibit_evaluation_warnings += current.lhs == truthvalue_false_node;
8082 else if (current.tree_type == TRUTH_ORIF_EXPR)
8083 c_inhibit_evaluation_warnings += current.lhs == truthvalue_true_node;
8085 /* Extract another operand. It may be the RHS of this expression
8086 or the LHS of a new, higher priority expression. */
8087 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8088 ? TRUTH_NOT_EXPR : ERROR_MARK);
8089 rhs = cp_parser_simple_cast_expression (parser);
8091 /* Get another operator token. Look up its precedence to avoid
8092 building a useless (immediately popped) stack entry for common
8093 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8094 token = cp_lexer_peek_token (parser->lexer);
8095 lookahead_prec = TOKEN_PRECEDENCE (token);
8096 if (lookahead_prec > new_prec)
8098 /* ... and prepare to parse the RHS of the new, higher priority
8099 expression. Since precedence levels on the stack are
8100 monotonically increasing, we do not have to care about
8101 stack overflows. */
8102 *sp = current;
8103 ++sp;
8104 current.lhs = rhs;
8105 current.lhs_type = rhs_type;
8106 current.prec = new_prec;
8107 new_prec = lookahead_prec;
8108 goto get_rhs;
8110 pop:
8111 lookahead_prec = new_prec;
8112 /* If the stack is not empty, we have parsed into LHS the right side
8113 (`4' in the example above) of an expression we had suspended.
8114 We can use the information on the stack to recover the LHS (`3')
8115 from the stack together with the tree code (`MULT_EXPR'), and
8116 the precedence of the higher level subexpression
8117 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8118 which will be used to actually build the additive expression. */
8119 rhs = current.lhs;
8120 rhs_type = current.lhs_type;
8121 --sp;
8122 current = *sp;
8125 /* Undo the disabling of warnings done above. */
8126 if (current.tree_type == TRUTH_ANDIF_EXPR)
8127 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_false_node;
8128 else if (current.tree_type == TRUTH_ORIF_EXPR)
8129 c_inhibit_evaluation_warnings -= current.lhs == truthvalue_true_node;
8131 if (warn_logical_not_paren
8132 && current.lhs_type == TRUTH_NOT_EXPR)
8133 warn_logical_not_parentheses (current.loc, current.tree_type, rhs);
8135 overload = NULL;
8136 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8137 ERROR_MARK for everything that is not a binary expression.
8138 This makes warn_about_parentheses miss some warnings that
8139 involve unary operators. For unary expressions we should
8140 pass the correct tree_code unless the unary expression was
8141 surrounded by parentheses.
8143 if (no_toplevel_fold_p
8144 && lookahead_prec <= current.prec
8145 && sp == stack)
8146 current.lhs = build2 (current.tree_type,
8147 TREE_CODE_CLASS (current.tree_type)
8148 == tcc_comparison
8149 ? boolean_type_node : TREE_TYPE (current.lhs),
8150 current.lhs, rhs);
8151 else
8152 current.lhs = build_x_binary_op (current.loc, current.tree_type,
8153 current.lhs, current.lhs_type,
8154 rhs, rhs_type, &overload,
8155 complain_flags (decltype_p));
8156 current.lhs_type = current.tree_type;
8157 if (EXPR_P (current.lhs))
8158 SET_EXPR_LOCATION (current.lhs, current.loc);
8160 /* If the binary operator required the use of an overloaded operator,
8161 then this expression cannot be an integral constant-expression.
8162 An overloaded operator can be used even if both operands are
8163 otherwise permissible in an integral constant-expression if at
8164 least one of the operands is of enumeration type. */
8166 if (overload
8167 && cp_parser_non_integral_constant_expression (parser,
8168 NIC_OVERLOADED))
8169 return error_mark_node;
8172 return current.lhs;
8175 static tree
8176 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8177 bool no_toplevel_fold_p,
8178 enum cp_parser_prec prec,
8179 cp_id_kind * pidk)
8181 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8182 /*decltype*/false, prec, pidk);
8185 /* Parse the `? expression : assignment-expression' part of a
8186 conditional-expression. The LOGICAL_OR_EXPR is the
8187 logical-or-expression that started the conditional-expression.
8188 Returns a representation of the entire conditional-expression.
8190 This routine is used by cp_parser_assignment_expression.
8192 ? expression : assignment-expression
8194 GNU Extensions:
8196 ? : assignment-expression */
8198 static tree
8199 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
8201 tree expr;
8202 tree assignment_expr;
8203 struct cp_token *token;
8204 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8206 /* Consume the `?' token. */
8207 cp_lexer_consume_token (parser->lexer);
8208 token = cp_lexer_peek_token (parser->lexer);
8209 if (cp_parser_allow_gnu_extensions_p (parser)
8210 && token->type == CPP_COLON)
8212 pedwarn (token->location, OPT_Wpedantic,
8213 "ISO C++ does not allow ?: with omitted middle operand");
8214 /* Implicit true clause. */
8215 expr = NULL_TREE;
8216 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
8217 warn_for_omitted_condop (token->location, logical_or_expr);
8219 else
8221 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8222 parser->colon_corrects_to_scope_p = false;
8223 /* Parse the expression. */
8224 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
8225 expr = cp_parser_expression (parser);
8226 c_inhibit_evaluation_warnings +=
8227 ((logical_or_expr == truthvalue_true_node)
8228 - (logical_or_expr == truthvalue_false_node));
8229 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8232 /* The next token should be a `:'. */
8233 cp_parser_require (parser, CPP_COLON, RT_COLON);
8234 /* Parse the assignment-expression. */
8235 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8236 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
8238 /* Build the conditional-expression. */
8239 return build_x_conditional_expr (loc, logical_or_expr,
8240 expr,
8241 assignment_expr,
8242 tf_warning_or_error);
8245 /* Parse an assignment-expression.
8247 assignment-expression:
8248 conditional-expression
8249 logical-or-expression assignment-operator assignment_expression
8250 throw-expression
8252 CAST_P is true if this expression is the target of a cast.
8253 DECLTYPE_P is true if this expression is the operand of decltype.
8255 Returns a representation for the expression. */
8257 static tree
8258 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8259 bool decltype_p, cp_id_kind * pidk)
8261 tree expr;
8263 /* If the next token is the `throw' keyword, then we're looking at
8264 a throw-expression. */
8265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
8266 expr = cp_parser_throw_expression (parser);
8267 /* Otherwise, it must be that we are looking at a
8268 logical-or-expression. */
8269 else
8271 /* Parse the binary expressions (logical-or-expression). */
8272 expr = cp_parser_binary_expression (parser, cast_p, false,
8273 decltype_p,
8274 PREC_NOT_OPERATOR, pidk);
8275 /* If the next token is a `?' then we're actually looking at a
8276 conditional-expression. */
8277 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
8278 return cp_parser_question_colon_clause (parser, expr);
8279 else
8281 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8283 /* If it's an assignment-operator, we're using the second
8284 production. */
8285 enum tree_code assignment_operator
8286 = cp_parser_assignment_operator_opt (parser);
8287 if (assignment_operator != ERROR_MARK)
8289 bool non_constant_p;
8290 location_t saved_input_location;
8292 /* Parse the right-hand side of the assignment. */
8293 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
8295 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
8296 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8298 /* An assignment may not appear in a
8299 constant-expression. */
8300 if (cp_parser_non_integral_constant_expression (parser,
8301 NIC_ASSIGNMENT))
8302 return error_mark_node;
8303 /* Build the assignment expression. Its default
8304 location is the location of the '=' token. */
8305 saved_input_location = input_location;
8306 input_location = loc;
8307 expr = build_x_modify_expr (loc, expr,
8308 assignment_operator,
8309 rhs,
8310 complain_flags (decltype_p));
8311 input_location = saved_input_location;
8316 return expr;
8319 static tree
8320 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
8321 cp_id_kind * pidk)
8323 return cp_parser_assignment_expression (parser, cast_p,
8324 /*decltype*/false, pidk);
8327 /* Parse an (optional) assignment-operator.
8329 assignment-operator: one of
8330 = *= /= %= += -= >>= <<= &= ^= |=
8332 GNU Extension:
8334 assignment-operator: one of
8335 <?= >?=
8337 If the next token is an assignment operator, the corresponding tree
8338 code is returned, and the token is consumed. For example, for
8339 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
8340 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
8341 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
8342 operator, ERROR_MARK is returned. */
8344 static enum tree_code
8345 cp_parser_assignment_operator_opt (cp_parser* parser)
8347 enum tree_code op;
8348 cp_token *token;
8350 /* Peek at the next token. */
8351 token = cp_lexer_peek_token (parser->lexer);
8353 switch (token->type)
8355 case CPP_EQ:
8356 op = NOP_EXPR;
8357 break;
8359 case CPP_MULT_EQ:
8360 op = MULT_EXPR;
8361 break;
8363 case CPP_DIV_EQ:
8364 op = TRUNC_DIV_EXPR;
8365 break;
8367 case CPP_MOD_EQ:
8368 op = TRUNC_MOD_EXPR;
8369 break;
8371 case CPP_PLUS_EQ:
8372 op = PLUS_EXPR;
8373 break;
8375 case CPP_MINUS_EQ:
8376 op = MINUS_EXPR;
8377 break;
8379 case CPP_RSHIFT_EQ:
8380 op = RSHIFT_EXPR;
8381 break;
8383 case CPP_LSHIFT_EQ:
8384 op = LSHIFT_EXPR;
8385 break;
8387 case CPP_AND_EQ:
8388 op = BIT_AND_EXPR;
8389 break;
8391 case CPP_XOR_EQ:
8392 op = BIT_XOR_EXPR;
8393 break;
8395 case CPP_OR_EQ:
8396 op = BIT_IOR_EXPR;
8397 break;
8399 default:
8400 /* Nothing else is an assignment operator. */
8401 op = ERROR_MARK;
8404 /* If it was an assignment operator, consume it. */
8405 if (op != ERROR_MARK)
8406 cp_lexer_consume_token (parser->lexer);
8408 return op;
8411 /* Parse an expression.
8413 expression:
8414 assignment-expression
8415 expression , assignment-expression
8417 CAST_P is true if this expression is the target of a cast.
8418 DECLTYPE_P is true if this expression is the immediate operand of decltype,
8419 except possibly parenthesized or on the RHS of a comma (N3276).
8421 Returns a representation of the expression. */
8423 static tree
8424 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
8425 bool cast_p, bool decltype_p)
8427 tree expression = NULL_TREE;
8428 location_t loc = UNKNOWN_LOCATION;
8430 while (true)
8432 tree assignment_expression;
8434 /* Parse the next assignment-expression. */
8435 assignment_expression
8436 = cp_parser_assignment_expression (parser, cast_p, decltype_p, pidk);
8438 /* We don't create a temporary for a call that is the immediate operand
8439 of decltype or on the RHS of a comma. But when we see a comma, we
8440 need to create a temporary for a call on the LHS. */
8441 if (decltype_p && !processing_template_decl
8442 && TREE_CODE (assignment_expression) == CALL_EXPR
8443 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
8444 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8445 assignment_expression
8446 = build_cplus_new (TREE_TYPE (assignment_expression),
8447 assignment_expression, tf_warning_or_error);
8449 /* If this is the first assignment-expression, we can just
8450 save it away. */
8451 if (!expression)
8452 expression = assignment_expression;
8453 else
8454 expression = build_x_compound_expr (loc, expression,
8455 assignment_expression,
8456 complain_flags (decltype_p));
8457 /* If the next token is not a comma, then we are done with the
8458 expression. */
8459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8460 break;
8461 /* Consume the `,'. */
8462 loc = cp_lexer_peek_token (parser->lexer)->location;
8463 cp_lexer_consume_token (parser->lexer);
8464 /* A comma operator cannot appear in a constant-expression. */
8465 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
8466 expression = error_mark_node;
8469 return expression;
8472 /* Parse a constant-expression.
8474 constant-expression:
8475 conditional-expression
8477 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
8478 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
8479 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
8480 is false, NON_CONSTANT_P should be NULL. */
8482 static tree
8483 cp_parser_constant_expression (cp_parser* parser,
8484 bool allow_non_constant_p,
8485 bool *non_constant_p)
8487 bool saved_integral_constant_expression_p;
8488 bool saved_allow_non_integral_constant_expression_p;
8489 bool saved_non_integral_constant_expression_p;
8490 tree expression;
8492 /* It might seem that we could simply parse the
8493 conditional-expression, and then check to see if it were
8494 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
8495 one that the compiler can figure out is constant, possibly after
8496 doing some simplifications or optimizations. The standard has a
8497 precise definition of constant-expression, and we must honor
8498 that, even though it is somewhat more restrictive.
8500 For example:
8502 int i[(2, 3)];
8504 is not a legal declaration, because `(2, 3)' is not a
8505 constant-expression. The `,' operator is forbidden in a
8506 constant-expression. However, GCC's constant-folding machinery
8507 will fold this operation to an INTEGER_CST for `3'. */
8509 /* Save the old settings. */
8510 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
8511 saved_allow_non_integral_constant_expression_p
8512 = parser->allow_non_integral_constant_expression_p;
8513 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
8514 /* We are now parsing a constant-expression. */
8515 parser->integral_constant_expression_p = true;
8516 parser->allow_non_integral_constant_expression_p
8517 = (allow_non_constant_p || cxx_dialect >= cxx11);
8518 parser->non_integral_constant_expression_p = false;
8519 /* Although the grammar says "conditional-expression", we parse an
8520 "assignment-expression", which also permits "throw-expression"
8521 and the use of assignment operators. In the case that
8522 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
8523 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
8524 actually essential that we look for an assignment-expression.
8525 For example, cp_parser_initializer_clauses uses this function to
8526 determine whether a particular assignment-expression is in fact
8527 constant. */
8528 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
8529 /* Restore the old settings. */
8530 parser->integral_constant_expression_p
8531 = saved_integral_constant_expression_p;
8532 parser->allow_non_integral_constant_expression_p
8533 = saved_allow_non_integral_constant_expression_p;
8534 if (cxx_dialect >= cxx11)
8536 /* Require an rvalue constant expression here; that's what our
8537 callers expect. Reference constant expressions are handled
8538 separately in e.g. cp_parser_template_argument. */
8539 bool is_const = potential_rvalue_constant_expression (expression);
8540 parser->non_integral_constant_expression_p = !is_const;
8541 if (!is_const && !allow_non_constant_p)
8542 require_potential_rvalue_constant_expression (expression);
8544 if (allow_non_constant_p)
8545 *non_constant_p = parser->non_integral_constant_expression_p;
8546 parser->non_integral_constant_expression_p
8547 = saved_non_integral_constant_expression_p;
8549 return expression;
8552 /* Parse __builtin_offsetof.
8554 offsetof-expression:
8555 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
8557 offsetof-member-designator:
8558 id-expression
8559 | offsetof-member-designator "." id-expression
8560 | offsetof-member-designator "[" expression "]"
8561 | offsetof-member-designator "->" id-expression */
8563 static tree
8564 cp_parser_builtin_offsetof (cp_parser *parser)
8566 int save_ice_p, save_non_ice_p;
8567 tree type, expr;
8568 cp_id_kind dummy;
8569 cp_token *token;
8571 /* We're about to accept non-integral-constant things, but will
8572 definitely yield an integral constant expression. Save and
8573 restore these values around our local parsing. */
8574 save_ice_p = parser->integral_constant_expression_p;
8575 save_non_ice_p = parser->non_integral_constant_expression_p;
8577 /* Consume the "__builtin_offsetof" token. */
8578 cp_lexer_consume_token (parser->lexer);
8579 /* Consume the opening `('. */
8580 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8581 /* Parse the type-id. */
8582 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8583 type = cp_parser_type_id (parser);
8584 /* Look for the `,'. */
8585 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8586 token = cp_lexer_peek_token (parser->lexer);
8588 /* Build the (type *)null that begins the traditional offsetof macro. */
8589 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
8590 tf_warning_or_error);
8592 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
8593 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
8594 true, &dummy, token->location);
8595 while (true)
8597 token = cp_lexer_peek_token (parser->lexer);
8598 switch (token->type)
8600 case CPP_OPEN_SQUARE:
8601 /* offsetof-member-designator "[" expression "]" */
8602 expr = cp_parser_postfix_open_square_expression (parser, expr,
8603 true, false);
8604 break;
8606 case CPP_DEREF:
8607 /* offsetof-member-designator "->" identifier */
8608 expr = grok_array_decl (token->location, expr,
8609 integer_zero_node, false);
8610 /* FALLTHRU */
8612 case CPP_DOT:
8613 /* offsetof-member-designator "." identifier */
8614 cp_lexer_consume_token (parser->lexer);
8615 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
8616 expr, true, &dummy,
8617 token->location);
8618 break;
8620 case CPP_CLOSE_PAREN:
8621 /* Consume the ")" token. */
8622 cp_lexer_consume_token (parser->lexer);
8623 goto success;
8625 default:
8626 /* Error. We know the following require will fail, but
8627 that gives the proper error message. */
8628 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8629 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
8630 expr = error_mark_node;
8631 goto failure;
8635 success:
8636 /* If we're processing a template, we can't finish the semantics yet.
8637 Otherwise we can fold the entire expression now. */
8638 if (processing_template_decl)
8640 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
8641 SET_EXPR_LOCATION (expr, loc);
8643 else
8644 expr = finish_offsetof (expr, loc);
8646 failure:
8647 parser->integral_constant_expression_p = save_ice_p;
8648 parser->non_integral_constant_expression_p = save_non_ice_p;
8650 return expr;
8653 /* Parse a trait expression.
8655 Returns a representation of the expression, the underlying type
8656 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
8658 static tree
8659 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
8661 cp_trait_kind kind;
8662 tree type1, type2 = NULL_TREE;
8663 bool binary = false;
8664 cp_decl_specifier_seq decl_specs;
8666 switch (keyword)
8668 case RID_HAS_NOTHROW_ASSIGN:
8669 kind = CPTK_HAS_NOTHROW_ASSIGN;
8670 break;
8671 case RID_HAS_NOTHROW_CONSTRUCTOR:
8672 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
8673 break;
8674 case RID_HAS_NOTHROW_COPY:
8675 kind = CPTK_HAS_NOTHROW_COPY;
8676 break;
8677 case RID_HAS_TRIVIAL_ASSIGN:
8678 kind = CPTK_HAS_TRIVIAL_ASSIGN;
8679 break;
8680 case RID_HAS_TRIVIAL_CONSTRUCTOR:
8681 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
8682 break;
8683 case RID_HAS_TRIVIAL_COPY:
8684 kind = CPTK_HAS_TRIVIAL_COPY;
8685 break;
8686 case RID_HAS_TRIVIAL_DESTRUCTOR:
8687 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
8688 break;
8689 case RID_HAS_VIRTUAL_DESTRUCTOR:
8690 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
8691 break;
8692 case RID_IS_ABSTRACT:
8693 kind = CPTK_IS_ABSTRACT;
8694 break;
8695 case RID_IS_BASE_OF:
8696 kind = CPTK_IS_BASE_OF;
8697 binary = true;
8698 break;
8699 case RID_IS_CLASS:
8700 kind = CPTK_IS_CLASS;
8701 break;
8702 case RID_IS_CONVERTIBLE_TO:
8703 kind = CPTK_IS_CONVERTIBLE_TO;
8704 binary = true;
8705 break;
8706 case RID_IS_EMPTY:
8707 kind = CPTK_IS_EMPTY;
8708 break;
8709 case RID_IS_ENUM:
8710 kind = CPTK_IS_ENUM;
8711 break;
8712 case RID_IS_FINAL:
8713 kind = CPTK_IS_FINAL;
8714 break;
8715 case RID_IS_LITERAL_TYPE:
8716 kind = CPTK_IS_LITERAL_TYPE;
8717 break;
8718 case RID_IS_POD:
8719 kind = CPTK_IS_POD;
8720 break;
8721 case RID_IS_POLYMORPHIC:
8722 kind = CPTK_IS_POLYMORPHIC;
8723 break;
8724 case RID_IS_STD_LAYOUT:
8725 kind = CPTK_IS_STD_LAYOUT;
8726 break;
8727 case RID_IS_TRIVIAL:
8728 kind = CPTK_IS_TRIVIAL;
8729 break;
8730 case RID_IS_UNION:
8731 kind = CPTK_IS_UNION;
8732 break;
8733 case RID_UNDERLYING_TYPE:
8734 kind = CPTK_UNDERLYING_TYPE;
8735 break;
8736 case RID_BASES:
8737 kind = CPTK_BASES;
8738 break;
8739 case RID_DIRECT_BASES:
8740 kind = CPTK_DIRECT_BASES;
8741 break;
8742 default:
8743 gcc_unreachable ();
8746 /* Consume the token. */
8747 cp_lexer_consume_token (parser->lexer);
8749 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8751 type1 = cp_parser_type_id (parser);
8753 if (type1 == error_mark_node)
8754 return error_mark_node;
8756 /* Build a trivial decl-specifier-seq. */
8757 clear_decl_specs (&decl_specs);
8758 decl_specs.type = type1;
8760 /* Call grokdeclarator to figure out what type this is. */
8761 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8762 /*initialized=*/0, /*attrlist=*/NULL);
8764 if (binary)
8766 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8768 type2 = cp_parser_type_id (parser);
8770 if (type2 == error_mark_node)
8771 return error_mark_node;
8773 /* Build a trivial decl-specifier-seq. */
8774 clear_decl_specs (&decl_specs);
8775 decl_specs.type = type2;
8777 /* Call grokdeclarator to figure out what type this is. */
8778 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
8779 /*initialized=*/0, /*attrlist=*/NULL);
8782 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8784 /* Complete the trait expression, which may mean either processing
8785 the trait expr now or saving it for template instantiation. */
8786 switch(kind)
8788 case CPTK_UNDERLYING_TYPE:
8789 return finish_underlying_type (type1);
8790 case CPTK_BASES:
8791 return finish_bases (type1, false);
8792 case CPTK_DIRECT_BASES:
8793 return finish_bases (type1, true);
8794 default:
8795 return finish_trait_expr (kind, type1, type2);
8799 /* Lambdas that appear in variable initializer or default argument scope
8800 get that in their mangling, so we need to record it. We might as well
8801 use the count for function and namespace scopes as well. */
8802 static GTY(()) tree lambda_scope;
8803 static GTY(()) int lambda_count;
8804 typedef struct GTY(()) tree_int
8806 tree t;
8807 int i;
8808 } tree_int;
8809 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
8811 static void
8812 start_lambda_scope (tree decl)
8814 tree_int ti;
8815 gcc_assert (decl);
8816 /* Once we're inside a function, we ignore other scopes and just push
8817 the function again so that popping works properly. */
8818 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8819 decl = current_function_decl;
8820 ti.t = lambda_scope;
8821 ti.i = lambda_count;
8822 vec_safe_push (lambda_scope_stack, ti);
8823 if (lambda_scope != decl)
8825 /* Don't reset the count if we're still in the same function. */
8826 lambda_scope = decl;
8827 lambda_count = 0;
8831 static void
8832 record_lambda_scope (tree lambda)
8834 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8835 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8838 static void
8839 finish_lambda_scope (void)
8841 tree_int *p = &lambda_scope_stack->last ();
8842 if (lambda_scope != p->t)
8844 lambda_scope = p->t;
8845 lambda_count = p->i;
8847 lambda_scope_stack->pop ();
8850 /* Parse a lambda expression.
8852 lambda-expression:
8853 lambda-introducer lambda-declarator [opt] compound-statement
8855 Returns a representation of the expression. */
8857 static tree
8858 cp_parser_lambda_expression (cp_parser* parser)
8860 tree lambda_expr = build_lambda_expr ();
8861 tree type;
8862 bool ok = true;
8863 cp_token *token = cp_lexer_peek_token (parser->lexer);
8865 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
8867 if (cp_unevaluated_operand)
8869 if (!token->error_reported)
8871 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8872 "lambda-expression in unevaluated context");
8873 token->error_reported = true;
8875 ok = false;
8878 /* We may be in the middle of deferred access check. Disable
8879 it now. */
8880 push_deferring_access_checks (dk_no_deferred);
8882 cp_parser_lambda_introducer (parser, lambda_expr);
8884 type = begin_lambda_type (lambda_expr);
8885 if (type == error_mark_node)
8886 return error_mark_node;
8888 record_lambda_scope (lambda_expr);
8890 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
8891 determine_visibility (TYPE_NAME (type));
8893 /* Now that we've started the type, add the capture fields for any
8894 explicit captures. */
8895 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8898 /* Inside the class, surrounding template-parameter-lists do not apply. */
8899 unsigned int saved_num_template_parameter_lists
8900 = parser->num_template_parameter_lists;
8901 unsigned char in_statement = parser->in_statement;
8902 bool in_switch_statement_p = parser->in_switch_statement_p;
8903 bool fully_implicit_function_template_p
8904 = parser->fully_implicit_function_template_p;
8905 tree implicit_template_parms = parser->implicit_template_parms;
8906 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
8907 bool auto_is_implicit_function_template_parm_p
8908 = parser->auto_is_implicit_function_template_parm_p;
8910 parser->num_template_parameter_lists = 0;
8911 parser->in_statement = 0;
8912 parser->in_switch_statement_p = false;
8913 parser->fully_implicit_function_template_p = false;
8914 parser->implicit_template_parms = 0;
8915 parser->implicit_template_scope = 0;
8916 parser->auto_is_implicit_function_template_parm_p = false;
8918 /* By virtue of defining a local class, a lambda expression has access to
8919 the private variables of enclosing classes. */
8921 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
8923 if (ok)
8924 cp_parser_lambda_body (parser, lambda_expr);
8925 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8927 if (cp_parser_skip_to_closing_brace (parser))
8928 cp_lexer_consume_token (parser->lexer);
8931 /* The capture list was built up in reverse order; fix that now. */
8932 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
8933 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8935 if (ok)
8936 maybe_add_lambda_conv_op (type);
8938 type = finish_struct (type, /*attributes=*/NULL_TREE);
8940 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8941 parser->in_statement = in_statement;
8942 parser->in_switch_statement_p = in_switch_statement_p;
8943 parser->fully_implicit_function_template_p
8944 = fully_implicit_function_template_p;
8945 parser->implicit_template_parms = implicit_template_parms;
8946 parser->implicit_template_scope = implicit_template_scope;
8947 parser->auto_is_implicit_function_template_parm_p
8948 = auto_is_implicit_function_template_parm_p;
8951 pop_deferring_access_checks ();
8953 /* This field is only used during parsing of the lambda. */
8954 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8956 /* This lambda shouldn't have any proxies left at this point. */
8957 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8958 /* And now that we're done, push proxies for an enclosing lambda. */
8959 insert_pending_capture_proxies ();
8961 if (ok)
8962 return build_lambda_object (lambda_expr);
8963 else
8964 return error_mark_node;
8967 /* Parse the beginning of a lambda expression.
8969 lambda-introducer:
8970 [ lambda-capture [opt] ]
8972 LAMBDA_EXPR is the current representation of the lambda expression. */
8974 static void
8975 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8977 /* Need commas after the first capture. */
8978 bool first = true;
8980 /* Eat the leading `['. */
8981 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8983 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8984 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8985 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8986 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8987 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8988 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8990 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8992 cp_lexer_consume_token (parser->lexer);
8993 first = false;
8996 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8998 cp_token* capture_token;
8999 tree capture_id;
9000 tree capture_init_expr;
9001 cp_id_kind idk = CP_ID_KIND_NONE;
9002 bool explicit_init_p = false;
9004 enum capture_kind_type
9006 BY_COPY,
9007 BY_REFERENCE
9009 enum capture_kind_type capture_kind = BY_COPY;
9011 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9013 error ("expected end of capture-list");
9014 return;
9017 if (first)
9018 first = false;
9019 else
9020 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9022 /* Possibly capture `this'. */
9023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9025 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9026 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9027 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9028 "with by-copy capture default");
9029 cp_lexer_consume_token (parser->lexer);
9030 add_capture (lambda_expr,
9031 /*id=*/this_identifier,
9032 /*initializer=*/finish_this_expr(),
9033 /*by_reference_p=*/false,
9034 explicit_init_p);
9035 continue;
9038 /* Remember whether we want to capture as a reference or not. */
9039 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9041 capture_kind = BY_REFERENCE;
9042 cp_lexer_consume_token (parser->lexer);
9045 /* Get the identifier. */
9046 capture_token = cp_lexer_peek_token (parser->lexer);
9047 capture_id = cp_parser_identifier (parser);
9049 if (capture_id == error_mark_node)
9050 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9051 delimiters, but I modified this to stop on unnested ']' as well. It
9052 was already changed to stop on unnested '}', so the
9053 "closing_parenthesis" name is no more misleading with my change. */
9055 cp_parser_skip_to_closing_parenthesis (parser,
9056 /*recovering=*/true,
9057 /*or_comma=*/true,
9058 /*consume_paren=*/true);
9059 break;
9062 /* Find the initializer for this capture. */
9063 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9064 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9065 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9067 bool direct, non_constant;
9068 /* An explicit initializer exists. */
9069 if (cxx_dialect < cxx14)
9070 pedwarn (input_location, 0,
9071 "lambda capture initializers "
9072 "only available with -std=c++14 or -std=gnu++14");
9073 capture_init_expr = cp_parser_initializer (parser, &direct,
9074 &non_constant);
9075 explicit_init_p = true;
9076 if (capture_init_expr == NULL_TREE)
9078 error ("empty initializer for lambda init-capture");
9079 capture_init_expr = error_mark_node;
9082 else
9084 const char* error_msg;
9086 /* Turn the identifier into an id-expression. */
9087 capture_init_expr
9088 = cp_parser_lookup_name_simple (parser, capture_id,
9089 capture_token->location);
9091 if (capture_init_expr == error_mark_node)
9093 unqualified_name_lookup_error (capture_id);
9094 continue;
9096 else if (DECL_P (capture_init_expr)
9097 && (!VAR_P (capture_init_expr)
9098 && TREE_CODE (capture_init_expr) != PARM_DECL))
9100 error_at (capture_token->location,
9101 "capture of non-variable %qD ",
9102 capture_init_expr);
9103 inform (0, "%q+#D declared here", capture_init_expr);
9104 continue;
9106 if (VAR_P (capture_init_expr)
9107 && decl_storage_duration (capture_init_expr) != dk_auto)
9109 if (pedwarn (capture_token->location, 0, "capture of variable "
9110 "%qD with non-automatic storage duration",
9111 capture_init_expr))
9112 inform (0, "%q+#D declared here", capture_init_expr);
9113 continue;
9116 capture_init_expr
9117 = finish_id_expression
9118 (capture_id,
9119 capture_init_expr,
9120 parser->scope,
9121 &idk,
9122 /*integral_constant_expression_p=*/false,
9123 /*allow_non_integral_constant_expression_p=*/false,
9124 /*non_integral_constant_expression_p=*/NULL,
9125 /*template_p=*/false,
9126 /*done=*/true,
9127 /*address_p=*/false,
9128 /*template_arg_p=*/false,
9129 &error_msg,
9130 capture_token->location);
9132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9134 cp_lexer_consume_token (parser->lexer);
9135 capture_init_expr = make_pack_expansion (capture_init_expr);
9137 else
9138 check_for_bare_parameter_packs (capture_init_expr);
9141 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9142 && !explicit_init_p)
9144 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9145 && capture_kind == BY_COPY)
9146 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9147 "of %qD redundant with by-copy capture default",
9148 capture_id);
9149 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9150 && capture_kind == BY_REFERENCE)
9151 pedwarn (capture_token->location, 0, "explicit by-reference "
9152 "capture of %qD redundant with by-reference capture "
9153 "default", capture_id);
9156 add_capture (lambda_expr,
9157 capture_id,
9158 capture_init_expr,
9159 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9160 explicit_init_p);
9163 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9166 /* Parse the (optional) middle of a lambda expression.
9168 lambda-declarator:
9169 < template-parameter-list [opt] >
9170 ( parameter-declaration-clause [opt] )
9171 attribute-specifier [opt]
9172 mutable [opt]
9173 exception-specification [opt]
9174 lambda-return-type-clause [opt]
9176 LAMBDA_EXPR is the current representation of the lambda expression. */
9178 static bool
9179 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
9181 /* 5.1.1.4 of the standard says:
9182 If a lambda-expression does not include a lambda-declarator, it is as if
9183 the lambda-declarator were ().
9184 This means an empty parameter list, no attributes, and no exception
9185 specification. */
9186 tree param_list = void_list_node;
9187 tree attributes = NULL_TREE;
9188 tree exception_spec = NULL_TREE;
9189 tree template_param_list = NULL_TREE;
9191 /* The template-parameter-list is optional, but must begin with
9192 an opening angle if present. */
9193 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
9195 if (cxx_dialect < cxx14)
9196 pedwarn (parser->lexer->next_token->location, 0,
9197 "lambda templates are only available with "
9198 "-std=c++14 or -std=gnu++14");
9200 cp_lexer_consume_token (parser->lexer);
9202 template_param_list = cp_parser_template_parameter_list (parser);
9204 cp_parser_skip_to_end_of_template_parameter_list (parser);
9206 /* We just processed one more parameter list. */
9207 ++parser->num_template_parameter_lists;
9210 /* The parameter-declaration-clause is optional (unless
9211 template-parameter-list was given), but must begin with an
9212 opening parenthesis if present. */
9213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9215 cp_lexer_consume_token (parser->lexer);
9217 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
9219 /* Parse parameters. */
9220 param_list = cp_parser_parameter_declaration_clause (parser);
9222 /* Default arguments shall not be specified in the
9223 parameter-declaration-clause of a lambda-declarator. */
9224 for (tree t = param_list; t; t = TREE_CHAIN (t))
9225 if (TREE_PURPOSE (t))
9226 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
9227 "default argument specified for lambda parameter");
9229 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9231 attributes = cp_parser_attributes_opt (parser);
9233 /* Parse optional `mutable' keyword. */
9234 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
9236 cp_lexer_consume_token (parser->lexer);
9237 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
9240 /* Parse optional exception specification. */
9241 exception_spec = cp_parser_exception_specification_opt (parser);
9243 /* Parse optional trailing return type. */
9244 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
9246 cp_lexer_consume_token (parser->lexer);
9247 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9248 = cp_parser_trailing_type_id (parser);
9251 /* The function parameters must be in scope all the way until after the
9252 trailing-return-type in case of decltype. */
9253 pop_bindings_and_leave_scope ();
9255 else if (template_param_list != NULL_TREE) // generate diagnostic
9256 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9258 /* Create the function call operator.
9260 Messing with declarators like this is no uglier than building up the
9261 FUNCTION_DECL by hand, and this is less likely to get out of sync with
9262 other code. */
9264 cp_decl_specifier_seq return_type_specs;
9265 cp_declarator* declarator;
9266 tree fco;
9267 int quals;
9268 void *p;
9270 clear_decl_specs (&return_type_specs);
9271 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9272 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
9273 else
9274 /* Maybe we will deduce the return type later. */
9275 return_type_specs.type = make_auto ();
9277 p = obstack_alloc (&declarator_obstack, 0);
9279 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
9280 sfk_none);
9282 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
9283 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
9284 declarator = make_call_declarator (declarator, param_list, quals,
9285 VIRT_SPEC_UNSPECIFIED,
9286 REF_QUAL_NONE,
9287 exception_spec,
9288 /*late_return_type=*/NULL_TREE);
9289 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
9291 fco = grokmethod (&return_type_specs,
9292 declarator,
9293 attributes);
9294 if (fco != error_mark_node)
9296 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
9297 DECL_ARTIFICIAL (fco) = 1;
9298 /* Give the object parameter a different name. */
9299 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
9300 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
9301 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
9303 if (template_param_list)
9305 fco = finish_member_template_decl (fco);
9306 finish_template_decl (template_param_list);
9307 --parser->num_template_parameter_lists;
9309 else if (parser->fully_implicit_function_template_p)
9310 fco = finish_fully_implicit_template (parser, fco);
9312 finish_member_declaration (fco);
9314 obstack_free (&declarator_obstack, p);
9316 return (fco != error_mark_node);
9320 /* Parse the body of a lambda expression, which is simply
9322 compound-statement
9324 but which requires special handling.
9325 LAMBDA_EXPR is the current representation of the lambda expression. */
9327 static void
9328 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
9330 bool nested = (current_function_decl != NULL_TREE);
9331 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
9332 if (nested)
9333 push_function_context ();
9334 else
9335 /* Still increment function_depth so that we don't GC in the
9336 middle of an expression. */
9337 ++function_depth;
9338 /* Clear this in case we're in the middle of a default argument. */
9339 parser->local_variables_forbidden_p = false;
9341 /* Finish the function call operator
9342 - class_specifier
9343 + late_parsing_for_member
9344 + function_definition_after_declarator
9345 + ctor_initializer_opt_and_function_body */
9347 tree fco = lambda_function (lambda_expr);
9348 tree body;
9349 bool done = false;
9350 tree compound_stmt;
9351 tree cap;
9353 /* Let the front end know that we are going to be defining this
9354 function. */
9355 start_preparsed_function (fco,
9356 NULL_TREE,
9357 SF_PRE_PARSED | SF_INCLASS_INLINE);
9359 start_lambda_scope (fco);
9360 body = begin_function_body ();
9362 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9363 goto out;
9365 /* Push the proxies for any explicit captures. */
9366 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
9367 cap = TREE_CHAIN (cap))
9368 build_capture_proxy (TREE_PURPOSE (cap));
9370 compound_stmt = begin_compound_stmt (0);
9372 /* 5.1.1.4 of the standard says:
9373 If a lambda-expression does not include a trailing-return-type, it
9374 is as if the trailing-return-type denotes the following type:
9375 * if the compound-statement is of the form
9376 { return attribute-specifier [opt] expression ; }
9377 the type of the returned expression after lvalue-to-rvalue
9378 conversion (_conv.lval_ 4.1), array-to-pointer conversion
9379 (_conv.array_ 4.2), and function-to-pointer conversion
9380 (_conv.func_ 4.3);
9381 * otherwise, void. */
9383 /* In a lambda that has neither a lambda-return-type-clause
9384 nor a deducible form, errors should be reported for return statements
9385 in the body. Since we used void as the placeholder return type, parsing
9386 the body as usual will give such desired behavior. */
9387 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
9388 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
9389 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
9391 tree expr = NULL_TREE;
9392 cp_id_kind idk = CP_ID_KIND_NONE;
9394 /* Parse tentatively in case there's more after the initial return
9395 statement. */
9396 cp_parser_parse_tentatively (parser);
9398 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
9400 expr = cp_parser_expression (parser, &idk);
9402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9403 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9405 if (cp_parser_parse_definitely (parser))
9407 if (!processing_template_decl)
9408 apply_deduced_return_type (fco, lambda_return_type (expr));
9410 /* Will get error here if type not deduced yet. */
9411 finish_return_stmt (expr);
9413 done = true;
9417 if (!done)
9419 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9420 cp_parser_label_declaration (parser);
9421 cp_parser_statement_seq_opt (parser, NULL_TREE);
9422 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9425 finish_compound_stmt (compound_stmt);
9427 out:
9428 finish_function_body (body);
9429 finish_lambda_scope ();
9431 /* Finish the function and generate code for it if necessary. */
9432 tree fn = finish_function (/*inline*/2);
9434 /* Only expand if the call op is not a template. */
9435 if (!DECL_TEMPLATE_INFO (fco))
9436 expand_or_defer_fn (fn);
9439 parser->local_variables_forbidden_p = local_variables_forbidden_p;
9440 if (nested)
9441 pop_function_context();
9442 else
9443 --function_depth;
9446 /* Statements [gram.stmt.stmt] */
9448 /* Parse a statement.
9450 statement:
9451 labeled-statement
9452 expression-statement
9453 compound-statement
9454 selection-statement
9455 iteration-statement
9456 jump-statement
9457 declaration-statement
9458 try-block
9460 C++11:
9462 statement:
9463 labeled-statement
9464 attribute-specifier-seq (opt) expression-statement
9465 attribute-specifier-seq (opt) compound-statement
9466 attribute-specifier-seq (opt) selection-statement
9467 attribute-specifier-seq (opt) iteration-statement
9468 attribute-specifier-seq (opt) jump-statement
9469 declaration-statement
9470 attribute-specifier-seq (opt) try-block
9472 TM Extension:
9474 statement:
9475 atomic-statement
9477 IN_COMPOUND is true when the statement is nested inside a
9478 cp_parser_compound_statement; this matters for certain pragmas.
9480 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9481 is a (possibly labeled) if statement which is not enclosed in braces
9482 and has an else clause. This is used to implement -Wparentheses. */
9484 static void
9485 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
9486 bool in_compound, bool *if_p)
9488 tree statement, std_attrs = NULL_TREE;
9489 cp_token *token;
9490 location_t statement_location, attrs_location;
9492 restart:
9493 if (if_p != NULL)
9494 *if_p = false;
9495 /* There is no statement yet. */
9496 statement = NULL_TREE;
9498 cp_lexer_save_tokens (parser->lexer);
9499 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
9500 if (c_dialect_objc ())
9501 /* In obj-c++, seeing '[[' might be the either the beginning of
9502 c++11 attributes, or a nested objc-message-expression. So
9503 let's parse the c++11 attributes tentatively. */
9504 cp_parser_parse_tentatively (parser);
9505 std_attrs = cp_parser_std_attribute_spec_seq (parser);
9506 if (c_dialect_objc ())
9508 if (!cp_parser_parse_definitely (parser))
9509 std_attrs = NULL_TREE;
9512 /* Peek at the next token. */
9513 token = cp_lexer_peek_token (parser->lexer);
9514 /* Remember the location of the first token in the statement. */
9515 statement_location = token->location;
9516 /* If this is a keyword, then that will often determine what kind of
9517 statement we have. */
9518 if (token->type == CPP_KEYWORD)
9520 enum rid keyword = token->keyword;
9522 switch (keyword)
9524 case RID_CASE:
9525 case RID_DEFAULT:
9526 /* Looks like a labeled-statement with a case label.
9527 Parse the label, and then use tail recursion to parse
9528 the statement. */
9529 cp_parser_label_for_labeled_statement (parser, std_attrs);
9530 goto restart;
9532 case RID_IF:
9533 case RID_SWITCH:
9534 statement = cp_parser_selection_statement (parser, if_p);
9535 break;
9537 case RID_WHILE:
9538 case RID_DO:
9539 case RID_FOR:
9540 statement = cp_parser_iteration_statement (parser, false);
9541 break;
9543 case RID_CILK_FOR:
9544 if (!flag_cilkplus)
9546 error_at (cp_lexer_peek_token (parser->lexer)->location,
9547 "-fcilkplus must be enabled to use %<_Cilk_for%>");
9548 cp_lexer_consume_token (parser->lexer);
9549 statement = error_mark_node;
9551 else
9552 statement = cp_parser_cilk_for (parser, integer_zero_node);
9553 break;
9555 case RID_BREAK:
9556 case RID_CONTINUE:
9557 case RID_RETURN:
9558 case RID_GOTO:
9559 statement = cp_parser_jump_statement (parser);
9560 break;
9562 case RID_CILK_SYNC:
9563 cp_lexer_consume_token (parser->lexer);
9564 if (flag_cilkplus)
9566 tree sync_expr = build_cilk_sync ();
9567 SET_EXPR_LOCATION (sync_expr,
9568 token->location);
9569 statement = finish_expr_stmt (sync_expr);
9571 else
9573 error_at (token->location, "-fcilkplus must be enabled to use"
9574 " %<_Cilk_sync%>");
9575 statement = error_mark_node;
9577 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9578 break;
9580 /* Objective-C++ exception-handling constructs. */
9581 case RID_AT_TRY:
9582 case RID_AT_CATCH:
9583 case RID_AT_FINALLY:
9584 case RID_AT_SYNCHRONIZED:
9585 case RID_AT_THROW:
9586 statement = cp_parser_objc_statement (parser);
9587 break;
9589 case RID_TRY:
9590 statement = cp_parser_try_block (parser);
9591 break;
9593 case RID_NAMESPACE:
9594 /* This must be a namespace alias definition. */
9595 cp_parser_declaration_statement (parser);
9596 return;
9598 case RID_TRANSACTION_ATOMIC:
9599 case RID_TRANSACTION_RELAXED:
9600 statement = cp_parser_transaction (parser, keyword);
9601 break;
9602 case RID_TRANSACTION_CANCEL:
9603 statement = cp_parser_transaction_cancel (parser);
9604 break;
9606 default:
9607 /* It might be a keyword like `int' that can start a
9608 declaration-statement. */
9609 break;
9612 else if (token->type == CPP_NAME)
9614 /* If the next token is a `:', then we are looking at a
9615 labeled-statement. */
9616 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9617 if (token->type == CPP_COLON)
9619 /* Looks like a labeled-statement with an ordinary label.
9620 Parse the label, and then use tail recursion to parse
9621 the statement. */
9623 cp_parser_label_for_labeled_statement (parser, std_attrs);
9624 goto restart;
9627 /* Anything that starts with a `{' must be a compound-statement. */
9628 else if (token->type == CPP_OPEN_BRACE)
9629 statement = cp_parser_compound_statement (parser, NULL, false, false);
9630 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
9631 a statement all its own. */
9632 else if (token->type == CPP_PRAGMA)
9634 /* Only certain OpenMP pragmas are attached to statements, and thus
9635 are considered statements themselves. All others are not. In
9636 the context of a compound, accept the pragma as a "statement" and
9637 return so that we can check for a close brace. Otherwise we
9638 require a real statement and must go back and read one. */
9639 if (in_compound)
9640 cp_parser_pragma (parser, pragma_compound);
9641 else if (!cp_parser_pragma (parser, pragma_stmt))
9642 goto restart;
9643 return;
9645 else if (token->type == CPP_EOF)
9647 cp_parser_error (parser, "expected statement");
9648 return;
9651 /* Everything else must be a declaration-statement or an
9652 expression-statement. Try for the declaration-statement
9653 first, unless we are looking at a `;', in which case we know that
9654 we have an expression-statement. */
9655 if (!statement)
9657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9659 if (std_attrs != NULL_TREE)
9661 /* Attributes should be parsed as part of the the
9662 declaration, so let's un-parse them. */
9663 cp_lexer_rollback_tokens (parser->lexer);
9664 std_attrs = NULL_TREE;
9667 cp_parser_parse_tentatively (parser);
9668 /* Try to parse the declaration-statement. */
9669 cp_parser_declaration_statement (parser);
9670 /* If that worked, we're done. */
9671 if (cp_parser_parse_definitely (parser))
9672 return;
9674 /* Look for an expression-statement instead. */
9675 statement = cp_parser_expression_statement (parser, in_statement_expr);
9678 /* Set the line number for the statement. */
9679 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
9680 SET_EXPR_LOCATION (statement, statement_location);
9682 /* Note that for now, we don't do anything with c++11 statements
9683 parsed at this level. */
9684 if (std_attrs != NULL_TREE)
9685 warning_at (attrs_location,
9686 OPT_Wattributes,
9687 "attributes at the beginning of statement are ignored");
9690 /* Parse the label for a labeled-statement, i.e.
9692 identifier :
9693 case constant-expression :
9694 default :
9696 GNU Extension:
9697 case constant-expression ... constant-expression : statement
9699 When a label is parsed without errors, the label is added to the
9700 parse tree by the finish_* functions, so this function doesn't
9701 have to return the label. */
9703 static void
9704 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
9706 cp_token *token;
9707 tree label = NULL_TREE;
9708 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9710 /* The next token should be an identifier. */
9711 token = cp_lexer_peek_token (parser->lexer);
9712 if (token->type != CPP_NAME
9713 && token->type != CPP_KEYWORD)
9715 cp_parser_error (parser, "expected labeled-statement");
9716 return;
9719 parser->colon_corrects_to_scope_p = false;
9720 switch (token->keyword)
9722 case RID_CASE:
9724 tree expr, expr_hi;
9725 cp_token *ellipsis;
9727 /* Consume the `case' token. */
9728 cp_lexer_consume_token (parser->lexer);
9729 /* Parse the constant-expression. */
9730 expr = cp_parser_constant_expression (parser,
9731 /*allow_non_constant_p=*/false,
9732 NULL);
9734 ellipsis = cp_lexer_peek_token (parser->lexer);
9735 if (ellipsis->type == CPP_ELLIPSIS)
9737 /* Consume the `...' token. */
9738 cp_lexer_consume_token (parser->lexer);
9739 expr_hi =
9740 cp_parser_constant_expression (parser,
9741 /*allow_non_constant_p=*/false,
9742 NULL);
9743 /* We don't need to emit warnings here, as the common code
9744 will do this for us. */
9746 else
9747 expr_hi = NULL_TREE;
9749 if (parser->in_switch_statement_p)
9750 finish_case_label (token->location, expr, expr_hi);
9751 else
9752 error_at (token->location,
9753 "case label %qE not within a switch statement",
9754 expr);
9756 break;
9758 case RID_DEFAULT:
9759 /* Consume the `default' token. */
9760 cp_lexer_consume_token (parser->lexer);
9762 if (parser->in_switch_statement_p)
9763 finish_case_label (token->location, NULL_TREE, NULL_TREE);
9764 else
9765 error_at (token->location, "case label not within a switch statement");
9766 break;
9768 default:
9769 /* Anything else must be an ordinary label. */
9770 label = finish_label_stmt (cp_parser_identifier (parser));
9771 break;
9774 /* Require the `:' token. */
9775 cp_parser_require (parser, CPP_COLON, RT_COLON);
9777 /* An ordinary label may optionally be followed by attributes.
9778 However, this is only permitted if the attributes are then
9779 followed by a semicolon. This is because, for backward
9780 compatibility, when parsing
9781 lab: __attribute__ ((unused)) int i;
9782 we want the attribute to attach to "i", not "lab". */
9783 if (label != NULL_TREE
9784 && cp_next_tokens_can_be_gnu_attribute_p (parser))
9786 tree attrs;
9787 cp_parser_parse_tentatively (parser);
9788 attrs = cp_parser_gnu_attributes_opt (parser);
9789 if (attrs == NULL_TREE
9790 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9791 cp_parser_abort_tentative_parse (parser);
9792 else if (!cp_parser_parse_definitely (parser))
9794 else
9795 attributes = chainon (attributes, attrs);
9798 if (attributes != NULL_TREE)
9799 cplus_decl_attributes (&label, attributes, 0);
9801 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9804 /* Parse an expression-statement.
9806 expression-statement:
9807 expression [opt] ;
9809 Returns the new EXPR_STMT -- or NULL_TREE if the expression
9810 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
9811 indicates whether this expression-statement is part of an
9812 expression statement. */
9814 static tree
9815 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
9817 tree statement = NULL_TREE;
9818 cp_token *token = cp_lexer_peek_token (parser->lexer);
9820 /* If the next token is a ';', then there is no expression
9821 statement. */
9822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9824 statement = cp_parser_expression (parser);
9825 if (statement == error_mark_node
9826 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9828 cp_parser_skip_to_end_of_block_or_statement (parser);
9829 return error_mark_node;
9833 /* Give a helpful message for "A<T>::type t;" and the like. */
9834 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
9835 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
9837 if (TREE_CODE (statement) == SCOPE_REF)
9838 error_at (token->location, "need %<typename%> before %qE because "
9839 "%qT is a dependent scope",
9840 statement, TREE_OPERAND (statement, 0));
9841 else if (is_overloaded_fn (statement)
9842 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
9844 /* A::A a; */
9845 tree fn = get_first_fn (statement);
9846 error_at (token->location,
9847 "%<%T::%D%> names the constructor, not the type",
9848 DECL_CONTEXT (fn), DECL_NAME (fn));
9852 /* Consume the final `;'. */
9853 cp_parser_consume_semicolon_at_end_of_statement (parser);
9855 if (in_statement_expr
9856 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9857 /* This is the final expression statement of a statement
9858 expression. */
9859 statement = finish_stmt_expr_expr (statement, in_statement_expr);
9860 else if (statement)
9861 statement = finish_expr_stmt (statement);
9863 return statement;
9866 /* Parse a compound-statement.
9868 compound-statement:
9869 { statement-seq [opt] }
9871 GNU extension:
9873 compound-statement:
9874 { label-declaration-seq [opt] statement-seq [opt] }
9876 label-declaration-seq:
9877 label-declaration
9878 label-declaration-seq label-declaration
9880 Returns a tree representing the statement. */
9882 static tree
9883 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
9884 bool in_try, bool function_body)
9886 tree compound_stmt;
9888 /* Consume the `{'. */
9889 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9890 return error_mark_node;
9891 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
9892 && !function_body)
9893 pedwarn (input_location, OPT_Wpedantic,
9894 "compound-statement in constexpr function");
9895 /* Begin the compound-statement. */
9896 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
9897 /* If the next keyword is `__label__' we have a label declaration. */
9898 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9899 cp_parser_label_declaration (parser);
9900 /* Parse an (optional) statement-seq. */
9901 cp_parser_statement_seq_opt (parser, in_statement_expr);
9902 /* Finish the compound-statement. */
9903 finish_compound_stmt (compound_stmt);
9904 /* Consume the `}'. */
9905 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9907 return compound_stmt;
9910 /* Parse an (optional) statement-seq.
9912 statement-seq:
9913 statement
9914 statement-seq [opt] statement */
9916 static void
9917 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
9919 /* Scan statements until there aren't any more. */
9920 while (true)
9922 cp_token *token = cp_lexer_peek_token (parser->lexer);
9924 /* If we are looking at a `}', then we have run out of
9925 statements; the same is true if we have reached the end
9926 of file, or have stumbled upon a stray '@end'. */
9927 if (token->type == CPP_CLOSE_BRACE
9928 || token->type == CPP_EOF
9929 || token->type == CPP_PRAGMA_EOL
9930 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
9931 break;
9933 /* If we are in a compound statement and find 'else' then
9934 something went wrong. */
9935 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
9937 if (parser->in_statement & IN_IF_STMT)
9938 break;
9939 else
9941 token = cp_lexer_consume_token (parser->lexer);
9942 error_at (token->location, "%<else%> without a previous %<if%>");
9946 /* Parse the statement. */
9947 cp_parser_statement (parser, in_statement_expr, true, NULL);
9951 /* Parse a selection-statement.
9953 selection-statement:
9954 if ( condition ) statement
9955 if ( condition ) statement else statement
9956 switch ( condition ) statement
9958 Returns the new IF_STMT or SWITCH_STMT.
9960 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9961 is a (possibly labeled) if statement which is not enclosed in
9962 braces and has an else clause. This is used to implement
9963 -Wparentheses. */
9965 static tree
9966 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9968 cp_token *token;
9969 enum rid keyword;
9971 if (if_p != NULL)
9972 *if_p = false;
9974 /* Peek at the next token. */
9975 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9977 /* See what kind of keyword it is. */
9978 keyword = token->keyword;
9979 switch (keyword)
9981 case RID_IF:
9982 case RID_SWITCH:
9984 tree statement;
9985 tree condition;
9987 /* Look for the `('. */
9988 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9990 cp_parser_skip_to_end_of_statement (parser);
9991 return error_mark_node;
9994 /* Begin the selection-statement. */
9995 if (keyword == RID_IF)
9996 statement = begin_if_stmt ();
9997 else
9998 statement = begin_switch_stmt ();
10000 /* Parse the condition. */
10001 condition = cp_parser_condition (parser);
10002 /* Look for the `)'. */
10003 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10004 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10005 /*consume_paren=*/true);
10007 if (keyword == RID_IF)
10009 bool nested_if;
10010 unsigned char in_statement;
10012 /* Add the condition. */
10013 finish_if_stmt_cond (condition, statement);
10015 /* Parse the then-clause. */
10016 in_statement = parser->in_statement;
10017 parser->in_statement |= IN_IF_STMT;
10018 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10021 add_stmt (build_empty_stmt (loc));
10022 cp_lexer_consume_token (parser->lexer);
10023 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
10024 warning_at (loc, OPT_Wempty_body, "suggest braces around "
10025 "empty body in an %<if%> statement");
10026 nested_if = false;
10028 else
10029 cp_parser_implicitly_scoped_statement (parser, &nested_if);
10030 parser->in_statement = in_statement;
10032 finish_then_clause (statement);
10034 /* If the next token is `else', parse the else-clause. */
10035 if (cp_lexer_next_token_is_keyword (parser->lexer,
10036 RID_ELSE))
10038 /* Consume the `else' keyword. */
10039 cp_lexer_consume_token (parser->lexer);
10040 begin_else_clause (statement);
10041 /* Parse the else-clause. */
10042 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10044 location_t loc;
10045 loc = cp_lexer_peek_token (parser->lexer)->location;
10046 warning_at (loc,
10047 OPT_Wempty_body, "suggest braces around "
10048 "empty body in an %<else%> statement");
10049 add_stmt (build_empty_stmt (loc));
10050 cp_lexer_consume_token (parser->lexer);
10052 else
10053 cp_parser_implicitly_scoped_statement (parser, NULL);
10055 finish_else_clause (statement);
10057 /* If we are currently parsing a then-clause, then
10058 IF_P will not be NULL. We set it to true to
10059 indicate that this if statement has an else clause.
10060 This may trigger the Wparentheses warning below
10061 when we get back up to the parent if statement. */
10062 if (if_p != NULL)
10063 *if_p = true;
10065 else
10067 /* This if statement does not have an else clause. If
10068 NESTED_IF is true, then the then-clause is an if
10069 statement which does have an else clause. We warn
10070 about the potential ambiguity. */
10071 if (nested_if)
10072 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10073 "suggest explicit braces to avoid ambiguous"
10074 " %<else%>");
10077 /* Now we're all done with the if-statement. */
10078 finish_if_stmt (statement);
10080 else
10082 bool in_switch_statement_p;
10083 unsigned char in_statement;
10085 /* Add the condition. */
10086 finish_switch_cond (condition, statement);
10088 /* Parse the body of the switch-statement. */
10089 in_switch_statement_p = parser->in_switch_statement_p;
10090 in_statement = parser->in_statement;
10091 parser->in_switch_statement_p = true;
10092 parser->in_statement |= IN_SWITCH_STMT;
10093 cp_parser_implicitly_scoped_statement (parser, NULL);
10094 parser->in_switch_statement_p = in_switch_statement_p;
10095 parser->in_statement = in_statement;
10097 /* Now we're all done with the switch-statement. */
10098 finish_switch_stmt (statement);
10101 return statement;
10103 break;
10105 default:
10106 cp_parser_error (parser, "expected selection-statement");
10107 return error_mark_node;
10111 /* Parse a condition.
10113 condition:
10114 expression
10115 type-specifier-seq declarator = initializer-clause
10116 type-specifier-seq declarator braced-init-list
10118 GNU Extension:
10120 condition:
10121 type-specifier-seq declarator asm-specification [opt]
10122 attributes [opt] = assignment-expression
10124 Returns the expression that should be tested. */
10126 static tree
10127 cp_parser_condition (cp_parser* parser)
10129 cp_decl_specifier_seq type_specifiers;
10130 const char *saved_message;
10131 int declares_class_or_enum;
10133 /* Try the declaration first. */
10134 cp_parser_parse_tentatively (parser);
10135 /* New types are not allowed in the type-specifier-seq for a
10136 condition. */
10137 saved_message = parser->type_definition_forbidden_message;
10138 parser->type_definition_forbidden_message
10139 = G_("types may not be defined in conditions");
10140 /* Parse the type-specifier-seq. */
10141 cp_parser_decl_specifier_seq (parser,
10142 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
10143 &type_specifiers,
10144 &declares_class_or_enum);
10145 /* Restore the saved message. */
10146 parser->type_definition_forbidden_message = saved_message;
10147 /* If all is well, we might be looking at a declaration. */
10148 if (!cp_parser_error_occurred (parser))
10150 tree decl;
10151 tree asm_specification;
10152 tree attributes;
10153 cp_declarator *declarator;
10154 tree initializer = NULL_TREE;
10156 /* Parse the declarator. */
10157 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10158 /*ctor_dtor_or_conv_p=*/NULL,
10159 /*parenthesized_p=*/NULL,
10160 /*member_p=*/false,
10161 /*friend_p=*/false);
10162 /* Parse the attributes. */
10163 attributes = cp_parser_attributes_opt (parser);
10164 /* Parse the asm-specification. */
10165 asm_specification = cp_parser_asm_specification_opt (parser);
10166 /* If the next token is not an `=' or '{', then we might still be
10167 looking at an expression. For example:
10169 if (A(a).x)
10171 looks like a decl-specifier-seq and a declarator -- but then
10172 there is no `=', so this is an expression. */
10173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10174 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10175 cp_parser_simulate_error (parser);
10177 /* If we did see an `=' or '{', then we are looking at a declaration
10178 for sure. */
10179 if (cp_parser_parse_definitely (parser))
10181 tree pushed_scope;
10182 bool non_constant_p;
10183 bool flags = LOOKUP_ONLYCONVERTING;
10185 /* Create the declaration. */
10186 decl = start_decl (declarator, &type_specifiers,
10187 /*initialized_p=*/true,
10188 attributes, /*prefix_attributes=*/NULL_TREE,
10189 &pushed_scope);
10191 /* Parse the initializer. */
10192 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10194 initializer = cp_parser_braced_list (parser, &non_constant_p);
10195 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
10196 flags = 0;
10198 else
10200 /* Consume the `='. */
10201 cp_parser_require (parser, CPP_EQ, RT_EQ);
10202 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
10204 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
10205 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10207 /* Process the initializer. */
10208 cp_finish_decl (decl,
10209 initializer, !non_constant_p,
10210 asm_specification,
10211 flags);
10213 if (pushed_scope)
10214 pop_scope (pushed_scope);
10216 return convert_from_reference (decl);
10219 /* If we didn't even get past the declarator successfully, we are
10220 definitely not looking at a declaration. */
10221 else
10222 cp_parser_abort_tentative_parse (parser);
10224 /* Otherwise, we are looking at an expression. */
10225 return cp_parser_expression (parser);
10228 /* Parses a for-statement or range-for-statement until the closing ')',
10229 not included. */
10231 static tree
10232 cp_parser_for (cp_parser *parser, bool ivdep)
10234 tree init, scope, decl;
10235 bool is_range_for;
10237 /* Begin the for-statement. */
10238 scope = begin_for_scope (&init);
10240 /* Parse the initialization. */
10241 is_range_for = cp_parser_for_init_statement (parser, &decl);
10243 if (is_range_for)
10244 return cp_parser_range_for (parser, scope, init, decl, ivdep);
10245 else
10246 return cp_parser_c_for (parser, scope, init, ivdep);
10249 static tree
10250 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
10252 /* Normal for loop */
10253 tree condition = NULL_TREE;
10254 tree expression = NULL_TREE;
10255 tree stmt;
10257 stmt = begin_for_stmt (scope, init);
10258 /* The for-init-statement has already been parsed in
10259 cp_parser_for_init_statement, so no work is needed here. */
10260 finish_for_init_stmt (stmt);
10262 /* If there's a condition, process it. */
10263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10264 condition = cp_parser_condition (parser);
10265 else if (ivdep)
10267 cp_parser_error (parser, "missing loop condition in loop with "
10268 "%<GCC ivdep%> pragma");
10269 condition = error_mark_node;
10271 finish_for_cond (condition, stmt, ivdep);
10272 /* Look for the `;'. */
10273 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10275 /* If there's an expression, process it. */
10276 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
10277 expression = cp_parser_expression (parser);
10278 finish_for_expr (expression, stmt);
10280 return stmt;
10283 /* Tries to parse a range-based for-statement:
10285 range-based-for:
10286 decl-specifier-seq declarator : expression
10288 The decl-specifier-seq declarator and the `:' are already parsed by
10289 cp_parser_for_init_statement. If processing_template_decl it returns a
10290 newly created RANGE_FOR_STMT; if not, it is converted to a
10291 regular FOR_STMT. */
10293 static tree
10294 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
10295 bool ivdep)
10297 tree stmt, range_expr;
10299 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10301 bool expr_non_constant_p;
10302 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10304 else
10305 range_expr = cp_parser_expression (parser);
10307 /* If in template, STMT is converted to a normal for-statement
10308 at instantiation. If not, it is done just ahead. */
10309 if (processing_template_decl)
10311 if (check_for_bare_parameter_packs (range_expr))
10312 range_expr = error_mark_node;
10313 stmt = begin_range_for_stmt (scope, init);
10314 if (ivdep)
10315 RANGE_FOR_IVDEP (stmt) = 1;
10316 finish_range_for_decl (stmt, range_decl, range_expr);
10317 if (!type_dependent_expression_p (range_expr)
10318 /* do_auto_deduction doesn't mess with template init-lists. */
10319 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
10320 do_range_for_auto_deduction (range_decl, range_expr);
10322 else
10324 stmt = begin_for_stmt (scope, init);
10325 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
10327 return stmt;
10330 /* Subroutine of cp_convert_range_for: given the initializer expression,
10331 builds up the range temporary. */
10333 static tree
10334 build_range_temp (tree range_expr)
10336 tree range_type, range_temp;
10338 /* Find out the type deduced by the declaration
10339 `auto &&__range = range_expr'. */
10340 range_type = cp_build_reference_type (make_auto (), true);
10341 range_type = do_auto_deduction (range_type, range_expr,
10342 type_uses_auto (range_type));
10344 /* Create the __range variable. */
10345 range_temp = build_decl (input_location, VAR_DECL,
10346 get_identifier ("__for_range"), range_type);
10347 TREE_USED (range_temp) = 1;
10348 DECL_ARTIFICIAL (range_temp) = 1;
10350 return range_temp;
10353 /* Used by cp_parser_range_for in template context: we aren't going to
10354 do a full conversion yet, but we still need to resolve auto in the
10355 type of the for-range-declaration if present. This is basically
10356 a shortcut version of cp_convert_range_for. */
10358 static void
10359 do_range_for_auto_deduction (tree decl, tree range_expr)
10361 tree auto_node = type_uses_auto (TREE_TYPE (decl));
10362 if (auto_node)
10364 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
10365 range_temp = convert_from_reference (build_range_temp (range_expr));
10366 iter_type = (cp_parser_perform_range_for_lookup
10367 (range_temp, &begin_dummy, &end_dummy));
10368 if (iter_type)
10370 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
10371 iter_type);
10372 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
10373 tf_warning_or_error);
10374 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
10375 iter_decl, auto_node);
10380 /* Converts a range-based for-statement into a normal
10381 for-statement, as per the definition.
10383 for (RANGE_DECL : RANGE_EXPR)
10384 BLOCK
10386 should be equivalent to:
10389 auto &&__range = RANGE_EXPR;
10390 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
10391 __begin != __end;
10392 ++__begin)
10394 RANGE_DECL = *__begin;
10395 BLOCK
10399 If RANGE_EXPR is an array:
10400 BEGIN_EXPR = __range
10401 END_EXPR = __range + ARRAY_SIZE(__range)
10402 Else if RANGE_EXPR has a member 'begin' or 'end':
10403 BEGIN_EXPR = __range.begin()
10404 END_EXPR = __range.end()
10405 Else:
10406 BEGIN_EXPR = begin(__range)
10407 END_EXPR = end(__range);
10409 If __range has a member 'begin' but not 'end', or vice versa, we must
10410 still use the second alternative (it will surely fail, however).
10411 When calling begin()/end() in the third alternative we must use
10412 argument dependent lookup, but always considering 'std' as an associated
10413 namespace. */
10415 tree
10416 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
10417 bool ivdep)
10419 tree begin, end;
10420 tree iter_type, begin_expr, end_expr;
10421 tree condition, expression;
10423 if (range_decl == error_mark_node || range_expr == error_mark_node)
10424 /* If an error happened previously do nothing or else a lot of
10425 unhelpful errors would be issued. */
10426 begin_expr = end_expr = iter_type = error_mark_node;
10427 else
10429 tree range_temp;
10431 if (TREE_CODE (range_expr) == VAR_DECL
10432 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
10433 /* Can't bind a reference to an array of runtime bound. */
10434 range_temp = range_expr;
10435 else
10437 range_temp = build_range_temp (range_expr);
10438 pushdecl (range_temp);
10439 cp_finish_decl (range_temp, range_expr,
10440 /*is_constant_init*/false, NULL_TREE,
10441 LOOKUP_ONLYCONVERTING);
10442 range_temp = convert_from_reference (range_temp);
10444 iter_type = cp_parser_perform_range_for_lookup (range_temp,
10445 &begin_expr, &end_expr);
10448 /* The new for initialization statement. */
10449 begin = build_decl (input_location, VAR_DECL,
10450 get_identifier ("__for_begin"), iter_type);
10451 TREE_USED (begin) = 1;
10452 DECL_ARTIFICIAL (begin) = 1;
10453 pushdecl (begin);
10454 cp_finish_decl (begin, begin_expr,
10455 /*is_constant_init*/false, NULL_TREE,
10456 LOOKUP_ONLYCONVERTING);
10458 end = build_decl (input_location, VAR_DECL,
10459 get_identifier ("__for_end"), iter_type);
10460 TREE_USED (end) = 1;
10461 DECL_ARTIFICIAL (end) = 1;
10462 pushdecl (end);
10463 cp_finish_decl (end, end_expr,
10464 /*is_constant_init*/false, NULL_TREE,
10465 LOOKUP_ONLYCONVERTING);
10467 finish_for_init_stmt (statement);
10469 /* The new for condition. */
10470 condition = build_x_binary_op (input_location, NE_EXPR,
10471 begin, ERROR_MARK,
10472 end, ERROR_MARK,
10473 NULL, tf_warning_or_error);
10474 finish_for_cond (condition, statement, ivdep);
10476 /* The new increment expression. */
10477 expression = finish_unary_op_expr (input_location,
10478 PREINCREMENT_EXPR, begin,
10479 tf_warning_or_error);
10480 finish_for_expr (expression, statement);
10482 /* The declaration is initialized with *__begin inside the loop body. */
10483 cp_finish_decl (range_decl,
10484 build_x_indirect_ref (input_location, begin, RO_NULL,
10485 tf_warning_or_error),
10486 /*is_constant_init*/false, NULL_TREE,
10487 LOOKUP_ONLYCONVERTING);
10489 return statement;
10492 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
10493 We need to solve both at the same time because the method used
10494 depends on the existence of members begin or end.
10495 Returns the type deduced for the iterator expression. */
10497 static tree
10498 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
10500 if (error_operand_p (range))
10502 *begin = *end = error_mark_node;
10503 return error_mark_node;
10506 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
10508 error ("range-based %<for%> expression of type %qT "
10509 "has incomplete type", TREE_TYPE (range));
10510 *begin = *end = error_mark_node;
10511 return error_mark_node;
10513 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
10515 /* If RANGE is an array, we will use pointer arithmetic. */
10516 *begin = range;
10517 *end = build_binary_op (input_location, PLUS_EXPR,
10518 range,
10519 array_type_nelts_top (TREE_TYPE (range)),
10521 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
10523 else
10525 /* If it is not an array, we must do a bit of magic. */
10526 tree id_begin, id_end;
10527 tree member_begin, member_end;
10529 *begin = *end = error_mark_node;
10531 id_begin = get_identifier ("begin");
10532 id_end = get_identifier ("end");
10533 member_begin = lookup_member (TREE_TYPE (range), id_begin,
10534 /*protect=*/2, /*want_type=*/false,
10535 tf_warning_or_error);
10536 member_end = lookup_member (TREE_TYPE (range), id_end,
10537 /*protect=*/2, /*want_type=*/false,
10538 tf_warning_or_error);
10540 if (member_begin != NULL_TREE || member_end != NULL_TREE)
10542 /* Use the member functions. */
10543 if (member_begin != NULL_TREE)
10544 *begin = cp_parser_range_for_member_function (range, id_begin);
10545 else
10546 error ("range-based %<for%> expression of type %qT has an "
10547 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
10549 if (member_end != NULL_TREE)
10550 *end = cp_parser_range_for_member_function (range, id_end);
10551 else
10552 error ("range-based %<for%> expression of type %qT has a "
10553 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
10555 else
10557 /* Use global functions with ADL. */
10558 vec<tree, va_gc> *vec;
10559 vec = make_tree_vector ();
10561 vec_safe_push (vec, range);
10563 member_begin = perform_koenig_lookup (id_begin, vec,
10564 tf_warning_or_error);
10565 *begin = finish_call_expr (member_begin, &vec, false, true,
10566 tf_warning_or_error);
10567 member_end = perform_koenig_lookup (id_end, vec,
10568 tf_warning_or_error);
10569 *end = finish_call_expr (member_end, &vec, false, true,
10570 tf_warning_or_error);
10572 release_tree_vector (vec);
10575 /* Last common checks. */
10576 if (*begin == error_mark_node || *end == error_mark_node)
10578 /* If one of the expressions is an error do no more checks. */
10579 *begin = *end = error_mark_node;
10580 return error_mark_node;
10582 else if (type_dependent_expression_p (*begin)
10583 || type_dependent_expression_p (*end))
10584 /* Can happen, when, eg, in a template context, Koenig lookup
10585 can't resolve begin/end (c++/58503). */
10586 return NULL_TREE;
10587 else
10589 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
10590 /* The unqualified type of the __begin and __end temporaries should
10591 be the same, as required by the multiple auto declaration. */
10592 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
10593 error ("inconsistent begin/end types in range-based %<for%> "
10594 "statement: %qT and %qT",
10595 TREE_TYPE (*begin), TREE_TYPE (*end));
10596 return iter_type;
10601 /* Helper function for cp_parser_perform_range_for_lookup.
10602 Builds a tree for RANGE.IDENTIFIER(). */
10604 static tree
10605 cp_parser_range_for_member_function (tree range, tree identifier)
10607 tree member, res;
10608 vec<tree, va_gc> *vec;
10610 member = finish_class_member_access_expr (range, identifier,
10611 false, tf_warning_or_error);
10612 if (member == error_mark_node)
10613 return error_mark_node;
10615 vec = make_tree_vector ();
10616 res = finish_call_expr (member, &vec,
10617 /*disallow_virtual=*/false,
10618 /*koenig_p=*/false,
10619 tf_warning_or_error);
10620 release_tree_vector (vec);
10621 return res;
10624 /* Parse an iteration-statement.
10626 iteration-statement:
10627 while ( condition ) statement
10628 do statement while ( expression ) ;
10629 for ( for-init-statement condition [opt] ; expression [opt] )
10630 statement
10632 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
10634 static tree
10635 cp_parser_iteration_statement (cp_parser* parser, bool ivdep)
10637 cp_token *token;
10638 enum rid keyword;
10639 tree statement;
10640 unsigned char in_statement;
10642 /* Peek at the next token. */
10643 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
10644 if (!token)
10645 return error_mark_node;
10647 /* Remember whether or not we are already within an iteration
10648 statement. */
10649 in_statement = parser->in_statement;
10651 /* See what kind of keyword it is. */
10652 keyword = token->keyword;
10653 switch (keyword)
10655 case RID_WHILE:
10657 tree condition;
10659 /* Begin the while-statement. */
10660 statement = begin_while_stmt ();
10661 /* Look for the `('. */
10662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10663 /* Parse the condition. */
10664 condition = cp_parser_condition (parser);
10665 finish_while_stmt_cond (condition, statement, ivdep);
10666 /* Look for the `)'. */
10667 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10668 /* Parse the dependent statement. */
10669 parser->in_statement = IN_ITERATION_STMT;
10670 cp_parser_already_scoped_statement (parser);
10671 parser->in_statement = in_statement;
10672 /* We're done with the while-statement. */
10673 finish_while_stmt (statement);
10675 break;
10677 case RID_DO:
10679 tree expression;
10681 /* Begin the do-statement. */
10682 statement = begin_do_stmt ();
10683 /* Parse the body of the do-statement. */
10684 parser->in_statement = IN_ITERATION_STMT;
10685 cp_parser_implicitly_scoped_statement (parser, NULL);
10686 parser->in_statement = in_statement;
10687 finish_do_body (statement);
10688 /* Look for the `while' keyword. */
10689 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
10690 /* Look for the `('. */
10691 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10692 /* Parse the expression. */
10693 expression = cp_parser_expression (parser);
10694 /* We're done with the do-statement. */
10695 finish_do_stmt (expression, statement, ivdep);
10696 /* Look for the `)'. */
10697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10698 /* Look for the `;'. */
10699 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10701 break;
10703 case RID_FOR:
10705 /* Look for the `('. */
10706 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10708 statement = cp_parser_for (parser, ivdep);
10710 /* Look for the `)'. */
10711 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10713 /* Parse the body of the for-statement. */
10714 parser->in_statement = IN_ITERATION_STMT;
10715 cp_parser_already_scoped_statement (parser);
10716 parser->in_statement = in_statement;
10718 /* We're done with the for-statement. */
10719 finish_for_stmt (statement);
10721 break;
10723 default:
10724 cp_parser_error (parser, "expected iteration-statement");
10725 statement = error_mark_node;
10726 break;
10729 return statement;
10732 /* Parse a for-init-statement or the declarator of a range-based-for.
10733 Returns true if a range-based-for declaration is seen.
10735 for-init-statement:
10736 expression-statement
10737 simple-declaration */
10739 static bool
10740 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
10742 /* If the next token is a `;', then we have an empty
10743 expression-statement. Grammatically, this is also a
10744 simple-declaration, but an invalid one, because it does not
10745 declare anything. Therefore, if we did not handle this case
10746 specially, we would issue an error message about an invalid
10747 declaration. */
10748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10750 bool is_range_for = false;
10751 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
10754 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
10756 /* N3994 -- for (id : init) ... */
10757 if (cxx_dialect < cxx1z)
10758 pedwarn (input_location, 0, "range-based for loop without a "
10759 "type-specifier only available with "
10760 "-std=c++1z or -std=gnu++1z");
10761 tree name = cp_parser_identifier (parser);
10762 tree type = cp_build_reference_type (make_auto (), /*rval*/true);
10763 *decl = build_decl (input_location, VAR_DECL, name, type);
10764 pushdecl (*decl);
10765 cp_lexer_consume_token (parser->lexer);
10766 return true;
10769 /* A colon is used in range-based for. */
10770 parser->colon_corrects_to_scope_p = false;
10772 /* We're going to speculatively look for a declaration, falling back
10773 to an expression, if necessary. */
10774 cp_parser_parse_tentatively (parser);
10775 /* Parse the declaration. */
10776 cp_parser_simple_declaration (parser,
10777 /*function_definition_allowed_p=*/false,
10778 decl);
10779 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10780 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10782 /* It is a range-for, consume the ':' */
10783 cp_lexer_consume_token (parser->lexer);
10784 is_range_for = true;
10785 if (cxx_dialect < cxx11)
10787 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
10788 "range-based %<for%> loops only available with "
10789 "-std=c++11 or -std=gnu++11");
10790 *decl = error_mark_node;
10793 else
10794 /* The ';' is not consumed yet because we told
10795 cp_parser_simple_declaration not to. */
10796 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10798 if (cp_parser_parse_definitely (parser))
10799 return is_range_for;
10800 /* If the tentative parse failed, then we shall need to look for an
10801 expression-statement. */
10803 /* If we are here, it is an expression-statement. */
10804 cp_parser_expression_statement (parser, NULL_TREE);
10805 return false;
10808 /* Parse a jump-statement.
10810 jump-statement:
10811 break ;
10812 continue ;
10813 return expression [opt] ;
10814 return braced-init-list ;
10815 goto identifier ;
10817 GNU extension:
10819 jump-statement:
10820 goto * expression ;
10822 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
10824 static tree
10825 cp_parser_jump_statement (cp_parser* parser)
10827 tree statement = error_mark_node;
10828 cp_token *token;
10829 enum rid keyword;
10830 unsigned char in_statement;
10832 /* Peek at the next token. */
10833 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
10834 if (!token)
10835 return error_mark_node;
10837 /* See what kind of keyword it is. */
10838 keyword = token->keyword;
10839 switch (keyword)
10841 case RID_BREAK:
10842 in_statement = parser->in_statement & ~IN_IF_STMT;
10843 switch (in_statement)
10845 case 0:
10846 error_at (token->location, "break statement not within loop or switch");
10847 break;
10848 default:
10849 gcc_assert ((in_statement & IN_SWITCH_STMT)
10850 || in_statement == IN_ITERATION_STMT);
10851 statement = finish_break_stmt ();
10852 if (in_statement == IN_ITERATION_STMT)
10853 break_maybe_infinite_loop ();
10854 break;
10855 case IN_OMP_BLOCK:
10856 error_at (token->location, "invalid exit from OpenMP structured block");
10857 break;
10858 case IN_OMP_FOR:
10859 error_at (token->location, "break statement used with OpenMP for loop");
10860 break;
10861 case IN_CILK_SIMD_FOR:
10862 error_at (token->location, "break statement used with Cilk Plus for loop");
10863 break;
10865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10866 break;
10868 case RID_CONTINUE:
10869 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
10871 case 0:
10872 error_at (token->location, "continue statement not within a loop");
10873 break;
10874 case IN_CILK_SIMD_FOR:
10875 error_at (token->location,
10876 "continue statement within %<#pragma simd%> loop body");
10877 /* Fall through. */
10878 case IN_ITERATION_STMT:
10879 case IN_OMP_FOR:
10880 statement = finish_continue_stmt ();
10881 break;
10882 case IN_OMP_BLOCK:
10883 error_at (token->location, "invalid exit from OpenMP structured block");
10884 break;
10885 default:
10886 gcc_unreachable ();
10888 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10889 break;
10891 case RID_RETURN:
10893 tree expr;
10894 bool expr_non_constant_p;
10896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10898 cp_lexer_set_source_position (parser->lexer);
10899 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10900 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
10902 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10903 expr = cp_parser_expression (parser);
10904 else
10905 /* If the next token is a `;', then there is no
10906 expression. */
10907 expr = NULL_TREE;
10908 /* Build the return-statement. */
10909 statement = finish_return_stmt (expr);
10910 /* Look for the final `;'. */
10911 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10913 break;
10915 case RID_GOTO:
10916 /* Create the goto-statement. */
10917 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
10919 /* Issue a warning about this use of a GNU extension. */
10920 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
10921 /* Consume the '*' token. */
10922 cp_lexer_consume_token (parser->lexer);
10923 /* Parse the dependent expression. */
10924 finish_goto_stmt (cp_parser_expression (parser));
10926 else
10927 finish_goto_stmt (cp_parser_identifier (parser));
10928 /* Look for the final `;'. */
10929 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10930 break;
10932 default:
10933 cp_parser_error (parser, "expected jump-statement");
10934 break;
10937 return statement;
10940 /* Parse a declaration-statement.
10942 declaration-statement:
10943 block-declaration */
10945 static void
10946 cp_parser_declaration_statement (cp_parser* parser)
10948 void *p;
10950 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10951 p = obstack_alloc (&declarator_obstack, 0);
10953 /* Parse the block-declaration. */
10954 cp_parser_block_declaration (parser, /*statement_p=*/true);
10956 /* Free any declarators allocated. */
10957 obstack_free (&declarator_obstack, p);
10960 /* Some dependent statements (like `if (cond) statement'), are
10961 implicitly in their own scope. In other words, if the statement is
10962 a single statement (as opposed to a compound-statement), it is
10963 none-the-less treated as if it were enclosed in braces. Any
10964 declarations appearing in the dependent statement are out of scope
10965 after control passes that point. This function parses a statement,
10966 but ensures that is in its own scope, even if it is not a
10967 compound-statement.
10969 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10970 is a (possibly labeled) if statement which is not enclosed in
10971 braces and has an else clause. This is used to implement
10972 -Wparentheses.
10974 Returns the new statement. */
10976 static tree
10977 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
10979 tree statement;
10981 if (if_p != NULL)
10982 *if_p = false;
10984 /* Mark if () ; with a special NOP_EXPR. */
10985 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10987 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10988 cp_lexer_consume_token (parser->lexer);
10989 statement = add_stmt (build_empty_stmt (loc));
10991 /* if a compound is opened, we simply parse the statement directly. */
10992 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10993 statement = cp_parser_compound_statement (parser, NULL, false, false);
10994 /* If the token is not a `{', then we must take special action. */
10995 else
10997 /* Create a compound-statement. */
10998 statement = begin_compound_stmt (0);
10999 /* Parse the dependent-statement. */
11000 cp_parser_statement (parser, NULL_TREE, false, if_p);
11001 /* Finish the dummy compound-statement. */
11002 finish_compound_stmt (statement);
11005 /* Return the statement. */
11006 return statement;
11009 /* For some dependent statements (like `while (cond) statement'), we
11010 have already created a scope. Therefore, even if the dependent
11011 statement is a compound-statement, we do not want to create another
11012 scope. */
11014 static void
11015 cp_parser_already_scoped_statement (cp_parser* parser)
11017 /* If the token is a `{', then we must take special action. */
11018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11019 cp_parser_statement (parser, NULL_TREE, false, NULL);
11020 else
11022 /* Avoid calling cp_parser_compound_statement, so that we
11023 don't create a new scope. Do everything else by hand. */
11024 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11025 /* If the next keyword is `__label__' we have a label declaration. */
11026 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11027 cp_parser_label_declaration (parser);
11028 /* Parse an (optional) statement-seq. */
11029 cp_parser_statement_seq_opt (parser, NULL_TREE);
11030 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11034 /* Declarations [gram.dcl.dcl] */
11036 /* Parse an optional declaration-sequence.
11038 declaration-seq:
11039 declaration
11040 declaration-seq declaration */
11042 static void
11043 cp_parser_declaration_seq_opt (cp_parser* parser)
11045 while (true)
11047 cp_token *token;
11049 token = cp_lexer_peek_token (parser->lexer);
11051 if (token->type == CPP_CLOSE_BRACE
11052 || token->type == CPP_EOF
11053 || token->type == CPP_PRAGMA_EOL)
11054 break;
11056 if (token->type == CPP_SEMICOLON)
11058 /* A declaration consisting of a single semicolon is
11059 invalid. Allow it unless we're being pedantic. */
11060 cp_lexer_consume_token (parser->lexer);
11061 if (!in_system_header_at (input_location))
11062 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11063 continue;
11066 /* If we're entering or exiting a region that's implicitly
11067 extern "C", modify the lang context appropriately. */
11068 if (!parser->implicit_extern_c && token->implicit_extern_c)
11070 push_lang_context (lang_name_c);
11071 parser->implicit_extern_c = true;
11073 else if (parser->implicit_extern_c && !token->implicit_extern_c)
11075 pop_lang_context ();
11076 parser->implicit_extern_c = false;
11079 if (token->type == CPP_PRAGMA)
11081 /* A top-level declaration can consist solely of a #pragma.
11082 A nested declaration cannot, so this is done here and not
11083 in cp_parser_declaration. (A #pragma at block scope is
11084 handled in cp_parser_statement.) */
11085 cp_parser_pragma (parser, pragma_external);
11086 continue;
11089 /* Parse the declaration itself. */
11090 cp_parser_declaration (parser);
11094 /* Parse a declaration.
11096 declaration:
11097 block-declaration
11098 function-definition
11099 template-declaration
11100 explicit-instantiation
11101 explicit-specialization
11102 linkage-specification
11103 namespace-definition
11105 GNU extension:
11107 declaration:
11108 __extension__ declaration */
11110 static void
11111 cp_parser_declaration (cp_parser* parser)
11113 cp_token token1;
11114 cp_token token2;
11115 int saved_pedantic;
11116 void *p;
11117 tree attributes = NULL_TREE;
11119 /* Check for the `__extension__' keyword. */
11120 if (cp_parser_extension_opt (parser, &saved_pedantic))
11122 /* Parse the qualified declaration. */
11123 cp_parser_declaration (parser);
11124 /* Restore the PEDANTIC flag. */
11125 pedantic = saved_pedantic;
11127 return;
11130 /* Try to figure out what kind of declaration is present. */
11131 token1 = *cp_lexer_peek_token (parser->lexer);
11133 if (token1.type != CPP_EOF)
11134 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
11135 else
11137 token2.type = CPP_EOF;
11138 token2.keyword = RID_MAX;
11141 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11142 p = obstack_alloc (&declarator_obstack, 0);
11144 /* If the next token is `extern' and the following token is a string
11145 literal, then we have a linkage specification. */
11146 if (token1.keyword == RID_EXTERN
11147 && cp_parser_is_pure_string_literal (&token2))
11148 cp_parser_linkage_specification (parser);
11149 /* If the next token is `template', then we have either a template
11150 declaration, an explicit instantiation, or an explicit
11151 specialization. */
11152 else if (token1.keyword == RID_TEMPLATE)
11154 /* `template <>' indicates a template specialization. */
11155 if (token2.type == CPP_LESS
11156 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
11157 cp_parser_explicit_specialization (parser);
11158 /* `template <' indicates a template declaration. */
11159 else if (token2.type == CPP_LESS)
11160 cp_parser_template_declaration (parser, /*member_p=*/false);
11161 /* Anything else must be an explicit instantiation. */
11162 else
11163 cp_parser_explicit_instantiation (parser);
11165 /* If the next token is `export', then we have a template
11166 declaration. */
11167 else if (token1.keyword == RID_EXPORT)
11168 cp_parser_template_declaration (parser, /*member_p=*/false);
11169 /* If the next token is `extern', 'static' or 'inline' and the one
11170 after that is `template', we have a GNU extended explicit
11171 instantiation directive. */
11172 else if (cp_parser_allow_gnu_extensions_p (parser)
11173 && (token1.keyword == RID_EXTERN
11174 || token1.keyword == RID_STATIC
11175 || token1.keyword == RID_INLINE)
11176 && token2.keyword == RID_TEMPLATE)
11177 cp_parser_explicit_instantiation (parser);
11178 /* If the next token is `namespace', check for a named or unnamed
11179 namespace definition. */
11180 else if (token1.keyword == RID_NAMESPACE
11181 && (/* A named namespace definition. */
11182 (token2.type == CPP_NAME
11183 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
11184 != CPP_EQ))
11185 /* An unnamed namespace definition. */
11186 || token2.type == CPP_OPEN_BRACE
11187 || token2.keyword == RID_ATTRIBUTE))
11188 cp_parser_namespace_definition (parser);
11189 /* An inline (associated) namespace definition. */
11190 else if (token1.keyword == RID_INLINE
11191 && token2.keyword == RID_NAMESPACE)
11192 cp_parser_namespace_definition (parser);
11193 /* Objective-C++ declaration/definition. */
11194 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
11195 cp_parser_objc_declaration (parser, NULL_TREE);
11196 else if (c_dialect_objc ()
11197 && token1.keyword == RID_ATTRIBUTE
11198 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
11199 cp_parser_objc_declaration (parser, attributes);
11200 /* We must have either a block declaration or a function
11201 definition. */
11202 else
11203 /* Try to parse a block-declaration, or a function-definition. */
11204 cp_parser_block_declaration (parser, /*statement_p=*/false);
11206 /* Free any declarators allocated. */
11207 obstack_free (&declarator_obstack, p);
11210 /* Parse a block-declaration.
11212 block-declaration:
11213 simple-declaration
11214 asm-definition
11215 namespace-alias-definition
11216 using-declaration
11217 using-directive
11219 GNU Extension:
11221 block-declaration:
11222 __extension__ block-declaration
11224 C++0x Extension:
11226 block-declaration:
11227 static_assert-declaration
11229 If STATEMENT_P is TRUE, then this block-declaration is occurring as
11230 part of a declaration-statement. */
11232 static void
11233 cp_parser_block_declaration (cp_parser *parser,
11234 bool statement_p)
11236 cp_token *token1;
11237 int saved_pedantic;
11239 /* Check for the `__extension__' keyword. */
11240 if (cp_parser_extension_opt (parser, &saved_pedantic))
11242 /* Parse the qualified declaration. */
11243 cp_parser_block_declaration (parser, statement_p);
11244 /* Restore the PEDANTIC flag. */
11245 pedantic = saved_pedantic;
11247 return;
11250 /* Peek at the next token to figure out which kind of declaration is
11251 present. */
11252 token1 = cp_lexer_peek_token (parser->lexer);
11254 /* If the next keyword is `asm', we have an asm-definition. */
11255 if (token1->keyword == RID_ASM)
11257 if (statement_p)
11258 cp_parser_commit_to_tentative_parse (parser);
11259 cp_parser_asm_definition (parser);
11261 /* If the next keyword is `namespace', we have a
11262 namespace-alias-definition. */
11263 else if (token1->keyword == RID_NAMESPACE)
11264 cp_parser_namespace_alias_definition (parser);
11265 /* If the next keyword is `using', we have a
11266 using-declaration, a using-directive, or an alias-declaration. */
11267 else if (token1->keyword == RID_USING)
11269 cp_token *token2;
11271 if (statement_p)
11272 cp_parser_commit_to_tentative_parse (parser);
11273 /* If the token after `using' is `namespace', then we have a
11274 using-directive. */
11275 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11276 if (token2->keyword == RID_NAMESPACE)
11277 cp_parser_using_directive (parser);
11278 /* If the second token after 'using' is '=', then we have an
11279 alias-declaration. */
11280 else if (cxx_dialect >= cxx11
11281 && token2->type == CPP_NAME
11282 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
11283 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
11284 cp_parser_alias_declaration (parser);
11285 /* Otherwise, it's a using-declaration. */
11286 else
11287 cp_parser_using_declaration (parser,
11288 /*access_declaration_p=*/false);
11290 /* If the next keyword is `__label__' we have a misplaced label
11291 declaration. */
11292 else if (token1->keyword == RID_LABEL)
11294 cp_lexer_consume_token (parser->lexer);
11295 error_at (token1->location, "%<__label__%> not at the beginning of a block");
11296 cp_parser_skip_to_end_of_statement (parser);
11297 /* If the next token is now a `;', consume it. */
11298 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11299 cp_lexer_consume_token (parser->lexer);
11301 /* If the next token is `static_assert' we have a static assertion. */
11302 else if (token1->keyword == RID_STATIC_ASSERT)
11303 cp_parser_static_assert (parser, /*member_p=*/false);
11304 /* Anything else must be a simple-declaration. */
11305 else
11306 cp_parser_simple_declaration (parser, !statement_p,
11307 /*maybe_range_for_decl*/NULL);
11310 /* Parse a simple-declaration.
11312 simple-declaration:
11313 decl-specifier-seq [opt] init-declarator-list [opt] ;
11315 init-declarator-list:
11316 init-declarator
11317 init-declarator-list , init-declarator
11319 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
11320 function-definition as a simple-declaration.
11322 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
11323 parsed declaration if it is an uninitialized single declarator not followed
11324 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
11325 if present, will not be consumed. */
11327 static void
11328 cp_parser_simple_declaration (cp_parser* parser,
11329 bool function_definition_allowed_p,
11330 tree *maybe_range_for_decl)
11332 cp_decl_specifier_seq decl_specifiers;
11333 int declares_class_or_enum;
11334 bool saw_declarator;
11336 if (maybe_range_for_decl)
11337 *maybe_range_for_decl = NULL_TREE;
11339 /* Defer access checks until we know what is being declared; the
11340 checks for names appearing in the decl-specifier-seq should be
11341 done as if we were in the scope of the thing being declared. */
11342 push_deferring_access_checks (dk_deferred);
11344 /* Parse the decl-specifier-seq. We have to keep track of whether
11345 or not the decl-specifier-seq declares a named class or
11346 enumeration type, since that is the only case in which the
11347 init-declarator-list is allowed to be empty.
11349 [dcl.dcl]
11351 In a simple-declaration, the optional init-declarator-list can be
11352 omitted only when declaring a class or enumeration, that is when
11353 the decl-specifier-seq contains either a class-specifier, an
11354 elaborated-type-specifier, or an enum-specifier. */
11355 cp_parser_decl_specifier_seq (parser,
11356 CP_PARSER_FLAGS_OPTIONAL,
11357 &decl_specifiers,
11358 &declares_class_or_enum);
11359 /* We no longer need to defer access checks. */
11360 stop_deferring_access_checks ();
11362 /* In a block scope, a valid declaration must always have a
11363 decl-specifier-seq. By not trying to parse declarators, we can
11364 resolve the declaration/expression ambiguity more quickly. */
11365 if (!function_definition_allowed_p
11366 && !decl_specifiers.any_specifiers_p)
11368 cp_parser_error (parser, "expected declaration");
11369 goto done;
11372 /* If the next two tokens are both identifiers, the code is
11373 erroneous. The usual cause of this situation is code like:
11375 T t;
11377 where "T" should name a type -- but does not. */
11378 if (!decl_specifiers.any_type_specifiers_p
11379 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
11381 /* If parsing tentatively, we should commit; we really are
11382 looking at a declaration. */
11383 cp_parser_commit_to_tentative_parse (parser);
11384 /* Give up. */
11385 goto done;
11388 /* If we have seen at least one decl-specifier, and the next token
11389 is not a parenthesis, then we must be looking at a declaration.
11390 (After "int (" we might be looking at a functional cast.) */
11391 if (decl_specifiers.any_specifiers_p
11392 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
11393 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
11394 && !cp_parser_error_occurred (parser))
11395 cp_parser_commit_to_tentative_parse (parser);
11397 /* Keep going until we hit the `;' at the end of the simple
11398 declaration. */
11399 saw_declarator = false;
11400 while (cp_lexer_next_token_is_not (parser->lexer,
11401 CPP_SEMICOLON))
11403 cp_token *token;
11404 bool function_definition_p;
11405 tree decl;
11407 if (saw_declarator)
11409 /* If we are processing next declarator, coma is expected */
11410 token = cp_lexer_peek_token (parser->lexer);
11411 gcc_assert (token->type == CPP_COMMA);
11412 cp_lexer_consume_token (parser->lexer);
11413 if (maybe_range_for_decl)
11414 *maybe_range_for_decl = error_mark_node;
11416 else
11417 saw_declarator = true;
11419 /* Parse the init-declarator. */
11420 decl = cp_parser_init_declarator (parser, &decl_specifiers,
11421 /*checks=*/NULL,
11422 function_definition_allowed_p,
11423 /*member_p=*/false,
11424 declares_class_or_enum,
11425 &function_definition_p,
11426 maybe_range_for_decl);
11427 /* If an error occurred while parsing tentatively, exit quickly.
11428 (That usually happens when in the body of a function; each
11429 statement is treated as a declaration-statement until proven
11430 otherwise.) */
11431 if (cp_parser_error_occurred (parser))
11432 goto done;
11433 /* Handle function definitions specially. */
11434 if (function_definition_p)
11436 /* If the next token is a `,', then we are probably
11437 processing something like:
11439 void f() {}, *p;
11441 which is erroneous. */
11442 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11444 cp_token *token = cp_lexer_peek_token (parser->lexer);
11445 error_at (token->location,
11446 "mixing"
11447 " declarations and function-definitions is forbidden");
11449 /* Otherwise, we're done with the list of declarators. */
11450 else
11452 pop_deferring_access_checks ();
11453 return;
11456 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
11457 *maybe_range_for_decl = decl;
11458 /* The next token should be either a `,' or a `;'. */
11459 token = cp_lexer_peek_token (parser->lexer);
11460 /* If it's a `,', there are more declarators to come. */
11461 if (token->type == CPP_COMMA)
11462 /* will be consumed next time around */;
11463 /* If it's a `;', we are done. */
11464 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
11465 break;
11466 /* Anything else is an error. */
11467 else
11469 /* If we have already issued an error message we don't need
11470 to issue another one. */
11471 if (decl != error_mark_node
11472 || cp_parser_uncommitted_to_tentative_parse_p (parser))
11473 cp_parser_error (parser, "expected %<,%> or %<;%>");
11474 /* Skip tokens until we reach the end of the statement. */
11475 cp_parser_skip_to_end_of_statement (parser);
11476 /* If the next token is now a `;', consume it. */
11477 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11478 cp_lexer_consume_token (parser->lexer);
11479 goto done;
11481 /* After the first time around, a function-definition is not
11482 allowed -- even if it was OK at first. For example:
11484 int i, f() {}
11486 is not valid. */
11487 function_definition_allowed_p = false;
11490 /* Issue an error message if no declarators are present, and the
11491 decl-specifier-seq does not itself declare a class or
11492 enumeration: [dcl.dcl]/3. */
11493 if (!saw_declarator)
11495 if (cp_parser_declares_only_class_p (parser))
11497 if (!declares_class_or_enum
11498 && decl_specifiers.type
11499 && OVERLOAD_TYPE_P (decl_specifiers.type))
11500 /* Ensure an error is issued anyway when finish_decltype_type,
11501 called via cp_parser_decl_specifier_seq, returns a class or
11502 an enumeration (c++/51786). */
11503 decl_specifiers.type = NULL_TREE;
11504 shadow_tag (&decl_specifiers);
11506 /* Perform any deferred access checks. */
11507 perform_deferred_access_checks (tf_warning_or_error);
11510 /* Consume the `;'. */
11511 if (!maybe_range_for_decl)
11512 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11514 done:
11515 pop_deferring_access_checks ();
11518 /* Parse a decl-specifier-seq.
11520 decl-specifier-seq:
11521 decl-specifier-seq [opt] decl-specifier
11522 decl-specifier attribute-specifier-seq [opt] (C++11)
11524 decl-specifier:
11525 storage-class-specifier
11526 type-specifier
11527 function-specifier
11528 friend
11529 typedef
11531 GNU Extension:
11533 decl-specifier:
11534 attributes
11536 Set *DECL_SPECS to a representation of the decl-specifier-seq.
11538 The parser flags FLAGS is used to control type-specifier parsing.
11540 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
11541 flags:
11543 1: one of the decl-specifiers is an elaborated-type-specifier
11544 (i.e., a type declaration)
11545 2: one of the decl-specifiers is an enum-specifier or a
11546 class-specifier (i.e., a type definition)
11550 static void
11551 cp_parser_decl_specifier_seq (cp_parser* parser,
11552 cp_parser_flags flags,
11553 cp_decl_specifier_seq *decl_specs,
11554 int* declares_class_or_enum)
11556 bool constructor_possible_p = !parser->in_declarator_p;
11557 bool found_decl_spec = false;
11558 cp_token *start_token = NULL;
11559 cp_decl_spec ds;
11561 /* Clear DECL_SPECS. */
11562 clear_decl_specs (decl_specs);
11564 /* Assume no class or enumeration type is declared. */
11565 *declares_class_or_enum = 0;
11567 /* Keep reading specifiers until there are no more to read. */
11568 while (true)
11570 bool constructor_p;
11571 cp_token *token;
11572 ds = ds_last;
11574 /* Peek at the next token. */
11575 token = cp_lexer_peek_token (parser->lexer);
11577 /* Save the first token of the decl spec list for error
11578 reporting. */
11579 if (!start_token)
11580 start_token = token;
11581 /* Handle attributes. */
11582 if (cp_next_tokens_can_be_attribute_p (parser))
11584 /* Parse the attributes. */
11585 tree attrs = cp_parser_attributes_opt (parser);
11587 /* In a sequence of declaration specifiers, c++11 attributes
11588 appertain to the type that precede them. In that case
11589 [dcl.spec]/1 says:
11591 The attribute-specifier-seq affects the type only for
11592 the declaration it appears in, not other declarations
11593 involving the same type.
11595 But for now let's force the user to position the
11596 attribute either at the beginning of the declaration or
11597 after the declarator-id, which would clearly mean that it
11598 applies to the declarator. */
11599 if (cxx11_attribute_p (attrs))
11601 if (!found_decl_spec)
11602 /* The c++11 attribute is at the beginning of the
11603 declaration. It appertains to the entity being
11604 declared. */;
11605 else
11607 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
11609 /* This is an attribute following a
11610 class-specifier. */
11611 if (decl_specs->type_definition_p)
11612 warn_misplaced_attr_for_class_type (token->location,
11613 decl_specs->type);
11614 attrs = NULL_TREE;
11616 else
11618 decl_specs->std_attributes
11619 = chainon (decl_specs->std_attributes,
11620 attrs);
11621 if (decl_specs->locations[ds_std_attribute] == 0)
11622 decl_specs->locations[ds_std_attribute] = token->location;
11624 continue;
11628 decl_specs->attributes
11629 = chainon (decl_specs->attributes,
11630 attrs);
11631 if (decl_specs->locations[ds_attribute] == 0)
11632 decl_specs->locations[ds_attribute] = token->location;
11633 continue;
11635 /* Assume we will find a decl-specifier keyword. */
11636 found_decl_spec = true;
11637 /* If the next token is an appropriate keyword, we can simply
11638 add it to the list. */
11639 switch (token->keyword)
11641 /* decl-specifier:
11642 friend
11643 constexpr */
11644 case RID_FRIEND:
11645 if (!at_class_scope_p ())
11647 error_at (token->location, "%<friend%> used outside of class");
11648 cp_lexer_purge_token (parser->lexer);
11650 else
11652 ds = ds_friend;
11653 /* Consume the token. */
11654 cp_lexer_consume_token (parser->lexer);
11656 break;
11658 case RID_CONSTEXPR:
11659 ds = ds_constexpr;
11660 cp_lexer_consume_token (parser->lexer);
11661 break;
11663 /* function-specifier:
11664 inline
11665 virtual
11666 explicit */
11667 case RID_INLINE:
11668 case RID_VIRTUAL:
11669 case RID_EXPLICIT:
11670 cp_parser_function_specifier_opt (parser, decl_specs);
11671 break;
11673 /* decl-specifier:
11674 typedef */
11675 case RID_TYPEDEF:
11676 ds = ds_typedef;
11677 /* Consume the token. */
11678 cp_lexer_consume_token (parser->lexer);
11679 /* A constructor declarator cannot appear in a typedef. */
11680 constructor_possible_p = false;
11681 /* The "typedef" keyword can only occur in a declaration; we
11682 may as well commit at this point. */
11683 cp_parser_commit_to_tentative_parse (parser);
11685 if (decl_specs->storage_class != sc_none)
11686 decl_specs->conflicting_specifiers_p = true;
11687 break;
11689 /* storage-class-specifier:
11690 auto
11691 register
11692 static
11693 extern
11694 mutable
11696 GNU Extension:
11697 thread */
11698 case RID_AUTO:
11699 if (cxx_dialect == cxx98)
11701 /* Consume the token. */
11702 cp_lexer_consume_token (parser->lexer);
11704 /* Complain about `auto' as a storage specifier, if
11705 we're complaining about C++0x compatibility. */
11706 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
11707 " changes meaning in C++11; please remove it");
11709 /* Set the storage class anyway. */
11710 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
11711 token);
11713 else
11714 /* C++0x auto type-specifier. */
11715 found_decl_spec = false;
11716 break;
11718 case RID_REGISTER:
11719 case RID_STATIC:
11720 case RID_EXTERN:
11721 case RID_MUTABLE:
11722 /* Consume the token. */
11723 cp_lexer_consume_token (parser->lexer);
11724 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
11725 token);
11726 break;
11727 case RID_THREAD:
11728 /* Consume the token. */
11729 ds = ds_thread;
11730 cp_lexer_consume_token (parser->lexer);
11731 break;
11733 default:
11734 /* We did not yet find a decl-specifier yet. */
11735 found_decl_spec = false;
11736 break;
11739 if (found_decl_spec
11740 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
11741 && token->keyword != RID_CONSTEXPR)
11742 error ("decl-specifier invalid in condition");
11744 if (ds != ds_last)
11745 set_and_check_decl_spec_loc (decl_specs, ds, token);
11747 /* Constructors are a special case. The `S' in `S()' is not a
11748 decl-specifier; it is the beginning of the declarator. */
11749 constructor_p
11750 = (!found_decl_spec
11751 && constructor_possible_p
11752 && (cp_parser_constructor_declarator_p
11753 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
11755 /* If we don't have a DECL_SPEC yet, then we must be looking at
11756 a type-specifier. */
11757 if (!found_decl_spec && !constructor_p)
11759 int decl_spec_declares_class_or_enum;
11760 bool is_cv_qualifier;
11761 tree type_spec;
11763 type_spec
11764 = cp_parser_type_specifier (parser, flags,
11765 decl_specs,
11766 /*is_declaration=*/true,
11767 &decl_spec_declares_class_or_enum,
11768 &is_cv_qualifier);
11769 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
11771 /* If this type-specifier referenced a user-defined type
11772 (a typedef, class-name, etc.), then we can't allow any
11773 more such type-specifiers henceforth.
11775 [dcl.spec]
11777 The longest sequence of decl-specifiers that could
11778 possibly be a type name is taken as the
11779 decl-specifier-seq of a declaration. The sequence shall
11780 be self-consistent as described below.
11782 [dcl.type]
11784 As a general rule, at most one type-specifier is allowed
11785 in the complete decl-specifier-seq of a declaration. The
11786 only exceptions are the following:
11788 -- const or volatile can be combined with any other
11789 type-specifier.
11791 -- signed or unsigned can be combined with char, long,
11792 short, or int.
11794 -- ..
11796 Example:
11798 typedef char* Pc;
11799 void g (const int Pc);
11801 Here, Pc is *not* part of the decl-specifier seq; it's
11802 the declarator. Therefore, once we see a type-specifier
11803 (other than a cv-qualifier), we forbid any additional
11804 user-defined types. We *do* still allow things like `int
11805 int' to be considered a decl-specifier-seq, and issue the
11806 error message later. */
11807 if (type_spec && !is_cv_qualifier)
11808 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
11809 /* A constructor declarator cannot follow a type-specifier. */
11810 if (type_spec)
11812 constructor_possible_p = false;
11813 found_decl_spec = true;
11814 if (!is_cv_qualifier)
11815 decl_specs->any_type_specifiers_p = true;
11819 /* If we still do not have a DECL_SPEC, then there are no more
11820 decl-specifiers. */
11821 if (!found_decl_spec)
11822 break;
11824 decl_specs->any_specifiers_p = true;
11825 /* After we see one decl-specifier, further decl-specifiers are
11826 always optional. */
11827 flags |= CP_PARSER_FLAGS_OPTIONAL;
11830 /* Don't allow a friend specifier with a class definition. */
11831 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
11832 && (*declares_class_or_enum & 2))
11833 error_at (decl_specs->locations[ds_friend],
11834 "class definition may not be declared a friend");
11837 /* Parse an (optional) storage-class-specifier.
11839 storage-class-specifier:
11840 auto
11841 register
11842 static
11843 extern
11844 mutable
11846 GNU Extension:
11848 storage-class-specifier:
11849 thread
11851 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
11853 static tree
11854 cp_parser_storage_class_specifier_opt (cp_parser* parser)
11856 switch (cp_lexer_peek_token (parser->lexer)->keyword)
11858 case RID_AUTO:
11859 if (cxx_dialect != cxx98)
11860 return NULL_TREE;
11861 /* Fall through for C++98. */
11863 case RID_REGISTER:
11864 case RID_STATIC:
11865 case RID_EXTERN:
11866 case RID_MUTABLE:
11867 case RID_THREAD:
11868 /* Consume the token. */
11869 return cp_lexer_consume_token (parser->lexer)->u.value;
11871 default:
11872 return NULL_TREE;
11876 /* Parse an (optional) function-specifier.
11878 function-specifier:
11879 inline
11880 virtual
11881 explicit
11883 Returns an IDENTIFIER_NODE corresponding to the keyword used.
11884 Updates DECL_SPECS, if it is non-NULL. */
11886 static tree
11887 cp_parser_function_specifier_opt (cp_parser* parser,
11888 cp_decl_specifier_seq *decl_specs)
11890 cp_token *token = cp_lexer_peek_token (parser->lexer);
11891 switch (token->keyword)
11893 case RID_INLINE:
11894 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
11895 break;
11897 case RID_VIRTUAL:
11898 /* 14.5.2.3 [temp.mem]
11900 A member function template shall not be virtual. */
11901 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
11902 error_at (token->location, "templates may not be %<virtual%>");
11903 else
11904 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
11905 break;
11907 case RID_EXPLICIT:
11908 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
11909 break;
11911 default:
11912 return NULL_TREE;
11915 /* Consume the token. */
11916 return cp_lexer_consume_token (parser->lexer)->u.value;
11919 /* Parse a linkage-specification.
11921 linkage-specification:
11922 extern string-literal { declaration-seq [opt] }
11923 extern string-literal declaration */
11925 static void
11926 cp_parser_linkage_specification (cp_parser* parser)
11928 tree linkage;
11930 /* Look for the `extern' keyword. */
11931 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
11933 /* Look for the string-literal. */
11934 linkage = cp_parser_string_literal (parser, false, false);
11936 /* Transform the literal into an identifier. If the literal is a
11937 wide-character string, or contains embedded NULs, then we can't
11938 handle it as the user wants. */
11939 if (strlen (TREE_STRING_POINTER (linkage))
11940 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
11942 cp_parser_error (parser, "invalid linkage-specification");
11943 /* Assume C++ linkage. */
11944 linkage = lang_name_cplusplus;
11946 else
11947 linkage = get_identifier (TREE_STRING_POINTER (linkage));
11949 /* We're now using the new linkage. */
11950 push_lang_context (linkage);
11952 /* If the next token is a `{', then we're using the first
11953 production. */
11954 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11956 cp_ensure_no_omp_declare_simd (parser);
11958 /* Consume the `{' token. */
11959 cp_lexer_consume_token (parser->lexer);
11960 /* Parse the declarations. */
11961 cp_parser_declaration_seq_opt (parser);
11962 /* Look for the closing `}'. */
11963 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11965 /* Otherwise, there's just one declaration. */
11966 else
11968 bool saved_in_unbraced_linkage_specification_p;
11970 saved_in_unbraced_linkage_specification_p
11971 = parser->in_unbraced_linkage_specification_p;
11972 parser->in_unbraced_linkage_specification_p = true;
11973 cp_parser_declaration (parser);
11974 parser->in_unbraced_linkage_specification_p
11975 = saved_in_unbraced_linkage_specification_p;
11978 /* We're done with the linkage-specification. */
11979 pop_lang_context ();
11982 /* Parse a static_assert-declaration.
11984 static_assert-declaration:
11985 static_assert ( constant-expression , string-literal ) ;
11987 If MEMBER_P, this static_assert is a class member. */
11989 static void
11990 cp_parser_static_assert(cp_parser *parser, bool member_p)
11992 tree condition;
11993 tree message;
11994 cp_token *token;
11995 location_t saved_loc;
11996 bool dummy;
11998 /* Peek at the `static_assert' token so we can keep track of exactly
11999 where the static assertion started. */
12000 token = cp_lexer_peek_token (parser->lexer);
12001 saved_loc = token->location;
12003 /* Look for the `static_assert' keyword. */
12004 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12005 RT_STATIC_ASSERT))
12006 return;
12008 /* We know we are in a static assertion; commit to any tentative
12009 parse. */
12010 if (cp_parser_parsing_tentatively (parser))
12011 cp_parser_commit_to_tentative_parse (parser);
12013 /* Parse the `(' starting the static assertion condition. */
12014 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12016 /* Parse the constant-expression. Allow a non-constant expression
12017 here in order to give better diagnostics in finish_static_assert. */
12018 condition =
12019 cp_parser_constant_expression (parser,
12020 /*allow_non_constant_p=*/true,
12021 /*non_constant_p=*/&dummy);
12023 /* Parse the separating `,'. */
12024 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
12026 /* Parse the string-literal message. */
12027 message = cp_parser_string_literal (parser,
12028 /*translate=*/false,
12029 /*wide_ok=*/true);
12031 /* A `)' completes the static assertion. */
12032 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12033 cp_parser_skip_to_closing_parenthesis (parser,
12034 /*recovering=*/true,
12035 /*or_comma=*/false,
12036 /*consume_paren=*/true);
12038 /* A semicolon terminates the declaration. */
12039 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12041 /* Complete the static assertion, which may mean either processing
12042 the static assert now or saving it for template instantiation. */
12043 finish_static_assert (condition, message, saved_loc, member_p);
12046 /* Parse the expression in decltype ( expression ). */
12048 static tree
12049 cp_parser_decltype_expr (cp_parser *parser,
12050 bool &id_expression_or_member_access_p)
12052 cp_token *id_expr_start_token;
12053 tree expr;
12055 /* First, try parsing an id-expression. */
12056 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
12057 cp_parser_parse_tentatively (parser);
12058 expr = cp_parser_id_expression (parser,
12059 /*template_keyword_p=*/false,
12060 /*check_dependency_p=*/true,
12061 /*template_p=*/NULL,
12062 /*declarator_p=*/false,
12063 /*optional_p=*/false);
12065 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
12067 bool non_integral_constant_expression_p = false;
12068 tree id_expression = expr;
12069 cp_id_kind idk;
12070 const char *error_msg;
12072 if (identifier_p (expr))
12073 /* Lookup the name we got back from the id-expression. */
12074 expr = cp_parser_lookup_name_simple (parser, expr,
12075 id_expr_start_token->location);
12077 if (expr
12078 && expr != error_mark_node
12079 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
12080 && TREE_CODE (expr) != TYPE_DECL
12081 && (TREE_CODE (expr) != BIT_NOT_EXPR
12082 || !TYPE_P (TREE_OPERAND (expr, 0)))
12083 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12085 /* Complete lookup of the id-expression. */
12086 expr = (finish_id_expression
12087 (id_expression, expr, parser->scope, &idk,
12088 /*integral_constant_expression_p=*/false,
12089 /*allow_non_integral_constant_expression_p=*/true,
12090 &non_integral_constant_expression_p,
12091 /*template_p=*/false,
12092 /*done=*/true,
12093 /*address_p=*/false,
12094 /*template_arg_p=*/false,
12095 &error_msg,
12096 id_expr_start_token->location));
12098 if (expr == error_mark_node)
12099 /* We found an id-expression, but it was something that we
12100 should not have found. This is an error, not something
12101 we can recover from, so note that we found an
12102 id-expression and we'll recover as gracefully as
12103 possible. */
12104 id_expression_or_member_access_p = true;
12107 if (expr
12108 && expr != error_mark_node
12109 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12110 /* We have an id-expression. */
12111 id_expression_or_member_access_p = true;
12114 if (!id_expression_or_member_access_p)
12116 /* Abort the id-expression parse. */
12117 cp_parser_abort_tentative_parse (parser);
12119 /* Parsing tentatively, again. */
12120 cp_parser_parse_tentatively (parser);
12122 /* Parse a class member access. */
12123 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
12124 /*cast_p=*/false, /*decltype*/true,
12125 /*member_access_only_p=*/true, NULL);
12127 if (expr
12128 && expr != error_mark_node
12129 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
12130 /* We have an id-expression. */
12131 id_expression_or_member_access_p = true;
12134 if (id_expression_or_member_access_p)
12135 /* We have parsed the complete id-expression or member access. */
12136 cp_parser_parse_definitely (parser);
12137 else
12139 /* Abort our attempt to parse an id-expression or member access
12140 expression. */
12141 cp_parser_abort_tentative_parse (parser);
12143 /* Parse a full expression. */
12144 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
12145 /*decltype_p=*/true);
12148 return expr;
12151 /* Parse a `decltype' type. Returns the type.
12153 simple-type-specifier:
12154 decltype ( expression )
12155 C++14 proposal:
12156 decltype ( auto ) */
12158 static tree
12159 cp_parser_decltype (cp_parser *parser)
12161 tree expr;
12162 bool id_expression_or_member_access_p = false;
12163 const char *saved_message;
12164 bool saved_integral_constant_expression_p;
12165 bool saved_non_integral_constant_expression_p;
12166 bool saved_greater_than_is_operator_p;
12167 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12169 if (start_token->type == CPP_DECLTYPE)
12171 /* Already parsed. */
12172 cp_lexer_consume_token (parser->lexer);
12173 return start_token->u.value;
12176 /* Look for the `decltype' token. */
12177 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
12178 return error_mark_node;
12180 /* Parse the opening `('. */
12181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
12182 return error_mark_node;
12184 /* decltype (auto) */
12185 if (cxx_dialect >= cxx14
12186 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
12188 cp_lexer_consume_token (parser->lexer);
12189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12190 return error_mark_node;
12191 expr = make_decltype_auto ();
12192 AUTO_IS_DECLTYPE (expr) = true;
12193 goto rewrite;
12196 /* Types cannot be defined in a `decltype' expression. Save away the
12197 old message. */
12198 saved_message = parser->type_definition_forbidden_message;
12200 /* And create the new one. */
12201 parser->type_definition_forbidden_message
12202 = G_("types may not be defined in %<decltype%> expressions");
12204 /* The restrictions on constant-expressions do not apply inside
12205 decltype expressions. */
12206 saved_integral_constant_expression_p
12207 = parser->integral_constant_expression_p;
12208 saved_non_integral_constant_expression_p
12209 = parser->non_integral_constant_expression_p;
12210 parser->integral_constant_expression_p = false;
12212 /* Within a parenthesized expression, a `>' token is always
12213 the greater-than operator. */
12214 saved_greater_than_is_operator_p
12215 = parser->greater_than_is_operator_p;
12216 parser->greater_than_is_operator_p = true;
12218 /* Do not actually evaluate the expression. */
12219 ++cp_unevaluated_operand;
12221 /* Do not warn about problems with the expression. */
12222 ++c_inhibit_evaluation_warnings;
12224 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
12226 /* Go back to evaluating expressions. */
12227 --cp_unevaluated_operand;
12228 --c_inhibit_evaluation_warnings;
12230 /* The `>' token might be the end of a template-id or
12231 template-parameter-list now. */
12232 parser->greater_than_is_operator_p
12233 = saved_greater_than_is_operator_p;
12235 /* Restore the old message and the integral constant expression
12236 flags. */
12237 parser->type_definition_forbidden_message = saved_message;
12238 parser->integral_constant_expression_p
12239 = saved_integral_constant_expression_p;
12240 parser->non_integral_constant_expression_p
12241 = saved_non_integral_constant_expression_p;
12243 /* Parse to the closing `)'. */
12244 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
12246 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12247 /*consume_paren=*/true);
12248 return error_mark_node;
12251 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
12252 tf_warning_or_error);
12254 rewrite:
12255 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
12256 it again. */
12257 start_token->type = CPP_DECLTYPE;
12258 start_token->u.value = expr;
12259 start_token->keyword = RID_MAX;
12260 cp_lexer_purge_tokens_after (parser->lexer, start_token);
12262 return expr;
12265 /* Special member functions [gram.special] */
12267 /* Parse a conversion-function-id.
12269 conversion-function-id:
12270 operator conversion-type-id
12272 Returns an IDENTIFIER_NODE representing the operator. */
12274 static tree
12275 cp_parser_conversion_function_id (cp_parser* parser)
12277 tree type;
12278 tree saved_scope;
12279 tree saved_qualifying_scope;
12280 tree saved_object_scope;
12281 tree pushed_scope = NULL_TREE;
12283 /* Look for the `operator' token. */
12284 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12285 return error_mark_node;
12286 /* When we parse the conversion-type-id, the current scope will be
12287 reset. However, we need that information in able to look up the
12288 conversion function later, so we save it here. */
12289 saved_scope = parser->scope;
12290 saved_qualifying_scope = parser->qualifying_scope;
12291 saved_object_scope = parser->object_scope;
12292 /* We must enter the scope of the class so that the names of
12293 entities declared within the class are available in the
12294 conversion-type-id. For example, consider:
12296 struct S {
12297 typedef int I;
12298 operator I();
12301 S::operator I() { ... }
12303 In order to see that `I' is a type-name in the definition, we
12304 must be in the scope of `S'. */
12305 if (saved_scope)
12306 pushed_scope = push_scope (saved_scope);
12307 /* Parse the conversion-type-id. */
12308 type = cp_parser_conversion_type_id (parser);
12309 /* Leave the scope of the class, if any. */
12310 if (pushed_scope)
12311 pop_scope (pushed_scope);
12312 /* Restore the saved scope. */
12313 parser->scope = saved_scope;
12314 parser->qualifying_scope = saved_qualifying_scope;
12315 parser->object_scope = saved_object_scope;
12316 /* If the TYPE is invalid, indicate failure. */
12317 if (type == error_mark_node)
12318 return error_mark_node;
12319 return mangle_conv_op_name_for_type (type);
12322 /* Parse a conversion-type-id:
12324 conversion-type-id:
12325 type-specifier-seq conversion-declarator [opt]
12327 Returns the TYPE specified. */
12329 static tree
12330 cp_parser_conversion_type_id (cp_parser* parser)
12332 tree attributes;
12333 cp_decl_specifier_seq type_specifiers;
12334 cp_declarator *declarator;
12335 tree type_specified;
12336 const char *saved_message;
12338 /* Parse the attributes. */
12339 attributes = cp_parser_attributes_opt (parser);
12341 saved_message = parser->type_definition_forbidden_message;
12342 parser->type_definition_forbidden_message
12343 = G_("types may not be defined in a conversion-type-id");
12345 /* Parse the type-specifiers. */
12346 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12347 /*is_trailing_return=*/false,
12348 &type_specifiers);
12350 parser->type_definition_forbidden_message = saved_message;
12352 /* If that didn't work, stop. */
12353 if (type_specifiers.type == error_mark_node)
12354 return error_mark_node;
12355 /* Parse the conversion-declarator. */
12356 declarator = cp_parser_conversion_declarator_opt (parser);
12358 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
12359 /*initialized=*/0, &attributes);
12360 if (attributes)
12361 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
12363 /* Don't give this error when parsing tentatively. This happens to
12364 work because we always parse this definitively once. */
12365 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
12366 && type_uses_auto (type_specified))
12368 if (cxx_dialect < cxx14)
12370 error ("invalid use of %<auto%> in conversion operator");
12371 return error_mark_node;
12373 else if (template_parm_scope_p ())
12374 warning (0, "use of %<auto%> in member template "
12375 "conversion operator can never be deduced");
12378 return type_specified;
12381 /* Parse an (optional) conversion-declarator.
12383 conversion-declarator:
12384 ptr-operator conversion-declarator [opt]
12388 static cp_declarator *
12389 cp_parser_conversion_declarator_opt (cp_parser* parser)
12391 enum tree_code code;
12392 tree class_type, std_attributes = NULL_TREE;
12393 cp_cv_quals cv_quals;
12395 /* We don't know if there's a ptr-operator next, or not. */
12396 cp_parser_parse_tentatively (parser);
12397 /* Try the ptr-operator. */
12398 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
12399 &std_attributes);
12400 /* If it worked, look for more conversion-declarators. */
12401 if (cp_parser_parse_definitely (parser))
12403 cp_declarator *declarator;
12405 /* Parse another optional declarator. */
12406 declarator = cp_parser_conversion_declarator_opt (parser);
12408 declarator = cp_parser_make_indirect_declarator
12409 (code, class_type, cv_quals, declarator, std_attributes);
12411 return declarator;
12414 return NULL;
12417 /* Parse an (optional) ctor-initializer.
12419 ctor-initializer:
12420 : mem-initializer-list
12422 Returns TRUE iff the ctor-initializer was actually present. */
12424 static bool
12425 cp_parser_ctor_initializer_opt (cp_parser* parser)
12427 /* If the next token is not a `:', then there is no
12428 ctor-initializer. */
12429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
12431 /* Do default initialization of any bases and members. */
12432 if (DECL_CONSTRUCTOR_P (current_function_decl))
12433 finish_mem_initializers (NULL_TREE);
12435 return false;
12438 /* Consume the `:' token. */
12439 cp_lexer_consume_token (parser->lexer);
12440 /* And the mem-initializer-list. */
12441 cp_parser_mem_initializer_list (parser);
12443 return true;
12446 /* Parse a mem-initializer-list.
12448 mem-initializer-list:
12449 mem-initializer ... [opt]
12450 mem-initializer ... [opt] , mem-initializer-list */
12452 static void
12453 cp_parser_mem_initializer_list (cp_parser* parser)
12455 tree mem_initializer_list = NULL_TREE;
12456 tree target_ctor = error_mark_node;
12457 cp_token *token = cp_lexer_peek_token (parser->lexer);
12459 /* Let the semantic analysis code know that we are starting the
12460 mem-initializer-list. */
12461 if (!DECL_CONSTRUCTOR_P (current_function_decl))
12462 error_at (token->location,
12463 "only constructors take member initializers");
12465 /* Loop through the list. */
12466 while (true)
12468 tree mem_initializer;
12470 token = cp_lexer_peek_token (parser->lexer);
12471 /* Parse the mem-initializer. */
12472 mem_initializer = cp_parser_mem_initializer (parser);
12473 /* If the next token is a `...', we're expanding member initializers. */
12474 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12476 /* Consume the `...'. */
12477 cp_lexer_consume_token (parser->lexer);
12479 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
12480 can be expanded but members cannot. */
12481 if (mem_initializer != error_mark_node
12482 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
12484 error_at (token->location,
12485 "cannot expand initializer for member %<%D%>",
12486 TREE_PURPOSE (mem_initializer));
12487 mem_initializer = error_mark_node;
12490 /* Construct the pack expansion type. */
12491 if (mem_initializer != error_mark_node)
12492 mem_initializer = make_pack_expansion (mem_initializer);
12494 if (target_ctor != error_mark_node
12495 && mem_initializer != error_mark_node)
12497 error ("mem-initializer for %qD follows constructor delegation",
12498 TREE_PURPOSE (mem_initializer));
12499 mem_initializer = error_mark_node;
12501 /* Look for a target constructor. */
12502 if (mem_initializer != error_mark_node
12503 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
12504 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
12506 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
12507 if (mem_initializer_list)
12509 error ("constructor delegation follows mem-initializer for %qD",
12510 TREE_PURPOSE (mem_initializer_list));
12511 mem_initializer = error_mark_node;
12513 target_ctor = mem_initializer;
12515 /* Add it to the list, unless it was erroneous. */
12516 if (mem_initializer != error_mark_node)
12518 TREE_CHAIN (mem_initializer) = mem_initializer_list;
12519 mem_initializer_list = mem_initializer;
12521 /* If the next token is not a `,', we're done. */
12522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12523 break;
12524 /* Consume the `,' token. */
12525 cp_lexer_consume_token (parser->lexer);
12528 /* Perform semantic analysis. */
12529 if (DECL_CONSTRUCTOR_P (current_function_decl))
12530 finish_mem_initializers (mem_initializer_list);
12533 /* Parse a mem-initializer.
12535 mem-initializer:
12536 mem-initializer-id ( expression-list [opt] )
12537 mem-initializer-id braced-init-list
12539 GNU extension:
12541 mem-initializer:
12542 ( expression-list [opt] )
12544 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
12545 class) or FIELD_DECL (for a non-static data member) to initialize;
12546 the TREE_VALUE is the expression-list. An empty initialization
12547 list is represented by void_list_node. */
12549 static tree
12550 cp_parser_mem_initializer (cp_parser* parser)
12552 tree mem_initializer_id;
12553 tree expression_list;
12554 tree member;
12555 cp_token *token = cp_lexer_peek_token (parser->lexer);
12557 /* Find out what is being initialized. */
12558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
12560 permerror (token->location,
12561 "anachronistic old-style base class initializer");
12562 mem_initializer_id = NULL_TREE;
12564 else
12566 mem_initializer_id = cp_parser_mem_initializer_id (parser);
12567 if (mem_initializer_id == error_mark_node)
12568 return mem_initializer_id;
12570 member = expand_member_init (mem_initializer_id);
12571 if (member && !DECL_P (member))
12572 in_base_initializer = 1;
12574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12576 bool expr_non_constant_p;
12577 cp_lexer_set_source_position (parser->lexer);
12578 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12579 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
12580 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
12581 expression_list = build_tree_list (NULL_TREE, expression_list);
12583 else
12585 vec<tree, va_gc> *vec;
12586 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
12587 /*cast_p=*/false,
12588 /*allow_expansion_p=*/true,
12589 /*non_constant_p=*/NULL);
12590 if (vec == NULL)
12591 return error_mark_node;
12592 expression_list = build_tree_list_vec (vec);
12593 release_tree_vector (vec);
12596 if (expression_list == error_mark_node)
12597 return error_mark_node;
12598 if (!expression_list)
12599 expression_list = void_type_node;
12601 in_base_initializer = 0;
12603 return member ? build_tree_list (member, expression_list) : error_mark_node;
12606 /* Parse a mem-initializer-id.
12608 mem-initializer-id:
12609 :: [opt] nested-name-specifier [opt] class-name
12610 identifier
12612 Returns a TYPE indicating the class to be initializer for the first
12613 production. Returns an IDENTIFIER_NODE indicating the data member
12614 to be initialized for the second production. */
12616 static tree
12617 cp_parser_mem_initializer_id (cp_parser* parser)
12619 bool global_scope_p;
12620 bool nested_name_specifier_p;
12621 bool template_p = false;
12622 tree id;
12624 cp_token *token = cp_lexer_peek_token (parser->lexer);
12626 /* `typename' is not allowed in this context ([temp.res]). */
12627 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12629 error_at (token->location,
12630 "keyword %<typename%> not allowed in this context (a qualified "
12631 "member initializer is implicitly a type)");
12632 cp_lexer_consume_token (parser->lexer);
12634 /* Look for the optional `::' operator. */
12635 global_scope_p
12636 = (cp_parser_global_scope_opt (parser,
12637 /*current_scope_valid_p=*/false)
12638 != NULL_TREE);
12639 /* Look for the optional nested-name-specifier. The simplest way to
12640 implement:
12642 [temp.res]
12644 The keyword `typename' is not permitted in a base-specifier or
12645 mem-initializer; in these contexts a qualified name that
12646 depends on a template-parameter is implicitly assumed to be a
12647 type name.
12649 is to assume that we have seen the `typename' keyword at this
12650 point. */
12651 nested_name_specifier_p
12652 = (cp_parser_nested_name_specifier_opt (parser,
12653 /*typename_keyword_p=*/true,
12654 /*check_dependency_p=*/true,
12655 /*type_p=*/true,
12656 /*is_declaration=*/true)
12657 != NULL_TREE);
12658 if (nested_name_specifier_p)
12659 template_p = cp_parser_optional_template_keyword (parser);
12660 /* If there is a `::' operator or a nested-name-specifier, then we
12661 are definitely looking for a class-name. */
12662 if (global_scope_p || nested_name_specifier_p)
12663 return cp_parser_class_name (parser,
12664 /*typename_keyword_p=*/true,
12665 /*template_keyword_p=*/template_p,
12666 typename_type,
12667 /*check_dependency_p=*/true,
12668 /*class_head_p=*/false,
12669 /*is_declaration=*/true);
12670 /* Otherwise, we could also be looking for an ordinary identifier. */
12671 cp_parser_parse_tentatively (parser);
12672 /* Try a class-name. */
12673 id = cp_parser_class_name (parser,
12674 /*typename_keyword_p=*/true,
12675 /*template_keyword_p=*/false,
12676 none_type,
12677 /*check_dependency_p=*/true,
12678 /*class_head_p=*/false,
12679 /*is_declaration=*/true);
12680 /* If we found one, we're done. */
12681 if (cp_parser_parse_definitely (parser))
12682 return id;
12683 /* Otherwise, look for an ordinary identifier. */
12684 return cp_parser_identifier (parser);
12687 /* Overloading [gram.over] */
12689 /* Parse an operator-function-id.
12691 operator-function-id:
12692 operator operator
12694 Returns an IDENTIFIER_NODE for the operator which is a
12695 human-readable spelling of the identifier, e.g., `operator +'. */
12697 static tree
12698 cp_parser_operator_function_id (cp_parser* parser)
12700 /* Look for the `operator' keyword. */
12701 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
12702 return error_mark_node;
12703 /* And then the name of the operator itself. */
12704 return cp_parser_operator (parser);
12707 /* Return an identifier node for a user-defined literal operator.
12708 The suffix identifier is chained to the operator name identifier. */
12710 static tree
12711 cp_literal_operator_id (const char* name)
12713 tree identifier;
12714 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
12715 + strlen (name) + 10);
12716 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
12717 identifier = get_identifier (buffer);
12719 return identifier;
12722 /* Parse an operator.
12724 operator:
12725 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
12726 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
12727 || ++ -- , ->* -> () []
12729 GNU Extensions:
12731 operator:
12732 <? >? <?= >?=
12734 Returns an IDENTIFIER_NODE for the operator which is a
12735 human-readable spelling of the identifier, e.g., `operator +'. */
12737 static tree
12738 cp_parser_operator (cp_parser* parser)
12740 tree id = NULL_TREE;
12741 cp_token *token;
12742 bool utf8 = false;
12744 /* Peek at the next token. */
12745 token = cp_lexer_peek_token (parser->lexer);
12746 /* Figure out which operator we have. */
12747 switch (token->type)
12749 case CPP_KEYWORD:
12751 enum tree_code op;
12753 /* The keyword should be either `new' or `delete'. */
12754 if (token->keyword == RID_NEW)
12755 op = NEW_EXPR;
12756 else if (token->keyword == RID_DELETE)
12757 op = DELETE_EXPR;
12758 else
12759 break;
12761 /* Consume the `new' or `delete' token. */
12762 cp_lexer_consume_token (parser->lexer);
12764 /* Peek at the next token. */
12765 token = cp_lexer_peek_token (parser->lexer);
12766 /* If it's a `[' token then this is the array variant of the
12767 operator. */
12768 if (token->type == CPP_OPEN_SQUARE)
12770 /* Consume the `[' token. */
12771 cp_lexer_consume_token (parser->lexer);
12772 /* Look for the `]' token. */
12773 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12774 id = ansi_opname (op == NEW_EXPR
12775 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
12777 /* Otherwise, we have the non-array variant. */
12778 else
12779 id = ansi_opname (op);
12781 return id;
12784 case CPP_PLUS:
12785 id = ansi_opname (PLUS_EXPR);
12786 break;
12788 case CPP_MINUS:
12789 id = ansi_opname (MINUS_EXPR);
12790 break;
12792 case CPP_MULT:
12793 id = ansi_opname (MULT_EXPR);
12794 break;
12796 case CPP_DIV:
12797 id = ansi_opname (TRUNC_DIV_EXPR);
12798 break;
12800 case CPP_MOD:
12801 id = ansi_opname (TRUNC_MOD_EXPR);
12802 break;
12804 case CPP_XOR:
12805 id = ansi_opname (BIT_XOR_EXPR);
12806 break;
12808 case CPP_AND:
12809 id = ansi_opname (BIT_AND_EXPR);
12810 break;
12812 case CPP_OR:
12813 id = ansi_opname (BIT_IOR_EXPR);
12814 break;
12816 case CPP_COMPL:
12817 id = ansi_opname (BIT_NOT_EXPR);
12818 break;
12820 case CPP_NOT:
12821 id = ansi_opname (TRUTH_NOT_EXPR);
12822 break;
12824 case CPP_EQ:
12825 id = ansi_assopname (NOP_EXPR);
12826 break;
12828 case CPP_LESS:
12829 id = ansi_opname (LT_EXPR);
12830 break;
12832 case CPP_GREATER:
12833 id = ansi_opname (GT_EXPR);
12834 break;
12836 case CPP_PLUS_EQ:
12837 id = ansi_assopname (PLUS_EXPR);
12838 break;
12840 case CPP_MINUS_EQ:
12841 id = ansi_assopname (MINUS_EXPR);
12842 break;
12844 case CPP_MULT_EQ:
12845 id = ansi_assopname (MULT_EXPR);
12846 break;
12848 case CPP_DIV_EQ:
12849 id = ansi_assopname (TRUNC_DIV_EXPR);
12850 break;
12852 case CPP_MOD_EQ:
12853 id = ansi_assopname (TRUNC_MOD_EXPR);
12854 break;
12856 case CPP_XOR_EQ:
12857 id = ansi_assopname (BIT_XOR_EXPR);
12858 break;
12860 case CPP_AND_EQ:
12861 id = ansi_assopname (BIT_AND_EXPR);
12862 break;
12864 case CPP_OR_EQ:
12865 id = ansi_assopname (BIT_IOR_EXPR);
12866 break;
12868 case CPP_LSHIFT:
12869 id = ansi_opname (LSHIFT_EXPR);
12870 break;
12872 case CPP_RSHIFT:
12873 id = ansi_opname (RSHIFT_EXPR);
12874 break;
12876 case CPP_LSHIFT_EQ:
12877 id = ansi_assopname (LSHIFT_EXPR);
12878 break;
12880 case CPP_RSHIFT_EQ:
12881 id = ansi_assopname (RSHIFT_EXPR);
12882 break;
12884 case CPP_EQ_EQ:
12885 id = ansi_opname (EQ_EXPR);
12886 break;
12888 case CPP_NOT_EQ:
12889 id = ansi_opname (NE_EXPR);
12890 break;
12892 case CPP_LESS_EQ:
12893 id = ansi_opname (LE_EXPR);
12894 break;
12896 case CPP_GREATER_EQ:
12897 id = ansi_opname (GE_EXPR);
12898 break;
12900 case CPP_AND_AND:
12901 id = ansi_opname (TRUTH_ANDIF_EXPR);
12902 break;
12904 case CPP_OR_OR:
12905 id = ansi_opname (TRUTH_ORIF_EXPR);
12906 break;
12908 case CPP_PLUS_PLUS:
12909 id = ansi_opname (POSTINCREMENT_EXPR);
12910 break;
12912 case CPP_MINUS_MINUS:
12913 id = ansi_opname (PREDECREMENT_EXPR);
12914 break;
12916 case CPP_COMMA:
12917 id = ansi_opname (COMPOUND_EXPR);
12918 break;
12920 case CPP_DEREF_STAR:
12921 id = ansi_opname (MEMBER_REF);
12922 break;
12924 case CPP_DEREF:
12925 id = ansi_opname (COMPONENT_REF);
12926 break;
12928 case CPP_OPEN_PAREN:
12929 /* Consume the `('. */
12930 cp_lexer_consume_token (parser->lexer);
12931 /* Look for the matching `)'. */
12932 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12933 return ansi_opname (CALL_EXPR);
12935 case CPP_OPEN_SQUARE:
12936 /* Consume the `['. */
12937 cp_lexer_consume_token (parser->lexer);
12938 /* Look for the matching `]'. */
12939 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
12940 return ansi_opname (ARRAY_REF);
12942 case CPP_UTF8STRING:
12943 case CPP_UTF8STRING_USERDEF:
12944 utf8 = true;
12945 case CPP_STRING:
12946 case CPP_WSTRING:
12947 case CPP_STRING16:
12948 case CPP_STRING32:
12949 case CPP_STRING_USERDEF:
12950 case CPP_WSTRING_USERDEF:
12951 case CPP_STRING16_USERDEF:
12952 case CPP_STRING32_USERDEF:
12954 tree str, string_tree;
12955 int sz, len;
12957 if (cxx_dialect == cxx98)
12958 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
12960 /* Consume the string. */
12961 str = cp_parser_string_literal (parser, /*translate=*/true,
12962 /*wide_ok=*/true, /*lookup_udlit=*/false);
12963 if (str == error_mark_node)
12964 return error_mark_node;
12965 else if (TREE_CODE (str) == USERDEF_LITERAL)
12967 string_tree = USERDEF_LITERAL_VALUE (str);
12968 id = USERDEF_LITERAL_SUFFIX_ID (str);
12970 else
12972 string_tree = str;
12973 /* Look for the suffix identifier. */
12974 token = cp_lexer_peek_token (parser->lexer);
12975 if (token->type == CPP_NAME)
12976 id = cp_parser_identifier (parser);
12977 else if (token->type == CPP_KEYWORD)
12979 error ("unexpected keyword;"
12980 " remove space between quotes and suffix identifier");
12981 return error_mark_node;
12983 else
12985 error ("expected suffix identifier");
12986 return error_mark_node;
12989 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
12990 (TREE_TYPE (TREE_TYPE (string_tree))));
12991 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
12992 if (len != 0)
12994 error ("expected empty string after %<operator%> keyword");
12995 return error_mark_node;
12997 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
12998 != char_type_node)
13000 error ("invalid encoding prefix in literal operator");
13001 return error_mark_node;
13003 if (id != error_mark_node)
13005 const char *name = IDENTIFIER_POINTER (id);
13006 id = cp_literal_operator_id (name);
13008 return id;
13011 default:
13012 /* Anything else is an error. */
13013 break;
13016 /* If we have selected an identifier, we need to consume the
13017 operator token. */
13018 if (id)
13019 cp_lexer_consume_token (parser->lexer);
13020 /* Otherwise, no valid operator name was present. */
13021 else
13023 cp_parser_error (parser, "expected operator");
13024 id = error_mark_node;
13027 return id;
13030 /* Parse a template-declaration.
13032 template-declaration:
13033 export [opt] template < template-parameter-list > declaration
13035 If MEMBER_P is TRUE, this template-declaration occurs within a
13036 class-specifier.
13038 The grammar rule given by the standard isn't correct. What
13039 is really meant is:
13041 template-declaration:
13042 export [opt] template-parameter-list-seq
13043 decl-specifier-seq [opt] init-declarator [opt] ;
13044 export [opt] template-parameter-list-seq
13045 function-definition
13047 template-parameter-list-seq:
13048 template-parameter-list-seq [opt]
13049 template < template-parameter-list > */
13051 static void
13052 cp_parser_template_declaration (cp_parser* parser, bool member_p)
13054 /* Check for `export'. */
13055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
13057 /* Consume the `export' token. */
13058 cp_lexer_consume_token (parser->lexer);
13059 /* Warn that we do not support `export'. */
13060 warning (0, "keyword %<export%> not implemented, and will be ignored");
13063 cp_parser_template_declaration_after_export (parser, member_p);
13066 /* Parse a template-parameter-list.
13068 template-parameter-list:
13069 template-parameter
13070 template-parameter-list , template-parameter
13072 Returns a TREE_LIST. Each node represents a template parameter.
13073 The nodes are connected via their TREE_CHAINs. */
13075 static tree
13076 cp_parser_template_parameter_list (cp_parser* parser)
13078 tree parameter_list = NULL_TREE;
13080 begin_template_parm_list ();
13082 /* The loop below parses the template parms. We first need to know
13083 the total number of template parms to be able to compute proper
13084 canonical types of each dependent type. So after the loop, when
13085 we know the total number of template parms,
13086 end_template_parm_list computes the proper canonical types and
13087 fixes up the dependent types accordingly. */
13088 while (true)
13090 tree parameter;
13091 bool is_non_type;
13092 bool is_parameter_pack;
13093 location_t parm_loc;
13095 /* Parse the template-parameter. */
13096 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
13097 parameter = cp_parser_template_parameter (parser,
13098 &is_non_type,
13099 &is_parameter_pack);
13100 /* Add it to the list. */
13101 if (parameter != error_mark_node)
13102 parameter_list = process_template_parm (parameter_list,
13103 parm_loc,
13104 parameter,
13105 is_non_type,
13106 is_parameter_pack);
13107 else
13109 tree err_parm = build_tree_list (parameter, parameter);
13110 parameter_list = chainon (parameter_list, err_parm);
13113 /* If the next token is not a `,', we're done. */
13114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13115 break;
13116 /* Otherwise, consume the `,' token. */
13117 cp_lexer_consume_token (parser->lexer);
13120 return end_template_parm_list (parameter_list);
13123 /* Parse a template-parameter.
13125 template-parameter:
13126 type-parameter
13127 parameter-declaration
13129 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
13130 the parameter. The TREE_PURPOSE is the default value, if any.
13131 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
13132 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
13133 set to true iff this parameter is a parameter pack. */
13135 static tree
13136 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
13137 bool *is_parameter_pack)
13139 cp_token *token;
13140 cp_parameter_declarator *parameter_declarator;
13141 cp_declarator *id_declarator;
13142 tree parm;
13144 /* Assume it is a type parameter or a template parameter. */
13145 *is_non_type = false;
13146 /* Assume it not a parameter pack. */
13147 *is_parameter_pack = false;
13148 /* Peek at the next token. */
13149 token = cp_lexer_peek_token (parser->lexer);
13150 /* If it is `class' or `template', we have a type-parameter. */
13151 if (token->keyword == RID_TEMPLATE)
13152 return cp_parser_type_parameter (parser, is_parameter_pack);
13153 /* If it is `class' or `typename' we do not know yet whether it is a
13154 type parameter or a non-type parameter. Consider:
13156 template <typename T, typename T::X X> ...
13160 template <class C, class D*> ...
13162 Here, the first parameter is a type parameter, and the second is
13163 a non-type parameter. We can tell by looking at the token after
13164 the identifier -- if it is a `,', `=', or `>' then we have a type
13165 parameter. */
13166 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
13168 /* Peek at the token after `class' or `typename'. */
13169 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13170 /* If it's an ellipsis, we have a template type parameter
13171 pack. */
13172 if (token->type == CPP_ELLIPSIS)
13173 return cp_parser_type_parameter (parser, is_parameter_pack);
13174 /* If it's an identifier, skip it. */
13175 if (token->type == CPP_NAME)
13176 token = cp_lexer_peek_nth_token (parser->lexer, 3);
13177 /* Now, see if the token looks like the end of a template
13178 parameter. */
13179 if (token->type == CPP_COMMA
13180 || token->type == CPP_EQ
13181 || token->type == CPP_GREATER)
13182 return cp_parser_type_parameter (parser, is_parameter_pack);
13185 /* Otherwise, it is a non-type parameter.
13187 [temp.param]
13189 When parsing a default template-argument for a non-type
13190 template-parameter, the first non-nested `>' is taken as the end
13191 of the template parameter-list rather than a greater-than
13192 operator. */
13193 *is_non_type = true;
13194 parameter_declarator
13195 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
13196 /*parenthesized_p=*/NULL);
13198 if (!parameter_declarator)
13199 return error_mark_node;
13201 /* If the parameter declaration is marked as a parameter pack, set
13202 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
13203 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
13204 grokdeclarator. */
13205 if (parameter_declarator->declarator
13206 && parameter_declarator->declarator->parameter_pack_p)
13208 *is_parameter_pack = true;
13209 parameter_declarator->declarator->parameter_pack_p = false;
13212 if (parameter_declarator->default_argument)
13214 /* Can happen in some cases of erroneous input (c++/34892). */
13215 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13216 /* Consume the `...' for better error recovery. */
13217 cp_lexer_consume_token (parser->lexer);
13219 /* If the next token is an ellipsis, and we don't already have it
13220 marked as a parameter pack, then we have a parameter pack (that
13221 has no declarator). */
13222 else if (!*is_parameter_pack
13223 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13224 && (declarator_can_be_parameter_pack
13225 (parameter_declarator->declarator)))
13227 /* Consume the `...'. */
13228 cp_lexer_consume_token (parser->lexer);
13229 maybe_warn_variadic_templates ();
13231 *is_parameter_pack = true;
13233 /* We might end up with a pack expansion as the type of the non-type
13234 template parameter, in which case this is a non-type template
13235 parameter pack. */
13236 else if (parameter_declarator->decl_specifiers.type
13237 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
13239 *is_parameter_pack = true;
13240 parameter_declarator->decl_specifiers.type =
13241 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
13244 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13246 /* Parameter packs cannot have default arguments. However, a
13247 user may try to do so, so we'll parse them and give an
13248 appropriate diagnostic here. */
13250 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13252 /* Find the name of the parameter pack. */
13253 id_declarator = parameter_declarator->declarator;
13254 while (id_declarator && id_declarator->kind != cdk_id)
13255 id_declarator = id_declarator->declarator;
13257 if (id_declarator && id_declarator->kind == cdk_id)
13258 error_at (start_token->location,
13259 "template parameter pack %qD cannot have a default argument",
13260 id_declarator->u.id.unqualified_name);
13261 else
13262 error_at (start_token->location,
13263 "template parameter pack cannot have a default argument");
13265 /* Parse the default argument, but throw away the result. */
13266 cp_parser_default_argument (parser, /*template_parm_p=*/true);
13269 parm = grokdeclarator (parameter_declarator->declarator,
13270 &parameter_declarator->decl_specifiers,
13271 TPARM, /*initialized=*/0,
13272 /*attrlist=*/NULL);
13273 if (parm == error_mark_node)
13274 return error_mark_node;
13276 return build_tree_list (parameter_declarator->default_argument, parm);
13279 /* Parse a type-parameter.
13281 type-parameter:
13282 class identifier [opt]
13283 class identifier [opt] = type-id
13284 typename identifier [opt]
13285 typename identifier [opt] = type-id
13286 template < template-parameter-list > class identifier [opt]
13287 template < template-parameter-list > class identifier [opt]
13288 = id-expression
13290 GNU Extension (variadic templates):
13292 type-parameter:
13293 class ... identifier [opt]
13294 typename ... identifier [opt]
13296 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
13297 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
13298 the declaration of the parameter.
13300 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
13302 static tree
13303 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
13305 cp_token *token;
13306 tree parameter;
13308 /* Look for a keyword to tell us what kind of parameter this is. */
13309 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
13310 if (!token)
13311 return error_mark_node;
13313 switch (token->keyword)
13315 case RID_CLASS:
13316 case RID_TYPENAME:
13318 tree identifier;
13319 tree default_argument;
13321 /* If the next token is an ellipsis, we have a template
13322 argument pack. */
13323 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13325 /* Consume the `...' token. */
13326 cp_lexer_consume_token (parser->lexer);
13327 maybe_warn_variadic_templates ();
13329 *is_parameter_pack = true;
13332 /* If the next token is an identifier, then it names the
13333 parameter. */
13334 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13335 identifier = cp_parser_identifier (parser);
13336 else
13337 identifier = NULL_TREE;
13339 /* Create the parameter. */
13340 parameter = finish_template_type_parm (class_type_node, identifier);
13342 /* If the next token is an `=', we have a default argument. */
13343 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13345 /* Consume the `=' token. */
13346 cp_lexer_consume_token (parser->lexer);
13347 /* Parse the default-argument. */
13348 push_deferring_access_checks (dk_no_deferred);
13349 default_argument = cp_parser_type_id (parser);
13351 /* Template parameter packs cannot have default
13352 arguments. */
13353 if (*is_parameter_pack)
13355 if (identifier)
13356 error_at (token->location,
13357 "template parameter pack %qD cannot have a "
13358 "default argument", identifier);
13359 else
13360 error_at (token->location,
13361 "template parameter packs cannot have "
13362 "default arguments");
13363 default_argument = NULL_TREE;
13365 pop_deferring_access_checks ();
13367 else
13368 default_argument = NULL_TREE;
13370 /* Create the combined representation of the parameter and the
13371 default argument. */
13372 parameter = build_tree_list (default_argument, parameter);
13374 break;
13376 case RID_TEMPLATE:
13378 tree identifier;
13379 tree default_argument;
13381 /* Look for the `<'. */
13382 cp_parser_require (parser, CPP_LESS, RT_LESS);
13383 /* Parse the template-parameter-list. */
13384 cp_parser_template_parameter_list (parser);
13385 /* Look for the `>'. */
13386 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13387 /* Look for the `class' or 'typename' keywords. */
13388 cp_parser_type_parameter_key (parser);
13389 /* If the next token is an ellipsis, we have a template
13390 argument pack. */
13391 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13393 /* Consume the `...' token. */
13394 cp_lexer_consume_token (parser->lexer);
13395 maybe_warn_variadic_templates ();
13397 *is_parameter_pack = true;
13399 /* If the next token is an `=', then there is a
13400 default-argument. If the next token is a `>', we are at
13401 the end of the parameter-list. If the next token is a `,',
13402 then we are at the end of this parameter. */
13403 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13404 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
13405 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13407 identifier = cp_parser_identifier (parser);
13408 /* Treat invalid names as if the parameter were nameless. */
13409 if (identifier == error_mark_node)
13410 identifier = NULL_TREE;
13412 else
13413 identifier = NULL_TREE;
13415 /* Create the template parameter. */
13416 parameter = finish_template_template_parm (class_type_node,
13417 identifier);
13419 /* If the next token is an `=', then there is a
13420 default-argument. */
13421 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13423 bool is_template;
13425 /* Consume the `='. */
13426 cp_lexer_consume_token (parser->lexer);
13427 /* Parse the id-expression. */
13428 push_deferring_access_checks (dk_no_deferred);
13429 /* save token before parsing the id-expression, for error
13430 reporting */
13431 token = cp_lexer_peek_token (parser->lexer);
13432 default_argument
13433 = cp_parser_id_expression (parser,
13434 /*template_keyword_p=*/false,
13435 /*check_dependency_p=*/true,
13436 /*template_p=*/&is_template,
13437 /*declarator_p=*/false,
13438 /*optional_p=*/false);
13439 if (TREE_CODE (default_argument) == TYPE_DECL)
13440 /* If the id-expression was a template-id that refers to
13441 a template-class, we already have the declaration here,
13442 so no further lookup is needed. */
13444 else
13445 /* Look up the name. */
13446 default_argument
13447 = cp_parser_lookup_name (parser, default_argument,
13448 none_type,
13449 /*is_template=*/is_template,
13450 /*is_namespace=*/false,
13451 /*check_dependency=*/true,
13452 /*ambiguous_decls=*/NULL,
13453 token->location);
13454 /* See if the default argument is valid. */
13455 default_argument
13456 = check_template_template_default_arg (default_argument);
13458 /* Template parameter packs cannot have default
13459 arguments. */
13460 if (*is_parameter_pack)
13462 if (identifier)
13463 error_at (token->location,
13464 "template parameter pack %qD cannot "
13465 "have a default argument",
13466 identifier);
13467 else
13468 error_at (token->location, "template parameter packs cannot "
13469 "have default arguments");
13470 default_argument = NULL_TREE;
13472 pop_deferring_access_checks ();
13474 else
13475 default_argument = NULL_TREE;
13477 /* Create the combined representation of the parameter and the
13478 default argument. */
13479 parameter = build_tree_list (default_argument, parameter);
13481 break;
13483 default:
13484 gcc_unreachable ();
13485 break;
13488 return parameter;
13491 /* Parse a template-id.
13493 template-id:
13494 template-name < template-argument-list [opt] >
13496 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
13497 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
13498 returned. Otherwise, if the template-name names a function, or set
13499 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
13500 names a class, returns a TYPE_DECL for the specialization.
13502 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
13503 uninstantiated templates. */
13505 static tree
13506 cp_parser_template_id (cp_parser *parser,
13507 bool template_keyword_p,
13508 bool check_dependency_p,
13509 enum tag_types tag_type,
13510 bool is_declaration)
13512 int i;
13513 tree templ;
13514 tree arguments;
13515 tree template_id;
13516 cp_token_position start_of_id = 0;
13517 deferred_access_check *chk;
13518 vec<deferred_access_check, va_gc> *access_check;
13519 cp_token *next_token = NULL, *next_token_2 = NULL;
13520 bool is_identifier;
13522 /* If the next token corresponds to a template-id, there is no need
13523 to reparse it. */
13524 next_token = cp_lexer_peek_token (parser->lexer);
13525 if (next_token->type == CPP_TEMPLATE_ID)
13527 struct tree_check *check_value;
13529 /* Get the stored value. */
13530 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
13531 /* Perform any access checks that were deferred. */
13532 access_check = check_value->checks;
13533 if (access_check)
13535 FOR_EACH_VEC_ELT (*access_check, i, chk)
13536 perform_or_defer_access_check (chk->binfo,
13537 chk->decl,
13538 chk->diag_decl,
13539 tf_warning_or_error);
13541 /* Return the stored value. */
13542 return check_value->value;
13545 /* Avoid performing name lookup if there is no possibility of
13546 finding a template-id. */
13547 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
13548 || (next_token->type == CPP_NAME
13549 && !cp_parser_nth_token_starts_template_argument_list_p
13550 (parser, 2)))
13552 cp_parser_error (parser, "expected template-id");
13553 return error_mark_node;
13556 /* Remember where the template-id starts. */
13557 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
13558 start_of_id = cp_lexer_token_position (parser->lexer, false);
13560 push_deferring_access_checks (dk_deferred);
13562 /* Parse the template-name. */
13563 is_identifier = false;
13564 templ = cp_parser_template_name (parser, template_keyword_p,
13565 check_dependency_p,
13566 is_declaration,
13567 tag_type,
13568 &is_identifier);
13569 if (templ == error_mark_node || is_identifier)
13571 pop_deferring_access_checks ();
13572 return templ;
13575 /* If we find the sequence `[:' after a template-name, it's probably
13576 a digraph-typo for `< ::'. Substitute the tokens and check if we can
13577 parse correctly the argument list. */
13578 next_token = cp_lexer_peek_token (parser->lexer);
13579 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13580 if (next_token->type == CPP_OPEN_SQUARE
13581 && next_token->flags & DIGRAPH
13582 && next_token_2->type == CPP_COLON
13583 && !(next_token_2->flags & PREV_WHITE))
13585 cp_parser_parse_tentatively (parser);
13586 /* Change `:' into `::'. */
13587 next_token_2->type = CPP_SCOPE;
13588 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
13589 CPP_LESS. */
13590 cp_lexer_consume_token (parser->lexer);
13592 /* Parse the arguments. */
13593 arguments = cp_parser_enclosed_template_argument_list (parser);
13594 if (!cp_parser_parse_definitely (parser))
13596 /* If we couldn't parse an argument list, then we revert our changes
13597 and return simply an error. Maybe this is not a template-id
13598 after all. */
13599 next_token_2->type = CPP_COLON;
13600 cp_parser_error (parser, "expected %<<%>");
13601 pop_deferring_access_checks ();
13602 return error_mark_node;
13604 /* Otherwise, emit an error about the invalid digraph, but continue
13605 parsing because we got our argument list. */
13606 if (permerror (next_token->location,
13607 "%<<::%> cannot begin a template-argument list"))
13609 static bool hint = false;
13610 inform (next_token->location,
13611 "%<<:%> is an alternate spelling for %<[%>."
13612 " Insert whitespace between %<<%> and %<::%>");
13613 if (!hint && !flag_permissive)
13615 inform (next_token->location, "(if you use %<-fpermissive%> "
13616 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
13617 "accept your code)");
13618 hint = true;
13622 else
13624 /* Look for the `<' that starts the template-argument-list. */
13625 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
13627 pop_deferring_access_checks ();
13628 return error_mark_node;
13630 /* Parse the arguments. */
13631 arguments = cp_parser_enclosed_template_argument_list (parser);
13634 /* Build a representation of the specialization. */
13635 if (identifier_p (templ))
13636 template_id = build_min_nt_loc (next_token->location,
13637 TEMPLATE_ID_EXPR,
13638 templ, arguments);
13639 else if (DECL_TYPE_TEMPLATE_P (templ)
13640 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
13642 bool entering_scope;
13643 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
13644 template (rather than some instantiation thereof) only if
13645 is not nested within some other construct. For example, in
13646 "template <typename T> void f(T) { A<T>::", A<T> is just an
13647 instantiation of A. */
13648 entering_scope = (template_parm_scope_p ()
13649 && cp_lexer_next_token_is (parser->lexer,
13650 CPP_SCOPE));
13651 template_id
13652 = finish_template_type (templ, arguments, entering_scope);
13654 else if (variable_template_p (templ))
13656 template_id = lookup_template_variable (templ, arguments);
13658 else
13660 /* If it's not a class-template or a template-template, it should be
13661 a function-template. */
13662 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
13663 || TREE_CODE (templ) == OVERLOAD
13664 || BASELINK_P (templ)));
13666 template_id = lookup_template_function (templ, arguments);
13669 /* If parsing tentatively, replace the sequence of tokens that makes
13670 up the template-id with a CPP_TEMPLATE_ID token. That way,
13671 should we re-parse the token stream, we will not have to repeat
13672 the effort required to do the parse, nor will we issue duplicate
13673 error messages about problems during instantiation of the
13674 template. */
13675 if (start_of_id
13676 /* Don't do this if we had a parse error in a declarator; re-parsing
13677 might succeed if a name changes meaning (60361). */
13678 && !(cp_parser_error_occurred (parser)
13679 && cp_parser_parsing_tentatively (parser)
13680 && parser->in_declarator_p))
13682 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
13684 /* Reset the contents of the START_OF_ID token. */
13685 token->type = CPP_TEMPLATE_ID;
13686 /* Retrieve any deferred checks. Do not pop this access checks yet
13687 so the memory will not be reclaimed during token replacing below. */
13688 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13689 token->u.tree_check_value->value = template_id;
13690 token->u.tree_check_value->checks = get_deferred_access_checks ();
13691 token->keyword = RID_MAX;
13693 /* Purge all subsequent tokens. */
13694 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
13696 /* ??? Can we actually assume that, if template_id ==
13697 error_mark_node, we will have issued a diagnostic to the
13698 user, as opposed to simply marking the tentative parse as
13699 failed? */
13700 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
13701 error_at (token->location, "parse error in template argument list");
13704 pop_to_parent_deferring_access_checks ();
13705 return template_id;
13708 /* Parse a template-name.
13710 template-name:
13711 identifier
13713 The standard should actually say:
13715 template-name:
13716 identifier
13717 operator-function-id
13719 A defect report has been filed about this issue.
13721 A conversion-function-id cannot be a template name because they cannot
13722 be part of a template-id. In fact, looking at this code:
13724 a.operator K<int>()
13726 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
13727 It is impossible to call a templated conversion-function-id with an
13728 explicit argument list, since the only allowed template parameter is
13729 the type to which it is converting.
13731 If TEMPLATE_KEYWORD_P is true, then we have just seen the
13732 `template' keyword, in a construction like:
13734 T::template f<3>()
13736 In that case `f' is taken to be a template-name, even though there
13737 is no way of knowing for sure.
13739 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
13740 name refers to a set of overloaded functions, at least one of which
13741 is a template, or an IDENTIFIER_NODE with the name of the template,
13742 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
13743 names are looked up inside uninstantiated templates. */
13745 static tree
13746 cp_parser_template_name (cp_parser* parser,
13747 bool template_keyword_p,
13748 bool check_dependency_p,
13749 bool is_declaration,
13750 enum tag_types tag_type,
13751 bool *is_identifier)
13753 tree identifier;
13754 tree decl;
13755 tree fns;
13756 cp_token *token = cp_lexer_peek_token (parser->lexer);
13758 /* If the next token is `operator', then we have either an
13759 operator-function-id or a conversion-function-id. */
13760 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
13762 /* We don't know whether we're looking at an
13763 operator-function-id or a conversion-function-id. */
13764 cp_parser_parse_tentatively (parser);
13765 /* Try an operator-function-id. */
13766 identifier = cp_parser_operator_function_id (parser);
13767 /* If that didn't work, try a conversion-function-id. */
13768 if (!cp_parser_parse_definitely (parser))
13770 cp_parser_error (parser, "expected template-name");
13771 return error_mark_node;
13774 /* Look for the identifier. */
13775 else
13776 identifier = cp_parser_identifier (parser);
13778 /* If we didn't find an identifier, we don't have a template-id. */
13779 if (identifier == error_mark_node)
13780 return error_mark_node;
13782 /* If the name immediately followed the `template' keyword, then it
13783 is a template-name. However, if the next token is not `<', then
13784 we do not treat it as a template-name, since it is not being used
13785 as part of a template-id. This enables us to handle constructs
13786 like:
13788 template <typename T> struct S { S(); };
13789 template <typename T> S<T>::S();
13791 correctly. We would treat `S' as a template -- if it were `S<T>'
13792 -- but we do not if there is no `<'. */
13794 if (processing_template_decl
13795 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
13797 /* In a declaration, in a dependent context, we pretend that the
13798 "template" keyword was present in order to improve error
13799 recovery. For example, given:
13801 template <typename T> void f(T::X<int>);
13803 we want to treat "X<int>" as a template-id. */
13804 if (is_declaration
13805 && !template_keyword_p
13806 && parser->scope && TYPE_P (parser->scope)
13807 && check_dependency_p
13808 && dependent_scope_p (parser->scope)
13809 /* Do not do this for dtors (or ctors), since they never
13810 need the template keyword before their name. */
13811 && !constructor_name_p (identifier, parser->scope))
13813 cp_token_position start = 0;
13815 /* Explain what went wrong. */
13816 error_at (token->location, "non-template %qD used as template",
13817 identifier);
13818 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
13819 parser->scope, identifier);
13820 /* If parsing tentatively, find the location of the "<" token. */
13821 if (cp_parser_simulate_error (parser))
13822 start = cp_lexer_token_position (parser->lexer, true);
13823 /* Parse the template arguments so that we can issue error
13824 messages about them. */
13825 cp_lexer_consume_token (parser->lexer);
13826 cp_parser_enclosed_template_argument_list (parser);
13827 /* Skip tokens until we find a good place from which to
13828 continue parsing. */
13829 cp_parser_skip_to_closing_parenthesis (parser,
13830 /*recovering=*/true,
13831 /*or_comma=*/true,
13832 /*consume_paren=*/false);
13833 /* If parsing tentatively, permanently remove the
13834 template argument list. That will prevent duplicate
13835 error messages from being issued about the missing
13836 "template" keyword. */
13837 if (start)
13838 cp_lexer_purge_tokens_after (parser->lexer, start);
13839 if (is_identifier)
13840 *is_identifier = true;
13841 return identifier;
13844 /* If the "template" keyword is present, then there is generally
13845 no point in doing name-lookup, so we just return IDENTIFIER.
13846 But, if the qualifying scope is non-dependent then we can
13847 (and must) do name-lookup normally. */
13848 if (template_keyword_p
13849 && (!parser->scope
13850 || (TYPE_P (parser->scope)
13851 && dependent_type_p (parser->scope))))
13852 return identifier;
13855 /* Look up the name. */
13856 decl = cp_parser_lookup_name (parser, identifier,
13857 tag_type,
13858 /*is_template=*/true,
13859 /*is_namespace=*/false,
13860 check_dependency_p,
13861 /*ambiguous_decls=*/NULL,
13862 token->location);
13864 /* If DECL is a template, then the name was a template-name. */
13865 if (TREE_CODE (decl) == TEMPLATE_DECL)
13867 else
13869 tree fn = NULL_TREE;
13871 /* The standard does not explicitly indicate whether a name that
13872 names a set of overloaded declarations, some of which are
13873 templates, is a template-name. However, such a name should
13874 be a template-name; otherwise, there is no way to form a
13875 template-id for the overloaded templates. */
13876 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
13877 if (TREE_CODE (fns) == OVERLOAD)
13878 for (fn = fns; fn; fn = OVL_NEXT (fn))
13879 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
13880 break;
13882 if (!fn)
13884 /* The name does not name a template. */
13885 cp_parser_error (parser, "expected template-name");
13886 return error_mark_node;
13890 /* If DECL is dependent, and refers to a function, then just return
13891 its name; we will look it up again during template instantiation. */
13892 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
13894 tree scope = ovl_scope (decl);
13895 if (TYPE_P (scope) && dependent_type_p (scope))
13896 return identifier;
13899 return decl;
13902 /* Parse a template-argument-list.
13904 template-argument-list:
13905 template-argument ... [opt]
13906 template-argument-list , template-argument ... [opt]
13908 Returns a TREE_VEC containing the arguments. */
13910 static tree
13911 cp_parser_template_argument_list (cp_parser* parser)
13913 tree fixed_args[10];
13914 unsigned n_args = 0;
13915 unsigned alloced = 10;
13916 tree *arg_ary = fixed_args;
13917 tree vec;
13918 bool saved_in_template_argument_list_p;
13919 bool saved_ice_p;
13920 bool saved_non_ice_p;
13922 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
13923 parser->in_template_argument_list_p = true;
13924 /* Even if the template-id appears in an integral
13925 constant-expression, the contents of the argument list do
13926 not. */
13927 saved_ice_p = parser->integral_constant_expression_p;
13928 parser->integral_constant_expression_p = false;
13929 saved_non_ice_p = parser->non_integral_constant_expression_p;
13930 parser->non_integral_constant_expression_p = false;
13932 /* Parse the arguments. */
13935 tree argument;
13937 if (n_args)
13938 /* Consume the comma. */
13939 cp_lexer_consume_token (parser->lexer);
13941 /* Parse the template-argument. */
13942 argument = cp_parser_template_argument (parser);
13944 /* If the next token is an ellipsis, we're expanding a template
13945 argument pack. */
13946 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13948 if (argument == error_mark_node)
13950 cp_token *token = cp_lexer_peek_token (parser->lexer);
13951 error_at (token->location,
13952 "expected parameter pack before %<...%>");
13954 /* Consume the `...' token. */
13955 cp_lexer_consume_token (parser->lexer);
13957 /* Make the argument into a TYPE_PACK_EXPANSION or
13958 EXPR_PACK_EXPANSION. */
13959 argument = make_pack_expansion (argument);
13962 if (n_args == alloced)
13964 alloced *= 2;
13966 if (arg_ary == fixed_args)
13968 arg_ary = XNEWVEC (tree, alloced);
13969 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
13971 else
13972 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
13974 arg_ary[n_args++] = argument;
13976 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
13978 vec = make_tree_vec (n_args);
13980 while (n_args--)
13981 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
13983 if (arg_ary != fixed_args)
13984 free (arg_ary);
13985 parser->non_integral_constant_expression_p = saved_non_ice_p;
13986 parser->integral_constant_expression_p = saved_ice_p;
13987 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
13988 #ifdef ENABLE_CHECKING
13989 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
13990 #endif
13991 return vec;
13994 /* Parse a template-argument.
13996 template-argument:
13997 assignment-expression
13998 type-id
13999 id-expression
14001 The representation is that of an assignment-expression, type-id, or
14002 id-expression -- except that the qualified id-expression is
14003 evaluated, so that the value returned is either a DECL or an
14004 OVERLOAD.
14006 Although the standard says "assignment-expression", it forbids
14007 throw-expressions or assignments in the template argument.
14008 Therefore, we use "conditional-expression" instead. */
14010 static tree
14011 cp_parser_template_argument (cp_parser* parser)
14013 tree argument;
14014 bool template_p;
14015 bool address_p;
14016 bool maybe_type_id = false;
14017 cp_token *token = NULL, *argument_start_token = NULL;
14018 location_t loc = 0;
14019 cp_id_kind idk;
14021 /* There's really no way to know what we're looking at, so we just
14022 try each alternative in order.
14024 [temp.arg]
14026 In a template-argument, an ambiguity between a type-id and an
14027 expression is resolved to a type-id, regardless of the form of
14028 the corresponding template-parameter.
14030 Therefore, we try a type-id first. */
14031 cp_parser_parse_tentatively (parser);
14032 argument = cp_parser_template_type_arg (parser);
14033 /* If there was no error parsing the type-id but the next token is a
14034 '>>', our behavior depends on which dialect of C++ we're
14035 parsing. In C++98, we probably found a typo for '> >'. But there
14036 are type-id which are also valid expressions. For instance:
14038 struct X { int operator >> (int); };
14039 template <int V> struct Foo {};
14040 Foo<X () >> 5> r;
14042 Here 'X()' is a valid type-id of a function type, but the user just
14043 wanted to write the expression "X() >> 5". Thus, we remember that we
14044 found a valid type-id, but we still try to parse the argument as an
14045 expression to see what happens.
14047 In C++0x, the '>>' will be considered two separate '>'
14048 tokens. */
14049 if (!cp_parser_error_occurred (parser)
14050 && cxx_dialect == cxx98
14051 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14053 maybe_type_id = true;
14054 cp_parser_abort_tentative_parse (parser);
14056 else
14058 /* If the next token isn't a `,' or a `>', then this argument wasn't
14059 really finished. This means that the argument is not a valid
14060 type-id. */
14061 if (!cp_parser_next_token_ends_template_argument_p (parser))
14062 cp_parser_error (parser, "expected template-argument");
14063 /* If that worked, we're done. */
14064 if (cp_parser_parse_definitely (parser))
14065 return argument;
14067 /* We're still not sure what the argument will be. */
14068 cp_parser_parse_tentatively (parser);
14069 /* Try a template. */
14070 argument_start_token = cp_lexer_peek_token (parser->lexer);
14071 argument = cp_parser_id_expression (parser,
14072 /*template_keyword_p=*/false,
14073 /*check_dependency_p=*/true,
14074 &template_p,
14075 /*declarator_p=*/false,
14076 /*optional_p=*/false);
14077 /* If the next token isn't a `,' or a `>', then this argument wasn't
14078 really finished. */
14079 if (!cp_parser_next_token_ends_template_argument_p (parser))
14080 cp_parser_error (parser, "expected template-argument");
14081 if (!cp_parser_error_occurred (parser))
14083 /* Figure out what is being referred to. If the id-expression
14084 was for a class template specialization, then we will have a
14085 TYPE_DECL at this point. There is no need to do name lookup
14086 at this point in that case. */
14087 if (TREE_CODE (argument) != TYPE_DECL)
14088 argument = cp_parser_lookup_name (parser, argument,
14089 none_type,
14090 /*is_template=*/template_p,
14091 /*is_namespace=*/false,
14092 /*check_dependency=*/true,
14093 /*ambiguous_decls=*/NULL,
14094 argument_start_token->location);
14095 if (TREE_CODE (argument) != TEMPLATE_DECL
14096 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
14097 cp_parser_error (parser, "expected template-name");
14099 if (cp_parser_parse_definitely (parser))
14100 return argument;
14101 /* It must be a non-type argument. There permitted cases are given
14102 in [temp.arg.nontype]:
14104 -- an integral constant-expression of integral or enumeration
14105 type; or
14107 -- the name of a non-type template-parameter; or
14109 -- the name of an object or function with external linkage...
14111 -- the address of an object or function with external linkage...
14113 -- a pointer to member... */
14114 /* Look for a non-type template parameter. */
14115 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14117 cp_parser_parse_tentatively (parser);
14118 argument = cp_parser_primary_expression (parser,
14119 /*address_p=*/false,
14120 /*cast_p=*/false,
14121 /*template_arg_p=*/true,
14122 &idk);
14123 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
14124 || !cp_parser_next_token_ends_template_argument_p (parser))
14125 cp_parser_simulate_error (parser);
14126 if (cp_parser_parse_definitely (parser))
14127 return argument;
14130 /* If the next token is "&", the argument must be the address of an
14131 object or function with external linkage. */
14132 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
14133 if (address_p)
14135 loc = cp_lexer_peek_token (parser->lexer)->location;
14136 cp_lexer_consume_token (parser->lexer);
14138 /* See if we might have an id-expression. */
14139 token = cp_lexer_peek_token (parser->lexer);
14140 if (token->type == CPP_NAME
14141 || token->keyword == RID_OPERATOR
14142 || token->type == CPP_SCOPE
14143 || token->type == CPP_TEMPLATE_ID
14144 || token->type == CPP_NESTED_NAME_SPECIFIER)
14146 cp_parser_parse_tentatively (parser);
14147 argument = cp_parser_primary_expression (parser,
14148 address_p,
14149 /*cast_p=*/false,
14150 /*template_arg_p=*/true,
14151 &idk);
14152 if (cp_parser_error_occurred (parser)
14153 || !cp_parser_next_token_ends_template_argument_p (parser))
14154 cp_parser_abort_tentative_parse (parser);
14155 else
14157 tree probe;
14159 if (INDIRECT_REF_P (argument))
14161 /* Strip the dereference temporarily. */
14162 gcc_assert (REFERENCE_REF_P (argument));
14163 argument = TREE_OPERAND (argument, 0);
14166 /* If we're in a template, we represent a qualified-id referring
14167 to a static data member as a SCOPE_REF even if the scope isn't
14168 dependent so that we can check access control later. */
14169 probe = argument;
14170 if (TREE_CODE (probe) == SCOPE_REF)
14171 probe = TREE_OPERAND (probe, 1);
14172 if (VAR_P (probe))
14174 /* A variable without external linkage might still be a
14175 valid constant-expression, so no error is issued here
14176 if the external-linkage check fails. */
14177 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
14178 cp_parser_simulate_error (parser);
14180 else if (is_overloaded_fn (argument))
14181 /* All overloaded functions are allowed; if the external
14182 linkage test does not pass, an error will be issued
14183 later. */
14185 else if (address_p
14186 && (TREE_CODE (argument) == OFFSET_REF
14187 || TREE_CODE (argument) == SCOPE_REF))
14188 /* A pointer-to-member. */
14190 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
14192 else
14193 cp_parser_simulate_error (parser);
14195 if (cp_parser_parse_definitely (parser))
14197 if (address_p)
14198 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
14199 tf_warning_or_error);
14200 else
14201 argument = convert_from_reference (argument);
14202 return argument;
14206 /* If the argument started with "&", there are no other valid
14207 alternatives at this point. */
14208 if (address_p)
14210 cp_parser_error (parser, "invalid non-type template argument");
14211 return error_mark_node;
14214 /* If the argument wasn't successfully parsed as a type-id followed
14215 by '>>', the argument can only be a constant expression now.
14216 Otherwise, we try parsing the constant-expression tentatively,
14217 because the argument could really be a type-id. */
14218 if (maybe_type_id)
14219 cp_parser_parse_tentatively (parser);
14220 argument = cp_parser_constant_expression (parser,
14221 /*allow_non_constant_p=*/false,
14222 /*non_constant_p=*/NULL);
14223 if (!maybe_type_id)
14224 return argument;
14225 if (!cp_parser_next_token_ends_template_argument_p (parser))
14226 cp_parser_error (parser, "expected template-argument");
14227 if (cp_parser_parse_definitely (parser))
14228 return argument;
14229 /* We did our best to parse the argument as a non type-id, but that
14230 was the only alternative that matched (albeit with a '>' after
14231 it). We can assume it's just a typo from the user, and a
14232 diagnostic will then be issued. */
14233 return cp_parser_template_type_arg (parser);
14236 /* Parse an explicit-instantiation.
14238 explicit-instantiation:
14239 template declaration
14241 Although the standard says `declaration', what it really means is:
14243 explicit-instantiation:
14244 template decl-specifier-seq [opt] declarator [opt] ;
14246 Things like `template int S<int>::i = 5, int S<double>::j;' are not
14247 supposed to be allowed. A defect report has been filed about this
14248 issue.
14250 GNU Extension:
14252 explicit-instantiation:
14253 storage-class-specifier template
14254 decl-specifier-seq [opt] declarator [opt] ;
14255 function-specifier template
14256 decl-specifier-seq [opt] declarator [opt] ; */
14258 static void
14259 cp_parser_explicit_instantiation (cp_parser* parser)
14261 int declares_class_or_enum;
14262 cp_decl_specifier_seq decl_specifiers;
14263 tree extension_specifier = NULL_TREE;
14265 timevar_push (TV_TEMPLATE_INST);
14267 /* Look for an (optional) storage-class-specifier or
14268 function-specifier. */
14269 if (cp_parser_allow_gnu_extensions_p (parser))
14271 extension_specifier
14272 = cp_parser_storage_class_specifier_opt (parser);
14273 if (!extension_specifier)
14274 extension_specifier
14275 = cp_parser_function_specifier_opt (parser,
14276 /*decl_specs=*/NULL);
14279 /* Look for the `template' keyword. */
14280 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14281 /* Let the front end know that we are processing an explicit
14282 instantiation. */
14283 begin_explicit_instantiation ();
14284 /* [temp.explicit] says that we are supposed to ignore access
14285 control while processing explicit instantiation directives. */
14286 push_deferring_access_checks (dk_no_check);
14287 /* Parse a decl-specifier-seq. */
14288 cp_parser_decl_specifier_seq (parser,
14289 CP_PARSER_FLAGS_OPTIONAL,
14290 &decl_specifiers,
14291 &declares_class_or_enum);
14292 /* If there was exactly one decl-specifier, and it declared a class,
14293 and there's no declarator, then we have an explicit type
14294 instantiation. */
14295 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
14297 tree type;
14299 type = check_tag_decl (&decl_specifiers,
14300 /*explicit_type_instantiation_p=*/true);
14301 /* Turn access control back on for names used during
14302 template instantiation. */
14303 pop_deferring_access_checks ();
14304 if (type)
14305 do_type_instantiation (type, extension_specifier,
14306 /*complain=*/tf_error);
14308 else
14310 cp_declarator *declarator;
14311 tree decl;
14313 /* Parse the declarator. */
14314 declarator
14315 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14316 /*ctor_dtor_or_conv_p=*/NULL,
14317 /*parenthesized_p=*/NULL,
14318 /*member_p=*/false,
14319 /*friend_p=*/false);
14320 if (declares_class_or_enum & 2)
14321 cp_parser_check_for_definition_in_return_type (declarator,
14322 decl_specifiers.type,
14323 decl_specifiers.locations[ds_type_spec]);
14324 if (declarator != cp_error_declarator)
14326 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
14327 permerror (decl_specifiers.locations[ds_inline],
14328 "explicit instantiation shall not use"
14329 " %<inline%> specifier");
14330 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
14331 permerror (decl_specifiers.locations[ds_constexpr],
14332 "explicit instantiation shall not use"
14333 " %<constexpr%> specifier");
14335 decl = grokdeclarator (declarator, &decl_specifiers,
14336 NORMAL, 0, &decl_specifiers.attributes);
14337 /* Turn access control back on for names used during
14338 template instantiation. */
14339 pop_deferring_access_checks ();
14340 /* Do the explicit instantiation. */
14341 do_decl_instantiation (decl, extension_specifier);
14343 else
14345 pop_deferring_access_checks ();
14346 /* Skip the body of the explicit instantiation. */
14347 cp_parser_skip_to_end_of_statement (parser);
14350 /* We're done with the instantiation. */
14351 end_explicit_instantiation ();
14353 cp_parser_consume_semicolon_at_end_of_statement (parser);
14355 timevar_pop (TV_TEMPLATE_INST);
14358 /* Parse an explicit-specialization.
14360 explicit-specialization:
14361 template < > declaration
14363 Although the standard says `declaration', what it really means is:
14365 explicit-specialization:
14366 template <> decl-specifier [opt] init-declarator [opt] ;
14367 template <> function-definition
14368 template <> explicit-specialization
14369 template <> template-declaration */
14371 static void
14372 cp_parser_explicit_specialization (cp_parser* parser)
14374 bool need_lang_pop;
14375 cp_token *token = cp_lexer_peek_token (parser->lexer);
14377 /* Look for the `template' keyword. */
14378 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
14379 /* Look for the `<'. */
14380 cp_parser_require (parser, CPP_LESS, RT_LESS);
14381 /* Look for the `>'. */
14382 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14383 /* We have processed another parameter list. */
14384 ++parser->num_template_parameter_lists;
14385 /* [temp]
14387 A template ... explicit specialization ... shall not have C
14388 linkage. */
14389 if (current_lang_name == lang_name_c)
14391 error_at (token->location, "template specialization with C linkage");
14392 /* Give it C++ linkage to avoid confusing other parts of the
14393 front end. */
14394 push_lang_context (lang_name_cplusplus);
14395 need_lang_pop = true;
14397 else
14398 need_lang_pop = false;
14399 /* Let the front end know that we are beginning a specialization. */
14400 if (!begin_specialization ())
14402 end_specialization ();
14403 return;
14406 /* If the next keyword is `template', we need to figure out whether
14407 or not we're looking a template-declaration. */
14408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14410 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14411 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
14412 cp_parser_template_declaration_after_export (parser,
14413 /*member_p=*/false);
14414 else
14415 cp_parser_explicit_specialization (parser);
14417 else
14418 /* Parse the dependent declaration. */
14419 cp_parser_single_declaration (parser,
14420 /*checks=*/NULL,
14421 /*member_p=*/false,
14422 /*explicit_specialization_p=*/true,
14423 /*friend_p=*/NULL);
14424 /* We're done with the specialization. */
14425 end_specialization ();
14426 /* For the erroneous case of a template with C linkage, we pushed an
14427 implicit C++ linkage scope; exit that scope now. */
14428 if (need_lang_pop)
14429 pop_lang_context ();
14430 /* We're done with this parameter list. */
14431 --parser->num_template_parameter_lists;
14434 /* Parse a type-specifier.
14436 type-specifier:
14437 simple-type-specifier
14438 class-specifier
14439 enum-specifier
14440 elaborated-type-specifier
14441 cv-qualifier
14443 GNU Extension:
14445 type-specifier:
14446 __complex__
14448 Returns a representation of the type-specifier. For a
14449 class-specifier, enum-specifier, or elaborated-type-specifier, a
14450 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
14452 The parser flags FLAGS is used to control type-specifier parsing.
14454 If IS_DECLARATION is TRUE, then this type-specifier is appearing
14455 in a decl-specifier-seq.
14457 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
14458 class-specifier, enum-specifier, or elaborated-type-specifier, then
14459 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
14460 if a type is declared; 2 if it is defined. Otherwise, it is set to
14461 zero.
14463 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
14464 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
14465 is set to FALSE. */
14467 static tree
14468 cp_parser_type_specifier (cp_parser* parser,
14469 cp_parser_flags flags,
14470 cp_decl_specifier_seq *decl_specs,
14471 bool is_declaration,
14472 int* declares_class_or_enum,
14473 bool* is_cv_qualifier)
14475 tree type_spec = NULL_TREE;
14476 cp_token *token;
14477 enum rid keyword;
14478 cp_decl_spec ds = ds_last;
14480 /* Assume this type-specifier does not declare a new type. */
14481 if (declares_class_or_enum)
14482 *declares_class_or_enum = 0;
14483 /* And that it does not specify a cv-qualifier. */
14484 if (is_cv_qualifier)
14485 *is_cv_qualifier = false;
14486 /* Peek at the next token. */
14487 token = cp_lexer_peek_token (parser->lexer);
14489 /* If we're looking at a keyword, we can use that to guide the
14490 production we choose. */
14491 keyword = token->keyword;
14492 switch (keyword)
14494 case RID_ENUM:
14495 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14496 goto elaborated_type_specifier;
14498 /* Look for the enum-specifier. */
14499 type_spec = cp_parser_enum_specifier (parser);
14500 /* If that worked, we're done. */
14501 if (type_spec)
14503 if (declares_class_or_enum)
14504 *declares_class_or_enum = 2;
14505 if (decl_specs)
14506 cp_parser_set_decl_spec_type (decl_specs,
14507 type_spec,
14508 token,
14509 /*type_definition_p=*/true);
14510 return type_spec;
14512 else
14513 goto elaborated_type_specifier;
14515 /* Any of these indicate either a class-specifier, or an
14516 elaborated-type-specifier. */
14517 case RID_CLASS:
14518 case RID_STRUCT:
14519 case RID_UNION:
14520 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
14521 goto elaborated_type_specifier;
14523 /* Parse tentatively so that we can back up if we don't find a
14524 class-specifier. */
14525 cp_parser_parse_tentatively (parser);
14526 /* Look for the class-specifier. */
14527 type_spec = cp_parser_class_specifier (parser);
14528 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
14529 /* If that worked, we're done. */
14530 if (cp_parser_parse_definitely (parser))
14532 if (declares_class_or_enum)
14533 *declares_class_or_enum = 2;
14534 if (decl_specs)
14535 cp_parser_set_decl_spec_type (decl_specs,
14536 type_spec,
14537 token,
14538 /*type_definition_p=*/true);
14539 return type_spec;
14542 /* Fall through. */
14543 elaborated_type_specifier:
14544 /* We're declaring (not defining) a class or enum. */
14545 if (declares_class_or_enum)
14546 *declares_class_or_enum = 1;
14548 /* Fall through. */
14549 case RID_TYPENAME:
14550 /* Look for an elaborated-type-specifier. */
14551 type_spec
14552 = (cp_parser_elaborated_type_specifier
14553 (parser,
14554 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
14555 is_declaration));
14556 if (decl_specs)
14557 cp_parser_set_decl_spec_type (decl_specs,
14558 type_spec,
14559 token,
14560 /*type_definition_p=*/false);
14561 return type_spec;
14563 case RID_CONST:
14564 ds = ds_const;
14565 if (is_cv_qualifier)
14566 *is_cv_qualifier = true;
14567 break;
14569 case RID_VOLATILE:
14570 ds = ds_volatile;
14571 if (is_cv_qualifier)
14572 *is_cv_qualifier = true;
14573 break;
14575 case RID_RESTRICT:
14576 ds = ds_restrict;
14577 if (is_cv_qualifier)
14578 *is_cv_qualifier = true;
14579 break;
14581 case RID_COMPLEX:
14582 /* The `__complex__' keyword is a GNU extension. */
14583 ds = ds_complex;
14584 break;
14586 default:
14587 break;
14590 /* Handle simple keywords. */
14591 if (ds != ds_last)
14593 if (decl_specs)
14595 set_and_check_decl_spec_loc (decl_specs, ds, token);
14596 decl_specs->any_specifiers_p = true;
14598 return cp_lexer_consume_token (parser->lexer)->u.value;
14601 /* If we do not already have a type-specifier, assume we are looking
14602 at a simple-type-specifier. */
14603 type_spec = cp_parser_simple_type_specifier (parser,
14604 decl_specs,
14605 flags);
14607 /* If we didn't find a type-specifier, and a type-specifier was not
14608 optional in this context, issue an error message. */
14609 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14611 cp_parser_error (parser, "expected type specifier");
14612 return error_mark_node;
14615 return type_spec;
14618 /* Parse a simple-type-specifier.
14620 simple-type-specifier:
14621 :: [opt] nested-name-specifier [opt] type-name
14622 :: [opt] nested-name-specifier template template-id
14623 char
14624 wchar_t
14625 bool
14626 short
14628 long
14629 signed
14630 unsigned
14631 float
14632 double
14633 void
14635 C++0x Extension:
14637 simple-type-specifier:
14638 auto
14639 decltype ( expression )
14640 char16_t
14641 char32_t
14642 __underlying_type ( type-id )
14644 GNU Extension:
14646 simple-type-specifier:
14647 __int128
14648 __typeof__ unary-expression
14649 __typeof__ ( type-id )
14650 __typeof__ ( type-id ) { initializer-list , [opt] }
14652 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
14653 appropriately updated. */
14655 static tree
14656 cp_parser_simple_type_specifier (cp_parser* parser,
14657 cp_decl_specifier_seq *decl_specs,
14658 cp_parser_flags flags)
14660 tree type = NULL_TREE;
14661 cp_token *token;
14663 /* Peek at the next token. */
14664 token = cp_lexer_peek_token (parser->lexer);
14666 /* If we're looking at a keyword, things are easy. */
14667 switch (token->keyword)
14669 case RID_CHAR:
14670 if (decl_specs)
14671 decl_specs->explicit_char_p = true;
14672 type = char_type_node;
14673 break;
14674 case RID_CHAR16:
14675 type = char16_type_node;
14676 break;
14677 case RID_CHAR32:
14678 type = char32_type_node;
14679 break;
14680 case RID_WCHAR:
14681 type = wchar_type_node;
14682 break;
14683 case RID_BOOL:
14684 type = boolean_type_node;
14685 break;
14686 case RID_SHORT:
14687 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
14688 type = short_integer_type_node;
14689 break;
14690 case RID_INT:
14691 if (decl_specs)
14692 decl_specs->explicit_int_p = true;
14693 type = integer_type_node;
14694 break;
14695 case RID_INT128:
14696 if (!int128_integer_type_node)
14697 break;
14698 if (decl_specs)
14699 decl_specs->explicit_int128_p = true;
14700 type = int128_integer_type_node;
14701 break;
14702 case RID_LONG:
14703 if (decl_specs)
14704 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
14705 type = long_integer_type_node;
14706 break;
14707 case RID_SIGNED:
14708 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
14709 type = integer_type_node;
14710 break;
14711 case RID_UNSIGNED:
14712 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
14713 type = unsigned_type_node;
14714 break;
14715 case RID_FLOAT:
14716 type = float_type_node;
14717 break;
14718 case RID_DOUBLE:
14719 type = double_type_node;
14720 break;
14721 case RID_VOID:
14722 type = void_type_node;
14723 break;
14725 case RID_AUTO:
14726 maybe_warn_cpp0x (CPP0X_AUTO);
14727 if (parser->auto_is_implicit_function_template_parm_p)
14729 type = synthesize_implicit_template_parm (parser);
14731 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
14733 if (cxx_dialect < cxx14)
14734 pedwarn (location_of (type), 0,
14735 "use of %<auto%> in lambda parameter declaration "
14736 "only available with "
14737 "-std=c++14 or -std=gnu++14");
14739 else if (cxx_dialect < cxx14)
14740 pedwarn (location_of (type), 0,
14741 "use of %<auto%> in parameter declaration "
14742 "only available with "
14743 "-std=c++14 or -std=gnu++14");
14744 else
14745 pedwarn (location_of (type), OPT_Wpedantic,
14746 "ISO C++ forbids use of %<auto%> in parameter "
14747 "declaration");
14749 else
14750 type = make_auto ();
14751 break;
14753 case RID_DECLTYPE:
14754 /* Since DR 743, decltype can either be a simple-type-specifier by
14755 itself or begin a nested-name-specifier. Parsing it will replace
14756 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
14757 handling below decide what to do. */
14758 cp_parser_decltype (parser);
14759 cp_lexer_set_token_position (parser->lexer, token);
14760 break;
14762 case RID_TYPEOF:
14763 /* Consume the `typeof' token. */
14764 cp_lexer_consume_token (parser->lexer);
14765 /* Parse the operand to `typeof'. */
14766 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
14767 /* If it is not already a TYPE, take its type. */
14768 if (!TYPE_P (type))
14769 type = finish_typeof (type);
14771 if (decl_specs)
14772 cp_parser_set_decl_spec_type (decl_specs, type,
14773 token,
14774 /*type_definition_p=*/false);
14776 return type;
14778 case RID_UNDERLYING_TYPE:
14779 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
14780 if (decl_specs)
14781 cp_parser_set_decl_spec_type (decl_specs, type,
14782 token,
14783 /*type_definition_p=*/false);
14785 return type;
14787 case RID_BASES:
14788 case RID_DIRECT_BASES:
14789 type = cp_parser_trait_expr (parser, token->keyword);
14790 if (decl_specs)
14791 cp_parser_set_decl_spec_type (decl_specs, type,
14792 token,
14793 /*type_definition_p=*/false);
14794 return type;
14795 default:
14796 break;
14799 /* If token is an already-parsed decltype not followed by ::,
14800 it's a simple-type-specifier. */
14801 if (token->type == CPP_DECLTYPE
14802 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
14804 type = token->u.value;
14805 if (decl_specs)
14806 cp_parser_set_decl_spec_type (decl_specs, type,
14807 token,
14808 /*type_definition_p=*/false);
14809 cp_lexer_consume_token (parser->lexer);
14810 return type;
14813 /* If the type-specifier was for a built-in type, we're done. */
14814 if (type)
14816 /* Record the type. */
14817 if (decl_specs
14818 && (token->keyword != RID_SIGNED
14819 && token->keyword != RID_UNSIGNED
14820 && token->keyword != RID_SHORT
14821 && token->keyword != RID_LONG))
14822 cp_parser_set_decl_spec_type (decl_specs,
14823 type,
14824 token,
14825 /*type_definition_p=*/false);
14826 if (decl_specs)
14827 decl_specs->any_specifiers_p = true;
14829 /* Consume the token. */
14830 cp_lexer_consume_token (parser->lexer);
14832 /* There is no valid C++ program where a non-template type is
14833 followed by a "<". That usually indicates that the user thought
14834 that the type was a template. */
14835 cp_parser_check_for_invalid_template_id (parser, type, none_type,
14836 token->location);
14838 return TYPE_NAME (type);
14841 /* The type-specifier must be a user-defined type. */
14842 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
14844 bool qualified_p;
14845 bool global_p;
14847 /* Don't gobble tokens or issue error messages if this is an
14848 optional type-specifier. */
14849 if (flags & CP_PARSER_FLAGS_OPTIONAL)
14850 cp_parser_parse_tentatively (parser);
14852 /* Look for the optional `::' operator. */
14853 global_p
14854 = (cp_parser_global_scope_opt (parser,
14855 /*current_scope_valid_p=*/false)
14856 != NULL_TREE);
14857 /* Look for the nested-name specifier. */
14858 qualified_p
14859 = (cp_parser_nested_name_specifier_opt (parser,
14860 /*typename_keyword_p=*/false,
14861 /*check_dependency_p=*/true,
14862 /*type_p=*/false,
14863 /*is_declaration=*/false)
14864 != NULL_TREE);
14865 token = cp_lexer_peek_token (parser->lexer);
14866 /* If we have seen a nested-name-specifier, and the next token
14867 is `template', then we are using the template-id production. */
14868 if (parser->scope
14869 && cp_parser_optional_template_keyword (parser))
14871 /* Look for the template-id. */
14872 type = cp_parser_template_id (parser,
14873 /*template_keyword_p=*/true,
14874 /*check_dependency_p=*/true,
14875 none_type,
14876 /*is_declaration=*/false);
14877 /* If the template-id did not name a type, we are out of
14878 luck. */
14879 if (TREE_CODE (type) != TYPE_DECL)
14881 cp_parser_error (parser, "expected template-id for type");
14882 type = NULL_TREE;
14885 /* Otherwise, look for a type-name. */
14886 else
14887 type = cp_parser_type_name (parser);
14888 /* Keep track of all name-lookups performed in class scopes. */
14889 if (type
14890 && !global_p
14891 && !qualified_p
14892 && TREE_CODE (type) == TYPE_DECL
14893 && identifier_p (DECL_NAME (type)))
14894 maybe_note_name_used_in_class (DECL_NAME (type), type);
14895 /* If it didn't work out, we don't have a TYPE. */
14896 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
14897 && !cp_parser_parse_definitely (parser))
14898 type = NULL_TREE;
14899 if (type && decl_specs)
14900 cp_parser_set_decl_spec_type (decl_specs, type,
14901 token,
14902 /*type_definition_p=*/false);
14905 /* If we didn't get a type-name, issue an error message. */
14906 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
14908 cp_parser_error (parser, "expected type-name");
14909 return error_mark_node;
14912 if (type && type != error_mark_node)
14914 /* See if TYPE is an Objective-C type, and if so, parse and
14915 accept any protocol references following it. Do this before
14916 the cp_parser_check_for_invalid_template_id() call, because
14917 Objective-C types can be followed by '<...>' which would
14918 enclose protocol names rather than template arguments, and so
14919 everything is fine. */
14920 if (c_dialect_objc () && !parser->scope
14921 && (objc_is_id (type) || objc_is_class_name (type)))
14923 tree protos = cp_parser_objc_protocol_refs_opt (parser);
14924 tree qual_type = objc_get_protocol_qualified_type (type, protos);
14926 /* Clobber the "unqualified" type previously entered into
14927 DECL_SPECS with the new, improved protocol-qualified version. */
14928 if (decl_specs)
14929 decl_specs->type = qual_type;
14931 return qual_type;
14934 /* There is no valid C++ program where a non-template type is
14935 followed by a "<". That usually indicates that the user
14936 thought that the type was a template. */
14937 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
14938 none_type,
14939 token->location);
14942 return type;
14945 /* Parse a type-name.
14947 type-name:
14948 class-name
14949 enum-name
14950 typedef-name
14951 simple-template-id [in c++0x]
14953 enum-name:
14954 identifier
14956 typedef-name:
14957 identifier
14959 Returns a TYPE_DECL for the type. */
14961 static tree
14962 cp_parser_type_name (cp_parser* parser)
14964 tree type_decl;
14966 /* We can't know yet whether it is a class-name or not. */
14967 cp_parser_parse_tentatively (parser);
14968 /* Try a class-name. */
14969 type_decl = cp_parser_class_name (parser,
14970 /*typename_keyword_p=*/false,
14971 /*template_keyword_p=*/false,
14972 none_type,
14973 /*check_dependency_p=*/true,
14974 /*class_head_p=*/false,
14975 /*is_declaration=*/false);
14976 /* If it's not a class-name, keep looking. */
14977 if (!cp_parser_parse_definitely (parser))
14979 if (cxx_dialect < cxx11)
14980 /* It must be a typedef-name or an enum-name. */
14981 return cp_parser_nonclass_name (parser);
14983 cp_parser_parse_tentatively (parser);
14984 /* It is either a simple-template-id representing an
14985 instantiation of an alias template... */
14986 type_decl = cp_parser_template_id (parser,
14987 /*template_keyword_p=*/false,
14988 /*check_dependency_p=*/true,
14989 none_type,
14990 /*is_declaration=*/false);
14991 /* Note that this must be an instantiation of an alias template
14992 because [temp.names]/6 says:
14994 A template-id that names an alias template specialization
14995 is a type-name.
14997 Whereas [temp.names]/7 says:
14999 A simple-template-id that names a class template
15000 specialization is a class-name. */
15001 if (type_decl != NULL_TREE
15002 && TREE_CODE (type_decl) == TYPE_DECL
15003 && TYPE_DECL_ALIAS_P (type_decl))
15004 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
15005 else
15006 cp_parser_simulate_error (parser);
15008 if (!cp_parser_parse_definitely (parser))
15009 /* ... Or a typedef-name or an enum-name. */
15010 return cp_parser_nonclass_name (parser);
15013 return type_decl;
15016 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
15018 enum-name:
15019 identifier
15021 typedef-name:
15022 identifier
15024 Returns a TYPE_DECL for the type. */
15026 static tree
15027 cp_parser_nonclass_name (cp_parser* parser)
15029 tree type_decl;
15030 tree identifier;
15032 cp_token *token = cp_lexer_peek_token (parser->lexer);
15033 identifier = cp_parser_identifier (parser);
15034 if (identifier == error_mark_node)
15035 return error_mark_node;
15037 /* Look up the type-name. */
15038 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
15040 type_decl = strip_using_decl (type_decl);
15042 if (TREE_CODE (type_decl) != TYPE_DECL
15043 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
15045 /* See if this is an Objective-C type. */
15046 tree protos = cp_parser_objc_protocol_refs_opt (parser);
15047 tree type = objc_get_protocol_qualified_type (identifier, protos);
15048 if (type)
15049 type_decl = TYPE_NAME (type);
15052 /* Issue an error if we did not find a type-name. */
15053 if (TREE_CODE (type_decl) != TYPE_DECL
15054 /* In Objective-C, we have the complication that class names are
15055 normally type names and start declarations (eg, the
15056 "NSObject" in "NSObject *object;"), but can be used in an
15057 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
15058 is an expression. So, a classname followed by a dot is not a
15059 valid type-name. */
15060 || (objc_is_class_name (TREE_TYPE (type_decl))
15061 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
15063 if (!cp_parser_simulate_error (parser))
15064 cp_parser_name_lookup_error (parser, identifier, type_decl,
15065 NLE_TYPE, token->location);
15066 return error_mark_node;
15068 /* Remember that the name was used in the definition of the
15069 current class so that we can check later to see if the
15070 meaning would have been different after the class was
15071 entirely defined. */
15072 else if (type_decl != error_mark_node
15073 && !parser->scope)
15074 maybe_note_name_used_in_class (identifier, type_decl);
15076 return type_decl;
15079 /* Parse an elaborated-type-specifier. Note that the grammar given
15080 here incorporates the resolution to DR68.
15082 elaborated-type-specifier:
15083 class-key :: [opt] nested-name-specifier [opt] identifier
15084 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
15085 enum-key :: [opt] nested-name-specifier [opt] identifier
15086 typename :: [opt] nested-name-specifier identifier
15087 typename :: [opt] nested-name-specifier template [opt]
15088 template-id
15090 GNU extension:
15092 elaborated-type-specifier:
15093 class-key attributes :: [opt] nested-name-specifier [opt] identifier
15094 class-key attributes :: [opt] nested-name-specifier [opt]
15095 template [opt] template-id
15096 enum attributes :: [opt] nested-name-specifier [opt] identifier
15098 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
15099 declared `friend'. If IS_DECLARATION is TRUE, then this
15100 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
15101 something is being declared.
15103 Returns the TYPE specified. */
15105 static tree
15106 cp_parser_elaborated_type_specifier (cp_parser* parser,
15107 bool is_friend,
15108 bool is_declaration)
15110 enum tag_types tag_type;
15111 tree identifier;
15112 tree type = NULL_TREE;
15113 tree attributes = NULL_TREE;
15114 tree globalscope;
15115 cp_token *token = NULL;
15117 /* See if we're looking at the `enum' keyword. */
15118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
15120 /* Consume the `enum' token. */
15121 cp_lexer_consume_token (parser->lexer);
15122 /* Remember that it's an enumeration type. */
15123 tag_type = enum_type;
15124 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
15125 enums) is used here. */
15126 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15127 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15129 pedwarn (input_location, 0, "elaborated-type-specifier "
15130 "for a scoped enum must not use the %<%D%> keyword",
15131 cp_lexer_peek_token (parser->lexer)->u.value);
15132 /* Consume the `struct' or `class' and parse it anyway. */
15133 cp_lexer_consume_token (parser->lexer);
15135 /* Parse the attributes. */
15136 attributes = cp_parser_attributes_opt (parser);
15138 /* Or, it might be `typename'. */
15139 else if (cp_lexer_next_token_is_keyword (parser->lexer,
15140 RID_TYPENAME))
15142 /* Consume the `typename' token. */
15143 cp_lexer_consume_token (parser->lexer);
15144 /* Remember that it's a `typename' type. */
15145 tag_type = typename_type;
15147 /* Otherwise it must be a class-key. */
15148 else
15150 tag_type = cp_parser_class_key (parser);
15151 if (tag_type == none_type)
15152 return error_mark_node;
15153 /* Parse the attributes. */
15154 attributes = cp_parser_attributes_opt (parser);
15157 /* Look for the `::' operator. */
15158 globalscope = cp_parser_global_scope_opt (parser,
15159 /*current_scope_valid_p=*/false);
15160 /* Look for the nested-name-specifier. */
15161 if (tag_type == typename_type && !globalscope)
15163 if (!cp_parser_nested_name_specifier (parser,
15164 /*typename_keyword_p=*/true,
15165 /*check_dependency_p=*/true,
15166 /*type_p=*/true,
15167 is_declaration))
15168 return error_mark_node;
15170 else
15171 /* Even though `typename' is not present, the proposed resolution
15172 to Core Issue 180 says that in `class A<T>::B', `B' should be
15173 considered a type-name, even if `A<T>' is dependent. */
15174 cp_parser_nested_name_specifier_opt (parser,
15175 /*typename_keyword_p=*/true,
15176 /*check_dependency_p=*/true,
15177 /*type_p=*/true,
15178 is_declaration);
15179 /* For everything but enumeration types, consider a template-id.
15180 For an enumeration type, consider only a plain identifier. */
15181 if (tag_type != enum_type)
15183 bool template_p = false;
15184 tree decl;
15186 /* Allow the `template' keyword. */
15187 template_p = cp_parser_optional_template_keyword (parser);
15188 /* If we didn't see `template', we don't know if there's a
15189 template-id or not. */
15190 if (!template_p)
15191 cp_parser_parse_tentatively (parser);
15192 /* Parse the template-id. */
15193 token = cp_lexer_peek_token (parser->lexer);
15194 decl = cp_parser_template_id (parser, template_p,
15195 /*check_dependency_p=*/true,
15196 tag_type,
15197 is_declaration);
15198 /* If we didn't find a template-id, look for an ordinary
15199 identifier. */
15200 if (!template_p && !cp_parser_parse_definitely (parser))
15202 /* We can get here when cp_parser_template_id, called by
15203 cp_parser_class_name with tag_type == none_type, succeeds
15204 and caches a BASELINK. Then, when called again here,
15205 instead of failing and returning an error_mark_node
15206 returns it (see template/typename17.C in C++11).
15207 ??? Could we diagnose this earlier? */
15208 else if (tag_type == typename_type && BASELINK_P (decl))
15210 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
15211 type = error_mark_node;
15213 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
15214 in effect, then we must assume that, upon instantiation, the
15215 template will correspond to a class. */
15216 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15217 && tag_type == typename_type)
15218 type = make_typename_type (parser->scope, decl,
15219 typename_type,
15220 /*complain=*/tf_error);
15221 /* If the `typename' keyword is in effect and DECL is not a type
15222 decl, then type is non existent. */
15223 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
15225 else if (TREE_CODE (decl) == TYPE_DECL)
15226 type = check_elaborated_type_specifier (tag_type, decl,
15227 /*allow_template_p=*/true);
15228 else if (decl == error_mark_node)
15229 type = error_mark_node;
15232 if (!type)
15234 token = cp_lexer_peek_token (parser->lexer);
15235 identifier = cp_parser_identifier (parser);
15237 if (identifier == error_mark_node)
15239 parser->scope = NULL_TREE;
15240 return error_mark_node;
15243 /* For a `typename', we needn't call xref_tag. */
15244 if (tag_type == typename_type
15245 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
15246 return cp_parser_make_typename_type (parser, identifier,
15247 token->location);
15249 /* Template parameter lists apply only if we are not within a
15250 function parameter list. */
15251 bool template_parm_lists_apply
15252 = parser->num_template_parameter_lists;
15253 if (template_parm_lists_apply)
15254 for (cp_binding_level *s = current_binding_level;
15255 s && s->kind != sk_template_parms;
15256 s = s->level_chain)
15257 if (s->kind == sk_function_parms)
15258 template_parm_lists_apply = false;
15260 /* Look up a qualified name in the usual way. */
15261 if (parser->scope)
15263 tree decl;
15264 tree ambiguous_decls;
15266 decl = cp_parser_lookup_name (parser, identifier,
15267 tag_type,
15268 /*is_template=*/false,
15269 /*is_namespace=*/false,
15270 /*check_dependency=*/true,
15271 &ambiguous_decls,
15272 token->location);
15274 /* If the lookup was ambiguous, an error will already have been
15275 issued. */
15276 if (ambiguous_decls)
15277 return error_mark_node;
15279 /* If we are parsing friend declaration, DECL may be a
15280 TEMPLATE_DECL tree node here. However, we need to check
15281 whether this TEMPLATE_DECL results in valid code. Consider
15282 the following example:
15284 namespace N {
15285 template <class T> class C {};
15287 class X {
15288 template <class T> friend class N::C; // #1, valid code
15290 template <class T> class Y {
15291 friend class N::C; // #2, invalid code
15294 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
15295 name lookup of `N::C'. We see that friend declaration must
15296 be template for the code to be valid. Note that
15297 processing_template_decl does not work here since it is
15298 always 1 for the above two cases. */
15300 decl = (cp_parser_maybe_treat_template_as_class
15301 (decl, /*tag_name_p=*/is_friend
15302 && template_parm_lists_apply));
15304 if (TREE_CODE (decl) != TYPE_DECL)
15306 cp_parser_diagnose_invalid_type_name (parser,
15307 identifier,
15308 token->location);
15309 return error_mark_node;
15312 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
15314 bool allow_template = (template_parm_lists_apply
15315 || DECL_SELF_REFERENCE_P (decl));
15316 type = check_elaborated_type_specifier (tag_type, decl,
15317 allow_template);
15319 if (type == error_mark_node)
15320 return error_mark_node;
15323 /* Forward declarations of nested types, such as
15325 class C1::C2;
15326 class C1::C2::C3;
15328 are invalid unless all components preceding the final '::'
15329 are complete. If all enclosing types are complete, these
15330 declarations become merely pointless.
15332 Invalid forward declarations of nested types are errors
15333 caught elsewhere in parsing. Those that are pointless arrive
15334 here. */
15336 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15337 && !is_friend && !processing_explicit_instantiation)
15338 warning (0, "declaration %qD does not declare anything", decl);
15340 type = TREE_TYPE (decl);
15342 else
15344 /* An elaborated-type-specifier sometimes introduces a new type and
15345 sometimes names an existing type. Normally, the rule is that it
15346 introduces a new type only if there is not an existing type of
15347 the same name already in scope. For example, given:
15349 struct S {};
15350 void f() { struct S s; }
15352 the `struct S' in the body of `f' is the same `struct S' as in
15353 the global scope; the existing definition is used. However, if
15354 there were no global declaration, this would introduce a new
15355 local class named `S'.
15357 An exception to this rule applies to the following code:
15359 namespace N { struct S; }
15361 Here, the elaborated-type-specifier names a new type
15362 unconditionally; even if there is already an `S' in the
15363 containing scope this declaration names a new type.
15364 This exception only applies if the elaborated-type-specifier
15365 forms the complete declaration:
15367 [class.name]
15369 A declaration consisting solely of `class-key identifier ;' is
15370 either a redeclaration of the name in the current scope or a
15371 forward declaration of the identifier as a class name. It
15372 introduces the name into the current scope.
15374 We are in this situation precisely when the next token is a `;'.
15376 An exception to the exception is that a `friend' declaration does
15377 *not* name a new type; i.e., given:
15379 struct S { friend struct T; };
15381 `T' is not a new type in the scope of `S'.
15383 Also, `new struct S' or `sizeof (struct S)' never results in the
15384 definition of a new type; a new type can only be declared in a
15385 declaration context. */
15387 tag_scope ts;
15388 bool template_p;
15390 if (is_friend)
15391 /* Friends have special name lookup rules. */
15392 ts = ts_within_enclosing_non_class;
15393 else if (is_declaration
15394 && cp_lexer_next_token_is (parser->lexer,
15395 CPP_SEMICOLON))
15396 /* This is a `class-key identifier ;' */
15397 ts = ts_current;
15398 else
15399 ts = ts_global;
15401 template_p =
15402 (template_parm_lists_apply
15403 && (cp_parser_next_token_starts_class_definition_p (parser)
15404 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
15405 /* An unqualified name was used to reference this type, so
15406 there were no qualifying templates. */
15407 if (template_parm_lists_apply
15408 && !cp_parser_check_template_parameters (parser,
15409 /*num_templates=*/0,
15410 token->location,
15411 /*declarator=*/NULL))
15412 return error_mark_node;
15413 type = xref_tag (tag_type, identifier, ts, template_p);
15417 if (type == error_mark_node)
15418 return error_mark_node;
15420 /* Allow attributes on forward declarations of classes. */
15421 if (attributes)
15423 if (TREE_CODE (type) == TYPENAME_TYPE)
15424 warning (OPT_Wattributes,
15425 "attributes ignored on uninstantiated type");
15426 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
15427 && ! processing_explicit_instantiation)
15428 warning (OPT_Wattributes,
15429 "attributes ignored on template instantiation");
15430 else if (is_declaration && cp_parser_declares_only_class_p (parser))
15431 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
15432 else
15433 warning (OPT_Wattributes,
15434 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
15437 if (tag_type != enum_type)
15439 /* Indicate whether this class was declared as a `class' or as a
15440 `struct'. */
15441 if (TREE_CODE (type) == RECORD_TYPE)
15442 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
15443 cp_parser_check_class_key (tag_type, type);
15446 /* A "<" cannot follow an elaborated type specifier. If that
15447 happens, the user was probably trying to form a template-id. */
15448 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
15449 token->location);
15451 return type;
15454 /* Parse an enum-specifier.
15456 enum-specifier:
15457 enum-head { enumerator-list [opt] }
15458 enum-head { enumerator-list , } [C++0x]
15460 enum-head:
15461 enum-key identifier [opt] enum-base [opt]
15462 enum-key nested-name-specifier identifier enum-base [opt]
15464 enum-key:
15465 enum
15466 enum class [C++0x]
15467 enum struct [C++0x]
15469 enum-base: [C++0x]
15470 : type-specifier-seq
15472 opaque-enum-specifier:
15473 enum-key identifier enum-base [opt] ;
15475 GNU Extensions:
15476 enum-key attributes[opt] identifier [opt] enum-base [opt]
15477 { enumerator-list [opt] }attributes[opt]
15478 enum-key attributes[opt] identifier [opt] enum-base [opt]
15479 { enumerator-list, }attributes[opt] [C++0x]
15481 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
15482 if the token stream isn't an enum-specifier after all. */
15484 static tree
15485 cp_parser_enum_specifier (cp_parser* parser)
15487 tree identifier;
15488 tree type = NULL_TREE;
15489 tree prev_scope;
15490 tree nested_name_specifier = NULL_TREE;
15491 tree attributes;
15492 bool scoped_enum_p = false;
15493 bool has_underlying_type = false;
15494 bool nested_being_defined = false;
15495 bool new_value_list = false;
15496 bool is_new_type = false;
15497 bool is_anonymous = false;
15498 tree underlying_type = NULL_TREE;
15499 cp_token *type_start_token = NULL;
15500 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
15502 parser->colon_corrects_to_scope_p = false;
15504 /* Parse tentatively so that we can back up if we don't find a
15505 enum-specifier. */
15506 cp_parser_parse_tentatively (parser);
15508 /* Caller guarantees that the current token is 'enum', an identifier
15509 possibly follows, and the token after that is an opening brace.
15510 If we don't have an identifier, fabricate an anonymous name for
15511 the enumeration being defined. */
15512 cp_lexer_consume_token (parser->lexer);
15514 /* Parse the "class" or "struct", which indicates a scoped
15515 enumeration type in C++0x. */
15516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
15517 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
15519 if (cxx_dialect < cxx11)
15520 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15522 /* Consume the `struct' or `class' token. */
15523 cp_lexer_consume_token (parser->lexer);
15525 scoped_enum_p = true;
15528 attributes = cp_parser_attributes_opt (parser);
15530 /* Clear the qualification. */
15531 parser->scope = NULL_TREE;
15532 parser->qualifying_scope = NULL_TREE;
15533 parser->object_scope = NULL_TREE;
15535 /* Figure out in what scope the declaration is being placed. */
15536 prev_scope = current_scope ();
15538 type_start_token = cp_lexer_peek_token (parser->lexer);
15540 push_deferring_access_checks (dk_no_check);
15541 nested_name_specifier
15542 = cp_parser_nested_name_specifier_opt (parser,
15543 /*typename_keyword_p=*/true,
15544 /*check_dependency_p=*/false,
15545 /*type_p=*/false,
15546 /*is_declaration=*/false);
15548 if (nested_name_specifier)
15550 tree name;
15552 identifier = cp_parser_identifier (parser);
15553 name = cp_parser_lookup_name (parser, identifier,
15554 enum_type,
15555 /*is_template=*/false,
15556 /*is_namespace=*/false,
15557 /*check_dependency=*/true,
15558 /*ambiguous_decls=*/NULL,
15559 input_location);
15560 if (name && name != error_mark_node)
15562 type = TREE_TYPE (name);
15563 if (TREE_CODE (type) == TYPENAME_TYPE)
15565 /* Are template enums allowed in ISO? */
15566 if (template_parm_scope_p ())
15567 pedwarn (type_start_token->location, OPT_Wpedantic,
15568 "%qD is an enumeration template", name);
15569 /* ignore a typename reference, for it will be solved by name
15570 in start_enum. */
15571 type = NULL_TREE;
15574 else if (nested_name_specifier == error_mark_node)
15575 /* We already issued an error. */;
15576 else
15577 error_at (type_start_token->location,
15578 "%qD is not an enumerator-name", identifier);
15580 else
15582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15583 identifier = cp_parser_identifier (parser);
15584 else
15586 identifier = make_anon_name ();
15587 is_anonymous = true;
15588 if (scoped_enum_p)
15589 error_at (type_start_token->location,
15590 "anonymous scoped enum is not allowed");
15593 pop_deferring_access_checks ();
15595 /* Check for the `:' that denotes a specified underlying type in C++0x.
15596 Note that a ':' could also indicate a bitfield width, however. */
15597 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15599 cp_decl_specifier_seq type_specifiers;
15601 /* Consume the `:'. */
15602 cp_lexer_consume_token (parser->lexer);
15604 /* Parse the type-specifier-seq. */
15605 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15606 /*is_trailing_return=*/false,
15607 &type_specifiers);
15609 /* At this point this is surely not elaborated type specifier. */
15610 if (!cp_parser_parse_definitely (parser))
15611 return NULL_TREE;
15613 if (cxx_dialect < cxx11)
15614 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
15616 has_underlying_type = true;
15618 /* If that didn't work, stop. */
15619 if (type_specifiers.type != error_mark_node)
15621 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
15622 /*initialized=*/0, NULL);
15623 if (underlying_type == error_mark_node
15624 || check_for_bare_parameter_packs (underlying_type))
15625 underlying_type = NULL_TREE;
15629 /* Look for the `{' but don't consume it yet. */
15630 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15632 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
15634 cp_parser_error (parser, "expected %<{%>");
15635 if (has_underlying_type)
15637 type = NULL_TREE;
15638 goto out;
15641 /* An opaque-enum-specifier must have a ';' here. */
15642 if ((scoped_enum_p || underlying_type)
15643 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15645 cp_parser_error (parser, "expected %<;%> or %<{%>");
15646 if (has_underlying_type)
15648 type = NULL_TREE;
15649 goto out;
15654 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
15655 return NULL_TREE;
15657 if (nested_name_specifier)
15659 if (CLASS_TYPE_P (nested_name_specifier))
15661 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
15662 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
15663 push_scope (nested_name_specifier);
15665 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15667 push_nested_namespace (nested_name_specifier);
15671 /* Issue an error message if type-definitions are forbidden here. */
15672 if (!cp_parser_check_type_definition (parser))
15673 type = error_mark_node;
15674 else
15675 /* Create the new type. We do this before consuming the opening
15676 brace so the enum will be recorded as being on the line of its
15677 tag (or the 'enum' keyword, if there is no tag). */
15678 type = start_enum (identifier, type, underlying_type,
15679 scoped_enum_p, &is_new_type);
15681 /* If the next token is not '{' it is an opaque-enum-specifier or an
15682 elaborated-type-specifier. */
15683 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15685 timevar_push (TV_PARSE_ENUM);
15686 if (nested_name_specifier
15687 && nested_name_specifier != error_mark_node)
15689 /* The following catches invalid code such as:
15690 enum class S<int>::E { A, B, C }; */
15691 if (!processing_specialization
15692 && CLASS_TYPE_P (nested_name_specifier)
15693 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
15694 error_at (type_start_token->location, "cannot add an enumerator "
15695 "list to a template instantiation");
15697 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
15699 error_at (type_start_token->location,
15700 "%<%T::%E%> has not been declared",
15701 TYPE_CONTEXT (nested_name_specifier),
15702 nested_name_specifier);
15703 type = error_mark_node;
15705 /* If that scope does not contain the scope in which the
15706 class was originally declared, the program is invalid. */
15707 else if (prev_scope && !is_ancestor (prev_scope,
15708 nested_name_specifier))
15710 if (at_namespace_scope_p ())
15711 error_at (type_start_token->location,
15712 "declaration of %qD in namespace %qD which does not "
15713 "enclose %qD",
15714 type, prev_scope, nested_name_specifier);
15715 else
15716 error_at (type_start_token->location,
15717 "declaration of %qD in %qD which does not "
15718 "enclose %qD",
15719 type, prev_scope, nested_name_specifier);
15720 type = error_mark_node;
15724 if (scoped_enum_p)
15725 begin_scope (sk_scoped_enum, type);
15727 /* Consume the opening brace. */
15728 cp_lexer_consume_token (parser->lexer);
15730 if (type == error_mark_node)
15731 ; /* Nothing to add */
15732 else if (OPAQUE_ENUM_P (type)
15733 || (cxx_dialect > cxx98 && processing_specialization))
15735 new_value_list = true;
15736 SET_OPAQUE_ENUM_P (type, false);
15737 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
15739 else
15741 error_at (type_start_token->location,
15742 "multiple definition of %q#T", type);
15743 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
15744 "previous definition here");
15745 type = error_mark_node;
15748 if (type == error_mark_node)
15749 cp_parser_skip_to_end_of_block_or_statement (parser);
15750 /* If the next token is not '}', then there are some enumerators. */
15751 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15753 if (is_anonymous && !scoped_enum_p)
15754 pedwarn (type_start_token->location, OPT_Wpedantic,
15755 "ISO C++ forbids empty anonymous enum");
15757 else
15758 cp_parser_enumerator_list (parser, type);
15760 /* Consume the final '}'. */
15761 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
15763 if (scoped_enum_p)
15764 finish_scope ();
15765 timevar_pop (TV_PARSE_ENUM);
15767 else
15769 /* If a ';' follows, then it is an opaque-enum-specifier
15770 and additional restrictions apply. */
15771 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15773 if (is_anonymous)
15774 error_at (type_start_token->location,
15775 "opaque-enum-specifier without name");
15776 else if (nested_name_specifier)
15777 error_at (type_start_token->location,
15778 "opaque-enum-specifier must use a simple identifier");
15782 /* Look for trailing attributes to apply to this enumeration, and
15783 apply them if appropriate. */
15784 if (cp_parser_allow_gnu_extensions_p (parser))
15786 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
15787 trailing_attr = chainon (trailing_attr, attributes);
15788 cplus_decl_attributes (&type,
15789 trailing_attr,
15790 (int) ATTR_FLAG_TYPE_IN_PLACE);
15793 /* Finish up the enumeration. */
15794 if (type != error_mark_node)
15796 if (new_value_list)
15797 finish_enum_value_list (type);
15798 if (is_new_type)
15799 finish_enum (type);
15802 if (nested_name_specifier)
15804 if (CLASS_TYPE_P (nested_name_specifier))
15806 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
15807 pop_scope (nested_name_specifier);
15809 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
15811 pop_nested_namespace (nested_name_specifier);
15814 out:
15815 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
15816 return type;
15819 /* Parse an enumerator-list. The enumerators all have the indicated
15820 TYPE.
15822 enumerator-list:
15823 enumerator-definition
15824 enumerator-list , enumerator-definition */
15826 static void
15827 cp_parser_enumerator_list (cp_parser* parser, tree type)
15829 while (true)
15831 /* Parse an enumerator-definition. */
15832 cp_parser_enumerator_definition (parser, type);
15834 /* If the next token is not a ',', we've reached the end of
15835 the list. */
15836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15837 break;
15838 /* Otherwise, consume the `,' and keep going. */
15839 cp_lexer_consume_token (parser->lexer);
15840 /* If the next token is a `}', there is a trailing comma. */
15841 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
15843 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
15844 pedwarn (input_location, OPT_Wpedantic,
15845 "comma at end of enumerator list");
15846 break;
15851 /* Parse an enumerator-definition. The enumerator has the indicated
15852 TYPE.
15854 enumerator-definition:
15855 enumerator
15856 enumerator = constant-expression
15858 enumerator:
15859 identifier */
15861 static void
15862 cp_parser_enumerator_definition (cp_parser* parser, tree type)
15864 tree identifier;
15865 tree value;
15866 location_t loc;
15868 /* Save the input location because we are interested in the location
15869 of the identifier and not the location of the explicit value. */
15870 loc = cp_lexer_peek_token (parser->lexer)->location;
15872 /* Look for the identifier. */
15873 identifier = cp_parser_identifier (parser);
15874 if (identifier == error_mark_node)
15875 return;
15877 /* If the next token is an '=', then there is an explicit value. */
15878 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15880 /* Consume the `=' token. */
15881 cp_lexer_consume_token (parser->lexer);
15882 /* Parse the value. */
15883 value = cp_parser_constant_expression (parser,
15884 /*allow_non_constant_p=*/false,
15885 NULL);
15887 else
15888 value = NULL_TREE;
15890 /* If we are processing a template, make sure the initializer of the
15891 enumerator doesn't contain any bare template parameter pack. */
15892 if (check_for_bare_parameter_packs (value))
15893 value = error_mark_node;
15895 /* integral_constant_value will pull out this expression, so make sure
15896 it's folded as appropriate. */
15897 value = fold_non_dependent_expr (value);
15899 /* Create the enumerator. */
15900 build_enumerator (identifier, value, type, loc);
15903 /* Parse a namespace-name.
15905 namespace-name:
15906 original-namespace-name
15907 namespace-alias
15909 Returns the NAMESPACE_DECL for the namespace. */
15911 static tree
15912 cp_parser_namespace_name (cp_parser* parser)
15914 tree identifier;
15915 tree namespace_decl;
15917 cp_token *token = cp_lexer_peek_token (parser->lexer);
15919 /* Get the name of the namespace. */
15920 identifier = cp_parser_identifier (parser);
15921 if (identifier == error_mark_node)
15922 return error_mark_node;
15924 /* Look up the identifier in the currently active scope. Look only
15925 for namespaces, due to:
15927 [basic.lookup.udir]
15929 When looking up a namespace-name in a using-directive or alias
15930 definition, only namespace names are considered.
15932 And:
15934 [basic.lookup.qual]
15936 During the lookup of a name preceding the :: scope resolution
15937 operator, object, function, and enumerator names are ignored.
15939 (Note that cp_parser_qualifying_entity only calls this
15940 function if the token after the name is the scope resolution
15941 operator.) */
15942 namespace_decl = cp_parser_lookup_name (parser, identifier,
15943 none_type,
15944 /*is_template=*/false,
15945 /*is_namespace=*/true,
15946 /*check_dependency=*/true,
15947 /*ambiguous_decls=*/NULL,
15948 token->location);
15949 /* If it's not a namespace, issue an error. */
15950 if (namespace_decl == error_mark_node
15951 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
15953 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15954 error_at (token->location, "%qD is not a namespace-name", identifier);
15955 cp_parser_error (parser, "expected namespace-name");
15956 namespace_decl = error_mark_node;
15959 return namespace_decl;
15962 /* Parse a namespace-definition.
15964 namespace-definition:
15965 named-namespace-definition
15966 unnamed-namespace-definition
15968 named-namespace-definition:
15969 original-namespace-definition
15970 extension-namespace-definition
15972 original-namespace-definition:
15973 namespace identifier { namespace-body }
15975 extension-namespace-definition:
15976 namespace original-namespace-name { namespace-body }
15978 unnamed-namespace-definition:
15979 namespace { namespace-body } */
15981 static void
15982 cp_parser_namespace_definition (cp_parser* parser)
15984 tree identifier, attribs;
15985 bool has_visibility;
15986 bool is_inline;
15988 cp_ensure_no_omp_declare_simd (parser);
15989 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
15991 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
15992 is_inline = true;
15993 cp_lexer_consume_token (parser->lexer);
15995 else
15996 is_inline = false;
15998 /* Look for the `namespace' keyword. */
15999 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16001 /* Get the name of the namespace. We do not attempt to distinguish
16002 between an original-namespace-definition and an
16003 extension-namespace-definition at this point. The semantic
16004 analysis routines are responsible for that. */
16005 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16006 identifier = cp_parser_identifier (parser);
16007 else
16008 identifier = NULL_TREE;
16010 /* Parse any specified attributes. */
16011 attribs = cp_parser_attributes_opt (parser);
16013 /* Look for the `{' to start the namespace. */
16014 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
16015 /* Start the namespace. */
16016 push_namespace (identifier);
16018 /* "inline namespace" is equivalent to a stub namespace definition
16019 followed by a strong using directive. */
16020 if (is_inline)
16022 tree name_space = current_namespace;
16023 /* Set up namespace association. */
16024 DECL_NAMESPACE_ASSOCIATIONS (name_space)
16025 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
16026 DECL_NAMESPACE_ASSOCIATIONS (name_space));
16027 /* Import the contents of the inline namespace. */
16028 pop_namespace ();
16029 do_using_directive (name_space);
16030 push_namespace (identifier);
16033 has_visibility = handle_namespace_attrs (current_namespace, attribs);
16035 /* Parse the body of the namespace. */
16036 cp_parser_namespace_body (parser);
16038 if (has_visibility)
16039 pop_visibility (1);
16041 /* Finish the namespace. */
16042 pop_namespace ();
16043 /* Look for the final `}'. */
16044 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16047 /* Parse a namespace-body.
16049 namespace-body:
16050 declaration-seq [opt] */
16052 static void
16053 cp_parser_namespace_body (cp_parser* parser)
16055 cp_parser_declaration_seq_opt (parser);
16058 /* Parse a namespace-alias-definition.
16060 namespace-alias-definition:
16061 namespace identifier = qualified-namespace-specifier ; */
16063 static void
16064 cp_parser_namespace_alias_definition (cp_parser* parser)
16066 tree identifier;
16067 tree namespace_specifier;
16069 cp_token *token = cp_lexer_peek_token (parser->lexer);
16071 /* Look for the `namespace' keyword. */
16072 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16073 /* Look for the identifier. */
16074 identifier = cp_parser_identifier (parser);
16075 if (identifier == error_mark_node)
16076 return;
16077 /* Look for the `=' token. */
16078 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
16079 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16081 error_at (token->location, "%<namespace%> definition is not allowed here");
16082 /* Skip the definition. */
16083 cp_lexer_consume_token (parser->lexer);
16084 if (cp_parser_skip_to_closing_brace (parser))
16085 cp_lexer_consume_token (parser->lexer);
16086 return;
16088 cp_parser_require (parser, CPP_EQ, RT_EQ);
16089 /* Look for the qualified-namespace-specifier. */
16090 namespace_specifier
16091 = cp_parser_qualified_namespace_specifier (parser);
16092 /* Look for the `;' token. */
16093 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16095 /* Register the alias in the symbol table. */
16096 do_namespace_alias (identifier, namespace_specifier);
16099 /* Parse a qualified-namespace-specifier.
16101 qualified-namespace-specifier:
16102 :: [opt] nested-name-specifier [opt] namespace-name
16104 Returns a NAMESPACE_DECL corresponding to the specified
16105 namespace. */
16107 static tree
16108 cp_parser_qualified_namespace_specifier (cp_parser* parser)
16110 /* Look for the optional `::'. */
16111 cp_parser_global_scope_opt (parser,
16112 /*current_scope_valid_p=*/false);
16114 /* Look for the optional nested-name-specifier. */
16115 cp_parser_nested_name_specifier_opt (parser,
16116 /*typename_keyword_p=*/false,
16117 /*check_dependency_p=*/true,
16118 /*type_p=*/false,
16119 /*is_declaration=*/true);
16121 return cp_parser_namespace_name (parser);
16124 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
16125 access declaration.
16127 using-declaration:
16128 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
16129 using :: unqualified-id ;
16131 access-declaration:
16132 qualified-id ;
16136 static bool
16137 cp_parser_using_declaration (cp_parser* parser,
16138 bool access_declaration_p)
16140 cp_token *token;
16141 bool typename_p = false;
16142 bool global_scope_p;
16143 tree decl;
16144 tree identifier;
16145 tree qscope;
16146 int oldcount = errorcount;
16147 cp_token *diag_token = NULL;
16149 if (access_declaration_p)
16151 diag_token = cp_lexer_peek_token (parser->lexer);
16152 cp_parser_parse_tentatively (parser);
16154 else
16156 /* Look for the `using' keyword. */
16157 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16159 /* Peek at the next token. */
16160 token = cp_lexer_peek_token (parser->lexer);
16161 /* See if it's `typename'. */
16162 if (token->keyword == RID_TYPENAME)
16164 /* Remember that we've seen it. */
16165 typename_p = true;
16166 /* Consume the `typename' token. */
16167 cp_lexer_consume_token (parser->lexer);
16171 /* Look for the optional global scope qualification. */
16172 global_scope_p
16173 = (cp_parser_global_scope_opt (parser,
16174 /*current_scope_valid_p=*/false)
16175 != NULL_TREE);
16177 /* If we saw `typename', or didn't see `::', then there must be a
16178 nested-name-specifier present. */
16179 if (typename_p || !global_scope_p)
16181 qscope = cp_parser_nested_name_specifier (parser, typename_p,
16182 /*check_dependency_p=*/true,
16183 /*type_p=*/false,
16184 /*is_declaration=*/true);
16185 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
16187 cp_parser_skip_to_end_of_block_or_statement (parser);
16188 return false;
16191 /* Otherwise, we could be in either of the two productions. In that
16192 case, treat the nested-name-specifier as optional. */
16193 else
16194 qscope = cp_parser_nested_name_specifier_opt (parser,
16195 /*typename_keyword_p=*/false,
16196 /*check_dependency_p=*/true,
16197 /*type_p=*/false,
16198 /*is_declaration=*/true);
16199 if (!qscope)
16200 qscope = global_namespace;
16201 else if (UNSCOPED_ENUM_P (qscope))
16202 qscope = CP_TYPE_CONTEXT (qscope);
16204 if (access_declaration_p && cp_parser_error_occurred (parser))
16205 /* Something has already gone wrong; there's no need to parse
16206 further. Since an error has occurred, the return value of
16207 cp_parser_parse_definitely will be false, as required. */
16208 return cp_parser_parse_definitely (parser);
16210 token = cp_lexer_peek_token (parser->lexer);
16211 /* Parse the unqualified-id. */
16212 identifier = cp_parser_unqualified_id (parser,
16213 /*template_keyword_p=*/false,
16214 /*check_dependency_p=*/true,
16215 /*declarator_p=*/true,
16216 /*optional_p=*/false);
16218 if (access_declaration_p)
16220 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16221 cp_parser_simulate_error (parser);
16222 if (!cp_parser_parse_definitely (parser))
16223 return false;
16226 /* The function we call to handle a using-declaration is different
16227 depending on what scope we are in. */
16228 if (qscope == error_mark_node || identifier == error_mark_node)
16230 else if (!identifier_p (identifier)
16231 && TREE_CODE (identifier) != BIT_NOT_EXPR)
16232 /* [namespace.udecl]
16234 A using declaration shall not name a template-id. */
16235 error_at (token->location,
16236 "a template-id may not appear in a using-declaration");
16237 else
16239 if (at_class_scope_p ())
16241 /* Create the USING_DECL. */
16242 decl = do_class_using_decl (parser->scope, identifier);
16244 if (decl && typename_p)
16245 USING_DECL_TYPENAME_P (decl) = 1;
16247 if (check_for_bare_parameter_packs (decl))
16249 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16250 return false;
16252 else
16253 /* Add it to the list of members in this class. */
16254 finish_member_declaration (decl);
16256 else
16258 decl = cp_parser_lookup_name_simple (parser,
16259 identifier,
16260 token->location);
16261 if (decl == error_mark_node)
16262 cp_parser_name_lookup_error (parser, identifier,
16263 decl, NLE_NULL,
16264 token->location);
16265 else if (check_for_bare_parameter_packs (decl))
16267 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16268 return false;
16270 else if (!at_namespace_scope_p ())
16271 do_local_using_decl (decl, qscope, identifier);
16272 else
16273 do_toplevel_using_decl (decl, qscope, identifier);
16277 /* Look for the final `;'. */
16278 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16280 if (access_declaration_p && errorcount == oldcount)
16281 warning_at (diag_token->location, OPT_Wdeprecated,
16282 "access declarations are deprecated "
16283 "in favour of using-declarations; "
16284 "suggestion: add the %<using%> keyword");
16286 return true;
16289 /* Parse an alias-declaration.
16291 alias-declaration:
16292 using identifier attribute-specifier-seq [opt] = type-id */
16294 static tree
16295 cp_parser_alias_declaration (cp_parser* parser)
16297 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
16298 location_t id_location;
16299 cp_declarator *declarator;
16300 cp_decl_specifier_seq decl_specs;
16301 bool member_p;
16302 const char *saved_message = NULL;
16304 /* Look for the `using' keyword. */
16305 cp_token *using_token
16306 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
16307 if (using_token == NULL)
16308 return error_mark_node;
16310 id_location = cp_lexer_peek_token (parser->lexer)->location;
16311 id = cp_parser_identifier (parser);
16312 if (id == error_mark_node)
16313 return error_mark_node;
16315 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
16316 attributes = cp_parser_attributes_opt (parser);
16317 if (attributes == error_mark_node)
16318 return error_mark_node;
16320 cp_parser_require (parser, CPP_EQ, RT_EQ);
16322 if (cp_parser_error_occurred (parser))
16323 return error_mark_node;
16325 cp_parser_commit_to_tentative_parse (parser);
16327 /* Now we are going to parse the type-id of the declaration. */
16330 [dcl.type]/3 says:
16332 "A type-specifier-seq shall not define a class or enumeration
16333 unless it appears in the type-id of an alias-declaration (7.1.3) that
16334 is not the declaration of a template-declaration."
16336 In other words, if we currently are in an alias template, the
16337 type-id should not define a type.
16339 So let's set parser->type_definition_forbidden_message in that
16340 case; cp_parser_check_type_definition (called by
16341 cp_parser_class_specifier) will then emit an error if a type is
16342 defined in the type-id. */
16343 if (parser->num_template_parameter_lists)
16345 saved_message = parser->type_definition_forbidden_message;
16346 parser->type_definition_forbidden_message =
16347 G_("types may not be defined in alias template declarations");
16350 type = cp_parser_type_id (parser);
16352 /* Restore the error message if need be. */
16353 if (parser->num_template_parameter_lists)
16354 parser->type_definition_forbidden_message = saved_message;
16356 if (type == error_mark_node
16357 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
16359 cp_parser_skip_to_end_of_block_or_statement (parser);
16360 return error_mark_node;
16363 /* A typedef-name can also be introduced by an alias-declaration. The
16364 identifier following the using keyword becomes a typedef-name. It has
16365 the same semantics as if it were introduced by the typedef
16366 specifier. In particular, it does not define a new type and it shall
16367 not appear in the type-id. */
16369 clear_decl_specs (&decl_specs);
16370 decl_specs.type = type;
16371 if (attributes != NULL_TREE)
16373 decl_specs.attributes = attributes;
16374 set_and_check_decl_spec_loc (&decl_specs,
16375 ds_attribute,
16376 attrs_token);
16378 set_and_check_decl_spec_loc (&decl_specs,
16379 ds_typedef,
16380 using_token);
16381 set_and_check_decl_spec_loc (&decl_specs,
16382 ds_alias,
16383 using_token);
16385 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
16386 declarator->id_loc = id_location;
16388 member_p = at_class_scope_p ();
16389 if (member_p)
16390 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
16391 NULL_TREE, attributes);
16392 else
16393 decl = start_decl (declarator, &decl_specs, 0,
16394 attributes, NULL_TREE, &pushed_scope);
16395 if (decl == error_mark_node)
16396 return decl;
16398 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
16400 if (pushed_scope)
16401 pop_scope (pushed_scope);
16403 /* If decl is a template, return its TEMPLATE_DECL so that it gets
16404 added into the symbol table; otherwise, return the TYPE_DECL. */
16405 if (DECL_LANG_SPECIFIC (decl)
16406 && DECL_TEMPLATE_INFO (decl)
16407 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
16409 decl = DECL_TI_TEMPLATE (decl);
16410 if (member_p)
16411 check_member_template (decl);
16414 return decl;
16417 /* Parse a using-directive.
16419 using-directive:
16420 using namespace :: [opt] nested-name-specifier [opt]
16421 namespace-name ; */
16423 static void
16424 cp_parser_using_directive (cp_parser* parser)
16426 tree namespace_decl;
16427 tree attribs;
16429 /* Look for the `using' keyword. */
16430 cp_parser_require_keyword (parser, RID_USING, RT_USING);
16431 /* And the `namespace' keyword. */
16432 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
16433 /* Look for the optional `::' operator. */
16434 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16435 /* And the optional nested-name-specifier. */
16436 cp_parser_nested_name_specifier_opt (parser,
16437 /*typename_keyword_p=*/false,
16438 /*check_dependency_p=*/true,
16439 /*type_p=*/false,
16440 /*is_declaration=*/true);
16441 /* Get the namespace being used. */
16442 namespace_decl = cp_parser_namespace_name (parser);
16443 /* And any specified attributes. */
16444 attribs = cp_parser_attributes_opt (parser);
16445 /* Update the symbol table. */
16446 parse_using_directive (namespace_decl, attribs);
16447 /* Look for the final `;'. */
16448 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16451 /* Parse an asm-definition.
16453 asm-definition:
16454 asm ( string-literal ) ;
16456 GNU Extension:
16458 asm-definition:
16459 asm volatile [opt] ( string-literal ) ;
16460 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
16461 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16462 : asm-operand-list [opt] ) ;
16463 asm volatile [opt] ( string-literal : asm-operand-list [opt]
16464 : asm-operand-list [opt]
16465 : asm-clobber-list [opt] ) ;
16466 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
16467 : asm-clobber-list [opt]
16468 : asm-goto-list ) ; */
16470 static void
16471 cp_parser_asm_definition (cp_parser* parser)
16473 tree string;
16474 tree outputs = NULL_TREE;
16475 tree inputs = NULL_TREE;
16476 tree clobbers = NULL_TREE;
16477 tree labels = NULL_TREE;
16478 tree asm_stmt;
16479 bool volatile_p = false;
16480 bool extended_p = false;
16481 bool invalid_inputs_p = false;
16482 bool invalid_outputs_p = false;
16483 bool goto_p = false;
16484 required_token missing = RT_NONE;
16486 /* Look for the `asm' keyword. */
16487 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
16488 /* See if the next token is `volatile'. */
16489 if (cp_parser_allow_gnu_extensions_p (parser)
16490 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
16492 /* Remember that we saw the `volatile' keyword. */
16493 volatile_p = true;
16494 /* Consume the token. */
16495 cp_lexer_consume_token (parser->lexer);
16497 if (cp_parser_allow_gnu_extensions_p (parser)
16498 && parser->in_function_body
16499 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
16501 /* Remember that we saw the `goto' keyword. */
16502 goto_p = true;
16503 /* Consume the token. */
16504 cp_lexer_consume_token (parser->lexer);
16506 /* Look for the opening `('. */
16507 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
16508 return;
16509 /* Look for the string. */
16510 string = cp_parser_string_literal (parser, false, false);
16511 if (string == error_mark_node)
16513 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16514 /*consume_paren=*/true);
16515 return;
16518 /* If we're allowing GNU extensions, check for the extended assembly
16519 syntax. Unfortunately, the `:' tokens need not be separated by
16520 a space in C, and so, for compatibility, we tolerate that here
16521 too. Doing that means that we have to treat the `::' operator as
16522 two `:' tokens. */
16523 if (cp_parser_allow_gnu_extensions_p (parser)
16524 && parser->in_function_body
16525 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
16526 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
16528 bool inputs_p = false;
16529 bool clobbers_p = false;
16530 bool labels_p = false;
16532 /* The extended syntax was used. */
16533 extended_p = true;
16535 /* Look for outputs. */
16536 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16538 /* Consume the `:'. */
16539 cp_lexer_consume_token (parser->lexer);
16540 /* Parse the output-operands. */
16541 if (cp_lexer_next_token_is_not (parser->lexer,
16542 CPP_COLON)
16543 && cp_lexer_next_token_is_not (parser->lexer,
16544 CPP_SCOPE)
16545 && cp_lexer_next_token_is_not (parser->lexer,
16546 CPP_CLOSE_PAREN)
16547 && !goto_p)
16548 outputs = cp_parser_asm_operand_list (parser);
16550 if (outputs == error_mark_node)
16551 invalid_outputs_p = true;
16553 /* If the next token is `::', there are no outputs, and the
16554 next token is the beginning of the inputs. */
16555 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16556 /* The inputs are coming next. */
16557 inputs_p = true;
16559 /* Look for inputs. */
16560 if (inputs_p
16561 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16563 /* Consume the `:' or `::'. */
16564 cp_lexer_consume_token (parser->lexer);
16565 /* Parse the output-operands. */
16566 if (cp_lexer_next_token_is_not (parser->lexer,
16567 CPP_COLON)
16568 && cp_lexer_next_token_is_not (parser->lexer,
16569 CPP_SCOPE)
16570 && cp_lexer_next_token_is_not (parser->lexer,
16571 CPP_CLOSE_PAREN))
16572 inputs = cp_parser_asm_operand_list (parser);
16574 if (inputs == error_mark_node)
16575 invalid_inputs_p = true;
16577 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16578 /* The clobbers are coming next. */
16579 clobbers_p = true;
16581 /* Look for clobbers. */
16582 if (clobbers_p
16583 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16585 clobbers_p = true;
16586 /* Consume the `:' or `::'. */
16587 cp_lexer_consume_token (parser->lexer);
16588 /* Parse the clobbers. */
16589 if (cp_lexer_next_token_is_not (parser->lexer,
16590 CPP_COLON)
16591 && cp_lexer_next_token_is_not (parser->lexer,
16592 CPP_CLOSE_PAREN))
16593 clobbers = cp_parser_asm_clobber_list (parser);
16595 else if (goto_p
16596 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16597 /* The labels are coming next. */
16598 labels_p = true;
16600 /* Look for labels. */
16601 if (labels_p
16602 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
16604 labels_p = true;
16605 /* Consume the `:' or `::'. */
16606 cp_lexer_consume_token (parser->lexer);
16607 /* Parse the labels. */
16608 labels = cp_parser_asm_label_list (parser);
16611 if (goto_p && !labels_p)
16612 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
16614 else if (goto_p)
16615 missing = RT_COLON_SCOPE;
16617 /* Look for the closing `)'. */
16618 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
16619 missing ? missing : RT_CLOSE_PAREN))
16620 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16621 /*consume_paren=*/true);
16622 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16624 if (!invalid_inputs_p && !invalid_outputs_p)
16626 /* Create the ASM_EXPR. */
16627 if (parser->in_function_body)
16629 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
16630 inputs, clobbers, labels);
16631 /* If the extended syntax was not used, mark the ASM_EXPR. */
16632 if (!extended_p)
16634 tree temp = asm_stmt;
16635 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
16636 temp = TREE_OPERAND (temp, 0);
16638 ASM_INPUT_P (temp) = 1;
16641 else
16642 symtab->finalize_toplevel_asm (string);
16646 /* Declarators [gram.dcl.decl] */
16648 /* Parse an init-declarator.
16650 init-declarator:
16651 declarator initializer [opt]
16653 GNU Extension:
16655 init-declarator:
16656 declarator asm-specification [opt] attributes [opt] initializer [opt]
16658 function-definition:
16659 decl-specifier-seq [opt] declarator ctor-initializer [opt]
16660 function-body
16661 decl-specifier-seq [opt] declarator function-try-block
16663 GNU Extension:
16665 function-definition:
16666 __extension__ function-definition
16668 TM Extension:
16670 function-definition:
16671 decl-specifier-seq [opt] declarator function-transaction-block
16673 The DECL_SPECIFIERS apply to this declarator. Returns a
16674 representation of the entity declared. If MEMBER_P is TRUE, then
16675 this declarator appears in a class scope. The new DECL created by
16676 this declarator is returned.
16678 The CHECKS are access checks that should be performed once we know
16679 what entity is being declared (and, therefore, what classes have
16680 befriended it).
16682 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
16683 for a function-definition here as well. If the declarator is a
16684 declarator for a function-definition, *FUNCTION_DEFINITION_P will
16685 be TRUE upon return. By that point, the function-definition will
16686 have been completely parsed.
16688 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
16689 is FALSE.
16691 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
16692 parsed declaration if it is an uninitialized single declarator not followed
16693 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
16694 if present, will not be consumed. If returned, this declarator will be
16695 created with SD_INITIALIZED but will not call cp_finish_decl. */
16697 static tree
16698 cp_parser_init_declarator (cp_parser* parser,
16699 cp_decl_specifier_seq *decl_specifiers,
16700 vec<deferred_access_check, va_gc> *checks,
16701 bool function_definition_allowed_p,
16702 bool member_p,
16703 int declares_class_or_enum,
16704 bool* function_definition_p,
16705 tree* maybe_range_for_decl)
16707 cp_token *token = NULL, *asm_spec_start_token = NULL,
16708 *attributes_start_token = NULL;
16709 cp_declarator *declarator;
16710 tree prefix_attributes;
16711 tree attributes = NULL;
16712 tree asm_specification;
16713 tree initializer;
16714 tree decl = NULL_TREE;
16715 tree scope;
16716 int is_initialized;
16717 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
16718 initialized with "= ..", CPP_OPEN_PAREN if initialized with
16719 "(...)". */
16720 enum cpp_ttype initialization_kind;
16721 bool is_direct_init = false;
16722 bool is_non_constant_init;
16723 int ctor_dtor_or_conv_p;
16724 bool friend_p = cp_parser_friend_p (decl_specifiers);
16725 tree pushed_scope = NULL_TREE;
16726 bool range_for_decl_p = false;
16727 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16729 /* Gather the attributes that were provided with the
16730 decl-specifiers. */
16731 prefix_attributes = decl_specifiers->attributes;
16733 /* Assume that this is not the declarator for a function
16734 definition. */
16735 if (function_definition_p)
16736 *function_definition_p = false;
16738 /* Default arguments are only permitted for function parameters. */
16739 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
16740 parser->default_arg_ok_p = false;
16742 /* Defer access checks while parsing the declarator; we cannot know
16743 what names are accessible until we know what is being
16744 declared. */
16745 resume_deferring_access_checks ();
16747 /* Parse the declarator. */
16748 token = cp_lexer_peek_token (parser->lexer);
16749 declarator
16750 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16751 &ctor_dtor_or_conv_p,
16752 /*parenthesized_p=*/NULL,
16753 member_p, friend_p);
16754 /* Gather up the deferred checks. */
16755 stop_deferring_access_checks ();
16757 parser->default_arg_ok_p = saved_default_arg_ok_p;
16759 /* If the DECLARATOR was erroneous, there's no need to go
16760 further. */
16761 if (declarator == cp_error_declarator)
16762 return error_mark_node;
16764 /* Check that the number of template-parameter-lists is OK. */
16765 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
16766 token->location))
16767 return error_mark_node;
16769 if (declares_class_or_enum & 2)
16770 cp_parser_check_for_definition_in_return_type (declarator,
16771 decl_specifiers->type,
16772 decl_specifiers->locations[ds_type_spec]);
16774 /* Figure out what scope the entity declared by the DECLARATOR is
16775 located in. `grokdeclarator' sometimes changes the scope, so
16776 we compute it now. */
16777 scope = get_scope_of_declarator (declarator);
16779 /* Perform any lookups in the declared type which were thought to be
16780 dependent, but are not in the scope of the declarator. */
16781 decl_specifiers->type
16782 = maybe_update_decl_type (decl_specifiers->type, scope);
16784 /* If we're allowing GNU extensions, look for an
16785 asm-specification. */
16786 if (cp_parser_allow_gnu_extensions_p (parser))
16788 /* Look for an asm-specification. */
16789 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
16790 asm_specification = cp_parser_asm_specification_opt (parser);
16792 else
16793 asm_specification = NULL_TREE;
16795 /* Look for attributes. */
16796 attributes_start_token = cp_lexer_peek_token (parser->lexer);
16797 attributes = cp_parser_attributes_opt (parser);
16799 /* Peek at the next token. */
16800 token = cp_lexer_peek_token (parser->lexer);
16802 bool bogus_implicit_tmpl = false;
16804 if (function_declarator_p (declarator))
16806 /* Check to see if the token indicates the start of a
16807 function-definition. */
16808 if (cp_parser_token_starts_function_definition_p (token))
16810 if (!function_definition_allowed_p)
16812 /* If a function-definition should not appear here, issue an
16813 error message. */
16814 cp_parser_error (parser,
16815 "a function-definition is not allowed here");
16816 return error_mark_node;
16819 location_t func_brace_location
16820 = cp_lexer_peek_token (parser->lexer)->location;
16822 /* Neither attributes nor an asm-specification are allowed
16823 on a function-definition. */
16824 if (asm_specification)
16825 error_at (asm_spec_start_token->location,
16826 "an asm-specification is not allowed "
16827 "on a function-definition");
16828 if (attributes)
16829 error_at (attributes_start_token->location,
16830 "attributes are not allowed "
16831 "on a function-definition");
16832 /* This is a function-definition. */
16833 *function_definition_p = true;
16835 /* Parse the function definition. */
16836 if (member_p)
16837 decl = cp_parser_save_member_function_body (parser,
16838 decl_specifiers,
16839 declarator,
16840 prefix_attributes);
16841 else
16842 decl =
16843 (cp_parser_function_definition_from_specifiers_and_declarator
16844 (parser, decl_specifiers, prefix_attributes, declarator));
16846 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
16848 /* This is where the prologue starts... */
16849 DECL_STRUCT_FUNCTION (decl)->function_start_locus
16850 = func_brace_location;
16853 return decl;
16856 else if (parser->fully_implicit_function_template_p)
16858 /* A non-template declaration involving a function parameter list
16859 containing an implicit template parameter will be made into a
16860 template. If the resulting declaration is not going to be an
16861 actual function then finish the template scope here to prevent it.
16862 An error message will be issued once we have a decl to talk about.
16864 FIXME probably we should do type deduction rather than create an
16865 implicit template, but the standard currently doesn't allow it. */
16866 bogus_implicit_tmpl = true;
16867 finish_fully_implicit_template (parser, NULL_TREE);
16870 /* [dcl.dcl]
16872 Only in function declarations for constructors, destructors, and
16873 type conversions can the decl-specifier-seq be omitted.
16875 We explicitly postpone this check past the point where we handle
16876 function-definitions because we tolerate function-definitions
16877 that are missing their return types in some modes. */
16878 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
16880 cp_parser_error (parser,
16881 "expected constructor, destructor, or type conversion");
16882 return error_mark_node;
16885 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
16886 if (token->type == CPP_EQ
16887 || token->type == CPP_OPEN_PAREN
16888 || token->type == CPP_OPEN_BRACE)
16890 is_initialized = SD_INITIALIZED;
16891 initialization_kind = token->type;
16892 if (maybe_range_for_decl)
16893 *maybe_range_for_decl = error_mark_node;
16895 if (token->type == CPP_EQ
16896 && function_declarator_p (declarator))
16898 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
16899 if (t2->keyword == RID_DEFAULT)
16900 is_initialized = SD_DEFAULTED;
16901 else if (t2->keyword == RID_DELETE)
16902 is_initialized = SD_DELETED;
16905 else
16907 /* If the init-declarator isn't initialized and isn't followed by a
16908 `,' or `;', it's not a valid init-declarator. */
16909 if (token->type != CPP_COMMA
16910 && token->type != CPP_SEMICOLON)
16912 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
16913 range_for_decl_p = true;
16914 else
16916 cp_parser_error (parser, "expected initializer");
16917 return error_mark_node;
16920 is_initialized = SD_UNINITIALIZED;
16921 initialization_kind = CPP_EOF;
16924 /* Because start_decl has side-effects, we should only call it if we
16925 know we're going ahead. By this point, we know that we cannot
16926 possibly be looking at any other construct. */
16927 cp_parser_commit_to_tentative_parse (parser);
16929 /* Enter the newly declared entry in the symbol table. If we're
16930 processing a declaration in a class-specifier, we wait until
16931 after processing the initializer. */
16932 if (!member_p)
16934 if (parser->in_unbraced_linkage_specification_p)
16935 decl_specifiers->storage_class = sc_extern;
16936 decl = start_decl (declarator, decl_specifiers,
16937 range_for_decl_p? SD_INITIALIZED : is_initialized,
16938 attributes, prefix_attributes, &pushed_scope);
16939 cp_finalize_omp_declare_simd (parser, decl);
16940 /* Adjust location of decl if declarator->id_loc is more appropriate:
16941 set, and decl wasn't merged with another decl, in which case its
16942 location would be different from input_location, and more accurate. */
16943 if (DECL_P (decl)
16944 && declarator->id_loc != UNKNOWN_LOCATION
16945 && DECL_SOURCE_LOCATION (decl) == input_location)
16946 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
16948 else if (scope)
16949 /* Enter the SCOPE. That way unqualified names appearing in the
16950 initializer will be looked up in SCOPE. */
16951 pushed_scope = push_scope (scope);
16953 /* Perform deferred access control checks, now that we know in which
16954 SCOPE the declared entity resides. */
16955 if (!member_p && decl)
16957 tree saved_current_function_decl = NULL_TREE;
16959 /* If the entity being declared is a function, pretend that we
16960 are in its scope. If it is a `friend', it may have access to
16961 things that would not otherwise be accessible. */
16962 if (TREE_CODE (decl) == FUNCTION_DECL)
16964 saved_current_function_decl = current_function_decl;
16965 current_function_decl = decl;
16968 /* Perform access checks for template parameters. */
16969 cp_parser_perform_template_parameter_access_checks (checks);
16971 /* Perform the access control checks for the declarator and the
16972 decl-specifiers. */
16973 perform_deferred_access_checks (tf_warning_or_error);
16975 /* Restore the saved value. */
16976 if (TREE_CODE (decl) == FUNCTION_DECL)
16977 current_function_decl = saved_current_function_decl;
16980 /* Parse the initializer. */
16981 initializer = NULL_TREE;
16982 is_direct_init = false;
16983 is_non_constant_init = true;
16984 if (is_initialized)
16986 if (function_declarator_p (declarator))
16988 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
16989 if (initialization_kind == CPP_EQ)
16990 initializer = cp_parser_pure_specifier (parser);
16991 else
16993 /* If the declaration was erroneous, we don't really
16994 know what the user intended, so just silently
16995 consume the initializer. */
16996 if (decl != error_mark_node)
16997 error_at (initializer_start_token->location,
16998 "initializer provided for function");
16999 cp_parser_skip_to_closing_parenthesis (parser,
17000 /*recovering=*/true,
17001 /*or_comma=*/false,
17002 /*consume_paren=*/true);
17005 else
17007 /* We want to record the extra mangling scope for in-class
17008 initializers of class members and initializers of static data
17009 member templates. The former involves deferring
17010 parsing of the initializer until end of class as with default
17011 arguments. So right here we only handle the latter. */
17012 if (!member_p && processing_template_decl)
17013 start_lambda_scope (decl);
17014 initializer = cp_parser_initializer (parser,
17015 &is_direct_init,
17016 &is_non_constant_init);
17017 if (!member_p && processing_template_decl)
17018 finish_lambda_scope ();
17019 if (initializer == error_mark_node)
17020 cp_parser_skip_to_end_of_statement (parser);
17024 /* The old parser allows attributes to appear after a parenthesized
17025 initializer. Mark Mitchell proposed removing this functionality
17026 on the GCC mailing lists on 2002-08-13. This parser accepts the
17027 attributes -- but ignores them. */
17028 if (cp_parser_allow_gnu_extensions_p (parser)
17029 && initialization_kind == CPP_OPEN_PAREN)
17030 if (cp_parser_attributes_opt (parser))
17031 warning (OPT_Wattributes,
17032 "attributes after parenthesized initializer ignored");
17034 /* And now complain about a non-function implicit template. */
17035 if (bogus_implicit_tmpl)
17036 error_at (DECL_SOURCE_LOCATION (decl),
17037 "non-function %qD declared as implicit template", decl);
17039 /* For an in-class declaration, use `grokfield' to create the
17040 declaration. */
17041 if (member_p)
17043 if (pushed_scope)
17045 pop_scope (pushed_scope);
17046 pushed_scope = NULL_TREE;
17048 decl = grokfield (declarator, decl_specifiers,
17049 initializer, !is_non_constant_init,
17050 /*asmspec=*/NULL_TREE,
17051 chainon (attributes, prefix_attributes));
17052 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
17053 cp_parser_save_default_args (parser, decl);
17054 cp_finalize_omp_declare_simd (parser, decl);
17057 /* Finish processing the declaration. But, skip member
17058 declarations. */
17059 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
17061 cp_finish_decl (decl,
17062 initializer, !is_non_constant_init,
17063 asm_specification,
17064 /* If the initializer is in parentheses, then this is
17065 a direct-initialization, which means that an
17066 `explicit' constructor is OK. Otherwise, an
17067 `explicit' constructor cannot be used. */
17068 ((is_direct_init || !is_initialized)
17069 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
17071 else if ((cxx_dialect != cxx98) && friend_p
17072 && decl && TREE_CODE (decl) == FUNCTION_DECL)
17073 /* Core issue #226 (C++0x only): A default template-argument
17074 shall not be specified in a friend class template
17075 declaration. */
17076 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
17077 /*is_partial=*/false, /*is_friend_decl=*/1);
17079 if (!friend_p && pushed_scope)
17080 pop_scope (pushed_scope);
17082 if (function_declarator_p (declarator)
17083 && parser->fully_implicit_function_template_p)
17085 if (member_p)
17086 decl = finish_fully_implicit_template (parser, decl);
17087 else
17088 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
17091 return decl;
17094 /* Parse a declarator.
17096 declarator:
17097 direct-declarator
17098 ptr-operator declarator
17100 abstract-declarator:
17101 ptr-operator abstract-declarator [opt]
17102 direct-abstract-declarator
17104 GNU Extensions:
17106 declarator:
17107 attributes [opt] direct-declarator
17108 attributes [opt] ptr-operator declarator
17110 abstract-declarator:
17111 attributes [opt] ptr-operator abstract-declarator [opt]
17112 attributes [opt] direct-abstract-declarator
17114 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
17115 detect constructor, destructor or conversion operators. It is set
17116 to -1 if the declarator is a name, and +1 if it is a
17117 function. Otherwise it is set to zero. Usually you just want to
17118 test for >0, but internally the negative value is used.
17120 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
17121 a decl-specifier-seq unless it declares a constructor, destructor,
17122 or conversion. It might seem that we could check this condition in
17123 semantic analysis, rather than parsing, but that makes it difficult
17124 to handle something like `f()'. We want to notice that there are
17125 no decl-specifiers, and therefore realize that this is an
17126 expression, not a declaration.)
17128 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
17129 the declarator is a direct-declarator of the form "(...)".
17131 MEMBER_P is true iff this declarator is a member-declarator.
17133 FRIEND_P is true iff this declarator is a friend. */
17135 static cp_declarator *
17136 cp_parser_declarator (cp_parser* parser,
17137 cp_parser_declarator_kind dcl_kind,
17138 int* ctor_dtor_or_conv_p,
17139 bool* parenthesized_p,
17140 bool member_p, bool friend_p)
17142 cp_declarator *declarator;
17143 enum tree_code code;
17144 cp_cv_quals cv_quals;
17145 tree class_type;
17146 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
17148 /* Assume this is not a constructor, destructor, or type-conversion
17149 operator. */
17150 if (ctor_dtor_or_conv_p)
17151 *ctor_dtor_or_conv_p = 0;
17153 if (cp_parser_allow_gnu_extensions_p (parser))
17154 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
17156 /* Check for the ptr-operator production. */
17157 cp_parser_parse_tentatively (parser);
17158 /* Parse the ptr-operator. */
17159 code = cp_parser_ptr_operator (parser,
17160 &class_type,
17161 &cv_quals,
17162 &std_attributes);
17164 /* If that worked, then we have a ptr-operator. */
17165 if (cp_parser_parse_definitely (parser))
17167 /* If a ptr-operator was found, then this declarator was not
17168 parenthesized. */
17169 if (parenthesized_p)
17170 *parenthesized_p = true;
17171 /* The dependent declarator is optional if we are parsing an
17172 abstract-declarator. */
17173 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17174 cp_parser_parse_tentatively (parser);
17176 /* Parse the dependent declarator. */
17177 declarator = cp_parser_declarator (parser, dcl_kind,
17178 /*ctor_dtor_or_conv_p=*/NULL,
17179 /*parenthesized_p=*/NULL,
17180 /*member_p=*/false,
17181 friend_p);
17183 /* If we are parsing an abstract-declarator, we must handle the
17184 case where the dependent declarator is absent. */
17185 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
17186 && !cp_parser_parse_definitely (parser))
17187 declarator = NULL;
17189 declarator = cp_parser_make_indirect_declarator
17190 (code, class_type, cv_quals, declarator, std_attributes);
17192 /* Everything else is a direct-declarator. */
17193 else
17195 if (parenthesized_p)
17196 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
17197 CPP_OPEN_PAREN);
17198 declarator = cp_parser_direct_declarator (parser, dcl_kind,
17199 ctor_dtor_or_conv_p,
17200 member_p, friend_p);
17203 if (gnu_attributes && declarator && declarator != cp_error_declarator)
17204 declarator->attributes = gnu_attributes;
17205 return declarator;
17208 /* Parse a direct-declarator or direct-abstract-declarator.
17210 direct-declarator:
17211 declarator-id
17212 direct-declarator ( parameter-declaration-clause )
17213 cv-qualifier-seq [opt]
17214 ref-qualifier [opt]
17215 exception-specification [opt]
17216 direct-declarator [ constant-expression [opt] ]
17217 ( declarator )
17219 direct-abstract-declarator:
17220 direct-abstract-declarator [opt]
17221 ( parameter-declaration-clause )
17222 cv-qualifier-seq [opt]
17223 ref-qualifier [opt]
17224 exception-specification [opt]
17225 direct-abstract-declarator [opt] [ constant-expression [opt] ]
17226 ( abstract-declarator )
17228 Returns a representation of the declarator. DCL_KIND is
17229 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
17230 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
17231 we are parsing a direct-declarator. It is
17232 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
17233 of ambiguity we prefer an abstract declarator, as per
17234 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
17235 as for cp_parser_declarator. */
17237 static cp_declarator *
17238 cp_parser_direct_declarator (cp_parser* parser,
17239 cp_parser_declarator_kind dcl_kind,
17240 int* ctor_dtor_or_conv_p,
17241 bool member_p, bool friend_p)
17243 cp_token *token;
17244 cp_declarator *declarator = NULL;
17245 tree scope = NULL_TREE;
17246 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17247 bool saved_in_declarator_p = parser->in_declarator_p;
17248 bool first = true;
17249 tree pushed_scope = NULL_TREE;
17251 while (true)
17253 /* Peek at the next token. */
17254 token = cp_lexer_peek_token (parser->lexer);
17255 if (token->type == CPP_OPEN_PAREN)
17257 /* This is either a parameter-declaration-clause, or a
17258 parenthesized declarator. When we know we are parsing a
17259 named declarator, it must be a parenthesized declarator
17260 if FIRST is true. For instance, `(int)' is a
17261 parameter-declaration-clause, with an omitted
17262 direct-abstract-declarator. But `((*))', is a
17263 parenthesized abstract declarator. Finally, when T is a
17264 template parameter `(T)' is a
17265 parameter-declaration-clause, and not a parenthesized
17266 named declarator.
17268 We first try and parse a parameter-declaration-clause,
17269 and then try a nested declarator (if FIRST is true).
17271 It is not an error for it not to be a
17272 parameter-declaration-clause, even when FIRST is
17273 false. Consider,
17275 int i (int);
17276 int i (3);
17278 The first is the declaration of a function while the
17279 second is the definition of a variable, including its
17280 initializer.
17282 Having seen only the parenthesis, we cannot know which of
17283 these two alternatives should be selected. Even more
17284 complex are examples like:
17286 int i (int (a));
17287 int i (int (3));
17289 The former is a function-declaration; the latter is a
17290 variable initialization.
17292 Thus again, we try a parameter-declaration-clause, and if
17293 that fails, we back out and return. */
17295 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17297 tree params;
17298 bool is_declarator = false;
17300 /* In a member-declarator, the only valid interpretation
17301 of a parenthesis is the start of a
17302 parameter-declaration-clause. (It is invalid to
17303 initialize a static data member with a parenthesized
17304 initializer; only the "=" form of initialization is
17305 permitted.) */
17306 if (!member_p)
17307 cp_parser_parse_tentatively (parser);
17309 /* Consume the `('. */
17310 cp_lexer_consume_token (parser->lexer);
17311 if (first)
17313 /* If this is going to be an abstract declarator, we're
17314 in a declarator and we can't have default args. */
17315 parser->default_arg_ok_p = false;
17316 parser->in_declarator_p = true;
17319 begin_scope (sk_function_parms, NULL_TREE);
17321 /* Parse the parameter-declaration-clause. */
17322 params = cp_parser_parameter_declaration_clause (parser);
17324 /* Consume the `)'. */
17325 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
17327 /* If all went well, parse the cv-qualifier-seq,
17328 ref-qualifier and the exception-specification. */
17329 if (member_p || cp_parser_parse_definitely (parser))
17331 cp_cv_quals cv_quals;
17332 cp_virt_specifiers virt_specifiers;
17333 cp_ref_qualifier ref_qual;
17334 tree exception_specification;
17335 tree late_return;
17336 tree attrs;
17337 bool memfn = (member_p || (pushed_scope
17338 && CLASS_TYPE_P (pushed_scope)));
17340 is_declarator = true;
17342 if (ctor_dtor_or_conv_p)
17343 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
17344 first = false;
17346 /* Parse the cv-qualifier-seq. */
17347 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17348 /* Parse the ref-qualifier. */
17349 ref_qual = cp_parser_ref_qualifier_opt (parser);
17350 /* And the exception-specification. */
17351 exception_specification
17352 = cp_parser_exception_specification_opt (parser);
17354 attrs = cp_parser_std_attribute_spec_seq (parser);
17356 /* In here, we handle cases where attribute is used after
17357 the function declaration. For example:
17358 void func (int x) __attribute__((vector(..))); */
17359 if (flag_cilkplus
17360 && cp_next_tokens_can_be_gnu_attribute_p (parser))
17362 cp_parser_parse_tentatively (parser);
17363 tree attr = cp_parser_gnu_attributes_opt (parser);
17364 if (cp_lexer_next_token_is_not (parser->lexer,
17365 CPP_SEMICOLON)
17366 && cp_lexer_next_token_is_not (parser->lexer,
17367 CPP_OPEN_BRACE))
17368 cp_parser_abort_tentative_parse (parser);
17369 else if (!cp_parser_parse_definitely (parser))
17371 else
17372 attrs = chainon (attr, attrs);
17374 late_return = (cp_parser_late_return_type_opt
17375 (parser, declarator,
17376 memfn ? cv_quals : -1));
17379 /* Parse the virt-specifier-seq. */
17380 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17382 /* Create the function-declarator. */
17383 declarator = make_call_declarator (declarator,
17384 params,
17385 cv_quals,
17386 virt_specifiers,
17387 ref_qual,
17388 exception_specification,
17389 late_return);
17390 declarator->std_attributes = attrs;
17391 /* Any subsequent parameter lists are to do with
17392 return type, so are not those of the declared
17393 function. */
17394 parser->default_arg_ok_p = false;
17397 /* Remove the function parms from scope. */
17398 pop_bindings_and_leave_scope ();
17400 if (is_declarator)
17401 /* Repeat the main loop. */
17402 continue;
17405 /* If this is the first, we can try a parenthesized
17406 declarator. */
17407 if (first)
17409 bool saved_in_type_id_in_expr_p;
17411 parser->default_arg_ok_p = saved_default_arg_ok_p;
17412 parser->in_declarator_p = saved_in_declarator_p;
17414 /* Consume the `('. */
17415 cp_lexer_consume_token (parser->lexer);
17416 /* Parse the nested declarator. */
17417 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17418 parser->in_type_id_in_expr_p = true;
17419 declarator
17420 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
17421 /*parenthesized_p=*/NULL,
17422 member_p, friend_p);
17423 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17424 first = false;
17425 /* Expect a `)'. */
17426 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
17427 declarator = cp_error_declarator;
17428 if (declarator == cp_error_declarator)
17429 break;
17431 goto handle_declarator;
17433 /* Otherwise, we must be done. */
17434 else
17435 break;
17437 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
17438 && token->type == CPP_OPEN_SQUARE
17439 && !cp_next_tokens_can_be_attribute_p (parser))
17441 /* Parse an array-declarator. */
17442 tree bounds, attrs;
17444 if (ctor_dtor_or_conv_p)
17445 *ctor_dtor_or_conv_p = 0;
17447 first = false;
17448 parser->default_arg_ok_p = false;
17449 parser->in_declarator_p = true;
17450 /* Consume the `['. */
17451 cp_lexer_consume_token (parser->lexer);
17452 /* Peek at the next token. */
17453 token = cp_lexer_peek_token (parser->lexer);
17454 /* If the next token is `]', then there is no
17455 constant-expression. */
17456 if (token->type != CPP_CLOSE_SQUARE)
17458 bool non_constant_p;
17459 bounds
17460 = cp_parser_constant_expression (parser,
17461 /*allow_non_constant=*/true,
17462 &non_constant_p);
17463 if (!non_constant_p)
17464 /* OK */;
17465 else if (error_operand_p (bounds))
17466 /* Already gave an error. */;
17467 else if (!parser->in_function_body
17468 || current_binding_level->kind == sk_function_parms)
17470 /* Normally, the array bound must be an integral constant
17471 expression. However, as an extension, we allow VLAs
17472 in function scopes as long as they aren't part of a
17473 parameter declaration. */
17474 cp_parser_error (parser,
17475 "array bound is not an integer constant");
17476 bounds = error_mark_node;
17478 else if (processing_template_decl
17479 && !type_dependent_expression_p (bounds))
17481 /* Remember this wasn't a constant-expression. */
17482 bounds = build_nop (TREE_TYPE (bounds), bounds);
17483 TREE_SIDE_EFFECTS (bounds) = 1;
17486 else
17487 bounds = NULL_TREE;
17488 /* Look for the closing `]'. */
17489 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17491 declarator = cp_error_declarator;
17492 break;
17495 attrs = cp_parser_std_attribute_spec_seq (parser);
17496 declarator = make_array_declarator (declarator, bounds);
17497 declarator->std_attributes = attrs;
17499 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
17502 tree qualifying_scope;
17503 tree unqualified_name;
17504 tree attrs;
17505 special_function_kind sfk;
17506 bool abstract_ok;
17507 bool pack_expansion_p = false;
17508 cp_token *declarator_id_start_token;
17510 /* Parse a declarator-id */
17511 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
17512 if (abstract_ok)
17514 cp_parser_parse_tentatively (parser);
17516 /* If we see an ellipsis, we should be looking at a
17517 parameter pack. */
17518 if (token->type == CPP_ELLIPSIS)
17520 /* Consume the `...' */
17521 cp_lexer_consume_token (parser->lexer);
17523 pack_expansion_p = true;
17527 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
17528 unqualified_name
17529 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
17530 qualifying_scope = parser->scope;
17531 if (abstract_ok)
17533 bool okay = false;
17535 if (!unqualified_name && pack_expansion_p)
17537 /* Check whether an error occurred. */
17538 okay = !cp_parser_error_occurred (parser);
17540 /* We already consumed the ellipsis to mark a
17541 parameter pack, but we have no way to report it,
17542 so abort the tentative parse. We will be exiting
17543 immediately anyway. */
17544 cp_parser_abort_tentative_parse (parser);
17546 else
17547 okay = cp_parser_parse_definitely (parser);
17549 if (!okay)
17550 unqualified_name = error_mark_node;
17551 else if (unqualified_name
17552 && (qualifying_scope
17553 || (!identifier_p (unqualified_name))))
17555 cp_parser_error (parser, "expected unqualified-id");
17556 unqualified_name = error_mark_node;
17560 if (!unqualified_name)
17561 return NULL;
17562 if (unqualified_name == error_mark_node)
17564 declarator = cp_error_declarator;
17565 pack_expansion_p = false;
17566 declarator->parameter_pack_p = false;
17567 break;
17570 attrs = cp_parser_std_attribute_spec_seq (parser);
17572 if (qualifying_scope && at_namespace_scope_p ()
17573 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
17575 /* In the declaration of a member of a template class
17576 outside of the class itself, the SCOPE will sometimes
17577 be a TYPENAME_TYPE. For example, given:
17579 template <typename T>
17580 int S<T>::R::i = 3;
17582 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
17583 this context, we must resolve S<T>::R to an ordinary
17584 type, rather than a typename type.
17586 The reason we normally avoid resolving TYPENAME_TYPEs
17587 is that a specialization of `S' might render
17588 `S<T>::R' not a type. However, if `S' is
17589 specialized, then this `i' will not be used, so there
17590 is no harm in resolving the types here. */
17591 tree type;
17593 /* Resolve the TYPENAME_TYPE. */
17594 type = resolve_typename_type (qualifying_scope,
17595 /*only_current_p=*/false);
17596 /* If that failed, the declarator is invalid. */
17597 if (TREE_CODE (type) == TYPENAME_TYPE)
17599 if (typedef_variant_p (type))
17600 error_at (declarator_id_start_token->location,
17601 "cannot define member of dependent typedef "
17602 "%qT", type);
17603 else
17604 error_at (declarator_id_start_token->location,
17605 "%<%T::%E%> is not a type",
17606 TYPE_CONTEXT (qualifying_scope),
17607 TYPE_IDENTIFIER (qualifying_scope));
17609 qualifying_scope = type;
17612 sfk = sfk_none;
17614 if (unqualified_name)
17616 tree class_type;
17618 if (qualifying_scope
17619 && CLASS_TYPE_P (qualifying_scope))
17620 class_type = qualifying_scope;
17621 else
17622 class_type = current_class_type;
17624 if (TREE_CODE (unqualified_name) == TYPE_DECL)
17626 tree name_type = TREE_TYPE (unqualified_name);
17627 if (class_type && same_type_p (name_type, class_type))
17629 if (qualifying_scope
17630 && CLASSTYPE_USE_TEMPLATE (name_type))
17632 error_at (declarator_id_start_token->location,
17633 "invalid use of constructor as a template");
17634 inform (declarator_id_start_token->location,
17635 "use %<%T::%D%> instead of %<%T::%D%> to "
17636 "name the constructor in a qualified name",
17637 class_type,
17638 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
17639 class_type, name_type);
17640 declarator = cp_error_declarator;
17641 break;
17643 else
17644 unqualified_name = constructor_name (class_type);
17646 else
17648 /* We do not attempt to print the declarator
17649 here because we do not have enough
17650 information about its original syntactic
17651 form. */
17652 cp_parser_error (parser, "invalid declarator");
17653 declarator = cp_error_declarator;
17654 break;
17658 if (class_type)
17660 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
17661 sfk = sfk_destructor;
17662 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
17663 sfk = sfk_conversion;
17664 else if (/* There's no way to declare a constructor
17665 for an anonymous type, even if the type
17666 got a name for linkage purposes. */
17667 !TYPE_WAS_ANONYMOUS (class_type)
17668 /* Handle correctly (c++/19200):
17670 struct S {
17671 struct T{};
17672 friend void S(T);
17675 and also:
17677 namespace N {
17678 void S();
17681 struct S {
17682 friend void N::S();
17683 }; */
17684 && !(friend_p
17685 && class_type != qualifying_scope)
17686 && constructor_name_p (unqualified_name,
17687 class_type))
17689 unqualified_name = constructor_name (class_type);
17690 sfk = sfk_constructor;
17692 else if (is_overloaded_fn (unqualified_name)
17693 && DECL_CONSTRUCTOR_P (get_first_fn
17694 (unqualified_name)))
17695 sfk = sfk_constructor;
17697 if (ctor_dtor_or_conv_p && sfk != sfk_none)
17698 *ctor_dtor_or_conv_p = -1;
17701 declarator = make_id_declarator (qualifying_scope,
17702 unqualified_name,
17703 sfk);
17704 declarator->std_attributes = attrs;
17705 declarator->id_loc = token->location;
17706 declarator->parameter_pack_p = pack_expansion_p;
17708 if (pack_expansion_p)
17709 maybe_warn_variadic_templates ();
17712 handle_declarator:;
17713 scope = get_scope_of_declarator (declarator);
17714 if (scope)
17716 /* Any names that appear after the declarator-id for a
17717 member are looked up in the containing scope. */
17718 if (at_function_scope_p ())
17720 /* But declarations with qualified-ids can't appear in a
17721 function. */
17722 cp_parser_error (parser, "qualified-id in declaration");
17723 declarator = cp_error_declarator;
17724 break;
17726 pushed_scope = push_scope (scope);
17728 parser->in_declarator_p = true;
17729 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
17730 || (declarator && declarator->kind == cdk_id))
17731 /* Default args are only allowed on function
17732 declarations. */
17733 parser->default_arg_ok_p = saved_default_arg_ok_p;
17734 else
17735 parser->default_arg_ok_p = false;
17737 first = false;
17739 /* We're done. */
17740 else
17741 break;
17744 /* For an abstract declarator, we might wind up with nothing at this
17745 point. That's an error; the declarator is not optional. */
17746 if (!declarator)
17747 cp_parser_error (parser, "expected declarator");
17749 /* If we entered a scope, we must exit it now. */
17750 if (pushed_scope)
17751 pop_scope (pushed_scope);
17753 parser->default_arg_ok_p = saved_default_arg_ok_p;
17754 parser->in_declarator_p = saved_in_declarator_p;
17756 return declarator;
17759 /* Parse a ptr-operator.
17761 ptr-operator:
17762 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17763 * cv-qualifier-seq [opt]
17765 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
17766 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
17768 GNU Extension:
17770 ptr-operator:
17771 & cv-qualifier-seq [opt]
17773 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
17774 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
17775 an rvalue reference. In the case of a pointer-to-member, *TYPE is
17776 filled in with the TYPE containing the member. *CV_QUALS is
17777 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
17778 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
17779 Note that the tree codes returned by this function have nothing
17780 to do with the types of trees that will be eventually be created
17781 to represent the pointer or reference type being parsed. They are
17782 just constants with suggestive names. */
17783 static enum tree_code
17784 cp_parser_ptr_operator (cp_parser* parser,
17785 tree* type,
17786 cp_cv_quals *cv_quals,
17787 tree *attributes)
17789 enum tree_code code = ERROR_MARK;
17790 cp_token *token;
17791 tree attrs = NULL_TREE;
17793 /* Assume that it's not a pointer-to-member. */
17794 *type = NULL_TREE;
17795 /* And that there are no cv-qualifiers. */
17796 *cv_quals = TYPE_UNQUALIFIED;
17798 /* Peek at the next token. */
17799 token = cp_lexer_peek_token (parser->lexer);
17801 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
17802 if (token->type == CPP_MULT)
17803 code = INDIRECT_REF;
17804 else if (token->type == CPP_AND)
17805 code = ADDR_EXPR;
17806 else if ((cxx_dialect != cxx98) &&
17807 token->type == CPP_AND_AND) /* C++0x only */
17808 code = NON_LVALUE_EXPR;
17810 if (code != ERROR_MARK)
17812 /* Consume the `*', `&' or `&&'. */
17813 cp_lexer_consume_token (parser->lexer);
17815 /* A `*' can be followed by a cv-qualifier-seq, and so can a
17816 `&', if we are allowing GNU extensions. (The only qualifier
17817 that can legally appear after `&' is `restrict', but that is
17818 enforced during semantic analysis. */
17819 if (code == INDIRECT_REF
17820 || cp_parser_allow_gnu_extensions_p (parser))
17821 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17823 attrs = cp_parser_std_attribute_spec_seq (parser);
17824 if (attributes != NULL)
17825 *attributes = attrs;
17827 else
17829 /* Try the pointer-to-member case. */
17830 cp_parser_parse_tentatively (parser);
17831 /* Look for the optional `::' operator. */
17832 cp_parser_global_scope_opt (parser,
17833 /*current_scope_valid_p=*/false);
17834 /* Look for the nested-name specifier. */
17835 token = cp_lexer_peek_token (parser->lexer);
17836 cp_parser_nested_name_specifier (parser,
17837 /*typename_keyword_p=*/false,
17838 /*check_dependency_p=*/true,
17839 /*type_p=*/false,
17840 /*is_declaration=*/false);
17841 /* If we found it, and the next token is a `*', then we are
17842 indeed looking at a pointer-to-member operator. */
17843 if (!cp_parser_error_occurred (parser)
17844 && cp_parser_require (parser, CPP_MULT, RT_MULT))
17846 /* Indicate that the `*' operator was used. */
17847 code = INDIRECT_REF;
17849 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
17850 error_at (token->location, "%qD is a namespace", parser->scope);
17851 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
17852 error_at (token->location, "cannot form pointer to member of "
17853 "non-class %q#T", parser->scope);
17854 else
17856 /* The type of which the member is a member is given by the
17857 current SCOPE. */
17858 *type = parser->scope;
17859 /* The next name will not be qualified. */
17860 parser->scope = NULL_TREE;
17861 parser->qualifying_scope = NULL_TREE;
17862 parser->object_scope = NULL_TREE;
17863 /* Look for optional c++11 attributes. */
17864 attrs = cp_parser_std_attribute_spec_seq (parser);
17865 if (attributes != NULL)
17866 *attributes = attrs;
17867 /* Look for the optional cv-qualifier-seq. */
17868 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
17871 /* If that didn't work we don't have a ptr-operator. */
17872 if (!cp_parser_parse_definitely (parser))
17873 cp_parser_error (parser, "expected ptr-operator");
17876 return code;
17879 /* Parse an (optional) cv-qualifier-seq.
17881 cv-qualifier-seq:
17882 cv-qualifier cv-qualifier-seq [opt]
17884 cv-qualifier:
17885 const
17886 volatile
17888 GNU Extension:
17890 cv-qualifier:
17891 __restrict__
17893 Returns a bitmask representing the cv-qualifiers. */
17895 static cp_cv_quals
17896 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
17898 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
17900 while (true)
17902 cp_token *token;
17903 cp_cv_quals cv_qualifier;
17905 /* Peek at the next token. */
17906 token = cp_lexer_peek_token (parser->lexer);
17907 /* See if it's a cv-qualifier. */
17908 switch (token->keyword)
17910 case RID_CONST:
17911 cv_qualifier = TYPE_QUAL_CONST;
17912 break;
17914 case RID_VOLATILE:
17915 cv_qualifier = TYPE_QUAL_VOLATILE;
17916 break;
17918 case RID_RESTRICT:
17919 cv_qualifier = TYPE_QUAL_RESTRICT;
17920 break;
17922 default:
17923 cv_qualifier = TYPE_UNQUALIFIED;
17924 break;
17927 if (!cv_qualifier)
17928 break;
17930 if (cv_quals & cv_qualifier)
17932 error_at (token->location, "duplicate cv-qualifier");
17933 cp_lexer_purge_token (parser->lexer);
17935 else
17937 cp_lexer_consume_token (parser->lexer);
17938 cv_quals |= cv_qualifier;
17942 return cv_quals;
17945 /* Parse an (optional) ref-qualifier
17947 ref-qualifier:
17951 Returns cp_ref_qualifier representing ref-qualifier. */
17953 static cp_ref_qualifier
17954 cp_parser_ref_qualifier_opt (cp_parser* parser)
17956 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
17958 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
17959 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
17960 return ref_qual;
17962 while (true)
17964 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
17965 cp_token *token = cp_lexer_peek_token (parser->lexer);
17967 switch (token->type)
17969 case CPP_AND:
17970 curr_ref_qual = REF_QUAL_LVALUE;
17971 break;
17973 case CPP_AND_AND:
17974 curr_ref_qual = REF_QUAL_RVALUE;
17975 break;
17977 default:
17978 curr_ref_qual = REF_QUAL_NONE;
17979 break;
17982 if (!curr_ref_qual)
17983 break;
17984 else if (ref_qual)
17986 error_at (token->location, "multiple ref-qualifiers");
17987 cp_lexer_purge_token (parser->lexer);
17989 else
17991 ref_qual = curr_ref_qual;
17992 cp_lexer_consume_token (parser->lexer);
17996 return ref_qual;
17999 /* Parse an (optional) virt-specifier-seq.
18001 virt-specifier-seq:
18002 virt-specifier virt-specifier-seq [opt]
18004 virt-specifier:
18005 override
18006 final
18008 Returns a bitmask representing the virt-specifiers. */
18010 static cp_virt_specifiers
18011 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
18013 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18015 while (true)
18017 cp_token *token;
18018 cp_virt_specifiers virt_specifier;
18020 /* Peek at the next token. */
18021 token = cp_lexer_peek_token (parser->lexer);
18022 /* See if it's a virt-specifier-qualifier. */
18023 if (token->type != CPP_NAME)
18024 break;
18025 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
18027 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18028 virt_specifier = VIRT_SPEC_OVERRIDE;
18030 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
18032 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
18033 virt_specifier = VIRT_SPEC_FINAL;
18035 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
18037 virt_specifier = VIRT_SPEC_FINAL;
18039 else
18040 break;
18042 if (virt_specifiers & virt_specifier)
18044 error_at (token->location, "duplicate virt-specifier");
18045 cp_lexer_purge_token (parser->lexer);
18047 else
18049 cp_lexer_consume_token (parser->lexer);
18050 virt_specifiers |= virt_specifier;
18053 return virt_specifiers;
18056 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
18057 is in scope even though it isn't real. */
18059 void
18060 inject_this_parameter (tree ctype, cp_cv_quals quals)
18062 tree this_parm;
18064 if (current_class_ptr)
18066 /* We don't clear this between NSDMIs. Is it already what we want? */
18067 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
18068 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
18069 && cp_type_quals (type) == quals)
18070 return;
18073 this_parm = build_this_parm (ctype, quals);
18074 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
18075 current_class_ptr = NULL_TREE;
18076 current_class_ref
18077 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
18078 current_class_ptr = this_parm;
18081 /* Return true iff our current scope is a non-static data member
18082 initializer. */
18084 bool
18085 parsing_nsdmi (void)
18087 /* We recognize NSDMI context by the context-less 'this' pointer set up
18088 by the function above. */
18089 if (current_class_ptr && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
18090 return true;
18091 return false;
18094 /* Parse a late-specified return type, if any. This is not a separate
18095 non-terminal, but part of a function declarator, which looks like
18097 -> trailing-type-specifier-seq abstract-declarator(opt)
18099 Returns the type indicated by the type-id.
18101 In addition to this this parses any queued up omp declare simd
18102 clauses and Cilk Plus SIMD-enabled function's vector attributes.
18104 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
18105 function. */
18107 static tree
18108 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
18109 cp_cv_quals quals)
18111 cp_token *token;
18112 tree type = NULL_TREE;
18113 bool declare_simd_p = (parser->omp_declare_simd
18114 && declarator
18115 && declarator->kind == cdk_id);
18117 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
18118 && declarator && declarator->kind == cdk_id);
18120 /* Peek at the next token. */
18121 token = cp_lexer_peek_token (parser->lexer);
18122 /* A late-specified return type is indicated by an initial '->'. */
18123 if (token->type != CPP_DEREF && !(declare_simd_p || cilk_simd_fn_vector_p))
18124 return NULL_TREE;
18126 tree save_ccp = current_class_ptr;
18127 tree save_ccr = current_class_ref;
18128 if (quals >= 0)
18130 /* DR 1207: 'this' is in scope in the trailing return type. */
18131 inject_this_parameter (current_class_type, quals);
18134 if (token->type == CPP_DEREF)
18136 /* Consume the ->. */
18137 cp_lexer_consume_token (parser->lexer);
18139 type = cp_parser_trailing_type_id (parser);
18142 if (cilk_simd_fn_vector_p)
18143 declarator->std_attributes
18144 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
18145 declarator->std_attributes);
18146 if (declare_simd_p)
18147 declarator->std_attributes
18148 = cp_parser_late_parsing_omp_declare_simd (parser,
18149 declarator->std_attributes);
18151 if (quals >= 0)
18153 current_class_ptr = save_ccp;
18154 current_class_ref = save_ccr;
18157 return type;
18160 /* Parse a declarator-id.
18162 declarator-id:
18163 id-expression
18164 :: [opt] nested-name-specifier [opt] type-name
18166 In the `id-expression' case, the value returned is as for
18167 cp_parser_id_expression if the id-expression was an unqualified-id.
18168 If the id-expression was a qualified-id, then a SCOPE_REF is
18169 returned. The first operand is the scope (either a NAMESPACE_DECL
18170 or TREE_TYPE), but the second is still just a representation of an
18171 unqualified-id. */
18173 static tree
18174 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
18176 tree id;
18177 /* The expression must be an id-expression. Assume that qualified
18178 names are the names of types so that:
18180 template <class T>
18181 int S<T>::R::i = 3;
18183 will work; we must treat `S<T>::R' as the name of a type.
18184 Similarly, assume that qualified names are templates, where
18185 required, so that:
18187 template <class T>
18188 int S<T>::R<T>::i = 3;
18190 will work, too. */
18191 id = cp_parser_id_expression (parser,
18192 /*template_keyword_p=*/false,
18193 /*check_dependency_p=*/false,
18194 /*template_p=*/NULL,
18195 /*declarator_p=*/true,
18196 optional_p);
18197 if (id && BASELINK_P (id))
18198 id = BASELINK_FUNCTIONS (id);
18199 return id;
18202 /* Parse a type-id.
18204 type-id:
18205 type-specifier-seq abstract-declarator [opt]
18207 Returns the TYPE specified. */
18209 static tree
18210 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
18211 bool is_trailing_return)
18213 cp_decl_specifier_seq type_specifier_seq;
18214 cp_declarator *abstract_declarator;
18216 /* Parse the type-specifier-seq. */
18217 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18218 is_trailing_return,
18219 &type_specifier_seq);
18220 if (type_specifier_seq.type == error_mark_node)
18221 return error_mark_node;
18223 /* There might or might not be an abstract declarator. */
18224 cp_parser_parse_tentatively (parser);
18225 /* Look for the declarator. */
18226 abstract_declarator
18227 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
18228 /*parenthesized_p=*/NULL,
18229 /*member_p=*/false,
18230 /*friend_p=*/false);
18231 /* Check to see if there really was a declarator. */
18232 if (!cp_parser_parse_definitely (parser))
18233 abstract_declarator = NULL;
18235 if (type_specifier_seq.type
18236 /* None of the valid uses of 'auto' in C++14 involve the type-id
18237 nonterminal, but it is valid in a trailing-return-type. */
18238 && !(cxx_dialect >= cxx14 && is_trailing_return)
18239 && type_uses_auto (type_specifier_seq.type))
18241 /* A type-id with type 'auto' is only ok if the abstract declarator
18242 is a function declarator with a late-specified return type. */
18243 if (abstract_declarator
18244 && abstract_declarator->kind == cdk_function
18245 && abstract_declarator->u.function.late_return_type)
18246 /* OK */;
18247 else
18249 error ("invalid use of %<auto%>");
18250 return error_mark_node;
18254 return groktypename (&type_specifier_seq, abstract_declarator,
18255 is_template_arg);
18258 static tree cp_parser_type_id (cp_parser *parser)
18260 return cp_parser_type_id_1 (parser, false, false);
18263 static tree cp_parser_template_type_arg (cp_parser *parser)
18265 tree r;
18266 const char *saved_message = parser->type_definition_forbidden_message;
18267 parser->type_definition_forbidden_message
18268 = G_("types may not be defined in template arguments");
18269 r = cp_parser_type_id_1 (parser, true, false);
18270 parser->type_definition_forbidden_message = saved_message;
18271 if (cxx_dialect >= cxx14 && type_uses_auto (r))
18273 error ("invalid use of %<auto%> in template argument");
18274 r = error_mark_node;
18276 return r;
18279 static tree cp_parser_trailing_type_id (cp_parser *parser)
18281 return cp_parser_type_id_1 (parser, false, true);
18284 /* Parse a type-specifier-seq.
18286 type-specifier-seq:
18287 type-specifier type-specifier-seq [opt]
18289 GNU extension:
18291 type-specifier-seq:
18292 attributes type-specifier-seq [opt]
18294 If IS_DECLARATION is true, we are at the start of a "condition" or
18295 exception-declaration, so we might be followed by a declarator-id.
18297 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
18298 i.e. we've just seen "->".
18300 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
18302 static void
18303 cp_parser_type_specifier_seq (cp_parser* parser,
18304 bool is_declaration,
18305 bool is_trailing_return,
18306 cp_decl_specifier_seq *type_specifier_seq)
18308 bool seen_type_specifier = false;
18309 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
18310 cp_token *start_token = NULL;
18312 /* Clear the TYPE_SPECIFIER_SEQ. */
18313 clear_decl_specs (type_specifier_seq);
18315 /* In the context of a trailing return type, enum E { } is an
18316 elaborated-type-specifier followed by a function-body, not an
18317 enum-specifier. */
18318 if (is_trailing_return)
18319 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
18321 /* Parse the type-specifiers and attributes. */
18322 while (true)
18324 tree type_specifier;
18325 bool is_cv_qualifier;
18327 /* Check for attributes first. */
18328 if (cp_next_tokens_can_be_attribute_p (parser))
18330 type_specifier_seq->attributes =
18331 chainon (type_specifier_seq->attributes,
18332 cp_parser_attributes_opt (parser));
18333 continue;
18336 /* record the token of the beginning of the type specifier seq,
18337 for error reporting purposes*/
18338 if (!start_token)
18339 start_token = cp_lexer_peek_token (parser->lexer);
18341 /* Look for the type-specifier. */
18342 type_specifier = cp_parser_type_specifier (parser,
18343 flags,
18344 type_specifier_seq,
18345 /*is_declaration=*/false,
18346 NULL,
18347 &is_cv_qualifier);
18348 if (!type_specifier)
18350 /* If the first type-specifier could not be found, this is not a
18351 type-specifier-seq at all. */
18352 if (!seen_type_specifier)
18354 /* Set in_declarator_p to avoid skipping to the semicolon. */
18355 int in_decl = parser->in_declarator_p;
18356 parser->in_declarator_p = true;
18358 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
18359 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
18360 cp_parser_error (parser, "expected type-specifier");
18362 parser->in_declarator_p = in_decl;
18364 type_specifier_seq->type = error_mark_node;
18365 return;
18367 /* If subsequent type-specifiers could not be found, the
18368 type-specifier-seq is complete. */
18369 break;
18372 seen_type_specifier = true;
18373 /* The standard says that a condition can be:
18375 type-specifier-seq declarator = assignment-expression
18377 However, given:
18379 struct S {};
18380 if (int S = ...)
18382 we should treat the "S" as a declarator, not as a
18383 type-specifier. The standard doesn't say that explicitly for
18384 type-specifier-seq, but it does say that for
18385 decl-specifier-seq in an ordinary declaration. Perhaps it
18386 would be clearer just to allow a decl-specifier-seq here, and
18387 then add a semantic restriction that if any decl-specifiers
18388 that are not type-specifiers appear, the program is invalid. */
18389 if (is_declaration && !is_cv_qualifier)
18390 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
18394 /* Return whether the function currently being declared has an associated
18395 template parameter list. */
18397 static bool
18398 function_being_declared_is_template_p (cp_parser* parser)
18400 if (!current_template_parms || processing_template_parmlist)
18401 return false;
18403 if (parser->implicit_template_scope)
18404 return true;
18406 if (at_class_scope_p ()
18407 && TYPE_BEING_DEFINED (current_class_type))
18408 return parser->num_template_parameter_lists != 0;
18410 return ((int) parser->num_template_parameter_lists > template_class_depth
18411 (current_class_type));
18414 /* Parse a parameter-declaration-clause.
18416 parameter-declaration-clause:
18417 parameter-declaration-list [opt] ... [opt]
18418 parameter-declaration-list , ...
18420 Returns a representation for the parameter declarations. A return
18421 value of NULL indicates a parameter-declaration-clause consisting
18422 only of an ellipsis. */
18424 static tree
18425 cp_parser_parameter_declaration_clause (cp_parser* parser)
18427 tree parameters;
18428 cp_token *token;
18429 bool ellipsis_p;
18430 bool is_error;
18432 struct cleanup {
18433 cp_parser* parser;
18434 int auto_is_implicit_function_template_parm_p;
18435 ~cleanup() {
18436 parser->auto_is_implicit_function_template_parm_p
18437 = auto_is_implicit_function_template_parm_p;
18439 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
18441 (void) cleanup;
18443 if (!processing_specialization
18444 && !processing_template_parmlist
18445 && !processing_explicit_instantiation)
18446 if (!current_function_decl
18447 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
18448 parser->auto_is_implicit_function_template_parm_p = true;
18450 /* Peek at the next token. */
18451 token = cp_lexer_peek_token (parser->lexer);
18452 /* Check for trivial parameter-declaration-clauses. */
18453 if (token->type == CPP_ELLIPSIS)
18455 /* Consume the `...' token. */
18456 cp_lexer_consume_token (parser->lexer);
18457 return NULL_TREE;
18459 else if (token->type == CPP_CLOSE_PAREN)
18460 /* There are no parameters. */
18462 #ifndef NO_IMPLICIT_EXTERN_C
18463 if (in_system_header_at (input_location)
18464 && current_class_type == NULL
18465 && current_lang_name == lang_name_c)
18466 return NULL_TREE;
18467 else
18468 #endif
18469 return void_list_node;
18471 /* Check for `(void)', too, which is a special case. */
18472 else if (token->keyword == RID_VOID
18473 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18474 == CPP_CLOSE_PAREN))
18476 /* Consume the `void' token. */
18477 cp_lexer_consume_token (parser->lexer);
18478 /* There are no parameters. */
18479 return void_list_node;
18482 /* Parse the parameter-declaration-list. */
18483 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
18484 /* If a parse error occurred while parsing the
18485 parameter-declaration-list, then the entire
18486 parameter-declaration-clause is erroneous. */
18487 if (is_error)
18488 return NULL;
18490 /* Peek at the next token. */
18491 token = cp_lexer_peek_token (parser->lexer);
18492 /* If it's a `,', the clause should terminate with an ellipsis. */
18493 if (token->type == CPP_COMMA)
18495 /* Consume the `,'. */
18496 cp_lexer_consume_token (parser->lexer);
18497 /* Expect an ellipsis. */
18498 ellipsis_p
18499 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
18501 /* It might also be `...' if the optional trailing `,' was
18502 omitted. */
18503 else if (token->type == CPP_ELLIPSIS)
18505 /* Consume the `...' token. */
18506 cp_lexer_consume_token (parser->lexer);
18507 /* And remember that we saw it. */
18508 ellipsis_p = true;
18510 else
18511 ellipsis_p = false;
18513 /* Finish the parameter list. */
18514 if (!ellipsis_p)
18515 parameters = chainon (parameters, void_list_node);
18517 return parameters;
18520 /* Parse a parameter-declaration-list.
18522 parameter-declaration-list:
18523 parameter-declaration
18524 parameter-declaration-list , parameter-declaration
18526 Returns a representation of the parameter-declaration-list, as for
18527 cp_parser_parameter_declaration_clause. However, the
18528 `void_list_node' is never appended to the list. Upon return,
18529 *IS_ERROR will be true iff an error occurred. */
18531 static tree
18532 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
18534 tree parameters = NULL_TREE;
18535 tree *tail = &parameters;
18536 bool saved_in_unbraced_linkage_specification_p;
18537 int index = 0;
18539 /* Assume all will go well. */
18540 *is_error = false;
18541 /* The special considerations that apply to a function within an
18542 unbraced linkage specifications do not apply to the parameters
18543 to the function. */
18544 saved_in_unbraced_linkage_specification_p
18545 = parser->in_unbraced_linkage_specification_p;
18546 parser->in_unbraced_linkage_specification_p = false;
18548 /* Look for more parameters. */
18549 while (true)
18551 cp_parameter_declarator *parameter;
18552 tree decl = error_mark_node;
18553 bool parenthesized_p = false;
18554 int template_parm_idx = (function_being_declared_is_template_p (parser)?
18555 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
18556 (current_template_parms)) : 0);
18558 /* Parse the parameter. */
18559 parameter
18560 = cp_parser_parameter_declaration (parser,
18561 /*template_parm_p=*/false,
18562 &parenthesized_p);
18564 /* We don't know yet if the enclosing context is deprecated, so wait
18565 and warn in grokparms if appropriate. */
18566 deprecated_state = DEPRECATED_SUPPRESS;
18568 if (parameter)
18570 /* If a function parameter pack was specified and an implicit template
18571 parameter was introduced during cp_parser_parameter_declaration,
18572 change any implicit parameters introduced into packs. */
18573 if (parser->implicit_template_parms
18574 && parameter->declarator
18575 && parameter->declarator->parameter_pack_p)
18577 int latest_template_parm_idx = TREE_VEC_LENGTH
18578 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
18580 if (latest_template_parm_idx != template_parm_idx)
18581 parameter->decl_specifiers.type = convert_generic_types_to_packs
18582 (parameter->decl_specifiers.type,
18583 template_parm_idx, latest_template_parm_idx);
18586 decl = grokdeclarator (parameter->declarator,
18587 &parameter->decl_specifiers,
18588 PARM,
18589 parameter->default_argument != NULL_TREE,
18590 &parameter->decl_specifiers.attributes);
18593 deprecated_state = DEPRECATED_NORMAL;
18595 /* If a parse error occurred parsing the parameter declaration,
18596 then the entire parameter-declaration-list is erroneous. */
18597 if (decl == error_mark_node)
18599 *is_error = true;
18600 parameters = error_mark_node;
18601 break;
18604 if (parameter->decl_specifiers.attributes)
18605 cplus_decl_attributes (&decl,
18606 parameter->decl_specifiers.attributes,
18608 if (DECL_NAME (decl))
18609 decl = pushdecl (decl);
18611 if (decl != error_mark_node)
18613 retrofit_lang_decl (decl);
18614 DECL_PARM_INDEX (decl) = ++index;
18615 DECL_PARM_LEVEL (decl) = function_parm_depth ();
18618 /* Add the new parameter to the list. */
18619 *tail = build_tree_list (parameter->default_argument, decl);
18620 tail = &TREE_CHAIN (*tail);
18622 /* Peek at the next token. */
18623 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
18624 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
18625 /* These are for Objective-C++ */
18626 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18627 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18628 /* The parameter-declaration-list is complete. */
18629 break;
18630 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18632 cp_token *token;
18634 /* Peek at the next token. */
18635 token = cp_lexer_peek_nth_token (parser->lexer, 2);
18636 /* If it's an ellipsis, then the list is complete. */
18637 if (token->type == CPP_ELLIPSIS)
18638 break;
18639 /* Otherwise, there must be more parameters. Consume the
18640 `,'. */
18641 cp_lexer_consume_token (parser->lexer);
18642 /* When parsing something like:
18644 int i(float f, double d)
18646 we can tell after seeing the declaration for "f" that we
18647 are not looking at an initialization of a variable "i",
18648 but rather at the declaration of a function "i".
18650 Due to the fact that the parsing of template arguments
18651 (as specified to a template-id) requires backtracking we
18652 cannot use this technique when inside a template argument
18653 list. */
18654 if (!parser->in_template_argument_list_p
18655 && !parser->in_type_id_in_expr_p
18656 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18657 /* However, a parameter-declaration of the form
18658 "float(f)" (which is a valid declaration of a
18659 parameter "f") can also be interpreted as an
18660 expression (the conversion of "f" to "float"). */
18661 && !parenthesized_p)
18662 cp_parser_commit_to_tentative_parse (parser);
18664 else
18666 cp_parser_error (parser, "expected %<,%> or %<...%>");
18667 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18668 cp_parser_skip_to_closing_parenthesis (parser,
18669 /*recovering=*/true,
18670 /*or_comma=*/false,
18671 /*consume_paren=*/false);
18672 break;
18676 parser->in_unbraced_linkage_specification_p
18677 = saved_in_unbraced_linkage_specification_p;
18679 /* Reset implicit_template_scope if we are about to leave the function
18680 parameter list that introduced it. Note that for out-of-line member
18681 definitions, there will be one or more class scopes before we get to
18682 the template parameter scope. */
18684 if (cp_binding_level *its = parser->implicit_template_scope)
18685 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
18687 while (maybe_its->kind == sk_class)
18688 maybe_its = maybe_its->level_chain;
18689 if (maybe_its == its)
18691 parser->implicit_template_parms = 0;
18692 parser->implicit_template_scope = 0;
18696 return parameters;
18699 /* Parse a parameter declaration.
18701 parameter-declaration:
18702 decl-specifier-seq ... [opt] declarator
18703 decl-specifier-seq declarator = assignment-expression
18704 decl-specifier-seq ... [opt] abstract-declarator [opt]
18705 decl-specifier-seq abstract-declarator [opt] = assignment-expression
18707 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
18708 declares a template parameter. (In that case, a non-nested `>'
18709 token encountered during the parsing of the assignment-expression
18710 is not interpreted as a greater-than operator.)
18712 Returns a representation of the parameter, or NULL if an error
18713 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
18714 true iff the declarator is of the form "(p)". */
18716 static cp_parameter_declarator *
18717 cp_parser_parameter_declaration (cp_parser *parser,
18718 bool template_parm_p,
18719 bool *parenthesized_p)
18721 int declares_class_or_enum;
18722 cp_decl_specifier_seq decl_specifiers;
18723 cp_declarator *declarator;
18724 tree default_argument;
18725 cp_token *token = NULL, *declarator_token_start = NULL;
18726 const char *saved_message;
18728 /* In a template parameter, `>' is not an operator.
18730 [temp.param]
18732 When parsing a default template-argument for a non-type
18733 template-parameter, the first non-nested `>' is taken as the end
18734 of the template parameter-list rather than a greater-than
18735 operator. */
18737 /* Type definitions may not appear in parameter types. */
18738 saved_message = parser->type_definition_forbidden_message;
18739 parser->type_definition_forbidden_message
18740 = G_("types may not be defined in parameter types");
18742 /* Parse the declaration-specifiers. */
18743 cp_parser_decl_specifier_seq (parser,
18744 CP_PARSER_FLAGS_NONE,
18745 &decl_specifiers,
18746 &declares_class_or_enum);
18748 /* Complain about missing 'typename' or other invalid type names. */
18749 if (!decl_specifiers.any_type_specifiers_p
18750 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18751 decl_specifiers.type = error_mark_node;
18753 /* If an error occurred, there's no reason to attempt to parse the
18754 rest of the declaration. */
18755 if (cp_parser_error_occurred (parser))
18757 parser->type_definition_forbidden_message = saved_message;
18758 return NULL;
18761 /* Peek at the next token. */
18762 token = cp_lexer_peek_token (parser->lexer);
18764 /* If the next token is a `)', `,', `=', `>', or `...', then there
18765 is no declarator. However, when variadic templates are enabled,
18766 there may be a declarator following `...'. */
18767 if (token->type == CPP_CLOSE_PAREN
18768 || token->type == CPP_COMMA
18769 || token->type == CPP_EQ
18770 || token->type == CPP_GREATER)
18772 declarator = NULL;
18773 if (parenthesized_p)
18774 *parenthesized_p = false;
18776 /* Otherwise, there should be a declarator. */
18777 else
18779 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18780 parser->default_arg_ok_p = false;
18782 /* After seeing a decl-specifier-seq, if the next token is not a
18783 "(", there is no possibility that the code is a valid
18784 expression. Therefore, if parsing tentatively, we commit at
18785 this point. */
18786 if (!parser->in_template_argument_list_p
18787 /* In an expression context, having seen:
18789 (int((char ...
18791 we cannot be sure whether we are looking at a
18792 function-type (taking a "char" as a parameter) or a cast
18793 of some object of type "char" to "int". */
18794 && !parser->in_type_id_in_expr_p
18795 && cp_parser_uncommitted_to_tentative_parse_p (parser)
18796 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18797 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
18798 cp_parser_commit_to_tentative_parse (parser);
18799 /* Parse the declarator. */
18800 declarator_token_start = token;
18801 declarator = cp_parser_declarator (parser,
18802 CP_PARSER_DECLARATOR_EITHER,
18803 /*ctor_dtor_or_conv_p=*/NULL,
18804 parenthesized_p,
18805 /*member_p=*/false,
18806 /*friend_p=*/false);
18807 parser->default_arg_ok_p = saved_default_arg_ok_p;
18808 /* After the declarator, allow more attributes. */
18809 decl_specifiers.attributes
18810 = chainon (decl_specifiers.attributes,
18811 cp_parser_attributes_opt (parser));
18814 /* If the next token is an ellipsis, and we have not seen a
18815 declarator name, and the type of the declarator contains parameter
18816 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
18817 a parameter pack expansion expression. Otherwise, leave the
18818 ellipsis for a C-style variadic function. */
18819 token = cp_lexer_peek_token (parser->lexer);
18820 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18822 tree type = decl_specifiers.type;
18824 if (type && DECL_P (type))
18825 type = TREE_TYPE (type);
18827 if (type
18828 && TREE_CODE (type) != TYPE_PACK_EXPANSION
18829 && declarator_can_be_parameter_pack (declarator)
18830 && (!declarator || !declarator->parameter_pack_p)
18831 && uses_parameter_packs (type))
18833 /* Consume the `...'. */
18834 cp_lexer_consume_token (parser->lexer);
18835 maybe_warn_variadic_templates ();
18837 /* Build a pack expansion type */
18838 if (declarator)
18839 declarator->parameter_pack_p = true;
18840 else
18841 decl_specifiers.type = make_pack_expansion (type);
18845 /* The restriction on defining new types applies only to the type
18846 of the parameter, not to the default argument. */
18847 parser->type_definition_forbidden_message = saved_message;
18849 /* If the next token is `=', then process a default argument. */
18850 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18852 token = cp_lexer_peek_token (parser->lexer);
18853 /* If we are defining a class, then the tokens that make up the
18854 default argument must be saved and processed later. */
18855 if (!template_parm_p && at_class_scope_p ()
18856 && TYPE_BEING_DEFINED (current_class_type)
18857 && !LAMBDA_TYPE_P (current_class_type))
18858 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
18859 /* Outside of a class definition, we can just parse the
18860 assignment-expression. */
18861 else
18862 default_argument
18863 = cp_parser_default_argument (parser, template_parm_p);
18865 if (!parser->default_arg_ok_p)
18867 if (flag_permissive)
18868 warning (0, "deprecated use of default argument for parameter of non-function");
18869 else
18871 error_at (token->location,
18872 "default arguments are only "
18873 "permitted for function parameters");
18874 default_argument = NULL_TREE;
18877 else if ((declarator && declarator->parameter_pack_p)
18878 || (decl_specifiers.type
18879 && PACK_EXPANSION_P (decl_specifiers.type)))
18881 /* Find the name of the parameter pack. */
18882 cp_declarator *id_declarator = declarator;
18883 while (id_declarator && id_declarator->kind != cdk_id)
18884 id_declarator = id_declarator->declarator;
18886 if (id_declarator && id_declarator->kind == cdk_id)
18887 error_at (declarator_token_start->location,
18888 template_parm_p
18889 ? G_("template parameter pack %qD "
18890 "cannot have a default argument")
18891 : G_("parameter pack %qD cannot have "
18892 "a default argument"),
18893 id_declarator->u.id.unqualified_name);
18894 else
18895 error_at (declarator_token_start->location,
18896 template_parm_p
18897 ? G_("template parameter pack cannot have "
18898 "a default argument")
18899 : G_("parameter pack cannot have a "
18900 "default argument"));
18902 default_argument = NULL_TREE;
18905 else
18906 default_argument = NULL_TREE;
18908 return make_parameter_declarator (&decl_specifiers,
18909 declarator,
18910 default_argument);
18913 /* Parse a default argument and return it.
18915 TEMPLATE_PARM_P is true if this is a default argument for a
18916 non-type template parameter. */
18917 static tree
18918 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
18920 tree default_argument = NULL_TREE;
18921 bool saved_greater_than_is_operator_p;
18922 bool saved_local_variables_forbidden_p;
18923 bool non_constant_p, is_direct_init;
18925 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
18926 set correctly. */
18927 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
18928 parser->greater_than_is_operator_p = !template_parm_p;
18929 /* Local variable names (and the `this' keyword) may not
18930 appear in a default argument. */
18931 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18932 parser->local_variables_forbidden_p = true;
18933 /* Parse the assignment-expression. */
18934 if (template_parm_p)
18935 push_deferring_access_checks (dk_no_deferred);
18936 tree saved_class_ptr = NULL_TREE;
18937 tree saved_class_ref = NULL_TREE;
18938 /* The "this" pointer is not valid in a default argument. */
18939 if (cfun)
18941 saved_class_ptr = current_class_ptr;
18942 cp_function_chain->x_current_class_ptr = NULL_TREE;
18943 saved_class_ref = current_class_ref;
18944 cp_function_chain->x_current_class_ref = NULL_TREE;
18946 default_argument
18947 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
18948 /* Restore the "this" pointer. */
18949 if (cfun)
18951 cp_function_chain->x_current_class_ptr = saved_class_ptr;
18952 cp_function_chain->x_current_class_ref = saved_class_ref;
18954 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
18955 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18956 if (template_parm_p)
18957 pop_deferring_access_checks ();
18958 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
18959 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18961 return default_argument;
18964 /* Parse a function-body.
18966 function-body:
18967 compound_statement */
18969 static void
18970 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
18972 cp_parser_compound_statement (parser, NULL, in_function_try_block, true);
18975 /* Parse a ctor-initializer-opt followed by a function-body. Return
18976 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
18977 is true we are parsing a function-try-block. */
18979 static bool
18980 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
18981 bool in_function_try_block)
18983 tree body, list;
18984 bool ctor_initializer_p;
18985 const bool check_body_p =
18986 DECL_CONSTRUCTOR_P (current_function_decl)
18987 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
18988 tree last = NULL;
18990 /* Begin the function body. */
18991 body = begin_function_body ();
18992 /* Parse the optional ctor-initializer. */
18993 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
18995 /* If we're parsing a constexpr constructor definition, we need
18996 to check that the constructor body is indeed empty. However,
18997 before we get to cp_parser_function_body lot of junk has been
18998 generated, so we can't just check that we have an empty block.
18999 Rather we take a snapshot of the outermost block, and check whether
19000 cp_parser_function_body changed its state. */
19001 if (check_body_p)
19003 list = cur_stmt_list;
19004 if (STATEMENT_LIST_TAIL (list))
19005 last = STATEMENT_LIST_TAIL (list)->stmt;
19007 /* Parse the function-body. */
19008 cp_parser_function_body (parser, in_function_try_block);
19009 if (check_body_p)
19010 check_constexpr_ctor_body (last, list);
19011 /* Finish the function body. */
19012 finish_function_body (body);
19014 return ctor_initializer_p;
19017 /* Parse an initializer.
19019 initializer:
19020 = initializer-clause
19021 ( expression-list )
19023 Returns an expression representing the initializer. If no
19024 initializer is present, NULL_TREE is returned.
19026 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
19027 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
19028 set to TRUE if there is no initializer present. If there is an
19029 initializer, and it is not a constant-expression, *NON_CONSTANT_P
19030 is set to true; otherwise it is set to false. */
19032 static tree
19033 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
19034 bool* non_constant_p)
19036 cp_token *token;
19037 tree init;
19039 /* Peek at the next token. */
19040 token = cp_lexer_peek_token (parser->lexer);
19042 /* Let our caller know whether or not this initializer was
19043 parenthesized. */
19044 *is_direct_init = (token->type != CPP_EQ);
19045 /* Assume that the initializer is constant. */
19046 *non_constant_p = false;
19048 if (token->type == CPP_EQ)
19050 /* Consume the `='. */
19051 cp_lexer_consume_token (parser->lexer);
19052 /* Parse the initializer-clause. */
19053 init = cp_parser_initializer_clause (parser, non_constant_p);
19055 else if (token->type == CPP_OPEN_PAREN)
19057 vec<tree, va_gc> *vec;
19058 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
19059 /*cast_p=*/false,
19060 /*allow_expansion_p=*/true,
19061 non_constant_p);
19062 if (vec == NULL)
19063 return error_mark_node;
19064 init = build_tree_list_vec (vec);
19065 release_tree_vector (vec);
19067 else if (token->type == CPP_OPEN_BRACE)
19069 cp_lexer_set_source_position (parser->lexer);
19070 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
19071 init = cp_parser_braced_list (parser, non_constant_p);
19072 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
19074 else
19076 /* Anything else is an error. */
19077 cp_parser_error (parser, "expected initializer");
19078 init = error_mark_node;
19081 return init;
19084 /* Parse an initializer-clause.
19086 initializer-clause:
19087 assignment-expression
19088 braced-init-list
19090 Returns an expression representing the initializer.
19092 If the `assignment-expression' production is used the value
19093 returned is simply a representation for the expression.
19095 Otherwise, calls cp_parser_braced_list. */
19097 static tree
19098 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
19100 tree initializer;
19102 /* Assume the expression is constant. */
19103 *non_constant_p = false;
19105 /* If it is not a `{', then we are looking at an
19106 assignment-expression. */
19107 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
19109 initializer
19110 = cp_parser_constant_expression (parser,
19111 /*allow_non_constant_p=*/true,
19112 non_constant_p);
19114 else
19115 initializer = cp_parser_braced_list (parser, non_constant_p);
19117 return initializer;
19120 /* Parse a brace-enclosed initializer list.
19122 braced-init-list:
19123 { initializer-list , [opt] }
19126 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
19127 the elements of the initializer-list (or NULL, if the last
19128 production is used). The TREE_TYPE for the CONSTRUCTOR will be
19129 NULL_TREE. There is no way to detect whether or not the optional
19130 trailing `,' was provided. NON_CONSTANT_P is as for
19131 cp_parser_initializer. */
19133 static tree
19134 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
19136 tree initializer;
19138 /* Consume the `{' token. */
19139 cp_lexer_consume_token (parser->lexer);
19140 /* Create a CONSTRUCTOR to represent the braced-initializer. */
19141 initializer = make_node (CONSTRUCTOR);
19142 /* If it's not a `}', then there is a non-trivial initializer. */
19143 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
19145 /* Parse the initializer list. */
19146 CONSTRUCTOR_ELTS (initializer)
19147 = cp_parser_initializer_list (parser, non_constant_p);
19148 /* A trailing `,' token is allowed. */
19149 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19150 cp_lexer_consume_token (parser->lexer);
19152 else
19153 *non_constant_p = false;
19154 /* Now, there should be a trailing `}'. */
19155 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19156 TREE_TYPE (initializer) = init_list_type_node;
19157 return initializer;
19160 /* Parse an initializer-list.
19162 initializer-list:
19163 initializer-clause ... [opt]
19164 initializer-list , initializer-clause ... [opt]
19166 GNU Extension:
19168 initializer-list:
19169 designation initializer-clause ...[opt]
19170 initializer-list , designation initializer-clause ...[opt]
19172 designation:
19173 . identifier =
19174 identifier :
19175 [ constant-expression ] =
19177 Returns a vec of constructor_elt. The VALUE of each elt is an expression
19178 for the initializer. If the INDEX of the elt is non-NULL, it is the
19179 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
19180 as for cp_parser_initializer. */
19182 static vec<constructor_elt, va_gc> *
19183 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
19185 vec<constructor_elt, va_gc> *v = NULL;
19187 /* Assume all of the expressions are constant. */
19188 *non_constant_p = false;
19190 /* Parse the rest of the list. */
19191 while (true)
19193 cp_token *token;
19194 tree designator;
19195 tree initializer;
19196 bool clause_non_constant_p;
19198 /* If the next token is an identifier and the following one is a
19199 colon, we are looking at the GNU designated-initializer
19200 syntax. */
19201 if (cp_parser_allow_gnu_extensions_p (parser)
19202 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
19203 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
19205 /* Warn the user that they are using an extension. */
19206 pedwarn (input_location, OPT_Wpedantic,
19207 "ISO C++ does not allow designated initializers");
19208 /* Consume the identifier. */
19209 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19210 /* Consume the `:'. */
19211 cp_lexer_consume_token (parser->lexer);
19213 /* Also handle the C99 syntax, '. id ='. */
19214 else if (cp_parser_allow_gnu_extensions_p (parser)
19215 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
19216 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
19217 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
19219 /* Warn the user that they are using an extension. */
19220 pedwarn (input_location, OPT_Wpedantic,
19221 "ISO C++ does not allow C99 designated initializers");
19222 /* Consume the `.'. */
19223 cp_lexer_consume_token (parser->lexer);
19224 /* Consume the identifier. */
19225 designator = cp_lexer_consume_token (parser->lexer)->u.value;
19226 /* Consume the `='. */
19227 cp_lexer_consume_token (parser->lexer);
19229 /* Also handle C99 array designators, '[ const ] ='. */
19230 else if (cp_parser_allow_gnu_extensions_p (parser)
19231 && !c_dialect_objc ()
19232 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19234 /* In C++11, [ could start a lambda-introducer. */
19235 bool non_const = false;
19237 cp_parser_parse_tentatively (parser);
19238 cp_lexer_consume_token (parser->lexer);
19239 designator = cp_parser_constant_expression (parser, true, &non_const);
19240 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19241 cp_parser_require (parser, CPP_EQ, RT_EQ);
19242 if (!cp_parser_parse_definitely (parser))
19243 designator = NULL_TREE;
19244 else if (non_const)
19245 require_potential_rvalue_constant_expression (designator);
19247 else
19248 designator = NULL_TREE;
19250 /* Parse the initializer. */
19251 initializer = cp_parser_initializer_clause (parser,
19252 &clause_non_constant_p);
19253 /* If any clause is non-constant, so is the entire initializer. */
19254 if (clause_non_constant_p)
19255 *non_constant_p = true;
19257 /* If we have an ellipsis, this is an initializer pack
19258 expansion. */
19259 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19261 /* Consume the `...'. */
19262 cp_lexer_consume_token (parser->lexer);
19264 /* Turn the initializer into an initializer expansion. */
19265 initializer = make_pack_expansion (initializer);
19268 /* Add it to the vector. */
19269 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
19271 /* If the next token is not a comma, we have reached the end of
19272 the list. */
19273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19274 break;
19276 /* Peek at the next token. */
19277 token = cp_lexer_peek_nth_token (parser->lexer, 2);
19278 /* If the next token is a `}', then we're still done. An
19279 initializer-clause can have a trailing `,' after the
19280 initializer-list and before the closing `}'. */
19281 if (token->type == CPP_CLOSE_BRACE)
19282 break;
19284 /* Consume the `,' token. */
19285 cp_lexer_consume_token (parser->lexer);
19288 return v;
19291 /* Classes [gram.class] */
19293 /* Parse a class-name.
19295 class-name:
19296 identifier
19297 template-id
19299 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
19300 to indicate that names looked up in dependent types should be
19301 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
19302 keyword has been used to indicate that the name that appears next
19303 is a template. TAG_TYPE indicates the explicit tag given before
19304 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
19305 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
19306 is the class being defined in a class-head.
19308 Returns the TYPE_DECL representing the class. */
19310 static tree
19311 cp_parser_class_name (cp_parser *parser,
19312 bool typename_keyword_p,
19313 bool template_keyword_p,
19314 enum tag_types tag_type,
19315 bool check_dependency_p,
19316 bool class_head_p,
19317 bool is_declaration)
19319 tree decl;
19320 tree scope;
19321 bool typename_p;
19322 cp_token *token;
19323 tree identifier = NULL_TREE;
19325 /* All class-names start with an identifier. */
19326 token = cp_lexer_peek_token (parser->lexer);
19327 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
19329 cp_parser_error (parser, "expected class-name");
19330 return error_mark_node;
19333 /* PARSER->SCOPE can be cleared when parsing the template-arguments
19334 to a template-id, so we save it here. */
19335 scope = parser->scope;
19336 if (scope == error_mark_node)
19337 return error_mark_node;
19339 /* Any name names a type if we're following the `typename' keyword
19340 in a qualified name where the enclosing scope is type-dependent. */
19341 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
19342 && dependent_type_p (scope));
19343 /* Handle the common case (an identifier, but not a template-id)
19344 efficiently. */
19345 if (token->type == CPP_NAME
19346 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
19348 cp_token *identifier_token;
19349 bool ambiguous_p;
19351 /* Look for the identifier. */
19352 identifier_token = cp_lexer_peek_token (parser->lexer);
19353 ambiguous_p = identifier_token->error_reported;
19354 identifier = cp_parser_identifier (parser);
19355 /* If the next token isn't an identifier, we are certainly not
19356 looking at a class-name. */
19357 if (identifier == error_mark_node)
19358 decl = error_mark_node;
19359 /* If we know this is a type-name, there's no need to look it
19360 up. */
19361 else if (typename_p)
19362 decl = identifier;
19363 else
19365 tree ambiguous_decls;
19366 /* If we already know that this lookup is ambiguous, then
19367 we've already issued an error message; there's no reason
19368 to check again. */
19369 if (ambiguous_p)
19371 cp_parser_simulate_error (parser);
19372 return error_mark_node;
19374 /* If the next token is a `::', then the name must be a type
19375 name.
19377 [basic.lookup.qual]
19379 During the lookup for a name preceding the :: scope
19380 resolution operator, object, function, and enumerator
19381 names are ignored. */
19382 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19383 tag_type = typename_type;
19384 /* Look up the name. */
19385 decl = cp_parser_lookup_name (parser, identifier,
19386 tag_type,
19387 /*is_template=*/false,
19388 /*is_namespace=*/false,
19389 check_dependency_p,
19390 &ambiguous_decls,
19391 identifier_token->location);
19392 if (ambiguous_decls)
19394 if (cp_parser_parsing_tentatively (parser))
19395 cp_parser_simulate_error (parser);
19396 return error_mark_node;
19400 else
19402 /* Try a template-id. */
19403 decl = cp_parser_template_id (parser, template_keyword_p,
19404 check_dependency_p,
19405 tag_type,
19406 is_declaration);
19407 if (decl == error_mark_node)
19408 return error_mark_node;
19411 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
19413 /* If this is a typename, create a TYPENAME_TYPE. */
19414 if (typename_p && decl != error_mark_node)
19416 decl = make_typename_type (scope, decl, typename_type,
19417 /*complain=*/tf_error);
19418 if (decl != error_mark_node)
19419 decl = TYPE_NAME (decl);
19422 decl = strip_using_decl (decl);
19424 /* Check to see that it is really the name of a class. */
19425 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
19426 && identifier_p (TREE_OPERAND (decl, 0))
19427 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19428 /* Situations like this:
19430 template <typename T> struct A {
19431 typename T::template X<int>::I i;
19434 are problematic. Is `T::template X<int>' a class-name? The
19435 standard does not seem to be definitive, but there is no other
19436 valid interpretation of the following `::'. Therefore, those
19437 names are considered class-names. */
19439 decl = make_typename_type (scope, decl, tag_type, tf_error);
19440 if (decl != error_mark_node)
19441 decl = TYPE_NAME (decl);
19443 else if (TREE_CODE (decl) != TYPE_DECL
19444 || TREE_TYPE (decl) == error_mark_node
19445 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
19446 /* In Objective-C 2.0, a classname followed by '.' starts a
19447 dot-syntax expression, and it's not a type-name. */
19448 || (c_dialect_objc ()
19449 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
19450 && objc_is_class_name (decl)))
19451 decl = error_mark_node;
19453 if (decl == error_mark_node)
19454 cp_parser_error (parser, "expected class-name");
19455 else if (identifier && !parser->scope)
19456 maybe_note_name_used_in_class (identifier, decl);
19458 return decl;
19461 /* Parse a class-specifier.
19463 class-specifier:
19464 class-head { member-specification [opt] }
19466 Returns the TREE_TYPE representing the class. */
19468 static tree
19469 cp_parser_class_specifier_1 (cp_parser* parser)
19471 tree type;
19472 tree attributes = NULL_TREE;
19473 bool nested_name_specifier_p;
19474 unsigned saved_num_template_parameter_lists;
19475 bool saved_in_function_body;
19476 unsigned char in_statement;
19477 bool in_switch_statement_p;
19478 bool saved_in_unbraced_linkage_specification_p;
19479 tree old_scope = NULL_TREE;
19480 tree scope = NULL_TREE;
19481 cp_token *closing_brace;
19483 push_deferring_access_checks (dk_no_deferred);
19485 /* Parse the class-head. */
19486 type = cp_parser_class_head (parser,
19487 &nested_name_specifier_p);
19488 /* If the class-head was a semantic disaster, skip the entire body
19489 of the class. */
19490 if (!type)
19492 cp_parser_skip_to_end_of_block_or_statement (parser);
19493 pop_deferring_access_checks ();
19494 return error_mark_node;
19497 /* Look for the `{'. */
19498 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
19500 pop_deferring_access_checks ();
19501 return error_mark_node;
19504 cp_ensure_no_omp_declare_simd (parser);
19506 /* Issue an error message if type-definitions are forbidden here. */
19507 cp_parser_check_type_definition (parser);
19508 /* Remember that we are defining one more class. */
19509 ++parser->num_classes_being_defined;
19510 /* Inside the class, surrounding template-parameter-lists do not
19511 apply. */
19512 saved_num_template_parameter_lists
19513 = parser->num_template_parameter_lists;
19514 parser->num_template_parameter_lists = 0;
19515 /* We are not in a function body. */
19516 saved_in_function_body = parser->in_function_body;
19517 parser->in_function_body = false;
19518 /* Or in a loop. */
19519 in_statement = parser->in_statement;
19520 parser->in_statement = 0;
19521 /* Or in a switch. */
19522 in_switch_statement_p = parser->in_switch_statement_p;
19523 parser->in_switch_statement_p = false;
19524 /* We are not immediately inside an extern "lang" block. */
19525 saved_in_unbraced_linkage_specification_p
19526 = parser->in_unbraced_linkage_specification_p;
19527 parser->in_unbraced_linkage_specification_p = false;
19529 /* Start the class. */
19530 if (nested_name_specifier_p)
19532 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
19533 old_scope = push_inner_scope (scope);
19535 type = begin_class_definition (type);
19537 if (type == error_mark_node)
19538 /* If the type is erroneous, skip the entire body of the class. */
19539 cp_parser_skip_to_closing_brace (parser);
19540 else
19541 /* Parse the member-specification. */
19542 cp_parser_member_specification_opt (parser);
19544 /* Look for the trailing `}'. */
19545 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19546 /* Look for trailing attributes to apply to this class. */
19547 if (cp_parser_allow_gnu_extensions_p (parser))
19548 attributes = cp_parser_gnu_attributes_opt (parser);
19549 if (type != error_mark_node)
19550 type = finish_struct (type, attributes);
19551 if (nested_name_specifier_p)
19552 pop_inner_scope (old_scope, scope);
19554 /* We've finished a type definition. Check for the common syntax
19555 error of forgetting a semicolon after the definition. We need to
19556 be careful, as we can't just check for not-a-semicolon and be done
19557 with it; the user might have typed:
19559 class X { } c = ...;
19560 class X { } *p = ...;
19562 and so forth. Instead, enumerate all the possible tokens that
19563 might follow this production; if we don't see one of them, then
19564 complain and silently insert the semicolon. */
19566 cp_token *token = cp_lexer_peek_token (parser->lexer);
19567 bool want_semicolon = true;
19569 if (cp_next_tokens_can_be_std_attribute_p (parser))
19570 /* Don't try to parse c++11 attributes here. As per the
19571 grammar, that should be a task for
19572 cp_parser_decl_specifier_seq. */
19573 want_semicolon = false;
19575 switch (token->type)
19577 case CPP_NAME:
19578 case CPP_SEMICOLON:
19579 case CPP_MULT:
19580 case CPP_AND:
19581 case CPP_OPEN_PAREN:
19582 case CPP_CLOSE_PAREN:
19583 case CPP_COMMA:
19584 want_semicolon = false;
19585 break;
19587 /* While it's legal for type qualifiers and storage class
19588 specifiers to follow type definitions in the grammar, only
19589 compiler testsuites contain code like that. Assume that if
19590 we see such code, then what we're really seeing is a case
19591 like:
19593 class X { }
19594 const <type> var = ...;
19598 class Y { }
19599 static <type> func (...) ...
19601 i.e. the qualifier or specifier applies to the next
19602 declaration. To do so, however, we need to look ahead one
19603 more token to see if *that* token is a type specifier.
19605 This code could be improved to handle:
19607 class Z { }
19608 static const <type> var = ...; */
19609 case CPP_KEYWORD:
19610 if (keyword_is_decl_specifier (token->keyword))
19612 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
19614 /* Handling user-defined types here would be nice, but very
19615 tricky. */
19616 want_semicolon
19617 = (lookahead->type == CPP_KEYWORD
19618 && keyword_begins_type_specifier (lookahead->keyword));
19620 break;
19621 default:
19622 break;
19625 /* If we don't have a type, then something is very wrong and we
19626 shouldn't try to do anything clever. Likewise for not seeing the
19627 closing brace. */
19628 if (closing_brace && TYPE_P (type) && want_semicolon)
19630 cp_token_position prev
19631 = cp_lexer_previous_token_position (parser->lexer);
19632 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
19633 location_t loc = prev_token->location;
19635 if (CLASSTYPE_DECLARED_CLASS (type))
19636 error_at (loc, "expected %<;%> after class definition");
19637 else if (TREE_CODE (type) == RECORD_TYPE)
19638 error_at (loc, "expected %<;%> after struct definition");
19639 else if (TREE_CODE (type) == UNION_TYPE)
19640 error_at (loc, "expected %<;%> after union definition");
19641 else
19642 gcc_unreachable ();
19644 /* Unget one token and smash it to look as though we encountered
19645 a semicolon in the input stream. */
19646 cp_lexer_set_token_position (parser->lexer, prev);
19647 token = cp_lexer_peek_token (parser->lexer);
19648 token->type = CPP_SEMICOLON;
19649 token->keyword = RID_MAX;
19653 /* If this class is not itself within the scope of another class,
19654 then we need to parse the bodies of all of the queued function
19655 definitions. Note that the queued functions defined in a class
19656 are not always processed immediately following the
19657 class-specifier for that class. Consider:
19659 struct A {
19660 struct B { void f() { sizeof (A); } };
19663 If `f' were processed before the processing of `A' were
19664 completed, there would be no way to compute the size of `A'.
19665 Note that the nesting we are interested in here is lexical --
19666 not the semantic nesting given by TYPE_CONTEXT. In particular,
19667 for:
19669 struct A { struct B; };
19670 struct A::B { void f() { } };
19672 there is no need to delay the parsing of `A::B::f'. */
19673 if (--parser->num_classes_being_defined == 0)
19675 tree decl;
19676 tree class_type = NULL_TREE;
19677 tree pushed_scope = NULL_TREE;
19678 unsigned ix;
19679 cp_default_arg_entry *e;
19680 tree save_ccp, save_ccr;
19682 /* In a first pass, parse default arguments to the functions.
19683 Then, in a second pass, parse the bodies of the functions.
19684 This two-phased approach handles cases like:
19686 struct S {
19687 void f() { g(); }
19688 void g(int i = 3);
19692 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
19694 decl = e->decl;
19695 /* If there are default arguments that have not yet been processed,
19696 take care of them now. */
19697 if (class_type != e->class_type)
19699 if (pushed_scope)
19700 pop_scope (pushed_scope);
19701 class_type = e->class_type;
19702 pushed_scope = push_scope (class_type);
19704 /* Make sure that any template parameters are in scope. */
19705 maybe_begin_member_template_processing (decl);
19706 /* Parse the default argument expressions. */
19707 cp_parser_late_parsing_default_args (parser, decl);
19708 /* Remove any template parameters from the symbol table. */
19709 maybe_end_member_template_processing ();
19711 vec_safe_truncate (unparsed_funs_with_default_args, 0);
19712 /* Now parse any NSDMIs. */
19713 save_ccp = current_class_ptr;
19714 save_ccr = current_class_ref;
19715 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
19717 if (class_type != DECL_CONTEXT (decl))
19719 if (pushed_scope)
19720 pop_scope (pushed_scope);
19721 class_type = DECL_CONTEXT (decl);
19722 pushed_scope = push_scope (class_type);
19724 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
19725 cp_parser_late_parsing_nsdmi (parser, decl);
19727 vec_safe_truncate (unparsed_nsdmis, 0);
19728 current_class_ptr = save_ccp;
19729 current_class_ref = save_ccr;
19730 if (pushed_scope)
19731 pop_scope (pushed_scope);
19733 /* Now do some post-NSDMI bookkeeping. */
19734 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
19735 after_nsdmi_defaulted_late_checks (class_type);
19736 vec_safe_truncate (unparsed_classes, 0);
19737 after_nsdmi_defaulted_late_checks (type);
19739 /* Now parse the body of the functions. */
19740 if (flag_openmp)
19742 /* OpenMP UDRs need to be parsed before all other functions. */
19743 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19744 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
19745 cp_parser_late_parsing_for_member (parser, decl);
19746 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19747 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
19748 cp_parser_late_parsing_for_member (parser, decl);
19750 else
19751 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
19752 cp_parser_late_parsing_for_member (parser, decl);
19753 vec_safe_truncate (unparsed_funs_with_definitions, 0);
19755 else
19756 vec_safe_push (unparsed_classes, type);
19758 /* Put back any saved access checks. */
19759 pop_deferring_access_checks ();
19761 /* Restore saved state. */
19762 parser->in_switch_statement_p = in_switch_statement_p;
19763 parser->in_statement = in_statement;
19764 parser->in_function_body = saved_in_function_body;
19765 parser->num_template_parameter_lists
19766 = saved_num_template_parameter_lists;
19767 parser->in_unbraced_linkage_specification_p
19768 = saved_in_unbraced_linkage_specification_p;
19770 return type;
19773 static tree
19774 cp_parser_class_specifier (cp_parser* parser)
19776 tree ret;
19777 timevar_push (TV_PARSE_STRUCT);
19778 ret = cp_parser_class_specifier_1 (parser);
19779 timevar_pop (TV_PARSE_STRUCT);
19780 return ret;
19783 /* Parse a class-head.
19785 class-head:
19786 class-key identifier [opt] base-clause [opt]
19787 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
19788 class-key nested-name-specifier [opt] template-id
19789 base-clause [opt]
19791 class-virt-specifier:
19792 final
19794 GNU Extensions:
19795 class-key attributes identifier [opt] base-clause [opt]
19796 class-key attributes nested-name-specifier identifier base-clause [opt]
19797 class-key attributes nested-name-specifier [opt] template-id
19798 base-clause [opt]
19800 Upon return BASES is initialized to the list of base classes (or
19801 NULL, if there are none) in the same form returned by
19802 cp_parser_base_clause.
19804 Returns the TYPE of the indicated class. Sets
19805 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
19806 involving a nested-name-specifier was used, and FALSE otherwise.
19808 Returns error_mark_node if this is not a class-head.
19810 Returns NULL_TREE if the class-head is syntactically valid, but
19811 semantically invalid in a way that means we should skip the entire
19812 body of the class. */
19814 static tree
19815 cp_parser_class_head (cp_parser* parser,
19816 bool* nested_name_specifier_p)
19818 tree nested_name_specifier;
19819 enum tag_types class_key;
19820 tree id = NULL_TREE;
19821 tree type = NULL_TREE;
19822 tree attributes;
19823 tree bases;
19824 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19825 bool template_id_p = false;
19826 bool qualified_p = false;
19827 bool invalid_nested_name_p = false;
19828 bool invalid_explicit_specialization_p = false;
19829 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
19830 tree pushed_scope = NULL_TREE;
19831 unsigned num_templates;
19832 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
19833 /* Assume no nested-name-specifier will be present. */
19834 *nested_name_specifier_p = false;
19835 /* Assume no template parameter lists will be used in defining the
19836 type. */
19837 num_templates = 0;
19838 parser->colon_corrects_to_scope_p = false;
19840 /* Look for the class-key. */
19841 class_key = cp_parser_class_key (parser);
19842 if (class_key == none_type)
19843 return error_mark_node;
19845 /* Parse the attributes. */
19846 attributes = cp_parser_attributes_opt (parser);
19848 /* If the next token is `::', that is invalid -- but sometimes
19849 people do try to write:
19851 struct ::S {};
19853 Handle this gracefully by accepting the extra qualifier, and then
19854 issuing an error about it later if this really is a
19855 class-head. If it turns out just to be an elaborated type
19856 specifier, remain silent. */
19857 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
19858 qualified_p = true;
19860 push_deferring_access_checks (dk_no_check);
19862 /* Determine the name of the class. Begin by looking for an
19863 optional nested-name-specifier. */
19864 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
19865 nested_name_specifier
19866 = cp_parser_nested_name_specifier_opt (parser,
19867 /*typename_keyword_p=*/false,
19868 /*check_dependency_p=*/false,
19869 /*type_p=*/true,
19870 /*is_declaration=*/false);
19871 /* If there was a nested-name-specifier, then there *must* be an
19872 identifier. */
19873 if (nested_name_specifier)
19875 type_start_token = cp_lexer_peek_token (parser->lexer);
19876 /* Although the grammar says `identifier', it really means
19877 `class-name' or `template-name'. You are only allowed to
19878 define a class that has already been declared with this
19879 syntax.
19881 The proposed resolution for Core Issue 180 says that wherever
19882 you see `class T::X' you should treat `X' as a type-name.
19884 It is OK to define an inaccessible class; for example:
19886 class A { class B; };
19887 class A::B {};
19889 We do not know if we will see a class-name, or a
19890 template-name. We look for a class-name first, in case the
19891 class-name is a template-id; if we looked for the
19892 template-name first we would stop after the template-name. */
19893 cp_parser_parse_tentatively (parser);
19894 type = cp_parser_class_name (parser,
19895 /*typename_keyword_p=*/false,
19896 /*template_keyword_p=*/false,
19897 class_type,
19898 /*check_dependency_p=*/false,
19899 /*class_head_p=*/true,
19900 /*is_declaration=*/false);
19901 /* If that didn't work, ignore the nested-name-specifier. */
19902 if (!cp_parser_parse_definitely (parser))
19904 invalid_nested_name_p = true;
19905 type_start_token = cp_lexer_peek_token (parser->lexer);
19906 id = cp_parser_identifier (parser);
19907 if (id == error_mark_node)
19908 id = NULL_TREE;
19910 /* If we could not find a corresponding TYPE, treat this
19911 declaration like an unqualified declaration. */
19912 if (type == error_mark_node)
19913 nested_name_specifier = NULL_TREE;
19914 /* Otherwise, count the number of templates used in TYPE and its
19915 containing scopes. */
19916 else
19918 tree scope;
19920 for (scope = TREE_TYPE (type);
19921 scope && TREE_CODE (scope) != NAMESPACE_DECL;
19922 scope = get_containing_scope (scope))
19923 if (TYPE_P (scope)
19924 && CLASS_TYPE_P (scope)
19925 && CLASSTYPE_TEMPLATE_INFO (scope)
19926 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
19927 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
19928 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
19929 ++num_templates;
19932 /* Otherwise, the identifier is optional. */
19933 else
19935 /* We don't know whether what comes next is a template-id,
19936 an identifier, or nothing at all. */
19937 cp_parser_parse_tentatively (parser);
19938 /* Check for a template-id. */
19939 type_start_token = cp_lexer_peek_token (parser->lexer);
19940 id = cp_parser_template_id (parser,
19941 /*template_keyword_p=*/false,
19942 /*check_dependency_p=*/true,
19943 class_key,
19944 /*is_declaration=*/true);
19945 /* If that didn't work, it could still be an identifier. */
19946 if (!cp_parser_parse_definitely (parser))
19948 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19950 type_start_token = cp_lexer_peek_token (parser->lexer);
19951 id = cp_parser_identifier (parser);
19953 else
19954 id = NULL_TREE;
19956 else
19958 template_id_p = true;
19959 ++num_templates;
19963 pop_deferring_access_checks ();
19965 if (id)
19967 cp_parser_check_for_invalid_template_id (parser, id,
19968 class_key,
19969 type_start_token->location);
19971 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19973 /* If it's not a `:' or a `{' then we can't really be looking at a
19974 class-head, since a class-head only appears as part of a
19975 class-specifier. We have to detect this situation before calling
19976 xref_tag, since that has irreversible side-effects. */
19977 if (!cp_parser_next_token_starts_class_definition_p (parser))
19979 cp_parser_error (parser, "expected %<{%> or %<:%>");
19980 type = error_mark_node;
19981 goto out;
19984 /* At this point, we're going ahead with the class-specifier, even
19985 if some other problem occurs. */
19986 cp_parser_commit_to_tentative_parse (parser);
19987 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
19989 cp_parser_error (parser,
19990 "cannot specify %<override%> for a class");
19991 type = error_mark_node;
19992 goto out;
19994 /* Issue the error about the overly-qualified name now. */
19995 if (qualified_p)
19997 cp_parser_error (parser,
19998 "global qualification of class name is invalid");
19999 type = error_mark_node;
20000 goto out;
20002 else if (invalid_nested_name_p)
20004 cp_parser_error (parser,
20005 "qualified name does not name a class");
20006 type = error_mark_node;
20007 goto out;
20009 else if (nested_name_specifier)
20011 tree scope;
20013 /* Reject typedef-names in class heads. */
20014 if (!DECL_IMPLICIT_TYPEDEF_P (type))
20016 error_at (type_start_token->location,
20017 "invalid class name in declaration of %qD",
20018 type);
20019 type = NULL_TREE;
20020 goto done;
20023 /* Figure out in what scope the declaration is being placed. */
20024 scope = current_scope ();
20025 /* If that scope does not contain the scope in which the
20026 class was originally declared, the program is invalid. */
20027 if (scope && !is_ancestor (scope, nested_name_specifier))
20029 if (at_namespace_scope_p ())
20030 error_at (type_start_token->location,
20031 "declaration of %qD in namespace %qD which does not "
20032 "enclose %qD",
20033 type, scope, nested_name_specifier);
20034 else
20035 error_at (type_start_token->location,
20036 "declaration of %qD in %qD which does not enclose %qD",
20037 type, scope, nested_name_specifier);
20038 type = NULL_TREE;
20039 goto done;
20041 /* [dcl.meaning]
20043 A declarator-id shall not be qualified except for the
20044 definition of a ... nested class outside of its class
20045 ... [or] the definition or explicit instantiation of a
20046 class member of a namespace outside of its namespace. */
20047 if (scope == nested_name_specifier)
20049 permerror (nested_name_specifier_token_start->location,
20050 "extra qualification not allowed");
20051 nested_name_specifier = NULL_TREE;
20052 num_templates = 0;
20055 /* An explicit-specialization must be preceded by "template <>". If
20056 it is not, try to recover gracefully. */
20057 if (at_namespace_scope_p ()
20058 && parser->num_template_parameter_lists == 0
20059 && template_id_p)
20061 error_at (type_start_token->location,
20062 "an explicit specialization must be preceded by %<template <>%>");
20063 invalid_explicit_specialization_p = true;
20064 /* Take the same action that would have been taken by
20065 cp_parser_explicit_specialization. */
20066 ++parser->num_template_parameter_lists;
20067 begin_specialization ();
20069 /* There must be no "return" statements between this point and the
20070 end of this function; set "type "to the correct return value and
20071 use "goto done;" to return. */
20072 /* Make sure that the right number of template parameters were
20073 present. */
20074 if (!cp_parser_check_template_parameters (parser, num_templates,
20075 type_start_token->location,
20076 /*declarator=*/NULL))
20078 /* If something went wrong, there is no point in even trying to
20079 process the class-definition. */
20080 type = NULL_TREE;
20081 goto done;
20084 /* Look up the type. */
20085 if (template_id_p)
20087 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
20088 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
20089 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
20091 error_at (type_start_token->location,
20092 "function template %qD redeclared as a class template", id);
20093 type = error_mark_node;
20095 else
20097 type = TREE_TYPE (id);
20098 type = maybe_process_partial_specialization (type);
20100 if (nested_name_specifier)
20101 pushed_scope = push_scope (nested_name_specifier);
20103 else if (nested_name_specifier)
20105 tree class_type;
20107 /* Given:
20109 template <typename T> struct S { struct T };
20110 template <typename T> struct S<T>::T { };
20112 we will get a TYPENAME_TYPE when processing the definition of
20113 `S::T'. We need to resolve it to the actual type before we
20114 try to define it. */
20115 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
20117 class_type = resolve_typename_type (TREE_TYPE (type),
20118 /*only_current_p=*/false);
20119 if (TREE_CODE (class_type) != TYPENAME_TYPE)
20120 type = TYPE_NAME (class_type);
20121 else
20123 cp_parser_error (parser, "could not resolve typename type");
20124 type = error_mark_node;
20128 if (maybe_process_partial_specialization (TREE_TYPE (type))
20129 == error_mark_node)
20131 type = NULL_TREE;
20132 goto done;
20135 class_type = current_class_type;
20136 /* Enter the scope indicated by the nested-name-specifier. */
20137 pushed_scope = push_scope (nested_name_specifier);
20138 /* Get the canonical version of this type. */
20139 type = TYPE_MAIN_DECL (TREE_TYPE (type));
20140 /* Call push_template_decl if it seems like we should be defining a
20141 template either from the template headers or the type we're
20142 defining, so that we diagnose both extra and missing headers. */
20143 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
20144 || (CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type))
20145 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE
20146 (TREE_TYPE (type)))))
20147 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
20149 type = push_template_decl (type);
20150 if (type == error_mark_node)
20152 type = NULL_TREE;
20153 goto done;
20157 type = TREE_TYPE (type);
20158 *nested_name_specifier_p = true;
20160 else /* The name is not a nested name. */
20162 /* If the class was unnamed, create a dummy name. */
20163 if (!id)
20164 id = make_anon_name ();
20165 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
20166 parser->num_template_parameter_lists);
20169 /* Indicate whether this class was declared as a `class' or as a
20170 `struct'. */
20171 if (TREE_CODE (type) == RECORD_TYPE)
20172 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
20173 cp_parser_check_class_key (class_key, type);
20175 /* If this type was already complete, and we see another definition,
20176 that's an error. */
20177 if (type != error_mark_node && COMPLETE_TYPE_P (type))
20179 error_at (type_start_token->location, "redefinition of %q#T",
20180 type);
20181 error_at (type_start_token->location, "previous definition of %q+#T",
20182 type);
20183 type = NULL_TREE;
20184 goto done;
20186 else if (type == error_mark_node)
20187 type = NULL_TREE;
20189 if (type)
20191 /* Apply attributes now, before any use of the class as a template
20192 argument in its base list. */
20193 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
20194 fixup_attribute_variants (type);
20197 /* We will have entered the scope containing the class; the names of
20198 base classes should be looked up in that context. For example:
20200 struct A { struct B {}; struct C; };
20201 struct A::C : B {};
20203 is valid. */
20205 /* Get the list of base-classes, if there is one. */
20206 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20208 /* PR59482: enter the class scope so that base-specifiers are looked
20209 up correctly. */
20210 if (type)
20211 pushclass (type);
20212 bases = cp_parser_base_clause (parser);
20213 /* PR59482: get out of the previously pushed class scope so that the
20214 subsequent pops pop the right thing. */
20215 if (type)
20216 popclass ();
20218 else
20219 bases = NULL_TREE;
20221 /* If we're really defining a class, process the base classes.
20222 If they're invalid, fail. */
20223 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20224 && !xref_basetypes (type, bases))
20225 type = NULL_TREE;
20227 done:
20228 /* Leave the scope given by the nested-name-specifier. We will
20229 enter the class scope itself while processing the members. */
20230 if (pushed_scope)
20231 pop_scope (pushed_scope);
20233 if (invalid_explicit_specialization_p)
20235 end_specialization ();
20236 --parser->num_template_parameter_lists;
20239 if (type)
20240 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
20241 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
20242 CLASSTYPE_FINAL (type) = 1;
20243 out:
20244 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20245 return type;
20248 /* Parse a class-key.
20250 class-key:
20251 class
20252 struct
20253 union
20255 Returns the kind of class-key specified, or none_type to indicate
20256 error. */
20258 static enum tag_types
20259 cp_parser_class_key (cp_parser* parser)
20261 cp_token *token;
20262 enum tag_types tag_type;
20264 /* Look for the class-key. */
20265 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
20266 if (!token)
20267 return none_type;
20269 /* Check to see if the TOKEN is a class-key. */
20270 tag_type = cp_parser_token_is_class_key (token);
20271 if (!tag_type)
20272 cp_parser_error (parser, "expected class-key");
20273 return tag_type;
20276 /* Parse a type-parameter-key.
20278 type-parameter-key:
20279 class
20280 typename
20283 static void
20284 cp_parser_type_parameter_key (cp_parser* parser)
20286 /* Look for the type-parameter-key. */
20287 enum tag_types tag_type = none_type;
20288 cp_token *token = cp_lexer_peek_token (parser->lexer);
20289 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
20291 cp_lexer_consume_token (parser->lexer);
20292 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
20293 /* typename is not allowed in a template template parameter
20294 by the standard until C++1Z. */
20295 pedwarn (token->location, OPT_Wpedantic,
20296 "ISO C++ forbids typename key in template template parameter;"
20297 " use -std=c++1z or -std=gnu++1z");
20299 else
20300 cp_parser_error (parser, "expected %<class%> or %<typename%>");
20302 return;
20305 /* Parse an (optional) member-specification.
20307 member-specification:
20308 member-declaration member-specification [opt]
20309 access-specifier : member-specification [opt] */
20311 static void
20312 cp_parser_member_specification_opt (cp_parser* parser)
20314 while (true)
20316 cp_token *token;
20317 enum rid keyword;
20319 /* Peek at the next token. */
20320 token = cp_lexer_peek_token (parser->lexer);
20321 /* If it's a `}', or EOF then we've seen all the members. */
20322 if (token->type == CPP_CLOSE_BRACE
20323 || token->type == CPP_EOF
20324 || token->type == CPP_PRAGMA_EOL)
20325 break;
20327 /* See if this token is a keyword. */
20328 keyword = token->keyword;
20329 switch (keyword)
20331 case RID_PUBLIC:
20332 case RID_PROTECTED:
20333 case RID_PRIVATE:
20334 /* Consume the access-specifier. */
20335 cp_lexer_consume_token (parser->lexer);
20336 /* Remember which access-specifier is active. */
20337 current_access_specifier = token->u.value;
20338 /* Look for the `:'. */
20339 cp_parser_require (parser, CPP_COLON, RT_COLON);
20340 break;
20342 default:
20343 /* Accept #pragmas at class scope. */
20344 if (token->type == CPP_PRAGMA)
20346 cp_parser_pragma (parser, pragma_member);
20347 break;
20350 /* Otherwise, the next construction must be a
20351 member-declaration. */
20352 cp_parser_member_declaration (parser);
20357 /* Parse a member-declaration.
20359 member-declaration:
20360 decl-specifier-seq [opt] member-declarator-list [opt] ;
20361 function-definition ; [opt]
20362 :: [opt] nested-name-specifier template [opt] unqualified-id ;
20363 using-declaration
20364 template-declaration
20365 alias-declaration
20367 member-declarator-list:
20368 member-declarator
20369 member-declarator-list , member-declarator
20371 member-declarator:
20372 declarator pure-specifier [opt]
20373 declarator constant-initializer [opt]
20374 identifier [opt] : constant-expression
20376 GNU Extensions:
20378 member-declaration:
20379 __extension__ member-declaration
20381 member-declarator:
20382 declarator attributes [opt] pure-specifier [opt]
20383 declarator attributes [opt] constant-initializer [opt]
20384 identifier [opt] attributes [opt] : constant-expression
20386 C++0x Extensions:
20388 member-declaration:
20389 static_assert-declaration */
20391 static void
20392 cp_parser_member_declaration (cp_parser* parser)
20394 cp_decl_specifier_seq decl_specifiers;
20395 tree prefix_attributes;
20396 tree decl;
20397 int declares_class_or_enum;
20398 bool friend_p;
20399 cp_token *token = NULL;
20400 cp_token *decl_spec_token_start = NULL;
20401 cp_token *initializer_token_start = NULL;
20402 int saved_pedantic;
20403 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
20405 /* Check for the `__extension__' keyword. */
20406 if (cp_parser_extension_opt (parser, &saved_pedantic))
20408 /* Recurse. */
20409 cp_parser_member_declaration (parser);
20410 /* Restore the old value of the PEDANTIC flag. */
20411 pedantic = saved_pedantic;
20413 return;
20416 /* Check for a template-declaration. */
20417 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
20419 /* An explicit specialization here is an error condition, and we
20420 expect the specialization handler to detect and report this. */
20421 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
20422 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
20423 cp_parser_explicit_specialization (parser);
20424 else
20425 cp_parser_template_declaration (parser, /*member_p=*/true);
20427 return;
20430 /* Check for a using-declaration. */
20431 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
20433 if (cxx_dialect < cxx11)
20435 /* Parse the using-declaration. */
20436 cp_parser_using_declaration (parser,
20437 /*access_declaration_p=*/false);
20438 return;
20440 else
20442 tree decl;
20443 bool alias_decl_expected;
20444 cp_parser_parse_tentatively (parser);
20445 decl = cp_parser_alias_declaration (parser);
20446 /* Note that if we actually see the '=' token after the
20447 identifier, cp_parser_alias_declaration commits the
20448 tentative parse. In that case, we really expects an
20449 alias-declaration. Otherwise, we expect a using
20450 declaration. */
20451 alias_decl_expected =
20452 !cp_parser_uncommitted_to_tentative_parse_p (parser);
20453 cp_parser_parse_definitely (parser);
20455 if (alias_decl_expected)
20456 finish_member_declaration (decl);
20457 else
20458 cp_parser_using_declaration (parser,
20459 /*access_declaration_p=*/false);
20460 return;
20464 /* Check for @defs. */
20465 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
20467 tree ivar, member;
20468 tree ivar_chains = cp_parser_objc_defs_expression (parser);
20469 ivar = ivar_chains;
20470 while (ivar)
20472 member = ivar;
20473 ivar = TREE_CHAIN (member);
20474 TREE_CHAIN (member) = NULL_TREE;
20475 finish_member_declaration (member);
20477 return;
20480 /* If the next token is `static_assert' we have a static assertion. */
20481 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
20483 cp_parser_static_assert (parser, /*member_p=*/true);
20484 return;
20487 parser->colon_corrects_to_scope_p = false;
20489 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
20490 goto out;
20492 /* Parse the decl-specifier-seq. */
20493 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20494 cp_parser_decl_specifier_seq (parser,
20495 CP_PARSER_FLAGS_OPTIONAL,
20496 &decl_specifiers,
20497 &declares_class_or_enum);
20498 /* Check for an invalid type-name. */
20499 if (!decl_specifiers.any_type_specifiers_p
20500 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20501 goto out;
20502 /* If there is no declarator, then the decl-specifier-seq should
20503 specify a type. */
20504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20506 /* If there was no decl-specifier-seq, and the next token is a
20507 `;', then we have something like:
20509 struct S { ; };
20511 [class.mem]
20513 Each member-declaration shall declare at least one member
20514 name of the class. */
20515 if (!decl_specifiers.any_specifiers_p)
20517 cp_token *token = cp_lexer_peek_token (parser->lexer);
20518 if (!in_system_header_at (token->location))
20519 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
20521 else
20523 tree type;
20525 /* See if this declaration is a friend. */
20526 friend_p = cp_parser_friend_p (&decl_specifiers);
20527 /* If there were decl-specifiers, check to see if there was
20528 a class-declaration. */
20529 type = check_tag_decl (&decl_specifiers,
20530 /*explicit_type_instantiation_p=*/false);
20531 /* Nested classes have already been added to the class, but
20532 a `friend' needs to be explicitly registered. */
20533 if (friend_p)
20535 /* If the `friend' keyword was present, the friend must
20536 be introduced with a class-key. */
20537 if (!declares_class_or_enum && cxx_dialect < cxx11)
20538 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
20539 "in C++03 a class-key must be used "
20540 "when declaring a friend");
20541 /* In this case:
20543 template <typename T> struct A {
20544 friend struct A<T>::B;
20547 A<T>::B will be represented by a TYPENAME_TYPE, and
20548 therefore not recognized by check_tag_decl. */
20549 if (!type)
20551 type = decl_specifiers.type;
20552 if (type && TREE_CODE (type) == TYPE_DECL)
20553 type = TREE_TYPE (type);
20555 if (!type || !TYPE_P (type))
20556 error_at (decl_spec_token_start->location,
20557 "friend declaration does not name a class or "
20558 "function");
20559 else
20560 make_friend_class (current_class_type, type,
20561 /*complain=*/true);
20563 /* If there is no TYPE, an error message will already have
20564 been issued. */
20565 else if (!type || type == error_mark_node)
20567 /* An anonymous aggregate has to be handled specially; such
20568 a declaration really declares a data member (with a
20569 particular type), as opposed to a nested class. */
20570 else if (ANON_AGGR_TYPE_P (type))
20572 /* C++11 9.5/6. */
20573 if (decl_specifiers.storage_class != sc_none)
20574 error_at (decl_spec_token_start->location,
20575 "a storage class on an anonymous aggregate "
20576 "in class scope is not allowed");
20578 /* Remove constructors and such from TYPE, now that we
20579 know it is an anonymous aggregate. */
20580 fixup_anonymous_aggr (type);
20581 /* And make the corresponding data member. */
20582 decl = build_decl (decl_spec_token_start->location,
20583 FIELD_DECL, NULL_TREE, type);
20584 /* Add it to the class. */
20585 finish_member_declaration (decl);
20587 else
20588 cp_parser_check_access_in_redeclaration
20589 (TYPE_NAME (type),
20590 decl_spec_token_start->location);
20593 else
20595 bool assume_semicolon = false;
20597 /* Clear attributes from the decl_specifiers but keep them
20598 around as prefix attributes that apply them to the entity
20599 being declared. */
20600 prefix_attributes = decl_specifiers.attributes;
20601 decl_specifiers.attributes = NULL_TREE;
20603 /* See if these declarations will be friends. */
20604 friend_p = cp_parser_friend_p (&decl_specifiers);
20606 /* Keep going until we hit the `;' at the end of the
20607 declaration. */
20608 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20610 tree attributes = NULL_TREE;
20611 tree first_attribute;
20613 /* Peek at the next token. */
20614 token = cp_lexer_peek_token (parser->lexer);
20616 /* Check for a bitfield declaration. */
20617 if (token->type == CPP_COLON
20618 || (token->type == CPP_NAME
20619 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
20620 == CPP_COLON))
20622 tree identifier;
20623 tree width;
20625 /* Get the name of the bitfield. Note that we cannot just
20626 check TOKEN here because it may have been invalidated by
20627 the call to cp_lexer_peek_nth_token above. */
20628 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
20629 identifier = cp_parser_identifier (parser);
20630 else
20631 identifier = NULL_TREE;
20633 /* Consume the `:' token. */
20634 cp_lexer_consume_token (parser->lexer);
20635 /* Get the width of the bitfield. */
20636 width
20637 = cp_parser_constant_expression (parser,
20638 /*allow_non_constant=*/false,
20639 NULL);
20641 /* Look for attributes that apply to the bitfield. */
20642 attributes = cp_parser_attributes_opt (parser);
20643 /* Remember which attributes are prefix attributes and
20644 which are not. */
20645 first_attribute = attributes;
20646 /* Combine the attributes. */
20647 attributes = chainon (prefix_attributes, attributes);
20649 /* Create the bitfield declaration. */
20650 decl = grokbitfield (identifier
20651 ? make_id_declarator (NULL_TREE,
20652 identifier,
20653 sfk_none)
20654 : NULL,
20655 &decl_specifiers,
20656 width,
20657 attributes);
20659 else
20661 cp_declarator *declarator;
20662 tree initializer;
20663 tree asm_specification;
20664 int ctor_dtor_or_conv_p;
20666 /* Parse the declarator. */
20667 declarator
20668 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20669 &ctor_dtor_or_conv_p,
20670 /*parenthesized_p=*/NULL,
20671 /*member_p=*/true,
20672 friend_p);
20674 /* If something went wrong parsing the declarator, make sure
20675 that we at least consume some tokens. */
20676 if (declarator == cp_error_declarator)
20678 /* Skip to the end of the statement. */
20679 cp_parser_skip_to_end_of_statement (parser);
20680 /* If the next token is not a semicolon, that is
20681 probably because we just skipped over the body of
20682 a function. So, we consume a semicolon if
20683 present, but do not issue an error message if it
20684 is not present. */
20685 if (cp_lexer_next_token_is (parser->lexer,
20686 CPP_SEMICOLON))
20687 cp_lexer_consume_token (parser->lexer);
20688 goto out;
20691 if (declares_class_or_enum & 2)
20692 cp_parser_check_for_definition_in_return_type
20693 (declarator, decl_specifiers.type,
20694 decl_specifiers.locations[ds_type_spec]);
20696 /* Look for an asm-specification. */
20697 asm_specification = cp_parser_asm_specification_opt (parser);
20698 /* Look for attributes that apply to the declaration. */
20699 attributes = cp_parser_attributes_opt (parser);
20700 /* Remember which attributes are prefix attributes and
20701 which are not. */
20702 first_attribute = attributes;
20703 /* Combine the attributes. */
20704 attributes = chainon (prefix_attributes, attributes);
20706 /* If it's an `=', then we have a constant-initializer or a
20707 pure-specifier. It is not correct to parse the
20708 initializer before registering the member declaration
20709 since the member declaration should be in scope while
20710 its initializer is processed. However, the rest of the
20711 front end does not yet provide an interface that allows
20712 us to handle this correctly. */
20713 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20715 /* In [class.mem]:
20717 A pure-specifier shall be used only in the declaration of
20718 a virtual function.
20720 A member-declarator can contain a constant-initializer
20721 only if it declares a static member of integral or
20722 enumeration type.
20724 Therefore, if the DECLARATOR is for a function, we look
20725 for a pure-specifier; otherwise, we look for a
20726 constant-initializer. When we call `grokfield', it will
20727 perform more stringent semantics checks. */
20728 initializer_token_start = cp_lexer_peek_token (parser->lexer);
20729 if (function_declarator_p (declarator)
20730 || (decl_specifiers.type
20731 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
20732 && declarator->kind == cdk_id
20733 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
20734 == FUNCTION_TYPE)))
20735 initializer = cp_parser_pure_specifier (parser);
20736 else if (decl_specifiers.storage_class != sc_static)
20737 initializer = cp_parser_save_nsdmi (parser);
20738 else if (cxx_dialect >= cxx11)
20740 bool nonconst;
20741 /* Don't require a constant rvalue in C++11, since we
20742 might want a reference constant. We'll enforce
20743 constancy later. */
20744 cp_lexer_consume_token (parser->lexer);
20745 /* Parse the initializer. */
20746 initializer = cp_parser_initializer_clause (parser,
20747 &nonconst);
20749 else
20750 /* Parse the initializer. */
20751 initializer = cp_parser_constant_initializer (parser);
20753 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
20754 && !function_declarator_p (declarator))
20756 bool x;
20757 if (decl_specifiers.storage_class != sc_static)
20758 initializer = cp_parser_save_nsdmi (parser);
20759 else
20760 initializer = cp_parser_initializer (parser, &x, &x);
20762 /* Otherwise, there is no initializer. */
20763 else
20764 initializer = NULL_TREE;
20766 /* See if we are probably looking at a function
20767 definition. We are certainly not looking at a
20768 member-declarator. Calling `grokfield' has
20769 side-effects, so we must not do it unless we are sure
20770 that we are looking at a member-declarator. */
20771 if (cp_parser_token_starts_function_definition_p
20772 (cp_lexer_peek_token (parser->lexer)))
20774 /* The grammar does not allow a pure-specifier to be
20775 used when a member function is defined. (It is
20776 possible that this fact is an oversight in the
20777 standard, since a pure function may be defined
20778 outside of the class-specifier. */
20779 if (initializer && initializer_token_start)
20780 error_at (initializer_token_start->location,
20781 "pure-specifier on function-definition");
20782 decl = cp_parser_save_member_function_body (parser,
20783 &decl_specifiers,
20784 declarator,
20785 attributes);
20786 if (parser->fully_implicit_function_template_p)
20787 decl = finish_fully_implicit_template (parser, decl);
20788 /* If the member was not a friend, declare it here. */
20789 if (!friend_p)
20790 finish_member_declaration (decl);
20791 /* Peek at the next token. */
20792 token = cp_lexer_peek_token (parser->lexer);
20793 /* If the next token is a semicolon, consume it. */
20794 if (token->type == CPP_SEMICOLON)
20795 cp_lexer_consume_token (parser->lexer);
20796 goto out;
20798 else
20799 if (declarator->kind == cdk_function)
20800 declarator->id_loc = token->location;
20801 /* Create the declaration. */
20802 decl = grokfield (declarator, &decl_specifiers,
20803 initializer, /*init_const_expr_p=*/true,
20804 asm_specification, attributes);
20805 if (parser->fully_implicit_function_template_p)
20807 if (friend_p)
20808 finish_fully_implicit_template (parser, 0);
20809 else
20810 decl = finish_fully_implicit_template (parser, decl);
20814 cp_finalize_omp_declare_simd (parser, decl);
20816 /* Reset PREFIX_ATTRIBUTES. */
20817 while (attributes && TREE_CHAIN (attributes) != first_attribute)
20818 attributes = TREE_CHAIN (attributes);
20819 if (attributes)
20820 TREE_CHAIN (attributes) = NULL_TREE;
20822 /* If there is any qualification still in effect, clear it
20823 now; we will be starting fresh with the next declarator. */
20824 parser->scope = NULL_TREE;
20825 parser->qualifying_scope = NULL_TREE;
20826 parser->object_scope = NULL_TREE;
20827 /* If it's a `,', then there are more declarators. */
20828 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20830 cp_lexer_consume_token (parser->lexer);
20831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20833 cp_token *token = cp_lexer_previous_token (parser->lexer);
20834 error_at (token->location,
20835 "stray %<,%> at end of member declaration");
20838 /* If the next token isn't a `;', then we have a parse error. */
20839 else if (cp_lexer_next_token_is_not (parser->lexer,
20840 CPP_SEMICOLON))
20842 /* The next token might be a ways away from where the
20843 actual semicolon is missing. Find the previous token
20844 and use that for our error position. */
20845 cp_token *token = cp_lexer_previous_token (parser->lexer);
20846 error_at (token->location,
20847 "expected %<;%> at end of member declaration");
20849 /* Assume that the user meant to provide a semicolon. If
20850 we were to cp_parser_skip_to_end_of_statement, we might
20851 skip to a semicolon inside a member function definition
20852 and issue nonsensical error messages. */
20853 assume_semicolon = true;
20856 if (decl)
20858 /* Add DECL to the list of members. */
20859 if (!friend_p)
20860 finish_member_declaration (decl);
20862 if (TREE_CODE (decl) == FUNCTION_DECL)
20863 cp_parser_save_default_args (parser, decl);
20864 else if (TREE_CODE (decl) == FIELD_DECL
20865 && !DECL_C_BIT_FIELD (decl)
20866 && DECL_INITIAL (decl))
20867 /* Add DECL to the queue of NSDMI to be parsed later. */
20868 vec_safe_push (unparsed_nsdmis, decl);
20871 if (assume_semicolon)
20872 goto out;
20876 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20877 out:
20878 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
20881 /* Parse a pure-specifier.
20883 pure-specifier:
20886 Returns INTEGER_ZERO_NODE if a pure specifier is found.
20887 Otherwise, ERROR_MARK_NODE is returned. */
20889 static tree
20890 cp_parser_pure_specifier (cp_parser* parser)
20892 cp_token *token;
20894 /* Look for the `=' token. */
20895 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20896 return error_mark_node;
20897 /* Look for the `0' token. */
20898 token = cp_lexer_peek_token (parser->lexer);
20900 if (token->type == CPP_EOF
20901 || token->type == CPP_PRAGMA_EOL)
20902 return error_mark_node;
20904 cp_lexer_consume_token (parser->lexer);
20906 /* Accept = default or = delete in c++0x mode. */
20907 if (token->keyword == RID_DEFAULT
20908 || token->keyword == RID_DELETE)
20910 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
20911 return token->u.value;
20914 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
20915 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
20917 cp_parser_error (parser,
20918 "invalid pure specifier (only %<= 0%> is allowed)");
20919 cp_parser_skip_to_end_of_statement (parser);
20920 return error_mark_node;
20922 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
20924 error_at (token->location, "templates may not be %<virtual%>");
20925 return error_mark_node;
20928 return integer_zero_node;
20931 /* Parse a constant-initializer.
20933 constant-initializer:
20934 = constant-expression
20936 Returns a representation of the constant-expression. */
20938 static tree
20939 cp_parser_constant_initializer (cp_parser* parser)
20941 /* Look for the `=' token. */
20942 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
20943 return error_mark_node;
20945 /* It is invalid to write:
20947 struct S { static const int i = { 7 }; };
20950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20952 cp_parser_error (parser,
20953 "a brace-enclosed initializer is not allowed here");
20954 /* Consume the opening brace. */
20955 cp_lexer_consume_token (parser->lexer);
20956 /* Skip the initializer. */
20957 cp_parser_skip_to_closing_brace (parser);
20958 /* Look for the trailing `}'. */
20959 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20961 return error_mark_node;
20964 return cp_parser_constant_expression (parser,
20965 /*allow_non_constant=*/false,
20966 NULL);
20969 /* Derived classes [gram.class.derived] */
20971 /* Parse a base-clause.
20973 base-clause:
20974 : base-specifier-list
20976 base-specifier-list:
20977 base-specifier ... [opt]
20978 base-specifier-list , base-specifier ... [opt]
20980 Returns a TREE_LIST representing the base-classes, in the order in
20981 which they were declared. The representation of each node is as
20982 described by cp_parser_base_specifier.
20984 In the case that no bases are specified, this function will return
20985 NULL_TREE, not ERROR_MARK_NODE. */
20987 static tree
20988 cp_parser_base_clause (cp_parser* parser)
20990 tree bases = NULL_TREE;
20992 /* Look for the `:' that begins the list. */
20993 cp_parser_require (parser, CPP_COLON, RT_COLON);
20995 /* Scan the base-specifier-list. */
20996 while (true)
20998 cp_token *token;
20999 tree base;
21000 bool pack_expansion_p = false;
21002 /* Look for the base-specifier. */
21003 base = cp_parser_base_specifier (parser);
21004 /* Look for the (optional) ellipsis. */
21005 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21007 /* Consume the `...'. */
21008 cp_lexer_consume_token (parser->lexer);
21010 pack_expansion_p = true;
21013 /* Add BASE to the front of the list. */
21014 if (base && base != error_mark_node)
21016 if (pack_expansion_p)
21017 /* Make this a pack expansion type. */
21018 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
21020 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
21022 TREE_CHAIN (base) = bases;
21023 bases = base;
21026 /* Peek at the next token. */
21027 token = cp_lexer_peek_token (parser->lexer);
21028 /* If it's not a comma, then the list is complete. */
21029 if (token->type != CPP_COMMA)
21030 break;
21031 /* Consume the `,'. */
21032 cp_lexer_consume_token (parser->lexer);
21035 /* PARSER->SCOPE may still be non-NULL at this point, if the last
21036 base class had a qualified name. However, the next name that
21037 appears is certainly not qualified. */
21038 parser->scope = NULL_TREE;
21039 parser->qualifying_scope = NULL_TREE;
21040 parser->object_scope = NULL_TREE;
21042 return nreverse (bases);
21045 /* Parse a base-specifier.
21047 base-specifier:
21048 :: [opt] nested-name-specifier [opt] class-name
21049 virtual access-specifier [opt] :: [opt] nested-name-specifier
21050 [opt] class-name
21051 access-specifier virtual [opt] :: [opt] nested-name-specifier
21052 [opt] class-name
21054 Returns a TREE_LIST. The TREE_PURPOSE will be one of
21055 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
21056 indicate the specifiers provided. The TREE_VALUE will be a TYPE
21057 (or the ERROR_MARK_NODE) indicating the type that was specified. */
21059 static tree
21060 cp_parser_base_specifier (cp_parser* parser)
21062 cp_token *token;
21063 bool done = false;
21064 bool virtual_p = false;
21065 bool duplicate_virtual_error_issued_p = false;
21066 bool duplicate_access_error_issued_p = false;
21067 bool class_scope_p, template_p;
21068 tree access = access_default_node;
21069 tree type;
21071 /* Process the optional `virtual' and `access-specifier'. */
21072 while (!done)
21074 /* Peek at the next token. */
21075 token = cp_lexer_peek_token (parser->lexer);
21076 /* Process `virtual'. */
21077 switch (token->keyword)
21079 case RID_VIRTUAL:
21080 /* If `virtual' appears more than once, issue an error. */
21081 if (virtual_p && !duplicate_virtual_error_issued_p)
21083 cp_parser_error (parser,
21084 "%<virtual%> specified more than once in base-specified");
21085 duplicate_virtual_error_issued_p = true;
21088 virtual_p = true;
21090 /* Consume the `virtual' token. */
21091 cp_lexer_consume_token (parser->lexer);
21093 break;
21095 case RID_PUBLIC:
21096 case RID_PROTECTED:
21097 case RID_PRIVATE:
21098 /* If more than one access specifier appears, issue an
21099 error. */
21100 if (access != access_default_node
21101 && !duplicate_access_error_issued_p)
21103 cp_parser_error (parser,
21104 "more than one access specifier in base-specified");
21105 duplicate_access_error_issued_p = true;
21108 access = ridpointers[(int) token->keyword];
21110 /* Consume the access-specifier. */
21111 cp_lexer_consume_token (parser->lexer);
21113 break;
21115 default:
21116 done = true;
21117 break;
21120 /* It is not uncommon to see programs mechanically, erroneously, use
21121 the 'typename' keyword to denote (dependent) qualified types
21122 as base classes. */
21123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
21125 token = cp_lexer_peek_token (parser->lexer);
21126 if (!processing_template_decl)
21127 error_at (token->location,
21128 "keyword %<typename%> not allowed outside of templates");
21129 else
21130 error_at (token->location,
21131 "keyword %<typename%> not allowed in this context "
21132 "(the base class is implicitly a type)");
21133 cp_lexer_consume_token (parser->lexer);
21136 /* Look for the optional `::' operator. */
21137 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21138 /* Look for the nested-name-specifier. The simplest way to
21139 implement:
21141 [temp.res]
21143 The keyword `typename' is not permitted in a base-specifier or
21144 mem-initializer; in these contexts a qualified name that
21145 depends on a template-parameter is implicitly assumed to be a
21146 type name.
21148 is to pretend that we have seen the `typename' keyword at this
21149 point. */
21150 cp_parser_nested_name_specifier_opt (parser,
21151 /*typename_keyword_p=*/true,
21152 /*check_dependency_p=*/true,
21153 typename_type,
21154 /*is_declaration=*/true);
21155 /* If the base class is given by a qualified name, assume that names
21156 we see are type names or templates, as appropriate. */
21157 class_scope_p = (parser->scope && TYPE_P (parser->scope));
21158 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
21160 if (!parser->scope
21161 && cp_lexer_next_token_is_decltype (parser->lexer))
21162 /* DR 950 allows decltype as a base-specifier. */
21163 type = cp_parser_decltype (parser);
21164 else
21166 /* Otherwise, look for the class-name. */
21167 type = cp_parser_class_name (parser,
21168 class_scope_p,
21169 template_p,
21170 typename_type,
21171 /*check_dependency_p=*/true,
21172 /*class_head_p=*/false,
21173 /*is_declaration=*/true);
21174 type = TREE_TYPE (type);
21177 if (type == error_mark_node)
21178 return error_mark_node;
21180 return finish_base_specifier (type, access, virtual_p);
21183 /* Exception handling [gram.exception] */
21185 /* Parse an (optional) noexcept-specification.
21187 noexcept-specification:
21188 noexcept ( constant-expression ) [opt]
21190 If no noexcept-specification is present, returns NULL_TREE.
21191 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
21192 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
21193 there are no parentheses. CONSUMED_EXPR will be set accordingly.
21194 Otherwise, returns a noexcept specification unless RETURN_COND is true,
21195 in which case a boolean condition is returned instead. */
21197 static tree
21198 cp_parser_noexcept_specification_opt (cp_parser* parser,
21199 bool require_constexpr,
21200 bool* consumed_expr,
21201 bool return_cond)
21203 cp_token *token;
21204 const char *saved_message;
21206 /* Peek at the next token. */
21207 token = cp_lexer_peek_token (parser->lexer);
21209 /* Is it a noexcept-specification? */
21210 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
21212 tree expr;
21213 cp_lexer_consume_token (parser->lexer);
21215 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
21217 cp_lexer_consume_token (parser->lexer);
21219 if (require_constexpr)
21221 /* Types may not be defined in an exception-specification. */
21222 saved_message = parser->type_definition_forbidden_message;
21223 parser->type_definition_forbidden_message
21224 = G_("types may not be defined in an exception-specification");
21226 expr = cp_parser_constant_expression (parser, false, NULL);
21228 /* Restore the saved message. */
21229 parser->type_definition_forbidden_message = saved_message;
21231 else
21233 expr = cp_parser_expression (parser);
21234 *consumed_expr = true;
21237 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21239 else
21241 expr = boolean_true_node;
21242 if (!require_constexpr)
21243 *consumed_expr = false;
21246 /* We cannot build a noexcept-spec right away because this will check
21247 that expr is a constexpr. */
21248 if (!return_cond)
21249 return build_noexcept_spec (expr, tf_warning_or_error);
21250 else
21251 return expr;
21253 else
21254 return NULL_TREE;
21257 /* Parse an (optional) exception-specification.
21259 exception-specification:
21260 throw ( type-id-list [opt] )
21262 Returns a TREE_LIST representing the exception-specification. The
21263 TREE_VALUE of each node is a type. */
21265 static tree
21266 cp_parser_exception_specification_opt (cp_parser* parser)
21268 cp_token *token;
21269 tree type_id_list;
21270 const char *saved_message;
21272 /* Peek at the next token. */
21273 token = cp_lexer_peek_token (parser->lexer);
21275 /* Is it a noexcept-specification? */
21276 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
21277 false);
21278 if (type_id_list != NULL_TREE)
21279 return type_id_list;
21281 /* If it's not `throw', then there's no exception-specification. */
21282 if (!cp_parser_is_keyword (token, RID_THROW))
21283 return NULL_TREE;
21285 #if 0
21286 /* Enable this once a lot of code has transitioned to noexcept? */
21287 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
21288 warning (OPT_Wdeprecated, "dynamic exception specifications are "
21289 "deprecated in C++0x; use %<noexcept%> instead");
21290 #endif
21292 /* Consume the `throw'. */
21293 cp_lexer_consume_token (parser->lexer);
21295 /* Look for the `('. */
21296 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21298 /* Peek at the next token. */
21299 token = cp_lexer_peek_token (parser->lexer);
21300 /* If it's not a `)', then there is a type-id-list. */
21301 if (token->type != CPP_CLOSE_PAREN)
21303 /* Types may not be defined in an exception-specification. */
21304 saved_message = parser->type_definition_forbidden_message;
21305 parser->type_definition_forbidden_message
21306 = G_("types may not be defined in an exception-specification");
21307 /* Parse the type-id-list. */
21308 type_id_list = cp_parser_type_id_list (parser);
21309 /* Restore the saved message. */
21310 parser->type_definition_forbidden_message = saved_message;
21312 else
21313 type_id_list = empty_except_spec;
21315 /* Look for the `)'. */
21316 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21318 return type_id_list;
21321 /* Parse an (optional) type-id-list.
21323 type-id-list:
21324 type-id ... [opt]
21325 type-id-list , type-id ... [opt]
21327 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
21328 in the order that the types were presented. */
21330 static tree
21331 cp_parser_type_id_list (cp_parser* parser)
21333 tree types = NULL_TREE;
21335 while (true)
21337 cp_token *token;
21338 tree type;
21340 /* Get the next type-id. */
21341 type = cp_parser_type_id (parser);
21342 /* Parse the optional ellipsis. */
21343 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21345 /* Consume the `...'. */
21346 cp_lexer_consume_token (parser->lexer);
21348 /* Turn the type into a pack expansion expression. */
21349 type = make_pack_expansion (type);
21351 /* Add it to the list. */
21352 types = add_exception_specifier (types, type, /*complain=*/1);
21353 /* Peek at the next token. */
21354 token = cp_lexer_peek_token (parser->lexer);
21355 /* If it is not a `,', we are done. */
21356 if (token->type != CPP_COMMA)
21357 break;
21358 /* Consume the `,'. */
21359 cp_lexer_consume_token (parser->lexer);
21362 return nreverse (types);
21365 /* Parse a try-block.
21367 try-block:
21368 try compound-statement handler-seq */
21370 static tree
21371 cp_parser_try_block (cp_parser* parser)
21373 tree try_block;
21375 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
21376 try_block = begin_try_block ();
21377 cp_parser_compound_statement (parser, NULL, true, false);
21378 finish_try_block (try_block);
21379 cp_parser_handler_seq (parser);
21380 finish_handler_sequence (try_block);
21382 return try_block;
21385 /* Parse a function-try-block.
21387 function-try-block:
21388 try ctor-initializer [opt] function-body handler-seq */
21390 static bool
21391 cp_parser_function_try_block (cp_parser* parser)
21393 tree compound_stmt;
21394 tree try_block;
21395 bool ctor_initializer_p;
21397 /* Look for the `try' keyword. */
21398 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
21399 return false;
21400 /* Let the rest of the front end know where we are. */
21401 try_block = begin_function_try_block (&compound_stmt);
21402 /* Parse the function-body. */
21403 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
21404 (parser, /*in_function_try_block=*/true);
21405 /* We're done with the `try' part. */
21406 finish_function_try_block (try_block);
21407 /* Parse the handlers. */
21408 cp_parser_handler_seq (parser);
21409 /* We're done with the handlers. */
21410 finish_function_handler_sequence (try_block, compound_stmt);
21412 return ctor_initializer_p;
21415 /* Parse a handler-seq.
21417 handler-seq:
21418 handler handler-seq [opt] */
21420 static void
21421 cp_parser_handler_seq (cp_parser* parser)
21423 while (true)
21425 cp_token *token;
21427 /* Parse the handler. */
21428 cp_parser_handler (parser);
21429 /* Peek at the next token. */
21430 token = cp_lexer_peek_token (parser->lexer);
21431 /* If it's not `catch' then there are no more handlers. */
21432 if (!cp_parser_is_keyword (token, RID_CATCH))
21433 break;
21437 /* Parse a handler.
21439 handler:
21440 catch ( exception-declaration ) compound-statement */
21442 static void
21443 cp_parser_handler (cp_parser* parser)
21445 tree handler;
21446 tree declaration;
21448 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
21449 handler = begin_handler ();
21450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21451 declaration = cp_parser_exception_declaration (parser);
21452 finish_handler_parms (declaration, handler);
21453 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21454 cp_parser_compound_statement (parser, NULL, false, false);
21455 finish_handler (handler);
21458 /* Parse an exception-declaration.
21460 exception-declaration:
21461 type-specifier-seq declarator
21462 type-specifier-seq abstract-declarator
21463 type-specifier-seq
21466 Returns a VAR_DECL for the declaration, or NULL_TREE if the
21467 ellipsis variant is used. */
21469 static tree
21470 cp_parser_exception_declaration (cp_parser* parser)
21472 cp_decl_specifier_seq type_specifiers;
21473 cp_declarator *declarator;
21474 const char *saved_message;
21476 /* If it's an ellipsis, it's easy to handle. */
21477 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21479 /* Consume the `...' token. */
21480 cp_lexer_consume_token (parser->lexer);
21481 return NULL_TREE;
21484 /* Types may not be defined in exception-declarations. */
21485 saved_message = parser->type_definition_forbidden_message;
21486 parser->type_definition_forbidden_message
21487 = G_("types may not be defined in exception-declarations");
21489 /* Parse the type-specifier-seq. */
21490 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
21491 /*is_trailing_return=*/false,
21492 &type_specifiers);
21493 /* If it's a `)', then there is no declarator. */
21494 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
21495 declarator = NULL;
21496 else
21497 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
21498 /*ctor_dtor_or_conv_p=*/NULL,
21499 /*parenthesized_p=*/NULL,
21500 /*member_p=*/false,
21501 /*friend_p=*/false);
21503 /* Restore the saved message. */
21504 parser->type_definition_forbidden_message = saved_message;
21506 if (!type_specifiers.any_specifiers_p)
21507 return error_mark_node;
21509 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
21512 /* Parse a throw-expression.
21514 throw-expression:
21515 throw assignment-expression [opt]
21517 Returns a THROW_EXPR representing the throw-expression. */
21519 static tree
21520 cp_parser_throw_expression (cp_parser* parser)
21522 tree expression;
21523 cp_token* token;
21525 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
21526 token = cp_lexer_peek_token (parser->lexer);
21527 /* Figure out whether or not there is an assignment-expression
21528 following the "throw" keyword. */
21529 if (token->type == CPP_COMMA
21530 || token->type == CPP_SEMICOLON
21531 || token->type == CPP_CLOSE_PAREN
21532 || token->type == CPP_CLOSE_SQUARE
21533 || token->type == CPP_CLOSE_BRACE
21534 || token->type == CPP_COLON)
21535 expression = NULL_TREE;
21536 else
21537 expression = cp_parser_assignment_expression (parser,
21538 /*cast_p=*/false, NULL);
21540 return build_throw (expression);
21543 /* GNU Extensions */
21545 /* Parse an (optional) asm-specification.
21547 asm-specification:
21548 asm ( string-literal )
21550 If the asm-specification is present, returns a STRING_CST
21551 corresponding to the string-literal. Otherwise, returns
21552 NULL_TREE. */
21554 static tree
21555 cp_parser_asm_specification_opt (cp_parser* parser)
21557 cp_token *token;
21558 tree asm_specification;
21560 /* Peek at the next token. */
21561 token = cp_lexer_peek_token (parser->lexer);
21562 /* If the next token isn't the `asm' keyword, then there's no
21563 asm-specification. */
21564 if (!cp_parser_is_keyword (token, RID_ASM))
21565 return NULL_TREE;
21567 /* Consume the `asm' token. */
21568 cp_lexer_consume_token (parser->lexer);
21569 /* Look for the `('. */
21570 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21572 /* Look for the string-literal. */
21573 asm_specification = cp_parser_string_literal (parser, false, false);
21575 /* Look for the `)'. */
21576 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21578 return asm_specification;
21581 /* Parse an asm-operand-list.
21583 asm-operand-list:
21584 asm-operand
21585 asm-operand-list , asm-operand
21587 asm-operand:
21588 string-literal ( expression )
21589 [ string-literal ] string-literal ( expression )
21591 Returns a TREE_LIST representing the operands. The TREE_VALUE of
21592 each node is the expression. The TREE_PURPOSE is itself a
21593 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
21594 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
21595 is a STRING_CST for the string literal before the parenthesis. Returns
21596 ERROR_MARK_NODE if any of the operands are invalid. */
21598 static tree
21599 cp_parser_asm_operand_list (cp_parser* parser)
21601 tree asm_operands = NULL_TREE;
21602 bool invalid_operands = false;
21604 while (true)
21606 tree string_literal;
21607 tree expression;
21608 tree name;
21610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21612 /* Consume the `[' token. */
21613 cp_lexer_consume_token (parser->lexer);
21614 /* Read the operand name. */
21615 name = cp_parser_identifier (parser);
21616 if (name != error_mark_node)
21617 name = build_string (IDENTIFIER_LENGTH (name),
21618 IDENTIFIER_POINTER (name));
21619 /* Look for the closing `]'. */
21620 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21622 else
21623 name = NULL_TREE;
21624 /* Look for the string-literal. */
21625 string_literal = cp_parser_string_literal (parser, false, false);
21627 /* Look for the `('. */
21628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21629 /* Parse the expression. */
21630 expression = cp_parser_expression (parser);
21631 /* Look for the `)'. */
21632 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21634 if (name == error_mark_node
21635 || string_literal == error_mark_node
21636 || expression == error_mark_node)
21637 invalid_operands = true;
21639 /* Add this operand to the list. */
21640 asm_operands = tree_cons (build_tree_list (name, string_literal),
21641 expression,
21642 asm_operands);
21643 /* If the next token is not a `,', there are no more
21644 operands. */
21645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21646 break;
21647 /* Consume the `,'. */
21648 cp_lexer_consume_token (parser->lexer);
21651 return invalid_operands ? error_mark_node : nreverse (asm_operands);
21654 /* Parse an asm-clobber-list.
21656 asm-clobber-list:
21657 string-literal
21658 asm-clobber-list , string-literal
21660 Returns a TREE_LIST, indicating the clobbers in the order that they
21661 appeared. The TREE_VALUE of each node is a STRING_CST. */
21663 static tree
21664 cp_parser_asm_clobber_list (cp_parser* parser)
21666 tree clobbers = NULL_TREE;
21668 while (true)
21670 tree string_literal;
21672 /* Look for the string literal. */
21673 string_literal = cp_parser_string_literal (parser, false, false);
21674 /* Add it to the list. */
21675 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
21676 /* If the next token is not a `,', then the list is
21677 complete. */
21678 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21679 break;
21680 /* Consume the `,' token. */
21681 cp_lexer_consume_token (parser->lexer);
21684 return clobbers;
21687 /* Parse an asm-label-list.
21689 asm-label-list:
21690 identifier
21691 asm-label-list , identifier
21693 Returns a TREE_LIST, indicating the labels in the order that they
21694 appeared. The TREE_VALUE of each node is a label. */
21696 static tree
21697 cp_parser_asm_label_list (cp_parser* parser)
21699 tree labels = NULL_TREE;
21701 while (true)
21703 tree identifier, label, name;
21705 /* Look for the identifier. */
21706 identifier = cp_parser_identifier (parser);
21707 if (!error_operand_p (identifier))
21709 label = lookup_label (identifier);
21710 if (TREE_CODE (label) == LABEL_DECL)
21712 TREE_USED (label) = 1;
21713 check_goto (label);
21714 name = build_string (IDENTIFIER_LENGTH (identifier),
21715 IDENTIFIER_POINTER (identifier));
21716 labels = tree_cons (name, label, labels);
21719 /* If the next token is not a `,', then the list is
21720 complete. */
21721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21722 break;
21723 /* Consume the `,' token. */
21724 cp_lexer_consume_token (parser->lexer);
21727 return nreverse (labels);
21730 /* Return TRUE iff the next tokens in the stream are possibly the
21731 beginning of a GNU extension attribute. */
21733 static bool
21734 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
21736 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
21739 /* Return TRUE iff the next tokens in the stream are possibly the
21740 beginning of a standard C++-11 attribute specifier. */
21742 static bool
21743 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
21745 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
21748 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21749 beginning of a standard C++-11 attribute specifier. */
21751 static bool
21752 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
21754 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21756 return (cxx_dialect >= cxx11
21757 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
21758 || (token->type == CPP_OPEN_SQUARE
21759 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
21760 && token->type == CPP_OPEN_SQUARE)));
21763 /* Return TRUE iff the next Nth tokens in the stream are possibly the
21764 beginning of a GNU extension attribute. */
21766 static bool
21767 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
21769 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
21771 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
21774 /* Return true iff the next tokens can be the beginning of either a
21775 GNU attribute list, or a standard C++11 attribute sequence. */
21777 static bool
21778 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
21780 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
21781 || cp_next_tokens_can_be_std_attribute_p (parser));
21784 /* Return true iff the next Nth tokens can be the beginning of either
21785 a GNU attribute list, or a standard C++11 attribute sequence. */
21787 static bool
21788 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
21790 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
21791 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
21794 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
21795 of GNU attributes, or return NULL. */
21797 static tree
21798 cp_parser_attributes_opt (cp_parser *parser)
21800 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21801 return cp_parser_gnu_attributes_opt (parser);
21802 return cp_parser_std_attribute_spec_seq (parser);
21805 #define CILK_SIMD_FN_CLAUSE_MASK \
21806 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
21807 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
21808 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
21809 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
21810 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
21812 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
21813 vector [(<clauses>)] */
21815 static void
21816 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
21818 bool first_p = parser->cilk_simd_fn_info == NULL;
21819 cp_token *token = v_token;
21820 if (first_p)
21822 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
21823 parser->cilk_simd_fn_info->error_seen = false;
21824 parser->cilk_simd_fn_info->fndecl_seen = false;
21825 parser->cilk_simd_fn_info->tokens = vNULL;
21827 int paren_scope = 0;
21828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21830 cp_lexer_consume_token (parser->lexer);
21831 v_token = cp_lexer_peek_token (parser->lexer);
21832 paren_scope++;
21834 while (paren_scope > 0)
21836 token = cp_lexer_peek_token (parser->lexer);
21837 if (token->type == CPP_OPEN_PAREN)
21838 paren_scope++;
21839 else if (token->type == CPP_CLOSE_PAREN)
21840 paren_scope--;
21841 /* Do not push the last ')' */
21842 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
21843 cp_lexer_consume_token (parser->lexer);
21846 token->type = CPP_PRAGMA_EOL;
21847 parser->lexer->next_token = token;
21848 cp_lexer_consume_token (parser->lexer);
21850 struct cp_token_cache *cp
21851 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
21852 parser->cilk_simd_fn_info->tokens.safe_push (cp);
21855 /* Parse an (optional) series of attributes.
21857 attributes:
21858 attributes attribute
21860 attribute:
21861 __attribute__ (( attribute-list [opt] ))
21863 The return value is as for cp_parser_gnu_attribute_list. */
21865 static tree
21866 cp_parser_gnu_attributes_opt (cp_parser* parser)
21868 tree attributes = NULL_TREE;
21870 while (true)
21872 cp_token *token;
21873 tree attribute_list;
21874 bool ok = true;
21876 /* Peek at the next token. */
21877 token = cp_lexer_peek_token (parser->lexer);
21878 /* If it's not `__attribute__', then we're done. */
21879 if (token->keyword != RID_ATTRIBUTE)
21880 break;
21882 /* Consume the `__attribute__' keyword. */
21883 cp_lexer_consume_token (parser->lexer);
21884 /* Look for the two `(' tokens. */
21885 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21886 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21888 /* Peek at the next token. */
21889 token = cp_lexer_peek_token (parser->lexer);
21890 if (token->type != CPP_CLOSE_PAREN)
21891 /* Parse the attribute-list. */
21892 attribute_list = cp_parser_gnu_attribute_list (parser);
21893 else
21894 /* If the next token is a `)', then there is no attribute
21895 list. */
21896 attribute_list = NULL;
21898 /* Look for the two `)' tokens. */
21899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21900 ok = false;
21901 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
21902 ok = false;
21903 if (!ok)
21904 cp_parser_skip_to_end_of_statement (parser);
21906 /* Add these new attributes to the list. */
21907 attributes = chainon (attributes, attribute_list);
21910 return attributes;
21913 /* Returns true of NAME is an IDENTIFIER_NODE with identiifer "vector,"
21914 "__vector" or "__vector__." */
21916 static inline bool
21917 is_cilkplus_vector_p (tree name)
21919 if (flag_cilkplus && is_attribute_p ("vector", name))
21920 return true;
21921 return false;
21924 /* Parse a GNU attribute-list.
21926 attribute-list:
21927 attribute
21928 attribute-list , attribute
21930 attribute:
21931 identifier
21932 identifier ( identifier )
21933 identifier ( identifier , expression-list )
21934 identifier ( expression-list )
21936 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
21937 to an attribute. The TREE_PURPOSE of each node is the identifier
21938 indicating which attribute is in use. The TREE_VALUE represents
21939 the arguments, if any. */
21941 static tree
21942 cp_parser_gnu_attribute_list (cp_parser* parser)
21944 tree attribute_list = NULL_TREE;
21945 bool save_translate_strings_p = parser->translate_strings_p;
21947 parser->translate_strings_p = false;
21948 while (true)
21950 cp_token *token;
21951 tree identifier;
21952 tree attribute;
21954 /* Look for the identifier. We also allow keywords here; for
21955 example `__attribute__ ((const))' is legal. */
21956 token = cp_lexer_peek_token (parser->lexer);
21957 if (token->type == CPP_NAME
21958 || token->type == CPP_KEYWORD)
21960 tree arguments = NULL_TREE;
21962 /* Consume the token, but save it since we need it for the
21963 SIMD enabled function parsing. */
21964 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
21966 /* Save away the identifier that indicates which attribute
21967 this is. */
21968 identifier = (token->type == CPP_KEYWORD)
21969 /* For keywords, use the canonical spelling, not the
21970 parsed identifier. */
21971 ? ridpointers[(int) token->keyword]
21972 : id_token->u.value;
21974 attribute = build_tree_list (identifier, NULL_TREE);
21976 /* Peek at the next token. */
21977 token = cp_lexer_peek_token (parser->lexer);
21978 /* If it's an `(', then parse the attribute arguments. */
21979 if (token->type == CPP_OPEN_PAREN)
21981 vec<tree, va_gc> *vec;
21982 int attr_flag = (attribute_takes_identifier_p (identifier)
21983 ? id_attr : normal_attr);
21984 if (is_cilkplus_vector_p (identifier))
21986 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
21987 continue;
21989 else
21990 vec = cp_parser_parenthesized_expression_list
21991 (parser, attr_flag, /*cast_p=*/false,
21992 /*allow_expansion_p=*/false,
21993 /*non_constant_p=*/NULL);
21994 if (vec == NULL)
21995 arguments = error_mark_node;
21996 else
21998 arguments = build_tree_list_vec (vec);
21999 release_tree_vector (vec);
22001 /* Save the arguments away. */
22002 TREE_VALUE (attribute) = arguments;
22004 else if (is_cilkplus_vector_p (identifier))
22006 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
22007 continue;
22010 if (arguments != error_mark_node)
22012 /* Add this attribute to the list. */
22013 TREE_CHAIN (attribute) = attribute_list;
22014 attribute_list = attribute;
22017 token = cp_lexer_peek_token (parser->lexer);
22019 /* Now, look for more attributes. If the next token isn't a
22020 `,', we're done. */
22021 if (token->type != CPP_COMMA)
22022 break;
22024 /* Consume the comma and keep going. */
22025 cp_lexer_consume_token (parser->lexer);
22027 parser->translate_strings_p = save_translate_strings_p;
22029 /* We built up the list in reverse order. */
22030 return nreverse (attribute_list);
22033 /* Parse a standard C++11 attribute.
22035 The returned representation is a TREE_LIST which TREE_PURPOSE is
22036 the scoped name of the attribute, and the TREE_VALUE is its
22037 arguments list.
22039 Note that the scoped name of the attribute is itself a TREE_LIST
22040 which TREE_PURPOSE is the namespace of the attribute, and
22041 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
22042 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
22043 and which TREE_PURPOSE is directly the attribute name.
22045 Clients of the attribute code should use get_attribute_namespace
22046 and get_attribute_name to get the actual namespace and name of
22047 attributes, regardless of their being GNU or C++11 attributes.
22049 attribute:
22050 attribute-token attribute-argument-clause [opt]
22052 attribute-token:
22053 identifier
22054 attribute-scoped-token
22056 attribute-scoped-token:
22057 attribute-namespace :: identifier
22059 attribute-namespace:
22060 identifier
22062 attribute-argument-clause:
22063 ( balanced-token-seq )
22065 balanced-token-seq:
22066 balanced-token [opt]
22067 balanced-token-seq balanced-token
22069 balanced-token:
22070 ( balanced-token-seq )
22071 [ balanced-token-seq ]
22072 { balanced-token-seq }. */
22074 static tree
22075 cp_parser_std_attribute (cp_parser *parser)
22077 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
22078 cp_token *token;
22080 /* First, parse name of the the attribute, a.k.a
22081 attribute-token. */
22083 token = cp_lexer_peek_token (parser->lexer);
22084 if (token->type == CPP_NAME)
22085 attr_id = token->u.value;
22086 else if (token->type == CPP_KEYWORD)
22087 attr_id = ridpointers[(int) token->keyword];
22088 else if (token->flags & NAMED_OP)
22089 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
22091 if (attr_id == NULL_TREE)
22092 return NULL_TREE;
22094 cp_lexer_consume_token (parser->lexer);
22096 token = cp_lexer_peek_token (parser->lexer);
22097 if (token->type == CPP_SCOPE)
22099 /* We are seeing a scoped attribute token. */
22101 cp_lexer_consume_token (parser->lexer);
22102 attr_ns = attr_id;
22104 token = cp_lexer_consume_token (parser->lexer);
22105 if (token->type == CPP_NAME)
22106 attr_id = token->u.value;
22107 else if (token->type == CPP_KEYWORD)
22108 attr_id = ridpointers[(int) token->keyword];
22109 else
22111 error_at (token->location,
22112 "expected an identifier for the attribute name");
22113 return error_mark_node;
22115 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
22116 NULL_TREE);
22117 token = cp_lexer_peek_token (parser->lexer);
22119 else
22121 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
22122 NULL_TREE);
22123 /* C++11 noreturn attribute is equivalent to GNU's. */
22124 if (is_attribute_p ("noreturn", attr_id))
22125 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22126 /* C++14 deprecated attribute is equivalent to GNU's. */
22127 else if (cxx_dialect >= cxx14 && is_attribute_p ("deprecated", attr_id))
22128 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
22131 /* Now parse the optional argument clause of the attribute. */
22133 if (token->type != CPP_OPEN_PAREN)
22134 return attribute;
22137 vec<tree, va_gc> *vec;
22138 int attr_flag = normal_attr;
22140 if (attr_ns == get_identifier ("gnu")
22141 && attribute_takes_identifier_p (attr_id))
22142 /* A GNU attribute that takes an identifier in parameter. */
22143 attr_flag = id_attr;
22145 vec = cp_parser_parenthesized_expression_list
22146 (parser, attr_flag, /*cast_p=*/false,
22147 /*allow_expansion_p=*/true,
22148 /*non_constant_p=*/NULL);
22149 if (vec == NULL)
22150 arguments = error_mark_node;
22151 else
22153 arguments = build_tree_list_vec (vec);
22154 release_tree_vector (vec);
22157 if (arguments == error_mark_node)
22158 attribute = error_mark_node;
22159 else
22160 TREE_VALUE (attribute) = arguments;
22163 return attribute;
22166 /* Parse a list of standard C++-11 attributes.
22168 attribute-list:
22169 attribute [opt]
22170 attribute-list , attribute[opt]
22171 attribute ...
22172 attribute-list , attribute ...
22175 static tree
22176 cp_parser_std_attribute_list (cp_parser *parser)
22178 tree attributes = NULL_TREE, attribute = NULL_TREE;
22179 cp_token *token = NULL;
22181 while (true)
22183 attribute = cp_parser_std_attribute (parser);
22184 if (attribute == error_mark_node)
22185 break;
22186 if (attribute != NULL_TREE)
22188 TREE_CHAIN (attribute) = attributes;
22189 attributes = attribute;
22191 token = cp_lexer_peek_token (parser->lexer);
22192 if (token->type != CPP_COMMA)
22193 break;
22194 cp_lexer_consume_token (parser->lexer);
22196 attributes = nreverse (attributes);
22197 return attributes;
22200 /* Parse a standard C++-11 attribute specifier.
22202 attribute-specifier:
22203 [ [ attribute-list ] ]
22204 alignment-specifier
22206 alignment-specifier:
22207 alignas ( type-id ... [opt] )
22208 alignas ( alignment-expression ... [opt] ). */
22210 static tree
22211 cp_parser_std_attribute_spec (cp_parser *parser)
22213 tree attributes = NULL_TREE;
22214 cp_token *token = cp_lexer_peek_token (parser->lexer);
22216 if (token->type == CPP_OPEN_SQUARE
22217 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
22219 cp_lexer_consume_token (parser->lexer);
22220 cp_lexer_consume_token (parser->lexer);
22222 attributes = cp_parser_std_attribute_list (parser);
22224 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
22225 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
22226 cp_parser_skip_to_end_of_statement (parser);
22227 else
22228 /* Warn about parsing c++11 attribute in non-c++1 mode, only
22229 when we are sure that we have actually parsed them. */
22230 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22232 else
22234 tree alignas_expr;
22236 /* Look for an alignment-specifier. */
22238 token = cp_lexer_peek_token (parser->lexer);
22240 if (token->type != CPP_KEYWORD
22241 || token->keyword != RID_ALIGNAS)
22242 return NULL_TREE;
22244 cp_lexer_consume_token (parser->lexer);
22245 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
22247 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
22249 cp_parser_error (parser, "expected %<(%>");
22250 return error_mark_node;
22253 cp_parser_parse_tentatively (parser);
22254 alignas_expr = cp_parser_type_id (parser);
22256 if (!cp_parser_parse_definitely (parser))
22258 gcc_assert (alignas_expr == error_mark_node
22259 || alignas_expr == NULL_TREE);
22261 alignas_expr =
22262 cp_parser_assignment_expression (parser, /*cast_p=*/false,
22263 /**cp_id_kind=*/NULL);
22264 if (alignas_expr == error_mark_node)
22265 cp_parser_skip_to_end_of_statement (parser);
22266 if (alignas_expr == NULL_TREE
22267 || alignas_expr == error_mark_node)
22268 return alignas_expr;
22271 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
22273 cp_parser_error (parser, "expected %<)%>");
22274 return error_mark_node;
22277 alignas_expr = cxx_alignas_expr (alignas_expr);
22279 /* Build the C++-11 representation of an 'aligned'
22280 attribute. */
22281 attributes =
22282 build_tree_list (build_tree_list (get_identifier ("gnu"),
22283 get_identifier ("aligned")),
22284 build_tree_list (NULL_TREE, alignas_expr));
22287 return attributes;
22290 /* Parse a standard C++-11 attribute-specifier-seq.
22292 attribute-specifier-seq:
22293 attribute-specifier-seq [opt] attribute-specifier
22296 static tree
22297 cp_parser_std_attribute_spec_seq (cp_parser *parser)
22299 tree attr_specs = NULL;
22301 while (true)
22303 tree attr_spec = cp_parser_std_attribute_spec (parser);
22304 if (attr_spec == NULL_TREE)
22305 break;
22306 if (attr_spec == error_mark_node)
22307 return error_mark_node;
22309 TREE_CHAIN (attr_spec) = attr_specs;
22310 attr_specs = attr_spec;
22313 attr_specs = nreverse (attr_specs);
22314 return attr_specs;
22317 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
22318 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
22319 current value of the PEDANTIC flag, regardless of whether or not
22320 the `__extension__' keyword is present. The caller is responsible
22321 for restoring the value of the PEDANTIC flag. */
22323 static bool
22324 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
22326 /* Save the old value of the PEDANTIC flag. */
22327 *saved_pedantic = pedantic;
22329 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
22331 /* Consume the `__extension__' token. */
22332 cp_lexer_consume_token (parser->lexer);
22333 /* We're not being pedantic while the `__extension__' keyword is
22334 in effect. */
22335 pedantic = 0;
22337 return true;
22340 return false;
22343 /* Parse a label declaration.
22345 label-declaration:
22346 __label__ label-declarator-seq ;
22348 label-declarator-seq:
22349 identifier , label-declarator-seq
22350 identifier */
22352 static void
22353 cp_parser_label_declaration (cp_parser* parser)
22355 /* Look for the `__label__' keyword. */
22356 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
22358 while (true)
22360 tree identifier;
22362 /* Look for an identifier. */
22363 identifier = cp_parser_identifier (parser);
22364 /* If we failed, stop. */
22365 if (identifier == error_mark_node)
22366 break;
22367 /* Declare it as a label. */
22368 finish_label_decl (identifier);
22369 /* If the next token is a `;', stop. */
22370 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22371 break;
22372 /* Look for the `,' separating the label declarations. */
22373 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
22376 /* Look for the final `;'. */
22377 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22380 /* Support Functions */
22382 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
22383 NAME should have one of the representations used for an
22384 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
22385 is returned. If PARSER->SCOPE is a dependent type, then a
22386 SCOPE_REF is returned.
22388 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
22389 returned; the name was already resolved when the TEMPLATE_ID_EXPR
22390 was formed. Abstractly, such entities should not be passed to this
22391 function, because they do not need to be looked up, but it is
22392 simpler to check for this special case here, rather than at the
22393 call-sites.
22395 In cases not explicitly covered above, this function returns a
22396 DECL, OVERLOAD, or baselink representing the result of the lookup.
22397 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
22398 is returned.
22400 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
22401 (e.g., "struct") that was used. In that case bindings that do not
22402 refer to types are ignored.
22404 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
22405 ignored.
22407 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
22408 are ignored.
22410 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
22411 types.
22413 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
22414 TREE_LIST of candidates if name-lookup results in an ambiguity, and
22415 NULL_TREE otherwise. */
22417 static tree
22418 cp_parser_lookup_name (cp_parser *parser, tree name,
22419 enum tag_types tag_type,
22420 bool is_template,
22421 bool is_namespace,
22422 bool check_dependency,
22423 tree *ambiguous_decls,
22424 location_t name_location)
22426 tree decl;
22427 tree object_type = parser->context->object_type;
22429 /* Assume that the lookup will be unambiguous. */
22430 if (ambiguous_decls)
22431 *ambiguous_decls = NULL_TREE;
22433 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
22434 no longer valid. Note that if we are parsing tentatively, and
22435 the parse fails, OBJECT_TYPE will be automatically restored. */
22436 parser->context->object_type = NULL_TREE;
22438 if (name == error_mark_node)
22439 return error_mark_node;
22441 /* A template-id has already been resolved; there is no lookup to
22442 do. */
22443 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
22444 return name;
22445 if (BASELINK_P (name))
22447 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
22448 == TEMPLATE_ID_EXPR);
22449 return name;
22452 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
22453 it should already have been checked to make sure that the name
22454 used matches the type being destroyed. */
22455 if (TREE_CODE (name) == BIT_NOT_EXPR)
22457 tree type;
22459 /* Figure out to which type this destructor applies. */
22460 if (parser->scope)
22461 type = parser->scope;
22462 else if (object_type)
22463 type = object_type;
22464 else
22465 type = current_class_type;
22466 /* If that's not a class type, there is no destructor. */
22467 if (!type || !CLASS_TYPE_P (type))
22468 return error_mark_node;
22469 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
22470 lazily_declare_fn (sfk_destructor, type);
22471 if (!CLASSTYPE_DESTRUCTORS (type))
22472 return error_mark_node;
22473 /* If it was a class type, return the destructor. */
22474 return CLASSTYPE_DESTRUCTORS (type);
22477 /* By this point, the NAME should be an ordinary identifier. If
22478 the id-expression was a qualified name, the qualifying scope is
22479 stored in PARSER->SCOPE at this point. */
22480 gcc_assert (identifier_p (name));
22482 /* Perform the lookup. */
22483 if (parser->scope)
22485 bool dependent_p;
22487 if (parser->scope == error_mark_node)
22488 return error_mark_node;
22490 /* If the SCOPE is dependent, the lookup must be deferred until
22491 the template is instantiated -- unless we are explicitly
22492 looking up names in uninstantiated templates. Even then, we
22493 cannot look up the name if the scope is not a class type; it
22494 might, for example, be a template type parameter. */
22495 dependent_p = (TYPE_P (parser->scope)
22496 && dependent_scope_p (parser->scope));
22497 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
22498 && dependent_p)
22499 /* Defer lookup. */
22500 decl = error_mark_node;
22501 else
22503 tree pushed_scope = NULL_TREE;
22505 /* If PARSER->SCOPE is a dependent type, then it must be a
22506 class type, and we must not be checking dependencies;
22507 otherwise, we would have processed this lookup above. So
22508 that PARSER->SCOPE is not considered a dependent base by
22509 lookup_member, we must enter the scope here. */
22510 if (dependent_p)
22511 pushed_scope = push_scope (parser->scope);
22513 /* If the PARSER->SCOPE is a template specialization, it
22514 may be instantiated during name lookup. In that case,
22515 errors may be issued. Even if we rollback the current
22516 tentative parse, those errors are valid. */
22517 decl = lookup_qualified_name (parser->scope, name,
22518 tag_type != none_type,
22519 /*complain=*/true);
22521 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
22522 lookup result and the nested-name-specifier nominates a class C:
22523 * if the name specified after the nested-name-specifier, when
22524 looked up in C, is the injected-class-name of C (Clause 9), or
22525 * if the name specified after the nested-name-specifier is the
22526 same as the identifier or the simple-template-id's template-
22527 name in the last component of the nested-name-specifier,
22528 the name is instead considered to name the constructor of
22529 class C. [ Note: for example, the constructor is not an
22530 acceptable lookup result in an elaborated-type-specifier so
22531 the constructor would not be used in place of the
22532 injected-class-name. --end note ] Such a constructor name
22533 shall be used only in the declarator-id of a declaration that
22534 names a constructor or in a using-declaration. */
22535 if (tag_type == none_type
22536 && DECL_SELF_REFERENCE_P (decl)
22537 && same_type_p (DECL_CONTEXT (decl), parser->scope))
22538 decl = lookup_qualified_name (parser->scope, ctor_identifier,
22539 tag_type != none_type,
22540 /*complain=*/true);
22542 /* If we have a single function from a using decl, pull it out. */
22543 if (TREE_CODE (decl) == OVERLOAD
22544 && !really_overloaded_fn (decl))
22545 decl = OVL_FUNCTION (decl);
22547 if (pushed_scope)
22548 pop_scope (pushed_scope);
22551 /* If the scope is a dependent type and either we deferred lookup or
22552 we did lookup but didn't find the name, rememeber the name. */
22553 if (decl == error_mark_node && TYPE_P (parser->scope)
22554 && dependent_type_p (parser->scope))
22556 if (tag_type)
22558 tree type;
22560 /* The resolution to Core Issue 180 says that `struct
22561 A::B' should be considered a type-name, even if `A'
22562 is dependent. */
22563 type = make_typename_type (parser->scope, name, tag_type,
22564 /*complain=*/tf_error);
22565 if (type != error_mark_node)
22566 decl = TYPE_NAME (type);
22568 else if (is_template
22569 && (cp_parser_next_token_ends_template_argument_p (parser)
22570 || cp_lexer_next_token_is (parser->lexer,
22571 CPP_CLOSE_PAREN)))
22572 decl = make_unbound_class_template (parser->scope,
22573 name, NULL_TREE,
22574 /*complain=*/tf_error);
22575 else
22576 decl = build_qualified_name (/*type=*/NULL_TREE,
22577 parser->scope, name,
22578 is_template);
22580 parser->qualifying_scope = parser->scope;
22581 parser->object_scope = NULL_TREE;
22583 else if (object_type)
22585 /* Look up the name in the scope of the OBJECT_TYPE, unless the
22586 OBJECT_TYPE is not a class. */
22587 if (CLASS_TYPE_P (object_type))
22588 /* If the OBJECT_TYPE is a template specialization, it may
22589 be instantiated during name lookup. In that case, errors
22590 may be issued. Even if we rollback the current tentative
22591 parse, those errors are valid. */
22592 decl = lookup_member (object_type,
22593 name,
22594 /*protect=*/0,
22595 tag_type != none_type,
22596 tf_warning_or_error);
22597 else
22598 decl = NULL_TREE;
22600 if (!decl)
22601 /* Look it up in the enclosing context. */
22602 decl = lookup_name_real (name, tag_type != none_type,
22603 /*nonclass=*/0,
22604 /*block_p=*/true, is_namespace, 0);
22605 parser->object_scope = object_type;
22606 parser->qualifying_scope = NULL_TREE;
22608 else
22610 decl = lookup_name_real (name, tag_type != none_type,
22611 /*nonclass=*/0,
22612 /*block_p=*/true, is_namespace, 0);
22613 parser->qualifying_scope = NULL_TREE;
22614 parser->object_scope = NULL_TREE;
22617 /* If the lookup failed, let our caller know. */
22618 if (!decl || decl == error_mark_node)
22619 return error_mark_node;
22621 /* Pull out the template from an injected-class-name (or multiple). */
22622 if (is_template)
22623 decl = maybe_get_template_decl_from_type_decl (decl);
22625 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
22626 if (TREE_CODE (decl) == TREE_LIST)
22628 if (ambiguous_decls)
22629 *ambiguous_decls = decl;
22630 /* The error message we have to print is too complicated for
22631 cp_parser_error, so we incorporate its actions directly. */
22632 if (!cp_parser_simulate_error (parser))
22634 error_at (name_location, "reference to %qD is ambiguous",
22635 name);
22636 print_candidates (decl);
22638 return error_mark_node;
22641 gcc_assert (DECL_P (decl)
22642 || TREE_CODE (decl) == OVERLOAD
22643 || TREE_CODE (decl) == SCOPE_REF
22644 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
22645 || BASELINK_P (decl));
22647 /* If we have resolved the name of a member declaration, check to
22648 see if the declaration is accessible. When the name resolves to
22649 set of overloaded functions, accessibility is checked when
22650 overload resolution is done.
22652 During an explicit instantiation, access is not checked at all,
22653 as per [temp.explicit]. */
22654 if (DECL_P (decl))
22655 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
22657 maybe_record_typedef_use (decl);
22659 return decl;
22662 /* Like cp_parser_lookup_name, but for use in the typical case where
22663 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
22664 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
22666 static tree
22667 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
22669 return cp_parser_lookup_name (parser, name,
22670 none_type,
22671 /*is_template=*/false,
22672 /*is_namespace=*/false,
22673 /*check_dependency=*/true,
22674 /*ambiguous_decls=*/NULL,
22675 location);
22678 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
22679 the current context, return the TYPE_DECL. If TAG_NAME_P is
22680 true, the DECL indicates the class being defined in a class-head,
22681 or declared in an elaborated-type-specifier.
22683 Otherwise, return DECL. */
22685 static tree
22686 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
22688 /* If the TEMPLATE_DECL is being declared as part of a class-head,
22689 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
22691 struct A {
22692 template <typename T> struct B;
22695 template <typename T> struct A::B {};
22697 Similarly, in an elaborated-type-specifier:
22699 namespace N { struct X{}; }
22701 struct A {
22702 template <typename T> friend struct N::X;
22705 However, if the DECL refers to a class type, and we are in
22706 the scope of the class, then the name lookup automatically
22707 finds the TYPE_DECL created by build_self_reference rather
22708 than a TEMPLATE_DECL. For example, in:
22710 template <class T> struct S {
22711 S s;
22714 there is no need to handle such case. */
22716 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
22717 return DECL_TEMPLATE_RESULT (decl);
22719 return decl;
22722 /* If too many, or too few, template-parameter lists apply to the
22723 declarator, issue an error message. Returns TRUE if all went well,
22724 and FALSE otherwise. */
22726 static bool
22727 cp_parser_check_declarator_template_parameters (cp_parser* parser,
22728 cp_declarator *declarator,
22729 location_t declarator_location)
22731 switch (declarator->kind)
22733 case cdk_id:
22735 unsigned num_templates = 0;
22736 tree scope = declarator->u.id.qualifying_scope;
22738 if (scope)
22739 num_templates = num_template_headers_for_class (scope);
22740 else if (TREE_CODE (declarator->u.id.unqualified_name)
22741 == TEMPLATE_ID_EXPR)
22742 /* If the DECLARATOR has the form `X<y>' then it uses one
22743 additional level of template parameters. */
22744 ++num_templates;
22746 return cp_parser_check_template_parameters
22747 (parser, num_templates, declarator_location, declarator);
22750 case cdk_function:
22751 case cdk_array:
22752 case cdk_pointer:
22753 case cdk_reference:
22754 case cdk_ptrmem:
22755 return (cp_parser_check_declarator_template_parameters
22756 (parser, declarator->declarator, declarator_location));
22758 case cdk_error:
22759 return true;
22761 default:
22762 gcc_unreachable ();
22764 return false;
22767 /* NUM_TEMPLATES were used in the current declaration. If that is
22768 invalid, return FALSE and issue an error messages. Otherwise,
22769 return TRUE. If DECLARATOR is non-NULL, then we are checking a
22770 declarator and we can print more accurate diagnostics. */
22772 static bool
22773 cp_parser_check_template_parameters (cp_parser* parser,
22774 unsigned num_templates,
22775 location_t location,
22776 cp_declarator *declarator)
22778 /* If there are the same number of template classes and parameter
22779 lists, that's OK. */
22780 if (parser->num_template_parameter_lists == num_templates)
22781 return true;
22782 /* If there are more, but only one more, then we are referring to a
22783 member template. That's OK too. */
22784 if (parser->num_template_parameter_lists == num_templates + 1)
22785 return true;
22786 /* If there are more template classes than parameter lists, we have
22787 something like:
22789 template <class T> void S<T>::R<T>::f (); */
22790 if (parser->num_template_parameter_lists < num_templates)
22792 if (declarator && !current_function_decl)
22793 error_at (location, "specializing member %<%T::%E%> "
22794 "requires %<template<>%> syntax",
22795 declarator->u.id.qualifying_scope,
22796 declarator->u.id.unqualified_name);
22797 else if (declarator)
22798 error_at (location, "invalid declaration of %<%T::%E%>",
22799 declarator->u.id.qualifying_scope,
22800 declarator->u.id.unqualified_name);
22801 else
22802 error_at (location, "too few template-parameter-lists");
22803 return false;
22805 /* Otherwise, there are too many template parameter lists. We have
22806 something like:
22808 template <class T> template <class U> void S::f(); */
22809 error_at (location, "too many template-parameter-lists");
22810 return false;
22813 /* Parse an optional `::' token indicating that the following name is
22814 from the global namespace. If so, PARSER->SCOPE is set to the
22815 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
22816 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
22817 Returns the new value of PARSER->SCOPE, if the `::' token is
22818 present, and NULL_TREE otherwise. */
22820 static tree
22821 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
22823 cp_token *token;
22825 /* Peek at the next token. */
22826 token = cp_lexer_peek_token (parser->lexer);
22827 /* If we're looking at a `::' token then we're starting from the
22828 global namespace, not our current location. */
22829 if (token->type == CPP_SCOPE)
22831 /* Consume the `::' token. */
22832 cp_lexer_consume_token (parser->lexer);
22833 /* Set the SCOPE so that we know where to start the lookup. */
22834 parser->scope = global_namespace;
22835 parser->qualifying_scope = global_namespace;
22836 parser->object_scope = NULL_TREE;
22838 return parser->scope;
22840 else if (!current_scope_valid_p)
22842 parser->scope = NULL_TREE;
22843 parser->qualifying_scope = NULL_TREE;
22844 parser->object_scope = NULL_TREE;
22847 return NULL_TREE;
22850 /* Returns TRUE if the upcoming token sequence is the start of a
22851 constructor declarator. If FRIEND_P is true, the declarator is
22852 preceded by the `friend' specifier. */
22854 static bool
22855 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
22857 bool constructor_p;
22858 bool outside_class_specifier_p;
22859 tree nested_name_specifier;
22860 cp_token *next_token;
22862 /* The common case is that this is not a constructor declarator, so
22863 try to avoid doing lots of work if at all possible. It's not
22864 valid declare a constructor at function scope. */
22865 if (parser->in_function_body)
22866 return false;
22867 /* And only certain tokens can begin a constructor declarator. */
22868 next_token = cp_lexer_peek_token (parser->lexer);
22869 if (next_token->type != CPP_NAME
22870 && next_token->type != CPP_SCOPE
22871 && next_token->type != CPP_NESTED_NAME_SPECIFIER
22872 && next_token->type != CPP_TEMPLATE_ID)
22873 return false;
22875 /* Parse tentatively; we are going to roll back all of the tokens
22876 consumed here. */
22877 cp_parser_parse_tentatively (parser);
22878 /* Assume that we are looking at a constructor declarator. */
22879 constructor_p = true;
22881 /* Look for the optional `::' operator. */
22882 cp_parser_global_scope_opt (parser,
22883 /*current_scope_valid_p=*/false);
22884 /* Look for the nested-name-specifier. */
22885 nested_name_specifier
22886 = (cp_parser_nested_name_specifier_opt (parser,
22887 /*typename_keyword_p=*/false,
22888 /*check_dependency_p=*/false,
22889 /*type_p=*/false,
22890 /*is_declaration=*/false));
22892 outside_class_specifier_p = (!at_class_scope_p ()
22893 || !TYPE_BEING_DEFINED (current_class_type)
22894 || friend_p);
22896 /* Outside of a class-specifier, there must be a
22897 nested-name-specifier. */
22898 if (!nested_name_specifier && outside_class_specifier_p)
22899 constructor_p = false;
22900 else if (nested_name_specifier == error_mark_node)
22901 constructor_p = false;
22903 /* If we have a class scope, this is easy; DR 147 says that S::S always
22904 names the constructor, and no other qualified name could. */
22905 if (constructor_p && nested_name_specifier
22906 && CLASS_TYPE_P (nested_name_specifier))
22908 tree id = cp_parser_unqualified_id (parser,
22909 /*template_keyword_p=*/false,
22910 /*check_dependency_p=*/false,
22911 /*declarator_p=*/true,
22912 /*optional_p=*/false);
22913 if (is_overloaded_fn (id))
22914 id = DECL_NAME (get_first_fn (id));
22915 if (!constructor_name_p (id, nested_name_specifier))
22916 constructor_p = false;
22918 /* If we still think that this might be a constructor-declarator,
22919 look for a class-name. */
22920 else if (constructor_p)
22922 /* If we have:
22924 template <typename T> struct S {
22925 S();
22928 we must recognize that the nested `S' names a class. */
22929 tree type_decl;
22930 type_decl = cp_parser_class_name (parser,
22931 /*typename_keyword_p=*/false,
22932 /*template_keyword_p=*/false,
22933 none_type,
22934 /*check_dependency_p=*/false,
22935 /*class_head_p=*/false,
22936 /*is_declaration=*/false);
22937 /* If there was no class-name, then this is not a constructor.
22938 Otherwise, if we are in a class-specifier and we aren't
22939 handling a friend declaration, check that its type matches
22940 current_class_type (c++/38313). Note: error_mark_node
22941 is left alone for error recovery purposes. */
22942 constructor_p = (!cp_parser_error_occurred (parser)
22943 && (outside_class_specifier_p
22944 || type_decl == error_mark_node
22945 || same_type_p (current_class_type,
22946 TREE_TYPE (type_decl))));
22948 /* If we're still considering a constructor, we have to see a `(',
22949 to begin the parameter-declaration-clause, followed by either a
22950 `)', an `...', or a decl-specifier. We need to check for a
22951 type-specifier to avoid being fooled into thinking that:
22953 S (f) (int);
22955 is a constructor. (It is actually a function named `f' that
22956 takes one parameter (of type `int') and returns a value of type
22957 `S'. */
22958 if (constructor_p
22959 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22960 constructor_p = false;
22962 if (constructor_p
22963 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
22964 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
22965 /* A parameter declaration begins with a decl-specifier,
22966 which is either the "attribute" keyword, a storage class
22967 specifier, or (usually) a type-specifier. */
22968 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
22970 tree type;
22971 tree pushed_scope = NULL_TREE;
22972 unsigned saved_num_template_parameter_lists;
22974 /* Names appearing in the type-specifier should be looked up
22975 in the scope of the class. */
22976 if (current_class_type)
22977 type = NULL_TREE;
22978 else
22980 type = TREE_TYPE (type_decl);
22981 if (TREE_CODE (type) == TYPENAME_TYPE)
22983 type = resolve_typename_type (type,
22984 /*only_current_p=*/false);
22985 if (TREE_CODE (type) == TYPENAME_TYPE)
22987 cp_parser_abort_tentative_parse (parser);
22988 return false;
22991 pushed_scope = push_scope (type);
22994 /* Inside the constructor parameter list, surrounding
22995 template-parameter-lists do not apply. */
22996 saved_num_template_parameter_lists
22997 = parser->num_template_parameter_lists;
22998 parser->num_template_parameter_lists = 0;
23000 /* Look for the type-specifier. */
23001 cp_parser_type_specifier (parser,
23002 CP_PARSER_FLAGS_NONE,
23003 /*decl_specs=*/NULL,
23004 /*is_declarator=*/true,
23005 /*declares_class_or_enum=*/NULL,
23006 /*is_cv_qualifier=*/NULL);
23008 parser->num_template_parameter_lists
23009 = saved_num_template_parameter_lists;
23011 /* Leave the scope of the class. */
23012 if (pushed_scope)
23013 pop_scope (pushed_scope);
23015 constructor_p = !cp_parser_error_occurred (parser);
23019 /* We did not really want to consume any tokens. */
23020 cp_parser_abort_tentative_parse (parser);
23022 return constructor_p;
23025 /* Parse the definition of the function given by the DECL_SPECIFIERS,
23026 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
23027 they must be performed once we are in the scope of the function.
23029 Returns the function defined. */
23031 static tree
23032 cp_parser_function_definition_from_specifiers_and_declarator
23033 (cp_parser* parser,
23034 cp_decl_specifier_seq *decl_specifiers,
23035 tree attributes,
23036 const cp_declarator *declarator)
23038 tree fn;
23039 bool success_p;
23041 /* Begin the function-definition. */
23042 success_p = start_function (decl_specifiers, declarator, attributes);
23044 /* The things we're about to see are not directly qualified by any
23045 template headers we've seen thus far. */
23046 reset_specialization ();
23048 /* If there were names looked up in the decl-specifier-seq that we
23049 did not check, check them now. We must wait until we are in the
23050 scope of the function to perform the checks, since the function
23051 might be a friend. */
23052 perform_deferred_access_checks (tf_warning_or_error);
23054 if (success_p)
23056 cp_finalize_omp_declare_simd (parser, current_function_decl);
23057 parser->omp_declare_simd = NULL;
23060 if (!success_p)
23062 /* Skip the entire function. */
23063 cp_parser_skip_to_end_of_block_or_statement (parser);
23064 fn = error_mark_node;
23066 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
23068 /* Seen already, skip it. An error message has already been output. */
23069 cp_parser_skip_to_end_of_block_or_statement (parser);
23070 fn = current_function_decl;
23071 current_function_decl = NULL_TREE;
23072 /* If this is a function from a class, pop the nested class. */
23073 if (current_class_name)
23074 pop_nested_class ();
23076 else
23078 timevar_id_t tv;
23079 if (DECL_DECLARED_INLINE_P (current_function_decl))
23080 tv = TV_PARSE_INLINE;
23081 else
23082 tv = TV_PARSE_FUNC;
23083 timevar_push (tv);
23084 fn = cp_parser_function_definition_after_declarator (parser,
23085 /*inline_p=*/false);
23086 timevar_pop (tv);
23089 return fn;
23092 /* Parse the part of a function-definition that follows the
23093 declarator. INLINE_P is TRUE iff this function is an inline
23094 function defined within a class-specifier.
23096 Returns the function defined. */
23098 static tree
23099 cp_parser_function_definition_after_declarator (cp_parser* parser,
23100 bool inline_p)
23102 tree fn;
23103 bool ctor_initializer_p = false;
23104 bool saved_in_unbraced_linkage_specification_p;
23105 bool saved_in_function_body;
23106 unsigned saved_num_template_parameter_lists;
23107 cp_token *token;
23108 bool fully_implicit_function_template_p
23109 = parser->fully_implicit_function_template_p;
23110 parser->fully_implicit_function_template_p = false;
23111 tree implicit_template_parms
23112 = parser->implicit_template_parms;
23113 parser->implicit_template_parms = 0;
23114 cp_binding_level* implicit_template_scope
23115 = parser->implicit_template_scope;
23116 parser->implicit_template_scope = 0;
23118 saved_in_function_body = parser->in_function_body;
23119 parser->in_function_body = true;
23120 /* If the next token is `return', then the code may be trying to
23121 make use of the "named return value" extension that G++ used to
23122 support. */
23123 token = cp_lexer_peek_token (parser->lexer);
23124 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
23126 /* Consume the `return' keyword. */
23127 cp_lexer_consume_token (parser->lexer);
23128 /* Look for the identifier that indicates what value is to be
23129 returned. */
23130 cp_parser_identifier (parser);
23131 /* Issue an error message. */
23132 error_at (token->location,
23133 "named return values are no longer supported");
23134 /* Skip tokens until we reach the start of the function body. */
23135 while (true)
23137 cp_token *token = cp_lexer_peek_token (parser->lexer);
23138 if (token->type == CPP_OPEN_BRACE
23139 || token->type == CPP_EOF
23140 || token->type == CPP_PRAGMA_EOL)
23141 break;
23142 cp_lexer_consume_token (parser->lexer);
23145 /* The `extern' in `extern "C" void f () { ... }' does not apply to
23146 anything declared inside `f'. */
23147 saved_in_unbraced_linkage_specification_p
23148 = parser->in_unbraced_linkage_specification_p;
23149 parser->in_unbraced_linkage_specification_p = false;
23150 /* Inside the function, surrounding template-parameter-lists do not
23151 apply. */
23152 saved_num_template_parameter_lists
23153 = parser->num_template_parameter_lists;
23154 parser->num_template_parameter_lists = 0;
23156 start_lambda_scope (current_function_decl);
23158 /* If the next token is `try', `__transaction_atomic', or
23159 `__transaction_relaxed`, then we are looking at either function-try-block
23160 or function-transaction-block. Note that all of these include the
23161 function-body. */
23162 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
23163 ctor_initializer_p = cp_parser_function_transaction (parser,
23164 RID_TRANSACTION_ATOMIC);
23165 else if (cp_lexer_next_token_is_keyword (parser->lexer,
23166 RID_TRANSACTION_RELAXED))
23167 ctor_initializer_p = cp_parser_function_transaction (parser,
23168 RID_TRANSACTION_RELAXED);
23169 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23170 ctor_initializer_p = cp_parser_function_try_block (parser);
23171 else
23172 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23173 (parser, /*in_function_try_block=*/false);
23175 finish_lambda_scope ();
23177 /* Finish the function. */
23178 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
23179 (inline_p ? 2 : 0));
23180 /* Generate code for it, if necessary. */
23181 expand_or_defer_fn (fn);
23182 /* Restore the saved values. */
23183 parser->in_unbraced_linkage_specification_p
23184 = saved_in_unbraced_linkage_specification_p;
23185 parser->num_template_parameter_lists
23186 = saved_num_template_parameter_lists;
23187 parser->in_function_body = saved_in_function_body;
23189 parser->fully_implicit_function_template_p
23190 = fully_implicit_function_template_p;
23191 parser->implicit_template_parms
23192 = implicit_template_parms;
23193 parser->implicit_template_scope
23194 = implicit_template_scope;
23196 if (parser->fully_implicit_function_template_p)
23197 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
23199 return fn;
23202 /* Parse a template-declaration, assuming that the `export' (and
23203 `extern') keywords, if present, has already been scanned. MEMBER_P
23204 is as for cp_parser_template_declaration. */
23206 static void
23207 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
23209 tree decl = NULL_TREE;
23210 vec<deferred_access_check, va_gc> *checks;
23211 tree parameter_list;
23212 bool friend_p = false;
23213 bool need_lang_pop;
23214 cp_token *token;
23216 /* Look for the `template' keyword. */
23217 token = cp_lexer_peek_token (parser->lexer);
23218 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
23219 return;
23221 /* And the `<'. */
23222 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
23223 return;
23224 if (at_class_scope_p () && current_function_decl)
23226 /* 14.5.2.2 [temp.mem]
23228 A local class shall not have member templates. */
23229 error_at (token->location,
23230 "invalid declaration of member template in local class");
23231 cp_parser_skip_to_end_of_block_or_statement (parser);
23232 return;
23234 /* [temp]
23236 A template ... shall not have C linkage. */
23237 if (current_lang_name == lang_name_c)
23239 error_at (token->location, "template with C linkage");
23240 /* Give it C++ linkage to avoid confusing other parts of the
23241 front end. */
23242 push_lang_context (lang_name_cplusplus);
23243 need_lang_pop = true;
23245 else
23246 need_lang_pop = false;
23248 /* We cannot perform access checks on the template parameter
23249 declarations until we know what is being declared, just as we
23250 cannot check the decl-specifier list. */
23251 push_deferring_access_checks (dk_deferred);
23253 /* If the next token is `>', then we have an invalid
23254 specialization. Rather than complain about an invalid template
23255 parameter, issue an error message here. */
23256 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
23258 cp_parser_error (parser, "invalid explicit specialization");
23259 begin_specialization ();
23260 parameter_list = NULL_TREE;
23262 else
23264 /* Parse the template parameters. */
23265 parameter_list = cp_parser_template_parameter_list (parser);
23268 /* Get the deferred access checks from the parameter list. These
23269 will be checked once we know what is being declared, as for a
23270 member template the checks must be performed in the scope of the
23271 class containing the member. */
23272 checks = get_deferred_access_checks ();
23274 /* Look for the `>'. */
23275 cp_parser_skip_to_end_of_template_parameter_list (parser);
23276 /* We just processed one more parameter list. */
23277 ++parser->num_template_parameter_lists;
23278 /* If the next token is `template', there are more template
23279 parameters. */
23280 if (cp_lexer_next_token_is_keyword (parser->lexer,
23281 RID_TEMPLATE))
23282 cp_parser_template_declaration_after_export (parser, member_p);
23283 else if (cxx_dialect >= cxx11
23284 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23285 decl = cp_parser_alias_declaration (parser);
23286 else
23288 /* There are no access checks when parsing a template, as we do not
23289 know if a specialization will be a friend. */
23290 push_deferring_access_checks (dk_no_check);
23291 token = cp_lexer_peek_token (parser->lexer);
23292 decl = cp_parser_single_declaration (parser,
23293 checks,
23294 member_p,
23295 /*explicit_specialization_p=*/false,
23296 &friend_p);
23297 pop_deferring_access_checks ();
23299 /* If this is a member template declaration, let the front
23300 end know. */
23301 if (member_p && !friend_p && decl)
23303 if (TREE_CODE (decl) == TYPE_DECL)
23304 cp_parser_check_access_in_redeclaration (decl, token->location);
23306 decl = finish_member_template_decl (decl);
23308 else if (friend_p && decl
23309 && DECL_DECLARES_TYPE_P (decl))
23310 make_friend_class (current_class_type, TREE_TYPE (decl),
23311 /*complain=*/true);
23313 /* We are done with the current parameter list. */
23314 --parser->num_template_parameter_lists;
23316 pop_deferring_access_checks ();
23318 /* Finish up. */
23319 finish_template_decl (parameter_list);
23321 /* Check the template arguments for a literal operator template. */
23322 if (decl
23323 && DECL_DECLARES_FUNCTION_P (decl)
23324 && UDLIT_OPER_P (DECL_NAME (decl)))
23326 bool ok = true;
23327 if (parameter_list == NULL_TREE)
23328 ok = false;
23329 else
23331 int num_parms = TREE_VEC_LENGTH (parameter_list);
23332 if (num_parms == 1)
23334 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
23335 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23336 if (TREE_TYPE (parm) != char_type_node
23337 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23338 ok = false;
23340 else if (num_parms == 2 && cxx_dialect >= cxx14)
23342 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
23343 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
23344 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
23345 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
23346 if (TREE_TYPE (parm) != TREE_TYPE (type)
23347 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
23348 ok = false;
23350 else
23351 ok = false;
23353 if (!ok)
23355 if (cxx_dialect >= cxx14)
23356 error ("literal operator template %qD has invalid parameter list."
23357 " Expected non-type template argument pack <char...>"
23358 " or <typename CharT, CharT...>",
23359 decl);
23360 else
23361 error ("literal operator template %qD has invalid parameter list."
23362 " Expected non-type template argument pack <char...>",
23363 decl);
23366 /* Register member declarations. */
23367 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
23368 finish_member_declaration (decl);
23369 /* For the erroneous case of a template with C linkage, we pushed an
23370 implicit C++ linkage scope; exit that scope now. */
23371 if (need_lang_pop)
23372 pop_lang_context ();
23373 /* If DECL is a function template, we must return to parse it later.
23374 (Even though there is no definition, there might be default
23375 arguments that need handling.) */
23376 if (member_p && decl
23377 && DECL_DECLARES_FUNCTION_P (decl))
23378 vec_safe_push (unparsed_funs_with_definitions, decl);
23381 /* Perform the deferred access checks from a template-parameter-list.
23382 CHECKS is a TREE_LIST of access checks, as returned by
23383 get_deferred_access_checks. */
23385 static void
23386 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
23388 ++processing_template_parmlist;
23389 perform_access_checks (checks, tf_warning_or_error);
23390 --processing_template_parmlist;
23393 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
23394 `function-definition' sequence that follows a template header.
23395 If MEMBER_P is true, this declaration appears in a class scope.
23397 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
23398 *FRIEND_P is set to TRUE iff the declaration is a friend. */
23400 static tree
23401 cp_parser_single_declaration (cp_parser* parser,
23402 vec<deferred_access_check, va_gc> *checks,
23403 bool member_p,
23404 bool explicit_specialization_p,
23405 bool* friend_p)
23407 int declares_class_or_enum;
23408 tree decl = NULL_TREE;
23409 cp_decl_specifier_seq decl_specifiers;
23410 bool function_definition_p = false;
23411 cp_token *decl_spec_token_start;
23413 /* This function is only used when processing a template
23414 declaration. */
23415 gcc_assert (innermost_scope_kind () == sk_template_parms
23416 || innermost_scope_kind () == sk_template_spec);
23418 /* Defer access checks until we know what is being declared. */
23419 push_deferring_access_checks (dk_deferred);
23421 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
23422 alternative. */
23423 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23424 cp_parser_decl_specifier_seq (parser,
23425 CP_PARSER_FLAGS_OPTIONAL,
23426 &decl_specifiers,
23427 &declares_class_or_enum);
23428 if (friend_p)
23429 *friend_p = cp_parser_friend_p (&decl_specifiers);
23431 /* There are no template typedefs. */
23432 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
23434 error_at (decl_spec_token_start->location,
23435 "template declaration of %<typedef%>");
23436 decl = error_mark_node;
23439 /* Gather up the access checks that occurred the
23440 decl-specifier-seq. */
23441 stop_deferring_access_checks ();
23443 /* Check for the declaration of a template class. */
23444 if (declares_class_or_enum)
23446 if (cp_parser_declares_only_class_p (parser))
23448 decl = shadow_tag (&decl_specifiers);
23450 /* In this case:
23452 struct C {
23453 friend template <typename T> struct A<T>::B;
23456 A<T>::B will be represented by a TYPENAME_TYPE, and
23457 therefore not recognized by shadow_tag. */
23458 if (friend_p && *friend_p
23459 && !decl
23460 && decl_specifiers.type
23461 && TYPE_P (decl_specifiers.type))
23462 decl = decl_specifiers.type;
23464 if (decl && decl != error_mark_node)
23465 decl = TYPE_NAME (decl);
23466 else
23467 decl = error_mark_node;
23469 /* Perform access checks for template parameters. */
23470 cp_parser_perform_template_parameter_access_checks (checks);
23474 /* Complain about missing 'typename' or other invalid type names. */
23475 if (!decl_specifiers.any_type_specifiers_p
23476 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23478 /* cp_parser_parse_and_diagnose_invalid_type_name calls
23479 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
23480 the rest of this declaration. */
23481 decl = error_mark_node;
23482 goto out;
23485 /* If it's not a template class, try for a template function. If
23486 the next token is a `;', then this declaration does not declare
23487 anything. But, if there were errors in the decl-specifiers, then
23488 the error might well have come from an attempted class-specifier.
23489 In that case, there's no need to warn about a missing declarator. */
23490 if (!decl
23491 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
23492 || decl_specifiers.type != error_mark_node))
23494 decl = cp_parser_init_declarator (parser,
23495 &decl_specifiers,
23496 checks,
23497 /*function_definition_allowed_p=*/true,
23498 member_p,
23499 declares_class_or_enum,
23500 &function_definition_p,
23501 NULL);
23503 /* 7.1.1-1 [dcl.stc]
23505 A storage-class-specifier shall not be specified in an explicit
23506 specialization... */
23507 if (decl
23508 && explicit_specialization_p
23509 && decl_specifiers.storage_class != sc_none)
23511 error_at (decl_spec_token_start->location,
23512 "explicit template specialization cannot have a storage class");
23513 decl = error_mark_node;
23516 if (decl && VAR_P (decl))
23517 check_template_variable (decl);
23520 /* Look for a trailing `;' after the declaration. */
23521 if (!function_definition_p
23522 && (decl == error_mark_node
23523 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
23524 cp_parser_skip_to_end_of_block_or_statement (parser);
23526 out:
23527 pop_deferring_access_checks ();
23529 /* Clear any current qualification; whatever comes next is the start
23530 of something new. */
23531 parser->scope = NULL_TREE;
23532 parser->qualifying_scope = NULL_TREE;
23533 parser->object_scope = NULL_TREE;
23535 return decl;
23538 /* Parse a cast-expression that is not the operand of a unary "&". */
23540 static tree
23541 cp_parser_simple_cast_expression (cp_parser *parser)
23543 return cp_parser_cast_expression (parser, /*address_p=*/false,
23544 /*cast_p=*/false, /*decltype*/false, NULL);
23547 /* Parse a functional cast to TYPE. Returns an expression
23548 representing the cast. */
23550 static tree
23551 cp_parser_functional_cast (cp_parser* parser, tree type)
23553 vec<tree, va_gc> *vec;
23554 tree expression_list;
23555 tree cast;
23556 bool nonconst_p;
23558 if (!type)
23559 type = error_mark_node;
23561 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23563 cp_lexer_set_source_position (parser->lexer);
23564 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23565 expression_list = cp_parser_braced_list (parser, &nonconst_p);
23566 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
23567 if (TREE_CODE (type) == TYPE_DECL)
23568 type = TREE_TYPE (type);
23569 return finish_compound_literal (type, expression_list,
23570 tf_warning_or_error);
23574 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
23575 /*cast_p=*/true,
23576 /*allow_expansion_p=*/true,
23577 /*non_constant_p=*/NULL);
23578 if (vec == NULL)
23579 expression_list = error_mark_node;
23580 else
23582 expression_list = build_tree_list_vec (vec);
23583 release_tree_vector (vec);
23586 cast = build_functional_cast (type, expression_list,
23587 tf_warning_or_error);
23588 /* [expr.const]/1: In an integral constant expression "only type
23589 conversions to integral or enumeration type can be used". */
23590 if (TREE_CODE (type) == TYPE_DECL)
23591 type = TREE_TYPE (type);
23592 if (cast != error_mark_node
23593 && !cast_valid_in_integral_constant_expression_p (type)
23594 && cp_parser_non_integral_constant_expression (parser,
23595 NIC_CONSTRUCTOR))
23596 return error_mark_node;
23597 return cast;
23600 /* Save the tokens that make up the body of a member function defined
23601 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
23602 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
23603 specifiers applied to the declaration. Returns the FUNCTION_DECL
23604 for the member function. */
23606 static tree
23607 cp_parser_save_member_function_body (cp_parser* parser,
23608 cp_decl_specifier_seq *decl_specifiers,
23609 cp_declarator *declarator,
23610 tree attributes)
23612 cp_token *first;
23613 cp_token *last;
23614 tree fn;
23616 /* Create the FUNCTION_DECL. */
23617 fn = grokmethod (decl_specifiers, declarator, attributes);
23618 cp_finalize_omp_declare_simd (parser, fn);
23619 /* If something went badly wrong, bail out now. */
23620 if (fn == error_mark_node)
23622 /* If there's a function-body, skip it. */
23623 if (cp_parser_token_starts_function_definition_p
23624 (cp_lexer_peek_token (parser->lexer)))
23625 cp_parser_skip_to_end_of_block_or_statement (parser);
23626 return error_mark_node;
23629 /* Remember it, if there default args to post process. */
23630 cp_parser_save_default_args (parser, fn);
23632 /* Save away the tokens that make up the body of the
23633 function. */
23634 first = parser->lexer->next_token;
23635 /* Handle function try blocks. */
23636 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
23637 cp_lexer_consume_token (parser->lexer);
23638 /* We can have braced-init-list mem-initializers before the fn body. */
23639 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23641 cp_lexer_consume_token (parser->lexer);
23642 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
23644 /* cache_group will stop after an un-nested { } pair, too. */
23645 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
23646 break;
23648 /* variadic mem-inits have ... after the ')'. */
23649 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23650 cp_lexer_consume_token (parser->lexer);
23653 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23654 /* Handle function try blocks. */
23655 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
23656 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
23657 last = parser->lexer->next_token;
23659 /* Save away the inline definition; we will process it when the
23660 class is complete. */
23661 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
23662 DECL_PENDING_INLINE_P (fn) = 1;
23664 /* We need to know that this was defined in the class, so that
23665 friend templates are handled correctly. */
23666 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
23668 /* Add FN to the queue of functions to be parsed later. */
23669 vec_safe_push (unparsed_funs_with_definitions, fn);
23671 return fn;
23674 /* Save the tokens that make up the in-class initializer for a non-static
23675 data member. Returns a DEFAULT_ARG. */
23677 static tree
23678 cp_parser_save_nsdmi (cp_parser* parser)
23680 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
23683 /* Parse a template-argument-list, as well as the trailing ">" (but
23684 not the opening "<"). See cp_parser_template_argument_list for the
23685 return value. */
23687 static tree
23688 cp_parser_enclosed_template_argument_list (cp_parser* parser)
23690 tree arguments;
23691 tree saved_scope;
23692 tree saved_qualifying_scope;
23693 tree saved_object_scope;
23694 bool saved_greater_than_is_operator_p;
23695 int saved_unevaluated_operand;
23696 int saved_inhibit_evaluation_warnings;
23698 /* [temp.names]
23700 When parsing a template-id, the first non-nested `>' is taken as
23701 the end of the template-argument-list rather than a greater-than
23702 operator. */
23703 saved_greater_than_is_operator_p
23704 = parser->greater_than_is_operator_p;
23705 parser->greater_than_is_operator_p = false;
23706 /* Parsing the argument list may modify SCOPE, so we save it
23707 here. */
23708 saved_scope = parser->scope;
23709 saved_qualifying_scope = parser->qualifying_scope;
23710 saved_object_scope = parser->object_scope;
23711 /* We need to evaluate the template arguments, even though this
23712 template-id may be nested within a "sizeof". */
23713 saved_unevaluated_operand = cp_unevaluated_operand;
23714 cp_unevaluated_operand = 0;
23715 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23716 c_inhibit_evaluation_warnings = 0;
23717 /* Parse the template-argument-list itself. */
23718 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
23719 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23720 arguments = NULL_TREE;
23721 else
23722 arguments = cp_parser_template_argument_list (parser);
23723 /* Look for the `>' that ends the template-argument-list. If we find
23724 a '>>' instead, it's probably just a typo. */
23725 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
23727 if (cxx_dialect != cxx98)
23729 /* In C++0x, a `>>' in a template argument list or cast
23730 expression is considered to be two separate `>'
23731 tokens. So, change the current token to a `>', but don't
23732 consume it: it will be consumed later when the outer
23733 template argument list (or cast expression) is parsed.
23734 Note that this replacement of `>' for `>>' is necessary
23735 even if we are parsing tentatively: in the tentative
23736 case, after calling
23737 cp_parser_enclosed_template_argument_list we will always
23738 throw away all of the template arguments and the first
23739 closing `>', either because the template argument list
23740 was erroneous or because we are replacing those tokens
23741 with a CPP_TEMPLATE_ID token. The second `>' (which will
23742 not have been thrown away) is needed either to close an
23743 outer template argument list or to complete a new-style
23744 cast. */
23745 cp_token *token = cp_lexer_peek_token (parser->lexer);
23746 token->type = CPP_GREATER;
23748 else if (!saved_greater_than_is_operator_p)
23750 /* If we're in a nested template argument list, the '>>' has
23751 to be a typo for '> >'. We emit the error message, but we
23752 continue parsing and we push a '>' as next token, so that
23753 the argument list will be parsed correctly. Note that the
23754 global source location is still on the token before the
23755 '>>', so we need to say explicitly where we want it. */
23756 cp_token *token = cp_lexer_peek_token (parser->lexer);
23757 error_at (token->location, "%<>>%> should be %<> >%> "
23758 "within a nested template argument list");
23760 token->type = CPP_GREATER;
23762 else
23764 /* If this is not a nested template argument list, the '>>'
23765 is a typo for '>'. Emit an error message and continue.
23766 Same deal about the token location, but here we can get it
23767 right by consuming the '>>' before issuing the diagnostic. */
23768 cp_token *token = cp_lexer_consume_token (parser->lexer);
23769 error_at (token->location,
23770 "spurious %<>>%>, use %<>%> to terminate "
23771 "a template argument list");
23774 else
23775 cp_parser_skip_to_end_of_template_parameter_list (parser);
23776 /* The `>' token might be a greater-than operator again now. */
23777 parser->greater_than_is_operator_p
23778 = saved_greater_than_is_operator_p;
23779 /* Restore the SAVED_SCOPE. */
23780 parser->scope = saved_scope;
23781 parser->qualifying_scope = saved_qualifying_scope;
23782 parser->object_scope = saved_object_scope;
23783 cp_unevaluated_operand = saved_unevaluated_operand;
23784 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23786 return arguments;
23789 /* MEMBER_FUNCTION is a member function, or a friend. If default
23790 arguments, or the body of the function have not yet been parsed,
23791 parse them now. */
23793 static void
23794 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
23796 timevar_push (TV_PARSE_INMETH);
23797 /* If this member is a template, get the underlying
23798 FUNCTION_DECL. */
23799 if (DECL_FUNCTION_TEMPLATE_P (member_function))
23800 member_function = DECL_TEMPLATE_RESULT (member_function);
23802 /* There should not be any class definitions in progress at this
23803 point; the bodies of members are only parsed outside of all class
23804 definitions. */
23805 gcc_assert (parser->num_classes_being_defined == 0);
23806 /* While we're parsing the member functions we might encounter more
23807 classes. We want to handle them right away, but we don't want
23808 them getting mixed up with functions that are currently in the
23809 queue. */
23810 push_unparsed_function_queues (parser);
23812 /* Make sure that any template parameters are in scope. */
23813 maybe_begin_member_template_processing (member_function);
23815 /* If the body of the function has not yet been parsed, parse it
23816 now. */
23817 if (DECL_PENDING_INLINE_P (member_function))
23819 tree function_scope;
23820 cp_token_cache *tokens;
23822 /* The function is no longer pending; we are processing it. */
23823 tokens = DECL_PENDING_INLINE_INFO (member_function);
23824 DECL_PENDING_INLINE_INFO (member_function) = NULL;
23825 DECL_PENDING_INLINE_P (member_function) = 0;
23827 /* If this is a local class, enter the scope of the containing
23828 function. */
23829 function_scope = current_function_decl;
23830 if (function_scope)
23831 push_function_context ();
23833 /* Push the body of the function onto the lexer stack. */
23834 cp_parser_push_lexer_for_tokens (parser, tokens);
23836 /* Let the front end know that we going to be defining this
23837 function. */
23838 start_preparsed_function (member_function, NULL_TREE,
23839 SF_PRE_PARSED | SF_INCLASS_INLINE);
23841 /* Don't do access checking if it is a templated function. */
23842 if (processing_template_decl)
23843 push_deferring_access_checks (dk_no_check);
23845 /* #pragma omp declare reduction needs special parsing. */
23846 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
23848 parser->lexer->in_pragma = true;
23849 cp_parser_omp_declare_reduction_exprs (member_function, parser);
23850 finish_function (/*inline*/2);
23851 cp_check_omp_declare_reduction (member_function);
23853 else
23854 /* Now, parse the body of the function. */
23855 cp_parser_function_definition_after_declarator (parser,
23856 /*inline_p=*/true);
23858 if (processing_template_decl)
23859 pop_deferring_access_checks ();
23861 /* Leave the scope of the containing function. */
23862 if (function_scope)
23863 pop_function_context ();
23864 cp_parser_pop_lexer (parser);
23867 /* Remove any template parameters from the symbol table. */
23868 maybe_end_member_template_processing ();
23870 /* Restore the queue. */
23871 pop_unparsed_function_queues (parser);
23872 timevar_pop (TV_PARSE_INMETH);
23875 /* If DECL contains any default args, remember it on the unparsed
23876 functions queue. */
23878 static void
23879 cp_parser_save_default_args (cp_parser* parser, tree decl)
23881 tree probe;
23883 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
23884 probe;
23885 probe = TREE_CHAIN (probe))
23886 if (TREE_PURPOSE (probe))
23888 cp_default_arg_entry entry = {current_class_type, decl};
23889 vec_safe_push (unparsed_funs_with_default_args, entry);
23890 break;
23894 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
23895 which is either a FIELD_DECL or PARM_DECL. Parse it and return
23896 the result. For a PARM_DECL, PARMTYPE is the corresponding type
23897 from the parameter-type-list. */
23899 static tree
23900 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
23901 tree default_arg, tree parmtype)
23903 cp_token_cache *tokens;
23904 tree parsed_arg;
23905 bool dummy;
23907 if (default_arg == error_mark_node)
23908 return error_mark_node;
23910 /* Push the saved tokens for the default argument onto the parser's
23911 lexer stack. */
23912 tokens = DEFARG_TOKENS (default_arg);
23913 cp_parser_push_lexer_for_tokens (parser, tokens);
23915 start_lambda_scope (decl);
23917 /* Parse the default argument. */
23918 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
23919 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
23920 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
23922 finish_lambda_scope ();
23924 if (parsed_arg == error_mark_node)
23925 cp_parser_skip_to_end_of_statement (parser);
23927 if (!processing_template_decl)
23929 /* In a non-template class, check conversions now. In a template,
23930 we'll wait and instantiate these as needed. */
23931 if (TREE_CODE (decl) == PARM_DECL)
23932 parsed_arg = check_default_argument (parmtype, parsed_arg,
23933 tf_warning_or_error);
23934 else
23935 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
23938 /* If the token stream has not been completely used up, then
23939 there was extra junk after the end of the default
23940 argument. */
23941 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
23943 if (TREE_CODE (decl) == PARM_DECL)
23944 cp_parser_error (parser, "expected %<,%>");
23945 else
23946 cp_parser_error (parser, "expected %<;%>");
23949 /* Revert to the main lexer. */
23950 cp_parser_pop_lexer (parser);
23952 return parsed_arg;
23955 /* FIELD is a non-static data member with an initializer which we saved for
23956 later; parse it now. */
23958 static void
23959 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
23961 tree def;
23963 maybe_begin_member_template_processing (field);
23965 push_unparsed_function_queues (parser);
23966 def = cp_parser_late_parse_one_default_arg (parser, field,
23967 DECL_INITIAL (field),
23968 NULL_TREE);
23969 pop_unparsed_function_queues (parser);
23971 maybe_end_member_template_processing ();
23973 DECL_INITIAL (field) = def;
23976 /* FN is a FUNCTION_DECL which may contains a parameter with an
23977 unparsed DEFAULT_ARG. Parse the default args now. This function
23978 assumes that the current scope is the scope in which the default
23979 argument should be processed. */
23981 static void
23982 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
23984 bool saved_local_variables_forbidden_p;
23985 tree parm, parmdecl;
23987 /* While we're parsing the default args, we might (due to the
23988 statement expression extension) encounter more classes. We want
23989 to handle them right away, but we don't want them getting mixed
23990 up with default args that are currently in the queue. */
23991 push_unparsed_function_queues (parser);
23993 /* Local variable names (and the `this' keyword) may not appear
23994 in a default argument. */
23995 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
23996 parser->local_variables_forbidden_p = true;
23998 push_defarg_context (fn);
24000 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
24001 parmdecl = DECL_ARGUMENTS (fn);
24002 parm && parm != void_list_node;
24003 parm = TREE_CHAIN (parm),
24004 parmdecl = DECL_CHAIN (parmdecl))
24006 tree default_arg = TREE_PURPOSE (parm);
24007 tree parsed_arg;
24008 vec<tree, va_gc> *insts;
24009 tree copy;
24010 unsigned ix;
24012 if (!default_arg)
24013 continue;
24015 if (TREE_CODE (default_arg) != DEFAULT_ARG)
24016 /* This can happen for a friend declaration for a function
24017 already declared with default arguments. */
24018 continue;
24020 parsed_arg
24021 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
24022 default_arg,
24023 TREE_VALUE (parm));
24024 if (parsed_arg == error_mark_node)
24026 continue;
24029 TREE_PURPOSE (parm) = parsed_arg;
24031 /* Update any instantiations we've already created. */
24032 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
24033 vec_safe_iterate (insts, ix, &copy); ix++)
24034 TREE_PURPOSE (copy) = parsed_arg;
24037 pop_defarg_context ();
24039 /* Make sure no default arg is missing. */
24040 check_default_args (fn);
24042 /* Restore the state of local_variables_forbidden_p. */
24043 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
24045 /* Restore the queue. */
24046 pop_unparsed_function_queues (parser);
24049 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
24051 sizeof ... ( identifier )
24053 where the 'sizeof' token has already been consumed. */
24055 static tree
24056 cp_parser_sizeof_pack (cp_parser *parser)
24058 /* Consume the `...'. */
24059 cp_lexer_consume_token (parser->lexer);
24060 maybe_warn_variadic_templates ();
24062 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
24063 if (paren)
24064 cp_lexer_consume_token (parser->lexer);
24065 else
24066 permerror (cp_lexer_peek_token (parser->lexer)->location,
24067 "%<sizeof...%> argument must be surrounded by parentheses");
24069 cp_token *token = cp_lexer_peek_token (parser->lexer);
24070 tree name = cp_parser_identifier (parser);
24071 if (name == error_mark_node)
24072 return error_mark_node;
24073 /* The name is not qualified. */
24074 parser->scope = NULL_TREE;
24075 parser->qualifying_scope = NULL_TREE;
24076 parser->object_scope = NULL_TREE;
24077 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
24078 if (expr == error_mark_node)
24079 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
24080 token->location);
24081 if (TREE_CODE (expr) == TYPE_DECL)
24082 expr = TREE_TYPE (expr);
24083 else if (TREE_CODE (expr) == CONST_DECL)
24084 expr = DECL_INITIAL (expr);
24085 expr = make_pack_expansion (expr);
24087 if (paren)
24088 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24090 return expr;
24093 /* Parse the operand of `sizeof' (or a similar operator). Returns
24094 either a TYPE or an expression, depending on the form of the
24095 input. The KEYWORD indicates which kind of expression we have
24096 encountered. */
24098 static tree
24099 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
24101 tree expr = NULL_TREE;
24102 const char *saved_message;
24103 char *tmp;
24104 bool saved_integral_constant_expression_p;
24105 bool saved_non_integral_constant_expression_p;
24107 /* If it's a `...', then we are computing the length of a parameter
24108 pack. */
24109 if (keyword == RID_SIZEOF
24110 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24111 return cp_parser_sizeof_pack (parser);
24113 /* Types cannot be defined in a `sizeof' expression. Save away the
24114 old message. */
24115 saved_message = parser->type_definition_forbidden_message;
24116 /* And create the new one. */
24117 tmp = concat ("types may not be defined in %<",
24118 IDENTIFIER_POINTER (ridpointers[keyword]),
24119 "%> expressions", NULL);
24120 parser->type_definition_forbidden_message = tmp;
24122 /* The restrictions on constant-expressions do not apply inside
24123 sizeof expressions. */
24124 saved_integral_constant_expression_p
24125 = parser->integral_constant_expression_p;
24126 saved_non_integral_constant_expression_p
24127 = parser->non_integral_constant_expression_p;
24128 parser->integral_constant_expression_p = false;
24130 /* Do not actually evaluate the expression. */
24131 ++cp_unevaluated_operand;
24132 ++c_inhibit_evaluation_warnings;
24133 /* If it's a `(', then we might be looking at the type-id
24134 construction. */
24135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24137 tree type = NULL_TREE;
24139 /* We can't be sure yet whether we're looking at a type-id or an
24140 expression. */
24141 cp_parser_parse_tentatively (parser);
24142 /* Note: as a GNU Extension, compound literals are considered
24143 postfix-expressions as they are in C99, so they are valid
24144 arguments to sizeof. See comment in cp_parser_cast_expression
24145 for details. */
24146 if (cp_parser_compound_literal_p (parser))
24147 cp_parser_simulate_error (parser);
24148 else
24150 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
24151 parser->in_type_id_in_expr_p = true;
24152 /* Look for the type-id. */
24153 type = cp_parser_type_id (parser);
24154 /* Look for the closing `)'. */
24155 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24156 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
24159 /* If all went well, then we're done. */
24160 if (cp_parser_parse_definitely (parser))
24162 cp_decl_specifier_seq decl_specs;
24164 /* Build a trivial decl-specifier-seq. */
24165 clear_decl_specs (&decl_specs);
24166 decl_specs.type = type;
24168 /* Call grokdeclarator to figure out what type this is. */
24169 expr = grokdeclarator (NULL,
24170 &decl_specs,
24171 TYPENAME,
24172 /*initialized=*/0,
24173 /*attrlist=*/NULL);
24177 /* If the type-id production did not work out, then we must be
24178 looking at the unary-expression production. */
24179 if (!expr)
24180 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
24181 /*cast_p=*/false, NULL);
24183 /* Go back to evaluating expressions. */
24184 --cp_unevaluated_operand;
24185 --c_inhibit_evaluation_warnings;
24187 /* Free the message we created. */
24188 free (tmp);
24189 /* And restore the old one. */
24190 parser->type_definition_forbidden_message = saved_message;
24191 parser->integral_constant_expression_p
24192 = saved_integral_constant_expression_p;
24193 parser->non_integral_constant_expression_p
24194 = saved_non_integral_constant_expression_p;
24196 return expr;
24199 /* If the current declaration has no declarator, return true. */
24201 static bool
24202 cp_parser_declares_only_class_p (cp_parser *parser)
24204 /* If the next token is a `;' or a `,' then there is no
24205 declarator. */
24206 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24207 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
24210 /* Update the DECL_SPECS to reflect the storage class indicated by
24211 KEYWORD. */
24213 static void
24214 cp_parser_set_storage_class (cp_parser *parser,
24215 cp_decl_specifier_seq *decl_specs,
24216 enum rid keyword,
24217 cp_token *token)
24219 cp_storage_class storage_class;
24221 if (parser->in_unbraced_linkage_specification_p)
24223 error_at (token->location, "invalid use of %qD in linkage specification",
24224 ridpointers[keyword]);
24225 return;
24227 else if (decl_specs->storage_class != sc_none)
24229 decl_specs->conflicting_specifiers_p = true;
24230 return;
24233 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
24234 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
24235 && decl_specs->gnu_thread_keyword_p)
24237 pedwarn (decl_specs->locations[ds_thread], 0,
24238 "%<__thread%> before %qD", ridpointers[keyword]);
24241 switch (keyword)
24243 case RID_AUTO:
24244 storage_class = sc_auto;
24245 break;
24246 case RID_REGISTER:
24247 storage_class = sc_register;
24248 break;
24249 case RID_STATIC:
24250 storage_class = sc_static;
24251 break;
24252 case RID_EXTERN:
24253 storage_class = sc_extern;
24254 break;
24255 case RID_MUTABLE:
24256 storage_class = sc_mutable;
24257 break;
24258 default:
24259 gcc_unreachable ();
24261 decl_specs->storage_class = storage_class;
24262 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
24264 /* A storage class specifier cannot be applied alongside a typedef
24265 specifier. If there is a typedef specifier present then set
24266 conflicting_specifiers_p which will trigger an error later
24267 on in grokdeclarator. */
24268 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
24269 decl_specs->conflicting_specifiers_p = true;
24272 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
24273 is true, the type is a class or enum definition. */
24275 static void
24276 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
24277 tree type_spec,
24278 cp_token *token,
24279 bool type_definition_p)
24281 decl_specs->any_specifiers_p = true;
24283 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
24284 (with, for example, in "typedef int wchar_t;") we remember that
24285 this is what happened. In system headers, we ignore these
24286 declarations so that G++ can work with system headers that are not
24287 C++-safe. */
24288 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
24289 && !type_definition_p
24290 && (type_spec == boolean_type_node
24291 || type_spec == char16_type_node
24292 || type_spec == char32_type_node
24293 || type_spec == wchar_type_node)
24294 && (decl_specs->type
24295 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
24296 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
24297 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
24298 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
24300 decl_specs->redefined_builtin_type = type_spec;
24301 set_and_check_decl_spec_loc (decl_specs,
24302 ds_redefined_builtin_type_spec,
24303 token);
24304 if (!decl_specs->type)
24306 decl_specs->type = type_spec;
24307 decl_specs->type_definition_p = false;
24308 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
24311 else if (decl_specs->type)
24312 decl_specs->multiple_types_p = true;
24313 else
24315 decl_specs->type = type_spec;
24316 decl_specs->type_definition_p = type_definition_p;
24317 decl_specs->redefined_builtin_type = NULL_TREE;
24318 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
24322 /* True iff TOKEN is the GNU keyword __thread. */
24324 static bool
24325 token_is__thread (cp_token *token)
24327 gcc_assert (token->keyword == RID_THREAD);
24328 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
24331 /* Set the location for a declarator specifier and check if it is
24332 duplicated.
24334 DECL_SPECS is the sequence of declarator specifiers onto which to
24335 set the location.
24337 DS is the single declarator specifier to set which location is to
24338 be set onto the existing sequence of declarators.
24340 LOCATION is the location for the declarator specifier to
24341 consider. */
24343 static void
24344 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
24345 cp_decl_spec ds, cp_token *token)
24347 gcc_assert (ds < ds_last);
24349 if (decl_specs == NULL)
24350 return;
24352 source_location location = token->location;
24354 if (decl_specs->locations[ds] == 0)
24356 decl_specs->locations[ds] = location;
24357 if (ds == ds_thread)
24358 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
24360 else
24362 if (ds == ds_long)
24364 if (decl_specs->locations[ds_long_long] != 0)
24365 error_at (location,
24366 "%<long long long%> is too long for GCC");
24367 else
24369 decl_specs->locations[ds_long_long] = location;
24370 pedwarn_cxx98 (location,
24371 OPT_Wlong_long,
24372 "ISO C++ 1998 does not support %<long long%>");
24375 else if (ds == ds_thread)
24377 bool gnu = token_is__thread (token);
24378 if (gnu != decl_specs->gnu_thread_keyword_p)
24379 error_at (location,
24380 "both %<__thread%> and %<thread_local%> specified");
24381 else
24382 error_at (location, "duplicate %qD", token->u.value);
24384 else
24386 static const char *const decl_spec_names[] = {
24387 "signed",
24388 "unsigned",
24389 "short",
24390 "long",
24391 "const",
24392 "volatile",
24393 "restrict",
24394 "inline",
24395 "virtual",
24396 "explicit",
24397 "friend",
24398 "typedef",
24399 "using",
24400 "constexpr",
24401 "__complex"
24403 error_at (location,
24404 "duplicate %qs", decl_spec_names[ds]);
24409 /* Return true iff the declarator specifier DS is present in the
24410 sequence of declarator specifiers DECL_SPECS. */
24412 bool
24413 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
24414 cp_decl_spec ds)
24416 gcc_assert (ds < ds_last);
24418 if (decl_specs == NULL)
24419 return false;
24421 return decl_specs->locations[ds] != 0;
24424 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
24425 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
24427 static bool
24428 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
24430 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
24433 /* Issue an error message indicating that TOKEN_DESC was expected.
24434 If KEYWORD is true, it indicated this function is called by
24435 cp_parser_require_keword and the required token can only be
24436 a indicated keyword. */
24438 static void
24439 cp_parser_required_error (cp_parser *parser,
24440 required_token token_desc,
24441 bool keyword)
24443 switch (token_desc)
24445 case RT_NEW:
24446 cp_parser_error (parser, "expected %<new%>");
24447 return;
24448 case RT_DELETE:
24449 cp_parser_error (parser, "expected %<delete%>");
24450 return;
24451 case RT_RETURN:
24452 cp_parser_error (parser, "expected %<return%>");
24453 return;
24454 case RT_WHILE:
24455 cp_parser_error (parser, "expected %<while%>");
24456 return;
24457 case RT_EXTERN:
24458 cp_parser_error (parser, "expected %<extern%>");
24459 return;
24460 case RT_STATIC_ASSERT:
24461 cp_parser_error (parser, "expected %<static_assert%>");
24462 return;
24463 case RT_DECLTYPE:
24464 cp_parser_error (parser, "expected %<decltype%>");
24465 return;
24466 case RT_OPERATOR:
24467 cp_parser_error (parser, "expected %<operator%>");
24468 return;
24469 case RT_CLASS:
24470 cp_parser_error (parser, "expected %<class%>");
24471 return;
24472 case RT_TEMPLATE:
24473 cp_parser_error (parser, "expected %<template%>");
24474 return;
24475 case RT_NAMESPACE:
24476 cp_parser_error (parser, "expected %<namespace%>");
24477 return;
24478 case RT_USING:
24479 cp_parser_error (parser, "expected %<using%>");
24480 return;
24481 case RT_ASM:
24482 cp_parser_error (parser, "expected %<asm%>");
24483 return;
24484 case RT_TRY:
24485 cp_parser_error (parser, "expected %<try%>");
24486 return;
24487 case RT_CATCH:
24488 cp_parser_error (parser, "expected %<catch%>");
24489 return;
24490 case RT_THROW:
24491 cp_parser_error (parser, "expected %<throw%>");
24492 return;
24493 case RT_LABEL:
24494 cp_parser_error (parser, "expected %<__label__%>");
24495 return;
24496 case RT_AT_TRY:
24497 cp_parser_error (parser, "expected %<@try%>");
24498 return;
24499 case RT_AT_SYNCHRONIZED:
24500 cp_parser_error (parser, "expected %<@synchronized%>");
24501 return;
24502 case RT_AT_THROW:
24503 cp_parser_error (parser, "expected %<@throw%>");
24504 return;
24505 case RT_TRANSACTION_ATOMIC:
24506 cp_parser_error (parser, "expected %<__transaction_atomic%>");
24507 return;
24508 case RT_TRANSACTION_RELAXED:
24509 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
24510 return;
24511 default:
24512 break;
24514 if (!keyword)
24516 switch (token_desc)
24518 case RT_SEMICOLON:
24519 cp_parser_error (parser, "expected %<;%>");
24520 return;
24521 case RT_OPEN_PAREN:
24522 cp_parser_error (parser, "expected %<(%>");
24523 return;
24524 case RT_CLOSE_BRACE:
24525 cp_parser_error (parser, "expected %<}%>");
24526 return;
24527 case RT_OPEN_BRACE:
24528 cp_parser_error (parser, "expected %<{%>");
24529 return;
24530 case RT_CLOSE_SQUARE:
24531 cp_parser_error (parser, "expected %<]%>");
24532 return;
24533 case RT_OPEN_SQUARE:
24534 cp_parser_error (parser, "expected %<[%>");
24535 return;
24536 case RT_COMMA:
24537 cp_parser_error (parser, "expected %<,%>");
24538 return;
24539 case RT_SCOPE:
24540 cp_parser_error (parser, "expected %<::%>");
24541 return;
24542 case RT_LESS:
24543 cp_parser_error (parser, "expected %<<%>");
24544 return;
24545 case RT_GREATER:
24546 cp_parser_error (parser, "expected %<>%>");
24547 return;
24548 case RT_EQ:
24549 cp_parser_error (parser, "expected %<=%>");
24550 return;
24551 case RT_ELLIPSIS:
24552 cp_parser_error (parser, "expected %<...%>");
24553 return;
24554 case RT_MULT:
24555 cp_parser_error (parser, "expected %<*%>");
24556 return;
24557 case RT_COMPL:
24558 cp_parser_error (parser, "expected %<~%>");
24559 return;
24560 case RT_COLON:
24561 cp_parser_error (parser, "expected %<:%>");
24562 return;
24563 case RT_COLON_SCOPE:
24564 cp_parser_error (parser, "expected %<:%> or %<::%>");
24565 return;
24566 case RT_CLOSE_PAREN:
24567 cp_parser_error (parser, "expected %<)%>");
24568 return;
24569 case RT_COMMA_CLOSE_PAREN:
24570 cp_parser_error (parser, "expected %<,%> or %<)%>");
24571 return;
24572 case RT_PRAGMA_EOL:
24573 cp_parser_error (parser, "expected end of line");
24574 return;
24575 case RT_NAME:
24576 cp_parser_error (parser, "expected identifier");
24577 return;
24578 case RT_SELECT:
24579 cp_parser_error (parser, "expected selection-statement");
24580 return;
24581 case RT_INTERATION:
24582 cp_parser_error (parser, "expected iteration-statement");
24583 return;
24584 case RT_JUMP:
24585 cp_parser_error (parser, "expected jump-statement");
24586 return;
24587 case RT_CLASS_KEY:
24588 cp_parser_error (parser, "expected class-key");
24589 return;
24590 case RT_CLASS_TYPENAME_TEMPLATE:
24591 cp_parser_error (parser,
24592 "expected %<class%>, %<typename%>, or %<template%>");
24593 return;
24594 default:
24595 gcc_unreachable ();
24598 else
24599 gcc_unreachable ();
24604 /* If the next token is of the indicated TYPE, consume it. Otherwise,
24605 issue an error message indicating that TOKEN_DESC was expected.
24607 Returns the token consumed, if the token had the appropriate type.
24608 Otherwise, returns NULL. */
24610 static cp_token *
24611 cp_parser_require (cp_parser* parser,
24612 enum cpp_ttype type,
24613 required_token token_desc)
24615 if (cp_lexer_next_token_is (parser->lexer, type))
24616 return cp_lexer_consume_token (parser->lexer);
24617 else
24619 /* Output the MESSAGE -- unless we're parsing tentatively. */
24620 if (!cp_parser_simulate_error (parser))
24621 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
24622 return NULL;
24626 /* An error message is produced if the next token is not '>'.
24627 All further tokens are skipped until the desired token is
24628 found or '{', '}', ';' or an unbalanced ')' or ']'. */
24630 static void
24631 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
24633 /* Current level of '< ... >'. */
24634 unsigned level = 0;
24635 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
24636 unsigned nesting_depth = 0;
24638 /* Are we ready, yet? If not, issue error message. */
24639 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
24640 return;
24642 /* Skip tokens until the desired token is found. */
24643 while (true)
24645 /* Peek at the next token. */
24646 switch (cp_lexer_peek_token (parser->lexer)->type)
24648 case CPP_LESS:
24649 if (!nesting_depth)
24650 ++level;
24651 break;
24653 case CPP_RSHIFT:
24654 if (cxx_dialect == cxx98)
24655 /* C++0x views the `>>' operator as two `>' tokens, but
24656 C++98 does not. */
24657 break;
24658 else if (!nesting_depth && level-- == 0)
24660 /* We've hit a `>>' where the first `>' closes the
24661 template argument list, and the second `>' is
24662 spurious. Just consume the `>>' and stop; we've
24663 already produced at least one error. */
24664 cp_lexer_consume_token (parser->lexer);
24665 return;
24667 /* Fall through for C++0x, so we handle the second `>' in
24668 the `>>'. */
24670 case CPP_GREATER:
24671 if (!nesting_depth && level-- == 0)
24673 /* We've reached the token we want, consume it and stop. */
24674 cp_lexer_consume_token (parser->lexer);
24675 return;
24677 break;
24679 case CPP_OPEN_PAREN:
24680 case CPP_OPEN_SQUARE:
24681 ++nesting_depth;
24682 break;
24684 case CPP_CLOSE_PAREN:
24685 case CPP_CLOSE_SQUARE:
24686 if (nesting_depth-- == 0)
24687 return;
24688 break;
24690 case CPP_EOF:
24691 case CPP_PRAGMA_EOL:
24692 case CPP_SEMICOLON:
24693 case CPP_OPEN_BRACE:
24694 case CPP_CLOSE_BRACE:
24695 /* The '>' was probably forgotten, don't look further. */
24696 return;
24698 default:
24699 break;
24702 /* Consume this token. */
24703 cp_lexer_consume_token (parser->lexer);
24707 /* If the next token is the indicated keyword, consume it. Otherwise,
24708 issue an error message indicating that TOKEN_DESC was expected.
24710 Returns the token consumed, if the token had the appropriate type.
24711 Otherwise, returns NULL. */
24713 static cp_token *
24714 cp_parser_require_keyword (cp_parser* parser,
24715 enum rid keyword,
24716 required_token token_desc)
24718 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
24720 if (token && token->keyword != keyword)
24722 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
24723 return NULL;
24726 return token;
24729 /* Returns TRUE iff TOKEN is a token that can begin the body of a
24730 function-definition. */
24732 static bool
24733 cp_parser_token_starts_function_definition_p (cp_token* token)
24735 return (/* An ordinary function-body begins with an `{'. */
24736 token->type == CPP_OPEN_BRACE
24737 /* A ctor-initializer begins with a `:'. */
24738 || token->type == CPP_COLON
24739 /* A function-try-block begins with `try'. */
24740 || token->keyword == RID_TRY
24741 /* A function-transaction-block begins with `__transaction_atomic'
24742 or `__transaction_relaxed'. */
24743 || token->keyword == RID_TRANSACTION_ATOMIC
24744 || token->keyword == RID_TRANSACTION_RELAXED
24745 /* The named return value extension begins with `return'. */
24746 || token->keyword == RID_RETURN);
24749 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
24750 definition. */
24752 static bool
24753 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
24755 cp_token *token;
24757 token = cp_lexer_peek_token (parser->lexer);
24758 return (token->type == CPP_OPEN_BRACE
24759 || (token->type == CPP_COLON
24760 && !parser->colon_doesnt_start_class_def_p));
24763 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
24764 C++0x) ending a template-argument. */
24766 static bool
24767 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
24769 cp_token *token;
24771 token = cp_lexer_peek_token (parser->lexer);
24772 return (token->type == CPP_COMMA
24773 || token->type == CPP_GREATER
24774 || token->type == CPP_ELLIPSIS
24775 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
24778 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
24779 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
24781 static bool
24782 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
24783 size_t n)
24785 cp_token *token;
24787 token = cp_lexer_peek_nth_token (parser->lexer, n);
24788 if (token->type == CPP_LESS)
24789 return true;
24790 /* Check for the sequence `<::' in the original code. It would be lexed as
24791 `[:', where `[' is a digraph, and there is no whitespace before
24792 `:'. */
24793 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
24795 cp_token *token2;
24796 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
24797 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
24798 return true;
24800 return false;
24803 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
24804 or none_type otherwise. */
24806 static enum tag_types
24807 cp_parser_token_is_class_key (cp_token* token)
24809 switch (token->keyword)
24811 case RID_CLASS:
24812 return class_type;
24813 case RID_STRUCT:
24814 return record_type;
24815 case RID_UNION:
24816 return union_type;
24818 default:
24819 return none_type;
24823 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
24824 or none_type otherwise or if the token is null. */
24826 static enum tag_types
24827 cp_parser_token_is_type_parameter_key (cp_token* token)
24829 if (!token)
24830 return none_type;
24832 switch (token->keyword)
24834 case RID_CLASS:
24835 return class_type;
24836 case RID_TYPENAME:
24837 return typename_type;
24839 default:
24840 return none_type;
24844 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
24846 static void
24847 cp_parser_check_class_key (enum tag_types class_key, tree type)
24849 if (type == error_mark_node)
24850 return;
24851 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
24853 if (permerror (input_location, "%qs tag used in naming %q#T",
24854 class_key == union_type ? "union"
24855 : class_key == record_type ? "struct" : "class",
24856 type))
24857 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
24858 "%q#T was previously declared here", type);
24862 /* Issue an error message if DECL is redeclared with different
24863 access than its original declaration [class.access.spec/3].
24864 This applies to nested classes and nested class templates.
24865 [class.mem/1]. */
24867 static void
24868 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
24870 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
24871 return;
24873 if ((TREE_PRIVATE (decl)
24874 != (current_access_specifier == access_private_node))
24875 || (TREE_PROTECTED (decl)
24876 != (current_access_specifier == access_protected_node)))
24877 error_at (location, "%qD redeclared with different access", decl);
24880 /* Look for the `template' keyword, as a syntactic disambiguator.
24881 Return TRUE iff it is present, in which case it will be
24882 consumed. */
24884 static bool
24885 cp_parser_optional_template_keyword (cp_parser *parser)
24887 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24889 /* In C++98 the `template' keyword can only be used within templates;
24890 outside templates the parser can always figure out what is a
24891 template and what is not. In C++11, per the resolution of DR 468,
24892 `template' is allowed in cases where it is not strictly necessary. */
24893 if (!processing_template_decl
24894 && pedantic && cxx_dialect == cxx98)
24896 cp_token *token = cp_lexer_peek_token (parser->lexer);
24897 pedwarn (token->location, OPT_Wpedantic,
24898 "in C++98 %<template%> (as a disambiguator) is only "
24899 "allowed within templates");
24900 /* If this part of the token stream is rescanned, the same
24901 error message would be generated. So, we purge the token
24902 from the stream. */
24903 cp_lexer_purge_token (parser->lexer);
24904 return false;
24906 else
24908 /* Consume the `template' keyword. */
24909 cp_lexer_consume_token (parser->lexer);
24910 return true;
24913 return false;
24916 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
24917 set PARSER->SCOPE, and perform other related actions. */
24919 static void
24920 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
24922 int i;
24923 struct tree_check *check_value;
24924 deferred_access_check *chk;
24925 vec<deferred_access_check, va_gc> *checks;
24927 /* Get the stored value. */
24928 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
24929 /* Perform any access checks that were deferred. */
24930 checks = check_value->checks;
24931 if (checks)
24933 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
24934 perform_or_defer_access_check (chk->binfo,
24935 chk->decl,
24936 chk->diag_decl, tf_warning_or_error);
24938 /* Set the scope from the stored value. */
24939 parser->scope = check_value->value;
24940 parser->qualifying_scope = check_value->qualifying_scope;
24941 parser->object_scope = NULL_TREE;
24944 /* Consume tokens up through a non-nested END token. Returns TRUE if we
24945 encounter the end of a block before what we were looking for. */
24947 static bool
24948 cp_parser_cache_group (cp_parser *parser,
24949 enum cpp_ttype end,
24950 unsigned depth)
24952 while (true)
24954 cp_token *token = cp_lexer_peek_token (parser->lexer);
24956 /* Abort a parenthesized expression if we encounter a semicolon. */
24957 if ((end == CPP_CLOSE_PAREN || depth == 0)
24958 && token->type == CPP_SEMICOLON)
24959 return true;
24960 /* If we've reached the end of the file, stop. */
24961 if (token->type == CPP_EOF
24962 || (end != CPP_PRAGMA_EOL
24963 && token->type == CPP_PRAGMA_EOL))
24964 return true;
24965 if (token->type == CPP_CLOSE_BRACE && depth == 0)
24966 /* We've hit the end of an enclosing block, so there's been some
24967 kind of syntax error. */
24968 return true;
24970 /* Consume the token. */
24971 cp_lexer_consume_token (parser->lexer);
24972 /* See if it starts a new group. */
24973 if (token->type == CPP_OPEN_BRACE)
24975 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
24976 /* In theory this should probably check end == '}', but
24977 cp_parser_save_member_function_body needs it to exit
24978 after either '}' or ')' when called with ')'. */
24979 if (depth == 0)
24980 return false;
24982 else if (token->type == CPP_OPEN_PAREN)
24984 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
24985 if (depth == 0 && end == CPP_CLOSE_PAREN)
24986 return false;
24988 else if (token->type == CPP_PRAGMA)
24989 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
24990 else if (token->type == end)
24991 return false;
24995 /* Like above, for caching a default argument or NSDMI. Both of these are
24996 terminated by a non-nested comma, but it can be unclear whether or not a
24997 comma is nested in a template argument list unless we do more parsing.
24998 In order to handle this ambiguity, when we encounter a ',' after a '<'
24999 we try to parse what follows as a parameter-declaration-list (in the
25000 case of a default argument) or a member-declarator (in the case of an
25001 NSDMI). If that succeeds, then we stop caching. */
25003 static tree
25004 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
25006 unsigned depth = 0;
25007 int maybe_template_id = 0;
25008 cp_token *first_token;
25009 cp_token *token;
25010 tree default_argument;
25012 /* Add tokens until we have processed the entire default
25013 argument. We add the range [first_token, token). */
25014 first_token = cp_lexer_peek_token (parser->lexer);
25015 if (first_token->type == CPP_OPEN_BRACE)
25017 /* For list-initialization, this is straightforward. */
25018 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
25019 token = cp_lexer_peek_token (parser->lexer);
25021 else while (true)
25023 bool done = false;
25025 /* Peek at the next token. */
25026 token = cp_lexer_peek_token (parser->lexer);
25027 /* What we do depends on what token we have. */
25028 switch (token->type)
25030 /* In valid code, a default argument must be
25031 immediately followed by a `,' `)', or `...'. */
25032 case CPP_COMMA:
25033 if (depth == 0 && maybe_template_id)
25035 /* If we've seen a '<', we might be in a
25036 template-argument-list. Until Core issue 325 is
25037 resolved, we don't know how this situation ought
25038 to be handled, so try to DTRT. We check whether
25039 what comes after the comma is a valid parameter
25040 declaration list. If it is, then the comma ends
25041 the default argument; otherwise the default
25042 argument continues. */
25043 bool error = false;
25045 /* Set ITALP so cp_parser_parameter_declaration_list
25046 doesn't decide to commit to this parse. */
25047 bool saved_italp = parser->in_template_argument_list_p;
25048 parser->in_template_argument_list_p = true;
25050 cp_parser_parse_tentatively (parser);
25051 cp_lexer_consume_token (parser->lexer);
25053 if (nsdmi)
25055 int ctor_dtor_or_conv_p;
25056 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
25057 &ctor_dtor_or_conv_p,
25058 /*parenthesized_p=*/NULL,
25059 /*member_p=*/true,
25060 /*friend_p=*/false);
25062 else
25064 begin_scope (sk_function_parms, NULL_TREE);
25065 cp_parser_parameter_declaration_list (parser, &error);
25066 pop_bindings_and_leave_scope ();
25068 if (!cp_parser_error_occurred (parser) && !error)
25069 done = true;
25070 cp_parser_abort_tentative_parse (parser);
25072 parser->in_template_argument_list_p = saved_italp;
25073 break;
25075 case CPP_CLOSE_PAREN:
25076 case CPP_ELLIPSIS:
25077 /* If we run into a non-nested `;', `}', or `]',
25078 then the code is invalid -- but the default
25079 argument is certainly over. */
25080 case CPP_SEMICOLON:
25081 case CPP_CLOSE_BRACE:
25082 case CPP_CLOSE_SQUARE:
25083 if (depth == 0
25084 /* Handle correctly int n = sizeof ... ( p ); */
25085 && token->type != CPP_ELLIPSIS)
25086 done = true;
25087 /* Update DEPTH, if necessary. */
25088 else if (token->type == CPP_CLOSE_PAREN
25089 || token->type == CPP_CLOSE_BRACE
25090 || token->type == CPP_CLOSE_SQUARE)
25091 --depth;
25092 break;
25094 case CPP_OPEN_PAREN:
25095 case CPP_OPEN_SQUARE:
25096 case CPP_OPEN_BRACE:
25097 ++depth;
25098 break;
25100 case CPP_LESS:
25101 if (depth == 0)
25102 /* This might be the comparison operator, or it might
25103 start a template argument list. */
25104 ++maybe_template_id;
25105 break;
25107 case CPP_RSHIFT:
25108 if (cxx_dialect == cxx98)
25109 break;
25110 /* Fall through for C++0x, which treats the `>>'
25111 operator like two `>' tokens in certain
25112 cases. */
25114 case CPP_GREATER:
25115 if (depth == 0)
25117 /* This might be an operator, or it might close a
25118 template argument list. But if a previous '<'
25119 started a template argument list, this will have
25120 closed it, so we can't be in one anymore. */
25121 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
25122 if (maybe_template_id < 0)
25123 maybe_template_id = 0;
25125 break;
25127 /* If we run out of tokens, issue an error message. */
25128 case CPP_EOF:
25129 case CPP_PRAGMA_EOL:
25130 error_at (token->location, "file ends in default argument");
25131 done = true;
25132 break;
25134 case CPP_NAME:
25135 case CPP_SCOPE:
25136 /* In these cases, we should look for template-ids.
25137 For example, if the default argument is
25138 `X<int, double>()', we need to do name lookup to
25139 figure out whether or not `X' is a template; if
25140 so, the `,' does not end the default argument.
25142 That is not yet done. */
25143 break;
25145 default:
25146 break;
25149 /* If we've reached the end, stop. */
25150 if (done)
25151 break;
25153 /* Add the token to the token block. */
25154 token = cp_lexer_consume_token (parser->lexer);
25157 /* Create a DEFAULT_ARG to represent the unparsed default
25158 argument. */
25159 default_argument = make_node (DEFAULT_ARG);
25160 DEFARG_TOKENS (default_argument)
25161 = cp_token_cache_new (first_token, token);
25162 DEFARG_INSTANTIATIONS (default_argument) = NULL;
25164 return default_argument;
25167 /* Begin parsing tentatively. We always save tokens while parsing
25168 tentatively so that if the tentative parsing fails we can restore the
25169 tokens. */
25171 static void
25172 cp_parser_parse_tentatively (cp_parser* parser)
25174 /* Enter a new parsing context. */
25175 parser->context = cp_parser_context_new (parser->context);
25176 /* Begin saving tokens. */
25177 cp_lexer_save_tokens (parser->lexer);
25178 /* In order to avoid repetitive access control error messages,
25179 access checks are queued up until we are no longer parsing
25180 tentatively. */
25181 push_deferring_access_checks (dk_deferred);
25184 /* Commit to the currently active tentative parse. */
25186 static void
25187 cp_parser_commit_to_tentative_parse (cp_parser* parser)
25189 cp_parser_context *context;
25190 cp_lexer *lexer;
25192 /* Mark all of the levels as committed. */
25193 lexer = parser->lexer;
25194 for (context = parser->context; context->next; context = context->next)
25196 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25197 break;
25198 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25199 while (!cp_lexer_saving_tokens (lexer))
25200 lexer = lexer->next;
25201 cp_lexer_commit_tokens (lexer);
25205 /* Commit to the topmost currently active tentative parse.
25207 Note that this function shouldn't be called when there are
25208 irreversible side-effects while in a tentative state. For
25209 example, we shouldn't create a permanent entry in the symbol
25210 table, or issue an error message that might not apply if the
25211 tentative parse is aborted. */
25213 static void
25214 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
25216 cp_parser_context *context = parser->context;
25217 cp_lexer *lexer = parser->lexer;
25219 if (context)
25221 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
25222 return;
25223 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
25225 while (!cp_lexer_saving_tokens (lexer))
25226 lexer = lexer->next;
25227 cp_lexer_commit_tokens (lexer);
25231 /* Abort the currently active tentative parse. All consumed tokens
25232 will be rolled back, and no diagnostics will be issued. */
25234 static void
25235 cp_parser_abort_tentative_parse (cp_parser* parser)
25237 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
25238 || errorcount > 0);
25239 cp_parser_simulate_error (parser);
25240 /* Now, pretend that we want to see if the construct was
25241 successfully parsed. */
25242 cp_parser_parse_definitely (parser);
25245 /* Stop parsing tentatively. If a parse error has occurred, restore the
25246 token stream. Otherwise, commit to the tokens we have consumed.
25247 Returns true if no error occurred; false otherwise. */
25249 static bool
25250 cp_parser_parse_definitely (cp_parser* parser)
25252 bool error_occurred;
25253 cp_parser_context *context;
25255 /* Remember whether or not an error occurred, since we are about to
25256 destroy that information. */
25257 error_occurred = cp_parser_error_occurred (parser);
25258 /* Remove the topmost context from the stack. */
25259 context = parser->context;
25260 parser->context = context->next;
25261 /* If no parse errors occurred, commit to the tentative parse. */
25262 if (!error_occurred)
25264 /* Commit to the tokens read tentatively, unless that was
25265 already done. */
25266 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
25267 cp_lexer_commit_tokens (parser->lexer);
25269 pop_to_parent_deferring_access_checks ();
25271 /* Otherwise, if errors occurred, roll back our state so that things
25272 are just as they were before we began the tentative parse. */
25273 else
25275 cp_lexer_rollback_tokens (parser->lexer);
25276 pop_deferring_access_checks ();
25278 /* Add the context to the front of the free list. */
25279 context->next = cp_parser_context_free_list;
25280 cp_parser_context_free_list = context;
25282 return !error_occurred;
25285 /* Returns true if we are parsing tentatively and are not committed to
25286 this tentative parse. */
25288 static bool
25289 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
25291 return (cp_parser_parsing_tentatively (parser)
25292 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
25295 /* Returns nonzero iff an error has occurred during the most recent
25296 tentative parse. */
25298 static bool
25299 cp_parser_error_occurred (cp_parser* parser)
25301 return (cp_parser_parsing_tentatively (parser)
25302 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
25305 /* Returns nonzero if GNU extensions are allowed. */
25307 static bool
25308 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
25310 return parser->allow_gnu_extensions_p;
25313 /* Objective-C++ Productions */
25316 /* Parse an Objective-C expression, which feeds into a primary-expression
25317 above.
25319 objc-expression:
25320 objc-message-expression
25321 objc-string-literal
25322 objc-encode-expression
25323 objc-protocol-expression
25324 objc-selector-expression
25326 Returns a tree representation of the expression. */
25328 static tree
25329 cp_parser_objc_expression (cp_parser* parser)
25331 /* Try to figure out what kind of declaration is present. */
25332 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
25334 switch (kwd->type)
25336 case CPP_OPEN_SQUARE:
25337 return cp_parser_objc_message_expression (parser);
25339 case CPP_OBJC_STRING:
25340 kwd = cp_lexer_consume_token (parser->lexer);
25341 return objc_build_string_object (kwd->u.value);
25343 case CPP_KEYWORD:
25344 switch (kwd->keyword)
25346 case RID_AT_ENCODE:
25347 return cp_parser_objc_encode_expression (parser);
25349 case RID_AT_PROTOCOL:
25350 return cp_parser_objc_protocol_expression (parser);
25352 case RID_AT_SELECTOR:
25353 return cp_parser_objc_selector_expression (parser);
25355 default:
25356 break;
25358 default:
25359 error_at (kwd->location,
25360 "misplaced %<@%D%> Objective-C++ construct",
25361 kwd->u.value);
25362 cp_parser_skip_to_end_of_block_or_statement (parser);
25365 return error_mark_node;
25368 /* Parse an Objective-C message expression.
25370 objc-message-expression:
25371 [ objc-message-receiver objc-message-args ]
25373 Returns a representation of an Objective-C message. */
25375 static tree
25376 cp_parser_objc_message_expression (cp_parser* parser)
25378 tree receiver, messageargs;
25380 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
25381 receiver = cp_parser_objc_message_receiver (parser);
25382 messageargs = cp_parser_objc_message_args (parser);
25383 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25385 return objc_build_message_expr (receiver, messageargs);
25388 /* Parse an objc-message-receiver.
25390 objc-message-receiver:
25391 expression
25392 simple-type-specifier
25394 Returns a representation of the type or expression. */
25396 static tree
25397 cp_parser_objc_message_receiver (cp_parser* parser)
25399 tree rcv;
25401 /* An Objective-C message receiver may be either (1) a type
25402 or (2) an expression. */
25403 cp_parser_parse_tentatively (parser);
25404 rcv = cp_parser_expression (parser);
25406 if (cp_parser_parse_definitely (parser))
25407 return rcv;
25409 rcv = cp_parser_simple_type_specifier (parser,
25410 /*decl_specs=*/NULL,
25411 CP_PARSER_FLAGS_NONE);
25413 return objc_get_class_reference (rcv);
25416 /* Parse the arguments and selectors comprising an Objective-C message.
25418 objc-message-args:
25419 objc-selector
25420 objc-selector-args
25421 objc-selector-args , objc-comma-args
25423 objc-selector-args:
25424 objc-selector [opt] : assignment-expression
25425 objc-selector-args objc-selector [opt] : assignment-expression
25427 objc-comma-args:
25428 assignment-expression
25429 objc-comma-args , assignment-expression
25431 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
25432 selector arguments and TREE_VALUE containing a list of comma
25433 arguments. */
25435 static tree
25436 cp_parser_objc_message_args (cp_parser* parser)
25438 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
25439 bool maybe_unary_selector_p = true;
25440 cp_token *token = cp_lexer_peek_token (parser->lexer);
25442 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25444 tree selector = NULL_TREE, arg;
25446 if (token->type != CPP_COLON)
25447 selector = cp_parser_objc_selector (parser);
25449 /* Detect if we have a unary selector. */
25450 if (maybe_unary_selector_p
25451 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25452 return build_tree_list (selector, NULL_TREE);
25454 maybe_unary_selector_p = false;
25455 cp_parser_require (parser, CPP_COLON, RT_COLON);
25456 arg = cp_parser_assignment_expression (parser, false, NULL);
25458 sel_args
25459 = chainon (sel_args,
25460 build_tree_list (selector, arg));
25462 token = cp_lexer_peek_token (parser->lexer);
25465 /* Handle non-selector arguments, if any. */
25466 while (token->type == CPP_COMMA)
25468 tree arg;
25470 cp_lexer_consume_token (parser->lexer);
25471 arg = cp_parser_assignment_expression (parser, false, NULL);
25473 addl_args
25474 = chainon (addl_args,
25475 build_tree_list (NULL_TREE, arg));
25477 token = cp_lexer_peek_token (parser->lexer);
25480 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
25482 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
25483 return build_tree_list (error_mark_node, error_mark_node);
25486 return build_tree_list (sel_args, addl_args);
25489 /* Parse an Objective-C encode expression.
25491 objc-encode-expression:
25492 @encode objc-typename
25494 Returns an encoded representation of the type argument. */
25496 static tree
25497 cp_parser_objc_encode_expression (cp_parser* parser)
25499 tree type;
25500 cp_token *token;
25502 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
25503 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25504 token = cp_lexer_peek_token (parser->lexer);
25505 type = complete_type (cp_parser_type_id (parser));
25506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25508 if (!type)
25510 error_at (token->location,
25511 "%<@encode%> must specify a type as an argument");
25512 return error_mark_node;
25515 /* This happens if we find @encode(T) (where T is a template
25516 typename or something dependent on a template typename) when
25517 parsing a template. In that case, we can't compile it
25518 immediately, but we rather create an AT_ENCODE_EXPR which will
25519 need to be instantiated when the template is used.
25521 if (dependent_type_p (type))
25523 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
25524 TREE_READONLY (value) = 1;
25525 return value;
25528 return objc_build_encode_expr (type);
25531 /* Parse an Objective-C @defs expression. */
25533 static tree
25534 cp_parser_objc_defs_expression (cp_parser *parser)
25536 tree name;
25538 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
25539 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25540 name = cp_parser_identifier (parser);
25541 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25543 return objc_get_class_ivars (name);
25546 /* Parse an Objective-C protocol expression.
25548 objc-protocol-expression:
25549 @protocol ( identifier )
25551 Returns a representation of the protocol expression. */
25553 static tree
25554 cp_parser_objc_protocol_expression (cp_parser* parser)
25556 tree proto;
25558 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
25559 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25560 proto = cp_parser_identifier (parser);
25561 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25563 return objc_build_protocol_expr (proto);
25566 /* Parse an Objective-C selector expression.
25568 objc-selector-expression:
25569 @selector ( objc-method-signature )
25571 objc-method-signature:
25572 objc-selector
25573 objc-selector-seq
25575 objc-selector-seq:
25576 objc-selector :
25577 objc-selector-seq objc-selector :
25579 Returns a representation of the method selector. */
25581 static tree
25582 cp_parser_objc_selector_expression (cp_parser* parser)
25584 tree sel_seq = NULL_TREE;
25585 bool maybe_unary_selector_p = true;
25586 cp_token *token;
25587 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25589 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
25590 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
25591 token = cp_lexer_peek_token (parser->lexer);
25593 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
25594 || token->type == CPP_SCOPE)
25596 tree selector = NULL_TREE;
25598 if (token->type != CPP_COLON
25599 || token->type == CPP_SCOPE)
25600 selector = cp_parser_objc_selector (parser);
25602 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
25603 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
25605 /* Detect if we have a unary selector. */
25606 if (maybe_unary_selector_p)
25608 sel_seq = selector;
25609 goto finish_selector;
25611 else
25613 cp_parser_error (parser, "expected %<:%>");
25616 maybe_unary_selector_p = false;
25617 token = cp_lexer_consume_token (parser->lexer);
25619 if (token->type == CPP_SCOPE)
25621 sel_seq
25622 = chainon (sel_seq,
25623 build_tree_list (selector, NULL_TREE));
25624 sel_seq
25625 = chainon (sel_seq,
25626 build_tree_list (NULL_TREE, NULL_TREE));
25628 else
25629 sel_seq
25630 = chainon (sel_seq,
25631 build_tree_list (selector, NULL_TREE));
25633 token = cp_lexer_peek_token (parser->lexer);
25636 finish_selector:
25637 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25639 return objc_build_selector_expr (loc, sel_seq);
25642 /* Parse a list of identifiers.
25644 objc-identifier-list:
25645 identifier
25646 objc-identifier-list , identifier
25648 Returns a TREE_LIST of identifier nodes. */
25650 static tree
25651 cp_parser_objc_identifier_list (cp_parser* parser)
25653 tree identifier;
25654 tree list;
25655 cp_token *sep;
25657 identifier = cp_parser_identifier (parser);
25658 if (identifier == error_mark_node)
25659 return error_mark_node;
25661 list = build_tree_list (NULL_TREE, identifier);
25662 sep = cp_lexer_peek_token (parser->lexer);
25664 while (sep->type == CPP_COMMA)
25666 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
25667 identifier = cp_parser_identifier (parser);
25668 if (identifier == error_mark_node)
25669 return list;
25671 list = chainon (list, build_tree_list (NULL_TREE,
25672 identifier));
25673 sep = cp_lexer_peek_token (parser->lexer);
25676 return list;
25679 /* Parse an Objective-C alias declaration.
25681 objc-alias-declaration:
25682 @compatibility_alias identifier identifier ;
25684 This function registers the alias mapping with the Objective-C front end.
25685 It returns nothing. */
25687 static void
25688 cp_parser_objc_alias_declaration (cp_parser* parser)
25690 tree alias, orig;
25692 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
25693 alias = cp_parser_identifier (parser);
25694 orig = cp_parser_identifier (parser);
25695 objc_declare_alias (alias, orig);
25696 cp_parser_consume_semicolon_at_end_of_statement (parser);
25699 /* Parse an Objective-C class forward-declaration.
25701 objc-class-declaration:
25702 @class objc-identifier-list ;
25704 The function registers the forward declarations with the Objective-C
25705 front end. It returns nothing. */
25707 static void
25708 cp_parser_objc_class_declaration (cp_parser* parser)
25710 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
25711 while (true)
25713 tree id;
25715 id = cp_parser_identifier (parser);
25716 if (id == error_mark_node)
25717 break;
25719 objc_declare_class (id);
25721 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25722 cp_lexer_consume_token (parser->lexer);
25723 else
25724 break;
25726 cp_parser_consume_semicolon_at_end_of_statement (parser);
25729 /* Parse a list of Objective-C protocol references.
25731 objc-protocol-refs-opt:
25732 objc-protocol-refs [opt]
25734 objc-protocol-refs:
25735 < objc-identifier-list >
25737 Returns a TREE_LIST of identifiers, if any. */
25739 static tree
25740 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
25742 tree protorefs = NULL_TREE;
25744 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
25746 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
25747 protorefs = cp_parser_objc_identifier_list (parser);
25748 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
25751 return protorefs;
25754 /* Parse a Objective-C visibility specification. */
25756 static void
25757 cp_parser_objc_visibility_spec (cp_parser* parser)
25759 cp_token *vis = cp_lexer_peek_token (parser->lexer);
25761 switch (vis->keyword)
25763 case RID_AT_PRIVATE:
25764 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
25765 break;
25766 case RID_AT_PROTECTED:
25767 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
25768 break;
25769 case RID_AT_PUBLIC:
25770 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
25771 break;
25772 case RID_AT_PACKAGE:
25773 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
25774 break;
25775 default:
25776 return;
25779 /* Eat '@private'/'@protected'/'@public'. */
25780 cp_lexer_consume_token (parser->lexer);
25783 /* Parse an Objective-C method type. Return 'true' if it is a class
25784 (+) method, and 'false' if it is an instance (-) method. */
25786 static inline bool
25787 cp_parser_objc_method_type (cp_parser* parser)
25789 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
25790 return true;
25791 else
25792 return false;
25795 /* Parse an Objective-C protocol qualifier. */
25797 static tree
25798 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
25800 tree quals = NULL_TREE, node;
25801 cp_token *token = cp_lexer_peek_token (parser->lexer);
25803 node = token->u.value;
25805 while (node && identifier_p (node)
25806 && (node == ridpointers [(int) RID_IN]
25807 || node == ridpointers [(int) RID_OUT]
25808 || node == ridpointers [(int) RID_INOUT]
25809 || node == ridpointers [(int) RID_BYCOPY]
25810 || node == ridpointers [(int) RID_BYREF]
25811 || node == ridpointers [(int) RID_ONEWAY]))
25813 quals = tree_cons (NULL_TREE, node, quals);
25814 cp_lexer_consume_token (parser->lexer);
25815 token = cp_lexer_peek_token (parser->lexer);
25816 node = token->u.value;
25819 return quals;
25822 /* Parse an Objective-C typename. */
25824 static tree
25825 cp_parser_objc_typename (cp_parser* parser)
25827 tree type_name = NULL_TREE;
25829 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25831 tree proto_quals, cp_type = NULL_TREE;
25833 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
25834 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
25836 /* An ObjC type name may consist of just protocol qualifiers, in which
25837 case the type shall default to 'id'. */
25838 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25840 cp_type = cp_parser_type_id (parser);
25842 /* If the type could not be parsed, an error has already
25843 been produced. For error recovery, behave as if it had
25844 not been specified, which will use the default type
25845 'id'. */
25846 if (cp_type == error_mark_node)
25848 cp_type = NULL_TREE;
25849 /* We need to skip to the closing parenthesis as
25850 cp_parser_type_id() does not seem to do it for
25851 us. */
25852 cp_parser_skip_to_closing_parenthesis (parser,
25853 /*recovering=*/true,
25854 /*or_comma=*/false,
25855 /*consume_paren=*/false);
25859 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
25860 type_name = build_tree_list (proto_quals, cp_type);
25863 return type_name;
25866 /* Check to see if TYPE refers to an Objective-C selector name. */
25868 static bool
25869 cp_parser_objc_selector_p (enum cpp_ttype type)
25871 return (type == CPP_NAME || type == CPP_KEYWORD
25872 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
25873 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
25874 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
25875 || type == CPP_XOR || type == CPP_XOR_EQ);
25878 /* Parse an Objective-C selector. */
25880 static tree
25881 cp_parser_objc_selector (cp_parser* parser)
25883 cp_token *token = cp_lexer_consume_token (parser->lexer);
25885 if (!cp_parser_objc_selector_p (token->type))
25887 error_at (token->location, "invalid Objective-C++ selector name");
25888 return error_mark_node;
25891 /* C++ operator names are allowed to appear in ObjC selectors. */
25892 switch (token->type)
25894 case CPP_AND_AND: return get_identifier ("and");
25895 case CPP_AND_EQ: return get_identifier ("and_eq");
25896 case CPP_AND: return get_identifier ("bitand");
25897 case CPP_OR: return get_identifier ("bitor");
25898 case CPP_COMPL: return get_identifier ("compl");
25899 case CPP_NOT: return get_identifier ("not");
25900 case CPP_NOT_EQ: return get_identifier ("not_eq");
25901 case CPP_OR_OR: return get_identifier ("or");
25902 case CPP_OR_EQ: return get_identifier ("or_eq");
25903 case CPP_XOR: return get_identifier ("xor");
25904 case CPP_XOR_EQ: return get_identifier ("xor_eq");
25905 default: return token->u.value;
25909 /* Parse an Objective-C params list. */
25911 static tree
25912 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
25914 tree params = NULL_TREE;
25915 bool maybe_unary_selector_p = true;
25916 cp_token *token = cp_lexer_peek_token (parser->lexer);
25918 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
25920 tree selector = NULL_TREE, type_name, identifier;
25921 tree parm_attr = NULL_TREE;
25923 if (token->keyword == RID_ATTRIBUTE)
25924 break;
25926 if (token->type != CPP_COLON)
25927 selector = cp_parser_objc_selector (parser);
25929 /* Detect if we have a unary selector. */
25930 if (maybe_unary_selector_p
25931 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
25933 params = selector; /* Might be followed by attributes. */
25934 break;
25937 maybe_unary_selector_p = false;
25938 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25940 /* Something went quite wrong. There should be a colon
25941 here, but there is not. Stop parsing parameters. */
25942 break;
25944 type_name = cp_parser_objc_typename (parser);
25945 /* New ObjC allows attributes on parameters too. */
25946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
25947 parm_attr = cp_parser_attributes_opt (parser);
25948 identifier = cp_parser_identifier (parser);
25950 params
25951 = chainon (params,
25952 objc_build_keyword_decl (selector,
25953 type_name,
25954 identifier,
25955 parm_attr));
25957 token = cp_lexer_peek_token (parser->lexer);
25960 if (params == NULL_TREE)
25962 cp_parser_error (parser, "objective-c++ method declaration is expected");
25963 return error_mark_node;
25966 /* We allow tail attributes for the method. */
25967 if (token->keyword == RID_ATTRIBUTE)
25969 *attributes = cp_parser_attributes_opt (parser);
25970 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
25971 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25972 return params;
25973 cp_parser_error (parser,
25974 "method attributes must be specified at the end");
25975 return error_mark_node;
25978 if (params == NULL_TREE)
25980 cp_parser_error (parser, "objective-c++ method declaration is expected");
25981 return error_mark_node;
25983 return params;
25986 /* Parse the non-keyword Objective-C params. */
25988 static tree
25989 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
25990 tree* attributes)
25992 tree params = make_node (TREE_LIST);
25993 cp_token *token = cp_lexer_peek_token (parser->lexer);
25994 *ellipsisp = false; /* Initially, assume no ellipsis. */
25996 while (token->type == CPP_COMMA)
25998 cp_parameter_declarator *parmdecl;
25999 tree parm;
26001 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26002 token = cp_lexer_peek_token (parser->lexer);
26004 if (token->type == CPP_ELLIPSIS)
26006 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
26007 *ellipsisp = true;
26008 token = cp_lexer_peek_token (parser->lexer);
26009 break;
26012 /* TODO: parse attributes for tail parameters. */
26013 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
26014 parm = grokdeclarator (parmdecl->declarator,
26015 &parmdecl->decl_specifiers,
26016 PARM, /*initialized=*/0,
26017 /*attrlist=*/NULL);
26019 chainon (params, build_tree_list (NULL_TREE, parm));
26020 token = cp_lexer_peek_token (parser->lexer);
26023 /* We allow tail attributes for the method. */
26024 if (token->keyword == RID_ATTRIBUTE)
26026 if (*attributes == NULL_TREE)
26028 *attributes = cp_parser_attributes_opt (parser);
26029 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26030 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26031 return params;
26033 else
26034 /* We have an error, but parse the attributes, so that we can
26035 carry on. */
26036 *attributes = cp_parser_attributes_opt (parser);
26038 cp_parser_error (parser,
26039 "method attributes must be specified at the end");
26040 return error_mark_node;
26043 return params;
26046 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
26048 static void
26049 cp_parser_objc_interstitial_code (cp_parser* parser)
26051 cp_token *token = cp_lexer_peek_token (parser->lexer);
26053 /* If the next token is `extern' and the following token is a string
26054 literal, then we have a linkage specification. */
26055 if (token->keyword == RID_EXTERN
26056 && cp_parser_is_pure_string_literal
26057 (cp_lexer_peek_nth_token (parser->lexer, 2)))
26058 cp_parser_linkage_specification (parser);
26059 /* Handle #pragma, if any. */
26060 else if (token->type == CPP_PRAGMA)
26061 cp_parser_pragma (parser, pragma_objc_icode);
26062 /* Allow stray semicolons. */
26063 else if (token->type == CPP_SEMICOLON)
26064 cp_lexer_consume_token (parser->lexer);
26065 /* Mark methods as optional or required, when building protocols. */
26066 else if (token->keyword == RID_AT_OPTIONAL)
26068 cp_lexer_consume_token (parser->lexer);
26069 objc_set_method_opt (true);
26071 else if (token->keyword == RID_AT_REQUIRED)
26073 cp_lexer_consume_token (parser->lexer);
26074 objc_set_method_opt (false);
26076 else if (token->keyword == RID_NAMESPACE)
26077 cp_parser_namespace_definition (parser);
26078 /* Other stray characters must generate errors. */
26079 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
26081 cp_lexer_consume_token (parser->lexer);
26082 error ("stray %qs between Objective-C++ methods",
26083 token->type == CPP_OPEN_BRACE ? "{" : "}");
26085 /* Finally, try to parse a block-declaration, or a function-definition. */
26086 else
26087 cp_parser_block_declaration (parser, /*statement_p=*/false);
26090 /* Parse a method signature. */
26092 static tree
26093 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
26095 tree rettype, kwdparms, optparms;
26096 bool ellipsis = false;
26097 bool is_class_method;
26099 is_class_method = cp_parser_objc_method_type (parser);
26100 rettype = cp_parser_objc_typename (parser);
26101 *attributes = NULL_TREE;
26102 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
26103 if (kwdparms == error_mark_node)
26104 return error_mark_node;
26105 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
26106 if (optparms == error_mark_node)
26107 return error_mark_node;
26109 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
26112 static bool
26113 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
26115 tree tattr;
26116 cp_lexer_save_tokens (parser->lexer);
26117 tattr = cp_parser_attributes_opt (parser);
26118 gcc_assert (tattr) ;
26120 /* If the attributes are followed by a method introducer, this is not allowed.
26121 Dump the attributes and flag the situation. */
26122 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
26123 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
26124 return true;
26126 /* Otherwise, the attributes introduce some interstitial code, possibly so
26127 rewind to allow that check. */
26128 cp_lexer_rollback_tokens (parser->lexer);
26129 return false;
26132 /* Parse an Objective-C method prototype list. */
26134 static void
26135 cp_parser_objc_method_prototype_list (cp_parser* parser)
26137 cp_token *token = cp_lexer_peek_token (parser->lexer);
26139 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26141 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26143 tree attributes, sig;
26144 bool is_class_method;
26145 if (token->type == CPP_PLUS)
26146 is_class_method = true;
26147 else
26148 is_class_method = false;
26149 sig = cp_parser_objc_method_signature (parser, &attributes);
26150 if (sig == error_mark_node)
26152 cp_parser_skip_to_end_of_block_or_statement (parser);
26153 token = cp_lexer_peek_token (parser->lexer);
26154 continue;
26156 objc_add_method_declaration (is_class_method, sig, attributes);
26157 cp_parser_consume_semicolon_at_end_of_statement (parser);
26159 else if (token->keyword == RID_AT_PROPERTY)
26160 cp_parser_objc_at_property_declaration (parser);
26161 else if (token->keyword == RID_ATTRIBUTE
26162 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26163 warning_at (cp_lexer_peek_token (parser->lexer)->location,
26164 OPT_Wattributes,
26165 "prefix attributes are ignored for methods");
26166 else
26167 /* Allow for interspersed non-ObjC++ code. */
26168 cp_parser_objc_interstitial_code (parser);
26170 token = cp_lexer_peek_token (parser->lexer);
26173 if (token->type != CPP_EOF)
26174 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26175 else
26176 cp_parser_error (parser, "expected %<@end%>");
26178 objc_finish_interface ();
26181 /* Parse an Objective-C method definition list. */
26183 static void
26184 cp_parser_objc_method_definition_list (cp_parser* parser)
26186 cp_token *token = cp_lexer_peek_token (parser->lexer);
26188 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
26190 tree meth;
26192 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
26194 cp_token *ptk;
26195 tree sig, attribute;
26196 bool is_class_method;
26197 if (token->type == CPP_PLUS)
26198 is_class_method = true;
26199 else
26200 is_class_method = false;
26201 push_deferring_access_checks (dk_deferred);
26202 sig = cp_parser_objc_method_signature (parser, &attribute);
26203 if (sig == error_mark_node)
26205 cp_parser_skip_to_end_of_block_or_statement (parser);
26206 token = cp_lexer_peek_token (parser->lexer);
26207 continue;
26209 objc_start_method_definition (is_class_method, sig, attribute,
26210 NULL_TREE);
26212 /* For historical reasons, we accept an optional semicolon. */
26213 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26214 cp_lexer_consume_token (parser->lexer);
26216 ptk = cp_lexer_peek_token (parser->lexer);
26217 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
26218 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
26220 perform_deferred_access_checks (tf_warning_or_error);
26221 stop_deferring_access_checks ();
26222 meth = cp_parser_function_definition_after_declarator (parser,
26223 false);
26224 pop_deferring_access_checks ();
26225 objc_finish_method_definition (meth);
26228 /* The following case will be removed once @synthesize is
26229 completely implemented. */
26230 else if (token->keyword == RID_AT_PROPERTY)
26231 cp_parser_objc_at_property_declaration (parser);
26232 else if (token->keyword == RID_AT_SYNTHESIZE)
26233 cp_parser_objc_at_synthesize_declaration (parser);
26234 else if (token->keyword == RID_AT_DYNAMIC)
26235 cp_parser_objc_at_dynamic_declaration (parser);
26236 else if (token->keyword == RID_ATTRIBUTE
26237 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
26238 warning_at (token->location, OPT_Wattributes,
26239 "prefix attributes are ignored for methods");
26240 else
26241 /* Allow for interspersed non-ObjC++ code. */
26242 cp_parser_objc_interstitial_code (parser);
26244 token = cp_lexer_peek_token (parser->lexer);
26247 if (token->type != CPP_EOF)
26248 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26249 else
26250 cp_parser_error (parser, "expected %<@end%>");
26252 objc_finish_implementation ();
26255 /* Parse Objective-C ivars. */
26257 static void
26258 cp_parser_objc_class_ivars (cp_parser* parser)
26260 cp_token *token = cp_lexer_peek_token (parser->lexer);
26262 if (token->type != CPP_OPEN_BRACE)
26263 return; /* No ivars specified. */
26265 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
26266 token = cp_lexer_peek_token (parser->lexer);
26268 while (token->type != CPP_CLOSE_BRACE
26269 && token->keyword != RID_AT_END && token->type != CPP_EOF)
26271 cp_decl_specifier_seq declspecs;
26272 int decl_class_or_enum_p;
26273 tree prefix_attributes;
26275 cp_parser_objc_visibility_spec (parser);
26277 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26278 break;
26280 cp_parser_decl_specifier_seq (parser,
26281 CP_PARSER_FLAGS_OPTIONAL,
26282 &declspecs,
26283 &decl_class_or_enum_p);
26285 /* auto, register, static, extern, mutable. */
26286 if (declspecs.storage_class != sc_none)
26288 cp_parser_error (parser, "invalid type for instance variable");
26289 declspecs.storage_class = sc_none;
26292 /* thread_local. */
26293 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26295 cp_parser_error (parser, "invalid type for instance variable");
26296 declspecs.locations[ds_thread] = 0;
26299 /* typedef. */
26300 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26302 cp_parser_error (parser, "invalid type for instance variable");
26303 declspecs.locations[ds_typedef] = 0;
26306 prefix_attributes = declspecs.attributes;
26307 declspecs.attributes = NULL_TREE;
26309 /* Keep going until we hit the `;' at the end of the
26310 declaration. */
26311 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26313 tree width = NULL_TREE, attributes, first_attribute, decl;
26314 cp_declarator *declarator = NULL;
26315 int ctor_dtor_or_conv_p;
26317 /* Check for a (possibly unnamed) bitfield declaration. */
26318 token = cp_lexer_peek_token (parser->lexer);
26319 if (token->type == CPP_COLON)
26320 goto eat_colon;
26322 if (token->type == CPP_NAME
26323 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
26324 == CPP_COLON))
26326 /* Get the name of the bitfield. */
26327 declarator = make_id_declarator (NULL_TREE,
26328 cp_parser_identifier (parser),
26329 sfk_none);
26331 eat_colon:
26332 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26333 /* Get the width of the bitfield. */
26334 width
26335 = cp_parser_constant_expression (parser,
26336 /*allow_non_constant=*/false,
26337 NULL);
26339 else
26341 /* Parse the declarator. */
26342 declarator
26343 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26344 &ctor_dtor_or_conv_p,
26345 /*parenthesized_p=*/NULL,
26346 /*member_p=*/false,
26347 /*friend_p=*/false);
26350 /* Look for attributes that apply to the ivar. */
26351 attributes = cp_parser_attributes_opt (parser);
26352 /* Remember which attributes are prefix attributes and
26353 which are not. */
26354 first_attribute = attributes;
26355 /* Combine the attributes. */
26356 attributes = chainon (prefix_attributes, attributes);
26358 if (width)
26359 /* Create the bitfield declaration. */
26360 decl = grokbitfield (declarator, &declspecs,
26361 width,
26362 attributes);
26363 else
26364 decl = grokfield (declarator, &declspecs,
26365 NULL_TREE, /*init_const_expr_p=*/false,
26366 NULL_TREE, attributes);
26368 /* Add the instance variable. */
26369 if (decl != error_mark_node && decl != NULL_TREE)
26370 objc_add_instance_variable (decl);
26372 /* Reset PREFIX_ATTRIBUTES. */
26373 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26374 attributes = TREE_CHAIN (attributes);
26375 if (attributes)
26376 TREE_CHAIN (attributes) = NULL_TREE;
26378 token = cp_lexer_peek_token (parser->lexer);
26380 if (token->type == CPP_COMMA)
26382 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26383 continue;
26385 break;
26388 cp_parser_consume_semicolon_at_end_of_statement (parser);
26389 token = cp_lexer_peek_token (parser->lexer);
26392 if (token->keyword == RID_AT_END)
26393 cp_parser_error (parser, "expected %<}%>");
26395 /* Do not consume the RID_AT_END, so it will be read again as terminating
26396 the @interface of @implementation. */
26397 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
26398 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
26400 /* For historical reasons, we accept an optional semicolon. */
26401 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26402 cp_lexer_consume_token (parser->lexer);
26405 /* Parse an Objective-C protocol declaration. */
26407 static void
26408 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
26410 tree proto, protorefs;
26411 cp_token *tok;
26413 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
26414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
26416 tok = cp_lexer_peek_token (parser->lexer);
26417 error_at (tok->location, "identifier expected after %<@protocol%>");
26418 cp_parser_consume_semicolon_at_end_of_statement (parser);
26419 return;
26422 /* See if we have a forward declaration or a definition. */
26423 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
26425 /* Try a forward declaration first. */
26426 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
26428 while (true)
26430 tree id;
26432 id = cp_parser_identifier (parser);
26433 if (id == error_mark_node)
26434 break;
26436 objc_declare_protocol (id, attributes);
26438 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
26439 cp_lexer_consume_token (parser->lexer);
26440 else
26441 break;
26443 cp_parser_consume_semicolon_at_end_of_statement (parser);
26446 /* Ok, we got a full-fledged definition (or at least should). */
26447 else
26449 proto = cp_parser_identifier (parser);
26450 protorefs = cp_parser_objc_protocol_refs_opt (parser);
26451 objc_start_protocol (proto, protorefs, attributes);
26452 cp_parser_objc_method_prototype_list (parser);
26456 /* Parse an Objective-C superclass or category. */
26458 static void
26459 cp_parser_objc_superclass_or_category (cp_parser *parser,
26460 bool iface_p,
26461 tree *super,
26462 tree *categ, bool *is_class_extension)
26464 cp_token *next = cp_lexer_peek_token (parser->lexer);
26466 *super = *categ = NULL_TREE;
26467 *is_class_extension = false;
26468 if (next->type == CPP_COLON)
26470 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
26471 *super = cp_parser_identifier (parser);
26473 else if (next->type == CPP_OPEN_PAREN)
26475 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
26477 /* If there is no category name, and this is an @interface, we
26478 have a class extension. */
26479 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26481 *categ = NULL_TREE;
26482 *is_class_extension = true;
26484 else
26485 *categ = cp_parser_identifier (parser);
26487 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26491 /* Parse an Objective-C class interface. */
26493 static void
26494 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
26496 tree name, super, categ, protos;
26497 bool is_class_extension;
26499 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
26500 name = cp_parser_identifier (parser);
26501 if (name == error_mark_node)
26503 /* It's hard to recover because even if valid @interface stuff
26504 is to follow, we can't compile it (or validate it) if we
26505 don't even know which class it refers to. Let's assume this
26506 was a stray '@interface' token in the stream and skip it.
26508 return;
26510 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
26511 &is_class_extension);
26512 protos = cp_parser_objc_protocol_refs_opt (parser);
26514 /* We have either a class or a category on our hands. */
26515 if (categ || is_class_extension)
26516 objc_start_category_interface (name, categ, protos, attributes);
26517 else
26519 objc_start_class_interface (name, super, protos, attributes);
26520 /* Handle instance variable declarations, if any. */
26521 cp_parser_objc_class_ivars (parser);
26522 objc_continue_interface ();
26525 cp_parser_objc_method_prototype_list (parser);
26528 /* Parse an Objective-C class implementation. */
26530 static void
26531 cp_parser_objc_class_implementation (cp_parser* parser)
26533 tree name, super, categ;
26534 bool is_class_extension;
26536 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
26537 name = cp_parser_identifier (parser);
26538 if (name == error_mark_node)
26540 /* It's hard to recover because even if valid @implementation
26541 stuff is to follow, we can't compile it (or validate it) if
26542 we don't even know which class it refers to. Let's assume
26543 this was a stray '@implementation' token in the stream and
26544 skip it.
26546 return;
26548 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
26549 &is_class_extension);
26551 /* We have either a class or a category on our hands. */
26552 if (categ)
26553 objc_start_category_implementation (name, categ);
26554 else
26556 objc_start_class_implementation (name, super);
26557 /* Handle instance variable declarations, if any. */
26558 cp_parser_objc_class_ivars (parser);
26559 objc_continue_implementation ();
26562 cp_parser_objc_method_definition_list (parser);
26565 /* Consume the @end token and finish off the implementation. */
26567 static void
26568 cp_parser_objc_end_implementation (cp_parser* parser)
26570 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
26571 objc_finish_implementation ();
26574 /* Parse an Objective-C declaration. */
26576 static void
26577 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
26579 /* Try to figure out what kind of declaration is present. */
26580 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26582 if (attributes)
26583 switch (kwd->keyword)
26585 case RID_AT_ALIAS:
26586 case RID_AT_CLASS:
26587 case RID_AT_END:
26588 error_at (kwd->location, "attributes may not be specified before"
26589 " the %<@%D%> Objective-C++ keyword",
26590 kwd->u.value);
26591 attributes = NULL;
26592 break;
26593 case RID_AT_IMPLEMENTATION:
26594 warning_at (kwd->location, OPT_Wattributes,
26595 "prefix attributes are ignored before %<@%D%>",
26596 kwd->u.value);
26597 attributes = NULL;
26598 default:
26599 break;
26602 switch (kwd->keyword)
26604 case RID_AT_ALIAS:
26605 cp_parser_objc_alias_declaration (parser);
26606 break;
26607 case RID_AT_CLASS:
26608 cp_parser_objc_class_declaration (parser);
26609 break;
26610 case RID_AT_PROTOCOL:
26611 cp_parser_objc_protocol_declaration (parser, attributes);
26612 break;
26613 case RID_AT_INTERFACE:
26614 cp_parser_objc_class_interface (parser, attributes);
26615 break;
26616 case RID_AT_IMPLEMENTATION:
26617 cp_parser_objc_class_implementation (parser);
26618 break;
26619 case RID_AT_END:
26620 cp_parser_objc_end_implementation (parser);
26621 break;
26622 default:
26623 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26624 kwd->u.value);
26625 cp_parser_skip_to_end_of_block_or_statement (parser);
26629 /* Parse an Objective-C try-catch-finally statement.
26631 objc-try-catch-finally-stmt:
26632 @try compound-statement objc-catch-clause-seq [opt]
26633 objc-finally-clause [opt]
26635 objc-catch-clause-seq:
26636 objc-catch-clause objc-catch-clause-seq [opt]
26638 objc-catch-clause:
26639 @catch ( objc-exception-declaration ) compound-statement
26641 objc-finally-clause:
26642 @finally compound-statement
26644 objc-exception-declaration:
26645 parameter-declaration
26646 '...'
26648 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
26650 Returns NULL_TREE.
26652 PS: This function is identical to c_parser_objc_try_catch_finally_statement
26653 for C. Keep them in sync. */
26655 static tree
26656 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
26658 location_t location;
26659 tree stmt;
26661 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
26662 location = cp_lexer_peek_token (parser->lexer)->location;
26663 objc_maybe_warn_exceptions (location);
26664 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
26665 node, lest it get absorbed into the surrounding block. */
26666 stmt = push_stmt_list ();
26667 cp_parser_compound_statement (parser, NULL, false, false);
26668 objc_begin_try_stmt (location, pop_stmt_list (stmt));
26670 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
26672 cp_parameter_declarator *parm;
26673 tree parameter_declaration = error_mark_node;
26674 bool seen_open_paren = false;
26676 cp_lexer_consume_token (parser->lexer);
26677 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26678 seen_open_paren = true;
26679 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26681 /* We have "@catch (...)" (where the '...' are literally
26682 what is in the code). Skip the '...'.
26683 parameter_declaration is set to NULL_TREE, and
26684 objc_being_catch_clauses() knows that that means
26685 '...'. */
26686 cp_lexer_consume_token (parser->lexer);
26687 parameter_declaration = NULL_TREE;
26689 else
26691 /* We have "@catch (NSException *exception)" or something
26692 like that. Parse the parameter declaration. */
26693 parm = cp_parser_parameter_declaration (parser, false, NULL);
26694 if (parm == NULL)
26695 parameter_declaration = error_mark_node;
26696 else
26697 parameter_declaration = grokdeclarator (parm->declarator,
26698 &parm->decl_specifiers,
26699 PARM, /*initialized=*/0,
26700 /*attrlist=*/NULL);
26702 if (seen_open_paren)
26703 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26704 else
26706 /* If there was no open parenthesis, we are recovering from
26707 an error, and we are trying to figure out what mistake
26708 the user has made. */
26710 /* If there is an immediate closing parenthesis, the user
26711 probably forgot the opening one (ie, they typed "@catch
26712 NSException *e)". Parse the closing parenthesis and keep
26713 going. */
26714 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
26715 cp_lexer_consume_token (parser->lexer);
26717 /* If these is no immediate closing parenthesis, the user
26718 probably doesn't know that parenthesis are required at
26719 all (ie, they typed "@catch NSException *e"). So, just
26720 forget about the closing parenthesis and keep going. */
26722 objc_begin_catch_clause (parameter_declaration);
26723 cp_parser_compound_statement (parser, NULL, false, false);
26724 objc_finish_catch_clause ();
26726 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
26728 cp_lexer_consume_token (parser->lexer);
26729 location = cp_lexer_peek_token (parser->lexer)->location;
26730 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
26731 node, lest it get absorbed into the surrounding block. */
26732 stmt = push_stmt_list ();
26733 cp_parser_compound_statement (parser, NULL, false, false);
26734 objc_build_finally_clause (location, pop_stmt_list (stmt));
26737 return objc_finish_try_stmt ();
26740 /* Parse an Objective-C synchronized statement.
26742 objc-synchronized-stmt:
26743 @synchronized ( expression ) compound-statement
26745 Returns NULL_TREE. */
26747 static tree
26748 cp_parser_objc_synchronized_statement (cp_parser *parser)
26750 location_t location;
26751 tree lock, stmt;
26753 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
26755 location = cp_lexer_peek_token (parser->lexer)->location;
26756 objc_maybe_warn_exceptions (location);
26757 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26758 lock = cp_parser_expression (parser);
26759 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26761 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
26762 node, lest it get absorbed into the surrounding block. */
26763 stmt = push_stmt_list ();
26764 cp_parser_compound_statement (parser, NULL, false, false);
26766 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
26769 /* Parse an Objective-C throw statement.
26771 objc-throw-stmt:
26772 @throw assignment-expression [opt] ;
26774 Returns a constructed '@throw' statement. */
26776 static tree
26777 cp_parser_objc_throw_statement (cp_parser *parser)
26779 tree expr = NULL_TREE;
26780 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26782 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
26784 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26785 expr = cp_parser_expression (parser);
26787 cp_parser_consume_semicolon_at_end_of_statement (parser);
26789 return objc_build_throw_stmt (loc, expr);
26792 /* Parse an Objective-C statement. */
26794 static tree
26795 cp_parser_objc_statement (cp_parser * parser)
26797 /* Try to figure out what kind of declaration is present. */
26798 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
26800 switch (kwd->keyword)
26802 case RID_AT_TRY:
26803 return cp_parser_objc_try_catch_finally_statement (parser);
26804 case RID_AT_SYNCHRONIZED:
26805 return cp_parser_objc_synchronized_statement (parser);
26806 case RID_AT_THROW:
26807 return cp_parser_objc_throw_statement (parser);
26808 default:
26809 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
26810 kwd->u.value);
26811 cp_parser_skip_to_end_of_block_or_statement (parser);
26814 return error_mark_node;
26817 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
26818 look ahead to see if an objc keyword follows the attributes. This
26819 is to detect the use of prefix attributes on ObjC @interface and
26820 @protocol. */
26822 static bool
26823 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
26825 cp_lexer_save_tokens (parser->lexer);
26826 *attrib = cp_parser_attributes_opt (parser);
26827 gcc_assert (*attrib);
26828 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
26830 cp_lexer_commit_tokens (parser->lexer);
26831 return true;
26833 cp_lexer_rollback_tokens (parser->lexer);
26834 return false;
26837 /* This routine is a minimal replacement for
26838 c_parser_struct_declaration () used when parsing the list of
26839 types/names or ObjC++ properties. For example, when parsing the
26840 code
26842 @property (readonly) int a, b, c;
26844 this function is responsible for parsing "int a, int b, int c" and
26845 returning the declarations as CHAIN of DECLs.
26847 TODO: Share this code with cp_parser_objc_class_ivars. It's very
26848 similar parsing. */
26849 static tree
26850 cp_parser_objc_struct_declaration (cp_parser *parser)
26852 tree decls = NULL_TREE;
26853 cp_decl_specifier_seq declspecs;
26854 int decl_class_or_enum_p;
26855 tree prefix_attributes;
26857 cp_parser_decl_specifier_seq (parser,
26858 CP_PARSER_FLAGS_NONE,
26859 &declspecs,
26860 &decl_class_or_enum_p);
26862 if (declspecs.type == error_mark_node)
26863 return error_mark_node;
26865 /* auto, register, static, extern, mutable. */
26866 if (declspecs.storage_class != sc_none)
26868 cp_parser_error (parser, "invalid type for property");
26869 declspecs.storage_class = sc_none;
26872 /* thread_local. */
26873 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
26875 cp_parser_error (parser, "invalid type for property");
26876 declspecs.locations[ds_thread] = 0;
26879 /* typedef. */
26880 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
26882 cp_parser_error (parser, "invalid type for property");
26883 declspecs.locations[ds_typedef] = 0;
26886 prefix_attributes = declspecs.attributes;
26887 declspecs.attributes = NULL_TREE;
26889 /* Keep going until we hit the `;' at the end of the declaration. */
26890 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26892 tree attributes, first_attribute, decl;
26893 cp_declarator *declarator;
26894 cp_token *token;
26896 /* Parse the declarator. */
26897 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
26898 NULL, NULL, false, false);
26900 /* Look for attributes that apply to the ivar. */
26901 attributes = cp_parser_attributes_opt (parser);
26902 /* Remember which attributes are prefix attributes and
26903 which are not. */
26904 first_attribute = attributes;
26905 /* Combine the attributes. */
26906 attributes = chainon (prefix_attributes, attributes);
26908 decl = grokfield (declarator, &declspecs,
26909 NULL_TREE, /*init_const_expr_p=*/false,
26910 NULL_TREE, attributes);
26912 if (decl == error_mark_node || decl == NULL_TREE)
26913 return error_mark_node;
26915 /* Reset PREFIX_ATTRIBUTES. */
26916 while (attributes && TREE_CHAIN (attributes) != first_attribute)
26917 attributes = TREE_CHAIN (attributes);
26918 if (attributes)
26919 TREE_CHAIN (attributes) = NULL_TREE;
26921 DECL_CHAIN (decl) = decls;
26922 decls = decl;
26924 token = cp_lexer_peek_token (parser->lexer);
26925 if (token->type == CPP_COMMA)
26927 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
26928 continue;
26930 else
26931 break;
26933 return decls;
26936 /* Parse an Objective-C @property declaration. The syntax is:
26938 objc-property-declaration:
26939 '@property' objc-property-attributes[opt] struct-declaration ;
26941 objc-property-attributes:
26942 '(' objc-property-attribute-list ')'
26944 objc-property-attribute-list:
26945 objc-property-attribute
26946 objc-property-attribute-list, objc-property-attribute
26948 objc-property-attribute
26949 'getter' = identifier
26950 'setter' = identifier
26951 'readonly'
26952 'readwrite'
26953 'assign'
26954 'retain'
26955 'copy'
26956 'nonatomic'
26958 For example:
26959 @property NSString *name;
26960 @property (readonly) id object;
26961 @property (retain, nonatomic, getter=getTheName) id name;
26962 @property int a, b, c;
26964 PS: This function is identical to
26965 c_parser_objc_at_property_declaration for C. Keep them in sync. */
26966 static void
26967 cp_parser_objc_at_property_declaration (cp_parser *parser)
26969 /* The following variables hold the attributes of the properties as
26970 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
26971 seen. When we see an attribute, we set them to 'true' (if they
26972 are boolean properties) or to the identifier (if they have an
26973 argument, ie, for getter and setter). Note that here we only
26974 parse the list of attributes, check the syntax and accumulate the
26975 attributes that we find. objc_add_property_declaration() will
26976 then process the information. */
26977 bool property_assign = false;
26978 bool property_copy = false;
26979 tree property_getter_ident = NULL_TREE;
26980 bool property_nonatomic = false;
26981 bool property_readonly = false;
26982 bool property_readwrite = false;
26983 bool property_retain = false;
26984 tree property_setter_ident = NULL_TREE;
26986 /* 'properties' is the list of properties that we read. Usually a
26987 single one, but maybe more (eg, in "@property int a, b, c;" there
26988 are three). */
26989 tree properties;
26990 location_t loc;
26992 loc = cp_lexer_peek_token (parser->lexer)->location;
26994 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
26996 /* Parse the optional attribute list... */
26997 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26999 /* Eat the '('. */
27000 cp_lexer_consume_token (parser->lexer);
27002 while (true)
27004 bool syntax_error = false;
27005 cp_token *token = cp_lexer_peek_token (parser->lexer);
27006 enum rid keyword;
27008 if (token->type != CPP_NAME)
27010 cp_parser_error (parser, "expected identifier");
27011 break;
27013 keyword = C_RID_CODE (token->u.value);
27014 cp_lexer_consume_token (parser->lexer);
27015 switch (keyword)
27017 case RID_ASSIGN: property_assign = true; break;
27018 case RID_COPY: property_copy = true; break;
27019 case RID_NONATOMIC: property_nonatomic = true; break;
27020 case RID_READONLY: property_readonly = true; break;
27021 case RID_READWRITE: property_readwrite = true; break;
27022 case RID_RETAIN: property_retain = true; break;
27024 case RID_GETTER:
27025 case RID_SETTER:
27026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
27028 if (keyword == RID_GETTER)
27029 cp_parser_error (parser,
27030 "missing %<=%> (after %<getter%> attribute)");
27031 else
27032 cp_parser_error (parser,
27033 "missing %<=%> (after %<setter%> attribute)");
27034 syntax_error = true;
27035 break;
27037 cp_lexer_consume_token (parser->lexer); /* eat the = */
27038 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
27040 cp_parser_error (parser, "expected identifier");
27041 syntax_error = true;
27042 break;
27044 if (keyword == RID_SETTER)
27046 if (property_setter_ident != NULL_TREE)
27048 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
27049 cp_lexer_consume_token (parser->lexer);
27051 else
27052 property_setter_ident = cp_parser_objc_selector (parser);
27053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27054 cp_parser_error (parser, "setter name must terminate with %<:%>");
27055 else
27056 cp_lexer_consume_token (parser->lexer);
27058 else
27060 if (property_getter_ident != NULL_TREE)
27062 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
27063 cp_lexer_consume_token (parser->lexer);
27065 else
27066 property_getter_ident = cp_parser_objc_selector (parser);
27068 break;
27069 default:
27070 cp_parser_error (parser, "unknown property attribute");
27071 syntax_error = true;
27072 break;
27075 if (syntax_error)
27076 break;
27078 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27079 cp_lexer_consume_token (parser->lexer);
27080 else
27081 break;
27084 /* FIXME: "@property (setter, assign);" will generate a spurious
27085 "error: expected ‘)’ before ‘,’ token". This is because
27086 cp_parser_require, unlike the C counterpart, will produce an
27087 error even if we are in error recovery. */
27088 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27090 cp_parser_skip_to_closing_parenthesis (parser,
27091 /*recovering=*/true,
27092 /*or_comma=*/false,
27093 /*consume_paren=*/true);
27097 /* ... and the property declaration(s). */
27098 properties = cp_parser_objc_struct_declaration (parser);
27100 if (properties == error_mark_node)
27102 cp_parser_skip_to_end_of_statement (parser);
27103 /* If the next token is now a `;', consume it. */
27104 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27105 cp_lexer_consume_token (parser->lexer);
27106 return;
27109 if (properties == NULL_TREE)
27110 cp_parser_error (parser, "expected identifier");
27111 else
27113 /* Comma-separated properties are chained together in
27114 reverse order; add them one by one. */
27115 properties = nreverse (properties);
27117 for (; properties; properties = TREE_CHAIN (properties))
27118 objc_add_property_declaration (loc, copy_node (properties),
27119 property_readonly, property_readwrite,
27120 property_assign, property_retain,
27121 property_copy, property_nonatomic,
27122 property_getter_ident, property_setter_ident);
27125 cp_parser_consume_semicolon_at_end_of_statement (parser);
27128 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
27130 objc-synthesize-declaration:
27131 @synthesize objc-synthesize-identifier-list ;
27133 objc-synthesize-identifier-list:
27134 objc-synthesize-identifier
27135 objc-synthesize-identifier-list, objc-synthesize-identifier
27137 objc-synthesize-identifier
27138 identifier
27139 identifier = identifier
27141 For example:
27142 @synthesize MyProperty;
27143 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
27145 PS: This function is identical to c_parser_objc_at_synthesize_declaration
27146 for C. Keep them in sync.
27148 static void
27149 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
27151 tree list = NULL_TREE;
27152 location_t loc;
27153 loc = cp_lexer_peek_token (parser->lexer)->location;
27155 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
27156 while (true)
27158 tree property, ivar;
27159 property = cp_parser_identifier (parser);
27160 if (property == error_mark_node)
27162 cp_parser_consume_semicolon_at_end_of_statement (parser);
27163 return;
27165 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27167 cp_lexer_consume_token (parser->lexer);
27168 ivar = cp_parser_identifier (parser);
27169 if (ivar == error_mark_node)
27171 cp_parser_consume_semicolon_at_end_of_statement (parser);
27172 return;
27175 else
27176 ivar = NULL_TREE;
27177 list = chainon (list, build_tree_list (ivar, property));
27178 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27179 cp_lexer_consume_token (parser->lexer);
27180 else
27181 break;
27183 cp_parser_consume_semicolon_at_end_of_statement (parser);
27184 objc_add_synthesize_declaration (loc, list);
27187 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
27189 objc-dynamic-declaration:
27190 @dynamic identifier-list ;
27192 For example:
27193 @dynamic MyProperty;
27194 @dynamic MyProperty, AnotherProperty;
27196 PS: This function is identical to c_parser_objc_at_dynamic_declaration
27197 for C. Keep them in sync.
27199 static void
27200 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
27202 tree list = NULL_TREE;
27203 location_t loc;
27204 loc = cp_lexer_peek_token (parser->lexer)->location;
27206 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
27207 while (true)
27209 tree property;
27210 property = cp_parser_identifier (parser);
27211 if (property == error_mark_node)
27213 cp_parser_consume_semicolon_at_end_of_statement (parser);
27214 return;
27216 list = chainon (list, build_tree_list (NULL, property));
27217 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27218 cp_lexer_consume_token (parser->lexer);
27219 else
27220 break;
27222 cp_parser_consume_semicolon_at_end_of_statement (parser);
27223 objc_add_dynamic_declaration (loc, list);
27227 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
27229 /* Returns name of the next clause.
27230 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
27231 the token is not consumed. Otherwise appropriate pragma_omp_clause is
27232 returned and the token is consumed. */
27234 static pragma_omp_clause
27235 cp_parser_omp_clause_name (cp_parser *parser)
27237 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
27239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
27240 result = PRAGMA_OMP_CLAUSE_IF;
27241 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
27242 result = PRAGMA_OMP_CLAUSE_DEFAULT;
27243 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
27244 result = PRAGMA_OMP_CLAUSE_PRIVATE;
27245 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
27246 result = PRAGMA_OMP_CLAUSE_FOR;
27247 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27249 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27250 const char *p = IDENTIFIER_POINTER (id);
27252 switch (p[0])
27254 case 'a':
27255 if (!strcmp ("aligned", p))
27256 result = PRAGMA_OMP_CLAUSE_ALIGNED;
27257 break;
27258 case 'c':
27259 if (!strcmp ("collapse", p))
27260 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
27261 else if (!strcmp ("copyin", p))
27262 result = PRAGMA_OMP_CLAUSE_COPYIN;
27263 else if (!strcmp ("copyprivate", p))
27264 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
27265 break;
27266 case 'd':
27267 if (!strcmp ("depend", p))
27268 result = PRAGMA_OMP_CLAUSE_DEPEND;
27269 else if (!strcmp ("device", p))
27270 result = PRAGMA_OMP_CLAUSE_DEVICE;
27271 else if (!strcmp ("dist_schedule", p))
27272 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
27273 break;
27274 case 'f':
27275 if (!strcmp ("final", p))
27276 result = PRAGMA_OMP_CLAUSE_FINAL;
27277 else if (!strcmp ("firstprivate", p))
27278 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
27279 else if (!strcmp ("from", p))
27280 result = PRAGMA_OMP_CLAUSE_FROM;
27281 break;
27282 case 'i':
27283 if (!strcmp ("inbranch", p))
27284 result = PRAGMA_OMP_CLAUSE_INBRANCH;
27285 break;
27286 case 'l':
27287 if (!strcmp ("lastprivate", p))
27288 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
27289 else if (!strcmp ("linear", p))
27290 result = PRAGMA_OMP_CLAUSE_LINEAR;
27291 break;
27292 case 'm':
27293 if (!strcmp ("map", p))
27294 result = PRAGMA_OMP_CLAUSE_MAP;
27295 else if (!strcmp ("mergeable", p))
27296 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
27297 else if (flag_cilkplus && !strcmp ("mask", p))
27298 result = PRAGMA_CILK_CLAUSE_MASK;
27299 break;
27300 case 'n':
27301 if (!strcmp ("notinbranch", p))
27302 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
27303 else if (!strcmp ("nowait", p))
27304 result = PRAGMA_OMP_CLAUSE_NOWAIT;
27305 else if (flag_cilkplus && !strcmp ("nomask", p))
27306 result = PRAGMA_CILK_CLAUSE_NOMASK;
27307 else if (!strcmp ("num_teams", p))
27308 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
27309 else if (!strcmp ("num_threads", p))
27310 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
27311 break;
27312 case 'o':
27313 if (!strcmp ("ordered", p))
27314 result = PRAGMA_OMP_CLAUSE_ORDERED;
27315 break;
27316 case 'p':
27317 if (!strcmp ("parallel", p))
27318 result = PRAGMA_OMP_CLAUSE_PARALLEL;
27319 else if (!strcmp ("proc_bind", p))
27320 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
27321 break;
27322 case 'r':
27323 if (!strcmp ("reduction", p))
27324 result = PRAGMA_OMP_CLAUSE_REDUCTION;
27325 break;
27326 case 's':
27327 if (!strcmp ("safelen", p))
27328 result = PRAGMA_OMP_CLAUSE_SAFELEN;
27329 else if (!strcmp ("schedule", p))
27330 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
27331 else if (!strcmp ("sections", p))
27332 result = PRAGMA_OMP_CLAUSE_SECTIONS;
27333 else if (!strcmp ("shared", p))
27334 result = PRAGMA_OMP_CLAUSE_SHARED;
27335 else if (!strcmp ("simdlen", p))
27336 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
27337 break;
27338 case 't':
27339 if (!strcmp ("taskgroup", p))
27340 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
27341 else if (!strcmp ("thread_limit", p))
27342 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
27343 else if (!strcmp ("to", p))
27344 result = PRAGMA_OMP_CLAUSE_TO;
27345 break;
27346 case 'u':
27347 if (!strcmp ("uniform", p))
27348 result = PRAGMA_OMP_CLAUSE_UNIFORM;
27349 else if (!strcmp ("untied", p))
27350 result = PRAGMA_OMP_CLAUSE_UNTIED;
27351 break;
27352 case 'v':
27353 if (flag_cilkplus && !strcmp ("vectorlength", p))
27354 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
27355 break;
27359 if (result != PRAGMA_OMP_CLAUSE_NONE)
27360 cp_lexer_consume_token (parser->lexer);
27362 return result;
27365 /* Validate that a clause of the given type does not already exist. */
27367 static void
27368 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
27369 const char *name, location_t location)
27371 tree c;
27373 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
27374 if (OMP_CLAUSE_CODE (c) == code)
27376 error_at (location, "too many %qs clauses", name);
27377 break;
27381 /* OpenMP 2.5:
27382 variable-list:
27383 identifier
27384 variable-list , identifier
27386 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
27387 colon). An opening parenthesis will have been consumed by the caller.
27389 If KIND is nonzero, create the appropriate node and install the decl
27390 in OMP_CLAUSE_DECL and add the node to the head of the list.
27392 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
27393 return the list created.
27395 COLON can be NULL if only closing parenthesis should end the list,
27396 or pointer to bool which will receive false if the list is terminated
27397 by closing parenthesis or true if the list is terminated by colon. */
27399 static tree
27400 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
27401 tree list, bool *colon)
27403 cp_token *token;
27404 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27405 if (colon)
27407 parser->colon_corrects_to_scope_p = false;
27408 *colon = false;
27410 while (1)
27412 tree name, decl;
27414 token = cp_lexer_peek_token (parser->lexer);
27415 name = cp_parser_id_expression (parser, /*template_p=*/false,
27416 /*check_dependency_p=*/true,
27417 /*template_p=*/NULL,
27418 /*declarator_p=*/false,
27419 /*optional_p=*/false);
27420 if (name == error_mark_node)
27421 goto skip_comma;
27423 decl = cp_parser_lookup_name_simple (parser, name, token->location);
27424 if (decl == error_mark_node)
27425 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
27426 token->location);
27427 else if (kind != 0)
27429 switch (kind)
27431 case OMP_CLAUSE_MAP:
27432 case OMP_CLAUSE_FROM:
27433 case OMP_CLAUSE_TO:
27434 case OMP_CLAUSE_DEPEND:
27435 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
27437 tree low_bound = NULL_TREE, length = NULL_TREE;
27439 parser->colon_corrects_to_scope_p = false;
27440 cp_lexer_consume_token (parser->lexer);
27441 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27442 low_bound = cp_parser_expression (parser);
27443 if (!colon)
27444 parser->colon_corrects_to_scope_p
27445 = saved_colon_corrects_to_scope_p;
27446 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
27447 length = integer_one_node;
27448 else
27450 /* Look for `:'. */
27451 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27452 goto skip_comma;
27453 if (!cp_lexer_next_token_is (parser->lexer,
27454 CPP_CLOSE_SQUARE))
27455 length = cp_parser_expression (parser);
27457 /* Look for the closing `]'. */
27458 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
27459 RT_CLOSE_SQUARE))
27460 goto skip_comma;
27461 decl = tree_cons (low_bound, length, decl);
27463 break;
27464 default:
27465 break;
27468 tree u = build_omp_clause (token->location, kind);
27469 OMP_CLAUSE_DECL (u) = decl;
27470 OMP_CLAUSE_CHAIN (u) = list;
27471 list = u;
27473 else
27474 list = tree_cons (decl, NULL_TREE, list);
27476 get_comma:
27477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
27478 break;
27479 cp_lexer_consume_token (parser->lexer);
27482 if (colon)
27483 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27485 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27487 *colon = true;
27488 cp_parser_require (parser, CPP_COLON, RT_COLON);
27489 return list;
27492 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27494 int ending;
27496 /* Try to resync to an unnested comma. Copied from
27497 cp_parser_parenthesized_expression_list. */
27498 skip_comma:
27499 if (colon)
27500 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27501 ending = cp_parser_skip_to_closing_parenthesis (parser,
27502 /*recovering=*/true,
27503 /*or_comma=*/true,
27504 /*consume_paren=*/true);
27505 if (ending < 0)
27506 goto get_comma;
27509 return list;
27512 /* Similarly, but expect leading and trailing parenthesis. This is a very
27513 common case for omp clauses. */
27515 static tree
27516 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
27518 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27519 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
27520 return list;
27523 /* OpenMP 3.0:
27524 collapse ( constant-expression ) */
27526 static tree
27527 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
27529 tree c, num;
27530 location_t loc;
27531 HOST_WIDE_INT n;
27533 loc = cp_lexer_peek_token (parser->lexer)->location;
27534 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27535 return list;
27537 num = cp_parser_constant_expression (parser, false, NULL);
27539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27540 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27541 /*or_comma=*/false,
27542 /*consume_paren=*/true);
27544 if (num == error_mark_node)
27545 return list;
27546 num = fold_non_dependent_expr (num);
27547 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
27548 || !tree_fits_shwi_p (num)
27549 || (n = tree_to_shwi (num)) <= 0
27550 || (int) n != n)
27552 error_at (loc, "collapse argument needs positive constant integer expression");
27553 return list;
27556 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
27557 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
27558 OMP_CLAUSE_CHAIN (c) = list;
27559 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
27561 return c;
27564 /* OpenMP 2.5:
27565 default ( shared | none ) */
27567 static tree
27568 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
27570 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
27571 tree c;
27573 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27574 return list;
27575 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27577 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27578 const char *p = IDENTIFIER_POINTER (id);
27580 switch (p[0])
27582 case 'n':
27583 if (strcmp ("none", p) != 0)
27584 goto invalid_kind;
27585 kind = OMP_CLAUSE_DEFAULT_NONE;
27586 break;
27588 case 's':
27589 if (strcmp ("shared", p) != 0)
27590 goto invalid_kind;
27591 kind = OMP_CLAUSE_DEFAULT_SHARED;
27592 break;
27594 default:
27595 goto invalid_kind;
27598 cp_lexer_consume_token (parser->lexer);
27600 else
27602 invalid_kind:
27603 cp_parser_error (parser, "expected %<none%> or %<shared%>");
27606 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27607 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27608 /*or_comma=*/false,
27609 /*consume_paren=*/true);
27611 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
27612 return list;
27614 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
27615 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
27616 OMP_CLAUSE_CHAIN (c) = list;
27617 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
27619 return c;
27622 /* OpenMP 3.1:
27623 final ( expression ) */
27625 static tree
27626 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
27628 tree t, c;
27630 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27631 return list;
27633 t = cp_parser_condition (parser);
27635 if (t == error_mark_node
27636 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27637 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27638 /*or_comma=*/false,
27639 /*consume_paren=*/true);
27641 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
27643 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
27644 OMP_CLAUSE_FINAL_EXPR (c) = t;
27645 OMP_CLAUSE_CHAIN (c) = list;
27647 return c;
27650 /* OpenMP 2.5:
27651 if ( expression ) */
27653 static tree
27654 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
27656 tree t, c;
27658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27659 return list;
27661 t = cp_parser_condition (parser);
27663 if (t == error_mark_node
27664 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27665 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27666 /*or_comma=*/false,
27667 /*consume_paren=*/true);
27669 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
27671 c = build_omp_clause (location, OMP_CLAUSE_IF);
27672 OMP_CLAUSE_IF_EXPR (c) = t;
27673 OMP_CLAUSE_CHAIN (c) = list;
27675 return c;
27678 /* OpenMP 3.1:
27679 mergeable */
27681 static tree
27682 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
27683 tree list, location_t location)
27685 tree c;
27687 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
27688 location);
27690 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
27691 OMP_CLAUSE_CHAIN (c) = list;
27692 return c;
27695 /* OpenMP 2.5:
27696 nowait */
27698 static tree
27699 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
27700 tree list, location_t location)
27702 tree c;
27704 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
27706 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
27707 OMP_CLAUSE_CHAIN (c) = list;
27708 return c;
27711 /* OpenMP 2.5:
27712 num_threads ( expression ) */
27714 static tree
27715 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
27716 location_t location)
27718 tree t, c;
27720 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27721 return list;
27723 t = cp_parser_expression (parser);
27725 if (t == error_mark_node
27726 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27727 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27728 /*or_comma=*/false,
27729 /*consume_paren=*/true);
27731 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
27732 "num_threads", location);
27734 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
27735 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
27736 OMP_CLAUSE_CHAIN (c) = list;
27738 return c;
27741 /* OpenMP 2.5:
27742 ordered */
27744 static tree
27745 cp_parser_omp_clause_ordered (cp_parser * /*parser*/,
27746 tree list, location_t location)
27748 tree c;
27750 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
27751 "ordered", location);
27753 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
27754 OMP_CLAUSE_CHAIN (c) = list;
27755 return c;
27758 /* OpenMP 2.5:
27759 reduction ( reduction-operator : variable-list )
27761 reduction-operator:
27762 One of: + * - & ^ | && ||
27764 OpenMP 3.1:
27766 reduction-operator:
27767 One of: + * - & ^ | && || min max
27769 OpenMP 4.0:
27771 reduction-operator:
27772 One of: + * - & ^ | && ||
27773 id-expression */
27775 static tree
27776 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
27778 enum tree_code code = ERROR_MARK;
27779 tree nlist, c, id = NULL_TREE;
27781 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27782 return list;
27784 switch (cp_lexer_peek_token (parser->lexer)->type)
27786 case CPP_PLUS: code = PLUS_EXPR; break;
27787 case CPP_MULT: code = MULT_EXPR; break;
27788 case CPP_MINUS: code = MINUS_EXPR; break;
27789 case CPP_AND: code = BIT_AND_EXPR; break;
27790 case CPP_XOR: code = BIT_XOR_EXPR; break;
27791 case CPP_OR: code = BIT_IOR_EXPR; break;
27792 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
27793 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
27794 default: break;
27797 if (code != ERROR_MARK)
27798 cp_lexer_consume_token (parser->lexer);
27799 else
27801 bool saved_colon_corrects_to_scope_p;
27802 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
27803 parser->colon_corrects_to_scope_p = false;
27804 id = cp_parser_id_expression (parser, /*template_p=*/false,
27805 /*check_dependency_p=*/true,
27806 /*template_p=*/NULL,
27807 /*declarator_p=*/false,
27808 /*optional_p=*/false);
27809 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27810 if (identifier_p (id))
27812 const char *p = IDENTIFIER_POINTER (id);
27814 if (strcmp (p, "min") == 0)
27815 code = MIN_EXPR;
27816 else if (strcmp (p, "max") == 0)
27817 code = MAX_EXPR;
27818 else if (id == ansi_opname (PLUS_EXPR))
27819 code = PLUS_EXPR;
27820 else if (id == ansi_opname (MULT_EXPR))
27821 code = MULT_EXPR;
27822 else if (id == ansi_opname (MINUS_EXPR))
27823 code = MINUS_EXPR;
27824 else if (id == ansi_opname (BIT_AND_EXPR))
27825 code = BIT_AND_EXPR;
27826 else if (id == ansi_opname (BIT_IOR_EXPR))
27827 code = BIT_IOR_EXPR;
27828 else if (id == ansi_opname (BIT_XOR_EXPR))
27829 code = BIT_XOR_EXPR;
27830 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
27831 code = TRUTH_ANDIF_EXPR;
27832 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
27833 code = TRUTH_ORIF_EXPR;
27834 id = omp_reduction_id (code, id, NULL_TREE);
27835 tree scope = parser->scope;
27836 if (scope)
27837 id = build_qualified_name (NULL_TREE, scope, id, false);
27838 parser->scope = NULL_TREE;
27839 parser->qualifying_scope = NULL_TREE;
27840 parser->object_scope = NULL_TREE;
27842 else
27844 error ("invalid reduction-identifier");
27845 resync_fail:
27846 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27847 /*or_comma=*/false,
27848 /*consume_paren=*/true);
27849 return list;
27853 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
27854 goto resync_fail;
27856 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
27857 NULL);
27858 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
27860 OMP_CLAUSE_REDUCTION_CODE (c) = code;
27861 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
27864 return nlist;
27867 /* OpenMP 2.5:
27868 schedule ( schedule-kind )
27869 schedule ( schedule-kind , expression )
27871 schedule-kind:
27872 static | dynamic | guided | runtime | auto */
27874 static tree
27875 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
27877 tree c, t;
27879 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
27880 return list;
27882 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
27884 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
27886 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
27887 const char *p = IDENTIFIER_POINTER (id);
27889 switch (p[0])
27891 case 'd':
27892 if (strcmp ("dynamic", p) != 0)
27893 goto invalid_kind;
27894 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
27895 break;
27897 case 'g':
27898 if (strcmp ("guided", p) != 0)
27899 goto invalid_kind;
27900 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
27901 break;
27903 case 'r':
27904 if (strcmp ("runtime", p) != 0)
27905 goto invalid_kind;
27906 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
27907 break;
27909 default:
27910 goto invalid_kind;
27913 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
27914 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
27915 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
27916 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
27917 else
27918 goto invalid_kind;
27919 cp_lexer_consume_token (parser->lexer);
27921 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27923 cp_token *token;
27924 cp_lexer_consume_token (parser->lexer);
27926 token = cp_lexer_peek_token (parser->lexer);
27927 t = cp_parser_assignment_expression (parser, false, NULL);
27929 if (t == error_mark_node)
27930 goto resync_fail;
27931 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
27932 error_at (token->location, "schedule %<runtime%> does not take "
27933 "a %<chunk_size%> parameter");
27934 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
27935 error_at (token->location, "schedule %<auto%> does not take "
27936 "a %<chunk_size%> parameter");
27937 else
27938 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
27940 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
27941 goto resync_fail;
27943 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
27944 goto resync_fail;
27946 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
27947 OMP_CLAUSE_CHAIN (c) = list;
27948 return c;
27950 invalid_kind:
27951 cp_parser_error (parser, "invalid schedule kind");
27952 resync_fail:
27953 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
27954 /*or_comma=*/false,
27955 /*consume_paren=*/true);
27956 return list;
27959 /* OpenMP 3.0:
27960 untied */
27962 static tree
27963 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
27964 tree list, location_t location)
27966 tree c;
27968 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
27970 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
27971 OMP_CLAUSE_CHAIN (c) = list;
27972 return c;
27975 /* OpenMP 4.0:
27976 inbranch
27977 notinbranch */
27979 static tree
27980 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
27981 tree list, location_t location)
27983 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
27984 tree c = build_omp_clause (location, code);
27985 OMP_CLAUSE_CHAIN (c) = list;
27986 return c;
27989 /* OpenMP 4.0:
27990 parallel
27992 sections
27993 taskgroup */
27995 static tree
27996 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
27997 enum omp_clause_code code,
27998 tree list, location_t location)
28000 tree c = build_omp_clause (location, code);
28001 OMP_CLAUSE_CHAIN (c) = list;
28002 return c;
28005 /* OpenMP 4.0:
28006 num_teams ( expression ) */
28008 static tree
28009 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
28010 location_t location)
28012 tree t, c;
28014 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28015 return list;
28017 t = cp_parser_expression (parser);
28019 if (t == error_mark_node
28020 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28021 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28022 /*or_comma=*/false,
28023 /*consume_paren=*/true);
28025 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
28026 "num_teams", location);
28028 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
28029 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
28030 OMP_CLAUSE_CHAIN (c) = list;
28032 return c;
28035 /* OpenMP 4.0:
28036 thread_limit ( expression ) */
28038 static tree
28039 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
28040 location_t location)
28042 tree t, c;
28044 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28045 return list;
28047 t = cp_parser_expression (parser);
28049 if (t == error_mark_node
28050 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28051 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28052 /*or_comma=*/false,
28053 /*consume_paren=*/true);
28055 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
28056 "thread_limit", location);
28058 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
28059 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
28060 OMP_CLAUSE_CHAIN (c) = list;
28062 return c;
28065 /* OpenMP 4.0:
28066 aligned ( variable-list )
28067 aligned ( variable-list : constant-expression ) */
28069 static tree
28070 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
28072 tree nlist, c, alignment = NULL_TREE;
28073 bool colon;
28075 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28076 return list;
28078 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
28079 &colon);
28081 if (colon)
28083 alignment = cp_parser_constant_expression (parser, false, NULL);
28085 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28086 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28087 /*or_comma=*/false,
28088 /*consume_paren=*/true);
28090 if (alignment == error_mark_node)
28091 alignment = NULL_TREE;
28094 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28095 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
28097 return nlist;
28100 /* OpenMP 4.0:
28101 linear ( variable-list )
28102 linear ( variable-list : expression ) */
28104 static tree
28105 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
28106 bool is_cilk_simd_fn)
28108 tree nlist, c, step = integer_one_node;
28109 bool colon;
28111 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28112 return list;
28114 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
28115 &colon);
28117 if (colon)
28119 step = cp_parser_expression (parser);
28121 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
28123 sorry ("using parameters for %<linear%> step is not supported yet");
28124 step = integer_one_node;
28126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28127 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28128 /*or_comma=*/false,
28129 /*consume_paren=*/true);
28131 if (step == error_mark_node)
28132 return list;
28135 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28136 OMP_CLAUSE_LINEAR_STEP (c) = step;
28138 return nlist;
28141 /* OpenMP 4.0:
28142 safelen ( constant-expression ) */
28144 static tree
28145 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
28146 location_t location)
28148 tree t, c;
28150 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28151 return list;
28153 t = cp_parser_constant_expression (parser, false, NULL);
28155 if (t == error_mark_node
28156 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28157 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28158 /*or_comma=*/false,
28159 /*consume_paren=*/true);
28161 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
28163 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
28164 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
28165 OMP_CLAUSE_CHAIN (c) = list;
28167 return c;
28170 /* OpenMP 4.0:
28171 simdlen ( constant-expression ) */
28173 static tree
28174 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
28175 location_t location)
28177 tree t, c;
28179 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28180 return list;
28182 t = cp_parser_constant_expression (parser, false, NULL);
28184 if (t == error_mark_node
28185 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28186 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28187 /*or_comma=*/false,
28188 /*consume_paren=*/true);
28190 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
28192 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
28193 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
28194 OMP_CLAUSE_CHAIN (c) = list;
28196 return c;
28199 /* OpenMP 4.0:
28200 depend ( depend-kind : variable-list )
28202 depend-kind:
28203 in | out | inout */
28205 static tree
28206 cp_parser_omp_clause_depend (cp_parser *parser, tree list)
28208 tree nlist, c;
28209 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
28211 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28212 return list;
28214 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28216 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28217 const char *p = IDENTIFIER_POINTER (id);
28219 if (strcmp ("in", p) == 0)
28220 kind = OMP_CLAUSE_DEPEND_IN;
28221 else if (strcmp ("inout", p) == 0)
28222 kind = OMP_CLAUSE_DEPEND_INOUT;
28223 else if (strcmp ("out", p) == 0)
28224 kind = OMP_CLAUSE_DEPEND_OUT;
28225 else
28226 goto invalid_kind;
28228 else
28229 goto invalid_kind;
28231 cp_lexer_consume_token (parser->lexer);
28232 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28233 goto resync_fail;
28235 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND, list,
28236 NULL);
28238 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28239 OMP_CLAUSE_DEPEND_KIND (c) = kind;
28241 return nlist;
28243 invalid_kind:
28244 cp_parser_error (parser, "invalid depend kind");
28245 resync_fail:
28246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28247 /*or_comma=*/false,
28248 /*consume_paren=*/true);
28249 return list;
28252 /* OpenMP 4.0:
28253 map ( map-kind : variable-list )
28254 map ( variable-list )
28256 map-kind:
28257 alloc | to | from | tofrom */
28259 static tree
28260 cp_parser_omp_clause_map (cp_parser *parser, tree list)
28262 tree nlist, c;
28263 enum omp_clause_map_kind kind = OMP_CLAUSE_MAP_TOFROM;
28265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28266 return list;
28268 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
28269 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
28271 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28272 const char *p = IDENTIFIER_POINTER (id);
28274 if (strcmp ("alloc", p) == 0)
28275 kind = OMP_CLAUSE_MAP_ALLOC;
28276 else if (strcmp ("to", p) == 0)
28277 kind = OMP_CLAUSE_MAP_TO;
28278 else if (strcmp ("from", p) == 0)
28279 kind = OMP_CLAUSE_MAP_FROM;
28280 else if (strcmp ("tofrom", p) == 0)
28281 kind = OMP_CLAUSE_MAP_TOFROM;
28282 else
28284 cp_parser_error (parser, "invalid map kind");
28285 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28286 /*or_comma=*/false,
28287 /*consume_paren=*/true);
28288 return list;
28290 cp_lexer_consume_token (parser->lexer);
28291 cp_lexer_consume_token (parser->lexer);
28294 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
28295 NULL);
28297 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
28298 OMP_CLAUSE_MAP_KIND (c) = kind;
28300 return nlist;
28303 /* OpenMP 4.0:
28304 device ( expression ) */
28306 static tree
28307 cp_parser_omp_clause_device (cp_parser *parser, tree list,
28308 location_t location)
28310 tree t, c;
28312 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28313 return list;
28315 t = cp_parser_expression (parser);
28317 if (t == error_mark_node
28318 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28319 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28320 /*or_comma=*/false,
28321 /*consume_paren=*/true);
28323 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
28324 "device", location);
28326 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
28327 OMP_CLAUSE_DEVICE_ID (c) = t;
28328 OMP_CLAUSE_CHAIN (c) = list;
28330 return c;
28333 /* OpenMP 4.0:
28334 dist_schedule ( static )
28335 dist_schedule ( static , expression ) */
28337 static tree
28338 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
28339 location_t location)
28341 tree c, t;
28343 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28344 return list;
28346 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
28348 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
28349 goto invalid_kind;
28350 cp_lexer_consume_token (parser->lexer);
28352 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28354 cp_lexer_consume_token (parser->lexer);
28356 t = cp_parser_assignment_expression (parser, false, NULL);
28358 if (t == error_mark_node)
28359 goto resync_fail;
28360 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
28362 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
28363 goto resync_fail;
28365 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28366 goto resync_fail;
28368 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
28369 location);
28370 OMP_CLAUSE_CHAIN (c) = list;
28371 return c;
28373 invalid_kind:
28374 cp_parser_error (parser, "invalid dist_schedule kind");
28375 resync_fail:
28376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28377 /*or_comma=*/false,
28378 /*consume_paren=*/true);
28379 return list;
28382 /* OpenMP 4.0:
28383 proc_bind ( proc-bind-kind )
28385 proc-bind-kind:
28386 master | close | spread */
28388 static tree
28389 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
28390 location_t location)
28392 tree c;
28393 enum omp_clause_proc_bind_kind kind;
28395 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
28396 return list;
28398 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28400 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28401 const char *p = IDENTIFIER_POINTER (id);
28403 if (strcmp ("master", p) == 0)
28404 kind = OMP_CLAUSE_PROC_BIND_MASTER;
28405 else if (strcmp ("close", p) == 0)
28406 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
28407 else if (strcmp ("spread", p) == 0)
28408 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
28409 else
28410 goto invalid_kind;
28412 else
28413 goto invalid_kind;
28415 cp_lexer_consume_token (parser->lexer);
28416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
28417 goto resync_fail;
28419 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
28420 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
28421 location);
28422 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
28423 OMP_CLAUSE_CHAIN (c) = list;
28424 return c;
28426 invalid_kind:
28427 cp_parser_error (parser, "invalid depend kind");
28428 resync_fail:
28429 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
28430 /*or_comma=*/false,
28431 /*consume_paren=*/true);
28432 return list;
28435 /* Parse all OpenMP clauses. The set clauses allowed by the directive
28436 is a bitmask in MASK. Return the list of clauses found; the result
28437 of clause default goes in *pdefault. */
28439 static tree
28440 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
28441 const char *where, cp_token *pragma_tok,
28442 bool finish_p = true)
28444 tree clauses = NULL;
28445 bool first = true;
28446 cp_token *token = NULL;
28447 bool cilk_simd_fn = false;
28449 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
28451 pragma_omp_clause c_kind;
28452 const char *c_name;
28453 tree prev = clauses;
28455 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28456 cp_lexer_consume_token (parser->lexer);
28458 token = cp_lexer_peek_token (parser->lexer);
28459 c_kind = cp_parser_omp_clause_name (parser);
28461 switch (c_kind)
28463 case PRAGMA_OMP_CLAUSE_COLLAPSE:
28464 clauses = cp_parser_omp_clause_collapse (parser, clauses,
28465 token->location);
28466 c_name = "collapse";
28467 break;
28468 case PRAGMA_OMP_CLAUSE_COPYIN:
28469 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
28470 c_name = "copyin";
28471 break;
28472 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
28473 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
28474 clauses);
28475 c_name = "copyprivate";
28476 break;
28477 case PRAGMA_OMP_CLAUSE_DEFAULT:
28478 clauses = cp_parser_omp_clause_default (parser, clauses,
28479 token->location);
28480 c_name = "default";
28481 break;
28482 case PRAGMA_OMP_CLAUSE_FINAL:
28483 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
28484 c_name = "final";
28485 break;
28486 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
28487 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
28488 clauses);
28489 c_name = "firstprivate";
28490 break;
28491 case PRAGMA_OMP_CLAUSE_IF:
28492 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
28493 c_name = "if";
28494 break;
28495 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
28496 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
28497 clauses);
28498 c_name = "lastprivate";
28499 break;
28500 case PRAGMA_OMP_CLAUSE_MERGEABLE:
28501 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
28502 token->location);
28503 c_name = "mergeable";
28504 break;
28505 case PRAGMA_OMP_CLAUSE_NOWAIT:
28506 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
28507 c_name = "nowait";
28508 break;
28509 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
28510 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
28511 token->location);
28512 c_name = "num_threads";
28513 break;
28514 case PRAGMA_OMP_CLAUSE_ORDERED:
28515 clauses = cp_parser_omp_clause_ordered (parser, clauses,
28516 token->location);
28517 c_name = "ordered";
28518 break;
28519 case PRAGMA_OMP_CLAUSE_PRIVATE:
28520 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
28521 clauses);
28522 c_name = "private";
28523 break;
28524 case PRAGMA_OMP_CLAUSE_REDUCTION:
28525 clauses = cp_parser_omp_clause_reduction (parser, clauses);
28526 c_name = "reduction";
28527 break;
28528 case PRAGMA_OMP_CLAUSE_SCHEDULE:
28529 clauses = cp_parser_omp_clause_schedule (parser, clauses,
28530 token->location);
28531 c_name = "schedule";
28532 break;
28533 case PRAGMA_OMP_CLAUSE_SHARED:
28534 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
28535 clauses);
28536 c_name = "shared";
28537 break;
28538 case PRAGMA_OMP_CLAUSE_UNTIED:
28539 clauses = cp_parser_omp_clause_untied (parser, clauses,
28540 token->location);
28541 c_name = "untied";
28542 break;
28543 case PRAGMA_OMP_CLAUSE_INBRANCH:
28544 case PRAGMA_CILK_CLAUSE_MASK:
28545 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
28546 clauses, token->location);
28547 c_name = "inbranch";
28548 break;
28549 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
28550 case PRAGMA_CILK_CLAUSE_NOMASK:
28551 clauses = cp_parser_omp_clause_branch (parser,
28552 OMP_CLAUSE_NOTINBRANCH,
28553 clauses, token->location);
28554 c_name = "notinbranch";
28555 break;
28556 case PRAGMA_OMP_CLAUSE_PARALLEL:
28557 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
28558 clauses, token->location);
28559 c_name = "parallel";
28560 if (!first)
28562 clause_not_first:
28563 error_at (token->location, "%qs must be the first clause of %qs",
28564 c_name, where);
28565 clauses = prev;
28567 break;
28568 case PRAGMA_OMP_CLAUSE_FOR:
28569 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
28570 clauses, token->location);
28571 c_name = "for";
28572 if (!first)
28573 goto clause_not_first;
28574 break;
28575 case PRAGMA_OMP_CLAUSE_SECTIONS:
28576 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
28577 clauses, token->location);
28578 c_name = "sections";
28579 if (!first)
28580 goto clause_not_first;
28581 break;
28582 case PRAGMA_OMP_CLAUSE_TASKGROUP:
28583 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
28584 clauses, token->location);
28585 c_name = "taskgroup";
28586 if (!first)
28587 goto clause_not_first;
28588 break;
28589 case PRAGMA_OMP_CLAUSE_TO:
28590 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO,
28591 clauses);
28592 c_name = "to";
28593 break;
28594 case PRAGMA_OMP_CLAUSE_FROM:
28595 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM,
28596 clauses);
28597 c_name = "from";
28598 break;
28599 case PRAGMA_OMP_CLAUSE_UNIFORM:
28600 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
28601 clauses);
28602 c_name = "uniform";
28603 break;
28604 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
28605 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
28606 token->location);
28607 c_name = "num_teams";
28608 break;
28609 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
28610 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
28611 token->location);
28612 c_name = "thread_limit";
28613 break;
28614 case PRAGMA_OMP_CLAUSE_ALIGNED:
28615 clauses = cp_parser_omp_clause_aligned (parser, clauses);
28616 c_name = "aligned";
28617 break;
28618 case PRAGMA_OMP_CLAUSE_LINEAR:
28619 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
28620 cilk_simd_fn = true;
28621 clauses = cp_parser_omp_clause_linear (parser, clauses, cilk_simd_fn);
28622 c_name = "linear";
28623 break;
28624 case PRAGMA_OMP_CLAUSE_DEPEND:
28625 clauses = cp_parser_omp_clause_depend (parser, clauses);
28626 c_name = "depend";
28627 break;
28628 case PRAGMA_OMP_CLAUSE_MAP:
28629 clauses = cp_parser_omp_clause_map (parser, clauses);
28630 c_name = "map";
28631 break;
28632 case PRAGMA_OMP_CLAUSE_DEVICE:
28633 clauses = cp_parser_omp_clause_device (parser, clauses,
28634 token->location);
28635 c_name = "device";
28636 break;
28637 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
28638 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
28639 token->location);
28640 c_name = "dist_schedule";
28641 break;
28642 case PRAGMA_OMP_CLAUSE_PROC_BIND:
28643 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
28644 token->location);
28645 c_name = "proc_bind";
28646 break;
28647 case PRAGMA_OMP_CLAUSE_SAFELEN:
28648 clauses = cp_parser_omp_clause_safelen (parser, clauses,
28649 token->location);
28650 c_name = "safelen";
28651 break;
28652 case PRAGMA_OMP_CLAUSE_SIMDLEN:
28653 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
28654 token->location);
28655 c_name = "simdlen";
28656 break;
28657 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
28658 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
28659 c_name = "simdlen";
28660 break;
28661 default:
28662 cp_parser_error (parser, "expected %<#pragma omp%> clause");
28663 goto saw_error;
28666 first = false;
28668 if (((mask >> c_kind) & 1) == 0)
28670 /* Remove the invalid clause(s) from the list to avoid
28671 confusing the rest of the compiler. */
28672 clauses = prev;
28673 error_at (token->location, "%qs is not valid for %qs", c_name, where);
28676 saw_error:
28677 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
28678 no reason to skip to the end. */
28679 if (!(flag_cilkplus && pragma_tok == NULL))
28680 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
28681 if (finish_p)
28682 return finish_omp_clauses (clauses);
28683 return clauses;
28686 /* OpenMP 2.5:
28687 structured-block:
28688 statement
28690 In practice, we're also interested in adding the statement to an
28691 outer node. So it is convenient if we work around the fact that
28692 cp_parser_statement calls add_stmt. */
28694 static unsigned
28695 cp_parser_begin_omp_structured_block (cp_parser *parser)
28697 unsigned save = parser->in_statement;
28699 /* Only move the values to IN_OMP_BLOCK if they weren't false.
28700 This preserves the "not within loop or switch" style error messages
28701 for nonsense cases like
28702 void foo() {
28703 #pragma omp single
28704 break;
28707 if (parser->in_statement)
28708 parser->in_statement = IN_OMP_BLOCK;
28710 return save;
28713 static void
28714 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
28716 parser->in_statement = save;
28719 static tree
28720 cp_parser_omp_structured_block (cp_parser *parser)
28722 tree stmt = begin_omp_structured_block ();
28723 unsigned int save = cp_parser_begin_omp_structured_block (parser);
28725 cp_parser_statement (parser, NULL_TREE, false, NULL);
28727 cp_parser_end_omp_structured_block (parser, save);
28728 return finish_omp_structured_block (stmt);
28731 /* OpenMP 2.5:
28732 # pragma omp atomic new-line
28733 expression-stmt
28735 expression-stmt:
28736 x binop= expr | x++ | ++x | x-- | --x
28737 binop:
28738 +, *, -, /, &, ^, |, <<, >>
28740 where x is an lvalue expression with scalar type.
28742 OpenMP 3.1:
28743 # pragma omp atomic new-line
28744 update-stmt
28746 # pragma omp atomic read new-line
28747 read-stmt
28749 # pragma omp atomic write new-line
28750 write-stmt
28752 # pragma omp atomic update new-line
28753 update-stmt
28755 # pragma omp atomic capture new-line
28756 capture-stmt
28758 # pragma omp atomic capture new-line
28759 capture-block
28761 read-stmt:
28762 v = x
28763 write-stmt:
28764 x = expr
28765 update-stmt:
28766 expression-stmt | x = x binop expr
28767 capture-stmt:
28768 v = expression-stmt
28769 capture-block:
28770 { v = x; update-stmt; } | { update-stmt; v = x; }
28772 OpenMP 4.0:
28773 update-stmt:
28774 expression-stmt | x = x binop expr | x = expr binop x
28775 capture-stmt:
28776 v = update-stmt
28777 capture-block:
28778 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
28780 where x and v are lvalue expressions with scalar type. */
28782 static void
28783 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
28785 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
28786 tree rhs1 = NULL_TREE, orig_lhs;
28787 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
28788 bool structured_block = false;
28789 bool seq_cst = false;
28791 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28793 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28794 const char *p = IDENTIFIER_POINTER (id);
28796 if (!strcmp (p, "seq_cst"))
28798 seq_cst = true;
28799 cp_lexer_consume_token (parser->lexer);
28800 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28801 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28802 cp_lexer_consume_token (parser->lexer);
28805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28807 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28808 const char *p = IDENTIFIER_POINTER (id);
28810 if (!strcmp (p, "read"))
28811 code = OMP_ATOMIC_READ;
28812 else if (!strcmp (p, "write"))
28813 code = NOP_EXPR;
28814 else if (!strcmp (p, "update"))
28815 code = OMP_ATOMIC;
28816 else if (!strcmp (p, "capture"))
28817 code = OMP_ATOMIC_CAPTURE_NEW;
28818 else
28819 p = NULL;
28820 if (p)
28821 cp_lexer_consume_token (parser->lexer);
28823 if (!seq_cst)
28825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
28826 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
28827 cp_lexer_consume_token (parser->lexer);
28829 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
28831 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
28832 const char *p = IDENTIFIER_POINTER (id);
28834 if (!strcmp (p, "seq_cst"))
28836 seq_cst = true;
28837 cp_lexer_consume_token (parser->lexer);
28841 cp_parser_require_pragma_eol (parser, pragma_tok);
28843 switch (code)
28845 case OMP_ATOMIC_READ:
28846 case NOP_EXPR: /* atomic write */
28847 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28848 /*cast_p=*/false, NULL);
28849 if (v == error_mark_node)
28850 goto saw_error;
28851 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28852 goto saw_error;
28853 if (code == NOP_EXPR)
28854 lhs = cp_parser_expression (parser);
28855 else
28856 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28857 /*cast_p=*/false, NULL);
28858 if (lhs == error_mark_node)
28859 goto saw_error;
28860 if (code == NOP_EXPR)
28862 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
28863 opcode. */
28864 code = OMP_ATOMIC;
28865 rhs = lhs;
28866 lhs = v;
28867 v = NULL_TREE;
28869 goto done;
28870 case OMP_ATOMIC_CAPTURE_NEW:
28871 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28873 cp_lexer_consume_token (parser->lexer);
28874 structured_block = true;
28876 else
28878 v = cp_parser_unary_expression (parser, /*address_p=*/false,
28879 /*cast_p=*/false, NULL);
28880 if (v == error_mark_node)
28881 goto saw_error;
28882 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
28883 goto saw_error;
28885 default:
28886 break;
28889 restart:
28890 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
28891 /*cast_p=*/false, NULL);
28892 orig_lhs = lhs;
28893 switch (TREE_CODE (lhs))
28895 case ERROR_MARK:
28896 goto saw_error;
28898 case POSTINCREMENT_EXPR:
28899 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28900 code = OMP_ATOMIC_CAPTURE_OLD;
28901 /* FALLTHROUGH */
28902 case PREINCREMENT_EXPR:
28903 lhs = TREE_OPERAND (lhs, 0);
28904 opcode = PLUS_EXPR;
28905 rhs = integer_one_node;
28906 break;
28908 case POSTDECREMENT_EXPR:
28909 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
28910 code = OMP_ATOMIC_CAPTURE_OLD;
28911 /* FALLTHROUGH */
28912 case PREDECREMENT_EXPR:
28913 lhs = TREE_OPERAND (lhs, 0);
28914 opcode = MINUS_EXPR;
28915 rhs = integer_one_node;
28916 break;
28918 case COMPOUND_EXPR:
28919 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
28920 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
28921 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
28922 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
28923 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
28924 (TREE_OPERAND (lhs, 1), 0), 0)))
28925 == BOOLEAN_TYPE)
28926 /* Undo effects of boolean_increment for post {in,de}crement. */
28927 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
28928 /* FALLTHRU */
28929 case MODIFY_EXPR:
28930 if (TREE_CODE (lhs) == MODIFY_EXPR
28931 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
28933 /* Undo effects of boolean_increment. */
28934 if (integer_onep (TREE_OPERAND (lhs, 1)))
28936 /* This is pre or post increment. */
28937 rhs = TREE_OPERAND (lhs, 1);
28938 lhs = TREE_OPERAND (lhs, 0);
28939 opcode = NOP_EXPR;
28940 if (code == OMP_ATOMIC_CAPTURE_NEW
28941 && !structured_block
28942 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
28943 code = OMP_ATOMIC_CAPTURE_OLD;
28944 break;
28947 /* FALLTHRU */
28948 default:
28949 switch (cp_lexer_peek_token (parser->lexer)->type)
28951 case CPP_MULT_EQ:
28952 opcode = MULT_EXPR;
28953 break;
28954 case CPP_DIV_EQ:
28955 opcode = TRUNC_DIV_EXPR;
28956 break;
28957 case CPP_PLUS_EQ:
28958 opcode = PLUS_EXPR;
28959 break;
28960 case CPP_MINUS_EQ:
28961 opcode = MINUS_EXPR;
28962 break;
28963 case CPP_LSHIFT_EQ:
28964 opcode = LSHIFT_EXPR;
28965 break;
28966 case CPP_RSHIFT_EQ:
28967 opcode = RSHIFT_EXPR;
28968 break;
28969 case CPP_AND_EQ:
28970 opcode = BIT_AND_EXPR;
28971 break;
28972 case CPP_OR_EQ:
28973 opcode = BIT_IOR_EXPR;
28974 break;
28975 case CPP_XOR_EQ:
28976 opcode = BIT_XOR_EXPR;
28977 break;
28978 case CPP_EQ:
28979 enum cp_parser_prec oprec;
28980 cp_token *token;
28981 cp_lexer_consume_token (parser->lexer);
28982 cp_parser_parse_tentatively (parser);
28983 rhs1 = cp_parser_simple_cast_expression (parser);
28984 if (rhs1 == error_mark_node)
28986 cp_parser_abort_tentative_parse (parser);
28987 cp_parser_simple_cast_expression (parser);
28988 goto saw_error;
28990 token = cp_lexer_peek_token (parser->lexer);
28991 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
28993 cp_parser_abort_tentative_parse (parser);
28994 cp_parser_parse_tentatively (parser);
28995 rhs = cp_parser_binary_expression (parser, false, true,
28996 PREC_NOT_OPERATOR, NULL);
28997 if (rhs == error_mark_node)
28999 cp_parser_abort_tentative_parse (parser);
29000 cp_parser_binary_expression (parser, false, true,
29001 PREC_NOT_OPERATOR, NULL);
29002 goto saw_error;
29004 switch (TREE_CODE (rhs))
29006 case MULT_EXPR:
29007 case TRUNC_DIV_EXPR:
29008 case PLUS_EXPR:
29009 case MINUS_EXPR:
29010 case LSHIFT_EXPR:
29011 case RSHIFT_EXPR:
29012 case BIT_AND_EXPR:
29013 case BIT_IOR_EXPR:
29014 case BIT_XOR_EXPR:
29015 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
29017 if (cp_parser_parse_definitely (parser))
29019 opcode = TREE_CODE (rhs);
29020 rhs1 = TREE_OPERAND (rhs, 0);
29021 rhs = TREE_OPERAND (rhs, 1);
29022 goto stmt_done;
29024 else
29025 goto saw_error;
29027 break;
29028 default:
29029 break;
29031 cp_parser_abort_tentative_parse (parser);
29032 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
29034 rhs = cp_parser_expression (parser);
29035 if (rhs == error_mark_node)
29036 goto saw_error;
29037 opcode = NOP_EXPR;
29038 rhs1 = NULL_TREE;
29039 goto stmt_done;
29041 cp_parser_error (parser,
29042 "invalid form of %<#pragma omp atomic%>");
29043 goto saw_error;
29045 if (!cp_parser_parse_definitely (parser))
29046 goto saw_error;
29047 switch (token->type)
29049 case CPP_SEMICOLON:
29050 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29052 code = OMP_ATOMIC_CAPTURE_OLD;
29053 v = lhs;
29054 lhs = NULL_TREE;
29055 lhs1 = rhs1;
29056 rhs1 = NULL_TREE;
29057 cp_lexer_consume_token (parser->lexer);
29058 goto restart;
29060 else if (structured_block)
29062 opcode = NOP_EXPR;
29063 rhs = rhs1;
29064 rhs1 = NULL_TREE;
29065 goto stmt_done;
29067 cp_parser_error (parser,
29068 "invalid form of %<#pragma omp atomic%>");
29069 goto saw_error;
29070 case CPP_MULT:
29071 opcode = MULT_EXPR;
29072 break;
29073 case CPP_DIV:
29074 opcode = TRUNC_DIV_EXPR;
29075 break;
29076 case CPP_PLUS:
29077 opcode = PLUS_EXPR;
29078 break;
29079 case CPP_MINUS:
29080 opcode = MINUS_EXPR;
29081 break;
29082 case CPP_LSHIFT:
29083 opcode = LSHIFT_EXPR;
29084 break;
29085 case CPP_RSHIFT:
29086 opcode = RSHIFT_EXPR;
29087 break;
29088 case CPP_AND:
29089 opcode = BIT_AND_EXPR;
29090 break;
29091 case CPP_OR:
29092 opcode = BIT_IOR_EXPR;
29093 break;
29094 case CPP_XOR:
29095 opcode = BIT_XOR_EXPR;
29096 break;
29097 default:
29098 cp_parser_error (parser,
29099 "invalid operator for %<#pragma omp atomic%>");
29100 goto saw_error;
29102 oprec = TOKEN_PRECEDENCE (token);
29103 gcc_assert (oprec != PREC_NOT_OPERATOR);
29104 if (commutative_tree_code (opcode))
29105 oprec = (enum cp_parser_prec) (oprec - 1);
29106 cp_lexer_consume_token (parser->lexer);
29107 rhs = cp_parser_binary_expression (parser, false, false,
29108 oprec, NULL);
29109 if (rhs == error_mark_node)
29110 goto saw_error;
29111 goto stmt_done;
29112 /* FALLTHROUGH */
29113 default:
29114 cp_parser_error (parser,
29115 "invalid operator for %<#pragma omp atomic%>");
29116 goto saw_error;
29118 cp_lexer_consume_token (parser->lexer);
29120 rhs = cp_parser_expression (parser);
29121 if (rhs == error_mark_node)
29122 goto saw_error;
29123 break;
29125 stmt_done:
29126 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
29128 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
29129 goto saw_error;
29130 v = cp_parser_unary_expression (parser, /*address_p=*/false,
29131 /*cast_p=*/false, NULL);
29132 if (v == error_mark_node)
29133 goto saw_error;
29134 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29135 goto saw_error;
29136 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
29137 /*cast_p=*/false, NULL);
29138 if (lhs1 == error_mark_node)
29139 goto saw_error;
29141 if (structured_block)
29143 cp_parser_consume_semicolon_at_end_of_statement (parser);
29144 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
29146 done:
29147 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
29148 if (!structured_block)
29149 cp_parser_consume_semicolon_at_end_of_statement (parser);
29150 return;
29152 saw_error:
29153 cp_parser_skip_to_end_of_block_or_statement (parser);
29154 if (structured_block)
29156 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29157 cp_lexer_consume_token (parser->lexer);
29158 else if (code == OMP_ATOMIC_CAPTURE_NEW)
29160 cp_parser_skip_to_end_of_block_or_statement (parser);
29161 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29162 cp_lexer_consume_token (parser->lexer);
29168 /* OpenMP 2.5:
29169 # pragma omp barrier new-line */
29171 static void
29172 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
29174 cp_parser_require_pragma_eol (parser, pragma_tok);
29175 finish_omp_barrier ();
29178 /* OpenMP 2.5:
29179 # pragma omp critical [(name)] new-line
29180 structured-block */
29182 static tree
29183 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
29185 tree stmt, name = NULL;
29187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29189 cp_lexer_consume_token (parser->lexer);
29191 name = cp_parser_identifier (parser);
29193 if (name == error_mark_node
29194 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29195 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29196 /*or_comma=*/false,
29197 /*consume_paren=*/true);
29198 if (name == error_mark_node)
29199 name = NULL;
29201 cp_parser_require_pragma_eol (parser, pragma_tok);
29203 stmt = cp_parser_omp_structured_block (parser);
29204 return c_finish_omp_critical (input_location, stmt, name);
29207 /* OpenMP 2.5:
29208 # pragma omp flush flush-vars[opt] new-line
29210 flush-vars:
29211 ( variable-list ) */
29213 static void
29214 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
29216 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29217 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
29218 cp_parser_require_pragma_eol (parser, pragma_tok);
29220 finish_omp_flush ();
29223 /* Helper function, to parse omp for increment expression. */
29225 static tree
29226 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
29228 tree cond = cp_parser_binary_expression (parser, false, true,
29229 PREC_NOT_OPERATOR, NULL);
29230 if (cond == error_mark_node
29231 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29233 cp_parser_skip_to_end_of_statement (parser);
29234 return error_mark_node;
29237 switch (TREE_CODE (cond))
29239 case GT_EXPR:
29240 case GE_EXPR:
29241 case LT_EXPR:
29242 case LE_EXPR:
29243 break;
29244 case NE_EXPR:
29245 if (code == CILK_SIMD || code == CILK_FOR)
29246 break;
29247 /* Fall through: OpenMP disallows NE_EXPR. */
29248 default:
29249 return error_mark_node;
29252 /* If decl is an iterator, preserve LHS and RHS of the relational
29253 expr until finish_omp_for. */
29254 if (decl
29255 && (type_dependent_expression_p (decl)
29256 || CLASS_TYPE_P (TREE_TYPE (decl))))
29257 return cond;
29259 return build_x_binary_op (input_location, TREE_CODE (cond),
29260 TREE_OPERAND (cond, 0), ERROR_MARK,
29261 TREE_OPERAND (cond, 1), ERROR_MARK,
29262 /*overload=*/NULL, tf_warning_or_error);
29265 /* Helper function, to parse omp for increment expression. */
29267 static tree
29268 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
29270 cp_token *token = cp_lexer_peek_token (parser->lexer);
29271 enum tree_code op;
29272 tree lhs, rhs;
29273 cp_id_kind idk;
29274 bool decl_first;
29276 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29278 op = (token->type == CPP_PLUS_PLUS
29279 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
29280 cp_lexer_consume_token (parser->lexer);
29281 lhs = cp_parser_simple_cast_expression (parser);
29282 if (lhs != decl)
29283 return error_mark_node;
29284 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29287 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
29288 if (lhs != decl)
29289 return error_mark_node;
29291 token = cp_lexer_peek_token (parser->lexer);
29292 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
29294 op = (token->type == CPP_PLUS_PLUS
29295 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
29296 cp_lexer_consume_token (parser->lexer);
29297 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
29300 op = cp_parser_assignment_operator_opt (parser);
29301 if (op == ERROR_MARK)
29302 return error_mark_node;
29304 if (op != NOP_EXPR)
29306 rhs = cp_parser_assignment_expression (parser, false, NULL);
29307 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
29308 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29311 lhs = cp_parser_binary_expression (parser, false, false,
29312 PREC_ADDITIVE_EXPRESSION, NULL);
29313 token = cp_lexer_peek_token (parser->lexer);
29314 decl_first = lhs == decl;
29315 if (decl_first)
29316 lhs = NULL_TREE;
29317 if (token->type != CPP_PLUS
29318 && token->type != CPP_MINUS)
29319 return error_mark_node;
29323 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
29324 cp_lexer_consume_token (parser->lexer);
29325 rhs = cp_parser_binary_expression (parser, false, false,
29326 PREC_ADDITIVE_EXPRESSION, NULL);
29327 token = cp_lexer_peek_token (parser->lexer);
29328 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
29330 if (lhs == NULL_TREE)
29332 if (op == PLUS_EXPR)
29333 lhs = rhs;
29334 else
29335 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
29336 tf_warning_or_error);
29338 else
29339 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
29340 ERROR_MARK, NULL, tf_warning_or_error);
29343 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
29345 if (!decl_first)
29347 if (rhs != decl || op == MINUS_EXPR)
29348 return error_mark_node;
29349 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
29351 else
29352 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
29354 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
29357 /* Parse the initialization statement of either an OpenMP for loop or
29358 a Cilk Plus for loop.
29360 Return true if the resulting construct should have an
29361 OMP_CLAUSE_PRIVATE added to it. */
29363 static bool
29364 cp_parser_omp_for_loop_init (cp_parser *parser,
29365 enum tree_code code,
29366 tree &this_pre_body,
29367 vec<tree, va_gc> *for_block,
29368 tree &init,
29369 tree &decl,
29370 tree &real_decl)
29372 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29373 return false;
29375 bool add_private_clause = false;
29377 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
29379 init-expr:
29380 var = lb
29381 integer-type var = lb
29382 random-access-iterator-type var = lb
29383 pointer-type var = lb
29385 cp_decl_specifier_seq type_specifiers;
29387 /* First, try to parse as an initialized declaration. See
29388 cp_parser_condition, from whence the bulk of this is copied. */
29390 cp_parser_parse_tentatively (parser);
29391 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
29392 /*is_trailing_return=*/false,
29393 &type_specifiers);
29394 if (cp_parser_parse_definitely (parser))
29396 /* If parsing a type specifier seq succeeded, then this
29397 MUST be a initialized declaration. */
29398 tree asm_specification, attributes;
29399 cp_declarator *declarator;
29401 declarator = cp_parser_declarator (parser,
29402 CP_PARSER_DECLARATOR_NAMED,
29403 /*ctor_dtor_or_conv_p=*/NULL,
29404 /*parenthesized_p=*/NULL,
29405 /*member_p=*/false,
29406 /*friend_p=*/false);
29407 attributes = cp_parser_attributes_opt (parser);
29408 asm_specification = cp_parser_asm_specification_opt (parser);
29410 if (declarator == cp_error_declarator)
29411 cp_parser_skip_to_end_of_statement (parser);
29413 else
29415 tree pushed_scope, auto_node;
29417 decl = start_decl (declarator, &type_specifiers,
29418 SD_INITIALIZED, attributes,
29419 /*prefix_attributes=*/NULL_TREE,
29420 &pushed_scope);
29422 auto_node = type_uses_auto (TREE_TYPE (decl));
29423 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29425 if (cp_lexer_next_token_is (parser->lexer,
29426 CPP_OPEN_PAREN))
29428 if (code != CILK_SIMD && code != CILK_FOR)
29429 error ("parenthesized initialization is not allowed in "
29430 "OpenMP %<for%> loop");
29431 else
29432 error ("parenthesized initialization is "
29433 "not allowed in for-loop");
29435 else
29436 /* Trigger an error. */
29437 cp_parser_require (parser, CPP_EQ, RT_EQ);
29439 init = error_mark_node;
29440 cp_parser_skip_to_end_of_statement (parser);
29442 else if (CLASS_TYPE_P (TREE_TYPE (decl))
29443 || type_dependent_expression_p (decl)
29444 || auto_node)
29446 bool is_direct_init, is_non_constant_init;
29448 init = cp_parser_initializer (parser,
29449 &is_direct_init,
29450 &is_non_constant_init);
29452 if (auto_node)
29454 TREE_TYPE (decl)
29455 = do_auto_deduction (TREE_TYPE (decl), init,
29456 auto_node);
29458 if (!CLASS_TYPE_P (TREE_TYPE (decl))
29459 && !type_dependent_expression_p (decl))
29460 goto non_class;
29463 cp_finish_decl (decl, init, !is_non_constant_init,
29464 asm_specification,
29465 LOOKUP_ONLYCONVERTING);
29466 if (CLASS_TYPE_P (TREE_TYPE (decl)))
29468 vec_safe_push (for_block, this_pre_body);
29469 init = NULL_TREE;
29471 else
29472 init = pop_stmt_list (this_pre_body);
29473 this_pre_body = NULL_TREE;
29475 else
29477 /* Consume '='. */
29478 cp_lexer_consume_token (parser->lexer);
29479 init = cp_parser_assignment_expression (parser, false, NULL);
29481 non_class:
29482 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
29483 init = error_mark_node;
29484 else
29485 cp_finish_decl (decl, NULL_TREE,
29486 /*init_const_expr_p=*/false,
29487 asm_specification,
29488 LOOKUP_ONLYCONVERTING);
29491 if (pushed_scope)
29492 pop_scope (pushed_scope);
29495 else
29497 cp_id_kind idk;
29498 /* If parsing a type specifier sequence failed, then
29499 this MUST be a simple expression. */
29500 if (code == CILK_FOR)
29501 error ("%<_Cilk_for%> allows expression instead of declaration only "
29502 "in C, not in C++");
29503 cp_parser_parse_tentatively (parser);
29504 decl = cp_parser_primary_expression (parser, false, false,
29505 false, &idk);
29506 if (!cp_parser_error_occurred (parser)
29507 && decl
29508 && DECL_P (decl)
29509 && CLASS_TYPE_P (TREE_TYPE (decl)))
29511 tree rhs;
29513 cp_parser_parse_definitely (parser);
29514 cp_parser_require (parser, CPP_EQ, RT_EQ);
29515 rhs = cp_parser_assignment_expression (parser, false, NULL);
29516 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
29517 decl, NOP_EXPR,
29518 rhs,
29519 tf_warning_or_error));
29520 add_private_clause = true;
29522 else
29524 decl = NULL;
29525 cp_parser_abort_tentative_parse (parser);
29526 init = cp_parser_expression (parser);
29527 if (init)
29529 if (TREE_CODE (init) == MODIFY_EXPR
29530 || TREE_CODE (init) == MODOP_EXPR)
29531 real_decl = TREE_OPERAND (init, 0);
29535 return add_private_clause;
29538 /* Parse the restricted form of the for statement allowed by OpenMP. */
29540 static tree
29541 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
29542 tree *cclauses)
29544 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
29545 tree real_decl, initv, condv, incrv, declv;
29546 tree this_pre_body, cl;
29547 location_t loc_first;
29548 bool collapse_err = false;
29549 int i, collapse = 1, nbraces = 0;
29550 vec<tree, va_gc> *for_block = make_tree_vector ();
29552 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
29553 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
29554 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
29556 gcc_assert (collapse >= 1);
29558 declv = make_tree_vec (collapse);
29559 initv = make_tree_vec (collapse);
29560 condv = make_tree_vec (collapse);
29561 incrv = make_tree_vec (collapse);
29563 loc_first = cp_lexer_peek_token (parser->lexer)->location;
29565 for (i = 0; i < collapse; i++)
29567 int bracecount = 0;
29568 bool add_private_clause = false;
29569 location_t loc;
29571 if (code != CILK_FOR
29572 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29574 cp_parser_error (parser, "for statement expected");
29575 return NULL;
29577 if (code == CILK_FOR
29578 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
29580 cp_parser_error (parser, "_Cilk_for statement expected");
29581 return NULL;
29583 loc = cp_lexer_consume_token (parser->lexer)->location;
29585 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29586 return NULL;
29588 init = decl = real_decl = NULL;
29589 this_pre_body = push_stmt_list ();
29591 add_private_clause
29592 |= cp_parser_omp_for_loop_init (parser, code,
29593 this_pre_body, for_block,
29594 init, decl, real_decl);
29596 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29597 if (this_pre_body)
29599 this_pre_body = pop_stmt_list (this_pre_body);
29600 if (pre_body)
29602 tree t = pre_body;
29603 pre_body = push_stmt_list ();
29604 add_stmt (t);
29605 add_stmt (this_pre_body);
29606 pre_body = pop_stmt_list (pre_body);
29608 else
29609 pre_body = this_pre_body;
29612 if (decl)
29613 real_decl = decl;
29614 if (cclauses != NULL
29615 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
29616 && real_decl != NULL_TREE)
29618 tree *c;
29619 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
29620 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
29621 && OMP_CLAUSE_DECL (*c) == real_decl)
29623 error_at (loc, "iteration variable %qD"
29624 " should not be firstprivate", real_decl);
29625 *c = OMP_CLAUSE_CHAIN (*c);
29627 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
29628 && OMP_CLAUSE_DECL (*c) == real_decl)
29630 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
29631 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
29632 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
29633 OMP_CLAUSE_DECL (l) = real_decl;
29634 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
29635 if (code == OMP_SIMD)
29637 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29638 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
29640 else
29642 OMP_CLAUSE_CHAIN (l) = clauses;
29643 clauses = l;
29645 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
29646 CP_OMP_CLAUSE_INFO (*c) = NULL;
29647 add_private_clause = false;
29649 else
29651 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
29652 && OMP_CLAUSE_DECL (*c) == real_decl)
29653 add_private_clause = false;
29654 c = &OMP_CLAUSE_CHAIN (*c);
29658 if (add_private_clause)
29660 tree c;
29661 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29663 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
29664 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
29665 && OMP_CLAUSE_DECL (c) == decl)
29666 break;
29667 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
29668 && OMP_CLAUSE_DECL (c) == decl)
29669 error_at (loc, "iteration variable %qD "
29670 "should not be firstprivate",
29671 decl);
29672 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
29673 && OMP_CLAUSE_DECL (c) == decl)
29674 error_at (loc, "iteration variable %qD should not be reduction",
29675 decl);
29677 if (c == NULL)
29679 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
29680 OMP_CLAUSE_DECL (c) = decl;
29681 c = finish_omp_clauses (c);
29682 if (c)
29684 OMP_CLAUSE_CHAIN (c) = clauses;
29685 clauses = c;
29690 cond = NULL;
29691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29692 cond = cp_parser_omp_for_cond (parser, decl, code);
29693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29695 incr = NULL;
29696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29698 /* If decl is an iterator, preserve the operator on decl
29699 until finish_omp_for. */
29700 if (real_decl
29701 && ((processing_template_decl
29702 && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
29703 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
29704 incr = cp_parser_omp_for_incr (parser, real_decl);
29705 else
29706 incr = cp_parser_expression (parser);
29707 if (CAN_HAVE_LOCATION_P (incr) && !EXPR_HAS_LOCATION (incr))
29708 SET_EXPR_LOCATION (incr, input_location);
29711 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29712 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
29713 /*or_comma=*/false,
29714 /*consume_paren=*/true);
29716 TREE_VEC_ELT (declv, i) = decl;
29717 TREE_VEC_ELT (initv, i) = init;
29718 TREE_VEC_ELT (condv, i) = cond;
29719 TREE_VEC_ELT (incrv, i) = incr;
29721 if (i == collapse - 1)
29722 break;
29724 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
29725 in between the collapsed for loops to be still considered perfectly
29726 nested. Hopefully the final version clarifies this.
29727 For now handle (multiple) {'s and empty statements. */
29728 cp_parser_parse_tentatively (parser);
29731 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29732 break;
29733 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29735 cp_lexer_consume_token (parser->lexer);
29736 bracecount++;
29738 else if (bracecount
29739 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29740 cp_lexer_consume_token (parser->lexer);
29741 else
29743 loc = cp_lexer_peek_token (parser->lexer)->location;
29744 error_at (loc, "not enough collapsed for loops");
29745 collapse_err = true;
29746 cp_parser_abort_tentative_parse (parser);
29747 declv = NULL_TREE;
29748 break;
29751 while (1);
29753 if (declv)
29755 cp_parser_parse_definitely (parser);
29756 nbraces += bracecount;
29760 /* Note that we saved the original contents of this flag when we entered
29761 the structured block, and so we don't need to re-save it here. */
29762 if (code == CILK_SIMD || code == CILK_FOR)
29763 parser->in_statement = IN_CILK_SIMD_FOR;
29764 else
29765 parser->in_statement = IN_OMP_FOR;
29767 /* Note that the grammar doesn't call for a structured block here,
29768 though the loop as a whole is a structured block. */
29769 body = push_stmt_list ();
29770 cp_parser_statement (parser, NULL_TREE, false, NULL);
29771 body = pop_stmt_list (body);
29773 if (declv == NULL_TREE)
29774 ret = NULL_TREE;
29775 else
29776 ret = finish_omp_for (loc_first, code, declv, initv, condv, incrv, body,
29777 pre_body, clauses);
29779 while (nbraces)
29781 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29783 cp_lexer_consume_token (parser->lexer);
29784 nbraces--;
29786 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29787 cp_lexer_consume_token (parser->lexer);
29788 else
29790 if (!collapse_err)
29792 error_at (cp_lexer_peek_token (parser->lexer)->location,
29793 "collapsed loops not perfectly nested");
29795 collapse_err = true;
29796 cp_parser_statement_seq_opt (parser, NULL);
29797 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
29798 break;
29802 while (!for_block->is_empty ())
29803 add_stmt (pop_stmt_list (for_block->pop ()));
29804 release_tree_vector (for_block);
29806 return ret;
29809 /* Helper function for OpenMP parsing, split clauses and call
29810 finish_omp_clauses on each of the set of clauses afterwards. */
29812 static void
29813 cp_omp_split_clauses (location_t loc, enum tree_code code,
29814 omp_clause_mask mask, tree clauses, tree *cclauses)
29816 int i;
29817 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
29818 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
29819 if (cclauses[i])
29820 cclauses[i] = finish_omp_clauses (cclauses[i]);
29823 /* OpenMP 4.0:
29824 #pragma omp simd simd-clause[optseq] new-line
29825 for-loop */
29827 #define OMP_SIMD_CLAUSE_MASK \
29828 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
29829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
29830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
29831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29836 static tree
29837 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
29838 char *p_name, omp_clause_mask mask, tree *cclauses)
29840 tree clauses, sb, ret;
29841 unsigned int save;
29842 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29844 strcat (p_name, " simd");
29845 mask |= OMP_SIMD_CLAUSE_MASK;
29846 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
29848 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29849 cclauses == NULL);
29850 if (cclauses)
29852 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
29853 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
29856 sb = begin_omp_structured_block ();
29857 save = cp_parser_begin_omp_structured_block (parser);
29859 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses);
29861 cp_parser_end_omp_structured_block (parser, save);
29862 add_stmt (finish_omp_structured_block (sb));
29864 return ret;
29867 /* OpenMP 2.5:
29868 #pragma omp for for-clause[optseq] new-line
29869 for-loop
29871 OpenMP 4.0:
29872 #pragma omp for simd for-simd-clause[optseq] new-line
29873 for-loop */
29875 #define OMP_FOR_CLAUSE_MASK \
29876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
29877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
29878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
29879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
29880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
29881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
29882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
29883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
29885 static tree
29886 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
29887 char *p_name, omp_clause_mask mask, tree *cclauses)
29889 tree clauses, sb, ret;
29890 unsigned int save;
29891 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29893 strcat (p_name, " for");
29894 mask |= OMP_FOR_CLAUSE_MASK;
29895 if (cclauses)
29896 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
29898 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29900 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29901 const char *p = IDENTIFIER_POINTER (id);
29903 if (strcmp (p, "simd") == 0)
29905 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
29906 if (cclauses == NULL)
29907 cclauses = cclauses_buf;
29909 cp_lexer_consume_token (parser->lexer);
29910 if (!flag_openmp) /* flag_openmp_simd */
29911 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29912 cclauses);
29913 sb = begin_omp_structured_block ();
29914 save = cp_parser_begin_omp_structured_block (parser);
29915 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
29916 cclauses);
29917 cp_parser_end_omp_structured_block (parser, save);
29918 tree body = finish_omp_structured_block (sb);
29919 if (ret == NULL)
29920 return ret;
29921 ret = make_node (OMP_FOR);
29922 TREE_TYPE (ret) = void_type_node;
29923 OMP_FOR_BODY (ret) = body;
29924 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29925 SET_EXPR_LOCATION (ret, loc);
29926 add_stmt (ret);
29927 return ret;
29930 if (!flag_openmp) /* flag_openmp_simd */
29932 cp_parser_require_pragma_eol (parser, pragma_tok);
29933 return NULL_TREE;
29936 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
29937 cclauses == NULL);
29938 if (cclauses)
29940 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
29941 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
29944 sb = begin_omp_structured_block ();
29945 save = cp_parser_begin_omp_structured_block (parser);
29947 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses);
29949 cp_parser_end_omp_structured_block (parser, save);
29950 add_stmt (finish_omp_structured_block (sb));
29952 return ret;
29955 /* OpenMP 2.5:
29956 # pragma omp master new-line
29957 structured-block */
29959 static tree
29960 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
29962 cp_parser_require_pragma_eol (parser, pragma_tok);
29963 return c_finish_omp_master (input_location,
29964 cp_parser_omp_structured_block (parser));
29967 /* OpenMP 2.5:
29968 # pragma omp ordered new-line
29969 structured-block */
29971 static tree
29972 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
29974 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29975 cp_parser_require_pragma_eol (parser, pragma_tok);
29976 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
29979 /* OpenMP 2.5:
29981 section-scope:
29982 { section-sequence }
29984 section-sequence:
29985 section-directive[opt] structured-block
29986 section-sequence section-directive structured-block */
29988 static tree
29989 cp_parser_omp_sections_scope (cp_parser *parser)
29991 tree stmt, substmt;
29992 bool error_suppress = false;
29993 cp_token *tok;
29995 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
29996 return NULL_TREE;
29998 stmt = push_stmt_list ();
30000 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
30002 substmt = cp_parser_omp_structured_block (parser);
30003 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30004 add_stmt (substmt);
30007 while (1)
30009 tok = cp_lexer_peek_token (parser->lexer);
30010 if (tok->type == CPP_CLOSE_BRACE)
30011 break;
30012 if (tok->type == CPP_EOF)
30013 break;
30015 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
30017 cp_lexer_consume_token (parser->lexer);
30018 cp_parser_require_pragma_eol (parser, tok);
30019 error_suppress = false;
30021 else if (!error_suppress)
30023 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
30024 error_suppress = true;
30027 substmt = cp_parser_omp_structured_block (parser);
30028 substmt = build1 (OMP_SECTION, void_type_node, substmt);
30029 add_stmt (substmt);
30031 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
30033 substmt = pop_stmt_list (stmt);
30035 stmt = make_node (OMP_SECTIONS);
30036 TREE_TYPE (stmt) = void_type_node;
30037 OMP_SECTIONS_BODY (stmt) = substmt;
30039 add_stmt (stmt);
30040 return stmt;
30043 /* OpenMP 2.5:
30044 # pragma omp sections sections-clause[optseq] newline
30045 sections-scope */
30047 #define OMP_SECTIONS_CLAUSE_MASK \
30048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
30051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30054 static tree
30055 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
30056 char *p_name, omp_clause_mask mask, tree *cclauses)
30058 tree clauses, ret;
30059 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30061 strcat (p_name, " sections");
30062 mask |= OMP_SECTIONS_CLAUSE_MASK;
30063 if (cclauses)
30064 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
30066 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30067 cclauses == NULL);
30068 if (cclauses)
30070 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
30071 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
30074 ret = cp_parser_omp_sections_scope (parser);
30075 if (ret)
30076 OMP_SECTIONS_CLAUSES (ret) = clauses;
30078 return ret;
30081 /* OpenMP 2.5:
30082 # pragma omp parallel parallel-clause[optseq] new-line
30083 structured-block
30084 # pragma omp parallel for parallel-for-clause[optseq] new-line
30085 structured-block
30086 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
30087 structured-block
30089 OpenMP 4.0:
30090 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
30091 structured-block */
30093 #define OMP_PARALLEL_CLAUSE_MASK \
30094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30099 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
30100 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
30102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
30104 static tree
30105 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
30106 char *p_name, omp_clause_mask mask, tree *cclauses)
30108 tree stmt, clauses, block;
30109 unsigned int save;
30110 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30112 strcat (p_name, " parallel");
30113 mask |= OMP_PARALLEL_CLAUSE_MASK;
30115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30117 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30118 if (cclauses == NULL)
30119 cclauses = cclauses_buf;
30121 cp_lexer_consume_token (parser->lexer);
30122 if (!flag_openmp) /* flag_openmp_simd */
30123 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30124 block = begin_omp_parallel ();
30125 save = cp_parser_begin_omp_structured_block (parser);
30126 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses);
30127 cp_parser_end_omp_structured_block (parser, save);
30128 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30129 block);
30130 if (ret == NULL_TREE)
30131 return ret;
30132 OMP_PARALLEL_COMBINED (stmt) = 1;
30133 return stmt;
30135 else if (cclauses)
30137 error_at (loc, "expected %<for%> after %qs", p_name);
30138 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30139 return NULL_TREE;
30141 else if (!flag_openmp) /* flag_openmp_simd */
30143 cp_parser_require_pragma_eol (parser, pragma_tok);
30144 return NULL_TREE;
30146 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30148 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30149 const char *p = IDENTIFIER_POINTER (id);
30150 if (strcmp (p, "sections") == 0)
30152 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30153 cclauses = cclauses_buf;
30155 cp_lexer_consume_token (parser->lexer);
30156 block = begin_omp_parallel ();
30157 save = cp_parser_begin_omp_structured_block (parser);
30158 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
30159 cp_parser_end_omp_structured_block (parser, save);
30160 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
30161 block);
30162 OMP_PARALLEL_COMBINED (stmt) = 1;
30163 return stmt;
30167 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
30169 block = begin_omp_parallel ();
30170 save = cp_parser_begin_omp_structured_block (parser);
30171 cp_parser_statement (parser, NULL_TREE, false, NULL);
30172 cp_parser_end_omp_structured_block (parser, save);
30173 stmt = finish_omp_parallel (clauses, block);
30174 return stmt;
30177 /* OpenMP 2.5:
30178 # pragma omp single single-clause[optseq] new-line
30179 structured-block */
30181 #define OMP_SINGLE_CLAUSE_MASK \
30182 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
30185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
30187 static tree
30188 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
30190 tree stmt = make_node (OMP_SINGLE);
30191 TREE_TYPE (stmt) = void_type_node;
30193 OMP_SINGLE_CLAUSES (stmt)
30194 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
30195 "#pragma omp single", pragma_tok);
30196 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
30198 return add_stmt (stmt);
30201 /* OpenMP 3.0:
30202 # pragma omp task task-clause[optseq] new-line
30203 structured-block */
30205 #define OMP_TASK_CLAUSE_MASK \
30206 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
30207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
30208 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
30209 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30210 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30211 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
30213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
30214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND))
30216 static tree
30217 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
30219 tree clauses, block;
30220 unsigned int save;
30222 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
30223 "#pragma omp task", pragma_tok);
30224 block = begin_omp_task ();
30225 save = cp_parser_begin_omp_structured_block (parser);
30226 cp_parser_statement (parser, NULL_TREE, false, NULL);
30227 cp_parser_end_omp_structured_block (parser, save);
30228 return finish_omp_task (clauses, block);
30231 /* OpenMP 3.0:
30232 # pragma omp taskwait new-line */
30234 static void
30235 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
30237 cp_parser_require_pragma_eol (parser, pragma_tok);
30238 finish_omp_taskwait ();
30241 /* OpenMP 3.1:
30242 # pragma omp taskyield new-line */
30244 static void
30245 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
30247 cp_parser_require_pragma_eol (parser, pragma_tok);
30248 finish_omp_taskyield ();
30251 /* OpenMP 4.0:
30252 # pragma omp taskgroup new-line
30253 structured-block */
30255 static tree
30256 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok)
30258 cp_parser_require_pragma_eol (parser, pragma_tok);
30259 return c_finish_omp_taskgroup (input_location,
30260 cp_parser_omp_structured_block (parser));
30264 /* OpenMP 2.5:
30265 # pragma omp threadprivate (variable-list) */
30267 static void
30268 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
30270 tree vars;
30272 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30273 cp_parser_require_pragma_eol (parser, pragma_tok);
30275 finish_omp_threadprivate (vars);
30278 /* OpenMP 4.0:
30279 # pragma omp cancel cancel-clause[optseq] new-line */
30281 #define OMP_CANCEL_CLAUSE_MASK \
30282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
30286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30288 static void
30289 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
30291 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
30292 "#pragma omp cancel", pragma_tok);
30293 finish_omp_cancel (clauses);
30296 /* OpenMP 4.0:
30297 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
30299 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
30300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
30301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
30302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
30303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
30305 static void
30306 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
30308 tree clauses;
30309 bool point_seen = false;
30311 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30313 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30314 const char *p = IDENTIFIER_POINTER (id);
30316 if (strcmp (p, "point") == 0)
30318 cp_lexer_consume_token (parser->lexer);
30319 point_seen = true;
30322 if (!point_seen)
30324 cp_parser_error (parser, "expected %<point%>");
30325 cp_parser_require_pragma_eol (parser, pragma_tok);
30326 return;
30329 clauses = cp_parser_omp_all_clauses (parser,
30330 OMP_CANCELLATION_POINT_CLAUSE_MASK,
30331 "#pragma omp cancellation point",
30332 pragma_tok);
30333 finish_omp_cancellation_point (clauses);
30336 /* OpenMP 4.0:
30337 #pragma omp distribute distribute-clause[optseq] new-line
30338 for-loop */
30340 #define OMP_DISTRIBUTE_CLAUSE_MASK \
30341 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
30344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
30346 static tree
30347 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
30348 char *p_name, omp_clause_mask mask, tree *cclauses)
30350 tree clauses, sb, ret;
30351 unsigned int save;
30352 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30354 strcat (p_name, " distribute");
30355 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
30357 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30359 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30360 const char *p = IDENTIFIER_POINTER (id);
30361 bool simd = false;
30362 bool parallel = false;
30364 if (strcmp (p, "simd") == 0)
30365 simd = true;
30366 else
30367 parallel = strcmp (p, "parallel") == 0;
30368 if (parallel || simd)
30370 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30371 if (cclauses == NULL)
30372 cclauses = cclauses_buf;
30373 cp_lexer_consume_token (parser->lexer);
30374 if (!flag_openmp) /* flag_openmp_simd */
30376 if (simd)
30377 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30378 cclauses);
30379 else
30380 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30381 cclauses);
30383 sb = begin_omp_structured_block ();
30384 save = cp_parser_begin_omp_structured_block (parser);
30385 if (simd)
30386 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
30387 cclauses);
30388 else
30389 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
30390 cclauses);
30391 cp_parser_end_omp_structured_block (parser, save);
30392 tree body = finish_omp_structured_block (sb);
30393 if (ret == NULL)
30394 return ret;
30395 ret = make_node (OMP_DISTRIBUTE);
30396 TREE_TYPE (ret) = void_type_node;
30397 OMP_FOR_BODY (ret) = body;
30398 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30399 SET_EXPR_LOCATION (ret, loc);
30400 add_stmt (ret);
30401 return ret;
30404 if (!flag_openmp) /* flag_openmp_simd */
30406 cp_parser_require_pragma_eol (parser, pragma_tok);
30407 return NULL_TREE;
30410 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30411 cclauses == NULL);
30412 if (cclauses)
30414 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
30415 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
30418 sb = begin_omp_structured_block ();
30419 save = cp_parser_begin_omp_structured_block (parser);
30421 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL);
30423 cp_parser_end_omp_structured_block (parser, save);
30424 add_stmt (finish_omp_structured_block (sb));
30426 return ret;
30429 /* OpenMP 4.0:
30430 # pragma omp teams teams-clause[optseq] new-line
30431 structured-block */
30433 #define OMP_TEAMS_CLAUSE_MASK \
30434 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
30435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
30436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
30437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
30438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
30439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
30440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
30442 static tree
30443 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
30444 char *p_name, omp_clause_mask mask, tree *cclauses)
30446 tree clauses, sb, ret;
30447 unsigned int save;
30448 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30450 strcat (p_name, " teams");
30451 mask |= OMP_TEAMS_CLAUSE_MASK;
30453 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30455 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30456 const char *p = IDENTIFIER_POINTER (id);
30457 if (strcmp (p, "distribute") == 0)
30459 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
30460 if (cclauses == NULL)
30461 cclauses = cclauses_buf;
30463 cp_lexer_consume_token (parser->lexer);
30464 if (!flag_openmp) /* flag_openmp_simd */
30465 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30466 cclauses);
30467 sb = begin_omp_structured_block ();
30468 save = cp_parser_begin_omp_structured_block (parser);
30469 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
30470 cclauses);
30471 cp_parser_end_omp_structured_block (parser, save);
30472 tree body = finish_omp_structured_block (sb);
30473 if (ret == NULL)
30474 return ret;
30475 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30476 ret = make_node (OMP_TEAMS);
30477 TREE_TYPE (ret) = void_type_node;
30478 OMP_TEAMS_CLAUSES (ret) = clauses;
30479 OMP_TEAMS_BODY (ret) = body;
30480 return add_stmt (ret);
30483 if (!flag_openmp) /* flag_openmp_simd */
30485 cp_parser_require_pragma_eol (parser, pragma_tok);
30486 return NULL_TREE;
30489 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
30490 cclauses == NULL);
30491 if (cclauses)
30493 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
30494 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
30497 tree stmt = make_node (OMP_TEAMS);
30498 TREE_TYPE (stmt) = void_type_node;
30499 OMP_TEAMS_CLAUSES (stmt) = clauses;
30500 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser);
30502 return add_stmt (stmt);
30505 /* OpenMP 4.0:
30506 # pragma omp target data target-data-clause[optseq] new-line
30507 structured-block */
30509 #define OMP_TARGET_DATA_CLAUSE_MASK \
30510 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30514 static tree
30515 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok)
30517 tree stmt = make_node (OMP_TARGET_DATA);
30518 TREE_TYPE (stmt) = void_type_node;
30520 OMP_TARGET_DATA_CLAUSES (stmt)
30521 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
30522 "#pragma omp target data", pragma_tok);
30523 keep_next_level (true);
30524 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser);
30526 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30527 return add_stmt (stmt);
30530 /* OpenMP 4.0:
30531 # pragma omp target update target-update-clause[optseq] new-line */
30533 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
30534 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
30535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
30536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30539 static bool
30540 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
30541 enum pragma_context context)
30543 if (context == pragma_stmt)
30545 error_at (pragma_tok->location,
30546 "%<#pragma omp target update%> may only be "
30547 "used in compound statements");
30548 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30549 return false;
30552 tree clauses
30553 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
30554 "#pragma omp target update", pragma_tok);
30555 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
30556 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
30558 error_at (pragma_tok->location,
30559 "%<#pragma omp target update must contain at least one "
30560 "%<from%> or %<to%> clauses");
30561 return false;
30564 tree stmt = make_node (OMP_TARGET_UPDATE);
30565 TREE_TYPE (stmt) = void_type_node;
30566 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
30567 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30568 add_stmt (stmt);
30569 return false;
30572 /* OpenMP 4.0:
30573 # pragma omp target target-clause[optseq] new-line
30574 structured-block */
30576 #define OMP_TARGET_CLAUSE_MASK \
30577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
30578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
30579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
30581 static bool
30582 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
30583 enum pragma_context context)
30585 if (context != pragma_stmt && context != pragma_compound)
30587 cp_parser_error (parser, "expected declaration specifiers");
30588 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30589 return false;
30592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30594 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30595 const char *p = IDENTIFIER_POINTER (id);
30597 if (strcmp (p, "teams") == 0)
30599 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
30600 char p_name[sizeof ("#pragma omp target teams distribute "
30601 "parallel for simd")];
30603 cp_lexer_consume_token (parser->lexer);
30604 strcpy (p_name, "#pragma omp target");
30605 if (!flag_openmp) /* flag_openmp_simd */
30607 tree stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
30608 OMP_TARGET_CLAUSE_MASK,
30609 cclauses);
30610 return stmt != NULL_TREE;
30612 keep_next_level (true);
30613 tree sb = begin_omp_structured_block ();
30614 unsigned save = cp_parser_begin_omp_structured_block (parser);
30615 tree ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
30616 OMP_TARGET_CLAUSE_MASK, cclauses);
30617 cp_parser_end_omp_structured_block (parser, save);
30618 tree body = finish_omp_structured_block (sb);
30619 if (ret == NULL_TREE)
30620 return false;
30621 tree stmt = make_node (OMP_TARGET);
30622 TREE_TYPE (stmt) = void_type_node;
30623 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
30624 OMP_TARGET_BODY (stmt) = body;
30625 add_stmt (stmt);
30626 return true;
30628 else if (!flag_openmp) /* flag_openmp_simd */
30630 cp_parser_require_pragma_eol (parser, pragma_tok);
30631 return false;
30633 else if (strcmp (p, "data") == 0)
30635 cp_lexer_consume_token (parser->lexer);
30636 cp_parser_omp_target_data (parser, pragma_tok);
30637 return true;
30639 else if (strcmp (p, "update") == 0)
30641 cp_lexer_consume_token (parser->lexer);
30642 return cp_parser_omp_target_update (parser, pragma_tok, context);
30646 tree stmt = make_node (OMP_TARGET);
30647 TREE_TYPE (stmt) = void_type_node;
30649 OMP_TARGET_CLAUSES (stmt)
30650 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
30651 "#pragma omp target", pragma_tok);
30652 keep_next_level (true);
30653 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser);
30655 SET_EXPR_LOCATION (stmt, pragma_tok->location);
30656 add_stmt (stmt);
30657 return true;
30660 /* OpenMP 4.0:
30661 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
30663 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
30664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
30665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
30666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
30667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
30668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
30669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
30671 static void
30672 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
30673 enum pragma_context context)
30675 bool first_p = parser->omp_declare_simd == NULL;
30676 cp_omp_declare_simd_data data;
30677 if (first_p)
30679 data.error_seen = false;
30680 data.fndecl_seen = false;
30681 data.tokens = vNULL;
30682 parser->omp_declare_simd = &data;
30684 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
30685 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
30686 cp_lexer_consume_token (parser->lexer);
30687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
30688 parser->omp_declare_simd->error_seen = true;
30689 cp_parser_require_pragma_eol (parser, pragma_tok);
30690 struct cp_token_cache *cp
30691 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
30692 parser->omp_declare_simd->tokens.safe_push (cp);
30693 if (first_p)
30695 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
30696 cp_parser_pragma (parser, context);
30697 switch (context)
30699 case pragma_external:
30700 cp_parser_declaration (parser);
30701 break;
30702 case pragma_member:
30703 cp_parser_member_declaration (parser);
30704 break;
30705 case pragma_objc_icode:
30706 cp_parser_block_declaration (parser, /*statement_p=*/false);
30707 break;
30708 default:
30709 cp_parser_declaration_statement (parser);
30710 break;
30712 if (parser->omp_declare_simd
30713 && !parser->omp_declare_simd->error_seen
30714 && !parser->omp_declare_simd->fndecl_seen)
30715 error_at (pragma_tok->location,
30716 "%<#pragma omp declare simd%> not immediately followed by "
30717 "function declaration or definition");
30718 data.tokens.release ();
30719 parser->omp_declare_simd = NULL;
30723 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
30724 This function is modelled similar to the late parsing of omp declare
30725 simd. */
30727 static tree
30728 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
30730 struct cp_token_cache *ce;
30731 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
30732 int ii = 0;
30734 if (parser->omp_declare_simd != NULL)
30736 error ("%<#pragma omp declare simd%> cannot be used in the same function"
30737 " marked as a Cilk Plus SIMD-enabled function");
30738 XDELETE (parser->cilk_simd_fn_info);
30739 parser->cilk_simd_fn_info = NULL;
30740 return attrs;
30742 if (!info->error_seen && info->fndecl_seen)
30744 error ("vector attribute not immediately followed by a single function"
30745 " declaration or definition");
30746 info->error_seen = true;
30748 if (info->error_seen)
30749 return attrs;
30751 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
30753 tree c, cl;
30755 cp_parser_push_lexer_for_tokens (parser, ce);
30756 parser->lexer->in_pragma = true;
30757 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
30758 "SIMD-enabled functions attribute",
30759 NULL);
30760 cp_parser_pop_lexer (parser);
30761 if (cl)
30762 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30764 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
30765 TREE_CHAIN (c) = attrs;
30766 attrs = c;
30768 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30769 TREE_CHAIN (c) = attrs;
30770 if (processing_template_decl)
30771 ATTR_IS_DEPENDENT (c) = 1;
30772 attrs = c;
30774 info->fndecl_seen = true;
30775 XDELETE (parser->cilk_simd_fn_info);
30776 parser->cilk_simd_fn_info = NULL;
30777 return attrs;
30780 /* Finalize #pragma omp declare simd clauses after direct declarator has
30781 been parsed, and put that into "omp declare simd" attribute. */
30783 static tree
30784 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
30786 struct cp_token_cache *ce;
30787 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
30788 int i;
30790 if (!data->error_seen && data->fndecl_seen)
30792 error ("%<#pragma omp declare simd%> not immediately followed by "
30793 "a single function declaration or definition");
30794 data->error_seen = true;
30795 return attrs;
30797 if (data->error_seen)
30798 return attrs;
30800 FOR_EACH_VEC_ELT (data->tokens, i, ce)
30802 tree c, cl;
30804 cp_parser_push_lexer_for_tokens (parser, ce);
30805 parser->lexer->in_pragma = true;
30806 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
30807 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
30808 cp_lexer_consume_token (parser->lexer);
30809 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
30810 "#pragma omp declare simd", pragma_tok);
30811 cp_parser_pop_lexer (parser);
30812 if (cl)
30813 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
30814 c = build_tree_list (get_identifier ("omp declare simd"), cl);
30815 TREE_CHAIN (c) = attrs;
30816 if (processing_template_decl)
30817 ATTR_IS_DEPENDENT (c) = 1;
30818 attrs = c;
30821 data->fndecl_seen = true;
30822 return attrs;
30826 /* OpenMP 4.0:
30827 # pragma omp declare target new-line
30828 declarations and definitions
30829 # pragma omp end declare target new-line */
30831 static void
30832 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
30834 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30835 scope_chain->omp_declare_target_attribute++;
30838 static void
30839 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
30841 const char *p = "";
30842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30845 p = IDENTIFIER_POINTER (id);
30847 if (strcmp (p, "declare") == 0)
30849 cp_lexer_consume_token (parser->lexer);
30850 p = "";
30851 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30853 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30854 p = IDENTIFIER_POINTER (id);
30856 if (strcmp (p, "target") == 0)
30857 cp_lexer_consume_token (parser->lexer);
30858 else
30860 cp_parser_error (parser, "expected %<target%>");
30861 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30862 return;
30865 else
30867 cp_parser_error (parser, "expected %<declare%>");
30868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30869 return;
30871 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
30872 if (!scope_chain->omp_declare_target_attribute)
30873 error_at (pragma_tok->location,
30874 "%<#pragma omp end declare target%> without corresponding "
30875 "%<#pragma omp declare target%>");
30876 else
30877 scope_chain->omp_declare_target_attribute--;
30880 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
30881 expression and optional initializer clause of
30882 #pragma omp declare reduction. We store the expression(s) as
30883 either 3, 6 or 7 special statements inside of the artificial function's
30884 body. The first two statements are DECL_EXPRs for the artificial
30885 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
30886 expression that uses those variables.
30887 If there was any INITIALIZER clause, this is followed by further statements,
30888 the fourth and fifth statements are DECL_EXPRs for the artificial
30889 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
30890 constructor variant (first token after open paren is not omp_priv),
30891 then the sixth statement is a statement with the function call expression
30892 that uses the OMP_PRIV and optionally OMP_ORIG variable.
30893 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
30894 to initialize the OMP_PRIV artificial variable and there is seventh
30895 statement, a DECL_EXPR of the OMP_PRIV statement again. */
30897 static bool
30898 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
30900 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
30901 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
30902 type = TREE_TYPE (type);
30903 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
30904 DECL_ARTIFICIAL (omp_out) = 1;
30905 pushdecl (omp_out);
30906 add_decl_expr (omp_out);
30907 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
30908 DECL_ARTIFICIAL (omp_in) = 1;
30909 pushdecl (omp_in);
30910 add_decl_expr (omp_in);
30911 tree combiner;
30912 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
30914 keep_next_level (true);
30915 tree block = begin_omp_structured_block ();
30916 combiner = cp_parser_expression (parser);
30917 finish_expr_stmt (combiner);
30918 block = finish_omp_structured_block (block);
30919 add_stmt (block);
30921 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30922 return false;
30924 const char *p = "";
30925 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30927 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30928 p = IDENTIFIER_POINTER (id);
30931 if (strcmp (p, "initializer") == 0)
30933 cp_lexer_consume_token (parser->lexer);
30934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30935 return false;
30937 p = "";
30938 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30940 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30941 p = IDENTIFIER_POINTER (id);
30944 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
30945 DECL_ARTIFICIAL (omp_priv) = 1;
30946 pushdecl (omp_priv);
30947 add_decl_expr (omp_priv);
30948 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
30949 DECL_ARTIFICIAL (omp_orig) = 1;
30950 pushdecl (omp_orig);
30951 add_decl_expr (omp_orig);
30953 keep_next_level (true);
30954 block = begin_omp_structured_block ();
30956 bool ctor = false;
30957 if (strcmp (p, "omp_priv") == 0)
30959 bool is_direct_init, is_non_constant_init;
30960 ctor = true;
30961 cp_lexer_consume_token (parser->lexer);
30962 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
30963 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
30964 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30965 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
30966 == CPP_CLOSE_PAREN
30967 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
30968 == CPP_CLOSE_PAREN))
30970 finish_omp_structured_block (block);
30971 error ("invalid initializer clause");
30972 return false;
30974 initializer = cp_parser_initializer (parser, &is_direct_init,
30975 &is_non_constant_init);
30976 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
30977 NULL_TREE, LOOKUP_ONLYCONVERTING);
30979 else
30981 cp_parser_parse_tentatively (parser);
30982 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
30983 /*check_dependency_p=*/true,
30984 /*template_p=*/NULL,
30985 /*declarator_p=*/false,
30986 /*optional_p=*/false);
30987 vec<tree, va_gc> *args;
30988 if (fn_name == error_mark_node
30989 || cp_parser_error_occurred (parser)
30990 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
30991 || ((args = cp_parser_parenthesized_expression_list
30992 (parser, non_attr, /*cast_p=*/false,
30993 /*allow_expansion_p=*/true,
30994 /*non_constant_p=*/NULL)),
30995 cp_parser_error_occurred (parser)))
30997 finish_omp_structured_block (block);
30998 cp_parser_abort_tentative_parse (parser);
30999 cp_parser_error (parser, "expected id-expression (arguments)");
31000 return false;
31002 unsigned int i;
31003 tree arg;
31004 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
31005 if (arg == omp_priv
31006 || (TREE_CODE (arg) == ADDR_EXPR
31007 && TREE_OPERAND (arg, 0) == omp_priv))
31008 break;
31009 cp_parser_abort_tentative_parse (parser);
31010 if (arg == NULL_TREE)
31011 error ("one of the initializer call arguments should be %<omp_priv%>"
31012 " or %<&omp_priv%>");
31013 initializer = cp_parser_postfix_expression (parser, false, false, false,
31014 false, NULL);
31015 finish_expr_stmt (initializer);
31018 block = finish_omp_structured_block (block);
31019 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
31020 finish_expr_stmt (block);
31022 if (ctor)
31023 add_decl_expr (omp_orig);
31025 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31026 return false;
31029 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
31030 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
31032 return true;
31035 /* OpenMP 4.0
31036 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31037 initializer-clause[opt] new-line
31039 initializer-clause:
31040 initializer (omp_priv initializer)
31041 initializer (function-name (argument-list)) */
31043 static void
31044 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
31045 enum pragma_context)
31047 auto_vec<tree> types;
31048 enum tree_code reduc_code = ERROR_MARK;
31049 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
31050 unsigned int i;
31051 cp_token *first_token;
31052 cp_token_cache *cp;
31053 int errs;
31054 void *p;
31056 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
31057 p = obstack_alloc (&declarator_obstack, 0);
31059 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31060 goto fail;
31062 switch (cp_lexer_peek_token (parser->lexer)->type)
31064 case CPP_PLUS:
31065 reduc_code = PLUS_EXPR;
31066 break;
31067 case CPP_MULT:
31068 reduc_code = MULT_EXPR;
31069 break;
31070 case CPP_MINUS:
31071 reduc_code = MINUS_EXPR;
31072 break;
31073 case CPP_AND:
31074 reduc_code = BIT_AND_EXPR;
31075 break;
31076 case CPP_XOR:
31077 reduc_code = BIT_XOR_EXPR;
31078 break;
31079 case CPP_OR:
31080 reduc_code = BIT_IOR_EXPR;
31081 break;
31082 case CPP_AND_AND:
31083 reduc_code = TRUTH_ANDIF_EXPR;
31084 break;
31085 case CPP_OR_OR:
31086 reduc_code = TRUTH_ORIF_EXPR;
31087 break;
31088 case CPP_NAME:
31089 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
31090 break;
31091 default:
31092 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
31093 "%<|%>, %<&&%>, %<||%> or identifier");
31094 goto fail;
31097 if (reduc_code != ERROR_MARK)
31098 cp_lexer_consume_token (parser->lexer);
31100 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
31101 if (reduc_id == error_mark_node)
31102 goto fail;
31104 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31105 goto fail;
31107 /* Types may not be defined in declare reduction type list. */
31108 const char *saved_message;
31109 saved_message = parser->type_definition_forbidden_message;
31110 parser->type_definition_forbidden_message
31111 = G_("types may not be defined in declare reduction type list");
31112 bool saved_colon_corrects_to_scope_p;
31113 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31114 parser->colon_corrects_to_scope_p = false;
31115 bool saved_colon_doesnt_start_class_def_p;
31116 saved_colon_doesnt_start_class_def_p
31117 = parser->colon_doesnt_start_class_def_p;
31118 parser->colon_doesnt_start_class_def_p = true;
31120 while (true)
31122 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31123 type = cp_parser_type_id (parser);
31124 if (type == error_mark_node)
31126 else if (ARITHMETIC_TYPE_P (type)
31127 && (orig_reduc_id == NULL_TREE
31128 || (TREE_CODE (type) != COMPLEX_TYPE
31129 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31130 "min") == 0
31131 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
31132 "max") == 0))))
31133 error_at (loc, "predeclared arithmetic type %qT in "
31134 "%<#pragma omp declare reduction%>", type);
31135 else if (TREE_CODE (type) == FUNCTION_TYPE
31136 || TREE_CODE (type) == METHOD_TYPE
31137 || TREE_CODE (type) == ARRAY_TYPE)
31138 error_at (loc, "function or array type %qT in "
31139 "%<#pragma omp declare reduction%>", type);
31140 else if (TREE_CODE (type) == REFERENCE_TYPE)
31141 error_at (loc, "reference type %qT in "
31142 "%<#pragma omp declare reduction%>", type);
31143 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
31144 error_at (loc, "const, volatile or __restrict qualified type %qT in "
31145 "%<#pragma omp declare reduction%>", type);
31146 else
31147 types.safe_push (type);
31149 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31150 cp_lexer_consume_token (parser->lexer);
31151 else
31152 break;
31155 /* Restore the saved message. */
31156 parser->type_definition_forbidden_message = saved_message;
31157 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31158 parser->colon_doesnt_start_class_def_p
31159 = saved_colon_doesnt_start_class_def_p;
31161 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
31162 || types.is_empty ())
31164 fail:
31165 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31166 goto done;
31169 first_token = cp_lexer_peek_token (parser->lexer);
31170 cp = NULL;
31171 errs = errorcount;
31172 FOR_EACH_VEC_ELT (types, i, type)
31174 tree fntype
31175 = build_function_type_list (void_type_node,
31176 cp_build_reference_type (type, false),
31177 NULL_TREE);
31178 tree this_reduc_id = reduc_id;
31179 if (!dependent_type_p (type))
31180 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
31181 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
31182 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
31183 DECL_ARTIFICIAL (fndecl) = 1;
31184 DECL_EXTERNAL (fndecl) = 1;
31185 DECL_DECLARED_INLINE_P (fndecl) = 1;
31186 DECL_IGNORED_P (fndecl) = 1;
31187 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
31188 DECL_ATTRIBUTES (fndecl)
31189 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
31190 DECL_ATTRIBUTES (fndecl));
31191 if (processing_template_decl)
31192 fndecl = push_template_decl (fndecl);
31193 bool block_scope = false;
31194 tree block = NULL_TREE;
31195 if (current_function_decl)
31197 block_scope = true;
31198 DECL_CONTEXT (fndecl) = global_namespace;
31199 if (!processing_template_decl)
31200 pushdecl (fndecl);
31202 else if (current_class_type)
31204 if (cp == NULL)
31206 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
31207 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
31208 cp_lexer_consume_token (parser->lexer);
31209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
31210 goto fail;
31211 cp = cp_token_cache_new (first_token,
31212 cp_lexer_peek_nth_token (parser->lexer,
31213 2));
31215 DECL_STATIC_FUNCTION_P (fndecl) = 1;
31216 finish_member_declaration (fndecl);
31217 DECL_PENDING_INLINE_INFO (fndecl) = cp;
31218 DECL_PENDING_INLINE_P (fndecl) = 1;
31219 vec_safe_push (unparsed_funs_with_definitions, fndecl);
31220 continue;
31222 else
31224 DECL_CONTEXT (fndecl) = current_namespace;
31225 pushdecl (fndecl);
31227 if (!block_scope)
31228 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
31229 else
31230 block = begin_omp_structured_block ();
31231 if (cp)
31233 cp_parser_push_lexer_for_tokens (parser, cp);
31234 parser->lexer->in_pragma = true;
31236 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
31238 if (!block_scope)
31239 finish_function (0);
31240 else
31241 DECL_CONTEXT (fndecl) = current_function_decl;
31242 if (cp)
31243 cp_parser_pop_lexer (parser);
31244 goto fail;
31246 if (cp)
31247 cp_parser_pop_lexer (parser);
31248 if (!block_scope)
31249 finish_function (0);
31250 else
31252 DECL_CONTEXT (fndecl) = current_function_decl;
31253 block = finish_omp_structured_block (block);
31254 if (TREE_CODE (block) == BIND_EXPR)
31255 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
31256 else if (TREE_CODE (block) == STATEMENT_LIST)
31257 DECL_SAVED_TREE (fndecl) = block;
31258 if (processing_template_decl)
31259 add_decl_expr (fndecl);
31261 cp_check_omp_declare_reduction (fndecl);
31262 if (cp == NULL && types.length () > 1)
31263 cp = cp_token_cache_new (first_token,
31264 cp_lexer_peek_nth_token (parser->lexer, 2));
31265 if (errs != errorcount)
31266 break;
31269 cp_parser_require_pragma_eol (parser, pragma_tok);
31271 done:
31272 /* Free any declarators allocated. */
31273 obstack_free (&declarator_obstack, p);
31276 /* OpenMP 4.0
31277 #pragma omp declare simd declare-simd-clauses[optseq] new-line
31278 #pragma omp declare reduction (reduction-id : typename-list : expression) \
31279 initializer-clause[opt] new-line
31280 #pragma omp declare target new-line */
31282 static void
31283 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
31284 enum pragma_context context)
31286 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31288 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31289 const char *p = IDENTIFIER_POINTER (id);
31291 if (strcmp (p, "simd") == 0)
31293 cp_lexer_consume_token (parser->lexer);
31294 cp_parser_omp_declare_simd (parser, pragma_tok,
31295 context);
31296 return;
31298 cp_ensure_no_omp_declare_simd (parser);
31299 if (strcmp (p, "reduction") == 0)
31301 cp_lexer_consume_token (parser->lexer);
31302 cp_parser_omp_declare_reduction (parser, pragma_tok,
31303 context);
31304 return;
31306 if (!flag_openmp) /* flag_openmp_simd */
31308 cp_parser_require_pragma_eol (parser, pragma_tok);
31309 return;
31311 if (strcmp (p, "target") == 0)
31313 cp_lexer_consume_token (parser->lexer);
31314 cp_parser_omp_declare_target (parser, pragma_tok);
31315 return;
31318 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
31319 "or %<target%>");
31320 cp_parser_require_pragma_eol (parser, pragma_tok);
31323 /* Main entry point to OpenMP statement pragmas. */
31325 static void
31326 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
31328 tree stmt;
31329 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
31330 omp_clause_mask mask (0);
31332 switch (pragma_tok->pragma_kind)
31334 case PRAGMA_OMP_ATOMIC:
31335 cp_parser_omp_atomic (parser, pragma_tok);
31336 return;
31337 case PRAGMA_OMP_CRITICAL:
31338 stmt = cp_parser_omp_critical (parser, pragma_tok);
31339 break;
31340 case PRAGMA_OMP_DISTRIBUTE:
31341 strcpy (p_name, "#pragma omp");
31342 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL);
31343 break;
31344 case PRAGMA_OMP_FOR:
31345 strcpy (p_name, "#pragma omp");
31346 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL);
31347 break;
31348 case PRAGMA_OMP_MASTER:
31349 stmt = cp_parser_omp_master (parser, pragma_tok);
31350 break;
31351 case PRAGMA_OMP_ORDERED:
31352 stmt = cp_parser_omp_ordered (parser, pragma_tok);
31353 break;
31354 case PRAGMA_OMP_PARALLEL:
31355 strcpy (p_name, "#pragma omp");
31356 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL);
31357 break;
31358 case PRAGMA_OMP_SECTIONS:
31359 strcpy (p_name, "#pragma omp");
31360 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
31361 break;
31362 case PRAGMA_OMP_SIMD:
31363 strcpy (p_name, "#pragma omp");
31364 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL);
31365 break;
31366 case PRAGMA_OMP_SINGLE:
31367 stmt = cp_parser_omp_single (parser, pragma_tok);
31368 break;
31369 case PRAGMA_OMP_TASK:
31370 stmt = cp_parser_omp_task (parser, pragma_tok);
31371 break;
31372 case PRAGMA_OMP_TASKGROUP:
31373 stmt = cp_parser_omp_taskgroup (parser, pragma_tok);
31374 break;
31375 case PRAGMA_OMP_TEAMS:
31376 strcpy (p_name, "#pragma omp");
31377 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL);
31378 break;
31379 default:
31380 gcc_unreachable ();
31383 if (stmt)
31384 SET_EXPR_LOCATION (stmt, pragma_tok->location);
31387 /* Transactional Memory parsing routines. */
31389 /* Parse a transaction attribute.
31391 txn-attribute:
31392 attribute
31393 [ [ identifier ] ]
31395 ??? Simplify this when C++0x bracket attributes are
31396 implemented properly. */
31398 static tree
31399 cp_parser_txn_attribute_opt (cp_parser *parser)
31401 cp_token *token;
31402 tree attr_name, attr = NULL;
31404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
31405 return cp_parser_attributes_opt (parser);
31407 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
31408 return NULL_TREE;
31409 cp_lexer_consume_token (parser->lexer);
31410 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
31411 goto error1;
31413 token = cp_lexer_peek_token (parser->lexer);
31414 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
31416 token = cp_lexer_consume_token (parser->lexer);
31418 attr_name = (token->type == CPP_KEYWORD
31419 /* For keywords, use the canonical spelling,
31420 not the parsed identifier. */
31421 ? ridpointers[(int) token->keyword]
31422 : token->u.value);
31423 attr = build_tree_list (attr_name, NULL_TREE);
31425 else
31426 cp_parser_error (parser, "expected identifier");
31428 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31429 error1:
31430 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
31431 return attr;
31434 /* Parse a __transaction_atomic or __transaction_relaxed statement.
31436 transaction-statement:
31437 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
31438 compound-statement
31439 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
31442 static tree
31443 cp_parser_transaction (cp_parser *parser, enum rid keyword)
31445 unsigned char old_in = parser->in_transaction;
31446 unsigned char this_in = 1, new_in;
31447 cp_token *token;
31448 tree stmt, attrs, noex;
31450 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31451 || keyword == RID_TRANSACTION_RELAXED);
31452 token = cp_parser_require_keyword (parser, keyword,
31453 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31454 : RT_TRANSACTION_RELAXED));
31455 gcc_assert (token != NULL);
31457 if (keyword == RID_TRANSACTION_RELAXED)
31458 this_in |= TM_STMT_ATTR_RELAXED;
31459 else
31461 attrs = cp_parser_txn_attribute_opt (parser);
31462 if (attrs)
31463 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31466 /* Parse a noexcept specification. */
31467 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
31469 /* Keep track if we're in the lexical scope of an outer transaction. */
31470 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
31472 stmt = begin_transaction_stmt (token->location, NULL, this_in);
31474 parser->in_transaction = new_in;
31475 cp_parser_compound_statement (parser, NULL, false, false);
31476 parser->in_transaction = old_in;
31478 finish_transaction_stmt (stmt, NULL, this_in, noex);
31480 return stmt;
31483 /* Parse a __transaction_atomic or __transaction_relaxed expression.
31485 transaction-expression:
31486 __transaction_atomic txn-noexcept-spec[opt] ( expression )
31487 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
31490 static tree
31491 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
31493 unsigned char old_in = parser->in_transaction;
31494 unsigned char this_in = 1;
31495 cp_token *token;
31496 tree expr, noex;
31497 bool noex_expr;
31499 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31500 || keyword == RID_TRANSACTION_RELAXED);
31502 if (!flag_tm)
31503 error (keyword == RID_TRANSACTION_RELAXED
31504 ? G_("%<__transaction_relaxed%> without transactional memory "
31505 "support enabled")
31506 : G_("%<__transaction_atomic%> without transactional memory "
31507 "support enabled"));
31509 token = cp_parser_require_keyword (parser, keyword,
31510 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31511 : RT_TRANSACTION_RELAXED));
31512 gcc_assert (token != NULL);
31514 if (keyword == RID_TRANSACTION_RELAXED)
31515 this_in |= TM_STMT_ATTR_RELAXED;
31517 /* Set this early. This might mean that we allow transaction_cancel in
31518 an expression that we find out later actually has to be a constexpr.
31519 However, we expect that cxx_constant_value will be able to deal with
31520 this; also, if the noexcept has no constexpr, then what we parse next
31521 really is a transaction's body. */
31522 parser->in_transaction = this_in;
31524 /* Parse a noexcept specification. */
31525 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
31526 true);
31528 if (!noex || !noex_expr
31529 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
31531 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
31533 expr = cp_parser_expression (parser);
31534 expr = finish_parenthesized_expr (expr);
31536 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
31538 else
31540 /* The only expression that is available got parsed for the noexcept
31541 already. noexcept is true then. */
31542 expr = noex;
31543 noex = boolean_true_node;
31546 expr = build_transaction_expr (token->location, expr, this_in, noex);
31547 parser->in_transaction = old_in;
31549 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
31550 return error_mark_node;
31552 return (flag_tm ? expr : error_mark_node);
31555 /* Parse a function-transaction-block.
31557 function-transaction-block:
31558 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
31559 function-body
31560 __transaction_atomic txn-attribute[opt] function-try-block
31561 __transaction_relaxed ctor-initializer[opt] function-body
31562 __transaction_relaxed function-try-block
31565 static bool
31566 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
31568 unsigned char old_in = parser->in_transaction;
31569 unsigned char new_in = 1;
31570 tree compound_stmt, stmt, attrs;
31571 bool ctor_initializer_p;
31572 cp_token *token;
31574 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
31575 || keyword == RID_TRANSACTION_RELAXED);
31576 token = cp_parser_require_keyword (parser, keyword,
31577 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
31578 : RT_TRANSACTION_RELAXED));
31579 gcc_assert (token != NULL);
31581 if (keyword == RID_TRANSACTION_RELAXED)
31582 new_in |= TM_STMT_ATTR_RELAXED;
31583 else
31585 attrs = cp_parser_txn_attribute_opt (parser);
31586 if (attrs)
31587 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
31590 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
31592 parser->in_transaction = new_in;
31594 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31595 ctor_initializer_p = cp_parser_function_try_block (parser);
31596 else
31597 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
31598 (parser, /*in_function_try_block=*/false);
31600 parser->in_transaction = old_in;
31602 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
31604 return ctor_initializer_p;
31607 /* Parse a __transaction_cancel statement.
31609 cancel-statement:
31610 __transaction_cancel txn-attribute[opt] ;
31611 __transaction_cancel txn-attribute[opt] throw-expression ;
31613 ??? Cancel and throw is not yet implemented. */
31615 static tree
31616 cp_parser_transaction_cancel (cp_parser *parser)
31618 cp_token *token;
31619 bool is_outer = false;
31620 tree stmt, attrs;
31622 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
31623 RT_TRANSACTION_CANCEL);
31624 gcc_assert (token != NULL);
31626 attrs = cp_parser_txn_attribute_opt (parser);
31627 if (attrs)
31628 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
31630 /* ??? Parse cancel-and-throw here. */
31632 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
31634 if (!flag_tm)
31636 error_at (token->location, "%<__transaction_cancel%> without "
31637 "transactional memory support enabled");
31638 return error_mark_node;
31640 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
31642 error_at (token->location, "%<__transaction_cancel%> within a "
31643 "%<__transaction_relaxed%>");
31644 return error_mark_node;
31646 else if (is_outer)
31648 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
31649 && !is_tm_may_cancel_outer (current_function_decl))
31651 error_at (token->location, "outer %<__transaction_cancel%> not "
31652 "within outer %<__transaction_atomic%>");
31653 error_at (token->location,
31654 " or a %<transaction_may_cancel_outer%> function");
31655 return error_mark_node;
31658 else if (parser->in_transaction == 0)
31660 error_at (token->location, "%<__transaction_cancel%> not within "
31661 "%<__transaction_atomic%>");
31662 return error_mark_node;
31665 stmt = build_tm_abort_call (token->location, is_outer);
31666 add_stmt (stmt);
31668 return stmt;
31671 /* The parser. */
31673 static GTY (()) cp_parser *the_parser;
31676 /* Special handling for the first token or line in the file. The first
31677 thing in the file might be #pragma GCC pch_preprocess, which loads a
31678 PCH file, which is a GC collection point. So we need to handle this
31679 first pragma without benefit of an existing lexer structure.
31681 Always returns one token to the caller in *FIRST_TOKEN. This is
31682 either the true first token of the file, or the first token after
31683 the initial pragma. */
31685 static void
31686 cp_parser_initial_pragma (cp_token *first_token)
31688 tree name = NULL;
31690 cp_lexer_get_preprocessor_token (NULL, first_token);
31691 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
31692 return;
31694 cp_lexer_get_preprocessor_token (NULL, first_token);
31695 if (first_token->type == CPP_STRING)
31697 name = first_token->u.value;
31699 cp_lexer_get_preprocessor_token (NULL, first_token);
31700 if (first_token->type != CPP_PRAGMA_EOL)
31701 error_at (first_token->location,
31702 "junk at end of %<#pragma GCC pch_preprocess%>");
31704 else
31705 error_at (first_token->location, "expected string literal");
31707 /* Skip to the end of the pragma. */
31708 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
31709 cp_lexer_get_preprocessor_token (NULL, first_token);
31711 /* Now actually load the PCH file. */
31712 if (name)
31713 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
31715 /* Read one more token to return to our caller. We have to do this
31716 after reading the PCH file in, since its pointers have to be
31717 live. */
31718 cp_lexer_get_preprocessor_token (NULL, first_token);
31721 /* Parses the grainsize pragma for the _Cilk_for statement.
31722 Syntax:
31723 #pragma cilk grainsize = <VALUE>. */
31725 static void
31726 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok)
31728 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
31730 tree exp = cp_parser_binary_expression (parser, false, false,
31731 PREC_NOT_OPERATOR, NULL);
31732 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31733 if (!exp || exp == error_mark_node)
31735 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
31736 return;
31739 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
31740 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
31741 cp_parser_cilk_for (parser, exp);
31742 else
31743 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
31744 "%<#pragma cilk grainsize%> is not followed by "
31745 "%<_Cilk_for%>");
31746 return;
31748 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31751 /* Normal parsing of a pragma token. Here we can (and must) use the
31752 regular lexer. */
31754 static bool
31755 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
31757 cp_token *pragma_tok;
31758 unsigned int id;
31760 pragma_tok = cp_lexer_consume_token (parser->lexer);
31761 gcc_assert (pragma_tok->type == CPP_PRAGMA);
31762 parser->lexer->in_pragma = true;
31764 id = pragma_tok->pragma_kind;
31765 if (id != PRAGMA_OMP_DECLARE_REDUCTION)
31766 cp_ensure_no_omp_declare_simd (parser);
31767 switch (id)
31769 case PRAGMA_GCC_PCH_PREPROCESS:
31770 error_at (pragma_tok->location,
31771 "%<#pragma GCC pch_preprocess%> must be first");
31772 break;
31774 case PRAGMA_OMP_BARRIER:
31775 switch (context)
31777 case pragma_compound:
31778 cp_parser_omp_barrier (parser, pragma_tok);
31779 return false;
31780 case pragma_stmt:
31781 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
31782 "used in compound statements");
31783 break;
31784 default:
31785 goto bad_stmt;
31787 break;
31789 case PRAGMA_OMP_FLUSH:
31790 switch (context)
31792 case pragma_compound:
31793 cp_parser_omp_flush (parser, pragma_tok);
31794 return false;
31795 case pragma_stmt:
31796 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
31797 "used in compound statements");
31798 break;
31799 default:
31800 goto bad_stmt;
31802 break;
31804 case PRAGMA_OMP_TASKWAIT:
31805 switch (context)
31807 case pragma_compound:
31808 cp_parser_omp_taskwait (parser, pragma_tok);
31809 return false;
31810 case pragma_stmt:
31811 error_at (pragma_tok->location,
31812 "%<#pragma omp taskwait%> may only be "
31813 "used in compound statements");
31814 break;
31815 default:
31816 goto bad_stmt;
31818 break;
31820 case PRAGMA_OMP_TASKYIELD:
31821 switch (context)
31823 case pragma_compound:
31824 cp_parser_omp_taskyield (parser, pragma_tok);
31825 return false;
31826 case pragma_stmt:
31827 error_at (pragma_tok->location,
31828 "%<#pragma omp taskyield%> may only be "
31829 "used in compound statements");
31830 break;
31831 default:
31832 goto bad_stmt;
31834 break;
31836 case PRAGMA_OMP_CANCEL:
31837 switch (context)
31839 case pragma_compound:
31840 cp_parser_omp_cancel (parser, pragma_tok);
31841 return false;
31842 case pragma_stmt:
31843 error_at (pragma_tok->location,
31844 "%<#pragma omp cancel%> may only be "
31845 "used in compound statements");
31846 break;
31847 default:
31848 goto bad_stmt;
31850 break;
31852 case PRAGMA_OMP_CANCELLATION_POINT:
31853 switch (context)
31855 case pragma_compound:
31856 cp_parser_omp_cancellation_point (parser, pragma_tok);
31857 return false;
31858 case pragma_stmt:
31859 error_at (pragma_tok->location,
31860 "%<#pragma omp cancellation point%> may only be "
31861 "used in compound statements");
31862 break;
31863 default:
31864 goto bad_stmt;
31866 break;
31868 case PRAGMA_OMP_THREADPRIVATE:
31869 cp_parser_omp_threadprivate (parser, pragma_tok);
31870 return false;
31872 case PRAGMA_OMP_DECLARE_REDUCTION:
31873 cp_parser_omp_declare (parser, pragma_tok, context);
31874 return false;
31876 case PRAGMA_OMP_ATOMIC:
31877 case PRAGMA_OMP_CRITICAL:
31878 case PRAGMA_OMP_DISTRIBUTE:
31879 case PRAGMA_OMP_FOR:
31880 case PRAGMA_OMP_MASTER:
31881 case PRAGMA_OMP_ORDERED:
31882 case PRAGMA_OMP_PARALLEL:
31883 case PRAGMA_OMP_SECTIONS:
31884 case PRAGMA_OMP_SIMD:
31885 case PRAGMA_OMP_SINGLE:
31886 case PRAGMA_OMP_TASK:
31887 case PRAGMA_OMP_TASKGROUP:
31888 case PRAGMA_OMP_TEAMS:
31889 if (context != pragma_stmt && context != pragma_compound)
31890 goto bad_stmt;
31891 cp_parser_omp_construct (parser, pragma_tok);
31892 return true;
31894 case PRAGMA_OMP_TARGET:
31895 return cp_parser_omp_target (parser, pragma_tok, context);
31897 case PRAGMA_OMP_END_DECLARE_TARGET:
31898 cp_parser_omp_end_declare_target (parser, pragma_tok);
31899 return false;
31901 case PRAGMA_OMP_SECTION:
31902 error_at (pragma_tok->location,
31903 "%<#pragma omp section%> may only be used in "
31904 "%<#pragma omp sections%> construct");
31905 break;
31907 case PRAGMA_IVDEP:
31909 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31910 cp_token *tok;
31911 tok = cp_lexer_peek_token (the_parser->lexer);
31912 if (tok->type != CPP_KEYWORD
31913 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
31914 && tok->keyword != RID_DO))
31916 cp_parser_error (parser, "for, while or do statement expected");
31917 return false;
31919 cp_parser_iteration_statement (parser, true);
31920 return true;
31923 case PRAGMA_CILK_SIMD:
31924 if (context == pragma_external)
31926 error_at (pragma_tok->location,
31927 "%<#pragma simd%> must be inside a function");
31928 break;
31930 cp_parser_cilk_simd (parser, pragma_tok);
31931 return true;
31933 case PRAGMA_CILK_GRAINSIZE:
31934 if (context == pragma_external)
31936 error_at (pragma_tok->location,
31937 "%<#pragma cilk grainsize%> must be inside a function");
31938 break;
31941 /* Ignore the pragma if Cilk Plus is not enabled. */
31942 if (flag_cilkplus)
31944 cp_parser_cilk_grainsize (parser, pragma_tok);
31945 return true;
31947 else
31949 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
31950 "%<#pragma cilk grainsize%>");
31951 break;
31954 default:
31955 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
31956 c_invoke_pragma_handler (id);
31957 break;
31959 bad_stmt:
31960 cp_parser_error (parser, "expected declaration specifiers");
31961 break;
31964 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
31965 return false;
31968 /* The interface the pragma parsers have to the lexer. */
31970 enum cpp_ttype
31971 pragma_lex (tree *value)
31973 cp_token *tok;
31974 enum cpp_ttype ret;
31976 tok = cp_lexer_peek_token (the_parser->lexer);
31978 ret = tok->type;
31979 *value = tok->u.value;
31981 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
31982 ret = CPP_EOF;
31983 else if (ret == CPP_STRING)
31984 *value = cp_parser_string_literal (the_parser, false, false);
31985 else
31987 cp_lexer_consume_token (the_parser->lexer);
31988 if (ret == CPP_KEYWORD)
31989 ret = CPP_NAME;
31992 return ret;
31996 /* External interface. */
31998 /* Parse one entire translation unit. */
32000 void
32001 c_parse_file (void)
32003 static bool already_called = false;
32005 if (already_called)
32006 fatal_error ("inter-module optimizations not implemented for C++");
32007 already_called = true;
32009 the_parser = cp_parser_new ();
32010 push_deferring_access_checks (flag_access_control
32011 ? dk_no_deferred : dk_no_check);
32012 cp_parser_translation_unit (the_parser);
32013 the_parser = NULL;
32016 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
32017 vectorlength clause:
32018 Syntax:
32019 vectorlength ( constant-expression ) */
32021 static tree
32022 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
32023 bool is_simd_fn)
32025 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32026 tree expr;
32027 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
32028 safelen clause. Thus, vectorlength is represented as OMP 4.0
32029 safelen. For SIMD-enabled function it is represented by OMP 4.0
32030 simdlen. */
32031 if (!is_simd_fn)
32032 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
32033 loc);
32034 else
32035 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
32036 loc);
32038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32039 return error_mark_node;
32041 expr = cp_parser_constant_expression (parser, false, NULL);
32042 expr = maybe_constant_value (expr);
32044 /* If expr == error_mark_node, then don't emit any errors nor
32045 create a clause. if any of the above functions returns
32046 error mark node then they would have emitted an error message. */
32047 if (expr == error_mark_node)
32049 else if (!TREE_TYPE (expr)
32050 || !TREE_CONSTANT (expr)
32051 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
32052 error_at (loc, "vectorlength must be an integer constant");
32053 else if (TREE_CONSTANT (expr)
32054 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
32055 error_at (loc, "vectorlength must be a power of 2");
32056 else
32058 tree c;
32059 if (!is_simd_fn)
32061 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
32062 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
32063 OMP_CLAUSE_CHAIN (c) = clauses;
32064 clauses = c;
32066 else
32068 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
32069 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
32070 OMP_CLAUSE_CHAIN (c) = clauses;
32071 clauses = c;
32075 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32076 return error_mark_node;
32077 return clauses;
32080 /* Handles the Cilk Plus #pragma simd linear clause.
32081 Syntax:
32082 linear ( simd-linear-variable-list )
32084 simd-linear-variable-list:
32085 simd-linear-variable
32086 simd-linear-variable-list , simd-linear-variable
32088 simd-linear-variable:
32089 id-expression
32090 id-expression : simd-linear-step
32092 simd-linear-step:
32093 conditional-expression */
32095 static tree
32096 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
32098 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32100 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32101 return clauses;
32102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32104 cp_parser_error (parser, "expected identifier");
32105 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32106 return error_mark_node;
32109 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32110 parser->colon_corrects_to_scope_p = false;
32111 while (1)
32113 cp_token *token = cp_lexer_peek_token (parser->lexer);
32114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32116 cp_parser_error (parser, "expected variable-name");
32117 clauses = error_mark_node;
32118 break;
32121 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
32122 false, false);
32123 tree decl = cp_parser_lookup_name_simple (parser, var_name,
32124 token->location);
32125 if (decl == error_mark_node)
32127 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
32128 token->location);
32129 clauses = error_mark_node;
32131 else
32133 tree e = NULL_TREE;
32134 tree step_size = integer_one_node;
32136 /* If present, parse the linear step. Otherwise, assume the default
32137 value of 1. */
32138 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
32140 cp_lexer_consume_token (parser->lexer);
32142 e = cp_parser_assignment_expression (parser, false, NULL);
32143 e = maybe_constant_value (e);
32145 if (e == error_mark_node)
32147 /* If an error has occurred, then the whole pragma is
32148 considered ill-formed. Thus, no reason to keep
32149 parsing. */
32150 clauses = error_mark_node;
32151 break;
32153 else if (type_dependent_expression_p (e)
32154 || value_dependent_expression_p (e)
32155 || (TREE_TYPE (e)
32156 && INTEGRAL_TYPE_P (TREE_TYPE (e))
32157 && (TREE_CONSTANT (e)
32158 || DECL_P (e))))
32159 step_size = e;
32160 else
32161 cp_parser_error (parser,
32162 "step size must be an integer constant "
32163 "expression or an integer variable");
32166 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
32167 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
32168 OMP_CLAUSE_DECL (l) = decl;
32169 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
32170 OMP_CLAUSE_CHAIN (l) = clauses;
32171 clauses = l;
32173 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32174 cp_lexer_consume_token (parser->lexer);
32175 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
32176 break;
32177 else
32179 error_at (cp_lexer_peek_token (parser->lexer)->location,
32180 "expected %<,%> or %<)%> after %qE", decl);
32181 clauses = error_mark_node;
32182 break;
32185 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32186 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
32187 return clauses;
32190 /* Returns the name of the next clause. If the clause is not
32191 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
32192 token is not consumed. Otherwise, the appropriate enum from the
32193 pragma_simd_clause is returned and the token is consumed. */
32195 static pragma_omp_clause
32196 cp_parser_cilk_simd_clause_name (cp_parser *parser)
32198 pragma_omp_clause clause_type;
32199 cp_token *token = cp_lexer_peek_token (parser->lexer);
32201 if (token->keyword == RID_PRIVATE)
32202 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
32203 else if (!token->u.value || token->type != CPP_NAME)
32204 return PRAGMA_CILK_CLAUSE_NONE;
32205 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
32206 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
32207 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
32208 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
32209 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
32210 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
32211 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
32212 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
32213 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
32214 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
32215 else
32216 return PRAGMA_CILK_CLAUSE_NONE;
32218 cp_lexer_consume_token (parser->lexer);
32219 return clause_type;
32222 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
32224 static tree
32225 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
32227 tree clauses = NULL_TREE;
32229 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
32230 && clauses != error_mark_node)
32232 pragma_omp_clause c_kind;
32233 c_kind = cp_parser_cilk_simd_clause_name (parser);
32234 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
32235 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
32236 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
32237 clauses = cp_parser_cilk_simd_linear (parser, clauses);
32238 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
32239 /* Use the OpenMP 4.0 equivalent function. */
32240 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
32241 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
32242 /* Use the OpenMP 4.0 equivalent function. */
32243 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32244 clauses);
32245 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
32246 /* Use the OMP 4.0 equivalent function. */
32247 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32248 clauses);
32249 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
32250 /* Use the OMP 4.0 equivalent function. */
32251 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32252 else
32254 clauses = error_mark_node;
32255 cp_parser_error (parser, "expected %<#pragma simd%> clause");
32256 break;
32260 cp_parser_skip_to_pragma_eol (parser, pragma_token);
32262 if (clauses == error_mark_node)
32263 return error_mark_node;
32264 else
32265 return c_finish_cilk_clauses (clauses);
32268 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
32270 static void
32271 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token)
32273 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
32275 if (clauses == error_mark_node)
32276 return;
32278 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
32280 error_at (cp_lexer_peek_token (parser->lexer)->location,
32281 "for statement expected");
32282 return;
32285 tree sb = begin_omp_structured_block ();
32286 int save = cp_parser_begin_omp_structured_block (parser);
32287 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL);
32288 if (ret)
32289 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
32290 cp_parser_end_omp_structured_block (parser, save);
32291 add_stmt (finish_omp_structured_block (sb));
32294 /* Main entry-point for parsing Cilk Plus _Cilk_for
32295 loops. The return value is error_mark_node
32296 when errors happen and CILK_FOR tree on success. */
32298 static tree
32299 cp_parser_cilk_for (cp_parser *parser, tree grain)
32301 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
32302 gcc_unreachable ();
32304 tree sb = begin_omp_structured_block ();
32305 int save = cp_parser_begin_omp_structured_block (parser);
32307 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
32308 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
32309 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
32310 clauses = finish_omp_clauses (clauses);
32312 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL);
32313 if (ret)
32314 cpp_validate_cilk_plus_loop (ret);
32315 else
32316 ret = error_mark_node;
32318 cp_parser_end_omp_structured_block (parser, save);
32319 add_stmt (finish_omp_structured_block (sb));
32320 return ret;
32323 /* Create an identifier for a generic parameter type (a synthesized
32324 template parameter implied by `auto' or a concept identifier). */
32326 static GTY(()) int generic_parm_count;
32327 static tree
32328 make_generic_type_name ()
32330 char buf[32];
32331 sprintf (buf, "auto:%d", ++generic_parm_count);
32332 return get_identifier (buf);
32335 /* Predicate that behaves as is_auto_or_concept but matches the parent
32336 node of the generic type rather than the generic type itself. This
32337 allows for type transformation in add_implicit_template_parms. */
32339 static inline bool
32340 tree_type_is_auto_or_concept (const_tree t)
32342 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
32345 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
32346 (creating a new template parameter list if necessary). Returns the newly
32347 created template type parm. */
32349 tree
32350 synthesize_implicit_template_parm (cp_parser *parser)
32352 gcc_assert (current_binding_level->kind == sk_function_parms);
32354 /* We are either continuing a function template that already contains implicit
32355 template parameters, creating a new fully-implicit function template, or
32356 extending an existing explicit function template with implicit template
32357 parameters. */
32359 cp_binding_level *const entry_scope = current_binding_level;
32361 bool become_template = false;
32362 cp_binding_level *parent_scope = 0;
32364 if (parser->implicit_template_scope)
32366 gcc_assert (parser->implicit_template_parms);
32368 current_binding_level = parser->implicit_template_scope;
32370 else
32372 /* Roll back to the existing template parameter scope (in the case of
32373 extending an explicit function template) or introduce a new template
32374 parameter scope ahead of the function parameter scope (or class scope
32375 in the case of out-of-line member definitions). The function scope is
32376 added back after template parameter synthesis below. */
32378 cp_binding_level *scope = entry_scope;
32380 while (scope->kind == sk_function_parms)
32382 parent_scope = scope;
32383 scope = scope->level_chain;
32385 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
32387 /* If not defining a class, then any class scope is a scope level in
32388 an out-of-line member definition. In this case simply wind back
32389 beyond the first such scope to inject the template parameter list.
32390 Otherwise wind back to the class being defined. The latter can
32391 occur in class member friend declarations such as:
32393 class A {
32394 void foo (auto);
32396 class B {
32397 friend void A::foo (auto);
32400 The template parameter list synthesized for the friend declaration
32401 must be injected in the scope of 'B'. This can also occur in
32402 erroneous cases such as:
32404 struct A {
32405 struct B {
32406 void foo (auto);
32408 void B::foo (auto) {}
32411 Here the attempted definition of 'B::foo' within 'A' is ill-formed
32412 but, nevertheless, the template parameter list synthesized for the
32413 declarator should be injected into the scope of 'A' as if the
32414 ill-formed template was specified explicitly. */
32416 while (scope->kind == sk_class && !scope->defining_class_p)
32418 parent_scope = scope;
32419 scope = scope->level_chain;
32423 current_binding_level = scope;
32425 if (scope->kind != sk_template_parms
32426 || !function_being_declared_is_template_p (parser))
32428 /* Introduce a new template parameter list for implicit template
32429 parameters. */
32431 become_template = true;
32433 parser->implicit_template_scope
32434 = begin_scope (sk_template_parms, NULL);
32436 ++processing_template_decl;
32438 parser->fully_implicit_function_template_p = true;
32439 ++parser->num_template_parameter_lists;
32441 else
32443 /* Synthesize implicit template parameters at the end of the explicit
32444 template parameter list. */
32446 gcc_assert (current_template_parms);
32448 parser->implicit_template_scope = scope;
32450 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32451 parser->implicit_template_parms
32452 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
32456 /* Synthesize a new template parameter and track the current template
32457 parameter chain with implicit_template_parms. */
32459 tree synth_id = make_generic_type_name ();
32460 tree synth_tmpl_parm = finish_template_type_parm (class_type_node,
32461 synth_id);
32462 tree new_parm
32463 = process_template_parm (parser->implicit_template_parms,
32464 input_location,
32465 build_tree_list (NULL_TREE, synth_tmpl_parm),
32466 /*non_type=*/false,
32467 /*param_pack=*/false);
32470 if (parser->implicit_template_parms)
32471 parser->implicit_template_parms
32472 = TREE_CHAIN (parser->implicit_template_parms);
32473 else
32474 parser->implicit_template_parms = new_parm;
32476 tree new_type = TREE_TYPE (getdecls ());
32478 /* If creating a fully implicit function template, start the new implicit
32479 template parameter list with this synthesized type, otherwise grow the
32480 current template parameter list. */
32482 if (become_template)
32484 parent_scope->level_chain = current_binding_level;
32486 tree new_parms = make_tree_vec (1);
32487 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
32488 current_template_parms = tree_cons (size_int (processing_template_decl),
32489 new_parms, current_template_parms);
32491 else
32493 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
32494 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
32495 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
32496 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
32499 current_binding_level = entry_scope;
32501 return new_type;
32504 /* Finish the declaration of a fully implicit function template. Such a
32505 template has no explicit template parameter list so has not been through the
32506 normal template head and tail processing. synthesize_implicit_template_parm
32507 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
32508 provided if the declaration is a class member such that its template
32509 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
32510 form is returned. Otherwise NULL_TREE is returned. */
32512 tree
32513 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
32515 gcc_assert (parser->fully_implicit_function_template_p);
32517 if (member_decl_opt && member_decl_opt != error_mark_node
32518 && DECL_VIRTUAL_P (member_decl_opt))
32520 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
32521 "implicit templates may not be %<virtual%>");
32522 DECL_VIRTUAL_P (member_decl_opt) = false;
32525 if (member_decl_opt)
32526 member_decl_opt = finish_member_template_decl (member_decl_opt);
32527 end_template_decl ();
32529 parser->fully_implicit_function_template_p = false;
32530 --parser->num_template_parameter_lists;
32532 return member_decl_opt;
32535 #include "gt-cp-parser.h"