PR c++/82357 - bit-field in template
[official-gcc.git] / gcc / cp / parser.c
blob2337be52c382bdfdfedd9f51c38fb3a777b76c2a
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->parenthesized = UNKNOWN_LOCATION;
1455 declarator->attributes = NULL_TREE;
1456 declarator->std_attributes = NULL_TREE;
1457 declarator->declarator = NULL;
1458 declarator->parameter_pack_p = false;
1459 declarator->id_loc = UNKNOWN_LOCATION;
1461 return declarator;
1464 /* Make a declarator for a generalized identifier. If
1465 QUALIFYING_SCOPE is non-NULL, the identifier is
1466 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1467 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1468 is, if any. */
1470 static cp_declarator *
1471 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1472 special_function_kind sfk)
1474 cp_declarator *declarator;
1476 /* It is valid to write:
1478 class C { void f(); };
1479 typedef C D;
1480 void D::f();
1482 The standard is not clear about whether `typedef const C D' is
1483 legal; as of 2002-09-15 the committee is considering that
1484 question. EDG 3.0 allows that syntax. Therefore, we do as
1485 well. */
1486 if (qualifying_scope && TYPE_P (qualifying_scope))
1487 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1489 gcc_assert (identifier_p (unqualified_name)
1490 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1491 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1493 declarator = make_declarator (cdk_id);
1494 declarator->u.id.qualifying_scope = qualifying_scope;
1495 declarator->u.id.unqualified_name = unqualified_name;
1496 declarator->u.id.sfk = sfk;
1498 return declarator;
1501 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1502 of modifiers such as const or volatile to apply to the pointer
1503 type, represented as identifiers. ATTRIBUTES represent the attributes that
1504 appertain to the pointer or reference. */
1506 cp_declarator *
1507 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1508 tree attributes)
1510 cp_declarator *declarator;
1512 declarator = make_declarator (cdk_pointer);
1513 declarator->declarator = target;
1514 declarator->u.pointer.qualifiers = cv_qualifiers;
1515 declarator->u.pointer.class_type = NULL_TREE;
1516 if (target)
1518 declarator->id_loc = target->id_loc;
1519 declarator->parameter_pack_p = target->parameter_pack_p;
1520 target->parameter_pack_p = false;
1522 else
1523 declarator->parameter_pack_p = false;
1525 declarator->std_attributes = attributes;
1527 return declarator;
1530 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1531 represent the attributes that appertain to the pointer or
1532 reference. */
1534 cp_declarator *
1535 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1536 bool rvalue_ref, tree attributes)
1538 cp_declarator *declarator;
1540 declarator = make_declarator (cdk_reference);
1541 declarator->declarator = target;
1542 declarator->u.reference.qualifiers = cv_qualifiers;
1543 declarator->u.reference.rvalue_ref = rvalue_ref;
1544 if (target)
1546 declarator->id_loc = target->id_loc;
1547 declarator->parameter_pack_p = target->parameter_pack_p;
1548 target->parameter_pack_p = false;
1550 else
1551 declarator->parameter_pack_p = false;
1553 declarator->std_attributes = attributes;
1555 return declarator;
1558 /* Like make_pointer_declarator -- but for a pointer to a non-static
1559 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1560 appertain to the pointer or reference. */
1562 cp_declarator *
1563 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1564 cp_declarator *pointee,
1565 tree attributes)
1567 cp_declarator *declarator;
1569 declarator = make_declarator (cdk_ptrmem);
1570 declarator->declarator = pointee;
1571 declarator->u.pointer.qualifiers = cv_qualifiers;
1572 declarator->u.pointer.class_type = class_type;
1574 if (pointee)
1576 declarator->parameter_pack_p = pointee->parameter_pack_p;
1577 pointee->parameter_pack_p = false;
1579 else
1580 declarator->parameter_pack_p = false;
1582 declarator->std_attributes = attributes;
1584 return declarator;
1587 /* Make a declarator for the function given by TARGET, with the
1588 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1589 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1590 indicates what exceptions can be thrown. */
1592 cp_declarator *
1593 make_call_declarator (cp_declarator *target,
1594 tree parms,
1595 cp_cv_quals cv_qualifiers,
1596 cp_virt_specifiers virt_specifiers,
1597 cp_ref_qualifier ref_qualifier,
1598 tree tx_qualifier,
1599 tree exception_specification,
1600 tree late_return_type,
1601 tree requires_clause)
1603 cp_declarator *declarator;
1605 declarator = make_declarator (cdk_function);
1606 declarator->declarator = target;
1607 declarator->u.function.parameters = parms;
1608 declarator->u.function.qualifiers = cv_qualifiers;
1609 declarator->u.function.virt_specifiers = virt_specifiers;
1610 declarator->u.function.ref_qualifier = ref_qualifier;
1611 declarator->u.function.tx_qualifier = tx_qualifier;
1612 declarator->u.function.exception_specification = exception_specification;
1613 declarator->u.function.late_return_type = late_return_type;
1614 declarator->u.function.requires_clause = requires_clause;
1615 if (target)
1617 declarator->id_loc = target->id_loc;
1618 declarator->parameter_pack_p = target->parameter_pack_p;
1619 target->parameter_pack_p = false;
1621 else
1622 declarator->parameter_pack_p = false;
1624 return declarator;
1627 /* Make a declarator for an array of BOUNDS elements, each of which is
1628 defined by ELEMENT. */
1630 cp_declarator *
1631 make_array_declarator (cp_declarator *element, tree bounds)
1633 cp_declarator *declarator;
1635 declarator = make_declarator (cdk_array);
1636 declarator->declarator = element;
1637 declarator->u.array.bounds = bounds;
1638 if (element)
1640 declarator->id_loc = element->id_loc;
1641 declarator->parameter_pack_p = element->parameter_pack_p;
1642 element->parameter_pack_p = false;
1644 else
1645 declarator->parameter_pack_p = false;
1647 return declarator;
1650 /* Determine whether the declarator we've seen so far can be a
1651 parameter pack, when followed by an ellipsis. */
1652 static bool
1653 declarator_can_be_parameter_pack (cp_declarator *declarator)
1655 if (declarator && declarator->parameter_pack_p)
1656 /* We already saw an ellipsis. */
1657 return false;
1659 /* Search for a declarator name, or any other declarator that goes
1660 after the point where the ellipsis could appear in a parameter
1661 pack. If we find any of these, then this declarator can not be
1662 made into a parameter pack. */
1663 bool found = false;
1664 while (declarator && !found)
1666 switch ((int)declarator->kind)
1668 case cdk_id:
1669 case cdk_array:
1670 case cdk_decomp:
1671 found = true;
1672 break;
1674 case cdk_error:
1675 return true;
1677 default:
1678 declarator = declarator->declarator;
1679 break;
1683 return !found;
1686 cp_parameter_declarator *no_parameters;
1688 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1689 DECLARATOR and DEFAULT_ARGUMENT. */
1691 cp_parameter_declarator *
1692 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1693 cp_declarator *declarator,
1694 tree default_argument,
1695 location_t loc,
1696 bool template_parameter_pack_p = false)
1698 cp_parameter_declarator *parameter;
1700 parameter = ((cp_parameter_declarator *)
1701 alloc_declarator (sizeof (cp_parameter_declarator)));
1702 parameter->next = NULL;
1703 if (decl_specifiers)
1704 parameter->decl_specifiers = *decl_specifiers;
1705 else
1706 clear_decl_specs (&parameter->decl_specifiers);
1707 parameter->declarator = declarator;
1708 parameter->default_argument = default_argument;
1709 parameter->template_parameter_pack_p = template_parameter_pack_p;
1710 parameter->loc = loc;
1712 return parameter;
1715 /* Returns true iff DECLARATOR is a declaration for a function. */
1717 static bool
1718 function_declarator_p (const cp_declarator *declarator)
1720 while (declarator)
1722 if (declarator->kind == cdk_function
1723 && declarator->declarator->kind == cdk_id)
1724 return true;
1725 if (declarator->kind == cdk_id
1726 || declarator->kind == cdk_decomp
1727 || declarator->kind == cdk_error)
1728 return false;
1729 declarator = declarator->declarator;
1731 return false;
1734 /* The parser. */
1736 /* Overview
1737 --------
1739 A cp_parser parses the token stream as specified by the C++
1740 grammar. Its job is purely parsing, not semantic analysis. For
1741 example, the parser breaks the token stream into declarators,
1742 expressions, statements, and other similar syntactic constructs.
1743 It does not check that the types of the expressions on either side
1744 of an assignment-statement are compatible, or that a function is
1745 not declared with a parameter of type `void'.
1747 The parser invokes routines elsewhere in the compiler to perform
1748 semantic analysis and to build up the abstract syntax tree for the
1749 code processed.
1751 The parser (and the template instantiation code, which is, in a
1752 way, a close relative of parsing) are the only parts of the
1753 compiler that should be calling push_scope and pop_scope, or
1754 related functions. The parser (and template instantiation code)
1755 keeps track of what scope is presently active; everything else
1756 should simply honor that. (The code that generates static
1757 initializers may also need to set the scope, in order to check
1758 access control correctly when emitting the initializers.)
1760 Methodology
1761 -----------
1763 The parser is of the standard recursive-descent variety. Upcoming
1764 tokens in the token stream are examined in order to determine which
1765 production to use when parsing a non-terminal. Some C++ constructs
1766 require arbitrary look ahead to disambiguate. For example, it is
1767 impossible, in the general case, to tell whether a statement is an
1768 expression or declaration without scanning the entire statement.
1769 Therefore, the parser is capable of "parsing tentatively." When the
1770 parser is not sure what construct comes next, it enters this mode.
1771 Then, while we attempt to parse the construct, the parser queues up
1772 error messages, rather than issuing them immediately, and saves the
1773 tokens it consumes. If the construct is parsed successfully, the
1774 parser "commits", i.e., it issues any queued error messages and
1775 the tokens that were being preserved are permanently discarded.
1776 If, however, the construct is not parsed successfully, the parser
1777 rolls back its state completely so that it can resume parsing using
1778 a different alternative.
1780 Future Improvements
1781 -------------------
1783 The performance of the parser could probably be improved substantially.
1784 We could often eliminate the need to parse tentatively by looking ahead
1785 a little bit. In some places, this approach might not entirely eliminate
1786 the need to parse tentatively, but it might still speed up the average
1787 case. */
1789 /* Flags that are passed to some parsing functions. These values can
1790 be bitwise-ored together. */
1792 enum
1794 /* No flags. */
1795 CP_PARSER_FLAGS_NONE = 0x0,
1796 /* The construct is optional. If it is not present, then no error
1797 should be issued. */
1798 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1799 /* When parsing a type-specifier, treat user-defined type-names
1800 as non-type identifiers. */
1801 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1802 /* When parsing a type-specifier, do not try to parse a class-specifier
1803 or enum-specifier. */
1804 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1805 /* When parsing a decl-specifier-seq, only allow type-specifier or
1806 constexpr. */
1807 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1808 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1809 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1812 /* This type is used for parameters and variables which hold
1813 combinations of the above flags. */
1814 typedef int cp_parser_flags;
1816 /* The different kinds of declarators we want to parse. */
1818 enum cp_parser_declarator_kind
1820 /* We want an abstract declarator. */
1821 CP_PARSER_DECLARATOR_ABSTRACT,
1822 /* We want a named declarator. */
1823 CP_PARSER_DECLARATOR_NAMED,
1824 /* We don't mind, but the name must be an unqualified-id. */
1825 CP_PARSER_DECLARATOR_EITHER
1828 /* The precedence values used to parse binary expressions. The minimum value
1829 of PREC must be 1, because zero is reserved to quickly discriminate
1830 binary operators from other tokens. */
1832 enum cp_parser_prec
1834 PREC_NOT_OPERATOR,
1835 PREC_LOGICAL_OR_EXPRESSION,
1836 PREC_LOGICAL_AND_EXPRESSION,
1837 PREC_INCLUSIVE_OR_EXPRESSION,
1838 PREC_EXCLUSIVE_OR_EXPRESSION,
1839 PREC_AND_EXPRESSION,
1840 PREC_EQUALITY_EXPRESSION,
1841 PREC_RELATIONAL_EXPRESSION,
1842 PREC_SHIFT_EXPRESSION,
1843 PREC_ADDITIVE_EXPRESSION,
1844 PREC_MULTIPLICATIVE_EXPRESSION,
1845 PREC_PM_EXPRESSION,
1846 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1849 /* A mapping from a token type to a corresponding tree node type, with a
1850 precedence value. */
1852 struct cp_parser_binary_operations_map_node
1854 /* The token type. */
1855 enum cpp_ttype token_type;
1856 /* The corresponding tree code. */
1857 enum tree_code tree_type;
1858 /* The precedence of this operator. */
1859 enum cp_parser_prec prec;
1862 struct cp_parser_expression_stack_entry
1864 /* Left hand side of the binary operation we are currently
1865 parsing. */
1866 cp_expr lhs;
1867 /* Original tree code for left hand side, if it was a binary
1868 expression itself (used for -Wparentheses). */
1869 enum tree_code lhs_type;
1870 /* Tree code for the binary operation we are parsing. */
1871 enum tree_code tree_type;
1872 /* Precedence of the binary operation we are parsing. */
1873 enum cp_parser_prec prec;
1874 /* Location of the binary operation we are parsing. */
1875 location_t loc;
1878 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1879 entries because precedence levels on the stack are monotonically
1880 increasing. */
1881 typedef struct cp_parser_expression_stack_entry
1882 cp_parser_expression_stack[NUM_PREC_VALUES];
1884 /* Prototypes. */
1886 /* Constructors and destructors. */
1888 static cp_parser_context *cp_parser_context_new
1889 (cp_parser_context *);
1891 /* Class variables. */
1893 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1895 /* The operator-precedence table used by cp_parser_binary_expression.
1896 Transformed into an associative array (binops_by_token) by
1897 cp_parser_new. */
1899 static const cp_parser_binary_operations_map_node binops[] = {
1900 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1901 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1903 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1905 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1907 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1908 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1910 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1911 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1913 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1914 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1916 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1918 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1919 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1921 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1923 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1925 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1927 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1929 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1932 /* The same as binops, but initialized by cp_parser_new so that
1933 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1934 for speed. */
1935 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1937 /* Constructors and destructors. */
1939 /* Construct a new context. The context below this one on the stack
1940 is given by NEXT. */
1942 static cp_parser_context *
1943 cp_parser_context_new (cp_parser_context* next)
1945 cp_parser_context *context;
1947 /* Allocate the storage. */
1948 if (cp_parser_context_free_list != NULL)
1950 /* Pull the first entry from the free list. */
1951 context = cp_parser_context_free_list;
1952 cp_parser_context_free_list = context->next;
1953 memset (context, 0, sizeof (*context));
1955 else
1956 context = ggc_cleared_alloc<cp_parser_context> ();
1958 /* No errors have occurred yet in this context. */
1959 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1960 /* If this is not the bottommost context, copy information that we
1961 need from the previous context. */
1962 if (next)
1964 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1965 expression, then we are parsing one in this context, too. */
1966 context->object_type = next->object_type;
1967 /* Thread the stack. */
1968 context->next = next;
1971 return context;
1974 /* Managing the unparsed function queues. */
1976 #define unparsed_funs_with_default_args \
1977 parser->unparsed_queues->last ().funs_with_default_args
1978 #define unparsed_funs_with_definitions \
1979 parser->unparsed_queues->last ().funs_with_definitions
1980 #define unparsed_nsdmis \
1981 parser->unparsed_queues->last ().nsdmis
1982 #define unparsed_classes \
1983 parser->unparsed_queues->last ().classes
1985 static void
1986 push_unparsed_function_queues (cp_parser *parser)
1988 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1989 vec_safe_push (parser->unparsed_queues, e);
1992 static void
1993 pop_unparsed_function_queues (cp_parser *parser)
1995 release_tree_vector (unparsed_funs_with_definitions);
1996 parser->unparsed_queues->pop ();
1999 /* Prototypes. */
2001 /* Constructors and destructors. */
2003 static cp_parser *cp_parser_new
2004 (void);
2006 /* Routines to parse various constructs.
2008 Those that return `tree' will return the error_mark_node (rather
2009 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2010 Sometimes, they will return an ordinary node if error-recovery was
2011 attempted, even though a parse error occurred. So, to check
2012 whether or not a parse error occurred, you should always use
2013 cp_parser_error_occurred. If the construct is optional (indicated
2014 either by an `_opt' in the name of the function that does the
2015 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2016 the construct is not present. */
2018 /* Lexical conventions [gram.lex] */
2020 static cp_expr cp_parser_identifier
2021 (cp_parser *);
2022 static cp_expr cp_parser_string_literal
2023 (cp_parser *, bool, bool, bool);
2024 static cp_expr cp_parser_userdef_char_literal
2025 (cp_parser *);
2026 static tree cp_parser_userdef_string_literal
2027 (tree);
2028 static cp_expr cp_parser_userdef_numeric_literal
2029 (cp_parser *);
2031 /* Basic concepts [gram.basic] */
2033 static bool cp_parser_translation_unit
2034 (cp_parser *);
2036 /* Expressions [gram.expr] */
2038 static cp_expr cp_parser_primary_expression
2039 (cp_parser *, bool, bool, bool, cp_id_kind *);
2040 static cp_expr cp_parser_id_expression
2041 (cp_parser *, bool, bool, bool *, bool, bool);
2042 static cp_expr cp_parser_unqualified_id
2043 (cp_parser *, bool, bool, bool, bool);
2044 static tree cp_parser_nested_name_specifier_opt
2045 (cp_parser *, bool, bool, bool, bool, bool = false);
2046 static tree cp_parser_nested_name_specifier
2047 (cp_parser *, bool, bool, bool, bool);
2048 static tree cp_parser_qualifying_entity
2049 (cp_parser *, bool, bool, bool, bool, bool);
2050 static cp_expr cp_parser_postfix_expression
2051 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2052 static tree cp_parser_postfix_open_square_expression
2053 (cp_parser *, tree, bool, bool);
2054 static tree cp_parser_postfix_dot_deref_expression
2055 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2056 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2057 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2058 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2059 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2060 static void cp_parser_pseudo_destructor_name
2061 (cp_parser *, tree, tree *, tree *);
2062 static cp_expr cp_parser_unary_expression
2063 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2064 static enum tree_code cp_parser_unary_operator
2065 (cp_token *);
2066 static tree cp_parser_new_expression
2067 (cp_parser *);
2068 static vec<tree, va_gc> *cp_parser_new_placement
2069 (cp_parser *);
2070 static tree cp_parser_new_type_id
2071 (cp_parser *, tree *);
2072 static cp_declarator *cp_parser_new_declarator_opt
2073 (cp_parser *);
2074 static cp_declarator *cp_parser_direct_new_declarator
2075 (cp_parser *);
2076 static vec<tree, va_gc> *cp_parser_new_initializer
2077 (cp_parser *);
2078 static tree cp_parser_delete_expression
2079 (cp_parser *);
2080 static cp_expr cp_parser_cast_expression
2081 (cp_parser *, bool, bool, bool, cp_id_kind *);
2082 static cp_expr cp_parser_binary_expression
2083 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2084 static tree cp_parser_question_colon_clause
2085 (cp_parser *, cp_expr);
2086 static cp_expr cp_parser_assignment_expression
2087 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2088 static enum tree_code cp_parser_assignment_operator_opt
2089 (cp_parser *);
2090 static cp_expr cp_parser_expression
2091 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2092 static cp_expr cp_parser_constant_expression
2093 (cp_parser *, bool = false, bool * = NULL, bool = false);
2094 static cp_expr cp_parser_builtin_offsetof
2095 (cp_parser *);
2096 static cp_expr cp_parser_lambda_expression
2097 (cp_parser *);
2098 static void cp_parser_lambda_introducer
2099 (cp_parser *, tree);
2100 static bool cp_parser_lambda_declarator_opt
2101 (cp_parser *, tree);
2102 static void cp_parser_lambda_body
2103 (cp_parser *, tree);
2105 /* Statements [gram.stmt.stmt] */
2107 static void cp_parser_statement
2108 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2109 static void cp_parser_label_for_labeled_statement
2110 (cp_parser *, tree);
2111 static tree cp_parser_expression_statement
2112 (cp_parser *, tree);
2113 static tree cp_parser_compound_statement
2114 (cp_parser *, tree, int, bool);
2115 static void cp_parser_statement_seq_opt
2116 (cp_parser *, tree);
2117 static tree cp_parser_selection_statement
2118 (cp_parser *, bool *, vec<tree> *);
2119 static tree cp_parser_condition
2120 (cp_parser *);
2121 static tree cp_parser_iteration_statement
2122 (cp_parser *, bool *, bool);
2123 static bool cp_parser_init_statement
2124 (cp_parser *, tree *decl);
2125 static tree cp_parser_for
2126 (cp_parser *, bool);
2127 static tree cp_parser_c_for
2128 (cp_parser *, tree, tree, bool);
2129 static tree cp_parser_range_for
2130 (cp_parser *, tree, tree, tree, bool);
2131 static void do_range_for_auto_deduction
2132 (tree, tree);
2133 static tree cp_parser_perform_range_for_lookup
2134 (tree, tree *, tree *);
2135 static tree cp_parser_range_for_member_function
2136 (tree, tree);
2137 static tree cp_parser_jump_statement
2138 (cp_parser *);
2139 static void cp_parser_declaration_statement
2140 (cp_parser *);
2142 static tree cp_parser_implicitly_scoped_statement
2143 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2144 static void cp_parser_already_scoped_statement
2145 (cp_parser *, bool *, const token_indent_info &);
2147 /* Declarations [gram.dcl.dcl] */
2149 static void cp_parser_declaration_seq_opt
2150 (cp_parser *);
2151 static void cp_parser_declaration
2152 (cp_parser *);
2153 static void cp_parser_block_declaration
2154 (cp_parser *, bool);
2155 static void cp_parser_simple_declaration
2156 (cp_parser *, bool, tree *);
2157 static void cp_parser_decl_specifier_seq
2158 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2159 static tree cp_parser_storage_class_specifier_opt
2160 (cp_parser *);
2161 static tree cp_parser_function_specifier_opt
2162 (cp_parser *, cp_decl_specifier_seq *);
2163 static tree cp_parser_type_specifier
2164 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2165 int *, bool *);
2166 static tree cp_parser_simple_type_specifier
2167 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2168 static tree cp_parser_type_name
2169 (cp_parser *, bool);
2170 static tree cp_parser_type_name
2171 (cp_parser *);
2172 static tree cp_parser_nonclass_name
2173 (cp_parser* parser);
2174 static tree cp_parser_elaborated_type_specifier
2175 (cp_parser *, bool, bool);
2176 static tree cp_parser_enum_specifier
2177 (cp_parser *);
2178 static void cp_parser_enumerator_list
2179 (cp_parser *, tree);
2180 static void cp_parser_enumerator_definition
2181 (cp_parser *, tree);
2182 static tree cp_parser_namespace_name
2183 (cp_parser *);
2184 static void cp_parser_namespace_definition
2185 (cp_parser *);
2186 static void cp_parser_namespace_body
2187 (cp_parser *);
2188 static tree cp_parser_qualified_namespace_specifier
2189 (cp_parser *);
2190 static void cp_parser_namespace_alias_definition
2191 (cp_parser *);
2192 static bool cp_parser_using_declaration
2193 (cp_parser *, bool);
2194 static void cp_parser_using_directive
2195 (cp_parser *);
2196 static tree cp_parser_alias_declaration
2197 (cp_parser *);
2198 static void cp_parser_asm_definition
2199 (cp_parser *);
2200 static void cp_parser_linkage_specification
2201 (cp_parser *);
2202 static void cp_parser_static_assert
2203 (cp_parser *, bool);
2204 static tree cp_parser_decltype
2205 (cp_parser *);
2206 static tree cp_parser_decomposition_declaration
2207 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2209 /* Declarators [gram.dcl.decl] */
2211 static tree cp_parser_init_declarator
2212 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2213 bool, bool, int, bool *, tree *, location_t *, tree *);
2214 static cp_declarator *cp_parser_declarator
2215 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2216 static cp_declarator *cp_parser_direct_declarator
2217 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2218 static enum tree_code cp_parser_ptr_operator
2219 (cp_parser *, tree *, cp_cv_quals *, tree *);
2220 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2221 (cp_parser *);
2222 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2223 (cp_parser *);
2224 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2225 (cp_parser *);
2226 static tree cp_parser_tx_qualifier_opt
2227 (cp_parser *);
2228 static tree cp_parser_late_return_type_opt
2229 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2230 static tree cp_parser_declarator_id
2231 (cp_parser *, bool);
2232 static tree cp_parser_type_id
2233 (cp_parser *);
2234 static tree cp_parser_template_type_arg
2235 (cp_parser *);
2236 static tree cp_parser_trailing_type_id (cp_parser *);
2237 static tree cp_parser_type_id_1
2238 (cp_parser *, bool, bool);
2239 static void cp_parser_type_specifier_seq
2240 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2241 static tree cp_parser_parameter_declaration_clause
2242 (cp_parser *);
2243 static tree cp_parser_parameter_declaration_list
2244 (cp_parser *, bool *);
2245 static cp_parameter_declarator *cp_parser_parameter_declaration
2246 (cp_parser *, bool, bool *);
2247 static tree cp_parser_default_argument
2248 (cp_parser *, bool);
2249 static void cp_parser_function_body
2250 (cp_parser *, bool);
2251 static tree cp_parser_initializer
2252 (cp_parser *, bool *, bool *);
2253 static cp_expr cp_parser_initializer_clause
2254 (cp_parser *, bool *);
2255 static cp_expr cp_parser_braced_list
2256 (cp_parser*, bool*);
2257 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2258 (cp_parser *, bool *);
2260 static void cp_parser_ctor_initializer_opt_and_function_body
2261 (cp_parser *, bool);
2263 static tree cp_parser_late_parsing_omp_declare_simd
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_cilk_simd_fn_info
2267 (cp_parser *, tree);
2269 static tree cp_parser_late_parsing_oacc_routine
2270 (cp_parser *, tree);
2272 static tree synthesize_implicit_template_parm
2273 (cp_parser *, tree);
2274 static tree finish_fully_implicit_template
2275 (cp_parser *, tree);
2277 /* Classes [gram.class] */
2279 static tree cp_parser_class_name
2280 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2281 static tree cp_parser_class_specifier
2282 (cp_parser *);
2283 static tree cp_parser_class_head
2284 (cp_parser *, bool *);
2285 static enum tag_types cp_parser_class_key
2286 (cp_parser *);
2287 static void cp_parser_type_parameter_key
2288 (cp_parser* parser);
2289 static void cp_parser_member_specification_opt
2290 (cp_parser *);
2291 static void cp_parser_member_declaration
2292 (cp_parser *);
2293 static tree cp_parser_pure_specifier
2294 (cp_parser *);
2295 static tree cp_parser_constant_initializer
2296 (cp_parser *);
2298 /* Derived classes [gram.class.derived] */
2300 static tree cp_parser_base_clause
2301 (cp_parser *);
2302 static tree cp_parser_base_specifier
2303 (cp_parser *);
2305 /* Special member functions [gram.special] */
2307 static tree cp_parser_conversion_function_id
2308 (cp_parser *);
2309 static tree cp_parser_conversion_type_id
2310 (cp_parser *);
2311 static cp_declarator *cp_parser_conversion_declarator_opt
2312 (cp_parser *);
2313 static void cp_parser_ctor_initializer_opt
2314 (cp_parser *);
2315 static void cp_parser_mem_initializer_list
2316 (cp_parser *);
2317 static tree cp_parser_mem_initializer
2318 (cp_parser *);
2319 static tree cp_parser_mem_initializer_id
2320 (cp_parser *);
2322 /* Overloading [gram.over] */
2324 static cp_expr cp_parser_operator_function_id
2325 (cp_parser *);
2326 static cp_expr cp_parser_operator
2327 (cp_parser *);
2329 /* Templates [gram.temp] */
2331 static void cp_parser_template_declaration
2332 (cp_parser *, bool);
2333 static tree cp_parser_template_parameter_list
2334 (cp_parser *);
2335 static tree cp_parser_template_parameter
2336 (cp_parser *, bool *, bool *);
2337 static tree cp_parser_type_parameter
2338 (cp_parser *, bool *);
2339 static tree cp_parser_template_id
2340 (cp_parser *, bool, bool, enum tag_types, bool);
2341 static tree cp_parser_template_name
2342 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2343 static tree cp_parser_template_argument_list
2344 (cp_parser *);
2345 static tree cp_parser_template_argument
2346 (cp_parser *);
2347 static void cp_parser_explicit_instantiation
2348 (cp_parser *);
2349 static void cp_parser_explicit_specialization
2350 (cp_parser *);
2352 /* Exception handling [gram.exception] */
2354 static tree cp_parser_try_block
2355 (cp_parser *);
2356 static void cp_parser_function_try_block
2357 (cp_parser *);
2358 static void cp_parser_handler_seq
2359 (cp_parser *);
2360 static void cp_parser_handler
2361 (cp_parser *);
2362 static tree cp_parser_exception_declaration
2363 (cp_parser *);
2364 static tree cp_parser_throw_expression
2365 (cp_parser *);
2366 static tree cp_parser_exception_specification_opt
2367 (cp_parser *);
2368 static tree cp_parser_type_id_list
2369 (cp_parser *);
2371 /* GNU Extensions */
2373 static tree cp_parser_asm_specification_opt
2374 (cp_parser *);
2375 static tree cp_parser_asm_operand_list
2376 (cp_parser *);
2377 static tree cp_parser_asm_clobber_list
2378 (cp_parser *);
2379 static tree cp_parser_asm_label_list
2380 (cp_parser *);
2381 static bool cp_next_tokens_can_be_attribute_p
2382 (cp_parser *);
2383 static bool cp_next_tokens_can_be_gnu_attribute_p
2384 (cp_parser *);
2385 static bool cp_next_tokens_can_be_std_attribute_p
2386 (cp_parser *);
2387 static bool cp_nth_tokens_can_be_std_attribute_p
2388 (cp_parser *, size_t);
2389 static bool cp_nth_tokens_can_be_gnu_attribute_p
2390 (cp_parser *, size_t);
2391 static bool cp_nth_tokens_can_be_attribute_p
2392 (cp_parser *, size_t);
2393 static tree cp_parser_attributes_opt
2394 (cp_parser *);
2395 static tree cp_parser_gnu_attributes_opt
2396 (cp_parser *);
2397 static tree cp_parser_gnu_attribute_list
2398 (cp_parser *);
2399 static tree cp_parser_std_attribute
2400 (cp_parser *, tree);
2401 static tree cp_parser_std_attribute_spec
2402 (cp_parser *);
2403 static tree cp_parser_std_attribute_spec_seq
2404 (cp_parser *);
2405 static bool cp_parser_extension_opt
2406 (cp_parser *, int *);
2407 static void cp_parser_label_declaration
2408 (cp_parser *);
2410 /* Concept Extensions */
2412 static tree cp_parser_requires_clause
2413 (cp_parser *);
2414 static tree cp_parser_requires_clause_opt
2415 (cp_parser *);
2416 static tree cp_parser_requires_expression
2417 (cp_parser *);
2418 static tree cp_parser_requirement_parameter_list
2419 (cp_parser *);
2420 static tree cp_parser_requirement_body
2421 (cp_parser *);
2422 static tree cp_parser_requirement_list
2423 (cp_parser *);
2424 static tree cp_parser_requirement
2425 (cp_parser *);
2426 static tree cp_parser_simple_requirement
2427 (cp_parser *);
2428 static tree cp_parser_compound_requirement
2429 (cp_parser *);
2430 static tree cp_parser_type_requirement
2431 (cp_parser *);
2432 static tree cp_parser_nested_requirement
2433 (cp_parser *);
2435 /* Transactional Memory Extensions */
2437 static tree cp_parser_transaction
2438 (cp_parser *, cp_token *);
2439 static tree cp_parser_transaction_expression
2440 (cp_parser *, enum rid);
2441 static void cp_parser_function_transaction
2442 (cp_parser *, enum rid);
2443 static tree cp_parser_transaction_cancel
2444 (cp_parser *);
2446 enum pragma_context {
2447 pragma_external,
2448 pragma_member,
2449 pragma_objc_icode,
2450 pragma_stmt,
2451 pragma_compound
2453 static bool cp_parser_pragma
2454 (cp_parser *, enum pragma_context, bool *);
2456 /* Objective-C++ Productions */
2458 static tree cp_parser_objc_message_receiver
2459 (cp_parser *);
2460 static tree cp_parser_objc_message_args
2461 (cp_parser *);
2462 static tree cp_parser_objc_message_expression
2463 (cp_parser *);
2464 static cp_expr cp_parser_objc_encode_expression
2465 (cp_parser *);
2466 static tree cp_parser_objc_defs_expression
2467 (cp_parser *);
2468 static tree cp_parser_objc_protocol_expression
2469 (cp_parser *);
2470 static tree cp_parser_objc_selector_expression
2471 (cp_parser *);
2472 static cp_expr cp_parser_objc_expression
2473 (cp_parser *);
2474 static bool cp_parser_objc_selector_p
2475 (enum cpp_ttype);
2476 static tree cp_parser_objc_selector
2477 (cp_parser *);
2478 static tree cp_parser_objc_protocol_refs_opt
2479 (cp_parser *);
2480 static void cp_parser_objc_declaration
2481 (cp_parser *, tree);
2482 static tree cp_parser_objc_statement
2483 (cp_parser *);
2484 static bool cp_parser_objc_valid_prefix_attributes
2485 (cp_parser *, tree *);
2486 static void cp_parser_objc_at_property_declaration
2487 (cp_parser *) ;
2488 static void cp_parser_objc_at_synthesize_declaration
2489 (cp_parser *) ;
2490 static void cp_parser_objc_at_dynamic_declaration
2491 (cp_parser *) ;
2492 static tree cp_parser_objc_struct_declaration
2493 (cp_parser *) ;
2495 /* Utility Routines */
2497 static cp_expr cp_parser_lookup_name
2498 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2499 static tree cp_parser_lookup_name_simple
2500 (cp_parser *, tree, location_t);
2501 static tree cp_parser_maybe_treat_template_as_class
2502 (tree, bool);
2503 static bool cp_parser_check_declarator_template_parameters
2504 (cp_parser *, cp_declarator *, location_t);
2505 static bool cp_parser_check_template_parameters
2506 (cp_parser *, unsigned, location_t, cp_declarator *);
2507 static cp_expr cp_parser_simple_cast_expression
2508 (cp_parser *);
2509 static tree cp_parser_global_scope_opt
2510 (cp_parser *, bool);
2511 static bool cp_parser_constructor_declarator_p
2512 (cp_parser *, bool);
2513 static tree cp_parser_function_definition_from_specifiers_and_declarator
2514 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2515 static tree cp_parser_function_definition_after_declarator
2516 (cp_parser *, bool);
2517 static bool cp_parser_template_declaration_after_export
2518 (cp_parser *, bool);
2519 static void cp_parser_perform_template_parameter_access_checks
2520 (vec<deferred_access_check, va_gc> *);
2521 static tree cp_parser_single_declaration
2522 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2523 static cp_expr cp_parser_functional_cast
2524 (cp_parser *, tree);
2525 static tree cp_parser_save_member_function_body
2526 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2527 static tree cp_parser_save_nsdmi
2528 (cp_parser *);
2529 static tree cp_parser_enclosed_template_argument_list
2530 (cp_parser *);
2531 static void cp_parser_save_default_args
2532 (cp_parser *, tree);
2533 static void cp_parser_late_parsing_for_member
2534 (cp_parser *, tree);
2535 static tree cp_parser_late_parse_one_default_arg
2536 (cp_parser *, tree, tree, tree);
2537 static void cp_parser_late_parsing_nsdmi
2538 (cp_parser *, tree);
2539 static void cp_parser_late_parsing_default_args
2540 (cp_parser *, tree);
2541 static tree cp_parser_sizeof_operand
2542 (cp_parser *, enum rid);
2543 static tree cp_parser_trait_expr
2544 (cp_parser *, enum rid);
2545 static bool cp_parser_declares_only_class_p
2546 (cp_parser *);
2547 static void cp_parser_set_storage_class
2548 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2549 static void cp_parser_set_decl_spec_type
2550 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2551 static void set_and_check_decl_spec_loc
2552 (cp_decl_specifier_seq *decl_specs,
2553 cp_decl_spec ds, cp_token *);
2554 static bool cp_parser_friend_p
2555 (const cp_decl_specifier_seq *);
2556 static void cp_parser_required_error
2557 (cp_parser *, required_token, bool, location_t);
2558 static cp_token *cp_parser_require
2559 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2560 static cp_token *cp_parser_require_keyword
2561 (cp_parser *, enum rid, required_token);
2562 static bool cp_parser_token_starts_function_definition_p
2563 (cp_token *);
2564 static bool cp_parser_next_token_starts_class_definition_p
2565 (cp_parser *);
2566 static bool cp_parser_next_token_ends_template_argument_p
2567 (cp_parser *);
2568 static bool cp_parser_nth_token_starts_template_argument_list_p
2569 (cp_parser *, size_t);
2570 static enum tag_types cp_parser_token_is_class_key
2571 (cp_token *);
2572 static enum tag_types cp_parser_token_is_type_parameter_key
2573 (cp_token *);
2574 static void cp_parser_check_class_key
2575 (enum tag_types, tree type);
2576 static void cp_parser_check_access_in_redeclaration
2577 (tree type, location_t location);
2578 static bool cp_parser_optional_template_keyword
2579 (cp_parser *);
2580 static void cp_parser_pre_parsed_nested_name_specifier
2581 (cp_parser *);
2582 static bool cp_parser_cache_group
2583 (cp_parser *, enum cpp_ttype, unsigned);
2584 static tree cp_parser_cache_defarg
2585 (cp_parser *parser, bool nsdmi);
2586 static void cp_parser_parse_tentatively
2587 (cp_parser *);
2588 static void cp_parser_commit_to_tentative_parse
2589 (cp_parser *);
2590 static void cp_parser_commit_to_topmost_tentative_parse
2591 (cp_parser *);
2592 static void cp_parser_abort_tentative_parse
2593 (cp_parser *);
2594 static bool cp_parser_parse_definitely
2595 (cp_parser *);
2596 static inline bool cp_parser_parsing_tentatively
2597 (cp_parser *);
2598 static bool cp_parser_uncommitted_to_tentative_parse_p
2599 (cp_parser *);
2600 static void cp_parser_error
2601 (cp_parser *, const char *);
2602 static void cp_parser_name_lookup_error
2603 (cp_parser *, tree, tree, name_lookup_error, location_t);
2604 static bool cp_parser_simulate_error
2605 (cp_parser *);
2606 static bool cp_parser_check_type_definition
2607 (cp_parser *);
2608 static void cp_parser_check_for_definition_in_return_type
2609 (cp_declarator *, tree, location_t type_location);
2610 static void cp_parser_check_for_invalid_template_id
2611 (cp_parser *, tree, enum tag_types, location_t location);
2612 static bool cp_parser_non_integral_constant_expression
2613 (cp_parser *, non_integral_constant);
2614 static void cp_parser_diagnose_invalid_type_name
2615 (cp_parser *, tree, location_t);
2616 static bool cp_parser_parse_and_diagnose_invalid_type_name
2617 (cp_parser *);
2618 static int cp_parser_skip_to_closing_parenthesis
2619 (cp_parser *, bool, bool, bool);
2620 static void cp_parser_skip_to_end_of_statement
2621 (cp_parser *);
2622 static void cp_parser_consume_semicolon_at_end_of_statement
2623 (cp_parser *);
2624 static void cp_parser_skip_to_end_of_block_or_statement
2625 (cp_parser *);
2626 static bool cp_parser_skip_to_closing_brace
2627 (cp_parser *);
2628 static void cp_parser_skip_to_end_of_template_parameter_list
2629 (cp_parser *);
2630 static void cp_parser_skip_to_pragma_eol
2631 (cp_parser*, cp_token *);
2632 static bool cp_parser_error_occurred
2633 (cp_parser *);
2634 static bool cp_parser_allow_gnu_extensions_p
2635 (cp_parser *);
2636 static bool cp_parser_is_pure_string_literal
2637 (cp_token *);
2638 static bool cp_parser_is_string_literal
2639 (cp_token *);
2640 static bool cp_parser_is_keyword
2641 (cp_token *, enum rid);
2642 static tree cp_parser_make_typename_type
2643 (cp_parser *, tree, location_t location);
2644 static cp_declarator * cp_parser_make_indirect_declarator
2645 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2646 static bool cp_parser_compound_literal_p
2647 (cp_parser *);
2648 static bool cp_parser_array_designator_p
2649 (cp_parser *);
2650 static bool cp_parser_init_statement_p
2651 (cp_parser *);
2652 static bool cp_parser_skip_to_closing_square_bracket
2653 (cp_parser *);
2655 /* Concept-related syntactic transformations */
2657 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2658 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2660 // -------------------------------------------------------------------------- //
2661 // Unevaluated Operand Guard
2663 // Implementation of an RAII helper for unevaluated operand parsing.
2664 cp_unevaluated::cp_unevaluated ()
2666 ++cp_unevaluated_operand;
2667 ++c_inhibit_evaluation_warnings;
2670 cp_unevaluated::~cp_unevaluated ()
2672 --c_inhibit_evaluation_warnings;
2673 --cp_unevaluated_operand;
2676 // -------------------------------------------------------------------------- //
2677 // Tentative Parsing
2679 /* Returns nonzero if we are parsing tentatively. */
2681 static inline bool
2682 cp_parser_parsing_tentatively (cp_parser* parser)
2684 return parser->context->next != NULL;
2687 /* Returns nonzero if TOKEN is a string literal. */
2689 static bool
2690 cp_parser_is_pure_string_literal (cp_token* token)
2692 return (token->type == CPP_STRING ||
2693 token->type == CPP_STRING16 ||
2694 token->type == CPP_STRING32 ||
2695 token->type == CPP_WSTRING ||
2696 token->type == CPP_UTF8STRING);
2699 /* Returns nonzero if TOKEN is a string literal
2700 of a user-defined string literal. */
2702 static bool
2703 cp_parser_is_string_literal (cp_token* token)
2705 return (cp_parser_is_pure_string_literal (token) ||
2706 token->type == CPP_STRING_USERDEF ||
2707 token->type == CPP_STRING16_USERDEF ||
2708 token->type == CPP_STRING32_USERDEF ||
2709 token->type == CPP_WSTRING_USERDEF ||
2710 token->type == CPP_UTF8STRING_USERDEF);
2713 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2715 static bool
2716 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2718 return token->keyword == keyword;
2721 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2722 PRAGMA_NONE. */
2724 static enum pragma_kind
2725 cp_parser_pragma_kind (cp_token *token)
2727 if (token->type != CPP_PRAGMA)
2728 return PRAGMA_NONE;
2729 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2730 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2733 /* Helper function for cp_parser_error.
2734 Having peeked a token of kind TOK1_KIND that might signify
2735 a conflict marker, peek successor tokens to determine
2736 if we actually do have a conflict marker.
2737 Specifically, we consider a run of 7 '<', '=' or '>' characters
2738 at the start of a line as a conflict marker.
2739 These come through the lexer as three pairs and a single,
2740 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2741 If it returns true, *OUT_LOC is written to with the location/range
2742 of the marker. */
2744 static bool
2745 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2746 location_t *out_loc)
2748 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2749 if (token2->type != tok1_kind)
2750 return false;
2751 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2752 if (token3->type != tok1_kind)
2753 return false;
2754 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2755 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2756 return false;
2758 /* It must be at the start of the line. */
2759 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2760 if (LOCATION_COLUMN (start_loc) != 1)
2761 return false;
2763 /* We have a conflict marker. Construct a location of the form:
2764 <<<<<<<
2765 ^~~~~~~
2766 with start == caret, finishing at the end of the marker. */
2767 location_t finish_loc = get_finish (token4->location);
2768 *out_loc = make_location (start_loc, start_loc, finish_loc);
2770 return true;
2773 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2774 RT_CLOSE_PAREN. */
2776 static const char *
2777 get_matching_symbol (required_token token_desc)
2779 switch (token_desc)
2781 default:
2782 gcc_unreachable ();
2783 return "";
2784 case RT_CLOSE_BRACE:
2785 return "{";
2786 case RT_CLOSE_PAREN:
2787 return "(";
2791 /* Attempt to convert TOKEN_DESC from a required_token to an
2792 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2794 static enum cpp_ttype
2795 get_required_cpp_ttype (required_token token_desc)
2797 switch (token_desc)
2799 case RT_SEMICOLON:
2800 return CPP_SEMICOLON;
2801 case RT_OPEN_PAREN:
2802 return CPP_OPEN_PAREN;
2803 case RT_CLOSE_BRACE:
2804 return CPP_CLOSE_BRACE;
2805 case RT_OPEN_BRACE:
2806 return CPP_OPEN_BRACE;
2807 case RT_CLOSE_SQUARE:
2808 return CPP_CLOSE_SQUARE;
2809 case RT_OPEN_SQUARE:
2810 return CPP_OPEN_SQUARE;
2811 case RT_COMMA:
2812 return CPP_COMMA;
2813 case RT_COLON:
2814 return CPP_COLON;
2815 case RT_CLOSE_PAREN:
2816 return CPP_CLOSE_PAREN;
2818 default:
2819 /* Use CPP_EOF as a "no completions possible" code. */
2820 return CPP_EOF;
2825 /* Subroutine of cp_parser_error and cp_parser_required_error.
2827 Issue a diagnostic of the form
2828 FILE:LINE: MESSAGE before TOKEN
2829 where TOKEN is the next token in the input stream. MESSAGE
2830 (specified by the caller) is usually of the form "expected
2831 OTHER-TOKEN".
2833 This bypasses the check for tentative passing, and potentially
2834 adds material needed by cp_parser_required_error.
2836 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2837 suggesting insertion of the missing token.
2839 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2840 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2841 location. */
2843 static void
2844 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
2845 required_token missing_token_desc,
2846 location_t matching_location)
2848 cp_token *token = cp_lexer_peek_token (parser->lexer);
2849 /* This diagnostic makes more sense if it is tagged to the line
2850 of the token we just peeked at. */
2851 cp_lexer_set_source_position_from_token (token);
2853 if (token->type == CPP_PRAGMA)
2855 error_at (token->location,
2856 "%<#pragma%> is not allowed here");
2857 cp_parser_skip_to_pragma_eol (parser, token);
2858 return;
2861 /* If this is actually a conflict marker, report it as such. */
2862 if (token->type == CPP_LSHIFT
2863 || token->type == CPP_RSHIFT
2864 || token->type == CPP_EQ_EQ)
2866 location_t loc;
2867 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2869 error_at (loc, "version control conflict marker in file");
2870 return;
2874 gcc_rich_location richloc (input_location);
2876 bool added_matching_location = false;
2878 if (missing_token_desc != RT_NONE)
2880 /* Potentially supply a fix-it hint, suggesting to add the
2881 missing token immediately after the *previous* token.
2882 This may move the primary location within richloc. */
2883 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
2884 location_t prev_token_loc
2885 = cp_lexer_previous_token (parser->lexer)->location;
2886 maybe_suggest_missing_token_insertion (&richloc, ttype, prev_token_loc);
2888 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2889 Attempt to consolidate diagnostics by printing it as a
2890 secondary range within the main diagnostic. */
2891 if (matching_location != UNKNOWN_LOCATION)
2892 added_matching_location
2893 = richloc.add_location_if_nearby (matching_location);
2896 /* Actually emit the error. */
2897 c_parse_error (gmsgid,
2898 /* Because c_parser_error does not understand
2899 CPP_KEYWORD, keywords are treated like
2900 identifiers. */
2901 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2902 token->u.value, token->flags, &richloc);
2904 if (missing_token_desc != RT_NONE)
2906 /* If we weren't able to consolidate matching_location, then
2907 print it as a secondary diagnostic. */
2908 if (matching_location != UNKNOWN_LOCATION
2909 && !added_matching_location)
2910 inform (matching_location, "to match this %qs",
2911 get_matching_symbol (missing_token_desc));
2915 /* If not parsing tentatively, issue a diagnostic of the form
2916 FILE:LINE: MESSAGE before TOKEN
2917 where TOKEN is the next token in the input stream. MESSAGE
2918 (specified by the caller) is usually of the form "expected
2919 OTHER-TOKEN". */
2921 static void
2922 cp_parser_error (cp_parser* parser, const char* gmsgid)
2924 if (!cp_parser_simulate_error (parser))
2925 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
2928 /* Issue an error about name-lookup failing. NAME is the
2929 IDENTIFIER_NODE DECL is the result of
2930 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2931 the thing that we hoped to find. */
2933 static void
2934 cp_parser_name_lookup_error (cp_parser* parser,
2935 tree name,
2936 tree decl,
2937 name_lookup_error desired,
2938 location_t location)
2940 /* If name lookup completely failed, tell the user that NAME was not
2941 declared. */
2942 if (decl == error_mark_node)
2944 if (parser->scope && parser->scope != global_namespace)
2945 error_at (location, "%<%E::%E%> has not been declared",
2946 parser->scope, name);
2947 else if (parser->scope == global_namespace)
2948 error_at (location, "%<::%E%> has not been declared", name);
2949 else if (parser->object_scope
2950 && !CLASS_TYPE_P (parser->object_scope))
2951 error_at (location, "request for member %qE in non-class type %qT",
2952 name, parser->object_scope);
2953 else if (parser->object_scope)
2954 error_at (location, "%<%T::%E%> has not been declared",
2955 parser->object_scope, name);
2956 else
2957 error_at (location, "%qE has not been declared", name);
2959 else if (parser->scope && parser->scope != global_namespace)
2961 switch (desired)
2963 case NLE_TYPE:
2964 error_at (location, "%<%E::%E%> is not a type",
2965 parser->scope, name);
2966 break;
2967 case NLE_CXX98:
2968 error_at (location, "%<%E::%E%> is not a class or namespace",
2969 parser->scope, name);
2970 break;
2971 case NLE_NOT_CXX98:
2972 error_at (location,
2973 "%<%E::%E%> is not a class, namespace, or enumeration",
2974 parser->scope, name);
2975 break;
2976 default:
2977 gcc_unreachable ();
2981 else if (parser->scope == global_namespace)
2983 switch (desired)
2985 case NLE_TYPE:
2986 error_at (location, "%<::%E%> is not a type", name);
2987 break;
2988 case NLE_CXX98:
2989 error_at (location, "%<::%E%> is not a class or namespace", name);
2990 break;
2991 case NLE_NOT_CXX98:
2992 error_at (location,
2993 "%<::%E%> is not a class, namespace, or enumeration",
2994 name);
2995 break;
2996 default:
2997 gcc_unreachable ();
3000 else
3002 switch (desired)
3004 case NLE_TYPE:
3005 error_at (location, "%qE is not a type", name);
3006 break;
3007 case NLE_CXX98:
3008 error_at (location, "%qE is not a class or namespace", name);
3009 break;
3010 case NLE_NOT_CXX98:
3011 error_at (location,
3012 "%qE is not a class, namespace, or enumeration", name);
3013 break;
3014 default:
3015 gcc_unreachable ();
3020 /* If we are parsing tentatively, remember that an error has occurred
3021 during this tentative parse. Returns true if the error was
3022 simulated; false if a message should be issued by the caller. */
3024 static bool
3025 cp_parser_simulate_error (cp_parser* parser)
3027 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3029 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3030 return true;
3032 return false;
3035 /* This function is called when a type is defined. If type
3036 definitions are forbidden at this point, an error message is
3037 issued. */
3039 static bool
3040 cp_parser_check_type_definition (cp_parser* parser)
3042 /* If types are forbidden here, issue a message. */
3043 if (parser->type_definition_forbidden_message)
3045 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3046 in the message need to be interpreted. */
3047 error (parser->type_definition_forbidden_message);
3048 return false;
3050 return true;
3053 /* This function is called when the DECLARATOR is processed. The TYPE
3054 was a type defined in the decl-specifiers. If it is invalid to
3055 define a type in the decl-specifiers for DECLARATOR, an error is
3056 issued. TYPE_LOCATION is the location of TYPE and is used
3057 for error reporting. */
3059 static void
3060 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3061 tree type, location_t type_location)
3063 /* [dcl.fct] forbids type definitions in return types.
3064 Unfortunately, it's not easy to know whether or not we are
3065 processing a return type until after the fact. */
3066 while (declarator
3067 && (declarator->kind == cdk_pointer
3068 || declarator->kind == cdk_reference
3069 || declarator->kind == cdk_ptrmem))
3070 declarator = declarator->declarator;
3071 if (declarator
3072 && declarator->kind == cdk_function)
3074 error_at (type_location,
3075 "new types may not be defined in a return type");
3076 inform (type_location,
3077 "(perhaps a semicolon is missing after the definition of %qT)",
3078 type);
3082 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3083 "<" in any valid C++ program. If the next token is indeed "<",
3084 issue a message warning the user about what appears to be an
3085 invalid attempt to form a template-id. LOCATION is the location
3086 of the type-specifier (TYPE) */
3088 static void
3089 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3090 tree type,
3091 enum tag_types tag_type,
3092 location_t location)
3094 cp_token_position start = 0;
3096 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3098 if (TREE_CODE (type) == TYPE_DECL)
3099 type = TREE_TYPE (type);
3100 if (TYPE_P (type) && !template_placeholder_p (type))
3101 error_at (location, "%qT is not a template", type);
3102 else if (identifier_p (type))
3104 if (tag_type != none_type)
3105 error_at (location, "%qE is not a class template", type);
3106 else
3107 error_at (location, "%qE is not a template", type);
3109 else
3110 error_at (location, "invalid template-id");
3111 /* Remember the location of the invalid "<". */
3112 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3113 start = cp_lexer_token_position (parser->lexer, true);
3114 /* Consume the "<". */
3115 cp_lexer_consume_token (parser->lexer);
3116 /* Parse the template arguments. */
3117 cp_parser_enclosed_template_argument_list (parser);
3118 /* Permanently remove the invalid template arguments so that
3119 this error message is not issued again. */
3120 if (start)
3121 cp_lexer_purge_tokens_after (parser->lexer, start);
3125 /* If parsing an integral constant-expression, issue an error message
3126 about the fact that THING appeared and return true. Otherwise,
3127 return false. In either case, set
3128 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3130 static bool
3131 cp_parser_non_integral_constant_expression (cp_parser *parser,
3132 non_integral_constant thing)
3134 parser->non_integral_constant_expression_p = true;
3135 if (parser->integral_constant_expression_p)
3137 if (!parser->allow_non_integral_constant_expression_p)
3139 const char *msg = NULL;
3140 switch (thing)
3142 case NIC_FLOAT:
3143 pedwarn (input_location, OPT_Wpedantic,
3144 "ISO C++ forbids using a floating-point literal "
3145 "in a constant-expression");
3146 return true;
3147 case NIC_CAST:
3148 error ("a cast to a type other than an integral or "
3149 "enumeration type cannot appear in a "
3150 "constant-expression");
3151 return true;
3152 case NIC_TYPEID:
3153 error ("%<typeid%> operator "
3154 "cannot appear in a constant-expression");
3155 return true;
3156 case NIC_NCC:
3157 error ("non-constant compound literals "
3158 "cannot appear in a constant-expression");
3159 return true;
3160 case NIC_FUNC_CALL:
3161 error ("a function call "
3162 "cannot appear in a constant-expression");
3163 return true;
3164 case NIC_INC:
3165 error ("an increment "
3166 "cannot appear in a constant-expression");
3167 return true;
3168 case NIC_DEC:
3169 error ("an decrement "
3170 "cannot appear in a constant-expression");
3171 return true;
3172 case NIC_ARRAY_REF:
3173 error ("an array reference "
3174 "cannot appear in a constant-expression");
3175 return true;
3176 case NIC_ADDR_LABEL:
3177 error ("the address of a label "
3178 "cannot appear in a constant-expression");
3179 return true;
3180 case NIC_OVERLOADED:
3181 error ("calls to overloaded operators "
3182 "cannot appear in a constant-expression");
3183 return true;
3184 case NIC_ASSIGNMENT:
3185 error ("an assignment cannot appear in a constant-expression");
3186 return true;
3187 case NIC_COMMA:
3188 error ("a comma operator "
3189 "cannot appear in a constant-expression");
3190 return true;
3191 case NIC_CONSTRUCTOR:
3192 error ("a call to a constructor "
3193 "cannot appear in a constant-expression");
3194 return true;
3195 case NIC_TRANSACTION:
3196 error ("a transaction expression "
3197 "cannot appear in a constant-expression");
3198 return true;
3199 case NIC_THIS:
3200 msg = "this";
3201 break;
3202 case NIC_FUNC_NAME:
3203 msg = "__FUNCTION__";
3204 break;
3205 case NIC_PRETTY_FUNC:
3206 msg = "__PRETTY_FUNCTION__";
3207 break;
3208 case NIC_C99_FUNC:
3209 msg = "__func__";
3210 break;
3211 case NIC_VA_ARG:
3212 msg = "va_arg";
3213 break;
3214 case NIC_ARROW:
3215 msg = "->";
3216 break;
3217 case NIC_POINT:
3218 msg = ".";
3219 break;
3220 case NIC_STAR:
3221 msg = "*";
3222 break;
3223 case NIC_ADDR:
3224 msg = "&";
3225 break;
3226 case NIC_PREINCREMENT:
3227 msg = "++";
3228 break;
3229 case NIC_PREDECREMENT:
3230 msg = "--";
3231 break;
3232 case NIC_NEW:
3233 msg = "new";
3234 break;
3235 case NIC_DEL:
3236 msg = "delete";
3237 break;
3238 default:
3239 gcc_unreachable ();
3241 if (msg)
3242 error ("%qs cannot appear in a constant-expression", msg);
3243 return true;
3246 return false;
3249 /* Emit a diagnostic for an invalid type name. This function commits
3250 to the current active tentative parse, if any. (Otherwise, the
3251 problematic construct might be encountered again later, resulting
3252 in duplicate error messages.) LOCATION is the location of ID. */
3254 static void
3255 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3256 location_t location)
3258 tree decl, ambiguous_decls;
3259 cp_parser_commit_to_tentative_parse (parser);
3260 /* Try to lookup the identifier. */
3261 decl = cp_parser_lookup_name (parser, id, none_type,
3262 /*is_template=*/false,
3263 /*is_namespace=*/false,
3264 /*check_dependency=*/true,
3265 &ambiguous_decls, location);
3266 if (ambiguous_decls)
3267 /* If the lookup was ambiguous, an error will already have
3268 been issued. */
3269 return;
3270 /* If the lookup found a template-name, it means that the user forgot
3271 to specify an argument list. Emit a useful error message. */
3272 if (DECL_TYPE_TEMPLATE_P (decl))
3274 error_at (location,
3275 "invalid use of template-name %qE without an argument list",
3276 decl);
3277 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3278 inform (location, "class template argument deduction is only available "
3279 "with -std=c++17 or -std=gnu++17");
3280 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3282 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3283 error_at (location, "invalid use of destructor %qD as a type", id);
3284 else if (TREE_CODE (decl) == TYPE_DECL)
3285 /* Something like 'unsigned A a;' */
3286 error_at (location, "invalid combination of multiple type-specifiers");
3287 else if (!parser->scope)
3289 /* Issue an error message. */
3290 const char *suggestion = NULL;
3291 if (TREE_CODE (id) == IDENTIFIER_NODE)
3292 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3293 if (suggestion)
3295 gcc_rich_location richloc (location);
3296 richloc.add_fixit_replace (suggestion);
3297 error_at_rich_loc (&richloc,
3298 "%qE does not name a type; did you mean %qs?",
3299 id, suggestion);
3301 else
3302 error_at (location, "%qE does not name a type", id);
3303 /* If we're in a template class, it's possible that the user was
3304 referring to a type from a base class. For example:
3306 template <typename T> struct A { typedef T X; };
3307 template <typename T> struct B : public A<T> { X x; };
3309 The user should have said "typename A<T>::X". */
3310 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3311 inform (location, "C++11 %<constexpr%> only available with "
3312 "-std=c++11 or -std=gnu++11");
3313 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3314 inform (location, "C++11 %<noexcept%> only available with "
3315 "-std=c++11 or -std=gnu++11");
3316 else if (cxx_dialect < cxx11
3317 && TREE_CODE (id) == IDENTIFIER_NODE
3318 && id_equal (id, "thread_local"))
3319 inform (location, "C++11 %<thread_local%> only available with "
3320 "-std=c++11 or -std=gnu++11");
3321 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3322 inform (location, "%<concept%> only available with -fconcepts");
3323 else if (processing_template_decl && current_class_type
3324 && TYPE_BINFO (current_class_type))
3326 tree b;
3328 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3330 b = TREE_CHAIN (b))
3332 tree base_type = BINFO_TYPE (b);
3333 if (CLASS_TYPE_P (base_type)
3334 && dependent_type_p (base_type))
3336 tree field;
3337 /* Go from a particular instantiation of the
3338 template (which will have an empty TYPE_FIELDs),
3339 to the main version. */
3340 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3341 for (field = TYPE_FIELDS (base_type);
3342 field;
3343 field = DECL_CHAIN (field))
3344 if (TREE_CODE (field) == TYPE_DECL
3345 && DECL_NAME (field) == id)
3347 inform (location,
3348 "(perhaps %<typename %T::%E%> was intended)",
3349 BINFO_TYPE (b), id);
3350 break;
3352 if (field)
3353 break;
3358 /* Here we diagnose qualified-ids where the scope is actually correct,
3359 but the identifier does not resolve to a valid type name. */
3360 else if (parser->scope != error_mark_node)
3362 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3364 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3365 error_at (location_of (id),
3366 "%qE in namespace %qE does not name a template type",
3367 id, parser->scope);
3368 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3369 error_at (location_of (id),
3370 "%qE in namespace %qE does not name a template type",
3371 TREE_OPERAND (id, 0), parser->scope);
3372 else
3373 error_at (location_of (id),
3374 "%qE in namespace %qE does not name a type",
3375 id, parser->scope);
3376 if (DECL_P (decl))
3377 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3379 else if (CLASS_TYPE_P (parser->scope)
3380 && constructor_name_p (id, parser->scope))
3382 /* A<T>::A<T>() */
3383 error_at (location, "%<%T::%E%> names the constructor, not"
3384 " the type", parser->scope, id);
3385 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3386 error_at (location, "and %qT has no template constructors",
3387 parser->scope);
3389 else if (TYPE_P (parser->scope)
3390 && dependent_scope_p (parser->scope))
3392 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3393 error_at (location,
3394 "need %<typename%> before %<%T::%D::%E%> because "
3395 "%<%T::%D%> is a dependent scope",
3396 TYPE_CONTEXT (parser->scope),
3397 TYPENAME_TYPE_FULLNAME (parser->scope),
3399 TYPE_CONTEXT (parser->scope),
3400 TYPENAME_TYPE_FULLNAME (parser->scope));
3401 else
3402 error_at (location, "need %<typename%> before %<%T::%E%> because "
3403 "%qT is a dependent scope",
3404 parser->scope, id, parser->scope);
3406 else if (TYPE_P (parser->scope))
3408 if (!COMPLETE_TYPE_P (parser->scope))
3409 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3410 parser->scope);
3411 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3412 error_at (location_of (id),
3413 "%qE in %q#T does not name a template type",
3414 id, parser->scope);
3415 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3416 error_at (location_of (id),
3417 "%qE in %q#T does not name a template type",
3418 TREE_OPERAND (id, 0), parser->scope);
3419 else
3420 error_at (location_of (id),
3421 "%qE in %q#T does not name a type",
3422 id, parser->scope);
3423 if (DECL_P (decl))
3424 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3426 else
3427 gcc_unreachable ();
3431 /* Check for a common situation where a type-name should be present,
3432 but is not, and issue a sensible error message. Returns true if an
3433 invalid type-name was detected.
3435 The situation handled by this function are variable declarations of the
3436 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3437 Usually, `ID' should name a type, but if we got here it means that it
3438 does not. We try to emit the best possible error message depending on
3439 how exactly the id-expression looks like. */
3441 static bool
3442 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3444 tree id;
3445 cp_token *token = cp_lexer_peek_token (parser->lexer);
3447 /* Avoid duplicate error about ambiguous lookup. */
3448 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3450 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3451 if (next->type == CPP_NAME && next->error_reported)
3452 goto out;
3455 cp_parser_parse_tentatively (parser);
3456 id = cp_parser_id_expression (parser,
3457 /*template_keyword_p=*/false,
3458 /*check_dependency_p=*/true,
3459 /*template_p=*/NULL,
3460 /*declarator_p=*/true,
3461 /*optional_p=*/false);
3462 /* If the next token is a (, this is a function with no explicit return
3463 type, i.e. constructor, destructor or conversion op. */
3464 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3465 || TREE_CODE (id) == TYPE_DECL)
3467 cp_parser_abort_tentative_parse (parser);
3468 return false;
3470 if (!cp_parser_parse_definitely (parser))
3471 return false;
3473 /* Emit a diagnostic for the invalid type. */
3474 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3475 out:
3476 /* If we aren't in the middle of a declarator (i.e. in a
3477 parameter-declaration-clause), skip to the end of the declaration;
3478 there's no point in trying to process it. */
3479 if (!parser->in_declarator_p)
3480 cp_parser_skip_to_end_of_block_or_statement (parser);
3481 return true;
3484 /* Consume tokens up to, and including, the next non-nested closing `)'.
3485 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3486 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3487 found an unnested token of that type. */
3489 static int
3490 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3491 bool recovering,
3492 cpp_ttype or_ttype,
3493 bool consume_paren)
3495 unsigned paren_depth = 0;
3496 unsigned brace_depth = 0;
3497 unsigned square_depth = 0;
3499 if (recovering && or_ttype == CPP_EOF
3500 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3501 return 0;
3503 while (true)
3505 cp_token * token = cp_lexer_peek_token (parser->lexer);
3507 /* Have we found what we're looking for before the closing paren? */
3508 if (token->type == or_ttype && or_ttype != CPP_EOF
3509 && !brace_depth && !paren_depth && !square_depth)
3510 return -1;
3512 switch (token->type)
3514 case CPP_EOF:
3515 case CPP_PRAGMA_EOL:
3516 /* If we've run out of tokens, then there is no closing `)'. */
3517 return 0;
3519 /* This is good for lambda expression capture-lists. */
3520 case CPP_OPEN_SQUARE:
3521 ++square_depth;
3522 break;
3523 case CPP_CLOSE_SQUARE:
3524 if (!square_depth--)
3525 return 0;
3526 break;
3528 case CPP_SEMICOLON:
3529 /* This matches the processing in skip_to_end_of_statement. */
3530 if (!brace_depth)
3531 return 0;
3532 break;
3534 case CPP_OPEN_BRACE:
3535 ++brace_depth;
3536 break;
3537 case CPP_CLOSE_BRACE:
3538 if (!brace_depth--)
3539 return 0;
3540 break;
3542 case CPP_OPEN_PAREN:
3543 if (!brace_depth)
3544 ++paren_depth;
3545 break;
3547 case CPP_CLOSE_PAREN:
3548 if (!brace_depth && !paren_depth--)
3550 if (consume_paren)
3551 cp_lexer_consume_token (parser->lexer);
3552 return 1;
3554 break;
3556 default:
3557 break;
3560 /* Consume the token. */
3561 cp_lexer_consume_token (parser->lexer);
3565 /* Consume tokens up to, and including, the next non-nested closing `)'.
3566 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3567 are doing error recovery. Returns -1 if OR_COMMA is true and we
3568 found an unnested token of that type. */
3570 static int
3571 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3572 bool recovering,
3573 bool or_comma,
3574 bool consume_paren)
3576 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3577 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3578 ttype, consume_paren);
3581 /* Consume tokens until we reach the end of the current statement.
3582 Normally, that will be just before consuming a `;'. However, if a
3583 non-nested `}' comes first, then we stop before consuming that. */
3585 static void
3586 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3588 unsigned nesting_depth = 0;
3590 /* Unwind generic function template scope if necessary. */
3591 if (parser->fully_implicit_function_template_p)
3592 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3594 while (true)
3596 cp_token *token = cp_lexer_peek_token (parser->lexer);
3598 switch (token->type)
3600 case CPP_EOF:
3601 case CPP_PRAGMA_EOL:
3602 /* If we've run out of tokens, stop. */
3603 return;
3605 case CPP_SEMICOLON:
3606 /* If the next token is a `;', we have reached the end of the
3607 statement. */
3608 if (!nesting_depth)
3609 return;
3610 break;
3612 case CPP_CLOSE_BRACE:
3613 /* If this is a non-nested '}', stop before consuming it.
3614 That way, when confronted with something like:
3616 { 3 + }
3618 we stop before consuming the closing '}', even though we
3619 have not yet reached a `;'. */
3620 if (nesting_depth == 0)
3621 return;
3623 /* If it is the closing '}' for a block that we have
3624 scanned, stop -- but only after consuming the token.
3625 That way given:
3627 void f g () { ... }
3628 typedef int I;
3630 we will stop after the body of the erroneously declared
3631 function, but before consuming the following `typedef'
3632 declaration. */
3633 if (--nesting_depth == 0)
3635 cp_lexer_consume_token (parser->lexer);
3636 return;
3638 break;
3640 case CPP_OPEN_BRACE:
3641 ++nesting_depth;
3642 break;
3644 default:
3645 break;
3648 /* Consume the token. */
3649 cp_lexer_consume_token (parser->lexer);
3653 /* This function is called at the end of a statement or declaration.
3654 If the next token is a semicolon, it is consumed; otherwise, error
3655 recovery is attempted. */
3657 static void
3658 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3660 /* Look for the trailing `;'. */
3661 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3663 /* If there is additional (erroneous) input, skip to the end of
3664 the statement. */
3665 cp_parser_skip_to_end_of_statement (parser);
3666 /* If the next token is now a `;', consume it. */
3667 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3668 cp_lexer_consume_token (parser->lexer);
3672 /* Skip tokens until we have consumed an entire block, or until we
3673 have consumed a non-nested `;'. */
3675 static void
3676 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3678 int nesting_depth = 0;
3680 /* Unwind generic function template scope if necessary. */
3681 if (parser->fully_implicit_function_template_p)
3682 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3684 while (nesting_depth >= 0)
3686 cp_token *token = cp_lexer_peek_token (parser->lexer);
3688 switch (token->type)
3690 case CPP_EOF:
3691 case CPP_PRAGMA_EOL:
3692 /* If we've run out of tokens, stop. */
3693 return;
3695 case CPP_SEMICOLON:
3696 /* Stop if this is an unnested ';'. */
3697 if (!nesting_depth)
3698 nesting_depth = -1;
3699 break;
3701 case CPP_CLOSE_BRACE:
3702 /* Stop if this is an unnested '}', or closes the outermost
3703 nesting level. */
3704 nesting_depth--;
3705 if (nesting_depth < 0)
3706 return;
3707 if (!nesting_depth)
3708 nesting_depth = -1;
3709 break;
3711 case CPP_OPEN_BRACE:
3712 /* Nest. */
3713 nesting_depth++;
3714 break;
3716 default:
3717 break;
3720 /* Consume the token. */
3721 cp_lexer_consume_token (parser->lexer);
3725 /* Skip tokens until a non-nested closing curly brace is the next
3726 token, or there are no more tokens. Return true in the first case,
3727 false otherwise. */
3729 static bool
3730 cp_parser_skip_to_closing_brace (cp_parser *parser)
3732 unsigned nesting_depth = 0;
3734 while (true)
3736 cp_token *token = cp_lexer_peek_token (parser->lexer);
3738 switch (token->type)
3740 case CPP_EOF:
3741 case CPP_PRAGMA_EOL:
3742 /* If we've run out of tokens, stop. */
3743 return false;
3745 case CPP_CLOSE_BRACE:
3746 /* If the next token is a non-nested `}', then we have reached
3747 the end of the current block. */
3748 if (nesting_depth-- == 0)
3749 return true;
3750 break;
3752 case CPP_OPEN_BRACE:
3753 /* If it the next token is a `{', then we are entering a new
3754 block. Consume the entire block. */
3755 ++nesting_depth;
3756 break;
3758 default:
3759 break;
3762 /* Consume the token. */
3763 cp_lexer_consume_token (parser->lexer);
3767 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3768 parameter is the PRAGMA token, allowing us to purge the entire pragma
3769 sequence. */
3771 static void
3772 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3774 cp_token *token;
3776 parser->lexer->in_pragma = false;
3779 token = cp_lexer_consume_token (parser->lexer);
3780 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3782 /* Ensure that the pragma is not parsed again. */
3783 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3786 /* Require pragma end of line, resyncing with it as necessary. The
3787 arguments are as for cp_parser_skip_to_pragma_eol. */
3789 static void
3790 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3792 parser->lexer->in_pragma = false;
3793 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3794 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3797 /* This is a simple wrapper around make_typename_type. When the id is
3798 an unresolved identifier node, we can provide a superior diagnostic
3799 using cp_parser_diagnose_invalid_type_name. */
3801 static tree
3802 cp_parser_make_typename_type (cp_parser *parser, tree id,
3803 location_t id_location)
3805 tree result;
3806 if (identifier_p (id))
3808 result = make_typename_type (parser->scope, id, typename_type,
3809 /*complain=*/tf_none);
3810 if (result == error_mark_node)
3811 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3812 return result;
3814 return make_typename_type (parser->scope, id, typename_type, tf_error);
3817 /* This is a wrapper around the
3818 make_{pointer,ptrmem,reference}_declarator functions that decides
3819 which one to call based on the CODE and CLASS_TYPE arguments. The
3820 CODE argument should be one of the values returned by
3821 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3822 appertain to the pointer or reference. */
3824 static cp_declarator *
3825 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3826 cp_cv_quals cv_qualifiers,
3827 cp_declarator *target,
3828 tree attributes)
3830 if (code == ERROR_MARK)
3831 return cp_error_declarator;
3833 if (code == INDIRECT_REF)
3834 if (class_type == NULL_TREE)
3835 return make_pointer_declarator (cv_qualifiers, target, attributes);
3836 else
3837 return make_ptrmem_declarator (cv_qualifiers, class_type,
3838 target, attributes);
3839 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3840 return make_reference_declarator (cv_qualifiers, target,
3841 false, attributes);
3842 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3843 return make_reference_declarator (cv_qualifiers, target,
3844 true, attributes);
3845 gcc_unreachable ();
3848 /* Create a new C++ parser. */
3850 static cp_parser *
3851 cp_parser_new (void)
3853 cp_parser *parser;
3854 cp_lexer *lexer;
3855 unsigned i;
3857 /* cp_lexer_new_main is called before doing GC allocation because
3858 cp_lexer_new_main might load a PCH file. */
3859 lexer = cp_lexer_new_main ();
3861 /* Initialize the binops_by_token so that we can get the tree
3862 directly from the token. */
3863 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3864 binops_by_token[binops[i].token_type] = binops[i];
3866 parser = ggc_cleared_alloc<cp_parser> ();
3867 parser->lexer = lexer;
3868 parser->context = cp_parser_context_new (NULL);
3870 /* For now, we always accept GNU extensions. */
3871 parser->allow_gnu_extensions_p = 1;
3873 /* The `>' token is a greater-than operator, not the end of a
3874 template-id. */
3875 parser->greater_than_is_operator_p = true;
3877 parser->default_arg_ok_p = true;
3879 /* We are not parsing a constant-expression. */
3880 parser->integral_constant_expression_p = false;
3881 parser->allow_non_integral_constant_expression_p = false;
3882 parser->non_integral_constant_expression_p = false;
3884 /* Local variable names are not forbidden. */
3885 parser->local_variables_forbidden_p = false;
3887 /* We are not processing an `extern "C"' declaration. */
3888 parser->in_unbraced_linkage_specification_p = false;
3890 /* We are not processing a declarator. */
3891 parser->in_declarator_p = false;
3893 /* We are not processing a template-argument-list. */
3894 parser->in_template_argument_list_p = false;
3896 /* We are not in an iteration statement. */
3897 parser->in_statement = 0;
3899 /* We are not in a switch statement. */
3900 parser->in_switch_statement_p = false;
3902 /* We are not parsing a type-id inside an expression. */
3903 parser->in_type_id_in_expr_p = false;
3905 /* Declarations aren't implicitly extern "C". */
3906 parser->implicit_extern_c = false;
3908 /* String literals should be translated to the execution character set. */
3909 parser->translate_strings_p = true;
3911 /* We are not parsing a function body. */
3912 parser->in_function_body = false;
3914 /* We can correct until told otherwise. */
3915 parser->colon_corrects_to_scope_p = true;
3917 /* The unparsed function queue is empty. */
3918 push_unparsed_function_queues (parser);
3920 /* There are no classes being defined. */
3921 parser->num_classes_being_defined = 0;
3923 /* No template parameters apply. */
3924 parser->num_template_parameter_lists = 0;
3926 /* Special parsing data structures. */
3927 parser->omp_declare_simd = NULL;
3928 parser->cilk_simd_fn_info = NULL;
3929 parser->oacc_routine = NULL;
3931 /* Not declaring an implicit function template. */
3932 parser->auto_is_implicit_function_template_parm_p = false;
3933 parser->fully_implicit_function_template_p = false;
3934 parser->implicit_template_parms = 0;
3935 parser->implicit_template_scope = 0;
3937 /* Allow constrained-type-specifiers. */
3938 parser->prevent_constrained_type_specifiers = 0;
3940 /* We haven't yet seen an 'extern "C"'. */
3941 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
3943 return parser;
3946 /* Create a cp_lexer structure which will emit the tokens in CACHE
3947 and push it onto the parser's lexer stack. This is used for delayed
3948 parsing of in-class method bodies and default arguments, and should
3949 not be confused with tentative parsing. */
3950 static void
3951 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3953 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3954 lexer->next = parser->lexer;
3955 parser->lexer = lexer;
3957 /* Move the current source position to that of the first token in the
3958 new lexer. */
3959 cp_lexer_set_source_position_from_token (lexer->next_token);
3962 /* Pop the top lexer off the parser stack. This is never used for the
3963 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3964 static void
3965 cp_parser_pop_lexer (cp_parser *parser)
3967 cp_lexer *lexer = parser->lexer;
3968 parser->lexer = lexer->next;
3969 cp_lexer_destroy (lexer);
3971 /* Put the current source position back where it was before this
3972 lexer was pushed. */
3973 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3976 /* Lexical conventions [gram.lex] */
3978 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3979 identifier. */
3981 static cp_expr
3982 cp_parser_identifier (cp_parser* parser)
3984 cp_token *token;
3986 /* Look for the identifier. */
3987 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3988 /* Return the value. */
3989 if (token)
3990 return cp_expr (token->u.value, token->location);
3991 else
3992 return error_mark_node;
3995 /* Parse a sequence of adjacent string constants. Returns a
3996 TREE_STRING representing the combined, nul-terminated string
3997 constant. If TRANSLATE is true, translate the string to the
3998 execution character set. If WIDE_OK is true, a wide string is
3999 invalid here.
4001 C++98 [lex.string] says that if a narrow string literal token is
4002 adjacent to a wide string literal token, the behavior is undefined.
4003 However, C99 6.4.5p4 says that this results in a wide string literal.
4004 We follow C99 here, for consistency with the C front end.
4006 This code is largely lifted from lex_string() in c-lex.c.
4008 FUTURE: ObjC++ will need to handle @-strings here. */
4009 static cp_expr
4010 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4011 bool lookup_udlit = true)
4013 tree value;
4014 size_t count;
4015 struct obstack str_ob;
4016 cpp_string str, istr, *strs;
4017 cp_token *tok;
4018 enum cpp_ttype type, curr_type;
4019 int have_suffix_p = 0;
4020 tree string_tree;
4021 tree suffix_id = NULL_TREE;
4022 bool curr_tok_is_userdef_p = false;
4024 tok = cp_lexer_peek_token (parser->lexer);
4025 if (!cp_parser_is_string_literal (tok))
4027 cp_parser_error (parser, "expected string-literal");
4028 return error_mark_node;
4031 location_t loc = tok->location;
4033 if (cpp_userdef_string_p (tok->type))
4035 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4036 curr_type = cpp_userdef_string_remove_type (tok->type);
4037 curr_tok_is_userdef_p = true;
4039 else
4041 string_tree = tok->u.value;
4042 curr_type = tok->type;
4044 type = curr_type;
4046 /* Try to avoid the overhead of creating and destroying an obstack
4047 for the common case of just one string. */
4048 if (!cp_parser_is_string_literal
4049 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4051 cp_lexer_consume_token (parser->lexer);
4053 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4054 str.len = TREE_STRING_LENGTH (string_tree);
4055 count = 1;
4057 if (curr_tok_is_userdef_p)
4059 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4060 have_suffix_p = 1;
4061 curr_type = cpp_userdef_string_remove_type (tok->type);
4063 else
4064 curr_type = tok->type;
4066 strs = &str;
4068 else
4070 location_t last_tok_loc = tok->location;
4071 gcc_obstack_init (&str_ob);
4072 count = 0;
4076 cp_lexer_consume_token (parser->lexer);
4077 count++;
4078 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4079 str.len = TREE_STRING_LENGTH (string_tree);
4081 if (curr_tok_is_userdef_p)
4083 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4084 if (have_suffix_p == 0)
4086 suffix_id = curr_suffix_id;
4087 have_suffix_p = 1;
4089 else if (have_suffix_p == 1
4090 && curr_suffix_id != suffix_id)
4092 error ("inconsistent user-defined literal suffixes"
4093 " %qD and %qD in string literal",
4094 suffix_id, curr_suffix_id);
4095 have_suffix_p = -1;
4097 curr_type = cpp_userdef_string_remove_type (tok->type);
4099 else
4100 curr_type = tok->type;
4102 if (type != curr_type)
4104 if (type == CPP_STRING)
4105 type = curr_type;
4106 else if (curr_type != CPP_STRING)
4108 rich_location rich_loc (line_table, tok->location);
4109 rich_loc.add_range (last_tok_loc, false);
4110 error_at_rich_loc (&rich_loc,
4111 "unsupported non-standard concatenation "
4112 "of string literals");
4116 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4118 last_tok_loc = tok->location;
4120 tok = cp_lexer_peek_token (parser->lexer);
4121 if (cpp_userdef_string_p (tok->type))
4123 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4124 curr_type = cpp_userdef_string_remove_type (tok->type);
4125 curr_tok_is_userdef_p = true;
4127 else
4129 string_tree = tok->u.value;
4130 curr_type = tok->type;
4131 curr_tok_is_userdef_p = false;
4134 while (cp_parser_is_string_literal (tok));
4136 /* A string literal built by concatenation has its caret=start at
4137 the start of the initial string, and its finish at the finish of
4138 the final string literal. */
4139 loc = make_location (loc, loc, get_finish (last_tok_loc));
4141 strs = (cpp_string *) obstack_finish (&str_ob);
4144 if (type != CPP_STRING && !wide_ok)
4146 cp_parser_error (parser, "a wide string is invalid in this context");
4147 type = CPP_STRING;
4150 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4151 (parse_in, strs, count, &istr, type))
4153 value = build_string (istr.len, (const char *)istr.text);
4154 free (CONST_CAST (unsigned char *, istr.text));
4156 switch (type)
4158 default:
4159 case CPP_STRING:
4160 case CPP_UTF8STRING:
4161 TREE_TYPE (value) = char_array_type_node;
4162 break;
4163 case CPP_STRING16:
4164 TREE_TYPE (value) = char16_array_type_node;
4165 break;
4166 case CPP_STRING32:
4167 TREE_TYPE (value) = char32_array_type_node;
4168 break;
4169 case CPP_WSTRING:
4170 TREE_TYPE (value) = wchar_array_type_node;
4171 break;
4174 value = fix_string_type (value);
4176 if (have_suffix_p)
4178 tree literal = build_userdef_literal (suffix_id, value,
4179 OT_NONE, NULL_TREE);
4180 if (lookup_udlit)
4181 value = cp_parser_userdef_string_literal (literal);
4182 else
4183 value = literal;
4186 else
4187 /* cpp_interpret_string has issued an error. */
4188 value = error_mark_node;
4190 if (count > 1)
4191 obstack_free (&str_ob, 0);
4193 return cp_expr (value, loc);
4196 /* Look up a literal operator with the name and the exact arguments. */
4198 static tree
4199 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4201 tree decl;
4202 decl = lookup_name (name);
4203 if (!decl || !is_overloaded_fn (decl))
4204 return error_mark_node;
4206 for (lkp_iterator iter (decl); iter; ++iter)
4208 unsigned int ix;
4209 bool found = true;
4210 tree fn = *iter;
4211 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4212 if (parmtypes != NULL_TREE)
4214 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4215 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4217 tree tparm = TREE_VALUE (parmtypes);
4218 tree targ = TREE_TYPE ((*args)[ix]);
4219 bool ptr = TYPE_PTR_P (tparm);
4220 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4221 if ((ptr || arr || !same_type_p (tparm, targ))
4222 && (!ptr || !arr
4223 || !same_type_p (TREE_TYPE (tparm),
4224 TREE_TYPE (targ))))
4225 found = false;
4227 if (found
4228 && ix == vec_safe_length (args)
4229 /* May be this should be sufficient_parms_p instead,
4230 depending on how exactly should user-defined literals
4231 work in presence of default arguments on the literal
4232 operator parameters. */
4233 && parmtypes == void_list_node)
4234 return decl;
4238 return error_mark_node;
4241 /* Parse a user-defined char constant. Returns a call to a user-defined
4242 literal operator taking the character as an argument. */
4244 static cp_expr
4245 cp_parser_userdef_char_literal (cp_parser *parser)
4247 cp_token *token = cp_lexer_consume_token (parser->lexer);
4248 tree literal = token->u.value;
4249 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4250 tree value = USERDEF_LITERAL_VALUE (literal);
4251 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4252 tree decl, result;
4254 /* Build up a call to the user-defined operator */
4255 /* Lookup the name we got back from the id-expression. */
4256 vec<tree, va_gc> *args = make_tree_vector ();
4257 vec_safe_push (args, value);
4258 decl = lookup_literal_operator (name, args);
4259 if (!decl || decl == error_mark_node)
4261 error ("unable to find character literal operator %qD with %qT argument",
4262 name, TREE_TYPE (value));
4263 release_tree_vector (args);
4264 return error_mark_node;
4266 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4267 release_tree_vector (args);
4268 return result;
4271 /* A subroutine of cp_parser_userdef_numeric_literal to
4272 create a char... template parameter pack from a string node. */
4274 static tree
4275 make_char_string_pack (tree value)
4277 tree charvec;
4278 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4279 const char *str = TREE_STRING_POINTER (value);
4280 int i, len = TREE_STRING_LENGTH (value) - 1;
4281 tree argvec = make_tree_vec (1);
4283 /* Fill in CHARVEC with all of the parameters. */
4284 charvec = make_tree_vec (len);
4285 for (i = 0; i < len; ++i)
4286 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4288 /* Build the argument packs. */
4289 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4291 TREE_VEC_ELT (argvec, 0) = argpack;
4293 return argvec;
4296 /* A subroutine of cp_parser_userdef_numeric_literal to
4297 create a char... template parameter pack from a string node. */
4299 static tree
4300 make_string_pack (tree value)
4302 tree charvec;
4303 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4304 const unsigned char *str
4305 = (const unsigned char *) TREE_STRING_POINTER (value);
4306 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4307 int len = TREE_STRING_LENGTH (value) / sz - 1;
4308 tree argvec = make_tree_vec (2);
4310 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4311 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4313 /* First template parm is character type. */
4314 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4316 /* Fill in CHARVEC with all of the parameters. */
4317 charvec = make_tree_vec (len);
4318 for (int i = 0; i < len; ++i)
4319 TREE_VEC_ELT (charvec, i)
4320 = double_int_to_tree (str_char_type_node,
4321 double_int::from_buffer (str + i * sz, sz));
4323 /* Build the argument packs. */
4324 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4326 TREE_VEC_ELT (argvec, 1) = argpack;
4328 return argvec;
4331 /* Parse a user-defined numeric constant. returns a call to a user-defined
4332 literal operator. */
4334 static cp_expr
4335 cp_parser_userdef_numeric_literal (cp_parser *parser)
4337 cp_token *token = cp_lexer_consume_token (parser->lexer);
4338 tree literal = token->u.value;
4339 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4340 tree value = USERDEF_LITERAL_VALUE (literal);
4341 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4342 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4343 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4344 tree decl, result;
4345 vec<tree, va_gc> *args;
4347 /* Look for a literal operator taking the exact type of numeric argument
4348 as the literal value. */
4349 args = make_tree_vector ();
4350 vec_safe_push (args, value);
4351 decl = lookup_literal_operator (name, args);
4352 if (decl && decl != error_mark_node)
4354 result = finish_call_expr (decl, &args, false, true,
4355 tf_warning_or_error);
4357 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4359 warning_at (token->location, OPT_Woverflow,
4360 "integer literal exceeds range of %qT type",
4361 long_long_unsigned_type_node);
4363 else
4365 if (overflow > 0)
4366 warning_at (token->location, OPT_Woverflow,
4367 "floating literal exceeds range of %qT type",
4368 long_double_type_node);
4369 else if (overflow < 0)
4370 warning_at (token->location, OPT_Woverflow,
4371 "floating literal truncated to zero");
4374 release_tree_vector (args);
4375 return result;
4377 release_tree_vector (args);
4379 /* If the numeric argument didn't work, look for a raw literal
4380 operator taking a const char* argument consisting of the number
4381 in string format. */
4382 args = make_tree_vector ();
4383 vec_safe_push (args, num_string);
4384 decl = lookup_literal_operator (name, args);
4385 if (decl && decl != error_mark_node)
4387 result = finish_call_expr (decl, &args, false, true,
4388 tf_warning_or_error);
4389 release_tree_vector (args);
4390 return result;
4392 release_tree_vector (args);
4394 /* If the raw literal didn't work, look for a non-type template
4395 function with parameter pack char.... Call the function with
4396 template parameter characters representing the number. */
4397 args = make_tree_vector ();
4398 decl = lookup_literal_operator (name, args);
4399 if (decl && decl != error_mark_node)
4401 tree tmpl_args = make_char_string_pack (num_string);
4402 decl = lookup_template_function (decl, tmpl_args);
4403 result = finish_call_expr (decl, &args, false, true,
4404 tf_warning_or_error);
4405 release_tree_vector (args);
4406 return result;
4409 release_tree_vector (args);
4411 error ("unable to find numeric literal operator %qD", name);
4412 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4413 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4414 "to enable more built-in suffixes");
4415 return error_mark_node;
4418 /* Parse a user-defined string constant. Returns a call to a user-defined
4419 literal operator taking a character pointer and the length of the string
4420 as arguments. */
4422 static tree
4423 cp_parser_userdef_string_literal (tree literal)
4425 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4426 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4427 tree value = USERDEF_LITERAL_VALUE (literal);
4428 int len = TREE_STRING_LENGTH (value)
4429 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4430 tree decl, result;
4431 vec<tree, va_gc> *args;
4433 /* Build up a call to the user-defined operator. */
4434 /* Lookup the name we got back from the id-expression. */
4435 args = make_tree_vector ();
4436 vec_safe_push (args, value);
4437 vec_safe_push (args, build_int_cst (size_type_node, len));
4438 decl = lookup_literal_operator (name, args);
4440 if (decl && decl != error_mark_node)
4442 result = finish_call_expr (decl, &args, false, true,
4443 tf_warning_or_error);
4444 release_tree_vector (args);
4445 return result;
4447 release_tree_vector (args);
4449 /* Look for a template function with typename parameter CharT
4450 and parameter pack CharT... Call the function with
4451 template parameter characters representing the string. */
4452 args = make_tree_vector ();
4453 decl = lookup_literal_operator (name, args);
4454 if (decl && decl != error_mark_node)
4456 tree tmpl_args = make_string_pack (value);
4457 decl = lookup_template_function (decl, tmpl_args);
4458 result = finish_call_expr (decl, &args, false, true,
4459 tf_warning_or_error);
4460 release_tree_vector (args);
4461 return result;
4463 release_tree_vector (args);
4465 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4466 name, TREE_TYPE (value), size_type_node);
4467 return error_mark_node;
4471 /* Basic concepts [gram.basic] */
4473 /* Parse a translation-unit.
4475 translation-unit:
4476 declaration-seq [opt]
4478 Returns TRUE if all went well. */
4480 static bool
4481 cp_parser_translation_unit (cp_parser* parser)
4483 /* The address of the first non-permanent object on the declarator
4484 obstack. */
4485 static void *declarator_obstack_base;
4487 bool success;
4489 /* Create the declarator obstack, if necessary. */
4490 if (!cp_error_declarator)
4492 gcc_obstack_init (&declarator_obstack);
4493 /* Create the error declarator. */
4494 cp_error_declarator = make_declarator (cdk_error);
4495 /* Create the empty parameter list. */
4496 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4497 UNKNOWN_LOCATION);
4498 /* Remember where the base of the declarator obstack lies. */
4499 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4502 cp_parser_declaration_seq_opt (parser);
4504 /* If there are no tokens left then all went well. */
4505 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4507 /* Get rid of the token array; we don't need it any more. */
4508 cp_lexer_destroy (parser->lexer);
4509 parser->lexer = NULL;
4511 /* This file might have been a context that's implicitly extern
4512 "C". If so, pop the lang context. (Only relevant for PCH.) */
4513 if (parser->implicit_extern_c)
4515 pop_lang_context ();
4516 parser->implicit_extern_c = false;
4519 /* Finish up. */
4520 finish_translation_unit ();
4522 success = true;
4524 else
4526 cp_parser_error (parser, "expected declaration");
4527 success = false;
4530 /* Make sure the declarator obstack was fully cleaned up. */
4531 gcc_assert (obstack_next_free (&declarator_obstack)
4532 == declarator_obstack_base);
4534 /* All went well. */
4535 return success;
4538 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4539 decltype context. */
4541 static inline tsubst_flags_t
4542 complain_flags (bool decltype_p)
4544 tsubst_flags_t complain = tf_warning_or_error;
4545 if (decltype_p)
4546 complain |= tf_decltype;
4547 return complain;
4550 /* We're about to parse a collection of statements. If we're currently
4551 parsing tentatively, set up a firewall so that any nested
4552 cp_parser_commit_to_tentative_parse won't affect the current context. */
4554 static cp_token_position
4555 cp_parser_start_tentative_firewall (cp_parser *parser)
4557 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4558 return 0;
4560 cp_parser_parse_tentatively (parser);
4561 cp_parser_commit_to_topmost_tentative_parse (parser);
4562 return cp_lexer_token_position (parser->lexer, false);
4565 /* We've finished parsing the collection of statements. Wrap up the
4566 firewall and replace the relevant tokens with the parsed form. */
4568 static void
4569 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4570 tree expr)
4572 if (!start)
4573 return;
4575 /* Finish the firewall level. */
4576 cp_parser_parse_definitely (parser);
4577 /* And remember the result of the parse for when we try again. */
4578 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4579 token->type = CPP_PREPARSED_EXPR;
4580 token->u.value = expr;
4581 token->keyword = RID_MAX;
4582 cp_lexer_purge_tokens_after (parser->lexer, start);
4585 /* Like the above functions, but let the user modify the tokens. Used by
4586 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4587 later parses, so it makes sense to localize the effects of
4588 cp_parser_commit_to_tentative_parse. */
4590 struct tentative_firewall
4592 cp_parser *parser;
4593 bool set;
4595 tentative_firewall (cp_parser *p): parser(p)
4597 /* If we're currently parsing tentatively, start a committed level as a
4598 firewall and then an inner tentative parse. */
4599 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4601 cp_parser_parse_tentatively (parser);
4602 cp_parser_commit_to_topmost_tentative_parse (parser);
4603 cp_parser_parse_tentatively (parser);
4607 ~tentative_firewall()
4609 if (set)
4611 /* Finish the inner tentative parse and the firewall, propagating any
4612 uncommitted error state to the outer tentative parse. */
4613 bool err = cp_parser_error_occurred (parser);
4614 cp_parser_parse_definitely (parser);
4615 cp_parser_parse_definitely (parser);
4616 if (err)
4617 cp_parser_simulate_error (parser);
4622 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4623 This class is for tracking such a matching pair of symbols.
4624 In particular, it tracks the location of the first token,
4625 so that if the second token is missing, we can highlight the
4626 location of the first token when notifying the user about the
4627 problem. */
4629 template <typename traits_t>
4630 class token_pair
4632 public:
4633 /* token_pair's ctor. */
4634 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4636 /* If the next token is the opening symbol for this pair, consume it and
4637 return true.
4638 Otherwise, issue an error and return false.
4639 In either case, record the location of the opening token. */
4641 bool require_open (cp_parser *parser)
4643 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4644 return cp_parser_require (parser, traits_t::open_token_type,
4645 traits_t::required_token_open);
4648 /* Consume the next token from PARSER, recording its location as
4649 that of the opening token within the pair. */
4651 cp_token * consume_open (cp_parser *parser)
4653 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4654 gcc_assert (tok->type == traits_t::open_token_type);
4655 m_open_loc = tok->location;
4656 return tok;
4659 /* If the next token is the closing symbol for this pair, consume it
4660 and return it.
4661 Otherwise, issue an error, highlighting the location of the
4662 corresponding opening token, and return NULL. */
4664 cp_token *require_close (cp_parser *parser) const
4666 return cp_parser_require (parser, traits_t::close_token_type,
4667 traits_t::required_token_close,
4668 m_open_loc);
4671 private:
4672 location_t m_open_loc;
4675 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4677 struct matching_paren_traits
4679 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4680 static const enum required_token required_token_open = RT_OPEN_PAREN;
4681 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4682 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4685 /* "matching_parens" is a token_pair<T> class for tracking matching
4686 pairs of parentheses. */
4688 typedef token_pair<matching_paren_traits> matching_parens;
4690 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4692 struct matching_brace_traits
4694 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4695 static const enum required_token required_token_open = RT_OPEN_BRACE;
4696 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4697 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4700 /* "matching_braces" is a token_pair<T> class for tracking matching
4701 pairs of braces. */
4703 typedef token_pair<matching_brace_traits> matching_braces;
4706 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4707 enclosing parentheses. */
4709 static cp_expr
4710 cp_parser_statement_expr (cp_parser *parser)
4712 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4714 /* Consume the '('. */
4715 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4716 matching_parens parens;
4717 parens.consume_open (parser);
4718 /* Start the statement-expression. */
4719 tree expr = begin_stmt_expr ();
4720 /* Parse the compound-statement. */
4721 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4722 /* Finish up. */
4723 expr = finish_stmt_expr (expr, false);
4724 /* Consume the ')'. */
4725 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4726 if (!parens.require_close (parser))
4727 cp_parser_skip_to_end_of_statement (parser);
4729 cp_parser_end_tentative_firewall (parser, start, expr);
4730 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4731 return cp_expr (expr, combined_loc);
4734 /* Expressions [gram.expr] */
4736 /* Parse a fold-operator.
4738 fold-operator:
4739 - * / % ^ & | = < > << >>
4740 = -= *= /= %= ^= &= |= <<= >>=
4741 == != <= >= && || , .* ->*
4743 This returns the tree code corresponding to the matched operator
4744 as an int. When the current token matches a compound assignment
4745 opertor, the resulting tree code is the negative value of the
4746 non-assignment operator. */
4748 static int
4749 cp_parser_fold_operator (cp_token *token)
4751 switch (token->type)
4753 case CPP_PLUS: return PLUS_EXPR;
4754 case CPP_MINUS: return MINUS_EXPR;
4755 case CPP_MULT: return MULT_EXPR;
4756 case CPP_DIV: return TRUNC_DIV_EXPR;
4757 case CPP_MOD: return TRUNC_MOD_EXPR;
4758 case CPP_XOR: return BIT_XOR_EXPR;
4759 case CPP_AND: return BIT_AND_EXPR;
4760 case CPP_OR: return BIT_IOR_EXPR;
4761 case CPP_LSHIFT: return LSHIFT_EXPR;
4762 case CPP_RSHIFT: return RSHIFT_EXPR;
4764 case CPP_EQ: return -NOP_EXPR;
4765 case CPP_PLUS_EQ: return -PLUS_EXPR;
4766 case CPP_MINUS_EQ: return -MINUS_EXPR;
4767 case CPP_MULT_EQ: return -MULT_EXPR;
4768 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4769 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4770 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4771 case CPP_AND_EQ: return -BIT_AND_EXPR;
4772 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4773 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4774 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4776 case CPP_EQ_EQ: return EQ_EXPR;
4777 case CPP_NOT_EQ: return NE_EXPR;
4778 case CPP_LESS: return LT_EXPR;
4779 case CPP_GREATER: return GT_EXPR;
4780 case CPP_LESS_EQ: return LE_EXPR;
4781 case CPP_GREATER_EQ: return GE_EXPR;
4783 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4784 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4786 case CPP_COMMA: return COMPOUND_EXPR;
4788 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4789 case CPP_DEREF_STAR: return MEMBER_REF;
4791 default: return ERROR_MARK;
4795 /* Returns true if CODE indicates a binary expression, which is not allowed in
4796 the LHS of a fold-expression. More codes will need to be added to use this
4797 function in other contexts. */
4799 static bool
4800 is_binary_op (tree_code code)
4802 switch (code)
4804 case PLUS_EXPR:
4805 case POINTER_PLUS_EXPR:
4806 case MINUS_EXPR:
4807 case MULT_EXPR:
4808 case TRUNC_DIV_EXPR:
4809 case TRUNC_MOD_EXPR:
4810 case BIT_XOR_EXPR:
4811 case BIT_AND_EXPR:
4812 case BIT_IOR_EXPR:
4813 case LSHIFT_EXPR:
4814 case RSHIFT_EXPR:
4816 case MODOP_EXPR:
4818 case EQ_EXPR:
4819 case NE_EXPR:
4820 case LE_EXPR:
4821 case GE_EXPR:
4822 case LT_EXPR:
4823 case GT_EXPR:
4825 case TRUTH_ANDIF_EXPR:
4826 case TRUTH_ORIF_EXPR:
4828 case COMPOUND_EXPR:
4830 case DOTSTAR_EXPR:
4831 case MEMBER_REF:
4832 return true;
4834 default:
4835 return false;
4839 /* If the next token is a suitable fold operator, consume it and return as
4840 the function above. */
4842 static int
4843 cp_parser_fold_operator (cp_parser *parser)
4845 cp_token* token = cp_lexer_peek_token (parser->lexer);
4846 int code = cp_parser_fold_operator (token);
4847 if (code != ERROR_MARK)
4848 cp_lexer_consume_token (parser->lexer);
4849 return code;
4852 /* Parse a fold-expression.
4854 fold-expression:
4855 ( ... folding-operator cast-expression)
4856 ( cast-expression folding-operator ... )
4857 ( cast-expression folding operator ... folding-operator cast-expression)
4859 Note that the '(' and ')' are matched in primary expression. */
4861 static cp_expr
4862 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4864 cp_id_kind pidk;
4866 // Left fold.
4867 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4869 cp_lexer_consume_token (parser->lexer);
4870 int op = cp_parser_fold_operator (parser);
4871 if (op == ERROR_MARK)
4873 cp_parser_error (parser, "expected binary operator");
4874 return error_mark_node;
4877 tree expr = cp_parser_cast_expression (parser, false, false,
4878 false, &pidk);
4879 if (expr == error_mark_node)
4880 return error_mark_node;
4881 return finish_left_unary_fold_expr (expr, op);
4884 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4885 int op = cp_parser_fold_operator (parser);
4886 if (op == ERROR_MARK)
4888 cp_parser_error (parser, "expected binary operator");
4889 return error_mark_node;
4892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4894 cp_parser_error (parser, "expected ...");
4895 return error_mark_node;
4897 cp_lexer_consume_token (parser->lexer);
4899 /* The operands of a fold-expression are cast-expressions, so binary or
4900 conditional expressions are not allowed. We check this here to avoid
4901 tentative parsing. */
4902 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4903 /* OK, the expression was parenthesized. */;
4904 else if (is_binary_op (TREE_CODE (expr1)))
4905 error_at (location_of (expr1),
4906 "binary expression in operand of fold-expression");
4907 else if (TREE_CODE (expr1) == COND_EXPR)
4908 error_at (location_of (expr1),
4909 "conditional expression in operand of fold-expression");
4911 // Right fold.
4912 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4913 return finish_right_unary_fold_expr (expr1, op);
4915 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4917 cp_parser_error (parser, "mismatched operator in fold-expression");
4918 return error_mark_node;
4920 cp_lexer_consume_token (parser->lexer);
4922 // Binary left or right fold.
4923 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4924 if (expr2 == error_mark_node)
4925 return error_mark_node;
4926 return finish_binary_fold_expr (expr1, expr2, op);
4929 /* Parse a primary-expression.
4931 primary-expression:
4932 literal
4933 this
4934 ( expression )
4935 id-expression
4936 lambda-expression (C++11)
4938 GNU Extensions:
4940 primary-expression:
4941 ( compound-statement )
4942 __builtin_va_arg ( assignment-expression , type-id )
4943 __builtin_offsetof ( type-id , offsetof-expression )
4945 C++ Extensions:
4946 __has_nothrow_assign ( type-id )
4947 __has_nothrow_constructor ( type-id )
4948 __has_nothrow_copy ( type-id )
4949 __has_trivial_assign ( type-id )
4950 __has_trivial_constructor ( type-id )
4951 __has_trivial_copy ( type-id )
4952 __has_trivial_destructor ( type-id )
4953 __has_virtual_destructor ( type-id )
4954 __is_abstract ( type-id )
4955 __is_base_of ( type-id , type-id )
4956 __is_class ( type-id )
4957 __is_empty ( type-id )
4958 __is_enum ( type-id )
4959 __is_final ( type-id )
4960 __is_literal_type ( type-id )
4961 __is_pod ( type-id )
4962 __is_polymorphic ( type-id )
4963 __is_std_layout ( type-id )
4964 __is_trivial ( type-id )
4965 __is_union ( type-id )
4967 Objective-C++ Extension:
4969 primary-expression:
4970 objc-expression
4972 literal:
4973 __null
4975 ADDRESS_P is true iff this expression was immediately preceded by
4976 "&" and therefore might denote a pointer-to-member. CAST_P is true
4977 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4978 true iff this expression is a template argument.
4980 Returns a representation of the expression. Upon return, *IDK
4981 indicates what kind of id-expression (if any) was present. */
4983 static cp_expr
4984 cp_parser_primary_expression (cp_parser *parser,
4985 bool address_p,
4986 bool cast_p,
4987 bool template_arg_p,
4988 bool decltype_p,
4989 cp_id_kind *idk)
4991 cp_token *token = NULL;
4993 /* Assume the primary expression is not an id-expression. */
4994 *idk = CP_ID_KIND_NONE;
4996 /* Peek at the next token. */
4997 token = cp_lexer_peek_token (parser->lexer);
4998 switch ((int) token->type)
5000 /* literal:
5001 integer-literal
5002 character-literal
5003 floating-literal
5004 string-literal
5005 boolean-literal
5006 pointer-literal
5007 user-defined-literal */
5008 case CPP_CHAR:
5009 case CPP_CHAR16:
5010 case CPP_CHAR32:
5011 case CPP_WCHAR:
5012 case CPP_UTF8CHAR:
5013 case CPP_NUMBER:
5014 case CPP_PREPARSED_EXPR:
5015 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5016 return cp_parser_userdef_numeric_literal (parser);
5017 token = cp_lexer_consume_token (parser->lexer);
5018 if (TREE_CODE (token->u.value) == FIXED_CST)
5020 error_at (token->location,
5021 "fixed-point types not supported in C++");
5022 return error_mark_node;
5024 /* Floating-point literals are only allowed in an integral
5025 constant expression if they are cast to an integral or
5026 enumeration type. */
5027 if (TREE_CODE (token->u.value) == REAL_CST
5028 && parser->integral_constant_expression_p
5029 && pedantic)
5031 /* CAST_P will be set even in invalid code like "int(2.7 +
5032 ...)". Therefore, we have to check that the next token
5033 is sure to end the cast. */
5034 if (cast_p)
5036 cp_token *next_token;
5038 next_token = cp_lexer_peek_token (parser->lexer);
5039 if (/* The comma at the end of an
5040 enumerator-definition. */
5041 next_token->type != CPP_COMMA
5042 /* The curly brace at the end of an enum-specifier. */
5043 && next_token->type != CPP_CLOSE_BRACE
5044 /* The end of a statement. */
5045 && next_token->type != CPP_SEMICOLON
5046 /* The end of the cast-expression. */
5047 && next_token->type != CPP_CLOSE_PAREN
5048 /* The end of an array bound. */
5049 && next_token->type != CPP_CLOSE_SQUARE
5050 /* The closing ">" in a template-argument-list. */
5051 && (next_token->type != CPP_GREATER
5052 || parser->greater_than_is_operator_p)
5053 /* C++0x only: A ">>" treated like two ">" tokens,
5054 in a template-argument-list. */
5055 && (next_token->type != CPP_RSHIFT
5056 || (cxx_dialect == cxx98)
5057 || parser->greater_than_is_operator_p))
5058 cast_p = false;
5061 /* If we are within a cast, then the constraint that the
5062 cast is to an integral or enumeration type will be
5063 checked at that point. If we are not within a cast, then
5064 this code is invalid. */
5065 if (!cast_p)
5066 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5068 return cp_expr (token->u.value, token->location);
5070 case CPP_CHAR_USERDEF:
5071 case CPP_CHAR16_USERDEF:
5072 case CPP_CHAR32_USERDEF:
5073 case CPP_WCHAR_USERDEF:
5074 case CPP_UTF8CHAR_USERDEF:
5075 return cp_parser_userdef_char_literal (parser);
5077 case CPP_STRING:
5078 case CPP_STRING16:
5079 case CPP_STRING32:
5080 case CPP_WSTRING:
5081 case CPP_UTF8STRING:
5082 case CPP_STRING_USERDEF:
5083 case CPP_STRING16_USERDEF:
5084 case CPP_STRING32_USERDEF:
5085 case CPP_WSTRING_USERDEF:
5086 case CPP_UTF8STRING_USERDEF:
5087 /* ??? Should wide strings be allowed when parser->translate_strings_p
5088 is false (i.e. in attributes)? If not, we can kill the third
5089 argument to cp_parser_string_literal. */
5090 return cp_parser_string_literal (parser,
5091 parser->translate_strings_p,
5092 true);
5094 case CPP_OPEN_PAREN:
5095 /* If we see `( { ' then we are looking at the beginning of
5096 a GNU statement-expression. */
5097 if (cp_parser_allow_gnu_extensions_p (parser)
5098 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5100 /* Statement-expressions are not allowed by the standard. */
5101 pedwarn (token->location, OPT_Wpedantic,
5102 "ISO C++ forbids braced-groups within expressions");
5104 /* And they're not allowed outside of a function-body; you
5105 cannot, for example, write:
5107 int i = ({ int j = 3; j + 1; });
5109 at class or namespace scope. */
5110 if (!parser->in_function_body
5111 || parser->in_template_argument_list_p)
5113 error_at (token->location,
5114 "statement-expressions are not allowed outside "
5115 "functions nor in template-argument lists");
5116 cp_parser_skip_to_end_of_block_or_statement (parser);
5117 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5118 cp_lexer_consume_token (parser->lexer);
5119 return error_mark_node;
5121 else
5122 return cp_parser_statement_expr (parser);
5124 /* Otherwise it's a normal parenthesized expression. */
5126 cp_expr expr;
5127 bool saved_greater_than_is_operator_p;
5129 location_t open_paren_loc = token->location;
5131 /* Consume the `('. */
5132 matching_parens parens;
5133 parens.consume_open (parser);
5134 /* Within a parenthesized expression, a `>' token is always
5135 the greater-than operator. */
5136 saved_greater_than_is_operator_p
5137 = parser->greater_than_is_operator_p;
5138 parser->greater_than_is_operator_p = true;
5140 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5141 /* Left fold expression. */
5142 expr = NULL_TREE;
5143 else
5144 /* Parse the parenthesized expression. */
5145 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5147 token = cp_lexer_peek_token (parser->lexer);
5148 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5150 expr = cp_parser_fold_expression (parser, expr);
5151 if (expr != error_mark_node
5152 && cxx_dialect < cxx17
5153 && !in_system_header_at (input_location))
5154 pedwarn (input_location, 0, "fold-expressions only available "
5155 "with -std=c++17 or -std=gnu++17");
5157 else
5158 /* Let the front end know that this expression was
5159 enclosed in parentheses. This matters in case, for
5160 example, the expression is of the form `A::B', since
5161 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5162 not. */
5163 expr = finish_parenthesized_expr (expr);
5165 /* DR 705: Wrapping an unqualified name in parentheses
5166 suppresses arg-dependent lookup. We want to pass back
5167 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5168 (c++/37862), but none of the others. */
5169 if (*idk != CP_ID_KIND_QUALIFIED)
5170 *idk = CP_ID_KIND_NONE;
5172 /* The `>' token might be the end of a template-id or
5173 template-parameter-list now. */
5174 parser->greater_than_is_operator_p
5175 = saved_greater_than_is_operator_p;
5177 /* Consume the `)'. */
5178 token = cp_lexer_peek_token (parser->lexer);
5179 location_t close_paren_loc = token->location;
5180 expr.set_range (open_paren_loc, close_paren_loc);
5181 if (!parens.require_close (parser)
5182 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5183 cp_parser_skip_to_end_of_statement (parser);
5185 return expr;
5188 case CPP_OPEN_SQUARE:
5190 if (c_dialect_objc ())
5192 /* We might have an Objective-C++ message. */
5193 cp_parser_parse_tentatively (parser);
5194 tree msg = cp_parser_objc_message_expression (parser);
5195 /* If that works out, we're done ... */
5196 if (cp_parser_parse_definitely (parser))
5197 return msg;
5198 /* ... else, fall though to see if it's a lambda. */
5200 cp_expr lam = cp_parser_lambda_expression (parser);
5201 /* Don't warn about a failed tentative parse. */
5202 if (cp_parser_error_occurred (parser))
5203 return error_mark_node;
5204 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5205 return lam;
5208 case CPP_OBJC_STRING:
5209 if (c_dialect_objc ())
5210 /* We have an Objective-C++ string literal. */
5211 return cp_parser_objc_expression (parser);
5212 cp_parser_error (parser, "expected primary-expression");
5213 return error_mark_node;
5215 case CPP_KEYWORD:
5216 switch (token->keyword)
5218 /* These two are the boolean literals. */
5219 case RID_TRUE:
5220 cp_lexer_consume_token (parser->lexer);
5221 return cp_expr (boolean_true_node, token->location);
5222 case RID_FALSE:
5223 cp_lexer_consume_token (parser->lexer);
5224 return cp_expr (boolean_false_node, token->location);
5226 /* The `__null' literal. */
5227 case RID_NULL:
5228 cp_lexer_consume_token (parser->lexer);
5229 return cp_expr (null_node, token->location);
5231 /* The `nullptr' literal. */
5232 case RID_NULLPTR:
5233 cp_lexer_consume_token (parser->lexer);
5234 return cp_expr (nullptr_node, token->location);
5236 /* Recognize the `this' keyword. */
5237 case RID_THIS:
5238 cp_lexer_consume_token (parser->lexer);
5239 if (parser->local_variables_forbidden_p)
5241 error_at (token->location,
5242 "%<this%> may not be used in this context");
5243 return error_mark_node;
5245 /* Pointers cannot appear in constant-expressions. */
5246 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5247 return error_mark_node;
5248 return cp_expr (finish_this_expr (), token->location);
5250 /* The `operator' keyword can be the beginning of an
5251 id-expression. */
5252 case RID_OPERATOR:
5253 goto id_expression;
5255 case RID_FUNCTION_NAME:
5256 case RID_PRETTY_FUNCTION_NAME:
5257 case RID_C99_FUNCTION_NAME:
5259 non_integral_constant name;
5261 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5262 __func__ are the names of variables -- but they are
5263 treated specially. Therefore, they are handled here,
5264 rather than relying on the generic id-expression logic
5265 below. Grammatically, these names are id-expressions.
5267 Consume the token. */
5268 token = cp_lexer_consume_token (parser->lexer);
5270 switch (token->keyword)
5272 case RID_FUNCTION_NAME:
5273 name = NIC_FUNC_NAME;
5274 break;
5275 case RID_PRETTY_FUNCTION_NAME:
5276 name = NIC_PRETTY_FUNC;
5277 break;
5278 case RID_C99_FUNCTION_NAME:
5279 name = NIC_C99_FUNC;
5280 break;
5281 default:
5282 gcc_unreachable ();
5285 if (cp_parser_non_integral_constant_expression (parser, name))
5286 return error_mark_node;
5288 /* Look up the name. */
5289 return finish_fname (token->u.value);
5292 case RID_VA_ARG:
5294 tree expression;
5295 tree type;
5296 source_location type_location;
5297 location_t start_loc
5298 = cp_lexer_peek_token (parser->lexer)->location;
5299 /* The `__builtin_va_arg' construct is used to handle
5300 `va_arg'. Consume the `__builtin_va_arg' token. */
5301 cp_lexer_consume_token (parser->lexer);
5302 /* Look for the opening `('. */
5303 matching_parens parens;
5304 parens.require_open (parser);
5305 /* Now, parse the assignment-expression. */
5306 expression = cp_parser_assignment_expression (parser);
5307 /* Look for the `,'. */
5308 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5309 type_location = cp_lexer_peek_token (parser->lexer)->location;
5310 /* Parse the type-id. */
5312 type_id_in_expr_sentinel s (parser);
5313 type = cp_parser_type_id (parser);
5315 /* Look for the closing `)'. */
5316 location_t finish_loc
5317 = cp_lexer_peek_token (parser->lexer)->location;
5318 parens.require_close (parser);
5319 /* Using `va_arg' in a constant-expression is not
5320 allowed. */
5321 if (cp_parser_non_integral_constant_expression (parser,
5322 NIC_VA_ARG))
5323 return error_mark_node;
5324 /* Construct a location of the form:
5325 __builtin_va_arg (v, int)
5326 ~~~~~~~~~~~~~~~~~~~~~^~~~
5327 with the caret at the type, ranging from the start of the
5328 "__builtin_va_arg" token to the close paren. */
5329 location_t combined_loc
5330 = make_location (type_location, start_loc, finish_loc);
5331 return build_x_va_arg (combined_loc, expression, type);
5334 case RID_OFFSETOF:
5335 return cp_parser_builtin_offsetof (parser);
5337 case RID_HAS_NOTHROW_ASSIGN:
5338 case RID_HAS_NOTHROW_CONSTRUCTOR:
5339 case RID_HAS_NOTHROW_COPY:
5340 case RID_HAS_TRIVIAL_ASSIGN:
5341 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5342 case RID_HAS_TRIVIAL_COPY:
5343 case RID_HAS_TRIVIAL_DESTRUCTOR:
5344 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5345 case RID_HAS_VIRTUAL_DESTRUCTOR:
5346 case RID_IS_ABSTRACT:
5347 case RID_IS_AGGREGATE:
5348 case RID_IS_BASE_OF:
5349 case RID_IS_CLASS:
5350 case RID_IS_EMPTY:
5351 case RID_IS_ENUM:
5352 case RID_IS_FINAL:
5353 case RID_IS_LITERAL_TYPE:
5354 case RID_IS_POD:
5355 case RID_IS_POLYMORPHIC:
5356 case RID_IS_SAME_AS:
5357 case RID_IS_STD_LAYOUT:
5358 case RID_IS_TRIVIAL:
5359 case RID_IS_TRIVIALLY_ASSIGNABLE:
5360 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5361 case RID_IS_TRIVIALLY_COPYABLE:
5362 case RID_IS_UNION:
5363 case RID_IS_ASSIGNABLE:
5364 case RID_IS_CONSTRUCTIBLE:
5365 return cp_parser_trait_expr (parser, token->keyword);
5367 // C++ concepts
5368 case RID_REQUIRES:
5369 return cp_parser_requires_expression (parser);
5371 /* Objective-C++ expressions. */
5372 case RID_AT_ENCODE:
5373 case RID_AT_PROTOCOL:
5374 case RID_AT_SELECTOR:
5375 return cp_parser_objc_expression (parser);
5377 case RID_TEMPLATE:
5378 if (parser->in_function_body
5379 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5380 == CPP_LESS))
5382 error_at (token->location,
5383 "a template declaration cannot appear at block scope");
5384 cp_parser_skip_to_end_of_block_or_statement (parser);
5385 return error_mark_node;
5387 /* FALLTHRU */
5388 default:
5389 cp_parser_error (parser, "expected primary-expression");
5390 return error_mark_node;
5393 /* An id-expression can start with either an identifier, a
5394 `::' as the beginning of a qualified-id, or the "operator"
5395 keyword. */
5396 case CPP_NAME:
5397 case CPP_SCOPE:
5398 case CPP_TEMPLATE_ID:
5399 case CPP_NESTED_NAME_SPECIFIER:
5401 id_expression:
5402 cp_expr id_expression;
5403 cp_expr decl;
5404 const char *error_msg;
5405 bool template_p;
5406 bool done;
5407 cp_token *id_expr_token;
5409 /* Parse the id-expression. */
5410 id_expression
5411 = cp_parser_id_expression (parser,
5412 /*template_keyword_p=*/false,
5413 /*check_dependency_p=*/true,
5414 &template_p,
5415 /*declarator_p=*/false,
5416 /*optional_p=*/false);
5417 if (id_expression == error_mark_node)
5418 return error_mark_node;
5419 id_expr_token = token;
5420 token = cp_lexer_peek_token (parser->lexer);
5421 done = (token->type != CPP_OPEN_SQUARE
5422 && token->type != CPP_OPEN_PAREN
5423 && token->type != CPP_DOT
5424 && token->type != CPP_DEREF
5425 && token->type != CPP_PLUS_PLUS
5426 && token->type != CPP_MINUS_MINUS);
5427 /* If we have a template-id, then no further lookup is
5428 required. If the template-id was for a template-class, we
5429 will sometimes have a TYPE_DECL at this point. */
5430 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5431 || TREE_CODE (id_expression) == TYPE_DECL)
5432 decl = id_expression;
5433 /* Look up the name. */
5434 else
5436 tree ambiguous_decls;
5438 /* If we already know that this lookup is ambiguous, then
5439 we've already issued an error message; there's no reason
5440 to check again. */
5441 if (id_expr_token->type == CPP_NAME
5442 && id_expr_token->error_reported)
5444 cp_parser_simulate_error (parser);
5445 return error_mark_node;
5448 decl = cp_parser_lookup_name (parser, id_expression,
5449 none_type,
5450 template_p,
5451 /*is_namespace=*/false,
5452 /*check_dependency=*/true,
5453 &ambiguous_decls,
5454 id_expr_token->location);
5455 /* If the lookup was ambiguous, an error will already have
5456 been issued. */
5457 if (ambiguous_decls)
5458 return error_mark_node;
5460 /* In Objective-C++, we may have an Objective-C 2.0
5461 dot-syntax for classes here. */
5462 if (c_dialect_objc ()
5463 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5464 && TREE_CODE (decl) == TYPE_DECL
5465 && objc_is_class_name (decl))
5467 tree component;
5468 cp_lexer_consume_token (parser->lexer);
5469 component = cp_parser_identifier (parser);
5470 if (component == error_mark_node)
5471 return error_mark_node;
5473 tree result = objc_build_class_component_ref (id_expression,
5474 component);
5475 /* Build a location of the form:
5476 expr.component
5477 ~~~~~^~~~~~~~~
5478 with caret at the start of the component name (at
5479 input_location), ranging from the start of the id_expression
5480 to the end of the component name. */
5481 location_t combined_loc
5482 = make_location (input_location, id_expression.get_start (),
5483 get_finish (input_location));
5484 protected_set_expr_location (result, combined_loc);
5485 return result;
5488 /* In Objective-C++, an instance variable (ivar) may be preferred
5489 to whatever cp_parser_lookup_name() found.
5490 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5491 rest of c-family, we have to do a little extra work to preserve
5492 any location information in cp_expr "decl". Given that
5493 objc_lookup_ivar is implemented in "c-family" and "objc", we
5494 have a trip through the pure "tree" type, rather than cp_expr.
5495 Naively copying it back to "decl" would implicitly give the
5496 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5497 store an EXPR_LOCATION. Hence we only update "decl" (and
5498 hence its location_t) if we get back a different tree node. */
5499 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5500 id_expression);
5501 if (decl_tree != decl.get_value ())
5502 decl = cp_expr (decl_tree);
5504 /* If name lookup gives us a SCOPE_REF, then the
5505 qualifying scope was dependent. */
5506 if (TREE_CODE (decl) == SCOPE_REF)
5508 /* At this point, we do not know if DECL is a valid
5509 integral constant expression. We assume that it is
5510 in fact such an expression, so that code like:
5512 template <int N> struct A {
5513 int a[B<N>::i];
5516 is accepted. At template-instantiation time, we
5517 will check that B<N>::i is actually a constant. */
5518 return decl;
5520 /* Check to see if DECL is a local variable in a context
5521 where that is forbidden. */
5522 if (parser->local_variables_forbidden_p
5523 && local_variable_p (decl))
5525 /* It might be that we only found DECL because we are
5526 trying to be generous with pre-ISO scoping rules.
5527 For example, consider:
5529 int i;
5530 void g() {
5531 for (int i = 0; i < 10; ++i) {}
5532 extern void f(int j = i);
5535 Here, name look up will originally find the out
5536 of scope `i'. We need to issue a warning message,
5537 but then use the global `i'. */
5538 decl = check_for_out_of_scope_variable (decl);
5539 if (local_variable_p (decl))
5541 error_at (id_expr_token->location,
5542 "local variable %qD may not appear in this context",
5543 decl.get_value ());
5544 return error_mark_node;
5549 decl = (finish_id_expression
5550 (id_expression, decl, parser->scope,
5551 idk,
5552 parser->integral_constant_expression_p,
5553 parser->allow_non_integral_constant_expression_p,
5554 &parser->non_integral_constant_expression_p,
5555 template_p, done, address_p,
5556 template_arg_p,
5557 &error_msg,
5558 id_expression.get_location ()));
5559 if (error_msg)
5560 cp_parser_error (parser, error_msg);
5561 decl.set_location (id_expr_token->location);
5562 return decl;
5565 /* Anything else is an error. */
5566 default:
5567 cp_parser_error (parser, "expected primary-expression");
5568 return error_mark_node;
5572 static inline cp_expr
5573 cp_parser_primary_expression (cp_parser *parser,
5574 bool address_p,
5575 bool cast_p,
5576 bool template_arg_p,
5577 cp_id_kind *idk)
5579 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5580 /*decltype*/false, idk);
5583 /* Parse an id-expression.
5585 id-expression:
5586 unqualified-id
5587 qualified-id
5589 qualified-id:
5590 :: [opt] nested-name-specifier template [opt] unqualified-id
5591 :: identifier
5592 :: operator-function-id
5593 :: template-id
5595 Return a representation of the unqualified portion of the
5596 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5597 a `::' or nested-name-specifier.
5599 Often, if the id-expression was a qualified-id, the caller will
5600 want to make a SCOPE_REF to represent the qualified-id. This
5601 function does not do this in order to avoid wastefully creating
5602 SCOPE_REFs when they are not required.
5604 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5605 `template' keyword.
5607 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5608 uninstantiated templates.
5610 If *TEMPLATE_P is non-NULL, it is set to true iff the
5611 `template' keyword is used to explicitly indicate that the entity
5612 named is a template.
5614 If DECLARATOR_P is true, the id-expression is appearing as part of
5615 a declarator, rather than as part of an expression. */
5617 static cp_expr
5618 cp_parser_id_expression (cp_parser *parser,
5619 bool template_keyword_p,
5620 bool check_dependency_p,
5621 bool *template_p,
5622 bool declarator_p,
5623 bool optional_p)
5625 bool global_scope_p;
5626 bool nested_name_specifier_p;
5628 /* Assume the `template' keyword was not used. */
5629 if (template_p)
5630 *template_p = template_keyword_p;
5632 /* Look for the optional `::' operator. */
5633 global_scope_p
5634 = (!template_keyword_p
5635 && (cp_parser_global_scope_opt (parser,
5636 /*current_scope_valid_p=*/false)
5637 != NULL_TREE));
5639 /* Look for the optional nested-name-specifier. */
5640 nested_name_specifier_p
5641 = (cp_parser_nested_name_specifier_opt (parser,
5642 /*typename_keyword_p=*/false,
5643 check_dependency_p,
5644 /*type_p=*/false,
5645 declarator_p,
5646 template_keyword_p)
5647 != NULL_TREE);
5649 /* If there is a nested-name-specifier, then we are looking at
5650 the first qualified-id production. */
5651 if (nested_name_specifier_p)
5653 tree saved_scope;
5654 tree saved_object_scope;
5655 tree saved_qualifying_scope;
5656 cp_expr unqualified_id;
5657 bool is_template;
5659 /* See if the next token is the `template' keyword. */
5660 if (!template_p)
5661 template_p = &is_template;
5662 *template_p = cp_parser_optional_template_keyword (parser);
5663 /* Name lookup we do during the processing of the
5664 unqualified-id might obliterate SCOPE. */
5665 saved_scope = parser->scope;
5666 saved_object_scope = parser->object_scope;
5667 saved_qualifying_scope = parser->qualifying_scope;
5668 /* Process the final unqualified-id. */
5669 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5670 check_dependency_p,
5671 declarator_p,
5672 /*optional_p=*/false);
5673 /* Restore the SAVED_SCOPE for our caller. */
5674 parser->scope = saved_scope;
5675 parser->object_scope = saved_object_scope;
5676 parser->qualifying_scope = saved_qualifying_scope;
5678 return unqualified_id;
5680 /* Otherwise, if we are in global scope, then we are looking at one
5681 of the other qualified-id productions. */
5682 else if (global_scope_p)
5684 cp_token *token;
5685 tree id;
5687 /* Peek at the next token. */
5688 token = cp_lexer_peek_token (parser->lexer);
5690 /* If it's an identifier, and the next token is not a "<", then
5691 we can avoid the template-id case. This is an optimization
5692 for this common case. */
5693 if (token->type == CPP_NAME
5694 && !cp_parser_nth_token_starts_template_argument_list_p
5695 (parser, 2))
5696 return cp_parser_identifier (parser);
5698 cp_parser_parse_tentatively (parser);
5699 /* Try a template-id. */
5700 id = cp_parser_template_id (parser,
5701 /*template_keyword_p=*/false,
5702 /*check_dependency_p=*/true,
5703 none_type,
5704 declarator_p);
5705 /* If that worked, we're done. */
5706 if (cp_parser_parse_definitely (parser))
5707 return id;
5709 /* Peek at the next token. (Changes in the token buffer may
5710 have invalidated the pointer obtained above.) */
5711 token = cp_lexer_peek_token (parser->lexer);
5713 switch (token->type)
5715 case CPP_NAME:
5716 return cp_parser_identifier (parser);
5718 case CPP_KEYWORD:
5719 if (token->keyword == RID_OPERATOR)
5720 return cp_parser_operator_function_id (parser);
5721 /* Fall through. */
5723 default:
5724 cp_parser_error (parser, "expected id-expression");
5725 return error_mark_node;
5728 else
5729 return cp_parser_unqualified_id (parser, template_keyword_p,
5730 /*check_dependency_p=*/true,
5731 declarator_p,
5732 optional_p);
5735 /* Parse an unqualified-id.
5737 unqualified-id:
5738 identifier
5739 operator-function-id
5740 conversion-function-id
5741 ~ class-name
5742 template-id
5744 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5745 keyword, in a construct like `A::template ...'.
5747 Returns a representation of unqualified-id. For the `identifier'
5748 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5749 production a BIT_NOT_EXPR is returned; the operand of the
5750 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5751 other productions, see the documentation accompanying the
5752 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5753 names are looked up in uninstantiated templates. If DECLARATOR_P
5754 is true, the unqualified-id is appearing as part of a declarator,
5755 rather than as part of an expression. */
5757 static cp_expr
5758 cp_parser_unqualified_id (cp_parser* parser,
5759 bool template_keyword_p,
5760 bool check_dependency_p,
5761 bool declarator_p,
5762 bool optional_p)
5764 cp_token *token;
5766 /* Peek at the next token. */
5767 token = cp_lexer_peek_token (parser->lexer);
5769 switch ((int) token->type)
5771 case CPP_NAME:
5773 tree id;
5775 /* We don't know yet whether or not this will be a
5776 template-id. */
5777 cp_parser_parse_tentatively (parser);
5778 /* Try a template-id. */
5779 id = cp_parser_template_id (parser, template_keyword_p,
5780 check_dependency_p,
5781 none_type,
5782 declarator_p);
5783 /* If it worked, we're done. */
5784 if (cp_parser_parse_definitely (parser))
5785 return id;
5786 /* Otherwise, it's an ordinary identifier. */
5787 return cp_parser_identifier (parser);
5790 case CPP_TEMPLATE_ID:
5791 return cp_parser_template_id (parser, template_keyword_p,
5792 check_dependency_p,
5793 none_type,
5794 declarator_p);
5796 case CPP_COMPL:
5798 tree type_decl;
5799 tree qualifying_scope;
5800 tree object_scope;
5801 tree scope;
5802 bool done;
5804 /* Consume the `~' token. */
5805 cp_lexer_consume_token (parser->lexer);
5806 /* Parse the class-name. The standard, as written, seems to
5807 say that:
5809 template <typename T> struct S { ~S (); };
5810 template <typename T> S<T>::~S() {}
5812 is invalid, since `~' must be followed by a class-name, but
5813 `S<T>' is dependent, and so not known to be a class.
5814 That's not right; we need to look in uninstantiated
5815 templates. A further complication arises from:
5817 template <typename T> void f(T t) {
5818 t.T::~T();
5821 Here, it is not possible to look up `T' in the scope of `T'
5822 itself. We must look in both the current scope, and the
5823 scope of the containing complete expression.
5825 Yet another issue is:
5827 struct S {
5828 int S;
5829 ~S();
5832 S::~S() {}
5834 The standard does not seem to say that the `S' in `~S'
5835 should refer to the type `S' and not the data member
5836 `S::S'. */
5838 /* DR 244 says that we look up the name after the "~" in the
5839 same scope as we looked up the qualifying name. That idea
5840 isn't fully worked out; it's more complicated than that. */
5841 scope = parser->scope;
5842 object_scope = parser->object_scope;
5843 qualifying_scope = parser->qualifying_scope;
5845 /* Check for invalid scopes. */
5846 if (scope == error_mark_node)
5848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5849 cp_lexer_consume_token (parser->lexer);
5850 return error_mark_node;
5852 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5854 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5855 error_at (token->location,
5856 "scope %qT before %<~%> is not a class-name",
5857 scope);
5858 cp_parser_simulate_error (parser);
5859 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5860 cp_lexer_consume_token (parser->lexer);
5861 return error_mark_node;
5863 gcc_assert (!scope || TYPE_P (scope));
5865 /* If the name is of the form "X::~X" it's OK even if X is a
5866 typedef. */
5867 token = cp_lexer_peek_token (parser->lexer);
5868 if (scope
5869 && token->type == CPP_NAME
5870 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5871 != CPP_LESS)
5872 && (token->u.value == TYPE_IDENTIFIER (scope)
5873 || (CLASS_TYPE_P (scope)
5874 && constructor_name_p (token->u.value, scope))))
5876 cp_lexer_consume_token (parser->lexer);
5877 return build_nt (BIT_NOT_EXPR, scope);
5880 /* ~auto means the destructor of whatever the object is. */
5881 if (cp_parser_is_keyword (token, RID_AUTO))
5883 if (cxx_dialect < cxx14)
5884 pedwarn (input_location, 0,
5885 "%<~auto%> only available with "
5886 "-std=c++14 or -std=gnu++14");
5887 cp_lexer_consume_token (parser->lexer);
5888 return build_nt (BIT_NOT_EXPR, make_auto ());
5891 /* If there was an explicit qualification (S::~T), first look
5892 in the scope given by the qualification (i.e., S).
5894 Note: in the calls to cp_parser_class_name below we pass
5895 typename_type so that lookup finds the injected-class-name
5896 rather than the constructor. */
5897 done = false;
5898 type_decl = NULL_TREE;
5899 if (scope)
5901 cp_parser_parse_tentatively (parser);
5902 type_decl = cp_parser_class_name (parser,
5903 /*typename_keyword_p=*/false,
5904 /*template_keyword_p=*/false,
5905 typename_type,
5906 /*check_dependency=*/false,
5907 /*class_head_p=*/false,
5908 declarator_p);
5909 if (cp_parser_parse_definitely (parser))
5910 done = true;
5912 /* In "N::S::~S", look in "N" as well. */
5913 if (!done && scope && qualifying_scope)
5915 cp_parser_parse_tentatively (parser);
5916 parser->scope = qualifying_scope;
5917 parser->object_scope = NULL_TREE;
5918 parser->qualifying_scope = NULL_TREE;
5919 type_decl
5920 = cp_parser_class_name (parser,
5921 /*typename_keyword_p=*/false,
5922 /*template_keyword_p=*/false,
5923 typename_type,
5924 /*check_dependency=*/false,
5925 /*class_head_p=*/false,
5926 declarator_p);
5927 if (cp_parser_parse_definitely (parser))
5928 done = true;
5930 /* In "p->S::~T", look in the scope given by "*p" as well. */
5931 else if (!done && object_scope)
5933 cp_parser_parse_tentatively (parser);
5934 parser->scope = object_scope;
5935 parser->object_scope = NULL_TREE;
5936 parser->qualifying_scope = NULL_TREE;
5937 type_decl
5938 = cp_parser_class_name (parser,
5939 /*typename_keyword_p=*/false,
5940 /*template_keyword_p=*/false,
5941 typename_type,
5942 /*check_dependency=*/false,
5943 /*class_head_p=*/false,
5944 declarator_p);
5945 if (cp_parser_parse_definitely (parser))
5946 done = true;
5948 /* Look in the surrounding context. */
5949 if (!done)
5951 parser->scope = NULL_TREE;
5952 parser->object_scope = NULL_TREE;
5953 parser->qualifying_scope = NULL_TREE;
5954 if (processing_template_decl)
5955 cp_parser_parse_tentatively (parser);
5956 type_decl
5957 = cp_parser_class_name (parser,
5958 /*typename_keyword_p=*/false,
5959 /*template_keyword_p=*/false,
5960 typename_type,
5961 /*check_dependency=*/false,
5962 /*class_head_p=*/false,
5963 declarator_p);
5964 if (processing_template_decl
5965 && ! cp_parser_parse_definitely (parser))
5967 /* We couldn't find a type with this name. If we're parsing
5968 tentatively, fail and try something else. */
5969 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5971 cp_parser_simulate_error (parser);
5972 return error_mark_node;
5974 /* Otherwise, accept it and check for a match at instantiation
5975 time. */
5976 type_decl = cp_parser_identifier (parser);
5977 if (type_decl != error_mark_node)
5978 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5979 return type_decl;
5982 /* If an error occurred, assume that the name of the
5983 destructor is the same as the name of the qualifying
5984 class. That allows us to keep parsing after running
5985 into ill-formed destructor names. */
5986 if (type_decl == error_mark_node && scope)
5987 return build_nt (BIT_NOT_EXPR, scope);
5988 else if (type_decl == error_mark_node)
5989 return error_mark_node;
5991 /* Check that destructor name and scope match. */
5992 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5994 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5995 error_at (token->location,
5996 "declaration of %<~%T%> as member of %qT",
5997 type_decl, scope);
5998 cp_parser_simulate_error (parser);
5999 return error_mark_node;
6002 /* [class.dtor]
6004 A typedef-name that names a class shall not be used as the
6005 identifier in the declarator for a destructor declaration. */
6006 if (declarator_p
6007 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6008 && !DECL_SELF_REFERENCE_P (type_decl)
6009 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6010 error_at (token->location,
6011 "typedef-name %qD used as destructor declarator",
6012 type_decl);
6014 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
6017 case CPP_KEYWORD:
6018 if (token->keyword == RID_OPERATOR)
6020 cp_expr id;
6022 /* This could be a template-id, so we try that first. */
6023 cp_parser_parse_tentatively (parser);
6024 /* Try a template-id. */
6025 id = cp_parser_template_id (parser, template_keyword_p,
6026 /*check_dependency_p=*/true,
6027 none_type,
6028 declarator_p);
6029 /* If that worked, we're done. */
6030 if (cp_parser_parse_definitely (parser))
6031 return id;
6032 /* We still don't know whether we're looking at an
6033 operator-function-id or a conversion-function-id. */
6034 cp_parser_parse_tentatively (parser);
6035 /* Try an operator-function-id. */
6036 id = cp_parser_operator_function_id (parser);
6037 /* If that didn't work, try a conversion-function-id. */
6038 if (!cp_parser_parse_definitely (parser))
6039 id = cp_parser_conversion_function_id (parser);
6040 else if (UDLIT_OPER_P (id))
6042 /* 17.6.3.3.5 */
6043 const char *name = UDLIT_OP_SUFFIX (id);
6044 if (name[0] != '_' && !in_system_header_at (input_location)
6045 && declarator_p)
6046 warning (OPT_Wliteral_suffix,
6047 "literal operator suffixes not preceded by %<_%>"
6048 " are reserved for future standardization");
6051 return id;
6053 /* Fall through. */
6055 default:
6056 if (optional_p)
6057 return NULL_TREE;
6058 cp_parser_error (parser, "expected unqualified-id");
6059 return error_mark_node;
6063 /* Parse an (optional) nested-name-specifier.
6065 nested-name-specifier: [C++98]
6066 class-or-namespace-name :: nested-name-specifier [opt]
6067 class-or-namespace-name :: template nested-name-specifier [opt]
6069 nested-name-specifier: [C++0x]
6070 type-name ::
6071 namespace-name ::
6072 nested-name-specifier identifier ::
6073 nested-name-specifier template [opt] simple-template-id ::
6075 PARSER->SCOPE should be set appropriately before this function is
6076 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6077 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6078 in name lookups.
6080 Sets PARSER->SCOPE to the class (TYPE) or namespace
6081 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6082 it unchanged if there is no nested-name-specifier. Returns the new
6083 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6085 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6086 part of a declaration and/or decl-specifier. */
6088 static tree
6089 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6090 bool typename_keyword_p,
6091 bool check_dependency_p,
6092 bool type_p,
6093 bool is_declaration,
6094 bool template_keyword_p /* = false */)
6096 bool success = false;
6097 cp_token_position start = 0;
6098 cp_token *token;
6100 /* Remember where the nested-name-specifier starts. */
6101 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6103 start = cp_lexer_token_position (parser->lexer, false);
6104 push_deferring_access_checks (dk_deferred);
6107 while (true)
6109 tree new_scope;
6110 tree old_scope;
6111 tree saved_qualifying_scope;
6113 /* Spot cases that cannot be the beginning of a
6114 nested-name-specifier. */
6115 token = cp_lexer_peek_token (parser->lexer);
6117 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6118 the already parsed nested-name-specifier. */
6119 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6121 /* Grab the nested-name-specifier and continue the loop. */
6122 cp_parser_pre_parsed_nested_name_specifier (parser);
6123 /* If we originally encountered this nested-name-specifier
6124 with IS_DECLARATION set to false, we will not have
6125 resolved TYPENAME_TYPEs, so we must do so here. */
6126 if (is_declaration
6127 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6129 new_scope = resolve_typename_type (parser->scope,
6130 /*only_current_p=*/false);
6131 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6132 parser->scope = new_scope;
6134 success = true;
6135 continue;
6138 /* Spot cases that cannot be the beginning of a
6139 nested-name-specifier. On the second and subsequent times
6140 through the loop, we look for the `template' keyword. */
6141 if (success && token->keyword == RID_TEMPLATE)
6143 /* A template-id can start a nested-name-specifier. */
6144 else if (token->type == CPP_TEMPLATE_ID)
6146 /* DR 743: decltype can be used in a nested-name-specifier. */
6147 else if (token_is_decltype (token))
6149 else
6151 /* If the next token is not an identifier, then it is
6152 definitely not a type-name or namespace-name. */
6153 if (token->type != CPP_NAME)
6154 break;
6155 /* If the following token is neither a `<' (to begin a
6156 template-id), nor a `::', then we are not looking at a
6157 nested-name-specifier. */
6158 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6160 if (token->type == CPP_COLON
6161 && parser->colon_corrects_to_scope_p
6162 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6164 gcc_rich_location richloc (token->location);
6165 richloc.add_fixit_replace ("::");
6166 error_at_rich_loc (&richloc,
6167 "found %<:%> in nested-name-specifier, "
6168 "expected %<::%>");
6169 token->type = CPP_SCOPE;
6172 if (token->type != CPP_SCOPE
6173 && !cp_parser_nth_token_starts_template_argument_list_p
6174 (parser, 2))
6175 break;
6178 /* The nested-name-specifier is optional, so we parse
6179 tentatively. */
6180 cp_parser_parse_tentatively (parser);
6182 /* Look for the optional `template' keyword, if this isn't the
6183 first time through the loop. */
6184 if (success)
6185 template_keyword_p = cp_parser_optional_template_keyword (parser);
6187 /* Save the old scope since the name lookup we are about to do
6188 might destroy it. */
6189 old_scope = parser->scope;
6190 saved_qualifying_scope = parser->qualifying_scope;
6191 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6192 look up names in "X<T>::I" in order to determine that "Y" is
6193 a template. So, if we have a typename at this point, we make
6194 an effort to look through it. */
6195 if (is_declaration
6196 && !typename_keyword_p
6197 && parser->scope
6198 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6199 parser->scope = resolve_typename_type (parser->scope,
6200 /*only_current_p=*/false);
6201 /* Parse the qualifying entity. */
6202 new_scope
6203 = cp_parser_qualifying_entity (parser,
6204 typename_keyword_p,
6205 template_keyword_p,
6206 check_dependency_p,
6207 type_p,
6208 is_declaration);
6209 /* Look for the `::' token. */
6210 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6212 /* If we found what we wanted, we keep going; otherwise, we're
6213 done. */
6214 if (!cp_parser_parse_definitely (parser))
6216 bool error_p = false;
6218 /* Restore the OLD_SCOPE since it was valid before the
6219 failed attempt at finding the last
6220 class-or-namespace-name. */
6221 parser->scope = old_scope;
6222 parser->qualifying_scope = saved_qualifying_scope;
6224 /* If the next token is a decltype, and the one after that is a
6225 `::', then the decltype has failed to resolve to a class or
6226 enumeration type. Give this error even when parsing
6227 tentatively since it can't possibly be valid--and we're going
6228 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6229 won't get another chance.*/
6230 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6231 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6232 == CPP_SCOPE))
6234 token = cp_lexer_consume_token (parser->lexer);
6235 error_at (token->location, "decltype evaluates to %qT, "
6236 "which is not a class or enumeration type",
6237 token->u.tree_check_value->value);
6238 parser->scope = error_mark_node;
6239 error_p = true;
6240 /* As below. */
6241 success = true;
6242 cp_lexer_consume_token (parser->lexer);
6245 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6246 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6248 /* If we have a non-type template-id followed by ::, it can't
6249 possibly be valid. */
6250 token = cp_lexer_peek_token (parser->lexer);
6251 tree tid = token->u.tree_check_value->value;
6252 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6253 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6255 tree tmpl = NULL_TREE;
6256 if (is_overloaded_fn (tid))
6258 tree fns = get_fns (tid);
6259 if (OVL_SINGLE_P (fns))
6260 tmpl = OVL_FIRST (fns);
6261 error_at (token->location, "function template-id %qD "
6262 "in nested-name-specifier", tid);
6264 else
6266 /* Variable template. */
6267 tmpl = TREE_OPERAND (tid, 0);
6268 gcc_assert (variable_template_p (tmpl));
6269 error_at (token->location, "variable template-id %qD "
6270 "in nested-name-specifier", tid);
6272 if (tmpl)
6273 inform (DECL_SOURCE_LOCATION (tmpl),
6274 "%qD declared here", tmpl);
6276 parser->scope = error_mark_node;
6277 error_p = true;
6278 /* As below. */
6279 success = true;
6280 cp_lexer_consume_token (parser->lexer);
6281 cp_lexer_consume_token (parser->lexer);
6285 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6286 break;
6287 /* If the next token is an identifier, and the one after
6288 that is a `::', then any valid interpretation would have
6289 found a class-or-namespace-name. */
6290 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6291 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6292 == CPP_SCOPE)
6293 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6294 != CPP_COMPL))
6296 token = cp_lexer_consume_token (parser->lexer);
6297 if (!error_p)
6299 if (!token->error_reported)
6301 tree decl;
6302 tree ambiguous_decls;
6304 decl = cp_parser_lookup_name (parser, token->u.value,
6305 none_type,
6306 /*is_template=*/false,
6307 /*is_namespace=*/false,
6308 /*check_dependency=*/true,
6309 &ambiguous_decls,
6310 token->location);
6311 if (TREE_CODE (decl) == TEMPLATE_DECL)
6312 error_at (token->location,
6313 "%qD used without template parameters",
6314 decl);
6315 else if (ambiguous_decls)
6317 // cp_parser_lookup_name has the same diagnostic,
6318 // thus make sure to emit it at most once.
6319 if (cp_parser_uncommitted_to_tentative_parse_p
6320 (parser))
6322 error_at (token->location,
6323 "reference to %qD is ambiguous",
6324 token->u.value);
6325 print_candidates (ambiguous_decls);
6327 decl = error_mark_node;
6329 else
6331 if (cxx_dialect != cxx98)
6332 cp_parser_name_lookup_error
6333 (parser, token->u.value, decl, NLE_NOT_CXX98,
6334 token->location);
6335 else
6336 cp_parser_name_lookup_error
6337 (parser, token->u.value, decl, NLE_CXX98,
6338 token->location);
6341 parser->scope = error_mark_node;
6342 error_p = true;
6343 /* Treat this as a successful nested-name-specifier
6344 due to:
6346 [basic.lookup.qual]
6348 If the name found is not a class-name (clause
6349 _class_) or namespace-name (_namespace.def_), the
6350 program is ill-formed. */
6351 success = true;
6353 cp_lexer_consume_token (parser->lexer);
6355 break;
6357 /* We've found one valid nested-name-specifier. */
6358 success = true;
6359 /* Name lookup always gives us a DECL. */
6360 if (TREE_CODE (new_scope) == TYPE_DECL)
6361 new_scope = TREE_TYPE (new_scope);
6362 /* Uses of "template" must be followed by actual templates. */
6363 if (template_keyword_p
6364 && !(CLASS_TYPE_P (new_scope)
6365 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6366 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6367 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6368 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6369 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6370 == TEMPLATE_ID_EXPR)))
6371 permerror (input_location, TYPE_P (new_scope)
6372 ? G_("%qT is not a template")
6373 : G_("%qD is not a template"),
6374 new_scope);
6375 /* If it is a class scope, try to complete it; we are about to
6376 be looking up names inside the class. */
6377 if (TYPE_P (new_scope)
6378 /* Since checking types for dependency can be expensive,
6379 avoid doing it if the type is already complete. */
6380 && !COMPLETE_TYPE_P (new_scope)
6381 /* Do not try to complete dependent types. */
6382 && !dependent_type_p (new_scope))
6384 new_scope = complete_type (new_scope);
6385 /* If it is a typedef to current class, use the current
6386 class instead, as the typedef won't have any names inside
6387 it yet. */
6388 if (!COMPLETE_TYPE_P (new_scope)
6389 && currently_open_class (new_scope))
6390 new_scope = TYPE_MAIN_VARIANT (new_scope);
6392 /* Make sure we look in the right scope the next time through
6393 the loop. */
6394 parser->scope = new_scope;
6397 /* If parsing tentatively, replace the sequence of tokens that makes
6398 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6399 token. That way, should we re-parse the token stream, we will
6400 not have to repeat the effort required to do the parse, nor will
6401 we issue duplicate error messages. */
6402 if (success && start)
6404 cp_token *token;
6406 token = cp_lexer_token_at (parser->lexer, start);
6407 /* Reset the contents of the START token. */
6408 token->type = CPP_NESTED_NAME_SPECIFIER;
6409 /* Retrieve any deferred checks. Do not pop this access checks yet
6410 so the memory will not be reclaimed during token replacing below. */
6411 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6412 token->u.tree_check_value->value = parser->scope;
6413 token->u.tree_check_value->checks = get_deferred_access_checks ();
6414 token->u.tree_check_value->qualifying_scope =
6415 parser->qualifying_scope;
6416 token->keyword = RID_MAX;
6418 /* Purge all subsequent tokens. */
6419 cp_lexer_purge_tokens_after (parser->lexer, start);
6422 if (start)
6423 pop_to_parent_deferring_access_checks ();
6425 return success ? parser->scope : NULL_TREE;
6428 /* Parse a nested-name-specifier. See
6429 cp_parser_nested_name_specifier_opt for details. This function
6430 behaves identically, except that it will an issue an error if no
6431 nested-name-specifier is present. */
6433 static tree
6434 cp_parser_nested_name_specifier (cp_parser *parser,
6435 bool typename_keyword_p,
6436 bool check_dependency_p,
6437 bool type_p,
6438 bool is_declaration)
6440 tree scope;
6442 /* Look for the nested-name-specifier. */
6443 scope = cp_parser_nested_name_specifier_opt (parser,
6444 typename_keyword_p,
6445 check_dependency_p,
6446 type_p,
6447 is_declaration);
6448 /* If it was not present, issue an error message. */
6449 if (!scope)
6451 cp_parser_error (parser, "expected nested-name-specifier");
6452 parser->scope = NULL_TREE;
6455 return scope;
6458 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6459 this is either a class-name or a namespace-name (which corresponds
6460 to the class-or-namespace-name production in the grammar). For
6461 C++0x, it can also be a type-name that refers to an enumeration
6462 type or a simple-template-id.
6464 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6465 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6466 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6467 TYPE_P is TRUE iff the next name should be taken as a class-name,
6468 even the same name is declared to be another entity in the same
6469 scope.
6471 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6472 specified by the class-or-namespace-name. If neither is found the
6473 ERROR_MARK_NODE is returned. */
6475 static tree
6476 cp_parser_qualifying_entity (cp_parser *parser,
6477 bool typename_keyword_p,
6478 bool template_keyword_p,
6479 bool check_dependency_p,
6480 bool type_p,
6481 bool is_declaration)
6483 tree saved_scope;
6484 tree saved_qualifying_scope;
6485 tree saved_object_scope;
6486 tree scope;
6487 bool only_class_p;
6488 bool successful_parse_p;
6490 /* DR 743: decltype can appear in a nested-name-specifier. */
6491 if (cp_lexer_next_token_is_decltype (parser->lexer))
6493 scope = cp_parser_decltype (parser);
6494 if (TREE_CODE (scope) != ENUMERAL_TYPE
6495 && !MAYBE_CLASS_TYPE_P (scope))
6497 cp_parser_simulate_error (parser);
6498 return error_mark_node;
6500 if (TYPE_NAME (scope))
6501 scope = TYPE_NAME (scope);
6502 return scope;
6505 /* Before we try to parse the class-name, we must save away the
6506 current PARSER->SCOPE since cp_parser_class_name will destroy
6507 it. */
6508 saved_scope = parser->scope;
6509 saved_qualifying_scope = parser->qualifying_scope;
6510 saved_object_scope = parser->object_scope;
6511 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6512 there is no need to look for a namespace-name. */
6513 only_class_p = template_keyword_p
6514 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6515 if (!only_class_p)
6516 cp_parser_parse_tentatively (parser);
6517 scope = cp_parser_class_name (parser,
6518 typename_keyword_p,
6519 template_keyword_p,
6520 type_p ? class_type : none_type,
6521 check_dependency_p,
6522 /*class_head_p=*/false,
6523 is_declaration,
6524 /*enum_ok=*/cxx_dialect > cxx98);
6525 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6526 /* If that didn't work, try for a namespace-name. */
6527 if (!only_class_p && !successful_parse_p)
6529 /* Restore the saved scope. */
6530 parser->scope = saved_scope;
6531 parser->qualifying_scope = saved_qualifying_scope;
6532 parser->object_scope = saved_object_scope;
6533 /* If we are not looking at an identifier followed by the scope
6534 resolution operator, then this is not part of a
6535 nested-name-specifier. (Note that this function is only used
6536 to parse the components of a nested-name-specifier.) */
6537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6538 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6539 return error_mark_node;
6540 scope = cp_parser_namespace_name (parser);
6543 return scope;
6546 /* Return true if we are looking at a compound-literal, false otherwise. */
6548 static bool
6549 cp_parser_compound_literal_p (cp_parser *parser)
6551 cp_lexer_save_tokens (parser->lexer);
6553 /* Skip tokens until the next token is a closing parenthesis.
6554 If we find the closing `)', and the next token is a `{', then
6555 we are looking at a compound-literal. */
6556 bool compound_literal_p
6557 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6558 /*consume_paren=*/true)
6559 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6561 /* Roll back the tokens we skipped. */
6562 cp_lexer_rollback_tokens (parser->lexer);
6564 return compound_literal_p;
6567 /* Parse a postfix-expression.
6569 postfix-expression:
6570 primary-expression
6571 postfix-expression [ expression ]
6572 postfix-expression ( expression-list [opt] )
6573 simple-type-specifier ( expression-list [opt] )
6574 typename :: [opt] nested-name-specifier identifier
6575 ( expression-list [opt] )
6576 typename :: [opt] nested-name-specifier template [opt] template-id
6577 ( expression-list [opt] )
6578 postfix-expression . template [opt] id-expression
6579 postfix-expression -> template [opt] id-expression
6580 postfix-expression . pseudo-destructor-name
6581 postfix-expression -> pseudo-destructor-name
6582 postfix-expression ++
6583 postfix-expression --
6584 dynamic_cast < type-id > ( expression )
6585 static_cast < type-id > ( expression )
6586 reinterpret_cast < type-id > ( expression )
6587 const_cast < type-id > ( expression )
6588 typeid ( expression )
6589 typeid ( type-id )
6591 GNU Extension:
6593 postfix-expression:
6594 ( type-id ) { initializer-list , [opt] }
6596 This extension is a GNU version of the C99 compound-literal
6597 construct. (The C99 grammar uses `type-name' instead of `type-id',
6598 but they are essentially the same concept.)
6600 If ADDRESS_P is true, the postfix expression is the operand of the
6601 `&' operator. CAST_P is true if this expression is the target of a
6602 cast.
6604 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6605 class member access expressions [expr.ref].
6607 Returns a representation of the expression. */
6609 static cp_expr
6610 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6611 bool member_access_only_p, bool decltype_p,
6612 cp_id_kind * pidk_return)
6614 cp_token *token;
6615 location_t loc;
6616 enum rid keyword;
6617 cp_id_kind idk = CP_ID_KIND_NONE;
6618 cp_expr postfix_expression = NULL_TREE;
6619 bool is_member_access = false;
6620 int saved_in_statement = -1;
6622 /* Peek at the next token. */
6623 token = cp_lexer_peek_token (parser->lexer);
6624 loc = token->location;
6625 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6627 /* Some of the productions are determined by keywords. */
6628 keyword = token->keyword;
6629 switch (keyword)
6631 case RID_DYNCAST:
6632 case RID_STATCAST:
6633 case RID_REINTCAST:
6634 case RID_CONSTCAST:
6636 tree type;
6637 cp_expr expression;
6638 const char *saved_message;
6639 bool saved_in_type_id_in_expr_p;
6641 /* All of these can be handled in the same way from the point
6642 of view of parsing. Begin by consuming the token
6643 identifying the cast. */
6644 cp_lexer_consume_token (parser->lexer);
6646 /* New types cannot be defined in the cast. */
6647 saved_message = parser->type_definition_forbidden_message;
6648 parser->type_definition_forbidden_message
6649 = G_("types may not be defined in casts");
6651 /* Look for the opening `<'. */
6652 cp_parser_require (parser, CPP_LESS, RT_LESS);
6653 /* Parse the type to which we are casting. */
6654 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6655 parser->in_type_id_in_expr_p = true;
6656 type = cp_parser_type_id (parser);
6657 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6658 /* Look for the closing `>'. */
6659 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6660 /* Restore the old message. */
6661 parser->type_definition_forbidden_message = saved_message;
6663 bool saved_greater_than_is_operator_p
6664 = parser->greater_than_is_operator_p;
6665 parser->greater_than_is_operator_p = true;
6667 /* And the expression which is being cast. */
6668 matching_parens parens;
6669 parens.require_open (parser);
6670 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6671 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6672 RT_CLOSE_PAREN);
6673 location_t end_loc = close_paren ?
6674 close_paren->location : UNKNOWN_LOCATION;
6676 parser->greater_than_is_operator_p
6677 = saved_greater_than_is_operator_p;
6679 /* Only type conversions to integral or enumeration types
6680 can be used in constant-expressions. */
6681 if (!cast_valid_in_integral_constant_expression_p (type)
6682 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6684 postfix_expression = error_mark_node;
6685 break;
6688 switch (keyword)
6690 case RID_DYNCAST:
6691 postfix_expression
6692 = build_dynamic_cast (type, expression, tf_warning_or_error);
6693 break;
6694 case RID_STATCAST:
6695 postfix_expression
6696 = build_static_cast (type, expression, tf_warning_or_error);
6697 break;
6698 case RID_REINTCAST:
6699 postfix_expression
6700 = build_reinterpret_cast (type, expression,
6701 tf_warning_or_error);
6702 break;
6703 case RID_CONSTCAST:
6704 postfix_expression
6705 = build_const_cast (type, expression, tf_warning_or_error);
6706 break;
6707 default:
6708 gcc_unreachable ();
6711 /* Construct a location e.g. :
6712 reinterpret_cast <int *> (expr)
6713 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6714 ranging from the start of the "*_cast" token to the final closing
6715 paren, with the caret at the start. */
6716 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6717 postfix_expression.set_location (cp_cast_loc);
6719 break;
6721 case RID_TYPEID:
6723 tree type;
6724 const char *saved_message;
6725 bool saved_in_type_id_in_expr_p;
6727 /* Consume the `typeid' token. */
6728 cp_lexer_consume_token (parser->lexer);
6729 /* Look for the `(' token. */
6730 matching_parens parens;
6731 parens.require_open (parser);
6732 /* Types cannot be defined in a `typeid' expression. */
6733 saved_message = parser->type_definition_forbidden_message;
6734 parser->type_definition_forbidden_message
6735 = G_("types may not be defined in a %<typeid%> expression");
6736 /* We can't be sure yet whether we're looking at a type-id or an
6737 expression. */
6738 cp_parser_parse_tentatively (parser);
6739 /* Try a type-id first. */
6740 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6741 parser->in_type_id_in_expr_p = true;
6742 type = cp_parser_type_id (parser);
6743 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6744 /* Look for the `)' token. Otherwise, we can't be sure that
6745 we're not looking at an expression: consider `typeid (int
6746 (3))', for example. */
6747 cp_token *close_paren = parens.require_close (parser);
6748 /* If all went well, simply lookup the type-id. */
6749 if (cp_parser_parse_definitely (parser))
6750 postfix_expression = get_typeid (type, tf_warning_or_error);
6751 /* Otherwise, fall back to the expression variant. */
6752 else
6754 tree expression;
6756 /* Look for an expression. */
6757 expression = cp_parser_expression (parser, & idk);
6758 /* Compute its typeid. */
6759 postfix_expression = build_typeid (expression, tf_warning_or_error);
6760 /* Look for the `)' token. */
6761 close_paren = parens.require_close (parser);
6763 /* Restore the saved message. */
6764 parser->type_definition_forbidden_message = saved_message;
6765 /* `typeid' may not appear in an integral constant expression. */
6766 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6767 postfix_expression = error_mark_node;
6769 /* Construct a location e.g. :
6770 typeid (expr)
6771 ^~~~~~~~~~~~~
6772 ranging from the start of the "typeid" token to the final closing
6773 paren, with the caret at the start. */
6774 if (close_paren)
6776 location_t typeid_loc
6777 = make_location (start_loc, start_loc, close_paren->location);
6778 postfix_expression.set_location (typeid_loc);
6781 break;
6783 case RID_TYPENAME:
6785 tree type;
6786 /* The syntax permitted here is the same permitted for an
6787 elaborated-type-specifier. */
6788 ++parser->prevent_constrained_type_specifiers;
6789 type = cp_parser_elaborated_type_specifier (parser,
6790 /*is_friend=*/false,
6791 /*is_declaration=*/false);
6792 --parser->prevent_constrained_type_specifiers;
6793 postfix_expression = cp_parser_functional_cast (parser, type);
6795 break;
6797 case RID_CILK_SPAWN:
6799 location_t cilk_spawn_loc
6800 = cp_lexer_peek_token (parser->lexer)->location;
6801 cp_lexer_consume_token (parser->lexer);
6802 token = cp_lexer_peek_token (parser->lexer);
6803 if (token->type == CPP_SEMICOLON)
6805 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6806 "an expression");
6807 postfix_expression = error_mark_node;
6808 break;
6810 else if (!current_function_decl)
6812 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6813 "inside a function");
6814 postfix_expression = error_mark_node;
6815 break;
6817 else
6819 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6820 saved_in_statement = parser->in_statement;
6821 parser->in_statement |= IN_CILK_SPAWN;
6823 cfun->calls_cilk_spawn = 1;
6824 postfix_expression =
6825 cp_parser_postfix_expression (parser, false, false,
6826 false, false, &idk);
6827 if (!flag_cilkplus)
6829 error_at (token->location, "-fcilkplus must be enabled to use"
6830 " %<_Cilk_spawn%>");
6831 cfun->calls_cilk_spawn = 0;
6833 else if (saved_in_statement & IN_CILK_SPAWN)
6835 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6836 "are not permitted");
6837 postfix_expression = error_mark_node;
6838 cfun->calls_cilk_spawn = 0;
6840 else
6842 location_t loc = postfix_expression.get_location ();
6843 postfix_expression = build_cilk_spawn (token->location,
6844 postfix_expression);
6845 /* Build a location of the form:
6846 _Cilk_spawn expr
6847 ~~~~~~~~~~~~^~~~
6848 with caret at the expr, ranging from the start of the
6849 _Cilk_spawn token to the end of the expression. */
6850 location_t combined_loc =
6851 make_location (loc, cilk_spawn_loc, get_finish (loc));
6852 postfix_expression.set_location (combined_loc);
6853 if (postfix_expression != error_mark_node)
6854 SET_EXPR_LOCATION (postfix_expression, input_location);
6855 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6857 break;
6860 case RID_ADDRESSOF:
6861 case RID_BUILTIN_SHUFFLE:
6862 case RID_BUILTIN_LAUNDER:
6864 vec<tree, va_gc> *vec;
6865 unsigned int i;
6866 tree p;
6868 cp_lexer_consume_token (parser->lexer);
6869 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL);
6872 if (vec == NULL)
6874 postfix_expression = error_mark_node;
6875 break;
6878 FOR_EACH_VEC_ELT (*vec, i, p)
6879 mark_exp_read (p);
6881 switch (keyword)
6883 case RID_ADDRESSOF:
6884 if (vec->length () == 1)
6885 postfix_expression
6886 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6887 else
6889 error_at (loc, "wrong number of arguments to "
6890 "%<__builtin_addressof%>");
6891 postfix_expression = error_mark_node;
6893 break;
6895 case RID_BUILTIN_LAUNDER:
6896 if (vec->length () == 1)
6897 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6898 tf_warning_or_error);
6899 else
6901 error_at (loc, "wrong number of arguments to "
6902 "%<__builtin_launder%>");
6903 postfix_expression = error_mark_node;
6905 break;
6907 case RID_BUILTIN_SHUFFLE:
6908 if (vec->length () == 2)
6909 postfix_expression
6910 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6911 (*vec)[1], tf_warning_or_error);
6912 else if (vec->length () == 3)
6913 postfix_expression
6914 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6915 (*vec)[2], tf_warning_or_error);
6916 else
6918 error_at (loc, "wrong number of arguments to "
6919 "%<__builtin_shuffle%>");
6920 postfix_expression = error_mark_node;
6922 break;
6924 default:
6925 gcc_unreachable ();
6927 break;
6930 default:
6932 tree type;
6934 /* If the next thing is a simple-type-specifier, we may be
6935 looking at a functional cast. We could also be looking at
6936 an id-expression. So, we try the functional cast, and if
6937 that doesn't work we fall back to the primary-expression. */
6938 cp_parser_parse_tentatively (parser);
6939 /* Look for the simple-type-specifier. */
6940 ++parser->prevent_constrained_type_specifiers;
6941 type = cp_parser_simple_type_specifier (parser,
6942 /*decl_specs=*/NULL,
6943 CP_PARSER_FLAGS_NONE);
6944 --parser->prevent_constrained_type_specifiers;
6945 /* Parse the cast itself. */
6946 if (!cp_parser_error_occurred (parser))
6947 postfix_expression
6948 = cp_parser_functional_cast (parser, type);
6949 /* If that worked, we're done. */
6950 if (cp_parser_parse_definitely (parser))
6951 break;
6953 /* If the functional-cast didn't work out, try a
6954 compound-literal. */
6955 if (cp_parser_allow_gnu_extensions_p (parser)
6956 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6958 cp_expr initializer = NULL_TREE;
6960 cp_parser_parse_tentatively (parser);
6962 matching_parens parens;
6963 parens.consume_open (parser);
6965 /* Avoid calling cp_parser_type_id pointlessly, see comment
6966 in cp_parser_cast_expression about c++/29234. */
6967 if (!cp_parser_compound_literal_p (parser))
6968 cp_parser_simulate_error (parser);
6969 else
6971 /* Parse the type. */
6972 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6973 parser->in_type_id_in_expr_p = true;
6974 type = cp_parser_type_id (parser);
6975 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6976 parens.require_close (parser);
6979 /* If things aren't going well, there's no need to
6980 keep going. */
6981 if (!cp_parser_error_occurred (parser))
6983 bool non_constant_p;
6984 /* Parse the brace-enclosed initializer list. */
6985 initializer = cp_parser_braced_list (parser,
6986 &non_constant_p);
6988 /* If that worked, we're definitely looking at a
6989 compound-literal expression. */
6990 if (cp_parser_parse_definitely (parser))
6992 /* Warn the user that a compound literal is not
6993 allowed in standard C++. */
6994 pedwarn (input_location, OPT_Wpedantic,
6995 "ISO C++ forbids compound-literals");
6996 /* For simplicity, we disallow compound literals in
6997 constant-expressions. We could
6998 allow compound literals of integer type, whose
6999 initializer was a constant, in constant
7000 expressions. Permitting that usage, as a further
7001 extension, would not change the meaning of any
7002 currently accepted programs. (Of course, as
7003 compound literals are not part of ISO C++, the
7004 standard has nothing to say.) */
7005 if (cp_parser_non_integral_constant_expression (parser,
7006 NIC_NCC))
7008 postfix_expression = error_mark_node;
7009 break;
7011 /* Form the representation of the compound-literal. */
7012 postfix_expression
7013 = finish_compound_literal (type, initializer,
7014 tf_warning_or_error, fcl_c99);
7015 postfix_expression.set_location (initializer.get_location ());
7016 break;
7020 /* It must be a primary-expression. */
7021 postfix_expression
7022 = cp_parser_primary_expression (parser, address_p, cast_p,
7023 /*template_arg_p=*/false,
7024 decltype_p,
7025 &idk);
7027 break;
7030 /* Note that we don't need to worry about calling build_cplus_new on a
7031 class-valued CALL_EXPR in decltype when it isn't the end of the
7032 postfix-expression; unary_complex_lvalue will take care of that for
7033 all these cases. */
7035 /* Keep looping until the postfix-expression is complete. */
7036 while (true)
7038 if (idk == CP_ID_KIND_UNQUALIFIED
7039 && identifier_p (postfix_expression)
7040 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7041 /* It is not a Koenig lookup function call. */
7042 postfix_expression
7043 = unqualified_name_lookup_error (postfix_expression);
7045 /* Peek at the next token. */
7046 token = cp_lexer_peek_token (parser->lexer);
7048 switch (token->type)
7050 case CPP_OPEN_SQUARE:
7051 if (cp_next_tokens_can_be_std_attribute_p (parser))
7053 cp_parser_error (parser,
7054 "two consecutive %<[%> shall "
7055 "only introduce an attribute");
7056 return error_mark_node;
7058 postfix_expression
7059 = cp_parser_postfix_open_square_expression (parser,
7060 postfix_expression,
7061 false,
7062 decltype_p);
7063 postfix_expression.set_range (start_loc,
7064 postfix_expression.get_location ());
7066 idk = CP_ID_KIND_NONE;
7067 is_member_access = false;
7068 break;
7070 case CPP_OPEN_PAREN:
7071 /* postfix-expression ( expression-list [opt] ) */
7073 bool koenig_p;
7074 bool is_builtin_constant_p;
7075 bool saved_integral_constant_expression_p = false;
7076 bool saved_non_integral_constant_expression_p = false;
7077 tsubst_flags_t complain = complain_flags (decltype_p);
7078 vec<tree, va_gc> *args;
7079 location_t close_paren_loc = UNKNOWN_LOCATION;
7081 is_member_access = false;
7083 is_builtin_constant_p
7084 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
7085 if (is_builtin_constant_p)
7087 /* The whole point of __builtin_constant_p is to allow
7088 non-constant expressions to appear as arguments. */
7089 saved_integral_constant_expression_p
7090 = parser->integral_constant_expression_p;
7091 saved_non_integral_constant_expression_p
7092 = parser->non_integral_constant_expression_p;
7093 parser->integral_constant_expression_p = false;
7095 args = (cp_parser_parenthesized_expression_list
7096 (parser, non_attr,
7097 /*cast_p=*/false, /*allow_expansion_p=*/true,
7098 /*non_constant_p=*/NULL,
7099 /*close_paren_loc=*/&close_paren_loc));
7100 if (is_builtin_constant_p)
7102 parser->integral_constant_expression_p
7103 = saved_integral_constant_expression_p;
7104 parser->non_integral_constant_expression_p
7105 = saved_non_integral_constant_expression_p;
7108 if (args == NULL)
7110 postfix_expression = error_mark_node;
7111 break;
7114 /* Function calls are not permitted in
7115 constant-expressions. */
7116 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7117 && cp_parser_non_integral_constant_expression (parser,
7118 NIC_FUNC_CALL))
7120 postfix_expression = error_mark_node;
7121 release_tree_vector (args);
7122 break;
7125 koenig_p = false;
7126 if (idk == CP_ID_KIND_UNQUALIFIED
7127 || idk == CP_ID_KIND_TEMPLATE_ID)
7129 if (identifier_p (postfix_expression))
7131 if (!args->is_empty ())
7133 koenig_p = true;
7134 if (!any_type_dependent_arguments_p (args))
7135 postfix_expression
7136 = perform_koenig_lookup (postfix_expression, args,
7137 complain);
7139 else
7140 postfix_expression
7141 = unqualified_fn_lookup_error (postfix_expression);
7143 /* We do not perform argument-dependent lookup if
7144 normal lookup finds a non-function, in accordance
7145 with the expected resolution of DR 218. */
7146 else if (!args->is_empty ()
7147 && is_overloaded_fn (postfix_expression))
7149 tree fn = get_first_fn (postfix_expression);
7150 fn = STRIP_TEMPLATE (fn);
7152 /* Do not do argument dependent lookup if regular
7153 lookup finds a member function or a block-scope
7154 function declaration. [basic.lookup.argdep]/3 */
7155 if (!DECL_FUNCTION_MEMBER_P (fn)
7156 && !DECL_LOCAL_FUNCTION_P (fn))
7158 koenig_p = true;
7159 if (!any_type_dependent_arguments_p (args))
7160 postfix_expression
7161 = perform_koenig_lookup (postfix_expression, args,
7162 complain);
7167 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7168 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7169 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7170 && vec_safe_length (args) == 3)
7172 tree arg0 = (*args)[0];
7173 tree arg1 = (*args)[1];
7174 tree arg2 = (*args)[2];
7175 int literal_mask = ((!!integer_zerop (arg1) << 1)
7176 | (!!integer_zerop (arg2) << 2));
7177 if (TREE_CODE (arg2) == CONST_DECL)
7178 arg2 = DECL_INITIAL (arg2);
7179 warn_for_memset (input_location, arg0, arg2, literal_mask);
7182 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7184 tree instance = TREE_OPERAND (postfix_expression, 0);
7185 tree fn = TREE_OPERAND (postfix_expression, 1);
7187 if (processing_template_decl
7188 && (type_dependent_object_expression_p (instance)
7189 || (!BASELINK_P (fn)
7190 && TREE_CODE (fn) != FIELD_DECL)
7191 || type_dependent_expression_p (fn)
7192 || any_type_dependent_arguments_p (args)))
7194 maybe_generic_this_capture (instance, fn);
7195 postfix_expression
7196 = build_min_nt_call_vec (postfix_expression, args);
7197 release_tree_vector (args);
7198 break;
7201 if (BASELINK_P (fn))
7203 postfix_expression
7204 = (build_new_method_call
7205 (instance, fn, &args, NULL_TREE,
7206 (idk == CP_ID_KIND_QUALIFIED
7207 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7208 : LOOKUP_NORMAL),
7209 /*fn_p=*/NULL,
7210 complain));
7212 else
7213 postfix_expression
7214 = finish_call_expr (postfix_expression, &args,
7215 /*disallow_virtual=*/false,
7216 /*koenig_p=*/false,
7217 complain);
7219 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7220 || TREE_CODE (postfix_expression) == MEMBER_REF
7221 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7222 postfix_expression = (build_offset_ref_call_from_tree
7223 (postfix_expression, &args,
7224 complain));
7225 else if (idk == CP_ID_KIND_QUALIFIED)
7226 /* A call to a static class member, or a namespace-scope
7227 function. */
7228 postfix_expression
7229 = finish_call_expr (postfix_expression, &args,
7230 /*disallow_virtual=*/true,
7231 koenig_p,
7232 complain);
7233 else
7234 /* All other function calls. */
7235 postfix_expression
7236 = finish_call_expr (postfix_expression, &args,
7237 /*disallow_virtual=*/false,
7238 koenig_p,
7239 complain);
7241 if (close_paren_loc != UNKNOWN_LOCATION)
7243 location_t combined_loc = make_location (token->location,
7244 start_loc,
7245 close_paren_loc);
7246 postfix_expression.set_location (combined_loc);
7249 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7250 idk = CP_ID_KIND_NONE;
7252 release_tree_vector (args);
7254 break;
7256 case CPP_DOT:
7257 case CPP_DEREF:
7258 /* postfix-expression . template [opt] id-expression
7259 postfix-expression . pseudo-destructor-name
7260 postfix-expression -> template [opt] id-expression
7261 postfix-expression -> pseudo-destructor-name */
7263 /* Consume the `.' or `->' operator. */
7264 cp_lexer_consume_token (parser->lexer);
7266 postfix_expression
7267 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7268 postfix_expression,
7269 false, &idk, loc);
7271 is_member_access = true;
7272 break;
7274 case CPP_PLUS_PLUS:
7275 /* postfix-expression ++ */
7276 /* Consume the `++' token. */
7277 cp_lexer_consume_token (parser->lexer);
7278 /* Generate a representation for the complete expression. */
7279 postfix_expression
7280 = finish_increment_expr (postfix_expression,
7281 POSTINCREMENT_EXPR);
7282 /* Increments may not appear in constant-expressions. */
7283 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7284 postfix_expression = error_mark_node;
7285 idk = CP_ID_KIND_NONE;
7286 is_member_access = false;
7287 break;
7289 case CPP_MINUS_MINUS:
7290 /* postfix-expression -- */
7291 /* Consume the `--' token. */
7292 cp_lexer_consume_token (parser->lexer);
7293 /* Generate a representation for the complete expression. */
7294 postfix_expression
7295 = finish_increment_expr (postfix_expression,
7296 POSTDECREMENT_EXPR);
7297 /* Decrements may not appear in constant-expressions. */
7298 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7299 postfix_expression = error_mark_node;
7300 idk = CP_ID_KIND_NONE;
7301 is_member_access = false;
7302 break;
7304 default:
7305 if (pidk_return != NULL)
7306 * pidk_return = idk;
7307 if (member_access_only_p)
7308 return is_member_access
7309 ? postfix_expression
7310 : cp_expr (error_mark_node);
7311 else
7312 return postfix_expression;
7316 /* We should never get here. */
7317 gcc_unreachable ();
7318 return error_mark_node;
7321 /* This function parses Cilk Plus array notations. If a normal array expr. is
7322 parsed then the array index is passed back to the caller through *INIT_INDEX
7323 and the function returns a NULL_TREE. If array notation expr. is parsed,
7324 then *INIT_INDEX is ignored by the caller and the function returns
7325 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7326 error_mark_node. */
7328 static tree
7329 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7330 tree array_value)
7332 cp_token *token = NULL;
7333 tree length_index, stride = NULL_TREE, value_tree, array_type;
7334 if (!array_value || array_value == error_mark_node)
7336 cp_parser_skip_to_end_of_statement (parser);
7337 return error_mark_node;
7340 array_type = TREE_TYPE (array_value);
7342 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7343 parser->colon_corrects_to_scope_p = false;
7344 token = cp_lexer_peek_token (parser->lexer);
7346 if (!token)
7348 cp_parser_error (parser, "expected %<:%> or numeral");
7349 return error_mark_node;
7351 else if (token->type == CPP_COLON)
7353 /* Consume the ':'. */
7354 cp_lexer_consume_token (parser->lexer);
7356 /* If we are here, then we have a case like this A[:]. */
7357 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7359 cp_parser_error (parser, "expected %<]%>");
7360 cp_parser_skip_to_end_of_statement (parser);
7361 return error_mark_node;
7363 *init_index = NULL_TREE;
7364 stride = NULL_TREE;
7365 length_index = NULL_TREE;
7367 else
7369 /* If we are here, then there are three valid possibilities:
7370 1. ARRAY [ EXP ]
7371 2. ARRAY [ EXP : EXP ]
7372 3. ARRAY [ EXP : EXP : EXP ] */
7374 *init_index = cp_parser_expression (parser);
7375 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7377 /* This indicates that we have a normal array expression. */
7378 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7379 return NULL_TREE;
7382 /* Consume the ':'. */
7383 cp_lexer_consume_token (parser->lexer);
7384 length_index = cp_parser_expression (parser);
7385 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7387 cp_lexer_consume_token (parser->lexer);
7388 stride = cp_parser_expression (parser);
7391 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7393 if (*init_index == error_mark_node || length_index == error_mark_node
7394 || stride == error_mark_node || array_type == error_mark_node)
7396 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7397 cp_lexer_consume_token (parser->lexer);
7398 return error_mark_node;
7400 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7402 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7403 length_index, stride, array_type);
7404 return value_tree;
7407 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7408 by cp_parser_builtin_offsetof. We're looking for
7410 postfix-expression [ expression ]
7411 postfix-expression [ braced-init-list ] (C++11)
7413 FOR_OFFSETOF is set if we're being called in that context, which
7414 changes how we deal with integer constant expressions. */
7416 static tree
7417 cp_parser_postfix_open_square_expression (cp_parser *parser,
7418 tree postfix_expression,
7419 bool for_offsetof,
7420 bool decltype_p)
7422 tree index = NULL_TREE;
7423 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7424 bool saved_greater_than_is_operator_p;
7426 /* Consume the `[' token. */
7427 cp_lexer_consume_token (parser->lexer);
7429 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7430 parser->greater_than_is_operator_p = true;
7432 /* Parse the index expression. */
7433 /* ??? For offsetof, there is a question of what to allow here. If
7434 offsetof is not being used in an integral constant expression context,
7435 then we *could* get the right answer by computing the value at runtime.
7436 If we are in an integral constant expression context, then we might
7437 could accept any constant expression; hard to say without analysis.
7438 Rather than open the barn door too wide right away, allow only integer
7439 constant expressions here. */
7440 if (for_offsetof)
7441 index = cp_parser_constant_expression (parser);
7442 else
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7446 bool expr_nonconst_p;
7447 cp_lexer_set_source_position (parser->lexer);
7448 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7449 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7450 if (flag_cilkplus
7451 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7453 error_at (cp_lexer_peek_token (parser->lexer)->location,
7454 "braced list index is not allowed with array "
7455 "notation");
7456 cp_parser_skip_to_end_of_statement (parser);
7457 return error_mark_node;
7460 else if (flag_cilkplus)
7462 /* Here are have these two options:
7463 ARRAY[EXP : EXP] - Array notation expr with default
7464 stride of 1.
7465 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7466 stride. */
7467 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7468 postfix_expression);
7469 if (an_exp)
7470 return an_exp;
7472 else
7473 index = cp_parser_expression (parser);
7476 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7478 /* Look for the closing `]'. */
7479 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7481 /* Build the ARRAY_REF. */
7482 postfix_expression = grok_array_decl (loc, postfix_expression,
7483 index, decltype_p);
7485 /* When not doing offsetof, array references are not permitted in
7486 constant-expressions. */
7487 if (!for_offsetof
7488 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7489 postfix_expression = error_mark_node;
7491 return postfix_expression;
7494 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7495 by cp_parser_builtin_offsetof. We're looking for
7497 postfix-expression . template [opt] id-expression
7498 postfix-expression . pseudo-destructor-name
7499 postfix-expression -> template [opt] id-expression
7500 postfix-expression -> pseudo-destructor-name
7502 FOR_OFFSETOF is set if we're being called in that context. That sorta
7503 limits what of the above we'll actually accept, but nevermind.
7504 TOKEN_TYPE is the "." or "->" token, which will already have been
7505 removed from the stream. */
7507 static tree
7508 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7509 enum cpp_ttype token_type,
7510 cp_expr postfix_expression,
7511 bool for_offsetof, cp_id_kind *idk,
7512 location_t location)
7514 tree name;
7515 bool dependent_p;
7516 bool pseudo_destructor_p;
7517 tree scope = NULL_TREE;
7518 location_t start_loc = postfix_expression.get_start ();
7520 /* If this is a `->' operator, dereference the pointer. */
7521 if (token_type == CPP_DEREF)
7522 postfix_expression = build_x_arrow (location, postfix_expression,
7523 tf_warning_or_error);
7524 /* Check to see whether or not the expression is type-dependent and
7525 not the current instantiation. */
7526 dependent_p = type_dependent_object_expression_p (postfix_expression);
7527 /* The identifier following the `->' or `.' is not qualified. */
7528 parser->scope = NULL_TREE;
7529 parser->qualifying_scope = NULL_TREE;
7530 parser->object_scope = NULL_TREE;
7531 *idk = CP_ID_KIND_NONE;
7533 /* Enter the scope corresponding to the type of the object
7534 given by the POSTFIX_EXPRESSION. */
7535 if (!dependent_p)
7537 scope = TREE_TYPE (postfix_expression);
7538 /* According to the standard, no expression should ever have
7539 reference type. Unfortunately, we do not currently match
7540 the standard in this respect in that our internal representation
7541 of an expression may have reference type even when the standard
7542 says it does not. Therefore, we have to manually obtain the
7543 underlying type here. */
7544 scope = non_reference (scope);
7545 /* The type of the POSTFIX_EXPRESSION must be complete. */
7546 /* Unlike the object expression in other contexts, *this is not
7547 required to be of complete type for purposes of class member
7548 access (5.2.5) outside the member function body. */
7549 if (postfix_expression != current_class_ref
7550 && scope != error_mark_node
7551 && !(processing_template_decl
7552 && current_class_type
7553 && (same_type_ignoring_top_level_qualifiers_p
7554 (scope, current_class_type))))
7556 scope = complete_type (scope);
7557 if (!COMPLETE_TYPE_P (scope)
7558 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7559 && EXPR_P (postfix_expression))
7561 /* In a template, be permissive by treating an object expression
7562 of incomplete type as dependent (after a pedwarn). */
7563 diagnostic_t kind = (processing_template_decl
7564 && MAYBE_CLASS_TYPE_P (scope)
7565 ? DK_PEDWARN
7566 : DK_ERROR);
7567 cxx_incomplete_type_diagnostic
7568 (location_of (postfix_expression),
7569 postfix_expression, scope, kind);
7570 if (!MAYBE_CLASS_TYPE_P (scope))
7571 return error_mark_node;
7572 if (processing_template_decl)
7574 dependent_p = true;
7575 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7580 if (!dependent_p)
7582 /* Let the name lookup machinery know that we are processing a
7583 class member access expression. */
7584 parser->context->object_type = scope;
7585 /* If something went wrong, we want to be able to discern that case,
7586 as opposed to the case where there was no SCOPE due to the type
7587 of expression being dependent. */
7588 if (!scope)
7589 scope = error_mark_node;
7590 /* If the SCOPE was erroneous, make the various semantic analysis
7591 functions exit quickly -- and without issuing additional error
7592 messages. */
7593 if (scope == error_mark_node)
7594 postfix_expression = error_mark_node;
7598 if (dependent_p)
7599 /* Tell cp_parser_lookup_name that there was an object, even though it's
7600 type-dependent. */
7601 parser->context->object_type = unknown_type_node;
7603 /* Assume this expression is not a pseudo-destructor access. */
7604 pseudo_destructor_p = false;
7606 /* If the SCOPE is a scalar type, then, if this is a valid program,
7607 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7608 is type dependent, it can be pseudo-destructor-name or something else.
7609 Try to parse it as pseudo-destructor-name first. */
7610 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7612 tree s;
7613 tree type;
7615 cp_parser_parse_tentatively (parser);
7616 /* Parse the pseudo-destructor-name. */
7617 s = NULL_TREE;
7618 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7619 &s, &type);
7620 if (dependent_p
7621 && (cp_parser_error_occurred (parser)
7622 || !SCALAR_TYPE_P (type)))
7623 cp_parser_abort_tentative_parse (parser);
7624 else if (cp_parser_parse_definitely (parser))
7626 pseudo_destructor_p = true;
7627 postfix_expression
7628 = finish_pseudo_destructor_expr (postfix_expression,
7629 s, type, location);
7633 if (!pseudo_destructor_p)
7635 /* If the SCOPE is not a scalar type, we are looking at an
7636 ordinary class member access expression, rather than a
7637 pseudo-destructor-name. */
7638 bool template_p;
7639 cp_token *token = cp_lexer_peek_token (parser->lexer);
7640 /* Parse the id-expression. */
7641 name = (cp_parser_id_expression
7642 (parser,
7643 cp_parser_optional_template_keyword (parser),
7644 /*check_dependency_p=*/true,
7645 &template_p,
7646 /*declarator_p=*/false,
7647 /*optional_p=*/false));
7648 /* In general, build a SCOPE_REF if the member name is qualified.
7649 However, if the name was not dependent and has already been
7650 resolved; there is no need to build the SCOPE_REF. For example;
7652 struct X { void f(); };
7653 template <typename T> void f(T* t) { t->X::f(); }
7655 Even though "t" is dependent, "X::f" is not and has been resolved
7656 to a BASELINK; there is no need to include scope information. */
7658 /* But we do need to remember that there was an explicit scope for
7659 virtual function calls. */
7660 if (parser->scope)
7661 *idk = CP_ID_KIND_QUALIFIED;
7663 /* If the name is a template-id that names a type, we will get a
7664 TYPE_DECL here. That is invalid code. */
7665 if (TREE_CODE (name) == TYPE_DECL)
7667 error_at (token->location, "invalid use of %qD", name);
7668 postfix_expression = error_mark_node;
7670 else
7672 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7674 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7676 error_at (token->location, "%<%D::%D%> is not a class member",
7677 parser->scope, name);
7678 postfix_expression = error_mark_node;
7680 else
7681 name = build_qualified_name (/*type=*/NULL_TREE,
7682 parser->scope,
7683 name,
7684 template_p);
7685 parser->scope = NULL_TREE;
7686 parser->qualifying_scope = NULL_TREE;
7687 parser->object_scope = NULL_TREE;
7689 if (parser->scope && name && BASELINK_P (name))
7690 adjust_result_of_qualified_name_lookup
7691 (name, parser->scope, scope);
7692 postfix_expression
7693 = finish_class_member_access_expr (postfix_expression, name,
7694 template_p,
7695 tf_warning_or_error);
7696 /* Build a location e.g.:
7697 ptr->access_expr
7698 ~~~^~~~~~~~~~~~~
7699 where the caret is at the deref token, ranging from
7700 the start of postfix_expression to the end of the access expr. */
7701 location_t end_loc
7702 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7703 location_t combined_loc
7704 = make_location (input_location, start_loc, end_loc);
7705 protected_set_expr_location (postfix_expression, combined_loc);
7709 /* We no longer need to look up names in the scope of the object on
7710 the left-hand side of the `.' or `->' operator. */
7711 parser->context->object_type = NULL_TREE;
7713 /* Outside of offsetof, these operators may not appear in
7714 constant-expressions. */
7715 if (!for_offsetof
7716 && (cp_parser_non_integral_constant_expression
7717 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7718 postfix_expression = error_mark_node;
7720 return postfix_expression;
7723 /* Parse a parenthesized expression-list.
7725 expression-list:
7726 assignment-expression
7727 expression-list, assignment-expression
7729 attribute-list:
7730 expression-list
7731 identifier
7732 identifier, expression-list
7734 CAST_P is true if this expression is the target of a cast.
7736 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7737 argument pack.
7739 Returns a vector of trees. Each element is a representation of an
7740 assignment-expression. NULL is returned if the ( and or ) are
7741 missing. An empty, but allocated, vector is returned on no
7742 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7743 if we are parsing an attribute list for an attribute that wants a
7744 plain identifier argument, normal_attr for an attribute that wants
7745 an expression, or non_attr if we aren't parsing an attribute list. If
7746 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7747 not all of the expressions in the list were constant.
7748 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7749 will be written to with the location of the closing parenthesis. If
7750 an error occurs, it may or may not be written to. */
7752 static vec<tree, va_gc> *
7753 cp_parser_parenthesized_expression_list (cp_parser* parser,
7754 int is_attribute_list,
7755 bool cast_p,
7756 bool allow_expansion_p,
7757 bool *non_constant_p,
7758 location_t *close_paren_loc)
7760 vec<tree, va_gc> *expression_list;
7761 bool fold_expr_p = is_attribute_list != non_attr;
7762 tree identifier = NULL_TREE;
7763 bool saved_greater_than_is_operator_p;
7765 /* Assume all the expressions will be constant. */
7766 if (non_constant_p)
7767 *non_constant_p = false;
7769 matching_parens parens;
7770 if (!parens.require_open (parser))
7771 return NULL;
7773 expression_list = make_tree_vector ();
7775 /* Within a parenthesized expression, a `>' token is always
7776 the greater-than operator. */
7777 saved_greater_than_is_operator_p
7778 = parser->greater_than_is_operator_p;
7779 parser->greater_than_is_operator_p = true;
7781 /* Consume expressions until there are no more. */
7782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7783 while (true)
7785 tree expr;
7787 /* At the beginning of attribute lists, check to see if the
7788 next token is an identifier. */
7789 if (is_attribute_list == id_attr
7790 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7792 cp_token *token;
7794 /* Consume the identifier. */
7795 token = cp_lexer_consume_token (parser->lexer);
7796 /* Save the identifier. */
7797 identifier = token->u.value;
7799 else
7801 bool expr_non_constant_p;
7803 /* Parse the next assignment-expression. */
7804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7806 /* A braced-init-list. */
7807 cp_lexer_set_source_position (parser->lexer);
7808 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7809 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7810 if (non_constant_p && expr_non_constant_p)
7811 *non_constant_p = true;
7813 else if (non_constant_p)
7815 expr = (cp_parser_constant_expression
7816 (parser, /*allow_non_constant_p=*/true,
7817 &expr_non_constant_p));
7818 if (expr_non_constant_p)
7819 *non_constant_p = true;
7821 else
7822 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7823 cast_p);
7825 if (fold_expr_p)
7826 expr = instantiate_non_dependent_expr (expr);
7828 /* If we have an ellipsis, then this is an expression
7829 expansion. */
7830 if (allow_expansion_p
7831 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7833 /* Consume the `...'. */
7834 cp_lexer_consume_token (parser->lexer);
7836 /* Build the argument pack. */
7837 expr = make_pack_expansion (expr);
7840 /* Add it to the list. We add error_mark_node
7841 expressions to the list, so that we can still tell if
7842 the correct form for a parenthesized expression-list
7843 is found. That gives better errors. */
7844 vec_safe_push (expression_list, expr);
7846 if (expr == error_mark_node)
7847 goto skip_comma;
7850 /* After the first item, attribute lists look the same as
7851 expression lists. */
7852 is_attribute_list = non_attr;
7854 get_comma:;
7855 /* If the next token isn't a `,', then we are done. */
7856 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7857 break;
7859 /* Otherwise, consume the `,' and keep going. */
7860 cp_lexer_consume_token (parser->lexer);
7863 if (close_paren_loc)
7864 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7866 if (!parens.require_close (parser))
7868 int ending;
7870 skip_comma:;
7871 /* We try and resync to an unnested comma, as that will give the
7872 user better diagnostics. */
7873 ending = cp_parser_skip_to_closing_parenthesis (parser,
7874 /*recovering=*/true,
7875 /*or_comma=*/true,
7876 /*consume_paren=*/true);
7877 if (ending < 0)
7878 goto get_comma;
7879 if (!ending)
7881 parser->greater_than_is_operator_p
7882 = saved_greater_than_is_operator_p;
7883 return NULL;
7887 parser->greater_than_is_operator_p
7888 = saved_greater_than_is_operator_p;
7890 if (identifier)
7891 vec_safe_insert (expression_list, 0, identifier);
7893 return expression_list;
7896 /* Parse a pseudo-destructor-name.
7898 pseudo-destructor-name:
7899 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7900 :: [opt] nested-name-specifier template template-id :: ~ type-name
7901 :: [opt] nested-name-specifier [opt] ~ type-name
7903 If either of the first two productions is used, sets *SCOPE to the
7904 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7905 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7906 or ERROR_MARK_NODE if the parse fails. */
7908 static void
7909 cp_parser_pseudo_destructor_name (cp_parser* parser,
7910 tree object,
7911 tree* scope,
7912 tree* type)
7914 bool nested_name_specifier_p;
7916 /* Handle ~auto. */
7917 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7918 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7919 && !type_dependent_expression_p (object))
7921 if (cxx_dialect < cxx14)
7922 pedwarn (input_location, 0,
7923 "%<~auto%> only available with "
7924 "-std=c++14 or -std=gnu++14");
7925 cp_lexer_consume_token (parser->lexer);
7926 cp_lexer_consume_token (parser->lexer);
7927 *scope = NULL_TREE;
7928 *type = TREE_TYPE (object);
7929 return;
7932 /* Assume that things will not work out. */
7933 *type = error_mark_node;
7935 /* Look for the optional `::' operator. */
7936 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7937 /* Look for the optional nested-name-specifier. */
7938 nested_name_specifier_p
7939 = (cp_parser_nested_name_specifier_opt (parser,
7940 /*typename_keyword_p=*/false,
7941 /*check_dependency_p=*/true,
7942 /*type_p=*/false,
7943 /*is_declaration=*/false)
7944 != NULL_TREE);
7945 /* Now, if we saw a nested-name-specifier, we might be doing the
7946 second production. */
7947 if (nested_name_specifier_p
7948 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7950 /* Consume the `template' keyword. */
7951 cp_lexer_consume_token (parser->lexer);
7952 /* Parse the template-id. */
7953 cp_parser_template_id (parser,
7954 /*template_keyword_p=*/true,
7955 /*check_dependency_p=*/false,
7956 class_type,
7957 /*is_declaration=*/true);
7958 /* Look for the `::' token. */
7959 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7961 /* If the next token is not a `~', then there might be some
7962 additional qualification. */
7963 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7965 /* At this point, we're looking for "type-name :: ~". The type-name
7966 must not be a class-name, since this is a pseudo-destructor. So,
7967 it must be either an enum-name, or a typedef-name -- both of which
7968 are just identifiers. So, we peek ahead to check that the "::"
7969 and "~" tokens are present; if they are not, then we can avoid
7970 calling type_name. */
7971 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7972 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7973 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7975 cp_parser_error (parser, "non-scalar type");
7976 return;
7979 /* Look for the type-name. */
7980 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7981 if (*scope == error_mark_node)
7982 return;
7984 /* Look for the `::' token. */
7985 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7987 else
7988 *scope = NULL_TREE;
7990 /* Look for the `~'. */
7991 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7993 /* Once we see the ~, this has to be a pseudo-destructor. */
7994 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7995 cp_parser_commit_to_topmost_tentative_parse (parser);
7997 /* Look for the type-name again. We are not responsible for
7998 checking that it matches the first type-name. */
7999 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8002 /* Parse a unary-expression.
8004 unary-expression:
8005 postfix-expression
8006 ++ cast-expression
8007 -- cast-expression
8008 unary-operator cast-expression
8009 sizeof unary-expression
8010 sizeof ( type-id )
8011 alignof ( type-id ) [C++0x]
8012 new-expression
8013 delete-expression
8015 GNU Extensions:
8017 unary-expression:
8018 __extension__ cast-expression
8019 __alignof__ unary-expression
8020 __alignof__ ( type-id )
8021 alignof unary-expression [C++0x]
8022 __real__ cast-expression
8023 __imag__ cast-expression
8024 && identifier
8025 sizeof ( type-id ) { initializer-list , [opt] }
8026 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8027 __alignof__ ( type-id ) { initializer-list , [opt] }
8029 ADDRESS_P is true iff the unary-expression is appearing as the
8030 operand of the `&' operator. CAST_P is true if this expression is
8031 the target of a cast.
8033 Returns a representation of the expression. */
8035 static cp_expr
8036 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8037 bool address_p, bool cast_p, bool decltype_p)
8039 cp_token *token;
8040 enum tree_code unary_operator;
8042 /* Peek at the next token. */
8043 token = cp_lexer_peek_token (parser->lexer);
8044 /* Some keywords give away the kind of expression. */
8045 if (token->type == CPP_KEYWORD)
8047 enum rid keyword = token->keyword;
8049 switch (keyword)
8051 case RID_ALIGNOF:
8052 case RID_SIZEOF:
8054 tree operand, ret;
8055 enum tree_code op;
8056 location_t start_loc = token->location;
8058 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8059 /* Consume the token. */
8060 cp_lexer_consume_token (parser->lexer);
8061 /* Parse the operand. */
8062 operand = cp_parser_sizeof_operand (parser, keyword);
8064 if (TYPE_P (operand))
8065 ret = cxx_sizeof_or_alignof_type (operand, op, true);
8066 else
8068 /* ISO C++ defines alignof only with types, not with
8069 expressions. So pedwarn if alignof is used with a non-
8070 type expression. However, __alignof__ is ok. */
8071 if (id_equal (token->u.value, "alignof"))
8072 pedwarn (token->location, OPT_Wpedantic,
8073 "ISO C++ does not allow %<alignof%> "
8074 "with a non-type");
8076 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
8078 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8079 SIZEOF_EXPR with the original operand. */
8080 if (op == SIZEOF_EXPR && ret != error_mark_node)
8082 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8084 if (!processing_template_decl && TYPE_P (operand))
8086 ret = build_min (SIZEOF_EXPR, size_type_node,
8087 build1 (NOP_EXPR, operand,
8088 error_mark_node));
8089 SIZEOF_EXPR_TYPE_P (ret) = 1;
8091 else
8092 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8093 TREE_SIDE_EFFECTS (ret) = 0;
8094 TREE_READONLY (ret) = 1;
8098 /* Construct a location e.g. :
8099 alignof (expr)
8100 ^~~~~~~~~~~~~~
8101 with start == caret at the start of the "alignof"/"sizeof"
8102 token, with the endpoint at the final closing paren. */
8103 location_t finish_loc
8104 = cp_lexer_previous_token (parser->lexer)->location;
8105 location_t compound_loc
8106 = make_location (start_loc, start_loc, finish_loc);
8108 cp_expr ret_expr (ret);
8109 ret_expr.set_location (compound_loc);
8110 return ret_expr;
8113 case RID_NEW:
8114 return cp_parser_new_expression (parser);
8116 case RID_DELETE:
8117 return cp_parser_delete_expression (parser);
8119 case RID_EXTENSION:
8121 /* The saved value of the PEDANTIC flag. */
8122 int saved_pedantic;
8123 tree expr;
8125 /* Save away the PEDANTIC flag. */
8126 cp_parser_extension_opt (parser, &saved_pedantic);
8127 /* Parse the cast-expression. */
8128 expr = cp_parser_simple_cast_expression (parser);
8129 /* Restore the PEDANTIC flag. */
8130 pedantic = saved_pedantic;
8132 return expr;
8135 case RID_REALPART:
8136 case RID_IMAGPART:
8138 tree expression;
8140 /* Consume the `__real__' or `__imag__' token. */
8141 cp_lexer_consume_token (parser->lexer);
8142 /* Parse the cast-expression. */
8143 expression = cp_parser_simple_cast_expression (parser);
8144 /* Create the complete representation. */
8145 return build_x_unary_op (token->location,
8146 (keyword == RID_REALPART
8147 ? REALPART_EXPR : IMAGPART_EXPR),
8148 expression,
8149 tf_warning_or_error);
8151 break;
8153 case RID_TRANSACTION_ATOMIC:
8154 case RID_TRANSACTION_RELAXED:
8155 return cp_parser_transaction_expression (parser, keyword);
8157 case RID_NOEXCEPT:
8159 tree expr;
8160 const char *saved_message;
8161 bool saved_integral_constant_expression_p;
8162 bool saved_non_integral_constant_expression_p;
8163 bool saved_greater_than_is_operator_p;
8165 cp_lexer_consume_token (parser->lexer);
8166 matching_parens parens;
8167 parens.require_open (parser);
8169 saved_message = parser->type_definition_forbidden_message;
8170 parser->type_definition_forbidden_message
8171 = G_("types may not be defined in %<noexcept%> expressions");
8173 saved_integral_constant_expression_p
8174 = parser->integral_constant_expression_p;
8175 saved_non_integral_constant_expression_p
8176 = parser->non_integral_constant_expression_p;
8177 parser->integral_constant_expression_p = false;
8179 saved_greater_than_is_operator_p
8180 = parser->greater_than_is_operator_p;
8181 parser->greater_than_is_operator_p = true;
8183 ++cp_unevaluated_operand;
8184 ++c_inhibit_evaluation_warnings;
8185 ++cp_noexcept_operand;
8186 expr = cp_parser_expression (parser);
8187 --cp_noexcept_operand;
8188 --c_inhibit_evaluation_warnings;
8189 --cp_unevaluated_operand;
8191 parser->greater_than_is_operator_p
8192 = saved_greater_than_is_operator_p;
8194 parser->integral_constant_expression_p
8195 = saved_integral_constant_expression_p;
8196 parser->non_integral_constant_expression_p
8197 = saved_non_integral_constant_expression_p;
8199 parser->type_definition_forbidden_message = saved_message;
8201 parens.require_close (parser);
8202 return finish_noexcept_expr (expr, tf_warning_or_error);
8205 default:
8206 break;
8210 /* Look for the `:: new' and `:: delete', which also signal the
8211 beginning of a new-expression, or delete-expression,
8212 respectively. If the next token is `::', then it might be one of
8213 these. */
8214 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8216 enum rid keyword;
8218 /* See if the token after the `::' is one of the keywords in
8219 which we're interested. */
8220 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8221 /* If it's `new', we have a new-expression. */
8222 if (keyword == RID_NEW)
8223 return cp_parser_new_expression (parser);
8224 /* Similarly, for `delete'. */
8225 else if (keyword == RID_DELETE)
8226 return cp_parser_delete_expression (parser);
8229 /* Look for a unary operator. */
8230 unary_operator = cp_parser_unary_operator (token);
8231 /* The `++' and `--' operators can be handled similarly, even though
8232 they are not technically unary-operators in the grammar. */
8233 if (unary_operator == ERROR_MARK)
8235 if (token->type == CPP_PLUS_PLUS)
8236 unary_operator = PREINCREMENT_EXPR;
8237 else if (token->type == CPP_MINUS_MINUS)
8238 unary_operator = PREDECREMENT_EXPR;
8239 /* Handle the GNU address-of-label extension. */
8240 else if (cp_parser_allow_gnu_extensions_p (parser)
8241 && token->type == CPP_AND_AND)
8243 tree identifier;
8244 tree expression;
8245 location_t start_loc = token->location;
8247 /* Consume the '&&' token. */
8248 cp_lexer_consume_token (parser->lexer);
8249 /* Look for the identifier. */
8250 location_t finish_loc
8251 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8252 identifier = cp_parser_identifier (parser);
8253 /* Construct a location of the form:
8254 &&label
8255 ^~~~~~~
8256 with caret==start at the "&&", finish at the end of the label. */
8257 location_t combined_loc
8258 = make_location (start_loc, start_loc, finish_loc);
8259 /* Create an expression representing the address. */
8260 expression = finish_label_address_expr (identifier, combined_loc);
8261 if (cp_parser_non_integral_constant_expression (parser,
8262 NIC_ADDR_LABEL))
8263 expression = error_mark_node;
8264 return expression;
8267 if (unary_operator != ERROR_MARK)
8269 cp_expr cast_expression;
8270 cp_expr expression = error_mark_node;
8271 non_integral_constant non_constant_p = NIC_NONE;
8272 location_t loc = token->location;
8273 tsubst_flags_t complain = complain_flags (decltype_p);
8275 /* Consume the operator token. */
8276 token = cp_lexer_consume_token (parser->lexer);
8277 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8279 /* Parse the cast-expression. */
8280 cast_expression
8281 = cp_parser_cast_expression (parser,
8282 unary_operator == ADDR_EXPR,
8283 /*cast_p=*/false,
8284 /*decltype*/false,
8285 pidk);
8287 /* Make a location:
8288 OP_TOKEN CAST_EXPRESSION
8289 ^~~~~~~~~~~~~~~~~~~~~~~~~
8290 with start==caret at the operator token, and
8291 extending to the end of the cast_expression. */
8292 loc = make_location (loc, loc, cast_expression.get_finish ());
8294 /* Now, build an appropriate representation. */
8295 switch (unary_operator)
8297 case INDIRECT_REF:
8298 non_constant_p = NIC_STAR;
8299 expression = build_x_indirect_ref (loc, cast_expression,
8300 RO_UNARY_STAR,
8301 complain);
8302 /* TODO: build_x_indirect_ref does not always honor the
8303 location, so ensure it is set. */
8304 expression.set_location (loc);
8305 break;
8307 case ADDR_EXPR:
8308 non_constant_p = NIC_ADDR;
8309 /* Fall through. */
8310 case BIT_NOT_EXPR:
8311 expression = build_x_unary_op (loc, unary_operator,
8312 cast_expression,
8313 complain);
8314 /* TODO: build_x_unary_op does not always honor the location,
8315 so ensure it is set. */
8316 expression.set_location (loc);
8317 break;
8319 case PREINCREMENT_EXPR:
8320 case PREDECREMENT_EXPR:
8321 non_constant_p = unary_operator == PREINCREMENT_EXPR
8322 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8323 /* Fall through. */
8324 case NEGATE_EXPR:
8325 /* Immediately fold negation of a constant, unless the constant is 0
8326 (since -0 == 0) or it would overflow. */
8327 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8328 && CONSTANT_CLASS_P (cast_expression)
8329 && !integer_zerop (cast_expression)
8330 && !TREE_OVERFLOW (cast_expression))
8332 tree folded = fold_build1 (unary_operator,
8333 TREE_TYPE (cast_expression),
8334 cast_expression);
8335 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8337 expression = cp_expr (folded, loc);
8338 break;
8341 /* Fall through. */
8342 case UNARY_PLUS_EXPR:
8343 case TRUTH_NOT_EXPR:
8344 expression = finish_unary_op_expr (loc, unary_operator,
8345 cast_expression, complain);
8346 break;
8348 default:
8349 gcc_unreachable ();
8352 if (non_constant_p != NIC_NONE
8353 && cp_parser_non_integral_constant_expression (parser,
8354 non_constant_p))
8355 expression = error_mark_node;
8357 return expression;
8360 return cp_parser_postfix_expression (parser, address_p, cast_p,
8361 /*member_access_only_p=*/false,
8362 decltype_p,
8363 pidk);
8366 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8367 unary-operator, the corresponding tree code is returned. */
8369 static enum tree_code
8370 cp_parser_unary_operator (cp_token* token)
8372 switch (token->type)
8374 case CPP_MULT:
8375 return INDIRECT_REF;
8377 case CPP_AND:
8378 return ADDR_EXPR;
8380 case CPP_PLUS:
8381 return UNARY_PLUS_EXPR;
8383 case CPP_MINUS:
8384 return NEGATE_EXPR;
8386 case CPP_NOT:
8387 return TRUTH_NOT_EXPR;
8389 case CPP_COMPL:
8390 return BIT_NOT_EXPR;
8392 default:
8393 return ERROR_MARK;
8397 /* Parse a new-expression.
8399 new-expression:
8400 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8401 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8403 Returns a representation of the expression. */
8405 static tree
8406 cp_parser_new_expression (cp_parser* parser)
8408 bool global_scope_p;
8409 vec<tree, va_gc> *placement;
8410 tree type;
8411 vec<tree, va_gc> *initializer;
8412 tree nelts = NULL_TREE;
8413 tree ret;
8415 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8417 /* Look for the optional `::' operator. */
8418 global_scope_p
8419 = (cp_parser_global_scope_opt (parser,
8420 /*current_scope_valid_p=*/false)
8421 != NULL_TREE);
8422 /* Look for the `new' operator. */
8423 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8424 /* There's no easy way to tell a new-placement from the
8425 `( type-id )' construct. */
8426 cp_parser_parse_tentatively (parser);
8427 /* Look for a new-placement. */
8428 placement = cp_parser_new_placement (parser);
8429 /* If that didn't work out, there's no new-placement. */
8430 if (!cp_parser_parse_definitely (parser))
8432 if (placement != NULL)
8433 release_tree_vector (placement);
8434 placement = NULL;
8437 /* If the next token is a `(', then we have a parenthesized
8438 type-id. */
8439 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8441 cp_token *token;
8442 const char *saved_message = parser->type_definition_forbidden_message;
8444 /* Consume the `('. */
8445 matching_parens parens;
8446 parens.consume_open (parser);
8448 /* Parse the type-id. */
8449 parser->type_definition_forbidden_message
8450 = G_("types may not be defined in a new-expression");
8452 type_id_in_expr_sentinel s (parser);
8453 type = cp_parser_type_id (parser);
8455 parser->type_definition_forbidden_message = saved_message;
8457 /* Look for the closing `)'. */
8458 parens.require_close (parser);
8459 token = cp_lexer_peek_token (parser->lexer);
8460 /* There should not be a direct-new-declarator in this production,
8461 but GCC used to allowed this, so we check and emit a sensible error
8462 message for this case. */
8463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8465 error_at (token->location,
8466 "array bound forbidden after parenthesized type-id");
8467 inform (token->location,
8468 "try removing the parentheses around the type-id");
8469 cp_parser_direct_new_declarator (parser);
8472 /* Otherwise, there must be a new-type-id. */
8473 else
8474 type = cp_parser_new_type_id (parser, &nelts);
8476 /* If the next token is a `(' or '{', then we have a new-initializer. */
8477 cp_token *token = cp_lexer_peek_token (parser->lexer);
8478 if (token->type == CPP_OPEN_PAREN
8479 || token->type == CPP_OPEN_BRACE)
8480 initializer = cp_parser_new_initializer (parser);
8481 else
8482 initializer = NULL;
8484 /* A new-expression may not appear in an integral constant
8485 expression. */
8486 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8487 ret = error_mark_node;
8488 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8489 of a new-type-id or type-id of a new-expression, the new-expression shall
8490 contain a new-initializer of the form ( assignment-expression )".
8491 Additionally, consistently with the spirit of DR 1467, we want to accept
8492 'new auto { 2 }' too. */
8493 else if ((ret = type_uses_auto (type))
8494 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8495 && (vec_safe_length (initializer) != 1
8496 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8497 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8499 error_at (token->location,
8500 "initialization of new-expression for type %<auto%> "
8501 "requires exactly one element");
8502 ret = error_mark_node;
8504 else
8506 /* Construct a location e.g.:
8507 ptr = new int[100]
8508 ^~~~~~~~~~~~
8509 with caret == start at the start of the "new" token, and the end
8510 at the end of the final token we consumed. */
8511 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8512 location_t end_loc = get_finish (end_tok->location);
8513 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8515 /* Create a representation of the new-expression. */
8516 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8517 tf_warning_or_error);
8518 protected_set_expr_location (ret, combined_loc);
8521 if (placement != NULL)
8522 release_tree_vector (placement);
8523 if (initializer != NULL)
8524 release_tree_vector (initializer);
8526 return ret;
8529 /* Parse a new-placement.
8531 new-placement:
8532 ( expression-list )
8534 Returns the same representation as for an expression-list. */
8536 static vec<tree, va_gc> *
8537 cp_parser_new_placement (cp_parser* parser)
8539 vec<tree, va_gc> *expression_list;
8541 /* Parse the expression-list. */
8542 expression_list = (cp_parser_parenthesized_expression_list
8543 (parser, non_attr, /*cast_p=*/false,
8544 /*allow_expansion_p=*/true,
8545 /*non_constant_p=*/NULL));
8547 if (expression_list && expression_list->is_empty ())
8548 error ("expected expression-list or type-id");
8550 return expression_list;
8553 /* Parse a new-type-id.
8555 new-type-id:
8556 type-specifier-seq new-declarator [opt]
8558 Returns the TYPE allocated. If the new-type-id indicates an array
8559 type, *NELTS is set to the number of elements in the last array
8560 bound; the TYPE will not include the last array bound. */
8562 static tree
8563 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8565 cp_decl_specifier_seq type_specifier_seq;
8566 cp_declarator *new_declarator;
8567 cp_declarator *declarator;
8568 cp_declarator *outer_declarator;
8569 const char *saved_message;
8571 /* The type-specifier sequence must not contain type definitions.
8572 (It cannot contain declarations of new types either, but if they
8573 are not definitions we will catch that because they are not
8574 complete.) */
8575 saved_message = parser->type_definition_forbidden_message;
8576 parser->type_definition_forbidden_message
8577 = G_("types may not be defined in a new-type-id");
8578 /* Parse the type-specifier-seq. */
8579 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8580 /*is_trailing_return=*/false,
8581 &type_specifier_seq);
8582 /* Restore the old message. */
8583 parser->type_definition_forbidden_message = saved_message;
8585 if (type_specifier_seq.type == error_mark_node)
8586 return error_mark_node;
8588 /* Parse the new-declarator. */
8589 new_declarator = cp_parser_new_declarator_opt (parser);
8591 /* Determine the number of elements in the last array dimension, if
8592 any. */
8593 *nelts = NULL_TREE;
8594 /* Skip down to the last array dimension. */
8595 declarator = new_declarator;
8596 outer_declarator = NULL;
8597 while (declarator && (declarator->kind == cdk_pointer
8598 || declarator->kind == cdk_ptrmem))
8600 outer_declarator = declarator;
8601 declarator = declarator->declarator;
8603 while (declarator
8604 && declarator->kind == cdk_array
8605 && declarator->declarator
8606 && declarator->declarator->kind == cdk_array)
8608 outer_declarator = declarator;
8609 declarator = declarator->declarator;
8612 if (declarator && declarator->kind == cdk_array)
8614 *nelts = declarator->u.array.bounds;
8615 if (*nelts == error_mark_node)
8616 *nelts = integer_one_node;
8618 if (outer_declarator)
8619 outer_declarator->declarator = declarator->declarator;
8620 else
8621 new_declarator = NULL;
8624 return groktypename (&type_specifier_seq, new_declarator, false);
8627 /* Parse an (optional) new-declarator.
8629 new-declarator:
8630 ptr-operator new-declarator [opt]
8631 direct-new-declarator
8633 Returns the declarator. */
8635 static cp_declarator *
8636 cp_parser_new_declarator_opt (cp_parser* parser)
8638 enum tree_code code;
8639 tree type, std_attributes = NULL_TREE;
8640 cp_cv_quals cv_quals;
8642 /* We don't know if there's a ptr-operator next, or not. */
8643 cp_parser_parse_tentatively (parser);
8644 /* Look for a ptr-operator. */
8645 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8646 /* If that worked, look for more new-declarators. */
8647 if (cp_parser_parse_definitely (parser))
8649 cp_declarator *declarator;
8651 /* Parse another optional declarator. */
8652 declarator = cp_parser_new_declarator_opt (parser);
8654 declarator = cp_parser_make_indirect_declarator
8655 (code, type, cv_quals, declarator, std_attributes);
8657 return declarator;
8660 /* If the next token is a `[', there is a direct-new-declarator. */
8661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8662 return cp_parser_direct_new_declarator (parser);
8664 return NULL;
8667 /* Parse a direct-new-declarator.
8669 direct-new-declarator:
8670 [ expression ]
8671 direct-new-declarator [constant-expression]
8675 static cp_declarator *
8676 cp_parser_direct_new_declarator (cp_parser* parser)
8678 cp_declarator *declarator = NULL;
8680 while (true)
8682 tree expression;
8683 cp_token *token;
8685 /* Look for the opening `['. */
8686 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8688 token = cp_lexer_peek_token (parser->lexer);
8689 expression = cp_parser_expression (parser);
8690 /* The standard requires that the expression have integral
8691 type. DR 74 adds enumeration types. We believe that the
8692 real intent is that these expressions be handled like the
8693 expression in a `switch' condition, which also allows
8694 classes with a single conversion to integral or
8695 enumeration type. */
8696 if (!processing_template_decl)
8698 expression
8699 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8700 expression,
8701 /*complain=*/true);
8702 if (!expression)
8704 error_at (token->location,
8705 "expression in new-declarator must have integral "
8706 "or enumeration type");
8707 expression = error_mark_node;
8711 /* Look for the closing `]'. */
8712 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8714 /* Add this bound to the declarator. */
8715 declarator = make_array_declarator (declarator, expression);
8717 /* If the next token is not a `[', then there are no more
8718 bounds. */
8719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8720 break;
8723 return declarator;
8726 /* Parse a new-initializer.
8728 new-initializer:
8729 ( expression-list [opt] )
8730 braced-init-list
8732 Returns a representation of the expression-list. */
8734 static vec<tree, va_gc> *
8735 cp_parser_new_initializer (cp_parser* parser)
8737 vec<tree, va_gc> *expression_list;
8739 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8741 tree t;
8742 bool expr_non_constant_p;
8743 cp_lexer_set_source_position (parser->lexer);
8744 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8745 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8746 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8747 expression_list = make_tree_vector_single (t);
8749 else
8750 expression_list = (cp_parser_parenthesized_expression_list
8751 (parser, non_attr, /*cast_p=*/false,
8752 /*allow_expansion_p=*/true,
8753 /*non_constant_p=*/NULL));
8755 return expression_list;
8758 /* Parse a delete-expression.
8760 delete-expression:
8761 :: [opt] delete cast-expression
8762 :: [opt] delete [ ] cast-expression
8764 Returns a representation of the expression. */
8766 static tree
8767 cp_parser_delete_expression (cp_parser* parser)
8769 bool global_scope_p;
8770 bool array_p;
8771 tree expression;
8773 /* Look for the optional `::' operator. */
8774 global_scope_p
8775 = (cp_parser_global_scope_opt (parser,
8776 /*current_scope_valid_p=*/false)
8777 != NULL_TREE);
8778 /* Look for the `delete' keyword. */
8779 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8780 /* See if the array syntax is in use. */
8781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8783 /* Consume the `[' token. */
8784 cp_lexer_consume_token (parser->lexer);
8785 /* Look for the `]' token. */
8786 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8787 /* Remember that this is the `[]' construct. */
8788 array_p = true;
8790 else
8791 array_p = false;
8793 /* Parse the cast-expression. */
8794 expression = cp_parser_simple_cast_expression (parser);
8796 /* A delete-expression may not appear in an integral constant
8797 expression. */
8798 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8799 return error_mark_node;
8801 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8802 tf_warning_or_error);
8805 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8806 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8807 0 otherwise. */
8809 static int
8810 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8812 cp_token *token = cp_lexer_peek_token (parser->lexer);
8813 switch (token->type)
8815 case CPP_COMMA:
8816 case CPP_SEMICOLON:
8817 case CPP_QUERY:
8818 case CPP_COLON:
8819 case CPP_CLOSE_SQUARE:
8820 case CPP_CLOSE_PAREN:
8821 case CPP_CLOSE_BRACE:
8822 case CPP_OPEN_BRACE:
8823 case CPP_DOT:
8824 case CPP_DOT_STAR:
8825 case CPP_DEREF:
8826 case CPP_DEREF_STAR:
8827 case CPP_DIV:
8828 case CPP_MOD:
8829 case CPP_LSHIFT:
8830 case CPP_RSHIFT:
8831 case CPP_LESS:
8832 case CPP_GREATER:
8833 case CPP_LESS_EQ:
8834 case CPP_GREATER_EQ:
8835 case CPP_EQ_EQ:
8836 case CPP_NOT_EQ:
8837 case CPP_EQ:
8838 case CPP_MULT_EQ:
8839 case CPP_DIV_EQ:
8840 case CPP_MOD_EQ:
8841 case CPP_PLUS_EQ:
8842 case CPP_MINUS_EQ:
8843 case CPP_RSHIFT_EQ:
8844 case CPP_LSHIFT_EQ:
8845 case CPP_AND_EQ:
8846 case CPP_XOR_EQ:
8847 case CPP_OR_EQ:
8848 case CPP_XOR:
8849 case CPP_OR:
8850 case CPP_OR_OR:
8851 case CPP_EOF:
8852 case CPP_ELLIPSIS:
8853 return 0;
8855 case CPP_OPEN_PAREN:
8856 /* In ((type ()) () the last () isn't a valid cast-expression,
8857 so the whole must be parsed as postfix-expression. */
8858 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8859 != CPP_CLOSE_PAREN;
8861 case CPP_OPEN_SQUARE:
8862 /* '[' may start a primary-expression in obj-c++ and in C++11,
8863 as a lambda-expression, eg, '(void)[]{}'. */
8864 if (cxx_dialect >= cxx11)
8865 return -1;
8866 return c_dialect_objc ();
8868 case CPP_PLUS_PLUS:
8869 case CPP_MINUS_MINUS:
8870 /* '++' and '--' may or may not start a cast-expression:
8872 struct T { void operator++(int); };
8873 void f() { (T())++; }
8877 int a;
8878 (int)++a; */
8879 return -1;
8881 default:
8882 return 1;
8886 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8887 in the order: const_cast, static_cast, reinterpret_cast.
8889 Don't suggest dynamic_cast.
8891 Return the first legal cast kind found, or NULL otherwise. */
8893 static const char *
8894 get_cast_suggestion (tree dst_type, tree orig_expr)
8896 tree trial;
8898 /* Reuse the parser logic by attempting to build the various kinds of
8899 cast, with "complain" disabled.
8900 Identify the first such cast that is valid. */
8902 /* Don't attempt to run such logic within template processing. */
8903 if (processing_template_decl)
8904 return NULL;
8906 /* First try const_cast. */
8907 trial = build_const_cast (dst_type, orig_expr, tf_none);
8908 if (trial != error_mark_node)
8909 return "const_cast";
8911 /* If that fails, try static_cast. */
8912 trial = build_static_cast (dst_type, orig_expr, tf_none);
8913 if (trial != error_mark_node)
8914 return "static_cast";
8916 /* Finally, try reinterpret_cast. */
8917 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8918 if (trial != error_mark_node)
8919 return "reinterpret_cast";
8921 /* No such cast possible. */
8922 return NULL;
8925 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8926 suggesting how to convert a C-style cast of the form:
8928 (DST_TYPE)ORIG_EXPR
8930 to a C++-style cast.
8932 The primary range of RICHLOC is asssumed to be that of the original
8933 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8934 of the parens in the C-style cast. */
8936 static void
8937 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8938 location_t close_paren_loc, tree orig_expr,
8939 tree dst_type)
8941 /* This function is non-trivial, so bail out now if the warning isn't
8942 going to be emitted. */
8943 if (!warn_old_style_cast)
8944 return;
8946 /* Try to find a legal C++ cast, trying them in order:
8947 const_cast, static_cast, reinterpret_cast. */
8948 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8949 if (!cast_suggestion)
8950 return;
8952 /* Replace the open paren with "CAST_SUGGESTION<". */
8953 pretty_printer pp;
8954 pp_printf (&pp, "%s<", cast_suggestion);
8955 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8957 /* Replace the close paren with "> (". */
8958 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8960 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8961 rich_loc->add_fixit_insert_after (")");
8965 /* Parse a cast-expression.
8967 cast-expression:
8968 unary-expression
8969 ( type-id ) cast-expression
8971 ADDRESS_P is true iff the unary-expression is appearing as the
8972 operand of the `&' operator. CAST_P is true if this expression is
8973 the target of a cast.
8975 Returns a representation of the expression. */
8977 static cp_expr
8978 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8979 bool decltype_p, cp_id_kind * pidk)
8981 /* If it's a `(', then we might be looking at a cast. */
8982 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8984 tree type = NULL_TREE;
8985 cp_expr expr (NULL_TREE);
8986 int cast_expression = 0;
8987 const char *saved_message;
8989 /* There's no way to know yet whether or not this is a cast.
8990 For example, `(int (3))' is a unary-expression, while `(int)
8991 3' is a cast. So, we resort to parsing tentatively. */
8992 cp_parser_parse_tentatively (parser);
8993 /* Types may not be defined in a cast. */
8994 saved_message = parser->type_definition_forbidden_message;
8995 parser->type_definition_forbidden_message
8996 = G_("types may not be defined in casts");
8997 /* Consume the `('. */
8998 matching_parens parens;
8999 cp_token *open_paren = parens.consume_open (parser);
9000 location_t open_paren_loc = open_paren->location;
9001 location_t close_paren_loc = UNKNOWN_LOCATION;
9003 /* A very tricky bit is that `(struct S) { 3 }' is a
9004 compound-literal (which we permit in C++ as an extension).
9005 But, that construct is not a cast-expression -- it is a
9006 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9007 is legal; if the compound-literal were a cast-expression,
9008 you'd need an extra set of parentheses.) But, if we parse
9009 the type-id, and it happens to be a class-specifier, then we
9010 will commit to the parse at that point, because we cannot
9011 undo the action that is done when creating a new class. So,
9012 then we cannot back up and do a postfix-expression.
9014 Another tricky case is the following (c++/29234):
9016 struct S { void operator () (); };
9018 void foo ()
9020 ( S()() );
9023 As a type-id we parse the parenthesized S()() as a function
9024 returning a function, groktypename complains and we cannot
9025 back up in this case either.
9027 Therefore, we scan ahead to the closing `)', and check to see
9028 if the tokens after the `)' can start a cast-expression. Otherwise
9029 we are dealing with an unary-expression, a postfix-expression
9030 or something else.
9032 Yet another tricky case, in C++11, is the following (c++/54891):
9034 (void)[]{};
9036 The issue is that usually, besides the case of lambda-expressions,
9037 the parenthesized type-id cannot be followed by '[', and, eg, we
9038 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9039 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9040 we don't commit, we try a cast-expression, then an unary-expression.
9042 Save tokens so that we can put them back. */
9043 cp_lexer_save_tokens (parser->lexer);
9045 /* We may be looking at a cast-expression. */
9046 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9047 /*consume_paren=*/true))
9048 cast_expression
9049 = cp_parser_tokens_start_cast_expression (parser);
9051 /* Roll back the tokens we skipped. */
9052 cp_lexer_rollback_tokens (parser->lexer);
9053 /* If we aren't looking at a cast-expression, simulate an error so
9054 that the call to cp_parser_error_occurred below returns true. */
9055 if (!cast_expression)
9056 cp_parser_simulate_error (parser);
9057 else
9059 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9060 parser->in_type_id_in_expr_p = true;
9061 /* Look for the type-id. */
9062 type = cp_parser_type_id (parser);
9063 /* Look for the closing `)'. */
9064 cp_token *close_paren = parens.require_close (parser);
9065 if (close_paren)
9066 close_paren_loc = close_paren->location;
9067 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9070 /* Restore the saved message. */
9071 parser->type_definition_forbidden_message = saved_message;
9073 /* At this point this can only be either a cast or a
9074 parenthesized ctor such as `(T ())' that looks like a cast to
9075 function returning T. */
9076 if (!cp_parser_error_occurred (parser))
9078 /* Only commit if the cast-expression doesn't start with
9079 '++', '--', or '[' in C++11. */
9080 if (cast_expression > 0)
9081 cp_parser_commit_to_topmost_tentative_parse (parser);
9083 expr = cp_parser_cast_expression (parser,
9084 /*address_p=*/false,
9085 /*cast_p=*/true,
9086 /*decltype_p=*/false,
9087 pidk);
9089 if (cp_parser_parse_definitely (parser))
9091 /* Warn about old-style casts, if so requested. */
9092 if (warn_old_style_cast
9093 && !in_system_header_at (input_location)
9094 && !VOID_TYPE_P (type)
9095 && current_lang_name != lang_name_c)
9097 gcc_rich_location rich_loc (input_location);
9098 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9099 expr, type);
9100 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
9101 "use of old-style cast to %q#T", type);
9104 /* Only type conversions to integral or enumeration types
9105 can be used in constant-expressions. */
9106 if (!cast_valid_in_integral_constant_expression_p (type)
9107 && cp_parser_non_integral_constant_expression (parser,
9108 NIC_CAST))
9109 return error_mark_node;
9111 /* Perform the cast. */
9112 /* Make a location:
9113 (TYPE) EXPR
9114 ^~~~~~~~~~~
9115 with start==caret at the open paren, extending to the
9116 end of "expr". */
9117 location_t cast_loc = make_location (open_paren_loc,
9118 open_paren_loc,
9119 expr.get_finish ());
9120 expr = build_c_cast (cast_loc, type, expr);
9121 return expr;
9124 else
9125 cp_parser_abort_tentative_parse (parser);
9128 /* If we get here, then it's not a cast, so it must be a
9129 unary-expression. */
9130 return cp_parser_unary_expression (parser, pidk, address_p,
9131 cast_p, decltype_p);
9134 /* Parse a binary expression of the general form:
9136 pm-expression:
9137 cast-expression
9138 pm-expression .* cast-expression
9139 pm-expression ->* cast-expression
9141 multiplicative-expression:
9142 pm-expression
9143 multiplicative-expression * pm-expression
9144 multiplicative-expression / pm-expression
9145 multiplicative-expression % pm-expression
9147 additive-expression:
9148 multiplicative-expression
9149 additive-expression + multiplicative-expression
9150 additive-expression - multiplicative-expression
9152 shift-expression:
9153 additive-expression
9154 shift-expression << additive-expression
9155 shift-expression >> additive-expression
9157 relational-expression:
9158 shift-expression
9159 relational-expression < shift-expression
9160 relational-expression > shift-expression
9161 relational-expression <= shift-expression
9162 relational-expression >= shift-expression
9164 GNU Extension:
9166 relational-expression:
9167 relational-expression <? shift-expression
9168 relational-expression >? shift-expression
9170 equality-expression:
9171 relational-expression
9172 equality-expression == relational-expression
9173 equality-expression != relational-expression
9175 and-expression:
9176 equality-expression
9177 and-expression & equality-expression
9179 exclusive-or-expression:
9180 and-expression
9181 exclusive-or-expression ^ and-expression
9183 inclusive-or-expression:
9184 exclusive-or-expression
9185 inclusive-or-expression | exclusive-or-expression
9187 logical-and-expression:
9188 inclusive-or-expression
9189 logical-and-expression && inclusive-or-expression
9191 logical-or-expression:
9192 logical-and-expression
9193 logical-or-expression || logical-and-expression
9195 All these are implemented with a single function like:
9197 binary-expression:
9198 simple-cast-expression
9199 binary-expression <token> binary-expression
9201 CAST_P is true if this expression is the target of a cast.
9203 The binops_by_token map is used to get the tree codes for each <token> type.
9204 binary-expressions are associated according to a precedence table. */
9206 #define TOKEN_PRECEDENCE(token) \
9207 (((token->type == CPP_GREATER \
9208 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9209 && !parser->greater_than_is_operator_p) \
9210 ? PREC_NOT_OPERATOR \
9211 : binops_by_token[token->type].prec)
9213 static cp_expr
9214 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9215 bool no_toplevel_fold_p,
9216 bool decltype_p,
9217 enum cp_parser_prec prec,
9218 cp_id_kind * pidk)
9220 cp_parser_expression_stack stack;
9221 cp_parser_expression_stack_entry *sp = &stack[0];
9222 cp_parser_expression_stack_entry current;
9223 cp_expr rhs;
9224 cp_token *token;
9225 enum tree_code rhs_type;
9226 enum cp_parser_prec new_prec, lookahead_prec;
9227 tree overload;
9229 /* Parse the first expression. */
9230 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9231 ? TRUTH_NOT_EXPR : ERROR_MARK);
9232 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9233 cast_p, decltype_p, pidk);
9234 current.prec = prec;
9236 if (cp_parser_error_occurred (parser))
9237 return error_mark_node;
9239 for (;;)
9241 /* Get an operator token. */
9242 token = cp_lexer_peek_token (parser->lexer);
9244 if (warn_cxx11_compat
9245 && token->type == CPP_RSHIFT
9246 && !parser->greater_than_is_operator_p)
9248 if (warning_at (token->location, OPT_Wc__11_compat,
9249 "%<>>%> operator is treated"
9250 " as two right angle brackets in C++11"))
9251 inform (token->location,
9252 "suggest parentheses around %<>>%> expression");
9255 new_prec = TOKEN_PRECEDENCE (token);
9256 if (new_prec != PREC_NOT_OPERATOR
9257 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9258 /* This is a fold-expression; handle it later. */
9259 new_prec = PREC_NOT_OPERATOR;
9261 /* Popping an entry off the stack means we completed a subexpression:
9262 - either we found a token which is not an operator (`>' where it is not
9263 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9264 will happen repeatedly;
9265 - or, we found an operator which has lower priority. This is the case
9266 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9267 parsing `3 * 4'. */
9268 if (new_prec <= current.prec)
9270 if (sp == stack)
9271 break;
9272 else
9273 goto pop;
9276 get_rhs:
9277 current.tree_type = binops_by_token[token->type].tree_type;
9278 current.loc = token->location;
9280 /* We used the operator token. */
9281 cp_lexer_consume_token (parser->lexer);
9283 /* For "false && x" or "true || x", x will never be executed;
9284 disable warnings while evaluating it. */
9285 if (current.tree_type == TRUTH_ANDIF_EXPR)
9286 c_inhibit_evaluation_warnings +=
9287 cp_fully_fold (current.lhs) == truthvalue_false_node;
9288 else if (current.tree_type == TRUTH_ORIF_EXPR)
9289 c_inhibit_evaluation_warnings +=
9290 cp_fully_fold (current.lhs) == truthvalue_true_node;
9292 /* Extract another operand. It may be the RHS of this expression
9293 or the LHS of a new, higher priority expression. */
9294 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9295 ? TRUTH_NOT_EXPR : ERROR_MARK);
9296 rhs = cp_parser_simple_cast_expression (parser);
9298 /* Get another operator token. Look up its precedence to avoid
9299 building a useless (immediately popped) stack entry for common
9300 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9301 token = cp_lexer_peek_token (parser->lexer);
9302 lookahead_prec = TOKEN_PRECEDENCE (token);
9303 if (lookahead_prec != PREC_NOT_OPERATOR
9304 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9305 lookahead_prec = PREC_NOT_OPERATOR;
9306 if (lookahead_prec > new_prec)
9308 /* ... and prepare to parse the RHS of the new, higher priority
9309 expression. Since precedence levels on the stack are
9310 monotonically increasing, we do not have to care about
9311 stack overflows. */
9312 *sp = current;
9313 ++sp;
9314 current.lhs = rhs;
9315 current.lhs_type = rhs_type;
9316 current.prec = new_prec;
9317 new_prec = lookahead_prec;
9318 goto get_rhs;
9320 pop:
9321 lookahead_prec = new_prec;
9322 /* If the stack is not empty, we have parsed into LHS the right side
9323 (`4' in the example above) of an expression we had suspended.
9324 We can use the information on the stack to recover the LHS (`3')
9325 from the stack together with the tree code (`MULT_EXPR'), and
9326 the precedence of the higher level subexpression
9327 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9328 which will be used to actually build the additive expression. */
9329 rhs = current.lhs;
9330 rhs_type = current.lhs_type;
9331 --sp;
9332 current = *sp;
9335 /* Undo the disabling of warnings done above. */
9336 if (current.tree_type == TRUTH_ANDIF_EXPR)
9337 c_inhibit_evaluation_warnings -=
9338 cp_fully_fold (current.lhs) == truthvalue_false_node;
9339 else if (current.tree_type == TRUTH_ORIF_EXPR)
9340 c_inhibit_evaluation_warnings -=
9341 cp_fully_fold (current.lhs) == truthvalue_true_node;
9343 if (warn_logical_not_paren
9344 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9345 && current.lhs_type == TRUTH_NOT_EXPR
9346 /* Avoid warning for !!x == y. */
9347 && (TREE_CODE (current.lhs) != NE_EXPR
9348 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9349 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9350 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9351 /* Avoid warning for !b == y where b is boolean. */
9352 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9353 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9354 != BOOLEAN_TYPE))))
9355 /* Avoid warning for !!b == y where b is boolean. */
9356 && (!DECL_P (current.lhs)
9357 || TREE_TYPE (current.lhs) == NULL_TREE
9358 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9359 warn_logical_not_parentheses (current.loc, current.tree_type,
9360 current.lhs, maybe_constant_value (rhs));
9362 overload = NULL;
9364 location_t combined_loc = make_location (current.loc,
9365 current.lhs.get_start (),
9366 rhs.get_finish ());
9368 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9369 ERROR_MARK for everything that is not a binary expression.
9370 This makes warn_about_parentheses miss some warnings that
9371 involve unary operators. For unary expressions we should
9372 pass the correct tree_code unless the unary expression was
9373 surrounded by parentheses.
9375 if (no_toplevel_fold_p
9376 && lookahead_prec <= current.prec
9377 && sp == stack)
9378 current.lhs = build2_loc (combined_loc,
9379 current.tree_type,
9380 TREE_CODE_CLASS (current.tree_type)
9381 == tcc_comparison
9382 ? boolean_type_node : TREE_TYPE (current.lhs),
9383 current.lhs, rhs);
9384 else
9386 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9387 current.lhs, current.lhs_type,
9388 rhs, rhs_type, &overload,
9389 complain_flags (decltype_p));
9390 /* TODO: build_x_binary_op doesn't always honor the location. */
9391 current.lhs.set_location (combined_loc);
9393 current.lhs_type = current.tree_type;
9395 /* If the binary operator required the use of an overloaded operator,
9396 then this expression cannot be an integral constant-expression.
9397 An overloaded operator can be used even if both operands are
9398 otherwise permissible in an integral constant-expression if at
9399 least one of the operands is of enumeration type. */
9401 if (overload
9402 && cp_parser_non_integral_constant_expression (parser,
9403 NIC_OVERLOADED))
9404 return error_mark_node;
9407 return current.lhs;
9410 static cp_expr
9411 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9412 bool no_toplevel_fold_p,
9413 enum cp_parser_prec prec,
9414 cp_id_kind * pidk)
9416 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9417 /*decltype*/false, prec, pidk);
9420 /* Parse the `? expression : assignment-expression' part of a
9421 conditional-expression. The LOGICAL_OR_EXPR is the
9422 logical-or-expression that started the conditional-expression.
9423 Returns a representation of the entire conditional-expression.
9425 This routine is used by cp_parser_assignment_expression.
9427 ? expression : assignment-expression
9429 GNU Extensions:
9431 ? : assignment-expression */
9433 static tree
9434 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9436 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9437 cp_expr assignment_expr;
9438 struct cp_token *token;
9439 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9441 /* Consume the `?' token. */
9442 cp_lexer_consume_token (parser->lexer);
9443 token = cp_lexer_peek_token (parser->lexer);
9444 if (cp_parser_allow_gnu_extensions_p (parser)
9445 && token->type == CPP_COLON)
9447 pedwarn (token->location, OPT_Wpedantic,
9448 "ISO C++ does not allow ?: with omitted middle operand");
9449 /* Implicit true clause. */
9450 expr = NULL_TREE;
9451 c_inhibit_evaluation_warnings +=
9452 folded_logical_or_expr == truthvalue_true_node;
9453 warn_for_omitted_condop (token->location, logical_or_expr);
9455 else
9457 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9458 parser->colon_corrects_to_scope_p = false;
9459 /* Parse the expression. */
9460 c_inhibit_evaluation_warnings +=
9461 folded_logical_or_expr == truthvalue_false_node;
9462 expr = cp_parser_expression (parser);
9463 c_inhibit_evaluation_warnings +=
9464 ((folded_logical_or_expr == truthvalue_true_node)
9465 - (folded_logical_or_expr == truthvalue_false_node));
9466 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9469 /* The next token should be a `:'. */
9470 cp_parser_require (parser, CPP_COLON, RT_COLON);
9471 /* Parse the assignment-expression. */
9472 assignment_expr = cp_parser_assignment_expression (parser);
9473 c_inhibit_evaluation_warnings -=
9474 folded_logical_or_expr == truthvalue_true_node;
9476 /* Make a location:
9477 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9478 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9479 with the caret at the "?", ranging from the start of
9480 the logical_or_expr to the end of the assignment_expr. */
9481 loc = make_location (loc,
9482 logical_or_expr.get_start (),
9483 assignment_expr.get_finish ());
9485 /* Build the conditional-expression. */
9486 return build_x_conditional_expr (loc, logical_or_expr,
9487 expr,
9488 assignment_expr,
9489 tf_warning_or_error);
9492 /* Parse an assignment-expression.
9494 assignment-expression:
9495 conditional-expression
9496 logical-or-expression assignment-operator assignment_expression
9497 throw-expression
9499 CAST_P is true if this expression is the target of a cast.
9500 DECLTYPE_P is true if this expression is the operand of decltype.
9502 Returns a representation for the expression. */
9504 static cp_expr
9505 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9506 bool cast_p, bool decltype_p)
9508 cp_expr expr;
9510 /* If the next token is the `throw' keyword, then we're looking at
9511 a throw-expression. */
9512 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9513 expr = cp_parser_throw_expression (parser);
9514 /* Otherwise, it must be that we are looking at a
9515 logical-or-expression. */
9516 else
9518 /* Parse the binary expressions (logical-or-expression). */
9519 expr = cp_parser_binary_expression (parser, cast_p, false,
9520 decltype_p,
9521 PREC_NOT_OPERATOR, pidk);
9522 /* If the next token is a `?' then we're actually looking at a
9523 conditional-expression. */
9524 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9525 return cp_parser_question_colon_clause (parser, expr);
9526 else
9528 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9530 /* If it's an assignment-operator, we're using the second
9531 production. */
9532 enum tree_code assignment_operator
9533 = cp_parser_assignment_operator_opt (parser);
9534 if (assignment_operator != ERROR_MARK)
9536 bool non_constant_p;
9538 /* Parse the right-hand side of the assignment. */
9539 cp_expr rhs = cp_parser_initializer_clause (parser,
9540 &non_constant_p);
9542 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9543 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9545 /* An assignment may not appear in a
9546 constant-expression. */
9547 if (cp_parser_non_integral_constant_expression (parser,
9548 NIC_ASSIGNMENT))
9549 return error_mark_node;
9550 /* Build the assignment expression. Its default
9551 location:
9552 LHS = RHS
9553 ~~~~^~~~~
9554 is the location of the '=' token as the
9555 caret, ranging from the start of the lhs to the
9556 end of the rhs. */
9557 loc = make_location (loc,
9558 expr.get_start (),
9559 rhs.get_finish ());
9560 expr = build_x_modify_expr (loc, expr,
9561 assignment_operator,
9562 rhs,
9563 complain_flags (decltype_p));
9564 /* TODO: build_x_modify_expr doesn't honor the location,
9565 so we must set it here. */
9566 expr.set_location (loc);
9571 return expr;
9574 /* Parse an (optional) assignment-operator.
9576 assignment-operator: one of
9577 = *= /= %= += -= >>= <<= &= ^= |=
9579 GNU Extension:
9581 assignment-operator: one of
9582 <?= >?=
9584 If the next token is an assignment operator, the corresponding tree
9585 code is returned, and the token is consumed. For example, for
9586 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9587 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9588 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9589 operator, ERROR_MARK is returned. */
9591 static enum tree_code
9592 cp_parser_assignment_operator_opt (cp_parser* parser)
9594 enum tree_code op;
9595 cp_token *token;
9597 /* Peek at the next token. */
9598 token = cp_lexer_peek_token (parser->lexer);
9600 switch (token->type)
9602 case CPP_EQ:
9603 op = NOP_EXPR;
9604 break;
9606 case CPP_MULT_EQ:
9607 op = MULT_EXPR;
9608 break;
9610 case CPP_DIV_EQ:
9611 op = TRUNC_DIV_EXPR;
9612 break;
9614 case CPP_MOD_EQ:
9615 op = TRUNC_MOD_EXPR;
9616 break;
9618 case CPP_PLUS_EQ:
9619 op = PLUS_EXPR;
9620 break;
9622 case CPP_MINUS_EQ:
9623 op = MINUS_EXPR;
9624 break;
9626 case CPP_RSHIFT_EQ:
9627 op = RSHIFT_EXPR;
9628 break;
9630 case CPP_LSHIFT_EQ:
9631 op = LSHIFT_EXPR;
9632 break;
9634 case CPP_AND_EQ:
9635 op = BIT_AND_EXPR;
9636 break;
9638 case CPP_XOR_EQ:
9639 op = BIT_XOR_EXPR;
9640 break;
9642 case CPP_OR_EQ:
9643 op = BIT_IOR_EXPR;
9644 break;
9646 default:
9647 /* Nothing else is an assignment operator. */
9648 op = ERROR_MARK;
9651 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9652 if (op != ERROR_MARK
9653 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9654 op = ERROR_MARK;
9656 /* If it was an assignment operator, consume it. */
9657 if (op != ERROR_MARK)
9658 cp_lexer_consume_token (parser->lexer);
9660 return op;
9663 /* Parse an expression.
9665 expression:
9666 assignment-expression
9667 expression , assignment-expression
9669 CAST_P is true if this expression is the target of a cast.
9670 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9671 except possibly parenthesized or on the RHS of a comma (N3276).
9673 Returns a representation of the expression. */
9675 static cp_expr
9676 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9677 bool cast_p, bool decltype_p)
9679 cp_expr expression = NULL_TREE;
9680 location_t loc = UNKNOWN_LOCATION;
9682 while (true)
9684 cp_expr assignment_expression;
9686 /* Parse the next assignment-expression. */
9687 assignment_expression
9688 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9690 /* We don't create a temporary for a call that is the immediate operand
9691 of decltype or on the RHS of a comma. But when we see a comma, we
9692 need to create a temporary for a call on the LHS. */
9693 if (decltype_p && !processing_template_decl
9694 && TREE_CODE (assignment_expression) == CALL_EXPR
9695 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9696 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9697 assignment_expression
9698 = build_cplus_new (TREE_TYPE (assignment_expression),
9699 assignment_expression, tf_warning_or_error);
9701 /* If this is the first assignment-expression, we can just
9702 save it away. */
9703 if (!expression)
9704 expression = assignment_expression;
9705 else
9707 /* Create a location with caret at the comma, ranging
9708 from the start of the LHS to the end of the RHS. */
9709 loc = make_location (loc,
9710 expression.get_start (),
9711 assignment_expression.get_finish ());
9712 expression = build_x_compound_expr (loc, expression,
9713 assignment_expression,
9714 complain_flags (decltype_p));
9715 expression.set_location (loc);
9717 /* If the next token is not a comma, or we're in a fold-expression, then
9718 we are done with the expression. */
9719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9720 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9721 break;
9722 /* Consume the `,'. */
9723 loc = cp_lexer_peek_token (parser->lexer)->location;
9724 cp_lexer_consume_token (parser->lexer);
9725 /* A comma operator cannot appear in a constant-expression. */
9726 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9727 expression = error_mark_node;
9730 return expression;
9733 /* Parse a constant-expression.
9735 constant-expression:
9736 conditional-expression
9738 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9739 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9740 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9741 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
9742 only parse a conditional-expression, otherwise parse an
9743 assignment-expression. See below for rationale. */
9745 static cp_expr
9746 cp_parser_constant_expression (cp_parser* parser,
9747 bool allow_non_constant_p,
9748 bool *non_constant_p,
9749 bool strict_p)
9751 bool saved_integral_constant_expression_p;
9752 bool saved_allow_non_integral_constant_expression_p;
9753 bool saved_non_integral_constant_expression_p;
9754 cp_expr expression;
9756 /* It might seem that we could simply parse the
9757 conditional-expression, and then check to see if it were
9758 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9759 one that the compiler can figure out is constant, possibly after
9760 doing some simplifications or optimizations. The standard has a
9761 precise definition of constant-expression, and we must honor
9762 that, even though it is somewhat more restrictive.
9764 For example:
9766 int i[(2, 3)];
9768 is not a legal declaration, because `(2, 3)' is not a
9769 constant-expression. The `,' operator is forbidden in a
9770 constant-expression. However, GCC's constant-folding machinery
9771 will fold this operation to an INTEGER_CST for `3'. */
9773 /* Save the old settings. */
9774 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9775 saved_allow_non_integral_constant_expression_p
9776 = parser->allow_non_integral_constant_expression_p;
9777 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9778 /* We are now parsing a constant-expression. */
9779 parser->integral_constant_expression_p = true;
9780 parser->allow_non_integral_constant_expression_p
9781 = (allow_non_constant_p || cxx_dialect >= cxx11);
9782 parser->non_integral_constant_expression_p = false;
9783 /* Although the grammar says "conditional-expression", when not STRICT_P,
9784 we parse an "assignment-expression", which also permits
9785 "throw-expression" and the use of assignment operators. In the case
9786 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9787 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9788 actually essential that we look for an assignment-expression.
9789 For example, cp_parser_initializer_clauses uses this function to
9790 determine whether a particular assignment-expression is in fact
9791 constant. */
9792 if (strict_p)
9794 /* Parse the binary expressions (logical-or-expression). */
9795 expression = cp_parser_binary_expression (parser, false, false, false,
9796 PREC_NOT_OPERATOR, NULL);
9797 /* If the next token is a `?' then we're actually looking at
9798 a conditional-expression; otherwise we're done. */
9799 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9800 expression = cp_parser_question_colon_clause (parser, expression);
9802 else
9803 expression = cp_parser_assignment_expression (parser);
9804 /* Restore the old settings. */
9805 parser->integral_constant_expression_p
9806 = saved_integral_constant_expression_p;
9807 parser->allow_non_integral_constant_expression_p
9808 = saved_allow_non_integral_constant_expression_p;
9809 if (cxx_dialect >= cxx11)
9811 /* Require an rvalue constant expression here; that's what our
9812 callers expect. Reference constant expressions are handled
9813 separately in e.g. cp_parser_template_argument. */
9814 tree decay = expression;
9815 if (TREE_TYPE (expression)
9816 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9817 decay = build_address (expression);
9818 bool is_const = potential_rvalue_constant_expression (decay);
9819 parser->non_integral_constant_expression_p = !is_const;
9820 if (!is_const && !allow_non_constant_p)
9821 require_potential_rvalue_constant_expression (decay);
9823 if (allow_non_constant_p)
9824 *non_constant_p = parser->non_integral_constant_expression_p;
9825 parser->non_integral_constant_expression_p
9826 = saved_non_integral_constant_expression_p;
9828 return expression;
9831 /* Parse __builtin_offsetof.
9833 offsetof-expression:
9834 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9836 offsetof-member-designator:
9837 id-expression
9838 | offsetof-member-designator "." id-expression
9839 | offsetof-member-designator "[" expression "]"
9840 | offsetof-member-designator "->" id-expression */
9842 static cp_expr
9843 cp_parser_builtin_offsetof (cp_parser *parser)
9845 int save_ice_p, save_non_ice_p;
9846 tree type;
9847 cp_expr expr;
9848 cp_id_kind dummy;
9849 cp_token *token;
9850 location_t finish_loc;
9852 /* We're about to accept non-integral-constant things, but will
9853 definitely yield an integral constant expression. Save and
9854 restore these values around our local parsing. */
9855 save_ice_p = parser->integral_constant_expression_p;
9856 save_non_ice_p = parser->non_integral_constant_expression_p;
9858 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9860 /* Consume the "__builtin_offsetof" token. */
9861 cp_lexer_consume_token (parser->lexer);
9862 /* Consume the opening `('. */
9863 matching_parens parens;
9864 parens.require_open (parser);
9865 /* Parse the type-id. */
9866 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9867 type = cp_parser_type_id (parser);
9868 /* Look for the `,'. */
9869 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9870 token = cp_lexer_peek_token (parser->lexer);
9872 /* Build the (type *)null that begins the traditional offsetof macro. */
9873 tree object_ptr
9874 = build_static_cast (build_pointer_type (type), null_pointer_node,
9875 tf_warning_or_error);
9877 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9878 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9879 true, &dummy, token->location);
9880 while (true)
9882 token = cp_lexer_peek_token (parser->lexer);
9883 switch (token->type)
9885 case CPP_OPEN_SQUARE:
9886 /* offsetof-member-designator "[" expression "]" */
9887 expr = cp_parser_postfix_open_square_expression (parser, expr,
9888 true, false);
9889 break;
9891 case CPP_DEREF:
9892 /* offsetof-member-designator "->" identifier */
9893 expr = grok_array_decl (token->location, expr,
9894 integer_zero_node, false);
9895 /* FALLTHRU */
9897 case CPP_DOT:
9898 /* offsetof-member-designator "." identifier */
9899 cp_lexer_consume_token (parser->lexer);
9900 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9901 expr, true, &dummy,
9902 token->location);
9903 break;
9905 case CPP_CLOSE_PAREN:
9906 /* Consume the ")" token. */
9907 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9908 cp_lexer_consume_token (parser->lexer);
9909 goto success;
9911 default:
9912 /* Error. We know the following require will fail, but
9913 that gives the proper error message. */
9914 parens.require_close (parser);
9915 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9916 expr = error_mark_node;
9917 goto failure;
9921 success:
9922 /* Make a location of the form:
9923 __builtin_offsetof (struct s, f)
9924 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9925 with caret at the type-id, ranging from the start of the
9926 "_builtin_offsetof" token to the close paren. */
9927 loc = make_location (loc, start_loc, finish_loc);
9928 /* The result will be an INTEGER_CST, so we need to explicitly
9929 preserve the location. */
9930 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9932 failure:
9933 parser->integral_constant_expression_p = save_ice_p;
9934 parser->non_integral_constant_expression_p = save_non_ice_p;
9936 return expr;
9939 /* Parse a trait expression.
9941 Returns a representation of the expression, the underlying type
9942 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9944 static tree
9945 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9947 cp_trait_kind kind;
9948 tree type1, type2 = NULL_TREE;
9949 bool binary = false;
9950 bool variadic = false;
9952 switch (keyword)
9954 case RID_HAS_NOTHROW_ASSIGN:
9955 kind = CPTK_HAS_NOTHROW_ASSIGN;
9956 break;
9957 case RID_HAS_NOTHROW_CONSTRUCTOR:
9958 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9959 break;
9960 case RID_HAS_NOTHROW_COPY:
9961 kind = CPTK_HAS_NOTHROW_COPY;
9962 break;
9963 case RID_HAS_TRIVIAL_ASSIGN:
9964 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9965 break;
9966 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9967 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9968 break;
9969 case RID_HAS_TRIVIAL_COPY:
9970 kind = CPTK_HAS_TRIVIAL_COPY;
9971 break;
9972 case RID_HAS_TRIVIAL_DESTRUCTOR:
9973 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9974 break;
9975 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9976 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9977 break;
9978 case RID_HAS_VIRTUAL_DESTRUCTOR:
9979 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9980 break;
9981 case RID_IS_ABSTRACT:
9982 kind = CPTK_IS_ABSTRACT;
9983 break;
9984 case RID_IS_AGGREGATE:
9985 kind = CPTK_IS_AGGREGATE;
9986 break;
9987 case RID_IS_BASE_OF:
9988 kind = CPTK_IS_BASE_OF;
9989 binary = true;
9990 break;
9991 case RID_IS_CLASS:
9992 kind = CPTK_IS_CLASS;
9993 break;
9994 case RID_IS_EMPTY:
9995 kind = CPTK_IS_EMPTY;
9996 break;
9997 case RID_IS_ENUM:
9998 kind = CPTK_IS_ENUM;
9999 break;
10000 case RID_IS_FINAL:
10001 kind = CPTK_IS_FINAL;
10002 break;
10003 case RID_IS_LITERAL_TYPE:
10004 kind = CPTK_IS_LITERAL_TYPE;
10005 break;
10006 case RID_IS_POD:
10007 kind = CPTK_IS_POD;
10008 break;
10009 case RID_IS_POLYMORPHIC:
10010 kind = CPTK_IS_POLYMORPHIC;
10011 break;
10012 case RID_IS_SAME_AS:
10013 kind = CPTK_IS_SAME_AS;
10014 binary = true;
10015 break;
10016 case RID_IS_STD_LAYOUT:
10017 kind = CPTK_IS_STD_LAYOUT;
10018 break;
10019 case RID_IS_TRIVIAL:
10020 kind = CPTK_IS_TRIVIAL;
10021 break;
10022 case RID_IS_TRIVIALLY_ASSIGNABLE:
10023 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
10024 binary = true;
10025 break;
10026 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
10027 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
10028 variadic = true;
10029 break;
10030 case RID_IS_TRIVIALLY_COPYABLE:
10031 kind = CPTK_IS_TRIVIALLY_COPYABLE;
10032 break;
10033 case RID_IS_UNION:
10034 kind = CPTK_IS_UNION;
10035 break;
10036 case RID_UNDERLYING_TYPE:
10037 kind = CPTK_UNDERLYING_TYPE;
10038 break;
10039 case RID_BASES:
10040 kind = CPTK_BASES;
10041 break;
10042 case RID_DIRECT_BASES:
10043 kind = CPTK_DIRECT_BASES;
10044 break;
10045 case RID_IS_ASSIGNABLE:
10046 kind = CPTK_IS_ASSIGNABLE;
10047 binary = true;
10048 break;
10049 case RID_IS_CONSTRUCTIBLE:
10050 kind = CPTK_IS_CONSTRUCTIBLE;
10051 variadic = true;
10052 break;
10053 default:
10054 gcc_unreachable ();
10057 /* Consume the token. */
10058 cp_lexer_consume_token (parser->lexer);
10060 matching_parens parens;
10061 parens.require_open (parser);
10064 type_id_in_expr_sentinel s (parser);
10065 type1 = cp_parser_type_id (parser);
10068 if (type1 == error_mark_node)
10069 return error_mark_node;
10071 if (binary)
10073 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10076 type_id_in_expr_sentinel s (parser);
10077 type2 = cp_parser_type_id (parser);
10080 if (type2 == error_mark_node)
10081 return error_mark_node;
10083 else if (variadic)
10085 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10087 cp_lexer_consume_token (parser->lexer);
10088 tree elt = cp_parser_type_id (parser);
10089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10091 cp_lexer_consume_token (parser->lexer);
10092 elt = make_pack_expansion (elt);
10094 if (elt == error_mark_node)
10095 return error_mark_node;
10096 type2 = tree_cons (NULL_TREE, elt, type2);
10100 parens.require_close (parser);
10102 /* Complete the trait expression, which may mean either processing
10103 the trait expr now or saving it for template instantiation. */
10104 switch (kind)
10106 case CPTK_UNDERLYING_TYPE:
10107 return finish_underlying_type (type1);
10108 case CPTK_BASES:
10109 return finish_bases (type1, false);
10110 case CPTK_DIRECT_BASES:
10111 return finish_bases (type1, true);
10112 default:
10113 return finish_trait_expr (kind, type1, type2);
10117 /* Parse a lambda expression.
10119 lambda-expression:
10120 lambda-introducer lambda-declarator [opt] compound-statement
10122 Returns a representation of the expression. */
10124 static cp_expr
10125 cp_parser_lambda_expression (cp_parser* parser)
10127 tree lambda_expr = build_lambda_expr ();
10128 tree type;
10129 bool ok = true;
10130 cp_token *token = cp_lexer_peek_token (parser->lexer);
10131 cp_token_position start = 0;
10133 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10135 if (cp_unevaluated_operand)
10137 if (!token->error_reported)
10139 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10140 "lambda-expression in unevaluated context");
10141 token->error_reported = true;
10143 ok = false;
10145 else if (parser->in_template_argument_list_p)
10147 if (!token->error_reported)
10149 error_at (token->location, "lambda-expression in template-argument");
10150 token->error_reported = true;
10152 ok = false;
10155 /* We may be in the middle of deferred access check. Disable
10156 it now. */
10157 push_deferring_access_checks (dk_no_deferred);
10159 cp_parser_lambda_introducer (parser, lambda_expr);
10161 type = begin_lambda_type (lambda_expr);
10162 if (type == error_mark_node)
10163 return error_mark_node;
10165 record_lambda_scope (lambda_expr);
10167 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10168 determine_visibility (TYPE_NAME (type));
10170 /* Now that we've started the type, add the capture fields for any
10171 explicit captures. */
10172 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10175 /* Inside the class, surrounding template-parameter-lists do not apply. */
10176 unsigned int saved_num_template_parameter_lists
10177 = parser->num_template_parameter_lists;
10178 unsigned char in_statement = parser->in_statement;
10179 bool in_switch_statement_p = parser->in_switch_statement_p;
10180 bool fully_implicit_function_template_p
10181 = parser->fully_implicit_function_template_p;
10182 tree implicit_template_parms = parser->implicit_template_parms;
10183 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10184 bool auto_is_implicit_function_template_parm_p
10185 = parser->auto_is_implicit_function_template_parm_p;
10187 parser->num_template_parameter_lists = 0;
10188 parser->in_statement = 0;
10189 parser->in_switch_statement_p = false;
10190 parser->fully_implicit_function_template_p = false;
10191 parser->implicit_template_parms = 0;
10192 parser->implicit_template_scope = 0;
10193 parser->auto_is_implicit_function_template_parm_p = false;
10195 /* By virtue of defining a local class, a lambda expression has access to
10196 the private variables of enclosing classes. */
10198 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10200 if (ok && cp_parser_error_occurred (parser))
10201 ok = false;
10203 if (ok)
10205 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10206 && cp_parser_start_tentative_firewall (parser))
10207 start = token;
10208 cp_parser_lambda_body (parser, lambda_expr);
10210 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10212 if (cp_parser_skip_to_closing_brace (parser))
10213 cp_lexer_consume_token (parser->lexer);
10216 /* The capture list was built up in reverse order; fix that now. */
10217 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10218 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10220 if (ok)
10221 maybe_add_lambda_conv_op (type);
10223 type = finish_struct (type, /*attributes=*/NULL_TREE);
10225 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10226 parser->in_statement = in_statement;
10227 parser->in_switch_statement_p = in_switch_statement_p;
10228 parser->fully_implicit_function_template_p
10229 = fully_implicit_function_template_p;
10230 parser->implicit_template_parms = implicit_template_parms;
10231 parser->implicit_template_scope = implicit_template_scope;
10232 parser->auto_is_implicit_function_template_parm_p
10233 = auto_is_implicit_function_template_parm_p;
10236 /* This field is only used during parsing of the lambda. */
10237 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10239 /* This lambda shouldn't have any proxies left at this point. */
10240 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10241 /* And now that we're done, push proxies for an enclosing lambda. */
10242 insert_pending_capture_proxies ();
10244 if (ok)
10245 lambda_expr = build_lambda_object (lambda_expr);
10246 else
10247 lambda_expr = error_mark_node;
10249 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10251 pop_deferring_access_checks ();
10253 return lambda_expr;
10256 /* Parse the beginning of a lambda expression.
10258 lambda-introducer:
10259 [ lambda-capture [opt] ]
10261 LAMBDA_EXPR is the current representation of the lambda expression. */
10263 static void
10264 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10266 /* Need commas after the first capture. */
10267 bool first = true;
10269 /* Eat the leading `['. */
10270 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10272 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10273 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10274 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10275 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10276 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10277 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10279 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10281 cp_lexer_consume_token (parser->lexer);
10282 first = false;
10285 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10287 cp_token* capture_token;
10288 tree capture_id;
10289 tree capture_init_expr;
10290 cp_id_kind idk = CP_ID_KIND_NONE;
10291 bool explicit_init_p = false;
10293 enum capture_kind_type
10295 BY_COPY,
10296 BY_REFERENCE
10298 enum capture_kind_type capture_kind = BY_COPY;
10300 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10302 error ("expected end of capture-list");
10303 return;
10306 if (first)
10307 first = false;
10308 else
10309 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10311 /* Possibly capture `this'. */
10312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10314 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10315 if (cxx_dialect < cxx2a
10316 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10317 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10318 "with by-copy capture default");
10319 cp_lexer_consume_token (parser->lexer);
10320 add_capture (lambda_expr,
10321 /*id=*/this_identifier,
10322 /*initializer=*/finish_this_expr (),
10323 /*by_reference_p=*/true,
10324 explicit_init_p);
10325 continue;
10328 /* Possibly capture `*this'. */
10329 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10330 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10332 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10333 if (cxx_dialect < cxx17)
10334 pedwarn (loc, 0, "%<*this%> capture only available with "
10335 "-std=c++17 or -std=gnu++17");
10336 cp_lexer_consume_token (parser->lexer);
10337 cp_lexer_consume_token (parser->lexer);
10338 add_capture (lambda_expr,
10339 /*id=*/this_identifier,
10340 /*initializer=*/finish_this_expr (),
10341 /*by_reference_p=*/false,
10342 explicit_init_p);
10343 continue;
10346 /* Remember whether we want to capture as a reference or not. */
10347 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10349 capture_kind = BY_REFERENCE;
10350 cp_lexer_consume_token (parser->lexer);
10353 /* Get the identifier. */
10354 capture_token = cp_lexer_peek_token (parser->lexer);
10355 capture_id = cp_parser_identifier (parser);
10357 if (capture_id == error_mark_node)
10358 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10359 delimiters, but I modified this to stop on unnested ']' as well. It
10360 was already changed to stop on unnested '}', so the
10361 "closing_parenthesis" name is no more misleading with my change. */
10363 cp_parser_skip_to_closing_parenthesis (parser,
10364 /*recovering=*/true,
10365 /*or_comma=*/true,
10366 /*consume_paren=*/true);
10367 break;
10370 /* Find the initializer for this capture. */
10371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10372 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10373 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10375 bool direct, non_constant;
10376 /* An explicit initializer exists. */
10377 if (cxx_dialect < cxx14)
10378 pedwarn (input_location, 0,
10379 "lambda capture initializers "
10380 "only available with -std=c++14 or -std=gnu++14");
10381 capture_init_expr = cp_parser_initializer (parser, &direct,
10382 &non_constant);
10383 explicit_init_p = true;
10384 if (capture_init_expr == NULL_TREE)
10386 error ("empty initializer for lambda init-capture");
10387 capture_init_expr = error_mark_node;
10390 else
10392 const char* error_msg;
10394 /* Turn the identifier into an id-expression. */
10395 capture_init_expr
10396 = cp_parser_lookup_name_simple (parser, capture_id,
10397 capture_token->location);
10399 if (capture_init_expr == error_mark_node)
10401 unqualified_name_lookup_error (capture_id);
10402 continue;
10404 else if (DECL_P (capture_init_expr)
10405 && (!VAR_P (capture_init_expr)
10406 && TREE_CODE (capture_init_expr) != PARM_DECL))
10408 error_at (capture_token->location,
10409 "capture of non-variable %qD ",
10410 capture_init_expr);
10411 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10412 "%q#D declared here", capture_init_expr);
10413 continue;
10415 if (VAR_P (capture_init_expr)
10416 && decl_storage_duration (capture_init_expr) != dk_auto)
10418 if (pedwarn (capture_token->location, 0, "capture of variable "
10419 "%qD with non-automatic storage duration",
10420 capture_init_expr))
10421 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10422 "%q#D declared here", capture_init_expr);
10423 continue;
10426 capture_init_expr
10427 = finish_id_expression
10428 (capture_id,
10429 capture_init_expr,
10430 parser->scope,
10431 &idk,
10432 /*integral_constant_expression_p=*/false,
10433 /*allow_non_integral_constant_expression_p=*/false,
10434 /*non_integral_constant_expression_p=*/NULL,
10435 /*template_p=*/false,
10436 /*done=*/true,
10437 /*address_p=*/false,
10438 /*template_arg_p=*/false,
10439 &error_msg,
10440 capture_token->location);
10442 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10444 cp_lexer_consume_token (parser->lexer);
10445 capture_init_expr = make_pack_expansion (capture_init_expr);
10447 else
10448 check_for_bare_parameter_packs (capture_init_expr);
10451 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10452 && !explicit_init_p)
10454 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10455 && capture_kind == BY_COPY)
10456 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10457 "of %qD redundant with by-copy capture default",
10458 capture_id);
10459 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10460 && capture_kind == BY_REFERENCE)
10461 pedwarn (capture_token->location, 0, "explicit by-reference "
10462 "capture of %qD redundant with by-reference capture "
10463 "default", capture_id);
10466 add_capture (lambda_expr,
10467 capture_id,
10468 capture_init_expr,
10469 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10470 explicit_init_p);
10473 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10476 /* Parse the (optional) middle of a lambda expression.
10478 lambda-declarator:
10479 < template-parameter-list [opt] >
10480 ( parameter-declaration-clause [opt] )
10481 attribute-specifier [opt]
10482 decl-specifier-seq [opt]
10483 exception-specification [opt]
10484 lambda-return-type-clause [opt]
10486 LAMBDA_EXPR is the current representation of the lambda expression. */
10488 static bool
10489 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10491 /* 5.1.1.4 of the standard says:
10492 If a lambda-expression does not include a lambda-declarator, it is as if
10493 the lambda-declarator were ().
10494 This means an empty parameter list, no attributes, and no exception
10495 specification. */
10496 tree param_list = void_list_node;
10497 tree attributes = NULL_TREE;
10498 tree exception_spec = NULL_TREE;
10499 tree template_param_list = NULL_TREE;
10500 tree tx_qual = NULL_TREE;
10501 tree return_type = NULL_TREE;
10502 cp_decl_specifier_seq lambda_specs;
10503 clear_decl_specs (&lambda_specs);
10505 /* The template-parameter-list is optional, but must begin with
10506 an opening angle if present. */
10507 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10509 if (cxx_dialect < cxx14)
10510 pedwarn (parser->lexer->next_token->location, 0,
10511 "lambda templates are only available with "
10512 "-std=c++14 or -std=gnu++14");
10513 else
10514 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10515 "ISO C++ does not support lambda templates");
10517 cp_lexer_consume_token (parser->lexer);
10519 template_param_list = cp_parser_template_parameter_list (parser);
10521 cp_parser_skip_to_end_of_template_parameter_list (parser);
10523 /* We just processed one more parameter list. */
10524 ++parser->num_template_parameter_lists;
10527 /* The parameter-declaration-clause is optional (unless
10528 template-parameter-list was given), but must begin with an
10529 opening parenthesis if present. */
10530 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10532 matching_parens parens;
10533 parens.consume_open (parser);
10535 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10537 /* Parse parameters. */
10538 param_list = cp_parser_parameter_declaration_clause (parser);
10540 /* Default arguments shall not be specified in the
10541 parameter-declaration-clause of a lambda-declarator. */
10542 if (cxx_dialect < cxx14)
10543 for (tree t = param_list; t; t = TREE_CHAIN (t))
10544 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10545 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10546 "default argument specified for lambda parameter");
10548 parens.require_close (parser);
10550 attributes = cp_parser_attributes_opt (parser);
10552 /* In the decl-specifier-seq of the lambda-declarator, each
10553 decl-specifier shall either be mutable or constexpr. */
10554 int declares_class_or_enum;
10555 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10556 cp_parser_decl_specifier_seq (parser,
10557 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10558 &lambda_specs, &declares_class_or_enum);
10559 if (lambda_specs.storage_class == sc_mutable)
10561 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10562 if (lambda_specs.conflicting_specifiers_p)
10563 error_at (lambda_specs.locations[ds_storage_class],
10564 "duplicate %<mutable%>");
10567 tx_qual = cp_parser_tx_qualifier_opt (parser);
10569 /* Parse optional exception specification. */
10570 exception_spec = cp_parser_exception_specification_opt (parser);
10572 /* Parse optional trailing return type. */
10573 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10575 cp_lexer_consume_token (parser->lexer);
10576 return_type = cp_parser_trailing_type_id (parser);
10579 /* The function parameters must be in scope all the way until after the
10580 trailing-return-type in case of decltype. */
10581 pop_bindings_and_leave_scope ();
10583 else if (template_param_list != NULL_TREE) // generate diagnostic
10584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10586 /* Create the function call operator.
10588 Messing with declarators like this is no uglier than building up the
10589 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10590 other code. */
10592 cp_decl_specifier_seq return_type_specs;
10593 cp_declarator* declarator;
10594 tree fco;
10595 int quals;
10596 void *p;
10598 clear_decl_specs (&return_type_specs);
10599 if (return_type)
10600 return_type_specs.type = return_type;
10601 else
10602 /* Maybe we will deduce the return type later. */
10603 return_type_specs.type = make_auto ();
10605 if (lambda_specs.locations[ds_constexpr])
10607 if (cxx_dialect >= cxx17)
10608 return_type_specs.locations[ds_constexpr]
10609 = lambda_specs.locations[ds_constexpr];
10610 else
10611 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10612 "lambda only available with -std=c++17 or -std=gnu++17");
10615 p = obstack_alloc (&declarator_obstack, 0);
10617 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10618 sfk_none);
10620 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10621 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10622 declarator = make_call_declarator (declarator, param_list, quals,
10623 VIRT_SPEC_UNSPECIFIED,
10624 REF_QUAL_NONE,
10625 tx_qual,
10626 exception_spec,
10627 /*late_return_type=*/NULL_TREE,
10628 /*requires_clause*/NULL_TREE);
10629 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10631 fco = grokmethod (&return_type_specs,
10632 declarator,
10633 attributes);
10634 if (fco != error_mark_node)
10636 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10637 DECL_ARTIFICIAL (fco) = 1;
10638 /* Give the object parameter a different name. */
10639 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10640 if (return_type)
10641 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10643 if (template_param_list)
10645 fco = finish_member_template_decl (fco);
10646 finish_template_decl (template_param_list);
10647 --parser->num_template_parameter_lists;
10649 else if (parser->fully_implicit_function_template_p)
10650 fco = finish_fully_implicit_template (parser, fco);
10652 finish_member_declaration (fco);
10654 obstack_free (&declarator_obstack, p);
10656 return (fco != error_mark_node);
10660 /* Parse the body of a lambda expression, which is simply
10662 compound-statement
10664 but which requires special handling.
10665 LAMBDA_EXPR is the current representation of the lambda expression. */
10667 static void
10668 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10670 bool nested = (current_function_decl != NULL_TREE);
10671 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10672 bool in_function_body = parser->in_function_body;
10673 if (nested)
10674 push_function_context ();
10675 else
10676 /* Still increment function_depth so that we don't GC in the
10677 middle of an expression. */
10678 ++function_depth;
10679 vec<tree> omp_privatization_save;
10680 save_omp_privatization_clauses (omp_privatization_save);
10681 /* Clear this in case we're in the middle of a default argument. */
10682 parser->local_variables_forbidden_p = false;
10683 parser->in_function_body = true;
10685 /* Finish the function call operator
10686 - class_specifier
10687 + late_parsing_for_member
10688 + function_definition_after_declarator
10689 + ctor_initializer_opt_and_function_body */
10691 local_specialization_stack s (lss_copy);
10693 tree fco = lambda_function (lambda_expr);
10694 tree body = start_lambda_function (fco, lambda_expr);
10695 bool done = false;
10696 tree compound_stmt;
10698 matching_braces braces;
10699 if (!braces.require_open (parser))
10700 goto out;
10702 compound_stmt = begin_compound_stmt (0);
10704 /* 5.1.1.4 of the standard says:
10705 If a lambda-expression does not include a trailing-return-type, it
10706 is as if the trailing-return-type denotes the following type:
10707 * if the compound-statement is of the form
10708 { return attribute-specifier [opt] expression ; }
10709 the type of the returned expression after lvalue-to-rvalue
10710 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10711 (_conv.array_ 4.2), and function-to-pointer conversion
10712 (_conv.func_ 4.3);
10713 * otherwise, void. */
10715 /* In a lambda that has neither a lambda-return-type-clause
10716 nor a deducible form, errors should be reported for return statements
10717 in the body. Since we used void as the placeholder return type, parsing
10718 the body as usual will give such desired behavior. */
10719 if (is_auto (TREE_TYPE (TREE_TYPE (fco)))
10720 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10721 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10723 tree expr = NULL_TREE;
10724 cp_id_kind idk = CP_ID_KIND_NONE;
10726 /* Parse tentatively in case there's more after the initial return
10727 statement. */
10728 cp_parser_parse_tentatively (parser);
10730 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10732 expr = cp_parser_expression (parser, &idk);
10734 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10735 braces.require_close (parser);
10737 if (cp_parser_parse_definitely (parser))
10739 if (!processing_template_decl)
10741 tree type = lambda_return_type (expr);
10742 apply_deduced_return_type (fco, type);
10743 if (type == error_mark_node)
10744 expr = error_mark_node;
10747 /* Will get error here if type not deduced yet. */
10748 finish_return_stmt (expr);
10750 done = true;
10754 if (!done)
10756 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10757 cp_parser_label_declaration (parser);
10758 cp_parser_statement_seq_opt (parser, NULL_TREE);
10759 braces.require_close (parser);
10762 finish_compound_stmt (compound_stmt);
10764 out:
10765 finish_lambda_function (body);
10768 restore_omp_privatization_clauses (omp_privatization_save);
10769 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10770 parser->in_function_body = in_function_body;
10771 if (nested)
10772 pop_function_context();
10773 else
10774 --function_depth;
10777 /* Statements [gram.stmt.stmt] */
10779 /* Parse a statement.
10781 statement:
10782 labeled-statement
10783 expression-statement
10784 compound-statement
10785 selection-statement
10786 iteration-statement
10787 jump-statement
10788 declaration-statement
10789 try-block
10791 C++11:
10793 statement:
10794 labeled-statement
10795 attribute-specifier-seq (opt) expression-statement
10796 attribute-specifier-seq (opt) compound-statement
10797 attribute-specifier-seq (opt) selection-statement
10798 attribute-specifier-seq (opt) iteration-statement
10799 attribute-specifier-seq (opt) jump-statement
10800 declaration-statement
10801 attribute-specifier-seq (opt) try-block
10803 init-statement:
10804 expression-statement
10805 simple-declaration
10807 TM Extension:
10809 statement:
10810 atomic-statement
10812 IN_COMPOUND is true when the statement is nested inside a
10813 cp_parser_compound_statement; this matters for certain pragmas.
10815 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10816 is a (possibly labeled) if statement which is not enclosed in braces
10817 and has an else clause. This is used to implement -Wparentheses.
10819 CHAIN is a vector of if-else-if conditions. */
10821 static void
10822 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10823 bool in_compound, bool *if_p, vec<tree> *chain,
10824 location_t *loc_after_labels)
10826 tree statement, std_attrs = NULL_TREE;
10827 cp_token *token;
10828 location_t statement_location, attrs_location;
10830 restart:
10831 if (if_p != NULL)
10832 *if_p = false;
10833 /* There is no statement yet. */
10834 statement = NULL_TREE;
10836 saved_token_sentinel saved_tokens (parser->lexer);
10837 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10838 if (c_dialect_objc ())
10839 /* In obj-c++, seeing '[[' might be the either the beginning of
10840 c++11 attributes, or a nested objc-message-expression. So
10841 let's parse the c++11 attributes tentatively. */
10842 cp_parser_parse_tentatively (parser);
10843 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10844 if (c_dialect_objc ())
10846 if (!cp_parser_parse_definitely (parser))
10847 std_attrs = NULL_TREE;
10850 /* Peek at the next token. */
10851 token = cp_lexer_peek_token (parser->lexer);
10852 /* Remember the location of the first token in the statement. */
10853 statement_location = token->location;
10854 /* If this is a keyword, then that will often determine what kind of
10855 statement we have. */
10856 if (token->type == CPP_KEYWORD)
10858 enum rid keyword = token->keyword;
10860 switch (keyword)
10862 case RID_CASE:
10863 case RID_DEFAULT:
10864 /* Looks like a labeled-statement with a case label.
10865 Parse the label, and then use tail recursion to parse
10866 the statement. */
10867 cp_parser_label_for_labeled_statement (parser, std_attrs);
10868 in_compound = false;
10869 goto restart;
10871 case RID_IF:
10872 case RID_SWITCH:
10873 statement = cp_parser_selection_statement (parser, if_p, chain);
10874 break;
10876 case RID_WHILE:
10877 case RID_DO:
10878 case RID_FOR:
10879 statement = cp_parser_iteration_statement (parser, if_p, false);
10880 break;
10882 case RID_CILK_FOR:
10883 if (!flag_cilkplus)
10885 error_at (cp_lexer_peek_token (parser->lexer)->location,
10886 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10887 cp_lexer_consume_token (parser->lexer);
10888 statement = error_mark_node;
10890 else
10891 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10892 break;
10894 case RID_BREAK:
10895 case RID_CONTINUE:
10896 case RID_RETURN:
10897 case RID_GOTO:
10898 statement = cp_parser_jump_statement (parser);
10899 break;
10901 case RID_CILK_SYNC:
10902 cp_lexer_consume_token (parser->lexer);
10903 if (flag_cilkplus)
10905 tree sync_expr = build_cilk_sync ();
10906 SET_EXPR_LOCATION (sync_expr,
10907 token->location);
10908 statement = finish_expr_stmt (sync_expr);
10910 else
10912 error_at (token->location, "-fcilkplus must be enabled to use"
10913 " %<_Cilk_sync%>");
10914 statement = error_mark_node;
10916 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10917 break;
10919 /* Objective-C++ exception-handling constructs. */
10920 case RID_AT_TRY:
10921 case RID_AT_CATCH:
10922 case RID_AT_FINALLY:
10923 case RID_AT_SYNCHRONIZED:
10924 case RID_AT_THROW:
10925 statement = cp_parser_objc_statement (parser);
10926 break;
10928 case RID_TRY:
10929 statement = cp_parser_try_block (parser);
10930 break;
10932 case RID_NAMESPACE:
10933 /* This must be a namespace alias definition. */
10934 cp_parser_declaration_statement (parser);
10935 return;
10937 case RID_TRANSACTION_ATOMIC:
10938 case RID_TRANSACTION_RELAXED:
10939 case RID_SYNCHRONIZED:
10940 case RID_ATOMIC_NOEXCEPT:
10941 case RID_ATOMIC_CANCEL:
10942 statement = cp_parser_transaction (parser, token);
10943 break;
10944 case RID_TRANSACTION_CANCEL:
10945 statement = cp_parser_transaction_cancel (parser);
10946 break;
10948 default:
10949 /* It might be a keyword like `int' that can start a
10950 declaration-statement. */
10951 break;
10954 else if (token->type == CPP_NAME)
10956 /* If the next token is a `:', then we are looking at a
10957 labeled-statement. */
10958 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10959 if (token->type == CPP_COLON)
10961 /* Looks like a labeled-statement with an ordinary label.
10962 Parse the label, and then use tail recursion to parse
10963 the statement. */
10965 cp_parser_label_for_labeled_statement (parser, std_attrs);
10966 in_compound = false;
10967 goto restart;
10970 /* Anything that starts with a `{' must be a compound-statement. */
10971 else if (token->type == CPP_OPEN_BRACE)
10972 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10973 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10974 a statement all its own. */
10975 else if (token->type == CPP_PRAGMA)
10977 /* Only certain OpenMP pragmas are attached to statements, and thus
10978 are considered statements themselves. All others are not. In
10979 the context of a compound, accept the pragma as a "statement" and
10980 return so that we can check for a close brace. Otherwise we
10981 require a real statement and must go back and read one. */
10982 if (in_compound)
10983 cp_parser_pragma (parser, pragma_compound, if_p);
10984 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10985 goto restart;
10986 return;
10988 else if (token->type == CPP_EOF)
10990 cp_parser_error (parser, "expected statement");
10991 return;
10994 /* Everything else must be a declaration-statement or an
10995 expression-statement. Try for the declaration-statement
10996 first, unless we are looking at a `;', in which case we know that
10997 we have an expression-statement. */
10998 if (!statement)
11000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11002 if (std_attrs != NULL_TREE)
11004 /* Attributes should be parsed as part of the the
11005 declaration, so let's un-parse them. */
11006 saved_tokens.rollback();
11007 std_attrs = NULL_TREE;
11010 cp_parser_parse_tentatively (parser);
11011 /* Try to parse the declaration-statement. */
11012 cp_parser_declaration_statement (parser);
11013 /* If that worked, we're done. */
11014 if (cp_parser_parse_definitely (parser))
11015 return;
11017 /* All preceding labels have been parsed at this point. */
11018 if (loc_after_labels != NULL)
11019 *loc_after_labels = statement_location;
11021 /* Look for an expression-statement instead. */
11022 statement = cp_parser_expression_statement (parser, in_statement_expr);
11024 /* Handle [[fallthrough]];. */
11025 if (attribute_fallthrough_p (std_attrs))
11027 /* The next token after the fallthrough attribute is ';'. */
11028 if (statement == NULL_TREE)
11030 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11031 statement = build_call_expr_internal_loc (statement_location,
11032 IFN_FALLTHROUGH,
11033 void_type_node, 0);
11034 finish_expr_stmt (statement);
11036 else
11037 warning_at (statement_location, OPT_Wattributes,
11038 "%<fallthrough%> attribute not followed by %<;%>");
11039 std_attrs = NULL_TREE;
11043 /* Set the line number for the statement. */
11044 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
11045 SET_EXPR_LOCATION (statement, statement_location);
11047 /* Allow "[[fallthrough]];", but warn otherwise. */
11048 if (std_attrs != NULL_TREE)
11049 warning_at (attrs_location,
11050 OPT_Wattributes,
11051 "attributes at the beginning of statement are ignored");
11054 /* Parse the label for a labeled-statement, i.e.
11056 identifier :
11057 case constant-expression :
11058 default :
11060 GNU Extension:
11061 case constant-expression ... constant-expression : statement
11063 When a label is parsed without errors, the label is added to the
11064 parse tree by the finish_* functions, so this function doesn't
11065 have to return the label. */
11067 static void
11068 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11070 cp_token *token;
11071 tree label = NULL_TREE;
11072 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11074 /* The next token should be an identifier. */
11075 token = cp_lexer_peek_token (parser->lexer);
11076 if (token->type != CPP_NAME
11077 && token->type != CPP_KEYWORD)
11079 cp_parser_error (parser, "expected labeled-statement");
11080 return;
11083 /* Remember whether this case or a user-defined label is allowed to fall
11084 through to. */
11085 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11087 parser->colon_corrects_to_scope_p = false;
11088 switch (token->keyword)
11090 case RID_CASE:
11092 tree expr, expr_hi;
11093 cp_token *ellipsis;
11095 /* Consume the `case' token. */
11096 cp_lexer_consume_token (parser->lexer);
11097 /* Parse the constant-expression. */
11098 expr = cp_parser_constant_expression (parser);
11099 if (check_for_bare_parameter_packs (expr))
11100 expr = error_mark_node;
11102 ellipsis = cp_lexer_peek_token (parser->lexer);
11103 if (ellipsis->type == CPP_ELLIPSIS)
11105 /* Consume the `...' token. */
11106 cp_lexer_consume_token (parser->lexer);
11107 expr_hi = cp_parser_constant_expression (parser);
11108 if (check_for_bare_parameter_packs (expr_hi))
11109 expr_hi = error_mark_node;
11111 /* We don't need to emit warnings here, as the common code
11112 will do this for us. */
11114 else
11115 expr_hi = NULL_TREE;
11117 if (parser->in_switch_statement_p)
11119 tree l = finish_case_label (token->location, expr, expr_hi);
11120 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11121 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11123 else
11124 error_at (token->location,
11125 "case label %qE not within a switch statement",
11126 expr);
11128 break;
11130 case RID_DEFAULT:
11131 /* Consume the `default' token. */
11132 cp_lexer_consume_token (parser->lexer);
11134 if (parser->in_switch_statement_p)
11136 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11137 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11138 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11140 else
11141 error_at (token->location, "case label not within a switch statement");
11142 break;
11144 default:
11145 /* Anything else must be an ordinary label. */
11146 label = finish_label_stmt (cp_parser_identifier (parser));
11147 if (label && TREE_CODE (label) == LABEL_DECL)
11148 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11149 break;
11152 /* Require the `:' token. */
11153 cp_parser_require (parser, CPP_COLON, RT_COLON);
11155 /* An ordinary label may optionally be followed by attributes.
11156 However, this is only permitted if the attributes are then
11157 followed by a semicolon. This is because, for backward
11158 compatibility, when parsing
11159 lab: __attribute__ ((unused)) int i;
11160 we want the attribute to attach to "i", not "lab". */
11161 if (label != NULL_TREE
11162 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11164 tree attrs;
11165 cp_parser_parse_tentatively (parser);
11166 attrs = cp_parser_gnu_attributes_opt (parser);
11167 if (attrs == NULL_TREE
11168 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11169 cp_parser_abort_tentative_parse (parser);
11170 else if (!cp_parser_parse_definitely (parser))
11172 else
11173 attributes = chainon (attributes, attrs);
11176 if (attributes != NULL_TREE)
11177 cplus_decl_attributes (&label, attributes, 0);
11179 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11182 /* Parse an expression-statement.
11184 expression-statement:
11185 expression [opt] ;
11187 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11188 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11189 indicates whether this expression-statement is part of an
11190 expression statement. */
11192 static tree
11193 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11195 tree statement = NULL_TREE;
11196 cp_token *token = cp_lexer_peek_token (parser->lexer);
11197 location_t loc = token->location;
11199 /* There might be attribute fallthrough. */
11200 tree attr = cp_parser_gnu_attributes_opt (parser);
11202 /* If the next token is a ';', then there is no expression
11203 statement. */
11204 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11206 statement = cp_parser_expression (parser);
11207 if (statement == error_mark_node
11208 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11210 cp_parser_skip_to_end_of_block_or_statement (parser);
11211 return error_mark_node;
11215 /* Handle [[fallthrough]];. */
11216 if (attribute_fallthrough_p (attr))
11218 /* The next token after the fallthrough attribute is ';'. */
11219 if (statement == NULL_TREE)
11220 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11221 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11222 void_type_node, 0);
11223 else
11224 warning_at (loc, OPT_Wattributes,
11225 "%<fallthrough%> attribute not followed by %<;%>");
11226 attr = NULL_TREE;
11229 /* Allow "[[fallthrough]];", but warn otherwise. */
11230 if (attr != NULL_TREE)
11231 warning_at (loc, OPT_Wattributes,
11232 "attributes at the beginning of statement are ignored");
11234 /* Give a helpful message for "A<T>::type t;" and the like. */
11235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11236 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11238 if (TREE_CODE (statement) == SCOPE_REF)
11239 error_at (token->location, "need %<typename%> before %qE because "
11240 "%qT is a dependent scope",
11241 statement, TREE_OPERAND (statement, 0));
11242 else if (is_overloaded_fn (statement)
11243 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11245 /* A::A a; */
11246 tree fn = get_first_fn (statement);
11247 error_at (token->location,
11248 "%<%T::%D%> names the constructor, not the type",
11249 DECL_CONTEXT (fn), DECL_NAME (fn));
11253 /* Consume the final `;'. */
11254 cp_parser_consume_semicolon_at_end_of_statement (parser);
11256 if (in_statement_expr
11257 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11258 /* This is the final expression statement of a statement
11259 expression. */
11260 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11261 else if (statement)
11262 statement = finish_expr_stmt (statement);
11264 return statement;
11267 /* Parse a compound-statement.
11269 compound-statement:
11270 { statement-seq [opt] }
11272 GNU extension:
11274 compound-statement:
11275 { label-declaration-seq [opt] statement-seq [opt] }
11277 label-declaration-seq:
11278 label-declaration
11279 label-declaration-seq label-declaration
11281 Returns a tree representing the statement. */
11283 static tree
11284 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11285 int bcs_flags, bool function_body)
11287 tree compound_stmt;
11288 matching_braces braces;
11290 /* Consume the `{'. */
11291 if (!braces.require_open (parser))
11292 return error_mark_node;
11293 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11294 && !function_body && cxx_dialect < cxx14)
11295 pedwarn (input_location, OPT_Wpedantic,
11296 "compound-statement in constexpr function");
11297 /* Begin the compound-statement. */
11298 compound_stmt = begin_compound_stmt (bcs_flags);
11299 /* If the next keyword is `__label__' we have a label declaration. */
11300 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11301 cp_parser_label_declaration (parser);
11302 /* Parse an (optional) statement-seq. */
11303 cp_parser_statement_seq_opt (parser, in_statement_expr);
11304 /* Finish the compound-statement. */
11305 finish_compound_stmt (compound_stmt);
11306 /* Consume the `}'. */
11307 braces.require_close (parser);
11309 return compound_stmt;
11312 /* Parse an (optional) statement-seq.
11314 statement-seq:
11315 statement
11316 statement-seq [opt] statement */
11318 static void
11319 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11321 /* Scan statements until there aren't any more. */
11322 while (true)
11324 cp_token *token = cp_lexer_peek_token (parser->lexer);
11326 /* If we are looking at a `}', then we have run out of
11327 statements; the same is true if we have reached the end
11328 of file, or have stumbled upon a stray '@end'. */
11329 if (token->type == CPP_CLOSE_BRACE
11330 || token->type == CPP_EOF
11331 || token->type == CPP_PRAGMA_EOL
11332 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11333 break;
11335 /* If we are in a compound statement and find 'else' then
11336 something went wrong. */
11337 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11339 if (parser->in_statement & IN_IF_STMT)
11340 break;
11341 else
11343 token = cp_lexer_consume_token (parser->lexer);
11344 error_at (token->location, "%<else%> without a previous %<if%>");
11348 /* Parse the statement. */
11349 cp_parser_statement (parser, in_statement_expr, true, NULL);
11353 /* Return true if we're looking at (init; cond), false otherwise. */
11355 static bool
11356 cp_parser_init_statement_p (cp_parser *parser)
11358 /* Save tokens so that we can put them back. */
11359 cp_lexer_save_tokens (parser->lexer);
11361 /* Look for ';' that is not nested in () or {}. */
11362 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11363 /*recovering=*/false,
11364 CPP_SEMICOLON,
11365 /*consume_paren=*/false);
11367 /* Roll back the tokens we skipped. */
11368 cp_lexer_rollback_tokens (parser->lexer);
11370 return ret == -1;
11373 /* Parse a selection-statement.
11375 selection-statement:
11376 if ( init-statement [opt] condition ) statement
11377 if ( init-statement [opt] condition ) statement else statement
11378 switch ( init-statement [opt] condition ) statement
11380 Returns the new IF_STMT or SWITCH_STMT.
11382 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11383 is a (possibly labeled) if statement which is not enclosed in
11384 braces and has an else clause. This is used to implement
11385 -Wparentheses.
11387 CHAIN is a vector of if-else-if conditions. This is used to implement
11388 -Wduplicated-cond. */
11390 static tree
11391 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11392 vec<tree> *chain)
11394 cp_token *token;
11395 enum rid keyword;
11396 token_indent_info guard_tinfo;
11398 if (if_p != NULL)
11399 *if_p = false;
11401 /* Peek at the next token. */
11402 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11403 guard_tinfo = get_token_indent_info (token);
11405 /* See what kind of keyword it is. */
11406 keyword = token->keyword;
11407 switch (keyword)
11409 case RID_IF:
11410 case RID_SWITCH:
11412 tree statement;
11413 tree condition;
11415 bool cx = false;
11416 if (keyword == RID_IF
11417 && cp_lexer_next_token_is_keyword (parser->lexer,
11418 RID_CONSTEXPR))
11420 cx = true;
11421 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11422 if (cxx_dialect < cxx17 && !in_system_header_at (tok->location))
11423 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11424 "with -std=c++17 or -std=gnu++17");
11427 /* Look for the `('. */
11428 matching_parens parens;
11429 if (!parens.require_open (parser))
11431 cp_parser_skip_to_end_of_statement (parser);
11432 return error_mark_node;
11435 /* Begin the selection-statement. */
11436 if (keyword == RID_IF)
11438 statement = begin_if_stmt ();
11439 IF_STMT_CONSTEXPR_P (statement) = cx;
11441 else
11442 statement = begin_switch_stmt ();
11444 /* Parse the optional init-statement. */
11445 if (cp_parser_init_statement_p (parser))
11447 tree decl;
11448 if (cxx_dialect < cxx17)
11449 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11450 "init-statement in selection statements only available "
11451 "with -std=c++17 or -std=gnu++17");
11452 cp_parser_init_statement (parser, &decl);
11455 /* Parse the condition. */
11456 condition = cp_parser_condition (parser);
11457 /* Look for the `)'. */
11458 if (!parens.require_close (parser))
11459 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11460 /*consume_paren=*/true);
11462 if (keyword == RID_IF)
11464 bool nested_if;
11465 unsigned char in_statement;
11467 /* Add the condition. */
11468 condition = finish_if_stmt_cond (condition, statement);
11470 if (warn_duplicated_cond)
11471 warn_duplicated_cond_add_or_warn (token->location, condition,
11472 &chain);
11474 /* Parse the then-clause. */
11475 in_statement = parser->in_statement;
11476 parser->in_statement |= IN_IF_STMT;
11478 /* Outside a template, the non-selected branch of a constexpr
11479 if is a 'discarded statement', i.e. unevaluated. */
11480 bool was_discarded = in_discarded_stmt;
11481 bool discard_then = (cx && !processing_template_decl
11482 && integer_zerop (condition));
11483 if (discard_then)
11485 in_discarded_stmt = true;
11486 ++c_inhibit_evaluation_warnings;
11489 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11490 guard_tinfo);
11492 parser->in_statement = in_statement;
11494 finish_then_clause (statement);
11496 if (discard_then)
11498 THEN_CLAUSE (statement) = NULL_TREE;
11499 in_discarded_stmt = was_discarded;
11500 --c_inhibit_evaluation_warnings;
11503 /* If the next token is `else', parse the else-clause. */
11504 if (cp_lexer_next_token_is_keyword (parser->lexer,
11505 RID_ELSE))
11507 bool discard_else = (cx && !processing_template_decl
11508 && integer_nonzerop (condition));
11509 if (discard_else)
11511 in_discarded_stmt = true;
11512 ++c_inhibit_evaluation_warnings;
11515 guard_tinfo
11516 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11517 /* Consume the `else' keyword. */
11518 cp_lexer_consume_token (parser->lexer);
11519 if (warn_duplicated_cond)
11521 if (cp_lexer_next_token_is_keyword (parser->lexer,
11522 RID_IF)
11523 && chain == NULL)
11525 /* We've got "if (COND) else if (COND2)". Start
11526 the condition chain and add COND as the first
11527 element. */
11528 chain = new vec<tree> ();
11529 if (!CONSTANT_CLASS_P (condition)
11530 && !TREE_SIDE_EFFECTS (condition))
11532 /* Wrap it in a NOP_EXPR so that we can set the
11533 location of the condition. */
11534 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11535 condition);
11536 SET_EXPR_LOCATION (e, token->location);
11537 chain->safe_push (e);
11540 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11541 RID_IF))
11543 /* This is if-else without subsequent if. Zap the
11544 condition chain; we would have already warned at
11545 this point. */
11546 delete chain;
11547 chain = NULL;
11550 begin_else_clause (statement);
11551 /* Parse the else-clause. */
11552 cp_parser_implicitly_scoped_statement (parser, NULL,
11553 guard_tinfo, chain);
11555 finish_else_clause (statement);
11557 /* If we are currently parsing a then-clause, then
11558 IF_P will not be NULL. We set it to true to
11559 indicate that this if statement has an else clause.
11560 This may trigger the Wparentheses warning below
11561 when we get back up to the parent if statement. */
11562 if (if_p != NULL)
11563 *if_p = true;
11565 if (discard_else)
11567 ELSE_CLAUSE (statement) = NULL_TREE;
11568 in_discarded_stmt = was_discarded;
11569 --c_inhibit_evaluation_warnings;
11572 else
11574 /* This if statement does not have an else clause. If
11575 NESTED_IF is true, then the then-clause has an if
11576 statement which does have an else clause. We warn
11577 about the potential ambiguity. */
11578 if (nested_if)
11579 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11580 "suggest explicit braces to avoid ambiguous"
11581 " %<else%>");
11582 if (warn_duplicated_cond)
11584 /* We don't need the condition chain anymore. */
11585 delete chain;
11586 chain = NULL;
11590 /* Now we're all done with the if-statement. */
11591 finish_if_stmt (statement);
11593 else
11595 bool in_switch_statement_p;
11596 unsigned char in_statement;
11598 /* Add the condition. */
11599 finish_switch_cond (condition, statement);
11601 /* Parse the body of the switch-statement. */
11602 in_switch_statement_p = parser->in_switch_statement_p;
11603 in_statement = parser->in_statement;
11604 parser->in_switch_statement_p = true;
11605 parser->in_statement |= IN_SWITCH_STMT;
11606 cp_parser_implicitly_scoped_statement (parser, if_p,
11607 guard_tinfo);
11608 parser->in_switch_statement_p = in_switch_statement_p;
11609 parser->in_statement = in_statement;
11611 /* Now we're all done with the switch-statement. */
11612 finish_switch_stmt (statement);
11615 return statement;
11617 break;
11619 default:
11620 cp_parser_error (parser, "expected selection-statement");
11621 return error_mark_node;
11625 /* Parse a condition.
11627 condition:
11628 expression
11629 type-specifier-seq declarator = initializer-clause
11630 type-specifier-seq declarator braced-init-list
11632 GNU Extension:
11634 condition:
11635 type-specifier-seq declarator asm-specification [opt]
11636 attributes [opt] = assignment-expression
11638 Returns the expression that should be tested. */
11640 static tree
11641 cp_parser_condition (cp_parser* parser)
11643 cp_decl_specifier_seq type_specifiers;
11644 const char *saved_message;
11645 int declares_class_or_enum;
11647 /* Try the declaration first. */
11648 cp_parser_parse_tentatively (parser);
11649 /* New types are not allowed in the type-specifier-seq for a
11650 condition. */
11651 saved_message = parser->type_definition_forbidden_message;
11652 parser->type_definition_forbidden_message
11653 = G_("types may not be defined in conditions");
11654 /* Parse the type-specifier-seq. */
11655 cp_parser_decl_specifier_seq (parser,
11656 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11657 &type_specifiers,
11658 &declares_class_or_enum);
11659 /* Restore the saved message. */
11660 parser->type_definition_forbidden_message = saved_message;
11661 /* If all is well, we might be looking at a declaration. */
11662 if (!cp_parser_error_occurred (parser))
11664 tree decl;
11665 tree asm_specification;
11666 tree attributes;
11667 cp_declarator *declarator;
11668 tree initializer = NULL_TREE;
11670 /* Parse the declarator. */
11671 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11672 /*ctor_dtor_or_conv_p=*/NULL,
11673 /*parenthesized_p=*/NULL,
11674 /*member_p=*/false,
11675 /*friend_p=*/false);
11676 /* Parse the attributes. */
11677 attributes = cp_parser_attributes_opt (parser);
11678 /* Parse the asm-specification. */
11679 asm_specification = cp_parser_asm_specification_opt (parser);
11680 /* If the next token is not an `=' or '{', then we might still be
11681 looking at an expression. For example:
11683 if (A(a).x)
11685 looks like a decl-specifier-seq and a declarator -- but then
11686 there is no `=', so this is an expression. */
11687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11688 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11689 cp_parser_simulate_error (parser);
11691 /* If we did see an `=' or '{', then we are looking at a declaration
11692 for sure. */
11693 if (cp_parser_parse_definitely (parser))
11695 tree pushed_scope;
11696 bool non_constant_p;
11697 int flags = LOOKUP_ONLYCONVERTING;
11699 /* Create the declaration. */
11700 decl = start_decl (declarator, &type_specifiers,
11701 /*initialized_p=*/true,
11702 attributes, /*prefix_attributes=*/NULL_TREE,
11703 &pushed_scope);
11705 /* Parse the initializer. */
11706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11708 initializer = cp_parser_braced_list (parser, &non_constant_p);
11709 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11710 flags = 0;
11712 else
11714 /* Consume the `='. */
11715 cp_parser_require (parser, CPP_EQ, RT_EQ);
11716 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11718 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11719 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11721 /* Process the initializer. */
11722 cp_finish_decl (decl,
11723 initializer, !non_constant_p,
11724 asm_specification,
11725 flags);
11727 if (pushed_scope)
11728 pop_scope (pushed_scope);
11730 return convert_from_reference (decl);
11733 /* If we didn't even get past the declarator successfully, we are
11734 definitely not looking at a declaration. */
11735 else
11736 cp_parser_abort_tentative_parse (parser);
11738 /* Otherwise, we are looking at an expression. */
11739 return cp_parser_expression (parser);
11742 /* Parses a for-statement or range-for-statement until the closing ')',
11743 not included. */
11745 static tree
11746 cp_parser_for (cp_parser *parser, bool ivdep)
11748 tree init, scope, decl;
11749 bool is_range_for;
11751 /* Begin the for-statement. */
11752 scope = begin_for_scope (&init);
11754 /* Parse the initialization. */
11755 is_range_for = cp_parser_init_statement (parser, &decl);
11757 if (is_range_for)
11758 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11759 else
11760 return cp_parser_c_for (parser, scope, init, ivdep);
11763 static tree
11764 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11766 /* Normal for loop */
11767 tree condition = NULL_TREE;
11768 tree expression = NULL_TREE;
11769 tree stmt;
11771 stmt = begin_for_stmt (scope, init);
11772 /* The init-statement has already been parsed in
11773 cp_parser_init_statement, so no work is needed here. */
11774 finish_init_stmt (stmt);
11776 /* If there's a condition, process it. */
11777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11778 condition = cp_parser_condition (parser);
11779 else if (ivdep)
11781 cp_parser_error (parser, "missing loop condition in loop with "
11782 "%<GCC ivdep%> pragma");
11783 condition = error_mark_node;
11785 finish_for_cond (condition, stmt, ivdep);
11786 /* Look for the `;'. */
11787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11789 /* If there's an expression, process it. */
11790 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11791 expression = cp_parser_expression (parser);
11792 finish_for_expr (expression, stmt);
11794 return stmt;
11797 /* Tries to parse a range-based for-statement:
11799 range-based-for:
11800 decl-specifier-seq declarator : expression
11802 The decl-specifier-seq declarator and the `:' are already parsed by
11803 cp_parser_init_statement. If processing_template_decl it returns a
11804 newly created RANGE_FOR_STMT; if not, it is converted to a
11805 regular FOR_STMT. */
11807 static tree
11808 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11809 bool ivdep)
11811 tree stmt, range_expr;
11812 auto_vec <cxx_binding *, 16> bindings;
11813 auto_vec <tree, 16> names;
11814 tree decomp_first_name = NULL_TREE;
11815 unsigned int decomp_cnt = 0;
11817 /* Get the range declaration momentarily out of the way so that
11818 the range expression doesn't clash with it. */
11819 if (range_decl != error_mark_node)
11821 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11823 tree v = DECL_VALUE_EXPR (range_decl);
11824 /* For decomposition declaration get all of the corresponding
11825 declarations out of the way. */
11826 if (TREE_CODE (v) == ARRAY_REF
11827 && VAR_P (TREE_OPERAND (v, 0))
11828 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11830 tree d = range_decl;
11831 range_decl = TREE_OPERAND (v, 0);
11832 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11833 decomp_first_name = d;
11834 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11836 tree name = DECL_NAME (d);
11837 names.safe_push (name);
11838 bindings.safe_push (IDENTIFIER_BINDING (name));
11839 IDENTIFIER_BINDING (name)
11840 = IDENTIFIER_BINDING (name)->previous;
11844 if (names.is_empty ())
11846 tree name = DECL_NAME (range_decl);
11847 names.safe_push (name);
11848 bindings.safe_push (IDENTIFIER_BINDING (name));
11849 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11855 bool expr_non_constant_p;
11856 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11858 else
11859 range_expr = cp_parser_expression (parser);
11861 /* Put the range declaration(s) back into scope. */
11862 for (unsigned int i = 0; i < names.length (); i++)
11864 cxx_binding *binding = bindings[i];
11865 binding->previous = IDENTIFIER_BINDING (names[i]);
11866 IDENTIFIER_BINDING (names[i]) = binding;
11869 /* If in template, STMT is converted to a normal for-statement
11870 at instantiation. If not, it is done just ahead. */
11871 if (processing_template_decl)
11873 if (check_for_bare_parameter_packs (range_expr))
11874 range_expr = error_mark_node;
11875 stmt = begin_range_for_stmt (scope, init);
11876 if (ivdep)
11877 RANGE_FOR_IVDEP (stmt) = 1;
11878 finish_range_for_decl (stmt, range_decl, range_expr);
11879 if (!type_dependent_expression_p (range_expr)
11880 /* do_auto_deduction doesn't mess with template init-lists. */
11881 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11882 do_range_for_auto_deduction (range_decl, range_expr);
11884 else
11886 stmt = begin_for_stmt (scope, init);
11887 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11888 decomp_first_name, decomp_cnt, ivdep);
11890 return stmt;
11893 /* Subroutine of cp_convert_range_for: given the initializer expression,
11894 builds up the range temporary. */
11896 static tree
11897 build_range_temp (tree range_expr)
11899 tree range_type, range_temp;
11901 /* Find out the type deduced by the declaration
11902 `auto &&__range = range_expr'. */
11903 range_type = cp_build_reference_type (make_auto (), true);
11904 range_type = do_auto_deduction (range_type, range_expr,
11905 type_uses_auto (range_type));
11907 /* Create the __range variable. */
11908 range_temp = build_decl (input_location, VAR_DECL,
11909 get_identifier ("__for_range"), range_type);
11910 TREE_USED (range_temp) = 1;
11911 DECL_ARTIFICIAL (range_temp) = 1;
11913 return range_temp;
11916 /* Used by cp_parser_range_for in template context: we aren't going to
11917 do a full conversion yet, but we still need to resolve auto in the
11918 type of the for-range-declaration if present. This is basically
11919 a shortcut version of cp_convert_range_for. */
11921 static void
11922 do_range_for_auto_deduction (tree decl, tree range_expr)
11924 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11925 if (auto_node)
11927 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11928 range_temp = convert_from_reference (build_range_temp (range_expr));
11929 iter_type = (cp_parser_perform_range_for_lookup
11930 (range_temp, &begin_dummy, &end_dummy));
11931 if (iter_type)
11933 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11934 iter_type);
11935 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11936 tf_warning_or_error);
11937 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11938 iter_decl, auto_node);
11943 /* Converts a range-based for-statement into a normal
11944 for-statement, as per the definition.
11946 for (RANGE_DECL : RANGE_EXPR)
11947 BLOCK
11949 should be equivalent to:
11952 auto &&__range = RANGE_EXPR;
11953 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11954 __begin != __end;
11955 ++__begin)
11957 RANGE_DECL = *__begin;
11958 BLOCK
11962 If RANGE_EXPR is an array:
11963 BEGIN_EXPR = __range
11964 END_EXPR = __range + ARRAY_SIZE(__range)
11965 Else if RANGE_EXPR has a member 'begin' or 'end':
11966 BEGIN_EXPR = __range.begin()
11967 END_EXPR = __range.end()
11968 Else:
11969 BEGIN_EXPR = begin(__range)
11970 END_EXPR = end(__range);
11972 If __range has a member 'begin' but not 'end', or vice versa, we must
11973 still use the second alternative (it will surely fail, however).
11974 When calling begin()/end() in the third alternative we must use
11975 argument dependent lookup, but always considering 'std' as an associated
11976 namespace. */
11978 tree
11979 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11980 tree decomp_first_name, unsigned int decomp_cnt,
11981 bool ivdep)
11983 tree begin, end;
11984 tree iter_type, begin_expr, end_expr;
11985 tree condition, expression;
11987 range_expr = mark_lvalue_use (range_expr);
11989 if (range_decl == error_mark_node || range_expr == error_mark_node)
11990 /* If an error happened previously do nothing or else a lot of
11991 unhelpful errors would be issued. */
11992 begin_expr = end_expr = iter_type = error_mark_node;
11993 else
11995 tree range_temp;
11997 if (VAR_P (range_expr)
11998 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11999 /* Can't bind a reference to an array of runtime bound. */
12000 range_temp = range_expr;
12001 else
12003 range_temp = build_range_temp (range_expr);
12004 pushdecl (range_temp);
12005 cp_finish_decl (range_temp, range_expr,
12006 /*is_constant_init*/false, NULL_TREE,
12007 LOOKUP_ONLYCONVERTING);
12008 range_temp = convert_from_reference (range_temp);
12010 iter_type = cp_parser_perform_range_for_lookup (range_temp,
12011 &begin_expr, &end_expr);
12014 /* The new for initialization statement. */
12015 begin = build_decl (input_location, VAR_DECL,
12016 get_identifier ("__for_begin"), iter_type);
12017 TREE_USED (begin) = 1;
12018 DECL_ARTIFICIAL (begin) = 1;
12019 pushdecl (begin);
12020 cp_finish_decl (begin, begin_expr,
12021 /*is_constant_init*/false, NULL_TREE,
12022 LOOKUP_ONLYCONVERTING);
12024 if (cxx_dialect >= cxx17)
12025 iter_type = cv_unqualified (TREE_TYPE (end_expr));
12026 end = build_decl (input_location, VAR_DECL,
12027 get_identifier ("__for_end"), iter_type);
12028 TREE_USED (end) = 1;
12029 DECL_ARTIFICIAL (end) = 1;
12030 pushdecl (end);
12031 cp_finish_decl (end, end_expr,
12032 /*is_constant_init*/false, NULL_TREE,
12033 LOOKUP_ONLYCONVERTING);
12035 finish_init_stmt (statement);
12037 /* The new for condition. */
12038 condition = build_x_binary_op (input_location, NE_EXPR,
12039 begin, ERROR_MARK,
12040 end, ERROR_MARK,
12041 NULL, tf_warning_or_error);
12042 finish_for_cond (condition, statement, ivdep);
12044 /* The new increment expression. */
12045 expression = finish_unary_op_expr (input_location,
12046 PREINCREMENT_EXPR, begin,
12047 tf_warning_or_error);
12048 finish_for_expr (expression, statement);
12050 /* The declaration is initialized with *__begin inside the loop body. */
12051 cp_finish_decl (range_decl,
12052 build_x_indirect_ref (input_location, begin, RO_NULL,
12053 tf_warning_or_error),
12054 /*is_constant_init*/false, NULL_TREE,
12055 LOOKUP_ONLYCONVERTING);
12056 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
12057 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
12059 return statement;
12062 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12063 We need to solve both at the same time because the method used
12064 depends on the existence of members begin or end.
12065 Returns the type deduced for the iterator expression. */
12067 static tree
12068 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12070 if (error_operand_p (range))
12072 *begin = *end = error_mark_node;
12073 return error_mark_node;
12076 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12078 error ("range-based %<for%> expression of type %qT "
12079 "has incomplete type", TREE_TYPE (range));
12080 *begin = *end = error_mark_node;
12081 return error_mark_node;
12083 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12085 /* If RANGE is an array, we will use pointer arithmetic. */
12086 *begin = decay_conversion (range, tf_warning_or_error);
12087 *end = build_binary_op (input_location, PLUS_EXPR,
12088 range,
12089 array_type_nelts_top (TREE_TYPE (range)),
12090 false);
12091 return TREE_TYPE (*begin);
12093 else
12095 /* If it is not an array, we must do a bit of magic. */
12096 tree id_begin, id_end;
12097 tree member_begin, member_end;
12099 *begin = *end = error_mark_node;
12101 id_begin = get_identifier ("begin");
12102 id_end = get_identifier ("end");
12103 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12104 /*protect=*/2, /*want_type=*/false,
12105 tf_warning_or_error);
12106 member_end = lookup_member (TREE_TYPE (range), id_end,
12107 /*protect=*/2, /*want_type=*/false,
12108 tf_warning_or_error);
12110 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12112 /* Use the member functions. */
12113 if (member_begin != NULL_TREE)
12114 *begin = cp_parser_range_for_member_function (range, id_begin);
12115 else
12116 error ("range-based %<for%> expression of type %qT has an "
12117 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12119 if (member_end != NULL_TREE)
12120 *end = cp_parser_range_for_member_function (range, id_end);
12121 else
12122 error ("range-based %<for%> expression of type %qT has a "
12123 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12125 else
12127 /* Use global functions with ADL. */
12128 vec<tree, va_gc> *vec;
12129 vec = make_tree_vector ();
12131 vec_safe_push (vec, range);
12133 member_begin = perform_koenig_lookup (id_begin, vec,
12134 tf_warning_or_error);
12135 *begin = finish_call_expr (member_begin, &vec, false, true,
12136 tf_warning_or_error);
12137 member_end = perform_koenig_lookup (id_end, vec,
12138 tf_warning_or_error);
12139 *end = finish_call_expr (member_end, &vec, false, true,
12140 tf_warning_or_error);
12142 release_tree_vector (vec);
12145 /* Last common checks. */
12146 if (*begin == error_mark_node || *end == error_mark_node)
12148 /* If one of the expressions is an error do no more checks. */
12149 *begin = *end = error_mark_node;
12150 return error_mark_node;
12152 else if (type_dependent_expression_p (*begin)
12153 || type_dependent_expression_p (*end))
12154 /* Can happen, when, eg, in a template context, Koenig lookup
12155 can't resolve begin/end (c++/58503). */
12156 return NULL_TREE;
12157 else
12159 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12160 /* The unqualified type of the __begin and __end temporaries should
12161 be the same, as required by the multiple auto declaration. */
12162 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12164 if (cxx_dialect >= cxx17
12165 && (build_x_binary_op (input_location, NE_EXPR,
12166 *begin, ERROR_MARK,
12167 *end, ERROR_MARK,
12168 NULL, tf_none)
12169 != error_mark_node))
12170 /* P0184R0 allows __begin and __end to have different types,
12171 but make sure they are comparable so we can give a better
12172 diagnostic. */;
12173 else
12174 error ("inconsistent begin/end types in range-based %<for%> "
12175 "statement: %qT and %qT",
12176 TREE_TYPE (*begin), TREE_TYPE (*end));
12178 return iter_type;
12183 /* Helper function for cp_parser_perform_range_for_lookup.
12184 Builds a tree for RANGE.IDENTIFIER(). */
12186 static tree
12187 cp_parser_range_for_member_function (tree range, tree identifier)
12189 tree member, res;
12190 vec<tree, va_gc> *vec;
12192 member = finish_class_member_access_expr (range, identifier,
12193 false, tf_warning_or_error);
12194 if (member == error_mark_node)
12195 return error_mark_node;
12197 vec = make_tree_vector ();
12198 res = finish_call_expr (member, &vec,
12199 /*disallow_virtual=*/false,
12200 /*koenig_p=*/false,
12201 tf_warning_or_error);
12202 release_tree_vector (vec);
12203 return res;
12206 /* Parse an iteration-statement.
12208 iteration-statement:
12209 while ( condition ) statement
12210 do statement while ( expression ) ;
12211 for ( init-statement condition [opt] ; expression [opt] )
12212 statement
12214 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12216 static tree
12217 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12219 cp_token *token;
12220 enum rid keyword;
12221 tree statement;
12222 unsigned char in_statement;
12223 token_indent_info guard_tinfo;
12225 /* Peek at the next token. */
12226 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12227 if (!token)
12228 return error_mark_node;
12230 guard_tinfo = get_token_indent_info (token);
12232 /* Remember whether or not we are already within an iteration
12233 statement. */
12234 in_statement = parser->in_statement;
12236 /* See what kind of keyword it is. */
12237 keyword = token->keyword;
12238 switch (keyword)
12240 case RID_WHILE:
12242 tree condition;
12244 /* Begin the while-statement. */
12245 statement = begin_while_stmt ();
12246 /* Look for the `('. */
12247 matching_parens parens;
12248 parens.require_open (parser);
12249 /* Parse the condition. */
12250 condition = cp_parser_condition (parser);
12251 finish_while_stmt_cond (condition, statement, ivdep);
12252 /* Look for the `)'. */
12253 parens.require_close (parser);
12254 /* Parse the dependent statement. */
12255 parser->in_statement = IN_ITERATION_STMT;
12256 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12257 parser->in_statement = in_statement;
12258 /* We're done with the while-statement. */
12259 finish_while_stmt (statement);
12261 break;
12263 case RID_DO:
12265 tree expression;
12267 /* Begin the do-statement. */
12268 statement = begin_do_stmt ();
12269 /* Parse the body of the do-statement. */
12270 parser->in_statement = IN_ITERATION_STMT;
12271 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12272 parser->in_statement = in_statement;
12273 finish_do_body (statement);
12274 /* Look for the `while' keyword. */
12275 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12276 /* Look for the `('. */
12277 matching_parens parens;
12278 parens.require_open (parser);
12279 /* Parse the expression. */
12280 expression = cp_parser_expression (parser);
12281 /* We're done with the do-statement. */
12282 finish_do_stmt (expression, statement, ivdep);
12283 /* Look for the `)'. */
12284 parens.require_close (parser);
12285 /* Look for the `;'. */
12286 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12288 break;
12290 case RID_FOR:
12292 /* Look for the `('. */
12293 matching_parens parens;
12294 parens.require_open (parser);
12296 statement = cp_parser_for (parser, ivdep);
12298 /* Look for the `)'. */
12299 parens.require_close (parser);
12301 /* Parse the body of the for-statement. */
12302 parser->in_statement = IN_ITERATION_STMT;
12303 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12304 parser->in_statement = in_statement;
12306 /* We're done with the for-statement. */
12307 finish_for_stmt (statement);
12309 break;
12311 default:
12312 cp_parser_error (parser, "expected iteration-statement");
12313 statement = error_mark_node;
12314 break;
12317 return statement;
12320 /* Parse a init-statement or the declarator of a range-based-for.
12321 Returns true if a range-based-for declaration is seen.
12323 init-statement:
12324 expression-statement
12325 simple-declaration */
12327 static bool
12328 cp_parser_init_statement (cp_parser* parser, tree *decl)
12330 /* If the next token is a `;', then we have an empty
12331 expression-statement. Grammatically, this is also a
12332 simple-declaration, but an invalid one, because it does not
12333 declare anything. Therefore, if we did not handle this case
12334 specially, we would issue an error message about an invalid
12335 declaration. */
12336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12338 bool is_range_for = false;
12339 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12341 /* A colon is used in range-based for. */
12342 parser->colon_corrects_to_scope_p = false;
12344 /* We're going to speculatively look for a declaration, falling back
12345 to an expression, if necessary. */
12346 cp_parser_parse_tentatively (parser);
12347 /* Parse the declaration. */
12348 cp_parser_simple_declaration (parser,
12349 /*function_definition_allowed_p=*/false,
12350 decl);
12351 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12352 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12354 /* It is a range-for, consume the ':' */
12355 cp_lexer_consume_token (parser->lexer);
12356 is_range_for = true;
12357 if (cxx_dialect < cxx11)
12359 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12360 "range-based %<for%> loops only available with "
12361 "-std=c++11 or -std=gnu++11");
12362 *decl = error_mark_node;
12365 else
12366 /* The ';' is not consumed yet because we told
12367 cp_parser_simple_declaration not to. */
12368 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12370 if (cp_parser_parse_definitely (parser))
12371 return is_range_for;
12372 /* If the tentative parse failed, then we shall need to look for an
12373 expression-statement. */
12375 /* If we are here, it is an expression-statement. */
12376 cp_parser_expression_statement (parser, NULL_TREE);
12377 return false;
12380 /* Parse a jump-statement.
12382 jump-statement:
12383 break ;
12384 continue ;
12385 return expression [opt] ;
12386 return braced-init-list ;
12387 goto identifier ;
12389 GNU extension:
12391 jump-statement:
12392 goto * expression ;
12394 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12396 static tree
12397 cp_parser_jump_statement (cp_parser* parser)
12399 tree statement = error_mark_node;
12400 cp_token *token;
12401 enum rid keyword;
12402 unsigned char in_statement;
12404 /* Peek at the next token. */
12405 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12406 if (!token)
12407 return error_mark_node;
12409 /* See what kind of keyword it is. */
12410 keyword = token->keyword;
12411 switch (keyword)
12413 case RID_BREAK:
12414 in_statement = parser->in_statement & ~IN_IF_STMT;
12415 switch (in_statement)
12417 case 0:
12418 error_at (token->location, "break statement not within loop or switch");
12419 break;
12420 default:
12421 gcc_assert ((in_statement & IN_SWITCH_STMT)
12422 || in_statement == IN_ITERATION_STMT);
12423 statement = finish_break_stmt ();
12424 if (in_statement == IN_ITERATION_STMT)
12425 break_maybe_infinite_loop ();
12426 break;
12427 case IN_OMP_BLOCK:
12428 error_at (token->location, "invalid exit from OpenMP structured block");
12429 break;
12430 case IN_OMP_FOR:
12431 error_at (token->location, "break statement used with OpenMP for loop");
12432 break;
12433 case IN_CILK_SIMD_FOR:
12434 error_at (token->location, "break statement used with Cilk Plus for loop");
12435 break;
12437 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12438 break;
12440 case RID_CONTINUE:
12441 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12443 case 0:
12444 error_at (token->location, "continue statement not within a loop");
12445 break;
12446 case IN_CILK_SIMD_FOR:
12447 error_at (token->location,
12448 "continue statement within %<#pragma simd%> loop body");
12449 /* Fall through. */
12450 case IN_ITERATION_STMT:
12451 case IN_OMP_FOR:
12452 statement = finish_continue_stmt ();
12453 break;
12454 case IN_OMP_BLOCK:
12455 error_at (token->location, "invalid exit from OpenMP structured block");
12456 break;
12457 default:
12458 gcc_unreachable ();
12460 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12461 break;
12463 case RID_RETURN:
12465 tree expr;
12466 bool expr_non_constant_p;
12468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12470 cp_lexer_set_source_position (parser->lexer);
12471 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12472 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12474 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12475 expr = cp_parser_expression (parser);
12476 else
12477 /* If the next token is a `;', then there is no
12478 expression. */
12479 expr = NULL_TREE;
12480 /* Build the return-statement. */
12481 if (current_function_auto_return_pattern && in_discarded_stmt)
12482 /* Don't deduce from a discarded return statement. */;
12483 else
12484 statement = finish_return_stmt (expr);
12485 /* Look for the final `;'. */
12486 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12488 break;
12490 case RID_GOTO:
12491 if (parser->in_function_body
12492 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12494 error ("%<goto%> in %<constexpr%> function");
12495 cp_function_chain->invalid_constexpr = true;
12498 /* Create the goto-statement. */
12499 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12501 /* Issue a warning about this use of a GNU extension. */
12502 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12503 /* Consume the '*' token. */
12504 cp_lexer_consume_token (parser->lexer);
12505 /* Parse the dependent expression. */
12506 finish_goto_stmt (cp_parser_expression (parser));
12508 else
12509 finish_goto_stmt (cp_parser_identifier (parser));
12510 /* Look for the final `;'. */
12511 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12512 break;
12514 default:
12515 cp_parser_error (parser, "expected jump-statement");
12516 break;
12519 return statement;
12522 /* Parse a declaration-statement.
12524 declaration-statement:
12525 block-declaration */
12527 static void
12528 cp_parser_declaration_statement (cp_parser* parser)
12530 void *p;
12532 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12533 p = obstack_alloc (&declarator_obstack, 0);
12535 /* Parse the block-declaration. */
12536 cp_parser_block_declaration (parser, /*statement_p=*/true);
12538 /* Free any declarators allocated. */
12539 obstack_free (&declarator_obstack, p);
12542 /* Some dependent statements (like `if (cond) statement'), are
12543 implicitly in their own scope. In other words, if the statement is
12544 a single statement (as opposed to a compound-statement), it is
12545 none-the-less treated as if it were enclosed in braces. Any
12546 declarations appearing in the dependent statement are out of scope
12547 after control passes that point. This function parses a statement,
12548 but ensures that is in its own scope, even if it is not a
12549 compound-statement.
12551 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12552 is a (possibly labeled) if statement which is not enclosed in
12553 braces and has an else clause. This is used to implement
12554 -Wparentheses.
12556 CHAIN is a vector of if-else-if conditions. This is used to implement
12557 -Wduplicated-cond.
12559 Returns the new statement. */
12561 static tree
12562 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12563 const token_indent_info &guard_tinfo,
12564 vec<tree> *chain)
12566 tree statement;
12567 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12568 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12569 token_indent_info body_tinfo
12570 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12572 if (if_p != NULL)
12573 *if_p = false;
12575 /* Mark if () ; with a special NOP_EXPR. */
12576 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12578 cp_lexer_consume_token (parser->lexer);
12579 statement = add_stmt (build_empty_stmt (body_loc));
12581 if (guard_tinfo.keyword == RID_IF
12582 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12583 warning_at (body_loc, OPT_Wempty_body,
12584 "suggest braces around empty body in an %<if%> statement");
12585 else if (guard_tinfo.keyword == RID_ELSE)
12586 warning_at (body_loc, OPT_Wempty_body,
12587 "suggest braces around empty body in an %<else%> statement");
12589 /* if a compound is opened, we simply parse the statement directly. */
12590 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12591 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12592 /* If the token is not a `{', then we must take special action. */
12593 else
12595 /* Create a compound-statement. */
12596 statement = begin_compound_stmt (0);
12597 /* Parse the dependent-statement. */
12598 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12599 &body_loc_after_labels);
12600 /* Finish the dummy compound-statement. */
12601 finish_compound_stmt (statement);
12604 token_indent_info next_tinfo
12605 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12606 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12608 if (body_loc_after_labels != UNKNOWN_LOCATION
12609 && next_tinfo.type != CPP_SEMICOLON)
12610 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12611 guard_tinfo.location, guard_tinfo.keyword);
12613 /* Return the statement. */
12614 return statement;
12617 /* For some dependent statements (like `while (cond) statement'), we
12618 have already created a scope. Therefore, even if the dependent
12619 statement is a compound-statement, we do not want to create another
12620 scope. */
12622 static void
12623 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12624 const token_indent_info &guard_tinfo)
12626 /* If the token is a `{', then we must take special action. */
12627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12629 token_indent_info body_tinfo
12630 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12631 location_t loc_after_labels = UNKNOWN_LOCATION;
12633 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12634 &loc_after_labels);
12635 token_indent_info next_tinfo
12636 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12637 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12639 if (loc_after_labels != UNKNOWN_LOCATION
12640 && next_tinfo.type != CPP_SEMICOLON)
12641 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12642 guard_tinfo.location,
12643 guard_tinfo.keyword);
12645 else
12647 /* Avoid calling cp_parser_compound_statement, so that we
12648 don't create a new scope. Do everything else by hand. */
12649 matching_braces braces;
12650 braces.require_open (parser);
12651 /* If the next keyword is `__label__' we have a label declaration. */
12652 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12653 cp_parser_label_declaration (parser);
12654 /* Parse an (optional) statement-seq. */
12655 cp_parser_statement_seq_opt (parser, NULL_TREE);
12656 braces.require_close (parser);
12660 /* Declarations [gram.dcl.dcl] */
12662 /* Parse an optional declaration-sequence.
12664 declaration-seq:
12665 declaration
12666 declaration-seq declaration */
12668 static void
12669 cp_parser_declaration_seq_opt (cp_parser* parser)
12671 while (true)
12673 cp_token *token;
12675 token = cp_lexer_peek_token (parser->lexer);
12677 if (token->type == CPP_CLOSE_BRACE
12678 || token->type == CPP_EOF
12679 || token->type == CPP_PRAGMA_EOL)
12680 break;
12682 if (token->type == CPP_SEMICOLON)
12684 /* A declaration consisting of a single semicolon is
12685 invalid. Allow it unless we're being pedantic. */
12686 cp_lexer_consume_token (parser->lexer);
12687 if (!in_system_header_at (input_location))
12688 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12689 continue;
12692 /* If we're entering or exiting a region that's implicitly
12693 extern "C", modify the lang context appropriately. */
12694 if (!parser->implicit_extern_c && token->implicit_extern_c)
12696 push_lang_context (lang_name_c);
12697 parser->implicit_extern_c = true;
12699 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12701 pop_lang_context ();
12702 parser->implicit_extern_c = false;
12705 if (token->type == CPP_PRAGMA)
12707 /* A top-level declaration can consist solely of a #pragma.
12708 A nested declaration cannot, so this is done here and not
12709 in cp_parser_declaration. (A #pragma at block scope is
12710 handled in cp_parser_statement.) */
12711 cp_parser_pragma (parser, pragma_external, NULL);
12712 continue;
12715 /* Parse the declaration itself. */
12716 cp_parser_declaration (parser);
12720 /* Parse a declaration.
12722 declaration:
12723 block-declaration
12724 function-definition
12725 template-declaration
12726 explicit-instantiation
12727 explicit-specialization
12728 linkage-specification
12729 namespace-definition
12731 C++17:
12732 deduction-guide
12734 GNU extension:
12736 declaration:
12737 __extension__ declaration */
12739 static void
12740 cp_parser_declaration (cp_parser* parser)
12742 cp_token token1;
12743 cp_token token2;
12744 int saved_pedantic;
12745 void *p;
12746 tree attributes = NULL_TREE;
12748 /* Check for the `__extension__' keyword. */
12749 if (cp_parser_extension_opt (parser, &saved_pedantic))
12751 /* Parse the qualified declaration. */
12752 cp_parser_declaration (parser);
12753 /* Restore the PEDANTIC flag. */
12754 pedantic = saved_pedantic;
12756 return;
12759 /* Try to figure out what kind of declaration is present. */
12760 token1 = *cp_lexer_peek_token (parser->lexer);
12762 if (token1.type != CPP_EOF)
12763 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12764 else
12766 token2.type = CPP_EOF;
12767 token2.keyword = RID_MAX;
12770 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12771 p = obstack_alloc (&declarator_obstack, 0);
12773 /* If the next token is `extern' and the following token is a string
12774 literal, then we have a linkage specification. */
12775 if (token1.keyword == RID_EXTERN
12776 && cp_parser_is_pure_string_literal (&token2))
12777 cp_parser_linkage_specification (parser);
12778 /* If the next token is `template', then we have either a template
12779 declaration, an explicit instantiation, or an explicit
12780 specialization. */
12781 else if (token1.keyword == RID_TEMPLATE)
12783 /* `template <>' indicates a template specialization. */
12784 if (token2.type == CPP_LESS
12785 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12786 cp_parser_explicit_specialization (parser);
12787 /* `template <' indicates a template declaration. */
12788 else if (token2.type == CPP_LESS)
12789 cp_parser_template_declaration (parser, /*member_p=*/false);
12790 /* Anything else must be an explicit instantiation. */
12791 else
12792 cp_parser_explicit_instantiation (parser);
12794 /* If the next token is `export', then we have a template
12795 declaration. */
12796 else if (token1.keyword == RID_EXPORT)
12797 cp_parser_template_declaration (parser, /*member_p=*/false);
12798 /* If the next token is `extern', 'static' or 'inline' and the one
12799 after that is `template', we have a GNU extended explicit
12800 instantiation directive. */
12801 else if (cp_parser_allow_gnu_extensions_p (parser)
12802 && (token1.keyword == RID_EXTERN
12803 || token1.keyword == RID_STATIC
12804 || token1.keyword == RID_INLINE)
12805 && token2.keyword == RID_TEMPLATE)
12806 cp_parser_explicit_instantiation (parser);
12807 /* If the next token is `namespace', check for a named or unnamed
12808 namespace definition. */
12809 else if (token1.keyword == RID_NAMESPACE
12810 && (/* A named namespace definition. */
12811 (token2.type == CPP_NAME
12812 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12813 != CPP_EQ))
12814 || (token2.type == CPP_OPEN_SQUARE
12815 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12816 == CPP_OPEN_SQUARE)
12817 /* An unnamed namespace definition. */
12818 || token2.type == CPP_OPEN_BRACE
12819 || token2.keyword == RID_ATTRIBUTE))
12820 cp_parser_namespace_definition (parser);
12821 /* An inline (associated) namespace definition. */
12822 else if (token1.keyword == RID_INLINE
12823 && token2.keyword == RID_NAMESPACE)
12824 cp_parser_namespace_definition (parser);
12825 /* Objective-C++ declaration/definition. */
12826 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12827 cp_parser_objc_declaration (parser, NULL_TREE);
12828 else if (c_dialect_objc ()
12829 && token1.keyword == RID_ATTRIBUTE
12830 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12831 cp_parser_objc_declaration (parser, attributes);
12832 /* At this point we may have a template declared by a concept
12833 introduction. */
12834 else if (flag_concepts
12835 && cp_parser_template_declaration_after_export (parser,
12836 /*member_p=*/false))
12837 /* We did. */;
12838 else
12839 /* Try to parse a block-declaration, or a function-definition. */
12840 cp_parser_block_declaration (parser, /*statement_p=*/false);
12842 /* Free any declarators allocated. */
12843 obstack_free (&declarator_obstack, p);
12846 /* Parse a block-declaration.
12848 block-declaration:
12849 simple-declaration
12850 asm-definition
12851 namespace-alias-definition
12852 using-declaration
12853 using-directive
12855 GNU Extension:
12857 block-declaration:
12858 __extension__ block-declaration
12860 C++0x Extension:
12862 block-declaration:
12863 static_assert-declaration
12865 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12866 part of a declaration-statement. */
12868 static void
12869 cp_parser_block_declaration (cp_parser *parser,
12870 bool statement_p)
12872 cp_token *token1;
12873 int saved_pedantic;
12875 /* Check for the `__extension__' keyword. */
12876 if (cp_parser_extension_opt (parser, &saved_pedantic))
12878 /* Parse the qualified declaration. */
12879 cp_parser_block_declaration (parser, statement_p);
12880 /* Restore the PEDANTIC flag. */
12881 pedantic = saved_pedantic;
12883 return;
12886 /* Peek at the next token to figure out which kind of declaration is
12887 present. */
12888 token1 = cp_lexer_peek_token (parser->lexer);
12890 /* If the next keyword is `asm', we have an asm-definition. */
12891 if (token1->keyword == RID_ASM)
12893 if (statement_p)
12894 cp_parser_commit_to_tentative_parse (parser);
12895 cp_parser_asm_definition (parser);
12897 /* If the next keyword is `namespace', we have a
12898 namespace-alias-definition. */
12899 else if (token1->keyword == RID_NAMESPACE)
12900 cp_parser_namespace_alias_definition (parser);
12901 /* If the next keyword is `using', we have a
12902 using-declaration, a using-directive, or an alias-declaration. */
12903 else if (token1->keyword == RID_USING)
12905 cp_token *token2;
12907 if (statement_p)
12908 cp_parser_commit_to_tentative_parse (parser);
12909 /* If the token after `using' is `namespace', then we have a
12910 using-directive. */
12911 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12912 if (token2->keyword == RID_NAMESPACE)
12913 cp_parser_using_directive (parser);
12914 /* If the second token after 'using' is '=', then we have an
12915 alias-declaration. */
12916 else if (cxx_dialect >= cxx11
12917 && token2->type == CPP_NAME
12918 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12919 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12920 cp_parser_alias_declaration (parser);
12921 /* Otherwise, it's a using-declaration. */
12922 else
12923 cp_parser_using_declaration (parser,
12924 /*access_declaration_p=*/false);
12926 /* If the next keyword is `__label__' we have a misplaced label
12927 declaration. */
12928 else if (token1->keyword == RID_LABEL)
12930 cp_lexer_consume_token (parser->lexer);
12931 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12932 cp_parser_skip_to_end_of_statement (parser);
12933 /* If the next token is now a `;', consume it. */
12934 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12935 cp_lexer_consume_token (parser->lexer);
12937 /* If the next token is `static_assert' we have a static assertion. */
12938 else if (token1->keyword == RID_STATIC_ASSERT)
12939 cp_parser_static_assert (parser, /*member_p=*/false);
12940 /* Anything else must be a simple-declaration. */
12941 else
12942 cp_parser_simple_declaration (parser, !statement_p,
12943 /*maybe_range_for_decl*/NULL);
12946 /* Parse a simple-declaration.
12948 simple-declaration:
12949 decl-specifier-seq [opt] init-declarator-list [opt] ;
12950 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12951 brace-or-equal-initializer ;
12953 init-declarator-list:
12954 init-declarator
12955 init-declarator-list , init-declarator
12957 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12958 function-definition as a simple-declaration.
12960 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12961 parsed declaration if it is an uninitialized single declarator not followed
12962 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12963 if present, will not be consumed. */
12965 static void
12966 cp_parser_simple_declaration (cp_parser* parser,
12967 bool function_definition_allowed_p,
12968 tree *maybe_range_for_decl)
12970 cp_decl_specifier_seq decl_specifiers;
12971 int declares_class_or_enum;
12972 bool saw_declarator;
12973 location_t comma_loc = UNKNOWN_LOCATION;
12974 location_t init_loc = UNKNOWN_LOCATION;
12976 if (maybe_range_for_decl)
12977 *maybe_range_for_decl = NULL_TREE;
12979 /* Defer access checks until we know what is being declared; the
12980 checks for names appearing in the decl-specifier-seq should be
12981 done as if we were in the scope of the thing being declared. */
12982 push_deferring_access_checks (dk_deferred);
12984 /* Parse the decl-specifier-seq. We have to keep track of whether
12985 or not the decl-specifier-seq declares a named class or
12986 enumeration type, since that is the only case in which the
12987 init-declarator-list is allowed to be empty.
12989 [dcl.dcl]
12991 In a simple-declaration, the optional init-declarator-list can be
12992 omitted only when declaring a class or enumeration, that is when
12993 the decl-specifier-seq contains either a class-specifier, an
12994 elaborated-type-specifier, or an enum-specifier. */
12995 cp_parser_decl_specifier_seq (parser,
12996 CP_PARSER_FLAGS_OPTIONAL,
12997 &decl_specifiers,
12998 &declares_class_or_enum);
12999 /* We no longer need to defer access checks. */
13000 stop_deferring_access_checks ();
13002 /* In a block scope, a valid declaration must always have a
13003 decl-specifier-seq. By not trying to parse declarators, we can
13004 resolve the declaration/expression ambiguity more quickly. */
13005 if (!function_definition_allowed_p
13006 && !decl_specifiers.any_specifiers_p)
13008 cp_parser_error (parser, "expected declaration");
13009 goto done;
13012 /* If the next two tokens are both identifiers, the code is
13013 erroneous. The usual cause of this situation is code like:
13015 T t;
13017 where "T" should name a type -- but does not. */
13018 if (!decl_specifiers.any_type_specifiers_p
13019 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13021 /* If parsing tentatively, we should commit; we really are
13022 looking at a declaration. */
13023 cp_parser_commit_to_tentative_parse (parser);
13024 /* Give up. */
13025 goto done;
13028 /* If we have seen at least one decl-specifier, and the next token
13029 is not a parenthesis, then we must be looking at a declaration.
13030 (After "int (" we might be looking at a functional cast.) */
13031 if (decl_specifiers.any_specifiers_p
13032 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13033 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13034 && !cp_parser_error_occurred (parser))
13035 cp_parser_commit_to_tentative_parse (parser);
13037 /* Look for C++17 decomposition declaration. */
13038 for (size_t n = 1; ; n++)
13039 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
13040 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
13041 continue;
13042 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
13043 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
13044 && decl_specifiers.any_specifiers_p)
13046 tree decl
13047 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
13048 maybe_range_for_decl,
13049 &init_loc);
13051 /* The next token should be either a `,' or a `;'. */
13052 cp_token *token = cp_lexer_peek_token (parser->lexer);
13053 /* If it's a `;', we are done. */
13054 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
13055 goto finish;
13056 /* Anything else is an error. */
13057 else
13059 /* If we have already issued an error message we don't need
13060 to issue another one. */
13061 if ((decl != error_mark_node
13062 && DECL_INITIAL (decl) != error_mark_node)
13063 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13064 cp_parser_error (parser, "expected %<,%> or %<;%>");
13065 /* Skip tokens until we reach the end of the statement. */
13066 cp_parser_skip_to_end_of_statement (parser);
13067 /* If the next token is now a `;', consume it. */
13068 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13069 cp_lexer_consume_token (parser->lexer);
13070 goto done;
13073 else
13074 break;
13076 tree last_type;
13077 bool auto_specifier_p;
13078 /* NULL_TREE if both variable and function declaration are allowed,
13079 error_mark_node if function declaration are not allowed and
13080 a FUNCTION_DECL that should be diagnosed if it is followed by
13081 variable declarations. */
13082 tree auto_function_declaration;
13084 last_type = NULL_TREE;
13085 auto_specifier_p
13086 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13087 auto_function_declaration = NULL_TREE;
13089 /* Keep going until we hit the `;' at the end of the simple
13090 declaration. */
13091 saw_declarator = false;
13092 while (cp_lexer_next_token_is_not (parser->lexer,
13093 CPP_SEMICOLON))
13095 cp_token *token;
13096 bool function_definition_p;
13097 tree decl;
13098 tree auto_result = NULL_TREE;
13100 if (saw_declarator)
13102 /* If we are processing next declarator, comma is expected */
13103 token = cp_lexer_peek_token (parser->lexer);
13104 gcc_assert (token->type == CPP_COMMA);
13105 cp_lexer_consume_token (parser->lexer);
13106 if (maybe_range_for_decl)
13108 *maybe_range_for_decl = error_mark_node;
13109 if (comma_loc == UNKNOWN_LOCATION)
13110 comma_loc = token->location;
13113 else
13114 saw_declarator = true;
13116 /* Parse the init-declarator. */
13117 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13118 /*checks=*/NULL,
13119 function_definition_allowed_p,
13120 /*member_p=*/false,
13121 declares_class_or_enum,
13122 &function_definition_p,
13123 maybe_range_for_decl,
13124 &init_loc,
13125 &auto_result);
13126 /* If an error occurred while parsing tentatively, exit quickly.
13127 (That usually happens when in the body of a function; each
13128 statement is treated as a declaration-statement until proven
13129 otherwise.) */
13130 if (cp_parser_error_occurred (parser))
13131 goto done;
13133 if (auto_specifier_p && cxx_dialect >= cxx14)
13135 /* If the init-declarator-list contains more than one
13136 init-declarator, they shall all form declarations of
13137 variables. */
13138 if (auto_function_declaration == NULL_TREE)
13139 auto_function_declaration
13140 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13141 else if (TREE_CODE (decl) == FUNCTION_DECL
13142 || auto_function_declaration != error_mark_node)
13144 error_at (decl_specifiers.locations[ds_type_spec],
13145 "non-variable %qD in declaration with more than one "
13146 "declarator with placeholder type",
13147 TREE_CODE (decl) == FUNCTION_DECL
13148 ? decl : auto_function_declaration);
13149 auto_function_declaration = error_mark_node;
13153 if (auto_result
13154 && (!processing_template_decl || !type_uses_auto (auto_result)))
13156 if (last_type
13157 && last_type != error_mark_node
13158 && !same_type_p (auto_result, last_type))
13160 /* If the list of declarators contains more than one declarator,
13161 the type of each declared variable is determined as described
13162 above. If the type deduced for the template parameter U is not
13163 the same in each deduction, the program is ill-formed. */
13164 error_at (decl_specifiers.locations[ds_type_spec],
13165 "inconsistent deduction for %qT: %qT and then %qT",
13166 decl_specifiers.type, last_type, auto_result);
13167 last_type = error_mark_node;
13169 else
13170 last_type = auto_result;
13173 /* Handle function definitions specially. */
13174 if (function_definition_p)
13176 /* If the next token is a `,', then we are probably
13177 processing something like:
13179 void f() {}, *p;
13181 which is erroneous. */
13182 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13184 cp_token *token = cp_lexer_peek_token (parser->lexer);
13185 error_at (token->location,
13186 "mixing"
13187 " declarations and function-definitions is forbidden");
13189 /* Otherwise, we're done with the list of declarators. */
13190 else
13192 pop_deferring_access_checks ();
13193 return;
13196 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13197 *maybe_range_for_decl = decl;
13198 /* The next token should be either a `,' or a `;'. */
13199 token = cp_lexer_peek_token (parser->lexer);
13200 /* If it's a `,', there are more declarators to come. */
13201 if (token->type == CPP_COMMA)
13202 /* will be consumed next time around */;
13203 /* If it's a `;', we are done. */
13204 else if (token->type == CPP_SEMICOLON)
13205 break;
13206 else if (maybe_range_for_decl)
13208 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13209 permerror (decl_specifiers.locations[ds_type_spec],
13210 "types may not be defined in a for-range-declaration");
13211 break;
13213 /* Anything else is an error. */
13214 else
13216 /* If we have already issued an error message we don't need
13217 to issue another one. */
13218 if ((decl != error_mark_node
13219 && DECL_INITIAL (decl) != error_mark_node)
13220 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13221 cp_parser_error (parser, "expected %<,%> or %<;%>");
13222 /* Skip tokens until we reach the end of the statement. */
13223 cp_parser_skip_to_end_of_statement (parser);
13224 /* If the next token is now a `;', consume it. */
13225 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13226 cp_lexer_consume_token (parser->lexer);
13227 goto done;
13229 /* After the first time around, a function-definition is not
13230 allowed -- even if it was OK at first. For example:
13232 int i, f() {}
13234 is not valid. */
13235 function_definition_allowed_p = false;
13238 /* Issue an error message if no declarators are present, and the
13239 decl-specifier-seq does not itself declare a class or
13240 enumeration: [dcl.dcl]/3. */
13241 if (!saw_declarator)
13243 if (cp_parser_declares_only_class_p (parser))
13245 if (!declares_class_or_enum
13246 && decl_specifiers.type
13247 && OVERLOAD_TYPE_P (decl_specifiers.type))
13248 /* Ensure an error is issued anyway when finish_decltype_type,
13249 called via cp_parser_decl_specifier_seq, returns a class or
13250 an enumeration (c++/51786). */
13251 decl_specifiers.type = NULL_TREE;
13252 shadow_tag (&decl_specifiers);
13254 /* Perform any deferred access checks. */
13255 perform_deferred_access_checks (tf_warning_or_error);
13258 /* Consume the `;'. */
13259 finish:
13260 if (!maybe_range_for_decl)
13261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13262 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13264 if (init_loc != UNKNOWN_LOCATION)
13265 error_at (init_loc, "initializer in range-based %<for%> loop");
13266 if (comma_loc != UNKNOWN_LOCATION)
13267 error_at (comma_loc,
13268 "multiple declarations in range-based %<for%> loop");
13271 done:
13272 pop_deferring_access_checks ();
13275 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13276 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13277 initializer ; */
13279 static tree
13280 cp_parser_decomposition_declaration (cp_parser *parser,
13281 cp_decl_specifier_seq *decl_specifiers,
13282 tree *maybe_range_for_decl,
13283 location_t *init_loc)
13285 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13286 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13287 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13289 /* Parse the identifier-list. */
13290 auto_vec<cp_expr, 10> v;
13291 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13292 while (true)
13294 cp_expr e = cp_parser_identifier (parser);
13295 if (e.get_value () == error_mark_node)
13296 break;
13297 v.safe_push (e);
13298 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13299 break;
13300 cp_lexer_consume_token (parser->lexer);
13303 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13304 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13306 end_loc = UNKNOWN_LOCATION;
13307 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13308 false);
13309 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13310 cp_lexer_consume_token (parser->lexer);
13311 else
13313 cp_parser_skip_to_end_of_statement (parser);
13314 return error_mark_node;
13318 if (cxx_dialect < cxx17)
13319 pedwarn (loc, 0, "structured bindings only available with "
13320 "-std=c++17 or -std=gnu++17");
13322 tree pushed_scope;
13323 cp_declarator *declarator = make_declarator (cdk_decomp);
13324 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13325 declarator->id_loc = loc;
13326 if (ref_qual != REF_QUAL_NONE)
13327 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13328 ref_qual == REF_QUAL_RVALUE,
13329 NULL_TREE);
13330 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13331 NULL_TREE, decl_specifiers->attributes,
13332 &pushed_scope);
13333 tree orig_decl = decl;
13335 unsigned int i;
13336 cp_expr e;
13337 cp_decl_specifier_seq decl_specs;
13338 clear_decl_specs (&decl_specs);
13339 decl_specs.type = make_auto ();
13340 tree prev = decl;
13341 FOR_EACH_VEC_ELT (v, i, e)
13343 if (i == 0)
13344 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13345 else
13346 declarator->u.id.unqualified_name = e.get_value ();
13347 declarator->id_loc = e.get_location ();
13348 tree elt_pushed_scope;
13349 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13350 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13351 if (decl2 == error_mark_node)
13352 decl = error_mark_node;
13353 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13355 /* Ensure we've diagnosed redeclaration if we aren't creating
13356 a new VAR_DECL. */
13357 gcc_assert (errorcount);
13358 decl = error_mark_node;
13360 else
13361 prev = decl2;
13362 if (elt_pushed_scope)
13363 pop_scope (elt_pushed_scope);
13366 if (v.is_empty ())
13368 error_at (loc, "empty structured binding declaration");
13369 decl = error_mark_node;
13372 if (maybe_range_for_decl == NULL
13373 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13375 bool non_constant_p = false, is_direct_init = false;
13376 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13377 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13378 &non_constant_p);
13379 if (initializer == NULL_TREE
13380 || (TREE_CODE (initializer) == TREE_LIST
13381 && TREE_CHAIN (initializer))
13382 || (TREE_CODE (initializer) == CONSTRUCTOR
13383 && CONSTRUCTOR_NELTS (initializer) != 1))
13385 error_at (loc, "invalid initializer for structured binding "
13386 "declaration");
13387 initializer = error_mark_node;
13390 if (decl != error_mark_node)
13392 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13393 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13394 cp_finish_decomp (decl, prev, v.length ());
13397 else if (decl != error_mark_node)
13399 *maybe_range_for_decl = prev;
13400 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13401 the underlying DECL. */
13402 cp_finish_decomp (decl, prev, v.length ());
13405 if (pushed_scope)
13406 pop_scope (pushed_scope);
13408 if (decl == error_mark_node && DECL_P (orig_decl))
13410 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13411 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13414 return decl;
13417 /* Parse a decl-specifier-seq.
13419 decl-specifier-seq:
13420 decl-specifier-seq [opt] decl-specifier
13421 decl-specifier attribute-specifier-seq [opt] (C++11)
13423 decl-specifier:
13424 storage-class-specifier
13425 type-specifier
13426 function-specifier
13427 friend
13428 typedef
13430 GNU Extension:
13432 decl-specifier:
13433 attributes
13435 Concepts Extension:
13437 decl-specifier:
13438 concept
13440 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13442 The parser flags FLAGS is used to control type-specifier parsing.
13444 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13445 flags:
13447 1: one of the decl-specifiers is an elaborated-type-specifier
13448 (i.e., a type declaration)
13449 2: one of the decl-specifiers is an enum-specifier or a
13450 class-specifier (i.e., a type definition)
13454 static void
13455 cp_parser_decl_specifier_seq (cp_parser* parser,
13456 cp_parser_flags flags,
13457 cp_decl_specifier_seq *decl_specs,
13458 int* declares_class_or_enum)
13460 bool constructor_possible_p = !parser->in_declarator_p;
13461 bool found_decl_spec = false;
13462 cp_token *start_token = NULL;
13463 cp_decl_spec ds;
13465 /* Clear DECL_SPECS. */
13466 clear_decl_specs (decl_specs);
13468 /* Assume no class or enumeration type is declared. */
13469 *declares_class_or_enum = 0;
13471 /* Keep reading specifiers until there are no more to read. */
13472 while (true)
13474 bool constructor_p;
13475 cp_token *token;
13476 ds = ds_last;
13478 /* Peek at the next token. */
13479 token = cp_lexer_peek_token (parser->lexer);
13481 /* Save the first token of the decl spec list for error
13482 reporting. */
13483 if (!start_token)
13484 start_token = token;
13485 /* Handle attributes. */
13486 if (cp_next_tokens_can_be_attribute_p (parser))
13488 /* Parse the attributes. */
13489 tree attrs = cp_parser_attributes_opt (parser);
13491 /* In a sequence of declaration specifiers, c++11 attributes
13492 appertain to the type that precede them. In that case
13493 [dcl.spec]/1 says:
13495 The attribute-specifier-seq affects the type only for
13496 the declaration it appears in, not other declarations
13497 involving the same type.
13499 But for now let's force the user to position the
13500 attribute either at the beginning of the declaration or
13501 after the declarator-id, which would clearly mean that it
13502 applies to the declarator. */
13503 if (cxx11_attribute_p (attrs))
13505 if (!found_decl_spec)
13506 /* The c++11 attribute is at the beginning of the
13507 declaration. It appertains to the entity being
13508 declared. */;
13509 else
13511 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13513 /* This is an attribute following a
13514 class-specifier. */
13515 if (decl_specs->type_definition_p)
13516 warn_misplaced_attr_for_class_type (token->location,
13517 decl_specs->type);
13518 attrs = NULL_TREE;
13520 else
13522 decl_specs->std_attributes
13523 = chainon (decl_specs->std_attributes,
13524 attrs);
13525 if (decl_specs->locations[ds_std_attribute] == 0)
13526 decl_specs->locations[ds_std_attribute] = token->location;
13528 continue;
13532 decl_specs->attributes
13533 = chainon (decl_specs->attributes,
13534 attrs);
13535 if (decl_specs->locations[ds_attribute] == 0)
13536 decl_specs->locations[ds_attribute] = token->location;
13537 continue;
13539 /* Assume we will find a decl-specifier keyword. */
13540 found_decl_spec = true;
13541 /* If the next token is an appropriate keyword, we can simply
13542 add it to the list. */
13543 switch (token->keyword)
13545 /* decl-specifier:
13546 friend
13547 constexpr */
13548 case RID_FRIEND:
13549 if (!at_class_scope_p ())
13551 gcc_rich_location richloc (token->location);
13552 richloc.add_fixit_remove ();
13553 error_at_rich_loc (&richloc, "%<friend%> used outside of class");
13554 cp_lexer_purge_token (parser->lexer);
13556 else
13558 ds = ds_friend;
13559 /* Consume the token. */
13560 cp_lexer_consume_token (parser->lexer);
13562 break;
13564 case RID_CONSTEXPR:
13565 ds = ds_constexpr;
13566 cp_lexer_consume_token (parser->lexer);
13567 break;
13569 case RID_CONCEPT:
13570 ds = ds_concept;
13571 cp_lexer_consume_token (parser->lexer);
13572 break;
13574 /* function-specifier:
13575 inline
13576 virtual
13577 explicit */
13578 case RID_INLINE:
13579 case RID_VIRTUAL:
13580 case RID_EXPLICIT:
13581 cp_parser_function_specifier_opt (parser, decl_specs);
13582 break;
13584 /* decl-specifier:
13585 typedef */
13586 case RID_TYPEDEF:
13587 ds = ds_typedef;
13588 /* Consume the token. */
13589 cp_lexer_consume_token (parser->lexer);
13590 /* A constructor declarator cannot appear in a typedef. */
13591 constructor_possible_p = false;
13592 /* The "typedef" keyword can only occur in a declaration; we
13593 may as well commit at this point. */
13594 cp_parser_commit_to_tentative_parse (parser);
13596 if (decl_specs->storage_class != sc_none)
13597 decl_specs->conflicting_specifiers_p = true;
13598 break;
13600 /* storage-class-specifier:
13601 auto
13602 register
13603 static
13604 extern
13605 mutable
13607 GNU Extension:
13608 thread */
13609 case RID_AUTO:
13610 if (cxx_dialect == cxx98)
13612 /* Consume the token. */
13613 cp_lexer_consume_token (parser->lexer);
13615 /* Complain about `auto' as a storage specifier, if
13616 we're complaining about C++0x compatibility. */
13617 gcc_rich_location richloc (token->location);
13618 richloc.add_fixit_remove ();
13619 warning_at_rich_loc (&richloc, OPT_Wc__11_compat,
13620 "%<auto%> changes meaning in C++11; "
13621 "please remove it");
13623 /* Set the storage class anyway. */
13624 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13625 token);
13627 else
13628 /* C++0x auto type-specifier. */
13629 found_decl_spec = false;
13630 break;
13632 case RID_REGISTER:
13633 case RID_STATIC:
13634 case RID_EXTERN:
13635 case RID_MUTABLE:
13636 /* Consume the token. */
13637 cp_lexer_consume_token (parser->lexer);
13638 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13639 token);
13640 break;
13641 case RID_THREAD:
13642 /* Consume the token. */
13643 ds = ds_thread;
13644 cp_lexer_consume_token (parser->lexer);
13645 break;
13647 default:
13648 /* We did not yet find a decl-specifier yet. */
13649 found_decl_spec = false;
13650 break;
13653 if (found_decl_spec
13654 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13655 && token->keyword != RID_CONSTEXPR)
13656 error ("decl-specifier invalid in condition");
13658 if (found_decl_spec
13659 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13660 && token->keyword != RID_MUTABLE
13661 && token->keyword != RID_CONSTEXPR)
13662 error_at (token->location, "%qD invalid in lambda",
13663 ridpointers[token->keyword]);
13665 if (ds != ds_last)
13666 set_and_check_decl_spec_loc (decl_specs, ds, token);
13668 /* Constructors are a special case. The `S' in `S()' is not a
13669 decl-specifier; it is the beginning of the declarator. */
13670 constructor_p
13671 = (!found_decl_spec
13672 && constructor_possible_p
13673 && (cp_parser_constructor_declarator_p
13674 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13676 /* If we don't have a DECL_SPEC yet, then we must be looking at
13677 a type-specifier. */
13678 if (!found_decl_spec && !constructor_p)
13680 int decl_spec_declares_class_or_enum;
13681 bool is_cv_qualifier;
13682 tree type_spec;
13684 type_spec
13685 = cp_parser_type_specifier (parser, flags,
13686 decl_specs,
13687 /*is_declaration=*/true,
13688 &decl_spec_declares_class_or_enum,
13689 &is_cv_qualifier);
13690 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13692 /* If this type-specifier referenced a user-defined type
13693 (a typedef, class-name, etc.), then we can't allow any
13694 more such type-specifiers henceforth.
13696 [dcl.spec]
13698 The longest sequence of decl-specifiers that could
13699 possibly be a type name is taken as the
13700 decl-specifier-seq of a declaration. The sequence shall
13701 be self-consistent as described below.
13703 [dcl.type]
13705 As a general rule, at most one type-specifier is allowed
13706 in the complete decl-specifier-seq of a declaration. The
13707 only exceptions are the following:
13709 -- const or volatile can be combined with any other
13710 type-specifier.
13712 -- signed or unsigned can be combined with char, long,
13713 short, or int.
13715 -- ..
13717 Example:
13719 typedef char* Pc;
13720 void g (const int Pc);
13722 Here, Pc is *not* part of the decl-specifier seq; it's
13723 the declarator. Therefore, once we see a type-specifier
13724 (other than a cv-qualifier), we forbid any additional
13725 user-defined types. We *do* still allow things like `int
13726 int' to be considered a decl-specifier-seq, and issue the
13727 error message later. */
13728 if (type_spec && !is_cv_qualifier)
13729 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13730 /* A constructor declarator cannot follow a type-specifier. */
13731 if (type_spec)
13733 constructor_possible_p = false;
13734 found_decl_spec = true;
13735 if (!is_cv_qualifier)
13736 decl_specs->any_type_specifiers_p = true;
13740 /* If we still do not have a DECL_SPEC, then there are no more
13741 decl-specifiers. */
13742 if (!found_decl_spec)
13743 break;
13745 decl_specs->any_specifiers_p = true;
13746 /* After we see one decl-specifier, further decl-specifiers are
13747 always optional. */
13748 flags |= CP_PARSER_FLAGS_OPTIONAL;
13751 /* Don't allow a friend specifier with a class definition. */
13752 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13753 && (*declares_class_or_enum & 2))
13754 error_at (decl_specs->locations[ds_friend],
13755 "class definition may not be declared a friend");
13758 /* Parse an (optional) storage-class-specifier.
13760 storage-class-specifier:
13761 auto
13762 register
13763 static
13764 extern
13765 mutable
13767 GNU Extension:
13769 storage-class-specifier:
13770 thread
13772 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13774 static tree
13775 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13777 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13779 case RID_AUTO:
13780 if (cxx_dialect != cxx98)
13781 return NULL_TREE;
13782 /* Fall through for C++98. */
13783 gcc_fallthrough ();
13785 case RID_REGISTER:
13786 case RID_STATIC:
13787 case RID_EXTERN:
13788 case RID_MUTABLE:
13789 case RID_THREAD:
13790 /* Consume the token. */
13791 return cp_lexer_consume_token (parser->lexer)->u.value;
13793 default:
13794 return NULL_TREE;
13798 /* Parse an (optional) function-specifier.
13800 function-specifier:
13801 inline
13802 virtual
13803 explicit
13805 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13806 Updates DECL_SPECS, if it is non-NULL. */
13808 static tree
13809 cp_parser_function_specifier_opt (cp_parser* parser,
13810 cp_decl_specifier_seq *decl_specs)
13812 cp_token *token = cp_lexer_peek_token (parser->lexer);
13813 switch (token->keyword)
13815 case RID_INLINE:
13816 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13817 break;
13819 case RID_VIRTUAL:
13820 /* 14.5.2.3 [temp.mem]
13822 A member function template shall not be virtual. */
13823 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13824 && current_class_type)
13825 error_at (token->location, "templates may not be %<virtual%>");
13826 else
13827 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13828 break;
13830 case RID_EXPLICIT:
13831 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13832 break;
13834 default:
13835 return NULL_TREE;
13838 /* Consume the token. */
13839 return cp_lexer_consume_token (parser->lexer)->u.value;
13842 /* Parse a linkage-specification.
13844 linkage-specification:
13845 extern string-literal { declaration-seq [opt] }
13846 extern string-literal declaration */
13848 static void
13849 cp_parser_linkage_specification (cp_parser* parser)
13851 tree linkage;
13853 /* Look for the `extern' keyword. */
13854 cp_token *extern_token
13855 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13857 /* Look for the string-literal. */
13858 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
13859 linkage = cp_parser_string_literal (parser, false, false);
13861 /* Transform the literal into an identifier. If the literal is a
13862 wide-character string, or contains embedded NULs, then we can't
13863 handle it as the user wants. */
13864 if (strlen (TREE_STRING_POINTER (linkage))
13865 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13867 cp_parser_error (parser, "invalid linkage-specification");
13868 /* Assume C++ linkage. */
13869 linkage = lang_name_cplusplus;
13871 else
13872 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13874 /* We're now using the new linkage. */
13875 push_lang_context (linkage);
13877 /* Preserve the location of the the innermost linkage specification,
13878 tracking the locations of nested specifications via a local. */
13879 location_t saved_location
13880 = parser->innermost_linkage_specification_location;
13881 /* Construct a location ranging from the start of the "extern" to
13882 the end of the string-literal, with the caret at the start, e.g.:
13883 extern "C" {
13884 ^~~~~~~~~~
13886 parser->innermost_linkage_specification_location
13887 = make_location (extern_token->location,
13888 extern_token->location,
13889 get_finish (string_token->location));
13891 /* If the next token is a `{', then we're using the first
13892 production. */
13893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13895 cp_ensure_no_omp_declare_simd (parser);
13896 cp_ensure_no_oacc_routine (parser);
13898 /* Consume the `{' token. */
13899 matching_braces braces;
13900 braces.consume_open (parser)->location;
13901 /* Parse the declarations. */
13902 cp_parser_declaration_seq_opt (parser);
13903 /* Look for the closing `}'. */
13904 braces.require_close (parser);
13906 /* Otherwise, there's just one declaration. */
13907 else
13909 bool saved_in_unbraced_linkage_specification_p;
13911 saved_in_unbraced_linkage_specification_p
13912 = parser->in_unbraced_linkage_specification_p;
13913 parser->in_unbraced_linkage_specification_p = true;
13914 cp_parser_declaration (parser);
13915 parser->in_unbraced_linkage_specification_p
13916 = saved_in_unbraced_linkage_specification_p;
13919 /* We're done with the linkage-specification. */
13920 pop_lang_context ();
13922 /* Restore location of parent linkage specification, if any. */
13923 parser->innermost_linkage_specification_location = saved_location;
13926 /* Parse a static_assert-declaration.
13928 static_assert-declaration:
13929 static_assert ( constant-expression , string-literal ) ;
13930 static_assert ( constant-expression ) ; (C++17)
13932 If MEMBER_P, this static_assert is a class member. */
13934 static void
13935 cp_parser_static_assert(cp_parser *parser, bool member_p)
13937 tree condition;
13938 tree message;
13939 cp_token *token;
13940 location_t saved_loc;
13941 bool dummy;
13943 /* Peek at the `static_assert' token so we can keep track of exactly
13944 where the static assertion started. */
13945 token = cp_lexer_peek_token (parser->lexer);
13946 saved_loc = token->location;
13948 /* Look for the `static_assert' keyword. */
13949 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13950 RT_STATIC_ASSERT))
13951 return;
13953 /* We know we are in a static assertion; commit to any tentative
13954 parse. */
13955 if (cp_parser_parsing_tentatively (parser))
13956 cp_parser_commit_to_tentative_parse (parser);
13958 /* Parse the `(' starting the static assertion condition. */
13959 matching_parens parens;
13960 parens.require_open (parser);
13962 /* Parse the constant-expression. Allow a non-constant expression
13963 here in order to give better diagnostics in finish_static_assert. */
13964 condition =
13965 cp_parser_constant_expression (parser,
13966 /*allow_non_constant_p=*/true,
13967 /*non_constant_p=*/&dummy);
13969 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13971 if (cxx_dialect < cxx17)
13972 pedwarn (input_location, OPT_Wpedantic,
13973 "static_assert without a message "
13974 "only available with -std=c++17 or -std=gnu++17");
13975 /* Eat the ')' */
13976 cp_lexer_consume_token (parser->lexer);
13977 message = build_string (1, "");
13978 TREE_TYPE (message) = char_array_type_node;
13979 fix_string_type (message);
13981 else
13983 /* Parse the separating `,'. */
13984 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13986 /* Parse the string-literal message. */
13987 message = cp_parser_string_literal (parser,
13988 /*translate=*/false,
13989 /*wide_ok=*/true);
13991 /* A `)' completes the static assertion. */
13992 if (!parens.require_close (parser))
13993 cp_parser_skip_to_closing_parenthesis (parser,
13994 /*recovering=*/true,
13995 /*or_comma=*/false,
13996 /*consume_paren=*/true);
13999 /* A semicolon terminates the declaration. */
14000 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14002 /* Complete the static assertion, which may mean either processing
14003 the static assert now or saving it for template instantiation. */
14004 finish_static_assert (condition, message, saved_loc, member_p);
14007 /* Parse the expression in decltype ( expression ). */
14009 static tree
14010 cp_parser_decltype_expr (cp_parser *parser,
14011 bool &id_expression_or_member_access_p)
14013 cp_token *id_expr_start_token;
14014 tree expr;
14016 /* Since we're going to preserve any side-effects from this parse, set up a
14017 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14018 in the expression. */
14019 tentative_firewall firewall (parser);
14021 /* First, try parsing an id-expression. */
14022 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
14023 cp_parser_parse_tentatively (parser);
14024 expr = cp_parser_id_expression (parser,
14025 /*template_keyword_p=*/false,
14026 /*check_dependency_p=*/true,
14027 /*template_p=*/NULL,
14028 /*declarator_p=*/false,
14029 /*optional_p=*/false);
14031 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
14033 bool non_integral_constant_expression_p = false;
14034 tree id_expression = expr;
14035 cp_id_kind idk;
14036 const char *error_msg;
14038 if (identifier_p (expr))
14039 /* Lookup the name we got back from the id-expression. */
14040 expr = cp_parser_lookup_name_simple (parser, expr,
14041 id_expr_start_token->location);
14043 if (expr
14044 && expr != error_mark_node
14045 && TREE_CODE (expr) != TYPE_DECL
14046 && (TREE_CODE (expr) != BIT_NOT_EXPR
14047 || !TYPE_P (TREE_OPERAND (expr, 0)))
14048 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14050 /* Complete lookup of the id-expression. */
14051 expr = (finish_id_expression
14052 (id_expression, expr, parser->scope, &idk,
14053 /*integral_constant_expression_p=*/false,
14054 /*allow_non_integral_constant_expression_p=*/true,
14055 &non_integral_constant_expression_p,
14056 /*template_p=*/false,
14057 /*done=*/true,
14058 /*address_p=*/false,
14059 /*template_arg_p=*/false,
14060 &error_msg,
14061 id_expr_start_token->location));
14063 if (expr == error_mark_node)
14064 /* We found an id-expression, but it was something that we
14065 should not have found. This is an error, not something
14066 we can recover from, so note that we found an
14067 id-expression and we'll recover as gracefully as
14068 possible. */
14069 id_expression_or_member_access_p = true;
14072 if (expr
14073 && expr != error_mark_node
14074 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14075 /* We have an id-expression. */
14076 id_expression_or_member_access_p = true;
14079 if (!id_expression_or_member_access_p)
14081 /* Abort the id-expression parse. */
14082 cp_parser_abort_tentative_parse (parser);
14084 /* Parsing tentatively, again. */
14085 cp_parser_parse_tentatively (parser);
14087 /* Parse a class member access. */
14088 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14089 /*cast_p=*/false, /*decltype*/true,
14090 /*member_access_only_p=*/true, NULL);
14092 if (expr
14093 && expr != error_mark_node
14094 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14095 /* We have an id-expression. */
14096 id_expression_or_member_access_p = true;
14099 if (id_expression_or_member_access_p)
14100 /* We have parsed the complete id-expression or member access. */
14101 cp_parser_parse_definitely (parser);
14102 else
14104 /* Abort our attempt to parse an id-expression or member access
14105 expression. */
14106 cp_parser_abort_tentative_parse (parser);
14108 /* Parse a full expression. */
14109 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14110 /*decltype_p=*/true);
14113 return expr;
14116 /* Parse a `decltype' type. Returns the type.
14118 simple-type-specifier:
14119 decltype ( expression )
14120 C++14 proposal:
14121 decltype ( auto ) */
14123 static tree
14124 cp_parser_decltype (cp_parser *parser)
14126 tree expr;
14127 bool id_expression_or_member_access_p = false;
14128 const char *saved_message;
14129 bool saved_integral_constant_expression_p;
14130 bool saved_non_integral_constant_expression_p;
14131 bool saved_greater_than_is_operator_p;
14132 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14134 if (start_token->type == CPP_DECLTYPE)
14136 /* Already parsed. */
14137 cp_lexer_consume_token (parser->lexer);
14138 return saved_checks_value (start_token->u.tree_check_value);
14141 /* Look for the `decltype' token. */
14142 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14143 return error_mark_node;
14145 /* Parse the opening `('. */
14146 matching_parens parens;
14147 if (!parens.require_open (parser))
14148 return error_mark_node;
14150 /* decltype (auto) */
14151 if (cxx_dialect >= cxx14
14152 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14154 cp_lexer_consume_token (parser->lexer);
14155 if (!parens.require_close (parser))
14156 return error_mark_node;
14157 expr = make_decltype_auto ();
14158 AUTO_IS_DECLTYPE (expr) = true;
14159 goto rewrite;
14162 /* Types cannot be defined in a `decltype' expression. Save away the
14163 old message. */
14164 saved_message = parser->type_definition_forbidden_message;
14166 /* And create the new one. */
14167 parser->type_definition_forbidden_message
14168 = G_("types may not be defined in %<decltype%> expressions");
14170 /* The restrictions on constant-expressions do not apply inside
14171 decltype expressions. */
14172 saved_integral_constant_expression_p
14173 = parser->integral_constant_expression_p;
14174 saved_non_integral_constant_expression_p
14175 = parser->non_integral_constant_expression_p;
14176 parser->integral_constant_expression_p = false;
14178 /* Within a parenthesized expression, a `>' token is always
14179 the greater-than operator. */
14180 saved_greater_than_is_operator_p
14181 = parser->greater_than_is_operator_p;
14182 parser->greater_than_is_operator_p = true;
14184 /* Do not actually evaluate the expression. */
14185 ++cp_unevaluated_operand;
14187 /* Do not warn about problems with the expression. */
14188 ++c_inhibit_evaluation_warnings;
14190 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14192 /* Go back to evaluating expressions. */
14193 --cp_unevaluated_operand;
14194 --c_inhibit_evaluation_warnings;
14196 /* The `>' token might be the end of a template-id or
14197 template-parameter-list now. */
14198 parser->greater_than_is_operator_p
14199 = saved_greater_than_is_operator_p;
14201 /* Restore the old message and the integral constant expression
14202 flags. */
14203 parser->type_definition_forbidden_message = saved_message;
14204 parser->integral_constant_expression_p
14205 = saved_integral_constant_expression_p;
14206 parser->non_integral_constant_expression_p
14207 = saved_non_integral_constant_expression_p;
14209 /* Parse to the closing `)'. */
14210 if (!parens.require_close (parser))
14212 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14213 /*consume_paren=*/true);
14214 return error_mark_node;
14217 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14218 tf_warning_or_error);
14220 rewrite:
14221 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14222 it again. */
14223 start_token->type = CPP_DECLTYPE;
14224 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14225 start_token->u.tree_check_value->value = expr;
14226 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14227 start_token->keyword = RID_MAX;
14228 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14230 return expr;
14233 /* Special member functions [gram.special] */
14235 /* Parse a conversion-function-id.
14237 conversion-function-id:
14238 operator conversion-type-id
14240 Returns an IDENTIFIER_NODE representing the operator. */
14242 static tree
14243 cp_parser_conversion_function_id (cp_parser* parser)
14245 tree type;
14246 tree saved_scope;
14247 tree saved_qualifying_scope;
14248 tree saved_object_scope;
14249 tree pushed_scope = NULL_TREE;
14251 /* Look for the `operator' token. */
14252 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14253 return error_mark_node;
14254 /* When we parse the conversion-type-id, the current scope will be
14255 reset. However, we need that information in able to look up the
14256 conversion function later, so we save it here. */
14257 saved_scope = parser->scope;
14258 saved_qualifying_scope = parser->qualifying_scope;
14259 saved_object_scope = parser->object_scope;
14260 /* We must enter the scope of the class so that the names of
14261 entities declared within the class are available in the
14262 conversion-type-id. For example, consider:
14264 struct S {
14265 typedef int I;
14266 operator I();
14269 S::operator I() { ... }
14271 In order to see that `I' is a type-name in the definition, we
14272 must be in the scope of `S'. */
14273 if (saved_scope)
14274 pushed_scope = push_scope (saved_scope);
14275 /* Parse the conversion-type-id. */
14276 type = cp_parser_conversion_type_id (parser);
14277 /* Leave the scope of the class, if any. */
14278 if (pushed_scope)
14279 pop_scope (pushed_scope);
14280 /* Restore the saved scope. */
14281 parser->scope = saved_scope;
14282 parser->qualifying_scope = saved_qualifying_scope;
14283 parser->object_scope = saved_object_scope;
14284 /* If the TYPE is invalid, indicate failure. */
14285 if (type == error_mark_node)
14286 return error_mark_node;
14287 return make_conv_op_name (type);
14290 /* Parse a conversion-type-id:
14292 conversion-type-id:
14293 type-specifier-seq conversion-declarator [opt]
14295 Returns the TYPE specified. */
14297 static tree
14298 cp_parser_conversion_type_id (cp_parser* parser)
14300 tree attributes;
14301 cp_decl_specifier_seq type_specifiers;
14302 cp_declarator *declarator;
14303 tree type_specified;
14304 const char *saved_message;
14306 /* Parse the attributes. */
14307 attributes = cp_parser_attributes_opt (parser);
14309 saved_message = parser->type_definition_forbidden_message;
14310 parser->type_definition_forbidden_message
14311 = G_("types may not be defined in a conversion-type-id");
14313 /* Parse the type-specifiers. */
14314 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14315 /*is_trailing_return=*/false,
14316 &type_specifiers);
14318 parser->type_definition_forbidden_message = saved_message;
14320 /* If that didn't work, stop. */
14321 if (type_specifiers.type == error_mark_node)
14322 return error_mark_node;
14323 /* Parse the conversion-declarator. */
14324 declarator = cp_parser_conversion_declarator_opt (parser);
14326 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14327 /*initialized=*/0, &attributes);
14328 if (attributes)
14329 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14331 /* Don't give this error when parsing tentatively. This happens to
14332 work because we always parse this definitively once. */
14333 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14334 && type_uses_auto (type_specified))
14336 if (cxx_dialect < cxx14)
14338 error ("invalid use of %<auto%> in conversion operator");
14339 return error_mark_node;
14341 else if (template_parm_scope_p ())
14342 warning (0, "use of %<auto%> in member template "
14343 "conversion operator can never be deduced");
14346 return type_specified;
14349 /* Parse an (optional) conversion-declarator.
14351 conversion-declarator:
14352 ptr-operator conversion-declarator [opt]
14356 static cp_declarator *
14357 cp_parser_conversion_declarator_opt (cp_parser* parser)
14359 enum tree_code code;
14360 tree class_type, std_attributes = NULL_TREE;
14361 cp_cv_quals cv_quals;
14363 /* We don't know if there's a ptr-operator next, or not. */
14364 cp_parser_parse_tentatively (parser);
14365 /* Try the ptr-operator. */
14366 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14367 &std_attributes);
14368 /* If it worked, look for more conversion-declarators. */
14369 if (cp_parser_parse_definitely (parser))
14371 cp_declarator *declarator;
14373 /* Parse another optional declarator. */
14374 declarator = cp_parser_conversion_declarator_opt (parser);
14376 declarator = cp_parser_make_indirect_declarator
14377 (code, class_type, cv_quals, declarator, std_attributes);
14379 return declarator;
14382 return NULL;
14385 /* Parse an (optional) ctor-initializer.
14387 ctor-initializer:
14388 : mem-initializer-list */
14390 static void
14391 cp_parser_ctor_initializer_opt (cp_parser* parser)
14393 /* If the next token is not a `:', then there is no
14394 ctor-initializer. */
14395 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14397 /* Do default initialization of any bases and members. */
14398 if (DECL_CONSTRUCTOR_P (current_function_decl))
14399 finish_mem_initializers (NULL_TREE);
14400 return;
14403 /* Consume the `:' token. */
14404 cp_lexer_consume_token (parser->lexer);
14405 /* And the mem-initializer-list. */
14406 cp_parser_mem_initializer_list (parser);
14409 /* Parse a mem-initializer-list.
14411 mem-initializer-list:
14412 mem-initializer ... [opt]
14413 mem-initializer ... [opt] , mem-initializer-list */
14415 static void
14416 cp_parser_mem_initializer_list (cp_parser* parser)
14418 tree mem_initializer_list = NULL_TREE;
14419 tree target_ctor = error_mark_node;
14420 cp_token *token = cp_lexer_peek_token (parser->lexer);
14422 /* Let the semantic analysis code know that we are starting the
14423 mem-initializer-list. */
14424 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14425 error_at (token->location,
14426 "only constructors take member initializers");
14428 /* Loop through the list. */
14429 while (true)
14431 tree mem_initializer;
14433 token = cp_lexer_peek_token (parser->lexer);
14434 /* Parse the mem-initializer. */
14435 mem_initializer = cp_parser_mem_initializer (parser);
14436 /* If the next token is a `...', we're expanding member initializers. */
14437 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14439 /* Consume the `...'. */
14440 cp_lexer_consume_token (parser->lexer);
14442 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14443 can be expanded but members cannot. */
14444 if (mem_initializer != error_mark_node
14445 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14447 error_at (token->location,
14448 "cannot expand initializer for member %qD",
14449 TREE_PURPOSE (mem_initializer));
14450 mem_initializer = error_mark_node;
14453 /* Construct the pack expansion type. */
14454 if (mem_initializer != error_mark_node)
14455 mem_initializer = make_pack_expansion (mem_initializer);
14457 if (target_ctor != error_mark_node
14458 && mem_initializer != error_mark_node)
14460 error ("mem-initializer for %qD follows constructor delegation",
14461 TREE_PURPOSE (mem_initializer));
14462 mem_initializer = error_mark_node;
14464 /* Look for a target constructor. */
14465 if (mem_initializer != error_mark_node
14466 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14467 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14469 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14470 if (mem_initializer_list)
14472 error ("constructor delegation follows mem-initializer for %qD",
14473 TREE_PURPOSE (mem_initializer_list));
14474 mem_initializer = error_mark_node;
14476 target_ctor = mem_initializer;
14478 /* Add it to the list, unless it was erroneous. */
14479 if (mem_initializer != error_mark_node)
14481 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14482 mem_initializer_list = mem_initializer;
14484 /* If the next token is not a `,', we're done. */
14485 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14486 break;
14487 /* Consume the `,' token. */
14488 cp_lexer_consume_token (parser->lexer);
14491 /* Perform semantic analysis. */
14492 if (DECL_CONSTRUCTOR_P (current_function_decl))
14493 finish_mem_initializers (mem_initializer_list);
14496 /* Parse a mem-initializer.
14498 mem-initializer:
14499 mem-initializer-id ( expression-list [opt] )
14500 mem-initializer-id braced-init-list
14502 GNU extension:
14504 mem-initializer:
14505 ( expression-list [opt] )
14507 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14508 class) or FIELD_DECL (for a non-static data member) to initialize;
14509 the TREE_VALUE is the expression-list. An empty initialization
14510 list is represented by void_list_node. */
14512 static tree
14513 cp_parser_mem_initializer (cp_parser* parser)
14515 tree mem_initializer_id;
14516 tree expression_list;
14517 tree member;
14518 cp_token *token = cp_lexer_peek_token (parser->lexer);
14520 /* Find out what is being initialized. */
14521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14523 permerror (token->location,
14524 "anachronistic old-style base class initializer");
14525 mem_initializer_id = NULL_TREE;
14527 else
14529 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14530 if (mem_initializer_id == error_mark_node)
14531 return mem_initializer_id;
14533 member = expand_member_init (mem_initializer_id);
14534 if (member && !DECL_P (member))
14535 in_base_initializer = 1;
14537 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14539 bool expr_non_constant_p;
14540 cp_lexer_set_source_position (parser->lexer);
14541 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14542 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14543 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14544 expression_list = build_tree_list (NULL_TREE, expression_list);
14546 else
14548 vec<tree, va_gc> *vec;
14549 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14550 /*cast_p=*/false,
14551 /*allow_expansion_p=*/true,
14552 /*non_constant_p=*/NULL);
14553 if (vec == NULL)
14554 return error_mark_node;
14555 expression_list = build_tree_list_vec (vec);
14556 release_tree_vector (vec);
14559 if (expression_list == error_mark_node)
14560 return error_mark_node;
14561 if (!expression_list)
14562 expression_list = void_type_node;
14564 in_base_initializer = 0;
14566 return member ? build_tree_list (member, expression_list) : error_mark_node;
14569 /* Parse a mem-initializer-id.
14571 mem-initializer-id:
14572 :: [opt] nested-name-specifier [opt] class-name
14573 decltype-specifier (C++11)
14574 identifier
14576 Returns a TYPE indicating the class to be initialized for the first
14577 production (and the second in C++11). Returns an IDENTIFIER_NODE
14578 indicating the data member to be initialized for the last production. */
14580 static tree
14581 cp_parser_mem_initializer_id (cp_parser* parser)
14583 bool global_scope_p;
14584 bool nested_name_specifier_p;
14585 bool template_p = false;
14586 tree id;
14588 cp_token *token = cp_lexer_peek_token (parser->lexer);
14590 /* `typename' is not allowed in this context ([temp.res]). */
14591 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14593 error_at (token->location,
14594 "keyword %<typename%> not allowed in this context (a qualified "
14595 "member initializer is implicitly a type)");
14596 cp_lexer_consume_token (parser->lexer);
14598 /* Look for the optional `::' operator. */
14599 global_scope_p
14600 = (cp_parser_global_scope_opt (parser,
14601 /*current_scope_valid_p=*/false)
14602 != NULL_TREE);
14603 /* Look for the optional nested-name-specifier. The simplest way to
14604 implement:
14606 [temp.res]
14608 The keyword `typename' is not permitted in a base-specifier or
14609 mem-initializer; in these contexts a qualified name that
14610 depends on a template-parameter is implicitly assumed to be a
14611 type name.
14613 is to assume that we have seen the `typename' keyword at this
14614 point. */
14615 nested_name_specifier_p
14616 = (cp_parser_nested_name_specifier_opt (parser,
14617 /*typename_keyword_p=*/true,
14618 /*check_dependency_p=*/true,
14619 /*type_p=*/true,
14620 /*is_declaration=*/true)
14621 != NULL_TREE);
14622 if (nested_name_specifier_p)
14623 template_p = cp_parser_optional_template_keyword (parser);
14624 /* If there is a `::' operator or a nested-name-specifier, then we
14625 are definitely looking for a class-name. */
14626 if (global_scope_p || nested_name_specifier_p)
14627 return cp_parser_class_name (parser,
14628 /*typename_keyword_p=*/true,
14629 /*template_keyword_p=*/template_p,
14630 typename_type,
14631 /*check_dependency_p=*/true,
14632 /*class_head_p=*/false,
14633 /*is_declaration=*/true);
14634 /* Otherwise, we could also be looking for an ordinary identifier. */
14635 cp_parser_parse_tentatively (parser);
14636 if (cp_lexer_next_token_is_decltype (parser->lexer))
14637 /* Try a decltype-specifier. */
14638 id = cp_parser_decltype (parser);
14639 else
14640 /* Otherwise, try a class-name. */
14641 id = cp_parser_class_name (parser,
14642 /*typename_keyword_p=*/true,
14643 /*template_keyword_p=*/false,
14644 none_type,
14645 /*check_dependency_p=*/true,
14646 /*class_head_p=*/false,
14647 /*is_declaration=*/true);
14648 /* If we found one, we're done. */
14649 if (cp_parser_parse_definitely (parser))
14650 return id;
14651 /* Otherwise, look for an ordinary identifier. */
14652 return cp_parser_identifier (parser);
14655 /* Overloading [gram.over] */
14657 /* Parse an operator-function-id.
14659 operator-function-id:
14660 operator operator
14662 Returns an IDENTIFIER_NODE for the operator which is a
14663 human-readable spelling of the identifier, e.g., `operator +'. */
14665 static cp_expr
14666 cp_parser_operator_function_id (cp_parser* parser)
14668 /* Look for the `operator' keyword. */
14669 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14670 return error_mark_node;
14671 /* And then the name of the operator itself. */
14672 return cp_parser_operator (parser);
14675 /* Return an identifier node for a user-defined literal operator.
14676 The suffix identifier is chained to the operator name identifier. */
14678 tree
14679 cp_literal_operator_id (const char* name)
14681 tree identifier;
14682 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14683 + strlen (name) + 10);
14684 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14685 identifier = get_identifier (buffer);
14687 return identifier;
14690 /* Parse an operator.
14692 operator:
14693 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14694 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14695 || ++ -- , ->* -> () []
14697 GNU Extensions:
14699 operator:
14700 <? >? <?= >?=
14702 Returns an IDENTIFIER_NODE for the operator which is a
14703 human-readable spelling of the identifier, e.g., `operator +'. */
14705 static cp_expr
14706 cp_parser_operator (cp_parser* parser)
14708 tree id = NULL_TREE;
14709 cp_token *token;
14710 bool utf8 = false;
14712 /* Peek at the next token. */
14713 token = cp_lexer_peek_token (parser->lexer);
14715 location_t start_loc = token->location;
14717 /* Figure out which operator we have. */
14718 switch (token->type)
14720 case CPP_KEYWORD:
14722 enum tree_code op;
14724 /* The keyword should be either `new' or `delete'. */
14725 if (token->keyword == RID_NEW)
14726 op = NEW_EXPR;
14727 else if (token->keyword == RID_DELETE)
14728 op = DELETE_EXPR;
14729 else
14730 break;
14732 /* Consume the `new' or `delete' token. */
14733 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14735 /* Peek at the next token. */
14736 token = cp_lexer_peek_token (parser->lexer);
14737 /* If it's a `[' token then this is the array variant of the
14738 operator. */
14739 if (token->type == CPP_OPEN_SQUARE)
14741 /* Consume the `[' token. */
14742 cp_lexer_consume_token (parser->lexer);
14743 /* Look for the `]' token. */
14744 if (cp_token *close_token
14745 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14746 end_loc = close_token->location;
14747 id = cp_operator_id (op == NEW_EXPR
14748 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14750 /* Otherwise, we have the non-array variant. */
14751 else
14752 id = cp_operator_id (op);
14754 location_t loc = make_location (start_loc, start_loc, end_loc);
14756 return cp_expr (id, loc);
14759 case CPP_PLUS:
14760 id = cp_operator_id (PLUS_EXPR);
14761 break;
14763 case CPP_MINUS:
14764 id = cp_operator_id (MINUS_EXPR);
14765 break;
14767 case CPP_MULT:
14768 id = cp_operator_id (MULT_EXPR);
14769 break;
14771 case CPP_DIV:
14772 id = cp_operator_id (TRUNC_DIV_EXPR);
14773 break;
14775 case CPP_MOD:
14776 id = cp_operator_id (TRUNC_MOD_EXPR);
14777 break;
14779 case CPP_XOR:
14780 id = cp_operator_id (BIT_XOR_EXPR);
14781 break;
14783 case CPP_AND:
14784 id = cp_operator_id (BIT_AND_EXPR);
14785 break;
14787 case CPP_OR:
14788 id = cp_operator_id (BIT_IOR_EXPR);
14789 break;
14791 case CPP_COMPL:
14792 id = cp_operator_id (BIT_NOT_EXPR);
14793 break;
14795 case CPP_NOT:
14796 id = cp_operator_id (TRUTH_NOT_EXPR);
14797 break;
14799 case CPP_EQ:
14800 id = cp_assignment_operator_id (NOP_EXPR);
14801 break;
14803 case CPP_LESS:
14804 id = cp_operator_id (LT_EXPR);
14805 break;
14807 case CPP_GREATER:
14808 id = cp_operator_id (GT_EXPR);
14809 break;
14811 case CPP_PLUS_EQ:
14812 id = cp_assignment_operator_id (PLUS_EXPR);
14813 break;
14815 case CPP_MINUS_EQ:
14816 id = cp_assignment_operator_id (MINUS_EXPR);
14817 break;
14819 case CPP_MULT_EQ:
14820 id = cp_assignment_operator_id (MULT_EXPR);
14821 break;
14823 case CPP_DIV_EQ:
14824 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14825 break;
14827 case CPP_MOD_EQ:
14828 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14829 break;
14831 case CPP_XOR_EQ:
14832 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14833 break;
14835 case CPP_AND_EQ:
14836 id = cp_assignment_operator_id (BIT_AND_EXPR);
14837 break;
14839 case CPP_OR_EQ:
14840 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14841 break;
14843 case CPP_LSHIFT:
14844 id = cp_operator_id (LSHIFT_EXPR);
14845 break;
14847 case CPP_RSHIFT:
14848 id = cp_operator_id (RSHIFT_EXPR);
14849 break;
14851 case CPP_LSHIFT_EQ:
14852 id = cp_assignment_operator_id (LSHIFT_EXPR);
14853 break;
14855 case CPP_RSHIFT_EQ:
14856 id = cp_assignment_operator_id (RSHIFT_EXPR);
14857 break;
14859 case CPP_EQ_EQ:
14860 id = cp_operator_id (EQ_EXPR);
14861 break;
14863 case CPP_NOT_EQ:
14864 id = cp_operator_id (NE_EXPR);
14865 break;
14867 case CPP_LESS_EQ:
14868 id = cp_operator_id (LE_EXPR);
14869 break;
14871 case CPP_GREATER_EQ:
14872 id = cp_operator_id (GE_EXPR);
14873 break;
14875 case CPP_AND_AND:
14876 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14877 break;
14879 case CPP_OR_OR:
14880 id = cp_operator_id (TRUTH_ORIF_EXPR);
14881 break;
14883 case CPP_PLUS_PLUS:
14884 id = cp_operator_id (POSTINCREMENT_EXPR);
14885 break;
14887 case CPP_MINUS_MINUS:
14888 id = cp_operator_id (PREDECREMENT_EXPR);
14889 break;
14891 case CPP_COMMA:
14892 id = cp_operator_id (COMPOUND_EXPR);
14893 break;
14895 case CPP_DEREF_STAR:
14896 id = cp_operator_id (MEMBER_REF);
14897 break;
14899 case CPP_DEREF:
14900 id = cp_operator_id (COMPONENT_REF);
14901 break;
14903 case CPP_OPEN_PAREN:
14905 /* Consume the `('. */
14906 matching_parens parens;
14907 parens.consume_open (parser);
14908 /* Look for the matching `)'. */
14909 parens.require_close (parser);
14910 return cp_operator_id (CALL_EXPR);
14913 case CPP_OPEN_SQUARE:
14914 /* Consume the `['. */
14915 cp_lexer_consume_token (parser->lexer);
14916 /* Look for the matching `]'. */
14917 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14918 return cp_operator_id (ARRAY_REF);
14920 case CPP_UTF8STRING:
14921 case CPP_UTF8STRING_USERDEF:
14922 utf8 = true;
14923 /* FALLTHRU */
14924 case CPP_STRING:
14925 case CPP_WSTRING:
14926 case CPP_STRING16:
14927 case CPP_STRING32:
14928 case CPP_STRING_USERDEF:
14929 case CPP_WSTRING_USERDEF:
14930 case CPP_STRING16_USERDEF:
14931 case CPP_STRING32_USERDEF:
14933 tree str, string_tree;
14934 int sz, len;
14936 if (cxx_dialect == cxx98)
14937 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14939 /* Consume the string. */
14940 str = cp_parser_string_literal (parser, /*translate=*/true,
14941 /*wide_ok=*/true, /*lookup_udlit=*/false);
14942 if (str == error_mark_node)
14943 return error_mark_node;
14944 else if (TREE_CODE (str) == USERDEF_LITERAL)
14946 string_tree = USERDEF_LITERAL_VALUE (str);
14947 id = USERDEF_LITERAL_SUFFIX_ID (str);
14949 else
14951 string_tree = str;
14952 /* Look for the suffix identifier. */
14953 token = cp_lexer_peek_token (parser->lexer);
14954 if (token->type == CPP_NAME)
14955 id = cp_parser_identifier (parser);
14956 else if (token->type == CPP_KEYWORD)
14958 error ("unexpected keyword;"
14959 " remove space between quotes and suffix identifier");
14960 return error_mark_node;
14962 else
14964 error ("expected suffix identifier");
14965 return error_mark_node;
14968 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14969 (TREE_TYPE (TREE_TYPE (string_tree))));
14970 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14971 if (len != 0)
14973 error ("expected empty string after %<operator%> keyword");
14974 return error_mark_node;
14976 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14977 != char_type_node)
14979 error ("invalid encoding prefix in literal operator");
14980 return error_mark_node;
14982 if (id != error_mark_node)
14984 const char *name = IDENTIFIER_POINTER (id);
14985 id = cp_literal_operator_id (name);
14987 return id;
14990 default:
14991 /* Anything else is an error. */
14992 break;
14995 /* If we have selected an identifier, we need to consume the
14996 operator token. */
14997 if (id)
14998 cp_lexer_consume_token (parser->lexer);
14999 /* Otherwise, no valid operator name was present. */
15000 else
15002 cp_parser_error (parser, "expected operator");
15003 id = error_mark_node;
15006 return cp_expr (id, start_loc);
15009 /* Parse a template-declaration.
15011 template-declaration:
15012 export [opt] template < template-parameter-list > declaration
15014 If MEMBER_P is TRUE, this template-declaration occurs within a
15015 class-specifier.
15017 The grammar rule given by the standard isn't correct. What
15018 is really meant is:
15020 template-declaration:
15021 export [opt] template-parameter-list-seq
15022 decl-specifier-seq [opt] init-declarator [opt] ;
15023 export [opt] template-parameter-list-seq
15024 function-definition
15026 template-parameter-list-seq:
15027 template-parameter-list-seq [opt]
15028 template < template-parameter-list >
15030 Concept Extensions:
15032 template-parameter-list-seq:
15033 template < template-parameter-list > requires-clause [opt]
15035 requires-clause:
15036 requires logical-or-expression */
15038 static void
15039 cp_parser_template_declaration (cp_parser* parser, bool member_p)
15041 /* Check for `export'. */
15042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
15044 /* Consume the `export' token. */
15045 cp_lexer_consume_token (parser->lexer);
15046 /* Warn that we do not support `export'. */
15047 warning (0, "keyword %<export%> not implemented, and will be ignored");
15050 cp_parser_template_declaration_after_export (parser, member_p);
15053 /* Parse a template-parameter-list.
15055 template-parameter-list:
15056 template-parameter
15057 template-parameter-list , template-parameter
15059 Returns a TREE_LIST. Each node represents a template parameter.
15060 The nodes are connected via their TREE_CHAINs. */
15062 static tree
15063 cp_parser_template_parameter_list (cp_parser* parser)
15065 tree parameter_list = NULL_TREE;
15067 begin_template_parm_list ();
15069 /* The loop below parses the template parms. We first need to know
15070 the total number of template parms to be able to compute proper
15071 canonical types of each dependent type. So after the loop, when
15072 we know the total number of template parms,
15073 end_template_parm_list computes the proper canonical types and
15074 fixes up the dependent types accordingly. */
15075 while (true)
15077 tree parameter;
15078 bool is_non_type;
15079 bool is_parameter_pack;
15080 location_t parm_loc;
15082 /* Parse the template-parameter. */
15083 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15084 parameter = cp_parser_template_parameter (parser,
15085 &is_non_type,
15086 &is_parameter_pack);
15087 /* Add it to the list. */
15088 if (parameter != error_mark_node)
15089 parameter_list = process_template_parm (parameter_list,
15090 parm_loc,
15091 parameter,
15092 is_non_type,
15093 is_parameter_pack);
15094 else
15096 tree err_parm = build_tree_list (parameter, parameter);
15097 parameter_list = chainon (parameter_list, err_parm);
15100 /* If the next token is not a `,', we're done. */
15101 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15102 break;
15103 /* Otherwise, consume the `,' token. */
15104 cp_lexer_consume_token (parser->lexer);
15107 return end_template_parm_list (parameter_list);
15110 /* Parse a introduction-list.
15112 introduction-list:
15113 introduced-parameter
15114 introduction-list , introduced-parameter
15116 introduced-parameter:
15117 ...[opt] identifier
15119 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15120 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15121 WILDCARD_DECL will also have DECL_NAME set and token location in
15122 DECL_SOURCE_LOCATION. */
15124 static tree
15125 cp_parser_introduction_list (cp_parser *parser)
15127 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15129 while (true)
15131 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15132 if (is_pack)
15133 cp_lexer_consume_token (parser->lexer);
15135 /* Build placeholder. */
15136 tree parm = build_nt (WILDCARD_DECL);
15137 DECL_SOURCE_LOCATION (parm)
15138 = cp_lexer_peek_token (parser->lexer)->location;
15139 DECL_NAME (parm) = cp_parser_identifier (parser);
15140 WILDCARD_PACK_P (parm) = is_pack;
15141 vec_safe_push (introduction_vec, parm);
15143 /* If the next token is not a `,', we're done. */
15144 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15145 break;
15146 /* Otherwise, consume the `,' token. */
15147 cp_lexer_consume_token (parser->lexer);
15150 /* Convert the vec into a TREE_VEC. */
15151 tree introduction_list = make_tree_vec (introduction_vec->length ());
15152 unsigned int n;
15153 tree parm;
15154 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15155 TREE_VEC_ELT (introduction_list, n) = parm;
15157 release_tree_vector (introduction_vec);
15158 return introduction_list;
15161 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15162 is an abstract declarator. */
15164 static inline cp_declarator*
15165 get_id_declarator (cp_declarator *declarator)
15167 cp_declarator *d = declarator;
15168 while (d && d->kind != cdk_id)
15169 d = d->declarator;
15170 return d;
15173 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15174 is an abstract declarator. */
15176 static inline tree
15177 get_unqualified_id (cp_declarator *declarator)
15179 declarator = get_id_declarator (declarator);
15180 if (declarator)
15181 return declarator->u.id.unqualified_name;
15182 else
15183 return NULL_TREE;
15186 /* Returns true if DECL represents a constrained-parameter. */
15188 static inline bool
15189 is_constrained_parameter (tree decl)
15191 return (decl
15192 && TREE_CODE (decl) == TYPE_DECL
15193 && CONSTRAINED_PARM_CONCEPT (decl)
15194 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15197 /* Returns true if PARM declares a constrained-parameter. */
15199 static inline bool
15200 is_constrained_parameter (cp_parameter_declarator *parm)
15202 return is_constrained_parameter (parm->decl_specifiers.type);
15205 /* Check that the type parameter is only a declarator-id, and that its
15206 type is not cv-qualified. */
15208 bool
15209 cp_parser_check_constrained_type_parm (cp_parser *parser,
15210 cp_parameter_declarator *parm)
15212 if (!parm->declarator)
15213 return true;
15215 if (parm->declarator->kind != cdk_id)
15217 cp_parser_error (parser, "invalid constrained type parameter");
15218 return false;
15221 /* Don't allow cv-qualified type parameters. */
15222 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15223 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15225 cp_parser_error (parser, "cv-qualified type parameter");
15226 return false;
15229 return true;
15232 /* Finish parsing/processing a template type parameter and checking
15233 various restrictions. */
15235 static inline tree
15236 cp_parser_constrained_type_template_parm (cp_parser *parser,
15237 tree id,
15238 cp_parameter_declarator* parmdecl)
15240 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15241 return finish_template_type_parm (class_type_node, id);
15242 else
15243 return error_mark_node;
15246 static tree
15247 finish_constrained_template_template_parm (tree proto, tree id)
15249 /* FIXME: This should probably be copied, and we may need to adjust
15250 the template parameter depths. */
15251 tree saved_parms = current_template_parms;
15252 begin_template_parm_list ();
15253 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15254 end_template_parm_list ();
15256 tree parm = finish_template_template_parm (class_type_node, id);
15257 current_template_parms = saved_parms;
15259 return parm;
15262 /* Finish parsing/processing a template template parameter by borrowing
15263 the template parameter list from the prototype parameter. */
15265 static tree
15266 cp_parser_constrained_template_template_parm (cp_parser *parser,
15267 tree proto,
15268 tree id,
15269 cp_parameter_declarator *parmdecl)
15271 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15272 return error_mark_node;
15273 return finish_constrained_template_template_parm (proto, id);
15276 /* Create a new non-type template parameter from the given PARM
15277 declarator. */
15279 static tree
15280 constrained_non_type_template_parm (bool *is_non_type,
15281 cp_parameter_declarator *parm)
15283 *is_non_type = true;
15284 cp_declarator *decl = parm->declarator;
15285 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15286 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15287 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15290 /* Build a constrained template parameter based on the PARMDECL
15291 declarator. The type of PARMDECL is the constrained type, which
15292 refers to the prototype template parameter that ultimately
15293 specifies the type of the declared parameter. */
15295 static tree
15296 finish_constrained_parameter (cp_parser *parser,
15297 cp_parameter_declarator *parmdecl,
15298 bool *is_non_type,
15299 bool *is_parameter_pack)
15301 tree decl = parmdecl->decl_specifiers.type;
15302 tree id = get_unqualified_id (parmdecl->declarator);
15303 tree def = parmdecl->default_argument;
15304 tree proto = DECL_INITIAL (decl);
15306 /* A template parameter constrained by a variadic concept shall also
15307 be declared as a template parameter pack. */
15308 bool is_variadic = template_parameter_pack_p (proto);
15309 if (is_variadic && !*is_parameter_pack)
15310 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15312 /* Build the parameter. Return an error if the declarator was invalid. */
15313 tree parm;
15314 if (TREE_CODE (proto) == TYPE_DECL)
15315 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15316 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15317 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15318 parmdecl);
15319 else
15320 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15321 if (parm == error_mark_node)
15322 return error_mark_node;
15324 /* Finish the parameter decl and create a node attaching the
15325 default argument and constraint. */
15326 parm = build_tree_list (def, parm);
15327 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15329 return parm;
15332 /* Returns true if the parsed type actually represents the declaration
15333 of a type template-parameter. */
15335 static inline bool
15336 declares_constrained_type_template_parameter (tree type)
15338 return (is_constrained_parameter (type)
15339 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15343 /* Returns true if the parsed type actually represents the declaration of
15344 a template template-parameter. */
15346 static bool
15347 declares_constrained_template_template_parameter (tree type)
15349 return (is_constrained_parameter (type)
15350 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15353 /* Parse a default argument for a type template-parameter.
15354 Note that diagnostics are handled in cp_parser_template_parameter. */
15356 static tree
15357 cp_parser_default_type_template_argument (cp_parser *parser)
15359 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15361 /* Consume the `=' token. */
15362 cp_lexer_consume_token (parser->lexer);
15364 cp_token *token = cp_lexer_peek_token (parser->lexer);
15366 /* Parse the default-argument. */
15367 push_deferring_access_checks (dk_no_deferred);
15368 tree default_argument = cp_parser_type_id (parser);
15369 pop_deferring_access_checks ();
15371 if (flag_concepts && type_uses_auto (default_argument))
15373 error_at (token->location,
15374 "invalid use of %<auto%> in default template argument");
15375 return error_mark_node;
15378 return default_argument;
15381 /* Parse a default argument for a template template-parameter. */
15383 static tree
15384 cp_parser_default_template_template_argument (cp_parser *parser)
15386 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15388 bool is_template;
15390 /* Consume the `='. */
15391 cp_lexer_consume_token (parser->lexer);
15392 /* Parse the id-expression. */
15393 push_deferring_access_checks (dk_no_deferred);
15394 /* save token before parsing the id-expression, for error
15395 reporting */
15396 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15397 tree default_argument
15398 = cp_parser_id_expression (parser,
15399 /*template_keyword_p=*/false,
15400 /*check_dependency_p=*/true,
15401 /*template_p=*/&is_template,
15402 /*declarator_p=*/false,
15403 /*optional_p=*/false);
15404 if (TREE_CODE (default_argument) == TYPE_DECL)
15405 /* If the id-expression was a template-id that refers to
15406 a template-class, we already have the declaration here,
15407 so no further lookup is needed. */
15409 else
15410 /* Look up the name. */
15411 default_argument
15412 = cp_parser_lookup_name (parser, default_argument,
15413 none_type,
15414 /*is_template=*/is_template,
15415 /*is_namespace=*/false,
15416 /*check_dependency=*/true,
15417 /*ambiguous_decls=*/NULL,
15418 token->location);
15419 /* See if the default argument is valid. */
15420 default_argument = check_template_template_default_arg (default_argument);
15421 pop_deferring_access_checks ();
15422 return default_argument;
15425 /* Parse a template-parameter.
15427 template-parameter:
15428 type-parameter
15429 parameter-declaration
15431 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15432 the parameter. The TREE_PURPOSE is the default value, if any.
15433 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15434 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15435 set to true iff this parameter is a parameter pack. */
15437 static tree
15438 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15439 bool *is_parameter_pack)
15441 cp_token *token;
15442 cp_parameter_declarator *parameter_declarator;
15443 tree parm;
15445 /* Assume it is a type parameter or a template parameter. */
15446 *is_non_type = false;
15447 /* Assume it not a parameter pack. */
15448 *is_parameter_pack = false;
15449 /* Peek at the next token. */
15450 token = cp_lexer_peek_token (parser->lexer);
15451 /* If it is `template', we have a type-parameter. */
15452 if (token->keyword == RID_TEMPLATE)
15453 return cp_parser_type_parameter (parser, is_parameter_pack);
15454 /* If it is `class' or `typename' we do not know yet whether it is a
15455 type parameter or a non-type parameter. Consider:
15457 template <typename T, typename T::X X> ...
15461 template <class C, class D*> ...
15463 Here, the first parameter is a type parameter, and the second is
15464 a non-type parameter. We can tell by looking at the token after
15465 the identifier -- if it is a `,', `=', or `>' then we have a type
15466 parameter. */
15467 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15469 /* Peek at the token after `class' or `typename'. */
15470 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15471 /* If it's an ellipsis, we have a template type parameter
15472 pack. */
15473 if (token->type == CPP_ELLIPSIS)
15474 return cp_parser_type_parameter (parser, is_parameter_pack);
15475 /* If it's an identifier, skip it. */
15476 if (token->type == CPP_NAME)
15477 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15478 /* Now, see if the token looks like the end of a template
15479 parameter. */
15480 if (token->type == CPP_COMMA
15481 || token->type == CPP_EQ
15482 || token->type == CPP_GREATER)
15483 return cp_parser_type_parameter (parser, is_parameter_pack);
15486 /* Otherwise, it is a non-type parameter or a constrained parameter.
15488 [temp.param]
15490 When parsing a default template-argument for a non-type
15491 template-parameter, the first non-nested `>' is taken as the end
15492 of the template parameter-list rather than a greater-than
15493 operator. */
15494 parameter_declarator
15495 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15496 /*parenthesized_p=*/NULL);
15498 if (!parameter_declarator)
15499 return error_mark_node;
15501 /* If the parameter declaration is marked as a parameter pack, set
15502 *IS_PARAMETER_PACK to notify the caller. */
15503 if (parameter_declarator->template_parameter_pack_p)
15504 *is_parameter_pack = true;
15506 if (parameter_declarator->default_argument)
15508 /* Can happen in some cases of erroneous input (c++/34892). */
15509 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15510 /* Consume the `...' for better error recovery. */
15511 cp_lexer_consume_token (parser->lexer);
15514 // The parameter may have been constrained.
15515 if (is_constrained_parameter (parameter_declarator))
15516 return finish_constrained_parameter (parser,
15517 parameter_declarator,
15518 is_non_type,
15519 is_parameter_pack);
15521 // Now we're sure that the parameter is a non-type parameter.
15522 *is_non_type = true;
15524 parm = grokdeclarator (parameter_declarator->declarator,
15525 &parameter_declarator->decl_specifiers,
15526 TPARM, /*initialized=*/0,
15527 /*attrlist=*/NULL);
15528 if (parm == error_mark_node)
15529 return error_mark_node;
15531 return build_tree_list (parameter_declarator->default_argument, parm);
15534 /* Parse a type-parameter.
15536 type-parameter:
15537 class identifier [opt]
15538 class identifier [opt] = type-id
15539 typename identifier [opt]
15540 typename identifier [opt] = type-id
15541 template < template-parameter-list > class identifier [opt]
15542 template < template-parameter-list > class identifier [opt]
15543 = id-expression
15545 GNU Extension (variadic templates):
15547 type-parameter:
15548 class ... identifier [opt]
15549 typename ... identifier [opt]
15551 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15552 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15553 the declaration of the parameter.
15555 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15557 static tree
15558 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15560 cp_token *token;
15561 tree parameter;
15563 /* Look for a keyword to tell us what kind of parameter this is. */
15564 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15565 if (!token)
15566 return error_mark_node;
15568 switch (token->keyword)
15570 case RID_CLASS:
15571 case RID_TYPENAME:
15573 tree identifier;
15574 tree default_argument;
15576 /* If the next token is an ellipsis, we have a template
15577 argument pack. */
15578 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15580 /* Consume the `...' token. */
15581 cp_lexer_consume_token (parser->lexer);
15582 maybe_warn_variadic_templates ();
15584 *is_parameter_pack = true;
15587 /* If the next token is an identifier, then it names the
15588 parameter. */
15589 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15590 identifier = cp_parser_identifier (parser);
15591 else
15592 identifier = NULL_TREE;
15594 /* Create the parameter. */
15595 parameter = finish_template_type_parm (class_type_node, identifier);
15597 /* If the next token is an `=', we have a default argument. */
15598 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15600 default_argument
15601 = cp_parser_default_type_template_argument (parser);
15603 /* Template parameter packs cannot have default
15604 arguments. */
15605 if (*is_parameter_pack)
15607 if (identifier)
15608 error_at (token->location,
15609 "template parameter pack %qD cannot have a "
15610 "default argument", identifier);
15611 else
15612 error_at (token->location,
15613 "template parameter packs cannot have "
15614 "default arguments");
15615 default_argument = NULL_TREE;
15617 else if (check_for_bare_parameter_packs (default_argument))
15618 default_argument = error_mark_node;
15620 else
15621 default_argument = NULL_TREE;
15623 /* Create the combined representation of the parameter and the
15624 default argument. */
15625 parameter = build_tree_list (default_argument, parameter);
15627 break;
15629 case RID_TEMPLATE:
15631 tree identifier;
15632 tree default_argument;
15634 /* Look for the `<'. */
15635 cp_parser_require (parser, CPP_LESS, RT_LESS);
15636 /* Parse the template-parameter-list. */
15637 cp_parser_template_parameter_list (parser);
15638 /* Look for the `>'. */
15639 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15641 // If template requirements are present, parse them.
15642 if (flag_concepts)
15644 tree reqs = get_shorthand_constraints (current_template_parms);
15645 if (tree r = cp_parser_requires_clause_opt (parser))
15646 reqs = conjoin_constraints (reqs, normalize_expression (r));
15647 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15650 /* Look for the `class' or 'typename' keywords. */
15651 cp_parser_type_parameter_key (parser);
15652 /* If the next token is an ellipsis, we have a template
15653 argument pack. */
15654 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15656 /* Consume the `...' token. */
15657 cp_lexer_consume_token (parser->lexer);
15658 maybe_warn_variadic_templates ();
15660 *is_parameter_pack = true;
15662 /* If the next token is an `=', then there is a
15663 default-argument. If the next token is a `>', we are at
15664 the end of the parameter-list. If the next token is a `,',
15665 then we are at the end of this parameter. */
15666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15667 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15668 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15670 identifier = cp_parser_identifier (parser);
15671 /* Treat invalid names as if the parameter were nameless. */
15672 if (identifier == error_mark_node)
15673 identifier = NULL_TREE;
15675 else
15676 identifier = NULL_TREE;
15678 /* Create the template parameter. */
15679 parameter = finish_template_template_parm (class_type_node,
15680 identifier);
15682 /* If the next token is an `=', then there is a
15683 default-argument. */
15684 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15686 default_argument
15687 = cp_parser_default_template_template_argument (parser);
15689 /* Template parameter packs cannot have default
15690 arguments. */
15691 if (*is_parameter_pack)
15693 if (identifier)
15694 error_at (token->location,
15695 "template parameter pack %qD cannot "
15696 "have a default argument",
15697 identifier);
15698 else
15699 error_at (token->location, "template parameter packs cannot "
15700 "have default arguments");
15701 default_argument = NULL_TREE;
15704 else
15705 default_argument = NULL_TREE;
15707 /* Create the combined representation of the parameter and the
15708 default argument. */
15709 parameter = build_tree_list (default_argument, parameter);
15711 break;
15713 default:
15714 gcc_unreachable ();
15715 break;
15718 return parameter;
15721 /* Parse a template-id.
15723 template-id:
15724 template-name < template-argument-list [opt] >
15726 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15727 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15728 returned. Otherwise, if the template-name names a function, or set
15729 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15730 names a class, returns a TYPE_DECL for the specialization.
15732 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15733 uninstantiated templates. */
15735 static tree
15736 cp_parser_template_id (cp_parser *parser,
15737 bool template_keyword_p,
15738 bool check_dependency_p,
15739 enum tag_types tag_type,
15740 bool is_declaration)
15742 tree templ;
15743 tree arguments;
15744 tree template_id;
15745 cp_token_position start_of_id = 0;
15746 cp_token *next_token = NULL, *next_token_2 = NULL;
15747 bool is_identifier;
15749 /* If the next token corresponds to a template-id, there is no need
15750 to reparse it. */
15751 cp_token *token = cp_lexer_peek_token (parser->lexer);
15752 if (token->type == CPP_TEMPLATE_ID)
15754 cp_lexer_consume_token (parser->lexer);
15755 return saved_checks_value (token->u.tree_check_value);
15758 /* Avoid performing name lookup if there is no possibility of
15759 finding a template-id. */
15760 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15761 || (token->type == CPP_NAME
15762 && !cp_parser_nth_token_starts_template_argument_list_p
15763 (parser, 2)))
15765 cp_parser_error (parser, "expected template-id");
15766 return error_mark_node;
15769 /* Remember where the template-id starts. */
15770 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15771 start_of_id = cp_lexer_token_position (parser->lexer, false);
15773 push_deferring_access_checks (dk_deferred);
15775 /* Parse the template-name. */
15776 is_identifier = false;
15777 templ = cp_parser_template_name (parser, template_keyword_p,
15778 check_dependency_p,
15779 is_declaration,
15780 tag_type,
15781 &is_identifier);
15782 if (templ == error_mark_node || is_identifier)
15784 pop_deferring_access_checks ();
15785 return templ;
15788 /* Since we're going to preserve any side-effects from this parse, set up a
15789 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15790 in the template arguments. */
15791 tentative_firewall firewall (parser);
15793 /* If we find the sequence `[:' after a template-name, it's probably
15794 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15795 parse correctly the argument list. */
15796 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15797 == CPP_OPEN_SQUARE)
15798 && next_token->flags & DIGRAPH
15799 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15800 == CPP_COLON)
15801 && !(next_token_2->flags & PREV_WHITE))
15803 cp_parser_parse_tentatively (parser);
15804 /* Change `:' into `::'. */
15805 next_token_2->type = CPP_SCOPE;
15806 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15807 CPP_LESS. */
15808 cp_lexer_consume_token (parser->lexer);
15810 /* Parse the arguments. */
15811 arguments = cp_parser_enclosed_template_argument_list (parser);
15812 if (!cp_parser_parse_definitely (parser))
15814 /* If we couldn't parse an argument list, then we revert our changes
15815 and return simply an error. Maybe this is not a template-id
15816 after all. */
15817 next_token_2->type = CPP_COLON;
15818 cp_parser_error (parser, "expected %<<%>");
15819 pop_deferring_access_checks ();
15820 return error_mark_node;
15822 /* Otherwise, emit an error about the invalid digraph, but continue
15823 parsing because we got our argument list. */
15824 if (permerror (next_token->location,
15825 "%<<::%> cannot begin a template-argument list"))
15827 static bool hint = false;
15828 inform (next_token->location,
15829 "%<<:%> is an alternate spelling for %<[%>."
15830 " Insert whitespace between %<<%> and %<::%>");
15831 if (!hint && !flag_permissive)
15833 inform (next_token->location, "(if you use %<-fpermissive%> "
15834 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15835 "accept your code)");
15836 hint = true;
15840 else
15842 /* Look for the `<' that starts the template-argument-list. */
15843 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15845 pop_deferring_access_checks ();
15846 return error_mark_node;
15848 /* Parse the arguments. */
15849 arguments = cp_parser_enclosed_template_argument_list (parser);
15852 /* Set the location to be of the form:
15853 template-name < template-argument-list [opt] >
15854 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15855 with caret == start at the start of the template-name,
15856 ranging until the closing '>'. */
15857 location_t finish_loc
15858 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15859 location_t combined_loc
15860 = make_location (token->location, token->location, finish_loc);
15862 /* Build a representation of the specialization. */
15863 if (identifier_p (templ))
15864 template_id = build_min_nt_loc (combined_loc,
15865 TEMPLATE_ID_EXPR,
15866 templ, arguments);
15867 else if (DECL_TYPE_TEMPLATE_P (templ)
15868 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15870 bool entering_scope;
15871 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15872 template (rather than some instantiation thereof) only if
15873 is not nested within some other construct. For example, in
15874 "template <typename T> void f(T) { A<T>::", A<T> is just an
15875 instantiation of A. */
15876 entering_scope = (template_parm_scope_p ()
15877 && cp_lexer_next_token_is (parser->lexer,
15878 CPP_SCOPE));
15879 template_id
15880 = finish_template_type (templ, arguments, entering_scope);
15882 /* A template-like identifier may be a partial concept id. */
15883 else if (flag_concepts
15884 && (template_id = (cp_parser_maybe_partial_concept_id
15885 (parser, templ, arguments))))
15886 return template_id;
15887 else if (variable_template_p (templ))
15889 template_id = lookup_template_variable (templ, arguments);
15890 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15891 SET_EXPR_LOCATION (template_id, combined_loc);
15893 else
15895 /* If it's not a class-template or a template-template, it should be
15896 a function-template. */
15897 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15898 || TREE_CODE (templ) == OVERLOAD
15899 || BASELINK_P (templ)));
15901 template_id = lookup_template_function (templ, arguments);
15902 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15903 SET_EXPR_LOCATION (template_id, combined_loc);
15906 /* If parsing tentatively, replace the sequence of tokens that makes
15907 up the template-id with a CPP_TEMPLATE_ID token. That way,
15908 should we re-parse the token stream, we will not have to repeat
15909 the effort required to do the parse, nor will we issue duplicate
15910 error messages about problems during instantiation of the
15911 template. */
15912 if (start_of_id
15913 /* Don't do this if we had a parse error in a declarator; re-parsing
15914 might succeed if a name changes meaning (60361). */
15915 && !(cp_parser_error_occurred (parser)
15916 && cp_parser_parsing_tentatively (parser)
15917 && parser->in_declarator_p))
15919 /* Reset the contents of the START_OF_ID token. */
15920 token->type = CPP_TEMPLATE_ID;
15921 token->location = combined_loc;
15923 /* We must mark the lookup as kept, so we don't throw it away on
15924 the first parse. */
15925 if (is_overloaded_fn (template_id))
15926 lookup_keep (get_fns (template_id), true);
15928 /* Retrieve any deferred checks. Do not pop this access checks yet
15929 so the memory will not be reclaimed during token replacing below. */
15930 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15931 token->u.tree_check_value->value = template_id;
15932 token->u.tree_check_value->checks = get_deferred_access_checks ();
15933 token->keyword = RID_MAX;
15935 /* Purge all subsequent tokens. */
15936 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15938 /* ??? Can we actually assume that, if template_id ==
15939 error_mark_node, we will have issued a diagnostic to the
15940 user, as opposed to simply marking the tentative parse as
15941 failed? */
15942 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15943 error_at (token->location, "parse error in template argument list");
15946 pop_to_parent_deferring_access_checks ();
15947 return template_id;
15950 /* Parse a template-name.
15952 template-name:
15953 identifier
15955 The standard should actually say:
15957 template-name:
15958 identifier
15959 operator-function-id
15961 A defect report has been filed about this issue.
15963 A conversion-function-id cannot be a template name because they cannot
15964 be part of a template-id. In fact, looking at this code:
15966 a.operator K<int>()
15968 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15969 It is impossible to call a templated conversion-function-id with an
15970 explicit argument list, since the only allowed template parameter is
15971 the type to which it is converting.
15973 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15974 `template' keyword, in a construction like:
15976 T::template f<3>()
15978 In that case `f' is taken to be a template-name, even though there
15979 is no way of knowing for sure.
15981 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15982 name refers to a set of overloaded functions, at least one of which
15983 is a template, or an IDENTIFIER_NODE with the name of the template,
15984 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15985 names are looked up inside uninstantiated templates. */
15987 static tree
15988 cp_parser_template_name (cp_parser* parser,
15989 bool template_keyword_p,
15990 bool check_dependency_p,
15991 bool is_declaration,
15992 enum tag_types tag_type,
15993 bool *is_identifier)
15995 tree identifier;
15996 tree decl;
15997 cp_token *token = cp_lexer_peek_token (parser->lexer);
15999 /* If the next token is `operator', then we have either an
16000 operator-function-id or a conversion-function-id. */
16001 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
16003 /* We don't know whether we're looking at an
16004 operator-function-id or a conversion-function-id. */
16005 cp_parser_parse_tentatively (parser);
16006 /* Try an operator-function-id. */
16007 identifier = cp_parser_operator_function_id (parser);
16008 /* If that didn't work, try a conversion-function-id. */
16009 if (!cp_parser_parse_definitely (parser))
16011 cp_parser_error (parser, "expected template-name");
16012 return error_mark_node;
16015 /* Look for the identifier. */
16016 else
16017 identifier = cp_parser_identifier (parser);
16019 /* If we didn't find an identifier, we don't have a template-id. */
16020 if (identifier == error_mark_node)
16021 return error_mark_node;
16023 /* If the name immediately followed the `template' keyword, then it
16024 is a template-name. However, if the next token is not `<', then
16025 we do not treat it as a template-name, since it is not being used
16026 as part of a template-id. This enables us to handle constructs
16027 like:
16029 template <typename T> struct S { S(); };
16030 template <typename T> S<T>::S();
16032 correctly. We would treat `S' as a template -- if it were `S<T>'
16033 -- but we do not if there is no `<'. */
16035 if (processing_template_decl
16036 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
16038 /* In a declaration, in a dependent context, we pretend that the
16039 "template" keyword was present in order to improve error
16040 recovery. For example, given:
16042 template <typename T> void f(T::X<int>);
16044 we want to treat "X<int>" as a template-id. */
16045 if (is_declaration
16046 && !template_keyword_p
16047 && parser->scope && TYPE_P (parser->scope)
16048 && check_dependency_p
16049 && dependent_scope_p (parser->scope)
16050 /* Do not do this for dtors (or ctors), since they never
16051 need the template keyword before their name. */
16052 && !constructor_name_p (identifier, parser->scope))
16054 cp_token_position start = 0;
16056 /* Explain what went wrong. */
16057 error_at (token->location, "non-template %qD used as template",
16058 identifier);
16059 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
16060 parser->scope, identifier);
16061 /* If parsing tentatively, find the location of the "<" token. */
16062 if (cp_parser_simulate_error (parser))
16063 start = cp_lexer_token_position (parser->lexer, true);
16064 /* Parse the template arguments so that we can issue error
16065 messages about them. */
16066 cp_lexer_consume_token (parser->lexer);
16067 cp_parser_enclosed_template_argument_list (parser);
16068 /* Skip tokens until we find a good place from which to
16069 continue parsing. */
16070 cp_parser_skip_to_closing_parenthesis (parser,
16071 /*recovering=*/true,
16072 /*or_comma=*/true,
16073 /*consume_paren=*/false);
16074 /* If parsing tentatively, permanently remove the
16075 template argument list. That will prevent duplicate
16076 error messages from being issued about the missing
16077 "template" keyword. */
16078 if (start)
16079 cp_lexer_purge_tokens_after (parser->lexer, start);
16080 if (is_identifier)
16081 *is_identifier = true;
16082 parser->context->object_type = NULL_TREE;
16083 return identifier;
16086 /* If the "template" keyword is present, then there is generally
16087 no point in doing name-lookup, so we just return IDENTIFIER.
16088 But, if the qualifying scope is non-dependent then we can
16089 (and must) do name-lookup normally. */
16090 if (template_keyword_p)
16092 tree scope = (parser->scope ? parser->scope
16093 : parser->context->object_type);
16094 if (scope && TYPE_P (scope)
16095 && (!CLASS_TYPE_P (scope)
16096 || (check_dependency_p && dependent_type_p (scope))))
16098 /* We're optimizing away the call to cp_parser_lookup_name, but
16099 we still need to do this. */
16100 parser->context->object_type = NULL_TREE;
16101 return identifier;
16106 /* Look up the name. */
16107 decl = cp_parser_lookup_name (parser, identifier,
16108 tag_type,
16109 /*is_template=*/true,
16110 /*is_namespace=*/false,
16111 check_dependency_p,
16112 /*ambiguous_decls=*/NULL,
16113 token->location);
16115 decl = strip_using_decl (decl);
16117 /* If DECL is a template, then the name was a template-name. */
16118 if (TREE_CODE (decl) == TEMPLATE_DECL)
16120 if (TREE_DEPRECATED (decl)
16121 && deprecated_state != DEPRECATED_SUPPRESS)
16122 warn_deprecated_use (decl, NULL_TREE);
16124 else
16126 /* The standard does not explicitly indicate whether a name that
16127 names a set of overloaded declarations, some of which are
16128 templates, is a template-name. However, such a name should
16129 be a template-name; otherwise, there is no way to form a
16130 template-id for the overloaded templates. */
16131 bool found = false;
16133 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16134 !found && iter; ++iter)
16135 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16136 found = true;
16138 if (!found)
16140 /* The name does not name a template. */
16141 cp_parser_error (parser, "expected template-name");
16142 return error_mark_node;
16146 /* If DECL is dependent, and refers to a function, then just return
16147 its name; we will look it up again during template instantiation. */
16148 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16150 tree scope = ovl_scope (decl);
16151 if (TYPE_P (scope) && dependent_type_p (scope))
16152 return identifier;
16155 return decl;
16158 /* Parse a template-argument-list.
16160 template-argument-list:
16161 template-argument ... [opt]
16162 template-argument-list , template-argument ... [opt]
16164 Returns a TREE_VEC containing the arguments. */
16166 static tree
16167 cp_parser_template_argument_list (cp_parser* parser)
16169 tree fixed_args[10];
16170 unsigned n_args = 0;
16171 unsigned alloced = 10;
16172 tree *arg_ary = fixed_args;
16173 tree vec;
16174 bool saved_in_template_argument_list_p;
16175 bool saved_ice_p;
16176 bool saved_non_ice_p;
16178 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16179 parser->in_template_argument_list_p = true;
16180 /* Even if the template-id appears in an integral
16181 constant-expression, the contents of the argument list do
16182 not. */
16183 saved_ice_p = parser->integral_constant_expression_p;
16184 parser->integral_constant_expression_p = false;
16185 saved_non_ice_p = parser->non_integral_constant_expression_p;
16186 parser->non_integral_constant_expression_p = false;
16188 /* Parse the arguments. */
16191 tree argument;
16193 if (n_args)
16194 /* Consume the comma. */
16195 cp_lexer_consume_token (parser->lexer);
16197 /* Parse the template-argument. */
16198 argument = cp_parser_template_argument (parser);
16200 /* If the next token is an ellipsis, we're expanding a template
16201 argument pack. */
16202 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16204 if (argument == error_mark_node)
16206 cp_token *token = cp_lexer_peek_token (parser->lexer);
16207 error_at (token->location,
16208 "expected parameter pack before %<...%>");
16210 /* Consume the `...' token. */
16211 cp_lexer_consume_token (parser->lexer);
16213 /* Make the argument into a TYPE_PACK_EXPANSION or
16214 EXPR_PACK_EXPANSION. */
16215 argument = make_pack_expansion (argument);
16218 if (n_args == alloced)
16220 alloced *= 2;
16222 if (arg_ary == fixed_args)
16224 arg_ary = XNEWVEC (tree, alloced);
16225 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16227 else
16228 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16230 arg_ary[n_args++] = argument;
16232 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16234 vec = make_tree_vec (n_args);
16236 while (n_args--)
16237 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16239 if (arg_ary != fixed_args)
16240 free (arg_ary);
16241 parser->non_integral_constant_expression_p = saved_non_ice_p;
16242 parser->integral_constant_expression_p = saved_ice_p;
16243 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16244 if (CHECKING_P)
16245 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16246 return vec;
16249 /* Parse a template-argument.
16251 template-argument:
16252 assignment-expression
16253 type-id
16254 id-expression
16256 The representation is that of an assignment-expression, type-id, or
16257 id-expression -- except that the qualified id-expression is
16258 evaluated, so that the value returned is either a DECL or an
16259 OVERLOAD.
16261 Although the standard says "assignment-expression", it forbids
16262 throw-expressions or assignments in the template argument.
16263 Therefore, we use "conditional-expression" instead. */
16265 static tree
16266 cp_parser_template_argument (cp_parser* parser)
16268 tree argument;
16269 bool template_p;
16270 bool address_p;
16271 bool maybe_type_id = false;
16272 cp_token *token = NULL, *argument_start_token = NULL;
16273 location_t loc = 0;
16274 cp_id_kind idk;
16276 /* There's really no way to know what we're looking at, so we just
16277 try each alternative in order.
16279 [temp.arg]
16281 In a template-argument, an ambiguity between a type-id and an
16282 expression is resolved to a type-id, regardless of the form of
16283 the corresponding template-parameter.
16285 Therefore, we try a type-id first. */
16286 cp_parser_parse_tentatively (parser);
16287 argument = cp_parser_template_type_arg (parser);
16288 /* If there was no error parsing the type-id but the next token is a
16289 '>>', our behavior depends on which dialect of C++ we're
16290 parsing. In C++98, we probably found a typo for '> >'. But there
16291 are type-id which are also valid expressions. For instance:
16293 struct X { int operator >> (int); };
16294 template <int V> struct Foo {};
16295 Foo<X () >> 5> r;
16297 Here 'X()' is a valid type-id of a function type, but the user just
16298 wanted to write the expression "X() >> 5". Thus, we remember that we
16299 found a valid type-id, but we still try to parse the argument as an
16300 expression to see what happens.
16302 In C++0x, the '>>' will be considered two separate '>'
16303 tokens. */
16304 if (!cp_parser_error_occurred (parser)
16305 && cxx_dialect == cxx98
16306 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16308 maybe_type_id = true;
16309 cp_parser_abort_tentative_parse (parser);
16311 else
16313 /* If the next token isn't a `,' or a `>', then this argument wasn't
16314 really finished. This means that the argument is not a valid
16315 type-id. */
16316 if (!cp_parser_next_token_ends_template_argument_p (parser))
16317 cp_parser_error (parser, "expected template-argument");
16318 /* If that worked, we're done. */
16319 if (cp_parser_parse_definitely (parser))
16320 return argument;
16322 /* We're still not sure what the argument will be. */
16323 cp_parser_parse_tentatively (parser);
16324 /* Try a template. */
16325 argument_start_token = cp_lexer_peek_token (parser->lexer);
16326 argument = cp_parser_id_expression (parser,
16327 /*template_keyword_p=*/false,
16328 /*check_dependency_p=*/true,
16329 &template_p,
16330 /*declarator_p=*/false,
16331 /*optional_p=*/false);
16332 /* If the next token isn't a `,' or a `>', then this argument wasn't
16333 really finished. */
16334 if (!cp_parser_next_token_ends_template_argument_p (parser))
16335 cp_parser_error (parser, "expected template-argument");
16336 if (!cp_parser_error_occurred (parser))
16338 /* Figure out what is being referred to. If the id-expression
16339 was for a class template specialization, then we will have a
16340 TYPE_DECL at this point. There is no need to do name lookup
16341 at this point in that case. */
16342 if (TREE_CODE (argument) != TYPE_DECL)
16343 argument = cp_parser_lookup_name (parser, argument,
16344 none_type,
16345 /*is_template=*/template_p,
16346 /*is_namespace=*/false,
16347 /*check_dependency=*/true,
16348 /*ambiguous_decls=*/NULL,
16349 argument_start_token->location);
16350 /* Handle a constrained-type-specifier for a non-type template
16351 parameter. */
16352 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16353 argument = decl;
16354 else if (TREE_CODE (argument) != TEMPLATE_DECL
16355 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16356 cp_parser_error (parser, "expected template-name");
16358 if (cp_parser_parse_definitely (parser))
16360 if (TREE_DEPRECATED (argument))
16361 warn_deprecated_use (argument, NULL_TREE);
16362 return argument;
16364 /* It must be a non-type argument. In C++17 any constant-expression is
16365 allowed. */
16366 if (cxx_dialect > cxx14)
16367 goto general_expr;
16369 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16371 -- an integral constant-expression of integral or enumeration
16372 type; or
16374 -- the name of a non-type template-parameter; or
16376 -- the name of an object or function with external linkage...
16378 -- the address of an object or function with external linkage...
16380 -- a pointer to member... */
16381 /* Look for a non-type template parameter. */
16382 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16384 cp_parser_parse_tentatively (parser);
16385 argument = cp_parser_primary_expression (parser,
16386 /*address_p=*/false,
16387 /*cast_p=*/false,
16388 /*template_arg_p=*/true,
16389 &idk);
16390 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16391 || !cp_parser_next_token_ends_template_argument_p (parser))
16392 cp_parser_simulate_error (parser);
16393 if (cp_parser_parse_definitely (parser))
16394 return argument;
16397 /* If the next token is "&", the argument must be the address of an
16398 object or function with external linkage. */
16399 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16400 if (address_p)
16402 loc = cp_lexer_peek_token (parser->lexer)->location;
16403 cp_lexer_consume_token (parser->lexer);
16405 /* See if we might have an id-expression. */
16406 token = cp_lexer_peek_token (parser->lexer);
16407 if (token->type == CPP_NAME
16408 || token->keyword == RID_OPERATOR
16409 || token->type == CPP_SCOPE
16410 || token->type == CPP_TEMPLATE_ID
16411 || token->type == CPP_NESTED_NAME_SPECIFIER)
16413 cp_parser_parse_tentatively (parser);
16414 argument = cp_parser_primary_expression (parser,
16415 address_p,
16416 /*cast_p=*/false,
16417 /*template_arg_p=*/true,
16418 &idk);
16419 if (cp_parser_error_occurred (parser)
16420 || !cp_parser_next_token_ends_template_argument_p (parser))
16421 cp_parser_abort_tentative_parse (parser);
16422 else
16424 tree probe;
16426 if (INDIRECT_REF_P (argument))
16428 /* Strip the dereference temporarily. */
16429 gcc_assert (REFERENCE_REF_P (argument));
16430 argument = TREE_OPERAND (argument, 0);
16433 /* If we're in a template, we represent a qualified-id referring
16434 to a static data member as a SCOPE_REF even if the scope isn't
16435 dependent so that we can check access control later. */
16436 probe = argument;
16437 if (TREE_CODE (probe) == SCOPE_REF)
16438 probe = TREE_OPERAND (probe, 1);
16439 if (VAR_P (probe))
16441 /* A variable without external linkage might still be a
16442 valid constant-expression, so no error is issued here
16443 if the external-linkage check fails. */
16444 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16445 cp_parser_simulate_error (parser);
16447 else if (is_overloaded_fn (argument))
16448 /* All overloaded functions are allowed; if the external
16449 linkage test does not pass, an error will be issued
16450 later. */
16452 else if (address_p
16453 && (TREE_CODE (argument) == OFFSET_REF
16454 || TREE_CODE (argument) == SCOPE_REF))
16455 /* A pointer-to-member. */
16457 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16459 else
16460 cp_parser_simulate_error (parser);
16462 if (cp_parser_parse_definitely (parser))
16464 if (address_p)
16465 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16466 tf_warning_or_error);
16467 else
16468 argument = convert_from_reference (argument);
16469 return argument;
16473 /* If the argument started with "&", there are no other valid
16474 alternatives at this point. */
16475 if (address_p)
16477 cp_parser_error (parser, "invalid non-type template argument");
16478 return error_mark_node;
16481 general_expr:
16482 /* If the argument wasn't successfully parsed as a type-id followed
16483 by '>>', the argument can only be a constant expression now.
16484 Otherwise, we try parsing the constant-expression tentatively,
16485 because the argument could really be a type-id. */
16486 if (maybe_type_id)
16487 cp_parser_parse_tentatively (parser);
16489 if (cxx_dialect <= cxx14)
16490 argument = cp_parser_constant_expression (parser);
16491 else
16493 /* With C++17 generalized non-type template arguments we need to handle
16494 lvalue constant expressions, too. */
16495 argument = cp_parser_assignment_expression (parser);
16496 require_potential_constant_expression (argument);
16499 if (!maybe_type_id)
16500 return argument;
16501 if (!cp_parser_next_token_ends_template_argument_p (parser))
16502 cp_parser_error (parser, "expected template-argument");
16503 if (cp_parser_parse_definitely (parser))
16504 return argument;
16505 /* We did our best to parse the argument as a non type-id, but that
16506 was the only alternative that matched (albeit with a '>' after
16507 it). We can assume it's just a typo from the user, and a
16508 diagnostic will then be issued. */
16509 return cp_parser_template_type_arg (parser);
16512 /* Parse an explicit-instantiation.
16514 explicit-instantiation:
16515 template declaration
16517 Although the standard says `declaration', what it really means is:
16519 explicit-instantiation:
16520 template decl-specifier-seq [opt] declarator [opt] ;
16522 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16523 supposed to be allowed. A defect report has been filed about this
16524 issue.
16526 GNU Extension:
16528 explicit-instantiation:
16529 storage-class-specifier template
16530 decl-specifier-seq [opt] declarator [opt] ;
16531 function-specifier template
16532 decl-specifier-seq [opt] declarator [opt] ; */
16534 static void
16535 cp_parser_explicit_instantiation (cp_parser* parser)
16537 int declares_class_or_enum;
16538 cp_decl_specifier_seq decl_specifiers;
16539 tree extension_specifier = NULL_TREE;
16541 timevar_push (TV_TEMPLATE_INST);
16543 /* Look for an (optional) storage-class-specifier or
16544 function-specifier. */
16545 if (cp_parser_allow_gnu_extensions_p (parser))
16547 extension_specifier
16548 = cp_parser_storage_class_specifier_opt (parser);
16549 if (!extension_specifier)
16550 extension_specifier
16551 = cp_parser_function_specifier_opt (parser,
16552 /*decl_specs=*/NULL);
16555 /* Look for the `template' keyword. */
16556 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16557 /* Let the front end know that we are processing an explicit
16558 instantiation. */
16559 begin_explicit_instantiation ();
16560 /* [temp.explicit] says that we are supposed to ignore access
16561 control while processing explicit instantiation directives. */
16562 push_deferring_access_checks (dk_no_check);
16563 /* Parse a decl-specifier-seq. */
16564 cp_parser_decl_specifier_seq (parser,
16565 CP_PARSER_FLAGS_OPTIONAL,
16566 &decl_specifiers,
16567 &declares_class_or_enum);
16568 /* If there was exactly one decl-specifier, and it declared a class,
16569 and there's no declarator, then we have an explicit type
16570 instantiation. */
16571 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16573 tree type;
16575 type = check_tag_decl (&decl_specifiers,
16576 /*explicit_type_instantiation_p=*/true);
16577 /* Turn access control back on for names used during
16578 template instantiation. */
16579 pop_deferring_access_checks ();
16580 if (type)
16581 do_type_instantiation (type, extension_specifier,
16582 /*complain=*/tf_error);
16584 else
16586 cp_declarator *declarator;
16587 tree decl;
16589 /* Parse the declarator. */
16590 declarator
16591 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16592 /*ctor_dtor_or_conv_p=*/NULL,
16593 /*parenthesized_p=*/NULL,
16594 /*member_p=*/false,
16595 /*friend_p=*/false);
16596 if (declares_class_or_enum & 2)
16597 cp_parser_check_for_definition_in_return_type (declarator,
16598 decl_specifiers.type,
16599 decl_specifiers.locations[ds_type_spec]);
16600 if (declarator != cp_error_declarator)
16602 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16603 permerror (decl_specifiers.locations[ds_inline],
16604 "explicit instantiation shall not use"
16605 " %<inline%> specifier");
16606 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16607 permerror (decl_specifiers.locations[ds_constexpr],
16608 "explicit instantiation shall not use"
16609 " %<constexpr%> specifier");
16611 decl = grokdeclarator (declarator, &decl_specifiers,
16612 NORMAL, 0, &decl_specifiers.attributes);
16613 /* Turn access control back on for names used during
16614 template instantiation. */
16615 pop_deferring_access_checks ();
16616 /* Do the explicit instantiation. */
16617 do_decl_instantiation (decl, extension_specifier);
16619 else
16621 pop_deferring_access_checks ();
16622 /* Skip the body of the explicit instantiation. */
16623 cp_parser_skip_to_end_of_statement (parser);
16626 /* We're done with the instantiation. */
16627 end_explicit_instantiation ();
16629 cp_parser_consume_semicolon_at_end_of_statement (parser);
16631 timevar_pop (TV_TEMPLATE_INST);
16634 /* Parse an explicit-specialization.
16636 explicit-specialization:
16637 template < > declaration
16639 Although the standard says `declaration', what it really means is:
16641 explicit-specialization:
16642 template <> decl-specifier [opt] init-declarator [opt] ;
16643 template <> function-definition
16644 template <> explicit-specialization
16645 template <> template-declaration */
16647 static void
16648 cp_parser_explicit_specialization (cp_parser* parser)
16650 bool need_lang_pop;
16651 cp_token *token = cp_lexer_peek_token (parser->lexer);
16653 /* Look for the `template' keyword. */
16654 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16655 /* Look for the `<'. */
16656 cp_parser_require (parser, CPP_LESS, RT_LESS);
16657 /* Look for the `>'. */
16658 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16659 /* We have processed another parameter list. */
16660 ++parser->num_template_parameter_lists;
16661 /* [temp]
16663 A template ... explicit specialization ... shall not have C
16664 linkage. */
16665 if (current_lang_name == lang_name_c)
16667 error_at (token->location, "template specialization with C linkage");
16668 maybe_show_extern_c_location ();
16669 /* Give it C++ linkage to avoid confusing other parts of the
16670 front end. */
16671 push_lang_context (lang_name_cplusplus);
16672 need_lang_pop = true;
16674 else
16675 need_lang_pop = false;
16676 /* Let the front end know that we are beginning a specialization. */
16677 if (!begin_specialization ())
16679 end_specialization ();
16680 return;
16683 /* If the next keyword is `template', we need to figure out whether
16684 or not we're looking a template-declaration. */
16685 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16687 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16688 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16689 cp_parser_template_declaration_after_export (parser,
16690 /*member_p=*/false);
16691 else
16692 cp_parser_explicit_specialization (parser);
16694 else
16695 /* Parse the dependent declaration. */
16696 cp_parser_single_declaration (parser,
16697 /*checks=*/NULL,
16698 /*member_p=*/false,
16699 /*explicit_specialization_p=*/true,
16700 /*friend_p=*/NULL);
16701 /* We're done with the specialization. */
16702 end_specialization ();
16703 /* For the erroneous case of a template with C linkage, we pushed an
16704 implicit C++ linkage scope; exit that scope now. */
16705 if (need_lang_pop)
16706 pop_lang_context ();
16707 /* We're done with this parameter list. */
16708 --parser->num_template_parameter_lists;
16711 /* Parse a type-specifier.
16713 type-specifier:
16714 simple-type-specifier
16715 class-specifier
16716 enum-specifier
16717 elaborated-type-specifier
16718 cv-qualifier
16720 GNU Extension:
16722 type-specifier:
16723 __complex__
16725 Returns a representation of the type-specifier. For a
16726 class-specifier, enum-specifier, or elaborated-type-specifier, a
16727 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16729 The parser flags FLAGS is used to control type-specifier parsing.
16731 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16732 in a decl-specifier-seq.
16734 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16735 class-specifier, enum-specifier, or elaborated-type-specifier, then
16736 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16737 if a type is declared; 2 if it is defined. Otherwise, it is set to
16738 zero.
16740 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16741 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16742 is set to FALSE. */
16744 static tree
16745 cp_parser_type_specifier (cp_parser* parser,
16746 cp_parser_flags flags,
16747 cp_decl_specifier_seq *decl_specs,
16748 bool is_declaration,
16749 int* declares_class_or_enum,
16750 bool* is_cv_qualifier)
16752 tree type_spec = NULL_TREE;
16753 cp_token *token;
16754 enum rid keyword;
16755 cp_decl_spec ds = ds_last;
16757 /* Assume this type-specifier does not declare a new type. */
16758 if (declares_class_or_enum)
16759 *declares_class_or_enum = 0;
16760 /* And that it does not specify a cv-qualifier. */
16761 if (is_cv_qualifier)
16762 *is_cv_qualifier = false;
16763 /* Peek at the next token. */
16764 token = cp_lexer_peek_token (parser->lexer);
16766 /* If we're looking at a keyword, we can use that to guide the
16767 production we choose. */
16768 keyword = token->keyword;
16769 switch (keyword)
16771 case RID_ENUM:
16772 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16773 goto elaborated_type_specifier;
16775 /* Look for the enum-specifier. */
16776 type_spec = cp_parser_enum_specifier (parser);
16777 /* If that worked, we're done. */
16778 if (type_spec)
16780 if (declares_class_or_enum)
16781 *declares_class_or_enum = 2;
16782 if (decl_specs)
16783 cp_parser_set_decl_spec_type (decl_specs,
16784 type_spec,
16785 token,
16786 /*type_definition_p=*/true);
16787 return type_spec;
16789 else
16790 goto elaborated_type_specifier;
16792 /* Any of these indicate either a class-specifier, or an
16793 elaborated-type-specifier. */
16794 case RID_CLASS:
16795 case RID_STRUCT:
16796 case RID_UNION:
16797 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16798 goto elaborated_type_specifier;
16800 /* Parse tentatively so that we can back up if we don't find a
16801 class-specifier. */
16802 cp_parser_parse_tentatively (parser);
16803 /* Look for the class-specifier. */
16804 type_spec = cp_parser_class_specifier (parser);
16805 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16806 /* If that worked, we're done. */
16807 if (cp_parser_parse_definitely (parser))
16809 if (declares_class_or_enum)
16810 *declares_class_or_enum = 2;
16811 if (decl_specs)
16812 cp_parser_set_decl_spec_type (decl_specs,
16813 type_spec,
16814 token,
16815 /*type_definition_p=*/true);
16816 return type_spec;
16819 /* Fall through. */
16820 elaborated_type_specifier:
16821 /* We're declaring (not defining) a class or enum. */
16822 if (declares_class_or_enum)
16823 *declares_class_or_enum = 1;
16825 /* Fall through. */
16826 case RID_TYPENAME:
16827 /* Look for an elaborated-type-specifier. */
16828 type_spec
16829 = (cp_parser_elaborated_type_specifier
16830 (parser,
16831 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16832 is_declaration));
16833 if (decl_specs)
16834 cp_parser_set_decl_spec_type (decl_specs,
16835 type_spec,
16836 token,
16837 /*type_definition_p=*/false);
16838 return type_spec;
16840 case RID_CONST:
16841 ds = ds_const;
16842 if (is_cv_qualifier)
16843 *is_cv_qualifier = true;
16844 break;
16846 case RID_VOLATILE:
16847 ds = ds_volatile;
16848 if (is_cv_qualifier)
16849 *is_cv_qualifier = true;
16850 break;
16852 case RID_RESTRICT:
16853 ds = ds_restrict;
16854 if (is_cv_qualifier)
16855 *is_cv_qualifier = true;
16856 break;
16858 case RID_COMPLEX:
16859 /* The `__complex__' keyword is a GNU extension. */
16860 ds = ds_complex;
16861 break;
16863 default:
16864 break;
16867 /* Handle simple keywords. */
16868 if (ds != ds_last)
16870 if (decl_specs)
16872 set_and_check_decl_spec_loc (decl_specs, ds, token);
16873 decl_specs->any_specifiers_p = true;
16875 return cp_lexer_consume_token (parser->lexer)->u.value;
16878 /* If we do not already have a type-specifier, assume we are looking
16879 at a simple-type-specifier. */
16880 type_spec = cp_parser_simple_type_specifier (parser,
16881 decl_specs,
16882 flags);
16884 /* If we didn't find a type-specifier, and a type-specifier was not
16885 optional in this context, issue an error message. */
16886 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16888 cp_parser_error (parser, "expected type specifier");
16889 return error_mark_node;
16892 return type_spec;
16895 /* Parse a simple-type-specifier.
16897 simple-type-specifier:
16898 :: [opt] nested-name-specifier [opt] type-name
16899 :: [opt] nested-name-specifier template template-id
16900 char
16901 wchar_t
16902 bool
16903 short
16905 long
16906 signed
16907 unsigned
16908 float
16909 double
16910 void
16912 C++11 Extension:
16914 simple-type-specifier:
16915 auto
16916 decltype ( expression )
16917 char16_t
16918 char32_t
16919 __underlying_type ( type-id )
16921 C++17 extension:
16923 nested-name-specifier(opt) template-name
16925 GNU Extension:
16927 simple-type-specifier:
16928 __int128
16929 __typeof__ unary-expression
16930 __typeof__ ( type-id )
16931 __typeof__ ( type-id ) { initializer-list , [opt] }
16933 Concepts Extension:
16935 simple-type-specifier:
16936 constrained-type-specifier
16938 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16939 appropriately updated. */
16941 static tree
16942 cp_parser_simple_type_specifier (cp_parser* parser,
16943 cp_decl_specifier_seq *decl_specs,
16944 cp_parser_flags flags)
16946 tree type = NULL_TREE;
16947 cp_token *token;
16948 int idx;
16950 /* Peek at the next token. */
16951 token = cp_lexer_peek_token (parser->lexer);
16953 /* If we're looking at a keyword, things are easy. */
16954 switch (token->keyword)
16956 case RID_CHAR:
16957 if (decl_specs)
16958 decl_specs->explicit_char_p = true;
16959 type = char_type_node;
16960 break;
16961 case RID_CHAR16:
16962 type = char16_type_node;
16963 break;
16964 case RID_CHAR32:
16965 type = char32_type_node;
16966 break;
16967 case RID_WCHAR:
16968 type = wchar_type_node;
16969 break;
16970 case RID_BOOL:
16971 type = boolean_type_node;
16972 break;
16973 case RID_SHORT:
16974 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16975 type = short_integer_type_node;
16976 break;
16977 case RID_INT:
16978 if (decl_specs)
16979 decl_specs->explicit_int_p = true;
16980 type = integer_type_node;
16981 break;
16982 case RID_INT_N_0:
16983 case RID_INT_N_1:
16984 case RID_INT_N_2:
16985 case RID_INT_N_3:
16986 idx = token->keyword - RID_INT_N_0;
16987 if (! int_n_enabled_p [idx])
16988 break;
16989 if (decl_specs)
16991 decl_specs->explicit_intN_p = true;
16992 decl_specs->int_n_idx = idx;
16994 type = int_n_trees [idx].signed_type;
16995 break;
16996 case RID_LONG:
16997 if (decl_specs)
16998 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16999 type = long_integer_type_node;
17000 break;
17001 case RID_SIGNED:
17002 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
17003 type = integer_type_node;
17004 break;
17005 case RID_UNSIGNED:
17006 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
17007 type = unsigned_type_node;
17008 break;
17009 case RID_FLOAT:
17010 type = float_type_node;
17011 break;
17012 case RID_DOUBLE:
17013 type = double_type_node;
17014 break;
17015 case RID_VOID:
17016 type = void_type_node;
17017 break;
17019 case RID_AUTO:
17020 maybe_warn_cpp0x (CPP0X_AUTO);
17021 if (parser->auto_is_implicit_function_template_parm_p)
17023 /* The 'auto' might be the placeholder return type for a function decl
17024 with trailing return type. */
17025 bool have_trailing_return_fn_decl = false;
17027 cp_parser_parse_tentatively (parser);
17028 cp_lexer_consume_token (parser->lexer);
17029 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
17030 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
17031 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17032 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
17034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17036 cp_lexer_consume_token (parser->lexer);
17037 cp_parser_skip_to_closing_parenthesis (parser,
17038 /*recovering*/false,
17039 /*or_comma*/false,
17040 /*consume_paren*/true);
17041 continue;
17044 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
17046 have_trailing_return_fn_decl = true;
17047 break;
17050 cp_lexer_consume_token (parser->lexer);
17052 cp_parser_abort_tentative_parse (parser);
17054 if (have_trailing_return_fn_decl)
17056 type = make_auto ();
17057 break;
17060 if (cxx_dialect >= cxx14)
17062 type = synthesize_implicit_template_parm (parser, NULL_TREE);
17063 type = TREE_TYPE (type);
17065 else
17066 type = error_mark_node;
17068 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
17070 if (cxx_dialect < cxx14)
17071 error_at (token->location,
17072 "use of %<auto%> in lambda parameter declaration "
17073 "only available with "
17074 "-std=c++14 or -std=gnu++14");
17076 else if (cxx_dialect < cxx14)
17077 error_at (token->location,
17078 "use of %<auto%> in parameter declaration "
17079 "only available with "
17080 "-std=c++14 or -std=gnu++14");
17081 else if (!flag_concepts)
17082 pedwarn (token->location, OPT_Wpedantic,
17083 "ISO C++ forbids use of %<auto%> in parameter "
17084 "declaration");
17086 else
17087 type = make_auto ();
17088 break;
17090 case RID_DECLTYPE:
17091 /* Since DR 743, decltype can either be a simple-type-specifier by
17092 itself or begin a nested-name-specifier. Parsing it will replace
17093 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17094 handling below decide what to do. */
17095 cp_parser_decltype (parser);
17096 cp_lexer_set_token_position (parser->lexer, token);
17097 break;
17099 case RID_TYPEOF:
17100 /* Consume the `typeof' token. */
17101 cp_lexer_consume_token (parser->lexer);
17102 /* Parse the operand to `typeof'. */
17103 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17104 /* If it is not already a TYPE, take its type. */
17105 if (!TYPE_P (type))
17106 type = finish_typeof (type);
17108 if (decl_specs)
17109 cp_parser_set_decl_spec_type (decl_specs, type,
17110 token,
17111 /*type_definition_p=*/false);
17113 return type;
17115 case RID_UNDERLYING_TYPE:
17116 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17117 if (decl_specs)
17118 cp_parser_set_decl_spec_type (decl_specs, type,
17119 token,
17120 /*type_definition_p=*/false);
17122 return type;
17124 case RID_BASES:
17125 case RID_DIRECT_BASES:
17126 type = cp_parser_trait_expr (parser, token->keyword);
17127 if (decl_specs)
17128 cp_parser_set_decl_spec_type (decl_specs, type,
17129 token,
17130 /*type_definition_p=*/false);
17131 return type;
17132 default:
17133 break;
17136 /* If token is an already-parsed decltype not followed by ::,
17137 it's a simple-type-specifier. */
17138 if (token->type == CPP_DECLTYPE
17139 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17141 type = saved_checks_value (token->u.tree_check_value);
17142 if (decl_specs)
17144 cp_parser_set_decl_spec_type (decl_specs, type,
17145 token,
17146 /*type_definition_p=*/false);
17147 /* Remember that we are handling a decltype in order to
17148 implement the resolution of DR 1510 when the argument
17149 isn't instantiation dependent. */
17150 decl_specs->decltype_p = true;
17152 cp_lexer_consume_token (parser->lexer);
17153 return type;
17156 /* If the type-specifier was for a built-in type, we're done. */
17157 if (type)
17159 /* Record the type. */
17160 if (decl_specs
17161 && (token->keyword != RID_SIGNED
17162 && token->keyword != RID_UNSIGNED
17163 && token->keyword != RID_SHORT
17164 && token->keyword != RID_LONG))
17165 cp_parser_set_decl_spec_type (decl_specs,
17166 type,
17167 token,
17168 /*type_definition_p=*/false);
17169 if (decl_specs)
17170 decl_specs->any_specifiers_p = true;
17172 /* Consume the token. */
17173 cp_lexer_consume_token (parser->lexer);
17175 if (type == error_mark_node)
17176 return error_mark_node;
17178 /* There is no valid C++ program where a non-template type is
17179 followed by a "<". That usually indicates that the user thought
17180 that the type was a template. */
17181 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17182 token->location);
17184 return TYPE_NAME (type);
17187 /* The type-specifier must be a user-defined type. */
17188 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17190 bool qualified_p;
17191 bool global_p;
17193 /* Don't gobble tokens or issue error messages if this is an
17194 optional type-specifier. */
17195 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17196 cp_parser_parse_tentatively (parser);
17198 token = cp_lexer_peek_token (parser->lexer);
17200 /* Look for the optional `::' operator. */
17201 global_p
17202 = (cp_parser_global_scope_opt (parser,
17203 /*current_scope_valid_p=*/false)
17204 != NULL_TREE);
17205 /* Look for the nested-name specifier. */
17206 qualified_p
17207 = (cp_parser_nested_name_specifier_opt (parser,
17208 /*typename_keyword_p=*/false,
17209 /*check_dependency_p=*/true,
17210 /*type_p=*/false,
17211 /*is_declaration=*/false)
17212 != NULL_TREE);
17213 /* If we have seen a nested-name-specifier, and the next token
17214 is `template', then we are using the template-id production. */
17215 if (parser->scope
17216 && cp_parser_optional_template_keyword (parser))
17218 /* Look for the template-id. */
17219 type = cp_parser_template_id (parser,
17220 /*template_keyword_p=*/true,
17221 /*check_dependency_p=*/true,
17222 none_type,
17223 /*is_declaration=*/false);
17224 /* If the template-id did not name a type, we are out of
17225 luck. */
17226 if (TREE_CODE (type) != TYPE_DECL)
17228 cp_parser_error (parser, "expected template-id for type");
17229 type = NULL_TREE;
17232 /* Otherwise, look for a type-name. */
17233 else
17234 type = cp_parser_type_name (parser);
17235 /* Keep track of all name-lookups performed in class scopes. */
17236 if (type
17237 && !global_p
17238 && !qualified_p
17239 && TREE_CODE (type) == TYPE_DECL
17240 && identifier_p (DECL_NAME (type)))
17241 maybe_note_name_used_in_class (DECL_NAME (type), type);
17242 /* If it didn't work out, we don't have a TYPE. */
17243 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx17)
17244 && !cp_parser_parse_definitely (parser))
17245 type = NULL_TREE;
17246 if (!type && cxx_dialect >= cxx17)
17248 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17249 cp_parser_parse_tentatively (parser);
17251 cp_parser_global_scope_opt (parser,
17252 /*current_scope_valid_p=*/false);
17253 cp_parser_nested_name_specifier_opt (parser,
17254 /*typename_keyword_p=*/false,
17255 /*check_dependency_p=*/true,
17256 /*type_p=*/false,
17257 /*is_declaration=*/false);
17258 tree name = cp_parser_identifier (parser);
17259 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17260 && parser->scope != error_mark_node)
17262 tree tmpl = cp_parser_lookup_name (parser, name,
17263 none_type,
17264 /*is_template=*/false,
17265 /*is_namespace=*/false,
17266 /*check_dependency=*/true,
17267 /*ambiguous_decls=*/NULL,
17268 token->location);
17269 if (tmpl && tmpl != error_mark_node
17270 && (DECL_CLASS_TEMPLATE_P (tmpl)
17271 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17272 type = make_template_placeholder (tmpl);
17273 else
17275 type = error_mark_node;
17276 if (!cp_parser_simulate_error (parser))
17277 cp_parser_name_lookup_error (parser, name, tmpl,
17278 NLE_TYPE, token->location);
17281 else
17282 type = error_mark_node;
17284 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17285 && !cp_parser_parse_definitely (parser))
17286 type = NULL_TREE;
17288 if (type && decl_specs)
17289 cp_parser_set_decl_spec_type (decl_specs, type,
17290 token,
17291 /*type_definition_p=*/false);
17294 /* If we didn't get a type-name, issue an error message. */
17295 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17297 cp_parser_error (parser, "expected type-name");
17298 return error_mark_node;
17301 if (type && type != error_mark_node)
17303 /* See if TYPE is an Objective-C type, and if so, parse and
17304 accept any protocol references following it. Do this before
17305 the cp_parser_check_for_invalid_template_id() call, because
17306 Objective-C types can be followed by '<...>' which would
17307 enclose protocol names rather than template arguments, and so
17308 everything is fine. */
17309 if (c_dialect_objc () && !parser->scope
17310 && (objc_is_id (type) || objc_is_class_name (type)))
17312 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17313 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17315 /* Clobber the "unqualified" type previously entered into
17316 DECL_SPECS with the new, improved protocol-qualified version. */
17317 if (decl_specs)
17318 decl_specs->type = qual_type;
17320 return qual_type;
17323 /* There is no valid C++ program where a non-template type is
17324 followed by a "<". That usually indicates that the user
17325 thought that the type was a template. */
17326 cp_parser_check_for_invalid_template_id (parser, type,
17327 none_type,
17328 token->location);
17331 return type;
17334 /* Parse a type-name.
17336 type-name:
17337 class-name
17338 enum-name
17339 typedef-name
17340 simple-template-id [in c++0x]
17342 enum-name:
17343 identifier
17345 typedef-name:
17346 identifier
17348 Concepts:
17350 type-name:
17351 concept-name
17352 partial-concept-id
17354 concept-name:
17355 identifier
17357 Returns a TYPE_DECL for the type. */
17359 static tree
17360 cp_parser_type_name (cp_parser* parser)
17362 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17365 /* See above. */
17366 static tree
17367 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17369 tree type_decl;
17371 /* We can't know yet whether it is a class-name or not. */
17372 cp_parser_parse_tentatively (parser);
17373 /* Try a class-name. */
17374 type_decl = cp_parser_class_name (parser,
17375 typename_keyword_p,
17376 /*template_keyword_p=*/false,
17377 none_type,
17378 /*check_dependency_p=*/true,
17379 /*class_head_p=*/false,
17380 /*is_declaration=*/false);
17381 /* If it's not a class-name, keep looking. */
17382 if (!cp_parser_parse_definitely (parser))
17384 if (cxx_dialect < cxx11)
17385 /* It must be a typedef-name or an enum-name. */
17386 return cp_parser_nonclass_name (parser);
17388 cp_parser_parse_tentatively (parser);
17389 /* It is either a simple-template-id representing an
17390 instantiation of an alias template... */
17391 type_decl = cp_parser_template_id (parser,
17392 /*template_keyword_p=*/false,
17393 /*check_dependency_p=*/true,
17394 none_type,
17395 /*is_declaration=*/false);
17396 /* Note that this must be an instantiation of an alias template
17397 because [temp.names]/6 says:
17399 A template-id that names an alias template specialization
17400 is a type-name.
17402 Whereas [temp.names]/7 says:
17404 A simple-template-id that names a class template
17405 specialization is a class-name.
17407 With concepts, this could also be a partial-concept-id that
17408 declares a non-type template parameter. */
17409 if (type_decl != NULL_TREE
17410 && TREE_CODE (type_decl) == TYPE_DECL
17411 && TYPE_DECL_ALIAS_P (type_decl))
17412 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17413 else if (is_constrained_parameter (type_decl))
17414 /* Don't do anything. */ ;
17415 else
17416 cp_parser_simulate_error (parser);
17418 if (!cp_parser_parse_definitely (parser))
17419 /* ... Or a typedef-name or an enum-name. */
17420 return cp_parser_nonclass_name (parser);
17423 return type_decl;
17426 /* Check if DECL and ARGS can form a constrained-type-specifier.
17427 If ARGS is non-null, we try to form a concept check of the
17428 form DECL<?, ARGS> where ? is a wildcard that matches any
17429 kind of template argument. If ARGS is NULL, then we try to
17430 form a concept check of the form DECL<?>. */
17432 static tree
17433 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17434 tree decl, tree args)
17436 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17438 /* If we a constrained-type-specifier cannot be deduced. */
17439 if (parser->prevent_constrained_type_specifiers)
17440 return NULL_TREE;
17442 /* A constrained type specifier can only be found in an
17443 overload set or as a reference to a template declaration.
17445 FIXME: This might be masking a bug. It's possible that
17446 that the deduction below is causing template specializations
17447 to be formed with the wildcard as an argument. */
17448 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17449 return NULL_TREE;
17451 /* Try to build a call expression that evaluates the
17452 concept. This can fail if the overload set refers
17453 only to non-templates. */
17454 tree placeholder = build_nt (WILDCARD_DECL);
17455 tree check = build_concept_check (decl, placeholder, args);
17456 if (check == error_mark_node)
17457 return NULL_TREE;
17459 /* Deduce the checked constraint and the prototype parameter.
17461 FIXME: In certain cases, failure to deduce should be a
17462 diagnosable error. */
17463 tree conc;
17464 tree proto;
17465 if (!deduce_constrained_parameter (check, conc, proto))
17466 return NULL_TREE;
17468 /* In template parameter scope, this results in a constrained
17469 parameter. Return a descriptor of that parm. */
17470 if (processing_template_parmlist)
17471 return build_constrained_parameter (conc, proto, args);
17473 /* In a parameter-declaration-clause, constrained-type
17474 specifiers result in invented template parameters. */
17475 if (parser->auto_is_implicit_function_template_parm_p)
17477 tree x = build_constrained_parameter (conc, proto, args);
17478 return synthesize_implicit_template_parm (parser, x);
17480 else
17482 /* Otherwise, we're in a context where the constrained
17483 type name is deduced and the constraint applies
17484 after deduction. */
17485 return make_constrained_auto (conc, args);
17488 return NULL_TREE;
17491 /* If DECL refers to a concept, return a TYPE_DECL representing
17492 the result of using the constrained type specifier in the
17493 current context. DECL refers to a concept if
17495 - it is an overload set containing a function concept taking a single
17496 type argument, or
17498 - it is a variable concept taking a single type argument. */
17500 static tree
17501 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17503 if (flag_concepts
17504 && (TREE_CODE (decl) == OVERLOAD
17505 || BASELINK_P (decl)
17506 || variable_concept_p (decl)))
17507 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17508 else
17509 return NULL_TREE;
17512 /* Check if DECL and ARGS form a partial-concept-id. If so,
17513 assign ID to the resulting constrained placeholder.
17515 Returns true if the partial-concept-id designates a placeholder
17516 and false otherwise. Note that *id is set to NULL_TREE in
17517 this case. */
17519 static tree
17520 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17522 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17525 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17526 or a concept-name.
17528 enum-name:
17529 identifier
17531 typedef-name:
17532 identifier
17534 concept-name:
17535 identifier
17537 Returns a TYPE_DECL for the type. */
17539 static tree
17540 cp_parser_nonclass_name (cp_parser* parser)
17542 tree type_decl;
17543 tree identifier;
17545 cp_token *token = cp_lexer_peek_token (parser->lexer);
17546 identifier = cp_parser_identifier (parser);
17547 if (identifier == error_mark_node)
17548 return error_mark_node;
17550 /* Look up the type-name. */
17551 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17553 type_decl = strip_using_decl (type_decl);
17555 /* If we found an overload set, then it may refer to a concept-name. */
17556 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17557 type_decl = decl;
17559 if (TREE_CODE (type_decl) != TYPE_DECL
17560 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17562 /* See if this is an Objective-C type. */
17563 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17564 tree type = objc_get_protocol_qualified_type (identifier, protos);
17565 if (type)
17566 type_decl = TYPE_NAME (type);
17569 /* Issue an error if we did not find a type-name. */
17570 if (TREE_CODE (type_decl) != TYPE_DECL
17571 /* In Objective-C, we have the complication that class names are
17572 normally type names and start declarations (eg, the
17573 "NSObject" in "NSObject *object;"), but can be used in an
17574 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17575 is an expression. So, a classname followed by a dot is not a
17576 valid type-name. */
17577 || (objc_is_class_name (TREE_TYPE (type_decl))
17578 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17580 if (!cp_parser_simulate_error (parser))
17581 cp_parser_name_lookup_error (parser, identifier, type_decl,
17582 NLE_TYPE, token->location);
17583 return error_mark_node;
17585 /* Remember that the name was used in the definition of the
17586 current class so that we can check later to see if the
17587 meaning would have been different after the class was
17588 entirely defined. */
17589 else if (type_decl != error_mark_node
17590 && !parser->scope)
17591 maybe_note_name_used_in_class (identifier, type_decl);
17593 return type_decl;
17596 /* Parse an elaborated-type-specifier. Note that the grammar given
17597 here incorporates the resolution to DR68.
17599 elaborated-type-specifier:
17600 class-key :: [opt] nested-name-specifier [opt] identifier
17601 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17602 enum-key :: [opt] nested-name-specifier [opt] identifier
17603 typename :: [opt] nested-name-specifier identifier
17604 typename :: [opt] nested-name-specifier template [opt]
17605 template-id
17607 GNU extension:
17609 elaborated-type-specifier:
17610 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17611 class-key attributes :: [opt] nested-name-specifier [opt]
17612 template [opt] template-id
17613 enum attributes :: [opt] nested-name-specifier [opt] identifier
17615 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17616 declared `friend'. If IS_DECLARATION is TRUE, then this
17617 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17618 something is being declared.
17620 Returns the TYPE specified. */
17622 static tree
17623 cp_parser_elaborated_type_specifier (cp_parser* parser,
17624 bool is_friend,
17625 bool is_declaration)
17627 enum tag_types tag_type;
17628 tree identifier;
17629 tree type = NULL_TREE;
17630 tree attributes = NULL_TREE;
17631 tree globalscope;
17632 cp_token *token = NULL;
17634 /* See if we're looking at the `enum' keyword. */
17635 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17637 /* Consume the `enum' token. */
17638 cp_lexer_consume_token (parser->lexer);
17639 /* Remember that it's an enumeration type. */
17640 tag_type = enum_type;
17641 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17642 enums) is used here. */
17643 cp_token *token = cp_lexer_peek_token (parser->lexer);
17644 if (cp_parser_is_keyword (token, RID_CLASS)
17645 || cp_parser_is_keyword (token, RID_STRUCT))
17647 gcc_rich_location richloc (token->location);
17648 richloc.add_range (input_location, false);
17649 richloc.add_fixit_remove ();
17650 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17651 "a scoped enum must not use the %qD keyword",
17652 token->u.value);
17653 /* Consume the `struct' or `class' and parse it anyway. */
17654 cp_lexer_consume_token (parser->lexer);
17656 /* Parse the attributes. */
17657 attributes = cp_parser_attributes_opt (parser);
17659 /* Or, it might be `typename'. */
17660 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17661 RID_TYPENAME))
17663 /* Consume the `typename' token. */
17664 cp_lexer_consume_token (parser->lexer);
17665 /* Remember that it's a `typename' type. */
17666 tag_type = typename_type;
17668 /* Otherwise it must be a class-key. */
17669 else
17671 tag_type = cp_parser_class_key (parser);
17672 if (tag_type == none_type)
17673 return error_mark_node;
17674 /* Parse the attributes. */
17675 attributes = cp_parser_attributes_opt (parser);
17678 /* Look for the `::' operator. */
17679 globalscope = cp_parser_global_scope_opt (parser,
17680 /*current_scope_valid_p=*/false);
17681 /* Look for the nested-name-specifier. */
17682 tree nested_name_specifier;
17683 if (tag_type == typename_type && !globalscope)
17685 nested_name_specifier
17686 = cp_parser_nested_name_specifier (parser,
17687 /*typename_keyword_p=*/true,
17688 /*check_dependency_p=*/true,
17689 /*type_p=*/true,
17690 is_declaration);
17691 if (!nested_name_specifier)
17692 return error_mark_node;
17694 else
17695 /* Even though `typename' is not present, the proposed resolution
17696 to Core Issue 180 says that in `class A<T>::B', `B' should be
17697 considered a type-name, even if `A<T>' is dependent. */
17698 nested_name_specifier
17699 = cp_parser_nested_name_specifier_opt (parser,
17700 /*typename_keyword_p=*/true,
17701 /*check_dependency_p=*/true,
17702 /*type_p=*/true,
17703 is_declaration);
17704 /* For everything but enumeration types, consider a template-id.
17705 For an enumeration type, consider only a plain identifier. */
17706 if (tag_type != enum_type)
17708 bool template_p = false;
17709 tree decl;
17711 /* Allow the `template' keyword. */
17712 template_p = cp_parser_optional_template_keyword (parser);
17713 /* If we didn't see `template', we don't know if there's a
17714 template-id or not. */
17715 if (!template_p)
17716 cp_parser_parse_tentatively (parser);
17717 /* Parse the template-id. */
17718 token = cp_lexer_peek_token (parser->lexer);
17719 decl = cp_parser_template_id (parser, template_p,
17720 /*check_dependency_p=*/true,
17721 tag_type,
17722 is_declaration);
17723 /* If we didn't find a template-id, look for an ordinary
17724 identifier. */
17725 if (!template_p && !cp_parser_parse_definitely (parser))
17727 /* We can get here when cp_parser_template_id, called by
17728 cp_parser_class_name with tag_type == none_type, succeeds
17729 and caches a BASELINK. Then, when called again here,
17730 instead of failing and returning an error_mark_node
17731 returns it (see template/typename17.C in C++11).
17732 ??? Could we diagnose this earlier? */
17733 else if (tag_type == typename_type && BASELINK_P (decl))
17735 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17736 type = error_mark_node;
17738 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17739 in effect, then we must assume that, upon instantiation, the
17740 template will correspond to a class. */
17741 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17742 && tag_type == typename_type)
17743 type = make_typename_type (parser->scope, decl,
17744 typename_type,
17745 /*complain=*/tf_error);
17746 /* If the `typename' keyword is in effect and DECL is not a type
17747 decl, then type is non existent. */
17748 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17750 else if (TREE_CODE (decl) == TYPE_DECL)
17752 type = check_elaborated_type_specifier (tag_type, decl,
17753 /*allow_template_p=*/true);
17755 /* If the next token is a semicolon, this must be a specialization,
17756 instantiation, or friend declaration. Check the scope while we
17757 still know whether or not we had a nested-name-specifier. */
17758 if (type != error_mark_node
17759 && !nested_name_specifier && !is_friend
17760 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17761 check_unqualified_spec_or_inst (type, token->location);
17763 else if (decl == error_mark_node)
17764 type = error_mark_node;
17767 if (!type)
17769 token = cp_lexer_peek_token (parser->lexer);
17770 identifier = cp_parser_identifier (parser);
17772 if (identifier == error_mark_node)
17774 parser->scope = NULL_TREE;
17775 return error_mark_node;
17778 /* For a `typename', we needn't call xref_tag. */
17779 if (tag_type == typename_type
17780 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17781 return cp_parser_make_typename_type (parser, identifier,
17782 token->location);
17784 /* Template parameter lists apply only if we are not within a
17785 function parameter list. */
17786 bool template_parm_lists_apply
17787 = parser->num_template_parameter_lists;
17788 if (template_parm_lists_apply)
17789 for (cp_binding_level *s = current_binding_level;
17790 s && s->kind != sk_template_parms;
17791 s = s->level_chain)
17792 if (s->kind == sk_function_parms)
17793 template_parm_lists_apply = false;
17795 /* Look up a qualified name in the usual way. */
17796 if (parser->scope)
17798 tree decl;
17799 tree ambiguous_decls;
17801 decl = cp_parser_lookup_name (parser, identifier,
17802 tag_type,
17803 /*is_template=*/false,
17804 /*is_namespace=*/false,
17805 /*check_dependency=*/true,
17806 &ambiguous_decls,
17807 token->location);
17809 /* If the lookup was ambiguous, an error will already have been
17810 issued. */
17811 if (ambiguous_decls)
17812 return error_mark_node;
17814 /* If we are parsing friend declaration, DECL may be a
17815 TEMPLATE_DECL tree node here. However, we need to check
17816 whether this TEMPLATE_DECL results in valid code. Consider
17817 the following example:
17819 namespace N {
17820 template <class T> class C {};
17822 class X {
17823 template <class T> friend class N::C; // #1, valid code
17825 template <class T> class Y {
17826 friend class N::C; // #2, invalid code
17829 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17830 name lookup of `N::C'. We see that friend declaration must
17831 be template for the code to be valid. Note that
17832 processing_template_decl does not work here since it is
17833 always 1 for the above two cases. */
17835 decl = (cp_parser_maybe_treat_template_as_class
17836 (decl, /*tag_name_p=*/is_friend
17837 && template_parm_lists_apply));
17839 if (TREE_CODE (decl) != TYPE_DECL)
17841 cp_parser_diagnose_invalid_type_name (parser,
17842 identifier,
17843 token->location);
17844 return error_mark_node;
17847 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17849 bool allow_template = (template_parm_lists_apply
17850 || DECL_SELF_REFERENCE_P (decl));
17851 type = check_elaborated_type_specifier (tag_type, decl,
17852 allow_template);
17854 if (type == error_mark_node)
17855 return error_mark_node;
17858 /* Forward declarations of nested types, such as
17860 class C1::C2;
17861 class C1::C2::C3;
17863 are invalid unless all components preceding the final '::'
17864 are complete. If all enclosing types are complete, these
17865 declarations become merely pointless.
17867 Invalid forward declarations of nested types are errors
17868 caught elsewhere in parsing. Those that are pointless arrive
17869 here. */
17871 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17872 && !is_friend && !processing_explicit_instantiation)
17873 warning (0, "declaration %qD does not declare anything", decl);
17875 type = TREE_TYPE (decl);
17877 else
17879 /* An elaborated-type-specifier sometimes introduces a new type and
17880 sometimes names an existing type. Normally, the rule is that it
17881 introduces a new type only if there is not an existing type of
17882 the same name already in scope. For example, given:
17884 struct S {};
17885 void f() { struct S s; }
17887 the `struct S' in the body of `f' is the same `struct S' as in
17888 the global scope; the existing definition is used. However, if
17889 there were no global declaration, this would introduce a new
17890 local class named `S'.
17892 An exception to this rule applies to the following code:
17894 namespace N { struct S; }
17896 Here, the elaborated-type-specifier names a new type
17897 unconditionally; even if there is already an `S' in the
17898 containing scope this declaration names a new type.
17899 This exception only applies if the elaborated-type-specifier
17900 forms the complete declaration:
17902 [class.name]
17904 A declaration consisting solely of `class-key identifier ;' is
17905 either a redeclaration of the name in the current scope or a
17906 forward declaration of the identifier as a class name. It
17907 introduces the name into the current scope.
17909 We are in this situation precisely when the next token is a `;'.
17911 An exception to the exception is that a `friend' declaration does
17912 *not* name a new type; i.e., given:
17914 struct S { friend struct T; };
17916 `T' is not a new type in the scope of `S'.
17918 Also, `new struct S' or `sizeof (struct S)' never results in the
17919 definition of a new type; a new type can only be declared in a
17920 declaration context. */
17922 tag_scope ts;
17923 bool template_p;
17925 if (is_friend)
17926 /* Friends have special name lookup rules. */
17927 ts = ts_within_enclosing_non_class;
17928 else if (is_declaration
17929 && cp_lexer_next_token_is (parser->lexer,
17930 CPP_SEMICOLON))
17931 /* This is a `class-key identifier ;' */
17932 ts = ts_current;
17933 else
17934 ts = ts_global;
17936 template_p =
17937 (template_parm_lists_apply
17938 && (cp_parser_next_token_starts_class_definition_p (parser)
17939 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17940 /* An unqualified name was used to reference this type, so
17941 there were no qualifying templates. */
17942 if (template_parm_lists_apply
17943 && !cp_parser_check_template_parameters (parser,
17944 /*num_templates=*/0,
17945 token->location,
17946 /*declarator=*/NULL))
17947 return error_mark_node;
17948 type = xref_tag (tag_type, identifier, ts, template_p);
17952 if (type == error_mark_node)
17953 return error_mark_node;
17955 /* Allow attributes on forward declarations of classes. */
17956 if (attributes)
17958 if (TREE_CODE (type) == TYPENAME_TYPE)
17959 warning (OPT_Wattributes,
17960 "attributes ignored on uninstantiated type");
17961 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17962 && ! processing_explicit_instantiation)
17963 warning (OPT_Wattributes,
17964 "attributes ignored on template instantiation");
17965 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17966 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17967 else
17968 warning (OPT_Wattributes,
17969 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17972 if (tag_type != enum_type)
17974 /* Indicate whether this class was declared as a `class' or as a
17975 `struct'. */
17976 if (CLASS_TYPE_P (type))
17977 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17978 cp_parser_check_class_key (tag_type, type);
17981 /* A "<" cannot follow an elaborated type specifier. If that
17982 happens, the user was probably trying to form a template-id. */
17983 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17984 token->location);
17986 return type;
17989 /* Parse an enum-specifier.
17991 enum-specifier:
17992 enum-head { enumerator-list [opt] }
17993 enum-head { enumerator-list , } [C++0x]
17995 enum-head:
17996 enum-key identifier [opt] enum-base [opt]
17997 enum-key nested-name-specifier identifier enum-base [opt]
17999 enum-key:
18000 enum
18001 enum class [C++0x]
18002 enum struct [C++0x]
18004 enum-base: [C++0x]
18005 : type-specifier-seq
18007 opaque-enum-specifier:
18008 enum-key identifier enum-base [opt] ;
18010 GNU Extensions:
18011 enum-key attributes[opt] identifier [opt] enum-base [opt]
18012 { enumerator-list [opt] }attributes[opt]
18013 enum-key attributes[opt] identifier [opt] enum-base [opt]
18014 { enumerator-list, }attributes[opt] [C++0x]
18016 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18017 if the token stream isn't an enum-specifier after all. */
18019 static tree
18020 cp_parser_enum_specifier (cp_parser* parser)
18022 tree identifier;
18023 tree type = NULL_TREE;
18024 tree prev_scope;
18025 tree nested_name_specifier = NULL_TREE;
18026 tree attributes;
18027 bool scoped_enum_p = false;
18028 bool has_underlying_type = false;
18029 bool nested_being_defined = false;
18030 bool new_value_list = false;
18031 bool is_new_type = false;
18032 bool is_unnamed = false;
18033 tree underlying_type = NULL_TREE;
18034 cp_token *type_start_token = NULL;
18035 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18037 parser->colon_corrects_to_scope_p = false;
18039 /* Parse tentatively so that we can back up if we don't find a
18040 enum-specifier. */
18041 cp_parser_parse_tentatively (parser);
18043 /* Caller guarantees that the current token is 'enum', an identifier
18044 possibly follows, and the token after that is an opening brace.
18045 If we don't have an identifier, fabricate an anonymous name for
18046 the enumeration being defined. */
18047 cp_lexer_consume_token (parser->lexer);
18049 /* Parse the "class" or "struct", which indicates a scoped
18050 enumeration type in C++0x. */
18051 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
18052 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
18054 if (cxx_dialect < cxx11)
18055 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18057 /* Consume the `struct' or `class' token. */
18058 cp_lexer_consume_token (parser->lexer);
18060 scoped_enum_p = true;
18063 attributes = cp_parser_attributes_opt (parser);
18065 /* Clear the qualification. */
18066 parser->scope = NULL_TREE;
18067 parser->qualifying_scope = NULL_TREE;
18068 parser->object_scope = NULL_TREE;
18070 /* Figure out in what scope the declaration is being placed. */
18071 prev_scope = current_scope ();
18073 type_start_token = cp_lexer_peek_token (parser->lexer);
18075 push_deferring_access_checks (dk_no_check);
18076 nested_name_specifier
18077 = cp_parser_nested_name_specifier_opt (parser,
18078 /*typename_keyword_p=*/true,
18079 /*check_dependency_p=*/false,
18080 /*type_p=*/false,
18081 /*is_declaration=*/false);
18083 if (nested_name_specifier)
18085 tree name;
18087 identifier = cp_parser_identifier (parser);
18088 name = cp_parser_lookup_name (parser, identifier,
18089 enum_type,
18090 /*is_template=*/false,
18091 /*is_namespace=*/false,
18092 /*check_dependency=*/true,
18093 /*ambiguous_decls=*/NULL,
18094 input_location);
18095 if (name && name != error_mark_node)
18097 type = TREE_TYPE (name);
18098 if (TREE_CODE (type) == TYPENAME_TYPE)
18100 /* Are template enums allowed in ISO? */
18101 if (template_parm_scope_p ())
18102 pedwarn (type_start_token->location, OPT_Wpedantic,
18103 "%qD is an enumeration template", name);
18104 /* ignore a typename reference, for it will be solved by name
18105 in start_enum. */
18106 type = NULL_TREE;
18109 else if (nested_name_specifier == error_mark_node)
18110 /* We already issued an error. */;
18111 else
18113 error_at (type_start_token->location,
18114 "%qD does not name an enumeration in %qT",
18115 identifier, nested_name_specifier);
18116 nested_name_specifier = error_mark_node;
18119 else
18121 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18122 identifier = cp_parser_identifier (parser);
18123 else
18125 identifier = make_anon_name ();
18126 is_unnamed = true;
18127 if (scoped_enum_p)
18128 error_at (type_start_token->location,
18129 "unnamed scoped enum is not allowed");
18132 pop_deferring_access_checks ();
18134 /* Check for the `:' that denotes a specified underlying type in C++0x.
18135 Note that a ':' could also indicate a bitfield width, however. */
18136 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18138 cp_decl_specifier_seq type_specifiers;
18140 /* Consume the `:'. */
18141 cp_lexer_consume_token (parser->lexer);
18143 /* Parse the type-specifier-seq. */
18144 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18145 /*is_trailing_return=*/false,
18146 &type_specifiers);
18148 /* At this point this is surely not elaborated type specifier. */
18149 if (!cp_parser_parse_definitely (parser))
18150 return NULL_TREE;
18152 if (cxx_dialect < cxx11)
18153 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18155 has_underlying_type = true;
18157 /* If that didn't work, stop. */
18158 if (type_specifiers.type != error_mark_node)
18160 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18161 /*initialized=*/0, NULL);
18162 if (underlying_type == error_mark_node
18163 || check_for_bare_parameter_packs (underlying_type))
18164 underlying_type = NULL_TREE;
18168 /* Look for the `{' but don't consume it yet. */
18169 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18171 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18173 cp_parser_error (parser, "expected %<{%>");
18174 if (has_underlying_type)
18176 type = NULL_TREE;
18177 goto out;
18180 /* An opaque-enum-specifier must have a ';' here. */
18181 if ((scoped_enum_p || underlying_type)
18182 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18184 cp_parser_error (parser, "expected %<;%> or %<{%>");
18185 if (has_underlying_type)
18187 type = NULL_TREE;
18188 goto out;
18193 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18194 return NULL_TREE;
18196 if (nested_name_specifier)
18198 if (CLASS_TYPE_P (nested_name_specifier))
18200 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18201 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18202 push_scope (nested_name_specifier);
18204 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18206 push_nested_namespace (nested_name_specifier);
18210 /* Issue an error message if type-definitions are forbidden here. */
18211 if (!cp_parser_check_type_definition (parser))
18212 type = error_mark_node;
18213 else
18214 /* Create the new type. We do this before consuming the opening
18215 brace so the enum will be recorded as being on the line of its
18216 tag (or the 'enum' keyword, if there is no tag). */
18217 type = start_enum (identifier, type, underlying_type,
18218 attributes, scoped_enum_p, &is_new_type);
18220 /* If the next token is not '{' it is an opaque-enum-specifier or an
18221 elaborated-type-specifier. */
18222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18224 timevar_push (TV_PARSE_ENUM);
18225 if (nested_name_specifier
18226 && nested_name_specifier != error_mark_node)
18228 /* The following catches invalid code such as:
18229 enum class S<int>::E { A, B, C }; */
18230 if (!processing_specialization
18231 && CLASS_TYPE_P (nested_name_specifier)
18232 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18233 error_at (type_start_token->location, "cannot add an enumerator "
18234 "list to a template instantiation");
18236 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18238 error_at (type_start_token->location,
18239 "%<%T::%E%> has not been declared",
18240 TYPE_CONTEXT (nested_name_specifier),
18241 nested_name_specifier);
18242 type = error_mark_node;
18244 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18245 && !CLASS_TYPE_P (nested_name_specifier))
18247 error_at (type_start_token->location, "nested name specifier "
18248 "%qT for enum declaration does not name a class "
18249 "or namespace", nested_name_specifier);
18250 type = error_mark_node;
18252 /* If that scope does not contain the scope in which the
18253 class was originally declared, the program is invalid. */
18254 else if (prev_scope && !is_ancestor (prev_scope,
18255 nested_name_specifier))
18257 if (at_namespace_scope_p ())
18258 error_at (type_start_token->location,
18259 "declaration of %qD in namespace %qD which does not "
18260 "enclose %qD",
18261 type, prev_scope, nested_name_specifier);
18262 else
18263 error_at (type_start_token->location,
18264 "declaration of %qD in %qD which does not "
18265 "enclose %qD",
18266 type, prev_scope, nested_name_specifier);
18267 type = error_mark_node;
18269 /* If that scope is the scope where the declaration is being placed
18270 the program is invalid. */
18271 else if (CLASS_TYPE_P (nested_name_specifier)
18272 && CLASS_TYPE_P (prev_scope)
18273 && same_type_p (nested_name_specifier, prev_scope))
18275 permerror (type_start_token->location,
18276 "extra qualification not allowed");
18277 nested_name_specifier = NULL_TREE;
18281 if (scoped_enum_p)
18282 begin_scope (sk_scoped_enum, type);
18284 /* Consume the opening brace. */
18285 matching_braces braces;
18286 braces.consume_open (parser);
18288 if (type == error_mark_node)
18289 ; /* Nothing to add */
18290 else if (OPAQUE_ENUM_P (type)
18291 || (cxx_dialect > cxx98 && processing_specialization))
18293 new_value_list = true;
18294 SET_OPAQUE_ENUM_P (type, false);
18295 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18297 else
18299 error_at (type_start_token->location,
18300 "multiple definition of %q#T", type);
18301 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18302 "previous definition here");
18303 type = error_mark_node;
18306 if (type == error_mark_node)
18307 cp_parser_skip_to_end_of_block_or_statement (parser);
18308 /* If the next token is not '}', then there are some enumerators. */
18309 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18311 if (is_unnamed && !scoped_enum_p)
18312 pedwarn (type_start_token->location, OPT_Wpedantic,
18313 "ISO C++ forbids empty unnamed enum");
18315 else
18316 cp_parser_enumerator_list (parser, type);
18318 /* Consume the final '}'. */
18319 braces.require_close (parser);
18321 if (scoped_enum_p)
18322 finish_scope ();
18323 timevar_pop (TV_PARSE_ENUM);
18325 else
18327 /* If a ';' follows, then it is an opaque-enum-specifier
18328 and additional restrictions apply. */
18329 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18331 if (is_unnamed)
18332 error_at (type_start_token->location,
18333 "opaque-enum-specifier without name");
18334 else if (nested_name_specifier)
18335 error_at (type_start_token->location,
18336 "opaque-enum-specifier must use a simple identifier");
18340 /* Look for trailing attributes to apply to this enumeration, and
18341 apply them if appropriate. */
18342 if (cp_parser_allow_gnu_extensions_p (parser))
18344 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18345 cplus_decl_attributes (&type,
18346 trailing_attr,
18347 (int) ATTR_FLAG_TYPE_IN_PLACE);
18350 /* Finish up the enumeration. */
18351 if (type != error_mark_node)
18353 if (new_value_list)
18354 finish_enum_value_list (type);
18355 if (is_new_type)
18356 finish_enum (type);
18359 if (nested_name_specifier)
18361 if (CLASS_TYPE_P (nested_name_specifier))
18363 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18364 pop_scope (nested_name_specifier);
18366 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18368 pop_nested_namespace (nested_name_specifier);
18371 out:
18372 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18373 return type;
18376 /* Parse an enumerator-list. The enumerators all have the indicated
18377 TYPE.
18379 enumerator-list:
18380 enumerator-definition
18381 enumerator-list , enumerator-definition */
18383 static void
18384 cp_parser_enumerator_list (cp_parser* parser, tree type)
18386 while (true)
18388 /* Parse an enumerator-definition. */
18389 cp_parser_enumerator_definition (parser, type);
18391 /* If the next token is not a ',', we've reached the end of
18392 the list. */
18393 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18394 break;
18395 /* Otherwise, consume the `,' and keep going. */
18396 cp_lexer_consume_token (parser->lexer);
18397 /* If the next token is a `}', there is a trailing comma. */
18398 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18400 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18401 pedwarn (input_location, OPT_Wpedantic,
18402 "comma at end of enumerator list");
18403 break;
18408 /* Parse an enumerator-definition. The enumerator has the indicated
18409 TYPE.
18411 enumerator-definition:
18412 enumerator
18413 enumerator = constant-expression
18415 enumerator:
18416 identifier
18418 GNU Extensions:
18420 enumerator-definition:
18421 enumerator attributes [opt]
18422 enumerator attributes [opt] = constant-expression */
18424 static void
18425 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18427 tree identifier;
18428 tree value;
18429 location_t loc;
18431 /* Save the input location because we are interested in the location
18432 of the identifier and not the location of the explicit value. */
18433 loc = cp_lexer_peek_token (parser->lexer)->location;
18435 /* Look for the identifier. */
18436 identifier = cp_parser_identifier (parser);
18437 if (identifier == error_mark_node)
18438 return;
18440 /* Parse any specified attributes. */
18441 tree attrs = cp_parser_attributes_opt (parser);
18443 /* If the next token is an '=', then there is an explicit value. */
18444 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18446 /* Consume the `=' token. */
18447 cp_lexer_consume_token (parser->lexer);
18448 /* Parse the value. */
18449 value = cp_parser_constant_expression (parser);
18451 else
18452 value = NULL_TREE;
18454 /* If we are processing a template, make sure the initializer of the
18455 enumerator doesn't contain any bare template parameter pack. */
18456 if (check_for_bare_parameter_packs (value))
18457 value = error_mark_node;
18459 /* Create the enumerator. */
18460 build_enumerator (identifier, value, type, attrs, loc);
18463 /* Parse a namespace-name.
18465 namespace-name:
18466 original-namespace-name
18467 namespace-alias
18469 Returns the NAMESPACE_DECL for the namespace. */
18471 static tree
18472 cp_parser_namespace_name (cp_parser* parser)
18474 tree identifier;
18475 tree namespace_decl;
18477 cp_token *token = cp_lexer_peek_token (parser->lexer);
18479 /* Get the name of the namespace. */
18480 identifier = cp_parser_identifier (parser);
18481 if (identifier == error_mark_node)
18482 return error_mark_node;
18484 /* Look up the identifier in the currently active scope. Look only
18485 for namespaces, due to:
18487 [basic.lookup.udir]
18489 When looking up a namespace-name in a using-directive or alias
18490 definition, only namespace names are considered.
18492 And:
18494 [basic.lookup.qual]
18496 During the lookup of a name preceding the :: scope resolution
18497 operator, object, function, and enumerator names are ignored.
18499 (Note that cp_parser_qualifying_entity only calls this
18500 function if the token after the name is the scope resolution
18501 operator.) */
18502 namespace_decl = cp_parser_lookup_name (parser, identifier,
18503 none_type,
18504 /*is_template=*/false,
18505 /*is_namespace=*/true,
18506 /*check_dependency=*/true,
18507 /*ambiguous_decls=*/NULL,
18508 token->location);
18509 /* If it's not a namespace, issue an error. */
18510 if (namespace_decl == error_mark_node
18511 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18513 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18514 error_at (token->location, "%qD is not a namespace-name", identifier);
18515 cp_parser_error (parser, "expected namespace-name");
18516 namespace_decl = error_mark_node;
18519 return namespace_decl;
18522 /* Parse a namespace-definition.
18524 namespace-definition:
18525 named-namespace-definition
18526 unnamed-namespace-definition
18528 named-namespace-definition:
18529 original-namespace-definition
18530 extension-namespace-definition
18532 original-namespace-definition:
18533 namespace identifier { namespace-body }
18535 extension-namespace-definition:
18536 namespace original-namespace-name { namespace-body }
18538 unnamed-namespace-definition:
18539 namespace { namespace-body } */
18541 static void
18542 cp_parser_namespace_definition (cp_parser* parser)
18544 tree identifier;
18545 int nested_definition_count = 0;
18547 cp_ensure_no_omp_declare_simd (parser);
18548 cp_ensure_no_oacc_routine (parser);
18550 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18552 if (is_inline)
18554 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18555 cp_lexer_consume_token (parser->lexer);
18558 /* Look for the `namespace' keyword. */
18559 cp_token* token
18560 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18562 /* Parse any specified attributes before the identifier. */
18563 tree attribs = cp_parser_attributes_opt (parser);
18565 for (;;)
18567 identifier = NULL_TREE;
18569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18571 identifier = cp_parser_identifier (parser);
18573 /* Parse any attributes specified after the identifier. */
18574 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18577 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18578 break;
18580 if (!nested_definition_count && cxx_dialect < cxx17)
18581 pedwarn (input_location, OPT_Wpedantic,
18582 "nested namespace definitions only available with "
18583 "-std=c++17 or -std=gnu++17");
18585 /* Nested namespace names can create new namespaces (unlike
18586 other qualified-ids). */
18587 if (int count = identifier ? push_namespace (identifier) : 0)
18588 nested_definition_count += count;
18589 else
18590 cp_parser_error (parser, "nested namespace name required");
18591 cp_lexer_consume_token (parser->lexer);
18594 if (nested_definition_count && !identifier)
18595 cp_parser_error (parser, "namespace name required");
18597 if (nested_definition_count && attribs)
18598 error_at (token->location,
18599 "a nested namespace definition cannot have attributes");
18600 if (nested_definition_count && is_inline)
18601 error_at (token->location,
18602 "a nested namespace definition cannot be inline");
18604 /* Start the namespace. */
18605 nested_definition_count += push_namespace (identifier, is_inline);
18607 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18609 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18611 /* Look for the `{' to validate starting the namespace. */
18612 matching_braces braces;
18613 if (braces.require_open (parser))
18615 /* Parse the body of the namespace. */
18616 cp_parser_namespace_body (parser);
18618 /* Look for the final `}'. */
18619 braces.require_close (parser);
18622 if (has_visibility)
18623 pop_visibility (1);
18625 /* Pop the nested namespace definitions. */
18626 while (nested_definition_count--)
18627 pop_namespace ();
18630 /* Parse a namespace-body.
18632 namespace-body:
18633 declaration-seq [opt] */
18635 static void
18636 cp_parser_namespace_body (cp_parser* parser)
18638 cp_parser_declaration_seq_opt (parser);
18641 /* Parse a namespace-alias-definition.
18643 namespace-alias-definition:
18644 namespace identifier = qualified-namespace-specifier ; */
18646 static void
18647 cp_parser_namespace_alias_definition (cp_parser* parser)
18649 tree identifier;
18650 tree namespace_specifier;
18652 cp_token *token = cp_lexer_peek_token (parser->lexer);
18654 /* Look for the `namespace' keyword. */
18655 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18656 /* Look for the identifier. */
18657 identifier = cp_parser_identifier (parser);
18658 if (identifier == error_mark_node)
18659 return;
18660 /* Look for the `=' token. */
18661 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18664 error_at (token->location, "%<namespace%> definition is not allowed here");
18665 /* Skip the definition. */
18666 cp_lexer_consume_token (parser->lexer);
18667 if (cp_parser_skip_to_closing_brace (parser))
18668 cp_lexer_consume_token (parser->lexer);
18669 return;
18671 cp_parser_require (parser, CPP_EQ, RT_EQ);
18672 /* Look for the qualified-namespace-specifier. */
18673 namespace_specifier
18674 = cp_parser_qualified_namespace_specifier (parser);
18675 /* Look for the `;' token. */
18676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18678 /* Register the alias in the symbol table. */
18679 do_namespace_alias (identifier, namespace_specifier);
18682 /* Parse a qualified-namespace-specifier.
18684 qualified-namespace-specifier:
18685 :: [opt] nested-name-specifier [opt] namespace-name
18687 Returns a NAMESPACE_DECL corresponding to the specified
18688 namespace. */
18690 static tree
18691 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18693 /* Look for the optional `::'. */
18694 cp_parser_global_scope_opt (parser,
18695 /*current_scope_valid_p=*/false);
18697 /* Look for the optional nested-name-specifier. */
18698 cp_parser_nested_name_specifier_opt (parser,
18699 /*typename_keyword_p=*/false,
18700 /*check_dependency_p=*/true,
18701 /*type_p=*/false,
18702 /*is_declaration=*/true);
18704 return cp_parser_namespace_name (parser);
18707 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18708 access declaration.
18710 using-declaration:
18711 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18712 using :: unqualified-id ;
18714 access-declaration:
18715 qualified-id ;
18719 static bool
18720 cp_parser_using_declaration (cp_parser* parser,
18721 bool access_declaration_p)
18723 cp_token *token;
18724 bool typename_p = false;
18725 bool global_scope_p;
18726 tree decl;
18727 tree identifier;
18728 tree qscope;
18729 int oldcount = errorcount;
18730 cp_token *diag_token = NULL;
18732 if (access_declaration_p)
18734 diag_token = cp_lexer_peek_token (parser->lexer);
18735 cp_parser_parse_tentatively (parser);
18737 else
18739 /* Look for the `using' keyword. */
18740 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18742 again:
18743 /* Peek at the next token. */
18744 token = cp_lexer_peek_token (parser->lexer);
18745 /* See if it's `typename'. */
18746 if (token->keyword == RID_TYPENAME)
18748 /* Remember that we've seen it. */
18749 typename_p = true;
18750 /* Consume the `typename' token. */
18751 cp_lexer_consume_token (parser->lexer);
18755 /* Look for the optional global scope qualification. */
18756 global_scope_p
18757 = (cp_parser_global_scope_opt (parser,
18758 /*current_scope_valid_p=*/false)
18759 != NULL_TREE);
18761 /* If we saw `typename', or didn't see `::', then there must be a
18762 nested-name-specifier present. */
18763 if (typename_p || !global_scope_p)
18765 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18766 /*check_dependency_p=*/true,
18767 /*type_p=*/false,
18768 /*is_declaration=*/true);
18769 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18771 cp_parser_skip_to_end_of_block_or_statement (parser);
18772 return false;
18775 /* Otherwise, we could be in either of the two productions. In that
18776 case, treat the nested-name-specifier as optional. */
18777 else
18778 qscope = cp_parser_nested_name_specifier_opt (parser,
18779 /*typename_keyword_p=*/false,
18780 /*check_dependency_p=*/true,
18781 /*type_p=*/false,
18782 /*is_declaration=*/true);
18783 if (!qscope)
18784 qscope = global_namespace;
18785 else if (UNSCOPED_ENUM_P (qscope))
18786 qscope = CP_TYPE_CONTEXT (qscope);
18788 if (access_declaration_p && cp_parser_error_occurred (parser))
18789 /* Something has already gone wrong; there's no need to parse
18790 further. Since an error has occurred, the return value of
18791 cp_parser_parse_definitely will be false, as required. */
18792 return cp_parser_parse_definitely (parser);
18794 token = cp_lexer_peek_token (parser->lexer);
18795 /* Parse the unqualified-id. */
18796 identifier = cp_parser_unqualified_id (parser,
18797 /*template_keyword_p=*/false,
18798 /*check_dependency_p=*/true,
18799 /*declarator_p=*/true,
18800 /*optional_p=*/false);
18802 if (access_declaration_p)
18804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18805 cp_parser_simulate_error (parser);
18806 if (!cp_parser_parse_definitely (parser))
18807 return false;
18809 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18811 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18812 if (cxx_dialect < cxx17
18813 && !in_system_header_at (ell->location))
18814 pedwarn (ell->location, 0,
18815 "pack expansion in using-declaration only available "
18816 "with -std=c++17 or -std=gnu++17");
18817 qscope = make_pack_expansion (qscope);
18820 /* The function we call to handle a using-declaration is different
18821 depending on what scope we are in. */
18822 if (qscope == error_mark_node || identifier == error_mark_node)
18824 else if (!identifier_p (identifier)
18825 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18826 /* [namespace.udecl]
18828 A using declaration shall not name a template-id. */
18829 error_at (token->location,
18830 "a template-id may not appear in a using-declaration");
18831 else
18833 if (at_class_scope_p ())
18835 /* Create the USING_DECL. */
18836 decl = do_class_using_decl (qscope, identifier);
18838 if (decl && typename_p)
18839 USING_DECL_TYPENAME_P (decl) = 1;
18841 if (check_for_bare_parameter_packs (decl))
18843 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18844 return false;
18846 else
18847 /* Add it to the list of members in this class. */
18848 finish_member_declaration (decl);
18850 else
18852 decl = cp_parser_lookup_name_simple (parser,
18853 identifier,
18854 token->location);
18855 if (decl == error_mark_node)
18856 cp_parser_name_lookup_error (parser, identifier,
18857 decl, NLE_NULL,
18858 token->location);
18859 else if (check_for_bare_parameter_packs (decl))
18861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18862 return false;
18864 else if (!at_namespace_scope_p ())
18865 finish_local_using_decl (decl, qscope, identifier);
18866 else
18867 finish_namespace_using_decl (decl, qscope, identifier);
18871 if (!access_declaration_p
18872 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18874 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18875 if (cxx_dialect < cxx17)
18876 pedwarn (comma->location, 0,
18877 "comma-separated list in using-declaration only available "
18878 "with -std=c++17 or -std=gnu++17");
18879 goto again;
18882 /* Look for the final `;'. */
18883 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18885 if (access_declaration_p && errorcount == oldcount)
18886 warning_at (diag_token->location, OPT_Wdeprecated,
18887 "access declarations are deprecated "
18888 "in favour of using-declarations; "
18889 "suggestion: add the %<using%> keyword");
18891 return true;
18894 /* Parse an alias-declaration.
18896 alias-declaration:
18897 using identifier attribute-specifier-seq [opt] = type-id */
18899 static tree
18900 cp_parser_alias_declaration (cp_parser* parser)
18902 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18903 location_t id_location;
18904 cp_declarator *declarator;
18905 cp_decl_specifier_seq decl_specs;
18906 bool member_p;
18907 const char *saved_message = NULL;
18909 /* Look for the `using' keyword. */
18910 cp_token *using_token
18911 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18912 if (using_token == NULL)
18913 return error_mark_node;
18915 id_location = cp_lexer_peek_token (parser->lexer)->location;
18916 id = cp_parser_identifier (parser);
18917 if (id == error_mark_node)
18918 return error_mark_node;
18920 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18921 attributes = cp_parser_attributes_opt (parser);
18922 if (attributes == error_mark_node)
18923 return error_mark_node;
18925 cp_parser_require (parser, CPP_EQ, RT_EQ);
18927 if (cp_parser_error_occurred (parser))
18928 return error_mark_node;
18930 cp_parser_commit_to_tentative_parse (parser);
18932 /* Now we are going to parse the type-id of the declaration. */
18935 [dcl.type]/3 says:
18937 "A type-specifier-seq shall not define a class or enumeration
18938 unless it appears in the type-id of an alias-declaration (7.1.3) that
18939 is not the declaration of a template-declaration."
18941 In other words, if we currently are in an alias template, the
18942 type-id should not define a type.
18944 So let's set parser->type_definition_forbidden_message in that
18945 case; cp_parser_check_type_definition (called by
18946 cp_parser_class_specifier) will then emit an error if a type is
18947 defined in the type-id. */
18948 if (parser->num_template_parameter_lists)
18950 saved_message = parser->type_definition_forbidden_message;
18951 parser->type_definition_forbidden_message =
18952 G_("types may not be defined in alias template declarations");
18955 type = cp_parser_type_id (parser);
18957 /* Restore the error message if need be. */
18958 if (parser->num_template_parameter_lists)
18959 parser->type_definition_forbidden_message = saved_message;
18961 if (type == error_mark_node
18962 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18964 cp_parser_skip_to_end_of_block_or_statement (parser);
18965 return error_mark_node;
18968 /* A typedef-name can also be introduced by an alias-declaration. The
18969 identifier following the using keyword becomes a typedef-name. It has
18970 the same semantics as if it were introduced by the typedef
18971 specifier. In particular, it does not define a new type and it shall
18972 not appear in the type-id. */
18974 clear_decl_specs (&decl_specs);
18975 decl_specs.type = type;
18976 if (attributes != NULL_TREE)
18978 decl_specs.attributes = attributes;
18979 set_and_check_decl_spec_loc (&decl_specs,
18980 ds_attribute,
18981 attrs_token);
18983 set_and_check_decl_spec_loc (&decl_specs,
18984 ds_typedef,
18985 using_token);
18986 set_and_check_decl_spec_loc (&decl_specs,
18987 ds_alias,
18988 using_token);
18990 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18991 declarator->id_loc = id_location;
18993 member_p = at_class_scope_p ();
18994 if (member_p)
18995 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18996 NULL_TREE, attributes);
18997 else
18998 decl = start_decl (declarator, &decl_specs, 0,
18999 attributes, NULL_TREE, &pushed_scope);
19000 if (decl == error_mark_node)
19001 return decl;
19003 // Attach constraints to the alias declaration.
19004 if (flag_concepts && current_template_parms)
19006 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
19007 tree constr = build_constraints (reqs, NULL_TREE);
19008 set_constraints (decl, constr);
19011 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
19013 if (pushed_scope)
19014 pop_scope (pushed_scope);
19016 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19017 added into the symbol table; otherwise, return the TYPE_DECL. */
19018 if (DECL_LANG_SPECIFIC (decl)
19019 && DECL_TEMPLATE_INFO (decl)
19020 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
19022 decl = DECL_TI_TEMPLATE (decl);
19023 if (member_p)
19024 check_member_template (decl);
19027 return decl;
19030 /* Parse a using-directive.
19032 using-directive:
19033 using namespace :: [opt] nested-name-specifier [opt]
19034 namespace-name ; */
19036 static void
19037 cp_parser_using_directive (cp_parser* parser)
19039 tree namespace_decl;
19040 tree attribs;
19042 /* Look for the `using' keyword. */
19043 cp_parser_require_keyword (parser, RID_USING, RT_USING);
19044 /* And the `namespace' keyword. */
19045 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
19046 /* Look for the optional `::' operator. */
19047 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19048 /* And the optional nested-name-specifier. */
19049 cp_parser_nested_name_specifier_opt (parser,
19050 /*typename_keyword_p=*/false,
19051 /*check_dependency_p=*/true,
19052 /*type_p=*/false,
19053 /*is_declaration=*/true);
19054 /* Get the namespace being used. */
19055 namespace_decl = cp_parser_namespace_name (parser);
19056 /* And any specified attributes. */
19057 attribs = cp_parser_attributes_opt (parser);
19059 /* Update the symbol table. */
19060 if (namespace_bindings_p ())
19061 finish_namespace_using_directive (namespace_decl, attribs);
19062 else
19063 finish_local_using_directive (namespace_decl, attribs);
19065 /* Look for the final `;'. */
19066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19069 /* Parse an asm-definition.
19071 asm-definition:
19072 asm ( string-literal ) ;
19074 GNU Extension:
19076 asm-definition:
19077 asm volatile [opt] ( string-literal ) ;
19078 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
19079 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19080 : asm-operand-list [opt] ) ;
19081 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19082 : asm-operand-list [opt]
19083 : asm-clobber-list [opt] ) ;
19084 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19085 : asm-clobber-list [opt]
19086 : asm-goto-list ) ; */
19088 static void
19089 cp_parser_asm_definition (cp_parser* parser)
19091 tree string;
19092 tree outputs = NULL_TREE;
19093 tree inputs = NULL_TREE;
19094 tree clobbers = NULL_TREE;
19095 tree labels = NULL_TREE;
19096 tree asm_stmt;
19097 bool volatile_p = false;
19098 bool extended_p = false;
19099 bool invalid_inputs_p = false;
19100 bool invalid_outputs_p = false;
19101 bool goto_p = false;
19102 required_token missing = RT_NONE;
19104 /* Look for the `asm' keyword. */
19105 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19107 if (parser->in_function_body
19108 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19110 error ("%<asm%> in %<constexpr%> function");
19111 cp_function_chain->invalid_constexpr = true;
19114 /* See if the next token is `volatile'. */
19115 if (cp_parser_allow_gnu_extensions_p (parser)
19116 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19118 /* Remember that we saw the `volatile' keyword. */
19119 volatile_p = true;
19120 /* Consume the token. */
19121 cp_lexer_consume_token (parser->lexer);
19123 if (cp_parser_allow_gnu_extensions_p (parser)
19124 && parser->in_function_body
19125 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19127 /* Remember that we saw the `goto' keyword. */
19128 goto_p = true;
19129 /* Consume the token. */
19130 cp_lexer_consume_token (parser->lexer);
19132 /* Look for the opening `('. */
19133 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19134 return;
19135 /* Look for the string. */
19136 string = cp_parser_string_literal (parser, false, false);
19137 if (string == error_mark_node)
19139 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19140 /*consume_paren=*/true);
19141 return;
19144 /* If we're allowing GNU extensions, check for the extended assembly
19145 syntax. Unfortunately, the `:' tokens need not be separated by
19146 a space in C, and so, for compatibility, we tolerate that here
19147 too. Doing that means that we have to treat the `::' operator as
19148 two `:' tokens. */
19149 if (cp_parser_allow_gnu_extensions_p (parser)
19150 && parser->in_function_body
19151 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19152 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19154 bool inputs_p = false;
19155 bool clobbers_p = false;
19156 bool labels_p = false;
19158 /* The extended syntax was used. */
19159 extended_p = true;
19161 /* Look for outputs. */
19162 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19164 /* Consume the `:'. */
19165 cp_lexer_consume_token (parser->lexer);
19166 /* Parse the output-operands. */
19167 if (cp_lexer_next_token_is_not (parser->lexer,
19168 CPP_COLON)
19169 && cp_lexer_next_token_is_not (parser->lexer,
19170 CPP_SCOPE)
19171 && cp_lexer_next_token_is_not (parser->lexer,
19172 CPP_CLOSE_PAREN)
19173 && !goto_p)
19175 outputs = cp_parser_asm_operand_list (parser);
19176 if (outputs == error_mark_node)
19177 invalid_outputs_p = true;
19180 /* If the next token is `::', there are no outputs, and the
19181 next token is the beginning of the inputs. */
19182 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19183 /* The inputs are coming next. */
19184 inputs_p = true;
19186 /* Look for inputs. */
19187 if (inputs_p
19188 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19190 /* Consume the `:' or `::'. */
19191 cp_lexer_consume_token (parser->lexer);
19192 /* Parse the output-operands. */
19193 if (cp_lexer_next_token_is_not (parser->lexer,
19194 CPP_COLON)
19195 && cp_lexer_next_token_is_not (parser->lexer,
19196 CPP_SCOPE)
19197 && cp_lexer_next_token_is_not (parser->lexer,
19198 CPP_CLOSE_PAREN))
19200 inputs = cp_parser_asm_operand_list (parser);
19201 if (inputs == error_mark_node)
19202 invalid_inputs_p = true;
19205 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19206 /* The clobbers are coming next. */
19207 clobbers_p = true;
19209 /* Look for clobbers. */
19210 if (clobbers_p
19211 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19213 clobbers_p = true;
19214 /* Consume the `:' or `::'. */
19215 cp_lexer_consume_token (parser->lexer);
19216 /* Parse the clobbers. */
19217 if (cp_lexer_next_token_is_not (parser->lexer,
19218 CPP_COLON)
19219 && cp_lexer_next_token_is_not (parser->lexer,
19220 CPP_CLOSE_PAREN))
19221 clobbers = cp_parser_asm_clobber_list (parser);
19223 else if (goto_p
19224 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19225 /* The labels are coming next. */
19226 labels_p = true;
19228 /* Look for labels. */
19229 if (labels_p
19230 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19232 labels_p = true;
19233 /* Consume the `:' or `::'. */
19234 cp_lexer_consume_token (parser->lexer);
19235 /* Parse the labels. */
19236 labels = cp_parser_asm_label_list (parser);
19239 if (goto_p && !labels_p)
19240 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19242 else if (goto_p)
19243 missing = RT_COLON_SCOPE;
19245 /* Look for the closing `)'. */
19246 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19247 missing ? missing : RT_CLOSE_PAREN))
19248 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19249 /*consume_paren=*/true);
19250 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19252 if (!invalid_inputs_p && !invalid_outputs_p)
19254 /* Create the ASM_EXPR. */
19255 if (parser->in_function_body)
19257 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19258 inputs, clobbers, labels);
19259 /* If the extended syntax was not used, mark the ASM_EXPR. */
19260 if (!extended_p)
19262 tree temp = asm_stmt;
19263 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19264 temp = TREE_OPERAND (temp, 0);
19266 ASM_INPUT_P (temp) = 1;
19269 else
19270 symtab->finalize_toplevel_asm (string);
19274 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19275 type that comes from the decl-specifier-seq. */
19277 static tree
19278 strip_declarator_types (tree type, cp_declarator *declarator)
19280 for (cp_declarator *d = declarator; d;)
19281 switch (d->kind)
19283 case cdk_id:
19284 case cdk_decomp:
19285 case cdk_error:
19286 d = NULL;
19287 break;
19289 default:
19290 if (TYPE_PTRMEMFUNC_P (type))
19291 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19292 type = TREE_TYPE (type);
19293 d = d->declarator;
19294 break;
19297 return type;
19300 /* Declarators [gram.dcl.decl] */
19302 /* Parse an init-declarator.
19304 init-declarator:
19305 declarator initializer [opt]
19307 GNU Extension:
19309 init-declarator:
19310 declarator asm-specification [opt] attributes [opt] initializer [opt]
19312 function-definition:
19313 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19314 function-body
19315 decl-specifier-seq [opt] declarator function-try-block
19317 GNU Extension:
19319 function-definition:
19320 __extension__ function-definition
19322 TM Extension:
19324 function-definition:
19325 decl-specifier-seq [opt] declarator function-transaction-block
19327 The DECL_SPECIFIERS apply to this declarator. Returns a
19328 representation of the entity declared. If MEMBER_P is TRUE, then
19329 this declarator appears in a class scope. The new DECL created by
19330 this declarator is returned.
19332 The CHECKS are access checks that should be performed once we know
19333 what entity is being declared (and, therefore, what classes have
19334 befriended it).
19336 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19337 for a function-definition here as well. If the declarator is a
19338 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19339 be TRUE upon return. By that point, the function-definition will
19340 have been completely parsed.
19342 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19343 is FALSE.
19345 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19346 parsed declaration if it is an uninitialized single declarator not followed
19347 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19348 if present, will not be consumed. If returned, this declarator will be
19349 created with SD_INITIALIZED but will not call cp_finish_decl.
19351 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19352 and there is an initializer, the pointed location_t is set to the
19353 location of the '=' or `(', or '{' in C++11 token introducing the
19354 initializer. */
19356 static tree
19357 cp_parser_init_declarator (cp_parser* parser,
19358 cp_decl_specifier_seq *decl_specifiers,
19359 vec<deferred_access_check, va_gc> *checks,
19360 bool function_definition_allowed_p,
19361 bool member_p,
19362 int declares_class_or_enum,
19363 bool* function_definition_p,
19364 tree* maybe_range_for_decl,
19365 location_t* init_loc,
19366 tree* auto_result)
19368 cp_token *token = NULL, *asm_spec_start_token = NULL,
19369 *attributes_start_token = NULL;
19370 cp_declarator *declarator;
19371 tree prefix_attributes;
19372 tree attributes = NULL;
19373 tree asm_specification;
19374 tree initializer;
19375 tree decl = NULL_TREE;
19376 tree scope;
19377 int is_initialized;
19378 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19379 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19380 "(...)". */
19381 enum cpp_ttype initialization_kind;
19382 bool is_direct_init = false;
19383 bool is_non_constant_init;
19384 int ctor_dtor_or_conv_p;
19385 bool friend_p = cp_parser_friend_p (decl_specifiers);
19386 tree pushed_scope = NULL_TREE;
19387 bool range_for_decl_p = false;
19388 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19389 location_t tmp_init_loc = UNKNOWN_LOCATION;
19391 /* Gather the attributes that were provided with the
19392 decl-specifiers. */
19393 prefix_attributes = decl_specifiers->attributes;
19395 /* Assume that this is not the declarator for a function
19396 definition. */
19397 if (function_definition_p)
19398 *function_definition_p = false;
19400 /* Default arguments are only permitted for function parameters. */
19401 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19402 parser->default_arg_ok_p = false;
19404 /* Defer access checks while parsing the declarator; we cannot know
19405 what names are accessible until we know what is being
19406 declared. */
19407 resume_deferring_access_checks ();
19409 token = cp_lexer_peek_token (parser->lexer);
19411 /* Parse the declarator. */
19412 declarator
19413 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19414 &ctor_dtor_or_conv_p,
19415 /*parenthesized_p=*/NULL,
19416 member_p, friend_p);
19417 /* Gather up the deferred checks. */
19418 stop_deferring_access_checks ();
19420 parser->default_arg_ok_p = saved_default_arg_ok_p;
19422 /* If the DECLARATOR was erroneous, there's no need to go
19423 further. */
19424 if (declarator == cp_error_declarator)
19425 return error_mark_node;
19427 /* Check that the number of template-parameter-lists is OK. */
19428 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19429 token->location))
19430 return error_mark_node;
19432 if (declares_class_or_enum & 2)
19433 cp_parser_check_for_definition_in_return_type (declarator,
19434 decl_specifiers->type,
19435 decl_specifiers->locations[ds_type_spec]);
19437 /* Figure out what scope the entity declared by the DECLARATOR is
19438 located in. `grokdeclarator' sometimes changes the scope, so
19439 we compute it now. */
19440 scope = get_scope_of_declarator (declarator);
19442 /* Perform any lookups in the declared type which were thought to be
19443 dependent, but are not in the scope of the declarator. */
19444 decl_specifiers->type
19445 = maybe_update_decl_type (decl_specifiers->type, scope);
19447 /* If we're allowing GNU extensions, look for an
19448 asm-specification. */
19449 if (cp_parser_allow_gnu_extensions_p (parser))
19451 /* Look for an asm-specification. */
19452 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19453 asm_specification = cp_parser_asm_specification_opt (parser);
19455 else
19456 asm_specification = NULL_TREE;
19458 /* Look for attributes. */
19459 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19460 attributes = cp_parser_attributes_opt (parser);
19462 /* Peek at the next token. */
19463 token = cp_lexer_peek_token (parser->lexer);
19465 bool bogus_implicit_tmpl = false;
19467 if (function_declarator_p (declarator))
19469 /* Handle C++17 deduction guides. */
19470 if (!decl_specifiers->type
19471 && ctor_dtor_or_conv_p <= 0
19472 && cxx_dialect >= cxx17)
19474 cp_declarator *id = get_id_declarator (declarator);
19475 tree name = id->u.id.unqualified_name;
19476 parser->scope = id->u.id.qualifying_scope;
19477 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19478 if (tmpl
19479 && (DECL_CLASS_TEMPLATE_P (tmpl)
19480 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19482 id->u.id.unqualified_name = dguide_name (tmpl);
19483 id->u.id.sfk = sfk_deduction_guide;
19484 ctor_dtor_or_conv_p = 1;
19488 /* Check to see if the token indicates the start of a
19489 function-definition. */
19490 if (cp_parser_token_starts_function_definition_p (token))
19492 if (!function_definition_allowed_p)
19494 /* If a function-definition should not appear here, issue an
19495 error message. */
19496 cp_parser_error (parser,
19497 "a function-definition is not allowed here");
19498 return error_mark_node;
19501 location_t func_brace_location
19502 = cp_lexer_peek_token (parser->lexer)->location;
19504 /* Neither attributes nor an asm-specification are allowed
19505 on a function-definition. */
19506 if (asm_specification)
19507 error_at (asm_spec_start_token->location,
19508 "an asm-specification is not allowed "
19509 "on a function-definition");
19510 if (attributes)
19511 error_at (attributes_start_token->location,
19512 "attributes are not allowed "
19513 "on a function-definition");
19514 /* This is a function-definition. */
19515 *function_definition_p = true;
19517 /* Parse the function definition. */
19518 if (member_p)
19519 decl = cp_parser_save_member_function_body (parser,
19520 decl_specifiers,
19521 declarator,
19522 prefix_attributes);
19523 else
19524 decl =
19525 (cp_parser_function_definition_from_specifiers_and_declarator
19526 (parser, decl_specifiers, prefix_attributes, declarator));
19528 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19530 /* This is where the prologue starts... */
19531 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19532 = func_brace_location;
19535 return decl;
19538 else if (parser->fully_implicit_function_template_p)
19540 /* A non-template declaration involving a function parameter list
19541 containing an implicit template parameter will be made into a
19542 template. If the resulting declaration is not going to be an
19543 actual function then finish the template scope here to prevent it.
19544 An error message will be issued once we have a decl to talk about.
19546 FIXME probably we should do type deduction rather than create an
19547 implicit template, but the standard currently doesn't allow it. */
19548 bogus_implicit_tmpl = true;
19549 finish_fully_implicit_template (parser, NULL_TREE);
19552 /* [dcl.dcl]
19554 Only in function declarations for constructors, destructors, type
19555 conversions, and deduction guides can the decl-specifier-seq be omitted.
19557 We explicitly postpone this check past the point where we handle
19558 function-definitions because we tolerate function-definitions
19559 that are missing their return types in some modes. */
19560 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19562 cp_parser_error (parser,
19563 "expected constructor, destructor, or type conversion");
19564 return error_mark_node;
19567 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19568 if (token->type == CPP_EQ
19569 || token->type == CPP_OPEN_PAREN
19570 || token->type == CPP_OPEN_BRACE)
19572 is_initialized = SD_INITIALIZED;
19573 initialization_kind = token->type;
19574 if (maybe_range_for_decl)
19575 *maybe_range_for_decl = error_mark_node;
19576 tmp_init_loc = token->location;
19577 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19578 *init_loc = tmp_init_loc;
19580 if (token->type == CPP_EQ
19581 && function_declarator_p (declarator))
19583 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19584 if (t2->keyword == RID_DEFAULT)
19585 is_initialized = SD_DEFAULTED;
19586 else if (t2->keyword == RID_DELETE)
19587 is_initialized = SD_DELETED;
19590 else
19592 /* If the init-declarator isn't initialized and isn't followed by a
19593 `,' or `;', it's not a valid init-declarator. */
19594 if (token->type != CPP_COMMA
19595 && token->type != CPP_SEMICOLON)
19597 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19598 range_for_decl_p = true;
19599 else
19601 if (!maybe_range_for_decl)
19602 cp_parser_error (parser, "expected initializer");
19603 return error_mark_node;
19606 is_initialized = SD_UNINITIALIZED;
19607 initialization_kind = CPP_EOF;
19610 /* Because start_decl has side-effects, we should only call it if we
19611 know we're going ahead. By this point, we know that we cannot
19612 possibly be looking at any other construct. */
19613 cp_parser_commit_to_tentative_parse (parser);
19615 /* Enter the newly declared entry in the symbol table. If we're
19616 processing a declaration in a class-specifier, we wait until
19617 after processing the initializer. */
19618 if (!member_p)
19620 if (parser->in_unbraced_linkage_specification_p)
19621 decl_specifiers->storage_class = sc_extern;
19622 decl = start_decl (declarator, decl_specifiers,
19623 range_for_decl_p? SD_INITIALIZED : is_initialized,
19624 attributes, prefix_attributes, &pushed_scope);
19625 cp_finalize_omp_declare_simd (parser, decl);
19626 cp_finalize_oacc_routine (parser, decl, false);
19627 /* Adjust location of decl if declarator->id_loc is more appropriate:
19628 set, and decl wasn't merged with another decl, in which case its
19629 location would be different from input_location, and more accurate. */
19630 if (DECL_P (decl)
19631 && declarator->id_loc != UNKNOWN_LOCATION
19632 && DECL_SOURCE_LOCATION (decl) == input_location)
19633 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19635 else if (scope)
19636 /* Enter the SCOPE. That way unqualified names appearing in the
19637 initializer will be looked up in SCOPE. */
19638 pushed_scope = push_scope (scope);
19640 /* Perform deferred access control checks, now that we know in which
19641 SCOPE the declared entity resides. */
19642 if (!member_p && decl)
19644 tree saved_current_function_decl = NULL_TREE;
19646 /* If the entity being declared is a function, pretend that we
19647 are in its scope. If it is a `friend', it may have access to
19648 things that would not otherwise be accessible. */
19649 if (TREE_CODE (decl) == FUNCTION_DECL)
19651 saved_current_function_decl = current_function_decl;
19652 current_function_decl = decl;
19655 /* Perform access checks for template parameters. */
19656 cp_parser_perform_template_parameter_access_checks (checks);
19658 /* Perform the access control checks for the declarator and the
19659 decl-specifiers. */
19660 perform_deferred_access_checks (tf_warning_or_error);
19662 /* Restore the saved value. */
19663 if (TREE_CODE (decl) == FUNCTION_DECL)
19664 current_function_decl = saved_current_function_decl;
19667 /* Parse the initializer. */
19668 initializer = NULL_TREE;
19669 is_direct_init = false;
19670 is_non_constant_init = true;
19671 if (is_initialized)
19673 if (function_declarator_p (declarator))
19675 if (initialization_kind == CPP_EQ)
19676 initializer = cp_parser_pure_specifier (parser);
19677 else
19679 /* If the declaration was erroneous, we don't really
19680 know what the user intended, so just silently
19681 consume the initializer. */
19682 if (decl != error_mark_node)
19683 error_at (tmp_init_loc, "initializer provided for function");
19684 cp_parser_skip_to_closing_parenthesis (parser,
19685 /*recovering=*/true,
19686 /*or_comma=*/false,
19687 /*consume_paren=*/true);
19690 else
19692 /* We want to record the extra mangling scope for in-class
19693 initializers of class members and initializers of static data
19694 member templates. The former involves deferring
19695 parsing of the initializer until end of class as with default
19696 arguments. So right here we only handle the latter. */
19697 if (!member_p && processing_template_decl)
19698 start_lambda_scope (decl);
19699 initializer = cp_parser_initializer (parser,
19700 &is_direct_init,
19701 &is_non_constant_init);
19702 if (!member_p && processing_template_decl)
19703 finish_lambda_scope ();
19704 if (initializer == error_mark_node)
19705 cp_parser_skip_to_end_of_statement (parser);
19709 /* The old parser allows attributes to appear after a parenthesized
19710 initializer. Mark Mitchell proposed removing this functionality
19711 on the GCC mailing lists on 2002-08-13. This parser accepts the
19712 attributes -- but ignores them. */
19713 if (cp_parser_allow_gnu_extensions_p (parser)
19714 && initialization_kind == CPP_OPEN_PAREN)
19715 if (cp_parser_attributes_opt (parser))
19716 warning (OPT_Wattributes,
19717 "attributes after parenthesized initializer ignored");
19719 /* And now complain about a non-function implicit template. */
19720 if (bogus_implicit_tmpl && decl != error_mark_node)
19721 error_at (DECL_SOURCE_LOCATION (decl),
19722 "non-function %qD declared as implicit template", decl);
19724 /* For an in-class declaration, use `grokfield' to create the
19725 declaration. */
19726 if (member_p)
19728 if (pushed_scope)
19730 pop_scope (pushed_scope);
19731 pushed_scope = NULL_TREE;
19733 decl = grokfield (declarator, decl_specifiers,
19734 initializer, !is_non_constant_init,
19735 /*asmspec=*/NULL_TREE,
19736 chainon (attributes, prefix_attributes));
19737 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19738 cp_parser_save_default_args (parser, decl);
19739 cp_finalize_omp_declare_simd (parser, decl);
19740 cp_finalize_oacc_routine (parser, decl, false);
19743 /* Finish processing the declaration. But, skip member
19744 declarations. */
19745 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19747 cp_finish_decl (decl,
19748 initializer, !is_non_constant_init,
19749 asm_specification,
19750 /* If the initializer is in parentheses, then this is
19751 a direct-initialization, which means that an
19752 `explicit' constructor is OK. Otherwise, an
19753 `explicit' constructor cannot be used. */
19754 ((is_direct_init || !is_initialized)
19755 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19757 else if ((cxx_dialect != cxx98) && friend_p
19758 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19759 /* Core issue #226 (C++0x only): A default template-argument
19760 shall not be specified in a friend class template
19761 declaration. */
19762 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19763 /*is_partial=*/false, /*is_friend_decl=*/1);
19765 if (!friend_p && pushed_scope)
19766 pop_scope (pushed_scope);
19768 if (function_declarator_p (declarator)
19769 && parser->fully_implicit_function_template_p)
19771 if (member_p)
19772 decl = finish_fully_implicit_template (parser, decl);
19773 else
19774 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19777 if (auto_result && is_initialized && decl_specifiers->type
19778 && type_uses_auto (decl_specifiers->type))
19779 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19781 return decl;
19784 /* Parse a declarator.
19786 declarator:
19787 direct-declarator
19788 ptr-operator declarator
19790 abstract-declarator:
19791 ptr-operator abstract-declarator [opt]
19792 direct-abstract-declarator
19794 GNU Extensions:
19796 declarator:
19797 attributes [opt] direct-declarator
19798 attributes [opt] ptr-operator declarator
19800 abstract-declarator:
19801 attributes [opt] ptr-operator abstract-declarator [opt]
19802 attributes [opt] direct-abstract-declarator
19804 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19805 detect constructors, destructors, deduction guides, or conversion operators.
19806 It is set to -1 if the declarator is a name, and +1 if it is a
19807 function. Otherwise it is set to zero. Usually you just want to
19808 test for >0, but internally the negative value is used.
19810 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19811 a decl-specifier-seq unless it declares a constructor, destructor,
19812 or conversion. It might seem that we could check this condition in
19813 semantic analysis, rather than parsing, but that makes it difficult
19814 to handle something like `f()'. We want to notice that there are
19815 no decl-specifiers, and therefore realize that this is an
19816 expression, not a declaration.)
19818 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19819 the declarator is a direct-declarator of the form "(...)".
19821 MEMBER_P is true iff this declarator is a member-declarator.
19823 FRIEND_P is true iff this declarator is a friend. */
19825 static cp_declarator *
19826 cp_parser_declarator (cp_parser* parser,
19827 cp_parser_declarator_kind dcl_kind,
19828 int* ctor_dtor_or_conv_p,
19829 bool* parenthesized_p,
19830 bool member_p, bool friend_p)
19832 cp_declarator *declarator;
19833 enum tree_code code;
19834 cp_cv_quals cv_quals;
19835 tree class_type;
19836 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19838 /* Assume this is not a constructor, destructor, or type-conversion
19839 operator. */
19840 if (ctor_dtor_or_conv_p)
19841 *ctor_dtor_or_conv_p = 0;
19843 if (cp_parser_allow_gnu_extensions_p (parser))
19844 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19846 /* Check for the ptr-operator production. */
19847 cp_parser_parse_tentatively (parser);
19848 /* Parse the ptr-operator. */
19849 code = cp_parser_ptr_operator (parser,
19850 &class_type,
19851 &cv_quals,
19852 &std_attributes);
19854 /* If that worked, then we have a ptr-operator. */
19855 if (cp_parser_parse_definitely (parser))
19857 /* If a ptr-operator was found, then this declarator was not
19858 parenthesized. */
19859 if (parenthesized_p)
19860 *parenthesized_p = true;
19861 /* The dependent declarator is optional if we are parsing an
19862 abstract-declarator. */
19863 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19864 cp_parser_parse_tentatively (parser);
19866 /* Parse the dependent declarator. */
19867 declarator = cp_parser_declarator (parser, dcl_kind,
19868 /*ctor_dtor_or_conv_p=*/NULL,
19869 /*parenthesized_p=*/NULL,
19870 /*member_p=*/false,
19871 friend_p);
19873 /* If we are parsing an abstract-declarator, we must handle the
19874 case where the dependent declarator is absent. */
19875 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19876 && !cp_parser_parse_definitely (parser))
19877 declarator = NULL;
19879 declarator = cp_parser_make_indirect_declarator
19880 (code, class_type, cv_quals, declarator, std_attributes);
19882 /* Everything else is a direct-declarator. */
19883 else
19885 if (parenthesized_p)
19886 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19887 CPP_OPEN_PAREN);
19888 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19889 ctor_dtor_or_conv_p,
19890 member_p, friend_p);
19893 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19894 declarator->attributes = gnu_attributes;
19895 return declarator;
19898 /* Parse a direct-declarator or direct-abstract-declarator.
19900 direct-declarator:
19901 declarator-id
19902 direct-declarator ( parameter-declaration-clause )
19903 cv-qualifier-seq [opt]
19904 ref-qualifier [opt]
19905 exception-specification [opt]
19906 direct-declarator [ constant-expression [opt] ]
19907 ( declarator )
19909 direct-abstract-declarator:
19910 direct-abstract-declarator [opt]
19911 ( parameter-declaration-clause )
19912 cv-qualifier-seq [opt]
19913 ref-qualifier [opt]
19914 exception-specification [opt]
19915 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19916 ( abstract-declarator )
19918 Returns a representation of the declarator. DCL_KIND is
19919 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19920 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19921 we are parsing a direct-declarator. It is
19922 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19923 of ambiguity we prefer an abstract declarator, as per
19924 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19925 as for cp_parser_declarator. */
19927 static cp_declarator *
19928 cp_parser_direct_declarator (cp_parser* parser,
19929 cp_parser_declarator_kind dcl_kind,
19930 int* ctor_dtor_or_conv_p,
19931 bool member_p, bool friend_p)
19933 cp_token *token;
19934 cp_declarator *declarator = NULL;
19935 tree scope = NULL_TREE;
19936 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19937 bool saved_in_declarator_p = parser->in_declarator_p;
19938 bool first = true;
19939 tree pushed_scope = NULL_TREE;
19940 cp_token *open_paren = NULL, *close_paren = NULL;
19942 while (true)
19944 /* Peek at the next token. */
19945 token = cp_lexer_peek_token (parser->lexer);
19946 if (token->type == CPP_OPEN_PAREN)
19948 /* This is either a parameter-declaration-clause, or a
19949 parenthesized declarator. When we know we are parsing a
19950 named declarator, it must be a parenthesized declarator
19951 if FIRST is true. For instance, `(int)' is a
19952 parameter-declaration-clause, with an omitted
19953 direct-abstract-declarator. But `((*))', is a
19954 parenthesized abstract declarator. Finally, when T is a
19955 template parameter `(T)' is a
19956 parameter-declaration-clause, and not a parenthesized
19957 named declarator.
19959 We first try and parse a parameter-declaration-clause,
19960 and then try a nested declarator (if FIRST is true).
19962 It is not an error for it not to be a
19963 parameter-declaration-clause, even when FIRST is
19964 false. Consider,
19966 int i (int);
19967 int i (3);
19969 The first is the declaration of a function while the
19970 second is the definition of a variable, including its
19971 initializer.
19973 Having seen only the parenthesis, we cannot know which of
19974 these two alternatives should be selected. Even more
19975 complex are examples like:
19977 int i (int (a));
19978 int i (int (3));
19980 The former is a function-declaration; the latter is a
19981 variable initialization.
19983 Thus again, we try a parameter-declaration-clause, and if
19984 that fails, we back out and return. */
19986 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19988 tree params;
19989 bool is_declarator = false;
19991 open_paren = NULL;
19993 /* In a member-declarator, the only valid interpretation
19994 of a parenthesis is the start of a
19995 parameter-declaration-clause. (It is invalid to
19996 initialize a static data member with a parenthesized
19997 initializer; only the "=" form of initialization is
19998 permitted.) */
19999 if (!member_p)
20000 cp_parser_parse_tentatively (parser);
20002 /* Consume the `('. */
20003 matching_parens parens;
20004 parens.consume_open (parser);
20005 if (first)
20007 /* If this is going to be an abstract declarator, we're
20008 in a declarator and we can't have default args. */
20009 parser->default_arg_ok_p = false;
20010 parser->in_declarator_p = true;
20013 begin_scope (sk_function_parms, NULL_TREE);
20015 /* Parse the parameter-declaration-clause. */
20016 params = cp_parser_parameter_declaration_clause (parser);
20018 /* Consume the `)'. */
20019 parens.require_close (parser);
20021 /* If all went well, parse the cv-qualifier-seq,
20022 ref-qualifier and the exception-specification. */
20023 if (member_p || cp_parser_parse_definitely (parser))
20025 cp_cv_quals cv_quals;
20026 cp_virt_specifiers virt_specifiers;
20027 cp_ref_qualifier ref_qual;
20028 tree exception_specification;
20029 tree late_return;
20030 tree attrs;
20031 bool memfn = (member_p || (pushed_scope
20032 && CLASS_TYPE_P (pushed_scope)));
20034 is_declarator = true;
20036 if (ctor_dtor_or_conv_p)
20037 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
20038 first = false;
20040 /* Parse the cv-qualifier-seq. */
20041 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20042 /* Parse the ref-qualifier. */
20043 ref_qual = cp_parser_ref_qualifier_opt (parser);
20044 /* Parse the tx-qualifier. */
20045 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
20046 /* And the exception-specification. */
20047 exception_specification
20048 = cp_parser_exception_specification_opt (parser);
20050 attrs = cp_parser_std_attribute_spec_seq (parser);
20052 /* In here, we handle cases where attribute is used after
20053 the function declaration. For example:
20054 void func (int x) __attribute__((vector(..))); */
20055 tree gnu_attrs = NULL_TREE;
20056 if (flag_cilkplus
20057 && cp_next_tokens_can_be_gnu_attribute_p (parser))
20059 cp_parser_parse_tentatively (parser);
20060 tree attr = cp_parser_gnu_attributes_opt (parser);
20061 if (cp_lexer_next_token_is_not (parser->lexer,
20062 CPP_SEMICOLON)
20063 && cp_lexer_next_token_is_not (parser->lexer,
20064 CPP_OPEN_BRACE))
20065 cp_parser_abort_tentative_parse (parser);
20066 else if (!cp_parser_parse_definitely (parser))
20068 else
20069 gnu_attrs = attr;
20071 tree requires_clause = NULL_TREE;
20072 late_return = (cp_parser_late_return_type_opt
20073 (parser, declarator, requires_clause,
20074 memfn ? cv_quals : -1));
20076 /* Parse the virt-specifier-seq. */
20077 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
20079 /* Create the function-declarator. */
20080 declarator = make_call_declarator (declarator,
20081 params,
20082 cv_quals,
20083 virt_specifiers,
20084 ref_qual,
20085 tx_qual,
20086 exception_specification,
20087 late_return,
20088 requires_clause);
20089 declarator->std_attributes = attrs;
20090 declarator->attributes = gnu_attrs;
20091 /* Any subsequent parameter lists are to do with
20092 return type, so are not those of the declared
20093 function. */
20094 parser->default_arg_ok_p = false;
20097 /* Remove the function parms from scope. */
20098 pop_bindings_and_leave_scope ();
20100 if (is_declarator)
20101 /* Repeat the main loop. */
20102 continue;
20105 /* If this is the first, we can try a parenthesized
20106 declarator. */
20107 if (first)
20109 bool saved_in_type_id_in_expr_p;
20111 parser->default_arg_ok_p = saved_default_arg_ok_p;
20112 parser->in_declarator_p = saved_in_declarator_p;
20114 open_paren = token;
20115 /* Consume the `('. */
20116 matching_parens parens;
20117 parens.consume_open (parser);
20118 /* Parse the nested declarator. */
20119 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20120 parser->in_type_id_in_expr_p = true;
20121 declarator
20122 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20123 /*parenthesized_p=*/NULL,
20124 member_p, friend_p);
20125 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20126 first = false;
20127 /* Expect a `)'. */
20128 close_paren = cp_lexer_peek_token (parser->lexer);
20129 if (!parens.require_close (parser))
20130 declarator = cp_error_declarator;
20131 if (declarator == cp_error_declarator)
20132 break;
20134 goto handle_declarator;
20136 /* Otherwise, we must be done. */
20137 else
20138 break;
20140 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20141 && token->type == CPP_OPEN_SQUARE
20142 && !cp_next_tokens_can_be_attribute_p (parser))
20144 /* Parse an array-declarator. */
20145 tree bounds, attrs;
20147 if (ctor_dtor_or_conv_p)
20148 *ctor_dtor_or_conv_p = 0;
20150 open_paren = NULL;
20151 first = false;
20152 parser->default_arg_ok_p = false;
20153 parser->in_declarator_p = true;
20154 /* Consume the `['. */
20155 cp_lexer_consume_token (parser->lexer);
20156 /* Peek at the next token. */
20157 token = cp_lexer_peek_token (parser->lexer);
20158 /* If the next token is `]', then there is no
20159 constant-expression. */
20160 if (token->type != CPP_CLOSE_SQUARE)
20162 bool non_constant_p;
20163 bounds
20164 = cp_parser_constant_expression (parser,
20165 /*allow_non_constant=*/true,
20166 &non_constant_p);
20167 if (!non_constant_p)
20168 /* OK */;
20169 else if (error_operand_p (bounds))
20170 /* Already gave an error. */;
20171 else if (!parser->in_function_body
20172 || current_binding_level->kind == sk_function_parms)
20174 /* Normally, the array bound must be an integral constant
20175 expression. However, as an extension, we allow VLAs
20176 in function scopes as long as they aren't part of a
20177 parameter declaration. */
20178 cp_parser_error (parser,
20179 "array bound is not an integer constant");
20180 bounds = error_mark_node;
20182 else if (processing_template_decl
20183 && !type_dependent_expression_p (bounds))
20185 /* Remember this wasn't a constant-expression. */
20186 bounds = build_nop (TREE_TYPE (bounds), bounds);
20187 TREE_SIDE_EFFECTS (bounds) = 1;
20190 else
20191 bounds = NULL_TREE;
20192 /* Look for the closing `]'. */
20193 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20195 declarator = cp_error_declarator;
20196 break;
20199 attrs = cp_parser_std_attribute_spec_seq (parser);
20200 declarator = make_array_declarator (declarator, bounds);
20201 declarator->std_attributes = attrs;
20203 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20206 tree qualifying_scope;
20207 tree unqualified_name;
20208 tree attrs;
20209 special_function_kind sfk;
20210 bool abstract_ok;
20211 bool pack_expansion_p = false;
20212 cp_token *declarator_id_start_token;
20214 /* Parse a declarator-id */
20215 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20216 if (abstract_ok)
20218 cp_parser_parse_tentatively (parser);
20220 /* If we see an ellipsis, we should be looking at a
20221 parameter pack. */
20222 if (token->type == CPP_ELLIPSIS)
20224 /* Consume the `...' */
20225 cp_lexer_consume_token (parser->lexer);
20227 pack_expansion_p = true;
20231 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20232 unqualified_name
20233 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20234 qualifying_scope = parser->scope;
20235 if (abstract_ok)
20237 bool okay = false;
20239 if (!unqualified_name && pack_expansion_p)
20241 /* Check whether an error occurred. */
20242 okay = !cp_parser_error_occurred (parser);
20244 /* We already consumed the ellipsis to mark a
20245 parameter pack, but we have no way to report it,
20246 so abort the tentative parse. We will be exiting
20247 immediately anyway. */
20248 cp_parser_abort_tentative_parse (parser);
20250 else
20251 okay = cp_parser_parse_definitely (parser);
20253 if (!okay)
20254 unqualified_name = error_mark_node;
20255 else if (unqualified_name
20256 && (qualifying_scope
20257 || (!identifier_p (unqualified_name))))
20259 cp_parser_error (parser, "expected unqualified-id");
20260 unqualified_name = error_mark_node;
20264 if (!unqualified_name)
20265 return NULL;
20266 if (unqualified_name == error_mark_node)
20268 declarator = cp_error_declarator;
20269 pack_expansion_p = false;
20270 declarator->parameter_pack_p = false;
20271 break;
20274 attrs = cp_parser_std_attribute_spec_seq (parser);
20276 if (qualifying_scope && at_namespace_scope_p ()
20277 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20279 /* In the declaration of a member of a template class
20280 outside of the class itself, the SCOPE will sometimes
20281 be a TYPENAME_TYPE. For example, given:
20283 template <typename T>
20284 int S<T>::R::i = 3;
20286 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20287 this context, we must resolve S<T>::R to an ordinary
20288 type, rather than a typename type.
20290 The reason we normally avoid resolving TYPENAME_TYPEs
20291 is that a specialization of `S' might render
20292 `S<T>::R' not a type. However, if `S' is
20293 specialized, then this `i' will not be used, so there
20294 is no harm in resolving the types here. */
20295 tree type;
20297 /* Resolve the TYPENAME_TYPE. */
20298 type = resolve_typename_type (qualifying_scope,
20299 /*only_current_p=*/false);
20300 /* If that failed, the declarator is invalid. */
20301 if (TREE_CODE (type) == TYPENAME_TYPE)
20303 if (typedef_variant_p (type))
20304 error_at (declarator_id_start_token->location,
20305 "cannot define member of dependent typedef "
20306 "%qT", type);
20307 else
20308 error_at (declarator_id_start_token->location,
20309 "%<%T::%E%> is not a type",
20310 TYPE_CONTEXT (qualifying_scope),
20311 TYPE_IDENTIFIER (qualifying_scope));
20313 qualifying_scope = type;
20316 sfk = sfk_none;
20318 if (unqualified_name)
20320 tree class_type;
20322 if (qualifying_scope
20323 && CLASS_TYPE_P (qualifying_scope))
20324 class_type = qualifying_scope;
20325 else
20326 class_type = current_class_type;
20328 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20330 tree name_type = TREE_TYPE (unqualified_name);
20332 if (!class_type || !same_type_p (name_type, class_type))
20334 /* We do not attempt to print the declarator
20335 here because we do not have enough
20336 information about its original syntactic
20337 form. */
20338 cp_parser_error (parser, "invalid declarator");
20339 declarator = cp_error_declarator;
20340 break;
20342 else if (qualifying_scope
20343 && CLASSTYPE_USE_TEMPLATE (name_type))
20345 error_at (declarator_id_start_token->location,
20346 "invalid use of constructor as a template");
20347 inform (declarator_id_start_token->location,
20348 "use %<%T::%D%> instead of %<%T::%D%> to "
20349 "name the constructor in a qualified name",
20350 class_type,
20351 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20352 class_type, name_type);
20353 declarator = cp_error_declarator;
20354 break;
20356 unqualified_name = constructor_name (class_type);
20359 if (class_type)
20361 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20362 sfk = sfk_destructor;
20363 else if (identifier_p (unqualified_name)
20364 && IDENTIFIER_CONV_OP_P (unqualified_name))
20365 sfk = sfk_conversion;
20366 else if (/* There's no way to declare a constructor
20367 for an unnamed type, even if the type
20368 got a name for linkage purposes. */
20369 !TYPE_WAS_UNNAMED (class_type)
20370 /* Handle correctly (c++/19200):
20372 struct S {
20373 struct T{};
20374 friend void S(T);
20377 and also:
20379 namespace N {
20380 void S();
20383 struct S {
20384 friend void N::S();
20385 }; */
20386 && (!friend_p || class_type == qualifying_scope)
20387 && constructor_name_p (unqualified_name,
20388 class_type))
20389 sfk = sfk_constructor;
20390 else if (is_overloaded_fn (unqualified_name)
20391 && DECL_CONSTRUCTOR_P (get_first_fn
20392 (unqualified_name)))
20393 sfk = sfk_constructor;
20395 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20396 *ctor_dtor_or_conv_p = -1;
20399 declarator = make_id_declarator (qualifying_scope,
20400 unqualified_name,
20401 sfk);
20402 declarator->std_attributes = attrs;
20403 declarator->id_loc = token->location;
20404 declarator->parameter_pack_p = pack_expansion_p;
20406 if (pack_expansion_p)
20407 maybe_warn_variadic_templates ();
20410 handle_declarator:;
20411 scope = get_scope_of_declarator (declarator);
20412 if (scope)
20414 /* Any names that appear after the declarator-id for a
20415 member are looked up in the containing scope. */
20416 if (at_function_scope_p ())
20418 /* But declarations with qualified-ids can't appear in a
20419 function. */
20420 cp_parser_error (parser, "qualified-id in declaration");
20421 declarator = cp_error_declarator;
20422 break;
20424 pushed_scope = push_scope (scope);
20426 parser->in_declarator_p = true;
20427 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20428 || (declarator && declarator->kind == cdk_id))
20429 /* Default args are only allowed on function
20430 declarations. */
20431 parser->default_arg_ok_p = saved_default_arg_ok_p;
20432 else
20433 parser->default_arg_ok_p = false;
20435 first = false;
20437 /* We're done. */
20438 else
20439 break;
20442 /* For an abstract declarator, we might wind up with nothing at this
20443 point. That's an error; the declarator is not optional. */
20444 if (!declarator)
20445 cp_parser_error (parser, "expected declarator");
20446 else if (open_paren)
20448 /* Record overly parenthesized declarator so we can give a
20449 diagnostic about confusing decl/expr disambiguation. */
20450 if (declarator->kind == cdk_array)
20452 /* If the open and close parens are on different lines, this
20453 is probably a formatting thing, so ignore. */
20454 expanded_location open = expand_location (open_paren->location);
20455 expanded_location close = expand_location (close_paren->location);
20456 if (open.line != close.line || open.file != close.file)
20457 open_paren = NULL;
20459 if (open_paren)
20460 declarator->parenthesized = open_paren->location;
20463 /* If we entered a scope, we must exit it now. */
20464 if (pushed_scope)
20465 pop_scope (pushed_scope);
20467 parser->default_arg_ok_p = saved_default_arg_ok_p;
20468 parser->in_declarator_p = saved_in_declarator_p;
20470 return declarator;
20473 /* Parse a ptr-operator.
20475 ptr-operator:
20476 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20477 * cv-qualifier-seq [opt]
20479 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20480 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20482 GNU Extension:
20484 ptr-operator:
20485 & cv-qualifier-seq [opt]
20487 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20488 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20489 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20490 filled in with the TYPE containing the member. *CV_QUALS is
20491 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20492 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20493 Note that the tree codes returned by this function have nothing
20494 to do with the types of trees that will be eventually be created
20495 to represent the pointer or reference type being parsed. They are
20496 just constants with suggestive names. */
20497 static enum tree_code
20498 cp_parser_ptr_operator (cp_parser* parser,
20499 tree* type,
20500 cp_cv_quals *cv_quals,
20501 tree *attributes)
20503 enum tree_code code = ERROR_MARK;
20504 cp_token *token;
20505 tree attrs = NULL_TREE;
20507 /* Assume that it's not a pointer-to-member. */
20508 *type = NULL_TREE;
20509 /* And that there are no cv-qualifiers. */
20510 *cv_quals = TYPE_UNQUALIFIED;
20512 /* Peek at the next token. */
20513 token = cp_lexer_peek_token (parser->lexer);
20515 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20516 if (token->type == CPP_MULT)
20517 code = INDIRECT_REF;
20518 else if (token->type == CPP_AND)
20519 code = ADDR_EXPR;
20520 else if ((cxx_dialect != cxx98) &&
20521 token->type == CPP_AND_AND) /* C++0x only */
20522 code = NON_LVALUE_EXPR;
20524 if (code != ERROR_MARK)
20526 /* Consume the `*', `&' or `&&'. */
20527 cp_lexer_consume_token (parser->lexer);
20529 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20530 `&', if we are allowing GNU extensions. (The only qualifier
20531 that can legally appear after `&' is `restrict', but that is
20532 enforced during semantic analysis. */
20533 if (code == INDIRECT_REF
20534 || cp_parser_allow_gnu_extensions_p (parser))
20535 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20537 attrs = cp_parser_std_attribute_spec_seq (parser);
20538 if (attributes != NULL)
20539 *attributes = attrs;
20541 else
20543 /* Try the pointer-to-member case. */
20544 cp_parser_parse_tentatively (parser);
20545 /* Look for the optional `::' operator. */
20546 cp_parser_global_scope_opt (parser,
20547 /*current_scope_valid_p=*/false);
20548 /* Look for the nested-name specifier. */
20549 token = cp_lexer_peek_token (parser->lexer);
20550 cp_parser_nested_name_specifier (parser,
20551 /*typename_keyword_p=*/false,
20552 /*check_dependency_p=*/true,
20553 /*type_p=*/false,
20554 /*is_declaration=*/false);
20555 /* If we found it, and the next token is a `*', then we are
20556 indeed looking at a pointer-to-member operator. */
20557 if (!cp_parser_error_occurred (parser)
20558 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20560 /* Indicate that the `*' operator was used. */
20561 code = INDIRECT_REF;
20563 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20564 error_at (token->location, "%qD is a namespace", parser->scope);
20565 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20566 error_at (token->location, "cannot form pointer to member of "
20567 "non-class %q#T", parser->scope);
20568 else
20570 /* The type of which the member is a member is given by the
20571 current SCOPE. */
20572 *type = parser->scope;
20573 /* The next name will not be qualified. */
20574 parser->scope = NULL_TREE;
20575 parser->qualifying_scope = NULL_TREE;
20576 parser->object_scope = NULL_TREE;
20577 /* Look for optional c++11 attributes. */
20578 attrs = cp_parser_std_attribute_spec_seq (parser);
20579 if (attributes != NULL)
20580 *attributes = attrs;
20581 /* Look for the optional cv-qualifier-seq. */
20582 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20585 /* If that didn't work we don't have a ptr-operator. */
20586 if (!cp_parser_parse_definitely (parser))
20587 cp_parser_error (parser, "expected ptr-operator");
20590 return code;
20593 /* Parse an (optional) cv-qualifier-seq.
20595 cv-qualifier-seq:
20596 cv-qualifier cv-qualifier-seq [opt]
20598 cv-qualifier:
20599 const
20600 volatile
20602 GNU Extension:
20604 cv-qualifier:
20605 __restrict__
20607 Returns a bitmask representing the cv-qualifiers. */
20609 static cp_cv_quals
20610 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20612 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20614 while (true)
20616 cp_token *token;
20617 cp_cv_quals cv_qualifier;
20619 /* Peek at the next token. */
20620 token = cp_lexer_peek_token (parser->lexer);
20621 /* See if it's a cv-qualifier. */
20622 switch (token->keyword)
20624 case RID_CONST:
20625 cv_qualifier = TYPE_QUAL_CONST;
20626 break;
20628 case RID_VOLATILE:
20629 cv_qualifier = TYPE_QUAL_VOLATILE;
20630 break;
20632 case RID_RESTRICT:
20633 cv_qualifier = TYPE_QUAL_RESTRICT;
20634 break;
20636 default:
20637 cv_qualifier = TYPE_UNQUALIFIED;
20638 break;
20641 if (!cv_qualifier)
20642 break;
20644 if (cv_quals & cv_qualifier)
20646 gcc_rich_location richloc (token->location);
20647 richloc.add_fixit_remove ();
20648 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20649 cp_lexer_purge_token (parser->lexer);
20651 else
20653 cp_lexer_consume_token (parser->lexer);
20654 cv_quals |= cv_qualifier;
20658 return cv_quals;
20661 /* Parse an (optional) ref-qualifier
20663 ref-qualifier:
20667 Returns cp_ref_qualifier representing ref-qualifier. */
20669 static cp_ref_qualifier
20670 cp_parser_ref_qualifier_opt (cp_parser* parser)
20672 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20674 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20675 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20676 return ref_qual;
20678 while (true)
20680 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20681 cp_token *token = cp_lexer_peek_token (parser->lexer);
20683 switch (token->type)
20685 case CPP_AND:
20686 curr_ref_qual = REF_QUAL_LVALUE;
20687 break;
20689 case CPP_AND_AND:
20690 curr_ref_qual = REF_QUAL_RVALUE;
20691 break;
20693 default:
20694 curr_ref_qual = REF_QUAL_NONE;
20695 break;
20698 if (!curr_ref_qual)
20699 break;
20700 else if (ref_qual)
20702 error_at (token->location, "multiple ref-qualifiers");
20703 cp_lexer_purge_token (parser->lexer);
20705 else
20707 ref_qual = curr_ref_qual;
20708 cp_lexer_consume_token (parser->lexer);
20712 return ref_qual;
20715 /* Parse an optional tx-qualifier.
20717 tx-qualifier:
20718 transaction_safe
20719 transaction_safe_dynamic */
20721 static tree
20722 cp_parser_tx_qualifier_opt (cp_parser *parser)
20724 cp_token *token = cp_lexer_peek_token (parser->lexer);
20725 if (token->type == CPP_NAME)
20727 tree name = token->u.value;
20728 const char *p = IDENTIFIER_POINTER (name);
20729 const int len = strlen ("transaction_safe");
20730 if (!strncmp (p, "transaction_safe", len))
20732 p += len;
20733 if (*p == '\0'
20734 || !strcmp (p, "_dynamic"))
20736 cp_lexer_consume_token (parser->lexer);
20737 if (!flag_tm)
20739 error ("%qE requires %<-fgnu-tm%>", name);
20740 return NULL_TREE;
20742 else
20743 return name;
20747 return NULL_TREE;
20750 /* Parse an (optional) virt-specifier-seq.
20752 virt-specifier-seq:
20753 virt-specifier virt-specifier-seq [opt]
20755 virt-specifier:
20756 override
20757 final
20759 Returns a bitmask representing the virt-specifiers. */
20761 static cp_virt_specifiers
20762 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20764 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20766 while (true)
20768 cp_token *token;
20769 cp_virt_specifiers virt_specifier;
20771 /* Peek at the next token. */
20772 token = cp_lexer_peek_token (parser->lexer);
20773 /* See if it's a virt-specifier-qualifier. */
20774 if (token->type != CPP_NAME)
20775 break;
20776 if (id_equal (token->u.value, "override"))
20778 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20779 virt_specifier = VIRT_SPEC_OVERRIDE;
20781 else if (id_equal (token->u.value, "final"))
20783 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20784 virt_specifier = VIRT_SPEC_FINAL;
20786 else if (id_equal (token->u.value, "__final"))
20788 virt_specifier = VIRT_SPEC_FINAL;
20790 else
20791 break;
20793 if (virt_specifiers & virt_specifier)
20795 gcc_rich_location richloc (token->location);
20796 richloc.add_fixit_remove ();
20797 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20798 cp_lexer_purge_token (parser->lexer);
20800 else
20802 cp_lexer_consume_token (parser->lexer);
20803 virt_specifiers |= virt_specifier;
20806 return virt_specifiers;
20809 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20810 is in scope even though it isn't real. */
20812 void
20813 inject_this_parameter (tree ctype, cp_cv_quals quals)
20815 tree this_parm;
20817 if (current_class_ptr)
20819 /* We don't clear this between NSDMIs. Is it already what we want? */
20820 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20821 if (DECL_P (current_class_ptr)
20822 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
20823 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
20824 && cp_type_quals (type) == quals)
20825 return;
20828 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20829 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20830 current_class_ptr = NULL_TREE;
20831 current_class_ref
20832 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20833 current_class_ptr = this_parm;
20836 /* Return true iff our current scope is a non-static data member
20837 initializer. */
20839 bool
20840 parsing_nsdmi (void)
20842 /* We recognize NSDMI context by the context-less 'this' pointer set up
20843 by the function above. */
20844 if (current_class_ptr
20845 && TREE_CODE (current_class_ptr) == PARM_DECL
20846 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20847 return true;
20848 return false;
20851 /* Parse a late-specified return type, if any. This is not a separate
20852 non-terminal, but part of a function declarator, which looks like
20854 -> trailing-type-specifier-seq abstract-declarator(opt)
20856 Returns the type indicated by the type-id.
20858 In addition to this, parse any queued up #pragma omp declare simd
20859 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20860 #pragma acc routine clauses.
20862 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20863 function. */
20865 static tree
20866 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20867 tree& requires_clause, cp_cv_quals quals)
20869 cp_token *token;
20870 tree type = NULL_TREE;
20871 bool declare_simd_p = (parser->omp_declare_simd
20872 && declarator
20873 && declarator->kind == cdk_id);
20875 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20876 && declarator && declarator->kind == cdk_id);
20878 bool oacc_routine_p = (parser->oacc_routine
20879 && declarator
20880 && declarator->kind == cdk_id);
20882 /* Peek at the next token. */
20883 token = cp_lexer_peek_token (parser->lexer);
20884 /* A late-specified return type is indicated by an initial '->'. */
20885 if (token->type != CPP_DEREF
20886 && token->keyword != RID_REQUIRES
20887 && !(token->type == CPP_NAME
20888 && token->u.value == ridpointers[RID_REQUIRES])
20889 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20890 return NULL_TREE;
20892 tree save_ccp = current_class_ptr;
20893 tree save_ccr = current_class_ref;
20894 if (quals >= 0)
20896 /* DR 1207: 'this' is in scope in the trailing return type. */
20897 inject_this_parameter (current_class_type, quals);
20900 if (token->type == CPP_DEREF)
20902 /* Consume the ->. */
20903 cp_lexer_consume_token (parser->lexer);
20905 type = cp_parser_trailing_type_id (parser);
20908 /* Function declarations may be followed by a trailing
20909 requires-clause. */
20910 requires_clause = cp_parser_requires_clause_opt (parser);
20912 if (cilk_simd_fn_vector_p)
20913 declarator->attributes
20914 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20915 declarator->attributes);
20916 if (declare_simd_p)
20917 declarator->attributes
20918 = cp_parser_late_parsing_omp_declare_simd (parser,
20919 declarator->attributes);
20920 if (oacc_routine_p)
20921 declarator->attributes
20922 = cp_parser_late_parsing_oacc_routine (parser,
20923 declarator->attributes);
20925 if (quals >= 0)
20927 current_class_ptr = save_ccp;
20928 current_class_ref = save_ccr;
20931 return type;
20934 /* Parse a declarator-id.
20936 declarator-id:
20937 id-expression
20938 :: [opt] nested-name-specifier [opt] type-name
20940 In the `id-expression' case, the value returned is as for
20941 cp_parser_id_expression if the id-expression was an unqualified-id.
20942 If the id-expression was a qualified-id, then a SCOPE_REF is
20943 returned. The first operand is the scope (either a NAMESPACE_DECL
20944 or TREE_TYPE), but the second is still just a representation of an
20945 unqualified-id. */
20947 static tree
20948 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20950 tree id;
20951 /* The expression must be an id-expression. Assume that qualified
20952 names are the names of types so that:
20954 template <class T>
20955 int S<T>::R::i = 3;
20957 will work; we must treat `S<T>::R' as the name of a type.
20958 Similarly, assume that qualified names are templates, where
20959 required, so that:
20961 template <class T>
20962 int S<T>::R<T>::i = 3;
20964 will work, too. */
20965 id = cp_parser_id_expression (parser,
20966 /*template_keyword_p=*/false,
20967 /*check_dependency_p=*/false,
20968 /*template_p=*/NULL,
20969 /*declarator_p=*/true,
20970 optional_p);
20971 if (id && BASELINK_P (id))
20972 id = BASELINK_FUNCTIONS (id);
20973 return id;
20976 /* Parse a type-id.
20978 type-id:
20979 type-specifier-seq abstract-declarator [opt]
20981 Returns the TYPE specified. */
20983 static tree
20984 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20985 bool is_trailing_return)
20987 cp_decl_specifier_seq type_specifier_seq;
20988 cp_declarator *abstract_declarator;
20990 /* Parse the type-specifier-seq. */
20991 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20992 is_trailing_return,
20993 &type_specifier_seq);
20994 if (is_template_arg && type_specifier_seq.type
20995 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20996 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20997 /* A bare template name as a template argument is a template template
20998 argument, not a placeholder, so fail parsing it as a type argument. */
21000 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
21001 cp_parser_simulate_error (parser);
21002 return error_mark_node;
21004 if (type_specifier_seq.type == error_mark_node)
21005 return error_mark_node;
21007 /* There might or might not be an abstract declarator. */
21008 cp_parser_parse_tentatively (parser);
21009 /* Look for the declarator. */
21010 abstract_declarator
21011 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
21012 /*parenthesized_p=*/NULL,
21013 /*member_p=*/false,
21014 /*friend_p=*/false);
21015 /* Check to see if there really was a declarator. */
21016 if (!cp_parser_parse_definitely (parser))
21017 abstract_declarator = NULL;
21019 if (type_specifier_seq.type
21020 /* The concepts TS allows 'auto' as a type-id. */
21021 && (!flag_concepts || parser->in_type_id_in_expr_p)
21022 /* None of the valid uses of 'auto' in C++14 involve the type-id
21023 nonterminal, but it is valid in a trailing-return-type. */
21024 && !(cxx_dialect >= cxx14 && is_trailing_return))
21025 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
21027 /* A type-id with type 'auto' is only ok if the abstract declarator
21028 is a function declarator with a late-specified return type.
21030 A type-id with 'auto' is also valid in a trailing-return-type
21031 in a compound-requirement. */
21032 if (abstract_declarator
21033 && abstract_declarator->kind == cdk_function
21034 && abstract_declarator->u.function.late_return_type)
21035 /* OK */;
21036 else if (parser->in_result_type_constraint_p)
21037 /* OK */;
21038 else
21040 location_t loc = type_specifier_seq.locations[ds_type_spec];
21041 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
21043 error_at (loc, "missing template arguments after %qT",
21044 auto_node);
21045 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
21046 tmpl);
21048 else
21049 error_at (loc, "invalid use of %qT", auto_node);
21050 return error_mark_node;
21054 return groktypename (&type_specifier_seq, abstract_declarator,
21055 is_template_arg);
21058 static tree
21059 cp_parser_type_id (cp_parser *parser)
21061 return cp_parser_type_id_1 (parser, false, false);
21064 static tree
21065 cp_parser_template_type_arg (cp_parser *parser)
21067 tree r;
21068 const char *saved_message = parser->type_definition_forbidden_message;
21069 parser->type_definition_forbidden_message
21070 = G_("types may not be defined in template arguments");
21071 r = cp_parser_type_id_1 (parser, true, false);
21072 parser->type_definition_forbidden_message = saved_message;
21073 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
21075 error ("invalid use of %<auto%> in template argument");
21076 r = error_mark_node;
21078 return r;
21081 static tree
21082 cp_parser_trailing_type_id (cp_parser *parser)
21084 return cp_parser_type_id_1 (parser, false, true);
21087 /* Parse a type-specifier-seq.
21089 type-specifier-seq:
21090 type-specifier type-specifier-seq [opt]
21092 GNU extension:
21094 type-specifier-seq:
21095 attributes type-specifier-seq [opt]
21097 If IS_DECLARATION is true, we are at the start of a "condition" or
21098 exception-declaration, so we might be followed by a declarator-id.
21100 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21101 i.e. we've just seen "->".
21103 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21105 static void
21106 cp_parser_type_specifier_seq (cp_parser* parser,
21107 bool is_declaration,
21108 bool is_trailing_return,
21109 cp_decl_specifier_seq *type_specifier_seq)
21111 bool seen_type_specifier = false;
21112 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21113 cp_token *start_token = NULL;
21115 /* Clear the TYPE_SPECIFIER_SEQ. */
21116 clear_decl_specs (type_specifier_seq);
21118 /* In the context of a trailing return type, enum E { } is an
21119 elaborated-type-specifier followed by a function-body, not an
21120 enum-specifier. */
21121 if (is_trailing_return)
21122 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21124 /* Parse the type-specifiers and attributes. */
21125 while (true)
21127 tree type_specifier;
21128 bool is_cv_qualifier;
21130 /* Check for attributes first. */
21131 if (cp_next_tokens_can_be_attribute_p (parser))
21133 type_specifier_seq->attributes =
21134 chainon (type_specifier_seq->attributes,
21135 cp_parser_attributes_opt (parser));
21136 continue;
21139 /* record the token of the beginning of the type specifier seq,
21140 for error reporting purposes*/
21141 if (!start_token)
21142 start_token = cp_lexer_peek_token (parser->lexer);
21144 /* Look for the type-specifier. */
21145 type_specifier = cp_parser_type_specifier (parser,
21146 flags,
21147 type_specifier_seq,
21148 /*is_declaration=*/false,
21149 NULL,
21150 &is_cv_qualifier);
21151 if (!type_specifier)
21153 /* If the first type-specifier could not be found, this is not a
21154 type-specifier-seq at all. */
21155 if (!seen_type_specifier)
21157 /* Set in_declarator_p to avoid skipping to the semicolon. */
21158 int in_decl = parser->in_declarator_p;
21159 parser->in_declarator_p = true;
21161 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21162 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21163 cp_parser_error (parser, "expected type-specifier");
21165 parser->in_declarator_p = in_decl;
21167 type_specifier_seq->type = error_mark_node;
21168 return;
21170 /* If subsequent type-specifiers could not be found, the
21171 type-specifier-seq is complete. */
21172 break;
21175 seen_type_specifier = true;
21176 /* The standard says that a condition can be:
21178 type-specifier-seq declarator = assignment-expression
21180 However, given:
21182 struct S {};
21183 if (int S = ...)
21185 we should treat the "S" as a declarator, not as a
21186 type-specifier. The standard doesn't say that explicitly for
21187 type-specifier-seq, but it does say that for
21188 decl-specifier-seq in an ordinary declaration. Perhaps it
21189 would be clearer just to allow a decl-specifier-seq here, and
21190 then add a semantic restriction that if any decl-specifiers
21191 that are not type-specifiers appear, the program is invalid. */
21192 if (is_declaration && !is_cv_qualifier)
21193 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21197 /* Return whether the function currently being declared has an associated
21198 template parameter list. */
21200 static bool
21201 function_being_declared_is_template_p (cp_parser* parser)
21203 if (!current_template_parms || processing_template_parmlist)
21204 return false;
21206 if (parser->implicit_template_scope)
21207 return true;
21209 if (at_class_scope_p ()
21210 && TYPE_BEING_DEFINED (current_class_type))
21211 return parser->num_template_parameter_lists != 0;
21213 return ((int) parser->num_template_parameter_lists > template_class_depth
21214 (current_class_type));
21217 /* Parse a parameter-declaration-clause.
21219 parameter-declaration-clause:
21220 parameter-declaration-list [opt] ... [opt]
21221 parameter-declaration-list , ...
21223 Returns a representation for the parameter declarations. A return
21224 value of NULL indicates a parameter-declaration-clause consisting
21225 only of an ellipsis. */
21227 static tree
21228 cp_parser_parameter_declaration_clause (cp_parser* parser)
21230 tree parameters;
21231 cp_token *token;
21232 bool ellipsis_p;
21233 bool is_error;
21235 struct cleanup {
21236 cp_parser* parser;
21237 int auto_is_implicit_function_template_parm_p;
21238 ~cleanup() {
21239 parser->auto_is_implicit_function_template_parm_p
21240 = auto_is_implicit_function_template_parm_p;
21242 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21244 (void) cleanup;
21246 if (!processing_specialization
21247 && !processing_template_parmlist
21248 && !processing_explicit_instantiation)
21249 if (!current_function_decl
21250 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21251 parser->auto_is_implicit_function_template_parm_p = true;
21253 /* Peek at the next token. */
21254 token = cp_lexer_peek_token (parser->lexer);
21255 /* Check for trivial parameter-declaration-clauses. */
21256 if (token->type == CPP_ELLIPSIS)
21258 /* Consume the `...' token. */
21259 cp_lexer_consume_token (parser->lexer);
21260 return NULL_TREE;
21262 else if (token->type == CPP_CLOSE_PAREN)
21263 /* There are no parameters. */
21265 #ifndef NO_IMPLICIT_EXTERN_C
21266 if (in_system_header_at (input_location)
21267 && current_class_type == NULL
21268 && current_lang_name == lang_name_c)
21269 return NULL_TREE;
21270 else
21271 #endif
21272 return void_list_node;
21274 /* Check for `(void)', too, which is a special case. */
21275 else if (token->keyword == RID_VOID
21276 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21277 == CPP_CLOSE_PAREN))
21279 /* Consume the `void' token. */
21280 cp_lexer_consume_token (parser->lexer);
21281 /* There are no parameters. */
21282 return void_list_node;
21285 /* Parse the parameter-declaration-list. */
21286 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21287 /* If a parse error occurred while parsing the
21288 parameter-declaration-list, then the entire
21289 parameter-declaration-clause is erroneous. */
21290 if (is_error)
21291 return NULL;
21293 /* Peek at the next token. */
21294 token = cp_lexer_peek_token (parser->lexer);
21295 /* If it's a `,', the clause should terminate with an ellipsis. */
21296 if (token->type == CPP_COMMA)
21298 /* Consume the `,'. */
21299 cp_lexer_consume_token (parser->lexer);
21300 /* Expect an ellipsis. */
21301 ellipsis_p
21302 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21304 /* It might also be `...' if the optional trailing `,' was
21305 omitted. */
21306 else if (token->type == CPP_ELLIPSIS)
21308 /* Consume the `...' token. */
21309 cp_lexer_consume_token (parser->lexer);
21310 /* And remember that we saw it. */
21311 ellipsis_p = true;
21313 else
21314 ellipsis_p = false;
21316 /* Finish the parameter list. */
21317 if (!ellipsis_p)
21318 parameters = chainon (parameters, void_list_node);
21320 return parameters;
21323 /* Parse a parameter-declaration-list.
21325 parameter-declaration-list:
21326 parameter-declaration
21327 parameter-declaration-list , parameter-declaration
21329 Returns a representation of the parameter-declaration-list, as for
21330 cp_parser_parameter_declaration_clause. However, the
21331 `void_list_node' is never appended to the list. Upon return,
21332 *IS_ERROR will be true iff an error occurred. */
21334 static tree
21335 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21337 tree parameters = NULL_TREE;
21338 tree *tail = &parameters;
21339 bool saved_in_unbraced_linkage_specification_p;
21340 int index = 0;
21342 /* Assume all will go well. */
21343 *is_error = false;
21344 /* The special considerations that apply to a function within an
21345 unbraced linkage specifications do not apply to the parameters
21346 to the function. */
21347 saved_in_unbraced_linkage_specification_p
21348 = parser->in_unbraced_linkage_specification_p;
21349 parser->in_unbraced_linkage_specification_p = false;
21351 /* Look for more parameters. */
21352 while (true)
21354 cp_parameter_declarator *parameter;
21355 tree decl = error_mark_node;
21356 bool parenthesized_p = false;
21357 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21358 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21359 (current_template_parms)) : 0);
21361 /* Parse the parameter. */
21362 parameter
21363 = cp_parser_parameter_declaration (parser,
21364 /*template_parm_p=*/false,
21365 &parenthesized_p);
21367 /* We don't know yet if the enclosing context is deprecated, so wait
21368 and warn in grokparms if appropriate. */
21369 deprecated_state = DEPRECATED_SUPPRESS;
21371 if (parameter)
21373 /* If a function parameter pack was specified and an implicit template
21374 parameter was introduced during cp_parser_parameter_declaration,
21375 change any implicit parameters introduced into packs. */
21376 if (parser->implicit_template_parms
21377 && parameter->declarator
21378 && parameter->declarator->parameter_pack_p)
21380 int latest_template_parm_idx = TREE_VEC_LENGTH
21381 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21383 if (latest_template_parm_idx != template_parm_idx)
21384 parameter->decl_specifiers.type = convert_generic_types_to_packs
21385 (parameter->decl_specifiers.type,
21386 template_parm_idx, latest_template_parm_idx);
21389 decl = grokdeclarator (parameter->declarator,
21390 &parameter->decl_specifiers,
21391 PARM,
21392 parameter->default_argument != NULL_TREE,
21393 &parameter->decl_specifiers.attributes);
21394 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
21395 DECL_SOURCE_LOCATION (decl) = parameter->loc;
21398 deprecated_state = DEPRECATED_NORMAL;
21400 /* If a parse error occurred parsing the parameter declaration,
21401 then the entire parameter-declaration-list is erroneous. */
21402 if (decl == error_mark_node)
21404 *is_error = true;
21405 parameters = error_mark_node;
21406 break;
21409 if (parameter->decl_specifiers.attributes)
21410 cplus_decl_attributes (&decl,
21411 parameter->decl_specifiers.attributes,
21413 if (DECL_NAME (decl))
21414 decl = pushdecl (decl);
21416 if (decl != error_mark_node)
21418 retrofit_lang_decl (decl);
21419 DECL_PARM_INDEX (decl) = ++index;
21420 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21423 /* Add the new parameter to the list. */
21424 *tail = build_tree_list (parameter->default_argument, decl);
21425 tail = &TREE_CHAIN (*tail);
21427 /* Peek at the next token. */
21428 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21429 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21430 /* These are for Objective-C++ */
21431 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21432 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21433 /* The parameter-declaration-list is complete. */
21434 break;
21435 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21437 cp_token *token;
21439 /* Peek at the next token. */
21440 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21441 /* If it's an ellipsis, then the list is complete. */
21442 if (token->type == CPP_ELLIPSIS)
21443 break;
21444 /* Otherwise, there must be more parameters. Consume the
21445 `,'. */
21446 cp_lexer_consume_token (parser->lexer);
21447 /* When parsing something like:
21449 int i(float f, double d)
21451 we can tell after seeing the declaration for "f" that we
21452 are not looking at an initialization of a variable "i",
21453 but rather at the declaration of a function "i".
21455 Due to the fact that the parsing of template arguments
21456 (as specified to a template-id) requires backtracking we
21457 cannot use this technique when inside a template argument
21458 list. */
21459 if (!parser->in_template_argument_list_p
21460 && !parser->in_type_id_in_expr_p
21461 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21462 /* However, a parameter-declaration of the form
21463 "float(f)" (which is a valid declaration of a
21464 parameter "f") can also be interpreted as an
21465 expression (the conversion of "f" to "float"). */
21466 && !parenthesized_p)
21467 cp_parser_commit_to_tentative_parse (parser);
21469 else
21471 cp_parser_error (parser, "expected %<,%> or %<...%>");
21472 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21473 cp_parser_skip_to_closing_parenthesis (parser,
21474 /*recovering=*/true,
21475 /*or_comma=*/false,
21476 /*consume_paren=*/false);
21477 break;
21481 parser->in_unbraced_linkage_specification_p
21482 = saved_in_unbraced_linkage_specification_p;
21484 /* Reset implicit_template_scope if we are about to leave the function
21485 parameter list that introduced it. Note that for out-of-line member
21486 definitions, there will be one or more class scopes before we get to
21487 the template parameter scope. */
21489 if (cp_binding_level *its = parser->implicit_template_scope)
21490 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21492 while (maybe_its->kind == sk_class)
21493 maybe_its = maybe_its->level_chain;
21494 if (maybe_its == its)
21496 parser->implicit_template_parms = 0;
21497 parser->implicit_template_scope = 0;
21501 return parameters;
21504 /* Parse a parameter declaration.
21506 parameter-declaration:
21507 decl-specifier-seq ... [opt] declarator
21508 decl-specifier-seq declarator = assignment-expression
21509 decl-specifier-seq ... [opt] abstract-declarator [opt]
21510 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21512 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21513 declares a template parameter. (In that case, a non-nested `>'
21514 token encountered during the parsing of the assignment-expression
21515 is not interpreted as a greater-than operator.)
21517 Returns a representation of the parameter, or NULL if an error
21518 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21519 true iff the declarator is of the form "(p)". */
21521 static cp_parameter_declarator *
21522 cp_parser_parameter_declaration (cp_parser *parser,
21523 bool template_parm_p,
21524 bool *parenthesized_p)
21526 int declares_class_or_enum;
21527 cp_decl_specifier_seq decl_specifiers;
21528 cp_declarator *declarator;
21529 tree default_argument;
21530 cp_token *token = NULL, *declarator_token_start = NULL;
21531 const char *saved_message;
21532 bool template_parameter_pack_p = false;
21534 /* In a template parameter, `>' is not an operator.
21536 [temp.param]
21538 When parsing a default template-argument for a non-type
21539 template-parameter, the first non-nested `>' is taken as the end
21540 of the template parameter-list rather than a greater-than
21541 operator. */
21543 /* Type definitions may not appear in parameter types. */
21544 saved_message = parser->type_definition_forbidden_message;
21545 parser->type_definition_forbidden_message
21546 = G_("types may not be defined in parameter types");
21548 /* Parse the declaration-specifiers. */
21549 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21550 cp_parser_decl_specifier_seq (parser,
21551 CP_PARSER_FLAGS_NONE,
21552 &decl_specifiers,
21553 &declares_class_or_enum);
21555 /* Complain about missing 'typename' or other invalid type names. */
21556 if (!decl_specifiers.any_type_specifiers_p
21557 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21558 decl_specifiers.type = error_mark_node;
21560 /* If an error occurred, there's no reason to attempt to parse the
21561 rest of the declaration. */
21562 if (cp_parser_error_occurred (parser))
21564 parser->type_definition_forbidden_message = saved_message;
21565 return NULL;
21568 /* Peek at the next token. */
21569 token = cp_lexer_peek_token (parser->lexer);
21571 /* If the next token is a `)', `,', `=', `>', or `...', then there
21572 is no declarator. However, when variadic templates are enabled,
21573 there may be a declarator following `...'. */
21574 if (token->type == CPP_CLOSE_PAREN
21575 || token->type == CPP_COMMA
21576 || token->type == CPP_EQ
21577 || token->type == CPP_GREATER)
21579 declarator = NULL;
21580 if (parenthesized_p)
21581 *parenthesized_p = false;
21583 /* Otherwise, there should be a declarator. */
21584 else
21586 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21587 parser->default_arg_ok_p = false;
21589 /* After seeing a decl-specifier-seq, if the next token is not a
21590 "(", there is no possibility that the code is a valid
21591 expression. Therefore, if parsing tentatively, we commit at
21592 this point. */
21593 if (!parser->in_template_argument_list_p
21594 /* In an expression context, having seen:
21596 (int((char ...
21598 we cannot be sure whether we are looking at a
21599 function-type (taking a "char" as a parameter) or a cast
21600 of some object of type "char" to "int". */
21601 && !parser->in_type_id_in_expr_p
21602 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21603 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21604 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21605 cp_parser_commit_to_tentative_parse (parser);
21606 /* Parse the declarator. */
21607 declarator_token_start = token;
21608 declarator = cp_parser_declarator (parser,
21609 CP_PARSER_DECLARATOR_EITHER,
21610 /*ctor_dtor_or_conv_p=*/NULL,
21611 parenthesized_p,
21612 /*member_p=*/false,
21613 /*friend_p=*/false);
21614 parser->default_arg_ok_p = saved_default_arg_ok_p;
21615 /* After the declarator, allow more attributes. */
21616 decl_specifiers.attributes
21617 = chainon (decl_specifiers.attributes,
21618 cp_parser_attributes_opt (parser));
21620 /* If the declarator is a template parameter pack, remember that and
21621 clear the flag in the declarator itself so we don't get errors
21622 from grokdeclarator. */
21623 if (template_parm_p && declarator && declarator->parameter_pack_p)
21625 declarator->parameter_pack_p = false;
21626 template_parameter_pack_p = true;
21630 /* If the next token is an ellipsis, and we have not seen a declarator
21631 name, and if either the type of the declarator contains parameter
21632 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21633 for, eg, abbreviated integral type names), then we actually have a
21634 parameter pack expansion expression. Otherwise, leave the ellipsis
21635 for a C-style variadic function. */
21636 token = cp_lexer_peek_token (parser->lexer);
21637 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21639 tree type = decl_specifiers.type;
21641 if (type && DECL_P (type))
21642 type = TREE_TYPE (type);
21644 if (((type
21645 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21646 && (template_parm_p || uses_parameter_packs (type)))
21647 || (!type && template_parm_p))
21648 && declarator_can_be_parameter_pack (declarator))
21650 /* Consume the `...'. */
21651 cp_lexer_consume_token (parser->lexer);
21652 maybe_warn_variadic_templates ();
21654 /* Build a pack expansion type */
21655 if (template_parm_p)
21656 template_parameter_pack_p = true;
21657 else if (declarator)
21658 declarator->parameter_pack_p = true;
21659 else
21660 decl_specifiers.type = make_pack_expansion (type);
21664 /* The restriction on defining new types applies only to the type
21665 of the parameter, not to the default argument. */
21666 parser->type_definition_forbidden_message = saved_message;
21668 /* If the next token is `=', then process a default argument. */
21669 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21671 tree type = decl_specifiers.type;
21672 token = cp_lexer_peek_token (parser->lexer);
21673 /* If we are defining a class, then the tokens that make up the
21674 default argument must be saved and processed later. */
21675 if (!template_parm_p && at_class_scope_p ()
21676 && TYPE_BEING_DEFINED (current_class_type)
21677 && !LAMBDA_TYPE_P (current_class_type))
21678 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21680 // A constrained-type-specifier may declare a type template-parameter.
21681 else if (declares_constrained_type_template_parameter (type))
21682 default_argument
21683 = cp_parser_default_type_template_argument (parser);
21685 // A constrained-type-specifier may declare a template-template-parameter.
21686 else if (declares_constrained_template_template_parameter (type))
21687 default_argument
21688 = cp_parser_default_template_template_argument (parser);
21690 /* Outside of a class definition, we can just parse the
21691 assignment-expression. */
21692 else
21693 default_argument
21694 = cp_parser_default_argument (parser, template_parm_p);
21696 if (!parser->default_arg_ok_p)
21698 permerror (token->location,
21699 "default arguments are only "
21700 "permitted for function parameters");
21702 else if ((declarator && declarator->parameter_pack_p)
21703 || template_parameter_pack_p
21704 || (decl_specifiers.type
21705 && PACK_EXPANSION_P (decl_specifiers.type)))
21707 /* Find the name of the parameter pack. */
21708 cp_declarator *id_declarator = declarator;
21709 while (id_declarator && id_declarator->kind != cdk_id)
21710 id_declarator = id_declarator->declarator;
21712 if (id_declarator && id_declarator->kind == cdk_id)
21713 error_at (declarator_token_start->location,
21714 template_parm_p
21715 ? G_("template parameter pack %qD "
21716 "cannot have a default argument")
21717 : G_("parameter pack %qD cannot have "
21718 "a default argument"),
21719 id_declarator->u.id.unqualified_name);
21720 else
21721 error_at (declarator_token_start->location,
21722 template_parm_p
21723 ? G_("template parameter pack cannot have "
21724 "a default argument")
21725 : G_("parameter pack cannot have a "
21726 "default argument"));
21728 default_argument = NULL_TREE;
21731 else
21732 default_argument = NULL_TREE;
21734 /* Generate a location for the parameter, ranging from the start of the
21735 initial token to the end of the final token (using input_location for
21736 the latter, set up by cp_lexer_set_source_position_from_token when
21737 consuming tokens).
21739 If we have a identifier, then use it for the caret location, e.g.
21741 extern int callee (int one, int (*two)(int, int), float three);
21742 ~~~~~~^~~~~~~~~~~~~~
21744 otherwise, reuse the start location for the caret location e.g.:
21746 extern int callee (int one, int (*)(int, int), float three);
21747 ^~~~~~~~~~~~~~~~~
21750 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
21751 ? declarator->id_loc
21752 : decl_spec_token_start->location);
21753 location_t param_loc = make_location (caret_loc,
21754 decl_spec_token_start->location,
21755 input_location);
21757 return make_parameter_declarator (&decl_specifiers,
21758 declarator,
21759 default_argument,
21760 param_loc,
21761 template_parameter_pack_p);
21764 /* Parse a default argument and return it.
21766 TEMPLATE_PARM_P is true if this is a default argument for a
21767 non-type template parameter. */
21768 static tree
21769 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21771 tree default_argument = NULL_TREE;
21772 bool saved_greater_than_is_operator_p;
21773 bool saved_local_variables_forbidden_p;
21774 bool non_constant_p, is_direct_init;
21776 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21777 set correctly. */
21778 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21779 parser->greater_than_is_operator_p = !template_parm_p;
21780 /* Local variable names (and the `this' keyword) may not
21781 appear in a default argument. */
21782 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21783 parser->local_variables_forbidden_p = true;
21784 /* Parse the assignment-expression. */
21785 if (template_parm_p)
21786 push_deferring_access_checks (dk_no_deferred);
21787 tree saved_class_ptr = NULL_TREE;
21788 tree saved_class_ref = NULL_TREE;
21789 /* The "this" pointer is not valid in a default argument. */
21790 if (cfun)
21792 saved_class_ptr = current_class_ptr;
21793 cp_function_chain->x_current_class_ptr = NULL_TREE;
21794 saved_class_ref = current_class_ref;
21795 cp_function_chain->x_current_class_ref = NULL_TREE;
21797 default_argument
21798 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21799 /* Restore the "this" pointer. */
21800 if (cfun)
21802 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21803 cp_function_chain->x_current_class_ref = saved_class_ref;
21805 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21806 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21807 if (template_parm_p)
21808 pop_deferring_access_checks ();
21809 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21810 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21812 return default_argument;
21815 /* Parse a function-body.
21817 function-body:
21818 compound_statement */
21820 static void
21821 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21823 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21824 ? BCS_TRY_BLOCK : BCS_NORMAL),
21825 true);
21828 /* Parse a ctor-initializer-opt followed by a function-body. Return
21829 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21830 is true we are parsing a function-try-block. */
21832 static void
21833 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21834 bool in_function_try_block)
21836 tree body, list;
21837 const bool check_body_p =
21838 DECL_CONSTRUCTOR_P (current_function_decl)
21839 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21840 tree last = NULL;
21842 /* Begin the function body. */
21843 body = begin_function_body ();
21844 /* Parse the optional ctor-initializer. */
21845 cp_parser_ctor_initializer_opt (parser);
21847 /* If we're parsing a constexpr constructor definition, we need
21848 to check that the constructor body is indeed empty. However,
21849 before we get to cp_parser_function_body lot of junk has been
21850 generated, so we can't just check that we have an empty block.
21851 Rather we take a snapshot of the outermost block, and check whether
21852 cp_parser_function_body changed its state. */
21853 if (check_body_p)
21855 list = cur_stmt_list;
21856 if (STATEMENT_LIST_TAIL (list))
21857 last = STATEMENT_LIST_TAIL (list)->stmt;
21859 /* Parse the function-body. */
21860 cp_parser_function_body (parser, in_function_try_block);
21861 if (check_body_p)
21862 check_constexpr_ctor_body (last, list, /*complain=*/true);
21863 /* Finish the function body. */
21864 finish_function_body (body);
21867 /* Parse an initializer.
21869 initializer:
21870 = initializer-clause
21871 ( expression-list )
21873 Returns an expression representing the initializer. If no
21874 initializer is present, NULL_TREE is returned.
21876 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21877 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21878 set to TRUE if there is no initializer present. If there is an
21879 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21880 is set to true; otherwise it is set to false. */
21882 static tree
21883 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21884 bool* non_constant_p)
21886 cp_token *token;
21887 tree init;
21889 /* Peek at the next token. */
21890 token = cp_lexer_peek_token (parser->lexer);
21892 /* Let our caller know whether or not this initializer was
21893 parenthesized. */
21894 *is_direct_init = (token->type != CPP_EQ);
21895 /* Assume that the initializer is constant. */
21896 *non_constant_p = false;
21898 if (token->type == CPP_EQ)
21900 /* Consume the `='. */
21901 cp_lexer_consume_token (parser->lexer);
21902 /* Parse the initializer-clause. */
21903 init = cp_parser_initializer_clause (parser, non_constant_p);
21905 else if (token->type == CPP_OPEN_PAREN)
21907 vec<tree, va_gc> *vec;
21908 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21909 /*cast_p=*/false,
21910 /*allow_expansion_p=*/true,
21911 non_constant_p);
21912 if (vec == NULL)
21913 return error_mark_node;
21914 init = build_tree_list_vec (vec);
21915 release_tree_vector (vec);
21917 else if (token->type == CPP_OPEN_BRACE)
21919 cp_lexer_set_source_position (parser->lexer);
21920 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21921 init = cp_parser_braced_list (parser, non_constant_p);
21922 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21924 else
21926 /* Anything else is an error. */
21927 cp_parser_error (parser, "expected initializer");
21928 init = error_mark_node;
21931 if (check_for_bare_parameter_packs (init))
21932 init = error_mark_node;
21934 return init;
21937 /* Parse an initializer-clause.
21939 initializer-clause:
21940 assignment-expression
21941 braced-init-list
21943 Returns an expression representing the initializer.
21945 If the `assignment-expression' production is used the value
21946 returned is simply a representation for the expression.
21948 Otherwise, calls cp_parser_braced_list. */
21950 static cp_expr
21951 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21953 cp_expr initializer;
21955 /* Assume the expression is constant. */
21956 *non_constant_p = false;
21958 /* If it is not a `{', then we are looking at an
21959 assignment-expression. */
21960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21962 initializer
21963 = cp_parser_constant_expression (parser,
21964 /*allow_non_constant_p=*/true,
21965 non_constant_p);
21967 else
21968 initializer = cp_parser_braced_list (parser, non_constant_p);
21970 return initializer;
21973 /* Parse a brace-enclosed initializer list.
21975 braced-init-list:
21976 { initializer-list , [opt] }
21979 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21980 the elements of the initializer-list (or NULL, if the last
21981 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21982 NULL_TREE. There is no way to detect whether or not the optional
21983 trailing `,' was provided. NON_CONSTANT_P is as for
21984 cp_parser_initializer. */
21986 static cp_expr
21987 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21989 tree initializer;
21990 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21992 /* Consume the `{' token. */
21993 matching_braces braces;
21994 braces.consume_open (parser);
21995 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21996 initializer = make_node (CONSTRUCTOR);
21997 /* If it's not a `}', then there is a non-trivial initializer. */
21998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
22000 /* Parse the initializer list. */
22001 CONSTRUCTOR_ELTS (initializer)
22002 = cp_parser_initializer_list (parser, non_constant_p);
22003 /* A trailing `,' token is allowed. */
22004 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22005 cp_lexer_consume_token (parser->lexer);
22007 else
22008 *non_constant_p = false;
22009 /* Now, there should be a trailing `}'. */
22010 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
22011 braces.require_close (parser);
22012 TREE_TYPE (initializer) = init_list_type_node;
22014 cp_expr result (initializer);
22015 /* Build a location of the form:
22016 { ... }
22017 ^~~~~~~
22018 with caret==start at the open brace, finish at the close brace. */
22019 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
22020 result.set_location (combined_loc);
22021 return result;
22024 /* Consume tokens up to, and including, the next non-nested closing `]'.
22025 Returns true iff we found a closing `]'. */
22027 static bool
22028 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
22030 unsigned square_depth = 0;
22032 while (true)
22034 cp_token * token = cp_lexer_peek_token (parser->lexer);
22036 switch (token->type)
22038 case CPP_EOF:
22039 case CPP_PRAGMA_EOL:
22040 /* If we've run out of tokens, then there is no closing `]'. */
22041 return false;
22043 case CPP_OPEN_SQUARE:
22044 ++square_depth;
22045 break;
22047 case CPP_CLOSE_SQUARE:
22048 if (!square_depth--)
22050 cp_lexer_consume_token (parser->lexer);
22051 return true;
22053 break;
22055 default:
22056 break;
22059 /* Consume the token. */
22060 cp_lexer_consume_token (parser->lexer);
22064 /* Return true if we are looking at an array-designator, false otherwise. */
22066 static bool
22067 cp_parser_array_designator_p (cp_parser *parser)
22069 /* Consume the `['. */
22070 cp_lexer_consume_token (parser->lexer);
22072 cp_lexer_save_tokens (parser->lexer);
22074 /* Skip tokens until the next token is a closing square bracket.
22075 If we find the closing `]', and the next token is a `=', then
22076 we are looking at an array designator. */
22077 bool array_designator_p
22078 = (cp_parser_skip_to_closing_square_bracket (parser)
22079 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
22081 /* Roll back the tokens we skipped. */
22082 cp_lexer_rollback_tokens (parser->lexer);
22084 return array_designator_p;
22087 /* Parse an initializer-list.
22089 initializer-list:
22090 initializer-clause ... [opt]
22091 initializer-list , initializer-clause ... [opt]
22093 GNU Extension:
22095 initializer-list:
22096 designation initializer-clause ...[opt]
22097 initializer-list , designation initializer-clause ...[opt]
22099 designation:
22100 . identifier =
22101 identifier :
22102 [ constant-expression ] =
22104 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22105 for the initializer. If the INDEX of the elt is non-NULL, it is the
22106 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22107 as for cp_parser_initializer. */
22109 static vec<constructor_elt, va_gc> *
22110 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22112 vec<constructor_elt, va_gc> *v = NULL;
22114 /* Assume all of the expressions are constant. */
22115 *non_constant_p = false;
22117 /* Parse the rest of the list. */
22118 while (true)
22120 cp_token *token;
22121 tree designator;
22122 tree initializer;
22123 bool clause_non_constant_p;
22125 /* If the next token is an identifier and the following one is a
22126 colon, we are looking at the GNU designated-initializer
22127 syntax. */
22128 if (cp_parser_allow_gnu_extensions_p (parser)
22129 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22130 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
22132 /* Warn the user that they are using an extension. */
22133 pedwarn (input_location, OPT_Wpedantic,
22134 "ISO C++ does not allow designated initializers");
22135 /* Consume the identifier. */
22136 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22137 /* Consume the `:'. */
22138 cp_lexer_consume_token (parser->lexer);
22140 /* Also handle the C99 syntax, '. id ='. */
22141 else if (cp_parser_allow_gnu_extensions_p (parser)
22142 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22143 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22144 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
22146 /* Warn the user that they are using an extension. */
22147 pedwarn (input_location, OPT_Wpedantic,
22148 "ISO C++ does not allow C99 designated initializers");
22149 /* Consume the `.'. */
22150 cp_lexer_consume_token (parser->lexer);
22151 /* Consume the identifier. */
22152 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22153 /* Consume the `='. */
22154 cp_lexer_consume_token (parser->lexer);
22156 /* Also handle C99 array designators, '[ const ] ='. */
22157 else if (cp_parser_allow_gnu_extensions_p (parser)
22158 && !c_dialect_objc ()
22159 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22161 /* In C++11, [ could start a lambda-introducer. */
22162 bool non_const = false;
22164 cp_parser_parse_tentatively (parser);
22166 if (!cp_parser_array_designator_p (parser))
22168 cp_parser_simulate_error (parser);
22169 designator = NULL_TREE;
22171 else
22173 designator = cp_parser_constant_expression (parser, true,
22174 &non_const);
22175 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22176 cp_parser_require (parser, CPP_EQ, RT_EQ);
22179 if (!cp_parser_parse_definitely (parser))
22180 designator = NULL_TREE;
22181 else if (non_const)
22182 require_potential_rvalue_constant_expression (designator);
22184 else
22185 designator = NULL_TREE;
22187 /* Parse the initializer. */
22188 initializer = cp_parser_initializer_clause (parser,
22189 &clause_non_constant_p);
22190 /* If any clause is non-constant, so is the entire initializer. */
22191 if (clause_non_constant_p)
22192 *non_constant_p = true;
22194 /* If we have an ellipsis, this is an initializer pack
22195 expansion. */
22196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22198 /* Consume the `...'. */
22199 cp_lexer_consume_token (parser->lexer);
22201 /* Turn the initializer into an initializer expansion. */
22202 initializer = make_pack_expansion (initializer);
22205 /* Add it to the vector. */
22206 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22208 /* If the next token is not a comma, we have reached the end of
22209 the list. */
22210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22211 break;
22213 /* Peek at the next token. */
22214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22215 /* If the next token is a `}', then we're still done. An
22216 initializer-clause can have a trailing `,' after the
22217 initializer-list and before the closing `}'. */
22218 if (token->type == CPP_CLOSE_BRACE)
22219 break;
22221 /* Consume the `,' token. */
22222 cp_lexer_consume_token (parser->lexer);
22225 return v;
22228 /* Classes [gram.class] */
22230 /* Parse a class-name.
22232 class-name:
22233 identifier
22234 template-id
22236 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22237 to indicate that names looked up in dependent types should be
22238 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22239 keyword has been used to indicate that the name that appears next
22240 is a template. TAG_TYPE indicates the explicit tag given before
22241 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22242 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22243 is the class being defined in a class-head. If ENUM_OK is TRUE,
22244 enum-names are also accepted.
22246 Returns the TYPE_DECL representing the class. */
22248 static tree
22249 cp_parser_class_name (cp_parser *parser,
22250 bool typename_keyword_p,
22251 bool template_keyword_p,
22252 enum tag_types tag_type,
22253 bool check_dependency_p,
22254 bool class_head_p,
22255 bool is_declaration,
22256 bool enum_ok)
22258 tree decl;
22259 tree scope;
22260 bool typename_p;
22261 cp_token *token;
22262 tree identifier = NULL_TREE;
22264 /* All class-names start with an identifier. */
22265 token = cp_lexer_peek_token (parser->lexer);
22266 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22268 cp_parser_error (parser, "expected class-name");
22269 return error_mark_node;
22272 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22273 to a template-id, so we save it here. */
22274 scope = parser->scope;
22275 if (scope == error_mark_node)
22276 return error_mark_node;
22278 /* Any name names a type if we're following the `typename' keyword
22279 in a qualified name where the enclosing scope is type-dependent. */
22280 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22281 && dependent_type_p (scope));
22282 /* Handle the common case (an identifier, but not a template-id)
22283 efficiently. */
22284 if (token->type == CPP_NAME
22285 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22287 cp_token *identifier_token;
22288 bool ambiguous_p;
22290 /* Look for the identifier. */
22291 identifier_token = cp_lexer_peek_token (parser->lexer);
22292 ambiguous_p = identifier_token->error_reported;
22293 identifier = cp_parser_identifier (parser);
22294 /* If the next token isn't an identifier, we are certainly not
22295 looking at a class-name. */
22296 if (identifier == error_mark_node)
22297 decl = error_mark_node;
22298 /* If we know this is a type-name, there's no need to look it
22299 up. */
22300 else if (typename_p)
22301 decl = identifier;
22302 else
22304 tree ambiguous_decls;
22305 /* If we already know that this lookup is ambiguous, then
22306 we've already issued an error message; there's no reason
22307 to check again. */
22308 if (ambiguous_p)
22310 cp_parser_simulate_error (parser);
22311 return error_mark_node;
22313 /* If the next token is a `::', then the name must be a type
22314 name.
22316 [basic.lookup.qual]
22318 During the lookup for a name preceding the :: scope
22319 resolution operator, object, function, and enumerator
22320 names are ignored. */
22321 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22322 tag_type = scope_type;
22323 /* Look up the name. */
22324 decl = cp_parser_lookup_name (parser, identifier,
22325 tag_type,
22326 /*is_template=*/false,
22327 /*is_namespace=*/false,
22328 check_dependency_p,
22329 &ambiguous_decls,
22330 identifier_token->location);
22331 if (ambiguous_decls)
22333 if (cp_parser_parsing_tentatively (parser))
22334 cp_parser_simulate_error (parser);
22335 return error_mark_node;
22339 else
22341 /* Try a template-id. */
22342 decl = cp_parser_template_id (parser, template_keyword_p,
22343 check_dependency_p,
22344 tag_type,
22345 is_declaration);
22346 if (decl == error_mark_node)
22347 return error_mark_node;
22350 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22352 /* If this is a typename, create a TYPENAME_TYPE. */
22353 if (typename_p && decl != error_mark_node)
22355 decl = make_typename_type (scope, decl, typename_type,
22356 /*complain=*/tf_error);
22357 if (decl != error_mark_node)
22358 decl = TYPE_NAME (decl);
22361 decl = strip_using_decl (decl);
22363 /* Check to see that it is really the name of a class. */
22364 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22365 && identifier_p (TREE_OPERAND (decl, 0))
22366 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22367 /* Situations like this:
22369 template <typename T> struct A {
22370 typename T::template X<int>::I i;
22373 are problematic. Is `T::template X<int>' a class-name? The
22374 standard does not seem to be definitive, but there is no other
22375 valid interpretation of the following `::'. Therefore, those
22376 names are considered class-names. */
22378 decl = make_typename_type (scope, decl, tag_type, tf_error);
22379 if (decl != error_mark_node)
22380 decl = TYPE_NAME (decl);
22382 else if (TREE_CODE (decl) != TYPE_DECL
22383 || TREE_TYPE (decl) == error_mark_node
22384 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22385 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22386 /* In Objective-C 2.0, a classname followed by '.' starts a
22387 dot-syntax expression, and it's not a type-name. */
22388 || (c_dialect_objc ()
22389 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22390 && objc_is_class_name (decl)))
22391 decl = error_mark_node;
22393 if (decl == error_mark_node)
22394 cp_parser_error (parser, "expected class-name");
22395 else if (identifier && !parser->scope)
22396 maybe_note_name_used_in_class (identifier, decl);
22398 return decl;
22401 /* Parse a class-specifier.
22403 class-specifier:
22404 class-head { member-specification [opt] }
22406 Returns the TREE_TYPE representing the class. */
22408 static tree
22409 cp_parser_class_specifier_1 (cp_parser* parser)
22411 tree type;
22412 tree attributes = NULL_TREE;
22413 bool nested_name_specifier_p;
22414 unsigned saved_num_template_parameter_lists;
22415 bool saved_in_function_body;
22416 unsigned char in_statement;
22417 bool in_switch_statement_p;
22418 bool saved_in_unbraced_linkage_specification_p;
22419 tree old_scope = NULL_TREE;
22420 tree scope = NULL_TREE;
22421 cp_token *closing_brace;
22423 push_deferring_access_checks (dk_no_deferred);
22425 /* Parse the class-head. */
22426 type = cp_parser_class_head (parser,
22427 &nested_name_specifier_p);
22428 /* If the class-head was a semantic disaster, skip the entire body
22429 of the class. */
22430 if (!type)
22432 cp_parser_skip_to_end_of_block_or_statement (parser);
22433 pop_deferring_access_checks ();
22434 return error_mark_node;
22437 /* Look for the `{'. */
22438 matching_braces braces;
22439 if (!braces.require_open (parser))
22441 pop_deferring_access_checks ();
22442 return error_mark_node;
22445 cp_ensure_no_omp_declare_simd (parser);
22446 cp_ensure_no_oacc_routine (parser);
22448 /* Issue an error message if type-definitions are forbidden here. */
22449 cp_parser_check_type_definition (parser);
22450 /* Remember that we are defining one more class. */
22451 ++parser->num_classes_being_defined;
22452 /* Inside the class, surrounding template-parameter-lists do not
22453 apply. */
22454 saved_num_template_parameter_lists
22455 = parser->num_template_parameter_lists;
22456 parser->num_template_parameter_lists = 0;
22457 /* We are not in a function body. */
22458 saved_in_function_body = parser->in_function_body;
22459 parser->in_function_body = false;
22460 /* Or in a loop. */
22461 in_statement = parser->in_statement;
22462 parser->in_statement = 0;
22463 /* Or in a switch. */
22464 in_switch_statement_p = parser->in_switch_statement_p;
22465 parser->in_switch_statement_p = false;
22466 /* We are not immediately inside an extern "lang" block. */
22467 saved_in_unbraced_linkage_specification_p
22468 = parser->in_unbraced_linkage_specification_p;
22469 parser->in_unbraced_linkage_specification_p = false;
22471 // Associate constraints with the type.
22472 if (flag_concepts)
22473 type = associate_classtype_constraints (type);
22475 /* Start the class. */
22476 if (nested_name_specifier_p)
22478 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22479 old_scope = push_inner_scope (scope);
22481 type = begin_class_definition (type);
22483 if (type == error_mark_node)
22484 /* If the type is erroneous, skip the entire body of the class. */
22485 cp_parser_skip_to_closing_brace (parser);
22486 else
22487 /* Parse the member-specification. */
22488 cp_parser_member_specification_opt (parser);
22490 /* Look for the trailing `}'. */
22491 closing_brace = braces.require_close (parser);
22492 /* Look for trailing attributes to apply to this class. */
22493 if (cp_parser_allow_gnu_extensions_p (parser))
22494 attributes = cp_parser_gnu_attributes_opt (parser);
22495 if (type != error_mark_node)
22496 type = finish_struct (type, attributes);
22497 if (nested_name_specifier_p)
22498 pop_inner_scope (old_scope, scope);
22500 /* We've finished a type definition. Check for the common syntax
22501 error of forgetting a semicolon after the definition. We need to
22502 be careful, as we can't just check for not-a-semicolon and be done
22503 with it; the user might have typed:
22505 class X { } c = ...;
22506 class X { } *p = ...;
22508 and so forth. Instead, enumerate all the possible tokens that
22509 might follow this production; if we don't see one of them, then
22510 complain and silently insert the semicolon. */
22512 cp_token *token = cp_lexer_peek_token (parser->lexer);
22513 bool want_semicolon = true;
22515 if (cp_next_tokens_can_be_std_attribute_p (parser))
22516 /* Don't try to parse c++11 attributes here. As per the
22517 grammar, that should be a task for
22518 cp_parser_decl_specifier_seq. */
22519 want_semicolon = false;
22521 switch (token->type)
22523 case CPP_NAME:
22524 case CPP_SEMICOLON:
22525 case CPP_MULT:
22526 case CPP_AND:
22527 case CPP_OPEN_PAREN:
22528 case CPP_CLOSE_PAREN:
22529 case CPP_COMMA:
22530 want_semicolon = false;
22531 break;
22533 /* While it's legal for type qualifiers and storage class
22534 specifiers to follow type definitions in the grammar, only
22535 compiler testsuites contain code like that. Assume that if
22536 we see such code, then what we're really seeing is a case
22537 like:
22539 class X { }
22540 const <type> var = ...;
22544 class Y { }
22545 static <type> func (...) ...
22547 i.e. the qualifier or specifier applies to the next
22548 declaration. To do so, however, we need to look ahead one
22549 more token to see if *that* token is a type specifier.
22551 This code could be improved to handle:
22553 class Z { }
22554 static const <type> var = ...; */
22555 case CPP_KEYWORD:
22556 if (keyword_is_decl_specifier (token->keyword))
22558 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22560 /* Handling user-defined types here would be nice, but very
22561 tricky. */
22562 want_semicolon
22563 = (lookahead->type == CPP_KEYWORD
22564 && keyword_begins_type_specifier (lookahead->keyword));
22566 break;
22567 default:
22568 break;
22571 /* If we don't have a type, then something is very wrong and we
22572 shouldn't try to do anything clever. Likewise for not seeing the
22573 closing brace. */
22574 if (closing_brace && TYPE_P (type) && want_semicolon)
22576 /* Locate the closing brace. */
22577 cp_token_position prev
22578 = cp_lexer_previous_token_position (parser->lexer);
22579 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22580 location_t loc = prev_token->location;
22582 /* We want to suggest insertion of a ';' immediately *after* the
22583 closing brace, so, if we can, offset the location by 1 column. */
22584 location_t next_loc = loc;
22585 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22586 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22588 rich_location richloc (line_table, next_loc);
22590 /* If we successfully offset the location, suggest the fix-it. */
22591 if (next_loc != loc)
22592 richloc.add_fixit_insert_before (next_loc, ";");
22594 if (CLASSTYPE_DECLARED_CLASS (type))
22595 error_at_rich_loc (&richloc,
22596 "expected %<;%> after class definition");
22597 else if (TREE_CODE (type) == RECORD_TYPE)
22598 error_at_rich_loc (&richloc,
22599 "expected %<;%> after struct definition");
22600 else if (TREE_CODE (type) == UNION_TYPE)
22601 error_at_rich_loc (&richloc,
22602 "expected %<;%> after union definition");
22603 else
22604 gcc_unreachable ();
22606 /* Unget one token and smash it to look as though we encountered
22607 a semicolon in the input stream. */
22608 cp_lexer_set_token_position (parser->lexer, prev);
22609 token = cp_lexer_peek_token (parser->lexer);
22610 token->type = CPP_SEMICOLON;
22611 token->keyword = RID_MAX;
22615 /* If this class is not itself within the scope of another class,
22616 then we need to parse the bodies of all of the queued function
22617 definitions. Note that the queued functions defined in a class
22618 are not always processed immediately following the
22619 class-specifier for that class. Consider:
22621 struct A {
22622 struct B { void f() { sizeof (A); } };
22625 If `f' were processed before the processing of `A' were
22626 completed, there would be no way to compute the size of `A'.
22627 Note that the nesting we are interested in here is lexical --
22628 not the semantic nesting given by TYPE_CONTEXT. In particular,
22629 for:
22631 struct A { struct B; };
22632 struct A::B { void f() { } };
22634 there is no need to delay the parsing of `A::B::f'. */
22635 if (--parser->num_classes_being_defined == 0)
22637 tree decl;
22638 tree class_type = NULL_TREE;
22639 tree pushed_scope = NULL_TREE;
22640 unsigned ix;
22641 cp_default_arg_entry *e;
22642 tree save_ccp, save_ccr;
22644 /* In a first pass, parse default arguments to the functions.
22645 Then, in a second pass, parse the bodies of the functions.
22646 This two-phased approach handles cases like:
22648 struct S {
22649 void f() { g(); }
22650 void g(int i = 3);
22654 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22656 decl = e->decl;
22657 /* If there are default arguments that have not yet been processed,
22658 take care of them now. */
22659 if (class_type != e->class_type)
22661 if (pushed_scope)
22662 pop_scope (pushed_scope);
22663 class_type = e->class_type;
22664 pushed_scope = push_scope (class_type);
22666 /* Make sure that any template parameters are in scope. */
22667 maybe_begin_member_template_processing (decl);
22668 /* Parse the default argument expressions. */
22669 cp_parser_late_parsing_default_args (parser, decl);
22670 /* Remove any template parameters from the symbol table. */
22671 maybe_end_member_template_processing ();
22673 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22674 /* Now parse any NSDMIs. */
22675 save_ccp = current_class_ptr;
22676 save_ccr = current_class_ref;
22677 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22679 if (class_type != DECL_CONTEXT (decl))
22681 if (pushed_scope)
22682 pop_scope (pushed_scope);
22683 class_type = DECL_CONTEXT (decl);
22684 pushed_scope = push_scope (class_type);
22686 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22687 cp_parser_late_parsing_nsdmi (parser, decl);
22689 vec_safe_truncate (unparsed_nsdmis, 0);
22690 current_class_ptr = save_ccp;
22691 current_class_ref = save_ccr;
22692 if (pushed_scope)
22693 pop_scope (pushed_scope);
22695 /* Now do some post-NSDMI bookkeeping. */
22696 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22697 after_nsdmi_defaulted_late_checks (class_type);
22698 vec_safe_truncate (unparsed_classes, 0);
22699 after_nsdmi_defaulted_late_checks (type);
22701 /* Now parse the body of the functions. */
22702 if (flag_openmp)
22704 /* OpenMP UDRs need to be parsed before all other functions. */
22705 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22706 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22707 cp_parser_late_parsing_for_member (parser, decl);
22708 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22709 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22710 cp_parser_late_parsing_for_member (parser, decl);
22712 else
22713 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22714 cp_parser_late_parsing_for_member (parser, decl);
22715 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22717 else
22718 vec_safe_push (unparsed_classes, type);
22720 /* Put back any saved access checks. */
22721 pop_deferring_access_checks ();
22723 /* Restore saved state. */
22724 parser->in_switch_statement_p = in_switch_statement_p;
22725 parser->in_statement = in_statement;
22726 parser->in_function_body = saved_in_function_body;
22727 parser->num_template_parameter_lists
22728 = saved_num_template_parameter_lists;
22729 parser->in_unbraced_linkage_specification_p
22730 = saved_in_unbraced_linkage_specification_p;
22732 return type;
22735 static tree
22736 cp_parser_class_specifier (cp_parser* parser)
22738 tree ret;
22739 timevar_push (TV_PARSE_STRUCT);
22740 ret = cp_parser_class_specifier_1 (parser);
22741 timevar_pop (TV_PARSE_STRUCT);
22742 return ret;
22745 /* Parse a class-head.
22747 class-head:
22748 class-key identifier [opt] base-clause [opt]
22749 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22750 class-key nested-name-specifier [opt] template-id
22751 base-clause [opt]
22753 class-virt-specifier:
22754 final
22756 GNU Extensions:
22757 class-key attributes identifier [opt] base-clause [opt]
22758 class-key attributes nested-name-specifier identifier base-clause [opt]
22759 class-key attributes nested-name-specifier [opt] template-id
22760 base-clause [opt]
22762 Upon return BASES is initialized to the list of base classes (or
22763 NULL, if there are none) in the same form returned by
22764 cp_parser_base_clause.
22766 Returns the TYPE of the indicated class. Sets
22767 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22768 involving a nested-name-specifier was used, and FALSE otherwise.
22770 Returns error_mark_node if this is not a class-head.
22772 Returns NULL_TREE if the class-head is syntactically valid, but
22773 semantically invalid in a way that means we should skip the entire
22774 body of the class. */
22776 static tree
22777 cp_parser_class_head (cp_parser* parser,
22778 bool* nested_name_specifier_p)
22780 tree nested_name_specifier;
22781 enum tag_types class_key;
22782 tree id = NULL_TREE;
22783 tree type = NULL_TREE;
22784 tree attributes;
22785 tree bases;
22786 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22787 bool template_id_p = false;
22788 bool qualified_p = false;
22789 bool invalid_nested_name_p = false;
22790 bool invalid_explicit_specialization_p = false;
22791 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22792 tree pushed_scope = NULL_TREE;
22793 unsigned num_templates;
22794 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22795 /* Assume no nested-name-specifier will be present. */
22796 *nested_name_specifier_p = false;
22797 /* Assume no template parameter lists will be used in defining the
22798 type. */
22799 num_templates = 0;
22800 parser->colon_corrects_to_scope_p = false;
22802 /* Look for the class-key. */
22803 class_key = cp_parser_class_key (parser);
22804 if (class_key == none_type)
22805 return error_mark_node;
22807 location_t class_head_start_location = input_location;
22809 /* Parse the attributes. */
22810 attributes = cp_parser_attributes_opt (parser);
22812 /* If the next token is `::', that is invalid -- but sometimes
22813 people do try to write:
22815 struct ::S {};
22817 Handle this gracefully by accepting the extra qualifier, and then
22818 issuing an error about it later if this really is a
22819 class-head. If it turns out just to be an elaborated type
22820 specifier, remain silent. */
22821 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22822 qualified_p = true;
22824 push_deferring_access_checks (dk_no_check);
22826 /* Determine the name of the class. Begin by looking for an
22827 optional nested-name-specifier. */
22828 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22829 nested_name_specifier
22830 = cp_parser_nested_name_specifier_opt (parser,
22831 /*typename_keyword_p=*/false,
22832 /*check_dependency_p=*/false,
22833 /*type_p=*/true,
22834 /*is_declaration=*/false);
22835 /* If there was a nested-name-specifier, then there *must* be an
22836 identifier. */
22838 cp_token *bad_template_keyword = NULL;
22840 if (nested_name_specifier)
22842 type_start_token = cp_lexer_peek_token (parser->lexer);
22843 /* Although the grammar says `identifier', it really means
22844 `class-name' or `template-name'. You are only allowed to
22845 define a class that has already been declared with this
22846 syntax.
22848 The proposed resolution for Core Issue 180 says that wherever
22849 you see `class T::X' you should treat `X' as a type-name.
22851 It is OK to define an inaccessible class; for example:
22853 class A { class B; };
22854 class A::B {};
22856 We do not know if we will see a class-name, or a
22857 template-name. We look for a class-name first, in case the
22858 class-name is a template-id; if we looked for the
22859 template-name first we would stop after the template-name. */
22860 cp_parser_parse_tentatively (parser);
22861 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22862 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22863 type = cp_parser_class_name (parser,
22864 /*typename_keyword_p=*/false,
22865 /*template_keyword_p=*/false,
22866 class_type,
22867 /*check_dependency_p=*/false,
22868 /*class_head_p=*/true,
22869 /*is_declaration=*/false);
22870 /* If that didn't work, ignore the nested-name-specifier. */
22871 if (!cp_parser_parse_definitely (parser))
22873 invalid_nested_name_p = true;
22874 type_start_token = cp_lexer_peek_token (parser->lexer);
22875 id = cp_parser_identifier (parser);
22876 if (id == error_mark_node)
22877 id = NULL_TREE;
22879 /* If we could not find a corresponding TYPE, treat this
22880 declaration like an unqualified declaration. */
22881 if (type == error_mark_node)
22882 nested_name_specifier = NULL_TREE;
22883 /* Otherwise, count the number of templates used in TYPE and its
22884 containing scopes. */
22885 else
22887 tree scope;
22889 for (scope = TREE_TYPE (type);
22890 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22891 scope = get_containing_scope (scope))
22892 if (TYPE_P (scope)
22893 && CLASS_TYPE_P (scope)
22894 && CLASSTYPE_TEMPLATE_INFO (scope)
22895 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22896 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22897 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22898 ++num_templates;
22901 /* Otherwise, the identifier is optional. */
22902 else
22904 /* We don't know whether what comes next is a template-id,
22905 an identifier, or nothing at all. */
22906 cp_parser_parse_tentatively (parser);
22907 /* Check for a template-id. */
22908 type_start_token = cp_lexer_peek_token (parser->lexer);
22909 id = cp_parser_template_id (parser,
22910 /*template_keyword_p=*/false,
22911 /*check_dependency_p=*/true,
22912 class_key,
22913 /*is_declaration=*/true);
22914 /* If that didn't work, it could still be an identifier. */
22915 if (!cp_parser_parse_definitely (parser))
22917 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22919 type_start_token = cp_lexer_peek_token (parser->lexer);
22920 id = cp_parser_identifier (parser);
22922 else
22923 id = NULL_TREE;
22925 else
22927 template_id_p = true;
22928 ++num_templates;
22932 pop_deferring_access_checks ();
22934 if (id)
22936 cp_parser_check_for_invalid_template_id (parser, id,
22937 class_key,
22938 type_start_token->location);
22940 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22942 /* If it's not a `:' or a `{' then we can't really be looking at a
22943 class-head, since a class-head only appears as part of a
22944 class-specifier. We have to detect this situation before calling
22945 xref_tag, since that has irreversible side-effects. */
22946 if (!cp_parser_next_token_starts_class_definition_p (parser))
22948 cp_parser_error (parser, "expected %<{%> or %<:%>");
22949 type = error_mark_node;
22950 goto out;
22953 /* At this point, we're going ahead with the class-specifier, even
22954 if some other problem occurs. */
22955 cp_parser_commit_to_tentative_parse (parser);
22956 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22958 cp_parser_error (parser,
22959 "cannot specify %<override%> for a class");
22960 type = error_mark_node;
22961 goto out;
22963 /* Issue the error about the overly-qualified name now. */
22964 if (qualified_p)
22966 cp_parser_error (parser,
22967 "global qualification of class name is invalid");
22968 type = error_mark_node;
22969 goto out;
22971 else if (invalid_nested_name_p)
22973 cp_parser_error (parser,
22974 "qualified name does not name a class");
22975 type = error_mark_node;
22976 goto out;
22978 else if (nested_name_specifier)
22980 tree scope;
22982 if (bad_template_keyword)
22983 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22984 keyword template shall not appear at the top level. */
22985 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22986 "keyword %<template%> not allowed in class-head-name");
22988 /* Reject typedef-names in class heads. */
22989 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22991 error_at (type_start_token->location,
22992 "invalid class name in declaration of %qD",
22993 type);
22994 type = NULL_TREE;
22995 goto done;
22998 /* Figure out in what scope the declaration is being placed. */
22999 scope = current_scope ();
23000 /* If that scope does not contain the scope in which the
23001 class was originally declared, the program is invalid. */
23002 if (scope && !is_ancestor (scope, nested_name_specifier))
23004 if (at_namespace_scope_p ())
23005 error_at (type_start_token->location,
23006 "declaration of %qD in namespace %qD which does not "
23007 "enclose %qD",
23008 type, scope, nested_name_specifier);
23009 else
23010 error_at (type_start_token->location,
23011 "declaration of %qD in %qD which does not enclose %qD",
23012 type, scope, nested_name_specifier);
23013 type = NULL_TREE;
23014 goto done;
23016 /* [dcl.meaning]
23018 A declarator-id shall not be qualified except for the
23019 definition of a ... nested class outside of its class
23020 ... [or] the definition or explicit instantiation of a
23021 class member of a namespace outside of its namespace. */
23022 if (scope == nested_name_specifier)
23024 permerror (nested_name_specifier_token_start->location,
23025 "extra qualification not allowed");
23026 nested_name_specifier = NULL_TREE;
23027 num_templates = 0;
23030 /* An explicit-specialization must be preceded by "template <>". If
23031 it is not, try to recover gracefully. */
23032 if (at_namespace_scope_p ()
23033 && parser->num_template_parameter_lists == 0
23034 && !processing_template_parmlist
23035 && template_id_p)
23037 /* Build a location of this form:
23038 struct typename <ARGS>
23039 ^~~~~~~~~~~~~~~~~~~~~~
23040 with caret==start at the start token, and
23041 finishing at the end of the type. */
23042 location_t reported_loc
23043 = make_location (class_head_start_location,
23044 class_head_start_location,
23045 get_finish (type_start_token->location));
23046 rich_location richloc (line_table, reported_loc);
23047 richloc.add_fixit_insert_before (class_head_start_location,
23048 "template <> ");
23049 error_at_rich_loc
23050 (&richloc,
23051 "an explicit specialization must be preceded by %<template <>%>");
23052 invalid_explicit_specialization_p = true;
23053 /* Take the same action that would have been taken by
23054 cp_parser_explicit_specialization. */
23055 ++parser->num_template_parameter_lists;
23056 begin_specialization ();
23058 /* There must be no "return" statements between this point and the
23059 end of this function; set "type "to the correct return value and
23060 use "goto done;" to return. */
23061 /* Make sure that the right number of template parameters were
23062 present. */
23063 if (!cp_parser_check_template_parameters (parser, num_templates,
23064 type_start_token->location,
23065 /*declarator=*/NULL))
23067 /* If something went wrong, there is no point in even trying to
23068 process the class-definition. */
23069 type = NULL_TREE;
23070 goto done;
23073 /* Look up the type. */
23074 if (template_id_p)
23076 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
23077 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
23078 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
23080 error_at (type_start_token->location,
23081 "function template %qD redeclared as a class template", id);
23082 type = error_mark_node;
23084 else
23086 type = TREE_TYPE (id);
23087 type = maybe_process_partial_specialization (type);
23089 /* Check the scope while we still know whether or not we had a
23090 nested-name-specifier. */
23091 if (type != error_mark_node)
23092 check_unqualified_spec_or_inst (type, type_start_token->location);
23094 if (nested_name_specifier)
23095 pushed_scope = push_scope (nested_name_specifier);
23097 else if (nested_name_specifier)
23099 tree class_type;
23101 /* Given:
23103 template <typename T> struct S { struct T };
23104 template <typename T> struct S<T>::T { };
23106 we will get a TYPENAME_TYPE when processing the definition of
23107 `S::T'. We need to resolve it to the actual type before we
23108 try to define it. */
23109 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23111 class_type = resolve_typename_type (TREE_TYPE (type),
23112 /*only_current_p=*/false);
23113 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23114 type = TYPE_NAME (class_type);
23115 else
23117 cp_parser_error (parser, "could not resolve typename type");
23118 type = error_mark_node;
23122 if (maybe_process_partial_specialization (TREE_TYPE (type))
23123 == error_mark_node)
23125 type = NULL_TREE;
23126 goto done;
23129 class_type = current_class_type;
23130 /* Enter the scope indicated by the nested-name-specifier. */
23131 pushed_scope = push_scope (nested_name_specifier);
23132 /* Get the canonical version of this type. */
23133 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23134 /* Call push_template_decl if it seems like we should be defining a
23135 template either from the template headers or the type we're
23136 defining, so that we diagnose both extra and missing headers. */
23137 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23138 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23139 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23141 type = push_template_decl (type);
23142 if (type == error_mark_node)
23144 type = NULL_TREE;
23145 goto done;
23149 type = TREE_TYPE (type);
23150 *nested_name_specifier_p = true;
23152 else /* The name is not a nested name. */
23154 /* If the class was unnamed, create a dummy name. */
23155 if (!id)
23156 id = make_anon_name ();
23157 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23158 ? ts_within_enclosing_non_class
23159 : ts_current);
23160 type = xref_tag (class_key, id, tag_scope,
23161 parser->num_template_parameter_lists);
23164 /* Indicate whether this class was declared as a `class' or as a
23165 `struct'. */
23166 if (TREE_CODE (type) == RECORD_TYPE)
23167 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23168 cp_parser_check_class_key (class_key, type);
23170 /* If this type was already complete, and we see another definition,
23171 that's an error. */
23172 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23174 error_at (type_start_token->location, "redefinition of %q#T",
23175 type);
23176 inform (location_of (type), "previous definition of %q#T",
23177 type);
23178 type = NULL_TREE;
23179 goto done;
23181 else if (type == error_mark_node)
23182 type = NULL_TREE;
23184 if (type)
23186 /* Apply attributes now, before any use of the class as a template
23187 argument in its base list. */
23188 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23189 fixup_attribute_variants (type);
23192 /* We will have entered the scope containing the class; the names of
23193 base classes should be looked up in that context. For example:
23195 struct A { struct B {}; struct C; };
23196 struct A::C : B {};
23198 is valid. */
23200 /* Get the list of base-classes, if there is one. */
23201 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23203 /* PR59482: enter the class scope so that base-specifiers are looked
23204 up correctly. */
23205 if (type)
23206 pushclass (type);
23207 bases = cp_parser_base_clause (parser);
23208 /* PR59482: get out of the previously pushed class scope so that the
23209 subsequent pops pop the right thing. */
23210 if (type)
23211 popclass ();
23213 else
23214 bases = NULL_TREE;
23216 /* If we're really defining a class, process the base classes.
23217 If they're invalid, fail. */
23218 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23219 xref_basetypes (type, bases);
23221 done:
23222 /* Leave the scope given by the nested-name-specifier. We will
23223 enter the class scope itself while processing the members. */
23224 if (pushed_scope)
23225 pop_scope (pushed_scope);
23227 if (invalid_explicit_specialization_p)
23229 end_specialization ();
23230 --parser->num_template_parameter_lists;
23233 if (type)
23234 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23235 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23236 CLASSTYPE_FINAL (type) = 1;
23237 out:
23238 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23239 return type;
23242 /* Parse a class-key.
23244 class-key:
23245 class
23246 struct
23247 union
23249 Returns the kind of class-key specified, or none_type to indicate
23250 error. */
23252 static enum tag_types
23253 cp_parser_class_key (cp_parser* parser)
23255 cp_token *token;
23256 enum tag_types tag_type;
23258 /* Look for the class-key. */
23259 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23260 if (!token)
23261 return none_type;
23263 /* Check to see if the TOKEN is a class-key. */
23264 tag_type = cp_parser_token_is_class_key (token);
23265 if (!tag_type)
23266 cp_parser_error (parser, "expected class-key");
23267 return tag_type;
23270 /* Parse a type-parameter-key.
23272 type-parameter-key:
23273 class
23274 typename
23277 static void
23278 cp_parser_type_parameter_key (cp_parser* parser)
23280 /* Look for the type-parameter-key. */
23281 enum tag_types tag_type = none_type;
23282 cp_token *token = cp_lexer_peek_token (parser->lexer);
23283 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23285 cp_lexer_consume_token (parser->lexer);
23286 if (pedantic && tag_type == typename_type && cxx_dialect < cxx17)
23287 /* typename is not allowed in a template template parameter
23288 by the standard until C++17. */
23289 pedwarn (token->location, OPT_Wpedantic,
23290 "ISO C++ forbids typename key in template template parameter;"
23291 " use -std=c++17 or -std=gnu++17");
23293 else
23294 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23296 return;
23299 /* Parse an (optional) member-specification.
23301 member-specification:
23302 member-declaration member-specification [opt]
23303 access-specifier : member-specification [opt] */
23305 static void
23306 cp_parser_member_specification_opt (cp_parser* parser)
23308 while (true)
23310 cp_token *token;
23311 enum rid keyword;
23313 /* Peek at the next token. */
23314 token = cp_lexer_peek_token (parser->lexer);
23315 /* If it's a `}', or EOF then we've seen all the members. */
23316 if (token->type == CPP_CLOSE_BRACE
23317 || token->type == CPP_EOF
23318 || token->type == CPP_PRAGMA_EOL)
23319 break;
23321 /* See if this token is a keyword. */
23322 keyword = token->keyword;
23323 switch (keyword)
23325 case RID_PUBLIC:
23326 case RID_PROTECTED:
23327 case RID_PRIVATE:
23328 /* Consume the access-specifier. */
23329 cp_lexer_consume_token (parser->lexer);
23330 /* Remember which access-specifier is active. */
23331 current_access_specifier = token->u.value;
23332 /* Look for the `:'. */
23333 cp_parser_require (parser, CPP_COLON, RT_COLON);
23334 break;
23336 default:
23337 /* Accept #pragmas at class scope. */
23338 if (token->type == CPP_PRAGMA)
23340 cp_parser_pragma (parser, pragma_member, NULL);
23341 break;
23344 /* Otherwise, the next construction must be a
23345 member-declaration. */
23346 cp_parser_member_declaration (parser);
23351 /* Parse a member-declaration.
23353 member-declaration:
23354 decl-specifier-seq [opt] member-declarator-list [opt] ;
23355 function-definition ; [opt]
23356 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23357 using-declaration
23358 template-declaration
23359 alias-declaration
23361 member-declarator-list:
23362 member-declarator
23363 member-declarator-list , member-declarator
23365 member-declarator:
23366 declarator pure-specifier [opt]
23367 declarator constant-initializer [opt]
23368 identifier [opt] : constant-expression
23370 GNU Extensions:
23372 member-declaration:
23373 __extension__ member-declaration
23375 member-declarator:
23376 declarator attributes [opt] pure-specifier [opt]
23377 declarator attributes [opt] constant-initializer [opt]
23378 identifier [opt] attributes [opt] : constant-expression
23380 C++0x Extensions:
23382 member-declaration:
23383 static_assert-declaration */
23385 static void
23386 cp_parser_member_declaration (cp_parser* parser)
23388 cp_decl_specifier_seq decl_specifiers;
23389 tree prefix_attributes;
23390 tree decl;
23391 int declares_class_or_enum;
23392 bool friend_p;
23393 cp_token *token = NULL;
23394 cp_token *decl_spec_token_start = NULL;
23395 cp_token *initializer_token_start = NULL;
23396 int saved_pedantic;
23397 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23399 /* Check for the `__extension__' keyword. */
23400 if (cp_parser_extension_opt (parser, &saved_pedantic))
23402 /* Recurse. */
23403 cp_parser_member_declaration (parser);
23404 /* Restore the old value of the PEDANTIC flag. */
23405 pedantic = saved_pedantic;
23407 return;
23410 /* Check for a template-declaration. */
23411 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23413 /* An explicit specialization here is an error condition, and we
23414 expect the specialization handler to detect and report this. */
23415 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23416 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23417 cp_parser_explicit_specialization (parser);
23418 else
23419 cp_parser_template_declaration (parser, /*member_p=*/true);
23421 return;
23423 /* Check for a template introduction. */
23424 else if (cp_parser_template_declaration_after_export (parser, true))
23425 return;
23427 /* Check for a using-declaration. */
23428 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23430 if (cxx_dialect < cxx11)
23432 /* Parse the using-declaration. */
23433 cp_parser_using_declaration (parser,
23434 /*access_declaration_p=*/false);
23435 return;
23437 else
23439 tree decl;
23440 bool alias_decl_expected;
23441 cp_parser_parse_tentatively (parser);
23442 decl = cp_parser_alias_declaration (parser);
23443 /* Note that if we actually see the '=' token after the
23444 identifier, cp_parser_alias_declaration commits the
23445 tentative parse. In that case, we really expect an
23446 alias-declaration. Otherwise, we expect a using
23447 declaration. */
23448 alias_decl_expected =
23449 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23450 cp_parser_parse_definitely (parser);
23452 if (alias_decl_expected)
23453 finish_member_declaration (decl);
23454 else
23455 cp_parser_using_declaration (parser,
23456 /*access_declaration_p=*/false);
23457 return;
23461 /* Check for @defs. */
23462 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23464 tree ivar, member;
23465 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23466 ivar = ivar_chains;
23467 while (ivar)
23469 member = ivar;
23470 ivar = TREE_CHAIN (member);
23471 TREE_CHAIN (member) = NULL_TREE;
23472 finish_member_declaration (member);
23474 return;
23477 /* If the next token is `static_assert' we have a static assertion. */
23478 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23480 cp_parser_static_assert (parser, /*member_p=*/true);
23481 return;
23484 parser->colon_corrects_to_scope_p = false;
23486 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23487 goto out;
23489 /* Parse the decl-specifier-seq. */
23490 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23491 cp_parser_decl_specifier_seq (parser,
23492 CP_PARSER_FLAGS_OPTIONAL,
23493 &decl_specifiers,
23494 &declares_class_or_enum);
23495 /* Check for an invalid type-name. */
23496 if (!decl_specifiers.any_type_specifiers_p
23497 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23498 goto out;
23499 /* If there is no declarator, then the decl-specifier-seq should
23500 specify a type. */
23501 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23503 /* If there was no decl-specifier-seq, and the next token is a
23504 `;', then we have something like:
23506 struct S { ; };
23508 [class.mem]
23510 Each member-declaration shall declare at least one member
23511 name of the class. */
23512 if (!decl_specifiers.any_specifiers_p)
23514 cp_token *token = cp_lexer_peek_token (parser->lexer);
23515 if (!in_system_header_at (token->location))
23517 gcc_rich_location richloc (token->location);
23518 richloc.add_fixit_remove ();
23519 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23522 else
23524 tree type;
23526 /* See if this declaration is a friend. */
23527 friend_p = cp_parser_friend_p (&decl_specifiers);
23528 /* If there were decl-specifiers, check to see if there was
23529 a class-declaration. */
23530 type = check_tag_decl (&decl_specifiers,
23531 /*explicit_type_instantiation_p=*/false);
23532 /* Nested classes have already been added to the class, but
23533 a `friend' needs to be explicitly registered. */
23534 if (friend_p)
23536 /* If the `friend' keyword was present, the friend must
23537 be introduced with a class-key. */
23538 if (!declares_class_or_enum && cxx_dialect < cxx11)
23539 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23540 "in C++03 a class-key must be used "
23541 "when declaring a friend");
23542 /* In this case:
23544 template <typename T> struct A {
23545 friend struct A<T>::B;
23548 A<T>::B will be represented by a TYPENAME_TYPE, and
23549 therefore not recognized by check_tag_decl. */
23550 if (!type)
23552 type = decl_specifiers.type;
23553 if (type && TREE_CODE (type) == TYPE_DECL)
23554 type = TREE_TYPE (type);
23556 if (!type || !TYPE_P (type))
23557 error_at (decl_spec_token_start->location,
23558 "friend declaration does not name a class or "
23559 "function");
23560 else
23561 make_friend_class (current_class_type, type,
23562 /*complain=*/true);
23564 /* If there is no TYPE, an error message will already have
23565 been issued. */
23566 else if (!type || type == error_mark_node)
23568 /* An anonymous aggregate has to be handled specially; such
23569 a declaration really declares a data member (with a
23570 particular type), as opposed to a nested class. */
23571 else if (ANON_AGGR_TYPE_P (type))
23573 /* C++11 9.5/6. */
23574 if (decl_specifiers.storage_class != sc_none)
23575 error_at (decl_spec_token_start->location,
23576 "a storage class on an anonymous aggregate "
23577 "in class scope is not allowed");
23579 /* Remove constructors and such from TYPE, now that we
23580 know it is an anonymous aggregate. */
23581 fixup_anonymous_aggr (type);
23582 /* And make the corresponding data member. */
23583 decl = build_decl (decl_spec_token_start->location,
23584 FIELD_DECL, NULL_TREE, type);
23585 /* Add it to the class. */
23586 finish_member_declaration (decl);
23588 else
23589 cp_parser_check_access_in_redeclaration
23590 (TYPE_NAME (type),
23591 decl_spec_token_start->location);
23594 else
23596 bool assume_semicolon = false;
23598 /* Clear attributes from the decl_specifiers but keep them
23599 around as prefix attributes that apply them to the entity
23600 being declared. */
23601 prefix_attributes = decl_specifiers.attributes;
23602 decl_specifiers.attributes = NULL_TREE;
23604 /* See if these declarations will be friends. */
23605 friend_p = cp_parser_friend_p (&decl_specifiers);
23607 /* Keep going until we hit the `;' at the end of the
23608 declaration. */
23609 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23611 tree attributes = NULL_TREE;
23612 tree first_attribute;
23613 tree initializer;
23614 bool is_bitfld = false;
23615 bool named_bitfld = false;
23617 /* Peek at the next token. */
23618 token = cp_lexer_peek_token (parser->lexer);
23620 /* The following code wants to know early if it is a bit-field
23621 or some other declaration. Attributes can appear before
23622 the `:' token, but are hopefully rare enough that the
23623 simplicity of the tentative lookup pays off. */
23624 if (cp_next_tokens_can_be_attribute_p (parser)
23625 || (token->type == CPP_NAME
23626 && cp_nth_tokens_can_be_attribute_p (parser, 2)
23627 && (named_bitfld = true)))
23629 cp_parser_parse_tentatively (parser);
23630 if (named_bitfld)
23631 cp_lexer_consume_token (parser->lexer);
23632 cp_parser_attributes_opt (parser);
23633 token = cp_lexer_peek_token (parser->lexer);
23634 is_bitfld = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
23635 cp_parser_abort_tentative_parse (parser);
23638 /* Check for a bitfield declaration. */
23639 if (is_bitfld
23640 || token->type == CPP_COLON
23641 || (token->type == CPP_NAME
23642 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
23643 && (named_bitfld = true)))
23645 tree identifier;
23646 tree width;
23647 tree late_attributes = NULL_TREE;
23649 if (named_bitfld)
23650 identifier = cp_parser_identifier (parser);
23651 else
23652 identifier = NULL_TREE;
23654 /* Look for attributes that apply to the bitfield. */
23655 attributes = cp_parser_attributes_opt (parser);
23657 /* Consume the `:' token. */
23658 cp_lexer_consume_token (parser->lexer);
23660 /* Get the width of the bitfield. */
23661 width = cp_parser_constant_expression (parser, false, NULL,
23662 cxx_dialect >= cxx11);
23664 /* In C++2A and as extension for C++11 and above we allow
23665 default member initializers for bit-fields. */
23666 initializer = NULL_TREE;
23667 if (cxx_dialect >= cxx11
23668 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
23669 || cp_lexer_next_token_is (parser->lexer,
23670 CPP_OPEN_BRACE)))
23672 location_t loc
23673 = cp_lexer_peek_token (parser->lexer)->location;
23674 if (cxx_dialect < cxx2a
23675 && !in_system_header_at (loc)
23676 && identifier != NULL_TREE)
23677 pedwarn (loc, 0,
23678 "default member initializers for bit-fields "
23679 "only available with -std=c++2a or "
23680 "-std=gnu++2a");
23682 initializer = cp_parser_save_nsdmi (parser);
23683 if (identifier == NULL_TREE)
23685 error_at (loc, "default member initializer for "
23686 "unnamed bit-field");
23687 initializer = NULL_TREE;
23690 else
23692 /* Look for attributes that apply to the bitfield after
23693 the `:' token and width. This is where GCC used to
23694 parse attributes in the past, pedwarn if there is
23695 a std attribute. */
23696 if (cp_next_tokens_can_be_std_attribute_p (parser))
23697 pedwarn (input_location, OPT_Wpedantic,
23698 "ISO C++ allows bit-field attributes only "
23699 "before the %<:%> token");
23701 late_attributes = cp_parser_attributes_opt (parser);
23704 attributes = chainon (attributes, late_attributes);
23706 /* Remember which attributes are prefix attributes and
23707 which are not. */
23708 first_attribute = attributes;
23709 /* Combine the attributes. */
23710 attributes = chainon (prefix_attributes, attributes);
23712 /* Create the bitfield declaration. */
23713 decl = grokbitfield (identifier
23714 ? make_id_declarator (NULL_TREE,
23715 identifier,
23716 sfk_none)
23717 : NULL,
23718 &decl_specifiers,
23719 width, initializer,
23720 attributes);
23722 else
23724 cp_declarator *declarator;
23725 tree asm_specification;
23726 int ctor_dtor_or_conv_p;
23728 /* Parse the declarator. */
23729 declarator
23730 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23731 &ctor_dtor_or_conv_p,
23732 /*parenthesized_p=*/NULL,
23733 /*member_p=*/true,
23734 friend_p);
23736 /* If something went wrong parsing the declarator, make sure
23737 that we at least consume some tokens. */
23738 if (declarator == cp_error_declarator)
23740 /* Skip to the end of the statement. */
23741 cp_parser_skip_to_end_of_statement (parser);
23742 /* If the next token is not a semicolon, that is
23743 probably because we just skipped over the body of
23744 a function. So, we consume a semicolon if
23745 present, but do not issue an error message if it
23746 is not present. */
23747 if (cp_lexer_next_token_is (parser->lexer,
23748 CPP_SEMICOLON))
23749 cp_lexer_consume_token (parser->lexer);
23750 goto out;
23753 if (declares_class_or_enum & 2)
23754 cp_parser_check_for_definition_in_return_type
23755 (declarator, decl_specifiers.type,
23756 decl_specifiers.locations[ds_type_spec]);
23758 /* Look for an asm-specification. */
23759 asm_specification = cp_parser_asm_specification_opt (parser);
23760 /* Look for attributes that apply to the declaration. */
23761 attributes = cp_parser_attributes_opt (parser);
23762 /* Remember which attributes are prefix attributes and
23763 which are not. */
23764 first_attribute = attributes;
23765 /* Combine the attributes. */
23766 attributes = chainon (prefix_attributes, attributes);
23768 /* If it's an `=', then we have a constant-initializer or a
23769 pure-specifier. It is not correct to parse the
23770 initializer before registering the member declaration
23771 since the member declaration should be in scope while
23772 its initializer is processed. However, the rest of the
23773 front end does not yet provide an interface that allows
23774 us to handle this correctly. */
23775 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23777 /* In [class.mem]:
23779 A pure-specifier shall be used only in the declaration of
23780 a virtual function.
23782 A member-declarator can contain a constant-initializer
23783 only if it declares a static member of integral or
23784 enumeration type.
23786 Therefore, if the DECLARATOR is for a function, we look
23787 for a pure-specifier; otherwise, we look for a
23788 constant-initializer. When we call `grokfield', it will
23789 perform more stringent semantics checks. */
23790 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23791 if (function_declarator_p (declarator)
23792 || (decl_specifiers.type
23793 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23794 && declarator->kind == cdk_id
23795 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23796 == FUNCTION_TYPE)))
23797 initializer = cp_parser_pure_specifier (parser);
23798 else if (decl_specifiers.storage_class != sc_static)
23799 initializer = cp_parser_save_nsdmi (parser);
23800 else if (cxx_dialect >= cxx11)
23802 bool nonconst;
23803 /* Don't require a constant rvalue in C++11, since we
23804 might want a reference constant. We'll enforce
23805 constancy later. */
23806 cp_lexer_consume_token (parser->lexer);
23807 /* Parse the initializer. */
23808 initializer = cp_parser_initializer_clause (parser,
23809 &nonconst);
23811 else
23812 /* Parse the initializer. */
23813 initializer = cp_parser_constant_initializer (parser);
23815 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23816 && !function_declarator_p (declarator))
23818 bool x;
23819 if (decl_specifiers.storage_class != sc_static)
23820 initializer = cp_parser_save_nsdmi (parser);
23821 else
23822 initializer = cp_parser_initializer (parser, &x, &x);
23824 /* Otherwise, there is no initializer. */
23825 else
23826 initializer = NULL_TREE;
23828 /* See if we are probably looking at a function
23829 definition. We are certainly not looking at a
23830 member-declarator. Calling `grokfield' has
23831 side-effects, so we must not do it unless we are sure
23832 that we are looking at a member-declarator. */
23833 if (cp_parser_token_starts_function_definition_p
23834 (cp_lexer_peek_token (parser->lexer)))
23836 /* The grammar does not allow a pure-specifier to be
23837 used when a member function is defined. (It is
23838 possible that this fact is an oversight in the
23839 standard, since a pure function may be defined
23840 outside of the class-specifier. */
23841 if (initializer && initializer_token_start)
23842 error_at (initializer_token_start->location,
23843 "pure-specifier on function-definition");
23844 decl = cp_parser_save_member_function_body (parser,
23845 &decl_specifiers,
23846 declarator,
23847 attributes);
23848 if (parser->fully_implicit_function_template_p)
23849 decl = finish_fully_implicit_template (parser, decl);
23850 /* If the member was not a friend, declare it here. */
23851 if (!friend_p)
23852 finish_member_declaration (decl);
23853 /* Peek at the next token. */
23854 token = cp_lexer_peek_token (parser->lexer);
23855 /* If the next token is a semicolon, consume it. */
23856 if (token->type == CPP_SEMICOLON)
23858 location_t semicolon_loc
23859 = cp_lexer_consume_token (parser->lexer)->location;
23860 gcc_rich_location richloc (semicolon_loc);
23861 richloc.add_fixit_remove ();
23862 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23863 "extra %<;%> after in-class "
23864 "function definition");
23866 goto out;
23868 else
23869 if (declarator->kind == cdk_function)
23870 declarator->id_loc = token->location;
23871 /* Create the declaration. */
23872 decl = grokfield (declarator, &decl_specifiers,
23873 initializer, /*init_const_expr_p=*/true,
23874 asm_specification, attributes);
23875 if (parser->fully_implicit_function_template_p)
23877 if (friend_p)
23878 finish_fully_implicit_template (parser, 0);
23879 else
23880 decl = finish_fully_implicit_template (parser, decl);
23884 cp_finalize_omp_declare_simd (parser, decl);
23885 cp_finalize_oacc_routine (parser, decl, false);
23887 /* Reset PREFIX_ATTRIBUTES. */
23888 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23889 attributes = TREE_CHAIN (attributes);
23890 if (attributes)
23891 TREE_CHAIN (attributes) = NULL_TREE;
23893 /* If there is any qualification still in effect, clear it
23894 now; we will be starting fresh with the next declarator. */
23895 parser->scope = NULL_TREE;
23896 parser->qualifying_scope = NULL_TREE;
23897 parser->object_scope = NULL_TREE;
23898 /* If it's a `,', then there are more declarators. */
23899 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23901 cp_lexer_consume_token (parser->lexer);
23902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23904 cp_token *token = cp_lexer_previous_token (parser->lexer);
23905 gcc_rich_location richloc (token->location);
23906 richloc.add_fixit_remove ();
23907 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23908 "member declaration");
23911 /* If the next token isn't a `;', then we have a parse error. */
23912 else if (cp_lexer_next_token_is_not (parser->lexer,
23913 CPP_SEMICOLON))
23915 /* The next token might be a ways away from where the
23916 actual semicolon is missing. Find the previous token
23917 and use that for our error position. */
23918 cp_token *token = cp_lexer_previous_token (parser->lexer);
23919 gcc_rich_location richloc (token->location);
23920 richloc.add_fixit_insert_after (";");
23921 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23922 "member declaration");
23924 /* Assume that the user meant to provide a semicolon. If
23925 we were to cp_parser_skip_to_end_of_statement, we might
23926 skip to a semicolon inside a member function definition
23927 and issue nonsensical error messages. */
23928 assume_semicolon = true;
23931 if (decl)
23933 /* Add DECL to the list of members. */
23934 if (!friend_p
23935 /* Explicitly include, eg, NSDMIs, for better error
23936 recovery (c++/58650). */
23937 || !DECL_DECLARES_FUNCTION_P (decl))
23938 finish_member_declaration (decl);
23940 if (TREE_CODE (decl) == FUNCTION_DECL)
23941 cp_parser_save_default_args (parser, decl);
23942 else if (TREE_CODE (decl) == FIELD_DECL
23943 && DECL_INITIAL (decl))
23944 /* Add DECL to the queue of NSDMI to be parsed later. */
23945 vec_safe_push (unparsed_nsdmis, decl);
23948 if (assume_semicolon)
23949 goto out;
23953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23954 out:
23955 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23958 /* Parse a pure-specifier.
23960 pure-specifier:
23963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23964 Otherwise, ERROR_MARK_NODE is returned. */
23966 static tree
23967 cp_parser_pure_specifier (cp_parser* parser)
23969 cp_token *token;
23971 /* Look for the `=' token. */
23972 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23973 return error_mark_node;
23974 /* Look for the `0' token. */
23975 token = cp_lexer_peek_token (parser->lexer);
23977 if (token->type == CPP_EOF
23978 || token->type == CPP_PRAGMA_EOL)
23979 return error_mark_node;
23981 cp_lexer_consume_token (parser->lexer);
23983 /* Accept = default or = delete in c++0x mode. */
23984 if (token->keyword == RID_DEFAULT
23985 || token->keyword == RID_DELETE)
23987 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23988 return token->u.value;
23991 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23992 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23994 cp_parser_error (parser,
23995 "invalid pure specifier (only %<= 0%> is allowed)");
23996 cp_parser_skip_to_end_of_statement (parser);
23997 return error_mark_node;
23999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
24001 error_at (token->location, "templates may not be %<virtual%>");
24002 return error_mark_node;
24005 return integer_zero_node;
24008 /* Parse a constant-initializer.
24010 constant-initializer:
24011 = constant-expression
24013 Returns a representation of the constant-expression. */
24015 static tree
24016 cp_parser_constant_initializer (cp_parser* parser)
24018 /* Look for the `=' token. */
24019 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24020 return error_mark_node;
24022 /* It is invalid to write:
24024 struct S { static const int i = { 7 }; };
24027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24029 cp_parser_error (parser,
24030 "a brace-enclosed initializer is not allowed here");
24031 /* Consume the opening brace. */
24032 matching_braces braces;
24033 braces.consume_open (parser);
24034 /* Skip the initializer. */
24035 cp_parser_skip_to_closing_brace (parser);
24036 /* Look for the trailing `}'. */
24037 braces.require_close (parser);
24039 return error_mark_node;
24042 return cp_parser_constant_expression (parser);
24045 /* Derived classes [gram.class.derived] */
24047 /* Parse a base-clause.
24049 base-clause:
24050 : base-specifier-list
24052 base-specifier-list:
24053 base-specifier ... [opt]
24054 base-specifier-list , base-specifier ... [opt]
24056 Returns a TREE_LIST representing the base-classes, in the order in
24057 which they were declared. The representation of each node is as
24058 described by cp_parser_base_specifier.
24060 In the case that no bases are specified, this function will return
24061 NULL_TREE, not ERROR_MARK_NODE. */
24063 static tree
24064 cp_parser_base_clause (cp_parser* parser)
24066 tree bases = NULL_TREE;
24068 /* Look for the `:' that begins the list. */
24069 cp_parser_require (parser, CPP_COLON, RT_COLON);
24071 /* Scan the base-specifier-list. */
24072 while (true)
24074 cp_token *token;
24075 tree base;
24076 bool pack_expansion_p = false;
24078 /* Look for the base-specifier. */
24079 base = cp_parser_base_specifier (parser);
24080 /* Look for the (optional) ellipsis. */
24081 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24083 /* Consume the `...'. */
24084 cp_lexer_consume_token (parser->lexer);
24086 pack_expansion_p = true;
24089 /* Add BASE to the front of the list. */
24090 if (base && base != error_mark_node)
24092 if (pack_expansion_p)
24093 /* Make this a pack expansion type. */
24094 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
24096 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
24098 TREE_CHAIN (base) = bases;
24099 bases = base;
24102 /* Peek at the next token. */
24103 token = cp_lexer_peek_token (parser->lexer);
24104 /* If it's not a comma, then the list is complete. */
24105 if (token->type != CPP_COMMA)
24106 break;
24107 /* Consume the `,'. */
24108 cp_lexer_consume_token (parser->lexer);
24111 /* PARSER->SCOPE may still be non-NULL at this point, if the last
24112 base class had a qualified name. However, the next name that
24113 appears is certainly not qualified. */
24114 parser->scope = NULL_TREE;
24115 parser->qualifying_scope = NULL_TREE;
24116 parser->object_scope = NULL_TREE;
24118 return nreverse (bases);
24121 /* Parse a base-specifier.
24123 base-specifier:
24124 :: [opt] nested-name-specifier [opt] class-name
24125 virtual access-specifier [opt] :: [opt] nested-name-specifier
24126 [opt] class-name
24127 access-specifier virtual [opt] :: [opt] nested-name-specifier
24128 [opt] class-name
24130 Returns a TREE_LIST. The TREE_PURPOSE will be one of
24131 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
24132 indicate the specifiers provided. The TREE_VALUE will be a TYPE
24133 (or the ERROR_MARK_NODE) indicating the type that was specified. */
24135 static tree
24136 cp_parser_base_specifier (cp_parser* parser)
24138 cp_token *token;
24139 bool done = false;
24140 bool virtual_p = false;
24141 bool duplicate_virtual_error_issued_p = false;
24142 bool duplicate_access_error_issued_p = false;
24143 bool class_scope_p, template_p;
24144 tree access = access_default_node;
24145 tree type;
24147 /* Process the optional `virtual' and `access-specifier'. */
24148 while (!done)
24150 /* Peek at the next token. */
24151 token = cp_lexer_peek_token (parser->lexer);
24152 /* Process `virtual'. */
24153 switch (token->keyword)
24155 case RID_VIRTUAL:
24156 /* If `virtual' appears more than once, issue an error. */
24157 if (virtual_p && !duplicate_virtual_error_issued_p)
24159 cp_parser_error (parser,
24160 "%<virtual%> specified more than once in base-specifier");
24161 duplicate_virtual_error_issued_p = true;
24164 virtual_p = true;
24166 /* Consume the `virtual' token. */
24167 cp_lexer_consume_token (parser->lexer);
24169 break;
24171 case RID_PUBLIC:
24172 case RID_PROTECTED:
24173 case RID_PRIVATE:
24174 /* If more than one access specifier appears, issue an
24175 error. */
24176 if (access != access_default_node
24177 && !duplicate_access_error_issued_p)
24179 cp_parser_error (parser,
24180 "more than one access specifier in base-specifier");
24181 duplicate_access_error_issued_p = true;
24184 access = ridpointers[(int) token->keyword];
24186 /* Consume the access-specifier. */
24187 cp_lexer_consume_token (parser->lexer);
24189 break;
24191 default:
24192 done = true;
24193 break;
24196 /* It is not uncommon to see programs mechanically, erroneously, use
24197 the 'typename' keyword to denote (dependent) qualified types
24198 as base classes. */
24199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24201 token = cp_lexer_peek_token (parser->lexer);
24202 if (!processing_template_decl)
24203 error_at (token->location,
24204 "keyword %<typename%> not allowed outside of templates");
24205 else
24206 error_at (token->location,
24207 "keyword %<typename%> not allowed in this context "
24208 "(the base class is implicitly a type)");
24209 cp_lexer_consume_token (parser->lexer);
24212 /* Look for the optional `::' operator. */
24213 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24214 /* Look for the nested-name-specifier. The simplest way to
24215 implement:
24217 [temp.res]
24219 The keyword `typename' is not permitted in a base-specifier or
24220 mem-initializer; in these contexts a qualified name that
24221 depends on a template-parameter is implicitly assumed to be a
24222 type name.
24224 is to pretend that we have seen the `typename' keyword at this
24225 point. */
24226 cp_parser_nested_name_specifier_opt (parser,
24227 /*typename_keyword_p=*/true,
24228 /*check_dependency_p=*/true,
24229 /*type_p=*/true,
24230 /*is_declaration=*/true);
24231 /* If the base class is given by a qualified name, assume that names
24232 we see are type names or templates, as appropriate. */
24233 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24234 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24236 if (!parser->scope
24237 && cp_lexer_next_token_is_decltype (parser->lexer))
24238 /* DR 950 allows decltype as a base-specifier. */
24239 type = cp_parser_decltype (parser);
24240 else
24242 /* Otherwise, look for the class-name. */
24243 type = cp_parser_class_name (parser,
24244 class_scope_p,
24245 template_p,
24246 typename_type,
24247 /*check_dependency_p=*/true,
24248 /*class_head_p=*/false,
24249 /*is_declaration=*/true);
24250 type = TREE_TYPE (type);
24253 if (type == error_mark_node)
24254 return error_mark_node;
24256 return finish_base_specifier (type, access, virtual_p);
24259 /* Exception handling [gram.exception] */
24261 /* Parse an (optional) noexcept-specification.
24263 noexcept-specification:
24264 noexcept ( constant-expression ) [opt]
24266 If no noexcept-specification is present, returns NULL_TREE.
24267 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24268 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24269 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24270 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24271 in which case a boolean condition is returned instead. */
24273 static tree
24274 cp_parser_noexcept_specification_opt (cp_parser* parser,
24275 bool require_constexpr,
24276 bool* consumed_expr,
24277 bool return_cond)
24279 cp_token *token;
24280 const char *saved_message;
24282 /* Peek at the next token. */
24283 token = cp_lexer_peek_token (parser->lexer);
24285 /* Is it a noexcept-specification? */
24286 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24288 tree expr;
24289 cp_lexer_consume_token (parser->lexer);
24291 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24293 matching_parens parens;
24294 parens.consume_open (parser);
24296 if (require_constexpr)
24298 /* Types may not be defined in an exception-specification. */
24299 saved_message = parser->type_definition_forbidden_message;
24300 parser->type_definition_forbidden_message
24301 = G_("types may not be defined in an exception-specification");
24303 expr = cp_parser_constant_expression (parser);
24305 /* Restore the saved message. */
24306 parser->type_definition_forbidden_message = saved_message;
24308 else
24310 expr = cp_parser_expression (parser);
24311 *consumed_expr = true;
24314 parens.require_close (parser);
24316 else
24318 expr = boolean_true_node;
24319 if (!require_constexpr)
24320 *consumed_expr = false;
24323 /* We cannot build a noexcept-spec right away because this will check
24324 that expr is a constexpr. */
24325 if (!return_cond)
24326 return build_noexcept_spec (expr, tf_warning_or_error);
24327 else
24328 return expr;
24330 else
24331 return NULL_TREE;
24334 /* Parse an (optional) exception-specification.
24336 exception-specification:
24337 throw ( type-id-list [opt] )
24339 Returns a TREE_LIST representing the exception-specification. The
24340 TREE_VALUE of each node is a type. */
24342 static tree
24343 cp_parser_exception_specification_opt (cp_parser* parser)
24345 cp_token *token;
24346 tree type_id_list;
24347 const char *saved_message;
24349 /* Peek at the next token. */
24350 token = cp_lexer_peek_token (parser->lexer);
24352 /* Is it a noexcept-specification? */
24353 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24354 false);
24355 if (type_id_list != NULL_TREE)
24356 return type_id_list;
24358 /* If it's not `throw', then there's no exception-specification. */
24359 if (!cp_parser_is_keyword (token, RID_THROW))
24360 return NULL_TREE;
24362 location_t loc = token->location;
24364 /* Consume the `throw'. */
24365 cp_lexer_consume_token (parser->lexer);
24367 /* Look for the `('. */
24368 matching_parens parens;
24369 parens.require_open (parser);
24371 /* Peek at the next token. */
24372 token = cp_lexer_peek_token (parser->lexer);
24373 /* If it's not a `)', then there is a type-id-list. */
24374 if (token->type != CPP_CLOSE_PAREN)
24376 /* Types may not be defined in an exception-specification. */
24377 saved_message = parser->type_definition_forbidden_message;
24378 parser->type_definition_forbidden_message
24379 = G_("types may not be defined in an exception-specification");
24380 /* Parse the type-id-list. */
24381 type_id_list = cp_parser_type_id_list (parser);
24382 /* Restore the saved message. */
24383 parser->type_definition_forbidden_message = saved_message;
24385 if (cxx_dialect >= cxx17)
24387 error_at (loc, "ISO C++17 does not allow dynamic exception "
24388 "specifications");
24389 type_id_list = NULL_TREE;
24391 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24392 warning_at (loc, OPT_Wdeprecated,
24393 "dynamic exception specifications are deprecated in "
24394 "C++11");
24396 /* In C++17, throw() is equivalent to noexcept (true). throw()
24397 is deprecated in C++11 and above as well, but is still widely used,
24398 so don't warn about it yet. */
24399 else if (cxx_dialect >= cxx17)
24400 type_id_list = noexcept_true_spec;
24401 else
24402 type_id_list = empty_except_spec;
24404 /* Look for the `)'. */
24405 parens.require_close (parser);
24407 return type_id_list;
24410 /* Parse an (optional) type-id-list.
24412 type-id-list:
24413 type-id ... [opt]
24414 type-id-list , type-id ... [opt]
24416 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24417 in the order that the types were presented. */
24419 static tree
24420 cp_parser_type_id_list (cp_parser* parser)
24422 tree types = NULL_TREE;
24424 while (true)
24426 cp_token *token;
24427 tree type;
24429 token = cp_lexer_peek_token (parser->lexer);
24431 /* Get the next type-id. */
24432 type = cp_parser_type_id (parser);
24433 /* Check for invalid 'auto'. */
24434 if (flag_concepts && type_uses_auto (type))
24436 error_at (token->location,
24437 "invalid use of %<auto%> in exception-specification");
24438 type = error_mark_node;
24440 /* Parse the optional ellipsis. */
24441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24443 /* Consume the `...'. */
24444 cp_lexer_consume_token (parser->lexer);
24446 /* Turn the type into a pack expansion expression. */
24447 type = make_pack_expansion (type);
24449 /* Add it to the list. */
24450 types = add_exception_specifier (types, type, /*complain=*/1);
24451 /* Peek at the next token. */
24452 token = cp_lexer_peek_token (parser->lexer);
24453 /* If it is not a `,', we are done. */
24454 if (token->type != CPP_COMMA)
24455 break;
24456 /* Consume the `,'. */
24457 cp_lexer_consume_token (parser->lexer);
24460 return nreverse (types);
24463 /* Parse a try-block.
24465 try-block:
24466 try compound-statement handler-seq */
24468 static tree
24469 cp_parser_try_block (cp_parser* parser)
24471 tree try_block;
24473 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24474 if (parser->in_function_body
24475 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24476 error ("%<try%> in %<constexpr%> function");
24478 try_block = begin_try_block ();
24479 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24480 finish_try_block (try_block);
24481 cp_parser_handler_seq (parser);
24482 finish_handler_sequence (try_block);
24484 return try_block;
24487 /* Parse a function-try-block.
24489 function-try-block:
24490 try ctor-initializer [opt] function-body handler-seq */
24492 static void
24493 cp_parser_function_try_block (cp_parser* parser)
24495 tree compound_stmt;
24496 tree try_block;
24498 /* Look for the `try' keyword. */
24499 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24500 return;
24501 /* Let the rest of the front end know where we are. */
24502 try_block = begin_function_try_block (&compound_stmt);
24503 /* Parse the function-body. */
24504 cp_parser_ctor_initializer_opt_and_function_body
24505 (parser, /*in_function_try_block=*/true);
24506 /* We're done with the `try' part. */
24507 finish_function_try_block (try_block);
24508 /* Parse the handlers. */
24509 cp_parser_handler_seq (parser);
24510 /* We're done with the handlers. */
24511 finish_function_handler_sequence (try_block, compound_stmt);
24514 /* Parse a handler-seq.
24516 handler-seq:
24517 handler handler-seq [opt] */
24519 static void
24520 cp_parser_handler_seq (cp_parser* parser)
24522 while (true)
24524 cp_token *token;
24526 /* Parse the handler. */
24527 cp_parser_handler (parser);
24528 /* Peek at the next token. */
24529 token = cp_lexer_peek_token (parser->lexer);
24530 /* If it's not `catch' then there are no more handlers. */
24531 if (!cp_parser_is_keyword (token, RID_CATCH))
24532 break;
24536 /* Parse a handler.
24538 handler:
24539 catch ( exception-declaration ) compound-statement */
24541 static void
24542 cp_parser_handler (cp_parser* parser)
24544 tree handler;
24545 tree declaration;
24547 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24548 handler = begin_handler ();
24549 matching_parens parens;
24550 parens.require_open (parser);
24551 declaration = cp_parser_exception_declaration (parser);
24552 finish_handler_parms (declaration, handler);
24553 parens.require_close (parser);
24554 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24555 finish_handler (handler);
24558 /* Parse an exception-declaration.
24560 exception-declaration:
24561 type-specifier-seq declarator
24562 type-specifier-seq abstract-declarator
24563 type-specifier-seq
24566 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24567 ellipsis variant is used. */
24569 static tree
24570 cp_parser_exception_declaration (cp_parser* parser)
24572 cp_decl_specifier_seq type_specifiers;
24573 cp_declarator *declarator;
24574 const char *saved_message;
24576 /* If it's an ellipsis, it's easy to handle. */
24577 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24579 /* Consume the `...' token. */
24580 cp_lexer_consume_token (parser->lexer);
24581 return NULL_TREE;
24584 /* Types may not be defined in exception-declarations. */
24585 saved_message = parser->type_definition_forbidden_message;
24586 parser->type_definition_forbidden_message
24587 = G_("types may not be defined in exception-declarations");
24589 /* Parse the type-specifier-seq. */
24590 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24591 /*is_trailing_return=*/false,
24592 &type_specifiers);
24593 /* If it's a `)', then there is no declarator. */
24594 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24595 declarator = NULL;
24596 else
24597 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24598 /*ctor_dtor_or_conv_p=*/NULL,
24599 /*parenthesized_p=*/NULL,
24600 /*member_p=*/false,
24601 /*friend_p=*/false);
24603 /* Restore the saved message. */
24604 parser->type_definition_forbidden_message = saved_message;
24606 if (!type_specifiers.any_specifiers_p)
24607 return error_mark_node;
24609 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24612 /* Parse a throw-expression.
24614 throw-expression:
24615 throw assignment-expression [opt]
24617 Returns a THROW_EXPR representing the throw-expression. */
24619 static tree
24620 cp_parser_throw_expression (cp_parser* parser)
24622 tree expression;
24623 cp_token* token;
24625 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24626 token = cp_lexer_peek_token (parser->lexer);
24627 /* Figure out whether or not there is an assignment-expression
24628 following the "throw" keyword. */
24629 if (token->type == CPP_COMMA
24630 || token->type == CPP_SEMICOLON
24631 || token->type == CPP_CLOSE_PAREN
24632 || token->type == CPP_CLOSE_SQUARE
24633 || token->type == CPP_CLOSE_BRACE
24634 || token->type == CPP_COLON)
24635 expression = NULL_TREE;
24636 else
24637 expression = cp_parser_assignment_expression (parser);
24639 return build_throw (expression);
24642 /* GNU Extensions */
24644 /* Parse an (optional) asm-specification.
24646 asm-specification:
24647 asm ( string-literal )
24649 If the asm-specification is present, returns a STRING_CST
24650 corresponding to the string-literal. Otherwise, returns
24651 NULL_TREE. */
24653 static tree
24654 cp_parser_asm_specification_opt (cp_parser* parser)
24656 cp_token *token;
24657 tree asm_specification;
24659 /* Peek at the next token. */
24660 token = cp_lexer_peek_token (parser->lexer);
24661 /* If the next token isn't the `asm' keyword, then there's no
24662 asm-specification. */
24663 if (!cp_parser_is_keyword (token, RID_ASM))
24664 return NULL_TREE;
24666 /* Consume the `asm' token. */
24667 cp_lexer_consume_token (parser->lexer);
24668 /* Look for the `('. */
24669 matching_parens parens;
24670 parens.require_open (parser);
24672 /* Look for the string-literal. */
24673 asm_specification = cp_parser_string_literal (parser, false, false);
24675 /* Look for the `)'. */
24676 parens.require_close (parser);
24678 return asm_specification;
24681 /* Parse an asm-operand-list.
24683 asm-operand-list:
24684 asm-operand
24685 asm-operand-list , asm-operand
24687 asm-operand:
24688 string-literal ( expression )
24689 [ string-literal ] string-literal ( expression )
24691 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24692 each node is the expression. The TREE_PURPOSE is itself a
24693 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24694 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24695 is a STRING_CST for the string literal before the parenthesis. Returns
24696 ERROR_MARK_NODE if any of the operands are invalid. */
24698 static tree
24699 cp_parser_asm_operand_list (cp_parser* parser)
24701 tree asm_operands = NULL_TREE;
24702 bool invalid_operands = false;
24704 while (true)
24706 tree string_literal;
24707 tree expression;
24708 tree name;
24710 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24712 /* Consume the `[' token. */
24713 cp_lexer_consume_token (parser->lexer);
24714 /* Read the operand name. */
24715 name = cp_parser_identifier (parser);
24716 if (name != error_mark_node)
24717 name = build_string (IDENTIFIER_LENGTH (name),
24718 IDENTIFIER_POINTER (name));
24719 /* Look for the closing `]'. */
24720 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24722 else
24723 name = NULL_TREE;
24724 /* Look for the string-literal. */
24725 string_literal = cp_parser_string_literal (parser, false, false);
24727 /* Look for the `('. */
24728 matching_parens parens;
24729 parens.require_open (parser);
24730 /* Parse the expression. */
24731 expression = cp_parser_expression (parser);
24732 /* Look for the `)'. */
24733 parens.require_close (parser);
24735 if (name == error_mark_node
24736 || string_literal == error_mark_node
24737 || expression == error_mark_node)
24738 invalid_operands = true;
24740 /* Add this operand to the list. */
24741 asm_operands = tree_cons (build_tree_list (name, string_literal),
24742 expression,
24743 asm_operands);
24744 /* If the next token is not a `,', there are no more
24745 operands. */
24746 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24747 break;
24748 /* Consume the `,'. */
24749 cp_lexer_consume_token (parser->lexer);
24752 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24755 /* Parse an asm-clobber-list.
24757 asm-clobber-list:
24758 string-literal
24759 asm-clobber-list , string-literal
24761 Returns a TREE_LIST, indicating the clobbers in the order that they
24762 appeared. The TREE_VALUE of each node is a STRING_CST. */
24764 static tree
24765 cp_parser_asm_clobber_list (cp_parser* parser)
24767 tree clobbers = NULL_TREE;
24769 while (true)
24771 tree string_literal;
24773 /* Look for the string literal. */
24774 string_literal = cp_parser_string_literal (parser, false, false);
24775 /* Add it to the list. */
24776 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24777 /* If the next token is not a `,', then the list is
24778 complete. */
24779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24780 break;
24781 /* Consume the `,' token. */
24782 cp_lexer_consume_token (parser->lexer);
24785 return clobbers;
24788 /* Parse an asm-label-list.
24790 asm-label-list:
24791 identifier
24792 asm-label-list , identifier
24794 Returns a TREE_LIST, indicating the labels in the order that they
24795 appeared. The TREE_VALUE of each node is a label. */
24797 static tree
24798 cp_parser_asm_label_list (cp_parser* parser)
24800 tree labels = NULL_TREE;
24802 while (true)
24804 tree identifier, label, name;
24806 /* Look for the identifier. */
24807 identifier = cp_parser_identifier (parser);
24808 if (!error_operand_p (identifier))
24810 label = lookup_label (identifier);
24811 if (TREE_CODE (label) == LABEL_DECL)
24813 TREE_USED (label) = 1;
24814 check_goto (label);
24815 name = build_string (IDENTIFIER_LENGTH (identifier),
24816 IDENTIFIER_POINTER (identifier));
24817 labels = tree_cons (name, label, labels);
24820 /* If the next token is not a `,', then the list is
24821 complete. */
24822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24823 break;
24824 /* Consume the `,' token. */
24825 cp_lexer_consume_token (parser->lexer);
24828 return nreverse (labels);
24831 /* Return TRUE iff the next tokens in the stream are possibly the
24832 beginning of a GNU extension attribute. */
24834 static bool
24835 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24837 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24840 /* Return TRUE iff the next tokens in the stream are possibly the
24841 beginning of a standard C++-11 attribute specifier. */
24843 static bool
24844 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24846 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24849 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24850 beginning of a standard C++-11 attribute specifier. */
24852 static bool
24853 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24855 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24857 return (cxx_dialect >= cxx11
24858 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24859 || (token->type == CPP_OPEN_SQUARE
24860 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24861 && token->type == CPP_OPEN_SQUARE)));
24864 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24865 beginning of a GNU extension attribute. */
24867 static bool
24868 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24870 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24872 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24875 /* Return true iff the next tokens can be the beginning of either a
24876 GNU attribute list, or a standard C++11 attribute sequence. */
24878 static bool
24879 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24881 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24882 || cp_next_tokens_can_be_std_attribute_p (parser));
24885 /* Return true iff the next Nth tokens can be the beginning of either
24886 a GNU attribute list, or a standard C++11 attribute sequence. */
24888 static bool
24889 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24891 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24892 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24895 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24896 of GNU attributes, or return NULL. */
24898 static tree
24899 cp_parser_attributes_opt (cp_parser *parser)
24901 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24902 return cp_parser_gnu_attributes_opt (parser);
24903 return cp_parser_std_attribute_spec_seq (parser);
24906 #define CILK_SIMD_FN_CLAUSE_MASK \
24907 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24908 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24909 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24910 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24911 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24913 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24914 vector [(<clauses>)] */
24916 static void
24917 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24919 bool first_p = parser->cilk_simd_fn_info == NULL;
24920 cp_token *token = v_token;
24921 if (first_p)
24923 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24924 parser->cilk_simd_fn_info->error_seen = false;
24925 parser->cilk_simd_fn_info->fndecl_seen = false;
24926 parser->cilk_simd_fn_info->tokens = vNULL;
24927 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24929 int paren_scope = 0;
24930 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24932 cp_lexer_consume_token (parser->lexer);
24933 v_token = cp_lexer_peek_token (parser->lexer);
24934 paren_scope++;
24936 while (paren_scope > 0)
24938 token = cp_lexer_peek_token (parser->lexer);
24939 if (token->type == CPP_OPEN_PAREN)
24940 paren_scope++;
24941 else if (token->type == CPP_CLOSE_PAREN)
24942 paren_scope--;
24943 /* Do not push the last ')' */
24944 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24945 cp_lexer_consume_token (parser->lexer);
24948 token->type = CPP_PRAGMA_EOL;
24949 parser->lexer->next_token = token;
24950 cp_lexer_consume_token (parser->lexer);
24952 struct cp_token_cache *cp
24953 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24954 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24957 /* Parse an (optional) series of attributes.
24959 attributes:
24960 attributes attribute
24962 attribute:
24963 __attribute__ (( attribute-list [opt] ))
24965 The return value is as for cp_parser_gnu_attribute_list. */
24967 static tree
24968 cp_parser_gnu_attributes_opt (cp_parser* parser)
24970 tree attributes = NULL_TREE;
24972 while (true)
24974 cp_token *token;
24975 tree attribute_list;
24976 bool ok = true;
24978 /* Peek at the next token. */
24979 token = cp_lexer_peek_token (parser->lexer);
24980 /* If it's not `__attribute__', then we're done. */
24981 if (token->keyword != RID_ATTRIBUTE)
24982 break;
24984 /* Consume the `__attribute__' keyword. */
24985 cp_lexer_consume_token (parser->lexer);
24986 /* Look for the two `(' tokens. */
24987 matching_parens outer_parens;
24988 outer_parens.require_open (parser);
24989 matching_parens inner_parens;
24990 inner_parens.require_open (parser);
24992 /* Peek at the next token. */
24993 token = cp_lexer_peek_token (parser->lexer);
24994 if (token->type != CPP_CLOSE_PAREN)
24995 /* Parse the attribute-list. */
24996 attribute_list = cp_parser_gnu_attribute_list (parser);
24997 else
24998 /* If the next token is a `)', then there is no attribute
24999 list. */
25000 attribute_list = NULL;
25002 /* Look for the two `)' tokens. */
25003 if (!inner_parens.require_close (parser))
25004 ok = false;
25005 if (!outer_parens.require_close (parser))
25006 ok = false;
25007 if (!ok)
25008 cp_parser_skip_to_end_of_statement (parser);
25010 /* Add these new attributes to the list. */
25011 attributes = chainon (attributes, attribute_list);
25014 return attributes;
25017 /* Parse a GNU attribute-list.
25019 attribute-list:
25020 attribute
25021 attribute-list , attribute
25023 attribute:
25024 identifier
25025 identifier ( identifier )
25026 identifier ( identifier , expression-list )
25027 identifier ( expression-list )
25029 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
25030 to an attribute. The TREE_PURPOSE of each node is the identifier
25031 indicating which attribute is in use. The TREE_VALUE represents
25032 the arguments, if any. */
25034 static tree
25035 cp_parser_gnu_attribute_list (cp_parser* parser)
25037 tree attribute_list = NULL_TREE;
25038 bool save_translate_strings_p = parser->translate_strings_p;
25040 parser->translate_strings_p = false;
25041 while (true)
25043 cp_token *token;
25044 tree identifier;
25045 tree attribute;
25047 /* Look for the identifier. We also allow keywords here; for
25048 example `__attribute__ ((const))' is legal. */
25049 token = cp_lexer_peek_token (parser->lexer);
25050 if (token->type == CPP_NAME
25051 || token->type == CPP_KEYWORD)
25053 tree arguments = NULL_TREE;
25055 /* Consume the token, but save it since we need it for the
25056 SIMD enabled function parsing. */
25057 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
25059 /* Save away the identifier that indicates which attribute
25060 this is. */
25061 identifier = (token->type == CPP_KEYWORD)
25062 /* For keywords, use the canonical spelling, not the
25063 parsed identifier. */
25064 ? ridpointers[(int) token->keyword]
25065 : id_token->u.value;
25067 identifier = canonicalize_attr_name (identifier);
25068 attribute = build_tree_list (identifier, NULL_TREE);
25070 /* Peek at the next token. */
25071 token = cp_lexer_peek_token (parser->lexer);
25072 /* If it's an `(', then parse the attribute arguments. */
25073 if (token->type == CPP_OPEN_PAREN)
25075 vec<tree, va_gc> *vec;
25076 int attr_flag = (attribute_takes_identifier_p (identifier)
25077 ? id_attr : normal_attr);
25078 if (is_cilkplus_vector_p (identifier))
25080 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25081 continue;
25083 else
25084 vec = cp_parser_parenthesized_expression_list
25085 (parser, attr_flag, /*cast_p=*/false,
25086 /*allow_expansion_p=*/false,
25087 /*non_constant_p=*/NULL);
25088 if (vec == NULL)
25089 arguments = error_mark_node;
25090 else
25092 arguments = build_tree_list_vec (vec);
25093 release_tree_vector (vec);
25095 /* Save the arguments away. */
25096 TREE_VALUE (attribute) = arguments;
25098 else if (is_cilkplus_vector_p (identifier))
25100 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
25101 continue;
25104 if (arguments != error_mark_node)
25106 /* Add this attribute to the list. */
25107 TREE_CHAIN (attribute) = attribute_list;
25108 attribute_list = attribute;
25111 token = cp_lexer_peek_token (parser->lexer);
25113 /* Now, look for more attributes. If the next token isn't a
25114 `,', we're done. */
25115 if (token->type != CPP_COMMA)
25116 break;
25118 /* Consume the comma and keep going. */
25119 cp_lexer_consume_token (parser->lexer);
25121 parser->translate_strings_p = save_translate_strings_p;
25123 /* We built up the list in reverse order. */
25124 return nreverse (attribute_list);
25127 /* Parse a standard C++11 attribute.
25129 The returned representation is a TREE_LIST which TREE_PURPOSE is
25130 the scoped name of the attribute, and the TREE_VALUE is its
25131 arguments list.
25133 Note that the scoped name of the attribute is itself a TREE_LIST
25134 which TREE_PURPOSE is the namespace of the attribute, and
25135 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
25136 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
25137 and which TREE_PURPOSE is directly the attribute name.
25139 Clients of the attribute code should use get_attribute_namespace
25140 and get_attribute_name to get the actual namespace and name of
25141 attributes, regardless of their being GNU or C++11 attributes.
25143 attribute:
25144 attribute-token attribute-argument-clause [opt]
25146 attribute-token:
25147 identifier
25148 attribute-scoped-token
25150 attribute-scoped-token:
25151 attribute-namespace :: identifier
25153 attribute-namespace:
25154 identifier
25156 attribute-argument-clause:
25157 ( balanced-token-seq )
25159 balanced-token-seq:
25160 balanced-token [opt]
25161 balanced-token-seq balanced-token
25163 balanced-token:
25164 ( balanced-token-seq )
25165 [ balanced-token-seq ]
25166 { balanced-token-seq }. */
25168 static tree
25169 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25171 tree attribute, attr_id = NULL_TREE, arguments;
25172 cp_token *token;
25174 /* First, parse name of the attribute, a.k.a attribute-token. */
25176 token = cp_lexer_peek_token (parser->lexer);
25177 if (token->type == CPP_NAME)
25178 attr_id = token->u.value;
25179 else if (token->type == CPP_KEYWORD)
25180 attr_id = ridpointers[(int) token->keyword];
25181 else if (token->flags & NAMED_OP)
25182 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25184 if (attr_id == NULL_TREE)
25185 return NULL_TREE;
25187 cp_lexer_consume_token (parser->lexer);
25189 token = cp_lexer_peek_token (parser->lexer);
25190 if (token->type == CPP_SCOPE)
25192 /* We are seeing a scoped attribute token. */
25194 cp_lexer_consume_token (parser->lexer);
25195 if (attr_ns)
25196 error_at (token->location, "attribute using prefix used together "
25197 "with scoped attribute token");
25198 attr_ns = attr_id;
25200 token = cp_lexer_consume_token (parser->lexer);
25201 if (token->type == CPP_NAME)
25202 attr_id = token->u.value;
25203 else if (token->type == CPP_KEYWORD)
25204 attr_id = ridpointers[(int) token->keyword];
25205 else if (token->flags & NAMED_OP)
25206 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25207 else
25209 error_at (token->location,
25210 "expected an identifier for the attribute name");
25211 return error_mark_node;
25214 attr_id = canonicalize_attr_name (attr_id);
25215 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25216 NULL_TREE);
25217 token = cp_lexer_peek_token (parser->lexer);
25219 else if (attr_ns)
25220 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25221 NULL_TREE);
25222 else
25224 attr_id = canonicalize_attr_name (attr_id);
25225 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25226 NULL_TREE);
25227 /* C++11 noreturn attribute is equivalent to GNU's. */
25228 if (is_attribute_p ("noreturn", attr_id))
25229 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25230 /* C++14 deprecated attribute is equivalent to GNU's. */
25231 else if (is_attribute_p ("deprecated", attr_id))
25232 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25233 /* C++17 fallthrough attribute is equivalent to GNU's. */
25234 else if (is_attribute_p ("fallthrough", attr_id))
25235 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25236 /* Transactional Memory TS optimize_for_synchronized attribute is
25237 equivalent to GNU transaction_callable. */
25238 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25239 TREE_PURPOSE (attribute)
25240 = get_identifier ("transaction_callable");
25241 /* Transactional Memory attributes are GNU attributes. */
25242 else if (tm_attr_to_mask (attr_id))
25243 TREE_PURPOSE (attribute) = attr_id;
25246 /* Now parse the optional argument clause of the attribute. */
25248 if (token->type != CPP_OPEN_PAREN)
25249 return attribute;
25252 vec<tree, va_gc> *vec;
25253 int attr_flag = normal_attr;
25255 if (attr_ns == get_identifier ("gnu")
25256 && attribute_takes_identifier_p (attr_id))
25257 /* A GNU attribute that takes an identifier in parameter. */
25258 attr_flag = id_attr;
25260 vec = cp_parser_parenthesized_expression_list
25261 (parser, attr_flag, /*cast_p=*/false,
25262 /*allow_expansion_p=*/true,
25263 /*non_constant_p=*/NULL);
25264 if (vec == NULL)
25265 arguments = error_mark_node;
25266 else
25268 arguments = build_tree_list_vec (vec);
25269 release_tree_vector (vec);
25272 if (arguments == error_mark_node)
25273 attribute = error_mark_node;
25274 else
25275 TREE_VALUE (attribute) = arguments;
25278 return attribute;
25281 /* Check that the attribute ATTRIBUTE appears at most once in the
25282 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25283 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25284 isn't implemented yet in GCC. */
25286 static void
25287 cp_parser_check_std_attribute (tree attributes, tree attribute)
25289 if (attributes)
25291 tree name = get_attribute_name (attribute);
25292 if (is_attribute_p ("noreturn", name)
25293 && lookup_attribute ("noreturn", attributes))
25294 error ("attribute %<noreturn%> can appear at most once "
25295 "in an attribute-list");
25296 else if (is_attribute_p ("deprecated", name)
25297 && lookup_attribute ("deprecated", attributes))
25298 error ("attribute %<deprecated%> can appear at most once "
25299 "in an attribute-list");
25303 /* Parse a list of standard C++-11 attributes.
25305 attribute-list:
25306 attribute [opt]
25307 attribute-list , attribute[opt]
25308 attribute ...
25309 attribute-list , attribute ...
25312 static tree
25313 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25315 tree attributes = NULL_TREE, attribute = NULL_TREE;
25316 cp_token *token = NULL;
25318 while (true)
25320 attribute = cp_parser_std_attribute (parser, attr_ns);
25321 if (attribute == error_mark_node)
25322 break;
25323 if (attribute != NULL_TREE)
25325 cp_parser_check_std_attribute (attributes, attribute);
25326 TREE_CHAIN (attribute) = attributes;
25327 attributes = attribute;
25329 token = cp_lexer_peek_token (parser->lexer);
25330 if (token->type == CPP_ELLIPSIS)
25332 cp_lexer_consume_token (parser->lexer);
25333 if (attribute == NULL_TREE)
25334 error_at (token->location,
25335 "expected attribute before %<...%>");
25336 else
25338 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25339 if (pack == error_mark_node)
25340 return error_mark_node;
25341 TREE_VALUE (attribute) = pack;
25343 token = cp_lexer_peek_token (parser->lexer);
25345 if (token->type != CPP_COMMA)
25346 break;
25347 cp_lexer_consume_token (parser->lexer);
25349 attributes = nreverse (attributes);
25350 return attributes;
25353 /* Parse a standard C++-11 attribute specifier.
25355 attribute-specifier:
25356 [ [ attribute-using-prefix [opt] attribute-list ] ]
25357 alignment-specifier
25359 attribute-using-prefix:
25360 using attribute-namespace :
25362 alignment-specifier:
25363 alignas ( type-id ... [opt] )
25364 alignas ( alignment-expression ... [opt] ). */
25366 static tree
25367 cp_parser_std_attribute_spec (cp_parser *parser)
25369 tree attributes = NULL_TREE;
25370 cp_token *token = cp_lexer_peek_token (parser->lexer);
25372 if (token->type == CPP_OPEN_SQUARE
25373 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25375 tree attr_ns = NULL_TREE;
25377 cp_lexer_consume_token (parser->lexer);
25378 cp_lexer_consume_token (parser->lexer);
25380 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25382 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25383 if (token->type == CPP_NAME)
25384 attr_ns = token->u.value;
25385 else if (token->type == CPP_KEYWORD)
25386 attr_ns = ridpointers[(int) token->keyword];
25387 else if (token->flags & NAMED_OP)
25388 attr_ns = get_identifier (cpp_type2name (token->type,
25389 token->flags));
25390 if (attr_ns
25391 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25393 if (cxx_dialect < cxx17
25394 && !in_system_header_at (input_location))
25395 pedwarn (input_location, 0,
25396 "attribute using prefix only available "
25397 "with -std=c++17 or -std=gnu++17");
25399 cp_lexer_consume_token (parser->lexer);
25400 cp_lexer_consume_token (parser->lexer);
25401 cp_lexer_consume_token (parser->lexer);
25403 else
25404 attr_ns = NULL_TREE;
25407 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25409 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25410 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25411 cp_parser_skip_to_end_of_statement (parser);
25412 else
25413 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25414 when we are sure that we have actually parsed them. */
25415 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25417 else
25419 tree alignas_expr;
25421 /* Look for an alignment-specifier. */
25423 token = cp_lexer_peek_token (parser->lexer);
25425 if (token->type != CPP_KEYWORD
25426 || token->keyword != RID_ALIGNAS)
25427 return NULL_TREE;
25429 cp_lexer_consume_token (parser->lexer);
25430 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25432 matching_parens parens;
25433 if (!parens.require_open (parser))
25435 cp_parser_error (parser, "expected %<(%>");
25436 return error_mark_node;
25439 cp_parser_parse_tentatively (parser);
25440 alignas_expr = cp_parser_type_id (parser);
25442 if (!cp_parser_parse_definitely (parser))
25444 alignas_expr = cp_parser_assignment_expression (parser);
25445 if (alignas_expr == error_mark_node)
25446 cp_parser_skip_to_end_of_statement (parser);
25447 if (alignas_expr == NULL_TREE
25448 || alignas_expr == error_mark_node)
25449 return alignas_expr;
25452 alignas_expr = cxx_alignas_expr (alignas_expr);
25453 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25455 /* Handle alignas (pack...). */
25456 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25458 cp_lexer_consume_token (parser->lexer);
25459 alignas_expr = make_pack_expansion (alignas_expr);
25462 /* Something went wrong, so don't build the attribute. */
25463 if (alignas_expr == error_mark_node)
25464 return error_mark_node;
25466 if (!parens.require_close (parser))
25468 cp_parser_error (parser, "expected %<)%>");
25469 return error_mark_node;
25472 /* Build the C++-11 representation of an 'aligned'
25473 attribute. */
25474 attributes =
25475 build_tree_list (build_tree_list (get_identifier ("gnu"),
25476 get_identifier ("aligned")),
25477 alignas_expr);
25480 return attributes;
25483 /* Parse a standard C++-11 attribute-specifier-seq.
25485 attribute-specifier-seq:
25486 attribute-specifier-seq [opt] attribute-specifier
25489 static tree
25490 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25492 tree attr_specs = NULL_TREE;
25493 tree attr_last = NULL_TREE;
25495 while (true)
25497 tree attr_spec = cp_parser_std_attribute_spec (parser);
25498 if (attr_spec == NULL_TREE)
25499 break;
25500 if (attr_spec == error_mark_node)
25501 return error_mark_node;
25503 if (attr_last)
25504 TREE_CHAIN (attr_last) = attr_spec;
25505 else
25506 attr_specs = attr_last = attr_spec;
25507 attr_last = tree_last (attr_last);
25510 return attr_specs;
25513 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25514 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25515 current value of the PEDANTIC flag, regardless of whether or not
25516 the `__extension__' keyword is present. The caller is responsible
25517 for restoring the value of the PEDANTIC flag. */
25519 static bool
25520 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25522 /* Save the old value of the PEDANTIC flag. */
25523 *saved_pedantic = pedantic;
25525 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25527 /* Consume the `__extension__' token. */
25528 cp_lexer_consume_token (parser->lexer);
25529 /* We're not being pedantic while the `__extension__' keyword is
25530 in effect. */
25531 pedantic = 0;
25533 return true;
25536 return false;
25539 /* Parse a label declaration.
25541 label-declaration:
25542 __label__ label-declarator-seq ;
25544 label-declarator-seq:
25545 identifier , label-declarator-seq
25546 identifier */
25548 static void
25549 cp_parser_label_declaration (cp_parser* parser)
25551 /* Look for the `__label__' keyword. */
25552 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25554 while (true)
25556 tree identifier;
25558 /* Look for an identifier. */
25559 identifier = cp_parser_identifier (parser);
25560 /* If we failed, stop. */
25561 if (identifier == error_mark_node)
25562 break;
25563 /* Declare it as a label. */
25564 finish_label_decl (identifier);
25565 /* If the next token is a `;', stop. */
25566 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25567 break;
25568 /* Look for the `,' separating the label declarations. */
25569 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25572 /* Look for the final `;'. */
25573 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25576 // -------------------------------------------------------------------------- //
25577 // Requires Clause
25579 // Parse a requires clause.
25581 // requires-clause:
25582 // 'requires' logical-or-expression
25584 // The required logical-or-expression must be a constant expression. Note
25585 // that we don't check that the expression is constepxr here. We defer until
25586 // we analyze constraints and then, we only check atomic constraints.
25587 static tree
25588 cp_parser_requires_clause (cp_parser *parser)
25590 // Parse the requires clause so that it is not automatically folded.
25591 ++processing_template_decl;
25592 tree expr = cp_parser_binary_expression (parser, false, false,
25593 PREC_NOT_OPERATOR, NULL);
25594 if (check_for_bare_parameter_packs (expr))
25595 expr = error_mark_node;
25596 --processing_template_decl;
25597 return expr;
25600 // Optionally parse a requires clause:
25601 static tree
25602 cp_parser_requires_clause_opt (cp_parser *parser)
25604 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25605 if (tok->keyword != RID_REQUIRES)
25607 if (!flag_concepts && tok->type == CPP_NAME
25608 && tok->u.value == ridpointers[RID_REQUIRES])
25610 error_at (cp_lexer_peek_token (parser->lexer)->location,
25611 "%<requires%> only available with -fconcepts");
25612 /* Parse and discard the requires-clause. */
25613 cp_lexer_consume_token (parser->lexer);
25614 cp_parser_requires_clause (parser);
25616 return NULL_TREE;
25618 cp_lexer_consume_token (parser->lexer);
25619 return cp_parser_requires_clause (parser);
25623 /*---------------------------------------------------------------------------
25624 Requires expressions
25625 ---------------------------------------------------------------------------*/
25627 /* Parse a requires expression
25629 requirement-expression:
25630 'requires' requirement-parameter-list [opt] requirement-body */
25631 static tree
25632 cp_parser_requires_expression (cp_parser *parser)
25634 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25635 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25637 /* A requires-expression shall appear only within a concept
25638 definition or a requires-clause.
25640 TODO: Implement this diagnostic correctly. */
25641 if (!processing_template_decl)
25643 error_at (loc, "a requires expression cannot appear outside a template");
25644 cp_parser_skip_to_end_of_statement (parser);
25645 return error_mark_node;
25648 tree parms, reqs;
25650 /* Local parameters are delared as variables within the scope
25651 of the expression. They are not visible past the end of
25652 the expression. Expressions within the requires-expression
25653 are unevaluated. */
25654 struct scope_sentinel
25656 scope_sentinel ()
25658 ++cp_unevaluated_operand;
25659 begin_scope (sk_block, NULL_TREE);
25662 ~scope_sentinel ()
25664 pop_bindings_and_leave_scope ();
25665 --cp_unevaluated_operand;
25667 } s;
25669 /* Parse the optional parameter list. */
25670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25672 parms = cp_parser_requirement_parameter_list (parser);
25673 if (parms == error_mark_node)
25674 return error_mark_node;
25676 else
25677 parms = NULL_TREE;
25679 /* Parse the requirement body. */
25680 reqs = cp_parser_requirement_body (parser);
25681 if (reqs == error_mark_node)
25682 return error_mark_node;
25685 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25686 the parm chain. */
25687 grokparms (parms, &parms);
25688 return finish_requires_expr (parms, reqs);
25691 /* Parse a parameterized requirement.
25693 requirement-parameter-list:
25694 '(' parameter-declaration-clause ')' */
25695 static tree
25696 cp_parser_requirement_parameter_list (cp_parser *parser)
25698 matching_parens parens;
25699 if (!parens.require_open (parser))
25700 return error_mark_node;
25702 tree parms = cp_parser_parameter_declaration_clause (parser);
25704 if (!parens.require_close (parser))
25705 return error_mark_node;
25707 return parms;
25710 /* Parse the body of a requirement.
25712 requirement-body:
25713 '{' requirement-list '}' */
25714 static tree
25715 cp_parser_requirement_body (cp_parser *parser)
25717 matching_braces braces;
25718 if (!braces.require_open (parser))
25719 return error_mark_node;
25721 tree reqs = cp_parser_requirement_list (parser);
25723 if (!braces.require_close (parser))
25724 return error_mark_node;
25726 return reqs;
25729 /* Parse a list of requirements.
25731 requirement-list:
25732 requirement
25733 requirement-list ';' requirement[opt] */
25734 static tree
25735 cp_parser_requirement_list (cp_parser *parser)
25737 tree result = NULL_TREE;
25738 while (true)
25740 tree req = cp_parser_requirement (parser);
25741 if (req == error_mark_node)
25742 return error_mark_node;
25744 result = tree_cons (NULL_TREE, req, result);
25746 /* If we see a semi-colon, consume it. */
25747 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25748 cp_lexer_consume_token (parser->lexer);
25750 /* Stop processing at the end of the list. */
25751 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25752 break;
25755 /* Reverse the order of requirements so they are analyzed in
25756 declaration order. */
25757 return nreverse (result);
25760 /* Parse a syntactic requirement or type requirement.
25762 requirement:
25763 simple-requirement
25764 compound-requirement
25765 type-requirement
25766 nested-requirement */
25767 static tree
25768 cp_parser_requirement (cp_parser *parser)
25770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25771 return cp_parser_compound_requirement (parser);
25772 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25773 return cp_parser_type_requirement (parser);
25774 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25775 return cp_parser_nested_requirement (parser);
25776 else
25777 return cp_parser_simple_requirement (parser);
25780 /* Parse a simple requirement.
25782 simple-requirement:
25783 expression ';' */
25784 static tree
25785 cp_parser_simple_requirement (cp_parser *parser)
25787 tree expr = cp_parser_expression (parser, NULL, false, false);
25788 if (!expr || expr == error_mark_node)
25789 return error_mark_node;
25791 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25792 return error_mark_node;
25794 return finish_simple_requirement (expr);
25797 /* Parse a type requirement
25799 type-requirement
25800 nested-name-specifier [opt] required-type-name ';'
25802 required-type-name:
25803 type-name
25804 'template' [opt] simple-template-id */
25805 static tree
25806 cp_parser_type_requirement (cp_parser *parser)
25808 cp_lexer_consume_token (parser->lexer);
25810 // Save the scope before parsing name specifiers.
25811 tree saved_scope = parser->scope;
25812 tree saved_object_scope = parser->object_scope;
25813 tree saved_qualifying_scope = parser->qualifying_scope;
25814 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25815 cp_parser_nested_name_specifier_opt (parser,
25816 /*typename_keyword_p=*/true,
25817 /*check_dependency_p=*/false,
25818 /*type_p=*/true,
25819 /*is_declaration=*/false);
25821 tree type;
25822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25824 cp_lexer_consume_token (parser->lexer);
25825 type = cp_parser_template_id (parser,
25826 /*template_keyword_p=*/true,
25827 /*check_dependency=*/false,
25828 /*tag_type=*/none_type,
25829 /*is_declaration=*/false);
25830 type = make_typename_type (parser->scope, type, typename_type,
25831 /*complain=*/tf_error);
25833 else
25834 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25836 if (TREE_CODE (type) == TYPE_DECL)
25837 type = TREE_TYPE (type);
25839 parser->scope = saved_scope;
25840 parser->object_scope = saved_object_scope;
25841 parser->qualifying_scope = saved_qualifying_scope;
25843 if (type == error_mark_node)
25844 cp_parser_skip_to_end_of_statement (parser);
25846 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25847 return error_mark_node;
25848 if (type == error_mark_node)
25849 return error_mark_node;
25851 return finish_type_requirement (type);
25854 /* Parse a compound requirement
25856 compound-requirement:
25857 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25858 static tree
25859 cp_parser_compound_requirement (cp_parser *parser)
25861 /* Parse an expression enclosed in '{ }'s. */
25862 matching_braces braces;
25863 if (!braces.require_open (parser))
25864 return error_mark_node;
25866 tree expr = cp_parser_expression (parser, NULL, false, false);
25867 if (!expr || expr == error_mark_node)
25868 return error_mark_node;
25870 if (!braces.require_close (parser))
25871 return error_mark_node;
25873 /* Parse the optional noexcept. */
25874 bool noexcept_p = false;
25875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25877 cp_lexer_consume_token (parser->lexer);
25878 noexcept_p = true;
25881 /* Parse the optional trailing return type. */
25882 tree type = NULL_TREE;
25883 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25885 cp_lexer_consume_token (parser->lexer);
25886 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25887 parser->in_result_type_constraint_p = true;
25888 type = cp_parser_trailing_type_id (parser);
25889 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25890 if (type == error_mark_node)
25891 return error_mark_node;
25894 return finish_compound_requirement (expr, type, noexcept_p);
25897 /* Parse a nested requirement. This is the same as a requires clause.
25899 nested-requirement:
25900 requires-clause */
25901 static tree
25902 cp_parser_nested_requirement (cp_parser *parser)
25904 cp_lexer_consume_token (parser->lexer);
25905 tree req = cp_parser_requires_clause (parser);
25906 if (req == error_mark_node)
25907 return error_mark_node;
25908 return finish_nested_requirement (req);
25911 /* Support Functions */
25913 /* Return the appropriate prefer_type argument for lookup_name_real based on
25914 tag_type and template_mem_access. */
25916 static inline int
25917 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25919 /* DR 141: When looking in the current enclosing context for a template-name
25920 after -> or ., only consider class templates. */
25921 if (template_mem_access)
25922 return 2;
25923 switch (tag_type)
25925 case none_type: return 0; // No preference.
25926 case scope_type: return 1; // Type or namespace.
25927 default: return 2; // Type only.
25931 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25932 NAME should have one of the representations used for an
25933 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25934 is returned. If PARSER->SCOPE is a dependent type, then a
25935 SCOPE_REF is returned.
25937 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25938 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25939 was formed. Abstractly, such entities should not be passed to this
25940 function, because they do not need to be looked up, but it is
25941 simpler to check for this special case here, rather than at the
25942 call-sites.
25944 In cases not explicitly covered above, this function returns a
25945 DECL, OVERLOAD, or baselink representing the result of the lookup.
25946 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25947 is returned.
25949 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25950 (e.g., "struct") that was used. In that case bindings that do not
25951 refer to types are ignored.
25953 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25954 ignored.
25956 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25957 are ignored.
25959 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25960 types.
25962 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25963 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25964 NULL_TREE otherwise. */
25966 static cp_expr
25967 cp_parser_lookup_name (cp_parser *parser, tree name,
25968 enum tag_types tag_type,
25969 bool is_template,
25970 bool is_namespace,
25971 bool check_dependency,
25972 tree *ambiguous_decls,
25973 location_t name_location)
25975 tree decl;
25976 tree object_type = parser->context->object_type;
25978 /* Assume that the lookup will be unambiguous. */
25979 if (ambiguous_decls)
25980 *ambiguous_decls = NULL_TREE;
25982 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25983 no longer valid. Note that if we are parsing tentatively, and
25984 the parse fails, OBJECT_TYPE will be automatically restored. */
25985 parser->context->object_type = NULL_TREE;
25987 if (name == error_mark_node)
25988 return error_mark_node;
25990 /* A template-id has already been resolved; there is no lookup to
25991 do. */
25992 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25993 return name;
25994 if (BASELINK_P (name))
25996 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25997 == TEMPLATE_ID_EXPR);
25998 return name;
26001 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
26002 it should already have been checked to make sure that the name
26003 used matches the type being destroyed. */
26004 if (TREE_CODE (name) == BIT_NOT_EXPR)
26006 tree type;
26008 /* Figure out to which type this destructor applies. */
26009 if (parser->scope)
26010 type = parser->scope;
26011 else if (object_type)
26012 type = object_type;
26013 else
26014 type = current_class_type;
26015 /* If that's not a class type, there is no destructor. */
26016 if (!type || !CLASS_TYPE_P (type))
26017 return error_mark_node;
26019 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
26020 lazily_declare_fn (sfk_destructor, type);
26022 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
26023 return dtor;
26025 return error_mark_node;
26028 /* By this point, the NAME should be an ordinary identifier. If
26029 the id-expression was a qualified name, the qualifying scope is
26030 stored in PARSER->SCOPE at this point. */
26031 gcc_assert (identifier_p (name));
26033 /* Perform the lookup. */
26034 if (parser->scope)
26036 bool dependent_p;
26038 if (parser->scope == error_mark_node)
26039 return error_mark_node;
26041 /* If the SCOPE is dependent, the lookup must be deferred until
26042 the template is instantiated -- unless we are explicitly
26043 looking up names in uninstantiated templates. Even then, we
26044 cannot look up the name if the scope is not a class type; it
26045 might, for example, be a template type parameter. */
26046 dependent_p = (TYPE_P (parser->scope)
26047 && dependent_scope_p (parser->scope));
26048 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
26049 && dependent_p)
26050 /* Defer lookup. */
26051 decl = error_mark_node;
26052 else
26054 tree pushed_scope = NULL_TREE;
26056 /* If PARSER->SCOPE is a dependent type, then it must be a
26057 class type, and we must not be checking dependencies;
26058 otherwise, we would have processed this lookup above. So
26059 that PARSER->SCOPE is not considered a dependent base by
26060 lookup_member, we must enter the scope here. */
26061 if (dependent_p)
26062 pushed_scope = push_scope (parser->scope);
26064 /* If the PARSER->SCOPE is a template specialization, it
26065 may be instantiated during name lookup. In that case,
26066 errors may be issued. Even if we rollback the current
26067 tentative parse, those errors are valid. */
26068 decl = lookup_qualified_name (parser->scope, name,
26069 prefer_type_arg (tag_type),
26070 /*complain=*/true);
26072 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
26073 lookup result and the nested-name-specifier nominates a class C:
26074 * if the name specified after the nested-name-specifier, when
26075 looked up in C, is the injected-class-name of C (Clause 9), or
26076 * if the name specified after the nested-name-specifier is the
26077 same as the identifier or the simple-template-id's template-
26078 name in the last component of the nested-name-specifier,
26079 the name is instead considered to name the constructor of
26080 class C. [ Note: for example, the constructor is not an
26081 acceptable lookup result in an elaborated-type-specifier so
26082 the constructor would not be used in place of the
26083 injected-class-name. --end note ] Such a constructor name
26084 shall be used only in the declarator-id of a declaration that
26085 names a constructor or in a using-declaration. */
26086 if (tag_type == none_type
26087 && DECL_SELF_REFERENCE_P (decl)
26088 && same_type_p (DECL_CONTEXT (decl), parser->scope))
26089 decl = lookup_qualified_name (parser->scope, ctor_identifier,
26090 prefer_type_arg (tag_type),
26091 /*complain=*/true);
26093 /* If we have a single function from a using decl, pull it out. */
26094 if (TREE_CODE (decl) == OVERLOAD
26095 && !really_overloaded_fn (decl))
26096 decl = OVL_FUNCTION (decl);
26098 if (pushed_scope)
26099 pop_scope (pushed_scope);
26102 /* If the scope is a dependent type and either we deferred lookup or
26103 we did lookup but didn't find the name, rememeber the name. */
26104 if (decl == error_mark_node && TYPE_P (parser->scope)
26105 && dependent_type_p (parser->scope))
26107 if (tag_type)
26109 tree type;
26111 /* The resolution to Core Issue 180 says that `struct
26112 A::B' should be considered a type-name, even if `A'
26113 is dependent. */
26114 type = make_typename_type (parser->scope, name, tag_type,
26115 /*complain=*/tf_error);
26116 if (type != error_mark_node)
26117 decl = TYPE_NAME (type);
26119 else if (is_template
26120 && (cp_parser_next_token_ends_template_argument_p (parser)
26121 || cp_lexer_next_token_is (parser->lexer,
26122 CPP_CLOSE_PAREN)))
26123 decl = make_unbound_class_template (parser->scope,
26124 name, NULL_TREE,
26125 /*complain=*/tf_error);
26126 else
26127 decl = build_qualified_name (/*type=*/NULL_TREE,
26128 parser->scope, name,
26129 is_template);
26131 parser->qualifying_scope = parser->scope;
26132 parser->object_scope = NULL_TREE;
26134 else if (object_type)
26136 /* Look up the name in the scope of the OBJECT_TYPE, unless the
26137 OBJECT_TYPE is not a class. */
26138 if (CLASS_TYPE_P (object_type))
26139 /* If the OBJECT_TYPE is a template specialization, it may
26140 be instantiated during name lookup. In that case, errors
26141 may be issued. Even if we rollback the current tentative
26142 parse, those errors are valid. */
26143 decl = lookup_member (object_type,
26144 name,
26145 /*protect=*/0,
26146 prefer_type_arg (tag_type),
26147 tf_warning_or_error);
26148 else
26149 decl = NULL_TREE;
26151 if (!decl)
26152 /* Look it up in the enclosing context. DR 141: When looking for a
26153 template-name after -> or ., only consider class templates. */
26154 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
26155 /*nonclass=*/0,
26156 /*block_p=*/true, is_namespace, 0);
26157 if (object_type == unknown_type_node)
26158 /* The object is type-dependent, so we can't look anything up; we used
26159 this to get the DR 141 behavior. */
26160 object_type = NULL_TREE;
26161 parser->object_scope = object_type;
26162 parser->qualifying_scope = NULL_TREE;
26164 else
26166 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26167 /*nonclass=*/0,
26168 /*block_p=*/true, is_namespace, 0);
26169 parser->qualifying_scope = NULL_TREE;
26170 parser->object_scope = NULL_TREE;
26173 /* If the lookup failed, let our caller know. */
26174 if (!decl || decl == error_mark_node)
26175 return error_mark_node;
26177 /* Pull out the template from an injected-class-name (or multiple). */
26178 if (is_template)
26179 decl = maybe_get_template_decl_from_type_decl (decl);
26181 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26182 if (TREE_CODE (decl) == TREE_LIST)
26184 if (ambiguous_decls)
26185 *ambiguous_decls = decl;
26186 /* The error message we have to print is too complicated for
26187 cp_parser_error, so we incorporate its actions directly. */
26188 if (!cp_parser_simulate_error (parser))
26190 error_at (name_location, "reference to %qD is ambiguous",
26191 name);
26192 print_candidates (decl);
26194 return error_mark_node;
26197 gcc_assert (DECL_P (decl)
26198 || TREE_CODE (decl) == OVERLOAD
26199 || TREE_CODE (decl) == SCOPE_REF
26200 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26201 || BASELINK_P (decl));
26203 /* If we have resolved the name of a member declaration, check to
26204 see if the declaration is accessible. When the name resolves to
26205 set of overloaded functions, accessibility is checked when
26206 overload resolution is done.
26208 During an explicit instantiation, access is not checked at all,
26209 as per [temp.explicit]. */
26210 if (DECL_P (decl))
26211 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26213 maybe_record_typedef_use (decl);
26215 return cp_expr (decl, name_location);
26218 /* Like cp_parser_lookup_name, but for use in the typical case where
26219 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26220 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26222 static tree
26223 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26225 return cp_parser_lookup_name (parser, name,
26226 none_type,
26227 /*is_template=*/false,
26228 /*is_namespace=*/false,
26229 /*check_dependency=*/true,
26230 /*ambiguous_decls=*/NULL,
26231 location);
26234 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26235 the current context, return the TYPE_DECL. If TAG_NAME_P is
26236 true, the DECL indicates the class being defined in a class-head,
26237 or declared in an elaborated-type-specifier.
26239 Otherwise, return DECL. */
26241 static tree
26242 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26244 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26245 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26247 struct A {
26248 template <typename T> struct B;
26251 template <typename T> struct A::B {};
26253 Similarly, in an elaborated-type-specifier:
26255 namespace N { struct X{}; }
26257 struct A {
26258 template <typename T> friend struct N::X;
26261 However, if the DECL refers to a class type, and we are in
26262 the scope of the class, then the name lookup automatically
26263 finds the TYPE_DECL created by build_self_reference rather
26264 than a TEMPLATE_DECL. For example, in:
26266 template <class T> struct S {
26267 S s;
26270 there is no need to handle such case. */
26272 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26273 return DECL_TEMPLATE_RESULT (decl);
26275 return decl;
26278 /* If too many, or too few, template-parameter lists apply to the
26279 declarator, issue an error message. Returns TRUE if all went well,
26280 and FALSE otherwise. */
26282 static bool
26283 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26284 cp_declarator *declarator,
26285 location_t declarator_location)
26287 switch (declarator->kind)
26289 case cdk_id:
26291 unsigned num_templates = 0;
26292 tree scope = declarator->u.id.qualifying_scope;
26294 if (scope)
26295 num_templates = num_template_headers_for_class (scope);
26296 else if (TREE_CODE (declarator->u.id.unqualified_name)
26297 == TEMPLATE_ID_EXPR)
26298 /* If the DECLARATOR has the form `X<y>' then it uses one
26299 additional level of template parameters. */
26300 ++num_templates;
26302 return cp_parser_check_template_parameters
26303 (parser, num_templates, declarator_location, declarator);
26306 case cdk_function:
26307 case cdk_array:
26308 case cdk_pointer:
26309 case cdk_reference:
26310 case cdk_ptrmem:
26311 return (cp_parser_check_declarator_template_parameters
26312 (parser, declarator->declarator, declarator_location));
26314 case cdk_decomp:
26315 case cdk_error:
26316 return true;
26318 default:
26319 gcc_unreachable ();
26321 return false;
26324 /* NUM_TEMPLATES were used in the current declaration. If that is
26325 invalid, return FALSE and issue an error messages. Otherwise,
26326 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26327 declarator and we can print more accurate diagnostics. */
26329 static bool
26330 cp_parser_check_template_parameters (cp_parser* parser,
26331 unsigned num_templates,
26332 location_t location,
26333 cp_declarator *declarator)
26335 /* If there are the same number of template classes and parameter
26336 lists, that's OK. */
26337 if (parser->num_template_parameter_lists == num_templates)
26338 return true;
26339 /* If there are more, but only one more, then we are referring to a
26340 member template. That's OK too. */
26341 if (parser->num_template_parameter_lists == num_templates + 1)
26342 return true;
26343 /* If there are more template classes than parameter lists, we have
26344 something like:
26346 template <class T> void S<T>::R<T>::f (); */
26347 if (parser->num_template_parameter_lists < num_templates)
26349 if (declarator && !current_function_decl)
26350 error_at (location, "specializing member %<%T::%E%> "
26351 "requires %<template<>%> syntax",
26352 declarator->u.id.qualifying_scope,
26353 declarator->u.id.unqualified_name);
26354 else if (declarator)
26355 error_at (location, "invalid declaration of %<%T::%E%>",
26356 declarator->u.id.qualifying_scope,
26357 declarator->u.id.unqualified_name);
26358 else
26359 error_at (location, "too few template-parameter-lists");
26360 return false;
26362 /* Otherwise, there are too many template parameter lists. We have
26363 something like:
26365 template <class T> template <class U> void S::f(); */
26366 error_at (location, "too many template-parameter-lists");
26367 return false;
26370 /* Parse an optional `::' token indicating that the following name is
26371 from the global namespace. If so, PARSER->SCOPE is set to the
26372 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26373 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26374 Returns the new value of PARSER->SCOPE, if the `::' token is
26375 present, and NULL_TREE otherwise. */
26377 static tree
26378 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26380 cp_token *token;
26382 /* Peek at the next token. */
26383 token = cp_lexer_peek_token (parser->lexer);
26384 /* If we're looking at a `::' token then we're starting from the
26385 global namespace, not our current location. */
26386 if (token->type == CPP_SCOPE)
26388 /* Consume the `::' token. */
26389 cp_lexer_consume_token (parser->lexer);
26390 /* Set the SCOPE so that we know where to start the lookup. */
26391 parser->scope = global_namespace;
26392 parser->qualifying_scope = global_namespace;
26393 parser->object_scope = NULL_TREE;
26395 return parser->scope;
26397 else if (!current_scope_valid_p)
26399 parser->scope = NULL_TREE;
26400 parser->qualifying_scope = NULL_TREE;
26401 parser->object_scope = NULL_TREE;
26404 return NULL_TREE;
26407 /* Returns TRUE if the upcoming token sequence is the start of a
26408 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26409 declarator is preceded by the `friend' specifier. */
26411 static bool
26412 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26414 bool constructor_p;
26415 bool outside_class_specifier_p;
26416 tree nested_name_specifier;
26417 cp_token *next_token;
26419 /* The common case is that this is not a constructor declarator, so
26420 try to avoid doing lots of work if at all possible. It's not
26421 valid declare a constructor at function scope. */
26422 if (parser->in_function_body)
26423 return false;
26424 /* And only certain tokens can begin a constructor declarator. */
26425 next_token = cp_lexer_peek_token (parser->lexer);
26426 if (next_token->type != CPP_NAME
26427 && next_token->type != CPP_SCOPE
26428 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26429 && next_token->type != CPP_TEMPLATE_ID)
26430 return false;
26432 /* Parse tentatively; we are going to roll back all of the tokens
26433 consumed here. */
26434 cp_parser_parse_tentatively (parser);
26435 /* Assume that we are looking at a constructor declarator. */
26436 constructor_p = true;
26438 /* Look for the optional `::' operator. */
26439 cp_parser_global_scope_opt (parser,
26440 /*current_scope_valid_p=*/false);
26441 /* Look for the nested-name-specifier. */
26442 nested_name_specifier
26443 = (cp_parser_nested_name_specifier_opt (parser,
26444 /*typename_keyword_p=*/false,
26445 /*check_dependency_p=*/false,
26446 /*type_p=*/false,
26447 /*is_declaration=*/false));
26449 outside_class_specifier_p = (!at_class_scope_p ()
26450 || !TYPE_BEING_DEFINED (current_class_type)
26451 || friend_p);
26453 /* Outside of a class-specifier, there must be a
26454 nested-name-specifier. Except in C++17 mode, where we
26455 might be declaring a guiding declaration. */
26456 if (!nested_name_specifier && outside_class_specifier_p
26457 && cxx_dialect < cxx17)
26458 constructor_p = false;
26459 else if (nested_name_specifier == error_mark_node)
26460 constructor_p = false;
26462 /* If we have a class scope, this is easy; DR 147 says that S::S always
26463 names the constructor, and no other qualified name could. */
26464 if (constructor_p && nested_name_specifier
26465 && CLASS_TYPE_P (nested_name_specifier))
26467 tree id = cp_parser_unqualified_id (parser,
26468 /*template_keyword_p=*/false,
26469 /*check_dependency_p=*/false,
26470 /*declarator_p=*/true,
26471 /*optional_p=*/false);
26472 if (is_overloaded_fn (id))
26473 id = DECL_NAME (get_first_fn (id));
26474 if (!constructor_name_p (id, nested_name_specifier))
26475 constructor_p = false;
26477 /* If we still think that this might be a constructor-declarator,
26478 look for a class-name. */
26479 else if (constructor_p)
26481 /* If we have:
26483 template <typename T> struct S {
26484 S();
26487 we must recognize that the nested `S' names a class. */
26488 if (cxx_dialect >= cxx17)
26489 cp_parser_parse_tentatively (parser);
26491 tree type_decl;
26492 type_decl = cp_parser_class_name (parser,
26493 /*typename_keyword_p=*/false,
26494 /*template_keyword_p=*/false,
26495 none_type,
26496 /*check_dependency_p=*/false,
26497 /*class_head_p=*/false,
26498 /*is_declaration=*/false);
26500 if (cxx_dialect >= cxx17
26501 && !cp_parser_parse_definitely (parser))
26503 type_decl = NULL_TREE;
26504 tree tmpl = cp_parser_template_name (parser,
26505 /*template_keyword*/false,
26506 /*check_dependency_p*/false,
26507 /*is_declaration*/false,
26508 none_type,
26509 /*is_identifier*/NULL);
26510 if (DECL_CLASS_TEMPLATE_P (tmpl)
26511 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26512 /* It's a deduction guide, return true. */;
26513 else
26514 cp_parser_simulate_error (parser);
26517 /* If there was no class-name, then this is not a constructor.
26518 Otherwise, if we are in a class-specifier and we aren't
26519 handling a friend declaration, check that its type matches
26520 current_class_type (c++/38313). Note: error_mark_node
26521 is left alone for error recovery purposes. */
26522 constructor_p = (!cp_parser_error_occurred (parser)
26523 && (outside_class_specifier_p
26524 || type_decl == NULL_TREE
26525 || type_decl == error_mark_node
26526 || same_type_p (current_class_type,
26527 TREE_TYPE (type_decl))));
26529 /* If we're still considering a constructor, we have to see a `(',
26530 to begin the parameter-declaration-clause, followed by either a
26531 `)', an `...', or a decl-specifier. We need to check for a
26532 type-specifier to avoid being fooled into thinking that:
26534 S (f) (int);
26536 is a constructor. (It is actually a function named `f' that
26537 takes one parameter (of type `int') and returns a value of type
26538 `S'. */
26539 if (constructor_p
26540 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26541 constructor_p = false;
26543 if (constructor_p
26544 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26545 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26546 /* A parameter declaration begins with a decl-specifier,
26547 which is either the "attribute" keyword, a storage class
26548 specifier, or (usually) a type-specifier. */
26549 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26551 tree type;
26552 tree pushed_scope = NULL_TREE;
26553 unsigned saved_num_template_parameter_lists;
26555 /* Names appearing in the type-specifier should be looked up
26556 in the scope of the class. */
26557 if (current_class_type)
26558 type = NULL_TREE;
26559 else if (type_decl)
26561 type = TREE_TYPE (type_decl);
26562 if (TREE_CODE (type) == TYPENAME_TYPE)
26564 type = resolve_typename_type (type,
26565 /*only_current_p=*/false);
26566 if (TREE_CODE (type) == TYPENAME_TYPE)
26568 cp_parser_abort_tentative_parse (parser);
26569 return false;
26572 pushed_scope = push_scope (type);
26575 /* Inside the constructor parameter list, surrounding
26576 template-parameter-lists do not apply. */
26577 saved_num_template_parameter_lists
26578 = parser->num_template_parameter_lists;
26579 parser->num_template_parameter_lists = 0;
26581 /* Look for the type-specifier. */
26582 cp_parser_type_specifier (parser,
26583 CP_PARSER_FLAGS_NONE,
26584 /*decl_specs=*/NULL,
26585 /*is_declarator=*/true,
26586 /*declares_class_or_enum=*/NULL,
26587 /*is_cv_qualifier=*/NULL);
26589 parser->num_template_parameter_lists
26590 = saved_num_template_parameter_lists;
26592 /* Leave the scope of the class. */
26593 if (pushed_scope)
26594 pop_scope (pushed_scope);
26596 constructor_p = !cp_parser_error_occurred (parser);
26600 /* We did not really want to consume any tokens. */
26601 cp_parser_abort_tentative_parse (parser);
26603 return constructor_p;
26606 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26607 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26608 they must be performed once we are in the scope of the function.
26610 Returns the function defined. */
26612 static tree
26613 cp_parser_function_definition_from_specifiers_and_declarator
26614 (cp_parser* parser,
26615 cp_decl_specifier_seq *decl_specifiers,
26616 tree attributes,
26617 const cp_declarator *declarator)
26619 tree fn;
26620 bool success_p;
26622 /* Begin the function-definition. */
26623 success_p = start_function (decl_specifiers, declarator, attributes);
26625 /* The things we're about to see are not directly qualified by any
26626 template headers we've seen thus far. */
26627 reset_specialization ();
26629 /* If there were names looked up in the decl-specifier-seq that we
26630 did not check, check them now. We must wait until we are in the
26631 scope of the function to perform the checks, since the function
26632 might be a friend. */
26633 perform_deferred_access_checks (tf_warning_or_error);
26635 if (success_p)
26637 cp_finalize_omp_declare_simd (parser, current_function_decl);
26638 parser->omp_declare_simd = NULL;
26639 cp_finalize_oacc_routine (parser, current_function_decl, true);
26640 parser->oacc_routine = NULL;
26643 if (!success_p)
26645 /* Skip the entire function. */
26646 cp_parser_skip_to_end_of_block_or_statement (parser);
26647 fn = error_mark_node;
26649 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26651 /* Seen already, skip it. An error message has already been output. */
26652 cp_parser_skip_to_end_of_block_or_statement (parser);
26653 fn = current_function_decl;
26654 current_function_decl = NULL_TREE;
26655 /* If this is a function from a class, pop the nested class. */
26656 if (current_class_name)
26657 pop_nested_class ();
26659 else
26661 timevar_id_t tv;
26662 if (DECL_DECLARED_INLINE_P (current_function_decl))
26663 tv = TV_PARSE_INLINE;
26664 else
26665 tv = TV_PARSE_FUNC;
26666 timevar_push (tv);
26667 fn = cp_parser_function_definition_after_declarator (parser,
26668 /*inline_p=*/false);
26669 timevar_pop (tv);
26672 return fn;
26675 /* Parse the part of a function-definition that follows the
26676 declarator. INLINE_P is TRUE iff this function is an inline
26677 function defined within a class-specifier.
26679 Returns the function defined. */
26681 static tree
26682 cp_parser_function_definition_after_declarator (cp_parser* parser,
26683 bool inline_p)
26685 tree fn;
26686 bool saved_in_unbraced_linkage_specification_p;
26687 bool saved_in_function_body;
26688 unsigned saved_num_template_parameter_lists;
26689 cp_token *token;
26690 bool fully_implicit_function_template_p
26691 = parser->fully_implicit_function_template_p;
26692 parser->fully_implicit_function_template_p = false;
26693 tree implicit_template_parms
26694 = parser->implicit_template_parms;
26695 parser->implicit_template_parms = 0;
26696 cp_binding_level* implicit_template_scope
26697 = parser->implicit_template_scope;
26698 parser->implicit_template_scope = 0;
26700 saved_in_function_body = parser->in_function_body;
26701 parser->in_function_body = true;
26702 /* If the next token is `return', then the code may be trying to
26703 make use of the "named return value" extension that G++ used to
26704 support. */
26705 token = cp_lexer_peek_token (parser->lexer);
26706 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26708 /* Consume the `return' keyword. */
26709 cp_lexer_consume_token (parser->lexer);
26710 /* Look for the identifier that indicates what value is to be
26711 returned. */
26712 cp_parser_identifier (parser);
26713 /* Issue an error message. */
26714 error_at (token->location,
26715 "named return values are no longer supported");
26716 /* Skip tokens until we reach the start of the function body. */
26717 while (true)
26719 cp_token *token = cp_lexer_peek_token (parser->lexer);
26720 if (token->type == CPP_OPEN_BRACE
26721 || token->type == CPP_EOF
26722 || token->type == CPP_PRAGMA_EOL)
26723 break;
26724 cp_lexer_consume_token (parser->lexer);
26727 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26728 anything declared inside `f'. */
26729 saved_in_unbraced_linkage_specification_p
26730 = parser->in_unbraced_linkage_specification_p;
26731 parser->in_unbraced_linkage_specification_p = false;
26732 /* Inside the function, surrounding template-parameter-lists do not
26733 apply. */
26734 saved_num_template_parameter_lists
26735 = parser->num_template_parameter_lists;
26736 parser->num_template_parameter_lists = 0;
26738 /* If the next token is `try', `__transaction_atomic', or
26739 `__transaction_relaxed`, then we are looking at either function-try-block
26740 or function-transaction-block. Note that all of these include the
26741 function-body. */
26742 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26743 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
26744 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26745 RID_TRANSACTION_RELAXED))
26746 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
26747 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26748 cp_parser_function_try_block (parser);
26749 else
26750 cp_parser_ctor_initializer_opt_and_function_body
26751 (parser, /*in_function_try_block=*/false);
26753 /* Finish the function. */
26754 fn = finish_function (inline_p);
26755 /* Generate code for it, if necessary. */
26756 expand_or_defer_fn (fn);
26757 /* Restore the saved values. */
26758 parser->in_unbraced_linkage_specification_p
26759 = saved_in_unbraced_linkage_specification_p;
26760 parser->num_template_parameter_lists
26761 = saved_num_template_parameter_lists;
26762 parser->in_function_body = saved_in_function_body;
26764 parser->fully_implicit_function_template_p
26765 = fully_implicit_function_template_p;
26766 parser->implicit_template_parms
26767 = implicit_template_parms;
26768 parser->implicit_template_scope
26769 = implicit_template_scope;
26771 if (parser->fully_implicit_function_template_p)
26772 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26774 return fn;
26777 /* Parse a template-declaration body (following argument list). */
26779 static void
26780 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26781 tree parameter_list,
26782 bool member_p)
26784 tree decl = NULL_TREE;
26785 bool friend_p = false;
26787 /* We just processed one more parameter list. */
26788 ++parser->num_template_parameter_lists;
26790 /* Get the deferred access checks from the parameter list. These
26791 will be checked once we know what is being declared, as for a
26792 member template the checks must be performed in the scope of the
26793 class containing the member. */
26794 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26796 /* Tentatively parse for a new template parameter list, which can either be
26797 the template keyword or a template introduction. */
26798 if (cp_parser_template_declaration_after_export (parser, member_p))
26799 /* OK */;
26800 else if (cxx_dialect >= cxx11
26801 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26802 decl = cp_parser_alias_declaration (parser);
26803 else
26805 /* There are no access checks when parsing a template, as we do not
26806 know if a specialization will be a friend. */
26807 push_deferring_access_checks (dk_no_check);
26808 cp_token *token = cp_lexer_peek_token (parser->lexer);
26809 decl = cp_parser_single_declaration (parser,
26810 checks,
26811 member_p,
26812 /*explicit_specialization_p=*/false,
26813 &friend_p);
26814 pop_deferring_access_checks ();
26816 /* If this is a member template declaration, let the front
26817 end know. */
26818 if (member_p && !friend_p && decl)
26820 if (TREE_CODE (decl) == TYPE_DECL)
26821 cp_parser_check_access_in_redeclaration (decl, token->location);
26823 decl = finish_member_template_decl (decl);
26825 else if (friend_p && decl
26826 && DECL_DECLARES_TYPE_P (decl))
26827 make_friend_class (current_class_type, TREE_TYPE (decl),
26828 /*complain=*/true);
26830 /* We are done with the current parameter list. */
26831 --parser->num_template_parameter_lists;
26833 pop_deferring_access_checks ();
26835 /* Finish up. */
26836 finish_template_decl (parameter_list);
26838 /* Check the template arguments for a literal operator template. */
26839 if (decl
26840 && DECL_DECLARES_FUNCTION_P (decl)
26841 && UDLIT_OPER_P (DECL_NAME (decl)))
26843 bool ok = true;
26844 if (parameter_list == NULL_TREE)
26845 ok = false;
26846 else
26848 int num_parms = TREE_VEC_LENGTH (parameter_list);
26849 if (num_parms == 1)
26851 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26852 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26853 if (TREE_TYPE (parm) != char_type_node
26854 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26855 ok = false;
26857 else if (num_parms == 2 && cxx_dialect >= cxx14)
26859 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26860 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26861 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26862 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26863 if (parm == error_mark_node
26864 || TREE_TYPE (parm) != TREE_TYPE (type)
26865 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26866 ok = false;
26868 else
26869 ok = false;
26871 if (!ok)
26873 if (cxx_dialect >= cxx14)
26874 error ("literal operator template %qD has invalid parameter list."
26875 " Expected non-type template argument pack <char...>"
26876 " or <typename CharT, CharT...>",
26877 decl);
26878 else
26879 error ("literal operator template %qD has invalid parameter list."
26880 " Expected non-type template argument pack <char...>",
26881 decl);
26885 /* Register member declarations. */
26886 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26887 finish_member_declaration (decl);
26888 /* If DECL is a function template, we must return to parse it later.
26889 (Even though there is no definition, there might be default
26890 arguments that need handling.) */
26891 if (member_p && decl
26892 && DECL_DECLARES_FUNCTION_P (decl))
26893 vec_safe_push (unparsed_funs_with_definitions, decl);
26896 /* Parse a template introduction header for a template-declaration. Returns
26897 false if tentative parse fails. */
26899 static bool
26900 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26902 cp_parser_parse_tentatively (parser);
26904 tree saved_scope = parser->scope;
26905 tree saved_object_scope = parser->object_scope;
26906 tree saved_qualifying_scope = parser->qualifying_scope;
26908 /* Look for the optional `::' operator. */
26909 cp_parser_global_scope_opt (parser,
26910 /*current_scope_valid_p=*/false);
26911 /* Look for the nested-name-specifier. */
26912 cp_parser_nested_name_specifier_opt (parser,
26913 /*typename_keyword_p=*/false,
26914 /*check_dependency_p=*/true,
26915 /*type_p=*/false,
26916 /*is_declaration=*/false);
26918 cp_token *token = cp_lexer_peek_token (parser->lexer);
26919 tree concept_name = cp_parser_identifier (parser);
26921 /* Look up the concept for which we will be matching
26922 template parameters. */
26923 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26924 token->location);
26925 parser->scope = saved_scope;
26926 parser->object_scope = saved_object_scope;
26927 parser->qualifying_scope = saved_qualifying_scope;
26929 if (concept_name == error_mark_node)
26930 cp_parser_simulate_error (parser);
26932 /* Look for opening brace for introduction. */
26933 matching_braces braces;
26934 braces.require_open (parser);
26936 if (!cp_parser_parse_definitely (parser))
26937 return false;
26939 push_deferring_access_checks (dk_deferred);
26941 /* Build vector of placeholder parameters and grab
26942 matching identifiers. */
26943 tree introduction_list = cp_parser_introduction_list (parser);
26945 /* The introduction-list shall not be empty. */
26946 int nargs = TREE_VEC_LENGTH (introduction_list);
26947 if (nargs == 0)
26949 error ("empty introduction-list");
26950 return true;
26953 /* Look for closing brace for introduction. */
26954 if (!braces.require_close (parser))
26955 return true;
26957 if (tmpl_decl == error_mark_node)
26959 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26960 token->location);
26961 return true;
26964 /* Build and associate the constraint. */
26965 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26966 if (parms && parms != error_mark_node)
26968 cp_parser_template_declaration_after_parameters (parser, parms,
26969 member_p);
26970 return true;
26973 error_at (token->location, "no matching concept for template-introduction");
26974 return true;
26977 /* Parse a normal template-declaration following the template keyword. */
26979 static void
26980 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26982 tree parameter_list;
26983 bool need_lang_pop;
26984 location_t location = input_location;
26986 /* Look for the `<' token. */
26987 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26988 return;
26989 if (at_class_scope_p () && current_function_decl)
26991 /* 14.5.2.2 [temp.mem]
26993 A local class shall not have member templates. */
26994 error_at (location,
26995 "invalid declaration of member template in local class");
26996 cp_parser_skip_to_end_of_block_or_statement (parser);
26997 return;
26999 /* [temp]
27001 A template ... shall not have C linkage. */
27002 if (current_lang_name == lang_name_c)
27004 error_at (location, "template with C linkage");
27005 maybe_show_extern_c_location ();
27006 /* Give it C++ linkage to avoid confusing other parts of the
27007 front end. */
27008 push_lang_context (lang_name_cplusplus);
27009 need_lang_pop = true;
27011 else
27012 need_lang_pop = false;
27014 /* We cannot perform access checks on the template parameter
27015 declarations until we know what is being declared, just as we
27016 cannot check the decl-specifier list. */
27017 push_deferring_access_checks (dk_deferred);
27019 /* If the next token is `>', then we have an invalid
27020 specialization. Rather than complain about an invalid template
27021 parameter, issue an error message here. */
27022 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
27024 cp_parser_error (parser, "invalid explicit specialization");
27025 begin_specialization ();
27026 parameter_list = NULL_TREE;
27028 else
27030 /* Parse the template parameters. */
27031 parameter_list = cp_parser_template_parameter_list (parser);
27034 /* Look for the `>'. */
27035 cp_parser_skip_to_end_of_template_parameter_list (parser);
27037 /* Manage template requirements */
27038 if (flag_concepts)
27040 tree reqs = get_shorthand_constraints (current_template_parms);
27041 if (tree r = cp_parser_requires_clause_opt (parser))
27042 reqs = conjoin_constraints (reqs, normalize_expression (r));
27043 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
27046 cp_parser_template_declaration_after_parameters (parser, parameter_list,
27047 member_p);
27049 /* For the erroneous case of a template with C linkage, we pushed an
27050 implicit C++ linkage scope; exit that scope now. */
27051 if (need_lang_pop)
27052 pop_lang_context ();
27055 /* Parse a template-declaration, assuming that the `export' (and
27056 `extern') keywords, if present, has already been scanned. MEMBER_P
27057 is as for cp_parser_template_declaration. */
27059 static bool
27060 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
27062 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27064 cp_lexer_consume_token (parser->lexer);
27065 cp_parser_explicit_template_declaration (parser, member_p);
27066 return true;
27068 else if (flag_concepts)
27069 return cp_parser_template_introduction (parser, member_p);
27071 return false;
27074 /* Perform the deferred access checks from a template-parameter-list.
27075 CHECKS is a TREE_LIST of access checks, as returned by
27076 get_deferred_access_checks. */
27078 static void
27079 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
27081 ++processing_template_parmlist;
27082 perform_access_checks (checks, tf_warning_or_error);
27083 --processing_template_parmlist;
27086 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
27087 `function-definition' sequence that follows a template header.
27088 If MEMBER_P is true, this declaration appears in a class scope.
27090 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
27091 *FRIEND_P is set to TRUE iff the declaration is a friend. */
27093 static tree
27094 cp_parser_single_declaration (cp_parser* parser,
27095 vec<deferred_access_check, va_gc> *checks,
27096 bool member_p,
27097 bool explicit_specialization_p,
27098 bool* friend_p)
27100 int declares_class_or_enum;
27101 tree decl = NULL_TREE;
27102 cp_decl_specifier_seq decl_specifiers;
27103 bool function_definition_p = false;
27104 cp_token *decl_spec_token_start;
27106 /* This function is only used when processing a template
27107 declaration. */
27108 gcc_assert (innermost_scope_kind () == sk_template_parms
27109 || innermost_scope_kind () == sk_template_spec);
27111 /* Defer access checks until we know what is being declared. */
27112 push_deferring_access_checks (dk_deferred);
27114 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
27115 alternative. */
27116 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27117 cp_parser_decl_specifier_seq (parser,
27118 CP_PARSER_FLAGS_OPTIONAL,
27119 &decl_specifiers,
27120 &declares_class_or_enum);
27121 if (friend_p)
27122 *friend_p = cp_parser_friend_p (&decl_specifiers);
27124 /* There are no template typedefs. */
27125 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27127 error_at (decl_spec_token_start->location,
27128 "template declaration of %<typedef%>");
27129 decl = error_mark_node;
27132 /* Gather up the access checks that occurred the
27133 decl-specifier-seq. */
27134 stop_deferring_access_checks ();
27136 /* Check for the declaration of a template class. */
27137 if (declares_class_or_enum)
27139 if (cp_parser_declares_only_class_p (parser)
27140 || (declares_class_or_enum & 2))
27142 // If this is a declaration, but not a definition, associate
27143 // any constraints with the type declaration. Constraints
27144 // are associated with definitions in cp_parser_class_specifier.
27145 if (declares_class_or_enum == 1)
27146 associate_classtype_constraints (decl_specifiers.type);
27148 decl = shadow_tag (&decl_specifiers);
27150 /* In this case:
27152 struct C {
27153 friend template <typename T> struct A<T>::B;
27156 A<T>::B will be represented by a TYPENAME_TYPE, and
27157 therefore not recognized by shadow_tag. */
27158 if (friend_p && *friend_p
27159 && !decl
27160 && decl_specifiers.type
27161 && TYPE_P (decl_specifiers.type))
27162 decl = decl_specifiers.type;
27164 if (decl && decl != error_mark_node)
27165 decl = TYPE_NAME (decl);
27166 else
27167 decl = error_mark_node;
27169 /* Perform access checks for template parameters. */
27170 cp_parser_perform_template_parameter_access_checks (checks);
27172 /* Give a helpful diagnostic for
27173 template <class T> struct A { } a;
27174 if we aren't already recovering from an error. */
27175 if (!cp_parser_declares_only_class_p (parser)
27176 && !seen_error ())
27178 error_at (cp_lexer_peek_token (parser->lexer)->location,
27179 "a class template declaration must not declare "
27180 "anything else");
27181 cp_parser_skip_to_end_of_block_or_statement (parser);
27182 goto out;
27187 /* Complain about missing 'typename' or other invalid type names. */
27188 if (!decl_specifiers.any_type_specifiers_p
27189 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27191 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27192 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27193 the rest of this declaration. */
27194 decl = error_mark_node;
27195 goto out;
27198 /* If it's not a template class, try for a template function. If
27199 the next token is a `;', then this declaration does not declare
27200 anything. But, if there were errors in the decl-specifiers, then
27201 the error might well have come from an attempted class-specifier.
27202 In that case, there's no need to warn about a missing declarator. */
27203 if (!decl
27204 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27205 || decl_specifiers.type != error_mark_node))
27207 decl = cp_parser_init_declarator (parser,
27208 &decl_specifiers,
27209 checks,
27210 /*function_definition_allowed_p=*/true,
27211 member_p,
27212 declares_class_or_enum,
27213 &function_definition_p,
27214 NULL, NULL, NULL);
27216 /* 7.1.1-1 [dcl.stc]
27218 A storage-class-specifier shall not be specified in an explicit
27219 specialization... */
27220 if (decl
27221 && explicit_specialization_p
27222 && decl_specifiers.storage_class != sc_none)
27224 error_at (decl_spec_token_start->location,
27225 "explicit template specialization cannot have a storage class");
27226 decl = error_mark_node;
27229 if (decl && VAR_P (decl))
27230 check_template_variable (decl);
27233 /* Look for a trailing `;' after the declaration. */
27234 if (!function_definition_p
27235 && (decl == error_mark_node
27236 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27237 cp_parser_skip_to_end_of_block_or_statement (parser);
27239 out:
27240 pop_deferring_access_checks ();
27242 /* Clear any current qualification; whatever comes next is the start
27243 of something new. */
27244 parser->scope = NULL_TREE;
27245 parser->qualifying_scope = NULL_TREE;
27246 parser->object_scope = NULL_TREE;
27248 return decl;
27251 /* Parse a cast-expression that is not the operand of a unary "&". */
27253 static cp_expr
27254 cp_parser_simple_cast_expression (cp_parser *parser)
27256 return cp_parser_cast_expression (parser, /*address_p=*/false,
27257 /*cast_p=*/false, /*decltype*/false, NULL);
27260 /* Parse a functional cast to TYPE. Returns an expression
27261 representing the cast. */
27263 static cp_expr
27264 cp_parser_functional_cast (cp_parser* parser, tree type)
27266 vec<tree, va_gc> *vec;
27267 tree expression_list;
27268 cp_expr cast;
27269 bool nonconst_p;
27271 location_t start_loc = input_location;
27273 if (!type)
27274 type = error_mark_node;
27276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27278 cp_lexer_set_source_position (parser->lexer);
27279 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27280 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27281 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27282 if (TREE_CODE (type) == TYPE_DECL)
27283 type = TREE_TYPE (type);
27285 cast = finish_compound_literal (type, expression_list,
27286 tf_warning_or_error, fcl_functional);
27287 /* Create a location of the form:
27288 type_name{i, f}
27289 ^~~~~~~~~~~~~~~
27290 with caret == start at the start of the type name,
27291 finishing at the closing brace. */
27292 location_t finish_loc
27293 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27294 location_t combined_loc = make_location (start_loc, start_loc,
27295 finish_loc);
27296 cast.set_location (combined_loc);
27297 return cast;
27301 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27302 /*cast_p=*/true,
27303 /*allow_expansion_p=*/true,
27304 /*non_constant_p=*/NULL);
27305 if (vec == NULL)
27306 expression_list = error_mark_node;
27307 else
27309 expression_list = build_tree_list_vec (vec);
27310 release_tree_vector (vec);
27313 cast = build_functional_cast (type, expression_list,
27314 tf_warning_or_error);
27315 /* [expr.const]/1: In an integral constant expression "only type
27316 conversions to integral or enumeration type can be used". */
27317 if (TREE_CODE (type) == TYPE_DECL)
27318 type = TREE_TYPE (type);
27319 if (cast != error_mark_node
27320 && !cast_valid_in_integral_constant_expression_p (type)
27321 && cp_parser_non_integral_constant_expression (parser,
27322 NIC_CONSTRUCTOR))
27323 return error_mark_node;
27325 /* Create a location of the form:
27326 float(i)
27327 ^~~~~~~~
27328 with caret == start at the start of the type name,
27329 finishing at the closing paren. */
27330 location_t finish_loc
27331 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27332 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27333 cast.set_location (combined_loc);
27334 return cast;
27337 /* Save the tokens that make up the body of a member function defined
27338 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27339 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27340 specifiers applied to the declaration. Returns the FUNCTION_DECL
27341 for the member function. */
27343 static tree
27344 cp_parser_save_member_function_body (cp_parser* parser,
27345 cp_decl_specifier_seq *decl_specifiers,
27346 cp_declarator *declarator,
27347 tree attributes)
27349 cp_token *first;
27350 cp_token *last;
27351 tree fn;
27352 bool function_try_block = false;
27354 /* Create the FUNCTION_DECL. */
27355 fn = grokmethod (decl_specifiers, declarator, attributes);
27356 cp_finalize_omp_declare_simd (parser, fn);
27357 cp_finalize_oacc_routine (parser, fn, true);
27358 /* If something went badly wrong, bail out now. */
27359 if (fn == error_mark_node)
27361 /* If there's a function-body, skip it. */
27362 if (cp_parser_token_starts_function_definition_p
27363 (cp_lexer_peek_token (parser->lexer)))
27364 cp_parser_skip_to_end_of_block_or_statement (parser);
27365 return error_mark_node;
27368 /* Remember it, if there default args to post process. */
27369 cp_parser_save_default_args (parser, fn);
27371 /* Save away the tokens that make up the body of the
27372 function. */
27373 first = parser->lexer->next_token;
27375 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27376 cp_lexer_consume_token (parser->lexer);
27377 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27378 RID_TRANSACTION_ATOMIC))
27380 cp_lexer_consume_token (parser->lexer);
27381 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27382 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27383 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27384 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27385 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27386 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27387 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27389 cp_lexer_consume_token (parser->lexer);
27390 cp_lexer_consume_token (parser->lexer);
27391 cp_lexer_consume_token (parser->lexer);
27392 cp_lexer_consume_token (parser->lexer);
27393 cp_lexer_consume_token (parser->lexer);
27395 else
27396 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27397 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27399 cp_lexer_consume_token (parser->lexer);
27400 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27401 break;
27405 /* Handle function try blocks. */
27406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27408 cp_lexer_consume_token (parser->lexer);
27409 function_try_block = true;
27411 /* We can have braced-init-list mem-initializers before the fn body. */
27412 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27414 cp_lexer_consume_token (parser->lexer);
27415 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27417 /* cache_group will stop after an un-nested { } pair, too. */
27418 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27419 break;
27421 /* variadic mem-inits have ... after the ')'. */
27422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27423 cp_lexer_consume_token (parser->lexer);
27426 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27427 /* Handle function try blocks. */
27428 if (function_try_block)
27429 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27430 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27431 last = parser->lexer->next_token;
27433 /* Save away the inline definition; we will process it when the
27434 class is complete. */
27435 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27436 DECL_PENDING_INLINE_P (fn) = 1;
27438 /* We need to know that this was defined in the class, so that
27439 friend templates are handled correctly. */
27440 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27442 /* Add FN to the queue of functions to be parsed later. */
27443 vec_safe_push (unparsed_funs_with_definitions, fn);
27445 return fn;
27448 /* Save the tokens that make up the in-class initializer for a non-static
27449 data member. Returns a DEFAULT_ARG. */
27451 static tree
27452 cp_parser_save_nsdmi (cp_parser* parser)
27454 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27457 /* Parse a template-argument-list, as well as the trailing ">" (but
27458 not the opening "<"). See cp_parser_template_argument_list for the
27459 return value. */
27461 static tree
27462 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27464 tree arguments;
27465 tree saved_scope;
27466 tree saved_qualifying_scope;
27467 tree saved_object_scope;
27468 bool saved_greater_than_is_operator_p;
27469 int saved_unevaluated_operand;
27470 int saved_inhibit_evaluation_warnings;
27472 /* [temp.names]
27474 When parsing a template-id, the first non-nested `>' is taken as
27475 the end of the template-argument-list rather than a greater-than
27476 operator. */
27477 saved_greater_than_is_operator_p
27478 = parser->greater_than_is_operator_p;
27479 parser->greater_than_is_operator_p = false;
27480 /* Parsing the argument list may modify SCOPE, so we save it
27481 here. */
27482 saved_scope = parser->scope;
27483 saved_qualifying_scope = parser->qualifying_scope;
27484 saved_object_scope = parser->object_scope;
27485 /* We need to evaluate the template arguments, even though this
27486 template-id may be nested within a "sizeof". */
27487 saved_unevaluated_operand = cp_unevaluated_operand;
27488 cp_unevaluated_operand = 0;
27489 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27490 c_inhibit_evaluation_warnings = 0;
27491 /* Parse the template-argument-list itself. */
27492 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27493 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27494 arguments = NULL_TREE;
27495 else
27496 arguments = cp_parser_template_argument_list (parser);
27497 /* Look for the `>' that ends the template-argument-list. If we find
27498 a '>>' instead, it's probably just a typo. */
27499 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27501 if (cxx_dialect != cxx98)
27503 /* In C++0x, a `>>' in a template argument list or cast
27504 expression is considered to be two separate `>'
27505 tokens. So, change the current token to a `>', but don't
27506 consume it: it will be consumed later when the outer
27507 template argument list (or cast expression) is parsed.
27508 Note that this replacement of `>' for `>>' is necessary
27509 even if we are parsing tentatively: in the tentative
27510 case, after calling
27511 cp_parser_enclosed_template_argument_list we will always
27512 throw away all of the template arguments and the first
27513 closing `>', either because the template argument list
27514 was erroneous or because we are replacing those tokens
27515 with a CPP_TEMPLATE_ID token. The second `>' (which will
27516 not have been thrown away) is needed either to close an
27517 outer template argument list or to complete a new-style
27518 cast. */
27519 cp_token *token = cp_lexer_peek_token (parser->lexer);
27520 token->type = CPP_GREATER;
27522 else if (!saved_greater_than_is_operator_p)
27524 /* If we're in a nested template argument list, the '>>' has
27525 to be a typo for '> >'. We emit the error message, but we
27526 continue parsing and we push a '>' as next token, so that
27527 the argument list will be parsed correctly. Note that the
27528 global source location is still on the token before the
27529 '>>', so we need to say explicitly where we want it. */
27530 cp_token *token = cp_lexer_peek_token (parser->lexer);
27531 gcc_rich_location richloc (token->location);
27532 richloc.add_fixit_replace ("> >");
27533 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27534 "within a nested template argument list");
27536 token->type = CPP_GREATER;
27538 else
27540 /* If this is not a nested template argument list, the '>>'
27541 is a typo for '>'. Emit an error message and continue.
27542 Same deal about the token location, but here we can get it
27543 right by consuming the '>>' before issuing the diagnostic. */
27544 cp_token *token = cp_lexer_consume_token (parser->lexer);
27545 error_at (token->location,
27546 "spurious %<>>%>, use %<>%> to terminate "
27547 "a template argument list");
27550 else
27551 cp_parser_skip_to_end_of_template_parameter_list (parser);
27552 /* The `>' token might be a greater-than operator again now. */
27553 parser->greater_than_is_operator_p
27554 = saved_greater_than_is_operator_p;
27555 /* Restore the SAVED_SCOPE. */
27556 parser->scope = saved_scope;
27557 parser->qualifying_scope = saved_qualifying_scope;
27558 parser->object_scope = saved_object_scope;
27559 cp_unevaluated_operand = saved_unevaluated_operand;
27560 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27562 return arguments;
27565 /* MEMBER_FUNCTION is a member function, or a friend. If default
27566 arguments, or the body of the function have not yet been parsed,
27567 parse them now. */
27569 static void
27570 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27572 timevar_push (TV_PARSE_INMETH);
27573 /* If this member is a template, get the underlying
27574 FUNCTION_DECL. */
27575 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27576 member_function = DECL_TEMPLATE_RESULT (member_function);
27578 /* There should not be any class definitions in progress at this
27579 point; the bodies of members are only parsed outside of all class
27580 definitions. */
27581 gcc_assert (parser->num_classes_being_defined == 0);
27582 /* While we're parsing the member functions we might encounter more
27583 classes. We want to handle them right away, but we don't want
27584 them getting mixed up with functions that are currently in the
27585 queue. */
27586 push_unparsed_function_queues (parser);
27588 /* Make sure that any template parameters are in scope. */
27589 maybe_begin_member_template_processing (member_function);
27591 /* If the body of the function has not yet been parsed, parse it
27592 now. */
27593 if (DECL_PENDING_INLINE_P (member_function))
27595 tree function_scope;
27596 cp_token_cache *tokens;
27598 /* The function is no longer pending; we are processing it. */
27599 tokens = DECL_PENDING_INLINE_INFO (member_function);
27600 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27601 DECL_PENDING_INLINE_P (member_function) = 0;
27603 /* If this is a local class, enter the scope of the containing
27604 function. */
27605 function_scope = current_function_decl;
27606 if (function_scope)
27607 push_function_context ();
27609 /* Push the body of the function onto the lexer stack. */
27610 cp_parser_push_lexer_for_tokens (parser, tokens);
27612 /* Let the front end know that we going to be defining this
27613 function. */
27614 start_preparsed_function (member_function, NULL_TREE,
27615 SF_PRE_PARSED | SF_INCLASS_INLINE);
27617 /* Don't do access checking if it is a templated function. */
27618 if (processing_template_decl)
27619 push_deferring_access_checks (dk_no_check);
27621 /* #pragma omp declare reduction needs special parsing. */
27622 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27624 parser->lexer->in_pragma = true;
27625 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27626 finish_function (/*inline_p=*/true);
27627 cp_check_omp_declare_reduction (member_function);
27629 else
27630 /* Now, parse the body of the function. */
27631 cp_parser_function_definition_after_declarator (parser,
27632 /*inline_p=*/true);
27634 if (processing_template_decl)
27635 pop_deferring_access_checks ();
27637 /* Leave the scope of the containing function. */
27638 if (function_scope)
27639 pop_function_context ();
27640 cp_parser_pop_lexer (parser);
27643 /* Remove any template parameters from the symbol table. */
27644 maybe_end_member_template_processing ();
27646 /* Restore the queue. */
27647 pop_unparsed_function_queues (parser);
27648 timevar_pop (TV_PARSE_INMETH);
27651 /* If DECL contains any default args, remember it on the unparsed
27652 functions queue. */
27654 static void
27655 cp_parser_save_default_args (cp_parser* parser, tree decl)
27657 tree probe;
27659 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27660 probe;
27661 probe = TREE_CHAIN (probe))
27662 if (TREE_PURPOSE (probe))
27664 cp_default_arg_entry entry = {current_class_type, decl};
27665 vec_safe_push (unparsed_funs_with_default_args, entry);
27666 break;
27670 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27671 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27672 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27673 from the parameter-type-list. */
27675 static tree
27676 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27677 tree default_arg, tree parmtype)
27679 cp_token_cache *tokens;
27680 tree parsed_arg;
27681 bool dummy;
27683 if (default_arg == error_mark_node)
27684 return error_mark_node;
27686 /* Push the saved tokens for the default argument onto the parser's
27687 lexer stack. */
27688 tokens = DEFARG_TOKENS (default_arg);
27689 cp_parser_push_lexer_for_tokens (parser, tokens);
27691 start_lambda_scope (decl);
27693 /* Parse the default argument. */
27694 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27695 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27696 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27698 finish_lambda_scope ();
27700 if (parsed_arg == error_mark_node)
27701 cp_parser_skip_to_end_of_statement (parser);
27703 if (!processing_template_decl)
27705 /* In a non-template class, check conversions now. In a template,
27706 we'll wait and instantiate these as needed. */
27707 if (TREE_CODE (decl) == PARM_DECL)
27708 parsed_arg = check_default_argument (parmtype, parsed_arg,
27709 tf_warning_or_error);
27710 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27711 parsed_arg = error_mark_node;
27712 else
27713 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27716 /* If the token stream has not been completely used up, then
27717 there was extra junk after the end of the default
27718 argument. */
27719 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27721 if (TREE_CODE (decl) == PARM_DECL)
27722 cp_parser_error (parser, "expected %<,%>");
27723 else
27724 cp_parser_error (parser, "expected %<;%>");
27727 /* Revert to the main lexer. */
27728 cp_parser_pop_lexer (parser);
27730 return parsed_arg;
27733 /* FIELD is a non-static data member with an initializer which we saved for
27734 later; parse it now. */
27736 static void
27737 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27739 tree def;
27741 maybe_begin_member_template_processing (field);
27743 push_unparsed_function_queues (parser);
27744 def = cp_parser_late_parse_one_default_arg (parser, field,
27745 DECL_INITIAL (field),
27746 NULL_TREE);
27747 pop_unparsed_function_queues (parser);
27749 maybe_end_member_template_processing ();
27751 DECL_INITIAL (field) = def;
27754 /* FN is a FUNCTION_DECL which may contains a parameter with an
27755 unparsed DEFAULT_ARG. Parse the default args now. This function
27756 assumes that the current scope is the scope in which the default
27757 argument should be processed. */
27759 static void
27760 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27762 bool saved_local_variables_forbidden_p;
27763 tree parm, parmdecl;
27765 /* While we're parsing the default args, we might (due to the
27766 statement expression extension) encounter more classes. We want
27767 to handle them right away, but we don't want them getting mixed
27768 up with default args that are currently in the queue. */
27769 push_unparsed_function_queues (parser);
27771 /* Local variable names (and the `this' keyword) may not appear
27772 in a default argument. */
27773 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27774 parser->local_variables_forbidden_p = true;
27776 push_defarg_context (fn);
27778 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27779 parmdecl = DECL_ARGUMENTS (fn);
27780 parm && parm != void_list_node;
27781 parm = TREE_CHAIN (parm),
27782 parmdecl = DECL_CHAIN (parmdecl))
27784 tree default_arg = TREE_PURPOSE (parm);
27785 tree parsed_arg;
27786 vec<tree, va_gc> *insts;
27787 tree copy;
27788 unsigned ix;
27790 if (!default_arg)
27791 continue;
27793 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27794 /* This can happen for a friend declaration for a function
27795 already declared with default arguments. */
27796 continue;
27798 parsed_arg
27799 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27800 default_arg,
27801 TREE_VALUE (parm));
27802 TREE_PURPOSE (parm) = parsed_arg;
27804 /* Update any instantiations we've already created. */
27805 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27806 vec_safe_iterate (insts, ix, &copy); ix++)
27807 TREE_PURPOSE (copy) = parsed_arg;
27810 pop_defarg_context ();
27812 /* Make sure no default arg is missing. */
27813 check_default_args (fn);
27815 /* Restore the state of local_variables_forbidden_p. */
27816 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27818 /* Restore the queue. */
27819 pop_unparsed_function_queues (parser);
27822 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27824 sizeof ... ( identifier )
27826 where the 'sizeof' token has already been consumed. */
27828 static tree
27829 cp_parser_sizeof_pack (cp_parser *parser)
27831 /* Consume the `...'. */
27832 cp_lexer_consume_token (parser->lexer);
27833 maybe_warn_variadic_templates ();
27835 matching_parens parens;
27836 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27837 if (paren)
27838 parens.consume_open (parser);
27839 else
27840 permerror (cp_lexer_peek_token (parser->lexer)->location,
27841 "%<sizeof...%> argument must be surrounded by parentheses");
27843 cp_token *token = cp_lexer_peek_token (parser->lexer);
27844 tree name = cp_parser_identifier (parser);
27845 if (name == error_mark_node)
27846 return error_mark_node;
27847 /* The name is not qualified. */
27848 parser->scope = NULL_TREE;
27849 parser->qualifying_scope = NULL_TREE;
27850 parser->object_scope = NULL_TREE;
27851 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27852 if (expr == error_mark_node)
27853 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27854 token->location);
27855 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27856 expr = TREE_TYPE (expr);
27857 else if (TREE_CODE (expr) == CONST_DECL)
27858 expr = DECL_INITIAL (expr);
27859 expr = make_pack_expansion (expr);
27860 PACK_EXPANSION_SIZEOF_P (expr) = true;
27862 if (paren)
27863 parens.require_close (parser);
27865 return expr;
27868 /* Parse the operand of `sizeof' (or a similar operator). Returns
27869 either a TYPE or an expression, depending on the form of the
27870 input. The KEYWORD indicates which kind of expression we have
27871 encountered. */
27873 static tree
27874 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27876 tree expr = NULL_TREE;
27877 const char *saved_message;
27878 char *tmp;
27879 bool saved_integral_constant_expression_p;
27880 bool saved_non_integral_constant_expression_p;
27882 /* If it's a `...', then we are computing the length of a parameter
27883 pack. */
27884 if (keyword == RID_SIZEOF
27885 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27886 return cp_parser_sizeof_pack (parser);
27888 /* Types cannot be defined in a `sizeof' expression. Save away the
27889 old message. */
27890 saved_message = parser->type_definition_forbidden_message;
27891 /* And create the new one. */
27892 tmp = concat ("types may not be defined in %<",
27893 IDENTIFIER_POINTER (ridpointers[keyword]),
27894 "%> expressions", NULL);
27895 parser->type_definition_forbidden_message = tmp;
27897 /* The restrictions on constant-expressions do not apply inside
27898 sizeof expressions. */
27899 saved_integral_constant_expression_p
27900 = parser->integral_constant_expression_p;
27901 saved_non_integral_constant_expression_p
27902 = parser->non_integral_constant_expression_p;
27903 parser->integral_constant_expression_p = false;
27905 /* Do not actually evaluate the expression. */
27906 ++cp_unevaluated_operand;
27907 ++c_inhibit_evaluation_warnings;
27908 /* If it's a `(', then we might be looking at the type-id
27909 construction. */
27910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27912 tree type = NULL_TREE;
27914 /* We can't be sure yet whether we're looking at a type-id or an
27915 expression. */
27916 cp_parser_parse_tentatively (parser);
27918 matching_parens parens;
27919 parens.consume_open (parser);
27921 /* Note: as a GNU Extension, compound literals are considered
27922 postfix-expressions as they are in C99, so they are valid
27923 arguments to sizeof. See comment in cp_parser_cast_expression
27924 for details. */
27925 if (cp_parser_compound_literal_p (parser))
27926 cp_parser_simulate_error (parser);
27927 else
27929 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27930 parser->in_type_id_in_expr_p = true;
27931 /* Look for the type-id. */
27932 type = cp_parser_type_id (parser);
27933 /* Look for the closing `)'. */
27934 parens.require_close (parser);
27935 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27938 /* If all went well, then we're done. */
27939 if (cp_parser_parse_definitely (parser))
27941 cp_decl_specifier_seq decl_specs;
27943 /* Build a trivial decl-specifier-seq. */
27944 clear_decl_specs (&decl_specs);
27945 decl_specs.type = type;
27947 /* Call grokdeclarator to figure out what type this is. */
27948 expr = grokdeclarator (NULL,
27949 &decl_specs,
27950 TYPENAME,
27951 /*initialized=*/0,
27952 /*attrlist=*/NULL);
27956 /* If the type-id production did not work out, then we must be
27957 looking at the unary-expression production. */
27958 if (!expr)
27959 expr = cp_parser_unary_expression (parser);
27961 /* Go back to evaluating expressions. */
27962 --cp_unevaluated_operand;
27963 --c_inhibit_evaluation_warnings;
27965 /* Free the message we created. */
27966 free (tmp);
27967 /* And restore the old one. */
27968 parser->type_definition_forbidden_message = saved_message;
27969 parser->integral_constant_expression_p
27970 = saved_integral_constant_expression_p;
27971 parser->non_integral_constant_expression_p
27972 = saved_non_integral_constant_expression_p;
27974 return expr;
27977 /* If the current declaration has no declarator, return true. */
27979 static bool
27980 cp_parser_declares_only_class_p (cp_parser *parser)
27982 /* If the next token is a `;' or a `,' then there is no
27983 declarator. */
27984 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27985 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27988 /* Update the DECL_SPECS to reflect the storage class indicated by
27989 KEYWORD. */
27991 static void
27992 cp_parser_set_storage_class (cp_parser *parser,
27993 cp_decl_specifier_seq *decl_specs,
27994 enum rid keyword,
27995 cp_token *token)
27997 cp_storage_class storage_class;
27999 if (parser->in_unbraced_linkage_specification_p)
28001 error_at (token->location, "invalid use of %qD in linkage specification",
28002 ridpointers[keyword]);
28003 return;
28005 else if (decl_specs->storage_class != sc_none)
28007 decl_specs->conflicting_specifiers_p = true;
28008 return;
28011 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
28012 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
28013 && decl_specs->gnu_thread_keyword_p)
28015 pedwarn (decl_specs->locations[ds_thread], 0,
28016 "%<__thread%> before %qD", ridpointers[keyword]);
28019 switch (keyword)
28021 case RID_AUTO:
28022 storage_class = sc_auto;
28023 break;
28024 case RID_REGISTER:
28025 storage_class = sc_register;
28026 break;
28027 case RID_STATIC:
28028 storage_class = sc_static;
28029 break;
28030 case RID_EXTERN:
28031 storage_class = sc_extern;
28032 break;
28033 case RID_MUTABLE:
28034 storage_class = sc_mutable;
28035 break;
28036 default:
28037 gcc_unreachable ();
28039 decl_specs->storage_class = storage_class;
28040 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
28042 /* A storage class specifier cannot be applied alongside a typedef
28043 specifier. If there is a typedef specifier present then set
28044 conflicting_specifiers_p which will trigger an error later
28045 on in grokdeclarator. */
28046 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
28047 decl_specs->conflicting_specifiers_p = true;
28050 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
28051 is true, the type is a class or enum definition. */
28053 static void
28054 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
28055 tree type_spec,
28056 cp_token *token,
28057 bool type_definition_p)
28059 decl_specs->any_specifiers_p = true;
28061 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
28062 (with, for example, in "typedef int wchar_t;") we remember that
28063 this is what happened. In system headers, we ignore these
28064 declarations so that G++ can work with system headers that are not
28065 C++-safe. */
28066 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
28067 && !type_definition_p
28068 && (type_spec == boolean_type_node
28069 || type_spec == char16_type_node
28070 || type_spec == char32_type_node
28071 || type_spec == wchar_type_node)
28072 && (decl_specs->type
28073 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
28074 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
28075 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
28076 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
28078 decl_specs->redefined_builtin_type = type_spec;
28079 set_and_check_decl_spec_loc (decl_specs,
28080 ds_redefined_builtin_type_spec,
28081 token);
28082 if (!decl_specs->type)
28084 decl_specs->type = type_spec;
28085 decl_specs->type_definition_p = false;
28086 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
28089 else if (decl_specs->type)
28090 decl_specs->multiple_types_p = true;
28091 else
28093 decl_specs->type = type_spec;
28094 decl_specs->type_definition_p = type_definition_p;
28095 decl_specs->redefined_builtin_type = NULL_TREE;
28096 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
28100 /* True iff TOKEN is the GNU keyword __thread. */
28102 static bool
28103 token_is__thread (cp_token *token)
28105 gcc_assert (token->keyword == RID_THREAD);
28106 return id_equal (token->u.value, "__thread");
28109 /* Set the location for a declarator specifier and check if it is
28110 duplicated.
28112 DECL_SPECS is the sequence of declarator specifiers onto which to
28113 set the location.
28115 DS is the single declarator specifier to set which location is to
28116 be set onto the existing sequence of declarators.
28118 LOCATION is the location for the declarator specifier to
28119 consider. */
28121 static void
28122 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
28123 cp_decl_spec ds, cp_token *token)
28125 gcc_assert (ds < ds_last);
28127 if (decl_specs == NULL)
28128 return;
28130 source_location location = token->location;
28132 if (decl_specs->locations[ds] == 0)
28134 decl_specs->locations[ds] = location;
28135 if (ds == ds_thread)
28136 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
28138 else
28140 if (ds == ds_long)
28142 if (decl_specs->locations[ds_long_long] != 0)
28143 error_at (location,
28144 "%<long long long%> is too long for GCC");
28145 else
28147 decl_specs->locations[ds_long_long] = location;
28148 pedwarn_cxx98 (location,
28149 OPT_Wlong_long,
28150 "ISO C++ 1998 does not support %<long long%>");
28153 else if (ds == ds_thread)
28155 bool gnu = token_is__thread (token);
28156 if (gnu != decl_specs->gnu_thread_keyword_p)
28157 error_at (location,
28158 "both %<__thread%> and %<thread_local%> specified");
28159 else
28161 gcc_rich_location richloc (location);
28162 richloc.add_fixit_remove ();
28163 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
28166 else
28168 static const char *const decl_spec_names[] = {
28169 "signed",
28170 "unsigned",
28171 "short",
28172 "long",
28173 "const",
28174 "volatile",
28175 "restrict",
28176 "inline",
28177 "virtual",
28178 "explicit",
28179 "friend",
28180 "typedef",
28181 "using",
28182 "constexpr",
28183 "__complex"
28185 gcc_rich_location richloc (location);
28186 richloc.add_fixit_remove ();
28187 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
28192 /* Return true iff the declarator specifier DS is present in the
28193 sequence of declarator specifiers DECL_SPECS. */
28195 bool
28196 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28197 cp_decl_spec ds)
28199 gcc_assert (ds < ds_last);
28201 if (decl_specs == NULL)
28202 return false;
28204 return decl_specs->locations[ds] != 0;
28207 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28208 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28210 static bool
28211 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28213 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28216 /* Issue an error message indicating that TOKEN_DESC was expected.
28217 If KEYWORD is true, it indicated this function is called by
28218 cp_parser_require_keword and the required token can only be
28219 a indicated keyword.
28221 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28222 within any error as the location of an "opening" token matching
28223 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28224 RT_CLOSE_PAREN). */
28226 static void
28227 cp_parser_required_error (cp_parser *parser,
28228 required_token token_desc,
28229 bool keyword,
28230 location_t matching_location)
28232 if (cp_parser_simulate_error (parser))
28233 return;
28235 const char *gmsgid = NULL;
28236 switch (token_desc)
28238 case RT_NEW:
28239 gmsgid = G_("expected %<new%>");
28240 break;
28241 case RT_DELETE:
28242 gmsgid = G_("expected %<delete%>");
28243 break;
28244 case RT_RETURN:
28245 gmsgid = G_("expected %<return%>");
28246 break;
28247 case RT_WHILE:
28248 gmsgid = G_("expected %<while%>");
28249 break;
28250 case RT_EXTERN:
28251 gmsgid = G_("expected %<extern%>");
28252 break;
28253 case RT_STATIC_ASSERT:
28254 gmsgid = G_("expected %<static_assert%>");
28255 break;
28256 case RT_DECLTYPE:
28257 gmsgid = G_("expected %<decltype%>");
28258 break;
28259 case RT_OPERATOR:
28260 gmsgid = G_("expected %<operator%>");
28261 break;
28262 case RT_CLASS:
28263 gmsgid = G_("expected %<class%>");
28264 break;
28265 case RT_TEMPLATE:
28266 gmsgid = G_("expected %<template%>");
28267 break;
28268 case RT_NAMESPACE:
28269 gmsgid = G_("expected %<namespace%>");
28270 break;
28271 case RT_USING:
28272 gmsgid = G_("expected %<using%>");
28273 break;
28274 case RT_ASM:
28275 gmsgid = G_("expected %<asm%>");
28276 break;
28277 case RT_TRY:
28278 gmsgid = G_("expected %<try%>");
28279 break;
28280 case RT_CATCH:
28281 gmsgid = G_("expected %<catch%>");
28282 break;
28283 case RT_THROW:
28284 gmsgid = G_("expected %<throw%>");
28285 break;
28286 case RT_LABEL:
28287 gmsgid = G_("expected %<__label__%>");
28288 break;
28289 case RT_AT_TRY:
28290 gmsgid = G_("expected %<@try%>");
28291 break;
28292 case RT_AT_SYNCHRONIZED:
28293 gmsgid = G_("expected %<@synchronized%>");
28294 break;
28295 case RT_AT_THROW:
28296 gmsgid = G_("expected %<@throw%>");
28297 break;
28298 case RT_TRANSACTION_ATOMIC:
28299 gmsgid = G_("expected %<__transaction_atomic%>");
28300 break;
28301 case RT_TRANSACTION_RELAXED:
28302 gmsgid = G_("expected %<__transaction_relaxed%>");
28303 break;
28304 default:
28305 break;
28308 if (!gmsgid && !keyword)
28310 switch (token_desc)
28312 case RT_SEMICOLON:
28313 gmsgid = G_("expected %<;%>");
28314 break;
28315 case RT_OPEN_PAREN:
28316 gmsgid = G_("expected %<(%>");
28317 break;
28318 case RT_CLOSE_BRACE:
28319 gmsgid = G_("expected %<}%>");
28320 break;
28321 case RT_OPEN_BRACE:
28322 gmsgid = G_("expected %<{%>");
28323 break;
28324 case RT_CLOSE_SQUARE:
28325 gmsgid = G_("expected %<]%>");
28326 break;
28327 case RT_OPEN_SQUARE:
28328 gmsgid = G_("expected %<[%>");
28329 break;
28330 case RT_COMMA:
28331 gmsgid = G_("expected %<,%>");
28332 break;
28333 case RT_SCOPE:
28334 gmsgid = G_("expected %<::%>");
28335 break;
28336 case RT_LESS:
28337 gmsgid = G_("expected %<<%>");
28338 break;
28339 case RT_GREATER:
28340 gmsgid = G_("expected %<>%>");
28341 break;
28342 case RT_EQ:
28343 gmsgid = G_("expected %<=%>");
28344 break;
28345 case RT_ELLIPSIS:
28346 gmsgid = G_("expected %<...%>");
28347 break;
28348 case RT_MULT:
28349 gmsgid = G_("expected %<*%>");
28350 break;
28351 case RT_COMPL:
28352 gmsgid = G_("expected %<~%>");
28353 break;
28354 case RT_COLON:
28355 gmsgid = G_("expected %<:%>");
28356 break;
28357 case RT_COLON_SCOPE:
28358 gmsgid = G_("expected %<:%> or %<::%>");
28359 break;
28360 case RT_CLOSE_PAREN:
28361 gmsgid = G_("expected %<)%>");
28362 break;
28363 case RT_COMMA_CLOSE_PAREN:
28364 gmsgid = G_("expected %<,%> or %<)%>");
28365 break;
28366 case RT_PRAGMA_EOL:
28367 gmsgid = G_("expected end of line");
28368 break;
28369 case RT_NAME:
28370 gmsgid = G_("expected identifier");
28371 break;
28372 case RT_SELECT:
28373 gmsgid = G_("expected selection-statement");
28374 break;
28375 case RT_ITERATION:
28376 gmsgid = G_("expected iteration-statement");
28377 break;
28378 case RT_JUMP:
28379 gmsgid = G_("expected jump-statement");
28380 break;
28381 case RT_CLASS_KEY:
28382 gmsgid = G_("expected class-key");
28383 break;
28384 case RT_CLASS_TYPENAME_TEMPLATE:
28385 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28386 break;
28387 default:
28388 gcc_unreachable ();
28392 if (gmsgid)
28393 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
28397 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28398 issue an error message indicating that TOKEN_DESC was expected.
28400 Returns the token consumed, if the token had the appropriate type.
28401 Otherwise, returns NULL.
28403 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28404 within any error as the location of an "opening" token matching
28405 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28406 RT_CLOSE_PAREN). */
28408 static cp_token *
28409 cp_parser_require (cp_parser* parser,
28410 enum cpp_ttype type,
28411 required_token token_desc,
28412 location_t matching_location)
28414 if (cp_lexer_next_token_is (parser->lexer, type))
28415 return cp_lexer_consume_token (parser->lexer);
28416 else
28418 /* Output the MESSAGE -- unless we're parsing tentatively. */
28419 if (!cp_parser_simulate_error (parser))
28420 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28421 matching_location);
28422 return NULL;
28426 /* An error message is produced if the next token is not '>'.
28427 All further tokens are skipped until the desired token is
28428 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28430 static void
28431 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28433 /* Current level of '< ... >'. */
28434 unsigned level = 0;
28435 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28436 unsigned nesting_depth = 0;
28438 /* Are we ready, yet? If not, issue error message. */
28439 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28440 return;
28442 /* Skip tokens until the desired token is found. */
28443 while (true)
28445 /* Peek at the next token. */
28446 switch (cp_lexer_peek_token (parser->lexer)->type)
28448 case CPP_LESS:
28449 if (!nesting_depth)
28450 ++level;
28451 break;
28453 case CPP_RSHIFT:
28454 if (cxx_dialect == cxx98)
28455 /* C++0x views the `>>' operator as two `>' tokens, but
28456 C++98 does not. */
28457 break;
28458 else if (!nesting_depth && level-- == 0)
28460 /* We've hit a `>>' where the first `>' closes the
28461 template argument list, and the second `>' is
28462 spurious. Just consume the `>>' and stop; we've
28463 already produced at least one error. */
28464 cp_lexer_consume_token (parser->lexer);
28465 return;
28467 /* Fall through for C++0x, so we handle the second `>' in
28468 the `>>'. */
28469 gcc_fallthrough ();
28471 case CPP_GREATER:
28472 if (!nesting_depth && level-- == 0)
28474 /* We've reached the token we want, consume it and stop. */
28475 cp_lexer_consume_token (parser->lexer);
28476 return;
28478 break;
28480 case CPP_OPEN_PAREN:
28481 case CPP_OPEN_SQUARE:
28482 ++nesting_depth;
28483 break;
28485 case CPP_CLOSE_PAREN:
28486 case CPP_CLOSE_SQUARE:
28487 if (nesting_depth-- == 0)
28488 return;
28489 break;
28491 case CPP_EOF:
28492 case CPP_PRAGMA_EOL:
28493 case CPP_SEMICOLON:
28494 case CPP_OPEN_BRACE:
28495 case CPP_CLOSE_BRACE:
28496 /* The '>' was probably forgotten, don't look further. */
28497 return;
28499 default:
28500 break;
28503 /* Consume this token. */
28504 cp_lexer_consume_token (parser->lexer);
28508 /* If the next token is the indicated keyword, consume it. Otherwise,
28509 issue an error message indicating that TOKEN_DESC was expected.
28511 Returns the token consumed, if the token had the appropriate type.
28512 Otherwise, returns NULL. */
28514 static cp_token *
28515 cp_parser_require_keyword (cp_parser* parser,
28516 enum rid keyword,
28517 required_token token_desc)
28519 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28521 if (token && token->keyword != keyword)
28523 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28524 UNKNOWN_LOCATION);
28525 return NULL;
28528 return token;
28531 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28532 function-definition. */
28534 static bool
28535 cp_parser_token_starts_function_definition_p (cp_token* token)
28537 return (/* An ordinary function-body begins with an `{'. */
28538 token->type == CPP_OPEN_BRACE
28539 /* A ctor-initializer begins with a `:'. */
28540 || token->type == CPP_COLON
28541 /* A function-try-block begins with `try'. */
28542 || token->keyword == RID_TRY
28543 /* A function-transaction-block begins with `__transaction_atomic'
28544 or `__transaction_relaxed'. */
28545 || token->keyword == RID_TRANSACTION_ATOMIC
28546 || token->keyword == RID_TRANSACTION_RELAXED
28547 /* The named return value extension begins with `return'. */
28548 || token->keyword == RID_RETURN);
28551 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28552 definition. */
28554 static bool
28555 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28557 cp_token *token;
28559 token = cp_lexer_peek_token (parser->lexer);
28560 return (token->type == CPP_OPEN_BRACE
28561 || (token->type == CPP_COLON
28562 && !parser->colon_doesnt_start_class_def_p));
28565 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28566 C++0x) ending a template-argument. */
28568 static bool
28569 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28571 cp_token *token;
28573 token = cp_lexer_peek_token (parser->lexer);
28574 return (token->type == CPP_COMMA
28575 || token->type == CPP_GREATER
28576 || token->type == CPP_ELLIPSIS
28577 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28580 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28581 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28583 static bool
28584 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28585 size_t n)
28587 cp_token *token;
28589 token = cp_lexer_peek_nth_token (parser->lexer, n);
28590 if (token->type == CPP_LESS)
28591 return true;
28592 /* Check for the sequence `<::' in the original code. It would be lexed as
28593 `[:', where `[' is a digraph, and there is no whitespace before
28594 `:'. */
28595 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28597 cp_token *token2;
28598 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28599 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28600 return true;
28602 return false;
28605 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28606 or none_type otherwise. */
28608 static enum tag_types
28609 cp_parser_token_is_class_key (cp_token* token)
28611 switch (token->keyword)
28613 case RID_CLASS:
28614 return class_type;
28615 case RID_STRUCT:
28616 return record_type;
28617 case RID_UNION:
28618 return union_type;
28620 default:
28621 return none_type;
28625 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28626 or none_type otherwise or if the token is null. */
28628 static enum tag_types
28629 cp_parser_token_is_type_parameter_key (cp_token* token)
28631 if (!token)
28632 return none_type;
28634 switch (token->keyword)
28636 case RID_CLASS:
28637 return class_type;
28638 case RID_TYPENAME:
28639 return typename_type;
28641 default:
28642 return none_type;
28646 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28648 static void
28649 cp_parser_check_class_key (enum tag_types class_key, tree type)
28651 if (type == error_mark_node)
28652 return;
28653 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28655 if (permerror (input_location, "%qs tag used in naming %q#T",
28656 class_key == union_type ? "union"
28657 : class_key == record_type ? "struct" : "class",
28658 type))
28659 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28660 "%q#T was previously declared here", type);
28664 /* Issue an error message if DECL is redeclared with different
28665 access than its original declaration [class.access.spec/3].
28666 This applies to nested classes, nested class templates and
28667 enumerations [class.mem/1]. */
28669 static void
28670 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28672 if (!decl
28673 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28674 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28675 return;
28677 if ((TREE_PRIVATE (decl)
28678 != (current_access_specifier == access_private_node))
28679 || (TREE_PROTECTED (decl)
28680 != (current_access_specifier == access_protected_node)))
28681 error_at (location, "%qD redeclared with different access", decl);
28684 /* Look for the `template' keyword, as a syntactic disambiguator.
28685 Return TRUE iff it is present, in which case it will be
28686 consumed. */
28688 static bool
28689 cp_parser_optional_template_keyword (cp_parser *parser)
28691 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28693 /* In C++98 the `template' keyword can only be used within templates;
28694 outside templates the parser can always figure out what is a
28695 template and what is not. In C++11, per the resolution of DR 468,
28696 `template' is allowed in cases where it is not strictly necessary. */
28697 if (!processing_template_decl
28698 && pedantic && cxx_dialect == cxx98)
28700 cp_token *token = cp_lexer_peek_token (parser->lexer);
28701 pedwarn (token->location, OPT_Wpedantic,
28702 "in C++98 %<template%> (as a disambiguator) is only "
28703 "allowed within templates");
28704 /* If this part of the token stream is rescanned, the same
28705 error message would be generated. So, we purge the token
28706 from the stream. */
28707 cp_lexer_purge_token (parser->lexer);
28708 return false;
28710 else
28712 /* Consume the `template' keyword. */
28713 cp_lexer_consume_token (parser->lexer);
28714 return true;
28717 return false;
28720 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28721 set PARSER->SCOPE, and perform other related actions. */
28723 static void
28724 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28726 struct tree_check *check_value;
28728 /* Get the stored value. */
28729 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28730 /* Set the scope from the stored value. */
28731 parser->scope = saved_checks_value (check_value);
28732 parser->qualifying_scope = check_value->qualifying_scope;
28733 parser->object_scope = NULL_TREE;
28736 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28737 encounter the end of a block before what we were looking for. */
28739 static bool
28740 cp_parser_cache_group (cp_parser *parser,
28741 enum cpp_ttype end,
28742 unsigned depth)
28744 while (true)
28746 cp_token *token = cp_lexer_peek_token (parser->lexer);
28748 /* Abort a parenthesized expression if we encounter a semicolon. */
28749 if ((end == CPP_CLOSE_PAREN || depth == 0)
28750 && token->type == CPP_SEMICOLON)
28751 return true;
28752 /* If we've reached the end of the file, stop. */
28753 if (token->type == CPP_EOF
28754 || (end != CPP_PRAGMA_EOL
28755 && token->type == CPP_PRAGMA_EOL))
28756 return true;
28757 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28758 /* We've hit the end of an enclosing block, so there's been some
28759 kind of syntax error. */
28760 return true;
28762 /* Consume the token. */
28763 cp_lexer_consume_token (parser->lexer);
28764 /* See if it starts a new group. */
28765 if (token->type == CPP_OPEN_BRACE)
28767 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28768 /* In theory this should probably check end == '}', but
28769 cp_parser_save_member_function_body needs it to exit
28770 after either '}' or ')' when called with ')'. */
28771 if (depth == 0)
28772 return false;
28774 else if (token->type == CPP_OPEN_PAREN)
28776 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28777 if (depth == 0 && end == CPP_CLOSE_PAREN)
28778 return false;
28780 else if (token->type == CPP_PRAGMA)
28781 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28782 else if (token->type == end)
28783 return false;
28787 /* Like above, for caching a default argument or NSDMI. Both of these are
28788 terminated by a non-nested comma, but it can be unclear whether or not a
28789 comma is nested in a template argument list unless we do more parsing.
28790 In order to handle this ambiguity, when we encounter a ',' after a '<'
28791 we try to parse what follows as a parameter-declaration-list (in the
28792 case of a default argument) or a member-declarator (in the case of an
28793 NSDMI). If that succeeds, then we stop caching. */
28795 static tree
28796 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28798 unsigned depth = 0;
28799 int maybe_template_id = 0;
28800 cp_token *first_token;
28801 cp_token *token;
28802 tree default_argument;
28804 /* Add tokens until we have processed the entire default
28805 argument. We add the range [first_token, token). */
28806 first_token = cp_lexer_peek_token (parser->lexer);
28807 if (first_token->type == CPP_OPEN_BRACE)
28809 /* For list-initialization, this is straightforward. */
28810 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28811 token = cp_lexer_peek_token (parser->lexer);
28813 else while (true)
28815 bool done = false;
28817 /* Peek at the next token. */
28818 token = cp_lexer_peek_token (parser->lexer);
28819 /* What we do depends on what token we have. */
28820 switch (token->type)
28822 /* In valid code, a default argument must be
28823 immediately followed by a `,' `)', or `...'. */
28824 case CPP_COMMA:
28825 if (depth == 0 && maybe_template_id)
28827 /* If we've seen a '<', we might be in a
28828 template-argument-list. Until Core issue 325 is
28829 resolved, we don't know how this situation ought
28830 to be handled, so try to DTRT. We check whether
28831 what comes after the comma is a valid parameter
28832 declaration list. If it is, then the comma ends
28833 the default argument; otherwise the default
28834 argument continues. */
28835 bool error = false;
28836 cp_token *peek;
28838 /* Set ITALP so cp_parser_parameter_declaration_list
28839 doesn't decide to commit to this parse. */
28840 bool saved_italp = parser->in_template_argument_list_p;
28841 parser->in_template_argument_list_p = true;
28843 cp_parser_parse_tentatively (parser);
28845 if (nsdmi)
28847 /* Parse declarators until we reach a non-comma or
28848 somthing that cannot be an initializer.
28849 Just checking whether we're looking at a single
28850 declarator is insufficient. Consider:
28851 int var = tuple<T,U>::x;
28852 The template parameter 'U' looks exactly like a
28853 declarator. */
28856 int ctor_dtor_or_conv_p;
28857 cp_lexer_consume_token (parser->lexer);
28858 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28859 &ctor_dtor_or_conv_p,
28860 /*parenthesized_p=*/NULL,
28861 /*member_p=*/true,
28862 /*friend_p=*/false);
28863 peek = cp_lexer_peek_token (parser->lexer);
28864 if (cp_parser_error_occurred (parser))
28865 break;
28867 while (peek->type == CPP_COMMA);
28868 /* If we met an '=' or ';' then the original comma
28869 was the end of the NSDMI. Otherwise assume
28870 we're still in the NSDMI. */
28871 error = (peek->type != CPP_EQ
28872 && peek->type != CPP_SEMICOLON);
28874 else
28876 cp_lexer_consume_token (parser->lexer);
28877 begin_scope (sk_function_parms, NULL_TREE);
28878 cp_parser_parameter_declaration_list (parser, &error);
28879 pop_bindings_and_leave_scope ();
28881 if (!cp_parser_error_occurred (parser) && !error)
28882 done = true;
28883 cp_parser_abort_tentative_parse (parser);
28885 parser->in_template_argument_list_p = saved_italp;
28886 break;
28888 /* FALLTHRU */
28889 case CPP_CLOSE_PAREN:
28890 case CPP_ELLIPSIS:
28891 /* If we run into a non-nested `;', `}', or `]',
28892 then the code is invalid -- but the default
28893 argument is certainly over. */
28894 case CPP_SEMICOLON:
28895 case CPP_CLOSE_BRACE:
28896 case CPP_CLOSE_SQUARE:
28897 if (depth == 0
28898 /* Handle correctly int n = sizeof ... ( p ); */
28899 && token->type != CPP_ELLIPSIS)
28900 done = true;
28901 /* Update DEPTH, if necessary. */
28902 else if (token->type == CPP_CLOSE_PAREN
28903 || token->type == CPP_CLOSE_BRACE
28904 || token->type == CPP_CLOSE_SQUARE)
28905 --depth;
28906 break;
28908 case CPP_OPEN_PAREN:
28909 case CPP_OPEN_SQUARE:
28910 case CPP_OPEN_BRACE:
28911 ++depth;
28912 break;
28914 case CPP_LESS:
28915 if (depth == 0)
28916 /* This might be the comparison operator, or it might
28917 start a template argument list. */
28918 ++maybe_template_id;
28919 break;
28921 case CPP_RSHIFT:
28922 if (cxx_dialect == cxx98)
28923 break;
28924 /* Fall through for C++0x, which treats the `>>'
28925 operator like two `>' tokens in certain
28926 cases. */
28927 gcc_fallthrough ();
28929 case CPP_GREATER:
28930 if (depth == 0)
28932 /* This might be an operator, or it might close a
28933 template argument list. But if a previous '<'
28934 started a template argument list, this will have
28935 closed it, so we can't be in one anymore. */
28936 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28937 if (maybe_template_id < 0)
28938 maybe_template_id = 0;
28940 break;
28942 /* If we run out of tokens, issue an error message. */
28943 case CPP_EOF:
28944 case CPP_PRAGMA_EOL:
28945 error_at (token->location, "file ends in default argument");
28946 return error_mark_node;
28948 case CPP_NAME:
28949 case CPP_SCOPE:
28950 /* In these cases, we should look for template-ids.
28951 For example, if the default argument is
28952 `X<int, double>()', we need to do name lookup to
28953 figure out whether or not `X' is a template; if
28954 so, the `,' does not end the default argument.
28956 That is not yet done. */
28957 break;
28959 default:
28960 break;
28963 /* If we've reached the end, stop. */
28964 if (done)
28965 break;
28967 /* Add the token to the token block. */
28968 token = cp_lexer_consume_token (parser->lexer);
28971 /* Create a DEFAULT_ARG to represent the unparsed default
28972 argument. */
28973 default_argument = make_node (DEFAULT_ARG);
28974 DEFARG_TOKENS (default_argument)
28975 = cp_token_cache_new (first_token, token);
28976 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28978 return default_argument;
28981 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28983 location_t
28984 defarg_location (tree default_argument)
28986 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28987 location_t start = tokens->first->location;
28988 location_t end = tokens->last->location;
28989 return make_location (start, start, end);
28992 /* Begin parsing tentatively. We always save tokens while parsing
28993 tentatively so that if the tentative parsing fails we can restore the
28994 tokens. */
28996 static void
28997 cp_parser_parse_tentatively (cp_parser* parser)
28999 /* Enter a new parsing context. */
29000 parser->context = cp_parser_context_new (parser->context);
29001 /* Begin saving tokens. */
29002 cp_lexer_save_tokens (parser->lexer);
29003 /* In order to avoid repetitive access control error messages,
29004 access checks are queued up until we are no longer parsing
29005 tentatively. */
29006 push_deferring_access_checks (dk_deferred);
29009 /* Commit to the currently active tentative parse. */
29011 static void
29012 cp_parser_commit_to_tentative_parse (cp_parser* parser)
29014 cp_parser_context *context;
29015 cp_lexer *lexer;
29017 /* Mark all of the levels as committed. */
29018 lexer = parser->lexer;
29019 for (context = parser->context; context->next; context = context->next)
29021 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29022 break;
29023 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29024 while (!cp_lexer_saving_tokens (lexer))
29025 lexer = lexer->next;
29026 cp_lexer_commit_tokens (lexer);
29030 /* Commit to the topmost currently active tentative parse.
29032 Note that this function shouldn't be called when there are
29033 irreversible side-effects while in a tentative state. For
29034 example, we shouldn't create a permanent entry in the symbol
29035 table, or issue an error message that might not apply if the
29036 tentative parse is aborted. */
29038 static void
29039 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
29041 cp_parser_context *context = parser->context;
29042 cp_lexer *lexer = parser->lexer;
29044 if (context)
29046 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
29047 return;
29048 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
29050 while (!cp_lexer_saving_tokens (lexer))
29051 lexer = lexer->next;
29052 cp_lexer_commit_tokens (lexer);
29056 /* Abort the currently active tentative parse. All consumed tokens
29057 will be rolled back, and no diagnostics will be issued. */
29059 static void
29060 cp_parser_abort_tentative_parse (cp_parser* parser)
29062 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
29063 || errorcount > 0);
29064 cp_parser_simulate_error (parser);
29065 /* Now, pretend that we want to see if the construct was
29066 successfully parsed. */
29067 cp_parser_parse_definitely (parser);
29070 /* Stop parsing tentatively. If a parse error has occurred, restore the
29071 token stream. Otherwise, commit to the tokens we have consumed.
29072 Returns true if no error occurred; false otherwise. */
29074 static bool
29075 cp_parser_parse_definitely (cp_parser* parser)
29077 bool error_occurred;
29078 cp_parser_context *context;
29080 /* Remember whether or not an error occurred, since we are about to
29081 destroy that information. */
29082 error_occurred = cp_parser_error_occurred (parser);
29083 /* Remove the topmost context from the stack. */
29084 context = parser->context;
29085 parser->context = context->next;
29086 /* If no parse errors occurred, commit to the tentative parse. */
29087 if (!error_occurred)
29089 /* Commit to the tokens read tentatively, unless that was
29090 already done. */
29091 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
29092 cp_lexer_commit_tokens (parser->lexer);
29094 pop_to_parent_deferring_access_checks ();
29096 /* Otherwise, if errors occurred, roll back our state so that things
29097 are just as they were before we began the tentative parse. */
29098 else
29100 cp_lexer_rollback_tokens (parser->lexer);
29101 pop_deferring_access_checks ();
29103 /* Add the context to the front of the free list. */
29104 context->next = cp_parser_context_free_list;
29105 cp_parser_context_free_list = context;
29107 return !error_occurred;
29110 /* Returns true if we are parsing tentatively and are not committed to
29111 this tentative parse. */
29113 static bool
29114 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29116 return (cp_parser_parsing_tentatively (parser)
29117 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29120 /* Returns nonzero iff an error has occurred during the most recent
29121 tentative parse. */
29123 static bool
29124 cp_parser_error_occurred (cp_parser* parser)
29126 return (cp_parser_parsing_tentatively (parser)
29127 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29130 /* Returns nonzero if GNU extensions are allowed. */
29132 static bool
29133 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29135 return parser->allow_gnu_extensions_p;
29138 /* Objective-C++ Productions */
29141 /* Parse an Objective-C expression, which feeds into a primary-expression
29142 above.
29144 objc-expression:
29145 objc-message-expression
29146 objc-string-literal
29147 objc-encode-expression
29148 objc-protocol-expression
29149 objc-selector-expression
29151 Returns a tree representation of the expression. */
29153 static cp_expr
29154 cp_parser_objc_expression (cp_parser* parser)
29156 /* Try to figure out what kind of declaration is present. */
29157 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29159 switch (kwd->type)
29161 case CPP_OPEN_SQUARE:
29162 return cp_parser_objc_message_expression (parser);
29164 case CPP_OBJC_STRING:
29165 kwd = cp_lexer_consume_token (parser->lexer);
29166 return objc_build_string_object (kwd->u.value);
29168 case CPP_KEYWORD:
29169 switch (kwd->keyword)
29171 case RID_AT_ENCODE:
29172 return cp_parser_objc_encode_expression (parser);
29174 case RID_AT_PROTOCOL:
29175 return cp_parser_objc_protocol_expression (parser);
29177 case RID_AT_SELECTOR:
29178 return cp_parser_objc_selector_expression (parser);
29180 default:
29181 break;
29183 default:
29184 error_at (kwd->location,
29185 "misplaced %<@%D%> Objective-C++ construct",
29186 kwd->u.value);
29187 cp_parser_skip_to_end_of_block_or_statement (parser);
29190 return error_mark_node;
29193 /* Parse an Objective-C message expression.
29195 objc-message-expression:
29196 [ objc-message-receiver objc-message-args ]
29198 Returns a representation of an Objective-C message. */
29200 static tree
29201 cp_parser_objc_message_expression (cp_parser* parser)
29203 tree receiver, messageargs;
29205 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29206 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29207 receiver = cp_parser_objc_message_receiver (parser);
29208 messageargs = cp_parser_objc_message_args (parser);
29209 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29210 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29212 tree result = objc_build_message_expr (receiver, messageargs);
29214 /* Construct a location e.g.
29215 [self func1:5]
29216 ^~~~~~~~~~~~~~
29217 ranging from the '[' to the ']', with the caret at the start. */
29218 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29219 protected_set_expr_location (result, combined_loc);
29221 return result;
29224 /* Parse an objc-message-receiver.
29226 objc-message-receiver:
29227 expression
29228 simple-type-specifier
29230 Returns a representation of the type or expression. */
29232 static tree
29233 cp_parser_objc_message_receiver (cp_parser* parser)
29235 tree rcv;
29237 /* An Objective-C message receiver may be either (1) a type
29238 or (2) an expression. */
29239 cp_parser_parse_tentatively (parser);
29240 rcv = cp_parser_expression (parser);
29242 /* If that worked out, fine. */
29243 if (cp_parser_parse_definitely (parser))
29244 return rcv;
29246 cp_parser_parse_tentatively (parser);
29247 rcv = cp_parser_simple_type_specifier (parser,
29248 /*decl_specs=*/NULL,
29249 CP_PARSER_FLAGS_NONE);
29251 if (cp_parser_parse_definitely (parser))
29252 return objc_get_class_reference (rcv);
29254 cp_parser_error (parser, "objective-c++ message receiver expected");
29255 return error_mark_node;
29258 /* Parse the arguments and selectors comprising an Objective-C message.
29260 objc-message-args:
29261 objc-selector
29262 objc-selector-args
29263 objc-selector-args , objc-comma-args
29265 objc-selector-args:
29266 objc-selector [opt] : assignment-expression
29267 objc-selector-args objc-selector [opt] : assignment-expression
29269 objc-comma-args:
29270 assignment-expression
29271 objc-comma-args , assignment-expression
29273 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29274 selector arguments and TREE_VALUE containing a list of comma
29275 arguments. */
29277 static tree
29278 cp_parser_objc_message_args (cp_parser* parser)
29280 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29281 bool maybe_unary_selector_p = true;
29282 cp_token *token = cp_lexer_peek_token (parser->lexer);
29284 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29286 tree selector = NULL_TREE, arg;
29288 if (token->type != CPP_COLON)
29289 selector = cp_parser_objc_selector (parser);
29291 /* Detect if we have a unary selector. */
29292 if (maybe_unary_selector_p
29293 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29294 return build_tree_list (selector, NULL_TREE);
29296 maybe_unary_selector_p = false;
29297 cp_parser_require (parser, CPP_COLON, RT_COLON);
29298 arg = cp_parser_assignment_expression (parser);
29300 sel_args
29301 = chainon (sel_args,
29302 build_tree_list (selector, arg));
29304 token = cp_lexer_peek_token (parser->lexer);
29307 /* Handle non-selector arguments, if any. */
29308 while (token->type == CPP_COMMA)
29310 tree arg;
29312 cp_lexer_consume_token (parser->lexer);
29313 arg = cp_parser_assignment_expression (parser);
29315 addl_args
29316 = chainon (addl_args,
29317 build_tree_list (NULL_TREE, arg));
29319 token = cp_lexer_peek_token (parser->lexer);
29322 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29324 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29325 return build_tree_list (error_mark_node, error_mark_node);
29328 return build_tree_list (sel_args, addl_args);
29331 /* Parse an Objective-C encode expression.
29333 objc-encode-expression:
29334 @encode objc-typename
29336 Returns an encoded representation of the type argument. */
29338 static cp_expr
29339 cp_parser_objc_encode_expression (cp_parser* parser)
29341 tree type;
29342 cp_token *token;
29343 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29345 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29346 matching_parens parens;
29347 parens.require_open (parser);
29348 token = cp_lexer_peek_token (parser->lexer);
29349 type = complete_type (cp_parser_type_id (parser));
29350 parens.require_close (parser);
29352 if (!type)
29354 error_at (token->location,
29355 "%<@encode%> must specify a type as an argument");
29356 return error_mark_node;
29359 /* This happens if we find @encode(T) (where T is a template
29360 typename or something dependent on a template typename) when
29361 parsing a template. In that case, we can't compile it
29362 immediately, but we rather create an AT_ENCODE_EXPR which will
29363 need to be instantiated when the template is used.
29365 if (dependent_type_p (type))
29367 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29368 TREE_READONLY (value) = 1;
29369 return value;
29373 /* Build a location of the form:
29374 @encode(int)
29375 ^~~~~~~~~~~~
29376 with caret==start at the @ token, finishing at the close paren. */
29377 location_t combined_loc
29378 = make_location (start_loc, start_loc,
29379 cp_lexer_previous_token (parser->lexer)->location);
29381 return cp_expr (objc_build_encode_expr (type), combined_loc);
29384 /* Parse an Objective-C @defs expression. */
29386 static tree
29387 cp_parser_objc_defs_expression (cp_parser *parser)
29389 tree name;
29391 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29392 matching_parens parens;
29393 parens.require_open (parser);
29394 name = cp_parser_identifier (parser);
29395 parens.require_close (parser);
29397 return objc_get_class_ivars (name);
29400 /* Parse an Objective-C protocol expression.
29402 objc-protocol-expression:
29403 @protocol ( identifier )
29405 Returns a representation of the protocol expression. */
29407 static tree
29408 cp_parser_objc_protocol_expression (cp_parser* parser)
29410 tree proto;
29411 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29413 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29414 matching_parens parens;
29415 parens.require_open (parser);
29416 proto = cp_parser_identifier (parser);
29417 parens.require_close (parser);
29419 /* Build a location of the form:
29420 @protocol(prot)
29421 ^~~~~~~~~~~~~~~
29422 with caret==start at the @ token, finishing at the close paren. */
29423 location_t combined_loc
29424 = make_location (start_loc, start_loc,
29425 cp_lexer_previous_token (parser->lexer)->location);
29426 tree result = objc_build_protocol_expr (proto);
29427 protected_set_expr_location (result, combined_loc);
29428 return result;
29431 /* Parse an Objective-C selector expression.
29433 objc-selector-expression:
29434 @selector ( objc-method-signature )
29436 objc-method-signature:
29437 objc-selector
29438 objc-selector-seq
29440 objc-selector-seq:
29441 objc-selector :
29442 objc-selector-seq objc-selector :
29444 Returns a representation of the method selector. */
29446 static tree
29447 cp_parser_objc_selector_expression (cp_parser* parser)
29449 tree sel_seq = NULL_TREE;
29450 bool maybe_unary_selector_p = true;
29451 cp_token *token;
29452 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29454 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29455 matching_parens parens;
29456 parens.require_open (parser);
29457 token = cp_lexer_peek_token (parser->lexer);
29459 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29460 || token->type == CPP_SCOPE)
29462 tree selector = NULL_TREE;
29464 if (token->type != CPP_COLON
29465 || token->type == CPP_SCOPE)
29466 selector = cp_parser_objc_selector (parser);
29468 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29469 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29471 /* Detect if we have a unary selector. */
29472 if (maybe_unary_selector_p)
29474 sel_seq = selector;
29475 goto finish_selector;
29477 else
29479 cp_parser_error (parser, "expected %<:%>");
29482 maybe_unary_selector_p = false;
29483 token = cp_lexer_consume_token (parser->lexer);
29485 if (token->type == CPP_SCOPE)
29487 sel_seq
29488 = chainon (sel_seq,
29489 build_tree_list (selector, NULL_TREE));
29490 sel_seq
29491 = chainon (sel_seq,
29492 build_tree_list (NULL_TREE, NULL_TREE));
29494 else
29495 sel_seq
29496 = chainon (sel_seq,
29497 build_tree_list (selector, NULL_TREE));
29499 token = cp_lexer_peek_token (parser->lexer);
29502 finish_selector:
29503 parens.require_close (parser);
29506 /* Build a location of the form:
29507 @selector(func)
29508 ^~~~~~~~~~~~~~~
29509 with caret==start at the @ token, finishing at the close paren. */
29510 location_t combined_loc
29511 = make_location (loc, loc,
29512 cp_lexer_previous_token (parser->lexer)->location);
29513 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29514 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29515 protected_set_expr_location (result, combined_loc);
29516 return result;
29519 /* Parse a list of identifiers.
29521 objc-identifier-list:
29522 identifier
29523 objc-identifier-list , identifier
29525 Returns a TREE_LIST of identifier nodes. */
29527 static tree
29528 cp_parser_objc_identifier_list (cp_parser* parser)
29530 tree identifier;
29531 tree list;
29532 cp_token *sep;
29534 identifier = cp_parser_identifier (parser);
29535 if (identifier == error_mark_node)
29536 return error_mark_node;
29538 list = build_tree_list (NULL_TREE, identifier);
29539 sep = cp_lexer_peek_token (parser->lexer);
29541 while (sep->type == CPP_COMMA)
29543 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29544 identifier = cp_parser_identifier (parser);
29545 if (identifier == error_mark_node)
29546 return list;
29548 list = chainon (list, build_tree_list (NULL_TREE,
29549 identifier));
29550 sep = cp_lexer_peek_token (parser->lexer);
29553 return list;
29556 /* Parse an Objective-C alias declaration.
29558 objc-alias-declaration:
29559 @compatibility_alias identifier identifier ;
29561 This function registers the alias mapping with the Objective-C front end.
29562 It returns nothing. */
29564 static void
29565 cp_parser_objc_alias_declaration (cp_parser* parser)
29567 tree alias, orig;
29569 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29570 alias = cp_parser_identifier (parser);
29571 orig = cp_parser_identifier (parser);
29572 objc_declare_alias (alias, orig);
29573 cp_parser_consume_semicolon_at_end_of_statement (parser);
29576 /* Parse an Objective-C class forward-declaration.
29578 objc-class-declaration:
29579 @class objc-identifier-list ;
29581 The function registers the forward declarations with the Objective-C
29582 front end. It returns nothing. */
29584 static void
29585 cp_parser_objc_class_declaration (cp_parser* parser)
29587 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29588 while (true)
29590 tree id;
29592 id = cp_parser_identifier (parser);
29593 if (id == error_mark_node)
29594 break;
29596 objc_declare_class (id);
29598 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29599 cp_lexer_consume_token (parser->lexer);
29600 else
29601 break;
29603 cp_parser_consume_semicolon_at_end_of_statement (parser);
29606 /* Parse a list of Objective-C protocol references.
29608 objc-protocol-refs-opt:
29609 objc-protocol-refs [opt]
29611 objc-protocol-refs:
29612 < objc-identifier-list >
29614 Returns a TREE_LIST of identifiers, if any. */
29616 static tree
29617 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29619 tree protorefs = NULL_TREE;
29621 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29623 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29624 protorefs = cp_parser_objc_identifier_list (parser);
29625 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29628 return protorefs;
29631 /* Parse a Objective-C visibility specification. */
29633 static void
29634 cp_parser_objc_visibility_spec (cp_parser* parser)
29636 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29638 switch (vis->keyword)
29640 case RID_AT_PRIVATE:
29641 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29642 break;
29643 case RID_AT_PROTECTED:
29644 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29645 break;
29646 case RID_AT_PUBLIC:
29647 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29648 break;
29649 case RID_AT_PACKAGE:
29650 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29651 break;
29652 default:
29653 return;
29656 /* Eat '@private'/'@protected'/'@public'. */
29657 cp_lexer_consume_token (parser->lexer);
29660 /* Parse an Objective-C method type. Return 'true' if it is a class
29661 (+) method, and 'false' if it is an instance (-) method. */
29663 static inline bool
29664 cp_parser_objc_method_type (cp_parser* parser)
29666 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29667 return true;
29668 else
29669 return false;
29672 /* Parse an Objective-C protocol qualifier. */
29674 static tree
29675 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29677 tree quals = NULL_TREE, node;
29678 cp_token *token = cp_lexer_peek_token (parser->lexer);
29680 node = token->u.value;
29682 while (node && identifier_p (node)
29683 && (node == ridpointers [(int) RID_IN]
29684 || node == ridpointers [(int) RID_OUT]
29685 || node == ridpointers [(int) RID_INOUT]
29686 || node == ridpointers [(int) RID_BYCOPY]
29687 || node == ridpointers [(int) RID_BYREF]
29688 || node == ridpointers [(int) RID_ONEWAY]))
29690 quals = tree_cons (NULL_TREE, node, quals);
29691 cp_lexer_consume_token (parser->lexer);
29692 token = cp_lexer_peek_token (parser->lexer);
29693 node = token->u.value;
29696 return quals;
29699 /* Parse an Objective-C typename. */
29701 static tree
29702 cp_parser_objc_typename (cp_parser* parser)
29704 tree type_name = NULL_TREE;
29706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29708 tree proto_quals, cp_type = NULL_TREE;
29710 matching_parens parens;
29711 parens.consume_open (parser); /* Eat '('. */
29712 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29714 /* An ObjC type name may consist of just protocol qualifiers, in which
29715 case the type shall default to 'id'. */
29716 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29718 cp_type = cp_parser_type_id (parser);
29720 /* If the type could not be parsed, an error has already
29721 been produced. For error recovery, behave as if it had
29722 not been specified, which will use the default type
29723 'id'. */
29724 if (cp_type == error_mark_node)
29726 cp_type = NULL_TREE;
29727 /* We need to skip to the closing parenthesis as
29728 cp_parser_type_id() does not seem to do it for
29729 us. */
29730 cp_parser_skip_to_closing_parenthesis (parser,
29731 /*recovering=*/true,
29732 /*or_comma=*/false,
29733 /*consume_paren=*/false);
29737 parens.require_close (parser);
29738 type_name = build_tree_list (proto_quals, cp_type);
29741 return type_name;
29744 /* Check to see if TYPE refers to an Objective-C selector name. */
29746 static bool
29747 cp_parser_objc_selector_p (enum cpp_ttype type)
29749 return (type == CPP_NAME || type == CPP_KEYWORD
29750 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29751 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29752 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29753 || type == CPP_XOR || type == CPP_XOR_EQ);
29756 /* Parse an Objective-C selector. */
29758 static tree
29759 cp_parser_objc_selector (cp_parser* parser)
29761 cp_token *token = cp_lexer_consume_token (parser->lexer);
29763 if (!cp_parser_objc_selector_p (token->type))
29765 error_at (token->location, "invalid Objective-C++ selector name");
29766 return error_mark_node;
29769 /* C++ operator names are allowed to appear in ObjC selectors. */
29770 switch (token->type)
29772 case CPP_AND_AND: return get_identifier ("and");
29773 case CPP_AND_EQ: return get_identifier ("and_eq");
29774 case CPP_AND: return get_identifier ("bitand");
29775 case CPP_OR: return get_identifier ("bitor");
29776 case CPP_COMPL: return get_identifier ("compl");
29777 case CPP_NOT: return get_identifier ("not");
29778 case CPP_NOT_EQ: return get_identifier ("not_eq");
29779 case CPP_OR_OR: return get_identifier ("or");
29780 case CPP_OR_EQ: return get_identifier ("or_eq");
29781 case CPP_XOR: return get_identifier ("xor");
29782 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29783 default: return token->u.value;
29787 /* Parse an Objective-C params list. */
29789 static tree
29790 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29792 tree params = NULL_TREE;
29793 bool maybe_unary_selector_p = true;
29794 cp_token *token = cp_lexer_peek_token (parser->lexer);
29796 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29798 tree selector = NULL_TREE, type_name, identifier;
29799 tree parm_attr = NULL_TREE;
29801 if (token->keyword == RID_ATTRIBUTE)
29802 break;
29804 if (token->type != CPP_COLON)
29805 selector = cp_parser_objc_selector (parser);
29807 /* Detect if we have a unary selector. */
29808 if (maybe_unary_selector_p
29809 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29811 params = selector; /* Might be followed by attributes. */
29812 break;
29815 maybe_unary_selector_p = false;
29816 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29818 /* Something went quite wrong. There should be a colon
29819 here, but there is not. Stop parsing parameters. */
29820 break;
29822 type_name = cp_parser_objc_typename (parser);
29823 /* New ObjC allows attributes on parameters too. */
29824 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29825 parm_attr = cp_parser_attributes_opt (parser);
29826 identifier = cp_parser_identifier (parser);
29828 params
29829 = chainon (params,
29830 objc_build_keyword_decl (selector,
29831 type_name,
29832 identifier,
29833 parm_attr));
29835 token = cp_lexer_peek_token (parser->lexer);
29838 if (params == NULL_TREE)
29840 cp_parser_error (parser, "objective-c++ method declaration is expected");
29841 return error_mark_node;
29844 /* We allow tail attributes for the method. */
29845 if (token->keyword == RID_ATTRIBUTE)
29847 *attributes = cp_parser_attributes_opt (parser);
29848 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29849 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29850 return params;
29851 cp_parser_error (parser,
29852 "method attributes must be specified at the end");
29853 return error_mark_node;
29856 if (params == NULL_TREE)
29858 cp_parser_error (parser, "objective-c++ method declaration is expected");
29859 return error_mark_node;
29861 return params;
29864 /* Parse the non-keyword Objective-C params. */
29866 static tree
29867 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29868 tree* attributes)
29870 tree params = make_node (TREE_LIST);
29871 cp_token *token = cp_lexer_peek_token (parser->lexer);
29872 *ellipsisp = false; /* Initially, assume no ellipsis. */
29874 while (token->type == CPP_COMMA)
29876 cp_parameter_declarator *parmdecl;
29877 tree parm;
29879 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29880 token = cp_lexer_peek_token (parser->lexer);
29882 if (token->type == CPP_ELLIPSIS)
29884 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29885 *ellipsisp = true;
29886 token = cp_lexer_peek_token (parser->lexer);
29887 break;
29890 /* TODO: parse attributes for tail parameters. */
29891 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29892 parm = grokdeclarator (parmdecl->declarator,
29893 &parmdecl->decl_specifiers,
29894 PARM, /*initialized=*/0,
29895 /*attrlist=*/NULL);
29897 chainon (params, build_tree_list (NULL_TREE, parm));
29898 token = cp_lexer_peek_token (parser->lexer);
29901 /* We allow tail attributes for the method. */
29902 if (token->keyword == RID_ATTRIBUTE)
29904 if (*attributes == NULL_TREE)
29906 *attributes = cp_parser_attributes_opt (parser);
29907 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29908 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29909 return params;
29911 else
29912 /* We have an error, but parse the attributes, so that we can
29913 carry on. */
29914 *attributes = cp_parser_attributes_opt (parser);
29916 cp_parser_error (parser,
29917 "method attributes must be specified at the end");
29918 return error_mark_node;
29921 return params;
29924 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29926 static void
29927 cp_parser_objc_interstitial_code (cp_parser* parser)
29929 cp_token *token = cp_lexer_peek_token (parser->lexer);
29931 /* If the next token is `extern' and the following token is a string
29932 literal, then we have a linkage specification. */
29933 if (token->keyword == RID_EXTERN
29934 && cp_parser_is_pure_string_literal
29935 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29936 cp_parser_linkage_specification (parser);
29937 /* Handle #pragma, if any. */
29938 else if (token->type == CPP_PRAGMA)
29939 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29940 /* Allow stray semicolons. */
29941 else if (token->type == CPP_SEMICOLON)
29942 cp_lexer_consume_token (parser->lexer);
29943 /* Mark methods as optional or required, when building protocols. */
29944 else if (token->keyword == RID_AT_OPTIONAL)
29946 cp_lexer_consume_token (parser->lexer);
29947 objc_set_method_opt (true);
29949 else if (token->keyword == RID_AT_REQUIRED)
29951 cp_lexer_consume_token (parser->lexer);
29952 objc_set_method_opt (false);
29954 else if (token->keyword == RID_NAMESPACE)
29955 cp_parser_namespace_definition (parser);
29956 /* Other stray characters must generate errors. */
29957 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29959 cp_lexer_consume_token (parser->lexer);
29960 error ("stray %qs between Objective-C++ methods",
29961 token->type == CPP_OPEN_BRACE ? "{" : "}");
29963 /* Finally, try to parse a block-declaration, or a function-definition. */
29964 else
29965 cp_parser_block_declaration (parser, /*statement_p=*/false);
29968 /* Parse a method signature. */
29970 static tree
29971 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29973 tree rettype, kwdparms, optparms;
29974 bool ellipsis = false;
29975 bool is_class_method;
29977 is_class_method = cp_parser_objc_method_type (parser);
29978 rettype = cp_parser_objc_typename (parser);
29979 *attributes = NULL_TREE;
29980 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29981 if (kwdparms == error_mark_node)
29982 return error_mark_node;
29983 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29984 if (optparms == error_mark_node)
29985 return error_mark_node;
29987 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29990 static bool
29991 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29993 tree tattr;
29994 cp_lexer_save_tokens (parser->lexer);
29995 tattr = cp_parser_attributes_opt (parser);
29996 gcc_assert (tattr) ;
29998 /* If the attributes are followed by a method introducer, this is not allowed.
29999 Dump the attributes and flag the situation. */
30000 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
30001 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
30002 return true;
30004 /* Otherwise, the attributes introduce some interstitial code, possibly so
30005 rewind to allow that check. */
30006 cp_lexer_rollback_tokens (parser->lexer);
30007 return false;
30010 /* Parse an Objective-C method prototype list. */
30012 static void
30013 cp_parser_objc_method_prototype_list (cp_parser* parser)
30015 cp_token *token = cp_lexer_peek_token (parser->lexer);
30017 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30019 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30021 tree attributes, sig;
30022 bool is_class_method;
30023 if (token->type == CPP_PLUS)
30024 is_class_method = true;
30025 else
30026 is_class_method = false;
30027 sig = cp_parser_objc_method_signature (parser, &attributes);
30028 if (sig == error_mark_node)
30030 cp_parser_skip_to_end_of_block_or_statement (parser);
30031 token = cp_lexer_peek_token (parser->lexer);
30032 continue;
30034 objc_add_method_declaration (is_class_method, sig, attributes);
30035 cp_parser_consume_semicolon_at_end_of_statement (parser);
30037 else if (token->keyword == RID_AT_PROPERTY)
30038 cp_parser_objc_at_property_declaration (parser);
30039 else if (token->keyword == RID_ATTRIBUTE
30040 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30041 warning_at (cp_lexer_peek_token (parser->lexer)->location,
30042 OPT_Wattributes,
30043 "prefix attributes are ignored for methods");
30044 else
30045 /* Allow for interspersed non-ObjC++ code. */
30046 cp_parser_objc_interstitial_code (parser);
30048 token = cp_lexer_peek_token (parser->lexer);
30051 if (token->type != CPP_EOF)
30052 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30053 else
30054 cp_parser_error (parser, "expected %<@end%>");
30056 objc_finish_interface ();
30059 /* Parse an Objective-C method definition list. */
30061 static void
30062 cp_parser_objc_method_definition_list (cp_parser* parser)
30064 cp_token *token = cp_lexer_peek_token (parser->lexer);
30066 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
30068 tree meth;
30070 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
30072 cp_token *ptk;
30073 tree sig, attribute;
30074 bool is_class_method;
30075 if (token->type == CPP_PLUS)
30076 is_class_method = true;
30077 else
30078 is_class_method = false;
30079 push_deferring_access_checks (dk_deferred);
30080 sig = cp_parser_objc_method_signature (parser, &attribute);
30081 if (sig == error_mark_node)
30083 cp_parser_skip_to_end_of_block_or_statement (parser);
30084 token = cp_lexer_peek_token (parser->lexer);
30085 continue;
30087 objc_start_method_definition (is_class_method, sig, attribute,
30088 NULL_TREE);
30090 /* For historical reasons, we accept an optional semicolon. */
30091 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30092 cp_lexer_consume_token (parser->lexer);
30094 ptk = cp_lexer_peek_token (parser->lexer);
30095 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
30096 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
30098 perform_deferred_access_checks (tf_warning_or_error);
30099 stop_deferring_access_checks ();
30100 meth = cp_parser_function_definition_after_declarator (parser,
30101 false);
30102 pop_deferring_access_checks ();
30103 objc_finish_method_definition (meth);
30106 /* The following case will be removed once @synthesize is
30107 completely implemented. */
30108 else if (token->keyword == RID_AT_PROPERTY)
30109 cp_parser_objc_at_property_declaration (parser);
30110 else if (token->keyword == RID_AT_SYNTHESIZE)
30111 cp_parser_objc_at_synthesize_declaration (parser);
30112 else if (token->keyword == RID_AT_DYNAMIC)
30113 cp_parser_objc_at_dynamic_declaration (parser);
30114 else if (token->keyword == RID_ATTRIBUTE
30115 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30116 warning_at (token->location, OPT_Wattributes,
30117 "prefix attributes are ignored for methods");
30118 else
30119 /* Allow for interspersed non-ObjC++ code. */
30120 cp_parser_objc_interstitial_code (parser);
30122 token = cp_lexer_peek_token (parser->lexer);
30125 if (token->type != CPP_EOF)
30126 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30127 else
30128 cp_parser_error (parser, "expected %<@end%>");
30130 objc_finish_implementation ();
30133 /* Parse Objective-C ivars. */
30135 static void
30136 cp_parser_objc_class_ivars (cp_parser* parser)
30138 cp_token *token = cp_lexer_peek_token (parser->lexer);
30140 if (token->type != CPP_OPEN_BRACE)
30141 return; /* No ivars specified. */
30143 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30144 token = cp_lexer_peek_token (parser->lexer);
30146 while (token->type != CPP_CLOSE_BRACE
30147 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30149 cp_decl_specifier_seq declspecs;
30150 int decl_class_or_enum_p;
30151 tree prefix_attributes;
30153 cp_parser_objc_visibility_spec (parser);
30155 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30156 break;
30158 cp_parser_decl_specifier_seq (parser,
30159 CP_PARSER_FLAGS_OPTIONAL,
30160 &declspecs,
30161 &decl_class_or_enum_p);
30163 /* auto, register, static, extern, mutable. */
30164 if (declspecs.storage_class != sc_none)
30166 cp_parser_error (parser, "invalid type for instance variable");
30167 declspecs.storage_class = sc_none;
30170 /* thread_local. */
30171 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30173 cp_parser_error (parser, "invalid type for instance variable");
30174 declspecs.locations[ds_thread] = 0;
30177 /* typedef. */
30178 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30180 cp_parser_error (parser, "invalid type for instance variable");
30181 declspecs.locations[ds_typedef] = 0;
30184 prefix_attributes = declspecs.attributes;
30185 declspecs.attributes = NULL_TREE;
30187 /* Keep going until we hit the `;' at the end of the
30188 declaration. */
30189 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30191 tree width = NULL_TREE, attributes, first_attribute, decl;
30192 cp_declarator *declarator = NULL;
30193 int ctor_dtor_or_conv_p;
30195 /* Check for a (possibly unnamed) bitfield declaration. */
30196 token = cp_lexer_peek_token (parser->lexer);
30197 if (token->type == CPP_COLON)
30198 goto eat_colon;
30200 if (token->type == CPP_NAME
30201 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30202 == CPP_COLON))
30204 /* Get the name of the bitfield. */
30205 declarator = make_id_declarator (NULL_TREE,
30206 cp_parser_identifier (parser),
30207 sfk_none);
30209 eat_colon:
30210 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30211 /* Get the width of the bitfield. */
30212 width
30213 = cp_parser_constant_expression (parser);
30215 else
30217 /* Parse the declarator. */
30218 declarator
30219 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30220 &ctor_dtor_or_conv_p,
30221 /*parenthesized_p=*/NULL,
30222 /*member_p=*/false,
30223 /*friend_p=*/false);
30226 /* Look for attributes that apply to the ivar. */
30227 attributes = cp_parser_attributes_opt (parser);
30228 /* Remember which attributes are prefix attributes and
30229 which are not. */
30230 first_attribute = attributes;
30231 /* Combine the attributes. */
30232 attributes = chainon (prefix_attributes, attributes);
30234 if (width)
30235 /* Create the bitfield declaration. */
30236 decl = grokbitfield (declarator, &declspecs,
30237 width, NULL_TREE, attributes);
30238 else
30239 decl = grokfield (declarator, &declspecs,
30240 NULL_TREE, /*init_const_expr_p=*/false,
30241 NULL_TREE, attributes);
30243 /* Add the instance variable. */
30244 if (decl != error_mark_node && decl != NULL_TREE)
30245 objc_add_instance_variable (decl);
30247 /* Reset PREFIX_ATTRIBUTES. */
30248 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30249 attributes = TREE_CHAIN (attributes);
30250 if (attributes)
30251 TREE_CHAIN (attributes) = NULL_TREE;
30253 token = cp_lexer_peek_token (parser->lexer);
30255 if (token->type == CPP_COMMA)
30257 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30258 continue;
30260 break;
30263 cp_parser_consume_semicolon_at_end_of_statement (parser);
30264 token = cp_lexer_peek_token (parser->lexer);
30267 if (token->keyword == RID_AT_END)
30268 cp_parser_error (parser, "expected %<}%>");
30270 /* Do not consume the RID_AT_END, so it will be read again as terminating
30271 the @interface of @implementation. */
30272 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30273 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30275 /* For historical reasons, we accept an optional semicolon. */
30276 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30277 cp_lexer_consume_token (parser->lexer);
30280 /* Parse an Objective-C protocol declaration. */
30282 static void
30283 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30285 tree proto, protorefs;
30286 cp_token *tok;
30288 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30289 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30291 tok = cp_lexer_peek_token (parser->lexer);
30292 error_at (tok->location, "identifier expected after %<@protocol%>");
30293 cp_parser_consume_semicolon_at_end_of_statement (parser);
30294 return;
30297 /* See if we have a forward declaration or a definition. */
30298 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30300 /* Try a forward declaration first. */
30301 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30303 while (true)
30305 tree id;
30307 id = cp_parser_identifier (parser);
30308 if (id == error_mark_node)
30309 break;
30311 objc_declare_protocol (id, attributes);
30313 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30314 cp_lexer_consume_token (parser->lexer);
30315 else
30316 break;
30318 cp_parser_consume_semicolon_at_end_of_statement (parser);
30321 /* Ok, we got a full-fledged definition (or at least should). */
30322 else
30324 proto = cp_parser_identifier (parser);
30325 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30326 objc_start_protocol (proto, protorefs, attributes);
30327 cp_parser_objc_method_prototype_list (parser);
30331 /* Parse an Objective-C superclass or category. */
30333 static void
30334 cp_parser_objc_superclass_or_category (cp_parser *parser,
30335 bool iface_p,
30336 tree *super,
30337 tree *categ, bool *is_class_extension)
30339 cp_token *next = cp_lexer_peek_token (parser->lexer);
30341 *super = *categ = NULL_TREE;
30342 *is_class_extension = false;
30343 if (next->type == CPP_COLON)
30345 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30346 *super = cp_parser_identifier (parser);
30348 else if (next->type == CPP_OPEN_PAREN)
30350 matching_parens parens;
30351 parens.consume_open (parser); /* Eat '('. */
30353 /* If there is no category name, and this is an @interface, we
30354 have a class extension. */
30355 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30357 *categ = NULL_TREE;
30358 *is_class_extension = true;
30360 else
30361 *categ = cp_parser_identifier (parser);
30363 parens.require_close (parser);
30367 /* Parse an Objective-C class interface. */
30369 static void
30370 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30372 tree name, super, categ, protos;
30373 bool is_class_extension;
30375 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30376 name = cp_parser_identifier (parser);
30377 if (name == error_mark_node)
30379 /* It's hard to recover because even if valid @interface stuff
30380 is to follow, we can't compile it (or validate it) if we
30381 don't even know which class it refers to. Let's assume this
30382 was a stray '@interface' token in the stream and skip it.
30384 return;
30386 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30387 &is_class_extension);
30388 protos = cp_parser_objc_protocol_refs_opt (parser);
30390 /* We have either a class or a category on our hands. */
30391 if (categ || is_class_extension)
30392 objc_start_category_interface (name, categ, protos, attributes);
30393 else
30395 objc_start_class_interface (name, super, protos, attributes);
30396 /* Handle instance variable declarations, if any. */
30397 cp_parser_objc_class_ivars (parser);
30398 objc_continue_interface ();
30401 cp_parser_objc_method_prototype_list (parser);
30404 /* Parse an Objective-C class implementation. */
30406 static void
30407 cp_parser_objc_class_implementation (cp_parser* parser)
30409 tree name, super, categ;
30410 bool is_class_extension;
30412 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30413 name = cp_parser_identifier (parser);
30414 if (name == error_mark_node)
30416 /* It's hard to recover because even if valid @implementation
30417 stuff is to follow, we can't compile it (or validate it) if
30418 we don't even know which class it refers to. Let's assume
30419 this was a stray '@implementation' token in the stream and
30420 skip it.
30422 return;
30424 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30425 &is_class_extension);
30427 /* We have either a class or a category on our hands. */
30428 if (categ)
30429 objc_start_category_implementation (name, categ);
30430 else
30432 objc_start_class_implementation (name, super);
30433 /* Handle instance variable declarations, if any. */
30434 cp_parser_objc_class_ivars (parser);
30435 objc_continue_implementation ();
30438 cp_parser_objc_method_definition_list (parser);
30441 /* Consume the @end token and finish off the implementation. */
30443 static void
30444 cp_parser_objc_end_implementation (cp_parser* parser)
30446 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30447 objc_finish_implementation ();
30450 /* Parse an Objective-C declaration. */
30452 static void
30453 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30455 /* Try to figure out what kind of declaration is present. */
30456 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30458 if (attributes)
30459 switch (kwd->keyword)
30461 case RID_AT_ALIAS:
30462 case RID_AT_CLASS:
30463 case RID_AT_END:
30464 error_at (kwd->location, "attributes may not be specified before"
30465 " the %<@%D%> Objective-C++ keyword",
30466 kwd->u.value);
30467 attributes = NULL;
30468 break;
30469 case RID_AT_IMPLEMENTATION:
30470 warning_at (kwd->location, OPT_Wattributes,
30471 "prefix attributes are ignored before %<@%D%>",
30472 kwd->u.value);
30473 attributes = NULL;
30474 default:
30475 break;
30478 switch (kwd->keyword)
30480 case RID_AT_ALIAS:
30481 cp_parser_objc_alias_declaration (parser);
30482 break;
30483 case RID_AT_CLASS:
30484 cp_parser_objc_class_declaration (parser);
30485 break;
30486 case RID_AT_PROTOCOL:
30487 cp_parser_objc_protocol_declaration (parser, attributes);
30488 break;
30489 case RID_AT_INTERFACE:
30490 cp_parser_objc_class_interface (parser, attributes);
30491 break;
30492 case RID_AT_IMPLEMENTATION:
30493 cp_parser_objc_class_implementation (parser);
30494 break;
30495 case RID_AT_END:
30496 cp_parser_objc_end_implementation (parser);
30497 break;
30498 default:
30499 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30500 kwd->u.value);
30501 cp_parser_skip_to_end_of_block_or_statement (parser);
30505 /* Parse an Objective-C try-catch-finally statement.
30507 objc-try-catch-finally-stmt:
30508 @try compound-statement objc-catch-clause-seq [opt]
30509 objc-finally-clause [opt]
30511 objc-catch-clause-seq:
30512 objc-catch-clause objc-catch-clause-seq [opt]
30514 objc-catch-clause:
30515 @catch ( objc-exception-declaration ) compound-statement
30517 objc-finally-clause:
30518 @finally compound-statement
30520 objc-exception-declaration:
30521 parameter-declaration
30522 '...'
30524 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30526 Returns NULL_TREE.
30528 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30529 for C. Keep them in sync. */
30531 static tree
30532 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30534 location_t location;
30535 tree stmt;
30537 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30538 location = cp_lexer_peek_token (parser->lexer)->location;
30539 objc_maybe_warn_exceptions (location);
30540 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30541 node, lest it get absorbed into the surrounding block. */
30542 stmt = push_stmt_list ();
30543 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30544 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30546 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30548 cp_parameter_declarator *parm;
30549 tree parameter_declaration = error_mark_node;
30550 bool seen_open_paren = false;
30551 matching_parens parens;
30553 cp_lexer_consume_token (parser->lexer);
30554 if (parens.require_open (parser))
30555 seen_open_paren = true;
30556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30558 /* We have "@catch (...)" (where the '...' are literally
30559 what is in the code). Skip the '...'.
30560 parameter_declaration is set to NULL_TREE, and
30561 objc_being_catch_clauses() knows that that means
30562 '...'. */
30563 cp_lexer_consume_token (parser->lexer);
30564 parameter_declaration = NULL_TREE;
30566 else
30568 /* We have "@catch (NSException *exception)" or something
30569 like that. Parse the parameter declaration. */
30570 parm = cp_parser_parameter_declaration (parser, false, NULL);
30571 if (parm == NULL)
30572 parameter_declaration = error_mark_node;
30573 else
30574 parameter_declaration = grokdeclarator (parm->declarator,
30575 &parm->decl_specifiers,
30576 PARM, /*initialized=*/0,
30577 /*attrlist=*/NULL);
30579 if (seen_open_paren)
30580 parens.require_close (parser);
30581 else
30583 /* If there was no open parenthesis, we are recovering from
30584 an error, and we are trying to figure out what mistake
30585 the user has made. */
30587 /* If there is an immediate closing parenthesis, the user
30588 probably forgot the opening one (ie, they typed "@catch
30589 NSException *e)". Parse the closing parenthesis and keep
30590 going. */
30591 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30592 cp_lexer_consume_token (parser->lexer);
30594 /* If these is no immediate closing parenthesis, the user
30595 probably doesn't know that parenthesis are required at
30596 all (ie, they typed "@catch NSException *e"). So, just
30597 forget about the closing parenthesis and keep going. */
30599 objc_begin_catch_clause (parameter_declaration);
30600 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30601 objc_finish_catch_clause ();
30603 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30605 cp_lexer_consume_token (parser->lexer);
30606 location = cp_lexer_peek_token (parser->lexer)->location;
30607 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30608 node, lest it get absorbed into the surrounding block. */
30609 stmt = push_stmt_list ();
30610 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30611 objc_build_finally_clause (location, pop_stmt_list (stmt));
30614 return objc_finish_try_stmt ();
30617 /* Parse an Objective-C synchronized statement.
30619 objc-synchronized-stmt:
30620 @synchronized ( expression ) compound-statement
30622 Returns NULL_TREE. */
30624 static tree
30625 cp_parser_objc_synchronized_statement (cp_parser *parser)
30627 location_t location;
30628 tree lock, stmt;
30630 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30632 location = cp_lexer_peek_token (parser->lexer)->location;
30633 objc_maybe_warn_exceptions (location);
30634 matching_parens parens;
30635 parens.require_open (parser);
30636 lock = cp_parser_expression (parser);
30637 parens.require_close (parser);
30639 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30640 node, lest it get absorbed into the surrounding block. */
30641 stmt = push_stmt_list ();
30642 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30644 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30647 /* Parse an Objective-C throw statement.
30649 objc-throw-stmt:
30650 @throw assignment-expression [opt] ;
30652 Returns a constructed '@throw' statement. */
30654 static tree
30655 cp_parser_objc_throw_statement (cp_parser *parser)
30657 tree expr = NULL_TREE;
30658 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30660 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30663 expr = cp_parser_expression (parser);
30665 cp_parser_consume_semicolon_at_end_of_statement (parser);
30667 return objc_build_throw_stmt (loc, expr);
30670 /* Parse an Objective-C statement. */
30672 static tree
30673 cp_parser_objc_statement (cp_parser * parser)
30675 /* Try to figure out what kind of declaration is present. */
30676 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30678 switch (kwd->keyword)
30680 case RID_AT_TRY:
30681 return cp_parser_objc_try_catch_finally_statement (parser);
30682 case RID_AT_SYNCHRONIZED:
30683 return cp_parser_objc_synchronized_statement (parser);
30684 case RID_AT_THROW:
30685 return cp_parser_objc_throw_statement (parser);
30686 default:
30687 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30688 kwd->u.value);
30689 cp_parser_skip_to_end_of_block_or_statement (parser);
30692 return error_mark_node;
30695 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30696 look ahead to see if an objc keyword follows the attributes. This
30697 is to detect the use of prefix attributes on ObjC @interface and
30698 @protocol. */
30700 static bool
30701 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30703 cp_lexer_save_tokens (parser->lexer);
30704 *attrib = cp_parser_attributes_opt (parser);
30705 gcc_assert (*attrib);
30706 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30708 cp_lexer_commit_tokens (parser->lexer);
30709 return true;
30711 cp_lexer_rollback_tokens (parser->lexer);
30712 return false;
30715 /* This routine is a minimal replacement for
30716 c_parser_struct_declaration () used when parsing the list of
30717 types/names or ObjC++ properties. For example, when parsing the
30718 code
30720 @property (readonly) int a, b, c;
30722 this function is responsible for parsing "int a, int b, int c" and
30723 returning the declarations as CHAIN of DECLs.
30725 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30726 similar parsing. */
30727 static tree
30728 cp_parser_objc_struct_declaration (cp_parser *parser)
30730 tree decls = NULL_TREE;
30731 cp_decl_specifier_seq declspecs;
30732 int decl_class_or_enum_p;
30733 tree prefix_attributes;
30735 cp_parser_decl_specifier_seq (parser,
30736 CP_PARSER_FLAGS_NONE,
30737 &declspecs,
30738 &decl_class_or_enum_p);
30740 if (declspecs.type == error_mark_node)
30741 return error_mark_node;
30743 /* auto, register, static, extern, mutable. */
30744 if (declspecs.storage_class != sc_none)
30746 cp_parser_error (parser, "invalid type for property");
30747 declspecs.storage_class = sc_none;
30750 /* thread_local. */
30751 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30753 cp_parser_error (parser, "invalid type for property");
30754 declspecs.locations[ds_thread] = 0;
30757 /* typedef. */
30758 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30760 cp_parser_error (parser, "invalid type for property");
30761 declspecs.locations[ds_typedef] = 0;
30764 prefix_attributes = declspecs.attributes;
30765 declspecs.attributes = NULL_TREE;
30767 /* Keep going until we hit the `;' at the end of the declaration. */
30768 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30770 tree attributes, first_attribute, decl;
30771 cp_declarator *declarator;
30772 cp_token *token;
30774 /* Parse the declarator. */
30775 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30776 NULL, NULL, false, false);
30778 /* Look for attributes that apply to the ivar. */
30779 attributes = cp_parser_attributes_opt (parser);
30780 /* Remember which attributes are prefix attributes and
30781 which are not. */
30782 first_attribute = attributes;
30783 /* Combine the attributes. */
30784 attributes = chainon (prefix_attributes, attributes);
30786 decl = grokfield (declarator, &declspecs,
30787 NULL_TREE, /*init_const_expr_p=*/false,
30788 NULL_TREE, attributes);
30790 if (decl == error_mark_node || decl == NULL_TREE)
30791 return error_mark_node;
30793 /* Reset PREFIX_ATTRIBUTES. */
30794 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30795 attributes = TREE_CHAIN (attributes);
30796 if (attributes)
30797 TREE_CHAIN (attributes) = NULL_TREE;
30799 DECL_CHAIN (decl) = decls;
30800 decls = decl;
30802 token = cp_lexer_peek_token (parser->lexer);
30803 if (token->type == CPP_COMMA)
30805 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30806 continue;
30808 else
30809 break;
30811 return decls;
30814 /* Parse an Objective-C @property declaration. The syntax is:
30816 objc-property-declaration:
30817 '@property' objc-property-attributes[opt] struct-declaration ;
30819 objc-property-attributes:
30820 '(' objc-property-attribute-list ')'
30822 objc-property-attribute-list:
30823 objc-property-attribute
30824 objc-property-attribute-list, objc-property-attribute
30826 objc-property-attribute
30827 'getter' = identifier
30828 'setter' = identifier
30829 'readonly'
30830 'readwrite'
30831 'assign'
30832 'retain'
30833 'copy'
30834 'nonatomic'
30836 For example:
30837 @property NSString *name;
30838 @property (readonly) id object;
30839 @property (retain, nonatomic, getter=getTheName) id name;
30840 @property int a, b, c;
30842 PS: This function is identical to
30843 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30844 static void
30845 cp_parser_objc_at_property_declaration (cp_parser *parser)
30847 /* The following variables hold the attributes of the properties as
30848 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30849 seen. When we see an attribute, we set them to 'true' (if they
30850 are boolean properties) or to the identifier (if they have an
30851 argument, ie, for getter and setter). Note that here we only
30852 parse the list of attributes, check the syntax and accumulate the
30853 attributes that we find. objc_add_property_declaration() will
30854 then process the information. */
30855 bool property_assign = false;
30856 bool property_copy = false;
30857 tree property_getter_ident = NULL_TREE;
30858 bool property_nonatomic = false;
30859 bool property_readonly = false;
30860 bool property_readwrite = false;
30861 bool property_retain = false;
30862 tree property_setter_ident = NULL_TREE;
30864 /* 'properties' is the list of properties that we read. Usually a
30865 single one, but maybe more (eg, in "@property int a, b, c;" there
30866 are three). */
30867 tree properties;
30868 location_t loc;
30870 loc = cp_lexer_peek_token (parser->lexer)->location;
30872 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30874 /* Parse the optional attribute list... */
30875 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30877 /* Eat the '('. */
30878 matching_parens parens;
30879 parens.consume_open (parser);
30881 while (true)
30883 bool syntax_error = false;
30884 cp_token *token = cp_lexer_peek_token (parser->lexer);
30885 enum rid keyword;
30887 if (token->type != CPP_NAME)
30889 cp_parser_error (parser, "expected identifier");
30890 break;
30892 keyword = C_RID_CODE (token->u.value);
30893 cp_lexer_consume_token (parser->lexer);
30894 switch (keyword)
30896 case RID_ASSIGN: property_assign = true; break;
30897 case RID_COPY: property_copy = true; break;
30898 case RID_NONATOMIC: property_nonatomic = true; break;
30899 case RID_READONLY: property_readonly = true; break;
30900 case RID_READWRITE: property_readwrite = true; break;
30901 case RID_RETAIN: property_retain = true; break;
30903 case RID_GETTER:
30904 case RID_SETTER:
30905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30907 if (keyword == RID_GETTER)
30908 cp_parser_error (parser,
30909 "missing %<=%> (after %<getter%> attribute)");
30910 else
30911 cp_parser_error (parser,
30912 "missing %<=%> (after %<setter%> attribute)");
30913 syntax_error = true;
30914 break;
30916 cp_lexer_consume_token (parser->lexer); /* eat the = */
30917 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30919 cp_parser_error (parser, "expected identifier");
30920 syntax_error = true;
30921 break;
30923 if (keyword == RID_SETTER)
30925 if (property_setter_ident != NULL_TREE)
30927 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30928 cp_lexer_consume_token (parser->lexer);
30930 else
30931 property_setter_ident = cp_parser_objc_selector (parser);
30932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30933 cp_parser_error (parser, "setter name must terminate with %<:%>");
30934 else
30935 cp_lexer_consume_token (parser->lexer);
30937 else
30939 if (property_getter_ident != NULL_TREE)
30941 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30942 cp_lexer_consume_token (parser->lexer);
30944 else
30945 property_getter_ident = cp_parser_objc_selector (parser);
30947 break;
30948 default:
30949 cp_parser_error (parser, "unknown property attribute");
30950 syntax_error = true;
30951 break;
30954 if (syntax_error)
30955 break;
30957 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30958 cp_lexer_consume_token (parser->lexer);
30959 else
30960 break;
30963 /* FIXME: "@property (setter, assign);" will generate a spurious
30964 "error: expected ‘)’ before ‘,’ token". This is because
30965 cp_parser_require, unlike the C counterpart, will produce an
30966 error even if we are in error recovery. */
30967 if (!parens.require_close (parser))
30969 cp_parser_skip_to_closing_parenthesis (parser,
30970 /*recovering=*/true,
30971 /*or_comma=*/false,
30972 /*consume_paren=*/true);
30976 /* ... and the property declaration(s). */
30977 properties = cp_parser_objc_struct_declaration (parser);
30979 if (properties == error_mark_node)
30981 cp_parser_skip_to_end_of_statement (parser);
30982 /* If the next token is now a `;', consume it. */
30983 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30984 cp_lexer_consume_token (parser->lexer);
30985 return;
30988 if (properties == NULL_TREE)
30989 cp_parser_error (parser, "expected identifier");
30990 else
30992 /* Comma-separated properties are chained together in
30993 reverse order; add them one by one. */
30994 properties = nreverse (properties);
30996 for (; properties; properties = TREE_CHAIN (properties))
30997 objc_add_property_declaration (loc, copy_node (properties),
30998 property_readonly, property_readwrite,
30999 property_assign, property_retain,
31000 property_copy, property_nonatomic,
31001 property_getter_ident, property_setter_ident);
31004 cp_parser_consume_semicolon_at_end_of_statement (parser);
31007 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
31009 objc-synthesize-declaration:
31010 @synthesize objc-synthesize-identifier-list ;
31012 objc-synthesize-identifier-list:
31013 objc-synthesize-identifier
31014 objc-synthesize-identifier-list, objc-synthesize-identifier
31016 objc-synthesize-identifier
31017 identifier
31018 identifier = identifier
31020 For example:
31021 @synthesize MyProperty;
31022 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
31024 PS: This function is identical to c_parser_objc_at_synthesize_declaration
31025 for C. Keep them in sync.
31027 static void
31028 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
31030 tree list = NULL_TREE;
31031 location_t loc;
31032 loc = cp_lexer_peek_token (parser->lexer)->location;
31034 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
31035 while (true)
31037 tree property, ivar;
31038 property = cp_parser_identifier (parser);
31039 if (property == error_mark_node)
31041 cp_parser_consume_semicolon_at_end_of_statement (parser);
31042 return;
31044 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
31046 cp_lexer_consume_token (parser->lexer);
31047 ivar = cp_parser_identifier (parser);
31048 if (ivar == error_mark_node)
31050 cp_parser_consume_semicolon_at_end_of_statement (parser);
31051 return;
31054 else
31055 ivar = NULL_TREE;
31056 list = chainon (list, build_tree_list (ivar, property));
31057 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31058 cp_lexer_consume_token (parser->lexer);
31059 else
31060 break;
31062 cp_parser_consume_semicolon_at_end_of_statement (parser);
31063 objc_add_synthesize_declaration (loc, list);
31066 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
31068 objc-dynamic-declaration:
31069 @dynamic identifier-list ;
31071 For example:
31072 @dynamic MyProperty;
31073 @dynamic MyProperty, AnotherProperty;
31075 PS: This function is identical to c_parser_objc_at_dynamic_declaration
31076 for C. Keep them in sync.
31078 static void
31079 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
31081 tree list = NULL_TREE;
31082 location_t loc;
31083 loc = cp_lexer_peek_token (parser->lexer)->location;
31085 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
31086 while (true)
31088 tree property;
31089 property = cp_parser_identifier (parser);
31090 if (property == error_mark_node)
31092 cp_parser_consume_semicolon_at_end_of_statement (parser);
31093 return;
31095 list = chainon (list, build_tree_list (NULL, property));
31096 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31097 cp_lexer_consume_token (parser->lexer);
31098 else
31099 break;
31101 cp_parser_consume_semicolon_at_end_of_statement (parser);
31102 objc_add_dynamic_declaration (loc, list);
31106 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31108 /* Returns name of the next clause.
31109 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31110 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31111 returned and the token is consumed. */
31113 static pragma_omp_clause
31114 cp_parser_omp_clause_name (cp_parser *parser)
31116 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31118 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31119 result = PRAGMA_OACC_CLAUSE_AUTO;
31120 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31121 result = PRAGMA_OMP_CLAUSE_IF;
31122 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31123 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31124 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31125 result = PRAGMA_OACC_CLAUSE_DELETE;
31126 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31127 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31128 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31129 result = PRAGMA_OMP_CLAUSE_FOR;
31130 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31132 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31133 const char *p = IDENTIFIER_POINTER (id);
31135 switch (p[0])
31137 case 'a':
31138 if (!strcmp ("aligned", p))
31139 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31140 else if (!strcmp ("async", p))
31141 result = PRAGMA_OACC_CLAUSE_ASYNC;
31142 break;
31143 case 'c':
31144 if (!strcmp ("collapse", p))
31145 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31146 else if (!strcmp ("copy", p))
31147 result = PRAGMA_OACC_CLAUSE_COPY;
31148 else if (!strcmp ("copyin", p))
31149 result = PRAGMA_OMP_CLAUSE_COPYIN;
31150 else if (!strcmp ("copyout", p))
31151 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31152 else if (!strcmp ("copyprivate", p))
31153 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31154 else if (!strcmp ("create", p))
31155 result = PRAGMA_OACC_CLAUSE_CREATE;
31156 break;
31157 case 'd':
31158 if (!strcmp ("defaultmap", p))
31159 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31160 else if (!strcmp ("depend", p))
31161 result = PRAGMA_OMP_CLAUSE_DEPEND;
31162 else if (!strcmp ("device", p))
31163 result = PRAGMA_OMP_CLAUSE_DEVICE;
31164 else if (!strcmp ("deviceptr", p))
31165 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31166 else if (!strcmp ("device_resident", p))
31167 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31168 else if (!strcmp ("dist_schedule", p))
31169 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31170 break;
31171 case 'f':
31172 if (!strcmp ("final", p))
31173 result = PRAGMA_OMP_CLAUSE_FINAL;
31174 else if (!strcmp ("firstprivate", p))
31175 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31176 else if (!strcmp ("from", p))
31177 result = PRAGMA_OMP_CLAUSE_FROM;
31178 break;
31179 case 'g':
31180 if (!strcmp ("gang", p))
31181 result = PRAGMA_OACC_CLAUSE_GANG;
31182 else if (!strcmp ("grainsize", p))
31183 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31184 break;
31185 case 'h':
31186 if (!strcmp ("hint", p))
31187 result = PRAGMA_OMP_CLAUSE_HINT;
31188 else if (!strcmp ("host", p))
31189 result = PRAGMA_OACC_CLAUSE_HOST;
31190 break;
31191 case 'i':
31192 if (!strcmp ("inbranch", p))
31193 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31194 else if (!strcmp ("independent", p))
31195 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31196 else if (!strcmp ("is_device_ptr", p))
31197 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31198 break;
31199 case 'l':
31200 if (!strcmp ("lastprivate", p))
31201 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31202 else if (!strcmp ("linear", p))
31203 result = PRAGMA_OMP_CLAUSE_LINEAR;
31204 else if (!strcmp ("link", p))
31205 result = PRAGMA_OMP_CLAUSE_LINK;
31206 break;
31207 case 'm':
31208 if (!strcmp ("map", p))
31209 result = PRAGMA_OMP_CLAUSE_MAP;
31210 else if (!strcmp ("mergeable", p))
31211 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31212 else if (flag_cilkplus && !strcmp ("mask", p))
31213 result = PRAGMA_CILK_CLAUSE_MASK;
31214 break;
31215 case 'n':
31216 if (!strcmp ("nogroup", p))
31217 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31218 else if (!strcmp ("notinbranch", p))
31219 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31220 else if (!strcmp ("nowait", p))
31221 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31222 else if (flag_cilkplus && !strcmp ("nomask", p))
31223 result = PRAGMA_CILK_CLAUSE_NOMASK;
31224 else if (!strcmp ("num_gangs", p))
31225 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31226 else if (!strcmp ("num_tasks", p))
31227 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31228 else if (!strcmp ("num_teams", p))
31229 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31230 else if (!strcmp ("num_threads", p))
31231 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31232 else if (!strcmp ("num_workers", p))
31233 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31234 break;
31235 case 'o':
31236 if (!strcmp ("ordered", p))
31237 result = PRAGMA_OMP_CLAUSE_ORDERED;
31238 break;
31239 case 'p':
31240 if (!strcmp ("parallel", p))
31241 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31242 else if (!strcmp ("present", p))
31243 result = PRAGMA_OACC_CLAUSE_PRESENT;
31244 else if (!strcmp ("present_or_copy", p)
31245 || !strcmp ("pcopy", p))
31246 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31247 else if (!strcmp ("present_or_copyin", p)
31248 || !strcmp ("pcopyin", p))
31249 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31250 else if (!strcmp ("present_or_copyout", p)
31251 || !strcmp ("pcopyout", p))
31252 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31253 else if (!strcmp ("present_or_create", p)
31254 || !strcmp ("pcreate", p))
31255 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31256 else if (!strcmp ("priority", p))
31257 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31258 else if (!strcmp ("proc_bind", p))
31259 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31260 break;
31261 case 'r':
31262 if (!strcmp ("reduction", p))
31263 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31264 break;
31265 case 's':
31266 if (!strcmp ("safelen", p))
31267 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31268 else if (!strcmp ("schedule", p))
31269 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31270 else if (!strcmp ("sections", p))
31271 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31272 else if (!strcmp ("self", p))
31273 result = PRAGMA_OACC_CLAUSE_SELF;
31274 else if (!strcmp ("seq", p))
31275 result = PRAGMA_OACC_CLAUSE_SEQ;
31276 else if (!strcmp ("shared", p))
31277 result = PRAGMA_OMP_CLAUSE_SHARED;
31278 else if (!strcmp ("simd", p))
31279 result = PRAGMA_OMP_CLAUSE_SIMD;
31280 else if (!strcmp ("simdlen", p))
31281 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31282 break;
31283 case 't':
31284 if (!strcmp ("taskgroup", p))
31285 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31286 else if (!strcmp ("thread_limit", p))
31287 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31288 else if (!strcmp ("threads", p))
31289 result = PRAGMA_OMP_CLAUSE_THREADS;
31290 else if (!strcmp ("tile", p))
31291 result = PRAGMA_OACC_CLAUSE_TILE;
31292 else if (!strcmp ("to", p))
31293 result = PRAGMA_OMP_CLAUSE_TO;
31294 break;
31295 case 'u':
31296 if (!strcmp ("uniform", p))
31297 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31298 else if (!strcmp ("untied", p))
31299 result = PRAGMA_OMP_CLAUSE_UNTIED;
31300 else if (!strcmp ("use_device", p))
31301 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31302 else if (!strcmp ("use_device_ptr", p))
31303 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31304 break;
31305 case 'v':
31306 if (!strcmp ("vector", p))
31307 result = PRAGMA_OACC_CLAUSE_VECTOR;
31308 else if (!strcmp ("vector_length", p))
31309 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31310 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31311 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31312 break;
31313 case 'w':
31314 if (!strcmp ("wait", p))
31315 result = PRAGMA_OACC_CLAUSE_WAIT;
31316 else if (!strcmp ("worker", p))
31317 result = PRAGMA_OACC_CLAUSE_WORKER;
31318 break;
31322 if (result != PRAGMA_OMP_CLAUSE_NONE)
31323 cp_lexer_consume_token (parser->lexer);
31325 return result;
31328 /* Validate that a clause of the given type does not already exist. */
31330 static void
31331 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31332 const char *name, location_t location)
31334 tree c;
31336 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31337 if (OMP_CLAUSE_CODE (c) == code)
31339 error_at (location, "too many %qs clauses", name);
31340 break;
31344 /* OpenMP 2.5:
31345 variable-list:
31346 identifier
31347 variable-list , identifier
31349 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31350 colon). An opening parenthesis will have been consumed by the caller.
31352 If KIND is nonzero, create the appropriate node and install the decl
31353 in OMP_CLAUSE_DECL and add the node to the head of the list.
31355 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31356 return the list created.
31358 COLON can be NULL if only closing parenthesis should end the list,
31359 or pointer to bool which will receive false if the list is terminated
31360 by closing parenthesis or true if the list is terminated by colon. */
31362 static tree
31363 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31364 tree list, bool *colon)
31366 cp_token *token;
31367 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31368 if (colon)
31370 parser->colon_corrects_to_scope_p = false;
31371 *colon = false;
31373 while (1)
31375 tree name, decl;
31377 token = cp_lexer_peek_token (parser->lexer);
31378 if (kind != 0
31379 && current_class_ptr
31380 && cp_parser_is_keyword (token, RID_THIS))
31382 decl = finish_this_expr ();
31383 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31384 || CONVERT_EXPR_P (decl))
31385 decl = TREE_OPERAND (decl, 0);
31386 cp_lexer_consume_token (parser->lexer);
31388 else
31390 name = cp_parser_id_expression (parser, /*template_p=*/false,
31391 /*check_dependency_p=*/true,
31392 /*template_p=*/NULL,
31393 /*declarator_p=*/false,
31394 /*optional_p=*/false);
31395 if (name == error_mark_node)
31396 goto skip_comma;
31398 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31399 if (decl == error_mark_node)
31400 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31401 token->location);
31403 if (decl == error_mark_node)
31405 else if (kind != 0)
31407 switch (kind)
31409 case OMP_CLAUSE__CACHE_:
31410 /* The OpenACC cache directive explicitly only allows "array
31411 elements or subarrays". */
31412 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31414 error_at (token->location, "expected %<[%>");
31415 decl = error_mark_node;
31416 break;
31418 /* FALLTHROUGH. */
31419 case OMP_CLAUSE_MAP:
31420 case OMP_CLAUSE_FROM:
31421 case OMP_CLAUSE_TO:
31422 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31424 location_t loc
31425 = cp_lexer_peek_token (parser->lexer)->location;
31426 cp_id_kind idk = CP_ID_KIND_NONE;
31427 cp_lexer_consume_token (parser->lexer);
31428 decl = convert_from_reference (decl);
31429 decl
31430 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31431 decl, false,
31432 &idk, loc);
31434 /* FALLTHROUGH. */
31435 case OMP_CLAUSE_DEPEND:
31436 case OMP_CLAUSE_REDUCTION:
31437 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31439 tree low_bound = NULL_TREE, length = NULL_TREE;
31441 parser->colon_corrects_to_scope_p = false;
31442 cp_lexer_consume_token (parser->lexer);
31443 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31444 low_bound = cp_parser_expression (parser);
31445 if (!colon)
31446 parser->colon_corrects_to_scope_p
31447 = saved_colon_corrects_to_scope_p;
31448 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31449 length = integer_one_node;
31450 else
31452 /* Look for `:'. */
31453 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31454 goto skip_comma;
31455 if (!cp_lexer_next_token_is (parser->lexer,
31456 CPP_CLOSE_SQUARE))
31457 length = cp_parser_expression (parser);
31459 /* Look for the closing `]'. */
31460 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31461 RT_CLOSE_SQUARE))
31462 goto skip_comma;
31464 decl = tree_cons (low_bound, length, decl);
31466 break;
31467 default:
31468 break;
31471 tree u = build_omp_clause (token->location, kind);
31472 OMP_CLAUSE_DECL (u) = decl;
31473 OMP_CLAUSE_CHAIN (u) = list;
31474 list = u;
31476 else
31477 list = tree_cons (decl, NULL_TREE, list);
31479 get_comma:
31480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31481 break;
31482 cp_lexer_consume_token (parser->lexer);
31485 if (colon)
31486 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31488 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31490 *colon = true;
31491 cp_parser_require (parser, CPP_COLON, RT_COLON);
31492 return list;
31495 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31497 int ending;
31499 /* Try to resync to an unnested comma. Copied from
31500 cp_parser_parenthesized_expression_list. */
31501 skip_comma:
31502 if (colon)
31503 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31504 ending = cp_parser_skip_to_closing_parenthesis (parser,
31505 /*recovering=*/true,
31506 /*or_comma=*/true,
31507 /*consume_paren=*/true);
31508 if (ending < 0)
31509 goto get_comma;
31512 return list;
31515 /* Similarly, but expect leading and trailing parenthesis. This is a very
31516 common case for omp clauses. */
31518 static tree
31519 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31521 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31522 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31523 return list;
31526 /* OpenACC 2.0:
31527 copy ( variable-list )
31528 copyin ( variable-list )
31529 copyout ( variable-list )
31530 create ( variable-list )
31531 delete ( variable-list )
31532 present ( variable-list )
31533 present_or_copy ( variable-list )
31534 pcopy ( variable-list )
31535 present_or_copyin ( variable-list )
31536 pcopyin ( variable-list )
31537 present_or_copyout ( variable-list )
31538 pcopyout ( variable-list )
31539 present_or_create ( variable-list )
31540 pcreate ( variable-list ) */
31542 static tree
31543 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31544 tree list)
31546 enum gomp_map_kind kind;
31547 switch (c_kind)
31549 case PRAGMA_OACC_CLAUSE_COPY:
31550 kind = GOMP_MAP_FORCE_TOFROM;
31551 break;
31552 case PRAGMA_OACC_CLAUSE_COPYIN:
31553 kind = GOMP_MAP_FORCE_TO;
31554 break;
31555 case PRAGMA_OACC_CLAUSE_COPYOUT:
31556 kind = GOMP_MAP_FORCE_FROM;
31557 break;
31558 case PRAGMA_OACC_CLAUSE_CREATE:
31559 kind = GOMP_MAP_FORCE_ALLOC;
31560 break;
31561 case PRAGMA_OACC_CLAUSE_DELETE:
31562 kind = GOMP_MAP_DELETE;
31563 break;
31564 case PRAGMA_OACC_CLAUSE_DEVICE:
31565 kind = GOMP_MAP_FORCE_TO;
31566 break;
31567 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31568 kind = GOMP_MAP_DEVICE_RESIDENT;
31569 break;
31570 case PRAGMA_OACC_CLAUSE_HOST:
31571 case PRAGMA_OACC_CLAUSE_SELF:
31572 kind = GOMP_MAP_FORCE_FROM;
31573 break;
31574 case PRAGMA_OACC_CLAUSE_LINK:
31575 kind = GOMP_MAP_LINK;
31576 break;
31577 case PRAGMA_OACC_CLAUSE_PRESENT:
31578 kind = GOMP_MAP_FORCE_PRESENT;
31579 break;
31580 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31581 kind = GOMP_MAP_TOFROM;
31582 break;
31583 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31584 kind = GOMP_MAP_TO;
31585 break;
31586 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31587 kind = GOMP_MAP_FROM;
31588 break;
31589 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31590 kind = GOMP_MAP_ALLOC;
31591 break;
31592 default:
31593 gcc_unreachable ();
31595 tree nl, c;
31596 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31598 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31599 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31601 return nl;
31604 /* OpenACC 2.0:
31605 deviceptr ( variable-list ) */
31607 static tree
31608 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31610 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31611 tree vars, t;
31613 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31614 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31615 variable-list must only allow for pointer variables. */
31616 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31617 for (t = vars; t; t = TREE_CHAIN (t))
31619 tree v = TREE_PURPOSE (t);
31620 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31621 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31622 OMP_CLAUSE_DECL (u) = v;
31623 OMP_CLAUSE_CHAIN (u) = list;
31624 list = u;
31627 return list;
31630 /* OpenACC 2.0:
31631 auto
31632 independent
31633 nohost
31634 seq */
31636 static tree
31637 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31638 enum omp_clause_code code,
31639 tree list, location_t location)
31641 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31642 tree c = build_omp_clause (location, code);
31643 OMP_CLAUSE_CHAIN (c) = list;
31644 return c;
31647 /* OpenACC:
31648 num_gangs ( expression )
31649 num_workers ( expression )
31650 vector_length ( expression ) */
31652 static tree
31653 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31654 const char *str, tree list)
31656 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31658 matching_parens parens;
31659 if (!parens.require_open (parser))
31660 return list;
31662 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31664 if (t == error_mark_node
31665 || !parens.require_close (parser))
31667 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31668 /*or_comma=*/false,
31669 /*consume_paren=*/true);
31670 return list;
31673 check_no_duplicate_clause (list, code, str, loc);
31675 tree c = build_omp_clause (loc, code);
31676 OMP_CLAUSE_OPERAND (c, 0) = t;
31677 OMP_CLAUSE_CHAIN (c) = list;
31678 return c;
31681 /* OpenACC:
31683 gang [( gang-arg-list )]
31684 worker [( [num:] int-expr )]
31685 vector [( [length:] int-expr )]
31687 where gang-arg is one of:
31689 [num:] int-expr
31690 static: size-expr
31692 and size-expr may be:
31695 int-expr
31698 static tree
31699 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31700 const char *str, tree list)
31702 const char *id = "num";
31703 cp_lexer *lexer = parser->lexer;
31704 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31705 location_t loc = cp_lexer_peek_token (lexer)->location;
31707 if (kind == OMP_CLAUSE_VECTOR)
31708 id = "length";
31710 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31712 matching_parens parens;
31713 parens.consume_open (parser);
31717 cp_token *next = cp_lexer_peek_token (lexer);
31718 int idx = 0;
31720 /* Gang static argument. */
31721 if (kind == OMP_CLAUSE_GANG
31722 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31724 cp_lexer_consume_token (lexer);
31726 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31727 goto cleanup_error;
31729 idx = 1;
31730 if (ops[idx] != NULL)
31732 cp_parser_error (parser, "too many %<static%> arguments");
31733 goto cleanup_error;
31736 /* Check for the '*' argument. */
31737 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31738 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31739 || cp_lexer_nth_token_is (parser->lexer, 2,
31740 CPP_CLOSE_PAREN)))
31742 cp_lexer_consume_token (lexer);
31743 ops[idx] = integer_minus_one_node;
31745 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31747 cp_lexer_consume_token (lexer);
31748 continue;
31750 else break;
31753 /* Worker num: argument and vector length: arguments. */
31754 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31755 && id_equal (next->u.value, id)
31756 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31758 cp_lexer_consume_token (lexer); /* id */
31759 cp_lexer_consume_token (lexer); /* ':' */
31762 /* Now collect the actual argument. */
31763 if (ops[idx] != NULL_TREE)
31765 cp_parser_error (parser, "unexpected argument");
31766 goto cleanup_error;
31769 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31770 false);
31771 if (expr == error_mark_node)
31772 goto cleanup_error;
31774 mark_exp_read (expr);
31775 ops[idx] = expr;
31777 if (kind == OMP_CLAUSE_GANG
31778 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31780 cp_lexer_consume_token (lexer);
31781 continue;
31783 break;
31785 while (1);
31787 if (!parens.require_close (parser))
31788 goto cleanup_error;
31791 check_no_duplicate_clause (list, kind, str, loc);
31793 c = build_omp_clause (loc, kind);
31795 if (ops[1])
31796 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31798 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31799 OMP_CLAUSE_CHAIN (c) = list;
31801 return c;
31803 cleanup_error:
31804 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31805 return list;
31808 /* OpenACC 2.0:
31809 tile ( size-expr-list ) */
31811 static tree
31812 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31814 tree c, expr = error_mark_node;
31815 tree tile = NULL_TREE;
31817 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31818 so, but the spec authors never considered such a case and have
31819 differing opinions on what it might mean, including 'not
31820 allowed'.) */
31821 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31822 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31823 clause_loc);
31825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31826 return list;
31830 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31831 return list;
31833 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31834 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31835 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31837 cp_lexer_consume_token (parser->lexer);
31838 expr = integer_zero_node;
31840 else
31841 expr = cp_parser_constant_expression (parser);
31843 tile = tree_cons (NULL_TREE, expr, tile);
31845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31847 /* Consume the trailing ')'. */
31848 cp_lexer_consume_token (parser->lexer);
31850 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31851 tile = nreverse (tile);
31852 OMP_CLAUSE_TILE_LIST (c) = tile;
31853 OMP_CLAUSE_CHAIN (c) = list;
31854 return c;
31857 /* OpenACC 2.0
31858 Parse wait clause or directive parameters. */
31860 static tree
31861 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31863 vec<tree, va_gc> *args;
31864 tree t, args_tree;
31866 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31867 /*cast_p=*/false,
31868 /*allow_expansion_p=*/true,
31869 /*non_constant_p=*/NULL);
31871 if (args == NULL || args->length () == 0)
31873 cp_parser_error (parser, "expected integer expression before ')'");
31874 if (args != NULL)
31875 release_tree_vector (args);
31876 return list;
31879 args_tree = build_tree_list_vec (args);
31881 release_tree_vector (args);
31883 for (t = args_tree; t; t = TREE_CHAIN (t))
31885 tree targ = TREE_VALUE (t);
31887 if (targ != error_mark_node)
31889 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31890 error ("%<wait%> expression must be integral");
31891 else
31893 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31895 targ = mark_rvalue_use (targ);
31896 OMP_CLAUSE_DECL (c) = targ;
31897 OMP_CLAUSE_CHAIN (c) = list;
31898 list = c;
31903 return list;
31906 /* OpenACC:
31907 wait ( int-expr-list ) */
31909 static tree
31910 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31912 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31914 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31915 return list;
31917 list = cp_parser_oacc_wait_list (parser, location, list);
31919 return list;
31922 /* OpenMP 3.0:
31923 collapse ( constant-expression ) */
31925 static tree
31926 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31928 tree c, num;
31929 location_t loc;
31930 HOST_WIDE_INT n;
31932 loc = cp_lexer_peek_token (parser->lexer)->location;
31933 matching_parens parens;
31934 if (!parens.require_open (parser))
31935 return list;
31937 num = cp_parser_constant_expression (parser);
31939 if (!parens.require_close (parser))
31940 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31941 /*or_comma=*/false,
31942 /*consume_paren=*/true);
31944 if (num == error_mark_node)
31945 return list;
31946 num = fold_non_dependent_expr (num);
31947 if (!tree_fits_shwi_p (num)
31948 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31949 || (n = tree_to_shwi (num)) <= 0
31950 || (int) n != n)
31952 error_at (loc, "collapse argument needs positive constant integer expression");
31953 return list;
31956 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31957 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31958 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31959 OMP_CLAUSE_CHAIN (c) = list;
31960 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31962 return c;
31965 /* OpenMP 2.5:
31966 default ( none | shared )
31968 OpenACC:
31969 default ( none | present ) */
31971 static tree
31972 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31973 location_t location, bool is_oacc)
31975 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31976 tree c;
31978 matching_parens parens;
31979 if (!parens.require_open (parser))
31980 return list;
31981 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31983 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31984 const char *p = IDENTIFIER_POINTER (id);
31986 switch (p[0])
31988 case 'n':
31989 if (strcmp ("none", p) != 0)
31990 goto invalid_kind;
31991 kind = OMP_CLAUSE_DEFAULT_NONE;
31992 break;
31994 case 'p':
31995 if (strcmp ("present", p) != 0 || !is_oacc)
31996 goto invalid_kind;
31997 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31998 break;
32000 case 's':
32001 if (strcmp ("shared", p) != 0 || is_oacc)
32002 goto invalid_kind;
32003 kind = OMP_CLAUSE_DEFAULT_SHARED;
32004 break;
32006 default:
32007 goto invalid_kind;
32010 cp_lexer_consume_token (parser->lexer);
32012 else
32014 invalid_kind:
32015 if (is_oacc)
32016 cp_parser_error (parser, "expected %<none%> or %<present%>");
32017 else
32018 cp_parser_error (parser, "expected %<none%> or %<shared%>");
32021 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
32022 || !parens.require_close (parser))
32023 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32024 /*or_comma=*/false,
32025 /*consume_paren=*/true);
32027 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
32028 return list;
32030 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
32031 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
32032 OMP_CLAUSE_CHAIN (c) = list;
32033 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
32035 return c;
32038 /* OpenMP 3.1:
32039 final ( expression ) */
32041 static tree
32042 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
32044 tree t, c;
32046 matching_parens parens;
32047 if (!parens.require_open (parser))
32048 return list;
32050 t = cp_parser_condition (parser);
32052 if (t == error_mark_node
32053 || !parens.require_close (parser))
32054 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32055 /*or_comma=*/false,
32056 /*consume_paren=*/true);
32058 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
32060 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
32061 OMP_CLAUSE_FINAL_EXPR (c) = t;
32062 OMP_CLAUSE_CHAIN (c) = list;
32064 return c;
32067 /* OpenMP 2.5:
32068 if ( expression )
32070 OpenMP 4.5:
32071 if ( directive-name-modifier : expression )
32073 directive-name-modifier:
32074 parallel | task | taskloop | target data | target | target update
32075 | target enter data | target exit data */
32077 static tree
32078 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
32079 bool is_omp)
32081 tree t, c;
32082 enum tree_code if_modifier = ERROR_MARK;
32084 matching_parens parens;
32085 if (!parens.require_open (parser))
32086 return list;
32088 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32090 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32091 const char *p = IDENTIFIER_POINTER (id);
32092 int n = 2;
32094 if (strcmp ("parallel", p) == 0)
32095 if_modifier = OMP_PARALLEL;
32096 else if (strcmp ("task", p) == 0)
32097 if_modifier = OMP_TASK;
32098 else if (strcmp ("taskloop", p) == 0)
32099 if_modifier = OMP_TASKLOOP;
32100 else if (strcmp ("target", p) == 0)
32102 if_modifier = OMP_TARGET;
32103 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
32105 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32106 p = IDENTIFIER_POINTER (id);
32107 if (strcmp ("data", p) == 0)
32108 if_modifier = OMP_TARGET_DATA;
32109 else if (strcmp ("update", p) == 0)
32110 if_modifier = OMP_TARGET_UPDATE;
32111 else if (strcmp ("enter", p) == 0)
32112 if_modifier = OMP_TARGET_ENTER_DATA;
32113 else if (strcmp ("exit", p) == 0)
32114 if_modifier = OMP_TARGET_EXIT_DATA;
32115 if (if_modifier != OMP_TARGET)
32116 n = 3;
32117 else
32119 location_t loc
32120 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32121 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32122 "or %<exit%>");
32123 if_modifier = ERROR_MARK;
32125 if (if_modifier == OMP_TARGET_ENTER_DATA
32126 || if_modifier == OMP_TARGET_EXIT_DATA)
32128 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32130 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32131 p = IDENTIFIER_POINTER (id);
32132 if (strcmp ("data", p) == 0)
32133 n = 4;
32135 if (n != 4)
32137 location_t loc
32138 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32139 error_at (loc, "expected %<data%>");
32140 if_modifier = ERROR_MARK;
32145 if (if_modifier != ERROR_MARK)
32147 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32149 while (n-- > 0)
32150 cp_lexer_consume_token (parser->lexer);
32152 else
32154 if (n > 2)
32156 location_t loc
32157 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32158 error_at (loc, "expected %<:%>");
32160 if_modifier = ERROR_MARK;
32165 t = cp_parser_condition (parser);
32167 if (t == error_mark_node
32168 || !parens.require_close (parser))
32169 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32170 /*or_comma=*/false,
32171 /*consume_paren=*/true);
32173 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32174 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32176 if (if_modifier != ERROR_MARK
32177 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32179 const char *p = NULL;
32180 switch (if_modifier)
32182 case OMP_PARALLEL: p = "parallel"; break;
32183 case OMP_TASK: p = "task"; break;
32184 case OMP_TASKLOOP: p = "taskloop"; break;
32185 case OMP_TARGET_DATA: p = "target data"; break;
32186 case OMP_TARGET: p = "target"; break;
32187 case OMP_TARGET_UPDATE: p = "target update"; break;
32188 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32189 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32190 default: gcc_unreachable ();
32192 error_at (location, "too many %<if%> clauses with %qs modifier",
32194 return list;
32196 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32198 if (!is_omp)
32199 error_at (location, "too many %<if%> clauses");
32200 else
32201 error_at (location, "too many %<if%> clauses without modifier");
32202 return list;
32204 else if (if_modifier == ERROR_MARK
32205 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32207 error_at (location, "if any %<if%> clause has modifier, then all "
32208 "%<if%> clauses have to use modifier");
32209 return list;
32213 c = build_omp_clause (location, OMP_CLAUSE_IF);
32214 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32215 OMP_CLAUSE_IF_EXPR (c) = t;
32216 OMP_CLAUSE_CHAIN (c) = list;
32218 return c;
32221 /* OpenMP 3.1:
32222 mergeable */
32224 static tree
32225 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32226 tree list, location_t location)
32228 tree c;
32230 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32231 location);
32233 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32234 OMP_CLAUSE_CHAIN (c) = list;
32235 return c;
32238 /* OpenMP 2.5:
32239 nowait */
32241 static tree
32242 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32243 tree list, location_t location)
32245 tree c;
32247 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32249 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32250 OMP_CLAUSE_CHAIN (c) = list;
32251 return c;
32254 /* OpenMP 2.5:
32255 num_threads ( expression ) */
32257 static tree
32258 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32259 location_t location)
32261 tree t, c;
32263 matching_parens parens;
32264 if (!parens.require_open (parser))
32265 return list;
32267 t = cp_parser_expression (parser);
32269 if (t == error_mark_node
32270 || !parens.require_close (parser))
32271 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32272 /*or_comma=*/false,
32273 /*consume_paren=*/true);
32275 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32276 "num_threads", location);
32278 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32279 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32280 OMP_CLAUSE_CHAIN (c) = list;
32282 return c;
32285 /* OpenMP 4.5:
32286 num_tasks ( expression ) */
32288 static tree
32289 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32290 location_t location)
32292 tree t, c;
32294 matching_parens parens;
32295 if (!parens.require_open (parser))
32296 return list;
32298 t = cp_parser_expression (parser);
32300 if (t == error_mark_node
32301 || !parens.require_close (parser))
32302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32303 /*or_comma=*/false,
32304 /*consume_paren=*/true);
32306 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32307 "num_tasks", location);
32309 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32310 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32311 OMP_CLAUSE_CHAIN (c) = list;
32313 return c;
32316 /* OpenMP 4.5:
32317 grainsize ( expression ) */
32319 static tree
32320 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32321 location_t location)
32323 tree t, c;
32325 matching_parens parens;
32326 if (!parens.require_open (parser))
32327 return list;
32329 t = cp_parser_expression (parser);
32331 if (t == error_mark_node
32332 || !parens.require_close (parser))
32333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32334 /*or_comma=*/false,
32335 /*consume_paren=*/true);
32337 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32338 "grainsize", location);
32340 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32341 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32342 OMP_CLAUSE_CHAIN (c) = list;
32344 return c;
32347 /* OpenMP 4.5:
32348 priority ( expression ) */
32350 static tree
32351 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32352 location_t location)
32354 tree t, c;
32356 matching_parens parens;
32357 if (!parens.require_open (parser))
32358 return list;
32360 t = cp_parser_expression (parser);
32362 if (t == error_mark_node
32363 || !parens.require_close (parser))
32364 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32365 /*or_comma=*/false,
32366 /*consume_paren=*/true);
32368 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32369 "priority", location);
32371 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32372 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32373 OMP_CLAUSE_CHAIN (c) = list;
32375 return c;
32378 /* OpenMP 4.5:
32379 hint ( expression ) */
32381 static tree
32382 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32383 location_t location)
32385 tree t, c;
32387 matching_parens parens;
32388 if (!parens.require_open (parser))
32389 return list;
32391 t = cp_parser_expression (parser);
32393 if (t == error_mark_node
32394 || !parens.require_close (parser))
32395 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32396 /*or_comma=*/false,
32397 /*consume_paren=*/true);
32399 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32401 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32402 OMP_CLAUSE_HINT_EXPR (c) = t;
32403 OMP_CLAUSE_CHAIN (c) = list;
32405 return c;
32408 /* OpenMP 4.5:
32409 defaultmap ( tofrom : scalar ) */
32411 static tree
32412 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32413 location_t location)
32415 tree c, id;
32416 const char *p;
32418 matching_parens parens;
32419 if (!parens.require_open (parser))
32420 return list;
32422 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32424 cp_parser_error (parser, "expected %<tofrom%>");
32425 goto out_err;
32427 id = cp_lexer_peek_token (parser->lexer)->u.value;
32428 p = IDENTIFIER_POINTER (id);
32429 if (strcmp (p, "tofrom") != 0)
32431 cp_parser_error (parser, "expected %<tofrom%>");
32432 goto out_err;
32434 cp_lexer_consume_token (parser->lexer);
32435 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32436 goto out_err;
32438 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32440 cp_parser_error (parser, "expected %<scalar%>");
32441 goto out_err;
32443 id = cp_lexer_peek_token (parser->lexer)->u.value;
32444 p = IDENTIFIER_POINTER (id);
32445 if (strcmp (p, "scalar") != 0)
32447 cp_parser_error (parser, "expected %<scalar%>");
32448 goto out_err;
32450 cp_lexer_consume_token (parser->lexer);
32451 if (!parens.require_close (parser))
32452 goto out_err;
32454 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32455 location);
32457 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32458 OMP_CLAUSE_CHAIN (c) = list;
32459 return c;
32461 out_err:
32462 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32463 /*or_comma=*/false,
32464 /*consume_paren=*/true);
32465 return list;
32468 /* OpenMP 2.5:
32469 ordered
32471 OpenMP 4.5:
32472 ordered ( constant-expression ) */
32474 static tree
32475 cp_parser_omp_clause_ordered (cp_parser *parser,
32476 tree list, location_t location)
32478 tree c, num = NULL_TREE;
32479 HOST_WIDE_INT n;
32481 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32482 "ordered", location);
32484 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32486 matching_parens parens;
32487 parens.consume_open (parser);
32489 num = cp_parser_constant_expression (parser);
32491 if (!parens.require_close (parser))
32492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32493 /*or_comma=*/false,
32494 /*consume_paren=*/true);
32496 if (num == error_mark_node)
32497 return list;
32498 num = fold_non_dependent_expr (num);
32499 if (!tree_fits_shwi_p (num)
32500 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32501 || (n = tree_to_shwi (num)) <= 0
32502 || (int) n != n)
32504 error_at (location,
32505 "ordered argument needs positive constant integer "
32506 "expression");
32507 return list;
32511 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32512 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32513 OMP_CLAUSE_CHAIN (c) = list;
32514 return c;
32517 /* OpenMP 2.5:
32518 reduction ( reduction-operator : variable-list )
32520 reduction-operator:
32521 One of: + * - & ^ | && ||
32523 OpenMP 3.1:
32525 reduction-operator:
32526 One of: + * - & ^ | && || min max
32528 OpenMP 4.0:
32530 reduction-operator:
32531 One of: + * - & ^ | && ||
32532 id-expression */
32534 static tree
32535 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32537 enum tree_code code = ERROR_MARK;
32538 tree nlist, c, id = NULL_TREE;
32540 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32541 return list;
32543 switch (cp_lexer_peek_token (parser->lexer)->type)
32545 case CPP_PLUS: code = PLUS_EXPR; break;
32546 case CPP_MULT: code = MULT_EXPR; break;
32547 case CPP_MINUS: code = MINUS_EXPR; break;
32548 case CPP_AND: code = BIT_AND_EXPR; break;
32549 case CPP_XOR: code = BIT_XOR_EXPR; break;
32550 case CPP_OR: code = BIT_IOR_EXPR; break;
32551 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32552 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32553 default: break;
32556 if (code != ERROR_MARK)
32557 cp_lexer_consume_token (parser->lexer);
32558 else
32560 bool saved_colon_corrects_to_scope_p;
32561 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32562 parser->colon_corrects_to_scope_p = false;
32563 id = cp_parser_id_expression (parser, /*template_p=*/false,
32564 /*check_dependency_p=*/true,
32565 /*template_p=*/NULL,
32566 /*declarator_p=*/false,
32567 /*optional_p=*/false);
32568 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32569 if (identifier_p (id))
32571 const char *p = IDENTIFIER_POINTER (id);
32573 if (strcmp (p, "min") == 0)
32574 code = MIN_EXPR;
32575 else if (strcmp (p, "max") == 0)
32576 code = MAX_EXPR;
32577 else if (id == cp_operator_id (PLUS_EXPR))
32578 code = PLUS_EXPR;
32579 else if (id == cp_operator_id (MULT_EXPR))
32580 code = MULT_EXPR;
32581 else if (id == cp_operator_id (MINUS_EXPR))
32582 code = MINUS_EXPR;
32583 else if (id == cp_operator_id (BIT_AND_EXPR))
32584 code = BIT_AND_EXPR;
32585 else if (id == cp_operator_id (BIT_IOR_EXPR))
32586 code = BIT_IOR_EXPR;
32587 else if (id == cp_operator_id (BIT_XOR_EXPR))
32588 code = BIT_XOR_EXPR;
32589 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32590 code = TRUTH_ANDIF_EXPR;
32591 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32592 code = TRUTH_ORIF_EXPR;
32593 id = omp_reduction_id (code, id, NULL_TREE);
32594 tree scope = parser->scope;
32595 if (scope)
32596 id = build_qualified_name (NULL_TREE, scope, id, false);
32597 parser->scope = NULL_TREE;
32598 parser->qualifying_scope = NULL_TREE;
32599 parser->object_scope = NULL_TREE;
32601 else
32603 error ("invalid reduction-identifier");
32604 resync_fail:
32605 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32606 /*or_comma=*/false,
32607 /*consume_paren=*/true);
32608 return list;
32612 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32613 goto resync_fail;
32615 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32616 NULL);
32617 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32619 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32620 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32623 return nlist;
32626 /* OpenMP 2.5:
32627 schedule ( schedule-kind )
32628 schedule ( schedule-kind , expression )
32630 schedule-kind:
32631 static | dynamic | guided | runtime | auto
32633 OpenMP 4.5:
32634 schedule ( schedule-modifier : schedule-kind )
32635 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32637 schedule-modifier:
32638 simd
32639 monotonic
32640 nonmonotonic */
32642 static tree
32643 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32645 tree c, t;
32646 int modifiers = 0, nmodifiers = 0;
32648 matching_parens parens;
32649 if (!parens.require_open (parser))
32650 return list;
32652 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32654 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32657 const char *p = IDENTIFIER_POINTER (id);
32658 if (strcmp ("simd", p) == 0)
32659 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32660 else if (strcmp ("monotonic", p) == 0)
32661 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32662 else if (strcmp ("nonmonotonic", p) == 0)
32663 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32664 else
32665 break;
32666 cp_lexer_consume_token (parser->lexer);
32667 if (nmodifiers++ == 0
32668 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32669 cp_lexer_consume_token (parser->lexer);
32670 else
32672 cp_parser_require (parser, CPP_COLON, RT_COLON);
32673 break;
32677 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32679 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32680 const char *p = IDENTIFIER_POINTER (id);
32682 switch (p[0])
32684 case 'd':
32685 if (strcmp ("dynamic", p) != 0)
32686 goto invalid_kind;
32687 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32688 break;
32690 case 'g':
32691 if (strcmp ("guided", p) != 0)
32692 goto invalid_kind;
32693 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32694 break;
32696 case 'r':
32697 if (strcmp ("runtime", p) != 0)
32698 goto invalid_kind;
32699 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32700 break;
32702 default:
32703 goto invalid_kind;
32706 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32707 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32708 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32709 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32710 else
32711 goto invalid_kind;
32712 cp_lexer_consume_token (parser->lexer);
32714 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32715 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32716 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32717 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32719 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32720 "specified");
32721 modifiers = 0;
32724 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32726 cp_token *token;
32727 cp_lexer_consume_token (parser->lexer);
32729 token = cp_lexer_peek_token (parser->lexer);
32730 t = cp_parser_assignment_expression (parser);
32732 if (t == error_mark_node)
32733 goto resync_fail;
32734 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32735 error_at (token->location, "schedule %<runtime%> does not take "
32736 "a %<chunk_size%> parameter");
32737 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32738 error_at (token->location, "schedule %<auto%> does not take "
32739 "a %<chunk_size%> parameter");
32740 else
32741 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32743 if (!parens.require_close (parser))
32744 goto resync_fail;
32746 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32747 goto resync_fail;
32749 OMP_CLAUSE_SCHEDULE_KIND (c)
32750 = (enum omp_clause_schedule_kind)
32751 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32753 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32754 OMP_CLAUSE_CHAIN (c) = list;
32755 return c;
32757 invalid_kind:
32758 cp_parser_error (parser, "invalid schedule kind");
32759 resync_fail:
32760 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32761 /*or_comma=*/false,
32762 /*consume_paren=*/true);
32763 return list;
32766 /* OpenMP 3.0:
32767 untied */
32769 static tree
32770 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32771 tree list, location_t location)
32773 tree c;
32775 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32777 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32778 OMP_CLAUSE_CHAIN (c) = list;
32779 return c;
32782 /* OpenMP 4.0:
32783 inbranch
32784 notinbranch */
32786 static tree
32787 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32788 tree list, location_t location)
32790 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32791 tree c = build_omp_clause (location, code);
32792 OMP_CLAUSE_CHAIN (c) = list;
32793 return c;
32796 /* OpenMP 4.0:
32797 parallel
32799 sections
32800 taskgroup */
32802 static tree
32803 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32804 enum omp_clause_code code,
32805 tree list, location_t location)
32807 tree c = build_omp_clause (location, code);
32808 OMP_CLAUSE_CHAIN (c) = list;
32809 return c;
32812 /* OpenMP 4.5:
32813 nogroup */
32815 static tree
32816 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32817 tree list, location_t location)
32819 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32820 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32821 OMP_CLAUSE_CHAIN (c) = list;
32822 return c;
32825 /* OpenMP 4.5:
32826 simd
32827 threads */
32829 static tree
32830 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32831 enum omp_clause_code code,
32832 tree list, location_t location)
32834 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32835 tree c = build_omp_clause (location, code);
32836 OMP_CLAUSE_CHAIN (c) = list;
32837 return c;
32840 /* OpenMP 4.0:
32841 num_teams ( expression ) */
32843 static tree
32844 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32845 location_t location)
32847 tree t, c;
32849 matching_parens parens;
32850 if (!parens.require_open (parser))
32851 return list;
32853 t = cp_parser_expression (parser);
32855 if (t == error_mark_node
32856 || !parens.require_close (parser))
32857 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32858 /*or_comma=*/false,
32859 /*consume_paren=*/true);
32861 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32862 "num_teams", location);
32864 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32865 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32866 OMP_CLAUSE_CHAIN (c) = list;
32868 return c;
32871 /* OpenMP 4.0:
32872 thread_limit ( expression ) */
32874 static tree
32875 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32876 location_t location)
32878 tree t, c;
32880 matching_parens parens;
32881 if (!parens.require_open (parser))
32882 return list;
32884 t = cp_parser_expression (parser);
32886 if (t == error_mark_node
32887 || !parens.require_close (parser))
32888 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32889 /*or_comma=*/false,
32890 /*consume_paren=*/true);
32892 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32893 "thread_limit", location);
32895 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32896 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32897 OMP_CLAUSE_CHAIN (c) = list;
32899 return c;
32902 /* OpenMP 4.0:
32903 aligned ( variable-list )
32904 aligned ( variable-list : constant-expression ) */
32906 static tree
32907 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32909 tree nlist, c, alignment = NULL_TREE;
32910 bool colon;
32912 matching_parens parens;
32913 if (!parens.require_open (parser))
32914 return list;
32916 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32917 &colon);
32919 if (colon)
32921 alignment = cp_parser_constant_expression (parser);
32923 if (!parens.require_close (parser))
32924 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32925 /*or_comma=*/false,
32926 /*consume_paren=*/true);
32928 if (alignment == error_mark_node)
32929 alignment = NULL_TREE;
32932 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32933 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32935 return nlist;
32938 /* OpenMP 4.0:
32939 linear ( variable-list )
32940 linear ( variable-list : expression )
32942 OpenMP 4.5:
32943 linear ( modifier ( variable-list ) )
32944 linear ( modifier ( variable-list ) : expression ) */
32946 static tree
32947 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32948 bool is_cilk_simd_fn, bool declare_simd)
32950 tree nlist, c, step = integer_one_node;
32951 bool colon;
32952 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32954 matching_parens parens;
32955 if (!parens.require_open (parser))
32956 return list;
32958 if (!is_cilk_simd_fn
32959 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32961 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32962 const char *p = IDENTIFIER_POINTER (id);
32964 if (strcmp ("ref", p) == 0)
32965 kind = OMP_CLAUSE_LINEAR_REF;
32966 else if (strcmp ("val", p) == 0)
32967 kind = OMP_CLAUSE_LINEAR_VAL;
32968 else if (strcmp ("uval", p) == 0)
32969 kind = OMP_CLAUSE_LINEAR_UVAL;
32970 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32971 cp_lexer_consume_token (parser->lexer);
32972 else
32973 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32976 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32977 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32978 &colon);
32979 else
32981 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32982 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32983 if (colon)
32984 cp_parser_require (parser, CPP_COLON, RT_COLON);
32985 else if (!parens.require_close (parser))
32986 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32987 /*or_comma=*/false,
32988 /*consume_paren=*/true);
32991 if (colon)
32993 step = NULL_TREE;
32994 if (declare_simd
32995 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32996 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32998 cp_token *token = cp_lexer_peek_token (parser->lexer);
32999 cp_parser_parse_tentatively (parser);
33000 step = cp_parser_id_expression (parser, /*template_p=*/false,
33001 /*check_dependency_p=*/true,
33002 /*template_p=*/NULL,
33003 /*declarator_p=*/false,
33004 /*optional_p=*/false);
33005 if (step != error_mark_node)
33006 step = cp_parser_lookup_name_simple (parser, step, token->location);
33007 if (step == error_mark_node)
33009 step = NULL_TREE;
33010 cp_parser_abort_tentative_parse (parser);
33012 else if (!cp_parser_parse_definitely (parser))
33013 step = NULL_TREE;
33015 if (!step)
33016 step = cp_parser_expression (parser);
33018 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
33020 sorry ("using parameters for %<linear%> step is not supported yet");
33021 step = integer_one_node;
33023 if (!parens.require_close (parser))
33024 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33025 /*or_comma=*/false,
33026 /*consume_paren=*/true);
33028 if (step == error_mark_node)
33029 return list;
33032 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33034 OMP_CLAUSE_LINEAR_STEP (c) = step;
33035 OMP_CLAUSE_LINEAR_KIND (c) = kind;
33038 return nlist;
33041 /* OpenMP 4.0:
33042 safelen ( constant-expression ) */
33044 static tree
33045 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
33046 location_t location)
33048 tree t, c;
33050 matching_parens parens;
33051 if (!parens.require_open (parser))
33052 return list;
33054 t = cp_parser_constant_expression (parser);
33056 if (t == error_mark_node
33057 || !parens.require_close (parser))
33058 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33059 /*or_comma=*/false,
33060 /*consume_paren=*/true);
33062 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
33064 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
33065 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
33066 OMP_CLAUSE_CHAIN (c) = list;
33068 return c;
33071 /* OpenMP 4.0:
33072 simdlen ( constant-expression ) */
33074 static tree
33075 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
33076 location_t location)
33078 tree t, c;
33080 matching_parens parens;
33081 if (!parens.require_open (parser))
33082 return list;
33084 t = cp_parser_constant_expression (parser);
33086 if (t == error_mark_node
33087 || !parens.require_close (parser))
33088 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33089 /*or_comma=*/false,
33090 /*consume_paren=*/true);
33092 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
33094 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
33095 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
33096 OMP_CLAUSE_CHAIN (c) = list;
33098 return c;
33101 /* OpenMP 4.5:
33102 vec:
33103 identifier [+/- integer]
33104 vec , identifier [+/- integer]
33107 static tree
33108 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33109 tree list)
33111 tree vec = NULL;
33113 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33115 cp_parser_error (parser, "expected identifier");
33116 return list;
33119 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33121 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33122 tree t, identifier = cp_parser_identifier (parser);
33123 tree addend = NULL;
33125 if (identifier == error_mark_node)
33126 t = error_mark_node;
33127 else
33129 t = cp_parser_lookup_name_simple
33130 (parser, identifier,
33131 cp_lexer_peek_token (parser->lexer)->location);
33132 if (t == error_mark_node)
33133 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33134 id_loc);
33137 bool neg = false;
33138 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33139 neg = true;
33140 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33142 addend = integer_zero_node;
33143 goto add_to_vector;
33145 cp_lexer_consume_token (parser->lexer);
33147 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33149 cp_parser_error (parser, "expected integer");
33150 return list;
33153 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33154 if (TREE_CODE (addend) != INTEGER_CST)
33156 cp_parser_error (parser, "expected integer");
33157 return list;
33159 cp_lexer_consume_token (parser->lexer);
33161 add_to_vector:
33162 if (t != error_mark_node)
33164 vec = tree_cons (addend, t, vec);
33165 if (neg)
33166 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33170 break;
33172 cp_lexer_consume_token (parser->lexer);
33175 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33177 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33178 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33179 OMP_CLAUSE_DECL (u) = nreverse (vec);
33180 OMP_CLAUSE_CHAIN (u) = list;
33181 return u;
33183 return list;
33186 /* OpenMP 4.0:
33187 depend ( depend-kind : variable-list )
33189 depend-kind:
33190 in | out | inout
33192 OpenMP 4.5:
33193 depend ( source )
33195 depend ( sink : vec ) */
33197 static tree
33198 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33200 tree nlist, c;
33201 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33203 matching_parens parens;
33204 if (!parens.require_open (parser))
33205 return list;
33207 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33209 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33210 const char *p = IDENTIFIER_POINTER (id);
33212 if (strcmp ("in", p) == 0)
33213 kind = OMP_CLAUSE_DEPEND_IN;
33214 else if (strcmp ("inout", p) == 0)
33215 kind = OMP_CLAUSE_DEPEND_INOUT;
33216 else if (strcmp ("out", p) == 0)
33217 kind = OMP_CLAUSE_DEPEND_OUT;
33218 else if (strcmp ("source", p) == 0)
33219 kind = OMP_CLAUSE_DEPEND_SOURCE;
33220 else if (strcmp ("sink", p) == 0)
33221 kind = OMP_CLAUSE_DEPEND_SINK;
33222 else
33223 goto invalid_kind;
33225 else
33226 goto invalid_kind;
33228 cp_lexer_consume_token (parser->lexer);
33230 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33232 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33233 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33234 OMP_CLAUSE_DECL (c) = NULL_TREE;
33235 OMP_CLAUSE_CHAIN (c) = list;
33236 if (!parens.require_close (parser))
33237 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33238 /*or_comma=*/false,
33239 /*consume_paren=*/true);
33240 return c;
33243 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33244 goto resync_fail;
33246 if (kind == OMP_CLAUSE_DEPEND_SINK)
33247 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33248 else
33250 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33251 list, NULL);
33253 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33254 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33256 return nlist;
33258 invalid_kind:
33259 cp_parser_error (parser, "invalid depend kind");
33260 resync_fail:
33261 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33262 /*or_comma=*/false,
33263 /*consume_paren=*/true);
33264 return list;
33267 /* OpenMP 4.0:
33268 map ( map-kind : variable-list )
33269 map ( variable-list )
33271 map-kind:
33272 alloc | to | from | tofrom
33274 OpenMP 4.5:
33275 map-kind:
33276 alloc | to | from | tofrom | release | delete
33278 map ( always [,] map-kind: variable-list ) */
33280 static tree
33281 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33283 tree nlist, c;
33284 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33285 bool always = false;
33287 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33288 return list;
33290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33293 const char *p = IDENTIFIER_POINTER (id);
33295 if (strcmp ("always", p) == 0)
33297 int nth = 2;
33298 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33299 nth++;
33300 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33301 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33302 == RID_DELETE))
33303 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33304 == CPP_COLON))
33306 always = true;
33307 cp_lexer_consume_token (parser->lexer);
33308 if (nth == 3)
33309 cp_lexer_consume_token (parser->lexer);
33314 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33315 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33318 const char *p = IDENTIFIER_POINTER (id);
33320 if (strcmp ("alloc", p) == 0)
33321 kind = GOMP_MAP_ALLOC;
33322 else if (strcmp ("to", p) == 0)
33323 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33324 else if (strcmp ("from", p) == 0)
33325 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33326 else if (strcmp ("tofrom", p) == 0)
33327 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33328 else if (strcmp ("release", p) == 0)
33329 kind = GOMP_MAP_RELEASE;
33330 else
33332 cp_parser_error (parser, "invalid map kind");
33333 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33334 /*or_comma=*/false,
33335 /*consume_paren=*/true);
33336 return list;
33338 cp_lexer_consume_token (parser->lexer);
33339 cp_lexer_consume_token (parser->lexer);
33341 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33342 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33344 kind = GOMP_MAP_DELETE;
33345 cp_lexer_consume_token (parser->lexer);
33346 cp_lexer_consume_token (parser->lexer);
33349 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33350 NULL);
33352 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33353 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33355 return nlist;
33358 /* OpenMP 4.0:
33359 device ( expression ) */
33361 static tree
33362 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33363 location_t location)
33365 tree t, c;
33367 matching_parens parens;
33368 if (!parens.require_open (parser))
33369 return list;
33371 t = cp_parser_expression (parser);
33373 if (t == error_mark_node
33374 || !parens.require_close (parser))
33375 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33376 /*or_comma=*/false,
33377 /*consume_paren=*/true);
33379 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33380 "device", location);
33382 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33383 OMP_CLAUSE_DEVICE_ID (c) = t;
33384 OMP_CLAUSE_CHAIN (c) = list;
33386 return c;
33389 /* OpenMP 4.0:
33390 dist_schedule ( static )
33391 dist_schedule ( static , expression ) */
33393 static tree
33394 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33395 location_t location)
33397 tree c, t;
33399 matching_parens parens;
33400 if (!parens.require_open (parser))
33401 return list;
33403 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33405 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33406 goto invalid_kind;
33407 cp_lexer_consume_token (parser->lexer);
33409 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33411 cp_lexer_consume_token (parser->lexer);
33413 t = cp_parser_assignment_expression (parser);
33415 if (t == error_mark_node)
33416 goto resync_fail;
33417 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33419 if (!parens.require_close (parser))
33420 goto resync_fail;
33422 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33423 goto resync_fail;
33425 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33426 location);
33427 OMP_CLAUSE_CHAIN (c) = list;
33428 return c;
33430 invalid_kind:
33431 cp_parser_error (parser, "invalid dist_schedule kind");
33432 resync_fail:
33433 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33434 /*or_comma=*/false,
33435 /*consume_paren=*/true);
33436 return list;
33439 /* OpenMP 4.0:
33440 proc_bind ( proc-bind-kind )
33442 proc-bind-kind:
33443 master | close | spread */
33445 static tree
33446 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33447 location_t location)
33449 tree c;
33450 enum omp_clause_proc_bind_kind kind;
33452 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33453 return list;
33455 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33457 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33458 const char *p = IDENTIFIER_POINTER (id);
33460 if (strcmp ("master", p) == 0)
33461 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33462 else if (strcmp ("close", p) == 0)
33463 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33464 else if (strcmp ("spread", p) == 0)
33465 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33466 else
33467 goto invalid_kind;
33469 else
33470 goto invalid_kind;
33472 cp_lexer_consume_token (parser->lexer);
33473 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33474 goto resync_fail;
33476 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33477 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33478 location);
33479 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33480 OMP_CLAUSE_CHAIN (c) = list;
33481 return c;
33483 invalid_kind:
33484 cp_parser_error (parser, "invalid depend kind");
33485 resync_fail:
33486 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33487 /*or_comma=*/false,
33488 /*consume_paren=*/true);
33489 return list;
33492 /* OpenACC:
33493 async [( int-expr )] */
33495 static tree
33496 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33498 tree c, t;
33499 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33501 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33503 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33505 matching_parens parens;
33506 parens.consume_open (parser);
33508 t = cp_parser_expression (parser);
33509 if (t == error_mark_node
33510 || !parens.require_close (parser))
33511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33512 /*or_comma=*/false,
33513 /*consume_paren=*/true);
33516 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33518 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33519 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33520 OMP_CLAUSE_CHAIN (c) = list;
33521 list = c;
33523 return list;
33526 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33527 is a bitmask in MASK. Return the list of clauses found. */
33529 static tree
33530 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33531 const char *where, cp_token *pragma_tok,
33532 bool finish_p = true)
33534 tree clauses = NULL;
33535 bool first = true;
33537 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33539 location_t here;
33540 pragma_omp_clause c_kind;
33541 omp_clause_code code;
33542 const char *c_name;
33543 tree prev = clauses;
33545 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33546 cp_lexer_consume_token (parser->lexer);
33548 here = cp_lexer_peek_token (parser->lexer)->location;
33549 c_kind = cp_parser_omp_clause_name (parser);
33551 switch (c_kind)
33553 case PRAGMA_OACC_CLAUSE_ASYNC:
33554 clauses = cp_parser_oacc_clause_async (parser, clauses);
33555 c_name = "async";
33556 break;
33557 case PRAGMA_OACC_CLAUSE_AUTO:
33558 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33559 clauses, here);
33560 c_name = "auto";
33561 break;
33562 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33563 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33564 c_name = "collapse";
33565 break;
33566 case PRAGMA_OACC_CLAUSE_COPY:
33567 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33568 c_name = "copy";
33569 break;
33570 case PRAGMA_OACC_CLAUSE_COPYIN:
33571 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33572 c_name = "copyin";
33573 break;
33574 case PRAGMA_OACC_CLAUSE_COPYOUT:
33575 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33576 c_name = "copyout";
33577 break;
33578 case PRAGMA_OACC_CLAUSE_CREATE:
33579 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33580 c_name = "create";
33581 break;
33582 case PRAGMA_OACC_CLAUSE_DELETE:
33583 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33584 c_name = "delete";
33585 break;
33586 case PRAGMA_OMP_CLAUSE_DEFAULT:
33587 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33588 c_name = "default";
33589 break;
33590 case PRAGMA_OACC_CLAUSE_DEVICE:
33591 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33592 c_name = "device";
33593 break;
33594 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33595 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33596 c_name = "deviceptr";
33597 break;
33598 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33599 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33600 c_name = "device_resident";
33601 break;
33602 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33603 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33604 clauses);
33605 c_name = "firstprivate";
33606 break;
33607 case PRAGMA_OACC_CLAUSE_GANG:
33608 c_name = "gang";
33609 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33610 c_name, clauses);
33611 break;
33612 case PRAGMA_OACC_CLAUSE_HOST:
33613 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33614 c_name = "host";
33615 break;
33616 case PRAGMA_OACC_CLAUSE_IF:
33617 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33618 c_name = "if";
33619 break;
33620 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33621 clauses = cp_parser_oacc_simple_clause (parser,
33622 OMP_CLAUSE_INDEPENDENT,
33623 clauses, here);
33624 c_name = "independent";
33625 break;
33626 case PRAGMA_OACC_CLAUSE_LINK:
33627 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33628 c_name = "link";
33629 break;
33630 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33631 code = OMP_CLAUSE_NUM_GANGS;
33632 c_name = "num_gangs";
33633 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33634 clauses);
33635 break;
33636 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33637 c_name = "num_workers";
33638 code = OMP_CLAUSE_NUM_WORKERS;
33639 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33640 clauses);
33641 break;
33642 case PRAGMA_OACC_CLAUSE_PRESENT:
33643 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33644 c_name = "present";
33645 break;
33646 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33647 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33648 c_name = "present_or_copy";
33649 break;
33650 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33651 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33652 c_name = "present_or_copyin";
33653 break;
33654 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33655 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33656 c_name = "present_or_copyout";
33657 break;
33658 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33659 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33660 c_name = "present_or_create";
33661 break;
33662 case PRAGMA_OACC_CLAUSE_PRIVATE:
33663 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33664 clauses);
33665 c_name = "private";
33666 break;
33667 case PRAGMA_OACC_CLAUSE_REDUCTION:
33668 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33669 c_name = "reduction";
33670 break;
33671 case PRAGMA_OACC_CLAUSE_SELF:
33672 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33673 c_name = "self";
33674 break;
33675 case PRAGMA_OACC_CLAUSE_SEQ:
33676 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33677 clauses, here);
33678 c_name = "seq";
33679 break;
33680 case PRAGMA_OACC_CLAUSE_TILE:
33681 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33682 c_name = "tile";
33683 break;
33684 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33685 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33686 clauses);
33687 c_name = "use_device";
33688 break;
33689 case PRAGMA_OACC_CLAUSE_VECTOR:
33690 c_name = "vector";
33691 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33692 c_name, clauses);
33693 break;
33694 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33695 c_name = "vector_length";
33696 code = OMP_CLAUSE_VECTOR_LENGTH;
33697 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33698 clauses);
33699 break;
33700 case PRAGMA_OACC_CLAUSE_WAIT:
33701 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33702 c_name = "wait";
33703 break;
33704 case PRAGMA_OACC_CLAUSE_WORKER:
33705 c_name = "worker";
33706 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33707 c_name, clauses);
33708 break;
33709 default:
33710 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33711 goto saw_error;
33714 first = false;
33716 if (((mask >> c_kind) & 1) == 0)
33718 /* Remove the invalid clause(s) from the list to avoid
33719 confusing the rest of the compiler. */
33720 clauses = prev;
33721 error_at (here, "%qs is not valid for %qs", c_name, where);
33725 saw_error:
33726 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33728 if (finish_p)
33729 return finish_omp_clauses (clauses, C_ORT_ACC);
33731 return clauses;
33734 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33735 is a bitmask in MASK. Return the list of clauses found; the result
33736 of clause default goes in *pdefault. */
33738 static tree
33739 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33740 const char *where, cp_token *pragma_tok,
33741 bool finish_p = true)
33743 tree clauses = NULL;
33744 bool first = true;
33745 cp_token *token = NULL;
33747 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33749 pragma_omp_clause c_kind;
33750 const char *c_name;
33751 tree prev = clauses;
33753 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33754 cp_lexer_consume_token (parser->lexer);
33756 token = cp_lexer_peek_token (parser->lexer);
33757 c_kind = cp_parser_omp_clause_name (parser);
33759 switch (c_kind)
33761 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33762 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33763 token->location);
33764 c_name = "collapse";
33765 break;
33766 case PRAGMA_OMP_CLAUSE_COPYIN:
33767 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33768 c_name = "copyin";
33769 break;
33770 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33771 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33772 clauses);
33773 c_name = "copyprivate";
33774 break;
33775 case PRAGMA_OMP_CLAUSE_DEFAULT:
33776 clauses = cp_parser_omp_clause_default (parser, clauses,
33777 token->location, false);
33778 c_name = "default";
33779 break;
33780 case PRAGMA_OMP_CLAUSE_FINAL:
33781 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33782 c_name = "final";
33783 break;
33784 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33785 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33786 clauses);
33787 c_name = "firstprivate";
33788 break;
33789 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33790 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33791 token->location);
33792 c_name = "grainsize";
33793 break;
33794 case PRAGMA_OMP_CLAUSE_HINT:
33795 clauses = cp_parser_omp_clause_hint (parser, clauses,
33796 token->location);
33797 c_name = "hint";
33798 break;
33799 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33800 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33801 token->location);
33802 c_name = "defaultmap";
33803 break;
33804 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33805 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33806 clauses);
33807 c_name = "use_device_ptr";
33808 break;
33809 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33810 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33811 clauses);
33812 c_name = "is_device_ptr";
33813 break;
33814 case PRAGMA_OMP_CLAUSE_IF:
33815 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33816 true);
33817 c_name = "if";
33818 break;
33819 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33820 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33821 clauses);
33822 c_name = "lastprivate";
33823 break;
33824 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33825 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33826 token->location);
33827 c_name = "mergeable";
33828 break;
33829 case PRAGMA_OMP_CLAUSE_NOWAIT:
33830 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33831 c_name = "nowait";
33832 break;
33833 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33834 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33835 token->location);
33836 c_name = "num_tasks";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33839 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33840 token->location);
33841 c_name = "num_threads";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_ORDERED:
33844 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33845 token->location);
33846 c_name = "ordered";
33847 break;
33848 case PRAGMA_OMP_CLAUSE_PRIORITY:
33849 clauses = cp_parser_omp_clause_priority (parser, clauses,
33850 token->location);
33851 c_name = "priority";
33852 break;
33853 case PRAGMA_OMP_CLAUSE_PRIVATE:
33854 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33855 clauses);
33856 c_name = "private";
33857 break;
33858 case PRAGMA_OMP_CLAUSE_REDUCTION:
33859 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33860 c_name = "reduction";
33861 break;
33862 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33863 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33864 token->location);
33865 c_name = "schedule";
33866 break;
33867 case PRAGMA_OMP_CLAUSE_SHARED:
33868 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33869 clauses);
33870 c_name = "shared";
33871 break;
33872 case PRAGMA_OMP_CLAUSE_UNTIED:
33873 clauses = cp_parser_omp_clause_untied (parser, clauses,
33874 token->location);
33875 c_name = "untied";
33876 break;
33877 case PRAGMA_OMP_CLAUSE_INBRANCH:
33878 case PRAGMA_CILK_CLAUSE_MASK:
33879 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33880 clauses, token->location);
33881 c_name = "inbranch";
33882 break;
33883 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33884 case PRAGMA_CILK_CLAUSE_NOMASK:
33885 clauses = cp_parser_omp_clause_branch (parser,
33886 OMP_CLAUSE_NOTINBRANCH,
33887 clauses, token->location);
33888 c_name = "notinbranch";
33889 break;
33890 case PRAGMA_OMP_CLAUSE_PARALLEL:
33891 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33892 clauses, token->location);
33893 c_name = "parallel";
33894 if (!first)
33896 clause_not_first:
33897 error_at (token->location, "%qs must be the first clause of %qs",
33898 c_name, where);
33899 clauses = prev;
33901 break;
33902 case PRAGMA_OMP_CLAUSE_FOR:
33903 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33904 clauses, token->location);
33905 c_name = "for";
33906 if (!first)
33907 goto clause_not_first;
33908 break;
33909 case PRAGMA_OMP_CLAUSE_SECTIONS:
33910 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33911 clauses, token->location);
33912 c_name = "sections";
33913 if (!first)
33914 goto clause_not_first;
33915 break;
33916 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33917 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33918 clauses, token->location);
33919 c_name = "taskgroup";
33920 if (!first)
33921 goto clause_not_first;
33922 break;
33923 case PRAGMA_OMP_CLAUSE_LINK:
33924 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33925 c_name = "to";
33926 break;
33927 case PRAGMA_OMP_CLAUSE_TO:
33928 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33929 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33930 clauses);
33931 else
33932 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33933 c_name = "to";
33934 break;
33935 case PRAGMA_OMP_CLAUSE_FROM:
33936 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33937 c_name = "from";
33938 break;
33939 case PRAGMA_OMP_CLAUSE_UNIFORM:
33940 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33941 clauses);
33942 c_name = "uniform";
33943 break;
33944 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33945 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33946 token->location);
33947 c_name = "num_teams";
33948 break;
33949 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33950 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33951 token->location);
33952 c_name = "thread_limit";
33953 break;
33954 case PRAGMA_OMP_CLAUSE_ALIGNED:
33955 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33956 c_name = "aligned";
33957 break;
33958 case PRAGMA_OMP_CLAUSE_LINEAR:
33960 bool cilk_simd_fn = false, declare_simd = false;
33961 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33962 cilk_simd_fn = true;
33963 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33964 declare_simd = true;
33965 clauses = cp_parser_omp_clause_linear (parser, clauses,
33966 cilk_simd_fn, declare_simd);
33968 c_name = "linear";
33969 break;
33970 case PRAGMA_OMP_CLAUSE_DEPEND:
33971 clauses = cp_parser_omp_clause_depend (parser, clauses,
33972 token->location);
33973 c_name = "depend";
33974 break;
33975 case PRAGMA_OMP_CLAUSE_MAP:
33976 clauses = cp_parser_omp_clause_map (parser, clauses);
33977 c_name = "map";
33978 break;
33979 case PRAGMA_OMP_CLAUSE_DEVICE:
33980 clauses = cp_parser_omp_clause_device (parser, clauses,
33981 token->location);
33982 c_name = "device";
33983 break;
33984 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33985 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33986 token->location);
33987 c_name = "dist_schedule";
33988 break;
33989 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33990 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33991 token->location);
33992 c_name = "proc_bind";
33993 break;
33994 case PRAGMA_OMP_CLAUSE_SAFELEN:
33995 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33996 token->location);
33997 c_name = "safelen";
33998 break;
33999 case PRAGMA_OMP_CLAUSE_SIMDLEN:
34000 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
34001 token->location);
34002 c_name = "simdlen";
34003 break;
34004 case PRAGMA_OMP_CLAUSE_NOGROUP:
34005 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
34006 token->location);
34007 c_name = "nogroup";
34008 break;
34009 case PRAGMA_OMP_CLAUSE_THREADS:
34010 clauses
34011 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
34012 clauses, token->location);
34013 c_name = "threads";
34014 break;
34015 case PRAGMA_OMP_CLAUSE_SIMD:
34016 clauses
34017 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
34018 clauses, token->location);
34019 c_name = "simd";
34020 break;
34021 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
34022 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
34023 c_name = "simdlen";
34024 break;
34025 default:
34026 cp_parser_error (parser, "expected %<#pragma omp%> clause");
34027 goto saw_error;
34030 first = false;
34032 if (((mask >> c_kind) & 1) == 0)
34034 /* Remove the invalid clause(s) from the list to avoid
34035 confusing the rest of the compiler. */
34036 clauses = prev;
34037 error_at (token->location, "%qs is not valid for %qs", c_name, where);
34040 saw_error:
34041 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
34042 no reason to skip to the end. */
34043 if (!(flag_cilkplus && pragma_tok == NULL))
34044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34045 if (finish_p)
34047 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
34048 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
34049 else
34050 return finish_omp_clauses (clauses, C_ORT_OMP);
34052 return clauses;
34055 /* OpenMP 2.5:
34056 structured-block:
34057 statement
34059 In practice, we're also interested in adding the statement to an
34060 outer node. So it is convenient if we work around the fact that
34061 cp_parser_statement calls add_stmt. */
34063 static unsigned
34064 cp_parser_begin_omp_structured_block (cp_parser *parser)
34066 unsigned save = parser->in_statement;
34068 /* Only move the values to IN_OMP_BLOCK if they weren't false.
34069 This preserves the "not within loop or switch" style error messages
34070 for nonsense cases like
34071 void foo() {
34072 #pragma omp single
34073 break;
34076 if (parser->in_statement)
34077 parser->in_statement = IN_OMP_BLOCK;
34079 return save;
34082 static void
34083 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
34085 parser->in_statement = save;
34088 static tree
34089 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
34091 tree stmt = begin_omp_structured_block ();
34092 unsigned int save = cp_parser_begin_omp_structured_block (parser);
34094 cp_parser_statement (parser, NULL_TREE, false, if_p);
34096 cp_parser_end_omp_structured_block (parser, save);
34097 return finish_omp_structured_block (stmt);
34100 /* OpenMP 2.5:
34101 # pragma omp atomic new-line
34102 expression-stmt
34104 expression-stmt:
34105 x binop= expr | x++ | ++x | x-- | --x
34106 binop:
34107 +, *, -, /, &, ^, |, <<, >>
34109 where x is an lvalue expression with scalar type.
34111 OpenMP 3.1:
34112 # pragma omp atomic new-line
34113 update-stmt
34115 # pragma omp atomic read new-line
34116 read-stmt
34118 # pragma omp atomic write new-line
34119 write-stmt
34121 # pragma omp atomic update new-line
34122 update-stmt
34124 # pragma omp atomic capture new-line
34125 capture-stmt
34127 # pragma omp atomic capture new-line
34128 capture-block
34130 read-stmt:
34131 v = x
34132 write-stmt:
34133 x = expr
34134 update-stmt:
34135 expression-stmt | x = x binop expr
34136 capture-stmt:
34137 v = expression-stmt
34138 capture-block:
34139 { v = x; update-stmt; } | { update-stmt; v = x; }
34141 OpenMP 4.0:
34142 update-stmt:
34143 expression-stmt | x = x binop expr | x = expr binop x
34144 capture-stmt:
34145 v = update-stmt
34146 capture-block:
34147 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34149 where x and v are lvalue expressions with scalar type. */
34151 static void
34152 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34154 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34155 tree rhs1 = NULL_TREE, orig_lhs;
34156 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34157 bool structured_block = false;
34158 bool seq_cst = false;
34160 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34162 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34163 const char *p = IDENTIFIER_POINTER (id);
34165 if (!strcmp (p, "seq_cst"))
34167 seq_cst = true;
34168 cp_lexer_consume_token (parser->lexer);
34169 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34170 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34171 cp_lexer_consume_token (parser->lexer);
34174 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34176 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34177 const char *p = IDENTIFIER_POINTER (id);
34179 if (!strcmp (p, "read"))
34180 code = OMP_ATOMIC_READ;
34181 else if (!strcmp (p, "write"))
34182 code = NOP_EXPR;
34183 else if (!strcmp (p, "update"))
34184 code = OMP_ATOMIC;
34185 else if (!strcmp (p, "capture"))
34186 code = OMP_ATOMIC_CAPTURE_NEW;
34187 else
34188 p = NULL;
34189 if (p)
34190 cp_lexer_consume_token (parser->lexer);
34192 if (!seq_cst)
34194 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34195 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34196 cp_lexer_consume_token (parser->lexer);
34198 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34200 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34201 const char *p = IDENTIFIER_POINTER (id);
34203 if (!strcmp (p, "seq_cst"))
34205 seq_cst = true;
34206 cp_lexer_consume_token (parser->lexer);
34210 cp_parser_require_pragma_eol (parser, pragma_tok);
34212 switch (code)
34214 case OMP_ATOMIC_READ:
34215 case NOP_EXPR: /* atomic write */
34216 v = cp_parser_unary_expression (parser);
34217 if (v == error_mark_node)
34218 goto saw_error;
34219 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34220 goto saw_error;
34221 if (code == NOP_EXPR)
34222 lhs = cp_parser_expression (parser);
34223 else
34224 lhs = cp_parser_unary_expression (parser);
34225 if (lhs == error_mark_node)
34226 goto saw_error;
34227 if (code == NOP_EXPR)
34229 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34230 opcode. */
34231 code = OMP_ATOMIC;
34232 rhs = lhs;
34233 lhs = v;
34234 v = NULL_TREE;
34236 goto done;
34237 case OMP_ATOMIC_CAPTURE_NEW:
34238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34240 cp_lexer_consume_token (parser->lexer);
34241 structured_block = true;
34243 else
34245 v = cp_parser_unary_expression (parser);
34246 if (v == error_mark_node)
34247 goto saw_error;
34248 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34249 goto saw_error;
34251 default:
34252 break;
34255 restart:
34256 lhs = cp_parser_unary_expression (parser);
34257 orig_lhs = lhs;
34258 switch (TREE_CODE (lhs))
34260 case ERROR_MARK:
34261 goto saw_error;
34263 case POSTINCREMENT_EXPR:
34264 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34265 code = OMP_ATOMIC_CAPTURE_OLD;
34266 /* FALLTHROUGH */
34267 case PREINCREMENT_EXPR:
34268 lhs = TREE_OPERAND (lhs, 0);
34269 opcode = PLUS_EXPR;
34270 rhs = integer_one_node;
34271 break;
34273 case POSTDECREMENT_EXPR:
34274 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34275 code = OMP_ATOMIC_CAPTURE_OLD;
34276 /* FALLTHROUGH */
34277 case PREDECREMENT_EXPR:
34278 lhs = TREE_OPERAND (lhs, 0);
34279 opcode = MINUS_EXPR;
34280 rhs = integer_one_node;
34281 break;
34283 case COMPOUND_EXPR:
34284 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34285 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34286 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34287 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34289 (TREE_OPERAND (lhs, 1), 0), 0)))
34290 == BOOLEAN_TYPE)
34291 /* Undo effects of boolean_increment for post {in,de}crement. */
34292 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34293 /* FALLTHRU */
34294 case MODIFY_EXPR:
34295 if (TREE_CODE (lhs) == MODIFY_EXPR
34296 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34298 /* Undo effects of boolean_increment. */
34299 if (integer_onep (TREE_OPERAND (lhs, 1)))
34301 /* This is pre or post increment. */
34302 rhs = TREE_OPERAND (lhs, 1);
34303 lhs = TREE_OPERAND (lhs, 0);
34304 opcode = NOP_EXPR;
34305 if (code == OMP_ATOMIC_CAPTURE_NEW
34306 && !structured_block
34307 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34308 code = OMP_ATOMIC_CAPTURE_OLD;
34309 break;
34312 /* FALLTHRU */
34313 default:
34314 switch (cp_lexer_peek_token (parser->lexer)->type)
34316 case CPP_MULT_EQ:
34317 opcode = MULT_EXPR;
34318 break;
34319 case CPP_DIV_EQ:
34320 opcode = TRUNC_DIV_EXPR;
34321 break;
34322 case CPP_PLUS_EQ:
34323 opcode = PLUS_EXPR;
34324 break;
34325 case CPP_MINUS_EQ:
34326 opcode = MINUS_EXPR;
34327 break;
34328 case CPP_LSHIFT_EQ:
34329 opcode = LSHIFT_EXPR;
34330 break;
34331 case CPP_RSHIFT_EQ:
34332 opcode = RSHIFT_EXPR;
34333 break;
34334 case CPP_AND_EQ:
34335 opcode = BIT_AND_EXPR;
34336 break;
34337 case CPP_OR_EQ:
34338 opcode = BIT_IOR_EXPR;
34339 break;
34340 case CPP_XOR_EQ:
34341 opcode = BIT_XOR_EXPR;
34342 break;
34343 case CPP_EQ:
34344 enum cp_parser_prec oprec;
34345 cp_token *token;
34346 cp_lexer_consume_token (parser->lexer);
34347 cp_parser_parse_tentatively (parser);
34348 rhs1 = cp_parser_simple_cast_expression (parser);
34349 if (rhs1 == error_mark_node)
34351 cp_parser_abort_tentative_parse (parser);
34352 cp_parser_simple_cast_expression (parser);
34353 goto saw_error;
34355 token = cp_lexer_peek_token (parser->lexer);
34356 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34358 cp_parser_abort_tentative_parse (parser);
34359 cp_parser_parse_tentatively (parser);
34360 rhs = cp_parser_binary_expression (parser, false, true,
34361 PREC_NOT_OPERATOR, NULL);
34362 if (rhs == error_mark_node)
34364 cp_parser_abort_tentative_parse (parser);
34365 cp_parser_binary_expression (parser, false, true,
34366 PREC_NOT_OPERATOR, NULL);
34367 goto saw_error;
34369 switch (TREE_CODE (rhs))
34371 case MULT_EXPR:
34372 case TRUNC_DIV_EXPR:
34373 case RDIV_EXPR:
34374 case PLUS_EXPR:
34375 case MINUS_EXPR:
34376 case LSHIFT_EXPR:
34377 case RSHIFT_EXPR:
34378 case BIT_AND_EXPR:
34379 case BIT_IOR_EXPR:
34380 case BIT_XOR_EXPR:
34381 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34383 if (cp_parser_parse_definitely (parser))
34385 opcode = TREE_CODE (rhs);
34386 rhs1 = TREE_OPERAND (rhs, 0);
34387 rhs = TREE_OPERAND (rhs, 1);
34388 goto stmt_done;
34390 else
34391 goto saw_error;
34393 break;
34394 default:
34395 break;
34397 cp_parser_abort_tentative_parse (parser);
34398 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34400 rhs = cp_parser_expression (parser);
34401 if (rhs == error_mark_node)
34402 goto saw_error;
34403 opcode = NOP_EXPR;
34404 rhs1 = NULL_TREE;
34405 goto stmt_done;
34407 cp_parser_error (parser,
34408 "invalid form of %<#pragma omp atomic%>");
34409 goto saw_error;
34411 if (!cp_parser_parse_definitely (parser))
34412 goto saw_error;
34413 switch (token->type)
34415 case CPP_SEMICOLON:
34416 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34418 code = OMP_ATOMIC_CAPTURE_OLD;
34419 v = lhs;
34420 lhs = NULL_TREE;
34421 lhs1 = rhs1;
34422 rhs1 = NULL_TREE;
34423 cp_lexer_consume_token (parser->lexer);
34424 goto restart;
34426 else if (structured_block)
34428 opcode = NOP_EXPR;
34429 rhs = rhs1;
34430 rhs1 = NULL_TREE;
34431 goto stmt_done;
34433 cp_parser_error (parser,
34434 "invalid form of %<#pragma omp atomic%>");
34435 goto saw_error;
34436 case CPP_MULT:
34437 opcode = MULT_EXPR;
34438 break;
34439 case CPP_DIV:
34440 opcode = TRUNC_DIV_EXPR;
34441 break;
34442 case CPP_PLUS:
34443 opcode = PLUS_EXPR;
34444 break;
34445 case CPP_MINUS:
34446 opcode = MINUS_EXPR;
34447 break;
34448 case CPP_LSHIFT:
34449 opcode = LSHIFT_EXPR;
34450 break;
34451 case CPP_RSHIFT:
34452 opcode = RSHIFT_EXPR;
34453 break;
34454 case CPP_AND:
34455 opcode = BIT_AND_EXPR;
34456 break;
34457 case CPP_OR:
34458 opcode = BIT_IOR_EXPR;
34459 break;
34460 case CPP_XOR:
34461 opcode = BIT_XOR_EXPR;
34462 break;
34463 default:
34464 cp_parser_error (parser,
34465 "invalid operator for %<#pragma omp atomic%>");
34466 goto saw_error;
34468 oprec = TOKEN_PRECEDENCE (token);
34469 gcc_assert (oprec != PREC_NOT_OPERATOR);
34470 if (commutative_tree_code (opcode))
34471 oprec = (enum cp_parser_prec) (oprec - 1);
34472 cp_lexer_consume_token (parser->lexer);
34473 rhs = cp_parser_binary_expression (parser, false, false,
34474 oprec, NULL);
34475 if (rhs == error_mark_node)
34476 goto saw_error;
34477 goto stmt_done;
34478 /* FALLTHROUGH */
34479 default:
34480 cp_parser_error (parser,
34481 "invalid operator for %<#pragma omp atomic%>");
34482 goto saw_error;
34484 cp_lexer_consume_token (parser->lexer);
34486 rhs = cp_parser_expression (parser);
34487 if (rhs == error_mark_node)
34488 goto saw_error;
34489 break;
34491 stmt_done:
34492 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34494 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34495 goto saw_error;
34496 v = cp_parser_unary_expression (parser);
34497 if (v == error_mark_node)
34498 goto saw_error;
34499 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34500 goto saw_error;
34501 lhs1 = cp_parser_unary_expression (parser);
34502 if (lhs1 == error_mark_node)
34503 goto saw_error;
34505 if (structured_block)
34507 cp_parser_consume_semicolon_at_end_of_statement (parser);
34508 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34510 done:
34511 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34512 if (!structured_block)
34513 cp_parser_consume_semicolon_at_end_of_statement (parser);
34514 return;
34516 saw_error:
34517 cp_parser_skip_to_end_of_block_or_statement (parser);
34518 if (structured_block)
34520 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34521 cp_lexer_consume_token (parser->lexer);
34522 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34524 cp_parser_skip_to_end_of_block_or_statement (parser);
34525 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34526 cp_lexer_consume_token (parser->lexer);
34532 /* OpenMP 2.5:
34533 # pragma omp barrier new-line */
34535 static void
34536 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34538 cp_parser_require_pragma_eol (parser, pragma_tok);
34539 finish_omp_barrier ();
34542 /* OpenMP 2.5:
34543 # pragma omp critical [(name)] new-line
34544 structured-block
34546 OpenMP 4.5:
34547 # pragma omp critical [(name) [hint(expression)]] new-line
34548 structured-block */
34550 #define OMP_CRITICAL_CLAUSE_MASK \
34551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34553 static tree
34554 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34556 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34558 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34560 matching_parens parens;
34561 parens.consume_open (parser);
34563 name = cp_parser_identifier (parser);
34565 if (name == error_mark_node
34566 || !parens.require_close (parser))
34567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34568 /*or_comma=*/false,
34569 /*consume_paren=*/true);
34570 if (name == error_mark_node)
34571 name = NULL;
34573 clauses = cp_parser_omp_all_clauses (parser,
34574 OMP_CRITICAL_CLAUSE_MASK,
34575 "#pragma omp critical", pragma_tok);
34577 else
34578 cp_parser_require_pragma_eol (parser, pragma_tok);
34580 stmt = cp_parser_omp_structured_block (parser, if_p);
34581 return c_finish_omp_critical (input_location, stmt, name, clauses);
34584 /* OpenMP 2.5:
34585 # pragma omp flush flush-vars[opt] new-line
34587 flush-vars:
34588 ( variable-list ) */
34590 static void
34591 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34594 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34595 cp_parser_require_pragma_eol (parser, pragma_tok);
34597 finish_omp_flush ();
34600 /* Helper function, to parse omp for increment expression. */
34602 static tree
34603 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34605 tree cond = cp_parser_binary_expression (parser, false, true,
34606 PREC_NOT_OPERATOR, NULL);
34607 if (cond == error_mark_node
34608 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34610 cp_parser_skip_to_end_of_statement (parser);
34611 return error_mark_node;
34614 switch (TREE_CODE (cond))
34616 case GT_EXPR:
34617 case GE_EXPR:
34618 case LT_EXPR:
34619 case LE_EXPR:
34620 break;
34621 case NE_EXPR:
34622 if (code == CILK_SIMD || code == CILK_FOR)
34623 break;
34624 /* Fall through: OpenMP disallows NE_EXPR. */
34625 gcc_fallthrough ();
34626 default:
34627 return error_mark_node;
34630 /* If decl is an iterator, preserve LHS and RHS of the relational
34631 expr until finish_omp_for. */
34632 if (decl
34633 && (type_dependent_expression_p (decl)
34634 || CLASS_TYPE_P (TREE_TYPE (decl))))
34635 return cond;
34637 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34638 TREE_CODE (cond),
34639 TREE_OPERAND (cond, 0), ERROR_MARK,
34640 TREE_OPERAND (cond, 1), ERROR_MARK,
34641 /*overload=*/NULL, tf_warning_or_error);
34644 /* Helper function, to parse omp for increment expression. */
34646 static tree
34647 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34649 cp_token *token = cp_lexer_peek_token (parser->lexer);
34650 enum tree_code op;
34651 tree lhs, rhs;
34652 cp_id_kind idk;
34653 bool decl_first;
34655 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34657 op = (token->type == CPP_PLUS_PLUS
34658 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34659 cp_lexer_consume_token (parser->lexer);
34660 lhs = cp_parser_simple_cast_expression (parser);
34661 if (lhs != decl
34662 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34663 return error_mark_node;
34664 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34667 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34668 if (lhs != decl
34669 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34670 return error_mark_node;
34672 token = cp_lexer_peek_token (parser->lexer);
34673 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34675 op = (token->type == CPP_PLUS_PLUS
34676 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34677 cp_lexer_consume_token (parser->lexer);
34678 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34681 op = cp_parser_assignment_operator_opt (parser);
34682 if (op == ERROR_MARK)
34683 return error_mark_node;
34685 if (op != NOP_EXPR)
34687 rhs = cp_parser_assignment_expression (parser);
34688 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34689 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34692 lhs = cp_parser_binary_expression (parser, false, false,
34693 PREC_ADDITIVE_EXPRESSION, NULL);
34694 token = cp_lexer_peek_token (parser->lexer);
34695 decl_first = (lhs == decl
34696 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34697 if (decl_first)
34698 lhs = NULL_TREE;
34699 if (token->type != CPP_PLUS
34700 && token->type != CPP_MINUS)
34701 return error_mark_node;
34705 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34706 cp_lexer_consume_token (parser->lexer);
34707 rhs = cp_parser_binary_expression (parser, false, false,
34708 PREC_ADDITIVE_EXPRESSION, NULL);
34709 token = cp_lexer_peek_token (parser->lexer);
34710 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34712 if (lhs == NULL_TREE)
34714 if (op == PLUS_EXPR)
34715 lhs = rhs;
34716 else
34717 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34718 tf_warning_or_error);
34720 else
34721 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34722 ERROR_MARK, NULL, tf_warning_or_error);
34725 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34727 if (!decl_first)
34729 if ((rhs != decl
34730 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34731 || op == MINUS_EXPR)
34732 return error_mark_node;
34733 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34735 else
34736 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34738 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34741 /* Parse the initialization statement of either an OpenMP for loop or
34742 a Cilk Plus for loop.
34744 Return true if the resulting construct should have an
34745 OMP_CLAUSE_PRIVATE added to it. */
34747 static tree
34748 cp_parser_omp_for_loop_init (cp_parser *parser,
34749 enum tree_code code,
34750 tree &this_pre_body,
34751 vec<tree, va_gc> *for_block,
34752 tree &init,
34753 tree &orig_init,
34754 tree &decl,
34755 tree &real_decl)
34757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34758 return NULL_TREE;
34760 tree add_private_clause = NULL_TREE;
34762 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34764 init-expr:
34765 var = lb
34766 integer-type var = lb
34767 random-access-iterator-type var = lb
34768 pointer-type var = lb
34770 cp_decl_specifier_seq type_specifiers;
34772 /* First, try to parse as an initialized declaration. See
34773 cp_parser_condition, from whence the bulk of this is copied. */
34775 cp_parser_parse_tentatively (parser);
34776 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34777 /*is_trailing_return=*/false,
34778 &type_specifiers);
34779 if (cp_parser_parse_definitely (parser))
34781 /* If parsing a type specifier seq succeeded, then this
34782 MUST be a initialized declaration. */
34783 tree asm_specification, attributes;
34784 cp_declarator *declarator;
34786 declarator = cp_parser_declarator (parser,
34787 CP_PARSER_DECLARATOR_NAMED,
34788 /*ctor_dtor_or_conv_p=*/NULL,
34789 /*parenthesized_p=*/NULL,
34790 /*member_p=*/false,
34791 /*friend_p=*/false);
34792 attributes = cp_parser_attributes_opt (parser);
34793 asm_specification = cp_parser_asm_specification_opt (parser);
34795 if (declarator == cp_error_declarator)
34796 cp_parser_skip_to_end_of_statement (parser);
34798 else
34800 tree pushed_scope, auto_node;
34802 decl = start_decl (declarator, &type_specifiers,
34803 SD_INITIALIZED, attributes,
34804 /*prefix_attributes=*/NULL_TREE,
34805 &pushed_scope);
34807 auto_node = type_uses_auto (TREE_TYPE (decl));
34808 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34810 if (cp_lexer_next_token_is (parser->lexer,
34811 CPP_OPEN_PAREN))
34813 if (code != CILK_SIMD && code != CILK_FOR)
34814 error ("parenthesized initialization is not allowed in "
34815 "OpenMP %<for%> loop");
34816 else
34817 error ("parenthesized initialization is "
34818 "not allowed in for-loop");
34820 else
34821 /* Trigger an error. */
34822 cp_parser_require (parser, CPP_EQ, RT_EQ);
34824 init = error_mark_node;
34825 cp_parser_skip_to_end_of_statement (parser);
34827 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34828 || type_dependent_expression_p (decl)
34829 || auto_node)
34831 bool is_direct_init, is_non_constant_init;
34833 init = cp_parser_initializer (parser,
34834 &is_direct_init,
34835 &is_non_constant_init);
34837 if (auto_node)
34839 TREE_TYPE (decl)
34840 = do_auto_deduction (TREE_TYPE (decl), init,
34841 auto_node);
34843 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34844 && !type_dependent_expression_p (decl))
34845 goto non_class;
34848 cp_finish_decl (decl, init, !is_non_constant_init,
34849 asm_specification,
34850 LOOKUP_ONLYCONVERTING);
34851 orig_init = init;
34852 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34854 vec_safe_push (for_block, this_pre_body);
34855 init = NULL_TREE;
34857 else
34859 init = pop_stmt_list (this_pre_body);
34860 if (init && TREE_CODE (init) == STATEMENT_LIST)
34862 tree_stmt_iterator i = tsi_start (init);
34863 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34864 while (!tsi_end_p (i))
34866 tree t = tsi_stmt (i);
34867 if (TREE_CODE (t) == DECL_EXPR
34868 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34870 tsi_delink (&i);
34871 vec_safe_push (for_block, t);
34872 continue;
34874 break;
34876 if (tsi_one_before_end_p (i))
34878 tree t = tsi_stmt (i);
34879 tsi_delink (&i);
34880 free_stmt_list (init);
34881 init = t;
34885 this_pre_body = NULL_TREE;
34887 else
34889 /* Consume '='. */
34890 cp_lexer_consume_token (parser->lexer);
34891 init = cp_parser_assignment_expression (parser);
34893 non_class:
34894 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34895 init = error_mark_node;
34896 else
34897 cp_finish_decl (decl, NULL_TREE,
34898 /*init_const_expr_p=*/false,
34899 asm_specification,
34900 LOOKUP_ONLYCONVERTING);
34903 if (pushed_scope)
34904 pop_scope (pushed_scope);
34907 else
34909 cp_id_kind idk;
34910 /* If parsing a type specifier sequence failed, then
34911 this MUST be a simple expression. */
34912 if (code == CILK_FOR)
34913 error ("%<_Cilk_for%> allows expression instead of declaration only "
34914 "in C, not in C++");
34915 cp_parser_parse_tentatively (parser);
34916 decl = cp_parser_primary_expression (parser, false, false,
34917 false, &idk);
34918 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34919 if (!cp_parser_error_occurred (parser)
34920 && decl
34921 && (TREE_CODE (decl) == COMPONENT_REF
34922 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34924 cp_parser_abort_tentative_parse (parser);
34925 cp_parser_parse_tentatively (parser);
34926 cp_token *token = cp_lexer_peek_token (parser->lexer);
34927 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34928 /*check_dependency_p=*/true,
34929 /*template_p=*/NULL,
34930 /*declarator_p=*/false,
34931 /*optional_p=*/false);
34932 if (name != error_mark_node
34933 && last_tok == cp_lexer_peek_token (parser->lexer))
34935 decl = cp_parser_lookup_name_simple (parser, name,
34936 token->location);
34937 if (TREE_CODE (decl) == FIELD_DECL)
34938 add_private_clause = omp_privatize_field (decl, false);
34940 cp_parser_abort_tentative_parse (parser);
34941 cp_parser_parse_tentatively (parser);
34942 decl = cp_parser_primary_expression (parser, false, false,
34943 false, &idk);
34945 if (!cp_parser_error_occurred (parser)
34946 && decl
34947 && DECL_P (decl)
34948 && CLASS_TYPE_P (TREE_TYPE (decl)))
34950 tree rhs;
34952 cp_parser_parse_definitely (parser);
34953 cp_parser_require (parser, CPP_EQ, RT_EQ);
34954 rhs = cp_parser_assignment_expression (parser);
34955 orig_init = rhs;
34956 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34957 decl, NOP_EXPR,
34958 rhs,
34959 tf_warning_or_error));
34960 if (!add_private_clause)
34961 add_private_clause = decl;
34963 else
34965 decl = NULL;
34966 cp_parser_abort_tentative_parse (parser);
34967 init = cp_parser_expression (parser);
34968 if (init)
34970 if (TREE_CODE (init) == MODIFY_EXPR
34971 || TREE_CODE (init) == MODOP_EXPR)
34972 real_decl = TREE_OPERAND (init, 0);
34976 return add_private_clause;
34979 /* Parse the restricted form of the for statement allowed by OpenMP. */
34981 static tree
34982 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34983 tree *cclauses, bool *if_p)
34985 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34986 tree real_decl, initv, condv, incrv, declv;
34987 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34988 location_t loc_first;
34989 bool collapse_err = false;
34990 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34991 vec<tree, va_gc> *for_block = make_tree_vector ();
34992 auto_vec<tree, 4> orig_inits;
34993 bool tiling = false;
34995 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34996 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34997 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34998 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
35000 tiling = true;
35001 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
35003 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
35004 && OMP_CLAUSE_ORDERED_EXPR (cl))
35006 ordered_cl = cl;
35007 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
35010 if (ordered && ordered < collapse)
35012 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
35013 "%<ordered%> clause parameter is less than %<collapse%>");
35014 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
35015 = build_int_cst (NULL_TREE, collapse);
35016 ordered = collapse;
35018 if (ordered)
35020 for (tree *pc = &clauses; *pc; )
35021 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
35023 error_at (OMP_CLAUSE_LOCATION (*pc),
35024 "%<linear%> clause may not be specified together "
35025 "with %<ordered%> clause with a parameter");
35026 *pc = OMP_CLAUSE_CHAIN (*pc);
35028 else
35029 pc = &OMP_CLAUSE_CHAIN (*pc);
35032 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
35033 count = ordered ? ordered : collapse;
35035 declv = make_tree_vec (count);
35036 initv = make_tree_vec (count);
35037 condv = make_tree_vec (count);
35038 incrv = make_tree_vec (count);
35040 loc_first = cp_lexer_peek_token (parser->lexer)->location;
35042 for (i = 0; i < count; i++)
35044 int bracecount = 0;
35045 tree add_private_clause = NULL_TREE;
35046 location_t loc;
35048 if (code != CILK_FOR
35049 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35051 if (!collapse_err)
35052 cp_parser_error (parser, "for statement expected");
35053 return NULL;
35055 if (code == CILK_FOR
35056 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
35058 if (!collapse_err)
35059 cp_parser_error (parser, "_Cilk_for statement expected");
35060 return NULL;
35062 loc = cp_lexer_consume_token (parser->lexer)->location;
35064 matching_parens parens;
35065 if (!parens.require_open (parser))
35066 return NULL;
35068 init = orig_init = decl = real_decl = NULL;
35069 this_pre_body = push_stmt_list ();
35071 add_private_clause
35072 = cp_parser_omp_for_loop_init (parser, code,
35073 this_pre_body, for_block,
35074 init, orig_init, decl, real_decl);
35076 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35077 if (this_pre_body)
35079 this_pre_body = pop_stmt_list (this_pre_body);
35080 if (pre_body)
35082 tree t = pre_body;
35083 pre_body = push_stmt_list ();
35084 add_stmt (t);
35085 add_stmt (this_pre_body);
35086 pre_body = pop_stmt_list (pre_body);
35088 else
35089 pre_body = this_pre_body;
35092 if (decl)
35093 real_decl = decl;
35094 if (cclauses != NULL
35095 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
35096 && real_decl != NULL_TREE)
35098 tree *c;
35099 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
35100 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
35101 && OMP_CLAUSE_DECL (*c) == real_decl)
35103 error_at (loc, "iteration variable %qD"
35104 " should not be firstprivate", real_decl);
35105 *c = OMP_CLAUSE_CHAIN (*c);
35107 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35108 && OMP_CLAUSE_DECL (*c) == real_decl)
35110 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35111 tree l = *c;
35112 *c = OMP_CLAUSE_CHAIN (*c);
35113 if (code == OMP_SIMD)
35115 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35116 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35118 else
35120 OMP_CLAUSE_CHAIN (l) = clauses;
35121 clauses = l;
35123 add_private_clause = NULL_TREE;
35125 else
35127 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35128 && OMP_CLAUSE_DECL (*c) == real_decl)
35129 add_private_clause = NULL_TREE;
35130 c = &OMP_CLAUSE_CHAIN (*c);
35134 if (add_private_clause)
35136 tree c;
35137 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35139 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35140 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35141 && OMP_CLAUSE_DECL (c) == decl)
35142 break;
35143 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35144 && OMP_CLAUSE_DECL (c) == decl)
35145 error_at (loc, "iteration variable %qD "
35146 "should not be firstprivate",
35147 decl);
35148 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35149 && OMP_CLAUSE_DECL (c) == decl)
35150 error_at (loc, "iteration variable %qD should not be reduction",
35151 decl);
35153 if (c == NULL)
35155 if (code != OMP_SIMD)
35156 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35157 else if (collapse == 1)
35158 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35159 else
35160 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35161 OMP_CLAUSE_DECL (c) = add_private_clause;
35162 c = finish_omp_clauses (c, C_ORT_OMP);
35163 if (c)
35165 OMP_CLAUSE_CHAIN (c) = clauses;
35166 clauses = c;
35167 /* For linear, signal that we need to fill up
35168 the so far unknown linear step. */
35169 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35170 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35175 cond = NULL;
35176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35177 cond = cp_parser_omp_for_cond (parser, decl, code);
35178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35180 incr = NULL;
35181 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35183 /* If decl is an iterator, preserve the operator on decl
35184 until finish_omp_for. */
35185 if (real_decl
35186 && ((processing_template_decl
35187 && (TREE_TYPE (real_decl) == NULL_TREE
35188 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35189 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35190 incr = cp_parser_omp_for_incr (parser, real_decl);
35191 else
35192 incr = cp_parser_expression (parser);
35193 if (!EXPR_HAS_LOCATION (incr))
35194 protected_set_expr_location (incr, input_location);
35197 if (!parens.require_close (parser))
35198 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35199 /*or_comma=*/false,
35200 /*consume_paren=*/true);
35202 TREE_VEC_ELT (declv, i) = decl;
35203 TREE_VEC_ELT (initv, i) = init;
35204 TREE_VEC_ELT (condv, i) = cond;
35205 TREE_VEC_ELT (incrv, i) = incr;
35206 if (orig_init)
35208 orig_inits.safe_grow_cleared (i + 1);
35209 orig_inits[i] = orig_init;
35212 if (i == count - 1)
35213 break;
35215 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35216 in between the collapsed for loops to be still considered perfectly
35217 nested. Hopefully the final version clarifies this.
35218 For now handle (multiple) {'s and empty statements. */
35219 cp_parser_parse_tentatively (parser);
35220 for (;;)
35222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35223 break;
35224 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35226 cp_lexer_consume_token (parser->lexer);
35227 bracecount++;
35229 else if (bracecount
35230 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35231 cp_lexer_consume_token (parser->lexer);
35232 else
35234 loc = cp_lexer_peek_token (parser->lexer)->location;
35235 error_at (loc, "not enough for loops to collapse");
35236 collapse_err = true;
35237 cp_parser_abort_tentative_parse (parser);
35238 declv = NULL_TREE;
35239 break;
35243 if (declv)
35245 cp_parser_parse_definitely (parser);
35246 nbraces += bracecount;
35250 if (nbraces)
35251 if_p = NULL;
35253 /* Note that we saved the original contents of this flag when we entered
35254 the structured block, and so we don't need to re-save it here. */
35255 if (code == CILK_SIMD || code == CILK_FOR)
35256 parser->in_statement = IN_CILK_SIMD_FOR;
35257 else
35258 parser->in_statement = IN_OMP_FOR;
35260 /* Note that the grammar doesn't call for a structured block here,
35261 though the loop as a whole is a structured block. */
35262 body = push_stmt_list ();
35263 cp_parser_statement (parser, NULL_TREE, false, if_p);
35264 body = pop_stmt_list (body);
35266 if (declv == NULL_TREE)
35267 ret = NULL_TREE;
35268 else
35269 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35270 body, pre_body, &orig_inits, clauses);
35272 while (nbraces)
35274 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35276 cp_lexer_consume_token (parser->lexer);
35277 nbraces--;
35279 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35280 cp_lexer_consume_token (parser->lexer);
35281 else
35283 if (!collapse_err)
35285 error_at (cp_lexer_peek_token (parser->lexer)->location,
35286 "collapsed loops not perfectly nested");
35288 collapse_err = true;
35289 cp_parser_statement_seq_opt (parser, NULL);
35290 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35291 break;
35295 while (!for_block->is_empty ())
35297 tree t = for_block->pop ();
35298 if (TREE_CODE (t) == STATEMENT_LIST)
35299 add_stmt (pop_stmt_list (t));
35300 else
35301 add_stmt (t);
35303 release_tree_vector (for_block);
35305 return ret;
35308 /* Helper function for OpenMP parsing, split clauses and call
35309 finish_omp_clauses on each of the set of clauses afterwards. */
35311 static void
35312 cp_omp_split_clauses (location_t loc, enum tree_code code,
35313 omp_clause_mask mask, tree clauses, tree *cclauses)
35315 int i;
35316 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35317 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35318 if (cclauses[i])
35319 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35322 /* OpenMP 4.0:
35323 #pragma omp simd simd-clause[optseq] new-line
35324 for-loop */
35326 #define OMP_SIMD_CLAUSE_MASK \
35327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35336 static tree
35337 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35338 char *p_name, omp_clause_mask mask, tree *cclauses,
35339 bool *if_p)
35341 tree clauses, sb, ret;
35342 unsigned int save;
35343 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35345 strcat (p_name, " simd");
35346 mask |= OMP_SIMD_CLAUSE_MASK;
35348 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35349 cclauses == NULL);
35350 if (cclauses)
35352 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35353 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35354 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35355 OMP_CLAUSE_ORDERED);
35356 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35358 error_at (OMP_CLAUSE_LOCATION (c),
35359 "%<ordered%> clause with parameter may not be specified "
35360 "on %qs construct", p_name);
35361 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35365 sb = begin_omp_structured_block ();
35366 save = cp_parser_begin_omp_structured_block (parser);
35368 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35370 cp_parser_end_omp_structured_block (parser, save);
35371 add_stmt (finish_omp_structured_block (sb));
35373 return ret;
35376 /* OpenMP 2.5:
35377 #pragma omp for for-clause[optseq] new-line
35378 for-loop
35380 OpenMP 4.0:
35381 #pragma omp for simd for-simd-clause[optseq] new-line
35382 for-loop */
35384 #define OMP_FOR_CLAUSE_MASK \
35385 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35395 static tree
35396 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35397 char *p_name, omp_clause_mask mask, tree *cclauses,
35398 bool *if_p)
35400 tree clauses, sb, ret;
35401 unsigned int save;
35402 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35404 strcat (p_name, " for");
35405 mask |= OMP_FOR_CLAUSE_MASK;
35406 /* parallel for{, simd} disallows nowait clause, but for
35407 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35408 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35409 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35410 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35411 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35412 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35414 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35416 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35417 const char *p = IDENTIFIER_POINTER (id);
35419 if (strcmp (p, "simd") == 0)
35421 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35422 if (cclauses == NULL)
35423 cclauses = cclauses_buf;
35425 cp_lexer_consume_token (parser->lexer);
35426 if (!flag_openmp) /* flag_openmp_simd */
35427 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35428 cclauses, if_p);
35429 sb = begin_omp_structured_block ();
35430 save = cp_parser_begin_omp_structured_block (parser);
35431 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35432 cclauses, if_p);
35433 cp_parser_end_omp_structured_block (parser, save);
35434 tree body = finish_omp_structured_block (sb);
35435 if (ret == NULL)
35436 return ret;
35437 ret = make_node (OMP_FOR);
35438 TREE_TYPE (ret) = void_type_node;
35439 OMP_FOR_BODY (ret) = body;
35440 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35441 SET_EXPR_LOCATION (ret, loc);
35442 add_stmt (ret);
35443 return ret;
35446 if (!flag_openmp) /* flag_openmp_simd */
35448 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35449 return NULL_TREE;
35452 /* Composite distribute parallel for disallows linear clause. */
35453 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35454 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35456 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35457 cclauses == NULL);
35458 if (cclauses)
35460 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35461 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35464 sb = begin_omp_structured_block ();
35465 save = cp_parser_begin_omp_structured_block (parser);
35467 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35469 cp_parser_end_omp_structured_block (parser, save);
35470 add_stmt (finish_omp_structured_block (sb));
35472 return ret;
35475 /* OpenMP 2.5:
35476 # pragma omp master new-line
35477 structured-block */
35479 static tree
35480 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35482 cp_parser_require_pragma_eol (parser, pragma_tok);
35483 return c_finish_omp_master (input_location,
35484 cp_parser_omp_structured_block (parser, if_p));
35487 /* OpenMP 2.5:
35488 # pragma omp ordered new-line
35489 structured-block
35491 OpenMP 4.5:
35492 # pragma omp ordered ordered-clauses new-line
35493 structured-block */
35495 #define OMP_ORDERED_CLAUSE_MASK \
35496 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35499 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35500 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35502 static bool
35503 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35504 enum pragma_context context, bool *if_p)
35506 location_t loc = pragma_tok->location;
35508 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35510 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35511 const char *p = IDENTIFIER_POINTER (id);
35513 if (strcmp (p, "depend") == 0)
35515 if (!flag_openmp) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35518 return false;
35520 if (context == pragma_stmt)
35522 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35523 "%<depend%> clause may only be used in compound "
35524 "statements");
35525 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35526 return false;
35528 tree clauses
35529 = cp_parser_omp_all_clauses (parser,
35530 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35531 "#pragma omp ordered", pragma_tok);
35532 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35533 return false;
35537 tree clauses
35538 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35539 "#pragma omp ordered", pragma_tok);
35541 if (!flag_openmp /* flag_openmp_simd */
35542 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
35543 return false;
35545 c_finish_omp_ordered (loc, clauses,
35546 cp_parser_omp_structured_block (parser, if_p));
35547 return true;
35550 /* OpenMP 2.5:
35552 section-scope:
35553 { section-sequence }
35555 section-sequence:
35556 section-directive[opt] structured-block
35557 section-sequence section-directive structured-block */
35559 static tree
35560 cp_parser_omp_sections_scope (cp_parser *parser)
35562 tree stmt, substmt;
35563 bool error_suppress = false;
35564 cp_token *tok;
35566 matching_braces braces;
35567 if (!braces.require_open (parser))
35568 return NULL_TREE;
35570 stmt = push_stmt_list ();
35572 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35573 != PRAGMA_OMP_SECTION)
35575 substmt = cp_parser_omp_structured_block (parser, NULL);
35576 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35577 add_stmt (substmt);
35580 while (1)
35582 tok = cp_lexer_peek_token (parser->lexer);
35583 if (tok->type == CPP_CLOSE_BRACE)
35584 break;
35585 if (tok->type == CPP_EOF)
35586 break;
35588 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35590 cp_lexer_consume_token (parser->lexer);
35591 cp_parser_require_pragma_eol (parser, tok);
35592 error_suppress = false;
35594 else if (!error_suppress)
35596 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35597 error_suppress = true;
35600 substmt = cp_parser_omp_structured_block (parser, NULL);
35601 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35602 add_stmt (substmt);
35604 braces.require_close (parser);
35606 substmt = pop_stmt_list (stmt);
35608 stmt = make_node (OMP_SECTIONS);
35609 TREE_TYPE (stmt) = void_type_node;
35610 OMP_SECTIONS_BODY (stmt) = substmt;
35612 add_stmt (stmt);
35613 return stmt;
35616 /* OpenMP 2.5:
35617 # pragma omp sections sections-clause[optseq] newline
35618 sections-scope */
35620 #define OMP_SECTIONS_CLAUSE_MASK \
35621 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35627 static tree
35628 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35629 char *p_name, omp_clause_mask mask, tree *cclauses)
35631 tree clauses, ret;
35632 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35634 strcat (p_name, " sections");
35635 mask |= OMP_SECTIONS_CLAUSE_MASK;
35636 if (cclauses)
35637 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35639 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35640 cclauses == NULL);
35641 if (cclauses)
35643 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35644 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35647 ret = cp_parser_omp_sections_scope (parser);
35648 if (ret)
35649 OMP_SECTIONS_CLAUSES (ret) = clauses;
35651 return ret;
35654 /* OpenMP 2.5:
35655 # pragma omp parallel parallel-clause[optseq] new-line
35656 structured-block
35657 # pragma omp parallel for parallel-for-clause[optseq] new-line
35658 structured-block
35659 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35660 structured-block
35662 OpenMP 4.0:
35663 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35664 structured-block */
35666 #define OMP_PARALLEL_CLAUSE_MASK \
35667 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35677 static tree
35678 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35679 char *p_name, omp_clause_mask mask, tree *cclauses,
35680 bool *if_p)
35682 tree stmt, clauses, block;
35683 unsigned int save;
35684 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35686 strcat (p_name, " parallel");
35687 mask |= OMP_PARALLEL_CLAUSE_MASK;
35688 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35689 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35690 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35691 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35693 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35695 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35696 if (cclauses == NULL)
35697 cclauses = cclauses_buf;
35699 cp_lexer_consume_token (parser->lexer);
35700 if (!flag_openmp) /* flag_openmp_simd */
35701 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35702 if_p);
35703 block = begin_omp_parallel ();
35704 save = cp_parser_begin_omp_structured_block (parser);
35705 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35706 if_p);
35707 cp_parser_end_omp_structured_block (parser, save);
35708 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35709 block);
35710 if (ret == NULL_TREE)
35711 return ret;
35712 OMP_PARALLEL_COMBINED (stmt) = 1;
35713 return stmt;
35715 /* When combined with distribute, parallel has to be followed by for.
35716 #pragma omp target parallel is allowed though. */
35717 else if (cclauses
35718 && (mask & (OMP_CLAUSE_MASK_1
35719 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35721 error_at (loc, "expected %<for%> after %qs", p_name);
35722 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35723 return NULL_TREE;
35725 else if (!flag_openmp) /* flag_openmp_simd */
35727 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35728 return NULL_TREE;
35730 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35732 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35733 const char *p = IDENTIFIER_POINTER (id);
35734 if (strcmp (p, "sections") == 0)
35736 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35737 cclauses = cclauses_buf;
35739 cp_lexer_consume_token (parser->lexer);
35740 block = begin_omp_parallel ();
35741 save = cp_parser_begin_omp_structured_block (parser);
35742 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35743 cp_parser_end_omp_structured_block (parser, save);
35744 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35745 block);
35746 OMP_PARALLEL_COMBINED (stmt) = 1;
35747 return stmt;
35751 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35752 cclauses == NULL);
35753 if (cclauses)
35755 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35756 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35759 block = begin_omp_parallel ();
35760 save = cp_parser_begin_omp_structured_block (parser);
35761 cp_parser_statement (parser, NULL_TREE, false, if_p);
35762 cp_parser_end_omp_structured_block (parser, save);
35763 stmt = finish_omp_parallel (clauses, block);
35764 return stmt;
35767 /* OpenMP 2.5:
35768 # pragma omp single single-clause[optseq] new-line
35769 structured-block */
35771 #define OMP_SINGLE_CLAUSE_MASK \
35772 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35774 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35777 static tree
35778 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35780 tree stmt = make_node (OMP_SINGLE);
35781 TREE_TYPE (stmt) = void_type_node;
35783 OMP_SINGLE_CLAUSES (stmt)
35784 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35785 "#pragma omp single", pragma_tok);
35786 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35788 return add_stmt (stmt);
35791 /* OpenMP 3.0:
35792 # pragma omp task task-clause[optseq] new-line
35793 structured-block */
35795 #define OMP_TASK_CLAUSE_MASK \
35796 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35797 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35798 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35807 static tree
35808 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35810 tree clauses, block;
35811 unsigned int save;
35813 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35814 "#pragma omp task", pragma_tok);
35815 block = begin_omp_task ();
35816 save = cp_parser_begin_omp_structured_block (parser);
35817 cp_parser_statement (parser, NULL_TREE, false, if_p);
35818 cp_parser_end_omp_structured_block (parser, save);
35819 return finish_omp_task (clauses, block);
35822 /* OpenMP 3.0:
35823 # pragma omp taskwait new-line */
35825 static void
35826 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35828 cp_parser_require_pragma_eol (parser, pragma_tok);
35829 finish_omp_taskwait ();
35832 /* OpenMP 3.1:
35833 # pragma omp taskyield new-line */
35835 static void
35836 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35838 cp_parser_require_pragma_eol (parser, pragma_tok);
35839 finish_omp_taskyield ();
35842 /* OpenMP 4.0:
35843 # pragma omp taskgroup new-line
35844 structured-block */
35846 static tree
35847 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35849 cp_parser_require_pragma_eol (parser, pragma_tok);
35850 return c_finish_omp_taskgroup (input_location,
35851 cp_parser_omp_structured_block (parser,
35852 if_p));
35856 /* OpenMP 2.5:
35857 # pragma omp threadprivate (variable-list) */
35859 static void
35860 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35862 tree vars;
35864 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35865 cp_parser_require_pragma_eol (parser, pragma_tok);
35867 finish_omp_threadprivate (vars);
35870 /* OpenMP 4.0:
35871 # pragma omp cancel cancel-clause[optseq] new-line */
35873 #define OMP_CANCEL_CLAUSE_MASK \
35874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35880 static void
35881 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35883 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35884 "#pragma omp cancel", pragma_tok);
35885 finish_omp_cancel (clauses);
35888 /* OpenMP 4.0:
35889 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35891 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35892 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35893 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35897 static void
35898 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35899 enum pragma_context context)
35901 tree clauses;
35902 bool point_seen = false;
35904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35907 const char *p = IDENTIFIER_POINTER (id);
35909 if (strcmp (p, "point") == 0)
35911 cp_lexer_consume_token (parser->lexer);
35912 point_seen = true;
35915 if (!point_seen)
35917 cp_parser_error (parser, "expected %<point%>");
35918 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35919 return;
35922 if (context != pragma_compound)
35924 if (context == pragma_stmt)
35925 error_at (pragma_tok->location,
35926 "%<#pragma %s%> may only be used in compound statements",
35927 "omp cancellation point");
35928 else
35929 cp_parser_error (parser, "expected declaration specifiers");
35930 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35931 return;
35934 clauses = cp_parser_omp_all_clauses (parser,
35935 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35936 "#pragma omp cancellation point",
35937 pragma_tok);
35938 finish_omp_cancellation_point (clauses);
35941 /* OpenMP 4.0:
35942 #pragma omp distribute distribute-clause[optseq] new-line
35943 for-loop */
35945 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35946 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35948 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35949 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35950 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35952 static tree
35953 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35954 char *p_name, omp_clause_mask mask, tree *cclauses,
35955 bool *if_p)
35957 tree clauses, sb, ret;
35958 unsigned int save;
35959 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35961 strcat (p_name, " distribute");
35962 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35964 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35966 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35967 const char *p = IDENTIFIER_POINTER (id);
35968 bool simd = false;
35969 bool parallel = false;
35971 if (strcmp (p, "simd") == 0)
35972 simd = true;
35973 else
35974 parallel = strcmp (p, "parallel") == 0;
35975 if (parallel || simd)
35977 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35978 if (cclauses == NULL)
35979 cclauses = cclauses_buf;
35980 cp_lexer_consume_token (parser->lexer);
35981 if (!flag_openmp) /* flag_openmp_simd */
35983 if (simd)
35984 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35985 cclauses, if_p);
35986 else
35987 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35988 cclauses, if_p);
35990 sb = begin_omp_structured_block ();
35991 save = cp_parser_begin_omp_structured_block (parser);
35992 if (simd)
35993 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35994 cclauses, if_p);
35995 else
35996 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35997 cclauses, if_p);
35998 cp_parser_end_omp_structured_block (parser, save);
35999 tree body = finish_omp_structured_block (sb);
36000 if (ret == NULL)
36001 return ret;
36002 ret = make_node (OMP_DISTRIBUTE);
36003 TREE_TYPE (ret) = void_type_node;
36004 OMP_FOR_BODY (ret) = body;
36005 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36006 SET_EXPR_LOCATION (ret, loc);
36007 add_stmt (ret);
36008 return ret;
36011 if (!flag_openmp) /* flag_openmp_simd */
36013 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36014 return NULL_TREE;
36017 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36018 cclauses == NULL);
36019 if (cclauses)
36021 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
36022 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
36025 sb = begin_omp_structured_block ();
36026 save = cp_parser_begin_omp_structured_block (parser);
36028 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
36030 cp_parser_end_omp_structured_block (parser, save);
36031 add_stmt (finish_omp_structured_block (sb));
36033 return ret;
36036 /* OpenMP 4.0:
36037 # pragma omp teams teams-clause[optseq] new-line
36038 structured-block */
36040 #define OMP_TEAMS_CLAUSE_MASK \
36041 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
36045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
36046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
36047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
36049 static tree
36050 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
36051 char *p_name, omp_clause_mask mask, tree *cclauses,
36052 bool *if_p)
36054 tree clauses, sb, ret;
36055 unsigned int save;
36056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36058 strcat (p_name, " teams");
36059 mask |= OMP_TEAMS_CLAUSE_MASK;
36061 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36063 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36064 const char *p = IDENTIFIER_POINTER (id);
36065 if (strcmp (p, "distribute") == 0)
36067 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36068 if (cclauses == NULL)
36069 cclauses = cclauses_buf;
36071 cp_lexer_consume_token (parser->lexer);
36072 if (!flag_openmp) /* flag_openmp_simd */
36073 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36074 cclauses, if_p);
36075 sb = begin_omp_structured_block ();
36076 save = cp_parser_begin_omp_structured_block (parser);
36077 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
36078 cclauses, if_p);
36079 cp_parser_end_omp_structured_block (parser, save);
36080 tree body = finish_omp_structured_block (sb);
36081 if (ret == NULL)
36082 return ret;
36083 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36084 ret = make_node (OMP_TEAMS);
36085 TREE_TYPE (ret) = void_type_node;
36086 OMP_TEAMS_CLAUSES (ret) = clauses;
36087 OMP_TEAMS_BODY (ret) = body;
36088 OMP_TEAMS_COMBINED (ret) = 1;
36089 SET_EXPR_LOCATION (ret, loc);
36090 return add_stmt (ret);
36093 if (!flag_openmp) /* flag_openmp_simd */
36095 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36096 return NULL_TREE;
36099 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36100 cclauses == NULL);
36101 if (cclauses)
36103 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
36104 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36107 tree stmt = make_node (OMP_TEAMS);
36108 TREE_TYPE (stmt) = void_type_node;
36109 OMP_TEAMS_CLAUSES (stmt) = clauses;
36110 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36111 SET_EXPR_LOCATION (stmt, loc);
36113 return add_stmt (stmt);
36116 /* OpenMP 4.0:
36117 # pragma omp target data target-data-clause[optseq] new-line
36118 structured-block */
36120 #define OMP_TARGET_DATA_CLAUSE_MASK \
36121 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36126 static tree
36127 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36129 tree clauses
36130 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36131 "#pragma omp target data", pragma_tok);
36132 int map_seen = 0;
36133 for (tree *pc = &clauses; *pc;)
36135 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36136 switch (OMP_CLAUSE_MAP_KIND (*pc))
36138 case GOMP_MAP_TO:
36139 case GOMP_MAP_ALWAYS_TO:
36140 case GOMP_MAP_FROM:
36141 case GOMP_MAP_ALWAYS_FROM:
36142 case GOMP_MAP_TOFROM:
36143 case GOMP_MAP_ALWAYS_TOFROM:
36144 case GOMP_MAP_ALLOC:
36145 map_seen = 3;
36146 break;
36147 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36148 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36149 case GOMP_MAP_ALWAYS_POINTER:
36150 break;
36151 default:
36152 map_seen |= 1;
36153 error_at (OMP_CLAUSE_LOCATION (*pc),
36154 "%<#pragma omp target data%> with map-type other "
36155 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36156 "on %<map%> clause");
36157 *pc = OMP_CLAUSE_CHAIN (*pc);
36158 continue;
36160 pc = &OMP_CLAUSE_CHAIN (*pc);
36163 if (map_seen != 3)
36165 if (map_seen == 0)
36166 error_at (pragma_tok->location,
36167 "%<#pragma omp target data%> must contain at least "
36168 "one %<map%> clause");
36169 return NULL_TREE;
36172 tree stmt = make_node (OMP_TARGET_DATA);
36173 TREE_TYPE (stmt) = void_type_node;
36174 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36176 keep_next_level (true);
36177 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36179 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36180 return add_stmt (stmt);
36183 /* OpenMP 4.5:
36184 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36185 structured-block */
36187 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36188 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36189 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36194 static tree
36195 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36196 enum pragma_context context)
36198 bool data_seen = false;
36199 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36202 const char *p = IDENTIFIER_POINTER (id);
36204 if (strcmp (p, "data") == 0)
36206 cp_lexer_consume_token (parser->lexer);
36207 data_seen = true;
36210 if (!data_seen)
36212 cp_parser_error (parser, "expected %<data%>");
36213 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36214 return NULL_TREE;
36217 if (context == pragma_stmt)
36219 error_at (pragma_tok->location,
36220 "%<#pragma %s%> may only be used in compound statements",
36221 "omp target enter data");
36222 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36223 return NULL_TREE;
36226 tree clauses
36227 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36228 "#pragma omp target enter data", pragma_tok);
36229 int map_seen = 0;
36230 for (tree *pc = &clauses; *pc;)
36232 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36233 switch (OMP_CLAUSE_MAP_KIND (*pc))
36235 case GOMP_MAP_TO:
36236 case GOMP_MAP_ALWAYS_TO:
36237 case GOMP_MAP_ALLOC:
36238 map_seen = 3;
36239 break;
36240 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36241 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36242 case GOMP_MAP_ALWAYS_POINTER:
36243 break;
36244 default:
36245 map_seen |= 1;
36246 error_at (OMP_CLAUSE_LOCATION (*pc),
36247 "%<#pragma omp target enter data%> with map-type other "
36248 "than %<to%> or %<alloc%> on %<map%> clause");
36249 *pc = OMP_CLAUSE_CHAIN (*pc);
36250 continue;
36252 pc = &OMP_CLAUSE_CHAIN (*pc);
36255 if (map_seen != 3)
36257 if (map_seen == 0)
36258 error_at (pragma_tok->location,
36259 "%<#pragma omp target enter data%> must contain at least "
36260 "one %<map%> clause");
36261 return NULL_TREE;
36264 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36265 TREE_TYPE (stmt) = void_type_node;
36266 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36267 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36268 return add_stmt (stmt);
36271 /* OpenMP 4.5:
36272 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36273 structured-block */
36275 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36282 static tree
36283 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36284 enum pragma_context context)
36286 bool data_seen = false;
36287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36289 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36290 const char *p = IDENTIFIER_POINTER (id);
36292 if (strcmp (p, "data") == 0)
36294 cp_lexer_consume_token (parser->lexer);
36295 data_seen = true;
36298 if (!data_seen)
36300 cp_parser_error (parser, "expected %<data%>");
36301 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36302 return NULL_TREE;
36305 if (context == pragma_stmt)
36307 error_at (pragma_tok->location,
36308 "%<#pragma %s%> may only be used in compound statements",
36309 "omp target exit data");
36310 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36311 return NULL_TREE;
36314 tree clauses
36315 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36316 "#pragma omp target exit data", pragma_tok);
36317 int map_seen = 0;
36318 for (tree *pc = &clauses; *pc;)
36320 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36321 switch (OMP_CLAUSE_MAP_KIND (*pc))
36323 case GOMP_MAP_FROM:
36324 case GOMP_MAP_ALWAYS_FROM:
36325 case GOMP_MAP_RELEASE:
36326 case GOMP_MAP_DELETE:
36327 map_seen = 3;
36328 break;
36329 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36330 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36331 case GOMP_MAP_ALWAYS_POINTER:
36332 break;
36333 default:
36334 map_seen |= 1;
36335 error_at (OMP_CLAUSE_LOCATION (*pc),
36336 "%<#pragma omp target exit data%> with map-type other "
36337 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36338 " clause");
36339 *pc = OMP_CLAUSE_CHAIN (*pc);
36340 continue;
36342 pc = &OMP_CLAUSE_CHAIN (*pc);
36345 if (map_seen != 3)
36347 if (map_seen == 0)
36348 error_at (pragma_tok->location,
36349 "%<#pragma omp target exit data%> must contain at least "
36350 "one %<map%> clause");
36351 return NULL_TREE;
36354 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36355 TREE_TYPE (stmt) = void_type_node;
36356 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36357 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36358 return add_stmt (stmt);
36361 /* OpenMP 4.0:
36362 # pragma omp target update target-update-clause[optseq] new-line */
36364 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36372 static bool
36373 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36374 enum pragma_context context)
36376 if (context == pragma_stmt)
36378 error_at (pragma_tok->location,
36379 "%<#pragma %s%> may only be used in compound statements",
36380 "omp target update");
36381 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36382 return false;
36385 tree clauses
36386 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36387 "#pragma omp target update", pragma_tok);
36388 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36389 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36391 error_at (pragma_tok->location,
36392 "%<#pragma omp target update%> must contain at least one "
36393 "%<from%> or %<to%> clauses");
36394 return false;
36397 tree stmt = make_node (OMP_TARGET_UPDATE);
36398 TREE_TYPE (stmt) = void_type_node;
36399 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36400 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36401 add_stmt (stmt);
36402 return false;
36405 /* OpenMP 4.0:
36406 # pragma omp target target-clause[optseq] new-line
36407 structured-block */
36409 #define OMP_TARGET_CLAUSE_MASK \
36410 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36420 static bool
36421 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36422 enum pragma_context context, bool *if_p)
36424 tree *pc = NULL, stmt;
36426 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36428 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36429 const char *p = IDENTIFIER_POINTER (id);
36430 enum tree_code ccode = ERROR_MARK;
36432 if (strcmp (p, "teams") == 0)
36433 ccode = OMP_TEAMS;
36434 else if (strcmp (p, "parallel") == 0)
36435 ccode = OMP_PARALLEL;
36436 else if (strcmp (p, "simd") == 0)
36437 ccode = OMP_SIMD;
36438 if (ccode != ERROR_MARK)
36440 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36441 char p_name[sizeof ("#pragma omp target teams distribute "
36442 "parallel for simd")];
36444 cp_lexer_consume_token (parser->lexer);
36445 strcpy (p_name, "#pragma omp target");
36446 if (!flag_openmp) /* flag_openmp_simd */
36448 tree stmt;
36449 switch (ccode)
36451 case OMP_TEAMS:
36452 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36453 OMP_TARGET_CLAUSE_MASK,
36454 cclauses, if_p);
36455 break;
36456 case OMP_PARALLEL:
36457 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36458 OMP_TARGET_CLAUSE_MASK,
36459 cclauses, if_p);
36460 break;
36461 case OMP_SIMD:
36462 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36463 OMP_TARGET_CLAUSE_MASK,
36464 cclauses, if_p);
36465 break;
36466 default:
36467 gcc_unreachable ();
36469 return stmt != NULL_TREE;
36471 keep_next_level (true);
36472 tree sb = begin_omp_structured_block (), ret;
36473 unsigned save = cp_parser_begin_omp_structured_block (parser);
36474 switch (ccode)
36476 case OMP_TEAMS:
36477 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36478 OMP_TARGET_CLAUSE_MASK, cclauses,
36479 if_p);
36480 break;
36481 case OMP_PARALLEL:
36482 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36483 OMP_TARGET_CLAUSE_MASK, cclauses,
36484 if_p);
36485 break;
36486 case OMP_SIMD:
36487 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36488 OMP_TARGET_CLAUSE_MASK, cclauses,
36489 if_p);
36490 break;
36491 default:
36492 gcc_unreachable ();
36494 cp_parser_end_omp_structured_block (parser, save);
36495 tree body = finish_omp_structured_block (sb);
36496 if (ret == NULL_TREE)
36497 return false;
36498 if (ccode == OMP_TEAMS && !processing_template_decl)
36500 /* For combined target teams, ensure the num_teams and
36501 thread_limit clause expressions are evaluated on the host,
36502 before entering the target construct. */
36503 tree c;
36504 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36505 c; c = OMP_CLAUSE_CHAIN (c))
36506 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36507 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36508 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36510 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36511 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36512 if (expr == error_mark_node)
36513 continue;
36514 tree tmp = TARGET_EXPR_SLOT (expr);
36515 add_stmt (expr);
36516 OMP_CLAUSE_OPERAND (c, 0) = expr;
36517 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36518 OMP_CLAUSE_FIRSTPRIVATE);
36519 OMP_CLAUSE_DECL (tc) = tmp;
36520 OMP_CLAUSE_CHAIN (tc)
36521 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36522 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36525 tree stmt = make_node (OMP_TARGET);
36526 TREE_TYPE (stmt) = void_type_node;
36527 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36528 OMP_TARGET_BODY (stmt) = body;
36529 OMP_TARGET_COMBINED (stmt) = 1;
36530 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36531 add_stmt (stmt);
36532 pc = &OMP_TARGET_CLAUSES (stmt);
36533 goto check_clauses;
36535 else if (!flag_openmp) /* flag_openmp_simd */
36537 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36538 return false;
36540 else if (strcmp (p, "data") == 0)
36542 cp_lexer_consume_token (parser->lexer);
36543 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36544 return true;
36546 else if (strcmp (p, "enter") == 0)
36548 cp_lexer_consume_token (parser->lexer);
36549 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36550 return false;
36552 else if (strcmp (p, "exit") == 0)
36554 cp_lexer_consume_token (parser->lexer);
36555 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36556 return false;
36558 else if (strcmp (p, "update") == 0)
36560 cp_lexer_consume_token (parser->lexer);
36561 return cp_parser_omp_target_update (parser, pragma_tok, context);
36564 if (!flag_openmp) /* flag_openmp_simd */
36566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36567 return false;
36570 stmt = make_node (OMP_TARGET);
36571 TREE_TYPE (stmt) = void_type_node;
36573 OMP_TARGET_CLAUSES (stmt)
36574 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36575 "#pragma omp target", pragma_tok);
36576 pc = &OMP_TARGET_CLAUSES (stmt);
36577 keep_next_level (true);
36578 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36580 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36581 add_stmt (stmt);
36583 check_clauses:
36584 while (*pc)
36586 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36587 switch (OMP_CLAUSE_MAP_KIND (*pc))
36589 case GOMP_MAP_TO:
36590 case GOMP_MAP_ALWAYS_TO:
36591 case GOMP_MAP_FROM:
36592 case GOMP_MAP_ALWAYS_FROM:
36593 case GOMP_MAP_TOFROM:
36594 case GOMP_MAP_ALWAYS_TOFROM:
36595 case GOMP_MAP_ALLOC:
36596 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36597 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36598 case GOMP_MAP_ALWAYS_POINTER:
36599 break;
36600 default:
36601 error_at (OMP_CLAUSE_LOCATION (*pc),
36602 "%<#pragma omp target%> with map-type other "
36603 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36604 "on %<map%> clause");
36605 *pc = OMP_CLAUSE_CHAIN (*pc);
36606 continue;
36608 pc = &OMP_CLAUSE_CHAIN (*pc);
36610 return true;
36613 /* OpenACC 2.0:
36614 # pragma acc cache (variable-list) new-line
36617 static tree
36618 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36620 tree stmt, clauses;
36622 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36623 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36625 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36627 stmt = make_node (OACC_CACHE);
36628 TREE_TYPE (stmt) = void_type_node;
36629 OACC_CACHE_CLAUSES (stmt) = clauses;
36630 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36631 add_stmt (stmt);
36633 return stmt;
36636 /* OpenACC 2.0:
36637 # pragma acc data oacc-data-clause[optseq] new-line
36638 structured-block */
36640 #define OACC_DATA_CLAUSE_MASK \
36641 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36653 static tree
36654 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36656 tree stmt, clauses, block;
36657 unsigned int save;
36659 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36660 "#pragma acc data", pragma_tok);
36662 block = begin_omp_parallel ();
36663 save = cp_parser_begin_omp_structured_block (parser);
36664 cp_parser_statement (parser, NULL_TREE, false, if_p);
36665 cp_parser_end_omp_structured_block (parser, save);
36666 stmt = finish_oacc_data (clauses, block);
36667 return stmt;
36670 /* OpenACC 2.0:
36671 # pragma acc host_data <clauses> new-line
36672 structured-block */
36674 #define OACC_HOST_DATA_CLAUSE_MASK \
36675 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36677 static tree
36678 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36680 tree stmt, clauses, block;
36681 unsigned int save;
36683 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36684 "#pragma acc host_data", pragma_tok);
36686 block = begin_omp_parallel ();
36687 save = cp_parser_begin_omp_structured_block (parser);
36688 cp_parser_statement (parser, NULL_TREE, false, if_p);
36689 cp_parser_end_omp_structured_block (parser, save);
36690 stmt = finish_oacc_host_data (clauses, block);
36691 return stmt;
36694 /* OpenACC 2.0:
36695 # pragma acc declare oacc-data-clause[optseq] new-line
36698 #define OACC_DECLARE_CLAUSE_MASK \
36699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36708 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36709 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36710 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36712 static tree
36713 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36715 tree clauses, stmt;
36716 bool error = false;
36718 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36719 "#pragma acc declare", pragma_tok, true);
36722 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36724 error_at (pragma_tok->location,
36725 "no valid clauses specified in %<#pragma acc declare%>");
36726 return NULL_TREE;
36729 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36731 location_t loc = OMP_CLAUSE_LOCATION (t);
36732 tree decl = OMP_CLAUSE_DECL (t);
36733 if (!DECL_P (decl))
36735 error_at (loc, "array section in %<#pragma acc declare%>");
36736 error = true;
36737 continue;
36739 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36740 switch (OMP_CLAUSE_MAP_KIND (t))
36742 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36743 case GOMP_MAP_FORCE_ALLOC:
36744 case GOMP_MAP_FORCE_TO:
36745 case GOMP_MAP_FORCE_DEVICEPTR:
36746 case GOMP_MAP_DEVICE_RESIDENT:
36747 break;
36749 case GOMP_MAP_LINK:
36750 if (!global_bindings_p ()
36751 && (TREE_STATIC (decl)
36752 || !DECL_EXTERNAL (decl)))
36754 error_at (loc,
36755 "%qD must be a global variable in "
36756 "%<#pragma acc declare link%>",
36757 decl);
36758 error = true;
36759 continue;
36761 break;
36763 default:
36764 if (global_bindings_p ())
36766 error_at (loc, "invalid OpenACC clause at file scope");
36767 error = true;
36768 continue;
36770 if (DECL_EXTERNAL (decl))
36772 error_at (loc,
36773 "invalid use of %<extern%> variable %qD "
36774 "in %<#pragma acc declare%>", decl);
36775 error = true;
36776 continue;
36778 else if (TREE_PUBLIC (decl))
36780 error_at (loc,
36781 "invalid use of %<global%> variable %qD "
36782 "in %<#pragma acc declare%>", decl);
36783 error = true;
36784 continue;
36786 break;
36789 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36790 || lookup_attribute ("omp declare target link",
36791 DECL_ATTRIBUTES (decl)))
36793 error_at (loc, "variable %qD used more than once with "
36794 "%<#pragma acc declare%>", decl);
36795 error = true;
36796 continue;
36799 if (!error)
36801 tree id;
36803 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36804 id = get_identifier ("omp declare target link");
36805 else
36806 id = get_identifier ("omp declare target");
36808 DECL_ATTRIBUTES (decl)
36809 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36810 if (global_bindings_p ())
36812 symtab_node *node = symtab_node::get (decl);
36813 if (node != NULL)
36815 node->offloadable = 1;
36816 if (ENABLE_OFFLOADING)
36818 g->have_offload = true;
36819 if (is_a <varpool_node *> (node))
36820 vec_safe_push (offload_vars, decl);
36827 if (error || global_bindings_p ())
36828 return NULL_TREE;
36830 stmt = make_node (OACC_DECLARE);
36831 TREE_TYPE (stmt) = void_type_node;
36832 OACC_DECLARE_CLAUSES (stmt) = clauses;
36833 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36835 add_stmt (stmt);
36837 return NULL_TREE;
36840 /* OpenACC 2.0:
36841 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36845 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36847 LOC is the location of the #pragma token.
36850 #define OACC_ENTER_DATA_CLAUSE_MASK \
36851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36859 #define OACC_EXIT_DATA_CLAUSE_MASK \
36860 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36866 static tree
36867 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36868 bool enter)
36870 location_t loc = pragma_tok->location;
36871 tree stmt, clauses;
36872 const char *p = "";
36874 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36875 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36877 if (strcmp (p, "data") != 0)
36879 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36880 enter ? "enter" : "exit");
36881 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36882 return NULL_TREE;
36885 cp_lexer_consume_token (parser->lexer);
36887 if (enter)
36888 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36889 "#pragma acc enter data", pragma_tok);
36890 else
36891 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36892 "#pragma acc exit data", pragma_tok);
36894 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36896 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36897 enter ? "enter" : "exit");
36898 return NULL_TREE;
36901 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36902 TREE_TYPE (stmt) = void_type_node;
36903 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36904 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36905 add_stmt (stmt);
36906 return stmt;
36909 /* OpenACC 2.0:
36910 # pragma acc loop oacc-loop-clause[optseq] new-line
36911 structured-block */
36913 #define OACC_LOOP_CLAUSE_MASK \
36914 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36915 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36916 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36917 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36918 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36919 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36925 static tree
36926 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36927 omp_clause_mask mask, tree *cclauses, bool *if_p)
36929 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36931 strcat (p_name, " loop");
36932 mask |= OACC_LOOP_CLAUSE_MASK;
36934 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36935 cclauses == NULL);
36936 if (cclauses)
36938 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36939 if (*cclauses)
36940 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36941 if (clauses)
36942 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36945 tree block = begin_omp_structured_block ();
36946 int save = cp_parser_begin_omp_structured_block (parser);
36947 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36948 cp_parser_end_omp_structured_block (parser, save);
36949 add_stmt (finish_omp_structured_block (block));
36951 return stmt;
36954 /* OpenACC 2.0:
36955 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36956 structured-block
36960 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36961 structured-block
36964 #define OACC_KERNELS_CLAUSE_MASK \
36965 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36966 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36967 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36968 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36969 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36970 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36971 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36972 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36973 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36974 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36975 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36977 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36978 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36979 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36980 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36981 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36983 #define OACC_PARALLEL_CLAUSE_MASK \
36984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36999 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
37000 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
37001 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
37002 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
37005 static tree
37006 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
37007 char *p_name, bool *if_p)
37009 omp_clause_mask mask;
37010 enum tree_code code;
37011 switch (cp_parser_pragma_kind (pragma_tok))
37013 case PRAGMA_OACC_KERNELS:
37014 strcat (p_name, " kernels");
37015 mask = OACC_KERNELS_CLAUSE_MASK;
37016 code = OACC_KERNELS;
37017 break;
37018 case PRAGMA_OACC_PARALLEL:
37019 strcat (p_name, " parallel");
37020 mask = OACC_PARALLEL_CLAUSE_MASK;
37021 code = OACC_PARALLEL;
37022 break;
37023 default:
37024 gcc_unreachable ();
37027 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37029 const char *p
37030 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
37031 if (strcmp (p, "loop") == 0)
37033 cp_lexer_consume_token (parser->lexer);
37034 tree block = begin_omp_parallel ();
37035 tree clauses;
37036 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
37037 if_p);
37038 return finish_omp_construct (code, block, clauses);
37042 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
37044 tree block = begin_omp_parallel ();
37045 unsigned int save = cp_parser_begin_omp_structured_block (parser);
37046 cp_parser_statement (parser, NULL_TREE, false, if_p);
37047 cp_parser_end_omp_structured_block (parser, save);
37048 return finish_omp_construct (code, block, clauses);
37051 /* OpenACC 2.0:
37052 # pragma acc update oacc-update-clause[optseq] new-line
37055 #define OACC_UPDATE_CLAUSE_MASK \
37056 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
37057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
37058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
37059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
37060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
37061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
37063 static tree
37064 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
37066 tree stmt, clauses;
37068 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
37069 "#pragma acc update", pragma_tok);
37071 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
37073 error_at (pragma_tok->location,
37074 "%<#pragma acc update%> must contain at least one "
37075 "%<device%> or %<host%> or %<self%> clause");
37076 return NULL_TREE;
37079 stmt = make_node (OACC_UPDATE);
37080 TREE_TYPE (stmt) = void_type_node;
37081 OACC_UPDATE_CLAUSES (stmt) = clauses;
37082 SET_EXPR_LOCATION (stmt, pragma_tok->location);
37083 add_stmt (stmt);
37084 return stmt;
37087 /* OpenACC 2.0:
37088 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
37090 LOC is the location of the #pragma token.
37093 #define OACC_WAIT_CLAUSE_MASK \
37094 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
37096 static tree
37097 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
37099 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
37100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37102 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37103 list = cp_parser_oacc_wait_list (parser, loc, list);
37105 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
37106 "#pragma acc wait", pragma_tok);
37108 stmt = c_finish_oacc_wait (loc, list, clauses);
37109 stmt = finish_expr_stmt (stmt);
37111 return stmt;
37114 /* OpenMP 4.0:
37115 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37117 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37118 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37125 static void
37126 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37127 enum pragma_context context)
37129 bool first_p = parser->omp_declare_simd == NULL;
37130 cp_omp_declare_simd_data data;
37131 if (first_p)
37133 data.error_seen = false;
37134 data.fndecl_seen = false;
37135 data.tokens = vNULL;
37136 data.clauses = NULL_TREE;
37137 /* It is safe to take the address of a local variable; it will only be
37138 used while this scope is live. */
37139 parser->omp_declare_simd = &data;
37142 /* Store away all pragma tokens. */
37143 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37144 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37145 cp_lexer_consume_token (parser->lexer);
37146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37147 parser->omp_declare_simd->error_seen = true;
37148 cp_parser_require_pragma_eol (parser, pragma_tok);
37149 struct cp_token_cache *cp
37150 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37151 parser->omp_declare_simd->tokens.safe_push (cp);
37153 if (first_p)
37155 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37156 cp_parser_pragma (parser, context, NULL);
37157 switch (context)
37159 case pragma_external:
37160 cp_parser_declaration (parser);
37161 break;
37162 case pragma_member:
37163 cp_parser_member_declaration (parser);
37164 break;
37165 case pragma_objc_icode:
37166 cp_parser_block_declaration (parser, /*statement_p=*/false);
37167 break;
37168 default:
37169 cp_parser_declaration_statement (parser);
37170 break;
37172 if (parser->omp_declare_simd
37173 && !parser->omp_declare_simd->error_seen
37174 && !parser->omp_declare_simd->fndecl_seen)
37175 error_at (pragma_tok->location,
37176 "%<#pragma omp declare simd%> not immediately followed by "
37177 "function declaration or definition");
37178 data.tokens.release ();
37179 parser->omp_declare_simd = NULL;
37183 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37184 This function is modelled similar to the late parsing of omp declare
37185 simd. */
37187 static tree
37188 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
37190 struct cp_token_cache *ce;
37191 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
37192 int ii = 0;
37194 if (parser->omp_declare_simd != NULL
37195 || lookup_attribute ("simd", attrs))
37197 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37198 "used in the same function marked as a Cilk Plus SIMD-enabled "
37199 "function");
37200 parser->cilk_simd_fn_info->tokens.release ();
37201 XDELETE (parser->cilk_simd_fn_info);
37202 parser->cilk_simd_fn_info = NULL;
37203 return attrs;
37205 if (!info->error_seen && info->fndecl_seen)
37207 error ("vector attribute not immediately followed by a single function"
37208 " declaration or definition");
37209 info->error_seen = true;
37211 if (info->error_seen)
37212 return attrs;
37214 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37216 tree c, cl;
37218 cp_parser_push_lexer_for_tokens (parser, ce);
37219 parser->lexer->in_pragma = true;
37220 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37221 "SIMD-enabled functions attribute",
37222 NULL);
37223 cp_parser_pop_lexer (parser);
37224 if (cl)
37225 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37227 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37228 TREE_CHAIN (c) = attrs;
37229 attrs = c;
37231 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37232 TREE_CHAIN (c) = attrs;
37233 if (processing_template_decl)
37234 ATTR_IS_DEPENDENT (c) = 1;
37235 attrs = c;
37237 info->fndecl_seen = true;
37238 parser->cilk_simd_fn_info->tokens.release ();
37239 XDELETE (parser->cilk_simd_fn_info);
37240 parser->cilk_simd_fn_info = NULL;
37241 return attrs;
37244 /* Finalize #pragma omp declare simd clauses after direct declarator has
37245 been parsed, and put that into "omp declare simd" attribute. */
37247 static tree
37248 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37250 struct cp_token_cache *ce;
37251 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37252 int i;
37254 if (!data->error_seen && data->fndecl_seen)
37256 error ("%<#pragma omp declare simd%> not immediately followed by "
37257 "a single function declaration or definition");
37258 data->error_seen = true;
37260 if (data->error_seen)
37261 return attrs;
37263 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37265 tree c, cl;
37267 cp_parser_push_lexer_for_tokens (parser, ce);
37268 parser->lexer->in_pragma = true;
37269 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37270 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37271 cp_lexer_consume_token (parser->lexer);
37272 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37273 "#pragma omp declare simd", pragma_tok);
37274 cp_parser_pop_lexer (parser);
37275 if (cl)
37276 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37277 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37278 TREE_CHAIN (c) = attrs;
37279 if (processing_template_decl)
37280 ATTR_IS_DEPENDENT (c) = 1;
37281 attrs = c;
37284 data->fndecl_seen = true;
37285 return attrs;
37289 /* OpenMP 4.0:
37290 # pragma omp declare target new-line
37291 declarations and definitions
37292 # pragma omp end declare target new-line
37294 OpenMP 4.5:
37295 # pragma omp declare target ( extended-list ) new-line
37297 # pragma omp declare target declare-target-clauses[seq] new-line */
37299 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37300 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37303 static void
37304 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37306 tree clauses = NULL_TREE;
37307 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37308 clauses
37309 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37310 "#pragma omp declare target", pragma_tok);
37311 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37313 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37314 clauses);
37315 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37316 cp_parser_require_pragma_eol (parser, pragma_tok);
37318 else
37320 cp_parser_require_pragma_eol (parser, pragma_tok);
37321 scope_chain->omp_declare_target_attribute++;
37322 return;
37324 if (scope_chain->omp_declare_target_attribute)
37325 error_at (pragma_tok->location,
37326 "%<#pragma omp declare target%> with clauses in between "
37327 "%<#pragma omp declare target%> without clauses and "
37328 "%<#pragma omp end declare target%>");
37329 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37331 tree t = OMP_CLAUSE_DECL (c), id;
37332 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37333 tree at2 = lookup_attribute ("omp declare target link",
37334 DECL_ATTRIBUTES (t));
37335 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37337 id = get_identifier ("omp declare target link");
37338 std::swap (at1, at2);
37340 else
37341 id = get_identifier ("omp declare target");
37342 if (at2)
37344 error_at (OMP_CLAUSE_LOCATION (c),
37345 "%qD specified both in declare target %<link%> and %<to%>"
37346 " clauses", t);
37347 continue;
37349 if (!at1)
37351 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37352 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37353 continue;
37355 symtab_node *node = symtab_node::get (t);
37356 if (node != NULL)
37358 node->offloadable = 1;
37359 if (ENABLE_OFFLOADING)
37361 g->have_offload = true;
37362 if (is_a <varpool_node *> (node))
37363 vec_safe_push (offload_vars, t);
37370 static void
37371 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37373 const char *p = "";
37374 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37376 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37377 p = IDENTIFIER_POINTER (id);
37379 if (strcmp (p, "declare") == 0)
37381 cp_lexer_consume_token (parser->lexer);
37382 p = "";
37383 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37385 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37386 p = IDENTIFIER_POINTER (id);
37388 if (strcmp (p, "target") == 0)
37389 cp_lexer_consume_token (parser->lexer);
37390 else
37392 cp_parser_error (parser, "expected %<target%>");
37393 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37394 return;
37397 else
37399 cp_parser_error (parser, "expected %<declare%>");
37400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37401 return;
37403 cp_parser_require_pragma_eol (parser, pragma_tok);
37404 if (!scope_chain->omp_declare_target_attribute)
37405 error_at (pragma_tok->location,
37406 "%<#pragma omp end declare target%> without corresponding "
37407 "%<#pragma omp declare target%>");
37408 else
37409 scope_chain->omp_declare_target_attribute--;
37412 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37413 expression and optional initializer clause of
37414 #pragma omp declare reduction. We store the expression(s) as
37415 either 3, 6 or 7 special statements inside of the artificial function's
37416 body. The first two statements are DECL_EXPRs for the artificial
37417 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37418 expression that uses those variables.
37419 If there was any INITIALIZER clause, this is followed by further statements,
37420 the fourth and fifth statements are DECL_EXPRs for the artificial
37421 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37422 constructor variant (first token after open paren is not omp_priv),
37423 then the sixth statement is a statement with the function call expression
37424 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37425 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37426 to initialize the OMP_PRIV artificial variable and there is seventh
37427 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37429 static bool
37430 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37432 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37433 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37434 type = TREE_TYPE (type);
37435 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37436 DECL_ARTIFICIAL (omp_out) = 1;
37437 pushdecl (omp_out);
37438 add_decl_expr (omp_out);
37439 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37440 DECL_ARTIFICIAL (omp_in) = 1;
37441 pushdecl (omp_in);
37442 add_decl_expr (omp_in);
37443 tree combiner;
37444 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37446 keep_next_level (true);
37447 tree block = begin_omp_structured_block ();
37448 combiner = cp_parser_expression (parser);
37449 finish_expr_stmt (combiner);
37450 block = finish_omp_structured_block (block);
37451 add_stmt (block);
37453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37454 return false;
37456 const char *p = "";
37457 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37459 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37460 p = IDENTIFIER_POINTER (id);
37463 if (strcmp (p, "initializer") == 0)
37465 cp_lexer_consume_token (parser->lexer);
37466 matching_parens parens;
37467 if (!parens.require_open (parser))
37468 return false;
37470 p = "";
37471 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37473 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37474 p = IDENTIFIER_POINTER (id);
37477 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37478 DECL_ARTIFICIAL (omp_priv) = 1;
37479 pushdecl (omp_priv);
37480 add_decl_expr (omp_priv);
37481 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37482 DECL_ARTIFICIAL (omp_orig) = 1;
37483 pushdecl (omp_orig);
37484 add_decl_expr (omp_orig);
37486 keep_next_level (true);
37487 block = begin_omp_structured_block ();
37489 bool ctor = false;
37490 if (strcmp (p, "omp_priv") == 0)
37492 bool is_direct_init, is_non_constant_init;
37493 ctor = true;
37494 cp_lexer_consume_token (parser->lexer);
37495 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37496 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37497 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37498 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37499 == CPP_CLOSE_PAREN
37500 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37501 == CPP_CLOSE_PAREN))
37503 finish_omp_structured_block (block);
37504 error ("invalid initializer clause");
37505 return false;
37507 initializer = cp_parser_initializer (parser, &is_direct_init,
37508 &is_non_constant_init);
37509 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37510 NULL_TREE, LOOKUP_ONLYCONVERTING);
37512 else
37514 cp_parser_parse_tentatively (parser);
37515 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37516 /*check_dependency_p=*/true,
37517 /*template_p=*/NULL,
37518 /*declarator_p=*/false,
37519 /*optional_p=*/false);
37520 vec<tree, va_gc> *args;
37521 if (fn_name == error_mark_node
37522 || cp_parser_error_occurred (parser)
37523 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37524 || ((args = cp_parser_parenthesized_expression_list
37525 (parser, non_attr, /*cast_p=*/false,
37526 /*allow_expansion_p=*/true,
37527 /*non_constant_p=*/NULL)),
37528 cp_parser_error_occurred (parser)))
37530 finish_omp_structured_block (block);
37531 cp_parser_abort_tentative_parse (parser);
37532 cp_parser_error (parser, "expected id-expression (arguments)");
37533 return false;
37535 unsigned int i;
37536 tree arg;
37537 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37538 if (arg == omp_priv
37539 || (TREE_CODE (arg) == ADDR_EXPR
37540 && TREE_OPERAND (arg, 0) == omp_priv))
37541 break;
37542 cp_parser_abort_tentative_parse (parser);
37543 if (arg == NULL_TREE)
37544 error ("one of the initializer call arguments should be %<omp_priv%>"
37545 " or %<&omp_priv%>");
37546 initializer = cp_parser_postfix_expression (parser, false, false, false,
37547 false, NULL);
37548 finish_expr_stmt (initializer);
37551 block = finish_omp_structured_block (block);
37552 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37553 add_stmt (block);
37555 if (ctor)
37556 add_decl_expr (omp_orig);
37558 if (!parens.require_close (parser))
37559 return false;
37562 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37563 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37564 UNKNOWN_LOCATION);
37566 return true;
37569 /* OpenMP 4.0
37570 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37571 initializer-clause[opt] new-line
37573 initializer-clause:
37574 initializer (omp_priv initializer)
37575 initializer (function-name (argument-list)) */
37577 static void
37578 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37579 enum pragma_context)
37581 auto_vec<tree> types;
37582 enum tree_code reduc_code = ERROR_MARK;
37583 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37584 unsigned int i;
37585 cp_token *first_token;
37586 cp_token_cache *cp;
37587 int errs;
37588 void *p;
37590 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37591 p = obstack_alloc (&declarator_obstack, 0);
37593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37594 goto fail;
37596 switch (cp_lexer_peek_token (parser->lexer)->type)
37598 case CPP_PLUS:
37599 reduc_code = PLUS_EXPR;
37600 break;
37601 case CPP_MULT:
37602 reduc_code = MULT_EXPR;
37603 break;
37604 case CPP_MINUS:
37605 reduc_code = MINUS_EXPR;
37606 break;
37607 case CPP_AND:
37608 reduc_code = BIT_AND_EXPR;
37609 break;
37610 case CPP_XOR:
37611 reduc_code = BIT_XOR_EXPR;
37612 break;
37613 case CPP_OR:
37614 reduc_code = BIT_IOR_EXPR;
37615 break;
37616 case CPP_AND_AND:
37617 reduc_code = TRUTH_ANDIF_EXPR;
37618 break;
37619 case CPP_OR_OR:
37620 reduc_code = TRUTH_ORIF_EXPR;
37621 break;
37622 case CPP_NAME:
37623 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37624 break;
37625 default:
37626 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37627 "%<|%>, %<&&%>, %<||%> or identifier");
37628 goto fail;
37631 if (reduc_code != ERROR_MARK)
37632 cp_lexer_consume_token (parser->lexer);
37634 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37635 if (reduc_id == error_mark_node)
37636 goto fail;
37638 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37639 goto fail;
37641 /* Types may not be defined in declare reduction type list. */
37642 const char *saved_message;
37643 saved_message = parser->type_definition_forbidden_message;
37644 parser->type_definition_forbidden_message
37645 = G_("types may not be defined in declare reduction type list");
37646 bool saved_colon_corrects_to_scope_p;
37647 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37648 parser->colon_corrects_to_scope_p = false;
37649 bool saved_colon_doesnt_start_class_def_p;
37650 saved_colon_doesnt_start_class_def_p
37651 = parser->colon_doesnt_start_class_def_p;
37652 parser->colon_doesnt_start_class_def_p = true;
37654 while (true)
37656 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37657 type = cp_parser_type_id (parser);
37658 if (type == error_mark_node)
37660 else if (ARITHMETIC_TYPE_P (type)
37661 && (orig_reduc_id == NULL_TREE
37662 || (TREE_CODE (type) != COMPLEX_TYPE
37663 && (id_equal (orig_reduc_id, "min")
37664 || id_equal (orig_reduc_id, "max")))))
37665 error_at (loc, "predeclared arithmetic type %qT in "
37666 "%<#pragma omp declare reduction%>", type);
37667 else if (TREE_CODE (type) == FUNCTION_TYPE
37668 || TREE_CODE (type) == METHOD_TYPE
37669 || TREE_CODE (type) == ARRAY_TYPE)
37670 error_at (loc, "function or array type %qT in "
37671 "%<#pragma omp declare reduction%>", type);
37672 else if (TREE_CODE (type) == REFERENCE_TYPE)
37673 error_at (loc, "reference type %qT in "
37674 "%<#pragma omp declare reduction%>", type);
37675 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37676 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37677 "%<#pragma omp declare reduction%>", type);
37678 else
37679 types.safe_push (type);
37681 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37682 cp_lexer_consume_token (parser->lexer);
37683 else
37684 break;
37687 /* Restore the saved message. */
37688 parser->type_definition_forbidden_message = saved_message;
37689 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37690 parser->colon_doesnt_start_class_def_p
37691 = saved_colon_doesnt_start_class_def_p;
37693 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37694 || types.is_empty ())
37696 fail:
37697 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37698 goto done;
37701 first_token = cp_lexer_peek_token (parser->lexer);
37702 cp = NULL;
37703 errs = errorcount;
37704 FOR_EACH_VEC_ELT (types, i, type)
37706 tree fntype
37707 = build_function_type_list (void_type_node,
37708 cp_build_reference_type (type, false),
37709 NULL_TREE);
37710 tree this_reduc_id = reduc_id;
37711 if (!dependent_type_p (type))
37712 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37713 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37714 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37715 DECL_ARTIFICIAL (fndecl) = 1;
37716 DECL_EXTERNAL (fndecl) = 1;
37717 DECL_DECLARED_INLINE_P (fndecl) = 1;
37718 DECL_IGNORED_P (fndecl) = 1;
37719 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37720 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37721 DECL_ATTRIBUTES (fndecl)
37722 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37723 DECL_ATTRIBUTES (fndecl));
37724 if (processing_template_decl)
37725 fndecl = push_template_decl (fndecl);
37726 bool block_scope = false;
37727 tree block = NULL_TREE;
37728 if (current_function_decl)
37730 block_scope = true;
37731 DECL_CONTEXT (fndecl) = global_namespace;
37732 if (!processing_template_decl)
37733 pushdecl (fndecl);
37735 else if (current_class_type)
37737 if (cp == NULL)
37739 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37740 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37741 cp_lexer_consume_token (parser->lexer);
37742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37743 goto fail;
37744 cp = cp_token_cache_new (first_token,
37745 cp_lexer_peek_nth_token (parser->lexer,
37746 2));
37748 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37749 finish_member_declaration (fndecl);
37750 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37751 DECL_PENDING_INLINE_P (fndecl) = 1;
37752 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37753 continue;
37755 else
37757 DECL_CONTEXT (fndecl) = current_namespace;
37758 pushdecl (fndecl);
37760 if (!block_scope)
37761 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37762 else
37763 block = begin_omp_structured_block ();
37764 if (cp)
37766 cp_parser_push_lexer_for_tokens (parser, cp);
37767 parser->lexer->in_pragma = true;
37769 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37771 if (!block_scope)
37772 finish_function (/*inline_p=*/false);
37773 else
37774 DECL_CONTEXT (fndecl) = current_function_decl;
37775 if (cp)
37776 cp_parser_pop_lexer (parser);
37777 goto fail;
37779 if (cp)
37780 cp_parser_pop_lexer (parser);
37781 if (!block_scope)
37782 finish_function (/*inline_p=*/false);
37783 else
37785 DECL_CONTEXT (fndecl) = current_function_decl;
37786 block = finish_omp_structured_block (block);
37787 if (TREE_CODE (block) == BIND_EXPR)
37788 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37789 else if (TREE_CODE (block) == STATEMENT_LIST)
37790 DECL_SAVED_TREE (fndecl) = block;
37791 if (processing_template_decl)
37792 add_decl_expr (fndecl);
37794 cp_check_omp_declare_reduction (fndecl);
37795 if (cp == NULL && types.length () > 1)
37796 cp = cp_token_cache_new (first_token,
37797 cp_lexer_peek_nth_token (parser->lexer, 2));
37798 if (errs != errorcount)
37799 break;
37802 cp_parser_require_pragma_eol (parser, pragma_tok);
37804 done:
37805 /* Free any declarators allocated. */
37806 obstack_free (&declarator_obstack, p);
37809 /* OpenMP 4.0
37810 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37811 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37812 initializer-clause[opt] new-line
37813 #pragma omp declare target new-line */
37815 static void
37816 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37817 enum pragma_context context)
37819 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37821 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37822 const char *p = IDENTIFIER_POINTER (id);
37824 if (strcmp (p, "simd") == 0)
37826 cp_lexer_consume_token (parser->lexer);
37827 cp_parser_omp_declare_simd (parser, pragma_tok,
37828 context);
37829 return;
37831 cp_ensure_no_omp_declare_simd (parser);
37832 if (strcmp (p, "reduction") == 0)
37834 cp_lexer_consume_token (parser->lexer);
37835 cp_parser_omp_declare_reduction (parser, pragma_tok,
37836 context);
37837 return;
37839 if (!flag_openmp) /* flag_openmp_simd */
37841 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37842 return;
37844 if (strcmp (p, "target") == 0)
37846 cp_lexer_consume_token (parser->lexer);
37847 cp_parser_omp_declare_target (parser, pragma_tok);
37848 return;
37851 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37852 "or %<target%>");
37853 cp_parser_require_pragma_eol (parser, pragma_tok);
37856 /* OpenMP 4.5:
37857 #pragma omp taskloop taskloop-clause[optseq] new-line
37858 for-loop
37860 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37861 for-loop */
37863 #define OMP_TASKLOOP_CLAUSE_MASK \
37864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37879 static tree
37880 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37881 char *p_name, omp_clause_mask mask, tree *cclauses,
37882 bool *if_p)
37884 tree clauses, sb, ret;
37885 unsigned int save;
37886 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37888 strcat (p_name, " taskloop");
37889 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37893 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37894 const char *p = IDENTIFIER_POINTER (id);
37896 if (strcmp (p, "simd") == 0)
37898 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37899 if (cclauses == NULL)
37900 cclauses = cclauses_buf;
37902 cp_lexer_consume_token (parser->lexer);
37903 if (!flag_openmp) /* flag_openmp_simd */
37904 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37905 cclauses, if_p);
37906 sb = begin_omp_structured_block ();
37907 save = cp_parser_begin_omp_structured_block (parser);
37908 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37909 cclauses, if_p);
37910 cp_parser_end_omp_structured_block (parser, save);
37911 tree body = finish_omp_structured_block (sb);
37912 if (ret == NULL)
37913 return ret;
37914 ret = make_node (OMP_TASKLOOP);
37915 TREE_TYPE (ret) = void_type_node;
37916 OMP_FOR_BODY (ret) = body;
37917 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37918 SET_EXPR_LOCATION (ret, loc);
37919 add_stmt (ret);
37920 return ret;
37923 if (!flag_openmp) /* flag_openmp_simd */
37925 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37926 return NULL_TREE;
37929 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37930 cclauses == NULL);
37931 if (cclauses)
37933 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37934 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37937 sb = begin_omp_structured_block ();
37938 save = cp_parser_begin_omp_structured_block (parser);
37940 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37941 if_p);
37943 cp_parser_end_omp_structured_block (parser, save);
37944 add_stmt (finish_omp_structured_block (sb));
37946 return ret;
37950 /* OpenACC 2.0:
37951 # pragma acc routine oacc-routine-clause[optseq] new-line
37952 function-definition
37954 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37957 #define OACC_ROUTINE_CLAUSE_MASK \
37958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37964 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37965 component, which must resolve to a declared namespace-scope
37966 function. The clauses are either processed directly (for a named
37967 function), or defered until the immediatley following declaration
37968 is parsed. */
37970 static void
37971 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37972 enum pragma_context context)
37974 gcc_checking_assert (context == pragma_external);
37975 /* The checking for "another pragma following this one" in the "no optional
37976 '( name )'" case makes sure that we dont re-enter. */
37977 gcc_checking_assert (parser->oacc_routine == NULL);
37979 cp_oacc_routine_data data;
37980 data.error_seen = false;
37981 data.fndecl_seen = false;
37982 data.tokens = vNULL;
37983 data.clauses = NULL_TREE;
37984 data.loc = pragma_tok->location;
37985 /* It is safe to take the address of a local variable; it will only be
37986 used while this scope is live. */
37987 parser->oacc_routine = &data;
37989 /* Look for optional '( name )'. */
37990 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37992 matching_parens parens;
37993 parens.consume_open (parser); /* '(' */
37995 /* We parse the name as an id-expression. If it resolves to
37996 anything other than a non-overloaded function at namespace
37997 scope, it's an error. */
37998 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37999 tree name = cp_parser_id_expression (parser,
38000 /*template_keyword_p=*/false,
38001 /*check_dependency_p=*/false,
38002 /*template_p=*/NULL,
38003 /*declarator_p=*/false,
38004 /*optional_p=*/false);
38005 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
38006 if (name != error_mark_node && decl == error_mark_node)
38007 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
38009 if (decl == error_mark_node
38010 || !parens.require_close (parser))
38012 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38013 parser->oacc_routine = NULL;
38014 return;
38017 data.clauses
38018 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38019 "#pragma acc routine",
38020 cp_lexer_peek_token (parser->lexer));
38022 if (decl && is_overloaded_fn (decl)
38023 && (TREE_CODE (decl) != FUNCTION_DECL
38024 || DECL_FUNCTION_TEMPLATE_P (decl)))
38026 error_at (name_loc,
38027 "%<#pragma acc routine%> names a set of overloads");
38028 parser->oacc_routine = NULL;
38029 return;
38032 /* Perhaps we should use the same rule as declarations in different
38033 namespaces? */
38034 if (!DECL_NAMESPACE_SCOPE_P (decl))
38036 error_at (name_loc,
38037 "%qD does not refer to a namespace scope function", decl);
38038 parser->oacc_routine = NULL;
38039 return;
38042 if (TREE_CODE (decl) != FUNCTION_DECL)
38044 error_at (name_loc, "%qD does not refer to a function", decl);
38045 parser->oacc_routine = NULL;
38046 return;
38049 cp_finalize_oacc_routine (parser, decl, false);
38050 parser->oacc_routine = NULL;
38052 else /* No optional '( name )'. */
38054 /* Store away all pragma tokens. */
38055 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38056 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
38057 cp_lexer_consume_token (parser->lexer);
38058 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
38059 parser->oacc_routine->error_seen = true;
38060 cp_parser_require_pragma_eol (parser, pragma_tok);
38061 struct cp_token_cache *cp
38062 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
38063 parser->oacc_routine->tokens.safe_push (cp);
38065 /* Emit a helpful diagnostic if there's another pragma following this
38066 one. */
38067 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
38069 cp_ensure_no_oacc_routine (parser);
38070 data.tokens.release ();
38071 /* ..., and then just keep going. */
38072 return;
38075 /* We only have to consider the pragma_external case here. */
38076 cp_parser_declaration (parser);
38077 if (parser->oacc_routine
38078 && !parser->oacc_routine->fndecl_seen)
38079 cp_ensure_no_oacc_routine (parser);
38080 else
38081 parser->oacc_routine = NULL;
38082 data.tokens.release ();
38086 /* Finalize #pragma acc routine clauses after direct declarator has
38087 been parsed. */
38089 static tree
38090 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
38092 struct cp_token_cache *ce;
38093 cp_oacc_routine_data *data = parser->oacc_routine;
38095 if (!data->error_seen && data->fndecl_seen)
38097 error_at (data->loc,
38098 "%<#pragma acc routine%> not immediately followed by "
38099 "a single function declaration or definition");
38100 data->error_seen = true;
38102 if (data->error_seen)
38103 return attrs;
38105 gcc_checking_assert (data->tokens.length () == 1);
38106 ce = data->tokens[0];
38108 cp_parser_push_lexer_for_tokens (parser, ce);
38109 parser->lexer->in_pragma = true;
38110 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
38112 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
38113 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
38114 parser->oacc_routine->clauses
38115 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38116 "#pragma acc routine", pragma_tok);
38117 cp_parser_pop_lexer (parser);
38118 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38119 fndecl_seen. */
38121 return attrs;
38124 /* Apply any saved OpenACC routine clauses to a just-parsed
38125 declaration. */
38127 static void
38128 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38130 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38132 /* Keep going if we're in error reporting mode. */
38133 if (parser->oacc_routine->error_seen
38134 || fndecl == error_mark_node)
38135 return;
38137 if (parser->oacc_routine->fndecl_seen)
38139 error_at (parser->oacc_routine->loc,
38140 "%<#pragma acc routine%> not immediately followed by"
38141 " a single function declaration or definition");
38142 parser->oacc_routine = NULL;
38143 return;
38145 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38147 cp_ensure_no_oacc_routine (parser);
38148 return;
38151 if (oacc_get_fn_attrib (fndecl))
38153 error_at (parser->oacc_routine->loc,
38154 "%<#pragma acc routine%> already applied to %qD", fndecl);
38155 parser->oacc_routine = NULL;
38156 return;
38159 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38161 error_at (parser->oacc_routine->loc,
38162 TREE_USED (fndecl)
38163 ? G_("%<#pragma acc routine%> must be applied before use")
38164 : G_("%<#pragma acc routine%> must be applied before "
38165 "definition"));
38166 parser->oacc_routine = NULL;
38167 return;
38170 /* Process the routine's dimension clauses. */
38171 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38172 oacc_replace_fn_attrib (fndecl, dims);
38174 /* Add an "omp declare target" attribute. */
38175 DECL_ATTRIBUTES (fndecl)
38176 = tree_cons (get_identifier ("omp declare target"),
38177 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38179 /* Don't unset parser->oacc_routine here: we may still need it to
38180 diagnose wrong usage. But, remember that we've used this "#pragma acc
38181 routine". */
38182 parser->oacc_routine->fndecl_seen = true;
38186 /* Main entry point to OpenMP statement pragmas. */
38188 static void
38189 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38191 tree stmt;
38192 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38193 omp_clause_mask mask (0);
38195 switch (cp_parser_pragma_kind (pragma_tok))
38197 case PRAGMA_OACC_ATOMIC:
38198 cp_parser_omp_atomic (parser, pragma_tok);
38199 return;
38200 case PRAGMA_OACC_CACHE:
38201 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38202 break;
38203 case PRAGMA_OACC_DATA:
38204 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38205 break;
38206 case PRAGMA_OACC_ENTER_DATA:
38207 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38208 break;
38209 case PRAGMA_OACC_EXIT_DATA:
38210 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38211 break;
38212 case PRAGMA_OACC_HOST_DATA:
38213 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38214 break;
38215 case PRAGMA_OACC_KERNELS:
38216 case PRAGMA_OACC_PARALLEL:
38217 strcpy (p_name, "#pragma acc");
38218 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38219 if_p);
38220 break;
38221 case PRAGMA_OACC_LOOP:
38222 strcpy (p_name, "#pragma acc");
38223 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38224 if_p);
38225 break;
38226 case PRAGMA_OACC_UPDATE:
38227 stmt = cp_parser_oacc_update (parser, pragma_tok);
38228 break;
38229 case PRAGMA_OACC_WAIT:
38230 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38231 break;
38232 case PRAGMA_OMP_ATOMIC:
38233 cp_parser_omp_atomic (parser, pragma_tok);
38234 return;
38235 case PRAGMA_OMP_CRITICAL:
38236 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38237 break;
38238 case PRAGMA_OMP_DISTRIBUTE:
38239 strcpy (p_name, "#pragma omp");
38240 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38241 if_p);
38242 break;
38243 case PRAGMA_OMP_FOR:
38244 strcpy (p_name, "#pragma omp");
38245 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38246 if_p);
38247 break;
38248 case PRAGMA_OMP_MASTER:
38249 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38250 break;
38251 case PRAGMA_OMP_PARALLEL:
38252 strcpy (p_name, "#pragma omp");
38253 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38254 if_p);
38255 break;
38256 case PRAGMA_OMP_SECTIONS:
38257 strcpy (p_name, "#pragma omp");
38258 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38259 break;
38260 case PRAGMA_OMP_SIMD:
38261 strcpy (p_name, "#pragma omp");
38262 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38263 if_p);
38264 break;
38265 case PRAGMA_OMP_SINGLE:
38266 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38267 break;
38268 case PRAGMA_OMP_TASK:
38269 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38270 break;
38271 case PRAGMA_OMP_TASKGROUP:
38272 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38273 break;
38274 case PRAGMA_OMP_TASKLOOP:
38275 strcpy (p_name, "#pragma omp");
38276 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38277 if_p);
38278 break;
38279 case PRAGMA_OMP_TEAMS:
38280 strcpy (p_name, "#pragma omp");
38281 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38282 if_p);
38283 break;
38284 default:
38285 gcc_unreachable ();
38288 protected_set_expr_location (stmt, pragma_tok->location);
38291 /* Transactional Memory parsing routines. */
38293 /* Parse a transaction attribute.
38295 txn-attribute:
38296 attribute
38297 [ [ identifier ] ]
38299 We use this instead of cp_parser_attributes_opt for transactions to avoid
38300 the pedwarn in C++98 mode. */
38302 static tree
38303 cp_parser_txn_attribute_opt (cp_parser *parser)
38305 cp_token *token;
38306 tree attr_name, attr = NULL;
38308 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38309 return cp_parser_attributes_opt (parser);
38311 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38312 return NULL_TREE;
38313 cp_lexer_consume_token (parser->lexer);
38314 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38315 goto error1;
38317 token = cp_lexer_peek_token (parser->lexer);
38318 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38320 token = cp_lexer_consume_token (parser->lexer);
38322 attr_name = (token->type == CPP_KEYWORD
38323 /* For keywords, use the canonical spelling,
38324 not the parsed identifier. */
38325 ? ridpointers[(int) token->keyword]
38326 : token->u.value);
38327 attr = build_tree_list (attr_name, NULL_TREE);
38329 else
38330 cp_parser_error (parser, "expected identifier");
38332 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38333 error1:
38334 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38335 return attr;
38338 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38340 transaction-statement:
38341 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38342 compound-statement
38343 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38346 static tree
38347 cp_parser_transaction (cp_parser *parser, cp_token *token)
38349 unsigned char old_in = parser->in_transaction;
38350 unsigned char this_in = 1, new_in;
38351 enum rid keyword = token->keyword;
38352 tree stmt, attrs, noex;
38354 cp_lexer_consume_token (parser->lexer);
38356 if (keyword == RID_TRANSACTION_RELAXED
38357 || keyword == RID_SYNCHRONIZED)
38358 this_in |= TM_STMT_ATTR_RELAXED;
38359 else
38361 attrs = cp_parser_txn_attribute_opt (parser);
38362 if (attrs)
38363 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38366 /* Parse a noexcept specification. */
38367 if (keyword == RID_ATOMIC_NOEXCEPT)
38368 noex = boolean_true_node;
38369 else if (keyword == RID_ATOMIC_CANCEL)
38371 /* cancel-and-throw is unimplemented. */
38372 sorry ("atomic_cancel");
38373 noex = NULL_TREE;
38375 else
38376 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38378 /* Keep track if we're in the lexical scope of an outer transaction. */
38379 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38381 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38383 parser->in_transaction = new_in;
38384 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38385 parser->in_transaction = old_in;
38387 finish_transaction_stmt (stmt, NULL, this_in, noex);
38389 return stmt;
38392 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38394 transaction-expression:
38395 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38396 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38399 static tree
38400 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38402 unsigned char old_in = parser->in_transaction;
38403 unsigned char this_in = 1;
38404 cp_token *token;
38405 tree expr, noex;
38406 bool noex_expr;
38407 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38409 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38410 || keyword == RID_TRANSACTION_RELAXED);
38412 if (!flag_tm)
38413 error_at (loc,
38414 keyword == RID_TRANSACTION_RELAXED
38415 ? G_("%<__transaction_relaxed%> without transactional memory "
38416 "support enabled")
38417 : G_("%<__transaction_atomic%> without transactional memory "
38418 "support enabled"));
38420 token = cp_parser_require_keyword (parser, keyword,
38421 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38422 : RT_TRANSACTION_RELAXED));
38423 gcc_assert (token != NULL);
38425 if (keyword == RID_TRANSACTION_RELAXED)
38426 this_in |= TM_STMT_ATTR_RELAXED;
38428 /* Set this early. This might mean that we allow transaction_cancel in
38429 an expression that we find out later actually has to be a constexpr.
38430 However, we expect that cxx_constant_value will be able to deal with
38431 this; also, if the noexcept has no constexpr, then what we parse next
38432 really is a transaction's body. */
38433 parser->in_transaction = this_in;
38435 /* Parse a noexcept specification. */
38436 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38437 true);
38439 if (!noex || !noex_expr
38440 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38442 matching_parens parens;
38443 parens.require_open (parser);
38445 expr = cp_parser_expression (parser);
38446 expr = finish_parenthesized_expr (expr);
38448 parens.require_close (parser);
38450 else
38452 /* The only expression that is available got parsed for the noexcept
38453 already. noexcept is true then. */
38454 expr = noex;
38455 noex = boolean_true_node;
38458 expr = build_transaction_expr (token->location, expr, this_in, noex);
38459 parser->in_transaction = old_in;
38461 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38462 return error_mark_node;
38464 return (flag_tm ? expr : error_mark_node);
38467 /* Parse a function-transaction-block.
38469 function-transaction-block:
38470 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38471 function-body
38472 __transaction_atomic txn-attribute[opt] function-try-block
38473 __transaction_relaxed ctor-initializer[opt] function-body
38474 __transaction_relaxed function-try-block
38477 static void
38478 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38480 unsigned char old_in = parser->in_transaction;
38481 unsigned char new_in = 1;
38482 tree compound_stmt, stmt, attrs;
38483 cp_token *token;
38485 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38486 || keyword == RID_TRANSACTION_RELAXED);
38487 token = cp_parser_require_keyword (parser, keyword,
38488 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38489 : RT_TRANSACTION_RELAXED));
38490 gcc_assert (token != NULL);
38492 if (keyword == RID_TRANSACTION_RELAXED)
38493 new_in |= TM_STMT_ATTR_RELAXED;
38494 else
38496 attrs = cp_parser_txn_attribute_opt (parser);
38497 if (attrs)
38498 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38501 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38503 parser->in_transaction = new_in;
38505 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38506 cp_parser_function_try_block (parser);
38507 else
38508 cp_parser_ctor_initializer_opt_and_function_body
38509 (parser, /*in_function_try_block=*/false);
38511 parser->in_transaction = old_in;
38513 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38516 /* Parse a __transaction_cancel statement.
38518 cancel-statement:
38519 __transaction_cancel txn-attribute[opt] ;
38520 __transaction_cancel txn-attribute[opt] throw-expression ;
38522 ??? Cancel and throw is not yet implemented. */
38524 static tree
38525 cp_parser_transaction_cancel (cp_parser *parser)
38527 cp_token *token;
38528 bool is_outer = false;
38529 tree stmt, attrs;
38531 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38532 RT_TRANSACTION_CANCEL);
38533 gcc_assert (token != NULL);
38535 attrs = cp_parser_txn_attribute_opt (parser);
38536 if (attrs)
38537 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38539 /* ??? Parse cancel-and-throw here. */
38541 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38543 if (!flag_tm)
38545 error_at (token->location, "%<__transaction_cancel%> without "
38546 "transactional memory support enabled");
38547 return error_mark_node;
38549 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38551 error_at (token->location, "%<__transaction_cancel%> within a "
38552 "%<__transaction_relaxed%>");
38553 return error_mark_node;
38555 else if (is_outer)
38557 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38558 && !is_tm_may_cancel_outer (current_function_decl))
38560 error_at (token->location, "outer %<__transaction_cancel%> not "
38561 "within outer %<__transaction_atomic%>");
38562 error_at (token->location,
38563 " or a %<transaction_may_cancel_outer%> function");
38564 return error_mark_node;
38567 else if (parser->in_transaction == 0)
38569 error_at (token->location, "%<__transaction_cancel%> not within "
38570 "%<__transaction_atomic%>");
38571 return error_mark_node;
38574 stmt = build_tm_abort_call (token->location, is_outer);
38575 add_stmt (stmt);
38577 return stmt;
38580 /* The parser. */
38582 static GTY (()) cp_parser *the_parser;
38585 /* Special handling for the first token or line in the file. The first
38586 thing in the file might be #pragma GCC pch_preprocess, which loads a
38587 PCH file, which is a GC collection point. So we need to handle this
38588 first pragma without benefit of an existing lexer structure.
38590 Always returns one token to the caller in *FIRST_TOKEN. This is
38591 either the true first token of the file, or the first token after
38592 the initial pragma. */
38594 static void
38595 cp_parser_initial_pragma (cp_token *first_token)
38597 tree name = NULL;
38599 cp_lexer_get_preprocessor_token (NULL, first_token);
38600 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38601 return;
38603 cp_lexer_get_preprocessor_token (NULL, first_token);
38604 if (first_token->type == CPP_STRING)
38606 name = first_token->u.value;
38608 cp_lexer_get_preprocessor_token (NULL, first_token);
38609 if (first_token->type != CPP_PRAGMA_EOL)
38610 error_at (first_token->location,
38611 "junk at end of %<#pragma GCC pch_preprocess%>");
38613 else
38614 error_at (first_token->location, "expected string literal");
38616 /* Skip to the end of the pragma. */
38617 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38618 cp_lexer_get_preprocessor_token (NULL, first_token);
38620 /* Now actually load the PCH file. */
38621 if (name)
38622 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38624 /* Read one more token to return to our caller. We have to do this
38625 after reading the PCH file in, since its pointers have to be
38626 live. */
38627 cp_lexer_get_preprocessor_token (NULL, first_token);
38630 /* Parses the grainsize pragma for the _Cilk_for statement.
38631 Syntax:
38632 #pragma cilk grainsize = <VALUE>. */
38634 static void
38635 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38637 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38639 tree exp = cp_parser_binary_expression (parser, false, false,
38640 PREC_NOT_OPERATOR, NULL);
38641 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38642 if (!exp || exp == error_mark_node)
38644 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38645 return;
38648 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38649 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38650 cp_parser_cilk_for (parser, exp, if_p);
38651 else
38652 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38653 "%<#pragma cilk grainsize%> is not followed by "
38654 "%<_Cilk_for%>");
38655 return;
38657 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38660 /* Normal parsing of a pragma token. Here we can (and must) use the
38661 regular lexer. */
38663 static bool
38664 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38666 cp_token *pragma_tok;
38667 unsigned int id;
38668 tree stmt;
38669 bool ret;
38671 pragma_tok = cp_lexer_consume_token (parser->lexer);
38672 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38673 parser->lexer->in_pragma = true;
38675 id = cp_parser_pragma_kind (pragma_tok);
38676 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38677 cp_ensure_no_omp_declare_simd (parser);
38678 switch (id)
38680 case PRAGMA_GCC_PCH_PREPROCESS:
38681 error_at (pragma_tok->location,
38682 "%<#pragma GCC pch_preprocess%> must be first");
38683 break;
38685 case PRAGMA_OMP_BARRIER:
38686 switch (context)
38688 case pragma_compound:
38689 cp_parser_omp_barrier (parser, pragma_tok);
38690 return false;
38691 case pragma_stmt:
38692 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38693 "used in compound statements", "omp barrier");
38694 break;
38695 default:
38696 goto bad_stmt;
38698 break;
38700 case PRAGMA_OMP_FLUSH:
38701 switch (context)
38703 case pragma_compound:
38704 cp_parser_omp_flush (parser, pragma_tok);
38705 return false;
38706 case pragma_stmt:
38707 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38708 "used in compound statements", "omp flush");
38709 break;
38710 default:
38711 goto bad_stmt;
38713 break;
38715 case PRAGMA_OMP_TASKWAIT:
38716 switch (context)
38718 case pragma_compound:
38719 cp_parser_omp_taskwait (parser, pragma_tok);
38720 return false;
38721 case pragma_stmt:
38722 error_at (pragma_tok->location,
38723 "%<#pragma %s%> may only be used in compound statements",
38724 "omp taskwait");
38725 break;
38726 default:
38727 goto bad_stmt;
38729 break;
38731 case PRAGMA_OMP_TASKYIELD:
38732 switch (context)
38734 case pragma_compound:
38735 cp_parser_omp_taskyield (parser, pragma_tok);
38736 return false;
38737 case pragma_stmt:
38738 error_at (pragma_tok->location,
38739 "%<#pragma %s%> may only be used in compound statements",
38740 "omp taskyield");
38741 break;
38742 default:
38743 goto bad_stmt;
38745 break;
38747 case PRAGMA_OMP_CANCEL:
38748 switch (context)
38750 case pragma_compound:
38751 cp_parser_omp_cancel (parser, pragma_tok);
38752 return false;
38753 case pragma_stmt:
38754 error_at (pragma_tok->location,
38755 "%<#pragma %s%> may only be used in compound statements",
38756 "omp cancel");
38757 break;
38758 default:
38759 goto bad_stmt;
38761 break;
38763 case PRAGMA_OMP_CANCELLATION_POINT:
38764 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38765 return false;
38767 case PRAGMA_OMP_THREADPRIVATE:
38768 cp_parser_omp_threadprivate (parser, pragma_tok);
38769 return false;
38771 case PRAGMA_OMP_DECLARE:
38772 cp_parser_omp_declare (parser, pragma_tok, context);
38773 return false;
38775 case PRAGMA_OACC_DECLARE:
38776 cp_parser_oacc_declare (parser, pragma_tok);
38777 return false;
38779 case PRAGMA_OACC_ENTER_DATA:
38780 if (context == pragma_stmt)
38782 error_at (pragma_tok->location,
38783 "%<#pragma %s%> may only be used in compound statements",
38784 "acc enter data");
38785 break;
38787 else if (context != pragma_compound)
38788 goto bad_stmt;
38789 cp_parser_omp_construct (parser, pragma_tok, if_p);
38790 return true;
38792 case PRAGMA_OACC_EXIT_DATA:
38793 if (context == pragma_stmt)
38795 error_at (pragma_tok->location,
38796 "%<#pragma %s%> may only be used in compound statements",
38797 "acc exit data");
38798 break;
38800 else if (context != pragma_compound)
38801 goto bad_stmt;
38802 cp_parser_omp_construct (parser, pragma_tok, if_p);
38803 return true;
38805 case PRAGMA_OACC_ROUTINE:
38806 if (context != pragma_external)
38808 error_at (pragma_tok->location,
38809 "%<#pragma acc routine%> must be at file scope");
38810 break;
38812 cp_parser_oacc_routine (parser, pragma_tok, context);
38813 return false;
38815 case PRAGMA_OACC_UPDATE:
38816 if (context == pragma_stmt)
38818 error_at (pragma_tok->location,
38819 "%<#pragma %s%> may only be used in compound statements",
38820 "acc update");
38821 break;
38823 else if (context != pragma_compound)
38824 goto bad_stmt;
38825 cp_parser_omp_construct (parser, pragma_tok, if_p);
38826 return true;
38828 case PRAGMA_OACC_WAIT:
38829 if (context == pragma_stmt)
38831 error_at (pragma_tok->location,
38832 "%<#pragma %s%> may only be used in compound statements",
38833 "acc wait");
38834 break;
38836 else if (context != pragma_compound)
38837 goto bad_stmt;
38838 cp_parser_omp_construct (parser, pragma_tok, if_p);
38839 return true;
38841 case PRAGMA_OACC_ATOMIC:
38842 case PRAGMA_OACC_CACHE:
38843 case PRAGMA_OACC_DATA:
38844 case PRAGMA_OACC_HOST_DATA:
38845 case PRAGMA_OACC_KERNELS:
38846 case PRAGMA_OACC_PARALLEL:
38847 case PRAGMA_OACC_LOOP:
38848 case PRAGMA_OMP_ATOMIC:
38849 case PRAGMA_OMP_CRITICAL:
38850 case PRAGMA_OMP_DISTRIBUTE:
38851 case PRAGMA_OMP_FOR:
38852 case PRAGMA_OMP_MASTER:
38853 case PRAGMA_OMP_PARALLEL:
38854 case PRAGMA_OMP_SECTIONS:
38855 case PRAGMA_OMP_SIMD:
38856 case PRAGMA_OMP_SINGLE:
38857 case PRAGMA_OMP_TASK:
38858 case PRAGMA_OMP_TASKGROUP:
38859 case PRAGMA_OMP_TASKLOOP:
38860 case PRAGMA_OMP_TEAMS:
38861 if (context != pragma_stmt && context != pragma_compound)
38862 goto bad_stmt;
38863 stmt = push_omp_privatization_clauses (false);
38864 cp_parser_omp_construct (parser, pragma_tok, if_p);
38865 pop_omp_privatization_clauses (stmt);
38866 return true;
38868 case PRAGMA_OMP_ORDERED:
38869 if (context != pragma_stmt && context != pragma_compound)
38870 goto bad_stmt;
38871 stmt = push_omp_privatization_clauses (false);
38872 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38873 pop_omp_privatization_clauses (stmt);
38874 return ret;
38876 case PRAGMA_OMP_TARGET:
38877 if (context != pragma_stmt && context != pragma_compound)
38878 goto bad_stmt;
38879 stmt = push_omp_privatization_clauses (false);
38880 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38881 pop_omp_privatization_clauses (stmt);
38882 return ret;
38884 case PRAGMA_OMP_END_DECLARE_TARGET:
38885 cp_parser_omp_end_declare_target (parser, pragma_tok);
38886 return false;
38888 case PRAGMA_OMP_SECTION:
38889 error_at (pragma_tok->location,
38890 "%<#pragma omp section%> may only be used in "
38891 "%<#pragma omp sections%> construct");
38892 break;
38894 case PRAGMA_IVDEP:
38896 if (context == pragma_external)
38898 error_at (pragma_tok->location,
38899 "%<#pragma GCC ivdep%> must be inside a function");
38900 break;
38902 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38903 cp_token *tok;
38904 tok = cp_lexer_peek_token (the_parser->lexer);
38905 if (tok->type != CPP_KEYWORD
38906 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38907 && tok->keyword != RID_DO))
38909 cp_parser_error (parser, "for, while or do statement expected");
38910 return false;
38912 cp_parser_iteration_statement (parser, if_p, true);
38913 return true;
38916 case PRAGMA_CILK_SIMD:
38917 if (context == pragma_external)
38919 error_at (pragma_tok->location,
38920 "%<#pragma simd%> must be inside a function");
38921 break;
38923 stmt = push_omp_privatization_clauses (false);
38924 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38925 pop_omp_privatization_clauses (stmt);
38926 return true;
38928 case PRAGMA_CILK_GRAINSIZE:
38929 if (context == pragma_external)
38931 error_at (pragma_tok->location,
38932 "%<#pragma cilk grainsize%> must be inside a function");
38933 break;
38936 /* Ignore the pragma if Cilk Plus is not enabled. */
38937 if (flag_cilkplus)
38939 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38940 return true;
38942 else
38944 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38945 "%<#pragma cilk grainsize%>");
38946 break;
38949 default:
38950 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38951 c_invoke_pragma_handler (id);
38952 break;
38954 bad_stmt:
38955 cp_parser_error (parser, "expected declaration specifiers");
38956 break;
38959 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38960 return false;
38963 /* The interface the pragma parsers have to the lexer. */
38965 enum cpp_ttype
38966 pragma_lex (tree *value, location_t *loc)
38968 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38969 enum cpp_ttype ret = tok->type;
38971 *value = tok->u.value;
38972 if (loc)
38973 *loc = tok->location;
38975 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38976 ret = CPP_EOF;
38977 else if (ret == CPP_STRING)
38978 *value = cp_parser_string_literal (the_parser, false, false);
38979 else
38981 if (ret == CPP_KEYWORD)
38982 ret = CPP_NAME;
38983 cp_lexer_consume_token (the_parser->lexer);
38986 return ret;
38990 /* External interface. */
38992 /* Parse one entire translation unit. */
38994 void
38995 c_parse_file (void)
38997 static bool already_called = false;
38999 if (already_called)
39000 fatal_error (input_location,
39001 "inter-module optimizations not implemented for C++");
39002 already_called = true;
39004 the_parser = cp_parser_new ();
39005 push_deferring_access_checks (flag_access_control
39006 ? dk_no_deferred : dk_no_check);
39007 cp_parser_translation_unit (the_parser);
39008 the_parser = NULL;
39011 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
39012 vectorlength clause:
39013 Syntax:
39014 vectorlength ( constant-expression ) */
39016 static tree
39017 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
39018 bool is_simd_fn)
39020 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39021 tree expr;
39022 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
39023 safelen clause. Thus, vectorlength is represented as OMP 4.0
39024 safelen. For SIMD-enabled function it is represented by OMP 4.0
39025 simdlen. */
39026 if (!is_simd_fn)
39027 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
39028 loc);
39029 else
39030 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
39031 loc);
39033 matching_parens parens;
39034 if (!parens.require_open (parser))
39035 return error_mark_node;
39037 expr = cp_parser_constant_expression (parser);
39038 expr = maybe_constant_value (expr);
39040 /* If expr == error_mark_node, then don't emit any errors nor
39041 create a clause. if any of the above functions returns
39042 error mark node then they would have emitted an error message. */
39043 if (expr == error_mark_node)
39045 else if (!TREE_TYPE (expr)
39046 || !TREE_CONSTANT (expr)
39047 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
39048 error_at (loc, "vectorlength must be an integer constant");
39049 else if (TREE_CONSTANT (expr)
39050 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
39051 error_at (loc, "vectorlength must be a power of 2");
39052 else
39054 tree c;
39055 if (!is_simd_fn)
39057 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
39058 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
39059 OMP_CLAUSE_CHAIN (c) = clauses;
39060 clauses = c;
39062 else
39064 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
39065 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
39066 OMP_CLAUSE_CHAIN (c) = clauses;
39067 clauses = c;
39071 if (!parens.require_close (parser))
39072 return error_mark_node;
39073 return clauses;
39076 /* Handles the Cilk Plus #pragma simd linear clause.
39077 Syntax:
39078 linear ( simd-linear-variable-list )
39080 simd-linear-variable-list:
39081 simd-linear-variable
39082 simd-linear-variable-list , simd-linear-variable
39084 simd-linear-variable:
39085 id-expression
39086 id-expression : simd-linear-step
39088 simd-linear-step:
39089 conditional-expression */
39091 static tree
39092 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
39094 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39096 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39097 return clauses;
39098 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39100 cp_parser_error (parser, "expected identifier");
39101 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39102 return error_mark_node;
39105 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
39106 parser->colon_corrects_to_scope_p = false;
39107 while (1)
39109 cp_token *token = cp_lexer_peek_token (parser->lexer);
39110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39112 cp_parser_error (parser, "expected variable-name");
39113 clauses = error_mark_node;
39114 break;
39117 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
39118 false, false);
39119 tree decl = cp_parser_lookup_name_simple (parser, var_name,
39120 token->location);
39121 if (decl == error_mark_node)
39123 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
39124 token->location);
39125 clauses = error_mark_node;
39127 else
39129 tree e = NULL_TREE;
39130 tree step_size = integer_one_node;
39132 /* If present, parse the linear step. Otherwise, assume the default
39133 value of 1. */
39134 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
39136 cp_lexer_consume_token (parser->lexer);
39138 e = cp_parser_assignment_expression (parser);
39139 e = maybe_constant_value (e);
39141 if (e == error_mark_node)
39143 /* If an error has occurred, then the whole pragma is
39144 considered ill-formed. Thus, no reason to keep
39145 parsing. */
39146 clauses = error_mark_node;
39147 break;
39149 else if (type_dependent_expression_p (e)
39150 || value_dependent_expression_p (e)
39151 || (TREE_TYPE (e)
39152 && INTEGRAL_TYPE_P (TREE_TYPE (e))
39153 && (TREE_CONSTANT (e)
39154 || DECL_P (e))))
39155 step_size = e;
39156 else
39157 cp_parser_error (parser,
39158 "step size must be an integer constant "
39159 "expression or an integer variable");
39162 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39163 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39164 OMP_CLAUSE_DECL (l) = decl;
39165 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
39166 OMP_CLAUSE_CHAIN (l) = clauses;
39167 clauses = l;
39169 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39170 cp_lexer_consume_token (parser->lexer);
39171 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39172 break;
39173 else
39175 error_at (cp_lexer_peek_token (parser->lexer)->location,
39176 "expected %<,%> or %<)%> after %qE", decl);
39177 clauses = error_mark_node;
39178 break;
39181 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39182 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39183 return clauses;
39186 /* Returns the name of the next clause. If the clause is not
39187 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39188 token is not consumed. Otherwise, the appropriate enum from the
39189 pragma_simd_clause is returned and the token is consumed. */
39191 static pragma_omp_clause
39192 cp_parser_cilk_simd_clause_name (cp_parser *parser)
39194 pragma_omp_clause clause_type;
39195 cp_token *token = cp_lexer_peek_token (parser->lexer);
39197 if (token->keyword == RID_PRIVATE)
39198 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
39199 else if (!token->u.value || token->type != CPP_NAME)
39200 return PRAGMA_CILK_CLAUSE_NONE;
39201 else if (id_equal (token->u.value, "vectorlength"))
39202 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
39203 else if (id_equal (token->u.value, "linear"))
39204 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
39205 else if (id_equal (token->u.value, "firstprivate"))
39206 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
39207 else if (id_equal (token->u.value, "lastprivate"))
39208 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39209 else if (id_equal (token->u.value, "reduction"))
39210 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39211 else
39212 return PRAGMA_CILK_CLAUSE_NONE;
39214 cp_lexer_consume_token (parser->lexer);
39215 return clause_type;
39218 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39220 static tree
39221 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39223 tree clauses = NULL_TREE;
39225 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39226 && clauses != error_mark_node)
39228 pragma_omp_clause c_kind;
39229 c_kind = cp_parser_cilk_simd_clause_name (parser);
39230 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39231 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39232 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39233 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39234 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39235 /* Use the OpenMP 4.0 equivalent function. */
39236 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39237 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39238 /* Use the OpenMP 4.0 equivalent function. */
39239 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39240 clauses);
39241 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39242 /* Use the OMP 4.0 equivalent function. */
39243 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39244 clauses);
39245 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39246 /* Use the OMP 4.0 equivalent function. */
39247 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39248 else
39250 clauses = error_mark_node;
39251 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39252 break;
39256 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39258 if (clauses == error_mark_node)
39259 return error_mark_node;
39260 else
39261 return finish_omp_clauses (clauses, C_ORT_CILK);
39264 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39266 static void
39267 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39269 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39271 if (clauses == error_mark_node)
39272 return;
39274 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39276 error_at (cp_lexer_peek_token (parser->lexer)->location,
39277 "for statement expected");
39278 return;
39281 tree sb = begin_omp_structured_block ();
39282 int save = cp_parser_begin_omp_structured_block (parser);
39283 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39284 if (ret)
39285 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39286 cp_parser_end_omp_structured_block (parser, save);
39287 add_stmt (finish_omp_structured_block (sb));
39290 /* Main entry-point for parsing Cilk Plus _Cilk_for
39291 loops. The return value is error_mark_node
39292 when errors happen and CILK_FOR tree on success. */
39294 static tree
39295 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39297 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39298 gcc_unreachable ();
39300 tree sb = begin_omp_structured_block ();
39301 int save = cp_parser_begin_omp_structured_block (parser);
39303 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39304 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39305 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39306 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39308 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39309 if (ret)
39310 cpp_validate_cilk_plus_loop (ret);
39311 else
39312 ret = error_mark_node;
39314 cp_parser_end_omp_structured_block (parser, save);
39315 add_stmt (finish_omp_structured_block (sb));
39316 return ret;
39319 /* Create an identifier for a generic parameter type (a synthesized
39320 template parameter implied by `auto' or a concept identifier). */
39322 static GTY(()) int generic_parm_count;
39323 static tree
39324 make_generic_type_name ()
39326 char buf[32];
39327 sprintf (buf, "auto:%d", ++generic_parm_count);
39328 return get_identifier (buf);
39331 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39332 (creating a new template parameter list if necessary). Returns the newly
39333 created template type parm. */
39335 static tree
39336 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39338 gcc_assert (current_binding_level->kind == sk_function_parms);
39340 /* Before committing to modifying any scope, if we're in an
39341 implicit template scope, and we're trying to synthesize a
39342 constrained parameter, try to find a previous parameter with
39343 the same name. This is the same-type rule for abbreviated
39344 function templates.
39346 NOTE: We can generate implicit parameters when tentatively
39347 parsing a nested name specifier, only to reject that parse
39348 later. However, matching the same template-id as part of a
39349 direct-declarator should generate an identical template
39350 parameter, so this rule will merge them. */
39351 if (parser->implicit_template_scope && constr)
39353 tree t = parser->implicit_template_parms;
39354 while (t)
39356 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39358 tree d = TREE_VALUE (t);
39359 if (TREE_CODE (d) == PARM_DECL)
39360 /* Return the TEMPLATE_PARM_INDEX. */
39361 d = DECL_INITIAL (d);
39362 return d;
39364 t = TREE_CHAIN (t);
39368 /* We are either continuing a function template that already contains implicit
39369 template parameters, creating a new fully-implicit function template, or
39370 extending an existing explicit function template with implicit template
39371 parameters. */
39373 cp_binding_level *const entry_scope = current_binding_level;
39375 bool become_template = false;
39376 cp_binding_level *parent_scope = 0;
39378 if (parser->implicit_template_scope)
39380 gcc_assert (parser->implicit_template_parms);
39382 current_binding_level = parser->implicit_template_scope;
39384 else
39386 /* Roll back to the existing template parameter scope (in the case of
39387 extending an explicit function template) or introduce a new template
39388 parameter scope ahead of the function parameter scope (or class scope
39389 in the case of out-of-line member definitions). The function scope is
39390 added back after template parameter synthesis below. */
39392 cp_binding_level *scope = entry_scope;
39394 while (scope->kind == sk_function_parms)
39396 parent_scope = scope;
39397 scope = scope->level_chain;
39399 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39401 /* If not defining a class, then any class scope is a scope level in
39402 an out-of-line member definition. In this case simply wind back
39403 beyond the first such scope to inject the template parameter list.
39404 Otherwise wind back to the class being defined. The latter can
39405 occur in class member friend declarations such as:
39407 class A {
39408 void foo (auto);
39410 class B {
39411 friend void A::foo (auto);
39414 The template parameter list synthesized for the friend declaration
39415 must be injected in the scope of 'B'. This can also occur in
39416 erroneous cases such as:
39418 struct A {
39419 struct B {
39420 void foo (auto);
39422 void B::foo (auto) {}
39425 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39426 but, nevertheless, the template parameter list synthesized for the
39427 declarator should be injected into the scope of 'A' as if the
39428 ill-formed template was specified explicitly. */
39430 while (scope->kind == sk_class && !scope->defining_class_p)
39432 parent_scope = scope;
39433 scope = scope->level_chain;
39437 current_binding_level = scope;
39439 if (scope->kind != sk_template_parms
39440 || !function_being_declared_is_template_p (parser))
39442 /* Introduce a new template parameter list for implicit template
39443 parameters. */
39445 become_template = true;
39447 parser->implicit_template_scope
39448 = begin_scope (sk_template_parms, NULL);
39450 ++processing_template_decl;
39452 parser->fully_implicit_function_template_p = true;
39453 ++parser->num_template_parameter_lists;
39455 else
39457 /* Synthesize implicit template parameters at the end of the explicit
39458 template parameter list. */
39460 gcc_assert (current_template_parms);
39462 parser->implicit_template_scope = scope;
39464 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39465 parser->implicit_template_parms
39466 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39470 /* Synthesize a new template parameter and track the current template
39471 parameter chain with implicit_template_parms. */
39473 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39474 tree synth_id = make_generic_type_name ();
39475 tree synth_tmpl_parm;
39476 bool non_type = false;
39478 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39479 synth_tmpl_parm
39480 = finish_template_type_parm (class_type_node, synth_id);
39481 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39482 synth_tmpl_parm
39483 = finish_constrained_template_template_parm (proto, synth_id);
39484 else
39486 synth_tmpl_parm = copy_decl (proto);
39487 DECL_NAME (synth_tmpl_parm) = synth_id;
39488 non_type = true;
39491 // Attach the constraint to the parm before processing.
39492 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39493 TREE_TYPE (node) = constr;
39494 tree new_parm
39495 = process_template_parm (parser->implicit_template_parms,
39496 input_location,
39497 node,
39498 /*non_type=*/non_type,
39499 /*param_pack=*/false);
39501 // Chain the new parameter to the list of implicit parameters.
39502 if (parser->implicit_template_parms)
39503 parser->implicit_template_parms
39504 = TREE_CHAIN (parser->implicit_template_parms);
39505 else
39506 parser->implicit_template_parms = new_parm;
39508 tree new_decl = get_local_decls ();
39509 if (non_type)
39510 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39511 new_decl = DECL_INITIAL (new_decl);
39513 /* If creating a fully implicit function template, start the new implicit
39514 template parameter list with this synthesized type, otherwise grow the
39515 current template parameter list. */
39517 if (become_template)
39519 parent_scope->level_chain = current_binding_level;
39521 tree new_parms = make_tree_vec (1);
39522 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39523 current_template_parms = tree_cons (size_int (processing_template_decl),
39524 new_parms, current_template_parms);
39526 else
39528 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39529 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39530 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39531 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39534 // If the new parameter was constrained, we need to add that to the
39535 // constraints in the template parameter list.
39536 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39538 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39539 reqs = conjoin_constraints (reqs, req);
39540 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39543 current_binding_level = entry_scope;
39545 return new_decl;
39548 /* Finish the declaration of a fully implicit function template. Such a
39549 template has no explicit template parameter list so has not been through the
39550 normal template head and tail processing. synthesize_implicit_template_parm
39551 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39552 provided if the declaration is a class member such that its template
39553 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39554 form is returned. Otherwise NULL_TREE is returned. */
39556 static tree
39557 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39559 gcc_assert (parser->fully_implicit_function_template_p);
39561 if (member_decl_opt && member_decl_opt != error_mark_node
39562 && DECL_VIRTUAL_P (member_decl_opt))
39564 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39565 "implicit templates may not be %<virtual%>");
39566 DECL_VIRTUAL_P (member_decl_opt) = false;
39569 if (member_decl_opt)
39570 member_decl_opt = finish_member_template_decl (member_decl_opt);
39571 end_template_decl ();
39573 parser->fully_implicit_function_template_p = false;
39574 --parser->num_template_parameter_lists;
39576 return member_decl_opt;
39579 /* Helper function for diagnostics that have complained about things
39580 being used with 'extern "C"' linkage.
39582 Attempt to issue a note showing where the 'extern "C"' linkage began. */
39584 void
39585 maybe_show_extern_c_location (void)
39587 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
39588 inform (the_parser->innermost_linkage_specification_location,
39589 "%<extern \"C\"%> linkage started here");
39592 #include "gt-cp-parser.h"