Add DW_CFA_AARCH64_negate_ra_state to dwarf2.def/h and dwarfnames.c
[official-gcc.git] / gcc / cp / parser.c
blob4bfae3655d5b1d7f41d8ba6454fca0775fa48f9d
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 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 "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_ITERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (IDENTIFIER_KEYWORD_P (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool, bool = false);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 rich_location richloc (line_table, input_location);
2808 c_parse_error (gmsgid,
2809 /* Because c_parser_error does not understand
2810 CPP_KEYWORD, keywords are treated like
2811 identifiers. */
2812 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2813 token->u.value, token->flags, &richloc);
2817 /* Issue an error about name-lookup failing. NAME is the
2818 IDENTIFIER_NODE DECL is the result of
2819 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2820 the thing that we hoped to find. */
2822 static void
2823 cp_parser_name_lookup_error (cp_parser* parser,
2824 tree name,
2825 tree decl,
2826 name_lookup_error desired,
2827 location_t location)
2829 /* If name lookup completely failed, tell the user that NAME was not
2830 declared. */
2831 if (decl == error_mark_node)
2833 if (parser->scope && parser->scope != global_namespace)
2834 error_at (location, "%<%E::%E%> has not been declared",
2835 parser->scope, name);
2836 else if (parser->scope == global_namespace)
2837 error_at (location, "%<::%E%> has not been declared", name);
2838 else if (parser->object_scope
2839 && !CLASS_TYPE_P (parser->object_scope))
2840 error_at (location, "request for member %qE in non-class type %qT",
2841 name, parser->object_scope);
2842 else if (parser->object_scope)
2843 error_at (location, "%<%T::%E%> has not been declared",
2844 parser->object_scope, name);
2845 else
2846 error_at (location, "%qE has not been declared", name);
2848 else if (parser->scope && parser->scope != global_namespace)
2850 switch (desired)
2852 case NLE_TYPE:
2853 error_at (location, "%<%E::%E%> is not a type",
2854 parser->scope, name);
2855 break;
2856 case NLE_CXX98:
2857 error_at (location, "%<%E::%E%> is not a class or namespace",
2858 parser->scope, name);
2859 break;
2860 case NLE_NOT_CXX98:
2861 error_at (location,
2862 "%<%E::%E%> is not a class, namespace, or enumeration",
2863 parser->scope, name);
2864 break;
2865 default:
2866 gcc_unreachable ();
2870 else if (parser->scope == global_namespace)
2872 switch (desired)
2874 case NLE_TYPE:
2875 error_at (location, "%<::%E%> is not a type", name);
2876 break;
2877 case NLE_CXX98:
2878 error_at (location, "%<::%E%> is not a class or namespace", name);
2879 break;
2880 case NLE_NOT_CXX98:
2881 error_at (location,
2882 "%<::%E%> is not a class, namespace, or enumeration",
2883 name);
2884 break;
2885 default:
2886 gcc_unreachable ();
2889 else
2891 switch (desired)
2893 case NLE_TYPE:
2894 error_at (location, "%qE is not a type", name);
2895 break;
2896 case NLE_CXX98:
2897 error_at (location, "%qE is not a class or namespace", name);
2898 break;
2899 case NLE_NOT_CXX98:
2900 error_at (location,
2901 "%qE is not a class, namespace, or enumeration", name);
2902 break;
2903 default:
2904 gcc_unreachable ();
2909 /* If we are parsing tentatively, remember that an error has occurred
2910 during this tentative parse. Returns true if the error was
2911 simulated; false if a message should be issued by the caller. */
2913 static bool
2914 cp_parser_simulate_error (cp_parser* parser)
2916 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2918 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2919 return true;
2921 return false;
2924 /* This function is called when a type is defined. If type
2925 definitions are forbidden at this point, an error message is
2926 issued. */
2928 static bool
2929 cp_parser_check_type_definition (cp_parser* parser)
2931 /* If types are forbidden here, issue a message. */
2932 if (parser->type_definition_forbidden_message)
2934 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2935 in the message need to be interpreted. */
2936 error (parser->type_definition_forbidden_message);
2937 return false;
2939 return true;
2942 /* This function is called when the DECLARATOR is processed. The TYPE
2943 was a type defined in the decl-specifiers. If it is invalid to
2944 define a type in the decl-specifiers for DECLARATOR, an error is
2945 issued. TYPE_LOCATION is the location of TYPE and is used
2946 for error reporting. */
2948 static void
2949 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2950 tree type, location_t type_location)
2952 /* [dcl.fct] forbids type definitions in return types.
2953 Unfortunately, it's not easy to know whether or not we are
2954 processing a return type until after the fact. */
2955 while (declarator
2956 && (declarator->kind == cdk_pointer
2957 || declarator->kind == cdk_reference
2958 || declarator->kind == cdk_ptrmem))
2959 declarator = declarator->declarator;
2960 if (declarator
2961 && declarator->kind == cdk_function)
2963 error_at (type_location,
2964 "new types may not be defined in a return type");
2965 inform (type_location,
2966 "(perhaps a semicolon is missing after the definition of %qT)",
2967 type);
2971 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2972 "<" in any valid C++ program. If the next token is indeed "<",
2973 issue a message warning the user about what appears to be an
2974 invalid attempt to form a template-id. LOCATION is the location
2975 of the type-specifier (TYPE) */
2977 static void
2978 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2979 tree type,
2980 enum tag_types tag_type,
2981 location_t location)
2983 cp_token_position start = 0;
2985 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2987 if (TREE_CODE (type) == TYPE_DECL)
2988 type = TREE_TYPE (type);
2989 if (TYPE_P (type) && !template_placeholder_p (type))
2990 error_at (location, "%qT is not a template", type);
2991 else if (identifier_p (type))
2993 if (tag_type != none_type)
2994 error_at (location, "%qE is not a class template", type);
2995 else
2996 error_at (location, "%qE is not a template", type);
2998 else
2999 error_at (location, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3002 start = cp_lexer_token_position (parser->lexer, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser->lexer);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3009 if (start)
3010 cp_lexer_purge_tokens_after (parser->lexer, start);
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3019 static bool
3020 cp_parser_non_integral_constant_expression (cp_parser *parser,
3021 non_integral_constant thing)
3023 parser->non_integral_constant_expression_p = true;
3024 if (parser->integral_constant_expression_p)
3026 if (!parser->allow_non_integral_constant_expression_p)
3028 const char *msg = NULL;
3029 switch (thing)
3031 case NIC_FLOAT:
3032 pedwarn (input_location, OPT_Wpedantic,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3035 return true;
3036 case NIC_CAST:
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3040 return true;
3041 case NIC_TYPEID:
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_NCC:
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_FUNC_CALL:
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_INC:
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_DEC:
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_ARRAY_REF:
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ADDR_LABEL:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_OVERLOADED:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3072 return true;
3073 case NIC_ASSIGNMENT:
3074 error ("an assignment cannot appear in a constant-expression");
3075 return true;
3076 case NIC_COMMA:
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_CONSTRUCTOR:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3083 return true;
3084 case NIC_TRANSACTION:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3087 return true;
3088 case NIC_THIS:
3089 msg = "this";
3090 break;
3091 case NIC_FUNC_NAME:
3092 msg = "__FUNCTION__";
3093 break;
3094 case NIC_PRETTY_FUNC:
3095 msg = "__PRETTY_FUNCTION__";
3096 break;
3097 case NIC_C99_FUNC:
3098 msg = "__func__";
3099 break;
3100 case NIC_VA_ARG:
3101 msg = "va_arg";
3102 break;
3103 case NIC_ARROW:
3104 msg = "->";
3105 break;
3106 case NIC_POINT:
3107 msg = ".";
3108 break;
3109 case NIC_STAR:
3110 msg = "*";
3111 break;
3112 case NIC_ADDR:
3113 msg = "&";
3114 break;
3115 case NIC_PREINCREMENT:
3116 msg = "++";
3117 break;
3118 case NIC_PREDECREMENT:
3119 msg = "--";
3120 break;
3121 case NIC_NEW:
3122 msg = "new";
3123 break;
3124 case NIC_DEL:
3125 msg = "delete";
3126 break;
3127 default:
3128 gcc_unreachable ();
3130 if (msg)
3131 error ("%qs cannot appear in a constant-expression", msg);
3132 return true;
3135 return false;
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3143 static void
3144 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3145 location_t location)
3147 tree decl, ambiguous_decls;
3148 cp_parser_commit_to_tentative_parse (parser);
3149 /* Try to lookup the identifier. */
3150 decl = cp_parser_lookup_name (parser, id, none_type,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls, location);
3155 if (ambiguous_decls)
3156 /* If the lookup was ambiguous, an error will already have
3157 been issued. */
3158 return;
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl))
3163 error_at (location,
3164 "invalid use of template-name %qE without an argument list",
3165 decl);
3166 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3167 inform (location, "class template argument deduction is only available "
3168 "with -std=c++1z or -std=gnu++1z");
3169 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3171 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3172 error_at (location, "invalid use of destructor %qD as a type", id);
3173 else if (TREE_CODE (decl) == TYPE_DECL)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location, "invalid combination of multiple type-specifiers");
3176 else if (!parser->scope)
3178 /* Issue an error message. */
3179 const char *suggestion = NULL;
3180 if (TREE_CODE (id) == IDENTIFIER_NODE)
3181 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3182 if (suggestion)
3184 gcc_rich_location richloc (location);
3185 richloc.add_fixit_replace (suggestion);
3186 error_at_rich_loc (&richloc,
3187 "%qE does not name a type; did you mean %qs?",
3188 id, suggestion);
3190 else
3191 error_at (location, "%qE does not name a type", id);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3200 inform (location, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3203 inform (location, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect < cxx11
3206 && TREE_CODE (id) == IDENTIFIER_NODE
3207 && id_equal (id, "thread_local"))
3208 inform (location, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3211 inform (location, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl && current_class_type
3213 && TYPE_BINFO (current_class_type))
3215 tree b;
3217 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3219 b = TREE_CHAIN (b))
3221 tree base_type = BINFO_TYPE (b);
3222 if (CLASS_TYPE_P (base_type)
3223 && dependent_type_p (base_type))
3225 tree field;
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3230 for (field = TYPE_FIELDS (base_type);
3231 field;
3232 field = DECL_CHAIN (field))
3233 if (TREE_CODE (field) == TYPE_DECL
3234 && DECL_NAME (field) == id)
3236 inform (location,
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b), id);
3239 break;
3241 if (field)
3242 break;
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser->scope != error_mark_node)
3251 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3254 error_at (location_of (id),
3255 "%qE in namespace %qE does not name a template type",
3256 id, parser->scope);
3257 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3258 error_at (location_of (id),
3259 "%qE in namespace %qE does not name a template type",
3260 TREE_OPERAND (id, 0), parser->scope);
3261 else
3262 error_at (location_of (id),
3263 "%qE in namespace %qE does not name a type",
3264 id, parser->scope);
3265 if (DECL_P (decl))
3266 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3268 else if (CLASS_TYPE_P (parser->scope)
3269 && constructor_name_p (id, parser->scope))
3271 /* A<T>::A<T>() */
3272 error_at (location, "%<%T::%E%> names the constructor, not"
3273 " the type", parser->scope, id);
3274 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3275 error_at (location, "and %qT has no template constructors",
3276 parser->scope);
3278 else if (TYPE_P (parser->scope)
3279 && dependent_scope_p (parser->scope))
3281 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3282 error_at (location,
3283 "need %<typename%> before %<%T::%D::%E%> because "
3284 "%<%T::%D%> is a dependent scope",
3285 TYPE_CONTEXT (parser->scope),
3286 TYPENAME_TYPE_FULLNAME (parser->scope),
3288 TYPE_CONTEXT (parser->scope),
3289 TYPENAME_TYPE_FULLNAME (parser->scope));
3290 else
3291 error_at (location, "need %<typename%> before %<%T::%E%> because "
3292 "%qT is a dependent scope",
3293 parser->scope, id, parser->scope);
3295 else if (TYPE_P (parser->scope))
3297 if (!COMPLETE_TYPE_P (parser->scope))
3298 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3299 parser->scope);
3300 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3301 error_at (location_of (id),
3302 "%qE in %q#T does not name a template type",
3303 id, parser->scope);
3304 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3305 error_at (location_of (id),
3306 "%qE in %q#T does not name a template type",
3307 TREE_OPERAND (id, 0), parser->scope);
3308 else
3309 error_at (location_of (id),
3310 "%qE in %q#T does not name a type",
3311 id, parser->scope);
3312 if (DECL_P (decl))
3313 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3315 else
3316 gcc_unreachable ();
3320 /* Check for a common situation where a type-name should be present,
3321 but is not, and issue a sensible error message. Returns true if an
3322 invalid type-name was detected.
3324 The situation handled by this function are variable declarations of the
3325 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3326 Usually, `ID' should name a type, but if we got here it means that it
3327 does not. We try to emit the best possible error message depending on
3328 how exactly the id-expression looks like. */
3330 static bool
3331 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3333 tree id;
3334 cp_token *token = cp_lexer_peek_token (parser->lexer);
3336 /* Avoid duplicate error about ambiguous lookup. */
3337 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3339 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3340 if (next->type == CPP_NAME && next->error_reported)
3341 goto out;
3344 cp_parser_parse_tentatively (parser);
3345 id = cp_parser_id_expression (parser,
3346 /*template_keyword_p=*/false,
3347 /*check_dependency_p=*/true,
3348 /*template_p=*/NULL,
3349 /*declarator_p=*/true,
3350 /*optional_p=*/false);
3351 /* If the next token is a (, this is a function with no explicit return
3352 type, i.e. constructor, destructor or conversion op. */
3353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3354 || TREE_CODE (id) == TYPE_DECL)
3356 cp_parser_abort_tentative_parse (parser);
3357 return false;
3359 if (!cp_parser_parse_definitely (parser))
3360 return false;
3362 /* Emit a diagnostic for the invalid type. */
3363 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3364 out:
3365 /* If we aren't in the middle of a declarator (i.e. in a
3366 parameter-declaration-clause), skip to the end of the declaration;
3367 there's no point in trying to process it. */
3368 if (!parser->in_declarator_p)
3369 cp_parser_skip_to_end_of_block_or_statement (parser);
3370 return true;
3373 /* Consume tokens up to, and including, the next non-nested closing `)'.
3374 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3375 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3376 found an unnested token of that type. */
3378 static int
3379 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3380 bool recovering,
3381 cpp_ttype or_ttype,
3382 bool consume_paren)
3384 unsigned paren_depth = 0;
3385 unsigned brace_depth = 0;
3386 unsigned square_depth = 0;
3388 if (recovering && or_ttype == CPP_EOF
3389 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3390 return 0;
3392 while (true)
3394 cp_token * token = cp_lexer_peek_token (parser->lexer);
3396 /* Have we found what we're looking for before the closing paren? */
3397 if (token->type == or_ttype && or_ttype != CPP_EOF
3398 && !brace_depth && !paren_depth && !square_depth)
3399 return -1;
3401 switch (token->type)
3403 case CPP_EOF:
3404 case CPP_PRAGMA_EOL:
3405 /* If we've run out of tokens, then there is no closing `)'. */
3406 return 0;
3408 /* This is good for lambda expression capture-lists. */
3409 case CPP_OPEN_SQUARE:
3410 ++square_depth;
3411 break;
3412 case CPP_CLOSE_SQUARE:
3413 if (!square_depth--)
3414 return 0;
3415 break;
3417 case CPP_SEMICOLON:
3418 /* This matches the processing in skip_to_end_of_statement. */
3419 if (!brace_depth)
3420 return 0;
3421 break;
3423 case CPP_OPEN_BRACE:
3424 ++brace_depth;
3425 break;
3426 case CPP_CLOSE_BRACE:
3427 if (!brace_depth--)
3428 return 0;
3429 break;
3431 case CPP_OPEN_PAREN:
3432 if (!brace_depth)
3433 ++paren_depth;
3434 break;
3436 case CPP_CLOSE_PAREN:
3437 if (!brace_depth && !paren_depth--)
3439 if (consume_paren)
3440 cp_lexer_consume_token (parser->lexer);
3441 return 1;
3443 break;
3445 default:
3446 break;
3449 /* Consume the token. */
3450 cp_lexer_consume_token (parser->lexer);
3454 /* Consume tokens up to, and including, the next non-nested closing `)'.
3455 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3456 are doing error recovery. Returns -1 if OR_COMMA is true and we
3457 found an unnested token of that type. */
3459 static int
3460 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3461 bool recovering,
3462 bool or_comma,
3463 bool consume_paren)
3465 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3466 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3467 ttype, consume_paren);
3470 /* Consume tokens until we reach the end of the current statement.
3471 Normally, that will be just before consuming a `;'. However, if a
3472 non-nested `}' comes first, then we stop before consuming that. */
3474 static void
3475 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3477 unsigned nesting_depth = 0;
3479 /* Unwind generic function template scope if necessary. */
3480 if (parser->fully_implicit_function_template_p)
3481 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3483 while (true)
3485 cp_token *token = cp_lexer_peek_token (parser->lexer);
3487 switch (token->type)
3489 case CPP_EOF:
3490 case CPP_PRAGMA_EOL:
3491 /* If we've run out of tokens, stop. */
3492 return;
3494 case CPP_SEMICOLON:
3495 /* If the next token is a `;', we have reached the end of the
3496 statement. */
3497 if (!nesting_depth)
3498 return;
3499 break;
3501 case CPP_CLOSE_BRACE:
3502 /* If this is a non-nested '}', stop before consuming it.
3503 That way, when confronted with something like:
3505 { 3 + }
3507 we stop before consuming the closing '}', even though we
3508 have not yet reached a `;'. */
3509 if (nesting_depth == 0)
3510 return;
3512 /* If it is the closing '}' for a block that we have
3513 scanned, stop -- but only after consuming the token.
3514 That way given:
3516 void f g () { ... }
3517 typedef int I;
3519 we will stop after the body of the erroneously declared
3520 function, but before consuming the following `typedef'
3521 declaration. */
3522 if (--nesting_depth == 0)
3524 cp_lexer_consume_token (parser->lexer);
3525 return;
3527 break;
3529 case CPP_OPEN_BRACE:
3530 ++nesting_depth;
3531 break;
3533 default:
3534 break;
3537 /* Consume the token. */
3538 cp_lexer_consume_token (parser->lexer);
3542 /* This function is called at the end of a statement or declaration.
3543 If the next token is a semicolon, it is consumed; otherwise, error
3544 recovery is attempted. */
3546 static void
3547 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3549 /* Look for the trailing `;'. */
3550 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3552 /* If there is additional (erroneous) input, skip to the end of
3553 the statement. */
3554 cp_parser_skip_to_end_of_statement (parser);
3555 /* If the next token is now a `;', consume it. */
3556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3557 cp_lexer_consume_token (parser->lexer);
3561 /* Skip tokens until we have consumed an entire block, or until we
3562 have consumed a non-nested `;'. */
3564 static void
3565 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3567 int nesting_depth = 0;
3569 /* Unwind generic function template scope if necessary. */
3570 if (parser->fully_implicit_function_template_p)
3571 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3573 while (nesting_depth >= 0)
3575 cp_token *token = cp_lexer_peek_token (parser->lexer);
3577 switch (token->type)
3579 case CPP_EOF:
3580 case CPP_PRAGMA_EOL:
3581 /* If we've run out of tokens, stop. */
3582 return;
3584 case CPP_SEMICOLON:
3585 /* Stop if this is an unnested ';'. */
3586 if (!nesting_depth)
3587 nesting_depth = -1;
3588 break;
3590 case CPP_CLOSE_BRACE:
3591 /* Stop if this is an unnested '}', or closes the outermost
3592 nesting level. */
3593 nesting_depth--;
3594 if (nesting_depth < 0)
3595 return;
3596 if (!nesting_depth)
3597 nesting_depth = -1;
3598 break;
3600 case CPP_OPEN_BRACE:
3601 /* Nest. */
3602 nesting_depth++;
3603 break;
3605 default:
3606 break;
3609 /* Consume the token. */
3610 cp_lexer_consume_token (parser->lexer);
3614 /* Skip tokens until a non-nested closing curly brace is the next
3615 token, or there are no more tokens. Return true in the first case,
3616 false otherwise. */
3618 static bool
3619 cp_parser_skip_to_closing_brace (cp_parser *parser)
3621 unsigned nesting_depth = 0;
3623 while (true)
3625 cp_token *token = cp_lexer_peek_token (parser->lexer);
3627 switch (token->type)
3629 case CPP_EOF:
3630 case CPP_PRAGMA_EOL:
3631 /* If we've run out of tokens, stop. */
3632 return false;
3634 case CPP_CLOSE_BRACE:
3635 /* If the next token is a non-nested `}', then we have reached
3636 the end of the current block. */
3637 if (nesting_depth-- == 0)
3638 return true;
3639 break;
3641 case CPP_OPEN_BRACE:
3642 /* If it the next token is a `{', then we are entering a new
3643 block. Consume the entire block. */
3644 ++nesting_depth;
3645 break;
3647 default:
3648 break;
3651 /* Consume the token. */
3652 cp_lexer_consume_token (parser->lexer);
3656 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3657 parameter is the PRAGMA token, allowing us to purge the entire pragma
3658 sequence. */
3660 static void
3661 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3663 cp_token *token;
3665 parser->lexer->in_pragma = false;
3668 token = cp_lexer_consume_token (parser->lexer);
3669 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3671 /* Ensure that the pragma is not parsed again. */
3672 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3675 /* Require pragma end of line, resyncing with it as necessary. The
3676 arguments are as for cp_parser_skip_to_pragma_eol. */
3678 static void
3679 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3681 parser->lexer->in_pragma = false;
3682 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3686 /* This is a simple wrapper around make_typename_type. When the id is
3687 an unresolved identifier node, we can provide a superior diagnostic
3688 using cp_parser_diagnose_invalid_type_name. */
3690 static tree
3691 cp_parser_make_typename_type (cp_parser *parser, tree id,
3692 location_t id_location)
3694 tree result;
3695 if (identifier_p (id))
3697 result = make_typename_type (parser->scope, id, typename_type,
3698 /*complain=*/tf_none);
3699 if (result == error_mark_node)
3700 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3701 return result;
3703 return make_typename_type (parser->scope, id, typename_type, tf_error);
3706 /* This is a wrapper around the
3707 make_{pointer,ptrmem,reference}_declarator functions that decides
3708 which one to call based on the CODE and CLASS_TYPE arguments. The
3709 CODE argument should be one of the values returned by
3710 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3711 appertain to the pointer or reference. */
3713 static cp_declarator *
3714 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3715 cp_cv_quals cv_qualifiers,
3716 cp_declarator *target,
3717 tree attributes)
3719 if (code == ERROR_MARK)
3720 return cp_error_declarator;
3722 if (code == INDIRECT_REF)
3723 if (class_type == NULL_TREE)
3724 return make_pointer_declarator (cv_qualifiers, target, attributes);
3725 else
3726 return make_ptrmem_declarator (cv_qualifiers, class_type,
3727 target, attributes);
3728 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3729 return make_reference_declarator (cv_qualifiers, target,
3730 false, attributes);
3731 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3732 return make_reference_declarator (cv_qualifiers, target,
3733 true, attributes);
3734 gcc_unreachable ();
3737 /* Create a new C++ parser. */
3739 static cp_parser *
3740 cp_parser_new (void)
3742 cp_parser *parser;
3743 cp_lexer *lexer;
3744 unsigned i;
3746 /* cp_lexer_new_main is called before doing GC allocation because
3747 cp_lexer_new_main might load a PCH file. */
3748 lexer = cp_lexer_new_main ();
3750 /* Initialize the binops_by_token so that we can get the tree
3751 directly from the token. */
3752 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3753 binops_by_token[binops[i].token_type] = binops[i];
3755 parser = ggc_cleared_alloc<cp_parser> ();
3756 parser->lexer = lexer;
3757 parser->context = cp_parser_context_new (NULL);
3759 /* For now, we always accept GNU extensions. */
3760 parser->allow_gnu_extensions_p = 1;
3762 /* The `>' token is a greater-than operator, not the end of a
3763 template-id. */
3764 parser->greater_than_is_operator_p = true;
3766 parser->default_arg_ok_p = true;
3768 /* We are not parsing a constant-expression. */
3769 parser->integral_constant_expression_p = false;
3770 parser->allow_non_integral_constant_expression_p = false;
3771 parser->non_integral_constant_expression_p = false;
3773 /* Local variable names are not forbidden. */
3774 parser->local_variables_forbidden_p = false;
3776 /* We are not processing an `extern "C"' declaration. */
3777 parser->in_unbraced_linkage_specification_p = false;
3779 /* We are not processing a declarator. */
3780 parser->in_declarator_p = false;
3782 /* We are not processing a template-argument-list. */
3783 parser->in_template_argument_list_p = false;
3785 /* We are not in an iteration statement. */
3786 parser->in_statement = 0;
3788 /* We are not in a switch statement. */
3789 parser->in_switch_statement_p = false;
3791 /* We are not parsing a type-id inside an expression. */
3792 parser->in_type_id_in_expr_p = false;
3794 /* Declarations aren't implicitly extern "C". */
3795 parser->implicit_extern_c = false;
3797 /* String literals should be translated to the execution character set. */
3798 parser->translate_strings_p = true;
3800 /* We are not parsing a function body. */
3801 parser->in_function_body = false;
3803 /* We can correct until told otherwise. */
3804 parser->colon_corrects_to_scope_p = true;
3806 /* The unparsed function queue is empty. */
3807 push_unparsed_function_queues (parser);
3809 /* There are no classes being defined. */
3810 parser->num_classes_being_defined = 0;
3812 /* No template parameters apply. */
3813 parser->num_template_parameter_lists = 0;
3815 /* Special parsing data structures. */
3816 parser->omp_declare_simd = NULL;
3817 parser->cilk_simd_fn_info = NULL;
3818 parser->oacc_routine = NULL;
3820 /* Not declaring an implicit function template. */
3821 parser->auto_is_implicit_function_template_parm_p = false;
3822 parser->fully_implicit_function_template_p = false;
3823 parser->implicit_template_parms = 0;
3824 parser->implicit_template_scope = 0;
3826 /* Allow constrained-type-specifiers. */
3827 parser->prevent_constrained_type_specifiers = 0;
3829 return parser;
3832 /* Create a cp_lexer structure which will emit the tokens in CACHE
3833 and push it onto the parser's lexer stack. This is used for delayed
3834 parsing of in-class method bodies and default arguments, and should
3835 not be confused with tentative parsing. */
3836 static void
3837 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3839 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3840 lexer->next = parser->lexer;
3841 parser->lexer = lexer;
3843 /* Move the current source position to that of the first token in the
3844 new lexer. */
3845 cp_lexer_set_source_position_from_token (lexer->next_token);
3848 /* Pop the top lexer off the parser stack. This is never used for the
3849 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3850 static void
3851 cp_parser_pop_lexer (cp_parser *parser)
3853 cp_lexer *lexer = parser->lexer;
3854 parser->lexer = lexer->next;
3855 cp_lexer_destroy (lexer);
3857 /* Put the current source position back where it was before this
3858 lexer was pushed. */
3859 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3862 /* Lexical conventions [gram.lex] */
3864 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3865 identifier. */
3867 static cp_expr
3868 cp_parser_identifier (cp_parser* parser)
3870 cp_token *token;
3872 /* Look for the identifier. */
3873 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3874 /* Return the value. */
3875 if (token)
3876 return cp_expr (token->u.value, token->location);
3877 else
3878 return error_mark_node;
3881 /* Parse a sequence of adjacent string constants. Returns a
3882 TREE_STRING representing the combined, nul-terminated string
3883 constant. If TRANSLATE is true, translate the string to the
3884 execution character set. If WIDE_OK is true, a wide string is
3885 invalid here.
3887 C++98 [lex.string] says that if a narrow string literal token is
3888 adjacent to a wide string literal token, the behavior is undefined.
3889 However, C99 6.4.5p4 says that this results in a wide string literal.
3890 We follow C99 here, for consistency with the C front end.
3892 This code is largely lifted from lex_string() in c-lex.c.
3894 FUTURE: ObjC++ will need to handle @-strings here. */
3895 static cp_expr
3896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3897 bool lookup_udlit = true)
3899 tree value;
3900 size_t count;
3901 struct obstack str_ob;
3902 cpp_string str, istr, *strs;
3903 cp_token *tok;
3904 enum cpp_ttype type, curr_type;
3905 int have_suffix_p = 0;
3906 tree string_tree;
3907 tree suffix_id = NULL_TREE;
3908 bool curr_tok_is_userdef_p = false;
3910 tok = cp_lexer_peek_token (parser->lexer);
3911 if (!cp_parser_is_string_literal (tok))
3913 cp_parser_error (parser, "expected string-literal");
3914 return error_mark_node;
3917 location_t loc = tok->location;
3919 if (cpp_userdef_string_p (tok->type))
3921 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3922 curr_type = cpp_userdef_string_remove_type (tok->type);
3923 curr_tok_is_userdef_p = true;
3925 else
3927 string_tree = tok->u.value;
3928 curr_type = tok->type;
3930 type = curr_type;
3932 /* Try to avoid the overhead of creating and destroying an obstack
3933 for the common case of just one string. */
3934 if (!cp_parser_is_string_literal
3935 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3937 cp_lexer_consume_token (parser->lexer);
3939 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3940 str.len = TREE_STRING_LENGTH (string_tree);
3941 count = 1;
3943 if (curr_tok_is_userdef_p)
3945 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3946 have_suffix_p = 1;
3947 curr_type = cpp_userdef_string_remove_type (tok->type);
3949 else
3950 curr_type = tok->type;
3952 strs = &str;
3954 else
3956 location_t last_tok_loc = tok->location;
3957 gcc_obstack_init (&str_ob);
3958 count = 0;
3962 cp_lexer_consume_token (parser->lexer);
3963 count++;
3964 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3965 str.len = TREE_STRING_LENGTH (string_tree);
3967 if (curr_tok_is_userdef_p)
3969 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3970 if (have_suffix_p == 0)
3972 suffix_id = curr_suffix_id;
3973 have_suffix_p = 1;
3975 else if (have_suffix_p == 1
3976 && curr_suffix_id != suffix_id)
3978 error ("inconsistent user-defined literal suffixes"
3979 " %qD and %qD in string literal",
3980 suffix_id, curr_suffix_id);
3981 have_suffix_p = -1;
3983 curr_type = cpp_userdef_string_remove_type (tok->type);
3985 else
3986 curr_type = tok->type;
3988 if (type != curr_type)
3990 if (type == CPP_STRING)
3991 type = curr_type;
3992 else if (curr_type != CPP_STRING)
3994 rich_location rich_loc (line_table, tok->location);
3995 rich_loc.add_range (last_tok_loc, false);
3996 error_at_rich_loc (&rich_loc,
3997 "unsupported non-standard concatenation "
3998 "of string literals");
4002 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4004 last_tok_loc = tok->location;
4006 tok = cp_lexer_peek_token (parser->lexer);
4007 if (cpp_userdef_string_p (tok->type))
4009 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4010 curr_type = cpp_userdef_string_remove_type (tok->type);
4011 curr_tok_is_userdef_p = true;
4013 else
4015 string_tree = tok->u.value;
4016 curr_type = tok->type;
4017 curr_tok_is_userdef_p = false;
4020 while (cp_parser_is_string_literal (tok));
4022 /* A string literal built by concatenation has its caret=start at
4023 the start of the initial string, and its finish at the finish of
4024 the final string literal. */
4025 loc = make_location (loc, loc, get_finish (last_tok_loc));
4027 strs = (cpp_string *) obstack_finish (&str_ob);
4030 if (type != CPP_STRING && !wide_ok)
4032 cp_parser_error (parser, "a wide string is invalid in this context");
4033 type = CPP_STRING;
4036 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4037 (parse_in, strs, count, &istr, type))
4039 value = build_string (istr.len, (const char *)istr.text);
4040 free (CONST_CAST (unsigned char *, istr.text));
4042 switch (type)
4044 default:
4045 case CPP_STRING:
4046 case CPP_UTF8STRING:
4047 TREE_TYPE (value) = char_array_type_node;
4048 break;
4049 case CPP_STRING16:
4050 TREE_TYPE (value) = char16_array_type_node;
4051 break;
4052 case CPP_STRING32:
4053 TREE_TYPE (value) = char32_array_type_node;
4054 break;
4055 case CPP_WSTRING:
4056 TREE_TYPE (value) = wchar_array_type_node;
4057 break;
4060 value = fix_string_type (value);
4062 if (have_suffix_p)
4064 tree literal = build_userdef_literal (suffix_id, value,
4065 OT_NONE, NULL_TREE);
4066 if (lookup_udlit)
4067 value = cp_parser_userdef_string_literal (literal);
4068 else
4069 value = literal;
4072 else
4073 /* cpp_interpret_string has issued an error. */
4074 value = error_mark_node;
4076 if (count > 1)
4077 obstack_free (&str_ob, 0);
4079 return cp_expr (value, loc);
4082 /* Look up a literal operator with the name and the exact arguments. */
4084 static tree
4085 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4087 tree decl;
4088 decl = lookup_name (name);
4089 if (!decl || !is_overloaded_fn (decl))
4090 return error_mark_node;
4092 for (lkp_iterator iter (decl); iter; ++iter)
4094 unsigned int ix;
4095 bool found = true;
4096 tree fn = *iter;
4097 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4098 if (parmtypes != NULL_TREE)
4100 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4101 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4103 tree tparm = TREE_VALUE (parmtypes);
4104 tree targ = TREE_TYPE ((*args)[ix]);
4105 bool ptr = TYPE_PTR_P (tparm);
4106 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4107 if ((ptr || arr || !same_type_p (tparm, targ))
4108 && (!ptr || !arr
4109 || !same_type_p (TREE_TYPE (tparm),
4110 TREE_TYPE (targ))))
4111 found = false;
4113 if (found
4114 && ix == vec_safe_length (args)
4115 /* May be this should be sufficient_parms_p instead,
4116 depending on how exactly should user-defined literals
4117 work in presence of default arguments on the literal
4118 operator parameters. */
4119 && parmtypes == void_list_node)
4120 return decl;
4124 return error_mark_node;
4127 /* Parse a user-defined char constant. Returns a call to a user-defined
4128 literal operator taking the character as an argument. */
4130 static cp_expr
4131 cp_parser_userdef_char_literal (cp_parser *parser)
4133 cp_token *token = cp_lexer_consume_token (parser->lexer);
4134 tree literal = token->u.value;
4135 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4136 tree value = USERDEF_LITERAL_VALUE (literal);
4137 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4138 tree decl, result;
4140 /* Build up a call to the user-defined operator */
4141 /* Lookup the name we got back from the id-expression. */
4142 vec<tree, va_gc> *args = make_tree_vector ();
4143 vec_safe_push (args, value);
4144 decl = lookup_literal_operator (name, args);
4145 if (!decl || decl == error_mark_node)
4147 error ("unable to find character literal operator %qD with %qT argument",
4148 name, TREE_TYPE (value));
4149 release_tree_vector (args);
4150 return error_mark_node;
4152 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4153 release_tree_vector (args);
4154 return result;
4157 /* A subroutine of cp_parser_userdef_numeric_literal to
4158 create a char... template parameter pack from a string node. */
4160 static tree
4161 make_char_string_pack (tree value)
4163 tree charvec;
4164 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4165 const char *str = TREE_STRING_POINTER (value);
4166 int i, len = TREE_STRING_LENGTH (value) - 1;
4167 tree argvec = make_tree_vec (1);
4169 /* Fill in CHARVEC with all of the parameters. */
4170 charvec = make_tree_vec (len);
4171 for (i = 0; i < len; ++i)
4172 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4174 /* Build the argument packs. */
4175 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4177 TREE_VEC_ELT (argvec, 0) = argpack;
4179 return argvec;
4182 /* A subroutine of cp_parser_userdef_numeric_literal to
4183 create a char... template parameter pack from a string node. */
4185 static tree
4186 make_string_pack (tree value)
4188 tree charvec;
4189 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4190 const unsigned char *str
4191 = (const unsigned char *) TREE_STRING_POINTER (value);
4192 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4193 int len = TREE_STRING_LENGTH (value) / sz - 1;
4194 tree argvec = make_tree_vec (2);
4196 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4197 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4199 /* First template parm is character type. */
4200 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4202 /* Fill in CHARVEC with all of the parameters. */
4203 charvec = make_tree_vec (len);
4204 for (int i = 0; i < len; ++i)
4205 TREE_VEC_ELT (charvec, i)
4206 = double_int_to_tree (str_char_type_node,
4207 double_int::from_buffer (str + i * sz, sz));
4209 /* Build the argument packs. */
4210 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4212 TREE_VEC_ELT (argvec, 1) = argpack;
4214 return argvec;
4217 /* Parse a user-defined numeric constant. returns a call to a user-defined
4218 literal operator. */
4220 static cp_expr
4221 cp_parser_userdef_numeric_literal (cp_parser *parser)
4223 cp_token *token = cp_lexer_consume_token (parser->lexer);
4224 tree literal = token->u.value;
4225 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4226 tree value = USERDEF_LITERAL_VALUE (literal);
4227 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4228 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4229 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4230 tree decl, result;
4231 vec<tree, va_gc> *args;
4233 /* Look for a literal operator taking the exact type of numeric argument
4234 as the literal value. */
4235 args = make_tree_vector ();
4236 vec_safe_push (args, value);
4237 decl = lookup_literal_operator (name, args);
4238 if (decl && decl != error_mark_node)
4240 result = finish_call_expr (decl, &args, false, true,
4241 tf_warning_or_error);
4243 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4245 warning_at (token->location, OPT_Woverflow,
4246 "integer literal exceeds range of %qT type",
4247 long_long_unsigned_type_node);
4249 else
4251 if (overflow > 0)
4252 warning_at (token->location, OPT_Woverflow,
4253 "floating literal exceeds range of %qT type",
4254 long_double_type_node);
4255 else if (overflow < 0)
4256 warning_at (token->location, OPT_Woverflow,
4257 "floating literal truncated to zero");
4260 release_tree_vector (args);
4261 return result;
4263 release_tree_vector (args);
4265 /* If the numeric argument didn't work, look for a raw literal
4266 operator taking a const char* argument consisting of the number
4267 in string format. */
4268 args = make_tree_vector ();
4269 vec_safe_push (args, num_string);
4270 decl = lookup_literal_operator (name, args);
4271 if (decl && decl != error_mark_node)
4273 result = finish_call_expr (decl, &args, false, true,
4274 tf_warning_or_error);
4275 release_tree_vector (args);
4276 return result;
4278 release_tree_vector (args);
4280 /* If the raw literal didn't work, look for a non-type template
4281 function with parameter pack char.... Call the function with
4282 template parameter characters representing the number. */
4283 args = make_tree_vector ();
4284 decl = lookup_literal_operator (name, args);
4285 if (decl && decl != error_mark_node)
4287 tree tmpl_args = make_char_string_pack (num_string);
4288 decl = lookup_template_function (decl, tmpl_args);
4289 result = finish_call_expr (decl, &args, false, true,
4290 tf_warning_or_error);
4291 release_tree_vector (args);
4292 return result;
4295 release_tree_vector (args);
4297 error ("unable to find numeric literal operator %qD", name);
4298 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4299 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4300 "to enable more built-in suffixes");
4301 return error_mark_node;
4304 /* Parse a user-defined string constant. Returns a call to a user-defined
4305 literal operator taking a character pointer and the length of the string
4306 as arguments. */
4308 static tree
4309 cp_parser_userdef_string_literal (tree literal)
4311 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4312 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4313 tree value = USERDEF_LITERAL_VALUE (literal);
4314 int len = TREE_STRING_LENGTH (value)
4315 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4316 tree decl, result;
4317 vec<tree, va_gc> *args;
4319 /* Build up a call to the user-defined operator. */
4320 /* Lookup the name we got back from the id-expression. */
4321 args = make_tree_vector ();
4322 vec_safe_push (args, value);
4323 vec_safe_push (args, build_int_cst (size_type_node, len));
4324 decl = lookup_literal_operator (name, args);
4326 if (decl && decl != error_mark_node)
4328 result = finish_call_expr (decl, &args, false, true,
4329 tf_warning_or_error);
4330 release_tree_vector (args);
4331 return result;
4333 release_tree_vector (args);
4335 /* Look for a template function with typename parameter CharT
4336 and parameter pack CharT... Call the function with
4337 template parameter characters representing the string. */
4338 args = make_tree_vector ();
4339 decl = lookup_literal_operator (name, args);
4340 if (decl && decl != error_mark_node)
4342 tree tmpl_args = make_string_pack (value);
4343 decl = lookup_template_function (decl, tmpl_args);
4344 result = finish_call_expr (decl, &args, false, true,
4345 tf_warning_or_error);
4346 release_tree_vector (args);
4347 return result;
4349 release_tree_vector (args);
4351 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4352 name, TREE_TYPE (value), size_type_node);
4353 return error_mark_node;
4357 /* Basic concepts [gram.basic] */
4359 /* Parse a translation-unit.
4361 translation-unit:
4362 declaration-seq [opt]
4364 Returns TRUE if all went well. */
4366 static bool
4367 cp_parser_translation_unit (cp_parser* parser)
4369 /* The address of the first non-permanent object on the declarator
4370 obstack. */
4371 static void *declarator_obstack_base;
4373 bool success;
4375 /* Create the declarator obstack, if necessary. */
4376 if (!cp_error_declarator)
4378 gcc_obstack_init (&declarator_obstack);
4379 /* Create the error declarator. */
4380 cp_error_declarator = make_declarator (cdk_error);
4381 /* Create the empty parameter list. */
4382 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4383 /* Remember where the base of the declarator obstack lies. */
4384 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4387 cp_parser_declaration_seq_opt (parser);
4389 /* If there are no tokens left then all went well. */
4390 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4392 /* Get rid of the token array; we don't need it any more. */
4393 cp_lexer_destroy (parser->lexer);
4394 parser->lexer = NULL;
4396 /* This file might have been a context that's implicitly extern
4397 "C". If so, pop the lang context. (Only relevant for PCH.) */
4398 if (parser->implicit_extern_c)
4400 pop_lang_context ();
4401 parser->implicit_extern_c = false;
4404 /* Finish up. */
4405 finish_translation_unit ();
4407 success = true;
4409 else
4411 cp_parser_error (parser, "expected declaration");
4412 success = false;
4415 /* Make sure the declarator obstack was fully cleaned up. */
4416 gcc_assert (obstack_next_free (&declarator_obstack)
4417 == declarator_obstack_base);
4419 /* All went well. */
4420 return success;
4423 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4424 decltype context. */
4426 static inline tsubst_flags_t
4427 complain_flags (bool decltype_p)
4429 tsubst_flags_t complain = tf_warning_or_error;
4430 if (decltype_p)
4431 complain |= tf_decltype;
4432 return complain;
4435 /* We're about to parse a collection of statements. If we're currently
4436 parsing tentatively, set up a firewall so that any nested
4437 cp_parser_commit_to_tentative_parse won't affect the current context. */
4439 static cp_token_position
4440 cp_parser_start_tentative_firewall (cp_parser *parser)
4442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4443 return 0;
4445 cp_parser_parse_tentatively (parser);
4446 cp_parser_commit_to_topmost_tentative_parse (parser);
4447 return cp_lexer_token_position (parser->lexer, false);
4450 /* We've finished parsing the collection of statements. Wrap up the
4451 firewall and replace the relevant tokens with the parsed form. */
4453 static void
4454 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4455 tree expr)
4457 if (!start)
4458 return;
4460 /* Finish the firewall level. */
4461 cp_parser_parse_definitely (parser);
4462 /* And remember the result of the parse for when we try again. */
4463 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4464 token->type = CPP_PREPARSED_EXPR;
4465 token->u.value = expr;
4466 token->keyword = RID_MAX;
4467 cp_lexer_purge_tokens_after (parser->lexer, start);
4470 /* Like the above functions, but let the user modify the tokens. Used by
4471 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4472 later parses, so it makes sense to localize the effects of
4473 cp_parser_commit_to_tentative_parse. */
4475 struct tentative_firewall
4477 cp_parser *parser;
4478 bool set;
4480 tentative_firewall (cp_parser *p): parser(p)
4482 /* If we're currently parsing tentatively, start a committed level as a
4483 firewall and then an inner tentative parse. */
4484 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4486 cp_parser_parse_tentatively (parser);
4487 cp_parser_commit_to_topmost_tentative_parse (parser);
4488 cp_parser_parse_tentatively (parser);
4492 ~tentative_firewall()
4494 if (set)
4496 /* Finish the inner tentative parse and the firewall, propagating any
4497 uncommitted error state to the outer tentative parse. */
4498 bool err = cp_parser_error_occurred (parser);
4499 cp_parser_parse_definitely (parser);
4500 cp_parser_parse_definitely (parser);
4501 if (err)
4502 cp_parser_simulate_error (parser);
4507 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4508 This class is for tracking such a matching pair of symbols.
4509 In particular, it tracks the location of the first token,
4510 so that if the second token is missing, we can highlight the
4511 location of the first token when notifying the user about the
4512 problem. */
4514 template <typename traits_t>
4515 class token_pair
4517 public:
4518 /* token_pair's ctor. */
4519 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4521 /* If the next token is the opening symbol for this pair, consume it and
4522 return true.
4523 Otherwise, issue an error and return false.
4524 In either case, record the location of the opening token. */
4526 bool require_open (cp_parser *parser)
4528 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4529 return cp_parser_require (parser, traits_t::open_token_type,
4530 traits_t::required_token_open);
4533 /* Consume the next token from PARSER, recording its location as
4534 that of the opening token within the pair. */
4536 cp_token * consume_open (cp_parser *parser)
4538 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4539 gcc_assert (tok->type == traits_t::open_token_type);
4540 m_open_loc = tok->location;
4541 return tok;
4544 /* If the next token is the closing symbol for this pair, consume it
4545 and return it.
4546 Otherwise, issue an error, highlighting the location of the
4547 corresponding opening token, and return NULL. */
4549 cp_token *require_close (cp_parser *parser) const
4551 return cp_parser_require (parser, traits_t::close_token_type,
4552 traits_t::required_token_close,
4553 m_open_loc);
4556 private:
4557 location_t m_open_loc;
4560 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4562 struct matching_paren_traits
4564 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4565 static const enum required_token required_token_open = RT_OPEN_PAREN;
4566 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4567 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4570 /* "matching_parens" is a token_pair<T> class for tracking matching
4571 pairs of parentheses. */
4573 typedef token_pair<matching_paren_traits> matching_parens;
4575 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4577 struct matching_brace_traits
4579 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4580 static const enum required_token required_token_open = RT_OPEN_BRACE;
4581 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4582 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4585 /* "matching_braces" is a token_pair<T> class for tracking matching
4586 pairs of braces. */
4588 typedef token_pair<matching_brace_traits> matching_braces;
4591 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4592 enclosing parentheses. */
4594 static cp_expr
4595 cp_parser_statement_expr (cp_parser *parser)
4597 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4599 /* Consume the '('. */
4600 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4601 matching_parens parens;
4602 parens.consume_open (parser);
4603 /* Start the statement-expression. */
4604 tree expr = begin_stmt_expr ();
4605 /* Parse the compound-statement. */
4606 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4607 /* Finish up. */
4608 expr = finish_stmt_expr (expr, false);
4609 /* Consume the ')'. */
4610 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4611 if (!parens.require_close (parser))
4612 cp_parser_skip_to_end_of_statement (parser);
4614 cp_parser_end_tentative_firewall (parser, start, expr);
4615 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4616 return cp_expr (expr, combined_loc);
4619 /* Expressions [gram.expr] */
4621 /* Parse a fold-operator.
4623 fold-operator:
4624 - * / % ^ & | = < > << >>
4625 = -= *= /= %= ^= &= |= <<= >>=
4626 == != <= >= && || , .* ->*
4628 This returns the tree code corresponding to the matched operator
4629 as an int. When the current token matches a compound assignment
4630 opertor, the resulting tree code is the negative value of the
4631 non-assignment operator. */
4633 static int
4634 cp_parser_fold_operator (cp_token *token)
4636 switch (token->type)
4638 case CPP_PLUS: return PLUS_EXPR;
4639 case CPP_MINUS: return MINUS_EXPR;
4640 case CPP_MULT: return MULT_EXPR;
4641 case CPP_DIV: return TRUNC_DIV_EXPR;
4642 case CPP_MOD: return TRUNC_MOD_EXPR;
4643 case CPP_XOR: return BIT_XOR_EXPR;
4644 case CPP_AND: return BIT_AND_EXPR;
4645 case CPP_OR: return BIT_IOR_EXPR;
4646 case CPP_LSHIFT: return LSHIFT_EXPR;
4647 case CPP_RSHIFT: return RSHIFT_EXPR;
4649 case CPP_EQ: return -NOP_EXPR;
4650 case CPP_PLUS_EQ: return -PLUS_EXPR;
4651 case CPP_MINUS_EQ: return -MINUS_EXPR;
4652 case CPP_MULT_EQ: return -MULT_EXPR;
4653 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4654 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4655 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4656 case CPP_AND_EQ: return -BIT_AND_EXPR;
4657 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4658 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4659 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4661 case CPP_EQ_EQ: return EQ_EXPR;
4662 case CPP_NOT_EQ: return NE_EXPR;
4663 case CPP_LESS: return LT_EXPR;
4664 case CPP_GREATER: return GT_EXPR;
4665 case CPP_LESS_EQ: return LE_EXPR;
4666 case CPP_GREATER_EQ: return GE_EXPR;
4668 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4669 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4671 case CPP_COMMA: return COMPOUND_EXPR;
4673 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4674 case CPP_DEREF_STAR: return MEMBER_REF;
4676 default: return ERROR_MARK;
4680 /* Returns true if CODE indicates a binary expression, which is not allowed in
4681 the LHS of a fold-expression. More codes will need to be added to use this
4682 function in other contexts. */
4684 static bool
4685 is_binary_op (tree_code code)
4687 switch (code)
4689 case PLUS_EXPR:
4690 case POINTER_PLUS_EXPR:
4691 case MINUS_EXPR:
4692 case MULT_EXPR:
4693 case TRUNC_DIV_EXPR:
4694 case TRUNC_MOD_EXPR:
4695 case BIT_XOR_EXPR:
4696 case BIT_AND_EXPR:
4697 case BIT_IOR_EXPR:
4698 case LSHIFT_EXPR:
4699 case RSHIFT_EXPR:
4701 case MODOP_EXPR:
4703 case EQ_EXPR:
4704 case NE_EXPR:
4705 case LE_EXPR:
4706 case GE_EXPR:
4707 case LT_EXPR:
4708 case GT_EXPR:
4710 case TRUTH_ANDIF_EXPR:
4711 case TRUTH_ORIF_EXPR:
4713 case COMPOUND_EXPR:
4715 case DOTSTAR_EXPR:
4716 case MEMBER_REF:
4717 return true;
4719 default:
4720 return false;
4724 /* If the next token is a suitable fold operator, consume it and return as
4725 the function above. */
4727 static int
4728 cp_parser_fold_operator (cp_parser *parser)
4730 cp_token* token = cp_lexer_peek_token (parser->lexer);
4731 int code = cp_parser_fold_operator (token);
4732 if (code != ERROR_MARK)
4733 cp_lexer_consume_token (parser->lexer);
4734 return code;
4737 /* Parse a fold-expression.
4739 fold-expression:
4740 ( ... folding-operator cast-expression)
4741 ( cast-expression folding-operator ... )
4742 ( cast-expression folding operator ... folding-operator cast-expression)
4744 Note that the '(' and ')' are matched in primary expression. */
4746 static cp_expr
4747 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4749 cp_id_kind pidk;
4751 // Left fold.
4752 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4754 cp_lexer_consume_token (parser->lexer);
4755 int op = cp_parser_fold_operator (parser);
4756 if (op == ERROR_MARK)
4758 cp_parser_error (parser, "expected binary operator");
4759 return error_mark_node;
4762 tree expr = cp_parser_cast_expression (parser, false, false,
4763 false, &pidk);
4764 if (expr == error_mark_node)
4765 return error_mark_node;
4766 return finish_left_unary_fold_expr (expr, op);
4769 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4770 int op = cp_parser_fold_operator (parser);
4771 if (op == ERROR_MARK)
4773 cp_parser_error (parser, "expected binary operator");
4774 return error_mark_node;
4777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4779 cp_parser_error (parser, "expected ...");
4780 return error_mark_node;
4782 cp_lexer_consume_token (parser->lexer);
4784 /* The operands of a fold-expression are cast-expressions, so binary or
4785 conditional expressions are not allowed. We check this here to avoid
4786 tentative parsing. */
4787 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4788 /* OK, the expression was parenthesized. */;
4789 else if (is_binary_op (TREE_CODE (expr1)))
4790 error_at (location_of (expr1),
4791 "binary expression in operand of fold-expression");
4792 else if (TREE_CODE (expr1) == COND_EXPR)
4793 error_at (location_of (expr1),
4794 "conditional expression in operand of fold-expression");
4796 // Right fold.
4797 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4798 return finish_right_unary_fold_expr (expr1, op);
4800 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4802 cp_parser_error (parser, "mismatched operator in fold-expression");
4803 return error_mark_node;
4805 cp_lexer_consume_token (parser->lexer);
4807 // Binary left or right fold.
4808 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4809 if (expr2 == error_mark_node)
4810 return error_mark_node;
4811 return finish_binary_fold_expr (expr1, expr2, op);
4814 /* Parse a primary-expression.
4816 primary-expression:
4817 literal
4818 this
4819 ( expression )
4820 id-expression
4821 lambda-expression (C++11)
4823 GNU Extensions:
4825 primary-expression:
4826 ( compound-statement )
4827 __builtin_va_arg ( assignment-expression , type-id )
4828 __builtin_offsetof ( type-id , offsetof-expression )
4830 C++ Extensions:
4831 __has_nothrow_assign ( type-id )
4832 __has_nothrow_constructor ( type-id )
4833 __has_nothrow_copy ( type-id )
4834 __has_trivial_assign ( type-id )
4835 __has_trivial_constructor ( type-id )
4836 __has_trivial_copy ( type-id )
4837 __has_trivial_destructor ( type-id )
4838 __has_virtual_destructor ( type-id )
4839 __is_abstract ( type-id )
4840 __is_base_of ( type-id , type-id )
4841 __is_class ( type-id )
4842 __is_empty ( type-id )
4843 __is_enum ( type-id )
4844 __is_final ( type-id )
4845 __is_literal_type ( type-id )
4846 __is_pod ( type-id )
4847 __is_polymorphic ( type-id )
4848 __is_std_layout ( type-id )
4849 __is_trivial ( type-id )
4850 __is_union ( type-id )
4852 Objective-C++ Extension:
4854 primary-expression:
4855 objc-expression
4857 literal:
4858 __null
4860 ADDRESS_P is true iff this expression was immediately preceded by
4861 "&" and therefore might denote a pointer-to-member. CAST_P is true
4862 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4863 true iff this expression is a template argument.
4865 Returns a representation of the expression. Upon return, *IDK
4866 indicates what kind of id-expression (if any) was present. */
4868 static cp_expr
4869 cp_parser_primary_expression (cp_parser *parser,
4870 bool address_p,
4871 bool cast_p,
4872 bool template_arg_p,
4873 bool decltype_p,
4874 cp_id_kind *idk)
4876 cp_token *token = NULL;
4878 /* Assume the primary expression is not an id-expression. */
4879 *idk = CP_ID_KIND_NONE;
4881 /* Peek at the next token. */
4882 token = cp_lexer_peek_token (parser->lexer);
4883 switch ((int) token->type)
4885 /* literal:
4886 integer-literal
4887 character-literal
4888 floating-literal
4889 string-literal
4890 boolean-literal
4891 pointer-literal
4892 user-defined-literal */
4893 case CPP_CHAR:
4894 case CPP_CHAR16:
4895 case CPP_CHAR32:
4896 case CPP_WCHAR:
4897 case CPP_UTF8CHAR:
4898 case CPP_NUMBER:
4899 case CPP_PREPARSED_EXPR:
4900 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4901 return cp_parser_userdef_numeric_literal (parser);
4902 token = cp_lexer_consume_token (parser->lexer);
4903 if (TREE_CODE (token->u.value) == FIXED_CST)
4905 error_at (token->location,
4906 "fixed-point types not supported in C++");
4907 return error_mark_node;
4909 /* Floating-point literals are only allowed in an integral
4910 constant expression if they are cast to an integral or
4911 enumeration type. */
4912 if (TREE_CODE (token->u.value) == REAL_CST
4913 && parser->integral_constant_expression_p
4914 && pedantic)
4916 /* CAST_P will be set even in invalid code like "int(2.7 +
4917 ...)". Therefore, we have to check that the next token
4918 is sure to end the cast. */
4919 if (cast_p)
4921 cp_token *next_token;
4923 next_token = cp_lexer_peek_token (parser->lexer);
4924 if (/* The comma at the end of an
4925 enumerator-definition. */
4926 next_token->type != CPP_COMMA
4927 /* The curly brace at the end of an enum-specifier. */
4928 && next_token->type != CPP_CLOSE_BRACE
4929 /* The end of a statement. */
4930 && next_token->type != CPP_SEMICOLON
4931 /* The end of the cast-expression. */
4932 && next_token->type != CPP_CLOSE_PAREN
4933 /* The end of an array bound. */
4934 && next_token->type != CPP_CLOSE_SQUARE
4935 /* The closing ">" in a template-argument-list. */
4936 && (next_token->type != CPP_GREATER
4937 || parser->greater_than_is_operator_p)
4938 /* C++0x only: A ">>" treated like two ">" tokens,
4939 in a template-argument-list. */
4940 && (next_token->type != CPP_RSHIFT
4941 || (cxx_dialect == cxx98)
4942 || parser->greater_than_is_operator_p))
4943 cast_p = false;
4946 /* If we are within a cast, then the constraint that the
4947 cast is to an integral or enumeration type will be
4948 checked at that point. If we are not within a cast, then
4949 this code is invalid. */
4950 if (!cast_p)
4951 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4953 return cp_expr (token->u.value, token->location);
4955 case CPP_CHAR_USERDEF:
4956 case CPP_CHAR16_USERDEF:
4957 case CPP_CHAR32_USERDEF:
4958 case CPP_WCHAR_USERDEF:
4959 case CPP_UTF8CHAR_USERDEF:
4960 return cp_parser_userdef_char_literal (parser);
4962 case CPP_STRING:
4963 case CPP_STRING16:
4964 case CPP_STRING32:
4965 case CPP_WSTRING:
4966 case CPP_UTF8STRING:
4967 case CPP_STRING_USERDEF:
4968 case CPP_STRING16_USERDEF:
4969 case CPP_STRING32_USERDEF:
4970 case CPP_WSTRING_USERDEF:
4971 case CPP_UTF8STRING_USERDEF:
4972 /* ??? Should wide strings be allowed when parser->translate_strings_p
4973 is false (i.e. in attributes)? If not, we can kill the third
4974 argument to cp_parser_string_literal. */
4975 return cp_parser_string_literal (parser,
4976 parser->translate_strings_p,
4977 true);
4979 case CPP_OPEN_PAREN:
4980 /* If we see `( { ' then we are looking at the beginning of
4981 a GNU statement-expression. */
4982 if (cp_parser_allow_gnu_extensions_p (parser)
4983 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4985 /* Statement-expressions are not allowed by the standard. */
4986 pedwarn (token->location, OPT_Wpedantic,
4987 "ISO C++ forbids braced-groups within expressions");
4989 /* And they're not allowed outside of a function-body; you
4990 cannot, for example, write:
4992 int i = ({ int j = 3; j + 1; });
4994 at class or namespace scope. */
4995 if (!parser->in_function_body
4996 || parser->in_template_argument_list_p)
4998 error_at (token->location,
4999 "statement-expressions are not allowed outside "
5000 "functions nor in template-argument lists");
5001 cp_parser_skip_to_end_of_block_or_statement (parser);
5002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5003 cp_lexer_consume_token (parser->lexer);
5004 return error_mark_node;
5006 else
5007 return cp_parser_statement_expr (parser);
5009 /* Otherwise it's a normal parenthesized expression. */
5011 cp_expr expr;
5012 bool saved_greater_than_is_operator_p;
5014 location_t open_paren_loc = token->location;
5016 /* Consume the `('. */
5017 matching_parens parens;
5018 parens.consume_open (parser);
5019 /* Within a parenthesized expression, a `>' token is always
5020 the greater-than operator. */
5021 saved_greater_than_is_operator_p
5022 = parser->greater_than_is_operator_p;
5023 parser->greater_than_is_operator_p = true;
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5026 /* Left fold expression. */
5027 expr = NULL_TREE;
5028 else
5029 /* Parse the parenthesized expression. */
5030 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5032 token = cp_lexer_peek_token (parser->lexer);
5033 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5035 expr = cp_parser_fold_expression (parser, expr);
5036 if (expr != error_mark_node
5037 && cxx_dialect < cxx1z
5038 && !in_system_header_at (input_location))
5039 pedwarn (input_location, 0, "fold-expressions only available "
5040 "with -std=c++1z or -std=gnu++1z");
5042 else
5043 /* Let the front end know that this expression was
5044 enclosed in parentheses. This matters in case, for
5045 example, the expression is of the form `A::B', since
5046 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5047 not. */
5048 expr = finish_parenthesized_expr (expr);
5050 /* DR 705: Wrapping an unqualified name in parentheses
5051 suppresses arg-dependent lookup. We want to pass back
5052 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5053 (c++/37862), but none of the others. */
5054 if (*idk != CP_ID_KIND_QUALIFIED)
5055 *idk = CP_ID_KIND_NONE;
5057 /* The `>' token might be the end of a template-id or
5058 template-parameter-list now. */
5059 parser->greater_than_is_operator_p
5060 = saved_greater_than_is_operator_p;
5062 /* Consume the `)'. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 location_t close_paren_loc = token->location;
5065 expr.set_range (open_paren_loc, close_paren_loc);
5066 if (!parens.require_close (parser)
5067 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5068 cp_parser_skip_to_end_of_statement (parser);
5070 return expr;
5073 case CPP_OPEN_SQUARE:
5075 if (c_dialect_objc ())
5077 /* We might have an Objective-C++ message. */
5078 cp_parser_parse_tentatively (parser);
5079 tree msg = cp_parser_objc_message_expression (parser);
5080 /* If that works out, we're done ... */
5081 if (cp_parser_parse_definitely (parser))
5082 return msg;
5083 /* ... else, fall though to see if it's a lambda. */
5085 cp_expr lam = cp_parser_lambda_expression (parser);
5086 /* Don't warn about a failed tentative parse. */
5087 if (cp_parser_error_occurred (parser))
5088 return error_mark_node;
5089 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5090 return lam;
5093 case CPP_OBJC_STRING:
5094 if (c_dialect_objc ())
5095 /* We have an Objective-C++ string literal. */
5096 return cp_parser_objc_expression (parser);
5097 cp_parser_error (parser, "expected primary-expression");
5098 return error_mark_node;
5100 case CPP_KEYWORD:
5101 switch (token->keyword)
5103 /* These two are the boolean literals. */
5104 case RID_TRUE:
5105 cp_lexer_consume_token (parser->lexer);
5106 return cp_expr (boolean_true_node, token->location);
5107 case RID_FALSE:
5108 cp_lexer_consume_token (parser->lexer);
5109 return cp_expr (boolean_false_node, token->location);
5111 /* The `__null' literal. */
5112 case RID_NULL:
5113 cp_lexer_consume_token (parser->lexer);
5114 return cp_expr (null_node, token->location);
5116 /* The `nullptr' literal. */
5117 case RID_NULLPTR:
5118 cp_lexer_consume_token (parser->lexer);
5119 return cp_expr (nullptr_node, token->location);
5121 /* Recognize the `this' keyword. */
5122 case RID_THIS:
5123 cp_lexer_consume_token (parser->lexer);
5124 if (parser->local_variables_forbidden_p)
5126 error_at (token->location,
5127 "%<this%> may not be used in this context");
5128 return error_mark_node;
5130 /* Pointers cannot appear in constant-expressions. */
5131 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5132 return error_mark_node;
5133 return cp_expr (finish_this_expr (), token->location);
5135 /* The `operator' keyword can be the beginning of an
5136 id-expression. */
5137 case RID_OPERATOR:
5138 goto id_expression;
5140 case RID_FUNCTION_NAME:
5141 case RID_PRETTY_FUNCTION_NAME:
5142 case RID_C99_FUNCTION_NAME:
5144 non_integral_constant name;
5146 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5147 __func__ are the names of variables -- but they are
5148 treated specially. Therefore, they are handled here,
5149 rather than relying on the generic id-expression logic
5150 below. Grammatically, these names are id-expressions.
5152 Consume the token. */
5153 token = cp_lexer_consume_token (parser->lexer);
5155 switch (token->keyword)
5157 case RID_FUNCTION_NAME:
5158 name = NIC_FUNC_NAME;
5159 break;
5160 case RID_PRETTY_FUNCTION_NAME:
5161 name = NIC_PRETTY_FUNC;
5162 break;
5163 case RID_C99_FUNCTION_NAME:
5164 name = NIC_C99_FUNC;
5165 break;
5166 default:
5167 gcc_unreachable ();
5170 if (cp_parser_non_integral_constant_expression (parser, name))
5171 return error_mark_node;
5173 /* Look up the name. */
5174 return finish_fname (token->u.value);
5177 case RID_VA_ARG:
5179 tree expression;
5180 tree type;
5181 source_location type_location;
5182 location_t start_loc
5183 = cp_lexer_peek_token (parser->lexer)->location;
5184 /* The `__builtin_va_arg' construct is used to handle
5185 `va_arg'. Consume the `__builtin_va_arg' token. */
5186 cp_lexer_consume_token (parser->lexer);
5187 /* Look for the opening `('. */
5188 matching_parens parens;
5189 parens.require_open (parser);
5190 /* Now, parse the assignment-expression. */
5191 expression = cp_parser_assignment_expression (parser);
5192 /* Look for the `,'. */
5193 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5194 type_location = cp_lexer_peek_token (parser->lexer)->location;
5195 /* Parse the type-id. */
5197 type_id_in_expr_sentinel s (parser);
5198 type = cp_parser_type_id (parser);
5200 /* Look for the closing `)'. */
5201 location_t finish_loc
5202 = cp_lexer_peek_token (parser->lexer)->location;
5203 parens.require_close (parser);
5204 /* Using `va_arg' in a constant-expression is not
5205 allowed. */
5206 if (cp_parser_non_integral_constant_expression (parser,
5207 NIC_VA_ARG))
5208 return error_mark_node;
5209 /* Construct a location of the form:
5210 __builtin_va_arg (v, int)
5211 ~~~~~~~~~~~~~~~~~~~~~^~~~
5212 with the caret at the type, ranging from the start of the
5213 "__builtin_va_arg" token to the close paren. */
5214 location_t combined_loc
5215 = make_location (type_location, start_loc, finish_loc);
5216 return build_x_va_arg (combined_loc, expression, type);
5219 case RID_OFFSETOF:
5220 return cp_parser_builtin_offsetof (parser);
5222 case RID_HAS_NOTHROW_ASSIGN:
5223 case RID_HAS_NOTHROW_CONSTRUCTOR:
5224 case RID_HAS_NOTHROW_COPY:
5225 case RID_HAS_TRIVIAL_ASSIGN:
5226 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5227 case RID_HAS_TRIVIAL_COPY:
5228 case RID_HAS_TRIVIAL_DESTRUCTOR:
5229 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5230 case RID_HAS_VIRTUAL_DESTRUCTOR:
5231 case RID_IS_ABSTRACT:
5232 case RID_IS_AGGREGATE:
5233 case RID_IS_BASE_OF:
5234 case RID_IS_CLASS:
5235 case RID_IS_EMPTY:
5236 case RID_IS_ENUM:
5237 case RID_IS_FINAL:
5238 case RID_IS_LITERAL_TYPE:
5239 case RID_IS_POD:
5240 case RID_IS_POLYMORPHIC:
5241 case RID_IS_SAME_AS:
5242 case RID_IS_STD_LAYOUT:
5243 case RID_IS_TRIVIAL:
5244 case RID_IS_TRIVIALLY_ASSIGNABLE:
5245 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5246 case RID_IS_TRIVIALLY_COPYABLE:
5247 case RID_IS_UNION:
5248 case RID_IS_ASSIGNABLE:
5249 case RID_IS_CONSTRUCTIBLE:
5250 return cp_parser_trait_expr (parser, token->keyword);
5252 // C++ concepts
5253 case RID_REQUIRES:
5254 return cp_parser_requires_expression (parser);
5256 /* Objective-C++ expressions. */
5257 case RID_AT_ENCODE:
5258 case RID_AT_PROTOCOL:
5259 case RID_AT_SELECTOR:
5260 return cp_parser_objc_expression (parser);
5262 case RID_TEMPLATE:
5263 if (parser->in_function_body
5264 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5265 == CPP_LESS))
5267 error_at (token->location,
5268 "a template declaration cannot appear at block scope");
5269 cp_parser_skip_to_end_of_block_or_statement (parser);
5270 return error_mark_node;
5272 /* FALLTHRU */
5273 default:
5274 cp_parser_error (parser, "expected primary-expression");
5275 return error_mark_node;
5278 /* An id-expression can start with either an identifier, a
5279 `::' as the beginning of a qualified-id, or the "operator"
5280 keyword. */
5281 case CPP_NAME:
5282 case CPP_SCOPE:
5283 case CPP_TEMPLATE_ID:
5284 case CPP_NESTED_NAME_SPECIFIER:
5286 id_expression:
5287 cp_expr id_expression;
5288 cp_expr decl;
5289 const char *error_msg;
5290 bool template_p;
5291 bool done;
5292 cp_token *id_expr_token;
5294 /* Parse the id-expression. */
5295 id_expression
5296 = cp_parser_id_expression (parser,
5297 /*template_keyword_p=*/false,
5298 /*check_dependency_p=*/true,
5299 &template_p,
5300 /*declarator_p=*/false,
5301 /*optional_p=*/false);
5302 if (id_expression == error_mark_node)
5303 return error_mark_node;
5304 id_expr_token = token;
5305 token = cp_lexer_peek_token (parser->lexer);
5306 done = (token->type != CPP_OPEN_SQUARE
5307 && token->type != CPP_OPEN_PAREN
5308 && token->type != CPP_DOT
5309 && token->type != CPP_DEREF
5310 && token->type != CPP_PLUS_PLUS
5311 && token->type != CPP_MINUS_MINUS);
5312 /* If we have a template-id, then no further lookup is
5313 required. If the template-id was for a template-class, we
5314 will sometimes have a TYPE_DECL at this point. */
5315 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5316 || TREE_CODE (id_expression) == TYPE_DECL)
5317 decl = id_expression;
5318 /* Look up the name. */
5319 else
5321 tree ambiguous_decls;
5323 /* If we already know that this lookup is ambiguous, then
5324 we've already issued an error message; there's no reason
5325 to check again. */
5326 if (id_expr_token->type == CPP_NAME
5327 && id_expr_token->error_reported)
5329 cp_parser_simulate_error (parser);
5330 return error_mark_node;
5333 decl = cp_parser_lookup_name (parser, id_expression,
5334 none_type,
5335 template_p,
5336 /*is_namespace=*/false,
5337 /*check_dependency=*/true,
5338 &ambiguous_decls,
5339 id_expr_token->location);
5340 /* If the lookup was ambiguous, an error will already have
5341 been issued. */
5342 if (ambiguous_decls)
5343 return error_mark_node;
5345 /* In Objective-C++, we may have an Objective-C 2.0
5346 dot-syntax for classes here. */
5347 if (c_dialect_objc ()
5348 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5349 && TREE_CODE (decl) == TYPE_DECL
5350 && objc_is_class_name (decl))
5352 tree component;
5353 cp_lexer_consume_token (parser->lexer);
5354 component = cp_parser_identifier (parser);
5355 if (component == error_mark_node)
5356 return error_mark_node;
5358 tree result = objc_build_class_component_ref (id_expression,
5359 component);
5360 /* Build a location of the form:
5361 expr.component
5362 ~~~~~^~~~~~~~~
5363 with caret at the start of the component name (at
5364 input_location), ranging from the start of the id_expression
5365 to the end of the component name. */
5366 location_t combined_loc
5367 = make_location (input_location, id_expression.get_start (),
5368 get_finish (input_location));
5369 protected_set_expr_location (result, combined_loc);
5370 return result;
5373 /* In Objective-C++, an instance variable (ivar) may be preferred
5374 to whatever cp_parser_lookup_name() found.
5375 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5376 rest of c-family, we have to do a little extra work to preserve
5377 any location information in cp_expr "decl". Given that
5378 objc_lookup_ivar is implemented in "c-family" and "objc", we
5379 have a trip through the pure "tree" type, rather than cp_expr.
5380 Naively copying it back to "decl" would implicitly give the
5381 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5382 store an EXPR_LOCATION. Hence we only update "decl" (and
5383 hence its location_t) if we get back a different tree node. */
5384 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5385 id_expression);
5386 if (decl_tree != decl.get_value ())
5387 decl = cp_expr (decl_tree);
5389 /* If name lookup gives us a SCOPE_REF, then the
5390 qualifying scope was dependent. */
5391 if (TREE_CODE (decl) == SCOPE_REF)
5393 /* At this point, we do not know if DECL is a valid
5394 integral constant expression. We assume that it is
5395 in fact such an expression, so that code like:
5397 template <int N> struct A {
5398 int a[B<N>::i];
5401 is accepted. At template-instantiation time, we
5402 will check that B<N>::i is actually a constant. */
5403 return decl;
5405 /* Check to see if DECL is a local variable in a context
5406 where that is forbidden. */
5407 if (parser->local_variables_forbidden_p
5408 && local_variable_p (decl))
5410 /* It might be that we only found DECL because we are
5411 trying to be generous with pre-ISO scoping rules.
5412 For example, consider:
5414 int i;
5415 void g() {
5416 for (int i = 0; i < 10; ++i) {}
5417 extern void f(int j = i);
5420 Here, name look up will originally find the out
5421 of scope `i'. We need to issue a warning message,
5422 but then use the global `i'. */
5423 decl = check_for_out_of_scope_variable (decl);
5424 if (local_variable_p (decl))
5426 error_at (id_expr_token->location,
5427 "local variable %qD may not appear in this context",
5428 decl.get_value ());
5429 return error_mark_node;
5434 decl = (finish_id_expression
5435 (id_expression, decl, parser->scope,
5436 idk,
5437 parser->integral_constant_expression_p,
5438 parser->allow_non_integral_constant_expression_p,
5439 &parser->non_integral_constant_expression_p,
5440 template_p, done, address_p,
5441 template_arg_p,
5442 &error_msg,
5443 id_expression.get_location ()));
5444 if (error_msg)
5445 cp_parser_error (parser, error_msg);
5446 decl.set_location (id_expr_token->location);
5447 return decl;
5450 /* Anything else is an error. */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5457 static inline cp_expr
5458 cp_parser_primary_expression (cp_parser *parser,
5459 bool address_p,
5460 bool cast_p,
5461 bool template_arg_p,
5462 cp_id_kind *idk)
5464 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5465 /*decltype*/false, idk);
5468 /* Parse an id-expression.
5470 id-expression:
5471 unqualified-id
5472 qualified-id
5474 qualified-id:
5475 :: [opt] nested-name-specifier template [opt] unqualified-id
5476 :: identifier
5477 :: operator-function-id
5478 :: template-id
5480 Return a representation of the unqualified portion of the
5481 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5482 a `::' or nested-name-specifier.
5484 Often, if the id-expression was a qualified-id, the caller will
5485 want to make a SCOPE_REF to represent the qualified-id. This
5486 function does not do this in order to avoid wastefully creating
5487 SCOPE_REFs when they are not required.
5489 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5490 `template' keyword.
5492 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5493 uninstantiated templates.
5495 If *TEMPLATE_P is non-NULL, it is set to true iff the
5496 `template' keyword is used to explicitly indicate that the entity
5497 named is a template.
5499 If DECLARATOR_P is true, the id-expression is appearing as part of
5500 a declarator, rather than as part of an expression. */
5502 static cp_expr
5503 cp_parser_id_expression (cp_parser *parser,
5504 bool template_keyword_p,
5505 bool check_dependency_p,
5506 bool *template_p,
5507 bool declarator_p,
5508 bool optional_p)
5510 bool global_scope_p;
5511 bool nested_name_specifier_p;
5513 /* Assume the `template' keyword was not used. */
5514 if (template_p)
5515 *template_p = template_keyword_p;
5517 /* Look for the optional `::' operator. */
5518 global_scope_p
5519 = (!template_keyword_p
5520 && (cp_parser_global_scope_opt (parser,
5521 /*current_scope_valid_p=*/false)
5522 != NULL_TREE));
5524 /* Look for the optional nested-name-specifier. */
5525 nested_name_specifier_p
5526 = (cp_parser_nested_name_specifier_opt (parser,
5527 /*typename_keyword_p=*/false,
5528 check_dependency_p,
5529 /*type_p=*/false,
5530 declarator_p,
5531 template_keyword_p)
5532 != NULL_TREE);
5534 /* If there is a nested-name-specifier, then we are looking at
5535 the first qualified-id production. */
5536 if (nested_name_specifier_p)
5538 tree saved_scope;
5539 tree saved_object_scope;
5540 tree saved_qualifying_scope;
5541 cp_expr unqualified_id;
5542 bool is_template;
5544 /* See if the next token is the `template' keyword. */
5545 if (!template_p)
5546 template_p = &is_template;
5547 *template_p = cp_parser_optional_template_keyword (parser);
5548 /* Name lookup we do during the processing of the
5549 unqualified-id might obliterate SCOPE. */
5550 saved_scope = parser->scope;
5551 saved_object_scope = parser->object_scope;
5552 saved_qualifying_scope = parser->qualifying_scope;
5553 /* Process the final unqualified-id. */
5554 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5555 check_dependency_p,
5556 declarator_p,
5557 /*optional_p=*/false);
5558 /* Restore the SAVED_SCOPE for our caller. */
5559 parser->scope = saved_scope;
5560 parser->object_scope = saved_object_scope;
5561 parser->qualifying_scope = saved_qualifying_scope;
5563 return unqualified_id;
5565 /* Otherwise, if we are in global scope, then we are looking at one
5566 of the other qualified-id productions. */
5567 else if (global_scope_p)
5569 cp_token *token;
5570 tree id;
5572 /* Peek at the next token. */
5573 token = cp_lexer_peek_token (parser->lexer);
5575 /* If it's an identifier, and the next token is not a "<", then
5576 we can avoid the template-id case. This is an optimization
5577 for this common case. */
5578 if (token->type == CPP_NAME
5579 && !cp_parser_nth_token_starts_template_argument_list_p
5580 (parser, 2))
5581 return cp_parser_identifier (parser);
5583 cp_parser_parse_tentatively (parser);
5584 /* Try a template-id. */
5585 id = cp_parser_template_id (parser,
5586 /*template_keyword_p=*/false,
5587 /*check_dependency_p=*/true,
5588 none_type,
5589 declarator_p);
5590 /* If that worked, we're done. */
5591 if (cp_parser_parse_definitely (parser))
5592 return id;
5594 /* Peek at the next token. (Changes in the token buffer may
5595 have invalidated the pointer obtained above.) */
5596 token = cp_lexer_peek_token (parser->lexer);
5598 switch (token->type)
5600 case CPP_NAME:
5601 return cp_parser_identifier (parser);
5603 case CPP_KEYWORD:
5604 if (token->keyword == RID_OPERATOR)
5605 return cp_parser_operator_function_id (parser);
5606 /* Fall through. */
5608 default:
5609 cp_parser_error (parser, "expected id-expression");
5610 return error_mark_node;
5613 else
5614 return cp_parser_unqualified_id (parser, template_keyword_p,
5615 /*check_dependency_p=*/true,
5616 declarator_p,
5617 optional_p);
5620 /* Parse an unqualified-id.
5622 unqualified-id:
5623 identifier
5624 operator-function-id
5625 conversion-function-id
5626 ~ class-name
5627 template-id
5629 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5630 keyword, in a construct like `A::template ...'.
5632 Returns a representation of unqualified-id. For the `identifier'
5633 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5634 production a BIT_NOT_EXPR is returned; the operand of the
5635 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5636 other productions, see the documentation accompanying the
5637 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5638 names are looked up in uninstantiated templates. If DECLARATOR_P
5639 is true, the unqualified-id is appearing as part of a declarator,
5640 rather than as part of an expression. */
5642 static cp_expr
5643 cp_parser_unqualified_id (cp_parser* parser,
5644 bool template_keyword_p,
5645 bool check_dependency_p,
5646 bool declarator_p,
5647 bool optional_p)
5649 cp_token *token;
5651 /* Peek at the next token. */
5652 token = cp_lexer_peek_token (parser->lexer);
5654 switch ((int) token->type)
5656 case CPP_NAME:
5658 tree id;
5660 /* We don't know yet whether or not this will be a
5661 template-id. */
5662 cp_parser_parse_tentatively (parser);
5663 /* Try a template-id. */
5664 id = cp_parser_template_id (parser, template_keyword_p,
5665 check_dependency_p,
5666 none_type,
5667 declarator_p);
5668 /* If it worked, we're done. */
5669 if (cp_parser_parse_definitely (parser))
5670 return id;
5671 /* Otherwise, it's an ordinary identifier. */
5672 return cp_parser_identifier (parser);
5675 case CPP_TEMPLATE_ID:
5676 return cp_parser_template_id (parser, template_keyword_p,
5677 check_dependency_p,
5678 none_type,
5679 declarator_p);
5681 case CPP_COMPL:
5683 tree type_decl;
5684 tree qualifying_scope;
5685 tree object_scope;
5686 tree scope;
5687 bool done;
5689 /* Consume the `~' token. */
5690 cp_lexer_consume_token (parser->lexer);
5691 /* Parse the class-name. The standard, as written, seems to
5692 say that:
5694 template <typename T> struct S { ~S (); };
5695 template <typename T> S<T>::~S() {}
5697 is invalid, since `~' must be followed by a class-name, but
5698 `S<T>' is dependent, and so not known to be a class.
5699 That's not right; we need to look in uninstantiated
5700 templates. A further complication arises from:
5702 template <typename T> void f(T t) {
5703 t.T::~T();
5706 Here, it is not possible to look up `T' in the scope of `T'
5707 itself. We must look in both the current scope, and the
5708 scope of the containing complete expression.
5710 Yet another issue is:
5712 struct S {
5713 int S;
5714 ~S();
5717 S::~S() {}
5719 The standard does not seem to say that the `S' in `~S'
5720 should refer to the type `S' and not the data member
5721 `S::S'. */
5723 /* DR 244 says that we look up the name after the "~" in the
5724 same scope as we looked up the qualifying name. That idea
5725 isn't fully worked out; it's more complicated than that. */
5726 scope = parser->scope;
5727 object_scope = parser->object_scope;
5728 qualifying_scope = parser->qualifying_scope;
5730 /* Check for invalid scopes. */
5731 if (scope == error_mark_node)
5733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5734 cp_lexer_consume_token (parser->lexer);
5735 return error_mark_node;
5737 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5740 error_at (token->location,
5741 "scope %qT before %<~%> is not a class-name",
5742 scope);
5743 cp_parser_simulate_error (parser);
5744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5745 cp_lexer_consume_token (parser->lexer);
5746 return error_mark_node;
5748 gcc_assert (!scope || TYPE_P (scope));
5750 /* If the name is of the form "X::~X" it's OK even if X is a
5751 typedef. */
5752 token = cp_lexer_peek_token (parser->lexer);
5753 if (scope
5754 && token->type == CPP_NAME
5755 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5756 != CPP_LESS)
5757 && (token->u.value == TYPE_IDENTIFIER (scope)
5758 || (CLASS_TYPE_P (scope)
5759 && constructor_name_p (token->u.value, scope))))
5761 cp_lexer_consume_token (parser->lexer);
5762 return build_nt (BIT_NOT_EXPR, scope);
5765 /* ~auto means the destructor of whatever the object is. */
5766 if (cp_parser_is_keyword (token, RID_AUTO))
5768 if (cxx_dialect < cxx14)
5769 pedwarn (input_location, 0,
5770 "%<~auto%> only available with "
5771 "-std=c++14 or -std=gnu++14");
5772 cp_lexer_consume_token (parser->lexer);
5773 return build_nt (BIT_NOT_EXPR, make_auto ());
5776 /* If there was an explicit qualification (S::~T), first look
5777 in the scope given by the qualification (i.e., S).
5779 Note: in the calls to cp_parser_class_name below we pass
5780 typename_type so that lookup finds the injected-class-name
5781 rather than the constructor. */
5782 done = false;
5783 type_decl = NULL_TREE;
5784 if (scope)
5786 cp_parser_parse_tentatively (parser);
5787 type_decl = cp_parser_class_name (parser,
5788 /*typename_keyword_p=*/false,
5789 /*template_keyword_p=*/false,
5790 typename_type,
5791 /*check_dependency=*/false,
5792 /*class_head_p=*/false,
5793 declarator_p);
5794 if (cp_parser_parse_definitely (parser))
5795 done = true;
5797 /* In "N::S::~S", look in "N" as well. */
5798 if (!done && scope && qualifying_scope)
5800 cp_parser_parse_tentatively (parser);
5801 parser->scope = qualifying_scope;
5802 parser->object_scope = NULL_TREE;
5803 parser->qualifying_scope = NULL_TREE;
5804 type_decl
5805 = cp_parser_class_name (parser,
5806 /*typename_keyword_p=*/false,
5807 /*template_keyword_p=*/false,
5808 typename_type,
5809 /*check_dependency=*/false,
5810 /*class_head_p=*/false,
5811 declarator_p);
5812 if (cp_parser_parse_definitely (parser))
5813 done = true;
5815 /* In "p->S::~T", look in the scope given by "*p" as well. */
5816 else if (!done && object_scope)
5818 cp_parser_parse_tentatively (parser);
5819 parser->scope = object_scope;
5820 parser->object_scope = NULL_TREE;
5821 parser->qualifying_scope = NULL_TREE;
5822 type_decl
5823 = cp_parser_class_name (parser,
5824 /*typename_keyword_p=*/false,
5825 /*template_keyword_p=*/false,
5826 typename_type,
5827 /*check_dependency=*/false,
5828 /*class_head_p=*/false,
5829 declarator_p);
5830 if (cp_parser_parse_definitely (parser))
5831 done = true;
5833 /* Look in the surrounding context. */
5834 if (!done)
5836 parser->scope = NULL_TREE;
5837 parser->object_scope = NULL_TREE;
5838 parser->qualifying_scope = NULL_TREE;
5839 if (processing_template_decl)
5840 cp_parser_parse_tentatively (parser);
5841 type_decl
5842 = cp_parser_class_name (parser,
5843 /*typename_keyword_p=*/false,
5844 /*template_keyword_p=*/false,
5845 typename_type,
5846 /*check_dependency=*/false,
5847 /*class_head_p=*/false,
5848 declarator_p);
5849 if (processing_template_decl
5850 && ! cp_parser_parse_definitely (parser))
5852 /* We couldn't find a type with this name. If we're parsing
5853 tentatively, fail and try something else. */
5854 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5856 cp_parser_simulate_error (parser);
5857 return error_mark_node;
5859 /* Otherwise, accept it and check for a match at instantiation
5860 time. */
5861 type_decl = cp_parser_identifier (parser);
5862 if (type_decl != error_mark_node)
5863 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5864 return type_decl;
5867 /* If an error occurred, assume that the name of the
5868 destructor is the same as the name of the qualifying
5869 class. That allows us to keep parsing after running
5870 into ill-formed destructor names. */
5871 if (type_decl == error_mark_node && scope)
5872 return build_nt (BIT_NOT_EXPR, scope);
5873 else if (type_decl == error_mark_node)
5874 return error_mark_node;
5876 /* Check that destructor name and scope match. */
5877 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5880 error_at (token->location,
5881 "declaration of %<~%T%> as member of %qT",
5882 type_decl, scope);
5883 cp_parser_simulate_error (parser);
5884 return error_mark_node;
5887 /* [class.dtor]
5889 A typedef-name that names a class shall not be used as the
5890 identifier in the declarator for a destructor declaration. */
5891 if (declarator_p
5892 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5893 && !DECL_SELF_REFERENCE_P (type_decl)
5894 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5895 error_at (token->location,
5896 "typedef-name %qD used as destructor declarator",
5897 type_decl);
5899 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5902 case CPP_KEYWORD:
5903 if (token->keyword == RID_OPERATOR)
5905 cp_expr id;
5907 /* This could be a template-id, so we try that first. */
5908 cp_parser_parse_tentatively (parser);
5909 /* Try a template-id. */
5910 id = cp_parser_template_id (parser, template_keyword_p,
5911 /*check_dependency_p=*/true,
5912 none_type,
5913 declarator_p);
5914 /* If that worked, we're done. */
5915 if (cp_parser_parse_definitely (parser))
5916 return id;
5917 /* We still don't know whether we're looking at an
5918 operator-function-id or a conversion-function-id. */
5919 cp_parser_parse_tentatively (parser);
5920 /* Try an operator-function-id. */
5921 id = cp_parser_operator_function_id (parser);
5922 /* If that didn't work, try a conversion-function-id. */
5923 if (!cp_parser_parse_definitely (parser))
5924 id = cp_parser_conversion_function_id (parser);
5925 else if (UDLIT_OPER_P (id))
5927 /* 17.6.3.3.5 */
5928 const char *name = UDLIT_OP_SUFFIX (id);
5929 if (name[0] != '_' && !in_system_header_at (input_location)
5930 && declarator_p)
5931 warning (OPT_Wliteral_suffix,
5932 "literal operator suffixes not preceded by %<_%>"
5933 " are reserved for future standardization");
5936 return id;
5938 /* Fall through. */
5940 default:
5941 if (optional_p)
5942 return NULL_TREE;
5943 cp_parser_error (parser, "expected unqualified-id");
5944 return error_mark_node;
5948 /* Parse an (optional) nested-name-specifier.
5950 nested-name-specifier: [C++98]
5951 class-or-namespace-name :: nested-name-specifier [opt]
5952 class-or-namespace-name :: template nested-name-specifier [opt]
5954 nested-name-specifier: [C++0x]
5955 type-name ::
5956 namespace-name ::
5957 nested-name-specifier identifier ::
5958 nested-name-specifier template [opt] simple-template-id ::
5960 PARSER->SCOPE should be set appropriately before this function is
5961 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5962 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5963 in name lookups.
5965 Sets PARSER->SCOPE to the class (TYPE) or namespace
5966 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5967 it unchanged if there is no nested-name-specifier. Returns the new
5968 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5970 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5971 part of a declaration and/or decl-specifier. */
5973 static tree
5974 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5975 bool typename_keyword_p,
5976 bool check_dependency_p,
5977 bool type_p,
5978 bool is_declaration,
5979 bool template_keyword_p /* = false */)
5981 bool success = false;
5982 cp_token_position start = 0;
5983 cp_token *token;
5985 /* Remember where the nested-name-specifier starts. */
5986 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5988 start = cp_lexer_token_position (parser->lexer, false);
5989 push_deferring_access_checks (dk_deferred);
5992 while (true)
5994 tree new_scope;
5995 tree old_scope;
5996 tree saved_qualifying_scope;
5998 /* Spot cases that cannot be the beginning of a
5999 nested-name-specifier. */
6000 token = cp_lexer_peek_token (parser->lexer);
6002 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6003 the already parsed nested-name-specifier. */
6004 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6006 /* Grab the nested-name-specifier and continue the loop. */
6007 cp_parser_pre_parsed_nested_name_specifier (parser);
6008 /* If we originally encountered this nested-name-specifier
6009 with IS_DECLARATION set to false, we will not have
6010 resolved TYPENAME_TYPEs, so we must do so here. */
6011 if (is_declaration
6012 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6014 new_scope = resolve_typename_type (parser->scope,
6015 /*only_current_p=*/false);
6016 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6017 parser->scope = new_scope;
6019 success = true;
6020 continue;
6023 /* Spot cases that cannot be the beginning of a
6024 nested-name-specifier. On the second and subsequent times
6025 through the loop, we look for the `template' keyword. */
6026 if (success && token->keyword == RID_TEMPLATE)
6028 /* A template-id can start a nested-name-specifier. */
6029 else if (token->type == CPP_TEMPLATE_ID)
6031 /* DR 743: decltype can be used in a nested-name-specifier. */
6032 else if (token_is_decltype (token))
6034 else
6036 /* If the next token is not an identifier, then it is
6037 definitely not a type-name or namespace-name. */
6038 if (token->type != CPP_NAME)
6039 break;
6040 /* If the following token is neither a `<' (to begin a
6041 template-id), nor a `::', then we are not looking at a
6042 nested-name-specifier. */
6043 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6045 if (token->type == CPP_COLON
6046 && parser->colon_corrects_to_scope_p
6047 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6049 gcc_rich_location richloc (token->location);
6050 richloc.add_fixit_replace ("::");
6051 error_at_rich_loc (&richloc,
6052 "found %<:%> in nested-name-specifier, "
6053 "expected %<::%>");
6054 token->type = CPP_SCOPE;
6057 if (token->type != CPP_SCOPE
6058 && !cp_parser_nth_token_starts_template_argument_list_p
6059 (parser, 2))
6060 break;
6063 /* The nested-name-specifier is optional, so we parse
6064 tentatively. */
6065 cp_parser_parse_tentatively (parser);
6067 /* Look for the optional `template' keyword, if this isn't the
6068 first time through the loop. */
6069 if (success)
6070 template_keyword_p = cp_parser_optional_template_keyword (parser);
6072 /* Save the old scope since the name lookup we are about to do
6073 might destroy it. */
6074 old_scope = parser->scope;
6075 saved_qualifying_scope = parser->qualifying_scope;
6076 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6077 look up names in "X<T>::I" in order to determine that "Y" is
6078 a template. So, if we have a typename at this point, we make
6079 an effort to look through it. */
6080 if (is_declaration
6081 && !typename_keyword_p
6082 && parser->scope
6083 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6084 parser->scope = resolve_typename_type (parser->scope,
6085 /*only_current_p=*/false);
6086 /* Parse the qualifying entity. */
6087 new_scope
6088 = cp_parser_qualifying_entity (parser,
6089 typename_keyword_p,
6090 template_keyword_p,
6091 check_dependency_p,
6092 type_p,
6093 is_declaration);
6094 /* Look for the `::' token. */
6095 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6097 /* If we found what we wanted, we keep going; otherwise, we're
6098 done. */
6099 if (!cp_parser_parse_definitely (parser))
6101 bool error_p = false;
6103 /* Restore the OLD_SCOPE since it was valid before the
6104 failed attempt at finding the last
6105 class-or-namespace-name. */
6106 parser->scope = old_scope;
6107 parser->qualifying_scope = saved_qualifying_scope;
6109 /* If the next token is a decltype, and the one after that is a
6110 `::', then the decltype has failed to resolve to a class or
6111 enumeration type. Give this error even when parsing
6112 tentatively since it can't possibly be valid--and we're going
6113 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6114 won't get another chance.*/
6115 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6116 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6117 == CPP_SCOPE))
6119 token = cp_lexer_consume_token (parser->lexer);
6120 error_at (token->location, "decltype evaluates to %qT, "
6121 "which is not a class or enumeration type",
6122 token->u.tree_check_value->value);
6123 parser->scope = error_mark_node;
6124 error_p = true;
6125 /* As below. */
6126 success = true;
6127 cp_lexer_consume_token (parser->lexer);
6130 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6131 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6133 /* If we have a non-type template-id followed by ::, it can't
6134 possibly be valid. */
6135 token = cp_lexer_peek_token (parser->lexer);
6136 tree tid = token->u.tree_check_value->value;
6137 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6138 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6140 tree tmpl = NULL_TREE;
6141 if (is_overloaded_fn (tid))
6143 tree fns = get_fns (tid);
6144 if (OVL_SINGLE_P (fns))
6145 tmpl = OVL_FIRST (fns);
6146 error_at (token->location, "function template-id %qD "
6147 "in nested-name-specifier", tid);
6149 else
6151 /* Variable template. */
6152 tmpl = TREE_OPERAND (tid, 0);
6153 gcc_assert (variable_template_p (tmpl));
6154 error_at (token->location, "variable template-id %qD "
6155 "in nested-name-specifier", tid);
6157 if (tmpl)
6158 inform (DECL_SOURCE_LOCATION (tmpl),
6159 "%qD declared here", tmpl);
6161 parser->scope = error_mark_node;
6162 error_p = true;
6163 /* As below. */
6164 success = true;
6165 cp_lexer_consume_token (parser->lexer);
6166 cp_lexer_consume_token (parser->lexer);
6170 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6171 break;
6172 /* If the next token is an identifier, and the one after
6173 that is a `::', then any valid interpretation would have
6174 found a class-or-namespace-name. */
6175 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6176 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6177 == CPP_SCOPE)
6178 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6179 != CPP_COMPL))
6181 token = cp_lexer_consume_token (parser->lexer);
6182 if (!error_p)
6184 if (!token->error_reported)
6186 tree decl;
6187 tree ambiguous_decls;
6189 decl = cp_parser_lookup_name (parser, token->u.value,
6190 none_type,
6191 /*is_template=*/false,
6192 /*is_namespace=*/false,
6193 /*check_dependency=*/true,
6194 &ambiguous_decls,
6195 token->location);
6196 if (TREE_CODE (decl) == TEMPLATE_DECL)
6197 error_at (token->location,
6198 "%qD used without template parameters",
6199 decl);
6200 else if (ambiguous_decls)
6202 // cp_parser_lookup_name has the same diagnostic,
6203 // thus make sure to emit it at most once.
6204 if (cp_parser_uncommitted_to_tentative_parse_p
6205 (parser))
6207 error_at (token->location,
6208 "reference to %qD is ambiguous",
6209 token->u.value);
6210 print_candidates (ambiguous_decls);
6212 decl = error_mark_node;
6214 else
6216 if (cxx_dialect != cxx98)
6217 cp_parser_name_lookup_error
6218 (parser, token->u.value, decl, NLE_NOT_CXX98,
6219 token->location);
6220 else
6221 cp_parser_name_lookup_error
6222 (parser, token->u.value, decl, NLE_CXX98,
6223 token->location);
6226 parser->scope = error_mark_node;
6227 error_p = true;
6228 /* Treat this as a successful nested-name-specifier
6229 due to:
6231 [basic.lookup.qual]
6233 If the name found is not a class-name (clause
6234 _class_) or namespace-name (_namespace.def_), the
6235 program is ill-formed. */
6236 success = true;
6238 cp_lexer_consume_token (parser->lexer);
6240 break;
6242 /* We've found one valid nested-name-specifier. */
6243 success = true;
6244 /* Name lookup always gives us a DECL. */
6245 if (TREE_CODE (new_scope) == TYPE_DECL)
6246 new_scope = TREE_TYPE (new_scope);
6247 /* Uses of "template" must be followed by actual templates. */
6248 if (template_keyword_p
6249 && !(CLASS_TYPE_P (new_scope)
6250 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6251 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6252 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6253 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6254 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6255 == TEMPLATE_ID_EXPR)))
6256 permerror (input_location, TYPE_P (new_scope)
6257 ? G_("%qT is not a template")
6258 : G_("%qD is not a template"),
6259 new_scope);
6260 /* If it is a class scope, try to complete it; we are about to
6261 be looking up names inside the class. */
6262 if (TYPE_P (new_scope)
6263 /* Since checking types for dependency can be expensive,
6264 avoid doing it if the type is already complete. */
6265 && !COMPLETE_TYPE_P (new_scope)
6266 /* Do not try to complete dependent types. */
6267 && !dependent_type_p (new_scope))
6269 new_scope = complete_type (new_scope);
6270 /* If it is a typedef to current class, use the current
6271 class instead, as the typedef won't have any names inside
6272 it yet. */
6273 if (!COMPLETE_TYPE_P (new_scope)
6274 && currently_open_class (new_scope))
6275 new_scope = TYPE_MAIN_VARIANT (new_scope);
6277 /* Make sure we look in the right scope the next time through
6278 the loop. */
6279 parser->scope = new_scope;
6282 /* If parsing tentatively, replace the sequence of tokens that makes
6283 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6284 token. That way, should we re-parse the token stream, we will
6285 not have to repeat the effort required to do the parse, nor will
6286 we issue duplicate error messages. */
6287 if (success && start)
6289 cp_token *token;
6291 token = cp_lexer_token_at (parser->lexer, start);
6292 /* Reset the contents of the START token. */
6293 token->type = CPP_NESTED_NAME_SPECIFIER;
6294 /* Retrieve any deferred checks. Do not pop this access checks yet
6295 so the memory will not be reclaimed during token replacing below. */
6296 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6297 token->u.tree_check_value->value = parser->scope;
6298 token->u.tree_check_value->checks = get_deferred_access_checks ();
6299 token->u.tree_check_value->qualifying_scope =
6300 parser->qualifying_scope;
6301 token->keyword = RID_MAX;
6303 /* Purge all subsequent tokens. */
6304 cp_lexer_purge_tokens_after (parser->lexer, start);
6307 if (start)
6308 pop_to_parent_deferring_access_checks ();
6310 return success ? parser->scope : NULL_TREE;
6313 /* Parse a nested-name-specifier. See
6314 cp_parser_nested_name_specifier_opt for details. This function
6315 behaves identically, except that it will an issue an error if no
6316 nested-name-specifier is present. */
6318 static tree
6319 cp_parser_nested_name_specifier (cp_parser *parser,
6320 bool typename_keyword_p,
6321 bool check_dependency_p,
6322 bool type_p,
6323 bool is_declaration)
6325 tree scope;
6327 /* Look for the nested-name-specifier. */
6328 scope = cp_parser_nested_name_specifier_opt (parser,
6329 typename_keyword_p,
6330 check_dependency_p,
6331 type_p,
6332 is_declaration);
6333 /* If it was not present, issue an error message. */
6334 if (!scope)
6336 cp_parser_error (parser, "expected nested-name-specifier");
6337 parser->scope = NULL_TREE;
6340 return scope;
6343 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6344 this is either a class-name or a namespace-name (which corresponds
6345 to the class-or-namespace-name production in the grammar). For
6346 C++0x, it can also be a type-name that refers to an enumeration
6347 type or a simple-template-id.
6349 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6350 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6351 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6352 TYPE_P is TRUE iff the next name should be taken as a class-name,
6353 even the same name is declared to be another entity in the same
6354 scope.
6356 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6357 specified by the class-or-namespace-name. If neither is found the
6358 ERROR_MARK_NODE is returned. */
6360 static tree
6361 cp_parser_qualifying_entity (cp_parser *parser,
6362 bool typename_keyword_p,
6363 bool template_keyword_p,
6364 bool check_dependency_p,
6365 bool type_p,
6366 bool is_declaration)
6368 tree saved_scope;
6369 tree saved_qualifying_scope;
6370 tree saved_object_scope;
6371 tree scope;
6372 bool only_class_p;
6373 bool successful_parse_p;
6375 /* DR 743: decltype can appear in a nested-name-specifier. */
6376 if (cp_lexer_next_token_is_decltype (parser->lexer))
6378 scope = cp_parser_decltype (parser);
6379 if (TREE_CODE (scope) != ENUMERAL_TYPE
6380 && !MAYBE_CLASS_TYPE_P (scope))
6382 cp_parser_simulate_error (parser);
6383 return error_mark_node;
6385 if (TYPE_NAME (scope))
6386 scope = TYPE_NAME (scope);
6387 return scope;
6390 /* Before we try to parse the class-name, we must save away the
6391 current PARSER->SCOPE since cp_parser_class_name will destroy
6392 it. */
6393 saved_scope = parser->scope;
6394 saved_qualifying_scope = parser->qualifying_scope;
6395 saved_object_scope = parser->object_scope;
6396 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6397 there is no need to look for a namespace-name. */
6398 only_class_p = template_keyword_p
6399 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6400 if (!only_class_p)
6401 cp_parser_parse_tentatively (parser);
6402 scope = cp_parser_class_name (parser,
6403 typename_keyword_p,
6404 template_keyword_p,
6405 type_p ? class_type : none_type,
6406 check_dependency_p,
6407 /*class_head_p=*/false,
6408 is_declaration,
6409 /*enum_ok=*/cxx_dialect > cxx98);
6410 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6411 /* If that didn't work, try for a namespace-name. */
6412 if (!only_class_p && !successful_parse_p)
6414 /* Restore the saved scope. */
6415 parser->scope = saved_scope;
6416 parser->qualifying_scope = saved_qualifying_scope;
6417 parser->object_scope = saved_object_scope;
6418 /* If we are not looking at an identifier followed by the scope
6419 resolution operator, then this is not part of a
6420 nested-name-specifier. (Note that this function is only used
6421 to parse the components of a nested-name-specifier.) */
6422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6423 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6424 return error_mark_node;
6425 scope = cp_parser_namespace_name (parser);
6428 return scope;
6431 /* Return true if we are looking at a compound-literal, false otherwise. */
6433 static bool
6434 cp_parser_compound_literal_p (cp_parser *parser)
6436 cp_lexer_save_tokens (parser->lexer);
6438 /* Skip tokens until the next token is a closing parenthesis.
6439 If we find the closing `)', and the next token is a `{', then
6440 we are looking at a compound-literal. */
6441 bool compound_literal_p
6442 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6443 /*consume_paren=*/true)
6444 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6446 /* Roll back the tokens we skipped. */
6447 cp_lexer_rollback_tokens (parser->lexer);
6449 return compound_literal_p;
6452 /* Parse a postfix-expression.
6454 postfix-expression:
6455 primary-expression
6456 postfix-expression [ expression ]
6457 postfix-expression ( expression-list [opt] )
6458 simple-type-specifier ( expression-list [opt] )
6459 typename :: [opt] nested-name-specifier identifier
6460 ( expression-list [opt] )
6461 typename :: [opt] nested-name-specifier template [opt] template-id
6462 ( expression-list [opt] )
6463 postfix-expression . template [opt] id-expression
6464 postfix-expression -> template [opt] id-expression
6465 postfix-expression . pseudo-destructor-name
6466 postfix-expression -> pseudo-destructor-name
6467 postfix-expression ++
6468 postfix-expression --
6469 dynamic_cast < type-id > ( expression )
6470 static_cast < type-id > ( expression )
6471 reinterpret_cast < type-id > ( expression )
6472 const_cast < type-id > ( expression )
6473 typeid ( expression )
6474 typeid ( type-id )
6476 GNU Extension:
6478 postfix-expression:
6479 ( type-id ) { initializer-list , [opt] }
6481 This extension is a GNU version of the C99 compound-literal
6482 construct. (The C99 grammar uses `type-name' instead of `type-id',
6483 but they are essentially the same concept.)
6485 If ADDRESS_P is true, the postfix expression is the operand of the
6486 `&' operator. CAST_P is true if this expression is the target of a
6487 cast.
6489 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6490 class member access expressions [expr.ref].
6492 Returns a representation of the expression. */
6494 static cp_expr
6495 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6496 bool member_access_only_p, bool decltype_p,
6497 cp_id_kind * pidk_return)
6499 cp_token *token;
6500 location_t loc;
6501 enum rid keyword;
6502 cp_id_kind idk = CP_ID_KIND_NONE;
6503 cp_expr postfix_expression = NULL_TREE;
6504 bool is_member_access = false;
6505 int saved_in_statement = -1;
6507 /* Peek at the next token. */
6508 token = cp_lexer_peek_token (parser->lexer);
6509 loc = token->location;
6510 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6512 /* Some of the productions are determined by keywords. */
6513 keyword = token->keyword;
6514 switch (keyword)
6516 case RID_DYNCAST:
6517 case RID_STATCAST:
6518 case RID_REINTCAST:
6519 case RID_CONSTCAST:
6521 tree type;
6522 cp_expr expression;
6523 const char *saved_message;
6524 bool saved_in_type_id_in_expr_p;
6526 /* All of these can be handled in the same way from the point
6527 of view of parsing. Begin by consuming the token
6528 identifying the cast. */
6529 cp_lexer_consume_token (parser->lexer);
6531 /* New types cannot be defined in the cast. */
6532 saved_message = parser->type_definition_forbidden_message;
6533 parser->type_definition_forbidden_message
6534 = G_("types may not be defined in casts");
6536 /* Look for the opening `<'. */
6537 cp_parser_require (parser, CPP_LESS, RT_LESS);
6538 /* Parse the type to which we are casting. */
6539 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6540 parser->in_type_id_in_expr_p = true;
6541 type = cp_parser_type_id (parser);
6542 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6543 /* Look for the closing `>'. */
6544 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6545 /* Restore the old message. */
6546 parser->type_definition_forbidden_message = saved_message;
6548 bool saved_greater_than_is_operator_p
6549 = parser->greater_than_is_operator_p;
6550 parser->greater_than_is_operator_p = true;
6552 /* And the expression which is being cast. */
6553 matching_parens parens;
6554 parens.require_open (parser);
6555 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6556 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6557 RT_CLOSE_PAREN);
6558 location_t end_loc = close_paren ?
6559 close_paren->location : UNKNOWN_LOCATION;
6561 parser->greater_than_is_operator_p
6562 = saved_greater_than_is_operator_p;
6564 /* Only type conversions to integral or enumeration types
6565 can be used in constant-expressions. */
6566 if (!cast_valid_in_integral_constant_expression_p (type)
6567 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6569 postfix_expression = error_mark_node;
6570 break;
6573 switch (keyword)
6575 case RID_DYNCAST:
6576 postfix_expression
6577 = build_dynamic_cast (type, expression, tf_warning_or_error);
6578 break;
6579 case RID_STATCAST:
6580 postfix_expression
6581 = build_static_cast (type, expression, tf_warning_or_error);
6582 break;
6583 case RID_REINTCAST:
6584 postfix_expression
6585 = build_reinterpret_cast (type, expression,
6586 tf_warning_or_error);
6587 break;
6588 case RID_CONSTCAST:
6589 postfix_expression
6590 = build_const_cast (type, expression, tf_warning_or_error);
6591 break;
6592 default:
6593 gcc_unreachable ();
6596 /* Construct a location e.g. :
6597 reinterpret_cast <int *> (expr)
6598 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6599 ranging from the start of the "*_cast" token to the final closing
6600 paren, with the caret at the start. */
6601 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6602 postfix_expression.set_location (cp_cast_loc);
6604 break;
6606 case RID_TYPEID:
6608 tree type;
6609 const char *saved_message;
6610 bool saved_in_type_id_in_expr_p;
6612 /* Consume the `typeid' token. */
6613 cp_lexer_consume_token (parser->lexer);
6614 /* Look for the `(' token. */
6615 matching_parens parens;
6616 parens.require_open (parser);
6617 /* Types cannot be defined in a `typeid' expression. */
6618 saved_message = parser->type_definition_forbidden_message;
6619 parser->type_definition_forbidden_message
6620 = G_("types may not be defined in a %<typeid%> expression");
6621 /* We can't be sure yet whether we're looking at a type-id or an
6622 expression. */
6623 cp_parser_parse_tentatively (parser);
6624 /* Try a type-id first. */
6625 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6626 parser->in_type_id_in_expr_p = true;
6627 type = cp_parser_type_id (parser);
6628 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6629 /* Look for the `)' token. Otherwise, we can't be sure that
6630 we're not looking at an expression: consider `typeid (int
6631 (3))', for example. */
6632 cp_token *close_paren = parens.require_close (parser);
6633 /* If all went well, simply lookup the type-id. */
6634 if (cp_parser_parse_definitely (parser))
6635 postfix_expression = get_typeid (type, tf_warning_or_error);
6636 /* Otherwise, fall back to the expression variant. */
6637 else
6639 tree expression;
6641 /* Look for an expression. */
6642 expression = cp_parser_expression (parser, & idk);
6643 /* Compute its typeid. */
6644 postfix_expression = build_typeid (expression, tf_warning_or_error);
6645 /* Look for the `)' token. */
6646 close_paren = parens.require_close (parser);
6648 /* Restore the saved message. */
6649 parser->type_definition_forbidden_message = saved_message;
6650 /* `typeid' may not appear in an integral constant expression. */
6651 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6652 postfix_expression = error_mark_node;
6654 /* Construct a location e.g. :
6655 typeid (expr)
6656 ^~~~~~~~~~~~~
6657 ranging from the start of the "typeid" token to the final closing
6658 paren, with the caret at the start. */
6659 if (close_paren)
6661 location_t typeid_loc
6662 = make_location (start_loc, start_loc, close_paren->location);
6663 postfix_expression.set_location (typeid_loc);
6666 break;
6668 case RID_TYPENAME:
6670 tree type;
6671 /* The syntax permitted here is the same permitted for an
6672 elaborated-type-specifier. */
6673 ++parser->prevent_constrained_type_specifiers;
6674 type = cp_parser_elaborated_type_specifier (parser,
6675 /*is_friend=*/false,
6676 /*is_declaration=*/false);
6677 --parser->prevent_constrained_type_specifiers;
6678 postfix_expression = cp_parser_functional_cast (parser, type);
6680 break;
6682 case RID_CILK_SPAWN:
6684 location_t cilk_spawn_loc
6685 = cp_lexer_peek_token (parser->lexer)->location;
6686 cp_lexer_consume_token (parser->lexer);
6687 token = cp_lexer_peek_token (parser->lexer);
6688 if (token->type == CPP_SEMICOLON)
6690 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6691 "an expression");
6692 postfix_expression = error_mark_node;
6693 break;
6695 else if (!current_function_decl)
6697 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6698 "inside a function");
6699 postfix_expression = error_mark_node;
6700 break;
6702 else
6704 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6705 saved_in_statement = parser->in_statement;
6706 parser->in_statement |= IN_CILK_SPAWN;
6708 cfun->calls_cilk_spawn = 1;
6709 postfix_expression =
6710 cp_parser_postfix_expression (parser, false, false,
6711 false, false, &idk);
6712 if (!flag_cilkplus)
6714 error_at (token->location, "-fcilkplus must be enabled to use"
6715 " %<_Cilk_spawn%>");
6716 cfun->calls_cilk_spawn = 0;
6718 else if (saved_in_statement & IN_CILK_SPAWN)
6720 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6721 "are not permitted");
6722 postfix_expression = error_mark_node;
6723 cfun->calls_cilk_spawn = 0;
6725 else
6727 location_t loc = postfix_expression.get_location ();
6728 postfix_expression = build_cilk_spawn (token->location,
6729 postfix_expression);
6730 /* Build a location of the form:
6731 _Cilk_spawn expr
6732 ~~~~~~~~~~~~^~~~
6733 with caret at the expr, ranging from the start of the
6734 _Cilk_spawn token to the end of the expression. */
6735 location_t combined_loc =
6736 make_location (loc, cilk_spawn_loc, get_finish (loc));
6737 postfix_expression.set_location (combined_loc);
6738 if (postfix_expression != error_mark_node)
6739 SET_EXPR_LOCATION (postfix_expression, input_location);
6740 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6742 break;
6745 case RID_ADDRESSOF:
6746 case RID_BUILTIN_SHUFFLE:
6747 case RID_BUILTIN_LAUNDER:
6749 vec<tree, va_gc> *vec;
6750 unsigned int i;
6751 tree p;
6753 cp_lexer_consume_token (parser->lexer);
6754 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6755 /*cast_p=*/false, /*allow_expansion_p=*/true,
6756 /*non_constant_p=*/NULL);
6757 if (vec == NULL)
6759 postfix_expression = error_mark_node;
6760 break;
6763 FOR_EACH_VEC_ELT (*vec, i, p)
6764 mark_exp_read (p);
6766 switch (keyword)
6768 case RID_ADDRESSOF:
6769 if (vec->length () == 1)
6770 postfix_expression
6771 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6772 else
6774 error_at (loc, "wrong number of arguments to "
6775 "%<__builtin_addressof%>");
6776 postfix_expression = error_mark_node;
6778 break;
6780 case RID_BUILTIN_LAUNDER:
6781 if (vec->length () == 1)
6782 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6783 tf_warning_or_error);
6784 else
6786 error_at (loc, "wrong number of arguments to "
6787 "%<__builtin_launder%>");
6788 postfix_expression = error_mark_node;
6790 break;
6792 case RID_BUILTIN_SHUFFLE:
6793 if (vec->length () == 2)
6794 postfix_expression
6795 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6796 (*vec)[1], tf_warning_or_error);
6797 else if (vec->length () == 3)
6798 postfix_expression
6799 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6800 (*vec)[2], tf_warning_or_error);
6801 else
6803 error_at (loc, "wrong number of arguments to "
6804 "%<__builtin_shuffle%>");
6805 postfix_expression = error_mark_node;
6807 break;
6809 default:
6810 gcc_unreachable ();
6812 break;
6815 default:
6817 tree type;
6819 /* If the next thing is a simple-type-specifier, we may be
6820 looking at a functional cast. We could also be looking at
6821 an id-expression. So, we try the functional cast, and if
6822 that doesn't work we fall back to the primary-expression. */
6823 cp_parser_parse_tentatively (parser);
6824 /* Look for the simple-type-specifier. */
6825 ++parser->prevent_constrained_type_specifiers;
6826 type = cp_parser_simple_type_specifier (parser,
6827 /*decl_specs=*/NULL,
6828 CP_PARSER_FLAGS_NONE);
6829 --parser->prevent_constrained_type_specifiers;
6830 /* Parse the cast itself. */
6831 if (!cp_parser_error_occurred (parser))
6832 postfix_expression
6833 = cp_parser_functional_cast (parser, type);
6834 /* If that worked, we're done. */
6835 if (cp_parser_parse_definitely (parser))
6836 break;
6838 /* If the functional-cast didn't work out, try a
6839 compound-literal. */
6840 if (cp_parser_allow_gnu_extensions_p (parser)
6841 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6843 cp_expr initializer = NULL_TREE;
6845 cp_parser_parse_tentatively (parser);
6847 matching_parens parens;
6848 parens.consume_open (parser);
6850 /* Avoid calling cp_parser_type_id pointlessly, see comment
6851 in cp_parser_cast_expression about c++/29234. */
6852 if (!cp_parser_compound_literal_p (parser))
6853 cp_parser_simulate_error (parser);
6854 else
6856 /* Parse the type. */
6857 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6858 parser->in_type_id_in_expr_p = true;
6859 type = cp_parser_type_id (parser);
6860 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6861 parens.require_close (parser);
6864 /* If things aren't going well, there's no need to
6865 keep going. */
6866 if (!cp_parser_error_occurred (parser))
6868 bool non_constant_p;
6869 /* Parse the brace-enclosed initializer list. */
6870 initializer = cp_parser_braced_list (parser,
6871 &non_constant_p);
6873 /* If that worked, we're definitely looking at a
6874 compound-literal expression. */
6875 if (cp_parser_parse_definitely (parser))
6877 /* Warn the user that a compound literal is not
6878 allowed in standard C++. */
6879 pedwarn (input_location, OPT_Wpedantic,
6880 "ISO C++ forbids compound-literals");
6881 /* For simplicity, we disallow compound literals in
6882 constant-expressions. We could
6883 allow compound literals of integer type, whose
6884 initializer was a constant, in constant
6885 expressions. Permitting that usage, as a further
6886 extension, would not change the meaning of any
6887 currently accepted programs. (Of course, as
6888 compound literals are not part of ISO C++, the
6889 standard has nothing to say.) */
6890 if (cp_parser_non_integral_constant_expression (parser,
6891 NIC_NCC))
6893 postfix_expression = error_mark_node;
6894 break;
6896 /* Form the representation of the compound-literal. */
6897 postfix_expression
6898 = finish_compound_literal (type, initializer,
6899 tf_warning_or_error, fcl_c99);
6900 postfix_expression.set_location (initializer.get_location ());
6901 break;
6905 /* It must be a primary-expression. */
6906 postfix_expression
6907 = cp_parser_primary_expression (parser, address_p, cast_p,
6908 /*template_arg_p=*/false,
6909 decltype_p,
6910 &idk);
6912 break;
6915 /* Note that we don't need to worry about calling build_cplus_new on a
6916 class-valued CALL_EXPR in decltype when it isn't the end of the
6917 postfix-expression; unary_complex_lvalue will take care of that for
6918 all these cases. */
6920 /* Keep looping until the postfix-expression is complete. */
6921 while (true)
6923 if (idk == CP_ID_KIND_UNQUALIFIED
6924 && identifier_p (postfix_expression)
6925 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6926 /* It is not a Koenig lookup function call. */
6927 postfix_expression
6928 = unqualified_name_lookup_error (postfix_expression);
6930 /* Peek at the next token. */
6931 token = cp_lexer_peek_token (parser->lexer);
6933 switch (token->type)
6935 case CPP_OPEN_SQUARE:
6936 if (cp_next_tokens_can_be_std_attribute_p (parser))
6938 cp_parser_error (parser,
6939 "two consecutive %<[%> shall "
6940 "only introduce an attribute");
6941 return error_mark_node;
6943 postfix_expression
6944 = cp_parser_postfix_open_square_expression (parser,
6945 postfix_expression,
6946 false,
6947 decltype_p);
6948 postfix_expression.set_range (start_loc,
6949 postfix_expression.get_location ());
6951 idk = CP_ID_KIND_NONE;
6952 is_member_access = false;
6953 break;
6955 case CPP_OPEN_PAREN:
6956 /* postfix-expression ( expression-list [opt] ) */
6958 bool koenig_p;
6959 bool is_builtin_constant_p;
6960 bool saved_integral_constant_expression_p = false;
6961 bool saved_non_integral_constant_expression_p = false;
6962 tsubst_flags_t complain = complain_flags (decltype_p);
6963 vec<tree, va_gc> *args;
6964 location_t close_paren_loc = UNKNOWN_LOCATION;
6966 is_member_access = false;
6968 is_builtin_constant_p
6969 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6970 if (is_builtin_constant_p)
6972 /* The whole point of __builtin_constant_p is to allow
6973 non-constant expressions to appear as arguments. */
6974 saved_integral_constant_expression_p
6975 = parser->integral_constant_expression_p;
6976 saved_non_integral_constant_expression_p
6977 = parser->non_integral_constant_expression_p;
6978 parser->integral_constant_expression_p = false;
6980 args = (cp_parser_parenthesized_expression_list
6981 (parser, non_attr,
6982 /*cast_p=*/false, /*allow_expansion_p=*/true,
6983 /*non_constant_p=*/NULL,
6984 /*close_paren_loc=*/&close_paren_loc));
6985 if (is_builtin_constant_p)
6987 parser->integral_constant_expression_p
6988 = saved_integral_constant_expression_p;
6989 parser->non_integral_constant_expression_p
6990 = saved_non_integral_constant_expression_p;
6993 if (args == NULL)
6995 postfix_expression = error_mark_node;
6996 break;
6999 /* Function calls are not permitted in
7000 constant-expressions. */
7001 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7002 && cp_parser_non_integral_constant_expression (parser,
7003 NIC_FUNC_CALL))
7005 postfix_expression = error_mark_node;
7006 release_tree_vector (args);
7007 break;
7010 koenig_p = false;
7011 if (idk == CP_ID_KIND_UNQUALIFIED
7012 || idk == CP_ID_KIND_TEMPLATE_ID)
7014 if (identifier_p (postfix_expression))
7016 if (!args->is_empty ())
7018 koenig_p = true;
7019 if (!any_type_dependent_arguments_p (args))
7020 postfix_expression
7021 = perform_koenig_lookup (postfix_expression, args,
7022 complain);
7024 else
7025 postfix_expression
7026 = unqualified_fn_lookup_error (postfix_expression);
7028 /* We do not perform argument-dependent lookup if
7029 normal lookup finds a non-function, in accordance
7030 with the expected resolution of DR 218. */
7031 else if (!args->is_empty ()
7032 && is_overloaded_fn (postfix_expression))
7034 tree fn = get_first_fn (postfix_expression);
7035 fn = STRIP_TEMPLATE (fn);
7037 /* Do not do argument dependent lookup if regular
7038 lookup finds a member function or a block-scope
7039 function declaration. [basic.lookup.argdep]/3 */
7040 if (!DECL_FUNCTION_MEMBER_P (fn)
7041 && !DECL_LOCAL_FUNCTION_P (fn))
7043 koenig_p = true;
7044 if (!any_type_dependent_arguments_p (args))
7045 postfix_expression
7046 = perform_koenig_lookup (postfix_expression, args,
7047 complain);
7052 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7053 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7054 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7055 && vec_safe_length (args) == 3)
7057 tree arg0 = (*args)[0];
7058 tree arg1 = (*args)[1];
7059 tree arg2 = (*args)[2];
7060 int literal_mask = ((!!integer_zerop (arg1) << 1)
7061 | (!!integer_zerop (arg2) << 2));
7062 if (TREE_CODE (arg2) == CONST_DECL)
7063 arg2 = DECL_INITIAL (arg2);
7064 warn_for_memset (input_location, arg0, arg2, literal_mask);
7067 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7069 tree instance = TREE_OPERAND (postfix_expression, 0);
7070 tree fn = TREE_OPERAND (postfix_expression, 1);
7072 if (processing_template_decl
7073 && (type_dependent_object_expression_p (instance)
7074 || (!BASELINK_P (fn)
7075 && TREE_CODE (fn) != FIELD_DECL)
7076 || type_dependent_expression_p (fn)
7077 || any_type_dependent_arguments_p (args)))
7079 maybe_generic_this_capture (instance, fn);
7080 postfix_expression
7081 = build_min_nt_call_vec (postfix_expression, args);
7082 release_tree_vector (args);
7083 break;
7086 if (BASELINK_P (fn))
7088 postfix_expression
7089 = (build_new_method_call
7090 (instance, fn, &args, NULL_TREE,
7091 (idk == CP_ID_KIND_QUALIFIED
7092 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7093 : LOOKUP_NORMAL),
7094 /*fn_p=*/NULL,
7095 complain));
7097 else
7098 postfix_expression
7099 = finish_call_expr (postfix_expression, &args,
7100 /*disallow_virtual=*/false,
7101 /*koenig_p=*/false,
7102 complain);
7104 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7105 || TREE_CODE (postfix_expression) == MEMBER_REF
7106 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7107 postfix_expression = (build_offset_ref_call_from_tree
7108 (postfix_expression, &args,
7109 complain));
7110 else if (idk == CP_ID_KIND_QUALIFIED)
7111 /* A call to a static class member, or a namespace-scope
7112 function. */
7113 postfix_expression
7114 = finish_call_expr (postfix_expression, &args,
7115 /*disallow_virtual=*/true,
7116 koenig_p,
7117 complain);
7118 else
7119 /* All other function calls. */
7120 postfix_expression
7121 = finish_call_expr (postfix_expression, &args,
7122 /*disallow_virtual=*/false,
7123 koenig_p,
7124 complain);
7126 if (close_paren_loc != UNKNOWN_LOCATION)
7128 location_t combined_loc = make_location (token->location,
7129 start_loc,
7130 close_paren_loc);
7131 postfix_expression.set_location (combined_loc);
7134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7135 idk = CP_ID_KIND_NONE;
7137 release_tree_vector (args);
7139 break;
7141 case CPP_DOT:
7142 case CPP_DEREF:
7143 /* postfix-expression . template [opt] id-expression
7144 postfix-expression . pseudo-destructor-name
7145 postfix-expression -> template [opt] id-expression
7146 postfix-expression -> pseudo-destructor-name */
7148 /* Consume the `.' or `->' operator. */
7149 cp_lexer_consume_token (parser->lexer);
7151 postfix_expression
7152 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7153 postfix_expression,
7154 false, &idk, loc);
7156 is_member_access = true;
7157 break;
7159 case CPP_PLUS_PLUS:
7160 /* postfix-expression ++ */
7161 /* Consume the `++' token. */
7162 cp_lexer_consume_token (parser->lexer);
7163 /* Generate a representation for the complete expression. */
7164 postfix_expression
7165 = finish_increment_expr (postfix_expression,
7166 POSTINCREMENT_EXPR);
7167 /* Increments may not appear in constant-expressions. */
7168 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7169 postfix_expression = error_mark_node;
7170 idk = CP_ID_KIND_NONE;
7171 is_member_access = false;
7172 break;
7174 case CPP_MINUS_MINUS:
7175 /* postfix-expression -- */
7176 /* Consume the `--' token. */
7177 cp_lexer_consume_token (parser->lexer);
7178 /* Generate a representation for the complete expression. */
7179 postfix_expression
7180 = finish_increment_expr (postfix_expression,
7181 POSTDECREMENT_EXPR);
7182 /* Decrements may not appear in constant-expressions. */
7183 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7184 postfix_expression = error_mark_node;
7185 idk = CP_ID_KIND_NONE;
7186 is_member_access = false;
7187 break;
7189 default:
7190 if (pidk_return != NULL)
7191 * pidk_return = idk;
7192 if (member_access_only_p)
7193 return is_member_access
7194 ? postfix_expression
7195 : cp_expr (error_mark_node);
7196 else
7197 return postfix_expression;
7201 /* We should never get here. */
7202 gcc_unreachable ();
7203 return error_mark_node;
7206 /* This function parses Cilk Plus array notations. If a normal array expr. is
7207 parsed then the array index is passed back to the caller through *INIT_INDEX
7208 and the function returns a NULL_TREE. If array notation expr. is parsed,
7209 then *INIT_INDEX is ignored by the caller and the function returns
7210 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7211 error_mark_node. */
7213 static tree
7214 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7215 tree array_value)
7217 cp_token *token = NULL;
7218 tree length_index, stride = NULL_TREE, value_tree, array_type;
7219 if (!array_value || array_value == error_mark_node)
7221 cp_parser_skip_to_end_of_statement (parser);
7222 return error_mark_node;
7225 array_type = TREE_TYPE (array_value);
7227 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7228 parser->colon_corrects_to_scope_p = false;
7229 token = cp_lexer_peek_token (parser->lexer);
7231 if (!token)
7233 cp_parser_error (parser, "expected %<:%> or numeral");
7234 return error_mark_node;
7236 else if (token->type == CPP_COLON)
7238 /* Consume the ':'. */
7239 cp_lexer_consume_token (parser->lexer);
7241 /* If we are here, then we have a case like this A[:]. */
7242 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7244 cp_parser_error (parser, "expected %<]%>");
7245 cp_parser_skip_to_end_of_statement (parser);
7246 return error_mark_node;
7248 *init_index = NULL_TREE;
7249 stride = NULL_TREE;
7250 length_index = NULL_TREE;
7252 else
7254 /* If we are here, then there are three valid possibilities:
7255 1. ARRAY [ EXP ]
7256 2. ARRAY [ EXP : EXP ]
7257 3. ARRAY [ EXP : EXP : EXP ] */
7259 *init_index = cp_parser_expression (parser);
7260 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7262 /* This indicates that we have a normal array expression. */
7263 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7264 return NULL_TREE;
7267 /* Consume the ':'. */
7268 cp_lexer_consume_token (parser->lexer);
7269 length_index = cp_parser_expression (parser);
7270 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7272 cp_lexer_consume_token (parser->lexer);
7273 stride = cp_parser_expression (parser);
7276 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7278 if (*init_index == error_mark_node || length_index == error_mark_node
7279 || stride == error_mark_node || array_type == error_mark_node)
7281 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7282 cp_lexer_consume_token (parser->lexer);
7283 return error_mark_node;
7285 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7287 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7288 length_index, stride, array_type);
7289 return value_tree;
7292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7293 by cp_parser_builtin_offsetof. We're looking for
7295 postfix-expression [ expression ]
7296 postfix-expression [ braced-init-list ] (C++11)
7298 FOR_OFFSETOF is set if we're being called in that context, which
7299 changes how we deal with integer constant expressions. */
7301 static tree
7302 cp_parser_postfix_open_square_expression (cp_parser *parser,
7303 tree postfix_expression,
7304 bool for_offsetof,
7305 bool decltype_p)
7307 tree index = NULL_TREE;
7308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7309 bool saved_greater_than_is_operator_p;
7311 /* Consume the `[' token. */
7312 cp_lexer_consume_token (parser->lexer);
7314 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7315 parser->greater_than_is_operator_p = true;
7317 /* Parse the index expression. */
7318 /* ??? For offsetof, there is a question of what to allow here. If
7319 offsetof is not being used in an integral constant expression context,
7320 then we *could* get the right answer by computing the value at runtime.
7321 If we are in an integral constant expression context, then we might
7322 could accept any constant expression; hard to say without analysis.
7323 Rather than open the barn door too wide right away, allow only integer
7324 constant expressions here. */
7325 if (for_offsetof)
7326 index = cp_parser_constant_expression (parser);
7327 else
7329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7331 bool expr_nonconst_p;
7332 cp_lexer_set_source_position (parser->lexer);
7333 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7334 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7335 if (flag_cilkplus
7336 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7338 error_at (cp_lexer_peek_token (parser->lexer)->location,
7339 "braced list index is not allowed with array "
7340 "notation");
7341 cp_parser_skip_to_end_of_statement (parser);
7342 return error_mark_node;
7345 else if (flag_cilkplus)
7347 /* Here are have these two options:
7348 ARRAY[EXP : EXP] - Array notation expr with default
7349 stride of 1.
7350 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7351 stride. */
7352 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7353 postfix_expression);
7354 if (an_exp)
7355 return an_exp;
7357 else
7358 index = cp_parser_expression (parser);
7361 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression = grok_array_decl (loc, postfix_expression,
7368 index, decltype_p);
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7372 if (!for_offsetof
7373 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7374 postfix_expression = error_mark_node;
7376 return postfix_expression;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7392 static tree
7393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7394 enum cpp_ttype token_type,
7395 cp_expr postfix_expression,
7396 bool for_offsetof, cp_id_kind *idk,
7397 location_t location)
7399 tree name;
7400 bool dependent_p;
7401 bool pseudo_destructor_p;
7402 tree scope = NULL_TREE;
7403 location_t start_loc = postfix_expression.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type == CPP_DEREF)
7407 postfix_expression = build_x_arrow (location, postfix_expression,
7408 tf_warning_or_error);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p = type_dependent_object_expression_p (postfix_expression);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser->scope = NULL_TREE;
7414 parser->qualifying_scope = NULL_TREE;
7415 parser->object_scope = NULL_TREE;
7416 *idk = CP_ID_KIND_NONE;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7420 if (!dependent_p)
7422 scope = TREE_TYPE (postfix_expression);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope = non_reference (scope);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression != current_class_ref
7435 && scope != error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope, current_class_type))))
7441 scope = complete_type (scope);
7442 if (!COMPLETE_TYPE_P (scope)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind = (processing_template_decl
7449 && MAYBE_CLASS_TYPE_P (scope)
7450 ? DK_PEDWARN
7451 : DK_ERROR);
7452 cxx_incomplete_type_diagnostic
7453 (location_of (postfix_expression),
7454 postfix_expression, scope, kind);
7455 if (!MAYBE_CLASS_TYPE_P (scope))
7456 return error_mark_node;
7457 if (processing_template_decl)
7459 dependent_p = true;
7460 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7465 if (!dependent_p)
7467 /* Let the name lookup machinery know that we are processing a
7468 class member access expression. */
7469 parser->context->object_type = scope;
7470 /* If something went wrong, we want to be able to discern that case,
7471 as opposed to the case where there was no SCOPE due to the type
7472 of expression being dependent. */
7473 if (!scope)
7474 scope = error_mark_node;
7475 /* If the SCOPE was erroneous, make the various semantic analysis
7476 functions exit quickly -- and without issuing additional error
7477 messages. */
7478 if (scope == error_mark_node)
7479 postfix_expression = error_mark_node;
7483 if (dependent_p)
7484 /* Tell cp_parser_lookup_name that there was an object, even though it's
7485 type-dependent. */
7486 parser->context->object_type = unknown_type_node;
7488 /* Assume this expression is not a pseudo-destructor access. */
7489 pseudo_destructor_p = false;
7491 /* If the SCOPE is a scalar type, then, if this is a valid program,
7492 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7493 is type dependent, it can be pseudo-destructor-name or something else.
7494 Try to parse it as pseudo-destructor-name first. */
7495 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7497 tree s;
7498 tree type;
7500 cp_parser_parse_tentatively (parser);
7501 /* Parse the pseudo-destructor-name. */
7502 s = NULL_TREE;
7503 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7504 &s, &type);
7505 if (dependent_p
7506 && (cp_parser_error_occurred (parser)
7507 || !SCALAR_TYPE_P (type)))
7508 cp_parser_abort_tentative_parse (parser);
7509 else if (cp_parser_parse_definitely (parser))
7511 pseudo_destructor_p = true;
7512 postfix_expression
7513 = finish_pseudo_destructor_expr (postfix_expression,
7514 s, type, location);
7518 if (!pseudo_destructor_p)
7520 /* If the SCOPE is not a scalar type, we are looking at an
7521 ordinary class member access expression, rather than a
7522 pseudo-destructor-name. */
7523 bool template_p;
7524 cp_token *token = cp_lexer_peek_token (parser->lexer);
7525 /* Parse the id-expression. */
7526 name = (cp_parser_id_expression
7527 (parser,
7528 cp_parser_optional_template_keyword (parser),
7529 /*check_dependency_p=*/true,
7530 &template_p,
7531 /*declarator_p=*/false,
7532 /*optional_p=*/false));
7533 /* In general, build a SCOPE_REF if the member name is qualified.
7534 However, if the name was not dependent and has already been
7535 resolved; there is no need to build the SCOPE_REF. For example;
7537 struct X { void f(); };
7538 template <typename T> void f(T* t) { t->X::f(); }
7540 Even though "t" is dependent, "X::f" is not and has been resolved
7541 to a BASELINK; there is no need to include scope information. */
7543 /* But we do need to remember that there was an explicit scope for
7544 virtual function calls. */
7545 if (parser->scope)
7546 *idk = CP_ID_KIND_QUALIFIED;
7548 /* If the name is a template-id that names a type, we will get a
7549 TYPE_DECL here. That is invalid code. */
7550 if (TREE_CODE (name) == TYPE_DECL)
7552 error_at (token->location, "invalid use of %qD", name);
7553 postfix_expression = error_mark_node;
7555 else
7557 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7559 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7561 error_at (token->location, "%<%D::%D%> is not a class member",
7562 parser->scope, name);
7563 postfix_expression = error_mark_node;
7565 else
7566 name = build_qualified_name (/*type=*/NULL_TREE,
7567 parser->scope,
7568 name,
7569 template_p);
7570 parser->scope = NULL_TREE;
7571 parser->qualifying_scope = NULL_TREE;
7572 parser->object_scope = NULL_TREE;
7574 if (parser->scope && name && BASELINK_P (name))
7575 adjust_result_of_qualified_name_lookup
7576 (name, parser->scope, scope);
7577 postfix_expression
7578 = finish_class_member_access_expr (postfix_expression, name,
7579 template_p,
7580 tf_warning_or_error);
7581 /* Build a location e.g.:
7582 ptr->access_expr
7583 ~~~^~~~~~~~~~~~~
7584 where the caret is at the deref token, ranging from
7585 the start of postfix_expression to the end of the access expr. */
7586 location_t end_loc
7587 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7588 location_t combined_loc
7589 = make_location (input_location, start_loc, end_loc);
7590 protected_set_expr_location (postfix_expression, combined_loc);
7594 /* We no longer need to look up names in the scope of the object on
7595 the left-hand side of the `.' or `->' operator. */
7596 parser->context->object_type = NULL_TREE;
7598 /* Outside of offsetof, these operators may not appear in
7599 constant-expressions. */
7600 if (!for_offsetof
7601 && (cp_parser_non_integral_constant_expression
7602 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7603 postfix_expression = error_mark_node;
7605 return postfix_expression;
7608 /* Parse a parenthesized expression-list.
7610 expression-list:
7611 assignment-expression
7612 expression-list, assignment-expression
7614 attribute-list:
7615 expression-list
7616 identifier
7617 identifier, expression-list
7619 CAST_P is true if this expression is the target of a cast.
7621 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7622 argument pack.
7624 Returns a vector of trees. Each element is a representation of an
7625 assignment-expression. NULL is returned if the ( and or ) are
7626 missing. An empty, but allocated, vector is returned on no
7627 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7628 if we are parsing an attribute list for an attribute that wants a
7629 plain identifier argument, normal_attr for an attribute that wants
7630 an expression, or non_attr if we aren't parsing an attribute list. If
7631 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7632 not all of the expressions in the list were constant.
7633 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7634 will be written to with the location of the closing parenthesis. If
7635 an error occurs, it may or may not be written to. */
7637 static vec<tree, va_gc> *
7638 cp_parser_parenthesized_expression_list (cp_parser* parser,
7639 int is_attribute_list,
7640 bool cast_p,
7641 bool allow_expansion_p,
7642 bool *non_constant_p,
7643 location_t *close_paren_loc)
7645 vec<tree, va_gc> *expression_list;
7646 bool fold_expr_p = is_attribute_list != non_attr;
7647 tree identifier = NULL_TREE;
7648 bool saved_greater_than_is_operator_p;
7650 /* Assume all the expressions will be constant. */
7651 if (non_constant_p)
7652 *non_constant_p = false;
7654 matching_parens parens;
7655 if (!parens.require_open (parser))
7656 return NULL;
7658 expression_list = make_tree_vector ();
7660 /* Within a parenthesized expression, a `>' token is always
7661 the greater-than operator. */
7662 saved_greater_than_is_operator_p
7663 = parser->greater_than_is_operator_p;
7664 parser->greater_than_is_operator_p = true;
7666 /* Consume expressions until there are no more. */
7667 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7668 while (true)
7670 tree expr;
7672 /* At the beginning of attribute lists, check to see if the
7673 next token is an identifier. */
7674 if (is_attribute_list == id_attr
7675 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7677 cp_token *token;
7679 /* Consume the identifier. */
7680 token = cp_lexer_consume_token (parser->lexer);
7681 /* Save the identifier. */
7682 identifier = token->u.value;
7684 else
7686 bool expr_non_constant_p;
7688 /* Parse the next assignment-expression. */
7689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7691 /* A braced-init-list. */
7692 cp_lexer_set_source_position (parser->lexer);
7693 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7694 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7695 if (non_constant_p && expr_non_constant_p)
7696 *non_constant_p = true;
7698 else if (non_constant_p)
7700 expr = (cp_parser_constant_expression
7701 (parser, /*allow_non_constant_p=*/true,
7702 &expr_non_constant_p));
7703 if (expr_non_constant_p)
7704 *non_constant_p = true;
7706 else
7707 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7708 cast_p);
7710 if (fold_expr_p)
7711 expr = instantiate_non_dependent_expr (expr);
7713 /* If we have an ellipsis, then this is an expression
7714 expansion. */
7715 if (allow_expansion_p
7716 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7718 /* Consume the `...'. */
7719 cp_lexer_consume_token (parser->lexer);
7721 /* Build the argument pack. */
7722 expr = make_pack_expansion (expr);
7725 /* Add it to the list. We add error_mark_node
7726 expressions to the list, so that we can still tell if
7727 the correct form for a parenthesized expression-list
7728 is found. That gives better errors. */
7729 vec_safe_push (expression_list, expr);
7731 if (expr == error_mark_node)
7732 goto skip_comma;
7735 /* After the first item, attribute lists look the same as
7736 expression lists. */
7737 is_attribute_list = non_attr;
7739 get_comma:;
7740 /* If the next token isn't a `,', then we are done. */
7741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7742 break;
7744 /* Otherwise, consume the `,' and keep going. */
7745 cp_lexer_consume_token (parser->lexer);
7748 if (close_paren_loc)
7749 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7751 if (!parens.require_close (parser))
7753 int ending;
7755 skip_comma:;
7756 /* We try and resync to an unnested comma, as that will give the
7757 user better diagnostics. */
7758 ending = cp_parser_skip_to_closing_parenthesis (parser,
7759 /*recovering=*/true,
7760 /*or_comma=*/true,
7761 /*consume_paren=*/true);
7762 if (ending < 0)
7763 goto get_comma;
7764 if (!ending)
7766 parser->greater_than_is_operator_p
7767 = saved_greater_than_is_operator_p;
7768 return NULL;
7772 parser->greater_than_is_operator_p
7773 = saved_greater_than_is_operator_p;
7775 if (identifier)
7776 vec_safe_insert (expression_list, 0, identifier);
7778 return expression_list;
7781 /* Parse a pseudo-destructor-name.
7783 pseudo-destructor-name:
7784 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7785 :: [opt] nested-name-specifier template template-id :: ~ type-name
7786 :: [opt] nested-name-specifier [opt] ~ type-name
7788 If either of the first two productions is used, sets *SCOPE to the
7789 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7790 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7791 or ERROR_MARK_NODE if the parse fails. */
7793 static void
7794 cp_parser_pseudo_destructor_name (cp_parser* parser,
7795 tree object,
7796 tree* scope,
7797 tree* type)
7799 bool nested_name_specifier_p;
7801 /* Handle ~auto. */
7802 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7803 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7804 && !type_dependent_expression_p (object))
7806 if (cxx_dialect < cxx14)
7807 pedwarn (input_location, 0,
7808 "%<~auto%> only available with "
7809 "-std=c++14 or -std=gnu++14");
7810 cp_lexer_consume_token (parser->lexer);
7811 cp_lexer_consume_token (parser->lexer);
7812 *scope = NULL_TREE;
7813 *type = TREE_TYPE (object);
7814 return;
7817 /* Assume that things will not work out. */
7818 *type = error_mark_node;
7820 /* Look for the optional `::' operator. */
7821 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7822 /* Look for the optional nested-name-specifier. */
7823 nested_name_specifier_p
7824 = (cp_parser_nested_name_specifier_opt (parser,
7825 /*typename_keyword_p=*/false,
7826 /*check_dependency_p=*/true,
7827 /*type_p=*/false,
7828 /*is_declaration=*/false)
7829 != NULL_TREE);
7830 /* Now, if we saw a nested-name-specifier, we might be doing the
7831 second production. */
7832 if (nested_name_specifier_p
7833 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7835 /* Consume the `template' keyword. */
7836 cp_lexer_consume_token (parser->lexer);
7837 /* Parse the template-id. */
7838 cp_parser_template_id (parser,
7839 /*template_keyword_p=*/true,
7840 /*check_dependency_p=*/false,
7841 class_type,
7842 /*is_declaration=*/true);
7843 /* Look for the `::' token. */
7844 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7846 /* If the next token is not a `~', then there might be some
7847 additional qualification. */
7848 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7850 /* At this point, we're looking for "type-name :: ~". The type-name
7851 must not be a class-name, since this is a pseudo-destructor. So,
7852 it must be either an enum-name, or a typedef-name -- both of which
7853 are just identifiers. So, we peek ahead to check that the "::"
7854 and "~" tokens are present; if they are not, then we can avoid
7855 calling type_name. */
7856 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7857 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7858 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7860 cp_parser_error (parser, "non-scalar type");
7861 return;
7864 /* Look for the type-name. */
7865 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7866 if (*scope == error_mark_node)
7867 return;
7869 /* Look for the `::' token. */
7870 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7872 else
7873 *scope = NULL_TREE;
7875 /* Look for the `~'. */
7876 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7878 /* Once we see the ~, this has to be a pseudo-destructor. */
7879 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7880 cp_parser_commit_to_topmost_tentative_parse (parser);
7882 /* Look for the type-name again. We are not responsible for
7883 checking that it matches the first type-name. */
7884 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7887 /* Parse a unary-expression.
7889 unary-expression:
7890 postfix-expression
7891 ++ cast-expression
7892 -- cast-expression
7893 unary-operator cast-expression
7894 sizeof unary-expression
7895 sizeof ( type-id )
7896 alignof ( type-id ) [C++0x]
7897 new-expression
7898 delete-expression
7900 GNU Extensions:
7902 unary-expression:
7903 __extension__ cast-expression
7904 __alignof__ unary-expression
7905 __alignof__ ( type-id )
7906 alignof unary-expression [C++0x]
7907 __real__ cast-expression
7908 __imag__ cast-expression
7909 && identifier
7910 sizeof ( type-id ) { initializer-list , [opt] }
7911 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7912 __alignof__ ( type-id ) { initializer-list , [opt] }
7914 ADDRESS_P is true iff the unary-expression is appearing as the
7915 operand of the `&' operator. CAST_P is true if this expression is
7916 the target of a cast.
7918 Returns a representation of the expression. */
7920 static cp_expr
7921 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7922 bool address_p, bool cast_p, bool decltype_p)
7924 cp_token *token;
7925 enum tree_code unary_operator;
7927 /* Peek at the next token. */
7928 token = cp_lexer_peek_token (parser->lexer);
7929 /* Some keywords give away the kind of expression. */
7930 if (token->type == CPP_KEYWORD)
7932 enum rid keyword = token->keyword;
7934 switch (keyword)
7936 case RID_ALIGNOF:
7937 case RID_SIZEOF:
7939 tree operand, ret;
7940 enum tree_code op;
7941 location_t start_loc = token->location;
7943 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7944 /* Consume the token. */
7945 cp_lexer_consume_token (parser->lexer);
7946 /* Parse the operand. */
7947 operand = cp_parser_sizeof_operand (parser, keyword);
7949 if (TYPE_P (operand))
7950 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7951 else
7953 /* ISO C++ defines alignof only with types, not with
7954 expressions. So pedwarn if alignof is used with a non-
7955 type expression. However, __alignof__ is ok. */
7956 if (id_equal (token->u.value, "alignof"))
7957 pedwarn (token->location, OPT_Wpedantic,
7958 "ISO C++ does not allow %<alignof%> "
7959 "with a non-type");
7961 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7963 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7964 SIZEOF_EXPR with the original operand. */
7965 if (op == SIZEOF_EXPR && ret != error_mark_node)
7967 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7969 if (!processing_template_decl && TYPE_P (operand))
7971 ret = build_min (SIZEOF_EXPR, size_type_node,
7972 build1 (NOP_EXPR, operand,
7973 error_mark_node));
7974 SIZEOF_EXPR_TYPE_P (ret) = 1;
7976 else
7977 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7978 TREE_SIDE_EFFECTS (ret) = 0;
7979 TREE_READONLY (ret) = 1;
7983 /* Construct a location e.g. :
7984 alignof (expr)
7985 ^~~~~~~~~~~~~~
7986 with start == caret at the start of the "alignof"/"sizeof"
7987 token, with the endpoint at the final closing paren. */
7988 location_t finish_loc
7989 = cp_lexer_previous_token (parser->lexer)->location;
7990 location_t compound_loc
7991 = make_location (start_loc, start_loc, finish_loc);
7993 cp_expr ret_expr (ret);
7994 ret_expr.set_location (compound_loc);
7995 return ret_expr;
7998 case RID_NEW:
7999 return cp_parser_new_expression (parser);
8001 case RID_DELETE:
8002 return cp_parser_delete_expression (parser);
8004 case RID_EXTENSION:
8006 /* The saved value of the PEDANTIC flag. */
8007 int saved_pedantic;
8008 tree expr;
8010 /* Save away the PEDANTIC flag. */
8011 cp_parser_extension_opt (parser, &saved_pedantic);
8012 /* Parse the cast-expression. */
8013 expr = cp_parser_simple_cast_expression (parser);
8014 /* Restore the PEDANTIC flag. */
8015 pedantic = saved_pedantic;
8017 return expr;
8020 case RID_REALPART:
8021 case RID_IMAGPART:
8023 tree expression;
8025 /* Consume the `__real__' or `__imag__' token. */
8026 cp_lexer_consume_token (parser->lexer);
8027 /* Parse the cast-expression. */
8028 expression = cp_parser_simple_cast_expression (parser);
8029 /* Create the complete representation. */
8030 return build_x_unary_op (token->location,
8031 (keyword == RID_REALPART
8032 ? REALPART_EXPR : IMAGPART_EXPR),
8033 expression,
8034 tf_warning_or_error);
8036 break;
8038 case RID_TRANSACTION_ATOMIC:
8039 case RID_TRANSACTION_RELAXED:
8040 return cp_parser_transaction_expression (parser, keyword);
8042 case RID_NOEXCEPT:
8044 tree expr;
8045 const char *saved_message;
8046 bool saved_integral_constant_expression_p;
8047 bool saved_non_integral_constant_expression_p;
8048 bool saved_greater_than_is_operator_p;
8050 cp_lexer_consume_token (parser->lexer);
8051 matching_parens parens;
8052 parens.require_open (parser);
8054 saved_message = parser->type_definition_forbidden_message;
8055 parser->type_definition_forbidden_message
8056 = G_("types may not be defined in %<noexcept%> expressions");
8058 saved_integral_constant_expression_p
8059 = parser->integral_constant_expression_p;
8060 saved_non_integral_constant_expression_p
8061 = parser->non_integral_constant_expression_p;
8062 parser->integral_constant_expression_p = false;
8064 saved_greater_than_is_operator_p
8065 = parser->greater_than_is_operator_p;
8066 parser->greater_than_is_operator_p = true;
8068 ++cp_unevaluated_operand;
8069 ++c_inhibit_evaluation_warnings;
8070 ++cp_noexcept_operand;
8071 expr = cp_parser_expression (parser);
8072 --cp_noexcept_operand;
8073 --c_inhibit_evaluation_warnings;
8074 --cp_unevaluated_operand;
8076 parser->greater_than_is_operator_p
8077 = saved_greater_than_is_operator_p;
8079 parser->integral_constant_expression_p
8080 = saved_integral_constant_expression_p;
8081 parser->non_integral_constant_expression_p
8082 = saved_non_integral_constant_expression_p;
8084 parser->type_definition_forbidden_message = saved_message;
8086 parens.require_close (parser);
8087 return finish_noexcept_expr (expr, tf_warning_or_error);
8090 default:
8091 break;
8095 /* Look for the `:: new' and `:: delete', which also signal the
8096 beginning of a new-expression, or delete-expression,
8097 respectively. If the next token is `::', then it might be one of
8098 these. */
8099 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8101 enum rid keyword;
8103 /* See if the token after the `::' is one of the keywords in
8104 which we're interested. */
8105 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8106 /* If it's `new', we have a new-expression. */
8107 if (keyword == RID_NEW)
8108 return cp_parser_new_expression (parser);
8109 /* Similarly, for `delete'. */
8110 else if (keyword == RID_DELETE)
8111 return cp_parser_delete_expression (parser);
8114 /* Look for a unary operator. */
8115 unary_operator = cp_parser_unary_operator (token);
8116 /* The `++' and `--' operators can be handled similarly, even though
8117 they are not technically unary-operators in the grammar. */
8118 if (unary_operator == ERROR_MARK)
8120 if (token->type == CPP_PLUS_PLUS)
8121 unary_operator = PREINCREMENT_EXPR;
8122 else if (token->type == CPP_MINUS_MINUS)
8123 unary_operator = PREDECREMENT_EXPR;
8124 /* Handle the GNU address-of-label extension. */
8125 else if (cp_parser_allow_gnu_extensions_p (parser)
8126 && token->type == CPP_AND_AND)
8128 tree identifier;
8129 tree expression;
8130 location_t start_loc = token->location;
8132 /* Consume the '&&' token. */
8133 cp_lexer_consume_token (parser->lexer);
8134 /* Look for the identifier. */
8135 location_t finish_loc
8136 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8137 identifier = cp_parser_identifier (parser);
8138 /* Construct a location of the form:
8139 &&label
8140 ^~~~~~~
8141 with caret==start at the "&&", finish at the end of the label. */
8142 location_t combined_loc
8143 = make_location (start_loc, start_loc, finish_loc);
8144 /* Create an expression representing the address. */
8145 expression = finish_label_address_expr (identifier, combined_loc);
8146 if (cp_parser_non_integral_constant_expression (parser,
8147 NIC_ADDR_LABEL))
8148 expression = error_mark_node;
8149 return expression;
8152 if (unary_operator != ERROR_MARK)
8154 cp_expr cast_expression;
8155 cp_expr expression = error_mark_node;
8156 non_integral_constant non_constant_p = NIC_NONE;
8157 location_t loc = token->location;
8158 tsubst_flags_t complain = complain_flags (decltype_p);
8160 /* Consume the operator token. */
8161 token = cp_lexer_consume_token (parser->lexer);
8162 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8164 /* Parse the cast-expression. */
8165 cast_expression
8166 = cp_parser_cast_expression (parser,
8167 unary_operator == ADDR_EXPR,
8168 /*cast_p=*/false,
8169 /*decltype*/false,
8170 pidk);
8172 /* Make a location:
8173 OP_TOKEN CAST_EXPRESSION
8174 ^~~~~~~~~~~~~~~~~~~~~~~~~
8175 with start==caret at the operator token, and
8176 extending to the end of the cast_expression. */
8177 loc = make_location (loc, loc, cast_expression.get_finish ());
8179 /* Now, build an appropriate representation. */
8180 switch (unary_operator)
8182 case INDIRECT_REF:
8183 non_constant_p = NIC_STAR;
8184 expression = build_x_indirect_ref (loc, cast_expression,
8185 RO_UNARY_STAR,
8186 complain);
8187 /* TODO: build_x_indirect_ref does not always honor the
8188 location, so ensure it is set. */
8189 expression.set_location (loc);
8190 break;
8192 case ADDR_EXPR:
8193 non_constant_p = NIC_ADDR;
8194 /* Fall through. */
8195 case BIT_NOT_EXPR:
8196 expression = build_x_unary_op (loc, unary_operator,
8197 cast_expression,
8198 complain);
8199 /* TODO: build_x_unary_op does not always honor the location,
8200 so ensure it is set. */
8201 expression.set_location (loc);
8202 break;
8204 case PREINCREMENT_EXPR:
8205 case PREDECREMENT_EXPR:
8206 non_constant_p = unary_operator == PREINCREMENT_EXPR
8207 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8208 /* Fall through. */
8209 case NEGATE_EXPR:
8210 /* Immediately fold negation of a constant, unless the constant is 0
8211 (since -0 == 0) or it would overflow. */
8212 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8213 && CONSTANT_CLASS_P (cast_expression)
8214 && !integer_zerop (cast_expression)
8215 && !TREE_OVERFLOW (cast_expression))
8217 tree folded = fold_build1 (unary_operator,
8218 TREE_TYPE (cast_expression),
8219 cast_expression);
8220 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8222 expression = cp_expr (folded, loc);
8223 break;
8226 /* Fall through. */
8227 case UNARY_PLUS_EXPR:
8228 case TRUTH_NOT_EXPR:
8229 expression = finish_unary_op_expr (loc, unary_operator,
8230 cast_expression, complain);
8231 break;
8233 default:
8234 gcc_unreachable ();
8237 if (non_constant_p != NIC_NONE
8238 && cp_parser_non_integral_constant_expression (parser,
8239 non_constant_p))
8240 expression = error_mark_node;
8242 return expression;
8245 return cp_parser_postfix_expression (parser, address_p, cast_p,
8246 /*member_access_only_p=*/false,
8247 decltype_p,
8248 pidk);
8251 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8252 unary-operator, the corresponding tree code is returned. */
8254 static enum tree_code
8255 cp_parser_unary_operator (cp_token* token)
8257 switch (token->type)
8259 case CPP_MULT:
8260 return INDIRECT_REF;
8262 case CPP_AND:
8263 return ADDR_EXPR;
8265 case CPP_PLUS:
8266 return UNARY_PLUS_EXPR;
8268 case CPP_MINUS:
8269 return NEGATE_EXPR;
8271 case CPP_NOT:
8272 return TRUTH_NOT_EXPR;
8274 case CPP_COMPL:
8275 return BIT_NOT_EXPR;
8277 default:
8278 return ERROR_MARK;
8282 /* Parse a new-expression.
8284 new-expression:
8285 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8286 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8288 Returns a representation of the expression. */
8290 static tree
8291 cp_parser_new_expression (cp_parser* parser)
8293 bool global_scope_p;
8294 vec<tree, va_gc> *placement;
8295 tree type;
8296 vec<tree, va_gc> *initializer;
8297 tree nelts = NULL_TREE;
8298 tree ret;
8300 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8302 /* Look for the optional `::' operator. */
8303 global_scope_p
8304 = (cp_parser_global_scope_opt (parser,
8305 /*current_scope_valid_p=*/false)
8306 != NULL_TREE);
8307 /* Look for the `new' operator. */
8308 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8309 /* There's no easy way to tell a new-placement from the
8310 `( type-id )' construct. */
8311 cp_parser_parse_tentatively (parser);
8312 /* Look for a new-placement. */
8313 placement = cp_parser_new_placement (parser);
8314 /* If that didn't work out, there's no new-placement. */
8315 if (!cp_parser_parse_definitely (parser))
8317 if (placement != NULL)
8318 release_tree_vector (placement);
8319 placement = NULL;
8322 /* If the next token is a `(', then we have a parenthesized
8323 type-id. */
8324 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8326 cp_token *token;
8327 const char *saved_message = parser->type_definition_forbidden_message;
8329 /* Consume the `('. */
8330 matching_parens parens;
8331 parens.consume_open (parser);
8333 /* Parse the type-id. */
8334 parser->type_definition_forbidden_message
8335 = G_("types may not be defined in a new-expression");
8337 type_id_in_expr_sentinel s (parser);
8338 type = cp_parser_type_id (parser);
8340 parser->type_definition_forbidden_message = saved_message;
8342 /* Look for the closing `)'. */
8343 parens.require_close (parser);
8344 token = cp_lexer_peek_token (parser->lexer);
8345 /* There should not be a direct-new-declarator in this production,
8346 but GCC used to allowed this, so we check and emit a sensible error
8347 message for this case. */
8348 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8350 error_at (token->location,
8351 "array bound forbidden after parenthesized type-id");
8352 inform (token->location,
8353 "try removing the parentheses around the type-id");
8354 cp_parser_direct_new_declarator (parser);
8357 /* Otherwise, there must be a new-type-id. */
8358 else
8359 type = cp_parser_new_type_id (parser, &nelts);
8361 /* If the next token is a `(' or '{', then we have a new-initializer. */
8362 cp_token *token = cp_lexer_peek_token (parser->lexer);
8363 if (token->type == CPP_OPEN_PAREN
8364 || token->type == CPP_OPEN_BRACE)
8365 initializer = cp_parser_new_initializer (parser);
8366 else
8367 initializer = NULL;
8369 /* A new-expression may not appear in an integral constant
8370 expression. */
8371 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8372 ret = error_mark_node;
8373 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8374 of a new-type-id or type-id of a new-expression, the new-expression shall
8375 contain a new-initializer of the form ( assignment-expression )".
8376 Additionally, consistently with the spirit of DR 1467, we want to accept
8377 'new auto { 2 }' too. */
8378 else if ((ret = type_uses_auto (type))
8379 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8380 && (vec_safe_length (initializer) != 1
8381 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8382 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8384 error_at (token->location,
8385 "initialization of new-expression for type %<auto%> "
8386 "requires exactly one element");
8387 ret = error_mark_node;
8389 else
8391 /* Construct a location e.g.:
8392 ptr = new int[100]
8393 ^~~~~~~~~~~~
8394 with caret == start at the start of the "new" token, and the end
8395 at the end of the final token we consumed. */
8396 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8397 location_t end_loc = get_finish (end_tok->location);
8398 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8400 /* Create a representation of the new-expression. */
8401 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8402 tf_warning_or_error);
8403 protected_set_expr_location (ret, combined_loc);
8406 if (placement != NULL)
8407 release_tree_vector (placement);
8408 if (initializer != NULL)
8409 release_tree_vector (initializer);
8411 return ret;
8414 /* Parse a new-placement.
8416 new-placement:
8417 ( expression-list )
8419 Returns the same representation as for an expression-list. */
8421 static vec<tree, va_gc> *
8422 cp_parser_new_placement (cp_parser* parser)
8424 vec<tree, va_gc> *expression_list;
8426 /* Parse the expression-list. */
8427 expression_list = (cp_parser_parenthesized_expression_list
8428 (parser, non_attr, /*cast_p=*/false,
8429 /*allow_expansion_p=*/true,
8430 /*non_constant_p=*/NULL));
8432 if (expression_list && expression_list->is_empty ())
8433 error ("expected expression-list or type-id");
8435 return expression_list;
8438 /* Parse a new-type-id.
8440 new-type-id:
8441 type-specifier-seq new-declarator [opt]
8443 Returns the TYPE allocated. If the new-type-id indicates an array
8444 type, *NELTS is set to the number of elements in the last array
8445 bound; the TYPE will not include the last array bound. */
8447 static tree
8448 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8450 cp_decl_specifier_seq type_specifier_seq;
8451 cp_declarator *new_declarator;
8452 cp_declarator *declarator;
8453 cp_declarator *outer_declarator;
8454 const char *saved_message;
8456 /* The type-specifier sequence must not contain type definitions.
8457 (It cannot contain declarations of new types either, but if they
8458 are not definitions we will catch that because they are not
8459 complete.) */
8460 saved_message = parser->type_definition_forbidden_message;
8461 parser->type_definition_forbidden_message
8462 = G_("types may not be defined in a new-type-id");
8463 /* Parse the type-specifier-seq. */
8464 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8465 /*is_trailing_return=*/false,
8466 &type_specifier_seq);
8467 /* Restore the old message. */
8468 parser->type_definition_forbidden_message = saved_message;
8470 if (type_specifier_seq.type == error_mark_node)
8471 return error_mark_node;
8473 /* Parse the new-declarator. */
8474 new_declarator = cp_parser_new_declarator_opt (parser);
8476 /* Determine the number of elements in the last array dimension, if
8477 any. */
8478 *nelts = NULL_TREE;
8479 /* Skip down to the last array dimension. */
8480 declarator = new_declarator;
8481 outer_declarator = NULL;
8482 while (declarator && (declarator->kind == cdk_pointer
8483 || declarator->kind == cdk_ptrmem))
8485 outer_declarator = declarator;
8486 declarator = declarator->declarator;
8488 while (declarator
8489 && declarator->kind == cdk_array
8490 && declarator->declarator
8491 && declarator->declarator->kind == cdk_array)
8493 outer_declarator = declarator;
8494 declarator = declarator->declarator;
8497 if (declarator && declarator->kind == cdk_array)
8499 *nelts = declarator->u.array.bounds;
8500 if (*nelts == error_mark_node)
8501 *nelts = integer_one_node;
8503 if (outer_declarator)
8504 outer_declarator->declarator = declarator->declarator;
8505 else
8506 new_declarator = NULL;
8509 return groktypename (&type_specifier_seq, new_declarator, false);
8512 /* Parse an (optional) new-declarator.
8514 new-declarator:
8515 ptr-operator new-declarator [opt]
8516 direct-new-declarator
8518 Returns the declarator. */
8520 static cp_declarator *
8521 cp_parser_new_declarator_opt (cp_parser* parser)
8523 enum tree_code code;
8524 tree type, std_attributes = NULL_TREE;
8525 cp_cv_quals cv_quals;
8527 /* We don't know if there's a ptr-operator next, or not. */
8528 cp_parser_parse_tentatively (parser);
8529 /* Look for a ptr-operator. */
8530 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8531 /* If that worked, look for more new-declarators. */
8532 if (cp_parser_parse_definitely (parser))
8534 cp_declarator *declarator;
8536 /* Parse another optional declarator. */
8537 declarator = cp_parser_new_declarator_opt (parser);
8539 declarator = cp_parser_make_indirect_declarator
8540 (code, type, cv_quals, declarator, std_attributes);
8542 return declarator;
8545 /* If the next token is a `[', there is a direct-new-declarator. */
8546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8547 return cp_parser_direct_new_declarator (parser);
8549 return NULL;
8552 /* Parse a direct-new-declarator.
8554 direct-new-declarator:
8555 [ expression ]
8556 direct-new-declarator [constant-expression]
8560 static cp_declarator *
8561 cp_parser_direct_new_declarator (cp_parser* parser)
8563 cp_declarator *declarator = NULL;
8565 while (true)
8567 tree expression;
8568 cp_token *token;
8570 /* Look for the opening `['. */
8571 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8573 token = cp_lexer_peek_token (parser->lexer);
8574 expression = cp_parser_expression (parser);
8575 /* The standard requires that the expression have integral
8576 type. DR 74 adds enumeration types. We believe that the
8577 real intent is that these expressions be handled like the
8578 expression in a `switch' condition, which also allows
8579 classes with a single conversion to integral or
8580 enumeration type. */
8581 if (!processing_template_decl)
8583 expression
8584 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8585 expression,
8586 /*complain=*/true);
8587 if (!expression)
8589 error_at (token->location,
8590 "expression in new-declarator must have integral "
8591 "or enumeration type");
8592 expression = error_mark_node;
8596 /* Look for the closing `]'. */
8597 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8599 /* Add this bound to the declarator. */
8600 declarator = make_array_declarator (declarator, expression);
8602 /* If the next token is not a `[', then there are no more
8603 bounds. */
8604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8605 break;
8608 return declarator;
8611 /* Parse a new-initializer.
8613 new-initializer:
8614 ( expression-list [opt] )
8615 braced-init-list
8617 Returns a representation of the expression-list. */
8619 static vec<tree, va_gc> *
8620 cp_parser_new_initializer (cp_parser* parser)
8622 vec<tree, va_gc> *expression_list;
8624 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8626 tree t;
8627 bool expr_non_constant_p;
8628 cp_lexer_set_source_position (parser->lexer);
8629 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8630 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8631 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8632 expression_list = make_tree_vector_single (t);
8634 else
8635 expression_list = (cp_parser_parenthesized_expression_list
8636 (parser, non_attr, /*cast_p=*/false,
8637 /*allow_expansion_p=*/true,
8638 /*non_constant_p=*/NULL));
8640 return expression_list;
8643 /* Parse a delete-expression.
8645 delete-expression:
8646 :: [opt] delete cast-expression
8647 :: [opt] delete [ ] cast-expression
8649 Returns a representation of the expression. */
8651 static tree
8652 cp_parser_delete_expression (cp_parser* parser)
8654 bool global_scope_p;
8655 bool array_p;
8656 tree expression;
8658 /* Look for the optional `::' operator. */
8659 global_scope_p
8660 = (cp_parser_global_scope_opt (parser,
8661 /*current_scope_valid_p=*/false)
8662 != NULL_TREE);
8663 /* Look for the `delete' keyword. */
8664 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8665 /* See if the array syntax is in use. */
8666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8668 /* Consume the `[' token. */
8669 cp_lexer_consume_token (parser->lexer);
8670 /* Look for the `]' token. */
8671 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8672 /* Remember that this is the `[]' construct. */
8673 array_p = true;
8675 else
8676 array_p = false;
8678 /* Parse the cast-expression. */
8679 expression = cp_parser_simple_cast_expression (parser);
8681 /* A delete-expression may not appear in an integral constant
8682 expression. */
8683 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8684 return error_mark_node;
8686 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8687 tf_warning_or_error);
8690 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8691 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8692 0 otherwise. */
8694 static int
8695 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8697 cp_token *token = cp_lexer_peek_token (parser->lexer);
8698 switch (token->type)
8700 case CPP_COMMA:
8701 case CPP_SEMICOLON:
8702 case CPP_QUERY:
8703 case CPP_COLON:
8704 case CPP_CLOSE_SQUARE:
8705 case CPP_CLOSE_PAREN:
8706 case CPP_CLOSE_BRACE:
8707 case CPP_OPEN_BRACE:
8708 case CPP_DOT:
8709 case CPP_DOT_STAR:
8710 case CPP_DEREF:
8711 case CPP_DEREF_STAR:
8712 case CPP_DIV:
8713 case CPP_MOD:
8714 case CPP_LSHIFT:
8715 case CPP_RSHIFT:
8716 case CPP_LESS:
8717 case CPP_GREATER:
8718 case CPP_LESS_EQ:
8719 case CPP_GREATER_EQ:
8720 case CPP_EQ_EQ:
8721 case CPP_NOT_EQ:
8722 case CPP_EQ:
8723 case CPP_MULT_EQ:
8724 case CPP_DIV_EQ:
8725 case CPP_MOD_EQ:
8726 case CPP_PLUS_EQ:
8727 case CPP_MINUS_EQ:
8728 case CPP_RSHIFT_EQ:
8729 case CPP_LSHIFT_EQ:
8730 case CPP_AND_EQ:
8731 case CPP_XOR_EQ:
8732 case CPP_OR_EQ:
8733 case CPP_XOR:
8734 case CPP_OR:
8735 case CPP_OR_OR:
8736 case CPP_EOF:
8737 case CPP_ELLIPSIS:
8738 return 0;
8740 case CPP_OPEN_PAREN:
8741 /* In ((type ()) () the last () isn't a valid cast-expression,
8742 so the whole must be parsed as postfix-expression. */
8743 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8744 != CPP_CLOSE_PAREN;
8746 case CPP_OPEN_SQUARE:
8747 /* '[' may start a primary-expression in obj-c++ and in C++11,
8748 as a lambda-expression, eg, '(void)[]{}'. */
8749 if (cxx_dialect >= cxx11)
8750 return -1;
8751 return c_dialect_objc ();
8753 case CPP_PLUS_PLUS:
8754 case CPP_MINUS_MINUS:
8755 /* '++' and '--' may or may not start a cast-expression:
8757 struct T { void operator++(int); };
8758 void f() { (T())++; }
8762 int a;
8763 (int)++a; */
8764 return -1;
8766 default:
8767 return 1;
8771 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8772 in the order: const_cast, static_cast, reinterpret_cast.
8774 Don't suggest dynamic_cast.
8776 Return the first legal cast kind found, or NULL otherwise. */
8778 static const char *
8779 get_cast_suggestion (tree dst_type, tree orig_expr)
8781 tree trial;
8783 /* Reuse the parser logic by attempting to build the various kinds of
8784 cast, with "complain" disabled.
8785 Identify the first such cast that is valid. */
8787 /* Don't attempt to run such logic within template processing. */
8788 if (processing_template_decl)
8789 return NULL;
8791 /* First try const_cast. */
8792 trial = build_const_cast (dst_type, orig_expr, tf_none);
8793 if (trial != error_mark_node)
8794 return "const_cast";
8796 /* If that fails, try static_cast. */
8797 trial = build_static_cast (dst_type, orig_expr, tf_none);
8798 if (trial != error_mark_node)
8799 return "static_cast";
8801 /* Finally, try reinterpret_cast. */
8802 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8803 if (trial != error_mark_node)
8804 return "reinterpret_cast";
8806 /* No such cast possible. */
8807 return NULL;
8810 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8811 suggesting how to convert a C-style cast of the form:
8813 (DST_TYPE)ORIG_EXPR
8815 to a C++-style cast.
8817 The primary range of RICHLOC is asssumed to be that of the original
8818 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8819 of the parens in the C-style cast. */
8821 static void
8822 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8823 location_t close_paren_loc, tree orig_expr,
8824 tree dst_type)
8826 /* This function is non-trivial, so bail out now if the warning isn't
8827 going to be emitted. */
8828 if (!warn_old_style_cast)
8829 return;
8831 /* Try to find a legal C++ cast, trying them in order:
8832 const_cast, static_cast, reinterpret_cast. */
8833 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8834 if (!cast_suggestion)
8835 return;
8837 /* Replace the open paren with "CAST_SUGGESTION<". */
8838 pretty_printer pp;
8839 pp_printf (&pp, "%s<", cast_suggestion);
8840 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8842 /* Replace the close paren with "> (". */
8843 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8845 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8846 rich_loc->add_fixit_insert_after (")");
8850 /* Parse a cast-expression.
8852 cast-expression:
8853 unary-expression
8854 ( type-id ) cast-expression
8856 ADDRESS_P is true iff the unary-expression is appearing as the
8857 operand of the `&' operator. CAST_P is true if this expression is
8858 the target of a cast.
8860 Returns a representation of the expression. */
8862 static cp_expr
8863 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8864 bool decltype_p, cp_id_kind * pidk)
8866 /* If it's a `(', then we might be looking at a cast. */
8867 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8869 tree type = NULL_TREE;
8870 cp_expr expr (NULL_TREE);
8871 int cast_expression = 0;
8872 const char *saved_message;
8874 /* There's no way to know yet whether or not this is a cast.
8875 For example, `(int (3))' is a unary-expression, while `(int)
8876 3' is a cast. So, we resort to parsing tentatively. */
8877 cp_parser_parse_tentatively (parser);
8878 /* Types may not be defined in a cast. */
8879 saved_message = parser->type_definition_forbidden_message;
8880 parser->type_definition_forbidden_message
8881 = G_("types may not be defined in casts");
8882 /* Consume the `('. */
8883 matching_parens parens;
8884 cp_token *open_paren = parens.consume_open (parser);
8885 location_t open_paren_loc = open_paren->location;
8886 location_t close_paren_loc = UNKNOWN_LOCATION;
8888 /* A very tricky bit is that `(struct S) { 3 }' is a
8889 compound-literal (which we permit in C++ as an extension).
8890 But, that construct is not a cast-expression -- it is a
8891 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8892 is legal; if the compound-literal were a cast-expression,
8893 you'd need an extra set of parentheses.) But, if we parse
8894 the type-id, and it happens to be a class-specifier, then we
8895 will commit to the parse at that point, because we cannot
8896 undo the action that is done when creating a new class. So,
8897 then we cannot back up and do a postfix-expression.
8899 Another tricky case is the following (c++/29234):
8901 struct S { void operator () (); };
8903 void foo ()
8905 ( S()() );
8908 As a type-id we parse the parenthesized S()() as a function
8909 returning a function, groktypename complains and we cannot
8910 back up in this case either.
8912 Therefore, we scan ahead to the closing `)', and check to see
8913 if the tokens after the `)' can start a cast-expression. Otherwise
8914 we are dealing with an unary-expression, a postfix-expression
8915 or something else.
8917 Yet another tricky case, in C++11, is the following (c++/54891):
8919 (void)[]{};
8921 The issue is that usually, besides the case of lambda-expressions,
8922 the parenthesized type-id cannot be followed by '[', and, eg, we
8923 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8924 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8925 we don't commit, we try a cast-expression, then an unary-expression.
8927 Save tokens so that we can put them back. */
8928 cp_lexer_save_tokens (parser->lexer);
8930 /* We may be looking at a cast-expression. */
8931 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8932 /*consume_paren=*/true))
8933 cast_expression
8934 = cp_parser_tokens_start_cast_expression (parser);
8936 /* Roll back the tokens we skipped. */
8937 cp_lexer_rollback_tokens (parser->lexer);
8938 /* If we aren't looking at a cast-expression, simulate an error so
8939 that the call to cp_parser_error_occurred below returns true. */
8940 if (!cast_expression)
8941 cp_parser_simulate_error (parser);
8942 else
8944 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8945 parser->in_type_id_in_expr_p = true;
8946 /* Look for the type-id. */
8947 type = cp_parser_type_id (parser);
8948 /* Look for the closing `)'. */
8949 cp_token *close_paren = parens.require_close (parser);
8950 if (close_paren)
8951 close_paren_loc = close_paren->location;
8952 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8955 /* Restore the saved message. */
8956 parser->type_definition_forbidden_message = saved_message;
8958 /* At this point this can only be either a cast or a
8959 parenthesized ctor such as `(T ())' that looks like a cast to
8960 function returning T. */
8961 if (!cp_parser_error_occurred (parser))
8963 /* Only commit if the cast-expression doesn't start with
8964 '++', '--', or '[' in C++11. */
8965 if (cast_expression > 0)
8966 cp_parser_commit_to_topmost_tentative_parse (parser);
8968 expr = cp_parser_cast_expression (parser,
8969 /*address_p=*/false,
8970 /*cast_p=*/true,
8971 /*decltype_p=*/false,
8972 pidk);
8974 if (cp_parser_parse_definitely (parser))
8976 /* Warn about old-style casts, if so requested. */
8977 if (warn_old_style_cast
8978 && !in_system_header_at (input_location)
8979 && !VOID_TYPE_P (type)
8980 && current_lang_name != lang_name_c)
8982 gcc_rich_location rich_loc (input_location);
8983 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8984 expr, type);
8985 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
8986 "use of old-style cast to %q#T", type);
8989 /* Only type conversions to integral or enumeration types
8990 can be used in constant-expressions. */
8991 if (!cast_valid_in_integral_constant_expression_p (type)
8992 && cp_parser_non_integral_constant_expression (parser,
8993 NIC_CAST))
8994 return error_mark_node;
8996 /* Perform the cast. */
8997 /* Make a location:
8998 (TYPE) EXPR
8999 ^~~~~~~~~~~
9000 with start==caret at the open paren, extending to the
9001 end of "expr". */
9002 location_t cast_loc = make_location (open_paren_loc,
9003 open_paren_loc,
9004 expr.get_finish ());
9005 expr = build_c_cast (cast_loc, type, expr);
9006 return expr;
9009 else
9010 cp_parser_abort_tentative_parse (parser);
9013 /* If we get here, then it's not a cast, so it must be a
9014 unary-expression. */
9015 return cp_parser_unary_expression (parser, pidk, address_p,
9016 cast_p, decltype_p);
9019 /* Parse a binary expression of the general form:
9021 pm-expression:
9022 cast-expression
9023 pm-expression .* cast-expression
9024 pm-expression ->* cast-expression
9026 multiplicative-expression:
9027 pm-expression
9028 multiplicative-expression * pm-expression
9029 multiplicative-expression / pm-expression
9030 multiplicative-expression % pm-expression
9032 additive-expression:
9033 multiplicative-expression
9034 additive-expression + multiplicative-expression
9035 additive-expression - multiplicative-expression
9037 shift-expression:
9038 additive-expression
9039 shift-expression << additive-expression
9040 shift-expression >> additive-expression
9042 relational-expression:
9043 shift-expression
9044 relational-expression < shift-expression
9045 relational-expression > shift-expression
9046 relational-expression <= shift-expression
9047 relational-expression >= shift-expression
9049 GNU Extension:
9051 relational-expression:
9052 relational-expression <? shift-expression
9053 relational-expression >? shift-expression
9055 equality-expression:
9056 relational-expression
9057 equality-expression == relational-expression
9058 equality-expression != relational-expression
9060 and-expression:
9061 equality-expression
9062 and-expression & equality-expression
9064 exclusive-or-expression:
9065 and-expression
9066 exclusive-or-expression ^ and-expression
9068 inclusive-or-expression:
9069 exclusive-or-expression
9070 inclusive-or-expression | exclusive-or-expression
9072 logical-and-expression:
9073 inclusive-or-expression
9074 logical-and-expression && inclusive-or-expression
9076 logical-or-expression:
9077 logical-and-expression
9078 logical-or-expression || logical-and-expression
9080 All these are implemented with a single function like:
9082 binary-expression:
9083 simple-cast-expression
9084 binary-expression <token> binary-expression
9086 CAST_P is true if this expression is the target of a cast.
9088 The binops_by_token map is used to get the tree codes for each <token> type.
9089 binary-expressions are associated according to a precedence table. */
9091 #define TOKEN_PRECEDENCE(token) \
9092 (((token->type == CPP_GREATER \
9093 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9094 && !parser->greater_than_is_operator_p) \
9095 ? PREC_NOT_OPERATOR \
9096 : binops_by_token[token->type].prec)
9098 static cp_expr
9099 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9100 bool no_toplevel_fold_p,
9101 bool decltype_p,
9102 enum cp_parser_prec prec,
9103 cp_id_kind * pidk)
9105 cp_parser_expression_stack stack;
9106 cp_parser_expression_stack_entry *sp = &stack[0];
9107 cp_parser_expression_stack_entry current;
9108 cp_expr rhs;
9109 cp_token *token;
9110 enum tree_code rhs_type;
9111 enum cp_parser_prec new_prec, lookahead_prec;
9112 tree overload;
9114 /* Parse the first expression. */
9115 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9116 ? TRUTH_NOT_EXPR : ERROR_MARK);
9117 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9118 cast_p, decltype_p, pidk);
9119 current.prec = prec;
9121 if (cp_parser_error_occurred (parser))
9122 return error_mark_node;
9124 for (;;)
9126 /* Get an operator token. */
9127 token = cp_lexer_peek_token (parser->lexer);
9129 if (warn_cxx11_compat
9130 && token->type == CPP_RSHIFT
9131 && !parser->greater_than_is_operator_p)
9133 if (warning_at (token->location, OPT_Wc__11_compat,
9134 "%<>>%> operator is treated"
9135 " as two right angle brackets in C++11"))
9136 inform (token->location,
9137 "suggest parentheses around %<>>%> expression");
9140 new_prec = TOKEN_PRECEDENCE (token);
9141 if (new_prec != PREC_NOT_OPERATOR
9142 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9143 /* This is a fold-expression; handle it later. */
9144 new_prec = PREC_NOT_OPERATOR;
9146 /* Popping an entry off the stack means we completed a subexpression:
9147 - either we found a token which is not an operator (`>' where it is not
9148 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9149 will happen repeatedly;
9150 - or, we found an operator which has lower priority. This is the case
9151 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9152 parsing `3 * 4'. */
9153 if (new_prec <= current.prec)
9155 if (sp == stack)
9156 break;
9157 else
9158 goto pop;
9161 get_rhs:
9162 current.tree_type = binops_by_token[token->type].tree_type;
9163 current.loc = token->location;
9165 /* We used the operator token. */
9166 cp_lexer_consume_token (parser->lexer);
9168 /* For "false && x" or "true || x", x will never be executed;
9169 disable warnings while evaluating it. */
9170 if (current.tree_type == TRUTH_ANDIF_EXPR)
9171 c_inhibit_evaluation_warnings +=
9172 cp_fully_fold (current.lhs) == truthvalue_false_node;
9173 else if (current.tree_type == TRUTH_ORIF_EXPR)
9174 c_inhibit_evaluation_warnings +=
9175 cp_fully_fold (current.lhs) == truthvalue_true_node;
9177 /* Extract another operand. It may be the RHS of this expression
9178 or the LHS of a new, higher priority expression. */
9179 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9180 ? TRUTH_NOT_EXPR : ERROR_MARK);
9181 rhs = cp_parser_simple_cast_expression (parser);
9183 /* Get another operator token. Look up its precedence to avoid
9184 building a useless (immediately popped) stack entry for common
9185 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9186 token = cp_lexer_peek_token (parser->lexer);
9187 lookahead_prec = TOKEN_PRECEDENCE (token);
9188 if (lookahead_prec != PREC_NOT_OPERATOR
9189 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9190 lookahead_prec = PREC_NOT_OPERATOR;
9191 if (lookahead_prec > new_prec)
9193 /* ... and prepare to parse the RHS of the new, higher priority
9194 expression. Since precedence levels on the stack are
9195 monotonically increasing, we do not have to care about
9196 stack overflows. */
9197 *sp = current;
9198 ++sp;
9199 current.lhs = rhs;
9200 current.lhs_type = rhs_type;
9201 current.prec = new_prec;
9202 new_prec = lookahead_prec;
9203 goto get_rhs;
9205 pop:
9206 lookahead_prec = new_prec;
9207 /* If the stack is not empty, we have parsed into LHS the right side
9208 (`4' in the example above) of an expression we had suspended.
9209 We can use the information on the stack to recover the LHS (`3')
9210 from the stack together with the tree code (`MULT_EXPR'), and
9211 the precedence of the higher level subexpression
9212 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9213 which will be used to actually build the additive expression. */
9214 rhs = current.lhs;
9215 rhs_type = current.lhs_type;
9216 --sp;
9217 current = *sp;
9220 /* Undo the disabling of warnings done above. */
9221 if (current.tree_type == TRUTH_ANDIF_EXPR)
9222 c_inhibit_evaluation_warnings -=
9223 cp_fully_fold (current.lhs) == truthvalue_false_node;
9224 else if (current.tree_type == TRUTH_ORIF_EXPR)
9225 c_inhibit_evaluation_warnings -=
9226 cp_fully_fold (current.lhs) == truthvalue_true_node;
9228 if (warn_logical_not_paren
9229 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9230 && current.lhs_type == TRUTH_NOT_EXPR
9231 /* Avoid warning for !!x == y. */
9232 && (TREE_CODE (current.lhs) != NE_EXPR
9233 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9234 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9235 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9236 /* Avoid warning for !b == y where b is boolean. */
9237 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9238 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9239 != BOOLEAN_TYPE))))
9240 /* Avoid warning for !!b == y where b is boolean. */
9241 && (!DECL_P (current.lhs)
9242 || TREE_TYPE (current.lhs) == NULL_TREE
9243 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9244 warn_logical_not_parentheses (current.loc, current.tree_type,
9245 current.lhs, maybe_constant_value (rhs));
9247 overload = NULL;
9249 location_t combined_loc = make_location (current.loc,
9250 current.lhs.get_start (),
9251 rhs.get_finish ());
9253 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9254 ERROR_MARK for everything that is not a binary expression.
9255 This makes warn_about_parentheses miss some warnings that
9256 involve unary operators. For unary expressions we should
9257 pass the correct tree_code unless the unary expression was
9258 surrounded by parentheses.
9260 if (no_toplevel_fold_p
9261 && lookahead_prec <= current.prec
9262 && sp == stack)
9263 current.lhs = build2_loc (combined_loc,
9264 current.tree_type,
9265 TREE_CODE_CLASS (current.tree_type)
9266 == tcc_comparison
9267 ? boolean_type_node : TREE_TYPE (current.lhs),
9268 current.lhs, rhs);
9269 else
9271 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9272 current.lhs, current.lhs_type,
9273 rhs, rhs_type, &overload,
9274 complain_flags (decltype_p));
9275 /* TODO: build_x_binary_op doesn't always honor the location. */
9276 current.lhs.set_location (combined_loc);
9278 current.lhs_type = current.tree_type;
9280 /* If the binary operator required the use of an overloaded operator,
9281 then this expression cannot be an integral constant-expression.
9282 An overloaded operator can be used even if both operands are
9283 otherwise permissible in an integral constant-expression if at
9284 least one of the operands is of enumeration type. */
9286 if (overload
9287 && cp_parser_non_integral_constant_expression (parser,
9288 NIC_OVERLOADED))
9289 return error_mark_node;
9292 return current.lhs;
9295 static cp_expr
9296 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9297 bool no_toplevel_fold_p,
9298 enum cp_parser_prec prec,
9299 cp_id_kind * pidk)
9301 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9302 /*decltype*/false, prec, pidk);
9305 /* Parse the `? expression : assignment-expression' part of a
9306 conditional-expression. The LOGICAL_OR_EXPR is the
9307 logical-or-expression that started the conditional-expression.
9308 Returns a representation of the entire conditional-expression.
9310 This routine is used by cp_parser_assignment_expression.
9312 ? expression : assignment-expression
9314 GNU Extensions:
9316 ? : assignment-expression */
9318 static tree
9319 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9321 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9322 cp_expr assignment_expr;
9323 struct cp_token *token;
9324 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9326 /* Consume the `?' token. */
9327 cp_lexer_consume_token (parser->lexer);
9328 token = cp_lexer_peek_token (parser->lexer);
9329 if (cp_parser_allow_gnu_extensions_p (parser)
9330 && token->type == CPP_COLON)
9332 pedwarn (token->location, OPT_Wpedantic,
9333 "ISO C++ does not allow ?: with omitted middle operand");
9334 /* Implicit true clause. */
9335 expr = NULL_TREE;
9336 c_inhibit_evaluation_warnings +=
9337 folded_logical_or_expr == truthvalue_true_node;
9338 warn_for_omitted_condop (token->location, logical_or_expr);
9340 else
9342 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9343 parser->colon_corrects_to_scope_p = false;
9344 /* Parse the expression. */
9345 c_inhibit_evaluation_warnings +=
9346 folded_logical_or_expr == truthvalue_false_node;
9347 expr = cp_parser_expression (parser);
9348 c_inhibit_evaluation_warnings +=
9349 ((folded_logical_or_expr == truthvalue_true_node)
9350 - (folded_logical_or_expr == truthvalue_false_node));
9351 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9354 /* The next token should be a `:'. */
9355 cp_parser_require (parser, CPP_COLON, RT_COLON);
9356 /* Parse the assignment-expression. */
9357 assignment_expr = cp_parser_assignment_expression (parser);
9358 c_inhibit_evaluation_warnings -=
9359 folded_logical_or_expr == truthvalue_true_node;
9361 /* Make a location:
9362 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9363 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9364 with the caret at the "?", ranging from the start of
9365 the logical_or_expr to the end of the assignment_expr. */
9366 loc = make_location (loc,
9367 logical_or_expr.get_start (),
9368 assignment_expr.get_finish ());
9370 /* Build the conditional-expression. */
9371 return build_x_conditional_expr (loc, logical_or_expr,
9372 expr,
9373 assignment_expr,
9374 tf_warning_or_error);
9377 /* Parse an assignment-expression.
9379 assignment-expression:
9380 conditional-expression
9381 logical-or-expression assignment-operator assignment_expression
9382 throw-expression
9384 CAST_P is true if this expression is the target of a cast.
9385 DECLTYPE_P is true if this expression is the operand of decltype.
9387 Returns a representation for the expression. */
9389 static cp_expr
9390 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9391 bool cast_p, bool decltype_p)
9393 cp_expr expr;
9395 /* If the next token is the `throw' keyword, then we're looking at
9396 a throw-expression. */
9397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9398 expr = cp_parser_throw_expression (parser);
9399 /* Otherwise, it must be that we are looking at a
9400 logical-or-expression. */
9401 else
9403 /* Parse the binary expressions (logical-or-expression). */
9404 expr = cp_parser_binary_expression (parser, cast_p, false,
9405 decltype_p,
9406 PREC_NOT_OPERATOR, pidk);
9407 /* If the next token is a `?' then we're actually looking at a
9408 conditional-expression. */
9409 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9410 return cp_parser_question_colon_clause (parser, expr);
9411 else
9413 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9415 /* If it's an assignment-operator, we're using the second
9416 production. */
9417 enum tree_code assignment_operator
9418 = cp_parser_assignment_operator_opt (parser);
9419 if (assignment_operator != ERROR_MARK)
9421 bool non_constant_p;
9423 /* Parse the right-hand side of the assignment. */
9424 cp_expr rhs = cp_parser_initializer_clause (parser,
9425 &non_constant_p);
9427 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9430 /* An assignment may not appear in a
9431 constant-expression. */
9432 if (cp_parser_non_integral_constant_expression (parser,
9433 NIC_ASSIGNMENT))
9434 return error_mark_node;
9435 /* Build the assignment expression. Its default
9436 location:
9437 LHS = RHS
9438 ~~~~^~~~~
9439 is the location of the '=' token as the
9440 caret, ranging from the start of the lhs to the
9441 end of the rhs. */
9442 loc = make_location (loc,
9443 expr.get_start (),
9444 rhs.get_finish ());
9445 expr = build_x_modify_expr (loc, expr,
9446 assignment_operator,
9447 rhs,
9448 complain_flags (decltype_p));
9449 /* TODO: build_x_modify_expr doesn't honor the location,
9450 so we must set it here. */
9451 expr.set_location (loc);
9456 return expr;
9459 /* Parse an (optional) assignment-operator.
9461 assignment-operator: one of
9462 = *= /= %= += -= >>= <<= &= ^= |=
9464 GNU Extension:
9466 assignment-operator: one of
9467 <?= >?=
9469 If the next token is an assignment operator, the corresponding tree
9470 code is returned, and the token is consumed. For example, for
9471 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9472 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9473 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9474 operator, ERROR_MARK is returned. */
9476 static enum tree_code
9477 cp_parser_assignment_operator_opt (cp_parser* parser)
9479 enum tree_code op;
9480 cp_token *token;
9482 /* Peek at the next token. */
9483 token = cp_lexer_peek_token (parser->lexer);
9485 switch (token->type)
9487 case CPP_EQ:
9488 op = NOP_EXPR;
9489 break;
9491 case CPP_MULT_EQ:
9492 op = MULT_EXPR;
9493 break;
9495 case CPP_DIV_EQ:
9496 op = TRUNC_DIV_EXPR;
9497 break;
9499 case CPP_MOD_EQ:
9500 op = TRUNC_MOD_EXPR;
9501 break;
9503 case CPP_PLUS_EQ:
9504 op = PLUS_EXPR;
9505 break;
9507 case CPP_MINUS_EQ:
9508 op = MINUS_EXPR;
9509 break;
9511 case CPP_RSHIFT_EQ:
9512 op = RSHIFT_EXPR;
9513 break;
9515 case CPP_LSHIFT_EQ:
9516 op = LSHIFT_EXPR;
9517 break;
9519 case CPP_AND_EQ:
9520 op = BIT_AND_EXPR;
9521 break;
9523 case CPP_XOR_EQ:
9524 op = BIT_XOR_EXPR;
9525 break;
9527 case CPP_OR_EQ:
9528 op = BIT_IOR_EXPR;
9529 break;
9531 default:
9532 /* Nothing else is an assignment operator. */
9533 op = ERROR_MARK;
9536 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9537 if (op != ERROR_MARK
9538 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9539 op = ERROR_MARK;
9541 /* If it was an assignment operator, consume it. */
9542 if (op != ERROR_MARK)
9543 cp_lexer_consume_token (parser->lexer);
9545 return op;
9548 /* Parse an expression.
9550 expression:
9551 assignment-expression
9552 expression , assignment-expression
9554 CAST_P is true if this expression is the target of a cast.
9555 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9556 except possibly parenthesized or on the RHS of a comma (N3276).
9558 Returns a representation of the expression. */
9560 static cp_expr
9561 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9562 bool cast_p, bool decltype_p)
9564 cp_expr expression = NULL_TREE;
9565 location_t loc = UNKNOWN_LOCATION;
9567 while (true)
9569 cp_expr assignment_expression;
9571 /* Parse the next assignment-expression. */
9572 assignment_expression
9573 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9575 /* We don't create a temporary for a call that is the immediate operand
9576 of decltype or on the RHS of a comma. But when we see a comma, we
9577 need to create a temporary for a call on the LHS. */
9578 if (decltype_p && !processing_template_decl
9579 && TREE_CODE (assignment_expression) == CALL_EXPR
9580 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9581 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9582 assignment_expression
9583 = build_cplus_new (TREE_TYPE (assignment_expression),
9584 assignment_expression, tf_warning_or_error);
9586 /* If this is the first assignment-expression, we can just
9587 save it away. */
9588 if (!expression)
9589 expression = assignment_expression;
9590 else
9592 /* Create a location with caret at the comma, ranging
9593 from the start of the LHS to the end of the RHS. */
9594 loc = make_location (loc,
9595 expression.get_start (),
9596 assignment_expression.get_finish ());
9597 expression = build_x_compound_expr (loc, expression,
9598 assignment_expression,
9599 complain_flags (decltype_p));
9600 expression.set_location (loc);
9602 /* If the next token is not a comma, or we're in a fold-expression, then
9603 we are done with the expression. */
9604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9605 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9606 break;
9607 /* Consume the `,'. */
9608 loc = cp_lexer_peek_token (parser->lexer)->location;
9609 cp_lexer_consume_token (parser->lexer);
9610 /* A comma operator cannot appear in a constant-expression. */
9611 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9612 expression = error_mark_node;
9615 return expression;
9618 /* Parse a constant-expression.
9620 constant-expression:
9621 conditional-expression
9623 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9624 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9625 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9626 is false, NON_CONSTANT_P should be NULL. */
9628 static cp_expr
9629 cp_parser_constant_expression (cp_parser* parser,
9630 bool allow_non_constant_p,
9631 bool *non_constant_p)
9633 bool saved_integral_constant_expression_p;
9634 bool saved_allow_non_integral_constant_expression_p;
9635 bool saved_non_integral_constant_expression_p;
9636 cp_expr expression;
9638 /* It might seem that we could simply parse the
9639 conditional-expression, and then check to see if it were
9640 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9641 one that the compiler can figure out is constant, possibly after
9642 doing some simplifications or optimizations. The standard has a
9643 precise definition of constant-expression, and we must honor
9644 that, even though it is somewhat more restrictive.
9646 For example:
9648 int i[(2, 3)];
9650 is not a legal declaration, because `(2, 3)' is not a
9651 constant-expression. The `,' operator is forbidden in a
9652 constant-expression. However, GCC's constant-folding machinery
9653 will fold this operation to an INTEGER_CST for `3'. */
9655 /* Save the old settings. */
9656 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9657 saved_allow_non_integral_constant_expression_p
9658 = parser->allow_non_integral_constant_expression_p;
9659 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9660 /* We are now parsing a constant-expression. */
9661 parser->integral_constant_expression_p = true;
9662 parser->allow_non_integral_constant_expression_p
9663 = (allow_non_constant_p || cxx_dialect >= cxx11);
9664 parser->non_integral_constant_expression_p = false;
9665 /* Although the grammar says "conditional-expression", we parse an
9666 "assignment-expression", which also permits "throw-expression"
9667 and the use of assignment operators. In the case that
9668 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9669 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9670 actually essential that we look for an assignment-expression.
9671 For example, cp_parser_initializer_clauses uses this function to
9672 determine whether a particular assignment-expression is in fact
9673 constant. */
9674 expression = cp_parser_assignment_expression (parser);
9675 /* Restore the old settings. */
9676 parser->integral_constant_expression_p
9677 = saved_integral_constant_expression_p;
9678 parser->allow_non_integral_constant_expression_p
9679 = saved_allow_non_integral_constant_expression_p;
9680 if (cxx_dialect >= cxx11)
9682 /* Require an rvalue constant expression here; that's what our
9683 callers expect. Reference constant expressions are handled
9684 separately in e.g. cp_parser_template_argument. */
9685 tree decay = expression;
9686 if (TREE_TYPE (expression)
9687 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9688 decay = build_address (expression);
9689 bool is_const = potential_rvalue_constant_expression (decay);
9690 parser->non_integral_constant_expression_p = !is_const;
9691 if (!is_const && !allow_non_constant_p)
9692 require_potential_rvalue_constant_expression (decay);
9694 if (allow_non_constant_p)
9695 *non_constant_p = parser->non_integral_constant_expression_p;
9696 parser->non_integral_constant_expression_p
9697 = saved_non_integral_constant_expression_p;
9699 return expression;
9702 /* Parse __builtin_offsetof.
9704 offsetof-expression:
9705 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9707 offsetof-member-designator:
9708 id-expression
9709 | offsetof-member-designator "." id-expression
9710 | offsetof-member-designator "[" expression "]"
9711 | offsetof-member-designator "->" id-expression */
9713 static cp_expr
9714 cp_parser_builtin_offsetof (cp_parser *parser)
9716 int save_ice_p, save_non_ice_p;
9717 tree type;
9718 cp_expr expr;
9719 cp_id_kind dummy;
9720 cp_token *token;
9721 location_t finish_loc;
9723 /* We're about to accept non-integral-constant things, but will
9724 definitely yield an integral constant expression. Save and
9725 restore these values around our local parsing. */
9726 save_ice_p = parser->integral_constant_expression_p;
9727 save_non_ice_p = parser->non_integral_constant_expression_p;
9729 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9731 /* Consume the "__builtin_offsetof" token. */
9732 cp_lexer_consume_token (parser->lexer);
9733 /* Consume the opening `('. */
9734 matching_parens parens;
9735 parens.require_open (parser);
9736 /* Parse the type-id. */
9737 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9738 type = cp_parser_type_id (parser);
9739 /* Look for the `,'. */
9740 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9741 token = cp_lexer_peek_token (parser->lexer);
9743 /* Build the (type *)null that begins the traditional offsetof macro. */
9744 tree object_ptr
9745 = build_static_cast (build_pointer_type (type), null_pointer_node,
9746 tf_warning_or_error);
9748 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9749 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9750 true, &dummy, token->location);
9751 while (true)
9753 token = cp_lexer_peek_token (parser->lexer);
9754 switch (token->type)
9756 case CPP_OPEN_SQUARE:
9757 /* offsetof-member-designator "[" expression "]" */
9758 expr = cp_parser_postfix_open_square_expression (parser, expr,
9759 true, false);
9760 break;
9762 case CPP_DEREF:
9763 /* offsetof-member-designator "->" identifier */
9764 expr = grok_array_decl (token->location, expr,
9765 integer_zero_node, false);
9766 /* FALLTHRU */
9768 case CPP_DOT:
9769 /* offsetof-member-designator "." identifier */
9770 cp_lexer_consume_token (parser->lexer);
9771 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9772 expr, true, &dummy,
9773 token->location);
9774 break;
9776 case CPP_CLOSE_PAREN:
9777 /* Consume the ")" token. */
9778 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9779 cp_lexer_consume_token (parser->lexer);
9780 goto success;
9782 default:
9783 /* Error. We know the following require will fail, but
9784 that gives the proper error message. */
9785 parens.require_close (parser);
9786 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9787 expr = error_mark_node;
9788 goto failure;
9792 success:
9793 /* Make a location of the form:
9794 __builtin_offsetof (struct s, f)
9795 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9796 with caret at the type-id, ranging from the start of the
9797 "_builtin_offsetof" token to the close paren. */
9798 loc = make_location (loc, start_loc, finish_loc);
9799 /* The result will be an INTEGER_CST, so we need to explicitly
9800 preserve the location. */
9801 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9803 failure:
9804 parser->integral_constant_expression_p = save_ice_p;
9805 parser->non_integral_constant_expression_p = save_non_ice_p;
9807 return expr;
9810 /* Parse a trait expression.
9812 Returns a representation of the expression, the underlying type
9813 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9815 static tree
9816 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9818 cp_trait_kind kind;
9819 tree type1, type2 = NULL_TREE;
9820 bool binary = false;
9821 bool variadic = false;
9823 switch (keyword)
9825 case RID_HAS_NOTHROW_ASSIGN:
9826 kind = CPTK_HAS_NOTHROW_ASSIGN;
9827 break;
9828 case RID_HAS_NOTHROW_CONSTRUCTOR:
9829 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9830 break;
9831 case RID_HAS_NOTHROW_COPY:
9832 kind = CPTK_HAS_NOTHROW_COPY;
9833 break;
9834 case RID_HAS_TRIVIAL_ASSIGN:
9835 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9836 break;
9837 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9838 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9839 break;
9840 case RID_HAS_TRIVIAL_COPY:
9841 kind = CPTK_HAS_TRIVIAL_COPY;
9842 break;
9843 case RID_HAS_TRIVIAL_DESTRUCTOR:
9844 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9845 break;
9846 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9847 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9848 break;
9849 case RID_HAS_VIRTUAL_DESTRUCTOR:
9850 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9851 break;
9852 case RID_IS_ABSTRACT:
9853 kind = CPTK_IS_ABSTRACT;
9854 break;
9855 case RID_IS_AGGREGATE:
9856 kind = CPTK_IS_AGGREGATE;
9857 break;
9858 case RID_IS_BASE_OF:
9859 kind = CPTK_IS_BASE_OF;
9860 binary = true;
9861 break;
9862 case RID_IS_CLASS:
9863 kind = CPTK_IS_CLASS;
9864 break;
9865 case RID_IS_EMPTY:
9866 kind = CPTK_IS_EMPTY;
9867 break;
9868 case RID_IS_ENUM:
9869 kind = CPTK_IS_ENUM;
9870 break;
9871 case RID_IS_FINAL:
9872 kind = CPTK_IS_FINAL;
9873 break;
9874 case RID_IS_LITERAL_TYPE:
9875 kind = CPTK_IS_LITERAL_TYPE;
9876 break;
9877 case RID_IS_POD:
9878 kind = CPTK_IS_POD;
9879 break;
9880 case RID_IS_POLYMORPHIC:
9881 kind = CPTK_IS_POLYMORPHIC;
9882 break;
9883 case RID_IS_SAME_AS:
9884 kind = CPTK_IS_SAME_AS;
9885 binary = true;
9886 break;
9887 case RID_IS_STD_LAYOUT:
9888 kind = CPTK_IS_STD_LAYOUT;
9889 break;
9890 case RID_IS_TRIVIAL:
9891 kind = CPTK_IS_TRIVIAL;
9892 break;
9893 case RID_IS_TRIVIALLY_ASSIGNABLE:
9894 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9895 binary = true;
9896 break;
9897 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9898 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9899 variadic = true;
9900 break;
9901 case RID_IS_TRIVIALLY_COPYABLE:
9902 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9903 break;
9904 case RID_IS_UNION:
9905 kind = CPTK_IS_UNION;
9906 break;
9907 case RID_UNDERLYING_TYPE:
9908 kind = CPTK_UNDERLYING_TYPE;
9909 break;
9910 case RID_BASES:
9911 kind = CPTK_BASES;
9912 break;
9913 case RID_DIRECT_BASES:
9914 kind = CPTK_DIRECT_BASES;
9915 break;
9916 case RID_IS_ASSIGNABLE:
9917 kind = CPTK_IS_ASSIGNABLE;
9918 binary = true;
9919 break;
9920 case RID_IS_CONSTRUCTIBLE:
9921 kind = CPTK_IS_CONSTRUCTIBLE;
9922 variadic = true;
9923 break;
9924 default:
9925 gcc_unreachable ();
9928 /* Consume the token. */
9929 cp_lexer_consume_token (parser->lexer);
9931 matching_parens parens;
9932 parens.require_open (parser);
9935 type_id_in_expr_sentinel s (parser);
9936 type1 = cp_parser_type_id (parser);
9939 if (type1 == error_mark_node)
9940 return error_mark_node;
9942 if (binary)
9944 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9947 type_id_in_expr_sentinel s (parser);
9948 type2 = cp_parser_type_id (parser);
9951 if (type2 == error_mark_node)
9952 return error_mark_node;
9954 else if (variadic)
9956 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9958 cp_lexer_consume_token (parser->lexer);
9959 tree elt = cp_parser_type_id (parser);
9960 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9962 cp_lexer_consume_token (parser->lexer);
9963 elt = make_pack_expansion (elt);
9965 if (elt == error_mark_node)
9966 return error_mark_node;
9967 type2 = tree_cons (NULL_TREE, elt, type2);
9971 parens.require_close (parser);
9973 /* Complete the trait expression, which may mean either processing
9974 the trait expr now or saving it for template instantiation. */
9975 switch (kind)
9977 case CPTK_UNDERLYING_TYPE:
9978 return finish_underlying_type (type1);
9979 case CPTK_BASES:
9980 return finish_bases (type1, false);
9981 case CPTK_DIRECT_BASES:
9982 return finish_bases (type1, true);
9983 default:
9984 return finish_trait_expr (kind, type1, type2);
9988 /* Parse a lambda expression.
9990 lambda-expression:
9991 lambda-introducer lambda-declarator [opt] compound-statement
9993 Returns a representation of the expression. */
9995 static cp_expr
9996 cp_parser_lambda_expression (cp_parser* parser)
9998 tree lambda_expr = build_lambda_expr ();
9999 tree type;
10000 bool ok = true;
10001 cp_token *token = cp_lexer_peek_token (parser->lexer);
10002 cp_token_position start = 0;
10004 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10006 if (cp_unevaluated_operand)
10008 if (!token->error_reported)
10010 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10011 "lambda-expression in unevaluated context");
10012 token->error_reported = true;
10014 ok = false;
10016 else if (parser->in_template_argument_list_p)
10018 if (!token->error_reported)
10020 error_at (token->location, "lambda-expression in template-argument");
10021 token->error_reported = true;
10023 ok = false;
10026 /* We may be in the middle of deferred access check. Disable
10027 it now. */
10028 push_deferring_access_checks (dk_no_deferred);
10030 cp_parser_lambda_introducer (parser, lambda_expr);
10032 type = begin_lambda_type (lambda_expr);
10033 if (type == error_mark_node)
10034 return error_mark_node;
10036 record_lambda_scope (lambda_expr);
10038 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10039 determine_visibility (TYPE_NAME (type));
10041 /* Now that we've started the type, add the capture fields for any
10042 explicit captures. */
10043 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10046 /* Inside the class, surrounding template-parameter-lists do not apply. */
10047 unsigned int saved_num_template_parameter_lists
10048 = parser->num_template_parameter_lists;
10049 unsigned char in_statement = parser->in_statement;
10050 bool in_switch_statement_p = parser->in_switch_statement_p;
10051 bool fully_implicit_function_template_p
10052 = parser->fully_implicit_function_template_p;
10053 tree implicit_template_parms = parser->implicit_template_parms;
10054 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10055 bool auto_is_implicit_function_template_parm_p
10056 = parser->auto_is_implicit_function_template_parm_p;
10058 parser->num_template_parameter_lists = 0;
10059 parser->in_statement = 0;
10060 parser->in_switch_statement_p = false;
10061 parser->fully_implicit_function_template_p = false;
10062 parser->implicit_template_parms = 0;
10063 parser->implicit_template_scope = 0;
10064 parser->auto_is_implicit_function_template_parm_p = false;
10066 /* By virtue of defining a local class, a lambda expression has access to
10067 the private variables of enclosing classes. */
10069 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10071 if (ok && cp_parser_error_occurred (parser))
10072 ok = false;
10074 if (ok)
10076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10077 && cp_parser_start_tentative_firewall (parser))
10078 start = token;
10079 cp_parser_lambda_body (parser, lambda_expr);
10081 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10083 if (cp_parser_skip_to_closing_brace (parser))
10084 cp_lexer_consume_token (parser->lexer);
10087 /* The capture list was built up in reverse order; fix that now. */
10088 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10089 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10091 if (ok)
10092 maybe_add_lambda_conv_op (type);
10094 type = finish_struct (type, /*attributes=*/NULL_TREE);
10096 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10097 parser->in_statement = in_statement;
10098 parser->in_switch_statement_p = in_switch_statement_p;
10099 parser->fully_implicit_function_template_p
10100 = fully_implicit_function_template_p;
10101 parser->implicit_template_parms = implicit_template_parms;
10102 parser->implicit_template_scope = implicit_template_scope;
10103 parser->auto_is_implicit_function_template_parm_p
10104 = auto_is_implicit_function_template_parm_p;
10107 /* This field is only used during parsing of the lambda. */
10108 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10110 /* This lambda shouldn't have any proxies left at this point. */
10111 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10112 /* And now that we're done, push proxies for an enclosing lambda. */
10113 insert_pending_capture_proxies ();
10115 if (ok)
10116 lambda_expr = build_lambda_object (lambda_expr);
10117 else
10118 lambda_expr = error_mark_node;
10120 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10122 pop_deferring_access_checks ();
10124 return lambda_expr;
10127 /* Parse the beginning of a lambda expression.
10129 lambda-introducer:
10130 [ lambda-capture [opt] ]
10132 LAMBDA_EXPR is the current representation of the lambda expression. */
10134 static void
10135 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10137 /* Need commas after the first capture. */
10138 bool first = true;
10140 /* Eat the leading `['. */
10141 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10143 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10144 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10145 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10146 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10147 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10148 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10150 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10152 cp_lexer_consume_token (parser->lexer);
10153 first = false;
10156 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10158 cp_token* capture_token;
10159 tree capture_id;
10160 tree capture_init_expr;
10161 cp_id_kind idk = CP_ID_KIND_NONE;
10162 bool explicit_init_p = false;
10164 enum capture_kind_type
10166 BY_COPY,
10167 BY_REFERENCE
10169 enum capture_kind_type capture_kind = BY_COPY;
10171 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10173 error ("expected end of capture-list");
10174 return;
10177 if (first)
10178 first = false;
10179 else
10180 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10182 /* Possibly capture `this'. */
10183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10185 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10186 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10187 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10188 "with by-copy capture default");
10189 cp_lexer_consume_token (parser->lexer);
10190 add_capture (lambda_expr,
10191 /*id=*/this_identifier,
10192 /*initializer=*/finish_this_expr (),
10193 /*by_reference_p=*/true,
10194 explicit_init_p);
10195 continue;
10198 /* Possibly capture `*this'. */
10199 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10200 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10202 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10203 if (cxx_dialect < cxx1z)
10204 pedwarn (loc, 0, "%<*this%> capture only available with "
10205 "-std=c++1z or -std=gnu++1z");
10206 cp_lexer_consume_token (parser->lexer);
10207 cp_lexer_consume_token (parser->lexer);
10208 add_capture (lambda_expr,
10209 /*id=*/this_identifier,
10210 /*initializer=*/finish_this_expr (),
10211 /*by_reference_p=*/false,
10212 explicit_init_p);
10213 continue;
10216 /* Remember whether we want to capture as a reference or not. */
10217 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10219 capture_kind = BY_REFERENCE;
10220 cp_lexer_consume_token (parser->lexer);
10223 /* Get the identifier. */
10224 capture_token = cp_lexer_peek_token (parser->lexer);
10225 capture_id = cp_parser_identifier (parser);
10227 if (capture_id == error_mark_node)
10228 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10229 delimiters, but I modified this to stop on unnested ']' as well. It
10230 was already changed to stop on unnested '}', so the
10231 "closing_parenthesis" name is no more misleading with my change. */
10233 cp_parser_skip_to_closing_parenthesis (parser,
10234 /*recovering=*/true,
10235 /*or_comma=*/true,
10236 /*consume_paren=*/true);
10237 break;
10240 /* Find the initializer for this capture. */
10241 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10242 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10243 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10245 bool direct, non_constant;
10246 /* An explicit initializer exists. */
10247 if (cxx_dialect < cxx14)
10248 pedwarn (input_location, 0,
10249 "lambda capture initializers "
10250 "only available with -std=c++14 or -std=gnu++14");
10251 capture_init_expr = cp_parser_initializer (parser, &direct,
10252 &non_constant);
10253 explicit_init_p = true;
10254 if (capture_init_expr == NULL_TREE)
10256 error ("empty initializer for lambda init-capture");
10257 capture_init_expr = error_mark_node;
10260 else
10262 const char* error_msg;
10264 /* Turn the identifier into an id-expression. */
10265 capture_init_expr
10266 = cp_parser_lookup_name_simple (parser, capture_id,
10267 capture_token->location);
10269 if (capture_init_expr == error_mark_node)
10271 unqualified_name_lookup_error (capture_id);
10272 continue;
10274 else if (DECL_P (capture_init_expr)
10275 && (!VAR_P (capture_init_expr)
10276 && TREE_CODE (capture_init_expr) != PARM_DECL))
10278 error_at (capture_token->location,
10279 "capture of non-variable %qD ",
10280 capture_init_expr);
10281 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10282 "%q#D declared here", capture_init_expr);
10283 continue;
10285 if (VAR_P (capture_init_expr)
10286 && decl_storage_duration (capture_init_expr) != dk_auto)
10288 if (pedwarn (capture_token->location, 0, "capture of variable "
10289 "%qD with non-automatic storage duration",
10290 capture_init_expr))
10291 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10292 "%q#D declared here", capture_init_expr);
10293 continue;
10296 capture_init_expr
10297 = finish_id_expression
10298 (capture_id,
10299 capture_init_expr,
10300 parser->scope,
10301 &idk,
10302 /*integral_constant_expression_p=*/false,
10303 /*allow_non_integral_constant_expression_p=*/false,
10304 /*non_integral_constant_expression_p=*/NULL,
10305 /*template_p=*/false,
10306 /*done=*/true,
10307 /*address_p=*/false,
10308 /*template_arg_p=*/false,
10309 &error_msg,
10310 capture_token->location);
10312 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10314 cp_lexer_consume_token (parser->lexer);
10315 capture_init_expr = make_pack_expansion (capture_init_expr);
10317 else
10318 check_for_bare_parameter_packs (capture_init_expr);
10321 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10322 && !explicit_init_p)
10324 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10325 && capture_kind == BY_COPY)
10326 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10327 "of %qD redundant with by-copy capture default",
10328 capture_id);
10329 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10330 && capture_kind == BY_REFERENCE)
10331 pedwarn (capture_token->location, 0, "explicit by-reference "
10332 "capture of %qD redundant with by-reference capture "
10333 "default", capture_id);
10336 add_capture (lambda_expr,
10337 capture_id,
10338 capture_init_expr,
10339 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10340 explicit_init_p);
10343 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10346 /* Parse the (optional) middle of a lambda expression.
10348 lambda-declarator:
10349 < template-parameter-list [opt] >
10350 ( parameter-declaration-clause [opt] )
10351 attribute-specifier [opt]
10352 decl-specifier-seq [opt]
10353 exception-specification [opt]
10354 lambda-return-type-clause [opt]
10356 LAMBDA_EXPR is the current representation of the lambda expression. */
10358 static bool
10359 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10361 /* 5.1.1.4 of the standard says:
10362 If a lambda-expression does not include a lambda-declarator, it is as if
10363 the lambda-declarator were ().
10364 This means an empty parameter list, no attributes, and no exception
10365 specification. */
10366 tree param_list = void_list_node;
10367 tree attributes = NULL_TREE;
10368 tree exception_spec = NULL_TREE;
10369 tree template_param_list = NULL_TREE;
10370 tree tx_qual = NULL_TREE;
10371 tree return_type = NULL_TREE;
10372 cp_decl_specifier_seq lambda_specs;
10373 clear_decl_specs (&lambda_specs);
10375 /* The template-parameter-list is optional, but must begin with
10376 an opening angle if present. */
10377 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10379 if (cxx_dialect < cxx14)
10380 pedwarn (parser->lexer->next_token->location, 0,
10381 "lambda templates are only available with "
10382 "-std=c++14 or -std=gnu++14");
10383 else
10384 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10385 "ISO C++ does not support lambda templates");
10387 cp_lexer_consume_token (parser->lexer);
10389 template_param_list = cp_parser_template_parameter_list (parser);
10391 cp_parser_skip_to_end_of_template_parameter_list (parser);
10393 /* We just processed one more parameter list. */
10394 ++parser->num_template_parameter_lists;
10397 /* The parameter-declaration-clause is optional (unless
10398 template-parameter-list was given), but must begin with an
10399 opening parenthesis if present. */
10400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10402 matching_parens parens;
10403 parens.consume_open (parser);
10405 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10407 /* Parse parameters. */
10408 param_list = cp_parser_parameter_declaration_clause (parser);
10410 /* Default arguments shall not be specified in the
10411 parameter-declaration-clause of a lambda-declarator. */
10412 if (cxx_dialect < cxx14)
10413 for (tree t = param_list; t; t = TREE_CHAIN (t))
10414 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10415 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10416 "default argument specified for lambda parameter");
10418 parens.require_close (parser);
10420 attributes = cp_parser_attributes_opt (parser);
10422 /* In the decl-specifier-seq of the lambda-declarator, each
10423 decl-specifier shall either be mutable or constexpr. */
10424 int declares_class_or_enum;
10425 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10426 cp_parser_decl_specifier_seq (parser,
10427 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10428 &lambda_specs, &declares_class_or_enum);
10429 if (lambda_specs.storage_class == sc_mutable)
10431 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10432 if (lambda_specs.conflicting_specifiers_p)
10433 error_at (lambda_specs.locations[ds_storage_class],
10434 "duplicate %<mutable%>");
10437 tx_qual = cp_parser_tx_qualifier_opt (parser);
10439 /* Parse optional exception specification. */
10440 exception_spec = cp_parser_exception_specification_opt (parser);
10442 /* Parse optional trailing return type. */
10443 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10445 cp_lexer_consume_token (parser->lexer);
10446 return_type = cp_parser_trailing_type_id (parser);
10449 /* The function parameters must be in scope all the way until after the
10450 trailing-return-type in case of decltype. */
10451 pop_bindings_and_leave_scope ();
10453 else if (template_param_list != NULL_TREE) // generate diagnostic
10454 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10456 /* Create the function call operator.
10458 Messing with declarators like this is no uglier than building up the
10459 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10460 other code. */
10462 cp_decl_specifier_seq return_type_specs;
10463 cp_declarator* declarator;
10464 tree fco;
10465 int quals;
10466 void *p;
10468 clear_decl_specs (&return_type_specs);
10469 if (return_type)
10470 return_type_specs.type = return_type;
10471 else
10472 /* Maybe we will deduce the return type later. */
10473 return_type_specs.type = make_auto ();
10475 if (lambda_specs.locations[ds_constexpr])
10477 if (cxx_dialect >= cxx1z)
10478 return_type_specs.locations[ds_constexpr]
10479 = lambda_specs.locations[ds_constexpr];
10480 else
10481 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10482 "lambda only available with -std=c++1z or -std=gnu++1z");
10485 p = obstack_alloc (&declarator_obstack, 0);
10487 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10488 sfk_none);
10490 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10491 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10492 declarator = make_call_declarator (declarator, param_list, quals,
10493 VIRT_SPEC_UNSPECIFIED,
10494 REF_QUAL_NONE,
10495 tx_qual,
10496 exception_spec,
10497 /*late_return_type=*/NULL_TREE,
10498 /*requires_clause*/NULL_TREE);
10499 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10501 fco = grokmethod (&return_type_specs,
10502 declarator,
10503 attributes);
10504 if (fco != error_mark_node)
10506 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10507 DECL_ARTIFICIAL (fco) = 1;
10508 /* Give the object parameter a different name. */
10509 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10510 if (return_type)
10511 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10513 if (template_param_list)
10515 fco = finish_member_template_decl (fco);
10516 finish_template_decl (template_param_list);
10517 --parser->num_template_parameter_lists;
10519 else if (parser->fully_implicit_function_template_p)
10520 fco = finish_fully_implicit_template (parser, fco);
10522 finish_member_declaration (fco);
10524 obstack_free (&declarator_obstack, p);
10526 return (fco != error_mark_node);
10530 /* Parse the body of a lambda expression, which is simply
10532 compound-statement
10534 but which requires special handling.
10535 LAMBDA_EXPR is the current representation of the lambda expression. */
10537 static void
10538 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10540 bool nested = (current_function_decl != NULL_TREE);
10541 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10542 if (nested)
10543 push_function_context ();
10544 else
10545 /* Still increment function_depth so that we don't GC in the
10546 middle of an expression. */
10547 ++function_depth;
10548 vec<tree> omp_privatization_save;
10549 save_omp_privatization_clauses (omp_privatization_save);
10550 /* Clear this in case we're in the middle of a default argument. */
10551 parser->local_variables_forbidden_p = false;
10553 /* Finish the function call operator
10554 - class_specifier
10555 + late_parsing_for_member
10556 + function_definition_after_declarator
10557 + ctor_initializer_opt_and_function_body */
10559 tree fco = lambda_function (lambda_expr);
10560 tree body = start_lambda_function (fco, lambda_expr);
10561 bool done = false;
10562 tree compound_stmt;
10564 matching_braces braces;
10565 if (!braces.require_open (parser))
10566 goto out;
10568 compound_stmt = begin_compound_stmt (0);
10570 /* 5.1.1.4 of the standard says:
10571 If a lambda-expression does not include a trailing-return-type, it
10572 is as if the trailing-return-type denotes the following type:
10573 * if the compound-statement is of the form
10574 { return attribute-specifier [opt] expression ; }
10575 the type of the returned expression after lvalue-to-rvalue
10576 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10577 (_conv.array_ 4.2), and function-to-pointer conversion
10578 (_conv.func_ 4.3);
10579 * otherwise, void. */
10581 /* In a lambda that has neither a lambda-return-type-clause
10582 nor a deducible form, errors should be reported for return statements
10583 in the body. Since we used void as the placeholder return type, parsing
10584 the body as usual will give such desired behavior. */
10585 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10586 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10587 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10589 tree expr = NULL_TREE;
10590 cp_id_kind idk = CP_ID_KIND_NONE;
10592 /* Parse tentatively in case there's more after the initial return
10593 statement. */
10594 cp_parser_parse_tentatively (parser);
10596 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10598 expr = cp_parser_expression (parser, &idk);
10600 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10601 braces.require_close (parser);
10603 if (cp_parser_parse_definitely (parser))
10605 if (!processing_template_decl)
10607 tree type = lambda_return_type (expr);
10608 apply_deduced_return_type (fco, type);
10609 if (type == error_mark_node)
10610 expr = error_mark_node;
10613 /* Will get error here if type not deduced yet. */
10614 finish_return_stmt (expr);
10616 done = true;
10620 if (!done)
10622 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10623 cp_parser_label_declaration (parser);
10624 cp_parser_statement_seq_opt (parser, NULL_TREE);
10625 braces.require_close (parser);
10628 finish_compound_stmt (compound_stmt);
10630 out:
10631 finish_lambda_function (body);
10634 restore_omp_privatization_clauses (omp_privatization_save);
10635 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10636 if (nested)
10637 pop_function_context();
10638 else
10639 --function_depth;
10642 /* Statements [gram.stmt.stmt] */
10644 /* Parse a statement.
10646 statement:
10647 labeled-statement
10648 expression-statement
10649 compound-statement
10650 selection-statement
10651 iteration-statement
10652 jump-statement
10653 declaration-statement
10654 try-block
10656 C++11:
10658 statement:
10659 labeled-statement
10660 attribute-specifier-seq (opt) expression-statement
10661 attribute-specifier-seq (opt) compound-statement
10662 attribute-specifier-seq (opt) selection-statement
10663 attribute-specifier-seq (opt) iteration-statement
10664 attribute-specifier-seq (opt) jump-statement
10665 declaration-statement
10666 attribute-specifier-seq (opt) try-block
10668 init-statement:
10669 expression-statement
10670 simple-declaration
10672 TM Extension:
10674 statement:
10675 atomic-statement
10677 IN_COMPOUND is true when the statement is nested inside a
10678 cp_parser_compound_statement; this matters for certain pragmas.
10680 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10681 is a (possibly labeled) if statement which is not enclosed in braces
10682 and has an else clause. This is used to implement -Wparentheses.
10684 CHAIN is a vector of if-else-if conditions. */
10686 static void
10687 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10688 bool in_compound, bool *if_p, vec<tree> *chain,
10689 location_t *loc_after_labels)
10691 tree statement, std_attrs = NULL_TREE;
10692 cp_token *token;
10693 location_t statement_location, attrs_location;
10695 restart:
10696 if (if_p != NULL)
10697 *if_p = false;
10698 /* There is no statement yet. */
10699 statement = NULL_TREE;
10701 saved_token_sentinel saved_tokens (parser->lexer);
10702 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10703 if (c_dialect_objc ())
10704 /* In obj-c++, seeing '[[' might be the either the beginning of
10705 c++11 attributes, or a nested objc-message-expression. So
10706 let's parse the c++11 attributes tentatively. */
10707 cp_parser_parse_tentatively (parser);
10708 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10709 if (c_dialect_objc ())
10711 if (!cp_parser_parse_definitely (parser))
10712 std_attrs = NULL_TREE;
10715 /* Peek at the next token. */
10716 token = cp_lexer_peek_token (parser->lexer);
10717 /* Remember the location of the first token in the statement. */
10718 statement_location = token->location;
10719 /* If this is a keyword, then that will often determine what kind of
10720 statement we have. */
10721 if (token->type == CPP_KEYWORD)
10723 enum rid keyword = token->keyword;
10725 switch (keyword)
10727 case RID_CASE:
10728 case RID_DEFAULT:
10729 /* Looks like a labeled-statement with a case label.
10730 Parse the label, and then use tail recursion to parse
10731 the statement. */
10732 cp_parser_label_for_labeled_statement (parser, std_attrs);
10733 in_compound = false;
10734 goto restart;
10736 case RID_IF:
10737 case RID_SWITCH:
10738 statement = cp_parser_selection_statement (parser, if_p, chain);
10739 break;
10741 case RID_WHILE:
10742 case RID_DO:
10743 case RID_FOR:
10744 statement = cp_parser_iteration_statement (parser, if_p, false);
10745 break;
10747 case RID_CILK_FOR:
10748 if (!flag_cilkplus)
10750 error_at (cp_lexer_peek_token (parser->lexer)->location,
10751 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10752 cp_lexer_consume_token (parser->lexer);
10753 statement = error_mark_node;
10755 else
10756 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10757 break;
10759 case RID_BREAK:
10760 case RID_CONTINUE:
10761 case RID_RETURN:
10762 case RID_GOTO:
10763 statement = cp_parser_jump_statement (parser);
10764 break;
10766 case RID_CILK_SYNC:
10767 cp_lexer_consume_token (parser->lexer);
10768 if (flag_cilkplus)
10770 tree sync_expr = build_cilk_sync ();
10771 SET_EXPR_LOCATION (sync_expr,
10772 token->location);
10773 statement = finish_expr_stmt (sync_expr);
10775 else
10777 error_at (token->location, "-fcilkplus must be enabled to use"
10778 " %<_Cilk_sync%>");
10779 statement = error_mark_node;
10781 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10782 break;
10784 /* Objective-C++ exception-handling constructs. */
10785 case RID_AT_TRY:
10786 case RID_AT_CATCH:
10787 case RID_AT_FINALLY:
10788 case RID_AT_SYNCHRONIZED:
10789 case RID_AT_THROW:
10790 statement = cp_parser_objc_statement (parser);
10791 break;
10793 case RID_TRY:
10794 statement = cp_parser_try_block (parser);
10795 break;
10797 case RID_NAMESPACE:
10798 /* This must be a namespace alias definition. */
10799 cp_parser_declaration_statement (parser);
10800 return;
10802 case RID_TRANSACTION_ATOMIC:
10803 case RID_TRANSACTION_RELAXED:
10804 case RID_SYNCHRONIZED:
10805 case RID_ATOMIC_NOEXCEPT:
10806 case RID_ATOMIC_CANCEL:
10807 statement = cp_parser_transaction (parser, token);
10808 break;
10809 case RID_TRANSACTION_CANCEL:
10810 statement = cp_parser_transaction_cancel (parser);
10811 break;
10813 default:
10814 /* It might be a keyword like `int' that can start a
10815 declaration-statement. */
10816 break;
10819 else if (token->type == CPP_NAME)
10821 /* If the next token is a `:', then we are looking at a
10822 labeled-statement. */
10823 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10824 if (token->type == CPP_COLON)
10826 /* Looks like a labeled-statement with an ordinary label.
10827 Parse the label, and then use tail recursion to parse
10828 the statement. */
10830 cp_parser_label_for_labeled_statement (parser, std_attrs);
10831 in_compound = false;
10832 goto restart;
10835 /* Anything that starts with a `{' must be a compound-statement. */
10836 else if (token->type == CPP_OPEN_BRACE)
10837 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10838 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10839 a statement all its own. */
10840 else if (token->type == CPP_PRAGMA)
10842 /* Only certain OpenMP pragmas are attached to statements, and thus
10843 are considered statements themselves. All others are not. In
10844 the context of a compound, accept the pragma as a "statement" and
10845 return so that we can check for a close brace. Otherwise we
10846 require a real statement and must go back and read one. */
10847 if (in_compound)
10848 cp_parser_pragma (parser, pragma_compound, if_p);
10849 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10850 goto restart;
10851 return;
10853 else if (token->type == CPP_EOF)
10855 cp_parser_error (parser, "expected statement");
10856 return;
10859 /* Everything else must be a declaration-statement or an
10860 expression-statement. Try for the declaration-statement
10861 first, unless we are looking at a `;', in which case we know that
10862 we have an expression-statement. */
10863 if (!statement)
10865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10867 if (std_attrs != NULL_TREE)
10869 /* Attributes should be parsed as part of the the
10870 declaration, so let's un-parse them. */
10871 saved_tokens.rollback();
10872 std_attrs = NULL_TREE;
10875 cp_parser_parse_tentatively (parser);
10876 /* Try to parse the declaration-statement. */
10877 cp_parser_declaration_statement (parser);
10878 /* If that worked, we're done. */
10879 if (cp_parser_parse_definitely (parser))
10880 return;
10882 /* All preceding labels have been parsed at this point. */
10883 if (loc_after_labels != NULL)
10884 *loc_after_labels = statement_location;
10886 /* Look for an expression-statement instead. */
10887 statement = cp_parser_expression_statement (parser, in_statement_expr);
10889 /* Handle [[fallthrough]];. */
10890 if (attribute_fallthrough_p (std_attrs))
10892 /* The next token after the fallthrough attribute is ';'. */
10893 if (statement == NULL_TREE)
10895 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10896 statement = build_call_expr_internal_loc (statement_location,
10897 IFN_FALLTHROUGH,
10898 void_type_node, 0);
10899 finish_expr_stmt (statement);
10901 else
10902 warning_at (statement_location, OPT_Wattributes,
10903 "%<fallthrough%> attribute not followed by %<;%>");
10904 std_attrs = NULL_TREE;
10908 /* Set the line number for the statement. */
10909 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10910 SET_EXPR_LOCATION (statement, statement_location);
10912 /* Allow "[[fallthrough]];", but warn otherwise. */
10913 if (std_attrs != NULL_TREE)
10914 warning_at (attrs_location,
10915 OPT_Wattributes,
10916 "attributes at the beginning of statement are ignored");
10919 /* Parse the label for a labeled-statement, i.e.
10921 identifier :
10922 case constant-expression :
10923 default :
10925 GNU Extension:
10926 case constant-expression ... constant-expression : statement
10928 When a label is parsed without errors, the label is added to the
10929 parse tree by the finish_* functions, so this function doesn't
10930 have to return the label. */
10932 static void
10933 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10935 cp_token *token;
10936 tree label = NULL_TREE;
10937 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10939 /* The next token should be an identifier. */
10940 token = cp_lexer_peek_token (parser->lexer);
10941 if (token->type != CPP_NAME
10942 && token->type != CPP_KEYWORD)
10944 cp_parser_error (parser, "expected labeled-statement");
10945 return;
10948 /* Remember whether this case or a user-defined label is allowed to fall
10949 through to. */
10950 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10952 parser->colon_corrects_to_scope_p = false;
10953 switch (token->keyword)
10955 case RID_CASE:
10957 tree expr, expr_hi;
10958 cp_token *ellipsis;
10960 /* Consume the `case' token. */
10961 cp_lexer_consume_token (parser->lexer);
10962 /* Parse the constant-expression. */
10963 expr = cp_parser_constant_expression (parser);
10964 if (check_for_bare_parameter_packs (expr))
10965 expr = error_mark_node;
10967 ellipsis = cp_lexer_peek_token (parser->lexer);
10968 if (ellipsis->type == CPP_ELLIPSIS)
10970 /* Consume the `...' token. */
10971 cp_lexer_consume_token (parser->lexer);
10972 expr_hi = cp_parser_constant_expression (parser);
10973 if (check_for_bare_parameter_packs (expr_hi))
10974 expr_hi = error_mark_node;
10976 /* We don't need to emit warnings here, as the common code
10977 will do this for us. */
10979 else
10980 expr_hi = NULL_TREE;
10982 if (parser->in_switch_statement_p)
10984 tree l = finish_case_label (token->location, expr, expr_hi);
10985 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10986 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10988 else
10989 error_at (token->location,
10990 "case label %qE not within a switch statement",
10991 expr);
10993 break;
10995 case RID_DEFAULT:
10996 /* Consume the `default' token. */
10997 cp_lexer_consume_token (parser->lexer);
10999 if (parser->in_switch_statement_p)
11001 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11002 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11003 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11005 else
11006 error_at (token->location, "case label not within a switch statement");
11007 break;
11009 default:
11010 /* Anything else must be an ordinary label. */
11011 label = finish_label_stmt (cp_parser_identifier (parser));
11012 if (label && TREE_CODE (label) == LABEL_DECL)
11013 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11014 break;
11017 /* Require the `:' token. */
11018 cp_parser_require (parser, CPP_COLON, RT_COLON);
11020 /* An ordinary label may optionally be followed by attributes.
11021 However, this is only permitted if the attributes are then
11022 followed by a semicolon. This is because, for backward
11023 compatibility, when parsing
11024 lab: __attribute__ ((unused)) int i;
11025 we want the attribute to attach to "i", not "lab". */
11026 if (label != NULL_TREE
11027 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11029 tree attrs;
11030 cp_parser_parse_tentatively (parser);
11031 attrs = cp_parser_gnu_attributes_opt (parser);
11032 if (attrs == NULL_TREE
11033 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11034 cp_parser_abort_tentative_parse (parser);
11035 else if (!cp_parser_parse_definitely (parser))
11037 else
11038 attributes = chainon (attributes, attrs);
11041 if (attributes != NULL_TREE)
11042 cplus_decl_attributes (&label, attributes, 0);
11044 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11047 /* Parse an expression-statement.
11049 expression-statement:
11050 expression [opt] ;
11052 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11053 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11054 indicates whether this expression-statement is part of an
11055 expression statement. */
11057 static tree
11058 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11060 tree statement = NULL_TREE;
11061 cp_token *token = cp_lexer_peek_token (parser->lexer);
11062 location_t loc = token->location;
11064 /* There might be attribute fallthrough. */
11065 tree attr = cp_parser_gnu_attributes_opt (parser);
11067 /* If the next token is a ';', then there is no expression
11068 statement. */
11069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11071 statement = cp_parser_expression (parser);
11072 if (statement == error_mark_node
11073 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11075 cp_parser_skip_to_end_of_block_or_statement (parser);
11076 return error_mark_node;
11080 /* Handle [[fallthrough]];. */
11081 if (attribute_fallthrough_p (attr))
11083 /* The next token after the fallthrough attribute is ';'. */
11084 if (statement == NULL_TREE)
11085 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11086 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11087 void_type_node, 0);
11088 else
11089 warning_at (loc, OPT_Wattributes,
11090 "%<fallthrough%> attribute not followed by %<;%>");
11091 attr = NULL_TREE;
11094 /* Allow "[[fallthrough]];", but warn otherwise. */
11095 if (attr != NULL_TREE)
11096 warning_at (loc, OPT_Wattributes,
11097 "attributes at the beginning of statement are ignored");
11099 /* Give a helpful message for "A<T>::type t;" and the like. */
11100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11101 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11103 if (TREE_CODE (statement) == SCOPE_REF)
11104 error_at (token->location, "need %<typename%> before %qE because "
11105 "%qT is a dependent scope",
11106 statement, TREE_OPERAND (statement, 0));
11107 else if (is_overloaded_fn (statement)
11108 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11110 /* A::A a; */
11111 tree fn = get_first_fn (statement);
11112 error_at (token->location,
11113 "%<%T::%D%> names the constructor, not the type",
11114 DECL_CONTEXT (fn), DECL_NAME (fn));
11118 /* Consume the final `;'. */
11119 cp_parser_consume_semicolon_at_end_of_statement (parser);
11121 if (in_statement_expr
11122 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11123 /* This is the final expression statement of a statement
11124 expression. */
11125 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11126 else if (statement)
11127 statement = finish_expr_stmt (statement);
11129 return statement;
11132 /* Parse a compound-statement.
11134 compound-statement:
11135 { statement-seq [opt] }
11137 GNU extension:
11139 compound-statement:
11140 { label-declaration-seq [opt] statement-seq [opt] }
11142 label-declaration-seq:
11143 label-declaration
11144 label-declaration-seq label-declaration
11146 Returns a tree representing the statement. */
11148 static tree
11149 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11150 int bcs_flags, bool function_body)
11152 tree compound_stmt;
11153 matching_braces braces;
11155 /* Consume the `{'. */
11156 if (!braces.require_open (parser))
11157 return error_mark_node;
11158 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11159 && !function_body && cxx_dialect < cxx14)
11160 pedwarn (input_location, OPT_Wpedantic,
11161 "compound-statement in constexpr function");
11162 /* Begin the compound-statement. */
11163 compound_stmt = begin_compound_stmt (bcs_flags);
11164 /* If the next keyword is `__label__' we have a label declaration. */
11165 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11166 cp_parser_label_declaration (parser);
11167 /* Parse an (optional) statement-seq. */
11168 cp_parser_statement_seq_opt (parser, in_statement_expr);
11169 /* Finish the compound-statement. */
11170 finish_compound_stmt (compound_stmt);
11171 /* Consume the `}'. */
11172 braces.require_close (parser);
11174 return compound_stmt;
11177 /* Parse an (optional) statement-seq.
11179 statement-seq:
11180 statement
11181 statement-seq [opt] statement */
11183 static void
11184 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11186 /* Scan statements until there aren't any more. */
11187 while (true)
11189 cp_token *token = cp_lexer_peek_token (parser->lexer);
11191 /* If we are looking at a `}', then we have run out of
11192 statements; the same is true if we have reached the end
11193 of file, or have stumbled upon a stray '@end'. */
11194 if (token->type == CPP_CLOSE_BRACE
11195 || token->type == CPP_EOF
11196 || token->type == CPP_PRAGMA_EOL
11197 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11198 break;
11200 /* If we are in a compound statement and find 'else' then
11201 something went wrong. */
11202 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11204 if (parser->in_statement & IN_IF_STMT)
11205 break;
11206 else
11208 token = cp_lexer_consume_token (parser->lexer);
11209 error_at (token->location, "%<else%> without a previous %<if%>");
11213 /* Parse the statement. */
11214 cp_parser_statement (parser, in_statement_expr, true, NULL);
11218 /* Return true if we're looking at (init; cond), false otherwise. */
11220 static bool
11221 cp_parser_init_statement_p (cp_parser *parser)
11223 /* Save tokens so that we can put them back. */
11224 cp_lexer_save_tokens (parser->lexer);
11226 /* Look for ';' that is not nested in () or {}. */
11227 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11228 /*recovering=*/false,
11229 CPP_SEMICOLON,
11230 /*consume_paren=*/false);
11232 /* Roll back the tokens we skipped. */
11233 cp_lexer_rollback_tokens (parser->lexer);
11235 return ret == -1;
11238 /* Parse a selection-statement.
11240 selection-statement:
11241 if ( init-statement [opt] condition ) statement
11242 if ( init-statement [opt] condition ) statement else statement
11243 switch ( init-statement [opt] condition ) statement
11245 Returns the new IF_STMT or SWITCH_STMT.
11247 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11248 is a (possibly labeled) if statement which is not enclosed in
11249 braces and has an else clause. This is used to implement
11250 -Wparentheses.
11252 CHAIN is a vector of if-else-if conditions. This is used to implement
11253 -Wduplicated-cond. */
11255 static tree
11256 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11257 vec<tree> *chain)
11259 cp_token *token;
11260 enum rid keyword;
11261 token_indent_info guard_tinfo;
11263 if (if_p != NULL)
11264 *if_p = false;
11266 /* Peek at the next token. */
11267 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11268 guard_tinfo = get_token_indent_info (token);
11270 /* See what kind of keyword it is. */
11271 keyword = token->keyword;
11272 switch (keyword)
11274 case RID_IF:
11275 case RID_SWITCH:
11277 tree statement;
11278 tree condition;
11280 bool cx = false;
11281 if (keyword == RID_IF
11282 && cp_lexer_next_token_is_keyword (parser->lexer,
11283 RID_CONSTEXPR))
11285 cx = true;
11286 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11287 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11288 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11289 "with -std=c++1z or -std=gnu++1z");
11292 /* Look for the `('. */
11293 matching_parens parens;
11294 if (!parens.require_open (parser))
11296 cp_parser_skip_to_end_of_statement (parser);
11297 return error_mark_node;
11300 /* Begin the selection-statement. */
11301 if (keyword == RID_IF)
11303 statement = begin_if_stmt ();
11304 IF_STMT_CONSTEXPR_P (statement) = cx;
11306 else
11307 statement = begin_switch_stmt ();
11309 /* Parse the optional init-statement. */
11310 if (cp_parser_init_statement_p (parser))
11312 tree decl;
11313 if (cxx_dialect < cxx1z)
11314 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11315 "init-statement in selection statements only available "
11316 "with -std=c++1z or -std=gnu++1z");
11317 cp_parser_init_statement (parser, &decl);
11320 /* Parse the condition. */
11321 condition = cp_parser_condition (parser);
11322 /* Look for the `)'. */
11323 if (!parens.require_close (parser))
11324 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11325 /*consume_paren=*/true);
11327 if (keyword == RID_IF)
11329 bool nested_if;
11330 unsigned char in_statement;
11332 /* Add the condition. */
11333 condition = finish_if_stmt_cond (condition, statement);
11335 if (warn_duplicated_cond)
11336 warn_duplicated_cond_add_or_warn (token->location, condition,
11337 &chain);
11339 /* Parse the then-clause. */
11340 in_statement = parser->in_statement;
11341 parser->in_statement |= IN_IF_STMT;
11343 /* Outside a template, the non-selected branch of a constexpr
11344 if is a 'discarded statement', i.e. unevaluated. */
11345 bool was_discarded = in_discarded_stmt;
11346 bool discard_then = (cx && !processing_template_decl
11347 && integer_zerop (condition));
11348 if (discard_then)
11350 in_discarded_stmt = true;
11351 ++c_inhibit_evaluation_warnings;
11354 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11355 guard_tinfo);
11357 parser->in_statement = in_statement;
11359 finish_then_clause (statement);
11361 if (discard_then)
11363 THEN_CLAUSE (statement) = NULL_TREE;
11364 in_discarded_stmt = was_discarded;
11365 --c_inhibit_evaluation_warnings;
11368 /* If the next token is `else', parse the else-clause. */
11369 if (cp_lexer_next_token_is_keyword (parser->lexer,
11370 RID_ELSE))
11372 bool discard_else = (cx && !processing_template_decl
11373 && integer_nonzerop (condition));
11374 if (discard_else)
11376 in_discarded_stmt = true;
11377 ++c_inhibit_evaluation_warnings;
11380 guard_tinfo
11381 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11382 /* Consume the `else' keyword. */
11383 cp_lexer_consume_token (parser->lexer);
11384 if (warn_duplicated_cond)
11386 if (cp_lexer_next_token_is_keyword (parser->lexer,
11387 RID_IF)
11388 && chain == NULL)
11390 /* We've got "if (COND) else if (COND2)". Start
11391 the condition chain and add COND as the first
11392 element. */
11393 chain = new vec<tree> ();
11394 if (!CONSTANT_CLASS_P (condition)
11395 && !TREE_SIDE_EFFECTS (condition))
11397 /* Wrap it in a NOP_EXPR so that we can set the
11398 location of the condition. */
11399 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11400 condition);
11401 SET_EXPR_LOCATION (e, token->location);
11402 chain->safe_push (e);
11405 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11406 RID_IF))
11408 /* This is if-else without subsequent if. Zap the
11409 condition chain; we would have already warned at
11410 this point. */
11411 delete chain;
11412 chain = NULL;
11415 begin_else_clause (statement);
11416 /* Parse the else-clause. */
11417 cp_parser_implicitly_scoped_statement (parser, NULL,
11418 guard_tinfo, chain);
11420 finish_else_clause (statement);
11422 /* If we are currently parsing a then-clause, then
11423 IF_P will not be NULL. We set it to true to
11424 indicate that this if statement has an else clause.
11425 This may trigger the Wparentheses warning below
11426 when we get back up to the parent if statement. */
11427 if (if_p != NULL)
11428 *if_p = true;
11430 if (discard_else)
11432 ELSE_CLAUSE (statement) = NULL_TREE;
11433 in_discarded_stmt = was_discarded;
11434 --c_inhibit_evaluation_warnings;
11437 else
11439 /* This if statement does not have an else clause. If
11440 NESTED_IF is true, then the then-clause has an if
11441 statement which does have an else clause. We warn
11442 about the potential ambiguity. */
11443 if (nested_if)
11444 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11445 "suggest explicit braces to avoid ambiguous"
11446 " %<else%>");
11447 if (warn_duplicated_cond)
11449 /* We don't need the condition chain anymore. */
11450 delete chain;
11451 chain = NULL;
11455 /* Now we're all done with the if-statement. */
11456 finish_if_stmt (statement);
11458 else
11460 bool in_switch_statement_p;
11461 unsigned char in_statement;
11463 /* Add the condition. */
11464 finish_switch_cond (condition, statement);
11466 /* Parse the body of the switch-statement. */
11467 in_switch_statement_p = parser->in_switch_statement_p;
11468 in_statement = parser->in_statement;
11469 parser->in_switch_statement_p = true;
11470 parser->in_statement |= IN_SWITCH_STMT;
11471 cp_parser_implicitly_scoped_statement (parser, if_p,
11472 guard_tinfo);
11473 parser->in_switch_statement_p = in_switch_statement_p;
11474 parser->in_statement = in_statement;
11476 /* Now we're all done with the switch-statement. */
11477 finish_switch_stmt (statement);
11480 return statement;
11482 break;
11484 default:
11485 cp_parser_error (parser, "expected selection-statement");
11486 return error_mark_node;
11490 /* Parse a condition.
11492 condition:
11493 expression
11494 type-specifier-seq declarator = initializer-clause
11495 type-specifier-seq declarator braced-init-list
11497 GNU Extension:
11499 condition:
11500 type-specifier-seq declarator asm-specification [opt]
11501 attributes [opt] = assignment-expression
11503 Returns the expression that should be tested. */
11505 static tree
11506 cp_parser_condition (cp_parser* parser)
11508 cp_decl_specifier_seq type_specifiers;
11509 const char *saved_message;
11510 int declares_class_or_enum;
11512 /* Try the declaration first. */
11513 cp_parser_parse_tentatively (parser);
11514 /* New types are not allowed in the type-specifier-seq for a
11515 condition. */
11516 saved_message = parser->type_definition_forbidden_message;
11517 parser->type_definition_forbidden_message
11518 = G_("types may not be defined in conditions");
11519 /* Parse the type-specifier-seq. */
11520 cp_parser_decl_specifier_seq (parser,
11521 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11522 &type_specifiers,
11523 &declares_class_or_enum);
11524 /* Restore the saved message. */
11525 parser->type_definition_forbidden_message = saved_message;
11526 /* If all is well, we might be looking at a declaration. */
11527 if (!cp_parser_error_occurred (parser))
11529 tree decl;
11530 tree asm_specification;
11531 tree attributes;
11532 cp_declarator *declarator;
11533 tree initializer = NULL_TREE;
11535 /* Parse the declarator. */
11536 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11537 /*ctor_dtor_or_conv_p=*/NULL,
11538 /*parenthesized_p=*/NULL,
11539 /*member_p=*/false,
11540 /*friend_p=*/false);
11541 /* Parse the attributes. */
11542 attributes = cp_parser_attributes_opt (parser);
11543 /* Parse the asm-specification. */
11544 asm_specification = cp_parser_asm_specification_opt (parser);
11545 /* If the next token is not an `=' or '{', then we might still be
11546 looking at an expression. For example:
11548 if (A(a).x)
11550 looks like a decl-specifier-seq and a declarator -- but then
11551 there is no `=', so this is an expression. */
11552 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11553 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11554 cp_parser_simulate_error (parser);
11556 /* If we did see an `=' or '{', then we are looking at a declaration
11557 for sure. */
11558 if (cp_parser_parse_definitely (parser))
11560 tree pushed_scope;
11561 bool non_constant_p;
11562 int flags = LOOKUP_ONLYCONVERTING;
11564 /* Create the declaration. */
11565 decl = start_decl (declarator, &type_specifiers,
11566 /*initialized_p=*/true,
11567 attributes, /*prefix_attributes=*/NULL_TREE,
11568 &pushed_scope);
11570 /* Parse the initializer. */
11571 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11573 initializer = cp_parser_braced_list (parser, &non_constant_p);
11574 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11575 flags = 0;
11577 else
11579 /* Consume the `='. */
11580 cp_parser_require (parser, CPP_EQ, RT_EQ);
11581 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11583 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11586 /* Process the initializer. */
11587 cp_finish_decl (decl,
11588 initializer, !non_constant_p,
11589 asm_specification,
11590 flags);
11592 if (pushed_scope)
11593 pop_scope (pushed_scope);
11595 return convert_from_reference (decl);
11598 /* If we didn't even get past the declarator successfully, we are
11599 definitely not looking at a declaration. */
11600 else
11601 cp_parser_abort_tentative_parse (parser);
11603 /* Otherwise, we are looking at an expression. */
11604 return cp_parser_expression (parser);
11607 /* Parses a for-statement or range-for-statement until the closing ')',
11608 not included. */
11610 static tree
11611 cp_parser_for (cp_parser *parser, bool ivdep)
11613 tree init, scope, decl;
11614 bool is_range_for;
11616 /* Begin the for-statement. */
11617 scope = begin_for_scope (&init);
11619 /* Parse the initialization. */
11620 is_range_for = cp_parser_init_statement (parser, &decl);
11622 if (is_range_for)
11623 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11624 else
11625 return cp_parser_c_for (parser, scope, init, ivdep);
11628 static tree
11629 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11631 /* Normal for loop */
11632 tree condition = NULL_TREE;
11633 tree expression = NULL_TREE;
11634 tree stmt;
11636 stmt = begin_for_stmt (scope, init);
11637 /* The init-statement has already been parsed in
11638 cp_parser_init_statement, so no work is needed here. */
11639 finish_init_stmt (stmt);
11641 /* If there's a condition, process it. */
11642 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11643 condition = cp_parser_condition (parser);
11644 else if (ivdep)
11646 cp_parser_error (parser, "missing loop condition in loop with "
11647 "%<GCC ivdep%> pragma");
11648 condition = error_mark_node;
11650 finish_for_cond (condition, stmt, ivdep);
11651 /* Look for the `;'. */
11652 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11654 /* If there's an expression, process it. */
11655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11656 expression = cp_parser_expression (parser);
11657 finish_for_expr (expression, stmt);
11659 return stmt;
11662 /* Tries to parse a range-based for-statement:
11664 range-based-for:
11665 decl-specifier-seq declarator : expression
11667 The decl-specifier-seq declarator and the `:' are already parsed by
11668 cp_parser_init_statement. If processing_template_decl it returns a
11669 newly created RANGE_FOR_STMT; if not, it is converted to a
11670 regular FOR_STMT. */
11672 static tree
11673 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11674 bool ivdep)
11676 tree stmt, range_expr;
11677 auto_vec <cxx_binding *, 16> bindings;
11678 auto_vec <tree, 16> names;
11679 tree decomp_first_name = NULL_TREE;
11680 unsigned int decomp_cnt = 0;
11682 /* Get the range declaration momentarily out of the way so that
11683 the range expression doesn't clash with it. */
11684 if (range_decl != error_mark_node)
11686 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11688 tree v = DECL_VALUE_EXPR (range_decl);
11689 /* For decomposition declaration get all of the corresponding
11690 declarations out of the way. */
11691 if (TREE_CODE (v) == ARRAY_REF
11692 && VAR_P (TREE_OPERAND (v, 0))
11693 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11695 tree d = range_decl;
11696 range_decl = TREE_OPERAND (v, 0);
11697 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11698 decomp_first_name = d;
11699 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11701 tree name = DECL_NAME (d);
11702 names.safe_push (name);
11703 bindings.safe_push (IDENTIFIER_BINDING (name));
11704 IDENTIFIER_BINDING (name)
11705 = IDENTIFIER_BINDING (name)->previous;
11709 if (names.is_empty ())
11711 tree name = DECL_NAME (range_decl);
11712 names.safe_push (name);
11713 bindings.safe_push (IDENTIFIER_BINDING (name));
11714 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11720 bool expr_non_constant_p;
11721 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11723 else
11724 range_expr = cp_parser_expression (parser);
11726 /* Put the range declaration(s) back into scope. */
11727 for (unsigned int i = 0; i < names.length (); i++)
11729 cxx_binding *binding = bindings[i];
11730 binding->previous = IDENTIFIER_BINDING (names[i]);
11731 IDENTIFIER_BINDING (names[i]) = binding;
11734 /* If in template, STMT is converted to a normal for-statement
11735 at instantiation. If not, it is done just ahead. */
11736 if (processing_template_decl)
11738 if (check_for_bare_parameter_packs (range_expr))
11739 range_expr = error_mark_node;
11740 stmt = begin_range_for_stmt (scope, init);
11741 if (ivdep)
11742 RANGE_FOR_IVDEP (stmt) = 1;
11743 finish_range_for_decl (stmt, range_decl, range_expr);
11744 if (!type_dependent_expression_p (range_expr)
11745 /* do_auto_deduction doesn't mess with template init-lists. */
11746 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11747 do_range_for_auto_deduction (range_decl, range_expr);
11749 else
11751 stmt = begin_for_stmt (scope, init);
11752 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11753 decomp_first_name, decomp_cnt, ivdep);
11755 return stmt;
11758 /* Subroutine of cp_convert_range_for: given the initializer expression,
11759 builds up the range temporary. */
11761 static tree
11762 build_range_temp (tree range_expr)
11764 tree range_type, range_temp;
11766 /* Find out the type deduced by the declaration
11767 `auto &&__range = range_expr'. */
11768 range_type = cp_build_reference_type (make_auto (), true);
11769 range_type = do_auto_deduction (range_type, range_expr,
11770 type_uses_auto (range_type));
11772 /* Create the __range variable. */
11773 range_temp = build_decl (input_location, VAR_DECL,
11774 get_identifier ("__for_range"), range_type);
11775 TREE_USED (range_temp) = 1;
11776 DECL_ARTIFICIAL (range_temp) = 1;
11778 return range_temp;
11781 /* Used by cp_parser_range_for in template context: we aren't going to
11782 do a full conversion yet, but we still need to resolve auto in the
11783 type of the for-range-declaration if present. This is basically
11784 a shortcut version of cp_convert_range_for. */
11786 static void
11787 do_range_for_auto_deduction (tree decl, tree range_expr)
11789 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11790 if (auto_node)
11792 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11793 range_temp = convert_from_reference (build_range_temp (range_expr));
11794 iter_type = (cp_parser_perform_range_for_lookup
11795 (range_temp, &begin_dummy, &end_dummy));
11796 if (iter_type)
11798 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11799 iter_type);
11800 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11801 tf_warning_or_error);
11802 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11803 iter_decl, auto_node);
11808 /* Converts a range-based for-statement into a normal
11809 for-statement, as per the definition.
11811 for (RANGE_DECL : RANGE_EXPR)
11812 BLOCK
11814 should be equivalent to:
11817 auto &&__range = RANGE_EXPR;
11818 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11819 __begin != __end;
11820 ++__begin)
11822 RANGE_DECL = *__begin;
11823 BLOCK
11827 If RANGE_EXPR is an array:
11828 BEGIN_EXPR = __range
11829 END_EXPR = __range + ARRAY_SIZE(__range)
11830 Else if RANGE_EXPR has a member 'begin' or 'end':
11831 BEGIN_EXPR = __range.begin()
11832 END_EXPR = __range.end()
11833 Else:
11834 BEGIN_EXPR = begin(__range)
11835 END_EXPR = end(__range);
11837 If __range has a member 'begin' but not 'end', or vice versa, we must
11838 still use the second alternative (it will surely fail, however).
11839 When calling begin()/end() in the third alternative we must use
11840 argument dependent lookup, but always considering 'std' as an associated
11841 namespace. */
11843 tree
11844 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11845 tree decomp_first_name, unsigned int decomp_cnt,
11846 bool ivdep)
11848 tree begin, end;
11849 tree iter_type, begin_expr, end_expr;
11850 tree condition, expression;
11852 if (range_decl == error_mark_node || range_expr == error_mark_node)
11853 /* If an error happened previously do nothing or else a lot of
11854 unhelpful errors would be issued. */
11855 begin_expr = end_expr = iter_type = error_mark_node;
11856 else
11858 tree range_temp;
11860 if (VAR_P (range_expr)
11861 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11862 /* Can't bind a reference to an array of runtime bound. */
11863 range_temp = range_expr;
11864 else
11866 range_temp = build_range_temp (range_expr);
11867 pushdecl (range_temp);
11868 cp_finish_decl (range_temp, range_expr,
11869 /*is_constant_init*/false, NULL_TREE,
11870 LOOKUP_ONLYCONVERTING);
11871 range_temp = convert_from_reference (range_temp);
11873 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11874 &begin_expr, &end_expr);
11877 /* The new for initialization statement. */
11878 begin = build_decl (input_location, VAR_DECL,
11879 get_identifier ("__for_begin"), iter_type);
11880 TREE_USED (begin) = 1;
11881 DECL_ARTIFICIAL (begin) = 1;
11882 pushdecl (begin);
11883 cp_finish_decl (begin, begin_expr,
11884 /*is_constant_init*/false, NULL_TREE,
11885 LOOKUP_ONLYCONVERTING);
11887 if (cxx_dialect >= cxx1z)
11888 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11889 end = build_decl (input_location, VAR_DECL,
11890 get_identifier ("__for_end"), iter_type);
11891 TREE_USED (end) = 1;
11892 DECL_ARTIFICIAL (end) = 1;
11893 pushdecl (end);
11894 cp_finish_decl (end, end_expr,
11895 /*is_constant_init*/false, NULL_TREE,
11896 LOOKUP_ONLYCONVERTING);
11898 finish_init_stmt (statement);
11900 /* The new for condition. */
11901 condition = build_x_binary_op (input_location, NE_EXPR,
11902 begin, ERROR_MARK,
11903 end, ERROR_MARK,
11904 NULL, tf_warning_or_error);
11905 finish_for_cond (condition, statement, ivdep);
11907 /* The new increment expression. */
11908 expression = finish_unary_op_expr (input_location,
11909 PREINCREMENT_EXPR, begin,
11910 tf_warning_or_error);
11911 finish_for_expr (expression, statement);
11913 /* The declaration is initialized with *__begin inside the loop body. */
11914 cp_finish_decl (range_decl,
11915 build_x_indirect_ref (input_location, begin, RO_NULL,
11916 tf_warning_or_error),
11917 /*is_constant_init*/false, NULL_TREE,
11918 LOOKUP_ONLYCONVERTING);
11919 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11920 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11922 return statement;
11925 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11926 We need to solve both at the same time because the method used
11927 depends on the existence of members begin or end.
11928 Returns the type deduced for the iterator expression. */
11930 static tree
11931 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11933 if (error_operand_p (range))
11935 *begin = *end = error_mark_node;
11936 return error_mark_node;
11939 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11941 error ("range-based %<for%> expression of type %qT "
11942 "has incomplete type", TREE_TYPE (range));
11943 *begin = *end = error_mark_node;
11944 return error_mark_node;
11946 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11948 /* If RANGE is an array, we will use pointer arithmetic. */
11949 *begin = decay_conversion (range, tf_warning_or_error);
11950 *end = build_binary_op (input_location, PLUS_EXPR,
11951 range,
11952 array_type_nelts_top (TREE_TYPE (range)),
11953 false);
11954 return TREE_TYPE (*begin);
11956 else
11958 /* If it is not an array, we must do a bit of magic. */
11959 tree id_begin, id_end;
11960 tree member_begin, member_end;
11962 *begin = *end = error_mark_node;
11964 id_begin = get_identifier ("begin");
11965 id_end = get_identifier ("end");
11966 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11967 /*protect=*/2, /*want_type=*/false,
11968 tf_warning_or_error);
11969 member_end = lookup_member (TREE_TYPE (range), id_end,
11970 /*protect=*/2, /*want_type=*/false,
11971 tf_warning_or_error);
11973 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11975 /* Use the member functions. */
11976 if (member_begin != NULL_TREE)
11977 *begin = cp_parser_range_for_member_function (range, id_begin);
11978 else
11979 error ("range-based %<for%> expression of type %qT has an "
11980 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11982 if (member_end != NULL_TREE)
11983 *end = cp_parser_range_for_member_function (range, id_end);
11984 else
11985 error ("range-based %<for%> expression of type %qT has a "
11986 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11988 else
11990 /* Use global functions with ADL. */
11991 vec<tree, va_gc> *vec;
11992 vec = make_tree_vector ();
11994 vec_safe_push (vec, range);
11996 member_begin = perform_koenig_lookup (id_begin, vec,
11997 tf_warning_or_error);
11998 *begin = finish_call_expr (member_begin, &vec, false, true,
11999 tf_warning_or_error);
12000 member_end = perform_koenig_lookup (id_end, vec,
12001 tf_warning_or_error);
12002 *end = finish_call_expr (member_end, &vec, false, true,
12003 tf_warning_or_error);
12005 release_tree_vector (vec);
12008 /* Last common checks. */
12009 if (*begin == error_mark_node || *end == error_mark_node)
12011 /* If one of the expressions is an error do no more checks. */
12012 *begin = *end = error_mark_node;
12013 return error_mark_node;
12015 else if (type_dependent_expression_p (*begin)
12016 || type_dependent_expression_p (*end))
12017 /* Can happen, when, eg, in a template context, Koenig lookup
12018 can't resolve begin/end (c++/58503). */
12019 return NULL_TREE;
12020 else
12022 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12023 /* The unqualified type of the __begin and __end temporaries should
12024 be the same, as required by the multiple auto declaration. */
12025 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12027 if (cxx_dialect >= cxx1z
12028 && (build_x_binary_op (input_location, NE_EXPR,
12029 *begin, ERROR_MARK,
12030 *end, ERROR_MARK,
12031 NULL, tf_none)
12032 != error_mark_node))
12033 /* P0184R0 allows __begin and __end to have different types,
12034 but make sure they are comparable so we can give a better
12035 diagnostic. */;
12036 else
12037 error ("inconsistent begin/end types in range-based %<for%> "
12038 "statement: %qT and %qT",
12039 TREE_TYPE (*begin), TREE_TYPE (*end));
12041 return iter_type;
12046 /* Helper function for cp_parser_perform_range_for_lookup.
12047 Builds a tree for RANGE.IDENTIFIER(). */
12049 static tree
12050 cp_parser_range_for_member_function (tree range, tree identifier)
12052 tree member, res;
12053 vec<tree, va_gc> *vec;
12055 member = finish_class_member_access_expr (range, identifier,
12056 false, tf_warning_or_error);
12057 if (member == error_mark_node)
12058 return error_mark_node;
12060 vec = make_tree_vector ();
12061 res = finish_call_expr (member, &vec,
12062 /*disallow_virtual=*/false,
12063 /*koenig_p=*/false,
12064 tf_warning_or_error);
12065 release_tree_vector (vec);
12066 return res;
12069 /* Parse an iteration-statement.
12071 iteration-statement:
12072 while ( condition ) statement
12073 do statement while ( expression ) ;
12074 for ( init-statement condition [opt] ; expression [opt] )
12075 statement
12077 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12079 static tree
12080 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12082 cp_token *token;
12083 enum rid keyword;
12084 tree statement;
12085 unsigned char in_statement;
12086 token_indent_info guard_tinfo;
12088 /* Peek at the next token. */
12089 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12090 if (!token)
12091 return error_mark_node;
12093 guard_tinfo = get_token_indent_info (token);
12095 /* Remember whether or not we are already within an iteration
12096 statement. */
12097 in_statement = parser->in_statement;
12099 /* See what kind of keyword it is. */
12100 keyword = token->keyword;
12101 switch (keyword)
12103 case RID_WHILE:
12105 tree condition;
12107 /* Begin the while-statement. */
12108 statement = begin_while_stmt ();
12109 /* Look for the `('. */
12110 matching_parens parens;
12111 parens.require_open (parser);
12112 /* Parse the condition. */
12113 condition = cp_parser_condition (parser);
12114 finish_while_stmt_cond (condition, statement, ivdep);
12115 /* Look for the `)'. */
12116 parens.require_close (parser);
12117 /* Parse the dependent statement. */
12118 parser->in_statement = IN_ITERATION_STMT;
12119 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12120 parser->in_statement = in_statement;
12121 /* We're done with the while-statement. */
12122 finish_while_stmt (statement);
12124 break;
12126 case RID_DO:
12128 tree expression;
12130 /* Begin the do-statement. */
12131 statement = begin_do_stmt ();
12132 /* Parse the body of the do-statement. */
12133 parser->in_statement = IN_ITERATION_STMT;
12134 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12135 parser->in_statement = in_statement;
12136 finish_do_body (statement);
12137 /* Look for the `while' keyword. */
12138 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12139 /* Look for the `('. */
12140 matching_parens parens;
12141 parens.require_open (parser);
12142 /* Parse the expression. */
12143 expression = cp_parser_expression (parser);
12144 /* We're done with the do-statement. */
12145 finish_do_stmt (expression, statement, ivdep);
12146 /* Look for the `)'. */
12147 parens.require_close (parser);
12148 /* Look for the `;'. */
12149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12151 break;
12153 case RID_FOR:
12155 /* Look for the `('. */
12156 matching_parens parens;
12157 parens.require_open (parser);
12159 statement = cp_parser_for (parser, ivdep);
12161 /* Look for the `)'. */
12162 parens.require_close (parser);
12164 /* Parse the body of the for-statement. */
12165 parser->in_statement = IN_ITERATION_STMT;
12166 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12167 parser->in_statement = in_statement;
12169 /* We're done with the for-statement. */
12170 finish_for_stmt (statement);
12172 break;
12174 default:
12175 cp_parser_error (parser, "expected iteration-statement");
12176 statement = error_mark_node;
12177 break;
12180 return statement;
12183 /* Parse a init-statement or the declarator of a range-based-for.
12184 Returns true if a range-based-for declaration is seen.
12186 init-statement:
12187 expression-statement
12188 simple-declaration */
12190 static bool
12191 cp_parser_init_statement (cp_parser* parser, tree *decl)
12193 /* If the next token is a `;', then we have an empty
12194 expression-statement. Grammatically, this is also a
12195 simple-declaration, but an invalid one, because it does not
12196 declare anything. Therefore, if we did not handle this case
12197 specially, we would issue an error message about an invalid
12198 declaration. */
12199 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12201 bool is_range_for = false;
12202 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12204 /* A colon is used in range-based for. */
12205 parser->colon_corrects_to_scope_p = false;
12207 /* We're going to speculatively look for a declaration, falling back
12208 to an expression, if necessary. */
12209 cp_parser_parse_tentatively (parser);
12210 /* Parse the declaration. */
12211 cp_parser_simple_declaration (parser,
12212 /*function_definition_allowed_p=*/false,
12213 decl);
12214 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12215 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12217 /* It is a range-for, consume the ':' */
12218 cp_lexer_consume_token (parser->lexer);
12219 is_range_for = true;
12220 if (cxx_dialect < cxx11)
12222 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12223 "range-based %<for%> loops only available with "
12224 "-std=c++11 or -std=gnu++11");
12225 *decl = error_mark_node;
12228 else
12229 /* The ';' is not consumed yet because we told
12230 cp_parser_simple_declaration not to. */
12231 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12233 if (cp_parser_parse_definitely (parser))
12234 return is_range_for;
12235 /* If the tentative parse failed, then we shall need to look for an
12236 expression-statement. */
12238 /* If we are here, it is an expression-statement. */
12239 cp_parser_expression_statement (parser, NULL_TREE);
12240 return false;
12243 /* Parse a jump-statement.
12245 jump-statement:
12246 break ;
12247 continue ;
12248 return expression [opt] ;
12249 return braced-init-list ;
12250 goto identifier ;
12252 GNU extension:
12254 jump-statement:
12255 goto * expression ;
12257 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12259 static tree
12260 cp_parser_jump_statement (cp_parser* parser)
12262 tree statement = error_mark_node;
12263 cp_token *token;
12264 enum rid keyword;
12265 unsigned char in_statement;
12267 /* Peek at the next token. */
12268 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12269 if (!token)
12270 return error_mark_node;
12272 /* See what kind of keyword it is. */
12273 keyword = token->keyword;
12274 switch (keyword)
12276 case RID_BREAK:
12277 in_statement = parser->in_statement & ~IN_IF_STMT;
12278 switch (in_statement)
12280 case 0:
12281 error_at (token->location, "break statement not within loop or switch");
12282 break;
12283 default:
12284 gcc_assert ((in_statement & IN_SWITCH_STMT)
12285 || in_statement == IN_ITERATION_STMT);
12286 statement = finish_break_stmt ();
12287 if (in_statement == IN_ITERATION_STMT)
12288 break_maybe_infinite_loop ();
12289 break;
12290 case IN_OMP_BLOCK:
12291 error_at (token->location, "invalid exit from OpenMP structured block");
12292 break;
12293 case IN_OMP_FOR:
12294 error_at (token->location, "break statement used with OpenMP for loop");
12295 break;
12296 case IN_CILK_SIMD_FOR:
12297 error_at (token->location, "break statement used with Cilk Plus for loop");
12298 break;
12300 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12301 break;
12303 case RID_CONTINUE:
12304 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12306 case 0:
12307 error_at (token->location, "continue statement not within a loop");
12308 break;
12309 case IN_CILK_SIMD_FOR:
12310 error_at (token->location,
12311 "continue statement within %<#pragma simd%> loop body");
12312 /* Fall through. */
12313 case IN_ITERATION_STMT:
12314 case IN_OMP_FOR:
12315 statement = finish_continue_stmt ();
12316 break;
12317 case IN_OMP_BLOCK:
12318 error_at (token->location, "invalid exit from OpenMP structured block");
12319 break;
12320 default:
12321 gcc_unreachable ();
12323 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12324 break;
12326 case RID_RETURN:
12328 tree expr;
12329 bool expr_non_constant_p;
12331 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12333 cp_lexer_set_source_position (parser->lexer);
12334 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12335 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12337 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12338 expr = cp_parser_expression (parser);
12339 else
12340 /* If the next token is a `;', then there is no
12341 expression. */
12342 expr = NULL_TREE;
12343 /* Build the return-statement. */
12344 if (current_function_auto_return_pattern && in_discarded_stmt)
12345 /* Don't deduce from a discarded return statement. */;
12346 else
12347 statement = finish_return_stmt (expr);
12348 /* Look for the final `;'. */
12349 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12351 break;
12353 case RID_GOTO:
12354 if (parser->in_function_body
12355 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12357 error ("%<goto%> in %<constexpr%> function");
12358 cp_function_chain->invalid_constexpr = true;
12361 /* Create the goto-statement. */
12362 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12364 /* Issue a warning about this use of a GNU extension. */
12365 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12366 /* Consume the '*' token. */
12367 cp_lexer_consume_token (parser->lexer);
12368 /* Parse the dependent expression. */
12369 finish_goto_stmt (cp_parser_expression (parser));
12371 else
12372 finish_goto_stmt (cp_parser_identifier (parser));
12373 /* Look for the final `;'. */
12374 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12375 break;
12377 default:
12378 cp_parser_error (parser, "expected jump-statement");
12379 break;
12382 return statement;
12385 /* Parse a declaration-statement.
12387 declaration-statement:
12388 block-declaration */
12390 static void
12391 cp_parser_declaration_statement (cp_parser* parser)
12393 void *p;
12395 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12396 p = obstack_alloc (&declarator_obstack, 0);
12398 /* Parse the block-declaration. */
12399 cp_parser_block_declaration (parser, /*statement_p=*/true);
12401 /* Free any declarators allocated. */
12402 obstack_free (&declarator_obstack, p);
12405 /* Some dependent statements (like `if (cond) statement'), are
12406 implicitly in their own scope. In other words, if the statement is
12407 a single statement (as opposed to a compound-statement), it is
12408 none-the-less treated as if it were enclosed in braces. Any
12409 declarations appearing in the dependent statement are out of scope
12410 after control passes that point. This function parses a statement,
12411 but ensures that is in its own scope, even if it is not a
12412 compound-statement.
12414 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12415 is a (possibly labeled) if statement which is not enclosed in
12416 braces and has an else clause. This is used to implement
12417 -Wparentheses.
12419 CHAIN is a vector of if-else-if conditions. This is used to implement
12420 -Wduplicated-cond.
12422 Returns the new statement. */
12424 static tree
12425 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12426 const token_indent_info &guard_tinfo,
12427 vec<tree> *chain)
12429 tree statement;
12430 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12431 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12432 token_indent_info body_tinfo
12433 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12435 if (if_p != NULL)
12436 *if_p = false;
12438 /* Mark if () ; with a special NOP_EXPR. */
12439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12441 cp_lexer_consume_token (parser->lexer);
12442 statement = add_stmt (build_empty_stmt (body_loc));
12444 if (guard_tinfo.keyword == RID_IF
12445 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12446 warning_at (body_loc, OPT_Wempty_body,
12447 "suggest braces around empty body in an %<if%> statement");
12448 else if (guard_tinfo.keyword == RID_ELSE)
12449 warning_at (body_loc, OPT_Wempty_body,
12450 "suggest braces around empty body in an %<else%> statement");
12452 /* if a compound is opened, we simply parse the statement directly. */
12453 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12454 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12455 /* If the token is not a `{', then we must take special action. */
12456 else
12458 /* Create a compound-statement. */
12459 statement = begin_compound_stmt (0);
12460 /* Parse the dependent-statement. */
12461 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12462 &body_loc_after_labels);
12463 /* Finish the dummy compound-statement. */
12464 finish_compound_stmt (statement);
12467 token_indent_info next_tinfo
12468 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12469 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12471 if (body_loc_after_labels != UNKNOWN_LOCATION
12472 && next_tinfo.type != CPP_SEMICOLON)
12473 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12474 guard_tinfo.location, guard_tinfo.keyword);
12476 /* Return the statement. */
12477 return statement;
12480 /* For some dependent statements (like `while (cond) statement'), we
12481 have already created a scope. Therefore, even if the dependent
12482 statement is a compound-statement, we do not want to create another
12483 scope. */
12485 static void
12486 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12487 const token_indent_info &guard_tinfo)
12489 /* If the token is a `{', then we must take special action. */
12490 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12492 token_indent_info body_tinfo
12493 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12494 location_t loc_after_labels = UNKNOWN_LOCATION;
12496 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12497 &loc_after_labels);
12498 token_indent_info next_tinfo
12499 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12500 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12502 if (loc_after_labels != UNKNOWN_LOCATION
12503 && next_tinfo.type != CPP_SEMICOLON)
12504 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12505 guard_tinfo.location,
12506 guard_tinfo.keyword);
12508 else
12510 /* Avoid calling cp_parser_compound_statement, so that we
12511 don't create a new scope. Do everything else by hand. */
12512 matching_braces braces;
12513 braces.require_open (parser);
12514 /* If the next keyword is `__label__' we have a label declaration. */
12515 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12516 cp_parser_label_declaration (parser);
12517 /* Parse an (optional) statement-seq. */
12518 cp_parser_statement_seq_opt (parser, NULL_TREE);
12519 braces.require_close (parser);
12523 /* Declarations [gram.dcl.dcl] */
12525 /* Parse an optional declaration-sequence.
12527 declaration-seq:
12528 declaration
12529 declaration-seq declaration */
12531 static void
12532 cp_parser_declaration_seq_opt (cp_parser* parser)
12534 while (true)
12536 cp_token *token;
12538 token = cp_lexer_peek_token (parser->lexer);
12540 if (token->type == CPP_CLOSE_BRACE
12541 || token->type == CPP_EOF
12542 || token->type == CPP_PRAGMA_EOL)
12543 break;
12545 if (token->type == CPP_SEMICOLON)
12547 /* A declaration consisting of a single semicolon is
12548 invalid. Allow it unless we're being pedantic. */
12549 cp_lexer_consume_token (parser->lexer);
12550 if (!in_system_header_at (input_location))
12551 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12552 continue;
12555 /* If we're entering or exiting a region that's implicitly
12556 extern "C", modify the lang context appropriately. */
12557 if (!parser->implicit_extern_c && token->implicit_extern_c)
12559 push_lang_context (lang_name_c);
12560 parser->implicit_extern_c = true;
12562 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12564 pop_lang_context ();
12565 parser->implicit_extern_c = false;
12568 if (token->type == CPP_PRAGMA)
12570 /* A top-level declaration can consist solely of a #pragma.
12571 A nested declaration cannot, so this is done here and not
12572 in cp_parser_declaration. (A #pragma at block scope is
12573 handled in cp_parser_statement.) */
12574 cp_parser_pragma (parser, pragma_external, NULL);
12575 continue;
12578 /* Parse the declaration itself. */
12579 cp_parser_declaration (parser);
12583 /* Parse a declaration.
12585 declaration:
12586 block-declaration
12587 function-definition
12588 template-declaration
12589 explicit-instantiation
12590 explicit-specialization
12591 linkage-specification
12592 namespace-definition
12594 C++17:
12595 deduction-guide
12597 GNU extension:
12599 declaration:
12600 __extension__ declaration */
12602 static void
12603 cp_parser_declaration (cp_parser* parser)
12605 cp_token token1;
12606 cp_token token2;
12607 int saved_pedantic;
12608 void *p;
12609 tree attributes = NULL_TREE;
12611 /* Check for the `__extension__' keyword. */
12612 if (cp_parser_extension_opt (parser, &saved_pedantic))
12614 /* Parse the qualified declaration. */
12615 cp_parser_declaration (parser);
12616 /* Restore the PEDANTIC flag. */
12617 pedantic = saved_pedantic;
12619 return;
12622 /* Try to figure out what kind of declaration is present. */
12623 token1 = *cp_lexer_peek_token (parser->lexer);
12625 if (token1.type != CPP_EOF)
12626 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12627 else
12629 token2.type = CPP_EOF;
12630 token2.keyword = RID_MAX;
12633 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12634 p = obstack_alloc (&declarator_obstack, 0);
12636 /* If the next token is `extern' and the following token is a string
12637 literal, then we have a linkage specification. */
12638 if (token1.keyword == RID_EXTERN
12639 && cp_parser_is_pure_string_literal (&token2))
12640 cp_parser_linkage_specification (parser);
12641 /* If the next token is `template', then we have either a template
12642 declaration, an explicit instantiation, or an explicit
12643 specialization. */
12644 else if (token1.keyword == RID_TEMPLATE)
12646 /* `template <>' indicates a template specialization. */
12647 if (token2.type == CPP_LESS
12648 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12649 cp_parser_explicit_specialization (parser);
12650 /* `template <' indicates a template declaration. */
12651 else if (token2.type == CPP_LESS)
12652 cp_parser_template_declaration (parser, /*member_p=*/false);
12653 /* Anything else must be an explicit instantiation. */
12654 else
12655 cp_parser_explicit_instantiation (parser);
12657 /* If the next token is `export', then we have a template
12658 declaration. */
12659 else if (token1.keyword == RID_EXPORT)
12660 cp_parser_template_declaration (parser, /*member_p=*/false);
12661 /* If the next token is `extern', 'static' or 'inline' and the one
12662 after that is `template', we have a GNU extended explicit
12663 instantiation directive. */
12664 else if (cp_parser_allow_gnu_extensions_p (parser)
12665 && (token1.keyword == RID_EXTERN
12666 || token1.keyword == RID_STATIC
12667 || token1.keyword == RID_INLINE)
12668 && token2.keyword == RID_TEMPLATE)
12669 cp_parser_explicit_instantiation (parser);
12670 /* If the next token is `namespace', check for a named or unnamed
12671 namespace definition. */
12672 else if (token1.keyword == RID_NAMESPACE
12673 && (/* A named namespace definition. */
12674 (token2.type == CPP_NAME
12675 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12676 != CPP_EQ))
12677 || (token2.type == CPP_OPEN_SQUARE
12678 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12679 == CPP_OPEN_SQUARE)
12680 /* An unnamed namespace definition. */
12681 || token2.type == CPP_OPEN_BRACE
12682 || token2.keyword == RID_ATTRIBUTE))
12683 cp_parser_namespace_definition (parser);
12684 /* An inline (associated) namespace definition. */
12685 else if (token1.keyword == RID_INLINE
12686 && token2.keyword == RID_NAMESPACE)
12687 cp_parser_namespace_definition (parser);
12688 /* Objective-C++ declaration/definition. */
12689 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12690 cp_parser_objc_declaration (parser, NULL_TREE);
12691 else if (c_dialect_objc ()
12692 && token1.keyword == RID_ATTRIBUTE
12693 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12694 cp_parser_objc_declaration (parser, attributes);
12695 /* At this point we may have a template declared by a concept
12696 introduction. */
12697 else if (flag_concepts
12698 && cp_parser_template_declaration_after_export (parser,
12699 /*member_p=*/false))
12700 /* We did. */;
12701 else
12702 /* Try to parse a block-declaration, or a function-definition. */
12703 cp_parser_block_declaration (parser, /*statement_p=*/false);
12705 /* Free any declarators allocated. */
12706 obstack_free (&declarator_obstack, p);
12709 /* Parse a block-declaration.
12711 block-declaration:
12712 simple-declaration
12713 asm-definition
12714 namespace-alias-definition
12715 using-declaration
12716 using-directive
12718 GNU Extension:
12720 block-declaration:
12721 __extension__ block-declaration
12723 C++0x Extension:
12725 block-declaration:
12726 static_assert-declaration
12728 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12729 part of a declaration-statement. */
12731 static void
12732 cp_parser_block_declaration (cp_parser *parser,
12733 bool statement_p)
12735 cp_token *token1;
12736 int saved_pedantic;
12738 /* Check for the `__extension__' keyword. */
12739 if (cp_parser_extension_opt (parser, &saved_pedantic))
12741 /* Parse the qualified declaration. */
12742 cp_parser_block_declaration (parser, statement_p);
12743 /* Restore the PEDANTIC flag. */
12744 pedantic = saved_pedantic;
12746 return;
12749 /* Peek at the next token to figure out which kind of declaration is
12750 present. */
12751 token1 = cp_lexer_peek_token (parser->lexer);
12753 /* If the next keyword is `asm', we have an asm-definition. */
12754 if (token1->keyword == RID_ASM)
12756 if (statement_p)
12757 cp_parser_commit_to_tentative_parse (parser);
12758 cp_parser_asm_definition (parser);
12760 /* If the next keyword is `namespace', we have a
12761 namespace-alias-definition. */
12762 else if (token1->keyword == RID_NAMESPACE)
12763 cp_parser_namespace_alias_definition (parser);
12764 /* If the next keyword is `using', we have a
12765 using-declaration, a using-directive, or an alias-declaration. */
12766 else if (token1->keyword == RID_USING)
12768 cp_token *token2;
12770 if (statement_p)
12771 cp_parser_commit_to_tentative_parse (parser);
12772 /* If the token after `using' is `namespace', then we have a
12773 using-directive. */
12774 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12775 if (token2->keyword == RID_NAMESPACE)
12776 cp_parser_using_directive (parser);
12777 /* If the second token after 'using' is '=', then we have an
12778 alias-declaration. */
12779 else if (cxx_dialect >= cxx11
12780 && token2->type == CPP_NAME
12781 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12782 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12783 cp_parser_alias_declaration (parser);
12784 /* Otherwise, it's a using-declaration. */
12785 else
12786 cp_parser_using_declaration (parser,
12787 /*access_declaration_p=*/false);
12789 /* If the next keyword is `__label__' we have a misplaced label
12790 declaration. */
12791 else if (token1->keyword == RID_LABEL)
12793 cp_lexer_consume_token (parser->lexer);
12794 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12795 cp_parser_skip_to_end_of_statement (parser);
12796 /* If the next token is now a `;', consume it. */
12797 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12798 cp_lexer_consume_token (parser->lexer);
12800 /* If the next token is `static_assert' we have a static assertion. */
12801 else if (token1->keyword == RID_STATIC_ASSERT)
12802 cp_parser_static_assert (parser, /*member_p=*/false);
12803 /* Anything else must be a simple-declaration. */
12804 else
12805 cp_parser_simple_declaration (parser, !statement_p,
12806 /*maybe_range_for_decl*/NULL);
12809 /* Parse a simple-declaration.
12811 simple-declaration:
12812 decl-specifier-seq [opt] init-declarator-list [opt] ;
12813 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12814 brace-or-equal-initializer ;
12816 init-declarator-list:
12817 init-declarator
12818 init-declarator-list , init-declarator
12820 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12821 function-definition as a simple-declaration.
12823 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12824 parsed declaration if it is an uninitialized single declarator not followed
12825 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12826 if present, will not be consumed. */
12828 static void
12829 cp_parser_simple_declaration (cp_parser* parser,
12830 bool function_definition_allowed_p,
12831 tree *maybe_range_for_decl)
12833 cp_decl_specifier_seq decl_specifiers;
12834 int declares_class_or_enum;
12835 bool saw_declarator;
12836 location_t comma_loc = UNKNOWN_LOCATION;
12837 location_t init_loc = UNKNOWN_LOCATION;
12839 if (maybe_range_for_decl)
12840 *maybe_range_for_decl = NULL_TREE;
12842 /* Defer access checks until we know what is being declared; the
12843 checks for names appearing in the decl-specifier-seq should be
12844 done as if we were in the scope of the thing being declared. */
12845 push_deferring_access_checks (dk_deferred);
12847 /* Parse the decl-specifier-seq. We have to keep track of whether
12848 or not the decl-specifier-seq declares a named class or
12849 enumeration type, since that is the only case in which the
12850 init-declarator-list is allowed to be empty.
12852 [dcl.dcl]
12854 In a simple-declaration, the optional init-declarator-list can be
12855 omitted only when declaring a class or enumeration, that is when
12856 the decl-specifier-seq contains either a class-specifier, an
12857 elaborated-type-specifier, or an enum-specifier. */
12858 cp_parser_decl_specifier_seq (parser,
12859 CP_PARSER_FLAGS_OPTIONAL,
12860 &decl_specifiers,
12861 &declares_class_or_enum);
12862 /* We no longer need to defer access checks. */
12863 stop_deferring_access_checks ();
12865 /* In a block scope, a valid declaration must always have a
12866 decl-specifier-seq. By not trying to parse declarators, we can
12867 resolve the declaration/expression ambiguity more quickly. */
12868 if (!function_definition_allowed_p
12869 && !decl_specifiers.any_specifiers_p)
12871 cp_parser_error (parser, "expected declaration");
12872 goto done;
12875 /* If the next two tokens are both identifiers, the code is
12876 erroneous. The usual cause of this situation is code like:
12878 T t;
12880 where "T" should name a type -- but does not. */
12881 if (!decl_specifiers.any_type_specifiers_p
12882 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12884 /* If parsing tentatively, we should commit; we really are
12885 looking at a declaration. */
12886 cp_parser_commit_to_tentative_parse (parser);
12887 /* Give up. */
12888 goto done;
12891 /* If we have seen at least one decl-specifier, and the next token
12892 is not a parenthesis, then we must be looking at a declaration.
12893 (After "int (" we might be looking at a functional cast.) */
12894 if (decl_specifiers.any_specifiers_p
12895 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12896 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12897 && !cp_parser_error_occurred (parser))
12898 cp_parser_commit_to_tentative_parse (parser);
12900 /* Look for C++17 decomposition declaration. */
12901 for (size_t n = 1; ; n++)
12902 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12903 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12904 continue;
12905 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12906 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12907 && decl_specifiers.any_specifiers_p)
12909 tree decl
12910 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12911 maybe_range_for_decl,
12912 &init_loc);
12914 /* The next token should be either a `,' or a `;'. */
12915 cp_token *token = cp_lexer_peek_token (parser->lexer);
12916 /* If it's a `;', we are done. */
12917 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12918 goto finish;
12919 /* Anything else is an error. */
12920 else
12922 /* If we have already issued an error message we don't need
12923 to issue another one. */
12924 if ((decl != error_mark_node
12925 && DECL_INITIAL (decl) != error_mark_node)
12926 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12927 cp_parser_error (parser, "expected %<,%> or %<;%>");
12928 /* Skip tokens until we reach the end of the statement. */
12929 cp_parser_skip_to_end_of_statement (parser);
12930 /* If the next token is now a `;', consume it. */
12931 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12932 cp_lexer_consume_token (parser->lexer);
12933 goto done;
12936 else
12937 break;
12939 tree last_type;
12940 bool auto_specifier_p;
12941 /* NULL_TREE if both variable and function declaration are allowed,
12942 error_mark_node if function declaration are not allowed and
12943 a FUNCTION_DECL that should be diagnosed if it is followed by
12944 variable declarations. */
12945 tree auto_function_declaration;
12947 last_type = NULL_TREE;
12948 auto_specifier_p
12949 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12950 auto_function_declaration = NULL_TREE;
12952 /* Keep going until we hit the `;' at the end of the simple
12953 declaration. */
12954 saw_declarator = false;
12955 while (cp_lexer_next_token_is_not (parser->lexer,
12956 CPP_SEMICOLON))
12958 cp_token *token;
12959 bool function_definition_p;
12960 tree decl;
12961 tree auto_result = NULL_TREE;
12963 if (saw_declarator)
12965 /* If we are processing next declarator, comma is expected */
12966 token = cp_lexer_peek_token (parser->lexer);
12967 gcc_assert (token->type == CPP_COMMA);
12968 cp_lexer_consume_token (parser->lexer);
12969 if (maybe_range_for_decl)
12971 *maybe_range_for_decl = error_mark_node;
12972 if (comma_loc == UNKNOWN_LOCATION)
12973 comma_loc = token->location;
12976 else
12977 saw_declarator = true;
12979 /* Parse the init-declarator. */
12980 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12981 /*checks=*/NULL,
12982 function_definition_allowed_p,
12983 /*member_p=*/false,
12984 declares_class_or_enum,
12985 &function_definition_p,
12986 maybe_range_for_decl,
12987 &init_loc,
12988 &auto_result);
12989 /* If an error occurred while parsing tentatively, exit quickly.
12990 (That usually happens when in the body of a function; each
12991 statement is treated as a declaration-statement until proven
12992 otherwise.) */
12993 if (cp_parser_error_occurred (parser))
12994 goto done;
12996 if (auto_specifier_p && cxx_dialect >= cxx14)
12998 /* If the init-declarator-list contains more than one
12999 init-declarator, they shall all form declarations of
13000 variables. */
13001 if (auto_function_declaration == NULL_TREE)
13002 auto_function_declaration
13003 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13004 else if (TREE_CODE (decl) == FUNCTION_DECL
13005 || auto_function_declaration != error_mark_node)
13007 error_at (decl_specifiers.locations[ds_type_spec],
13008 "non-variable %qD in declaration with more than one "
13009 "declarator with placeholder type",
13010 TREE_CODE (decl) == FUNCTION_DECL
13011 ? decl : auto_function_declaration);
13012 auto_function_declaration = error_mark_node;
13016 if (auto_result
13017 && (!processing_template_decl || !type_uses_auto (auto_result)))
13019 if (last_type
13020 && last_type != error_mark_node
13021 && !same_type_p (auto_result, last_type))
13023 /* If the list of declarators contains more than one declarator,
13024 the type of each declared variable is determined as described
13025 above. If the type deduced for the template parameter U is not
13026 the same in each deduction, the program is ill-formed. */
13027 error_at (decl_specifiers.locations[ds_type_spec],
13028 "inconsistent deduction for %qT: %qT and then %qT",
13029 decl_specifiers.type, last_type, auto_result);
13030 last_type = error_mark_node;
13032 else
13033 last_type = auto_result;
13036 /* Handle function definitions specially. */
13037 if (function_definition_p)
13039 /* If the next token is a `,', then we are probably
13040 processing something like:
13042 void f() {}, *p;
13044 which is erroneous. */
13045 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13047 cp_token *token = cp_lexer_peek_token (parser->lexer);
13048 error_at (token->location,
13049 "mixing"
13050 " declarations and function-definitions is forbidden");
13052 /* Otherwise, we're done with the list of declarators. */
13053 else
13055 pop_deferring_access_checks ();
13056 return;
13059 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13060 *maybe_range_for_decl = decl;
13061 /* The next token should be either a `,' or a `;'. */
13062 token = cp_lexer_peek_token (parser->lexer);
13063 /* If it's a `,', there are more declarators to come. */
13064 if (token->type == CPP_COMMA)
13065 /* will be consumed next time around */;
13066 /* If it's a `;', we are done. */
13067 else if (token->type == CPP_SEMICOLON)
13068 break;
13069 else if (maybe_range_for_decl)
13071 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13072 permerror (decl_specifiers.locations[ds_type_spec],
13073 "types may not be defined in a for-range-declaration");
13074 break;
13076 /* Anything else is an error. */
13077 else
13079 /* If we have already issued an error message we don't need
13080 to issue another one. */
13081 if ((decl != error_mark_node
13082 && DECL_INITIAL (decl) != error_mark_node)
13083 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13084 cp_parser_error (parser, "expected %<,%> or %<;%>");
13085 /* Skip tokens until we reach the end of the statement. */
13086 cp_parser_skip_to_end_of_statement (parser);
13087 /* If the next token is now a `;', consume it. */
13088 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13089 cp_lexer_consume_token (parser->lexer);
13090 goto done;
13092 /* After the first time around, a function-definition is not
13093 allowed -- even if it was OK at first. For example:
13095 int i, f() {}
13097 is not valid. */
13098 function_definition_allowed_p = false;
13101 /* Issue an error message if no declarators are present, and the
13102 decl-specifier-seq does not itself declare a class or
13103 enumeration: [dcl.dcl]/3. */
13104 if (!saw_declarator)
13106 if (cp_parser_declares_only_class_p (parser))
13108 if (!declares_class_or_enum
13109 && decl_specifiers.type
13110 && OVERLOAD_TYPE_P (decl_specifiers.type))
13111 /* Ensure an error is issued anyway when finish_decltype_type,
13112 called via cp_parser_decl_specifier_seq, returns a class or
13113 an enumeration (c++/51786). */
13114 decl_specifiers.type = NULL_TREE;
13115 shadow_tag (&decl_specifiers);
13117 /* Perform any deferred access checks. */
13118 perform_deferred_access_checks (tf_warning_or_error);
13121 /* Consume the `;'. */
13122 finish:
13123 if (!maybe_range_for_decl)
13124 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13125 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13127 if (init_loc != UNKNOWN_LOCATION)
13128 error_at (init_loc, "initializer in range-based %<for%> loop");
13129 if (comma_loc != UNKNOWN_LOCATION)
13130 error_at (comma_loc,
13131 "multiple declarations in range-based %<for%> loop");
13134 done:
13135 pop_deferring_access_checks ();
13138 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13139 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13140 initializer ; */
13142 static tree
13143 cp_parser_decomposition_declaration (cp_parser *parser,
13144 cp_decl_specifier_seq *decl_specifiers,
13145 tree *maybe_range_for_decl,
13146 location_t *init_loc)
13148 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13149 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13150 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13152 /* Parse the identifier-list. */
13153 auto_vec<cp_expr, 10> v;
13154 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13155 while (true)
13157 cp_expr e = cp_parser_identifier (parser);
13158 if (e.get_value () == error_mark_node)
13159 break;
13160 v.safe_push (e);
13161 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13162 break;
13163 cp_lexer_consume_token (parser->lexer);
13166 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13167 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13169 end_loc = UNKNOWN_LOCATION;
13170 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13171 false);
13172 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13173 cp_lexer_consume_token (parser->lexer);
13174 else
13176 cp_parser_skip_to_end_of_statement (parser);
13177 return error_mark_node;
13181 if (cxx_dialect < cxx1z)
13182 pedwarn (loc, 0, "structured bindings only available with "
13183 "-std=c++1z or -std=gnu++1z");
13185 tree pushed_scope;
13186 cp_declarator *declarator = make_declarator (cdk_decomp);
13187 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13188 declarator->id_loc = loc;
13189 if (ref_qual != REF_QUAL_NONE)
13190 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13191 ref_qual == REF_QUAL_RVALUE,
13192 NULL_TREE);
13193 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13194 NULL_TREE, decl_specifiers->attributes,
13195 &pushed_scope);
13196 tree orig_decl = decl;
13198 unsigned int i;
13199 cp_expr e;
13200 cp_decl_specifier_seq decl_specs;
13201 clear_decl_specs (&decl_specs);
13202 decl_specs.type = make_auto ();
13203 tree prev = decl;
13204 FOR_EACH_VEC_ELT (v, i, e)
13206 if (i == 0)
13207 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13208 else
13209 declarator->u.id.unqualified_name = e.get_value ();
13210 declarator->id_loc = e.get_location ();
13211 tree elt_pushed_scope;
13212 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13213 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13214 if (decl2 == error_mark_node)
13215 decl = error_mark_node;
13216 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13218 /* Ensure we've diagnosed redeclaration if we aren't creating
13219 a new VAR_DECL. */
13220 gcc_assert (errorcount);
13221 decl = error_mark_node;
13223 else
13224 prev = decl2;
13225 if (elt_pushed_scope)
13226 pop_scope (elt_pushed_scope);
13229 if (v.is_empty ())
13231 error_at (loc, "empty structured binding declaration");
13232 decl = error_mark_node;
13235 if (maybe_range_for_decl == NULL
13236 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13238 bool non_constant_p = false, is_direct_init = false;
13239 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13240 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13241 &non_constant_p);
13242 if (initializer == NULL_TREE
13243 || (TREE_CODE (initializer) == TREE_LIST
13244 && TREE_CHAIN (initializer))
13245 || (TREE_CODE (initializer) == CONSTRUCTOR
13246 && CONSTRUCTOR_NELTS (initializer) != 1))
13248 error_at (loc, "invalid initializer for structured binding "
13249 "declaration");
13250 initializer = error_mark_node;
13253 if (decl != error_mark_node)
13255 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13256 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13257 cp_finish_decomp (decl, prev, v.length ());
13260 else if (decl != error_mark_node)
13262 *maybe_range_for_decl = prev;
13263 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13264 the underlying DECL. */
13265 cp_finish_decomp (decl, prev, v.length ());
13268 if (pushed_scope)
13269 pop_scope (pushed_scope);
13271 if (decl == error_mark_node && DECL_P (orig_decl))
13273 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13274 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13277 return decl;
13280 /* Parse a decl-specifier-seq.
13282 decl-specifier-seq:
13283 decl-specifier-seq [opt] decl-specifier
13284 decl-specifier attribute-specifier-seq [opt] (C++11)
13286 decl-specifier:
13287 storage-class-specifier
13288 type-specifier
13289 function-specifier
13290 friend
13291 typedef
13293 GNU Extension:
13295 decl-specifier:
13296 attributes
13298 Concepts Extension:
13300 decl-specifier:
13301 concept
13303 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13305 The parser flags FLAGS is used to control type-specifier parsing.
13307 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13308 flags:
13310 1: one of the decl-specifiers is an elaborated-type-specifier
13311 (i.e., a type declaration)
13312 2: one of the decl-specifiers is an enum-specifier or a
13313 class-specifier (i.e., a type definition)
13317 static void
13318 cp_parser_decl_specifier_seq (cp_parser* parser,
13319 cp_parser_flags flags,
13320 cp_decl_specifier_seq *decl_specs,
13321 int* declares_class_or_enum)
13323 bool constructor_possible_p = !parser->in_declarator_p;
13324 bool found_decl_spec = false;
13325 cp_token *start_token = NULL;
13326 cp_decl_spec ds;
13328 /* Clear DECL_SPECS. */
13329 clear_decl_specs (decl_specs);
13331 /* Assume no class or enumeration type is declared. */
13332 *declares_class_or_enum = 0;
13334 /* Keep reading specifiers until there are no more to read. */
13335 while (true)
13337 bool constructor_p;
13338 cp_token *token;
13339 ds = ds_last;
13341 /* Peek at the next token. */
13342 token = cp_lexer_peek_token (parser->lexer);
13344 /* Save the first token of the decl spec list for error
13345 reporting. */
13346 if (!start_token)
13347 start_token = token;
13348 /* Handle attributes. */
13349 if (cp_next_tokens_can_be_attribute_p (parser))
13351 /* Parse the attributes. */
13352 tree attrs = cp_parser_attributes_opt (parser);
13354 /* In a sequence of declaration specifiers, c++11 attributes
13355 appertain to the type that precede them. In that case
13356 [dcl.spec]/1 says:
13358 The attribute-specifier-seq affects the type only for
13359 the declaration it appears in, not other declarations
13360 involving the same type.
13362 But for now let's force the user to position the
13363 attribute either at the beginning of the declaration or
13364 after the declarator-id, which would clearly mean that it
13365 applies to the declarator. */
13366 if (cxx11_attribute_p (attrs))
13368 if (!found_decl_spec)
13369 /* The c++11 attribute is at the beginning of the
13370 declaration. It appertains to the entity being
13371 declared. */;
13372 else
13374 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13376 /* This is an attribute following a
13377 class-specifier. */
13378 if (decl_specs->type_definition_p)
13379 warn_misplaced_attr_for_class_type (token->location,
13380 decl_specs->type);
13381 attrs = NULL_TREE;
13383 else
13385 decl_specs->std_attributes
13386 = chainon (decl_specs->std_attributes,
13387 attrs);
13388 if (decl_specs->locations[ds_std_attribute] == 0)
13389 decl_specs->locations[ds_std_attribute] = token->location;
13391 continue;
13395 decl_specs->attributes
13396 = chainon (decl_specs->attributes,
13397 attrs);
13398 if (decl_specs->locations[ds_attribute] == 0)
13399 decl_specs->locations[ds_attribute] = token->location;
13400 continue;
13402 /* Assume we will find a decl-specifier keyword. */
13403 found_decl_spec = true;
13404 /* If the next token is an appropriate keyword, we can simply
13405 add it to the list. */
13406 switch (token->keyword)
13408 /* decl-specifier:
13409 friend
13410 constexpr */
13411 case RID_FRIEND:
13412 if (!at_class_scope_p ())
13414 gcc_rich_location richloc (token->location);
13415 richloc.add_fixit_remove ();
13416 error_at_rich_loc (&richloc, "%<friend%> used outside of class");
13417 cp_lexer_purge_token (parser->lexer);
13419 else
13421 ds = ds_friend;
13422 /* Consume the token. */
13423 cp_lexer_consume_token (parser->lexer);
13425 break;
13427 case RID_CONSTEXPR:
13428 ds = ds_constexpr;
13429 cp_lexer_consume_token (parser->lexer);
13430 break;
13432 case RID_CONCEPT:
13433 ds = ds_concept;
13434 cp_lexer_consume_token (parser->lexer);
13435 break;
13437 /* function-specifier:
13438 inline
13439 virtual
13440 explicit */
13441 case RID_INLINE:
13442 case RID_VIRTUAL:
13443 case RID_EXPLICIT:
13444 cp_parser_function_specifier_opt (parser, decl_specs);
13445 break;
13447 /* decl-specifier:
13448 typedef */
13449 case RID_TYPEDEF:
13450 ds = ds_typedef;
13451 /* Consume the token. */
13452 cp_lexer_consume_token (parser->lexer);
13453 /* A constructor declarator cannot appear in a typedef. */
13454 constructor_possible_p = false;
13455 /* The "typedef" keyword can only occur in a declaration; we
13456 may as well commit at this point. */
13457 cp_parser_commit_to_tentative_parse (parser);
13459 if (decl_specs->storage_class != sc_none)
13460 decl_specs->conflicting_specifiers_p = true;
13461 break;
13463 /* storage-class-specifier:
13464 auto
13465 register
13466 static
13467 extern
13468 mutable
13470 GNU Extension:
13471 thread */
13472 case RID_AUTO:
13473 if (cxx_dialect == cxx98)
13475 /* Consume the token. */
13476 cp_lexer_consume_token (parser->lexer);
13478 /* Complain about `auto' as a storage specifier, if
13479 we're complaining about C++0x compatibility. */
13480 gcc_rich_location richloc (token->location);
13481 richloc.add_fixit_remove ();
13482 warning_at_rich_loc (&richloc, OPT_Wc__11_compat,
13483 "%<auto%> changes meaning in C++11; "
13484 "please remove it");
13486 /* Set the storage class anyway. */
13487 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13488 token);
13490 else
13491 /* C++0x auto type-specifier. */
13492 found_decl_spec = false;
13493 break;
13495 case RID_REGISTER:
13496 case RID_STATIC:
13497 case RID_EXTERN:
13498 case RID_MUTABLE:
13499 /* Consume the token. */
13500 cp_lexer_consume_token (parser->lexer);
13501 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13502 token);
13503 break;
13504 case RID_THREAD:
13505 /* Consume the token. */
13506 ds = ds_thread;
13507 cp_lexer_consume_token (parser->lexer);
13508 break;
13510 default:
13511 /* We did not yet find a decl-specifier yet. */
13512 found_decl_spec = false;
13513 break;
13516 if (found_decl_spec
13517 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13518 && token->keyword != RID_CONSTEXPR)
13519 error ("decl-specifier invalid in condition");
13521 if (found_decl_spec
13522 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13523 && token->keyword != RID_MUTABLE
13524 && token->keyword != RID_CONSTEXPR)
13525 error_at (token->location, "%qD invalid in lambda",
13526 ridpointers[token->keyword]);
13528 if (ds != ds_last)
13529 set_and_check_decl_spec_loc (decl_specs, ds, token);
13531 /* Constructors are a special case. The `S' in `S()' is not a
13532 decl-specifier; it is the beginning of the declarator. */
13533 constructor_p
13534 = (!found_decl_spec
13535 && constructor_possible_p
13536 && (cp_parser_constructor_declarator_p
13537 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13539 /* If we don't have a DECL_SPEC yet, then we must be looking at
13540 a type-specifier. */
13541 if (!found_decl_spec && !constructor_p)
13543 int decl_spec_declares_class_or_enum;
13544 bool is_cv_qualifier;
13545 tree type_spec;
13547 type_spec
13548 = cp_parser_type_specifier (parser, flags,
13549 decl_specs,
13550 /*is_declaration=*/true,
13551 &decl_spec_declares_class_or_enum,
13552 &is_cv_qualifier);
13553 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13555 /* If this type-specifier referenced a user-defined type
13556 (a typedef, class-name, etc.), then we can't allow any
13557 more such type-specifiers henceforth.
13559 [dcl.spec]
13561 The longest sequence of decl-specifiers that could
13562 possibly be a type name is taken as the
13563 decl-specifier-seq of a declaration. The sequence shall
13564 be self-consistent as described below.
13566 [dcl.type]
13568 As a general rule, at most one type-specifier is allowed
13569 in the complete decl-specifier-seq of a declaration. The
13570 only exceptions are the following:
13572 -- const or volatile can be combined with any other
13573 type-specifier.
13575 -- signed or unsigned can be combined with char, long,
13576 short, or int.
13578 -- ..
13580 Example:
13582 typedef char* Pc;
13583 void g (const int Pc);
13585 Here, Pc is *not* part of the decl-specifier seq; it's
13586 the declarator. Therefore, once we see a type-specifier
13587 (other than a cv-qualifier), we forbid any additional
13588 user-defined types. We *do* still allow things like `int
13589 int' to be considered a decl-specifier-seq, and issue the
13590 error message later. */
13591 if (type_spec && !is_cv_qualifier)
13592 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13593 /* A constructor declarator cannot follow a type-specifier. */
13594 if (type_spec)
13596 constructor_possible_p = false;
13597 found_decl_spec = true;
13598 if (!is_cv_qualifier)
13599 decl_specs->any_type_specifiers_p = true;
13603 /* If we still do not have a DECL_SPEC, then there are no more
13604 decl-specifiers. */
13605 if (!found_decl_spec)
13606 break;
13608 decl_specs->any_specifiers_p = true;
13609 /* After we see one decl-specifier, further decl-specifiers are
13610 always optional. */
13611 flags |= CP_PARSER_FLAGS_OPTIONAL;
13614 /* Don't allow a friend specifier with a class definition. */
13615 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13616 && (*declares_class_or_enum & 2))
13617 error_at (decl_specs->locations[ds_friend],
13618 "class definition may not be declared a friend");
13621 /* Parse an (optional) storage-class-specifier.
13623 storage-class-specifier:
13624 auto
13625 register
13626 static
13627 extern
13628 mutable
13630 GNU Extension:
13632 storage-class-specifier:
13633 thread
13635 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13637 static tree
13638 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13640 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13642 case RID_AUTO:
13643 if (cxx_dialect != cxx98)
13644 return NULL_TREE;
13645 /* Fall through for C++98. */
13646 gcc_fallthrough ();
13648 case RID_REGISTER:
13649 case RID_STATIC:
13650 case RID_EXTERN:
13651 case RID_MUTABLE:
13652 case RID_THREAD:
13653 /* Consume the token. */
13654 return cp_lexer_consume_token (parser->lexer)->u.value;
13656 default:
13657 return NULL_TREE;
13661 /* Parse an (optional) function-specifier.
13663 function-specifier:
13664 inline
13665 virtual
13666 explicit
13668 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13669 Updates DECL_SPECS, if it is non-NULL. */
13671 static tree
13672 cp_parser_function_specifier_opt (cp_parser* parser,
13673 cp_decl_specifier_seq *decl_specs)
13675 cp_token *token = cp_lexer_peek_token (parser->lexer);
13676 switch (token->keyword)
13678 case RID_INLINE:
13679 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13680 break;
13682 case RID_VIRTUAL:
13683 /* 14.5.2.3 [temp.mem]
13685 A member function template shall not be virtual. */
13686 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13687 && current_class_type)
13688 error_at (token->location, "templates may not be %<virtual%>");
13689 else
13690 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13691 break;
13693 case RID_EXPLICIT:
13694 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13695 break;
13697 default:
13698 return NULL_TREE;
13701 /* Consume the token. */
13702 return cp_lexer_consume_token (parser->lexer)->u.value;
13705 /* Parse a linkage-specification.
13707 linkage-specification:
13708 extern string-literal { declaration-seq [opt] }
13709 extern string-literal declaration */
13711 static void
13712 cp_parser_linkage_specification (cp_parser* parser)
13714 tree linkage;
13716 /* Look for the `extern' keyword. */
13717 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13719 /* Look for the string-literal. */
13720 linkage = cp_parser_string_literal (parser, false, false);
13722 /* Transform the literal into an identifier. If the literal is a
13723 wide-character string, or contains embedded NULs, then we can't
13724 handle it as the user wants. */
13725 if (strlen (TREE_STRING_POINTER (linkage))
13726 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13728 cp_parser_error (parser, "invalid linkage-specification");
13729 /* Assume C++ linkage. */
13730 linkage = lang_name_cplusplus;
13732 else
13733 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13735 /* We're now using the new linkage. */
13736 push_lang_context (linkage);
13738 /* If the next token is a `{', then we're using the first
13739 production. */
13740 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13742 cp_ensure_no_omp_declare_simd (parser);
13743 cp_ensure_no_oacc_routine (parser);
13745 /* Consume the `{' token. */
13746 matching_braces braces;
13747 braces.consume_open (parser)->location;
13748 /* Parse the declarations. */
13749 cp_parser_declaration_seq_opt (parser);
13750 /* Look for the closing `}'. */
13751 braces.require_close (parser);
13753 /* Otherwise, there's just one declaration. */
13754 else
13756 bool saved_in_unbraced_linkage_specification_p;
13758 saved_in_unbraced_linkage_specification_p
13759 = parser->in_unbraced_linkage_specification_p;
13760 parser->in_unbraced_linkage_specification_p = true;
13761 cp_parser_declaration (parser);
13762 parser->in_unbraced_linkage_specification_p
13763 = saved_in_unbraced_linkage_specification_p;
13766 /* We're done with the linkage-specification. */
13767 pop_lang_context ();
13770 /* Parse a static_assert-declaration.
13772 static_assert-declaration:
13773 static_assert ( constant-expression , string-literal ) ;
13774 static_assert ( constant-expression ) ; (C++1Z)
13776 If MEMBER_P, this static_assert is a class member. */
13778 static void
13779 cp_parser_static_assert(cp_parser *parser, bool member_p)
13781 tree condition;
13782 tree message;
13783 cp_token *token;
13784 location_t saved_loc;
13785 bool dummy;
13787 /* Peek at the `static_assert' token so we can keep track of exactly
13788 where the static assertion started. */
13789 token = cp_lexer_peek_token (parser->lexer);
13790 saved_loc = token->location;
13792 /* Look for the `static_assert' keyword. */
13793 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13794 RT_STATIC_ASSERT))
13795 return;
13797 /* We know we are in a static assertion; commit to any tentative
13798 parse. */
13799 if (cp_parser_parsing_tentatively (parser))
13800 cp_parser_commit_to_tentative_parse (parser);
13802 /* Parse the `(' starting the static assertion condition. */
13803 matching_parens parens;
13804 parens.require_open (parser);
13806 /* Parse the constant-expression. Allow a non-constant expression
13807 here in order to give better diagnostics in finish_static_assert. */
13808 condition =
13809 cp_parser_constant_expression (parser,
13810 /*allow_non_constant_p=*/true,
13811 /*non_constant_p=*/&dummy);
13813 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13815 if (cxx_dialect < cxx1z)
13816 pedwarn (input_location, OPT_Wpedantic,
13817 "static_assert without a message "
13818 "only available with -std=c++1z or -std=gnu++1z");
13819 /* Eat the ')' */
13820 cp_lexer_consume_token (parser->lexer);
13821 message = build_string (1, "");
13822 TREE_TYPE (message) = char_array_type_node;
13823 fix_string_type (message);
13825 else
13827 /* Parse the separating `,'. */
13828 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13830 /* Parse the string-literal message. */
13831 message = cp_parser_string_literal (parser,
13832 /*translate=*/false,
13833 /*wide_ok=*/true);
13835 /* A `)' completes the static assertion. */
13836 if (!parens.require_close (parser))
13837 cp_parser_skip_to_closing_parenthesis (parser,
13838 /*recovering=*/true,
13839 /*or_comma=*/false,
13840 /*consume_paren=*/true);
13843 /* A semicolon terminates the declaration. */
13844 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13846 /* Complete the static assertion, which may mean either processing
13847 the static assert now or saving it for template instantiation. */
13848 finish_static_assert (condition, message, saved_loc, member_p);
13851 /* Parse the expression in decltype ( expression ). */
13853 static tree
13854 cp_parser_decltype_expr (cp_parser *parser,
13855 bool &id_expression_or_member_access_p)
13857 cp_token *id_expr_start_token;
13858 tree expr;
13860 /* Since we're going to preserve any side-effects from this parse, set up a
13861 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13862 in the expression. */
13863 tentative_firewall firewall (parser);
13865 /* First, try parsing an id-expression. */
13866 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13867 cp_parser_parse_tentatively (parser);
13868 expr = cp_parser_id_expression (parser,
13869 /*template_keyword_p=*/false,
13870 /*check_dependency_p=*/true,
13871 /*template_p=*/NULL,
13872 /*declarator_p=*/false,
13873 /*optional_p=*/false);
13875 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13877 bool non_integral_constant_expression_p = false;
13878 tree id_expression = expr;
13879 cp_id_kind idk;
13880 const char *error_msg;
13882 if (identifier_p (expr))
13883 /* Lookup the name we got back from the id-expression. */
13884 expr = cp_parser_lookup_name_simple (parser, expr,
13885 id_expr_start_token->location);
13887 if (expr
13888 && expr != error_mark_node
13889 && TREE_CODE (expr) != TYPE_DECL
13890 && (TREE_CODE (expr) != BIT_NOT_EXPR
13891 || !TYPE_P (TREE_OPERAND (expr, 0)))
13892 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13894 /* Complete lookup of the id-expression. */
13895 expr = (finish_id_expression
13896 (id_expression, expr, parser->scope, &idk,
13897 /*integral_constant_expression_p=*/false,
13898 /*allow_non_integral_constant_expression_p=*/true,
13899 &non_integral_constant_expression_p,
13900 /*template_p=*/false,
13901 /*done=*/true,
13902 /*address_p=*/false,
13903 /*template_arg_p=*/false,
13904 &error_msg,
13905 id_expr_start_token->location));
13907 if (expr == error_mark_node)
13908 /* We found an id-expression, but it was something that we
13909 should not have found. This is an error, not something
13910 we can recover from, so note that we found an
13911 id-expression and we'll recover as gracefully as
13912 possible. */
13913 id_expression_or_member_access_p = true;
13916 if (expr
13917 && expr != error_mark_node
13918 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13919 /* We have an id-expression. */
13920 id_expression_or_member_access_p = true;
13923 if (!id_expression_or_member_access_p)
13925 /* Abort the id-expression parse. */
13926 cp_parser_abort_tentative_parse (parser);
13928 /* Parsing tentatively, again. */
13929 cp_parser_parse_tentatively (parser);
13931 /* Parse a class member access. */
13932 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13933 /*cast_p=*/false, /*decltype*/true,
13934 /*member_access_only_p=*/true, NULL);
13936 if (expr
13937 && expr != error_mark_node
13938 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13939 /* We have an id-expression. */
13940 id_expression_or_member_access_p = true;
13943 if (id_expression_or_member_access_p)
13944 /* We have parsed the complete id-expression or member access. */
13945 cp_parser_parse_definitely (parser);
13946 else
13948 /* Abort our attempt to parse an id-expression or member access
13949 expression. */
13950 cp_parser_abort_tentative_parse (parser);
13952 /* Parse a full expression. */
13953 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13954 /*decltype_p=*/true);
13957 return expr;
13960 /* Parse a `decltype' type. Returns the type.
13962 simple-type-specifier:
13963 decltype ( expression )
13964 C++14 proposal:
13965 decltype ( auto ) */
13967 static tree
13968 cp_parser_decltype (cp_parser *parser)
13970 tree expr;
13971 bool id_expression_or_member_access_p = false;
13972 const char *saved_message;
13973 bool saved_integral_constant_expression_p;
13974 bool saved_non_integral_constant_expression_p;
13975 bool saved_greater_than_is_operator_p;
13976 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13978 if (start_token->type == CPP_DECLTYPE)
13980 /* Already parsed. */
13981 cp_lexer_consume_token (parser->lexer);
13982 return saved_checks_value (start_token->u.tree_check_value);
13985 /* Look for the `decltype' token. */
13986 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13987 return error_mark_node;
13989 /* Parse the opening `('. */
13990 matching_parens parens;
13991 if (!parens.require_open (parser))
13992 return error_mark_node;
13994 /* decltype (auto) */
13995 if (cxx_dialect >= cxx14
13996 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13998 cp_lexer_consume_token (parser->lexer);
13999 if (!parens.require_close (parser))
14000 return error_mark_node;
14001 expr = make_decltype_auto ();
14002 AUTO_IS_DECLTYPE (expr) = true;
14003 goto rewrite;
14006 /* Types cannot be defined in a `decltype' expression. Save away the
14007 old message. */
14008 saved_message = parser->type_definition_forbidden_message;
14010 /* And create the new one. */
14011 parser->type_definition_forbidden_message
14012 = G_("types may not be defined in %<decltype%> expressions");
14014 /* The restrictions on constant-expressions do not apply inside
14015 decltype expressions. */
14016 saved_integral_constant_expression_p
14017 = parser->integral_constant_expression_p;
14018 saved_non_integral_constant_expression_p
14019 = parser->non_integral_constant_expression_p;
14020 parser->integral_constant_expression_p = false;
14022 /* Within a parenthesized expression, a `>' token is always
14023 the greater-than operator. */
14024 saved_greater_than_is_operator_p
14025 = parser->greater_than_is_operator_p;
14026 parser->greater_than_is_operator_p = true;
14028 /* Do not actually evaluate the expression. */
14029 ++cp_unevaluated_operand;
14031 /* Do not warn about problems with the expression. */
14032 ++c_inhibit_evaluation_warnings;
14034 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14036 /* Go back to evaluating expressions. */
14037 --cp_unevaluated_operand;
14038 --c_inhibit_evaluation_warnings;
14040 /* The `>' token might be the end of a template-id or
14041 template-parameter-list now. */
14042 parser->greater_than_is_operator_p
14043 = saved_greater_than_is_operator_p;
14045 /* Restore the old message and the integral constant expression
14046 flags. */
14047 parser->type_definition_forbidden_message = saved_message;
14048 parser->integral_constant_expression_p
14049 = saved_integral_constant_expression_p;
14050 parser->non_integral_constant_expression_p
14051 = saved_non_integral_constant_expression_p;
14053 /* Parse to the closing `)'. */
14054 if (!parens.require_close (parser))
14056 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14057 /*consume_paren=*/true);
14058 return error_mark_node;
14061 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14062 tf_warning_or_error);
14064 rewrite:
14065 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14066 it again. */
14067 start_token->type = CPP_DECLTYPE;
14068 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14069 start_token->u.tree_check_value->value = expr;
14070 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14071 start_token->keyword = RID_MAX;
14072 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14074 return expr;
14077 /* Special member functions [gram.special] */
14079 /* Parse a conversion-function-id.
14081 conversion-function-id:
14082 operator conversion-type-id
14084 Returns an IDENTIFIER_NODE representing the operator. */
14086 static tree
14087 cp_parser_conversion_function_id (cp_parser* parser)
14089 tree type;
14090 tree saved_scope;
14091 tree saved_qualifying_scope;
14092 tree saved_object_scope;
14093 tree pushed_scope = NULL_TREE;
14095 /* Look for the `operator' token. */
14096 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14097 return error_mark_node;
14098 /* When we parse the conversion-type-id, the current scope will be
14099 reset. However, we need that information in able to look up the
14100 conversion function later, so we save it here. */
14101 saved_scope = parser->scope;
14102 saved_qualifying_scope = parser->qualifying_scope;
14103 saved_object_scope = parser->object_scope;
14104 /* We must enter the scope of the class so that the names of
14105 entities declared within the class are available in the
14106 conversion-type-id. For example, consider:
14108 struct S {
14109 typedef int I;
14110 operator I();
14113 S::operator I() { ... }
14115 In order to see that `I' is a type-name in the definition, we
14116 must be in the scope of `S'. */
14117 if (saved_scope)
14118 pushed_scope = push_scope (saved_scope);
14119 /* Parse the conversion-type-id. */
14120 type = cp_parser_conversion_type_id (parser);
14121 /* Leave the scope of the class, if any. */
14122 if (pushed_scope)
14123 pop_scope (pushed_scope);
14124 /* Restore the saved scope. */
14125 parser->scope = saved_scope;
14126 parser->qualifying_scope = saved_qualifying_scope;
14127 parser->object_scope = saved_object_scope;
14128 /* If the TYPE is invalid, indicate failure. */
14129 if (type == error_mark_node)
14130 return error_mark_node;
14131 return make_conv_op_name (type);
14134 /* Parse a conversion-type-id:
14136 conversion-type-id:
14137 type-specifier-seq conversion-declarator [opt]
14139 Returns the TYPE specified. */
14141 static tree
14142 cp_parser_conversion_type_id (cp_parser* parser)
14144 tree attributes;
14145 cp_decl_specifier_seq type_specifiers;
14146 cp_declarator *declarator;
14147 tree type_specified;
14148 const char *saved_message;
14150 /* Parse the attributes. */
14151 attributes = cp_parser_attributes_opt (parser);
14153 saved_message = parser->type_definition_forbidden_message;
14154 parser->type_definition_forbidden_message
14155 = G_("types may not be defined in a conversion-type-id");
14157 /* Parse the type-specifiers. */
14158 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14159 /*is_trailing_return=*/false,
14160 &type_specifiers);
14162 parser->type_definition_forbidden_message = saved_message;
14164 /* If that didn't work, stop. */
14165 if (type_specifiers.type == error_mark_node)
14166 return error_mark_node;
14167 /* Parse the conversion-declarator. */
14168 declarator = cp_parser_conversion_declarator_opt (parser);
14170 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14171 /*initialized=*/0, &attributes);
14172 if (attributes)
14173 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14175 /* Don't give this error when parsing tentatively. This happens to
14176 work because we always parse this definitively once. */
14177 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14178 && type_uses_auto (type_specified))
14180 if (cxx_dialect < cxx14)
14182 error ("invalid use of %<auto%> in conversion operator");
14183 return error_mark_node;
14185 else if (template_parm_scope_p ())
14186 warning (0, "use of %<auto%> in member template "
14187 "conversion operator can never be deduced");
14190 return type_specified;
14193 /* Parse an (optional) conversion-declarator.
14195 conversion-declarator:
14196 ptr-operator conversion-declarator [opt]
14200 static cp_declarator *
14201 cp_parser_conversion_declarator_opt (cp_parser* parser)
14203 enum tree_code code;
14204 tree class_type, std_attributes = NULL_TREE;
14205 cp_cv_quals cv_quals;
14207 /* We don't know if there's a ptr-operator next, or not. */
14208 cp_parser_parse_tentatively (parser);
14209 /* Try the ptr-operator. */
14210 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14211 &std_attributes);
14212 /* If it worked, look for more conversion-declarators. */
14213 if (cp_parser_parse_definitely (parser))
14215 cp_declarator *declarator;
14217 /* Parse another optional declarator. */
14218 declarator = cp_parser_conversion_declarator_opt (parser);
14220 declarator = cp_parser_make_indirect_declarator
14221 (code, class_type, cv_quals, declarator, std_attributes);
14223 return declarator;
14226 return NULL;
14229 /* Parse an (optional) ctor-initializer.
14231 ctor-initializer:
14232 : mem-initializer-list
14234 Returns TRUE iff the ctor-initializer was actually present. */
14236 static bool
14237 cp_parser_ctor_initializer_opt (cp_parser* parser)
14239 /* If the next token is not a `:', then there is no
14240 ctor-initializer. */
14241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14243 /* Do default initialization of any bases and members. */
14244 if (DECL_CONSTRUCTOR_P (current_function_decl))
14245 finish_mem_initializers (NULL_TREE);
14247 return false;
14250 /* Consume the `:' token. */
14251 cp_lexer_consume_token (parser->lexer);
14252 /* And the mem-initializer-list. */
14253 cp_parser_mem_initializer_list (parser);
14255 return true;
14258 /* Parse a mem-initializer-list.
14260 mem-initializer-list:
14261 mem-initializer ... [opt]
14262 mem-initializer ... [opt] , mem-initializer-list */
14264 static void
14265 cp_parser_mem_initializer_list (cp_parser* parser)
14267 tree mem_initializer_list = NULL_TREE;
14268 tree target_ctor = error_mark_node;
14269 cp_token *token = cp_lexer_peek_token (parser->lexer);
14271 /* Let the semantic analysis code know that we are starting the
14272 mem-initializer-list. */
14273 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14274 error_at (token->location,
14275 "only constructors take member initializers");
14277 /* Loop through the list. */
14278 while (true)
14280 tree mem_initializer;
14282 token = cp_lexer_peek_token (parser->lexer);
14283 /* Parse the mem-initializer. */
14284 mem_initializer = cp_parser_mem_initializer (parser);
14285 /* If the next token is a `...', we're expanding member initializers. */
14286 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14288 /* Consume the `...'. */
14289 cp_lexer_consume_token (parser->lexer);
14291 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14292 can be expanded but members cannot. */
14293 if (mem_initializer != error_mark_node
14294 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14296 error_at (token->location,
14297 "cannot expand initializer for member %qD",
14298 TREE_PURPOSE (mem_initializer));
14299 mem_initializer = error_mark_node;
14302 /* Construct the pack expansion type. */
14303 if (mem_initializer != error_mark_node)
14304 mem_initializer = make_pack_expansion (mem_initializer);
14306 if (target_ctor != error_mark_node
14307 && mem_initializer != error_mark_node)
14309 error ("mem-initializer for %qD follows constructor delegation",
14310 TREE_PURPOSE (mem_initializer));
14311 mem_initializer = error_mark_node;
14313 /* Look for a target constructor. */
14314 if (mem_initializer != error_mark_node
14315 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14316 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14318 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14319 if (mem_initializer_list)
14321 error ("constructor delegation follows mem-initializer for %qD",
14322 TREE_PURPOSE (mem_initializer_list));
14323 mem_initializer = error_mark_node;
14325 target_ctor = mem_initializer;
14327 /* Add it to the list, unless it was erroneous. */
14328 if (mem_initializer != error_mark_node)
14330 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14331 mem_initializer_list = mem_initializer;
14333 /* If the next token is not a `,', we're done. */
14334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14335 break;
14336 /* Consume the `,' token. */
14337 cp_lexer_consume_token (parser->lexer);
14340 /* Perform semantic analysis. */
14341 if (DECL_CONSTRUCTOR_P (current_function_decl))
14342 finish_mem_initializers (mem_initializer_list);
14345 /* Parse a mem-initializer.
14347 mem-initializer:
14348 mem-initializer-id ( expression-list [opt] )
14349 mem-initializer-id braced-init-list
14351 GNU extension:
14353 mem-initializer:
14354 ( expression-list [opt] )
14356 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14357 class) or FIELD_DECL (for a non-static data member) to initialize;
14358 the TREE_VALUE is the expression-list. An empty initialization
14359 list is represented by void_list_node. */
14361 static tree
14362 cp_parser_mem_initializer (cp_parser* parser)
14364 tree mem_initializer_id;
14365 tree expression_list;
14366 tree member;
14367 cp_token *token = cp_lexer_peek_token (parser->lexer);
14369 /* Find out what is being initialized. */
14370 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14372 permerror (token->location,
14373 "anachronistic old-style base class initializer");
14374 mem_initializer_id = NULL_TREE;
14376 else
14378 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14379 if (mem_initializer_id == error_mark_node)
14380 return mem_initializer_id;
14382 member = expand_member_init (mem_initializer_id);
14383 if (member && !DECL_P (member))
14384 in_base_initializer = 1;
14386 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14388 bool expr_non_constant_p;
14389 cp_lexer_set_source_position (parser->lexer);
14390 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14391 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14392 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14393 expression_list = build_tree_list (NULL_TREE, expression_list);
14395 else
14397 vec<tree, va_gc> *vec;
14398 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14399 /*cast_p=*/false,
14400 /*allow_expansion_p=*/true,
14401 /*non_constant_p=*/NULL);
14402 if (vec == NULL)
14403 return error_mark_node;
14404 expression_list = build_tree_list_vec (vec);
14405 release_tree_vector (vec);
14408 if (expression_list == error_mark_node)
14409 return error_mark_node;
14410 if (!expression_list)
14411 expression_list = void_type_node;
14413 in_base_initializer = 0;
14415 return member ? build_tree_list (member, expression_list) : error_mark_node;
14418 /* Parse a mem-initializer-id.
14420 mem-initializer-id:
14421 :: [opt] nested-name-specifier [opt] class-name
14422 decltype-specifier (C++11)
14423 identifier
14425 Returns a TYPE indicating the class to be initialized for the first
14426 production (and the second in C++11). Returns an IDENTIFIER_NODE
14427 indicating the data member to be initialized for the last production. */
14429 static tree
14430 cp_parser_mem_initializer_id (cp_parser* parser)
14432 bool global_scope_p;
14433 bool nested_name_specifier_p;
14434 bool template_p = false;
14435 tree id;
14437 cp_token *token = cp_lexer_peek_token (parser->lexer);
14439 /* `typename' is not allowed in this context ([temp.res]). */
14440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14442 error_at (token->location,
14443 "keyword %<typename%> not allowed in this context (a qualified "
14444 "member initializer is implicitly a type)");
14445 cp_lexer_consume_token (parser->lexer);
14447 /* Look for the optional `::' operator. */
14448 global_scope_p
14449 = (cp_parser_global_scope_opt (parser,
14450 /*current_scope_valid_p=*/false)
14451 != NULL_TREE);
14452 /* Look for the optional nested-name-specifier. The simplest way to
14453 implement:
14455 [temp.res]
14457 The keyword `typename' is not permitted in a base-specifier or
14458 mem-initializer; in these contexts a qualified name that
14459 depends on a template-parameter is implicitly assumed to be a
14460 type name.
14462 is to assume that we have seen the `typename' keyword at this
14463 point. */
14464 nested_name_specifier_p
14465 = (cp_parser_nested_name_specifier_opt (parser,
14466 /*typename_keyword_p=*/true,
14467 /*check_dependency_p=*/true,
14468 /*type_p=*/true,
14469 /*is_declaration=*/true)
14470 != NULL_TREE);
14471 if (nested_name_specifier_p)
14472 template_p = cp_parser_optional_template_keyword (parser);
14473 /* If there is a `::' operator or a nested-name-specifier, then we
14474 are definitely looking for a class-name. */
14475 if (global_scope_p || nested_name_specifier_p)
14476 return cp_parser_class_name (parser,
14477 /*typename_keyword_p=*/true,
14478 /*template_keyword_p=*/template_p,
14479 typename_type,
14480 /*check_dependency_p=*/true,
14481 /*class_head_p=*/false,
14482 /*is_declaration=*/true);
14483 /* Otherwise, we could also be looking for an ordinary identifier. */
14484 cp_parser_parse_tentatively (parser);
14485 if (cp_lexer_next_token_is_decltype (parser->lexer))
14486 /* Try a decltype-specifier. */
14487 id = cp_parser_decltype (parser);
14488 else
14489 /* Otherwise, try a class-name. */
14490 id = cp_parser_class_name (parser,
14491 /*typename_keyword_p=*/true,
14492 /*template_keyword_p=*/false,
14493 none_type,
14494 /*check_dependency_p=*/true,
14495 /*class_head_p=*/false,
14496 /*is_declaration=*/true);
14497 /* If we found one, we're done. */
14498 if (cp_parser_parse_definitely (parser))
14499 return id;
14500 /* Otherwise, look for an ordinary identifier. */
14501 return cp_parser_identifier (parser);
14504 /* Overloading [gram.over] */
14506 /* Parse an operator-function-id.
14508 operator-function-id:
14509 operator operator
14511 Returns an IDENTIFIER_NODE for the operator which is a
14512 human-readable spelling of the identifier, e.g., `operator +'. */
14514 static cp_expr
14515 cp_parser_operator_function_id (cp_parser* parser)
14517 /* Look for the `operator' keyword. */
14518 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14519 return error_mark_node;
14520 /* And then the name of the operator itself. */
14521 return cp_parser_operator (parser);
14524 /* Return an identifier node for a user-defined literal operator.
14525 The suffix identifier is chained to the operator name identifier. */
14527 tree
14528 cp_literal_operator_id (const char* name)
14530 tree identifier;
14531 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14532 + strlen (name) + 10);
14533 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14534 identifier = get_identifier (buffer);
14536 return identifier;
14539 /* Parse an operator.
14541 operator:
14542 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14543 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14544 || ++ -- , ->* -> () []
14546 GNU Extensions:
14548 operator:
14549 <? >? <?= >?=
14551 Returns an IDENTIFIER_NODE for the operator which is a
14552 human-readable spelling of the identifier, e.g., `operator +'. */
14554 static cp_expr
14555 cp_parser_operator (cp_parser* parser)
14557 tree id = NULL_TREE;
14558 cp_token *token;
14559 bool utf8 = false;
14561 /* Peek at the next token. */
14562 token = cp_lexer_peek_token (parser->lexer);
14564 location_t start_loc = token->location;
14566 /* Figure out which operator we have. */
14567 switch (token->type)
14569 case CPP_KEYWORD:
14571 enum tree_code op;
14573 /* The keyword should be either `new' or `delete'. */
14574 if (token->keyword == RID_NEW)
14575 op = NEW_EXPR;
14576 else if (token->keyword == RID_DELETE)
14577 op = DELETE_EXPR;
14578 else
14579 break;
14581 /* Consume the `new' or `delete' token. */
14582 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14584 /* Peek at the next token. */
14585 token = cp_lexer_peek_token (parser->lexer);
14586 /* If it's a `[' token then this is the array variant of the
14587 operator. */
14588 if (token->type == CPP_OPEN_SQUARE)
14590 /* Consume the `[' token. */
14591 cp_lexer_consume_token (parser->lexer);
14592 /* Look for the `]' token. */
14593 if (cp_token *close_token
14594 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14595 end_loc = close_token->location;
14596 id = cp_operator_id (op == NEW_EXPR
14597 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14599 /* Otherwise, we have the non-array variant. */
14600 else
14601 id = cp_operator_id (op);
14603 location_t loc = make_location (start_loc, start_loc, end_loc);
14605 return cp_expr (id, loc);
14608 case CPP_PLUS:
14609 id = cp_operator_id (PLUS_EXPR);
14610 break;
14612 case CPP_MINUS:
14613 id = cp_operator_id (MINUS_EXPR);
14614 break;
14616 case CPP_MULT:
14617 id = cp_operator_id (MULT_EXPR);
14618 break;
14620 case CPP_DIV:
14621 id = cp_operator_id (TRUNC_DIV_EXPR);
14622 break;
14624 case CPP_MOD:
14625 id = cp_operator_id (TRUNC_MOD_EXPR);
14626 break;
14628 case CPP_XOR:
14629 id = cp_operator_id (BIT_XOR_EXPR);
14630 break;
14632 case CPP_AND:
14633 id = cp_operator_id (BIT_AND_EXPR);
14634 break;
14636 case CPP_OR:
14637 id = cp_operator_id (BIT_IOR_EXPR);
14638 break;
14640 case CPP_COMPL:
14641 id = cp_operator_id (BIT_NOT_EXPR);
14642 break;
14644 case CPP_NOT:
14645 id = cp_operator_id (TRUTH_NOT_EXPR);
14646 break;
14648 case CPP_EQ:
14649 id = cp_assignment_operator_id (NOP_EXPR);
14650 break;
14652 case CPP_LESS:
14653 id = cp_operator_id (LT_EXPR);
14654 break;
14656 case CPP_GREATER:
14657 id = cp_operator_id (GT_EXPR);
14658 break;
14660 case CPP_PLUS_EQ:
14661 id = cp_assignment_operator_id (PLUS_EXPR);
14662 break;
14664 case CPP_MINUS_EQ:
14665 id = cp_assignment_operator_id (MINUS_EXPR);
14666 break;
14668 case CPP_MULT_EQ:
14669 id = cp_assignment_operator_id (MULT_EXPR);
14670 break;
14672 case CPP_DIV_EQ:
14673 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14674 break;
14676 case CPP_MOD_EQ:
14677 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14678 break;
14680 case CPP_XOR_EQ:
14681 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14682 break;
14684 case CPP_AND_EQ:
14685 id = cp_assignment_operator_id (BIT_AND_EXPR);
14686 break;
14688 case CPP_OR_EQ:
14689 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14690 break;
14692 case CPP_LSHIFT:
14693 id = cp_operator_id (LSHIFT_EXPR);
14694 break;
14696 case CPP_RSHIFT:
14697 id = cp_operator_id (RSHIFT_EXPR);
14698 break;
14700 case CPP_LSHIFT_EQ:
14701 id = cp_assignment_operator_id (LSHIFT_EXPR);
14702 break;
14704 case CPP_RSHIFT_EQ:
14705 id = cp_assignment_operator_id (RSHIFT_EXPR);
14706 break;
14708 case CPP_EQ_EQ:
14709 id = cp_operator_id (EQ_EXPR);
14710 break;
14712 case CPP_NOT_EQ:
14713 id = cp_operator_id (NE_EXPR);
14714 break;
14716 case CPP_LESS_EQ:
14717 id = cp_operator_id (LE_EXPR);
14718 break;
14720 case CPP_GREATER_EQ:
14721 id = cp_operator_id (GE_EXPR);
14722 break;
14724 case CPP_AND_AND:
14725 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14726 break;
14728 case CPP_OR_OR:
14729 id = cp_operator_id (TRUTH_ORIF_EXPR);
14730 break;
14732 case CPP_PLUS_PLUS:
14733 id = cp_operator_id (POSTINCREMENT_EXPR);
14734 break;
14736 case CPP_MINUS_MINUS:
14737 id = cp_operator_id (PREDECREMENT_EXPR);
14738 break;
14740 case CPP_COMMA:
14741 id = cp_operator_id (COMPOUND_EXPR);
14742 break;
14744 case CPP_DEREF_STAR:
14745 id = cp_operator_id (MEMBER_REF);
14746 break;
14748 case CPP_DEREF:
14749 id = cp_operator_id (COMPONENT_REF);
14750 break;
14752 case CPP_OPEN_PAREN:
14754 /* Consume the `('. */
14755 matching_parens parens;
14756 parens.consume_open (parser);
14757 /* Look for the matching `)'. */
14758 parens.require_close (parser);
14759 return cp_operator_id (CALL_EXPR);
14762 case CPP_OPEN_SQUARE:
14763 /* Consume the `['. */
14764 cp_lexer_consume_token (parser->lexer);
14765 /* Look for the matching `]'. */
14766 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14767 return cp_operator_id (ARRAY_REF);
14769 case CPP_UTF8STRING:
14770 case CPP_UTF8STRING_USERDEF:
14771 utf8 = true;
14772 /* FALLTHRU */
14773 case CPP_STRING:
14774 case CPP_WSTRING:
14775 case CPP_STRING16:
14776 case CPP_STRING32:
14777 case CPP_STRING_USERDEF:
14778 case CPP_WSTRING_USERDEF:
14779 case CPP_STRING16_USERDEF:
14780 case CPP_STRING32_USERDEF:
14782 tree str, string_tree;
14783 int sz, len;
14785 if (cxx_dialect == cxx98)
14786 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14788 /* Consume the string. */
14789 str = cp_parser_string_literal (parser, /*translate=*/true,
14790 /*wide_ok=*/true, /*lookup_udlit=*/false);
14791 if (str == error_mark_node)
14792 return error_mark_node;
14793 else if (TREE_CODE (str) == USERDEF_LITERAL)
14795 string_tree = USERDEF_LITERAL_VALUE (str);
14796 id = USERDEF_LITERAL_SUFFIX_ID (str);
14798 else
14800 string_tree = str;
14801 /* Look for the suffix identifier. */
14802 token = cp_lexer_peek_token (parser->lexer);
14803 if (token->type == CPP_NAME)
14804 id = cp_parser_identifier (parser);
14805 else if (token->type == CPP_KEYWORD)
14807 error ("unexpected keyword;"
14808 " remove space between quotes and suffix identifier");
14809 return error_mark_node;
14811 else
14813 error ("expected suffix identifier");
14814 return error_mark_node;
14817 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14818 (TREE_TYPE (TREE_TYPE (string_tree))));
14819 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14820 if (len != 0)
14822 error ("expected empty string after %<operator%> keyword");
14823 return error_mark_node;
14825 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14826 != char_type_node)
14828 error ("invalid encoding prefix in literal operator");
14829 return error_mark_node;
14831 if (id != error_mark_node)
14833 const char *name = IDENTIFIER_POINTER (id);
14834 id = cp_literal_operator_id (name);
14836 return id;
14839 default:
14840 /* Anything else is an error. */
14841 break;
14844 /* If we have selected an identifier, we need to consume the
14845 operator token. */
14846 if (id)
14847 cp_lexer_consume_token (parser->lexer);
14848 /* Otherwise, no valid operator name was present. */
14849 else
14851 cp_parser_error (parser, "expected operator");
14852 id = error_mark_node;
14855 return cp_expr (id, start_loc);
14858 /* Parse a template-declaration.
14860 template-declaration:
14861 export [opt] template < template-parameter-list > declaration
14863 If MEMBER_P is TRUE, this template-declaration occurs within a
14864 class-specifier.
14866 The grammar rule given by the standard isn't correct. What
14867 is really meant is:
14869 template-declaration:
14870 export [opt] template-parameter-list-seq
14871 decl-specifier-seq [opt] init-declarator [opt] ;
14872 export [opt] template-parameter-list-seq
14873 function-definition
14875 template-parameter-list-seq:
14876 template-parameter-list-seq [opt]
14877 template < template-parameter-list >
14879 Concept Extensions:
14881 template-parameter-list-seq:
14882 template < template-parameter-list > requires-clause [opt]
14884 requires-clause:
14885 requires logical-or-expression */
14887 static void
14888 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14890 /* Check for `export'. */
14891 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14893 /* Consume the `export' token. */
14894 cp_lexer_consume_token (parser->lexer);
14895 /* Warn that we do not support `export'. */
14896 warning (0, "keyword %<export%> not implemented, and will be ignored");
14899 cp_parser_template_declaration_after_export (parser, member_p);
14902 /* Parse a template-parameter-list.
14904 template-parameter-list:
14905 template-parameter
14906 template-parameter-list , template-parameter
14908 Returns a TREE_LIST. Each node represents a template parameter.
14909 The nodes are connected via their TREE_CHAINs. */
14911 static tree
14912 cp_parser_template_parameter_list (cp_parser* parser)
14914 tree parameter_list = NULL_TREE;
14916 begin_template_parm_list ();
14918 /* The loop below parses the template parms. We first need to know
14919 the total number of template parms to be able to compute proper
14920 canonical types of each dependent type. So after the loop, when
14921 we know the total number of template parms,
14922 end_template_parm_list computes the proper canonical types and
14923 fixes up the dependent types accordingly. */
14924 while (true)
14926 tree parameter;
14927 bool is_non_type;
14928 bool is_parameter_pack;
14929 location_t parm_loc;
14931 /* Parse the template-parameter. */
14932 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14933 parameter = cp_parser_template_parameter (parser,
14934 &is_non_type,
14935 &is_parameter_pack);
14936 /* Add it to the list. */
14937 if (parameter != error_mark_node)
14938 parameter_list = process_template_parm (parameter_list,
14939 parm_loc,
14940 parameter,
14941 is_non_type,
14942 is_parameter_pack);
14943 else
14945 tree err_parm = build_tree_list (parameter, parameter);
14946 parameter_list = chainon (parameter_list, err_parm);
14949 /* If the next token is not a `,', we're done. */
14950 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14951 break;
14952 /* Otherwise, consume the `,' token. */
14953 cp_lexer_consume_token (parser->lexer);
14956 return end_template_parm_list (parameter_list);
14959 /* Parse a introduction-list.
14961 introduction-list:
14962 introduced-parameter
14963 introduction-list , introduced-parameter
14965 introduced-parameter:
14966 ...[opt] identifier
14968 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14969 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14970 WILDCARD_DECL will also have DECL_NAME set and token location in
14971 DECL_SOURCE_LOCATION. */
14973 static tree
14974 cp_parser_introduction_list (cp_parser *parser)
14976 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14978 while (true)
14980 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14981 if (is_pack)
14982 cp_lexer_consume_token (parser->lexer);
14984 /* Build placeholder. */
14985 tree parm = build_nt (WILDCARD_DECL);
14986 DECL_SOURCE_LOCATION (parm)
14987 = cp_lexer_peek_token (parser->lexer)->location;
14988 DECL_NAME (parm) = cp_parser_identifier (parser);
14989 WILDCARD_PACK_P (parm) = is_pack;
14990 vec_safe_push (introduction_vec, parm);
14992 /* If the next token is not a `,', we're done. */
14993 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14994 break;
14995 /* Otherwise, consume the `,' token. */
14996 cp_lexer_consume_token (parser->lexer);
14999 /* Convert the vec into a TREE_VEC. */
15000 tree introduction_list = make_tree_vec (introduction_vec->length ());
15001 unsigned int n;
15002 tree parm;
15003 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15004 TREE_VEC_ELT (introduction_list, n) = parm;
15006 release_tree_vector (introduction_vec);
15007 return introduction_list;
15010 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15011 is an abstract declarator. */
15013 static inline cp_declarator*
15014 get_id_declarator (cp_declarator *declarator)
15016 cp_declarator *d = declarator;
15017 while (d && d->kind != cdk_id)
15018 d = d->declarator;
15019 return d;
15022 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15023 is an abstract declarator. */
15025 static inline tree
15026 get_unqualified_id (cp_declarator *declarator)
15028 declarator = get_id_declarator (declarator);
15029 if (declarator)
15030 return declarator->u.id.unqualified_name;
15031 else
15032 return NULL_TREE;
15035 /* Returns true if DECL represents a constrained-parameter. */
15037 static inline bool
15038 is_constrained_parameter (tree decl)
15040 return (decl
15041 && TREE_CODE (decl) == TYPE_DECL
15042 && CONSTRAINED_PARM_CONCEPT (decl)
15043 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15046 /* Returns true if PARM declares a constrained-parameter. */
15048 static inline bool
15049 is_constrained_parameter (cp_parameter_declarator *parm)
15051 return is_constrained_parameter (parm->decl_specifiers.type);
15054 /* Check that the type parameter is only a declarator-id, and that its
15055 type is not cv-qualified. */
15057 bool
15058 cp_parser_check_constrained_type_parm (cp_parser *parser,
15059 cp_parameter_declarator *parm)
15061 if (!parm->declarator)
15062 return true;
15064 if (parm->declarator->kind != cdk_id)
15066 cp_parser_error (parser, "invalid constrained type parameter");
15067 return false;
15070 /* Don't allow cv-qualified type parameters. */
15071 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15072 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15074 cp_parser_error (parser, "cv-qualified type parameter");
15075 return false;
15078 return true;
15081 /* Finish parsing/processing a template type parameter and checking
15082 various restrictions. */
15084 static inline tree
15085 cp_parser_constrained_type_template_parm (cp_parser *parser,
15086 tree id,
15087 cp_parameter_declarator* parmdecl)
15089 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15090 return finish_template_type_parm (class_type_node, id);
15091 else
15092 return error_mark_node;
15095 static tree
15096 finish_constrained_template_template_parm (tree proto, tree id)
15098 /* FIXME: This should probably be copied, and we may need to adjust
15099 the template parameter depths. */
15100 tree saved_parms = current_template_parms;
15101 begin_template_parm_list ();
15102 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15103 end_template_parm_list ();
15105 tree parm = finish_template_template_parm (class_type_node, id);
15106 current_template_parms = saved_parms;
15108 return parm;
15111 /* Finish parsing/processing a template template parameter by borrowing
15112 the template parameter list from the prototype parameter. */
15114 static tree
15115 cp_parser_constrained_template_template_parm (cp_parser *parser,
15116 tree proto,
15117 tree id,
15118 cp_parameter_declarator *parmdecl)
15120 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15121 return error_mark_node;
15122 return finish_constrained_template_template_parm (proto, id);
15125 /* Create a new non-type template parameter from the given PARM
15126 declarator. */
15128 static tree
15129 constrained_non_type_template_parm (bool *is_non_type,
15130 cp_parameter_declarator *parm)
15132 *is_non_type = true;
15133 cp_declarator *decl = parm->declarator;
15134 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15135 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15136 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15139 /* Build a constrained template parameter based on the PARMDECL
15140 declarator. The type of PARMDECL is the constrained type, which
15141 refers to the prototype template parameter that ultimately
15142 specifies the type of the declared parameter. */
15144 static tree
15145 finish_constrained_parameter (cp_parser *parser,
15146 cp_parameter_declarator *parmdecl,
15147 bool *is_non_type,
15148 bool *is_parameter_pack)
15150 tree decl = parmdecl->decl_specifiers.type;
15151 tree id = get_unqualified_id (parmdecl->declarator);
15152 tree def = parmdecl->default_argument;
15153 tree proto = DECL_INITIAL (decl);
15155 /* A template parameter constrained by a variadic concept shall also
15156 be declared as a template parameter pack. */
15157 bool is_variadic = template_parameter_pack_p (proto);
15158 if (is_variadic && !*is_parameter_pack)
15159 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15161 /* Build the parameter. Return an error if the declarator was invalid. */
15162 tree parm;
15163 if (TREE_CODE (proto) == TYPE_DECL)
15164 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15165 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15166 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15167 parmdecl);
15168 else
15169 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15170 if (parm == error_mark_node)
15171 return error_mark_node;
15173 /* Finish the parameter decl and create a node attaching the
15174 default argument and constraint. */
15175 parm = build_tree_list (def, parm);
15176 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15178 return parm;
15181 /* Returns true if the parsed type actually represents the declaration
15182 of a type template-parameter. */
15184 static inline bool
15185 declares_constrained_type_template_parameter (tree type)
15187 return (is_constrained_parameter (type)
15188 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15192 /* Returns true if the parsed type actually represents the declaration of
15193 a template template-parameter. */
15195 static bool
15196 declares_constrained_template_template_parameter (tree type)
15198 return (is_constrained_parameter (type)
15199 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15202 /* Parse a default argument for a type template-parameter.
15203 Note that diagnostics are handled in cp_parser_template_parameter. */
15205 static tree
15206 cp_parser_default_type_template_argument (cp_parser *parser)
15208 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15210 /* Consume the `=' token. */
15211 cp_lexer_consume_token (parser->lexer);
15213 cp_token *token = cp_lexer_peek_token (parser->lexer);
15215 /* Parse the default-argument. */
15216 push_deferring_access_checks (dk_no_deferred);
15217 tree default_argument = cp_parser_type_id (parser);
15218 pop_deferring_access_checks ();
15220 if (flag_concepts && type_uses_auto (default_argument))
15222 error_at (token->location,
15223 "invalid use of %<auto%> in default template argument");
15224 return error_mark_node;
15227 return default_argument;
15230 /* Parse a default argument for a template template-parameter. */
15232 static tree
15233 cp_parser_default_template_template_argument (cp_parser *parser)
15235 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15237 bool is_template;
15239 /* Consume the `='. */
15240 cp_lexer_consume_token (parser->lexer);
15241 /* Parse the id-expression. */
15242 push_deferring_access_checks (dk_no_deferred);
15243 /* save token before parsing the id-expression, for error
15244 reporting */
15245 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15246 tree default_argument
15247 = cp_parser_id_expression (parser,
15248 /*template_keyword_p=*/false,
15249 /*check_dependency_p=*/true,
15250 /*template_p=*/&is_template,
15251 /*declarator_p=*/false,
15252 /*optional_p=*/false);
15253 if (TREE_CODE (default_argument) == TYPE_DECL)
15254 /* If the id-expression was a template-id that refers to
15255 a template-class, we already have the declaration here,
15256 so no further lookup is needed. */
15258 else
15259 /* Look up the name. */
15260 default_argument
15261 = cp_parser_lookup_name (parser, default_argument,
15262 none_type,
15263 /*is_template=*/is_template,
15264 /*is_namespace=*/false,
15265 /*check_dependency=*/true,
15266 /*ambiguous_decls=*/NULL,
15267 token->location);
15268 /* See if the default argument is valid. */
15269 default_argument = check_template_template_default_arg (default_argument);
15270 pop_deferring_access_checks ();
15271 return default_argument;
15274 /* Parse a template-parameter.
15276 template-parameter:
15277 type-parameter
15278 parameter-declaration
15280 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15281 the parameter. The TREE_PURPOSE is the default value, if any.
15282 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15283 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15284 set to true iff this parameter is a parameter pack. */
15286 static tree
15287 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15288 bool *is_parameter_pack)
15290 cp_token *token;
15291 cp_parameter_declarator *parameter_declarator;
15292 tree parm;
15294 /* Assume it is a type parameter or a template parameter. */
15295 *is_non_type = false;
15296 /* Assume it not a parameter pack. */
15297 *is_parameter_pack = false;
15298 /* Peek at the next token. */
15299 token = cp_lexer_peek_token (parser->lexer);
15300 /* If it is `template', we have a type-parameter. */
15301 if (token->keyword == RID_TEMPLATE)
15302 return cp_parser_type_parameter (parser, is_parameter_pack);
15303 /* If it is `class' or `typename' we do not know yet whether it is a
15304 type parameter or a non-type parameter. Consider:
15306 template <typename T, typename T::X X> ...
15310 template <class C, class D*> ...
15312 Here, the first parameter is a type parameter, and the second is
15313 a non-type parameter. We can tell by looking at the token after
15314 the identifier -- if it is a `,', `=', or `>' then we have a type
15315 parameter. */
15316 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15318 /* Peek at the token after `class' or `typename'. */
15319 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15320 /* If it's an ellipsis, we have a template type parameter
15321 pack. */
15322 if (token->type == CPP_ELLIPSIS)
15323 return cp_parser_type_parameter (parser, is_parameter_pack);
15324 /* If it's an identifier, skip it. */
15325 if (token->type == CPP_NAME)
15326 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15327 /* Now, see if the token looks like the end of a template
15328 parameter. */
15329 if (token->type == CPP_COMMA
15330 || token->type == CPP_EQ
15331 || token->type == CPP_GREATER)
15332 return cp_parser_type_parameter (parser, is_parameter_pack);
15335 /* Otherwise, it is a non-type parameter or a constrained parameter.
15337 [temp.param]
15339 When parsing a default template-argument for a non-type
15340 template-parameter, the first non-nested `>' is taken as the end
15341 of the template parameter-list rather than a greater-than
15342 operator. */
15343 parameter_declarator
15344 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15345 /*parenthesized_p=*/NULL);
15347 if (!parameter_declarator)
15348 return error_mark_node;
15350 /* If the parameter declaration is marked as a parameter pack, set
15351 *IS_PARAMETER_PACK to notify the caller. */
15352 if (parameter_declarator->template_parameter_pack_p)
15353 *is_parameter_pack = true;
15355 if (parameter_declarator->default_argument)
15357 /* Can happen in some cases of erroneous input (c++/34892). */
15358 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15359 /* Consume the `...' for better error recovery. */
15360 cp_lexer_consume_token (parser->lexer);
15363 // The parameter may have been constrained.
15364 if (is_constrained_parameter (parameter_declarator))
15365 return finish_constrained_parameter (parser,
15366 parameter_declarator,
15367 is_non_type,
15368 is_parameter_pack);
15370 // Now we're sure that the parameter is a non-type parameter.
15371 *is_non_type = true;
15373 parm = grokdeclarator (parameter_declarator->declarator,
15374 &parameter_declarator->decl_specifiers,
15375 TPARM, /*initialized=*/0,
15376 /*attrlist=*/NULL);
15377 if (parm == error_mark_node)
15378 return error_mark_node;
15380 return build_tree_list (parameter_declarator->default_argument, parm);
15383 /* Parse a type-parameter.
15385 type-parameter:
15386 class identifier [opt]
15387 class identifier [opt] = type-id
15388 typename identifier [opt]
15389 typename identifier [opt] = type-id
15390 template < template-parameter-list > class identifier [opt]
15391 template < template-parameter-list > class identifier [opt]
15392 = id-expression
15394 GNU Extension (variadic templates):
15396 type-parameter:
15397 class ... identifier [opt]
15398 typename ... identifier [opt]
15400 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15401 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15402 the declaration of the parameter.
15404 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15406 static tree
15407 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15409 cp_token *token;
15410 tree parameter;
15412 /* Look for a keyword to tell us what kind of parameter this is. */
15413 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15414 if (!token)
15415 return error_mark_node;
15417 switch (token->keyword)
15419 case RID_CLASS:
15420 case RID_TYPENAME:
15422 tree identifier;
15423 tree default_argument;
15425 /* If the next token is an ellipsis, we have a template
15426 argument pack. */
15427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15429 /* Consume the `...' token. */
15430 cp_lexer_consume_token (parser->lexer);
15431 maybe_warn_variadic_templates ();
15433 *is_parameter_pack = true;
15436 /* If the next token is an identifier, then it names the
15437 parameter. */
15438 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15439 identifier = cp_parser_identifier (parser);
15440 else
15441 identifier = NULL_TREE;
15443 /* Create the parameter. */
15444 parameter = finish_template_type_parm (class_type_node, identifier);
15446 /* If the next token is an `=', we have a default argument. */
15447 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15449 default_argument
15450 = cp_parser_default_type_template_argument (parser);
15452 /* Template parameter packs cannot have default
15453 arguments. */
15454 if (*is_parameter_pack)
15456 if (identifier)
15457 error_at (token->location,
15458 "template parameter pack %qD cannot have a "
15459 "default argument", identifier);
15460 else
15461 error_at (token->location,
15462 "template parameter packs cannot have "
15463 "default arguments");
15464 default_argument = NULL_TREE;
15466 else if (check_for_bare_parameter_packs (default_argument))
15467 default_argument = error_mark_node;
15469 else
15470 default_argument = NULL_TREE;
15472 /* Create the combined representation of the parameter and the
15473 default argument. */
15474 parameter = build_tree_list (default_argument, parameter);
15476 break;
15478 case RID_TEMPLATE:
15480 tree identifier;
15481 tree default_argument;
15483 /* Look for the `<'. */
15484 cp_parser_require (parser, CPP_LESS, RT_LESS);
15485 /* Parse the template-parameter-list. */
15486 cp_parser_template_parameter_list (parser);
15487 /* Look for the `>'. */
15488 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15490 // If template requirements are present, parse them.
15491 if (flag_concepts)
15493 tree reqs = get_shorthand_constraints (current_template_parms);
15494 if (tree r = cp_parser_requires_clause_opt (parser))
15495 reqs = conjoin_constraints (reqs, normalize_expression (r));
15496 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15499 /* Look for the `class' or 'typename' keywords. */
15500 cp_parser_type_parameter_key (parser);
15501 /* If the next token is an ellipsis, we have a template
15502 argument pack. */
15503 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15505 /* Consume the `...' token. */
15506 cp_lexer_consume_token (parser->lexer);
15507 maybe_warn_variadic_templates ();
15509 *is_parameter_pack = true;
15511 /* If the next token is an `=', then there is a
15512 default-argument. If the next token is a `>', we are at
15513 the end of the parameter-list. If the next token is a `,',
15514 then we are at the end of this parameter. */
15515 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15516 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15517 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15519 identifier = cp_parser_identifier (parser);
15520 /* Treat invalid names as if the parameter were nameless. */
15521 if (identifier == error_mark_node)
15522 identifier = NULL_TREE;
15524 else
15525 identifier = NULL_TREE;
15527 /* Create the template parameter. */
15528 parameter = finish_template_template_parm (class_type_node,
15529 identifier);
15531 /* If the next token is an `=', then there is a
15532 default-argument. */
15533 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15535 default_argument
15536 = cp_parser_default_template_template_argument (parser);
15538 /* Template parameter packs cannot have default
15539 arguments. */
15540 if (*is_parameter_pack)
15542 if (identifier)
15543 error_at (token->location,
15544 "template parameter pack %qD cannot "
15545 "have a default argument",
15546 identifier);
15547 else
15548 error_at (token->location, "template parameter packs cannot "
15549 "have default arguments");
15550 default_argument = NULL_TREE;
15553 else
15554 default_argument = NULL_TREE;
15556 /* Create the combined representation of the parameter and the
15557 default argument. */
15558 parameter = build_tree_list (default_argument, parameter);
15560 break;
15562 default:
15563 gcc_unreachable ();
15564 break;
15567 return parameter;
15570 /* Parse a template-id.
15572 template-id:
15573 template-name < template-argument-list [opt] >
15575 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15576 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15577 returned. Otherwise, if the template-name names a function, or set
15578 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15579 names a class, returns a TYPE_DECL for the specialization.
15581 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15582 uninstantiated templates. */
15584 static tree
15585 cp_parser_template_id (cp_parser *parser,
15586 bool template_keyword_p,
15587 bool check_dependency_p,
15588 enum tag_types tag_type,
15589 bool is_declaration)
15591 tree templ;
15592 tree arguments;
15593 tree template_id;
15594 cp_token_position start_of_id = 0;
15595 cp_token *next_token = NULL, *next_token_2 = NULL;
15596 bool is_identifier;
15598 /* If the next token corresponds to a template-id, there is no need
15599 to reparse it. */
15600 cp_token *token = cp_lexer_peek_token (parser->lexer);
15601 if (token->type == CPP_TEMPLATE_ID)
15603 cp_lexer_consume_token (parser->lexer);
15604 return saved_checks_value (token->u.tree_check_value);
15607 /* Avoid performing name lookup if there is no possibility of
15608 finding a template-id. */
15609 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15610 || (token->type == CPP_NAME
15611 && !cp_parser_nth_token_starts_template_argument_list_p
15612 (parser, 2)))
15614 cp_parser_error (parser, "expected template-id");
15615 return error_mark_node;
15618 /* Remember where the template-id starts. */
15619 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15620 start_of_id = cp_lexer_token_position (parser->lexer, false);
15622 push_deferring_access_checks (dk_deferred);
15624 /* Parse the template-name. */
15625 is_identifier = false;
15626 templ = cp_parser_template_name (parser, template_keyword_p,
15627 check_dependency_p,
15628 is_declaration,
15629 tag_type,
15630 &is_identifier);
15631 if (templ == error_mark_node || is_identifier)
15633 pop_deferring_access_checks ();
15634 return templ;
15637 /* Since we're going to preserve any side-effects from this parse, set up a
15638 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15639 in the template arguments. */
15640 tentative_firewall firewall (parser);
15642 /* If we find the sequence `[:' after a template-name, it's probably
15643 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15644 parse correctly the argument list. */
15645 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15646 == CPP_OPEN_SQUARE)
15647 && next_token->flags & DIGRAPH
15648 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15649 == CPP_COLON)
15650 && !(next_token_2->flags & PREV_WHITE))
15652 cp_parser_parse_tentatively (parser);
15653 /* Change `:' into `::'. */
15654 next_token_2->type = CPP_SCOPE;
15655 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15656 CPP_LESS. */
15657 cp_lexer_consume_token (parser->lexer);
15659 /* Parse the arguments. */
15660 arguments = cp_parser_enclosed_template_argument_list (parser);
15661 if (!cp_parser_parse_definitely (parser))
15663 /* If we couldn't parse an argument list, then we revert our changes
15664 and return simply an error. Maybe this is not a template-id
15665 after all. */
15666 next_token_2->type = CPP_COLON;
15667 cp_parser_error (parser, "expected %<<%>");
15668 pop_deferring_access_checks ();
15669 return error_mark_node;
15671 /* Otherwise, emit an error about the invalid digraph, but continue
15672 parsing because we got our argument list. */
15673 if (permerror (next_token->location,
15674 "%<<::%> cannot begin a template-argument list"))
15676 static bool hint = false;
15677 inform (next_token->location,
15678 "%<<:%> is an alternate spelling for %<[%>."
15679 " Insert whitespace between %<<%> and %<::%>");
15680 if (!hint && !flag_permissive)
15682 inform (next_token->location, "(if you use %<-fpermissive%> "
15683 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15684 "accept your code)");
15685 hint = true;
15689 else
15691 /* Look for the `<' that starts the template-argument-list. */
15692 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15694 pop_deferring_access_checks ();
15695 return error_mark_node;
15697 /* Parse the arguments. */
15698 arguments = cp_parser_enclosed_template_argument_list (parser);
15701 /* Set the location to be of the form:
15702 template-name < template-argument-list [opt] >
15703 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15704 with caret == start at the start of the template-name,
15705 ranging until the closing '>'. */
15706 location_t finish_loc
15707 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15708 location_t combined_loc
15709 = make_location (token->location, token->location, finish_loc);
15711 /* Build a representation of the specialization. */
15712 if (identifier_p (templ))
15713 template_id = build_min_nt_loc (combined_loc,
15714 TEMPLATE_ID_EXPR,
15715 templ, arguments);
15716 else if (DECL_TYPE_TEMPLATE_P (templ)
15717 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15719 bool entering_scope;
15720 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15721 template (rather than some instantiation thereof) only if
15722 is not nested within some other construct. For example, in
15723 "template <typename T> void f(T) { A<T>::", A<T> is just an
15724 instantiation of A. */
15725 entering_scope = (template_parm_scope_p ()
15726 && cp_lexer_next_token_is (parser->lexer,
15727 CPP_SCOPE));
15728 template_id
15729 = finish_template_type (templ, arguments, entering_scope);
15731 /* A template-like identifier may be a partial concept id. */
15732 else if (flag_concepts
15733 && (template_id = (cp_parser_maybe_partial_concept_id
15734 (parser, templ, arguments))))
15735 return template_id;
15736 else if (variable_template_p (templ))
15738 template_id = lookup_template_variable (templ, arguments);
15739 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15740 SET_EXPR_LOCATION (template_id, combined_loc);
15742 else
15744 /* If it's not a class-template or a template-template, it should be
15745 a function-template. */
15746 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15747 || TREE_CODE (templ) == OVERLOAD
15748 || BASELINK_P (templ)));
15750 template_id = lookup_template_function (templ, arguments);
15751 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15752 SET_EXPR_LOCATION (template_id, combined_loc);
15755 /* If parsing tentatively, replace the sequence of tokens that makes
15756 up the template-id with a CPP_TEMPLATE_ID token. That way,
15757 should we re-parse the token stream, we will not have to repeat
15758 the effort required to do the parse, nor will we issue duplicate
15759 error messages about problems during instantiation of the
15760 template. */
15761 if (start_of_id
15762 /* Don't do this if we had a parse error in a declarator; re-parsing
15763 might succeed if a name changes meaning (60361). */
15764 && !(cp_parser_error_occurred (parser)
15765 && cp_parser_parsing_tentatively (parser)
15766 && parser->in_declarator_p))
15768 /* Reset the contents of the START_OF_ID token. */
15769 token->type = CPP_TEMPLATE_ID;
15770 token->location = combined_loc;
15772 /* We must mark the lookup as kept, so we don't throw it away on
15773 the first parse. */
15774 if (is_overloaded_fn (template_id))
15775 lookup_keep (get_fns (template_id), true);
15777 /* Retrieve any deferred checks. Do not pop this access checks yet
15778 so the memory will not be reclaimed during token replacing below. */
15779 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15780 token->u.tree_check_value->value = template_id;
15781 token->u.tree_check_value->checks = get_deferred_access_checks ();
15782 token->keyword = RID_MAX;
15784 /* Purge all subsequent tokens. */
15785 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15787 /* ??? Can we actually assume that, if template_id ==
15788 error_mark_node, we will have issued a diagnostic to the
15789 user, as opposed to simply marking the tentative parse as
15790 failed? */
15791 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15792 error_at (token->location, "parse error in template argument list");
15795 pop_to_parent_deferring_access_checks ();
15796 return template_id;
15799 /* Parse a template-name.
15801 template-name:
15802 identifier
15804 The standard should actually say:
15806 template-name:
15807 identifier
15808 operator-function-id
15810 A defect report has been filed about this issue.
15812 A conversion-function-id cannot be a template name because they cannot
15813 be part of a template-id. In fact, looking at this code:
15815 a.operator K<int>()
15817 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15818 It is impossible to call a templated conversion-function-id with an
15819 explicit argument list, since the only allowed template parameter is
15820 the type to which it is converting.
15822 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15823 `template' keyword, in a construction like:
15825 T::template f<3>()
15827 In that case `f' is taken to be a template-name, even though there
15828 is no way of knowing for sure.
15830 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15831 name refers to a set of overloaded functions, at least one of which
15832 is a template, or an IDENTIFIER_NODE with the name of the template,
15833 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15834 names are looked up inside uninstantiated templates. */
15836 static tree
15837 cp_parser_template_name (cp_parser* parser,
15838 bool template_keyword_p,
15839 bool check_dependency_p,
15840 bool is_declaration,
15841 enum tag_types tag_type,
15842 bool *is_identifier)
15844 tree identifier;
15845 tree decl;
15846 cp_token *token = cp_lexer_peek_token (parser->lexer);
15848 /* If the next token is `operator', then we have either an
15849 operator-function-id or a conversion-function-id. */
15850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15852 /* We don't know whether we're looking at an
15853 operator-function-id or a conversion-function-id. */
15854 cp_parser_parse_tentatively (parser);
15855 /* Try an operator-function-id. */
15856 identifier = cp_parser_operator_function_id (parser);
15857 /* If that didn't work, try a conversion-function-id. */
15858 if (!cp_parser_parse_definitely (parser))
15860 cp_parser_error (parser, "expected template-name");
15861 return error_mark_node;
15864 /* Look for the identifier. */
15865 else
15866 identifier = cp_parser_identifier (parser);
15868 /* If we didn't find an identifier, we don't have a template-id. */
15869 if (identifier == error_mark_node)
15870 return error_mark_node;
15872 /* If the name immediately followed the `template' keyword, then it
15873 is a template-name. However, if the next token is not `<', then
15874 we do not treat it as a template-name, since it is not being used
15875 as part of a template-id. This enables us to handle constructs
15876 like:
15878 template <typename T> struct S { S(); };
15879 template <typename T> S<T>::S();
15881 correctly. We would treat `S' as a template -- if it were `S<T>'
15882 -- but we do not if there is no `<'. */
15884 if (processing_template_decl
15885 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15887 /* In a declaration, in a dependent context, we pretend that the
15888 "template" keyword was present in order to improve error
15889 recovery. For example, given:
15891 template <typename T> void f(T::X<int>);
15893 we want to treat "X<int>" as a template-id. */
15894 if (is_declaration
15895 && !template_keyword_p
15896 && parser->scope && TYPE_P (parser->scope)
15897 && check_dependency_p
15898 && dependent_scope_p (parser->scope)
15899 /* Do not do this for dtors (or ctors), since they never
15900 need the template keyword before their name. */
15901 && !constructor_name_p (identifier, parser->scope))
15903 cp_token_position start = 0;
15905 /* Explain what went wrong. */
15906 error_at (token->location, "non-template %qD used as template",
15907 identifier);
15908 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15909 parser->scope, identifier);
15910 /* If parsing tentatively, find the location of the "<" token. */
15911 if (cp_parser_simulate_error (parser))
15912 start = cp_lexer_token_position (parser->lexer, true);
15913 /* Parse the template arguments so that we can issue error
15914 messages about them. */
15915 cp_lexer_consume_token (parser->lexer);
15916 cp_parser_enclosed_template_argument_list (parser);
15917 /* Skip tokens until we find a good place from which to
15918 continue parsing. */
15919 cp_parser_skip_to_closing_parenthesis (parser,
15920 /*recovering=*/true,
15921 /*or_comma=*/true,
15922 /*consume_paren=*/false);
15923 /* If parsing tentatively, permanently remove the
15924 template argument list. That will prevent duplicate
15925 error messages from being issued about the missing
15926 "template" keyword. */
15927 if (start)
15928 cp_lexer_purge_tokens_after (parser->lexer, start);
15929 if (is_identifier)
15930 *is_identifier = true;
15931 parser->context->object_type = NULL_TREE;
15932 return identifier;
15935 /* If the "template" keyword is present, then there is generally
15936 no point in doing name-lookup, so we just return IDENTIFIER.
15937 But, if the qualifying scope is non-dependent then we can
15938 (and must) do name-lookup normally. */
15939 if (template_keyword_p)
15941 tree scope = (parser->scope ? parser->scope
15942 : parser->context->object_type);
15943 if (scope && TYPE_P (scope)
15944 && (!CLASS_TYPE_P (scope)
15945 || (check_dependency_p && dependent_type_p (scope))))
15947 /* We're optimizing away the call to cp_parser_lookup_name, but
15948 we still need to do this. */
15949 parser->context->object_type = NULL_TREE;
15950 return identifier;
15955 /* Look up the name. */
15956 decl = cp_parser_lookup_name (parser, identifier,
15957 tag_type,
15958 /*is_template=*/true,
15959 /*is_namespace=*/false,
15960 check_dependency_p,
15961 /*ambiguous_decls=*/NULL,
15962 token->location);
15964 decl = strip_using_decl (decl);
15966 /* If DECL is a template, then the name was a template-name. */
15967 if (TREE_CODE (decl) == TEMPLATE_DECL)
15969 if (TREE_DEPRECATED (decl)
15970 && deprecated_state != DEPRECATED_SUPPRESS)
15971 warn_deprecated_use (decl, NULL_TREE);
15973 else
15975 /* The standard does not explicitly indicate whether a name that
15976 names a set of overloaded declarations, some of which are
15977 templates, is a template-name. However, such a name should
15978 be a template-name; otherwise, there is no way to form a
15979 template-id for the overloaded templates. */
15980 bool found = false;
15982 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15983 !found && iter; ++iter)
15984 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15985 found = true;
15987 if (!found)
15989 /* The name does not name a template. */
15990 cp_parser_error (parser, "expected template-name");
15991 return error_mark_node;
15995 /* If DECL is dependent, and refers to a function, then just return
15996 its name; we will look it up again during template instantiation. */
15997 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15999 tree scope = ovl_scope (decl);
16000 if (TYPE_P (scope) && dependent_type_p (scope))
16001 return identifier;
16004 return decl;
16007 /* Parse a template-argument-list.
16009 template-argument-list:
16010 template-argument ... [opt]
16011 template-argument-list , template-argument ... [opt]
16013 Returns a TREE_VEC containing the arguments. */
16015 static tree
16016 cp_parser_template_argument_list (cp_parser* parser)
16018 tree fixed_args[10];
16019 unsigned n_args = 0;
16020 unsigned alloced = 10;
16021 tree *arg_ary = fixed_args;
16022 tree vec;
16023 bool saved_in_template_argument_list_p;
16024 bool saved_ice_p;
16025 bool saved_non_ice_p;
16027 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16028 parser->in_template_argument_list_p = true;
16029 /* Even if the template-id appears in an integral
16030 constant-expression, the contents of the argument list do
16031 not. */
16032 saved_ice_p = parser->integral_constant_expression_p;
16033 parser->integral_constant_expression_p = false;
16034 saved_non_ice_p = parser->non_integral_constant_expression_p;
16035 parser->non_integral_constant_expression_p = false;
16037 /* Parse the arguments. */
16040 tree argument;
16042 if (n_args)
16043 /* Consume the comma. */
16044 cp_lexer_consume_token (parser->lexer);
16046 /* Parse the template-argument. */
16047 argument = cp_parser_template_argument (parser);
16049 /* If the next token is an ellipsis, we're expanding a template
16050 argument pack. */
16051 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16053 if (argument == error_mark_node)
16055 cp_token *token = cp_lexer_peek_token (parser->lexer);
16056 error_at (token->location,
16057 "expected parameter pack before %<...%>");
16059 /* Consume the `...' token. */
16060 cp_lexer_consume_token (parser->lexer);
16062 /* Make the argument into a TYPE_PACK_EXPANSION or
16063 EXPR_PACK_EXPANSION. */
16064 argument = make_pack_expansion (argument);
16067 if (n_args == alloced)
16069 alloced *= 2;
16071 if (arg_ary == fixed_args)
16073 arg_ary = XNEWVEC (tree, alloced);
16074 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16076 else
16077 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16079 arg_ary[n_args++] = argument;
16081 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16083 vec = make_tree_vec (n_args);
16085 while (n_args--)
16086 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16088 if (arg_ary != fixed_args)
16089 free (arg_ary);
16090 parser->non_integral_constant_expression_p = saved_non_ice_p;
16091 parser->integral_constant_expression_p = saved_ice_p;
16092 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16093 if (CHECKING_P)
16094 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16095 return vec;
16098 /* Parse a template-argument.
16100 template-argument:
16101 assignment-expression
16102 type-id
16103 id-expression
16105 The representation is that of an assignment-expression, type-id, or
16106 id-expression -- except that the qualified id-expression is
16107 evaluated, so that the value returned is either a DECL or an
16108 OVERLOAD.
16110 Although the standard says "assignment-expression", it forbids
16111 throw-expressions or assignments in the template argument.
16112 Therefore, we use "conditional-expression" instead. */
16114 static tree
16115 cp_parser_template_argument (cp_parser* parser)
16117 tree argument;
16118 bool template_p;
16119 bool address_p;
16120 bool maybe_type_id = false;
16121 cp_token *token = NULL, *argument_start_token = NULL;
16122 location_t loc = 0;
16123 cp_id_kind idk;
16125 /* There's really no way to know what we're looking at, so we just
16126 try each alternative in order.
16128 [temp.arg]
16130 In a template-argument, an ambiguity between a type-id and an
16131 expression is resolved to a type-id, regardless of the form of
16132 the corresponding template-parameter.
16134 Therefore, we try a type-id first. */
16135 cp_parser_parse_tentatively (parser);
16136 argument = cp_parser_template_type_arg (parser);
16137 /* If there was no error parsing the type-id but the next token is a
16138 '>>', our behavior depends on which dialect of C++ we're
16139 parsing. In C++98, we probably found a typo for '> >'. But there
16140 are type-id which are also valid expressions. For instance:
16142 struct X { int operator >> (int); };
16143 template <int V> struct Foo {};
16144 Foo<X () >> 5> r;
16146 Here 'X()' is a valid type-id of a function type, but the user just
16147 wanted to write the expression "X() >> 5". Thus, we remember that we
16148 found a valid type-id, but we still try to parse the argument as an
16149 expression to see what happens.
16151 In C++0x, the '>>' will be considered two separate '>'
16152 tokens. */
16153 if (!cp_parser_error_occurred (parser)
16154 && cxx_dialect == cxx98
16155 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16157 maybe_type_id = true;
16158 cp_parser_abort_tentative_parse (parser);
16160 else
16162 /* If the next token isn't a `,' or a `>', then this argument wasn't
16163 really finished. This means that the argument is not a valid
16164 type-id. */
16165 if (!cp_parser_next_token_ends_template_argument_p (parser))
16166 cp_parser_error (parser, "expected template-argument");
16167 /* If that worked, we're done. */
16168 if (cp_parser_parse_definitely (parser))
16169 return argument;
16171 /* We're still not sure what the argument will be. */
16172 cp_parser_parse_tentatively (parser);
16173 /* Try a template. */
16174 argument_start_token = cp_lexer_peek_token (parser->lexer);
16175 argument = cp_parser_id_expression (parser,
16176 /*template_keyword_p=*/false,
16177 /*check_dependency_p=*/true,
16178 &template_p,
16179 /*declarator_p=*/false,
16180 /*optional_p=*/false);
16181 /* If the next token isn't a `,' or a `>', then this argument wasn't
16182 really finished. */
16183 if (!cp_parser_next_token_ends_template_argument_p (parser))
16184 cp_parser_error (parser, "expected template-argument");
16185 if (!cp_parser_error_occurred (parser))
16187 /* Figure out what is being referred to. If the id-expression
16188 was for a class template specialization, then we will have a
16189 TYPE_DECL at this point. There is no need to do name lookup
16190 at this point in that case. */
16191 if (TREE_CODE (argument) != TYPE_DECL)
16192 argument = cp_parser_lookup_name (parser, argument,
16193 none_type,
16194 /*is_template=*/template_p,
16195 /*is_namespace=*/false,
16196 /*check_dependency=*/true,
16197 /*ambiguous_decls=*/NULL,
16198 argument_start_token->location);
16199 /* Handle a constrained-type-specifier for a non-type template
16200 parameter. */
16201 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16202 argument = decl;
16203 else if (TREE_CODE (argument) != TEMPLATE_DECL
16204 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16205 cp_parser_error (parser, "expected template-name");
16207 if (cp_parser_parse_definitely (parser))
16209 if (TREE_DEPRECATED (argument))
16210 warn_deprecated_use (argument, NULL_TREE);
16211 return argument;
16213 /* It must be a non-type argument. In C++17 any constant-expression is
16214 allowed. */
16215 if (cxx_dialect > cxx14)
16216 goto general_expr;
16218 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16220 -- an integral constant-expression of integral or enumeration
16221 type; or
16223 -- the name of a non-type template-parameter; or
16225 -- the name of an object or function with external linkage...
16227 -- the address of an object or function with external linkage...
16229 -- a pointer to member... */
16230 /* Look for a non-type template parameter. */
16231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16233 cp_parser_parse_tentatively (parser);
16234 argument = cp_parser_primary_expression (parser,
16235 /*address_p=*/false,
16236 /*cast_p=*/false,
16237 /*template_arg_p=*/true,
16238 &idk);
16239 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16240 || !cp_parser_next_token_ends_template_argument_p (parser))
16241 cp_parser_simulate_error (parser);
16242 if (cp_parser_parse_definitely (parser))
16243 return argument;
16246 /* If the next token is "&", the argument must be the address of an
16247 object or function with external linkage. */
16248 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16249 if (address_p)
16251 loc = cp_lexer_peek_token (parser->lexer)->location;
16252 cp_lexer_consume_token (parser->lexer);
16254 /* See if we might have an id-expression. */
16255 token = cp_lexer_peek_token (parser->lexer);
16256 if (token->type == CPP_NAME
16257 || token->keyword == RID_OPERATOR
16258 || token->type == CPP_SCOPE
16259 || token->type == CPP_TEMPLATE_ID
16260 || token->type == CPP_NESTED_NAME_SPECIFIER)
16262 cp_parser_parse_tentatively (parser);
16263 argument = cp_parser_primary_expression (parser,
16264 address_p,
16265 /*cast_p=*/false,
16266 /*template_arg_p=*/true,
16267 &idk);
16268 if (cp_parser_error_occurred (parser)
16269 || !cp_parser_next_token_ends_template_argument_p (parser))
16270 cp_parser_abort_tentative_parse (parser);
16271 else
16273 tree probe;
16275 if (INDIRECT_REF_P (argument))
16277 /* Strip the dereference temporarily. */
16278 gcc_assert (REFERENCE_REF_P (argument));
16279 argument = TREE_OPERAND (argument, 0);
16282 /* If we're in a template, we represent a qualified-id referring
16283 to a static data member as a SCOPE_REF even if the scope isn't
16284 dependent so that we can check access control later. */
16285 probe = argument;
16286 if (TREE_CODE (probe) == SCOPE_REF)
16287 probe = TREE_OPERAND (probe, 1);
16288 if (VAR_P (probe))
16290 /* A variable without external linkage might still be a
16291 valid constant-expression, so no error is issued here
16292 if the external-linkage check fails. */
16293 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16294 cp_parser_simulate_error (parser);
16296 else if (is_overloaded_fn (argument))
16297 /* All overloaded functions are allowed; if the external
16298 linkage test does not pass, an error will be issued
16299 later. */
16301 else if (address_p
16302 && (TREE_CODE (argument) == OFFSET_REF
16303 || TREE_CODE (argument) == SCOPE_REF))
16304 /* A pointer-to-member. */
16306 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16308 else
16309 cp_parser_simulate_error (parser);
16311 if (cp_parser_parse_definitely (parser))
16313 if (address_p)
16314 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16315 tf_warning_or_error);
16316 else
16317 argument = convert_from_reference (argument);
16318 return argument;
16322 /* If the argument started with "&", there are no other valid
16323 alternatives at this point. */
16324 if (address_p)
16326 cp_parser_error (parser, "invalid non-type template argument");
16327 return error_mark_node;
16330 general_expr:
16331 /* If the argument wasn't successfully parsed as a type-id followed
16332 by '>>', the argument can only be a constant expression now.
16333 Otherwise, we try parsing the constant-expression tentatively,
16334 because the argument could really be a type-id. */
16335 if (maybe_type_id)
16336 cp_parser_parse_tentatively (parser);
16338 if (cxx_dialect <= cxx14)
16339 argument = cp_parser_constant_expression (parser);
16340 else
16342 /* With C++17 generalized non-type template arguments we need to handle
16343 lvalue constant expressions, too. */
16344 argument = cp_parser_assignment_expression (parser);
16345 require_potential_constant_expression (argument);
16348 if (!maybe_type_id)
16349 return argument;
16350 if (!cp_parser_next_token_ends_template_argument_p (parser))
16351 cp_parser_error (parser, "expected template-argument");
16352 if (cp_parser_parse_definitely (parser))
16353 return argument;
16354 /* We did our best to parse the argument as a non type-id, but that
16355 was the only alternative that matched (albeit with a '>' after
16356 it). We can assume it's just a typo from the user, and a
16357 diagnostic will then be issued. */
16358 return cp_parser_template_type_arg (parser);
16361 /* Parse an explicit-instantiation.
16363 explicit-instantiation:
16364 template declaration
16366 Although the standard says `declaration', what it really means is:
16368 explicit-instantiation:
16369 template decl-specifier-seq [opt] declarator [opt] ;
16371 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16372 supposed to be allowed. A defect report has been filed about this
16373 issue.
16375 GNU Extension:
16377 explicit-instantiation:
16378 storage-class-specifier template
16379 decl-specifier-seq [opt] declarator [opt] ;
16380 function-specifier template
16381 decl-specifier-seq [opt] declarator [opt] ; */
16383 static void
16384 cp_parser_explicit_instantiation (cp_parser* parser)
16386 int declares_class_or_enum;
16387 cp_decl_specifier_seq decl_specifiers;
16388 tree extension_specifier = NULL_TREE;
16390 timevar_push (TV_TEMPLATE_INST);
16392 /* Look for an (optional) storage-class-specifier or
16393 function-specifier. */
16394 if (cp_parser_allow_gnu_extensions_p (parser))
16396 extension_specifier
16397 = cp_parser_storage_class_specifier_opt (parser);
16398 if (!extension_specifier)
16399 extension_specifier
16400 = cp_parser_function_specifier_opt (parser,
16401 /*decl_specs=*/NULL);
16404 /* Look for the `template' keyword. */
16405 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16406 /* Let the front end know that we are processing an explicit
16407 instantiation. */
16408 begin_explicit_instantiation ();
16409 /* [temp.explicit] says that we are supposed to ignore access
16410 control while processing explicit instantiation directives. */
16411 push_deferring_access_checks (dk_no_check);
16412 /* Parse a decl-specifier-seq. */
16413 cp_parser_decl_specifier_seq (parser,
16414 CP_PARSER_FLAGS_OPTIONAL,
16415 &decl_specifiers,
16416 &declares_class_or_enum);
16417 /* If there was exactly one decl-specifier, and it declared a class,
16418 and there's no declarator, then we have an explicit type
16419 instantiation. */
16420 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16422 tree type;
16424 type = check_tag_decl (&decl_specifiers,
16425 /*explicit_type_instantiation_p=*/true);
16426 /* Turn access control back on for names used during
16427 template instantiation. */
16428 pop_deferring_access_checks ();
16429 if (type)
16430 do_type_instantiation (type, extension_specifier,
16431 /*complain=*/tf_error);
16433 else
16435 cp_declarator *declarator;
16436 tree decl;
16438 /* Parse the declarator. */
16439 declarator
16440 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16441 /*ctor_dtor_or_conv_p=*/NULL,
16442 /*parenthesized_p=*/NULL,
16443 /*member_p=*/false,
16444 /*friend_p=*/false);
16445 if (declares_class_or_enum & 2)
16446 cp_parser_check_for_definition_in_return_type (declarator,
16447 decl_specifiers.type,
16448 decl_specifiers.locations[ds_type_spec]);
16449 if (declarator != cp_error_declarator)
16451 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16452 permerror (decl_specifiers.locations[ds_inline],
16453 "explicit instantiation shall not use"
16454 " %<inline%> specifier");
16455 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16456 permerror (decl_specifiers.locations[ds_constexpr],
16457 "explicit instantiation shall not use"
16458 " %<constexpr%> specifier");
16460 decl = grokdeclarator (declarator, &decl_specifiers,
16461 NORMAL, 0, &decl_specifiers.attributes);
16462 /* Turn access control back on for names used during
16463 template instantiation. */
16464 pop_deferring_access_checks ();
16465 /* Do the explicit instantiation. */
16466 do_decl_instantiation (decl, extension_specifier);
16468 else
16470 pop_deferring_access_checks ();
16471 /* Skip the body of the explicit instantiation. */
16472 cp_parser_skip_to_end_of_statement (parser);
16475 /* We're done with the instantiation. */
16476 end_explicit_instantiation ();
16478 cp_parser_consume_semicolon_at_end_of_statement (parser);
16480 timevar_pop (TV_TEMPLATE_INST);
16483 /* Parse an explicit-specialization.
16485 explicit-specialization:
16486 template < > declaration
16488 Although the standard says `declaration', what it really means is:
16490 explicit-specialization:
16491 template <> decl-specifier [opt] init-declarator [opt] ;
16492 template <> function-definition
16493 template <> explicit-specialization
16494 template <> template-declaration */
16496 static void
16497 cp_parser_explicit_specialization (cp_parser* parser)
16499 bool need_lang_pop;
16500 cp_token *token = cp_lexer_peek_token (parser->lexer);
16502 /* Look for the `template' keyword. */
16503 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16504 /* Look for the `<'. */
16505 cp_parser_require (parser, CPP_LESS, RT_LESS);
16506 /* Look for the `>'. */
16507 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16508 /* We have processed another parameter list. */
16509 ++parser->num_template_parameter_lists;
16510 /* [temp]
16512 A template ... explicit specialization ... shall not have C
16513 linkage. */
16514 if (current_lang_name == lang_name_c)
16516 error_at (token->location, "template specialization with C linkage");
16517 /* Give it C++ linkage to avoid confusing other parts of the
16518 front end. */
16519 push_lang_context (lang_name_cplusplus);
16520 need_lang_pop = true;
16522 else
16523 need_lang_pop = false;
16524 /* Let the front end know that we are beginning a specialization. */
16525 if (!begin_specialization ())
16527 end_specialization ();
16528 return;
16531 /* If the next keyword is `template', we need to figure out whether
16532 or not we're looking a template-declaration. */
16533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16535 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16536 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16537 cp_parser_template_declaration_after_export (parser,
16538 /*member_p=*/false);
16539 else
16540 cp_parser_explicit_specialization (parser);
16542 else
16543 /* Parse the dependent declaration. */
16544 cp_parser_single_declaration (parser,
16545 /*checks=*/NULL,
16546 /*member_p=*/false,
16547 /*explicit_specialization_p=*/true,
16548 /*friend_p=*/NULL);
16549 /* We're done with the specialization. */
16550 end_specialization ();
16551 /* For the erroneous case of a template with C linkage, we pushed an
16552 implicit C++ linkage scope; exit that scope now. */
16553 if (need_lang_pop)
16554 pop_lang_context ();
16555 /* We're done with this parameter list. */
16556 --parser->num_template_parameter_lists;
16559 /* Parse a type-specifier.
16561 type-specifier:
16562 simple-type-specifier
16563 class-specifier
16564 enum-specifier
16565 elaborated-type-specifier
16566 cv-qualifier
16568 GNU Extension:
16570 type-specifier:
16571 __complex__
16573 Returns a representation of the type-specifier. For a
16574 class-specifier, enum-specifier, or elaborated-type-specifier, a
16575 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16577 The parser flags FLAGS is used to control type-specifier parsing.
16579 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16580 in a decl-specifier-seq.
16582 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16583 class-specifier, enum-specifier, or elaborated-type-specifier, then
16584 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16585 if a type is declared; 2 if it is defined. Otherwise, it is set to
16586 zero.
16588 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16589 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16590 is set to FALSE. */
16592 static tree
16593 cp_parser_type_specifier (cp_parser* parser,
16594 cp_parser_flags flags,
16595 cp_decl_specifier_seq *decl_specs,
16596 bool is_declaration,
16597 int* declares_class_or_enum,
16598 bool* is_cv_qualifier)
16600 tree type_spec = NULL_TREE;
16601 cp_token *token;
16602 enum rid keyword;
16603 cp_decl_spec ds = ds_last;
16605 /* Assume this type-specifier does not declare a new type. */
16606 if (declares_class_or_enum)
16607 *declares_class_or_enum = 0;
16608 /* And that it does not specify a cv-qualifier. */
16609 if (is_cv_qualifier)
16610 *is_cv_qualifier = false;
16611 /* Peek at the next token. */
16612 token = cp_lexer_peek_token (parser->lexer);
16614 /* If we're looking at a keyword, we can use that to guide the
16615 production we choose. */
16616 keyword = token->keyword;
16617 switch (keyword)
16619 case RID_ENUM:
16620 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16621 goto elaborated_type_specifier;
16623 /* Look for the enum-specifier. */
16624 type_spec = cp_parser_enum_specifier (parser);
16625 /* If that worked, we're done. */
16626 if (type_spec)
16628 if (declares_class_or_enum)
16629 *declares_class_or_enum = 2;
16630 if (decl_specs)
16631 cp_parser_set_decl_spec_type (decl_specs,
16632 type_spec,
16633 token,
16634 /*type_definition_p=*/true);
16635 return type_spec;
16637 else
16638 goto elaborated_type_specifier;
16640 /* Any of these indicate either a class-specifier, or an
16641 elaborated-type-specifier. */
16642 case RID_CLASS:
16643 case RID_STRUCT:
16644 case RID_UNION:
16645 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16646 goto elaborated_type_specifier;
16648 /* Parse tentatively so that we can back up if we don't find a
16649 class-specifier. */
16650 cp_parser_parse_tentatively (parser);
16651 /* Look for the class-specifier. */
16652 type_spec = cp_parser_class_specifier (parser);
16653 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16654 /* If that worked, we're done. */
16655 if (cp_parser_parse_definitely (parser))
16657 if (declares_class_or_enum)
16658 *declares_class_or_enum = 2;
16659 if (decl_specs)
16660 cp_parser_set_decl_spec_type (decl_specs,
16661 type_spec,
16662 token,
16663 /*type_definition_p=*/true);
16664 return type_spec;
16667 /* Fall through. */
16668 elaborated_type_specifier:
16669 /* We're declaring (not defining) a class or enum. */
16670 if (declares_class_or_enum)
16671 *declares_class_or_enum = 1;
16673 /* Fall through. */
16674 case RID_TYPENAME:
16675 /* Look for an elaborated-type-specifier. */
16676 type_spec
16677 = (cp_parser_elaborated_type_specifier
16678 (parser,
16679 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16680 is_declaration));
16681 if (decl_specs)
16682 cp_parser_set_decl_spec_type (decl_specs,
16683 type_spec,
16684 token,
16685 /*type_definition_p=*/false);
16686 return type_spec;
16688 case RID_CONST:
16689 ds = ds_const;
16690 if (is_cv_qualifier)
16691 *is_cv_qualifier = true;
16692 break;
16694 case RID_VOLATILE:
16695 ds = ds_volatile;
16696 if (is_cv_qualifier)
16697 *is_cv_qualifier = true;
16698 break;
16700 case RID_RESTRICT:
16701 ds = ds_restrict;
16702 if (is_cv_qualifier)
16703 *is_cv_qualifier = true;
16704 break;
16706 case RID_COMPLEX:
16707 /* The `__complex__' keyword is a GNU extension. */
16708 ds = ds_complex;
16709 break;
16711 default:
16712 break;
16715 /* Handle simple keywords. */
16716 if (ds != ds_last)
16718 if (decl_specs)
16720 set_and_check_decl_spec_loc (decl_specs, ds, token);
16721 decl_specs->any_specifiers_p = true;
16723 return cp_lexer_consume_token (parser->lexer)->u.value;
16726 /* If we do not already have a type-specifier, assume we are looking
16727 at a simple-type-specifier. */
16728 type_spec = cp_parser_simple_type_specifier (parser,
16729 decl_specs,
16730 flags);
16732 /* If we didn't find a type-specifier, and a type-specifier was not
16733 optional in this context, issue an error message. */
16734 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16736 cp_parser_error (parser, "expected type specifier");
16737 return error_mark_node;
16740 return type_spec;
16743 /* Parse a simple-type-specifier.
16745 simple-type-specifier:
16746 :: [opt] nested-name-specifier [opt] type-name
16747 :: [opt] nested-name-specifier template template-id
16748 char
16749 wchar_t
16750 bool
16751 short
16753 long
16754 signed
16755 unsigned
16756 float
16757 double
16758 void
16760 C++11 Extension:
16762 simple-type-specifier:
16763 auto
16764 decltype ( expression )
16765 char16_t
16766 char32_t
16767 __underlying_type ( type-id )
16769 C++17 extension:
16771 nested-name-specifier(opt) template-name
16773 GNU Extension:
16775 simple-type-specifier:
16776 __int128
16777 __typeof__ unary-expression
16778 __typeof__ ( type-id )
16779 __typeof__ ( type-id ) { initializer-list , [opt] }
16781 Concepts Extension:
16783 simple-type-specifier:
16784 constrained-type-specifier
16786 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16787 appropriately updated. */
16789 static tree
16790 cp_parser_simple_type_specifier (cp_parser* parser,
16791 cp_decl_specifier_seq *decl_specs,
16792 cp_parser_flags flags)
16794 tree type = NULL_TREE;
16795 cp_token *token;
16796 int idx;
16798 /* Peek at the next token. */
16799 token = cp_lexer_peek_token (parser->lexer);
16801 /* If we're looking at a keyword, things are easy. */
16802 switch (token->keyword)
16804 case RID_CHAR:
16805 if (decl_specs)
16806 decl_specs->explicit_char_p = true;
16807 type = char_type_node;
16808 break;
16809 case RID_CHAR16:
16810 type = char16_type_node;
16811 break;
16812 case RID_CHAR32:
16813 type = char32_type_node;
16814 break;
16815 case RID_WCHAR:
16816 type = wchar_type_node;
16817 break;
16818 case RID_BOOL:
16819 type = boolean_type_node;
16820 break;
16821 case RID_SHORT:
16822 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16823 type = short_integer_type_node;
16824 break;
16825 case RID_INT:
16826 if (decl_specs)
16827 decl_specs->explicit_int_p = true;
16828 type = integer_type_node;
16829 break;
16830 case RID_INT_N_0:
16831 case RID_INT_N_1:
16832 case RID_INT_N_2:
16833 case RID_INT_N_3:
16834 idx = token->keyword - RID_INT_N_0;
16835 if (! int_n_enabled_p [idx])
16836 break;
16837 if (decl_specs)
16839 decl_specs->explicit_intN_p = true;
16840 decl_specs->int_n_idx = idx;
16842 type = int_n_trees [idx].signed_type;
16843 break;
16844 case RID_LONG:
16845 if (decl_specs)
16846 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16847 type = long_integer_type_node;
16848 break;
16849 case RID_SIGNED:
16850 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16851 type = integer_type_node;
16852 break;
16853 case RID_UNSIGNED:
16854 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16855 type = unsigned_type_node;
16856 break;
16857 case RID_FLOAT:
16858 type = float_type_node;
16859 break;
16860 case RID_DOUBLE:
16861 type = double_type_node;
16862 break;
16863 case RID_VOID:
16864 type = void_type_node;
16865 break;
16867 case RID_AUTO:
16868 maybe_warn_cpp0x (CPP0X_AUTO);
16869 if (parser->auto_is_implicit_function_template_parm_p)
16871 /* The 'auto' might be the placeholder return type for a function decl
16872 with trailing return type. */
16873 bool have_trailing_return_fn_decl = false;
16875 cp_parser_parse_tentatively (parser);
16876 cp_lexer_consume_token (parser->lexer);
16877 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16878 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16879 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16880 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16882 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16884 cp_lexer_consume_token (parser->lexer);
16885 cp_parser_skip_to_closing_parenthesis (parser,
16886 /*recovering*/false,
16887 /*or_comma*/false,
16888 /*consume_paren*/true);
16889 continue;
16892 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16894 have_trailing_return_fn_decl = true;
16895 break;
16898 cp_lexer_consume_token (parser->lexer);
16900 cp_parser_abort_tentative_parse (parser);
16902 if (have_trailing_return_fn_decl)
16904 type = make_auto ();
16905 break;
16908 if (cxx_dialect >= cxx14)
16910 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16911 type = TREE_TYPE (type);
16913 else
16914 type = error_mark_node;
16916 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16918 if (cxx_dialect < cxx14)
16919 error_at (token->location,
16920 "use of %<auto%> in lambda parameter declaration "
16921 "only available with "
16922 "-std=c++14 or -std=gnu++14");
16924 else if (cxx_dialect < cxx14)
16925 error_at (token->location,
16926 "use of %<auto%> in parameter declaration "
16927 "only available with "
16928 "-std=c++14 or -std=gnu++14");
16929 else if (!flag_concepts)
16930 pedwarn (token->location, OPT_Wpedantic,
16931 "ISO C++ forbids use of %<auto%> in parameter "
16932 "declaration");
16934 else
16935 type = make_auto ();
16936 break;
16938 case RID_DECLTYPE:
16939 /* Since DR 743, decltype can either be a simple-type-specifier by
16940 itself or begin a nested-name-specifier. Parsing it will replace
16941 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16942 handling below decide what to do. */
16943 cp_parser_decltype (parser);
16944 cp_lexer_set_token_position (parser->lexer, token);
16945 break;
16947 case RID_TYPEOF:
16948 /* Consume the `typeof' token. */
16949 cp_lexer_consume_token (parser->lexer);
16950 /* Parse the operand to `typeof'. */
16951 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16952 /* If it is not already a TYPE, take its type. */
16953 if (!TYPE_P (type))
16954 type = finish_typeof (type);
16956 if (decl_specs)
16957 cp_parser_set_decl_spec_type (decl_specs, type,
16958 token,
16959 /*type_definition_p=*/false);
16961 return type;
16963 case RID_UNDERLYING_TYPE:
16964 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16965 if (decl_specs)
16966 cp_parser_set_decl_spec_type (decl_specs, type,
16967 token,
16968 /*type_definition_p=*/false);
16970 return type;
16972 case RID_BASES:
16973 case RID_DIRECT_BASES:
16974 type = cp_parser_trait_expr (parser, token->keyword);
16975 if (decl_specs)
16976 cp_parser_set_decl_spec_type (decl_specs, type,
16977 token,
16978 /*type_definition_p=*/false);
16979 return type;
16980 default:
16981 break;
16984 /* If token is an already-parsed decltype not followed by ::,
16985 it's a simple-type-specifier. */
16986 if (token->type == CPP_DECLTYPE
16987 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16989 type = saved_checks_value (token->u.tree_check_value);
16990 if (decl_specs)
16992 cp_parser_set_decl_spec_type (decl_specs, type,
16993 token,
16994 /*type_definition_p=*/false);
16995 /* Remember that we are handling a decltype in order to
16996 implement the resolution of DR 1510 when the argument
16997 isn't instantiation dependent. */
16998 decl_specs->decltype_p = true;
17000 cp_lexer_consume_token (parser->lexer);
17001 return type;
17004 /* If the type-specifier was for a built-in type, we're done. */
17005 if (type)
17007 /* Record the type. */
17008 if (decl_specs
17009 && (token->keyword != RID_SIGNED
17010 && token->keyword != RID_UNSIGNED
17011 && token->keyword != RID_SHORT
17012 && token->keyword != RID_LONG))
17013 cp_parser_set_decl_spec_type (decl_specs,
17014 type,
17015 token,
17016 /*type_definition_p=*/false);
17017 if (decl_specs)
17018 decl_specs->any_specifiers_p = true;
17020 /* Consume the token. */
17021 cp_lexer_consume_token (parser->lexer);
17023 if (type == error_mark_node)
17024 return error_mark_node;
17026 /* There is no valid C++ program where a non-template type is
17027 followed by a "<". That usually indicates that the user thought
17028 that the type was a template. */
17029 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17030 token->location);
17032 return TYPE_NAME (type);
17035 /* The type-specifier must be a user-defined type. */
17036 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17038 bool qualified_p;
17039 bool global_p;
17041 /* Don't gobble tokens or issue error messages if this is an
17042 optional type-specifier. */
17043 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17044 cp_parser_parse_tentatively (parser);
17046 token = cp_lexer_peek_token (parser->lexer);
17048 /* Look for the optional `::' operator. */
17049 global_p
17050 = (cp_parser_global_scope_opt (parser,
17051 /*current_scope_valid_p=*/false)
17052 != NULL_TREE);
17053 /* Look for the nested-name specifier. */
17054 qualified_p
17055 = (cp_parser_nested_name_specifier_opt (parser,
17056 /*typename_keyword_p=*/false,
17057 /*check_dependency_p=*/true,
17058 /*type_p=*/false,
17059 /*is_declaration=*/false)
17060 != NULL_TREE);
17061 /* If we have seen a nested-name-specifier, and the next token
17062 is `template', then we are using the template-id production. */
17063 if (parser->scope
17064 && cp_parser_optional_template_keyword (parser))
17066 /* Look for the template-id. */
17067 type = cp_parser_template_id (parser,
17068 /*template_keyword_p=*/true,
17069 /*check_dependency_p=*/true,
17070 none_type,
17071 /*is_declaration=*/false);
17072 /* If the template-id did not name a type, we are out of
17073 luck. */
17074 if (TREE_CODE (type) != TYPE_DECL)
17076 cp_parser_error (parser, "expected template-id for type");
17077 type = NULL_TREE;
17080 /* Otherwise, look for a type-name. */
17081 else
17082 type = cp_parser_type_name (parser);
17083 /* Keep track of all name-lookups performed in class scopes. */
17084 if (type
17085 && !global_p
17086 && !qualified_p
17087 && TREE_CODE (type) == TYPE_DECL
17088 && identifier_p (DECL_NAME (type)))
17089 maybe_note_name_used_in_class (DECL_NAME (type), type);
17090 /* If it didn't work out, we don't have a TYPE. */
17091 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17092 && !cp_parser_parse_definitely (parser))
17093 type = NULL_TREE;
17094 if (!type && cxx_dialect >= cxx1z)
17096 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17097 cp_parser_parse_tentatively (parser);
17099 cp_parser_global_scope_opt (parser,
17100 /*current_scope_valid_p=*/false);
17101 cp_parser_nested_name_specifier_opt (parser,
17102 /*typename_keyword_p=*/false,
17103 /*check_dependency_p=*/true,
17104 /*type_p=*/false,
17105 /*is_declaration=*/false);
17106 tree name = cp_parser_identifier (parser);
17107 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17108 && parser->scope != error_mark_node)
17110 tree tmpl = cp_parser_lookup_name (parser, name,
17111 none_type,
17112 /*is_template=*/false,
17113 /*is_namespace=*/false,
17114 /*check_dependency=*/true,
17115 /*ambiguous_decls=*/NULL,
17116 token->location);
17117 if (tmpl && tmpl != error_mark_node
17118 && (DECL_CLASS_TEMPLATE_P (tmpl)
17119 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17120 type = make_template_placeholder (tmpl);
17121 else
17123 type = error_mark_node;
17124 if (!cp_parser_simulate_error (parser))
17125 cp_parser_name_lookup_error (parser, name, tmpl,
17126 NLE_TYPE, token->location);
17129 else
17130 type = error_mark_node;
17132 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17133 && !cp_parser_parse_definitely (parser))
17134 type = NULL_TREE;
17136 if (type && decl_specs)
17137 cp_parser_set_decl_spec_type (decl_specs, type,
17138 token,
17139 /*type_definition_p=*/false);
17142 /* If we didn't get a type-name, issue an error message. */
17143 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17145 cp_parser_error (parser, "expected type-name");
17146 return error_mark_node;
17149 if (type && type != error_mark_node)
17151 /* See if TYPE is an Objective-C type, and if so, parse and
17152 accept any protocol references following it. Do this before
17153 the cp_parser_check_for_invalid_template_id() call, because
17154 Objective-C types can be followed by '<...>' which would
17155 enclose protocol names rather than template arguments, and so
17156 everything is fine. */
17157 if (c_dialect_objc () && !parser->scope
17158 && (objc_is_id (type) || objc_is_class_name (type)))
17160 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17161 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17163 /* Clobber the "unqualified" type previously entered into
17164 DECL_SPECS with the new, improved protocol-qualified version. */
17165 if (decl_specs)
17166 decl_specs->type = qual_type;
17168 return qual_type;
17171 /* There is no valid C++ program where a non-template type is
17172 followed by a "<". That usually indicates that the user
17173 thought that the type was a template. */
17174 cp_parser_check_for_invalid_template_id (parser, type,
17175 none_type,
17176 token->location);
17179 return type;
17182 /* Parse a type-name.
17184 type-name:
17185 class-name
17186 enum-name
17187 typedef-name
17188 simple-template-id [in c++0x]
17190 enum-name:
17191 identifier
17193 typedef-name:
17194 identifier
17196 Concepts:
17198 type-name:
17199 concept-name
17200 partial-concept-id
17202 concept-name:
17203 identifier
17205 Returns a TYPE_DECL for the type. */
17207 static tree
17208 cp_parser_type_name (cp_parser* parser)
17210 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17213 /* See above. */
17214 static tree
17215 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17217 tree type_decl;
17219 /* We can't know yet whether it is a class-name or not. */
17220 cp_parser_parse_tentatively (parser);
17221 /* Try a class-name. */
17222 type_decl = cp_parser_class_name (parser,
17223 typename_keyword_p,
17224 /*template_keyword_p=*/false,
17225 none_type,
17226 /*check_dependency_p=*/true,
17227 /*class_head_p=*/false,
17228 /*is_declaration=*/false);
17229 /* If it's not a class-name, keep looking. */
17230 if (!cp_parser_parse_definitely (parser))
17232 if (cxx_dialect < cxx11)
17233 /* It must be a typedef-name or an enum-name. */
17234 return cp_parser_nonclass_name (parser);
17236 cp_parser_parse_tentatively (parser);
17237 /* It is either a simple-template-id representing an
17238 instantiation of an alias template... */
17239 type_decl = cp_parser_template_id (parser,
17240 /*template_keyword_p=*/false,
17241 /*check_dependency_p=*/true,
17242 none_type,
17243 /*is_declaration=*/false);
17244 /* Note that this must be an instantiation of an alias template
17245 because [temp.names]/6 says:
17247 A template-id that names an alias template specialization
17248 is a type-name.
17250 Whereas [temp.names]/7 says:
17252 A simple-template-id that names a class template
17253 specialization is a class-name.
17255 With concepts, this could also be a partial-concept-id that
17256 declares a non-type template parameter. */
17257 if (type_decl != NULL_TREE
17258 && TREE_CODE (type_decl) == TYPE_DECL
17259 && TYPE_DECL_ALIAS_P (type_decl))
17260 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17261 else if (is_constrained_parameter (type_decl))
17262 /* Don't do anything. */ ;
17263 else
17264 cp_parser_simulate_error (parser);
17266 if (!cp_parser_parse_definitely (parser))
17267 /* ... Or a typedef-name or an enum-name. */
17268 return cp_parser_nonclass_name (parser);
17271 return type_decl;
17274 /* Check if DECL and ARGS can form a constrained-type-specifier.
17275 If ARGS is non-null, we try to form a concept check of the
17276 form DECL<?, ARGS> where ? is a wildcard that matches any
17277 kind of template argument. If ARGS is NULL, then we try to
17278 form a concept check of the form DECL<?>. */
17280 static tree
17281 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17282 tree decl, tree args)
17284 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17286 /* If we a constrained-type-specifier cannot be deduced. */
17287 if (parser->prevent_constrained_type_specifiers)
17288 return NULL_TREE;
17290 /* A constrained type specifier can only be found in an
17291 overload set or as a reference to a template declaration.
17293 FIXME: This might be masking a bug. It's possible that
17294 that the deduction below is causing template specializations
17295 to be formed with the wildcard as an argument. */
17296 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17297 return NULL_TREE;
17299 /* Try to build a call expression that evaluates the
17300 concept. This can fail if the overload set refers
17301 only to non-templates. */
17302 tree placeholder = build_nt (WILDCARD_DECL);
17303 tree check = build_concept_check (decl, placeholder, args);
17304 if (check == error_mark_node)
17305 return NULL_TREE;
17307 /* Deduce the checked constraint and the prototype parameter.
17309 FIXME: In certain cases, failure to deduce should be a
17310 diagnosable error. */
17311 tree conc;
17312 tree proto;
17313 if (!deduce_constrained_parameter (check, conc, proto))
17314 return NULL_TREE;
17316 /* In template parameter scope, this results in a constrained
17317 parameter. Return a descriptor of that parm. */
17318 if (processing_template_parmlist)
17319 return build_constrained_parameter (conc, proto, args);
17321 /* In a parameter-declaration-clause, constrained-type
17322 specifiers result in invented template parameters. */
17323 if (parser->auto_is_implicit_function_template_parm_p)
17325 tree x = build_constrained_parameter (conc, proto, args);
17326 return synthesize_implicit_template_parm (parser, x);
17328 else
17330 /* Otherwise, we're in a context where the constrained
17331 type name is deduced and the constraint applies
17332 after deduction. */
17333 return make_constrained_auto (conc, args);
17336 return NULL_TREE;
17339 /* If DECL refers to a concept, return a TYPE_DECL representing
17340 the result of using the constrained type specifier in the
17341 current context. DECL refers to a concept if
17343 - it is an overload set containing a function concept taking a single
17344 type argument, or
17346 - it is a variable concept taking a single type argument. */
17348 static tree
17349 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17351 if (flag_concepts
17352 && (TREE_CODE (decl) == OVERLOAD
17353 || BASELINK_P (decl)
17354 || variable_concept_p (decl)))
17355 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17356 else
17357 return NULL_TREE;
17360 /* Check if DECL and ARGS form a partial-concept-id. If so,
17361 assign ID to the resulting constrained placeholder.
17363 Returns true if the partial-concept-id designates a placeholder
17364 and false otherwise. Note that *id is set to NULL_TREE in
17365 this case. */
17367 static tree
17368 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17370 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17373 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17374 or a concept-name.
17376 enum-name:
17377 identifier
17379 typedef-name:
17380 identifier
17382 concept-name:
17383 identifier
17385 Returns a TYPE_DECL for the type. */
17387 static tree
17388 cp_parser_nonclass_name (cp_parser* parser)
17390 tree type_decl;
17391 tree identifier;
17393 cp_token *token = cp_lexer_peek_token (parser->lexer);
17394 identifier = cp_parser_identifier (parser);
17395 if (identifier == error_mark_node)
17396 return error_mark_node;
17398 /* Look up the type-name. */
17399 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17401 type_decl = strip_using_decl (type_decl);
17403 /* If we found an overload set, then it may refer to a concept-name. */
17404 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17405 type_decl = decl;
17407 if (TREE_CODE (type_decl) != TYPE_DECL
17408 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17410 /* See if this is an Objective-C type. */
17411 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17412 tree type = objc_get_protocol_qualified_type (identifier, protos);
17413 if (type)
17414 type_decl = TYPE_NAME (type);
17417 /* Issue an error if we did not find a type-name. */
17418 if (TREE_CODE (type_decl) != TYPE_DECL
17419 /* In Objective-C, we have the complication that class names are
17420 normally type names and start declarations (eg, the
17421 "NSObject" in "NSObject *object;"), but can be used in an
17422 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17423 is an expression. So, a classname followed by a dot is not a
17424 valid type-name. */
17425 || (objc_is_class_name (TREE_TYPE (type_decl))
17426 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17428 if (!cp_parser_simulate_error (parser))
17429 cp_parser_name_lookup_error (parser, identifier, type_decl,
17430 NLE_TYPE, token->location);
17431 return error_mark_node;
17433 /* Remember that the name was used in the definition of the
17434 current class so that we can check later to see if the
17435 meaning would have been different after the class was
17436 entirely defined. */
17437 else if (type_decl != error_mark_node
17438 && !parser->scope)
17439 maybe_note_name_used_in_class (identifier, type_decl);
17441 return type_decl;
17444 /* Parse an elaborated-type-specifier. Note that the grammar given
17445 here incorporates the resolution to DR68.
17447 elaborated-type-specifier:
17448 class-key :: [opt] nested-name-specifier [opt] identifier
17449 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17450 enum-key :: [opt] nested-name-specifier [opt] identifier
17451 typename :: [opt] nested-name-specifier identifier
17452 typename :: [opt] nested-name-specifier template [opt]
17453 template-id
17455 GNU extension:
17457 elaborated-type-specifier:
17458 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17459 class-key attributes :: [opt] nested-name-specifier [opt]
17460 template [opt] template-id
17461 enum attributes :: [opt] nested-name-specifier [opt] identifier
17463 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17464 declared `friend'. If IS_DECLARATION is TRUE, then this
17465 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17466 something is being declared.
17468 Returns the TYPE specified. */
17470 static tree
17471 cp_parser_elaborated_type_specifier (cp_parser* parser,
17472 bool is_friend,
17473 bool is_declaration)
17475 enum tag_types tag_type;
17476 tree identifier;
17477 tree type = NULL_TREE;
17478 tree attributes = NULL_TREE;
17479 tree globalscope;
17480 cp_token *token = NULL;
17482 /* See if we're looking at the `enum' keyword. */
17483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17485 /* Consume the `enum' token. */
17486 cp_lexer_consume_token (parser->lexer);
17487 /* Remember that it's an enumeration type. */
17488 tag_type = enum_type;
17489 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17490 enums) is used here. */
17491 cp_token *token = cp_lexer_peek_token (parser->lexer);
17492 if (cp_parser_is_keyword (token, RID_CLASS)
17493 || cp_parser_is_keyword (token, RID_STRUCT))
17495 gcc_rich_location richloc (token->location);
17496 richloc.add_range (input_location, false);
17497 richloc.add_fixit_remove ();
17498 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17499 "a scoped enum must not use the %qD keyword",
17500 token->u.value);
17501 /* Consume the `struct' or `class' and parse it anyway. */
17502 cp_lexer_consume_token (parser->lexer);
17504 /* Parse the attributes. */
17505 attributes = cp_parser_attributes_opt (parser);
17507 /* Or, it might be `typename'. */
17508 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17509 RID_TYPENAME))
17511 /* Consume the `typename' token. */
17512 cp_lexer_consume_token (parser->lexer);
17513 /* Remember that it's a `typename' type. */
17514 tag_type = typename_type;
17516 /* Otherwise it must be a class-key. */
17517 else
17519 tag_type = cp_parser_class_key (parser);
17520 if (tag_type == none_type)
17521 return error_mark_node;
17522 /* Parse the attributes. */
17523 attributes = cp_parser_attributes_opt (parser);
17526 /* Look for the `::' operator. */
17527 globalscope = cp_parser_global_scope_opt (parser,
17528 /*current_scope_valid_p=*/false);
17529 /* Look for the nested-name-specifier. */
17530 tree nested_name_specifier;
17531 if (tag_type == typename_type && !globalscope)
17533 nested_name_specifier
17534 = cp_parser_nested_name_specifier (parser,
17535 /*typename_keyword_p=*/true,
17536 /*check_dependency_p=*/true,
17537 /*type_p=*/true,
17538 is_declaration);
17539 if (!nested_name_specifier)
17540 return error_mark_node;
17542 else
17543 /* Even though `typename' is not present, the proposed resolution
17544 to Core Issue 180 says that in `class A<T>::B', `B' should be
17545 considered a type-name, even if `A<T>' is dependent. */
17546 nested_name_specifier
17547 = cp_parser_nested_name_specifier_opt (parser,
17548 /*typename_keyword_p=*/true,
17549 /*check_dependency_p=*/true,
17550 /*type_p=*/true,
17551 is_declaration);
17552 /* For everything but enumeration types, consider a template-id.
17553 For an enumeration type, consider only a plain identifier. */
17554 if (tag_type != enum_type)
17556 bool template_p = false;
17557 tree decl;
17559 /* Allow the `template' keyword. */
17560 template_p = cp_parser_optional_template_keyword (parser);
17561 /* If we didn't see `template', we don't know if there's a
17562 template-id or not. */
17563 if (!template_p)
17564 cp_parser_parse_tentatively (parser);
17565 /* Parse the template-id. */
17566 token = cp_lexer_peek_token (parser->lexer);
17567 decl = cp_parser_template_id (parser, template_p,
17568 /*check_dependency_p=*/true,
17569 tag_type,
17570 is_declaration);
17571 /* If we didn't find a template-id, look for an ordinary
17572 identifier. */
17573 if (!template_p && !cp_parser_parse_definitely (parser))
17575 /* We can get here when cp_parser_template_id, called by
17576 cp_parser_class_name with tag_type == none_type, succeeds
17577 and caches a BASELINK. Then, when called again here,
17578 instead of failing and returning an error_mark_node
17579 returns it (see template/typename17.C in C++11).
17580 ??? Could we diagnose this earlier? */
17581 else if (tag_type == typename_type && BASELINK_P (decl))
17583 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17584 type = error_mark_node;
17586 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17587 in effect, then we must assume that, upon instantiation, the
17588 template will correspond to a class. */
17589 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17590 && tag_type == typename_type)
17591 type = make_typename_type (parser->scope, decl,
17592 typename_type,
17593 /*complain=*/tf_error);
17594 /* If the `typename' keyword is in effect and DECL is not a type
17595 decl, then type is non existent. */
17596 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17598 else if (TREE_CODE (decl) == TYPE_DECL)
17600 type = check_elaborated_type_specifier (tag_type, decl,
17601 /*allow_template_p=*/true);
17603 /* If the next token is a semicolon, this must be a specialization,
17604 instantiation, or friend declaration. Check the scope while we
17605 still know whether or not we had a nested-name-specifier. */
17606 if (type != error_mark_node
17607 && !nested_name_specifier && !is_friend
17608 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17609 check_unqualified_spec_or_inst (type, token->location);
17611 else if (decl == error_mark_node)
17612 type = error_mark_node;
17615 if (!type)
17617 token = cp_lexer_peek_token (parser->lexer);
17618 identifier = cp_parser_identifier (parser);
17620 if (identifier == error_mark_node)
17622 parser->scope = NULL_TREE;
17623 return error_mark_node;
17626 /* For a `typename', we needn't call xref_tag. */
17627 if (tag_type == typename_type
17628 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17629 return cp_parser_make_typename_type (parser, identifier,
17630 token->location);
17632 /* Template parameter lists apply only if we are not within a
17633 function parameter list. */
17634 bool template_parm_lists_apply
17635 = parser->num_template_parameter_lists;
17636 if (template_parm_lists_apply)
17637 for (cp_binding_level *s = current_binding_level;
17638 s && s->kind != sk_template_parms;
17639 s = s->level_chain)
17640 if (s->kind == sk_function_parms)
17641 template_parm_lists_apply = false;
17643 /* Look up a qualified name in the usual way. */
17644 if (parser->scope)
17646 tree decl;
17647 tree ambiguous_decls;
17649 decl = cp_parser_lookup_name (parser, identifier,
17650 tag_type,
17651 /*is_template=*/false,
17652 /*is_namespace=*/false,
17653 /*check_dependency=*/true,
17654 &ambiguous_decls,
17655 token->location);
17657 /* If the lookup was ambiguous, an error will already have been
17658 issued. */
17659 if (ambiguous_decls)
17660 return error_mark_node;
17662 /* If we are parsing friend declaration, DECL may be a
17663 TEMPLATE_DECL tree node here. However, we need to check
17664 whether this TEMPLATE_DECL results in valid code. Consider
17665 the following example:
17667 namespace N {
17668 template <class T> class C {};
17670 class X {
17671 template <class T> friend class N::C; // #1, valid code
17673 template <class T> class Y {
17674 friend class N::C; // #2, invalid code
17677 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17678 name lookup of `N::C'. We see that friend declaration must
17679 be template for the code to be valid. Note that
17680 processing_template_decl does not work here since it is
17681 always 1 for the above two cases. */
17683 decl = (cp_parser_maybe_treat_template_as_class
17684 (decl, /*tag_name_p=*/is_friend
17685 && template_parm_lists_apply));
17687 if (TREE_CODE (decl) != TYPE_DECL)
17689 cp_parser_diagnose_invalid_type_name (parser,
17690 identifier,
17691 token->location);
17692 return error_mark_node;
17695 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17697 bool allow_template = (template_parm_lists_apply
17698 || DECL_SELF_REFERENCE_P (decl));
17699 type = check_elaborated_type_specifier (tag_type, decl,
17700 allow_template);
17702 if (type == error_mark_node)
17703 return error_mark_node;
17706 /* Forward declarations of nested types, such as
17708 class C1::C2;
17709 class C1::C2::C3;
17711 are invalid unless all components preceding the final '::'
17712 are complete. If all enclosing types are complete, these
17713 declarations become merely pointless.
17715 Invalid forward declarations of nested types are errors
17716 caught elsewhere in parsing. Those that are pointless arrive
17717 here. */
17719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17720 && !is_friend && !processing_explicit_instantiation)
17721 warning (0, "declaration %qD does not declare anything", decl);
17723 type = TREE_TYPE (decl);
17725 else
17727 /* An elaborated-type-specifier sometimes introduces a new type and
17728 sometimes names an existing type. Normally, the rule is that it
17729 introduces a new type only if there is not an existing type of
17730 the same name already in scope. For example, given:
17732 struct S {};
17733 void f() { struct S s; }
17735 the `struct S' in the body of `f' is the same `struct S' as in
17736 the global scope; the existing definition is used. However, if
17737 there were no global declaration, this would introduce a new
17738 local class named `S'.
17740 An exception to this rule applies to the following code:
17742 namespace N { struct S; }
17744 Here, the elaborated-type-specifier names a new type
17745 unconditionally; even if there is already an `S' in the
17746 containing scope this declaration names a new type.
17747 This exception only applies if the elaborated-type-specifier
17748 forms the complete declaration:
17750 [class.name]
17752 A declaration consisting solely of `class-key identifier ;' is
17753 either a redeclaration of the name in the current scope or a
17754 forward declaration of the identifier as a class name. It
17755 introduces the name into the current scope.
17757 We are in this situation precisely when the next token is a `;'.
17759 An exception to the exception is that a `friend' declaration does
17760 *not* name a new type; i.e., given:
17762 struct S { friend struct T; };
17764 `T' is not a new type in the scope of `S'.
17766 Also, `new struct S' or `sizeof (struct S)' never results in the
17767 definition of a new type; a new type can only be declared in a
17768 declaration context. */
17770 tag_scope ts;
17771 bool template_p;
17773 if (is_friend)
17774 /* Friends have special name lookup rules. */
17775 ts = ts_within_enclosing_non_class;
17776 else if (is_declaration
17777 && cp_lexer_next_token_is (parser->lexer,
17778 CPP_SEMICOLON))
17779 /* This is a `class-key identifier ;' */
17780 ts = ts_current;
17781 else
17782 ts = ts_global;
17784 template_p =
17785 (template_parm_lists_apply
17786 && (cp_parser_next_token_starts_class_definition_p (parser)
17787 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17788 /* An unqualified name was used to reference this type, so
17789 there were no qualifying templates. */
17790 if (template_parm_lists_apply
17791 && !cp_parser_check_template_parameters (parser,
17792 /*num_templates=*/0,
17793 token->location,
17794 /*declarator=*/NULL))
17795 return error_mark_node;
17796 type = xref_tag (tag_type, identifier, ts, template_p);
17800 if (type == error_mark_node)
17801 return error_mark_node;
17803 /* Allow attributes on forward declarations of classes. */
17804 if (attributes)
17806 if (TREE_CODE (type) == TYPENAME_TYPE)
17807 warning (OPT_Wattributes,
17808 "attributes ignored on uninstantiated type");
17809 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17810 && ! processing_explicit_instantiation)
17811 warning (OPT_Wattributes,
17812 "attributes ignored on template instantiation");
17813 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17814 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17815 else
17816 warning (OPT_Wattributes,
17817 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17820 if (tag_type != enum_type)
17822 /* Indicate whether this class was declared as a `class' or as a
17823 `struct'. */
17824 if (CLASS_TYPE_P (type))
17825 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17826 cp_parser_check_class_key (tag_type, type);
17829 /* A "<" cannot follow an elaborated type specifier. If that
17830 happens, the user was probably trying to form a template-id. */
17831 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17832 token->location);
17834 return type;
17837 /* Parse an enum-specifier.
17839 enum-specifier:
17840 enum-head { enumerator-list [opt] }
17841 enum-head { enumerator-list , } [C++0x]
17843 enum-head:
17844 enum-key identifier [opt] enum-base [opt]
17845 enum-key nested-name-specifier identifier enum-base [opt]
17847 enum-key:
17848 enum
17849 enum class [C++0x]
17850 enum struct [C++0x]
17852 enum-base: [C++0x]
17853 : type-specifier-seq
17855 opaque-enum-specifier:
17856 enum-key identifier enum-base [opt] ;
17858 GNU Extensions:
17859 enum-key attributes[opt] identifier [opt] enum-base [opt]
17860 { enumerator-list [opt] }attributes[opt]
17861 enum-key attributes[opt] identifier [opt] enum-base [opt]
17862 { enumerator-list, }attributes[opt] [C++0x]
17864 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17865 if the token stream isn't an enum-specifier after all. */
17867 static tree
17868 cp_parser_enum_specifier (cp_parser* parser)
17870 tree identifier;
17871 tree type = NULL_TREE;
17872 tree prev_scope;
17873 tree nested_name_specifier = NULL_TREE;
17874 tree attributes;
17875 bool scoped_enum_p = false;
17876 bool has_underlying_type = false;
17877 bool nested_being_defined = false;
17878 bool new_value_list = false;
17879 bool is_new_type = false;
17880 bool is_unnamed = false;
17881 tree underlying_type = NULL_TREE;
17882 cp_token *type_start_token = NULL;
17883 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17885 parser->colon_corrects_to_scope_p = false;
17887 /* Parse tentatively so that we can back up if we don't find a
17888 enum-specifier. */
17889 cp_parser_parse_tentatively (parser);
17891 /* Caller guarantees that the current token is 'enum', an identifier
17892 possibly follows, and the token after that is an opening brace.
17893 If we don't have an identifier, fabricate an anonymous name for
17894 the enumeration being defined. */
17895 cp_lexer_consume_token (parser->lexer);
17897 /* Parse the "class" or "struct", which indicates a scoped
17898 enumeration type in C++0x. */
17899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17900 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17902 if (cxx_dialect < cxx11)
17903 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17905 /* Consume the `struct' or `class' token. */
17906 cp_lexer_consume_token (parser->lexer);
17908 scoped_enum_p = true;
17911 attributes = cp_parser_attributes_opt (parser);
17913 /* Clear the qualification. */
17914 parser->scope = NULL_TREE;
17915 parser->qualifying_scope = NULL_TREE;
17916 parser->object_scope = NULL_TREE;
17918 /* Figure out in what scope the declaration is being placed. */
17919 prev_scope = current_scope ();
17921 type_start_token = cp_lexer_peek_token (parser->lexer);
17923 push_deferring_access_checks (dk_no_check);
17924 nested_name_specifier
17925 = cp_parser_nested_name_specifier_opt (parser,
17926 /*typename_keyword_p=*/true,
17927 /*check_dependency_p=*/false,
17928 /*type_p=*/false,
17929 /*is_declaration=*/false);
17931 if (nested_name_specifier)
17933 tree name;
17935 identifier = cp_parser_identifier (parser);
17936 name = cp_parser_lookup_name (parser, identifier,
17937 enum_type,
17938 /*is_template=*/false,
17939 /*is_namespace=*/false,
17940 /*check_dependency=*/true,
17941 /*ambiguous_decls=*/NULL,
17942 input_location);
17943 if (name && name != error_mark_node)
17945 type = TREE_TYPE (name);
17946 if (TREE_CODE (type) == TYPENAME_TYPE)
17948 /* Are template enums allowed in ISO? */
17949 if (template_parm_scope_p ())
17950 pedwarn (type_start_token->location, OPT_Wpedantic,
17951 "%qD is an enumeration template", name);
17952 /* ignore a typename reference, for it will be solved by name
17953 in start_enum. */
17954 type = NULL_TREE;
17957 else if (nested_name_specifier == error_mark_node)
17958 /* We already issued an error. */;
17959 else
17961 error_at (type_start_token->location,
17962 "%qD does not name an enumeration in %qT",
17963 identifier, nested_name_specifier);
17964 nested_name_specifier = error_mark_node;
17967 else
17969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17970 identifier = cp_parser_identifier (parser);
17971 else
17973 identifier = make_anon_name ();
17974 is_unnamed = true;
17975 if (scoped_enum_p)
17976 error_at (type_start_token->location,
17977 "unnamed scoped enum is not allowed");
17980 pop_deferring_access_checks ();
17982 /* Check for the `:' that denotes a specified underlying type in C++0x.
17983 Note that a ':' could also indicate a bitfield width, however. */
17984 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17986 cp_decl_specifier_seq type_specifiers;
17988 /* Consume the `:'. */
17989 cp_lexer_consume_token (parser->lexer);
17991 /* Parse the type-specifier-seq. */
17992 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17993 /*is_trailing_return=*/false,
17994 &type_specifiers);
17996 /* At this point this is surely not elaborated type specifier. */
17997 if (!cp_parser_parse_definitely (parser))
17998 return NULL_TREE;
18000 if (cxx_dialect < cxx11)
18001 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18003 has_underlying_type = true;
18005 /* If that didn't work, stop. */
18006 if (type_specifiers.type != error_mark_node)
18008 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18009 /*initialized=*/0, NULL);
18010 if (underlying_type == error_mark_node
18011 || check_for_bare_parameter_packs (underlying_type))
18012 underlying_type = NULL_TREE;
18016 /* Look for the `{' but don't consume it yet. */
18017 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18019 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18021 cp_parser_error (parser, "expected %<{%>");
18022 if (has_underlying_type)
18024 type = NULL_TREE;
18025 goto out;
18028 /* An opaque-enum-specifier must have a ';' here. */
18029 if ((scoped_enum_p || underlying_type)
18030 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18032 cp_parser_error (parser, "expected %<;%> or %<{%>");
18033 if (has_underlying_type)
18035 type = NULL_TREE;
18036 goto out;
18041 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18042 return NULL_TREE;
18044 if (nested_name_specifier)
18046 if (CLASS_TYPE_P (nested_name_specifier))
18048 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18049 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18050 push_scope (nested_name_specifier);
18052 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18054 push_nested_namespace (nested_name_specifier);
18058 /* Issue an error message if type-definitions are forbidden here. */
18059 if (!cp_parser_check_type_definition (parser))
18060 type = error_mark_node;
18061 else
18062 /* Create the new type. We do this before consuming the opening
18063 brace so the enum will be recorded as being on the line of its
18064 tag (or the 'enum' keyword, if there is no tag). */
18065 type = start_enum (identifier, type, underlying_type,
18066 attributes, scoped_enum_p, &is_new_type);
18068 /* If the next token is not '{' it is an opaque-enum-specifier or an
18069 elaborated-type-specifier. */
18070 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18072 timevar_push (TV_PARSE_ENUM);
18073 if (nested_name_specifier
18074 && nested_name_specifier != error_mark_node)
18076 /* The following catches invalid code such as:
18077 enum class S<int>::E { A, B, C }; */
18078 if (!processing_specialization
18079 && CLASS_TYPE_P (nested_name_specifier)
18080 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18081 error_at (type_start_token->location, "cannot add an enumerator "
18082 "list to a template instantiation");
18084 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18086 error_at (type_start_token->location,
18087 "%<%T::%E%> has not been declared",
18088 TYPE_CONTEXT (nested_name_specifier),
18089 nested_name_specifier);
18090 type = error_mark_node;
18092 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18093 && !CLASS_TYPE_P (nested_name_specifier))
18095 error_at (type_start_token->location, "nested name specifier "
18096 "%qT for enum declaration does not name a class "
18097 "or namespace", nested_name_specifier);
18098 type = error_mark_node;
18100 /* If that scope does not contain the scope in which the
18101 class was originally declared, the program is invalid. */
18102 else if (prev_scope && !is_ancestor (prev_scope,
18103 nested_name_specifier))
18105 if (at_namespace_scope_p ())
18106 error_at (type_start_token->location,
18107 "declaration of %qD in namespace %qD which does not "
18108 "enclose %qD",
18109 type, prev_scope, nested_name_specifier);
18110 else
18111 error_at (type_start_token->location,
18112 "declaration of %qD in %qD which does not "
18113 "enclose %qD",
18114 type, prev_scope, nested_name_specifier);
18115 type = error_mark_node;
18117 /* If that scope is the scope where the declaration is being placed
18118 the program is invalid. */
18119 else if (CLASS_TYPE_P (nested_name_specifier)
18120 && CLASS_TYPE_P (prev_scope)
18121 && same_type_p (nested_name_specifier, prev_scope))
18123 permerror (type_start_token->location,
18124 "extra qualification not allowed");
18125 nested_name_specifier = NULL_TREE;
18129 if (scoped_enum_p)
18130 begin_scope (sk_scoped_enum, type);
18132 /* Consume the opening brace. */
18133 matching_braces braces;
18134 braces.consume_open (parser);
18136 if (type == error_mark_node)
18137 ; /* Nothing to add */
18138 else if (OPAQUE_ENUM_P (type)
18139 || (cxx_dialect > cxx98 && processing_specialization))
18141 new_value_list = true;
18142 SET_OPAQUE_ENUM_P (type, false);
18143 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18145 else
18147 error_at (type_start_token->location,
18148 "multiple definition of %q#T", type);
18149 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18150 "previous definition here");
18151 type = error_mark_node;
18154 if (type == error_mark_node)
18155 cp_parser_skip_to_end_of_block_or_statement (parser);
18156 /* If the next token is not '}', then there are some enumerators. */
18157 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18159 if (is_unnamed && !scoped_enum_p)
18160 pedwarn (type_start_token->location, OPT_Wpedantic,
18161 "ISO C++ forbids empty unnamed enum");
18163 else
18164 cp_parser_enumerator_list (parser, type);
18166 /* Consume the final '}'. */
18167 braces.require_close (parser);
18169 if (scoped_enum_p)
18170 finish_scope ();
18171 timevar_pop (TV_PARSE_ENUM);
18173 else
18175 /* If a ';' follows, then it is an opaque-enum-specifier
18176 and additional restrictions apply. */
18177 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18179 if (is_unnamed)
18180 error_at (type_start_token->location,
18181 "opaque-enum-specifier without name");
18182 else if (nested_name_specifier)
18183 error_at (type_start_token->location,
18184 "opaque-enum-specifier must use a simple identifier");
18188 /* Look for trailing attributes to apply to this enumeration, and
18189 apply them if appropriate. */
18190 if (cp_parser_allow_gnu_extensions_p (parser))
18192 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18193 cplus_decl_attributes (&type,
18194 trailing_attr,
18195 (int) ATTR_FLAG_TYPE_IN_PLACE);
18198 /* Finish up the enumeration. */
18199 if (type != error_mark_node)
18201 if (new_value_list)
18202 finish_enum_value_list (type);
18203 if (is_new_type)
18204 finish_enum (type);
18207 if (nested_name_specifier)
18209 if (CLASS_TYPE_P (nested_name_specifier))
18211 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18212 pop_scope (nested_name_specifier);
18214 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18216 pop_nested_namespace (nested_name_specifier);
18219 out:
18220 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18221 return type;
18224 /* Parse an enumerator-list. The enumerators all have the indicated
18225 TYPE.
18227 enumerator-list:
18228 enumerator-definition
18229 enumerator-list , enumerator-definition */
18231 static void
18232 cp_parser_enumerator_list (cp_parser* parser, tree type)
18234 while (true)
18236 /* Parse an enumerator-definition. */
18237 cp_parser_enumerator_definition (parser, type);
18239 /* If the next token is not a ',', we've reached the end of
18240 the list. */
18241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18242 break;
18243 /* Otherwise, consume the `,' and keep going. */
18244 cp_lexer_consume_token (parser->lexer);
18245 /* If the next token is a `}', there is a trailing comma. */
18246 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18248 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18249 pedwarn (input_location, OPT_Wpedantic,
18250 "comma at end of enumerator list");
18251 break;
18256 /* Parse an enumerator-definition. The enumerator has the indicated
18257 TYPE.
18259 enumerator-definition:
18260 enumerator
18261 enumerator = constant-expression
18263 enumerator:
18264 identifier
18266 GNU Extensions:
18268 enumerator-definition:
18269 enumerator attributes [opt]
18270 enumerator attributes [opt] = constant-expression */
18272 static void
18273 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18275 tree identifier;
18276 tree value;
18277 location_t loc;
18279 /* Save the input location because we are interested in the location
18280 of the identifier and not the location of the explicit value. */
18281 loc = cp_lexer_peek_token (parser->lexer)->location;
18283 /* Look for the identifier. */
18284 identifier = cp_parser_identifier (parser);
18285 if (identifier == error_mark_node)
18286 return;
18288 /* Parse any specified attributes. */
18289 tree attrs = cp_parser_attributes_opt (parser);
18291 /* If the next token is an '=', then there is an explicit value. */
18292 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18294 /* Consume the `=' token. */
18295 cp_lexer_consume_token (parser->lexer);
18296 /* Parse the value. */
18297 value = cp_parser_constant_expression (parser);
18299 else
18300 value = NULL_TREE;
18302 /* If we are processing a template, make sure the initializer of the
18303 enumerator doesn't contain any bare template parameter pack. */
18304 if (check_for_bare_parameter_packs (value))
18305 value = error_mark_node;
18307 /* Create the enumerator. */
18308 build_enumerator (identifier, value, type, attrs, loc);
18311 /* Parse a namespace-name.
18313 namespace-name:
18314 original-namespace-name
18315 namespace-alias
18317 Returns the NAMESPACE_DECL for the namespace. */
18319 static tree
18320 cp_parser_namespace_name (cp_parser* parser)
18322 tree identifier;
18323 tree namespace_decl;
18325 cp_token *token = cp_lexer_peek_token (parser->lexer);
18327 /* Get the name of the namespace. */
18328 identifier = cp_parser_identifier (parser);
18329 if (identifier == error_mark_node)
18330 return error_mark_node;
18332 /* Look up the identifier in the currently active scope. Look only
18333 for namespaces, due to:
18335 [basic.lookup.udir]
18337 When looking up a namespace-name in a using-directive or alias
18338 definition, only namespace names are considered.
18340 And:
18342 [basic.lookup.qual]
18344 During the lookup of a name preceding the :: scope resolution
18345 operator, object, function, and enumerator names are ignored.
18347 (Note that cp_parser_qualifying_entity only calls this
18348 function if the token after the name is the scope resolution
18349 operator.) */
18350 namespace_decl = cp_parser_lookup_name (parser, identifier,
18351 none_type,
18352 /*is_template=*/false,
18353 /*is_namespace=*/true,
18354 /*check_dependency=*/true,
18355 /*ambiguous_decls=*/NULL,
18356 token->location);
18357 /* If it's not a namespace, issue an error. */
18358 if (namespace_decl == error_mark_node
18359 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18361 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18362 error_at (token->location, "%qD is not a namespace-name", identifier);
18363 cp_parser_error (parser, "expected namespace-name");
18364 namespace_decl = error_mark_node;
18367 return namespace_decl;
18370 /* Parse a namespace-definition.
18372 namespace-definition:
18373 named-namespace-definition
18374 unnamed-namespace-definition
18376 named-namespace-definition:
18377 original-namespace-definition
18378 extension-namespace-definition
18380 original-namespace-definition:
18381 namespace identifier { namespace-body }
18383 extension-namespace-definition:
18384 namespace original-namespace-name { namespace-body }
18386 unnamed-namespace-definition:
18387 namespace { namespace-body } */
18389 static void
18390 cp_parser_namespace_definition (cp_parser* parser)
18392 tree identifier;
18393 int nested_definition_count = 0;
18395 cp_ensure_no_omp_declare_simd (parser);
18396 cp_ensure_no_oacc_routine (parser);
18398 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18400 if (is_inline)
18402 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18403 cp_lexer_consume_token (parser->lexer);
18406 /* Look for the `namespace' keyword. */
18407 cp_token* token
18408 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18410 /* Parse any specified attributes before the identifier. */
18411 tree attribs = cp_parser_attributes_opt (parser);
18413 for (;;)
18415 identifier = NULL_TREE;
18417 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18419 identifier = cp_parser_identifier (parser);
18421 /* Parse any attributes specified after the identifier. */
18422 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18425 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18426 break;
18428 if (!nested_definition_count && cxx_dialect < cxx1z)
18429 pedwarn (input_location, OPT_Wpedantic,
18430 "nested namespace definitions only available with "
18431 "-std=c++1z or -std=gnu++1z");
18433 /* Nested namespace names can create new namespaces (unlike
18434 other qualified-ids). */
18435 if (int count = identifier ? push_namespace (identifier) : 0)
18436 nested_definition_count += count;
18437 else
18438 cp_parser_error (parser, "nested namespace name required");
18439 cp_lexer_consume_token (parser->lexer);
18442 if (nested_definition_count && !identifier)
18443 cp_parser_error (parser, "namespace name required");
18445 if (nested_definition_count && attribs)
18446 error_at (token->location,
18447 "a nested namespace definition cannot have attributes");
18448 if (nested_definition_count && is_inline)
18449 error_at (token->location,
18450 "a nested namespace definition cannot be inline");
18452 /* Start the namespace. */
18453 nested_definition_count += push_namespace (identifier, is_inline);
18455 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18457 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18459 /* Look for the `{' to validate starting the namespace. */
18460 matching_braces braces;
18461 if (braces.require_open (parser))
18463 /* Parse the body of the namespace. */
18464 cp_parser_namespace_body (parser);
18466 /* Look for the final `}'. */
18467 braces.require_close (parser);
18470 if (has_visibility)
18471 pop_visibility (1);
18473 /* Pop the nested namespace definitions. */
18474 while (nested_definition_count--)
18475 pop_namespace ();
18478 /* Parse a namespace-body.
18480 namespace-body:
18481 declaration-seq [opt] */
18483 static void
18484 cp_parser_namespace_body (cp_parser* parser)
18486 cp_parser_declaration_seq_opt (parser);
18489 /* Parse a namespace-alias-definition.
18491 namespace-alias-definition:
18492 namespace identifier = qualified-namespace-specifier ; */
18494 static void
18495 cp_parser_namespace_alias_definition (cp_parser* parser)
18497 tree identifier;
18498 tree namespace_specifier;
18500 cp_token *token = cp_lexer_peek_token (parser->lexer);
18502 /* Look for the `namespace' keyword. */
18503 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18504 /* Look for the identifier. */
18505 identifier = cp_parser_identifier (parser);
18506 if (identifier == error_mark_node)
18507 return;
18508 /* Look for the `=' token. */
18509 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18510 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18512 error_at (token->location, "%<namespace%> definition is not allowed here");
18513 /* Skip the definition. */
18514 cp_lexer_consume_token (parser->lexer);
18515 if (cp_parser_skip_to_closing_brace (parser))
18516 cp_lexer_consume_token (parser->lexer);
18517 return;
18519 cp_parser_require (parser, CPP_EQ, RT_EQ);
18520 /* Look for the qualified-namespace-specifier. */
18521 namespace_specifier
18522 = cp_parser_qualified_namespace_specifier (parser);
18523 /* Look for the `;' token. */
18524 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18526 /* Register the alias in the symbol table. */
18527 do_namespace_alias (identifier, namespace_specifier);
18530 /* Parse a qualified-namespace-specifier.
18532 qualified-namespace-specifier:
18533 :: [opt] nested-name-specifier [opt] namespace-name
18535 Returns a NAMESPACE_DECL corresponding to the specified
18536 namespace. */
18538 static tree
18539 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18541 /* Look for the optional `::'. */
18542 cp_parser_global_scope_opt (parser,
18543 /*current_scope_valid_p=*/false);
18545 /* Look for the optional nested-name-specifier. */
18546 cp_parser_nested_name_specifier_opt (parser,
18547 /*typename_keyword_p=*/false,
18548 /*check_dependency_p=*/true,
18549 /*type_p=*/false,
18550 /*is_declaration=*/true);
18552 return cp_parser_namespace_name (parser);
18555 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18556 access declaration.
18558 using-declaration:
18559 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18560 using :: unqualified-id ;
18562 access-declaration:
18563 qualified-id ;
18567 static bool
18568 cp_parser_using_declaration (cp_parser* parser,
18569 bool access_declaration_p)
18571 cp_token *token;
18572 bool typename_p = false;
18573 bool global_scope_p;
18574 tree decl;
18575 tree identifier;
18576 tree qscope;
18577 int oldcount = errorcount;
18578 cp_token *diag_token = NULL;
18580 if (access_declaration_p)
18582 diag_token = cp_lexer_peek_token (parser->lexer);
18583 cp_parser_parse_tentatively (parser);
18585 else
18587 /* Look for the `using' keyword. */
18588 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18590 again:
18591 /* Peek at the next token. */
18592 token = cp_lexer_peek_token (parser->lexer);
18593 /* See if it's `typename'. */
18594 if (token->keyword == RID_TYPENAME)
18596 /* Remember that we've seen it. */
18597 typename_p = true;
18598 /* Consume the `typename' token. */
18599 cp_lexer_consume_token (parser->lexer);
18603 /* Look for the optional global scope qualification. */
18604 global_scope_p
18605 = (cp_parser_global_scope_opt (parser,
18606 /*current_scope_valid_p=*/false)
18607 != NULL_TREE);
18609 /* If we saw `typename', or didn't see `::', then there must be a
18610 nested-name-specifier present. */
18611 if (typename_p || !global_scope_p)
18613 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18614 /*check_dependency_p=*/true,
18615 /*type_p=*/false,
18616 /*is_declaration=*/true);
18617 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18619 cp_parser_skip_to_end_of_block_or_statement (parser);
18620 return false;
18623 /* Otherwise, we could be in either of the two productions. In that
18624 case, treat the nested-name-specifier as optional. */
18625 else
18626 qscope = cp_parser_nested_name_specifier_opt (parser,
18627 /*typename_keyword_p=*/false,
18628 /*check_dependency_p=*/true,
18629 /*type_p=*/false,
18630 /*is_declaration=*/true);
18631 if (!qscope)
18632 qscope = global_namespace;
18633 else if (UNSCOPED_ENUM_P (qscope))
18634 qscope = CP_TYPE_CONTEXT (qscope);
18636 if (access_declaration_p && cp_parser_error_occurred (parser))
18637 /* Something has already gone wrong; there's no need to parse
18638 further. Since an error has occurred, the return value of
18639 cp_parser_parse_definitely will be false, as required. */
18640 return cp_parser_parse_definitely (parser);
18642 token = cp_lexer_peek_token (parser->lexer);
18643 /* Parse the unqualified-id. */
18644 identifier = cp_parser_unqualified_id (parser,
18645 /*template_keyword_p=*/false,
18646 /*check_dependency_p=*/true,
18647 /*declarator_p=*/true,
18648 /*optional_p=*/false);
18650 if (access_declaration_p)
18652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18653 cp_parser_simulate_error (parser);
18654 if (!cp_parser_parse_definitely (parser))
18655 return false;
18657 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18659 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18660 if (cxx_dialect < cxx1z
18661 && !in_system_header_at (ell->location))
18662 pedwarn (ell->location, 0,
18663 "pack expansion in using-declaration only available "
18664 "with -std=c++1z or -std=gnu++1z");
18665 qscope = make_pack_expansion (qscope);
18668 /* The function we call to handle a using-declaration is different
18669 depending on what scope we are in. */
18670 if (qscope == error_mark_node || identifier == error_mark_node)
18672 else if (!identifier_p (identifier)
18673 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18674 /* [namespace.udecl]
18676 A using declaration shall not name a template-id. */
18677 error_at (token->location,
18678 "a template-id may not appear in a using-declaration");
18679 else
18681 if (at_class_scope_p ())
18683 /* Create the USING_DECL. */
18684 decl = do_class_using_decl (qscope, identifier);
18686 if (decl && typename_p)
18687 USING_DECL_TYPENAME_P (decl) = 1;
18689 if (check_for_bare_parameter_packs (decl))
18691 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18692 return false;
18694 else
18695 /* Add it to the list of members in this class. */
18696 finish_member_declaration (decl);
18698 else
18700 decl = cp_parser_lookup_name_simple (parser,
18701 identifier,
18702 token->location);
18703 if (decl == error_mark_node)
18704 cp_parser_name_lookup_error (parser, identifier,
18705 decl, NLE_NULL,
18706 token->location);
18707 else if (check_for_bare_parameter_packs (decl))
18709 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18710 return false;
18712 else if (!at_namespace_scope_p ())
18713 finish_local_using_decl (decl, qscope, identifier);
18714 else
18715 finish_namespace_using_decl (decl, qscope, identifier);
18719 if (!access_declaration_p
18720 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18722 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18723 if (cxx_dialect < cxx1z)
18724 pedwarn (comma->location, 0,
18725 "comma-separated list in using-declaration only available "
18726 "with -std=c++1z or -std=gnu++1z");
18727 goto again;
18730 /* Look for the final `;'. */
18731 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18733 if (access_declaration_p && errorcount == oldcount)
18734 warning_at (diag_token->location, OPT_Wdeprecated,
18735 "access declarations are deprecated "
18736 "in favour of using-declarations; "
18737 "suggestion: add the %<using%> keyword");
18739 return true;
18742 /* Parse an alias-declaration.
18744 alias-declaration:
18745 using identifier attribute-specifier-seq [opt] = type-id */
18747 static tree
18748 cp_parser_alias_declaration (cp_parser* parser)
18750 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18751 location_t id_location;
18752 cp_declarator *declarator;
18753 cp_decl_specifier_seq decl_specs;
18754 bool member_p;
18755 const char *saved_message = NULL;
18757 /* Look for the `using' keyword. */
18758 cp_token *using_token
18759 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18760 if (using_token == NULL)
18761 return error_mark_node;
18763 id_location = cp_lexer_peek_token (parser->lexer)->location;
18764 id = cp_parser_identifier (parser);
18765 if (id == error_mark_node)
18766 return error_mark_node;
18768 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18769 attributes = cp_parser_attributes_opt (parser);
18770 if (attributes == error_mark_node)
18771 return error_mark_node;
18773 cp_parser_require (parser, CPP_EQ, RT_EQ);
18775 if (cp_parser_error_occurred (parser))
18776 return error_mark_node;
18778 cp_parser_commit_to_tentative_parse (parser);
18780 /* Now we are going to parse the type-id of the declaration. */
18783 [dcl.type]/3 says:
18785 "A type-specifier-seq shall not define a class or enumeration
18786 unless it appears in the type-id of an alias-declaration (7.1.3) that
18787 is not the declaration of a template-declaration."
18789 In other words, if we currently are in an alias template, the
18790 type-id should not define a type.
18792 So let's set parser->type_definition_forbidden_message in that
18793 case; cp_parser_check_type_definition (called by
18794 cp_parser_class_specifier) will then emit an error if a type is
18795 defined in the type-id. */
18796 if (parser->num_template_parameter_lists)
18798 saved_message = parser->type_definition_forbidden_message;
18799 parser->type_definition_forbidden_message =
18800 G_("types may not be defined in alias template declarations");
18803 type = cp_parser_type_id (parser);
18805 /* Restore the error message if need be. */
18806 if (parser->num_template_parameter_lists)
18807 parser->type_definition_forbidden_message = saved_message;
18809 if (type == error_mark_node
18810 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18812 cp_parser_skip_to_end_of_block_or_statement (parser);
18813 return error_mark_node;
18816 /* A typedef-name can also be introduced by an alias-declaration. The
18817 identifier following the using keyword becomes a typedef-name. It has
18818 the same semantics as if it were introduced by the typedef
18819 specifier. In particular, it does not define a new type and it shall
18820 not appear in the type-id. */
18822 clear_decl_specs (&decl_specs);
18823 decl_specs.type = type;
18824 if (attributes != NULL_TREE)
18826 decl_specs.attributes = attributes;
18827 set_and_check_decl_spec_loc (&decl_specs,
18828 ds_attribute,
18829 attrs_token);
18831 set_and_check_decl_spec_loc (&decl_specs,
18832 ds_typedef,
18833 using_token);
18834 set_and_check_decl_spec_loc (&decl_specs,
18835 ds_alias,
18836 using_token);
18838 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18839 declarator->id_loc = id_location;
18841 member_p = at_class_scope_p ();
18842 if (member_p)
18843 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18844 NULL_TREE, attributes);
18845 else
18846 decl = start_decl (declarator, &decl_specs, 0,
18847 attributes, NULL_TREE, &pushed_scope);
18848 if (decl == error_mark_node)
18849 return decl;
18851 // Attach constraints to the alias declaration.
18852 if (flag_concepts && current_template_parms)
18854 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18855 tree constr = build_constraints (reqs, NULL_TREE);
18856 set_constraints (decl, constr);
18859 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18861 if (pushed_scope)
18862 pop_scope (pushed_scope);
18864 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18865 added into the symbol table; otherwise, return the TYPE_DECL. */
18866 if (DECL_LANG_SPECIFIC (decl)
18867 && DECL_TEMPLATE_INFO (decl)
18868 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18870 decl = DECL_TI_TEMPLATE (decl);
18871 if (member_p)
18872 check_member_template (decl);
18875 return decl;
18878 /* Parse a using-directive.
18880 using-directive:
18881 using namespace :: [opt] nested-name-specifier [opt]
18882 namespace-name ; */
18884 static void
18885 cp_parser_using_directive (cp_parser* parser)
18887 tree namespace_decl;
18888 tree attribs;
18890 /* Look for the `using' keyword. */
18891 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18892 /* And the `namespace' keyword. */
18893 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18894 /* Look for the optional `::' operator. */
18895 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18896 /* And the optional nested-name-specifier. */
18897 cp_parser_nested_name_specifier_opt (parser,
18898 /*typename_keyword_p=*/false,
18899 /*check_dependency_p=*/true,
18900 /*type_p=*/false,
18901 /*is_declaration=*/true);
18902 /* Get the namespace being used. */
18903 namespace_decl = cp_parser_namespace_name (parser);
18904 /* And any specified attributes. */
18905 attribs = cp_parser_attributes_opt (parser);
18907 /* Update the symbol table. */
18908 if (namespace_bindings_p ())
18909 finish_namespace_using_directive (namespace_decl, attribs);
18910 else
18911 finish_local_using_directive (namespace_decl, attribs);
18913 /* Look for the final `;'. */
18914 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18917 /* Parse an asm-definition.
18919 asm-definition:
18920 asm ( string-literal ) ;
18922 GNU Extension:
18924 asm-definition:
18925 asm volatile [opt] ( string-literal ) ;
18926 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18927 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18928 : asm-operand-list [opt] ) ;
18929 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18930 : asm-operand-list [opt]
18931 : asm-clobber-list [opt] ) ;
18932 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18933 : asm-clobber-list [opt]
18934 : asm-goto-list ) ; */
18936 static void
18937 cp_parser_asm_definition (cp_parser* parser)
18939 tree string;
18940 tree outputs = NULL_TREE;
18941 tree inputs = NULL_TREE;
18942 tree clobbers = NULL_TREE;
18943 tree labels = NULL_TREE;
18944 tree asm_stmt;
18945 bool volatile_p = false;
18946 bool extended_p = false;
18947 bool invalid_inputs_p = false;
18948 bool invalid_outputs_p = false;
18949 bool goto_p = false;
18950 required_token missing = RT_NONE;
18952 /* Look for the `asm' keyword. */
18953 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18955 if (parser->in_function_body
18956 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18958 error ("%<asm%> in %<constexpr%> function");
18959 cp_function_chain->invalid_constexpr = true;
18962 /* See if the next token is `volatile'. */
18963 if (cp_parser_allow_gnu_extensions_p (parser)
18964 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18966 /* Remember that we saw the `volatile' keyword. */
18967 volatile_p = true;
18968 /* Consume the token. */
18969 cp_lexer_consume_token (parser->lexer);
18971 if (cp_parser_allow_gnu_extensions_p (parser)
18972 && parser->in_function_body
18973 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18975 /* Remember that we saw the `goto' keyword. */
18976 goto_p = true;
18977 /* Consume the token. */
18978 cp_lexer_consume_token (parser->lexer);
18980 /* Look for the opening `('. */
18981 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18982 return;
18983 /* Look for the string. */
18984 string = cp_parser_string_literal (parser, false, false);
18985 if (string == error_mark_node)
18987 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18988 /*consume_paren=*/true);
18989 return;
18992 /* If we're allowing GNU extensions, check for the extended assembly
18993 syntax. Unfortunately, the `:' tokens need not be separated by
18994 a space in C, and so, for compatibility, we tolerate that here
18995 too. Doing that means that we have to treat the `::' operator as
18996 two `:' tokens. */
18997 if (cp_parser_allow_gnu_extensions_p (parser)
18998 && parser->in_function_body
18999 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19000 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19002 bool inputs_p = false;
19003 bool clobbers_p = false;
19004 bool labels_p = false;
19006 /* The extended syntax was used. */
19007 extended_p = true;
19009 /* Look for outputs. */
19010 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19012 /* Consume the `:'. */
19013 cp_lexer_consume_token (parser->lexer);
19014 /* Parse the output-operands. */
19015 if (cp_lexer_next_token_is_not (parser->lexer,
19016 CPP_COLON)
19017 && cp_lexer_next_token_is_not (parser->lexer,
19018 CPP_SCOPE)
19019 && cp_lexer_next_token_is_not (parser->lexer,
19020 CPP_CLOSE_PAREN)
19021 && !goto_p)
19023 outputs = cp_parser_asm_operand_list (parser);
19024 if (outputs == error_mark_node)
19025 invalid_outputs_p = true;
19028 /* If the next token is `::', there are no outputs, and the
19029 next token is the beginning of the inputs. */
19030 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19031 /* The inputs are coming next. */
19032 inputs_p = true;
19034 /* Look for inputs. */
19035 if (inputs_p
19036 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19038 /* Consume the `:' or `::'. */
19039 cp_lexer_consume_token (parser->lexer);
19040 /* Parse the output-operands. */
19041 if (cp_lexer_next_token_is_not (parser->lexer,
19042 CPP_COLON)
19043 && cp_lexer_next_token_is_not (parser->lexer,
19044 CPP_SCOPE)
19045 && cp_lexer_next_token_is_not (parser->lexer,
19046 CPP_CLOSE_PAREN))
19048 inputs = cp_parser_asm_operand_list (parser);
19049 if (inputs == error_mark_node)
19050 invalid_inputs_p = true;
19053 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19054 /* The clobbers are coming next. */
19055 clobbers_p = true;
19057 /* Look for clobbers. */
19058 if (clobbers_p
19059 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19061 clobbers_p = true;
19062 /* Consume the `:' or `::'. */
19063 cp_lexer_consume_token (parser->lexer);
19064 /* Parse the clobbers. */
19065 if (cp_lexer_next_token_is_not (parser->lexer,
19066 CPP_COLON)
19067 && cp_lexer_next_token_is_not (parser->lexer,
19068 CPP_CLOSE_PAREN))
19069 clobbers = cp_parser_asm_clobber_list (parser);
19071 else if (goto_p
19072 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19073 /* The labels are coming next. */
19074 labels_p = true;
19076 /* Look for labels. */
19077 if (labels_p
19078 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19080 labels_p = true;
19081 /* Consume the `:' or `::'. */
19082 cp_lexer_consume_token (parser->lexer);
19083 /* Parse the labels. */
19084 labels = cp_parser_asm_label_list (parser);
19087 if (goto_p && !labels_p)
19088 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19090 else if (goto_p)
19091 missing = RT_COLON_SCOPE;
19093 /* Look for the closing `)'. */
19094 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19095 missing ? missing : RT_CLOSE_PAREN))
19096 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19097 /*consume_paren=*/true);
19098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19100 if (!invalid_inputs_p && !invalid_outputs_p)
19102 /* Create the ASM_EXPR. */
19103 if (parser->in_function_body)
19105 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19106 inputs, clobbers, labels);
19107 /* If the extended syntax was not used, mark the ASM_EXPR. */
19108 if (!extended_p)
19110 tree temp = asm_stmt;
19111 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19112 temp = TREE_OPERAND (temp, 0);
19114 ASM_INPUT_P (temp) = 1;
19117 else
19118 symtab->finalize_toplevel_asm (string);
19122 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19123 type that comes from the decl-specifier-seq. */
19125 static tree
19126 strip_declarator_types (tree type, cp_declarator *declarator)
19128 for (cp_declarator *d = declarator; d;)
19129 switch (d->kind)
19131 case cdk_id:
19132 case cdk_decomp:
19133 case cdk_error:
19134 d = NULL;
19135 break;
19137 default:
19138 if (TYPE_PTRMEMFUNC_P (type))
19139 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19140 type = TREE_TYPE (type);
19141 d = d->declarator;
19142 break;
19145 return type;
19148 /* Declarators [gram.dcl.decl] */
19150 /* Parse an init-declarator.
19152 init-declarator:
19153 declarator initializer [opt]
19155 GNU Extension:
19157 init-declarator:
19158 declarator asm-specification [opt] attributes [opt] initializer [opt]
19160 function-definition:
19161 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19162 function-body
19163 decl-specifier-seq [opt] declarator function-try-block
19165 GNU Extension:
19167 function-definition:
19168 __extension__ function-definition
19170 TM Extension:
19172 function-definition:
19173 decl-specifier-seq [opt] declarator function-transaction-block
19175 The DECL_SPECIFIERS apply to this declarator. Returns a
19176 representation of the entity declared. If MEMBER_P is TRUE, then
19177 this declarator appears in a class scope. The new DECL created by
19178 this declarator is returned.
19180 The CHECKS are access checks that should be performed once we know
19181 what entity is being declared (and, therefore, what classes have
19182 befriended it).
19184 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19185 for a function-definition here as well. If the declarator is a
19186 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19187 be TRUE upon return. By that point, the function-definition will
19188 have been completely parsed.
19190 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19191 is FALSE.
19193 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19194 parsed declaration if it is an uninitialized single declarator not followed
19195 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19196 if present, will not be consumed. If returned, this declarator will be
19197 created with SD_INITIALIZED but will not call cp_finish_decl.
19199 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19200 and there is an initializer, the pointed location_t is set to the
19201 location of the '=' or `(', or '{' in C++11 token introducing the
19202 initializer. */
19204 static tree
19205 cp_parser_init_declarator (cp_parser* parser,
19206 cp_decl_specifier_seq *decl_specifiers,
19207 vec<deferred_access_check, va_gc> *checks,
19208 bool function_definition_allowed_p,
19209 bool member_p,
19210 int declares_class_or_enum,
19211 bool* function_definition_p,
19212 tree* maybe_range_for_decl,
19213 location_t* init_loc,
19214 tree* auto_result)
19216 cp_token *token = NULL, *asm_spec_start_token = NULL,
19217 *attributes_start_token = NULL;
19218 cp_declarator *declarator;
19219 tree prefix_attributes;
19220 tree attributes = NULL;
19221 tree asm_specification;
19222 tree initializer;
19223 tree decl = NULL_TREE;
19224 tree scope;
19225 int is_initialized;
19226 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19227 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19228 "(...)". */
19229 enum cpp_ttype initialization_kind;
19230 bool is_direct_init = false;
19231 bool is_non_constant_init;
19232 int ctor_dtor_or_conv_p;
19233 bool friend_p = cp_parser_friend_p (decl_specifiers);
19234 tree pushed_scope = NULL_TREE;
19235 bool range_for_decl_p = false;
19236 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19237 location_t tmp_init_loc = UNKNOWN_LOCATION;
19239 /* Gather the attributes that were provided with the
19240 decl-specifiers. */
19241 prefix_attributes = decl_specifiers->attributes;
19243 /* Assume that this is not the declarator for a function
19244 definition. */
19245 if (function_definition_p)
19246 *function_definition_p = false;
19248 /* Default arguments are only permitted for function parameters. */
19249 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19250 parser->default_arg_ok_p = false;
19252 /* Defer access checks while parsing the declarator; we cannot know
19253 what names are accessible until we know what is being
19254 declared. */
19255 resume_deferring_access_checks ();
19257 token = cp_lexer_peek_token (parser->lexer);
19259 /* Parse the declarator. */
19260 declarator
19261 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19262 &ctor_dtor_or_conv_p,
19263 /*parenthesized_p=*/NULL,
19264 member_p, friend_p);
19265 /* Gather up the deferred checks. */
19266 stop_deferring_access_checks ();
19268 parser->default_arg_ok_p = saved_default_arg_ok_p;
19270 /* If the DECLARATOR was erroneous, there's no need to go
19271 further. */
19272 if (declarator == cp_error_declarator)
19273 return error_mark_node;
19275 /* Check that the number of template-parameter-lists is OK. */
19276 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19277 token->location))
19278 return error_mark_node;
19280 if (declares_class_or_enum & 2)
19281 cp_parser_check_for_definition_in_return_type (declarator,
19282 decl_specifiers->type,
19283 decl_specifiers->locations[ds_type_spec]);
19285 /* Figure out what scope the entity declared by the DECLARATOR is
19286 located in. `grokdeclarator' sometimes changes the scope, so
19287 we compute it now. */
19288 scope = get_scope_of_declarator (declarator);
19290 /* Perform any lookups in the declared type which were thought to be
19291 dependent, but are not in the scope of the declarator. */
19292 decl_specifiers->type
19293 = maybe_update_decl_type (decl_specifiers->type, scope);
19295 /* If we're allowing GNU extensions, look for an
19296 asm-specification. */
19297 if (cp_parser_allow_gnu_extensions_p (parser))
19299 /* Look for an asm-specification. */
19300 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19301 asm_specification = cp_parser_asm_specification_opt (parser);
19303 else
19304 asm_specification = NULL_TREE;
19306 /* Look for attributes. */
19307 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19308 attributes = cp_parser_attributes_opt (parser);
19310 /* Peek at the next token. */
19311 token = cp_lexer_peek_token (parser->lexer);
19313 bool bogus_implicit_tmpl = false;
19315 if (function_declarator_p (declarator))
19317 /* Handle C++17 deduction guides. */
19318 if (!decl_specifiers->type
19319 && ctor_dtor_or_conv_p <= 0
19320 && cxx_dialect >= cxx1z)
19322 cp_declarator *id = get_id_declarator (declarator);
19323 tree name = id->u.id.unqualified_name;
19324 parser->scope = id->u.id.qualifying_scope;
19325 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19326 if (tmpl
19327 && (DECL_CLASS_TEMPLATE_P (tmpl)
19328 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19330 id->u.id.unqualified_name = dguide_name (tmpl);
19331 id->u.id.sfk = sfk_deduction_guide;
19332 ctor_dtor_or_conv_p = 1;
19336 /* Check to see if the token indicates the start of a
19337 function-definition. */
19338 if (cp_parser_token_starts_function_definition_p (token))
19340 if (!function_definition_allowed_p)
19342 /* If a function-definition should not appear here, issue an
19343 error message. */
19344 cp_parser_error (parser,
19345 "a function-definition is not allowed here");
19346 return error_mark_node;
19349 location_t func_brace_location
19350 = cp_lexer_peek_token (parser->lexer)->location;
19352 /* Neither attributes nor an asm-specification are allowed
19353 on a function-definition. */
19354 if (asm_specification)
19355 error_at (asm_spec_start_token->location,
19356 "an asm-specification is not allowed "
19357 "on a function-definition");
19358 if (attributes)
19359 error_at (attributes_start_token->location,
19360 "attributes are not allowed "
19361 "on a function-definition");
19362 /* This is a function-definition. */
19363 *function_definition_p = true;
19365 /* Parse the function definition. */
19366 if (member_p)
19367 decl = cp_parser_save_member_function_body (parser,
19368 decl_specifiers,
19369 declarator,
19370 prefix_attributes);
19371 else
19372 decl =
19373 (cp_parser_function_definition_from_specifiers_and_declarator
19374 (parser, decl_specifiers, prefix_attributes, declarator));
19376 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19378 /* This is where the prologue starts... */
19379 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19380 = func_brace_location;
19383 return decl;
19386 else if (parser->fully_implicit_function_template_p)
19388 /* A non-template declaration involving a function parameter list
19389 containing an implicit template parameter will be made into a
19390 template. If the resulting declaration is not going to be an
19391 actual function then finish the template scope here to prevent it.
19392 An error message will be issued once we have a decl to talk about.
19394 FIXME probably we should do type deduction rather than create an
19395 implicit template, but the standard currently doesn't allow it. */
19396 bogus_implicit_tmpl = true;
19397 finish_fully_implicit_template (parser, NULL_TREE);
19400 /* [dcl.dcl]
19402 Only in function declarations for constructors, destructors, type
19403 conversions, and deduction guides can the decl-specifier-seq be omitted.
19405 We explicitly postpone this check past the point where we handle
19406 function-definitions because we tolerate function-definitions
19407 that are missing their return types in some modes. */
19408 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19410 cp_parser_error (parser,
19411 "expected constructor, destructor, or type conversion");
19412 return error_mark_node;
19415 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19416 if (token->type == CPP_EQ
19417 || token->type == CPP_OPEN_PAREN
19418 || token->type == CPP_OPEN_BRACE)
19420 is_initialized = SD_INITIALIZED;
19421 initialization_kind = token->type;
19422 if (maybe_range_for_decl)
19423 *maybe_range_for_decl = error_mark_node;
19424 tmp_init_loc = token->location;
19425 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19426 *init_loc = tmp_init_loc;
19428 if (token->type == CPP_EQ
19429 && function_declarator_p (declarator))
19431 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19432 if (t2->keyword == RID_DEFAULT)
19433 is_initialized = SD_DEFAULTED;
19434 else if (t2->keyword == RID_DELETE)
19435 is_initialized = SD_DELETED;
19438 else
19440 /* If the init-declarator isn't initialized and isn't followed by a
19441 `,' or `;', it's not a valid init-declarator. */
19442 if (token->type != CPP_COMMA
19443 && token->type != CPP_SEMICOLON)
19445 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19446 range_for_decl_p = true;
19447 else
19449 if (!maybe_range_for_decl)
19450 cp_parser_error (parser, "expected initializer");
19451 return error_mark_node;
19454 is_initialized = SD_UNINITIALIZED;
19455 initialization_kind = CPP_EOF;
19458 /* Because start_decl has side-effects, we should only call it if we
19459 know we're going ahead. By this point, we know that we cannot
19460 possibly be looking at any other construct. */
19461 cp_parser_commit_to_tentative_parse (parser);
19463 /* Enter the newly declared entry in the symbol table. If we're
19464 processing a declaration in a class-specifier, we wait until
19465 after processing the initializer. */
19466 if (!member_p)
19468 if (parser->in_unbraced_linkage_specification_p)
19469 decl_specifiers->storage_class = sc_extern;
19470 decl = start_decl (declarator, decl_specifiers,
19471 range_for_decl_p? SD_INITIALIZED : is_initialized,
19472 attributes, prefix_attributes, &pushed_scope);
19473 cp_finalize_omp_declare_simd (parser, decl);
19474 cp_finalize_oacc_routine (parser, decl, false);
19475 /* Adjust location of decl if declarator->id_loc is more appropriate:
19476 set, and decl wasn't merged with another decl, in which case its
19477 location would be different from input_location, and more accurate. */
19478 if (DECL_P (decl)
19479 && declarator->id_loc != UNKNOWN_LOCATION
19480 && DECL_SOURCE_LOCATION (decl) == input_location)
19481 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19483 else if (scope)
19484 /* Enter the SCOPE. That way unqualified names appearing in the
19485 initializer will be looked up in SCOPE. */
19486 pushed_scope = push_scope (scope);
19488 /* Perform deferred access control checks, now that we know in which
19489 SCOPE the declared entity resides. */
19490 if (!member_p && decl)
19492 tree saved_current_function_decl = NULL_TREE;
19494 /* If the entity being declared is a function, pretend that we
19495 are in its scope. If it is a `friend', it may have access to
19496 things that would not otherwise be accessible. */
19497 if (TREE_CODE (decl) == FUNCTION_DECL)
19499 saved_current_function_decl = current_function_decl;
19500 current_function_decl = decl;
19503 /* Perform access checks for template parameters. */
19504 cp_parser_perform_template_parameter_access_checks (checks);
19506 /* Perform the access control checks for the declarator and the
19507 decl-specifiers. */
19508 perform_deferred_access_checks (tf_warning_or_error);
19510 /* Restore the saved value. */
19511 if (TREE_CODE (decl) == FUNCTION_DECL)
19512 current_function_decl = saved_current_function_decl;
19515 /* Parse the initializer. */
19516 initializer = NULL_TREE;
19517 is_direct_init = false;
19518 is_non_constant_init = true;
19519 if (is_initialized)
19521 if (function_declarator_p (declarator))
19523 if (initialization_kind == CPP_EQ)
19524 initializer = cp_parser_pure_specifier (parser);
19525 else
19527 /* If the declaration was erroneous, we don't really
19528 know what the user intended, so just silently
19529 consume the initializer. */
19530 if (decl != error_mark_node)
19531 error_at (tmp_init_loc, "initializer provided for function");
19532 cp_parser_skip_to_closing_parenthesis (parser,
19533 /*recovering=*/true,
19534 /*or_comma=*/false,
19535 /*consume_paren=*/true);
19538 else
19540 /* We want to record the extra mangling scope for in-class
19541 initializers of class members and initializers of static data
19542 member templates. The former involves deferring
19543 parsing of the initializer until end of class as with default
19544 arguments. So right here we only handle the latter. */
19545 if (!member_p && processing_template_decl)
19546 start_lambda_scope (decl);
19547 initializer = cp_parser_initializer (parser,
19548 &is_direct_init,
19549 &is_non_constant_init);
19550 if (!member_p && processing_template_decl)
19551 finish_lambda_scope ();
19552 if (initializer == error_mark_node)
19553 cp_parser_skip_to_end_of_statement (parser);
19557 /* The old parser allows attributes to appear after a parenthesized
19558 initializer. Mark Mitchell proposed removing this functionality
19559 on the GCC mailing lists on 2002-08-13. This parser accepts the
19560 attributes -- but ignores them. */
19561 if (cp_parser_allow_gnu_extensions_p (parser)
19562 && initialization_kind == CPP_OPEN_PAREN)
19563 if (cp_parser_attributes_opt (parser))
19564 warning (OPT_Wattributes,
19565 "attributes after parenthesized initializer ignored");
19567 /* And now complain about a non-function implicit template. */
19568 if (bogus_implicit_tmpl && decl != error_mark_node)
19569 error_at (DECL_SOURCE_LOCATION (decl),
19570 "non-function %qD declared as implicit template", decl);
19572 /* For an in-class declaration, use `grokfield' to create the
19573 declaration. */
19574 if (member_p)
19576 if (pushed_scope)
19578 pop_scope (pushed_scope);
19579 pushed_scope = NULL_TREE;
19581 decl = grokfield (declarator, decl_specifiers,
19582 initializer, !is_non_constant_init,
19583 /*asmspec=*/NULL_TREE,
19584 chainon (attributes, prefix_attributes));
19585 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19586 cp_parser_save_default_args (parser, decl);
19587 cp_finalize_omp_declare_simd (parser, decl);
19588 cp_finalize_oacc_routine (parser, decl, false);
19591 /* Finish processing the declaration. But, skip member
19592 declarations. */
19593 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19595 cp_finish_decl (decl,
19596 initializer, !is_non_constant_init,
19597 asm_specification,
19598 /* If the initializer is in parentheses, then this is
19599 a direct-initialization, which means that an
19600 `explicit' constructor is OK. Otherwise, an
19601 `explicit' constructor cannot be used. */
19602 ((is_direct_init || !is_initialized)
19603 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19605 else if ((cxx_dialect != cxx98) && friend_p
19606 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19607 /* Core issue #226 (C++0x only): A default template-argument
19608 shall not be specified in a friend class template
19609 declaration. */
19610 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19611 /*is_partial=*/false, /*is_friend_decl=*/1);
19613 if (!friend_p && pushed_scope)
19614 pop_scope (pushed_scope);
19616 if (function_declarator_p (declarator)
19617 && parser->fully_implicit_function_template_p)
19619 if (member_p)
19620 decl = finish_fully_implicit_template (parser, decl);
19621 else
19622 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19625 if (auto_result && is_initialized && decl_specifiers->type
19626 && type_uses_auto (decl_specifiers->type))
19627 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19629 return decl;
19632 /* Parse a declarator.
19634 declarator:
19635 direct-declarator
19636 ptr-operator declarator
19638 abstract-declarator:
19639 ptr-operator abstract-declarator [opt]
19640 direct-abstract-declarator
19642 GNU Extensions:
19644 declarator:
19645 attributes [opt] direct-declarator
19646 attributes [opt] ptr-operator declarator
19648 abstract-declarator:
19649 attributes [opt] ptr-operator abstract-declarator [opt]
19650 attributes [opt] direct-abstract-declarator
19652 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19653 detect constructors, destructors, deduction guides, or conversion operators.
19654 It is set to -1 if the declarator is a name, and +1 if it is a
19655 function. Otherwise it is set to zero. Usually you just want to
19656 test for >0, but internally the negative value is used.
19658 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19659 a decl-specifier-seq unless it declares a constructor, destructor,
19660 or conversion. It might seem that we could check this condition in
19661 semantic analysis, rather than parsing, but that makes it difficult
19662 to handle something like `f()'. We want to notice that there are
19663 no decl-specifiers, and therefore realize that this is an
19664 expression, not a declaration.)
19666 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19667 the declarator is a direct-declarator of the form "(...)".
19669 MEMBER_P is true iff this declarator is a member-declarator.
19671 FRIEND_P is true iff this declarator is a friend. */
19673 static cp_declarator *
19674 cp_parser_declarator (cp_parser* parser,
19675 cp_parser_declarator_kind dcl_kind,
19676 int* ctor_dtor_or_conv_p,
19677 bool* parenthesized_p,
19678 bool member_p, bool friend_p)
19680 cp_declarator *declarator;
19681 enum tree_code code;
19682 cp_cv_quals cv_quals;
19683 tree class_type;
19684 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19686 /* Assume this is not a constructor, destructor, or type-conversion
19687 operator. */
19688 if (ctor_dtor_or_conv_p)
19689 *ctor_dtor_or_conv_p = 0;
19691 if (cp_parser_allow_gnu_extensions_p (parser))
19692 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19694 /* Check for the ptr-operator production. */
19695 cp_parser_parse_tentatively (parser);
19696 /* Parse the ptr-operator. */
19697 code = cp_parser_ptr_operator (parser,
19698 &class_type,
19699 &cv_quals,
19700 &std_attributes);
19702 /* If that worked, then we have a ptr-operator. */
19703 if (cp_parser_parse_definitely (parser))
19705 /* If a ptr-operator was found, then this declarator was not
19706 parenthesized. */
19707 if (parenthesized_p)
19708 *parenthesized_p = true;
19709 /* The dependent declarator is optional if we are parsing an
19710 abstract-declarator. */
19711 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19712 cp_parser_parse_tentatively (parser);
19714 /* Parse the dependent declarator. */
19715 declarator = cp_parser_declarator (parser, dcl_kind,
19716 /*ctor_dtor_or_conv_p=*/NULL,
19717 /*parenthesized_p=*/NULL,
19718 /*member_p=*/false,
19719 friend_p);
19721 /* If we are parsing an abstract-declarator, we must handle the
19722 case where the dependent declarator is absent. */
19723 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19724 && !cp_parser_parse_definitely (parser))
19725 declarator = NULL;
19727 declarator = cp_parser_make_indirect_declarator
19728 (code, class_type, cv_quals, declarator, std_attributes);
19730 /* Everything else is a direct-declarator. */
19731 else
19733 if (parenthesized_p)
19734 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19735 CPP_OPEN_PAREN);
19736 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19737 ctor_dtor_or_conv_p,
19738 member_p, friend_p);
19741 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19742 declarator->attributes = gnu_attributes;
19743 return declarator;
19746 /* Parse a direct-declarator or direct-abstract-declarator.
19748 direct-declarator:
19749 declarator-id
19750 direct-declarator ( parameter-declaration-clause )
19751 cv-qualifier-seq [opt]
19752 ref-qualifier [opt]
19753 exception-specification [opt]
19754 direct-declarator [ constant-expression [opt] ]
19755 ( declarator )
19757 direct-abstract-declarator:
19758 direct-abstract-declarator [opt]
19759 ( parameter-declaration-clause )
19760 cv-qualifier-seq [opt]
19761 ref-qualifier [opt]
19762 exception-specification [opt]
19763 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19764 ( abstract-declarator )
19766 Returns a representation of the declarator. DCL_KIND is
19767 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19768 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19769 we are parsing a direct-declarator. It is
19770 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19771 of ambiguity we prefer an abstract declarator, as per
19772 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19773 as for cp_parser_declarator. */
19775 static cp_declarator *
19776 cp_parser_direct_declarator (cp_parser* parser,
19777 cp_parser_declarator_kind dcl_kind,
19778 int* ctor_dtor_or_conv_p,
19779 bool member_p, bool friend_p)
19781 cp_token *token;
19782 cp_declarator *declarator = NULL;
19783 tree scope = NULL_TREE;
19784 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19785 bool saved_in_declarator_p = parser->in_declarator_p;
19786 bool first = true;
19787 tree pushed_scope = NULL_TREE;
19789 while (true)
19791 /* Peek at the next token. */
19792 token = cp_lexer_peek_token (parser->lexer);
19793 if (token->type == CPP_OPEN_PAREN)
19795 /* This is either a parameter-declaration-clause, or a
19796 parenthesized declarator. When we know we are parsing a
19797 named declarator, it must be a parenthesized declarator
19798 if FIRST is true. For instance, `(int)' is a
19799 parameter-declaration-clause, with an omitted
19800 direct-abstract-declarator. But `((*))', is a
19801 parenthesized abstract declarator. Finally, when T is a
19802 template parameter `(T)' is a
19803 parameter-declaration-clause, and not a parenthesized
19804 named declarator.
19806 We first try and parse a parameter-declaration-clause,
19807 and then try a nested declarator (if FIRST is true).
19809 It is not an error for it not to be a
19810 parameter-declaration-clause, even when FIRST is
19811 false. Consider,
19813 int i (int);
19814 int i (3);
19816 The first is the declaration of a function while the
19817 second is the definition of a variable, including its
19818 initializer.
19820 Having seen only the parenthesis, we cannot know which of
19821 these two alternatives should be selected. Even more
19822 complex are examples like:
19824 int i (int (a));
19825 int i (int (3));
19827 The former is a function-declaration; the latter is a
19828 variable initialization.
19830 Thus again, we try a parameter-declaration-clause, and if
19831 that fails, we back out and return. */
19833 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19835 tree params;
19836 bool is_declarator = false;
19838 /* In a member-declarator, the only valid interpretation
19839 of a parenthesis is the start of a
19840 parameter-declaration-clause. (It is invalid to
19841 initialize a static data member with a parenthesized
19842 initializer; only the "=" form of initialization is
19843 permitted.) */
19844 if (!member_p)
19845 cp_parser_parse_tentatively (parser);
19847 /* Consume the `('. */
19848 matching_parens parens;
19849 parens.consume_open (parser);
19850 if (first)
19852 /* If this is going to be an abstract declarator, we're
19853 in a declarator and we can't have default args. */
19854 parser->default_arg_ok_p = false;
19855 parser->in_declarator_p = true;
19858 begin_scope (sk_function_parms, NULL_TREE);
19860 /* Parse the parameter-declaration-clause. */
19861 params = cp_parser_parameter_declaration_clause (parser);
19863 /* Consume the `)'. */
19864 parens.require_close (parser);
19866 /* If all went well, parse the cv-qualifier-seq,
19867 ref-qualifier and the exception-specification. */
19868 if (member_p || cp_parser_parse_definitely (parser))
19870 cp_cv_quals cv_quals;
19871 cp_virt_specifiers virt_specifiers;
19872 cp_ref_qualifier ref_qual;
19873 tree exception_specification;
19874 tree late_return;
19875 tree attrs;
19876 bool memfn = (member_p || (pushed_scope
19877 && CLASS_TYPE_P (pushed_scope)));
19879 is_declarator = true;
19881 if (ctor_dtor_or_conv_p)
19882 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19883 first = false;
19885 /* Parse the cv-qualifier-seq. */
19886 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19887 /* Parse the ref-qualifier. */
19888 ref_qual = cp_parser_ref_qualifier_opt (parser);
19889 /* Parse the tx-qualifier. */
19890 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19891 /* And the exception-specification. */
19892 exception_specification
19893 = cp_parser_exception_specification_opt (parser);
19895 attrs = cp_parser_std_attribute_spec_seq (parser);
19897 /* In here, we handle cases where attribute is used after
19898 the function declaration. For example:
19899 void func (int x) __attribute__((vector(..))); */
19900 tree gnu_attrs = NULL_TREE;
19901 if (flag_cilkplus
19902 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19904 cp_parser_parse_tentatively (parser);
19905 tree attr = cp_parser_gnu_attributes_opt (parser);
19906 if (cp_lexer_next_token_is_not (parser->lexer,
19907 CPP_SEMICOLON)
19908 && cp_lexer_next_token_is_not (parser->lexer,
19909 CPP_OPEN_BRACE))
19910 cp_parser_abort_tentative_parse (parser);
19911 else if (!cp_parser_parse_definitely (parser))
19913 else
19914 gnu_attrs = attr;
19916 tree requires_clause = NULL_TREE;
19917 late_return = (cp_parser_late_return_type_opt
19918 (parser, declarator, requires_clause,
19919 memfn ? cv_quals : -1));
19921 /* Parse the virt-specifier-seq. */
19922 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19924 /* Create the function-declarator. */
19925 declarator = make_call_declarator (declarator,
19926 params,
19927 cv_quals,
19928 virt_specifiers,
19929 ref_qual,
19930 tx_qual,
19931 exception_specification,
19932 late_return,
19933 requires_clause);
19934 declarator->std_attributes = attrs;
19935 declarator->attributes = gnu_attrs;
19936 /* Any subsequent parameter lists are to do with
19937 return type, so are not those of the declared
19938 function. */
19939 parser->default_arg_ok_p = false;
19942 /* Remove the function parms from scope. */
19943 pop_bindings_and_leave_scope ();
19945 if (is_declarator)
19946 /* Repeat the main loop. */
19947 continue;
19950 /* If this is the first, we can try a parenthesized
19951 declarator. */
19952 if (first)
19954 bool saved_in_type_id_in_expr_p;
19956 parser->default_arg_ok_p = saved_default_arg_ok_p;
19957 parser->in_declarator_p = saved_in_declarator_p;
19959 /* Consume the `('. */
19960 matching_parens parens;
19961 parens.consume_open (parser);
19962 /* Parse the nested declarator. */
19963 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19964 parser->in_type_id_in_expr_p = true;
19965 declarator
19966 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19967 /*parenthesized_p=*/NULL,
19968 member_p, friend_p);
19969 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19970 first = false;
19971 /* Expect a `)'. */
19972 if (!parens.require_close (parser))
19973 declarator = cp_error_declarator;
19974 if (declarator == cp_error_declarator)
19975 break;
19977 goto handle_declarator;
19979 /* Otherwise, we must be done. */
19980 else
19981 break;
19983 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19984 && token->type == CPP_OPEN_SQUARE
19985 && !cp_next_tokens_can_be_attribute_p (parser))
19987 /* Parse an array-declarator. */
19988 tree bounds, attrs;
19990 if (ctor_dtor_or_conv_p)
19991 *ctor_dtor_or_conv_p = 0;
19993 first = false;
19994 parser->default_arg_ok_p = false;
19995 parser->in_declarator_p = true;
19996 /* Consume the `['. */
19997 cp_lexer_consume_token (parser->lexer);
19998 /* Peek at the next token. */
19999 token = cp_lexer_peek_token (parser->lexer);
20000 /* If the next token is `]', then there is no
20001 constant-expression. */
20002 if (token->type != CPP_CLOSE_SQUARE)
20004 bool non_constant_p;
20005 bounds
20006 = cp_parser_constant_expression (parser,
20007 /*allow_non_constant=*/true,
20008 &non_constant_p);
20009 if (!non_constant_p)
20010 /* OK */;
20011 else if (error_operand_p (bounds))
20012 /* Already gave an error. */;
20013 else if (!parser->in_function_body
20014 || current_binding_level->kind == sk_function_parms)
20016 /* Normally, the array bound must be an integral constant
20017 expression. However, as an extension, we allow VLAs
20018 in function scopes as long as they aren't part of a
20019 parameter declaration. */
20020 cp_parser_error (parser,
20021 "array bound is not an integer constant");
20022 bounds = error_mark_node;
20024 else if (processing_template_decl
20025 && !type_dependent_expression_p (bounds))
20027 /* Remember this wasn't a constant-expression. */
20028 bounds = build_nop (TREE_TYPE (bounds), bounds);
20029 TREE_SIDE_EFFECTS (bounds) = 1;
20032 else
20033 bounds = NULL_TREE;
20034 /* Look for the closing `]'. */
20035 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20037 declarator = cp_error_declarator;
20038 break;
20041 attrs = cp_parser_std_attribute_spec_seq (parser);
20042 declarator = make_array_declarator (declarator, bounds);
20043 declarator->std_attributes = attrs;
20045 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20048 tree qualifying_scope;
20049 tree unqualified_name;
20050 tree attrs;
20051 special_function_kind sfk;
20052 bool abstract_ok;
20053 bool pack_expansion_p = false;
20054 cp_token *declarator_id_start_token;
20056 /* Parse a declarator-id */
20057 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20058 if (abstract_ok)
20060 cp_parser_parse_tentatively (parser);
20062 /* If we see an ellipsis, we should be looking at a
20063 parameter pack. */
20064 if (token->type == CPP_ELLIPSIS)
20066 /* Consume the `...' */
20067 cp_lexer_consume_token (parser->lexer);
20069 pack_expansion_p = true;
20073 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20074 unqualified_name
20075 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20076 qualifying_scope = parser->scope;
20077 if (abstract_ok)
20079 bool okay = false;
20081 if (!unqualified_name && pack_expansion_p)
20083 /* Check whether an error occurred. */
20084 okay = !cp_parser_error_occurred (parser);
20086 /* We already consumed the ellipsis to mark a
20087 parameter pack, but we have no way to report it,
20088 so abort the tentative parse. We will be exiting
20089 immediately anyway. */
20090 cp_parser_abort_tentative_parse (parser);
20092 else
20093 okay = cp_parser_parse_definitely (parser);
20095 if (!okay)
20096 unqualified_name = error_mark_node;
20097 else if (unqualified_name
20098 && (qualifying_scope
20099 || (!identifier_p (unqualified_name))))
20101 cp_parser_error (parser, "expected unqualified-id");
20102 unqualified_name = error_mark_node;
20106 if (!unqualified_name)
20107 return NULL;
20108 if (unqualified_name == error_mark_node)
20110 declarator = cp_error_declarator;
20111 pack_expansion_p = false;
20112 declarator->parameter_pack_p = false;
20113 break;
20116 attrs = cp_parser_std_attribute_spec_seq (parser);
20118 if (qualifying_scope && at_namespace_scope_p ()
20119 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20121 /* In the declaration of a member of a template class
20122 outside of the class itself, the SCOPE will sometimes
20123 be a TYPENAME_TYPE. For example, given:
20125 template <typename T>
20126 int S<T>::R::i = 3;
20128 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20129 this context, we must resolve S<T>::R to an ordinary
20130 type, rather than a typename type.
20132 The reason we normally avoid resolving TYPENAME_TYPEs
20133 is that a specialization of `S' might render
20134 `S<T>::R' not a type. However, if `S' is
20135 specialized, then this `i' will not be used, so there
20136 is no harm in resolving the types here. */
20137 tree type;
20139 /* Resolve the TYPENAME_TYPE. */
20140 type = resolve_typename_type (qualifying_scope,
20141 /*only_current_p=*/false);
20142 /* If that failed, the declarator is invalid. */
20143 if (TREE_CODE (type) == TYPENAME_TYPE)
20145 if (typedef_variant_p (type))
20146 error_at (declarator_id_start_token->location,
20147 "cannot define member of dependent typedef "
20148 "%qT", type);
20149 else
20150 error_at (declarator_id_start_token->location,
20151 "%<%T::%E%> is not a type",
20152 TYPE_CONTEXT (qualifying_scope),
20153 TYPE_IDENTIFIER (qualifying_scope));
20155 qualifying_scope = type;
20158 sfk = sfk_none;
20160 if (unqualified_name)
20162 tree class_type;
20164 if (qualifying_scope
20165 && CLASS_TYPE_P (qualifying_scope))
20166 class_type = qualifying_scope;
20167 else
20168 class_type = current_class_type;
20170 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20172 tree name_type = TREE_TYPE (unqualified_name);
20174 if (!class_type || !same_type_p (name_type, class_type))
20176 /* We do not attempt to print the declarator
20177 here because we do not have enough
20178 information about its original syntactic
20179 form. */
20180 cp_parser_error (parser, "invalid declarator");
20181 declarator = cp_error_declarator;
20182 break;
20184 else if (qualifying_scope
20185 && CLASSTYPE_USE_TEMPLATE (name_type))
20187 error_at (declarator_id_start_token->location,
20188 "invalid use of constructor as a template");
20189 inform (declarator_id_start_token->location,
20190 "use %<%T::%D%> instead of %<%T::%D%> to "
20191 "name the constructor in a qualified name",
20192 class_type,
20193 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20194 class_type, name_type);
20195 declarator = cp_error_declarator;
20196 break;
20198 unqualified_name = constructor_name (class_type);
20201 if (class_type)
20203 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20204 sfk = sfk_destructor;
20205 else if (identifier_p (unqualified_name)
20206 && IDENTIFIER_CONV_OP_P (unqualified_name))
20207 sfk = sfk_conversion;
20208 else if (/* There's no way to declare a constructor
20209 for an unnamed type, even if the type
20210 got a name for linkage purposes. */
20211 !TYPE_WAS_UNNAMED (class_type)
20212 /* Handle correctly (c++/19200):
20214 struct S {
20215 struct T{};
20216 friend void S(T);
20219 and also:
20221 namespace N {
20222 void S();
20225 struct S {
20226 friend void N::S();
20227 }; */
20228 && (!friend_p || class_type == qualifying_scope)
20229 && constructor_name_p (unqualified_name,
20230 class_type))
20231 sfk = sfk_constructor;
20232 else if (is_overloaded_fn (unqualified_name)
20233 && DECL_CONSTRUCTOR_P (get_first_fn
20234 (unqualified_name)))
20235 sfk = sfk_constructor;
20237 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20238 *ctor_dtor_or_conv_p = -1;
20241 declarator = make_id_declarator (qualifying_scope,
20242 unqualified_name,
20243 sfk);
20244 declarator->std_attributes = attrs;
20245 declarator->id_loc = token->location;
20246 declarator->parameter_pack_p = pack_expansion_p;
20248 if (pack_expansion_p)
20249 maybe_warn_variadic_templates ();
20252 handle_declarator:;
20253 scope = get_scope_of_declarator (declarator);
20254 if (scope)
20256 /* Any names that appear after the declarator-id for a
20257 member are looked up in the containing scope. */
20258 if (at_function_scope_p ())
20260 /* But declarations with qualified-ids can't appear in a
20261 function. */
20262 cp_parser_error (parser, "qualified-id in declaration");
20263 declarator = cp_error_declarator;
20264 break;
20266 pushed_scope = push_scope (scope);
20268 parser->in_declarator_p = true;
20269 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20270 || (declarator && declarator->kind == cdk_id))
20271 /* Default args are only allowed on function
20272 declarations. */
20273 parser->default_arg_ok_p = saved_default_arg_ok_p;
20274 else
20275 parser->default_arg_ok_p = false;
20277 first = false;
20279 /* We're done. */
20280 else
20281 break;
20284 /* For an abstract declarator, we might wind up with nothing at this
20285 point. That's an error; the declarator is not optional. */
20286 if (!declarator)
20287 cp_parser_error (parser, "expected declarator");
20289 /* If we entered a scope, we must exit it now. */
20290 if (pushed_scope)
20291 pop_scope (pushed_scope);
20293 parser->default_arg_ok_p = saved_default_arg_ok_p;
20294 parser->in_declarator_p = saved_in_declarator_p;
20296 return declarator;
20299 /* Parse a ptr-operator.
20301 ptr-operator:
20302 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20303 * cv-qualifier-seq [opt]
20305 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20306 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20308 GNU Extension:
20310 ptr-operator:
20311 & cv-qualifier-seq [opt]
20313 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20314 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20315 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20316 filled in with the TYPE containing the member. *CV_QUALS is
20317 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20318 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20319 Note that the tree codes returned by this function have nothing
20320 to do with the types of trees that will be eventually be created
20321 to represent the pointer or reference type being parsed. They are
20322 just constants with suggestive names. */
20323 static enum tree_code
20324 cp_parser_ptr_operator (cp_parser* parser,
20325 tree* type,
20326 cp_cv_quals *cv_quals,
20327 tree *attributes)
20329 enum tree_code code = ERROR_MARK;
20330 cp_token *token;
20331 tree attrs = NULL_TREE;
20333 /* Assume that it's not a pointer-to-member. */
20334 *type = NULL_TREE;
20335 /* And that there are no cv-qualifiers. */
20336 *cv_quals = TYPE_UNQUALIFIED;
20338 /* Peek at the next token. */
20339 token = cp_lexer_peek_token (parser->lexer);
20341 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20342 if (token->type == CPP_MULT)
20343 code = INDIRECT_REF;
20344 else if (token->type == CPP_AND)
20345 code = ADDR_EXPR;
20346 else if ((cxx_dialect != cxx98) &&
20347 token->type == CPP_AND_AND) /* C++0x only */
20348 code = NON_LVALUE_EXPR;
20350 if (code != ERROR_MARK)
20352 /* Consume the `*', `&' or `&&'. */
20353 cp_lexer_consume_token (parser->lexer);
20355 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20356 `&', if we are allowing GNU extensions. (The only qualifier
20357 that can legally appear after `&' is `restrict', but that is
20358 enforced during semantic analysis. */
20359 if (code == INDIRECT_REF
20360 || cp_parser_allow_gnu_extensions_p (parser))
20361 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20363 attrs = cp_parser_std_attribute_spec_seq (parser);
20364 if (attributes != NULL)
20365 *attributes = attrs;
20367 else
20369 /* Try the pointer-to-member case. */
20370 cp_parser_parse_tentatively (parser);
20371 /* Look for the optional `::' operator. */
20372 cp_parser_global_scope_opt (parser,
20373 /*current_scope_valid_p=*/false);
20374 /* Look for the nested-name specifier. */
20375 token = cp_lexer_peek_token (parser->lexer);
20376 cp_parser_nested_name_specifier (parser,
20377 /*typename_keyword_p=*/false,
20378 /*check_dependency_p=*/true,
20379 /*type_p=*/false,
20380 /*is_declaration=*/false);
20381 /* If we found it, and the next token is a `*', then we are
20382 indeed looking at a pointer-to-member operator. */
20383 if (!cp_parser_error_occurred (parser)
20384 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20386 /* Indicate that the `*' operator was used. */
20387 code = INDIRECT_REF;
20389 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20390 error_at (token->location, "%qD is a namespace", parser->scope);
20391 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20392 error_at (token->location, "cannot form pointer to member of "
20393 "non-class %q#T", parser->scope);
20394 else
20396 /* The type of which the member is a member is given by the
20397 current SCOPE. */
20398 *type = parser->scope;
20399 /* The next name will not be qualified. */
20400 parser->scope = NULL_TREE;
20401 parser->qualifying_scope = NULL_TREE;
20402 parser->object_scope = NULL_TREE;
20403 /* Look for optional c++11 attributes. */
20404 attrs = cp_parser_std_attribute_spec_seq (parser);
20405 if (attributes != NULL)
20406 *attributes = attrs;
20407 /* Look for the optional cv-qualifier-seq. */
20408 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20411 /* If that didn't work we don't have a ptr-operator. */
20412 if (!cp_parser_parse_definitely (parser))
20413 cp_parser_error (parser, "expected ptr-operator");
20416 return code;
20419 /* Parse an (optional) cv-qualifier-seq.
20421 cv-qualifier-seq:
20422 cv-qualifier cv-qualifier-seq [opt]
20424 cv-qualifier:
20425 const
20426 volatile
20428 GNU Extension:
20430 cv-qualifier:
20431 __restrict__
20433 Returns a bitmask representing the cv-qualifiers. */
20435 static cp_cv_quals
20436 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20438 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20440 while (true)
20442 cp_token *token;
20443 cp_cv_quals cv_qualifier;
20445 /* Peek at the next token. */
20446 token = cp_lexer_peek_token (parser->lexer);
20447 /* See if it's a cv-qualifier. */
20448 switch (token->keyword)
20450 case RID_CONST:
20451 cv_qualifier = TYPE_QUAL_CONST;
20452 break;
20454 case RID_VOLATILE:
20455 cv_qualifier = TYPE_QUAL_VOLATILE;
20456 break;
20458 case RID_RESTRICT:
20459 cv_qualifier = TYPE_QUAL_RESTRICT;
20460 break;
20462 default:
20463 cv_qualifier = TYPE_UNQUALIFIED;
20464 break;
20467 if (!cv_qualifier)
20468 break;
20470 if (cv_quals & cv_qualifier)
20472 gcc_rich_location richloc (token->location);
20473 richloc.add_fixit_remove ();
20474 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20475 cp_lexer_purge_token (parser->lexer);
20477 else
20479 cp_lexer_consume_token (parser->lexer);
20480 cv_quals |= cv_qualifier;
20484 return cv_quals;
20487 /* Parse an (optional) ref-qualifier
20489 ref-qualifier:
20493 Returns cp_ref_qualifier representing ref-qualifier. */
20495 static cp_ref_qualifier
20496 cp_parser_ref_qualifier_opt (cp_parser* parser)
20498 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20500 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20501 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20502 return ref_qual;
20504 while (true)
20506 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20507 cp_token *token = cp_lexer_peek_token (parser->lexer);
20509 switch (token->type)
20511 case CPP_AND:
20512 curr_ref_qual = REF_QUAL_LVALUE;
20513 break;
20515 case CPP_AND_AND:
20516 curr_ref_qual = REF_QUAL_RVALUE;
20517 break;
20519 default:
20520 curr_ref_qual = REF_QUAL_NONE;
20521 break;
20524 if (!curr_ref_qual)
20525 break;
20526 else if (ref_qual)
20528 error_at (token->location, "multiple ref-qualifiers");
20529 cp_lexer_purge_token (parser->lexer);
20531 else
20533 ref_qual = curr_ref_qual;
20534 cp_lexer_consume_token (parser->lexer);
20538 return ref_qual;
20541 /* Parse an optional tx-qualifier.
20543 tx-qualifier:
20544 transaction_safe
20545 transaction_safe_dynamic */
20547 static tree
20548 cp_parser_tx_qualifier_opt (cp_parser *parser)
20550 cp_token *token = cp_lexer_peek_token (parser->lexer);
20551 if (token->type == CPP_NAME)
20553 tree name = token->u.value;
20554 const char *p = IDENTIFIER_POINTER (name);
20555 const int len = strlen ("transaction_safe");
20556 if (!strncmp (p, "transaction_safe", len))
20558 p += len;
20559 if (*p == '\0'
20560 || !strcmp (p, "_dynamic"))
20562 cp_lexer_consume_token (parser->lexer);
20563 if (!flag_tm)
20565 error ("%qE requires %<-fgnu-tm%>", name);
20566 return NULL_TREE;
20568 else
20569 return name;
20573 return NULL_TREE;
20576 /* Parse an (optional) virt-specifier-seq.
20578 virt-specifier-seq:
20579 virt-specifier virt-specifier-seq [opt]
20581 virt-specifier:
20582 override
20583 final
20585 Returns a bitmask representing the virt-specifiers. */
20587 static cp_virt_specifiers
20588 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20590 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20592 while (true)
20594 cp_token *token;
20595 cp_virt_specifiers virt_specifier;
20597 /* Peek at the next token. */
20598 token = cp_lexer_peek_token (parser->lexer);
20599 /* See if it's a virt-specifier-qualifier. */
20600 if (token->type != CPP_NAME)
20601 break;
20602 if (id_equal (token->u.value, "override"))
20604 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20605 virt_specifier = VIRT_SPEC_OVERRIDE;
20607 else if (id_equal (token->u.value, "final"))
20609 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20610 virt_specifier = VIRT_SPEC_FINAL;
20612 else if (id_equal (token->u.value, "__final"))
20614 virt_specifier = VIRT_SPEC_FINAL;
20616 else
20617 break;
20619 if (virt_specifiers & virt_specifier)
20621 gcc_rich_location richloc (token->location);
20622 richloc.add_fixit_remove ();
20623 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20624 cp_lexer_purge_token (parser->lexer);
20626 else
20628 cp_lexer_consume_token (parser->lexer);
20629 virt_specifiers |= virt_specifier;
20632 return virt_specifiers;
20635 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20636 is in scope even though it isn't real. */
20638 void
20639 inject_this_parameter (tree ctype, cp_cv_quals quals)
20641 tree this_parm;
20643 if (current_class_ptr)
20645 /* We don't clear this between NSDMIs. Is it already what we want? */
20646 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20647 if (DECL_P (current_class_ptr)
20648 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20649 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20650 && cp_type_quals (type) == quals)
20651 return;
20654 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20655 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20656 current_class_ptr = NULL_TREE;
20657 current_class_ref
20658 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20659 current_class_ptr = this_parm;
20662 /* Return true iff our current scope is a non-static data member
20663 initializer. */
20665 bool
20666 parsing_nsdmi (void)
20668 /* We recognize NSDMI context by the context-less 'this' pointer set up
20669 by the function above. */
20670 if (current_class_ptr
20671 && TREE_CODE (current_class_ptr) == PARM_DECL
20672 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20673 return true;
20674 return false;
20677 /* Parse a late-specified return type, if any. This is not a separate
20678 non-terminal, but part of a function declarator, which looks like
20680 -> trailing-type-specifier-seq abstract-declarator(opt)
20682 Returns the type indicated by the type-id.
20684 In addition to this, parse any queued up #pragma omp declare simd
20685 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20686 #pragma acc routine clauses.
20688 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20689 function. */
20691 static tree
20692 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20693 tree& requires_clause, cp_cv_quals quals)
20695 cp_token *token;
20696 tree type = NULL_TREE;
20697 bool declare_simd_p = (parser->omp_declare_simd
20698 && declarator
20699 && declarator->kind == cdk_id);
20701 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20702 && declarator && declarator->kind == cdk_id);
20704 bool oacc_routine_p = (parser->oacc_routine
20705 && declarator
20706 && declarator->kind == cdk_id);
20708 /* Peek at the next token. */
20709 token = cp_lexer_peek_token (parser->lexer);
20710 /* A late-specified return type is indicated by an initial '->'. */
20711 if (token->type != CPP_DEREF
20712 && token->keyword != RID_REQUIRES
20713 && !(token->type == CPP_NAME
20714 && token->u.value == ridpointers[RID_REQUIRES])
20715 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20716 return NULL_TREE;
20718 tree save_ccp = current_class_ptr;
20719 tree save_ccr = current_class_ref;
20720 if (quals >= 0)
20722 /* DR 1207: 'this' is in scope in the trailing return type. */
20723 inject_this_parameter (current_class_type, quals);
20726 if (token->type == CPP_DEREF)
20728 /* Consume the ->. */
20729 cp_lexer_consume_token (parser->lexer);
20731 type = cp_parser_trailing_type_id (parser);
20734 /* Function declarations may be followed by a trailing
20735 requires-clause. */
20736 requires_clause = cp_parser_requires_clause_opt (parser);
20738 if (cilk_simd_fn_vector_p)
20739 declarator->attributes
20740 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20741 declarator->attributes);
20742 if (declare_simd_p)
20743 declarator->attributes
20744 = cp_parser_late_parsing_omp_declare_simd (parser,
20745 declarator->attributes);
20746 if (oacc_routine_p)
20747 declarator->attributes
20748 = cp_parser_late_parsing_oacc_routine (parser,
20749 declarator->attributes);
20751 if (quals >= 0)
20753 current_class_ptr = save_ccp;
20754 current_class_ref = save_ccr;
20757 return type;
20760 /* Parse a declarator-id.
20762 declarator-id:
20763 id-expression
20764 :: [opt] nested-name-specifier [opt] type-name
20766 In the `id-expression' case, the value returned is as for
20767 cp_parser_id_expression if the id-expression was an unqualified-id.
20768 If the id-expression was a qualified-id, then a SCOPE_REF is
20769 returned. The first operand is the scope (either a NAMESPACE_DECL
20770 or TREE_TYPE), but the second is still just a representation of an
20771 unqualified-id. */
20773 static tree
20774 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20776 tree id;
20777 /* The expression must be an id-expression. Assume that qualified
20778 names are the names of types so that:
20780 template <class T>
20781 int S<T>::R::i = 3;
20783 will work; we must treat `S<T>::R' as the name of a type.
20784 Similarly, assume that qualified names are templates, where
20785 required, so that:
20787 template <class T>
20788 int S<T>::R<T>::i = 3;
20790 will work, too. */
20791 id = cp_parser_id_expression (parser,
20792 /*template_keyword_p=*/false,
20793 /*check_dependency_p=*/false,
20794 /*template_p=*/NULL,
20795 /*declarator_p=*/true,
20796 optional_p);
20797 if (id && BASELINK_P (id))
20798 id = BASELINK_FUNCTIONS (id);
20799 return id;
20802 /* Parse a type-id.
20804 type-id:
20805 type-specifier-seq abstract-declarator [opt]
20807 Returns the TYPE specified. */
20809 static tree
20810 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20811 bool is_trailing_return)
20813 cp_decl_specifier_seq type_specifier_seq;
20814 cp_declarator *abstract_declarator;
20816 /* Parse the type-specifier-seq. */
20817 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20818 is_trailing_return,
20819 &type_specifier_seq);
20820 if (is_template_arg && type_specifier_seq.type
20821 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20822 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20823 /* A bare template name as a template argument is a template template
20824 argument, not a placeholder, so fail parsing it as a type argument. */
20826 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20827 cp_parser_simulate_error (parser);
20828 return error_mark_node;
20830 if (type_specifier_seq.type == error_mark_node)
20831 return error_mark_node;
20833 /* There might or might not be an abstract declarator. */
20834 cp_parser_parse_tentatively (parser);
20835 /* Look for the declarator. */
20836 abstract_declarator
20837 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20838 /*parenthesized_p=*/NULL,
20839 /*member_p=*/false,
20840 /*friend_p=*/false);
20841 /* Check to see if there really was a declarator. */
20842 if (!cp_parser_parse_definitely (parser))
20843 abstract_declarator = NULL;
20845 if (type_specifier_seq.type
20846 /* The concepts TS allows 'auto' as a type-id. */
20847 && (!flag_concepts || parser->in_type_id_in_expr_p)
20848 /* None of the valid uses of 'auto' in C++14 involve the type-id
20849 nonterminal, but it is valid in a trailing-return-type. */
20850 && !(cxx_dialect >= cxx14 && is_trailing_return))
20851 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20853 /* A type-id with type 'auto' is only ok if the abstract declarator
20854 is a function declarator with a late-specified return type.
20856 A type-id with 'auto' is also valid in a trailing-return-type
20857 in a compound-requirement. */
20858 if (abstract_declarator
20859 && abstract_declarator->kind == cdk_function
20860 && abstract_declarator->u.function.late_return_type)
20861 /* OK */;
20862 else if (parser->in_result_type_constraint_p)
20863 /* OK */;
20864 else
20866 location_t loc = type_specifier_seq.locations[ds_type_spec];
20867 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20869 error_at (loc, "missing template arguments after %qT",
20870 auto_node);
20871 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20872 tmpl);
20874 else
20875 error_at (loc, "invalid use of %qT", auto_node);
20876 return error_mark_node;
20880 return groktypename (&type_specifier_seq, abstract_declarator,
20881 is_template_arg);
20884 static tree
20885 cp_parser_type_id (cp_parser *parser)
20887 return cp_parser_type_id_1 (parser, false, false);
20890 static tree
20891 cp_parser_template_type_arg (cp_parser *parser)
20893 tree r;
20894 const char *saved_message = parser->type_definition_forbidden_message;
20895 parser->type_definition_forbidden_message
20896 = G_("types may not be defined in template arguments");
20897 r = cp_parser_type_id_1 (parser, true, false);
20898 parser->type_definition_forbidden_message = saved_message;
20899 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20901 error ("invalid use of %<auto%> in template argument");
20902 r = error_mark_node;
20904 return r;
20907 static tree
20908 cp_parser_trailing_type_id (cp_parser *parser)
20910 return cp_parser_type_id_1 (parser, false, true);
20913 /* Parse a type-specifier-seq.
20915 type-specifier-seq:
20916 type-specifier type-specifier-seq [opt]
20918 GNU extension:
20920 type-specifier-seq:
20921 attributes type-specifier-seq [opt]
20923 If IS_DECLARATION is true, we are at the start of a "condition" or
20924 exception-declaration, so we might be followed by a declarator-id.
20926 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20927 i.e. we've just seen "->".
20929 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20931 static void
20932 cp_parser_type_specifier_seq (cp_parser* parser,
20933 bool is_declaration,
20934 bool is_trailing_return,
20935 cp_decl_specifier_seq *type_specifier_seq)
20937 bool seen_type_specifier = false;
20938 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20939 cp_token *start_token = NULL;
20941 /* Clear the TYPE_SPECIFIER_SEQ. */
20942 clear_decl_specs (type_specifier_seq);
20944 /* In the context of a trailing return type, enum E { } is an
20945 elaborated-type-specifier followed by a function-body, not an
20946 enum-specifier. */
20947 if (is_trailing_return)
20948 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20950 /* Parse the type-specifiers and attributes. */
20951 while (true)
20953 tree type_specifier;
20954 bool is_cv_qualifier;
20956 /* Check for attributes first. */
20957 if (cp_next_tokens_can_be_attribute_p (parser))
20959 type_specifier_seq->attributes =
20960 chainon (type_specifier_seq->attributes,
20961 cp_parser_attributes_opt (parser));
20962 continue;
20965 /* record the token of the beginning of the type specifier seq,
20966 for error reporting purposes*/
20967 if (!start_token)
20968 start_token = cp_lexer_peek_token (parser->lexer);
20970 /* Look for the type-specifier. */
20971 type_specifier = cp_parser_type_specifier (parser,
20972 flags,
20973 type_specifier_seq,
20974 /*is_declaration=*/false,
20975 NULL,
20976 &is_cv_qualifier);
20977 if (!type_specifier)
20979 /* If the first type-specifier could not be found, this is not a
20980 type-specifier-seq at all. */
20981 if (!seen_type_specifier)
20983 /* Set in_declarator_p to avoid skipping to the semicolon. */
20984 int in_decl = parser->in_declarator_p;
20985 parser->in_declarator_p = true;
20987 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20988 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20989 cp_parser_error (parser, "expected type-specifier");
20991 parser->in_declarator_p = in_decl;
20993 type_specifier_seq->type = error_mark_node;
20994 return;
20996 /* If subsequent type-specifiers could not be found, the
20997 type-specifier-seq is complete. */
20998 break;
21001 seen_type_specifier = true;
21002 /* The standard says that a condition can be:
21004 type-specifier-seq declarator = assignment-expression
21006 However, given:
21008 struct S {};
21009 if (int S = ...)
21011 we should treat the "S" as a declarator, not as a
21012 type-specifier. The standard doesn't say that explicitly for
21013 type-specifier-seq, but it does say that for
21014 decl-specifier-seq in an ordinary declaration. Perhaps it
21015 would be clearer just to allow a decl-specifier-seq here, and
21016 then add a semantic restriction that if any decl-specifiers
21017 that are not type-specifiers appear, the program is invalid. */
21018 if (is_declaration && !is_cv_qualifier)
21019 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21023 /* Return whether the function currently being declared has an associated
21024 template parameter list. */
21026 static bool
21027 function_being_declared_is_template_p (cp_parser* parser)
21029 if (!current_template_parms || processing_template_parmlist)
21030 return false;
21032 if (parser->implicit_template_scope)
21033 return true;
21035 if (at_class_scope_p ()
21036 && TYPE_BEING_DEFINED (current_class_type))
21037 return parser->num_template_parameter_lists != 0;
21039 return ((int) parser->num_template_parameter_lists > template_class_depth
21040 (current_class_type));
21043 /* Parse a parameter-declaration-clause.
21045 parameter-declaration-clause:
21046 parameter-declaration-list [opt] ... [opt]
21047 parameter-declaration-list , ...
21049 Returns a representation for the parameter declarations. A return
21050 value of NULL indicates a parameter-declaration-clause consisting
21051 only of an ellipsis. */
21053 static tree
21054 cp_parser_parameter_declaration_clause (cp_parser* parser)
21056 tree parameters;
21057 cp_token *token;
21058 bool ellipsis_p;
21059 bool is_error;
21061 struct cleanup {
21062 cp_parser* parser;
21063 int auto_is_implicit_function_template_parm_p;
21064 ~cleanup() {
21065 parser->auto_is_implicit_function_template_parm_p
21066 = auto_is_implicit_function_template_parm_p;
21068 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21070 (void) cleanup;
21072 if (!processing_specialization
21073 && !processing_template_parmlist
21074 && !processing_explicit_instantiation)
21075 if (!current_function_decl
21076 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21077 parser->auto_is_implicit_function_template_parm_p = true;
21079 /* Peek at the next token. */
21080 token = cp_lexer_peek_token (parser->lexer);
21081 /* Check for trivial parameter-declaration-clauses. */
21082 if (token->type == CPP_ELLIPSIS)
21084 /* Consume the `...' token. */
21085 cp_lexer_consume_token (parser->lexer);
21086 return NULL_TREE;
21088 else if (token->type == CPP_CLOSE_PAREN)
21089 /* There are no parameters. */
21091 #ifndef NO_IMPLICIT_EXTERN_C
21092 if (in_system_header_at (input_location)
21093 && current_class_type == NULL
21094 && current_lang_name == lang_name_c)
21095 return NULL_TREE;
21096 else
21097 #endif
21098 return void_list_node;
21100 /* Check for `(void)', too, which is a special case. */
21101 else if (token->keyword == RID_VOID
21102 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21103 == CPP_CLOSE_PAREN))
21105 /* Consume the `void' token. */
21106 cp_lexer_consume_token (parser->lexer);
21107 /* There are no parameters. */
21108 return void_list_node;
21111 /* Parse the parameter-declaration-list. */
21112 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21113 /* If a parse error occurred while parsing the
21114 parameter-declaration-list, then the entire
21115 parameter-declaration-clause is erroneous. */
21116 if (is_error)
21117 return NULL;
21119 /* Peek at the next token. */
21120 token = cp_lexer_peek_token (parser->lexer);
21121 /* If it's a `,', the clause should terminate with an ellipsis. */
21122 if (token->type == CPP_COMMA)
21124 /* Consume the `,'. */
21125 cp_lexer_consume_token (parser->lexer);
21126 /* Expect an ellipsis. */
21127 ellipsis_p
21128 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21130 /* It might also be `...' if the optional trailing `,' was
21131 omitted. */
21132 else if (token->type == CPP_ELLIPSIS)
21134 /* Consume the `...' token. */
21135 cp_lexer_consume_token (parser->lexer);
21136 /* And remember that we saw it. */
21137 ellipsis_p = true;
21139 else
21140 ellipsis_p = false;
21142 /* Finish the parameter list. */
21143 if (!ellipsis_p)
21144 parameters = chainon (parameters, void_list_node);
21146 return parameters;
21149 /* Parse a parameter-declaration-list.
21151 parameter-declaration-list:
21152 parameter-declaration
21153 parameter-declaration-list , parameter-declaration
21155 Returns a representation of the parameter-declaration-list, as for
21156 cp_parser_parameter_declaration_clause. However, the
21157 `void_list_node' is never appended to the list. Upon return,
21158 *IS_ERROR will be true iff an error occurred. */
21160 static tree
21161 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21163 tree parameters = NULL_TREE;
21164 tree *tail = &parameters;
21165 bool saved_in_unbraced_linkage_specification_p;
21166 int index = 0;
21168 /* Assume all will go well. */
21169 *is_error = false;
21170 /* The special considerations that apply to a function within an
21171 unbraced linkage specifications do not apply to the parameters
21172 to the function. */
21173 saved_in_unbraced_linkage_specification_p
21174 = parser->in_unbraced_linkage_specification_p;
21175 parser->in_unbraced_linkage_specification_p = false;
21177 /* Look for more parameters. */
21178 while (true)
21180 cp_parameter_declarator *parameter;
21181 tree decl = error_mark_node;
21182 bool parenthesized_p = false;
21183 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21184 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21185 (current_template_parms)) : 0);
21187 /* Parse the parameter. */
21188 parameter
21189 = cp_parser_parameter_declaration (parser,
21190 /*template_parm_p=*/false,
21191 &parenthesized_p);
21193 /* We don't know yet if the enclosing context is deprecated, so wait
21194 and warn in grokparms if appropriate. */
21195 deprecated_state = DEPRECATED_SUPPRESS;
21197 if (parameter)
21199 /* If a function parameter pack was specified and an implicit template
21200 parameter was introduced during cp_parser_parameter_declaration,
21201 change any implicit parameters introduced into packs. */
21202 if (parser->implicit_template_parms
21203 && parameter->declarator
21204 && parameter->declarator->parameter_pack_p)
21206 int latest_template_parm_idx = TREE_VEC_LENGTH
21207 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21209 if (latest_template_parm_idx != template_parm_idx)
21210 parameter->decl_specifiers.type = convert_generic_types_to_packs
21211 (parameter->decl_specifiers.type,
21212 template_parm_idx, latest_template_parm_idx);
21215 decl = grokdeclarator (parameter->declarator,
21216 &parameter->decl_specifiers,
21217 PARM,
21218 parameter->default_argument != NULL_TREE,
21219 &parameter->decl_specifiers.attributes);
21222 deprecated_state = DEPRECATED_NORMAL;
21224 /* If a parse error occurred parsing the parameter declaration,
21225 then the entire parameter-declaration-list is erroneous. */
21226 if (decl == error_mark_node)
21228 *is_error = true;
21229 parameters = error_mark_node;
21230 break;
21233 if (parameter->decl_specifiers.attributes)
21234 cplus_decl_attributes (&decl,
21235 parameter->decl_specifiers.attributes,
21237 if (DECL_NAME (decl))
21238 decl = pushdecl (decl);
21240 if (decl != error_mark_node)
21242 retrofit_lang_decl (decl);
21243 DECL_PARM_INDEX (decl) = ++index;
21244 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21247 /* Add the new parameter to the list. */
21248 *tail = build_tree_list (parameter->default_argument, decl);
21249 tail = &TREE_CHAIN (*tail);
21251 /* Peek at the next token. */
21252 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21253 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21254 /* These are for Objective-C++ */
21255 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21256 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21257 /* The parameter-declaration-list is complete. */
21258 break;
21259 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21261 cp_token *token;
21263 /* Peek at the next token. */
21264 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21265 /* If it's an ellipsis, then the list is complete. */
21266 if (token->type == CPP_ELLIPSIS)
21267 break;
21268 /* Otherwise, there must be more parameters. Consume the
21269 `,'. */
21270 cp_lexer_consume_token (parser->lexer);
21271 /* When parsing something like:
21273 int i(float f, double d)
21275 we can tell after seeing the declaration for "f" that we
21276 are not looking at an initialization of a variable "i",
21277 but rather at the declaration of a function "i".
21279 Due to the fact that the parsing of template arguments
21280 (as specified to a template-id) requires backtracking we
21281 cannot use this technique when inside a template argument
21282 list. */
21283 if (!parser->in_template_argument_list_p
21284 && !parser->in_type_id_in_expr_p
21285 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21286 /* However, a parameter-declaration of the form
21287 "float(f)" (which is a valid declaration of a
21288 parameter "f") can also be interpreted as an
21289 expression (the conversion of "f" to "float"). */
21290 && !parenthesized_p)
21291 cp_parser_commit_to_tentative_parse (parser);
21293 else
21295 cp_parser_error (parser, "expected %<,%> or %<...%>");
21296 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21297 cp_parser_skip_to_closing_parenthesis (parser,
21298 /*recovering=*/true,
21299 /*or_comma=*/false,
21300 /*consume_paren=*/false);
21301 break;
21305 parser->in_unbraced_linkage_specification_p
21306 = saved_in_unbraced_linkage_specification_p;
21308 /* Reset implicit_template_scope if we are about to leave the function
21309 parameter list that introduced it. Note that for out-of-line member
21310 definitions, there will be one or more class scopes before we get to
21311 the template parameter scope. */
21313 if (cp_binding_level *its = parser->implicit_template_scope)
21314 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21316 while (maybe_its->kind == sk_class)
21317 maybe_its = maybe_its->level_chain;
21318 if (maybe_its == its)
21320 parser->implicit_template_parms = 0;
21321 parser->implicit_template_scope = 0;
21325 return parameters;
21328 /* Parse a parameter declaration.
21330 parameter-declaration:
21331 decl-specifier-seq ... [opt] declarator
21332 decl-specifier-seq declarator = assignment-expression
21333 decl-specifier-seq ... [opt] abstract-declarator [opt]
21334 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21336 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21337 declares a template parameter. (In that case, a non-nested `>'
21338 token encountered during the parsing of the assignment-expression
21339 is not interpreted as a greater-than operator.)
21341 Returns a representation of the parameter, or NULL if an error
21342 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21343 true iff the declarator is of the form "(p)". */
21345 static cp_parameter_declarator *
21346 cp_parser_parameter_declaration (cp_parser *parser,
21347 bool template_parm_p,
21348 bool *parenthesized_p)
21350 int declares_class_or_enum;
21351 cp_decl_specifier_seq decl_specifiers;
21352 cp_declarator *declarator;
21353 tree default_argument;
21354 cp_token *token = NULL, *declarator_token_start = NULL;
21355 const char *saved_message;
21356 bool template_parameter_pack_p = false;
21358 /* In a template parameter, `>' is not an operator.
21360 [temp.param]
21362 When parsing a default template-argument for a non-type
21363 template-parameter, the first non-nested `>' is taken as the end
21364 of the template parameter-list rather than a greater-than
21365 operator. */
21367 /* Type definitions may not appear in parameter types. */
21368 saved_message = parser->type_definition_forbidden_message;
21369 parser->type_definition_forbidden_message
21370 = G_("types may not be defined in parameter types");
21372 /* Parse the declaration-specifiers. */
21373 cp_parser_decl_specifier_seq (parser,
21374 CP_PARSER_FLAGS_NONE,
21375 &decl_specifiers,
21376 &declares_class_or_enum);
21378 /* Complain about missing 'typename' or other invalid type names. */
21379 if (!decl_specifiers.any_type_specifiers_p
21380 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21381 decl_specifiers.type = error_mark_node;
21383 /* If an error occurred, there's no reason to attempt to parse the
21384 rest of the declaration. */
21385 if (cp_parser_error_occurred (parser))
21387 parser->type_definition_forbidden_message = saved_message;
21388 return NULL;
21391 /* Peek at the next token. */
21392 token = cp_lexer_peek_token (parser->lexer);
21394 /* If the next token is a `)', `,', `=', `>', or `...', then there
21395 is no declarator. However, when variadic templates are enabled,
21396 there may be a declarator following `...'. */
21397 if (token->type == CPP_CLOSE_PAREN
21398 || token->type == CPP_COMMA
21399 || token->type == CPP_EQ
21400 || token->type == CPP_GREATER)
21402 declarator = NULL;
21403 if (parenthesized_p)
21404 *parenthesized_p = false;
21406 /* Otherwise, there should be a declarator. */
21407 else
21409 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21410 parser->default_arg_ok_p = false;
21412 /* After seeing a decl-specifier-seq, if the next token is not a
21413 "(", there is no possibility that the code is a valid
21414 expression. Therefore, if parsing tentatively, we commit at
21415 this point. */
21416 if (!parser->in_template_argument_list_p
21417 /* In an expression context, having seen:
21419 (int((char ...
21421 we cannot be sure whether we are looking at a
21422 function-type (taking a "char" as a parameter) or a cast
21423 of some object of type "char" to "int". */
21424 && !parser->in_type_id_in_expr_p
21425 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21426 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21427 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21428 cp_parser_commit_to_tentative_parse (parser);
21429 /* Parse the declarator. */
21430 declarator_token_start = token;
21431 declarator = cp_parser_declarator (parser,
21432 CP_PARSER_DECLARATOR_EITHER,
21433 /*ctor_dtor_or_conv_p=*/NULL,
21434 parenthesized_p,
21435 /*member_p=*/false,
21436 /*friend_p=*/false);
21437 parser->default_arg_ok_p = saved_default_arg_ok_p;
21438 /* After the declarator, allow more attributes. */
21439 decl_specifiers.attributes
21440 = chainon (decl_specifiers.attributes,
21441 cp_parser_attributes_opt (parser));
21443 /* If the declarator is a template parameter pack, remember that and
21444 clear the flag in the declarator itself so we don't get errors
21445 from grokdeclarator. */
21446 if (template_parm_p && declarator && declarator->parameter_pack_p)
21448 declarator->parameter_pack_p = false;
21449 template_parameter_pack_p = true;
21453 /* If the next token is an ellipsis, and we have not seen a declarator
21454 name, and if either the type of the declarator contains parameter
21455 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21456 for, eg, abbreviated integral type names), then we actually have a
21457 parameter pack expansion expression. Otherwise, leave the ellipsis
21458 for a C-style variadic function. */
21459 token = cp_lexer_peek_token (parser->lexer);
21460 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21462 tree type = decl_specifiers.type;
21464 if (type && DECL_P (type))
21465 type = TREE_TYPE (type);
21467 if (((type
21468 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21469 && (template_parm_p || uses_parameter_packs (type)))
21470 || (!type && template_parm_p))
21471 && declarator_can_be_parameter_pack (declarator))
21473 /* Consume the `...'. */
21474 cp_lexer_consume_token (parser->lexer);
21475 maybe_warn_variadic_templates ();
21477 /* Build a pack expansion type */
21478 if (template_parm_p)
21479 template_parameter_pack_p = true;
21480 else if (declarator)
21481 declarator->parameter_pack_p = true;
21482 else
21483 decl_specifiers.type = make_pack_expansion (type);
21487 /* The restriction on defining new types applies only to the type
21488 of the parameter, not to the default argument. */
21489 parser->type_definition_forbidden_message = saved_message;
21491 /* If the next token is `=', then process a default argument. */
21492 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21494 tree type = decl_specifiers.type;
21495 token = cp_lexer_peek_token (parser->lexer);
21496 /* If we are defining a class, then the tokens that make up the
21497 default argument must be saved and processed later. */
21498 if (!template_parm_p && at_class_scope_p ()
21499 && TYPE_BEING_DEFINED (current_class_type)
21500 && !LAMBDA_TYPE_P (current_class_type))
21501 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21503 // A constrained-type-specifier may declare a type template-parameter.
21504 else if (declares_constrained_type_template_parameter (type))
21505 default_argument
21506 = cp_parser_default_type_template_argument (parser);
21508 // A constrained-type-specifier may declare a template-template-parameter.
21509 else if (declares_constrained_template_template_parameter (type))
21510 default_argument
21511 = cp_parser_default_template_template_argument (parser);
21513 /* Outside of a class definition, we can just parse the
21514 assignment-expression. */
21515 else
21516 default_argument
21517 = cp_parser_default_argument (parser, template_parm_p);
21519 if (!parser->default_arg_ok_p)
21521 permerror (token->location,
21522 "default arguments are only "
21523 "permitted for function parameters");
21525 else if ((declarator && declarator->parameter_pack_p)
21526 || template_parameter_pack_p
21527 || (decl_specifiers.type
21528 && PACK_EXPANSION_P (decl_specifiers.type)))
21530 /* Find the name of the parameter pack. */
21531 cp_declarator *id_declarator = declarator;
21532 while (id_declarator && id_declarator->kind != cdk_id)
21533 id_declarator = id_declarator->declarator;
21535 if (id_declarator && id_declarator->kind == cdk_id)
21536 error_at (declarator_token_start->location,
21537 template_parm_p
21538 ? G_("template parameter pack %qD "
21539 "cannot have a default argument")
21540 : G_("parameter pack %qD cannot have "
21541 "a default argument"),
21542 id_declarator->u.id.unqualified_name);
21543 else
21544 error_at (declarator_token_start->location,
21545 template_parm_p
21546 ? G_("template parameter pack cannot have "
21547 "a default argument")
21548 : G_("parameter pack cannot have a "
21549 "default argument"));
21551 default_argument = NULL_TREE;
21554 else
21555 default_argument = NULL_TREE;
21557 return make_parameter_declarator (&decl_specifiers,
21558 declarator,
21559 default_argument,
21560 template_parameter_pack_p);
21563 /* Parse a default argument and return it.
21565 TEMPLATE_PARM_P is true if this is a default argument for a
21566 non-type template parameter. */
21567 static tree
21568 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21570 tree default_argument = NULL_TREE;
21571 bool saved_greater_than_is_operator_p;
21572 bool saved_local_variables_forbidden_p;
21573 bool non_constant_p, is_direct_init;
21575 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21576 set correctly. */
21577 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21578 parser->greater_than_is_operator_p = !template_parm_p;
21579 /* Local variable names (and the `this' keyword) may not
21580 appear in a default argument. */
21581 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21582 parser->local_variables_forbidden_p = true;
21583 /* Parse the assignment-expression. */
21584 if (template_parm_p)
21585 push_deferring_access_checks (dk_no_deferred);
21586 tree saved_class_ptr = NULL_TREE;
21587 tree saved_class_ref = NULL_TREE;
21588 /* The "this" pointer is not valid in a default argument. */
21589 if (cfun)
21591 saved_class_ptr = current_class_ptr;
21592 cp_function_chain->x_current_class_ptr = NULL_TREE;
21593 saved_class_ref = current_class_ref;
21594 cp_function_chain->x_current_class_ref = NULL_TREE;
21596 default_argument
21597 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21598 /* Restore the "this" pointer. */
21599 if (cfun)
21601 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21602 cp_function_chain->x_current_class_ref = saved_class_ref;
21604 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21605 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21606 if (template_parm_p)
21607 pop_deferring_access_checks ();
21608 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21609 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21611 return default_argument;
21614 /* Parse a function-body.
21616 function-body:
21617 compound_statement */
21619 static void
21620 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21622 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21623 ? BCS_TRY_BLOCK : BCS_NORMAL),
21624 true);
21627 /* Parse a ctor-initializer-opt followed by a function-body. Return
21628 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21629 is true we are parsing a function-try-block. */
21631 static bool
21632 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21633 bool in_function_try_block)
21635 tree body, list;
21636 bool ctor_initializer_p;
21637 const bool check_body_p =
21638 DECL_CONSTRUCTOR_P (current_function_decl)
21639 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21640 tree last = NULL;
21642 /* Begin the function body. */
21643 body = begin_function_body ();
21644 /* Parse the optional ctor-initializer. */
21645 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21647 /* If we're parsing a constexpr constructor definition, we need
21648 to check that the constructor body is indeed empty. However,
21649 before we get to cp_parser_function_body lot of junk has been
21650 generated, so we can't just check that we have an empty block.
21651 Rather we take a snapshot of the outermost block, and check whether
21652 cp_parser_function_body changed its state. */
21653 if (check_body_p)
21655 list = cur_stmt_list;
21656 if (STATEMENT_LIST_TAIL (list))
21657 last = STATEMENT_LIST_TAIL (list)->stmt;
21659 /* Parse the function-body. */
21660 cp_parser_function_body (parser, in_function_try_block);
21661 if (check_body_p)
21662 check_constexpr_ctor_body (last, list, /*complain=*/true);
21663 /* Finish the function body. */
21664 finish_function_body (body);
21666 return ctor_initializer_p;
21669 /* Parse an initializer.
21671 initializer:
21672 = initializer-clause
21673 ( expression-list )
21675 Returns an expression representing the initializer. If no
21676 initializer is present, NULL_TREE is returned.
21678 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21679 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21680 set to TRUE if there is no initializer present. If there is an
21681 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21682 is set to true; otherwise it is set to false. */
21684 static tree
21685 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21686 bool* non_constant_p)
21688 cp_token *token;
21689 tree init;
21691 /* Peek at the next token. */
21692 token = cp_lexer_peek_token (parser->lexer);
21694 /* Let our caller know whether or not this initializer was
21695 parenthesized. */
21696 *is_direct_init = (token->type != CPP_EQ);
21697 /* Assume that the initializer is constant. */
21698 *non_constant_p = false;
21700 if (token->type == CPP_EQ)
21702 /* Consume the `='. */
21703 cp_lexer_consume_token (parser->lexer);
21704 /* Parse the initializer-clause. */
21705 init = cp_parser_initializer_clause (parser, non_constant_p);
21707 else if (token->type == CPP_OPEN_PAREN)
21709 vec<tree, va_gc> *vec;
21710 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21711 /*cast_p=*/false,
21712 /*allow_expansion_p=*/true,
21713 non_constant_p);
21714 if (vec == NULL)
21715 return error_mark_node;
21716 init = build_tree_list_vec (vec);
21717 release_tree_vector (vec);
21719 else if (token->type == CPP_OPEN_BRACE)
21721 cp_lexer_set_source_position (parser->lexer);
21722 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21723 init = cp_parser_braced_list (parser, non_constant_p);
21724 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21726 else
21728 /* Anything else is an error. */
21729 cp_parser_error (parser, "expected initializer");
21730 init = error_mark_node;
21733 if (check_for_bare_parameter_packs (init))
21734 init = error_mark_node;
21736 return init;
21739 /* Parse an initializer-clause.
21741 initializer-clause:
21742 assignment-expression
21743 braced-init-list
21745 Returns an expression representing the initializer.
21747 If the `assignment-expression' production is used the value
21748 returned is simply a representation for the expression.
21750 Otherwise, calls cp_parser_braced_list. */
21752 static cp_expr
21753 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21755 cp_expr initializer;
21757 /* Assume the expression is constant. */
21758 *non_constant_p = false;
21760 /* If it is not a `{', then we are looking at an
21761 assignment-expression. */
21762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21764 initializer
21765 = cp_parser_constant_expression (parser,
21766 /*allow_non_constant_p=*/true,
21767 non_constant_p);
21769 else
21770 initializer = cp_parser_braced_list (parser, non_constant_p);
21772 return initializer;
21775 /* Parse a brace-enclosed initializer list.
21777 braced-init-list:
21778 { initializer-list , [opt] }
21781 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21782 the elements of the initializer-list (or NULL, if the last
21783 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21784 NULL_TREE. There is no way to detect whether or not the optional
21785 trailing `,' was provided. NON_CONSTANT_P is as for
21786 cp_parser_initializer. */
21788 static cp_expr
21789 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21791 tree initializer;
21792 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21794 /* Consume the `{' token. */
21795 matching_braces braces;
21796 braces.consume_open (parser);
21797 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21798 initializer = make_node (CONSTRUCTOR);
21799 /* If it's not a `}', then there is a non-trivial initializer. */
21800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21802 /* Parse the initializer list. */
21803 CONSTRUCTOR_ELTS (initializer)
21804 = cp_parser_initializer_list (parser, non_constant_p);
21805 /* A trailing `,' token is allowed. */
21806 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21807 cp_lexer_consume_token (parser->lexer);
21809 else
21810 *non_constant_p = false;
21811 /* Now, there should be a trailing `}'. */
21812 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21813 braces.require_close (parser);
21814 TREE_TYPE (initializer) = init_list_type_node;
21816 cp_expr result (initializer);
21817 /* Build a location of the form:
21818 { ... }
21819 ^~~~~~~
21820 with caret==start at the open brace, finish at the close brace. */
21821 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21822 result.set_location (combined_loc);
21823 return result;
21826 /* Consume tokens up to, and including, the next non-nested closing `]'.
21827 Returns true iff we found a closing `]'. */
21829 static bool
21830 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21832 unsigned square_depth = 0;
21834 while (true)
21836 cp_token * token = cp_lexer_peek_token (parser->lexer);
21838 switch (token->type)
21840 case CPP_EOF:
21841 case CPP_PRAGMA_EOL:
21842 /* If we've run out of tokens, then there is no closing `]'. */
21843 return false;
21845 case CPP_OPEN_SQUARE:
21846 ++square_depth;
21847 break;
21849 case CPP_CLOSE_SQUARE:
21850 if (!square_depth--)
21852 cp_lexer_consume_token (parser->lexer);
21853 return true;
21855 break;
21857 default:
21858 break;
21861 /* Consume the token. */
21862 cp_lexer_consume_token (parser->lexer);
21866 /* Return true if we are looking at an array-designator, false otherwise. */
21868 static bool
21869 cp_parser_array_designator_p (cp_parser *parser)
21871 /* Consume the `['. */
21872 cp_lexer_consume_token (parser->lexer);
21874 cp_lexer_save_tokens (parser->lexer);
21876 /* Skip tokens until the next token is a closing square bracket.
21877 If we find the closing `]', and the next token is a `=', then
21878 we are looking at an array designator. */
21879 bool array_designator_p
21880 = (cp_parser_skip_to_closing_square_bracket (parser)
21881 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21883 /* Roll back the tokens we skipped. */
21884 cp_lexer_rollback_tokens (parser->lexer);
21886 return array_designator_p;
21889 /* Parse an initializer-list.
21891 initializer-list:
21892 initializer-clause ... [opt]
21893 initializer-list , initializer-clause ... [opt]
21895 GNU Extension:
21897 initializer-list:
21898 designation initializer-clause ...[opt]
21899 initializer-list , designation initializer-clause ...[opt]
21901 designation:
21902 . identifier =
21903 identifier :
21904 [ constant-expression ] =
21906 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21907 for the initializer. If the INDEX of the elt is non-NULL, it is the
21908 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21909 as for cp_parser_initializer. */
21911 static vec<constructor_elt, va_gc> *
21912 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21914 vec<constructor_elt, va_gc> *v = NULL;
21916 /* Assume all of the expressions are constant. */
21917 *non_constant_p = false;
21919 /* Parse the rest of the list. */
21920 while (true)
21922 cp_token *token;
21923 tree designator;
21924 tree initializer;
21925 bool clause_non_constant_p;
21927 /* If the next token is an identifier and the following one is a
21928 colon, we are looking at the GNU designated-initializer
21929 syntax. */
21930 if (cp_parser_allow_gnu_extensions_p (parser)
21931 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21932 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21934 /* Warn the user that they are using an extension. */
21935 pedwarn (input_location, OPT_Wpedantic,
21936 "ISO C++ does not allow designated initializers");
21937 /* Consume the identifier. */
21938 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21939 /* Consume the `:'. */
21940 cp_lexer_consume_token (parser->lexer);
21942 /* Also handle the C99 syntax, '. id ='. */
21943 else if (cp_parser_allow_gnu_extensions_p (parser)
21944 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21945 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21946 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21948 /* Warn the user that they are using an extension. */
21949 pedwarn (input_location, OPT_Wpedantic,
21950 "ISO C++ does not allow C99 designated initializers");
21951 /* Consume the `.'. */
21952 cp_lexer_consume_token (parser->lexer);
21953 /* Consume the identifier. */
21954 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21955 /* Consume the `='. */
21956 cp_lexer_consume_token (parser->lexer);
21958 /* Also handle C99 array designators, '[ const ] ='. */
21959 else if (cp_parser_allow_gnu_extensions_p (parser)
21960 && !c_dialect_objc ()
21961 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21963 /* In C++11, [ could start a lambda-introducer. */
21964 bool non_const = false;
21966 cp_parser_parse_tentatively (parser);
21968 if (!cp_parser_array_designator_p (parser))
21970 cp_parser_simulate_error (parser);
21971 designator = NULL_TREE;
21973 else
21975 designator = cp_parser_constant_expression (parser, true,
21976 &non_const);
21977 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21978 cp_parser_require (parser, CPP_EQ, RT_EQ);
21981 if (!cp_parser_parse_definitely (parser))
21982 designator = NULL_TREE;
21983 else if (non_const)
21984 require_potential_rvalue_constant_expression (designator);
21986 else
21987 designator = NULL_TREE;
21989 /* Parse the initializer. */
21990 initializer = cp_parser_initializer_clause (parser,
21991 &clause_non_constant_p);
21992 /* If any clause is non-constant, so is the entire initializer. */
21993 if (clause_non_constant_p)
21994 *non_constant_p = true;
21996 /* If we have an ellipsis, this is an initializer pack
21997 expansion. */
21998 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22000 /* Consume the `...'. */
22001 cp_lexer_consume_token (parser->lexer);
22003 /* Turn the initializer into an initializer expansion. */
22004 initializer = make_pack_expansion (initializer);
22007 /* Add it to the vector. */
22008 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22010 /* If the next token is not a comma, we have reached the end of
22011 the list. */
22012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22013 break;
22015 /* Peek at the next token. */
22016 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22017 /* If the next token is a `}', then we're still done. An
22018 initializer-clause can have a trailing `,' after the
22019 initializer-list and before the closing `}'. */
22020 if (token->type == CPP_CLOSE_BRACE)
22021 break;
22023 /* Consume the `,' token. */
22024 cp_lexer_consume_token (parser->lexer);
22027 return v;
22030 /* Classes [gram.class] */
22032 /* Parse a class-name.
22034 class-name:
22035 identifier
22036 template-id
22038 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22039 to indicate that names looked up in dependent types should be
22040 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22041 keyword has been used to indicate that the name that appears next
22042 is a template. TAG_TYPE indicates the explicit tag given before
22043 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22044 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22045 is the class being defined in a class-head. If ENUM_OK is TRUE,
22046 enum-names are also accepted.
22048 Returns the TYPE_DECL representing the class. */
22050 static tree
22051 cp_parser_class_name (cp_parser *parser,
22052 bool typename_keyword_p,
22053 bool template_keyword_p,
22054 enum tag_types tag_type,
22055 bool check_dependency_p,
22056 bool class_head_p,
22057 bool is_declaration,
22058 bool enum_ok)
22060 tree decl;
22061 tree scope;
22062 bool typename_p;
22063 cp_token *token;
22064 tree identifier = NULL_TREE;
22066 /* All class-names start with an identifier. */
22067 token = cp_lexer_peek_token (parser->lexer);
22068 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22070 cp_parser_error (parser, "expected class-name");
22071 return error_mark_node;
22074 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22075 to a template-id, so we save it here. */
22076 scope = parser->scope;
22077 if (scope == error_mark_node)
22078 return error_mark_node;
22080 /* Any name names a type if we're following the `typename' keyword
22081 in a qualified name where the enclosing scope is type-dependent. */
22082 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22083 && dependent_type_p (scope));
22084 /* Handle the common case (an identifier, but not a template-id)
22085 efficiently. */
22086 if (token->type == CPP_NAME
22087 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22089 cp_token *identifier_token;
22090 bool ambiguous_p;
22092 /* Look for the identifier. */
22093 identifier_token = cp_lexer_peek_token (parser->lexer);
22094 ambiguous_p = identifier_token->error_reported;
22095 identifier = cp_parser_identifier (parser);
22096 /* If the next token isn't an identifier, we are certainly not
22097 looking at a class-name. */
22098 if (identifier == error_mark_node)
22099 decl = error_mark_node;
22100 /* If we know this is a type-name, there's no need to look it
22101 up. */
22102 else if (typename_p)
22103 decl = identifier;
22104 else
22106 tree ambiguous_decls;
22107 /* If we already know that this lookup is ambiguous, then
22108 we've already issued an error message; there's no reason
22109 to check again. */
22110 if (ambiguous_p)
22112 cp_parser_simulate_error (parser);
22113 return error_mark_node;
22115 /* If the next token is a `::', then the name must be a type
22116 name.
22118 [basic.lookup.qual]
22120 During the lookup for a name preceding the :: scope
22121 resolution operator, object, function, and enumerator
22122 names are ignored. */
22123 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22124 tag_type = scope_type;
22125 /* Look up the name. */
22126 decl = cp_parser_lookup_name (parser, identifier,
22127 tag_type,
22128 /*is_template=*/false,
22129 /*is_namespace=*/false,
22130 check_dependency_p,
22131 &ambiguous_decls,
22132 identifier_token->location);
22133 if (ambiguous_decls)
22135 if (cp_parser_parsing_tentatively (parser))
22136 cp_parser_simulate_error (parser);
22137 return error_mark_node;
22141 else
22143 /* Try a template-id. */
22144 decl = cp_parser_template_id (parser, template_keyword_p,
22145 check_dependency_p,
22146 tag_type,
22147 is_declaration);
22148 if (decl == error_mark_node)
22149 return error_mark_node;
22152 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22154 /* If this is a typename, create a TYPENAME_TYPE. */
22155 if (typename_p && decl != error_mark_node)
22157 decl = make_typename_type (scope, decl, typename_type,
22158 /*complain=*/tf_error);
22159 if (decl != error_mark_node)
22160 decl = TYPE_NAME (decl);
22163 decl = strip_using_decl (decl);
22165 /* Check to see that it is really the name of a class. */
22166 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22167 && identifier_p (TREE_OPERAND (decl, 0))
22168 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22169 /* Situations like this:
22171 template <typename T> struct A {
22172 typename T::template X<int>::I i;
22175 are problematic. Is `T::template X<int>' a class-name? The
22176 standard does not seem to be definitive, but there is no other
22177 valid interpretation of the following `::'. Therefore, those
22178 names are considered class-names. */
22180 decl = make_typename_type (scope, decl, tag_type, tf_error);
22181 if (decl != error_mark_node)
22182 decl = TYPE_NAME (decl);
22184 else if (TREE_CODE (decl) != TYPE_DECL
22185 || TREE_TYPE (decl) == error_mark_node
22186 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22187 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22188 /* In Objective-C 2.0, a classname followed by '.' starts a
22189 dot-syntax expression, and it's not a type-name. */
22190 || (c_dialect_objc ()
22191 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22192 && objc_is_class_name (decl)))
22193 decl = error_mark_node;
22195 if (decl == error_mark_node)
22196 cp_parser_error (parser, "expected class-name");
22197 else if (identifier && !parser->scope)
22198 maybe_note_name_used_in_class (identifier, decl);
22200 return decl;
22203 /* Parse a class-specifier.
22205 class-specifier:
22206 class-head { member-specification [opt] }
22208 Returns the TREE_TYPE representing the class. */
22210 static tree
22211 cp_parser_class_specifier_1 (cp_parser* parser)
22213 tree type;
22214 tree attributes = NULL_TREE;
22215 bool nested_name_specifier_p;
22216 unsigned saved_num_template_parameter_lists;
22217 bool saved_in_function_body;
22218 unsigned char in_statement;
22219 bool in_switch_statement_p;
22220 bool saved_in_unbraced_linkage_specification_p;
22221 tree old_scope = NULL_TREE;
22222 tree scope = NULL_TREE;
22223 cp_token *closing_brace;
22225 push_deferring_access_checks (dk_no_deferred);
22227 /* Parse the class-head. */
22228 type = cp_parser_class_head (parser,
22229 &nested_name_specifier_p);
22230 /* If the class-head was a semantic disaster, skip the entire body
22231 of the class. */
22232 if (!type)
22234 cp_parser_skip_to_end_of_block_or_statement (parser);
22235 pop_deferring_access_checks ();
22236 return error_mark_node;
22239 /* Look for the `{'. */
22240 matching_braces braces;
22241 if (!braces.require_open (parser))
22243 pop_deferring_access_checks ();
22244 return error_mark_node;
22247 cp_ensure_no_omp_declare_simd (parser);
22248 cp_ensure_no_oacc_routine (parser);
22250 /* Issue an error message if type-definitions are forbidden here. */
22251 cp_parser_check_type_definition (parser);
22252 /* Remember that we are defining one more class. */
22253 ++parser->num_classes_being_defined;
22254 /* Inside the class, surrounding template-parameter-lists do not
22255 apply. */
22256 saved_num_template_parameter_lists
22257 = parser->num_template_parameter_lists;
22258 parser->num_template_parameter_lists = 0;
22259 /* We are not in a function body. */
22260 saved_in_function_body = parser->in_function_body;
22261 parser->in_function_body = false;
22262 /* Or in a loop. */
22263 in_statement = parser->in_statement;
22264 parser->in_statement = 0;
22265 /* Or in a switch. */
22266 in_switch_statement_p = parser->in_switch_statement_p;
22267 parser->in_switch_statement_p = false;
22268 /* We are not immediately inside an extern "lang" block. */
22269 saved_in_unbraced_linkage_specification_p
22270 = parser->in_unbraced_linkage_specification_p;
22271 parser->in_unbraced_linkage_specification_p = false;
22273 // Associate constraints with the type.
22274 if (flag_concepts)
22275 type = associate_classtype_constraints (type);
22277 /* Start the class. */
22278 if (nested_name_specifier_p)
22280 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22281 old_scope = push_inner_scope (scope);
22283 type = begin_class_definition (type);
22285 if (type == error_mark_node)
22286 /* If the type is erroneous, skip the entire body of the class. */
22287 cp_parser_skip_to_closing_brace (parser);
22288 else
22289 /* Parse the member-specification. */
22290 cp_parser_member_specification_opt (parser);
22292 /* Look for the trailing `}'. */
22293 closing_brace = braces.require_close (parser);
22294 /* Look for trailing attributes to apply to this class. */
22295 if (cp_parser_allow_gnu_extensions_p (parser))
22296 attributes = cp_parser_gnu_attributes_opt (parser);
22297 if (type != error_mark_node)
22298 type = finish_struct (type, attributes);
22299 if (nested_name_specifier_p)
22300 pop_inner_scope (old_scope, scope);
22302 /* We've finished a type definition. Check for the common syntax
22303 error of forgetting a semicolon after the definition. We need to
22304 be careful, as we can't just check for not-a-semicolon and be done
22305 with it; the user might have typed:
22307 class X { } c = ...;
22308 class X { } *p = ...;
22310 and so forth. Instead, enumerate all the possible tokens that
22311 might follow this production; if we don't see one of them, then
22312 complain and silently insert the semicolon. */
22314 cp_token *token = cp_lexer_peek_token (parser->lexer);
22315 bool want_semicolon = true;
22317 if (cp_next_tokens_can_be_std_attribute_p (parser))
22318 /* Don't try to parse c++11 attributes here. As per the
22319 grammar, that should be a task for
22320 cp_parser_decl_specifier_seq. */
22321 want_semicolon = false;
22323 switch (token->type)
22325 case CPP_NAME:
22326 case CPP_SEMICOLON:
22327 case CPP_MULT:
22328 case CPP_AND:
22329 case CPP_OPEN_PAREN:
22330 case CPP_CLOSE_PAREN:
22331 case CPP_COMMA:
22332 want_semicolon = false;
22333 break;
22335 /* While it's legal for type qualifiers and storage class
22336 specifiers to follow type definitions in the grammar, only
22337 compiler testsuites contain code like that. Assume that if
22338 we see such code, then what we're really seeing is a case
22339 like:
22341 class X { }
22342 const <type> var = ...;
22346 class Y { }
22347 static <type> func (...) ...
22349 i.e. the qualifier or specifier applies to the next
22350 declaration. To do so, however, we need to look ahead one
22351 more token to see if *that* token is a type specifier.
22353 This code could be improved to handle:
22355 class Z { }
22356 static const <type> var = ...; */
22357 case CPP_KEYWORD:
22358 if (keyword_is_decl_specifier (token->keyword))
22360 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22362 /* Handling user-defined types here would be nice, but very
22363 tricky. */
22364 want_semicolon
22365 = (lookahead->type == CPP_KEYWORD
22366 && keyword_begins_type_specifier (lookahead->keyword));
22368 break;
22369 default:
22370 break;
22373 /* If we don't have a type, then something is very wrong and we
22374 shouldn't try to do anything clever. Likewise for not seeing the
22375 closing brace. */
22376 if (closing_brace && TYPE_P (type) && want_semicolon)
22378 /* Locate the closing brace. */
22379 cp_token_position prev
22380 = cp_lexer_previous_token_position (parser->lexer);
22381 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22382 location_t loc = prev_token->location;
22384 /* We want to suggest insertion of a ';' immediately *after* the
22385 closing brace, so, if we can, offset the location by 1 column. */
22386 location_t next_loc = loc;
22387 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22388 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22390 rich_location richloc (line_table, next_loc);
22392 /* If we successfully offset the location, suggest the fix-it. */
22393 if (next_loc != loc)
22394 richloc.add_fixit_insert_before (next_loc, ";");
22396 if (CLASSTYPE_DECLARED_CLASS (type))
22397 error_at_rich_loc (&richloc,
22398 "expected %<;%> after class definition");
22399 else if (TREE_CODE (type) == RECORD_TYPE)
22400 error_at_rich_loc (&richloc,
22401 "expected %<;%> after struct definition");
22402 else if (TREE_CODE (type) == UNION_TYPE)
22403 error_at_rich_loc (&richloc,
22404 "expected %<;%> after union definition");
22405 else
22406 gcc_unreachable ();
22408 /* Unget one token and smash it to look as though we encountered
22409 a semicolon in the input stream. */
22410 cp_lexer_set_token_position (parser->lexer, prev);
22411 token = cp_lexer_peek_token (parser->lexer);
22412 token->type = CPP_SEMICOLON;
22413 token->keyword = RID_MAX;
22417 /* If this class is not itself within the scope of another class,
22418 then we need to parse the bodies of all of the queued function
22419 definitions. Note that the queued functions defined in a class
22420 are not always processed immediately following the
22421 class-specifier for that class. Consider:
22423 struct A {
22424 struct B { void f() { sizeof (A); } };
22427 If `f' were processed before the processing of `A' were
22428 completed, there would be no way to compute the size of `A'.
22429 Note that the nesting we are interested in here is lexical --
22430 not the semantic nesting given by TYPE_CONTEXT. In particular,
22431 for:
22433 struct A { struct B; };
22434 struct A::B { void f() { } };
22436 there is no need to delay the parsing of `A::B::f'. */
22437 if (--parser->num_classes_being_defined == 0)
22439 tree decl;
22440 tree class_type = NULL_TREE;
22441 tree pushed_scope = NULL_TREE;
22442 unsigned ix;
22443 cp_default_arg_entry *e;
22444 tree save_ccp, save_ccr;
22446 /* In a first pass, parse default arguments to the functions.
22447 Then, in a second pass, parse the bodies of the functions.
22448 This two-phased approach handles cases like:
22450 struct S {
22451 void f() { g(); }
22452 void g(int i = 3);
22456 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22458 decl = e->decl;
22459 /* If there are default arguments that have not yet been processed,
22460 take care of them now. */
22461 if (class_type != e->class_type)
22463 if (pushed_scope)
22464 pop_scope (pushed_scope);
22465 class_type = e->class_type;
22466 pushed_scope = push_scope (class_type);
22468 /* Make sure that any template parameters are in scope. */
22469 maybe_begin_member_template_processing (decl);
22470 /* Parse the default argument expressions. */
22471 cp_parser_late_parsing_default_args (parser, decl);
22472 /* Remove any template parameters from the symbol table. */
22473 maybe_end_member_template_processing ();
22475 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22476 /* Now parse any NSDMIs. */
22477 save_ccp = current_class_ptr;
22478 save_ccr = current_class_ref;
22479 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22481 if (class_type != DECL_CONTEXT (decl))
22483 if (pushed_scope)
22484 pop_scope (pushed_scope);
22485 class_type = DECL_CONTEXT (decl);
22486 pushed_scope = push_scope (class_type);
22488 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22489 cp_parser_late_parsing_nsdmi (parser, decl);
22491 vec_safe_truncate (unparsed_nsdmis, 0);
22492 current_class_ptr = save_ccp;
22493 current_class_ref = save_ccr;
22494 if (pushed_scope)
22495 pop_scope (pushed_scope);
22497 /* Now do some post-NSDMI bookkeeping. */
22498 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22499 after_nsdmi_defaulted_late_checks (class_type);
22500 vec_safe_truncate (unparsed_classes, 0);
22501 after_nsdmi_defaulted_late_checks (type);
22503 /* Now parse the body of the functions. */
22504 if (flag_openmp)
22506 /* OpenMP UDRs need to be parsed before all other functions. */
22507 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22508 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22509 cp_parser_late_parsing_for_member (parser, decl);
22510 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22511 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22512 cp_parser_late_parsing_for_member (parser, decl);
22514 else
22515 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22516 cp_parser_late_parsing_for_member (parser, decl);
22517 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22519 else
22520 vec_safe_push (unparsed_classes, type);
22522 /* Put back any saved access checks. */
22523 pop_deferring_access_checks ();
22525 /* Restore saved state. */
22526 parser->in_switch_statement_p = in_switch_statement_p;
22527 parser->in_statement = in_statement;
22528 parser->in_function_body = saved_in_function_body;
22529 parser->num_template_parameter_lists
22530 = saved_num_template_parameter_lists;
22531 parser->in_unbraced_linkage_specification_p
22532 = saved_in_unbraced_linkage_specification_p;
22534 return type;
22537 static tree
22538 cp_parser_class_specifier (cp_parser* parser)
22540 tree ret;
22541 timevar_push (TV_PARSE_STRUCT);
22542 ret = cp_parser_class_specifier_1 (parser);
22543 timevar_pop (TV_PARSE_STRUCT);
22544 return ret;
22547 /* Parse a class-head.
22549 class-head:
22550 class-key identifier [opt] base-clause [opt]
22551 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22552 class-key nested-name-specifier [opt] template-id
22553 base-clause [opt]
22555 class-virt-specifier:
22556 final
22558 GNU Extensions:
22559 class-key attributes identifier [opt] base-clause [opt]
22560 class-key attributes nested-name-specifier identifier base-clause [opt]
22561 class-key attributes nested-name-specifier [opt] template-id
22562 base-clause [opt]
22564 Upon return BASES is initialized to the list of base classes (or
22565 NULL, if there are none) in the same form returned by
22566 cp_parser_base_clause.
22568 Returns the TYPE of the indicated class. Sets
22569 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22570 involving a nested-name-specifier was used, and FALSE otherwise.
22572 Returns error_mark_node if this is not a class-head.
22574 Returns NULL_TREE if the class-head is syntactically valid, but
22575 semantically invalid in a way that means we should skip the entire
22576 body of the class. */
22578 static tree
22579 cp_parser_class_head (cp_parser* parser,
22580 bool* nested_name_specifier_p)
22582 tree nested_name_specifier;
22583 enum tag_types class_key;
22584 tree id = NULL_TREE;
22585 tree type = NULL_TREE;
22586 tree attributes;
22587 tree bases;
22588 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22589 bool template_id_p = false;
22590 bool qualified_p = false;
22591 bool invalid_nested_name_p = false;
22592 bool invalid_explicit_specialization_p = false;
22593 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22594 tree pushed_scope = NULL_TREE;
22595 unsigned num_templates;
22596 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22597 /* Assume no nested-name-specifier will be present. */
22598 *nested_name_specifier_p = false;
22599 /* Assume no template parameter lists will be used in defining the
22600 type. */
22601 num_templates = 0;
22602 parser->colon_corrects_to_scope_p = false;
22604 /* Look for the class-key. */
22605 class_key = cp_parser_class_key (parser);
22606 if (class_key == none_type)
22607 return error_mark_node;
22609 location_t class_head_start_location = input_location;
22611 /* Parse the attributes. */
22612 attributes = cp_parser_attributes_opt (parser);
22614 /* If the next token is `::', that is invalid -- but sometimes
22615 people do try to write:
22617 struct ::S {};
22619 Handle this gracefully by accepting the extra qualifier, and then
22620 issuing an error about it later if this really is a
22621 class-head. If it turns out just to be an elaborated type
22622 specifier, remain silent. */
22623 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22624 qualified_p = true;
22626 push_deferring_access_checks (dk_no_check);
22628 /* Determine the name of the class. Begin by looking for an
22629 optional nested-name-specifier. */
22630 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22631 nested_name_specifier
22632 = cp_parser_nested_name_specifier_opt (parser,
22633 /*typename_keyword_p=*/false,
22634 /*check_dependency_p=*/false,
22635 /*type_p=*/true,
22636 /*is_declaration=*/false);
22637 /* If there was a nested-name-specifier, then there *must* be an
22638 identifier. */
22640 cp_token *bad_template_keyword = NULL;
22642 if (nested_name_specifier)
22644 type_start_token = cp_lexer_peek_token (parser->lexer);
22645 /* Although the grammar says `identifier', it really means
22646 `class-name' or `template-name'. You are only allowed to
22647 define a class that has already been declared with this
22648 syntax.
22650 The proposed resolution for Core Issue 180 says that wherever
22651 you see `class T::X' you should treat `X' as a type-name.
22653 It is OK to define an inaccessible class; for example:
22655 class A { class B; };
22656 class A::B {};
22658 We do not know if we will see a class-name, or a
22659 template-name. We look for a class-name first, in case the
22660 class-name is a template-id; if we looked for the
22661 template-name first we would stop after the template-name. */
22662 cp_parser_parse_tentatively (parser);
22663 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22664 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22665 type = cp_parser_class_name (parser,
22666 /*typename_keyword_p=*/false,
22667 /*template_keyword_p=*/false,
22668 class_type,
22669 /*check_dependency_p=*/false,
22670 /*class_head_p=*/true,
22671 /*is_declaration=*/false);
22672 /* If that didn't work, ignore the nested-name-specifier. */
22673 if (!cp_parser_parse_definitely (parser))
22675 invalid_nested_name_p = true;
22676 type_start_token = cp_lexer_peek_token (parser->lexer);
22677 id = cp_parser_identifier (parser);
22678 if (id == error_mark_node)
22679 id = NULL_TREE;
22681 /* If we could not find a corresponding TYPE, treat this
22682 declaration like an unqualified declaration. */
22683 if (type == error_mark_node)
22684 nested_name_specifier = NULL_TREE;
22685 /* Otherwise, count the number of templates used in TYPE and its
22686 containing scopes. */
22687 else
22689 tree scope;
22691 for (scope = TREE_TYPE (type);
22692 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22693 scope = get_containing_scope (scope))
22694 if (TYPE_P (scope)
22695 && CLASS_TYPE_P (scope)
22696 && CLASSTYPE_TEMPLATE_INFO (scope)
22697 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22698 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22699 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22700 ++num_templates;
22703 /* Otherwise, the identifier is optional. */
22704 else
22706 /* We don't know whether what comes next is a template-id,
22707 an identifier, or nothing at all. */
22708 cp_parser_parse_tentatively (parser);
22709 /* Check for a template-id. */
22710 type_start_token = cp_lexer_peek_token (parser->lexer);
22711 id = cp_parser_template_id (parser,
22712 /*template_keyword_p=*/false,
22713 /*check_dependency_p=*/true,
22714 class_key,
22715 /*is_declaration=*/true);
22716 /* If that didn't work, it could still be an identifier. */
22717 if (!cp_parser_parse_definitely (parser))
22719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22721 type_start_token = cp_lexer_peek_token (parser->lexer);
22722 id = cp_parser_identifier (parser);
22724 else
22725 id = NULL_TREE;
22727 else
22729 template_id_p = true;
22730 ++num_templates;
22734 pop_deferring_access_checks ();
22736 if (id)
22738 cp_parser_check_for_invalid_template_id (parser, id,
22739 class_key,
22740 type_start_token->location);
22742 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22744 /* If it's not a `:' or a `{' then we can't really be looking at a
22745 class-head, since a class-head only appears as part of a
22746 class-specifier. We have to detect this situation before calling
22747 xref_tag, since that has irreversible side-effects. */
22748 if (!cp_parser_next_token_starts_class_definition_p (parser))
22750 cp_parser_error (parser, "expected %<{%> or %<:%>");
22751 type = error_mark_node;
22752 goto out;
22755 /* At this point, we're going ahead with the class-specifier, even
22756 if some other problem occurs. */
22757 cp_parser_commit_to_tentative_parse (parser);
22758 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22760 cp_parser_error (parser,
22761 "cannot specify %<override%> for a class");
22762 type = error_mark_node;
22763 goto out;
22765 /* Issue the error about the overly-qualified name now. */
22766 if (qualified_p)
22768 cp_parser_error (parser,
22769 "global qualification of class name is invalid");
22770 type = error_mark_node;
22771 goto out;
22773 else if (invalid_nested_name_p)
22775 cp_parser_error (parser,
22776 "qualified name does not name a class");
22777 type = error_mark_node;
22778 goto out;
22780 else if (nested_name_specifier)
22782 tree scope;
22784 if (bad_template_keyword)
22785 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22786 keyword template shall not appear at the top level. */
22787 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22788 "keyword %<template%> not allowed in class-head-name");
22790 /* Reject typedef-names in class heads. */
22791 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22793 error_at (type_start_token->location,
22794 "invalid class name in declaration of %qD",
22795 type);
22796 type = NULL_TREE;
22797 goto done;
22800 /* Figure out in what scope the declaration is being placed. */
22801 scope = current_scope ();
22802 /* If that scope does not contain the scope in which the
22803 class was originally declared, the program is invalid. */
22804 if (scope && !is_ancestor (scope, nested_name_specifier))
22806 if (at_namespace_scope_p ())
22807 error_at (type_start_token->location,
22808 "declaration of %qD in namespace %qD which does not "
22809 "enclose %qD",
22810 type, scope, nested_name_specifier);
22811 else
22812 error_at (type_start_token->location,
22813 "declaration of %qD in %qD which does not enclose %qD",
22814 type, scope, nested_name_specifier);
22815 type = NULL_TREE;
22816 goto done;
22818 /* [dcl.meaning]
22820 A declarator-id shall not be qualified except for the
22821 definition of a ... nested class outside of its class
22822 ... [or] the definition or explicit instantiation of a
22823 class member of a namespace outside of its namespace. */
22824 if (scope == nested_name_specifier)
22826 permerror (nested_name_specifier_token_start->location,
22827 "extra qualification not allowed");
22828 nested_name_specifier = NULL_TREE;
22829 num_templates = 0;
22832 /* An explicit-specialization must be preceded by "template <>". If
22833 it is not, try to recover gracefully. */
22834 if (at_namespace_scope_p ()
22835 && parser->num_template_parameter_lists == 0
22836 && !processing_template_parmlist
22837 && template_id_p)
22839 /* Build a location of this form:
22840 struct typename <ARGS>
22841 ^~~~~~~~~~~~~~~~~~~~~~
22842 with caret==start at the start token, and
22843 finishing at the end of the type. */
22844 location_t reported_loc
22845 = make_location (class_head_start_location,
22846 class_head_start_location,
22847 get_finish (type_start_token->location));
22848 rich_location richloc (line_table, reported_loc);
22849 richloc.add_fixit_insert_before (class_head_start_location,
22850 "template <> ");
22851 error_at_rich_loc
22852 (&richloc,
22853 "an explicit specialization must be preceded by %<template <>%>");
22854 invalid_explicit_specialization_p = true;
22855 /* Take the same action that would have been taken by
22856 cp_parser_explicit_specialization. */
22857 ++parser->num_template_parameter_lists;
22858 begin_specialization ();
22860 /* There must be no "return" statements between this point and the
22861 end of this function; set "type "to the correct return value and
22862 use "goto done;" to return. */
22863 /* Make sure that the right number of template parameters were
22864 present. */
22865 if (!cp_parser_check_template_parameters (parser, num_templates,
22866 type_start_token->location,
22867 /*declarator=*/NULL))
22869 /* If something went wrong, there is no point in even trying to
22870 process the class-definition. */
22871 type = NULL_TREE;
22872 goto done;
22875 /* Look up the type. */
22876 if (template_id_p)
22878 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22879 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22880 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22882 error_at (type_start_token->location,
22883 "function template %qD redeclared as a class template", id);
22884 type = error_mark_node;
22886 else
22888 type = TREE_TYPE (id);
22889 type = maybe_process_partial_specialization (type);
22891 /* Check the scope while we still know whether or not we had a
22892 nested-name-specifier. */
22893 if (type != error_mark_node)
22894 check_unqualified_spec_or_inst (type, type_start_token->location);
22896 if (nested_name_specifier)
22897 pushed_scope = push_scope (nested_name_specifier);
22899 else if (nested_name_specifier)
22901 tree class_type;
22903 /* Given:
22905 template <typename T> struct S { struct T };
22906 template <typename T> struct S<T>::T { };
22908 we will get a TYPENAME_TYPE when processing the definition of
22909 `S::T'. We need to resolve it to the actual type before we
22910 try to define it. */
22911 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22913 class_type = resolve_typename_type (TREE_TYPE (type),
22914 /*only_current_p=*/false);
22915 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22916 type = TYPE_NAME (class_type);
22917 else
22919 cp_parser_error (parser, "could not resolve typename type");
22920 type = error_mark_node;
22924 if (maybe_process_partial_specialization (TREE_TYPE (type))
22925 == error_mark_node)
22927 type = NULL_TREE;
22928 goto done;
22931 class_type = current_class_type;
22932 /* Enter the scope indicated by the nested-name-specifier. */
22933 pushed_scope = push_scope (nested_name_specifier);
22934 /* Get the canonical version of this type. */
22935 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22936 /* Call push_template_decl if it seems like we should be defining a
22937 template either from the template headers or the type we're
22938 defining, so that we diagnose both extra and missing headers. */
22939 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22940 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22941 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22943 type = push_template_decl (type);
22944 if (type == error_mark_node)
22946 type = NULL_TREE;
22947 goto done;
22951 type = TREE_TYPE (type);
22952 *nested_name_specifier_p = true;
22954 else /* The name is not a nested name. */
22956 /* If the class was unnamed, create a dummy name. */
22957 if (!id)
22958 id = make_anon_name ();
22959 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22960 ? ts_within_enclosing_non_class
22961 : ts_current);
22962 type = xref_tag (class_key, id, tag_scope,
22963 parser->num_template_parameter_lists);
22966 /* Indicate whether this class was declared as a `class' or as a
22967 `struct'. */
22968 if (TREE_CODE (type) == RECORD_TYPE)
22969 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22970 cp_parser_check_class_key (class_key, type);
22972 /* If this type was already complete, and we see another definition,
22973 that's an error. */
22974 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22976 error_at (type_start_token->location, "redefinition of %q#T",
22977 type);
22978 inform (location_of (type), "previous definition of %q#T",
22979 type);
22980 type = NULL_TREE;
22981 goto done;
22983 else if (type == error_mark_node)
22984 type = NULL_TREE;
22986 if (type)
22988 /* Apply attributes now, before any use of the class as a template
22989 argument in its base list. */
22990 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22991 fixup_attribute_variants (type);
22994 /* We will have entered the scope containing the class; the names of
22995 base classes should be looked up in that context. For example:
22997 struct A { struct B {}; struct C; };
22998 struct A::C : B {};
23000 is valid. */
23002 /* Get the list of base-classes, if there is one. */
23003 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23005 /* PR59482: enter the class scope so that base-specifiers are looked
23006 up correctly. */
23007 if (type)
23008 pushclass (type);
23009 bases = cp_parser_base_clause (parser);
23010 /* PR59482: get out of the previously pushed class scope so that the
23011 subsequent pops pop the right thing. */
23012 if (type)
23013 popclass ();
23015 else
23016 bases = NULL_TREE;
23018 /* If we're really defining a class, process the base classes.
23019 If they're invalid, fail. */
23020 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23021 xref_basetypes (type, bases);
23023 done:
23024 /* Leave the scope given by the nested-name-specifier. We will
23025 enter the class scope itself while processing the members. */
23026 if (pushed_scope)
23027 pop_scope (pushed_scope);
23029 if (invalid_explicit_specialization_p)
23031 end_specialization ();
23032 --parser->num_template_parameter_lists;
23035 if (type)
23036 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23037 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23038 CLASSTYPE_FINAL (type) = 1;
23039 out:
23040 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23041 return type;
23044 /* Parse a class-key.
23046 class-key:
23047 class
23048 struct
23049 union
23051 Returns the kind of class-key specified, or none_type to indicate
23052 error. */
23054 static enum tag_types
23055 cp_parser_class_key (cp_parser* parser)
23057 cp_token *token;
23058 enum tag_types tag_type;
23060 /* Look for the class-key. */
23061 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23062 if (!token)
23063 return none_type;
23065 /* Check to see if the TOKEN is a class-key. */
23066 tag_type = cp_parser_token_is_class_key (token);
23067 if (!tag_type)
23068 cp_parser_error (parser, "expected class-key");
23069 return tag_type;
23072 /* Parse a type-parameter-key.
23074 type-parameter-key:
23075 class
23076 typename
23079 static void
23080 cp_parser_type_parameter_key (cp_parser* parser)
23082 /* Look for the type-parameter-key. */
23083 enum tag_types tag_type = none_type;
23084 cp_token *token = cp_lexer_peek_token (parser->lexer);
23085 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23087 cp_lexer_consume_token (parser->lexer);
23088 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23089 /* typename is not allowed in a template template parameter
23090 by the standard until C++1Z. */
23091 pedwarn (token->location, OPT_Wpedantic,
23092 "ISO C++ forbids typename key in template template parameter;"
23093 " use -std=c++1z or -std=gnu++1z");
23095 else
23096 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23098 return;
23101 /* Parse an (optional) member-specification.
23103 member-specification:
23104 member-declaration member-specification [opt]
23105 access-specifier : member-specification [opt] */
23107 static void
23108 cp_parser_member_specification_opt (cp_parser* parser)
23110 while (true)
23112 cp_token *token;
23113 enum rid keyword;
23115 /* Peek at the next token. */
23116 token = cp_lexer_peek_token (parser->lexer);
23117 /* If it's a `}', or EOF then we've seen all the members. */
23118 if (token->type == CPP_CLOSE_BRACE
23119 || token->type == CPP_EOF
23120 || token->type == CPP_PRAGMA_EOL)
23121 break;
23123 /* See if this token is a keyword. */
23124 keyword = token->keyword;
23125 switch (keyword)
23127 case RID_PUBLIC:
23128 case RID_PROTECTED:
23129 case RID_PRIVATE:
23130 /* Consume the access-specifier. */
23131 cp_lexer_consume_token (parser->lexer);
23132 /* Remember which access-specifier is active. */
23133 current_access_specifier = token->u.value;
23134 /* Look for the `:'. */
23135 cp_parser_require (parser, CPP_COLON, RT_COLON);
23136 break;
23138 default:
23139 /* Accept #pragmas at class scope. */
23140 if (token->type == CPP_PRAGMA)
23142 cp_parser_pragma (parser, pragma_member, NULL);
23143 break;
23146 /* Otherwise, the next construction must be a
23147 member-declaration. */
23148 cp_parser_member_declaration (parser);
23153 /* Parse a member-declaration.
23155 member-declaration:
23156 decl-specifier-seq [opt] member-declarator-list [opt] ;
23157 function-definition ; [opt]
23158 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23159 using-declaration
23160 template-declaration
23161 alias-declaration
23163 member-declarator-list:
23164 member-declarator
23165 member-declarator-list , member-declarator
23167 member-declarator:
23168 declarator pure-specifier [opt]
23169 declarator constant-initializer [opt]
23170 identifier [opt] : constant-expression
23172 GNU Extensions:
23174 member-declaration:
23175 __extension__ member-declaration
23177 member-declarator:
23178 declarator attributes [opt] pure-specifier [opt]
23179 declarator attributes [opt] constant-initializer [opt]
23180 identifier [opt] attributes [opt] : constant-expression
23182 C++0x Extensions:
23184 member-declaration:
23185 static_assert-declaration */
23187 static void
23188 cp_parser_member_declaration (cp_parser* parser)
23190 cp_decl_specifier_seq decl_specifiers;
23191 tree prefix_attributes;
23192 tree decl;
23193 int declares_class_or_enum;
23194 bool friend_p;
23195 cp_token *token = NULL;
23196 cp_token *decl_spec_token_start = NULL;
23197 cp_token *initializer_token_start = NULL;
23198 int saved_pedantic;
23199 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23201 /* Check for the `__extension__' keyword. */
23202 if (cp_parser_extension_opt (parser, &saved_pedantic))
23204 /* Recurse. */
23205 cp_parser_member_declaration (parser);
23206 /* Restore the old value of the PEDANTIC flag. */
23207 pedantic = saved_pedantic;
23209 return;
23212 /* Check for a template-declaration. */
23213 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23215 /* An explicit specialization here is an error condition, and we
23216 expect the specialization handler to detect and report this. */
23217 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23218 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23219 cp_parser_explicit_specialization (parser);
23220 else
23221 cp_parser_template_declaration (parser, /*member_p=*/true);
23223 return;
23225 /* Check for a template introduction. */
23226 else if (cp_parser_template_declaration_after_export (parser, true))
23227 return;
23229 /* Check for a using-declaration. */
23230 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23232 if (cxx_dialect < cxx11)
23234 /* Parse the using-declaration. */
23235 cp_parser_using_declaration (parser,
23236 /*access_declaration_p=*/false);
23237 return;
23239 else
23241 tree decl;
23242 bool alias_decl_expected;
23243 cp_parser_parse_tentatively (parser);
23244 decl = cp_parser_alias_declaration (parser);
23245 /* Note that if we actually see the '=' token after the
23246 identifier, cp_parser_alias_declaration commits the
23247 tentative parse. In that case, we really expect an
23248 alias-declaration. Otherwise, we expect a using
23249 declaration. */
23250 alias_decl_expected =
23251 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23252 cp_parser_parse_definitely (parser);
23254 if (alias_decl_expected)
23255 finish_member_declaration (decl);
23256 else
23257 cp_parser_using_declaration (parser,
23258 /*access_declaration_p=*/false);
23259 return;
23263 /* Check for @defs. */
23264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23266 tree ivar, member;
23267 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23268 ivar = ivar_chains;
23269 while (ivar)
23271 member = ivar;
23272 ivar = TREE_CHAIN (member);
23273 TREE_CHAIN (member) = NULL_TREE;
23274 finish_member_declaration (member);
23276 return;
23279 /* If the next token is `static_assert' we have a static assertion. */
23280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23282 cp_parser_static_assert (parser, /*member_p=*/true);
23283 return;
23286 parser->colon_corrects_to_scope_p = false;
23288 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23289 goto out;
23291 /* Parse the decl-specifier-seq. */
23292 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23293 cp_parser_decl_specifier_seq (parser,
23294 CP_PARSER_FLAGS_OPTIONAL,
23295 &decl_specifiers,
23296 &declares_class_or_enum);
23297 /* Check for an invalid type-name. */
23298 if (!decl_specifiers.any_type_specifiers_p
23299 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23300 goto out;
23301 /* If there is no declarator, then the decl-specifier-seq should
23302 specify a type. */
23303 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23305 /* If there was no decl-specifier-seq, and the next token is a
23306 `;', then we have something like:
23308 struct S { ; };
23310 [class.mem]
23312 Each member-declaration shall declare at least one member
23313 name of the class. */
23314 if (!decl_specifiers.any_specifiers_p)
23316 cp_token *token = cp_lexer_peek_token (parser->lexer);
23317 if (!in_system_header_at (token->location))
23319 gcc_rich_location richloc (token->location);
23320 richloc.add_fixit_remove ();
23321 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23324 else
23326 tree type;
23328 /* See if this declaration is a friend. */
23329 friend_p = cp_parser_friend_p (&decl_specifiers);
23330 /* If there were decl-specifiers, check to see if there was
23331 a class-declaration. */
23332 type = check_tag_decl (&decl_specifiers,
23333 /*explicit_type_instantiation_p=*/false);
23334 /* Nested classes have already been added to the class, but
23335 a `friend' needs to be explicitly registered. */
23336 if (friend_p)
23338 /* If the `friend' keyword was present, the friend must
23339 be introduced with a class-key. */
23340 if (!declares_class_or_enum && cxx_dialect < cxx11)
23341 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23342 "in C++03 a class-key must be used "
23343 "when declaring a friend");
23344 /* In this case:
23346 template <typename T> struct A {
23347 friend struct A<T>::B;
23350 A<T>::B will be represented by a TYPENAME_TYPE, and
23351 therefore not recognized by check_tag_decl. */
23352 if (!type)
23354 type = decl_specifiers.type;
23355 if (type && TREE_CODE (type) == TYPE_DECL)
23356 type = TREE_TYPE (type);
23358 if (!type || !TYPE_P (type))
23359 error_at (decl_spec_token_start->location,
23360 "friend declaration does not name a class or "
23361 "function");
23362 else
23363 make_friend_class (current_class_type, type,
23364 /*complain=*/true);
23366 /* If there is no TYPE, an error message will already have
23367 been issued. */
23368 else if (!type || type == error_mark_node)
23370 /* An anonymous aggregate has to be handled specially; such
23371 a declaration really declares a data member (with a
23372 particular type), as opposed to a nested class. */
23373 else if (ANON_AGGR_TYPE_P (type))
23375 /* C++11 9.5/6. */
23376 if (decl_specifiers.storage_class != sc_none)
23377 error_at (decl_spec_token_start->location,
23378 "a storage class on an anonymous aggregate "
23379 "in class scope is not allowed");
23381 /* Remove constructors and such from TYPE, now that we
23382 know it is an anonymous aggregate. */
23383 fixup_anonymous_aggr (type);
23384 /* And make the corresponding data member. */
23385 decl = build_decl (decl_spec_token_start->location,
23386 FIELD_DECL, NULL_TREE, type);
23387 /* Add it to the class. */
23388 finish_member_declaration (decl);
23390 else
23391 cp_parser_check_access_in_redeclaration
23392 (TYPE_NAME (type),
23393 decl_spec_token_start->location);
23396 else
23398 bool assume_semicolon = false;
23400 /* Clear attributes from the decl_specifiers but keep them
23401 around as prefix attributes that apply them to the entity
23402 being declared. */
23403 prefix_attributes = decl_specifiers.attributes;
23404 decl_specifiers.attributes = NULL_TREE;
23406 /* See if these declarations will be friends. */
23407 friend_p = cp_parser_friend_p (&decl_specifiers);
23409 /* Keep going until we hit the `;' at the end of the
23410 declaration. */
23411 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23413 tree attributes = NULL_TREE;
23414 tree first_attribute;
23416 /* Peek at the next token. */
23417 token = cp_lexer_peek_token (parser->lexer);
23419 /* Check for a bitfield declaration. */
23420 if (token->type == CPP_COLON
23421 || (token->type == CPP_NAME
23422 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23423 == CPP_COLON))
23425 tree identifier;
23426 tree width;
23428 /* Get the name of the bitfield. Note that we cannot just
23429 check TOKEN here because it may have been invalidated by
23430 the call to cp_lexer_peek_nth_token above. */
23431 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23432 identifier = cp_parser_identifier (parser);
23433 else
23434 identifier = NULL_TREE;
23436 /* Consume the `:' token. */
23437 cp_lexer_consume_token (parser->lexer);
23438 /* Get the width of the bitfield. */
23439 width
23440 = cp_parser_constant_expression (parser);
23442 /* Look for attributes that apply to the bitfield. */
23443 attributes = cp_parser_attributes_opt (parser);
23444 /* Remember which attributes are prefix attributes and
23445 which are not. */
23446 first_attribute = attributes;
23447 /* Combine the attributes. */
23448 attributes = chainon (prefix_attributes, attributes);
23450 /* Create the bitfield declaration. */
23451 decl = grokbitfield (identifier
23452 ? make_id_declarator (NULL_TREE,
23453 identifier,
23454 sfk_none)
23455 : NULL,
23456 &decl_specifiers,
23457 width,
23458 attributes);
23460 else
23462 cp_declarator *declarator;
23463 tree initializer;
23464 tree asm_specification;
23465 int ctor_dtor_or_conv_p;
23467 /* Parse the declarator. */
23468 declarator
23469 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23470 &ctor_dtor_or_conv_p,
23471 /*parenthesized_p=*/NULL,
23472 /*member_p=*/true,
23473 friend_p);
23475 /* If something went wrong parsing the declarator, make sure
23476 that we at least consume some tokens. */
23477 if (declarator == cp_error_declarator)
23479 /* Skip to the end of the statement. */
23480 cp_parser_skip_to_end_of_statement (parser);
23481 /* If the next token is not a semicolon, that is
23482 probably because we just skipped over the body of
23483 a function. So, we consume a semicolon if
23484 present, but do not issue an error message if it
23485 is not present. */
23486 if (cp_lexer_next_token_is (parser->lexer,
23487 CPP_SEMICOLON))
23488 cp_lexer_consume_token (parser->lexer);
23489 goto out;
23492 if (declares_class_or_enum & 2)
23493 cp_parser_check_for_definition_in_return_type
23494 (declarator, decl_specifiers.type,
23495 decl_specifiers.locations[ds_type_spec]);
23497 /* Look for an asm-specification. */
23498 asm_specification = cp_parser_asm_specification_opt (parser);
23499 /* Look for attributes that apply to the declaration. */
23500 attributes = cp_parser_attributes_opt (parser);
23501 /* Remember which attributes are prefix attributes and
23502 which are not. */
23503 first_attribute = attributes;
23504 /* Combine the attributes. */
23505 attributes = chainon (prefix_attributes, attributes);
23507 /* If it's an `=', then we have a constant-initializer or a
23508 pure-specifier. It is not correct to parse the
23509 initializer before registering the member declaration
23510 since the member declaration should be in scope while
23511 its initializer is processed. However, the rest of the
23512 front end does not yet provide an interface that allows
23513 us to handle this correctly. */
23514 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23516 /* In [class.mem]:
23518 A pure-specifier shall be used only in the declaration of
23519 a virtual function.
23521 A member-declarator can contain a constant-initializer
23522 only if it declares a static member of integral or
23523 enumeration type.
23525 Therefore, if the DECLARATOR is for a function, we look
23526 for a pure-specifier; otherwise, we look for a
23527 constant-initializer. When we call `grokfield', it will
23528 perform more stringent semantics checks. */
23529 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23530 if (function_declarator_p (declarator)
23531 || (decl_specifiers.type
23532 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23533 && declarator->kind == cdk_id
23534 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23535 == FUNCTION_TYPE)))
23536 initializer = cp_parser_pure_specifier (parser);
23537 else if (decl_specifiers.storage_class != sc_static)
23538 initializer = cp_parser_save_nsdmi (parser);
23539 else if (cxx_dialect >= cxx11)
23541 bool nonconst;
23542 /* Don't require a constant rvalue in C++11, since we
23543 might want a reference constant. We'll enforce
23544 constancy later. */
23545 cp_lexer_consume_token (parser->lexer);
23546 /* Parse the initializer. */
23547 initializer = cp_parser_initializer_clause (parser,
23548 &nonconst);
23550 else
23551 /* Parse the initializer. */
23552 initializer = cp_parser_constant_initializer (parser);
23554 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23555 && !function_declarator_p (declarator))
23557 bool x;
23558 if (decl_specifiers.storage_class != sc_static)
23559 initializer = cp_parser_save_nsdmi (parser);
23560 else
23561 initializer = cp_parser_initializer (parser, &x, &x);
23563 /* Otherwise, there is no initializer. */
23564 else
23565 initializer = NULL_TREE;
23567 /* See if we are probably looking at a function
23568 definition. We are certainly not looking at a
23569 member-declarator. Calling `grokfield' has
23570 side-effects, so we must not do it unless we are sure
23571 that we are looking at a member-declarator. */
23572 if (cp_parser_token_starts_function_definition_p
23573 (cp_lexer_peek_token (parser->lexer)))
23575 /* The grammar does not allow a pure-specifier to be
23576 used when a member function is defined. (It is
23577 possible that this fact is an oversight in the
23578 standard, since a pure function may be defined
23579 outside of the class-specifier. */
23580 if (initializer && initializer_token_start)
23581 error_at (initializer_token_start->location,
23582 "pure-specifier on function-definition");
23583 decl = cp_parser_save_member_function_body (parser,
23584 &decl_specifiers,
23585 declarator,
23586 attributes);
23587 if (parser->fully_implicit_function_template_p)
23588 decl = finish_fully_implicit_template (parser, decl);
23589 /* If the member was not a friend, declare it here. */
23590 if (!friend_p)
23591 finish_member_declaration (decl);
23592 /* Peek at the next token. */
23593 token = cp_lexer_peek_token (parser->lexer);
23594 /* If the next token is a semicolon, consume it. */
23595 if (token->type == CPP_SEMICOLON)
23597 location_t semicolon_loc
23598 = cp_lexer_consume_token (parser->lexer)->location;
23599 gcc_rich_location richloc (semicolon_loc);
23600 richloc.add_fixit_remove ();
23601 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23602 "extra %<;%> after in-class "
23603 "function definition");
23605 goto out;
23607 else
23608 if (declarator->kind == cdk_function)
23609 declarator->id_loc = token->location;
23610 /* Create the declaration. */
23611 decl = grokfield (declarator, &decl_specifiers,
23612 initializer, /*init_const_expr_p=*/true,
23613 asm_specification, attributes);
23614 if (parser->fully_implicit_function_template_p)
23616 if (friend_p)
23617 finish_fully_implicit_template (parser, 0);
23618 else
23619 decl = finish_fully_implicit_template (parser, decl);
23623 cp_finalize_omp_declare_simd (parser, decl);
23624 cp_finalize_oacc_routine (parser, decl, false);
23626 /* Reset PREFIX_ATTRIBUTES. */
23627 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23628 attributes = TREE_CHAIN (attributes);
23629 if (attributes)
23630 TREE_CHAIN (attributes) = NULL_TREE;
23632 /* If there is any qualification still in effect, clear it
23633 now; we will be starting fresh with the next declarator. */
23634 parser->scope = NULL_TREE;
23635 parser->qualifying_scope = NULL_TREE;
23636 parser->object_scope = NULL_TREE;
23637 /* If it's a `,', then there are more declarators. */
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23640 cp_lexer_consume_token (parser->lexer);
23641 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23643 cp_token *token = cp_lexer_previous_token (parser->lexer);
23644 gcc_rich_location richloc (token->location);
23645 richloc.add_fixit_remove ();
23646 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23647 "member declaration");
23650 /* If the next token isn't a `;', then we have a parse error. */
23651 else if (cp_lexer_next_token_is_not (parser->lexer,
23652 CPP_SEMICOLON))
23654 /* The next token might be a ways away from where the
23655 actual semicolon is missing. Find the previous token
23656 and use that for our error position. */
23657 cp_token *token = cp_lexer_previous_token (parser->lexer);
23658 gcc_rich_location richloc (token->location);
23659 richloc.add_fixit_insert_after (";");
23660 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23661 "member declaration");
23663 /* Assume that the user meant to provide a semicolon. If
23664 we were to cp_parser_skip_to_end_of_statement, we might
23665 skip to a semicolon inside a member function definition
23666 and issue nonsensical error messages. */
23667 assume_semicolon = true;
23670 if (decl)
23672 /* Add DECL to the list of members. */
23673 if (!friend_p
23674 /* Explicitly include, eg, NSDMIs, for better error
23675 recovery (c++/58650). */
23676 || !DECL_DECLARES_FUNCTION_P (decl))
23677 finish_member_declaration (decl);
23679 if (TREE_CODE (decl) == FUNCTION_DECL)
23680 cp_parser_save_default_args (parser, decl);
23681 else if (TREE_CODE (decl) == FIELD_DECL
23682 && !DECL_C_BIT_FIELD (decl)
23683 && DECL_INITIAL (decl))
23684 /* Add DECL to the queue of NSDMI to be parsed later. */
23685 vec_safe_push (unparsed_nsdmis, decl);
23688 if (assume_semicolon)
23689 goto out;
23693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23694 out:
23695 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23698 /* Parse a pure-specifier.
23700 pure-specifier:
23703 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23704 Otherwise, ERROR_MARK_NODE is returned. */
23706 static tree
23707 cp_parser_pure_specifier (cp_parser* parser)
23709 cp_token *token;
23711 /* Look for the `=' token. */
23712 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23713 return error_mark_node;
23714 /* Look for the `0' token. */
23715 token = cp_lexer_peek_token (parser->lexer);
23717 if (token->type == CPP_EOF
23718 || token->type == CPP_PRAGMA_EOL)
23719 return error_mark_node;
23721 cp_lexer_consume_token (parser->lexer);
23723 /* Accept = default or = delete in c++0x mode. */
23724 if (token->keyword == RID_DEFAULT
23725 || token->keyword == RID_DELETE)
23727 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23728 return token->u.value;
23731 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23732 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23734 cp_parser_error (parser,
23735 "invalid pure specifier (only %<= 0%> is allowed)");
23736 cp_parser_skip_to_end_of_statement (parser);
23737 return error_mark_node;
23739 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23741 error_at (token->location, "templates may not be %<virtual%>");
23742 return error_mark_node;
23745 return integer_zero_node;
23748 /* Parse a constant-initializer.
23750 constant-initializer:
23751 = constant-expression
23753 Returns a representation of the constant-expression. */
23755 static tree
23756 cp_parser_constant_initializer (cp_parser* parser)
23758 /* Look for the `=' token. */
23759 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23760 return error_mark_node;
23762 /* It is invalid to write:
23764 struct S { static const int i = { 7 }; };
23767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23769 cp_parser_error (parser,
23770 "a brace-enclosed initializer is not allowed here");
23771 /* Consume the opening brace. */
23772 matching_braces braces;
23773 braces.consume_open (parser);
23774 /* Skip the initializer. */
23775 cp_parser_skip_to_closing_brace (parser);
23776 /* Look for the trailing `}'. */
23777 braces.require_close (parser);
23779 return error_mark_node;
23782 return cp_parser_constant_expression (parser);
23785 /* Derived classes [gram.class.derived] */
23787 /* Parse a base-clause.
23789 base-clause:
23790 : base-specifier-list
23792 base-specifier-list:
23793 base-specifier ... [opt]
23794 base-specifier-list , base-specifier ... [opt]
23796 Returns a TREE_LIST representing the base-classes, in the order in
23797 which they were declared. The representation of each node is as
23798 described by cp_parser_base_specifier.
23800 In the case that no bases are specified, this function will return
23801 NULL_TREE, not ERROR_MARK_NODE. */
23803 static tree
23804 cp_parser_base_clause (cp_parser* parser)
23806 tree bases = NULL_TREE;
23808 /* Look for the `:' that begins the list. */
23809 cp_parser_require (parser, CPP_COLON, RT_COLON);
23811 /* Scan the base-specifier-list. */
23812 while (true)
23814 cp_token *token;
23815 tree base;
23816 bool pack_expansion_p = false;
23818 /* Look for the base-specifier. */
23819 base = cp_parser_base_specifier (parser);
23820 /* Look for the (optional) ellipsis. */
23821 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23823 /* Consume the `...'. */
23824 cp_lexer_consume_token (parser->lexer);
23826 pack_expansion_p = true;
23829 /* Add BASE to the front of the list. */
23830 if (base && base != error_mark_node)
23832 if (pack_expansion_p)
23833 /* Make this a pack expansion type. */
23834 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23836 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23838 TREE_CHAIN (base) = bases;
23839 bases = base;
23842 /* Peek at the next token. */
23843 token = cp_lexer_peek_token (parser->lexer);
23844 /* If it's not a comma, then the list is complete. */
23845 if (token->type != CPP_COMMA)
23846 break;
23847 /* Consume the `,'. */
23848 cp_lexer_consume_token (parser->lexer);
23851 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23852 base class had a qualified name. However, the next name that
23853 appears is certainly not qualified. */
23854 parser->scope = NULL_TREE;
23855 parser->qualifying_scope = NULL_TREE;
23856 parser->object_scope = NULL_TREE;
23858 return nreverse (bases);
23861 /* Parse a base-specifier.
23863 base-specifier:
23864 :: [opt] nested-name-specifier [opt] class-name
23865 virtual access-specifier [opt] :: [opt] nested-name-specifier
23866 [opt] class-name
23867 access-specifier virtual [opt] :: [opt] nested-name-specifier
23868 [opt] class-name
23870 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23871 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23872 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23873 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23875 static tree
23876 cp_parser_base_specifier (cp_parser* parser)
23878 cp_token *token;
23879 bool done = false;
23880 bool virtual_p = false;
23881 bool duplicate_virtual_error_issued_p = false;
23882 bool duplicate_access_error_issued_p = false;
23883 bool class_scope_p, template_p;
23884 tree access = access_default_node;
23885 tree type;
23887 /* Process the optional `virtual' and `access-specifier'. */
23888 while (!done)
23890 /* Peek at the next token. */
23891 token = cp_lexer_peek_token (parser->lexer);
23892 /* Process `virtual'. */
23893 switch (token->keyword)
23895 case RID_VIRTUAL:
23896 /* If `virtual' appears more than once, issue an error. */
23897 if (virtual_p && !duplicate_virtual_error_issued_p)
23899 cp_parser_error (parser,
23900 "%<virtual%> specified more than once in base-specifier");
23901 duplicate_virtual_error_issued_p = true;
23904 virtual_p = true;
23906 /* Consume the `virtual' token. */
23907 cp_lexer_consume_token (parser->lexer);
23909 break;
23911 case RID_PUBLIC:
23912 case RID_PROTECTED:
23913 case RID_PRIVATE:
23914 /* If more than one access specifier appears, issue an
23915 error. */
23916 if (access != access_default_node
23917 && !duplicate_access_error_issued_p)
23919 cp_parser_error (parser,
23920 "more than one access specifier in base-specifier");
23921 duplicate_access_error_issued_p = true;
23924 access = ridpointers[(int) token->keyword];
23926 /* Consume the access-specifier. */
23927 cp_lexer_consume_token (parser->lexer);
23929 break;
23931 default:
23932 done = true;
23933 break;
23936 /* It is not uncommon to see programs mechanically, erroneously, use
23937 the 'typename' keyword to denote (dependent) qualified types
23938 as base classes. */
23939 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23941 token = cp_lexer_peek_token (parser->lexer);
23942 if (!processing_template_decl)
23943 error_at (token->location,
23944 "keyword %<typename%> not allowed outside of templates");
23945 else
23946 error_at (token->location,
23947 "keyword %<typename%> not allowed in this context "
23948 "(the base class is implicitly a type)");
23949 cp_lexer_consume_token (parser->lexer);
23952 /* Look for the optional `::' operator. */
23953 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23954 /* Look for the nested-name-specifier. The simplest way to
23955 implement:
23957 [temp.res]
23959 The keyword `typename' is not permitted in a base-specifier or
23960 mem-initializer; in these contexts a qualified name that
23961 depends on a template-parameter is implicitly assumed to be a
23962 type name.
23964 is to pretend that we have seen the `typename' keyword at this
23965 point. */
23966 cp_parser_nested_name_specifier_opt (parser,
23967 /*typename_keyword_p=*/true,
23968 /*check_dependency_p=*/true,
23969 /*type_p=*/true,
23970 /*is_declaration=*/true);
23971 /* If the base class is given by a qualified name, assume that names
23972 we see are type names or templates, as appropriate. */
23973 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23974 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23976 if (!parser->scope
23977 && cp_lexer_next_token_is_decltype (parser->lexer))
23978 /* DR 950 allows decltype as a base-specifier. */
23979 type = cp_parser_decltype (parser);
23980 else
23982 /* Otherwise, look for the class-name. */
23983 type = cp_parser_class_name (parser,
23984 class_scope_p,
23985 template_p,
23986 typename_type,
23987 /*check_dependency_p=*/true,
23988 /*class_head_p=*/false,
23989 /*is_declaration=*/true);
23990 type = TREE_TYPE (type);
23993 if (type == error_mark_node)
23994 return error_mark_node;
23996 return finish_base_specifier (type, access, virtual_p);
23999 /* Exception handling [gram.exception] */
24001 /* Parse an (optional) noexcept-specification.
24003 noexcept-specification:
24004 noexcept ( constant-expression ) [opt]
24006 If no noexcept-specification is present, returns NULL_TREE.
24007 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24008 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24009 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24010 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24011 in which case a boolean condition is returned instead. */
24013 static tree
24014 cp_parser_noexcept_specification_opt (cp_parser* parser,
24015 bool require_constexpr,
24016 bool* consumed_expr,
24017 bool return_cond)
24019 cp_token *token;
24020 const char *saved_message;
24022 /* Peek at the next token. */
24023 token = cp_lexer_peek_token (parser->lexer);
24025 /* Is it a noexcept-specification? */
24026 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24028 tree expr;
24029 cp_lexer_consume_token (parser->lexer);
24031 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24033 matching_parens parens;
24034 parens.consume_open (parser);
24036 if (require_constexpr)
24038 /* Types may not be defined in an exception-specification. */
24039 saved_message = parser->type_definition_forbidden_message;
24040 parser->type_definition_forbidden_message
24041 = G_("types may not be defined in an exception-specification");
24043 expr = cp_parser_constant_expression (parser);
24045 /* Restore the saved message. */
24046 parser->type_definition_forbidden_message = saved_message;
24048 else
24050 expr = cp_parser_expression (parser);
24051 *consumed_expr = true;
24054 parens.require_close (parser);
24056 else
24058 expr = boolean_true_node;
24059 if (!require_constexpr)
24060 *consumed_expr = false;
24063 /* We cannot build a noexcept-spec right away because this will check
24064 that expr is a constexpr. */
24065 if (!return_cond)
24066 return build_noexcept_spec (expr, tf_warning_or_error);
24067 else
24068 return expr;
24070 else
24071 return NULL_TREE;
24074 /* Parse an (optional) exception-specification.
24076 exception-specification:
24077 throw ( type-id-list [opt] )
24079 Returns a TREE_LIST representing the exception-specification. The
24080 TREE_VALUE of each node is a type. */
24082 static tree
24083 cp_parser_exception_specification_opt (cp_parser* parser)
24085 cp_token *token;
24086 tree type_id_list;
24087 const char *saved_message;
24089 /* Peek at the next token. */
24090 token = cp_lexer_peek_token (parser->lexer);
24092 /* Is it a noexcept-specification? */
24093 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24094 false);
24095 if (type_id_list != NULL_TREE)
24096 return type_id_list;
24098 /* If it's not `throw', then there's no exception-specification. */
24099 if (!cp_parser_is_keyword (token, RID_THROW))
24100 return NULL_TREE;
24102 location_t loc = token->location;
24104 /* Consume the `throw'. */
24105 cp_lexer_consume_token (parser->lexer);
24107 /* Look for the `('. */
24108 matching_parens parens;
24109 parens.require_open (parser);
24111 /* Peek at the next token. */
24112 token = cp_lexer_peek_token (parser->lexer);
24113 /* If it's not a `)', then there is a type-id-list. */
24114 if (token->type != CPP_CLOSE_PAREN)
24116 /* Types may not be defined in an exception-specification. */
24117 saved_message = parser->type_definition_forbidden_message;
24118 parser->type_definition_forbidden_message
24119 = G_("types may not be defined in an exception-specification");
24120 /* Parse the type-id-list. */
24121 type_id_list = cp_parser_type_id_list (parser);
24122 /* Restore the saved message. */
24123 parser->type_definition_forbidden_message = saved_message;
24125 if (cxx_dialect >= cxx1z)
24127 error_at (loc, "ISO C++1z does not allow dynamic exception "
24128 "specifications");
24129 type_id_list = NULL_TREE;
24131 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24132 warning_at (loc, OPT_Wdeprecated,
24133 "dynamic exception specifications are deprecated in "
24134 "C++11");
24136 /* In C++17, throw() is equivalent to noexcept (true). throw()
24137 is deprecated in C++11 and above as well, but is still widely used,
24138 so don't warn about it yet. */
24139 else if (cxx_dialect >= cxx1z)
24140 type_id_list = noexcept_true_spec;
24141 else
24142 type_id_list = empty_except_spec;
24144 /* Look for the `)'. */
24145 parens.require_close (parser);
24147 return type_id_list;
24150 /* Parse an (optional) type-id-list.
24152 type-id-list:
24153 type-id ... [opt]
24154 type-id-list , type-id ... [opt]
24156 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24157 in the order that the types were presented. */
24159 static tree
24160 cp_parser_type_id_list (cp_parser* parser)
24162 tree types = NULL_TREE;
24164 while (true)
24166 cp_token *token;
24167 tree type;
24169 token = cp_lexer_peek_token (parser->lexer);
24171 /* Get the next type-id. */
24172 type = cp_parser_type_id (parser);
24173 /* Check for invalid 'auto'. */
24174 if (flag_concepts && type_uses_auto (type))
24176 error_at (token->location,
24177 "invalid use of %<auto%> in exception-specification");
24178 type = error_mark_node;
24180 /* Parse the optional ellipsis. */
24181 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24183 /* Consume the `...'. */
24184 cp_lexer_consume_token (parser->lexer);
24186 /* Turn the type into a pack expansion expression. */
24187 type = make_pack_expansion (type);
24189 /* Add it to the list. */
24190 types = add_exception_specifier (types, type, /*complain=*/1);
24191 /* Peek at the next token. */
24192 token = cp_lexer_peek_token (parser->lexer);
24193 /* If it is not a `,', we are done. */
24194 if (token->type != CPP_COMMA)
24195 break;
24196 /* Consume the `,'. */
24197 cp_lexer_consume_token (parser->lexer);
24200 return nreverse (types);
24203 /* Parse a try-block.
24205 try-block:
24206 try compound-statement handler-seq */
24208 static tree
24209 cp_parser_try_block (cp_parser* parser)
24211 tree try_block;
24213 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24214 if (parser->in_function_body
24215 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24216 error ("%<try%> in %<constexpr%> function");
24218 try_block = begin_try_block ();
24219 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24220 finish_try_block (try_block);
24221 cp_parser_handler_seq (parser);
24222 finish_handler_sequence (try_block);
24224 return try_block;
24227 /* Parse a function-try-block.
24229 function-try-block:
24230 try ctor-initializer [opt] function-body handler-seq */
24232 static bool
24233 cp_parser_function_try_block (cp_parser* parser)
24235 tree compound_stmt;
24236 tree try_block;
24237 bool ctor_initializer_p;
24239 /* Look for the `try' keyword. */
24240 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24241 return false;
24242 /* Let the rest of the front end know where we are. */
24243 try_block = begin_function_try_block (&compound_stmt);
24244 /* Parse the function-body. */
24245 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24246 (parser, /*in_function_try_block=*/true);
24247 /* We're done with the `try' part. */
24248 finish_function_try_block (try_block);
24249 /* Parse the handlers. */
24250 cp_parser_handler_seq (parser);
24251 /* We're done with the handlers. */
24252 finish_function_handler_sequence (try_block, compound_stmt);
24254 return ctor_initializer_p;
24257 /* Parse a handler-seq.
24259 handler-seq:
24260 handler handler-seq [opt] */
24262 static void
24263 cp_parser_handler_seq (cp_parser* parser)
24265 while (true)
24267 cp_token *token;
24269 /* Parse the handler. */
24270 cp_parser_handler (parser);
24271 /* Peek at the next token. */
24272 token = cp_lexer_peek_token (parser->lexer);
24273 /* If it's not `catch' then there are no more handlers. */
24274 if (!cp_parser_is_keyword (token, RID_CATCH))
24275 break;
24279 /* Parse a handler.
24281 handler:
24282 catch ( exception-declaration ) compound-statement */
24284 static void
24285 cp_parser_handler (cp_parser* parser)
24287 tree handler;
24288 tree declaration;
24290 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24291 handler = begin_handler ();
24292 matching_parens parens;
24293 parens.require_open (parser);
24294 declaration = cp_parser_exception_declaration (parser);
24295 finish_handler_parms (declaration, handler);
24296 parens.require_close (parser);
24297 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24298 finish_handler (handler);
24301 /* Parse an exception-declaration.
24303 exception-declaration:
24304 type-specifier-seq declarator
24305 type-specifier-seq abstract-declarator
24306 type-specifier-seq
24309 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24310 ellipsis variant is used. */
24312 static tree
24313 cp_parser_exception_declaration (cp_parser* parser)
24315 cp_decl_specifier_seq type_specifiers;
24316 cp_declarator *declarator;
24317 const char *saved_message;
24319 /* If it's an ellipsis, it's easy to handle. */
24320 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24322 /* Consume the `...' token. */
24323 cp_lexer_consume_token (parser->lexer);
24324 return NULL_TREE;
24327 /* Types may not be defined in exception-declarations. */
24328 saved_message = parser->type_definition_forbidden_message;
24329 parser->type_definition_forbidden_message
24330 = G_("types may not be defined in exception-declarations");
24332 /* Parse the type-specifier-seq. */
24333 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24334 /*is_trailing_return=*/false,
24335 &type_specifiers);
24336 /* If it's a `)', then there is no declarator. */
24337 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24338 declarator = NULL;
24339 else
24340 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24341 /*ctor_dtor_or_conv_p=*/NULL,
24342 /*parenthesized_p=*/NULL,
24343 /*member_p=*/false,
24344 /*friend_p=*/false);
24346 /* Restore the saved message. */
24347 parser->type_definition_forbidden_message = saved_message;
24349 if (!type_specifiers.any_specifiers_p)
24350 return error_mark_node;
24352 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24355 /* Parse a throw-expression.
24357 throw-expression:
24358 throw assignment-expression [opt]
24360 Returns a THROW_EXPR representing the throw-expression. */
24362 static tree
24363 cp_parser_throw_expression (cp_parser* parser)
24365 tree expression;
24366 cp_token* token;
24368 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24369 token = cp_lexer_peek_token (parser->lexer);
24370 /* Figure out whether or not there is an assignment-expression
24371 following the "throw" keyword. */
24372 if (token->type == CPP_COMMA
24373 || token->type == CPP_SEMICOLON
24374 || token->type == CPP_CLOSE_PAREN
24375 || token->type == CPP_CLOSE_SQUARE
24376 || token->type == CPP_CLOSE_BRACE
24377 || token->type == CPP_COLON)
24378 expression = NULL_TREE;
24379 else
24380 expression = cp_parser_assignment_expression (parser);
24382 return build_throw (expression);
24385 /* GNU Extensions */
24387 /* Parse an (optional) asm-specification.
24389 asm-specification:
24390 asm ( string-literal )
24392 If the asm-specification is present, returns a STRING_CST
24393 corresponding to the string-literal. Otherwise, returns
24394 NULL_TREE. */
24396 static tree
24397 cp_parser_asm_specification_opt (cp_parser* parser)
24399 cp_token *token;
24400 tree asm_specification;
24402 /* Peek at the next token. */
24403 token = cp_lexer_peek_token (parser->lexer);
24404 /* If the next token isn't the `asm' keyword, then there's no
24405 asm-specification. */
24406 if (!cp_parser_is_keyword (token, RID_ASM))
24407 return NULL_TREE;
24409 /* Consume the `asm' token. */
24410 cp_lexer_consume_token (parser->lexer);
24411 /* Look for the `('. */
24412 matching_parens parens;
24413 parens.require_open (parser);
24415 /* Look for the string-literal. */
24416 asm_specification = cp_parser_string_literal (parser, false, false);
24418 /* Look for the `)'. */
24419 parens.require_close (parser);
24421 return asm_specification;
24424 /* Parse an asm-operand-list.
24426 asm-operand-list:
24427 asm-operand
24428 asm-operand-list , asm-operand
24430 asm-operand:
24431 string-literal ( expression )
24432 [ string-literal ] string-literal ( expression )
24434 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24435 each node is the expression. The TREE_PURPOSE is itself a
24436 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24437 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24438 is a STRING_CST for the string literal before the parenthesis. Returns
24439 ERROR_MARK_NODE if any of the operands are invalid. */
24441 static tree
24442 cp_parser_asm_operand_list (cp_parser* parser)
24444 tree asm_operands = NULL_TREE;
24445 bool invalid_operands = false;
24447 while (true)
24449 tree string_literal;
24450 tree expression;
24451 tree name;
24453 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24455 /* Consume the `[' token. */
24456 cp_lexer_consume_token (parser->lexer);
24457 /* Read the operand name. */
24458 name = cp_parser_identifier (parser);
24459 if (name != error_mark_node)
24460 name = build_string (IDENTIFIER_LENGTH (name),
24461 IDENTIFIER_POINTER (name));
24462 /* Look for the closing `]'. */
24463 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24465 else
24466 name = NULL_TREE;
24467 /* Look for the string-literal. */
24468 string_literal = cp_parser_string_literal (parser, false, false);
24470 /* Look for the `('. */
24471 matching_parens parens;
24472 parens.require_open (parser);
24473 /* Parse the expression. */
24474 expression = cp_parser_expression (parser);
24475 /* Look for the `)'. */
24476 parens.require_close (parser);
24478 if (name == error_mark_node
24479 || string_literal == error_mark_node
24480 || expression == error_mark_node)
24481 invalid_operands = true;
24483 /* Add this operand to the list. */
24484 asm_operands = tree_cons (build_tree_list (name, string_literal),
24485 expression,
24486 asm_operands);
24487 /* If the next token is not a `,', there are no more
24488 operands. */
24489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24490 break;
24491 /* Consume the `,'. */
24492 cp_lexer_consume_token (parser->lexer);
24495 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24498 /* Parse an asm-clobber-list.
24500 asm-clobber-list:
24501 string-literal
24502 asm-clobber-list , string-literal
24504 Returns a TREE_LIST, indicating the clobbers in the order that they
24505 appeared. The TREE_VALUE of each node is a STRING_CST. */
24507 static tree
24508 cp_parser_asm_clobber_list (cp_parser* parser)
24510 tree clobbers = NULL_TREE;
24512 while (true)
24514 tree string_literal;
24516 /* Look for the string literal. */
24517 string_literal = cp_parser_string_literal (parser, false, false);
24518 /* Add it to the list. */
24519 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24520 /* If the next token is not a `,', then the list is
24521 complete. */
24522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24523 break;
24524 /* Consume the `,' token. */
24525 cp_lexer_consume_token (parser->lexer);
24528 return clobbers;
24531 /* Parse an asm-label-list.
24533 asm-label-list:
24534 identifier
24535 asm-label-list , identifier
24537 Returns a TREE_LIST, indicating the labels in the order that they
24538 appeared. The TREE_VALUE of each node is a label. */
24540 static tree
24541 cp_parser_asm_label_list (cp_parser* parser)
24543 tree labels = NULL_TREE;
24545 while (true)
24547 tree identifier, label, name;
24549 /* Look for the identifier. */
24550 identifier = cp_parser_identifier (parser);
24551 if (!error_operand_p (identifier))
24553 label = lookup_label (identifier);
24554 if (TREE_CODE (label) == LABEL_DECL)
24556 TREE_USED (label) = 1;
24557 check_goto (label);
24558 name = build_string (IDENTIFIER_LENGTH (identifier),
24559 IDENTIFIER_POINTER (identifier));
24560 labels = tree_cons (name, label, labels);
24563 /* If the next token is not a `,', then the list is
24564 complete. */
24565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24566 break;
24567 /* Consume the `,' token. */
24568 cp_lexer_consume_token (parser->lexer);
24571 return nreverse (labels);
24574 /* Return TRUE iff the next tokens in the stream are possibly the
24575 beginning of a GNU extension attribute. */
24577 static bool
24578 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24580 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24583 /* Return TRUE iff the next tokens in the stream are possibly the
24584 beginning of a standard C++-11 attribute specifier. */
24586 static bool
24587 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24589 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24592 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24593 beginning of a standard C++-11 attribute specifier. */
24595 static bool
24596 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24598 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24600 return (cxx_dialect >= cxx11
24601 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24602 || (token->type == CPP_OPEN_SQUARE
24603 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24604 && token->type == CPP_OPEN_SQUARE)));
24607 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24608 beginning of a GNU extension attribute. */
24610 static bool
24611 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24613 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24615 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24618 /* Return true iff the next tokens can be the beginning of either a
24619 GNU attribute list, or a standard C++11 attribute sequence. */
24621 static bool
24622 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24624 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24625 || cp_next_tokens_can_be_std_attribute_p (parser));
24628 /* Return true iff the next Nth tokens can be the beginning of either
24629 a GNU attribute list, or a standard C++11 attribute sequence. */
24631 static bool
24632 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24634 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24635 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24638 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24639 of GNU attributes, or return NULL. */
24641 static tree
24642 cp_parser_attributes_opt (cp_parser *parser)
24644 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24645 return cp_parser_gnu_attributes_opt (parser);
24646 return cp_parser_std_attribute_spec_seq (parser);
24649 #define CILK_SIMD_FN_CLAUSE_MASK \
24650 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24651 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24652 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24653 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24654 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24656 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24657 vector [(<clauses>)] */
24659 static void
24660 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24662 bool first_p = parser->cilk_simd_fn_info == NULL;
24663 cp_token *token = v_token;
24664 if (first_p)
24666 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24667 parser->cilk_simd_fn_info->error_seen = false;
24668 parser->cilk_simd_fn_info->fndecl_seen = false;
24669 parser->cilk_simd_fn_info->tokens = vNULL;
24670 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24672 int paren_scope = 0;
24673 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24675 cp_lexer_consume_token (parser->lexer);
24676 v_token = cp_lexer_peek_token (parser->lexer);
24677 paren_scope++;
24679 while (paren_scope > 0)
24681 token = cp_lexer_peek_token (parser->lexer);
24682 if (token->type == CPP_OPEN_PAREN)
24683 paren_scope++;
24684 else if (token->type == CPP_CLOSE_PAREN)
24685 paren_scope--;
24686 /* Do not push the last ')' */
24687 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24688 cp_lexer_consume_token (parser->lexer);
24691 token->type = CPP_PRAGMA_EOL;
24692 parser->lexer->next_token = token;
24693 cp_lexer_consume_token (parser->lexer);
24695 struct cp_token_cache *cp
24696 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24697 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24700 /* Parse an (optional) series of attributes.
24702 attributes:
24703 attributes attribute
24705 attribute:
24706 __attribute__ (( attribute-list [opt] ))
24708 The return value is as for cp_parser_gnu_attribute_list. */
24710 static tree
24711 cp_parser_gnu_attributes_opt (cp_parser* parser)
24713 tree attributes = NULL_TREE;
24715 while (true)
24717 cp_token *token;
24718 tree attribute_list;
24719 bool ok = true;
24721 /* Peek at the next token. */
24722 token = cp_lexer_peek_token (parser->lexer);
24723 /* If it's not `__attribute__', then we're done. */
24724 if (token->keyword != RID_ATTRIBUTE)
24725 break;
24727 /* Consume the `__attribute__' keyword. */
24728 cp_lexer_consume_token (parser->lexer);
24729 /* Look for the two `(' tokens. */
24730 matching_parens outer_parens;
24731 outer_parens.require_open (parser);
24732 matching_parens inner_parens;
24733 inner_parens.require_open (parser);
24735 /* Peek at the next token. */
24736 token = cp_lexer_peek_token (parser->lexer);
24737 if (token->type != CPP_CLOSE_PAREN)
24738 /* Parse the attribute-list. */
24739 attribute_list = cp_parser_gnu_attribute_list (parser);
24740 else
24741 /* If the next token is a `)', then there is no attribute
24742 list. */
24743 attribute_list = NULL;
24745 /* Look for the two `)' tokens. */
24746 if (!inner_parens.require_close (parser))
24747 ok = false;
24748 if (!outer_parens.require_close (parser))
24749 ok = false;
24750 if (!ok)
24751 cp_parser_skip_to_end_of_statement (parser);
24753 /* Add these new attributes to the list. */
24754 attributes = chainon (attributes, attribute_list);
24757 return attributes;
24760 /* Parse a GNU attribute-list.
24762 attribute-list:
24763 attribute
24764 attribute-list , attribute
24766 attribute:
24767 identifier
24768 identifier ( identifier )
24769 identifier ( identifier , expression-list )
24770 identifier ( expression-list )
24772 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24773 to an attribute. The TREE_PURPOSE of each node is the identifier
24774 indicating which attribute is in use. The TREE_VALUE represents
24775 the arguments, if any. */
24777 static tree
24778 cp_parser_gnu_attribute_list (cp_parser* parser)
24780 tree attribute_list = NULL_TREE;
24781 bool save_translate_strings_p = parser->translate_strings_p;
24783 parser->translate_strings_p = false;
24784 while (true)
24786 cp_token *token;
24787 tree identifier;
24788 tree attribute;
24790 /* Look for the identifier. We also allow keywords here; for
24791 example `__attribute__ ((const))' is legal. */
24792 token = cp_lexer_peek_token (parser->lexer);
24793 if (token->type == CPP_NAME
24794 || token->type == CPP_KEYWORD)
24796 tree arguments = NULL_TREE;
24798 /* Consume the token, but save it since we need it for the
24799 SIMD enabled function parsing. */
24800 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24802 /* Save away the identifier that indicates which attribute
24803 this is. */
24804 identifier = (token->type == CPP_KEYWORD)
24805 /* For keywords, use the canonical spelling, not the
24806 parsed identifier. */
24807 ? ridpointers[(int) token->keyword]
24808 : id_token->u.value;
24810 identifier = canonicalize_attr_name (identifier);
24811 attribute = build_tree_list (identifier, NULL_TREE);
24813 /* Peek at the next token. */
24814 token = cp_lexer_peek_token (parser->lexer);
24815 /* If it's an `(', then parse the attribute arguments. */
24816 if (token->type == CPP_OPEN_PAREN)
24818 vec<tree, va_gc> *vec;
24819 int attr_flag = (attribute_takes_identifier_p (identifier)
24820 ? id_attr : normal_attr);
24821 if (is_cilkplus_vector_p (identifier))
24823 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24824 continue;
24826 else
24827 vec = cp_parser_parenthesized_expression_list
24828 (parser, attr_flag, /*cast_p=*/false,
24829 /*allow_expansion_p=*/false,
24830 /*non_constant_p=*/NULL);
24831 if (vec == NULL)
24832 arguments = error_mark_node;
24833 else
24835 arguments = build_tree_list_vec (vec);
24836 release_tree_vector (vec);
24838 /* Save the arguments away. */
24839 TREE_VALUE (attribute) = arguments;
24841 else if (is_cilkplus_vector_p (identifier))
24843 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24844 continue;
24847 if (arguments != error_mark_node)
24849 /* Add this attribute to the list. */
24850 TREE_CHAIN (attribute) = attribute_list;
24851 attribute_list = attribute;
24854 token = cp_lexer_peek_token (parser->lexer);
24856 /* Now, look for more attributes. If the next token isn't a
24857 `,', we're done. */
24858 if (token->type != CPP_COMMA)
24859 break;
24861 /* Consume the comma and keep going. */
24862 cp_lexer_consume_token (parser->lexer);
24864 parser->translate_strings_p = save_translate_strings_p;
24866 /* We built up the list in reverse order. */
24867 return nreverse (attribute_list);
24870 /* Parse a standard C++11 attribute.
24872 The returned representation is a TREE_LIST which TREE_PURPOSE is
24873 the scoped name of the attribute, and the TREE_VALUE is its
24874 arguments list.
24876 Note that the scoped name of the attribute is itself a TREE_LIST
24877 which TREE_PURPOSE is the namespace of the attribute, and
24878 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24879 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24880 and which TREE_PURPOSE is directly the attribute name.
24882 Clients of the attribute code should use get_attribute_namespace
24883 and get_attribute_name to get the actual namespace and name of
24884 attributes, regardless of their being GNU or C++11 attributes.
24886 attribute:
24887 attribute-token attribute-argument-clause [opt]
24889 attribute-token:
24890 identifier
24891 attribute-scoped-token
24893 attribute-scoped-token:
24894 attribute-namespace :: identifier
24896 attribute-namespace:
24897 identifier
24899 attribute-argument-clause:
24900 ( balanced-token-seq )
24902 balanced-token-seq:
24903 balanced-token [opt]
24904 balanced-token-seq balanced-token
24906 balanced-token:
24907 ( balanced-token-seq )
24908 [ balanced-token-seq ]
24909 { balanced-token-seq }. */
24911 static tree
24912 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24914 tree attribute, attr_id = NULL_TREE, arguments;
24915 cp_token *token;
24917 /* First, parse name of the attribute, a.k.a attribute-token. */
24919 token = cp_lexer_peek_token (parser->lexer);
24920 if (token->type == CPP_NAME)
24921 attr_id = token->u.value;
24922 else if (token->type == CPP_KEYWORD)
24923 attr_id = ridpointers[(int) token->keyword];
24924 else if (token->flags & NAMED_OP)
24925 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24927 if (attr_id == NULL_TREE)
24928 return NULL_TREE;
24930 cp_lexer_consume_token (parser->lexer);
24932 token = cp_lexer_peek_token (parser->lexer);
24933 if (token->type == CPP_SCOPE)
24935 /* We are seeing a scoped attribute token. */
24937 cp_lexer_consume_token (parser->lexer);
24938 if (attr_ns)
24939 error_at (token->location, "attribute using prefix used together "
24940 "with scoped attribute token");
24941 attr_ns = attr_id;
24943 token = cp_lexer_consume_token (parser->lexer);
24944 if (token->type == CPP_NAME)
24945 attr_id = token->u.value;
24946 else if (token->type == CPP_KEYWORD)
24947 attr_id = ridpointers[(int) token->keyword];
24948 else if (token->flags & NAMED_OP)
24949 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24950 else
24952 error_at (token->location,
24953 "expected an identifier for the attribute name");
24954 return error_mark_node;
24957 attr_id = canonicalize_attr_name (attr_id);
24958 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24959 NULL_TREE);
24960 token = cp_lexer_peek_token (parser->lexer);
24962 else if (attr_ns)
24963 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24964 NULL_TREE);
24965 else
24967 attr_id = canonicalize_attr_name (attr_id);
24968 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24969 NULL_TREE);
24970 /* C++11 noreturn attribute is equivalent to GNU's. */
24971 if (is_attribute_p ("noreturn", attr_id))
24972 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24973 /* C++14 deprecated attribute is equivalent to GNU's. */
24974 else if (is_attribute_p ("deprecated", attr_id))
24975 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24976 /* C++17 fallthrough attribute is equivalent to GNU's. */
24977 else if (is_attribute_p ("fallthrough", attr_id))
24978 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24979 /* Transactional Memory TS optimize_for_synchronized attribute is
24980 equivalent to GNU transaction_callable. */
24981 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24982 TREE_PURPOSE (attribute)
24983 = get_identifier ("transaction_callable");
24984 /* Transactional Memory attributes are GNU attributes. */
24985 else if (tm_attr_to_mask (attr_id))
24986 TREE_PURPOSE (attribute) = attr_id;
24989 /* Now parse the optional argument clause of the attribute. */
24991 if (token->type != CPP_OPEN_PAREN)
24992 return attribute;
24995 vec<tree, va_gc> *vec;
24996 int attr_flag = normal_attr;
24998 if (attr_ns == get_identifier ("gnu")
24999 && attribute_takes_identifier_p (attr_id))
25000 /* A GNU attribute that takes an identifier in parameter. */
25001 attr_flag = id_attr;
25003 vec = cp_parser_parenthesized_expression_list
25004 (parser, attr_flag, /*cast_p=*/false,
25005 /*allow_expansion_p=*/true,
25006 /*non_constant_p=*/NULL);
25007 if (vec == NULL)
25008 arguments = error_mark_node;
25009 else
25011 arguments = build_tree_list_vec (vec);
25012 release_tree_vector (vec);
25015 if (arguments == error_mark_node)
25016 attribute = error_mark_node;
25017 else
25018 TREE_VALUE (attribute) = arguments;
25021 return attribute;
25024 /* Check that the attribute ATTRIBUTE appears at most once in the
25025 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25026 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25027 isn't implemented yet in GCC. */
25029 static void
25030 cp_parser_check_std_attribute (tree attributes, tree attribute)
25032 if (attributes)
25034 tree name = get_attribute_name (attribute);
25035 if (is_attribute_p ("noreturn", name)
25036 && lookup_attribute ("noreturn", attributes))
25037 error ("attribute %<noreturn%> can appear at most once "
25038 "in an attribute-list");
25039 else if (is_attribute_p ("deprecated", name)
25040 && lookup_attribute ("deprecated", attributes))
25041 error ("attribute %<deprecated%> can appear at most once "
25042 "in an attribute-list");
25046 /* Parse a list of standard C++-11 attributes.
25048 attribute-list:
25049 attribute [opt]
25050 attribute-list , attribute[opt]
25051 attribute ...
25052 attribute-list , attribute ...
25055 static tree
25056 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25058 tree attributes = NULL_TREE, attribute = NULL_TREE;
25059 cp_token *token = NULL;
25061 while (true)
25063 attribute = cp_parser_std_attribute (parser, attr_ns);
25064 if (attribute == error_mark_node)
25065 break;
25066 if (attribute != NULL_TREE)
25068 cp_parser_check_std_attribute (attributes, attribute);
25069 TREE_CHAIN (attribute) = attributes;
25070 attributes = attribute;
25072 token = cp_lexer_peek_token (parser->lexer);
25073 if (token->type == CPP_ELLIPSIS)
25075 cp_lexer_consume_token (parser->lexer);
25076 if (attribute == NULL_TREE)
25077 error_at (token->location,
25078 "expected attribute before %<...%>");
25079 else
25081 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25082 if (pack == error_mark_node)
25083 return error_mark_node;
25084 TREE_VALUE (attribute) = pack;
25086 token = cp_lexer_peek_token (parser->lexer);
25088 if (token->type != CPP_COMMA)
25089 break;
25090 cp_lexer_consume_token (parser->lexer);
25092 attributes = nreverse (attributes);
25093 return attributes;
25096 /* Parse a standard C++-11 attribute specifier.
25098 attribute-specifier:
25099 [ [ attribute-using-prefix [opt] attribute-list ] ]
25100 alignment-specifier
25102 attribute-using-prefix:
25103 using attribute-namespace :
25105 alignment-specifier:
25106 alignas ( type-id ... [opt] )
25107 alignas ( alignment-expression ... [opt] ). */
25109 static tree
25110 cp_parser_std_attribute_spec (cp_parser *parser)
25112 tree attributes = NULL_TREE;
25113 cp_token *token = cp_lexer_peek_token (parser->lexer);
25115 if (token->type == CPP_OPEN_SQUARE
25116 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25118 tree attr_ns = NULL_TREE;
25120 cp_lexer_consume_token (parser->lexer);
25121 cp_lexer_consume_token (parser->lexer);
25123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25125 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25126 if (token->type == CPP_NAME)
25127 attr_ns = token->u.value;
25128 else if (token->type == CPP_KEYWORD)
25129 attr_ns = ridpointers[(int) token->keyword];
25130 else if (token->flags & NAMED_OP)
25131 attr_ns = get_identifier (cpp_type2name (token->type,
25132 token->flags));
25133 if (attr_ns
25134 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25136 if (cxx_dialect < cxx1z
25137 && !in_system_header_at (input_location))
25138 pedwarn (input_location, 0,
25139 "attribute using prefix only available "
25140 "with -std=c++1z or -std=gnu++1z");
25142 cp_lexer_consume_token (parser->lexer);
25143 cp_lexer_consume_token (parser->lexer);
25144 cp_lexer_consume_token (parser->lexer);
25146 else
25147 attr_ns = NULL_TREE;
25150 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25152 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25153 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25154 cp_parser_skip_to_end_of_statement (parser);
25155 else
25156 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25157 when we are sure that we have actually parsed them. */
25158 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25160 else
25162 tree alignas_expr;
25164 /* Look for an alignment-specifier. */
25166 token = cp_lexer_peek_token (parser->lexer);
25168 if (token->type != CPP_KEYWORD
25169 || token->keyword != RID_ALIGNAS)
25170 return NULL_TREE;
25172 cp_lexer_consume_token (parser->lexer);
25173 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25175 matching_parens parens;
25176 if (!parens.require_open (parser))
25178 cp_parser_error (parser, "expected %<(%>");
25179 return error_mark_node;
25182 cp_parser_parse_tentatively (parser);
25183 alignas_expr = cp_parser_type_id (parser);
25185 if (!cp_parser_parse_definitely (parser))
25187 alignas_expr = cp_parser_assignment_expression (parser);
25188 if (alignas_expr == error_mark_node)
25189 cp_parser_skip_to_end_of_statement (parser);
25190 if (alignas_expr == NULL_TREE
25191 || alignas_expr == error_mark_node)
25192 return alignas_expr;
25195 alignas_expr = cxx_alignas_expr (alignas_expr);
25196 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25198 /* Handle alignas (pack...). */
25199 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25201 cp_lexer_consume_token (parser->lexer);
25202 alignas_expr = make_pack_expansion (alignas_expr);
25205 /* Something went wrong, so don't build the attribute. */
25206 if (alignas_expr == error_mark_node)
25207 return error_mark_node;
25209 if (!parens.require_close (parser))
25211 cp_parser_error (parser, "expected %<)%>");
25212 return error_mark_node;
25215 /* Build the C++-11 representation of an 'aligned'
25216 attribute. */
25217 attributes =
25218 build_tree_list (build_tree_list (get_identifier ("gnu"),
25219 get_identifier ("aligned")),
25220 alignas_expr);
25223 return attributes;
25226 /* Parse a standard C++-11 attribute-specifier-seq.
25228 attribute-specifier-seq:
25229 attribute-specifier-seq [opt] attribute-specifier
25232 static tree
25233 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25235 tree attr_specs = NULL_TREE;
25236 tree attr_last = NULL_TREE;
25238 while (true)
25240 tree attr_spec = cp_parser_std_attribute_spec (parser);
25241 if (attr_spec == NULL_TREE)
25242 break;
25243 if (attr_spec == error_mark_node)
25244 return error_mark_node;
25246 if (attr_last)
25247 TREE_CHAIN (attr_last) = attr_spec;
25248 else
25249 attr_specs = attr_last = attr_spec;
25250 attr_last = tree_last (attr_last);
25253 return attr_specs;
25256 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25257 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25258 current value of the PEDANTIC flag, regardless of whether or not
25259 the `__extension__' keyword is present. The caller is responsible
25260 for restoring the value of the PEDANTIC flag. */
25262 static bool
25263 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25265 /* Save the old value of the PEDANTIC flag. */
25266 *saved_pedantic = pedantic;
25268 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25270 /* Consume the `__extension__' token. */
25271 cp_lexer_consume_token (parser->lexer);
25272 /* We're not being pedantic while the `__extension__' keyword is
25273 in effect. */
25274 pedantic = 0;
25276 return true;
25279 return false;
25282 /* Parse a label declaration.
25284 label-declaration:
25285 __label__ label-declarator-seq ;
25287 label-declarator-seq:
25288 identifier , label-declarator-seq
25289 identifier */
25291 static void
25292 cp_parser_label_declaration (cp_parser* parser)
25294 /* Look for the `__label__' keyword. */
25295 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25297 while (true)
25299 tree identifier;
25301 /* Look for an identifier. */
25302 identifier = cp_parser_identifier (parser);
25303 /* If we failed, stop. */
25304 if (identifier == error_mark_node)
25305 break;
25306 /* Declare it as a label. */
25307 finish_label_decl (identifier);
25308 /* If the next token is a `;', stop. */
25309 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25310 break;
25311 /* Look for the `,' separating the label declarations. */
25312 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25315 /* Look for the final `;'. */
25316 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25319 // -------------------------------------------------------------------------- //
25320 // Requires Clause
25322 // Parse a requires clause.
25324 // requires-clause:
25325 // 'requires' logical-or-expression
25327 // The required logical-or-expression must be a constant expression. Note
25328 // that we don't check that the expression is constepxr here. We defer until
25329 // we analyze constraints and then, we only check atomic constraints.
25330 static tree
25331 cp_parser_requires_clause (cp_parser *parser)
25333 // Parse the requires clause so that it is not automatically folded.
25334 ++processing_template_decl;
25335 tree expr = cp_parser_binary_expression (parser, false, false,
25336 PREC_NOT_OPERATOR, NULL);
25337 if (check_for_bare_parameter_packs (expr))
25338 expr = error_mark_node;
25339 --processing_template_decl;
25340 return expr;
25343 // Optionally parse a requires clause:
25344 static tree
25345 cp_parser_requires_clause_opt (cp_parser *parser)
25347 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25348 if (tok->keyword != RID_REQUIRES)
25350 if (!flag_concepts && tok->type == CPP_NAME
25351 && tok->u.value == ridpointers[RID_REQUIRES])
25353 error_at (cp_lexer_peek_token (parser->lexer)->location,
25354 "%<requires%> only available with -fconcepts");
25355 /* Parse and discard the requires-clause. */
25356 cp_lexer_consume_token (parser->lexer);
25357 cp_parser_requires_clause (parser);
25359 return NULL_TREE;
25361 cp_lexer_consume_token (parser->lexer);
25362 return cp_parser_requires_clause (parser);
25366 /*---------------------------------------------------------------------------
25367 Requires expressions
25368 ---------------------------------------------------------------------------*/
25370 /* Parse a requires expression
25372 requirement-expression:
25373 'requires' requirement-parameter-list [opt] requirement-body */
25374 static tree
25375 cp_parser_requires_expression (cp_parser *parser)
25377 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25378 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25380 /* A requires-expression shall appear only within a concept
25381 definition or a requires-clause.
25383 TODO: Implement this diagnostic correctly. */
25384 if (!processing_template_decl)
25386 error_at (loc, "a requires expression cannot appear outside a template");
25387 cp_parser_skip_to_end_of_statement (parser);
25388 return error_mark_node;
25391 tree parms, reqs;
25393 /* Local parameters are delared as variables within the scope
25394 of the expression. They are not visible past the end of
25395 the expression. Expressions within the requires-expression
25396 are unevaluated. */
25397 struct scope_sentinel
25399 scope_sentinel ()
25401 ++cp_unevaluated_operand;
25402 begin_scope (sk_block, NULL_TREE);
25405 ~scope_sentinel ()
25407 pop_bindings_and_leave_scope ();
25408 --cp_unevaluated_operand;
25410 } s;
25412 /* Parse the optional parameter list. */
25413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25415 parms = cp_parser_requirement_parameter_list (parser);
25416 if (parms == error_mark_node)
25417 return error_mark_node;
25419 else
25420 parms = NULL_TREE;
25422 /* Parse the requirement body. */
25423 reqs = cp_parser_requirement_body (parser);
25424 if (reqs == error_mark_node)
25425 return error_mark_node;
25428 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25429 the parm chain. */
25430 grokparms (parms, &parms);
25431 return finish_requires_expr (parms, reqs);
25434 /* Parse a parameterized requirement.
25436 requirement-parameter-list:
25437 '(' parameter-declaration-clause ')' */
25438 static tree
25439 cp_parser_requirement_parameter_list (cp_parser *parser)
25441 matching_parens parens;
25442 if (!parens.require_open (parser))
25443 return error_mark_node;
25445 tree parms = cp_parser_parameter_declaration_clause (parser);
25447 if (!parens.require_close (parser))
25448 return error_mark_node;
25450 return parms;
25453 /* Parse the body of a requirement.
25455 requirement-body:
25456 '{' requirement-list '}' */
25457 static tree
25458 cp_parser_requirement_body (cp_parser *parser)
25460 matching_braces braces;
25461 if (!braces.require_open (parser))
25462 return error_mark_node;
25464 tree reqs = cp_parser_requirement_list (parser);
25466 if (!braces.require_close (parser))
25467 return error_mark_node;
25469 return reqs;
25472 /* Parse a list of requirements.
25474 requirement-list:
25475 requirement
25476 requirement-list ';' requirement[opt] */
25477 static tree
25478 cp_parser_requirement_list (cp_parser *parser)
25480 tree result = NULL_TREE;
25481 while (true)
25483 tree req = cp_parser_requirement (parser);
25484 if (req == error_mark_node)
25485 return error_mark_node;
25487 result = tree_cons (NULL_TREE, req, result);
25489 /* If we see a semi-colon, consume it. */
25490 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25491 cp_lexer_consume_token (parser->lexer);
25493 /* Stop processing at the end of the list. */
25494 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25495 break;
25498 /* Reverse the order of requirements so they are analyzed in
25499 declaration order. */
25500 return nreverse (result);
25503 /* Parse a syntactic requirement or type requirement.
25505 requirement:
25506 simple-requirement
25507 compound-requirement
25508 type-requirement
25509 nested-requirement */
25510 static tree
25511 cp_parser_requirement (cp_parser *parser)
25513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25514 return cp_parser_compound_requirement (parser);
25515 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25516 return cp_parser_type_requirement (parser);
25517 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25518 return cp_parser_nested_requirement (parser);
25519 else
25520 return cp_parser_simple_requirement (parser);
25523 /* Parse a simple requirement.
25525 simple-requirement:
25526 expression ';' */
25527 static tree
25528 cp_parser_simple_requirement (cp_parser *parser)
25530 tree expr = cp_parser_expression (parser, NULL, false, false);
25531 if (!expr || expr == error_mark_node)
25532 return error_mark_node;
25534 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25535 return error_mark_node;
25537 return finish_simple_requirement (expr);
25540 /* Parse a type requirement
25542 type-requirement
25543 nested-name-specifier [opt] required-type-name ';'
25545 required-type-name:
25546 type-name
25547 'template' [opt] simple-template-id */
25548 static tree
25549 cp_parser_type_requirement (cp_parser *parser)
25551 cp_lexer_consume_token (parser->lexer);
25553 // Save the scope before parsing name specifiers.
25554 tree saved_scope = parser->scope;
25555 tree saved_object_scope = parser->object_scope;
25556 tree saved_qualifying_scope = parser->qualifying_scope;
25557 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25558 cp_parser_nested_name_specifier_opt (parser,
25559 /*typename_keyword_p=*/true,
25560 /*check_dependency_p=*/false,
25561 /*type_p=*/true,
25562 /*is_declaration=*/false);
25564 tree type;
25565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25567 cp_lexer_consume_token (parser->lexer);
25568 type = cp_parser_template_id (parser,
25569 /*template_keyword_p=*/true,
25570 /*check_dependency=*/false,
25571 /*tag_type=*/none_type,
25572 /*is_declaration=*/false);
25573 type = make_typename_type (parser->scope, type, typename_type,
25574 /*complain=*/tf_error);
25576 else
25577 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25579 if (TREE_CODE (type) == TYPE_DECL)
25580 type = TREE_TYPE (type);
25582 parser->scope = saved_scope;
25583 parser->object_scope = saved_object_scope;
25584 parser->qualifying_scope = saved_qualifying_scope;
25586 if (type == error_mark_node)
25587 cp_parser_skip_to_end_of_statement (parser);
25589 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25590 return error_mark_node;
25591 if (type == error_mark_node)
25592 return error_mark_node;
25594 return finish_type_requirement (type);
25597 /* Parse a compound requirement
25599 compound-requirement:
25600 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25601 static tree
25602 cp_parser_compound_requirement (cp_parser *parser)
25604 /* Parse an expression enclosed in '{ }'s. */
25605 matching_braces braces;
25606 if (!braces.require_open (parser))
25607 return error_mark_node;
25609 tree expr = cp_parser_expression (parser, NULL, false, false);
25610 if (!expr || expr == error_mark_node)
25611 return error_mark_node;
25613 if (!braces.require_close (parser))
25614 return error_mark_node;
25616 /* Parse the optional noexcept. */
25617 bool noexcept_p = false;
25618 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25620 cp_lexer_consume_token (parser->lexer);
25621 noexcept_p = true;
25624 /* Parse the optional trailing return type. */
25625 tree type = NULL_TREE;
25626 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25628 cp_lexer_consume_token (parser->lexer);
25629 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25630 parser->in_result_type_constraint_p = true;
25631 type = cp_parser_trailing_type_id (parser);
25632 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25633 if (type == error_mark_node)
25634 return error_mark_node;
25637 return finish_compound_requirement (expr, type, noexcept_p);
25640 /* Parse a nested requirement. This is the same as a requires clause.
25642 nested-requirement:
25643 requires-clause */
25644 static tree
25645 cp_parser_nested_requirement (cp_parser *parser)
25647 cp_lexer_consume_token (parser->lexer);
25648 tree req = cp_parser_requires_clause (parser);
25649 if (req == error_mark_node)
25650 return error_mark_node;
25651 return finish_nested_requirement (req);
25654 /* Support Functions */
25656 /* Return the appropriate prefer_type argument for lookup_name_real based on
25657 tag_type and template_mem_access. */
25659 static inline int
25660 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25662 /* DR 141: When looking in the current enclosing context for a template-name
25663 after -> or ., only consider class templates. */
25664 if (template_mem_access)
25665 return 2;
25666 switch (tag_type)
25668 case none_type: return 0; // No preference.
25669 case scope_type: return 1; // Type or namespace.
25670 default: return 2; // Type only.
25674 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25675 NAME should have one of the representations used for an
25676 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25677 is returned. If PARSER->SCOPE is a dependent type, then a
25678 SCOPE_REF is returned.
25680 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25681 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25682 was formed. Abstractly, such entities should not be passed to this
25683 function, because they do not need to be looked up, but it is
25684 simpler to check for this special case here, rather than at the
25685 call-sites.
25687 In cases not explicitly covered above, this function returns a
25688 DECL, OVERLOAD, or baselink representing the result of the lookup.
25689 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25690 is returned.
25692 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25693 (e.g., "struct") that was used. In that case bindings that do not
25694 refer to types are ignored.
25696 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25697 ignored.
25699 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25700 are ignored.
25702 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25703 types.
25705 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25706 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25707 NULL_TREE otherwise. */
25709 static cp_expr
25710 cp_parser_lookup_name (cp_parser *parser, tree name,
25711 enum tag_types tag_type,
25712 bool is_template,
25713 bool is_namespace,
25714 bool check_dependency,
25715 tree *ambiguous_decls,
25716 location_t name_location)
25718 tree decl;
25719 tree object_type = parser->context->object_type;
25721 /* Assume that the lookup will be unambiguous. */
25722 if (ambiguous_decls)
25723 *ambiguous_decls = NULL_TREE;
25725 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25726 no longer valid. Note that if we are parsing tentatively, and
25727 the parse fails, OBJECT_TYPE will be automatically restored. */
25728 parser->context->object_type = NULL_TREE;
25730 if (name == error_mark_node)
25731 return error_mark_node;
25733 /* A template-id has already been resolved; there is no lookup to
25734 do. */
25735 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25736 return name;
25737 if (BASELINK_P (name))
25739 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25740 == TEMPLATE_ID_EXPR);
25741 return name;
25744 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25745 it should already have been checked to make sure that the name
25746 used matches the type being destroyed. */
25747 if (TREE_CODE (name) == BIT_NOT_EXPR)
25749 tree type;
25751 /* Figure out to which type this destructor applies. */
25752 if (parser->scope)
25753 type = parser->scope;
25754 else if (object_type)
25755 type = object_type;
25756 else
25757 type = current_class_type;
25758 /* If that's not a class type, there is no destructor. */
25759 if (!type || !CLASS_TYPE_P (type))
25760 return error_mark_node;
25762 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25763 lazily_declare_fn (sfk_destructor, type);
25765 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25766 return dtor;
25768 return error_mark_node;
25771 /* By this point, the NAME should be an ordinary identifier. If
25772 the id-expression was a qualified name, the qualifying scope is
25773 stored in PARSER->SCOPE at this point. */
25774 gcc_assert (identifier_p (name));
25776 /* Perform the lookup. */
25777 if (parser->scope)
25779 bool dependent_p;
25781 if (parser->scope == error_mark_node)
25782 return error_mark_node;
25784 /* If the SCOPE is dependent, the lookup must be deferred until
25785 the template is instantiated -- unless we are explicitly
25786 looking up names in uninstantiated templates. Even then, we
25787 cannot look up the name if the scope is not a class type; it
25788 might, for example, be a template type parameter. */
25789 dependent_p = (TYPE_P (parser->scope)
25790 && dependent_scope_p (parser->scope));
25791 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25792 && dependent_p)
25793 /* Defer lookup. */
25794 decl = error_mark_node;
25795 else
25797 tree pushed_scope = NULL_TREE;
25799 /* If PARSER->SCOPE is a dependent type, then it must be a
25800 class type, and we must not be checking dependencies;
25801 otherwise, we would have processed this lookup above. So
25802 that PARSER->SCOPE is not considered a dependent base by
25803 lookup_member, we must enter the scope here. */
25804 if (dependent_p)
25805 pushed_scope = push_scope (parser->scope);
25807 /* If the PARSER->SCOPE is a template specialization, it
25808 may be instantiated during name lookup. In that case,
25809 errors may be issued. Even if we rollback the current
25810 tentative parse, those errors are valid. */
25811 decl = lookup_qualified_name (parser->scope, name,
25812 prefer_type_arg (tag_type),
25813 /*complain=*/true);
25815 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25816 lookup result and the nested-name-specifier nominates a class C:
25817 * if the name specified after the nested-name-specifier, when
25818 looked up in C, is the injected-class-name of C (Clause 9), or
25819 * if the name specified after the nested-name-specifier is the
25820 same as the identifier or the simple-template-id's template-
25821 name in the last component of the nested-name-specifier,
25822 the name is instead considered to name the constructor of
25823 class C. [ Note: for example, the constructor is not an
25824 acceptable lookup result in an elaborated-type-specifier so
25825 the constructor would not be used in place of the
25826 injected-class-name. --end note ] Such a constructor name
25827 shall be used only in the declarator-id of a declaration that
25828 names a constructor or in a using-declaration. */
25829 if (tag_type == none_type
25830 && DECL_SELF_REFERENCE_P (decl)
25831 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25832 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25833 prefer_type_arg (tag_type),
25834 /*complain=*/true);
25836 /* If we have a single function from a using decl, pull it out. */
25837 if (TREE_CODE (decl) == OVERLOAD
25838 && !really_overloaded_fn (decl))
25839 decl = OVL_FUNCTION (decl);
25841 if (pushed_scope)
25842 pop_scope (pushed_scope);
25845 /* If the scope is a dependent type and either we deferred lookup or
25846 we did lookup but didn't find the name, rememeber the name. */
25847 if (decl == error_mark_node && TYPE_P (parser->scope)
25848 && dependent_type_p (parser->scope))
25850 if (tag_type)
25852 tree type;
25854 /* The resolution to Core Issue 180 says that `struct
25855 A::B' should be considered a type-name, even if `A'
25856 is dependent. */
25857 type = make_typename_type (parser->scope, name, tag_type,
25858 /*complain=*/tf_error);
25859 if (type != error_mark_node)
25860 decl = TYPE_NAME (type);
25862 else if (is_template
25863 && (cp_parser_next_token_ends_template_argument_p (parser)
25864 || cp_lexer_next_token_is (parser->lexer,
25865 CPP_CLOSE_PAREN)))
25866 decl = make_unbound_class_template (parser->scope,
25867 name, NULL_TREE,
25868 /*complain=*/tf_error);
25869 else
25870 decl = build_qualified_name (/*type=*/NULL_TREE,
25871 parser->scope, name,
25872 is_template);
25874 parser->qualifying_scope = parser->scope;
25875 parser->object_scope = NULL_TREE;
25877 else if (object_type)
25879 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25880 OBJECT_TYPE is not a class. */
25881 if (CLASS_TYPE_P (object_type))
25882 /* If the OBJECT_TYPE is a template specialization, it may
25883 be instantiated during name lookup. In that case, errors
25884 may be issued. Even if we rollback the current tentative
25885 parse, those errors are valid. */
25886 decl = lookup_member (object_type,
25887 name,
25888 /*protect=*/0,
25889 prefer_type_arg (tag_type),
25890 tf_warning_or_error);
25891 else
25892 decl = NULL_TREE;
25894 if (!decl)
25895 /* Look it up in the enclosing context. DR 141: When looking for a
25896 template-name after -> or ., only consider class templates. */
25897 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25898 /*nonclass=*/0,
25899 /*block_p=*/true, is_namespace, 0);
25900 if (object_type == unknown_type_node)
25901 /* The object is type-dependent, so we can't look anything up; we used
25902 this to get the DR 141 behavior. */
25903 object_type = NULL_TREE;
25904 parser->object_scope = object_type;
25905 parser->qualifying_scope = NULL_TREE;
25907 else
25909 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25910 /*nonclass=*/0,
25911 /*block_p=*/true, is_namespace, 0);
25912 parser->qualifying_scope = NULL_TREE;
25913 parser->object_scope = NULL_TREE;
25916 /* If the lookup failed, let our caller know. */
25917 if (!decl || decl == error_mark_node)
25918 return error_mark_node;
25920 /* Pull out the template from an injected-class-name (or multiple). */
25921 if (is_template)
25922 decl = maybe_get_template_decl_from_type_decl (decl);
25924 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25925 if (TREE_CODE (decl) == TREE_LIST)
25927 if (ambiguous_decls)
25928 *ambiguous_decls = decl;
25929 /* The error message we have to print is too complicated for
25930 cp_parser_error, so we incorporate its actions directly. */
25931 if (!cp_parser_simulate_error (parser))
25933 error_at (name_location, "reference to %qD is ambiguous",
25934 name);
25935 print_candidates (decl);
25937 return error_mark_node;
25940 gcc_assert (DECL_P (decl)
25941 || TREE_CODE (decl) == OVERLOAD
25942 || TREE_CODE (decl) == SCOPE_REF
25943 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25944 || BASELINK_P (decl));
25946 /* If we have resolved the name of a member declaration, check to
25947 see if the declaration is accessible. When the name resolves to
25948 set of overloaded functions, accessibility is checked when
25949 overload resolution is done.
25951 During an explicit instantiation, access is not checked at all,
25952 as per [temp.explicit]. */
25953 if (DECL_P (decl))
25954 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25956 maybe_record_typedef_use (decl);
25958 return cp_expr (decl, name_location);
25961 /* Like cp_parser_lookup_name, but for use in the typical case where
25962 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25963 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25965 static tree
25966 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25968 return cp_parser_lookup_name (parser, name,
25969 none_type,
25970 /*is_template=*/false,
25971 /*is_namespace=*/false,
25972 /*check_dependency=*/true,
25973 /*ambiguous_decls=*/NULL,
25974 location);
25977 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25978 the current context, return the TYPE_DECL. If TAG_NAME_P is
25979 true, the DECL indicates the class being defined in a class-head,
25980 or declared in an elaborated-type-specifier.
25982 Otherwise, return DECL. */
25984 static tree
25985 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25987 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25988 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25990 struct A {
25991 template <typename T> struct B;
25994 template <typename T> struct A::B {};
25996 Similarly, in an elaborated-type-specifier:
25998 namespace N { struct X{}; }
26000 struct A {
26001 template <typename T> friend struct N::X;
26004 However, if the DECL refers to a class type, and we are in
26005 the scope of the class, then the name lookup automatically
26006 finds the TYPE_DECL created by build_self_reference rather
26007 than a TEMPLATE_DECL. For example, in:
26009 template <class T> struct S {
26010 S s;
26013 there is no need to handle such case. */
26015 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26016 return DECL_TEMPLATE_RESULT (decl);
26018 return decl;
26021 /* If too many, or too few, template-parameter lists apply to the
26022 declarator, issue an error message. Returns TRUE if all went well,
26023 and FALSE otherwise. */
26025 static bool
26026 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26027 cp_declarator *declarator,
26028 location_t declarator_location)
26030 switch (declarator->kind)
26032 case cdk_id:
26034 unsigned num_templates = 0;
26035 tree scope = declarator->u.id.qualifying_scope;
26037 if (scope)
26038 num_templates = num_template_headers_for_class (scope);
26039 else if (TREE_CODE (declarator->u.id.unqualified_name)
26040 == TEMPLATE_ID_EXPR)
26041 /* If the DECLARATOR has the form `X<y>' then it uses one
26042 additional level of template parameters. */
26043 ++num_templates;
26045 return cp_parser_check_template_parameters
26046 (parser, num_templates, declarator_location, declarator);
26049 case cdk_function:
26050 case cdk_array:
26051 case cdk_pointer:
26052 case cdk_reference:
26053 case cdk_ptrmem:
26054 return (cp_parser_check_declarator_template_parameters
26055 (parser, declarator->declarator, declarator_location));
26057 case cdk_decomp:
26058 case cdk_error:
26059 return true;
26061 default:
26062 gcc_unreachable ();
26064 return false;
26067 /* NUM_TEMPLATES were used in the current declaration. If that is
26068 invalid, return FALSE and issue an error messages. Otherwise,
26069 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26070 declarator and we can print more accurate diagnostics. */
26072 static bool
26073 cp_parser_check_template_parameters (cp_parser* parser,
26074 unsigned num_templates,
26075 location_t location,
26076 cp_declarator *declarator)
26078 /* If there are the same number of template classes and parameter
26079 lists, that's OK. */
26080 if (parser->num_template_parameter_lists == num_templates)
26081 return true;
26082 /* If there are more, but only one more, then we are referring to a
26083 member template. That's OK too. */
26084 if (parser->num_template_parameter_lists == num_templates + 1)
26085 return true;
26086 /* If there are more template classes than parameter lists, we have
26087 something like:
26089 template <class T> void S<T>::R<T>::f (); */
26090 if (parser->num_template_parameter_lists < num_templates)
26092 if (declarator && !current_function_decl)
26093 error_at (location, "specializing member %<%T::%E%> "
26094 "requires %<template<>%> syntax",
26095 declarator->u.id.qualifying_scope,
26096 declarator->u.id.unqualified_name);
26097 else if (declarator)
26098 error_at (location, "invalid declaration of %<%T::%E%>",
26099 declarator->u.id.qualifying_scope,
26100 declarator->u.id.unqualified_name);
26101 else
26102 error_at (location, "too few template-parameter-lists");
26103 return false;
26105 /* Otherwise, there are too many template parameter lists. We have
26106 something like:
26108 template <class T> template <class U> void S::f(); */
26109 error_at (location, "too many template-parameter-lists");
26110 return false;
26113 /* Parse an optional `::' token indicating that the following name is
26114 from the global namespace. If so, PARSER->SCOPE is set to the
26115 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26116 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26117 Returns the new value of PARSER->SCOPE, if the `::' token is
26118 present, and NULL_TREE otherwise. */
26120 static tree
26121 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26123 cp_token *token;
26125 /* Peek at the next token. */
26126 token = cp_lexer_peek_token (parser->lexer);
26127 /* If we're looking at a `::' token then we're starting from the
26128 global namespace, not our current location. */
26129 if (token->type == CPP_SCOPE)
26131 /* Consume the `::' token. */
26132 cp_lexer_consume_token (parser->lexer);
26133 /* Set the SCOPE so that we know where to start the lookup. */
26134 parser->scope = global_namespace;
26135 parser->qualifying_scope = global_namespace;
26136 parser->object_scope = NULL_TREE;
26138 return parser->scope;
26140 else if (!current_scope_valid_p)
26142 parser->scope = NULL_TREE;
26143 parser->qualifying_scope = NULL_TREE;
26144 parser->object_scope = NULL_TREE;
26147 return NULL_TREE;
26150 /* Returns TRUE if the upcoming token sequence is the start of a
26151 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26152 declarator is preceded by the `friend' specifier. */
26154 static bool
26155 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26157 bool constructor_p;
26158 bool outside_class_specifier_p;
26159 tree nested_name_specifier;
26160 cp_token *next_token;
26162 /* The common case is that this is not a constructor declarator, so
26163 try to avoid doing lots of work if at all possible. It's not
26164 valid declare a constructor at function scope. */
26165 if (parser->in_function_body)
26166 return false;
26167 /* And only certain tokens can begin a constructor declarator. */
26168 next_token = cp_lexer_peek_token (parser->lexer);
26169 if (next_token->type != CPP_NAME
26170 && next_token->type != CPP_SCOPE
26171 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26172 && next_token->type != CPP_TEMPLATE_ID)
26173 return false;
26175 /* Parse tentatively; we are going to roll back all of the tokens
26176 consumed here. */
26177 cp_parser_parse_tentatively (parser);
26178 /* Assume that we are looking at a constructor declarator. */
26179 constructor_p = true;
26181 /* Look for the optional `::' operator. */
26182 cp_parser_global_scope_opt (parser,
26183 /*current_scope_valid_p=*/false);
26184 /* Look for the nested-name-specifier. */
26185 nested_name_specifier
26186 = (cp_parser_nested_name_specifier_opt (parser,
26187 /*typename_keyword_p=*/false,
26188 /*check_dependency_p=*/false,
26189 /*type_p=*/false,
26190 /*is_declaration=*/false));
26192 outside_class_specifier_p = (!at_class_scope_p ()
26193 || !TYPE_BEING_DEFINED (current_class_type)
26194 || friend_p);
26196 /* Outside of a class-specifier, there must be a
26197 nested-name-specifier. Except in C++17 mode, where we
26198 might be declaring a guiding declaration. */
26199 if (!nested_name_specifier && outside_class_specifier_p
26200 && cxx_dialect < cxx1z)
26201 constructor_p = false;
26202 else if (nested_name_specifier == error_mark_node)
26203 constructor_p = false;
26205 /* If we have a class scope, this is easy; DR 147 says that S::S always
26206 names the constructor, and no other qualified name could. */
26207 if (constructor_p && nested_name_specifier
26208 && CLASS_TYPE_P (nested_name_specifier))
26210 tree id = cp_parser_unqualified_id (parser,
26211 /*template_keyword_p=*/false,
26212 /*check_dependency_p=*/false,
26213 /*declarator_p=*/true,
26214 /*optional_p=*/false);
26215 if (is_overloaded_fn (id))
26216 id = DECL_NAME (get_first_fn (id));
26217 if (!constructor_name_p (id, nested_name_specifier))
26218 constructor_p = false;
26220 /* If we still think that this might be a constructor-declarator,
26221 look for a class-name. */
26222 else if (constructor_p)
26224 /* If we have:
26226 template <typename T> struct S {
26227 S();
26230 we must recognize that the nested `S' names a class. */
26231 if (cxx_dialect >= cxx1z)
26232 cp_parser_parse_tentatively (parser);
26234 tree type_decl;
26235 type_decl = cp_parser_class_name (parser,
26236 /*typename_keyword_p=*/false,
26237 /*template_keyword_p=*/false,
26238 none_type,
26239 /*check_dependency_p=*/false,
26240 /*class_head_p=*/false,
26241 /*is_declaration=*/false);
26243 if (cxx_dialect >= cxx1z
26244 && !cp_parser_parse_definitely (parser))
26246 type_decl = NULL_TREE;
26247 tree tmpl = cp_parser_template_name (parser,
26248 /*template_keyword*/false,
26249 /*check_dependency_p*/false,
26250 /*is_declaration*/false,
26251 none_type,
26252 /*is_identifier*/NULL);
26253 if (DECL_CLASS_TEMPLATE_P (tmpl)
26254 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26255 /* It's a deduction guide, return true. */;
26256 else
26257 cp_parser_simulate_error (parser);
26260 /* If there was no class-name, then this is not a constructor.
26261 Otherwise, if we are in a class-specifier and we aren't
26262 handling a friend declaration, check that its type matches
26263 current_class_type (c++/38313). Note: error_mark_node
26264 is left alone for error recovery purposes. */
26265 constructor_p = (!cp_parser_error_occurred (parser)
26266 && (outside_class_specifier_p
26267 || type_decl == NULL_TREE
26268 || type_decl == error_mark_node
26269 || same_type_p (current_class_type,
26270 TREE_TYPE (type_decl))));
26272 /* If we're still considering a constructor, we have to see a `(',
26273 to begin the parameter-declaration-clause, followed by either a
26274 `)', an `...', or a decl-specifier. We need to check for a
26275 type-specifier to avoid being fooled into thinking that:
26277 S (f) (int);
26279 is a constructor. (It is actually a function named `f' that
26280 takes one parameter (of type `int') and returns a value of type
26281 `S'. */
26282 if (constructor_p
26283 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26284 constructor_p = false;
26286 if (constructor_p
26287 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26288 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26289 /* A parameter declaration begins with a decl-specifier,
26290 which is either the "attribute" keyword, a storage class
26291 specifier, or (usually) a type-specifier. */
26292 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26294 tree type;
26295 tree pushed_scope = NULL_TREE;
26296 unsigned saved_num_template_parameter_lists;
26298 /* Names appearing in the type-specifier should be looked up
26299 in the scope of the class. */
26300 if (current_class_type)
26301 type = NULL_TREE;
26302 else if (type_decl)
26304 type = TREE_TYPE (type_decl);
26305 if (TREE_CODE (type) == TYPENAME_TYPE)
26307 type = resolve_typename_type (type,
26308 /*only_current_p=*/false);
26309 if (TREE_CODE (type) == TYPENAME_TYPE)
26311 cp_parser_abort_tentative_parse (parser);
26312 return false;
26315 pushed_scope = push_scope (type);
26318 /* Inside the constructor parameter list, surrounding
26319 template-parameter-lists do not apply. */
26320 saved_num_template_parameter_lists
26321 = parser->num_template_parameter_lists;
26322 parser->num_template_parameter_lists = 0;
26324 /* Look for the type-specifier. */
26325 cp_parser_type_specifier (parser,
26326 CP_PARSER_FLAGS_NONE,
26327 /*decl_specs=*/NULL,
26328 /*is_declarator=*/true,
26329 /*declares_class_or_enum=*/NULL,
26330 /*is_cv_qualifier=*/NULL);
26332 parser->num_template_parameter_lists
26333 = saved_num_template_parameter_lists;
26335 /* Leave the scope of the class. */
26336 if (pushed_scope)
26337 pop_scope (pushed_scope);
26339 constructor_p = !cp_parser_error_occurred (parser);
26343 /* We did not really want to consume any tokens. */
26344 cp_parser_abort_tentative_parse (parser);
26346 return constructor_p;
26349 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26350 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26351 they must be performed once we are in the scope of the function.
26353 Returns the function defined. */
26355 static tree
26356 cp_parser_function_definition_from_specifiers_and_declarator
26357 (cp_parser* parser,
26358 cp_decl_specifier_seq *decl_specifiers,
26359 tree attributes,
26360 const cp_declarator *declarator)
26362 tree fn;
26363 bool success_p;
26365 /* Begin the function-definition. */
26366 success_p = start_function (decl_specifiers, declarator, attributes);
26368 /* The things we're about to see are not directly qualified by any
26369 template headers we've seen thus far. */
26370 reset_specialization ();
26372 /* If there were names looked up in the decl-specifier-seq that we
26373 did not check, check them now. We must wait until we are in the
26374 scope of the function to perform the checks, since the function
26375 might be a friend. */
26376 perform_deferred_access_checks (tf_warning_or_error);
26378 if (success_p)
26380 cp_finalize_omp_declare_simd (parser, current_function_decl);
26381 parser->omp_declare_simd = NULL;
26382 cp_finalize_oacc_routine (parser, current_function_decl, true);
26383 parser->oacc_routine = NULL;
26386 if (!success_p)
26388 /* Skip the entire function. */
26389 cp_parser_skip_to_end_of_block_or_statement (parser);
26390 fn = error_mark_node;
26392 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26394 /* Seen already, skip it. An error message has already been output. */
26395 cp_parser_skip_to_end_of_block_or_statement (parser);
26396 fn = current_function_decl;
26397 current_function_decl = NULL_TREE;
26398 /* If this is a function from a class, pop the nested class. */
26399 if (current_class_name)
26400 pop_nested_class ();
26402 else
26404 timevar_id_t tv;
26405 if (DECL_DECLARED_INLINE_P (current_function_decl))
26406 tv = TV_PARSE_INLINE;
26407 else
26408 tv = TV_PARSE_FUNC;
26409 timevar_push (tv);
26410 fn = cp_parser_function_definition_after_declarator (parser,
26411 /*inline_p=*/false);
26412 timevar_pop (tv);
26415 return fn;
26418 /* Parse the part of a function-definition that follows the
26419 declarator. INLINE_P is TRUE iff this function is an inline
26420 function defined within a class-specifier.
26422 Returns the function defined. */
26424 static tree
26425 cp_parser_function_definition_after_declarator (cp_parser* parser,
26426 bool inline_p)
26428 tree fn;
26429 bool ctor_initializer_p = false;
26430 bool saved_in_unbraced_linkage_specification_p;
26431 bool saved_in_function_body;
26432 unsigned saved_num_template_parameter_lists;
26433 cp_token *token;
26434 bool fully_implicit_function_template_p
26435 = parser->fully_implicit_function_template_p;
26436 parser->fully_implicit_function_template_p = false;
26437 tree implicit_template_parms
26438 = parser->implicit_template_parms;
26439 parser->implicit_template_parms = 0;
26440 cp_binding_level* implicit_template_scope
26441 = parser->implicit_template_scope;
26442 parser->implicit_template_scope = 0;
26444 saved_in_function_body = parser->in_function_body;
26445 parser->in_function_body = true;
26446 /* If the next token is `return', then the code may be trying to
26447 make use of the "named return value" extension that G++ used to
26448 support. */
26449 token = cp_lexer_peek_token (parser->lexer);
26450 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26452 /* Consume the `return' keyword. */
26453 cp_lexer_consume_token (parser->lexer);
26454 /* Look for the identifier that indicates what value is to be
26455 returned. */
26456 cp_parser_identifier (parser);
26457 /* Issue an error message. */
26458 error_at (token->location,
26459 "named return values are no longer supported");
26460 /* Skip tokens until we reach the start of the function body. */
26461 while (true)
26463 cp_token *token = cp_lexer_peek_token (parser->lexer);
26464 if (token->type == CPP_OPEN_BRACE
26465 || token->type == CPP_EOF
26466 || token->type == CPP_PRAGMA_EOL)
26467 break;
26468 cp_lexer_consume_token (parser->lexer);
26471 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26472 anything declared inside `f'. */
26473 saved_in_unbraced_linkage_specification_p
26474 = parser->in_unbraced_linkage_specification_p;
26475 parser->in_unbraced_linkage_specification_p = false;
26476 /* Inside the function, surrounding template-parameter-lists do not
26477 apply. */
26478 saved_num_template_parameter_lists
26479 = parser->num_template_parameter_lists;
26480 parser->num_template_parameter_lists = 0;
26482 /* If the next token is `try', `__transaction_atomic', or
26483 `__transaction_relaxed`, then we are looking at either function-try-block
26484 or function-transaction-block. Note that all of these include the
26485 function-body. */
26486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26487 ctor_initializer_p = cp_parser_function_transaction (parser,
26488 RID_TRANSACTION_ATOMIC);
26489 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26490 RID_TRANSACTION_RELAXED))
26491 ctor_initializer_p = cp_parser_function_transaction (parser,
26492 RID_TRANSACTION_RELAXED);
26493 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26494 ctor_initializer_p = cp_parser_function_try_block (parser);
26495 else
26496 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26497 (parser, /*in_function_try_block=*/false);
26499 /* Finish the function. */
26500 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26501 (inline_p ? 2 : 0));
26502 /* Generate code for it, if necessary. */
26503 expand_or_defer_fn (fn);
26504 /* Restore the saved values. */
26505 parser->in_unbraced_linkage_specification_p
26506 = saved_in_unbraced_linkage_specification_p;
26507 parser->num_template_parameter_lists
26508 = saved_num_template_parameter_lists;
26509 parser->in_function_body = saved_in_function_body;
26511 parser->fully_implicit_function_template_p
26512 = fully_implicit_function_template_p;
26513 parser->implicit_template_parms
26514 = implicit_template_parms;
26515 parser->implicit_template_scope
26516 = implicit_template_scope;
26518 if (parser->fully_implicit_function_template_p)
26519 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26521 return fn;
26524 /* Parse a template-declaration body (following argument list). */
26526 static void
26527 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26528 tree parameter_list,
26529 bool member_p)
26531 tree decl = NULL_TREE;
26532 bool friend_p = false;
26534 /* We just processed one more parameter list. */
26535 ++parser->num_template_parameter_lists;
26537 /* Get the deferred access checks from the parameter list. These
26538 will be checked once we know what is being declared, as for a
26539 member template the checks must be performed in the scope of the
26540 class containing the member. */
26541 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26543 /* Tentatively parse for a new template parameter list, which can either be
26544 the template keyword or a template introduction. */
26545 if (cp_parser_template_declaration_after_export (parser, member_p))
26546 /* OK */;
26547 else if (cxx_dialect >= cxx11
26548 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26549 decl = cp_parser_alias_declaration (parser);
26550 else
26552 /* There are no access checks when parsing a template, as we do not
26553 know if a specialization will be a friend. */
26554 push_deferring_access_checks (dk_no_check);
26555 cp_token *token = cp_lexer_peek_token (parser->lexer);
26556 decl = cp_parser_single_declaration (parser,
26557 checks,
26558 member_p,
26559 /*explicit_specialization_p=*/false,
26560 &friend_p);
26561 pop_deferring_access_checks ();
26563 /* If this is a member template declaration, let the front
26564 end know. */
26565 if (member_p && !friend_p && decl)
26567 if (TREE_CODE (decl) == TYPE_DECL)
26568 cp_parser_check_access_in_redeclaration (decl, token->location);
26570 decl = finish_member_template_decl (decl);
26572 else if (friend_p && decl
26573 && DECL_DECLARES_TYPE_P (decl))
26574 make_friend_class (current_class_type, TREE_TYPE (decl),
26575 /*complain=*/true);
26577 /* We are done with the current parameter list. */
26578 --parser->num_template_parameter_lists;
26580 pop_deferring_access_checks ();
26582 /* Finish up. */
26583 finish_template_decl (parameter_list);
26585 /* Check the template arguments for a literal operator template. */
26586 if (decl
26587 && DECL_DECLARES_FUNCTION_P (decl)
26588 && UDLIT_OPER_P (DECL_NAME (decl)))
26590 bool ok = true;
26591 if (parameter_list == NULL_TREE)
26592 ok = false;
26593 else
26595 int num_parms = TREE_VEC_LENGTH (parameter_list);
26596 if (num_parms == 1)
26598 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26599 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26600 if (TREE_TYPE (parm) != char_type_node
26601 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26602 ok = false;
26604 else if (num_parms == 2 && cxx_dialect >= cxx14)
26606 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26607 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26608 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26609 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26610 if (parm == error_mark_node
26611 || TREE_TYPE (parm) != TREE_TYPE (type)
26612 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26613 ok = false;
26615 else
26616 ok = false;
26618 if (!ok)
26620 if (cxx_dialect >= cxx14)
26621 error ("literal operator template %qD has invalid parameter list."
26622 " Expected non-type template argument pack <char...>"
26623 " or <typename CharT, CharT...>",
26624 decl);
26625 else
26626 error ("literal operator template %qD has invalid parameter list."
26627 " Expected non-type template argument pack <char...>",
26628 decl);
26632 /* Register member declarations. */
26633 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26634 finish_member_declaration (decl);
26635 /* If DECL is a function template, we must return to parse it later.
26636 (Even though there is no definition, there might be default
26637 arguments that need handling.) */
26638 if (member_p && decl
26639 && DECL_DECLARES_FUNCTION_P (decl))
26640 vec_safe_push (unparsed_funs_with_definitions, decl);
26643 /* Parse a template introduction header for a template-declaration. Returns
26644 false if tentative parse fails. */
26646 static bool
26647 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26649 cp_parser_parse_tentatively (parser);
26651 tree saved_scope = parser->scope;
26652 tree saved_object_scope = parser->object_scope;
26653 tree saved_qualifying_scope = parser->qualifying_scope;
26655 /* Look for the optional `::' operator. */
26656 cp_parser_global_scope_opt (parser,
26657 /*current_scope_valid_p=*/false);
26658 /* Look for the nested-name-specifier. */
26659 cp_parser_nested_name_specifier_opt (parser,
26660 /*typename_keyword_p=*/false,
26661 /*check_dependency_p=*/true,
26662 /*type_p=*/false,
26663 /*is_declaration=*/false);
26665 cp_token *token = cp_lexer_peek_token (parser->lexer);
26666 tree concept_name = cp_parser_identifier (parser);
26668 /* Look up the concept for which we will be matching
26669 template parameters. */
26670 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26671 token->location);
26672 parser->scope = saved_scope;
26673 parser->object_scope = saved_object_scope;
26674 parser->qualifying_scope = saved_qualifying_scope;
26676 if (concept_name == error_mark_node)
26677 cp_parser_simulate_error (parser);
26679 /* Look for opening brace for introduction. */
26680 matching_braces braces;
26681 braces.require_open (parser);
26683 if (!cp_parser_parse_definitely (parser))
26684 return false;
26686 push_deferring_access_checks (dk_deferred);
26688 /* Build vector of placeholder parameters and grab
26689 matching identifiers. */
26690 tree introduction_list = cp_parser_introduction_list (parser);
26692 /* The introduction-list shall not be empty. */
26693 int nargs = TREE_VEC_LENGTH (introduction_list);
26694 if (nargs == 0)
26696 error ("empty introduction-list");
26697 return true;
26700 /* Look for closing brace for introduction. */
26701 if (!braces.require_close (parser))
26702 return true;
26704 if (tmpl_decl == error_mark_node)
26706 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26707 token->location);
26708 return true;
26711 /* Build and associate the constraint. */
26712 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26713 if (parms && parms != error_mark_node)
26715 cp_parser_template_declaration_after_parameters (parser, parms,
26716 member_p);
26717 return true;
26720 error_at (token->location, "no matching concept for template-introduction");
26721 return true;
26724 /* Parse a normal template-declaration following the template keyword. */
26726 static void
26727 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26729 tree parameter_list;
26730 bool need_lang_pop;
26731 location_t location = input_location;
26733 /* Look for the `<' token. */
26734 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26735 return;
26736 if (at_class_scope_p () && current_function_decl)
26738 /* 14.5.2.2 [temp.mem]
26740 A local class shall not have member templates. */
26741 error_at (location,
26742 "invalid declaration of member template in local class");
26743 cp_parser_skip_to_end_of_block_or_statement (parser);
26744 return;
26746 /* [temp]
26748 A template ... shall not have C linkage. */
26749 if (current_lang_name == lang_name_c)
26751 error_at (location, "template with C linkage");
26752 /* Give it C++ linkage to avoid confusing other parts of the
26753 front end. */
26754 push_lang_context (lang_name_cplusplus);
26755 need_lang_pop = true;
26757 else
26758 need_lang_pop = false;
26760 /* We cannot perform access checks on the template parameter
26761 declarations until we know what is being declared, just as we
26762 cannot check the decl-specifier list. */
26763 push_deferring_access_checks (dk_deferred);
26765 /* If the next token is `>', then we have an invalid
26766 specialization. Rather than complain about an invalid template
26767 parameter, issue an error message here. */
26768 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26770 cp_parser_error (parser, "invalid explicit specialization");
26771 begin_specialization ();
26772 parameter_list = NULL_TREE;
26774 else
26776 /* Parse the template parameters. */
26777 parameter_list = cp_parser_template_parameter_list (parser);
26780 /* Look for the `>'. */
26781 cp_parser_skip_to_end_of_template_parameter_list (parser);
26783 /* Manage template requirements */
26784 if (flag_concepts)
26786 tree reqs = get_shorthand_constraints (current_template_parms);
26787 if (tree r = cp_parser_requires_clause_opt (parser))
26788 reqs = conjoin_constraints (reqs, normalize_expression (r));
26789 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26792 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26793 member_p);
26795 /* For the erroneous case of a template with C linkage, we pushed an
26796 implicit C++ linkage scope; exit that scope now. */
26797 if (need_lang_pop)
26798 pop_lang_context ();
26801 /* Parse a template-declaration, assuming that the `export' (and
26802 `extern') keywords, if present, has already been scanned. MEMBER_P
26803 is as for cp_parser_template_declaration. */
26805 static bool
26806 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26808 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26810 cp_lexer_consume_token (parser->lexer);
26811 cp_parser_explicit_template_declaration (parser, member_p);
26812 return true;
26814 else if (flag_concepts)
26815 return cp_parser_template_introduction (parser, member_p);
26817 return false;
26820 /* Perform the deferred access checks from a template-parameter-list.
26821 CHECKS is a TREE_LIST of access checks, as returned by
26822 get_deferred_access_checks. */
26824 static void
26825 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26827 ++processing_template_parmlist;
26828 perform_access_checks (checks, tf_warning_or_error);
26829 --processing_template_parmlist;
26832 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26833 `function-definition' sequence that follows a template header.
26834 If MEMBER_P is true, this declaration appears in a class scope.
26836 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26837 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26839 static tree
26840 cp_parser_single_declaration (cp_parser* parser,
26841 vec<deferred_access_check, va_gc> *checks,
26842 bool member_p,
26843 bool explicit_specialization_p,
26844 bool* friend_p)
26846 int declares_class_or_enum;
26847 tree decl = NULL_TREE;
26848 cp_decl_specifier_seq decl_specifiers;
26849 bool function_definition_p = false;
26850 cp_token *decl_spec_token_start;
26852 /* This function is only used when processing a template
26853 declaration. */
26854 gcc_assert (innermost_scope_kind () == sk_template_parms
26855 || innermost_scope_kind () == sk_template_spec);
26857 /* Defer access checks until we know what is being declared. */
26858 push_deferring_access_checks (dk_deferred);
26860 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26861 alternative. */
26862 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26863 cp_parser_decl_specifier_seq (parser,
26864 CP_PARSER_FLAGS_OPTIONAL,
26865 &decl_specifiers,
26866 &declares_class_or_enum);
26867 if (friend_p)
26868 *friend_p = cp_parser_friend_p (&decl_specifiers);
26870 /* There are no template typedefs. */
26871 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26873 error_at (decl_spec_token_start->location,
26874 "template declaration of %<typedef%>");
26875 decl = error_mark_node;
26878 /* Gather up the access checks that occurred the
26879 decl-specifier-seq. */
26880 stop_deferring_access_checks ();
26882 /* Check for the declaration of a template class. */
26883 if (declares_class_or_enum)
26885 if (cp_parser_declares_only_class_p (parser)
26886 || (declares_class_or_enum & 2))
26888 // If this is a declaration, but not a definition, associate
26889 // any constraints with the type declaration. Constraints
26890 // are associated with definitions in cp_parser_class_specifier.
26891 if (declares_class_or_enum == 1)
26892 associate_classtype_constraints (decl_specifiers.type);
26894 decl = shadow_tag (&decl_specifiers);
26896 /* In this case:
26898 struct C {
26899 friend template <typename T> struct A<T>::B;
26902 A<T>::B will be represented by a TYPENAME_TYPE, and
26903 therefore not recognized by shadow_tag. */
26904 if (friend_p && *friend_p
26905 && !decl
26906 && decl_specifiers.type
26907 && TYPE_P (decl_specifiers.type))
26908 decl = decl_specifiers.type;
26910 if (decl && decl != error_mark_node)
26911 decl = TYPE_NAME (decl);
26912 else
26913 decl = error_mark_node;
26915 /* Perform access checks for template parameters. */
26916 cp_parser_perform_template_parameter_access_checks (checks);
26918 /* Give a helpful diagnostic for
26919 template <class T> struct A { } a;
26920 if we aren't already recovering from an error. */
26921 if (!cp_parser_declares_only_class_p (parser)
26922 && !seen_error ())
26924 error_at (cp_lexer_peek_token (parser->lexer)->location,
26925 "a class template declaration must not declare "
26926 "anything else");
26927 cp_parser_skip_to_end_of_block_or_statement (parser);
26928 goto out;
26933 /* Complain about missing 'typename' or other invalid type names. */
26934 if (!decl_specifiers.any_type_specifiers_p
26935 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26937 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26938 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26939 the rest of this declaration. */
26940 decl = error_mark_node;
26941 goto out;
26944 /* If it's not a template class, try for a template function. If
26945 the next token is a `;', then this declaration does not declare
26946 anything. But, if there were errors in the decl-specifiers, then
26947 the error might well have come from an attempted class-specifier.
26948 In that case, there's no need to warn about a missing declarator. */
26949 if (!decl
26950 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26951 || decl_specifiers.type != error_mark_node))
26953 decl = cp_parser_init_declarator (parser,
26954 &decl_specifiers,
26955 checks,
26956 /*function_definition_allowed_p=*/true,
26957 member_p,
26958 declares_class_or_enum,
26959 &function_definition_p,
26960 NULL, NULL, NULL);
26962 /* 7.1.1-1 [dcl.stc]
26964 A storage-class-specifier shall not be specified in an explicit
26965 specialization... */
26966 if (decl
26967 && explicit_specialization_p
26968 && decl_specifiers.storage_class != sc_none)
26970 error_at (decl_spec_token_start->location,
26971 "explicit template specialization cannot have a storage class");
26972 decl = error_mark_node;
26975 if (decl && VAR_P (decl))
26976 check_template_variable (decl);
26979 /* Look for a trailing `;' after the declaration. */
26980 if (!function_definition_p
26981 && (decl == error_mark_node
26982 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26983 cp_parser_skip_to_end_of_block_or_statement (parser);
26985 out:
26986 pop_deferring_access_checks ();
26988 /* Clear any current qualification; whatever comes next is the start
26989 of something new. */
26990 parser->scope = NULL_TREE;
26991 parser->qualifying_scope = NULL_TREE;
26992 parser->object_scope = NULL_TREE;
26994 return decl;
26997 /* Parse a cast-expression that is not the operand of a unary "&". */
26999 static cp_expr
27000 cp_parser_simple_cast_expression (cp_parser *parser)
27002 return cp_parser_cast_expression (parser, /*address_p=*/false,
27003 /*cast_p=*/false, /*decltype*/false, NULL);
27006 /* Parse a functional cast to TYPE. Returns an expression
27007 representing the cast. */
27009 static cp_expr
27010 cp_parser_functional_cast (cp_parser* parser, tree type)
27012 vec<tree, va_gc> *vec;
27013 tree expression_list;
27014 cp_expr cast;
27015 bool nonconst_p;
27017 location_t start_loc = input_location;
27019 if (!type)
27020 type = error_mark_node;
27022 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27024 cp_lexer_set_source_position (parser->lexer);
27025 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27026 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27027 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27028 if (TREE_CODE (type) == TYPE_DECL)
27029 type = TREE_TYPE (type);
27031 cast = finish_compound_literal (type, expression_list,
27032 tf_warning_or_error, fcl_functional);
27033 /* Create a location of the form:
27034 type_name{i, f}
27035 ^~~~~~~~~~~~~~~
27036 with caret == start at the start of the type name,
27037 finishing at the closing brace. */
27038 location_t finish_loc
27039 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27040 location_t combined_loc = make_location (start_loc, start_loc,
27041 finish_loc);
27042 cast.set_location (combined_loc);
27043 return cast;
27047 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27048 /*cast_p=*/true,
27049 /*allow_expansion_p=*/true,
27050 /*non_constant_p=*/NULL);
27051 if (vec == NULL)
27052 expression_list = error_mark_node;
27053 else
27055 expression_list = build_tree_list_vec (vec);
27056 release_tree_vector (vec);
27059 cast = build_functional_cast (type, expression_list,
27060 tf_warning_or_error);
27061 /* [expr.const]/1: In an integral constant expression "only type
27062 conversions to integral or enumeration type can be used". */
27063 if (TREE_CODE (type) == TYPE_DECL)
27064 type = TREE_TYPE (type);
27065 if (cast != error_mark_node
27066 && !cast_valid_in_integral_constant_expression_p (type)
27067 && cp_parser_non_integral_constant_expression (parser,
27068 NIC_CONSTRUCTOR))
27069 return error_mark_node;
27071 /* Create a location of the form:
27072 float(i)
27073 ^~~~~~~~
27074 with caret == start at the start of the type name,
27075 finishing at the closing paren. */
27076 location_t finish_loc
27077 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27078 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27079 cast.set_location (combined_loc);
27080 return cast;
27083 /* Save the tokens that make up the body of a member function defined
27084 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27085 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27086 specifiers applied to the declaration. Returns the FUNCTION_DECL
27087 for the member function. */
27089 static tree
27090 cp_parser_save_member_function_body (cp_parser* parser,
27091 cp_decl_specifier_seq *decl_specifiers,
27092 cp_declarator *declarator,
27093 tree attributes)
27095 cp_token *first;
27096 cp_token *last;
27097 tree fn;
27098 bool function_try_block = false;
27100 /* Create the FUNCTION_DECL. */
27101 fn = grokmethod (decl_specifiers, declarator, attributes);
27102 cp_finalize_omp_declare_simd (parser, fn);
27103 cp_finalize_oacc_routine (parser, fn, true);
27104 /* If something went badly wrong, bail out now. */
27105 if (fn == error_mark_node)
27107 /* If there's a function-body, skip it. */
27108 if (cp_parser_token_starts_function_definition_p
27109 (cp_lexer_peek_token (parser->lexer)))
27110 cp_parser_skip_to_end_of_block_or_statement (parser);
27111 return error_mark_node;
27114 /* Remember it, if there default args to post process. */
27115 cp_parser_save_default_args (parser, fn);
27117 /* Save away the tokens that make up the body of the
27118 function. */
27119 first = parser->lexer->next_token;
27121 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27122 cp_lexer_consume_token (parser->lexer);
27123 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27124 RID_TRANSACTION_ATOMIC))
27126 cp_lexer_consume_token (parser->lexer);
27127 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27129 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27130 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27131 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27132 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27133 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27135 cp_lexer_consume_token (parser->lexer);
27136 cp_lexer_consume_token (parser->lexer);
27137 cp_lexer_consume_token (parser->lexer);
27138 cp_lexer_consume_token (parser->lexer);
27139 cp_lexer_consume_token (parser->lexer);
27141 else
27142 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27143 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27145 cp_lexer_consume_token (parser->lexer);
27146 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27147 break;
27151 /* Handle function try blocks. */
27152 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27154 cp_lexer_consume_token (parser->lexer);
27155 function_try_block = true;
27157 /* We can have braced-init-list mem-initializers before the fn body. */
27158 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27160 cp_lexer_consume_token (parser->lexer);
27161 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27163 /* cache_group will stop after an un-nested { } pair, too. */
27164 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27165 break;
27167 /* variadic mem-inits have ... after the ')'. */
27168 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27169 cp_lexer_consume_token (parser->lexer);
27172 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27173 /* Handle function try blocks. */
27174 if (function_try_block)
27175 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27176 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27177 last = parser->lexer->next_token;
27179 /* Save away the inline definition; we will process it when the
27180 class is complete. */
27181 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27182 DECL_PENDING_INLINE_P (fn) = 1;
27184 /* We need to know that this was defined in the class, so that
27185 friend templates are handled correctly. */
27186 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27188 /* Add FN to the queue of functions to be parsed later. */
27189 vec_safe_push (unparsed_funs_with_definitions, fn);
27191 return fn;
27194 /* Save the tokens that make up the in-class initializer for a non-static
27195 data member. Returns a DEFAULT_ARG. */
27197 static tree
27198 cp_parser_save_nsdmi (cp_parser* parser)
27200 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27203 /* Parse a template-argument-list, as well as the trailing ">" (but
27204 not the opening "<"). See cp_parser_template_argument_list for the
27205 return value. */
27207 static tree
27208 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27210 tree arguments;
27211 tree saved_scope;
27212 tree saved_qualifying_scope;
27213 tree saved_object_scope;
27214 bool saved_greater_than_is_operator_p;
27215 int saved_unevaluated_operand;
27216 int saved_inhibit_evaluation_warnings;
27218 /* [temp.names]
27220 When parsing a template-id, the first non-nested `>' is taken as
27221 the end of the template-argument-list rather than a greater-than
27222 operator. */
27223 saved_greater_than_is_operator_p
27224 = parser->greater_than_is_operator_p;
27225 parser->greater_than_is_operator_p = false;
27226 /* Parsing the argument list may modify SCOPE, so we save it
27227 here. */
27228 saved_scope = parser->scope;
27229 saved_qualifying_scope = parser->qualifying_scope;
27230 saved_object_scope = parser->object_scope;
27231 /* We need to evaluate the template arguments, even though this
27232 template-id may be nested within a "sizeof". */
27233 saved_unevaluated_operand = cp_unevaluated_operand;
27234 cp_unevaluated_operand = 0;
27235 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27236 c_inhibit_evaluation_warnings = 0;
27237 /* Parse the template-argument-list itself. */
27238 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27239 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27240 arguments = NULL_TREE;
27241 else
27242 arguments = cp_parser_template_argument_list (parser);
27243 /* Look for the `>' that ends the template-argument-list. If we find
27244 a '>>' instead, it's probably just a typo. */
27245 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27247 if (cxx_dialect != cxx98)
27249 /* In C++0x, a `>>' in a template argument list or cast
27250 expression is considered to be two separate `>'
27251 tokens. So, change the current token to a `>', but don't
27252 consume it: it will be consumed later when the outer
27253 template argument list (or cast expression) is parsed.
27254 Note that this replacement of `>' for `>>' is necessary
27255 even if we are parsing tentatively: in the tentative
27256 case, after calling
27257 cp_parser_enclosed_template_argument_list we will always
27258 throw away all of the template arguments and the first
27259 closing `>', either because the template argument list
27260 was erroneous or because we are replacing those tokens
27261 with a CPP_TEMPLATE_ID token. The second `>' (which will
27262 not have been thrown away) is needed either to close an
27263 outer template argument list or to complete a new-style
27264 cast. */
27265 cp_token *token = cp_lexer_peek_token (parser->lexer);
27266 token->type = CPP_GREATER;
27268 else if (!saved_greater_than_is_operator_p)
27270 /* If we're in a nested template argument list, the '>>' has
27271 to be a typo for '> >'. We emit the error message, but we
27272 continue parsing and we push a '>' as next token, so that
27273 the argument list will be parsed correctly. Note that the
27274 global source location is still on the token before the
27275 '>>', so we need to say explicitly where we want it. */
27276 cp_token *token = cp_lexer_peek_token (parser->lexer);
27277 gcc_rich_location richloc (token->location);
27278 richloc.add_fixit_replace ("> >");
27279 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27280 "within a nested template argument list");
27282 token->type = CPP_GREATER;
27284 else
27286 /* If this is not a nested template argument list, the '>>'
27287 is a typo for '>'. Emit an error message and continue.
27288 Same deal about the token location, but here we can get it
27289 right by consuming the '>>' before issuing the diagnostic. */
27290 cp_token *token = cp_lexer_consume_token (parser->lexer);
27291 error_at (token->location,
27292 "spurious %<>>%>, use %<>%> to terminate "
27293 "a template argument list");
27296 else
27297 cp_parser_skip_to_end_of_template_parameter_list (parser);
27298 /* The `>' token might be a greater-than operator again now. */
27299 parser->greater_than_is_operator_p
27300 = saved_greater_than_is_operator_p;
27301 /* Restore the SAVED_SCOPE. */
27302 parser->scope = saved_scope;
27303 parser->qualifying_scope = saved_qualifying_scope;
27304 parser->object_scope = saved_object_scope;
27305 cp_unevaluated_operand = saved_unevaluated_operand;
27306 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27308 return arguments;
27311 /* MEMBER_FUNCTION is a member function, or a friend. If default
27312 arguments, or the body of the function have not yet been parsed,
27313 parse them now. */
27315 static void
27316 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27318 timevar_push (TV_PARSE_INMETH);
27319 /* If this member is a template, get the underlying
27320 FUNCTION_DECL. */
27321 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27322 member_function = DECL_TEMPLATE_RESULT (member_function);
27324 /* There should not be any class definitions in progress at this
27325 point; the bodies of members are only parsed outside of all class
27326 definitions. */
27327 gcc_assert (parser->num_classes_being_defined == 0);
27328 /* While we're parsing the member functions we might encounter more
27329 classes. We want to handle them right away, but we don't want
27330 them getting mixed up with functions that are currently in the
27331 queue. */
27332 push_unparsed_function_queues (parser);
27334 /* Make sure that any template parameters are in scope. */
27335 maybe_begin_member_template_processing (member_function);
27337 /* If the body of the function has not yet been parsed, parse it
27338 now. */
27339 if (DECL_PENDING_INLINE_P (member_function))
27341 tree function_scope;
27342 cp_token_cache *tokens;
27344 /* The function is no longer pending; we are processing it. */
27345 tokens = DECL_PENDING_INLINE_INFO (member_function);
27346 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27347 DECL_PENDING_INLINE_P (member_function) = 0;
27349 /* If this is a local class, enter the scope of the containing
27350 function. */
27351 function_scope = current_function_decl;
27352 if (function_scope)
27353 push_function_context ();
27355 /* Push the body of the function onto the lexer stack. */
27356 cp_parser_push_lexer_for_tokens (parser, tokens);
27358 /* Let the front end know that we going to be defining this
27359 function. */
27360 start_preparsed_function (member_function, NULL_TREE,
27361 SF_PRE_PARSED | SF_INCLASS_INLINE);
27363 /* Don't do access checking if it is a templated function. */
27364 if (processing_template_decl)
27365 push_deferring_access_checks (dk_no_check);
27367 /* #pragma omp declare reduction needs special parsing. */
27368 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27370 parser->lexer->in_pragma = true;
27371 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27372 finish_function (/*inline*/2);
27373 cp_check_omp_declare_reduction (member_function);
27375 else
27376 /* Now, parse the body of the function. */
27377 cp_parser_function_definition_after_declarator (parser,
27378 /*inline_p=*/true);
27380 if (processing_template_decl)
27381 pop_deferring_access_checks ();
27383 /* Leave the scope of the containing function. */
27384 if (function_scope)
27385 pop_function_context ();
27386 cp_parser_pop_lexer (parser);
27389 /* Remove any template parameters from the symbol table. */
27390 maybe_end_member_template_processing ();
27392 /* Restore the queue. */
27393 pop_unparsed_function_queues (parser);
27394 timevar_pop (TV_PARSE_INMETH);
27397 /* If DECL contains any default args, remember it on the unparsed
27398 functions queue. */
27400 static void
27401 cp_parser_save_default_args (cp_parser* parser, tree decl)
27403 tree probe;
27405 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27406 probe;
27407 probe = TREE_CHAIN (probe))
27408 if (TREE_PURPOSE (probe))
27410 cp_default_arg_entry entry = {current_class_type, decl};
27411 vec_safe_push (unparsed_funs_with_default_args, entry);
27412 break;
27416 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27417 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27418 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27419 from the parameter-type-list. */
27421 static tree
27422 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27423 tree default_arg, tree parmtype)
27425 cp_token_cache *tokens;
27426 tree parsed_arg;
27427 bool dummy;
27429 if (default_arg == error_mark_node)
27430 return error_mark_node;
27432 /* Push the saved tokens for the default argument onto the parser's
27433 lexer stack. */
27434 tokens = DEFARG_TOKENS (default_arg);
27435 cp_parser_push_lexer_for_tokens (parser, tokens);
27437 start_lambda_scope (decl);
27439 /* Parse the default argument. */
27440 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27441 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27442 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27444 finish_lambda_scope ();
27446 if (parsed_arg == error_mark_node)
27447 cp_parser_skip_to_end_of_statement (parser);
27449 if (!processing_template_decl)
27451 /* In a non-template class, check conversions now. In a template,
27452 we'll wait and instantiate these as needed. */
27453 if (TREE_CODE (decl) == PARM_DECL)
27454 parsed_arg = check_default_argument (parmtype, parsed_arg,
27455 tf_warning_or_error);
27456 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27457 parsed_arg = error_mark_node;
27458 else
27459 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27462 /* If the token stream has not been completely used up, then
27463 there was extra junk after the end of the default
27464 argument. */
27465 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27467 if (TREE_CODE (decl) == PARM_DECL)
27468 cp_parser_error (parser, "expected %<,%>");
27469 else
27470 cp_parser_error (parser, "expected %<;%>");
27473 /* Revert to the main lexer. */
27474 cp_parser_pop_lexer (parser);
27476 return parsed_arg;
27479 /* FIELD is a non-static data member with an initializer which we saved for
27480 later; parse it now. */
27482 static void
27483 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27485 tree def;
27487 maybe_begin_member_template_processing (field);
27489 push_unparsed_function_queues (parser);
27490 def = cp_parser_late_parse_one_default_arg (parser, field,
27491 DECL_INITIAL (field),
27492 NULL_TREE);
27493 pop_unparsed_function_queues (parser);
27495 maybe_end_member_template_processing ();
27497 DECL_INITIAL (field) = def;
27500 /* FN is a FUNCTION_DECL which may contains a parameter with an
27501 unparsed DEFAULT_ARG. Parse the default args now. This function
27502 assumes that the current scope is the scope in which the default
27503 argument should be processed. */
27505 static void
27506 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27508 bool saved_local_variables_forbidden_p;
27509 tree parm, parmdecl;
27511 /* While we're parsing the default args, we might (due to the
27512 statement expression extension) encounter more classes. We want
27513 to handle them right away, but we don't want them getting mixed
27514 up with default args that are currently in the queue. */
27515 push_unparsed_function_queues (parser);
27517 /* Local variable names (and the `this' keyword) may not appear
27518 in a default argument. */
27519 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27520 parser->local_variables_forbidden_p = true;
27522 push_defarg_context (fn);
27524 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27525 parmdecl = DECL_ARGUMENTS (fn);
27526 parm && parm != void_list_node;
27527 parm = TREE_CHAIN (parm),
27528 parmdecl = DECL_CHAIN (parmdecl))
27530 tree default_arg = TREE_PURPOSE (parm);
27531 tree parsed_arg;
27532 vec<tree, va_gc> *insts;
27533 tree copy;
27534 unsigned ix;
27536 if (!default_arg)
27537 continue;
27539 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27540 /* This can happen for a friend declaration for a function
27541 already declared with default arguments. */
27542 continue;
27544 parsed_arg
27545 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27546 default_arg,
27547 TREE_VALUE (parm));
27548 TREE_PURPOSE (parm) = parsed_arg;
27550 /* Update any instantiations we've already created. */
27551 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27552 vec_safe_iterate (insts, ix, &copy); ix++)
27553 TREE_PURPOSE (copy) = parsed_arg;
27556 pop_defarg_context ();
27558 /* Make sure no default arg is missing. */
27559 check_default_args (fn);
27561 /* Restore the state of local_variables_forbidden_p. */
27562 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27564 /* Restore the queue. */
27565 pop_unparsed_function_queues (parser);
27568 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27570 sizeof ... ( identifier )
27572 where the 'sizeof' token has already been consumed. */
27574 static tree
27575 cp_parser_sizeof_pack (cp_parser *parser)
27577 /* Consume the `...'. */
27578 cp_lexer_consume_token (parser->lexer);
27579 maybe_warn_variadic_templates ();
27581 matching_parens parens;
27582 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27583 if (paren)
27584 parens.consume_open (parser);
27585 else
27586 permerror (cp_lexer_peek_token (parser->lexer)->location,
27587 "%<sizeof...%> argument must be surrounded by parentheses");
27589 cp_token *token = cp_lexer_peek_token (parser->lexer);
27590 tree name = cp_parser_identifier (parser);
27591 if (name == error_mark_node)
27592 return error_mark_node;
27593 /* The name is not qualified. */
27594 parser->scope = NULL_TREE;
27595 parser->qualifying_scope = NULL_TREE;
27596 parser->object_scope = NULL_TREE;
27597 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27598 if (expr == error_mark_node)
27599 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27600 token->location);
27601 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27602 expr = TREE_TYPE (expr);
27603 else if (TREE_CODE (expr) == CONST_DECL)
27604 expr = DECL_INITIAL (expr);
27605 expr = make_pack_expansion (expr);
27606 PACK_EXPANSION_SIZEOF_P (expr) = true;
27608 if (paren)
27609 parens.require_close (parser);
27611 return expr;
27614 /* Parse the operand of `sizeof' (or a similar operator). Returns
27615 either a TYPE or an expression, depending on the form of the
27616 input. The KEYWORD indicates which kind of expression we have
27617 encountered. */
27619 static tree
27620 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27622 tree expr = NULL_TREE;
27623 const char *saved_message;
27624 char *tmp;
27625 bool saved_integral_constant_expression_p;
27626 bool saved_non_integral_constant_expression_p;
27628 /* If it's a `...', then we are computing the length of a parameter
27629 pack. */
27630 if (keyword == RID_SIZEOF
27631 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27632 return cp_parser_sizeof_pack (parser);
27634 /* Types cannot be defined in a `sizeof' expression. Save away the
27635 old message. */
27636 saved_message = parser->type_definition_forbidden_message;
27637 /* And create the new one. */
27638 tmp = concat ("types may not be defined in %<",
27639 IDENTIFIER_POINTER (ridpointers[keyword]),
27640 "%> expressions", NULL);
27641 parser->type_definition_forbidden_message = tmp;
27643 /* The restrictions on constant-expressions do not apply inside
27644 sizeof expressions. */
27645 saved_integral_constant_expression_p
27646 = parser->integral_constant_expression_p;
27647 saved_non_integral_constant_expression_p
27648 = parser->non_integral_constant_expression_p;
27649 parser->integral_constant_expression_p = false;
27651 /* Do not actually evaluate the expression. */
27652 ++cp_unevaluated_operand;
27653 ++c_inhibit_evaluation_warnings;
27654 /* If it's a `(', then we might be looking at the type-id
27655 construction. */
27656 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27658 tree type = NULL_TREE;
27660 /* We can't be sure yet whether we're looking at a type-id or an
27661 expression. */
27662 cp_parser_parse_tentatively (parser);
27664 matching_parens parens;
27665 parens.consume_open (parser);
27667 /* Note: as a GNU Extension, compound literals are considered
27668 postfix-expressions as they are in C99, so they are valid
27669 arguments to sizeof. See comment in cp_parser_cast_expression
27670 for details. */
27671 if (cp_parser_compound_literal_p (parser))
27672 cp_parser_simulate_error (parser);
27673 else
27675 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27676 parser->in_type_id_in_expr_p = true;
27677 /* Look for the type-id. */
27678 type = cp_parser_type_id (parser);
27679 /* Look for the closing `)'. */
27680 parens.require_close (parser);
27681 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27684 /* If all went well, then we're done. */
27685 if (cp_parser_parse_definitely (parser))
27687 cp_decl_specifier_seq decl_specs;
27689 /* Build a trivial decl-specifier-seq. */
27690 clear_decl_specs (&decl_specs);
27691 decl_specs.type = type;
27693 /* Call grokdeclarator to figure out what type this is. */
27694 expr = grokdeclarator (NULL,
27695 &decl_specs,
27696 TYPENAME,
27697 /*initialized=*/0,
27698 /*attrlist=*/NULL);
27702 /* If the type-id production did not work out, then we must be
27703 looking at the unary-expression production. */
27704 if (!expr)
27705 expr = cp_parser_unary_expression (parser);
27707 /* Go back to evaluating expressions. */
27708 --cp_unevaluated_operand;
27709 --c_inhibit_evaluation_warnings;
27711 /* Free the message we created. */
27712 free (tmp);
27713 /* And restore the old one. */
27714 parser->type_definition_forbidden_message = saved_message;
27715 parser->integral_constant_expression_p
27716 = saved_integral_constant_expression_p;
27717 parser->non_integral_constant_expression_p
27718 = saved_non_integral_constant_expression_p;
27720 return expr;
27723 /* If the current declaration has no declarator, return true. */
27725 static bool
27726 cp_parser_declares_only_class_p (cp_parser *parser)
27728 /* If the next token is a `;' or a `,' then there is no
27729 declarator. */
27730 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27731 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27734 /* Update the DECL_SPECS to reflect the storage class indicated by
27735 KEYWORD. */
27737 static void
27738 cp_parser_set_storage_class (cp_parser *parser,
27739 cp_decl_specifier_seq *decl_specs,
27740 enum rid keyword,
27741 cp_token *token)
27743 cp_storage_class storage_class;
27745 if (parser->in_unbraced_linkage_specification_p)
27747 error_at (token->location, "invalid use of %qD in linkage specification",
27748 ridpointers[keyword]);
27749 return;
27751 else if (decl_specs->storage_class != sc_none)
27753 decl_specs->conflicting_specifiers_p = true;
27754 return;
27757 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27758 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27759 && decl_specs->gnu_thread_keyword_p)
27761 pedwarn (decl_specs->locations[ds_thread], 0,
27762 "%<__thread%> before %qD", ridpointers[keyword]);
27765 switch (keyword)
27767 case RID_AUTO:
27768 storage_class = sc_auto;
27769 break;
27770 case RID_REGISTER:
27771 storage_class = sc_register;
27772 break;
27773 case RID_STATIC:
27774 storage_class = sc_static;
27775 break;
27776 case RID_EXTERN:
27777 storage_class = sc_extern;
27778 break;
27779 case RID_MUTABLE:
27780 storage_class = sc_mutable;
27781 break;
27782 default:
27783 gcc_unreachable ();
27785 decl_specs->storage_class = storage_class;
27786 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27788 /* A storage class specifier cannot be applied alongside a typedef
27789 specifier. If there is a typedef specifier present then set
27790 conflicting_specifiers_p which will trigger an error later
27791 on in grokdeclarator. */
27792 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27793 decl_specs->conflicting_specifiers_p = true;
27796 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27797 is true, the type is a class or enum definition. */
27799 static void
27800 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27801 tree type_spec,
27802 cp_token *token,
27803 bool type_definition_p)
27805 decl_specs->any_specifiers_p = true;
27807 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27808 (with, for example, in "typedef int wchar_t;") we remember that
27809 this is what happened. In system headers, we ignore these
27810 declarations so that G++ can work with system headers that are not
27811 C++-safe. */
27812 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27813 && !type_definition_p
27814 && (type_spec == boolean_type_node
27815 || type_spec == char16_type_node
27816 || type_spec == char32_type_node
27817 || type_spec == wchar_type_node)
27818 && (decl_specs->type
27819 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27820 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27821 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27822 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27824 decl_specs->redefined_builtin_type = type_spec;
27825 set_and_check_decl_spec_loc (decl_specs,
27826 ds_redefined_builtin_type_spec,
27827 token);
27828 if (!decl_specs->type)
27830 decl_specs->type = type_spec;
27831 decl_specs->type_definition_p = false;
27832 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27835 else if (decl_specs->type)
27836 decl_specs->multiple_types_p = true;
27837 else
27839 decl_specs->type = type_spec;
27840 decl_specs->type_definition_p = type_definition_p;
27841 decl_specs->redefined_builtin_type = NULL_TREE;
27842 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27846 /* True iff TOKEN is the GNU keyword __thread. */
27848 static bool
27849 token_is__thread (cp_token *token)
27851 gcc_assert (token->keyword == RID_THREAD);
27852 return id_equal (token->u.value, "__thread");
27855 /* Set the location for a declarator specifier and check if it is
27856 duplicated.
27858 DECL_SPECS is the sequence of declarator specifiers onto which to
27859 set the location.
27861 DS is the single declarator specifier to set which location is to
27862 be set onto the existing sequence of declarators.
27864 LOCATION is the location for the declarator specifier to
27865 consider. */
27867 static void
27868 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27869 cp_decl_spec ds, cp_token *token)
27871 gcc_assert (ds < ds_last);
27873 if (decl_specs == NULL)
27874 return;
27876 source_location location = token->location;
27878 if (decl_specs->locations[ds] == 0)
27880 decl_specs->locations[ds] = location;
27881 if (ds == ds_thread)
27882 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27884 else
27886 if (ds == ds_long)
27888 if (decl_specs->locations[ds_long_long] != 0)
27889 error_at (location,
27890 "%<long long long%> is too long for GCC");
27891 else
27893 decl_specs->locations[ds_long_long] = location;
27894 pedwarn_cxx98 (location,
27895 OPT_Wlong_long,
27896 "ISO C++ 1998 does not support %<long long%>");
27899 else if (ds == ds_thread)
27901 bool gnu = token_is__thread (token);
27902 if (gnu != decl_specs->gnu_thread_keyword_p)
27903 error_at (location,
27904 "both %<__thread%> and %<thread_local%> specified");
27905 else
27907 gcc_rich_location richloc (location);
27908 richloc.add_fixit_remove ();
27909 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27912 else
27914 static const char *const decl_spec_names[] = {
27915 "signed",
27916 "unsigned",
27917 "short",
27918 "long",
27919 "const",
27920 "volatile",
27921 "restrict",
27922 "inline",
27923 "virtual",
27924 "explicit",
27925 "friend",
27926 "typedef",
27927 "using",
27928 "constexpr",
27929 "__complex"
27931 gcc_rich_location richloc (location);
27932 richloc.add_fixit_remove ();
27933 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27938 /* Return true iff the declarator specifier DS is present in the
27939 sequence of declarator specifiers DECL_SPECS. */
27941 bool
27942 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27943 cp_decl_spec ds)
27945 gcc_assert (ds < ds_last);
27947 if (decl_specs == NULL)
27948 return false;
27950 return decl_specs->locations[ds] != 0;
27953 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27954 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27956 static bool
27957 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27959 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27962 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
27963 RT_CLOSE_PAREN. */
27965 static const char *
27966 get_matching_symbol (required_token token_desc)
27968 switch (token_desc)
27970 default:
27971 gcc_unreachable ();
27972 return "";
27973 case RT_CLOSE_BRACE:
27974 return "{";
27975 case RT_CLOSE_PAREN:
27976 return "(";
27980 /* Issue an error message indicating that TOKEN_DESC was expected.
27981 If KEYWORD is true, it indicated this function is called by
27982 cp_parser_require_keword and the required token can only be
27983 a indicated keyword.
27985 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
27986 within any error as the location of an "opening" token matching
27987 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
27988 RT_CLOSE_PAREN). */
27990 static void
27991 cp_parser_required_error (cp_parser *parser,
27992 required_token token_desc,
27993 bool keyword,
27994 location_t matching_location)
27996 if (cp_parser_simulate_error (parser))
27997 return;
27999 const char *gmsgid = NULL;
28000 switch (token_desc)
28002 case RT_NEW:
28003 gmsgid = G_("expected %<new%>");
28004 break;
28005 case RT_DELETE:
28006 gmsgid = G_("expected %<delete%>");
28007 break;
28008 case RT_RETURN:
28009 gmsgid = G_("expected %<return%>");
28010 break;
28011 case RT_WHILE:
28012 gmsgid = G_("expected %<while%>");
28013 break;
28014 case RT_EXTERN:
28015 gmsgid = G_("expected %<extern%>");
28016 break;
28017 case RT_STATIC_ASSERT:
28018 gmsgid = G_("expected %<static_assert%>");
28019 break;
28020 case RT_DECLTYPE:
28021 gmsgid = G_("expected %<decltype%>");
28022 break;
28023 case RT_OPERATOR:
28024 gmsgid = G_("expected %<operator%>");
28025 break;
28026 case RT_CLASS:
28027 gmsgid = G_("expected %<class%>");
28028 break;
28029 case RT_TEMPLATE:
28030 gmsgid = G_("expected %<template%>");
28031 break;
28032 case RT_NAMESPACE:
28033 gmsgid = G_("expected %<namespace%>");
28034 break;
28035 case RT_USING:
28036 gmsgid = G_("expected %<using%>");
28037 break;
28038 case RT_ASM:
28039 gmsgid = G_("expected %<asm%>");
28040 break;
28041 case RT_TRY:
28042 gmsgid = G_("expected %<try%>");
28043 break;
28044 case RT_CATCH:
28045 gmsgid = G_("expected %<catch%>");
28046 break;
28047 case RT_THROW:
28048 gmsgid = G_("expected %<throw%>");
28049 break;
28050 case RT_LABEL:
28051 gmsgid = G_("expected %<__label__%>");
28052 break;
28053 case RT_AT_TRY:
28054 gmsgid = G_("expected %<@try%>");
28055 break;
28056 case RT_AT_SYNCHRONIZED:
28057 gmsgid = G_("expected %<@synchronized%>");
28058 break;
28059 case RT_AT_THROW:
28060 gmsgid = G_("expected %<@throw%>");
28061 break;
28062 case RT_TRANSACTION_ATOMIC:
28063 gmsgid = G_("expected %<__transaction_atomic%>");
28064 break;
28065 case RT_TRANSACTION_RELAXED:
28066 gmsgid = G_("expected %<__transaction_relaxed%>");
28067 break;
28068 default:
28069 break;
28072 if (!gmsgid && !keyword)
28074 switch (token_desc)
28076 case RT_SEMICOLON:
28077 gmsgid = G_("expected %<;%>");
28078 break;
28079 case RT_OPEN_PAREN:
28080 gmsgid = G_("expected %<(%>");
28081 break;
28082 case RT_CLOSE_BRACE:
28083 gmsgid = G_("expected %<}%>");
28084 break;
28085 case RT_OPEN_BRACE:
28086 gmsgid = G_("expected %<{%>");
28087 break;
28088 case RT_CLOSE_SQUARE:
28089 gmsgid = G_("expected %<]%>");
28090 break;
28091 case RT_OPEN_SQUARE:
28092 gmsgid = G_("expected %<[%>");
28093 break;
28094 case RT_COMMA:
28095 gmsgid = G_("expected %<,%>");
28096 break;
28097 case RT_SCOPE:
28098 gmsgid = G_("expected %<::%>");
28099 break;
28100 case RT_LESS:
28101 gmsgid = G_("expected %<<%>");
28102 break;
28103 case RT_GREATER:
28104 gmsgid = G_("expected %<>%>");
28105 break;
28106 case RT_EQ:
28107 gmsgid = G_("expected %<=%>");
28108 break;
28109 case RT_ELLIPSIS:
28110 gmsgid = G_("expected %<...%>");
28111 break;
28112 case RT_MULT:
28113 gmsgid = G_("expected %<*%>");
28114 break;
28115 case RT_COMPL:
28116 gmsgid = G_("expected %<~%>");
28117 break;
28118 case RT_COLON:
28119 gmsgid = G_("expected %<:%>");
28120 break;
28121 case RT_COLON_SCOPE:
28122 gmsgid = G_("expected %<:%> or %<::%>");
28123 break;
28124 case RT_CLOSE_PAREN:
28125 gmsgid = G_("expected %<)%>");
28126 break;
28127 case RT_COMMA_CLOSE_PAREN:
28128 gmsgid = G_("expected %<,%> or %<)%>");
28129 break;
28130 case RT_PRAGMA_EOL:
28131 gmsgid = G_("expected end of line");
28132 break;
28133 case RT_NAME:
28134 gmsgid = G_("expected identifier");
28135 break;
28136 case RT_SELECT:
28137 gmsgid = G_("expected selection-statement");
28138 break;
28139 case RT_ITERATION:
28140 gmsgid = G_("expected iteration-statement");
28141 break;
28142 case RT_JUMP:
28143 gmsgid = G_("expected jump-statement");
28144 break;
28145 case RT_CLASS_KEY:
28146 gmsgid = G_("expected class-key");
28147 break;
28148 case RT_CLASS_TYPENAME_TEMPLATE:
28149 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28150 break;
28151 default:
28152 gcc_unreachable ();
28156 if (gmsgid)
28158 /* Emulate rest of cp_parser_error. */
28159 cp_token *token = cp_lexer_peek_token (parser->lexer);
28160 cp_lexer_set_source_position_from_token (token);
28162 gcc_rich_location richloc (input_location);
28164 /* If matching_location != UNKNOWN_LOCATION, highlight it.
28165 Attempt to consolidate diagnostics by printing it as a
28166 secondary range within the main diagnostic. */
28167 bool added_matching_location = false;
28168 if (matching_location != UNKNOWN_LOCATION)
28169 added_matching_location
28170 = richloc.add_location_if_nearby (matching_location);
28172 c_parse_error (gmsgid,
28173 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
28174 token->u.value, token->flags, &richloc);
28176 /* If we weren't able to consolidate matching_location, then
28177 print it as a secondary diagnostic. */
28178 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
28179 inform (matching_location, "to match this %qs",
28180 get_matching_symbol (token_desc));
28185 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28186 issue an error message indicating that TOKEN_DESC was expected.
28188 Returns the token consumed, if the token had the appropriate type.
28189 Otherwise, returns NULL.
28191 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28192 within any error as the location of an "opening" token matching
28193 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28194 RT_CLOSE_PAREN). */
28196 static cp_token *
28197 cp_parser_require (cp_parser* parser,
28198 enum cpp_ttype type,
28199 required_token token_desc,
28200 location_t matching_location)
28202 if (cp_lexer_next_token_is (parser->lexer, type))
28203 return cp_lexer_consume_token (parser->lexer);
28204 else
28206 /* Output the MESSAGE -- unless we're parsing tentatively. */
28207 if (!cp_parser_simulate_error (parser))
28208 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28209 matching_location);
28210 return NULL;
28214 /* An error message is produced if the next token is not '>'.
28215 All further tokens are skipped until the desired token is
28216 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28218 static void
28219 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28221 /* Current level of '< ... >'. */
28222 unsigned level = 0;
28223 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28224 unsigned nesting_depth = 0;
28226 /* Are we ready, yet? If not, issue error message. */
28227 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28228 return;
28230 /* Skip tokens until the desired token is found. */
28231 while (true)
28233 /* Peek at the next token. */
28234 switch (cp_lexer_peek_token (parser->lexer)->type)
28236 case CPP_LESS:
28237 if (!nesting_depth)
28238 ++level;
28239 break;
28241 case CPP_RSHIFT:
28242 if (cxx_dialect == cxx98)
28243 /* C++0x views the `>>' operator as two `>' tokens, but
28244 C++98 does not. */
28245 break;
28246 else if (!nesting_depth && level-- == 0)
28248 /* We've hit a `>>' where the first `>' closes the
28249 template argument list, and the second `>' is
28250 spurious. Just consume the `>>' and stop; we've
28251 already produced at least one error. */
28252 cp_lexer_consume_token (parser->lexer);
28253 return;
28255 /* Fall through for C++0x, so we handle the second `>' in
28256 the `>>'. */
28257 gcc_fallthrough ();
28259 case CPP_GREATER:
28260 if (!nesting_depth && level-- == 0)
28262 /* We've reached the token we want, consume it and stop. */
28263 cp_lexer_consume_token (parser->lexer);
28264 return;
28266 break;
28268 case CPP_OPEN_PAREN:
28269 case CPP_OPEN_SQUARE:
28270 ++nesting_depth;
28271 break;
28273 case CPP_CLOSE_PAREN:
28274 case CPP_CLOSE_SQUARE:
28275 if (nesting_depth-- == 0)
28276 return;
28277 break;
28279 case CPP_EOF:
28280 case CPP_PRAGMA_EOL:
28281 case CPP_SEMICOLON:
28282 case CPP_OPEN_BRACE:
28283 case CPP_CLOSE_BRACE:
28284 /* The '>' was probably forgotten, don't look further. */
28285 return;
28287 default:
28288 break;
28291 /* Consume this token. */
28292 cp_lexer_consume_token (parser->lexer);
28296 /* If the next token is the indicated keyword, consume it. Otherwise,
28297 issue an error message indicating that TOKEN_DESC was expected.
28299 Returns the token consumed, if the token had the appropriate type.
28300 Otherwise, returns NULL. */
28302 static cp_token *
28303 cp_parser_require_keyword (cp_parser* parser,
28304 enum rid keyword,
28305 required_token token_desc)
28307 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28309 if (token && token->keyword != keyword)
28311 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28312 UNKNOWN_LOCATION);
28313 return NULL;
28316 return token;
28319 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28320 function-definition. */
28322 static bool
28323 cp_parser_token_starts_function_definition_p (cp_token* token)
28325 return (/* An ordinary function-body begins with an `{'. */
28326 token->type == CPP_OPEN_BRACE
28327 /* A ctor-initializer begins with a `:'. */
28328 || token->type == CPP_COLON
28329 /* A function-try-block begins with `try'. */
28330 || token->keyword == RID_TRY
28331 /* A function-transaction-block begins with `__transaction_atomic'
28332 or `__transaction_relaxed'. */
28333 || token->keyword == RID_TRANSACTION_ATOMIC
28334 || token->keyword == RID_TRANSACTION_RELAXED
28335 /* The named return value extension begins with `return'. */
28336 || token->keyword == RID_RETURN);
28339 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28340 definition. */
28342 static bool
28343 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28345 cp_token *token;
28347 token = cp_lexer_peek_token (parser->lexer);
28348 return (token->type == CPP_OPEN_BRACE
28349 || (token->type == CPP_COLON
28350 && !parser->colon_doesnt_start_class_def_p));
28353 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28354 C++0x) ending a template-argument. */
28356 static bool
28357 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28359 cp_token *token;
28361 token = cp_lexer_peek_token (parser->lexer);
28362 return (token->type == CPP_COMMA
28363 || token->type == CPP_GREATER
28364 || token->type == CPP_ELLIPSIS
28365 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28368 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28369 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28371 static bool
28372 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28373 size_t n)
28375 cp_token *token;
28377 token = cp_lexer_peek_nth_token (parser->lexer, n);
28378 if (token->type == CPP_LESS)
28379 return true;
28380 /* Check for the sequence `<::' in the original code. It would be lexed as
28381 `[:', where `[' is a digraph, and there is no whitespace before
28382 `:'. */
28383 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28385 cp_token *token2;
28386 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28387 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28388 return true;
28390 return false;
28393 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28394 or none_type otherwise. */
28396 static enum tag_types
28397 cp_parser_token_is_class_key (cp_token* token)
28399 switch (token->keyword)
28401 case RID_CLASS:
28402 return class_type;
28403 case RID_STRUCT:
28404 return record_type;
28405 case RID_UNION:
28406 return union_type;
28408 default:
28409 return none_type;
28413 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28414 or none_type otherwise or if the token is null. */
28416 static enum tag_types
28417 cp_parser_token_is_type_parameter_key (cp_token* token)
28419 if (!token)
28420 return none_type;
28422 switch (token->keyword)
28424 case RID_CLASS:
28425 return class_type;
28426 case RID_TYPENAME:
28427 return typename_type;
28429 default:
28430 return none_type;
28434 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28436 static void
28437 cp_parser_check_class_key (enum tag_types class_key, tree type)
28439 if (type == error_mark_node)
28440 return;
28441 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28443 if (permerror (input_location, "%qs tag used in naming %q#T",
28444 class_key == union_type ? "union"
28445 : class_key == record_type ? "struct" : "class",
28446 type))
28447 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28448 "%q#T was previously declared here", type);
28452 /* Issue an error message if DECL is redeclared with different
28453 access than its original declaration [class.access.spec/3].
28454 This applies to nested classes, nested class templates and
28455 enumerations [class.mem/1]. */
28457 static void
28458 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28460 if (!decl
28461 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28462 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28463 return;
28465 if ((TREE_PRIVATE (decl)
28466 != (current_access_specifier == access_private_node))
28467 || (TREE_PROTECTED (decl)
28468 != (current_access_specifier == access_protected_node)))
28469 error_at (location, "%qD redeclared with different access", decl);
28472 /* Look for the `template' keyword, as a syntactic disambiguator.
28473 Return TRUE iff it is present, in which case it will be
28474 consumed. */
28476 static bool
28477 cp_parser_optional_template_keyword (cp_parser *parser)
28479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28481 /* In C++98 the `template' keyword can only be used within templates;
28482 outside templates the parser can always figure out what is a
28483 template and what is not. In C++11, per the resolution of DR 468,
28484 `template' is allowed in cases where it is not strictly necessary. */
28485 if (!processing_template_decl
28486 && pedantic && cxx_dialect == cxx98)
28488 cp_token *token = cp_lexer_peek_token (parser->lexer);
28489 pedwarn (token->location, OPT_Wpedantic,
28490 "in C++98 %<template%> (as a disambiguator) is only "
28491 "allowed within templates");
28492 /* If this part of the token stream is rescanned, the same
28493 error message would be generated. So, we purge the token
28494 from the stream. */
28495 cp_lexer_purge_token (parser->lexer);
28496 return false;
28498 else
28500 /* Consume the `template' keyword. */
28501 cp_lexer_consume_token (parser->lexer);
28502 return true;
28505 return false;
28508 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28509 set PARSER->SCOPE, and perform other related actions. */
28511 static void
28512 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28514 struct tree_check *check_value;
28516 /* Get the stored value. */
28517 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28518 /* Set the scope from the stored value. */
28519 parser->scope = saved_checks_value (check_value);
28520 parser->qualifying_scope = check_value->qualifying_scope;
28521 parser->object_scope = NULL_TREE;
28524 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28525 encounter the end of a block before what we were looking for. */
28527 static bool
28528 cp_parser_cache_group (cp_parser *parser,
28529 enum cpp_ttype end,
28530 unsigned depth)
28532 while (true)
28534 cp_token *token = cp_lexer_peek_token (parser->lexer);
28536 /* Abort a parenthesized expression if we encounter a semicolon. */
28537 if ((end == CPP_CLOSE_PAREN || depth == 0)
28538 && token->type == CPP_SEMICOLON)
28539 return true;
28540 /* If we've reached the end of the file, stop. */
28541 if (token->type == CPP_EOF
28542 || (end != CPP_PRAGMA_EOL
28543 && token->type == CPP_PRAGMA_EOL))
28544 return true;
28545 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28546 /* We've hit the end of an enclosing block, so there's been some
28547 kind of syntax error. */
28548 return true;
28550 /* Consume the token. */
28551 cp_lexer_consume_token (parser->lexer);
28552 /* See if it starts a new group. */
28553 if (token->type == CPP_OPEN_BRACE)
28555 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28556 /* In theory this should probably check end == '}', but
28557 cp_parser_save_member_function_body needs it to exit
28558 after either '}' or ')' when called with ')'. */
28559 if (depth == 0)
28560 return false;
28562 else if (token->type == CPP_OPEN_PAREN)
28564 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28565 if (depth == 0 && end == CPP_CLOSE_PAREN)
28566 return false;
28568 else if (token->type == CPP_PRAGMA)
28569 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28570 else if (token->type == end)
28571 return false;
28575 /* Like above, for caching a default argument or NSDMI. Both of these are
28576 terminated by a non-nested comma, but it can be unclear whether or not a
28577 comma is nested in a template argument list unless we do more parsing.
28578 In order to handle this ambiguity, when we encounter a ',' after a '<'
28579 we try to parse what follows as a parameter-declaration-list (in the
28580 case of a default argument) or a member-declarator (in the case of an
28581 NSDMI). If that succeeds, then we stop caching. */
28583 static tree
28584 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28586 unsigned depth = 0;
28587 int maybe_template_id = 0;
28588 cp_token *first_token;
28589 cp_token *token;
28590 tree default_argument;
28592 /* Add tokens until we have processed the entire default
28593 argument. We add the range [first_token, token). */
28594 first_token = cp_lexer_peek_token (parser->lexer);
28595 if (first_token->type == CPP_OPEN_BRACE)
28597 /* For list-initialization, this is straightforward. */
28598 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28599 token = cp_lexer_peek_token (parser->lexer);
28601 else while (true)
28603 bool done = false;
28605 /* Peek at the next token. */
28606 token = cp_lexer_peek_token (parser->lexer);
28607 /* What we do depends on what token we have. */
28608 switch (token->type)
28610 /* In valid code, a default argument must be
28611 immediately followed by a `,' `)', or `...'. */
28612 case CPP_COMMA:
28613 if (depth == 0 && maybe_template_id)
28615 /* If we've seen a '<', we might be in a
28616 template-argument-list. Until Core issue 325 is
28617 resolved, we don't know how this situation ought
28618 to be handled, so try to DTRT. We check whether
28619 what comes after the comma is a valid parameter
28620 declaration list. If it is, then the comma ends
28621 the default argument; otherwise the default
28622 argument continues. */
28623 bool error = false;
28624 cp_token *peek;
28626 /* Set ITALP so cp_parser_parameter_declaration_list
28627 doesn't decide to commit to this parse. */
28628 bool saved_italp = parser->in_template_argument_list_p;
28629 parser->in_template_argument_list_p = true;
28631 cp_parser_parse_tentatively (parser);
28633 if (nsdmi)
28635 /* Parse declarators until we reach a non-comma or
28636 somthing that cannot be an initializer.
28637 Just checking whether we're looking at a single
28638 declarator is insufficient. Consider:
28639 int var = tuple<T,U>::x;
28640 The template parameter 'U' looks exactly like a
28641 declarator. */
28644 int ctor_dtor_or_conv_p;
28645 cp_lexer_consume_token (parser->lexer);
28646 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28647 &ctor_dtor_or_conv_p,
28648 /*parenthesized_p=*/NULL,
28649 /*member_p=*/true,
28650 /*friend_p=*/false);
28651 peek = cp_lexer_peek_token (parser->lexer);
28652 if (cp_parser_error_occurred (parser))
28653 break;
28655 while (peek->type == CPP_COMMA);
28656 /* If we met an '=' or ';' then the original comma
28657 was the end of the NSDMI. Otherwise assume
28658 we're still in the NSDMI. */
28659 error = (peek->type != CPP_EQ
28660 && peek->type != CPP_SEMICOLON);
28662 else
28664 cp_lexer_consume_token (parser->lexer);
28665 begin_scope (sk_function_parms, NULL_TREE);
28666 cp_parser_parameter_declaration_list (parser, &error);
28667 pop_bindings_and_leave_scope ();
28669 if (!cp_parser_error_occurred (parser) && !error)
28670 done = true;
28671 cp_parser_abort_tentative_parse (parser);
28673 parser->in_template_argument_list_p = saved_italp;
28674 break;
28676 /* FALLTHRU */
28677 case CPP_CLOSE_PAREN:
28678 case CPP_ELLIPSIS:
28679 /* If we run into a non-nested `;', `}', or `]',
28680 then the code is invalid -- but the default
28681 argument is certainly over. */
28682 case CPP_SEMICOLON:
28683 case CPP_CLOSE_BRACE:
28684 case CPP_CLOSE_SQUARE:
28685 if (depth == 0
28686 /* Handle correctly int n = sizeof ... ( p ); */
28687 && token->type != CPP_ELLIPSIS)
28688 done = true;
28689 /* Update DEPTH, if necessary. */
28690 else if (token->type == CPP_CLOSE_PAREN
28691 || token->type == CPP_CLOSE_BRACE
28692 || token->type == CPP_CLOSE_SQUARE)
28693 --depth;
28694 break;
28696 case CPP_OPEN_PAREN:
28697 case CPP_OPEN_SQUARE:
28698 case CPP_OPEN_BRACE:
28699 ++depth;
28700 break;
28702 case CPP_LESS:
28703 if (depth == 0)
28704 /* This might be the comparison operator, or it might
28705 start a template argument list. */
28706 ++maybe_template_id;
28707 break;
28709 case CPP_RSHIFT:
28710 if (cxx_dialect == cxx98)
28711 break;
28712 /* Fall through for C++0x, which treats the `>>'
28713 operator like two `>' tokens in certain
28714 cases. */
28715 gcc_fallthrough ();
28717 case CPP_GREATER:
28718 if (depth == 0)
28720 /* This might be an operator, or it might close a
28721 template argument list. But if a previous '<'
28722 started a template argument list, this will have
28723 closed it, so we can't be in one anymore. */
28724 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28725 if (maybe_template_id < 0)
28726 maybe_template_id = 0;
28728 break;
28730 /* If we run out of tokens, issue an error message. */
28731 case CPP_EOF:
28732 case CPP_PRAGMA_EOL:
28733 error_at (token->location, "file ends in default argument");
28734 return error_mark_node;
28736 case CPP_NAME:
28737 case CPP_SCOPE:
28738 /* In these cases, we should look for template-ids.
28739 For example, if the default argument is
28740 `X<int, double>()', we need to do name lookup to
28741 figure out whether or not `X' is a template; if
28742 so, the `,' does not end the default argument.
28744 That is not yet done. */
28745 break;
28747 default:
28748 break;
28751 /* If we've reached the end, stop. */
28752 if (done)
28753 break;
28755 /* Add the token to the token block. */
28756 token = cp_lexer_consume_token (parser->lexer);
28759 /* Create a DEFAULT_ARG to represent the unparsed default
28760 argument. */
28761 default_argument = make_node (DEFAULT_ARG);
28762 DEFARG_TOKENS (default_argument)
28763 = cp_token_cache_new (first_token, token);
28764 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28766 return default_argument;
28769 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28771 location_t
28772 defarg_location (tree default_argument)
28774 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28775 location_t start = tokens->first->location;
28776 location_t end = tokens->last->location;
28777 return make_location (start, start, end);
28780 /* Begin parsing tentatively. We always save tokens while parsing
28781 tentatively so that if the tentative parsing fails we can restore the
28782 tokens. */
28784 static void
28785 cp_parser_parse_tentatively (cp_parser* parser)
28787 /* Enter a new parsing context. */
28788 parser->context = cp_parser_context_new (parser->context);
28789 /* Begin saving tokens. */
28790 cp_lexer_save_tokens (parser->lexer);
28791 /* In order to avoid repetitive access control error messages,
28792 access checks are queued up until we are no longer parsing
28793 tentatively. */
28794 push_deferring_access_checks (dk_deferred);
28797 /* Commit to the currently active tentative parse. */
28799 static void
28800 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28802 cp_parser_context *context;
28803 cp_lexer *lexer;
28805 /* Mark all of the levels as committed. */
28806 lexer = parser->lexer;
28807 for (context = parser->context; context->next; context = context->next)
28809 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28810 break;
28811 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28812 while (!cp_lexer_saving_tokens (lexer))
28813 lexer = lexer->next;
28814 cp_lexer_commit_tokens (lexer);
28818 /* Commit to the topmost currently active tentative parse.
28820 Note that this function shouldn't be called when there are
28821 irreversible side-effects while in a tentative state. For
28822 example, we shouldn't create a permanent entry in the symbol
28823 table, or issue an error message that might not apply if the
28824 tentative parse is aborted. */
28826 static void
28827 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28829 cp_parser_context *context = parser->context;
28830 cp_lexer *lexer = parser->lexer;
28832 if (context)
28834 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28835 return;
28836 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28838 while (!cp_lexer_saving_tokens (lexer))
28839 lexer = lexer->next;
28840 cp_lexer_commit_tokens (lexer);
28844 /* Abort the currently active tentative parse. All consumed tokens
28845 will be rolled back, and no diagnostics will be issued. */
28847 static void
28848 cp_parser_abort_tentative_parse (cp_parser* parser)
28850 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28851 || errorcount > 0);
28852 cp_parser_simulate_error (parser);
28853 /* Now, pretend that we want to see if the construct was
28854 successfully parsed. */
28855 cp_parser_parse_definitely (parser);
28858 /* Stop parsing tentatively. If a parse error has occurred, restore the
28859 token stream. Otherwise, commit to the tokens we have consumed.
28860 Returns true if no error occurred; false otherwise. */
28862 static bool
28863 cp_parser_parse_definitely (cp_parser* parser)
28865 bool error_occurred;
28866 cp_parser_context *context;
28868 /* Remember whether or not an error occurred, since we are about to
28869 destroy that information. */
28870 error_occurred = cp_parser_error_occurred (parser);
28871 /* Remove the topmost context from the stack. */
28872 context = parser->context;
28873 parser->context = context->next;
28874 /* If no parse errors occurred, commit to the tentative parse. */
28875 if (!error_occurred)
28877 /* Commit to the tokens read tentatively, unless that was
28878 already done. */
28879 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28880 cp_lexer_commit_tokens (parser->lexer);
28882 pop_to_parent_deferring_access_checks ();
28884 /* Otherwise, if errors occurred, roll back our state so that things
28885 are just as they were before we began the tentative parse. */
28886 else
28888 cp_lexer_rollback_tokens (parser->lexer);
28889 pop_deferring_access_checks ();
28891 /* Add the context to the front of the free list. */
28892 context->next = cp_parser_context_free_list;
28893 cp_parser_context_free_list = context;
28895 return !error_occurred;
28898 /* Returns true if we are parsing tentatively and are not committed to
28899 this tentative parse. */
28901 static bool
28902 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28904 return (cp_parser_parsing_tentatively (parser)
28905 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28908 /* Returns nonzero iff an error has occurred during the most recent
28909 tentative parse. */
28911 static bool
28912 cp_parser_error_occurred (cp_parser* parser)
28914 return (cp_parser_parsing_tentatively (parser)
28915 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28918 /* Returns nonzero if GNU extensions are allowed. */
28920 static bool
28921 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28923 return parser->allow_gnu_extensions_p;
28926 /* Objective-C++ Productions */
28929 /* Parse an Objective-C expression, which feeds into a primary-expression
28930 above.
28932 objc-expression:
28933 objc-message-expression
28934 objc-string-literal
28935 objc-encode-expression
28936 objc-protocol-expression
28937 objc-selector-expression
28939 Returns a tree representation of the expression. */
28941 static cp_expr
28942 cp_parser_objc_expression (cp_parser* parser)
28944 /* Try to figure out what kind of declaration is present. */
28945 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28947 switch (kwd->type)
28949 case CPP_OPEN_SQUARE:
28950 return cp_parser_objc_message_expression (parser);
28952 case CPP_OBJC_STRING:
28953 kwd = cp_lexer_consume_token (parser->lexer);
28954 return objc_build_string_object (kwd->u.value);
28956 case CPP_KEYWORD:
28957 switch (kwd->keyword)
28959 case RID_AT_ENCODE:
28960 return cp_parser_objc_encode_expression (parser);
28962 case RID_AT_PROTOCOL:
28963 return cp_parser_objc_protocol_expression (parser);
28965 case RID_AT_SELECTOR:
28966 return cp_parser_objc_selector_expression (parser);
28968 default:
28969 break;
28971 default:
28972 error_at (kwd->location,
28973 "misplaced %<@%D%> Objective-C++ construct",
28974 kwd->u.value);
28975 cp_parser_skip_to_end_of_block_or_statement (parser);
28978 return error_mark_node;
28981 /* Parse an Objective-C message expression.
28983 objc-message-expression:
28984 [ objc-message-receiver objc-message-args ]
28986 Returns a representation of an Objective-C message. */
28988 static tree
28989 cp_parser_objc_message_expression (cp_parser* parser)
28991 tree receiver, messageargs;
28993 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28994 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28995 receiver = cp_parser_objc_message_receiver (parser);
28996 messageargs = cp_parser_objc_message_args (parser);
28997 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28998 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29000 tree result = objc_build_message_expr (receiver, messageargs);
29002 /* Construct a location e.g.
29003 [self func1:5]
29004 ^~~~~~~~~~~~~~
29005 ranging from the '[' to the ']', with the caret at the start. */
29006 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29007 protected_set_expr_location (result, combined_loc);
29009 return result;
29012 /* Parse an objc-message-receiver.
29014 objc-message-receiver:
29015 expression
29016 simple-type-specifier
29018 Returns a representation of the type or expression. */
29020 static tree
29021 cp_parser_objc_message_receiver (cp_parser* parser)
29023 tree rcv;
29025 /* An Objective-C message receiver may be either (1) a type
29026 or (2) an expression. */
29027 cp_parser_parse_tentatively (parser);
29028 rcv = cp_parser_expression (parser);
29030 /* If that worked out, fine. */
29031 if (cp_parser_parse_definitely (parser))
29032 return rcv;
29034 cp_parser_parse_tentatively (parser);
29035 rcv = cp_parser_simple_type_specifier (parser,
29036 /*decl_specs=*/NULL,
29037 CP_PARSER_FLAGS_NONE);
29039 if (cp_parser_parse_definitely (parser))
29040 return objc_get_class_reference (rcv);
29042 cp_parser_error (parser, "objective-c++ message receiver expected");
29043 return error_mark_node;
29046 /* Parse the arguments and selectors comprising an Objective-C message.
29048 objc-message-args:
29049 objc-selector
29050 objc-selector-args
29051 objc-selector-args , objc-comma-args
29053 objc-selector-args:
29054 objc-selector [opt] : assignment-expression
29055 objc-selector-args objc-selector [opt] : assignment-expression
29057 objc-comma-args:
29058 assignment-expression
29059 objc-comma-args , assignment-expression
29061 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29062 selector arguments and TREE_VALUE containing a list of comma
29063 arguments. */
29065 static tree
29066 cp_parser_objc_message_args (cp_parser* parser)
29068 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29069 bool maybe_unary_selector_p = true;
29070 cp_token *token = cp_lexer_peek_token (parser->lexer);
29072 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29074 tree selector = NULL_TREE, arg;
29076 if (token->type != CPP_COLON)
29077 selector = cp_parser_objc_selector (parser);
29079 /* Detect if we have a unary selector. */
29080 if (maybe_unary_selector_p
29081 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29082 return build_tree_list (selector, NULL_TREE);
29084 maybe_unary_selector_p = false;
29085 cp_parser_require (parser, CPP_COLON, RT_COLON);
29086 arg = cp_parser_assignment_expression (parser);
29088 sel_args
29089 = chainon (sel_args,
29090 build_tree_list (selector, arg));
29092 token = cp_lexer_peek_token (parser->lexer);
29095 /* Handle non-selector arguments, if any. */
29096 while (token->type == CPP_COMMA)
29098 tree arg;
29100 cp_lexer_consume_token (parser->lexer);
29101 arg = cp_parser_assignment_expression (parser);
29103 addl_args
29104 = chainon (addl_args,
29105 build_tree_list (NULL_TREE, arg));
29107 token = cp_lexer_peek_token (parser->lexer);
29110 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29112 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29113 return build_tree_list (error_mark_node, error_mark_node);
29116 return build_tree_list (sel_args, addl_args);
29119 /* Parse an Objective-C encode expression.
29121 objc-encode-expression:
29122 @encode objc-typename
29124 Returns an encoded representation of the type argument. */
29126 static cp_expr
29127 cp_parser_objc_encode_expression (cp_parser* parser)
29129 tree type;
29130 cp_token *token;
29131 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29133 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29134 matching_parens parens;
29135 parens.require_open (parser);
29136 token = cp_lexer_peek_token (parser->lexer);
29137 type = complete_type (cp_parser_type_id (parser));
29138 parens.require_close (parser);
29140 if (!type)
29142 error_at (token->location,
29143 "%<@encode%> must specify a type as an argument");
29144 return error_mark_node;
29147 /* This happens if we find @encode(T) (where T is a template
29148 typename or something dependent on a template typename) when
29149 parsing a template. In that case, we can't compile it
29150 immediately, but we rather create an AT_ENCODE_EXPR which will
29151 need to be instantiated when the template is used.
29153 if (dependent_type_p (type))
29155 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29156 TREE_READONLY (value) = 1;
29157 return value;
29161 /* Build a location of the form:
29162 @encode(int)
29163 ^~~~~~~~~~~~
29164 with caret==start at the @ token, finishing at the close paren. */
29165 location_t combined_loc
29166 = make_location (start_loc, start_loc,
29167 cp_lexer_previous_token (parser->lexer)->location);
29169 return cp_expr (objc_build_encode_expr (type), combined_loc);
29172 /* Parse an Objective-C @defs expression. */
29174 static tree
29175 cp_parser_objc_defs_expression (cp_parser *parser)
29177 tree name;
29179 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29180 matching_parens parens;
29181 parens.require_open (parser);
29182 name = cp_parser_identifier (parser);
29183 parens.require_close (parser);
29185 return objc_get_class_ivars (name);
29188 /* Parse an Objective-C protocol expression.
29190 objc-protocol-expression:
29191 @protocol ( identifier )
29193 Returns a representation of the protocol expression. */
29195 static tree
29196 cp_parser_objc_protocol_expression (cp_parser* parser)
29198 tree proto;
29199 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29201 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29202 matching_parens parens;
29203 parens.require_open (parser);
29204 proto = cp_parser_identifier (parser);
29205 parens.require_close (parser);
29207 /* Build a location of the form:
29208 @protocol(prot)
29209 ^~~~~~~~~~~~~~~
29210 with caret==start at the @ token, finishing at the close paren. */
29211 location_t combined_loc
29212 = make_location (start_loc, start_loc,
29213 cp_lexer_previous_token (parser->lexer)->location);
29214 tree result = objc_build_protocol_expr (proto);
29215 protected_set_expr_location (result, combined_loc);
29216 return result;
29219 /* Parse an Objective-C selector expression.
29221 objc-selector-expression:
29222 @selector ( objc-method-signature )
29224 objc-method-signature:
29225 objc-selector
29226 objc-selector-seq
29228 objc-selector-seq:
29229 objc-selector :
29230 objc-selector-seq objc-selector :
29232 Returns a representation of the method selector. */
29234 static tree
29235 cp_parser_objc_selector_expression (cp_parser* parser)
29237 tree sel_seq = NULL_TREE;
29238 bool maybe_unary_selector_p = true;
29239 cp_token *token;
29240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29242 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29243 matching_parens parens;
29244 parens.require_open (parser);
29245 token = cp_lexer_peek_token (parser->lexer);
29247 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29248 || token->type == CPP_SCOPE)
29250 tree selector = NULL_TREE;
29252 if (token->type != CPP_COLON
29253 || token->type == CPP_SCOPE)
29254 selector = cp_parser_objc_selector (parser);
29256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29257 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29259 /* Detect if we have a unary selector. */
29260 if (maybe_unary_selector_p)
29262 sel_seq = selector;
29263 goto finish_selector;
29265 else
29267 cp_parser_error (parser, "expected %<:%>");
29270 maybe_unary_selector_p = false;
29271 token = cp_lexer_consume_token (parser->lexer);
29273 if (token->type == CPP_SCOPE)
29275 sel_seq
29276 = chainon (sel_seq,
29277 build_tree_list (selector, NULL_TREE));
29278 sel_seq
29279 = chainon (sel_seq,
29280 build_tree_list (NULL_TREE, NULL_TREE));
29282 else
29283 sel_seq
29284 = chainon (sel_seq,
29285 build_tree_list (selector, NULL_TREE));
29287 token = cp_lexer_peek_token (parser->lexer);
29290 finish_selector:
29291 parens.require_close (parser);
29294 /* Build a location of the form:
29295 @selector(func)
29296 ^~~~~~~~~~~~~~~
29297 with caret==start at the @ token, finishing at the close paren. */
29298 location_t combined_loc
29299 = make_location (loc, loc,
29300 cp_lexer_previous_token (parser->lexer)->location);
29301 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29302 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29303 protected_set_expr_location (result, combined_loc);
29304 return result;
29307 /* Parse a list of identifiers.
29309 objc-identifier-list:
29310 identifier
29311 objc-identifier-list , identifier
29313 Returns a TREE_LIST of identifier nodes. */
29315 static tree
29316 cp_parser_objc_identifier_list (cp_parser* parser)
29318 tree identifier;
29319 tree list;
29320 cp_token *sep;
29322 identifier = cp_parser_identifier (parser);
29323 if (identifier == error_mark_node)
29324 return error_mark_node;
29326 list = build_tree_list (NULL_TREE, identifier);
29327 sep = cp_lexer_peek_token (parser->lexer);
29329 while (sep->type == CPP_COMMA)
29331 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29332 identifier = cp_parser_identifier (parser);
29333 if (identifier == error_mark_node)
29334 return list;
29336 list = chainon (list, build_tree_list (NULL_TREE,
29337 identifier));
29338 sep = cp_lexer_peek_token (parser->lexer);
29341 return list;
29344 /* Parse an Objective-C alias declaration.
29346 objc-alias-declaration:
29347 @compatibility_alias identifier identifier ;
29349 This function registers the alias mapping with the Objective-C front end.
29350 It returns nothing. */
29352 static void
29353 cp_parser_objc_alias_declaration (cp_parser* parser)
29355 tree alias, orig;
29357 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29358 alias = cp_parser_identifier (parser);
29359 orig = cp_parser_identifier (parser);
29360 objc_declare_alias (alias, orig);
29361 cp_parser_consume_semicolon_at_end_of_statement (parser);
29364 /* Parse an Objective-C class forward-declaration.
29366 objc-class-declaration:
29367 @class objc-identifier-list ;
29369 The function registers the forward declarations with the Objective-C
29370 front end. It returns nothing. */
29372 static void
29373 cp_parser_objc_class_declaration (cp_parser* parser)
29375 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29376 while (true)
29378 tree id;
29380 id = cp_parser_identifier (parser);
29381 if (id == error_mark_node)
29382 break;
29384 objc_declare_class (id);
29386 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29387 cp_lexer_consume_token (parser->lexer);
29388 else
29389 break;
29391 cp_parser_consume_semicolon_at_end_of_statement (parser);
29394 /* Parse a list of Objective-C protocol references.
29396 objc-protocol-refs-opt:
29397 objc-protocol-refs [opt]
29399 objc-protocol-refs:
29400 < objc-identifier-list >
29402 Returns a TREE_LIST of identifiers, if any. */
29404 static tree
29405 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29407 tree protorefs = NULL_TREE;
29409 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29411 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29412 protorefs = cp_parser_objc_identifier_list (parser);
29413 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29416 return protorefs;
29419 /* Parse a Objective-C visibility specification. */
29421 static void
29422 cp_parser_objc_visibility_spec (cp_parser* parser)
29424 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29426 switch (vis->keyword)
29428 case RID_AT_PRIVATE:
29429 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29430 break;
29431 case RID_AT_PROTECTED:
29432 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29433 break;
29434 case RID_AT_PUBLIC:
29435 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29436 break;
29437 case RID_AT_PACKAGE:
29438 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29439 break;
29440 default:
29441 return;
29444 /* Eat '@private'/'@protected'/'@public'. */
29445 cp_lexer_consume_token (parser->lexer);
29448 /* Parse an Objective-C method type. Return 'true' if it is a class
29449 (+) method, and 'false' if it is an instance (-) method. */
29451 static inline bool
29452 cp_parser_objc_method_type (cp_parser* parser)
29454 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29455 return true;
29456 else
29457 return false;
29460 /* Parse an Objective-C protocol qualifier. */
29462 static tree
29463 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29465 tree quals = NULL_TREE, node;
29466 cp_token *token = cp_lexer_peek_token (parser->lexer);
29468 node = token->u.value;
29470 while (node && identifier_p (node)
29471 && (node == ridpointers [(int) RID_IN]
29472 || node == ridpointers [(int) RID_OUT]
29473 || node == ridpointers [(int) RID_INOUT]
29474 || node == ridpointers [(int) RID_BYCOPY]
29475 || node == ridpointers [(int) RID_BYREF]
29476 || node == ridpointers [(int) RID_ONEWAY]))
29478 quals = tree_cons (NULL_TREE, node, quals);
29479 cp_lexer_consume_token (parser->lexer);
29480 token = cp_lexer_peek_token (parser->lexer);
29481 node = token->u.value;
29484 return quals;
29487 /* Parse an Objective-C typename. */
29489 static tree
29490 cp_parser_objc_typename (cp_parser* parser)
29492 tree type_name = NULL_TREE;
29494 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29496 tree proto_quals, cp_type = NULL_TREE;
29498 matching_parens parens;
29499 parens.consume_open (parser); /* Eat '('. */
29500 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29502 /* An ObjC type name may consist of just protocol qualifiers, in which
29503 case the type shall default to 'id'. */
29504 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29506 cp_type = cp_parser_type_id (parser);
29508 /* If the type could not be parsed, an error has already
29509 been produced. For error recovery, behave as if it had
29510 not been specified, which will use the default type
29511 'id'. */
29512 if (cp_type == error_mark_node)
29514 cp_type = NULL_TREE;
29515 /* We need to skip to the closing parenthesis as
29516 cp_parser_type_id() does not seem to do it for
29517 us. */
29518 cp_parser_skip_to_closing_parenthesis (parser,
29519 /*recovering=*/true,
29520 /*or_comma=*/false,
29521 /*consume_paren=*/false);
29525 parens.require_close (parser);
29526 type_name = build_tree_list (proto_quals, cp_type);
29529 return type_name;
29532 /* Check to see if TYPE refers to an Objective-C selector name. */
29534 static bool
29535 cp_parser_objc_selector_p (enum cpp_ttype type)
29537 return (type == CPP_NAME || type == CPP_KEYWORD
29538 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29539 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29540 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29541 || type == CPP_XOR || type == CPP_XOR_EQ);
29544 /* Parse an Objective-C selector. */
29546 static tree
29547 cp_parser_objc_selector (cp_parser* parser)
29549 cp_token *token = cp_lexer_consume_token (parser->lexer);
29551 if (!cp_parser_objc_selector_p (token->type))
29553 error_at (token->location, "invalid Objective-C++ selector name");
29554 return error_mark_node;
29557 /* C++ operator names are allowed to appear in ObjC selectors. */
29558 switch (token->type)
29560 case CPP_AND_AND: return get_identifier ("and");
29561 case CPP_AND_EQ: return get_identifier ("and_eq");
29562 case CPP_AND: return get_identifier ("bitand");
29563 case CPP_OR: return get_identifier ("bitor");
29564 case CPP_COMPL: return get_identifier ("compl");
29565 case CPP_NOT: return get_identifier ("not");
29566 case CPP_NOT_EQ: return get_identifier ("not_eq");
29567 case CPP_OR_OR: return get_identifier ("or");
29568 case CPP_OR_EQ: return get_identifier ("or_eq");
29569 case CPP_XOR: return get_identifier ("xor");
29570 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29571 default: return token->u.value;
29575 /* Parse an Objective-C params list. */
29577 static tree
29578 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29580 tree params = NULL_TREE;
29581 bool maybe_unary_selector_p = true;
29582 cp_token *token = cp_lexer_peek_token (parser->lexer);
29584 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29586 tree selector = NULL_TREE, type_name, identifier;
29587 tree parm_attr = NULL_TREE;
29589 if (token->keyword == RID_ATTRIBUTE)
29590 break;
29592 if (token->type != CPP_COLON)
29593 selector = cp_parser_objc_selector (parser);
29595 /* Detect if we have a unary selector. */
29596 if (maybe_unary_selector_p
29597 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29599 params = selector; /* Might be followed by attributes. */
29600 break;
29603 maybe_unary_selector_p = false;
29604 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29606 /* Something went quite wrong. There should be a colon
29607 here, but there is not. Stop parsing parameters. */
29608 break;
29610 type_name = cp_parser_objc_typename (parser);
29611 /* New ObjC allows attributes on parameters too. */
29612 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29613 parm_attr = cp_parser_attributes_opt (parser);
29614 identifier = cp_parser_identifier (parser);
29616 params
29617 = chainon (params,
29618 objc_build_keyword_decl (selector,
29619 type_name,
29620 identifier,
29621 parm_attr));
29623 token = cp_lexer_peek_token (parser->lexer);
29626 if (params == NULL_TREE)
29628 cp_parser_error (parser, "objective-c++ method declaration is expected");
29629 return error_mark_node;
29632 /* We allow tail attributes for the method. */
29633 if (token->keyword == RID_ATTRIBUTE)
29635 *attributes = cp_parser_attributes_opt (parser);
29636 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29637 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29638 return params;
29639 cp_parser_error (parser,
29640 "method attributes must be specified at the end");
29641 return error_mark_node;
29644 if (params == NULL_TREE)
29646 cp_parser_error (parser, "objective-c++ method declaration is expected");
29647 return error_mark_node;
29649 return params;
29652 /* Parse the non-keyword Objective-C params. */
29654 static tree
29655 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29656 tree* attributes)
29658 tree params = make_node (TREE_LIST);
29659 cp_token *token = cp_lexer_peek_token (parser->lexer);
29660 *ellipsisp = false; /* Initially, assume no ellipsis. */
29662 while (token->type == CPP_COMMA)
29664 cp_parameter_declarator *parmdecl;
29665 tree parm;
29667 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29668 token = cp_lexer_peek_token (parser->lexer);
29670 if (token->type == CPP_ELLIPSIS)
29672 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29673 *ellipsisp = true;
29674 token = cp_lexer_peek_token (parser->lexer);
29675 break;
29678 /* TODO: parse attributes for tail parameters. */
29679 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29680 parm = grokdeclarator (parmdecl->declarator,
29681 &parmdecl->decl_specifiers,
29682 PARM, /*initialized=*/0,
29683 /*attrlist=*/NULL);
29685 chainon (params, build_tree_list (NULL_TREE, parm));
29686 token = cp_lexer_peek_token (parser->lexer);
29689 /* We allow tail attributes for the method. */
29690 if (token->keyword == RID_ATTRIBUTE)
29692 if (*attributes == NULL_TREE)
29694 *attributes = cp_parser_attributes_opt (parser);
29695 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29696 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29697 return params;
29699 else
29700 /* We have an error, but parse the attributes, so that we can
29701 carry on. */
29702 *attributes = cp_parser_attributes_opt (parser);
29704 cp_parser_error (parser,
29705 "method attributes must be specified at the end");
29706 return error_mark_node;
29709 return params;
29712 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29714 static void
29715 cp_parser_objc_interstitial_code (cp_parser* parser)
29717 cp_token *token = cp_lexer_peek_token (parser->lexer);
29719 /* If the next token is `extern' and the following token is a string
29720 literal, then we have a linkage specification. */
29721 if (token->keyword == RID_EXTERN
29722 && cp_parser_is_pure_string_literal
29723 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29724 cp_parser_linkage_specification (parser);
29725 /* Handle #pragma, if any. */
29726 else if (token->type == CPP_PRAGMA)
29727 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29728 /* Allow stray semicolons. */
29729 else if (token->type == CPP_SEMICOLON)
29730 cp_lexer_consume_token (parser->lexer);
29731 /* Mark methods as optional or required, when building protocols. */
29732 else if (token->keyword == RID_AT_OPTIONAL)
29734 cp_lexer_consume_token (parser->lexer);
29735 objc_set_method_opt (true);
29737 else if (token->keyword == RID_AT_REQUIRED)
29739 cp_lexer_consume_token (parser->lexer);
29740 objc_set_method_opt (false);
29742 else if (token->keyword == RID_NAMESPACE)
29743 cp_parser_namespace_definition (parser);
29744 /* Other stray characters must generate errors. */
29745 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29747 cp_lexer_consume_token (parser->lexer);
29748 error ("stray %qs between Objective-C++ methods",
29749 token->type == CPP_OPEN_BRACE ? "{" : "}");
29751 /* Finally, try to parse a block-declaration, or a function-definition. */
29752 else
29753 cp_parser_block_declaration (parser, /*statement_p=*/false);
29756 /* Parse a method signature. */
29758 static tree
29759 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29761 tree rettype, kwdparms, optparms;
29762 bool ellipsis = false;
29763 bool is_class_method;
29765 is_class_method = cp_parser_objc_method_type (parser);
29766 rettype = cp_parser_objc_typename (parser);
29767 *attributes = NULL_TREE;
29768 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29769 if (kwdparms == error_mark_node)
29770 return error_mark_node;
29771 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29772 if (optparms == error_mark_node)
29773 return error_mark_node;
29775 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29778 static bool
29779 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29781 tree tattr;
29782 cp_lexer_save_tokens (parser->lexer);
29783 tattr = cp_parser_attributes_opt (parser);
29784 gcc_assert (tattr) ;
29786 /* If the attributes are followed by a method introducer, this is not allowed.
29787 Dump the attributes and flag the situation. */
29788 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29789 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29790 return true;
29792 /* Otherwise, the attributes introduce some interstitial code, possibly so
29793 rewind to allow that check. */
29794 cp_lexer_rollback_tokens (parser->lexer);
29795 return false;
29798 /* Parse an Objective-C method prototype list. */
29800 static void
29801 cp_parser_objc_method_prototype_list (cp_parser* parser)
29803 cp_token *token = cp_lexer_peek_token (parser->lexer);
29805 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29807 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29809 tree attributes, sig;
29810 bool is_class_method;
29811 if (token->type == CPP_PLUS)
29812 is_class_method = true;
29813 else
29814 is_class_method = false;
29815 sig = cp_parser_objc_method_signature (parser, &attributes);
29816 if (sig == error_mark_node)
29818 cp_parser_skip_to_end_of_block_or_statement (parser);
29819 token = cp_lexer_peek_token (parser->lexer);
29820 continue;
29822 objc_add_method_declaration (is_class_method, sig, attributes);
29823 cp_parser_consume_semicolon_at_end_of_statement (parser);
29825 else if (token->keyword == RID_AT_PROPERTY)
29826 cp_parser_objc_at_property_declaration (parser);
29827 else if (token->keyword == RID_ATTRIBUTE
29828 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29829 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29830 OPT_Wattributes,
29831 "prefix attributes are ignored for methods");
29832 else
29833 /* Allow for interspersed non-ObjC++ code. */
29834 cp_parser_objc_interstitial_code (parser);
29836 token = cp_lexer_peek_token (parser->lexer);
29839 if (token->type != CPP_EOF)
29840 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29841 else
29842 cp_parser_error (parser, "expected %<@end%>");
29844 objc_finish_interface ();
29847 /* Parse an Objective-C method definition list. */
29849 static void
29850 cp_parser_objc_method_definition_list (cp_parser* parser)
29852 cp_token *token = cp_lexer_peek_token (parser->lexer);
29854 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29856 tree meth;
29858 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29860 cp_token *ptk;
29861 tree sig, attribute;
29862 bool is_class_method;
29863 if (token->type == CPP_PLUS)
29864 is_class_method = true;
29865 else
29866 is_class_method = false;
29867 push_deferring_access_checks (dk_deferred);
29868 sig = cp_parser_objc_method_signature (parser, &attribute);
29869 if (sig == error_mark_node)
29871 cp_parser_skip_to_end_of_block_or_statement (parser);
29872 token = cp_lexer_peek_token (parser->lexer);
29873 continue;
29875 objc_start_method_definition (is_class_method, sig, attribute,
29876 NULL_TREE);
29878 /* For historical reasons, we accept an optional semicolon. */
29879 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29880 cp_lexer_consume_token (parser->lexer);
29882 ptk = cp_lexer_peek_token (parser->lexer);
29883 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29884 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29886 perform_deferred_access_checks (tf_warning_or_error);
29887 stop_deferring_access_checks ();
29888 meth = cp_parser_function_definition_after_declarator (parser,
29889 false);
29890 pop_deferring_access_checks ();
29891 objc_finish_method_definition (meth);
29894 /* The following case will be removed once @synthesize is
29895 completely implemented. */
29896 else if (token->keyword == RID_AT_PROPERTY)
29897 cp_parser_objc_at_property_declaration (parser);
29898 else if (token->keyword == RID_AT_SYNTHESIZE)
29899 cp_parser_objc_at_synthesize_declaration (parser);
29900 else if (token->keyword == RID_AT_DYNAMIC)
29901 cp_parser_objc_at_dynamic_declaration (parser);
29902 else if (token->keyword == RID_ATTRIBUTE
29903 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29904 warning_at (token->location, OPT_Wattributes,
29905 "prefix attributes are ignored for methods");
29906 else
29907 /* Allow for interspersed non-ObjC++ code. */
29908 cp_parser_objc_interstitial_code (parser);
29910 token = cp_lexer_peek_token (parser->lexer);
29913 if (token->type != CPP_EOF)
29914 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29915 else
29916 cp_parser_error (parser, "expected %<@end%>");
29918 objc_finish_implementation ();
29921 /* Parse Objective-C ivars. */
29923 static void
29924 cp_parser_objc_class_ivars (cp_parser* parser)
29926 cp_token *token = cp_lexer_peek_token (parser->lexer);
29928 if (token->type != CPP_OPEN_BRACE)
29929 return; /* No ivars specified. */
29931 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29932 token = cp_lexer_peek_token (parser->lexer);
29934 while (token->type != CPP_CLOSE_BRACE
29935 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29937 cp_decl_specifier_seq declspecs;
29938 int decl_class_or_enum_p;
29939 tree prefix_attributes;
29941 cp_parser_objc_visibility_spec (parser);
29943 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29944 break;
29946 cp_parser_decl_specifier_seq (parser,
29947 CP_PARSER_FLAGS_OPTIONAL,
29948 &declspecs,
29949 &decl_class_or_enum_p);
29951 /* auto, register, static, extern, mutable. */
29952 if (declspecs.storage_class != sc_none)
29954 cp_parser_error (parser, "invalid type for instance variable");
29955 declspecs.storage_class = sc_none;
29958 /* thread_local. */
29959 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29961 cp_parser_error (parser, "invalid type for instance variable");
29962 declspecs.locations[ds_thread] = 0;
29965 /* typedef. */
29966 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29968 cp_parser_error (parser, "invalid type for instance variable");
29969 declspecs.locations[ds_typedef] = 0;
29972 prefix_attributes = declspecs.attributes;
29973 declspecs.attributes = NULL_TREE;
29975 /* Keep going until we hit the `;' at the end of the
29976 declaration. */
29977 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29979 tree width = NULL_TREE, attributes, first_attribute, decl;
29980 cp_declarator *declarator = NULL;
29981 int ctor_dtor_or_conv_p;
29983 /* Check for a (possibly unnamed) bitfield declaration. */
29984 token = cp_lexer_peek_token (parser->lexer);
29985 if (token->type == CPP_COLON)
29986 goto eat_colon;
29988 if (token->type == CPP_NAME
29989 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29990 == CPP_COLON))
29992 /* Get the name of the bitfield. */
29993 declarator = make_id_declarator (NULL_TREE,
29994 cp_parser_identifier (parser),
29995 sfk_none);
29997 eat_colon:
29998 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29999 /* Get the width of the bitfield. */
30000 width
30001 = cp_parser_constant_expression (parser);
30003 else
30005 /* Parse the declarator. */
30006 declarator
30007 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30008 &ctor_dtor_or_conv_p,
30009 /*parenthesized_p=*/NULL,
30010 /*member_p=*/false,
30011 /*friend_p=*/false);
30014 /* Look for attributes that apply to the ivar. */
30015 attributes = cp_parser_attributes_opt (parser);
30016 /* Remember which attributes are prefix attributes and
30017 which are not. */
30018 first_attribute = attributes;
30019 /* Combine the attributes. */
30020 attributes = chainon (prefix_attributes, attributes);
30022 if (width)
30023 /* Create the bitfield declaration. */
30024 decl = grokbitfield (declarator, &declspecs,
30025 width,
30026 attributes);
30027 else
30028 decl = grokfield (declarator, &declspecs,
30029 NULL_TREE, /*init_const_expr_p=*/false,
30030 NULL_TREE, attributes);
30032 /* Add the instance variable. */
30033 if (decl != error_mark_node && decl != NULL_TREE)
30034 objc_add_instance_variable (decl);
30036 /* Reset PREFIX_ATTRIBUTES. */
30037 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30038 attributes = TREE_CHAIN (attributes);
30039 if (attributes)
30040 TREE_CHAIN (attributes) = NULL_TREE;
30042 token = cp_lexer_peek_token (parser->lexer);
30044 if (token->type == CPP_COMMA)
30046 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30047 continue;
30049 break;
30052 cp_parser_consume_semicolon_at_end_of_statement (parser);
30053 token = cp_lexer_peek_token (parser->lexer);
30056 if (token->keyword == RID_AT_END)
30057 cp_parser_error (parser, "expected %<}%>");
30059 /* Do not consume the RID_AT_END, so it will be read again as terminating
30060 the @interface of @implementation. */
30061 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30062 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30064 /* For historical reasons, we accept an optional semicolon. */
30065 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30066 cp_lexer_consume_token (parser->lexer);
30069 /* Parse an Objective-C protocol declaration. */
30071 static void
30072 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30074 tree proto, protorefs;
30075 cp_token *tok;
30077 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30080 tok = cp_lexer_peek_token (parser->lexer);
30081 error_at (tok->location, "identifier expected after %<@protocol%>");
30082 cp_parser_consume_semicolon_at_end_of_statement (parser);
30083 return;
30086 /* See if we have a forward declaration or a definition. */
30087 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30089 /* Try a forward declaration first. */
30090 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30092 while (true)
30094 tree id;
30096 id = cp_parser_identifier (parser);
30097 if (id == error_mark_node)
30098 break;
30100 objc_declare_protocol (id, attributes);
30102 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30103 cp_lexer_consume_token (parser->lexer);
30104 else
30105 break;
30107 cp_parser_consume_semicolon_at_end_of_statement (parser);
30110 /* Ok, we got a full-fledged definition (or at least should). */
30111 else
30113 proto = cp_parser_identifier (parser);
30114 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30115 objc_start_protocol (proto, protorefs, attributes);
30116 cp_parser_objc_method_prototype_list (parser);
30120 /* Parse an Objective-C superclass or category. */
30122 static void
30123 cp_parser_objc_superclass_or_category (cp_parser *parser,
30124 bool iface_p,
30125 tree *super,
30126 tree *categ, bool *is_class_extension)
30128 cp_token *next = cp_lexer_peek_token (parser->lexer);
30130 *super = *categ = NULL_TREE;
30131 *is_class_extension = false;
30132 if (next->type == CPP_COLON)
30134 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30135 *super = cp_parser_identifier (parser);
30137 else if (next->type == CPP_OPEN_PAREN)
30139 matching_parens parens;
30140 parens.consume_open (parser); /* Eat '('. */
30142 /* If there is no category name, and this is an @interface, we
30143 have a class extension. */
30144 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30146 *categ = NULL_TREE;
30147 *is_class_extension = true;
30149 else
30150 *categ = cp_parser_identifier (parser);
30152 parens.require_close (parser);
30156 /* Parse an Objective-C class interface. */
30158 static void
30159 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30161 tree name, super, categ, protos;
30162 bool is_class_extension;
30164 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30165 name = cp_parser_identifier (parser);
30166 if (name == error_mark_node)
30168 /* It's hard to recover because even if valid @interface stuff
30169 is to follow, we can't compile it (or validate it) if we
30170 don't even know which class it refers to. Let's assume this
30171 was a stray '@interface' token in the stream and skip it.
30173 return;
30175 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30176 &is_class_extension);
30177 protos = cp_parser_objc_protocol_refs_opt (parser);
30179 /* We have either a class or a category on our hands. */
30180 if (categ || is_class_extension)
30181 objc_start_category_interface (name, categ, protos, attributes);
30182 else
30184 objc_start_class_interface (name, super, protos, attributes);
30185 /* Handle instance variable declarations, if any. */
30186 cp_parser_objc_class_ivars (parser);
30187 objc_continue_interface ();
30190 cp_parser_objc_method_prototype_list (parser);
30193 /* Parse an Objective-C class implementation. */
30195 static void
30196 cp_parser_objc_class_implementation (cp_parser* parser)
30198 tree name, super, categ;
30199 bool is_class_extension;
30201 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30202 name = cp_parser_identifier (parser);
30203 if (name == error_mark_node)
30205 /* It's hard to recover because even if valid @implementation
30206 stuff is to follow, we can't compile it (or validate it) if
30207 we don't even know which class it refers to. Let's assume
30208 this was a stray '@implementation' token in the stream and
30209 skip it.
30211 return;
30213 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30214 &is_class_extension);
30216 /* We have either a class or a category on our hands. */
30217 if (categ)
30218 objc_start_category_implementation (name, categ);
30219 else
30221 objc_start_class_implementation (name, super);
30222 /* Handle instance variable declarations, if any. */
30223 cp_parser_objc_class_ivars (parser);
30224 objc_continue_implementation ();
30227 cp_parser_objc_method_definition_list (parser);
30230 /* Consume the @end token and finish off the implementation. */
30232 static void
30233 cp_parser_objc_end_implementation (cp_parser* parser)
30235 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30236 objc_finish_implementation ();
30239 /* Parse an Objective-C declaration. */
30241 static void
30242 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30244 /* Try to figure out what kind of declaration is present. */
30245 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30247 if (attributes)
30248 switch (kwd->keyword)
30250 case RID_AT_ALIAS:
30251 case RID_AT_CLASS:
30252 case RID_AT_END:
30253 error_at (kwd->location, "attributes may not be specified before"
30254 " the %<@%D%> Objective-C++ keyword",
30255 kwd->u.value);
30256 attributes = NULL;
30257 break;
30258 case RID_AT_IMPLEMENTATION:
30259 warning_at (kwd->location, OPT_Wattributes,
30260 "prefix attributes are ignored before %<@%D%>",
30261 kwd->u.value);
30262 attributes = NULL;
30263 default:
30264 break;
30267 switch (kwd->keyword)
30269 case RID_AT_ALIAS:
30270 cp_parser_objc_alias_declaration (parser);
30271 break;
30272 case RID_AT_CLASS:
30273 cp_parser_objc_class_declaration (parser);
30274 break;
30275 case RID_AT_PROTOCOL:
30276 cp_parser_objc_protocol_declaration (parser, attributes);
30277 break;
30278 case RID_AT_INTERFACE:
30279 cp_parser_objc_class_interface (parser, attributes);
30280 break;
30281 case RID_AT_IMPLEMENTATION:
30282 cp_parser_objc_class_implementation (parser);
30283 break;
30284 case RID_AT_END:
30285 cp_parser_objc_end_implementation (parser);
30286 break;
30287 default:
30288 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30289 kwd->u.value);
30290 cp_parser_skip_to_end_of_block_or_statement (parser);
30294 /* Parse an Objective-C try-catch-finally statement.
30296 objc-try-catch-finally-stmt:
30297 @try compound-statement objc-catch-clause-seq [opt]
30298 objc-finally-clause [opt]
30300 objc-catch-clause-seq:
30301 objc-catch-clause objc-catch-clause-seq [opt]
30303 objc-catch-clause:
30304 @catch ( objc-exception-declaration ) compound-statement
30306 objc-finally-clause:
30307 @finally compound-statement
30309 objc-exception-declaration:
30310 parameter-declaration
30311 '...'
30313 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30315 Returns NULL_TREE.
30317 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30318 for C. Keep them in sync. */
30320 static tree
30321 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30323 location_t location;
30324 tree stmt;
30326 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30327 location = cp_lexer_peek_token (parser->lexer)->location;
30328 objc_maybe_warn_exceptions (location);
30329 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30330 node, lest it get absorbed into the surrounding block. */
30331 stmt = push_stmt_list ();
30332 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30333 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30335 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30337 cp_parameter_declarator *parm;
30338 tree parameter_declaration = error_mark_node;
30339 bool seen_open_paren = false;
30340 matching_parens parens;
30342 cp_lexer_consume_token (parser->lexer);
30343 if (parens.require_open (parser))
30344 seen_open_paren = true;
30345 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30347 /* We have "@catch (...)" (where the '...' are literally
30348 what is in the code). Skip the '...'.
30349 parameter_declaration is set to NULL_TREE, and
30350 objc_being_catch_clauses() knows that that means
30351 '...'. */
30352 cp_lexer_consume_token (parser->lexer);
30353 parameter_declaration = NULL_TREE;
30355 else
30357 /* We have "@catch (NSException *exception)" or something
30358 like that. Parse the parameter declaration. */
30359 parm = cp_parser_parameter_declaration (parser, false, NULL);
30360 if (parm == NULL)
30361 parameter_declaration = error_mark_node;
30362 else
30363 parameter_declaration = grokdeclarator (parm->declarator,
30364 &parm->decl_specifiers,
30365 PARM, /*initialized=*/0,
30366 /*attrlist=*/NULL);
30368 if (seen_open_paren)
30369 parens.require_close (parser);
30370 else
30372 /* If there was no open parenthesis, we are recovering from
30373 an error, and we are trying to figure out what mistake
30374 the user has made. */
30376 /* If there is an immediate closing parenthesis, the user
30377 probably forgot the opening one (ie, they typed "@catch
30378 NSException *e)". Parse the closing parenthesis and keep
30379 going. */
30380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30381 cp_lexer_consume_token (parser->lexer);
30383 /* If these is no immediate closing parenthesis, the user
30384 probably doesn't know that parenthesis are required at
30385 all (ie, they typed "@catch NSException *e"). So, just
30386 forget about the closing parenthesis and keep going. */
30388 objc_begin_catch_clause (parameter_declaration);
30389 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30390 objc_finish_catch_clause ();
30392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30394 cp_lexer_consume_token (parser->lexer);
30395 location = cp_lexer_peek_token (parser->lexer)->location;
30396 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30397 node, lest it get absorbed into the surrounding block. */
30398 stmt = push_stmt_list ();
30399 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30400 objc_build_finally_clause (location, pop_stmt_list (stmt));
30403 return objc_finish_try_stmt ();
30406 /* Parse an Objective-C synchronized statement.
30408 objc-synchronized-stmt:
30409 @synchronized ( expression ) compound-statement
30411 Returns NULL_TREE. */
30413 static tree
30414 cp_parser_objc_synchronized_statement (cp_parser *parser)
30416 location_t location;
30417 tree lock, stmt;
30419 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30421 location = cp_lexer_peek_token (parser->lexer)->location;
30422 objc_maybe_warn_exceptions (location);
30423 matching_parens parens;
30424 parens.require_open (parser);
30425 lock = cp_parser_expression (parser);
30426 parens.require_close (parser);
30428 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30429 node, lest it get absorbed into the surrounding block. */
30430 stmt = push_stmt_list ();
30431 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30433 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30436 /* Parse an Objective-C throw statement.
30438 objc-throw-stmt:
30439 @throw assignment-expression [opt] ;
30441 Returns a constructed '@throw' statement. */
30443 static tree
30444 cp_parser_objc_throw_statement (cp_parser *parser)
30446 tree expr = NULL_TREE;
30447 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30449 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30451 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30452 expr = cp_parser_expression (parser);
30454 cp_parser_consume_semicolon_at_end_of_statement (parser);
30456 return objc_build_throw_stmt (loc, expr);
30459 /* Parse an Objective-C statement. */
30461 static tree
30462 cp_parser_objc_statement (cp_parser * parser)
30464 /* Try to figure out what kind of declaration is present. */
30465 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30467 switch (kwd->keyword)
30469 case RID_AT_TRY:
30470 return cp_parser_objc_try_catch_finally_statement (parser);
30471 case RID_AT_SYNCHRONIZED:
30472 return cp_parser_objc_synchronized_statement (parser);
30473 case RID_AT_THROW:
30474 return cp_parser_objc_throw_statement (parser);
30475 default:
30476 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30477 kwd->u.value);
30478 cp_parser_skip_to_end_of_block_or_statement (parser);
30481 return error_mark_node;
30484 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30485 look ahead to see if an objc keyword follows the attributes. This
30486 is to detect the use of prefix attributes on ObjC @interface and
30487 @protocol. */
30489 static bool
30490 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30492 cp_lexer_save_tokens (parser->lexer);
30493 *attrib = cp_parser_attributes_opt (parser);
30494 gcc_assert (*attrib);
30495 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30497 cp_lexer_commit_tokens (parser->lexer);
30498 return true;
30500 cp_lexer_rollback_tokens (parser->lexer);
30501 return false;
30504 /* This routine is a minimal replacement for
30505 c_parser_struct_declaration () used when parsing the list of
30506 types/names or ObjC++ properties. For example, when parsing the
30507 code
30509 @property (readonly) int a, b, c;
30511 this function is responsible for parsing "int a, int b, int c" and
30512 returning the declarations as CHAIN of DECLs.
30514 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30515 similar parsing. */
30516 static tree
30517 cp_parser_objc_struct_declaration (cp_parser *parser)
30519 tree decls = NULL_TREE;
30520 cp_decl_specifier_seq declspecs;
30521 int decl_class_or_enum_p;
30522 tree prefix_attributes;
30524 cp_parser_decl_specifier_seq (parser,
30525 CP_PARSER_FLAGS_NONE,
30526 &declspecs,
30527 &decl_class_or_enum_p);
30529 if (declspecs.type == error_mark_node)
30530 return error_mark_node;
30532 /* auto, register, static, extern, mutable. */
30533 if (declspecs.storage_class != sc_none)
30535 cp_parser_error (parser, "invalid type for property");
30536 declspecs.storage_class = sc_none;
30539 /* thread_local. */
30540 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30542 cp_parser_error (parser, "invalid type for property");
30543 declspecs.locations[ds_thread] = 0;
30546 /* typedef. */
30547 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30549 cp_parser_error (parser, "invalid type for property");
30550 declspecs.locations[ds_typedef] = 0;
30553 prefix_attributes = declspecs.attributes;
30554 declspecs.attributes = NULL_TREE;
30556 /* Keep going until we hit the `;' at the end of the declaration. */
30557 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30559 tree attributes, first_attribute, decl;
30560 cp_declarator *declarator;
30561 cp_token *token;
30563 /* Parse the declarator. */
30564 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30565 NULL, NULL, false, false);
30567 /* Look for attributes that apply to the ivar. */
30568 attributes = cp_parser_attributes_opt (parser);
30569 /* Remember which attributes are prefix attributes and
30570 which are not. */
30571 first_attribute = attributes;
30572 /* Combine the attributes. */
30573 attributes = chainon (prefix_attributes, attributes);
30575 decl = grokfield (declarator, &declspecs,
30576 NULL_TREE, /*init_const_expr_p=*/false,
30577 NULL_TREE, attributes);
30579 if (decl == error_mark_node || decl == NULL_TREE)
30580 return error_mark_node;
30582 /* Reset PREFIX_ATTRIBUTES. */
30583 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30584 attributes = TREE_CHAIN (attributes);
30585 if (attributes)
30586 TREE_CHAIN (attributes) = NULL_TREE;
30588 DECL_CHAIN (decl) = decls;
30589 decls = decl;
30591 token = cp_lexer_peek_token (parser->lexer);
30592 if (token->type == CPP_COMMA)
30594 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30595 continue;
30597 else
30598 break;
30600 return decls;
30603 /* Parse an Objective-C @property declaration. The syntax is:
30605 objc-property-declaration:
30606 '@property' objc-property-attributes[opt] struct-declaration ;
30608 objc-property-attributes:
30609 '(' objc-property-attribute-list ')'
30611 objc-property-attribute-list:
30612 objc-property-attribute
30613 objc-property-attribute-list, objc-property-attribute
30615 objc-property-attribute
30616 'getter' = identifier
30617 'setter' = identifier
30618 'readonly'
30619 'readwrite'
30620 'assign'
30621 'retain'
30622 'copy'
30623 'nonatomic'
30625 For example:
30626 @property NSString *name;
30627 @property (readonly) id object;
30628 @property (retain, nonatomic, getter=getTheName) id name;
30629 @property int a, b, c;
30631 PS: This function is identical to
30632 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30633 static void
30634 cp_parser_objc_at_property_declaration (cp_parser *parser)
30636 /* The following variables hold the attributes of the properties as
30637 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30638 seen. When we see an attribute, we set them to 'true' (if they
30639 are boolean properties) or to the identifier (if they have an
30640 argument, ie, for getter and setter). Note that here we only
30641 parse the list of attributes, check the syntax and accumulate the
30642 attributes that we find. objc_add_property_declaration() will
30643 then process the information. */
30644 bool property_assign = false;
30645 bool property_copy = false;
30646 tree property_getter_ident = NULL_TREE;
30647 bool property_nonatomic = false;
30648 bool property_readonly = false;
30649 bool property_readwrite = false;
30650 bool property_retain = false;
30651 tree property_setter_ident = NULL_TREE;
30653 /* 'properties' is the list of properties that we read. Usually a
30654 single one, but maybe more (eg, in "@property int a, b, c;" there
30655 are three). */
30656 tree properties;
30657 location_t loc;
30659 loc = cp_lexer_peek_token (parser->lexer)->location;
30661 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30663 /* Parse the optional attribute list... */
30664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30666 /* Eat the '('. */
30667 matching_parens parens;
30668 parens.consume_open (parser);
30670 while (true)
30672 bool syntax_error = false;
30673 cp_token *token = cp_lexer_peek_token (parser->lexer);
30674 enum rid keyword;
30676 if (token->type != CPP_NAME)
30678 cp_parser_error (parser, "expected identifier");
30679 break;
30681 keyword = C_RID_CODE (token->u.value);
30682 cp_lexer_consume_token (parser->lexer);
30683 switch (keyword)
30685 case RID_ASSIGN: property_assign = true; break;
30686 case RID_COPY: property_copy = true; break;
30687 case RID_NONATOMIC: property_nonatomic = true; break;
30688 case RID_READONLY: property_readonly = true; break;
30689 case RID_READWRITE: property_readwrite = true; break;
30690 case RID_RETAIN: property_retain = true; break;
30692 case RID_GETTER:
30693 case RID_SETTER:
30694 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30696 if (keyword == RID_GETTER)
30697 cp_parser_error (parser,
30698 "missing %<=%> (after %<getter%> attribute)");
30699 else
30700 cp_parser_error (parser,
30701 "missing %<=%> (after %<setter%> attribute)");
30702 syntax_error = true;
30703 break;
30705 cp_lexer_consume_token (parser->lexer); /* eat the = */
30706 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30708 cp_parser_error (parser, "expected identifier");
30709 syntax_error = true;
30710 break;
30712 if (keyword == RID_SETTER)
30714 if (property_setter_ident != NULL_TREE)
30716 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30717 cp_lexer_consume_token (parser->lexer);
30719 else
30720 property_setter_ident = cp_parser_objc_selector (parser);
30721 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30722 cp_parser_error (parser, "setter name must terminate with %<:%>");
30723 else
30724 cp_lexer_consume_token (parser->lexer);
30726 else
30728 if (property_getter_ident != NULL_TREE)
30730 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30731 cp_lexer_consume_token (parser->lexer);
30733 else
30734 property_getter_ident = cp_parser_objc_selector (parser);
30736 break;
30737 default:
30738 cp_parser_error (parser, "unknown property attribute");
30739 syntax_error = true;
30740 break;
30743 if (syntax_error)
30744 break;
30746 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30747 cp_lexer_consume_token (parser->lexer);
30748 else
30749 break;
30752 /* FIXME: "@property (setter, assign);" will generate a spurious
30753 "error: expected ‘)’ before ‘,’ token". This is because
30754 cp_parser_require, unlike the C counterpart, will produce an
30755 error even if we are in error recovery. */
30756 if (!parens.require_close (parser))
30758 cp_parser_skip_to_closing_parenthesis (parser,
30759 /*recovering=*/true,
30760 /*or_comma=*/false,
30761 /*consume_paren=*/true);
30765 /* ... and the property declaration(s). */
30766 properties = cp_parser_objc_struct_declaration (parser);
30768 if (properties == error_mark_node)
30770 cp_parser_skip_to_end_of_statement (parser);
30771 /* If the next token is now a `;', consume it. */
30772 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30773 cp_lexer_consume_token (parser->lexer);
30774 return;
30777 if (properties == NULL_TREE)
30778 cp_parser_error (parser, "expected identifier");
30779 else
30781 /* Comma-separated properties are chained together in
30782 reverse order; add them one by one. */
30783 properties = nreverse (properties);
30785 for (; properties; properties = TREE_CHAIN (properties))
30786 objc_add_property_declaration (loc, copy_node (properties),
30787 property_readonly, property_readwrite,
30788 property_assign, property_retain,
30789 property_copy, property_nonatomic,
30790 property_getter_ident, property_setter_ident);
30793 cp_parser_consume_semicolon_at_end_of_statement (parser);
30796 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30798 objc-synthesize-declaration:
30799 @synthesize objc-synthesize-identifier-list ;
30801 objc-synthesize-identifier-list:
30802 objc-synthesize-identifier
30803 objc-synthesize-identifier-list, objc-synthesize-identifier
30805 objc-synthesize-identifier
30806 identifier
30807 identifier = identifier
30809 For example:
30810 @synthesize MyProperty;
30811 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30813 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30814 for C. Keep them in sync.
30816 static void
30817 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30819 tree list = NULL_TREE;
30820 location_t loc;
30821 loc = cp_lexer_peek_token (parser->lexer)->location;
30823 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30824 while (true)
30826 tree property, ivar;
30827 property = cp_parser_identifier (parser);
30828 if (property == error_mark_node)
30830 cp_parser_consume_semicolon_at_end_of_statement (parser);
30831 return;
30833 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30835 cp_lexer_consume_token (parser->lexer);
30836 ivar = cp_parser_identifier (parser);
30837 if (ivar == error_mark_node)
30839 cp_parser_consume_semicolon_at_end_of_statement (parser);
30840 return;
30843 else
30844 ivar = NULL_TREE;
30845 list = chainon (list, build_tree_list (ivar, property));
30846 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30847 cp_lexer_consume_token (parser->lexer);
30848 else
30849 break;
30851 cp_parser_consume_semicolon_at_end_of_statement (parser);
30852 objc_add_synthesize_declaration (loc, list);
30855 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30857 objc-dynamic-declaration:
30858 @dynamic identifier-list ;
30860 For example:
30861 @dynamic MyProperty;
30862 @dynamic MyProperty, AnotherProperty;
30864 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30865 for C. Keep them in sync.
30867 static void
30868 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30870 tree list = NULL_TREE;
30871 location_t loc;
30872 loc = cp_lexer_peek_token (parser->lexer)->location;
30874 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30875 while (true)
30877 tree property;
30878 property = cp_parser_identifier (parser);
30879 if (property == error_mark_node)
30881 cp_parser_consume_semicolon_at_end_of_statement (parser);
30882 return;
30884 list = chainon (list, build_tree_list (NULL, property));
30885 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30886 cp_lexer_consume_token (parser->lexer);
30887 else
30888 break;
30890 cp_parser_consume_semicolon_at_end_of_statement (parser);
30891 objc_add_dynamic_declaration (loc, list);
30895 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30897 /* Returns name of the next clause.
30898 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30899 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30900 returned and the token is consumed. */
30902 static pragma_omp_clause
30903 cp_parser_omp_clause_name (cp_parser *parser)
30905 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30907 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30908 result = PRAGMA_OACC_CLAUSE_AUTO;
30909 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30910 result = PRAGMA_OMP_CLAUSE_IF;
30911 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30912 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30913 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30914 result = PRAGMA_OACC_CLAUSE_DELETE;
30915 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30916 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30917 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30918 result = PRAGMA_OMP_CLAUSE_FOR;
30919 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30921 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30922 const char *p = IDENTIFIER_POINTER (id);
30924 switch (p[0])
30926 case 'a':
30927 if (!strcmp ("aligned", p))
30928 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30929 else if (!strcmp ("async", p))
30930 result = PRAGMA_OACC_CLAUSE_ASYNC;
30931 break;
30932 case 'c':
30933 if (!strcmp ("collapse", p))
30934 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30935 else if (!strcmp ("copy", p))
30936 result = PRAGMA_OACC_CLAUSE_COPY;
30937 else if (!strcmp ("copyin", p))
30938 result = PRAGMA_OMP_CLAUSE_COPYIN;
30939 else if (!strcmp ("copyout", p))
30940 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30941 else if (!strcmp ("copyprivate", p))
30942 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30943 else if (!strcmp ("create", p))
30944 result = PRAGMA_OACC_CLAUSE_CREATE;
30945 break;
30946 case 'd':
30947 if (!strcmp ("defaultmap", p))
30948 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30949 else if (!strcmp ("depend", p))
30950 result = PRAGMA_OMP_CLAUSE_DEPEND;
30951 else if (!strcmp ("device", p))
30952 result = PRAGMA_OMP_CLAUSE_DEVICE;
30953 else if (!strcmp ("deviceptr", p))
30954 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30955 else if (!strcmp ("device_resident", p))
30956 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30957 else if (!strcmp ("dist_schedule", p))
30958 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30959 break;
30960 case 'f':
30961 if (!strcmp ("final", p))
30962 result = PRAGMA_OMP_CLAUSE_FINAL;
30963 else if (!strcmp ("firstprivate", p))
30964 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30965 else if (!strcmp ("from", p))
30966 result = PRAGMA_OMP_CLAUSE_FROM;
30967 break;
30968 case 'g':
30969 if (!strcmp ("gang", p))
30970 result = PRAGMA_OACC_CLAUSE_GANG;
30971 else if (!strcmp ("grainsize", p))
30972 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30973 break;
30974 case 'h':
30975 if (!strcmp ("hint", p))
30976 result = PRAGMA_OMP_CLAUSE_HINT;
30977 else if (!strcmp ("host", p))
30978 result = PRAGMA_OACC_CLAUSE_HOST;
30979 break;
30980 case 'i':
30981 if (!strcmp ("inbranch", p))
30982 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30983 else if (!strcmp ("independent", p))
30984 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30985 else if (!strcmp ("is_device_ptr", p))
30986 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30987 break;
30988 case 'l':
30989 if (!strcmp ("lastprivate", p))
30990 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30991 else if (!strcmp ("linear", p))
30992 result = PRAGMA_OMP_CLAUSE_LINEAR;
30993 else if (!strcmp ("link", p))
30994 result = PRAGMA_OMP_CLAUSE_LINK;
30995 break;
30996 case 'm':
30997 if (!strcmp ("map", p))
30998 result = PRAGMA_OMP_CLAUSE_MAP;
30999 else if (!strcmp ("mergeable", p))
31000 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31001 else if (flag_cilkplus && !strcmp ("mask", p))
31002 result = PRAGMA_CILK_CLAUSE_MASK;
31003 break;
31004 case 'n':
31005 if (!strcmp ("nogroup", p))
31006 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31007 else if (!strcmp ("notinbranch", p))
31008 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31009 else if (!strcmp ("nowait", p))
31010 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31011 else if (flag_cilkplus && !strcmp ("nomask", p))
31012 result = PRAGMA_CILK_CLAUSE_NOMASK;
31013 else if (!strcmp ("num_gangs", p))
31014 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31015 else if (!strcmp ("num_tasks", p))
31016 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31017 else if (!strcmp ("num_teams", p))
31018 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31019 else if (!strcmp ("num_threads", p))
31020 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31021 else if (!strcmp ("num_workers", p))
31022 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31023 break;
31024 case 'o':
31025 if (!strcmp ("ordered", p))
31026 result = PRAGMA_OMP_CLAUSE_ORDERED;
31027 break;
31028 case 'p':
31029 if (!strcmp ("parallel", p))
31030 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31031 else if (!strcmp ("present", p))
31032 result = PRAGMA_OACC_CLAUSE_PRESENT;
31033 else if (!strcmp ("present_or_copy", p)
31034 || !strcmp ("pcopy", p))
31035 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31036 else if (!strcmp ("present_or_copyin", p)
31037 || !strcmp ("pcopyin", p))
31038 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31039 else if (!strcmp ("present_or_copyout", p)
31040 || !strcmp ("pcopyout", p))
31041 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31042 else if (!strcmp ("present_or_create", p)
31043 || !strcmp ("pcreate", p))
31044 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31045 else if (!strcmp ("priority", p))
31046 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31047 else if (!strcmp ("proc_bind", p))
31048 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31049 break;
31050 case 'r':
31051 if (!strcmp ("reduction", p))
31052 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31053 break;
31054 case 's':
31055 if (!strcmp ("safelen", p))
31056 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31057 else if (!strcmp ("schedule", p))
31058 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31059 else if (!strcmp ("sections", p))
31060 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31061 else if (!strcmp ("self", p))
31062 result = PRAGMA_OACC_CLAUSE_SELF;
31063 else if (!strcmp ("seq", p))
31064 result = PRAGMA_OACC_CLAUSE_SEQ;
31065 else if (!strcmp ("shared", p))
31066 result = PRAGMA_OMP_CLAUSE_SHARED;
31067 else if (!strcmp ("simd", p))
31068 result = PRAGMA_OMP_CLAUSE_SIMD;
31069 else if (!strcmp ("simdlen", p))
31070 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31071 break;
31072 case 't':
31073 if (!strcmp ("taskgroup", p))
31074 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31075 else if (!strcmp ("thread_limit", p))
31076 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31077 else if (!strcmp ("threads", p))
31078 result = PRAGMA_OMP_CLAUSE_THREADS;
31079 else if (!strcmp ("tile", p))
31080 result = PRAGMA_OACC_CLAUSE_TILE;
31081 else if (!strcmp ("to", p))
31082 result = PRAGMA_OMP_CLAUSE_TO;
31083 break;
31084 case 'u':
31085 if (!strcmp ("uniform", p))
31086 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31087 else if (!strcmp ("untied", p))
31088 result = PRAGMA_OMP_CLAUSE_UNTIED;
31089 else if (!strcmp ("use_device", p))
31090 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31091 else if (!strcmp ("use_device_ptr", p))
31092 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31093 break;
31094 case 'v':
31095 if (!strcmp ("vector", p))
31096 result = PRAGMA_OACC_CLAUSE_VECTOR;
31097 else if (!strcmp ("vector_length", p))
31098 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31099 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31100 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31101 break;
31102 case 'w':
31103 if (!strcmp ("wait", p))
31104 result = PRAGMA_OACC_CLAUSE_WAIT;
31105 else if (!strcmp ("worker", p))
31106 result = PRAGMA_OACC_CLAUSE_WORKER;
31107 break;
31111 if (result != PRAGMA_OMP_CLAUSE_NONE)
31112 cp_lexer_consume_token (parser->lexer);
31114 return result;
31117 /* Validate that a clause of the given type does not already exist. */
31119 static void
31120 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31121 const char *name, location_t location)
31123 tree c;
31125 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31126 if (OMP_CLAUSE_CODE (c) == code)
31128 error_at (location, "too many %qs clauses", name);
31129 break;
31133 /* OpenMP 2.5:
31134 variable-list:
31135 identifier
31136 variable-list , identifier
31138 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31139 colon). An opening parenthesis will have been consumed by the caller.
31141 If KIND is nonzero, create the appropriate node and install the decl
31142 in OMP_CLAUSE_DECL and add the node to the head of the list.
31144 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31145 return the list created.
31147 COLON can be NULL if only closing parenthesis should end the list,
31148 or pointer to bool which will receive false if the list is terminated
31149 by closing parenthesis or true if the list is terminated by colon. */
31151 static tree
31152 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31153 tree list, bool *colon)
31155 cp_token *token;
31156 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31157 if (colon)
31159 parser->colon_corrects_to_scope_p = false;
31160 *colon = false;
31162 while (1)
31164 tree name, decl;
31166 token = cp_lexer_peek_token (parser->lexer);
31167 if (kind != 0
31168 && current_class_ptr
31169 && cp_parser_is_keyword (token, RID_THIS))
31171 decl = finish_this_expr ();
31172 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31173 || CONVERT_EXPR_P (decl))
31174 decl = TREE_OPERAND (decl, 0);
31175 cp_lexer_consume_token (parser->lexer);
31177 else
31179 name = cp_parser_id_expression (parser, /*template_p=*/false,
31180 /*check_dependency_p=*/true,
31181 /*template_p=*/NULL,
31182 /*declarator_p=*/false,
31183 /*optional_p=*/false);
31184 if (name == error_mark_node)
31185 goto skip_comma;
31187 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31188 if (decl == error_mark_node)
31189 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31190 token->location);
31192 if (decl == error_mark_node)
31194 else if (kind != 0)
31196 switch (kind)
31198 case OMP_CLAUSE__CACHE_:
31199 /* The OpenACC cache directive explicitly only allows "array
31200 elements or subarrays". */
31201 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31203 error_at (token->location, "expected %<[%>");
31204 decl = error_mark_node;
31205 break;
31207 /* FALLTHROUGH. */
31208 case OMP_CLAUSE_MAP:
31209 case OMP_CLAUSE_FROM:
31210 case OMP_CLAUSE_TO:
31211 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31213 location_t loc
31214 = cp_lexer_peek_token (parser->lexer)->location;
31215 cp_id_kind idk = CP_ID_KIND_NONE;
31216 cp_lexer_consume_token (parser->lexer);
31217 decl = convert_from_reference (decl);
31218 decl
31219 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31220 decl, false,
31221 &idk, loc);
31223 /* FALLTHROUGH. */
31224 case OMP_CLAUSE_DEPEND:
31225 case OMP_CLAUSE_REDUCTION:
31226 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31228 tree low_bound = NULL_TREE, length = NULL_TREE;
31230 parser->colon_corrects_to_scope_p = false;
31231 cp_lexer_consume_token (parser->lexer);
31232 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31233 low_bound = cp_parser_expression (parser);
31234 if (!colon)
31235 parser->colon_corrects_to_scope_p
31236 = saved_colon_corrects_to_scope_p;
31237 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31238 length = integer_one_node;
31239 else
31241 /* Look for `:'. */
31242 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31243 goto skip_comma;
31244 if (!cp_lexer_next_token_is (parser->lexer,
31245 CPP_CLOSE_SQUARE))
31246 length = cp_parser_expression (parser);
31248 /* Look for the closing `]'. */
31249 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31250 RT_CLOSE_SQUARE))
31251 goto skip_comma;
31253 decl = tree_cons (low_bound, length, decl);
31255 break;
31256 default:
31257 break;
31260 tree u = build_omp_clause (token->location, kind);
31261 OMP_CLAUSE_DECL (u) = decl;
31262 OMP_CLAUSE_CHAIN (u) = list;
31263 list = u;
31265 else
31266 list = tree_cons (decl, NULL_TREE, list);
31268 get_comma:
31269 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31270 break;
31271 cp_lexer_consume_token (parser->lexer);
31274 if (colon)
31275 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31277 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31279 *colon = true;
31280 cp_parser_require (parser, CPP_COLON, RT_COLON);
31281 return list;
31284 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31286 int ending;
31288 /* Try to resync to an unnested comma. Copied from
31289 cp_parser_parenthesized_expression_list. */
31290 skip_comma:
31291 if (colon)
31292 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31293 ending = cp_parser_skip_to_closing_parenthesis (parser,
31294 /*recovering=*/true,
31295 /*or_comma=*/true,
31296 /*consume_paren=*/true);
31297 if (ending < 0)
31298 goto get_comma;
31301 return list;
31304 /* Similarly, but expect leading and trailing parenthesis. This is a very
31305 common case for omp clauses. */
31307 static tree
31308 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31310 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31311 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31312 return list;
31315 /* OpenACC 2.0:
31316 copy ( variable-list )
31317 copyin ( variable-list )
31318 copyout ( variable-list )
31319 create ( variable-list )
31320 delete ( variable-list )
31321 present ( variable-list )
31322 present_or_copy ( variable-list )
31323 pcopy ( variable-list )
31324 present_or_copyin ( variable-list )
31325 pcopyin ( variable-list )
31326 present_or_copyout ( variable-list )
31327 pcopyout ( variable-list )
31328 present_or_create ( variable-list )
31329 pcreate ( variable-list ) */
31331 static tree
31332 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31333 tree list)
31335 enum gomp_map_kind kind;
31336 switch (c_kind)
31338 case PRAGMA_OACC_CLAUSE_COPY:
31339 kind = GOMP_MAP_FORCE_TOFROM;
31340 break;
31341 case PRAGMA_OACC_CLAUSE_COPYIN:
31342 kind = GOMP_MAP_FORCE_TO;
31343 break;
31344 case PRAGMA_OACC_CLAUSE_COPYOUT:
31345 kind = GOMP_MAP_FORCE_FROM;
31346 break;
31347 case PRAGMA_OACC_CLAUSE_CREATE:
31348 kind = GOMP_MAP_FORCE_ALLOC;
31349 break;
31350 case PRAGMA_OACC_CLAUSE_DELETE:
31351 kind = GOMP_MAP_DELETE;
31352 break;
31353 case PRAGMA_OACC_CLAUSE_DEVICE:
31354 kind = GOMP_MAP_FORCE_TO;
31355 break;
31356 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31357 kind = GOMP_MAP_DEVICE_RESIDENT;
31358 break;
31359 case PRAGMA_OACC_CLAUSE_HOST:
31360 case PRAGMA_OACC_CLAUSE_SELF:
31361 kind = GOMP_MAP_FORCE_FROM;
31362 break;
31363 case PRAGMA_OACC_CLAUSE_LINK:
31364 kind = GOMP_MAP_LINK;
31365 break;
31366 case PRAGMA_OACC_CLAUSE_PRESENT:
31367 kind = GOMP_MAP_FORCE_PRESENT;
31368 break;
31369 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31370 kind = GOMP_MAP_TOFROM;
31371 break;
31372 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31373 kind = GOMP_MAP_TO;
31374 break;
31375 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31376 kind = GOMP_MAP_FROM;
31377 break;
31378 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31379 kind = GOMP_MAP_ALLOC;
31380 break;
31381 default:
31382 gcc_unreachable ();
31384 tree nl, c;
31385 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31387 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31388 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31390 return nl;
31393 /* OpenACC 2.0:
31394 deviceptr ( variable-list ) */
31396 static tree
31397 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31399 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31400 tree vars, t;
31402 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31403 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31404 variable-list must only allow for pointer variables. */
31405 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31406 for (t = vars; t; t = TREE_CHAIN (t))
31408 tree v = TREE_PURPOSE (t);
31409 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31410 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31411 OMP_CLAUSE_DECL (u) = v;
31412 OMP_CLAUSE_CHAIN (u) = list;
31413 list = u;
31416 return list;
31419 /* OpenACC 2.0:
31420 auto
31421 independent
31422 nohost
31423 seq */
31425 static tree
31426 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31427 enum omp_clause_code code,
31428 tree list, location_t location)
31430 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31431 tree c = build_omp_clause (location, code);
31432 OMP_CLAUSE_CHAIN (c) = list;
31433 return c;
31436 /* OpenACC:
31437 num_gangs ( expression )
31438 num_workers ( expression )
31439 vector_length ( expression ) */
31441 static tree
31442 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31443 const char *str, tree list)
31445 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31447 matching_parens parens;
31448 if (!parens.require_open (parser))
31449 return list;
31451 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31453 if (t == error_mark_node
31454 || !parens.require_close (parser))
31456 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31457 /*or_comma=*/false,
31458 /*consume_paren=*/true);
31459 return list;
31462 check_no_duplicate_clause (list, code, str, loc);
31464 tree c = build_omp_clause (loc, code);
31465 OMP_CLAUSE_OPERAND (c, 0) = t;
31466 OMP_CLAUSE_CHAIN (c) = list;
31467 return c;
31470 /* OpenACC:
31472 gang [( gang-arg-list )]
31473 worker [( [num:] int-expr )]
31474 vector [( [length:] int-expr )]
31476 where gang-arg is one of:
31478 [num:] int-expr
31479 static: size-expr
31481 and size-expr may be:
31484 int-expr
31487 static tree
31488 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31489 const char *str, tree list)
31491 const char *id = "num";
31492 cp_lexer *lexer = parser->lexer;
31493 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31494 location_t loc = cp_lexer_peek_token (lexer)->location;
31496 if (kind == OMP_CLAUSE_VECTOR)
31497 id = "length";
31499 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31501 matching_parens parens;
31502 parens.consume_open (parser);
31506 cp_token *next = cp_lexer_peek_token (lexer);
31507 int idx = 0;
31509 /* Gang static argument. */
31510 if (kind == OMP_CLAUSE_GANG
31511 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31513 cp_lexer_consume_token (lexer);
31515 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31516 goto cleanup_error;
31518 idx = 1;
31519 if (ops[idx] != NULL)
31521 cp_parser_error (parser, "too many %<static%> arguments");
31522 goto cleanup_error;
31525 /* Check for the '*' argument. */
31526 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31527 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31528 || cp_lexer_nth_token_is (parser->lexer, 2,
31529 CPP_CLOSE_PAREN)))
31531 cp_lexer_consume_token (lexer);
31532 ops[idx] = integer_minus_one_node;
31534 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31536 cp_lexer_consume_token (lexer);
31537 continue;
31539 else break;
31542 /* Worker num: argument and vector length: arguments. */
31543 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31544 && id_equal (next->u.value, id)
31545 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31547 cp_lexer_consume_token (lexer); /* id */
31548 cp_lexer_consume_token (lexer); /* ':' */
31551 /* Now collect the actual argument. */
31552 if (ops[idx] != NULL_TREE)
31554 cp_parser_error (parser, "unexpected argument");
31555 goto cleanup_error;
31558 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31559 false);
31560 if (expr == error_mark_node)
31561 goto cleanup_error;
31563 mark_exp_read (expr);
31564 ops[idx] = expr;
31566 if (kind == OMP_CLAUSE_GANG
31567 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31569 cp_lexer_consume_token (lexer);
31570 continue;
31572 break;
31574 while (1);
31576 if (!parens.require_close (parser))
31577 goto cleanup_error;
31580 check_no_duplicate_clause (list, kind, str, loc);
31582 c = build_omp_clause (loc, kind);
31584 if (ops[1])
31585 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31587 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31588 OMP_CLAUSE_CHAIN (c) = list;
31590 return c;
31592 cleanup_error:
31593 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31594 return list;
31597 /* OpenACC 2.0:
31598 tile ( size-expr-list ) */
31600 static tree
31601 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31603 tree c, expr = error_mark_node;
31604 tree tile = NULL_TREE;
31606 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31607 so, but the spec authors never considered such a case and have
31608 differing opinions on what it might mean, including 'not
31609 allowed'.) */
31610 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31611 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31612 clause_loc);
31614 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31615 return list;
31619 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31620 return list;
31622 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31623 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31624 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31626 cp_lexer_consume_token (parser->lexer);
31627 expr = integer_zero_node;
31629 else
31630 expr = cp_parser_constant_expression (parser);
31632 tile = tree_cons (NULL_TREE, expr, tile);
31634 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31636 /* Consume the trailing ')'. */
31637 cp_lexer_consume_token (parser->lexer);
31639 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31640 tile = nreverse (tile);
31641 OMP_CLAUSE_TILE_LIST (c) = tile;
31642 OMP_CLAUSE_CHAIN (c) = list;
31643 return c;
31646 /* OpenACC 2.0
31647 Parse wait clause or directive parameters. */
31649 static tree
31650 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31652 vec<tree, va_gc> *args;
31653 tree t, args_tree;
31655 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31656 /*cast_p=*/false,
31657 /*allow_expansion_p=*/true,
31658 /*non_constant_p=*/NULL);
31660 if (args == NULL || args->length () == 0)
31662 cp_parser_error (parser, "expected integer expression before ')'");
31663 if (args != NULL)
31664 release_tree_vector (args);
31665 return list;
31668 args_tree = build_tree_list_vec (args);
31670 release_tree_vector (args);
31672 for (t = args_tree; t; t = TREE_CHAIN (t))
31674 tree targ = TREE_VALUE (t);
31676 if (targ != error_mark_node)
31678 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31679 error ("%<wait%> expression must be integral");
31680 else
31682 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31684 mark_rvalue_use (targ);
31685 OMP_CLAUSE_DECL (c) = targ;
31686 OMP_CLAUSE_CHAIN (c) = list;
31687 list = c;
31692 return list;
31695 /* OpenACC:
31696 wait ( int-expr-list ) */
31698 static tree
31699 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31701 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31703 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31704 return list;
31706 list = cp_parser_oacc_wait_list (parser, location, list);
31708 return list;
31711 /* OpenMP 3.0:
31712 collapse ( constant-expression ) */
31714 static tree
31715 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31717 tree c, num;
31718 location_t loc;
31719 HOST_WIDE_INT n;
31721 loc = cp_lexer_peek_token (parser->lexer)->location;
31722 matching_parens parens;
31723 if (!parens.require_open (parser))
31724 return list;
31726 num = cp_parser_constant_expression (parser);
31728 if (!parens.require_close (parser))
31729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31730 /*or_comma=*/false,
31731 /*consume_paren=*/true);
31733 if (num == error_mark_node)
31734 return list;
31735 num = fold_non_dependent_expr (num);
31736 if (!tree_fits_shwi_p (num)
31737 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31738 || (n = tree_to_shwi (num)) <= 0
31739 || (int) n != n)
31741 error_at (loc, "collapse argument needs positive constant integer expression");
31742 return list;
31745 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31746 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31747 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31748 OMP_CLAUSE_CHAIN (c) = list;
31749 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31751 return c;
31754 /* OpenMP 2.5:
31755 default ( none | shared )
31757 OpenACC:
31758 default ( none | present ) */
31760 static tree
31761 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31762 location_t location, bool is_oacc)
31764 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31765 tree c;
31767 matching_parens parens;
31768 if (!parens.require_open (parser))
31769 return list;
31770 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31772 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31773 const char *p = IDENTIFIER_POINTER (id);
31775 switch (p[0])
31777 case 'n':
31778 if (strcmp ("none", p) != 0)
31779 goto invalid_kind;
31780 kind = OMP_CLAUSE_DEFAULT_NONE;
31781 break;
31783 case 'p':
31784 if (strcmp ("present", p) != 0 || !is_oacc)
31785 goto invalid_kind;
31786 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31787 break;
31789 case 's':
31790 if (strcmp ("shared", p) != 0 || is_oacc)
31791 goto invalid_kind;
31792 kind = OMP_CLAUSE_DEFAULT_SHARED;
31793 break;
31795 default:
31796 goto invalid_kind;
31799 cp_lexer_consume_token (parser->lexer);
31801 else
31803 invalid_kind:
31804 if (is_oacc)
31805 cp_parser_error (parser, "expected %<none%> or %<present%>");
31806 else
31807 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31810 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31811 || !parens.require_close (parser))
31812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31813 /*or_comma=*/false,
31814 /*consume_paren=*/true);
31816 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31817 return list;
31819 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31820 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31821 OMP_CLAUSE_CHAIN (c) = list;
31822 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31824 return c;
31827 /* OpenMP 3.1:
31828 final ( expression ) */
31830 static tree
31831 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31833 tree t, c;
31835 matching_parens parens;
31836 if (!parens.require_open (parser))
31837 return list;
31839 t = cp_parser_condition (parser);
31841 if (t == error_mark_node
31842 || !parens.require_close (parser))
31843 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31844 /*or_comma=*/false,
31845 /*consume_paren=*/true);
31847 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31849 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31850 OMP_CLAUSE_FINAL_EXPR (c) = t;
31851 OMP_CLAUSE_CHAIN (c) = list;
31853 return c;
31856 /* OpenMP 2.5:
31857 if ( expression )
31859 OpenMP 4.5:
31860 if ( directive-name-modifier : expression )
31862 directive-name-modifier:
31863 parallel | task | taskloop | target data | target | target update
31864 | target enter data | target exit data */
31866 static tree
31867 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31868 bool is_omp)
31870 tree t, c;
31871 enum tree_code if_modifier = ERROR_MARK;
31873 matching_parens parens;
31874 if (!parens.require_open (parser))
31875 return list;
31877 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31879 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31880 const char *p = IDENTIFIER_POINTER (id);
31881 int n = 2;
31883 if (strcmp ("parallel", p) == 0)
31884 if_modifier = OMP_PARALLEL;
31885 else if (strcmp ("task", p) == 0)
31886 if_modifier = OMP_TASK;
31887 else if (strcmp ("taskloop", p) == 0)
31888 if_modifier = OMP_TASKLOOP;
31889 else if (strcmp ("target", p) == 0)
31891 if_modifier = OMP_TARGET;
31892 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31894 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31895 p = IDENTIFIER_POINTER (id);
31896 if (strcmp ("data", p) == 0)
31897 if_modifier = OMP_TARGET_DATA;
31898 else if (strcmp ("update", p) == 0)
31899 if_modifier = OMP_TARGET_UPDATE;
31900 else if (strcmp ("enter", p) == 0)
31901 if_modifier = OMP_TARGET_ENTER_DATA;
31902 else if (strcmp ("exit", p) == 0)
31903 if_modifier = OMP_TARGET_EXIT_DATA;
31904 if (if_modifier != OMP_TARGET)
31905 n = 3;
31906 else
31908 location_t loc
31909 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31910 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31911 "or %<exit%>");
31912 if_modifier = ERROR_MARK;
31914 if (if_modifier == OMP_TARGET_ENTER_DATA
31915 || if_modifier == OMP_TARGET_EXIT_DATA)
31917 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31919 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31920 p = IDENTIFIER_POINTER (id);
31921 if (strcmp ("data", p) == 0)
31922 n = 4;
31924 if (n != 4)
31926 location_t loc
31927 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31928 error_at (loc, "expected %<data%>");
31929 if_modifier = ERROR_MARK;
31934 if (if_modifier != ERROR_MARK)
31936 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31938 while (n-- > 0)
31939 cp_lexer_consume_token (parser->lexer);
31941 else
31943 if (n > 2)
31945 location_t loc
31946 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31947 error_at (loc, "expected %<:%>");
31949 if_modifier = ERROR_MARK;
31954 t = cp_parser_condition (parser);
31956 if (t == error_mark_node
31957 || !parens.require_close (parser))
31958 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31959 /*or_comma=*/false,
31960 /*consume_paren=*/true);
31962 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31963 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31965 if (if_modifier != ERROR_MARK
31966 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31968 const char *p = NULL;
31969 switch (if_modifier)
31971 case OMP_PARALLEL: p = "parallel"; break;
31972 case OMP_TASK: p = "task"; break;
31973 case OMP_TASKLOOP: p = "taskloop"; break;
31974 case OMP_TARGET_DATA: p = "target data"; break;
31975 case OMP_TARGET: p = "target"; break;
31976 case OMP_TARGET_UPDATE: p = "target update"; break;
31977 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31978 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31979 default: gcc_unreachable ();
31981 error_at (location, "too many %<if%> clauses with %qs modifier",
31983 return list;
31985 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31987 if (!is_omp)
31988 error_at (location, "too many %<if%> clauses");
31989 else
31990 error_at (location, "too many %<if%> clauses without modifier");
31991 return list;
31993 else if (if_modifier == ERROR_MARK
31994 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31996 error_at (location, "if any %<if%> clause has modifier, then all "
31997 "%<if%> clauses have to use modifier");
31998 return list;
32002 c = build_omp_clause (location, OMP_CLAUSE_IF);
32003 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32004 OMP_CLAUSE_IF_EXPR (c) = t;
32005 OMP_CLAUSE_CHAIN (c) = list;
32007 return c;
32010 /* OpenMP 3.1:
32011 mergeable */
32013 static tree
32014 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32015 tree list, location_t location)
32017 tree c;
32019 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32020 location);
32022 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32023 OMP_CLAUSE_CHAIN (c) = list;
32024 return c;
32027 /* OpenMP 2.5:
32028 nowait */
32030 static tree
32031 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32032 tree list, location_t location)
32034 tree c;
32036 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32038 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32039 OMP_CLAUSE_CHAIN (c) = list;
32040 return c;
32043 /* OpenMP 2.5:
32044 num_threads ( expression ) */
32046 static tree
32047 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32048 location_t location)
32050 tree t, c;
32052 matching_parens parens;
32053 if (!parens.require_open (parser))
32054 return list;
32056 t = cp_parser_expression (parser);
32058 if (t == error_mark_node
32059 || !parens.require_close (parser))
32060 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32061 /*or_comma=*/false,
32062 /*consume_paren=*/true);
32064 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32065 "num_threads", location);
32067 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32068 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32069 OMP_CLAUSE_CHAIN (c) = list;
32071 return c;
32074 /* OpenMP 4.5:
32075 num_tasks ( expression ) */
32077 static tree
32078 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32079 location_t location)
32081 tree t, c;
32083 matching_parens parens;
32084 if (!parens.require_open (parser))
32085 return list;
32087 t = cp_parser_expression (parser);
32089 if (t == error_mark_node
32090 || !parens.require_close (parser))
32091 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32092 /*or_comma=*/false,
32093 /*consume_paren=*/true);
32095 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32096 "num_tasks", location);
32098 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32099 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32100 OMP_CLAUSE_CHAIN (c) = list;
32102 return c;
32105 /* OpenMP 4.5:
32106 grainsize ( expression ) */
32108 static tree
32109 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32110 location_t location)
32112 tree t, c;
32114 matching_parens parens;
32115 if (!parens.require_open (parser))
32116 return list;
32118 t = cp_parser_expression (parser);
32120 if (t == error_mark_node
32121 || !parens.require_close (parser))
32122 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32123 /*or_comma=*/false,
32124 /*consume_paren=*/true);
32126 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32127 "grainsize", location);
32129 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32130 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32131 OMP_CLAUSE_CHAIN (c) = list;
32133 return c;
32136 /* OpenMP 4.5:
32137 priority ( expression ) */
32139 static tree
32140 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32141 location_t location)
32143 tree t, c;
32145 matching_parens parens;
32146 if (!parens.require_open (parser))
32147 return list;
32149 t = cp_parser_expression (parser);
32151 if (t == error_mark_node
32152 || !parens.require_close (parser))
32153 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32154 /*or_comma=*/false,
32155 /*consume_paren=*/true);
32157 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32158 "priority", location);
32160 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32161 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32162 OMP_CLAUSE_CHAIN (c) = list;
32164 return c;
32167 /* OpenMP 4.5:
32168 hint ( expression ) */
32170 static tree
32171 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32172 location_t location)
32174 tree t, c;
32176 matching_parens parens;
32177 if (!parens.require_open (parser))
32178 return list;
32180 t = cp_parser_expression (parser);
32182 if (t == error_mark_node
32183 || !parens.require_close (parser))
32184 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32185 /*or_comma=*/false,
32186 /*consume_paren=*/true);
32188 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32190 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32191 OMP_CLAUSE_HINT_EXPR (c) = t;
32192 OMP_CLAUSE_CHAIN (c) = list;
32194 return c;
32197 /* OpenMP 4.5:
32198 defaultmap ( tofrom : scalar ) */
32200 static tree
32201 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32202 location_t location)
32204 tree c, id;
32205 const char *p;
32207 matching_parens parens;
32208 if (!parens.require_open (parser))
32209 return list;
32211 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32213 cp_parser_error (parser, "expected %<tofrom%>");
32214 goto out_err;
32216 id = cp_lexer_peek_token (parser->lexer)->u.value;
32217 p = IDENTIFIER_POINTER (id);
32218 if (strcmp (p, "tofrom") != 0)
32220 cp_parser_error (parser, "expected %<tofrom%>");
32221 goto out_err;
32223 cp_lexer_consume_token (parser->lexer);
32224 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32225 goto out_err;
32227 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32229 cp_parser_error (parser, "expected %<scalar%>");
32230 goto out_err;
32232 id = cp_lexer_peek_token (parser->lexer)->u.value;
32233 p = IDENTIFIER_POINTER (id);
32234 if (strcmp (p, "scalar") != 0)
32236 cp_parser_error (parser, "expected %<scalar%>");
32237 goto out_err;
32239 cp_lexer_consume_token (parser->lexer);
32240 if (!parens.require_close (parser))
32241 goto out_err;
32243 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32244 location);
32246 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32247 OMP_CLAUSE_CHAIN (c) = list;
32248 return c;
32250 out_err:
32251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32252 /*or_comma=*/false,
32253 /*consume_paren=*/true);
32254 return list;
32257 /* OpenMP 2.5:
32258 ordered
32260 OpenMP 4.5:
32261 ordered ( constant-expression ) */
32263 static tree
32264 cp_parser_omp_clause_ordered (cp_parser *parser,
32265 tree list, location_t location)
32267 tree c, num = NULL_TREE;
32268 HOST_WIDE_INT n;
32270 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32271 "ordered", location);
32273 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32275 matching_parens parens;
32276 parens.consume_open (parser);
32278 num = cp_parser_constant_expression (parser);
32280 if (!parens.require_close (parser))
32281 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32282 /*or_comma=*/false,
32283 /*consume_paren=*/true);
32285 if (num == error_mark_node)
32286 return list;
32287 num = fold_non_dependent_expr (num);
32288 if (!tree_fits_shwi_p (num)
32289 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32290 || (n = tree_to_shwi (num)) <= 0
32291 || (int) n != n)
32293 error_at (location,
32294 "ordered argument needs positive constant integer "
32295 "expression");
32296 return list;
32300 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32301 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32302 OMP_CLAUSE_CHAIN (c) = list;
32303 return c;
32306 /* OpenMP 2.5:
32307 reduction ( reduction-operator : variable-list )
32309 reduction-operator:
32310 One of: + * - & ^ | && ||
32312 OpenMP 3.1:
32314 reduction-operator:
32315 One of: + * - & ^ | && || min max
32317 OpenMP 4.0:
32319 reduction-operator:
32320 One of: + * - & ^ | && ||
32321 id-expression */
32323 static tree
32324 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32326 enum tree_code code = ERROR_MARK;
32327 tree nlist, c, id = NULL_TREE;
32329 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32330 return list;
32332 switch (cp_lexer_peek_token (parser->lexer)->type)
32334 case CPP_PLUS: code = PLUS_EXPR; break;
32335 case CPP_MULT: code = MULT_EXPR; break;
32336 case CPP_MINUS: code = MINUS_EXPR; break;
32337 case CPP_AND: code = BIT_AND_EXPR; break;
32338 case CPP_XOR: code = BIT_XOR_EXPR; break;
32339 case CPP_OR: code = BIT_IOR_EXPR; break;
32340 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32341 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32342 default: break;
32345 if (code != ERROR_MARK)
32346 cp_lexer_consume_token (parser->lexer);
32347 else
32349 bool saved_colon_corrects_to_scope_p;
32350 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32351 parser->colon_corrects_to_scope_p = false;
32352 id = cp_parser_id_expression (parser, /*template_p=*/false,
32353 /*check_dependency_p=*/true,
32354 /*template_p=*/NULL,
32355 /*declarator_p=*/false,
32356 /*optional_p=*/false);
32357 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32358 if (identifier_p (id))
32360 const char *p = IDENTIFIER_POINTER (id);
32362 if (strcmp (p, "min") == 0)
32363 code = MIN_EXPR;
32364 else if (strcmp (p, "max") == 0)
32365 code = MAX_EXPR;
32366 else if (id == cp_operator_id (PLUS_EXPR))
32367 code = PLUS_EXPR;
32368 else if (id == cp_operator_id (MULT_EXPR))
32369 code = MULT_EXPR;
32370 else if (id == cp_operator_id (MINUS_EXPR))
32371 code = MINUS_EXPR;
32372 else if (id == cp_operator_id (BIT_AND_EXPR))
32373 code = BIT_AND_EXPR;
32374 else if (id == cp_operator_id (BIT_IOR_EXPR))
32375 code = BIT_IOR_EXPR;
32376 else if (id == cp_operator_id (BIT_XOR_EXPR))
32377 code = BIT_XOR_EXPR;
32378 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32379 code = TRUTH_ANDIF_EXPR;
32380 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32381 code = TRUTH_ORIF_EXPR;
32382 id = omp_reduction_id (code, id, NULL_TREE);
32383 tree scope = parser->scope;
32384 if (scope)
32385 id = build_qualified_name (NULL_TREE, scope, id, false);
32386 parser->scope = NULL_TREE;
32387 parser->qualifying_scope = NULL_TREE;
32388 parser->object_scope = NULL_TREE;
32390 else
32392 error ("invalid reduction-identifier");
32393 resync_fail:
32394 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32395 /*or_comma=*/false,
32396 /*consume_paren=*/true);
32397 return list;
32401 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32402 goto resync_fail;
32404 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32405 NULL);
32406 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32408 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32409 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32412 return nlist;
32415 /* OpenMP 2.5:
32416 schedule ( schedule-kind )
32417 schedule ( schedule-kind , expression )
32419 schedule-kind:
32420 static | dynamic | guided | runtime | auto
32422 OpenMP 4.5:
32423 schedule ( schedule-modifier : schedule-kind )
32424 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32426 schedule-modifier:
32427 simd
32428 monotonic
32429 nonmonotonic */
32431 static tree
32432 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32434 tree c, t;
32435 int modifiers = 0, nmodifiers = 0;
32437 matching_parens parens;
32438 if (!parens.require_open (parser))
32439 return list;
32441 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32443 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32445 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32446 const char *p = IDENTIFIER_POINTER (id);
32447 if (strcmp ("simd", p) == 0)
32448 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32449 else if (strcmp ("monotonic", p) == 0)
32450 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32451 else if (strcmp ("nonmonotonic", p) == 0)
32452 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32453 else
32454 break;
32455 cp_lexer_consume_token (parser->lexer);
32456 if (nmodifiers++ == 0
32457 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32458 cp_lexer_consume_token (parser->lexer);
32459 else
32461 cp_parser_require (parser, CPP_COLON, RT_COLON);
32462 break;
32466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32468 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32469 const char *p = IDENTIFIER_POINTER (id);
32471 switch (p[0])
32473 case 'd':
32474 if (strcmp ("dynamic", p) != 0)
32475 goto invalid_kind;
32476 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32477 break;
32479 case 'g':
32480 if (strcmp ("guided", p) != 0)
32481 goto invalid_kind;
32482 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32483 break;
32485 case 'r':
32486 if (strcmp ("runtime", p) != 0)
32487 goto invalid_kind;
32488 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32489 break;
32491 default:
32492 goto invalid_kind;
32495 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32496 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32497 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32498 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32499 else
32500 goto invalid_kind;
32501 cp_lexer_consume_token (parser->lexer);
32503 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32504 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32505 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32506 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32508 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32509 "specified");
32510 modifiers = 0;
32513 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32515 cp_token *token;
32516 cp_lexer_consume_token (parser->lexer);
32518 token = cp_lexer_peek_token (parser->lexer);
32519 t = cp_parser_assignment_expression (parser);
32521 if (t == error_mark_node)
32522 goto resync_fail;
32523 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32524 error_at (token->location, "schedule %<runtime%> does not take "
32525 "a %<chunk_size%> parameter");
32526 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32527 error_at (token->location, "schedule %<auto%> does not take "
32528 "a %<chunk_size%> parameter");
32529 else
32530 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32532 if (!parens.require_close (parser))
32533 goto resync_fail;
32535 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32536 goto resync_fail;
32538 OMP_CLAUSE_SCHEDULE_KIND (c)
32539 = (enum omp_clause_schedule_kind)
32540 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32542 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32543 OMP_CLAUSE_CHAIN (c) = list;
32544 return c;
32546 invalid_kind:
32547 cp_parser_error (parser, "invalid schedule kind");
32548 resync_fail:
32549 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32550 /*or_comma=*/false,
32551 /*consume_paren=*/true);
32552 return list;
32555 /* OpenMP 3.0:
32556 untied */
32558 static tree
32559 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32560 tree list, location_t location)
32562 tree c;
32564 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32566 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32567 OMP_CLAUSE_CHAIN (c) = list;
32568 return c;
32571 /* OpenMP 4.0:
32572 inbranch
32573 notinbranch */
32575 static tree
32576 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32577 tree list, location_t location)
32579 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32580 tree c = build_omp_clause (location, code);
32581 OMP_CLAUSE_CHAIN (c) = list;
32582 return c;
32585 /* OpenMP 4.0:
32586 parallel
32588 sections
32589 taskgroup */
32591 static tree
32592 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32593 enum omp_clause_code code,
32594 tree list, location_t location)
32596 tree c = build_omp_clause (location, code);
32597 OMP_CLAUSE_CHAIN (c) = list;
32598 return c;
32601 /* OpenMP 4.5:
32602 nogroup */
32604 static tree
32605 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32606 tree list, location_t location)
32608 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32609 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32610 OMP_CLAUSE_CHAIN (c) = list;
32611 return c;
32614 /* OpenMP 4.5:
32615 simd
32616 threads */
32618 static tree
32619 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32620 enum omp_clause_code code,
32621 tree list, location_t location)
32623 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32624 tree c = build_omp_clause (location, code);
32625 OMP_CLAUSE_CHAIN (c) = list;
32626 return c;
32629 /* OpenMP 4.0:
32630 num_teams ( expression ) */
32632 static tree
32633 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32634 location_t location)
32636 tree t, c;
32638 matching_parens parens;
32639 if (!parens.require_open (parser))
32640 return list;
32642 t = cp_parser_expression (parser);
32644 if (t == error_mark_node
32645 || !parens.require_close (parser))
32646 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32647 /*or_comma=*/false,
32648 /*consume_paren=*/true);
32650 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32651 "num_teams", location);
32653 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32654 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32655 OMP_CLAUSE_CHAIN (c) = list;
32657 return c;
32660 /* OpenMP 4.0:
32661 thread_limit ( expression ) */
32663 static tree
32664 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32665 location_t location)
32667 tree t, c;
32669 matching_parens parens;
32670 if (!parens.require_open (parser))
32671 return list;
32673 t = cp_parser_expression (parser);
32675 if (t == error_mark_node
32676 || !parens.require_close (parser))
32677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32678 /*or_comma=*/false,
32679 /*consume_paren=*/true);
32681 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32682 "thread_limit", location);
32684 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32685 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32686 OMP_CLAUSE_CHAIN (c) = list;
32688 return c;
32691 /* OpenMP 4.0:
32692 aligned ( variable-list )
32693 aligned ( variable-list : constant-expression ) */
32695 static tree
32696 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32698 tree nlist, c, alignment = NULL_TREE;
32699 bool colon;
32701 matching_parens parens;
32702 if (!parens.require_open (parser))
32703 return list;
32705 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32706 &colon);
32708 if (colon)
32710 alignment = cp_parser_constant_expression (parser);
32712 if (!parens.require_close (parser))
32713 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32714 /*or_comma=*/false,
32715 /*consume_paren=*/true);
32717 if (alignment == error_mark_node)
32718 alignment = NULL_TREE;
32721 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32722 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32724 return nlist;
32727 /* OpenMP 4.0:
32728 linear ( variable-list )
32729 linear ( variable-list : expression )
32731 OpenMP 4.5:
32732 linear ( modifier ( variable-list ) )
32733 linear ( modifier ( variable-list ) : expression ) */
32735 static tree
32736 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32737 bool is_cilk_simd_fn, bool declare_simd)
32739 tree nlist, c, step = integer_one_node;
32740 bool colon;
32741 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32743 matching_parens parens;
32744 if (!parens.require_open (parser))
32745 return list;
32747 if (!is_cilk_simd_fn
32748 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32750 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32751 const char *p = IDENTIFIER_POINTER (id);
32753 if (strcmp ("ref", p) == 0)
32754 kind = OMP_CLAUSE_LINEAR_REF;
32755 else if (strcmp ("val", p) == 0)
32756 kind = OMP_CLAUSE_LINEAR_VAL;
32757 else if (strcmp ("uval", p) == 0)
32758 kind = OMP_CLAUSE_LINEAR_UVAL;
32759 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32760 cp_lexer_consume_token (parser->lexer);
32761 else
32762 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32765 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32766 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32767 &colon);
32768 else
32770 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32771 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32772 if (colon)
32773 cp_parser_require (parser, CPP_COLON, RT_COLON);
32774 else if (!parens.require_close (parser))
32775 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32776 /*or_comma=*/false,
32777 /*consume_paren=*/true);
32780 if (colon)
32782 step = NULL_TREE;
32783 if (declare_simd
32784 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32787 cp_token *token = cp_lexer_peek_token (parser->lexer);
32788 cp_parser_parse_tentatively (parser);
32789 step = cp_parser_id_expression (parser, /*template_p=*/false,
32790 /*check_dependency_p=*/true,
32791 /*template_p=*/NULL,
32792 /*declarator_p=*/false,
32793 /*optional_p=*/false);
32794 if (step != error_mark_node)
32795 step = cp_parser_lookup_name_simple (parser, step, token->location);
32796 if (step == error_mark_node)
32798 step = NULL_TREE;
32799 cp_parser_abort_tentative_parse (parser);
32801 else if (!cp_parser_parse_definitely (parser))
32802 step = NULL_TREE;
32804 if (!step)
32805 step = cp_parser_expression (parser);
32807 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32809 sorry ("using parameters for %<linear%> step is not supported yet");
32810 step = integer_one_node;
32812 if (!parens.require_close (parser))
32813 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32814 /*or_comma=*/false,
32815 /*consume_paren=*/true);
32817 if (step == error_mark_node)
32818 return list;
32821 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32823 OMP_CLAUSE_LINEAR_STEP (c) = step;
32824 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32827 return nlist;
32830 /* OpenMP 4.0:
32831 safelen ( constant-expression ) */
32833 static tree
32834 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32835 location_t location)
32837 tree t, c;
32839 matching_parens parens;
32840 if (!parens.require_open (parser))
32841 return list;
32843 t = cp_parser_constant_expression (parser);
32845 if (t == error_mark_node
32846 || !parens.require_close (parser))
32847 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32848 /*or_comma=*/false,
32849 /*consume_paren=*/true);
32851 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32853 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32854 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32855 OMP_CLAUSE_CHAIN (c) = list;
32857 return c;
32860 /* OpenMP 4.0:
32861 simdlen ( constant-expression ) */
32863 static tree
32864 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32865 location_t location)
32867 tree t, c;
32869 matching_parens parens;
32870 if (!parens.require_open (parser))
32871 return list;
32873 t = cp_parser_constant_expression (parser);
32875 if (t == error_mark_node
32876 || !parens.require_close (parser))
32877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32878 /*or_comma=*/false,
32879 /*consume_paren=*/true);
32881 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32883 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32884 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32885 OMP_CLAUSE_CHAIN (c) = list;
32887 return c;
32890 /* OpenMP 4.5:
32891 vec:
32892 identifier [+/- integer]
32893 vec , identifier [+/- integer]
32896 static tree
32897 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32898 tree list)
32900 tree vec = NULL;
32902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32904 cp_parser_error (parser, "expected identifier");
32905 return list;
32908 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32910 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32911 tree t, identifier = cp_parser_identifier (parser);
32912 tree addend = NULL;
32914 if (identifier == error_mark_node)
32915 t = error_mark_node;
32916 else
32918 t = cp_parser_lookup_name_simple
32919 (parser, identifier,
32920 cp_lexer_peek_token (parser->lexer)->location);
32921 if (t == error_mark_node)
32922 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32923 id_loc);
32926 bool neg = false;
32927 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32928 neg = true;
32929 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32931 addend = integer_zero_node;
32932 goto add_to_vector;
32934 cp_lexer_consume_token (parser->lexer);
32936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32938 cp_parser_error (parser, "expected integer");
32939 return list;
32942 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32943 if (TREE_CODE (addend) != INTEGER_CST)
32945 cp_parser_error (parser, "expected integer");
32946 return list;
32948 cp_lexer_consume_token (parser->lexer);
32950 add_to_vector:
32951 if (t != error_mark_node)
32953 vec = tree_cons (addend, t, vec);
32954 if (neg)
32955 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32959 break;
32961 cp_lexer_consume_token (parser->lexer);
32964 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32966 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32967 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32968 OMP_CLAUSE_DECL (u) = nreverse (vec);
32969 OMP_CLAUSE_CHAIN (u) = list;
32970 return u;
32972 return list;
32975 /* OpenMP 4.0:
32976 depend ( depend-kind : variable-list )
32978 depend-kind:
32979 in | out | inout
32981 OpenMP 4.5:
32982 depend ( source )
32984 depend ( sink : vec ) */
32986 static tree
32987 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32989 tree nlist, c;
32990 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32992 matching_parens parens;
32993 if (!parens.require_open (parser))
32994 return list;
32996 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32998 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32999 const char *p = IDENTIFIER_POINTER (id);
33001 if (strcmp ("in", p) == 0)
33002 kind = OMP_CLAUSE_DEPEND_IN;
33003 else if (strcmp ("inout", p) == 0)
33004 kind = OMP_CLAUSE_DEPEND_INOUT;
33005 else if (strcmp ("out", p) == 0)
33006 kind = OMP_CLAUSE_DEPEND_OUT;
33007 else if (strcmp ("source", p) == 0)
33008 kind = OMP_CLAUSE_DEPEND_SOURCE;
33009 else if (strcmp ("sink", p) == 0)
33010 kind = OMP_CLAUSE_DEPEND_SINK;
33011 else
33012 goto invalid_kind;
33014 else
33015 goto invalid_kind;
33017 cp_lexer_consume_token (parser->lexer);
33019 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33021 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33022 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33023 OMP_CLAUSE_DECL (c) = NULL_TREE;
33024 OMP_CLAUSE_CHAIN (c) = list;
33025 if (!parens.require_close (parser))
33026 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33027 /*or_comma=*/false,
33028 /*consume_paren=*/true);
33029 return c;
33032 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33033 goto resync_fail;
33035 if (kind == OMP_CLAUSE_DEPEND_SINK)
33036 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33037 else
33039 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33040 list, NULL);
33042 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33043 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33045 return nlist;
33047 invalid_kind:
33048 cp_parser_error (parser, "invalid depend kind");
33049 resync_fail:
33050 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33051 /*or_comma=*/false,
33052 /*consume_paren=*/true);
33053 return list;
33056 /* OpenMP 4.0:
33057 map ( map-kind : variable-list )
33058 map ( variable-list )
33060 map-kind:
33061 alloc | to | from | tofrom
33063 OpenMP 4.5:
33064 map-kind:
33065 alloc | to | from | tofrom | release | delete
33067 map ( always [,] map-kind: variable-list ) */
33069 static tree
33070 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33072 tree nlist, c;
33073 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33074 bool always = false;
33076 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33077 return list;
33079 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33081 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33082 const char *p = IDENTIFIER_POINTER (id);
33084 if (strcmp ("always", p) == 0)
33086 int nth = 2;
33087 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33088 nth++;
33089 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33090 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33091 == RID_DELETE))
33092 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33093 == CPP_COLON))
33095 always = true;
33096 cp_lexer_consume_token (parser->lexer);
33097 if (nth == 3)
33098 cp_lexer_consume_token (parser->lexer);
33103 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33104 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33106 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33107 const char *p = IDENTIFIER_POINTER (id);
33109 if (strcmp ("alloc", p) == 0)
33110 kind = GOMP_MAP_ALLOC;
33111 else if (strcmp ("to", p) == 0)
33112 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33113 else if (strcmp ("from", p) == 0)
33114 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33115 else if (strcmp ("tofrom", p) == 0)
33116 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33117 else if (strcmp ("release", p) == 0)
33118 kind = GOMP_MAP_RELEASE;
33119 else
33121 cp_parser_error (parser, "invalid map kind");
33122 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33123 /*or_comma=*/false,
33124 /*consume_paren=*/true);
33125 return list;
33127 cp_lexer_consume_token (parser->lexer);
33128 cp_lexer_consume_token (parser->lexer);
33130 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33131 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33133 kind = GOMP_MAP_DELETE;
33134 cp_lexer_consume_token (parser->lexer);
33135 cp_lexer_consume_token (parser->lexer);
33138 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33139 NULL);
33141 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33142 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33144 return nlist;
33147 /* OpenMP 4.0:
33148 device ( expression ) */
33150 static tree
33151 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33152 location_t location)
33154 tree t, c;
33156 matching_parens parens;
33157 if (!parens.require_open (parser))
33158 return list;
33160 t = cp_parser_expression (parser);
33162 if (t == error_mark_node
33163 || !parens.require_close (parser))
33164 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33165 /*or_comma=*/false,
33166 /*consume_paren=*/true);
33168 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33169 "device", location);
33171 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33172 OMP_CLAUSE_DEVICE_ID (c) = t;
33173 OMP_CLAUSE_CHAIN (c) = list;
33175 return c;
33178 /* OpenMP 4.0:
33179 dist_schedule ( static )
33180 dist_schedule ( static , expression ) */
33182 static tree
33183 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33184 location_t location)
33186 tree c, t;
33188 matching_parens parens;
33189 if (!parens.require_open (parser))
33190 return list;
33192 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33194 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33195 goto invalid_kind;
33196 cp_lexer_consume_token (parser->lexer);
33198 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33200 cp_lexer_consume_token (parser->lexer);
33202 t = cp_parser_assignment_expression (parser);
33204 if (t == error_mark_node)
33205 goto resync_fail;
33206 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33208 if (!parens.require_close (parser))
33209 goto resync_fail;
33211 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33212 goto resync_fail;
33214 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33215 location);
33216 OMP_CLAUSE_CHAIN (c) = list;
33217 return c;
33219 invalid_kind:
33220 cp_parser_error (parser, "invalid dist_schedule kind");
33221 resync_fail:
33222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33223 /*or_comma=*/false,
33224 /*consume_paren=*/true);
33225 return list;
33228 /* OpenMP 4.0:
33229 proc_bind ( proc-bind-kind )
33231 proc-bind-kind:
33232 master | close | spread */
33234 static tree
33235 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33236 location_t location)
33238 tree c;
33239 enum omp_clause_proc_bind_kind kind;
33241 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33242 return list;
33244 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33246 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33247 const char *p = IDENTIFIER_POINTER (id);
33249 if (strcmp ("master", p) == 0)
33250 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33251 else if (strcmp ("close", p) == 0)
33252 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33253 else if (strcmp ("spread", p) == 0)
33254 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33255 else
33256 goto invalid_kind;
33258 else
33259 goto invalid_kind;
33261 cp_lexer_consume_token (parser->lexer);
33262 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33263 goto resync_fail;
33265 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33266 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33267 location);
33268 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33269 OMP_CLAUSE_CHAIN (c) = list;
33270 return c;
33272 invalid_kind:
33273 cp_parser_error (parser, "invalid depend kind");
33274 resync_fail:
33275 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33276 /*or_comma=*/false,
33277 /*consume_paren=*/true);
33278 return list;
33281 /* OpenACC:
33282 async [( int-expr )] */
33284 static tree
33285 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33287 tree c, t;
33288 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33290 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33292 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33294 matching_parens parens;
33295 parens.consume_open (parser);
33297 t = cp_parser_expression (parser);
33298 if (t == error_mark_node
33299 || !parens.require_close (parser))
33300 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33301 /*or_comma=*/false,
33302 /*consume_paren=*/true);
33305 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33307 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33308 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33309 OMP_CLAUSE_CHAIN (c) = list;
33310 list = c;
33312 return list;
33315 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33316 is a bitmask in MASK. Return the list of clauses found. */
33318 static tree
33319 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33320 const char *where, cp_token *pragma_tok,
33321 bool finish_p = true)
33323 tree clauses = NULL;
33324 bool first = true;
33326 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33328 location_t here;
33329 pragma_omp_clause c_kind;
33330 omp_clause_code code;
33331 const char *c_name;
33332 tree prev = clauses;
33334 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33335 cp_lexer_consume_token (parser->lexer);
33337 here = cp_lexer_peek_token (parser->lexer)->location;
33338 c_kind = cp_parser_omp_clause_name (parser);
33340 switch (c_kind)
33342 case PRAGMA_OACC_CLAUSE_ASYNC:
33343 clauses = cp_parser_oacc_clause_async (parser, clauses);
33344 c_name = "async";
33345 break;
33346 case PRAGMA_OACC_CLAUSE_AUTO:
33347 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33348 clauses, here);
33349 c_name = "auto";
33350 break;
33351 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33352 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33353 c_name = "collapse";
33354 break;
33355 case PRAGMA_OACC_CLAUSE_COPY:
33356 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33357 c_name = "copy";
33358 break;
33359 case PRAGMA_OACC_CLAUSE_COPYIN:
33360 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33361 c_name = "copyin";
33362 break;
33363 case PRAGMA_OACC_CLAUSE_COPYOUT:
33364 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33365 c_name = "copyout";
33366 break;
33367 case PRAGMA_OACC_CLAUSE_CREATE:
33368 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33369 c_name = "create";
33370 break;
33371 case PRAGMA_OACC_CLAUSE_DELETE:
33372 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33373 c_name = "delete";
33374 break;
33375 case PRAGMA_OMP_CLAUSE_DEFAULT:
33376 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33377 c_name = "default";
33378 break;
33379 case PRAGMA_OACC_CLAUSE_DEVICE:
33380 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33381 c_name = "device";
33382 break;
33383 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33384 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33385 c_name = "deviceptr";
33386 break;
33387 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33388 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33389 c_name = "device_resident";
33390 break;
33391 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33393 clauses);
33394 c_name = "firstprivate";
33395 break;
33396 case PRAGMA_OACC_CLAUSE_GANG:
33397 c_name = "gang";
33398 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33399 c_name, clauses);
33400 break;
33401 case PRAGMA_OACC_CLAUSE_HOST:
33402 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33403 c_name = "host";
33404 break;
33405 case PRAGMA_OACC_CLAUSE_IF:
33406 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33407 c_name = "if";
33408 break;
33409 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33410 clauses = cp_parser_oacc_simple_clause (parser,
33411 OMP_CLAUSE_INDEPENDENT,
33412 clauses, here);
33413 c_name = "independent";
33414 break;
33415 case PRAGMA_OACC_CLAUSE_LINK:
33416 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33417 c_name = "link";
33418 break;
33419 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33420 code = OMP_CLAUSE_NUM_GANGS;
33421 c_name = "num_gangs";
33422 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33423 clauses);
33424 break;
33425 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33426 c_name = "num_workers";
33427 code = OMP_CLAUSE_NUM_WORKERS;
33428 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33429 clauses);
33430 break;
33431 case PRAGMA_OACC_CLAUSE_PRESENT:
33432 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33433 c_name = "present";
33434 break;
33435 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33436 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33437 c_name = "present_or_copy";
33438 break;
33439 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33440 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33441 c_name = "present_or_copyin";
33442 break;
33443 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33444 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33445 c_name = "present_or_copyout";
33446 break;
33447 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33448 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33449 c_name = "present_or_create";
33450 break;
33451 case PRAGMA_OACC_CLAUSE_PRIVATE:
33452 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33453 clauses);
33454 c_name = "private";
33455 break;
33456 case PRAGMA_OACC_CLAUSE_REDUCTION:
33457 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33458 c_name = "reduction";
33459 break;
33460 case PRAGMA_OACC_CLAUSE_SELF:
33461 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33462 c_name = "self";
33463 break;
33464 case PRAGMA_OACC_CLAUSE_SEQ:
33465 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33466 clauses, here);
33467 c_name = "seq";
33468 break;
33469 case PRAGMA_OACC_CLAUSE_TILE:
33470 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33471 c_name = "tile";
33472 break;
33473 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33475 clauses);
33476 c_name = "use_device";
33477 break;
33478 case PRAGMA_OACC_CLAUSE_VECTOR:
33479 c_name = "vector";
33480 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33481 c_name, clauses);
33482 break;
33483 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33484 c_name = "vector_length";
33485 code = OMP_CLAUSE_VECTOR_LENGTH;
33486 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33487 clauses);
33488 break;
33489 case PRAGMA_OACC_CLAUSE_WAIT:
33490 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33491 c_name = "wait";
33492 break;
33493 case PRAGMA_OACC_CLAUSE_WORKER:
33494 c_name = "worker";
33495 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33496 c_name, clauses);
33497 break;
33498 default:
33499 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33500 goto saw_error;
33503 first = false;
33505 if (((mask >> c_kind) & 1) == 0)
33507 /* Remove the invalid clause(s) from the list to avoid
33508 confusing the rest of the compiler. */
33509 clauses = prev;
33510 error_at (here, "%qs is not valid for %qs", c_name, where);
33514 saw_error:
33515 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33517 if (finish_p)
33518 return finish_omp_clauses (clauses, C_ORT_ACC);
33520 return clauses;
33523 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33524 is a bitmask in MASK. Return the list of clauses found; the result
33525 of clause default goes in *pdefault. */
33527 static tree
33528 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33529 const char *where, cp_token *pragma_tok,
33530 bool finish_p = true)
33532 tree clauses = NULL;
33533 bool first = true;
33534 cp_token *token = NULL;
33536 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33538 pragma_omp_clause c_kind;
33539 const char *c_name;
33540 tree prev = clauses;
33542 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33543 cp_lexer_consume_token (parser->lexer);
33545 token = cp_lexer_peek_token (parser->lexer);
33546 c_kind = cp_parser_omp_clause_name (parser);
33548 switch (c_kind)
33550 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33551 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33552 token->location);
33553 c_name = "collapse";
33554 break;
33555 case PRAGMA_OMP_CLAUSE_COPYIN:
33556 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33557 c_name = "copyin";
33558 break;
33559 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33560 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33561 clauses);
33562 c_name = "copyprivate";
33563 break;
33564 case PRAGMA_OMP_CLAUSE_DEFAULT:
33565 clauses = cp_parser_omp_clause_default (parser, clauses,
33566 token->location, false);
33567 c_name = "default";
33568 break;
33569 case PRAGMA_OMP_CLAUSE_FINAL:
33570 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33571 c_name = "final";
33572 break;
33573 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33574 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33575 clauses);
33576 c_name = "firstprivate";
33577 break;
33578 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33579 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33580 token->location);
33581 c_name = "grainsize";
33582 break;
33583 case PRAGMA_OMP_CLAUSE_HINT:
33584 clauses = cp_parser_omp_clause_hint (parser, clauses,
33585 token->location);
33586 c_name = "hint";
33587 break;
33588 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33589 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33590 token->location);
33591 c_name = "defaultmap";
33592 break;
33593 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33594 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33595 clauses);
33596 c_name = "use_device_ptr";
33597 break;
33598 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33599 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33600 clauses);
33601 c_name = "is_device_ptr";
33602 break;
33603 case PRAGMA_OMP_CLAUSE_IF:
33604 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33605 true);
33606 c_name = "if";
33607 break;
33608 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33609 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33610 clauses);
33611 c_name = "lastprivate";
33612 break;
33613 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33614 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33615 token->location);
33616 c_name = "mergeable";
33617 break;
33618 case PRAGMA_OMP_CLAUSE_NOWAIT:
33619 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33620 c_name = "nowait";
33621 break;
33622 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33623 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33624 token->location);
33625 c_name = "num_tasks";
33626 break;
33627 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33628 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33629 token->location);
33630 c_name = "num_threads";
33631 break;
33632 case PRAGMA_OMP_CLAUSE_ORDERED:
33633 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33634 token->location);
33635 c_name = "ordered";
33636 break;
33637 case PRAGMA_OMP_CLAUSE_PRIORITY:
33638 clauses = cp_parser_omp_clause_priority (parser, clauses,
33639 token->location);
33640 c_name = "priority";
33641 break;
33642 case PRAGMA_OMP_CLAUSE_PRIVATE:
33643 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33644 clauses);
33645 c_name = "private";
33646 break;
33647 case PRAGMA_OMP_CLAUSE_REDUCTION:
33648 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33649 c_name = "reduction";
33650 break;
33651 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33652 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33653 token->location);
33654 c_name = "schedule";
33655 break;
33656 case PRAGMA_OMP_CLAUSE_SHARED:
33657 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33658 clauses);
33659 c_name = "shared";
33660 break;
33661 case PRAGMA_OMP_CLAUSE_UNTIED:
33662 clauses = cp_parser_omp_clause_untied (parser, clauses,
33663 token->location);
33664 c_name = "untied";
33665 break;
33666 case PRAGMA_OMP_CLAUSE_INBRANCH:
33667 case PRAGMA_CILK_CLAUSE_MASK:
33668 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33669 clauses, token->location);
33670 c_name = "inbranch";
33671 break;
33672 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33673 case PRAGMA_CILK_CLAUSE_NOMASK:
33674 clauses = cp_parser_omp_clause_branch (parser,
33675 OMP_CLAUSE_NOTINBRANCH,
33676 clauses, token->location);
33677 c_name = "notinbranch";
33678 break;
33679 case PRAGMA_OMP_CLAUSE_PARALLEL:
33680 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33681 clauses, token->location);
33682 c_name = "parallel";
33683 if (!first)
33685 clause_not_first:
33686 error_at (token->location, "%qs must be the first clause of %qs",
33687 c_name, where);
33688 clauses = prev;
33690 break;
33691 case PRAGMA_OMP_CLAUSE_FOR:
33692 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33693 clauses, token->location);
33694 c_name = "for";
33695 if (!first)
33696 goto clause_not_first;
33697 break;
33698 case PRAGMA_OMP_CLAUSE_SECTIONS:
33699 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33700 clauses, token->location);
33701 c_name = "sections";
33702 if (!first)
33703 goto clause_not_first;
33704 break;
33705 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33706 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33707 clauses, token->location);
33708 c_name = "taskgroup";
33709 if (!first)
33710 goto clause_not_first;
33711 break;
33712 case PRAGMA_OMP_CLAUSE_LINK:
33713 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33714 c_name = "to";
33715 break;
33716 case PRAGMA_OMP_CLAUSE_TO:
33717 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33718 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33719 clauses);
33720 else
33721 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33722 c_name = "to";
33723 break;
33724 case PRAGMA_OMP_CLAUSE_FROM:
33725 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33726 c_name = "from";
33727 break;
33728 case PRAGMA_OMP_CLAUSE_UNIFORM:
33729 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33730 clauses);
33731 c_name = "uniform";
33732 break;
33733 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33734 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33735 token->location);
33736 c_name = "num_teams";
33737 break;
33738 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33739 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33740 token->location);
33741 c_name = "thread_limit";
33742 break;
33743 case PRAGMA_OMP_CLAUSE_ALIGNED:
33744 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33745 c_name = "aligned";
33746 break;
33747 case PRAGMA_OMP_CLAUSE_LINEAR:
33749 bool cilk_simd_fn = false, declare_simd = false;
33750 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33751 cilk_simd_fn = true;
33752 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33753 declare_simd = true;
33754 clauses = cp_parser_omp_clause_linear (parser, clauses,
33755 cilk_simd_fn, declare_simd);
33757 c_name = "linear";
33758 break;
33759 case PRAGMA_OMP_CLAUSE_DEPEND:
33760 clauses = cp_parser_omp_clause_depend (parser, clauses,
33761 token->location);
33762 c_name = "depend";
33763 break;
33764 case PRAGMA_OMP_CLAUSE_MAP:
33765 clauses = cp_parser_omp_clause_map (parser, clauses);
33766 c_name = "map";
33767 break;
33768 case PRAGMA_OMP_CLAUSE_DEVICE:
33769 clauses = cp_parser_omp_clause_device (parser, clauses,
33770 token->location);
33771 c_name = "device";
33772 break;
33773 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33774 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33775 token->location);
33776 c_name = "dist_schedule";
33777 break;
33778 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33779 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33780 token->location);
33781 c_name = "proc_bind";
33782 break;
33783 case PRAGMA_OMP_CLAUSE_SAFELEN:
33784 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33785 token->location);
33786 c_name = "safelen";
33787 break;
33788 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33789 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33790 token->location);
33791 c_name = "simdlen";
33792 break;
33793 case PRAGMA_OMP_CLAUSE_NOGROUP:
33794 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33795 token->location);
33796 c_name = "nogroup";
33797 break;
33798 case PRAGMA_OMP_CLAUSE_THREADS:
33799 clauses
33800 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33801 clauses, token->location);
33802 c_name = "threads";
33803 break;
33804 case PRAGMA_OMP_CLAUSE_SIMD:
33805 clauses
33806 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33807 clauses, token->location);
33808 c_name = "simd";
33809 break;
33810 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33811 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33812 c_name = "simdlen";
33813 break;
33814 default:
33815 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33816 goto saw_error;
33819 first = false;
33821 if (((mask >> c_kind) & 1) == 0)
33823 /* Remove the invalid clause(s) from the list to avoid
33824 confusing the rest of the compiler. */
33825 clauses = prev;
33826 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33829 saw_error:
33830 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33831 no reason to skip to the end. */
33832 if (!(flag_cilkplus && pragma_tok == NULL))
33833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33834 if (finish_p)
33836 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33837 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33838 else
33839 return finish_omp_clauses (clauses, C_ORT_OMP);
33841 return clauses;
33844 /* OpenMP 2.5:
33845 structured-block:
33846 statement
33848 In practice, we're also interested in adding the statement to an
33849 outer node. So it is convenient if we work around the fact that
33850 cp_parser_statement calls add_stmt. */
33852 static unsigned
33853 cp_parser_begin_omp_structured_block (cp_parser *parser)
33855 unsigned save = parser->in_statement;
33857 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33858 This preserves the "not within loop or switch" style error messages
33859 for nonsense cases like
33860 void foo() {
33861 #pragma omp single
33862 break;
33865 if (parser->in_statement)
33866 parser->in_statement = IN_OMP_BLOCK;
33868 return save;
33871 static void
33872 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33874 parser->in_statement = save;
33877 static tree
33878 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33880 tree stmt = begin_omp_structured_block ();
33881 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33883 cp_parser_statement (parser, NULL_TREE, false, if_p);
33885 cp_parser_end_omp_structured_block (parser, save);
33886 return finish_omp_structured_block (stmt);
33889 /* OpenMP 2.5:
33890 # pragma omp atomic new-line
33891 expression-stmt
33893 expression-stmt:
33894 x binop= expr | x++ | ++x | x-- | --x
33895 binop:
33896 +, *, -, /, &, ^, |, <<, >>
33898 where x is an lvalue expression with scalar type.
33900 OpenMP 3.1:
33901 # pragma omp atomic new-line
33902 update-stmt
33904 # pragma omp atomic read new-line
33905 read-stmt
33907 # pragma omp atomic write new-line
33908 write-stmt
33910 # pragma omp atomic update new-line
33911 update-stmt
33913 # pragma omp atomic capture new-line
33914 capture-stmt
33916 # pragma omp atomic capture new-line
33917 capture-block
33919 read-stmt:
33920 v = x
33921 write-stmt:
33922 x = expr
33923 update-stmt:
33924 expression-stmt | x = x binop expr
33925 capture-stmt:
33926 v = expression-stmt
33927 capture-block:
33928 { v = x; update-stmt; } | { update-stmt; v = x; }
33930 OpenMP 4.0:
33931 update-stmt:
33932 expression-stmt | x = x binop expr | x = expr binop x
33933 capture-stmt:
33934 v = update-stmt
33935 capture-block:
33936 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33938 where x and v are lvalue expressions with scalar type. */
33940 static void
33941 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33943 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33944 tree rhs1 = NULL_TREE, orig_lhs;
33945 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33946 bool structured_block = false;
33947 bool seq_cst = false;
33949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33952 const char *p = IDENTIFIER_POINTER (id);
33954 if (!strcmp (p, "seq_cst"))
33956 seq_cst = true;
33957 cp_lexer_consume_token (parser->lexer);
33958 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33959 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33960 cp_lexer_consume_token (parser->lexer);
33963 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33965 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33966 const char *p = IDENTIFIER_POINTER (id);
33968 if (!strcmp (p, "read"))
33969 code = OMP_ATOMIC_READ;
33970 else if (!strcmp (p, "write"))
33971 code = NOP_EXPR;
33972 else if (!strcmp (p, "update"))
33973 code = OMP_ATOMIC;
33974 else if (!strcmp (p, "capture"))
33975 code = OMP_ATOMIC_CAPTURE_NEW;
33976 else
33977 p = NULL;
33978 if (p)
33979 cp_lexer_consume_token (parser->lexer);
33981 if (!seq_cst)
33983 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33984 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33985 cp_lexer_consume_token (parser->lexer);
33987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33990 const char *p = IDENTIFIER_POINTER (id);
33992 if (!strcmp (p, "seq_cst"))
33994 seq_cst = true;
33995 cp_lexer_consume_token (parser->lexer);
33999 cp_parser_require_pragma_eol (parser, pragma_tok);
34001 switch (code)
34003 case OMP_ATOMIC_READ:
34004 case NOP_EXPR: /* atomic write */
34005 v = cp_parser_unary_expression (parser);
34006 if (v == error_mark_node)
34007 goto saw_error;
34008 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34009 goto saw_error;
34010 if (code == NOP_EXPR)
34011 lhs = cp_parser_expression (parser);
34012 else
34013 lhs = cp_parser_unary_expression (parser);
34014 if (lhs == error_mark_node)
34015 goto saw_error;
34016 if (code == NOP_EXPR)
34018 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34019 opcode. */
34020 code = OMP_ATOMIC;
34021 rhs = lhs;
34022 lhs = v;
34023 v = NULL_TREE;
34025 goto done;
34026 case OMP_ATOMIC_CAPTURE_NEW:
34027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34029 cp_lexer_consume_token (parser->lexer);
34030 structured_block = true;
34032 else
34034 v = cp_parser_unary_expression (parser);
34035 if (v == error_mark_node)
34036 goto saw_error;
34037 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34038 goto saw_error;
34040 default:
34041 break;
34044 restart:
34045 lhs = cp_parser_unary_expression (parser);
34046 orig_lhs = lhs;
34047 switch (TREE_CODE (lhs))
34049 case ERROR_MARK:
34050 goto saw_error;
34052 case POSTINCREMENT_EXPR:
34053 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34054 code = OMP_ATOMIC_CAPTURE_OLD;
34055 /* FALLTHROUGH */
34056 case PREINCREMENT_EXPR:
34057 lhs = TREE_OPERAND (lhs, 0);
34058 opcode = PLUS_EXPR;
34059 rhs = integer_one_node;
34060 break;
34062 case POSTDECREMENT_EXPR:
34063 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34064 code = OMP_ATOMIC_CAPTURE_OLD;
34065 /* FALLTHROUGH */
34066 case PREDECREMENT_EXPR:
34067 lhs = TREE_OPERAND (lhs, 0);
34068 opcode = MINUS_EXPR;
34069 rhs = integer_one_node;
34070 break;
34072 case COMPOUND_EXPR:
34073 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34074 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34075 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34076 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34077 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34078 (TREE_OPERAND (lhs, 1), 0), 0)))
34079 == BOOLEAN_TYPE)
34080 /* Undo effects of boolean_increment for post {in,de}crement. */
34081 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34082 /* FALLTHRU */
34083 case MODIFY_EXPR:
34084 if (TREE_CODE (lhs) == MODIFY_EXPR
34085 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34087 /* Undo effects of boolean_increment. */
34088 if (integer_onep (TREE_OPERAND (lhs, 1)))
34090 /* This is pre or post increment. */
34091 rhs = TREE_OPERAND (lhs, 1);
34092 lhs = TREE_OPERAND (lhs, 0);
34093 opcode = NOP_EXPR;
34094 if (code == OMP_ATOMIC_CAPTURE_NEW
34095 && !structured_block
34096 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34097 code = OMP_ATOMIC_CAPTURE_OLD;
34098 break;
34101 /* FALLTHRU */
34102 default:
34103 switch (cp_lexer_peek_token (parser->lexer)->type)
34105 case CPP_MULT_EQ:
34106 opcode = MULT_EXPR;
34107 break;
34108 case CPP_DIV_EQ:
34109 opcode = TRUNC_DIV_EXPR;
34110 break;
34111 case CPP_PLUS_EQ:
34112 opcode = PLUS_EXPR;
34113 break;
34114 case CPP_MINUS_EQ:
34115 opcode = MINUS_EXPR;
34116 break;
34117 case CPP_LSHIFT_EQ:
34118 opcode = LSHIFT_EXPR;
34119 break;
34120 case CPP_RSHIFT_EQ:
34121 opcode = RSHIFT_EXPR;
34122 break;
34123 case CPP_AND_EQ:
34124 opcode = BIT_AND_EXPR;
34125 break;
34126 case CPP_OR_EQ:
34127 opcode = BIT_IOR_EXPR;
34128 break;
34129 case CPP_XOR_EQ:
34130 opcode = BIT_XOR_EXPR;
34131 break;
34132 case CPP_EQ:
34133 enum cp_parser_prec oprec;
34134 cp_token *token;
34135 cp_lexer_consume_token (parser->lexer);
34136 cp_parser_parse_tentatively (parser);
34137 rhs1 = cp_parser_simple_cast_expression (parser);
34138 if (rhs1 == error_mark_node)
34140 cp_parser_abort_tentative_parse (parser);
34141 cp_parser_simple_cast_expression (parser);
34142 goto saw_error;
34144 token = cp_lexer_peek_token (parser->lexer);
34145 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34147 cp_parser_abort_tentative_parse (parser);
34148 cp_parser_parse_tentatively (parser);
34149 rhs = cp_parser_binary_expression (parser, false, true,
34150 PREC_NOT_OPERATOR, NULL);
34151 if (rhs == error_mark_node)
34153 cp_parser_abort_tentative_parse (parser);
34154 cp_parser_binary_expression (parser, false, true,
34155 PREC_NOT_OPERATOR, NULL);
34156 goto saw_error;
34158 switch (TREE_CODE (rhs))
34160 case MULT_EXPR:
34161 case TRUNC_DIV_EXPR:
34162 case RDIV_EXPR:
34163 case PLUS_EXPR:
34164 case MINUS_EXPR:
34165 case LSHIFT_EXPR:
34166 case RSHIFT_EXPR:
34167 case BIT_AND_EXPR:
34168 case BIT_IOR_EXPR:
34169 case BIT_XOR_EXPR:
34170 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34172 if (cp_parser_parse_definitely (parser))
34174 opcode = TREE_CODE (rhs);
34175 rhs1 = TREE_OPERAND (rhs, 0);
34176 rhs = TREE_OPERAND (rhs, 1);
34177 goto stmt_done;
34179 else
34180 goto saw_error;
34182 break;
34183 default:
34184 break;
34186 cp_parser_abort_tentative_parse (parser);
34187 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34189 rhs = cp_parser_expression (parser);
34190 if (rhs == error_mark_node)
34191 goto saw_error;
34192 opcode = NOP_EXPR;
34193 rhs1 = NULL_TREE;
34194 goto stmt_done;
34196 cp_parser_error (parser,
34197 "invalid form of %<#pragma omp atomic%>");
34198 goto saw_error;
34200 if (!cp_parser_parse_definitely (parser))
34201 goto saw_error;
34202 switch (token->type)
34204 case CPP_SEMICOLON:
34205 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34207 code = OMP_ATOMIC_CAPTURE_OLD;
34208 v = lhs;
34209 lhs = NULL_TREE;
34210 lhs1 = rhs1;
34211 rhs1 = NULL_TREE;
34212 cp_lexer_consume_token (parser->lexer);
34213 goto restart;
34215 else if (structured_block)
34217 opcode = NOP_EXPR;
34218 rhs = rhs1;
34219 rhs1 = NULL_TREE;
34220 goto stmt_done;
34222 cp_parser_error (parser,
34223 "invalid form of %<#pragma omp atomic%>");
34224 goto saw_error;
34225 case CPP_MULT:
34226 opcode = MULT_EXPR;
34227 break;
34228 case CPP_DIV:
34229 opcode = TRUNC_DIV_EXPR;
34230 break;
34231 case CPP_PLUS:
34232 opcode = PLUS_EXPR;
34233 break;
34234 case CPP_MINUS:
34235 opcode = MINUS_EXPR;
34236 break;
34237 case CPP_LSHIFT:
34238 opcode = LSHIFT_EXPR;
34239 break;
34240 case CPP_RSHIFT:
34241 opcode = RSHIFT_EXPR;
34242 break;
34243 case CPP_AND:
34244 opcode = BIT_AND_EXPR;
34245 break;
34246 case CPP_OR:
34247 opcode = BIT_IOR_EXPR;
34248 break;
34249 case CPP_XOR:
34250 opcode = BIT_XOR_EXPR;
34251 break;
34252 default:
34253 cp_parser_error (parser,
34254 "invalid operator for %<#pragma omp atomic%>");
34255 goto saw_error;
34257 oprec = TOKEN_PRECEDENCE (token);
34258 gcc_assert (oprec != PREC_NOT_OPERATOR);
34259 if (commutative_tree_code (opcode))
34260 oprec = (enum cp_parser_prec) (oprec - 1);
34261 cp_lexer_consume_token (parser->lexer);
34262 rhs = cp_parser_binary_expression (parser, false, false,
34263 oprec, NULL);
34264 if (rhs == error_mark_node)
34265 goto saw_error;
34266 goto stmt_done;
34267 /* FALLTHROUGH */
34268 default:
34269 cp_parser_error (parser,
34270 "invalid operator for %<#pragma omp atomic%>");
34271 goto saw_error;
34273 cp_lexer_consume_token (parser->lexer);
34275 rhs = cp_parser_expression (parser);
34276 if (rhs == error_mark_node)
34277 goto saw_error;
34278 break;
34280 stmt_done:
34281 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34283 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34284 goto saw_error;
34285 v = cp_parser_unary_expression (parser);
34286 if (v == error_mark_node)
34287 goto saw_error;
34288 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34289 goto saw_error;
34290 lhs1 = cp_parser_unary_expression (parser);
34291 if (lhs1 == error_mark_node)
34292 goto saw_error;
34294 if (structured_block)
34296 cp_parser_consume_semicolon_at_end_of_statement (parser);
34297 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34299 done:
34300 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34301 if (!structured_block)
34302 cp_parser_consume_semicolon_at_end_of_statement (parser);
34303 return;
34305 saw_error:
34306 cp_parser_skip_to_end_of_block_or_statement (parser);
34307 if (structured_block)
34309 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34310 cp_lexer_consume_token (parser->lexer);
34311 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34313 cp_parser_skip_to_end_of_block_or_statement (parser);
34314 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34315 cp_lexer_consume_token (parser->lexer);
34321 /* OpenMP 2.5:
34322 # pragma omp barrier new-line */
34324 static void
34325 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34327 cp_parser_require_pragma_eol (parser, pragma_tok);
34328 finish_omp_barrier ();
34331 /* OpenMP 2.5:
34332 # pragma omp critical [(name)] new-line
34333 structured-block
34335 OpenMP 4.5:
34336 # pragma omp critical [(name) [hint(expression)]] new-line
34337 structured-block */
34339 #define OMP_CRITICAL_CLAUSE_MASK \
34340 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34342 static tree
34343 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34345 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34349 matching_parens parens;
34350 parens.consume_open (parser);
34352 name = cp_parser_identifier (parser);
34354 if (name == error_mark_node
34355 || !parens.require_close (parser))
34356 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34357 /*or_comma=*/false,
34358 /*consume_paren=*/true);
34359 if (name == error_mark_node)
34360 name = NULL;
34362 clauses = cp_parser_omp_all_clauses (parser,
34363 OMP_CRITICAL_CLAUSE_MASK,
34364 "#pragma omp critical", pragma_tok);
34366 else
34367 cp_parser_require_pragma_eol (parser, pragma_tok);
34369 stmt = cp_parser_omp_structured_block (parser, if_p);
34370 return c_finish_omp_critical (input_location, stmt, name, clauses);
34373 /* OpenMP 2.5:
34374 # pragma omp flush flush-vars[opt] new-line
34376 flush-vars:
34377 ( variable-list ) */
34379 static void
34380 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34383 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34384 cp_parser_require_pragma_eol (parser, pragma_tok);
34386 finish_omp_flush ();
34389 /* Helper function, to parse omp for increment expression. */
34391 static tree
34392 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34394 tree cond = cp_parser_binary_expression (parser, false, true,
34395 PREC_NOT_OPERATOR, NULL);
34396 if (cond == error_mark_node
34397 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34399 cp_parser_skip_to_end_of_statement (parser);
34400 return error_mark_node;
34403 switch (TREE_CODE (cond))
34405 case GT_EXPR:
34406 case GE_EXPR:
34407 case LT_EXPR:
34408 case LE_EXPR:
34409 break;
34410 case NE_EXPR:
34411 if (code == CILK_SIMD || code == CILK_FOR)
34412 break;
34413 /* Fall through: OpenMP disallows NE_EXPR. */
34414 gcc_fallthrough ();
34415 default:
34416 return error_mark_node;
34419 /* If decl is an iterator, preserve LHS and RHS of the relational
34420 expr until finish_omp_for. */
34421 if (decl
34422 && (type_dependent_expression_p (decl)
34423 || CLASS_TYPE_P (TREE_TYPE (decl))))
34424 return cond;
34426 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34427 TREE_CODE (cond),
34428 TREE_OPERAND (cond, 0), ERROR_MARK,
34429 TREE_OPERAND (cond, 1), ERROR_MARK,
34430 /*overload=*/NULL, tf_warning_or_error);
34433 /* Helper function, to parse omp for increment expression. */
34435 static tree
34436 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34438 cp_token *token = cp_lexer_peek_token (parser->lexer);
34439 enum tree_code op;
34440 tree lhs, rhs;
34441 cp_id_kind idk;
34442 bool decl_first;
34444 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34446 op = (token->type == CPP_PLUS_PLUS
34447 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34448 cp_lexer_consume_token (parser->lexer);
34449 lhs = cp_parser_simple_cast_expression (parser);
34450 if (lhs != decl
34451 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34452 return error_mark_node;
34453 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34456 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34457 if (lhs != decl
34458 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34459 return error_mark_node;
34461 token = cp_lexer_peek_token (parser->lexer);
34462 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34464 op = (token->type == CPP_PLUS_PLUS
34465 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34466 cp_lexer_consume_token (parser->lexer);
34467 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34470 op = cp_parser_assignment_operator_opt (parser);
34471 if (op == ERROR_MARK)
34472 return error_mark_node;
34474 if (op != NOP_EXPR)
34476 rhs = cp_parser_assignment_expression (parser);
34477 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34478 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34481 lhs = cp_parser_binary_expression (parser, false, false,
34482 PREC_ADDITIVE_EXPRESSION, NULL);
34483 token = cp_lexer_peek_token (parser->lexer);
34484 decl_first = (lhs == decl
34485 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34486 if (decl_first)
34487 lhs = NULL_TREE;
34488 if (token->type != CPP_PLUS
34489 && token->type != CPP_MINUS)
34490 return error_mark_node;
34494 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34495 cp_lexer_consume_token (parser->lexer);
34496 rhs = cp_parser_binary_expression (parser, false, false,
34497 PREC_ADDITIVE_EXPRESSION, NULL);
34498 token = cp_lexer_peek_token (parser->lexer);
34499 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34501 if (lhs == NULL_TREE)
34503 if (op == PLUS_EXPR)
34504 lhs = rhs;
34505 else
34506 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34507 tf_warning_or_error);
34509 else
34510 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34511 ERROR_MARK, NULL, tf_warning_or_error);
34514 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34516 if (!decl_first)
34518 if ((rhs != decl
34519 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34520 || op == MINUS_EXPR)
34521 return error_mark_node;
34522 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34524 else
34525 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34527 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34530 /* Parse the initialization statement of either an OpenMP for loop or
34531 a Cilk Plus for loop.
34533 Return true if the resulting construct should have an
34534 OMP_CLAUSE_PRIVATE added to it. */
34536 static tree
34537 cp_parser_omp_for_loop_init (cp_parser *parser,
34538 enum tree_code code,
34539 tree &this_pre_body,
34540 vec<tree, va_gc> *for_block,
34541 tree &init,
34542 tree &orig_init,
34543 tree &decl,
34544 tree &real_decl)
34546 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34547 return NULL_TREE;
34549 tree add_private_clause = NULL_TREE;
34551 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34553 init-expr:
34554 var = lb
34555 integer-type var = lb
34556 random-access-iterator-type var = lb
34557 pointer-type var = lb
34559 cp_decl_specifier_seq type_specifiers;
34561 /* First, try to parse as an initialized declaration. See
34562 cp_parser_condition, from whence the bulk of this is copied. */
34564 cp_parser_parse_tentatively (parser);
34565 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34566 /*is_trailing_return=*/false,
34567 &type_specifiers);
34568 if (cp_parser_parse_definitely (parser))
34570 /* If parsing a type specifier seq succeeded, then this
34571 MUST be a initialized declaration. */
34572 tree asm_specification, attributes;
34573 cp_declarator *declarator;
34575 declarator = cp_parser_declarator (parser,
34576 CP_PARSER_DECLARATOR_NAMED,
34577 /*ctor_dtor_or_conv_p=*/NULL,
34578 /*parenthesized_p=*/NULL,
34579 /*member_p=*/false,
34580 /*friend_p=*/false);
34581 attributes = cp_parser_attributes_opt (parser);
34582 asm_specification = cp_parser_asm_specification_opt (parser);
34584 if (declarator == cp_error_declarator)
34585 cp_parser_skip_to_end_of_statement (parser);
34587 else
34589 tree pushed_scope, auto_node;
34591 decl = start_decl (declarator, &type_specifiers,
34592 SD_INITIALIZED, attributes,
34593 /*prefix_attributes=*/NULL_TREE,
34594 &pushed_scope);
34596 auto_node = type_uses_auto (TREE_TYPE (decl));
34597 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34599 if (cp_lexer_next_token_is (parser->lexer,
34600 CPP_OPEN_PAREN))
34602 if (code != CILK_SIMD && code != CILK_FOR)
34603 error ("parenthesized initialization is not allowed in "
34604 "OpenMP %<for%> loop");
34605 else
34606 error ("parenthesized initialization is "
34607 "not allowed in for-loop");
34609 else
34610 /* Trigger an error. */
34611 cp_parser_require (parser, CPP_EQ, RT_EQ);
34613 init = error_mark_node;
34614 cp_parser_skip_to_end_of_statement (parser);
34616 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34617 || type_dependent_expression_p (decl)
34618 || auto_node)
34620 bool is_direct_init, is_non_constant_init;
34622 init = cp_parser_initializer (parser,
34623 &is_direct_init,
34624 &is_non_constant_init);
34626 if (auto_node)
34628 TREE_TYPE (decl)
34629 = do_auto_deduction (TREE_TYPE (decl), init,
34630 auto_node);
34632 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34633 && !type_dependent_expression_p (decl))
34634 goto non_class;
34637 cp_finish_decl (decl, init, !is_non_constant_init,
34638 asm_specification,
34639 LOOKUP_ONLYCONVERTING);
34640 orig_init = init;
34641 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34643 vec_safe_push (for_block, this_pre_body);
34644 init = NULL_TREE;
34646 else
34648 init = pop_stmt_list (this_pre_body);
34649 if (init && TREE_CODE (init) == STATEMENT_LIST)
34651 tree_stmt_iterator i = tsi_start (init);
34652 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34653 while (!tsi_end_p (i))
34655 tree t = tsi_stmt (i);
34656 if (TREE_CODE (t) == DECL_EXPR
34657 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34659 tsi_delink (&i);
34660 vec_safe_push (for_block, t);
34661 continue;
34663 break;
34665 if (tsi_one_before_end_p (i))
34667 tree t = tsi_stmt (i);
34668 tsi_delink (&i);
34669 free_stmt_list (init);
34670 init = t;
34674 this_pre_body = NULL_TREE;
34676 else
34678 /* Consume '='. */
34679 cp_lexer_consume_token (parser->lexer);
34680 init = cp_parser_assignment_expression (parser);
34682 non_class:
34683 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34684 init = error_mark_node;
34685 else
34686 cp_finish_decl (decl, NULL_TREE,
34687 /*init_const_expr_p=*/false,
34688 asm_specification,
34689 LOOKUP_ONLYCONVERTING);
34692 if (pushed_scope)
34693 pop_scope (pushed_scope);
34696 else
34698 cp_id_kind idk;
34699 /* If parsing a type specifier sequence failed, then
34700 this MUST be a simple expression. */
34701 if (code == CILK_FOR)
34702 error ("%<_Cilk_for%> allows expression instead of declaration only "
34703 "in C, not in C++");
34704 cp_parser_parse_tentatively (parser);
34705 decl = cp_parser_primary_expression (parser, false, false,
34706 false, &idk);
34707 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34708 if (!cp_parser_error_occurred (parser)
34709 && decl
34710 && (TREE_CODE (decl) == COMPONENT_REF
34711 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34713 cp_parser_abort_tentative_parse (parser);
34714 cp_parser_parse_tentatively (parser);
34715 cp_token *token = cp_lexer_peek_token (parser->lexer);
34716 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34717 /*check_dependency_p=*/true,
34718 /*template_p=*/NULL,
34719 /*declarator_p=*/false,
34720 /*optional_p=*/false);
34721 if (name != error_mark_node
34722 && last_tok == cp_lexer_peek_token (parser->lexer))
34724 decl = cp_parser_lookup_name_simple (parser, name,
34725 token->location);
34726 if (TREE_CODE (decl) == FIELD_DECL)
34727 add_private_clause = omp_privatize_field (decl, false);
34729 cp_parser_abort_tentative_parse (parser);
34730 cp_parser_parse_tentatively (parser);
34731 decl = cp_parser_primary_expression (parser, false, false,
34732 false, &idk);
34734 if (!cp_parser_error_occurred (parser)
34735 && decl
34736 && DECL_P (decl)
34737 && CLASS_TYPE_P (TREE_TYPE (decl)))
34739 tree rhs;
34741 cp_parser_parse_definitely (parser);
34742 cp_parser_require (parser, CPP_EQ, RT_EQ);
34743 rhs = cp_parser_assignment_expression (parser);
34744 orig_init = rhs;
34745 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34746 decl, NOP_EXPR,
34747 rhs,
34748 tf_warning_or_error));
34749 if (!add_private_clause)
34750 add_private_clause = decl;
34752 else
34754 decl = NULL;
34755 cp_parser_abort_tentative_parse (parser);
34756 init = cp_parser_expression (parser);
34757 if (init)
34759 if (TREE_CODE (init) == MODIFY_EXPR
34760 || TREE_CODE (init) == MODOP_EXPR)
34761 real_decl = TREE_OPERAND (init, 0);
34765 return add_private_clause;
34768 /* Parse the restricted form of the for statement allowed by OpenMP. */
34770 static tree
34771 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34772 tree *cclauses, bool *if_p)
34774 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34775 tree real_decl, initv, condv, incrv, declv;
34776 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34777 location_t loc_first;
34778 bool collapse_err = false;
34779 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34780 vec<tree, va_gc> *for_block = make_tree_vector ();
34781 auto_vec<tree, 4> orig_inits;
34782 bool tiling = false;
34784 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34785 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34786 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34787 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34789 tiling = true;
34790 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34792 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34793 && OMP_CLAUSE_ORDERED_EXPR (cl))
34795 ordered_cl = cl;
34796 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34799 if (ordered && ordered < collapse)
34801 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34802 "%<ordered%> clause parameter is less than %<collapse%>");
34803 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34804 = build_int_cst (NULL_TREE, collapse);
34805 ordered = collapse;
34807 if (ordered)
34809 for (tree *pc = &clauses; *pc; )
34810 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34812 error_at (OMP_CLAUSE_LOCATION (*pc),
34813 "%<linear%> clause may not be specified together "
34814 "with %<ordered%> clause with a parameter");
34815 *pc = OMP_CLAUSE_CHAIN (*pc);
34817 else
34818 pc = &OMP_CLAUSE_CHAIN (*pc);
34821 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34822 count = ordered ? ordered : collapse;
34824 declv = make_tree_vec (count);
34825 initv = make_tree_vec (count);
34826 condv = make_tree_vec (count);
34827 incrv = make_tree_vec (count);
34829 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34831 for (i = 0; i < count; i++)
34833 int bracecount = 0;
34834 tree add_private_clause = NULL_TREE;
34835 location_t loc;
34837 if (code != CILK_FOR
34838 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34840 if (!collapse_err)
34841 cp_parser_error (parser, "for statement expected");
34842 return NULL;
34844 if (code == CILK_FOR
34845 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34847 if (!collapse_err)
34848 cp_parser_error (parser, "_Cilk_for statement expected");
34849 return NULL;
34851 loc = cp_lexer_consume_token (parser->lexer)->location;
34853 matching_parens parens;
34854 if (!parens.require_open (parser))
34855 return NULL;
34857 init = orig_init = decl = real_decl = NULL;
34858 this_pre_body = push_stmt_list ();
34860 add_private_clause
34861 = cp_parser_omp_for_loop_init (parser, code,
34862 this_pre_body, for_block,
34863 init, orig_init, decl, real_decl);
34865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34866 if (this_pre_body)
34868 this_pre_body = pop_stmt_list (this_pre_body);
34869 if (pre_body)
34871 tree t = pre_body;
34872 pre_body = push_stmt_list ();
34873 add_stmt (t);
34874 add_stmt (this_pre_body);
34875 pre_body = pop_stmt_list (pre_body);
34877 else
34878 pre_body = this_pre_body;
34881 if (decl)
34882 real_decl = decl;
34883 if (cclauses != NULL
34884 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34885 && real_decl != NULL_TREE)
34887 tree *c;
34888 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34889 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34890 && OMP_CLAUSE_DECL (*c) == real_decl)
34892 error_at (loc, "iteration variable %qD"
34893 " should not be firstprivate", real_decl);
34894 *c = OMP_CLAUSE_CHAIN (*c);
34896 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34897 && OMP_CLAUSE_DECL (*c) == real_decl)
34899 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34900 tree l = *c;
34901 *c = OMP_CLAUSE_CHAIN (*c);
34902 if (code == OMP_SIMD)
34904 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34905 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34907 else
34909 OMP_CLAUSE_CHAIN (l) = clauses;
34910 clauses = l;
34912 add_private_clause = NULL_TREE;
34914 else
34916 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34917 && OMP_CLAUSE_DECL (*c) == real_decl)
34918 add_private_clause = NULL_TREE;
34919 c = &OMP_CLAUSE_CHAIN (*c);
34923 if (add_private_clause)
34925 tree c;
34926 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34928 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34929 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34930 && OMP_CLAUSE_DECL (c) == decl)
34931 break;
34932 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34933 && OMP_CLAUSE_DECL (c) == decl)
34934 error_at (loc, "iteration variable %qD "
34935 "should not be firstprivate",
34936 decl);
34937 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34938 && OMP_CLAUSE_DECL (c) == decl)
34939 error_at (loc, "iteration variable %qD should not be reduction",
34940 decl);
34942 if (c == NULL)
34944 if (code != OMP_SIMD)
34945 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34946 else if (collapse == 1)
34947 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34948 else
34949 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34950 OMP_CLAUSE_DECL (c) = add_private_clause;
34951 c = finish_omp_clauses (c, C_ORT_OMP);
34952 if (c)
34954 OMP_CLAUSE_CHAIN (c) = clauses;
34955 clauses = c;
34956 /* For linear, signal that we need to fill up
34957 the so far unknown linear step. */
34958 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34959 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34964 cond = NULL;
34965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34966 cond = cp_parser_omp_for_cond (parser, decl, code);
34967 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34969 incr = NULL;
34970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34972 /* If decl is an iterator, preserve the operator on decl
34973 until finish_omp_for. */
34974 if (real_decl
34975 && ((processing_template_decl
34976 && (TREE_TYPE (real_decl) == NULL_TREE
34977 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34978 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34979 incr = cp_parser_omp_for_incr (parser, real_decl);
34980 else
34981 incr = cp_parser_expression (parser);
34982 if (!EXPR_HAS_LOCATION (incr))
34983 protected_set_expr_location (incr, input_location);
34986 if (!parens.require_close (parser))
34987 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34988 /*or_comma=*/false,
34989 /*consume_paren=*/true);
34991 TREE_VEC_ELT (declv, i) = decl;
34992 TREE_VEC_ELT (initv, i) = init;
34993 TREE_VEC_ELT (condv, i) = cond;
34994 TREE_VEC_ELT (incrv, i) = incr;
34995 if (orig_init)
34997 orig_inits.safe_grow_cleared (i + 1);
34998 orig_inits[i] = orig_init;
35001 if (i == count - 1)
35002 break;
35004 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35005 in between the collapsed for loops to be still considered perfectly
35006 nested. Hopefully the final version clarifies this.
35007 For now handle (multiple) {'s and empty statements. */
35008 cp_parser_parse_tentatively (parser);
35009 for (;;)
35011 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35012 break;
35013 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35015 cp_lexer_consume_token (parser->lexer);
35016 bracecount++;
35018 else if (bracecount
35019 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35020 cp_lexer_consume_token (parser->lexer);
35021 else
35023 loc = cp_lexer_peek_token (parser->lexer)->location;
35024 error_at (loc, "not enough for loops to collapse");
35025 collapse_err = true;
35026 cp_parser_abort_tentative_parse (parser);
35027 declv = NULL_TREE;
35028 break;
35032 if (declv)
35034 cp_parser_parse_definitely (parser);
35035 nbraces += bracecount;
35039 if (nbraces)
35040 if_p = NULL;
35042 /* Note that we saved the original contents of this flag when we entered
35043 the structured block, and so we don't need to re-save it here. */
35044 if (code == CILK_SIMD || code == CILK_FOR)
35045 parser->in_statement = IN_CILK_SIMD_FOR;
35046 else
35047 parser->in_statement = IN_OMP_FOR;
35049 /* Note that the grammar doesn't call for a structured block here,
35050 though the loop as a whole is a structured block. */
35051 body = push_stmt_list ();
35052 cp_parser_statement (parser, NULL_TREE, false, if_p);
35053 body = pop_stmt_list (body);
35055 if (declv == NULL_TREE)
35056 ret = NULL_TREE;
35057 else
35058 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35059 body, pre_body, &orig_inits, clauses);
35061 while (nbraces)
35063 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35065 cp_lexer_consume_token (parser->lexer);
35066 nbraces--;
35068 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35069 cp_lexer_consume_token (parser->lexer);
35070 else
35072 if (!collapse_err)
35074 error_at (cp_lexer_peek_token (parser->lexer)->location,
35075 "collapsed loops not perfectly nested");
35077 collapse_err = true;
35078 cp_parser_statement_seq_opt (parser, NULL);
35079 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35080 break;
35084 while (!for_block->is_empty ())
35086 tree t = for_block->pop ();
35087 if (TREE_CODE (t) == STATEMENT_LIST)
35088 add_stmt (pop_stmt_list (t));
35089 else
35090 add_stmt (t);
35092 release_tree_vector (for_block);
35094 return ret;
35097 /* Helper function for OpenMP parsing, split clauses and call
35098 finish_omp_clauses on each of the set of clauses afterwards. */
35100 static void
35101 cp_omp_split_clauses (location_t loc, enum tree_code code,
35102 omp_clause_mask mask, tree clauses, tree *cclauses)
35104 int i;
35105 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35106 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35107 if (cclauses[i])
35108 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35111 /* OpenMP 4.0:
35112 #pragma omp simd simd-clause[optseq] new-line
35113 for-loop */
35115 #define OMP_SIMD_CLAUSE_MASK \
35116 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35125 static tree
35126 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35127 char *p_name, omp_clause_mask mask, tree *cclauses,
35128 bool *if_p)
35130 tree clauses, sb, ret;
35131 unsigned int save;
35132 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35134 strcat (p_name, " simd");
35135 mask |= OMP_SIMD_CLAUSE_MASK;
35137 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35138 cclauses == NULL);
35139 if (cclauses)
35141 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35142 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35143 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35144 OMP_CLAUSE_ORDERED);
35145 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35147 error_at (OMP_CLAUSE_LOCATION (c),
35148 "%<ordered%> clause with parameter may not be specified "
35149 "on %qs construct", p_name);
35150 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35154 sb = begin_omp_structured_block ();
35155 save = cp_parser_begin_omp_structured_block (parser);
35157 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35159 cp_parser_end_omp_structured_block (parser, save);
35160 add_stmt (finish_omp_structured_block (sb));
35162 return ret;
35165 /* OpenMP 2.5:
35166 #pragma omp for for-clause[optseq] new-line
35167 for-loop
35169 OpenMP 4.0:
35170 #pragma omp for simd for-simd-clause[optseq] new-line
35171 for-loop */
35173 #define OMP_FOR_CLAUSE_MASK \
35174 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35184 static tree
35185 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35186 char *p_name, omp_clause_mask mask, tree *cclauses,
35187 bool *if_p)
35189 tree clauses, sb, ret;
35190 unsigned int save;
35191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35193 strcat (p_name, " for");
35194 mask |= OMP_FOR_CLAUSE_MASK;
35195 /* parallel for{, simd} disallows nowait clause, but for
35196 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35197 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35198 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35199 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35200 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35201 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35203 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35205 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35206 const char *p = IDENTIFIER_POINTER (id);
35208 if (strcmp (p, "simd") == 0)
35210 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35211 if (cclauses == NULL)
35212 cclauses = cclauses_buf;
35214 cp_lexer_consume_token (parser->lexer);
35215 if (!flag_openmp) /* flag_openmp_simd */
35216 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35217 cclauses, if_p);
35218 sb = begin_omp_structured_block ();
35219 save = cp_parser_begin_omp_structured_block (parser);
35220 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35221 cclauses, if_p);
35222 cp_parser_end_omp_structured_block (parser, save);
35223 tree body = finish_omp_structured_block (sb);
35224 if (ret == NULL)
35225 return ret;
35226 ret = make_node (OMP_FOR);
35227 TREE_TYPE (ret) = void_type_node;
35228 OMP_FOR_BODY (ret) = body;
35229 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35230 SET_EXPR_LOCATION (ret, loc);
35231 add_stmt (ret);
35232 return ret;
35235 if (!flag_openmp) /* flag_openmp_simd */
35237 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35238 return NULL_TREE;
35241 /* Composite distribute parallel for disallows linear clause. */
35242 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35243 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35245 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35246 cclauses == NULL);
35247 if (cclauses)
35249 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35250 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35253 sb = begin_omp_structured_block ();
35254 save = cp_parser_begin_omp_structured_block (parser);
35256 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35258 cp_parser_end_omp_structured_block (parser, save);
35259 add_stmt (finish_omp_structured_block (sb));
35261 return ret;
35264 /* OpenMP 2.5:
35265 # pragma omp master new-line
35266 structured-block */
35268 static tree
35269 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35271 cp_parser_require_pragma_eol (parser, pragma_tok);
35272 return c_finish_omp_master (input_location,
35273 cp_parser_omp_structured_block (parser, if_p));
35276 /* OpenMP 2.5:
35277 # pragma omp ordered new-line
35278 structured-block
35280 OpenMP 4.5:
35281 # pragma omp ordered ordered-clauses new-line
35282 structured-block */
35284 #define OMP_ORDERED_CLAUSE_MASK \
35285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35288 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35289 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35291 static bool
35292 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35293 enum pragma_context context, bool *if_p)
35295 location_t loc = pragma_tok->location;
35297 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35299 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35300 const char *p = IDENTIFIER_POINTER (id);
35302 if (strcmp (p, "depend") == 0)
35304 if (!flag_openmp) /* flag_openmp_simd */
35306 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35307 return false;
35309 if (context == pragma_stmt)
35311 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35312 "%<depend%> clause may only be used in compound "
35313 "statements");
35314 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35315 return false;
35317 tree clauses
35318 = cp_parser_omp_all_clauses (parser,
35319 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35320 "#pragma omp ordered", pragma_tok);
35321 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35322 return false;
35326 tree clauses
35327 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35328 "#pragma omp ordered", pragma_tok);
35330 if (!flag_openmp /* flag_openmp_simd */
35331 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35332 return false;
35334 c_finish_omp_ordered (loc, clauses,
35335 cp_parser_omp_structured_block (parser, if_p));
35336 return true;
35339 /* OpenMP 2.5:
35341 section-scope:
35342 { section-sequence }
35344 section-sequence:
35345 section-directive[opt] structured-block
35346 section-sequence section-directive structured-block */
35348 static tree
35349 cp_parser_omp_sections_scope (cp_parser *parser)
35351 tree stmt, substmt;
35352 bool error_suppress = false;
35353 cp_token *tok;
35355 matching_braces braces;
35356 if (!braces.require_open (parser))
35357 return NULL_TREE;
35359 stmt = push_stmt_list ();
35361 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35362 != PRAGMA_OMP_SECTION)
35364 substmt = cp_parser_omp_structured_block (parser, NULL);
35365 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35366 add_stmt (substmt);
35369 while (1)
35371 tok = cp_lexer_peek_token (parser->lexer);
35372 if (tok->type == CPP_CLOSE_BRACE)
35373 break;
35374 if (tok->type == CPP_EOF)
35375 break;
35377 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35379 cp_lexer_consume_token (parser->lexer);
35380 cp_parser_require_pragma_eol (parser, tok);
35381 error_suppress = false;
35383 else if (!error_suppress)
35385 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35386 error_suppress = true;
35389 substmt = cp_parser_omp_structured_block (parser, NULL);
35390 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35391 add_stmt (substmt);
35393 braces.require_close (parser);
35395 substmt = pop_stmt_list (stmt);
35397 stmt = make_node (OMP_SECTIONS);
35398 TREE_TYPE (stmt) = void_type_node;
35399 OMP_SECTIONS_BODY (stmt) = substmt;
35401 add_stmt (stmt);
35402 return stmt;
35405 /* OpenMP 2.5:
35406 # pragma omp sections sections-clause[optseq] newline
35407 sections-scope */
35409 #define OMP_SECTIONS_CLAUSE_MASK \
35410 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35416 static tree
35417 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35418 char *p_name, omp_clause_mask mask, tree *cclauses)
35420 tree clauses, ret;
35421 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35423 strcat (p_name, " sections");
35424 mask |= OMP_SECTIONS_CLAUSE_MASK;
35425 if (cclauses)
35426 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35428 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35429 cclauses == NULL);
35430 if (cclauses)
35432 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35433 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35436 ret = cp_parser_omp_sections_scope (parser);
35437 if (ret)
35438 OMP_SECTIONS_CLAUSES (ret) = clauses;
35440 return ret;
35443 /* OpenMP 2.5:
35444 # pragma omp parallel parallel-clause[optseq] new-line
35445 structured-block
35446 # pragma omp parallel for parallel-for-clause[optseq] new-line
35447 structured-block
35448 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35449 structured-block
35451 OpenMP 4.0:
35452 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35453 structured-block */
35455 #define OMP_PARALLEL_CLAUSE_MASK \
35456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35466 static tree
35467 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35468 char *p_name, omp_clause_mask mask, tree *cclauses,
35469 bool *if_p)
35471 tree stmt, clauses, block;
35472 unsigned int save;
35473 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35475 strcat (p_name, " parallel");
35476 mask |= OMP_PARALLEL_CLAUSE_MASK;
35477 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35478 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35479 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35480 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35484 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35485 if (cclauses == NULL)
35486 cclauses = cclauses_buf;
35488 cp_lexer_consume_token (parser->lexer);
35489 if (!flag_openmp) /* flag_openmp_simd */
35490 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35491 if_p);
35492 block = begin_omp_parallel ();
35493 save = cp_parser_begin_omp_structured_block (parser);
35494 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35495 if_p);
35496 cp_parser_end_omp_structured_block (parser, save);
35497 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35498 block);
35499 if (ret == NULL_TREE)
35500 return ret;
35501 OMP_PARALLEL_COMBINED (stmt) = 1;
35502 return stmt;
35504 /* When combined with distribute, parallel has to be followed by for.
35505 #pragma omp target parallel is allowed though. */
35506 else if (cclauses
35507 && (mask & (OMP_CLAUSE_MASK_1
35508 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35510 error_at (loc, "expected %<for%> after %qs", p_name);
35511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35512 return NULL_TREE;
35514 else if (!flag_openmp) /* flag_openmp_simd */
35516 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35517 return NULL_TREE;
35519 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35521 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35522 const char *p = IDENTIFIER_POINTER (id);
35523 if (strcmp (p, "sections") == 0)
35525 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35526 cclauses = cclauses_buf;
35528 cp_lexer_consume_token (parser->lexer);
35529 block = begin_omp_parallel ();
35530 save = cp_parser_begin_omp_structured_block (parser);
35531 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35532 cp_parser_end_omp_structured_block (parser, save);
35533 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35534 block);
35535 OMP_PARALLEL_COMBINED (stmt) = 1;
35536 return stmt;
35540 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35541 cclauses == NULL);
35542 if (cclauses)
35544 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35545 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35548 block = begin_omp_parallel ();
35549 save = cp_parser_begin_omp_structured_block (parser);
35550 cp_parser_statement (parser, NULL_TREE, false, if_p);
35551 cp_parser_end_omp_structured_block (parser, save);
35552 stmt = finish_omp_parallel (clauses, block);
35553 return stmt;
35556 /* OpenMP 2.5:
35557 # pragma omp single single-clause[optseq] new-line
35558 structured-block */
35560 #define OMP_SINGLE_CLAUSE_MASK \
35561 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35566 static tree
35567 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35569 tree stmt = make_node (OMP_SINGLE);
35570 TREE_TYPE (stmt) = void_type_node;
35572 OMP_SINGLE_CLAUSES (stmt)
35573 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35574 "#pragma omp single", pragma_tok);
35575 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35577 return add_stmt (stmt);
35580 /* OpenMP 3.0:
35581 # pragma omp task task-clause[optseq] new-line
35582 structured-block */
35584 #define OMP_TASK_CLAUSE_MASK \
35585 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35596 static tree
35597 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35599 tree clauses, block;
35600 unsigned int save;
35602 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35603 "#pragma omp task", pragma_tok);
35604 block = begin_omp_task ();
35605 save = cp_parser_begin_omp_structured_block (parser);
35606 cp_parser_statement (parser, NULL_TREE, false, if_p);
35607 cp_parser_end_omp_structured_block (parser, save);
35608 return finish_omp_task (clauses, block);
35611 /* OpenMP 3.0:
35612 # pragma omp taskwait new-line */
35614 static void
35615 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35617 cp_parser_require_pragma_eol (parser, pragma_tok);
35618 finish_omp_taskwait ();
35621 /* OpenMP 3.1:
35622 # pragma omp taskyield new-line */
35624 static void
35625 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35627 cp_parser_require_pragma_eol (parser, pragma_tok);
35628 finish_omp_taskyield ();
35631 /* OpenMP 4.0:
35632 # pragma omp taskgroup new-line
35633 structured-block */
35635 static tree
35636 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35638 cp_parser_require_pragma_eol (parser, pragma_tok);
35639 return c_finish_omp_taskgroup (input_location,
35640 cp_parser_omp_structured_block (parser,
35641 if_p));
35645 /* OpenMP 2.5:
35646 # pragma omp threadprivate (variable-list) */
35648 static void
35649 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35651 tree vars;
35653 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35654 cp_parser_require_pragma_eol (parser, pragma_tok);
35656 finish_omp_threadprivate (vars);
35659 /* OpenMP 4.0:
35660 # pragma omp cancel cancel-clause[optseq] new-line */
35662 #define OMP_CANCEL_CLAUSE_MASK \
35663 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35669 static void
35670 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35672 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35673 "#pragma omp cancel", pragma_tok);
35674 finish_omp_cancel (clauses);
35677 /* OpenMP 4.0:
35678 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35680 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35681 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35686 static void
35687 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35688 enum pragma_context context)
35690 tree clauses;
35691 bool point_seen = false;
35693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35695 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35696 const char *p = IDENTIFIER_POINTER (id);
35698 if (strcmp (p, "point") == 0)
35700 cp_lexer_consume_token (parser->lexer);
35701 point_seen = true;
35704 if (!point_seen)
35706 cp_parser_error (parser, "expected %<point%>");
35707 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35708 return;
35711 if (context != pragma_compound)
35713 if (context == pragma_stmt)
35714 error_at (pragma_tok->location,
35715 "%<#pragma %s%> may only be used in compound statements",
35716 "omp cancellation point");
35717 else
35718 cp_parser_error (parser, "expected declaration specifiers");
35719 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35720 return;
35723 clauses = cp_parser_omp_all_clauses (parser,
35724 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35725 "#pragma omp cancellation point",
35726 pragma_tok);
35727 finish_omp_cancellation_point (clauses);
35730 /* OpenMP 4.0:
35731 #pragma omp distribute distribute-clause[optseq] new-line
35732 for-loop */
35734 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35741 static tree
35742 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35743 char *p_name, omp_clause_mask mask, tree *cclauses,
35744 bool *if_p)
35746 tree clauses, sb, ret;
35747 unsigned int save;
35748 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35750 strcat (p_name, " distribute");
35751 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35755 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35756 const char *p = IDENTIFIER_POINTER (id);
35757 bool simd = false;
35758 bool parallel = false;
35760 if (strcmp (p, "simd") == 0)
35761 simd = true;
35762 else
35763 parallel = strcmp (p, "parallel") == 0;
35764 if (parallel || simd)
35766 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35767 if (cclauses == NULL)
35768 cclauses = cclauses_buf;
35769 cp_lexer_consume_token (parser->lexer);
35770 if (!flag_openmp) /* flag_openmp_simd */
35772 if (simd)
35773 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35774 cclauses, if_p);
35775 else
35776 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35777 cclauses, if_p);
35779 sb = begin_omp_structured_block ();
35780 save = cp_parser_begin_omp_structured_block (parser);
35781 if (simd)
35782 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35783 cclauses, if_p);
35784 else
35785 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35786 cclauses, if_p);
35787 cp_parser_end_omp_structured_block (parser, save);
35788 tree body = finish_omp_structured_block (sb);
35789 if (ret == NULL)
35790 return ret;
35791 ret = make_node (OMP_DISTRIBUTE);
35792 TREE_TYPE (ret) = void_type_node;
35793 OMP_FOR_BODY (ret) = body;
35794 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35795 SET_EXPR_LOCATION (ret, loc);
35796 add_stmt (ret);
35797 return ret;
35800 if (!flag_openmp) /* flag_openmp_simd */
35802 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35803 return NULL_TREE;
35806 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35807 cclauses == NULL);
35808 if (cclauses)
35810 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35811 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35814 sb = begin_omp_structured_block ();
35815 save = cp_parser_begin_omp_structured_block (parser);
35817 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35819 cp_parser_end_omp_structured_block (parser, save);
35820 add_stmt (finish_omp_structured_block (sb));
35822 return ret;
35825 /* OpenMP 4.0:
35826 # pragma omp teams teams-clause[optseq] new-line
35827 structured-block */
35829 #define OMP_TEAMS_CLAUSE_MASK \
35830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35838 static tree
35839 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35840 char *p_name, omp_clause_mask mask, tree *cclauses,
35841 bool *if_p)
35843 tree clauses, sb, ret;
35844 unsigned int save;
35845 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35847 strcat (p_name, " teams");
35848 mask |= OMP_TEAMS_CLAUSE_MASK;
35850 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35852 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35853 const char *p = IDENTIFIER_POINTER (id);
35854 if (strcmp (p, "distribute") == 0)
35856 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35857 if (cclauses == NULL)
35858 cclauses = cclauses_buf;
35860 cp_lexer_consume_token (parser->lexer);
35861 if (!flag_openmp) /* flag_openmp_simd */
35862 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35863 cclauses, if_p);
35864 sb = begin_omp_structured_block ();
35865 save = cp_parser_begin_omp_structured_block (parser);
35866 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35867 cclauses, if_p);
35868 cp_parser_end_omp_structured_block (parser, save);
35869 tree body = finish_omp_structured_block (sb);
35870 if (ret == NULL)
35871 return ret;
35872 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35873 ret = make_node (OMP_TEAMS);
35874 TREE_TYPE (ret) = void_type_node;
35875 OMP_TEAMS_CLAUSES (ret) = clauses;
35876 OMP_TEAMS_BODY (ret) = body;
35877 OMP_TEAMS_COMBINED (ret) = 1;
35878 SET_EXPR_LOCATION (ret, loc);
35879 return add_stmt (ret);
35882 if (!flag_openmp) /* flag_openmp_simd */
35884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35885 return NULL_TREE;
35888 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35889 cclauses == NULL);
35890 if (cclauses)
35892 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35893 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35896 tree stmt = make_node (OMP_TEAMS);
35897 TREE_TYPE (stmt) = void_type_node;
35898 OMP_TEAMS_CLAUSES (stmt) = clauses;
35899 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35900 SET_EXPR_LOCATION (stmt, loc);
35902 return add_stmt (stmt);
35905 /* OpenMP 4.0:
35906 # pragma omp target data target-data-clause[optseq] new-line
35907 structured-block */
35909 #define OMP_TARGET_DATA_CLAUSE_MASK \
35910 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35915 static tree
35916 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35918 tree clauses
35919 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35920 "#pragma omp target data", pragma_tok);
35921 int map_seen = 0;
35922 for (tree *pc = &clauses; *pc;)
35924 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35925 switch (OMP_CLAUSE_MAP_KIND (*pc))
35927 case GOMP_MAP_TO:
35928 case GOMP_MAP_ALWAYS_TO:
35929 case GOMP_MAP_FROM:
35930 case GOMP_MAP_ALWAYS_FROM:
35931 case GOMP_MAP_TOFROM:
35932 case GOMP_MAP_ALWAYS_TOFROM:
35933 case GOMP_MAP_ALLOC:
35934 map_seen = 3;
35935 break;
35936 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35937 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35938 case GOMP_MAP_ALWAYS_POINTER:
35939 break;
35940 default:
35941 map_seen |= 1;
35942 error_at (OMP_CLAUSE_LOCATION (*pc),
35943 "%<#pragma omp target data%> with map-type other "
35944 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35945 "on %<map%> clause");
35946 *pc = OMP_CLAUSE_CHAIN (*pc);
35947 continue;
35949 pc = &OMP_CLAUSE_CHAIN (*pc);
35952 if (map_seen != 3)
35954 if (map_seen == 0)
35955 error_at (pragma_tok->location,
35956 "%<#pragma omp target data%> must contain at least "
35957 "one %<map%> clause");
35958 return NULL_TREE;
35961 tree stmt = make_node (OMP_TARGET_DATA);
35962 TREE_TYPE (stmt) = void_type_node;
35963 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35965 keep_next_level (true);
35966 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35968 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35969 return add_stmt (stmt);
35972 /* OpenMP 4.5:
35973 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35974 structured-block */
35976 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35977 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35983 static tree
35984 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35985 enum pragma_context context)
35987 bool data_seen = false;
35988 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35990 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35991 const char *p = IDENTIFIER_POINTER (id);
35993 if (strcmp (p, "data") == 0)
35995 cp_lexer_consume_token (parser->lexer);
35996 data_seen = true;
35999 if (!data_seen)
36001 cp_parser_error (parser, "expected %<data%>");
36002 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36003 return NULL_TREE;
36006 if (context == pragma_stmt)
36008 error_at (pragma_tok->location,
36009 "%<#pragma %s%> may only be used in compound statements",
36010 "omp target enter data");
36011 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36012 return NULL_TREE;
36015 tree clauses
36016 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36017 "#pragma omp target enter data", pragma_tok);
36018 int map_seen = 0;
36019 for (tree *pc = &clauses; *pc;)
36021 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36022 switch (OMP_CLAUSE_MAP_KIND (*pc))
36024 case GOMP_MAP_TO:
36025 case GOMP_MAP_ALWAYS_TO:
36026 case GOMP_MAP_ALLOC:
36027 map_seen = 3;
36028 break;
36029 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36030 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36031 case GOMP_MAP_ALWAYS_POINTER:
36032 break;
36033 default:
36034 map_seen |= 1;
36035 error_at (OMP_CLAUSE_LOCATION (*pc),
36036 "%<#pragma omp target enter data%> with map-type other "
36037 "than %<to%> or %<alloc%> on %<map%> clause");
36038 *pc = OMP_CLAUSE_CHAIN (*pc);
36039 continue;
36041 pc = &OMP_CLAUSE_CHAIN (*pc);
36044 if (map_seen != 3)
36046 if (map_seen == 0)
36047 error_at (pragma_tok->location,
36048 "%<#pragma omp target enter data%> must contain at least "
36049 "one %<map%> clause");
36050 return NULL_TREE;
36053 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36054 TREE_TYPE (stmt) = void_type_node;
36055 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36056 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36057 return add_stmt (stmt);
36060 /* OpenMP 4.5:
36061 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36062 structured-block */
36064 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36065 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36071 static tree
36072 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36073 enum pragma_context context)
36075 bool data_seen = false;
36076 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36078 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36079 const char *p = IDENTIFIER_POINTER (id);
36081 if (strcmp (p, "data") == 0)
36083 cp_lexer_consume_token (parser->lexer);
36084 data_seen = true;
36087 if (!data_seen)
36089 cp_parser_error (parser, "expected %<data%>");
36090 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36091 return NULL_TREE;
36094 if (context == pragma_stmt)
36096 error_at (pragma_tok->location,
36097 "%<#pragma %s%> may only be used in compound statements",
36098 "omp target exit data");
36099 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36100 return NULL_TREE;
36103 tree clauses
36104 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36105 "#pragma omp target exit data", pragma_tok);
36106 int map_seen = 0;
36107 for (tree *pc = &clauses; *pc;)
36109 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36110 switch (OMP_CLAUSE_MAP_KIND (*pc))
36112 case GOMP_MAP_FROM:
36113 case GOMP_MAP_ALWAYS_FROM:
36114 case GOMP_MAP_RELEASE:
36115 case GOMP_MAP_DELETE:
36116 map_seen = 3;
36117 break;
36118 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36119 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36120 case GOMP_MAP_ALWAYS_POINTER:
36121 break;
36122 default:
36123 map_seen |= 1;
36124 error_at (OMP_CLAUSE_LOCATION (*pc),
36125 "%<#pragma omp target exit data%> with map-type other "
36126 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36127 " clause");
36128 *pc = OMP_CLAUSE_CHAIN (*pc);
36129 continue;
36131 pc = &OMP_CLAUSE_CHAIN (*pc);
36134 if (map_seen != 3)
36136 if (map_seen == 0)
36137 error_at (pragma_tok->location,
36138 "%<#pragma omp target exit data%> must contain at least "
36139 "one %<map%> clause");
36140 return NULL_TREE;
36143 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36144 TREE_TYPE (stmt) = void_type_node;
36145 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36146 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36147 return add_stmt (stmt);
36150 /* OpenMP 4.0:
36151 # pragma omp target update target-update-clause[optseq] new-line */
36153 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36161 static bool
36162 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36163 enum pragma_context context)
36165 if (context == pragma_stmt)
36167 error_at (pragma_tok->location,
36168 "%<#pragma %s%> may only be used in compound statements",
36169 "omp target update");
36170 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36171 return false;
36174 tree clauses
36175 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36176 "#pragma omp target update", pragma_tok);
36177 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36178 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36180 error_at (pragma_tok->location,
36181 "%<#pragma omp target update%> must contain at least one "
36182 "%<from%> or %<to%> clauses");
36183 return false;
36186 tree stmt = make_node (OMP_TARGET_UPDATE);
36187 TREE_TYPE (stmt) = void_type_node;
36188 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36189 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36190 add_stmt (stmt);
36191 return false;
36194 /* OpenMP 4.0:
36195 # pragma omp target target-clause[optseq] new-line
36196 structured-block */
36198 #define OMP_TARGET_CLAUSE_MASK \
36199 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36207 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36209 static bool
36210 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36211 enum pragma_context context, bool *if_p)
36213 tree *pc = NULL, stmt;
36215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36217 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36218 const char *p = IDENTIFIER_POINTER (id);
36219 enum tree_code ccode = ERROR_MARK;
36221 if (strcmp (p, "teams") == 0)
36222 ccode = OMP_TEAMS;
36223 else if (strcmp (p, "parallel") == 0)
36224 ccode = OMP_PARALLEL;
36225 else if (strcmp (p, "simd") == 0)
36226 ccode = OMP_SIMD;
36227 if (ccode != ERROR_MARK)
36229 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36230 char p_name[sizeof ("#pragma omp target teams distribute "
36231 "parallel for simd")];
36233 cp_lexer_consume_token (parser->lexer);
36234 strcpy (p_name, "#pragma omp target");
36235 if (!flag_openmp) /* flag_openmp_simd */
36237 tree stmt;
36238 switch (ccode)
36240 case OMP_TEAMS:
36241 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36242 OMP_TARGET_CLAUSE_MASK,
36243 cclauses, if_p);
36244 break;
36245 case OMP_PARALLEL:
36246 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36247 OMP_TARGET_CLAUSE_MASK,
36248 cclauses, if_p);
36249 break;
36250 case OMP_SIMD:
36251 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36252 OMP_TARGET_CLAUSE_MASK,
36253 cclauses, if_p);
36254 break;
36255 default:
36256 gcc_unreachable ();
36258 return stmt != NULL_TREE;
36260 keep_next_level (true);
36261 tree sb = begin_omp_structured_block (), ret;
36262 unsigned save = cp_parser_begin_omp_structured_block (parser);
36263 switch (ccode)
36265 case OMP_TEAMS:
36266 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36267 OMP_TARGET_CLAUSE_MASK, cclauses,
36268 if_p);
36269 break;
36270 case OMP_PARALLEL:
36271 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36272 OMP_TARGET_CLAUSE_MASK, cclauses,
36273 if_p);
36274 break;
36275 case OMP_SIMD:
36276 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36277 OMP_TARGET_CLAUSE_MASK, cclauses,
36278 if_p);
36279 break;
36280 default:
36281 gcc_unreachable ();
36283 cp_parser_end_omp_structured_block (parser, save);
36284 tree body = finish_omp_structured_block (sb);
36285 if (ret == NULL_TREE)
36286 return false;
36287 if (ccode == OMP_TEAMS && !processing_template_decl)
36289 /* For combined target teams, ensure the num_teams and
36290 thread_limit clause expressions are evaluated on the host,
36291 before entering the target construct. */
36292 tree c;
36293 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36294 c; c = OMP_CLAUSE_CHAIN (c))
36295 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36296 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36297 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36299 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36300 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36301 if (expr == error_mark_node)
36302 continue;
36303 tree tmp = TARGET_EXPR_SLOT (expr);
36304 add_stmt (expr);
36305 OMP_CLAUSE_OPERAND (c, 0) = expr;
36306 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36307 OMP_CLAUSE_FIRSTPRIVATE);
36308 OMP_CLAUSE_DECL (tc) = tmp;
36309 OMP_CLAUSE_CHAIN (tc)
36310 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36311 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36314 tree stmt = make_node (OMP_TARGET);
36315 TREE_TYPE (stmt) = void_type_node;
36316 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36317 OMP_TARGET_BODY (stmt) = body;
36318 OMP_TARGET_COMBINED (stmt) = 1;
36319 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36320 add_stmt (stmt);
36321 pc = &OMP_TARGET_CLAUSES (stmt);
36322 goto check_clauses;
36324 else if (!flag_openmp) /* flag_openmp_simd */
36326 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36327 return false;
36329 else if (strcmp (p, "data") == 0)
36331 cp_lexer_consume_token (parser->lexer);
36332 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36333 return true;
36335 else if (strcmp (p, "enter") == 0)
36337 cp_lexer_consume_token (parser->lexer);
36338 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36339 return false;
36341 else if (strcmp (p, "exit") == 0)
36343 cp_lexer_consume_token (parser->lexer);
36344 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36345 return false;
36347 else if (strcmp (p, "update") == 0)
36349 cp_lexer_consume_token (parser->lexer);
36350 return cp_parser_omp_target_update (parser, pragma_tok, context);
36353 if (!flag_openmp) /* flag_openmp_simd */
36355 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36356 return false;
36359 stmt = make_node (OMP_TARGET);
36360 TREE_TYPE (stmt) = void_type_node;
36362 OMP_TARGET_CLAUSES (stmt)
36363 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36364 "#pragma omp target", pragma_tok);
36365 pc = &OMP_TARGET_CLAUSES (stmt);
36366 keep_next_level (true);
36367 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36369 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36370 add_stmt (stmt);
36372 check_clauses:
36373 while (*pc)
36375 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36376 switch (OMP_CLAUSE_MAP_KIND (*pc))
36378 case GOMP_MAP_TO:
36379 case GOMP_MAP_ALWAYS_TO:
36380 case GOMP_MAP_FROM:
36381 case GOMP_MAP_ALWAYS_FROM:
36382 case GOMP_MAP_TOFROM:
36383 case GOMP_MAP_ALWAYS_TOFROM:
36384 case GOMP_MAP_ALLOC:
36385 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36386 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36387 case GOMP_MAP_ALWAYS_POINTER:
36388 break;
36389 default:
36390 error_at (OMP_CLAUSE_LOCATION (*pc),
36391 "%<#pragma omp target%> with map-type other "
36392 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36393 "on %<map%> clause");
36394 *pc = OMP_CLAUSE_CHAIN (*pc);
36395 continue;
36397 pc = &OMP_CLAUSE_CHAIN (*pc);
36399 return true;
36402 /* OpenACC 2.0:
36403 # pragma acc cache (variable-list) new-line
36406 static tree
36407 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36409 tree stmt, clauses;
36411 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36412 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36414 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36416 stmt = make_node (OACC_CACHE);
36417 TREE_TYPE (stmt) = void_type_node;
36418 OACC_CACHE_CLAUSES (stmt) = clauses;
36419 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36420 add_stmt (stmt);
36422 return stmt;
36425 /* OpenACC 2.0:
36426 # pragma acc data oacc-data-clause[optseq] new-line
36427 structured-block */
36429 #define OACC_DATA_CLAUSE_MASK \
36430 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36433 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36442 static tree
36443 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36445 tree stmt, clauses, block;
36446 unsigned int save;
36448 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36449 "#pragma acc data", pragma_tok);
36451 block = begin_omp_parallel ();
36452 save = cp_parser_begin_omp_structured_block (parser);
36453 cp_parser_statement (parser, NULL_TREE, false, if_p);
36454 cp_parser_end_omp_structured_block (parser, save);
36455 stmt = finish_oacc_data (clauses, block);
36456 return stmt;
36459 /* OpenACC 2.0:
36460 # pragma acc host_data <clauses> new-line
36461 structured-block */
36463 #define OACC_HOST_DATA_CLAUSE_MASK \
36464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36466 static tree
36467 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36469 tree stmt, clauses, block;
36470 unsigned int save;
36472 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36473 "#pragma acc host_data", pragma_tok);
36475 block = begin_omp_parallel ();
36476 save = cp_parser_begin_omp_structured_block (parser);
36477 cp_parser_statement (parser, NULL_TREE, false, if_p);
36478 cp_parser_end_omp_structured_block (parser, save);
36479 stmt = finish_oacc_host_data (clauses, block);
36480 return stmt;
36483 /* OpenACC 2.0:
36484 # pragma acc declare oacc-data-clause[optseq] new-line
36487 #define OACC_DECLARE_CLAUSE_MASK \
36488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36501 static tree
36502 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36504 tree clauses, stmt;
36505 bool error = false;
36507 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36508 "#pragma acc declare", pragma_tok, true);
36511 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36513 error_at (pragma_tok->location,
36514 "no valid clauses specified in %<#pragma acc declare%>");
36515 return NULL_TREE;
36518 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36520 location_t loc = OMP_CLAUSE_LOCATION (t);
36521 tree decl = OMP_CLAUSE_DECL (t);
36522 if (!DECL_P (decl))
36524 error_at (loc, "array section in %<#pragma acc declare%>");
36525 error = true;
36526 continue;
36528 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36529 switch (OMP_CLAUSE_MAP_KIND (t))
36531 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36532 case GOMP_MAP_FORCE_ALLOC:
36533 case GOMP_MAP_FORCE_TO:
36534 case GOMP_MAP_FORCE_DEVICEPTR:
36535 case GOMP_MAP_DEVICE_RESIDENT:
36536 break;
36538 case GOMP_MAP_LINK:
36539 if (!global_bindings_p ()
36540 && (TREE_STATIC (decl)
36541 || !DECL_EXTERNAL (decl)))
36543 error_at (loc,
36544 "%qD must be a global variable in "
36545 "%<#pragma acc declare link%>",
36546 decl);
36547 error = true;
36548 continue;
36550 break;
36552 default:
36553 if (global_bindings_p ())
36555 error_at (loc, "invalid OpenACC clause at file scope");
36556 error = true;
36557 continue;
36559 if (DECL_EXTERNAL (decl))
36561 error_at (loc,
36562 "invalid use of %<extern%> variable %qD "
36563 "in %<#pragma acc declare%>", decl);
36564 error = true;
36565 continue;
36567 else if (TREE_PUBLIC (decl))
36569 error_at (loc,
36570 "invalid use of %<global%> variable %qD "
36571 "in %<#pragma acc declare%>", decl);
36572 error = true;
36573 continue;
36575 break;
36578 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36579 || lookup_attribute ("omp declare target link",
36580 DECL_ATTRIBUTES (decl)))
36582 error_at (loc, "variable %qD used more than once with "
36583 "%<#pragma acc declare%>", decl);
36584 error = true;
36585 continue;
36588 if (!error)
36590 tree id;
36592 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36593 id = get_identifier ("omp declare target link");
36594 else
36595 id = get_identifier ("omp declare target");
36597 DECL_ATTRIBUTES (decl)
36598 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36599 if (global_bindings_p ())
36601 symtab_node *node = symtab_node::get (decl);
36602 if (node != NULL)
36604 node->offloadable = 1;
36605 if (ENABLE_OFFLOADING)
36607 g->have_offload = true;
36608 if (is_a <varpool_node *> (node))
36609 vec_safe_push (offload_vars, decl);
36616 if (error || global_bindings_p ())
36617 return NULL_TREE;
36619 stmt = make_node (OACC_DECLARE);
36620 TREE_TYPE (stmt) = void_type_node;
36621 OACC_DECLARE_CLAUSES (stmt) = clauses;
36622 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36624 add_stmt (stmt);
36626 return NULL_TREE;
36629 /* OpenACC 2.0:
36630 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36634 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36636 LOC is the location of the #pragma token.
36639 #define OACC_ENTER_DATA_CLAUSE_MASK \
36640 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36648 #define OACC_EXIT_DATA_CLAUSE_MASK \
36649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36655 static tree
36656 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36657 bool enter)
36659 location_t loc = pragma_tok->location;
36660 tree stmt, clauses;
36661 const char *p = "";
36663 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36664 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36666 if (strcmp (p, "data") != 0)
36668 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36669 enter ? "enter" : "exit");
36670 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36671 return NULL_TREE;
36674 cp_lexer_consume_token (parser->lexer);
36676 if (enter)
36677 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36678 "#pragma acc enter data", pragma_tok);
36679 else
36680 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36681 "#pragma acc exit data", pragma_tok);
36683 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36685 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36686 enter ? "enter" : "exit");
36687 return NULL_TREE;
36690 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36691 TREE_TYPE (stmt) = void_type_node;
36692 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36693 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36694 add_stmt (stmt);
36695 return stmt;
36698 /* OpenACC 2.0:
36699 # pragma acc loop oacc-loop-clause[optseq] new-line
36700 structured-block */
36702 #define OACC_LOOP_CLAUSE_MASK \
36703 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36714 static tree
36715 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36716 omp_clause_mask mask, tree *cclauses, bool *if_p)
36718 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36720 strcat (p_name, " loop");
36721 mask |= OACC_LOOP_CLAUSE_MASK;
36723 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36724 cclauses == NULL);
36725 if (cclauses)
36727 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36728 if (*cclauses)
36729 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36730 if (clauses)
36731 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36734 tree block = begin_omp_structured_block ();
36735 int save = cp_parser_begin_omp_structured_block (parser);
36736 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36737 cp_parser_end_omp_structured_block (parser, save);
36738 add_stmt (finish_omp_structured_block (block));
36740 return stmt;
36743 /* OpenACC 2.0:
36744 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36745 structured-block
36749 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36750 structured-block
36753 #define OACC_KERNELS_CLAUSE_MASK \
36754 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36772 #define OACC_PARALLEL_CLAUSE_MASK \
36773 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36794 static tree
36795 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36796 char *p_name, bool *if_p)
36798 omp_clause_mask mask;
36799 enum tree_code code;
36800 switch (cp_parser_pragma_kind (pragma_tok))
36802 case PRAGMA_OACC_KERNELS:
36803 strcat (p_name, " kernels");
36804 mask = OACC_KERNELS_CLAUSE_MASK;
36805 code = OACC_KERNELS;
36806 break;
36807 case PRAGMA_OACC_PARALLEL:
36808 strcat (p_name, " parallel");
36809 mask = OACC_PARALLEL_CLAUSE_MASK;
36810 code = OACC_PARALLEL;
36811 break;
36812 default:
36813 gcc_unreachable ();
36816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36818 const char *p
36819 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36820 if (strcmp (p, "loop") == 0)
36822 cp_lexer_consume_token (parser->lexer);
36823 tree block = begin_omp_parallel ();
36824 tree clauses;
36825 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36826 if_p);
36827 return finish_omp_construct (code, block, clauses);
36831 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36833 tree block = begin_omp_parallel ();
36834 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36835 cp_parser_statement (parser, NULL_TREE, false, if_p);
36836 cp_parser_end_omp_structured_block (parser, save);
36837 return finish_omp_construct (code, block, clauses);
36840 /* OpenACC 2.0:
36841 # pragma acc update oacc-update-clause[optseq] new-line
36844 #define OACC_UPDATE_CLAUSE_MASK \
36845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36852 static tree
36853 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36855 tree stmt, clauses;
36857 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36858 "#pragma acc update", pragma_tok);
36860 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36862 error_at (pragma_tok->location,
36863 "%<#pragma acc update%> must contain at least one "
36864 "%<device%> or %<host%> or %<self%> clause");
36865 return NULL_TREE;
36868 stmt = make_node (OACC_UPDATE);
36869 TREE_TYPE (stmt) = void_type_node;
36870 OACC_UPDATE_CLAUSES (stmt) = clauses;
36871 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36872 add_stmt (stmt);
36873 return stmt;
36876 /* OpenACC 2.0:
36877 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36879 LOC is the location of the #pragma token.
36882 #define OACC_WAIT_CLAUSE_MASK \
36883 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36885 static tree
36886 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36888 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36889 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36891 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36892 list = cp_parser_oacc_wait_list (parser, loc, list);
36894 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36895 "#pragma acc wait", pragma_tok);
36897 stmt = c_finish_oacc_wait (loc, list, clauses);
36898 stmt = finish_expr_stmt (stmt);
36900 return stmt;
36903 /* OpenMP 4.0:
36904 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36906 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36907 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36914 static void
36915 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36916 enum pragma_context context)
36918 bool first_p = parser->omp_declare_simd == NULL;
36919 cp_omp_declare_simd_data data;
36920 if (first_p)
36922 data.error_seen = false;
36923 data.fndecl_seen = false;
36924 data.tokens = vNULL;
36925 data.clauses = NULL_TREE;
36926 /* It is safe to take the address of a local variable; it will only be
36927 used while this scope is live. */
36928 parser->omp_declare_simd = &data;
36931 /* Store away all pragma tokens. */
36932 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36933 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36934 cp_lexer_consume_token (parser->lexer);
36935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36936 parser->omp_declare_simd->error_seen = true;
36937 cp_parser_require_pragma_eol (parser, pragma_tok);
36938 struct cp_token_cache *cp
36939 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36940 parser->omp_declare_simd->tokens.safe_push (cp);
36942 if (first_p)
36944 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36945 cp_parser_pragma (parser, context, NULL);
36946 switch (context)
36948 case pragma_external:
36949 cp_parser_declaration (parser);
36950 break;
36951 case pragma_member:
36952 cp_parser_member_declaration (parser);
36953 break;
36954 case pragma_objc_icode:
36955 cp_parser_block_declaration (parser, /*statement_p=*/false);
36956 break;
36957 default:
36958 cp_parser_declaration_statement (parser);
36959 break;
36961 if (parser->omp_declare_simd
36962 && !parser->omp_declare_simd->error_seen
36963 && !parser->omp_declare_simd->fndecl_seen)
36964 error_at (pragma_tok->location,
36965 "%<#pragma omp declare simd%> not immediately followed by "
36966 "function declaration or definition");
36967 data.tokens.release ();
36968 parser->omp_declare_simd = NULL;
36972 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36973 This function is modelled similar to the late parsing of omp declare
36974 simd. */
36976 static tree
36977 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36979 struct cp_token_cache *ce;
36980 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36981 int ii = 0;
36983 if (parser->omp_declare_simd != NULL
36984 || lookup_attribute ("simd", attrs))
36986 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36987 "used in the same function marked as a Cilk Plus SIMD-enabled "
36988 "function");
36989 parser->cilk_simd_fn_info->tokens.release ();
36990 XDELETE (parser->cilk_simd_fn_info);
36991 parser->cilk_simd_fn_info = NULL;
36992 return attrs;
36994 if (!info->error_seen && info->fndecl_seen)
36996 error ("vector attribute not immediately followed by a single function"
36997 " declaration or definition");
36998 info->error_seen = true;
37000 if (info->error_seen)
37001 return attrs;
37003 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37005 tree c, cl;
37007 cp_parser_push_lexer_for_tokens (parser, ce);
37008 parser->lexer->in_pragma = true;
37009 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37010 "SIMD-enabled functions attribute",
37011 NULL);
37012 cp_parser_pop_lexer (parser);
37013 if (cl)
37014 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37016 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37017 TREE_CHAIN (c) = attrs;
37018 attrs = c;
37020 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37021 TREE_CHAIN (c) = attrs;
37022 if (processing_template_decl)
37023 ATTR_IS_DEPENDENT (c) = 1;
37024 attrs = c;
37026 info->fndecl_seen = true;
37027 parser->cilk_simd_fn_info->tokens.release ();
37028 XDELETE (parser->cilk_simd_fn_info);
37029 parser->cilk_simd_fn_info = NULL;
37030 return attrs;
37033 /* Finalize #pragma omp declare simd clauses after direct declarator has
37034 been parsed, and put that into "omp declare simd" attribute. */
37036 static tree
37037 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37039 struct cp_token_cache *ce;
37040 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37041 int i;
37043 if (!data->error_seen && data->fndecl_seen)
37045 error ("%<#pragma omp declare simd%> not immediately followed by "
37046 "a single function declaration or definition");
37047 data->error_seen = true;
37049 if (data->error_seen)
37050 return attrs;
37052 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37054 tree c, cl;
37056 cp_parser_push_lexer_for_tokens (parser, ce);
37057 parser->lexer->in_pragma = true;
37058 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37059 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37060 cp_lexer_consume_token (parser->lexer);
37061 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37062 "#pragma omp declare simd", pragma_tok);
37063 cp_parser_pop_lexer (parser);
37064 if (cl)
37065 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37066 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37067 TREE_CHAIN (c) = attrs;
37068 if (processing_template_decl)
37069 ATTR_IS_DEPENDENT (c) = 1;
37070 attrs = c;
37073 data->fndecl_seen = true;
37074 return attrs;
37078 /* OpenMP 4.0:
37079 # pragma omp declare target new-line
37080 declarations and definitions
37081 # pragma omp end declare target new-line
37083 OpenMP 4.5:
37084 # pragma omp declare target ( extended-list ) new-line
37086 # pragma omp declare target declare-target-clauses[seq] new-line */
37088 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37092 static void
37093 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37095 tree clauses = NULL_TREE;
37096 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37097 clauses
37098 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37099 "#pragma omp declare target", pragma_tok);
37100 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37102 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37103 clauses);
37104 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37105 cp_parser_require_pragma_eol (parser, pragma_tok);
37107 else
37109 cp_parser_require_pragma_eol (parser, pragma_tok);
37110 scope_chain->omp_declare_target_attribute++;
37111 return;
37113 if (scope_chain->omp_declare_target_attribute)
37114 error_at (pragma_tok->location,
37115 "%<#pragma omp declare target%> with clauses in between "
37116 "%<#pragma omp declare target%> without clauses and "
37117 "%<#pragma omp end declare target%>");
37118 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37120 tree t = OMP_CLAUSE_DECL (c), id;
37121 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37122 tree at2 = lookup_attribute ("omp declare target link",
37123 DECL_ATTRIBUTES (t));
37124 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37126 id = get_identifier ("omp declare target link");
37127 std::swap (at1, at2);
37129 else
37130 id = get_identifier ("omp declare target");
37131 if (at2)
37133 error_at (OMP_CLAUSE_LOCATION (c),
37134 "%qD specified both in declare target %<link%> and %<to%>"
37135 " clauses", t);
37136 continue;
37138 if (!at1)
37140 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37141 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37142 continue;
37144 symtab_node *node = symtab_node::get (t);
37145 if (node != NULL)
37147 node->offloadable = 1;
37148 if (ENABLE_OFFLOADING)
37150 g->have_offload = true;
37151 if (is_a <varpool_node *> (node))
37152 vec_safe_push (offload_vars, t);
37159 static void
37160 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37162 const char *p = "";
37163 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37165 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37166 p = IDENTIFIER_POINTER (id);
37168 if (strcmp (p, "declare") == 0)
37170 cp_lexer_consume_token (parser->lexer);
37171 p = "";
37172 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37174 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37175 p = IDENTIFIER_POINTER (id);
37177 if (strcmp (p, "target") == 0)
37178 cp_lexer_consume_token (parser->lexer);
37179 else
37181 cp_parser_error (parser, "expected %<target%>");
37182 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37183 return;
37186 else
37188 cp_parser_error (parser, "expected %<declare%>");
37189 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37190 return;
37192 cp_parser_require_pragma_eol (parser, pragma_tok);
37193 if (!scope_chain->omp_declare_target_attribute)
37194 error_at (pragma_tok->location,
37195 "%<#pragma omp end declare target%> without corresponding "
37196 "%<#pragma omp declare target%>");
37197 else
37198 scope_chain->omp_declare_target_attribute--;
37201 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37202 expression and optional initializer clause of
37203 #pragma omp declare reduction. We store the expression(s) as
37204 either 3, 6 or 7 special statements inside of the artificial function's
37205 body. The first two statements are DECL_EXPRs for the artificial
37206 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37207 expression that uses those variables.
37208 If there was any INITIALIZER clause, this is followed by further statements,
37209 the fourth and fifth statements are DECL_EXPRs for the artificial
37210 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37211 constructor variant (first token after open paren is not omp_priv),
37212 then the sixth statement is a statement with the function call expression
37213 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37214 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37215 to initialize the OMP_PRIV artificial variable and there is seventh
37216 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37218 static bool
37219 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37221 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37222 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37223 type = TREE_TYPE (type);
37224 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37225 DECL_ARTIFICIAL (omp_out) = 1;
37226 pushdecl (omp_out);
37227 add_decl_expr (omp_out);
37228 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37229 DECL_ARTIFICIAL (omp_in) = 1;
37230 pushdecl (omp_in);
37231 add_decl_expr (omp_in);
37232 tree combiner;
37233 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37235 keep_next_level (true);
37236 tree block = begin_omp_structured_block ();
37237 combiner = cp_parser_expression (parser);
37238 finish_expr_stmt (combiner);
37239 block = finish_omp_structured_block (block);
37240 add_stmt (block);
37242 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37243 return false;
37245 const char *p = "";
37246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37248 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37249 p = IDENTIFIER_POINTER (id);
37252 if (strcmp (p, "initializer") == 0)
37254 cp_lexer_consume_token (parser->lexer);
37255 matching_parens parens;
37256 if (!parens.require_open (parser))
37257 return false;
37259 p = "";
37260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37262 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37263 p = IDENTIFIER_POINTER (id);
37266 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37267 DECL_ARTIFICIAL (omp_priv) = 1;
37268 pushdecl (omp_priv);
37269 add_decl_expr (omp_priv);
37270 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37271 DECL_ARTIFICIAL (omp_orig) = 1;
37272 pushdecl (omp_orig);
37273 add_decl_expr (omp_orig);
37275 keep_next_level (true);
37276 block = begin_omp_structured_block ();
37278 bool ctor = false;
37279 if (strcmp (p, "omp_priv") == 0)
37281 bool is_direct_init, is_non_constant_init;
37282 ctor = true;
37283 cp_lexer_consume_token (parser->lexer);
37284 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37285 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37286 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37287 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37288 == CPP_CLOSE_PAREN
37289 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37290 == CPP_CLOSE_PAREN))
37292 finish_omp_structured_block (block);
37293 error ("invalid initializer clause");
37294 return false;
37296 initializer = cp_parser_initializer (parser, &is_direct_init,
37297 &is_non_constant_init);
37298 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37299 NULL_TREE, LOOKUP_ONLYCONVERTING);
37301 else
37303 cp_parser_parse_tentatively (parser);
37304 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37305 /*check_dependency_p=*/true,
37306 /*template_p=*/NULL,
37307 /*declarator_p=*/false,
37308 /*optional_p=*/false);
37309 vec<tree, va_gc> *args;
37310 if (fn_name == error_mark_node
37311 || cp_parser_error_occurred (parser)
37312 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37313 || ((args = cp_parser_parenthesized_expression_list
37314 (parser, non_attr, /*cast_p=*/false,
37315 /*allow_expansion_p=*/true,
37316 /*non_constant_p=*/NULL)),
37317 cp_parser_error_occurred (parser)))
37319 finish_omp_structured_block (block);
37320 cp_parser_abort_tentative_parse (parser);
37321 cp_parser_error (parser, "expected id-expression (arguments)");
37322 return false;
37324 unsigned int i;
37325 tree arg;
37326 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37327 if (arg == omp_priv
37328 || (TREE_CODE (arg) == ADDR_EXPR
37329 && TREE_OPERAND (arg, 0) == omp_priv))
37330 break;
37331 cp_parser_abort_tentative_parse (parser);
37332 if (arg == NULL_TREE)
37333 error ("one of the initializer call arguments should be %<omp_priv%>"
37334 " or %<&omp_priv%>");
37335 initializer = cp_parser_postfix_expression (parser, false, false, false,
37336 false, NULL);
37337 finish_expr_stmt (initializer);
37340 block = finish_omp_structured_block (block);
37341 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37342 add_stmt (block);
37344 if (ctor)
37345 add_decl_expr (omp_orig);
37347 if (!parens.require_close (parser))
37348 return false;
37351 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37352 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37353 UNKNOWN_LOCATION);
37355 return true;
37358 /* OpenMP 4.0
37359 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37360 initializer-clause[opt] new-line
37362 initializer-clause:
37363 initializer (omp_priv initializer)
37364 initializer (function-name (argument-list)) */
37366 static void
37367 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37368 enum pragma_context)
37370 auto_vec<tree> types;
37371 enum tree_code reduc_code = ERROR_MARK;
37372 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37373 unsigned int i;
37374 cp_token *first_token;
37375 cp_token_cache *cp;
37376 int errs;
37377 void *p;
37379 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37380 p = obstack_alloc (&declarator_obstack, 0);
37382 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37383 goto fail;
37385 switch (cp_lexer_peek_token (parser->lexer)->type)
37387 case CPP_PLUS:
37388 reduc_code = PLUS_EXPR;
37389 break;
37390 case CPP_MULT:
37391 reduc_code = MULT_EXPR;
37392 break;
37393 case CPP_MINUS:
37394 reduc_code = MINUS_EXPR;
37395 break;
37396 case CPP_AND:
37397 reduc_code = BIT_AND_EXPR;
37398 break;
37399 case CPP_XOR:
37400 reduc_code = BIT_XOR_EXPR;
37401 break;
37402 case CPP_OR:
37403 reduc_code = BIT_IOR_EXPR;
37404 break;
37405 case CPP_AND_AND:
37406 reduc_code = TRUTH_ANDIF_EXPR;
37407 break;
37408 case CPP_OR_OR:
37409 reduc_code = TRUTH_ORIF_EXPR;
37410 break;
37411 case CPP_NAME:
37412 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37413 break;
37414 default:
37415 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37416 "%<|%>, %<&&%>, %<||%> or identifier");
37417 goto fail;
37420 if (reduc_code != ERROR_MARK)
37421 cp_lexer_consume_token (parser->lexer);
37423 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37424 if (reduc_id == error_mark_node)
37425 goto fail;
37427 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37428 goto fail;
37430 /* Types may not be defined in declare reduction type list. */
37431 const char *saved_message;
37432 saved_message = parser->type_definition_forbidden_message;
37433 parser->type_definition_forbidden_message
37434 = G_("types may not be defined in declare reduction type list");
37435 bool saved_colon_corrects_to_scope_p;
37436 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37437 parser->colon_corrects_to_scope_p = false;
37438 bool saved_colon_doesnt_start_class_def_p;
37439 saved_colon_doesnt_start_class_def_p
37440 = parser->colon_doesnt_start_class_def_p;
37441 parser->colon_doesnt_start_class_def_p = true;
37443 while (true)
37445 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37446 type = cp_parser_type_id (parser);
37447 if (type == error_mark_node)
37449 else if (ARITHMETIC_TYPE_P (type)
37450 && (orig_reduc_id == NULL_TREE
37451 || (TREE_CODE (type) != COMPLEX_TYPE
37452 && (id_equal (orig_reduc_id, "min")
37453 || id_equal (orig_reduc_id, "max")))))
37454 error_at (loc, "predeclared arithmetic type %qT in "
37455 "%<#pragma omp declare reduction%>", type);
37456 else if (TREE_CODE (type) == FUNCTION_TYPE
37457 || TREE_CODE (type) == METHOD_TYPE
37458 || TREE_CODE (type) == ARRAY_TYPE)
37459 error_at (loc, "function or array type %qT in "
37460 "%<#pragma omp declare reduction%>", type);
37461 else if (TREE_CODE (type) == REFERENCE_TYPE)
37462 error_at (loc, "reference type %qT in "
37463 "%<#pragma omp declare reduction%>", type);
37464 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37465 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37466 "%<#pragma omp declare reduction%>", type);
37467 else
37468 types.safe_push (type);
37470 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37471 cp_lexer_consume_token (parser->lexer);
37472 else
37473 break;
37476 /* Restore the saved message. */
37477 parser->type_definition_forbidden_message = saved_message;
37478 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37479 parser->colon_doesnt_start_class_def_p
37480 = saved_colon_doesnt_start_class_def_p;
37482 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37483 || types.is_empty ())
37485 fail:
37486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37487 goto done;
37490 first_token = cp_lexer_peek_token (parser->lexer);
37491 cp = NULL;
37492 errs = errorcount;
37493 FOR_EACH_VEC_ELT (types, i, type)
37495 tree fntype
37496 = build_function_type_list (void_type_node,
37497 cp_build_reference_type (type, false),
37498 NULL_TREE);
37499 tree this_reduc_id = reduc_id;
37500 if (!dependent_type_p (type))
37501 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37502 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37503 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37504 DECL_ARTIFICIAL (fndecl) = 1;
37505 DECL_EXTERNAL (fndecl) = 1;
37506 DECL_DECLARED_INLINE_P (fndecl) = 1;
37507 DECL_IGNORED_P (fndecl) = 1;
37508 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37509 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37510 DECL_ATTRIBUTES (fndecl)
37511 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37512 DECL_ATTRIBUTES (fndecl));
37513 if (processing_template_decl)
37514 fndecl = push_template_decl (fndecl);
37515 bool block_scope = false;
37516 tree block = NULL_TREE;
37517 if (current_function_decl)
37519 block_scope = true;
37520 DECL_CONTEXT (fndecl) = global_namespace;
37521 if (!processing_template_decl)
37522 pushdecl (fndecl);
37524 else if (current_class_type)
37526 if (cp == NULL)
37528 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37529 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37530 cp_lexer_consume_token (parser->lexer);
37531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37532 goto fail;
37533 cp = cp_token_cache_new (first_token,
37534 cp_lexer_peek_nth_token (parser->lexer,
37535 2));
37537 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37538 finish_member_declaration (fndecl);
37539 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37540 DECL_PENDING_INLINE_P (fndecl) = 1;
37541 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37542 continue;
37544 else
37546 DECL_CONTEXT (fndecl) = current_namespace;
37547 pushdecl (fndecl);
37549 if (!block_scope)
37550 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37551 else
37552 block = begin_omp_structured_block ();
37553 if (cp)
37555 cp_parser_push_lexer_for_tokens (parser, cp);
37556 parser->lexer->in_pragma = true;
37558 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37560 if (!block_scope)
37561 finish_function (0);
37562 else
37563 DECL_CONTEXT (fndecl) = current_function_decl;
37564 if (cp)
37565 cp_parser_pop_lexer (parser);
37566 goto fail;
37568 if (cp)
37569 cp_parser_pop_lexer (parser);
37570 if (!block_scope)
37571 finish_function (0);
37572 else
37574 DECL_CONTEXT (fndecl) = current_function_decl;
37575 block = finish_omp_structured_block (block);
37576 if (TREE_CODE (block) == BIND_EXPR)
37577 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37578 else if (TREE_CODE (block) == STATEMENT_LIST)
37579 DECL_SAVED_TREE (fndecl) = block;
37580 if (processing_template_decl)
37581 add_decl_expr (fndecl);
37583 cp_check_omp_declare_reduction (fndecl);
37584 if (cp == NULL && types.length () > 1)
37585 cp = cp_token_cache_new (first_token,
37586 cp_lexer_peek_nth_token (parser->lexer, 2));
37587 if (errs != errorcount)
37588 break;
37591 cp_parser_require_pragma_eol (parser, pragma_tok);
37593 done:
37594 /* Free any declarators allocated. */
37595 obstack_free (&declarator_obstack, p);
37598 /* OpenMP 4.0
37599 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37600 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37601 initializer-clause[opt] new-line
37602 #pragma omp declare target new-line */
37604 static void
37605 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37606 enum pragma_context context)
37608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37611 const char *p = IDENTIFIER_POINTER (id);
37613 if (strcmp (p, "simd") == 0)
37615 cp_lexer_consume_token (parser->lexer);
37616 cp_parser_omp_declare_simd (parser, pragma_tok,
37617 context);
37618 return;
37620 cp_ensure_no_omp_declare_simd (parser);
37621 if (strcmp (p, "reduction") == 0)
37623 cp_lexer_consume_token (parser->lexer);
37624 cp_parser_omp_declare_reduction (parser, pragma_tok,
37625 context);
37626 return;
37628 if (!flag_openmp) /* flag_openmp_simd */
37630 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37631 return;
37633 if (strcmp (p, "target") == 0)
37635 cp_lexer_consume_token (parser->lexer);
37636 cp_parser_omp_declare_target (parser, pragma_tok);
37637 return;
37640 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37641 "or %<target%>");
37642 cp_parser_require_pragma_eol (parser, pragma_tok);
37645 /* OpenMP 4.5:
37646 #pragma omp taskloop taskloop-clause[optseq] new-line
37647 for-loop
37649 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37650 for-loop */
37652 #define OMP_TASKLOOP_CLAUSE_MASK \
37653 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37656 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37664 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37666 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37668 static tree
37669 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37670 char *p_name, omp_clause_mask mask, tree *cclauses,
37671 bool *if_p)
37673 tree clauses, sb, ret;
37674 unsigned int save;
37675 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37677 strcat (p_name, " taskloop");
37678 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37680 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37682 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37683 const char *p = IDENTIFIER_POINTER (id);
37685 if (strcmp (p, "simd") == 0)
37687 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37688 if (cclauses == NULL)
37689 cclauses = cclauses_buf;
37691 cp_lexer_consume_token (parser->lexer);
37692 if (!flag_openmp) /* flag_openmp_simd */
37693 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37694 cclauses, if_p);
37695 sb = begin_omp_structured_block ();
37696 save = cp_parser_begin_omp_structured_block (parser);
37697 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37698 cclauses, if_p);
37699 cp_parser_end_omp_structured_block (parser, save);
37700 tree body = finish_omp_structured_block (sb);
37701 if (ret == NULL)
37702 return ret;
37703 ret = make_node (OMP_TASKLOOP);
37704 TREE_TYPE (ret) = void_type_node;
37705 OMP_FOR_BODY (ret) = body;
37706 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37707 SET_EXPR_LOCATION (ret, loc);
37708 add_stmt (ret);
37709 return ret;
37712 if (!flag_openmp) /* flag_openmp_simd */
37714 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37715 return NULL_TREE;
37718 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37719 cclauses == NULL);
37720 if (cclauses)
37722 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37723 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37726 sb = begin_omp_structured_block ();
37727 save = cp_parser_begin_omp_structured_block (parser);
37729 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37730 if_p);
37732 cp_parser_end_omp_structured_block (parser, save);
37733 add_stmt (finish_omp_structured_block (sb));
37735 return ret;
37739 /* OpenACC 2.0:
37740 # pragma acc routine oacc-routine-clause[optseq] new-line
37741 function-definition
37743 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37746 #define OACC_ROUTINE_CLAUSE_MASK \
37747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37753 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37754 component, which must resolve to a declared namespace-scope
37755 function. The clauses are either processed directly (for a named
37756 function), or defered until the immediatley following declaration
37757 is parsed. */
37759 static void
37760 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37761 enum pragma_context context)
37763 gcc_checking_assert (context == pragma_external);
37764 /* The checking for "another pragma following this one" in the "no optional
37765 '( name )'" case makes sure that we dont re-enter. */
37766 gcc_checking_assert (parser->oacc_routine == NULL);
37768 cp_oacc_routine_data data;
37769 data.error_seen = false;
37770 data.fndecl_seen = false;
37771 data.tokens = vNULL;
37772 data.clauses = NULL_TREE;
37773 data.loc = pragma_tok->location;
37774 /* It is safe to take the address of a local variable; it will only be
37775 used while this scope is live. */
37776 parser->oacc_routine = &data;
37778 /* Look for optional '( name )'. */
37779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37781 matching_parens parens;
37782 parens.consume_open (parser); /* '(' */
37784 /* We parse the name as an id-expression. If it resolves to
37785 anything other than a non-overloaded function at namespace
37786 scope, it's an error. */
37787 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37788 tree name = cp_parser_id_expression (parser,
37789 /*template_keyword_p=*/false,
37790 /*check_dependency_p=*/false,
37791 /*template_p=*/NULL,
37792 /*declarator_p=*/false,
37793 /*optional_p=*/false);
37794 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37795 if (name != error_mark_node && decl == error_mark_node)
37796 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37798 if (decl == error_mark_node
37799 || !parens.require_close (parser))
37801 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37802 parser->oacc_routine = NULL;
37803 return;
37806 data.clauses
37807 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37808 "#pragma acc routine",
37809 cp_lexer_peek_token (parser->lexer));
37811 if (decl && is_overloaded_fn (decl)
37812 && (TREE_CODE (decl) != FUNCTION_DECL
37813 || DECL_FUNCTION_TEMPLATE_P (decl)))
37815 error_at (name_loc,
37816 "%<#pragma acc routine%> names a set of overloads");
37817 parser->oacc_routine = NULL;
37818 return;
37821 /* Perhaps we should use the same rule as declarations in different
37822 namespaces? */
37823 if (!DECL_NAMESPACE_SCOPE_P (decl))
37825 error_at (name_loc,
37826 "%qD does not refer to a namespace scope function", decl);
37827 parser->oacc_routine = NULL;
37828 return;
37831 if (TREE_CODE (decl) != FUNCTION_DECL)
37833 error_at (name_loc, "%qD does not refer to a function", decl);
37834 parser->oacc_routine = NULL;
37835 return;
37838 cp_finalize_oacc_routine (parser, decl, false);
37839 parser->oacc_routine = NULL;
37841 else /* No optional '( name )'. */
37843 /* Store away all pragma tokens. */
37844 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37845 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37846 cp_lexer_consume_token (parser->lexer);
37847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37848 parser->oacc_routine->error_seen = true;
37849 cp_parser_require_pragma_eol (parser, pragma_tok);
37850 struct cp_token_cache *cp
37851 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37852 parser->oacc_routine->tokens.safe_push (cp);
37854 /* Emit a helpful diagnostic if there's another pragma following this
37855 one. */
37856 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37858 cp_ensure_no_oacc_routine (parser);
37859 data.tokens.release ();
37860 /* ..., and then just keep going. */
37861 return;
37864 /* We only have to consider the pragma_external case here. */
37865 cp_parser_declaration (parser);
37866 if (parser->oacc_routine
37867 && !parser->oacc_routine->fndecl_seen)
37868 cp_ensure_no_oacc_routine (parser);
37869 else
37870 parser->oacc_routine = NULL;
37871 data.tokens.release ();
37875 /* Finalize #pragma acc routine clauses after direct declarator has
37876 been parsed. */
37878 static tree
37879 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37881 struct cp_token_cache *ce;
37882 cp_oacc_routine_data *data = parser->oacc_routine;
37884 if (!data->error_seen && data->fndecl_seen)
37886 error_at (data->loc,
37887 "%<#pragma acc routine%> not immediately followed by "
37888 "a single function declaration or definition");
37889 data->error_seen = true;
37891 if (data->error_seen)
37892 return attrs;
37894 gcc_checking_assert (data->tokens.length () == 1);
37895 ce = data->tokens[0];
37897 cp_parser_push_lexer_for_tokens (parser, ce);
37898 parser->lexer->in_pragma = true;
37899 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37901 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37902 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37903 parser->oacc_routine->clauses
37904 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37905 "#pragma acc routine", pragma_tok);
37906 cp_parser_pop_lexer (parser);
37907 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37908 fndecl_seen. */
37910 return attrs;
37913 /* Apply any saved OpenACC routine clauses to a just-parsed
37914 declaration. */
37916 static void
37917 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37919 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37921 /* Keep going if we're in error reporting mode. */
37922 if (parser->oacc_routine->error_seen
37923 || fndecl == error_mark_node)
37924 return;
37926 if (parser->oacc_routine->fndecl_seen)
37928 error_at (parser->oacc_routine->loc,
37929 "%<#pragma acc routine%> not immediately followed by"
37930 " a single function declaration or definition");
37931 parser->oacc_routine = NULL;
37932 return;
37934 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37936 cp_ensure_no_oacc_routine (parser);
37937 return;
37940 if (oacc_get_fn_attrib (fndecl))
37942 error_at (parser->oacc_routine->loc,
37943 "%<#pragma acc routine%> already applied to %qD", fndecl);
37944 parser->oacc_routine = NULL;
37945 return;
37948 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37950 error_at (parser->oacc_routine->loc,
37951 TREE_USED (fndecl)
37952 ? G_("%<#pragma acc routine%> must be applied before use")
37953 : G_("%<#pragma acc routine%> must be applied before "
37954 "definition"));
37955 parser->oacc_routine = NULL;
37956 return;
37959 /* Process the routine's dimension clauses. */
37960 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37961 oacc_replace_fn_attrib (fndecl, dims);
37963 /* Add an "omp declare target" attribute. */
37964 DECL_ATTRIBUTES (fndecl)
37965 = tree_cons (get_identifier ("omp declare target"),
37966 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37968 /* Don't unset parser->oacc_routine here: we may still need it to
37969 diagnose wrong usage. But, remember that we've used this "#pragma acc
37970 routine". */
37971 parser->oacc_routine->fndecl_seen = true;
37975 /* Main entry point to OpenMP statement pragmas. */
37977 static void
37978 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37980 tree stmt;
37981 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37982 omp_clause_mask mask (0);
37984 switch (cp_parser_pragma_kind (pragma_tok))
37986 case PRAGMA_OACC_ATOMIC:
37987 cp_parser_omp_atomic (parser, pragma_tok);
37988 return;
37989 case PRAGMA_OACC_CACHE:
37990 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37991 break;
37992 case PRAGMA_OACC_DATA:
37993 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37994 break;
37995 case PRAGMA_OACC_ENTER_DATA:
37996 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37997 break;
37998 case PRAGMA_OACC_EXIT_DATA:
37999 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38000 break;
38001 case PRAGMA_OACC_HOST_DATA:
38002 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38003 break;
38004 case PRAGMA_OACC_KERNELS:
38005 case PRAGMA_OACC_PARALLEL:
38006 strcpy (p_name, "#pragma acc");
38007 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38008 if_p);
38009 break;
38010 case PRAGMA_OACC_LOOP:
38011 strcpy (p_name, "#pragma acc");
38012 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38013 if_p);
38014 break;
38015 case PRAGMA_OACC_UPDATE:
38016 stmt = cp_parser_oacc_update (parser, pragma_tok);
38017 break;
38018 case PRAGMA_OACC_WAIT:
38019 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38020 break;
38021 case PRAGMA_OMP_ATOMIC:
38022 cp_parser_omp_atomic (parser, pragma_tok);
38023 return;
38024 case PRAGMA_OMP_CRITICAL:
38025 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38026 break;
38027 case PRAGMA_OMP_DISTRIBUTE:
38028 strcpy (p_name, "#pragma omp");
38029 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38030 if_p);
38031 break;
38032 case PRAGMA_OMP_FOR:
38033 strcpy (p_name, "#pragma omp");
38034 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38035 if_p);
38036 break;
38037 case PRAGMA_OMP_MASTER:
38038 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38039 break;
38040 case PRAGMA_OMP_PARALLEL:
38041 strcpy (p_name, "#pragma omp");
38042 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38043 if_p);
38044 break;
38045 case PRAGMA_OMP_SECTIONS:
38046 strcpy (p_name, "#pragma omp");
38047 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38048 break;
38049 case PRAGMA_OMP_SIMD:
38050 strcpy (p_name, "#pragma omp");
38051 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38052 if_p);
38053 break;
38054 case PRAGMA_OMP_SINGLE:
38055 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38056 break;
38057 case PRAGMA_OMP_TASK:
38058 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38059 break;
38060 case PRAGMA_OMP_TASKGROUP:
38061 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38062 break;
38063 case PRAGMA_OMP_TASKLOOP:
38064 strcpy (p_name, "#pragma omp");
38065 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38066 if_p);
38067 break;
38068 case PRAGMA_OMP_TEAMS:
38069 strcpy (p_name, "#pragma omp");
38070 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38071 if_p);
38072 break;
38073 default:
38074 gcc_unreachable ();
38077 protected_set_expr_location (stmt, pragma_tok->location);
38080 /* Transactional Memory parsing routines. */
38082 /* Parse a transaction attribute.
38084 txn-attribute:
38085 attribute
38086 [ [ identifier ] ]
38088 We use this instead of cp_parser_attributes_opt for transactions to avoid
38089 the pedwarn in C++98 mode. */
38091 static tree
38092 cp_parser_txn_attribute_opt (cp_parser *parser)
38094 cp_token *token;
38095 tree attr_name, attr = NULL;
38097 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38098 return cp_parser_attributes_opt (parser);
38100 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38101 return NULL_TREE;
38102 cp_lexer_consume_token (parser->lexer);
38103 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38104 goto error1;
38106 token = cp_lexer_peek_token (parser->lexer);
38107 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38109 token = cp_lexer_consume_token (parser->lexer);
38111 attr_name = (token->type == CPP_KEYWORD
38112 /* For keywords, use the canonical spelling,
38113 not the parsed identifier. */
38114 ? ridpointers[(int) token->keyword]
38115 : token->u.value);
38116 attr = build_tree_list (attr_name, NULL_TREE);
38118 else
38119 cp_parser_error (parser, "expected identifier");
38121 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38122 error1:
38123 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38124 return attr;
38127 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38129 transaction-statement:
38130 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38131 compound-statement
38132 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38135 static tree
38136 cp_parser_transaction (cp_parser *parser, cp_token *token)
38138 unsigned char old_in = parser->in_transaction;
38139 unsigned char this_in = 1, new_in;
38140 enum rid keyword = token->keyword;
38141 tree stmt, attrs, noex;
38143 cp_lexer_consume_token (parser->lexer);
38145 if (keyword == RID_TRANSACTION_RELAXED
38146 || keyword == RID_SYNCHRONIZED)
38147 this_in |= TM_STMT_ATTR_RELAXED;
38148 else
38150 attrs = cp_parser_txn_attribute_opt (parser);
38151 if (attrs)
38152 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38155 /* Parse a noexcept specification. */
38156 if (keyword == RID_ATOMIC_NOEXCEPT)
38157 noex = boolean_true_node;
38158 else if (keyword == RID_ATOMIC_CANCEL)
38160 /* cancel-and-throw is unimplemented. */
38161 sorry ("atomic_cancel");
38162 noex = NULL_TREE;
38164 else
38165 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38167 /* Keep track if we're in the lexical scope of an outer transaction. */
38168 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38170 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38172 parser->in_transaction = new_in;
38173 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38174 parser->in_transaction = old_in;
38176 finish_transaction_stmt (stmt, NULL, this_in, noex);
38178 return stmt;
38181 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38183 transaction-expression:
38184 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38185 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38188 static tree
38189 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38191 unsigned char old_in = parser->in_transaction;
38192 unsigned char this_in = 1;
38193 cp_token *token;
38194 tree expr, noex;
38195 bool noex_expr;
38196 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38198 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38199 || keyword == RID_TRANSACTION_RELAXED);
38201 if (!flag_tm)
38202 error_at (loc,
38203 keyword == RID_TRANSACTION_RELAXED
38204 ? G_("%<__transaction_relaxed%> without transactional memory "
38205 "support enabled")
38206 : G_("%<__transaction_atomic%> without transactional memory "
38207 "support enabled"));
38209 token = cp_parser_require_keyword (parser, keyword,
38210 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38211 : RT_TRANSACTION_RELAXED));
38212 gcc_assert (token != NULL);
38214 if (keyword == RID_TRANSACTION_RELAXED)
38215 this_in |= TM_STMT_ATTR_RELAXED;
38217 /* Set this early. This might mean that we allow transaction_cancel in
38218 an expression that we find out later actually has to be a constexpr.
38219 However, we expect that cxx_constant_value will be able to deal with
38220 this; also, if the noexcept has no constexpr, then what we parse next
38221 really is a transaction's body. */
38222 parser->in_transaction = this_in;
38224 /* Parse a noexcept specification. */
38225 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38226 true);
38228 if (!noex || !noex_expr
38229 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38231 matching_parens parens;
38232 parens.require_open (parser);
38234 expr = cp_parser_expression (parser);
38235 expr = finish_parenthesized_expr (expr);
38237 parens.require_close (parser);
38239 else
38241 /* The only expression that is available got parsed for the noexcept
38242 already. noexcept is true then. */
38243 expr = noex;
38244 noex = boolean_true_node;
38247 expr = build_transaction_expr (token->location, expr, this_in, noex);
38248 parser->in_transaction = old_in;
38250 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38251 return error_mark_node;
38253 return (flag_tm ? expr : error_mark_node);
38256 /* Parse a function-transaction-block.
38258 function-transaction-block:
38259 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38260 function-body
38261 __transaction_atomic txn-attribute[opt] function-try-block
38262 __transaction_relaxed ctor-initializer[opt] function-body
38263 __transaction_relaxed function-try-block
38266 static bool
38267 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38269 unsigned char old_in = parser->in_transaction;
38270 unsigned char new_in = 1;
38271 tree compound_stmt, stmt, attrs;
38272 bool ctor_initializer_p;
38273 cp_token *token;
38275 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38276 || keyword == RID_TRANSACTION_RELAXED);
38277 token = cp_parser_require_keyword (parser, keyword,
38278 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38279 : RT_TRANSACTION_RELAXED));
38280 gcc_assert (token != NULL);
38282 if (keyword == RID_TRANSACTION_RELAXED)
38283 new_in |= TM_STMT_ATTR_RELAXED;
38284 else
38286 attrs = cp_parser_txn_attribute_opt (parser);
38287 if (attrs)
38288 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38291 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38293 parser->in_transaction = new_in;
38295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38296 ctor_initializer_p = cp_parser_function_try_block (parser);
38297 else
38298 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38299 (parser, /*in_function_try_block=*/false);
38301 parser->in_transaction = old_in;
38303 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38305 return ctor_initializer_p;
38308 /* Parse a __transaction_cancel statement.
38310 cancel-statement:
38311 __transaction_cancel txn-attribute[opt] ;
38312 __transaction_cancel txn-attribute[opt] throw-expression ;
38314 ??? Cancel and throw is not yet implemented. */
38316 static tree
38317 cp_parser_transaction_cancel (cp_parser *parser)
38319 cp_token *token;
38320 bool is_outer = false;
38321 tree stmt, attrs;
38323 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38324 RT_TRANSACTION_CANCEL);
38325 gcc_assert (token != NULL);
38327 attrs = cp_parser_txn_attribute_opt (parser);
38328 if (attrs)
38329 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38331 /* ??? Parse cancel-and-throw here. */
38333 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38335 if (!flag_tm)
38337 error_at (token->location, "%<__transaction_cancel%> without "
38338 "transactional memory support enabled");
38339 return error_mark_node;
38341 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38343 error_at (token->location, "%<__transaction_cancel%> within a "
38344 "%<__transaction_relaxed%>");
38345 return error_mark_node;
38347 else if (is_outer)
38349 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38350 && !is_tm_may_cancel_outer (current_function_decl))
38352 error_at (token->location, "outer %<__transaction_cancel%> not "
38353 "within outer %<__transaction_atomic%>");
38354 error_at (token->location,
38355 " or a %<transaction_may_cancel_outer%> function");
38356 return error_mark_node;
38359 else if (parser->in_transaction == 0)
38361 error_at (token->location, "%<__transaction_cancel%> not within "
38362 "%<__transaction_atomic%>");
38363 return error_mark_node;
38366 stmt = build_tm_abort_call (token->location, is_outer);
38367 add_stmt (stmt);
38369 return stmt;
38372 /* The parser. */
38374 static GTY (()) cp_parser *the_parser;
38377 /* Special handling for the first token or line in the file. The first
38378 thing in the file might be #pragma GCC pch_preprocess, which loads a
38379 PCH file, which is a GC collection point. So we need to handle this
38380 first pragma without benefit of an existing lexer structure.
38382 Always returns one token to the caller in *FIRST_TOKEN. This is
38383 either the true first token of the file, or the first token after
38384 the initial pragma. */
38386 static void
38387 cp_parser_initial_pragma (cp_token *first_token)
38389 tree name = NULL;
38391 cp_lexer_get_preprocessor_token (NULL, first_token);
38392 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38393 return;
38395 cp_lexer_get_preprocessor_token (NULL, first_token);
38396 if (first_token->type == CPP_STRING)
38398 name = first_token->u.value;
38400 cp_lexer_get_preprocessor_token (NULL, first_token);
38401 if (first_token->type != CPP_PRAGMA_EOL)
38402 error_at (first_token->location,
38403 "junk at end of %<#pragma GCC pch_preprocess%>");
38405 else
38406 error_at (first_token->location, "expected string literal");
38408 /* Skip to the end of the pragma. */
38409 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38410 cp_lexer_get_preprocessor_token (NULL, first_token);
38412 /* Now actually load the PCH file. */
38413 if (name)
38414 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38416 /* Read one more token to return to our caller. We have to do this
38417 after reading the PCH file in, since its pointers have to be
38418 live. */
38419 cp_lexer_get_preprocessor_token (NULL, first_token);
38422 /* Parses the grainsize pragma for the _Cilk_for statement.
38423 Syntax:
38424 #pragma cilk grainsize = <VALUE>. */
38426 static void
38427 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38429 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38431 tree exp = cp_parser_binary_expression (parser, false, false,
38432 PREC_NOT_OPERATOR, NULL);
38433 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38434 if (!exp || exp == error_mark_node)
38436 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38437 return;
38440 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38441 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38442 cp_parser_cilk_for (parser, exp, if_p);
38443 else
38444 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38445 "%<#pragma cilk grainsize%> is not followed by "
38446 "%<_Cilk_for%>");
38447 return;
38449 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38452 /* Normal parsing of a pragma token. Here we can (and must) use the
38453 regular lexer. */
38455 static bool
38456 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38458 cp_token *pragma_tok;
38459 unsigned int id;
38460 tree stmt;
38461 bool ret;
38463 pragma_tok = cp_lexer_consume_token (parser->lexer);
38464 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38465 parser->lexer->in_pragma = true;
38467 id = cp_parser_pragma_kind (pragma_tok);
38468 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38469 cp_ensure_no_omp_declare_simd (parser);
38470 switch (id)
38472 case PRAGMA_GCC_PCH_PREPROCESS:
38473 error_at (pragma_tok->location,
38474 "%<#pragma GCC pch_preprocess%> must be first");
38475 break;
38477 case PRAGMA_OMP_BARRIER:
38478 switch (context)
38480 case pragma_compound:
38481 cp_parser_omp_barrier (parser, pragma_tok);
38482 return false;
38483 case pragma_stmt:
38484 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38485 "used in compound statements", "omp barrier");
38486 break;
38487 default:
38488 goto bad_stmt;
38490 break;
38492 case PRAGMA_OMP_FLUSH:
38493 switch (context)
38495 case pragma_compound:
38496 cp_parser_omp_flush (parser, pragma_tok);
38497 return false;
38498 case pragma_stmt:
38499 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38500 "used in compound statements", "omp flush");
38501 break;
38502 default:
38503 goto bad_stmt;
38505 break;
38507 case PRAGMA_OMP_TASKWAIT:
38508 switch (context)
38510 case pragma_compound:
38511 cp_parser_omp_taskwait (parser, pragma_tok);
38512 return false;
38513 case pragma_stmt:
38514 error_at (pragma_tok->location,
38515 "%<#pragma %s%> may only be used in compound statements",
38516 "omp taskwait");
38517 break;
38518 default:
38519 goto bad_stmt;
38521 break;
38523 case PRAGMA_OMP_TASKYIELD:
38524 switch (context)
38526 case pragma_compound:
38527 cp_parser_omp_taskyield (parser, pragma_tok);
38528 return false;
38529 case pragma_stmt:
38530 error_at (pragma_tok->location,
38531 "%<#pragma %s%> may only be used in compound statements",
38532 "omp taskyield");
38533 break;
38534 default:
38535 goto bad_stmt;
38537 break;
38539 case PRAGMA_OMP_CANCEL:
38540 switch (context)
38542 case pragma_compound:
38543 cp_parser_omp_cancel (parser, pragma_tok);
38544 return false;
38545 case pragma_stmt:
38546 error_at (pragma_tok->location,
38547 "%<#pragma %s%> may only be used in compound statements",
38548 "omp cancel");
38549 break;
38550 default:
38551 goto bad_stmt;
38553 break;
38555 case PRAGMA_OMP_CANCELLATION_POINT:
38556 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38557 return false;
38559 case PRAGMA_OMP_THREADPRIVATE:
38560 cp_parser_omp_threadprivate (parser, pragma_tok);
38561 return false;
38563 case PRAGMA_OMP_DECLARE:
38564 cp_parser_omp_declare (parser, pragma_tok, context);
38565 return false;
38567 case PRAGMA_OACC_DECLARE:
38568 cp_parser_oacc_declare (parser, pragma_tok);
38569 return false;
38571 case PRAGMA_OACC_ENTER_DATA:
38572 if (context == pragma_stmt)
38574 error_at (pragma_tok->location,
38575 "%<#pragma %s%> may only be used in compound statements",
38576 "acc enter data");
38577 break;
38579 else if (context != pragma_compound)
38580 goto bad_stmt;
38581 cp_parser_omp_construct (parser, pragma_tok, if_p);
38582 return true;
38584 case PRAGMA_OACC_EXIT_DATA:
38585 if (context == pragma_stmt)
38587 error_at (pragma_tok->location,
38588 "%<#pragma %s%> may only be used in compound statements",
38589 "acc exit data");
38590 break;
38592 else if (context != pragma_compound)
38593 goto bad_stmt;
38594 cp_parser_omp_construct (parser, pragma_tok, if_p);
38595 return true;
38597 case PRAGMA_OACC_ROUTINE:
38598 if (context != pragma_external)
38600 error_at (pragma_tok->location,
38601 "%<#pragma acc routine%> must be at file scope");
38602 break;
38604 cp_parser_oacc_routine (parser, pragma_tok, context);
38605 return false;
38607 case PRAGMA_OACC_UPDATE:
38608 if (context == pragma_stmt)
38610 error_at (pragma_tok->location,
38611 "%<#pragma %s%> may only be used in compound statements",
38612 "acc update");
38613 break;
38615 else if (context != pragma_compound)
38616 goto bad_stmt;
38617 cp_parser_omp_construct (parser, pragma_tok, if_p);
38618 return true;
38620 case PRAGMA_OACC_WAIT:
38621 if (context == pragma_stmt)
38623 error_at (pragma_tok->location,
38624 "%<#pragma %s%> may only be used in compound statements",
38625 "acc wait");
38626 break;
38628 else if (context != pragma_compound)
38629 goto bad_stmt;
38630 cp_parser_omp_construct (parser, pragma_tok, if_p);
38631 return true;
38633 case PRAGMA_OACC_ATOMIC:
38634 case PRAGMA_OACC_CACHE:
38635 case PRAGMA_OACC_DATA:
38636 case PRAGMA_OACC_HOST_DATA:
38637 case PRAGMA_OACC_KERNELS:
38638 case PRAGMA_OACC_PARALLEL:
38639 case PRAGMA_OACC_LOOP:
38640 case PRAGMA_OMP_ATOMIC:
38641 case PRAGMA_OMP_CRITICAL:
38642 case PRAGMA_OMP_DISTRIBUTE:
38643 case PRAGMA_OMP_FOR:
38644 case PRAGMA_OMP_MASTER:
38645 case PRAGMA_OMP_PARALLEL:
38646 case PRAGMA_OMP_SECTIONS:
38647 case PRAGMA_OMP_SIMD:
38648 case PRAGMA_OMP_SINGLE:
38649 case PRAGMA_OMP_TASK:
38650 case PRAGMA_OMP_TASKGROUP:
38651 case PRAGMA_OMP_TASKLOOP:
38652 case PRAGMA_OMP_TEAMS:
38653 if (context != pragma_stmt && context != pragma_compound)
38654 goto bad_stmt;
38655 stmt = push_omp_privatization_clauses (false);
38656 cp_parser_omp_construct (parser, pragma_tok, if_p);
38657 pop_omp_privatization_clauses (stmt);
38658 return true;
38660 case PRAGMA_OMP_ORDERED:
38661 if (context != pragma_stmt && context != pragma_compound)
38662 goto bad_stmt;
38663 stmt = push_omp_privatization_clauses (false);
38664 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38665 pop_omp_privatization_clauses (stmt);
38666 return ret;
38668 case PRAGMA_OMP_TARGET:
38669 if (context != pragma_stmt && context != pragma_compound)
38670 goto bad_stmt;
38671 stmt = push_omp_privatization_clauses (false);
38672 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38673 pop_omp_privatization_clauses (stmt);
38674 return ret;
38676 case PRAGMA_OMP_END_DECLARE_TARGET:
38677 cp_parser_omp_end_declare_target (parser, pragma_tok);
38678 return false;
38680 case PRAGMA_OMP_SECTION:
38681 error_at (pragma_tok->location,
38682 "%<#pragma omp section%> may only be used in "
38683 "%<#pragma omp sections%> construct");
38684 break;
38686 case PRAGMA_IVDEP:
38688 if (context == pragma_external)
38690 error_at (pragma_tok->location,
38691 "%<#pragma GCC ivdep%> must be inside a function");
38692 break;
38694 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38695 cp_token *tok;
38696 tok = cp_lexer_peek_token (the_parser->lexer);
38697 if (tok->type != CPP_KEYWORD
38698 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38699 && tok->keyword != RID_DO))
38701 cp_parser_error (parser, "for, while or do statement expected");
38702 return false;
38704 cp_parser_iteration_statement (parser, if_p, true);
38705 return true;
38708 case PRAGMA_CILK_SIMD:
38709 if (context == pragma_external)
38711 error_at (pragma_tok->location,
38712 "%<#pragma simd%> must be inside a function");
38713 break;
38715 stmt = push_omp_privatization_clauses (false);
38716 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38717 pop_omp_privatization_clauses (stmt);
38718 return true;
38720 case PRAGMA_CILK_GRAINSIZE:
38721 if (context == pragma_external)
38723 error_at (pragma_tok->location,
38724 "%<#pragma cilk grainsize%> must be inside a function");
38725 break;
38728 /* Ignore the pragma if Cilk Plus is not enabled. */
38729 if (flag_cilkplus)
38731 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38732 return true;
38734 else
38736 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38737 "%<#pragma cilk grainsize%>");
38738 break;
38741 default:
38742 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38743 c_invoke_pragma_handler (id);
38744 break;
38746 bad_stmt:
38747 cp_parser_error (parser, "expected declaration specifiers");
38748 break;
38751 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38752 return false;
38755 /* The interface the pragma parsers have to the lexer. */
38757 enum cpp_ttype
38758 pragma_lex (tree *value, location_t *loc)
38760 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38761 enum cpp_ttype ret = tok->type;
38763 *value = tok->u.value;
38764 if (loc)
38765 *loc = tok->location;
38767 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38768 ret = CPP_EOF;
38769 else if (ret == CPP_STRING)
38770 *value = cp_parser_string_literal (the_parser, false, false);
38771 else
38773 if (ret == CPP_KEYWORD)
38774 ret = CPP_NAME;
38775 cp_lexer_consume_token (the_parser->lexer);
38778 return ret;
38782 /* External interface. */
38784 /* Parse one entire translation unit. */
38786 void
38787 c_parse_file (void)
38789 static bool already_called = false;
38791 if (already_called)
38792 fatal_error (input_location,
38793 "inter-module optimizations not implemented for C++");
38794 already_called = true;
38796 the_parser = cp_parser_new ();
38797 push_deferring_access_checks (flag_access_control
38798 ? dk_no_deferred : dk_no_check);
38799 cp_parser_translation_unit (the_parser);
38800 the_parser = NULL;
38803 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38804 vectorlength clause:
38805 Syntax:
38806 vectorlength ( constant-expression ) */
38808 static tree
38809 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38810 bool is_simd_fn)
38812 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38813 tree expr;
38814 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38815 safelen clause. Thus, vectorlength is represented as OMP 4.0
38816 safelen. For SIMD-enabled function it is represented by OMP 4.0
38817 simdlen. */
38818 if (!is_simd_fn)
38819 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38820 loc);
38821 else
38822 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38823 loc);
38825 matching_parens parens;
38826 if (!parens.require_open (parser))
38827 return error_mark_node;
38829 expr = cp_parser_constant_expression (parser);
38830 expr = maybe_constant_value (expr);
38832 /* If expr == error_mark_node, then don't emit any errors nor
38833 create a clause. if any of the above functions returns
38834 error mark node then they would have emitted an error message. */
38835 if (expr == error_mark_node)
38837 else if (!TREE_TYPE (expr)
38838 || !TREE_CONSTANT (expr)
38839 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38840 error_at (loc, "vectorlength must be an integer constant");
38841 else if (TREE_CONSTANT (expr)
38842 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38843 error_at (loc, "vectorlength must be a power of 2");
38844 else
38846 tree c;
38847 if (!is_simd_fn)
38849 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38850 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38851 OMP_CLAUSE_CHAIN (c) = clauses;
38852 clauses = c;
38854 else
38856 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38857 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38858 OMP_CLAUSE_CHAIN (c) = clauses;
38859 clauses = c;
38863 if (!parens.require_close (parser))
38864 return error_mark_node;
38865 return clauses;
38868 /* Handles the Cilk Plus #pragma simd linear clause.
38869 Syntax:
38870 linear ( simd-linear-variable-list )
38872 simd-linear-variable-list:
38873 simd-linear-variable
38874 simd-linear-variable-list , simd-linear-variable
38876 simd-linear-variable:
38877 id-expression
38878 id-expression : simd-linear-step
38880 simd-linear-step:
38881 conditional-expression */
38883 static tree
38884 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38889 return clauses;
38890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38892 cp_parser_error (parser, "expected identifier");
38893 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38894 return error_mark_node;
38897 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38898 parser->colon_corrects_to_scope_p = false;
38899 while (1)
38901 cp_token *token = cp_lexer_peek_token (parser->lexer);
38902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38904 cp_parser_error (parser, "expected variable-name");
38905 clauses = error_mark_node;
38906 break;
38909 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38910 false, false);
38911 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38912 token->location);
38913 if (decl == error_mark_node)
38915 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38916 token->location);
38917 clauses = error_mark_node;
38919 else
38921 tree e = NULL_TREE;
38922 tree step_size = integer_one_node;
38924 /* If present, parse the linear step. Otherwise, assume the default
38925 value of 1. */
38926 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38928 cp_lexer_consume_token (parser->lexer);
38930 e = cp_parser_assignment_expression (parser);
38931 e = maybe_constant_value (e);
38933 if (e == error_mark_node)
38935 /* If an error has occurred, then the whole pragma is
38936 considered ill-formed. Thus, no reason to keep
38937 parsing. */
38938 clauses = error_mark_node;
38939 break;
38941 else if (type_dependent_expression_p (e)
38942 || value_dependent_expression_p (e)
38943 || (TREE_TYPE (e)
38944 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38945 && (TREE_CONSTANT (e)
38946 || DECL_P (e))))
38947 step_size = e;
38948 else
38949 cp_parser_error (parser,
38950 "step size must be an integer constant "
38951 "expression or an integer variable");
38954 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38955 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38956 OMP_CLAUSE_DECL (l) = decl;
38957 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38958 OMP_CLAUSE_CHAIN (l) = clauses;
38959 clauses = l;
38961 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38962 cp_lexer_consume_token (parser->lexer);
38963 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38964 break;
38965 else
38967 error_at (cp_lexer_peek_token (parser->lexer)->location,
38968 "expected %<,%> or %<)%> after %qE", decl);
38969 clauses = error_mark_node;
38970 break;
38973 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38974 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38975 return clauses;
38978 /* Returns the name of the next clause. If the clause is not
38979 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38980 token is not consumed. Otherwise, the appropriate enum from the
38981 pragma_simd_clause is returned and the token is consumed. */
38983 static pragma_omp_clause
38984 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38986 pragma_omp_clause clause_type;
38987 cp_token *token = cp_lexer_peek_token (parser->lexer);
38989 if (token->keyword == RID_PRIVATE)
38990 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38991 else if (!token->u.value || token->type != CPP_NAME)
38992 return PRAGMA_CILK_CLAUSE_NONE;
38993 else if (id_equal (token->u.value, "vectorlength"))
38994 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38995 else if (id_equal (token->u.value, "linear"))
38996 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38997 else if (id_equal (token->u.value, "firstprivate"))
38998 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38999 else if (id_equal (token->u.value, "lastprivate"))
39000 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39001 else if (id_equal (token->u.value, "reduction"))
39002 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39003 else
39004 return PRAGMA_CILK_CLAUSE_NONE;
39006 cp_lexer_consume_token (parser->lexer);
39007 return clause_type;
39010 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39012 static tree
39013 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39015 tree clauses = NULL_TREE;
39017 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39018 && clauses != error_mark_node)
39020 pragma_omp_clause c_kind;
39021 c_kind = cp_parser_cilk_simd_clause_name (parser);
39022 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39023 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39024 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39025 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39026 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39027 /* Use the OpenMP 4.0 equivalent function. */
39028 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39029 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39030 /* Use the OpenMP 4.0 equivalent function. */
39031 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39032 clauses);
39033 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39034 /* Use the OMP 4.0 equivalent function. */
39035 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39036 clauses);
39037 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39038 /* Use the OMP 4.0 equivalent function. */
39039 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39040 else
39042 clauses = error_mark_node;
39043 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39044 break;
39048 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39050 if (clauses == error_mark_node)
39051 return error_mark_node;
39052 else
39053 return finish_omp_clauses (clauses, C_ORT_CILK);
39056 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39058 static void
39059 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39061 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39063 if (clauses == error_mark_node)
39064 return;
39066 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39068 error_at (cp_lexer_peek_token (parser->lexer)->location,
39069 "for statement expected");
39070 return;
39073 tree sb = begin_omp_structured_block ();
39074 int save = cp_parser_begin_omp_structured_block (parser);
39075 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39076 if (ret)
39077 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39078 cp_parser_end_omp_structured_block (parser, save);
39079 add_stmt (finish_omp_structured_block (sb));
39082 /* Main entry-point for parsing Cilk Plus _Cilk_for
39083 loops. The return value is error_mark_node
39084 when errors happen and CILK_FOR tree on success. */
39086 static tree
39087 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39089 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39090 gcc_unreachable ();
39092 tree sb = begin_omp_structured_block ();
39093 int save = cp_parser_begin_omp_structured_block (parser);
39095 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39096 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39097 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39098 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39100 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39101 if (ret)
39102 cpp_validate_cilk_plus_loop (ret);
39103 else
39104 ret = error_mark_node;
39106 cp_parser_end_omp_structured_block (parser, save);
39107 add_stmt (finish_omp_structured_block (sb));
39108 return ret;
39111 /* Create an identifier for a generic parameter type (a synthesized
39112 template parameter implied by `auto' or a concept identifier). */
39114 static GTY(()) int generic_parm_count;
39115 static tree
39116 make_generic_type_name ()
39118 char buf[32];
39119 sprintf (buf, "auto:%d", ++generic_parm_count);
39120 return get_identifier (buf);
39123 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39124 (creating a new template parameter list if necessary). Returns the newly
39125 created template type parm. */
39127 static tree
39128 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39130 gcc_assert (current_binding_level->kind == sk_function_parms);
39132 /* Before committing to modifying any scope, if we're in an
39133 implicit template scope, and we're trying to synthesize a
39134 constrained parameter, try to find a previous parameter with
39135 the same name. This is the same-type rule for abbreviated
39136 function templates.
39138 NOTE: We can generate implicit parameters when tentatively
39139 parsing a nested name specifier, only to reject that parse
39140 later. However, matching the same template-id as part of a
39141 direct-declarator should generate an identical template
39142 parameter, so this rule will merge them. */
39143 if (parser->implicit_template_scope && constr)
39145 tree t = parser->implicit_template_parms;
39146 while (t)
39148 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39150 tree d = TREE_VALUE (t);
39151 if (TREE_CODE (d) == PARM_DECL)
39152 /* Return the TEMPLATE_PARM_INDEX. */
39153 d = DECL_INITIAL (d);
39154 return d;
39156 t = TREE_CHAIN (t);
39160 /* We are either continuing a function template that already contains implicit
39161 template parameters, creating a new fully-implicit function template, or
39162 extending an existing explicit function template with implicit template
39163 parameters. */
39165 cp_binding_level *const entry_scope = current_binding_level;
39167 bool become_template = false;
39168 cp_binding_level *parent_scope = 0;
39170 if (parser->implicit_template_scope)
39172 gcc_assert (parser->implicit_template_parms);
39174 current_binding_level = parser->implicit_template_scope;
39176 else
39178 /* Roll back to the existing template parameter scope (in the case of
39179 extending an explicit function template) or introduce a new template
39180 parameter scope ahead of the function parameter scope (or class scope
39181 in the case of out-of-line member definitions). The function scope is
39182 added back after template parameter synthesis below. */
39184 cp_binding_level *scope = entry_scope;
39186 while (scope->kind == sk_function_parms)
39188 parent_scope = scope;
39189 scope = scope->level_chain;
39191 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39193 /* If not defining a class, then any class scope is a scope level in
39194 an out-of-line member definition. In this case simply wind back
39195 beyond the first such scope to inject the template parameter list.
39196 Otherwise wind back to the class being defined. The latter can
39197 occur in class member friend declarations such as:
39199 class A {
39200 void foo (auto);
39202 class B {
39203 friend void A::foo (auto);
39206 The template parameter list synthesized for the friend declaration
39207 must be injected in the scope of 'B'. This can also occur in
39208 erroneous cases such as:
39210 struct A {
39211 struct B {
39212 void foo (auto);
39214 void B::foo (auto) {}
39217 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39218 but, nevertheless, the template parameter list synthesized for the
39219 declarator should be injected into the scope of 'A' as if the
39220 ill-formed template was specified explicitly. */
39222 while (scope->kind == sk_class && !scope->defining_class_p)
39224 parent_scope = scope;
39225 scope = scope->level_chain;
39229 current_binding_level = scope;
39231 if (scope->kind != sk_template_parms
39232 || !function_being_declared_is_template_p (parser))
39234 /* Introduce a new template parameter list for implicit template
39235 parameters. */
39237 become_template = true;
39239 parser->implicit_template_scope
39240 = begin_scope (sk_template_parms, NULL);
39242 ++processing_template_decl;
39244 parser->fully_implicit_function_template_p = true;
39245 ++parser->num_template_parameter_lists;
39247 else
39249 /* Synthesize implicit template parameters at the end of the explicit
39250 template parameter list. */
39252 gcc_assert (current_template_parms);
39254 parser->implicit_template_scope = scope;
39256 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39257 parser->implicit_template_parms
39258 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39262 /* Synthesize a new template parameter and track the current template
39263 parameter chain with implicit_template_parms. */
39265 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39266 tree synth_id = make_generic_type_name ();
39267 tree synth_tmpl_parm;
39268 bool non_type = false;
39270 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39271 synth_tmpl_parm
39272 = finish_template_type_parm (class_type_node, synth_id);
39273 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39274 synth_tmpl_parm
39275 = finish_constrained_template_template_parm (proto, synth_id);
39276 else
39278 synth_tmpl_parm = copy_decl (proto);
39279 DECL_NAME (synth_tmpl_parm) = synth_id;
39280 non_type = true;
39283 // Attach the constraint to the parm before processing.
39284 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39285 TREE_TYPE (node) = constr;
39286 tree new_parm
39287 = process_template_parm (parser->implicit_template_parms,
39288 input_location,
39289 node,
39290 /*non_type=*/non_type,
39291 /*param_pack=*/false);
39293 // Chain the new parameter to the list of implicit parameters.
39294 if (parser->implicit_template_parms)
39295 parser->implicit_template_parms
39296 = TREE_CHAIN (parser->implicit_template_parms);
39297 else
39298 parser->implicit_template_parms = new_parm;
39300 tree new_decl = get_local_decls ();
39301 if (non_type)
39302 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39303 new_decl = DECL_INITIAL (new_decl);
39305 /* If creating a fully implicit function template, start the new implicit
39306 template parameter list with this synthesized type, otherwise grow the
39307 current template parameter list. */
39309 if (become_template)
39311 parent_scope->level_chain = current_binding_level;
39313 tree new_parms = make_tree_vec (1);
39314 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39315 current_template_parms = tree_cons (size_int (processing_template_decl),
39316 new_parms, current_template_parms);
39318 else
39320 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39321 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39322 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39323 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39326 // If the new parameter was constrained, we need to add that to the
39327 // constraints in the template parameter list.
39328 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39330 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39331 reqs = conjoin_constraints (reqs, req);
39332 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39335 current_binding_level = entry_scope;
39337 return new_decl;
39340 /* Finish the declaration of a fully implicit function template. Such a
39341 template has no explicit template parameter list so has not been through the
39342 normal template head and tail processing. synthesize_implicit_template_parm
39343 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39344 provided if the declaration is a class member such that its template
39345 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39346 form is returned. Otherwise NULL_TREE is returned. */
39348 static tree
39349 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39351 gcc_assert (parser->fully_implicit_function_template_p);
39353 if (member_decl_opt && member_decl_opt != error_mark_node
39354 && DECL_VIRTUAL_P (member_decl_opt))
39356 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39357 "implicit templates may not be %<virtual%>");
39358 DECL_VIRTUAL_P (member_decl_opt) = false;
39361 if (member_decl_opt)
39362 member_decl_opt = finish_member_template_decl (member_decl_opt);
39363 end_template_decl ();
39365 parser->fully_implicit_function_template_p = false;
39366 --parser->num_template_parameter_lists;
39368 return member_decl_opt;
39371 #include "gt-cp-parser.h"