c++: trivial formatting cleanups
[official-gcc.git] / gcc / cp / parser.cc
blob10d303884fbacc43d7fc8d6ffa3723821b41a945
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2022 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 #define INCLUDE_MEMORY
23 #include "system.h"
24 #include "coretypes.h"
25 #include "cp-tree.h"
26 #include "c-family/c-common.h"
27 #include "timevar.h"
28 #include "stringpool.h"
29 #include "cgraph.h"
30 #include "print-tree.h"
31 #include "attribs.h"
32 #include "trans-mem.h"
33 #include "intl.h"
34 #include "decl.h"
35 #include "c-family/c-objc.h"
36 #include "plugin.h"
37 #include "tree-pretty-print.h"
38 #include "parser.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
43 #include "context.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
47 #include "memmodel.h"
48 #include "c-family/known-headers.h"
49 #include "bitmap.h"
52 /* The lexer. */
54 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
55 and c-lex.cc) and the C++ parser. */
57 /* The various kinds of non integral constant we encounter. */
58 enum non_integral_constant {
59 NIC_NONE,
60 /* floating-point literal */
61 NIC_FLOAT,
62 /* %<this%> */
63 NIC_THIS,
64 /* %<__FUNCTION__%> */
65 NIC_FUNC_NAME,
66 /* %<__PRETTY_FUNCTION__%> */
67 NIC_PRETTY_FUNC,
68 /* %<__func__%> */
69 NIC_C99_FUNC,
70 /* "%<va_arg%> */
71 NIC_VA_ARG,
72 /* a cast */
73 NIC_CAST,
74 /* %<typeid%> operator */
75 NIC_TYPEID,
76 /* non-constant compound literals */
77 NIC_NCC,
78 /* a function call */
79 NIC_FUNC_CALL,
80 /* an increment */
81 NIC_INC,
82 /* an decrement */
83 NIC_DEC,
84 /* an array reference */
85 NIC_ARRAY_REF,
86 /* %<->%> */
87 NIC_ARROW,
88 /* %<.%> */
89 NIC_POINT,
90 /* the address of a label */
91 NIC_ADDR_LABEL,
92 /* %<*%> */
93 NIC_STAR,
94 /* %<&%> */
95 NIC_ADDR,
96 /* %<++%> */
97 NIC_PREINCREMENT,
98 /* %<--%> */
99 NIC_PREDECREMENT,
100 /* %<new%> */
101 NIC_NEW,
102 /* %<delete%> */
103 NIC_DEL,
104 /* calls to overloaded operators */
105 NIC_OVERLOADED,
106 /* an assignment */
107 NIC_ASSIGNMENT,
108 /* a comma operator */
109 NIC_COMMA,
110 /* a call to a constructor */
111 NIC_CONSTRUCTOR,
112 /* a transaction expression */
113 NIC_TRANSACTION
116 /* The various kinds of errors about name-lookup failing. */
117 enum name_lookup_error {
118 /* NULL */
119 NLE_NULL,
120 /* is not a type */
121 NLE_TYPE,
122 /* is not a class or namespace */
123 NLE_CXX98,
124 /* is not a class, namespace, or enumeration */
125 NLE_NOT_CXX98
128 /* The various kinds of required token */
129 enum required_token {
130 RT_NONE,
131 RT_SEMICOLON, /* ';' */
132 RT_OPEN_PAREN, /* '(' */
133 RT_CLOSE_BRACE, /* '}' */
134 RT_OPEN_BRACE, /* '{' */
135 RT_CLOSE_SQUARE, /* ']' */
136 RT_OPEN_SQUARE, /* '[' */
137 RT_COMMA, /* ',' */
138 RT_SCOPE, /* '::' */
139 RT_LESS, /* '<' */
140 RT_GREATER, /* '>' */
141 RT_EQ, /* '=' */
142 RT_ELLIPSIS, /* '...' */
143 RT_MULT, /* '*' */
144 RT_COMPL, /* '~' */
145 RT_COLON, /* ':' */
146 RT_COLON_SCOPE, /* ':' or '::' */
147 RT_CLOSE_PAREN, /* ')' */
148 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
149 RT_PRAGMA_EOL, /* end of line */
150 RT_NAME, /* identifier */
152 /* The type is CPP_KEYWORD */
153 RT_NEW, /* new */
154 RT_DELETE, /* delete */
155 RT_RETURN, /* return */
156 RT_WHILE, /* while */
157 RT_EXTERN, /* extern */
158 RT_STATIC_ASSERT, /* static_assert */
159 RT_DECLTYPE, /* decltype */
160 RT_OPERATOR, /* operator */
161 RT_CLASS, /* class */
162 RT_TEMPLATE, /* template */
163 RT_NAMESPACE, /* namespace */
164 RT_USING, /* using */
165 RT_ASM, /* asm */
166 RT_TRY, /* try */
167 RT_CATCH, /* catch */
168 RT_THROW, /* throw */
169 RT_AUTO, /* auto */
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 */
184 RT_CO_YIELD /* co_yield */
187 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
188 reverting it on destruction. */
190 class type_id_in_expr_sentinel
192 cp_parser *parser;
193 bool saved;
194 public:
195 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
196 : parser (parser),
197 saved (parser->in_type_id_in_expr_p)
198 { parser->in_type_id_in_expr_p = set; }
199 ~type_id_in_expr_sentinel ()
200 { parser->in_type_id_in_expr_p = saved; }
203 /* Prototypes. */
205 static cp_lexer *cp_lexer_new_main
206 (void);
207 static cp_lexer *cp_lexer_new_from_tokens
208 (cp_token_cache *tokens);
209 static void cp_lexer_destroy
210 (cp_lexer *);
211 static int cp_lexer_saving_tokens
212 (const cp_lexer *);
213 static cp_token *cp_lexer_token_at
214 (cp_lexer *, cp_token_position);
215 static void cp_lexer_get_preprocessor_token
216 (unsigned, cp_token *);
217 static inline cp_token *cp_lexer_peek_token
218 (cp_lexer *);
219 static cp_token *cp_lexer_peek_nth_token
220 (cp_lexer *, size_t);
221 static inline bool cp_lexer_next_token_is
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_not
224 (cp_lexer *, enum cpp_ttype);
225 static bool cp_lexer_next_token_is_keyword
226 (cp_lexer *, enum rid);
227 static cp_token *cp_lexer_consume_token
228 (cp_lexer *);
229 static void cp_lexer_purge_token
230 (cp_lexer *);
231 static void cp_lexer_purge_tokens_after
232 (cp_lexer *, cp_token_position);
233 static void cp_lexer_save_tokens
234 (cp_lexer *);
235 static void cp_lexer_commit_tokens
236 (cp_lexer *);
237 static void cp_lexer_rollback_tokens
238 (cp_lexer *);
239 static void cp_lexer_print_token
240 (FILE *, cp_token *);
241 static inline bool cp_lexer_debugging_p
242 (cp_lexer *);
243 static void cp_lexer_start_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
245 static void cp_lexer_stop_debugging
246 (cp_lexer *) ATTRIBUTE_UNUSED;
248 static cp_token_cache *cp_token_cache_new
249 (cp_token *, cp_token *);
250 static tree cp_parser_late_noexcept_specifier
251 (cp_parser *, tree);
252 static void noexcept_override_late_checks
253 (tree, tree);
255 static void cp_parser_initial_pragma
256 (cp_token *);
258 static bool cp_parser_omp_declare_reduction_exprs
259 (tree, cp_parser *);
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 forbidden in current context",
544 (parser->local_variables_forbidden_p
545 & LOCAL_VARS_FORBIDDEN));
546 cp_debug_print_flag (file, "'this' forbidden in current context",
547 (parser->local_variables_forbidden_p
548 & THIS_FORBIDDEN));
549 cp_debug_print_flag (file, "In unbraced linkage specification",
550 parser->in_unbraced_linkage_specification_p);
551 cp_debug_print_flag (file, "Parsing a declarator",
552 parser->in_declarator_p);
553 cp_debug_print_flag (file, "In template argument list",
554 parser->in_template_argument_list_p);
555 cp_debug_print_flag (file, "Parsing an iteration statement",
556 parser->in_statement & IN_ITERATION_STMT);
557 cp_debug_print_flag (file, "Parsing a switch statement",
558 parser->in_statement & IN_SWITCH_STMT);
559 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
560 parser->in_statement & IN_OMP_BLOCK);
561 cp_debug_print_flag (file, "Parsing an OpenMP loop",
562 parser->in_statement & IN_OMP_FOR);
563 cp_debug_print_flag (file, "Parsing an if statement",
564 parser->in_statement & IN_IF_STMT);
565 cp_debug_print_flag (file, "Parsing a type-id in an expression "
566 "context", parser->in_type_id_in_expr_p);
567 cp_debug_print_flag (file, "String expressions should be translated "
568 "to execution character set",
569 parser->translate_strings_p);
570 cp_debug_print_flag (file, "Parsing function body outside of a "
571 "local class", parser->in_function_body);
572 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
573 parser->colon_corrects_to_scope_p);
574 cp_debug_print_flag (file, "Colon doesn't start a class definition",
575 parser->colon_doesnt_start_class_def_p);
576 cp_debug_print_flag (file, "Parsing an Objective-C++ message context",
577 parser->objective_c_message_context_p);
578 if (parser->type_definition_forbidden_message)
579 fprintf (file, "Error message for forbidden type definitions: %s %s\n",
580 parser->type_definition_forbidden_message,
581 parser->type_definition_forbidden_message_arg
582 ? parser->type_definition_forbidden_message_arg : "<none>");
583 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
584 fprintf (file, "Number of class definitions in progress: %u\n",
585 parser->num_classes_being_defined);
586 fprintf (file, "Number of template parameter lists for the current "
587 "declaration: %u\n", parser->num_template_parameter_lists);
588 cp_debug_parser_tokens (file, parser, window_size);
589 token = parser->lexer->next_token;
590 fprintf (file, "Next token to parse:\n");
591 fprintf (file, "\tToken: ");
592 cp_lexer_print_token (file, token);
593 eloc = expand_location (token->location);
594 fprintf (file, "\n\tFile: %s\n", eloc.file);
595 fprintf (file, "\tLine: %d\n", eloc.line);
596 fprintf (file, "\tColumn: %d\n", eloc.column);
599 DEBUG_FUNCTION void
600 debug (cp_parser &ref)
602 cp_debug_parser (stderr, &ref);
605 DEBUG_FUNCTION void
606 debug (cp_parser *ptr)
608 if (ptr)
609 debug (*ptr);
610 else
611 fprintf (stderr, "<nil>\n");
614 /* Allocate memory for a new lexer object and return it. */
616 static cp_lexer *
617 cp_lexer_alloc (void)
619 /* Allocate the memory. */
620 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
622 /* Initially we are not debugging. */
623 lexer->debugging_p = false;
625 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
627 /* Create the buffer. */
628 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
630 return lexer;
633 /* Return TRUE if token is the start of a module declaration that will be
634 terminated by a CPP_PRAGMA_EOL token. */
635 static inline bool
636 cp_token_is_module_directive (cp_token *token)
638 return token->keyword == RID__EXPORT
639 || token->keyword == RID__MODULE
640 || token->keyword == RID__IMPORT;
643 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
644 PRAGMA_NONE. */
646 static enum pragma_kind
647 cp_parser_pragma_kind (cp_token *token)
649 if (token->type != CPP_PRAGMA)
650 return PRAGMA_NONE;
651 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
652 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
655 /* Handle early pragmas such as #pragma GCC diagnostic, which needs to be done
656 during preprocessing for the case of preprocessing-related diagnostics. This
657 is called immediately after pushing the CPP_PRAGMA_EOL token onto
658 lexer->buffer. */
660 static void
661 cp_lexer_handle_early_pragma (cp_lexer *lexer)
663 const auto first_token = lexer->buffer->address ();
664 const auto last_token = first_token + lexer->buffer->length () - 1;
666 /* Back up to the start of the pragma so pragma_lex () can parse it when
667 c-pragma lib asks it to. */
668 auto begin = last_token;
669 gcc_assert (begin->type == CPP_PRAGMA_EOL);
670 while (begin->type != CPP_PRAGMA)
672 if (cp_token_is_module_directive (begin))
673 return;
674 gcc_assert (begin != first_token);
675 --begin;
677 gcc_assert (!lexer->next_token);
678 gcc_assert (!lexer->last_token);
679 lexer->next_token = begin;
680 lexer->last_token = last_token;
682 /* Dispatch it. */
683 const unsigned int id
684 = cp_parser_pragma_kind (cp_lexer_consume_token (lexer));
685 if (id >= PRAGMA_FIRST_EXTERNAL)
686 c_invoke_early_pragma_handler (id);
688 /* Reset to normal state. */
689 lexer->next_token = lexer->last_token = nullptr;
692 /* The parser. */
693 static cp_parser *cp_parser_new (cp_lexer *);
694 static GTY (()) cp_parser *the_parser;
696 /* Create a new main C++ lexer, the lexer that gets tokens from the
697 preprocessor, and also create the main parser. */
699 static cp_lexer *
700 cp_lexer_new_main (void)
702 cp_token token;
704 /* It's possible that parsing the first pragma will load a PCH file,
705 which is a GC collection point. So we have to do that before
706 allocating any memory. */
707 cp_lexer_get_preprocessor_token (0, &token);
708 cp_parser_initial_pragma (&token);
709 c_common_no_more_pch ();
711 cp_lexer *lexer = cp_lexer_alloc ();
712 /* Put the first token in the buffer. */
713 cp_token *tok = lexer->buffer->quick_push (token);
715 uintptr_t filter = 0;
716 if (modules_p ())
717 filter = module_token_cdtor (parse_in, filter);
719 /* Create the parser now, so we can use it to handle early pragmas. */
720 gcc_assert (!the_parser);
721 the_parser = cp_parser_new (lexer);
723 /* Get the remaining tokens from the preprocessor. */
724 while (tok->type != CPP_EOF)
726 if (filter)
727 /* Process the previous token. */
728 module_token_lang (tok->type, tok->keyword, tok->u.value,
729 tok->location, filter);
731 /* Check for early pragmas that need to be handled now. */
732 if (tok->type == CPP_PRAGMA_EOL)
733 cp_lexer_handle_early_pragma (lexer);
735 tok = vec_safe_push (lexer->buffer, cp_token ());
736 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN, tok);
739 lexer->next_token = lexer->buffer->address ();
740 lexer->last_token = lexer->next_token
741 + lexer->buffer->length ()
742 - 1;
744 if (lexer->buffer->length () != 1)
746 /* Set the EOF token's location to be the just after the previous
747 token's range. That way 'at-eof' diagnostics point at something
748 meaninful. */
749 auto range = get_range_from_loc (line_table, tok[-1].location);
750 tok[0].location
751 = linemap_position_for_loc_and_offset (line_table, range.m_finish, 1);
754 if (filter)
755 module_token_cdtor (parse_in, filter);
757 /* Subsequent preprocessor diagnostics should use compiler
758 diagnostic functions to get the compiler source location. */
759 override_libcpp_locations = true;
761 maybe_check_all_macros (parse_in);
763 gcc_assert (!lexer->next_token->purged_p);
764 return lexer;
767 /* Create a new lexer whose token stream is primed with the tokens in
768 CACHE. When these tokens are exhausted, no new tokens will be read. */
770 static cp_lexer *
771 cp_lexer_new_from_tokens (cp_token_cache *cache)
773 cp_token *first = cache->first;
774 cp_token *last = cache->last;
775 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
777 /* We do not own the buffer. */
778 lexer->buffer = NULL;
780 /* Insert an EOF token. */
781 lexer->saved_type = last->type;
782 lexer->saved_keyword = last->keyword;
783 last->type = CPP_EOF;
784 last->keyword = RID_MAX;
786 lexer->next_token = first;
787 lexer->last_token = last;
789 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
791 /* Initially we are not debugging. */
792 lexer->debugging_p = false;
794 gcc_assert (!lexer->next_token->purged_p
795 && !lexer->last_token->purged_p);
796 return lexer;
799 /* Frees all resources associated with LEXER. */
801 static void
802 cp_lexer_destroy (cp_lexer *lexer)
804 if (lexer->buffer)
805 vec_free (lexer->buffer);
806 else
808 /* Restore the token we overwrite with EOF. */
809 lexer->last_token->type = lexer->saved_type;
810 lexer->last_token->keyword = lexer->saved_keyword;
812 lexer->saved_tokens.release ();
813 ggc_free (lexer);
816 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
817 be used. The point of this flag is to help the compiler to fold away calls
818 to cp_lexer_debugging_p within this source file at compile time, when the
819 lexer is not being debugged. */
821 #define LEXER_DEBUGGING_ENABLED_P false
823 /* Returns nonzero if debugging information should be output. */
825 static inline bool
826 cp_lexer_debugging_p (cp_lexer *lexer)
828 if (!LEXER_DEBUGGING_ENABLED_P)
829 return false;
831 return lexer->debugging_p;
835 static inline cp_token_position
836 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
838 return lexer->next_token - previous_p;
841 static inline cp_token *
842 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
844 return pos;
847 static inline void
848 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
850 lexer->next_token = cp_lexer_token_at (lexer, pos);
853 static inline cp_token_position
854 cp_lexer_previous_token_position (cp_lexer *lexer)
856 return cp_lexer_token_position (lexer, true);
859 static inline cp_token *
860 cp_lexer_previous_token (cp_lexer *lexer)
862 cp_token_position tp = cp_lexer_previous_token_position (lexer);
864 /* Skip past purged tokens. */
865 while (tp->purged_p)
867 gcc_assert (tp != vec_safe_address (lexer->buffer));
868 tp--;
871 return cp_lexer_token_at (lexer, tp);
874 /* Same as above, but return NULL when the lexer doesn't own the token
875 buffer or if the next_token is at the start of the token
876 vector or if all previous tokens are purged. */
878 static cp_token *
879 cp_lexer_safe_previous_token (cp_lexer *lexer)
881 if (lexer->buffer
882 && lexer->next_token != lexer->buffer->address ())
884 cp_token_position tp = cp_lexer_previous_token_position (lexer);
886 /* Skip past purged tokens. */
887 while (tp->purged_p)
889 if (tp == lexer->buffer->address ())
890 return NULL;
891 tp--;
893 return cp_lexer_token_at (lexer, tp);
896 return NULL;
899 /* Overload for make_location, taking the lexer to mean the location of the
900 previous token. */
902 static inline location_t
903 make_location (location_t caret, location_t start, cp_lexer *lexer)
905 cp_token *t = cp_lexer_previous_token (lexer);
906 return make_location (caret, start, t->location);
909 /* Overload for make_location taking tokens instead of locations. */
911 static inline location_t
912 make_location (cp_token *caret, cp_token *start, cp_token *end)
914 return make_location (caret->location, start->location, end->location);
917 /* nonzero if we are presently saving tokens. */
919 static inline int
920 cp_lexer_saving_tokens (const cp_lexer* lexer)
922 return lexer->saved_tokens.length () != 0;
925 /* Store the next token from the preprocessor in *TOKEN. Return true
926 if we reach EOF. If LEXER is NULL, assume we are handling an
927 initial #pragma pch_preprocess, and thus want the lexer to return
928 processed strings.
930 Diagnostics issued from this function must have their controlling option (if
931 any) in c.opt annotated as a libcpp option via the CppReason property. */
933 static void
934 cp_lexer_get_preprocessor_token (unsigned flags, cp_token *token)
936 static int is_extern_c = 0;
938 /* Get a new token from the preprocessor. */
939 token->type
940 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
941 flags);
942 token->keyword = RID_MAX;
943 token->purged_p = false;
944 token->error_reported = false;
945 token->tree_check_p = false;
946 /* Usually never see a zero, but just in case ... */
947 token->main_source_p = line_table->depth <= 1;
949 /* On some systems, some header files are surrounded by an
950 implicit extern "C" block. Set a flag in the token if it
951 comes from such a header. */
952 is_extern_c += pending_lang_change;
953 pending_lang_change = 0;
954 token->implicit_extern_c = is_extern_c > 0;
956 /* Check to see if this token is a keyword. */
957 if (token->type == CPP_NAME)
959 if (IDENTIFIER_KEYWORD_P (token->u.value))
961 /* Mark this token as a keyword. */
962 token->type = CPP_KEYWORD;
963 /* Record which keyword. */
964 token->keyword = C_RID_CODE (token->u.value);
966 else
968 if (warn_cxx11_compat
969 && ((C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
970 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
971 /* These are outside the CXX11 range. */
972 || C_RID_CODE (token->u.value) == RID_ALIGNOF
973 || C_RID_CODE (token->u.value) == RID_ALIGNAS
974 || C_RID_CODE (token->u.value)== RID_THREAD))
976 /* Warn about the C++11 keyword (but still treat it as
977 an identifier). */
978 warning_at (token->location, OPT_Wc__11_compat,
979 "identifier %qE is a keyword in C++11",
980 token->u.value);
982 /* Clear out the C_RID_CODE so we don't warn about this
983 particular identifier-turned-keyword again. */
984 C_SET_RID_CODE (token->u.value, RID_MAX);
986 if (warn_cxx20_compat
987 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX20
988 && C_RID_CODE (token->u.value) <= RID_LAST_CXX20)
990 /* Warn about the C++20 keyword (but still treat it as
991 an identifier). */
992 warning_at (token->location, OPT_Wc__20_compat,
993 "identifier %qE is a keyword in C++20",
994 token->u.value);
996 /* Clear out the C_RID_CODE so we don't warn about this
997 particular identifier-turned-keyword again. */
998 C_SET_RID_CODE (token->u.value, RID_MAX);
1001 token->keyword = RID_MAX;
1004 else if (token->type == CPP_AT_NAME)
1006 /* This only happens in Objective-C++; it must be a keyword. */
1007 token->type = CPP_KEYWORD;
1008 switch (C_RID_CODE (token->u.value))
1010 /* Replace 'class' with '@class', 'private' with '@private',
1011 etc. This prevents confusion with the C++ keyword
1012 'class', and makes the tokens consistent with other
1013 Objective-C 'AT' keywords. For example '@class' is
1014 reported as RID_AT_CLASS which is consistent with
1015 '@synchronized', which is reported as
1016 RID_AT_SYNCHRONIZED.
1018 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
1019 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
1020 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
1021 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
1022 case RID_THROW: token->keyword = RID_AT_THROW; break;
1023 case RID_TRY: token->keyword = RID_AT_TRY; break;
1024 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
1025 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
1026 default: token->keyword = C_RID_CODE (token->u.value);
1031 /* Update the globals input_location and the input file stack from TOKEN. */
1032 static inline void
1033 cp_lexer_set_source_position_from_token (cp_token *token)
1035 input_location = token->location;
1038 /* Update the globals input_location and the input file stack from LEXER. */
1039 static inline void
1040 cp_lexer_set_source_position (cp_lexer *lexer)
1042 cp_token *token = cp_lexer_peek_token (lexer);
1043 cp_lexer_set_source_position_from_token (token);
1046 /* Return a pointer to the next token in the token stream, but do not
1047 consume it. */
1049 static inline cp_token *
1050 cp_lexer_peek_token (cp_lexer *lexer)
1052 if (cp_lexer_debugging_p (lexer))
1054 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
1055 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
1056 putc ('\n', cp_lexer_debug_stream);
1058 return lexer->next_token;
1061 /* Return true if the next token has the indicated TYPE. */
1063 static inline bool
1064 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
1066 return cp_lexer_peek_token (lexer)->type == type;
1069 /* Return true if the next token does not have the indicated TYPE. */
1071 static inline bool
1072 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
1074 return !cp_lexer_next_token_is (lexer, type);
1077 /* Return true if the next token is the indicated KEYWORD. */
1079 static inline bool
1080 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
1082 return cp_lexer_peek_token (lexer)->keyword == keyword;
1085 static inline bool
1086 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
1088 return cp_lexer_peek_nth_token (lexer, n)->type == type;
1091 static inline bool
1092 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
1094 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
1097 /* Return true if KEYWORD can start a decl-specifier. */
1099 bool
1100 cp_keyword_starts_decl_specifier_p (enum rid keyword)
1102 switch (keyword)
1104 /* auto specifier: storage-class-specifier in C++,
1105 simple-type-specifier in C++0x. */
1106 case RID_AUTO:
1107 /* Storage classes. */
1108 case RID_REGISTER:
1109 case RID_STATIC:
1110 case RID_EXTERN:
1111 case RID_MUTABLE:
1112 case RID_THREAD:
1113 /* Elaborated type specifiers. */
1114 case RID_ENUM:
1115 case RID_CLASS:
1116 case RID_STRUCT:
1117 case RID_UNION:
1118 case RID_TYPENAME:
1119 /* Simple type specifiers. */
1120 case RID_CHAR:
1121 case RID_CHAR8:
1122 case RID_CHAR16:
1123 case RID_CHAR32:
1124 case RID_WCHAR:
1125 case RID_BOOL:
1126 case RID_SHORT:
1127 case RID_INT:
1128 case RID_LONG:
1129 case RID_SIGNED:
1130 case RID_UNSIGNED:
1131 case RID_FLOAT:
1132 case RID_DOUBLE:
1133 CASE_RID_FLOATN_NX:
1134 case RID_VOID:
1135 /* CV qualifiers. */
1136 case RID_CONST:
1137 case RID_VOLATILE:
1138 /* Function specifiers. */
1139 case RID_EXPLICIT:
1140 case RID_VIRTUAL:
1141 /* friend/typdef/inline specifiers. */
1142 case RID_FRIEND:
1143 case RID_TYPEDEF:
1144 case RID_INLINE:
1145 /* GNU extensions. */
1146 case RID_TYPEOF:
1147 /* C++11 extensions. */
1148 case RID_DECLTYPE:
1149 case RID_CONSTEXPR:
1150 /* C++20 extensions. */
1151 case RID_CONSTINIT:
1152 case RID_CONSTEVAL:
1153 return true;
1155 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
1156 case RID_##CODE:
1157 #include "cp-trait.def"
1158 #undef DEFTRAIT_TYPE
1159 return true;
1161 default:
1162 if (keyword >= RID_FIRST_INT_N
1163 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
1164 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
1165 return true;
1166 return false;
1170 /* Return true if the next token is a keyword for a decl-specifier. */
1172 static bool
1173 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
1175 cp_token *token;
1177 token = cp_lexer_peek_token (lexer);
1178 return cp_keyword_starts_decl_specifier_p (token->keyword);
1181 /* Returns TRUE iff the token T begins a decltype type. */
1183 static bool
1184 token_is_decltype (cp_token *t)
1186 return (t->keyword == RID_DECLTYPE
1187 || t->type == CPP_DECLTYPE);
1190 /* Returns TRUE iff the next token begins a decltype type. */
1192 static bool
1193 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1195 cp_token *t = cp_lexer_peek_token (lexer);
1196 return token_is_decltype (t);
1199 /* Called when processing a token with tree_check_value; perform or defer the
1200 associated checks and return the value. */
1202 static tree
1203 saved_checks_value (struct tree_check *check_value)
1205 /* Perform any access checks that were deferred. */
1206 vec<deferred_access_check, va_gc> *checks;
1207 deferred_access_check *chk;
1208 checks = check_value->checks;
1209 if (checks)
1211 int i;
1212 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1213 perform_or_defer_access_check (chk->binfo,
1214 chk->decl,
1215 chk->diag_decl, tf_warning_or_error);
1217 /* Return the stored value. */
1218 return check_value->value;
1221 /* Return a pointer to the Nth token in the token stream. If N is 1,
1222 then this is precisely equivalent to cp_lexer_peek_token (except
1223 that it is not inline). One would like to disallow that case, but
1224 there is one case (cp_parser_nth_token_starts_template_id) where
1225 the caller passes a variable for N and it might be 1. */
1227 static cp_token *
1228 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1230 cp_token *token;
1232 /* N is 1-based, not zero-based. */
1233 gcc_assert (n > 0);
1235 if (cp_lexer_debugging_p (lexer))
1236 fprintf (cp_lexer_debug_stream,
1237 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1239 --n;
1240 token = lexer->next_token;
1241 while (n && token->type != CPP_EOF)
1243 ++token;
1244 if (!token->purged_p)
1245 --n;
1248 if (cp_lexer_debugging_p (lexer))
1250 cp_lexer_print_token (cp_lexer_debug_stream, token);
1251 putc ('\n', cp_lexer_debug_stream);
1254 return token;
1257 /* Return the next token, and advance the lexer's next_token pointer
1258 to point to the next non-purged token. */
1260 static cp_token *
1261 cp_lexer_consume_token (cp_lexer* lexer)
1263 cp_token *token = lexer->next_token;
1267 gcc_assert (token->type != CPP_EOF);
1268 lexer->next_token++;
1270 while (lexer->next_token->purged_p);
1272 cp_lexer_set_source_position_from_token (token);
1274 /* Provide debugging output. */
1275 if (cp_lexer_debugging_p (lexer))
1277 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1278 cp_lexer_print_token (cp_lexer_debug_stream, token);
1279 putc ('\n', cp_lexer_debug_stream);
1282 return token;
1285 /* Permanently remove the next token from the token stream, and
1286 advance the next_token pointer to refer to the next non-purged
1287 token. */
1289 static void
1290 cp_lexer_purge_token (cp_lexer *lexer)
1292 cp_token *tok = lexer->next_token;
1294 gcc_assert (tok->type != CPP_EOF);
1295 tok->purged_p = true;
1296 tok->location = UNKNOWN_LOCATION;
1297 tok->u.value = NULL_TREE;
1298 tok->keyword = RID_MAX;
1301 tok++;
1302 while (tok->purged_p);
1303 lexer->next_token = tok;
1306 /* Permanently remove all tokens after TOK, up to, but not
1307 including, the token that will be returned next by
1308 cp_lexer_peek_token. */
1310 static void
1311 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1313 cp_token *peek = lexer->next_token;
1315 gcc_assert (tok < peek);
1317 for (tok++; tok != peek; tok++)
1319 tok->purged_p = true;
1320 tok->location = UNKNOWN_LOCATION;
1321 tok->u.value = NULL_TREE;
1322 tok->keyword = RID_MAX;
1326 /* Begin saving tokens. All tokens consumed after this point will be
1327 preserved. */
1329 static void
1330 cp_lexer_save_tokens (cp_lexer* lexer)
1332 /* Provide debugging output. */
1333 if (cp_lexer_debugging_p (lexer))
1334 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1336 lexer->saved_tokens.safe_push (lexer->next_token);
1339 /* Commit to the portion of the token stream most recently saved. */
1341 static void
1342 cp_lexer_commit_tokens (cp_lexer* lexer)
1344 /* Provide debugging output. */
1345 if (cp_lexer_debugging_p (lexer))
1346 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1348 lexer->saved_tokens.pop ();
1351 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1352 to the token stream. Stop saving tokens. */
1354 static void
1355 cp_lexer_rollback_tokens (cp_lexer* lexer)
1357 /* Provide debugging output. */
1358 if (cp_lexer_debugging_p (lexer))
1359 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1361 lexer->next_token = lexer->saved_tokens.pop ();
1364 /* Determines what saved_token_sentinel does when going out of scope. */
1366 enum saved_token_sentinel_mode {
1367 STS_COMMIT,
1368 STS_ROLLBACK,
1369 STS_DONOTHING
1372 /* RAII wrapper around the above functions, with sanity checking (the token
1373 stream should be the same at the point of instantiation as it is at the
1374 point of destruction).
1376 Creating a variable saves tokens. MODE determines what happens when the
1377 object is destroyed. STS_COMMIT commits tokens (default),
1378 STS_ROLLBACK rolls-back and STS_DONOTHING does nothing. Calling
1379 rollback() will immediately roll-back tokens and set MODE to
1380 STS_DONOTHING. */
1382 struct saved_token_sentinel
1384 cp_lexer *lexer;
1385 unsigned len;
1386 saved_token_sentinel_mode mode;
1387 saved_token_sentinel (cp_lexer *_lexer,
1388 saved_token_sentinel_mode _mode = STS_COMMIT)
1389 : lexer (_lexer), mode (_mode)
1391 len = lexer->saved_tokens.length ();
1392 cp_lexer_save_tokens (lexer);
1394 void rollback ()
1396 cp_lexer_rollback_tokens (lexer);
1397 cp_lexer_set_source_position_from_token
1398 (cp_lexer_previous_token (lexer));
1399 mode = STS_DONOTHING;
1401 ~saved_token_sentinel ()
1403 if (mode == STS_COMMIT)
1404 cp_lexer_commit_tokens (lexer);
1405 else if (mode == STS_ROLLBACK)
1406 rollback ();
1408 gcc_assert (lexer->saved_tokens.length () == len);
1412 /* Print a representation of the TOKEN on the STREAM. */
1414 static void
1415 cp_lexer_print_token (FILE * stream, cp_token *token)
1417 /* We don't use cpp_type2name here because the parser defines
1418 a few tokens of its own. */
1419 static const char *const token_names[] = {
1420 /* cpplib-defined token types */
1421 #define OP(e, s) #e,
1422 #define TK(e, s) #e,
1423 TTYPE_TABLE
1424 #undef OP
1425 #undef TK
1426 /* C++ parser token types - see "Manifest constants", above. */
1427 "KEYWORD",
1428 "TEMPLATE_ID",
1429 "NESTED_NAME_SPECIFIER",
1432 /* For some tokens, print the associated data. */
1433 switch (token->type)
1435 case CPP_KEYWORD:
1436 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1437 For example, `struct' is mapped to an INTEGER_CST. */
1438 if (!identifier_p (token->u.value))
1439 break;
1440 /* fall through */
1441 case CPP_NAME:
1442 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1443 break;
1445 case CPP_STRING:
1446 case CPP_STRING16:
1447 case CPP_STRING32:
1448 case CPP_WSTRING:
1449 case CPP_UTF8STRING:
1450 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1451 break;
1453 case CPP_NUMBER:
1454 print_generic_expr (stream, token->u.value);
1455 break;
1457 default:
1458 /* If we have a name for the token, print it out. Otherwise, we
1459 simply give the numeric code. */
1460 if (token->type < ARRAY_SIZE(token_names))
1461 fputs (token_names[token->type], stream);
1462 else
1463 fprintf (stream, "[%d]", token->type);
1464 break;
1468 DEBUG_FUNCTION void
1469 debug (cp_token &ref)
1471 cp_lexer_print_token (stderr, &ref);
1472 fprintf (stderr, "\n");
1475 DEBUG_FUNCTION void
1476 debug (cp_token *ptr)
1478 if (ptr)
1479 debug (*ptr);
1480 else
1481 fprintf (stderr, "<nil>\n");
1485 /* Start emitting debugging information. */
1487 static void
1488 cp_lexer_start_debugging (cp_lexer* lexer)
1490 if (!LEXER_DEBUGGING_ENABLED_P)
1491 fatal_error (input_location,
1492 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1494 lexer->debugging_p = true;
1495 cp_lexer_debug_stream = stderr;
1498 /* Stop emitting debugging information. */
1500 static void
1501 cp_lexer_stop_debugging (cp_lexer* lexer)
1503 if (!LEXER_DEBUGGING_ENABLED_P)
1504 fatal_error (input_location,
1505 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1507 lexer->debugging_p = false;
1508 cp_lexer_debug_stream = NULL;
1511 /* Create a new cp_token_cache, representing a range of tokens. */
1513 static cp_token_cache *
1514 cp_token_cache_new (cp_token *first, cp_token *last)
1516 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1517 cache->first = first;
1518 cache->last = last;
1519 return cache;
1522 /* Diagnose if #pragma omp declare simd isn't followed immediately
1523 by function declaration or definition. */
1525 static inline void
1526 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1528 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1530 error ("%<#pragma omp declare %s%> not immediately followed by "
1531 "function declaration or definition",
1532 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1533 parser->omp_declare_simd = NULL;
1537 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1538 and put that into "omp declare simd" attribute. */
1540 static inline void
1541 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1543 if (UNLIKELY (parser->omp_declare_simd != NULL))
1545 if (fndecl == error_mark_node)
1547 parser->omp_declare_simd = NULL;
1548 return;
1550 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1552 cp_ensure_no_omp_declare_simd (parser);
1553 return;
1558 /* Similarly, but for use in declaration parsing functions
1559 which call cp_parser_handle_directive_omp_attributes. */
1561 static inline void
1562 cp_finalize_omp_declare_simd (cp_parser *parser, cp_omp_declare_simd_data *data)
1564 if (parser->omp_declare_simd != data)
1565 return;
1567 if (!parser->omp_declare_simd->error_seen
1568 && !parser->omp_declare_simd->fndecl_seen)
1569 error_at (parser->omp_declare_simd->loc,
1570 "%<declare %s%> directive not immediately followed by "
1571 "function declaration or definition",
1572 parser->omp_declare_simd->variant_p ? "variant" : "simd");
1573 parser->omp_declare_simd = NULL;
1576 /* Diagnose if #pragma acc routine isn't followed immediately by function
1577 declaration or definition. */
1579 static inline void
1580 cp_ensure_no_oacc_routine (cp_parser *parser)
1582 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1584 error_at (parser->oacc_routine->loc,
1585 "%<#pragma acc routine%> not immediately followed by "
1586 "function declaration or definition");
1587 parser->oacc_routine = NULL;
1591 /* Decl-specifiers. */
1593 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1595 static void
1596 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1598 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1601 /* Declarators. */
1603 /* Nothing other than the parser should be creating declarators;
1604 declarators are a semi-syntactic representation of C++ entities.
1605 Other parts of the front end that need to create entities (like
1606 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1608 static cp_declarator *make_call_declarator
1609 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier,
1610 tree, tree, tree, tree, location_t);
1611 static cp_declarator *make_array_declarator
1612 (cp_declarator *, tree);
1613 static cp_declarator *make_pointer_declarator
1614 (cp_cv_quals, cp_declarator *, tree);
1615 static cp_declarator *make_reference_declarator
1616 (cp_cv_quals, cp_declarator *, bool, tree);
1617 static cp_declarator *make_ptrmem_declarator
1618 (cp_cv_quals, tree, cp_declarator *, tree);
1620 /* An erroneous declarator. */
1621 static cp_declarator *cp_error_declarator;
1623 /* The obstack on which declarators and related data structures are
1624 allocated. */
1625 static struct obstack declarator_obstack;
1627 /* Alloc BYTES from the declarator memory pool. */
1629 static inline void *
1630 alloc_declarator (size_t bytes)
1632 return obstack_alloc (&declarator_obstack, bytes);
1635 /* Allocate a declarator of the indicated KIND. Clear fields that are
1636 common to all declarators. */
1638 static cp_declarator *
1639 make_declarator (cp_declarator_kind kind)
1641 cp_declarator *declarator;
1643 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1644 declarator->kind = kind;
1645 declarator->parenthesized = UNKNOWN_LOCATION;
1646 declarator->attributes = NULL_TREE;
1647 declarator->std_attributes = NULL_TREE;
1648 declarator->declarator = NULL;
1649 declarator->parameter_pack_p = false;
1650 declarator->id_loc = UNKNOWN_LOCATION;
1651 declarator->init_loc = UNKNOWN_LOCATION;
1653 return declarator;
1656 /* Make a declarator for a generalized identifier. If
1657 QUALIFYING_SCOPE is non-NULL, the identifier is
1658 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1659 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1660 is, if any. */
1662 static cp_declarator *
1663 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1664 special_function_kind sfk, location_t id_location)
1666 cp_declarator *declarator;
1668 /* It is valid to write:
1670 class C { void f(); };
1671 typedef C D;
1672 void D::f();
1674 The standard is not clear about whether `typedef const C D' is
1675 legal; as of 2002-09-15 the committee is considering that
1676 question. EDG 3.0 allows that syntax. Therefore, we do as
1677 well. */
1678 if (qualifying_scope && TYPE_P (qualifying_scope))
1679 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1681 gcc_assert (identifier_p (unqualified_name)
1682 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1683 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1685 declarator = make_declarator (cdk_id);
1686 declarator->u.id.qualifying_scope = qualifying_scope;
1687 declarator->u.id.unqualified_name = unqualified_name;
1688 declarator->u.id.sfk = sfk;
1689 declarator->id_loc = id_location;
1691 return declarator;
1694 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1695 of modifiers such as const or volatile to apply to the pointer
1696 type, represented as identifiers. ATTRIBUTES represent the attributes that
1697 appertain to the pointer or reference. */
1699 cp_declarator *
1700 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1701 tree attributes)
1703 cp_declarator *declarator;
1705 declarator = make_declarator (cdk_pointer);
1706 declarator->declarator = target;
1707 declarator->u.pointer.qualifiers = cv_qualifiers;
1708 declarator->u.pointer.class_type = NULL_TREE;
1709 if (target)
1711 declarator->id_loc = target->id_loc;
1712 declarator->parameter_pack_p = target->parameter_pack_p;
1713 target->parameter_pack_p = false;
1715 else
1716 declarator->parameter_pack_p = false;
1718 declarator->std_attributes = attributes;
1720 return declarator;
1723 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1724 represent the attributes that appertain to the pointer or
1725 reference. */
1727 cp_declarator *
1728 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1729 bool rvalue_ref, tree attributes)
1731 cp_declarator *declarator;
1733 declarator = make_declarator (cdk_reference);
1734 declarator->declarator = target;
1735 declarator->u.reference.qualifiers = cv_qualifiers;
1736 declarator->u.reference.rvalue_ref = rvalue_ref;
1737 if (target)
1739 declarator->id_loc = target->id_loc;
1740 declarator->parameter_pack_p = target->parameter_pack_p;
1741 target->parameter_pack_p = false;
1743 else
1744 declarator->parameter_pack_p = false;
1746 declarator->std_attributes = attributes;
1748 return declarator;
1751 /* Like make_pointer_declarator -- but for a pointer to a non-static
1752 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1753 appertain to the pointer or reference. */
1755 cp_declarator *
1756 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1757 cp_declarator *pointee,
1758 tree attributes)
1760 cp_declarator *declarator;
1762 declarator = make_declarator (cdk_ptrmem);
1763 declarator->declarator = pointee;
1764 declarator->u.pointer.qualifiers = cv_qualifiers;
1765 declarator->u.pointer.class_type = class_type;
1767 if (pointee)
1769 declarator->parameter_pack_p = pointee->parameter_pack_p;
1770 pointee->parameter_pack_p = false;
1772 else
1773 declarator->parameter_pack_p = false;
1775 declarator->std_attributes = attributes;
1777 return declarator;
1780 /* Make a declarator for the function given by TARGET, with the
1781 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1782 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1783 indicates what exceptions can be thrown. */
1785 cp_declarator *
1786 make_call_declarator (cp_declarator *target,
1787 tree parms,
1788 cp_cv_quals cv_qualifiers,
1789 cp_virt_specifiers virt_specifiers,
1790 cp_ref_qualifier ref_qualifier,
1791 tree tx_qualifier,
1792 tree exception_specification,
1793 tree late_return_type,
1794 tree requires_clause,
1795 location_t parens_loc)
1797 cp_declarator *declarator;
1799 declarator = make_declarator (cdk_function);
1800 declarator->declarator = target;
1801 declarator->u.function.parameters = parms;
1802 declarator->u.function.qualifiers = cv_qualifiers;
1803 declarator->u.function.virt_specifiers = virt_specifiers;
1804 declarator->u.function.ref_qualifier = ref_qualifier;
1805 declarator->u.function.tx_qualifier = tx_qualifier;
1806 declarator->u.function.exception_specification = exception_specification;
1807 declarator->u.function.late_return_type = late_return_type;
1808 declarator->u.function.requires_clause = requires_clause;
1809 declarator->u.function.parens_loc = parens_loc;
1810 if (target)
1812 declarator->id_loc = target->id_loc;
1813 declarator->parameter_pack_p = target->parameter_pack_p;
1814 target->parameter_pack_p = false;
1816 else
1817 declarator->parameter_pack_p = false;
1819 return declarator;
1822 /* Make a declarator for an array of BOUNDS elements, each of which is
1823 defined by ELEMENT. */
1825 cp_declarator *
1826 make_array_declarator (cp_declarator *element, tree bounds)
1828 cp_declarator *declarator;
1830 declarator = make_declarator (cdk_array);
1831 declarator->declarator = element;
1832 declarator->u.array.bounds = bounds;
1833 if (element)
1835 declarator->id_loc = element->id_loc;
1836 declarator->parameter_pack_p = element->parameter_pack_p;
1837 element->parameter_pack_p = false;
1839 else
1840 declarator->parameter_pack_p = false;
1842 return declarator;
1845 /* Determine whether the declarator we've seen so far can be a
1846 parameter pack, when followed by an ellipsis. */
1847 static bool
1848 declarator_can_be_parameter_pack (cp_declarator *declarator)
1850 if (declarator && declarator->parameter_pack_p)
1851 /* We already saw an ellipsis. */
1852 return false;
1854 /* Search for a declarator name, or any other declarator that goes
1855 after the point where the ellipsis could appear in a parameter
1856 pack. If we find any of these, then this declarator cannot be
1857 made into a parameter pack. */
1858 bool found = false;
1859 while (declarator && !found)
1861 switch ((int)declarator->kind)
1863 case cdk_id:
1864 case cdk_array:
1865 case cdk_decomp:
1866 found = true;
1867 break;
1869 case cdk_error:
1870 return true;
1872 default:
1873 declarator = declarator->declarator;
1874 break;
1878 return !found;
1881 cp_parameter_declarator *no_parameters;
1883 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1884 DECLARATOR and DEFAULT_ARGUMENT. */
1886 cp_parameter_declarator *
1887 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1888 cp_declarator *declarator,
1889 tree default_argument,
1890 location_t loc,
1891 bool template_parameter_pack_p = false)
1893 cp_parameter_declarator *parameter;
1895 parameter = ((cp_parameter_declarator *)
1896 alloc_declarator (sizeof (cp_parameter_declarator)));
1897 parameter->next = NULL;
1898 if (decl_specifiers)
1899 parameter->decl_specifiers = *decl_specifiers;
1900 else
1901 clear_decl_specs (&parameter->decl_specifiers);
1902 parameter->declarator = declarator;
1903 parameter->default_argument = default_argument;
1904 parameter->template_parameter_pack_p = template_parameter_pack_p;
1905 parameter->loc = loc;
1907 return parameter;
1910 /* Returns true iff DECLARATOR is a declaration for a function. */
1912 static bool
1913 function_declarator_p (const cp_declarator *declarator)
1915 while (declarator)
1917 if (declarator->kind == cdk_function
1918 && declarator->declarator->kind == cdk_id)
1919 return true;
1920 if (declarator->kind == cdk_id
1921 || declarator->kind == cdk_decomp
1922 || declarator->kind == cdk_error)
1923 return false;
1924 declarator = declarator->declarator;
1926 return false;
1929 /* The parser. */
1931 /* Overview
1932 --------
1934 A cp_parser parses the token stream as specified by the C++
1935 grammar. Its job is purely parsing, not semantic analysis. For
1936 example, the parser breaks the token stream into declarators,
1937 expressions, statements, and other similar syntactic constructs.
1938 It does not check that the types of the expressions on either side
1939 of an assignment-statement are compatible, or that a function is
1940 not declared with a parameter of type `void'.
1942 The parser invokes routines elsewhere in the compiler to perform
1943 semantic analysis and to build up the abstract syntax tree for the
1944 code processed.
1946 The parser (and the template instantiation code, which is, in a
1947 way, a close relative of parsing) are the only parts of the
1948 compiler that should be calling push_scope and pop_scope, or
1949 related functions. The parser (and template instantiation code)
1950 keeps track of what scope is presently active; everything else
1951 should simply honor that. (The code that generates static
1952 initializers may also need to set the scope, in order to check
1953 access control correctly when emitting the initializers.)
1955 Methodology
1956 -----------
1958 The parser is of the standard recursive-descent variety. Upcoming
1959 tokens in the token stream are examined in order to determine which
1960 production to use when parsing a non-terminal. Some C++ constructs
1961 require arbitrary look ahead to disambiguate. For example, it is
1962 impossible, in the general case, to tell whether a statement is an
1963 expression or declaration without scanning the entire statement.
1964 Therefore, the parser is capable of "parsing tentatively." When the
1965 parser is not sure what construct comes next, it enters this mode.
1966 Then, while we attempt to parse the construct, the parser queues up
1967 error messages, rather than issuing them immediately, and saves the
1968 tokens it consumes. If the construct is parsed successfully, the
1969 parser "commits", i.e., it issues any queued error messages and
1970 the tokens that were being preserved are permanently discarded.
1971 If, however, the construct is not parsed successfully, the parser
1972 rolls back its state completely so that it can resume parsing using
1973 a different alternative.
1975 Future Improvements
1976 -------------------
1978 The performance of the parser could probably be improved substantially.
1979 We could often eliminate the need to parse tentatively by looking ahead
1980 a little bit. In some places, this approach might not entirely eliminate
1981 the need to parse tentatively, but it might still speed up the average
1982 case. */
1984 /* Flags that are passed to some parsing functions. These values can
1985 be bitwise-ored together. */
1987 enum
1989 /* No flags. */
1990 CP_PARSER_FLAGS_NONE = 0x0,
1991 /* The construct is optional. If it is not present, then no error
1992 should be issued. */
1993 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1994 /* When parsing a type-specifier, treat user-defined type-names
1995 as non-type identifiers. */
1996 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1997 /* When parsing a type-specifier, do not try to parse a class-specifier
1998 or enum-specifier. */
1999 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
2000 /* When parsing a decl-specifier-seq, only allow type-specifier or
2001 constexpr. */
2002 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
2003 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
2004 for C++20 consteval or for C++23 static. */
2005 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10,
2006 /* When parsing a decl-specifier-seq, allow missing typename. */
2007 CP_PARSER_FLAGS_TYPENAME_OPTIONAL = 0x20,
2008 /* When parsing of the noexcept-specifier should be delayed. */
2009 CP_PARSER_FLAGS_DELAY_NOEXCEPT = 0x40,
2010 /* When parsing a consteval declarator. */
2011 CP_PARSER_FLAGS_CONSTEVAL = 0x80
2014 /* This type is used for parameters and variables which hold
2015 combinations of the above flags. */
2016 typedef int cp_parser_flags;
2018 /* The different kinds of declarators we want to parse. */
2020 enum cp_parser_declarator_kind
2022 /* We want an abstract declarator. */
2023 CP_PARSER_DECLARATOR_ABSTRACT,
2024 /* We want a named declarator. */
2025 CP_PARSER_DECLARATOR_NAMED,
2026 /* We don't mind, but the name must be an unqualified-id. */
2027 CP_PARSER_DECLARATOR_EITHER
2030 /* The precedence values used to parse binary expressions. The minimum value
2031 of PREC must be 1, because zero is reserved to quickly discriminate
2032 binary operators from other tokens. */
2034 enum cp_parser_prec
2036 PREC_NOT_OPERATOR,
2037 PREC_LOGICAL_OR_EXPRESSION,
2038 PREC_LOGICAL_AND_EXPRESSION,
2039 PREC_INCLUSIVE_OR_EXPRESSION,
2040 PREC_EXCLUSIVE_OR_EXPRESSION,
2041 PREC_AND_EXPRESSION,
2042 PREC_EQUALITY_EXPRESSION,
2043 PREC_RELATIONAL_EXPRESSION,
2044 PREC_SPACESHIP_EXPRESSION,
2045 PREC_SHIFT_EXPRESSION,
2046 PREC_ADDITIVE_EXPRESSION,
2047 PREC_MULTIPLICATIVE_EXPRESSION,
2048 PREC_PM_EXPRESSION,
2049 NUM_PREC_VALUES = PREC_PM_EXPRESSION
2052 /* A mapping from a token type to a corresponding tree node type, with a
2053 precedence value. */
2055 struct cp_parser_binary_operations_map_node
2057 /* The token type. */
2058 enum cpp_ttype token_type;
2059 /* The corresponding tree code. */
2060 enum tree_code tree_type;
2061 /* The precedence of this operator. */
2062 enum cp_parser_prec prec;
2065 struct cp_parser_expression_stack_entry
2067 /* Left hand side of the binary operation we are currently
2068 parsing. */
2069 cp_expr lhs;
2070 /* Original tree code for left hand side, if it was a binary
2071 expression itself (used for -Wparentheses). */
2072 enum tree_code lhs_type;
2073 /* Tree code for the binary operation we are parsing. */
2074 enum tree_code tree_type;
2075 /* Precedence of the binary operation we are parsing. */
2076 enum cp_parser_prec prec;
2077 /* Location of the binary operation we are parsing. */
2078 location_t loc;
2079 /* Flags from the operator token. */
2080 unsigned char flags;
2083 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
2084 entries because precedence levels on the stack are monotonically
2085 increasing. */
2086 typedef struct cp_parser_expression_stack_entry
2087 cp_parser_expression_stack[NUM_PREC_VALUES];
2089 /* Prototypes. */
2091 /* Constructors and destructors. */
2093 static cp_parser_context *cp_parser_context_new
2094 (cp_parser_context *);
2096 /* Class variables. */
2098 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
2100 /* The operator-precedence table used by cp_parser_binary_expression.
2101 Transformed into an associative array (binops_by_token) by
2102 cp_parser_new. */
2104 static const cp_parser_binary_operations_map_node binops[] = {
2105 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
2106 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
2108 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2109 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2110 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
2112 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2113 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
2115 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2116 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
2118 { CPP_SPACESHIP, SPACESHIP_EXPR, PREC_SPACESHIP_EXPRESSION },
2120 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
2121 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
2122 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
2123 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
2125 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
2126 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
2128 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
2130 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
2132 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
2134 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
2136 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
2139 /* The same as binops, but initialized by cp_parser_new so that
2140 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2141 for speed. */
2142 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
2144 /* Constructors and destructors. */
2146 /* Construct a new context. The context below this one on the stack
2147 is given by NEXT. */
2149 static cp_parser_context *
2150 cp_parser_context_new (cp_parser_context* next)
2152 cp_parser_context *context;
2154 /* Allocate the storage. */
2155 if (cp_parser_context_free_list != NULL)
2157 /* Pull the first entry from the free list. */
2158 context = cp_parser_context_free_list;
2159 cp_parser_context_free_list = context->next;
2160 memset (context, 0, sizeof (*context));
2162 else
2163 context = ggc_cleared_alloc<cp_parser_context> ();
2165 /* No errors have occurred yet in this context. */
2166 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
2167 /* If this is not the bottommost context, copy information that we
2168 need from the previous context. */
2169 if (next)
2171 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2172 expression, then we are parsing one in this context, too. */
2173 context->object_type = next->object_type;
2174 /* Thread the stack. */
2175 context->next = next;
2178 return context;
2181 /* Managing the unparsed function queues. */
2183 #define unparsed_funs_with_default_args \
2184 parser->unparsed_queues->last ().funs_with_default_args
2185 #define unparsed_funs_with_definitions \
2186 parser->unparsed_queues->last ().funs_with_definitions
2187 #define unparsed_nsdmis \
2188 parser->unparsed_queues->last ().nsdmis
2189 #define unparsed_noexcepts \
2190 parser->unparsed_queues->last ().noexcepts
2192 static void
2193 push_unparsed_function_queues (cp_parser *parser)
2195 cp_unparsed_functions_entry e = { NULL, make_tree_vector (), NULL, NULL };
2196 vec_safe_push (parser->unparsed_queues, e);
2199 static void
2200 pop_unparsed_function_queues (cp_parser *parser)
2202 release_tree_vector (unparsed_funs_with_definitions);
2203 parser->unparsed_queues->pop ();
2206 /* Prototypes. */
2208 /* Routines to parse various constructs.
2210 Those that return `tree' will return the error_mark_node (rather
2211 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2212 Sometimes, they will return an ordinary node if error-recovery was
2213 attempted, even though a parse error occurred. So, to check
2214 whether or not a parse error occurred, you should always use
2215 cp_parser_error_occurred. If the construct is optional (indicated
2216 either by an `_opt' in the name of the function that does the
2217 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2218 the construct is not present. */
2220 /* Lexical conventions [gram.lex] */
2222 static cp_expr cp_parser_identifier
2223 (cp_parser *);
2224 static cp_expr cp_parser_string_literal
2225 (cp_parser *, bool, bool, bool);
2226 static cp_expr cp_parser_userdef_char_literal
2227 (cp_parser *);
2228 static tree cp_parser_userdef_string_literal
2229 (tree);
2230 static cp_expr cp_parser_userdef_numeric_literal
2231 (cp_parser *);
2233 /* Basic concepts [gram.basic] */
2235 static void cp_parser_translation_unit (cp_parser *);
2237 /* Expressions [gram.expr] */
2239 static cp_expr cp_parser_primary_expression
2240 (cp_parser *, bool, bool, bool, cp_id_kind *);
2241 static cp_expr cp_parser_id_expression
2242 (cp_parser *, bool, bool, bool *, bool, bool);
2243 static cp_expr cp_parser_unqualified_id
2244 (cp_parser *, bool, bool, bool, bool);
2245 static tree cp_parser_nested_name_specifier_opt
2246 (cp_parser *, bool, bool, bool, bool, bool = false);
2247 static tree cp_parser_nested_name_specifier
2248 (cp_parser *, bool, bool, bool, bool);
2249 static tree cp_parser_qualifying_entity
2250 (cp_parser *, bool, bool, bool, bool, bool);
2251 static cp_expr cp_parser_postfix_expression
2252 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2253 static tree cp_parser_postfix_open_square_expression
2254 (cp_parser *, tree, bool, bool);
2255 static tree cp_parser_postfix_dot_deref_expression
2256 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2257 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2258 (cp_parser *, int, bool, bool, bool *, location_t * = NULL,
2259 bool = false);
2260 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2261 enum { non_attr = 0, normal_attr = 1, id_attr = 2, assume_attr = 3 };
2262 static void cp_parser_pseudo_destructor_name
2263 (cp_parser *, tree, tree *, tree *);
2264 static cp_expr cp_parser_unary_expression
2265 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2266 static enum tree_code cp_parser_unary_operator
2267 (cp_token *);
2268 static tree cp_parser_has_attribute_expression
2269 (cp_parser *);
2270 static tree cp_parser_new_expression
2271 (cp_parser *);
2272 static vec<tree, va_gc> *cp_parser_new_placement
2273 (cp_parser *);
2274 static tree cp_parser_new_type_id
2275 (cp_parser *, tree *);
2276 static cp_declarator *cp_parser_new_declarator_opt
2277 (cp_parser *);
2278 static cp_declarator *cp_parser_direct_new_declarator
2279 (cp_parser *);
2280 static vec<tree, va_gc> *cp_parser_new_initializer
2281 (cp_parser *);
2282 static tree cp_parser_delete_expression
2283 (cp_parser *);
2284 static cp_expr cp_parser_cast_expression
2285 (cp_parser *, bool, bool, bool, cp_id_kind *);
2286 static cp_expr cp_parser_binary_expression
2287 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2288 static tree cp_parser_question_colon_clause
2289 (cp_parser *, cp_expr);
2290 static cp_expr cp_parser_conditional_expression (cp_parser *);
2291 static cp_expr cp_parser_assignment_expression
2292 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2293 static enum tree_code cp_parser_assignment_operator_opt
2294 (cp_parser *);
2295 static cp_expr cp_parser_expression
2296 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2297 static cp_expr cp_parser_constant_expression
2298 (cp_parser *, int = 0, bool * = NULL, bool = false);
2299 static cp_expr cp_parser_builtin_offsetof
2300 (cp_parser *);
2301 static cp_expr cp_parser_lambda_expression
2302 (cp_parser *);
2303 static void cp_parser_lambda_introducer
2304 (cp_parser *, tree);
2305 static bool cp_parser_lambda_declarator_opt
2306 (cp_parser *, tree);
2307 static void cp_parser_lambda_body
2308 (cp_parser *, tree);
2310 /* Statements [gram.stmt.stmt] */
2312 static void cp_parser_statement
2313 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2314 static void cp_parser_label_for_labeled_statement
2315 (cp_parser *, tree);
2316 static tree cp_parser_expression_statement
2317 (cp_parser *, tree);
2318 static tree cp_parser_compound_statement
2319 (cp_parser *, tree, int, bool);
2320 static void cp_parser_statement_seq_opt
2321 (cp_parser *, tree);
2322 static tree cp_parser_selection_statement
2323 (cp_parser *, bool *, vec<tree> *);
2324 static tree cp_parser_condition
2325 (cp_parser *);
2326 static tree cp_parser_iteration_statement
2327 (cp_parser *, bool *, bool, unsigned short);
2328 static bool cp_parser_init_statement
2329 (cp_parser *, tree *decl);
2330 static tree cp_parser_for
2331 (cp_parser *, bool, unsigned short);
2332 static tree cp_parser_c_for
2333 (cp_parser *, tree, tree, bool, unsigned short);
2334 static tree cp_parser_range_for
2335 (cp_parser *, tree, tree, tree, bool, unsigned short, bool);
2336 static void do_range_for_auto_deduction
2337 (tree, tree);
2338 static tree cp_parser_perform_range_for_lookup
2339 (tree, tree *, tree *);
2340 static tree cp_parser_range_for_member_function
2341 (tree, tree);
2342 static tree cp_parser_jump_statement
2343 (cp_parser *);
2344 static void cp_parser_declaration_statement
2345 (cp_parser *);
2347 static tree cp_parser_implicitly_scoped_statement
2348 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2349 static void cp_parser_already_scoped_statement
2350 (cp_parser *, bool *, const token_indent_info &);
2352 /* State of module-declaration parsing. */
2353 enum module_parse
2355 MP_NOT_MODULE, /* Not a module. */
2357 _MP_UNUSED,
2359 MP_FIRST, /* First declaration of TU. */
2360 MP_GLOBAL, /* Global Module Fragment. */
2362 MP_PURVIEW_IMPORTS, /* Imports of a module. */
2363 MP_PURVIEW, /* Purview of a named module. */
2365 MP_PRIVATE_IMPORTS, /* Imports of a Private Module Fragment. */
2366 MP_PRIVATE, /* Private Module Fragment. */
2369 static module_parse cp_parser_module_declaration
2370 (cp_parser *parser, module_parse, bool exporting);
2371 static void cp_parser_import_declaration
2372 (cp_parser *parser, module_parse, bool exporting);
2374 /* Declarations [gram.dcl.dcl] */
2376 static void cp_parser_declaration_seq_opt
2377 (cp_parser *);
2378 static void cp_parser_declaration
2379 (cp_parser *, tree);
2380 static void cp_parser_toplevel_declaration
2381 (cp_parser *);
2382 static void cp_parser_block_declaration
2383 (cp_parser *, bool);
2384 static void cp_parser_simple_declaration
2385 (cp_parser *, bool, tree *);
2386 static void cp_parser_decl_specifier_seq
2387 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2388 static tree cp_parser_storage_class_specifier_opt
2389 (cp_parser *);
2390 static tree cp_parser_function_specifier_opt
2391 (cp_parser *, cp_decl_specifier_seq *);
2392 static tree cp_parser_type_specifier
2393 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2394 int *, bool *);
2395 static tree cp_parser_simple_type_specifier
2396 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2397 static tree cp_parser_placeholder_type_specifier
2398 (cp_parser *, location_t, tree, bool);
2399 static tree cp_parser_type_name
2400 (cp_parser *, bool);
2401 static tree cp_parser_nonclass_name
2402 (cp_parser* parser);
2403 static tree cp_parser_elaborated_type_specifier
2404 (cp_parser *, bool, bool);
2405 static tree cp_parser_enum_specifier
2406 (cp_parser *);
2407 static void cp_parser_enumerator_list
2408 (cp_parser *, tree);
2409 static void cp_parser_enumerator_definition
2410 (cp_parser *, tree);
2411 static tree cp_parser_namespace_name
2412 (cp_parser *);
2413 static void cp_parser_namespace_definition
2414 (cp_parser *);
2415 static void cp_parser_namespace_body
2416 (cp_parser *);
2417 static tree cp_parser_qualified_namespace_specifier
2418 (cp_parser *);
2419 static void cp_parser_namespace_alias_definition
2420 (cp_parser *);
2421 static bool cp_parser_using_declaration
2422 (cp_parser *, bool);
2423 static void cp_parser_using_directive
2424 (cp_parser *);
2425 static void cp_parser_using_enum
2426 (cp_parser *);
2427 static tree cp_parser_alias_declaration
2428 (cp_parser *);
2429 static void cp_parser_asm_definition
2430 (cp_parser *);
2431 static void cp_parser_linkage_specification
2432 (cp_parser *, tree);
2433 static void cp_parser_static_assert
2434 (cp_parser *, bool);
2435 static tree cp_parser_decltype
2436 (cp_parser *);
2437 static tree cp_parser_decomposition_declaration
2438 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2440 /* Declarators [gram.dcl.decl] */
2442 static tree cp_parser_init_declarator
2443 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *,
2444 vec<deferred_access_check, va_gc> *, bool, bool, int, bool *, tree *,
2445 location_t *, tree *);
2446 static cp_declarator *cp_parser_declarator
2447 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool *,
2448 bool, bool, bool);
2449 static cp_declarator *cp_parser_direct_declarator
2450 (cp_parser *, cp_parser_declarator_kind, cp_parser_flags, int *, bool, bool,
2451 bool);
2452 static enum tree_code cp_parser_ptr_operator
2453 (cp_parser *, tree *, cp_cv_quals *, tree *);
2454 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2455 (cp_parser *);
2456 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2457 (cp_parser *);
2458 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2459 (cp_parser *);
2460 static tree cp_parser_tx_qualifier_opt
2461 (cp_parser *);
2462 static tree cp_parser_late_return_type_opt
2463 (cp_parser *, cp_declarator *, tree &);
2464 static tree cp_parser_declarator_id
2465 (cp_parser *, bool);
2466 static tree cp_parser_type_id
2467 (cp_parser *, cp_parser_flags = CP_PARSER_FLAGS_NONE, location_t * = NULL);
2468 static tree cp_parser_template_type_arg
2469 (cp_parser *);
2470 static tree cp_parser_trailing_type_id (cp_parser *);
2471 static tree cp_parser_type_id_1
2472 (cp_parser *, cp_parser_flags, bool, bool, location_t *);
2473 static void cp_parser_type_specifier_seq
2474 (cp_parser *, cp_parser_flags, bool, bool, cp_decl_specifier_seq *);
2475 static tree cp_parser_parameter_declaration_clause
2476 (cp_parser *, cp_parser_flags);
2477 static tree cp_parser_parameter_declaration_list
2478 (cp_parser *, cp_parser_flags, auto_vec<tree> *);
2479 static cp_parameter_declarator *cp_parser_parameter_declaration
2480 (cp_parser *, cp_parser_flags, bool, bool *);
2481 static tree cp_parser_default_argument
2482 (cp_parser *, bool);
2483 static void cp_parser_function_body
2484 (cp_parser *, bool);
2485 static tree cp_parser_initializer
2486 (cp_parser *, bool *, bool *, bool = false);
2487 static cp_expr cp_parser_initializer_clause
2488 (cp_parser *, bool *);
2489 static cp_expr cp_parser_braced_list
2490 (cp_parser*, bool*);
2491 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2492 (cp_parser *, bool *, bool *);
2494 static void cp_parser_ctor_initializer_opt_and_function_body
2495 (cp_parser *, bool);
2497 static tree cp_parser_late_parsing_omp_declare_simd
2498 (cp_parser *, tree);
2500 static tree cp_parser_late_parsing_oacc_routine
2501 (cp_parser *, tree);
2503 static tree synthesize_implicit_template_parm
2504 (cp_parser *, tree);
2505 static tree finish_fully_implicit_template
2506 (cp_parser *, tree);
2507 static void abort_fully_implicit_template
2508 (cp_parser *);
2510 /* Classes [gram.class] */
2512 static tree cp_parser_class_name
2513 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2514 static tree cp_parser_class_specifier
2515 (cp_parser *);
2516 static tree cp_parser_class_head
2517 (cp_parser *, bool *);
2518 static enum tag_types cp_parser_class_key
2519 (cp_parser *);
2520 static void cp_parser_type_parameter_key
2521 (cp_parser* parser);
2522 static void cp_parser_member_specification_opt
2523 (cp_parser *);
2524 static void cp_parser_member_declaration
2525 (cp_parser *);
2526 static tree cp_parser_pure_specifier
2527 (cp_parser *);
2528 static tree cp_parser_constant_initializer
2529 (cp_parser *);
2531 /* Derived classes [gram.class.derived] */
2533 static tree cp_parser_base_clause
2534 (cp_parser *);
2535 static tree cp_parser_base_specifier
2536 (cp_parser *);
2538 /* Special member functions [gram.special] */
2540 static tree cp_parser_conversion_function_id
2541 (cp_parser *);
2542 static tree cp_parser_conversion_type_id
2543 (cp_parser *);
2544 static cp_declarator *cp_parser_conversion_declarator_opt
2545 (cp_parser *);
2546 static void cp_parser_ctor_initializer_opt
2547 (cp_parser *);
2548 static void cp_parser_mem_initializer_list
2549 (cp_parser *);
2550 static tree cp_parser_mem_initializer
2551 (cp_parser *);
2552 static tree cp_parser_mem_initializer_id
2553 (cp_parser *);
2555 /* Overloading [gram.over] */
2557 static cp_expr cp_parser_operator_function_id
2558 (cp_parser *);
2559 static cp_expr cp_parser_operator
2560 (cp_parser *, location_t);
2562 /* Templates [gram.temp] */
2564 static void cp_parser_template_declaration
2565 (cp_parser *, bool);
2566 static tree cp_parser_template_parameter_list
2567 (cp_parser *);
2568 static tree cp_parser_template_parameter
2569 (cp_parser *, bool *, bool *);
2570 static tree cp_parser_type_parameter
2571 (cp_parser *, bool *);
2572 static tree cp_parser_template_id
2573 (cp_parser *, bool, bool, enum tag_types, bool);
2574 static tree cp_parser_template_id_expr
2575 (cp_parser *, bool, bool, bool);
2576 static tree cp_parser_template_name
2577 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2578 static tree cp_parser_template_argument_list
2579 (cp_parser *);
2580 static tree cp_parser_template_argument
2581 (cp_parser *);
2582 static void cp_parser_explicit_instantiation
2583 (cp_parser *);
2584 static void cp_parser_explicit_specialization
2585 (cp_parser *);
2587 /* Exception handling [gram.except] */
2589 static tree cp_parser_try_block
2590 (cp_parser *);
2591 static void cp_parser_function_try_block
2592 (cp_parser *);
2593 static void cp_parser_handler_seq
2594 (cp_parser *);
2595 static void cp_parser_handler
2596 (cp_parser *);
2597 static tree cp_parser_exception_declaration
2598 (cp_parser *);
2599 static tree cp_parser_throw_expression
2600 (cp_parser *);
2601 static tree cp_parser_exception_specification_opt
2602 (cp_parser *, cp_parser_flags);
2603 static tree cp_parser_type_id_list
2604 (cp_parser *);
2605 static tree cp_parser_noexcept_specification_opt
2606 (cp_parser *, cp_parser_flags, bool, bool *, bool);
2608 /* GNU Extensions */
2610 static tree cp_parser_asm_specification_opt
2611 (cp_parser *);
2612 static tree cp_parser_asm_operand_list
2613 (cp_parser *);
2614 static tree cp_parser_asm_clobber_list
2615 (cp_parser *);
2616 static tree cp_parser_asm_label_list
2617 (cp_parser *);
2618 static bool cp_next_tokens_can_be_attribute_p
2619 (cp_parser *);
2620 static bool cp_next_tokens_can_be_gnu_attribute_p
2621 (cp_parser *);
2622 static bool cp_next_tokens_can_be_std_attribute_p
2623 (cp_parser *);
2624 static bool cp_nth_tokens_can_be_std_attribute_p
2625 (cp_parser *, size_t);
2626 static bool cp_nth_tokens_can_be_gnu_attribute_p
2627 (cp_parser *, size_t);
2628 static bool cp_nth_tokens_can_be_attribute_p
2629 (cp_parser *, size_t);
2630 static tree cp_parser_attributes_opt
2631 (cp_parser *);
2632 static tree cp_parser_gnu_attributes_opt
2633 (cp_parser *);
2634 static tree cp_parser_gnu_attribute_list
2635 (cp_parser *, bool = false);
2636 static tree cp_parser_std_attribute
2637 (cp_parser *, tree);
2638 static tree cp_parser_std_attribute_spec
2639 (cp_parser *);
2640 static tree cp_parser_std_attribute_spec_seq
2641 (cp_parser *);
2642 static size_t cp_parser_skip_std_attribute_spec_seq
2643 (cp_parser *, size_t);
2644 static size_t cp_parser_skip_attributes_opt
2645 (cp_parser *, size_t);
2646 static bool cp_parser_extension_opt
2647 (cp_parser *, int *);
2648 static void cp_parser_label_declaration
2649 (cp_parser *);
2651 /* Concept Extensions */
2653 static tree cp_parser_concept_definition
2654 (cp_parser *);
2655 static tree cp_parser_constraint_expression
2656 (cp_parser *);
2657 static tree cp_parser_requires_clause_opt
2658 (cp_parser *, bool);
2659 static tree cp_parser_requires_expression
2660 (cp_parser *);
2661 static tree cp_parser_requirement_parameter_list
2662 (cp_parser *);
2663 static tree cp_parser_requirement_body
2664 (cp_parser *);
2665 static tree cp_parser_requirement_seq
2666 (cp_parser *);
2667 static tree cp_parser_requirement
2668 (cp_parser *);
2669 static tree cp_parser_simple_requirement
2670 (cp_parser *);
2671 static tree cp_parser_compound_requirement
2672 (cp_parser *);
2673 static tree cp_parser_type_requirement
2674 (cp_parser *);
2675 static tree cp_parser_nested_requirement
2676 (cp_parser *);
2678 /* Transactional Memory Extensions */
2680 static tree cp_parser_transaction
2681 (cp_parser *, cp_token *);
2682 static tree cp_parser_transaction_expression
2683 (cp_parser *, enum rid);
2684 static void cp_parser_function_transaction
2685 (cp_parser *, enum rid);
2686 static tree cp_parser_transaction_cancel
2687 (cp_parser *);
2689 /* Coroutine extensions. */
2691 static tree cp_parser_yield_expression
2692 (cp_parser *);
2695 enum pragma_context {
2696 pragma_external,
2697 pragma_member,
2698 pragma_objc_icode,
2699 pragma_stmt,
2700 pragma_compound
2702 static bool cp_parser_pragma
2703 (cp_parser *, enum pragma_context, bool *);
2705 /* Objective-C++ Productions */
2707 static tree cp_parser_objc_message_receiver
2708 (cp_parser *);
2709 static tree cp_parser_objc_message_args
2710 (cp_parser *);
2711 static tree cp_parser_objc_message_expression
2712 (cp_parser *);
2713 static cp_expr cp_parser_objc_encode_expression
2714 (cp_parser *);
2715 static tree cp_parser_objc_defs_expression
2716 (cp_parser *);
2717 static tree cp_parser_objc_protocol_expression
2718 (cp_parser *);
2719 static tree cp_parser_objc_selector_expression
2720 (cp_parser *);
2721 static cp_expr cp_parser_objc_expression
2722 (cp_parser *);
2723 static bool cp_parser_objc_selector_p
2724 (enum cpp_ttype);
2725 static tree cp_parser_objc_selector
2726 (cp_parser *);
2727 static tree cp_parser_objc_protocol_refs_opt
2728 (cp_parser *);
2729 static void cp_parser_objc_declaration
2730 (cp_parser *, tree);
2731 static tree cp_parser_objc_statement
2732 (cp_parser *);
2733 static bool cp_parser_objc_valid_prefix_attributes
2734 (cp_parser *, tree *);
2735 static void cp_parser_objc_at_property_declaration
2736 (cp_parser *) ;
2737 static void cp_parser_objc_at_synthesize_declaration
2738 (cp_parser *) ;
2739 static void cp_parser_objc_at_dynamic_declaration
2740 (cp_parser *) ;
2741 static tree cp_parser_objc_struct_declaration
2742 (cp_parser *) ;
2744 /* Utility Routines */
2746 static cp_expr cp_parser_lookup_name
2747 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2748 static tree cp_parser_lookup_name_simple
2749 (cp_parser *, tree, location_t);
2750 static tree cp_parser_maybe_treat_template_as_class
2751 (tree, bool);
2752 static bool cp_parser_check_declarator_template_parameters
2753 (cp_parser *, cp_declarator *, location_t);
2754 static bool cp_parser_check_template_parameters
2755 (cp_parser *, unsigned, bool, location_t, cp_declarator *);
2756 static cp_expr cp_parser_simple_cast_expression
2757 (cp_parser *);
2758 static tree cp_parser_global_scope_opt
2759 (cp_parser *, bool);
2760 static bool cp_parser_constructor_declarator_p
2761 (cp_parser *, cp_parser_flags, bool);
2762 static tree cp_parser_function_definition_from_specifiers_and_declarator
2763 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2764 static tree cp_parser_function_definition_after_declarator
2765 (cp_parser *, bool);
2766 static bool cp_parser_template_declaration_after_export
2767 (cp_parser *, bool);
2768 static void cp_parser_perform_template_parameter_access_checks
2769 (vec<deferred_access_check, va_gc> *);
2770 static tree cp_parser_single_declaration
2771 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2772 static cp_expr cp_parser_functional_cast
2773 (cp_parser *, tree);
2774 static tree cp_parser_save_member_function_body
2775 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2776 static tree cp_parser_save_nsdmi
2777 (cp_parser *);
2778 static tree cp_parser_enclosed_template_argument_list
2779 (cp_parser *);
2780 static void cp_parser_save_default_args
2781 (cp_parser *, tree);
2782 static void cp_parser_late_parsing_for_member
2783 (cp_parser *, tree);
2784 static tree cp_parser_late_parse_one_default_arg
2785 (cp_parser *, tree, tree, tree);
2786 static void cp_parser_late_parsing_nsdmi
2787 (cp_parser *, tree);
2788 static void cp_parser_late_parsing_default_args
2789 (cp_parser *, tree);
2790 static tree cp_parser_sizeof_operand
2791 (cp_parser *, enum rid);
2792 static cp_expr cp_parser_trait
2793 (cp_parser *, enum rid);
2794 static bool cp_parser_declares_only_class_p
2795 (cp_parser *);
2796 static void cp_parser_set_storage_class
2797 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2798 static void cp_parser_set_decl_spec_type
2799 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2800 static void set_and_check_decl_spec_loc
2801 (cp_decl_specifier_seq *decl_specs,
2802 cp_decl_spec ds, cp_token *);
2803 static bool cp_parser_friend_p
2804 (const cp_decl_specifier_seq *);
2805 static void cp_parser_required_error
2806 (cp_parser *, required_token, bool, location_t);
2807 static cp_token *cp_parser_require
2808 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2809 static cp_token *cp_parser_require_keyword
2810 (cp_parser *, enum rid, required_token);
2811 static bool cp_parser_token_starts_function_definition_p
2812 (cp_token *);
2813 static bool cp_parser_next_token_starts_class_definition_p
2814 (cp_parser *);
2815 static bool cp_parser_next_token_ends_template_argument_p
2816 (cp_parser *);
2817 static bool cp_parser_nth_token_starts_template_argument_list_p
2818 (cp_parser *, size_t);
2819 static enum tag_types cp_parser_token_is_class_key
2820 (cp_token *);
2821 static enum tag_types cp_parser_token_is_type_parameter_key
2822 (cp_token *);
2823 static void cp_parser_maybe_warn_enum_key (cp_parser *, location_t, tree, rid);
2824 static void cp_parser_check_class_key
2825 (cp_parser *, location_t, enum tag_types, tree type, bool, bool);
2826 static void cp_parser_check_access_in_redeclaration
2827 (tree type, location_t location);
2828 static bool cp_parser_optional_template_keyword
2829 (cp_parser *);
2830 static void cp_parser_pre_parsed_nested_name_specifier
2831 (cp_parser *);
2832 static bool cp_parser_cache_group
2833 (cp_parser *, enum cpp_ttype, unsigned);
2834 static tree cp_parser_cache_defarg
2835 (cp_parser *parser, bool nsdmi);
2836 static void cp_parser_parse_tentatively
2837 (cp_parser *);
2838 static void cp_parser_commit_to_tentative_parse
2839 (cp_parser *);
2840 static void cp_parser_commit_to_topmost_tentative_parse
2841 (cp_parser *);
2842 static void cp_parser_abort_tentative_parse
2843 (cp_parser *);
2844 static bool cp_parser_parse_definitely
2845 (cp_parser *);
2846 static inline bool cp_parser_parsing_tentatively
2847 (cp_parser *);
2848 static bool cp_parser_uncommitted_to_tentative_parse_p
2849 (cp_parser *);
2850 static void cp_parser_error
2851 (cp_parser *, const char *);
2852 static void cp_parser_name_lookup_error
2853 (cp_parser *, tree, tree, name_lookup_error, location_t);
2854 static bool cp_parser_simulate_error
2855 (cp_parser *);
2856 static bool cp_parser_check_type_definition
2857 (cp_parser *);
2858 static void cp_parser_check_for_definition_in_return_type
2859 (cp_declarator *, tree, location_t type_location);
2860 static void cp_parser_check_for_invalid_template_id
2861 (cp_parser *, tree, enum tag_types, location_t location);
2862 static bool cp_parser_non_integral_constant_expression
2863 (cp_parser *, non_integral_constant);
2864 static void cp_parser_diagnose_invalid_type_name
2865 (cp_parser *, tree, location_t);
2866 static bool cp_parser_parse_and_diagnose_invalid_type_name
2867 (cp_parser *);
2868 static int cp_parser_skip_to_closing_parenthesis
2869 (cp_parser *, bool, bool, bool);
2870 static void cp_parser_skip_to_end_of_statement
2871 (cp_parser *);
2872 static void cp_parser_consume_semicolon_at_end_of_statement
2873 (cp_parser *);
2874 static void cp_parser_skip_to_end_of_block_or_statement
2875 (cp_parser *);
2876 static bool cp_parser_skip_to_closing_brace
2877 (cp_parser *);
2878 static bool cp_parser_skip_entire_template_parameter_list
2879 (cp_parser *);
2880 static void cp_parser_require_end_of_template_parameter_list
2881 (cp_parser *);
2882 static bool cp_parser_skip_to_end_of_template_parameter_list
2883 (cp_parser *);
2884 static void cp_parser_skip_to_pragma_eol
2885 (cp_parser*, cp_token *);
2886 static bool cp_parser_error_occurred
2887 (cp_parser *);
2888 static bool cp_parser_allow_gnu_extensions_p
2889 (cp_parser *);
2890 static bool cp_parser_is_pure_string_literal
2891 (cp_token *);
2892 static bool cp_parser_is_string_literal
2893 (cp_token *);
2894 static bool cp_parser_is_keyword
2895 (cp_token *, enum rid);
2896 static tree cp_parser_make_typename_type
2897 (cp_parser *, tree, location_t location);
2898 static cp_declarator * cp_parser_make_indirect_declarator
2899 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2900 static bool cp_parser_compound_literal_p
2901 (cp_parser *);
2902 static bool cp_parser_array_designator_p
2903 (cp_parser *);
2904 static bool cp_parser_init_statement_p
2905 (cp_parser *);
2906 static bool cp_parser_skip_to_closing_square_bracket
2907 (cp_parser *);
2908 static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
2910 // -------------------------------------------------------------------------- //
2911 // Unevaluated Operand Guard
2913 // Implementation of an RAII helper for unevaluated operand parsing.
2914 cp_unevaluated::cp_unevaluated ()
2916 ++cp_unevaluated_operand;
2917 ++c_inhibit_evaluation_warnings;
2920 cp_unevaluated::~cp_unevaluated ()
2922 --c_inhibit_evaluation_warnings;
2923 --cp_unevaluated_operand;
2926 // -------------------------------------------------------------------------- //
2927 // Tentative Parsing
2929 /* Returns nonzero if we are parsing tentatively. */
2931 static inline bool
2932 cp_parser_parsing_tentatively (cp_parser* parser)
2934 return parser->context->next != NULL;
2937 /* Returns nonzero if TOKEN is a string literal. */
2939 static bool
2940 cp_parser_is_pure_string_literal (cp_token* token)
2942 return (token->type == CPP_STRING ||
2943 token->type == CPP_STRING16 ||
2944 token->type == CPP_STRING32 ||
2945 token->type == CPP_WSTRING ||
2946 token->type == CPP_UTF8STRING);
2949 /* Returns nonzero if TOKEN is a string literal
2950 of a user-defined string literal. */
2952 static bool
2953 cp_parser_is_string_literal (cp_token* token)
2955 return (cp_parser_is_pure_string_literal (token) ||
2956 token->type == CPP_STRING_USERDEF ||
2957 token->type == CPP_STRING16_USERDEF ||
2958 token->type == CPP_STRING32_USERDEF ||
2959 token->type == CPP_WSTRING_USERDEF ||
2960 token->type == CPP_UTF8STRING_USERDEF);
2963 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2965 static bool
2966 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2968 return token->keyword == keyword;
2971 /* Helper function for cp_parser_error.
2972 Having peeked a token of kind TOK1_KIND that might signify
2973 a conflict marker, peek successor tokens to determine
2974 if we actually do have a conflict marker.
2975 Specifically, we consider a run of 7 '<', '=' or '>' characters
2976 at the start of a line as a conflict marker.
2977 These come through the lexer as three pairs and a single,
2978 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2979 If it returns true, *OUT_LOC is written to with the location/range
2980 of the marker. */
2982 static bool
2983 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2984 location_t *out_loc)
2986 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2987 if (token2->type != tok1_kind)
2988 return false;
2989 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2990 if (token3->type != tok1_kind)
2991 return false;
2992 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2993 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2994 return false;
2996 /* It must be at the start of the line. */
2997 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2998 if (LOCATION_COLUMN (start_loc) != 1)
2999 return false;
3001 /* We have a conflict marker. Construct a location of the form:
3002 <<<<<<<
3003 ^~~~~~~
3004 with start == caret, finishing at the end of the marker. */
3005 location_t finish_loc = get_finish (token4->location);
3006 *out_loc = make_location (start_loc, start_loc, finish_loc);
3008 return true;
3011 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
3012 RT_CLOSE_PAREN. */
3014 static const char *
3015 get_matching_symbol (required_token token_desc)
3017 switch (token_desc)
3019 default:
3020 gcc_unreachable ();
3021 return "";
3022 case RT_CLOSE_BRACE:
3023 return "{";
3024 case RT_CLOSE_PAREN:
3025 return "(";
3029 /* Attempt to convert TOKEN_DESC from a required_token to an
3030 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
3032 static enum cpp_ttype
3033 get_required_cpp_ttype (required_token token_desc)
3035 switch (token_desc)
3037 case RT_SEMICOLON:
3038 return CPP_SEMICOLON;
3039 case RT_OPEN_PAREN:
3040 return CPP_OPEN_PAREN;
3041 case RT_CLOSE_BRACE:
3042 return CPP_CLOSE_BRACE;
3043 case RT_OPEN_BRACE:
3044 return CPP_OPEN_BRACE;
3045 case RT_CLOSE_SQUARE:
3046 return CPP_CLOSE_SQUARE;
3047 case RT_OPEN_SQUARE:
3048 return CPP_OPEN_SQUARE;
3049 case RT_COMMA:
3050 return CPP_COMMA;
3051 case RT_COLON:
3052 return CPP_COLON;
3053 case RT_CLOSE_PAREN:
3054 return CPP_CLOSE_PAREN;
3056 default:
3057 /* Use CPP_EOF as a "no completions possible" code. */
3058 return CPP_EOF;
3063 /* Subroutine of cp_parser_error and cp_parser_required_error.
3065 Issue a diagnostic of the form
3066 FILE:LINE: MESSAGE before TOKEN
3067 where TOKEN is the next token in the input stream. MESSAGE
3068 (specified by the caller) is usually of the form "expected
3069 OTHER-TOKEN".
3071 This bypasses the check for tentative passing, and potentially
3072 adds material needed by cp_parser_required_error.
3074 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
3075 suggesting insertion of the missing token.
3077 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
3078 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
3079 location. */
3081 static void
3082 cp_parser_error_1 (cp_parser* parser, const char* gmsgid,
3083 required_token missing_token_desc,
3084 location_t matching_location)
3086 cp_token *token = cp_lexer_peek_token (parser->lexer);
3087 /* This diagnostic makes more sense if it is tagged to the line
3088 of the token we just peeked at. */
3089 cp_lexer_set_source_position_from_token (token);
3091 if (token->type == CPP_PRAGMA)
3093 error_at (token->location,
3094 "%<#pragma%> is not allowed here");
3095 cp_parser_skip_to_pragma_eol (parser, token);
3096 return;
3099 /* If this is actually a conflict marker, report it as such. */
3100 if (token->type == CPP_LSHIFT
3101 || token->type == CPP_RSHIFT
3102 || token->type == CPP_EQ_EQ)
3104 location_t loc;
3105 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
3107 error_at (loc, "version control conflict marker in file");
3108 expanded_location token_exploc = expand_location (token->location);
3109 /* Consume tokens until the end of the source line. */
3110 for (;;)
3112 cp_lexer_consume_token (parser->lexer);
3113 cp_token *next = cp_lexer_peek_token (parser->lexer);
3114 if (next->type == CPP_EOF)
3115 break;
3116 if (next->location == UNKNOWN_LOCATION
3117 || loc == UNKNOWN_LOCATION)
3118 break;
3120 expanded_location next_exploc = expand_location (next->location);
3121 if (next_exploc.file != token_exploc.file)
3122 break;
3123 if (next_exploc.line != token_exploc.line)
3124 break;
3126 return;
3130 auto_diagnostic_group d;
3131 gcc_rich_location richloc (input_location);
3133 bool added_matching_location = false;
3135 if (missing_token_desc != RT_NONE)
3136 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3138 /* Potentially supply a fix-it hint, suggesting to add the
3139 missing token immediately after the *previous* token.
3140 This may move the primary location within richloc. */
3141 enum cpp_ttype ttype = get_required_cpp_ttype (missing_token_desc);
3142 location_t prev_token_loc = prev_token->location;
3143 maybe_suggest_missing_token_insertion (&richloc, ttype,
3144 prev_token_loc);
3146 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3147 Attempt to consolidate diagnostics by printing it as a
3148 secondary range within the main diagnostic. */
3149 if (matching_location != UNKNOWN_LOCATION)
3150 added_matching_location
3151 = richloc.add_location_if_nearby (matching_location);
3154 /* If we were parsing a string-literal and there is an unknown name
3155 token right after, then check to see if that could also have been
3156 a literal string by checking the name against a list of known
3157 standard string literal constants defined in header files. If
3158 there is one, then add that as an hint to the error message. */
3159 name_hint h;
3160 if (token->type == CPP_NAME)
3161 if (cp_token *prev_token = cp_lexer_safe_previous_token (parser->lexer))
3162 if (cp_parser_is_string_literal (prev_token))
3164 tree name = token->u.value;
3165 const char *token_name = IDENTIFIER_POINTER (name);
3166 const char *header_hint
3167 = get_cp_stdlib_header_for_string_macro_name (token_name);
3168 if (header_hint != NULL)
3169 h = name_hint (NULL, new suggest_missing_header (token->location,
3170 token_name,
3171 header_hint));
3174 /* Actually emit the error. */
3175 c_parse_error (gmsgid,
3176 /* Because c_parser_error does not understand
3177 CPP_KEYWORD, keywords are treated like
3178 identifiers. */
3179 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
3180 token->u.value, token->flags, &richloc);
3182 if (missing_token_desc != RT_NONE)
3184 /* If we weren't able to consolidate matching_location, then
3185 print it as a secondary diagnostic. */
3186 if (matching_location != UNKNOWN_LOCATION
3187 && !added_matching_location)
3188 inform (matching_location, "to match this %qs",
3189 get_matching_symbol (missing_token_desc));
3193 /* If not parsing tentatively, issue a diagnostic of the form
3194 FILE:LINE: MESSAGE before TOKEN
3195 where TOKEN is the next token in the input stream. MESSAGE
3196 (specified by the caller) is usually of the form "expected
3197 OTHER-TOKEN". */
3199 static void
3200 cp_parser_error (cp_parser* parser, const char* gmsgid)
3202 if (!cp_parser_simulate_error (parser))
3203 cp_parser_error_1 (parser, gmsgid, RT_NONE, UNKNOWN_LOCATION);
3206 /* Issue an error about name-lookup failing. NAME is the
3207 IDENTIFIER_NODE DECL is the result of
3208 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3209 the thing that we hoped to find. */
3211 static void
3212 cp_parser_name_lookup_error (cp_parser* parser,
3213 tree name,
3214 tree decl,
3215 name_lookup_error desired,
3216 location_t location)
3218 /* If name lookup completely failed, tell the user that NAME was not
3219 declared. */
3220 if (decl == error_mark_node)
3222 if (parser->scope && parser->scope != global_namespace)
3223 error_at (location, "%<%E::%E%> has not been declared",
3224 parser->scope, name);
3225 else if (parser->scope == global_namespace)
3226 error_at (location, "%<::%E%> has not been declared", name);
3227 else if (parser->object_scope
3228 && !CLASS_TYPE_P (parser->object_scope))
3229 error_at (location, "request for member %qE in non-class type %qT",
3230 name, parser->object_scope);
3231 else if (parser->object_scope)
3232 error_at (location, "%<%T::%E%> has not been declared",
3233 parser->object_scope, name);
3234 else
3235 error_at (location, "%qE has not been declared", name);
3237 else if (parser->scope && parser->scope != global_namespace)
3239 switch (desired)
3241 case NLE_TYPE:
3242 error_at (location, "%<%E::%E%> is not a type",
3243 parser->scope, name);
3244 break;
3245 case NLE_CXX98:
3246 error_at (location, "%<%E::%E%> is not a class or namespace",
3247 parser->scope, name);
3248 break;
3249 case NLE_NOT_CXX98:
3250 error_at (location,
3251 "%<%E::%E%> is not a class, namespace, or enumeration",
3252 parser->scope, name);
3253 break;
3254 default:
3255 gcc_unreachable ();
3259 else if (parser->scope == global_namespace)
3261 switch (desired)
3263 case NLE_TYPE:
3264 error_at (location, "%<::%E%> is not a type", name);
3265 break;
3266 case NLE_CXX98:
3267 error_at (location, "%<::%E%> is not a class or namespace", name);
3268 break;
3269 case NLE_NOT_CXX98:
3270 error_at (location,
3271 "%<::%E%> is not a class, namespace, or enumeration",
3272 name);
3273 break;
3274 default:
3275 gcc_unreachable ();
3278 else
3280 switch (desired)
3282 case NLE_TYPE:
3283 error_at (location, "%qE is not a type", name);
3284 break;
3285 case NLE_CXX98:
3286 error_at (location, "%qE is not a class or namespace", name);
3287 break;
3288 case NLE_NOT_CXX98:
3289 error_at (location,
3290 "%qE is not a class, namespace, or enumeration", name);
3291 break;
3292 default:
3293 gcc_unreachable ();
3298 /* If we are parsing tentatively, remember that an error has occurred
3299 during this tentative parse. Returns true if the error was
3300 simulated; false if a message should be issued by the caller. */
3302 static bool
3303 cp_parser_simulate_error (cp_parser* parser)
3305 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3307 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
3308 return true;
3310 return false;
3313 /* This function is called when a type is defined. If type
3314 definitions are forbidden at this point, an error message is
3315 issued. */
3317 static bool
3318 cp_parser_check_type_definition (cp_parser* parser)
3320 /* If types are forbidden here, issue a message. */
3321 if (parser->type_definition_forbidden_message)
3323 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3324 or %qs in the message need to be interpreted. */
3325 error (parser->type_definition_forbidden_message,
3326 parser->type_definition_forbidden_message_arg);
3327 return false;
3329 return true;
3332 /* This function is called when the DECLARATOR is processed. The TYPE
3333 was a type defined in the decl-specifiers. If it is invalid to
3334 define a type in the decl-specifiers for DECLARATOR, an error is
3335 issued. TYPE_LOCATION is the location of TYPE and is used
3336 for error reporting. */
3338 static void
3339 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
3340 tree type, location_t type_location)
3342 /* [dcl.fct] forbids type definitions in return types.
3343 Unfortunately, it's not easy to know whether or not we are
3344 processing a return type until after the fact. */
3345 while (declarator
3346 && (declarator->kind == cdk_pointer
3347 || declarator->kind == cdk_reference
3348 || declarator->kind == cdk_ptrmem))
3349 declarator = declarator->declarator;
3350 if (declarator
3351 && declarator->kind == cdk_function)
3353 error_at (type_location,
3354 "new types may not be defined in a return type");
3355 inform (type_location,
3356 "(perhaps a semicolon is missing after the definition of %qT)",
3357 type);
3361 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3362 "<" in any valid C++ program. If the next token is indeed "<",
3363 issue a message warning the user about what appears to be an
3364 invalid attempt to form a template-id. LOCATION is the location
3365 of the type-specifier (TYPE) */
3367 static void
3368 cp_parser_check_for_invalid_template_id (cp_parser* parser,
3369 tree type,
3370 enum tag_types tag_type,
3371 location_t location)
3373 cp_token_position start = 0;
3375 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3377 if (TREE_CODE (type) == TYPE_DECL)
3378 type = TREE_TYPE (type);
3379 if (TYPE_P (type) && !template_placeholder_p (type))
3380 error_at (location, "%qT is not a template", type);
3381 else if (identifier_p (type))
3383 if (tag_type != none_type)
3384 error_at (location, "%qE is not a class template", type);
3385 else
3386 error_at (location, "%qE is not a template", type);
3388 else
3389 error_at (location, "invalid template-id");
3390 /* Remember the location of the invalid "<". */
3391 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3392 start = cp_lexer_token_position (parser->lexer, true);
3393 /* Consume the "<". */
3394 cp_lexer_consume_token (parser->lexer);
3395 /* Parse the template arguments. */
3396 cp_parser_enclosed_template_argument_list (parser);
3397 /* Permanently remove the invalid template arguments so that
3398 this error message is not issued again. */
3399 if (start)
3400 cp_lexer_purge_tokens_after (parser->lexer, start);
3404 /* If parsing an integral constant-expression, issue an error message
3405 about the fact that THING appeared and return true. Otherwise,
3406 return false. In either case, set
3407 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3409 static bool
3410 cp_parser_non_integral_constant_expression (cp_parser *parser,
3411 non_integral_constant thing)
3413 parser->non_integral_constant_expression_p = true;
3414 if (parser->integral_constant_expression_p)
3416 if (!parser->allow_non_integral_constant_expression_p)
3418 const char *msg = NULL;
3419 switch (thing)
3421 case NIC_FLOAT:
3422 pedwarn (input_location, OPT_Wpedantic,
3423 "ISO C++ forbids using a floating-point literal "
3424 "in a constant-expression");
3425 return true;
3426 case NIC_CAST:
3427 error ("a cast to a type other than an integral or "
3428 "enumeration type cannot appear in a "
3429 "constant-expression");
3430 return true;
3431 case NIC_TYPEID:
3432 error ("%<typeid%> operator "
3433 "cannot appear in a constant-expression");
3434 return true;
3435 case NIC_NCC:
3436 error ("non-constant compound literals "
3437 "cannot appear in a constant-expression");
3438 return true;
3439 case NIC_FUNC_CALL:
3440 error ("a function call "
3441 "cannot appear in a constant-expression");
3442 return true;
3443 case NIC_INC:
3444 error ("an increment "
3445 "cannot appear in a constant-expression");
3446 return true;
3447 case NIC_DEC:
3448 error ("an decrement "
3449 "cannot appear in a constant-expression");
3450 return true;
3451 case NIC_ARRAY_REF:
3452 error ("an array reference "
3453 "cannot appear in a constant-expression");
3454 return true;
3455 case NIC_ADDR_LABEL:
3456 error ("the address of a label "
3457 "cannot appear in a constant-expression");
3458 return true;
3459 case NIC_OVERLOADED:
3460 error ("calls to overloaded operators "
3461 "cannot appear in a constant-expression");
3462 return true;
3463 case NIC_ASSIGNMENT:
3464 error ("an assignment cannot appear in a constant-expression");
3465 return true;
3466 case NIC_COMMA:
3467 error ("a comma operator "
3468 "cannot appear in a constant-expression");
3469 return true;
3470 case NIC_CONSTRUCTOR:
3471 error ("a call to a constructor "
3472 "cannot appear in a constant-expression");
3473 return true;
3474 case NIC_TRANSACTION:
3475 error ("a transaction expression "
3476 "cannot appear in a constant-expression");
3477 return true;
3478 case NIC_THIS:
3479 msg = "this";
3480 break;
3481 case NIC_FUNC_NAME:
3482 msg = "__FUNCTION__";
3483 break;
3484 case NIC_PRETTY_FUNC:
3485 msg = "__PRETTY_FUNCTION__";
3486 break;
3487 case NIC_C99_FUNC:
3488 msg = "__func__";
3489 break;
3490 case NIC_VA_ARG:
3491 msg = "va_arg";
3492 break;
3493 case NIC_ARROW:
3494 msg = "->";
3495 break;
3496 case NIC_POINT:
3497 msg = ".";
3498 break;
3499 case NIC_STAR:
3500 msg = "*";
3501 break;
3502 case NIC_ADDR:
3503 msg = "&";
3504 break;
3505 case NIC_PREINCREMENT:
3506 msg = "++";
3507 break;
3508 case NIC_PREDECREMENT:
3509 msg = "--";
3510 break;
3511 case NIC_NEW:
3512 msg = "new";
3513 break;
3514 case NIC_DEL:
3515 msg = "delete";
3516 break;
3517 default:
3518 gcc_unreachable ();
3520 if (msg)
3521 error ("%qs cannot appear in a constant-expression", msg);
3522 return true;
3525 return false;
3528 /* Emit a diagnostic for an invalid type name. This function commits
3529 to the current active tentative parse, if any. (Otherwise, the
3530 problematic construct might be encountered again later, resulting
3531 in duplicate error messages.) LOCATION is the location of ID. */
3533 static void
3534 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3535 location_t location)
3537 tree decl, ambiguous_decls;
3538 cp_parser_commit_to_tentative_parse (parser);
3539 /* Try to lookup the identifier. */
3540 decl = cp_parser_lookup_name (parser, id, none_type,
3541 /*is_template=*/false,
3542 /*is_namespace=*/false,
3543 /*check_dependency=*/true,
3544 &ambiguous_decls, location);
3545 if (ambiguous_decls)
3546 /* If the lookup was ambiguous, an error will already have
3547 been issued. */
3548 return;
3549 /* If the lookup found a template-name, it means that the user forgot
3550 to specify an argument list. Emit a useful error message. */
3551 if (DECL_TYPE_TEMPLATE_P (decl))
3553 auto_diagnostic_group d;
3554 error_at (location,
3555 "invalid use of template-name %qE without an argument list",
3556 decl);
3557 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx17)
3558 inform (location, "class template argument deduction is only available "
3559 "with %<-std=c++17%> or %<-std=gnu++17%>");
3560 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3562 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3563 error_at (location, "invalid use of destructor %qD as a type", id);
3564 else if (TREE_CODE (decl) == TYPE_DECL)
3565 /* Something like 'unsigned A a;' */
3566 error_at (location, "invalid combination of multiple type-specifiers");
3567 else if (!parser->scope)
3569 /* Issue an error message. */
3570 auto_diagnostic_group d;
3571 name_hint hint;
3572 if (TREE_CODE (id) == IDENTIFIER_NODE)
3573 hint = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME, location);
3574 if (const char *suggestion = hint.suggestion ())
3576 gcc_rich_location richloc (location);
3577 richloc.add_fixit_replace (suggestion);
3578 error_at (&richloc,
3579 "%qE does not name a type; did you mean %qs?",
3580 id, suggestion);
3582 else
3583 error_at (location, "%qE does not name a type", id);
3584 /* If we're in a template class, it's possible that the user was
3585 referring to a type from a base class. For example:
3587 template <typename T> struct A { typedef T X; };
3588 template <typename T> struct B : public A<T> { X x; };
3590 The user should have said "typename A<T>::X". */
3591 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3592 inform (location, "C++11 %<constexpr%> only available with "
3593 "%<-std=c++11%> or %<-std=gnu++11%>");
3594 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3595 inform (location, "C++11 %<noexcept%> only available with "
3596 "%<-std=c++11%> or %<-std=gnu++11%>");
3597 else if (TREE_CODE (id) == IDENTIFIER_NODE
3598 && (id_equal (id, "module") || id_equal (id, "import")))
3600 if (modules_p ())
3601 inform (location, "%qE is not recognized as a module control-line",
3602 id);
3603 else if (cxx_dialect < cxx20)
3604 inform (location, "C++20 %qE only available with %<-fmodules-ts%>",
3605 id);
3606 else
3607 inform (location, "C++20 %qE only available with %<-fmodules-ts%>"
3608 ", which is not yet enabled with %<-std=c++20%>", id);
3610 else if (cxx_dialect < cxx11
3611 && TREE_CODE (id) == IDENTIFIER_NODE
3612 && id_equal (id, "thread_local"))
3613 inform (location, "C++11 %<thread_local%> only available with "
3614 "%<-std=c++11%> or %<-std=gnu++11%>");
3615 else if (cxx_dialect < cxx20 && id == ridpointers[(int)RID_CONSTINIT])
3616 inform (location, "C++20 %<constinit%> only available with "
3617 "%<-std=c++20%> or %<-std=gnu++20%>");
3618 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3619 inform (location, "%<concept%> only available with %<-std=c++20%> or "
3620 "%<-fconcepts%>");
3621 else if (!flag_concepts && id == ridpointers[(int)RID_REQUIRES])
3622 inform (location, "%<requires%> only available with %<-std=c++20%> or "
3623 "%<-fconcepts%>");
3624 else if (processing_template_decl && current_class_type
3625 && TYPE_BINFO (current_class_type))
3627 for (tree b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3628 b; b = TREE_CHAIN (b))
3630 tree base_type = BINFO_TYPE (b);
3631 if (CLASS_TYPE_P (base_type)
3632 && dependent_type_p (base_type))
3634 /* Go from a particular instantiation of the
3635 template (which will have an empty TYPE_FIELDs),
3636 to the main version. */
3637 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3638 for (tree field = TYPE_FIELDS (base_type);
3639 field; field = DECL_CHAIN (field))
3640 if (TREE_CODE (field) == TYPE_DECL
3641 && DECL_NAME (field) == id)
3643 inform (location,
3644 "(perhaps %<typename %T::%E%> was intended)",
3645 BINFO_TYPE (b), id);
3646 goto found;
3650 found:;
3653 /* Here we diagnose qualified-ids where the scope is actually correct,
3654 but the identifier does not resolve to a valid type name. */
3655 else if (parser->scope != error_mark_node)
3657 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3659 auto_diagnostic_group d;
3660 name_hint hint;
3661 if (decl == error_mark_node)
3662 hint = suggest_alternative_in_explicit_scope (location, id,
3663 parser->scope);
3664 const char *suggestion = hint.suggestion ();
3665 gcc_rich_location richloc (location_of (id));
3666 if (suggestion)
3667 richloc.add_fixit_replace (suggestion);
3668 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3670 if (suggestion)
3671 error_at (&richloc,
3672 "%qE in namespace %qE does not name a template"
3673 " type; did you mean %qs?",
3674 id, parser->scope, suggestion);
3675 else
3676 error_at (&richloc,
3677 "%qE in namespace %qE does not name a template type",
3678 id, parser->scope);
3680 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3682 if (suggestion)
3683 error_at (&richloc,
3684 "%qE in namespace %qE does not name a template"
3685 " type; did you mean %qs?",
3686 TREE_OPERAND (id, 0), parser->scope, suggestion);
3687 else
3688 error_at (&richloc,
3689 "%qE in namespace %qE does not name a template"
3690 " type",
3691 TREE_OPERAND (id, 0), parser->scope);
3693 else
3695 if (suggestion)
3696 error_at (&richloc,
3697 "%qE in namespace %qE does not name a type"
3698 "; did you mean %qs?",
3699 id, parser->scope, suggestion);
3700 else
3701 error_at (&richloc,
3702 "%qE in namespace %qE does not name a type",
3703 id, parser->scope);
3705 if (DECL_P (decl))
3706 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3708 else if (CLASS_TYPE_P (parser->scope)
3709 && constructor_name_p (id, parser->scope))
3711 /* A<T>::A<T>() */
3712 auto_diagnostic_group d;
3713 error_at (location, "%<%T::%E%> names the constructor, not"
3714 " the type", parser->scope, id);
3715 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3716 error_at (location, "and %qT has no template constructors",
3717 parser->scope);
3719 else if (TYPE_P (parser->scope)
3720 && dependent_scope_p (parser->scope))
3722 gcc_rich_location richloc (location);
3723 richloc.add_fixit_insert_before ("typename ");
3724 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3725 error_at (&richloc,
3726 "need %<typename%> before %<%T::%D::%E%> because "
3727 "%<%T::%D%> is a dependent scope",
3728 TYPE_CONTEXT (parser->scope),
3729 TYPENAME_TYPE_FULLNAME (parser->scope),
3731 TYPE_CONTEXT (parser->scope),
3732 TYPENAME_TYPE_FULLNAME (parser->scope));
3733 else
3734 error_at (&richloc, "need %<typename%> before %<%T::%E%> because "
3735 "%qT is a dependent scope",
3736 parser->scope, id, parser->scope);
3738 else if (TYPE_P (parser->scope))
3740 auto_diagnostic_group d;
3741 if (!COMPLETE_TYPE_P (parser->scope))
3742 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3743 parser->scope);
3744 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3745 error_at (location_of (id),
3746 "%qE in %q#T does not name a template type",
3747 id, parser->scope);
3748 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3749 error_at (location_of (id),
3750 "%qE in %q#T does not name a template type",
3751 TREE_OPERAND (id, 0), parser->scope);
3752 else
3753 error_at (location_of (id),
3754 "%qE in %q#T does not name a type",
3755 id, parser->scope);
3756 if (DECL_P (decl))
3757 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3759 else
3760 gcc_unreachable ();
3764 /* Check for a common situation where a type-name should be present,
3765 but is not, and issue a sensible error message. Returns true if an
3766 invalid type-name was detected.
3768 The situation handled by this function are variable declarations of the
3769 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3770 Usually, `ID' should name a type, but if we got here it means that it
3771 does not. We try to emit the best possible error message depending on
3772 how exactly the id-expression looks like. */
3774 static bool
3775 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3777 tree id;
3778 cp_token *token = cp_lexer_peek_token (parser->lexer);
3780 /* Avoid duplicate error about ambiguous lookup. */
3781 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3783 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3784 if (next->type == CPP_NAME && next->error_reported)
3785 goto out;
3788 cp_parser_parse_tentatively (parser);
3789 id = cp_parser_id_expression (parser,
3790 /*template_keyword_p=*/false,
3791 /*check_dependency_p=*/true,
3792 /*template_p=*/NULL,
3793 /*declarator_p=*/true,
3794 /*optional_p=*/false);
3795 /* If the next token is a (, this is a function with no explicit return
3796 type, i.e. constructor, destructor or conversion op. */
3797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3798 || TREE_CODE (id) == TYPE_DECL)
3800 cp_parser_abort_tentative_parse (parser);
3801 return false;
3803 if (!cp_parser_parse_definitely (parser))
3804 return false;
3806 /* Emit a diagnostic for the invalid type. */
3807 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3808 out:
3809 /* If we aren't in the middle of a declarator (i.e. in a
3810 parameter-declaration-clause), skip to the end of the declaration;
3811 there's no point in trying to process it. */
3812 if (!parser->in_declarator_p)
3813 cp_parser_skip_to_end_of_block_or_statement (parser);
3814 return true;
3817 /* Consume tokens up to, and including, the next non-nested closing `)'.
3818 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3819 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3820 found an unnested token of that type. */
3822 static int
3823 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3824 bool recovering,
3825 cpp_ttype or_ttype,
3826 bool consume_paren)
3828 unsigned paren_depth = 0;
3829 unsigned brace_depth = 0;
3830 unsigned square_depth = 0;
3831 unsigned condop_depth = 0;
3833 if (recovering && or_ttype == CPP_EOF
3834 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3835 return 0;
3837 while (true)
3839 cp_token * token = cp_lexer_peek_token (parser->lexer);
3841 /* Have we found what we're looking for before the closing paren? */
3842 if (token->type == or_ttype && or_ttype != CPP_EOF
3843 && !brace_depth && !paren_depth && !square_depth && !condop_depth)
3844 return -1;
3846 switch (token->type)
3848 case CPP_PRAGMA_EOL:
3849 if (!parser->lexer->in_pragma)
3850 break;
3851 /* FALLTHRU */
3852 case CPP_EOF:
3853 /* If we've run out of tokens, then there is no closing `)'. */
3854 return 0;
3856 /* This is good for lambda expression capture-lists. */
3857 case CPP_OPEN_SQUARE:
3858 ++square_depth;
3859 break;
3860 case CPP_CLOSE_SQUARE:
3861 if (!square_depth--)
3862 return 0;
3863 break;
3865 case CPP_SEMICOLON:
3866 /* This matches the processing in skip_to_end_of_statement. */
3867 if (!brace_depth)
3868 return 0;
3869 break;
3871 case CPP_OPEN_BRACE:
3872 ++brace_depth;
3873 break;
3874 case CPP_CLOSE_BRACE:
3875 if (!brace_depth--)
3876 return 0;
3877 break;
3879 case CPP_OPEN_PAREN:
3880 if (!brace_depth)
3881 ++paren_depth;
3882 break;
3884 case CPP_CLOSE_PAREN:
3885 if (!brace_depth && !paren_depth--)
3887 if (consume_paren)
3888 cp_lexer_consume_token (parser->lexer);
3889 return 1;
3891 break;
3893 case CPP_QUERY:
3894 if (!brace_depth && !paren_depth && !square_depth)
3895 ++condop_depth;
3896 break;
3898 case CPP_COLON:
3899 if (!brace_depth && !paren_depth && !square_depth && condop_depth > 0)
3900 condop_depth--;
3901 break;
3903 case CPP_KEYWORD:
3904 if (!cp_token_is_module_directive (token))
3905 break;
3906 /* FALLTHROUGH */
3908 case CPP_PRAGMA:
3909 /* We fell into a pragma. Skip it, and continue. */
3910 cp_parser_skip_to_pragma_eol (parser, recovering ? token : nullptr);
3911 continue;
3913 default:
3914 break;
3917 /* Consume the token. */
3918 cp_lexer_consume_token (parser->lexer);
3922 /* Consume tokens up to, and including, the next non-nested closing `)'.
3923 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3924 are doing error recovery. Returns -1 if OR_COMMA is true and we
3925 found an unnested token of that type. */
3927 static int
3928 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3929 bool recovering,
3930 bool or_comma,
3931 bool consume_paren)
3933 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3934 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3935 ttype, consume_paren);
3938 /* Consume tokens until we reach the end of the current statement.
3939 Normally, that will be just before consuming a `;'. However, if a
3940 non-nested `}' comes first, then we stop before consuming that. */
3942 static void
3943 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3945 unsigned nesting_depth = 0;
3947 /* Unwind generic function template scope if necessary. */
3948 if (parser->fully_implicit_function_template_p)
3949 abort_fully_implicit_template (parser);
3951 while (true)
3953 cp_token *token = cp_lexer_peek_token (parser->lexer);
3955 switch (token->type)
3957 case CPP_PRAGMA_EOL:
3958 if (!parser->lexer->in_pragma)
3959 break;
3960 /* FALLTHRU */
3961 case CPP_EOF:
3962 /* If we've run out of tokens, stop. */
3963 return;
3965 case CPP_SEMICOLON:
3966 /* If the next token is a `;', we have reached the end of the
3967 statement. */
3968 if (!nesting_depth)
3969 return;
3970 break;
3972 case CPP_CLOSE_BRACE:
3973 /* If this is a non-nested '}', stop before consuming it.
3974 That way, when confronted with something like:
3976 { 3 + }
3978 we stop before consuming the closing '}', even though we
3979 have not yet reached a `;'. */
3980 if (nesting_depth == 0)
3981 return;
3983 /* If it is the closing '}' for a block that we have
3984 scanned, stop -- but only after consuming the token.
3985 That way given:
3987 void f g () { ... }
3988 typedef int I;
3990 we will stop after the body of the erroneously declared
3991 function, but before consuming the following `typedef'
3992 declaration. */
3993 if (--nesting_depth == 0)
3995 cp_lexer_consume_token (parser->lexer);
3996 return;
3998 break;
4000 case CPP_OPEN_BRACE:
4001 ++nesting_depth;
4002 break;
4004 case CPP_KEYWORD:
4005 if (!cp_token_is_module_directive (token))
4006 break;
4007 /* FALLTHROUGH */
4009 case CPP_PRAGMA:
4010 /* We fell into a pragma. Skip it, and continue or return. */
4011 cp_parser_skip_to_pragma_eol (parser, token);
4012 if (!nesting_depth)
4013 return;
4014 continue;
4016 default:
4017 break;
4020 /* Consume the token. */
4021 cp_lexer_consume_token (parser->lexer);
4025 /* This function is called at the end of a statement or declaration.
4026 If the next token is a semicolon, it is consumed; otherwise, error
4027 recovery is attempted. */
4029 static void
4030 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
4032 /* Look for the trailing `;'. */
4033 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
4035 /* If there is additional (erroneous) input, skip to the end of
4036 the statement. */
4037 cp_parser_skip_to_end_of_statement (parser);
4038 /* If the next token is now a `;', consume it. */
4039 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
4040 cp_lexer_consume_token (parser->lexer);
4044 /* Skip tokens until we have consumed an entire block, or until we
4045 have consumed a non-nested `;'. */
4047 static void
4048 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
4050 int nesting_depth = 0;
4052 /* Unwind generic function template scope if necessary. */
4053 if (parser->fully_implicit_function_template_p)
4054 abort_fully_implicit_template (parser);
4056 while (nesting_depth >= 0)
4058 cp_token *token = cp_lexer_peek_token (parser->lexer);
4060 switch (token->type)
4062 case CPP_PRAGMA_EOL:
4063 if (!parser->lexer->in_pragma)
4064 break;
4065 /* FALLTHRU */
4066 case CPP_EOF:
4067 /* If we've run out of tokens, stop. */
4068 return;
4070 case CPP_SEMICOLON:
4071 /* Stop if this is an unnested ';'. */
4072 if (!nesting_depth)
4073 nesting_depth = -1;
4074 break;
4076 case CPP_CLOSE_BRACE:
4077 /* Stop if this is an unnested '}', or closes the outermost
4078 nesting level. */
4079 nesting_depth--;
4080 if (nesting_depth < 0)
4081 return;
4082 if (!nesting_depth)
4083 nesting_depth = -1;
4084 break;
4086 case CPP_OPEN_BRACE:
4087 /* Nest. */
4088 nesting_depth++;
4089 break;
4091 case CPP_KEYWORD:
4092 if (!cp_token_is_module_directive (token))
4093 break;
4094 /* FALLTHROUGH */
4096 case CPP_PRAGMA:
4097 /* Skip it, and continue or return. */
4098 cp_parser_skip_to_pragma_eol (parser, token);
4099 if (!nesting_depth)
4100 return;
4101 continue;
4103 default:
4104 break;
4107 /* Consume the token. */
4108 cp_lexer_consume_token (parser->lexer);
4112 /* Skip tokens until a non-nested closing curly brace is the next
4113 token, or there are no more tokens. Return true in the first case,
4114 false otherwise. */
4116 static bool
4117 cp_parser_skip_to_closing_brace (cp_parser *parser)
4119 unsigned nesting_depth = 0;
4121 while (true)
4123 cp_token *token = cp_lexer_peek_token (parser->lexer);
4125 switch (token->type)
4127 case CPP_PRAGMA_EOL:
4128 if (!parser->lexer->in_pragma)
4129 break;
4130 /* FALLTHRU */
4131 case CPP_EOF:
4132 /* If we've run out of tokens, stop. */
4133 return false;
4135 case CPP_CLOSE_BRACE:
4136 /* If the next token is a non-nested `}', then we have reached
4137 the end of the current block. */
4138 if (nesting_depth-- == 0)
4139 return true;
4140 break;
4142 case CPP_OPEN_BRACE:
4143 /* If it the next token is a `{', then we are entering a new
4144 block. Consume the entire block. */
4145 ++nesting_depth;
4146 break;
4148 default:
4149 break;
4152 /* Consume the token. */
4153 cp_lexer_consume_token (parser->lexer);
4157 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4158 parameter is the PRAGMA token, allowing us to purge the entire pragma
4159 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4160 forwards (not error recovery). */
4162 static void
4163 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
4165 cp_token *token;
4169 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4170 before an EOF token, even when the EOF is on the pragma line.
4171 We should never get here without being inside a deferred
4172 pragma. */
4173 gcc_checking_assert (cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
4174 token = cp_lexer_consume_token (parser->lexer);
4176 while (token->type != CPP_PRAGMA_EOL);
4178 if (pragma_tok)
4180 parser->lexer->in_pragma = false;
4181 if (parser->lexer->in_omp_attribute_pragma
4182 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4184 parser->lexer = parser->lexer->next;
4185 /* Put the current source position back where it was before this
4186 lexer was pushed. */
4187 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4192 /* Require pragma end of line, resyncing with it as necessary. The
4193 arguments are as for cp_parser_skip_to_pragma_eol. */
4195 static void
4196 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
4198 parser->lexer->in_pragma = false;
4199 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
4200 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
4201 else if (parser->lexer->in_omp_attribute_pragma
4202 && cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4204 parser->lexer = parser->lexer->next;
4205 /* Put the current source position back where it was before this
4206 lexer was pushed. */
4207 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4211 /* This is a simple wrapper around make_typename_type. When the id is
4212 an unresolved identifier node, we can provide a superior diagnostic
4213 using cp_parser_diagnose_invalid_type_name. */
4215 static tree
4216 cp_parser_make_typename_type (cp_parser *parser, tree id,
4217 location_t id_location)
4219 tree result;
4220 if (identifier_p (id))
4222 result = make_typename_type (parser->scope, id, typename_type,
4223 /*complain=*/tf_none);
4224 if (result == error_mark_node)
4225 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
4226 return result;
4228 return make_typename_type (parser->scope, id, typename_type, tf_error);
4231 /* This is a wrapper around the
4232 make_{pointer,ptrmem,reference}_declarator functions that decides
4233 which one to call based on the CODE and CLASS_TYPE arguments. The
4234 CODE argument should be one of the values returned by
4235 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4236 appertain to the pointer or reference. */
4238 static cp_declarator *
4239 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
4240 cp_cv_quals cv_qualifiers,
4241 cp_declarator *target,
4242 tree attributes)
4244 if (code == ERROR_MARK || target == cp_error_declarator)
4245 return cp_error_declarator;
4247 if (code == INDIRECT_REF)
4248 if (class_type == NULL_TREE)
4249 return make_pointer_declarator (cv_qualifiers, target, attributes);
4250 else
4251 return make_ptrmem_declarator (cv_qualifiers, class_type,
4252 target, attributes);
4253 else if (code == ADDR_EXPR && class_type == NULL_TREE)
4254 return make_reference_declarator (cv_qualifiers, target,
4255 false, attributes);
4256 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
4257 return make_reference_declarator (cv_qualifiers, target,
4258 true, attributes);
4259 gcc_unreachable ();
4262 /* Create a new C++ parser. */
4264 static cp_parser *
4265 cp_parser_new (cp_lexer *lexer)
4267 /* Initialize the binops_by_token so that we can get the tree
4268 directly from the token. */
4269 for (unsigned i = 0; i < ARRAY_SIZE (binops); i++)
4270 binops_by_token[binops[i].token_type] = binops[i];
4272 cp_parser *parser = ggc_cleared_alloc<cp_parser> ();
4273 parser->lexer = lexer;
4274 parser->context = cp_parser_context_new (NULL);
4276 /* For now, we always accept GNU extensions. */
4277 parser->allow_gnu_extensions_p = 1;
4279 /* The `>' token is a greater-than operator, not the end of a
4280 template-id. */
4281 parser->greater_than_is_operator_p = true;
4283 parser->default_arg_ok_p = true;
4285 /* We are not parsing a constant-expression. */
4286 parser->integral_constant_expression_p = false;
4287 parser->allow_non_integral_constant_expression_p = false;
4288 parser->non_integral_constant_expression_p = false;
4290 /* Local variable names are not forbidden. */
4291 parser->local_variables_forbidden_p = 0;
4293 /* We are not processing an `extern "C"' declaration. */
4294 parser->in_unbraced_linkage_specification_p = false;
4296 /* We are not processing a declarator. */
4297 parser->in_declarator_p = false;
4299 /* We are not processing a template-argument-list. */
4300 parser->in_template_argument_list_p = false;
4302 /* We are not in an iteration statement. */
4303 parser->in_statement = 0;
4305 /* We are not in a switch statement. */
4306 parser->in_switch_statement_p = false;
4308 /* We are not parsing a type-id inside an expression. */
4309 parser->in_type_id_in_expr_p = false;
4311 /* String literals should be translated to the execution character set. */
4312 parser->translate_strings_p = true;
4314 /* We are not parsing a function body. */
4315 parser->in_function_body = false;
4317 /* We can correct until told otherwise. */
4318 parser->colon_corrects_to_scope_p = true;
4320 /* The unparsed function queue is empty. */
4321 push_unparsed_function_queues (parser);
4323 /* There are no classes being defined. */
4324 parser->num_classes_being_defined = 0;
4326 /* No template parameters apply. */
4327 parser->num_template_parameter_lists = 0;
4329 /* Special parsing data structures. */
4330 parser->omp_declare_simd = NULL;
4331 parser->oacc_routine = NULL;
4333 /* Not declaring an implicit function template. */
4334 parser->auto_is_implicit_function_template_parm_p = false;
4335 parser->fully_implicit_function_template_p = false;
4336 parser->implicit_template_parms = 0;
4337 parser->implicit_template_scope = 0;
4339 /* Allow constrained-type-specifiers. */
4340 parser->prevent_constrained_type_specifiers = 0;
4342 /* We haven't yet seen an 'extern "C"'. */
4343 parser->innermost_linkage_specification_location = UNKNOWN_LOCATION;
4345 return parser;
4348 /* Create a cp_lexer structure which will emit the tokens in CACHE
4349 and push it onto the parser's lexer stack. This is used for delayed
4350 parsing of in-class method bodies and default arguments, and should
4351 not be confused with tentative parsing. */
4352 static void
4353 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
4355 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
4356 lexer->next = parser->lexer;
4357 parser->lexer = lexer;
4359 /* Move the current source position to that of the first token in the
4360 new lexer. */
4361 cp_lexer_set_source_position_from_token (lexer->next_token);
4364 /* Pop the top lexer off the parser stack. This is never used for the
4365 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4366 static void
4367 cp_parser_pop_lexer (cp_parser *parser)
4369 cp_lexer *lexer = parser->lexer;
4370 parser->lexer = lexer->next;
4371 cp_lexer_destroy (lexer);
4373 /* Put the current source position back where it was before this
4374 lexer was pushed. */
4375 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
4378 /* Lexical conventions [gram.lex] */
4380 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4381 identifier. */
4383 static cp_expr
4384 cp_parser_identifier (cp_parser* parser)
4386 cp_token *token;
4388 /* Look for the identifier. */
4389 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
4390 /* Return the value. */
4391 if (token)
4392 return cp_expr (token->u.value, token->location);
4393 else
4394 return error_mark_node;
4397 /* Parse a sequence of adjacent string constants. Returns a
4398 TREE_STRING representing the combined, nul-terminated string
4399 constant. If TRANSLATE is true, translate the string to the
4400 execution character set. If WIDE_OK is true, a wide string is
4401 invalid here.
4403 C++98 [lex.string] says that if a narrow string literal token is
4404 adjacent to a wide string literal token, the behavior is undefined.
4405 However, C99 6.4.5p4 says that this results in a wide string literal.
4406 We follow C99 here, for consistency with the C front end.
4408 This code is largely lifted from lex_string() in c-lex.cc.
4410 FUTURE: ObjC++ will need to handle @-strings here. */
4411 static cp_expr
4412 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
4413 bool lookup_udlit = true)
4415 tree value;
4416 size_t count;
4417 struct obstack str_ob;
4418 struct obstack loc_ob;
4419 cpp_string str, istr, *strs;
4420 cp_token *tok;
4421 enum cpp_ttype type, curr_type;
4422 int have_suffix_p = 0;
4423 tree string_tree;
4424 tree suffix_id = NULL_TREE;
4425 bool curr_tok_is_userdef_p = false;
4427 tok = cp_lexer_peek_token (parser->lexer);
4428 if (!cp_parser_is_string_literal (tok))
4430 cp_parser_error (parser, "expected string-literal");
4431 return error_mark_node;
4434 location_t loc = tok->location;
4436 if (cpp_userdef_string_p (tok->type))
4438 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4439 curr_type = cpp_userdef_string_remove_type (tok->type);
4440 curr_tok_is_userdef_p = true;
4442 else
4444 string_tree = tok->u.value;
4445 curr_type = tok->type;
4447 type = curr_type;
4449 /* Try to avoid the overhead of creating and destroying an obstack
4450 for the common case of just one string. */
4451 if (!cp_parser_is_string_literal
4452 (cp_lexer_peek_nth_token (parser->lexer, 2)))
4454 cp_lexer_consume_token (parser->lexer);
4456 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4457 str.len = TREE_STRING_LENGTH (string_tree);
4458 count = 1;
4460 if (curr_tok_is_userdef_p)
4462 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4463 have_suffix_p = 1;
4464 curr_type = cpp_userdef_string_remove_type (tok->type);
4466 else
4467 curr_type = tok->type;
4469 strs = &str;
4471 else
4473 location_t last_tok_loc = tok->location;
4474 gcc_obstack_init (&str_ob);
4475 gcc_obstack_init (&loc_ob);
4476 count = 0;
4480 cp_lexer_consume_token (parser->lexer);
4481 count++;
4482 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
4483 str.len = TREE_STRING_LENGTH (string_tree);
4485 if (curr_tok_is_userdef_p)
4487 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
4488 if (have_suffix_p == 0)
4490 suffix_id = curr_suffix_id;
4491 have_suffix_p = 1;
4493 else if (have_suffix_p == 1
4494 && curr_suffix_id != suffix_id)
4496 error ("inconsistent user-defined literal suffixes"
4497 " %qD and %qD in string literal",
4498 suffix_id, curr_suffix_id);
4499 have_suffix_p = -1;
4501 curr_type = cpp_userdef_string_remove_type (tok->type);
4503 else
4504 curr_type = tok->type;
4506 if (type != curr_type)
4508 if (type == CPP_STRING)
4509 type = curr_type;
4510 else if (curr_type != CPP_STRING)
4512 rich_location rich_loc (line_table, tok->location);
4513 rich_loc.add_range (last_tok_loc);
4514 error_at (&rich_loc,
4515 "concatenation of string literals with "
4516 "conflicting encoding prefixes");
4520 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4521 obstack_grow (&loc_ob, &tok->location, sizeof (location_t));
4523 last_tok_loc = tok->location;
4525 tok = cp_lexer_peek_token (parser->lexer);
4526 if (cpp_userdef_string_p (tok->type))
4528 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4529 curr_type = cpp_userdef_string_remove_type (tok->type);
4530 curr_tok_is_userdef_p = true;
4532 else
4534 string_tree = tok->u.value;
4535 curr_type = tok->type;
4536 curr_tok_is_userdef_p = false;
4539 while (cp_parser_is_string_literal (tok));
4541 /* A string literal built by concatenation has its caret=start at
4542 the start of the initial string, and its finish at the finish of
4543 the final string literal. */
4544 loc = make_location (loc, loc, get_finish (last_tok_loc));
4546 strs = (cpp_string *) obstack_finish (&str_ob);
4549 if (type != CPP_STRING && !wide_ok)
4551 cp_parser_error (parser, "a wide string is invalid in this context");
4552 type = CPP_STRING;
4555 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4556 (parse_in, strs, count, &istr, type))
4558 value = build_string (istr.len, (const char *)istr.text);
4559 free (CONST_CAST (unsigned char *, istr.text));
4560 if (count > 1)
4562 location_t *locs = (location_t *)obstack_finish (&loc_ob);
4563 gcc_assert (g_string_concat_db);
4564 g_string_concat_db->record_string_concatenation (count, locs);
4567 switch (type)
4569 default:
4570 case CPP_STRING:
4571 TREE_TYPE (value) = char_array_type_node;
4572 break;
4573 case CPP_UTF8STRING:
4574 if (flag_char8_t)
4575 TREE_TYPE (value) = char8_array_type_node;
4576 else
4577 TREE_TYPE (value) = char_array_type_node;
4578 break;
4579 case CPP_STRING16:
4580 TREE_TYPE (value) = char16_array_type_node;
4581 break;
4582 case CPP_STRING32:
4583 TREE_TYPE (value) = char32_array_type_node;
4584 break;
4585 case CPP_WSTRING:
4586 TREE_TYPE (value) = wchar_array_type_node;
4587 break;
4590 value = fix_string_type (value);
4592 if (have_suffix_p)
4594 tree literal = build_userdef_literal (suffix_id, value,
4595 OT_NONE, NULL_TREE);
4596 if (lookup_udlit)
4597 value = cp_parser_userdef_string_literal (literal);
4598 else
4599 value = literal;
4602 else
4603 /* cpp_interpret_string has issued an error. */
4604 value = error_mark_node;
4606 if (count > 1)
4608 obstack_free (&str_ob, 0);
4609 obstack_free (&loc_ob, 0);
4612 return cp_expr (value, loc);
4615 /* Look up a literal operator with the name and the exact arguments. */
4617 static tree
4618 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4620 tree decl = lookup_name (name);
4621 if (!decl || !is_overloaded_fn (decl))
4622 return error_mark_node;
4624 for (lkp_iterator iter (decl); iter; ++iter)
4626 tree fn = *iter;
4628 if (tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn)))
4630 unsigned int ix;
4631 bool found = true;
4633 for (ix = 0;
4634 found && ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4635 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4637 tree tparm = TREE_VALUE (parmtypes);
4638 tree targ = TREE_TYPE ((*args)[ix]);
4639 bool ptr = TYPE_PTR_P (tparm);
4640 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4641 if ((ptr || arr || !same_type_p (tparm, targ))
4642 && (!ptr || !arr
4643 || !same_type_p (TREE_TYPE (tparm),
4644 TREE_TYPE (targ))))
4645 found = false;
4648 if (found
4649 && ix == vec_safe_length (args)
4650 /* May be this should be sufficient_parms_p instead,
4651 depending on how exactly should user-defined literals
4652 work in presence of default arguments on the literal
4653 operator parameters. */
4654 && parmtypes == void_list_node)
4655 return decl;
4659 return error_mark_node;
4662 /* Parse a user-defined char constant. Returns a call to a user-defined
4663 literal operator taking the character as an argument. */
4665 static cp_expr
4666 cp_parser_userdef_char_literal (cp_parser *parser)
4668 cp_token *token = cp_lexer_consume_token (parser->lexer);
4669 tree literal = token->u.value;
4670 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4671 tree value = USERDEF_LITERAL_VALUE (literal);
4672 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4673 tree decl, result;
4675 /* Build up a call to the user-defined operator */
4676 /* Lookup the name we got back from the id-expression. */
4677 releasing_vec args;
4678 vec_safe_push (args, value);
4679 decl = lookup_literal_operator (name, args);
4680 if (!decl || decl == error_mark_node)
4682 error ("unable to find character literal operator %qD with %qT argument",
4683 name, TREE_TYPE (value));
4684 return error_mark_node;
4686 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4687 return result;
4690 /* A subroutine of cp_parser_userdef_numeric_literal to
4691 create a char... template parameter pack from a string node. */
4693 static tree
4694 make_char_string_pack (tree value)
4696 tree charvec;
4697 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4698 const unsigned char *str
4699 = (const unsigned char *) TREE_STRING_POINTER (value);
4700 int i, len = TREE_STRING_LENGTH (value) - 1;
4701 tree argvec = make_tree_vec (1);
4703 /* Fill in CHARVEC with all of the parameters. */
4704 charvec = make_tree_vec (len);
4705 for (i = 0; i < len; ++i)
4707 unsigned char s[3] = { '\'', str[i], '\'' };
4708 cpp_string in = { 3, s };
4709 cpp_string out = { 0, 0 };
4710 if (!cpp_interpret_string (parse_in, &in, 1, &out, CPP_STRING))
4711 return NULL_TREE;
4712 gcc_assert (out.len == 2);
4713 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node,
4714 out.text[0]);
4717 /* Build the argument packs. */
4718 ARGUMENT_PACK_ARGS (argpack) = charvec;
4720 TREE_VEC_ELT (argvec, 0) = argpack;
4722 return argvec;
4725 /* A subroutine of cp_parser_userdef_numeric_literal to
4726 create a char... template parameter pack from a string node. */
4728 static tree
4729 make_string_pack (tree value)
4731 tree charvec;
4732 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4733 const unsigned char *str
4734 = (const unsigned char *) TREE_STRING_POINTER (value);
4735 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4736 int len = TREE_STRING_LENGTH (value) / sz - 1;
4737 tree argvec = make_tree_vec (2);
4739 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4740 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4742 /* First template parm is character type. */
4743 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4745 /* Fill in CHARVEC with all of the parameters. */
4746 charvec = make_tree_vec (len);
4747 for (int i = 0; i < len; ++i)
4748 TREE_VEC_ELT (charvec, i)
4749 = double_int_to_tree (str_char_type_node,
4750 double_int::from_buffer (str + i * sz, sz));
4752 /* Build the argument packs. */
4753 ARGUMENT_PACK_ARGS (argpack) = charvec;
4755 TREE_VEC_ELT (argvec, 1) = argpack;
4757 return argvec;
4760 /* Parse a user-defined numeric constant. returns a call to a user-defined
4761 literal operator. */
4763 static cp_expr
4764 cp_parser_userdef_numeric_literal (cp_parser *parser)
4766 cp_token *token = cp_lexer_consume_token (parser->lexer);
4767 tree literal = token->u.value;
4768 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4769 tree value = USERDEF_LITERAL_VALUE (literal);
4770 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4771 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4772 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4773 tree decl, result;
4775 /* Look for a literal operator taking the exact type of numeric argument
4776 as the literal value. */
4777 releasing_vec args;
4778 vec_safe_push (args, value);
4779 decl = lookup_literal_operator (name, args);
4780 if (decl && decl != error_mark_node)
4782 result = finish_call_expr (decl, &args, false, true,
4783 tf_warning_or_error);
4785 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4787 warning_at (token->location, OPT_Woverflow,
4788 "integer literal exceeds range of %qT type",
4789 long_long_unsigned_type_node);
4791 else
4793 if (overflow > 0)
4794 warning_at (token->location, OPT_Woverflow,
4795 "floating literal exceeds range of %qT type",
4796 long_double_type_node);
4797 else if (overflow < 0)
4798 warning_at (token->location, OPT_Woverflow,
4799 "floating literal truncated to zero");
4802 return result;
4805 /* If the numeric argument didn't work, look for a raw literal
4806 operator taking a const char* argument consisting of the number
4807 in string format. */
4808 args->truncate (0);
4809 vec_safe_push (args, num_string);
4810 decl = lookup_literal_operator (name, args);
4811 if (decl && decl != error_mark_node)
4813 result = finish_call_expr (decl, &args, false, true,
4814 tf_warning_or_error);
4815 return result;
4818 /* If the raw literal didn't work, look for a non-type template
4819 function with parameter pack char.... Call the function with
4820 template parameter characters representing the number. */
4821 args->truncate (0);
4822 decl = lookup_literal_operator (name, args);
4823 if (decl && decl != error_mark_node)
4825 tree tmpl_args = make_char_string_pack (num_string);
4826 if (tmpl_args == NULL_TREE)
4828 error ("failed to translate literal to execution character set %qT",
4829 num_string);
4830 return error_mark_node;
4832 decl = lookup_template_function (decl, tmpl_args);
4833 result = finish_call_expr (decl, &args, false, true,
4834 tf_warning_or_error);
4835 return result;
4838 /* In C++14 the standard library defines complex number suffixes that
4839 conflict with GNU extensions. Prefer them if <complex> is #included. */
4840 bool ext = cpp_get_options (parse_in)->ext_numeric_literals;
4841 bool i14 = (cxx_dialect > cxx11
4842 && (id_equal (suffix_id, "i")
4843 || id_equal (suffix_id, "if")
4844 || id_equal (suffix_id, "il")));
4845 diagnostic_t kind = DK_ERROR;
4846 int opt = 0;
4848 if (i14 && ext)
4850 tree cxlit = lookup_qualified_name (std_node, "complex_literals",
4851 LOOK_want::NORMAL, false);
4852 if (cxlit == error_mark_node)
4854 /* No <complex>, so pedwarn and use GNU semantics. */
4855 kind = DK_PEDWARN;
4856 opt = OPT_Wpedantic;
4860 bool complained
4861 = emit_diagnostic (kind, input_location, opt,
4862 "unable to find numeric literal operator %qD", name);
4864 if (!complained)
4865 /* Don't inform either. */;
4866 else if (i14)
4868 inform (token->location, "add %<using namespace std::complex_literals%> "
4869 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4870 "suffixes");
4871 if (ext)
4872 inform (token->location, "or use %<j%> instead of %<i%> for the "
4873 "GNU built-in suffix");
4875 else if (!ext)
4876 inform (token->location, "use %<-fext-numeric-literals%> "
4877 "to enable more built-in suffixes");
4879 if (kind == DK_ERROR)
4880 value = error_mark_node;
4881 else
4883 /* Use the built-in semantics. */
4884 tree type;
4885 if (id_equal (suffix_id, "i"))
4887 if (TREE_CODE (value) == INTEGER_CST)
4888 type = integer_type_node;
4889 else
4890 type = double_type_node;
4892 else if (id_equal (suffix_id, "if"))
4893 type = float_type_node;
4894 else /* if (id_equal (suffix_id, "il")) */
4895 type = long_double_type_node;
4897 value = fold_build2 (COMPLEX_EXPR, build_complex_type (type),
4898 build_zero_cst (type), fold_convert (type, value));
4901 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4902 /* Avoid repeated diagnostics. */
4903 token->u.value = value;
4904 return value;
4907 /* Parse a user-defined string constant. Returns a call to a user-defined
4908 literal operator taking a character pointer and the length of the string
4909 as arguments. */
4911 static tree
4912 cp_parser_userdef_string_literal (tree literal)
4914 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4915 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4916 tree value = USERDEF_LITERAL_VALUE (literal);
4917 int len = TREE_STRING_LENGTH (value)
4918 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4919 tree decl;
4921 /* Build up a call to the user-defined operator. */
4922 /* Lookup the name we got back from the id-expression. */
4923 releasing_vec args;
4924 vec_safe_push (args, value);
4925 vec_safe_push (args, build_int_cst (size_type_node, len));
4926 decl = lookup_literal_operator (name, args);
4928 if (decl && decl != error_mark_node)
4929 return finish_call_expr (decl, &args, false, true,
4930 tf_warning_or_error);
4932 /* Look for a suitable template function, either (C++20) with a single
4933 parameter of class type, or (N3599) with typename parameter CharT and
4934 parameter pack CharT... */
4935 args->truncate (0);
4936 decl = lookup_literal_operator (name, args);
4937 if (decl && decl != error_mark_node)
4939 /* Use resolve_nondeduced_context to try to choose one form of template
4940 or the other. */
4941 tree tmpl_args = make_tree_vec (1);
4942 TREE_VEC_ELT (tmpl_args, 0) = value;
4943 decl = lookup_template_function (decl, tmpl_args);
4944 tree res = resolve_nondeduced_context (decl, tf_none);
4945 if (DECL_P (res))
4946 decl = res;
4947 else
4949 TREE_OPERAND (decl, 1) = make_string_pack (value);
4950 res = resolve_nondeduced_context (decl, tf_none);
4951 if (DECL_P (res))
4952 decl = res;
4954 if (!DECL_P (decl) && cxx_dialect > cxx17)
4955 TREE_OPERAND (decl, 1) = tmpl_args;
4956 return finish_call_expr (decl, &args, false, true,
4957 tf_warning_or_error);
4960 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4961 name, TREE_TYPE (value), size_type_node);
4962 return error_mark_node;
4966 /* Basic concepts [gram.basic] */
4968 /* Parse a translation-unit.
4970 translation-unit:
4971 declaration-seq [opt] */
4973 static void
4974 cp_parser_translation_unit (cp_parser* parser)
4976 gcc_checking_assert (!cp_error_declarator);
4978 /* Create the declarator obstack. */
4979 gcc_obstack_init (&declarator_obstack);
4980 /* Create the error declarator. */
4981 cp_error_declarator = make_declarator (cdk_error);
4982 /* Create the empty parameter list. */
4983 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE,
4984 UNKNOWN_LOCATION);
4985 /* Remember where the base of the declarator obstack lies. */
4986 void *declarator_obstack_base = obstack_next_free (&declarator_obstack);
4988 push_deferring_access_checks (flag_access_control
4989 ? dk_no_deferred : dk_no_check);
4991 module_parse mp_state = MP_NOT_MODULE;
4992 if (modules_p () && !header_module_p ())
4993 mp_state = MP_FIRST;
4995 bool implicit_extern_c = false;
4997 /* Parse until EOF. */
4998 for (;;)
5000 cp_token *token = cp_lexer_peek_token (parser->lexer);
5002 /* If we're entering or exiting a region that's implicitly
5003 extern "C", modify the lang context appropriately. This is
5004 so horrible. Please die. */
5005 if (implicit_extern_c
5006 != cp_lexer_peek_token (parser->lexer)->implicit_extern_c)
5008 implicit_extern_c = !implicit_extern_c;
5009 if (implicit_extern_c)
5010 push_lang_context (lang_name_c);
5011 else
5012 pop_lang_context ();
5015 if (token->type == CPP_EOF)
5016 break;
5018 if (modules_p ())
5020 /* Top-level module declarations are ok, and change the
5021 portion of file we're in. Top-level import declarations
5022 are significant for the import portions. */
5024 cp_token *next = token;
5025 bool exporting = token->keyword == RID__EXPORT;
5026 if (exporting)
5028 cp_lexer_consume_token (parser->lexer);
5029 next = cp_lexer_peek_token (parser->lexer);
5031 if (next->keyword == RID__MODULE)
5033 mp_state
5034 = cp_parser_module_declaration (parser, mp_state, exporting);
5035 continue;
5037 else if (next->keyword == RID__IMPORT)
5039 if (mp_state == MP_FIRST)
5040 mp_state = MP_NOT_MODULE;
5041 cp_parser_import_declaration (parser, mp_state, exporting);
5042 continue;
5044 else
5045 gcc_checking_assert (!exporting);
5047 if (mp_state == MP_GLOBAL && token->main_source_p)
5049 static bool warned = false;
5050 if (!warned)
5052 warned = true;
5053 error_at (token->location,
5054 "global module fragment contents must be"
5055 " from preprocessor inclusion");
5060 /* This relies on the ordering of module_parse values. */
5061 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
5062 /* We're no longer in the import portion of a named module. */
5063 mp_state = module_parse (mp_state + 1);
5064 else if (mp_state == MP_FIRST)
5065 mp_state = MP_NOT_MODULE;
5067 if (token->type == CPP_CLOSE_BRACE)
5069 cp_parser_error (parser, "expected declaration");
5070 cp_lexer_consume_token (parser->lexer);
5071 /* If the next token is now a `;', consume it. */
5072 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
5073 cp_lexer_consume_token (parser->lexer);
5075 else
5076 cp_parser_toplevel_declaration (parser);
5079 /* Get rid of the token array; we don't need it any more. */
5080 cp_lexer_destroy (parser->lexer);
5081 parser->lexer = NULL;
5083 /* The EOF should have reset this. */
5084 gcc_checking_assert (!implicit_extern_c);
5086 /* Make sure the declarator obstack was fully cleaned up. */
5087 gcc_assert (obstack_next_free (&declarator_obstack)
5088 == declarator_obstack_base);
5091 /* Return the appropriate tsubst flags for parsing, possibly in N3276
5092 decltype context. */
5094 static inline tsubst_flags_t
5095 complain_flags (bool decltype_p)
5097 tsubst_flags_t complain = tf_warning_or_error;
5098 if (decltype_p)
5099 complain |= tf_decltype;
5100 return complain;
5103 /* We're about to parse a collection of statements. If we're currently
5104 parsing tentatively, set up a firewall so that any nested
5105 cp_parser_commit_to_tentative_parse won't affect the current context. */
5107 static cp_token_position
5108 cp_parser_start_tentative_firewall (cp_parser *parser)
5110 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5111 return 0;
5113 cp_parser_parse_tentatively (parser);
5114 cp_parser_commit_to_topmost_tentative_parse (parser);
5115 return cp_lexer_token_position (parser->lexer, false);
5118 /* We've finished parsing the collection of statements. Wrap up the
5119 firewall and replace the relevant tokens with the parsed form. */
5121 static void
5122 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
5123 tree expr)
5125 if (!start)
5126 return;
5128 /* Finish the firewall level. */
5129 cp_parser_parse_definitely (parser);
5130 /* And remember the result of the parse for when we try again. */
5131 cp_token *token = cp_lexer_token_at (parser->lexer, start);
5132 token->type = CPP_PREPARSED_EXPR;
5133 token->u.value = expr;
5134 token->keyword = RID_MAX;
5135 cp_lexer_purge_tokens_after (parser->lexer, start);
5138 /* Like the above functions, but let the user modify the tokens. Used by
5139 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5140 later parses, so it makes sense to localize the effects of
5141 cp_parser_commit_to_tentative_parse. */
5143 struct tentative_firewall
5145 cp_parser *parser;
5146 bool set;
5148 tentative_firewall (cp_parser *p): parser(p)
5150 /* If we're currently parsing tentatively, start a committed level as a
5151 firewall and then an inner tentative parse. */
5152 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
5154 cp_parser_parse_tentatively (parser);
5155 cp_parser_commit_to_topmost_tentative_parse (parser);
5156 cp_parser_parse_tentatively (parser);
5160 ~tentative_firewall()
5162 if (set)
5164 /* Finish the inner tentative parse and the firewall, propagating any
5165 uncommitted error state to the outer tentative parse. */
5166 bool err = cp_parser_error_occurred (parser);
5167 cp_parser_parse_definitely (parser);
5168 cp_parser_parse_definitely (parser);
5169 if (err)
5170 cp_parser_simulate_error (parser);
5175 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5176 This class is for tracking such a matching pair of symbols.
5177 In particular, it tracks the location of the first token,
5178 so that if the second token is missing, we can highlight the
5179 location of the first token when notifying the user about the
5180 problem. */
5182 template <typename traits_t>
5183 class token_pair
5185 public:
5186 /* token_pair's ctor. */
5187 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
5189 /* If the next token is the opening symbol for this pair, consume it and
5190 return true.
5191 Otherwise, issue an error and return false.
5192 In either case, record the location of the opening token. */
5194 bool require_open (cp_parser *parser)
5196 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
5197 return cp_parser_require (parser, traits_t::open_token_type,
5198 traits_t::required_token_open);
5201 /* Consume the next token from PARSER, recording its location as
5202 that of the opening token within the pair. */
5204 cp_token * consume_open (cp_parser *parser)
5206 cp_token *tok = cp_lexer_consume_token (parser->lexer);
5207 gcc_assert (tok->type == traits_t::open_token_type);
5208 m_open_loc = tok->location;
5209 return tok;
5212 /* If the next token is the closing symbol for this pair, consume it
5213 and return it.
5214 Otherwise, issue an error, highlighting the location of the
5215 corresponding opening token, and return NULL. */
5217 cp_token *require_close (cp_parser *parser) const
5219 return cp_parser_require (parser, traits_t::close_token_type,
5220 traits_t::required_token_close,
5221 m_open_loc);
5224 location_t open_location () const { return m_open_loc; }
5226 private:
5227 location_t m_open_loc;
5230 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5232 struct matching_paren_traits
5234 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
5235 static const enum required_token required_token_open = RT_OPEN_PAREN;
5236 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
5237 static const enum required_token required_token_close = RT_CLOSE_PAREN;
5240 /* "matching_parens" is a token_pair<T> class for tracking matching
5241 pairs of parentheses. */
5243 typedef token_pair<matching_paren_traits> matching_parens;
5245 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5247 struct matching_brace_traits
5249 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
5250 static const enum required_token required_token_open = RT_OPEN_BRACE;
5251 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
5252 static const enum required_token required_token_close = RT_CLOSE_BRACE;
5255 /* "matching_braces" is a token_pair<T> class for tracking matching
5256 pairs of braces. */
5258 typedef token_pair<matching_brace_traits> matching_braces;
5261 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5262 enclosing parentheses. */
5264 static cp_expr
5265 cp_parser_statement_expr (cp_parser *parser)
5267 cp_token_position start = cp_parser_start_tentative_firewall (parser);
5269 /* Consume the '('. */
5270 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
5271 matching_parens parens;
5272 parens.consume_open (parser);
5273 /* Start the statement-expression. */
5274 tree expr = begin_stmt_expr ();
5275 /* Parse the compound-statement. */
5276 cp_parser_compound_statement (parser, expr, BCS_STMT_EXPR, false);
5277 /* Finish up. */
5278 expr = finish_stmt_expr (expr, false);
5279 /* Consume the ')'. */
5280 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
5281 if (!parens.require_close (parser))
5282 cp_parser_skip_to_end_of_statement (parser);
5284 cp_parser_end_tentative_firewall (parser, start, expr);
5285 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
5286 return cp_expr (expr, combined_loc);
5289 /* Expressions [gram.expr] */
5291 /* Parse a fold-operator.
5293 fold-operator:
5294 - * / % ^ & | = < > << >>
5295 = -= *= /= %= ^= &= |= <<= >>=
5296 == != <= >= && || , .* ->*
5298 This returns the tree code corresponding to the matched operator
5299 as an int. When the current token matches a compound assignment
5300 operator, the resulting tree code is the negative value of the
5301 non-assignment operator. */
5303 static int
5304 cp_parser_fold_operator (cp_token *token)
5306 switch (token->type)
5308 case CPP_PLUS: return PLUS_EXPR;
5309 case CPP_MINUS: return MINUS_EXPR;
5310 case CPP_MULT: return MULT_EXPR;
5311 case CPP_DIV: return TRUNC_DIV_EXPR;
5312 case CPP_MOD: return TRUNC_MOD_EXPR;
5313 case CPP_XOR: return BIT_XOR_EXPR;
5314 case CPP_AND: return BIT_AND_EXPR;
5315 case CPP_OR: return BIT_IOR_EXPR;
5316 case CPP_LSHIFT: return LSHIFT_EXPR;
5317 case CPP_RSHIFT: return RSHIFT_EXPR;
5319 case CPP_EQ: return -NOP_EXPR;
5320 case CPP_PLUS_EQ: return -PLUS_EXPR;
5321 case CPP_MINUS_EQ: return -MINUS_EXPR;
5322 case CPP_MULT_EQ: return -MULT_EXPR;
5323 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
5324 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
5325 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
5326 case CPP_AND_EQ: return -BIT_AND_EXPR;
5327 case CPP_OR_EQ: return -BIT_IOR_EXPR;
5328 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
5329 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
5331 case CPP_EQ_EQ: return EQ_EXPR;
5332 case CPP_NOT_EQ: return NE_EXPR;
5333 case CPP_LESS: return LT_EXPR;
5334 case CPP_GREATER: return GT_EXPR;
5335 case CPP_LESS_EQ: return LE_EXPR;
5336 case CPP_GREATER_EQ: return GE_EXPR;
5338 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
5339 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
5341 case CPP_COMMA: return COMPOUND_EXPR;
5343 case CPP_DOT_STAR: return DOTSTAR_EXPR;
5344 case CPP_DEREF_STAR: return MEMBER_REF;
5346 default: return ERROR_MARK;
5350 /* Returns true if CODE indicates a binary expression, which is not allowed in
5351 the LHS of a fold-expression. More codes will need to be added to use this
5352 function in other contexts. */
5354 static bool
5355 is_binary_op (tree_code code)
5357 switch (code)
5359 case PLUS_EXPR:
5360 case POINTER_PLUS_EXPR:
5361 case MINUS_EXPR:
5362 case MULT_EXPR:
5363 case TRUNC_DIV_EXPR:
5364 case TRUNC_MOD_EXPR:
5365 case BIT_XOR_EXPR:
5366 case BIT_AND_EXPR:
5367 case BIT_IOR_EXPR:
5368 case LSHIFT_EXPR:
5369 case RSHIFT_EXPR:
5371 case MODOP_EXPR:
5373 case EQ_EXPR:
5374 case NE_EXPR:
5375 case LE_EXPR:
5376 case GE_EXPR:
5377 case LT_EXPR:
5378 case GT_EXPR:
5380 case TRUTH_ANDIF_EXPR:
5381 case TRUTH_ORIF_EXPR:
5383 case COMPOUND_EXPR:
5385 case DOTSTAR_EXPR:
5386 case MEMBER_REF:
5387 return true;
5389 default:
5390 return false;
5394 /* If the next token is a suitable fold operator, consume it and return as
5395 the function above. */
5397 static int
5398 cp_parser_fold_operator (cp_parser *parser)
5400 cp_token* token = cp_lexer_peek_token (parser->lexer);
5401 int code = cp_parser_fold_operator (token);
5402 if (code != ERROR_MARK)
5403 cp_lexer_consume_token (parser->lexer);
5404 return code;
5407 /* Parse a fold-expression.
5409 fold-expression:
5410 ( ... folding-operator cast-expression)
5411 ( cast-expression folding-operator ... )
5412 ( cast-expression folding operator ... folding-operator cast-expression)
5414 Note that the '(' and ')' are matched in primary expression. */
5416 static cp_expr
5417 cp_parser_fold_expression (cp_parser *parser, tree expr1)
5419 cp_id_kind pidk;
5421 // Left fold.
5422 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5424 if (expr1)
5425 return error_mark_node;
5426 cp_lexer_consume_token (parser->lexer);
5427 int op = cp_parser_fold_operator (parser);
5428 if (op == ERROR_MARK)
5430 cp_parser_error (parser, "expected binary operator");
5431 return error_mark_node;
5434 tree expr = cp_parser_cast_expression (parser, false, false,
5435 false, &pidk);
5436 if (expr == error_mark_node)
5437 return error_mark_node;
5438 return finish_left_unary_fold_expr (expr, op);
5441 const cp_token* token = cp_lexer_peek_token (parser->lexer);
5442 int op = cp_parser_fold_operator (parser);
5443 if (op == ERROR_MARK)
5445 cp_parser_error (parser, "expected binary operator");
5446 return error_mark_node;
5449 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
5451 cp_parser_error (parser, "expected ...");
5452 return error_mark_node;
5454 cp_lexer_consume_token (parser->lexer);
5456 /* The operands of a fold-expression are cast-expressions, so binary or
5457 conditional expressions are not allowed. We check this here to avoid
5458 tentative parsing. */
5459 if (EXPR_P (expr1) && warning_suppressed_p (expr1, OPT_Wparentheses))
5460 /* OK, the expression was parenthesized. */;
5461 else if (is_binary_op (TREE_CODE (expr1)))
5462 error_at (location_of (expr1),
5463 "binary expression in operand of fold-expression");
5464 else if (TREE_CODE (expr1) == COND_EXPR
5465 || (REFERENCE_REF_P (expr1)
5466 && TREE_CODE (TREE_OPERAND (expr1, 0)) == COND_EXPR))
5467 error_at (location_of (expr1),
5468 "conditional expression in operand of fold-expression");
5470 // Right fold.
5471 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5472 return finish_right_unary_fold_expr (expr1, op);
5474 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
5476 cp_parser_error (parser, "mismatched operator in fold-expression");
5477 return error_mark_node;
5479 cp_lexer_consume_token (parser->lexer);
5481 // Binary left or right fold.
5482 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
5483 if (expr2 == error_mark_node)
5484 return error_mark_node;
5485 return finish_binary_fold_expr (expr1, expr2, op);
5488 /* Parse a primary-expression.
5490 primary-expression:
5491 literal
5492 this
5493 ( expression )
5494 id-expression
5495 lambda-expression (C++11)
5497 GNU Extensions:
5499 primary-expression:
5500 ( compound-statement )
5501 __builtin_va_arg ( assignment-expression , type-id )
5502 __builtin_offsetof ( type-id , offsetof-expression )
5504 C++ Extensions:
5505 __has_nothrow_assign ( type-id )
5506 __has_nothrow_constructor ( type-id )
5507 __has_nothrow_copy ( type-id )
5508 __has_trivial_assign ( type-id )
5509 __has_trivial_constructor ( type-id )
5510 __has_trivial_copy ( type-id )
5511 __has_trivial_destructor ( type-id )
5512 __has_virtual_destructor ( type-id )
5513 __is_abstract ( type-id )
5514 __is_base_of ( type-id , type-id )
5515 __is_class ( type-id )
5516 __is_empty ( type-id )
5517 __is_enum ( type-id )
5518 __is_final ( type-id )
5519 __is_literal_type ( type-id )
5520 __is_pod ( type-id )
5521 __is_polymorphic ( type-id )
5522 __is_std_layout ( type-id )
5523 __is_trivial ( type-id )
5524 __is_union ( type-id )
5526 Objective-C++ Extension:
5528 primary-expression:
5529 objc-expression
5531 literal:
5532 __null
5534 ADDRESS_P is true iff this expression was immediately preceded by
5535 "&" and therefore might denote a pointer-to-member. CAST_P is true
5536 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5537 true iff this expression is a template argument.
5539 Returns a representation of the expression. Upon return, *IDK
5540 indicates what kind of id-expression (if any) was present. */
5542 static cp_expr
5543 cp_parser_primary_expression (cp_parser *parser,
5544 bool address_p,
5545 bool cast_p,
5546 bool template_arg_p,
5547 bool decltype_p,
5548 cp_id_kind *idk)
5550 cp_token *token = NULL;
5552 /* Assume the primary expression is not an id-expression. */
5553 *idk = CP_ID_KIND_NONE;
5555 /* Peek at the next token. */
5556 token = cp_lexer_peek_token (parser->lexer);
5557 switch ((int) token->type)
5559 /* literal:
5560 integer-literal
5561 character-literal
5562 floating-literal
5563 string-literal
5564 boolean-literal
5565 pointer-literal
5566 user-defined-literal */
5567 case CPP_CHAR:
5568 case CPP_CHAR16:
5569 case CPP_CHAR32:
5570 case CPP_WCHAR:
5571 case CPP_UTF8CHAR:
5572 case CPP_NUMBER:
5573 case CPP_PREPARSED_EXPR:
5574 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
5575 return cp_parser_userdef_numeric_literal (parser);
5576 token = cp_lexer_consume_token (parser->lexer);
5577 if (TREE_CODE (token->u.value) == FIXED_CST)
5579 error_at (token->location,
5580 "fixed-point types not supported in C++");
5581 return error_mark_node;
5583 /* Floating-point literals are only allowed in an integral
5584 constant expression if they are cast to an integral or
5585 enumeration type. */
5586 if (TREE_CODE (token->u.value) == REAL_CST
5587 && parser->integral_constant_expression_p
5588 && pedantic)
5590 /* CAST_P will be set even in invalid code like "int(2.7 +
5591 ...)". Therefore, we have to check that the next token
5592 is sure to end the cast. */
5593 if (cast_p)
5595 cp_token *next_token;
5597 next_token = cp_lexer_peek_token (parser->lexer);
5598 if (/* The comma at the end of an
5599 enumerator-definition. */
5600 next_token->type != CPP_COMMA
5601 /* The curly brace at the end of an enum-specifier. */
5602 && next_token->type != CPP_CLOSE_BRACE
5603 /* The end of a statement. */
5604 && next_token->type != CPP_SEMICOLON
5605 /* The end of the cast-expression. */
5606 && next_token->type != CPP_CLOSE_PAREN
5607 /* The end of an array bound. */
5608 && next_token->type != CPP_CLOSE_SQUARE
5609 /* The closing ">" in a template-argument-list. */
5610 && (next_token->type != CPP_GREATER
5611 || parser->greater_than_is_operator_p)
5612 /* C++0x only: A ">>" treated like two ">" tokens,
5613 in a template-argument-list. */
5614 && (next_token->type != CPP_RSHIFT
5615 || (cxx_dialect == cxx98)
5616 || parser->greater_than_is_operator_p))
5617 cast_p = false;
5620 /* If we are within a cast, then the constraint that the
5621 cast is to an integral or enumeration type will be
5622 checked at that point. If we are not within a cast, then
5623 this code is invalid. */
5624 if (!cast_p)
5625 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
5627 return (cp_expr (token->u.value, token->location, token->flags & DECIMAL_INT)
5628 .maybe_add_location_wrapper ());
5630 case CPP_CHAR_USERDEF:
5631 case CPP_CHAR16_USERDEF:
5632 case CPP_CHAR32_USERDEF:
5633 case CPP_WCHAR_USERDEF:
5634 case CPP_UTF8CHAR_USERDEF:
5635 return cp_parser_userdef_char_literal (parser);
5637 case CPP_STRING:
5638 case CPP_STRING16:
5639 case CPP_STRING32:
5640 case CPP_WSTRING:
5641 case CPP_UTF8STRING:
5642 case CPP_STRING_USERDEF:
5643 case CPP_STRING16_USERDEF:
5644 case CPP_STRING32_USERDEF:
5645 case CPP_WSTRING_USERDEF:
5646 case CPP_UTF8STRING_USERDEF:
5647 /* ??? Should wide strings be allowed when parser->translate_strings_p
5648 is false (i.e. in attributes)? If not, we can kill the third
5649 argument to cp_parser_string_literal. */
5650 return (cp_parser_string_literal (parser,
5651 parser->translate_strings_p,
5652 true)
5653 .maybe_add_location_wrapper ());
5655 case CPP_OPEN_PAREN:
5656 /* If we see `( { ' then we are looking at the beginning of
5657 a GNU statement-expression. */
5658 if (cp_parser_allow_gnu_extensions_p (parser)
5659 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
5661 /* Statement-expressions are not allowed by the standard. */
5662 pedwarn (token->location, OPT_Wpedantic,
5663 "ISO C++ forbids braced-groups within expressions");
5665 /* And they're not allowed outside of a function-body; you
5666 cannot, for example, write:
5668 int i = ({ int j = 3; j + 1; });
5670 at class or namespace scope. */
5671 if (!parser->in_function_body
5672 || parser->in_template_argument_list_p)
5674 error_at (token->location,
5675 "statement-expressions are not allowed outside "
5676 "functions nor in template-argument lists");
5677 cp_parser_skip_to_end_of_block_or_statement (parser);
5678 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5679 cp_lexer_consume_token (parser->lexer);
5680 return error_mark_node;
5682 else
5683 return cp_parser_statement_expr (parser);
5685 /* Otherwise it's a normal parenthesized expression. */
5687 cp_expr expr;
5688 bool saved_greater_than_is_operator_p;
5690 location_t open_paren_loc = token->location;
5692 /* Consume the `('. */
5693 matching_parens parens;
5694 parens.consume_open (parser);
5695 /* Within a parenthesized expression, a `>' token is always
5696 the greater-than operator. */
5697 saved_greater_than_is_operator_p
5698 = parser->greater_than_is_operator_p;
5699 parser->greater_than_is_operator_p = true;
5701 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5702 /* Left fold expression. */
5703 expr = NULL_TREE;
5704 else
5705 /* Parse the parenthesized expression. */
5706 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5708 token = cp_lexer_peek_token (parser->lexer);
5709 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5711 expr = cp_parser_fold_expression (parser, expr);
5712 if (expr != error_mark_node
5713 && cxx_dialect < cxx17)
5714 pedwarn (input_location, OPT_Wc__17_extensions,
5715 "fold-expressions only available with %<-std=c++17%> "
5716 "or %<-std=gnu++17%>");
5718 else
5719 /* Let the front end know that this expression was
5720 enclosed in parentheses. This matters in case, for
5721 example, the expression is of the form `A::B', since
5722 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5723 not. */
5724 expr = finish_parenthesized_expr (expr);
5726 /* DR 705: Wrapping an unqualified name in parentheses
5727 suppresses arg-dependent lookup. We want to pass back
5728 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5729 (c++/37862), but none of the others. */
5730 if (*idk != CP_ID_KIND_QUALIFIED)
5731 *idk = CP_ID_KIND_NONE;
5733 /* The `>' token might be the end of a template-id or
5734 template-parameter-list now. */
5735 parser->greater_than_is_operator_p
5736 = saved_greater_than_is_operator_p;
5738 /* Consume the `)'. */
5739 token = cp_lexer_peek_token (parser->lexer);
5740 location_t close_paren_loc = token->location;
5741 bool no_wparens = warning_suppressed_p (expr, OPT_Wparentheses);
5742 expr.set_range (open_paren_loc, close_paren_loc);
5743 if (no_wparens)
5744 suppress_warning (expr, OPT_Wparentheses);
5745 if (!parens.require_close (parser)
5746 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5747 cp_parser_skip_to_end_of_statement (parser);
5749 return expr;
5752 case CPP_OPEN_SQUARE:
5754 if (c_dialect_objc ())
5756 /* We might have an Objective-C++ message. */
5757 cp_parser_parse_tentatively (parser);
5758 tree msg = cp_parser_objc_message_expression (parser);
5759 /* If that works out, we're done ... */
5760 if (cp_parser_parse_definitely (parser))
5761 return msg;
5762 /* ... else, fall though to see if it's a lambda. */
5764 cp_expr lam = cp_parser_lambda_expression (parser);
5765 /* Don't warn about a failed tentative parse. */
5766 if (cp_parser_error_occurred (parser))
5767 return error_mark_node;
5768 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5769 return lam;
5772 case CPP_OBJC_STRING:
5773 if (c_dialect_objc ())
5774 /* We have an Objective-C++ string literal. */
5775 return cp_parser_objc_expression (parser);
5776 cp_parser_error (parser, "expected primary-expression");
5777 return error_mark_node;
5779 case CPP_KEYWORD:
5780 switch (token->keyword)
5782 /* These two are the boolean literals. */
5783 case RID_TRUE:
5784 cp_lexer_consume_token (parser->lexer);
5785 return cp_expr (boolean_true_node, token->location);
5786 case RID_FALSE:
5787 cp_lexer_consume_token (parser->lexer);
5788 return cp_expr (boolean_false_node, token->location);
5790 /* The `__null' literal. */
5791 case RID_NULL:
5792 cp_lexer_consume_token (parser->lexer);
5793 return cp_expr (null_node, token->location);
5795 /* The `nullptr' literal. */
5796 case RID_NULLPTR:
5797 cp_lexer_consume_token (parser->lexer);
5798 return cp_expr (nullptr_node, token->location);
5800 /* Recognize the `this' keyword. */
5801 case RID_THIS:
5802 cp_lexer_consume_token (parser->lexer);
5803 if (parser->local_variables_forbidden_p & THIS_FORBIDDEN)
5805 error_at (token->location,
5806 "%<this%> may not be used in this context");
5807 return error_mark_node;
5809 /* Pointers cannot appear in constant-expressions. */
5810 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5811 return error_mark_node;
5812 return cp_expr (finish_this_expr (), token->location);
5814 /* The `operator' keyword can be the beginning of an
5815 id-expression. */
5816 case RID_OPERATOR:
5817 goto id_expression;
5819 case RID_FUNCTION_NAME:
5820 case RID_PRETTY_FUNCTION_NAME:
5821 case RID_C99_FUNCTION_NAME:
5823 non_integral_constant name;
5825 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5826 __func__ are the names of variables -- but they are
5827 treated specially. Therefore, they are handled here,
5828 rather than relying on the generic id-expression logic
5829 below. Grammatically, these names are id-expressions.
5831 Consume the token. */
5832 token = cp_lexer_consume_token (parser->lexer);
5834 switch (token->keyword)
5836 case RID_FUNCTION_NAME:
5837 name = NIC_FUNC_NAME;
5838 break;
5839 case RID_PRETTY_FUNCTION_NAME:
5840 name = NIC_PRETTY_FUNC;
5841 break;
5842 case RID_C99_FUNCTION_NAME:
5843 name = NIC_C99_FUNC;
5844 break;
5845 default:
5846 gcc_unreachable ();
5849 if (cp_parser_non_integral_constant_expression (parser, name))
5850 return error_mark_node;
5852 /* Look up the name. */
5853 return finish_fname (token->u.value);
5856 case RID_VA_ARG:
5858 tree expression;
5859 tree type;
5860 location_t type_location;
5861 location_t start_loc
5862 = cp_lexer_peek_token (parser->lexer)->location;
5863 /* The `__builtin_va_arg' construct is used to handle
5864 `va_arg'. Consume the `__builtin_va_arg' token. */
5865 cp_lexer_consume_token (parser->lexer);
5866 /* Look for the opening `('. */
5867 matching_parens parens;
5868 parens.require_open (parser);
5869 /* Now, parse the assignment-expression. */
5870 expression = cp_parser_assignment_expression (parser);
5871 /* Look for the `,'. */
5872 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5873 type_location = cp_lexer_peek_token (parser->lexer)->location;
5874 /* Parse the type-id. */
5876 type_id_in_expr_sentinel s (parser);
5877 type = cp_parser_type_id (parser);
5879 /* Look for the closing `)'. */
5880 location_t finish_loc
5881 = cp_lexer_peek_token (parser->lexer)->location;
5882 parens.require_close (parser);
5883 /* Using `va_arg' in a constant-expression is not
5884 allowed. */
5885 if (cp_parser_non_integral_constant_expression (parser,
5886 NIC_VA_ARG))
5887 return error_mark_node;
5888 /* Construct a location of the form:
5889 __builtin_va_arg (v, int)
5890 ~~~~~~~~~~~~~~~~~~~~~^~~~
5891 with the caret at the type, ranging from the start of the
5892 "__builtin_va_arg" token to the close paren. */
5893 location_t combined_loc
5894 = make_location (type_location, start_loc, finish_loc);
5895 return build_x_va_arg (combined_loc, expression, type);
5898 case RID_OFFSETOF:
5899 return cp_parser_builtin_offsetof (parser);
5901 #define DEFTRAIT_EXPR(CODE, NAME, ARITY) \
5902 case RID_##CODE:
5903 #include "cp-trait.def"
5904 #undef DEFTRAIT_EXPR
5905 return cp_parser_trait (parser, token->keyword);
5907 // C++ concepts
5908 case RID_REQUIRES:
5909 return cp_parser_requires_expression (parser);
5911 /* Objective-C++ expressions. */
5912 case RID_AT_ENCODE:
5913 case RID_AT_PROTOCOL:
5914 case RID_AT_SELECTOR:
5915 return cp_parser_objc_expression (parser);
5917 case RID_OMP_ALL_MEMORY:
5918 gcc_assert (flag_openmp);
5919 cp_lexer_consume_token (parser->lexer);
5920 error_at (token->location,
5921 "%<omp_all_memory%> may only be used in OpenMP "
5922 "%<depend%> clause");
5923 return error_mark_node;
5925 case RID_TEMPLATE:
5926 if (parser->in_function_body
5927 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5928 == CPP_LESS))
5930 error_at (token->location,
5931 "a template declaration cannot appear at block scope");
5932 cp_parser_skip_to_end_of_block_or_statement (parser);
5933 return error_mark_node;
5935 /* FALLTHRU */
5936 default:
5937 cp_parser_error (parser, "expected primary-expression");
5938 return error_mark_node;
5941 /* An id-expression can start with either an identifier, a
5942 `::' as the beginning of a qualified-id, or the "operator"
5943 keyword. */
5944 case CPP_NAME:
5945 case CPP_SCOPE:
5946 case CPP_TEMPLATE_ID:
5947 case CPP_NESTED_NAME_SPECIFIER:
5949 id_expression:
5950 cp_expr id_expression;
5951 cp_expr decl;
5952 const char *error_msg;
5953 bool template_p;
5954 bool done;
5955 cp_token *id_expr_token;
5957 /* Parse the id-expression. */
5958 id_expression
5959 = cp_parser_id_expression (parser,
5960 /*template_keyword_p=*/false,
5961 /*check_dependency_p=*/true,
5962 &template_p,
5963 /*declarator_p=*/false,
5964 /*optional_p=*/false);
5965 if (id_expression == error_mark_node)
5966 return error_mark_node;
5967 id_expr_token = token;
5968 token = cp_lexer_peek_token (parser->lexer);
5969 done = (token->type != CPP_OPEN_SQUARE
5970 && token->type != CPP_OPEN_PAREN
5971 && token->type != CPP_DOT
5972 && token->type != CPP_DEREF
5973 && token->type != CPP_PLUS_PLUS
5974 && token->type != CPP_MINUS_MINUS);
5975 /* If we have a template-id, then no further lookup is
5976 required. If the template-id was for a template-class, we
5977 will sometimes have a TYPE_DECL at this point. */
5978 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5979 || TREE_CODE (id_expression) == TYPE_DECL)
5980 decl = id_expression;
5981 /* Look up the name. */
5982 else
5984 tree ambiguous_decls;
5986 /* If we already know that this lookup is ambiguous, then
5987 we've already issued an error message; there's no reason
5988 to check again. */
5989 if (id_expr_token->type == CPP_NAME
5990 && id_expr_token->error_reported)
5992 cp_parser_simulate_error (parser);
5993 return error_mark_node;
5996 decl = cp_parser_lookup_name (parser, id_expression,
5997 none_type,
5998 template_p,
5999 /*is_namespace=*/false,
6000 /*check_dependency=*/true,
6001 &ambiguous_decls,
6002 id_expression.get_location ());
6003 /* If the lookup was ambiguous, an error will already have
6004 been issued. */
6005 if (ambiguous_decls)
6006 return error_mark_node;
6008 /* In Objective-C++, we may have an Objective-C 2.0
6009 dot-syntax for classes here. */
6010 if (c_dialect_objc ()
6011 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
6012 && TREE_CODE (decl) == TYPE_DECL
6013 && objc_is_class_name (decl))
6015 tree component;
6016 cp_lexer_consume_token (parser->lexer);
6017 component = cp_parser_identifier (parser);
6018 if (component == error_mark_node)
6019 return error_mark_node;
6021 tree result = objc_build_class_component_ref (id_expression,
6022 component);
6023 /* Build a location of the form:
6024 expr.component
6025 ~~~~~^~~~~~~~~
6026 with caret at the start of the component name (at
6027 input_location), ranging from the start of the id_expression
6028 to the end of the component name. */
6029 location_t combined_loc
6030 = make_location (input_location, id_expression.get_start (),
6031 get_finish (input_location));
6032 protected_set_expr_location (result, combined_loc);
6033 return result;
6036 /* In Objective-C++, an instance variable (ivar) may be preferred
6037 to whatever cp_parser_lookup_name() found.
6038 Call objc_lookup_ivar. To avoid exposing cp_expr to the
6039 rest of c-family, we have to do a little extra work to preserve
6040 any location information in cp_expr "decl". Given that
6041 objc_lookup_ivar is implemented in "c-family" and "objc", we
6042 have a trip through the pure "tree" type, rather than cp_expr.
6043 Naively copying it back to "decl" would implicitly give the
6044 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
6045 store an EXPR_LOCATION. Hence we only update "decl" (and
6046 hence its location_t) if we get back a different tree node. */
6047 tree decl_tree = objc_lookup_ivar (decl.get_value (),
6048 id_expression);
6049 if (decl_tree != decl.get_value ())
6050 decl = cp_expr (decl_tree);
6052 /* If name lookup gives us a SCOPE_REF, then the
6053 qualifying scope was dependent. */
6054 if (TREE_CODE (decl) == SCOPE_REF)
6056 /* At this point, we do not know if DECL is a valid
6057 integral constant expression. We assume that it is
6058 in fact such an expression, so that code like:
6060 template <int N> struct A {
6061 int a[B<N>::i];
6064 is accepted. At template-instantiation time, we
6065 will check that B<N>::i is actually a constant. */
6066 return decl;
6068 /* Check to see if DECL is a local variable in a context
6069 where that is forbidden. */
6070 if ((parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
6071 && local_variable_p (decl)
6072 /* DR 2082 permits local variables in unevaluated contexts
6073 within a default argument. */
6074 && !cp_unevaluated_operand)
6076 const char *msg
6077 = (TREE_CODE (decl) == PARM_DECL
6078 ? _("parameter %qD may not appear in this context")
6079 : _("local variable %qD may not appear in this context"));
6080 error_at (id_expression.get_location (), msg,
6081 decl.get_value ());
6082 return error_mark_node;
6086 decl = (finish_id_expression
6087 (id_expression, decl, parser->scope,
6088 idk,
6089 parser->integral_constant_expression_p,
6090 parser->allow_non_integral_constant_expression_p,
6091 &parser->non_integral_constant_expression_p,
6092 template_p, done, address_p,
6093 template_arg_p,
6094 &error_msg,
6095 id_expression.get_location ()));
6096 if (error_msg)
6097 cp_parser_error (parser, error_msg);
6098 /* Build a location for an id-expression of the form:
6099 ::ns::id
6100 ~~~~~~^~
6104 i.e. from the start of the first token to the end of the final
6105 token, with the caret at the start of the unqualified-id. */
6106 location_t caret_loc = get_pure_location (id_expression.get_location ());
6107 location_t start_loc = get_start (id_expr_token->location);
6108 location_t finish_loc = get_finish (id_expression.get_location ());
6109 location_t combined_loc
6110 = make_location (caret_loc, start_loc, finish_loc);
6112 decl.set_location (combined_loc);
6113 return decl;
6116 /* Anything else is an error. */
6117 default:
6118 cp_parser_error (parser, "expected primary-expression");
6119 return error_mark_node;
6123 static inline cp_expr
6124 cp_parser_primary_expression (cp_parser *parser,
6125 bool address_p,
6126 bool cast_p,
6127 bool template_arg_p,
6128 cp_id_kind *idk)
6130 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
6131 /*decltype*/false, idk);
6134 /* Complain about missing template keyword when naming a dependent
6135 member template. */
6137 static void
6138 missing_template_diag (location_t loc, diagnostic_t diag_kind = DK_WARNING)
6140 if (warning_suppressed_at (loc, OPT_Wmissing_template_keyword))
6141 return;
6143 gcc_rich_location richloc (loc);
6144 richloc.add_fixit_insert_before ("template");
6145 emit_diagnostic (diag_kind, &richloc, OPT_Wmissing_template_keyword,
6146 "expected %qs keyword before dependent "
6147 "template name", "template");
6148 suppress_warning_at (loc, OPT_Wmissing_template_keyword);
6151 /* Parse an id-expression.
6153 id-expression:
6154 unqualified-id
6155 qualified-id
6157 qualified-id:
6158 :: [opt] nested-name-specifier template [opt] unqualified-id
6159 :: identifier
6160 :: operator-function-id
6161 :: template-id
6163 Return a representation of the unqualified portion of the
6164 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6165 a `::' or nested-name-specifier.
6167 Often, if the id-expression was a qualified-id, the caller will
6168 want to make a SCOPE_REF to represent the qualified-id. This
6169 function does not do this in order to avoid wastefully creating
6170 SCOPE_REFs when they are not required.
6172 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6173 `template' keyword.
6175 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6176 uninstantiated templates.
6178 If *TEMPLATE_P is non-NULL, it is set to true iff the
6179 `template' keyword is used to explicitly indicate that the entity
6180 named is a template.
6182 If DECLARATOR_P is true, the id-expression is appearing as part of
6183 a declarator, rather than as part of an expression. */
6185 static cp_expr
6186 cp_parser_id_expression (cp_parser *parser,
6187 bool template_keyword_p,
6188 bool check_dependency_p,
6189 bool *template_p,
6190 bool declarator_p,
6191 bool optional_p)
6193 bool global_scope_p;
6194 bool nested_name_specifier_p;
6196 /* Assume the `template' keyword was not used. */
6197 if (template_p)
6198 *template_p = template_keyword_p;
6200 /* Look for the optional `::' operator. */
6201 global_scope_p
6202 = (!template_keyword_p
6203 && (cp_parser_global_scope_opt (parser,
6204 /*current_scope_valid_p=*/false)
6205 != NULL_TREE));
6207 /* Look for the optional nested-name-specifier. */
6208 nested_name_specifier_p
6209 = (cp_parser_nested_name_specifier_opt (parser,
6210 /*typename_keyword_p=*/false,
6211 check_dependency_p,
6212 /*type_p=*/false,
6213 declarator_p,
6214 template_keyword_p)
6215 != NULL_TREE);
6217 cp_expr id = NULL_TREE;
6218 tree scope = parser->scope;
6220 /* Peek at the next token. */
6221 cp_token *token = cp_lexer_peek_token (parser->lexer);
6223 /* If there is a nested-name-specifier, then we are looking at
6224 the first qualified-id production. */
6225 if (nested_name_specifier_p)
6227 tree saved_object_scope;
6228 tree saved_qualifying_scope;
6230 /* See if the next token is the `template' keyword. */
6231 if (!template_p)
6232 template_p = &template_keyword_p;
6233 *template_p = cp_parser_optional_template_keyword (parser);
6234 /* Name lookup we do during the processing of the
6235 unqualified-id might obliterate SCOPE. */
6236 saved_object_scope = parser->object_scope;
6237 saved_qualifying_scope = parser->qualifying_scope;
6238 /* Process the final unqualified-id. */
6239 id = cp_parser_unqualified_id (parser, *template_p,
6240 check_dependency_p,
6241 declarator_p,
6242 /*optional_p=*/false);
6243 /* Restore the SAVED_SCOPE for our caller. */
6244 parser->scope = scope;
6245 parser->object_scope = saved_object_scope;
6246 parser->qualifying_scope = saved_qualifying_scope;
6248 /* Otherwise, if we are in global scope, then we are looking at one
6249 of the other qualified-id productions. */
6250 else if (global_scope_p)
6252 /* If it's an identifier, and the next token is not a "<", then
6253 we can avoid the template-id case. This is an optimization
6254 for this common case. */
6255 if (token->type == CPP_NAME
6256 && !cp_parser_nth_token_starts_template_argument_list_p
6257 (parser, 2))
6258 return cp_parser_identifier (parser);
6260 cp_parser_parse_tentatively (parser);
6261 /* Try a template-id. */
6262 id = cp_parser_template_id_expr (parser,
6263 /*template_keyword_p=*/false,
6264 /*check_dependency_p=*/true,
6265 declarator_p);
6266 /* If that worked, we're done. */
6267 if (cp_parser_parse_definitely (parser))
6268 return id;
6270 /* Peek at the next token. (Changes in the token buffer may
6271 have invalidated the pointer obtained above.) */
6272 token = cp_lexer_peek_token (parser->lexer);
6274 switch (token->type)
6276 case CPP_NAME:
6277 id = cp_parser_identifier (parser);
6278 break;
6280 case CPP_KEYWORD:
6281 if (token->keyword == RID_OPERATOR)
6283 id = cp_parser_operator_function_id (parser);
6284 break;
6286 /* Fall through. */
6288 default:
6289 cp_parser_error (parser, "expected id-expression");
6290 return error_mark_node;
6293 else
6295 if (!scope)
6296 scope = parser->context->object_type;
6297 id = cp_parser_unqualified_id (parser, template_keyword_p,
6298 /*check_dependency_p=*/true,
6299 declarator_p,
6300 optional_p);
6303 if (id && TREE_CODE (id) == IDENTIFIER_NODE
6304 && warn_missing_template_keyword
6305 && !template_keyword_p
6306 /* Don't warn if we're looking inside templates. */
6307 && check_dependency_p
6308 /* In a template argument list a > could be closing
6309 the enclosing targs. */
6310 && !parser->in_template_argument_list_p
6311 && scope && dependentish_scope_p (scope)
6312 /* Don't confuse an ill-formed constructor declarator for a missing
6313 template keyword in a return type. */
6314 && !(declarator_p && constructor_name_p (id, scope))
6315 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1)
6316 && warning_enabled_at (token->location,
6317 OPT_Wmissing_template_keyword))
6319 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
6320 if (cp_parser_skip_entire_template_parameter_list (parser)
6321 /* An operator after the > suggests that the > ends a
6322 template-id; a name or literal suggests that the > is an
6323 operator. */
6324 && (cp_lexer_peek_token (parser->lexer)->type
6325 <= CPP_LAST_PUNCTUATOR))
6326 missing_template_diag (token->location);
6329 return id;
6332 /* Parse an unqualified-id.
6334 unqualified-id:
6335 identifier
6336 operator-function-id
6337 conversion-function-id
6338 ~ class-name
6339 template-id
6341 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6342 keyword, in a construct like `A::template ...'.
6344 Returns a representation of unqualified-id. For the `identifier'
6345 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6346 production a BIT_NOT_EXPR is returned; the operand of the
6347 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6348 other productions, see the documentation accompanying the
6349 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6350 names are looked up in uninstantiated templates. If DECLARATOR_P
6351 is true, the unqualified-id is appearing as part of a declarator,
6352 rather than as part of an expression. */
6354 static cp_expr
6355 cp_parser_unqualified_id (cp_parser* parser,
6356 bool template_keyword_p,
6357 bool check_dependency_p,
6358 bool declarator_p,
6359 bool optional_p)
6361 cp_token *token;
6363 /* Peek at the next token. */
6364 token = cp_lexer_peek_token (parser->lexer);
6366 switch ((int) token->type)
6368 case CPP_NAME:
6370 tree id;
6372 /* We don't know yet whether or not this will be a
6373 template-id. */
6374 cp_parser_parse_tentatively (parser);
6375 /* Try a template-id. */
6376 id = cp_parser_template_id_expr (parser, template_keyword_p,
6377 check_dependency_p,
6378 declarator_p);
6379 /* If it worked, we're done. */
6380 if (cp_parser_parse_definitely (parser))
6381 return id;
6382 /* Otherwise, it's an ordinary identifier. */
6383 return cp_parser_identifier (parser);
6386 case CPP_TEMPLATE_ID:
6387 return cp_parser_template_id_expr (parser, template_keyword_p,
6388 check_dependency_p,
6389 declarator_p);
6391 case CPP_COMPL:
6393 tree type_decl;
6394 tree qualifying_scope;
6395 tree object_scope;
6396 tree scope;
6397 bool done;
6398 location_t tilde_loc = token->location;
6400 /* Consume the `~' token. */
6401 cp_lexer_consume_token (parser->lexer);
6402 /* Parse the class-name. The standard, as written, seems to
6403 say that:
6405 template <typename T> struct S { ~S (); };
6406 template <typename T> S<T>::~S() {}
6408 is invalid, since `~' must be followed by a class-name, but
6409 `S<T>' is dependent, and so not known to be a class.
6410 That's not right; we need to look in uninstantiated
6411 templates. A further complication arises from:
6413 template <typename T> void f(T t) {
6414 t.T::~T();
6417 Here, it is not possible to look up `T' in the scope of `T'
6418 itself. We must look in both the current scope, and the
6419 scope of the containing complete expression.
6421 Yet another issue is:
6423 struct S {
6424 int S;
6425 ~S();
6428 S::~S() {}
6430 The standard does not seem to say that the `S' in `~S'
6431 should refer to the type `S' and not the data member
6432 `S::S'. */
6434 /* DR 244 says that we look up the name after the "~" in the
6435 same scope as we looked up the qualifying name. That idea
6436 isn't fully worked out; it's more complicated than that. */
6437 scope = parser->scope;
6438 object_scope = parser->object_scope;
6439 qualifying_scope = parser->qualifying_scope;
6441 /* Check for invalid scopes. */
6442 if (scope == error_mark_node)
6444 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6445 cp_lexer_consume_token (parser->lexer);
6446 return error_mark_node;
6448 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
6450 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6451 error_at (token->location,
6452 "scope %qT before %<~%> is not a class-name",
6453 scope);
6454 cp_parser_simulate_error (parser);
6455 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
6456 cp_lexer_consume_token (parser->lexer);
6457 return error_mark_node;
6459 if (template_keyword_p)
6461 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6462 error_at (tilde_loc, "%<template%> keyword not permitted in "
6463 "destructor name");
6464 cp_parser_simulate_error (parser);
6465 return error_mark_node;
6468 gcc_assert (!scope || TYPE_P (scope));
6470 token = cp_lexer_peek_token (parser->lexer);
6472 /* Create a location with caret == start at the tilde,
6473 finishing at the end of the peeked token, e.g:
6474 ~token
6475 ^~~~~~. */
6476 location_t loc
6477 = make_location (tilde_loc, tilde_loc, token->location);
6479 /* If the name is of the form "X::~X" it's OK even if X is a
6480 typedef. */
6482 if (scope
6483 && token->type == CPP_NAME
6484 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6485 != CPP_LESS)
6486 && (token->u.value == TYPE_IDENTIFIER (scope)
6487 || (CLASS_TYPE_P (scope)
6488 && constructor_name_p (token->u.value, scope))))
6490 cp_lexer_consume_token (parser->lexer);
6491 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6494 /* ~auto means the destructor of whatever the object is. */
6495 if (cp_parser_is_keyword (token, RID_AUTO))
6497 if (cxx_dialect < cxx14)
6498 pedwarn (loc, OPT_Wc__14_extensions,
6499 "%<~auto%> only available with "
6500 "%<-std=c++14%> or %<-std=gnu++14%>");
6501 cp_lexer_consume_token (parser->lexer);
6502 return build_min_nt_loc (loc, BIT_NOT_EXPR, make_auto ());
6505 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6506 declarator-id of a constructor or destructor. */
6507 if (token->type == CPP_TEMPLATE_ID && declarator_p
6508 && cxx_dialect >= cxx20)
6510 if (!cp_parser_simulate_error (parser))
6511 error_at (tilde_loc, "template-id not allowed for destructor");
6512 return error_mark_node;
6515 /* If there was an explicit qualification (S::~T), first look
6516 in the scope given by the qualification (i.e., S).
6518 Note: in the calls to cp_parser_class_name below we pass
6519 typename_type so that lookup finds the injected-class-name
6520 rather than the constructor. */
6521 done = false;
6522 type_decl = NULL_TREE;
6523 if (scope)
6525 cp_parser_parse_tentatively (parser);
6526 type_decl = cp_parser_class_name (parser,
6527 /*typename_keyword_p=*/false,
6528 /*template_keyword_p=*/false,
6529 typename_type,
6530 /*check_dependency=*/false,
6531 /*class_head_p=*/false,
6532 declarator_p);
6533 if (cp_parser_parse_definitely (parser))
6534 done = true;
6536 /* In "N::S::~S", look in "N" as well. */
6537 if (!done && scope && qualifying_scope)
6539 cp_parser_parse_tentatively (parser);
6540 parser->scope = qualifying_scope;
6541 parser->object_scope = NULL_TREE;
6542 parser->qualifying_scope = NULL_TREE;
6543 type_decl
6544 = cp_parser_class_name (parser,
6545 /*typename_keyword_p=*/false,
6546 /*template_keyword_p=*/false,
6547 typename_type,
6548 /*check_dependency=*/false,
6549 /*class_head_p=*/false,
6550 declarator_p);
6551 if (cp_parser_parse_definitely (parser))
6552 done = true;
6554 /* In "p->S::~T", look in the scope given by "*p" as well. */
6555 else if (!done && object_scope)
6557 cp_parser_parse_tentatively (parser);
6558 parser->scope = object_scope;
6559 parser->object_scope = NULL_TREE;
6560 parser->qualifying_scope = NULL_TREE;
6561 type_decl
6562 = cp_parser_class_name (parser,
6563 /*typename_keyword_p=*/false,
6564 /*template_keyword_p=*/false,
6565 typename_type,
6566 /*check_dependency=*/false,
6567 /*class_head_p=*/false,
6568 declarator_p);
6569 if (cp_parser_parse_definitely (parser))
6570 done = true;
6572 /* Look in the surrounding context. */
6573 if (!done)
6575 parser->scope = NULL_TREE;
6576 parser->object_scope = NULL_TREE;
6577 parser->qualifying_scope = NULL_TREE;
6578 if (processing_template_decl)
6579 cp_parser_parse_tentatively (parser);
6580 type_decl
6581 = cp_parser_class_name (parser,
6582 /*typename_keyword_p=*/false,
6583 /*template_keyword_p=*/false,
6584 typename_type,
6585 /*check_dependency=*/false,
6586 /*class_head_p=*/false,
6587 declarator_p);
6588 if (processing_template_decl
6589 && ! cp_parser_parse_definitely (parser))
6591 /* We couldn't find a type with this name. If we're parsing
6592 tentatively, fail and try something else. */
6593 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6595 cp_parser_simulate_error (parser);
6596 return error_mark_node;
6598 /* Otherwise, accept it and check for a match at instantiation
6599 time. */
6600 type_decl = cp_parser_identifier (parser);
6601 if (type_decl != error_mark_node)
6602 type_decl = build_min_nt_loc (loc, BIT_NOT_EXPR, type_decl);
6603 return type_decl;
6606 /* If an error occurred, assume that the name of the
6607 destructor is the same as the name of the qualifying
6608 class. That allows us to keep parsing after running
6609 into ill-formed destructor names. */
6610 if (type_decl == error_mark_node && scope)
6611 return build_min_nt_loc (loc, BIT_NOT_EXPR, scope);
6612 else if (type_decl == error_mark_node)
6613 return error_mark_node;
6615 /* Check that destructor name and scope match. */
6616 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
6618 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
6619 error_at (loc,
6620 "declaration of %<~%T%> as member of %qT",
6621 type_decl, scope);
6622 cp_parser_simulate_error (parser);
6623 return error_mark_node;
6626 /* [class.dtor]
6628 A typedef-name that names a class shall not be used as the
6629 identifier in the declarator for a destructor declaration. */
6630 if (declarator_p
6631 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
6632 && !DECL_SELF_REFERENCE_P (type_decl)
6633 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
6634 error_at (loc,
6635 "typedef-name %qD used as destructor declarator",
6636 type_decl);
6638 return build_min_nt_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type_decl));
6641 case CPP_KEYWORD:
6642 if (token->keyword == RID_OPERATOR)
6644 cp_expr id;
6646 /* This could be a template-id, so we try that first. */
6647 cp_parser_parse_tentatively (parser);
6648 /* Try a template-id. */
6649 id = cp_parser_template_id_expr (parser, template_keyword_p,
6650 /*check_dependency_p=*/true,
6651 declarator_p);
6652 /* If that worked, we're done. */
6653 if (cp_parser_parse_definitely (parser))
6654 return id;
6655 /* We still don't know whether we're looking at an
6656 operator-function-id or a conversion-function-id. */
6657 cp_parser_parse_tentatively (parser);
6658 /* Try an operator-function-id. */
6659 id = cp_parser_operator_function_id (parser);
6660 /* If that didn't work, try a conversion-function-id. */
6661 if (!cp_parser_parse_definitely (parser))
6662 id = cp_parser_conversion_function_id (parser);
6664 return id;
6666 /* Fall through. */
6668 default:
6669 if (optional_p)
6670 return NULL_TREE;
6671 cp_parser_error (parser, "expected unqualified-id");
6672 return error_mark_node;
6676 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6677 be a template-id or the name shall refer to a class template or an
6678 alias template. */
6680 static void
6681 check_template_keyword_in_nested_name_spec (tree name)
6683 if (CLASS_TYPE_P (name)
6684 && ((CLASSTYPE_USE_TEMPLATE (name)
6685 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name)))
6686 || CLASSTYPE_IS_TEMPLATE (name)))
6687 return;
6689 if (TREE_CODE (name) == TYPENAME_TYPE
6690 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name)) == TEMPLATE_ID_EXPR)
6691 return;
6692 /* Alias templates are also OK. */
6693 else if (alias_template_specialization_p (name, nt_opaque))
6694 return;
6696 permerror (input_location, TYPE_P (name)
6697 ? G_("%qT is not a template")
6698 : G_("%qD is not a template"),
6699 name);
6702 /* Parse an (optional) nested-name-specifier.
6704 nested-name-specifier: [C++98]
6705 class-or-namespace-name :: nested-name-specifier [opt]
6706 class-or-namespace-name :: template nested-name-specifier [opt]
6708 nested-name-specifier: [C++0x]
6709 type-name ::
6710 namespace-name ::
6711 nested-name-specifier identifier ::
6712 nested-name-specifier template [opt] simple-template-id ::
6714 PARSER->SCOPE should be set appropriately before this function is
6715 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6716 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6717 in name lookups.
6719 Sets PARSER->SCOPE to the class (TYPE) or namespace
6720 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6721 it unchanged if there is no nested-name-specifier. Returns the new
6722 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6724 If CHECK_DEPENDENCY_P is FALSE, names are looked up in dependent scopes.
6726 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6727 part of a declaration and/or decl-specifier. */
6729 static tree
6730 cp_parser_nested_name_specifier_opt (cp_parser *parser,
6731 bool typename_keyword_p,
6732 bool check_dependency_p,
6733 bool type_p,
6734 bool is_declaration,
6735 bool template_keyword_p /* = false */)
6737 bool success = false;
6738 cp_token_position start = 0;
6739 cp_token *token;
6741 /* Remember where the nested-name-specifier starts. */
6742 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
6743 && cp_lexer_next_token_is_not (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
6745 start = cp_lexer_token_position (parser->lexer, false);
6746 push_deferring_access_checks (dk_deferred);
6749 while (true)
6751 tree new_scope;
6752 tree old_scope;
6753 tree saved_qualifying_scope;
6755 /* Spot cases that cannot be the beginning of a
6756 nested-name-specifier. */
6757 token = cp_lexer_peek_token (parser->lexer);
6759 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6760 the already parsed nested-name-specifier. */
6761 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6763 /* Grab the nested-name-specifier and continue the loop. */
6764 cp_parser_pre_parsed_nested_name_specifier (parser);
6765 /* If we originally encountered this nested-name-specifier
6766 with CHECK_DEPENDENCY_P set to true, we will not have
6767 resolved TYPENAME_TYPEs, so we must do so here. */
6768 if (is_declaration
6769 && !check_dependency_p
6770 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6772 new_scope = resolve_typename_type (parser->scope,
6773 /*only_current_p=*/false);
6774 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6775 parser->scope = new_scope;
6777 success = true;
6778 continue;
6781 /* Spot cases that cannot be the beginning of a
6782 nested-name-specifier. On the second and subsequent times
6783 through the loop, we look for the `template' keyword. */
6784 if (success && token->keyword == RID_TEMPLATE)
6786 /* A template-id can start a nested-name-specifier. */
6787 else if (token->type == CPP_TEMPLATE_ID)
6789 /* DR 743: decltype can be used in a nested-name-specifier. */
6790 else if (token_is_decltype (token))
6792 else
6794 /* If the next token is not an identifier, then it is
6795 definitely not a type-name or namespace-name. */
6796 if (token->type != CPP_NAME)
6797 break;
6798 /* If the following token is neither a `<' (to begin a
6799 template-id), nor a `::', then we are not looking at a
6800 nested-name-specifier. */
6801 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6803 if (token->type == CPP_COLON
6804 && parser->colon_corrects_to_scope_p
6805 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME
6806 /* name:name is a valid sequence in an Objective C message. */
6807 && !parser->objective_c_message_context_p)
6809 gcc_rich_location richloc (token->location);
6810 richloc.add_fixit_replace ("::");
6811 error_at (&richloc,
6812 "found %<:%> in nested-name-specifier, "
6813 "expected %<::%>");
6814 token->type = CPP_SCOPE;
6817 if (token->type != CPP_SCOPE
6818 && !cp_parser_nth_token_starts_template_argument_list_p
6819 (parser, 2))
6820 break;
6823 /* The nested-name-specifier is optional, so we parse
6824 tentatively. */
6825 cp_parser_parse_tentatively (parser);
6827 /* Look for the optional `template' keyword, if this isn't the
6828 first time through the loop. */
6829 if (success)
6831 template_keyword_p = cp_parser_optional_template_keyword (parser);
6832 /* DR1710: "In a qualified-id used as the name in
6833 a typename-specifier, elaborated-type-specifier, using-declaration,
6834 or class-or-decltype, an optional keyword template appearing at
6835 the top level is ignored." */
6836 if (!template_keyword_p
6837 && typename_keyword_p
6838 && cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
6839 template_keyword_p = true;
6842 /* Save the old scope since the name lookup we are about to do
6843 might destroy it. */
6844 old_scope = parser->scope;
6845 saved_qualifying_scope = parser->qualifying_scope;
6846 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6847 look up names in "X<T>::I" in order to determine that "Y" is
6848 a template. So, if we have a typename at this point, we make
6849 an effort to look through it. */
6850 if (is_declaration
6851 && !check_dependency_p
6852 && !typename_keyword_p
6853 && parser->scope
6854 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6855 parser->scope = resolve_typename_type (parser->scope,
6856 /*only_current_p=*/false);
6857 /* Parse the qualifying entity. */
6858 new_scope
6859 = cp_parser_qualifying_entity (parser,
6860 typename_keyword_p,
6861 template_keyword_p,
6862 check_dependency_p,
6863 type_p,
6864 is_declaration);
6865 /* Look for the `::' token. */
6866 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6868 /* If we found what we wanted, we keep going; otherwise, we're
6869 done. */
6870 if (!cp_parser_parse_definitely (parser))
6872 bool error_p = false;
6874 /* Restore the OLD_SCOPE since it was valid before the
6875 failed attempt at finding the last
6876 class-or-namespace-name. */
6877 parser->scope = old_scope;
6878 parser->qualifying_scope = saved_qualifying_scope;
6880 /* If the next token is a decltype, and the one after that is a
6881 `::', then the decltype has failed to resolve to a class or
6882 enumeration type. Give this error even when parsing
6883 tentatively since it can't possibly be valid--and we're going
6884 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6885 won't get another chance.*/
6886 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6887 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6888 == CPP_SCOPE))
6890 token = cp_lexer_consume_token (parser->lexer);
6891 tree dtype = token->u.tree_check_value->value;
6892 if (dtype != error_mark_node)
6893 error_at (token->location, "%<decltype%> evaluates to %qT, "
6894 "which is not a class or enumeration type",
6895 dtype);
6896 parser->scope = error_mark_node;
6897 error_p = true;
6898 /* As below. */
6899 success = true;
6900 cp_lexer_consume_token (parser->lexer);
6903 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6904 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6906 /* If we have a non-type template-id followed by ::, it can't
6907 possibly be valid. */
6908 token = cp_lexer_peek_token (parser->lexer);
6909 tree tid = token->u.tree_check_value->value;
6910 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6911 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6913 tree tmpl = NULL_TREE;
6914 if (is_overloaded_fn (tid))
6916 tree fns = get_fns (tid);
6917 if (OVL_SINGLE_P (fns))
6918 tmpl = OVL_FIRST (fns);
6919 if (function_concept_p (fns))
6920 error_at (token->location, "concept-id %qD "
6921 "in nested-name-specifier", tid);
6922 else
6923 error_at (token->location, "function template-id "
6924 "%qD in nested-name-specifier", tid);
6926 else
6928 tmpl = TREE_OPERAND (tid, 0);
6929 if (variable_concept_p (tmpl)
6930 || standard_concept_p (tmpl))
6931 error_at (token->location, "concept-id %qD "
6932 "in nested-name-specifier", tid);
6933 else
6935 /* Variable template. */
6936 gcc_assert (variable_template_p (tmpl));
6937 error_at (token->location, "variable template-id "
6938 "%qD in nested-name-specifier", tid);
6941 if (tmpl)
6942 inform (DECL_SOURCE_LOCATION (tmpl),
6943 "%qD declared here", tmpl);
6945 parser->scope = error_mark_node;
6946 error_p = true;
6947 /* As below. */
6948 success = true;
6949 cp_lexer_consume_token (parser->lexer);
6950 cp_lexer_consume_token (parser->lexer);
6954 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6955 break;
6956 /* If the next token is an identifier, and the one after
6957 that is a `::', then any valid interpretation would have
6958 found a class-or-namespace-name. */
6959 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6960 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6961 == CPP_SCOPE)
6962 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6963 != CPP_COMPL))
6965 token = cp_lexer_consume_token (parser->lexer);
6966 if (!error_p)
6968 if (!token->error_reported)
6970 tree decl;
6971 tree ambiguous_decls;
6973 decl = cp_parser_lookup_name (parser, token->u.value,
6974 none_type,
6975 /*is_template=*/false,
6976 /*is_namespace=*/false,
6977 /*check_dependency=*/true,
6978 &ambiguous_decls,
6979 token->location);
6980 if (TREE_CODE (decl) == TEMPLATE_DECL)
6981 error_at (token->location,
6982 "%qD used without template arguments",
6983 decl);
6984 else if (ambiguous_decls)
6986 // cp_parser_lookup_name has the same diagnostic,
6987 // thus make sure to emit it at most once.
6988 if (cp_parser_uncommitted_to_tentative_parse_p
6989 (parser))
6991 error_at (token->location,
6992 "reference to %qD is ambiguous",
6993 token->u.value);
6994 print_candidates (ambiguous_decls);
6996 decl = error_mark_node;
6998 else
7000 if (cxx_dialect != cxx98)
7001 cp_parser_name_lookup_error
7002 (parser, token->u.value, decl, NLE_NOT_CXX98,
7003 token->location);
7004 else
7005 cp_parser_name_lookup_error
7006 (parser, token->u.value, decl, NLE_CXX98,
7007 token->location);
7010 parser->scope = error_mark_node;
7011 error_p = true;
7012 /* Treat this as a successful nested-name-specifier
7013 due to:
7015 [basic.lookup.qual]
7017 If the name found is not a class-name (clause
7018 _class_) or namespace-name (_namespace.def_), the
7019 program is ill-formed. */
7020 success = true;
7022 cp_lexer_consume_token (parser->lexer);
7024 break;
7026 /* We've found one valid nested-name-specifier. */
7027 success = true;
7028 /* Name lookup always gives us a DECL. */
7029 if (TREE_CODE (new_scope) == TYPE_DECL)
7030 new_scope = TREE_TYPE (new_scope);
7031 /* Uses of "template" must be followed by actual templates. */
7032 if (template_keyword_p)
7033 check_template_keyword_in_nested_name_spec (new_scope);
7034 /* If it is a class scope, try to complete it; we are about to
7035 be looking up names inside the class. */
7036 if (TYPE_P (new_scope)
7037 /* Since checking types for dependency can be expensive,
7038 avoid doing it if the type is already complete. */
7039 && !COMPLETE_TYPE_P (new_scope)
7040 /* Do not try to complete dependent types. */
7041 && !dependent_type_p (new_scope))
7043 new_scope = complete_type (new_scope);
7044 /* If it is a typedef to current class, use the current
7045 class instead, as the typedef won't have any names inside
7046 it yet. */
7047 if (!COMPLETE_TYPE_P (new_scope)
7048 && currently_open_class (new_scope))
7049 new_scope = TYPE_MAIN_VARIANT (new_scope);
7051 /* Make sure we look in the right scope the next time through
7052 the loop. */
7053 parser->scope = new_scope;
7056 /* If parsing tentatively, replace the sequence of tokens that makes
7057 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
7058 token. That way, should we re-parse the token stream, we will
7059 not have to repeat the effort required to do the parse, nor will
7060 we issue duplicate error messages. */
7061 if (success && start)
7063 cp_token *token;
7065 token = cp_lexer_token_at (parser->lexer, start);
7066 /* Reset the contents of the START token. */
7067 token->type = CPP_NESTED_NAME_SPECIFIER;
7068 /* Retrieve any deferred checks. Do not pop this access checks yet
7069 so the memory will not be reclaimed during token replacing below. */
7070 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
7071 token->tree_check_p = true;
7072 token->u.tree_check_value->value = parser->scope;
7073 token->u.tree_check_value->checks = get_deferred_access_checks ();
7074 token->u.tree_check_value->qualifying_scope =
7075 parser->qualifying_scope;
7076 token->keyword = RID_MAX;
7078 /* Purge all subsequent tokens. */
7079 cp_lexer_purge_tokens_after (parser->lexer, start);
7082 if (start)
7083 pop_to_parent_deferring_access_checks ();
7085 return success ? parser->scope : NULL_TREE;
7088 /* Parse a nested-name-specifier. See
7089 cp_parser_nested_name_specifier_opt for details. This function
7090 behaves identically, except that it will an issue an error if no
7091 nested-name-specifier is present. */
7093 static tree
7094 cp_parser_nested_name_specifier (cp_parser *parser,
7095 bool typename_keyword_p,
7096 bool check_dependency_p,
7097 bool type_p,
7098 bool is_declaration)
7100 tree scope;
7102 /* Look for the nested-name-specifier. */
7103 scope = cp_parser_nested_name_specifier_opt (parser,
7104 typename_keyword_p,
7105 check_dependency_p,
7106 type_p,
7107 is_declaration);
7108 /* If it was not present, issue an error message. */
7109 if (!scope)
7111 cp_parser_error (parser, "expected nested-name-specifier");
7112 parser->scope = NULL_TREE;
7115 return scope;
7118 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
7119 this is either a class-name or a namespace-name (which corresponds
7120 to the class-or-namespace-name production in the grammar). For
7121 C++0x, it can also be a type-name that refers to an enumeration
7122 type or a simple-template-id.
7124 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
7125 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
7126 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
7127 TYPE_P is TRUE iff the next name should be taken as a class-name,
7128 even the same name is declared to be another entity in the same
7129 scope.
7131 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
7132 specified by the class-or-namespace-name. If neither is found the
7133 ERROR_MARK_NODE is returned. */
7135 static tree
7136 cp_parser_qualifying_entity (cp_parser *parser,
7137 bool typename_keyword_p,
7138 bool template_keyword_p,
7139 bool check_dependency_p,
7140 bool type_p,
7141 bool is_declaration)
7143 tree saved_scope;
7144 tree saved_qualifying_scope;
7145 tree saved_object_scope;
7146 tree scope;
7147 bool only_class_p;
7148 bool successful_parse_p;
7150 /* DR 743: decltype can appear in a nested-name-specifier. */
7151 if (cp_lexer_next_token_is_decltype (parser->lexer))
7153 scope = cp_parser_decltype (parser);
7154 if (TREE_CODE (scope) != ENUMERAL_TYPE
7155 && !MAYBE_CLASS_TYPE_P (scope))
7157 cp_parser_simulate_error (parser);
7158 return error_mark_node;
7160 if (TYPE_NAME (scope))
7161 scope = TYPE_NAME (scope);
7162 return scope;
7165 /* Before we try to parse the class-name, we must save away the
7166 current PARSER->SCOPE since cp_parser_class_name will destroy
7167 it. */
7168 saved_scope = parser->scope;
7169 saved_qualifying_scope = parser->qualifying_scope;
7170 saved_object_scope = parser->object_scope;
7171 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
7172 there is no need to look for a namespace-name. */
7173 only_class_p = template_keyword_p
7174 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
7175 if (!only_class_p)
7176 cp_parser_parse_tentatively (parser);
7177 scope = cp_parser_class_name (parser,
7178 typename_keyword_p,
7179 template_keyword_p,
7180 type_p ? class_type : none_type,
7181 check_dependency_p,
7182 /*class_head_p=*/false,
7183 is_declaration,
7184 /*enum_ok=*/cxx_dialect > cxx98);
7185 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
7186 /* If that didn't work, try for a namespace-name. */
7187 if (!only_class_p && !successful_parse_p)
7189 /* Restore the saved scope. */
7190 parser->scope = saved_scope;
7191 parser->qualifying_scope = saved_qualifying_scope;
7192 parser->object_scope = saved_object_scope;
7193 /* If we are not looking at an identifier followed by the scope
7194 resolution operator, then this is not part of a
7195 nested-name-specifier. (Note that this function is only used
7196 to parse the components of a nested-name-specifier.) */
7197 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
7198 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
7199 return error_mark_node;
7200 scope = cp_parser_namespace_name (parser);
7203 return scope;
7206 /* Return true if we are looking at a compound-literal, false otherwise. */
7208 static bool
7209 cp_parser_compound_literal_p (cp_parser *parser)
7211 cp_lexer_save_tokens (parser->lexer);
7213 /* Skip tokens until the next token is a closing parenthesis.
7214 If we find the closing `)', and the next token is a `{', then
7215 we are looking at a compound-literal. */
7216 bool compound_literal_p
7217 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7218 /*consume_paren=*/true)
7219 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7221 /* Roll back the tokens we skipped. */
7222 cp_lexer_rollback_tokens (parser->lexer);
7224 return compound_literal_p;
7227 /* Return true if EXPR is the integer constant zero or a complex constant
7228 of zero, without any folding, but ignoring location wrappers. */
7230 bool
7231 literal_integer_zerop (const_tree expr)
7233 return (location_wrapper_p (expr)
7234 && integer_zerop (TREE_OPERAND (expr, 0)));
7237 /* Parse a postfix-expression.
7239 postfix-expression:
7240 primary-expression
7241 postfix-expression [ expression ]
7242 postfix-expression ( expression-list [opt] )
7243 simple-type-specifier ( expression-list [opt] )
7244 typename :: [opt] nested-name-specifier identifier
7245 ( expression-list [opt] )
7246 typename :: [opt] nested-name-specifier template [opt] template-id
7247 ( expression-list [opt] )
7248 postfix-expression . template [opt] id-expression
7249 postfix-expression -> template [opt] id-expression
7250 postfix-expression . pseudo-destructor-name
7251 postfix-expression -> pseudo-destructor-name
7252 postfix-expression ++
7253 postfix-expression --
7254 dynamic_cast < type-id > ( expression )
7255 static_cast < type-id > ( expression )
7256 reinterpret_cast < type-id > ( expression )
7257 const_cast < type-id > ( expression )
7258 typeid ( expression )
7259 typeid ( type-id )
7261 GNU Extension:
7263 postfix-expression:
7264 ( type-id ) { initializer-list , [opt] }
7266 This extension is a GNU version of the C99 compound-literal
7267 construct. (The C99 grammar uses `type-name' instead of `type-id',
7268 but they are essentially the same concept.)
7270 If ADDRESS_P is true, the postfix expression is the operand of the
7271 `&' operator. CAST_P is true if this expression is the target of a
7272 cast.
7274 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7275 class member access expressions [expr.ref].
7277 Returns a representation of the expression. */
7279 static cp_expr
7280 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
7281 bool member_access_only_p, bool decltype_p,
7282 cp_id_kind * pidk_return)
7284 cp_token *token;
7285 location_t loc;
7286 enum rid keyword;
7287 cp_id_kind idk = CP_ID_KIND_NONE;
7288 cp_expr postfix_expression = NULL_TREE;
7289 bool is_member_access = false;
7291 /* Peek at the next token. */
7292 token = cp_lexer_peek_token (parser->lexer);
7293 loc = token->location;
7294 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
7296 /* Some of the productions are determined by keywords. */
7297 keyword = token->keyword;
7298 switch (keyword)
7300 case RID_DYNCAST:
7301 case RID_STATCAST:
7302 case RID_REINTCAST:
7303 case RID_CONSTCAST:
7305 tree type;
7306 cp_expr expression;
7307 const char *saved_message;
7308 bool saved_in_type_id_in_expr_p;
7310 /* All of these can be handled in the same way from the point
7311 of view of parsing. Begin by consuming the token
7312 identifying the cast. */
7313 cp_lexer_consume_token (parser->lexer);
7315 /* New types cannot be defined in the cast. */
7316 saved_message = parser->type_definition_forbidden_message;
7317 parser->type_definition_forbidden_message
7318 = G_("types may not be defined in casts");
7320 /* Look for the opening `<'. */
7321 cp_parser_require (parser, CPP_LESS, RT_LESS);
7322 /* Parse the type to which we are casting. */
7323 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7324 parser->in_type_id_in_expr_p = true;
7325 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
7326 NULL);
7327 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7328 /* Look for the closing `>'. */
7329 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
7330 /* Restore the old message. */
7331 parser->type_definition_forbidden_message = saved_message;
7333 bool saved_greater_than_is_operator_p
7334 = parser->greater_than_is_operator_p;
7335 parser->greater_than_is_operator_p = true;
7337 /* And the expression which is being cast. */
7338 matching_parens parens;
7339 parens.require_open (parser);
7340 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
7341 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
7342 RT_CLOSE_PAREN);
7343 location_t end_loc = close_paren ?
7344 close_paren->location : UNKNOWN_LOCATION;
7346 parser->greater_than_is_operator_p
7347 = saved_greater_than_is_operator_p;
7349 /* Only type conversions to integral or enumeration types
7350 can be used in constant-expressions. */
7351 if (!cast_valid_in_integral_constant_expression_p (type)
7352 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
7354 postfix_expression = error_mark_node;
7355 break;
7358 /* Construct a location e.g. :
7359 reinterpret_cast <int *> (expr)
7360 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7361 ranging from the start of the "*_cast" token to the final closing
7362 paren, with the caret at the start. */
7363 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
7365 switch (keyword)
7367 case RID_DYNCAST:
7368 postfix_expression
7369 = build_dynamic_cast (cp_cast_loc, type, expression,
7370 tf_warning_or_error);
7371 break;
7372 case RID_STATCAST:
7373 postfix_expression
7374 = build_static_cast (cp_cast_loc, type, expression,
7375 tf_warning_or_error);
7376 break;
7377 case RID_REINTCAST:
7378 postfix_expression
7379 = build_reinterpret_cast (cp_cast_loc, type, expression,
7380 tf_warning_or_error);
7381 break;
7382 case RID_CONSTCAST:
7383 postfix_expression
7384 = build_const_cast (cp_cast_loc, type, expression,
7385 tf_warning_or_error);
7386 break;
7387 default:
7388 gcc_unreachable ();
7391 break;
7393 case RID_TYPEID:
7395 tree type;
7396 const char *saved_message;
7397 bool saved_in_type_id_in_expr_p;
7399 /* Consume the `typeid' token. */
7400 cp_lexer_consume_token (parser->lexer);
7401 /* Look for the `(' token. */
7402 matching_parens parens;
7403 parens.require_open (parser);
7404 /* Types cannot be defined in a `typeid' expression. */
7405 saved_message = parser->type_definition_forbidden_message;
7406 parser->type_definition_forbidden_message
7407 = G_("types may not be defined in a %<typeid%> expression");
7408 /* We can't be sure yet whether we're looking at a type-id or an
7409 expression. */
7410 cp_parser_parse_tentatively (parser);
7411 /* Try a type-id first. */
7412 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7413 parser->in_type_id_in_expr_p = true;
7414 type = cp_parser_type_id (parser);
7415 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7416 /* Look for the `)' token. Otherwise, we can't be sure that
7417 we're not looking at an expression: consider `typeid (int
7418 (3))', for example. */
7419 cp_token *close_paren = parens.require_close (parser);
7420 /* If all went well, simply lookup the type-id. */
7421 if (cp_parser_parse_definitely (parser))
7422 postfix_expression = get_typeid (type, tf_warning_or_error);
7423 /* Otherwise, fall back to the expression variant. */
7424 else
7426 tree expression;
7428 /* Look for an expression. */
7429 expression = cp_parser_expression (parser, & idk);
7430 /* Compute its typeid. */
7431 postfix_expression = build_typeid (expression, tf_warning_or_error);
7432 /* Look for the `)' token. */
7433 close_paren = parens.require_close (parser);
7435 /* Restore the saved message. */
7436 parser->type_definition_forbidden_message = saved_message;
7437 /* `typeid' may not appear in an integral constant expression. */
7438 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
7439 postfix_expression = error_mark_node;
7441 /* Construct a location e.g. :
7442 typeid (expr)
7443 ^~~~~~~~~~~~~
7444 ranging from the start of the "typeid" token to the final closing
7445 paren, with the caret at the start. */
7446 if (close_paren)
7448 location_t typeid_loc
7449 = make_location (start_loc, start_loc, close_paren->location);
7450 postfix_expression.set_location (typeid_loc);
7451 postfix_expression.maybe_add_location_wrapper ();
7454 break;
7456 case RID_TYPENAME:
7458 tree type;
7459 /* The syntax permitted here is the same permitted for an
7460 elaborated-type-specifier. */
7461 ++parser->prevent_constrained_type_specifiers;
7462 type = cp_parser_elaborated_type_specifier (parser,
7463 /*is_friend=*/false,
7464 /*is_declaration=*/false);
7465 --parser->prevent_constrained_type_specifiers;
7466 postfix_expression = cp_parser_functional_cast (parser, type);
7468 break;
7470 case RID_ADDRESSOF:
7471 case RID_BUILTIN_SHUFFLE:
7472 case RID_BUILTIN_SHUFFLEVECTOR:
7473 case RID_BUILTIN_LAUNDER:
7474 case RID_BUILTIN_ASSOC_BARRIER:
7476 vec<tree, va_gc> *vec;
7478 cp_lexer_consume_token (parser->lexer);
7479 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
7480 /*cast_p=*/false, /*allow_expansion_p=*/true,
7481 /*non_constant_p=*/NULL);
7482 if (vec == NULL)
7484 postfix_expression = error_mark_node;
7485 break;
7488 for (tree p : *vec)
7489 mark_exp_read (p);
7491 switch (keyword)
7493 case RID_ADDRESSOF:
7494 if (vec->length () == 1)
7495 postfix_expression
7496 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
7497 else
7499 error_at (loc, "wrong number of arguments to "
7500 "%<__builtin_addressof%>");
7501 postfix_expression = error_mark_node;
7503 break;
7505 case RID_BUILTIN_LAUNDER:
7506 if (vec->length () == 1)
7507 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
7508 tf_warning_or_error);
7509 else
7511 error_at (loc, "wrong number of arguments to "
7512 "%<__builtin_launder%>");
7513 postfix_expression = error_mark_node;
7515 break;
7517 case RID_BUILTIN_ASSOC_BARRIER:
7518 if (vec->length () == 1)
7519 postfix_expression = build1_loc (loc, PAREN_EXPR,
7520 TREE_TYPE ((*vec)[0]),
7521 (*vec)[0]);
7522 else
7524 error_at (loc, "wrong number of arguments to "
7525 "%<__builtin_assoc_barrier%>");
7526 postfix_expression = error_mark_node;
7528 break;
7530 case RID_BUILTIN_SHUFFLE:
7531 if (vec->length () == 2)
7532 postfix_expression
7533 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
7534 (*vec)[1], tf_warning_or_error);
7535 else if (vec->length () == 3)
7536 postfix_expression
7537 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
7538 (*vec)[2], tf_warning_or_error);
7539 else
7541 error_at (loc, "wrong number of arguments to "
7542 "%<__builtin_shuffle%>");
7543 postfix_expression = error_mark_node;
7545 break;
7547 case RID_BUILTIN_SHUFFLEVECTOR:
7548 if (vec->length () < 3)
7550 error_at (loc, "wrong number of arguments to "
7551 "%<__builtin_shufflevector%>");
7552 postfix_expression = error_mark_node;
7554 else
7556 postfix_expression
7557 = build_x_shufflevector (loc, vec, tf_warning_or_error);
7559 break;
7561 default:
7562 gcc_unreachable ();
7564 break;
7567 case RID_BUILTIN_CONVERTVECTOR:
7569 tree expression;
7570 tree type;
7571 /* Consume the `__builtin_convertvector' token. */
7572 cp_lexer_consume_token (parser->lexer);
7573 /* Look for the opening `('. */
7574 matching_parens parens;
7575 parens.require_open (parser);
7576 /* Now, parse the assignment-expression. */
7577 expression = cp_parser_assignment_expression (parser);
7578 /* Look for the `,'. */
7579 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7580 location_t type_location
7581 = cp_lexer_peek_token (parser->lexer)->location;
7582 /* Parse the type-id. */
7584 type_id_in_expr_sentinel s (parser);
7585 type = cp_parser_type_id (parser);
7587 /* Look for the closing `)'. */
7588 parens.require_close (parser);
7589 postfix_expression
7590 = cp_build_vec_convert (expression, type_location, type,
7591 tf_warning_or_error);
7592 break;
7595 case RID_BUILTIN_BIT_CAST:
7597 tree expression;
7598 tree type;
7599 /* Consume the `__builtin_bit_cast' token. */
7600 cp_lexer_consume_token (parser->lexer);
7601 /* Look for the opening `('. */
7602 matching_parens parens;
7603 parens.require_open (parser);
7604 location_t type_location
7605 = cp_lexer_peek_token (parser->lexer)->location;
7606 /* Parse the type-id. */
7608 type_id_in_expr_sentinel s (parser);
7609 type = cp_parser_type_id (parser);
7611 /* Look for the `,'. */
7612 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7613 /* Now, parse the assignment-expression. */
7614 expression = cp_parser_assignment_expression (parser);
7615 /* Look for the closing `)'. */
7616 parens.require_close (parser);
7617 postfix_expression
7618 = cp_build_bit_cast (type_location, type, expression,
7619 tf_warning_or_error);
7620 break;
7623 default:
7625 tree type;
7627 /* If the next thing is a simple-type-specifier, we may be
7628 looking at a functional cast. We could also be looking at
7629 an id-expression. So, we try the functional cast, and if
7630 that doesn't work we fall back to the primary-expression. */
7631 cp_parser_parse_tentatively (parser);
7632 /* Look for the simple-type-specifier. */
7633 ++parser->prevent_constrained_type_specifiers;
7634 type = cp_parser_simple_type_specifier (parser,
7635 /*decl_specs=*/NULL,
7636 CP_PARSER_FLAGS_NONE);
7637 --parser->prevent_constrained_type_specifiers;
7638 /* Parse the cast itself. */
7639 if (!cp_parser_error_occurred (parser))
7640 postfix_expression
7641 = cp_parser_functional_cast (parser, type);
7642 /* If that worked, we're done. */
7643 if (cp_parser_parse_definitely (parser))
7644 break;
7646 /* If the functional-cast didn't work out, try a
7647 compound-literal. */
7648 if (cp_parser_allow_gnu_extensions_p (parser)
7649 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7651 cp_expr initializer = NULL_TREE;
7653 cp_parser_parse_tentatively (parser);
7655 matching_parens parens;
7656 parens.consume_open (parser);
7658 /* Avoid calling cp_parser_type_id pointlessly, see comment
7659 in cp_parser_cast_expression about c++/29234. */
7660 if (!cp_parser_compound_literal_p (parser))
7661 cp_parser_simulate_error (parser);
7662 else
7664 /* Parse the type. */
7665 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7666 parser->in_type_id_in_expr_p = true;
7667 type = cp_parser_type_id (parser);
7668 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7669 parens.require_close (parser);
7672 /* If things aren't going well, there's no need to
7673 keep going. */
7674 if (!cp_parser_error_occurred (parser))
7676 bool non_constant_p;
7677 /* Parse the brace-enclosed initializer list. */
7678 initializer = cp_parser_braced_list (parser,
7679 &non_constant_p);
7681 /* If that worked, we're definitely looking at a
7682 compound-literal expression. */
7683 if (cp_parser_parse_definitely (parser))
7685 /* Warn the user that a compound literal is not
7686 allowed in standard C++. */
7687 pedwarn (input_location, OPT_Wpedantic,
7688 "ISO C++ forbids compound-literals");
7689 /* For simplicity, we disallow compound literals in
7690 constant-expressions. We could
7691 allow compound literals of integer type, whose
7692 initializer was a constant, in constant
7693 expressions. Permitting that usage, as a further
7694 extension, would not change the meaning of any
7695 currently accepted programs. (Of course, as
7696 compound literals are not part of ISO C++, the
7697 standard has nothing to say.) */
7698 if (cp_parser_non_integral_constant_expression (parser,
7699 NIC_NCC))
7701 postfix_expression = error_mark_node;
7702 break;
7704 /* Form the representation of the compound-literal. */
7705 postfix_expression
7706 = finish_compound_literal (type, initializer,
7707 tf_warning_or_error, fcl_c99);
7708 postfix_expression.set_location (initializer.get_location ());
7709 break;
7713 /* It must be a primary-expression. */
7714 postfix_expression
7715 = cp_parser_primary_expression (parser, address_p, cast_p,
7716 /*template_arg_p=*/false,
7717 decltype_p,
7718 &idk);
7720 break;
7723 /* Note that we don't need to worry about calling build_cplus_new on a
7724 class-valued CALL_EXPR in decltype when it isn't the end of the
7725 postfix-expression; unary_complex_lvalue will take care of that for
7726 all these cases. */
7728 /* Keep looping until the postfix-expression is complete. */
7729 while (true)
7731 if (idk == CP_ID_KIND_UNQUALIFIED
7732 && identifier_p (postfix_expression)
7733 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7734 /* It is not a Koenig lookup function call. */
7735 postfix_expression
7736 = unqualified_name_lookup_error (postfix_expression);
7738 /* Peek at the next token. */
7739 token = cp_lexer_peek_token (parser->lexer);
7741 switch (token->type)
7743 case CPP_OPEN_SQUARE:
7744 if (cp_next_tokens_can_be_std_attribute_p (parser))
7746 cp_parser_error (parser,
7747 "two consecutive %<[%> shall "
7748 "only introduce an attribute");
7749 return error_mark_node;
7751 postfix_expression
7752 = cp_parser_postfix_open_square_expression (parser,
7753 postfix_expression,
7754 false,
7755 decltype_p);
7756 postfix_expression.set_range (start_loc,
7757 postfix_expression.get_location ());
7759 idk = CP_ID_KIND_NONE;
7760 is_member_access = false;
7761 break;
7763 case CPP_OPEN_PAREN:
7764 /* postfix-expression ( expression-list [opt] ) */
7766 bool koenig_p;
7767 bool is_builtin_constant_p;
7768 bool saved_integral_constant_expression_p = false;
7769 bool saved_non_integral_constant_expression_p = false;
7770 tsubst_flags_t complain = complain_flags (decltype_p);
7771 vec<tree, va_gc> *args;
7772 location_t close_paren_loc = UNKNOWN_LOCATION;
7773 location_t combined_loc = UNKNOWN_LOCATION;
7775 is_member_access = false;
7777 tree stripped_expression
7778 = tree_strip_any_location_wrapper (postfix_expression);
7779 is_builtin_constant_p
7780 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression);
7781 if (is_builtin_constant_p)
7783 /* The whole point of __builtin_constant_p is to allow
7784 non-constant expressions to appear as arguments. */
7785 saved_integral_constant_expression_p
7786 = parser->integral_constant_expression_p;
7787 saved_non_integral_constant_expression_p
7788 = parser->non_integral_constant_expression_p;
7789 parser->integral_constant_expression_p = false;
7791 args = (cp_parser_parenthesized_expression_list
7792 (parser, non_attr,
7793 /*cast_p=*/false, /*allow_expansion_p=*/true,
7794 /*non_constant_p=*/NULL,
7795 /*close_paren_loc=*/&close_paren_loc,
7796 /*wrap_locations_p=*/true));
7797 if (is_builtin_constant_p)
7799 parser->integral_constant_expression_p
7800 = saved_integral_constant_expression_p;
7801 parser->non_integral_constant_expression_p
7802 = saved_non_integral_constant_expression_p;
7805 if (args == NULL)
7807 postfix_expression = error_mark_node;
7808 break;
7811 /* Function calls are not permitted in
7812 constant-expressions. */
7813 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7814 && cp_parser_non_integral_constant_expression (parser,
7815 NIC_FUNC_CALL))
7817 postfix_expression = error_mark_node;
7818 release_tree_vector (args);
7819 break;
7822 koenig_p = false;
7823 if (idk == CP_ID_KIND_UNQUALIFIED
7824 || idk == CP_ID_KIND_TEMPLATE_ID)
7826 if (identifier_p (postfix_expression)
7827 /* In C++20, we may need to perform ADL for a template
7828 name. */
7829 || (TREE_CODE (postfix_expression) == TEMPLATE_ID_EXPR
7830 && identifier_p (TREE_OPERAND (postfix_expression, 0))))
7832 if (!args->is_empty ())
7834 koenig_p = true;
7835 if (!any_type_dependent_arguments_p (args))
7836 postfix_expression
7837 = perform_koenig_lookup (postfix_expression, args,
7838 complain);
7840 else
7841 postfix_expression
7842 = unqualified_fn_lookup_error (postfix_expression);
7844 /* We do not perform argument-dependent lookup if
7845 normal lookup finds a non-function, in accordance
7846 with the expected resolution of DR 218. */
7847 else if (!args->is_empty ()
7848 && is_overloaded_fn (postfix_expression))
7850 /* Do not do argument dependent lookup if regular
7851 lookup finds a member function or a block-scope
7852 function declaration. [basic.lookup.argdep]/3 */
7853 bool do_adl_p = true;
7854 tree fns = get_fns (postfix_expression);
7855 for (lkp_iterator iter (fns); iter; ++iter)
7857 tree fn = STRIP_TEMPLATE (*iter);
7858 if ((TREE_CODE (fn) == USING_DECL
7859 && DECL_DEPENDENT_P (fn))
7860 || DECL_FUNCTION_MEMBER_P (fn)
7861 || DECL_LOCAL_DECL_P (fn))
7863 do_adl_p = false;
7864 break;
7868 if (do_adl_p)
7870 koenig_p = true;
7871 if (!any_type_dependent_arguments_p (args))
7872 postfix_expression
7873 = perform_koenig_lookup (postfix_expression, args,
7874 complain);
7879 /* Temporarily set input_location to the combined location
7880 with call expression range, as e.g. build_out_target_exprs
7881 called from convert_default_arg relies on input_location,
7882 so updating it only when the call is fully built results
7883 in inconsistencies between location handling in templates
7884 and outside of templates. */
7885 if (close_paren_loc != UNKNOWN_LOCATION)
7886 combined_loc = make_location (token->location, start_loc,
7887 close_paren_loc);
7888 iloc_sentinel ils (combined_loc);
7890 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7892 tree instance = TREE_OPERAND (postfix_expression, 0);
7893 tree fn = TREE_OPERAND (postfix_expression, 1);
7895 if (processing_template_decl
7896 && (type_dependent_object_expression_p (instance)
7897 || (!BASELINK_P (fn)
7898 && TREE_CODE (fn) != FIELD_DECL)
7899 || type_dependent_expression_p (fn)
7900 || any_type_dependent_arguments_p (args)))
7902 maybe_generic_this_capture (instance, fn);
7903 postfix_expression
7904 = build_min_nt_call_vec (postfix_expression, args);
7906 else if (BASELINK_P (fn))
7908 postfix_expression
7909 = (build_new_method_call
7910 (instance, fn, &args, NULL_TREE,
7911 (idk == CP_ID_KIND_QUALIFIED
7912 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7913 : LOOKUP_NORMAL),
7914 /*fn_p=*/NULL,
7915 complain));
7917 else
7918 postfix_expression
7919 = finish_call_expr (postfix_expression, &args,
7920 /*disallow_virtual=*/false,
7921 /*koenig_p=*/false,
7922 complain);
7924 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7925 || TREE_CODE (postfix_expression) == MEMBER_REF
7926 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7927 postfix_expression = (build_offset_ref_call_from_tree
7928 (postfix_expression, &args,
7929 complain));
7930 else if (idk == CP_ID_KIND_QUALIFIED)
7931 /* A call to a static class member, or a namespace-scope
7932 function. */
7933 postfix_expression
7934 = finish_call_expr (postfix_expression, &args,
7935 /*disallow_virtual=*/true,
7936 koenig_p,
7937 complain);
7938 else
7939 /* All other function calls. */
7940 postfix_expression
7941 = finish_call_expr (postfix_expression, &args,
7942 /*disallow_virtual=*/false,
7943 koenig_p,
7944 complain);
7946 if (close_paren_loc != UNKNOWN_LOCATION)
7947 postfix_expression.set_location (combined_loc);
7949 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7950 idk = CP_ID_KIND_NONE;
7952 release_tree_vector (args);
7954 break;
7956 case CPP_DOT:
7957 case CPP_DEREF:
7958 /* postfix-expression . template [opt] id-expression
7959 postfix-expression . pseudo-destructor-name
7960 postfix-expression -> template [opt] id-expression
7961 postfix-expression -> pseudo-destructor-name */
7963 /* Consume the `.' or `->' operator. */
7964 cp_lexer_consume_token (parser->lexer);
7966 postfix_expression
7967 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7968 postfix_expression,
7969 false, &idk, loc);
7971 is_member_access = true;
7972 break;
7974 case CPP_PLUS_PLUS:
7975 /* postfix-expression ++ */
7976 /* Consume the `++' token. */
7977 cp_lexer_consume_token (parser->lexer);
7978 /* Generate a representation for the complete expression. */
7979 postfix_expression
7980 = finish_increment_expr (postfix_expression,
7981 POSTINCREMENT_EXPR);
7982 /* Increments may not appear in constant-expressions. */
7983 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7984 postfix_expression = error_mark_node;
7985 idk = CP_ID_KIND_NONE;
7986 is_member_access = false;
7987 break;
7989 case CPP_MINUS_MINUS:
7990 /* postfix-expression -- */
7991 /* Consume the `--' token. */
7992 cp_lexer_consume_token (parser->lexer);
7993 /* Generate a representation for the complete expression. */
7994 postfix_expression
7995 = finish_increment_expr (postfix_expression,
7996 POSTDECREMENT_EXPR);
7997 /* Decrements may not appear in constant-expressions. */
7998 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7999 postfix_expression = error_mark_node;
8000 idk = CP_ID_KIND_NONE;
8001 is_member_access = false;
8002 break;
8004 default:
8005 if (pidk_return != NULL)
8006 * pidk_return = idk;
8007 if (member_access_only_p)
8008 return is_member_access
8009 ? postfix_expression
8010 : cp_expr (error_mark_node);
8011 else
8012 return postfix_expression;
8017 /* Helper function for cp_parser_parenthesized_expression_list and
8018 cp_parser_postfix_open_square_expression. Parse a single element
8019 of parenthesized expression list. */
8021 static cp_expr
8022 cp_parser_parenthesized_expression_list_elt (cp_parser *parser, bool cast_p,
8023 bool allow_expansion_p,
8024 bool *non_constant_p)
8026 cp_expr expr (NULL_TREE);
8027 bool expr_non_constant_p;
8029 /* Parse the next assignment-expression. */
8030 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8032 /* A braced-init-list. */
8033 cp_lexer_set_source_position (parser->lexer);
8034 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8035 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8036 if (non_constant_p && expr_non_constant_p)
8037 *non_constant_p = true;
8039 else if (non_constant_p)
8041 expr = cp_parser_constant_expression (parser,
8042 /*allow_non_constant_p=*/true,
8043 &expr_non_constant_p);
8044 if (expr_non_constant_p)
8045 *non_constant_p = true;
8047 else
8048 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL, cast_p);
8050 /* If we have an ellipsis, then this is an expression expansion. */
8051 if (allow_expansion_p
8052 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8054 /* Consume the `...'. */
8055 cp_lexer_consume_token (parser->lexer);
8057 /* Build the argument pack. */
8058 expr = make_pack_expansion (expr);
8060 return expr;
8063 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8064 by cp_parser_builtin_offsetof. We're looking for
8066 postfix-expression [ expression ]
8067 postfix-expression [ braced-init-list ] (C++11)
8068 postfix-expression [ expression-list[opt] ] (C++23)
8070 FOR_OFFSETOF is set if we're being called in that context, which
8071 changes how we deal with integer constant expressions. */
8073 static tree
8074 cp_parser_postfix_open_square_expression (cp_parser *parser,
8075 tree postfix_expression,
8076 bool for_offsetof,
8077 bool decltype_p)
8079 tree index = NULL_TREE;
8080 releasing_vec expression_list = NULL;
8081 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8082 bool saved_greater_than_is_operator_p;
8084 /* Consume the `[' token. */
8085 cp_lexer_consume_token (parser->lexer);
8087 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
8088 parser->greater_than_is_operator_p = true;
8090 /* Parse the index expression. */
8091 /* ??? For offsetof, there is a question of what to allow here. If
8092 offsetof is not being used in an integral constant expression context,
8093 then we *could* get the right answer by computing the value at runtime.
8094 If we are in an integral constant expression context, then we might
8095 could accept any constant expression; hard to say without analysis.
8096 Rather than open the barn door too wide right away, allow only integer
8097 constant expressions here. */
8098 if (for_offsetof)
8099 index = cp_parser_constant_expression (parser);
8100 else
8102 if (cxx_dialect >= cxx23
8103 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
8104 *&expression_list = make_tree_vector ();
8105 else if (cxx_dialect >= cxx23)
8107 while (true)
8109 cp_expr expr
8110 = cp_parser_parenthesized_expression_list_elt (parser,
8111 /*cast_p=*/
8112 false,
8113 /*allow_exp_p=*/
8114 true,
8115 /*non_cst_p=*/
8116 NULL);
8118 if (expr == error_mark_node)
8119 index = error_mark_node;
8120 else if (expression_list.get () == NULL
8121 && !PACK_EXPANSION_P (expr.get_value ()))
8122 index = expr.get_value ();
8123 else
8124 vec_safe_push (expression_list, expr.get_value ());
8126 /* If the next token isn't a `,', then we are done. */
8127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8128 break;
8130 if (expression_list.get () == NULL && index != error_mark_node)
8132 *&expression_list = make_tree_vector_single (index);
8133 index = NULL_TREE;
8136 /* Otherwise, consume the `,' and keep going. */
8137 cp_lexer_consume_token (parser->lexer);
8139 if (expression_list.get () && index == error_mark_node)
8140 expression_list.release ();
8142 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8144 bool expr_nonconst_p;
8145 cp_lexer_set_source_position (parser->lexer);
8146 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8147 index = cp_parser_braced_list (parser, &expr_nonconst_p);
8149 else
8150 index = cp_parser_expression (parser, NULL, /*cast_p=*/false,
8151 /*decltype_p=*/false,
8152 /*warn_comma_p=*/warn_comma_subscript);
8155 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
8157 /* Look for the closing `]'. */
8158 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8160 /* Build the ARRAY_REF. */
8161 postfix_expression = grok_array_decl (loc, postfix_expression,
8162 index, &expression_list,
8163 tf_warning_or_error
8164 | (decltype_p ? tf_decltype : 0));
8166 /* When not doing offsetof, array references are not permitted in
8167 constant-expressions. */
8168 if (!for_offsetof
8169 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
8170 postfix_expression = error_mark_node;
8172 return postfix_expression;
8175 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
8176 dereference of incomplete type, returns true if error_mark_node should
8177 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
8178 and *DEPENDENT_P. */
8180 bool
8181 cp_parser_dot_deref_incomplete (tree *scope, cp_expr *postfix_expression,
8182 bool *dependent_p)
8184 /* In a template, be permissive by treating an object expression
8185 of incomplete type as dependent (after a pedwarn). */
8186 diagnostic_t kind = (processing_template_decl
8187 && MAYBE_CLASS_TYPE_P (*scope) ? DK_PEDWARN : DK_ERROR);
8189 switch (TREE_CODE (*postfix_expression))
8191 case CAST_EXPR:
8192 case REINTERPRET_CAST_EXPR:
8193 case CONST_CAST_EXPR:
8194 case STATIC_CAST_EXPR:
8195 case DYNAMIC_CAST_EXPR:
8196 case IMPLICIT_CONV_EXPR:
8197 case VIEW_CONVERT_EXPR:
8198 case NON_LVALUE_EXPR:
8199 kind = DK_ERROR;
8200 break;
8201 case OVERLOAD:
8202 /* Don't emit any diagnostic for OVERLOADs. */
8203 kind = DK_IGNORED;
8204 break;
8205 default:
8206 /* Avoid clobbering e.g. DECLs. */
8207 if (!EXPR_P (*postfix_expression))
8208 kind = DK_ERROR;
8209 break;
8212 if (kind == DK_IGNORED)
8213 return false;
8215 location_t exploc = location_of (*postfix_expression);
8216 cxx_incomplete_type_diagnostic (exploc, *postfix_expression, *scope, kind);
8217 if (!MAYBE_CLASS_TYPE_P (*scope))
8218 return true;
8219 if (kind == DK_ERROR)
8220 *scope = *postfix_expression = error_mark_node;
8221 else if (processing_template_decl)
8223 *dependent_p = true;
8224 *scope = TREE_TYPE (*postfix_expression) = NULL_TREE;
8226 return false;
8229 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
8230 by cp_parser_builtin_offsetof. We're looking for
8232 postfix-expression . template [opt] id-expression
8233 postfix-expression . pseudo-destructor-name
8234 postfix-expression -> template [opt] id-expression
8235 postfix-expression -> pseudo-destructor-name
8237 FOR_OFFSETOF is set if we're being called in that context. That sorta
8238 limits what of the above we'll actually accept, but nevermind.
8239 TOKEN_TYPE is the "." or "->" token, which will already have been
8240 removed from the stream. */
8242 static tree
8243 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
8244 enum cpp_ttype token_type,
8245 cp_expr postfix_expression,
8246 bool for_offsetof, cp_id_kind *idk,
8247 location_t location)
8249 tree name;
8250 bool dependent_p;
8251 bool pseudo_destructor_p;
8252 tree scope = NULL_TREE;
8253 location_t start_loc = postfix_expression.get_start ();
8255 /* If this is a `->' operator, dereference the pointer. */
8256 if (token_type == CPP_DEREF)
8257 postfix_expression = build_x_arrow (location, postfix_expression,
8258 tf_warning_or_error);
8259 /* Check to see whether or not the expression is type-dependent and
8260 not the current instantiation. */
8261 dependent_p = type_dependent_object_expression_p (postfix_expression);
8262 /* The identifier following the `->' or `.' is not qualified. */
8263 parser->scope = NULL_TREE;
8264 parser->qualifying_scope = NULL_TREE;
8265 parser->object_scope = NULL_TREE;
8266 *idk = CP_ID_KIND_NONE;
8268 /* Enter the scope corresponding to the type of the object
8269 given by the POSTFIX_EXPRESSION. */
8270 if (!dependent_p)
8272 scope = TREE_TYPE (postfix_expression);
8273 /* According to the standard, no expression should ever have
8274 reference type. Unfortunately, we do not currently match
8275 the standard in this respect in that our internal representation
8276 of an expression may have reference type even when the standard
8277 says it does not. Therefore, we have to manually obtain the
8278 underlying type here. */
8279 scope = non_reference (scope);
8280 /* The type of the POSTFIX_EXPRESSION must be complete. */
8281 /* Unlike the object expression in other contexts, *this is not
8282 required to be of complete type for purposes of class member
8283 access (5.2.5) outside the member function body. */
8284 if (postfix_expression != current_class_ref
8285 && scope != error_mark_node
8286 && !currently_open_class (scope))
8288 scope = complete_type (scope);
8289 if (!COMPLETE_TYPE_P (scope)
8290 && cp_parser_dot_deref_incomplete (&scope, &postfix_expression,
8291 &dependent_p))
8292 return error_mark_node;
8295 if (!dependent_p)
8297 /* Let the name lookup machinery know that we are processing a
8298 class member access expression. */
8299 parser->context->object_type = scope;
8300 /* If something went wrong, we want to be able to discern that case,
8301 as opposed to the case where there was no SCOPE due to the type
8302 of expression being dependent. */
8303 if (!scope)
8304 scope = error_mark_node;
8305 /* If the SCOPE was erroneous, make the various semantic analysis
8306 functions exit quickly -- and without issuing additional error
8307 messages. */
8308 if (scope == error_mark_node)
8309 postfix_expression = error_mark_node;
8313 if (dependent_p)
8315 tree type = TREE_TYPE (postfix_expression);
8316 /* If we don't have a (type-dependent) object of class type, use
8317 typeof to figure out the type of the object. */
8318 if (type == NULL_TREE || is_auto (type))
8319 type = finish_typeof (postfix_expression);
8320 parser->context->object_type = type;
8323 /* Assume this expression is not a pseudo-destructor access. */
8324 pseudo_destructor_p = false;
8326 /* If the SCOPE is a scalar type, then, if this is a valid program,
8327 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8328 is type dependent, it can be pseudo-destructor-name or something else.
8329 Try to parse it as pseudo-destructor-name first. */
8330 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
8332 tree s;
8333 tree type;
8335 cp_parser_parse_tentatively (parser);
8336 /* Parse the pseudo-destructor-name. */
8337 s = NULL_TREE;
8338 cp_parser_pseudo_destructor_name (parser, postfix_expression,
8339 &s, &type);
8340 if (dependent_p
8341 && (cp_parser_error_occurred (parser)
8342 || !SCALAR_TYPE_P (type)))
8343 cp_parser_abort_tentative_parse (parser);
8344 else if (cp_parser_parse_definitely (parser))
8346 pseudo_destructor_p = true;
8347 postfix_expression
8348 = finish_pseudo_destructor_expr (postfix_expression,
8349 s, type, location);
8353 if (!pseudo_destructor_p)
8355 /* If the SCOPE is not a scalar type, we are looking at an
8356 ordinary class member access expression, rather than a
8357 pseudo-destructor-name. */
8358 bool template_p;
8359 cp_token *token = cp_lexer_peek_token (parser->lexer);
8360 /* Parse the id-expression. */
8361 name = (cp_parser_id_expression
8362 (parser,
8363 cp_parser_optional_template_keyword (parser),
8364 /*check_dependency_p=*/true,
8365 &template_p,
8366 /*declarator_p=*/false,
8367 /*optional_p=*/false));
8368 /* In general, build a SCOPE_REF if the member name is qualified.
8369 However, if the name was not dependent and has already been
8370 resolved; there is no need to build the SCOPE_REF. For example;
8372 struct X { void f(); };
8373 template <typename T> void f(T* t) { t->X::f(); }
8375 Even though "t" is dependent, "X::f" is not and has been resolved
8376 to a BASELINK; there is no need to include scope information. */
8378 /* But we do need to remember that there was an explicit scope for
8379 virtual function calls. */
8380 if (parser->scope)
8381 *idk = CP_ID_KIND_QUALIFIED;
8383 /* If the name is a template-id that names a type, we will get a
8384 TYPE_DECL here. That is invalid code. */
8385 if (TREE_CODE (name) == TYPE_DECL)
8387 error_at (token->location, "invalid use of %qD", name);
8388 postfix_expression = error_mark_node;
8390 else
8392 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
8394 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
8396 error_at (token->location, "%<%D::%D%> is not a class member",
8397 parser->scope, name);
8398 postfix_expression = error_mark_node;
8400 else
8401 name = build_qualified_name (/*type=*/NULL_TREE,
8402 parser->scope,
8403 name,
8404 template_p);
8405 parser->scope = NULL_TREE;
8406 parser->qualifying_scope = NULL_TREE;
8407 parser->object_scope = NULL_TREE;
8409 if (parser->scope && name && BASELINK_P (name))
8410 adjust_result_of_qualified_name_lookup
8411 (name, parser->scope, scope);
8412 postfix_expression
8413 = finish_class_member_access_expr (postfix_expression, name,
8414 template_p,
8415 tf_warning_or_error);
8416 /* Build a location e.g.:
8417 ptr->access_expr
8418 ~~~^~~~~~~~~~~~~
8419 where the caret is at the deref token, ranging from
8420 the start of postfix_expression to the end of the access expr. */
8421 location_t combined_loc
8422 = make_location (input_location, start_loc, parser->lexer);
8423 protected_set_expr_location (postfix_expression, combined_loc);
8427 /* We no longer need to look up names in the scope of the object on
8428 the left-hand side of the `.' or `->' operator. */
8429 parser->context->object_type = NULL_TREE;
8431 /* Outside of offsetof, these operators may not appear in
8432 constant-expressions. */
8433 if (!for_offsetof
8434 && (cp_parser_non_integral_constant_expression
8435 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
8436 postfix_expression = error_mark_node;
8438 return postfix_expression;
8441 /* Parse a parenthesized expression-list.
8443 expression-list:
8444 assignment-expression
8445 expression-list, assignment-expression
8447 attribute-list:
8448 expression-list
8449 identifier
8450 identifier, expression-list
8452 CAST_P is true if this expression is the target of a cast.
8454 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8455 argument pack.
8457 WRAP_LOCATIONS_P is true if expressions within this list for which
8458 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8459 their source locations.
8461 Returns a vector of trees. Each element is a representation of an
8462 assignment-expression. NULL is returned if the ( and or ) are
8463 missing. An empty, but allocated, vector is returned on no
8464 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8465 if we are parsing an attribute list for an attribute that wants a
8466 plain identifier argument, normal_attr for an attribute that wants
8467 an expression, or non_attr if we aren't parsing an attribute list. If
8468 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8469 not all of the expressions in the list were constant.
8470 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8471 will be written to with the location of the closing parenthesis. If
8472 an error occurs, it may or may not be written to. */
8474 static vec<tree, va_gc> *
8475 cp_parser_parenthesized_expression_list (cp_parser* parser,
8476 int is_attribute_list,
8477 bool cast_p,
8478 bool allow_expansion_p,
8479 bool *non_constant_p,
8480 location_t *close_paren_loc,
8481 bool wrap_locations_p)
8483 vec<tree, va_gc> *expression_list;
8484 bool saved_greater_than_is_operator_p;
8486 /* Assume all the expressions will be constant. */
8487 if (non_constant_p)
8488 *non_constant_p = false;
8490 matching_parens parens;
8491 if (!parens.require_open (parser))
8492 return NULL;
8494 expression_list = make_tree_vector ();
8496 /* Within a parenthesized expression, a `>' token is always
8497 the greater-than operator. */
8498 saved_greater_than_is_operator_p
8499 = parser->greater_than_is_operator_p;
8500 parser->greater_than_is_operator_p = true;
8502 cp_expr expr (NULL_TREE);
8504 /* Consume expressions until there are no more. */
8505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8506 while (true)
8508 /* At the beginning of attribute lists, check to see if the
8509 next token is an identifier. */
8510 if (is_attribute_list == id_attr
8511 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
8512 expr = cp_lexer_consume_token (parser->lexer)->u.value;
8513 else if (is_attribute_list == assume_attr)
8514 expr = cp_parser_conditional_expression (parser);
8515 else
8516 expr
8517 = cp_parser_parenthesized_expression_list_elt (parser, cast_p,
8518 allow_expansion_p,
8519 non_constant_p);
8521 if (wrap_locations_p)
8522 expr.maybe_add_location_wrapper ();
8524 /* Add it to the list. We add error_mark_node
8525 expressions to the list, so that we can still tell if
8526 the correct form for a parenthesized expression-list
8527 is found. That gives better errors. */
8528 vec_safe_push (expression_list, expr.get_value ());
8530 if (expr == error_mark_node)
8531 goto skip_comma;
8533 /* After the first item, attribute lists look the same as
8534 expression lists. */
8535 is_attribute_list = non_attr;
8537 get_comma:;
8538 /* If the next token isn't a `,', then we are done. */
8539 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8540 break;
8542 /* Otherwise, consume the `,' and keep going. */
8543 cp_lexer_consume_token (parser->lexer);
8546 if (close_paren_loc)
8547 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
8549 if (!parens.require_close (parser))
8551 int ending;
8553 skip_comma:;
8554 /* We try and resync to an unnested comma, as that will give the
8555 user better diagnostics. */
8556 ending = cp_parser_skip_to_closing_parenthesis (parser,
8557 /*recovering=*/true,
8558 /*or_comma=*/true,
8559 /*consume_paren=*/true);
8560 if (ending < 0)
8561 goto get_comma;
8562 if (!ending)
8564 parser->greater_than_is_operator_p
8565 = saved_greater_than_is_operator_p;
8566 return NULL;
8570 parser->greater_than_is_operator_p
8571 = saved_greater_than_is_operator_p;
8573 return expression_list;
8576 /* Parse a pseudo-destructor-name.
8578 pseudo-destructor-name:
8579 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8580 :: [opt] nested-name-specifier template template-id :: ~ type-name
8581 :: [opt] nested-name-specifier [opt] ~ type-name
8583 If either of the first two productions is used, sets *SCOPE to the
8584 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8585 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8586 or ERROR_MARK_NODE if the parse fails. */
8588 static void
8589 cp_parser_pseudo_destructor_name (cp_parser* parser,
8590 tree object,
8591 tree* scope,
8592 tree* type)
8594 bool nested_name_specifier_p;
8596 /* Handle ~auto. */
8597 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
8598 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
8599 && !type_dependent_expression_p (object))
8601 if (cxx_dialect < cxx14)
8602 pedwarn (input_location, OPT_Wc__14_extensions,
8603 "%<~auto%> only available with "
8604 "%<-std=c++14%> or %<-std=gnu++14%>");
8605 cp_lexer_consume_token (parser->lexer);
8606 cp_lexer_consume_token (parser->lexer);
8607 *scope = NULL_TREE;
8608 *type = TREE_TYPE (object);
8609 return;
8612 /* Assume that things will not work out. */
8613 *type = error_mark_node;
8615 /* Look for the optional `::' operator. */
8616 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
8617 /* Look for the optional nested-name-specifier. */
8618 nested_name_specifier_p
8619 = (cp_parser_nested_name_specifier_opt (parser,
8620 /*typename_keyword_p=*/false,
8621 /*check_dependency_p=*/true,
8622 /*type_p=*/false,
8623 /*is_declaration=*/false)
8624 != NULL_TREE);
8625 /* Now, if we saw a nested-name-specifier, we might be doing the
8626 second production. */
8627 if (nested_name_specifier_p
8628 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8630 /* Consume the `template' keyword. */
8631 cp_lexer_consume_token (parser->lexer);
8632 /* Parse the template-id. */
8633 cp_parser_template_id (parser,
8634 /*template_keyword_p=*/true,
8635 /*check_dependency_p=*/false,
8636 class_type,
8637 /*is_declaration=*/true);
8638 /* Look for the `::' token. */
8639 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8641 /* If the next token is not a `~', then there might be some
8642 additional qualification. */
8643 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
8645 /* At this point, we're looking for "type-name :: ~". The type-name
8646 must not be a class-name, since this is a pseudo-destructor. So,
8647 it must be either an enum-name, or a typedef-name -- both of which
8648 are just identifiers. So, we peek ahead to check that the "::"
8649 and "~" tokens are present; if they are not, then we can avoid
8650 calling type_name. */
8651 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
8652 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
8653 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
8655 cp_parser_error (parser, "non-scalar type");
8656 return;
8659 /* Look for the type-name. */
8660 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
8661 if (*scope == error_mark_node)
8662 return;
8664 /* Look for the `::' token. */
8665 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
8667 else
8668 *scope = NULL_TREE;
8670 /* Look for the `~'. */
8671 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
8673 /* Once we see the ~, this has to be a pseudo-destructor. */
8674 if (!processing_template_decl && !cp_parser_error_occurred (parser))
8675 cp_parser_commit_to_topmost_tentative_parse (parser);
8677 /* Look for the type-name again. We are not responsible for
8678 checking that it matches the first type-name. */
8679 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
8682 /* Parse a unary-expression.
8684 unary-expression:
8685 postfix-expression
8686 ++ cast-expression
8687 -- cast-expression
8688 await-expression
8689 unary-operator cast-expression
8690 sizeof unary-expression
8691 sizeof ( type-id )
8692 alignof ( type-id ) [C++0x]
8693 new-expression
8694 delete-expression
8696 GNU Extensions:
8698 unary-expression:
8699 __extension__ cast-expression
8700 __alignof__ unary-expression
8701 __alignof__ ( type-id )
8702 alignof unary-expression [C++0x]
8703 __real__ cast-expression
8704 __imag__ cast-expression
8705 && identifier
8706 sizeof ( type-id ) { initializer-list , [opt] }
8707 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8708 __alignof__ ( type-id ) { initializer-list , [opt] }
8710 ADDRESS_P is true iff the unary-expression is appearing as the
8711 operand of the `&' operator. CAST_P is true if this expression is
8712 the target of a cast.
8714 Returns a representation of the expression. */
8716 static cp_expr
8717 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
8718 bool address_p, bool cast_p, bool decltype_p)
8720 cp_token *token;
8721 enum tree_code unary_operator;
8723 /* Peek at the next token. */
8724 token = cp_lexer_peek_token (parser->lexer);
8725 /* Some keywords give away the kind of expression. */
8726 if (token->type == CPP_KEYWORD)
8728 enum rid keyword = token->keyword;
8730 switch (keyword)
8732 case RID_ALIGNOF:
8733 case RID_SIZEOF:
8735 tree operand, ret;
8736 enum tree_code op;
8737 location_t start_loc = token->location;
8739 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
8740 bool std_alignof = id_equal (token->u.value, "alignof");
8742 /* Consume the token. */
8743 cp_lexer_consume_token (parser->lexer);
8744 /* Parse the operand. */
8745 operand = cp_parser_sizeof_operand (parser, keyword);
8747 /* Construct a location e.g. :
8748 alignof (expr)
8749 ^~~~~~~~~~~~~~
8750 with start == caret at the start of the "alignof"/"sizeof"
8751 token, with the endpoint at the final closing paren. */
8752 location_t compound_loc
8753 = make_location (start_loc, start_loc, parser->lexer);
8755 if (TYPE_P (operand))
8756 ret = cxx_sizeof_or_alignof_type (compound_loc, operand, op,
8757 std_alignof, true);
8758 else
8760 /* ISO C++ defines alignof only with types, not with
8761 expressions. So pedwarn if alignof is used with a non-
8762 type expression. However, __alignof__ is ok. */
8763 if (std_alignof)
8764 pedwarn (token->location, OPT_Wpedantic,
8765 "ISO C++ does not allow %<alignof%> "
8766 "with a non-type");
8768 ret = cxx_sizeof_or_alignof_expr (compound_loc, operand, op,
8769 std_alignof, true);
8771 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8772 SIZEOF_EXPR with the original operand. */
8773 if (op == SIZEOF_EXPR && ret != error_mark_node)
8775 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
8777 if (!processing_template_decl && TYPE_P (operand))
8779 ret = build_min (SIZEOF_EXPR, size_type_node,
8780 build1 (NOP_EXPR, operand,
8781 error_mark_node));
8782 SIZEOF_EXPR_TYPE_P (ret) = 1;
8784 else
8785 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
8786 TREE_SIDE_EFFECTS (ret) = 0;
8787 TREE_READONLY (ret) = 1;
8788 SET_EXPR_LOCATION (ret, compound_loc);
8792 cp_expr ret_expr (ret, compound_loc);
8793 ret_expr = ret_expr.maybe_add_location_wrapper ();
8794 return ret_expr;
8797 case RID_BUILTIN_HAS_ATTRIBUTE:
8798 return cp_parser_has_attribute_expression (parser);
8800 case RID_NEW:
8801 return cp_parser_new_expression (parser);
8803 case RID_DELETE:
8804 return cp_parser_delete_expression (parser);
8806 case RID_EXTENSION:
8808 /* The saved value of the PEDANTIC flag. */
8809 int saved_pedantic;
8810 tree expr;
8812 /* Save away the PEDANTIC flag. */
8813 cp_parser_extension_opt (parser, &saved_pedantic);
8814 /* Parse the cast-expression. */
8815 expr = cp_parser_simple_cast_expression (parser);
8816 /* Restore the PEDANTIC flag. */
8817 pedantic = saved_pedantic;
8819 return expr;
8822 case RID_REALPART:
8823 case RID_IMAGPART:
8825 tree expression;
8827 /* Consume the `__real__' or `__imag__' token. */
8828 cp_lexer_consume_token (parser->lexer);
8829 /* Parse the cast-expression. */
8830 expression = cp_parser_simple_cast_expression (parser);
8831 /* Create the complete representation. */
8832 return build_x_unary_op (token->location,
8833 (keyword == RID_REALPART
8834 ? REALPART_EXPR : IMAGPART_EXPR),
8835 expression, NULL_TREE,
8836 tf_warning_or_error);
8838 break;
8840 case RID_TRANSACTION_ATOMIC:
8841 case RID_TRANSACTION_RELAXED:
8842 return cp_parser_transaction_expression (parser, keyword);
8844 case RID_NOEXCEPT:
8846 tree expr;
8847 const char *saved_message;
8848 bool saved_integral_constant_expression_p;
8849 bool saved_non_integral_constant_expression_p;
8850 bool saved_greater_than_is_operator_p;
8852 location_t start_loc = token->location;
8854 cp_lexer_consume_token (parser->lexer);
8855 matching_parens parens;
8856 parens.require_open (parser);
8858 saved_message = parser->type_definition_forbidden_message;
8859 parser->type_definition_forbidden_message
8860 = G_("types may not be defined in %<noexcept%> expressions");
8862 saved_integral_constant_expression_p
8863 = parser->integral_constant_expression_p;
8864 saved_non_integral_constant_expression_p
8865 = parser->non_integral_constant_expression_p;
8866 parser->integral_constant_expression_p = false;
8868 saved_greater_than_is_operator_p
8869 = parser->greater_than_is_operator_p;
8870 parser->greater_than_is_operator_p = true;
8872 ++cp_unevaluated_operand;
8873 ++c_inhibit_evaluation_warnings;
8874 ++cp_noexcept_operand;
8875 expr = cp_parser_expression (parser);
8876 --cp_noexcept_operand;
8877 --c_inhibit_evaluation_warnings;
8878 --cp_unevaluated_operand;
8880 parser->greater_than_is_operator_p
8881 = saved_greater_than_is_operator_p;
8883 parser->integral_constant_expression_p
8884 = saved_integral_constant_expression_p;
8885 parser->non_integral_constant_expression_p
8886 = saved_non_integral_constant_expression_p;
8888 parser->type_definition_forbidden_message = saved_message;
8890 parens.require_close (parser);
8892 /* Construct a location of the form:
8893 noexcept (expr)
8894 ^~~~~~~~~~~~~~~
8895 with start == caret, finishing at the close-paren. */
8896 location_t noexcept_loc
8897 = make_location (start_loc, start_loc, parser->lexer);
8899 return cp_expr (finish_noexcept_expr (expr, tf_warning_or_error),
8900 noexcept_loc);
8903 case RID_CO_AWAIT:
8905 tree expr;
8906 location_t kw_loc = token->location;
8908 /* Consume the `co_await' token. */
8909 cp_lexer_consume_token (parser->lexer);
8910 /* Parse its cast-expression. */
8911 expr = cp_parser_simple_cast_expression (parser);
8912 if (expr == error_mark_node)
8913 return error_mark_node;
8915 /* Handle [expr.await]. */
8916 return cp_expr (finish_co_await_expr (kw_loc, expr));
8919 default:
8920 break;
8924 /* Look for the `:: new' and `:: delete', which also signal the
8925 beginning of a new-expression, or delete-expression,
8926 respectively. If the next token is `::', then it might be one of
8927 these. */
8928 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8930 enum rid keyword;
8932 /* See if the token after the `::' is one of the keywords in
8933 which we're interested. */
8934 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8935 /* If it's `new', we have a new-expression. */
8936 if (keyword == RID_NEW)
8937 return cp_parser_new_expression (parser);
8938 /* Similarly, for `delete'. */
8939 else if (keyword == RID_DELETE)
8940 return cp_parser_delete_expression (parser);
8943 /* Look for a unary operator. */
8944 unary_operator = cp_parser_unary_operator (token);
8945 /* The `++' and `--' operators can be handled similarly, even though
8946 they are not technically unary-operators in the grammar. */
8947 if (unary_operator == ERROR_MARK)
8949 if (token->type == CPP_PLUS_PLUS)
8950 unary_operator = PREINCREMENT_EXPR;
8951 else if (token->type == CPP_MINUS_MINUS)
8952 unary_operator = PREDECREMENT_EXPR;
8953 /* Handle the GNU address-of-label extension. */
8954 else if (cp_parser_allow_gnu_extensions_p (parser)
8955 && token->type == CPP_AND_AND)
8957 tree identifier;
8958 tree expression;
8959 location_t start_loc = token->location;
8961 /* Consume the '&&' token. */
8962 cp_lexer_consume_token (parser->lexer);
8963 /* Look for the identifier. */
8964 identifier = cp_parser_identifier (parser);
8965 /* Construct a location of the form:
8966 &&label
8967 ^~~~~~~
8968 with caret==start at the "&&", finish at the end of the label. */
8969 location_t combined_loc
8970 = make_location (start_loc, start_loc, parser->lexer);
8971 /* Create an expression representing the address. */
8972 expression = finish_label_address_expr (identifier, combined_loc);
8973 if (cp_parser_non_integral_constant_expression (parser,
8974 NIC_ADDR_LABEL))
8975 expression = error_mark_node;
8976 return expression;
8979 if (unary_operator != ERROR_MARK)
8981 cp_expr cast_expression;
8982 cp_expr expression = error_mark_node;
8983 non_integral_constant non_constant_p = NIC_NONE;
8984 location_t loc = token->location;
8985 tsubst_flags_t complain = complain_flags (decltype_p);
8987 /* Consume the operator token. */
8988 token = cp_lexer_consume_token (parser->lexer);
8989 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8991 /* Parse the cast-expression. */
8992 cast_expression
8993 = cp_parser_cast_expression (parser,
8994 unary_operator == ADDR_EXPR,
8995 /*cast_p=*/false,
8996 /*decltype*/false,
8997 pidk);
8999 /* Make a location:
9000 OP_TOKEN CAST_EXPRESSION
9001 ^~~~~~~~~~~~~~~~~~~~~~~~~
9002 with start==caret at the operator token, and
9003 extending to the end of the cast_expression. */
9004 loc = make_location (loc, loc, cast_expression.get_finish ());
9006 /* Now, build an appropriate representation. */
9007 switch (unary_operator)
9009 case INDIRECT_REF:
9010 non_constant_p = NIC_STAR;
9011 expression = build_x_indirect_ref (loc, cast_expression,
9012 RO_UNARY_STAR, NULL_TREE,
9013 complain);
9014 /* TODO: build_x_indirect_ref does not always honor the
9015 location, so ensure it is set. */
9016 expression.set_location (loc);
9017 break;
9019 case ADDR_EXPR:
9020 non_constant_p = NIC_ADDR;
9021 /* Fall through. */
9022 case BIT_NOT_EXPR:
9023 expression = build_x_unary_op (loc, unary_operator,
9024 cast_expression,
9025 NULL_TREE, complain);
9026 /* TODO: build_x_unary_op does not always honor the location,
9027 so ensure it is set. */
9028 expression.set_location (loc);
9029 break;
9031 case PREINCREMENT_EXPR:
9032 case PREDECREMENT_EXPR:
9033 non_constant_p = unary_operator == PREINCREMENT_EXPR
9034 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
9035 /* Fall through. */
9036 case NEGATE_EXPR:
9037 /* Immediately fold negation of a constant, unless the constant is 0
9038 (since -0 == 0) or it would overflow. */
9039 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER)
9041 tree stripped_expr
9042 = tree_strip_any_location_wrapper (cast_expression);
9043 if (CONSTANT_CLASS_P (stripped_expr)
9044 && !integer_zerop (stripped_expr)
9045 && !TREE_OVERFLOW (stripped_expr))
9047 tree folded = fold_build1 (unary_operator,
9048 TREE_TYPE (stripped_expr),
9049 stripped_expr);
9050 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
9052 expression = maybe_wrap_with_location (folded, loc);
9053 break;
9057 /* Fall through. */
9058 case UNARY_PLUS_EXPR:
9059 case TRUTH_NOT_EXPR:
9060 expression = finish_unary_op_expr (loc, unary_operator,
9061 cast_expression, complain);
9062 break;
9064 default:
9065 gcc_unreachable ();
9068 if (non_constant_p != NIC_NONE
9069 && cp_parser_non_integral_constant_expression (parser,
9070 non_constant_p))
9071 expression = error_mark_node;
9073 return expression;
9076 return cp_parser_postfix_expression (parser, address_p, cast_p,
9077 /*member_access_only_p=*/false,
9078 decltype_p,
9079 pidk);
9082 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
9083 unary-operator, the corresponding tree code is returned. */
9085 static enum tree_code
9086 cp_parser_unary_operator (cp_token* token)
9088 switch (token->type)
9090 case CPP_MULT:
9091 return INDIRECT_REF;
9093 case CPP_AND:
9094 return ADDR_EXPR;
9096 case CPP_PLUS:
9097 return UNARY_PLUS_EXPR;
9099 case CPP_MINUS:
9100 return NEGATE_EXPR;
9102 case CPP_NOT:
9103 return TRUTH_NOT_EXPR;
9105 case CPP_COMPL:
9106 return BIT_NOT_EXPR;
9108 default:
9109 return ERROR_MARK;
9113 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
9114 Returns a representation of the expression. */
9116 static tree
9117 cp_parser_has_attribute_expression (cp_parser *parser)
9119 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9121 /* Consume the __builtin_has_attribute token. */
9122 cp_lexer_consume_token (parser->lexer);
9124 matching_parens parens;
9125 if (!parens.require_open (parser))
9126 return error_mark_node;
9128 /* Types cannot be defined in a `sizeof' expression. Save away the
9129 old message. */
9130 const char *saved_message = parser->type_definition_forbidden_message;
9131 const char *saved_message_arg
9132 = parser->type_definition_forbidden_message_arg;
9133 parser->type_definition_forbidden_message
9134 = G_("types may not be defined in %qs expressions");
9135 parser->type_definition_forbidden_message_arg
9136 = IDENTIFIER_POINTER (ridpointers[RID_BUILTIN_HAS_ATTRIBUTE]);
9138 /* The restrictions on constant-expressions do not apply inside
9139 sizeof expressions. */
9140 bool saved_integral_constant_expression_p
9141 = parser->integral_constant_expression_p;
9142 bool saved_non_integral_constant_expression_p
9143 = parser->non_integral_constant_expression_p;
9144 parser->integral_constant_expression_p = false;
9146 /* Do not actually evaluate the expression. */
9147 ++cp_unevaluated_operand;
9148 ++c_inhibit_evaluation_warnings;
9150 tree oper = NULL_TREE;
9152 /* We can't be sure yet whether we're looking at a type-id or an
9153 expression. */
9154 cp_parser_parse_tentatively (parser);
9156 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9157 parser->in_type_id_in_expr_p = true;
9158 /* Look for the type-id. */
9159 oper = cp_parser_type_id (parser);
9160 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9162 cp_parser_parse_definitely (parser);
9164 /* If the type-id production did not work out, then we must be
9165 looking at an expression. */
9166 if (!oper || oper == error_mark_node)
9167 oper = cp_parser_assignment_expression (parser);
9169 STRIP_ANY_LOCATION_WRAPPER (oper);
9171 /* Go back to evaluating expressions. */
9172 --cp_unevaluated_operand;
9173 --c_inhibit_evaluation_warnings;
9175 /* And restore the old one. */
9176 parser->type_definition_forbidden_message = saved_message;
9177 parser->type_definition_forbidden_message_arg = saved_message_arg;
9178 parser->integral_constant_expression_p
9179 = saved_integral_constant_expression_p;
9180 parser->non_integral_constant_expression_p
9181 = saved_non_integral_constant_expression_p;
9183 /* Consume the comma if it's there. */
9184 if (!cp_parser_require (parser, CPP_COMMA, RT_COMMA))
9186 cp_parser_skip_to_closing_parenthesis (parser, false, false,
9187 /*consume_paren=*/true);
9188 return error_mark_node;
9191 /* Parse the attribute specification. */
9192 bool ret = false;
9193 location_t atloc = cp_lexer_peek_token (parser->lexer)->location;
9194 if (tree attr = cp_parser_gnu_attribute_list (parser, /*exactly_one=*/true))
9196 if (oper == error_mark_node)
9197 /* Nothing. */;
9198 else if (processing_template_decl && uses_template_parms (oper))
9199 sorry_at (atloc, "%<__builtin_has_attribute%> with dependent argument "
9200 "not supported yet");
9201 else
9203 /* Fold constant expressions used in attributes first. */
9204 cp_check_const_attributes (attr);
9206 /* Finally, see if OPER has been declared with ATTR. */
9207 ret = has_attribute (atloc, oper, attr, default_conversion);
9210 parens.require_close (parser);
9212 else
9214 error_at (atloc, "expected identifier");
9215 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9218 /* Construct a location e.g. :
9219 __builtin_has_attribute (oper, attr)
9220 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9221 with start == caret at the start of the built-in token,
9222 and with the endpoint at the final closing paren. */
9223 location_t compound_loc
9224 = make_location (start_loc, start_loc, parser->lexer);
9226 cp_expr ret_expr (ret ? boolean_true_node : boolean_false_node);
9227 ret_expr.set_location (compound_loc);
9228 ret_expr = ret_expr.maybe_add_location_wrapper ();
9229 return ret_expr;
9232 /* Parse a new-expression.
9234 new-expression:
9235 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
9236 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
9238 Returns a representation of the expression. */
9240 static tree
9241 cp_parser_new_expression (cp_parser* parser)
9243 bool global_scope_p;
9244 vec<tree, va_gc> *placement;
9245 tree type;
9246 vec<tree, va_gc> *initializer;
9247 tree nelts = NULL_TREE;
9248 tree ret;
9250 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9252 /* Look for the optional `::' operator. */
9253 global_scope_p
9254 = (cp_parser_global_scope_opt (parser,
9255 /*current_scope_valid_p=*/false)
9256 != NULL_TREE);
9257 /* Look for the `new' operator. */
9258 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
9259 /* There's no easy way to tell a new-placement from the
9260 `( type-id )' construct. */
9261 cp_parser_parse_tentatively (parser);
9262 /* Look for a new-placement. */
9263 placement = cp_parser_new_placement (parser);
9264 /* If that didn't work out, there's no new-placement. */
9265 if (!cp_parser_parse_definitely (parser))
9267 if (placement != NULL)
9268 release_tree_vector (placement);
9269 placement = NULL;
9272 /* If the next token is a `(', then we have a parenthesized
9273 type-id. */
9274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9276 cp_token *token;
9277 const char *saved_message = parser->type_definition_forbidden_message;
9279 /* Consume the `('. */
9280 matching_parens parens;
9281 parens.consume_open (parser);
9283 /* Parse the type-id. */
9284 parser->type_definition_forbidden_message
9285 = G_("types may not be defined in a new-expression");
9287 type_id_in_expr_sentinel s (parser);
9288 type = cp_parser_type_id (parser);
9290 parser->type_definition_forbidden_message = saved_message;
9292 /* Look for the closing `)'. */
9293 parens.require_close (parser);
9294 token = cp_lexer_peek_token (parser->lexer);
9295 /* There should not be a direct-new-declarator in this production,
9296 but GCC used to allowed this, so we check and emit a sensible error
9297 message for this case. */
9298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9300 error_at (token->location,
9301 "array bound forbidden after parenthesized type-id");
9302 inform (token->location,
9303 "try removing the parentheses around the type-id");
9304 cp_parser_direct_new_declarator (parser);
9307 /* Otherwise, there must be a new-type-id. */
9308 else
9309 type = cp_parser_new_type_id (parser, &nelts);
9311 /* If the next token is a `(' or '{', then we have a new-initializer. */
9312 cp_token *token = cp_lexer_peek_token (parser->lexer);
9313 if (token->type == CPP_OPEN_PAREN
9314 || token->type == CPP_OPEN_BRACE)
9315 initializer = cp_parser_new_initializer (parser);
9316 else
9317 initializer = NULL;
9319 /* A new-expression may not appear in an integral constant
9320 expression. */
9321 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
9322 ret = error_mark_node;
9323 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9324 of a new-type-id or type-id of a new-expression, the new-expression shall
9325 contain a new-initializer of the form ( assignment-expression )".
9326 Additionally, consistently with the spirit of DR 1467, we want to accept
9327 'new auto { 2 }' too. */
9328 else if ((ret = type_uses_auto (type))
9329 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
9330 && (vec_safe_length (initializer) != 1
9331 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
9332 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
9334 error_at (token->location,
9335 "initialization of new-expression for type %<auto%> "
9336 "requires exactly one element");
9337 ret = error_mark_node;
9339 else
9341 /* Construct a location e.g.:
9342 ptr = new int[100]
9343 ^~~~~~~~~~~~
9344 with caret == start at the start of the "new" token, and the end
9345 at the end of the final token we consumed. */
9346 location_t combined_loc = make_location (start_loc, start_loc,
9347 parser->lexer);
9348 /* Create a representation of the new-expression. */
9349 ret = build_new (combined_loc, &placement, type, nelts, &initializer,
9350 global_scope_p, tf_warning_or_error);
9353 if (placement != NULL)
9354 release_tree_vector (placement);
9355 if (initializer != NULL)
9356 release_tree_vector (initializer);
9358 return ret;
9361 /* Parse a new-placement.
9363 new-placement:
9364 ( expression-list )
9366 Returns the same representation as for an expression-list. */
9368 static vec<tree, va_gc> *
9369 cp_parser_new_placement (cp_parser* parser)
9371 vec<tree, va_gc> *expression_list;
9373 /* Parse the expression-list. */
9374 expression_list = (cp_parser_parenthesized_expression_list
9375 (parser, non_attr, /*cast_p=*/false,
9376 /*allow_expansion_p=*/true,
9377 /*non_constant_p=*/NULL));
9379 if (expression_list && expression_list->is_empty ())
9380 error ("expected expression-list or type-id");
9382 return expression_list;
9385 /* Parse a new-type-id.
9387 new-type-id:
9388 type-specifier-seq new-declarator [opt]
9390 Returns the TYPE allocated. If the new-type-id indicates an array
9391 type, *NELTS is set to the number of elements in the last array
9392 bound; the TYPE will not include the last array bound. */
9394 static tree
9395 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
9397 cp_decl_specifier_seq type_specifier_seq;
9398 cp_declarator *new_declarator;
9399 cp_declarator *declarator;
9400 cp_declarator *outer_declarator;
9401 const char *saved_message;
9403 /* The type-specifier sequence must not contain type definitions.
9404 (It cannot contain declarations of new types either, but if they
9405 are not definitions we will catch that because they are not
9406 complete.) */
9407 saved_message = parser->type_definition_forbidden_message;
9408 parser->type_definition_forbidden_message
9409 = G_("types may not be defined in a new-type-id");
9410 /* Parse the type-specifier-seq. */
9411 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
9412 /*is_declaration=*/false,
9413 /*is_trailing_return=*/false,
9414 &type_specifier_seq);
9415 /* Restore the old message. */
9416 parser->type_definition_forbidden_message = saved_message;
9418 if (type_specifier_seq.type == error_mark_node)
9419 return error_mark_node;
9421 /* Parse the new-declarator. */
9422 new_declarator = cp_parser_new_declarator_opt (parser);
9424 /* Determine the number of elements in the last array dimension, if
9425 any. */
9426 *nelts = NULL_TREE;
9427 /* Skip down to the last array dimension. */
9428 declarator = new_declarator;
9429 outer_declarator = NULL;
9430 while (declarator && (declarator->kind == cdk_pointer
9431 || declarator->kind == cdk_ptrmem))
9433 outer_declarator = declarator;
9434 declarator = declarator->declarator;
9436 while (declarator
9437 && declarator->kind == cdk_array
9438 && declarator->declarator
9439 && declarator->declarator->kind == cdk_array)
9441 outer_declarator = declarator;
9442 declarator = declarator->declarator;
9445 if (declarator && declarator->kind == cdk_array)
9447 *nelts = declarator->u.array.bounds;
9448 if (*nelts == error_mark_node)
9449 *nelts = integer_one_node;
9451 if (*nelts == NULL_TREE)
9452 /* Leave [] in the declarator. */;
9453 else if (outer_declarator)
9454 outer_declarator->declarator = declarator->declarator;
9455 else
9456 new_declarator = NULL;
9459 return groktypename (&type_specifier_seq, new_declarator, false);
9462 /* Parse an (optional) new-declarator.
9464 new-declarator:
9465 ptr-operator new-declarator [opt]
9466 direct-new-declarator
9468 Returns the declarator. */
9470 static cp_declarator *
9471 cp_parser_new_declarator_opt (cp_parser* parser)
9473 enum tree_code code;
9474 tree type, std_attributes = NULL_TREE;
9475 cp_cv_quals cv_quals;
9477 /* We don't know if there's a ptr-operator next, or not. */
9478 cp_parser_parse_tentatively (parser);
9479 /* Look for a ptr-operator. */
9480 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
9481 /* If that worked, look for more new-declarators. */
9482 if (cp_parser_parse_definitely (parser))
9484 cp_declarator *declarator;
9486 /* Parse another optional declarator. */
9487 declarator = cp_parser_new_declarator_opt (parser);
9489 declarator = cp_parser_make_indirect_declarator
9490 (code, type, cv_quals, declarator, std_attributes);
9492 return declarator;
9495 /* If the next token is a `[', there is a direct-new-declarator. */
9496 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9497 return cp_parser_direct_new_declarator (parser);
9499 return NULL;
9502 /* Parse a direct-new-declarator.
9504 direct-new-declarator:
9505 [ expression ]
9506 direct-new-declarator [constant-expression]
9510 static cp_declarator *
9511 cp_parser_direct_new_declarator (cp_parser* parser)
9513 cp_declarator *declarator = NULL;
9514 bool first_p = true;
9516 while (true)
9518 tree expression;
9519 cp_token *token;
9521 /* Look for the opening `['. */
9522 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9524 token = cp_lexer_peek_token (parser->lexer);
9525 if (token->type == CPP_CLOSE_SQUARE && first_p)
9526 expression = NULL_TREE;
9527 else
9528 expression = cp_parser_expression (parser);
9529 /* The standard requires that the expression have integral
9530 type. DR 74 adds enumeration types. We believe that the
9531 real intent is that these expressions be handled like the
9532 expression in a `switch' condition, which also allows
9533 classes with a single conversion to integral or
9534 enumeration type. */
9535 if (expression && !processing_template_decl)
9537 expression
9538 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
9539 expression,
9540 /*complain=*/true);
9541 if (!expression)
9543 error_at (token->location,
9544 "expression in new-declarator must have integral "
9545 "or enumeration type");
9546 expression = error_mark_node;
9550 /* Look for the closing `]'. */
9551 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9553 /* Add this bound to the declarator. */
9554 declarator = make_array_declarator (declarator, expression);
9556 /* If the next token is not a `[', then there are no more
9557 bounds. */
9558 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
9559 break;
9560 first_p = false;
9563 return declarator;
9566 /* Parse a new-initializer.
9568 new-initializer:
9569 ( expression-list [opt] )
9570 braced-init-list
9572 Returns a representation of the expression-list. */
9574 static vec<tree, va_gc> *
9575 cp_parser_new_initializer (cp_parser* parser)
9577 vec<tree, va_gc> *expression_list;
9579 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9581 tree t;
9582 bool expr_non_constant_p;
9583 cp_lexer_set_source_position (parser->lexer);
9584 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9585 t = cp_parser_braced_list (parser, &expr_non_constant_p);
9586 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
9587 expression_list = make_tree_vector_single (t);
9589 else
9590 expression_list = (cp_parser_parenthesized_expression_list
9591 (parser, non_attr, /*cast_p=*/false,
9592 /*allow_expansion_p=*/true,
9593 /*non_constant_p=*/NULL));
9595 return expression_list;
9598 /* Parse a delete-expression.
9600 delete-expression:
9601 :: [opt] delete cast-expression
9602 :: [opt] delete [ ] cast-expression
9604 Returns a representation of the expression. */
9606 static tree
9607 cp_parser_delete_expression (cp_parser* parser)
9609 bool global_scope_p;
9610 bool array_p;
9611 tree expression;
9612 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9614 /* Look for the optional `::' operator. */
9615 global_scope_p
9616 = (cp_parser_global_scope_opt (parser,
9617 /*current_scope_valid_p=*/false)
9618 != NULL_TREE);
9619 /* Look for the `delete' keyword. */
9620 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
9621 /* See if the array syntax is in use. */
9622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
9624 /* Consume the `[' token. */
9625 cp_lexer_consume_token (parser->lexer);
9626 /* Look for the `]' token. */
9627 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
9628 /* Remember that this is the `[]' construct. */
9629 array_p = true;
9631 else
9632 array_p = false;
9634 /* Parse the cast-expression. */
9635 expression = cp_parser_simple_cast_expression (parser);
9637 /* A delete-expression may not appear in an integral constant
9638 expression. */
9639 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
9640 return error_mark_node;
9642 /* Construct a location e.g.:
9643 delete [ ] ptr
9644 ^~~~~~~~~~~~~~
9645 with caret == start at the start of the "delete" token, and
9646 the end at the end of the final token we consumed. */
9647 location_t combined_loc = make_location (start_loc, start_loc,
9648 parser->lexer);
9649 expression = delete_sanity (combined_loc, expression, NULL_TREE, array_p,
9650 global_scope_p, tf_warning_or_error);
9652 return expression;
9655 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9656 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9657 0 otherwise. */
9659 static int
9660 cp_parser_tokens_start_cast_expression (cp_parser *parser)
9662 cp_token *token = cp_lexer_peek_token (parser->lexer);
9663 switch (token->type)
9665 case CPP_COMMA:
9666 case CPP_SEMICOLON:
9667 case CPP_QUERY:
9668 case CPP_COLON:
9669 case CPP_CLOSE_SQUARE:
9670 case CPP_CLOSE_PAREN:
9671 case CPP_CLOSE_BRACE:
9672 case CPP_OPEN_BRACE:
9673 case CPP_DOT:
9674 case CPP_DOT_STAR:
9675 case CPP_DEREF:
9676 case CPP_DEREF_STAR:
9677 case CPP_DIV:
9678 case CPP_MOD:
9679 case CPP_LSHIFT:
9680 case CPP_RSHIFT:
9681 case CPP_LESS:
9682 case CPP_GREATER:
9683 case CPP_LESS_EQ:
9684 case CPP_GREATER_EQ:
9685 case CPP_EQ_EQ:
9686 case CPP_NOT_EQ:
9687 case CPP_EQ:
9688 case CPP_MULT_EQ:
9689 case CPP_DIV_EQ:
9690 case CPP_MOD_EQ:
9691 case CPP_PLUS_EQ:
9692 case CPP_MINUS_EQ:
9693 case CPP_RSHIFT_EQ:
9694 case CPP_LSHIFT_EQ:
9695 case CPP_AND_EQ:
9696 case CPP_XOR_EQ:
9697 case CPP_OR_EQ:
9698 case CPP_XOR:
9699 case CPP_OR:
9700 case CPP_OR_OR:
9701 case CPP_EOF:
9702 case CPP_ELLIPSIS:
9703 return 0;
9705 case CPP_OPEN_PAREN:
9706 /* In ((type ()) () the last () isn't a valid cast-expression,
9707 so the whole must be parsed as postfix-expression. */
9708 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
9709 != CPP_CLOSE_PAREN;
9711 case CPP_OPEN_SQUARE:
9712 /* '[' may start a primary-expression in obj-c++ and in C++11,
9713 as a lambda-expression, eg, '(void)[]{}'. */
9714 if (cxx_dialect >= cxx11)
9715 return -1;
9716 return c_dialect_objc ();
9718 case CPP_PLUS_PLUS:
9719 case CPP_MINUS_MINUS:
9720 /* '++' and '--' may or may not start a cast-expression:
9722 struct T { void operator++(int); };
9723 void f() { (T())++; }
9727 int a;
9728 (int)++a; */
9729 return -1;
9731 default:
9732 return 1;
9736 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9737 in the order: const_cast, static_cast, reinterpret_cast.
9739 Don't suggest dynamic_cast.
9741 Return the first legal cast kind found, or NULL otherwise. */
9743 static const char *
9744 get_cast_suggestion (tree dst_type, tree orig_expr)
9746 tree trial;
9748 /* Reuse the parser logic by attempting to build the various kinds of
9749 cast, with "complain" disabled.
9750 Identify the first such cast that is valid. */
9752 /* Don't attempt to run such logic within template processing. */
9753 if (processing_template_decl)
9754 return NULL;
9756 /* First try const_cast. */
9757 trial = build_const_cast (input_location, dst_type, orig_expr, tf_none);
9758 if (trial != error_mark_node)
9759 return "const_cast";
9761 /* If that fails, try static_cast. */
9762 trial = build_static_cast (input_location, dst_type, orig_expr, tf_none);
9763 if (trial != error_mark_node)
9764 return "static_cast";
9766 /* Finally, try reinterpret_cast. */
9767 trial = build_reinterpret_cast (input_location, dst_type, orig_expr,
9768 tf_none);
9769 if (trial != error_mark_node)
9770 return "reinterpret_cast";
9772 /* No such cast possible. */
9773 return NULL;
9776 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9777 suggesting how to convert a C-style cast of the form:
9779 (DST_TYPE)ORIG_EXPR
9781 to a C++-style cast.
9783 The primary range of RICHLOC is asssumed to be that of the original
9784 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9785 of the parens in the C-style cast. */
9787 static void
9788 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
9789 location_t close_paren_loc, tree orig_expr,
9790 tree dst_type)
9792 /* This function is non-trivial, so bail out now if the warning isn't
9793 going to be emitted. */
9794 if (!warn_old_style_cast)
9795 return;
9797 /* Try to find a legal C++ cast, trying them in order:
9798 const_cast, static_cast, reinterpret_cast. */
9799 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
9800 if (!cast_suggestion)
9801 return;
9803 /* Replace the open paren with "CAST_SUGGESTION<". */
9804 pretty_printer pp;
9805 pp_string (&pp, cast_suggestion);
9806 pp_less (&pp);
9807 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
9809 /* Replace the close paren with "> (". */
9810 rich_loc->add_fixit_replace (close_paren_loc, "> (");
9812 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9813 rich_loc->add_fixit_insert_after (")");
9817 /* Parse a cast-expression.
9819 cast-expression:
9820 unary-expression
9821 ( type-id ) cast-expression
9823 ADDRESS_P is true iff the unary-expression is appearing as the
9824 operand of the `&' operator. CAST_P is true if this expression is
9825 the target of a cast.
9827 Returns a representation of the expression. */
9829 static cp_expr
9830 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
9831 bool decltype_p, cp_id_kind * pidk)
9833 /* If it's a `(', then we might be looking at a cast. */
9834 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9836 tree type = NULL_TREE;
9837 cp_expr expr (NULL_TREE);
9838 int cast_expression = 0;
9839 const char *saved_message;
9841 /* There's no way to know yet whether or not this is a cast.
9842 For example, `(int (3))' is a unary-expression, while `(int)
9843 3' is a cast. So, we resort to parsing tentatively. */
9844 cp_parser_parse_tentatively (parser);
9845 /* Types may not be defined in a cast. */
9846 saved_message = parser->type_definition_forbidden_message;
9847 parser->type_definition_forbidden_message
9848 = G_("types may not be defined in casts");
9849 /* Consume the `('. */
9850 matching_parens parens;
9851 cp_token *open_paren = parens.consume_open (parser);
9852 location_t open_paren_loc = open_paren->location;
9853 location_t close_paren_loc = UNKNOWN_LOCATION;
9855 /* A very tricky bit is that `(struct S) { 3 }' is a
9856 compound-literal (which we permit in C++ as an extension).
9857 But, that construct is not a cast-expression -- it is a
9858 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9859 is legal; if the compound-literal were a cast-expression,
9860 you'd need an extra set of parentheses.) But, if we parse
9861 the type-id, and it happens to be a class-specifier, then we
9862 will commit to the parse at that point, because we cannot
9863 undo the action that is done when creating a new class. So,
9864 then we cannot back up and do a postfix-expression.
9866 Another tricky case is the following (c++/29234):
9868 struct S { void operator () (); };
9870 void foo ()
9872 ( S()() );
9875 As a type-id we parse the parenthesized S()() as a function
9876 returning a function, groktypename complains and we cannot
9877 back up in this case either.
9879 Therefore, we scan ahead to the closing `)', and check to see
9880 if the tokens after the `)' can start a cast-expression. Otherwise
9881 we are dealing with an unary-expression, a postfix-expression
9882 or something else.
9884 Yet another tricky case, in C++11, is the following (c++/54891):
9886 (void)[]{};
9888 The issue is that usually, besides the case of lambda-expressions,
9889 the parenthesized type-id cannot be followed by '[', and, eg, we
9890 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9891 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9892 we don't commit, we try a cast-expression, then an unary-expression.
9894 Save tokens so that we can put them back. */
9895 cp_lexer_save_tokens (parser->lexer);
9897 /* We may be looking at a cast-expression. */
9898 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
9899 /*consume_paren=*/true))
9900 cast_expression
9901 = cp_parser_tokens_start_cast_expression (parser);
9903 /* Roll back the tokens we skipped. */
9904 cp_lexer_rollback_tokens (parser->lexer);
9905 /* If we aren't looking at a cast-expression, simulate an error so
9906 that the call to cp_parser_error_occurred below returns true. */
9907 if (!cast_expression)
9908 cp_parser_simulate_error (parser);
9909 else
9911 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
9912 parser->in_type_id_in_expr_p = true;
9913 /* Look for the type-id. */
9914 type = cp_parser_type_id (parser);
9915 /* Look for the closing `)'. */
9916 cp_token *close_paren = parens.require_close (parser);
9917 if (close_paren)
9918 close_paren_loc = close_paren->location;
9919 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
9922 /* Restore the saved message. */
9923 parser->type_definition_forbidden_message = saved_message;
9925 /* At this point this can only be either a cast or a
9926 parenthesized ctor such as `(T ())' that looks like a cast to
9927 function returning T. */
9928 if (!cp_parser_error_occurred (parser))
9930 /* Only commit if the cast-expression doesn't start with
9931 '++', '--', or '[' in C++11. */
9932 if (cast_expression > 0)
9933 cp_parser_commit_to_topmost_tentative_parse (parser);
9935 expr = cp_parser_cast_expression (parser,
9936 /*address_p=*/false,
9937 /*cast_p=*/true,
9938 /*decltype_p=*/false,
9939 pidk);
9941 if (cp_parser_parse_definitely (parser))
9943 /* Warn about old-style casts, if so requested. */
9944 if (warn_old_style_cast
9945 && !in_system_header_at (input_location)
9946 && !VOID_TYPE_P (type)
9947 && current_lang_name != lang_name_c)
9949 gcc_rich_location rich_loc (input_location);
9950 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
9951 expr, type);
9952 warning_at (&rich_loc, OPT_Wold_style_cast,
9953 "use of old-style cast to %q#T", type);
9956 /* Only type conversions to integral or enumeration types
9957 can be used in constant-expressions. */
9958 if (!cast_valid_in_integral_constant_expression_p (type)
9959 && cp_parser_non_integral_constant_expression (parser,
9960 NIC_CAST))
9961 return error_mark_node;
9963 /* Perform the cast. */
9964 /* Make a location:
9965 (TYPE) EXPR
9966 ^~~~~~~~~~~
9967 with start==caret at the open paren, extending to the
9968 end of "expr". */
9969 location_t cast_loc = make_location (open_paren_loc,
9970 open_paren_loc,
9971 expr.get_finish ());
9972 expr = build_c_cast (cast_loc, type, expr);
9973 return expr;
9976 else
9977 cp_parser_abort_tentative_parse (parser);
9980 /* If we get here, then it's not a cast, so it must be a
9981 unary-expression. */
9982 return cp_parser_unary_expression (parser, pidk, address_p,
9983 cast_p, decltype_p);
9986 /* Parse a binary expression of the general form:
9988 pm-expression:
9989 cast-expression
9990 pm-expression .* cast-expression
9991 pm-expression ->* cast-expression
9993 multiplicative-expression:
9994 pm-expression
9995 multiplicative-expression * pm-expression
9996 multiplicative-expression / pm-expression
9997 multiplicative-expression % pm-expression
9999 additive-expression:
10000 multiplicative-expression
10001 additive-expression + multiplicative-expression
10002 additive-expression - multiplicative-expression
10004 shift-expression:
10005 additive-expression
10006 shift-expression << additive-expression
10007 shift-expression >> additive-expression
10009 relational-expression:
10010 shift-expression
10011 relational-expression < shift-expression
10012 relational-expression > shift-expression
10013 relational-expression <= shift-expression
10014 relational-expression >= shift-expression
10016 GNU Extension:
10018 relational-expression:
10019 relational-expression <? shift-expression
10020 relational-expression >? shift-expression
10022 equality-expression:
10023 relational-expression
10024 equality-expression == relational-expression
10025 equality-expression != relational-expression
10027 and-expression:
10028 equality-expression
10029 and-expression & equality-expression
10031 exclusive-or-expression:
10032 and-expression
10033 exclusive-or-expression ^ and-expression
10035 inclusive-or-expression:
10036 exclusive-or-expression
10037 inclusive-or-expression | exclusive-or-expression
10039 logical-and-expression:
10040 inclusive-or-expression
10041 logical-and-expression && inclusive-or-expression
10043 logical-or-expression:
10044 logical-and-expression
10045 logical-or-expression || logical-and-expression
10047 All these are implemented with a single function like:
10049 binary-expression:
10050 simple-cast-expression
10051 binary-expression <token> binary-expression
10053 CAST_P is true if this expression is the target of a cast.
10055 The binops_by_token map is used to get the tree codes for each <token> type.
10056 binary-expressions are associated according to a precedence table. */
10058 #define TOKEN_PRECEDENCE(token) \
10059 (((token->type == CPP_GREATER \
10060 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
10061 && !parser->greater_than_is_operator_p) \
10062 ? PREC_NOT_OPERATOR \
10063 : binops_by_token[token->type].prec)
10065 static cp_expr
10066 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10067 bool no_toplevel_fold_p,
10068 bool decltype_p,
10069 enum cp_parser_prec prec,
10070 cp_id_kind * pidk)
10072 cp_parser_expression_stack stack;
10073 cp_parser_expression_stack_entry *sp = &stack[0];
10074 cp_parser_expression_stack_entry *disable_warnings_sp = NULL;
10075 cp_parser_expression_stack_entry current;
10076 cp_expr rhs;
10077 cp_token *token;
10078 enum tree_code rhs_type;
10079 enum cp_parser_prec new_prec, lookahead_prec;
10080 tree overload;
10082 /* Parse the first expression. */
10083 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10084 ? TRUTH_NOT_EXPR : ERROR_MARK);
10085 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
10086 cast_p, decltype_p, pidk);
10087 current.prec = prec;
10089 if (cp_parser_error_occurred (parser))
10090 return error_mark_node;
10092 for (;;)
10094 /* Get an operator token. */
10095 token = cp_lexer_peek_token (parser->lexer);
10097 if (warn_cxx11_compat
10098 && token->type == CPP_RSHIFT
10099 && !parser->greater_than_is_operator_p)
10101 if (warning_at (token->location, OPT_Wc__11_compat,
10102 "%<>>%> operator is treated"
10103 " as two right angle brackets in C++11"))
10104 inform (token->location,
10105 "suggest parentheses around %<>>%> expression");
10108 new_prec = TOKEN_PRECEDENCE (token);
10109 if (new_prec != PREC_NOT_OPERATOR
10110 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10111 /* This is a fold-expression; handle it later. */
10112 new_prec = PREC_NOT_OPERATOR;
10114 /* Popping an entry off the stack means we completed a subexpression:
10115 - either we found a token which is not an operator (`>' where it is not
10116 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
10117 will happen repeatedly;
10118 - or, we found an operator which has lower priority. This is the case
10119 where the recursive descent *ascends*, as in `3 * 4 + 5' after
10120 parsing `3 * 4'. */
10121 if (new_prec <= current.prec)
10123 if (sp == stack)
10124 break;
10125 else
10126 goto pop;
10129 get_rhs:
10130 current.tree_type = binops_by_token[token->type].tree_type;
10131 current.loc = token->location;
10132 current.flags = token->flags;
10134 /* We used the operator token. */
10135 cp_lexer_consume_token (parser->lexer);
10137 /* For "false && x" or "true || x", x will never be executed;
10138 disable warnings while evaluating it. */
10139 if ((current.tree_type == TRUTH_ANDIF_EXPR
10140 && cp_fully_fold (current.lhs) == truthvalue_false_node)
10141 || (current.tree_type == TRUTH_ORIF_EXPR
10142 && cp_fully_fold (current.lhs) == truthvalue_true_node))
10144 disable_warnings_sp = sp;
10145 ++c_inhibit_evaluation_warnings;
10148 /* Extract another operand. It may be the RHS of this expression
10149 or the LHS of a new, higher priority expression. */
10150 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
10151 ? TRUTH_NOT_EXPR : ERROR_MARK);
10152 rhs = cp_parser_simple_cast_expression (parser);
10154 /* Get another operator token. Look up its precedence to avoid
10155 building a useless (immediately popped) stack entry for common
10156 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
10157 token = cp_lexer_peek_token (parser->lexer);
10158 lookahead_prec = TOKEN_PRECEDENCE (token);
10159 if (lookahead_prec != PREC_NOT_OPERATOR
10160 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10161 lookahead_prec = PREC_NOT_OPERATOR;
10162 if (lookahead_prec > new_prec)
10164 /* ... and prepare to parse the RHS of the new, higher priority
10165 expression. Since precedence levels on the stack are
10166 monotonically increasing, we do not have to care about
10167 stack overflows. */
10168 *sp = current;
10169 ++sp;
10170 current.lhs = rhs;
10171 current.lhs_type = rhs_type;
10172 current.prec = new_prec;
10173 new_prec = lookahead_prec;
10174 goto get_rhs;
10176 pop:
10177 lookahead_prec = new_prec;
10178 /* If the stack is not empty, we have parsed into LHS the right side
10179 (`4' in the example above) of an expression we had suspended.
10180 We can use the information on the stack to recover the LHS (`3')
10181 from the stack together with the tree code (`MULT_EXPR'), and
10182 the precedence of the higher level subexpression
10183 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
10184 which will be used to actually build the additive expression. */
10185 rhs = current.lhs;
10186 rhs_type = current.lhs_type;
10187 --sp;
10188 current = *sp;
10191 /* Undo the disabling of warnings done above. */
10192 if (sp == disable_warnings_sp)
10194 disable_warnings_sp = NULL;
10195 --c_inhibit_evaluation_warnings;
10198 if (warn_logical_not_paren
10199 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
10200 && current.lhs_type == TRUTH_NOT_EXPR
10201 /* Avoid warning for !!x == y. */
10202 && (TREE_CODE (current.lhs) != NE_EXPR
10203 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
10204 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
10205 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
10206 /* Avoid warning for !b == y where b is boolean. */
10207 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
10208 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
10209 != BOOLEAN_TYPE))))
10210 /* Avoid warning for !!b == y where b is boolean. */
10211 && (!DECL_P (tree_strip_any_location_wrapper (current.lhs))
10212 || TREE_TYPE (current.lhs) == NULL_TREE
10213 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
10214 warn_logical_not_parentheses (current.loc, current.tree_type,
10215 current.lhs, maybe_constant_value (rhs));
10217 if (warn_xor_used_as_pow
10218 && current.tree_type == BIT_XOR_EXPR
10219 /* Don't warn for named "xor" (as opposed to '^'). */
10220 && !(current.flags & NAMED_OP)
10221 && current.lhs.decimal_p ()
10222 && rhs.decimal_p ())
10223 check_for_xor_used_as_pow
10224 (current.lhs.get_location (),
10225 tree_strip_any_location_wrapper (current.lhs),
10226 current.loc,
10227 tree_strip_any_location_wrapper (rhs));
10229 overload = NULL;
10231 location_t combined_loc = make_location (current.loc,
10232 current.lhs.get_start (),
10233 rhs.get_finish ());
10235 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
10236 ERROR_MARK for everything that is not a binary expression.
10237 This makes warn_about_parentheses miss some warnings that
10238 involve unary operators. For unary expressions we should
10239 pass the correct tree_code unless the unary expression was
10240 surrounded by parentheses.
10242 if (no_toplevel_fold_p
10243 && lookahead_prec <= current.prec
10244 && sp == stack)
10246 if (current.lhs == error_mark_node || rhs == error_mark_node)
10247 current.lhs = error_mark_node;
10248 else
10250 current.lhs.maybe_add_location_wrapper ();
10251 rhs.maybe_add_location_wrapper ();
10252 current.lhs
10253 = build_min (current.tree_type,
10254 TREE_CODE_CLASS (current.tree_type)
10255 == tcc_comparison
10256 ? boolean_type_node : TREE_TYPE (current.lhs),
10257 current.lhs.get_value (), rhs.get_value ());
10258 SET_EXPR_LOCATION (current.lhs, combined_loc);
10261 else
10263 op_location_t op_loc (current.loc, combined_loc);
10264 current.lhs = build_x_binary_op (op_loc, current.tree_type,
10265 current.lhs, current.lhs_type,
10266 rhs, rhs_type, NULL_TREE, &overload,
10267 complain_flags (decltype_p));
10268 /* TODO: build_x_binary_op doesn't always honor the location. */
10269 current.lhs.set_location (combined_loc);
10271 current.lhs_type = current.tree_type;
10273 /* If the binary operator required the use of an overloaded operator,
10274 then this expression cannot be an integral constant-expression.
10275 An overloaded operator can be used even if both operands are
10276 otherwise permissible in an integral constant-expression if at
10277 least one of the operands is of enumeration type. */
10279 if (overload
10280 && cp_parser_non_integral_constant_expression (parser,
10281 NIC_OVERLOADED))
10282 return error_mark_node;
10285 return current.lhs;
10288 static cp_expr
10289 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
10290 bool no_toplevel_fold_p,
10291 enum cp_parser_prec prec,
10292 cp_id_kind * pidk)
10294 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
10295 /*decltype*/false, prec, pidk);
10298 /* Parse the `? expression : assignment-expression' part of a
10299 conditional-expression. The LOGICAL_OR_EXPR is the
10300 logical-or-expression that started the conditional-expression.
10301 Returns a representation of the entire conditional-expression.
10303 This routine is used by cp_parser_assignment_expression
10304 and cp_parser_conditional_expression.
10306 ? expression : assignment-expression
10308 GNU Extensions:
10310 ? : assignment-expression */
10312 static tree
10313 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
10315 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
10316 cp_expr assignment_expr;
10317 struct cp_token *token;
10318 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10320 /* Consume the `?' token. */
10321 cp_lexer_consume_token (parser->lexer);
10322 token = cp_lexer_peek_token (parser->lexer);
10323 if (cp_parser_allow_gnu_extensions_p (parser)
10324 && token->type == CPP_COLON)
10326 pedwarn (token->location, OPT_Wpedantic,
10327 "ISO C++ does not allow %<?:%> with omitted middle operand");
10328 /* Implicit true clause. */
10329 expr = NULL_TREE;
10330 c_inhibit_evaluation_warnings +=
10331 folded_logical_or_expr == truthvalue_true_node;
10332 warn_for_omitted_condop (token->location, logical_or_expr);
10334 else
10336 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10337 parser->colon_corrects_to_scope_p = false;
10338 /* Parse the expression. */
10339 c_inhibit_evaluation_warnings +=
10340 folded_logical_or_expr == truthvalue_false_node;
10341 expr = cp_parser_expression (parser);
10342 c_inhibit_evaluation_warnings +=
10343 ((folded_logical_or_expr == truthvalue_true_node)
10344 - (folded_logical_or_expr == truthvalue_false_node));
10345 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10348 /* The next token should be a `:'. */
10349 cp_parser_require (parser, CPP_COLON, RT_COLON);
10350 /* Parse the assignment-expression. */
10351 assignment_expr = cp_parser_assignment_expression (parser);
10352 c_inhibit_evaluation_warnings -=
10353 folded_logical_or_expr == truthvalue_true_node;
10355 /* Make a location:
10356 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10357 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10358 with the caret at the "?", ranging from the start of
10359 the logical_or_expr to the end of the assignment_expr. */
10360 loc = make_location (loc,
10361 logical_or_expr.get_start (),
10362 assignment_expr.get_finish ());
10364 /* Build the conditional-expression. */
10365 return build_x_conditional_expr (loc, logical_or_expr,
10366 expr,
10367 assignment_expr,
10368 tf_warning_or_error);
10371 /* Parse a conditional-expression.
10373 conditional-expression:
10374 logical-or-expression
10375 logical-or-expression ? expression : assignment-expression
10377 GNU Extensions:
10379 logical-or-expression ? : assignment-expression */
10381 static cp_expr
10382 cp_parser_conditional_expression (cp_parser *parser)
10384 cp_expr expr = cp_parser_binary_expression (parser, false, false, false,
10385 PREC_NOT_OPERATOR, NULL);
10386 /* If the next token is a `?' then we're actually looking at
10387 a conditional-expression; otherwise we're done. */
10388 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10389 return cp_parser_question_colon_clause (parser, expr);
10390 return expr;
10393 /* Parse an assignment-expression.
10395 assignment-expression:
10396 conditional-expression
10397 logical-or-expression assignment-operator assignment_expression
10398 throw-expression
10399 yield-expression
10401 CAST_P is true if this expression is the target of a cast.
10402 DECLTYPE_P is true if this expression is the operand of decltype.
10404 Returns a representation for the expression. */
10406 static cp_expr
10407 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
10408 bool cast_p, bool decltype_p)
10410 cp_expr expr;
10412 /* If the next token is the `throw' keyword, then we're looking at
10413 a throw-expression. */
10414 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
10415 expr = cp_parser_throw_expression (parser);
10416 /* If the next token is the `co_yield' keyword, then we're looking at
10417 a yield-expression. */
10418 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CO_YIELD))
10419 expr = cp_parser_yield_expression (parser);
10420 /* Otherwise, it must be that we are looking at a
10421 logical-or-expression. */
10422 else
10424 /* Parse the binary expressions (logical-or-expression). */
10425 expr = cp_parser_binary_expression (parser, cast_p, false,
10426 decltype_p,
10427 PREC_NOT_OPERATOR, pidk);
10428 /* If the next token is a `?' then we're actually looking at a
10429 conditional-expression. */
10430 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
10431 return cp_parser_question_colon_clause (parser, expr);
10432 else
10434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10436 /* If it's an assignment-operator, we're using the second
10437 production. */
10438 enum tree_code assignment_operator
10439 = cp_parser_assignment_operator_opt (parser);
10440 if (assignment_operator != ERROR_MARK)
10442 bool non_constant_p;
10444 /* Parse the right-hand side of the assignment. */
10445 cp_expr rhs = cp_parser_initializer_clause (parser,
10446 &non_constant_p);
10448 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
10449 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10451 /* An assignment may not appear in a
10452 constant-expression. */
10453 if (cp_parser_non_integral_constant_expression (parser,
10454 NIC_ASSIGNMENT))
10455 return error_mark_node;
10456 /* Build the assignment expression. Its default
10457 location:
10458 LHS = RHS
10459 ~~~~^~~~~
10460 is the location of the '=' token as the
10461 caret, ranging from the start of the lhs to the
10462 end of the rhs. */
10463 loc = make_location (loc,
10464 expr.get_start (),
10465 rhs.get_finish ());
10466 expr = build_x_modify_expr (loc, expr,
10467 assignment_operator,
10468 rhs, NULL_TREE,
10469 complain_flags (decltype_p));
10470 /* TODO: build_x_modify_expr doesn't honor the location,
10471 so we must set it here. */
10472 expr.set_location (loc);
10477 return expr;
10480 /* Parse an (optional) assignment-operator.
10482 assignment-operator: one of
10483 = *= /= %= += -= >>= <<= &= ^= |=
10485 GNU Extension:
10487 assignment-operator: one of
10488 <?= >?=
10490 If the next token is an assignment operator, the corresponding tree
10491 code is returned, and the token is consumed. For example, for
10492 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10493 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10494 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10495 operator, ERROR_MARK is returned. */
10497 static enum tree_code
10498 cp_parser_assignment_operator_opt (cp_parser* parser)
10500 enum tree_code op;
10501 cp_token *token;
10503 /* Peek at the next token. */
10504 token = cp_lexer_peek_token (parser->lexer);
10506 switch (token->type)
10508 case CPP_EQ:
10509 op = NOP_EXPR;
10510 break;
10512 case CPP_MULT_EQ:
10513 op = MULT_EXPR;
10514 break;
10516 case CPP_DIV_EQ:
10517 op = TRUNC_DIV_EXPR;
10518 break;
10520 case CPP_MOD_EQ:
10521 op = TRUNC_MOD_EXPR;
10522 break;
10524 case CPP_PLUS_EQ:
10525 op = PLUS_EXPR;
10526 break;
10528 case CPP_MINUS_EQ:
10529 op = MINUS_EXPR;
10530 break;
10532 case CPP_RSHIFT_EQ:
10533 op = RSHIFT_EXPR;
10534 break;
10536 case CPP_LSHIFT_EQ:
10537 op = LSHIFT_EXPR;
10538 break;
10540 case CPP_AND_EQ:
10541 op = BIT_AND_EXPR;
10542 break;
10544 case CPP_XOR_EQ:
10545 op = BIT_XOR_EXPR;
10546 break;
10548 case CPP_OR_EQ:
10549 op = BIT_IOR_EXPR;
10550 break;
10552 default:
10553 /* Nothing else is an assignment operator. */
10554 op = ERROR_MARK;
10557 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10558 if (op != ERROR_MARK
10559 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10560 op = ERROR_MARK;
10562 /* If it was an assignment operator, consume it. */
10563 if (op != ERROR_MARK)
10564 cp_lexer_consume_token (parser->lexer);
10566 return op;
10569 /* Parse an expression.
10571 expression:
10572 assignment-expression
10573 expression , assignment-expression
10575 CAST_P is true if this expression is the target of a cast.
10576 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10577 except possibly parenthesized or on the RHS of a comma (N3276).
10578 WARN_COMMA_P is true if a comma should be diagnosed.
10580 Returns a representation of the expression. */
10582 static cp_expr
10583 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
10584 bool cast_p, bool decltype_p, bool warn_comma_p)
10586 cp_expr expression = NULL_TREE;
10587 location_t loc = UNKNOWN_LOCATION;
10589 while (true)
10591 cp_expr assignment_expression;
10593 /* Parse the next assignment-expression. */
10594 assignment_expression
10595 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
10597 /* We don't create a temporary for a call that is the immediate operand
10598 of decltype or on the RHS of a comma. But when we see a comma, we
10599 need to create a temporary for a call on the LHS. */
10600 if (decltype_p && !processing_template_decl
10601 && TREE_CODE (assignment_expression) == CALL_EXPR
10602 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
10603 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10604 assignment_expression
10605 = build_cplus_new (TREE_TYPE (assignment_expression),
10606 assignment_expression, tf_warning_or_error);
10608 /* If this is the first assignment-expression, we can just
10609 save it away. */
10610 if (!expression)
10611 expression = assignment_expression;
10612 else
10614 /* Create a location with caret at the comma, ranging
10615 from the start of the LHS to the end of the RHS. */
10616 loc = make_location (loc,
10617 expression.get_start (),
10618 assignment_expression.get_finish ());
10619 expression = build_x_compound_expr (loc, expression,
10620 assignment_expression, NULL_TREE,
10621 complain_flags (decltype_p));
10622 expression.set_location (loc);
10624 /* If the next token is not a comma, or we're in a fold-expression, then
10625 we are done with the expression. */
10626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
10627 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
10628 break;
10629 /* Consume the `,'. */
10630 loc = cp_lexer_peek_token (parser->lexer)->location;
10631 if (warn_comma_p)
10633 /* [depr.comma.subscript]: A comma expression appearing as
10634 the expr-or-braced-init-list of a subscripting expression
10635 is deprecated. A parenthesized comma expression is not
10636 deprecated. */
10637 warning_at (loc, OPT_Wcomma_subscript,
10638 "top-level comma expression in array subscript "
10639 "is deprecated");
10640 warn_comma_p = false;
10642 cp_lexer_consume_token (parser->lexer);
10643 /* A comma operator cannot appear in a constant-expression. */
10644 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
10645 expression = error_mark_node;
10648 return expression;
10651 /* Parse a constant-expression.
10653 constant-expression:
10654 conditional-expression
10656 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10657 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10658 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10659 is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
10660 greater than 1, this isn't really a constant-expression, only a
10661 potentially constant-evaluated expression. If STRICT_P is true,
10662 only parse a conditional-expression, otherwise parse an
10663 assignment-expression. See below for rationale. */
10665 static cp_expr
10666 cp_parser_constant_expression (cp_parser* parser,
10667 int allow_non_constant_p,
10668 bool *non_constant_p,
10669 bool strict_p)
10671 bool saved_integral_constant_expression_p;
10672 bool saved_allow_non_integral_constant_expression_p;
10673 bool saved_non_integral_constant_expression_p;
10674 cp_expr expression;
10676 /* It might seem that we could simply parse the
10677 conditional-expression, and then check to see if it were
10678 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10679 one that the compiler can figure out is constant, possibly after
10680 doing some simplifications or optimizations. The standard has a
10681 precise definition of constant-expression, and we must honor
10682 that, even though it is somewhat more restrictive.
10684 For example:
10686 int i[(2, 3)];
10688 is not a legal declaration, because `(2, 3)' is not a
10689 constant-expression. The `,' operator is forbidden in a
10690 constant-expression. However, GCC's constant-folding machinery
10691 will fold this operation to an INTEGER_CST for `3'. */
10693 /* Save the old settings. */
10694 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
10695 saved_allow_non_integral_constant_expression_p
10696 = parser->allow_non_integral_constant_expression_p;
10697 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
10698 /* We are now parsing a constant-expression. */
10699 parser->integral_constant_expression_p = true;
10700 parser->allow_non_integral_constant_expression_p
10701 = (allow_non_constant_p || cxx_dialect >= cxx11);
10702 parser->non_integral_constant_expression_p = false;
10704 /* A manifestly constant-evaluated expression is evaluated even in an
10705 unevaluated operand. */
10706 cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
10708 /* Although the grammar says "conditional-expression", when not STRICT_P,
10709 we parse an "assignment-expression", which also permits
10710 "throw-expression" and the use of assignment operators. In the case
10711 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10712 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10713 actually essential that we look for an assignment-expression.
10714 For example, cp_parser_initializer_clauses uses this function to
10715 determine whether a particular assignment-expression is in fact
10716 constant. */
10717 if (strict_p)
10718 expression = cp_parser_conditional_expression (parser);
10719 else
10720 expression = cp_parser_assignment_expression (parser);
10721 /* Restore the old settings. */
10722 parser->integral_constant_expression_p
10723 = saved_integral_constant_expression_p;
10724 parser->allow_non_integral_constant_expression_p
10725 = saved_allow_non_integral_constant_expression_p;
10726 if (cxx_dialect >= cxx11)
10728 /* Require an rvalue constant expression here; that's what our
10729 callers expect. Reference constant expressions are handled
10730 separately in e.g. cp_parser_template_argument. */
10731 tree decay = expression;
10732 if (TREE_TYPE (expression)
10733 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
10734 decay = build_address (expression);
10735 bool is_const = is_rvalue_constant_expression (decay);
10736 parser->non_integral_constant_expression_p = !is_const;
10737 if (!is_const && !allow_non_constant_p)
10738 require_rvalue_constant_expression (decay);
10740 if (allow_non_constant_p)
10741 *non_constant_p = parser->non_integral_constant_expression_p;
10742 parser->non_integral_constant_expression_p
10743 = saved_non_integral_constant_expression_p;
10745 return expression;
10748 /* Parse __builtin_offsetof.
10750 offsetof-expression:
10751 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10753 offsetof-member-designator:
10754 id-expression
10755 | offsetof-member-designator "." id-expression
10756 | offsetof-member-designator "[" expression "]"
10757 | offsetof-member-designator "->" id-expression */
10759 static cp_expr
10760 cp_parser_builtin_offsetof (cp_parser *parser)
10762 int save_ice_p, save_non_ice_p;
10763 tree type;
10764 cp_expr expr;
10765 cp_id_kind dummy;
10766 cp_token *token;
10767 location_t finish_loc;
10769 /* We're about to accept non-integral-constant things, but will
10770 definitely yield an integral constant expression. Save and
10771 restore these values around our local parsing. */
10772 save_ice_p = parser->integral_constant_expression_p;
10773 save_non_ice_p = parser->non_integral_constant_expression_p;
10775 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10777 /* Consume the "__builtin_offsetof" token. */
10778 cp_lexer_consume_token (parser->lexer);
10779 /* Consume the opening `('. */
10780 matching_parens parens;
10781 parens.require_open (parser);
10782 /* Parse the type-id. */
10783 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10785 const char *saved_message = parser->type_definition_forbidden_message;
10786 parser->type_definition_forbidden_message
10787 = G_("types may not be defined within %<__builtin_offsetof%>");
10788 type = cp_parser_type_id (parser);
10789 parser->type_definition_forbidden_message = saved_message;
10791 /* Look for the `,'. */
10792 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10793 token = cp_lexer_peek_token (parser->lexer);
10795 /* Build the (type *)null that begins the traditional offsetof macro. */
10796 tree object_ptr
10797 = build_static_cast (input_location, build_pointer_type (type),
10798 null_pointer_node, tf_warning_or_error);
10800 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10801 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
10802 true, &dummy, token->location);
10803 while (true)
10805 token = cp_lexer_peek_token (parser->lexer);
10806 switch (token->type)
10808 case CPP_OPEN_SQUARE:
10809 /* offsetof-member-designator "[" expression "]" */
10810 expr = cp_parser_postfix_open_square_expression (parser, expr,
10811 true, false);
10812 break;
10814 case CPP_DEREF:
10815 /* offsetof-member-designator "->" identifier */
10816 expr = grok_array_decl (token->location, expr, integer_zero_node,
10817 NULL, tf_warning_or_error);
10818 /* FALLTHRU */
10820 case CPP_DOT:
10821 /* offsetof-member-designator "." identifier */
10822 cp_lexer_consume_token (parser->lexer);
10823 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
10824 expr, true, &dummy,
10825 token->location);
10826 break;
10828 case CPP_CLOSE_PAREN:
10829 /* Consume the ")" token. */
10830 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10831 cp_lexer_consume_token (parser->lexer);
10832 goto success;
10834 default:
10835 /* Error. We know the following require will fail, but
10836 that gives the proper error message. */
10837 parens.require_close (parser);
10838 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
10839 expr = error_mark_node;
10840 goto failure;
10844 success:
10845 /* Make a location of the form:
10846 __builtin_offsetof (struct s, f)
10847 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10848 with caret at the type-id, ranging from the start of the
10849 "_builtin_offsetof" token to the close paren. */
10850 loc = make_location (loc, start_loc, finish_loc);
10851 /* The result will be an INTEGER_CST, so we need to explicitly
10852 preserve the location. */
10853 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
10855 failure:
10856 parser->integral_constant_expression_p = save_ice_p;
10857 parser->non_integral_constant_expression_p = save_non_ice_p;
10859 expr = expr.maybe_add_location_wrapper ();
10860 return expr;
10863 /* Parse a builtin trait expression or type. */
10865 static cp_expr
10866 cp_parser_trait (cp_parser* parser, enum rid keyword)
10868 cp_trait_kind kind;
10869 tree type1, type2 = NULL_TREE;
10870 bool binary = false;
10871 bool variadic = false;
10872 bool type = false;
10874 switch (keyword)
10876 #define DEFTRAIT(TCC, CODE, NAME, ARITY) \
10877 case RID_##CODE: \
10878 kind = CPTK_##CODE; \
10879 binary = (ARITY == 2); \
10880 variadic = (ARITY == -1); \
10881 type = (TCC == tcc_type); \
10882 break;
10883 #include "cp-trait.def"
10884 #undef DEFTRAIT
10885 default:
10886 gcc_unreachable ();
10889 /* Get location of initial token. */
10890 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
10892 /* Consume the token. */
10893 cp_lexer_consume_token (parser->lexer);
10895 matching_parens parens;
10896 parens.require_open (parser);
10899 type_id_in_expr_sentinel s (parser);
10900 type1 = cp_parser_type_id (parser);
10903 if (type1 == error_mark_node)
10904 return error_mark_node;
10906 if (binary)
10908 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10911 type_id_in_expr_sentinel s (parser);
10912 type2 = cp_parser_type_id (parser);
10915 if (type2 == error_mark_node)
10916 return error_mark_node;
10918 else if (variadic)
10920 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10922 cp_lexer_consume_token (parser->lexer);
10923 tree elt = cp_parser_type_id (parser);
10924 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10926 cp_lexer_consume_token (parser->lexer);
10927 elt = make_pack_expansion (elt);
10929 if (elt == error_mark_node)
10930 return error_mark_node;
10931 type2 = tree_cons (NULL_TREE, elt, type2);
10933 type2 = nreverse (type2);
10936 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
10937 parens.require_close (parser);
10939 /* Construct a location of the form:
10940 __is_trivially_copyable(_Tp)
10941 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10942 with start == caret, finishing at the close-paren. */
10943 location_t trait_loc = make_location (start_loc, start_loc, finish_loc);
10945 /* Complete the trait expression, which may mean either processing
10946 the trait expr now or saving it for template instantiation. */
10947 switch (kind)
10949 case CPTK_BASES:
10950 return cp_expr (finish_bases (type1, false), trait_loc);
10951 case CPTK_DIRECT_BASES:
10952 return cp_expr (finish_bases (type1, true), trait_loc);
10953 default:
10954 if (type)
10955 return finish_trait_type (kind, type1, type2);
10956 else
10957 return finish_trait_expr (trait_loc, kind, type1, type2);
10961 /* Parse a lambda expression.
10963 lambda-expression:
10964 lambda-introducer lambda-declarator [opt] compound-statement
10965 lambda-introducer < template-parameter-list > requires-clause [opt]
10966 lambda-declarator [opt] compound-statement
10968 Returns a representation of the expression. */
10970 static cp_expr
10971 cp_parser_lambda_expression (cp_parser* parser)
10973 tree lambda_expr = build_lambda_expr ();
10974 tree type;
10975 bool ok = true;
10976 cp_token *token = cp_lexer_peek_token (parser->lexer);
10977 cp_token_position start = 0;
10979 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10981 if (cxx_dialect >= cxx20)
10983 /* C++20 allows lambdas in unevaluated context, but one in the type of a
10984 non-type parameter is nonsensical.
10986 Distinguish a lambda in the parameter type from a lambda in the
10987 default argument by looking at local_variables_forbidden_p, which is
10988 only set in default arguments. */
10989 if (processing_template_parmlist && !parser->local_variables_forbidden_p)
10991 error_at (token->location,
10992 "lambda-expression in template parameter type");
10993 token->error_reported = true;
10994 ok = false;
10997 else if (cp_unevaluated_operand)
10999 if (!token->error_reported)
11001 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
11002 "lambda-expression in unevaluated context"
11003 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11004 token->error_reported = true;
11006 ok = false;
11008 else if (parser->in_template_argument_list_p || processing_template_parmlist)
11010 if (!token->error_reported)
11012 error_at (token->location, "lambda-expression in template-argument"
11013 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
11014 token->error_reported = true;
11016 ok = false;
11019 /* We may be in the middle of deferred access check. Disable
11020 it now. */
11021 push_deferring_access_checks (dk_no_deferred);
11023 cp_parser_lambda_introducer (parser, lambda_expr);
11024 if (cp_parser_error_occurred (parser))
11025 return error_mark_node;
11027 type = begin_lambda_type (lambda_expr);
11028 if (type == error_mark_node)
11029 return error_mark_node;
11031 record_lambda_scope (lambda_expr);
11033 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
11034 determine_visibility (TYPE_NAME (type));
11036 /* Now that we've started the type, add the capture fields for any
11037 explicit captures. */
11038 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11041 /* Inside the class, surrounding template-parameter-lists do not apply. */
11042 unsigned int saved_num_template_parameter_lists
11043 = parser->num_template_parameter_lists;
11044 unsigned char in_statement = parser->in_statement;
11045 bool in_switch_statement_p = parser->in_switch_statement_p;
11046 bool fully_implicit_function_template_p
11047 = parser->fully_implicit_function_template_p;
11048 tree implicit_template_parms = parser->implicit_template_parms;
11049 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
11050 bool auto_is_implicit_function_template_parm_p
11051 = parser->auto_is_implicit_function_template_parm_p;
11053 parser->num_template_parameter_lists = 0;
11054 parser->in_statement = 0;
11055 parser->in_switch_statement_p = false;
11056 parser->fully_implicit_function_template_p = false;
11057 parser->implicit_template_parms = 0;
11058 parser->implicit_template_scope = 0;
11059 parser->auto_is_implicit_function_template_parm_p = false;
11061 /* The body of a lambda in a discarded statement is not discarded. */
11062 bool discarded = in_discarded_stmt;
11063 in_discarded_stmt = 0;
11065 /* Similarly the body of a lambda in immediate function context is not
11066 in immediate function context. */
11067 bool save_in_consteval_if_p = in_consteval_if_p;
11068 in_consteval_if_p = false;
11070 /* By virtue of defining a local class, a lambda expression has access to
11071 the private variables of enclosing classes. */
11073 if (cp_parser_start_tentative_firewall (parser))
11074 start = token;
11076 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
11078 if (ok && cp_parser_error_occurred (parser))
11079 ok = false;
11081 if (ok)
11083 cp_parser_lambda_body (parser, lambda_expr);
11085 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11087 if (cp_parser_skip_to_closing_brace (parser))
11088 cp_lexer_consume_token (parser->lexer);
11091 /* The capture list was built up in reverse order; fix that now. */
11092 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
11093 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
11095 if (ok)
11096 maybe_add_lambda_conv_op (type);
11098 finish_struct (type, /*attributes=*/NULL_TREE);
11100 in_consteval_if_p = save_in_consteval_if_p;
11101 in_discarded_stmt = discarded;
11103 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
11104 parser->in_statement = in_statement;
11105 parser->in_switch_statement_p = in_switch_statement_p;
11106 parser->fully_implicit_function_template_p
11107 = fully_implicit_function_template_p;
11108 parser->implicit_template_parms = implicit_template_parms;
11109 parser->implicit_template_scope = implicit_template_scope;
11110 parser->auto_is_implicit_function_template_parm_p
11111 = auto_is_implicit_function_template_parm_p;
11114 /* This field is only used during parsing of the lambda. */
11115 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
11117 /* This lambda shouldn't have any proxies left at this point. */
11118 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
11119 /* And now that we're done, push proxies for an enclosing lambda. */
11120 insert_pending_capture_proxies ();
11122 /* Update the lambda expression to a range. */
11123 LAMBDA_EXPR_LOCATION (lambda_expr) = make_location (token->location,
11124 token->location,
11125 parser->lexer);
11127 if (ok)
11128 lambda_expr = build_lambda_object (lambda_expr);
11129 else
11130 lambda_expr = error_mark_node;
11132 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
11134 pop_deferring_access_checks ();
11136 return lambda_expr;
11139 /* Parse the beginning of a lambda expression.
11141 lambda-introducer:
11142 [ lambda-capture [opt] ]
11144 LAMBDA_EXPR is the current representation of the lambda expression. */
11146 static void
11147 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
11149 /* Need commas after the first capture. */
11150 bool first = true;
11152 /* Eat the leading `['. */
11153 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
11155 /* Record default capture mode. "[&" "[=" "[&," "[=," */
11156 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11157 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS)
11158 && !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)
11159 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11160 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
11161 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11162 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
11164 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
11166 cp_lexer_consume_token (parser->lexer);
11167 first = false;
11169 if (!(at_function_scope_p () || parsing_nsdmi ()))
11170 error ("non-local lambda expression cannot have a capture-default");
11173 hash_set<tree, true> ids;
11174 tree first_capture_id = NULL_TREE;
11175 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
11177 cp_token* capture_token;
11178 tree capture_id;
11179 tree capture_init_expr;
11180 cp_id_kind idk = CP_ID_KIND_NONE;
11181 bool explicit_init_p = false;
11183 enum capture_kind_type
11185 BY_COPY,
11186 BY_REFERENCE
11188 enum capture_kind_type capture_kind = BY_COPY;
11190 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
11192 error ("expected end of capture-list");
11193 return;
11196 if (first)
11197 first = false;
11198 else
11199 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
11201 /* Possibly capture `this'. */
11202 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
11204 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11205 if (cxx_dialect < cxx20 && pedantic
11206 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
11207 pedwarn (loc, OPT_Wc__20_extensions,
11208 "explicit by-copy capture of %<this%> "
11209 "with by-copy capture default only available with "
11210 "%<-std=c++20%> or %<-std=gnu++20%>");
11211 cp_lexer_consume_token (parser->lexer);
11212 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11213 pedwarn (input_location, 0,
11214 "already captured %qD in lambda expression",
11215 this_identifier);
11216 else
11217 add_capture (lambda_expr, /*id=*/this_identifier,
11218 /*initializer=*/finish_this_expr (),
11219 /*by_reference_p=*/true, explicit_init_p);
11220 continue;
11223 /* Possibly capture `*this'. */
11224 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
11225 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11227 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11228 if (cxx_dialect < cxx17)
11229 pedwarn (loc, OPT_Wc__17_extensions,
11230 "%<*this%> capture only available with "
11231 "%<-std=c++17%> or %<-std=gnu++17%>");
11232 cp_lexer_consume_token (parser->lexer);
11233 cp_lexer_consume_token (parser->lexer);
11234 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
11235 pedwarn (input_location, 0,
11236 "already captured %qD in lambda expression",
11237 this_identifier);
11238 else
11239 add_capture (lambda_expr, /*id=*/this_identifier,
11240 /*initializer=*/finish_this_expr (),
11241 /*by_reference_p=*/false, explicit_init_p);
11242 continue;
11245 /* But reject `&this'. */
11246 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
11247 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
11249 error_at (cp_lexer_peek_token (parser->lexer)->location,
11250 "%<this%> cannot be captured by reference");
11251 cp_lexer_consume_token (parser->lexer);
11252 cp_lexer_consume_token (parser->lexer);
11253 continue;
11256 /* Remember whether we want to capture as a reference or not. */
11257 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
11259 capture_kind = BY_REFERENCE;
11260 cp_lexer_consume_token (parser->lexer);
11263 bool init_pack_expansion = false;
11264 location_t ellipsis_loc = UNKNOWN_LOCATION;
11265 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11267 ellipsis_loc = cp_lexer_peek_token (parser->lexer)->location;
11268 if (cxx_dialect < cxx20)
11269 pedwarn (ellipsis_loc, OPT_Wc__20_extensions,
11270 "pack init-capture only available with "
11271 "%<-std=c++20%> or %<-std=gnu++20%>");
11272 cp_lexer_consume_token (parser->lexer);
11273 init_pack_expansion = true;
11276 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11277 if (init_pack_expansion && capture_kind != BY_REFERENCE
11278 && cp_lexer_next_token_is (parser->lexer, CPP_AND))
11280 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
11281 0, "%<&%> should come before %<...%>");
11282 capture_kind = BY_REFERENCE;
11283 cp_lexer_consume_token (parser->lexer);
11286 /* Get the identifier. */
11287 capture_token = cp_lexer_peek_token (parser->lexer);
11288 capture_id = cp_parser_identifier (parser);
11290 if (capture_id == error_mark_node)
11291 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11292 delimiters, but I modified this to stop on unnested ']' as well. It
11293 was already changed to stop on unnested '}', so the
11294 "closing_parenthesis" name is no more misleading with my change. */
11296 cp_parser_skip_to_closing_parenthesis (parser,
11297 /*recovering=*/true,
11298 /*or_comma=*/true,
11299 /*consume_paren=*/true);
11300 break;
11303 /* Find the initializer for this capture. */
11304 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11305 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11306 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11308 bool direct, non_constant;
11309 /* An explicit initializer exists. */
11310 if (cxx_dialect < cxx14)
11311 pedwarn (input_location, OPT_Wc__14_extensions,
11312 "lambda capture initializers "
11313 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11314 capture_init_expr = cp_parser_initializer (parser, &direct,
11315 &non_constant, true);
11316 explicit_init_p = true;
11317 if (capture_init_expr == NULL_TREE)
11319 error ("empty initializer for lambda init-capture");
11320 capture_init_expr = error_mark_node;
11322 if (init_pack_expansion)
11323 capture_init_expr = make_pack_expansion (capture_init_expr);
11325 else
11327 const char* error_msg;
11329 /* Turn the identifier into an id-expression. */
11330 capture_init_expr
11331 = cp_parser_lookup_name_simple (parser, capture_id,
11332 capture_token->location);
11334 if (capture_init_expr == error_mark_node)
11336 unqualified_name_lookup_error (capture_id);
11337 continue;
11339 else if (!VAR_P (capture_init_expr)
11340 && TREE_CODE (capture_init_expr) != PARM_DECL)
11342 error_at (capture_token->location,
11343 "capture of non-variable %qE",
11344 capture_init_expr);
11345 if (DECL_P (capture_init_expr))
11346 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11347 "%q#D declared here", capture_init_expr);
11348 continue;
11350 if (VAR_P (capture_init_expr)
11351 && decl_storage_duration (capture_init_expr) != dk_auto)
11353 if (pedwarn (capture_token->location, 0, "capture of variable "
11354 "%qD with non-automatic storage duration",
11355 capture_init_expr))
11356 inform (DECL_SOURCE_LOCATION (capture_init_expr),
11357 "%q#D declared here", capture_init_expr);
11358 continue;
11361 capture_init_expr
11362 = finish_id_expression
11363 (capture_id,
11364 capture_init_expr,
11365 parser->scope,
11366 &idk,
11367 /*integral_constant_expression_p=*/false,
11368 /*allow_non_integral_constant_expression_p=*/false,
11369 /*non_integral_constant_expression_p=*/NULL,
11370 /*template_p=*/false,
11371 /*done=*/true,
11372 /*address_p=*/false,
11373 /*template_arg_p=*/false,
11374 &error_msg,
11375 capture_token->location);
11377 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11379 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
11380 cp_lexer_consume_token (parser->lexer);
11381 capture_init_expr = make_pack_expansion (capture_init_expr);
11382 if (init_pack_expansion)
11384 /* If what follows is an initializer, the second '...' is
11385 invalid. But for cases like [...xs...], the first one
11386 is invalid. */
11387 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
11388 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
11389 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11390 ellipsis_loc = loc;
11391 error_at (ellipsis_loc, "too many %<...%> in lambda capture");
11392 continue;
11397 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11398 && !explicit_init_p)
11400 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
11401 && capture_kind == BY_COPY)
11402 pedwarn (capture_token->location, 0, "explicit by-copy capture "
11403 "of %qD redundant with by-copy capture default",
11404 capture_id);
11405 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
11406 && capture_kind == BY_REFERENCE)
11407 pedwarn (capture_token->location, 0, "explicit by-reference "
11408 "capture of %qD redundant with by-reference capture "
11409 "default", capture_id);
11412 /* Check for duplicates.
11413 Optimize for the zero or one explicit captures cases and only create
11414 the hash_set after adding second capture. */
11415 bool found = false;
11416 if (!ids.is_empty ())
11417 found = ids.add (capture_id);
11418 else if (first_capture_id == NULL_TREE)
11419 first_capture_id = capture_id;
11420 else if (capture_id == first_capture_id)
11421 found = true;
11422 else
11424 ids.add (first_capture_id);
11425 ids.add (capture_id);
11427 if (found)
11428 pedwarn (input_location, 0,
11429 "already captured %qD in lambda expression", capture_id);
11430 else
11431 add_capture (lambda_expr, capture_id, capture_init_expr,
11432 /*by_reference_p=*/capture_kind == BY_REFERENCE,
11433 explicit_init_p);
11435 /* If there is any qualification still in effect, clear it
11436 now; we will be starting fresh with the next capture. */
11437 parser->scope = NULL_TREE;
11438 parser->qualifying_scope = NULL_TREE;
11439 parser->object_scope = NULL_TREE;
11442 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11445 /* Parse the (optional) middle of a lambda expression.
11447 lambda-declarator:
11448 ( parameter-declaration-clause ) lambda-specifiers requires-clause [opt]
11449 lambda-specifiers (C++23)
11451 lambda-specifiers:
11452 decl-specifier-seq [opt] noexcept-specifier [opt]
11453 attribute-specifier-seq [opt] trailing-return-type [opt]
11455 LAMBDA_EXPR is the current representation of the lambda expression. */
11457 static bool
11458 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
11460 /* 5.1.1.4 of the standard says:
11461 If a lambda-expression does not include a lambda-declarator, it is as if
11462 the lambda-declarator were ().
11463 This means an empty parameter list, no attributes, and no exception
11464 specification. */
11465 tree param_list = void_list_node;
11466 tree std_attrs = NULL_TREE;
11467 tree gnu_attrs = NULL_TREE;
11468 tree exception_spec = NULL_TREE;
11469 tree template_param_list = NULL_TREE;
11470 tree tx_qual = NULL_TREE;
11471 tree return_type = NULL_TREE;
11472 tree trailing_requires_clause = NULL_TREE;
11473 bool has_param_list = false;
11474 location_t omitted_parms_loc = UNKNOWN_LOCATION;
11475 cp_decl_specifier_seq lambda_specs;
11476 clear_decl_specs (&lambda_specs);
11477 /* A lambda op() is const unless explicitly 'mutable'. */
11478 cp_cv_quals quals = TYPE_QUAL_CONST;
11480 /* The template-parameter-list is optional, but must begin with
11481 an opening angle if present. */
11482 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
11484 if (cxx_dialect < cxx14)
11485 pedwarn (parser->lexer->next_token->location, OPT_Wc__14_extensions,
11486 "lambda templates are only available with "
11487 "%<-std=c++14%> or %<-std=gnu++14%>");
11488 else if (pedantic && cxx_dialect < cxx20)
11489 pedwarn (parser->lexer->next_token->location, OPT_Wc__20_extensions,
11490 "lambda templates are only available with "
11491 "%<-std=c++20%> or %<-std=gnu++20%>");
11493 cp_lexer_consume_token (parser->lexer);
11495 template_param_list = cp_parser_template_parameter_list (parser);
11496 cp_parser_require_end_of_template_parameter_list (parser);
11498 /* We may have a constrained generic lambda; parse the requires-clause
11499 immediately after the template-parameter-list and combine with any
11500 shorthand constraints present. */
11501 tree dreqs = cp_parser_requires_clause_opt (parser, true);
11502 if (flag_concepts)
11504 tree reqs = get_shorthand_constraints (current_template_parms);
11505 if (dreqs)
11506 reqs = combine_constraint_expressions (reqs, dreqs);
11507 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
11510 /* We just processed one more parameter list. */
11511 ++parser->num_template_parameter_lists;
11514 /* Committee discussion supports allowing attributes here. */
11515 lambda_specs.attributes = cp_parser_attributes_opt (parser);
11517 /* The parameter-declaration-clause is optional (unless
11518 template-parameter-list was given), but must begin with an
11519 opening parenthesis if present. */
11520 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11522 matching_parens parens;
11523 parens.consume_open (parser);
11525 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
11527 /* Parse parameters. */
11528 param_list
11529 = cp_parser_parameter_declaration_clause
11530 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL);
11532 /* Default arguments shall not be specified in the
11533 parameter-declaration-clause of a lambda-declarator. */
11534 if (pedantic && cxx_dialect < cxx14)
11535 for (tree t = param_list; t; t = TREE_CHAIN (t))
11536 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
11537 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
11538 OPT_Wc__14_extensions,
11539 "default argument specified for lambda parameter");
11541 parens.require_close (parser);
11542 has_param_list = true;
11544 else if (cxx_dialect < cxx23)
11545 omitted_parms_loc = cp_lexer_peek_token (parser->lexer)->location;
11547 /* In the decl-specifier-seq of the lambda-declarator, each
11548 decl-specifier shall either be mutable or constexpr. */
11549 int declares_class_or_enum;
11550 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
11551 cp_parser_decl_specifier_seq (parser,
11552 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
11553 &lambda_specs, &declares_class_or_enum);
11555 if (omitted_parms_loc && lambda_specs.any_specifiers_p)
11557 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11558 "parameter declaration before lambda declaration "
11559 "specifiers only optional with %<-std=c++2b%> or "
11560 "%<-std=gnu++2b%>");
11561 omitted_parms_loc = UNKNOWN_LOCATION;
11564 if (lambda_specs.storage_class == sc_mutable)
11566 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
11567 quals = TYPE_UNQUALIFIED;
11569 else if (lambda_specs.storage_class == sc_static)
11571 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
11572 || LAMBDA_EXPR_CAPTURE_LIST (lambda_expr))
11573 error_at (lambda_specs.locations[ds_storage_class],
11574 "%<static%> lambda specifier with lambda capture");
11575 else
11577 LAMBDA_EXPR_STATIC_P (lambda_expr) = 1;
11578 quals = TYPE_UNQUALIFIED;
11582 tx_qual = cp_parser_tx_qualifier_opt (parser);
11583 if (omitted_parms_loc && tx_qual)
11585 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11586 "parameter declaration before lambda transaction "
11587 "qualifier only optional with %<-std=c++2b%> or "
11588 "%<-std=gnu++2b%>");
11589 omitted_parms_loc = UNKNOWN_LOCATION;
11592 /* Parse optional exception specification. */
11593 exception_spec
11594 = cp_parser_exception_specification_opt (parser, CP_PARSER_FLAGS_NONE);
11596 if (omitted_parms_loc && exception_spec)
11598 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11599 "parameter declaration before lambda exception "
11600 "specification only optional with %<-std=c++2b%> or "
11601 "%<-std=gnu++2b%>");
11602 omitted_parms_loc = UNKNOWN_LOCATION;
11605 /* GCC 8 accepted attributes here, and this is the place for standard C++11
11606 attributes that appertain to the function type. */
11607 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11608 gnu_attrs = cp_parser_gnu_attributes_opt (parser);
11609 else
11610 std_attrs = cp_parser_std_attribute_spec_seq (parser);
11612 /* Parse optional trailing return type. */
11613 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
11615 if (omitted_parms_loc)
11616 pedwarn (omitted_parms_loc, OPT_Wc__23_extensions,
11617 "parameter declaration before lambda trailing "
11618 "return type only optional with %<-std=c++2b%> or "
11619 "%<-std=gnu++2b%>");
11620 cp_lexer_consume_token (parser->lexer);
11621 return_type = cp_parser_trailing_type_id (parser);
11624 /* Also allow GNU attributes at the very end of the declaration, the usual
11625 place for GNU attributes. */
11626 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
11627 gnu_attrs = chainon (gnu_attrs, cp_parser_gnu_attributes_opt (parser));
11629 if (has_param_list)
11631 /* Parse optional trailing requires clause. */
11632 trailing_requires_clause = cp_parser_requires_clause_opt (parser, false);
11634 /* The function parameters must be in scope all the way until after the
11635 trailing-return-type in case of decltype. */
11636 pop_bindings_and_leave_scope ();
11639 /* Create the function call operator.
11641 Messing with declarators like this is no uglier than building up the
11642 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11643 other code. */
11645 cp_decl_specifier_seq return_type_specs;
11646 cp_declarator* declarator;
11647 tree fco;
11648 void *p;
11650 clear_decl_specs (&return_type_specs);
11651 return_type_specs.type = make_auto ();
11653 if (lambda_specs.locations[ds_constexpr])
11655 if (cxx_dialect >= cxx17)
11656 return_type_specs.locations[ds_constexpr]
11657 = lambda_specs.locations[ds_constexpr];
11658 else
11659 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
11660 "lambda only available with %<-std=c++17%> or "
11661 "%<-std=gnu++17%>");
11663 if (lambda_specs.locations[ds_consteval])
11664 return_type_specs.locations[ds_consteval]
11665 = lambda_specs.locations[ds_consteval];
11666 if (LAMBDA_EXPR_STATIC_P (lambda_expr))
11668 return_type_specs.storage_class = sc_static;
11669 return_type_specs.locations[ds_storage_class]
11670 = lambda_specs.locations[ds_storage_class];
11673 p = obstack_alloc (&declarator_obstack, 0);
11675 declarator = make_id_declarator (NULL_TREE, call_op_identifier, sfk_none,
11676 LAMBDA_EXPR_LOCATION (lambda_expr));
11678 declarator = make_call_declarator (declarator, param_list, quals,
11679 VIRT_SPEC_UNSPECIFIED,
11680 REF_QUAL_NONE,
11681 tx_qual,
11682 exception_spec,
11683 return_type,
11684 trailing_requires_clause,
11685 UNKNOWN_LOCATION);
11686 declarator->std_attributes = std_attrs;
11688 fco = grokmethod (&return_type_specs,
11689 declarator,
11690 chainon (gnu_attrs, lambda_specs.attributes));
11691 if (fco != error_mark_node)
11693 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
11694 DECL_ARTIFICIAL (fco) = 1;
11695 if (!LAMBDA_EXPR_STATIC_P (lambda_expr))
11696 /* Give the object parameter a different name. */
11697 DECL_NAME (DECL_ARGUMENTS (fco)) = closure_identifier;
11698 DECL_SET_LAMBDA_FUNCTION (fco, true);
11700 if (template_param_list)
11702 fco = finish_member_template_decl (fco);
11703 finish_template_decl (template_param_list);
11704 --parser->num_template_parameter_lists;
11706 else if (parser->fully_implicit_function_template_p)
11707 fco = finish_fully_implicit_template (parser, fco);
11709 finish_member_declaration (fco);
11711 obstack_free (&declarator_obstack, p);
11713 return (fco != error_mark_node);
11717 /* Parse the body of a lambda expression, which is simply
11719 compound-statement
11721 but which requires special handling.
11722 LAMBDA_EXPR is the current representation of the lambda expression. */
11724 static void
11725 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
11727 bool nested = (current_function_decl != NULL_TREE);
11728 unsigned char local_variables_forbidden_p
11729 = parser->local_variables_forbidden_p;
11730 bool in_function_body = parser->in_function_body;
11732 /* The body of a lambda-expression is not a subexpression of the enclosing
11733 expression. */
11734 cp_evaluated ev;
11736 if (nested)
11737 push_function_context ();
11738 else
11739 /* Still increment function_depth so that we don't GC in the
11740 middle of an expression. */
11741 ++function_depth;
11743 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
11744 auto ord = make_temp_override (parser->oacc_routine, NULL);
11745 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
11746 vec<tree> omp_privatization_save;
11747 save_omp_privatization_clauses (omp_privatization_save);
11748 /* Clear this in case we're in the middle of a default argument. */
11749 parser->local_variables_forbidden_p = 0;
11750 parser->in_function_body = true;
11753 local_specialization_stack s (lss_copy);
11754 tree fco = lambda_function (lambda_expr);
11755 tree body = start_lambda_function (fco, lambda_expr);
11757 /* Originally C++11 required us to peek for 'return expr'; and
11758 process it specially here to deduce the return type. N3638
11759 removed the need for that. */
11760 cp_parser_function_body (parser, false);
11762 finish_lambda_function (body);
11765 restore_omp_privatization_clauses (omp_privatization_save);
11766 parser->local_variables_forbidden_p = local_variables_forbidden_p;
11767 parser->in_function_body = in_function_body;
11768 if (nested)
11769 pop_function_context();
11770 else
11771 --function_depth;
11774 /* Statements [gram.stmt.stmt] */
11776 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11778 static void
11779 add_debug_begin_stmt (location_t loc)
11781 if (!MAY_HAVE_DEBUG_MARKER_STMTS)
11782 return;
11783 if (DECL_DECLARED_CONCEPT_P (current_function_decl))
11784 /* A concept is never expanded normally. */
11785 return;
11787 tree stmt = build0 (DEBUG_BEGIN_STMT, void_type_node);
11788 SET_EXPR_LOCATION (stmt, loc);
11789 add_stmt (stmt);
11792 struct cp_omp_attribute_data
11794 cp_token_cache *tokens;
11795 const c_omp_directive *dir;
11796 c_omp_directive_kind kind;
11799 /* Handle omp::directive and omp::sequence attributes in ATTRS
11800 (if any) at the start of a statement or in attribute-declaration. */
11802 static tree
11803 cp_parser_handle_statement_omp_attributes (cp_parser *parser, tree attrs)
11805 if (!flag_openmp && !flag_openmp_simd)
11806 return attrs;
11808 auto_vec<cp_omp_attribute_data, 16> vec;
11809 int cnt = 0;
11810 int tokens = 0;
11811 bool bad = false;
11812 for (tree *pa = &attrs; *pa; )
11813 if (get_attribute_namespace (*pa) == omp_identifier
11814 && is_attribute_p ("directive", get_attribute_name (*pa)))
11816 cnt++;
11817 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
11819 tree d = TREE_VALUE (a);
11820 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
11821 cp_token *first = DEFPARSE_TOKENS (d)->first;
11822 cp_token *last = DEFPARSE_TOKENS (d)->last;
11823 if (parser->omp_attrs_forbidden_p)
11825 error_at (first->location,
11826 "mixing OpenMP directives with attribute and pragma "
11827 "syntax on the same statement");
11828 parser->omp_attrs_forbidden_p = false;
11829 bad = true;
11831 const char *directive[3] = {};
11832 for (int i = 0; i < 3; i++)
11834 tree id = NULL_TREE;
11835 if (first + i == last)
11836 break;
11837 if (first[i].type == CPP_NAME)
11838 id = first[i].u.value;
11839 else if (first[i].type == CPP_KEYWORD)
11840 id = ridpointers[(int) first[i].keyword];
11841 else
11842 break;
11843 directive[i] = IDENTIFIER_POINTER (id);
11845 const c_omp_directive *dir = NULL;
11846 if (directive[0])
11847 dir = c_omp_categorize_directive (directive[0], directive[1],
11848 directive[2]);
11849 if (dir == NULL)
11851 error_at (first->location,
11852 "unknown OpenMP directive name in %<omp::directive%>"
11853 " attribute argument");
11854 continue;
11856 c_omp_directive_kind kind = dir->kind;
11857 if (dir->id == PRAGMA_OMP_ORDERED)
11859 /* ordered is C_OMP_DIR_CONSTRUCT only if it doesn't contain
11860 depend/doacross clause. */
11861 if (directive[1]
11862 && (strcmp (directive[1], "depend") == 0
11863 || strcmp (directive[1], "doacross") == 0))
11864 kind = C_OMP_DIR_STANDALONE;
11865 else if (first + 2 < last
11866 && first[1].type == CPP_COMMA
11867 && first[2].type == CPP_NAME
11868 && (strcmp (IDENTIFIER_POINTER (first[2].u.value),
11869 "depend") == 0
11870 || strcmp (IDENTIFIER_POINTER (first[2].u.value),
11871 "doacross") == 0))
11872 kind = C_OMP_DIR_STANDALONE;
11874 else if (dir->id == PRAGMA_OMP_ERROR)
11876 /* error with at(execution) clause is C_OMP_DIR_STANDALONE. */
11877 int paren_depth = 0;
11878 for (int i = 1; first + i < last; i++)
11879 if (first[i].type == CPP_OPEN_PAREN)
11880 paren_depth++;
11881 else if (first[i].type == CPP_CLOSE_PAREN)
11882 paren_depth--;
11883 else if (paren_depth == 0
11884 && first + i + 2 < last
11885 && first[i].type == CPP_NAME
11886 && first[i + 1].type == CPP_OPEN_PAREN
11887 && first[i + 2].type == CPP_NAME
11888 && !strcmp (IDENTIFIER_POINTER (first[i].u.value),
11889 "at")
11890 && !strcmp (IDENTIFIER_POINTER (first[i
11891 + 2].u.value),
11892 "execution"))
11894 kind = C_OMP_DIR_STANDALONE;
11895 break;
11898 cp_omp_attribute_data v = { DEFPARSE_TOKENS (d), dir, kind };
11899 vec.safe_push (v);
11900 if (flag_openmp || dir->simd)
11901 tokens += (last - first) + 1;
11903 cp_omp_attribute_data v = {};
11904 vec.safe_push (v);
11905 *pa = TREE_CHAIN (*pa);
11907 else
11908 pa = &TREE_CHAIN (*pa);
11910 if (bad)
11911 return attrs;
11913 unsigned int i;
11914 cp_omp_attribute_data *v;
11915 cp_omp_attribute_data *construct_seen = nullptr;
11916 cp_omp_attribute_data *standalone_seen = nullptr;
11917 cp_omp_attribute_data *prev_standalone_seen = nullptr;
11918 FOR_EACH_VEC_ELT (vec, i, v)
11919 if (v->tokens)
11921 if (v->kind == C_OMP_DIR_CONSTRUCT && !construct_seen)
11922 construct_seen = v;
11923 else if (v->kind == C_OMP_DIR_STANDALONE && !standalone_seen)
11924 standalone_seen = v;
11926 else
11928 if (standalone_seen && !prev_standalone_seen)
11930 prev_standalone_seen = standalone_seen;
11931 standalone_seen = nullptr;
11935 if (cnt > 1 && construct_seen)
11937 error_at (construct_seen->tokens->first->location,
11938 "OpenMP construct among %<omp::directive%> attributes"
11939 " requires all %<omp::directive%> attributes on the"
11940 " same statement to be in the same %<omp::sequence%>");
11941 return attrs;
11943 if (cnt > 1 && standalone_seen && prev_standalone_seen)
11945 error_at (standalone_seen->tokens->first->location,
11946 "multiple OpenMP standalone directives among"
11947 " %<omp::directive%> attributes must be all within the"
11948 " same %<omp::sequence%>");
11949 return attrs;
11952 if (prev_standalone_seen)
11953 standalone_seen = prev_standalone_seen;
11954 if (standalone_seen
11955 && !cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11957 error_at (standalone_seen->tokens->first->location,
11958 "standalone OpenMP directives in %<omp::directive%> attribute"
11959 " can only appear on an empty statement");
11960 return attrs;
11962 if (cnt && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
11964 cp_token *token = cp_lexer_peek_token (parser->lexer);
11965 enum pragma_kind kind = cp_parser_pragma_kind (token);
11966 if (kind >= PRAGMA_OMP__START_ && kind <= PRAGMA_OMP__LAST_)
11968 error_at (token->location,
11969 "mixing OpenMP directives with attribute and pragma "
11970 "syntax on the same statement");
11971 return attrs;
11975 if (!tokens)
11976 return attrs;
11977 tokens++;
11978 cp_lexer *lexer = cp_lexer_alloc ();
11979 lexer->debugging_p = parser->lexer->debugging_p;
11980 vec_safe_reserve (lexer->buffer, tokens, true);
11981 FOR_EACH_VEC_ELT (vec, i, v)
11983 if (!v->tokens)
11984 continue;
11985 if (!flag_openmp && !v->dir->simd)
11986 continue;
11987 cp_token *first = v->tokens->first;
11988 cp_token *last = v->tokens->last;
11989 cp_token tok = {};
11990 tok.type = CPP_PRAGMA;
11991 tok.keyword = RID_MAX;
11992 tok.u.value = build_int_cst (NULL, v->dir->id);
11993 tok.location = first->location;
11994 lexer->buffer->quick_push (tok);
11995 while (++first < last)
11996 lexer->buffer->quick_push (*first);
11997 tok = {};
11998 tok.type = CPP_PRAGMA_EOL;
11999 tok.keyword = RID_MAX;
12000 tok.location = last->location;
12001 lexer->buffer->quick_push (tok);
12003 cp_token tok = {};
12004 tok.type = CPP_EOF;
12005 tok.keyword = RID_MAX;
12006 tok.location = lexer->buffer->last ().location;
12007 lexer->buffer->quick_push (tok);
12008 lexer->next = parser->lexer;
12009 lexer->next_token = lexer->buffer->address ();
12010 lexer->last_token = lexer->next_token
12011 + lexer->buffer->length ()
12012 - 1;
12013 lexer->in_omp_attribute_pragma = true;
12014 parser->lexer = lexer;
12015 /* Move the current source position to that of the first token in the
12016 new lexer. */
12017 cp_lexer_set_source_position_from_token (lexer->next_token);
12018 return attrs;
12021 /* Handle omp::directive and omp::sequence attributes in *PATTRS
12022 (if any) at the start or after declaration-id of a declaration. */
12024 static void
12025 cp_parser_handle_directive_omp_attributes (cp_parser *parser, tree *pattrs,
12026 cp_omp_declare_simd_data *data,
12027 bool start)
12029 if (!flag_openmp && !flag_openmp_simd)
12030 return;
12032 int cnt = 0;
12033 bool bad = false;
12034 bool variant_p = false;
12035 location_t loc = UNKNOWN_LOCATION;
12036 for (tree pa = *pattrs; pa; pa = TREE_CHAIN (pa))
12037 if (get_attribute_namespace (pa) == omp_identifier
12038 && is_attribute_p ("directive", get_attribute_name (pa)))
12040 for (tree a = TREE_VALUE (pa); a; a = TREE_CHAIN (a))
12042 tree d = TREE_VALUE (a);
12043 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
12044 cp_token *first = DEFPARSE_TOKENS (d)->first;
12045 cp_token *last = DEFPARSE_TOKENS (d)->last;
12046 const char *directive[3] = {};
12047 for (int i = 0; i < 3; i++)
12049 tree id = NULL_TREE;
12050 if (first + i == last)
12051 break;
12052 if (first[i].type == CPP_NAME)
12053 id = first[i].u.value;
12054 else if (first[i].type == CPP_KEYWORD)
12055 id = ridpointers[(int) first[i].keyword];
12056 else
12057 break;
12058 directive[i] = IDENTIFIER_POINTER (id);
12060 const c_omp_directive *dir = NULL;
12061 if (directive[0])
12062 dir = c_omp_categorize_directive (directive[0], directive[1],
12063 directive[2]);
12064 if (dir == NULL)
12065 continue;
12066 if (dir->id == PRAGMA_OMP_DECLARE
12067 && (strcmp (directive[1], "simd") == 0
12068 || strcmp (directive[1], "variant") == 0))
12070 if (cnt++ == 0)
12072 variant_p = strcmp (directive[1], "variant") == 0;
12073 loc = first->location;
12075 if (start && parser->omp_declare_simd && !bad)
12077 error_at (first->location,
12078 "mixing OpenMP directives with attribute and "
12079 "pragma syntax on the same declaration");
12080 bad = true;
12086 if (bad)
12088 for (tree *pa = pattrs; *pa; )
12089 if (get_attribute_namespace (*pa) == omp_identifier
12090 && is_attribute_p ("directive", get_attribute_name (*pa)))
12091 *pa = TREE_CHAIN (*pa);
12092 else
12093 pa = &TREE_CHAIN (*pa);
12094 return;
12096 if (cnt == 0)
12097 return;
12099 if (parser->omp_declare_simd == NULL)
12101 data->error_seen = false;
12102 data->fndecl_seen = false;
12103 data->variant_p = variant_p;
12104 data->loc = loc;
12105 data->tokens = vNULL;
12106 data->attribs[0] = NULL;
12107 data->attribs[1] = NULL;
12108 parser->omp_declare_simd = data;
12110 parser->omp_declare_simd->attribs[!start] = pattrs;
12113 /* Parse a statement.
12115 statement:
12116 labeled-statement
12117 expression-statement
12118 compound-statement
12119 selection-statement
12120 iteration-statement
12121 jump-statement
12122 declaration-statement
12123 try-block
12125 C++11:
12127 statement:
12128 labeled-statement
12129 attribute-specifier-seq (opt) expression-statement
12130 attribute-specifier-seq (opt) compound-statement
12131 attribute-specifier-seq (opt) selection-statement
12132 attribute-specifier-seq (opt) iteration-statement
12133 attribute-specifier-seq (opt) jump-statement
12134 declaration-statement
12135 attribute-specifier-seq (opt) try-block
12137 init-statement:
12138 expression-statement
12139 simple-declaration
12140 alias-declaration
12142 TM Extension:
12144 statement:
12145 atomic-statement
12147 IN_COMPOUND is true when the statement is nested inside a
12148 cp_parser_compound_statement.
12150 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12151 is a (possibly labeled) if statement which is not enclosed in braces
12152 and has an else clause. This is used to implement -Wparentheses.
12154 CHAIN is a vector of if-else-if conditions. */
12156 static void
12157 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
12158 const bool in_compound, bool *if_p, vec<tree> *chain,
12159 location_t *loc_after_labels)
12161 tree statement, std_attrs = NULL_TREE;
12162 cp_token *token;
12163 location_t statement_location, attrs_loc;
12164 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12165 bool has_std_attrs;
12166 /* A copy of IN_COMPOUND which is set to false after seeing a label.
12167 This matters for certain pragmas. */
12168 bool in_compound_for_pragma = in_compound;
12170 restart:
12171 if (if_p != NULL)
12172 *if_p = false;
12173 /* There is no statement yet. */
12174 statement = NULL_TREE;
12176 saved_token_sentinel saved_tokens (parser->lexer);
12177 token = cp_lexer_peek_token (parser->lexer);
12178 attrs_loc = token->location;
12179 if (c_dialect_objc ())
12180 /* In obj-c++, seeing '[[' might be the either the beginning of
12181 c++11 attributes, or a nested objc-message-expression. So
12182 let's parse the c++11 attributes tentatively. */
12183 cp_parser_parse_tentatively (parser);
12184 std_attrs = cp_parser_std_attribute_spec_seq (parser);
12185 if (std_attrs)
12186 attrs_loc = make_location (attrs_loc, attrs_loc, parser->lexer);
12187 if (c_dialect_objc ())
12189 if (!cp_parser_parse_definitely (parser))
12190 std_attrs = NULL_TREE;
12192 has_std_attrs = cp_lexer_peek_token (parser->lexer) != token;
12194 /* Peek at the next token. */
12195 token = cp_lexer_peek_token (parser->lexer);
12196 bool omp_attrs_forbidden_p;
12197 omp_attrs_forbidden_p = parser->omp_attrs_forbidden_p;
12199 if (std_attrs && (flag_openmp || flag_openmp_simd))
12201 bool handle_omp_attribs = false;
12202 if (token->type == CPP_KEYWORD)
12203 switch (token->keyword)
12205 case RID_IF:
12206 case RID_SWITCH:
12207 case RID_WHILE:
12208 case RID_DO:
12209 case RID_FOR:
12210 case RID_BREAK:
12211 case RID_CONTINUE:
12212 case RID_RETURN:
12213 case RID_CO_RETURN:
12214 case RID_GOTO:
12215 case RID_AT_TRY:
12216 case RID_AT_CATCH:
12217 case RID_AT_FINALLY:
12218 case RID_AT_SYNCHRONIZED:
12219 case RID_AT_THROW:
12220 case RID_TRY:
12221 case RID_TRANSACTION_ATOMIC:
12222 case RID_TRANSACTION_RELAXED:
12223 case RID_SYNCHRONIZED:
12224 case RID_ATOMIC_NOEXCEPT:
12225 case RID_ATOMIC_CANCEL:
12226 case RID_TRANSACTION_CANCEL:
12227 handle_omp_attribs = true;
12228 break;
12229 default:
12230 break;
12232 else if (token->type == CPP_SEMICOLON
12233 || token->type == CPP_OPEN_BRACE
12234 || token->type == CPP_PRAGMA)
12235 handle_omp_attribs = true;
12236 if (handle_omp_attribs)
12238 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
12239 std_attrs);
12240 token = cp_lexer_peek_token (parser->lexer);
12243 parser->omp_attrs_forbidden_p = false;
12245 /* Remember the location of the first token in the statement. */
12246 cp_token *statement_token = token;
12247 statement_location = token->location;
12248 add_debug_begin_stmt (statement_location);
12249 /* If this is a keyword, then that will often determine what kind of
12250 statement we have. */
12251 if (token->type == CPP_KEYWORD)
12253 enum rid keyword = token->keyword;
12255 switch (keyword)
12257 case RID_CASE:
12258 case RID_DEFAULT:
12259 /* Looks like a labeled-statement with a case label.
12260 Parse the label, and then use tail recursion to parse
12261 the statement. */
12262 cp_parser_label_for_labeled_statement (parser, std_attrs);
12263 in_compound_for_pragma = false;
12264 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12265 goto restart;
12267 case RID_IF:
12268 case RID_SWITCH:
12269 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12270 statement = cp_parser_selection_statement (parser, if_p, chain);
12271 break;
12273 case RID_WHILE:
12274 case RID_DO:
12275 case RID_FOR:
12276 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12277 statement = cp_parser_iteration_statement (parser, if_p, false, 0);
12278 break;
12280 case RID_BREAK:
12281 case RID_CONTINUE:
12282 case RID_RETURN:
12283 case RID_CO_RETURN:
12284 case RID_GOTO:
12285 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12286 statement = cp_parser_jump_statement (parser);
12287 break;
12289 /* Objective-C++ exception-handling constructs. */
12290 case RID_AT_TRY:
12291 case RID_AT_CATCH:
12292 case RID_AT_FINALLY:
12293 case RID_AT_SYNCHRONIZED:
12294 case RID_AT_THROW:
12295 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12296 statement = cp_parser_objc_statement (parser);
12297 break;
12299 case RID_TRY:
12300 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12301 statement = cp_parser_try_block (parser);
12302 break;
12304 case RID_NAMESPACE:
12305 /* This must be a namespace alias definition. */
12306 if (has_std_attrs)
12308 /* Attributes should be parsed as part of the
12309 declaration, so let's un-parse them. */
12310 saved_tokens.rollback();
12311 std_attrs = NULL_TREE;
12313 cp_parser_declaration_statement (parser);
12314 return;
12316 case RID_TRANSACTION_ATOMIC:
12317 case RID_TRANSACTION_RELAXED:
12318 case RID_SYNCHRONIZED:
12319 case RID_ATOMIC_NOEXCEPT:
12320 case RID_ATOMIC_CANCEL:
12321 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12322 statement = cp_parser_transaction (parser, token);
12323 break;
12324 case RID_TRANSACTION_CANCEL:
12325 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12326 statement = cp_parser_transaction_cancel (parser);
12327 break;
12329 default:
12330 /* It might be a keyword like `int' that can start a
12331 declaration-statement. */
12332 break;
12335 else if (token->type == CPP_NAME)
12337 /* If the next token is a `:', then we are looking at a
12338 labeled-statement. */
12339 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12340 if (token->type == CPP_COLON)
12342 /* Looks like a labeled-statement with an ordinary label.
12343 Parse the label, and then use tail recursion to parse
12344 the statement. */
12346 cp_parser_label_for_labeled_statement (parser, std_attrs);
12348 /* If there's no statement, it's not a labeled-statement, just
12349 a label. That's allowed in C++23, but only if we're at the
12350 end of a compound-statement. */
12351 if (in_compound
12352 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12354 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12355 if (cxx_dialect < cxx23)
12356 pedwarn (loc, OPT_Wc__23_extensions,
12357 "label at end of compound statement only available "
12358 "with %<-std=c++2b%> or %<-std=gnu++2b%>");
12359 return;
12361 in_compound_for_pragma = false;
12362 in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
12363 goto restart;
12366 /* Anything that starts with a `{' must be a compound-statement. */
12367 else if (token->type == CPP_OPEN_BRACE)
12369 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12370 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12372 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
12373 a statement all its own. */
12374 else if (token->type == CPP_PRAGMA)
12376 do_pragma:;
12377 cp_lexer *lexer = parser->lexer;
12378 bool do_restart = false;
12379 /* Only certain OpenMP pragmas are attached to statements, and thus
12380 are considered statements themselves. All others are not. In
12381 the context of a compound, accept the pragma as a "statement" and
12382 return so that we can check for a close brace. Otherwise we
12383 require a real statement and must go back and read one. */
12384 if (in_compound_for_pragma)
12385 cp_parser_pragma (parser, pragma_compound, if_p);
12386 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
12387 do_restart = true;
12388 if (parser->lexer != lexer
12389 && lexer->in_omp_attribute_pragma
12390 && (!in_omp_attribute_pragma || lexer->orphan_p))
12392 if (saved_tokens.lexer == lexer)
12394 if (saved_tokens.mode == STS_COMMIT)
12395 cp_lexer_commit_tokens (lexer);
12396 gcc_assert (lexer->saved_tokens.length () == saved_tokens.len);
12397 saved_tokens.lexer = parser->lexer;
12398 saved_tokens.mode = STS_DONOTHING;
12399 saved_tokens.len = parser->lexer->saved_tokens.length ();
12401 cp_lexer_destroy (lexer);
12402 lexer = parser->lexer;
12404 if (do_restart)
12405 goto restart;
12406 if (parser->lexer == lexer
12407 && lexer->in_omp_attribute_pragma
12408 && !in_omp_attribute_pragma)
12409 parser->lexer->orphan_p = true;
12410 return;
12412 else if (token->type == CPP_EOF)
12414 cp_parser_error (parser, "expected statement");
12415 return;
12418 /* Everything else must be a declaration-statement or an
12419 expression-statement. Try for the declaration-statement
12420 first, unless we are looking at a `;', in which case we know that
12421 we have an expression-statement. */
12422 if (!statement)
12424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12426 if (has_std_attrs)
12427 /* Attributes should be parsed as part of the declaration,
12428 so let's un-parse them. */
12429 saved_tokens.rollback();
12431 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12432 cp_parser_parse_tentatively (parser);
12433 /* Try to parse the declaration-statement. */
12434 cp_parser_declaration_statement (parser);
12435 parser->omp_attrs_forbidden_p = false;
12436 /* If that worked, we're done. */
12437 if (cp_parser_parse_definitely (parser))
12438 return;
12439 /* It didn't work, restore the post-attribute position. */
12440 if (has_std_attrs)
12442 cp_lexer_set_token_position (parser->lexer, statement_token);
12443 if (flag_openmp || flag_openmp_simd)
12445 size_t i = 1;
12446 bool handle_omp_attribs = true;
12447 while (cp_lexer_peek_nth_token (parser->lexer, i)->keyword
12448 == RID_EXTENSION)
12449 i++;
12450 switch (cp_lexer_peek_nth_token (parser->lexer, i)->keyword)
12452 case RID_ASM:
12453 case RID_NAMESPACE:
12454 case RID_USING:
12455 case RID_LABEL:
12456 case RID_STATIC_ASSERT:
12457 /* Don't handle OpenMP attribs on keywords that
12458 always start a declaration statement but don't
12459 accept attribute before it and therefore
12460 the tentative cp_parser_declaration_statement
12461 fails to parse because of that. */
12462 handle_omp_attribs = false;
12463 break;
12464 default:
12465 break;
12468 if (handle_omp_attribs)
12470 parser->omp_attrs_forbidden_p = omp_attrs_forbidden_p;
12471 std_attrs
12472 = cp_parser_handle_statement_omp_attributes
12473 (parser, std_attrs);
12474 parser->omp_attrs_forbidden_p = false;
12475 token = cp_lexer_peek_token (parser->lexer);
12476 if (token->type == CPP_PRAGMA)
12477 goto do_pragma;
12482 /* All preceding labels have been parsed at this point. */
12483 if (loc_after_labels != NULL)
12484 *loc_after_labels = statement_location;
12486 std_attrs = process_stmt_hotness_attribute (std_attrs, attrs_loc);
12488 /* Look for an expression-statement instead. */
12489 statement = cp_parser_expression_statement (parser, in_statement_expr);
12491 std_attrs = process_stmt_assume_attribute (std_attrs, statement,
12492 attrs_loc);
12494 /* Handle [[fallthrough]];. */
12495 if (attribute_fallthrough_p (std_attrs))
12497 /* The next token after the fallthrough attribute is ';'. */
12498 if (statement == NULL_TREE)
12500 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12501 statement = build_call_expr_internal_loc (statement_location,
12502 IFN_FALLTHROUGH,
12503 void_type_node, 0);
12504 finish_expr_stmt (statement);
12506 else
12507 warning_at (statement_location, OPT_Wattributes,
12508 "%<fallthrough%> attribute not followed by %<;%>");
12509 std_attrs = NULL_TREE;
12513 /* Set the line number for the statement. */
12514 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
12515 SET_EXPR_LOCATION (statement, statement_location);
12517 /* Allow "[[fallthrough]];" or "[[assume(cond)]];", but warn otherwise. */
12518 if (std_attrs != NULL_TREE)
12519 warning_at (attrs_loc,
12520 OPT_Wattributes,
12521 "attributes at the beginning of statement are ignored");
12524 /* Append ATTR to attribute list ATTRS. */
12526 tree
12527 attr_chainon (tree attrs, tree attr)
12529 if (attrs == error_mark_node)
12530 return error_mark_node;
12531 if (attr == error_mark_node)
12532 return error_mark_node;
12533 return chainon (attrs, attr);
12536 /* Parse the label for a labeled-statement, i.e.
12538 label:
12539 attribute-specifier-seq[opt] identifier :
12540 attribute-specifier-seq[opt] case constant-expression :
12541 attribute-specifier-seq[opt] default :
12543 labeled-statement:
12544 label statement
12546 GNU Extension:
12547 case constant-expression ... constant-expression : statement
12549 When a label is parsed without errors, the label is added to the
12550 parse tree by the finish_* functions, so this function doesn't
12551 have to return the label. */
12553 static void
12554 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
12556 cp_token *token;
12557 tree label = NULL_TREE;
12558 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12560 /* The next token should be an identifier. */
12561 token = cp_lexer_peek_token (parser->lexer);
12562 if (token->type != CPP_NAME
12563 && token->type != CPP_KEYWORD)
12565 cp_parser_error (parser, "expected labeled-statement");
12566 return;
12569 /* Remember whether this case or a user-defined label is allowed to fall
12570 through to. */
12571 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
12573 parser->colon_corrects_to_scope_p = false;
12574 switch (token->keyword)
12576 case RID_CASE:
12578 tree expr, expr_hi;
12579 cp_token *ellipsis;
12581 /* Consume the `case' token. */
12582 cp_lexer_consume_token (parser->lexer);
12583 /* Parse the constant-expression. */
12584 expr = cp_parser_constant_expression (parser);
12585 if (check_for_bare_parameter_packs (expr))
12586 expr = error_mark_node;
12588 ellipsis = cp_lexer_peek_token (parser->lexer);
12589 if (ellipsis->type == CPP_ELLIPSIS)
12591 /* Consume the `...' token. */
12592 cp_lexer_consume_token (parser->lexer);
12593 expr_hi = cp_parser_constant_expression (parser);
12594 if (check_for_bare_parameter_packs (expr_hi))
12595 expr_hi = error_mark_node;
12597 /* We don't need to emit warnings here, as the common code
12598 will do this for us. */
12600 else
12601 expr_hi = NULL_TREE;
12603 if (parser->in_switch_statement_p)
12605 tree l = finish_case_label (token->location, expr, expr_hi);
12606 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12608 label = CASE_LABEL (l);
12609 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12612 else
12613 error_at (token->location,
12614 "case label %qE not within a switch statement",
12615 expr);
12617 break;
12619 case RID_DEFAULT:
12620 /* Consume the `default' token. */
12621 cp_lexer_consume_token (parser->lexer);
12623 if (parser->in_switch_statement_p)
12625 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
12626 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
12628 label = CASE_LABEL (l);
12629 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12632 else
12633 error_at (token->location, "case label not within a switch statement");
12634 break;
12636 default:
12637 /* Anything else must be an ordinary label. */
12638 label = finish_label_stmt (cp_parser_identifier (parser));
12639 if (label && TREE_CODE (label) == LABEL_DECL)
12640 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
12641 break;
12644 /* Require the `:' token. */
12645 cp_parser_require (parser, CPP_COLON, RT_COLON);
12647 /* An ordinary label may optionally be followed by attributes.
12648 However, this is only permitted if the attributes are then
12649 followed by a semicolon. This is because, for backward
12650 compatibility, when parsing
12651 lab: __attribute__ ((unused)) int i;
12652 we want the attribute to attach to "i", not "lab". */
12653 if (label != NULL_TREE
12654 && cp_next_tokens_can_be_gnu_attribute_p (parser))
12656 tree attrs;
12657 cp_parser_parse_tentatively (parser);
12658 attrs = cp_parser_gnu_attributes_opt (parser);
12659 if (attrs == NULL_TREE
12660 /* And fallthrough always binds to the expression-statement. */
12661 || attribute_fallthrough_p (attrs)
12662 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12663 cp_parser_abort_tentative_parse (parser);
12664 else if (!cp_parser_parse_definitely (parser))
12666 else
12667 attributes = attr_chainon (attributes, attrs);
12670 if (attributes != NULL_TREE)
12671 cplus_decl_attributes (&label, attributes, 0);
12673 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12676 /* Parse an expression-statement.
12678 expression-statement:
12679 expression [opt] ;
12681 Returns the new EXPR_STMT -- or NULL_TREE if the expression
12682 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
12683 indicates whether this expression-statement is part of an
12684 expression statement. */
12686 static tree
12687 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
12689 tree statement = NULL_TREE;
12690 cp_token *token = cp_lexer_peek_token (parser->lexer);
12691 location_t loc = token->location;
12693 /* There might be attribute fallthrough. */
12694 tree attr = cp_parser_gnu_attributes_opt (parser);
12696 /* If the next token is a ';', then there is no expression
12697 statement. */
12698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12700 statement = cp_parser_expression (parser);
12701 if (statement == error_mark_node
12702 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12704 cp_parser_skip_to_end_of_block_or_statement (parser);
12705 return error_mark_node;
12709 attr = process_stmt_assume_attribute (attr, statement, loc);
12711 /* Handle [[fallthrough]];. */
12712 if (attribute_fallthrough_p (attr))
12714 /* The next token after the fallthrough attribute is ';'. */
12715 if (statement == NULL_TREE)
12716 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
12717 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
12718 void_type_node, 0);
12719 else
12720 warning_at (loc, OPT_Wattributes,
12721 "%<fallthrough%> attribute not followed by %<;%>");
12722 attr = NULL_TREE;
12725 /* Allow "[[fallthrough]];", but warn otherwise. */
12726 if (attr != NULL_TREE)
12727 warning_at (loc, OPT_Wattributes,
12728 "attributes at the beginning of statement are ignored");
12730 /* Give a helpful message for "A<T>::type t;" and the like. */
12731 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
12732 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
12734 if (TREE_CODE (statement) == SCOPE_REF)
12735 error_at (token->location, "need %<typename%> before %qE because "
12736 "%qT is a dependent scope",
12737 statement, TREE_OPERAND (statement, 0));
12738 else if (is_overloaded_fn (statement)
12739 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
12741 /* A::A a; */
12742 tree fn = get_first_fn (statement);
12743 error_at (token->location,
12744 "%<%T::%D%> names the constructor, not the type",
12745 DECL_CONTEXT (fn), DECL_NAME (fn));
12749 /* Consume the final `;'. */
12750 cp_parser_consume_semicolon_at_end_of_statement (parser);
12752 if (in_statement_expr
12753 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12754 /* This is the final expression statement of a statement
12755 expression. */
12756 statement = finish_stmt_expr_expr (statement, in_statement_expr);
12757 else if (statement)
12758 statement = finish_expr_stmt (statement);
12760 return statement;
12763 /* Parse a compound-statement.
12765 compound-statement:
12766 { statement-seq [opt] label-seq [opt] }
12768 label-seq:
12769 label
12770 label-seq label
12772 GNU extension:
12774 compound-statement:
12775 { label-declaration-seq [opt] statement-seq [opt] }
12777 label-declaration-seq:
12778 label-declaration
12779 label-declaration-seq label-declaration
12781 Returns a tree representing the statement. */
12783 static tree
12784 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
12785 int bcs_flags, bool function_body)
12787 tree compound_stmt;
12788 matching_braces braces;
12790 /* Consume the `{'. */
12791 if (!braces.require_open (parser))
12792 return error_mark_node;
12793 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
12794 && !function_body && cxx_dialect < cxx14)
12795 pedwarn (input_location, OPT_Wpedantic,
12796 "compound-statement in %<constexpr%> function");
12797 /* Begin the compound-statement. */
12798 compound_stmt = begin_compound_stmt (bcs_flags);
12799 /* If the next keyword is `__label__' we have a label declaration. */
12800 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12801 cp_parser_label_declaration (parser);
12802 /* Parse an (optional) statement-seq. */
12803 cp_parser_statement_seq_opt (parser, in_statement_expr);
12805 /* Consume the `}'. */
12806 braces.require_close (parser);
12808 /* Finish the compound-statement. */
12809 finish_compound_stmt (compound_stmt);
12811 return compound_stmt;
12814 /* Parse an (optional) statement-seq.
12816 statement-seq:
12817 statement
12818 statement-seq [opt] statement */
12820 static void
12821 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
12823 /* Scan statements until there aren't any more. */
12824 while (true)
12826 cp_token *token = cp_lexer_peek_token (parser->lexer);
12828 /* If we are looking at a `}', then we have run out of
12829 statements; the same is true if we have reached the end
12830 of file, or have stumbled upon a stray '@end'. */
12831 if (token->type == CPP_CLOSE_BRACE
12832 || token->type == CPP_EOF
12833 || token->type == CPP_PRAGMA_EOL
12834 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
12835 break;
12837 /* If we are in a compound statement and find 'else' then
12838 something went wrong. */
12839 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
12841 if (parser->in_statement & IN_IF_STMT)
12842 break;
12843 else
12845 token = cp_lexer_consume_token (parser->lexer);
12846 error_at (token->location, "%<else%> without a previous %<if%>");
12850 /* Parse the statement. */
12851 cp_parser_statement (parser, in_statement_expr, true, NULL);
12855 /* Return true if this is the C++20 version of range-based-for with
12856 init-statement. */
12858 static bool
12859 cp_parser_range_based_for_with_init_p (cp_parser *parser)
12861 bool r = false;
12863 /* Save tokens so that we can put them back. */
12864 cp_lexer_save_tokens (parser->lexer);
12866 /* There has to be an unnested ; followed by an unnested :. */
12867 if (cp_parser_skip_to_closing_parenthesis_1 (parser,
12868 /*recovering=*/false,
12869 CPP_SEMICOLON,
12870 /*consume_paren=*/false) != -1)
12871 goto out;
12873 /* We found the semicolon, eat it now. */
12874 cp_lexer_consume_token (parser->lexer);
12876 /* Now look for ':' that is not nested in () or {}. */
12877 r = (cp_parser_skip_to_closing_parenthesis_1 (parser,
12878 /*recovering=*/false,
12879 CPP_COLON,
12880 /*consume_paren=*/false) == -1);
12882 out:
12883 /* Roll back the tokens we skipped. */
12884 cp_lexer_rollback_tokens (parser->lexer);
12886 return r;
12889 /* Return true if we're looking at (init; cond), false otherwise. */
12891 static bool
12892 cp_parser_init_statement_p (cp_parser *parser)
12894 /* Save tokens so that we can put them back. */
12895 cp_lexer_save_tokens (parser->lexer);
12897 /* Look for ';' that is not nested in () or {}. */
12898 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
12899 /*recovering=*/false,
12900 CPP_SEMICOLON,
12901 /*consume_paren=*/false);
12903 /* Roll back the tokens we skipped. */
12904 cp_lexer_rollback_tokens (parser->lexer);
12906 return ret == -1;
12909 /* Parse a selection-statement.
12911 selection-statement:
12912 if ( init-statement [opt] condition ) statement
12913 if ( init-statement [opt] condition ) statement else statement
12914 switch ( init-statement [opt] condition ) statement
12916 Returns the new IF_STMT or SWITCH_STMT.
12918 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12919 is a (possibly labeled) if statement which is not enclosed in
12920 braces and has an else clause. This is used to implement
12921 -Wparentheses.
12923 CHAIN is a vector of if-else-if conditions. This is used to implement
12924 -Wduplicated-cond. */
12926 static tree
12927 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
12928 vec<tree> *chain)
12930 cp_token *token;
12931 enum rid keyword;
12932 token_indent_info guard_tinfo;
12934 if (if_p != NULL)
12935 *if_p = false;
12937 /* Peek at the next token. */
12938 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
12939 guard_tinfo = get_token_indent_info (token);
12941 /* See what kind of keyword it is. */
12942 keyword = token->keyword;
12943 switch (keyword)
12945 case RID_IF:
12946 case RID_SWITCH:
12948 tree statement;
12949 tree condition;
12951 bool cx = false;
12952 if (keyword == RID_IF
12953 && cp_lexer_next_token_is_keyword (parser->lexer,
12954 RID_CONSTEXPR))
12956 cx = true;
12957 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12958 if (cxx_dialect < cxx17)
12959 pedwarn (tok->location, OPT_Wc__17_extensions,
12960 "%<if constexpr%> only available with "
12961 "%<-std=c++17%> or %<-std=gnu++17%>");
12963 int ce = 0;
12964 if (keyword == RID_IF && !cx)
12966 if (cp_lexer_next_token_is_keyword (parser->lexer,
12967 RID_CONSTEVAL))
12968 ce = 1;
12969 else if (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
12970 && cp_lexer_nth_token_is_keyword (parser->lexer, 2,
12971 RID_CONSTEVAL))
12973 ce = -1;
12974 cp_lexer_consume_token (parser->lexer);
12977 if (ce)
12979 cp_token *tok = cp_lexer_consume_token (parser->lexer);
12980 if (cxx_dialect < cxx23)
12981 pedwarn (tok->location, OPT_Wc__23_extensions,
12982 "%<if consteval%> only available with "
12983 "%<-std=c++2b%> or %<-std=gnu++2b%>");
12985 bool save_in_consteval_if_p = in_consteval_if_p;
12986 statement = begin_if_stmt ();
12987 IF_STMT_CONSTEVAL_P (statement) = true;
12988 condition = finish_if_stmt_cond (boolean_false_node, statement);
12990 gcc_rich_location richloc (tok->location);
12991 bool non_compound_stmt_p = false;
12992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12994 non_compound_stmt_p = true;
12995 richloc.add_fixit_insert_after (tok->location, "{");
12998 in_consteval_if_p |= ce > 0;
12999 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
13001 if (non_compound_stmt_p)
13003 location_t before_loc
13004 = cp_lexer_peek_token (parser->lexer)->location;
13005 richloc.add_fixit_insert_before (before_loc, "}");
13006 error_at (&richloc,
13007 "%<if consteval%> requires compound statement");
13008 non_compound_stmt_p = false;
13011 finish_then_clause (statement);
13013 /* If the next token is `else', parse the else-clause. */
13014 if (cp_lexer_next_token_is_keyword (parser->lexer,
13015 RID_ELSE))
13017 cp_token *else_tok = cp_lexer_peek_token (parser->lexer);
13018 gcc_rich_location else_richloc (else_tok->location);
13019 guard_tinfo = get_token_indent_info (else_tok);
13020 /* Consume the `else' keyword. */
13021 cp_lexer_consume_token (parser->lexer);
13023 begin_else_clause (statement);
13025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13027 non_compound_stmt_p = true;
13028 else_richloc.add_fixit_insert_after (else_tok->location,
13029 "{");
13032 in_consteval_if_p = save_in_consteval_if_p | (ce < 0);
13033 cp_parser_implicitly_scoped_statement (parser, NULL,
13034 guard_tinfo);
13036 if (non_compound_stmt_p)
13038 location_t before_loc
13039 = cp_lexer_peek_token (parser->lexer)->location;
13040 else_richloc.add_fixit_insert_before (before_loc, "}");
13041 error_at (&else_richloc,
13042 "%<if consteval%> requires compound statement");
13045 finish_else_clause (statement);
13048 in_consteval_if_p = save_in_consteval_if_p;
13049 if (ce < 0)
13051 std::swap (THEN_CLAUSE (statement), ELSE_CLAUSE (statement));
13052 if (THEN_CLAUSE (statement) == NULL_TREE)
13053 THEN_CLAUSE (statement) = build_empty_stmt (tok->location);
13056 finish_if_stmt (statement);
13057 return statement;
13060 /* Look for the `('. */
13061 matching_parens parens;
13062 if (!parens.require_open (parser))
13064 cp_parser_skip_to_end_of_statement (parser);
13065 return error_mark_node;
13068 /* Begin the selection-statement. */
13069 if (keyword == RID_IF)
13071 statement = begin_if_stmt ();
13072 IF_STMT_CONSTEXPR_P (statement) = cx;
13074 else
13075 statement = begin_switch_stmt ();
13077 /* Parse the optional init-statement. */
13078 if (cp_parser_init_statement_p (parser))
13080 tree decl;
13081 if (cxx_dialect < cxx17)
13082 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13083 OPT_Wc__17_extensions,
13084 "init-statement in selection statements only available "
13085 "with %<-std=c++17%> or %<-std=gnu++17%>");
13086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13087 /* A non-empty init-statement can have arbitrary side
13088 effects. */
13089 vec_free (chain);
13090 cp_parser_init_statement (parser, &decl);
13093 /* Parse the condition. */
13094 condition = cp_parser_condition (parser);
13095 /* Look for the `)'. */
13096 if (!parens.require_close (parser))
13097 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13098 /*consume_paren=*/true);
13100 if (keyword == RID_IF)
13102 bool nested_if;
13103 unsigned char in_statement;
13105 /* Add the condition. */
13106 condition = finish_if_stmt_cond (condition, statement);
13108 if (warn_duplicated_cond)
13109 warn_duplicated_cond_add_or_warn (token->location, condition,
13110 &chain);
13112 /* Parse the then-clause. */
13113 in_statement = parser->in_statement;
13114 parser->in_statement |= IN_IF_STMT;
13116 /* Outside a template, the non-selected branch of a constexpr
13117 if is a 'discarded statement', i.e. unevaluated. */
13118 bool was_discarded = in_discarded_stmt;
13119 bool discard_then = (cx && !processing_template_decl
13120 && integer_zerop (condition));
13121 if (discard_then)
13123 in_discarded_stmt = true;
13124 ++c_inhibit_evaluation_warnings;
13127 cp_parser_implicitly_scoped_statement (parser, &nested_if,
13128 guard_tinfo);
13130 parser->in_statement = in_statement;
13132 finish_then_clause (statement);
13134 if (discard_then)
13136 THEN_CLAUSE (statement) = NULL_TREE;
13137 in_discarded_stmt = was_discarded;
13138 --c_inhibit_evaluation_warnings;
13141 /* If the next token is `else', parse the else-clause. */
13142 if (cp_lexer_next_token_is_keyword (parser->lexer,
13143 RID_ELSE))
13145 bool discard_else = (cx && !processing_template_decl
13146 && integer_nonzerop (condition));
13147 if (discard_else)
13149 in_discarded_stmt = true;
13150 ++c_inhibit_evaluation_warnings;
13153 guard_tinfo
13154 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
13155 /* Consume the `else' keyword. */
13156 cp_lexer_consume_token (parser->lexer);
13157 if (warn_duplicated_cond)
13159 if (cp_lexer_next_token_is_keyword (parser->lexer,
13160 RID_IF)
13161 && chain == NULL)
13163 /* We've got "if (COND) else if (COND2)". Start
13164 the condition chain and add COND as the first
13165 element. */
13166 chain = new vec<tree> ();
13167 if (!CONSTANT_CLASS_P (condition)
13168 && !TREE_SIDE_EFFECTS (condition))
13170 /* Wrap it in a NOP_EXPR so that we can set the
13171 location of the condition. */
13172 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
13173 condition);
13174 SET_EXPR_LOCATION (e, token->location);
13175 chain->safe_push (e);
13178 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
13179 RID_IF))
13180 /* This is if-else without subsequent if. Zap the
13181 condition chain; we would have already warned at
13182 this point. */
13183 vec_free (chain);
13185 begin_else_clause (statement);
13186 /* Parse the else-clause. */
13187 cp_parser_implicitly_scoped_statement (parser, NULL,
13188 guard_tinfo, chain);
13190 finish_else_clause (statement);
13192 /* If we are currently parsing a then-clause, then
13193 IF_P will not be NULL. We set it to true to
13194 indicate that this if statement has an else clause.
13195 This may trigger the Wparentheses warning below
13196 when we get back up to the parent if statement. */
13197 if (if_p != NULL)
13198 *if_p = true;
13200 if (discard_else)
13202 ELSE_CLAUSE (statement) = NULL_TREE;
13203 in_discarded_stmt = was_discarded;
13204 --c_inhibit_evaluation_warnings;
13207 else
13209 /* This if statement does not have an else clause. If
13210 NESTED_IF is true, then the then-clause has an if
13211 statement which does have an else clause. We warn
13212 about the potential ambiguity. */
13213 if (nested_if)
13214 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
13215 "suggest explicit braces to avoid ambiguous"
13216 " %<else%>");
13217 if (warn_duplicated_cond)
13218 /* We don't need the condition chain anymore. */
13219 vec_free (chain);
13222 /* Now we're all done with the if-statement. */
13223 finish_if_stmt (statement);
13225 else
13227 bool in_switch_statement_p;
13228 unsigned char in_statement;
13230 /* Add the condition. */
13231 finish_switch_cond (condition, statement);
13233 /* Parse the body of the switch-statement. */
13234 in_switch_statement_p = parser->in_switch_statement_p;
13235 in_statement = parser->in_statement;
13236 parser->in_switch_statement_p = true;
13237 parser->in_statement |= IN_SWITCH_STMT;
13238 cp_parser_implicitly_scoped_statement (parser, if_p,
13239 guard_tinfo);
13240 parser->in_switch_statement_p = in_switch_statement_p;
13241 parser->in_statement = in_statement;
13243 /* Now we're all done with the switch-statement. */
13244 finish_switch_stmt (statement);
13247 return statement;
13249 break;
13251 default:
13252 cp_parser_error (parser, "expected selection-statement");
13253 return error_mark_node;
13257 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
13258 If we have seen at least one decl-specifier, and the next token is not
13259 a parenthesis (after "int (" we might be looking at a functional cast)
13260 neither we are dealing with a concept-check expression then we must be
13261 looking at a declaration. */
13263 static void
13264 cp_parser_maybe_commit_to_declaration (cp_parser* parser,
13265 cp_decl_specifier_seq *decl_specs)
13267 if (decl_specs->any_specifiers_p
13268 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
13269 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
13270 && !cp_parser_error_occurred (parser)
13271 && !(decl_specs->type
13272 && TREE_CODE (decl_specs->type) == TYPE_DECL
13273 && is_constrained_auto (TREE_TYPE (decl_specs->type))))
13274 cp_parser_commit_to_tentative_parse (parser);
13277 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
13278 The declarator shall not specify a function or an array. Returns
13279 TRUE if the declarator is valid, FALSE otherwise. */
13281 static bool
13282 cp_parser_check_condition_declarator (cp_parser* parser,
13283 cp_declarator *declarator,
13284 location_t loc)
13286 if (declarator == cp_error_declarator
13287 || function_declarator_p (declarator)
13288 || declarator->kind == cdk_array)
13290 if (declarator == cp_error_declarator)
13291 /* Already complained. */;
13292 else if (declarator->kind == cdk_array)
13293 error_at (loc, "condition declares an array");
13294 else
13295 error_at (loc, "condition declares a function");
13296 if (parser->fully_implicit_function_template_p)
13297 abort_fully_implicit_template (parser);
13298 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
13299 /*or_comma=*/false,
13300 /*consume_paren=*/false);
13301 return false;
13303 else
13304 return true;
13307 /* Parse a condition.
13309 condition:
13310 expression
13311 type-specifier-seq declarator = initializer-clause
13312 type-specifier-seq declarator braced-init-list
13314 GNU Extension:
13316 condition:
13317 type-specifier-seq declarator asm-specification [opt]
13318 attributes [opt] = assignment-expression
13320 Returns the expression that should be tested. */
13322 static tree
13323 cp_parser_condition (cp_parser* parser)
13325 cp_decl_specifier_seq type_specifiers;
13326 const char *saved_message;
13327 int declares_class_or_enum;
13329 /* Try the declaration first. */
13330 cp_parser_parse_tentatively (parser);
13331 /* New types are not allowed in the type-specifier-seq for a
13332 condition. */
13333 saved_message = parser->type_definition_forbidden_message;
13334 parser->type_definition_forbidden_message
13335 = G_("types may not be defined in conditions");
13336 /* Parse the type-specifier-seq. */
13337 cp_parser_decl_specifier_seq (parser,
13338 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
13339 &type_specifiers,
13340 &declares_class_or_enum);
13341 /* Restore the saved message. */
13342 parser->type_definition_forbidden_message = saved_message;
13344 /* Gather the attributes that were provided with the
13345 decl-specifiers. */
13346 tree prefix_attributes = type_specifiers.attributes;
13348 cp_parser_maybe_commit_to_declaration (parser, &type_specifiers);
13350 /* If all is well, we might be looking at a declaration. */
13351 if (!cp_parser_error_occurred (parser))
13353 tree decl;
13354 tree asm_specification;
13355 tree attributes;
13356 cp_declarator *declarator;
13357 tree initializer = NULL_TREE;
13358 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13360 /* Parse the declarator. */
13361 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13362 CP_PARSER_FLAGS_NONE,
13363 /*ctor_dtor_or_conv_p=*/NULL,
13364 /*parenthesized_p=*/NULL,
13365 /*member_p=*/false,
13366 /*friend_p=*/false,
13367 /*static_p=*/false);
13368 /* Parse the attributes. */
13369 attributes = cp_parser_attributes_opt (parser);
13370 /* Parse the asm-specification. */
13371 asm_specification = cp_parser_asm_specification_opt (parser);
13372 /* If the next token is not an `=' or '{', then we might still be
13373 looking at an expression. For example:
13375 if (A(a).x)
13377 looks like a decl-specifier-seq and a declarator -- but then
13378 there is no `=', so this is an expression. */
13379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
13380 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13381 cp_parser_simulate_error (parser);
13383 /* If we did see an `=' or '{', then we are looking at a declaration
13384 for sure. */
13385 if (cp_parser_parse_definitely (parser))
13387 tree pushed_scope;
13388 bool non_constant_p = false;
13389 int flags = LOOKUP_ONLYCONVERTING;
13391 if (!cp_parser_check_condition_declarator (parser, declarator, loc))
13392 return error_mark_node;
13394 /* Create the declaration. */
13395 decl = start_decl (declarator, &type_specifiers,
13396 /*initialized_p=*/true,
13397 attributes, prefix_attributes,
13398 &pushed_scope);
13400 declarator->init_loc = cp_lexer_peek_token (parser->lexer)->location;
13401 /* Parse the initializer. */
13402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13404 initializer = cp_parser_braced_list (parser, &non_constant_p);
13405 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
13406 flags = 0;
13408 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13410 /* Consume the `='. */
13411 cp_lexer_consume_token (parser->lexer);
13412 initializer = cp_parser_initializer_clause (parser,
13413 &non_constant_p);
13415 else
13417 cp_parser_error (parser, "expected initializer");
13418 initializer = error_mark_node;
13420 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
13421 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13423 /* Process the initializer. */
13424 cp_finish_decl (decl,
13425 initializer, !non_constant_p,
13426 asm_specification,
13427 flags);
13429 if (pushed_scope)
13430 pop_scope (pushed_scope);
13432 return convert_from_reference (decl);
13435 /* If we didn't even get past the declarator successfully, we are
13436 definitely not looking at a declaration. */
13437 else
13438 cp_parser_abort_tentative_parse (parser);
13440 /* Otherwise, we are looking at an expression. */
13441 return cp_parser_expression (parser);
13444 /* Parses a for-statement or range-for-statement until the closing ')',
13445 not included. */
13447 static tree
13448 cp_parser_for (cp_parser *parser, bool ivdep, unsigned short unroll)
13450 tree init, scope, decl;
13451 bool is_range_for;
13453 /* Begin the for-statement. */
13454 scope = begin_for_scope (&init);
13456 /* Maybe parse the optional init-statement in a range-based for loop. */
13457 if (cp_parser_range_based_for_with_init_p (parser)
13458 /* Checked for diagnostic purposes only. */
13459 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13461 tree dummy;
13462 cp_parser_init_statement (parser, &dummy);
13463 if (cxx_dialect < cxx20)
13465 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
13466 OPT_Wc__20_extensions,
13467 "range-based %<for%> loops with initializer only "
13468 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13469 decl = error_mark_node;
13473 /* Parse the initialization. */
13474 is_range_for = cp_parser_init_statement (parser, &decl);
13476 if (is_range_for)
13477 return cp_parser_range_for (parser, scope, init, decl, ivdep, unroll,
13478 false);
13479 else
13480 return cp_parser_c_for (parser, scope, init, ivdep, unroll);
13483 static tree
13484 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep,
13485 unsigned short unroll)
13487 /* Normal for loop */
13488 tree condition = NULL_TREE;
13489 tree expression = NULL_TREE;
13490 tree stmt;
13492 stmt = begin_for_stmt (scope, init);
13493 /* The init-statement has already been parsed in
13494 cp_parser_init_statement, so no work is needed here. */
13495 finish_init_stmt (stmt);
13497 /* If there's a condition, process it. */
13498 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13499 condition = cp_parser_condition (parser);
13500 else if (ivdep)
13502 cp_parser_error (parser, "missing loop condition in loop with "
13503 "%<GCC ivdep%> pragma");
13504 condition = error_mark_node;
13506 else if (unroll)
13508 cp_parser_error (parser, "missing loop condition in loop with "
13509 "%<GCC unroll%> pragma");
13510 condition = error_mark_node;
13512 finish_for_cond (condition, stmt, ivdep, unroll);
13513 /* Look for the `;'. */
13514 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13516 /* If there's an expression, process it. */
13517 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13518 expression = cp_parser_expression (parser);
13519 finish_for_expr (expression, stmt);
13521 return stmt;
13524 /* Tries to parse a range-based for-statement:
13526 range-based-for:
13527 decl-specifier-seq declarator : expression
13529 The decl-specifier-seq declarator and the `:' are already parsed by
13530 cp_parser_init_statement. If processing_template_decl it returns a
13531 newly created RANGE_FOR_STMT; if not, it is converted to a
13532 regular FOR_STMT. */
13534 static tree
13535 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
13536 bool ivdep, unsigned short unroll, bool is_omp)
13538 tree stmt, range_expr;
13539 auto_vec <cxx_binding *, 16> bindings;
13540 auto_vec <tree, 16> names;
13541 tree decomp_first_name = NULL_TREE;
13542 unsigned int decomp_cnt = 0;
13544 /* Get the range declaration momentarily out of the way so that
13545 the range expression doesn't clash with it. */
13546 if (range_decl != error_mark_node)
13548 if (DECL_HAS_VALUE_EXPR_P (range_decl))
13550 tree v = DECL_VALUE_EXPR (range_decl);
13551 /* For decomposition declaration get all of the corresponding
13552 declarations out of the way. */
13553 if (TREE_CODE (v) == ARRAY_REF
13554 && VAR_P (TREE_OPERAND (v, 0))
13555 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
13557 tree d = range_decl;
13558 range_decl = TREE_OPERAND (v, 0);
13559 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
13560 decomp_first_name = d;
13561 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
13563 tree name = DECL_NAME (d);
13564 names.safe_push (name);
13565 bindings.safe_push (IDENTIFIER_BINDING (name));
13566 IDENTIFIER_BINDING (name)
13567 = IDENTIFIER_BINDING (name)->previous;
13571 if (names.is_empty ())
13573 tree name = DECL_NAME (range_decl);
13574 names.safe_push (name);
13575 bindings.safe_push (IDENTIFIER_BINDING (name));
13576 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
13580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13582 bool expr_non_constant_p;
13583 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
13585 else
13586 range_expr = cp_parser_expression (parser);
13588 /* Put the range declaration(s) back into scope. */
13589 for (unsigned int i = 0; i < names.length (); i++)
13591 cxx_binding *binding = bindings[i];
13592 binding->previous = IDENTIFIER_BINDING (names[i]);
13593 IDENTIFIER_BINDING (names[i]) = binding;
13596 /* finish_omp_for has its own code for the following, so just
13597 return the range_expr instead. */
13598 if (is_omp)
13599 return range_expr;
13601 /* If in template, STMT is converted to a normal for-statement
13602 at instantiation. If not, it is done just ahead. */
13603 if (processing_template_decl)
13605 if (check_for_bare_parameter_packs (range_expr))
13606 range_expr = error_mark_node;
13607 stmt = begin_range_for_stmt (scope, init);
13608 if (ivdep)
13609 RANGE_FOR_IVDEP (stmt) = 1;
13610 if (unroll)
13611 RANGE_FOR_UNROLL (stmt) = build_int_cst (integer_type_node, unroll);
13612 finish_range_for_decl (stmt, range_decl, range_expr);
13613 if (!type_dependent_expression_p (range_expr)
13614 /* do_auto_deduction doesn't mess with template init-lists. */
13615 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
13616 do_range_for_auto_deduction (range_decl, range_expr);
13618 else
13620 stmt = begin_for_stmt (scope, init);
13621 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
13622 decomp_first_name, decomp_cnt, ivdep,
13623 unroll);
13625 return stmt;
13628 /* Subroutine of cp_convert_range_for: given the initializer expression,
13629 builds up the range temporary. */
13631 static tree
13632 build_range_temp (tree range_expr)
13634 /* Find out the type deduced by the declaration
13635 `auto &&__range = range_expr'. */
13636 tree auto_node = make_auto ();
13637 tree range_type = cp_build_reference_type (auto_node, true);
13638 range_type = do_auto_deduction (range_type, range_expr, auto_node);
13640 /* Create the __range variable. */
13641 tree range_temp = build_decl (input_location, VAR_DECL,
13642 for_range__identifier, range_type);
13643 TREE_USED (range_temp) = 1;
13644 DECL_ARTIFICIAL (range_temp) = 1;
13646 return range_temp;
13649 /* Used by cp_parser_range_for in template context: we aren't going to
13650 do a full conversion yet, but we still need to resolve auto in the
13651 type of the for-range-declaration if present. This is basically
13652 a shortcut version of cp_convert_range_for. */
13654 static void
13655 do_range_for_auto_deduction (tree decl, tree range_expr)
13657 tree auto_node = type_uses_auto (TREE_TYPE (decl));
13658 if (auto_node)
13660 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
13661 range_temp = convert_from_reference (build_range_temp (range_expr));
13662 iter_type = (cp_parser_perform_range_for_lookup
13663 (range_temp, &begin_dummy, &end_dummy));
13664 if (iter_type)
13666 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
13667 iter_type);
13668 iter_decl = build_x_indirect_ref (input_location, iter_decl,
13669 RO_UNARY_STAR, NULL_TREE,
13670 tf_warning_or_error);
13671 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
13672 iter_decl, auto_node,
13673 tf_warning_or_error,
13674 adc_variable_type);
13679 /* Warns when the loop variable should be changed to a reference type to
13680 avoid unnecessary copying. I.e., from
13682 for (const auto x : range)
13684 where range returns a reference, to
13686 for (const auto &x : range)
13688 if this version doesn't make a copy.
13690 This function also warns when the loop variable is initialized with
13691 a value of a different type resulting in a copy:
13693 int arr[10];
13694 for (const double &x : arr)
13696 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
13697 This function is never called when processing_template_decl is on. */
13699 static void
13700 warn_for_range_copy (tree decl, tree expr)
13702 if (!warn_range_loop_construct
13703 || decl == error_mark_node)
13704 return;
13706 location_t loc = DECL_SOURCE_LOCATION (decl);
13707 tree type = TREE_TYPE (decl);
13709 if (from_macro_expansion_at (loc))
13710 return;
13712 if (TYPE_REF_P (type))
13714 if (glvalue_p (expr)
13715 && ref_conv_binds_to_temporary (type, expr).is_true ())
13717 auto_diagnostic_group d;
13718 if (warning_at (loc, OPT_Wrange_loop_construct,
13719 "loop variable %qD of type %qT binds to a temporary "
13720 "constructed from type %qT", decl, type,
13721 TREE_TYPE (expr)))
13723 tree ref = cp_build_qualified_type (TREE_TYPE (expr),
13724 TYPE_QUAL_CONST);
13725 ref = cp_build_reference_type (ref, /*rval*/false);
13726 inform (loc, "use non-reference type %qT to make the copy "
13727 "explicit or %qT to prevent copying",
13728 non_reference (type), ref);
13731 return;
13733 else if (!CP_TYPE_CONST_P (type))
13734 return;
13736 /* Since small trivially copyable types are cheap to copy, we suppress the
13737 warning for them. 64B is a common size of a cache line. */
13738 if (TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST
13739 || (tree_to_uhwi (TYPE_SIZE_UNIT (type)) <= 64
13740 && trivially_copyable_p (type)))
13741 return;
13743 /* If we can initialize a reference directly, suggest that to avoid the
13744 copy. */
13745 tree rtype = cp_build_reference_type (type, /*rval*/false);
13746 if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
13748 auto_diagnostic_group d;
13749 if (warning_at (loc, OPT_Wrange_loop_construct,
13750 "loop variable %qD creates a copy from type %qT",
13751 decl, type))
13753 gcc_rich_location richloc (loc);
13754 richloc.add_fixit_insert_before ("&");
13755 inform (&richloc, "use reference type to prevent copying");
13760 /* Converts a range-based for-statement into a normal
13761 for-statement, as per the definition.
13763 for (RANGE_DECL : RANGE_EXPR)
13764 BLOCK
13766 should be equivalent to:
13769 auto &&__range = RANGE_EXPR;
13770 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
13771 __begin != __end;
13772 ++__begin)
13774 RANGE_DECL = *__begin;
13775 BLOCK
13779 If RANGE_EXPR is an array:
13780 BEGIN_EXPR = __range
13781 END_EXPR = __range + ARRAY_SIZE(__range)
13782 Else if RANGE_EXPR has a member 'begin' or 'end':
13783 BEGIN_EXPR = __range.begin()
13784 END_EXPR = __range.end()
13785 Else:
13786 BEGIN_EXPR = begin(__range)
13787 END_EXPR = end(__range);
13789 If __range has a member 'begin' but not 'end', or vice versa, we must
13790 still use the second alternative (it will surely fail, however).
13791 When calling begin()/end() in the third alternative we must use
13792 argument dependent lookup, but always considering 'std' as an associated
13793 namespace. */
13795 tree
13796 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
13797 tree decomp_first_name, unsigned int decomp_cnt,
13798 bool ivdep, unsigned short unroll)
13800 tree begin, end;
13801 tree iter_type, begin_expr, end_expr;
13802 tree condition, expression;
13804 range_expr = mark_lvalue_use (range_expr);
13806 if (range_decl == error_mark_node || range_expr == error_mark_node)
13807 /* If an error happened previously do nothing or else a lot of
13808 unhelpful errors would be issued. */
13809 begin_expr = end_expr = iter_type = error_mark_node;
13810 else
13812 tree range_temp;
13814 if (VAR_P (range_expr)
13815 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
13816 /* Can't bind a reference to an array of runtime bound. */
13817 range_temp = range_expr;
13818 else
13820 range_temp = build_range_temp (range_expr);
13821 pushdecl (range_temp);
13822 cp_finish_decl (range_temp, range_expr,
13823 /*is_constant_init*/false, NULL_TREE,
13824 LOOKUP_ONLYCONVERTING);
13825 range_temp = convert_from_reference (range_temp);
13827 iter_type = cp_parser_perform_range_for_lookup (range_temp,
13828 &begin_expr, &end_expr);
13831 /* The new for initialization statement. */
13832 begin = build_decl (input_location, VAR_DECL, for_begin__identifier,
13833 iter_type);
13834 TREE_USED (begin) = 1;
13835 DECL_ARTIFICIAL (begin) = 1;
13836 pushdecl (begin);
13837 cp_finish_decl (begin, begin_expr,
13838 /*is_constant_init*/false, NULL_TREE,
13839 LOOKUP_ONLYCONVERTING);
13841 if (cxx_dialect >= cxx17)
13842 iter_type = cv_unqualified (TREE_TYPE (end_expr));
13843 end = build_decl (input_location, VAR_DECL, for_end__identifier, iter_type);
13844 TREE_USED (end) = 1;
13845 DECL_ARTIFICIAL (end) = 1;
13846 pushdecl (end);
13847 cp_finish_decl (end, end_expr,
13848 /*is_constant_init*/false, NULL_TREE,
13849 LOOKUP_ONLYCONVERTING);
13851 finish_init_stmt (statement);
13853 /* The new for condition. */
13854 condition = build_x_binary_op (input_location, NE_EXPR,
13855 begin, ERROR_MARK,
13856 end, ERROR_MARK,
13857 NULL_TREE, NULL, tf_warning_or_error);
13858 finish_for_cond (condition, statement, ivdep, unroll);
13860 /* The new increment expression. */
13861 expression = finish_unary_op_expr (input_location,
13862 PREINCREMENT_EXPR, begin,
13863 tf_warning_or_error);
13864 finish_for_expr (expression, statement);
13866 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13867 cp_maybe_mangle_decomp (range_decl, decomp_first_name, decomp_cnt);
13869 /* The declaration is initialized with *__begin inside the loop body. */
13870 tree deref_begin = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
13871 NULL_TREE, tf_warning_or_error);
13872 cp_finish_decl (range_decl, deref_begin,
13873 /*is_constant_init*/false, NULL_TREE,
13874 LOOKUP_ONLYCONVERTING);
13875 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
13876 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
13878 warn_for_range_copy (range_decl, deref_begin);
13880 return statement;
13883 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13884 We need to solve both at the same time because the method used
13885 depends on the existence of members begin or end.
13886 Returns the type deduced for the iterator expression. */
13888 static tree
13889 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
13891 if (error_operand_p (range))
13893 *begin = *end = error_mark_node;
13894 return error_mark_node;
13897 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
13899 error ("range-based %<for%> expression of type %qT "
13900 "has incomplete type", TREE_TYPE (range));
13901 *begin = *end = error_mark_node;
13902 return error_mark_node;
13904 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
13906 /* If RANGE is an array, we will use pointer arithmetic. */
13907 *begin = decay_conversion (range, tf_warning_or_error);
13908 *end = build_binary_op (input_location, PLUS_EXPR,
13909 range,
13910 array_type_nelts_top (TREE_TYPE (range)),
13911 false);
13912 return TREE_TYPE (*begin);
13914 else
13916 /* If it is not an array, we must do a bit of magic. */
13917 tree id_begin, id_end;
13918 tree member_begin, member_end;
13920 *begin = *end = error_mark_node;
13922 id_begin = get_identifier ("begin");
13923 id_end = get_identifier ("end");
13924 member_begin = lookup_member (TREE_TYPE (range), id_begin,
13925 /*protect=*/2, /*want_type=*/false,
13926 tf_warning_or_error);
13927 member_end = lookup_member (TREE_TYPE (range), id_end,
13928 /*protect=*/2, /*want_type=*/false,
13929 tf_warning_or_error);
13931 if (member_begin != NULL_TREE && member_end != NULL_TREE)
13933 /* Use the member functions. */
13934 *begin = cp_parser_range_for_member_function (range, id_begin);
13935 *end = cp_parser_range_for_member_function (range, id_end);
13937 else
13939 /* Use global functions with ADL. */
13940 releasing_vec vec;
13942 vec_safe_push (vec, range);
13944 member_begin = perform_koenig_lookup (id_begin, vec,
13945 tf_warning_or_error);
13946 *begin = finish_call_expr (member_begin, &vec, false, true,
13947 tf_warning_or_error);
13948 member_end = perform_koenig_lookup (id_end, vec,
13949 tf_warning_or_error);
13950 *end = finish_call_expr (member_end, &vec, false, true,
13951 tf_warning_or_error);
13954 /* Last common checks. */
13955 if (*begin == error_mark_node || *end == error_mark_node)
13957 /* If one of the expressions is an error do no more checks. */
13958 *begin = *end = error_mark_node;
13959 return error_mark_node;
13961 else if (type_dependent_expression_p (*begin)
13962 || type_dependent_expression_p (*end))
13963 /* Can happen, when, eg, in a template context, Koenig lookup
13964 can't resolve begin/end (c++/58503). */
13965 return NULL_TREE;
13966 else
13968 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
13969 /* The unqualified type of the __begin and __end temporaries should
13970 be the same, as required by the multiple auto declaration. */
13971 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
13973 if (cxx_dialect >= cxx17
13974 && (build_x_binary_op (input_location, NE_EXPR,
13975 *begin, ERROR_MARK,
13976 *end, ERROR_MARK,
13977 NULL_TREE, NULL, tf_none)
13978 != error_mark_node))
13979 /* P0184R0 allows __begin and __end to have different types,
13980 but make sure they are comparable so we can give a better
13981 diagnostic. */;
13982 else
13983 error ("inconsistent begin/end types in range-based %<for%> "
13984 "statement: %qT and %qT",
13985 TREE_TYPE (*begin), TREE_TYPE (*end));
13987 return iter_type;
13992 /* Helper function for cp_parser_perform_range_for_lookup.
13993 Builds a tree for RANGE.IDENTIFIER(). */
13995 static tree
13996 cp_parser_range_for_member_function (tree range, tree identifier)
13998 tree member, res;
14000 member = finish_class_member_access_expr (range, identifier,
14001 false, tf_warning_or_error);
14002 if (member == error_mark_node)
14003 return error_mark_node;
14005 releasing_vec vec;
14006 res = finish_call_expr (member, &vec,
14007 /*disallow_virtual=*/false,
14008 /*koenig_p=*/false,
14009 tf_warning_or_error);
14010 return res;
14013 /* Parse an iteration-statement.
14015 iteration-statement:
14016 while ( condition ) statement
14017 do statement while ( expression ) ;
14018 for ( init-statement condition [opt] ; expression [opt] )
14019 statement
14021 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
14023 static tree
14024 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
14025 unsigned short unroll)
14027 cp_token *token;
14028 enum rid keyword;
14029 tree statement;
14030 unsigned char in_statement;
14031 token_indent_info guard_tinfo;
14033 /* Peek at the next token. */
14034 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
14035 if (!token)
14036 return error_mark_node;
14038 guard_tinfo = get_token_indent_info (token);
14040 /* Remember whether or not we are already within an iteration
14041 statement. */
14042 in_statement = parser->in_statement;
14044 /* See what kind of keyword it is. */
14045 keyword = token->keyword;
14046 switch (keyword)
14048 case RID_WHILE:
14050 tree condition;
14052 /* Begin the while-statement. */
14053 statement = begin_while_stmt ();
14054 /* Look for the `('. */
14055 matching_parens parens;
14056 parens.require_open (parser);
14057 /* Parse the condition. */
14058 condition = cp_parser_condition (parser);
14059 finish_while_stmt_cond (condition, statement, ivdep, unroll);
14060 /* Look for the `)'. */
14061 parens.require_close (parser);
14062 /* Parse the dependent statement. */
14063 parser->in_statement = IN_ITERATION_STMT;
14064 bool prev = note_iteration_stmt_body_start ();
14065 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14066 note_iteration_stmt_body_end (prev);
14067 parser->in_statement = in_statement;
14068 /* We're done with the while-statement. */
14069 finish_while_stmt (statement);
14071 break;
14073 case RID_DO:
14075 tree expression;
14077 /* Begin the do-statement. */
14078 statement = begin_do_stmt ();
14079 /* Parse the body of the do-statement. */
14080 parser->in_statement = IN_ITERATION_STMT;
14081 bool prev = note_iteration_stmt_body_start ();
14082 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
14083 note_iteration_stmt_body_end (prev);
14084 parser->in_statement = in_statement;
14085 finish_do_body (statement);
14086 /* Look for the `while' keyword. */
14087 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
14088 /* Look for the `('. */
14089 matching_parens parens;
14090 parens.require_open (parser);
14091 /* Parse the expression. */
14092 expression = cp_parser_expression (parser);
14093 /* We're done with the do-statement. */
14094 finish_do_stmt (expression, statement, ivdep, unroll);
14095 /* Look for the `)'. */
14096 parens.require_close (parser);
14097 /* Look for the `;'. */
14098 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14100 break;
14102 case RID_FOR:
14104 /* Look for the `('. */
14105 matching_parens parens;
14106 parens.require_open (parser);
14108 statement = cp_parser_for (parser, ivdep, unroll);
14110 /* Look for the `)'. */
14111 parens.require_close (parser);
14113 /* Parse the body of the for-statement. */
14114 parser->in_statement = IN_ITERATION_STMT;
14115 bool prev = note_iteration_stmt_body_start ();
14116 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
14117 note_iteration_stmt_body_end (prev);
14118 parser->in_statement = in_statement;
14120 /* We're done with the for-statement. */
14121 finish_for_stmt (statement);
14123 break;
14125 default:
14126 cp_parser_error (parser, "expected iteration-statement");
14127 statement = error_mark_node;
14128 break;
14131 return statement;
14134 /* Parse an init-statement or the declarator of a range-based-for.
14135 Returns true if a range-based-for declaration is seen.
14137 init-statement:
14138 expression-statement
14139 simple-declaration
14140 alias-declaration */
14142 static bool
14143 cp_parser_init_statement (cp_parser *parser, tree *decl)
14145 /* If the next token is a `;', then we have an empty
14146 expression-statement. Grammatically, this is also a
14147 simple-declaration, but an invalid one, because it does not
14148 declare anything. Therefore, if we did not handle this case
14149 specially, we would issue an error message about an invalid
14150 declaration. */
14151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14153 bool is_range_for = false;
14154 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14156 /* A colon is used in range-based for. */
14157 parser->colon_corrects_to_scope_p = false;
14159 /* We're going to speculatively look for a declaration, falling back
14160 to an expression, if necessary. */
14161 cp_parser_parse_tentatively (parser);
14162 bool expect_semicolon_p = true;
14163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14165 cp_parser_alias_declaration (parser);
14166 expect_semicolon_p = false;
14167 if (cxx_dialect < cxx23
14168 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
14169 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14170 OPT_Wc__23_extensions,
14171 "alias-declaration in init-statement only "
14172 "available with %<-std=c++23%> or %<-std=gnu++23%>");
14174 else
14175 /* Parse the declaration. */
14176 cp_parser_simple_declaration (parser,
14177 /*function_definition_allowed_p=*/false,
14178 decl);
14179 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14180 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14182 /* It is a range-for, consume the ':'. */
14183 cp_lexer_consume_token (parser->lexer);
14184 is_range_for = true;
14185 if (cxx_dialect < cxx11)
14186 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
14187 OPT_Wc__11_extensions,
14188 "range-based %<for%> loops only available with "
14189 "%<-std=c++11%> or %<-std=gnu++11%>");
14191 else if (expect_semicolon_p)
14192 /* The ';' is not consumed yet because we told
14193 cp_parser_simple_declaration not to. */
14194 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14196 if (cp_parser_parse_definitely (parser))
14197 return is_range_for;
14198 /* If the tentative parse failed, then we shall need to look for an
14199 expression-statement. */
14201 /* If we are here, it is an expression-statement. */
14202 cp_parser_expression_statement (parser, NULL_TREE);
14203 return false;
14206 /* Parse a jump-statement.
14208 jump-statement:
14209 break ;
14210 continue ;
14211 return expression [opt] ;
14212 return braced-init-list ;
14213 coroutine-return-statement;
14214 goto identifier ;
14216 GNU extension:
14218 jump-statement:
14219 goto * expression ;
14221 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
14223 static tree
14224 cp_parser_jump_statement (cp_parser* parser)
14226 tree statement = error_mark_node;
14227 cp_token *token;
14228 enum rid keyword;
14229 unsigned char in_statement;
14231 /* Peek at the next token. */
14232 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
14233 if (!token)
14234 return error_mark_node;
14236 /* See what kind of keyword it is. */
14237 keyword = token->keyword;
14238 switch (keyword)
14240 case RID_BREAK:
14241 in_statement = parser->in_statement & ~IN_IF_STMT;
14242 switch (in_statement)
14244 case 0:
14245 error_at (token->location, "break statement not within loop or switch");
14246 break;
14247 default:
14248 gcc_assert ((in_statement & IN_SWITCH_STMT)
14249 || in_statement == IN_ITERATION_STMT);
14250 statement = finish_break_stmt ();
14251 if (in_statement == IN_ITERATION_STMT)
14252 break_maybe_infinite_loop ();
14253 break;
14254 case IN_OMP_BLOCK:
14255 error_at (token->location, "invalid exit from OpenMP structured block");
14256 break;
14257 case IN_OMP_FOR:
14258 error_at (token->location, "break statement used with OpenMP for loop");
14259 break;
14261 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14262 break;
14264 case RID_CONTINUE:
14265 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
14267 case 0:
14268 error_at (token->location, "continue statement not within a loop");
14269 break;
14270 /* Fall through. */
14271 case IN_ITERATION_STMT:
14272 case IN_OMP_FOR:
14273 statement = finish_continue_stmt ();
14274 break;
14275 case IN_OMP_BLOCK:
14276 error_at (token->location, "invalid exit from OpenMP structured block");
14277 break;
14278 default:
14279 gcc_unreachable ();
14281 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14282 break;
14284 case RID_CO_RETURN:
14285 case RID_RETURN:
14287 tree expr;
14288 bool expr_non_constant_p;
14290 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14292 cp_lexer_set_source_position (parser->lexer);
14293 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14294 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
14296 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14297 expr = cp_parser_expression (parser);
14298 else
14299 /* If the next token is a `;', then there is no
14300 expression. */
14301 expr = NULL_TREE;
14302 /* Build the return-statement, check co-return first, since type
14303 deduction is not valid there. */
14304 if (keyword == RID_CO_RETURN)
14305 statement = finish_co_return_stmt (token->location, expr);
14306 else if (FNDECL_USED_AUTO (current_function_decl) && in_discarded_stmt)
14307 /* Don't deduce from a discarded return statement. */;
14308 else
14309 statement = finish_return_stmt (expr);
14310 /* Look for the final `;'. */
14311 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14313 break;
14315 case RID_GOTO:
14316 if (parser->in_function_body
14317 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
14318 && cxx_dialect < cxx23)
14320 error ("%<goto%> in %<constexpr%> function only available with "
14321 "%<-std=c++2b%> or %<-std=gnu++2b%>");
14322 cp_function_chain->invalid_constexpr = true;
14325 /* Create the goto-statement. */
14326 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
14328 /* Issue a warning about this use of a GNU extension. */
14329 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
14330 /* Consume the '*' token. */
14331 cp_lexer_consume_token (parser->lexer);
14332 /* Parse the dependent expression. */
14333 finish_goto_stmt (cp_parser_expression (parser));
14335 else
14336 finish_goto_stmt (cp_parser_identifier (parser));
14337 /* Look for the final `;'. */
14338 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14339 break;
14341 default:
14342 cp_parser_error (parser, "expected jump-statement");
14343 break;
14346 return statement;
14349 /* Parse a declaration-statement.
14351 declaration-statement:
14352 block-declaration */
14354 static void
14355 cp_parser_declaration_statement (cp_parser* parser)
14357 void *p;
14359 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14360 p = obstack_alloc (&declarator_obstack, 0);
14362 /* Parse the block-declaration. */
14363 cp_parser_block_declaration (parser, /*statement_p=*/true);
14365 /* Free any declarators allocated. */
14366 obstack_free (&declarator_obstack, p);
14369 /* Some dependent statements (like `if (cond) statement'), are
14370 implicitly in their own scope. In other words, if the statement is
14371 a single statement (as opposed to a compound-statement), it is
14372 none-the-less treated as if it were enclosed in braces. Any
14373 declarations appearing in the dependent statement are out of scope
14374 after control passes that point. This function parses a statement,
14375 but ensures that is in its own scope, even if it is not a
14376 compound-statement.
14378 If IF_P is not NULL, *IF_P is set to indicate whether the statement
14379 is a (possibly labeled) if statement which is not enclosed in
14380 braces and has an else clause. This is used to implement
14381 -Wparentheses.
14383 CHAIN is a vector of if-else-if conditions. This is used to implement
14384 -Wduplicated-cond.
14386 Returns the new statement. */
14388 static tree
14389 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
14390 const token_indent_info &guard_tinfo,
14391 vec<tree> *chain)
14393 tree statement;
14394 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
14395 location_t body_loc_after_labels = UNKNOWN_LOCATION;
14396 token_indent_info body_tinfo
14397 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14399 if (if_p != NULL)
14400 *if_p = false;
14402 /* Mark if () ; with a special NOP_EXPR. */
14403 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14405 cp_lexer_consume_token (parser->lexer);
14406 statement = add_stmt (build_empty_stmt (body_loc));
14408 if (guard_tinfo.keyword == RID_IF
14409 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
14410 warning_at (body_loc, OPT_Wempty_body,
14411 "suggest braces around empty body in an %<if%> statement");
14412 else if (guard_tinfo.keyword == RID_ELSE)
14413 warning_at (body_loc, OPT_Wempty_body,
14414 "suggest braces around empty body in an %<else%> statement");
14416 /* if a compound is opened, we simply parse the statement directly. */
14417 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14418 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
14419 /* If the token is not a `{', then we must take special action. */
14420 else
14422 /* Create a compound-statement. */
14423 statement = begin_compound_stmt (0);
14424 /* Parse the dependent-statement. */
14425 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
14426 &body_loc_after_labels);
14427 /* Finish the dummy compound-statement. */
14428 finish_compound_stmt (statement);
14431 token_indent_info next_tinfo
14432 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14433 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14435 if (body_loc_after_labels != UNKNOWN_LOCATION
14436 && next_tinfo.type != CPP_SEMICOLON)
14437 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
14438 guard_tinfo.location, guard_tinfo.keyword);
14440 /* Return the statement. */
14441 return statement;
14444 /* For some dependent statements (like `while (cond) statement'), we
14445 have already created a scope. Therefore, even if the dependent
14446 statement is a compound-statement, we do not want to create another
14447 scope. */
14449 static void
14450 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
14451 const token_indent_info &guard_tinfo)
14453 /* If the token is a `{', then we must take special action. */
14454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14456 token_indent_info body_tinfo
14457 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14458 location_t loc_after_labels = UNKNOWN_LOCATION;
14460 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
14461 &loc_after_labels);
14462 token_indent_info next_tinfo
14463 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
14464 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
14466 if (loc_after_labels != UNKNOWN_LOCATION
14467 && next_tinfo.type != CPP_SEMICOLON)
14468 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
14469 guard_tinfo.location,
14470 guard_tinfo.keyword);
14472 else
14474 /* Avoid calling cp_parser_compound_statement, so that we
14475 don't create a new scope. Do everything else by hand. */
14476 matching_braces braces;
14477 braces.require_open (parser);
14478 /* If the next keyword is `__label__' we have a label declaration. */
14479 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
14480 cp_parser_label_declaration (parser);
14481 /* Parse an (optional) statement-seq. */
14482 cp_parser_statement_seq_opt (parser, NULL_TREE);
14483 braces.require_close (parser);
14487 /* Modules */
14489 /* Parse a module-name,
14490 identifier
14491 module-name . identifier
14492 header-name
14494 Returns a pointer to module object, NULL. */
14496 static module_state *
14497 cp_parser_module_name (cp_parser *parser)
14499 cp_token *token = cp_lexer_peek_token (parser->lexer);
14500 if (token->type == CPP_HEADER_NAME)
14502 cp_lexer_consume_token (parser->lexer);
14504 return get_module (token->u.value);
14507 module_state *parent = NULL;
14508 bool partitioned = false;
14509 if (token->type == CPP_COLON && named_module_p ())
14511 partitioned = true;
14512 cp_lexer_consume_token (parser->lexer);
14515 for (;;)
14517 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME)
14519 cp_parser_error (parser, "expected module-name");
14520 break;
14523 tree name = cp_lexer_consume_token (parser->lexer)->u.value;
14524 parent = get_module (name, parent, partitioned);
14525 token = cp_lexer_peek_token (parser->lexer);
14526 if (!partitioned && token->type == CPP_COLON)
14527 partitioned = true;
14528 else if (token->type != CPP_DOT)
14529 break;
14531 cp_lexer_consume_token (parser->lexer);
14534 return parent;
14537 /* Named module-declaration
14538 __module ; PRAGMA_EOL
14539 __module private ; PRAGMA_EOL (unimplemented)
14540 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
14543 static module_parse
14544 cp_parser_module_declaration (cp_parser *parser, module_parse mp_state,
14545 bool exporting)
14547 /* We're a pseudo pragma. */
14548 parser->lexer->in_pragma = true;
14549 cp_token *token = cp_lexer_consume_token (parser->lexer);
14551 if (flag_header_unit)
14553 error_at (token->location,
14554 "module-declaration not permitted in header-unit");
14555 goto skip_eol;
14557 else if (mp_state == MP_FIRST && !exporting
14558 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14560 /* Start global module fragment. */
14561 cp_lexer_consume_token (parser->lexer);
14562 module_kind = MK_NAMED;
14563 mp_state = MP_GLOBAL;
14564 cp_parser_require_pragma_eol (parser, token);
14566 else if (!exporting
14567 && cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14568 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_PRIVATE)
14569 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_SEMICOLON))
14571 cp_lexer_consume_token (parser->lexer);
14572 cp_lexer_consume_token (parser->lexer);
14573 cp_lexer_consume_token (parser->lexer);
14574 cp_parser_require_pragma_eol (parser, token);
14576 if ((mp_state == MP_PURVIEW || mp_state == MP_PURVIEW_IMPORTS)
14577 && module_has_cmi_p ())
14579 mp_state = MP_PRIVATE_IMPORTS;
14580 sorry_at (token->location, "private module fragment");
14582 else
14583 error_at (token->location,
14584 "private module fragment only permitted in purview"
14585 " of module interface or partition");
14587 else if (!(mp_state == MP_FIRST || mp_state == MP_GLOBAL))
14589 /* Neither the first declaration, nor in a GMF. */
14590 error_at (token->location, "module-declaration only permitted as first"
14591 " declaration, or ending a global module fragment");
14592 skip_eol:
14593 cp_parser_skip_to_pragma_eol (parser, token);
14595 else
14597 module_state *mod = cp_parser_module_name (parser);
14598 tree attrs = cp_parser_attributes_opt (parser);
14600 mp_state = MP_PURVIEW_IMPORTS;
14601 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14602 goto skip_eol;
14604 declare_module (mod, token->location, exporting, attrs, parse_in);
14605 cp_parser_require_pragma_eol (parser, token);
14608 return mp_state;
14611 /* Import-declaration
14612 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
14614 static void
14615 cp_parser_import_declaration (cp_parser *parser, module_parse mp_state,
14616 bool exporting)
14618 /* We're a pseudo pragma. */
14619 parser->lexer->in_pragma = true;
14620 cp_token *token = cp_lexer_consume_token (parser->lexer);
14622 if (mp_state == MP_PURVIEW || mp_state == MP_PRIVATE)
14624 error_at (token->location, "post-module-declaration"
14625 " imports must be contiguous");
14626 note_lexer:
14627 inform (token->location, "perhaps insert a line break, or other"
14628 " disambiguation, to prevent this being considered a"
14629 " module control-line");
14630 skip_eol:
14631 cp_parser_skip_to_pragma_eol (parser, token);
14633 else if (current_scope () != global_namespace)
14635 error_at (token->location, "import-declaration must be at global scope");
14636 goto note_lexer;
14638 else
14640 module_state *mod = cp_parser_module_name (parser);
14641 tree attrs = cp_parser_attributes_opt (parser);
14643 if (!mod || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
14644 goto skip_eol;
14645 cp_parser_require_pragma_eol (parser, token);
14647 if (parser->in_unbraced_linkage_specification_p)
14648 error_at (token->location, "import cannot appear directly in"
14649 " a linkage-specification");
14651 if (mp_state == MP_PURVIEW_IMPORTS || mp_state == MP_PRIVATE_IMPORTS)
14653 /* Module-purview imports must not be from source inclusion
14654 [cpp.import]/7 */
14655 if (attrs
14656 && private_lookup_attribute ("__translated",
14657 strlen ("__translated"), attrs))
14658 error_at (token->location, "post-module-declaration imports"
14659 " must not be include-translated");
14660 else if (!token->main_source_p)
14661 error_at (token->location, "post-module-declaration imports"
14662 " must not be from header inclusion");
14665 import_module (mod, token->location, exporting, attrs, parse_in);
14669 /* export-declaration.
14671 export declaration
14672 export { declaration-seq-opt } */
14674 static void
14675 cp_parser_module_export (cp_parser *parser)
14677 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT));
14678 cp_token *token = cp_lexer_consume_token (parser->lexer);
14680 if (!module_interface_p ())
14681 error_at (token->location,
14682 "%qE may only occur after a module interface declaration",
14683 token->u.value);
14685 bool braced = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE);
14687 unsigned mk = module_kind;
14688 if (module_exporting_p ())
14689 error_at (token->location,
14690 "%qE may only occur once in an export declaration",
14691 token->u.value);
14692 module_kind |= MK_EXPORTING;
14694 if (braced)
14696 cp_ensure_no_omp_declare_simd (parser);
14697 cp_ensure_no_oacc_routine (parser);
14699 cp_lexer_consume_token (parser->lexer);
14700 cp_parser_declaration_seq_opt (parser);
14701 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14703 else
14705 /* Explicitly check if the next tokens might be a
14706 module-directive line, so we can give a clearer error message
14707 about why the directive will be rejected. */
14708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID__MODULE)
14709 || cp_lexer_next_token_is_keyword (parser->lexer, RID__IMPORT)
14710 || cp_lexer_next_token_is_keyword (parser->lexer, RID__EXPORT))
14711 error_at (token->location, "%<export%> not part of following"
14712 " module-directive");
14713 cp_parser_declaration (parser, NULL_TREE);
14716 module_kind = mk;
14719 /* Declarations [gram.dcl.dcl] */
14721 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
14722 is the top-level declaration sequence. That affects whether we
14723 deal with module-preamble.
14725 declaration-seq:
14726 declaration
14727 declaration-seq declaration */
14729 static void
14730 cp_parser_declaration_seq_opt (cp_parser* parser)
14732 while (true)
14734 cp_token *token = cp_lexer_peek_token (parser->lexer);
14736 if (token->type == CPP_CLOSE_BRACE
14737 || token->type == CPP_EOF)
14738 break;
14739 else
14740 cp_parser_toplevel_declaration (parser);
14744 /* Parse a declaration.
14746 declaration:
14747 block-declaration
14748 function-definition
14749 template-declaration
14750 explicit-instantiation
14751 explicit-specialization
14752 linkage-specification
14753 namespace-definition
14755 C++17:
14756 deduction-guide
14758 modules:
14759 (all these are only allowed at the outermost level, check
14760 that semantically, for better diagnostics)
14761 module-declaration
14762 module-export-declaration
14763 module-import-declaration
14764 export-declaration
14766 GNU extension:
14768 declaration:
14769 __extension__ declaration */
14771 static void
14772 cp_parser_declaration (cp_parser* parser, tree prefix_attrs)
14774 int saved_pedantic;
14776 /* Check for the `__extension__' keyword. */
14777 if (cp_parser_extension_opt (parser, &saved_pedantic))
14779 /* Parse the qualified declaration. */
14780 cp_parser_declaration (parser, prefix_attrs);
14781 /* Restore the PEDANTIC flag. */
14782 pedantic = saved_pedantic;
14784 return;
14787 /* Try to figure out what kind of declaration is present. */
14788 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
14789 cp_token *token2 = (token1->type == CPP_EOF
14790 ? token1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14792 if (token1->type == CPP_SEMICOLON)
14794 cp_lexer_consume_token (parser->lexer);
14795 /* A declaration consisting of a single semicolon is invalid
14796 * before C++11. Allow it unless we're being pedantic. */
14797 if (cxx_dialect < cxx11)
14798 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
14799 return;
14801 else if (cp_lexer_nth_token_is (parser->lexer,
14802 cp_parser_skip_std_attribute_spec_seq (parser,
14804 CPP_SEMICOLON))
14806 location_t attrs_loc = token1->location;
14807 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
14809 if (std_attrs && (flag_openmp || flag_openmp_simd))
14811 gcc_assert (!parser->lexer->in_omp_attribute_pragma);
14812 std_attrs = cp_parser_handle_statement_omp_attributes (parser,
14813 std_attrs);
14814 if (parser->lexer->in_omp_attribute_pragma)
14816 cp_lexer *lexer = parser->lexer;
14817 while (parser->lexer->in_omp_attribute_pragma)
14819 gcc_assert (cp_lexer_next_token_is (parser->lexer,
14820 CPP_PRAGMA));
14821 cp_parser_pragma (parser, pragma_external, NULL);
14823 cp_lexer_destroy (lexer);
14827 if (std_attrs != NULL_TREE && !attribute_ignored_p (std_attrs))
14828 warning_at (make_location (attrs_loc, attrs_loc, parser->lexer),
14829 OPT_Wattributes, "attribute ignored");
14830 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14831 cp_lexer_consume_token (parser->lexer);
14832 return;
14835 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
14836 void *p = obstack_alloc (&declarator_obstack, 0);
14838 tree attributes = NULL_TREE;
14840 /* Conditionally, allow attributes to precede a linkage specification. */
14841 if (token1->keyword == RID_ATTRIBUTE)
14843 cp_lexer_save_tokens (parser->lexer);
14844 attributes = cp_parser_attributes_opt (parser);
14845 cp_token *t1 = cp_lexer_peek_token (parser->lexer);
14846 cp_token *t2 = (t1->type == CPP_EOF
14847 ? t1 : cp_lexer_peek_nth_token (parser->lexer, 2));
14848 if (t1->keyword == RID_EXTERN
14849 && cp_parser_is_pure_string_literal (t2))
14851 cp_lexer_commit_tokens (parser->lexer);
14852 /* We might have already been here. */
14853 if (!c_dialect_objc ())
14855 location_t where = get_finish (t2->location);
14856 warning_at (token1->location, OPT_Wattributes, "attributes are"
14857 " not permitted in this position");
14858 where = linemap_position_for_loc_and_offset (line_table,
14859 where, 1);
14860 inform (where, "attributes may be inserted here");
14861 attributes = NULL_TREE;
14863 token1 = t1;
14864 token2 = t2;
14866 else
14868 cp_lexer_rollback_tokens (parser->lexer);
14869 attributes = NULL_TREE;
14872 /* If we already had some attributes, and we've added more, then prepend.
14873 Otherwise attributes just contains any that we just read. */
14874 if (prefix_attrs)
14876 if (attributes)
14877 TREE_CHAIN (prefix_attrs) = attributes;
14878 attributes = prefix_attrs;
14881 /* If the next token is `extern' and the following token is a string
14882 literal, then we have a linkage specification. */
14883 if (token1->keyword == RID_EXTERN
14884 && cp_parser_is_pure_string_literal (token2))
14885 cp_parser_linkage_specification (parser, attributes);
14886 /* If the next token is `template', then we have either a template
14887 declaration, an explicit instantiation, or an explicit
14888 specialization. */
14889 else if (token1->keyword == RID_TEMPLATE)
14891 /* `template <>' indicates a template specialization. */
14892 if (token2->type == CPP_LESS
14893 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14894 cp_parser_explicit_specialization (parser);
14895 /* `template <' indicates a template declaration. */
14896 else if (token2->type == CPP_LESS)
14897 cp_parser_template_declaration (parser, /*member_p=*/false);
14898 /* Anything else must be an explicit instantiation. */
14899 else
14900 cp_parser_explicit_instantiation (parser);
14902 /* If the next token is `export', it's new-style modules or
14903 old-style template. */
14904 else if (token1->keyword == RID_EXPORT)
14906 if (!modules_p ())
14907 cp_parser_template_declaration (parser, /*member_p=*/false);
14908 else
14909 cp_parser_module_export (parser);
14911 else if (cp_token_is_module_directive (token1))
14913 bool exporting = token1->keyword == RID__EXPORT;
14914 cp_token *next = exporting ? token2 : token1;
14915 if (exporting)
14916 cp_lexer_consume_token (parser->lexer);
14917 // In module purview this will be ill-formed.
14918 auto state = (!named_module_p () ? MP_NOT_MODULE
14919 : module_purview_p () ? MP_PURVIEW
14920 : MP_GLOBAL);
14921 if (next->keyword == RID__MODULE)
14922 cp_parser_module_declaration (parser, state, exporting);
14923 else
14924 cp_parser_import_declaration (parser, state, exporting);
14926 /* If the next token is `extern', 'static' or 'inline' and the one
14927 after that is `template', we have a GNU extended explicit
14928 instantiation directive. */
14929 else if (cp_parser_allow_gnu_extensions_p (parser)
14930 && token2->keyword == RID_TEMPLATE
14931 && (token1->keyword == RID_EXTERN
14932 || token1->keyword == RID_STATIC
14933 || token1->keyword == RID_INLINE))
14934 cp_parser_explicit_instantiation (parser);
14935 /* If the next token is `namespace', check for a named or unnamed
14936 namespace definition. */
14937 else if (token1->keyword == RID_NAMESPACE
14938 && (/* A named namespace definition. */
14939 (token2->type == CPP_NAME
14940 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
14941 != CPP_EQ))
14942 || (token2->type == CPP_OPEN_SQUARE
14943 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
14944 == CPP_OPEN_SQUARE)
14945 /* An unnamed namespace definition. */
14946 || token2->type == CPP_OPEN_BRACE
14947 || token2->keyword == RID_ATTRIBUTE))
14948 cp_parser_namespace_definition (parser);
14949 /* An inline (associated) namespace definition. */
14950 else if (token2->keyword == RID_NAMESPACE
14951 && token1->keyword == RID_INLINE)
14952 cp_parser_namespace_definition (parser);
14953 /* Objective-C++ declaration/definition. */
14954 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1->keyword))
14955 cp_parser_objc_declaration (parser, attributes);
14956 else if (c_dialect_objc ()
14957 && token1->keyword == RID_ATTRIBUTE
14958 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
14959 cp_parser_objc_declaration (parser, attributes);
14960 /* At this point we may have a template declared by a concept
14961 introduction. */
14962 else if (flag_concepts
14963 && cp_parser_template_declaration_after_export (parser,
14964 /*member_p=*/false))
14965 /* We did. */;
14966 else
14967 /* Try to parse a block-declaration, or a function-definition. */
14968 cp_parser_block_declaration (parser, /*statement_p=*/false);
14970 /* Free any declarators allocated. */
14971 obstack_free (&declarator_obstack, p);
14974 /* Parse a namespace-scope declaration. */
14976 static void
14977 cp_parser_toplevel_declaration (cp_parser* parser)
14979 cp_token *token = cp_lexer_peek_token (parser->lexer);
14981 if (token->type == CPP_PRAGMA)
14982 /* A top-level declaration can consist solely of a #pragma. A
14983 nested declaration cannot, so this is done here and not in
14984 cp_parser_declaration. (A #pragma at block scope is
14985 handled in cp_parser_statement.) */
14986 cp_parser_pragma (parser, pragma_external, NULL);
14987 else
14988 /* Parse the declaration itself. */
14989 cp_parser_declaration (parser, NULL_TREE);
14992 /* Parse a block-declaration.
14994 block-declaration:
14995 simple-declaration
14996 asm-definition
14997 namespace-alias-definition
14998 using-declaration
14999 using-directive
15001 GNU Extension:
15003 block-declaration:
15004 __extension__ block-declaration
15006 C++0x Extension:
15008 block-declaration:
15009 static_assert-declaration
15011 If STATEMENT_P is TRUE, then this block-declaration is occurring as
15012 part of a declaration-statement. */
15014 static void
15015 cp_parser_block_declaration (cp_parser *parser,
15016 bool statement_p)
15018 int saved_pedantic;
15020 /* Check for the `__extension__' keyword. */
15021 if (cp_parser_extension_opt (parser, &saved_pedantic))
15023 /* Parse the qualified declaration. */
15024 cp_parser_block_declaration (parser, statement_p);
15025 /* Restore the PEDANTIC flag. */
15026 pedantic = saved_pedantic;
15028 return;
15031 /* Peek at the next token to figure out which kind of declaration is
15032 present. */
15033 cp_token *token1 = cp_lexer_peek_token (parser->lexer);
15034 size_t attr_idx;
15036 /* If the next keyword is `asm', we have an asm-definition. */
15037 if (token1->keyword == RID_ASM)
15039 if (statement_p)
15040 cp_parser_commit_to_tentative_parse (parser);
15041 cp_parser_asm_definition (parser);
15043 /* If the next keyword is `namespace', we have a
15044 namespace-alias-definition. */
15045 else if (token1->keyword == RID_NAMESPACE)
15046 cp_parser_namespace_alias_definition (parser);
15047 /* If the next keyword is `using', we have a
15048 using-declaration, a using-directive, or an alias-declaration. */
15049 else if (token1->keyword == RID_USING)
15051 cp_token *token2;
15053 if (statement_p)
15054 cp_parser_commit_to_tentative_parse (parser);
15055 /* If the token after `using' is `namespace', then we have a
15056 using-directive. */
15057 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15058 if (token2->keyword == RID_NAMESPACE)
15059 cp_parser_using_directive (parser);
15060 else if (token2->keyword == RID_ENUM)
15061 cp_parser_using_enum (parser);
15062 /* If the second token after 'using' is '=', then we have an
15063 alias-declaration. */
15064 else if (cxx_dialect >= cxx11
15065 && token2->type == CPP_NAME
15066 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
15067 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
15068 cp_parser_alias_declaration (parser);
15069 /* Otherwise, it's a using-declaration. */
15070 else
15071 cp_parser_using_declaration (parser,
15072 /*access_declaration_p=*/false);
15074 /* If the next keyword is `__label__' we have a misplaced label
15075 declaration. */
15076 else if (token1->keyword == RID_LABEL)
15078 cp_lexer_consume_token (parser->lexer);
15079 error_at (token1->location, "%<__label__%> not at the beginning of a block");
15080 cp_parser_skip_to_end_of_statement (parser);
15081 /* If the next token is now a `;', consume it. */
15082 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15083 cp_lexer_consume_token (parser->lexer);
15085 /* If the next token is `static_assert' we have a static assertion. */
15086 else if (token1->keyword == RID_STATIC_ASSERT)
15087 cp_parser_static_assert (parser, /*member_p=*/false);
15088 /* If the next tokens after attributes is `using namespace', then we have
15089 a using-directive. */
15090 else if ((attr_idx = cp_parser_skip_std_attribute_spec_seq (parser, 1)) != 1
15091 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx,
15092 RID_USING)
15093 && cp_lexer_nth_token_is_keyword (parser->lexer, attr_idx + 1,
15094 RID_NAMESPACE))
15096 if (statement_p)
15097 cp_parser_commit_to_tentative_parse (parser);
15098 cp_parser_using_directive (parser);
15100 /* Anything else must be a simple-declaration. */
15101 else
15102 cp_parser_simple_declaration (parser, !statement_p,
15103 /*maybe_range_for_decl*/NULL);
15106 /* Parse a simple-declaration.
15108 simple-declaration:
15109 decl-specifier-seq [opt] init-declarator-list [opt] ;
15110 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15111 brace-or-equal-initializer ;
15113 init-declarator-list:
15114 init-declarator
15115 init-declarator-list , init-declarator
15117 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
15118 function-definition as a simple-declaration.
15120 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15121 parsed declaration if it is an uninitialized single declarator not followed
15122 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15123 if present, will not be consumed. */
15125 static void
15126 cp_parser_simple_declaration (cp_parser* parser,
15127 bool function_definition_allowed_p,
15128 tree *maybe_range_for_decl)
15130 cp_decl_specifier_seq decl_specifiers;
15131 int declares_class_or_enum;
15132 bool saw_declarator;
15133 location_t comma_loc = UNKNOWN_LOCATION;
15134 location_t init_loc = UNKNOWN_LOCATION;
15136 if (maybe_range_for_decl)
15137 *maybe_range_for_decl = NULL_TREE;
15139 /* Defer access checks until we know what is being declared; the
15140 checks for names appearing in the decl-specifier-seq should be
15141 done as if we were in the scope of the thing being declared. */
15142 push_deferring_access_checks (dk_deferred);
15144 /* Parse the decl-specifier-seq. We have to keep track of whether
15145 or not the decl-specifier-seq declares a named class or
15146 enumeration type, since that is the only case in which the
15147 init-declarator-list is allowed to be empty.
15149 [dcl.dcl]
15151 In a simple-declaration, the optional init-declarator-list can be
15152 omitted only when declaring a class or enumeration, that is when
15153 the decl-specifier-seq contains either a class-specifier, an
15154 elaborated-type-specifier, or an enum-specifier. */
15155 cp_parser_decl_specifier_seq (parser,
15156 CP_PARSER_FLAGS_OPTIONAL,
15157 &decl_specifiers,
15158 &declares_class_or_enum);
15159 /* We no longer need to defer access checks. */
15160 stop_deferring_access_checks ();
15162 cp_omp_declare_simd_data odsd;
15163 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
15164 cp_parser_handle_directive_omp_attributes (parser,
15165 &decl_specifiers.attributes,
15166 &odsd, true);
15168 /* In a block scope, a valid declaration must always have a
15169 decl-specifier-seq. By not trying to parse declarators, we can
15170 resolve the declaration/expression ambiguity more quickly. */
15171 if (!function_definition_allowed_p
15172 && !decl_specifiers.any_specifiers_p)
15174 cp_parser_error (parser, "expected declaration");
15175 goto done;
15178 /* If the next two tokens are both identifiers, the code is
15179 erroneous. The usual cause of this situation is code like:
15181 T t;
15183 where "T" should name a type -- but does not. */
15184 if (!decl_specifiers.any_type_specifiers_p
15185 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15187 /* If parsing tentatively, we should commit; we really are
15188 looking at a declaration. */
15189 cp_parser_commit_to_tentative_parse (parser);
15190 /* Give up. */
15191 goto done;
15194 cp_parser_maybe_commit_to_declaration (parser, &decl_specifiers);
15196 /* Look for C++17 decomposition declaration. */
15197 for (size_t n = 1; ; n++)
15198 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
15199 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
15200 continue;
15201 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
15202 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
15203 && decl_specifiers.any_specifiers_p)
15205 tree decl
15206 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
15207 maybe_range_for_decl,
15208 &init_loc);
15210 /* The next token should be either a `,' or a `;'. */
15211 cp_token *token = cp_lexer_peek_token (parser->lexer);
15212 /* If it's a `;', we are done. */
15213 if (token->type == CPP_SEMICOLON)
15214 goto finish;
15215 else if (maybe_range_for_decl)
15217 if (*maybe_range_for_decl == NULL_TREE)
15218 *maybe_range_for_decl = error_mark_node;
15219 goto finish;
15221 /* Anything else is an error. */
15222 else
15224 /* If we have already issued an error message we don't need
15225 to issue another one. */
15226 if ((decl != error_mark_node
15227 && DECL_INITIAL (decl) != error_mark_node)
15228 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15229 cp_parser_error (parser, "expected %<;%>");
15230 /* Skip tokens until we reach the end of the statement. */
15231 cp_parser_skip_to_end_of_statement (parser);
15232 /* If the next token is now a `;', consume it. */
15233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15234 cp_lexer_consume_token (parser->lexer);
15235 goto done;
15238 else
15239 break;
15241 tree last_type;
15242 bool auto_specifier_p;
15243 /* NULL_TREE if both variable and function declaration are allowed,
15244 error_mark_node if function declaration are not allowed and
15245 a FUNCTION_DECL that should be diagnosed if it is followed by
15246 variable declarations. */
15247 tree auto_function_declaration;
15249 last_type = NULL_TREE;
15250 auto_specifier_p
15251 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
15252 auto_function_declaration = NULL_TREE;
15254 /* Keep going until we hit the `;' at the end of the simple
15255 declaration. */
15256 saw_declarator = false;
15257 while (cp_lexer_next_token_is_not (parser->lexer,
15258 CPP_SEMICOLON))
15260 cp_token *token;
15261 bool function_definition_p;
15262 tree decl;
15263 tree auto_result = NULL_TREE;
15265 if (saw_declarator)
15267 /* If we are processing next declarator, comma is expected */
15268 token = cp_lexer_peek_token (parser->lexer);
15269 gcc_assert (token->type == CPP_COMMA);
15270 cp_lexer_consume_token (parser->lexer);
15271 if (maybe_range_for_decl)
15273 *maybe_range_for_decl = error_mark_node;
15274 if (comma_loc == UNKNOWN_LOCATION)
15275 comma_loc = token->location;
15278 else
15279 saw_declarator = true;
15281 /* Parse the init-declarator. */
15282 decl = cp_parser_init_declarator (parser,
15283 CP_PARSER_FLAGS_NONE,
15284 &decl_specifiers,
15285 /*checks=*/NULL,
15286 function_definition_allowed_p,
15287 /*member_p=*/false,
15288 declares_class_or_enum,
15289 &function_definition_p,
15290 maybe_range_for_decl,
15291 &init_loc,
15292 &auto_result);
15293 /* If an error occurred while parsing tentatively, exit quickly.
15294 (That usually happens when in the body of a function; each
15295 statement is treated as a declaration-statement until proven
15296 otherwise.) */
15297 if (cp_parser_error_occurred (parser))
15298 goto done;
15300 if (auto_specifier_p && cxx_dialect >= cxx14)
15302 /* If the init-declarator-list contains more than one
15303 init-declarator, they shall all form declarations of
15304 variables. */
15305 if (auto_function_declaration == NULL_TREE)
15306 auto_function_declaration
15307 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
15308 else if (TREE_CODE (decl) == FUNCTION_DECL
15309 || auto_function_declaration != error_mark_node)
15311 error_at (decl_specifiers.locations[ds_type_spec],
15312 "non-variable %qD in declaration with more than one "
15313 "declarator with placeholder type",
15314 TREE_CODE (decl) == FUNCTION_DECL
15315 ? decl : auto_function_declaration);
15316 auto_function_declaration = error_mark_node;
15320 if (auto_result
15321 && (!processing_template_decl || !type_uses_auto (auto_result)))
15323 if (last_type
15324 && last_type != error_mark_node
15325 && !same_type_p (auto_result, last_type))
15327 /* If the list of declarators contains more than one declarator,
15328 the type of each declared variable is determined as described
15329 above. If the type deduced for the template parameter U is not
15330 the same in each deduction, the program is ill-formed. */
15331 error_at (decl_specifiers.locations[ds_type_spec],
15332 "inconsistent deduction for %qT: %qT and then %qT",
15333 decl_specifiers.type, last_type, auto_result);
15334 last_type = error_mark_node;
15336 else
15337 last_type = auto_result;
15340 /* Handle function definitions specially. */
15341 if (function_definition_p)
15343 /* If the next token is a `,', then we are probably
15344 processing something like:
15346 void f() {}, *p;
15348 which is erroneous. */
15349 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15351 cp_token *token = cp_lexer_peek_token (parser->lexer);
15352 error_at (token->location,
15353 "mixing"
15354 " declarations and function-definitions is forbidden");
15356 /* Otherwise, we're done with the list of declarators. */
15357 else
15359 pop_deferring_access_checks ();
15360 cp_finalize_omp_declare_simd (parser, &odsd);
15361 return;
15364 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
15365 *maybe_range_for_decl = decl;
15366 /* The next token should be either a `,' or a `;'. */
15367 token = cp_lexer_peek_token (parser->lexer);
15368 /* If it's a `,', there are more declarators to come. */
15369 if (token->type == CPP_COMMA)
15370 /* will be consumed next time around */;
15371 /* If it's a `;', we are done. */
15372 else if (token->type == CPP_SEMICOLON)
15373 break;
15374 else if (maybe_range_for_decl)
15376 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
15377 permerror (decl_specifiers.locations[ds_type_spec],
15378 "types may not be defined in a for-range-declaration");
15379 break;
15381 /* Anything else is an error. */
15382 else
15384 /* If we have already issued an error message we don't need
15385 to issue another one. */
15386 if ((decl != error_mark_node
15387 && DECL_INITIAL (decl) != error_mark_node)
15388 || cp_parser_uncommitted_to_tentative_parse_p (parser))
15389 cp_parser_error (parser, "expected %<,%> or %<;%>");
15390 /* Skip tokens until we reach the end of the statement. */
15391 cp_parser_skip_to_end_of_statement (parser);
15392 /* If the next token is now a `;', consume it. */
15393 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15394 cp_lexer_consume_token (parser->lexer);
15395 goto done;
15397 /* After the first time around, a function-definition is not
15398 allowed -- even if it was OK at first. For example:
15400 int i, f() {}
15402 is not valid. */
15403 function_definition_allowed_p = false;
15406 /* Issue an error message if no declarators are present, and the
15407 decl-specifier-seq does not itself declare a class or
15408 enumeration: [dcl.dcl]/3. */
15409 if (!saw_declarator)
15411 if (cp_parser_declares_only_class_p (parser))
15413 if (!declares_class_or_enum
15414 && decl_specifiers.type
15415 && OVERLOAD_TYPE_P (decl_specifiers.type))
15416 /* Ensure an error is issued anyway when finish_decltype_type,
15417 called via cp_parser_decl_specifier_seq, returns a class or
15418 an enumeration (c++/51786). */
15419 decl_specifiers.type = NULL_TREE;
15420 shadow_tag (&decl_specifiers);
15422 /* Perform any deferred access checks. */
15423 perform_deferred_access_checks (tf_warning_or_error);
15426 /* Consume the `;'. */
15427 finish:
15428 if (!maybe_range_for_decl)
15429 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15430 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15432 if (init_loc != UNKNOWN_LOCATION)
15433 error_at (init_loc, "initializer in range-based %<for%> loop");
15434 if (comma_loc != UNKNOWN_LOCATION)
15435 error_at (comma_loc,
15436 "multiple declarations in range-based %<for%> loop");
15439 done:
15440 pop_deferring_access_checks ();
15441 cp_finalize_omp_declare_simd (parser, &odsd);
15444 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
15445 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
15446 initializer ; */
15448 static tree
15449 cp_parser_decomposition_declaration (cp_parser *parser,
15450 cp_decl_specifier_seq *decl_specifiers,
15451 tree *maybe_range_for_decl,
15452 location_t *init_loc)
15454 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
15455 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
15456 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
15458 /* Parse the identifier-list. */
15459 auto_vec<cp_expr, 10> v;
15460 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15461 while (true)
15463 cp_expr e = cp_parser_identifier (parser);
15464 if (e.get_value () == error_mark_node)
15465 break;
15466 v.safe_push (e);
15467 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15468 break;
15469 cp_lexer_consume_token (parser->lexer);
15472 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
15473 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15475 end_loc = UNKNOWN_LOCATION;
15476 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
15477 false);
15478 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
15479 cp_lexer_consume_token (parser->lexer);
15480 else
15482 cp_parser_skip_to_end_of_statement (parser);
15483 return error_mark_node;
15487 if (cxx_dialect < cxx17)
15488 pedwarn (loc, OPT_Wc__17_extensions,
15489 "structured bindings only available with "
15490 "%<-std=c++17%> or %<-std=gnu++17%>");
15492 tree pushed_scope;
15493 cp_declarator *declarator = make_declarator (cdk_decomp);
15494 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
15495 declarator->id_loc = loc;
15496 if (ref_qual != REF_QUAL_NONE)
15497 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
15498 ref_qual == REF_QUAL_RVALUE,
15499 NULL_TREE);
15500 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
15501 NULL_TREE, decl_specifiers->attributes,
15502 &pushed_scope);
15503 tree orig_decl = decl;
15505 unsigned int i;
15506 cp_expr e;
15507 cp_decl_specifier_seq decl_specs;
15508 clear_decl_specs (&decl_specs);
15509 decl_specs.type = make_auto ();
15510 tree prev = decl;
15511 FOR_EACH_VEC_ELT (v, i, e)
15513 if (i == 0)
15514 declarator = make_id_declarator (NULL_TREE, e.get_value (),
15515 sfk_none, e.get_location ());
15516 else
15518 declarator->u.id.unqualified_name = e.get_value ();
15519 declarator->id_loc = e.get_location ();
15521 tree elt_pushed_scope;
15522 tree decl2 = start_decl (declarator, &decl_specs, SD_DECOMPOSITION,
15523 NULL_TREE, NULL_TREE, &elt_pushed_scope);
15524 if (decl2 == error_mark_node)
15525 decl = error_mark_node;
15526 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
15528 /* Ensure we've diagnosed redeclaration if we aren't creating
15529 a new VAR_DECL. */
15530 gcc_assert (errorcount);
15531 decl = error_mark_node;
15533 else
15534 prev = decl2;
15535 if (elt_pushed_scope)
15536 pop_scope (elt_pushed_scope);
15539 if (v.is_empty ())
15541 error_at (loc, "empty structured binding declaration");
15542 decl = error_mark_node;
15545 if (maybe_range_for_decl == NULL
15546 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
15548 bool non_constant_p = false, is_direct_init = false;
15549 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
15550 tree initializer = cp_parser_initializer (parser, &is_direct_init,
15551 &non_constant_p);
15552 if (initializer == NULL_TREE
15553 || (TREE_CODE (initializer) == TREE_LIST
15554 && TREE_CHAIN (initializer))
15555 || (is_direct_init
15556 && BRACE_ENCLOSED_INITIALIZER_P (initializer)
15557 && CONSTRUCTOR_NELTS (initializer) != 1))
15559 error_at (loc, "invalid initializer for structured binding "
15560 "declaration");
15561 initializer = error_mark_node;
15564 if (decl != error_mark_node)
15566 cp_maybe_mangle_decomp (decl, prev, v.length ());
15567 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
15568 (is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15569 cp_finish_decomp (decl, prev, v.length ());
15572 else if (decl != error_mark_node)
15574 *maybe_range_for_decl = prev;
15575 /* Ensure DECL_VALUE_EXPR is created for all the decls but
15576 the underlying DECL. */
15577 cp_finish_decomp (decl, prev, v.length ());
15580 if (pushed_scope)
15581 pop_scope (pushed_scope);
15583 if (decl == error_mark_node && DECL_P (orig_decl))
15585 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
15586 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
15589 return decl;
15592 /* Names of storage classes. */
15594 static const char *const
15595 cp_storage_class_name[] = {
15596 "", "auto", "register", "static", "extern", "mutable"
15599 /* Parse a decl-specifier-seq.
15601 decl-specifier-seq:
15602 decl-specifier-seq [opt] decl-specifier
15603 decl-specifier attribute-specifier-seq [opt] (C++11)
15605 decl-specifier:
15606 storage-class-specifier
15607 type-specifier
15608 function-specifier
15609 friend
15610 typedef
15612 GNU Extension:
15614 decl-specifier:
15615 attributes
15617 Concepts Extension:
15619 decl-specifier:
15620 concept
15622 Set *DECL_SPECS to a representation of the decl-specifier-seq.
15624 The parser flags FLAGS is used to control type-specifier parsing.
15626 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
15627 flags:
15629 1: one of the decl-specifiers is an elaborated-type-specifier
15630 (i.e., a type declaration)
15631 2: one of the decl-specifiers is an enum-specifier or a
15632 class-specifier (i.e., a type definition)
15636 static void
15637 cp_parser_decl_specifier_seq (cp_parser* parser,
15638 cp_parser_flags flags,
15639 cp_decl_specifier_seq *decl_specs,
15640 int* declares_class_or_enum)
15642 bool constructor_possible_p = !parser->in_declarator_p;
15643 bool found_decl_spec = false;
15644 cp_token *start_token = NULL;
15645 cp_decl_spec ds;
15647 /* Clear DECL_SPECS. */
15648 clear_decl_specs (decl_specs);
15650 /* Assume no class or enumeration type is declared. */
15651 *declares_class_or_enum = 0;
15653 /* Keep reading specifiers until there are no more to read. */
15654 while (true)
15656 bool constructor_p;
15657 cp_token *token;
15658 ds = ds_last;
15660 /* Peek at the next token. */
15661 token = cp_lexer_peek_token (parser->lexer);
15663 /* Save the first token of the decl spec list for error
15664 reporting. */
15665 if (!start_token)
15666 start_token = token;
15667 /* Handle attributes. */
15668 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) == 0
15669 && cp_next_tokens_can_be_attribute_p (parser))
15671 /* Parse the attributes. */
15672 tree attrs = cp_parser_attributes_opt (parser);
15674 /* In a sequence of declaration specifiers, c++11 attributes
15675 appertain to the type that precede them. In that case
15676 [dcl.spec]/1 says:
15678 The attribute-specifier-seq affects the type only for
15679 the declaration it appears in, not other declarations
15680 involving the same type.
15682 But for now let's force the user to position the
15683 attribute either at the beginning of the declaration or
15684 after the declarator-id, which would clearly mean that it
15685 applies to the declarator. */
15686 if (cxx11_attribute_p (attrs))
15688 if (!found_decl_spec)
15689 /* The c++11 attribute is at the beginning of the
15690 declaration. It appertains to the entity being
15691 declared. */;
15692 else
15694 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
15696 /* This is an attribute following a
15697 class-specifier. */
15698 if (decl_specs->type_definition_p)
15699 warn_misplaced_attr_for_class_type (token->location,
15700 decl_specs->type);
15701 attrs = NULL_TREE;
15703 else
15705 decl_specs->std_attributes
15706 = attr_chainon (decl_specs->std_attributes, attrs);
15707 if (decl_specs->locations[ds_std_attribute] == 0)
15708 decl_specs->locations[ds_std_attribute] = token->location;
15710 continue;
15714 decl_specs->attributes
15715 = attr_chainon (decl_specs->attributes, attrs);
15716 if (decl_specs->locations[ds_attribute] == 0)
15717 decl_specs->locations[ds_attribute] = token->location;
15718 continue;
15720 /* Assume we will find a decl-specifier keyword. */
15721 found_decl_spec = true;
15722 /* If the next token is an appropriate keyword, we can simply
15723 add it to the list. */
15724 switch (token->keyword)
15726 /* decl-specifier:
15727 friend
15728 constexpr
15729 constinit */
15730 case RID_FRIEND:
15731 if (!at_class_scope_p ())
15733 gcc_rich_location richloc (token->location);
15734 richloc.add_fixit_remove ();
15735 error_at (&richloc, "%<friend%> used outside of class");
15736 cp_lexer_purge_token (parser->lexer);
15738 else
15740 ds = ds_friend;
15741 /* Consume the token. */
15742 cp_lexer_consume_token (parser->lexer);
15744 break;
15746 case RID_CONSTEXPR:
15747 ds = ds_constexpr;
15748 cp_lexer_consume_token (parser->lexer);
15749 break;
15751 case RID_CONSTINIT:
15752 ds = ds_constinit;
15753 cp_lexer_consume_token (parser->lexer);
15754 break;
15756 case RID_CONSTEVAL:
15757 ds = ds_consteval;
15758 cp_lexer_consume_token (parser->lexer);
15759 break;
15761 case RID_CONCEPT:
15762 ds = ds_concept;
15763 cp_lexer_consume_token (parser->lexer);
15765 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15766 break;
15768 /* Warn for concept as a decl-specifier. We'll rewrite these as
15769 concept declarations later. */
15770 if (!flag_concepts_ts)
15772 cp_token *next = cp_lexer_peek_token (parser->lexer);
15773 if (next->keyword == RID_BOOL)
15774 pedwarn (next->location, 0, "the %<bool%> keyword is not "
15775 "allowed in a C++20 concept definition");
15776 else
15777 pedwarn (token->location, 0, "C++20 concept definition syntax "
15778 "is %<concept <name> = <expr>%>");
15781 /* In C++20 a concept definition is just 'concept name = expr;'
15782 Support that syntax as a TS extension by pretending we've seen
15783 the 'bool' specifier. */
15784 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15785 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ)
15786 && !decl_specs->any_type_specifiers_p)
15788 cp_parser_set_decl_spec_type (decl_specs, boolean_type_node,
15789 token, /*type_definition*/false);
15790 decl_specs->any_type_specifiers_p = true;
15792 break;
15794 /* function-specifier:
15795 inline
15796 virtual
15797 explicit */
15798 case RID_INLINE:
15799 case RID_VIRTUAL:
15800 case RID_EXPLICIT:
15801 cp_parser_function_specifier_opt (parser, decl_specs);
15802 break;
15804 /* decl-specifier:
15805 typedef */
15806 case RID_TYPEDEF:
15807 ds = ds_typedef;
15808 /* Consume the token. */
15809 cp_lexer_consume_token (parser->lexer);
15811 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15812 break;
15814 /* A constructor declarator cannot appear in a typedef. */
15815 constructor_possible_p = false;
15816 /* The "typedef" keyword can only occur in a declaration; we
15817 may as well commit at this point. */
15818 cp_parser_commit_to_tentative_parse (parser);
15820 if (decl_specs->storage_class != sc_none)
15822 if (decl_specs->conflicting_specifiers_p)
15823 break;
15824 gcc_rich_location richloc (token->location);
15825 location_t oloc = decl_specs->locations[ds_storage_class];
15826 richloc.add_location_if_nearby (oloc);
15827 error_at (&richloc,
15828 "%<typedef%> specifier conflicts with %qs",
15829 cp_storage_class_name[decl_specs->storage_class]);
15830 decl_specs->conflicting_specifiers_p = true;
15832 break;
15834 /* storage-class-specifier:
15835 auto
15836 register
15837 static
15838 extern
15839 mutable
15841 GNU Extension:
15842 thread */
15843 case RID_AUTO:
15844 if (cxx_dialect == cxx98)
15846 /* Consume the token. */
15847 cp_lexer_consume_token (parser->lexer);
15849 /* Complain about `auto' as a storage specifier, if
15850 we're complaining about C++0x compatibility. */
15851 gcc_rich_location richloc (token->location);
15852 richloc.add_fixit_remove ();
15853 warning_at (&richloc, OPT_Wc__11_compat,
15854 "%<auto%> changes meaning in C++11; "
15855 "please remove it");
15857 /* Set the storage class anyway. */
15858 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
15859 token);
15861 else
15862 /* C++0x auto type-specifier. */
15863 found_decl_spec = false;
15864 break;
15866 case RID_REGISTER:
15867 case RID_STATIC:
15868 case RID_EXTERN:
15869 case RID_MUTABLE:
15870 /* Consume the token. */
15871 cp_lexer_consume_token (parser->lexer);
15872 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
15873 token);
15874 break;
15875 case RID_THREAD:
15876 /* Consume the token. */
15877 ds = ds_thread;
15878 cp_lexer_consume_token (parser->lexer);
15879 break;
15881 default:
15882 /* We did not yet find a decl-specifier yet. */
15883 found_decl_spec = false;
15884 break;
15887 if (found_decl_spec
15888 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
15889 && token->keyword != RID_CONSTEXPR)
15890 error ("%qD invalid in condition", ridpointers[token->keyword]);
15892 if (found_decl_spec
15893 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15894 && token->keyword != RID_MUTABLE
15895 && token->keyword != RID_CONSTEXPR
15896 && token->keyword != RID_CONSTEVAL)
15898 if (token->keyword != RID_STATIC)
15899 error_at (token->location, "%qD invalid in lambda",
15900 ridpointers[token->keyword]);
15901 else if (cxx_dialect < cxx23)
15902 pedwarn (token->location, OPT_Wc__23_extensions,
15903 "%qD only valid in lambda with %<-std=c++23%> or "
15904 "%<-std=gnu++23%>", ridpointers[token->keyword]);
15907 if (ds != ds_last)
15908 set_and_check_decl_spec_loc (decl_specs, ds, token);
15910 /* Constructors are a special case. The `S' in `S()' is not a
15911 decl-specifier; it is the beginning of the declarator. */
15912 constructor_p
15913 = (!found_decl_spec
15914 && constructor_possible_p
15915 && (cp_parser_constructor_declarator_p
15916 (parser, flags, decl_spec_seq_has_spec_p (decl_specs,
15917 ds_friend))));
15919 /* If we don't have a DECL_SPEC yet, then we must be looking at
15920 a type-specifier. */
15921 if (!found_decl_spec && !constructor_p)
15923 int decl_spec_declares_class_or_enum;
15924 bool is_cv_qualifier;
15925 tree type_spec;
15927 if (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
15928 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15930 type_spec
15931 = cp_parser_type_specifier (parser, flags,
15932 decl_specs,
15933 /*is_declaration=*/true,
15934 &decl_spec_declares_class_or_enum,
15935 &is_cv_qualifier);
15936 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
15938 /* If this type-specifier referenced a user-defined type
15939 (a typedef, class-name, etc.), then we can't allow any
15940 more such type-specifiers henceforth.
15942 [dcl.spec]
15944 The longest sequence of decl-specifiers that could
15945 possibly be a type name is taken as the
15946 decl-specifier-seq of a declaration. The sequence shall
15947 be self-consistent as described below.
15949 [dcl.type]
15951 As a general rule, at most one type-specifier is allowed
15952 in the complete decl-specifier-seq of a declaration. The
15953 only exceptions are the following:
15955 -- const or volatile can be combined with any other
15956 type-specifier.
15958 -- signed or unsigned can be combined with char, long,
15959 short, or int.
15961 -- ..
15963 Example:
15965 typedef char* Pc;
15966 void g (const int Pc);
15968 Here, Pc is *not* part of the decl-specifier seq; it's
15969 the declarator. Therefore, once we see a type-specifier
15970 (other than a cv-qualifier), we forbid any additional
15971 user-defined types. We *do* still allow things like `int
15972 int' to be considered a decl-specifier-seq, and issue the
15973 error message later. */
15974 if (type_spec && !is_cv_qualifier)
15975 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15976 /* A constructor declarator cannot follow a type-specifier. */
15977 if (type_spec)
15979 constructor_possible_p = false;
15980 found_decl_spec = true;
15981 if (!is_cv_qualifier)
15982 decl_specs->any_type_specifiers_p = true;
15984 if ((flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR) != 0)
15985 error_at (token->location, "type-specifier invalid in lambda");
15989 /* If we still do not have a DECL_SPEC, then there are no more
15990 decl-specifiers. */
15991 if (!found_decl_spec)
15992 break;
15994 if (decl_specs->std_attributes)
15996 error_at (decl_specs->locations[ds_std_attribute],
15997 "standard attributes in middle of decl-specifiers");
15998 inform (decl_specs->locations[ds_std_attribute],
15999 "standard attributes must precede the decl-specifiers to "
16000 "apply to the declaration, or follow them to apply to "
16001 "the type");
16004 decl_specs->any_specifiers_p = true;
16005 /* After we see one decl-specifier, further decl-specifiers are
16006 always optional. */
16007 flags |= CP_PARSER_FLAGS_OPTIONAL;
16010 /* Don't allow a friend specifier with a class definition. */
16011 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
16012 && (*declares_class_or_enum & 2))
16013 error_at (decl_specs->locations[ds_friend],
16014 "class definition may not be declared a friend");
16017 /* Parse an (optional) storage-class-specifier.
16019 storage-class-specifier:
16020 auto
16021 register
16022 static
16023 extern
16024 mutable
16026 GNU Extension:
16028 storage-class-specifier:
16029 thread
16031 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
16033 static tree
16034 cp_parser_storage_class_specifier_opt (cp_parser* parser)
16036 switch (cp_lexer_peek_token (parser->lexer)->keyword)
16038 case RID_AUTO:
16039 if (cxx_dialect != cxx98)
16040 return NULL_TREE;
16041 /* Fall through for C++98. */
16042 gcc_fallthrough ();
16044 case RID_REGISTER:
16045 case RID_STATIC:
16046 case RID_EXTERN:
16047 case RID_MUTABLE:
16048 case RID_THREAD:
16049 /* Consume the token. */
16050 return cp_lexer_consume_token (parser->lexer)->u.value;
16052 default:
16053 return NULL_TREE;
16057 /* Parse an (optional) function-specifier.
16059 function-specifier:
16060 inline
16061 virtual
16062 explicit
16064 C++20 Extension:
16065 explicit(constant-expression)
16067 Returns an IDENTIFIER_NODE corresponding to the keyword used.
16068 Updates DECL_SPECS, if it is non-NULL. */
16070 static tree
16071 cp_parser_function_specifier_opt (cp_parser* parser,
16072 cp_decl_specifier_seq *decl_specs)
16074 cp_token *token = cp_lexer_peek_token (parser->lexer);
16075 switch (token->keyword)
16077 case RID_INLINE:
16078 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
16079 break;
16081 case RID_VIRTUAL:
16082 /* 14.5.2.3 [temp.mem]
16084 A member function template shall not be virtual. */
16085 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16086 && current_class_type)
16087 error_at (token->location, "templates may not be %<virtual%>");
16088 else
16089 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
16090 break;
16092 case RID_EXPLICIT:
16094 tree id = cp_lexer_consume_token (parser->lexer)->u.value;
16095 /* If we see '(', it's C++20 explicit(bool). */
16096 tree expr;
16097 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16099 matching_parens parens;
16100 parens.consume_open (parser);
16102 /* New types are not allowed in an explicit-specifier. */
16103 const char *saved_message
16104 = parser->type_definition_forbidden_message;
16105 parser->type_definition_forbidden_message
16106 = G_("types may not be defined in explicit-specifier");
16108 if (cxx_dialect < cxx20)
16109 pedwarn (token->location, OPT_Wc__20_extensions,
16110 "%<explicit(bool)%> only available with %<-std=c++20%> "
16111 "or %<-std=gnu++20%>");
16113 /* Parse the constant-expression. */
16114 expr = cp_parser_constant_expression (parser);
16116 /* Restore the saved message. */
16117 parser->type_definition_forbidden_message = saved_message;
16118 parens.require_close (parser);
16120 else
16121 /* The explicit-specifier explicit without a constant-expression is
16122 equivalent to the explicit-specifier explicit(true). */
16123 expr = boolean_true_node;
16125 /* [dcl.fct.spec]
16126 "the constant-expression, if supplied, shall be a contextually
16127 converted constant expression of type bool." */
16128 expr = build_explicit_specifier (expr, tf_warning_or_error);
16129 /* We could evaluate it -- mark the decl as appropriate. */
16130 if (expr == boolean_true_node)
16131 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
16132 else if (expr == boolean_false_node)
16133 /* Don't mark the decl as explicit. */;
16134 else if (decl_specs)
16135 /* The expression was value-dependent. Remember it so that we can
16136 substitute it later. */
16137 decl_specs->explicit_specifier = expr;
16138 return id;
16141 default:
16142 return NULL_TREE;
16145 /* Consume the token. */
16146 return cp_lexer_consume_token (parser->lexer)->u.value;
16149 /* Parse a linkage-specification.
16151 linkage-specification:
16152 extern string-literal { declaration-seq [opt] }
16153 extern string-literal declaration */
16155 static void
16156 cp_parser_linkage_specification (cp_parser* parser, tree prefix_attr)
16158 tree linkage;
16160 /* Look for the `extern' keyword. */
16161 cp_token *extern_token
16162 = cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
16164 /* Look for the string-literal. */
16165 cp_token *string_token = cp_lexer_peek_token (parser->lexer);
16166 linkage = cp_parser_string_literal (parser, false, false);
16168 /* Transform the literal into an identifier. If the literal is a
16169 wide-character string, or contains embedded NULs, then we can't
16170 handle it as the user wants. */
16171 if (linkage == error_mark_node
16172 || strlen (TREE_STRING_POINTER (linkage))
16173 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
16175 cp_parser_error (parser, "invalid linkage-specification");
16176 /* Assume C++ linkage. */
16177 linkage = lang_name_cplusplus;
16179 else
16180 linkage = get_identifier (TREE_STRING_POINTER (linkage));
16182 /* We're now using the new linkage. */
16183 unsigned saved_module = module_kind;
16184 module_kind &= ~MK_ATTACH;
16185 push_lang_context (linkage);
16187 /* Preserve the location of the innermost linkage specification,
16188 tracking the locations of nested specifications via a local. */
16189 location_t saved_location
16190 = parser->innermost_linkage_specification_location;
16191 /* Construct a location ranging from the start of the "extern" to
16192 the end of the string-literal, with the caret at the start, e.g.:
16193 extern "C" {
16194 ^~~~~~~~~~
16196 parser->innermost_linkage_specification_location
16197 = make_location (extern_token->location,
16198 extern_token->location,
16199 get_finish (string_token->location));
16201 /* If the next token is a `{', then we're using the first
16202 production. */
16203 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16205 cp_ensure_no_omp_declare_simd (parser);
16206 cp_ensure_no_oacc_routine (parser);
16208 /* Consume the `{' token. */
16209 matching_braces braces;
16210 braces.consume_open (parser);
16211 /* Parse the declarations. */
16212 cp_parser_declaration_seq_opt (parser);
16213 /* Look for the closing `}'. */
16214 braces.require_close (parser);
16216 /* Otherwise, there's just one declaration. */
16217 else
16219 bool saved_in_unbraced_linkage_specification_p;
16221 saved_in_unbraced_linkage_specification_p
16222 = parser->in_unbraced_linkage_specification_p;
16223 parser->in_unbraced_linkage_specification_p = true;
16224 cp_parser_declaration (parser, prefix_attr);
16225 parser->in_unbraced_linkage_specification_p
16226 = saved_in_unbraced_linkage_specification_p;
16229 /* We're done with the linkage-specification. */
16230 pop_lang_context ();
16231 module_kind = saved_module;
16233 /* Restore location of parent linkage specification, if any. */
16234 parser->innermost_linkage_specification_location = saved_location;
16237 /* Parse a static_assert-declaration.
16239 static_assert-declaration:
16240 static_assert ( constant-expression , string-literal ) ;
16241 static_assert ( constant-expression ) ; (C++17)
16243 If MEMBER_P, this static_assert is a class member. */
16245 static void
16246 cp_parser_static_assert(cp_parser *parser, bool member_p)
16248 cp_expr condition;
16249 location_t token_loc;
16250 tree message;
16251 bool dummy;
16253 /* Peek at the `static_assert' token so we can keep track of exactly
16254 where the static assertion started. */
16255 token_loc = cp_lexer_peek_token (parser->lexer)->location;
16257 /* Look for the `static_assert' keyword. */
16258 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
16259 RT_STATIC_ASSERT))
16260 return;
16262 /* We know we are in a static assertion; commit to any tentative
16263 parse. */
16264 if (cp_parser_parsing_tentatively (parser))
16265 cp_parser_commit_to_tentative_parse (parser);
16267 /* Parse the `(' starting the static assertion condition. */
16268 matching_parens parens;
16269 parens.require_open (parser);
16271 /* Parse the constant-expression. Allow a non-constant expression
16272 here in order to give better diagnostics in finish_static_assert. */
16273 condition =
16274 cp_parser_constant_expression (parser,
16275 /*allow_non_constant_p=*/true,
16276 /*non_constant_p=*/&dummy);
16278 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16280 if (pedantic && cxx_dialect < cxx17)
16281 pedwarn (input_location, OPT_Wc__17_extensions,
16282 "%<static_assert%> without a message "
16283 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
16284 /* Eat the ')' */
16285 cp_lexer_consume_token (parser->lexer);
16286 message = build_string (1, "");
16287 TREE_TYPE (message) = char_array_type_node;
16288 fix_string_type (message);
16290 else
16292 /* Parse the separating `,'. */
16293 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
16295 /* Parse the string-literal message. */
16296 message = cp_parser_string_literal (parser,
16297 /*translate=*/false,
16298 /*wide_ok=*/true);
16300 /* A `)' completes the static assertion. */
16301 if (!parens.require_close (parser))
16302 cp_parser_skip_to_closing_parenthesis (parser,
16303 /*recovering=*/true,
16304 /*or_comma=*/false,
16305 /*consume_paren=*/true);
16308 /* A semicolon terminates the declaration. */
16309 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
16311 /* Get the location for the static assertion. Use that of the
16312 condition if available, otherwise, use that of the "static_assert"
16313 token. */
16314 location_t assert_loc = condition.get_location ();
16315 if (assert_loc == UNKNOWN_LOCATION)
16316 assert_loc = token_loc;
16318 /* Complete the static assertion, which may mean either processing
16319 the static assert now or saving it for template instantiation. */
16320 finish_static_assert (condition, message, assert_loc, member_p,
16321 /*show_expr_p=*/false);
16324 /* Parse the expression in decltype ( expression ). */
16326 static tree
16327 cp_parser_decltype_expr (cp_parser *parser,
16328 bool &id_expression_or_member_access_p)
16330 cp_token *id_expr_start_token;
16331 tree expr;
16333 /* First, try parsing an id-expression. */
16334 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
16335 cp_parser_parse_tentatively (parser);
16336 expr = cp_parser_id_expression (parser,
16337 /*template_keyword_p=*/false,
16338 /*check_dependency_p=*/true,
16339 /*template_p=*/NULL,
16340 /*declarator_p=*/false,
16341 /*optional_p=*/false);
16343 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
16345 bool non_integral_constant_expression_p = false;
16346 tree id_expression = expr;
16347 cp_id_kind idk;
16348 const char *error_msg;
16350 if (identifier_p (expr))
16351 /* Lookup the name we got back from the id-expression. */
16352 expr = cp_parser_lookup_name_simple (parser, expr,
16353 id_expr_start_token->location);
16355 if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
16356 /* A template without args is not a complete id-expression. */
16357 expr = error_mark_node;
16359 if (expr
16360 && expr != error_mark_node
16361 && TREE_CODE (expr) != TYPE_DECL
16362 && (TREE_CODE (expr) != BIT_NOT_EXPR
16363 || !TYPE_P (TREE_OPERAND (expr, 0)))
16364 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16366 /* Complete lookup of the id-expression. */
16367 expr = (finish_id_expression
16368 (id_expression, expr, parser->scope, &idk,
16369 /*integral_constant_expression_p=*/false,
16370 /*allow_non_integral_constant_expression_p=*/true,
16371 &non_integral_constant_expression_p,
16372 /*template_p=*/false,
16373 /*done=*/true,
16374 /*address_p=*/false,
16375 /*template_arg_p=*/false,
16376 &error_msg,
16377 id_expr_start_token->location));
16379 if (expr == error_mark_node)
16380 /* We found an id-expression, but it was something that we
16381 should not have found. This is an error, not something
16382 we can recover from, so note that we found an
16383 id-expression and we'll recover as gracefully as
16384 possible. */
16385 id_expression_or_member_access_p = true;
16388 if (expr
16389 && expr != error_mark_node
16390 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16391 /* We have an id-expression. */
16392 id_expression_or_member_access_p = true;
16395 if (!id_expression_or_member_access_p)
16397 /* Abort the id-expression parse. */
16398 cp_parser_abort_tentative_parse (parser);
16400 /* Parsing tentatively, again. */
16401 cp_parser_parse_tentatively (parser);
16403 /* Parse a class member access. */
16404 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
16405 /*cast_p=*/false, /*decltype*/true,
16406 /*member_access_only_p=*/true, NULL);
16408 if (expr
16409 && expr != error_mark_node
16410 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
16411 /* We have an id-expression. */
16412 id_expression_or_member_access_p = true;
16415 if (id_expression_or_member_access_p)
16416 /* We have parsed the complete id-expression or member access. */
16417 cp_parser_parse_definitely (parser);
16418 else
16420 /* Abort our attempt to parse an id-expression or member access
16421 expression. */
16422 cp_parser_abort_tentative_parse (parser);
16424 /* Parse a full expression. */
16425 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
16426 /*decltype_p=*/true);
16429 return expr;
16432 /* Parse a `decltype' type. Returns the type.
16434 decltype-specifier:
16435 decltype ( expression )
16436 C++14:
16437 decltype ( auto ) */
16439 static tree
16440 cp_parser_decltype (cp_parser *parser)
16442 bool id_expression_or_member_access_p = false;
16443 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
16445 if (start_token->type == CPP_DECLTYPE)
16447 /* Already parsed. */
16448 cp_lexer_consume_token (parser->lexer);
16449 return saved_checks_value (start_token->u.tree_check_value);
16452 /* Look for the `decltype' token. */
16453 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
16454 return error_mark_node;
16456 /* Parse the opening `('. */
16457 matching_parens parens;
16458 if (!parens.require_open (parser))
16459 return error_mark_node;
16461 /* Since we're going to preserve any side-effects from this parse, set up a
16462 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16463 in the expression. */
16464 tentative_firewall firewall (parser);
16466 /* If in_declarator_p, a reparse as an expression might succeed (60361).
16467 Otherwise, commit now for better diagnostics. */
16468 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
16469 && !parser->in_declarator_p)
16470 cp_parser_commit_to_topmost_tentative_parse (parser);
16472 push_deferring_access_checks (dk_deferred);
16474 tree expr = NULL_TREE;
16476 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO)
16477 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
16479 /* decltype (auto) */
16480 cp_lexer_consume_token (parser->lexer);
16481 if (cxx_dialect < cxx14)
16483 error_at (start_token->location,
16484 "%<decltype(auto)%> type specifier only available with "
16485 "%<-std=c++14%> or %<-std=gnu++14%>");
16486 expr = error_mark_node;
16489 else
16491 /* decltype (expression) */
16493 /* Types cannot be defined in a `decltype' expression. Save away the
16494 old message and set the new one. */
16495 const char *saved_message = parser->type_definition_forbidden_message;
16496 parser->type_definition_forbidden_message
16497 = G_("types may not be defined in %<decltype%> expressions");
16499 /* The restrictions on constant-expressions do not apply inside
16500 decltype expressions. */
16501 bool saved_integral_constant_expression_p
16502 = parser->integral_constant_expression_p;
16503 bool saved_non_integral_constant_expression_p
16504 = parser->non_integral_constant_expression_p;
16505 parser->integral_constant_expression_p = false;
16507 /* Within a parenthesized expression, a `>' token is always
16508 the greater-than operator. */
16509 bool saved_greater_than_is_operator_p
16510 = parser->greater_than_is_operator_p;
16511 parser->greater_than_is_operator_p = true;
16513 /* Don't synthesize an implicit template type parameter here. This
16514 could happen with C++23 code like
16516 void f(decltype(new auto{0}));
16518 where we want to deduce the auto right away so that the parameter
16519 is of type 'int *'. */
16520 auto cleanup = make_temp_override
16521 (parser->auto_is_implicit_function_template_parm_p, false);
16523 /* Do not actually evaluate the expression. */
16524 ++cp_unevaluated_operand;
16526 /* Do not warn about problems with the expression. */
16527 ++c_inhibit_evaluation_warnings;
16529 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
16530 STRIP_ANY_LOCATION_WRAPPER (expr);
16532 /* Go back to evaluating expressions. */
16533 --cp_unevaluated_operand;
16534 --c_inhibit_evaluation_warnings;
16536 /* The `>' token might be the end of a template-id or
16537 template-parameter-list now. */
16538 parser->greater_than_is_operator_p
16539 = saved_greater_than_is_operator_p;
16541 /* Restore the old message and the integral constant expression
16542 flags. */
16543 parser->type_definition_forbidden_message = saved_message;
16544 parser->integral_constant_expression_p
16545 = saved_integral_constant_expression_p;
16546 parser->non_integral_constant_expression_p
16547 = saved_non_integral_constant_expression_p;
16550 /* Parse to the closing `)'. */
16551 if (expr == error_mark_node || !parens.require_close (parser))
16553 cp_parser_skip_to_closing_parenthesis (parser, true, false,
16554 /*consume_paren=*/true);
16555 expr = error_mark_node;
16558 /* If we got a parse error while tentative, bail out now. */
16559 if (cp_parser_error_occurred (parser))
16561 pop_deferring_access_checks ();
16562 return error_mark_node;
16565 if (!expr)
16566 /* Build auto. */
16567 expr = make_decltype_auto ();
16568 else
16569 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
16570 tf_warning_or_error);
16572 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
16573 it again. */
16574 start_token->type = CPP_DECLTYPE;
16575 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
16576 start_token->tree_check_p = true;
16577 start_token->u.tree_check_value->value = expr;
16578 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
16579 start_token->keyword = RID_MAX;
16581 location_t loc = start_token->location;
16582 loc = make_location (loc, loc, parser->lexer);
16583 start_token->location = loc;
16585 cp_lexer_purge_tokens_after (parser->lexer, start_token);
16587 pop_to_parent_deferring_access_checks ();
16589 return expr;
16592 /* Special member functions [gram.special] */
16594 /* Parse a conversion-function-id.
16596 conversion-function-id:
16597 operator conversion-type-id
16599 Returns an IDENTIFIER_NODE representing the operator. */
16601 static tree
16602 cp_parser_conversion_function_id (cp_parser* parser)
16604 tree type;
16605 tree saved_scope;
16606 tree saved_qualifying_scope;
16607 tree saved_object_scope;
16608 tree pushed_scope = NULL_TREE;
16610 /* Look for the `operator' token. */
16611 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
16612 return error_mark_node;
16613 /* When we parse the conversion-type-id, the current scope will be
16614 reset. However, we need that information in able to look up the
16615 conversion function later, so we save it here. */
16616 saved_scope = parser->scope;
16617 saved_qualifying_scope = parser->qualifying_scope;
16618 saved_object_scope = parser->object_scope;
16619 /* We must enter the scope of the class so that the names of
16620 entities declared within the class are available in the
16621 conversion-type-id. For example, consider:
16623 struct S {
16624 typedef int I;
16625 operator I();
16628 S::operator I() { ... }
16630 In order to see that `I' is a type-name in the definition, we
16631 must be in the scope of `S'. */
16632 if (saved_scope)
16633 pushed_scope = push_scope (saved_scope);
16634 /* Parse the conversion-type-id. */
16635 type = cp_parser_conversion_type_id (parser);
16636 /* Leave the scope of the class, if any. */
16637 if (pushed_scope)
16638 pop_scope (pushed_scope);
16639 /* Restore the saved scope. */
16640 parser->scope = saved_scope;
16641 parser->qualifying_scope = saved_qualifying_scope;
16642 parser->object_scope = saved_object_scope;
16643 /* If the TYPE is invalid, indicate failure. */
16644 if (type == error_mark_node)
16645 return error_mark_node;
16646 return make_conv_op_name (type);
16649 /* Parse a conversion-type-id:
16651 conversion-type-id:
16652 type-specifier-seq conversion-declarator [opt]
16654 Returns the TYPE specified. */
16656 static tree
16657 cp_parser_conversion_type_id (cp_parser* parser)
16659 tree attributes;
16660 cp_decl_specifier_seq type_specifiers;
16661 cp_declarator *declarator;
16662 tree type_specified;
16663 const char *saved_message;
16665 /* Parse the attributes. */
16666 attributes = cp_parser_attributes_opt (parser);
16668 saved_message = parser->type_definition_forbidden_message;
16669 parser->type_definition_forbidden_message
16670 = G_("types may not be defined in a conversion-type-id");
16672 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
16673 optional in conversion-type-id. */
16674 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
16675 /*is_declaration=*/false,
16676 /*is_trailing_return=*/false,
16677 &type_specifiers);
16679 parser->type_definition_forbidden_message = saved_message;
16681 /* If that didn't work, stop. */
16682 if (type_specifiers.type == error_mark_node)
16683 return error_mark_node;
16684 /* Parse the conversion-declarator. */
16685 declarator = cp_parser_conversion_declarator_opt (parser);
16687 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
16688 /*initialized=*/0, &attributes);
16689 if (attributes)
16690 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
16692 /* Don't give this error when parsing tentatively. This happens to
16693 work because we always parse this definitively once. */
16694 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
16695 && type_uses_auto (type_specified))
16697 if (cxx_dialect < cxx14)
16699 error ("invalid use of %<auto%> in conversion operator");
16700 return error_mark_node;
16702 else if (template_parm_scope_p ())
16703 warning (0, "use of %<auto%> in member template "
16704 "conversion operator can never be deduced");
16707 return type_specified;
16710 /* Parse an (optional) conversion-declarator.
16712 conversion-declarator:
16713 ptr-operator conversion-declarator [opt]
16717 static cp_declarator *
16718 cp_parser_conversion_declarator_opt (cp_parser* parser)
16720 enum tree_code code;
16721 tree class_type, std_attributes = NULL_TREE;
16722 cp_cv_quals cv_quals;
16724 /* We don't know if there's a ptr-operator next, or not. */
16725 cp_parser_parse_tentatively (parser);
16726 /* Try the ptr-operator. */
16727 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
16728 &std_attributes);
16729 /* If it worked, look for more conversion-declarators. */
16730 if (cp_parser_parse_definitely (parser))
16732 cp_declarator *declarator;
16734 /* Parse another optional declarator. */
16735 declarator = cp_parser_conversion_declarator_opt (parser);
16737 declarator = cp_parser_make_indirect_declarator
16738 (code, class_type, cv_quals, declarator, std_attributes);
16740 return declarator;
16743 return NULL;
16746 /* Parse an (optional) ctor-initializer.
16748 ctor-initializer:
16749 : mem-initializer-list */
16751 static void
16752 cp_parser_ctor_initializer_opt (cp_parser* parser)
16754 /* If the next token is not a `:', then there is no
16755 ctor-initializer. */
16756 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16758 /* Do default initialization of any bases and members. */
16759 if (DECL_CONSTRUCTOR_P (current_function_decl))
16760 finish_mem_initializers (NULL_TREE);
16761 return;
16764 /* Consume the `:' token. */
16765 cp_lexer_consume_token (parser->lexer);
16766 /* And the mem-initializer-list. */
16767 cp_parser_mem_initializer_list (parser);
16770 /* Parse a mem-initializer-list.
16772 mem-initializer-list:
16773 mem-initializer ... [opt]
16774 mem-initializer ... [opt] , mem-initializer-list */
16776 static void
16777 cp_parser_mem_initializer_list (cp_parser* parser)
16779 tree mem_initializer_list = NULL_TREE;
16780 tree target_ctor = error_mark_node;
16781 cp_token *token = cp_lexer_peek_token (parser->lexer);
16783 /* Let the semantic analysis code know that we are starting the
16784 mem-initializer-list. */
16785 if (!DECL_CONSTRUCTOR_P (current_function_decl))
16786 error_at (token->location,
16787 "only constructors take member initializers");
16789 /* Loop through the list. */
16790 while (true)
16792 tree mem_initializer;
16794 token = cp_lexer_peek_token (parser->lexer);
16795 /* Parse the mem-initializer. */
16796 mem_initializer = cp_parser_mem_initializer (parser);
16797 /* If the next token is a `...', we're expanding member initializers. */
16798 bool ellipsis = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
16799 if (ellipsis
16800 || (mem_initializer != error_mark_node
16801 && check_for_bare_parameter_packs (TREE_PURPOSE
16802 (mem_initializer))))
16804 /* Consume the `...'. */
16805 if (ellipsis)
16806 cp_lexer_consume_token (parser->lexer);
16808 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
16809 can be expanded but members cannot. */
16810 if (mem_initializer != error_mark_node
16811 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
16813 error_at (token->location,
16814 "cannot expand initializer for member %qD",
16815 TREE_PURPOSE (mem_initializer));
16816 mem_initializer = error_mark_node;
16819 /* Construct the pack expansion type. */
16820 if (mem_initializer != error_mark_node)
16821 mem_initializer = make_pack_expansion (mem_initializer);
16823 if (target_ctor != error_mark_node
16824 && mem_initializer != error_mark_node)
16826 error ("mem-initializer for %qD follows constructor delegation",
16827 TREE_PURPOSE (mem_initializer));
16828 mem_initializer = error_mark_node;
16830 /* Look for a target constructor. */
16831 if (mem_initializer != error_mark_node
16832 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
16833 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
16835 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
16836 if (mem_initializer_list)
16838 error ("constructor delegation follows mem-initializer for %qD",
16839 TREE_PURPOSE (mem_initializer_list));
16840 mem_initializer = error_mark_node;
16842 target_ctor = mem_initializer;
16844 /* Add it to the list, unless it was erroneous. */
16845 if (mem_initializer != error_mark_node)
16847 TREE_CHAIN (mem_initializer) = mem_initializer_list;
16848 mem_initializer_list = mem_initializer;
16850 /* If the next token is not a `,', we're done. */
16851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16852 break;
16853 /* Consume the `,' token. */
16854 cp_lexer_consume_token (parser->lexer);
16857 /* Perform semantic analysis. */
16858 if (DECL_CONSTRUCTOR_P (current_function_decl))
16859 finish_mem_initializers (mem_initializer_list);
16862 /* Parse a mem-initializer.
16864 mem-initializer:
16865 mem-initializer-id ( expression-list [opt] )
16866 mem-initializer-id braced-init-list
16868 GNU extension:
16870 mem-initializer:
16871 ( expression-list [opt] )
16873 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
16874 class) or FIELD_DECL (for a non-static data member) to initialize;
16875 the TREE_VALUE is the expression-list. An empty initialization
16876 list is represented by void_list_node. */
16878 static tree
16879 cp_parser_mem_initializer (cp_parser* parser)
16881 tree mem_initializer_id;
16882 tree expression_list;
16883 tree member;
16884 cp_token *token = cp_lexer_peek_token (parser->lexer);
16886 /* Find out what is being initialized. */
16887 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16889 permerror (token->location,
16890 "anachronistic old-style base class initializer");
16891 mem_initializer_id = NULL_TREE;
16893 else
16895 mem_initializer_id = cp_parser_mem_initializer_id (parser);
16896 if (mem_initializer_id == error_mark_node)
16897 return mem_initializer_id;
16899 member = expand_member_init (mem_initializer_id);
16900 if (member && !DECL_P (member))
16901 in_base_initializer = 1;
16903 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16905 bool expr_non_constant_p;
16906 cp_lexer_set_source_position (parser->lexer);
16907 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16908 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
16909 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
16910 expression_list = build_tree_list (NULL_TREE, expression_list);
16912 else
16914 vec<tree, va_gc> *vec;
16915 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16916 /*cast_p=*/false,
16917 /*allow_expansion_p=*/true,
16918 /*non_constant_p=*/NULL,
16919 /*close_paren_loc=*/NULL,
16920 /*wrap_locations_p=*/true);
16921 if (vec == NULL)
16922 return error_mark_node;
16923 expression_list = build_tree_list_vec (vec);
16924 release_tree_vector (vec);
16927 if (expression_list == error_mark_node)
16928 return error_mark_node;
16929 if (!expression_list)
16930 expression_list = void_type_node;
16932 in_base_initializer = 0;
16934 if (!member)
16935 return error_mark_node;
16936 tree node = build_tree_list (member, expression_list);
16938 /* We can't attach the source location of this initializer directly to
16939 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
16940 within the TREE_TYPE of the list node. */
16941 location_t loc
16942 = make_location (token->location, token->location, parser->lexer);
16943 tree dummy = build0 (EMPTY_CLASS_EXPR, NULL_TREE);
16944 SET_EXPR_LOCATION (dummy, loc);
16945 TREE_TYPE (node) = dummy;
16947 return node;
16950 /* Parse a mem-initializer-id.
16952 mem-initializer-id:
16953 :: [opt] nested-name-specifier [opt] class-name
16954 decltype-specifier (C++11)
16955 identifier
16957 Returns a TYPE indicating the class to be initialized for the first
16958 production (and the second in C++11). Returns an IDENTIFIER_NODE
16959 indicating the data member to be initialized for the last production. */
16961 static tree
16962 cp_parser_mem_initializer_id (cp_parser* parser)
16964 bool global_scope_p;
16965 bool nested_name_specifier_p;
16966 bool template_p = false;
16967 tree id;
16969 cp_token *token = cp_lexer_peek_token (parser->lexer);
16971 /* `typename' is not allowed in this context ([temp.res]). */
16972 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16974 error_at (token->location,
16975 "keyword %<typename%> not allowed in this context (a qualified "
16976 "member initializer is implicitly a type)");
16977 cp_lexer_consume_token (parser->lexer);
16979 /* Look for the optional `::' operator. */
16980 global_scope_p
16981 = (cp_parser_global_scope_opt (parser,
16982 /*current_scope_valid_p=*/false)
16983 != NULL_TREE);
16984 /* Look for the optional nested-name-specifier. The simplest way to
16985 implement:
16987 [temp.res]
16989 The keyword `typename' is not permitted in a base-specifier or
16990 mem-initializer; in these contexts a qualified name that
16991 depends on a template-parameter is implicitly assumed to be a
16992 type name.
16994 is to assume that we have seen the `typename' keyword at this
16995 point. */
16996 nested_name_specifier_p
16997 = (cp_parser_nested_name_specifier_opt (parser,
16998 /*typename_keyword_p=*/true,
16999 /*check_dependency_p=*/true,
17000 /*type_p=*/true,
17001 /*is_declaration=*/true)
17002 != NULL_TREE);
17003 if (nested_name_specifier_p)
17004 template_p = cp_parser_optional_template_keyword (parser);
17005 /* If there is a `::' operator or a nested-name-specifier, then we
17006 are definitely looking for a class-name. */
17007 if (global_scope_p || nested_name_specifier_p)
17008 return cp_parser_class_name (parser,
17009 /*typename_keyword_p=*/true,
17010 /*template_keyword_p=*/template_p,
17011 typename_type,
17012 /*check_dependency_p=*/true,
17013 /*class_head_p=*/false,
17014 /*is_declaration=*/true);
17015 /* Otherwise, we could also be looking for an ordinary identifier. */
17016 cp_parser_parse_tentatively (parser);
17017 if (cp_lexer_next_token_is_decltype (parser->lexer))
17018 /* Try a decltype-specifier. */
17019 id = cp_parser_decltype (parser);
17020 else
17021 /* Otherwise, try a class-name. */
17022 id = cp_parser_class_name (parser,
17023 /*typename_keyword_p=*/true,
17024 /*template_keyword_p=*/false,
17025 none_type,
17026 /*check_dependency_p=*/true,
17027 /*class_head_p=*/false,
17028 /*is_declaration=*/true);
17029 /* If we found one, we're done. */
17030 if (cp_parser_parse_definitely (parser))
17031 return id;
17032 /* Otherwise, look for an ordinary identifier. */
17033 return cp_parser_identifier (parser);
17036 /* Overloading [gram.over] */
17038 /* Parse an operator-function-id.
17040 operator-function-id:
17041 operator operator
17043 Returns an IDENTIFIER_NODE for the operator which is a
17044 human-readable spelling of the identifier, e.g., `operator +'. */
17046 static cp_expr
17047 cp_parser_operator_function_id (cp_parser* parser)
17049 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
17050 /* Look for the `operator' keyword. */
17051 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
17052 return error_mark_node;
17053 /* And then the name of the operator itself. */
17054 return cp_parser_operator (parser, start_loc);
17057 /* Return an identifier node for a user-defined literal operator.
17058 The suffix identifier is chained to the operator name identifier. */
17060 tree
17061 cp_literal_operator_id (const char* name)
17063 tree identifier;
17064 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
17065 + strlen (name) + 10);
17066 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
17067 identifier = get_identifier (buffer);
17068 XDELETEVEC (buffer);
17070 return identifier;
17073 /* Parse an operator.
17075 operator:
17076 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
17077 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
17078 || ++ -- , ->* -> () []
17080 GNU Extensions:
17082 operator:
17083 <? >? <?= >?=
17085 Returns an IDENTIFIER_NODE for the operator which is a
17086 human-readable spelling of the identifier, e.g., `operator +'. */
17088 static cp_expr
17089 cp_parser_operator (cp_parser* parser, location_t start_loc)
17091 tree id = NULL_TREE;
17092 cp_token *token;
17093 bool utf8 = false;
17095 /* Peek at the next token. */
17096 token = cp_lexer_peek_token (parser->lexer);
17098 location_t end_loc = token->location;
17100 /* Figure out which operator we have. */
17101 enum tree_code op = ERROR_MARK;
17102 bool assop = false;
17103 bool consumed = false;
17104 switch (token->type)
17106 case CPP_KEYWORD:
17108 /* The keyword should be either `new', `delete' or `co_await'. */
17109 if (token->keyword == RID_NEW)
17110 op = NEW_EXPR;
17111 else if (token->keyword == RID_DELETE)
17112 op = DELETE_EXPR;
17113 else if (token->keyword == RID_CO_AWAIT)
17114 op = CO_AWAIT_EXPR;
17115 else
17116 break;
17118 /* Consume the `new', `delete' or co_await token. */
17119 end_loc = cp_lexer_consume_token (parser->lexer)->location;
17121 /* Peek at the next token. */
17122 token = cp_lexer_peek_token (parser->lexer);
17123 /* If it's a `[' token then this is the array variant of the
17124 operator. */
17125 if (token->type == CPP_OPEN_SQUARE
17126 && op != CO_AWAIT_EXPR)
17128 /* Consume the `[' token. */
17129 cp_lexer_consume_token (parser->lexer);
17130 /* Look for the `]' token. */
17131 if (cp_token *close_token
17132 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
17133 end_loc = close_token->location;
17134 op = op == NEW_EXPR ? VEC_NEW_EXPR : VEC_DELETE_EXPR;
17136 consumed = true;
17137 break;
17140 case CPP_PLUS:
17141 op = PLUS_EXPR;
17142 break;
17144 case CPP_MINUS:
17145 op = MINUS_EXPR;
17146 break;
17148 case CPP_MULT:
17149 op = MULT_EXPR;
17150 break;
17152 case CPP_DIV:
17153 op = TRUNC_DIV_EXPR;
17154 break;
17156 case CPP_MOD:
17157 op = TRUNC_MOD_EXPR;
17158 break;
17160 case CPP_XOR:
17161 op = BIT_XOR_EXPR;
17162 break;
17164 case CPP_AND:
17165 op = BIT_AND_EXPR;
17166 break;
17168 case CPP_OR:
17169 op = BIT_IOR_EXPR;
17170 break;
17172 case CPP_COMPL:
17173 op = BIT_NOT_EXPR;
17174 break;
17176 case CPP_NOT:
17177 op = TRUTH_NOT_EXPR;
17178 break;
17180 case CPP_EQ:
17181 assop = true;
17182 op = NOP_EXPR;
17183 break;
17185 case CPP_LESS:
17186 op = LT_EXPR;
17187 break;
17189 case CPP_GREATER:
17190 op = GT_EXPR;
17191 break;
17193 case CPP_PLUS_EQ:
17194 assop = true;
17195 op = PLUS_EXPR;
17196 break;
17198 case CPP_MINUS_EQ:
17199 assop = true;
17200 op = MINUS_EXPR;
17201 break;
17203 case CPP_MULT_EQ:
17204 assop = true;
17205 op = MULT_EXPR;
17206 break;
17208 case CPP_DIV_EQ:
17209 assop = true;
17210 op = TRUNC_DIV_EXPR;
17211 break;
17213 case CPP_MOD_EQ:
17214 assop = true;
17215 op = TRUNC_MOD_EXPR;
17216 break;
17218 case CPP_XOR_EQ:
17219 assop = true;
17220 op = BIT_XOR_EXPR;
17221 break;
17223 case CPP_AND_EQ:
17224 assop = true;
17225 op = BIT_AND_EXPR;
17226 break;
17228 case CPP_OR_EQ:
17229 assop = true;
17230 op = BIT_IOR_EXPR;
17231 break;
17233 case CPP_LSHIFT:
17234 op = LSHIFT_EXPR;
17235 break;
17237 case CPP_RSHIFT:
17238 op = RSHIFT_EXPR;
17239 break;
17241 case CPP_LSHIFT_EQ:
17242 assop = true;
17243 op = LSHIFT_EXPR;
17244 break;
17246 case CPP_RSHIFT_EQ:
17247 assop = true;
17248 op = RSHIFT_EXPR;
17249 break;
17251 case CPP_EQ_EQ:
17252 op = EQ_EXPR;
17253 break;
17255 case CPP_NOT_EQ:
17256 op = NE_EXPR;
17257 break;
17259 case CPP_LESS_EQ:
17260 op = LE_EXPR;
17261 break;
17263 case CPP_GREATER_EQ:
17264 op = GE_EXPR;
17265 break;
17267 case CPP_SPACESHIP:
17268 op = SPACESHIP_EXPR;
17269 break;
17271 case CPP_AND_AND:
17272 op = TRUTH_ANDIF_EXPR;
17273 break;
17275 case CPP_OR_OR:
17276 op = TRUTH_ORIF_EXPR;
17277 break;
17279 case CPP_PLUS_PLUS:
17280 op = POSTINCREMENT_EXPR;
17281 break;
17283 case CPP_MINUS_MINUS:
17284 op = PREDECREMENT_EXPR;
17285 break;
17287 case CPP_COMMA:
17288 op = COMPOUND_EXPR;
17289 break;
17291 case CPP_DEREF_STAR:
17292 op = MEMBER_REF;
17293 break;
17295 case CPP_DEREF:
17296 op = COMPONENT_REF;
17297 break;
17299 case CPP_QUERY:
17300 op = COND_EXPR;
17301 /* Consume the `?'. */
17302 cp_lexer_consume_token (parser->lexer);
17303 /* Look for the matching `:'. */
17304 cp_parser_require (parser, CPP_COLON, RT_COLON);
17305 consumed = true;
17306 break;
17308 case CPP_OPEN_PAREN:
17310 /* Consume the `('. */
17311 matching_parens parens;
17312 parens.consume_open (parser);
17313 /* Look for the matching `)'. */
17314 token = parens.require_close (parser);
17315 if (token)
17316 end_loc = token->location;
17317 op = CALL_EXPR;
17318 consumed = true;
17319 break;
17322 case CPP_OPEN_SQUARE:
17323 /* Consume the `['. */
17324 cp_lexer_consume_token (parser->lexer);
17325 /* Look for the matching `]'. */
17326 token = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17327 if (token)
17328 end_loc = token->location;
17329 op = ARRAY_REF;
17330 consumed = true;
17331 break;
17333 case CPP_UTF8STRING:
17334 case CPP_UTF8STRING_USERDEF:
17335 utf8 = true;
17336 /* FALLTHRU */
17337 case CPP_STRING:
17338 case CPP_WSTRING:
17339 case CPP_STRING16:
17340 case CPP_STRING32:
17341 case CPP_STRING_USERDEF:
17342 case CPP_WSTRING_USERDEF:
17343 case CPP_STRING16_USERDEF:
17344 case CPP_STRING32_USERDEF:
17346 cp_expr str;
17347 tree string_tree;
17348 int sz, len;
17350 if (cxx_dialect == cxx98)
17351 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
17353 /* Consume the string. */
17354 str = cp_parser_string_literal (parser, /*translate=*/true,
17355 /*wide_ok=*/true, /*lookup_udlit=*/false);
17356 if (str == error_mark_node)
17357 return error_mark_node;
17358 else if (TREE_CODE (str) == USERDEF_LITERAL)
17360 string_tree = USERDEF_LITERAL_VALUE (str.get_value ());
17361 id = USERDEF_LITERAL_SUFFIX_ID (str.get_value ());
17362 end_loc = str.get_location ();
17364 else
17366 string_tree = str;
17367 /* Look for the suffix identifier. */
17368 token = cp_lexer_peek_token (parser->lexer);
17369 if (token->type == CPP_NAME)
17371 id = cp_parser_identifier (parser);
17372 end_loc = token->location;
17374 else if (token->type == CPP_KEYWORD)
17376 error ("unexpected keyword;"
17377 " remove space between quotes and suffix identifier");
17378 return error_mark_node;
17380 else
17382 error ("expected suffix identifier");
17383 return error_mark_node;
17386 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
17387 (TREE_TYPE (TREE_TYPE (string_tree))));
17388 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
17389 if (len != 0)
17391 error ("expected empty string after %<operator%> keyword");
17392 return error_mark_node;
17394 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
17395 != char_type_node)
17397 error ("invalid encoding prefix in literal operator");
17398 return error_mark_node;
17400 if (id != error_mark_node)
17402 const char *name = IDENTIFIER_POINTER (id);
17403 id = cp_literal_operator_id (name);
17405 /* Generate a location of the form:
17406 "" _suffix_identifier
17407 ^~~~~~~~~~~~~~~~~~~~~
17408 with caret == start at the start token, finish at the end of the
17409 suffix identifier. */
17410 location_t combined_loc
17411 = make_location (start_loc, start_loc, parser->lexer);
17412 return cp_expr (id, combined_loc);
17415 default:
17416 /* Anything else is an error. */
17417 break;
17420 /* If we have selected an identifier, we need to consume the
17421 operator token. */
17422 if (op != ERROR_MARK)
17424 id = ovl_op_identifier (assop, op);
17425 if (!consumed)
17426 cp_lexer_consume_token (parser->lexer);
17428 /* Otherwise, no valid operator name was present. */
17429 else
17431 cp_parser_error (parser, "expected operator");
17432 id = error_mark_node;
17435 start_loc = make_location (start_loc, start_loc, get_finish (end_loc));
17436 return cp_expr (id, start_loc);
17439 /* Parse a template-declaration.
17441 template-declaration:
17442 export [opt] template < template-parameter-list > declaration
17444 If MEMBER_P is TRUE, this template-declaration occurs within a
17445 class-specifier.
17447 The grammar rule given by the standard isn't correct. What
17448 is really meant is:
17450 template-declaration:
17451 export [opt] template-parameter-list-seq
17452 decl-specifier-seq [opt] init-declarator [opt] ;
17453 export [opt] template-parameter-list-seq
17454 function-definition
17456 template-parameter-list-seq:
17457 template-parameter-list-seq [opt]
17458 template < template-parameter-list >
17460 Concept Extensions:
17462 template-parameter-list-seq:
17463 template < template-parameter-list > requires-clause [opt]
17465 requires-clause:
17466 requires logical-or-expression */
17468 static void
17469 cp_parser_template_declaration (cp_parser* parser, bool member_p)
17471 /* Check for `export'. */
17472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
17474 /* Consume the `export' token. */
17475 cp_lexer_consume_token (parser->lexer);
17476 /* Warn that this use of export is deprecated. */
17477 if (cxx_dialect < cxx11)
17478 warning (0, "keyword %<export%> not implemented, and will be ignored");
17479 else if (cxx_dialect < cxx20)
17480 warning (0, "keyword %<export%> is deprecated, and is ignored");
17481 else
17482 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
17485 cp_parser_template_declaration_after_export (parser, member_p);
17488 /* Parse a template-parameter-list.
17490 template-parameter-list:
17491 template-parameter
17492 template-parameter-list , template-parameter
17494 Returns a TREE_LIST. Each node represents a template parameter.
17495 The nodes are connected via their TREE_CHAINs. */
17497 static tree
17498 cp_parser_template_parameter_list (cp_parser* parser)
17500 tree parameter_list = NULL_TREE;
17502 /* Don't create wrapper nodes within a template-parameter-list,
17503 since we don't want to have different types based on the
17504 spelling location of constants and decls within them. */
17505 auto_suppress_location_wrappers sentinel;
17507 begin_template_parm_list ();
17509 /* The loop below parses the template parms. We first need to know
17510 the total number of template parms to be able to compute proper
17511 canonical types of each dependent type. So after the loop, when
17512 we know the total number of template parms,
17513 end_template_parm_list computes the proper canonical types and
17514 fixes up the dependent types accordingly. */
17515 while (true)
17517 tree parameter;
17518 bool is_non_type;
17519 bool is_parameter_pack;
17520 location_t parm_loc;
17522 /* Parse the template-parameter. */
17523 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
17524 parameter = cp_parser_template_parameter (parser,
17525 &is_non_type,
17526 &is_parameter_pack);
17527 /* Add it to the list. */
17528 if (parameter != error_mark_node)
17529 parameter_list = process_template_parm (parameter_list,
17530 parm_loc,
17531 parameter,
17532 is_non_type,
17533 is_parameter_pack);
17534 else
17536 tree err_parm = build_tree_list (parameter, parameter);
17537 parameter_list = chainon (parameter_list, err_parm);
17540 /* If the next token is not a `,', we're done. */
17541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17542 break;
17543 /* Otherwise, consume the `,' token. */
17544 cp_lexer_consume_token (parser->lexer);
17547 return end_template_parm_list (parameter_list);
17550 /* Parse a introduction-list.
17552 introduction-list:
17553 introduced-parameter
17554 introduction-list , introduced-parameter
17556 introduced-parameter:
17557 ...[opt] identifier
17559 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
17560 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
17561 WILDCARD_DECL will also have DECL_NAME set and token location in
17562 DECL_SOURCE_LOCATION. */
17564 static tree
17565 cp_parser_introduction_list (cp_parser *parser)
17567 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
17569 while (true)
17571 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
17572 if (is_pack)
17573 cp_lexer_consume_token (parser->lexer);
17575 tree identifier = cp_parser_identifier (parser);
17576 if (identifier == error_mark_node)
17577 break;
17579 /* Build placeholder. */
17580 tree parm = build_nt (WILDCARD_DECL);
17581 DECL_SOURCE_LOCATION (parm)
17582 = cp_lexer_peek_token (parser->lexer)->location;
17583 DECL_NAME (parm) = identifier;
17584 WILDCARD_PACK_P (parm) = is_pack;
17585 vec_safe_push (introduction_vec, parm);
17587 /* If the next token is not a `,', we're done. */
17588 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17589 break;
17590 /* Otherwise, consume the `,' token. */
17591 cp_lexer_consume_token (parser->lexer);
17594 /* Convert the vec into a TREE_VEC. */
17595 tree introduction_list = make_tree_vec (introduction_vec->length ());
17596 unsigned int n;
17597 tree parm;
17598 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
17599 TREE_VEC_ELT (introduction_list, n) = parm;
17601 release_tree_vector (introduction_vec);
17602 return introduction_list;
17605 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
17606 is an abstract declarator. */
17608 static inline cp_declarator*
17609 get_id_declarator (cp_declarator *declarator)
17611 cp_declarator *d = declarator;
17612 while (d && d->kind != cdk_id)
17613 d = d->declarator;
17614 return d;
17617 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
17618 is an abstract declarator. */
17620 static inline tree
17621 get_unqualified_id (cp_declarator *declarator)
17623 declarator = get_id_declarator (declarator);
17624 if (declarator)
17625 return declarator->u.id.unqualified_name;
17626 else
17627 return NULL_TREE;
17630 /* Returns true if TYPE would declare a constrained constrained-parameter. */
17632 static inline bool
17633 is_constrained_parameter (tree type)
17635 return (type
17636 && TREE_CODE (type) == TYPE_DECL
17637 && CONSTRAINED_PARM_CONCEPT (type)
17638 && DECL_P (CONSTRAINED_PARM_CONCEPT (type)));
17641 /* Returns true if PARM declares a constrained-parameter. */
17643 static inline bool
17644 is_constrained_parameter (cp_parameter_declarator *parm)
17646 return is_constrained_parameter (parm->decl_specifiers.type);
17649 /* Check that the type parameter is only a declarator-id, and that its
17650 type is not cv-qualified. */
17652 bool
17653 cp_parser_check_constrained_type_parm (cp_parser *parser,
17654 cp_parameter_declarator *parm)
17656 if (!parm->declarator)
17657 return true;
17659 if (parm->declarator->kind != cdk_id)
17661 cp_parser_error (parser, "invalid constrained type parameter");
17662 return false;
17665 /* Don't allow cv-qualified type parameters. */
17666 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
17667 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
17669 cp_parser_error (parser, "cv-qualified type parameter");
17670 return false;
17673 return true;
17676 /* Finish parsing/processing a template type parameter and checking
17677 various restrictions. */
17679 static inline tree
17680 cp_parser_constrained_type_template_parm (cp_parser *parser,
17681 tree id,
17682 cp_parameter_declarator* parmdecl)
17684 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
17685 return finish_template_type_parm (class_type_node, id);
17686 else
17687 return error_mark_node;
17690 static tree
17691 finish_constrained_template_template_parm (tree proto, tree id)
17693 /* FIXME: This should probably be copied, and we may need to adjust
17694 the template parameter depths. */
17695 tree saved_parms = current_template_parms;
17696 begin_template_parm_list ();
17697 current_template_parms = DECL_TEMPLATE_PARMS (proto);
17698 end_template_parm_list ();
17700 tree parm = finish_template_template_parm (class_type_node, id);
17701 current_template_parms = saved_parms;
17703 return parm;
17706 /* Finish parsing/processing a template template parameter by borrowing
17707 the template parameter list from the prototype parameter. */
17709 static tree
17710 cp_parser_constrained_template_template_parm (cp_parser *parser,
17711 tree proto,
17712 tree id,
17713 cp_parameter_declarator *parmdecl)
17715 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
17716 return error_mark_node;
17717 return finish_constrained_template_template_parm (proto, id);
17720 /* Create a new non-type template parameter from the given PARM
17721 declarator. */
17723 static tree
17724 cp_parser_constrained_non_type_template_parm (bool *is_non_type,
17725 cp_parameter_declarator *parm)
17727 *is_non_type = true;
17728 cp_declarator *decl = parm->declarator;
17729 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
17730 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
17731 return grokdeclarator (decl, specs, TPARM, 0, NULL);
17734 /* Build a constrained template parameter based on the PARMDECL
17735 declarator. The type of PARMDECL is the constrained type, which
17736 refers to the prototype template parameter that ultimately
17737 specifies the type of the declared parameter. */
17739 static tree
17740 finish_constrained_parameter (cp_parser *parser,
17741 cp_parameter_declarator *parmdecl,
17742 bool *is_non_type)
17744 tree decl = parmdecl->decl_specifiers.type;
17745 tree id = get_unqualified_id (parmdecl->declarator);
17746 tree def = parmdecl->default_argument;
17747 tree proto = DECL_INITIAL (decl);
17749 /* Build the parameter. Return an error if the declarator was invalid. */
17750 tree parm;
17751 if (TREE_CODE (proto) == TYPE_DECL)
17752 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
17753 else if (TREE_CODE (proto) == TEMPLATE_DECL)
17754 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
17755 parmdecl);
17756 else
17757 parm = cp_parser_constrained_non_type_template_parm (is_non_type, parmdecl);
17758 if (parm == error_mark_node)
17759 return error_mark_node;
17761 /* Finish the parameter decl and create a node attaching the
17762 default argument and constraint. */
17763 parm = build_tree_list (def, parm);
17764 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
17766 return parm;
17769 /* Returns true if the parsed type actually represents the declaration
17770 of a type template-parameter. */
17772 static bool
17773 declares_constrained_type_template_parameter (tree type)
17775 return (is_constrained_parameter (type)
17776 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
17779 /* Returns true if the parsed type actually represents the declaration of
17780 a template template-parameter. */
17782 static bool
17783 declares_constrained_template_template_parameter (tree type)
17785 return (is_constrained_parameter (type)
17786 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
17789 /* Parse a default argument for a type template-parameter.
17790 Note that diagnostics are handled in cp_parser_template_parameter. */
17792 static tree
17793 cp_parser_default_type_template_argument (cp_parser *parser)
17795 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17797 /* Consume the `=' token. */
17798 cp_lexer_consume_token (parser->lexer);
17800 cp_token *token = cp_lexer_peek_token (parser->lexer);
17802 /* Tell cp_parser_lambda_expression this is a default argument. */
17803 auto lvf = make_temp_override (parser->local_variables_forbidden_p);
17804 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
17806 /* Parse the default-argument. */
17807 push_deferring_access_checks (dk_no_deferred);
17808 tree default_argument = cp_parser_type_id (parser,
17809 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17810 NULL);
17811 pop_deferring_access_checks ();
17813 if (flag_concepts && type_uses_auto (default_argument))
17815 error_at (token->location,
17816 "invalid use of %<auto%> in default template argument");
17817 return error_mark_node;
17820 return default_argument;
17823 /* Parse a default argument for a template template-parameter. */
17825 static tree
17826 cp_parser_default_template_template_argument (cp_parser *parser)
17828 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
17830 bool is_template;
17832 /* Consume the `='. */
17833 cp_lexer_consume_token (parser->lexer);
17834 /* Parse the id-expression. */
17835 push_deferring_access_checks (dk_no_deferred);
17836 /* save token before parsing the id-expression, for error
17837 reporting */
17838 const cp_token* token = cp_lexer_peek_token (parser->lexer);
17839 tree default_argument
17840 = cp_parser_id_expression (parser,
17841 /*template_keyword_p=*/false,
17842 /*check_dependency_p=*/true,
17843 /*template_p=*/&is_template,
17844 /*declarator_p=*/false,
17845 /*optional_p=*/false);
17846 if (TREE_CODE (default_argument) == TYPE_DECL)
17847 /* If the id-expression was a template-id that refers to
17848 a template-class, we already have the declaration here,
17849 so no further lookup is needed. */
17851 else
17852 /* Look up the name. */
17853 default_argument
17854 = cp_parser_lookup_name (parser, default_argument,
17855 none_type,
17856 /*is_template=*/is_template,
17857 /*is_namespace=*/false,
17858 /*check_dependency=*/true,
17859 /*ambiguous_decls=*/NULL,
17860 token->location);
17861 /* See if the default argument is valid. */
17862 default_argument = check_template_template_default_arg (default_argument);
17863 pop_deferring_access_checks ();
17864 return default_argument;
17867 /* Parse a template-parameter.
17869 template-parameter:
17870 type-parameter
17871 parameter-declaration
17873 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
17874 the parameter. The TREE_PURPOSE is the default value, if any.
17875 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
17876 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
17877 set to true iff this parameter is a parameter pack. */
17879 static tree
17880 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
17881 bool *is_parameter_pack)
17883 cp_token *token;
17884 cp_parameter_declarator *parameter_declarator;
17885 tree parm;
17887 /* Assume it is a type parameter or a template parameter. */
17888 *is_non_type = false;
17889 /* Assume it not a parameter pack. */
17890 *is_parameter_pack = false;
17891 /* Peek at the next token. */
17892 token = cp_lexer_peek_token (parser->lexer);
17893 /* If it is `template', we have a type-parameter. */
17894 if (token->keyword == RID_TEMPLATE)
17895 return cp_parser_type_parameter (parser, is_parameter_pack);
17896 /* If it is `class' or `typename' we do not know yet whether it is a
17897 type parameter or a non-type parameter. Consider:
17899 template <typename T, typename T::X X> ...
17903 template <class C, class D*> ...
17905 Here, the first parameter is a type parameter, and the second is
17906 a non-type parameter. We can tell by looking at the token after
17907 the identifier -- if it is a `,', `=', or `>' then we have a type
17908 parameter. */
17909 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
17911 /* Peek at the token after `class' or `typename'. */
17912 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17913 /* If it's an ellipsis, we have a template type parameter
17914 pack. */
17915 if (token->type == CPP_ELLIPSIS)
17916 return cp_parser_type_parameter (parser, is_parameter_pack);
17917 /* If it's an identifier, skip it. */
17918 if (token->type == CPP_NAME)
17919 token = cp_lexer_peek_nth_token (parser->lexer, 3);
17920 /* Now, see if the token looks like the end of a template
17921 parameter. */
17922 if (token->type == CPP_COMMA
17923 || token->type == CPP_EQ
17924 || token->type == CPP_GREATER)
17925 return cp_parser_type_parameter (parser, is_parameter_pack);
17928 /* Otherwise, it is a non-type parameter or a constrained parameter.
17930 [temp.param]
17932 When parsing a default template-argument for a non-type
17933 template-parameter, the first non-nested `>' is taken as the end
17934 of the template parameter-list rather than a greater-than
17935 operator. */
17936 parameter_declarator
17937 = cp_parser_parameter_declaration (parser,
17938 CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
17939 /*template_parm_p=*/true,
17940 /*parenthesized_p=*/NULL);
17942 if (!parameter_declarator)
17943 return error_mark_node;
17945 /* If the parameter declaration is marked as a parameter pack, set
17946 *IS_PARAMETER_PACK to notify the caller. */
17947 if (parameter_declarator->template_parameter_pack_p)
17948 *is_parameter_pack = true;
17950 if (parameter_declarator->default_argument)
17952 /* Can happen in some cases of erroneous input (c++/34892). */
17953 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17954 /* Consume the `...' for better error recovery. */
17955 cp_lexer_consume_token (parser->lexer);
17958 /* The parameter may have been constrained type parameter. */
17959 if (is_constrained_parameter (parameter_declarator))
17960 return finish_constrained_parameter (parser,
17961 parameter_declarator,
17962 is_non_type);
17964 // Now we're sure that the parameter is a non-type parameter.
17965 *is_non_type = true;
17967 parm = grokdeclarator (parameter_declarator->declarator,
17968 &parameter_declarator->decl_specifiers,
17969 TPARM, /*initialized=*/0,
17970 /*attrlist=*/NULL);
17971 if (parm == error_mark_node)
17972 return error_mark_node;
17974 return build_tree_list (parameter_declarator->default_argument, parm);
17977 /* Parse a type-parameter.
17979 type-parameter:
17980 class identifier [opt]
17981 class identifier [opt] = type-id
17982 typename identifier [opt]
17983 typename identifier [opt] = type-id
17984 template < template-parameter-list > class identifier [opt]
17985 template < template-parameter-list > class identifier [opt]
17986 = id-expression
17988 GNU Extension (variadic templates):
17990 type-parameter:
17991 class ... identifier [opt]
17992 typename ... identifier [opt]
17994 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17995 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17996 the declaration of the parameter.
17998 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
18000 static tree
18001 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
18003 cp_token *token;
18004 tree parameter;
18006 /* Look for a keyword to tell us what kind of parameter this is. */
18007 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
18008 if (!token)
18009 return error_mark_node;
18011 switch (token->keyword)
18013 case RID_CLASS:
18014 case RID_TYPENAME:
18016 tree identifier;
18017 tree default_argument;
18019 /* If the next token is an ellipsis, we have a template
18020 argument pack. */
18021 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18023 /* Consume the `...' token. */
18024 cp_lexer_consume_token (parser->lexer);
18025 maybe_warn_variadic_templates ();
18027 *is_parameter_pack = true;
18030 /* If the next token is an identifier, then it names the
18031 parameter. */
18032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18033 identifier = cp_parser_identifier (parser);
18034 else
18035 identifier = NULL_TREE;
18037 /* Create the parameter. */
18038 parameter = finish_template_type_parm (class_type_node, identifier);
18040 /* If the next token is an `=', we have a default argument. */
18041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18043 default_argument
18044 = cp_parser_default_type_template_argument (parser);
18046 /* Template parameter packs cannot have default
18047 arguments. */
18048 if (*is_parameter_pack)
18050 if (identifier)
18051 error_at (token->location,
18052 "template parameter pack %qD cannot have a "
18053 "default argument", identifier);
18054 else
18055 error_at (token->location,
18056 "template parameter packs cannot have "
18057 "default arguments");
18058 default_argument = NULL_TREE;
18060 else if (check_for_bare_parameter_packs (default_argument))
18061 default_argument = error_mark_node;
18063 else
18064 default_argument = NULL_TREE;
18066 /* Create the combined representation of the parameter and the
18067 default argument. */
18068 parameter = build_tree_list (default_argument, parameter);
18070 break;
18072 case RID_TEMPLATE:
18074 tree identifier;
18075 tree default_argument;
18077 /* Look for the `<'. */
18078 cp_parser_require (parser, CPP_LESS, RT_LESS);
18079 /* Parse the template-parameter-list. */
18080 cp_parser_template_parameter_list (parser);
18081 /* Look for the `>'. */
18082 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
18084 /* If template requirements are present, parse them. */
18085 if (flag_concepts)
18087 tree reqs = get_shorthand_constraints (current_template_parms);
18088 if (tree dreqs = cp_parser_requires_clause_opt (parser, false))
18089 reqs = combine_constraint_expressions (reqs, dreqs);
18090 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
18093 /* Look for the `class' or 'typename' keywords. */
18094 cp_parser_type_parameter_key (parser);
18095 /* If the next token is an ellipsis, we have a template
18096 argument pack. */
18097 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18099 /* Consume the `...' token. */
18100 cp_lexer_consume_token (parser->lexer);
18101 maybe_warn_variadic_templates ();
18103 *is_parameter_pack = true;
18105 /* If the next token is an `=', then there is a
18106 default-argument. If the next token is a `>', we are at
18107 the end of the parameter-list. If the next token is a `,',
18108 then we are at the end of this parameter. */
18109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
18110 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
18111 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18113 identifier = cp_parser_identifier (parser);
18114 /* Treat invalid names as if the parameter were nameless. */
18115 if (identifier == error_mark_node)
18116 identifier = NULL_TREE;
18118 else
18119 identifier = NULL_TREE;
18121 /* Create the template parameter. */
18122 parameter = finish_template_template_parm (class_type_node,
18123 identifier);
18125 /* If the next token is an `=', then there is a
18126 default-argument. */
18127 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18129 default_argument
18130 = cp_parser_default_template_template_argument (parser);
18132 /* Template parameter packs cannot have default
18133 arguments. */
18134 if (*is_parameter_pack)
18136 if (identifier)
18137 error_at (token->location,
18138 "template parameter pack %qD cannot "
18139 "have a default argument",
18140 identifier);
18141 else
18142 error_at (token->location, "template parameter packs cannot "
18143 "have default arguments");
18144 default_argument = NULL_TREE;
18147 else
18148 default_argument = NULL_TREE;
18150 /* Create the combined representation of the parameter and the
18151 default argument. */
18152 parameter = build_tree_list (default_argument, parameter);
18154 break;
18156 default:
18157 gcc_unreachable ();
18158 break;
18161 return parameter;
18164 /* Parse a template-id.
18166 template-id:
18167 template-name < template-argument-list [opt] >
18169 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
18170 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
18171 returned. Otherwise, if the template-name names a function, or set
18172 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
18173 names a class, returns a TYPE_DECL for the specialization.
18175 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
18176 uninstantiated templates. */
18178 static tree
18179 cp_parser_template_id (cp_parser *parser,
18180 bool template_keyword_p,
18181 bool check_dependency_p,
18182 enum tag_types tag_type,
18183 bool is_declaration)
18185 tree templ;
18186 tree arguments;
18187 tree template_id;
18188 cp_token_position start_of_id = 0;
18189 cp_token *next_token = NULL, *next_token_2 = NULL;
18190 bool is_identifier;
18192 /* If the next token corresponds to a template-id, there is no need
18193 to reparse it. */
18194 cp_token *token = cp_lexer_peek_token (parser->lexer);
18196 if (token->type == CPP_TEMPLATE_ID)
18198 cp_lexer_consume_token (parser->lexer);
18199 return saved_checks_value (token->u.tree_check_value);
18202 /* Avoid performing name lookup if there is no possibility of
18203 finding a template-id. */
18204 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
18205 || (token->type == CPP_NAME
18206 && !cp_parser_nth_token_starts_template_argument_list_p
18207 (parser, 2)))
18209 cp_parser_error (parser, "expected template-id");
18210 return error_mark_node;
18213 /* Remember where the template-id starts. */
18214 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
18215 start_of_id = cp_lexer_token_position (parser->lexer, false);
18217 push_deferring_access_checks (dk_deferred);
18219 /* Parse the template-name. */
18220 is_identifier = false;
18221 templ = cp_parser_template_name (parser, template_keyword_p,
18222 check_dependency_p,
18223 is_declaration,
18224 tag_type,
18225 &is_identifier);
18227 /* Push any access checks inside the firewall we're about to create. */
18228 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
18229 pop_deferring_access_checks ();
18230 if (templ == error_mark_node || is_identifier)
18231 return templ;
18233 /* Since we're going to preserve any side-effects from this parse, set up a
18234 firewall to protect our callers from cp_parser_commit_to_tentative_parse
18235 in the template arguments. */
18236 tentative_firewall firewall (parser);
18237 reopen_deferring_access_checks (checks);
18239 /* If we find the sequence `[:' after a template-name, it's probably
18240 a digraph-typo for `< ::'. Substitute the tokens and check if we can
18241 parse correctly the argument list. */
18242 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
18243 == CPP_OPEN_SQUARE)
18244 && next_token->flags & DIGRAPH
18245 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
18246 == CPP_COLON)
18247 && !(next_token_2->flags & PREV_WHITE))
18249 cp_parser_parse_tentatively (parser);
18250 /* Change `:' into `::'. */
18251 next_token_2->type = CPP_SCOPE;
18252 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
18253 CPP_LESS. */
18254 cp_lexer_consume_token (parser->lexer);
18256 /* Parse the arguments. */
18257 arguments = cp_parser_enclosed_template_argument_list (parser);
18258 if (!cp_parser_parse_definitely (parser))
18260 /* If we couldn't parse an argument list, then we revert our changes
18261 and return simply an error. Maybe this is not a template-id
18262 after all. */
18263 next_token_2->type = CPP_COLON;
18264 cp_parser_error (parser, "expected %<<%>");
18265 pop_deferring_access_checks ();
18266 return error_mark_node;
18268 /* Otherwise, emit an error about the invalid digraph, but continue
18269 parsing because we got our argument list. */
18270 if (permerror (next_token->location,
18271 "%<<::%> cannot begin a template-argument list"))
18273 static bool hint = false;
18274 inform (next_token->location,
18275 "%<<:%> is an alternate spelling for %<[%>."
18276 " Insert whitespace between %<<%> and %<::%>");
18277 if (!hint && !flag_permissive)
18279 inform (next_token->location, "(if you use %<-fpermissive%> "
18280 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
18281 "accept your code)");
18282 hint = true;
18286 else
18288 /* Look for the `<' that starts the template-argument-list. */
18289 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
18291 pop_deferring_access_checks ();
18292 return error_mark_node;
18294 /* Parse the arguments. */
18295 arguments = cp_parser_enclosed_template_argument_list (parser);
18297 if ((cxx_dialect > cxx17)
18298 && (TREE_CODE (templ) == FUNCTION_DECL || identifier_p (templ))
18299 && !template_keyword_p
18300 && (cp_parser_error_occurred (parser)
18301 || cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)))
18303 /* This didn't go well. */
18304 if (TREE_CODE (templ) == FUNCTION_DECL)
18306 /* C++20 says that "function-name < a;" is now ill-formed. */
18307 if (cp_parser_error_occurred (parser))
18309 error_at (token->location, "invalid template-argument-list");
18310 inform (token->location, "function name as the left hand "
18311 "operand of %<<%> is ill-formed in C++20; wrap the "
18312 "function name in %<()%>");
18314 else
18315 /* We expect "f<targs>" to be followed by "(args)". */
18316 error_at (cp_lexer_peek_token (parser->lexer)->location,
18317 "expected %<(%> after template-argument-list");
18318 if (start_of_id)
18319 /* Purge all subsequent tokens. */
18320 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18322 else
18323 cp_parser_simulate_error (parser);
18324 pop_deferring_access_checks ();
18325 return error_mark_node;
18329 /* Set the location to be of the form:
18330 template-name < template-argument-list [opt] >
18331 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18332 with caret == start at the start of the template-name,
18333 ranging until the closing '>'. */
18334 location_t combined_loc
18335 = make_location (token->location, token->location, parser->lexer);
18337 /* Check for concepts autos where they don't belong. We could
18338 identify types in some cases of identifier TEMPL, looking ahead
18339 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
18340 types. We reject them in functions, but if what we have is an
18341 identifier, even with none_type we can't conclude it's NOT a
18342 type, we have to wait for template substitution. */
18343 if (flag_concepts && check_auto_in_tmpl_args (templ, arguments))
18344 template_id = error_mark_node;
18345 /* Build a representation of the specialization. */
18346 else if (identifier_p (templ))
18347 template_id = build_min_nt_loc (combined_loc,
18348 TEMPLATE_ID_EXPR,
18349 templ, arguments);
18350 else if (DECL_TYPE_TEMPLATE_P (templ)
18351 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
18353 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
18354 template (rather than some instantiation thereof) only if
18355 is not nested within some other construct. For example, in
18356 "template <typename T> void f(T) { A<T>::", A<T> is just an
18357 instantiation of A. */
18358 bool entering_scope
18359 = (template_parm_scope_p ()
18360 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE));
18361 template_id
18362 = finish_template_type (templ, arguments, entering_scope);
18364 else if (concept_definition_p (templ))
18366 /* The caller will decide whether this is a concept check or type
18367 constraint. */
18368 template_id = build2_loc (combined_loc, TEMPLATE_ID_EXPR,
18369 boolean_type_node, templ, arguments);
18371 else if (variable_template_p (templ))
18373 template_id = lookup_template_variable (templ, arguments);
18374 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18375 SET_EXPR_LOCATION (template_id, combined_loc);
18377 else if (TREE_CODE (templ) == TYPE_DECL
18378 && TREE_CODE (TREE_TYPE (templ)) == TYPENAME_TYPE)
18380 /* Some type template in dependent scope. */
18381 tree &name = TYPENAME_TYPE_FULLNAME (TREE_TYPE (templ));
18382 name = build_min_nt_loc (combined_loc,
18383 TEMPLATE_ID_EXPR,
18384 name, arguments);
18385 template_id = templ;
18387 else
18389 /* If it's not a class-template or a template-template, it should be
18390 a function-template. */
18391 gcc_assert (OVL_P (templ) || BASELINK_P (templ));
18393 template_id = lookup_template_function (templ, arguments);
18394 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
18395 SET_EXPR_LOCATION (template_id, combined_loc);
18398 /* If parsing tentatively, replace the sequence of tokens that makes
18399 up the template-id with a CPP_TEMPLATE_ID token. That way,
18400 should we re-parse the token stream, we will not have to repeat
18401 the effort required to do the parse, nor will we issue duplicate
18402 error messages about problems during instantiation of the
18403 template. */
18404 if (start_of_id
18405 /* Don't do this if we had a parse error in a declarator; re-parsing
18406 might succeed if a name changes meaning (60361). */
18407 && !(cp_parser_error_occurred (parser)
18408 && cp_parser_parsing_tentatively (parser)
18409 && parser->in_declarator_p))
18411 /* Reset the contents of the START_OF_ID token. */
18412 token->type = CPP_TEMPLATE_ID;
18413 token->location = combined_loc;
18415 /* Retrieve any deferred checks. Do not pop this access checks yet
18416 so the memory will not be reclaimed during token replacing below. */
18417 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
18418 token->tree_check_p = true;
18419 token->u.tree_check_value->value = template_id;
18420 token->u.tree_check_value->checks = get_deferred_access_checks ();
18421 token->keyword = RID_MAX;
18423 /* Purge all subsequent tokens. */
18424 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
18426 /* ??? Can we actually assume that, if template_id ==
18427 error_mark_node, we will have issued a diagnostic to the
18428 user, as opposed to simply marking the tentative parse as
18429 failed? */
18430 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
18431 error_at (token->location, "parse error in template argument list");
18434 pop_to_parent_deferring_access_checks ();
18435 return template_id;
18438 /* Like cp_parser_template_id, called in non-type context. */
18440 static tree
18441 cp_parser_template_id_expr (cp_parser *parser,
18442 bool template_keyword_p,
18443 bool check_dependency_p,
18444 bool is_declaration)
18446 tree x = cp_parser_template_id (parser, template_keyword_p, check_dependency_p,
18447 none_type, is_declaration);
18448 if (TREE_CODE (x) == TEMPLATE_ID_EXPR
18449 && concept_check_p (x))
18450 /* We didn't check the arguments in cp_parser_template_id; do that now. */
18451 return build_concept_id (x);
18452 return x;
18455 /* Parse a template-name.
18457 template-name:
18458 identifier
18460 The standard should actually say:
18462 template-name:
18463 identifier
18464 operator-function-id
18466 A defect report has been filed about this issue.
18468 A conversion-function-id cannot be a template name because they cannot
18469 be part of a template-id. In fact, looking at this code:
18471 a.operator K<int>()
18473 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
18474 It is impossible to call a templated conversion-function-id with an
18475 explicit argument list, since the only allowed template parameter is
18476 the type to which it is converting.
18478 If TEMPLATE_KEYWORD_P is true, then we have just seen the
18479 `template' keyword, in a construction like:
18481 T::template f<3>()
18483 In that case `f' is taken to be a template-name, even though there
18484 is no way of knowing for sure.
18486 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
18487 name refers to a set of overloaded functions, at least one of which
18488 is a template, or an IDENTIFIER_NODE with the name of the template,
18489 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
18490 names are looked up inside uninstantiated templates. */
18492 static tree
18493 cp_parser_template_name (cp_parser* parser,
18494 bool template_keyword_p,
18495 bool check_dependency_p,
18496 bool is_declaration,
18497 enum tag_types tag_type,
18498 bool *is_identifier)
18500 tree identifier;
18501 tree decl;
18502 cp_token *token = cp_lexer_peek_token (parser->lexer);
18504 /* If the next token is `operator', then we have either an
18505 operator-function-id or a conversion-function-id. */
18506 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
18508 /* We don't know whether we're looking at an
18509 operator-function-id or a conversion-function-id. */
18510 cp_parser_parse_tentatively (parser);
18511 /* Try an operator-function-id. */
18512 identifier = cp_parser_operator_function_id (parser);
18513 /* If that didn't work, try a conversion-function-id. */
18514 if (!cp_parser_parse_definitely (parser))
18516 cp_parser_error (parser, "expected template-name");
18517 return error_mark_node;
18520 /* Look for the identifier. */
18521 else
18522 identifier = cp_parser_identifier (parser);
18524 /* If we didn't find an identifier, we don't have a template-id. */
18525 if (identifier == error_mark_node)
18526 return error_mark_node;
18528 /* If the name immediately followed the `template' keyword, then it
18529 is a template-name. However, if the next token is not `<', then
18530 we do not treat it as a template-name, since it is not being used
18531 as part of a template-id. This enables us to handle constructs
18532 like:
18534 template <typename T> struct S { S(); };
18535 template <typename T> S<T>::S();
18537 correctly. We would treat `S' as a template -- if it were `S<T>'
18538 -- but we do not if there is no `<'. */
18540 if (processing_template_decl
18541 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18543 /* In a declaration, in a dependent context, we pretend that the
18544 "template" keyword was present in order to improve error
18545 recovery. For example, given:
18547 template <typename T> void f(T::X<int>);
18549 we want to treat "X<int>" as a template-id. */
18550 if (is_declaration
18551 && !template_keyword_p
18552 && parser->scope && TYPE_P (parser->scope)
18553 && check_dependency_p
18554 && dependent_scope_p (parser->scope)
18555 /* Do not do this for dtors (or ctors), since they never
18556 need the template keyword before their name. */
18557 && !constructor_name_p (identifier, parser->scope))
18559 cp_token_position start = 0;
18561 /* Explain what went wrong. */
18562 error_at (token->location, "non-template %qD used as template",
18563 identifier);
18564 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
18565 parser->scope, identifier);
18566 /* If parsing tentatively, find the location of the "<" token. */
18567 if (cp_parser_simulate_error (parser))
18568 start = cp_lexer_token_position (parser->lexer, true);
18569 /* Parse the template arguments so that we can issue error
18570 messages about them. */
18571 cp_lexer_consume_token (parser->lexer);
18572 cp_parser_enclosed_template_argument_list (parser);
18573 /* Skip tokens until we find a good place from which to
18574 continue parsing. */
18575 cp_parser_skip_to_closing_parenthesis (parser,
18576 /*recovering=*/true,
18577 /*or_comma=*/true,
18578 /*consume_paren=*/false);
18579 /* If parsing tentatively, permanently remove the
18580 template argument list. That will prevent duplicate
18581 error messages from being issued about the missing
18582 "template" keyword. */
18583 if (start)
18584 cp_lexer_purge_tokens_after (parser->lexer, start);
18585 if (is_identifier)
18586 *is_identifier = true;
18587 parser->context->object_type = NULL_TREE;
18588 return identifier;
18591 /* If the "template" keyword is present, then there is generally
18592 no point in doing name-lookup, so we just return IDENTIFIER.
18593 But, if the qualifying scope is non-dependent then we can
18594 (and must) do name-lookup normally. */
18595 if (template_keyword_p)
18597 tree scope = (parser->scope ? parser->scope
18598 : parser->context->object_type);
18599 if (scope && TYPE_P (scope)
18600 && (!CLASS_TYPE_P (scope)
18601 || (check_dependency_p && dependent_scope_p (scope))))
18603 /* We're optimizing away the call to cp_parser_lookup_name, but
18604 we still need to do this. */
18605 parser->object_scope = parser->context->object_type;
18606 parser->context->object_type = NULL_TREE;
18607 return identifier;
18612 /* cp_parser_lookup_name clears OBJECT_TYPE. */
18613 tree scope = (parser->scope ? parser->scope
18614 : parser->context->object_type);
18616 /* Look up the name. */
18617 decl = cp_parser_lookup_name (parser, identifier,
18618 tag_type,
18619 /*is_template=*/true,
18620 /*is_namespace=*/false,
18621 check_dependency_p,
18622 /*ambiguous_decls=*/NULL,
18623 token->location);
18625 decl = strip_using_decl (decl);
18627 /* 13.3 [temp.names] A < is interpreted as the delimiter of a
18628 template-argument-list if it follows a name that is not a
18629 conversion-function-id and
18630 - that follows the keyword template or a ~ after a nested-name-specifier or
18631 in a class member access expression, or
18632 - for which name lookup finds the injected-class-name of a class template
18633 or finds any declaration of a template, or
18634 - that is an unqualified name for which name lookup either finds one or
18635 more functions or finds nothing, or
18636 - that is a terminal name in a using-declarator (9.9), in a declarator-id
18637 (9.3.4), or in a type-only context other than a nested-name-specifier
18638 (13.8). */
18640 /* Handle injected-class-name. */
18641 decl = maybe_get_template_decl_from_type_decl (decl);
18643 /* If DECL is a template, then the name was a template-name. */
18644 if (TREE_CODE (decl) == TEMPLATE_DECL)
18646 if ((TREE_DEPRECATED (decl) || TREE_UNAVAILABLE (decl))
18647 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS)
18649 tree d = DECL_TEMPLATE_RESULT (decl);
18650 tree attr;
18651 if (TREE_CODE (d) == TYPE_DECL)
18652 attr = TYPE_ATTRIBUTES (TREE_TYPE (d));
18653 else
18654 attr = DECL_ATTRIBUTES (d);
18655 if (TREE_UNAVAILABLE (decl))
18657 attr = lookup_attribute ("unavailable", attr);
18658 error_unavailable_use (decl, attr);
18660 else if (TREE_DEPRECATED (decl)
18661 && deprecated_state != DEPRECATED_SUPPRESS)
18663 attr = lookup_attribute ("deprecated", attr);
18664 warn_deprecated_use (decl, attr);
18668 else
18670 /* Look through an overload set for any templates. */
18671 bool found = false;
18673 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
18674 !found && iter; ++iter)
18675 if (TREE_CODE (*iter) == TEMPLATE_DECL)
18676 found = true;
18678 /* "an unqualified name for which name lookup either finds one or more
18679 functions or finds nothing". */
18680 if (!found
18681 && (cxx_dialect > cxx17)
18682 && !scope
18683 && cp_lexer_next_token_is (parser->lexer, CPP_LESS)
18684 && tag_type == none_type)
18686 /* The "more functions" case. Just use the OVERLOAD as normally.
18687 We don't use is_overloaded_fn here to avoid considering
18688 BASELINKs. */
18689 if (TREE_CODE (decl) == OVERLOAD
18690 /* Name lookup found one function. */
18691 || TREE_CODE (decl) == FUNCTION_DECL
18692 /* Name lookup found nothing. */
18693 || decl == error_mark_node)
18694 found = true;
18697 /* "that follows the keyword template"..."in a type-only context" */
18698 if (!found && scope
18699 && (template_keyword_p || tag_type != none_type)
18700 && dependentish_scope_p (scope)
18701 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
18702 found = true;
18704 if (!found)
18706 /* The name does not name a template. */
18707 cp_parser_error (parser, "expected template-name");
18708 return error_mark_node;
18710 else if ((!DECL_P (decl) && !is_overloaded_fn (decl))
18711 || TREE_CODE (decl) == USING_DECL
18712 /* cp_parser_template_id can only handle some TYPE_DECLs. */
18713 || (TREE_CODE (decl) == TYPE_DECL
18714 && TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE))
18715 /* Repeat the lookup at instantiation time. */
18716 decl = identifier;
18719 return decl;
18722 /* Parse a template-argument-list.
18724 template-argument-list:
18725 template-argument ... [opt]
18726 template-argument-list , template-argument ... [opt]
18728 Returns a TREE_VEC containing the arguments. */
18730 static tree
18731 cp_parser_template_argument_list (cp_parser* parser)
18733 bool saved_in_template_argument_list_p;
18734 bool saved_ice_p;
18735 bool saved_non_ice_p;
18737 /* Don't create location wrapper nodes within a template-argument-list. */
18738 auto_suppress_location_wrappers sentinel;
18740 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
18741 parser->in_template_argument_list_p = true;
18742 /* Even if the template-id appears in an integral
18743 constant-expression, the contents of the argument list do
18744 not. */
18745 saved_ice_p = parser->integral_constant_expression_p;
18746 parser->integral_constant_expression_p = false;
18747 saved_non_ice_p = parser->non_integral_constant_expression_p;
18748 parser->non_integral_constant_expression_p = false;
18750 /* Parse the arguments. */
18751 auto_vec<tree, 10> args;
18754 if (!args.is_empty ())
18755 /* Consume the comma. */
18756 cp_lexer_consume_token (parser->lexer);
18758 /* Parse the template-argument. */
18759 tree argument = cp_parser_template_argument (parser);
18761 /* If the next token is an ellipsis, we're expanding a template
18762 argument pack. */
18763 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18765 if (argument == error_mark_node)
18767 cp_token *token = cp_lexer_peek_token (parser->lexer);
18768 error_at (token->location,
18769 "expected parameter pack before %<...%>");
18771 /* Consume the `...' token. */
18772 cp_lexer_consume_token (parser->lexer);
18774 /* Make the argument into a TYPE_PACK_EXPANSION or
18775 EXPR_PACK_EXPANSION. */
18776 argument = make_pack_expansion (argument);
18779 args.safe_push (argument);
18781 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18783 int n_args = args.length ();
18784 tree vec = make_tree_vec (n_args);
18786 for (int i = 0; i < n_args; i++)
18787 TREE_VEC_ELT (vec, i) = args[i];
18789 parser->non_integral_constant_expression_p = saved_non_ice_p;
18790 parser->integral_constant_expression_p = saved_ice_p;
18791 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
18792 if (CHECKING_P)
18793 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
18794 return vec;
18797 /* Parse a template-argument.
18799 template-argument:
18800 assignment-expression
18801 type-id
18802 id-expression
18804 The representation is that of an assignment-expression, type-id, or
18805 id-expression -- except that the qualified id-expression is
18806 evaluated, so that the value returned is either a DECL or an
18807 OVERLOAD.
18809 Although the standard says "assignment-expression", it forbids
18810 throw-expressions or assignments in the template argument.
18811 Therefore, we use "conditional-expression" instead. */
18813 static tree
18814 cp_parser_template_argument (cp_parser* parser)
18816 tree argument;
18817 bool template_p;
18818 bool address_p;
18819 bool maybe_type_id = false;
18820 cp_token *token = NULL, *argument_start_token = NULL;
18821 location_t loc = 0;
18822 cp_id_kind idk;
18824 /* There's really no way to know what we're looking at, so we just
18825 try each alternative in order.
18827 [temp.arg]
18829 In a template-argument, an ambiguity between a type-id and an
18830 expression is resolved to a type-id, regardless of the form of
18831 the corresponding template-parameter.
18833 Therefore, we try a type-id first. */
18834 cp_parser_parse_tentatively (parser);
18835 argument = cp_parser_template_type_arg (parser);
18836 /* If there was no error parsing the type-id but the next token is a
18837 '>>', our behavior depends on which dialect of C++ we're
18838 parsing. In C++98, we probably found a typo for '> >'. But there
18839 are type-id which are also valid expressions. For instance:
18841 struct X { int operator >> (int); };
18842 template <int V> struct Foo {};
18843 Foo<X () >> 5> r;
18845 Here 'X()' is a valid type-id of a function type, but the user just
18846 wanted to write the expression "X() >> 5". Thus, we remember that we
18847 found a valid type-id, but we still try to parse the argument as an
18848 expression to see what happens.
18850 In C++0x, the '>>' will be considered two separate '>'
18851 tokens. */
18852 if (!cp_parser_error_occurred (parser)
18853 && ((cxx_dialect == cxx98
18854 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18855 /* Similarly for >= which
18856 cp_parser_next_token_ends_template_argument_p treats for
18857 diagnostics purposes as mistyped > =, but can be valid
18858 after a type-id. */
18859 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)))
18861 maybe_type_id = true;
18862 cp_parser_abort_tentative_parse (parser);
18864 else
18866 /* If the next token isn't a `,' or a `>', then this argument wasn't
18867 really finished. This means that the argument is not a valid
18868 type-id. */
18869 if (!cp_parser_next_token_ends_template_argument_p (parser))
18870 cp_parser_error (parser, "expected template-argument");
18871 /* If that worked, we're done. */
18872 if (cp_parser_parse_definitely (parser))
18873 return argument;
18875 /* We're still not sure what the argument will be. */
18876 cp_parser_parse_tentatively (parser);
18877 /* Try a template. */
18878 argument_start_token = cp_lexer_peek_token (parser->lexer);
18879 argument = cp_parser_id_expression (parser,
18880 /*template_keyword_p=*/false,
18881 /*check_dependency_p=*/true,
18882 &template_p,
18883 /*declarator_p=*/false,
18884 /*optional_p=*/false);
18885 /* If the next token isn't a `,' or a `>', then this argument wasn't
18886 really finished. */
18887 if (!cp_parser_next_token_ends_template_argument_p (parser))
18888 cp_parser_error (parser, "expected template-argument");
18889 if (!cp_parser_error_occurred (parser))
18891 /* Figure out what is being referred to. If the id-expression
18892 was for a class template specialization, then we will have a
18893 TYPE_DECL at this point. There is no need to do name lookup
18894 at this point in that case. */
18895 if (TREE_CODE (argument) != TYPE_DECL)
18896 argument = cp_parser_lookup_name (parser, argument,
18897 none_type,
18898 /*is_template=*/template_p,
18899 /*is_namespace=*/false,
18900 /*check_dependency=*/true,
18901 /*ambiguous_decls=*/NULL,
18902 argument_start_token->location);
18903 if (TREE_CODE (argument) != TEMPLATE_DECL
18904 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
18905 cp_parser_error (parser, "expected template-name");
18907 if (cp_parser_parse_definitely (parser))
18909 if (TREE_UNAVAILABLE (argument))
18910 error_unavailable_use (argument, NULL_TREE);
18911 else if (TREE_DEPRECATED (argument))
18912 warn_deprecated_use (argument, NULL_TREE);
18913 return argument;
18915 /* It must be a non-type argument. In C++17 any constant-expression is
18916 allowed. */
18917 if (cxx_dialect > cxx14)
18918 goto general_expr;
18920 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
18922 -- an integral constant-expression of integral or enumeration
18923 type; or
18925 -- the name of a non-type template-parameter; or
18927 -- the name of an object or function with external linkage...
18929 -- the address of an object or function with external linkage...
18931 -- a pointer to member... */
18932 /* Look for a non-type template parameter. */
18933 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18935 cp_parser_parse_tentatively (parser);
18936 argument = cp_parser_primary_expression (parser,
18937 /*address_p=*/false,
18938 /*cast_p=*/false,
18939 /*template_arg_p=*/true,
18940 &idk);
18941 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
18942 || !cp_parser_next_token_ends_template_argument_p (parser))
18943 cp_parser_simulate_error (parser);
18944 if (cp_parser_parse_definitely (parser))
18945 return argument;
18948 /* If the next token is "&", the argument must be the address of an
18949 object or function with external linkage. */
18950 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
18951 if (address_p)
18953 loc = cp_lexer_peek_token (parser->lexer)->location;
18954 cp_lexer_consume_token (parser->lexer);
18956 /* See if we might have an id-expression. */
18957 token = cp_lexer_peek_token (parser->lexer);
18958 if (token->type == CPP_NAME
18959 || token->keyword == RID_OPERATOR
18960 || token->type == CPP_SCOPE
18961 || token->type == CPP_TEMPLATE_ID
18962 || token->type == CPP_NESTED_NAME_SPECIFIER)
18964 cp_parser_parse_tentatively (parser);
18965 argument = cp_parser_primary_expression (parser,
18966 address_p,
18967 /*cast_p=*/false,
18968 /*template_arg_p=*/true,
18969 &idk);
18970 if (cp_parser_error_occurred (parser)
18971 || !cp_parser_next_token_ends_template_argument_p (parser))
18972 cp_parser_abort_tentative_parse (parser);
18973 else
18975 tree probe;
18977 if (INDIRECT_REF_P (argument))
18979 /* Strip the dereference temporarily. */
18980 gcc_assert (REFERENCE_REF_P (argument));
18981 argument = TREE_OPERAND (argument, 0);
18984 /* If we're in a template, we represent a qualified-id referring
18985 to a static data member as a SCOPE_REF even if the scope isn't
18986 dependent so that we can check access control later. */
18987 probe = argument;
18988 if (TREE_CODE (probe) == SCOPE_REF)
18989 probe = TREE_OPERAND (probe, 1);
18990 if (VAR_P (probe))
18992 /* A variable without external linkage might still be a
18993 valid constant-expression, so no error is issued here
18994 if the external-linkage check fails. */
18995 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
18996 cp_parser_simulate_error (parser);
18998 else if (is_overloaded_fn (argument))
18999 /* All overloaded functions are allowed; if the external
19000 linkage test does not pass, an error will be issued
19001 later. */
19003 else if (address_p
19004 && (TREE_CODE (argument) == OFFSET_REF
19005 || TREE_CODE (argument) == SCOPE_REF))
19006 /* A pointer-to-member. */
19008 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
19010 else
19011 cp_parser_simulate_error (parser);
19013 if (cp_parser_parse_definitely (parser))
19015 if (address_p)
19016 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
19017 NULL_TREE, tf_warning_or_error);
19018 else
19019 argument = convert_from_reference (argument);
19020 return argument;
19024 /* If the argument started with "&", there are no other valid
19025 alternatives at this point. */
19026 if (address_p)
19028 cp_parser_error (parser, "invalid non-type template argument");
19029 return error_mark_node;
19032 general_expr:
19033 /* If the argument wasn't successfully parsed as a type-id followed
19034 by '>>', the argument can only be a constant expression now.
19035 Otherwise, we try parsing the constant-expression tentatively,
19036 because the argument could really be a type-id. */
19037 if (maybe_type_id)
19038 cp_parser_parse_tentatively (parser);
19040 if (cxx_dialect <= cxx14)
19041 argument = cp_parser_constant_expression (parser);
19042 else
19044 /* In C++20, we can encounter a braced-init-list. */
19045 if (cxx_dialect >= cxx20
19046 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19048 bool expr_non_constant_p;
19049 return cp_parser_braced_list (parser, &expr_non_constant_p);
19052 /* With C++17 generalized non-type template arguments we need to handle
19053 lvalue constant expressions, too. */
19054 argument = cp_parser_assignment_expression (parser);
19055 require_potential_constant_expression (argument);
19058 if (!maybe_type_id)
19059 return argument;
19060 if (!cp_parser_next_token_ends_template_argument_p (parser))
19061 cp_parser_error (parser, "expected template-argument");
19062 if (cp_parser_parse_definitely (parser))
19063 return argument;
19064 /* We did our best to parse the argument as a non type-id, but that
19065 was the only alternative that matched (albeit with a '>' after
19066 it). We can assume it's just a typo from the user, and a
19067 diagnostic will then be issued. */
19068 return cp_parser_template_type_arg (parser);
19071 /* Parse an explicit-instantiation.
19073 explicit-instantiation:
19074 template declaration
19076 Although the standard says `declaration', what it really means is:
19078 explicit-instantiation:
19079 template decl-specifier-seq [opt] declarator [opt] ;
19081 Things like `template int S<int>::i = 5, int S<double>::j;' are not
19082 supposed to be allowed. A defect report has been filed about this
19083 issue.
19085 GNU Extension:
19087 explicit-instantiation:
19088 storage-class-specifier template
19089 decl-specifier-seq [opt] declarator [opt] ;
19090 function-specifier template
19091 decl-specifier-seq [opt] declarator [opt] ; */
19093 static void
19094 cp_parser_explicit_instantiation (cp_parser* parser)
19096 int declares_class_or_enum;
19097 cp_decl_specifier_seq decl_specifiers;
19098 tree extension_specifier = NULL_TREE;
19100 auto_timevar tv (TV_TEMPLATE_INST);
19102 /* Look for an (optional) storage-class-specifier or
19103 function-specifier. */
19104 if (cp_parser_allow_gnu_extensions_p (parser))
19106 extension_specifier
19107 = cp_parser_storage_class_specifier_opt (parser);
19108 if (!extension_specifier)
19109 extension_specifier
19110 = cp_parser_function_specifier_opt (parser,
19111 /*decl_specs=*/NULL);
19114 /* Look for the `template' keyword. */
19115 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19116 /* Let the front end know that we are processing an explicit
19117 instantiation. */
19118 begin_explicit_instantiation ();
19119 /* [temp.explicit] says that we are supposed to ignore access
19120 control while processing explicit instantiation directives. */
19121 push_deferring_access_checks (dk_no_check);
19122 /* Parse a decl-specifier-seq. */
19123 cp_parser_decl_specifier_seq (parser,
19124 CP_PARSER_FLAGS_OPTIONAL,
19125 &decl_specifiers,
19126 &declares_class_or_enum);
19128 cp_omp_declare_simd_data odsd;
19129 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
19130 cp_parser_handle_directive_omp_attributes (parser,
19131 &decl_specifiers.attributes,
19132 &odsd, true);
19134 /* If there was exactly one decl-specifier, and it declared a class,
19135 and there's no declarator, then we have an explicit type
19136 instantiation. */
19137 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
19139 tree type = check_tag_decl (&decl_specifiers,
19140 /*explicit_type_instantiation_p=*/true);
19141 /* Turn access control back on for names used during
19142 template instantiation. */
19143 pop_deferring_access_checks ();
19144 if (type)
19145 do_type_instantiation (type, extension_specifier,
19146 /*complain=*/tf_error);
19148 else
19150 cp_declarator *declarator;
19151 tree decl;
19153 /* Parse the declarator. */
19154 declarator
19155 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19156 CP_PARSER_FLAGS_NONE,
19157 /*ctor_dtor_or_conv_p=*/NULL,
19158 /*parenthesized_p=*/NULL,
19159 /*member_p=*/false,
19160 /*friend_p=*/false,
19161 /*static_p=*/false);
19162 if (declares_class_or_enum & 2)
19163 cp_parser_check_for_definition_in_return_type (declarator,
19164 decl_specifiers.type,
19165 decl_specifiers.locations[ds_type_spec]);
19166 if (declarator != cp_error_declarator)
19168 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
19169 permerror (decl_specifiers.locations[ds_inline],
19170 "explicit instantiation shall not use"
19171 " %<inline%> specifier");
19172 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
19173 permerror (decl_specifiers.locations[ds_constexpr],
19174 "explicit instantiation shall not use"
19175 " %<constexpr%> specifier");
19176 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_consteval))
19177 permerror (decl_specifiers.locations[ds_consteval],
19178 "explicit instantiation shall not use"
19179 " %<consteval%> specifier");
19181 decl = grokdeclarator (declarator, &decl_specifiers,
19182 NORMAL, 0, &decl_specifiers.attributes);
19183 /* Turn access control back on for names used during
19184 template instantiation. */
19185 pop_deferring_access_checks ();
19186 /* Do the explicit instantiation. */
19187 do_decl_instantiation (decl, extension_specifier);
19189 else
19191 pop_deferring_access_checks ();
19192 /* Skip the body of the explicit instantiation. */
19193 cp_parser_skip_to_end_of_statement (parser);
19196 /* We're done with the instantiation. */
19197 end_explicit_instantiation ();
19199 cp_parser_consume_semicolon_at_end_of_statement (parser);
19201 cp_finalize_omp_declare_simd (parser, &odsd);
19204 /* Parse an explicit-specialization.
19206 explicit-specialization:
19207 template < > declaration
19209 Although the standard says `declaration', what it really means is:
19211 explicit-specialization:
19212 template <> decl-specifier [opt] init-declarator [opt] ;
19213 template <> function-definition
19214 template <> explicit-specialization
19215 template <> template-declaration */
19217 static void
19218 cp_parser_explicit_specialization (cp_parser* parser)
19220 cp_token *token = cp_lexer_peek_token (parser->lexer);
19222 /* Look for the `template' keyword. */
19223 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
19224 /* Look for the `<'. */
19225 cp_parser_require (parser, CPP_LESS, RT_LESS);
19226 /* Look for the `>'. */
19227 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
19228 /* We have processed another parameter list. */
19229 ++parser->num_template_parameter_lists;
19231 /* [temp]
19233 A template ... explicit specialization ... shall not have C
19234 linkage. */
19235 bool need_lang_pop = current_lang_name == lang_name_c;
19236 if (need_lang_pop)
19238 error_at (token->location, "template specialization with C linkage");
19239 maybe_show_extern_c_location ();
19241 /* Give it C++ linkage to avoid confusing other parts of the
19242 front end. */
19243 push_lang_context (lang_name_cplusplus);
19246 /* Let the front end know that we are beginning a specialization. */
19247 if (begin_specialization ())
19249 /* If the next keyword is `template', we need to figure out
19250 whether or not we're looking a template-declaration. */
19251 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19253 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
19254 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
19255 cp_parser_template_declaration_after_export (parser,
19256 /*member_p=*/false);
19257 else
19258 cp_parser_explicit_specialization (parser);
19260 else
19261 /* Parse the dependent declaration. */
19262 cp_parser_single_declaration (parser,
19263 /*checks=*/NULL,
19264 /*member_p=*/false,
19265 /*explicit_specialization_p=*/true,
19266 /*friend_p=*/NULL);
19269 /* We're done with the specialization. */
19270 end_specialization ();
19272 /* For the erroneous case of a template with C linkage, we pushed an
19273 implicit C++ linkage scope; exit that scope now. */
19274 if (need_lang_pop)
19275 pop_lang_context ();
19277 /* We're done with this parameter list. */
19278 --parser->num_template_parameter_lists;
19281 /* Preserve the attributes across a garbage collect (by making it a GC
19282 root), which can occur when parsing a member function. */
19284 static GTY(()) vec<tree, va_gc> *cp_parser_decl_specs_attrs;
19286 /* Parse a type-specifier.
19288 type-specifier:
19289 simple-type-specifier
19290 class-specifier
19291 enum-specifier
19292 elaborated-type-specifier
19293 cv-qualifier
19295 GNU Extension:
19297 type-specifier:
19298 __complex__
19300 Returns a representation of the type-specifier. For a
19301 class-specifier, enum-specifier, or elaborated-type-specifier, a
19302 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
19304 The parser flags FLAGS is used to control type-specifier parsing.
19306 If IS_DECLARATION is TRUE, then this type-specifier is appearing
19307 in a decl-specifier-seq.
19309 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
19310 class-specifier, enum-specifier, or elaborated-type-specifier, then
19311 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
19312 if a type is declared; 2 if it is defined. Otherwise, it is set to
19313 zero.
19315 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
19316 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
19317 is set to FALSE. */
19319 static tree
19320 cp_parser_type_specifier (cp_parser* parser,
19321 cp_parser_flags flags,
19322 cp_decl_specifier_seq *decl_specs,
19323 bool is_declaration,
19324 int* declares_class_or_enum,
19325 bool* is_cv_qualifier)
19327 tree type_spec = NULL_TREE;
19328 cp_token *token;
19329 enum rid keyword;
19330 cp_decl_spec ds = ds_last;
19332 /* Assume this type-specifier does not declare a new type. */
19333 if (declares_class_or_enum)
19334 *declares_class_or_enum = 0;
19335 /* And that it does not specify a cv-qualifier. */
19336 if (is_cv_qualifier)
19337 *is_cv_qualifier = false;
19338 /* Peek at the next token. */
19339 token = cp_lexer_peek_token (parser->lexer);
19341 /* If we're looking at a keyword, we can use that to guide the
19342 production we choose. */
19343 keyword = token->keyword;
19344 switch (keyword)
19346 case RID_ENUM:
19347 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19348 goto elaborated_type_specifier;
19350 /* Look for the enum-specifier. */
19351 type_spec = cp_parser_enum_specifier (parser);
19352 /* If that worked, we're done. */
19353 if (type_spec)
19355 if (declares_class_or_enum)
19356 *declares_class_or_enum = 2;
19357 if (decl_specs)
19358 cp_parser_set_decl_spec_type (decl_specs,
19359 type_spec,
19360 token,
19361 /*type_definition_p=*/true);
19362 return type_spec;
19364 else
19365 goto elaborated_type_specifier;
19367 /* Any of these indicate either a class-specifier, or an
19368 elaborated-type-specifier. */
19369 case RID_CLASS:
19370 case RID_STRUCT:
19371 case RID_UNION:
19372 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
19373 goto elaborated_type_specifier;
19375 /* Parse tentatively so that we can back up if we don't find a
19376 class-specifier. */
19377 cp_parser_parse_tentatively (parser);
19378 if (decl_specs->attributes)
19379 vec_safe_push (cp_parser_decl_specs_attrs, decl_specs->attributes);
19380 /* Look for the class-specifier. */
19381 type_spec = cp_parser_class_specifier (parser);
19382 if (decl_specs->attributes)
19383 cp_parser_decl_specs_attrs->pop ();
19384 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
19385 /* If that worked, we're done. */
19386 if (cp_parser_parse_definitely (parser))
19388 if (declares_class_or_enum)
19389 *declares_class_or_enum = 2;
19390 if (decl_specs)
19391 cp_parser_set_decl_spec_type (decl_specs,
19392 type_spec,
19393 token,
19394 /*type_definition_p=*/true);
19395 return type_spec;
19398 /* Fall through. */
19399 elaborated_type_specifier:
19400 /* We're declaring (not defining) a class or enum. */
19401 if (declares_class_or_enum)
19402 *declares_class_or_enum = 1;
19404 /* Fall through. */
19405 case RID_TYPENAME:
19406 /* Look for an elaborated-type-specifier. */
19407 type_spec
19408 = (cp_parser_elaborated_type_specifier
19409 (parser,
19410 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
19411 is_declaration));
19412 if (decl_specs)
19413 cp_parser_set_decl_spec_type (decl_specs,
19414 type_spec,
19415 token,
19416 /*type_definition_p=*/false);
19417 return type_spec;
19419 case RID_CONST:
19420 ds = ds_const;
19421 if (is_cv_qualifier)
19422 *is_cv_qualifier = true;
19423 break;
19425 case RID_VOLATILE:
19426 ds = ds_volatile;
19427 if (is_cv_qualifier)
19428 *is_cv_qualifier = true;
19429 break;
19431 case RID_RESTRICT:
19432 ds = ds_restrict;
19433 if (is_cv_qualifier)
19434 *is_cv_qualifier = true;
19435 break;
19437 case RID_COMPLEX:
19438 /* The `__complex__' keyword is a GNU extension. */
19439 ds = ds_complex;
19440 break;
19442 default:
19443 break;
19446 /* Handle simple keywords. */
19447 if (ds != ds_last)
19449 if (decl_specs)
19451 set_and_check_decl_spec_loc (decl_specs, ds, token);
19452 decl_specs->any_specifiers_p = true;
19454 return cp_lexer_consume_token (parser->lexer)->u.value;
19457 /* If we do not already have a type-specifier, assume we are looking
19458 at a simple-type-specifier. */
19459 type_spec = cp_parser_simple_type_specifier (parser,
19460 decl_specs,
19461 flags);
19463 /* If we didn't find a type-specifier, and a type-specifier was not
19464 optional in this context, issue an error message. */
19465 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19467 cp_parser_error (parser, "expected type specifier");
19468 return error_mark_node;
19471 return type_spec;
19474 /* Parse a simple-type-specifier.
19476 simple-type-specifier:
19477 :: [opt] nested-name-specifier [opt] type-name
19478 :: [opt] nested-name-specifier template template-id
19479 char
19480 wchar_t
19481 bool
19482 short
19484 long
19485 signed
19486 unsigned
19487 float
19488 double
19489 void
19491 C++11 Extension:
19493 simple-type-specifier:
19494 auto
19495 decltype ( expression )
19496 char16_t
19497 char32_t
19498 __underlying_type ( type-id )
19500 C++17 extension:
19502 nested-name-specifier(opt) template-name
19504 GNU Extension:
19506 simple-type-specifier:
19507 __int128
19508 __typeof__ unary-expression
19509 __typeof__ ( type-id )
19510 __typeof__ ( type-id ) { initializer-list , [opt] }
19512 Concepts Extension:
19514 simple-type-specifier:
19515 constrained-type-specifier
19517 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
19518 appropriately updated. */
19520 static tree
19521 cp_parser_simple_type_specifier (cp_parser* parser,
19522 cp_decl_specifier_seq *decl_specs,
19523 cp_parser_flags flags)
19525 tree type = NULL_TREE;
19526 cp_token *token;
19527 int idx;
19529 /* Peek at the next token. */
19530 token = cp_lexer_peek_token (parser->lexer);
19532 /* If we're looking at a keyword, things are easy. */
19533 switch (token->keyword)
19535 case RID_CHAR:
19536 if (decl_specs)
19537 decl_specs->explicit_char_p = true;
19538 type = char_type_node;
19539 break;
19540 case RID_CHAR8:
19541 type = char8_type_node;
19542 break;
19543 case RID_CHAR16:
19544 type = char16_type_node;
19545 break;
19546 case RID_CHAR32:
19547 type = char32_type_node;
19548 break;
19549 case RID_WCHAR:
19550 type = wchar_type_node;
19551 break;
19552 case RID_BOOL:
19553 type = boolean_type_node;
19554 break;
19555 case RID_SHORT:
19556 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
19557 type = short_integer_type_node;
19558 break;
19559 case RID_INT:
19560 if (decl_specs)
19561 decl_specs->explicit_int_p = true;
19562 type = integer_type_node;
19563 break;
19564 case RID_INT_N_0:
19565 case RID_INT_N_1:
19566 case RID_INT_N_2:
19567 case RID_INT_N_3:
19568 idx = token->keyword - RID_INT_N_0;
19569 if (! int_n_enabled_p [idx])
19570 break;
19571 if (decl_specs)
19573 decl_specs->explicit_intN_p = true;
19574 decl_specs->int_n_idx = idx;
19575 /* Check if the alternate "__intN__" form has been used instead of
19576 "__intN". */
19577 if (startswith (IDENTIFIER_POINTER (token->u.value)
19578 + (IDENTIFIER_LENGTH (token->u.value) - 2), "__"))
19579 decl_specs->int_n_alt = true;
19581 type = int_n_trees [idx].signed_type;
19582 break;
19583 case RID_LONG:
19584 if (decl_specs)
19585 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
19586 type = long_integer_type_node;
19587 break;
19588 case RID_SIGNED:
19589 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
19590 type = integer_type_node;
19591 break;
19592 case RID_UNSIGNED:
19593 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
19594 type = unsigned_type_node;
19595 break;
19596 case RID_FLOAT:
19597 type = float_type_node;
19598 break;
19599 case RID_DOUBLE:
19600 type = double_type_node;
19601 break;
19602 CASE_RID_FLOATN_NX:
19603 type = FLOATN_NX_TYPE_NODE (token->keyword - RID_FLOATN_NX_FIRST);
19604 if (type == NULL_TREE)
19605 error ("%<_Float%d%s%> is not supported on this target",
19606 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].n,
19607 floatn_nx_types[token->keyword - RID_FLOATN_NX_FIRST].extended
19608 ? "x" : "");
19609 break;
19610 case RID_VOID:
19611 type = void_type_node;
19612 break;
19614 case RID_AUTO:
19615 maybe_warn_cpp0x (CPP0X_AUTO);
19616 if (parser->auto_is_implicit_function_template_parm_p)
19618 /* The 'auto' might be the placeholder return type for a function decl
19619 with trailing return type. */
19620 bool have_trailing_return_fn_decl = false;
19622 cp_parser_parse_tentatively (parser);
19623 cp_lexer_consume_token (parser->lexer);
19624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
19625 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
19626 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19627 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
19629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19631 cp_lexer_consume_token (parser->lexer);
19632 cp_parser_skip_to_closing_parenthesis (parser,
19633 /*recovering*/false,
19634 /*or_comma*/false,
19635 /*consume_paren*/true);
19636 continue;
19639 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
19641 have_trailing_return_fn_decl = true;
19642 break;
19645 cp_lexer_consume_token (parser->lexer);
19647 cp_parser_abort_tentative_parse (parser);
19649 if (have_trailing_return_fn_decl)
19651 type = make_auto ();
19652 break;
19655 if (cxx_dialect >= cxx14)
19657 type = synthesize_implicit_template_parm (parser, NULL_TREE);
19658 type = TREE_TYPE (type);
19660 else
19661 type = error_mark_node;
19663 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
19665 if (cxx_dialect < cxx14)
19666 error_at (token->location,
19667 "use of %<auto%> in lambda parameter declaration "
19668 "only available with "
19669 "%<-std=c++14%> or %<-std=gnu++14%>");
19671 else if (cxx_dialect < cxx14)
19672 error_at (token->location,
19673 "use of %<auto%> in parameter declaration "
19674 "only available with "
19675 "%<-std=c++14%> or %<-std=gnu++14%>");
19676 else if (!flag_concepts)
19677 pedwarn (token->location, 0,
19678 "use of %<auto%> in parameter declaration "
19679 "only available with %<-std=c++20%> or %<-fconcepts%>");
19681 else
19682 type = make_auto ();
19683 break;
19685 case RID_DECLTYPE:
19686 /* Since DR 743, decltype can either be a simple-type-specifier by
19687 itself or begin a nested-name-specifier. Parsing it will replace
19688 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
19689 handling below decide what to do. */
19690 cp_parser_decltype (parser);
19691 cp_lexer_set_token_position (parser->lexer, token);
19692 break;
19694 case RID_TYPEOF:
19695 /* Consume the `typeof' token. */
19696 cp_lexer_consume_token (parser->lexer);
19697 /* Parse the operand to `typeof'. */
19698 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
19699 /* If it is not already a TYPE, take its type. */
19700 if (!TYPE_P (type))
19701 type = finish_typeof (type);
19703 if (decl_specs)
19704 cp_parser_set_decl_spec_type (decl_specs, type,
19705 token,
19706 /*type_definition_p=*/false);
19708 return type;
19710 #define DEFTRAIT_TYPE(CODE, NAME, ARITY) \
19711 case RID_##CODE:
19712 #include "cp-trait.def"
19713 #undef DEFTRAIT_TYPE
19714 type = cp_parser_trait (parser, token->keyword);
19715 if (decl_specs)
19716 cp_parser_set_decl_spec_type (decl_specs, type,
19717 token,
19718 /*type_definition_p=*/false);
19720 return type;
19722 default:
19723 break;
19726 /* If token is an already-parsed decltype not followed by ::,
19727 it's a simple-type-specifier. */
19728 if (token->type == CPP_DECLTYPE
19729 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
19731 type = saved_checks_value (token->u.tree_check_value);
19732 if (decl_specs)
19734 cp_parser_set_decl_spec_type (decl_specs, type,
19735 token,
19736 /*type_definition_p=*/false);
19737 /* Remember that we are handling a decltype in order to
19738 implement the resolution of DR 1510 when the argument
19739 isn't instantiation dependent. */
19740 decl_specs->decltype_p = true;
19742 cp_lexer_consume_token (parser->lexer);
19743 return type;
19746 /* If the type-specifier was for a built-in type, we're done. */
19747 if (type)
19749 /* Record the type. */
19750 if (decl_specs
19751 && (token->keyword != RID_SIGNED
19752 && token->keyword != RID_UNSIGNED
19753 && token->keyword != RID_SHORT
19754 && token->keyword != RID_LONG))
19755 cp_parser_set_decl_spec_type (decl_specs,
19756 type,
19757 token,
19758 /*type_definition_p=*/false);
19759 if (decl_specs)
19760 decl_specs->any_specifiers_p = true;
19762 /* Consume the token. */
19763 cp_lexer_consume_token (parser->lexer);
19765 if (type == error_mark_node)
19766 return error_mark_node;
19768 /* There is no valid C++ program where a non-template type is
19769 followed by a "<". That usually indicates that the user thought
19770 that the type was a template. */
19771 cp_parser_check_for_invalid_template_id (parser, type, none_type,
19772 token->location);
19774 return TYPE_NAME (type);
19777 /* The type-specifier must be a user-defined type. */
19778 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
19780 bool qualified_p;
19781 bool global_p;
19782 const bool typename_p = (cxx_dialect >= cxx20
19783 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
19785 /* Don't gobble tokens or issue error messages if this is an
19786 optional type-specifier. */
19787 if (flags & CP_PARSER_FLAGS_OPTIONAL)
19788 cp_parser_parse_tentatively (parser);
19790 /* Remember current tentative parsing state -- if we know we need
19791 a type, we can give better diagnostics here. */
19792 bool tent = cp_parser_parsing_tentatively (parser);
19794 token = cp_lexer_peek_token (parser->lexer);
19796 /* Look for the optional `::' operator. */
19797 global_p
19798 = (cp_parser_global_scope_opt (parser,
19799 /*current_scope_valid_p=*/false)
19800 != NULL_TREE);
19801 /* Look for the nested-name specifier. */
19802 qualified_p
19803 = (cp_parser_nested_name_specifier_opt (parser,
19804 /*typename_keyword_p=*/false,
19805 /*check_dependency_p=*/true,
19806 /*type_p=*/false,
19807 /*is_declaration=*/false)
19808 != NULL_TREE);
19809 /* If we have seen a nested-name-specifier, and the next token
19810 is `template', then we are using the template-id production. */
19811 if (parser->scope
19812 && cp_parser_optional_template_keyword (parser))
19814 /* Look for the template-id. */
19815 type = cp_parser_template_id (parser,
19816 /*template_keyword_p=*/true,
19817 /*check_dependency_p=*/true,
19818 none_type,
19819 /*is_declaration=*/false);
19820 /* If the template-id did not name a type, we are out of
19821 luck. */
19822 if (TREE_CODE (type) != TYPE_DECL)
19824 /* ...unless we pretend we have seen 'typename'. */
19825 if (typename_p)
19826 type = cp_parser_make_typename_type (parser, type,
19827 token->location);
19828 else
19830 cp_parser_error (parser, "expected template-id for type");
19831 type = error_mark_node;
19835 /* DR 1812: A < following a qualified-id in a typename-specifier
19836 could safely be assumed to begin a template argument list, so
19837 the template keyword should be optional. */
19838 else if (parser->scope
19839 && qualified_p
19840 && typename_p
19841 && cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID))
19843 cp_parser_parse_tentatively (parser);
19845 type = cp_parser_template_id (parser,
19846 /*template_keyword_p=*/true,
19847 /*check_dependency_p=*/true,
19848 none_type,
19849 /*is_declaration=*/false);
19850 /* This is handled below, so back off. */
19851 if (type && concept_check_p (type))
19852 cp_parser_simulate_error (parser);
19854 if (!cp_parser_parse_definitely (parser))
19855 type = NULL_TREE;
19856 else if (TREE_CODE (type) == TEMPLATE_ID_EXPR)
19857 type = make_typename_type (parser->scope, type, typename_type,
19858 /*complain=*/tf_error);
19859 else if (TREE_CODE (type) != TYPE_DECL)
19860 type = NULL_TREE;
19863 /* Otherwise, look for a type-name. */
19864 if (!type)
19866 if (cxx_dialect >= cxx17)
19867 cp_parser_parse_tentatively (parser);
19869 type = cp_parser_type_name (parser, (qualified_p && typename_p));
19871 if (cxx_dialect >= cxx17 && !cp_parser_parse_definitely (parser))
19872 type = NULL_TREE;
19875 if (!type && flag_concepts && decl_specs)
19877 /* Try for a type-constraint with template arguments. We check
19878 decl_specs here to avoid trying this for a functional cast. */
19880 cp_parser_parse_tentatively (parser);
19882 type = cp_parser_template_id (parser,
19883 /*template_keyword_p=*/false,
19884 /*check_dependency_p=*/true,
19885 none_type,
19886 /*is_declaration=*/false);
19887 if (type && concept_check_p (type))
19889 location_t loc = EXPR_LOCATION (type);
19890 type = cp_parser_placeholder_type_specifier (parser, loc,
19891 type, tent);
19892 if (tent && type == error_mark_node)
19893 /* Perhaps it's a concept-check expression. */
19894 cp_parser_simulate_error (parser);
19896 else
19897 cp_parser_simulate_error (parser);
19899 if (!cp_parser_parse_definitely (parser))
19900 type = NULL_TREE;
19903 if (!type && cxx_dialect >= cxx17)
19905 /* Try class template argument deduction or type-constraint without
19906 template arguments. */
19907 tree name = cp_parser_identifier (parser);
19908 if (name && TREE_CODE (name) == IDENTIFIER_NODE
19909 && parser->scope != error_mark_node)
19911 location_t loc
19912 = cp_lexer_previous_token (parser->lexer)->location;
19913 tree tmpl = cp_parser_lookup_name (parser, name,
19914 none_type,
19915 /*is_template=*/false,
19916 /*is_namespace=*/false,
19917 /*check_dependency=*/true,
19918 /*ambiguous_decls=*/NULL,
19919 token->location);
19920 if (tmpl && tmpl != error_mark_node
19921 && ctad_template_p (tmpl))
19922 type = make_template_placeholder (tmpl);
19923 else if (flag_concepts && tmpl && concept_definition_p (tmpl))
19924 type = cp_parser_placeholder_type_specifier (parser, loc,
19925 tmpl, tent);
19926 else
19928 type = error_mark_node;
19929 if (!cp_parser_simulate_error (parser))
19930 cp_parser_name_lookup_error (parser, name, tmpl,
19931 NLE_TYPE, token->location);
19934 else
19935 type = error_mark_node;
19938 /* If it didn't work out, we don't have a TYPE. */
19939 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
19940 && !cp_parser_parse_definitely (parser))
19941 type = NULL_TREE;
19943 /* Keep track of all name-lookups performed in class scopes. */
19944 if (type
19945 && !global_p
19946 && !qualified_p
19947 && TREE_CODE (type) == TYPE_DECL
19948 && identifier_p (DECL_NAME (type)))
19949 maybe_note_name_used_in_class (DECL_NAME (type), type);
19951 if (type && decl_specs)
19952 cp_parser_set_decl_spec_type (decl_specs, type,
19953 token,
19954 /*type_definition_p=*/false);
19957 /* If we didn't get a type-name, issue an error message. */
19958 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
19960 cp_parser_error (parser, "expected type-name");
19961 return error_mark_node;
19964 if (type && type != error_mark_node)
19966 /* See if TYPE is an Objective-C type, and if so, parse and
19967 accept any protocol references following it. Do this before
19968 the cp_parser_check_for_invalid_template_id() call, because
19969 Objective-C types can be followed by '<...>' which would
19970 enclose protocol names rather than template arguments, and so
19971 everything is fine. */
19972 if (c_dialect_objc () && !parser->scope
19973 && (objc_is_id (type) || objc_is_class_name (type)))
19975 tree protos = cp_parser_objc_protocol_refs_opt (parser);
19976 tree qual_type = objc_get_protocol_qualified_type (type, protos);
19978 /* Clobber the "unqualified" type previously entered into
19979 DECL_SPECS with the new, improved protocol-qualified version. */
19980 if (decl_specs)
19981 decl_specs->type = qual_type;
19983 return qual_type;
19986 /* There is no valid C++ program where a non-template type is
19987 followed by a "<". That usually indicates that the user
19988 thought that the type was a template. */
19989 cp_parser_check_for_invalid_template_id (parser, type,
19990 none_type,
19991 token->location);
19994 return type;
19997 /* Parse the remainder of a placholder-type-specifier.
19999 placeholder-type-specifier:
20000 type-constraint_opt auto
20001 type-constraint_opt decltype(auto)
20003 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
20004 and passed as TMPL. This function converts TMPL to an actual type-constraint,
20005 parses the placeholder type, and performs some contextual syntactic analysis.
20007 LOC provides the location of the template name.
20009 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
20010 don't give an error if TMPL isn't a valid type-constraint, as the template-id
20011 might actually be a concept-check,
20013 Note that the Concepts TS allows the auto or decltype(auto) to be
20014 omitted in a constrained-type-specifier. */
20016 static tree
20017 cp_parser_placeholder_type_specifier (cp_parser *parser, location_t loc,
20018 tree tmpl, bool tentative)
20020 if (tmpl == error_mark_node)
20021 return error_mark_node;
20023 tree orig_tmpl = tmpl;
20025 /* Get the arguments as written for subsequent analysis. */
20026 tree args = NULL_TREE;
20027 if (TREE_CODE (tmpl) == TEMPLATE_ID_EXPR)
20029 args = TREE_OPERAND (tmpl, 1);
20030 tmpl = TREE_OPERAND (tmpl, 0);
20032 else
20033 /* A concept-name with no arguments can't be an expression. */
20034 tentative = false;
20036 tsubst_flags_t complain = tentative ? tf_none : tf_warning_or_error;
20038 /* Get the concept and prototype parameter for the constraint. */
20039 tree_pair info = finish_type_constraints (tmpl, args, complain);
20040 tree con = info.first;
20041 tree proto = info.second;
20042 if (con == error_mark_node)
20043 return error_mark_node;
20045 /* As per the standard, require auto or decltype(auto), except in some
20046 cases (template parameter lists, -fconcepts-ts enabled). */
20047 cp_token *placeholder = NULL, *close_paren = NULL;
20048 if (cxx_dialect >= cxx20)
20050 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20051 placeholder = cp_lexer_consume_token (parser->lexer);
20052 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DECLTYPE))
20054 placeholder = cp_lexer_consume_token (parser->lexer);
20055 matching_parens parens;
20056 parens.require_open (parser);
20057 cp_parser_require_keyword (parser, RID_AUTO, RT_AUTO);
20058 close_paren = parens.require_close (parser);
20062 /* A type constraint constrains a contextually determined type or type
20063 parameter pack. However, the Concepts TS does allow concepts
20064 to introduce non-type and template template parameters. */
20065 if (TREE_CODE (proto) != TYPE_DECL)
20067 if (!flag_concepts_ts
20068 || !processing_template_parmlist)
20070 if (!tentative)
20072 error_at (loc, "%qE does not constrain a type", DECL_NAME (con));
20073 inform (DECL_SOURCE_LOCATION (con), "concept defined here");
20075 return error_mark_node;
20079 /* In a template parameter list, a type-parameter can be introduced
20080 by type-constraints alone. */
20081 if (processing_template_parmlist && !placeholder)
20083 /* In a default argument we may not be creating new parameters. */
20084 if (parser->local_variables_forbidden_p & LOCAL_VARS_FORBIDDEN)
20086 /* If this assert turns out to be false, do error() instead. */
20087 gcc_assert (tentative);
20088 return error_mark_node;
20090 return build_constrained_parameter (con, proto, args);
20093 /* Diagnose issues placeholder issues. */
20094 if (!flag_concepts_ts
20095 && !parser->in_result_type_constraint_p
20096 && !placeholder)
20098 if (tentative)
20099 /* Perhaps it's a concept-check expression (c++/91073). */
20100 return error_mark_node;
20102 tree id = build_nt (TEMPLATE_ID_EXPR, tmpl, args);
20103 tree expr = DECL_P (orig_tmpl) ? DECL_NAME (con) : id;
20104 error_at (input_location,
20105 "expected %<auto%> or %<decltype(auto)%> after %qE", expr);
20106 /* Fall through. This is an error of omission. */
20108 else if (parser->in_result_type_constraint_p && placeholder)
20110 /* A trailing return type only allows type-constraints. */
20111 error_at (input_location,
20112 "unexpected placeholder in constrained result type");
20115 /* In a parameter-declaration-clause, a placeholder-type-specifier
20116 results in an invented template parameter. */
20117 if (parser->auto_is_implicit_function_template_parm_p)
20119 if (close_paren)
20121 location_t loc = make_location (placeholder->location,
20122 placeholder->location,
20123 close_paren->location);
20124 error_at (loc, "cannot declare a parameter with %<decltype(auto)%>");
20125 return error_mark_node;
20127 tree parm = build_constrained_parameter (con, proto, args);
20128 return synthesize_implicit_template_parm (parser, parm);
20131 /* Determine if the type should be deduced using template argument
20132 deduction or decltype deduction. Note that the latter is always
20133 used for type-constraints in trailing return types. */
20134 bool decltype_p = placeholder
20135 ? placeholder->keyword == RID_DECLTYPE
20136 : parser->in_result_type_constraint_p;
20138 /* Otherwise, this is the type of a variable or return type. */
20139 if (decltype_p)
20140 return make_constrained_decltype_auto (con, args);
20141 else
20142 return make_constrained_auto (con, args);
20145 /* Parse a type-name.
20147 type-name:
20148 class-name
20149 enum-name
20150 typedef-name
20151 simple-template-id [in c++0x]
20153 enum-name:
20154 identifier
20156 typedef-name:
20157 identifier
20159 Concepts:
20161 type-name:
20162 concept-name
20163 partial-concept-id
20165 concept-name:
20166 identifier
20168 Returns a TYPE_DECL for the type. */
20170 static tree
20171 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
20173 tree type_decl;
20175 /* We can't know yet whether it is a class-name or not. */
20176 cp_parser_parse_tentatively (parser);
20177 /* Try a class-name. */
20178 type_decl = cp_parser_class_name (parser,
20179 typename_keyword_p,
20180 /*template_keyword_p=*/false,
20181 none_type,
20182 /*check_dependency_p=*/true,
20183 /*class_head_p=*/false,
20184 /*is_declaration=*/false);
20185 /* If it's not a class-name, keep looking. */
20186 if (!cp_parser_parse_definitely (parser))
20188 if (cxx_dialect < cxx11)
20189 /* It must be a typedef-name or an enum-name. */
20190 return cp_parser_nonclass_name (parser);
20192 cp_parser_parse_tentatively (parser);
20193 /* It is either a simple-template-id representing an
20194 instantiation of an alias template... */
20195 type_decl = cp_parser_template_id (parser,
20196 /*template_keyword_p=*/false,
20197 /*check_dependency_p=*/true,
20198 none_type,
20199 /*is_declaration=*/false);
20200 /* Note that this must be an instantiation of an alias template
20201 because [temp.names]/6 says:
20203 A template-id that names an alias template specialization
20204 is a type-name.
20206 Whereas [temp.names]/7 says:
20208 A simple-template-id that names a class template
20209 specialization is a class-name.
20211 With concepts, this could also be a partial-concept-id that
20212 declares a non-type template parameter. */
20213 if (type_decl != NULL_TREE
20214 && TREE_CODE (type_decl) == TYPE_DECL
20215 && TYPE_DECL_ALIAS_P (type_decl))
20216 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
20217 else
20218 cp_parser_simulate_error (parser);
20220 if (!cp_parser_parse_definitely (parser))
20221 /* ... Or a typedef-name or an enum-name. */
20222 return cp_parser_nonclass_name (parser);
20225 return type_decl;
20228 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
20229 or a concept-name.
20231 enum-name:
20232 identifier
20234 typedef-name:
20235 identifier
20237 concept-name:
20238 identifier
20240 Returns a TYPE_DECL for the type. */
20242 static tree
20243 cp_parser_nonclass_name (cp_parser* parser)
20245 tree type_decl;
20246 tree identifier;
20248 cp_token *token = cp_lexer_peek_token (parser->lexer);
20249 identifier = cp_parser_identifier (parser);
20250 if (identifier == error_mark_node)
20251 return error_mark_node;
20253 /* Look up the type-name. */
20254 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
20256 type_decl = strip_using_decl (type_decl);
20258 if (TREE_CODE (type_decl) != TYPE_DECL
20259 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
20261 /* See if this is an Objective-C type. */
20262 tree protos = cp_parser_objc_protocol_refs_opt (parser);
20263 tree type = objc_get_protocol_qualified_type (identifier, protos);
20264 if (type)
20265 type_decl = TYPE_NAME (type);
20268 /* Issue an error if we did not find a type-name. */
20269 if (TREE_CODE (type_decl) != TYPE_DECL
20270 /* In Objective-C, we have the complication that class names are
20271 normally type names and start declarations (eg, the
20272 "NSObject" in "NSObject *object;"), but can be used in an
20273 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
20274 is an expression. So, a classname followed by a dot is not a
20275 valid type-name. */
20276 || (objc_is_class_name (TREE_TYPE (type_decl))
20277 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
20279 if (!cp_parser_simulate_error (parser))
20280 cp_parser_name_lookup_error (parser, identifier, type_decl,
20281 NLE_TYPE, token->location);
20282 return error_mark_node;
20284 /* Remember that the name was used in the definition of the
20285 current class so that we can check later to see if the
20286 meaning would have been different after the class was
20287 entirely defined. */
20288 else if (type_decl != error_mark_node
20289 && !parser->scope)
20290 maybe_note_name_used_in_class (identifier, type_decl);
20292 return type_decl;
20295 /* Parse an elaborated-type-specifier. Note that the grammar given
20296 here incorporates the resolution to DR68.
20298 elaborated-type-specifier:
20299 class-key :: [opt] nested-name-specifier [opt] identifier
20300 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
20301 enum-key :: [opt] nested-name-specifier [opt] identifier
20302 typename :: [opt] nested-name-specifier identifier
20303 typename :: [opt] nested-name-specifier template [opt]
20304 template-id
20306 GNU extension:
20308 elaborated-type-specifier:
20309 class-key attributes :: [opt] nested-name-specifier [opt] identifier
20310 class-key attributes :: [opt] nested-name-specifier [opt]
20311 template [opt] template-id
20312 enum attributes :: [opt] nested-name-specifier [opt] identifier
20314 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
20315 declared `friend'. If IS_DECLARATION is TRUE, then this
20316 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
20317 something is being declared.
20319 Returns the TYPE specified. */
20321 static tree
20322 cp_parser_elaborated_type_specifier (cp_parser* parser,
20323 bool is_friend,
20324 bool is_declaration)
20326 enum tag_types tag_type;
20327 tree identifier;
20328 tree type = NULL_TREE;
20329 tree attributes = NULL_TREE;
20330 tree globalscope;
20331 cp_token *token = NULL;
20333 /* For class and enum types the location of the class-key or enum-key. */
20334 location_t key_loc = cp_lexer_peek_token (parser->lexer)->location;
20335 /* For a scoped enum, the 'class' or 'struct' keyword id. */
20336 rid scoped_key = RID_MAX;
20338 /* See if we're looking at the `enum' keyword. */
20339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
20341 /* Consume the `enum' token. */
20342 cp_lexer_consume_token (parser->lexer);
20343 /* Remember that it's an enumeration type. */
20344 tag_type = enum_type;
20345 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
20346 enums) is used here. */
20347 cp_token *token = cp_lexer_peek_token (parser->lexer);
20348 if (cp_parser_is_keyword (token, scoped_key = RID_CLASS)
20349 || cp_parser_is_keyword (token, scoped_key = RID_STRUCT))
20351 location_t loc = token->location;
20352 gcc_rich_location richloc (loc);
20353 richloc.add_range (input_location);
20354 richloc.add_fixit_remove ();
20355 pedwarn (&richloc, 0, "elaborated-type-specifier for "
20356 "a scoped enum must not use the %qD keyword",
20357 token->u.value);
20358 /* Consume the `struct' or `class' and parse it anyway. */
20359 cp_lexer_consume_token (parser->lexer);
20360 /* Create a combined location for the whole scoped-enum-key. */
20361 key_loc = make_location (key_loc, key_loc, loc);
20363 else
20364 scoped_key = RID_MAX;
20366 /* Parse the attributes. */
20367 attributes = cp_parser_attributes_opt (parser);
20369 /* Or, it might be `typename'. */
20370 else if (cp_lexer_next_token_is_keyword (parser->lexer,
20371 RID_TYPENAME))
20373 /* Consume the `typename' token. */
20374 cp_lexer_consume_token (parser->lexer);
20375 /* Remember that it's a `typename' type. */
20376 tag_type = typename_type;
20378 /* Otherwise it must be a class-key. */
20379 else
20381 key_loc = cp_lexer_peek_token (parser->lexer)->location;
20382 tag_type = cp_parser_class_key (parser);
20383 if (tag_type == none_type)
20384 return error_mark_node;
20385 /* Parse the attributes. */
20386 attributes = cp_parser_attributes_opt (parser);
20389 /* Look for the `::' operator. */
20390 globalscope = cp_parser_global_scope_opt (parser,
20391 /*current_scope_valid_p=*/false);
20392 /* Look for the nested-name-specifier. */
20393 tree nested_name_specifier;
20394 if (tag_type == typename_type && !globalscope)
20396 nested_name_specifier
20397 = cp_parser_nested_name_specifier (parser,
20398 /*typename_keyword_p=*/true,
20399 /*check_dependency_p=*/true,
20400 /*type_p=*/true,
20401 is_declaration);
20402 if (!nested_name_specifier)
20403 return error_mark_node;
20405 else
20406 /* Even though `typename' is not present, the proposed resolution
20407 to Core Issue 180 says that in `class A<T>::B', `B' should be
20408 considered a type-name, even if `A<T>' is dependent. */
20409 nested_name_specifier
20410 = cp_parser_nested_name_specifier_opt (parser,
20411 /*typename_keyword_p=*/true,
20412 /*check_dependency_p=*/true,
20413 /*type_p=*/true,
20414 is_declaration);
20415 /* For everything but enumeration types, consider a template-id.
20416 For an enumeration type, consider only a plain identifier. */
20417 if (tag_type != enum_type)
20419 bool template_p = false;
20420 tree decl;
20422 /* Allow the `template' keyword. */
20423 template_p = cp_parser_optional_template_keyword (parser);
20424 /* If we didn't see `template', we don't know if there's a
20425 template-id or not. */
20426 if (!template_p)
20427 cp_parser_parse_tentatively (parser);
20428 /* The `template' keyword must follow a nested-name-specifier. */
20429 else if (!nested_name_specifier && !globalscope)
20431 cp_parser_error (parser, "%<template%> must follow a nested-"
20432 "name-specifier");
20433 return error_mark_node;
20436 /* Parse the template-id. */
20437 token = cp_lexer_peek_token (parser->lexer);
20438 decl = cp_parser_template_id (parser, template_p,
20439 /*check_dependency_p=*/true,
20440 tag_type,
20441 is_declaration);
20442 /* If we didn't find a template-id, look for an ordinary
20443 identifier. */
20444 if (!template_p && !cp_parser_parse_definitely (parser))
20446 /* We can get here when cp_parser_template_id, called by
20447 cp_parser_class_name with tag_type == none_type, succeeds
20448 and caches a BASELINK. Then, when called again here,
20449 instead of failing and returning an error_mark_node
20450 returns it (see template/typename17.C in C++11).
20451 ??? Could we diagnose this earlier? */
20452 else if (tag_type == typename_type && BASELINK_P (decl))
20454 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
20455 type = error_mark_node;
20457 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
20458 in effect, then we must assume that, upon instantiation, the
20459 template will correspond to a class. */
20460 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
20461 && tag_type == typename_type)
20462 type = make_typename_type (parser->scope, decl,
20463 typename_type,
20464 /*complain=*/tf_error);
20465 /* If the `typename' keyword is in effect and DECL is not a type
20466 decl, then type is non existent. */
20467 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
20469 else if (TREE_CODE (decl) == TYPE_DECL)
20471 type = check_elaborated_type_specifier (tag_type, decl,
20472 /*allow_template_p=*/true);
20474 /* If the next token is a semicolon, this must be a specialization,
20475 instantiation, or friend declaration. Check the scope while we
20476 still know whether or not we had a nested-name-specifier. */
20477 if (type != error_mark_node
20478 && !nested_name_specifier && !is_friend
20479 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20480 check_unqualified_spec_or_inst (type, token->location);
20482 else if (decl == error_mark_node)
20483 type = error_mark_node;
20486 if (!type)
20488 token = cp_lexer_peek_token (parser->lexer);
20489 identifier = cp_parser_identifier (parser);
20491 if (identifier == error_mark_node)
20493 parser->scope = NULL_TREE;
20494 return error_mark_node;
20497 /* For a `typename', we needn't call xref_tag. */
20498 if (tag_type == typename_type
20499 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
20500 return cp_parser_make_typename_type (parser, identifier,
20501 token->location);
20503 /* Template parameter lists apply only if we are not within a
20504 function parameter list. */
20505 bool template_parm_lists_apply
20506 = parser->num_template_parameter_lists;
20507 if (template_parm_lists_apply)
20508 for (cp_binding_level *s = current_binding_level;
20509 s && s->kind != sk_template_parms;
20510 s = s->level_chain)
20511 if (s->kind == sk_function_parms)
20512 template_parm_lists_apply = false;
20514 /* Look up a qualified name in the usual way. */
20515 if (parser->scope)
20517 tree decl;
20518 tree ambiguous_decls;
20520 decl = cp_parser_lookup_name (parser, identifier,
20521 tag_type,
20522 /*is_template=*/false,
20523 /*is_namespace=*/false,
20524 /*check_dependency=*/true,
20525 &ambiguous_decls,
20526 token->location);
20528 /* If the lookup was ambiguous, an error will already have been
20529 issued. */
20530 if (ambiguous_decls)
20531 return error_mark_node;
20533 /* If we are parsing friend declaration, DECL may be a
20534 TEMPLATE_DECL tree node here. However, we need to check
20535 whether this TEMPLATE_DECL results in valid code. Consider
20536 the following example:
20538 namespace N {
20539 template <class T> class C {};
20541 class X {
20542 template <class T> friend class N::C; // #1, valid code
20544 template <class T> class Y {
20545 friend class N::C; // #2, invalid code
20548 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
20549 name lookup of `N::C'. We see that friend declaration must
20550 be template for the code to be valid. Note that
20551 processing_template_decl does not work here since it is
20552 always 1 for the above two cases. */
20554 decl = (cp_parser_maybe_treat_template_as_class
20555 (decl, /*tag_name_p=*/is_friend
20556 && template_parm_lists_apply));
20558 if (TREE_CODE (decl) != TYPE_DECL)
20560 cp_parser_diagnose_invalid_type_name (parser,
20561 identifier,
20562 token->location);
20563 return error_mark_node;
20566 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
20568 bool allow_template = (template_parm_lists_apply
20569 || DECL_SELF_REFERENCE_P (decl));
20570 type = check_elaborated_type_specifier (tag_type, decl,
20571 allow_template);
20573 if (type == error_mark_node)
20574 return error_mark_node;
20577 /* Forward declarations of nested types, such as
20579 class C1::C2;
20580 class C1::C2::C3;
20582 are invalid unless all components preceding the final '::'
20583 are complete. If all enclosing types are complete, these
20584 declarations become merely pointless.
20586 Invalid forward declarations of nested types are errors
20587 caught elsewhere in parsing. Those that are pointless arrive
20588 here. */
20590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20591 && !is_friend && is_declaration
20592 && !processing_explicit_instantiation)
20593 warning (0, "declaration %qD does not declare anything", decl);
20595 type = TREE_TYPE (decl);
20597 else
20599 /* An elaborated-type-specifier sometimes introduces a new type and
20600 sometimes names an existing type. Normally, the rule is that it
20601 introduces a new type only if there is not an existing type of
20602 the same name already in scope. For example, given:
20604 struct S {};
20605 void f() { struct S s; }
20607 the `struct S' in the body of `f' is the same `struct S' as in
20608 the global scope; the existing definition is used. However, if
20609 there were no global declaration, this would introduce a new
20610 local class named `S'.
20612 An exception to this rule applies to the following code:
20614 namespace N { struct S; }
20616 Here, the elaborated-type-specifier names a new type
20617 unconditionally; even if there is already an `S' in the
20618 containing scope this declaration names a new type.
20619 This exception only applies if the elaborated-type-specifier
20620 forms the complete declaration:
20622 [class.name]
20624 A declaration consisting solely of `class-key identifier ;' is
20625 either a redeclaration of the name in the current scope or a
20626 forward declaration of the identifier as a class name. It
20627 introduces the name into the current scope.
20629 We are in this situation precisely when the next token is a `;'.
20631 An exception to the exception is that a `friend' declaration does
20632 *not* name a new type; i.e., given:
20634 struct S { friend struct T; };
20636 `T' is not a new type in the scope of `S'.
20638 Also, `new struct S' or `sizeof (struct S)' never results in the
20639 definition of a new type; a new type can only be declared in a
20640 declaration context. */
20642 TAG_how how;
20644 if (is_friend)
20645 /* Friends have special name lookup rules. */
20646 how = TAG_how::HIDDEN_FRIEND;
20647 else if (is_declaration
20648 && cp_lexer_next_token_is (parser->lexer,
20649 CPP_SEMICOLON))
20650 /* This is a `class-key identifier ;' */
20651 how = TAG_how::CURRENT_ONLY;
20652 else
20653 how = TAG_how::GLOBAL;
20655 bool template_p =
20656 (template_parm_lists_apply
20657 && (cp_parser_next_token_starts_class_definition_p (parser)
20658 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
20659 /* An unqualified name was used to reference this type, so
20660 there were no qualifying templates. */
20661 if (template_parm_lists_apply
20662 && !cp_parser_check_template_parameters (parser,
20663 /*num_templates=*/0,
20664 /*template_id*/false,
20665 token->location,
20666 /*declarator=*/NULL))
20667 return error_mark_node;
20669 type = xref_tag (tag_type, identifier, how, template_p);
20673 if (type == error_mark_node)
20674 return error_mark_node;
20676 /* Allow attributes on forward declarations of classes. */
20677 if (attributes)
20679 if (TREE_CODE (type) == TYPENAME_TYPE)
20680 warning (OPT_Wattributes,
20681 "attributes ignored on uninstantiated type");
20682 else if (tag_type != enum_type
20683 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM
20684 && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
20685 && ! processing_explicit_instantiation)
20686 warning (OPT_Wattributes,
20687 "attributes ignored on template instantiation");
20688 else if (is_friend && cxx11_attribute_p (attributes))
20690 if (warning (OPT_Wattributes, "attribute ignored"))
20691 inform (input_location, "an attribute that appertains to a friend "
20692 "declaration that is not a definition is ignored");
20694 else if (is_declaration && cp_parser_declares_only_class_p (parser))
20695 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
20696 else
20697 warning (OPT_Wattributes,
20698 "attributes ignored on elaborated-type-specifier that is "
20699 "not a forward declaration");
20702 if (tag_type == enum_type)
20703 cp_parser_maybe_warn_enum_key (parser, key_loc, type, scoped_key);
20704 else
20706 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
20707 for alias definition. */
20708 bool decl_class = (is_declaration
20709 && cp_parser_declares_only_class_p (parser));
20710 cp_parser_check_class_key (parser, key_loc, tag_type, type, false,
20711 decl_class);
20713 /* Indicate whether this class was declared as a `class' or as a
20714 `struct'. */
20715 if (CLASS_TYPE_P (type) && !currently_open_class (type))
20716 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
20719 /* A "<" cannot follow an elaborated type specifier. If that
20720 happens, the user was probably trying to form a template-id. */
20721 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
20722 token->location);
20724 return type;
20727 /* Parse an enum-specifier.
20729 enum-specifier:
20730 enum-head { enumerator-list [opt] }
20731 enum-head { enumerator-list , } [C++0x]
20733 enum-head:
20734 enum-key identifier [opt] enum-base [opt]
20735 enum-key nested-name-specifier identifier enum-base [opt]
20737 enum-key:
20738 enum
20739 enum class [C++0x]
20740 enum struct [C++0x]
20742 enum-base: [C++0x]
20743 : type-specifier-seq
20745 opaque-enum-specifier:
20746 enum-key identifier enum-base [opt] ;
20748 GNU Extensions:
20749 enum-key attributes[opt] identifier [opt] enum-base [opt]
20750 { enumerator-list [opt] }attributes[opt]
20751 enum-key attributes[opt] identifier [opt] enum-base [opt]
20752 { enumerator-list, }attributes[opt] [C++0x]
20754 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
20755 if the token stream isn't an enum-specifier after all. */
20757 static tree
20758 cp_parser_enum_specifier (cp_parser* parser)
20760 tree identifier;
20761 tree type = NULL_TREE;
20762 tree prev_scope;
20763 tree nested_name_specifier = NULL_TREE;
20764 tree attributes;
20765 bool scoped_enum_p = false;
20766 bool has_underlying_type = false;
20767 bool nested_being_defined = false;
20768 bool new_value_list = false;
20769 bool is_new_type = false;
20770 bool is_unnamed = false;
20771 tree underlying_type = NULL_TREE;
20772 cp_token *type_start_token = NULL;
20773 auto cleanup = make_temp_override (parser->colon_corrects_to_scope_p, false);
20775 /* Parse tentatively so that we can back up if we don't find a
20776 enum-specifier. */
20777 cp_parser_parse_tentatively (parser);
20779 /* Caller guarantees that the current token is 'enum', an identifier
20780 possibly follows, and the token after that is an opening brace.
20781 If we don't have an identifier, fabricate an anonymous name for
20782 the enumeration being defined. */
20783 cp_lexer_consume_token (parser->lexer);
20785 /* Parse the "class" or "struct", which indicates a scoped
20786 enumeration type in C++0x. */
20787 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
20788 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
20790 if (cxx_dialect < cxx11)
20791 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20793 /* Consume the `struct' or `class' token. */
20794 cp_lexer_consume_token (parser->lexer);
20796 scoped_enum_p = true;
20799 attributes = cp_parser_attributes_opt (parser);
20801 /* Clear the qualification. */
20802 parser->scope = NULL_TREE;
20803 parser->qualifying_scope = NULL_TREE;
20804 parser->object_scope = NULL_TREE;
20806 /* Figure out in what scope the declaration is being placed. */
20807 prev_scope = current_scope ();
20809 type_start_token = cp_lexer_peek_token (parser->lexer);
20811 push_deferring_access_checks (dk_no_check);
20812 nested_name_specifier
20813 = cp_parser_nested_name_specifier_opt (parser,
20814 /*typename_keyword_p=*/true,
20815 /*check_dependency_p=*/false,
20816 /*type_p=*/false,
20817 /*is_declaration=*/false);
20819 if (nested_name_specifier)
20821 tree name;
20823 identifier = cp_parser_identifier (parser);
20824 name = cp_parser_lookup_name (parser, identifier,
20825 enum_type,
20826 /*is_template=*/false,
20827 /*is_namespace=*/false,
20828 /*check_dependency=*/true,
20829 /*ambiguous_decls=*/NULL,
20830 input_location);
20831 if (name && name != error_mark_node)
20833 type = TREE_TYPE (name);
20834 if (TREE_CODE (type) == TYPENAME_TYPE)
20836 /* Are template enums allowed in ISO? */
20837 if (template_parm_scope_p ())
20838 pedwarn (type_start_token->location, OPT_Wpedantic,
20839 "%qD is an enumeration template", name);
20840 /* ignore a typename reference, for it will be solved by name
20841 in start_enum. */
20842 type = NULL_TREE;
20845 else if (nested_name_specifier == error_mark_node)
20846 /* We already issued an error. */;
20847 else
20849 error_at (type_start_token->location,
20850 "%qD does not name an enumeration in %qT",
20851 identifier, nested_name_specifier);
20852 nested_name_specifier = error_mark_node;
20855 else
20857 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20858 identifier = cp_parser_identifier (parser);
20859 else
20861 identifier = make_anon_name ();
20862 is_unnamed = true;
20863 if (scoped_enum_p)
20864 error_at (type_start_token->location,
20865 "unnamed scoped enum is not allowed");
20868 pop_deferring_access_checks ();
20870 /* Check for the `:' that denotes a specified underlying type in C++0x.
20871 Note that a ':' could also indicate a bitfield width, however. */
20872 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20874 cp_decl_specifier_seq type_specifiers;
20876 /* Consume the `:'. */
20877 cp_lexer_consume_token (parser->lexer);
20879 auto tdf
20880 = make_temp_override (parser->type_definition_forbidden_message,
20881 G_("types may not be defined in enum-base"));
20883 /* Parse the type-specifier-seq. */
20884 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
20885 /*is_declaration=*/false,
20886 /*is_trailing_return=*/false,
20887 &type_specifiers);
20889 /* At this point this is surely not elaborated type specifier. */
20890 if (!cp_parser_parse_definitely (parser))
20891 return NULL_TREE;
20893 if (cxx_dialect < cxx11)
20894 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
20896 has_underlying_type = true;
20898 /* If that didn't work, stop. */
20899 if (type_specifiers.type != error_mark_node)
20901 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
20902 /*initialized=*/0, NULL);
20903 if (underlying_type == error_mark_node
20904 || check_for_bare_parameter_packs (underlying_type))
20905 underlying_type = NULL_TREE;
20909 /* Look for the `{' but don't consume it yet. */
20910 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20912 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
20914 if (has_underlying_type)
20915 cp_parser_commit_to_tentative_parse (parser);
20916 cp_parser_error (parser, "expected %<{%>");
20917 if (has_underlying_type)
20918 return error_mark_node;
20920 /* An opaque-enum-specifier must have a ';' here. */
20921 if ((scoped_enum_p || underlying_type)
20922 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20924 if (has_underlying_type)
20925 cp_parser_commit_to_tentative_parse (parser);
20926 cp_parser_error (parser, "expected %<;%> or %<{%>");
20927 if (has_underlying_type)
20928 return error_mark_node;
20932 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
20933 return NULL_TREE;
20935 if (nested_name_specifier)
20937 if (CLASS_TYPE_P (nested_name_specifier))
20939 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
20940 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
20941 push_scope (nested_name_specifier);
20943 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
20944 push_nested_namespace (nested_name_specifier);
20947 /* Issue an error message if type-definitions are forbidden here. */
20948 if (!cp_parser_check_type_definition (parser))
20949 type = error_mark_node;
20950 else
20951 /* Create the new type. We do this before consuming the opening
20952 brace so the enum will be recorded as being on the line of its
20953 tag (or the 'enum' keyword, if there is no tag). */
20954 type = start_enum (identifier, type, underlying_type,
20955 attributes, scoped_enum_p, &is_new_type);
20957 /* If the next token is not '{' it is an opaque-enum-specifier or an
20958 elaborated-type-specifier. */
20959 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20961 auto_timevar tv (TV_PARSE_ENUM);
20963 if (nested_name_specifier
20964 && nested_name_specifier != error_mark_node)
20966 /* The following catches invalid code such as:
20967 enum class S<int>::E { A, B, C }; */
20968 if (!processing_specialization
20969 && CLASS_TYPE_P (nested_name_specifier)
20970 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
20971 error_at (type_start_token->location, "cannot add an enumerator "
20972 "list to a template instantiation");
20974 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
20976 error_at (type_start_token->location,
20977 "%<%T::%E%> has not been declared",
20978 TYPE_CONTEXT (nested_name_specifier),
20979 nested_name_specifier);
20980 type = error_mark_node;
20982 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
20983 && !CLASS_TYPE_P (nested_name_specifier))
20985 error_at (type_start_token->location, "nested name specifier "
20986 "%qT for enum declaration does not name a class "
20987 "or namespace", nested_name_specifier);
20988 type = error_mark_node;
20990 /* If that scope does not contain the scope in which the
20991 class was originally declared, the program is invalid. */
20992 else if (prev_scope && !is_ancestor (prev_scope,
20993 nested_name_specifier))
20995 if (at_namespace_scope_p ())
20996 error_at (type_start_token->location,
20997 "declaration of %qD in namespace %qD which does not "
20998 "enclose %qD",
20999 type, prev_scope, nested_name_specifier);
21000 else
21001 error_at (type_start_token->location,
21002 "declaration of %qD in %qD which does not "
21003 "enclose %qD",
21004 type, prev_scope, nested_name_specifier);
21005 type = error_mark_node;
21007 /* If that scope is the scope where the declaration is being placed
21008 the program is invalid. */
21009 else if (CLASS_TYPE_P (nested_name_specifier)
21010 && CLASS_TYPE_P (prev_scope)
21011 && same_type_p (nested_name_specifier, prev_scope))
21013 permerror (type_start_token->location,
21014 "extra qualification not allowed");
21015 nested_name_specifier = NULL_TREE;
21019 if (scoped_enum_p)
21020 begin_scope (sk_scoped_enum, type);
21022 /* Consume the opening brace. */
21023 matching_braces braces;
21024 braces.consume_open (parser);
21026 if (type == error_mark_node)
21027 ; /* Nothing to add */
21028 else if (OPAQUE_ENUM_P (type)
21029 || (cxx_dialect > cxx98 && processing_specialization))
21031 new_value_list = true;
21032 SET_OPAQUE_ENUM_P (type, false);
21033 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
21035 else
21037 error_at (type_start_token->location,
21038 "multiple definition of %q#T", type);
21039 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
21040 "previous definition here");
21041 type = error_mark_node;
21044 if (type == error_mark_node)
21045 cp_parser_skip_to_end_of_block_or_statement (parser);
21046 /* If the next token is not '}', then there are some enumerators. */
21047 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21049 if (is_unnamed && !scoped_enum_p
21050 /* Don't warn for enum {} a; here. */
21051 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SEMICOLON))
21052 pedwarn (type_start_token->location, OPT_Wpedantic,
21053 "ISO C++ forbids empty unnamed enum");
21055 else
21057 /* We've seen a '{' so we know we're in an enum-specifier.
21058 Commit to any tentative parse to get syntax errors. */
21059 cp_parser_commit_to_tentative_parse (parser);
21060 cp_parser_enumerator_list (parser, type);
21063 /* Consume the final '}'. */
21064 braces.require_close (parser);
21066 if (scoped_enum_p)
21067 finish_scope ();
21069 else
21071 /* If a ';' follows, then it is an opaque-enum-specifier
21072 and additional restrictions apply. */
21073 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21075 if (is_unnamed)
21076 error_at (type_start_token->location,
21077 "opaque-enum-specifier without name");
21078 else if (nested_name_specifier)
21079 error_at (type_start_token->location,
21080 "opaque-enum-specifier must use a simple identifier");
21084 /* Look for trailing attributes to apply to this enumeration, and
21085 apply them if appropriate. */
21086 if (cp_parser_allow_gnu_extensions_p (parser))
21088 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
21089 cplus_decl_attributes (&type,
21090 trailing_attr,
21091 (int) ATTR_FLAG_TYPE_IN_PLACE);
21094 /* Finish up the enumeration. */
21095 if (type != error_mark_node)
21097 if (new_value_list)
21098 finish_enum_value_list (type);
21099 if (is_new_type)
21100 finish_enum (type);
21103 if (nested_name_specifier)
21105 if (CLASS_TYPE_P (nested_name_specifier))
21107 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
21108 pop_scope (nested_name_specifier);
21110 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
21111 pop_nested_namespace (nested_name_specifier);
21113 return type;
21116 /* Parse an enumerator-list. The enumerators all have the indicated
21117 TYPE.
21119 enumerator-list:
21120 enumerator-definition
21121 enumerator-list , enumerator-definition */
21123 static void
21124 cp_parser_enumerator_list (cp_parser* parser, tree type)
21126 while (true)
21128 /* Parse an enumerator-definition. */
21129 cp_parser_enumerator_definition (parser, type);
21131 /* If the next token is not a ',', we've reached the end of
21132 the list. */
21133 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21134 break;
21135 /* Otherwise, consume the `,' and keep going. */
21136 cp_lexer_consume_token (parser->lexer);
21137 /* If the next token is a `}', there is a trailing comma. */
21138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21140 if (cxx_dialect < cxx11)
21141 pedwarn (input_location, OPT_Wpedantic,
21142 "comma at end of enumerator list");
21143 break;
21148 /* Parse an enumerator-definition. The enumerator has the indicated
21149 TYPE.
21151 enumerator-definition:
21152 enumerator
21153 enumerator = constant-expression
21155 enumerator:
21156 identifier
21158 GNU Extensions:
21160 enumerator-definition:
21161 enumerator attributes [opt]
21162 enumerator attributes [opt] = constant-expression */
21164 static void
21165 cp_parser_enumerator_definition (cp_parser* parser, tree type)
21167 tree identifier;
21168 tree value;
21169 location_t loc;
21171 /* Save the input location because we are interested in the location
21172 of the identifier and not the location of the explicit value. */
21173 loc = cp_lexer_peek_token (parser->lexer)->location;
21175 /* Look for the identifier. */
21176 identifier = cp_parser_identifier (parser);
21177 if (identifier == error_mark_node)
21178 return;
21180 /* Parse any specified attributes. */
21181 tree attrs = cp_parser_attributes_opt (parser);
21183 /* If the next token is an '=', then there is an explicit value. */
21184 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21186 /* Consume the `=' token. */
21187 cp_lexer_consume_token (parser->lexer);
21188 /* Parse the value. */
21189 value = cp_parser_constant_expression (parser);
21191 else
21192 value = NULL_TREE;
21194 /* If we are processing a template, make sure the initializer of the
21195 enumerator doesn't contain any bare template parameter pack. */
21196 if (current_lambda_expr ())
21198 /* In a lambda it should work, but doesn't currently. */
21199 if (uses_parameter_packs (value))
21201 sorry ("unexpanded parameter pack in enumerator in lambda");
21202 value = error_mark_node;
21205 else if (check_for_bare_parameter_packs (value))
21206 value = error_mark_node;
21208 /* Create the enumerator. */
21209 build_enumerator (identifier, value, type, attrs, loc);
21212 /* Parse a namespace-name.
21214 namespace-name:
21215 original-namespace-name
21216 namespace-alias
21218 Returns the NAMESPACE_DECL for the namespace. */
21220 static tree
21221 cp_parser_namespace_name (cp_parser* parser)
21223 tree identifier;
21224 tree namespace_decl;
21226 cp_token *token = cp_lexer_peek_token (parser->lexer);
21228 /* Get the name of the namespace. */
21229 identifier = cp_parser_identifier (parser);
21230 if (identifier == error_mark_node)
21231 return error_mark_node;
21233 /* Look up the identifier in the currently active scope. Look only
21234 for namespaces, due to:
21236 [basic.lookup.udir]
21238 When looking up a namespace-name in a using-directive or alias
21239 definition, only namespace names are considered.
21241 And:
21243 [basic.lookup.qual]
21245 During the lookup of a name preceding the :: scope resolution
21246 operator, object, function, and enumerator names are ignored.
21248 (Note that cp_parser_qualifying_entity only calls this
21249 function if the token after the name is the scope resolution
21250 operator.) */
21251 namespace_decl = cp_parser_lookup_name (parser, identifier,
21252 none_type,
21253 /*is_template=*/false,
21254 /*is_namespace=*/true,
21255 /*check_dependency=*/true,
21256 /*ambiguous_decls=*/NULL,
21257 token->location);
21258 /* If it's not a namespace, issue an error. */
21259 if (namespace_decl == error_mark_node
21260 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
21262 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21264 auto_diagnostic_group d;
21265 name_hint hint;
21266 if (namespace_decl == error_mark_node
21267 && parser->scope && TREE_CODE (parser->scope) == NAMESPACE_DECL)
21268 hint = suggest_alternative_in_explicit_scope (token->location,
21269 identifier,
21270 parser->scope);
21271 if (const char *suggestion = hint.suggestion ())
21273 gcc_rich_location richloc (token->location);
21274 richloc.add_fixit_replace (suggestion);
21275 error_at (&richloc,
21276 "%qD is not a namespace-name; did you mean %qs?",
21277 identifier, suggestion);
21279 else
21280 error_at (token->location, "%qD is not a namespace-name",
21281 identifier);
21283 else
21284 cp_parser_error (parser, "expected namespace-name");
21285 namespace_decl = error_mark_node;
21288 return namespace_decl;
21291 /* Parse a namespace-definition.
21293 namespace-definition:
21294 named-namespace-definition
21295 unnamed-namespace-definition
21297 named-namespace-definition:
21298 original-namespace-definition
21299 extension-namespace-definition
21301 original-namespace-definition:
21302 namespace identifier { namespace-body }
21304 extension-namespace-definition:
21305 namespace original-namespace-name { namespace-body }
21307 unnamed-namespace-definition:
21308 namespace { namespace-body } */
21310 static void
21311 cp_parser_namespace_definition (cp_parser* parser)
21313 tree identifier;
21314 int nested_definition_count = 0;
21316 cp_ensure_no_omp_declare_simd (parser);
21317 cp_ensure_no_oacc_routine (parser);
21319 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
21320 const bool topmost_inline_p = is_inline;
21322 if (is_inline)
21324 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
21325 cp_lexer_consume_token (parser->lexer);
21328 /* Look for the `namespace' keyword. */
21329 cp_token* token
21330 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21332 /* Parse any specified attributes before the identifier. */
21333 tree attribs = cp_parser_attributes_opt (parser);
21335 for (;;)
21337 identifier = NULL_TREE;
21339 bool nested_inline_p = cp_lexer_next_token_is_keyword (parser->lexer,
21340 RID_INLINE);
21341 if (nested_inline_p && nested_definition_count != 0)
21343 if (pedantic && cxx_dialect < cxx20)
21344 pedwarn (cp_lexer_peek_token (parser->lexer)->location,
21345 OPT_Wc__20_extensions, "nested inline namespace "
21346 "definitions only available with %<-std=c++20%> or "
21347 "%<-std=gnu++20%>");
21348 cp_lexer_consume_token (parser->lexer);
21351 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21353 identifier = cp_parser_identifier (parser);
21355 if (cp_next_tokens_can_be_std_attribute_p (parser))
21356 pedwarn (input_location, OPT_Wpedantic,
21357 "standard attributes on namespaces must precede "
21358 "the namespace name");
21360 /* Parse any attributes specified after the identifier. */
21361 attribs = attr_chainon (attribs, cp_parser_attributes_opt (parser));
21364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21366 /* Don't forget that the innermost namespace might have been
21367 marked as inline. Use |= because we cannot overwrite
21368 IS_INLINE in case the outermost namespace is inline, but
21369 there are no nested inlines. */
21370 is_inline |= nested_inline_p;
21371 break;
21374 if (!nested_definition_count && pedantic && cxx_dialect < cxx17)
21375 pedwarn (input_location, OPT_Wc__17_extensions,
21376 "nested namespace definitions only available with "
21377 "%<-std=c++17%> or %<-std=gnu++17%>");
21379 /* Nested namespace names can create new namespaces (unlike
21380 other qualified-ids). */
21381 if (int count = (identifier
21382 ? push_namespace (identifier, nested_inline_p)
21383 : 0))
21384 nested_definition_count += count;
21385 else
21386 cp_parser_error (parser, "nested namespace name required");
21387 cp_lexer_consume_token (parser->lexer);
21390 if (nested_definition_count && !identifier)
21391 cp_parser_error (parser, "namespace name required");
21393 if (nested_definition_count && attribs)
21394 error_at (token->location,
21395 "a nested namespace definition cannot have attributes");
21396 if (nested_definition_count && topmost_inline_p)
21397 error_at (token->location,
21398 "a nested namespace definition cannot be inline");
21400 /* Start the namespace. */
21401 nested_definition_count += push_namespace (identifier, is_inline);
21403 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
21405 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
21407 /* Look for the `{' to validate starting the namespace. */
21408 matching_braces braces;
21409 if (braces.require_open (parser))
21411 /* Parse the body of the namespace. */
21412 cp_parser_namespace_body (parser);
21414 /* Look for the final `}'. */
21415 braces.require_close (parser);
21418 if (has_visibility)
21419 pop_visibility (1);
21421 /* Pop the nested namespace definitions. */
21422 while (nested_definition_count--)
21423 pop_namespace ();
21426 /* Parse a namespace-body.
21428 namespace-body:
21429 declaration-seq [opt] */
21431 static void
21432 cp_parser_namespace_body (cp_parser* parser)
21434 cp_parser_declaration_seq_opt (parser);
21437 /* Parse a namespace-alias-definition.
21439 namespace-alias-definition:
21440 namespace identifier = qualified-namespace-specifier ; */
21442 static void
21443 cp_parser_namespace_alias_definition (cp_parser* parser)
21445 tree identifier;
21446 tree namespace_specifier;
21448 cp_token *token = cp_lexer_peek_token (parser->lexer);
21450 /* Look for the `namespace' keyword. */
21451 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21452 /* Look for the identifier. */
21453 identifier = cp_parser_identifier (parser);
21454 if (identifier == error_mark_node)
21455 return;
21456 /* Look for the `=' token. */
21457 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
21458 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21460 error_at (token->location, "%<namespace%> definition is not allowed here");
21461 /* Skip the definition. */
21462 cp_lexer_consume_token (parser->lexer);
21463 if (cp_parser_skip_to_closing_brace (parser))
21464 cp_lexer_consume_token (parser->lexer);
21465 return;
21467 cp_parser_require (parser, CPP_EQ, RT_EQ);
21468 /* Look for the qualified-namespace-specifier. */
21469 namespace_specifier
21470 = cp_parser_qualified_namespace_specifier (parser);
21471 cp_warn_deprecated_use_scopes (namespace_specifier);
21472 /* Look for the `;' token. */
21473 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21475 /* Register the alias in the symbol table. */
21476 do_namespace_alias (identifier, namespace_specifier);
21479 /* Parse a qualified-namespace-specifier.
21481 qualified-namespace-specifier:
21482 :: [opt] nested-name-specifier [opt] namespace-name
21484 Returns a NAMESPACE_DECL corresponding to the specified
21485 namespace. */
21487 static tree
21488 cp_parser_qualified_namespace_specifier (cp_parser* parser)
21490 /* Look for the optional `::'. */
21491 cp_parser_global_scope_opt (parser,
21492 /*current_scope_valid_p=*/false);
21494 /* Look for the optional nested-name-specifier. */
21495 cp_parser_nested_name_specifier_opt (parser,
21496 /*typename_keyword_p=*/false,
21497 /*check_dependency_p=*/true,
21498 /*type_p=*/false,
21499 /*is_declaration=*/true);
21501 return cp_parser_namespace_name (parser);
21504 /* Subroutine of cp_parser_using_declaration. */
21506 static tree
21507 finish_using_decl (tree qscope, tree identifier, bool typename_p = false)
21509 tree decl = NULL_TREE;
21510 if (at_class_scope_p ())
21512 /* Create the USING_DECL. */
21513 decl = do_class_using_decl (qscope, identifier);
21515 if (check_for_bare_parameter_packs (decl))
21516 return error_mark_node;
21518 if (decl && typename_p)
21519 USING_DECL_TYPENAME_P (decl) = 1;
21521 /* Add it to the list of members in this class. */
21522 finish_member_declaration (decl);
21524 else
21525 finish_nonmember_using_decl (qscope, identifier);
21526 return decl;
21529 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
21530 access declaration.
21532 using-declaration:
21533 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
21534 using :: unqualified-id ;
21536 access-declaration:
21537 qualified-id ;
21541 static bool
21542 cp_parser_using_declaration (cp_parser* parser,
21543 bool access_declaration_p)
21545 cp_token *token;
21546 bool typename_p = false;
21547 bool global_scope_p;
21548 tree identifier;
21549 tree qscope;
21550 int oldcount = errorcount;
21551 cp_token *diag_token = NULL;
21553 if (access_declaration_p)
21555 diag_token = cp_lexer_peek_token (parser->lexer);
21556 cp_parser_parse_tentatively (parser);
21558 else
21560 /* Look for the `using' keyword. */
21561 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21563 again:
21564 /* Peek at the next token. */
21565 token = cp_lexer_peek_token (parser->lexer);
21566 /* See if it's `typename'. */
21567 if (token->keyword == RID_TYPENAME)
21569 /* Remember that we've seen it. */
21570 typename_p = true;
21571 /* Consume the `typename' token. */
21572 cp_lexer_consume_token (parser->lexer);
21576 /* Look for the optional global scope qualification. */
21577 global_scope_p
21578 = (cp_parser_global_scope_opt (parser,
21579 /*current_scope_valid_p=*/false)
21580 != NULL_TREE);
21582 /* If we saw `typename', or didn't see `::', then there must be a
21583 nested-name-specifier present. */
21584 if (typename_p || !global_scope_p)
21586 qscope = cp_parser_nested_name_specifier (parser, typename_p,
21587 /*check_dependency_p=*/true,
21588 /*type_p=*/false,
21589 /*is_declaration=*/true);
21590 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
21592 cp_parser_skip_to_end_of_block_or_statement (parser);
21593 return false;
21596 /* Otherwise, we could be in either of the two productions. In that
21597 case, treat the nested-name-specifier as optional. */
21598 else
21599 qscope = cp_parser_nested_name_specifier_opt (parser,
21600 /*typename_keyword_p=*/false,
21601 /*check_dependency_p=*/true,
21602 /*type_p=*/false,
21603 /*is_declaration=*/true);
21604 if (!qscope)
21605 qscope = global_namespace;
21607 cp_warn_deprecated_use_scopes (qscope);
21609 if (access_declaration_p && cp_parser_error_occurred (parser))
21610 /* Something has already gone wrong; there's no need to parse
21611 further. Since an error has occurred, the return value of
21612 cp_parser_parse_definitely will be false, as required. */
21613 return cp_parser_parse_definitely (parser);
21615 token = cp_lexer_peek_token (parser->lexer);
21616 /* Parse the unqualified-id. */
21617 identifier = cp_parser_unqualified_id (parser,
21618 /*template_keyword_p=*/false,
21619 /*check_dependency_p=*/true,
21620 /*declarator_p=*/true,
21621 /*optional_p=*/false);
21623 if (access_declaration_p)
21625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21626 cp_parser_simulate_error (parser);
21627 if (!cp_parser_parse_definitely (parser))
21628 return false;
21630 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21632 cp_token *ell = cp_lexer_consume_token (parser->lexer);
21633 if (cxx_dialect < cxx17)
21634 pedwarn (ell->location, OPT_Wc__17_extensions,
21635 "pack expansion in using-declaration only available "
21636 "with %<-std=c++17%> or %<-std=gnu++17%>");
21637 qscope = make_pack_expansion (qscope);
21640 /* The function we call to handle a using-declaration is different
21641 depending on what scope we are in. */
21642 if (qscope == error_mark_node || identifier == error_mark_node)
21644 else if (!identifier_p (identifier)
21645 && TREE_CODE (identifier) != BIT_NOT_EXPR)
21646 /* [namespace.udecl]
21648 A using declaration shall not name a template-id. */
21649 error_at (token->location,
21650 "a template-id may not appear in a using-declaration");
21651 else
21653 tree decl = finish_using_decl (qscope, identifier, typename_p);
21655 if (decl == error_mark_node)
21657 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21658 return false;
21662 if (!access_declaration_p
21663 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21665 cp_token *comma = cp_lexer_consume_token (parser->lexer);
21666 if (cxx_dialect < cxx17)
21667 pedwarn (comma->location, OPT_Wc__17_extensions,
21668 "comma-separated list in using-declaration only available "
21669 "with %<-std=c++17%> or %<-std=gnu++17%>");
21670 goto again;
21673 /* Look for the final `;'. */
21674 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21676 if (access_declaration_p && errorcount == oldcount)
21677 warning_at (diag_token->location, OPT_Wdeprecated,
21678 "access declarations are deprecated "
21679 "in favour of using-declarations; "
21680 "suggestion: add the %<using%> keyword");
21682 return true;
21685 /* C++20 using enum declaration.
21687 using-enum-declaration :
21688 using elaborated-enum-specifier ; */
21690 static void
21691 cp_parser_using_enum (cp_parser *parser)
21693 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21695 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
21696 breaks one of the motivating examples in using-enum-5.C.
21697 cp_parser_simple_type_specifier seems to be closer to what we actually
21698 want, though that hasn't been properly specified yet. */
21700 /* Consume 'enum'. */
21701 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM));
21702 cp_lexer_consume_token (parser->lexer);
21704 cp_token *start = cp_lexer_peek_token (parser->lexer);
21706 tree type = (cp_parser_simple_type_specifier
21707 (parser, NULL, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
21709 cp_token *end = cp_lexer_previous_token (parser->lexer);
21711 if (type == error_mark_node
21712 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21714 cp_parser_skip_to_end_of_block_or_statement (parser);
21715 return;
21717 if (TREE_CODE (type) == TYPE_DECL)
21718 type = TREE_TYPE (type);
21720 /* The elaborated-enum-specifier shall not name a dependent type and the type
21721 shall have a reachable enum-specifier. */
21722 const char *msg = nullptr;
21723 if (cxx_dialect < cxx20)
21724 msg = _("%<using enum%> "
21725 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
21726 else if (dependent_type_p (type))
21727 msg = _("%<using enum%> of dependent type %qT");
21728 else if (TREE_CODE (type) != ENUMERAL_TYPE)
21729 msg = _("%<using enum%> of non-enumeration type %q#T");
21730 else if (!COMPLETE_TYPE_P (type))
21731 msg = _("%<using enum%> of incomplete type %qT");
21732 else if (OPAQUE_ENUM_P (type))
21733 msg = _("%<using enum%> of %qT before its enum-specifier");
21734 if (msg)
21736 location_t loc = make_location (start, start, end);
21737 auto_diagnostic_group g;
21738 error_at (loc, msg, type);
21739 loc = location_of (type);
21740 if (cxx_dialect < cxx20 || loc == input_location)
21742 else if (OPAQUE_ENUM_P (type))
21743 inform (loc, "opaque-enum-declaration here");
21744 else
21745 inform (loc, "declared here");
21748 /* A using-enum-declaration introduces the enumerator names of the named
21749 enumeration as if by a using-declaration for each enumerator. */
21750 if (TREE_CODE (type) == ENUMERAL_TYPE)
21751 for (tree v = TYPE_VALUES (type); v; v = TREE_CHAIN (v))
21752 finish_using_decl (type, DECL_NAME (TREE_VALUE (v)));
21755 /* Parse an alias-declaration.
21757 alias-declaration:
21758 using identifier attribute-specifier-seq [opt] = type-id */
21760 static tree
21761 cp_parser_alias_declaration (cp_parser* parser)
21763 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
21764 location_t id_location, type_location;
21765 cp_declarator *declarator;
21766 cp_decl_specifier_seq decl_specs;
21767 bool member_p;
21768 const char *saved_message = NULL;
21770 /* Look for the `using' keyword. */
21771 cp_token *using_token
21772 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
21773 if (using_token == NULL)
21774 return error_mark_node;
21776 id_location = cp_lexer_peek_token (parser->lexer)->location;
21777 id = cp_parser_identifier (parser);
21778 if (id == error_mark_node)
21779 return error_mark_node;
21781 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
21782 attributes = cp_parser_attributes_opt (parser);
21783 if (attributes == error_mark_node)
21784 return error_mark_node;
21786 cp_parser_require (parser, CPP_EQ, RT_EQ);
21788 if (cp_parser_error_occurred (parser))
21789 return error_mark_node;
21791 cp_parser_commit_to_tentative_parse (parser);
21793 /* Now we are going to parse the type-id of the declaration. */
21796 [dcl.type]/3 says:
21798 "A type-specifier-seq shall not define a class or enumeration
21799 unless it appears in the type-id of an alias-declaration (7.1.3) that
21800 is not the declaration of a template-declaration."
21802 In other words, if we currently are in an alias template, the
21803 type-id should not define a type.
21805 So let's set parser->type_definition_forbidden_message in that
21806 case; cp_parser_check_type_definition (called by
21807 cp_parser_class_specifier) will then emit an error if a type is
21808 defined in the type-id. */
21809 if (parser->num_template_parameter_lists)
21811 saved_message = parser->type_definition_forbidden_message;
21812 parser->type_definition_forbidden_message =
21813 G_("types may not be defined in alias template declarations");
21816 type = cp_parser_type_id (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
21817 &type_location);
21819 /* Restore the error message if need be. */
21820 if (parser->num_template_parameter_lists)
21821 parser->type_definition_forbidden_message = saved_message;
21823 if (type == error_mark_node
21824 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
21826 cp_parser_skip_to_end_of_block_or_statement (parser);
21827 return error_mark_node;
21830 /* A typedef-name can also be introduced by an alias-declaration. The
21831 identifier following the using keyword becomes a typedef-name. It has
21832 the same semantics as if it were introduced by the typedef
21833 specifier. In particular, it does not define a new type and it shall
21834 not appear in the type-id. */
21836 clear_decl_specs (&decl_specs);
21837 decl_specs.type = type;
21838 if (attributes != NULL_TREE)
21840 decl_specs.attributes = attributes;
21841 set_and_check_decl_spec_loc (&decl_specs,
21842 ds_attribute,
21843 attrs_token);
21845 set_and_check_decl_spec_loc (&decl_specs,
21846 ds_typedef,
21847 using_token);
21848 set_and_check_decl_spec_loc (&decl_specs,
21849 ds_alias,
21850 using_token);
21851 decl_specs.locations[ds_type_spec] = type_location;
21853 if (parser->num_template_parameter_lists
21854 && !cp_parser_check_template_parameters (parser,
21855 /*num_templates=*/0,
21856 /*template_id*/false,
21857 id_location,
21858 /*declarator=*/NULL))
21859 return error_mark_node;
21861 declarator = make_id_declarator (NULL_TREE, id, sfk_none, id_location);
21863 member_p = at_class_scope_p ();
21864 if (member_p)
21865 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
21866 NULL_TREE, attributes);
21867 else
21868 decl = start_decl (declarator, &decl_specs, 0,
21869 attributes, NULL_TREE, &pushed_scope);
21870 if (decl == error_mark_node)
21871 return decl;
21873 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
21875 if (pushed_scope)
21876 pop_scope (pushed_scope);
21878 /* If decl is a template, return its TEMPLATE_DECL so that it gets
21879 added into the symbol table; otherwise, return the TYPE_DECL. */
21880 if (DECL_LANG_SPECIFIC (decl)
21881 && DECL_TEMPLATE_INFO (decl)
21882 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
21884 decl = DECL_TI_TEMPLATE (decl);
21885 if (member_p)
21886 check_member_template (decl);
21889 return decl;
21892 /* Parse a using-directive.
21894 using-directive:
21895 attribute-specifier-seq [opt] using namespace :: [opt]
21896 nested-name-specifier [opt] namespace-name ; */
21898 static void
21899 cp_parser_using_directive (cp_parser* parser)
21901 tree namespace_decl;
21902 tree attribs = cp_parser_std_attribute_spec_seq (parser);
21903 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21905 /* Error during attribute parsing that resulted in skipping
21906 to next semicolon. */
21907 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21908 return;
21911 /* Look for the `using' keyword. */
21912 cp_parser_require_keyword (parser, RID_USING, RT_USING);
21913 /* And the `namespace' keyword. */
21914 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
21915 /* Look for the optional `::' operator. */
21916 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
21917 /* And the optional nested-name-specifier. */
21918 cp_parser_nested_name_specifier_opt (parser,
21919 /*typename_keyword_p=*/false,
21920 /*check_dependency_p=*/true,
21921 /*type_p=*/false,
21922 /*is_declaration=*/true);
21923 /* Get the namespace being used. */
21924 namespace_decl = cp_parser_namespace_name (parser);
21925 cp_warn_deprecated_use_scopes (namespace_decl);
21926 /* And any specified GNU attributes. */
21927 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
21928 attribs = chainon (attribs, cp_parser_gnu_attributes_opt (parser));
21930 /* Update the symbol table. */
21931 finish_using_directive (namespace_decl, attribs);
21933 /* Look for the final `;'. */
21934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
21937 /* Parse an asm-definition.
21939 asm-qualifier:
21940 volatile
21941 inline
21942 goto
21944 asm-qualifier-list:
21945 asm-qualifier
21946 asm-qualifier-list asm-qualifier
21948 asm-definition:
21949 asm ( string-literal ) ;
21951 GNU Extension:
21953 asm-definition:
21954 asm asm-qualifier-list [opt] ( string-literal ) ;
21955 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
21956 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21957 : asm-operand-list [opt] ) ;
21958 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
21959 : asm-operand-list [opt]
21960 : asm-clobber-list [opt] ) ;
21961 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
21962 : asm-clobber-list [opt]
21963 : asm-goto-list ) ;
21965 The form with asm-goto-list is valid if and only if the asm-qualifier-list
21966 contains goto, and is the only allowed form in that case. No duplicates are
21967 allowed in an asm-qualifier-list. */
21969 static void
21970 cp_parser_asm_definition (cp_parser* parser)
21972 tree string;
21973 tree outputs = NULL_TREE;
21974 tree inputs = NULL_TREE;
21975 tree clobbers = NULL_TREE;
21976 tree labels = NULL_TREE;
21977 tree asm_stmt;
21978 bool extended_p = false;
21979 bool invalid_inputs_p = false;
21980 bool invalid_outputs_p = false;
21981 required_token missing = RT_NONE;
21982 location_t asm_loc = cp_lexer_peek_token (parser->lexer)->location;
21984 /* Look for the `asm' keyword. */
21985 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
21987 /* In C++20, unevaluated inline assembly is permitted in constexpr
21988 functions. */
21989 if (parser->in_function_body
21990 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
21991 && cxx_dialect < cxx20)
21992 pedwarn (asm_loc, OPT_Wc__20_extensions, "%<asm%> in %<constexpr%> "
21993 "function only available with %<-std=c++20%> or "
21994 "%<-std=gnu++20%>");
21996 /* Handle the asm-qualifier-list. */
21997 location_t volatile_loc = UNKNOWN_LOCATION;
21998 location_t inline_loc = UNKNOWN_LOCATION;
21999 location_t goto_loc = UNKNOWN_LOCATION;
22000 location_t first_loc = UNKNOWN_LOCATION;
22002 if (cp_parser_allow_gnu_extensions_p (parser))
22003 for (;;)
22005 cp_token *token = cp_lexer_peek_token (parser->lexer);
22006 location_t loc = token->location;
22007 switch (cp_lexer_peek_token (parser->lexer)->keyword)
22009 case RID_VOLATILE:
22010 if (volatile_loc)
22012 error_at (loc, "duplicate %<asm%> qualifier %qT",
22013 token->u.value);
22014 inform (volatile_loc, "first seen here");
22016 else
22018 if (!parser->in_function_body)
22019 warning_at (loc, 0, "%<asm%> qualifier %qT ignored "
22020 "outside of function body", token->u.value);
22021 volatile_loc = loc;
22023 cp_lexer_consume_token (parser->lexer);
22024 continue;
22026 case RID_INLINE:
22027 if (inline_loc)
22029 error_at (loc, "duplicate %<asm%> qualifier %qT",
22030 token->u.value);
22031 inform (inline_loc, "first seen here");
22033 else
22034 inline_loc = loc;
22035 if (!first_loc)
22036 first_loc = loc;
22037 cp_lexer_consume_token (parser->lexer);
22038 continue;
22040 case RID_GOTO:
22041 if (goto_loc)
22043 error_at (loc, "duplicate %<asm%> qualifier %qT",
22044 token->u.value);
22045 inform (goto_loc, "first seen here");
22047 else
22048 goto_loc = loc;
22049 if (!first_loc)
22050 first_loc = loc;
22051 cp_lexer_consume_token (parser->lexer);
22052 continue;
22054 case RID_CONST:
22055 case RID_RESTRICT:
22056 error_at (loc, "%qT is not an %<asm%> qualifier", token->u.value);
22057 cp_lexer_consume_token (parser->lexer);
22058 continue;
22060 default:
22061 break;
22063 break;
22066 bool volatile_p = (volatile_loc != UNKNOWN_LOCATION);
22067 bool inline_p = (inline_loc != UNKNOWN_LOCATION);
22068 bool goto_p = (goto_loc != UNKNOWN_LOCATION);
22070 if (!parser->in_function_body && (inline_p || goto_p))
22072 error_at (first_loc, "%<asm%> qualifier outside of function body");
22073 inline_p = goto_p = false;
22076 /* Look for the opening `('. */
22077 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22078 return;
22079 /* Look for the string. */
22080 string = cp_parser_string_literal (parser, false, false);
22081 if (string == error_mark_node)
22083 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22084 /*consume_paren=*/true);
22085 return;
22088 /* If we're allowing GNU extensions, check for the extended assembly
22089 syntax. Unfortunately, the `:' tokens need not be separated by
22090 a space in C, and so, for compatibility, we tolerate that here
22091 too. Doing that means that we have to treat the `::' operator as
22092 two `:' tokens. */
22093 if (cp_parser_allow_gnu_extensions_p (parser)
22094 && parser->in_function_body
22095 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
22096 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
22098 bool inputs_p = false;
22099 bool clobbers_p = false;
22100 bool labels_p = false;
22102 /* The extended syntax was used. */
22103 extended_p = true;
22105 /* Look for outputs. */
22106 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22108 /* Consume the `:'. */
22109 cp_lexer_consume_token (parser->lexer);
22110 /* Parse the output-operands. */
22111 if (cp_lexer_next_token_is_not (parser->lexer,
22112 CPP_COLON)
22113 && cp_lexer_next_token_is_not (parser->lexer,
22114 CPP_SCOPE)
22115 && cp_lexer_next_token_is_not (parser->lexer,
22116 CPP_CLOSE_PAREN))
22118 outputs = cp_parser_asm_operand_list (parser);
22119 if (outputs == error_mark_node)
22120 invalid_outputs_p = true;
22123 /* If the next token is `::', there are no outputs, and the
22124 next token is the beginning of the inputs. */
22125 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22126 /* The inputs are coming next. */
22127 inputs_p = true;
22129 /* Look for inputs. */
22130 if (inputs_p
22131 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22133 /* Consume the `:' or `::'. */
22134 cp_lexer_consume_token (parser->lexer);
22135 /* Parse the output-operands. */
22136 if (cp_lexer_next_token_is_not (parser->lexer,
22137 CPP_COLON)
22138 && cp_lexer_next_token_is_not (parser->lexer,
22139 CPP_SCOPE)
22140 && cp_lexer_next_token_is_not (parser->lexer,
22141 CPP_CLOSE_PAREN))
22143 inputs = cp_parser_asm_operand_list (parser);
22144 if (inputs == error_mark_node)
22145 invalid_inputs_p = true;
22148 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22149 /* The clobbers are coming next. */
22150 clobbers_p = true;
22152 /* Look for clobbers. */
22153 if (clobbers_p
22154 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22156 clobbers_p = true;
22157 /* Consume the `:' or `::'. */
22158 cp_lexer_consume_token (parser->lexer);
22159 /* Parse the clobbers. */
22160 if (cp_lexer_next_token_is_not (parser->lexer,
22161 CPP_COLON)
22162 && cp_lexer_next_token_is_not (parser->lexer,
22163 CPP_CLOSE_PAREN))
22164 clobbers = cp_parser_asm_clobber_list (parser);
22166 else if (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22167 /* The labels are coming next. */
22168 labels_p = true;
22170 /* Look for labels. */
22171 if (labels_p
22172 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
22174 labels_p = true;
22175 /* Consume the `:' or `::'. */
22176 cp_lexer_consume_token (parser->lexer);
22177 /* Parse the labels. */
22178 labels = cp_parser_asm_label_list (parser);
22181 if (goto_p && !labels_p)
22182 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
22184 else if (goto_p)
22185 missing = RT_COLON_SCOPE;
22187 /* Look for the closing `)'. */
22188 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
22189 missing ? missing : RT_CLOSE_PAREN))
22190 cp_parser_skip_to_closing_parenthesis (parser, true, false,
22191 /*consume_paren=*/true);
22192 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22194 if (!invalid_inputs_p && !invalid_outputs_p)
22196 /* Create the ASM_EXPR. */
22197 if (parser->in_function_body)
22199 asm_stmt = finish_asm_stmt (asm_loc, volatile_p, string, outputs,
22200 inputs, clobbers, labels, inline_p);
22201 /* If the extended syntax was not used, mark the ASM_EXPR. */
22202 if (!extended_p)
22204 tree temp = asm_stmt;
22205 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
22206 temp = TREE_OPERAND (temp, 0);
22208 ASM_INPUT_P (temp) = 1;
22211 else
22212 symtab->finalize_toplevel_asm (string);
22216 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
22217 type that comes from the decl-specifier-seq. */
22219 static tree
22220 strip_declarator_types (tree type, cp_declarator *declarator)
22222 for (cp_declarator *d = declarator; d;)
22223 switch (d->kind)
22225 case cdk_id:
22226 case cdk_decomp:
22227 case cdk_error:
22228 d = NULL;
22229 break;
22231 default:
22232 if (TYPE_PTRMEMFUNC_P (type))
22233 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
22234 type = TREE_TYPE (type);
22235 d = d->declarator;
22236 break;
22239 return type;
22242 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
22243 a construct looks like a variable definition but is actually a function
22244 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
22245 is the declarator for this function declaration. */
22247 static void
22248 warn_about_ambiguous_parse (const cp_decl_specifier_seq *decl_specifiers,
22249 const cp_declarator *declarator)
22251 /* Only warn if we are declaring a function at block scope. */
22252 if (!at_function_scope_p ())
22253 return;
22255 /* And only if there is no storage class specified. */
22256 if (decl_specifiers->storage_class != sc_none
22257 || decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22258 return;
22260 if (declarator->kind != cdk_function
22261 || !declarator->declarator
22262 || declarator->declarator->kind != cdk_id
22263 || !identifier_p (get_unqualified_id
22264 (const_cast<cp_declarator *>(declarator))))
22265 return;
22267 /* Don't warn when the whole declarator (not just the declarator-id!)
22268 was parenthesized. That is, don't warn for int(n()) but do warn
22269 for int(f)(). */
22270 if (declarator->parenthesized != UNKNOWN_LOCATION)
22271 return;
22273 tree type;
22274 if (decl_specifiers->type)
22276 type = decl_specifiers->type;
22277 if (TREE_CODE (type) == TYPE_DECL)
22278 type = TREE_TYPE (type);
22280 /* If the return type is void there is no ambiguity. */
22281 if (same_type_p (type, void_type_node))
22282 return;
22284 else if (decl_specifiers->any_type_specifiers_p)
22285 /* Code like long f(); will have null ->type. If we have any
22286 type-specifiers, pretend we've seen int. */
22287 type = integer_type_node;
22288 else
22289 return;
22291 auto_diagnostic_group d;
22292 location_t loc = declarator->u.function.parens_loc;
22293 tree params = declarator->u.function.parameters;
22294 const bool has_list_ctor_p = CLASS_TYPE_P (type) && TYPE_HAS_LIST_CTOR (type);
22296 /* The T t() case. */
22297 if (params == void_list_node)
22299 if (warning_at (loc, OPT_Wvexing_parse,
22300 "empty parentheses were disambiguated as a function "
22301 "declaration"))
22303 /* () means value-initialization (C++03 and up); {} (C++11 and up)
22304 means value-initialization or aggregate-initialization, nothing
22305 means default-initialization. We can only suggest removing the
22306 parentheses/adding {} if T has a default constructor. */
22307 if (!CLASS_TYPE_P (type) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
22309 gcc_rich_location iloc (loc);
22310 iloc.add_fixit_remove ();
22311 inform (&iloc, "remove parentheses to default-initialize "
22312 "a variable");
22313 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22315 if (CP_AGGREGATE_TYPE_P (type))
22316 inform (loc, "or replace parentheses with braces to "
22317 "aggregate-initialize a variable");
22318 else
22319 inform (loc, "or replace parentheses with braces to "
22320 "value-initialize a variable");
22324 return;
22327 /* If we had (...) or the parameter-list wasn't parenthesized,
22328 we're done. */
22329 if (params == NULL_TREE || !PARENTHESIZED_LIST_P (params))
22330 return;
22332 /* The T t(X()) case. */
22333 if (list_length (params) == 2)
22335 if (warning_at (loc, OPT_Wvexing_parse,
22336 "parentheses were disambiguated as a function "
22337 "declaration"))
22339 gcc_rich_location iloc (loc);
22340 /* {}-initialization means that we can use an initializer-list
22341 constructor if no default constructor is available, so don't
22342 suggest using {} for classes that have an initializer_list
22343 constructor. */
22344 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22346 iloc.add_fixit_replace (get_start (loc), "{");
22347 iloc.add_fixit_replace (get_finish (loc), "}");
22348 inform (&iloc, "replace parentheses with braces to declare a "
22349 "variable");
22351 else
22353 iloc.add_fixit_insert_after (get_start (loc), "(");
22354 iloc.add_fixit_insert_before (get_finish (loc), ")");
22355 inform (&iloc, "add parentheses to declare a variable");
22359 /* The T t(X(), X()) case. */
22360 else if (warning_at (loc, OPT_Wvexing_parse,
22361 "parentheses were disambiguated as a function "
22362 "declaration"))
22364 gcc_rich_location iloc (loc);
22365 if (cxx_dialect >= cxx11 && !has_list_ctor_p)
22367 iloc.add_fixit_replace (get_start (loc), "{");
22368 iloc.add_fixit_replace (get_finish (loc), "}");
22369 inform (&iloc, "replace parentheses with braces to declare a "
22370 "variable");
22375 /* If DECLARATOR with DECL_SPECS is a function declarator that has
22376 the form of a deduction guide, tag it as such. CTOR_DTOR_OR_CONV_P
22377 has the same meaning as in cp_parser_declarator. */
22379 static void
22380 cp_parser_maybe_adjust_declarator_for_dguide (cp_parser *parser,
22381 cp_decl_specifier_seq *decl_specs,
22382 cp_declarator *declarator,
22383 int *ctor_dtor_or_conv_p)
22385 if (cxx_dialect >= cxx17
22386 && *ctor_dtor_or_conv_p <= 0
22387 && !decl_specs->type
22388 && !decl_specs->any_type_specifiers_p
22389 && function_declarator_p (declarator))
22391 cp_declarator *id = get_id_declarator (declarator);
22392 tree name = id->u.id.unqualified_name;
22393 parser->scope = id->u.id.qualifying_scope;
22394 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
22395 if (tmpl
22396 && (DECL_CLASS_TEMPLATE_P (tmpl)
22397 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
22399 id->u.id.unqualified_name = dguide_name (tmpl);
22400 id->u.id.sfk = sfk_deduction_guide;
22401 *ctor_dtor_or_conv_p = 1;
22406 /* Declarators [gram.dcl.decl] */
22408 /* Parse an init-declarator.
22410 init-declarator:
22411 declarator initializer [opt]
22413 GNU Extension:
22415 init-declarator:
22416 declarator asm-specification [opt] attributes [opt] initializer [opt]
22418 function-definition:
22419 decl-specifier-seq [opt] declarator ctor-initializer [opt]
22420 function-body
22421 decl-specifier-seq [opt] declarator function-try-block
22423 GNU Extension:
22425 function-definition:
22426 __extension__ function-definition
22428 TM Extension:
22430 function-definition:
22431 decl-specifier-seq [opt] declarator function-transaction-block
22433 The parser flags FLAGS is used to control type-specifier parsing.
22435 The DECL_SPECIFIERS apply to this declarator. Returns a
22436 representation of the entity declared. If MEMBER_P is TRUE, then
22437 this declarator appears in a class scope. The new DECL created by
22438 this declarator is returned.
22440 The CHECKS are access checks that should be performed once we know
22441 what entity is being declared (and, therefore, what classes have
22442 befriended it).
22444 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
22445 for a function-definition here as well. If the declarator is a
22446 declarator for a function-definition, *FUNCTION_DEFINITION_P will
22447 be TRUE upon return. By that point, the function-definition will
22448 have been completely parsed.
22450 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
22451 is FALSE.
22453 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
22454 parsed declaration if it is an uninitialized single declarator not followed
22455 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
22456 if present, will not be consumed. If returned, this declarator will be
22457 created with SD_INITIALIZED but will not call cp_finish_decl.
22459 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
22460 and there is an initializer, the pointed location_t is set to the
22461 location of the '=' or `(', or '{' in C++11 token introducing the
22462 initializer. */
22464 static tree
22465 cp_parser_init_declarator (cp_parser* parser,
22466 cp_parser_flags flags,
22467 cp_decl_specifier_seq *decl_specifiers,
22468 vec<deferred_access_check, va_gc> *checks,
22469 bool function_definition_allowed_p,
22470 bool member_p,
22471 int declares_class_or_enum,
22472 bool* function_definition_p,
22473 tree* maybe_range_for_decl,
22474 location_t* init_loc,
22475 tree* auto_result)
22477 cp_token *token = NULL, *asm_spec_start_token = NULL,
22478 *attributes_start_token = NULL;
22479 cp_declarator *declarator;
22480 tree prefix_attributes;
22481 tree attributes = NULL;
22482 tree asm_specification;
22483 tree initializer;
22484 tree decl = NULL_TREE;
22485 tree scope;
22486 int is_initialized;
22487 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
22488 initialized with "= ..", CPP_OPEN_PAREN if initialized with
22489 "(...)". */
22490 enum cpp_ttype initialization_kind;
22491 bool is_direct_init = false;
22492 bool is_non_constant_init;
22493 int ctor_dtor_or_conv_p;
22494 bool friend_p = cp_parser_friend_p (decl_specifiers);
22495 bool static_p = decl_specifiers->storage_class == sc_static;
22496 tree pushed_scope = NULL_TREE;
22497 bool range_for_decl_p = false;
22498 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
22499 location_t tmp_init_loc = UNKNOWN_LOCATION;
22501 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_consteval))
22502 flags |= CP_PARSER_FLAGS_CONSTEVAL;
22504 /* Assume that this is not the declarator for a function
22505 definition. */
22506 if (function_definition_p)
22507 *function_definition_p = false;
22509 /* Default arguments are only permitted for function parameters. */
22510 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
22511 parser->default_arg_ok_p = false;
22513 /* Defer access checks while parsing the declarator; we cannot know
22514 what names are accessible until we know what is being
22515 declared. */
22516 resume_deferring_access_checks ();
22518 token = cp_lexer_peek_token (parser->lexer);
22520 /* Parse the declarator. */
22521 declarator
22522 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22523 flags, &ctor_dtor_or_conv_p,
22524 /*parenthesized_p=*/NULL,
22525 member_p, friend_p, static_p);
22526 /* Gather up the deferred checks. */
22527 stop_deferring_access_checks ();
22529 parser->default_arg_ok_p = saved_default_arg_ok_p;
22531 /* If the DECLARATOR was erroneous, there's no need to go
22532 further. */
22533 if (declarator == cp_error_declarator)
22534 return error_mark_node;
22536 /* Check that the number of template-parameter-lists is OK. */
22537 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
22538 token->location))
22539 return error_mark_node;
22541 if (declares_class_or_enum & 2)
22542 cp_parser_check_for_definition_in_return_type (declarator,
22543 decl_specifiers->type,
22544 decl_specifiers->locations[ds_type_spec]);
22546 /* Figure out what scope the entity declared by the DECLARATOR is
22547 located in. `grokdeclarator' sometimes changes the scope, so
22548 we compute it now. */
22549 scope = get_scope_of_declarator (declarator);
22551 /* Perform any lookups in the declared type which were thought to be
22552 dependent, but are not in the scope of the declarator. */
22553 decl_specifiers->type
22554 = maybe_update_decl_type (decl_specifiers->type, scope);
22556 /* If we're allowing GNU extensions, look for an
22557 asm-specification. */
22558 if (cp_parser_allow_gnu_extensions_p (parser))
22560 /* Look for an asm-specification. */
22561 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
22562 asm_specification = cp_parser_asm_specification_opt (parser);
22564 else
22565 asm_specification = NULL_TREE;
22567 /* Gather the attributes that were provided with the
22568 decl-specifiers. */
22569 prefix_attributes = decl_specifiers->attributes;
22571 /* Look for attributes. */
22572 attributes_start_token = cp_lexer_peek_token (parser->lexer);
22573 attributes = cp_parser_attributes_opt (parser);
22575 /* Peek at the next token. */
22576 token = cp_lexer_peek_token (parser->lexer);
22578 bool bogus_implicit_tmpl = false;
22580 if (function_declarator_p (declarator))
22582 /* Handle C++17 deduction guides. Note that class-scope
22583 non-template deduction guides are instead handled in
22584 cp_parser_member_declaration. */
22585 cp_parser_maybe_adjust_declarator_for_dguide (parser,
22586 decl_specifiers,
22587 declarator,
22588 &ctor_dtor_or_conv_p);
22590 if (!member_p && !cp_parser_error_occurred (parser))
22591 warn_about_ambiguous_parse (decl_specifiers, declarator);
22593 /* Check to see if the token indicates the start of a
22594 function-definition. */
22595 if (cp_parser_token_starts_function_definition_p (token))
22597 if (!function_definition_allowed_p)
22599 /* If a function-definition should not appear here, issue an
22600 error message. */
22601 cp_parser_error (parser,
22602 "a function-definition is not allowed here");
22603 return error_mark_node;
22606 location_t func_brace_location
22607 = cp_lexer_peek_token (parser->lexer)->location;
22609 /* Neither attributes nor an asm-specification are allowed
22610 on a function-definition. */
22611 if (asm_specification)
22612 error_at (asm_spec_start_token->location,
22613 "an %<asm%> specification is not allowed "
22614 "on a function-definition");
22615 if (attributes)
22616 error_at (attributes_start_token->location,
22617 "attributes are not allowed "
22618 "on a function-definition");
22619 /* This is a function-definition. */
22620 *function_definition_p = true;
22622 /* Parse the function definition. */
22623 if (member_p)
22624 decl = cp_parser_save_member_function_body (parser,
22625 decl_specifiers,
22626 declarator,
22627 prefix_attributes);
22628 else
22629 decl =
22630 (cp_parser_function_definition_from_specifiers_and_declarator
22631 (parser, decl_specifiers, prefix_attributes, declarator));
22633 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
22635 /* This is where the prologue starts... */
22636 DECL_STRUCT_FUNCTION (decl)->function_start_locus
22637 = func_brace_location;
22640 return decl;
22643 else if (parser->fully_implicit_function_template_p)
22645 /* A non-template declaration involving a function parameter list
22646 containing an implicit template parameter will be made into a
22647 template. If the resulting declaration is not going to be an
22648 actual function then finish the template scope here to prevent it.
22649 An error message will be issued once we have a decl to talk about.
22651 FIXME probably we should do type deduction rather than create an
22652 implicit template, but the standard currently doesn't allow it. */
22653 bogus_implicit_tmpl = true;
22654 finish_fully_implicit_template (parser, NULL_TREE);
22657 /* [dcl.dcl]
22659 Only in function declarations for constructors, destructors, type
22660 conversions, and deduction guides can the decl-specifier-seq be omitted.
22662 We explicitly postpone this check past the point where we handle
22663 function-definitions because we tolerate function-definitions
22664 that are missing their return types in some modes. */
22665 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
22667 cp_parser_error (parser,
22668 "expected constructor, destructor, or type conversion");
22669 return error_mark_node;
22672 /* An `=' or an '{' in C++11, indicate an initializer. An '(' may indicate
22673 an initializer as well. */
22674 if (token->type == CPP_EQ
22675 || token->type == CPP_OPEN_PAREN
22676 || token->type == CPP_OPEN_BRACE)
22678 /* Don't get fooled into thinking that F(i)(1)(2) is an initializer.
22679 It isn't; it's an expression. (Here '(i)' would have already been
22680 parsed as a declarator.) */
22681 if (token->type == CPP_OPEN_PAREN
22682 && cp_parser_uncommitted_to_tentative_parse_p (parser))
22684 cp_lexer_save_tokens (parser->lexer);
22685 cp_lexer_consume_token (parser->lexer);
22686 cp_parser_skip_to_closing_parenthesis (parser,
22687 /*recovering*/false,
22688 /*or_comma*/false,
22689 /*consume_paren*/true);
22690 /* If this is an initializer, only a ',' or ';' can follow: either
22691 we have another init-declarator, or we're at the end of an
22692 init-declarator-list which can only be followed by a ';'. */
22693 bool ok = (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22694 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22695 cp_lexer_rollback_tokens (parser->lexer);
22696 if (UNLIKELY (!ok))
22697 /* Not an init-declarator. */
22698 return error_mark_node;
22700 is_initialized = SD_INITIALIZED;
22701 initialization_kind = token->type;
22702 declarator->init_loc = token->location;
22703 if (maybe_range_for_decl)
22704 *maybe_range_for_decl = error_mark_node;
22705 tmp_init_loc = token->location;
22706 if (init_loc && *init_loc == UNKNOWN_LOCATION)
22707 *init_loc = tmp_init_loc;
22709 if (token->type == CPP_EQ
22710 && function_declarator_p (declarator))
22712 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
22713 if (t2->keyword == RID_DEFAULT)
22714 is_initialized = SD_DEFAULTED;
22715 else if (t2->keyword == RID_DELETE)
22716 is_initialized = SD_DELETED;
22719 else
22721 /* If the init-declarator isn't initialized and isn't followed by a
22722 `,' or `;', it's not a valid init-declarator. */
22723 if (token->type != CPP_COMMA
22724 && token->type != CPP_SEMICOLON)
22726 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
22727 range_for_decl_p = true;
22728 else
22730 if (!maybe_range_for_decl)
22731 cp_parser_error (parser, "expected initializer");
22732 return error_mark_node;
22735 is_initialized = SD_UNINITIALIZED;
22736 initialization_kind = CPP_EOF;
22739 /* Because start_decl has side-effects, we should only call it if we
22740 know we're going ahead. By this point, we know that we cannot
22741 possibly be looking at any other construct. */
22742 cp_parser_commit_to_tentative_parse (parser);
22744 /* Enter the newly declared entry in the symbol table. If we're
22745 processing a declaration in a class-specifier, we wait until
22746 after processing the initializer. */
22747 if (!member_p)
22749 if (parser->in_unbraced_linkage_specification_p)
22750 decl_specifiers->storage_class = sc_extern;
22751 decl = start_decl (declarator, decl_specifiers,
22752 range_for_decl_p? SD_INITIALIZED : is_initialized,
22753 attributes, prefix_attributes, &pushed_scope);
22754 cp_finalize_omp_declare_simd (parser, decl);
22755 cp_finalize_oacc_routine (parser, decl, false);
22756 /* Adjust location of decl if declarator->id_loc is more appropriate:
22757 set, and decl wasn't merged with another decl, in which case its
22758 location would be different from input_location, and more accurate. */
22759 if (DECL_P (decl)
22760 && declarator->id_loc != UNKNOWN_LOCATION
22761 && DECL_SOURCE_LOCATION (decl) == input_location)
22762 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
22764 else if (scope)
22765 /* Enter the SCOPE. That way unqualified names appearing in the
22766 initializer will be looked up in SCOPE. */
22767 pushed_scope = push_scope (scope);
22769 /* Perform deferred access control checks, now that we know in which
22770 SCOPE the declared entity resides. */
22771 if (!member_p && decl)
22773 tree saved_current_function_decl = NULL_TREE;
22775 /* If the entity being declared is a function, pretend that we
22776 are in its scope. If it is a `friend', it may have access to
22777 things that would not otherwise be accessible. */
22778 if (TREE_CODE (decl) == FUNCTION_DECL)
22780 saved_current_function_decl = current_function_decl;
22781 current_function_decl = decl;
22784 /* Perform access checks for template parameters. */
22785 cp_parser_perform_template_parameter_access_checks (checks);
22787 /* Perform the access control checks for the declarator and the
22788 decl-specifiers. */
22789 perform_deferred_access_checks (tf_warning_or_error);
22791 /* Restore the saved value. */
22792 if (TREE_CODE (decl) == FUNCTION_DECL)
22793 current_function_decl = saved_current_function_decl;
22796 /* Parse the initializer. */
22797 initializer = NULL_TREE;
22798 is_direct_init = false;
22799 is_non_constant_init = true;
22800 if (is_initialized)
22802 if (function_declarator_p (declarator))
22804 if (initialization_kind == CPP_EQ)
22805 initializer = cp_parser_pure_specifier (parser);
22806 else
22808 /* If the declaration was erroneous, we don't really
22809 know what the user intended, so just silently
22810 consume the initializer. */
22811 if (decl != error_mark_node)
22812 error_at (tmp_init_loc, "initializer provided for function");
22813 cp_parser_skip_to_closing_parenthesis (parser,
22814 /*recovering=*/true,
22815 /*or_comma=*/false,
22816 /*consume_paren=*/true);
22819 else
22821 /* We want to record the extra mangling scope for in-class
22822 initializers of class members and initializers of static
22823 data member templates and namespace-scope initializers.
22824 The former involves deferring parsing of the initializer
22825 until end of class as with default arguments. So right
22826 here we only handle the latter two. */
22827 bool has_lambda_scope = false;
22829 if (decl != error_mark_node
22830 && !member_p
22831 && (processing_template_decl || DECL_NAMESPACE_SCOPE_P (decl)))
22832 has_lambda_scope = true;
22834 if (has_lambda_scope)
22835 start_lambda_scope (decl);
22836 initializer = cp_parser_initializer (parser,
22837 &is_direct_init,
22838 &is_non_constant_init);
22839 if (has_lambda_scope)
22840 finish_lambda_scope ();
22841 if (initializer == error_mark_node)
22842 cp_parser_skip_to_end_of_statement (parser);
22846 /* The old parser allows attributes to appear after a parenthesized
22847 initializer. Mark Mitchell proposed removing this functionality
22848 on the GCC mailing lists on 2002-08-13. This parser accepts the
22849 attributes -- but ignores them. Made a permerror in GCC 8. */
22850 if (cp_parser_allow_gnu_extensions_p (parser)
22851 && initialization_kind == CPP_OPEN_PAREN
22852 && cp_parser_attributes_opt (parser)
22853 && permerror (input_location,
22854 "attributes after parenthesized initializer ignored"))
22856 static bool hint;
22857 if (flag_permissive && !hint)
22859 hint = true;
22860 inform (input_location,
22861 "this flexibility is deprecated and will be removed");
22865 /* And now complain about a non-function implicit template. */
22866 if (bogus_implicit_tmpl && decl != error_mark_node)
22867 error_at (DECL_SOURCE_LOCATION (decl),
22868 "non-function %qD declared as implicit template", decl);
22870 /* For an in-class declaration, use `grokfield' to create the
22871 declaration. */
22872 if (member_p)
22874 if (pushed_scope)
22876 pop_scope (pushed_scope);
22877 pushed_scope = NULL_TREE;
22879 decl = grokfield (declarator, decl_specifiers,
22880 initializer, !is_non_constant_init,
22881 /*asmspec=*/NULL_TREE,
22882 attr_chainon (attributes, prefix_attributes));
22883 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
22884 cp_parser_save_default_args (parser, decl);
22885 cp_finalize_omp_declare_simd (parser, decl);
22886 cp_finalize_oacc_routine (parser, decl, false);
22889 /* Finish processing the declaration. But, skip member
22890 declarations. */
22891 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
22893 cp_finish_decl (decl,
22894 initializer, !is_non_constant_init,
22895 asm_specification,
22896 /* If the initializer is in parentheses, then this is
22897 a direct-initialization, which means that an
22898 `explicit' constructor is OK. Otherwise, an
22899 `explicit' constructor cannot be used. */
22900 ((is_direct_init || !is_initialized)
22901 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
22903 else if ((cxx_dialect != cxx98) && friend_p
22904 && decl && TREE_CODE (decl) == FUNCTION_DECL)
22905 /* Core issue #226 (C++0x only): A default template-argument
22906 shall not be specified in a friend class template
22907 declaration. */
22908 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
22909 /*is_partial=*/false, /*is_friend_decl=*/1);
22911 if (!friend_p && pushed_scope)
22912 pop_scope (pushed_scope);
22914 if (function_declarator_p (declarator)
22915 && parser->fully_implicit_function_template_p)
22917 if (member_p)
22918 decl = finish_fully_implicit_template (parser, decl);
22919 else
22920 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
22923 if (auto_result && is_initialized && decl_specifiers->type
22924 && type_uses_auto (decl_specifiers->type))
22925 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
22927 return decl;
22930 /* Parse a declarator.
22932 declarator:
22933 direct-declarator
22934 ptr-operator declarator
22936 abstract-declarator:
22937 ptr-operator abstract-declarator [opt]
22938 direct-abstract-declarator
22940 GNU Extensions:
22942 declarator:
22943 attributes [opt] direct-declarator
22944 attributes [opt] ptr-operator declarator
22946 abstract-declarator:
22947 attributes [opt] ptr-operator abstract-declarator [opt]
22948 attributes [opt] direct-abstract-declarator
22950 The parser flags FLAGS is used to control type-specifier parsing.
22952 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
22953 detect constructors, destructors, deduction guides, or conversion operators.
22954 It is set to -1 if the declarator is a name, and +1 if it is a
22955 function. Otherwise it is set to zero. Usually you just want to
22956 test for >0, but internally the negative value is used.
22958 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
22959 a decl-specifier-seq unless it declares a constructor, destructor,
22960 or conversion. It might seem that we could check this condition in
22961 semantic analysis, rather than parsing, but that makes it difficult
22962 to handle something like `f()'. We want to notice that there are
22963 no decl-specifiers, and therefore realize that this is an
22964 expression, not a declaration.)
22966 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
22967 the declarator is a direct-declarator of the form "(...)".
22969 MEMBER_P is true iff this declarator is a member-declarator.
22971 FRIEND_P is true iff this declarator is a friend.
22973 STATIC_P is true iff the keyword static was seen. */
22975 static cp_declarator *
22976 cp_parser_declarator (cp_parser* parser,
22977 cp_parser_declarator_kind dcl_kind,
22978 cp_parser_flags flags,
22979 int* ctor_dtor_or_conv_p,
22980 bool* parenthesized_p,
22981 bool member_p, bool friend_p, bool static_p)
22983 cp_declarator *declarator;
22984 enum tree_code code;
22985 cp_cv_quals cv_quals;
22986 tree class_type;
22987 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
22989 /* Assume this is not a constructor, destructor, or type-conversion
22990 operator. */
22991 if (ctor_dtor_or_conv_p)
22992 *ctor_dtor_or_conv_p = 0;
22994 if (cp_parser_allow_gnu_extensions_p (parser))
22995 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
22997 /* Check for the ptr-operator production. */
22998 cp_parser_parse_tentatively (parser);
22999 /* Parse the ptr-operator. */
23000 code = cp_parser_ptr_operator (parser,
23001 &class_type,
23002 &cv_quals,
23003 &std_attributes);
23005 /* If that worked, then we have a ptr-operator. */
23006 if (cp_parser_parse_definitely (parser))
23008 /* If a ptr-operator was found, then this declarator was not
23009 parenthesized. */
23010 if (parenthesized_p)
23011 *parenthesized_p = false;
23012 /* The dependent declarator is optional if we are parsing an
23013 abstract-declarator. */
23014 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23015 cp_parser_parse_tentatively (parser);
23017 /* Parse the dependent declarator. */
23018 declarator = cp_parser_declarator (parser, dcl_kind, flags,
23019 /*ctor_dtor_or_conv_p=*/NULL,
23020 /*parenthesized_p=*/NULL,
23021 member_p, friend_p, static_p);
23023 /* If we are parsing an abstract-declarator, we must handle the
23024 case where the dependent declarator is absent. */
23025 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
23026 && !cp_parser_parse_definitely (parser))
23027 declarator = NULL;
23029 declarator = cp_parser_make_indirect_declarator
23030 (code, class_type, cv_quals, declarator, std_attributes);
23032 /* Everything else is a direct-declarator. */
23033 else
23035 if (parenthesized_p)
23036 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
23037 CPP_OPEN_PAREN);
23038 declarator = cp_parser_direct_declarator (parser, dcl_kind,
23039 flags, ctor_dtor_or_conv_p,
23040 member_p, friend_p, static_p);
23043 if (gnu_attributes && declarator && declarator != cp_error_declarator)
23044 declarator->attributes = gnu_attributes;
23045 return declarator;
23048 /* Parse a direct-declarator or direct-abstract-declarator.
23050 direct-declarator:
23051 declarator-id
23052 direct-declarator ( parameter-declaration-clause )
23053 cv-qualifier-seq [opt]
23054 ref-qualifier [opt]
23055 exception-specification [opt]
23056 direct-declarator [ constant-expression [opt] ]
23057 ( declarator )
23059 direct-abstract-declarator:
23060 direct-abstract-declarator [opt]
23061 ( parameter-declaration-clause )
23062 cv-qualifier-seq [opt]
23063 ref-qualifier [opt]
23064 exception-specification [opt]
23065 direct-abstract-declarator [opt] [ constant-expression [opt] ]
23066 ( abstract-declarator )
23068 Returns a representation of the declarator. DCL_KIND is
23069 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
23070 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
23071 we are parsing a direct-declarator. It is
23072 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
23073 of ambiguity we prefer an abstract declarator, as per
23074 [dcl.ambig.res].
23075 The parser flags FLAGS is used to control type-specifier parsing.
23076 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
23077 as for cp_parser_declarator. */
23079 static cp_declarator *
23080 cp_parser_direct_declarator (cp_parser* parser,
23081 cp_parser_declarator_kind dcl_kind,
23082 cp_parser_flags flags,
23083 int* ctor_dtor_or_conv_p,
23084 bool member_p, bool friend_p, bool static_p)
23086 cp_token *token;
23087 cp_declarator *declarator = NULL;
23088 tree scope = NULL_TREE;
23089 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
23090 bool saved_in_declarator_p = parser->in_declarator_p;
23091 bool first = true;
23092 tree pushed_scope = NULL_TREE;
23093 cp_token *open_paren = NULL, *close_paren = NULL;
23095 while (true)
23097 /* Peek at the next token. */
23098 token = cp_lexer_peek_token (parser->lexer);
23099 if (token->type == CPP_OPEN_PAREN)
23101 /* This is either a parameter-declaration-clause, or a
23102 parenthesized declarator. When we know we are parsing a
23103 named declarator, it must be a parenthesized declarator
23104 if FIRST is true. For instance, `(int)' is a
23105 parameter-declaration-clause, with an omitted
23106 direct-abstract-declarator. But `((*))', is a
23107 parenthesized abstract declarator. Finally, when T is a
23108 template parameter `(T)' is a
23109 parameter-declaration-clause, and not a parenthesized
23110 named declarator.
23112 We first try and parse a parameter-declaration-clause,
23113 and then try a nested declarator (if FIRST is true).
23115 It is not an error for it not to be a
23116 parameter-declaration-clause, even when FIRST is
23117 false. Consider,
23119 int i (int);
23120 int i (3);
23122 The first is the declaration of a function while the
23123 second is the definition of a variable, including its
23124 initializer.
23126 Having seen only the parenthesis, we cannot know which of
23127 these two alternatives should be selected. Even more
23128 complex are examples like:
23130 int i (int (a));
23131 int i (int (3));
23133 The former is a function-declaration; the latter is a
23134 variable initialization.
23136 Thus again, we try a parameter-declaration-clause, and if
23137 that fails, we back out and return. */
23139 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23141 tree params;
23142 bool is_declarator = false;
23144 open_paren = NULL;
23146 /* In a member-declarator, the only valid interpretation
23147 of a parenthesis is the start of a
23148 parameter-declaration-clause. (It is invalid to
23149 initialize a static data member with a parenthesized
23150 initializer; only the "=" form of initialization is
23151 permitted.) */
23152 if (!member_p)
23153 cp_parser_parse_tentatively (parser);
23155 /* Consume the `('. */
23156 const location_t parens_start = token->location;
23157 matching_parens parens;
23158 parens.consume_open (parser);
23159 if (first)
23161 /* If this is going to be an abstract declarator, we're
23162 in a declarator and we can't have default args. */
23163 parser->default_arg_ok_p = false;
23164 parser->in_declarator_p = true;
23167 begin_scope (sk_function_parms, NULL_TREE);
23169 /* Parse the parameter-declaration-clause. */
23170 params
23171 = cp_parser_parameter_declaration_clause (parser, flags);
23172 const location_t parens_end
23173 = cp_lexer_peek_token (parser->lexer)->location;
23175 /* Consume the `)'. */
23176 parens.require_close (parser);
23178 /* If all went well, parse the cv-qualifier-seq,
23179 ref-qualifier and the exception-specification. */
23180 if (member_p || cp_parser_parse_definitely (parser))
23182 cp_cv_quals cv_quals;
23183 cp_virt_specifiers virt_specifiers;
23184 cp_ref_qualifier ref_qual;
23185 tree exception_specification;
23186 tree late_return;
23187 tree attrs;
23188 bool memfn = (member_p || (pushed_scope
23189 && CLASS_TYPE_P (pushed_scope)));
23190 unsigned char local_variables_forbidden_p
23191 = parser->local_variables_forbidden_p;
23192 /* 'this' is not allowed in static member functions. */
23193 if (static_p || friend_p)
23194 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
23196 is_declarator = true;
23198 if (ctor_dtor_or_conv_p)
23199 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
23200 first = false;
23202 /* Parse the cv-qualifier-seq. */
23203 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23204 /* Parse the ref-qualifier. */
23205 ref_qual = cp_parser_ref_qualifier_opt (parser);
23206 /* Parse the tx-qualifier. */
23207 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
23209 tree save_ccp = current_class_ptr;
23210 tree save_ccr = current_class_ref;
23211 if (memfn && !friend_p && !static_p)
23212 /* DR 1207: 'this' is in scope after the cv-quals. */
23213 inject_this_parameter (current_class_type, cv_quals);
23215 /* If it turned out that this is e.g. a pointer to a
23216 function, we don't want to delay noexcept parsing. */
23217 if (declarator == NULL || declarator->kind != cdk_id)
23218 flags &= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT;
23220 /* Parse the exception-specification. */
23221 exception_specification
23222 = cp_parser_exception_specification_opt (parser,
23223 flags);
23225 attrs = cp_parser_std_attribute_spec_seq (parser);
23227 cp_omp_declare_simd_data odsd;
23228 if ((flag_openmp || flag_openmp_simd)
23229 && declarator
23230 && declarator->std_attributes
23231 && declarator->kind == cdk_id)
23233 tree *pa = &declarator->std_attributes;
23234 cp_parser_handle_directive_omp_attributes (parser, pa,
23235 &odsd, false);
23238 /* In here, we handle cases where attribute is used after
23239 the function declaration. For example:
23240 void func (int x) __attribute__((vector(..))); */
23241 tree gnu_attrs = NULL_TREE;
23242 tree requires_clause = NULL_TREE;
23243 late_return
23244 = cp_parser_late_return_type_opt (parser, declarator,
23245 requires_clause);
23247 cp_finalize_omp_declare_simd (parser, &odsd);
23249 /* Parse the virt-specifier-seq. */
23250 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
23252 location_t parens_loc = make_location (parens_start,
23253 parens_start,
23254 parens_end);
23255 /* Create the function-declarator. */
23256 declarator = make_call_declarator (declarator,
23257 params,
23258 cv_quals,
23259 virt_specifiers,
23260 ref_qual,
23261 tx_qual,
23262 exception_specification,
23263 late_return,
23264 requires_clause,
23265 parens_loc);
23266 declarator->std_attributes = attrs;
23267 declarator->attributes = gnu_attrs;
23268 /* Any subsequent parameter lists are to do with
23269 return type, so are not those of the declared
23270 function. */
23271 parser->default_arg_ok_p = false;
23273 current_class_ptr = save_ccp;
23274 current_class_ref = save_ccr;
23276 /* Restore the state of local_variables_forbidden_p. */
23277 parser->local_variables_forbidden_p
23278 = local_variables_forbidden_p;
23281 /* Remove the function parms from scope. */
23282 pop_bindings_and_leave_scope ();
23284 if (is_declarator)
23285 /* Repeat the main loop. */
23286 continue;
23289 /* If this is the first, we can try a parenthesized
23290 declarator. */
23291 if (first)
23293 bool saved_in_type_id_in_expr_p;
23295 parser->default_arg_ok_p = saved_default_arg_ok_p;
23296 parser->in_declarator_p = saved_in_declarator_p;
23298 open_paren = token;
23299 /* Consume the `('. */
23300 matching_parens parens;
23301 parens.consume_open (parser);
23302 /* Parse the nested declarator. */
23303 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
23304 parser->in_type_id_in_expr_p = true;
23305 declarator
23306 = cp_parser_declarator (parser, dcl_kind, flags,
23307 ctor_dtor_or_conv_p,
23308 /*parenthesized_p=*/NULL,
23309 member_p, friend_p,
23310 /*static_p=*/false);
23311 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
23312 first = false;
23313 /* Expect a `)'. */
23314 close_paren = cp_lexer_peek_token (parser->lexer);
23315 if (!parens.require_close (parser))
23316 declarator = cp_error_declarator;
23317 if (declarator == cp_error_declarator)
23318 break;
23320 goto handle_declarator;
23322 /* Otherwise, we must be done. */
23323 else
23324 break;
23326 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
23327 && token->type == CPP_OPEN_SQUARE
23328 && !cp_next_tokens_can_be_attribute_p (parser))
23330 /* Parse an array-declarator. */
23331 tree bounds, attrs;
23333 if (ctor_dtor_or_conv_p)
23334 *ctor_dtor_or_conv_p = 0;
23336 open_paren = NULL;
23337 first = false;
23338 parser->default_arg_ok_p = false;
23339 parser->in_declarator_p = true;
23340 /* Consume the `['. */
23341 cp_lexer_consume_token (parser->lexer);
23342 /* Peek at the next token. */
23343 token = cp_lexer_peek_token (parser->lexer);
23344 /* If the next token is `]', then there is no
23345 constant-expression. */
23346 if (token->type != CPP_CLOSE_SQUARE)
23348 bool non_constant_p;
23349 bounds
23350 = cp_parser_constant_expression (parser,
23351 /*allow_non_constant=*/true,
23352 &non_constant_p);
23353 if (!non_constant_p)
23354 /* OK */;
23355 else if (error_operand_p (bounds))
23356 /* Already gave an error. */;
23357 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
23358 /* Let compute_array_index_type diagnose this. */;
23359 else if (!parser->in_function_body
23360 || parsing_function_declarator ())
23362 /* Normally, the array bound must be an integral constant
23363 expression. However, as an extension, we allow VLAs
23364 in function scopes as long as they aren't part of a
23365 parameter declaration. */
23366 cp_parser_error (parser,
23367 "array bound is not an integer constant");
23368 bounds = error_mark_node;
23370 else if (processing_template_decl
23371 && !type_dependent_expression_p (bounds))
23373 /* Remember this wasn't a constant-expression. */
23374 bounds = build_nop (TREE_TYPE (bounds), bounds);
23375 TREE_SIDE_EFFECTS (bounds) = 1;
23378 else
23379 bounds = NULL_TREE;
23380 /* Look for the closing `]'. */
23381 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
23383 declarator = cp_error_declarator;
23384 break;
23387 attrs = cp_parser_std_attribute_spec_seq (parser);
23388 declarator = make_array_declarator (declarator, bounds);
23389 declarator->std_attributes = attrs;
23391 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
23394 tree qualifying_scope;
23395 tree unqualified_name;
23396 tree attrs;
23397 special_function_kind sfk;
23398 bool abstract_ok;
23399 bool pack_expansion_p = false;
23400 cp_token *declarator_id_start_token;
23402 /* Parse a declarator-id */
23403 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
23404 if (abstract_ok)
23406 cp_parser_parse_tentatively (parser);
23408 /* If we see an ellipsis, we should be looking at a
23409 parameter pack. */
23410 if (token->type == CPP_ELLIPSIS)
23412 /* Consume the `...' */
23413 cp_lexer_consume_token (parser->lexer);
23415 pack_expansion_p = true;
23419 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
23420 unqualified_name
23421 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
23422 qualifying_scope = parser->scope;
23423 if (abstract_ok)
23425 bool okay = false;
23427 if (!unqualified_name && pack_expansion_p)
23429 /* Check whether an error occurred. */
23430 okay = !cp_parser_error_occurred (parser);
23432 /* We already consumed the ellipsis to mark a
23433 parameter pack, but we have no way to report it,
23434 so abort the tentative parse. We will be exiting
23435 immediately anyway. */
23436 cp_parser_abort_tentative_parse (parser);
23438 else
23439 okay = cp_parser_parse_definitely (parser);
23441 if (!okay)
23442 unqualified_name = error_mark_node;
23443 else if (unqualified_name
23444 && (qualifying_scope
23445 || (!identifier_p (unqualified_name))))
23447 cp_parser_error (parser, "expected unqualified-id");
23448 unqualified_name = error_mark_node;
23452 if (!unqualified_name)
23453 return NULL;
23454 if (unqualified_name == error_mark_node)
23456 declarator = cp_error_declarator;
23457 pack_expansion_p = false;
23458 declarator->parameter_pack_p = false;
23459 break;
23462 attrs = cp_parser_std_attribute_spec_seq (parser);
23464 if (qualifying_scope && at_namespace_scope_p ()
23465 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
23467 /* In the declaration of a member of a template class
23468 outside of the class itself, the SCOPE will sometimes
23469 be a TYPENAME_TYPE. For example, given:
23471 template <typename T>
23472 int S<T>::R::i = 3;
23474 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
23475 this context, we must resolve S<T>::R to an ordinary
23476 type, rather than a typename type.
23478 The reason we normally avoid resolving TYPENAME_TYPEs
23479 is that a specialization of `S' might render
23480 `S<T>::R' not a type. However, if `S' is
23481 specialized, then this `i' will not be used, so there
23482 is no harm in resolving the types here. */
23483 tree type;
23485 /* Resolve the TYPENAME_TYPE. */
23486 type = resolve_typename_type (qualifying_scope,
23487 /*only_current_p=*/false);
23488 /* If that failed, the declarator is invalid. */
23489 if (TREE_CODE (type) == TYPENAME_TYPE)
23491 if (typedef_variant_p (type))
23492 error_at (declarator_id_start_token->location,
23493 "cannot define member of dependent typedef "
23494 "%qT", type);
23495 else
23496 error_at (declarator_id_start_token->location,
23497 "%<%T::%E%> is not a type",
23498 TYPE_CONTEXT (qualifying_scope),
23499 TYPE_IDENTIFIER (qualifying_scope));
23501 qualifying_scope = type;
23504 sfk = sfk_none;
23506 if (unqualified_name)
23508 tree class_type;
23510 if (qualifying_scope
23511 && CLASS_TYPE_P (qualifying_scope))
23512 class_type = qualifying_scope;
23513 else
23514 class_type = current_class_type;
23516 if (TREE_CODE (unqualified_name) == TYPE_DECL)
23518 tree name_type = TREE_TYPE (unqualified_name);
23520 if (!class_type || !same_type_p (name_type, class_type))
23522 /* We do not attempt to print the declarator
23523 here because we do not have enough
23524 information about its original syntactic
23525 form. */
23526 cp_parser_error (parser, "invalid declarator");
23527 declarator = cp_error_declarator;
23528 break;
23530 else if (qualifying_scope
23531 && CLASSTYPE_USE_TEMPLATE (name_type))
23533 error_at (declarator_id_start_token->location,
23534 "invalid use of constructor as a template");
23535 inform (declarator_id_start_token->location,
23536 "use %<%T::%D%> instead of %<%T::%D%> to "
23537 "name the constructor in a qualified name",
23538 class_type,
23539 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
23540 class_type, name_type);
23541 declarator = cp_error_declarator;
23542 break;
23544 unqualified_name = constructor_name (class_type);
23547 if (class_type)
23549 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
23550 sfk = sfk_destructor;
23551 else if (identifier_p (unqualified_name)
23552 && IDENTIFIER_CONV_OP_P (unqualified_name))
23553 sfk = sfk_conversion;
23554 else if (/* There's no way to declare a constructor
23555 for an unnamed type, even if the type
23556 got a name for linkage purposes. */
23557 !TYPE_WAS_UNNAMED (class_type)
23558 /* Handle correctly (c++/19200):
23560 struct S {
23561 struct T{};
23562 friend void S(T);
23565 and also:
23567 namespace N {
23568 void S();
23571 struct S {
23572 friend void N::S();
23573 }; */
23574 && (!friend_p || class_type == qualifying_scope)
23575 && constructor_name_p (unqualified_name,
23576 class_type))
23577 sfk = sfk_constructor;
23578 else if (is_overloaded_fn (unqualified_name)
23579 && DECL_CONSTRUCTOR_P (get_first_fn
23580 (unqualified_name)))
23581 sfk = sfk_constructor;
23583 if (ctor_dtor_or_conv_p && sfk != sfk_none)
23584 *ctor_dtor_or_conv_p = -1;
23587 declarator = make_id_declarator (qualifying_scope,
23588 unqualified_name,
23589 sfk, token->location);
23590 declarator->std_attributes = attrs;
23591 declarator->parameter_pack_p = pack_expansion_p;
23593 if (pack_expansion_p)
23594 maybe_warn_variadic_templates ();
23596 /* We're looking for this case in [temp.res]:
23597 A qualified-id is assumed to name a type if [...]
23598 - it is a decl-specifier of the decl-specifier-seq of a
23599 parameter-declaration in a declarator of a function or
23600 function template declaration, ... */
23601 if (cxx_dialect >= cxx20
23602 && (flags & CP_PARSER_FLAGS_TYPENAME_OPTIONAL)
23603 && declarator->kind == cdk_id
23604 && !at_class_scope_p ()
23605 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23607 /* ...whose declarator-id is qualified. If it isn't, never
23608 assume the parameters to refer to types. */
23609 if (qualifying_scope == NULL_TREE)
23610 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23611 else
23613 /* Now we have something like
23614 template <typename T> int C::x(S::p);
23615 which can be a function template declaration or a
23616 variable template definition. If name lookup for
23617 the declarator-id C::x finds one or more function
23618 templates, assume S::p to name a type. Otherwise,
23619 don't. */
23620 tree decl
23621 = cp_parser_lookup_name (parser, unqualified_name,
23622 none_type,
23623 /*is_template=*/false,
23624 /*is_namespace=*/false,
23625 /*check_dependency=*/false,
23626 /*ambiguous_decls=*/NULL,
23627 token->location);
23629 if (!is_overloaded_fn (decl)
23630 /* Allow
23631 template<typename T>
23632 A<T>::A(T::type) { } */
23633 && !(MAYBE_CLASS_TYPE_P (qualifying_scope)
23634 && constructor_name_p (unqualified_name,
23635 qualifying_scope)))
23636 flags &= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
23641 handle_declarator:;
23642 scope = get_scope_of_declarator (declarator);
23643 if (scope)
23645 /* Any names that appear after the declarator-id for a
23646 member are looked up in the containing scope. */
23647 if (at_function_scope_p ())
23649 /* But declarations with qualified-ids can't appear in a
23650 function. */
23651 cp_parser_error (parser, "qualified-id in declaration");
23652 declarator = cp_error_declarator;
23653 break;
23655 pushed_scope = push_scope (scope);
23657 parser->in_declarator_p = true;
23658 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
23659 || (declarator && declarator->kind == cdk_id))
23660 /* Default args are only allowed on function
23661 declarations. */
23662 parser->default_arg_ok_p = saved_default_arg_ok_p;
23663 else
23664 parser->default_arg_ok_p = false;
23666 first = false;
23668 /* We're done. */
23669 else
23670 break;
23673 /* For an abstract declarator, we might wind up with nothing at this
23674 point. That's an error; the declarator is not optional. */
23675 if (!declarator)
23676 cp_parser_error (parser, "expected declarator");
23677 else if (open_paren)
23679 /* Record overly parenthesized declarator so we can give a
23680 diagnostic about confusing decl/expr disambiguation. */
23681 if (declarator->kind == cdk_array)
23683 /* If the open and close parens are on different lines, this
23684 is probably a formatting thing, so ignore. */
23685 expanded_location open = expand_location (open_paren->location);
23686 expanded_location close = expand_location (close_paren->location);
23687 if (open.line != close.line || open.file != close.file)
23688 open_paren = NULL;
23690 if (open_paren)
23691 declarator->parenthesized = make_location (open_paren->location,
23692 open_paren->location,
23693 close_paren->location);
23696 /* If we entered a scope, we must exit it now. */
23697 if (pushed_scope)
23698 pop_scope (pushed_scope);
23700 parser->default_arg_ok_p = saved_default_arg_ok_p;
23701 parser->in_declarator_p = saved_in_declarator_p;
23703 return declarator;
23706 /* Parse a ptr-operator.
23708 ptr-operator:
23709 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23710 * cv-qualifier-seq [opt]
23712 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
23713 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
23715 GNU Extension:
23717 ptr-operator:
23718 & cv-qualifier-seq [opt]
23720 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
23721 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
23722 an rvalue reference. In the case of a pointer-to-member, *TYPE is
23723 filled in with the TYPE containing the member. *CV_QUALS is
23724 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
23725 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
23726 Note that the tree codes returned by this function have nothing
23727 to do with the types of trees that will be eventually be created
23728 to represent the pointer or reference type being parsed. They are
23729 just constants with suggestive names. */
23730 static enum tree_code
23731 cp_parser_ptr_operator (cp_parser* parser,
23732 tree* type,
23733 cp_cv_quals *cv_quals,
23734 tree *attributes)
23736 enum tree_code code = ERROR_MARK;
23737 cp_token *token;
23738 tree attrs = NULL_TREE;
23740 /* Assume that it's not a pointer-to-member. */
23741 *type = NULL_TREE;
23742 /* And that there are no cv-qualifiers. */
23743 *cv_quals = TYPE_UNQUALIFIED;
23745 /* Peek at the next token. */
23746 token = cp_lexer_peek_token (parser->lexer);
23748 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
23749 if (token->type == CPP_MULT)
23750 code = INDIRECT_REF;
23751 else if (token->type == CPP_AND)
23752 code = ADDR_EXPR;
23753 else if ((cxx_dialect != cxx98) &&
23754 token->type == CPP_AND_AND) /* C++0x only */
23755 code = NON_LVALUE_EXPR;
23757 if (code != ERROR_MARK)
23759 /* Consume the `*', `&' or `&&'. */
23760 cp_lexer_consume_token (parser->lexer);
23762 /* A `*' can be followed by a cv-qualifier-seq, and so can a
23763 `&', if we are allowing GNU extensions. (The only qualifier
23764 that can legally appear after `&' is `restrict', but that is
23765 enforced during semantic analysis. */
23766 if (code == INDIRECT_REF
23767 || cp_parser_allow_gnu_extensions_p (parser))
23768 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23770 attrs = cp_parser_std_attribute_spec_seq (parser);
23771 if (attributes != NULL)
23772 *attributes = attrs;
23774 else
23776 /* Try the pointer-to-member case. */
23777 cp_parser_parse_tentatively (parser);
23778 /* Look for the optional `::' operator. */
23779 cp_parser_global_scope_opt (parser,
23780 /*current_scope_valid_p=*/false);
23781 /* Look for the nested-name specifier. */
23782 token = cp_lexer_peek_token (parser->lexer);
23783 cp_parser_nested_name_specifier (parser,
23784 /*typename_keyword_p=*/false,
23785 /*check_dependency_p=*/true,
23786 /*type_p=*/false,
23787 /*is_declaration=*/false);
23788 /* If we found it, and the next token is a `*', then we are
23789 indeed looking at a pointer-to-member operator. */
23790 if (!cp_parser_error_occurred (parser)
23791 && cp_parser_require (parser, CPP_MULT, RT_MULT))
23793 /* Indicate that the `*' operator was used. */
23794 code = INDIRECT_REF;
23796 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
23797 error_at (token->location, "%qD is a namespace", parser->scope);
23798 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
23799 error_at (token->location, "cannot form pointer to member of "
23800 "non-class %q#T", parser->scope);
23801 else
23803 /* The type of which the member is a member is given by the
23804 current SCOPE. */
23805 *type = parser->scope;
23806 /* The next name will not be qualified. */
23807 parser->scope = NULL_TREE;
23808 parser->qualifying_scope = NULL_TREE;
23809 parser->object_scope = NULL_TREE;
23810 /* Look for optional c++11 attributes. */
23811 attrs = cp_parser_std_attribute_spec_seq (parser);
23812 if (attributes != NULL)
23813 *attributes = attrs;
23814 /* Look for the optional cv-qualifier-seq. */
23815 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
23818 /* If that didn't work we don't have a ptr-operator. */
23819 if (!cp_parser_parse_definitely (parser))
23820 cp_parser_error (parser, "expected ptr-operator");
23823 return code;
23826 /* Parse an (optional) cv-qualifier-seq.
23828 cv-qualifier-seq:
23829 cv-qualifier cv-qualifier-seq [opt]
23831 cv-qualifier:
23832 const
23833 volatile
23835 GNU Extension:
23837 cv-qualifier:
23838 __restrict__
23840 Returns a bitmask representing the cv-qualifiers. */
23842 static cp_cv_quals
23843 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
23845 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
23847 while (true)
23849 cp_token *token;
23850 cp_cv_quals cv_qualifier;
23852 /* Peek at the next token. */
23853 token = cp_lexer_peek_token (parser->lexer);
23854 /* See if it's a cv-qualifier. */
23855 switch (token->keyword)
23857 case RID_CONST:
23858 cv_qualifier = TYPE_QUAL_CONST;
23859 break;
23861 case RID_VOLATILE:
23862 cv_qualifier = TYPE_QUAL_VOLATILE;
23863 break;
23865 case RID_RESTRICT:
23866 cv_qualifier = TYPE_QUAL_RESTRICT;
23867 break;
23869 default:
23870 cv_qualifier = TYPE_UNQUALIFIED;
23871 break;
23874 if (!cv_qualifier)
23875 break;
23877 if (cv_quals & cv_qualifier)
23879 gcc_rich_location richloc (token->location);
23880 richloc.add_fixit_remove ();
23881 error_at (&richloc, "duplicate cv-qualifier");
23882 cp_lexer_purge_token (parser->lexer);
23884 else
23886 cp_lexer_consume_token (parser->lexer);
23887 cv_quals |= cv_qualifier;
23891 return cv_quals;
23894 /* Parse an (optional) ref-qualifier
23896 ref-qualifier:
23900 Returns cp_ref_qualifier representing ref-qualifier. */
23902 static cp_ref_qualifier
23903 cp_parser_ref_qualifier_opt (cp_parser* parser)
23905 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
23907 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
23908 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
23909 return ref_qual;
23911 while (true)
23913 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
23914 cp_token *token = cp_lexer_peek_token (parser->lexer);
23916 switch (token->type)
23918 case CPP_AND:
23919 curr_ref_qual = REF_QUAL_LVALUE;
23920 break;
23922 case CPP_AND_AND:
23923 curr_ref_qual = REF_QUAL_RVALUE;
23924 break;
23926 default:
23927 curr_ref_qual = REF_QUAL_NONE;
23928 break;
23931 if (!curr_ref_qual)
23932 break;
23933 else if (ref_qual)
23935 error_at (token->location, "multiple ref-qualifiers");
23936 cp_lexer_purge_token (parser->lexer);
23938 else
23940 ref_qual = curr_ref_qual;
23941 cp_lexer_consume_token (parser->lexer);
23945 return ref_qual;
23948 /* Parse an optional tx-qualifier.
23950 tx-qualifier:
23951 transaction_safe
23952 transaction_safe_dynamic */
23954 static tree
23955 cp_parser_tx_qualifier_opt (cp_parser *parser)
23957 cp_token *token = cp_lexer_peek_token (parser->lexer);
23958 if (token->type == CPP_NAME)
23960 tree name = token->u.value;
23961 const char *p = IDENTIFIER_POINTER (name);
23962 const int len = strlen ("transaction_safe");
23963 if (startswith (p, "transaction_safe"))
23965 p += len;
23966 if (*p == '\0'
23967 || !strcmp (p, "_dynamic"))
23969 cp_lexer_consume_token (parser->lexer);
23970 if (!flag_tm)
23972 error ("%qE requires %<-fgnu-tm%>", name);
23973 return NULL_TREE;
23975 else
23976 return name;
23980 return NULL_TREE;
23983 /* Parse an (optional) virt-specifier-seq.
23985 virt-specifier-seq:
23986 virt-specifier virt-specifier-seq [opt]
23988 virt-specifier:
23989 override
23990 final
23992 Returns a bitmask representing the virt-specifiers. */
23994 static cp_virt_specifiers
23995 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
23997 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
23999 while (true)
24001 cp_token *token;
24002 cp_virt_specifiers virt_specifier;
24004 /* Peek at the next token. */
24005 token = cp_lexer_peek_token (parser->lexer);
24006 /* See if it's a virt-specifier-qualifier. */
24007 if (token->type != CPP_NAME)
24008 break;
24009 if (id_equal (token->u.value, "override"))
24011 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24012 virt_specifier = VIRT_SPEC_OVERRIDE;
24014 else if (id_equal (token->u.value, "final"))
24016 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
24017 virt_specifier = VIRT_SPEC_FINAL;
24019 else if (id_equal (token->u.value, "__final"))
24021 virt_specifier = VIRT_SPEC_FINAL;
24023 else
24024 break;
24026 if (virt_specifiers & virt_specifier)
24028 gcc_rich_location richloc (token->location);
24029 richloc.add_fixit_remove ();
24030 error_at (&richloc, "duplicate virt-specifier");
24031 cp_lexer_purge_token (parser->lexer);
24033 else
24035 cp_lexer_consume_token (parser->lexer);
24036 virt_specifiers |= virt_specifier;
24039 return virt_specifiers;
24042 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
24043 is in scope even though it isn't real. */
24045 void
24046 inject_this_parameter (tree ctype, cp_cv_quals quals)
24048 tree this_parm;
24050 if (current_class_ptr)
24052 /* We don't clear this between NSDMIs. Is it already what we want? */
24053 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
24054 if (DECL_P (current_class_ptr)
24055 && DECL_CONTEXT (current_class_ptr) == NULL_TREE
24056 && same_type_ignoring_top_level_qualifiers_p (ctype, type)
24057 && cp_type_quals (type) == quals)
24058 return;
24061 this_parm = build_this_parm (NULL_TREE, ctype, quals);
24062 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
24063 current_class_ptr = NULL_TREE;
24064 current_class_ref
24065 = cp_build_fold_indirect_ref (this_parm);
24066 current_class_ptr = this_parm;
24069 /* Return true iff our current scope is a non-static data member
24070 initializer. */
24072 bool
24073 parsing_nsdmi (void)
24075 /* We recognize NSDMI context by the context-less 'this' pointer set up
24076 by the function above. */
24077 if (current_class_ptr
24078 && TREE_CODE (current_class_ptr) == PARM_DECL
24079 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
24080 return true;
24081 return false;
24084 /* True if we're parsing a function declarator. */
24086 bool
24087 parsing_function_declarator ()
24089 /* this_entity is NULL for a function parameter scope while parsing the
24090 declarator; it is set when parsing the body of the function. */
24091 return (current_binding_level->kind == sk_function_parms
24092 && !current_binding_level->this_entity);
24095 /* Parse a late-specified return type, if any. This is not a separate
24096 non-terminal, but part of a function declarator, which looks like
24098 -> trailing-type-specifier-seq abstract-declarator(opt)
24100 Returns the type indicated by the type-id.
24102 In addition to this, parse any queued up #pragma omp declare simd
24103 clauses, and #pragma acc routine clauses.
24105 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
24106 function. */
24108 static tree
24109 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
24110 tree& requires_clause)
24112 cp_token *token;
24113 tree type = NULL_TREE;
24114 bool declare_simd_p = (parser->omp_declare_simd
24115 && declarator
24116 && declarator->kind == cdk_id);
24118 bool oacc_routine_p = (parser->oacc_routine
24119 && declarator
24120 && declarator->kind == cdk_id);
24122 /* Peek at the next token. */
24123 token = cp_lexer_peek_token (parser->lexer);
24124 /* A late-specified return type is indicated by an initial '->'. */
24125 if (token->type != CPP_DEREF
24126 && token->keyword != RID_REQUIRES
24127 && !(token->type == CPP_NAME
24128 && token->u.value == ridpointers[RID_REQUIRES])
24129 && !(declare_simd_p || oacc_routine_p))
24130 return NULL_TREE;
24132 if (token->type == CPP_DEREF)
24134 /* Consume the ->. */
24135 cp_lexer_consume_token (parser->lexer);
24137 type = cp_parser_trailing_type_id (parser);
24140 /* Function declarations may be followed by a trailing
24141 requires-clause. */
24142 requires_clause = cp_parser_requires_clause_opt (parser, false);
24144 if (declare_simd_p)
24145 declarator->attributes
24146 = cp_parser_late_parsing_omp_declare_simd (parser,
24147 declarator->attributes);
24148 if (oacc_routine_p)
24149 declarator->attributes
24150 = cp_parser_late_parsing_oacc_routine (parser,
24151 declarator->attributes);
24153 return type;
24156 /* Parse a declarator-id.
24158 declarator-id:
24159 id-expression
24160 :: [opt] nested-name-specifier [opt] type-name
24162 In the `id-expression' case, the value returned is as for
24163 cp_parser_id_expression if the id-expression was an unqualified-id.
24164 If the id-expression was a qualified-id, then a SCOPE_REF is
24165 returned. The first operand is the scope (either a NAMESPACE_DECL
24166 or TREE_TYPE), but the second is still just a representation of an
24167 unqualified-id. */
24169 static tree
24170 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
24172 tree id;
24173 /* The expression must be an id-expression. Assume that qualified
24174 names are the names of types so that:
24176 template <class T>
24177 int S<T>::R::i = 3;
24179 will work; we must treat `S<T>::R' as the name of a type.
24180 Similarly, assume that qualified names are templates, where
24181 required, so that:
24183 template <class T>
24184 int S<T>::R<T>::i = 3;
24186 will work, too. */
24187 id = cp_parser_id_expression (parser,
24188 /*template_keyword_p=*/false,
24189 /*check_dependency_p=*/false,
24190 /*template_p=*/NULL,
24191 /*declarator_p=*/true,
24192 optional_p);
24193 if (id && BASELINK_P (id))
24194 id = BASELINK_FUNCTIONS (id);
24195 return id;
24198 /* Parse a type-id.
24200 type-id:
24201 type-specifier-seq abstract-declarator [opt]
24203 The parser flags FLAGS is used to control type-specifier parsing.
24205 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
24207 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24208 i.e. we've just seen "->".
24210 Returns the TYPE specified. */
24212 static tree
24213 cp_parser_type_id_1 (cp_parser *parser, cp_parser_flags flags,
24214 bool is_template_arg, bool is_trailing_return,
24215 location_t *type_location)
24217 cp_decl_specifier_seq type_specifier_seq;
24218 cp_declarator *abstract_declarator;
24220 /* Parse the type-specifier-seq. */
24221 cp_parser_type_specifier_seq (parser, flags,
24222 /*is_declaration=*/false,
24223 is_trailing_return,
24224 &type_specifier_seq);
24225 if (type_location)
24226 *type_location = type_specifier_seq.locations[ds_type_spec];
24228 if (is_template_arg && type_specifier_seq.type
24229 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
24230 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
24231 /* A bare template name as a template argument is a template template
24232 argument, not a placeholder, so fail parsing it as a type argument. */
24234 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
24235 cp_parser_simulate_error (parser);
24236 return error_mark_node;
24238 if (type_specifier_seq.type == error_mark_node)
24239 return error_mark_node;
24241 /* There might or might not be an abstract declarator. */
24242 cp_parser_parse_tentatively (parser);
24243 /* Look for the declarator. */
24244 abstract_declarator
24245 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT,
24246 CP_PARSER_FLAGS_NONE, NULL,
24247 /*parenthesized_p=*/NULL,
24248 /*member_p=*/false,
24249 /*friend_p=*/false,
24250 /*static_p=*/false);
24251 /* Check to see if there really was a declarator. */
24252 if (!cp_parser_parse_definitely (parser))
24253 abstract_declarator = NULL;
24255 bool auto_typeid_ok = false;
24256 /* The concepts TS allows 'auto' as a type-id. */
24257 if (flag_concepts_ts)
24258 auto_typeid_ok = !parser->in_type_id_in_expr_p;
24259 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
24260 outside the template-argument-list context here only for the sake of
24261 diagnostic: grokdeclarator then can emit a better error message for
24262 e.g. using T = auto. */
24263 else if (flag_concepts)
24264 auto_typeid_ok = (!parser->in_type_id_in_expr_p
24265 && !parser->in_template_argument_list_p);
24267 if (type_specifier_seq.type
24268 && !auto_typeid_ok
24269 /* None of the valid uses of 'auto' in C++14 involve the type-id
24270 nonterminal, but it is valid in a trailing-return-type. */
24271 && !(cxx_dialect >= cxx14 && is_trailing_return))
24272 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
24274 /* A type-id with type 'auto' is only ok if the abstract declarator
24275 is a function declarator with a late-specified return type.
24277 A type-id with 'auto' is also valid in a trailing-return-type
24278 in a compound-requirement. */
24279 if (abstract_declarator
24280 && abstract_declarator->kind == cdk_function
24281 && abstract_declarator->u.function.late_return_type)
24282 /* OK */;
24283 else if (parser->in_result_type_constraint_p)
24284 /* OK */;
24285 else
24287 if (!cp_parser_simulate_error (parser))
24289 location_t loc = type_specifier_seq.locations[ds_type_spec];
24290 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
24292 auto_diagnostic_group g;
24293 gcc_rich_location richloc (loc);
24294 richloc.add_fixit_insert_after ("<>");
24295 error_at (&richloc, "missing template arguments after %qE",
24296 tmpl);
24297 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
24298 tmpl);
24300 else if (parser->in_template_argument_list_p)
24301 error_at (loc, "%qT not permitted in template argument",
24302 auto_node);
24303 else
24304 error_at (loc, "invalid use of %qT", auto_node);
24306 return error_mark_node;
24310 return groktypename (&type_specifier_seq, abstract_declarator,
24311 is_template_arg);
24314 /* Wrapper for cp_parser_type_id_1. */
24316 static tree
24317 cp_parser_type_id (cp_parser *parser, cp_parser_flags flags,
24318 location_t *type_location)
24320 return cp_parser_type_id_1 (parser, flags, false, false, type_location);
24323 /* Wrapper for cp_parser_type_id_1. */
24325 static tree
24326 cp_parser_template_type_arg (cp_parser *parser)
24328 tree r;
24329 const char *saved_message = parser->type_definition_forbidden_message;
24330 parser->type_definition_forbidden_message
24331 = G_("types may not be defined in template arguments");
24332 r = cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_NONE, true, false, NULL);
24333 parser->type_definition_forbidden_message = saved_message;
24334 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
24336 error ("invalid use of %<auto%> in template argument");
24337 r = error_mark_node;
24339 return r;
24342 /* Wrapper for cp_parser_type_id_1. */
24344 static tree
24345 cp_parser_trailing_type_id (cp_parser *parser)
24347 return cp_parser_type_id_1 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL,
24348 false, true, NULL);
24351 /* Parse a type-specifier-seq.
24353 type-specifier-seq:
24354 type-specifier type-specifier-seq [opt]
24356 GNU extension:
24358 type-specifier-seq:
24359 attributes type-specifier-seq [opt]
24361 The parser flags FLAGS is used to control type-specifier parsing.
24363 If IS_DECLARATION is true, we are at the start of a "condition" or
24364 exception-declaration, so we might be followed by a declarator-id.
24366 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
24367 i.e. we've just seen "->".
24369 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
24371 static void
24372 cp_parser_type_specifier_seq (cp_parser* parser,
24373 cp_parser_flags flags,
24374 bool is_declaration,
24375 bool is_trailing_return,
24376 cp_decl_specifier_seq *type_specifier_seq)
24378 bool seen_type_specifier = false;
24379 cp_token *start_token = NULL;
24381 /* Clear the TYPE_SPECIFIER_SEQ. */
24382 clear_decl_specs (type_specifier_seq);
24384 flags |= CP_PARSER_FLAGS_OPTIONAL;
24385 /* In the context of a trailing return type, enum E { } is an
24386 elaborated-type-specifier followed by a function-body, not an
24387 enum-specifier. */
24388 if (is_trailing_return)
24389 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
24391 /* Parse the type-specifiers and attributes. */
24392 while (true)
24394 tree type_specifier;
24395 bool is_cv_qualifier;
24397 /* Check for attributes first. */
24398 if (cp_next_tokens_can_be_attribute_p (parser))
24400 /* GNU attributes at the end of a declaration apply to the
24401 declaration as a whole, not to the trailing return type. So look
24402 ahead to see if these attributes are at the end. */
24403 if (seen_type_specifier && is_trailing_return
24404 && cp_next_tokens_can_be_gnu_attribute_p (parser))
24406 size_t n = cp_parser_skip_attributes_opt (parser, 1);
24407 cp_token *tok = cp_lexer_peek_nth_token (parser->lexer, n);
24408 if (tok->type == CPP_SEMICOLON || tok->type == CPP_COMMA
24409 || tok->type == CPP_EQ || tok->type == CPP_OPEN_BRACE)
24410 break;
24412 type_specifier_seq->attributes
24413 = attr_chainon (type_specifier_seq->attributes,
24414 cp_parser_attributes_opt (parser));
24415 continue;
24418 /* record the token of the beginning of the type specifier seq,
24419 for error reporting purposes*/
24420 if (!start_token)
24421 start_token = cp_lexer_peek_token (parser->lexer);
24423 /* Look for the type-specifier. */
24424 type_specifier = cp_parser_type_specifier (parser,
24425 flags,
24426 type_specifier_seq,
24427 /*is_declaration=*/false,
24428 NULL,
24429 &is_cv_qualifier);
24430 if (!type_specifier)
24432 /* If the first type-specifier could not be found, this is not a
24433 type-specifier-seq at all. */
24434 if (!seen_type_specifier)
24436 /* Set in_declarator_p to avoid skipping to the semicolon. */
24437 int in_decl = parser->in_declarator_p;
24438 parser->in_declarator_p = true;
24440 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
24441 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
24442 cp_parser_error (parser, "expected type-specifier");
24444 parser->in_declarator_p = in_decl;
24446 type_specifier_seq->type = error_mark_node;
24447 return;
24449 /* If subsequent type-specifiers could not be found, the
24450 type-specifier-seq is complete. */
24451 break;
24454 seen_type_specifier = true;
24455 /* The standard says that a condition can be:
24457 type-specifier-seq declarator = assignment-expression
24459 However, given:
24461 struct S {};
24462 if (int S = ...)
24464 we should treat the "S" as a declarator, not as a
24465 type-specifier. The standard doesn't say that explicitly for
24466 type-specifier-seq, but it does say that for
24467 decl-specifier-seq in an ordinary declaration. Perhaps it
24468 would be clearer just to allow a decl-specifier-seq here, and
24469 then add a semantic restriction that if any decl-specifiers
24470 that are not type-specifiers appear, the program is invalid. */
24471 if (is_declaration && !is_cv_qualifier)
24472 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
24476 /* Return whether the function currently being declared has an associated
24477 template parameter list. */
24479 static bool
24480 function_being_declared_is_template_p (cp_parser* parser)
24482 if (!current_template_parms || processing_template_parmlist)
24483 return false;
24485 if (parser->implicit_template_scope)
24486 return true;
24488 if (at_class_scope_p ()
24489 && TYPE_BEING_DEFINED (current_class_type))
24490 return parser->num_template_parameter_lists != 0;
24492 return ((int) parser->num_template_parameter_lists > template_class_depth
24493 (current_class_type));
24496 /* Parse a parameter-declaration-clause.
24498 parameter-declaration-clause:
24499 parameter-declaration-list [opt] ... [opt]
24500 parameter-declaration-list , ...
24502 The parser flags FLAGS is used to control type-specifier parsing.
24504 Returns a representation for the parameter declarations. A return
24505 value of NULL indicates a parameter-declaration-clause consisting
24506 only of an ellipsis. */
24508 static tree
24509 cp_parser_parameter_declaration_clause (cp_parser* parser,
24510 cp_parser_flags flags)
24512 tree parameters;
24513 cp_token *token;
24514 bool ellipsis_p;
24516 auto cleanup = make_temp_override
24517 (parser->auto_is_implicit_function_template_parm_p);
24519 if (!processing_specialization
24520 && !processing_template_parmlist
24521 && !processing_explicit_instantiation
24522 /* default_arg_ok_p tracks whether this is a parameter-clause for an
24523 actual function or a random abstract declarator. */
24524 && parser->default_arg_ok_p)
24525 if (!current_function_decl
24526 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
24527 parser->auto_is_implicit_function_template_parm_p = true;
24529 /* Peek at the next token. */
24530 token = cp_lexer_peek_token (parser->lexer);
24531 /* Check for trivial parameter-declaration-clauses. */
24532 if (token->type == CPP_ELLIPSIS)
24534 /* Consume the `...' token. */
24535 cp_lexer_consume_token (parser->lexer);
24536 return NULL_TREE;
24538 else if (token->type == CPP_CLOSE_PAREN)
24539 /* There are no parameters. */
24540 return void_list_node;
24541 /* Check for `(void)', too, which is a special case. */
24542 else if (token->keyword == RID_VOID
24543 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
24544 == CPP_CLOSE_PAREN))
24546 /* Consume the `void' token. */
24547 cp_lexer_consume_token (parser->lexer);
24548 /* There are no parameters. */
24549 return explicit_void_list_node;
24552 /* A vector of parameters that haven't been pushed yet. */
24553 auto_vec<tree> pending_decls;
24555 /* Parse the parameter-declaration-list. */
24556 parameters = cp_parser_parameter_declaration_list (parser, flags,
24557 &pending_decls);
24558 /* If a parse error occurred while parsing the
24559 parameter-declaration-list, then the entire
24560 parameter-declaration-clause is erroneous. */
24561 if (parameters == error_mark_node)
24562 return NULL_TREE;
24564 /* Peek at the next token. */
24565 token = cp_lexer_peek_token (parser->lexer);
24566 /* If it's a `,', the clause should terminate with an ellipsis. */
24567 if (token->type == CPP_COMMA)
24569 /* Consume the `,'. */
24570 cp_lexer_consume_token (parser->lexer);
24571 /* Expect an ellipsis. */
24572 ellipsis_p
24573 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
24575 /* It might also be `...' if the optional trailing `,' was
24576 omitted. */
24577 else if (token->type == CPP_ELLIPSIS)
24579 /* Consume the `...' token. */
24580 cp_lexer_consume_token (parser->lexer);
24581 /* And remember that we saw it. */
24582 ellipsis_p = true;
24584 else
24585 ellipsis_p = false;
24587 /* A valid parameter-declaration-clause can only be followed by a ')'.
24588 So it's time to push all the parameters we have seen now that we
24589 know we have a valid declaration. Note that here we may not have
24590 committed yet, nor should we. Pushing here will detect the error
24591 of redefining a parameter. */
24592 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24593 for (tree p : pending_decls)
24594 pushdecl (p);
24596 /* Finish the parameter list. */
24597 if (!ellipsis_p)
24598 parameters = chainon (parameters, void_list_node);
24600 return parameters;
24603 /* Parse a parameter-declaration-list.
24605 parameter-declaration-list:
24606 parameter-declaration
24607 parameter-declaration-list , parameter-declaration
24609 The parser flags FLAGS is used to control type-specifier parsing.
24610 PENDING_DECLS is a vector of parameters that haven't been pushed yet.
24612 Returns a representation of the parameter-declaration-list, as for
24613 cp_parser_parameter_declaration_clause. However, the
24614 `void_list_node' is never appended to the list. */
24616 static tree
24617 cp_parser_parameter_declaration_list (cp_parser* parser,
24618 cp_parser_flags flags,
24619 auto_vec<tree> *pending_decls)
24621 tree parameters = NULL_TREE;
24622 tree *tail = &parameters;
24623 bool saved_in_unbraced_linkage_specification_p;
24624 int index = 0;
24626 /* The special considerations that apply to a function within an
24627 unbraced linkage specifications do not apply to the parameters
24628 to the function. */
24629 saved_in_unbraced_linkage_specification_p
24630 = parser->in_unbraced_linkage_specification_p;
24631 parser->in_unbraced_linkage_specification_p = false;
24633 /* Look for more parameters. */
24634 while (true)
24636 cp_parameter_declarator *parameter;
24637 tree decl = error_mark_node;
24638 bool parenthesized_p = false;
24640 /* Parse the parameter. */
24641 parameter
24642 = cp_parser_parameter_declaration (parser, flags,
24643 /*template_parm_p=*/false,
24644 &parenthesized_p);
24646 /* We don't know yet if the enclosing context is unavailable or deprecated,
24647 so wait and deal with it in grokparms if appropriate. */
24648 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS;
24650 if (parameter && !cp_parser_error_occurred (parser))
24652 decl = grokdeclarator (parameter->declarator,
24653 &parameter->decl_specifiers,
24654 PARM,
24655 parameter->default_argument != NULL_TREE,
24656 &parameter->decl_specifiers.attributes);
24657 if (decl != error_mark_node && parameter->loc != UNKNOWN_LOCATION)
24658 DECL_SOURCE_LOCATION (decl) = parameter->loc;
24661 deprecated_state = DEPRECATED_NORMAL;
24663 /* If a parse error occurred parsing the parameter declaration,
24664 then the entire parameter-declaration-list is erroneous. */
24665 if (decl == error_mark_node)
24667 parameters = error_mark_node;
24668 break;
24671 if (parameter->decl_specifiers.attributes)
24672 cplus_decl_attributes (&decl,
24673 parameter->decl_specifiers.attributes,
24675 if (DECL_NAME (decl))
24677 /* We cannot always pushdecl while parsing tentatively because
24678 it may have side effects and we can't be sure yet if we're
24679 parsing a declaration, e.g.:
24681 S foo(int(x), int(x), int{x});
24683 where it's not clear if we're dealing with a constructor call
24684 or a function declaration until we've seen the last argument
24685 which breaks it up.
24686 It's safe to pushdecl so long as it doesn't result in a clash
24687 with an already-pushed parameter. But we don't delay pushing
24688 different parameters to handle
24690 S foo(int(i), decltype(i) j = 42);
24692 which is valid. */
24693 if (pending_decls
24694 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24695 /* See if PARAMETERS already contains a parameter with the same
24696 DECL_NAME as DECL. */
24697 && [parameters, decl] {
24698 for (tree p = parameters; p; p = TREE_CHAIN (p))
24699 if (DECL_NAME (decl) == DECL_NAME (TREE_VALUE (p)))
24700 return true;
24701 return false;
24702 }())
24703 pending_decls->safe_push (decl);
24704 else
24705 decl = pushdecl (decl);
24708 if (decl != error_mark_node)
24710 retrofit_lang_decl (decl);
24711 DECL_PARM_INDEX (decl) = ++index;
24712 DECL_PARM_LEVEL (decl) = function_parm_depth ();
24715 /* Add the new parameter to the list. */
24716 *tail = build_tree_list (parameter->default_argument, decl);
24717 tail = &TREE_CHAIN (*tail);
24719 /* If the parameters were parenthesized, it's the case of
24720 T foo(X(x)) which looks like a variable definition but
24721 is a function declaration. */
24722 if (index == 1 || PARENTHESIZED_LIST_P (parameters))
24723 PARENTHESIZED_LIST_P (parameters) = parenthesized_p;
24725 /* Peek at the next token. */
24726 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
24727 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
24728 /* These are for Objective-C++ */
24729 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
24730 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24731 /* The parameter-declaration-list is complete. */
24732 break;
24733 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24735 cp_token *token;
24737 /* Peek at the next token. */
24738 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24739 /* If it's an ellipsis, then the list is complete. */
24740 if (token->type == CPP_ELLIPSIS)
24741 break;
24742 /* Otherwise, there must be more parameters. Consume the
24743 `,'. */
24744 cp_lexer_consume_token (parser->lexer);
24745 /* When parsing something like:
24747 int i(float f, double d)
24749 we can tell after seeing the declaration for "f" that we
24750 are not looking at an initialization of a variable "i",
24751 but rather at the declaration of a function "i".
24753 Due to the fact that the parsing of template arguments
24754 (as specified to a template-id) requires backtracking we
24755 cannot use this technique when inside a template argument
24756 list. */
24757 if (!parser->in_template_argument_list_p
24758 && !parser->in_type_id_in_expr_p
24759 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24760 /* However, a parameter-declaration of the form
24761 "float(f)" (which is a valid declaration of a
24762 parameter "f") can also be interpreted as an
24763 expression (the conversion of "f" to "float"). */
24764 && !parenthesized_p)
24765 cp_parser_commit_to_tentative_parse (parser);
24767 else
24769 cp_parser_error (parser, "expected %<,%> or %<...%>");
24770 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
24771 cp_parser_skip_to_closing_parenthesis (parser,
24772 /*recovering=*/true,
24773 /*or_comma=*/false,
24774 /*consume_paren=*/false);
24775 break;
24779 parser->in_unbraced_linkage_specification_p
24780 = saved_in_unbraced_linkage_specification_p;
24782 /* Reset implicit_template_scope if we are about to leave the function
24783 parameter list that introduced it. Note that for out-of-line member
24784 definitions, there will be one or more class scopes before we get to
24785 the template parameter scope. */
24787 if (cp_binding_level *its = parser->implicit_template_scope)
24788 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
24790 while (maybe_its->kind == sk_class)
24791 maybe_its = maybe_its->level_chain;
24792 if (maybe_its == its)
24794 parser->implicit_template_parms = 0;
24795 parser->implicit_template_scope = 0;
24799 return parameters;
24802 /* Parse a parameter declaration.
24804 parameter-declaration:
24805 decl-specifier-seq ... [opt] declarator
24806 decl-specifier-seq declarator = assignment-expression
24807 decl-specifier-seq ... [opt] abstract-declarator [opt]
24808 decl-specifier-seq abstract-declarator [opt] = assignment-expression
24810 The parser flags FLAGS is used to control type-specifier parsing.
24812 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
24813 declares a template parameter. (In that case, a non-nested `>'
24814 token encountered during the parsing of the assignment-expression
24815 is not interpreted as a greater-than operator.)
24817 Returns a representation of the parameter, or NULL if an error
24818 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
24819 true iff the declarator is of the form "(p)". */
24821 static cp_parameter_declarator *
24822 cp_parser_parameter_declaration (cp_parser *parser,
24823 cp_parser_flags flags,
24824 bool template_parm_p,
24825 bool *parenthesized_p)
24827 int declares_class_or_enum;
24828 cp_decl_specifier_seq decl_specifiers;
24829 cp_declarator *declarator;
24830 tree default_argument;
24831 cp_token *token = NULL, *declarator_token_start = NULL;
24832 const char *saved_message;
24833 bool template_parameter_pack_p = false;
24835 /* In a template parameter, `>' is not an operator.
24837 [temp.param]
24839 When parsing a default template-argument for a non-type
24840 template-parameter, the first non-nested `>' is taken as the end
24841 of the template parameter-list rather than a greater-than
24842 operator. */
24844 /* Type definitions may not appear in parameter types. */
24845 saved_message = parser->type_definition_forbidden_message;
24846 parser->type_definition_forbidden_message
24847 = G_("types may not be defined in parameter types");
24849 int template_parm_idx = (function_being_declared_is_template_p (parser) ?
24850 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
24851 (current_template_parms)) : 0);
24853 /* Parse the declaration-specifiers. */
24854 cp_token *decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
24855 cp_parser_decl_specifier_seq (parser,
24856 flags,
24857 &decl_specifiers,
24858 &declares_class_or_enum);
24860 /* [dcl.spec.auto.general]: "A placeholder-type-specifier of the form
24861 type-constraint opt auto can be used as a decl-specifier of the
24862 decl-specifier-seq of a parameter-declaration of a function declaration
24863 or lambda-expression..." but we must not synthesize an implicit template
24864 type parameter in its declarator. That is, in "void f(auto[auto{10}]);"
24865 we want to synthesize only the first auto. */
24866 auto cleanup = make_temp_override
24867 (parser->auto_is_implicit_function_template_parm_p, false);
24869 /* Complain about missing 'typename' or other invalid type names. */
24870 if (!decl_specifiers.any_type_specifiers_p
24871 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
24872 decl_specifiers.type = error_mark_node;
24874 /* If an error occurred, there's no reason to attempt to parse the
24875 rest of the declaration. */
24876 if (cp_parser_error_occurred (parser))
24878 parser->type_definition_forbidden_message = saved_message;
24879 return NULL;
24882 /* Peek at the next token. */
24883 token = cp_lexer_peek_token (parser->lexer);
24885 /* If the next token is a `)', `,', `=', `>', or `...', then there
24886 is no declarator. However, when variadic templates are enabled,
24887 there may be a declarator following `...'. */
24888 if (token->type == CPP_CLOSE_PAREN
24889 || token->type == CPP_COMMA
24890 || token->type == CPP_EQ
24891 || token->type == CPP_GREATER)
24893 declarator = NULL;
24894 if (parenthesized_p)
24895 *parenthesized_p = false;
24897 /* Otherwise, there should be a declarator. */
24898 else
24900 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
24901 parser->default_arg_ok_p = false;
24903 /* After seeing a decl-specifier-seq, if the next token is not a
24904 "(" or "{", there is no possibility that the code is a valid
24905 expression. Therefore, if parsing tentatively, we commit at
24906 this point. */
24907 if (!parser->in_template_argument_list_p
24908 /* In an expression context, having seen:
24910 (int((char ...
24912 we cannot be sure whether we are looking at a
24913 function-type (taking a "char" as a parameter) or a cast
24914 of some object of type "char" to "int". */
24915 && !parser->in_type_id_in_expr_p
24916 && cp_parser_uncommitted_to_tentative_parse_p (parser)
24917 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
24919 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24921 if (decl_specifiers.type
24922 && template_placeholder_p (decl_specifiers.type))
24923 /* This is a CTAD expression, not a parameter declaration. */
24924 cp_parser_simulate_error (parser);
24926 else
24927 cp_parser_commit_to_tentative_parse (parser);
24929 /* Parse the declarator. */
24930 declarator_token_start = token;
24931 declarator = cp_parser_declarator (parser,
24932 CP_PARSER_DECLARATOR_EITHER,
24933 CP_PARSER_FLAGS_NONE,
24934 /*ctor_dtor_or_conv_p=*/NULL,
24935 parenthesized_p,
24936 /*member_p=*/false,
24937 /*friend_p=*/false,
24938 /*static_p=*/false);
24939 parser->default_arg_ok_p = saved_default_arg_ok_p;
24940 /* After the declarator, allow more attributes. */
24941 decl_specifiers.attributes
24942 = attr_chainon (decl_specifiers.attributes,
24943 cp_parser_attributes_opt (parser));
24945 /* If the declarator is a template parameter pack, remember that and
24946 clear the flag in the declarator itself so we don't get errors
24947 from grokdeclarator. */
24948 if (template_parm_p && declarator && declarator->parameter_pack_p)
24950 declarator->parameter_pack_p = false;
24951 template_parameter_pack_p = true;
24955 /* If the next token is an ellipsis, and we have not seen a declarator
24956 name, and if either the type of the declarator contains parameter
24957 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
24958 for, eg, abbreviated integral type names), then we actually have a
24959 parameter pack expansion expression. Otherwise, leave the ellipsis
24960 for a C-style variadic function. */
24961 token = cp_lexer_peek_token (parser->lexer);
24963 /* If a function parameter pack was specified and an implicit template
24964 parameter was introduced during cp_parser_parameter_declaration,
24965 change any implicit parameters introduced into packs. */
24966 if (parser->implicit_template_parms
24967 && ((token->type == CPP_ELLIPSIS
24968 && declarator_can_be_parameter_pack (declarator))
24969 || (declarator && declarator->parameter_pack_p)))
24971 int latest_template_parm_idx = TREE_VEC_LENGTH
24972 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
24974 if (latest_template_parm_idx != template_parm_idx)
24975 decl_specifiers.type = convert_generic_types_to_packs
24976 (decl_specifiers.type,
24977 template_parm_idx, latest_template_parm_idx);
24980 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24982 tree type = decl_specifiers.type;
24984 if (type && DECL_P (type))
24985 type = TREE_TYPE (type);
24987 if (((type
24988 && TREE_CODE (type) != TYPE_PACK_EXPANSION
24989 && (template_parm_p || uses_parameter_packs (type)))
24990 || (!type && template_parm_p))
24991 && declarator_can_be_parameter_pack (declarator))
24993 /* Consume the `...'. */
24994 cp_lexer_consume_token (parser->lexer);
24995 maybe_warn_variadic_templates ();
24997 /* Build a pack expansion type */
24998 if (template_parm_p)
24999 template_parameter_pack_p = true;
25000 else if (declarator)
25001 declarator->parameter_pack_p = true;
25002 else
25003 decl_specifiers.type = make_pack_expansion (type);
25007 /* The restriction on defining new types applies only to the type
25008 of the parameter, not to the default argument. */
25009 parser->type_definition_forbidden_message = saved_message;
25011 /* If the next token is `=', then process a default argument. */
25012 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25014 tree type = decl_specifiers.type;
25015 token = cp_lexer_peek_token (parser->lexer);
25016 if (declarator)
25017 declarator->init_loc = token->location;
25018 /* If we are defining a class, then the tokens that make up the
25019 default argument must be saved and processed later. */
25020 if (!template_parm_p && at_class_scope_p ()
25021 && TYPE_BEING_DEFINED (current_class_type)
25022 && !LAMBDA_TYPE_P (current_class_type))
25023 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
25025 /* A constrained-type-specifier may declare a type
25026 template-parameter. */
25027 else if (declares_constrained_type_template_parameter (type))
25028 default_argument
25029 = cp_parser_default_type_template_argument (parser);
25031 /* A constrained-type-specifier may declare a
25032 template-template-parameter. */
25033 else if (declares_constrained_template_template_parameter (type))
25034 default_argument
25035 = cp_parser_default_template_template_argument (parser);
25037 /* Outside of a class definition, we can just parse the
25038 assignment-expression. */
25039 else
25040 default_argument
25041 = cp_parser_default_argument (parser, template_parm_p);
25043 if (!parser->default_arg_ok_p)
25045 permerror (token->location,
25046 "default arguments are only "
25047 "permitted for function parameters");
25049 else if ((declarator && declarator->parameter_pack_p)
25050 || template_parameter_pack_p
25051 || (decl_specifiers.type
25052 && PACK_EXPANSION_P (decl_specifiers.type)))
25054 /* Find the name of the parameter pack. */
25055 cp_declarator *id_declarator = declarator;
25056 while (id_declarator && id_declarator->kind != cdk_id)
25057 id_declarator = id_declarator->declarator;
25059 if (id_declarator && id_declarator->kind == cdk_id)
25060 error_at (declarator_token_start->location,
25061 template_parm_p
25062 ? G_("template parameter pack %qD "
25063 "cannot have a default argument")
25064 : G_("parameter pack %qD cannot have "
25065 "a default argument"),
25066 id_declarator->u.id.unqualified_name);
25067 else
25068 error_at (declarator_token_start->location,
25069 template_parm_p
25070 ? G_("template parameter pack cannot have "
25071 "a default argument")
25072 : G_("parameter pack cannot have a "
25073 "default argument"));
25075 default_argument = NULL_TREE;
25078 else
25079 default_argument = NULL_TREE;
25081 if (default_argument)
25082 STRIP_ANY_LOCATION_WRAPPER (default_argument);
25084 /* Generate a location for the parameter, ranging from the start of the
25085 initial token to the end of the final token (using input_location for
25086 the latter, set up by cp_lexer_set_source_position_from_token when
25087 consuming tokens).
25089 If we have a identifier, then use it for the caret location, e.g.
25091 extern int callee (int one, int (*two)(int, int), float three);
25092 ~~~~~~^~~~~~~~~~~~~~
25094 otherwise, reuse the start location for the caret location e.g.:
25096 extern int callee (int one, int (*)(int, int), float three);
25097 ^~~~~~~~~~~~~~~~~
25100 location_t caret_loc = (declarator && declarator->id_loc != UNKNOWN_LOCATION
25101 ? declarator->id_loc
25102 : decl_spec_token_start->location);
25103 location_t param_loc = make_location (caret_loc,
25104 decl_spec_token_start->location,
25105 input_location);
25107 return make_parameter_declarator (&decl_specifiers,
25108 declarator,
25109 default_argument,
25110 param_loc,
25111 template_parameter_pack_p);
25114 /* Parse a default argument and return it.
25116 TEMPLATE_PARM_P is true if this is a default argument for a
25117 non-type template parameter. */
25118 static tree
25119 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
25121 tree default_argument = NULL_TREE;
25122 bool saved_greater_than_is_operator_p;
25123 unsigned char saved_local_variables_forbidden_p;
25124 bool non_constant_p, is_direct_init;
25126 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
25127 set correctly. */
25128 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
25129 parser->greater_than_is_operator_p = !template_parm_p;
25130 auto odsd = make_temp_override (parser->omp_declare_simd, NULL);
25131 auto ord = make_temp_override (parser->oacc_routine, NULL);
25132 auto oafp = make_temp_override (parser->omp_attrs_forbidden_p, false);
25134 /* Local variable names (and the `this' keyword) may not
25135 appear in a default argument. */
25136 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
25137 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
25138 /* Parse the assignment-expression. */
25139 if (template_parm_p)
25140 push_deferring_access_checks (dk_no_deferred);
25141 tree saved_class_ptr = NULL_TREE;
25142 tree saved_class_ref = NULL_TREE;
25143 /* The "this" pointer is not valid in a default argument. */
25144 if (cfun)
25146 saved_class_ptr = current_class_ptr;
25147 cp_function_chain->x_current_class_ptr = NULL_TREE;
25148 saved_class_ref = current_class_ref;
25149 cp_function_chain->x_current_class_ref = NULL_TREE;
25151 default_argument
25152 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
25153 /* Restore the "this" pointer. */
25154 if (cfun)
25156 cp_function_chain->x_current_class_ptr = saved_class_ptr;
25157 cp_function_chain->x_current_class_ref = saved_class_ref;
25159 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
25160 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25161 if (template_parm_p)
25162 pop_deferring_access_checks ();
25163 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
25164 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
25166 return default_argument;
25169 /* Parse a function-body.
25171 function-body:
25172 compound_statement */
25174 static void
25175 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
25177 cp_parser_compound_statement (parser, NULL, (in_function_try_block
25178 ? BCS_TRY_BLOCK : BCS_NORMAL),
25179 true);
25182 /* Parse a ctor-initializer-opt followed by a function-body. Return
25183 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
25184 is true we are parsing a function-try-block. */
25186 static void
25187 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
25188 bool in_function_try_block)
25190 tree body, list;
25191 const bool check_body_p
25192 = (DECL_CONSTRUCTOR_P (current_function_decl)
25193 && DECL_DECLARED_CONSTEXPR_P (current_function_decl));
25194 tree last = NULL;
25196 if (in_function_try_block
25197 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
25198 && cxx_dialect < cxx20)
25200 if (DECL_CONSTRUCTOR_P (current_function_decl))
25201 pedwarn (input_location, OPT_Wc__20_extensions,
25202 "function-try-block body of %<constexpr%> constructor only "
25203 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25204 else
25205 pedwarn (input_location, OPT_Wc__20_extensions,
25206 "function-try-block body of %<constexpr%> function only "
25207 "available with %<-std=c++20%> or %<-std=gnu++20%>");
25210 /* Begin the function body. */
25211 body = begin_function_body ();
25212 /* Parse the optional ctor-initializer. */
25213 cp_parser_ctor_initializer_opt (parser);
25215 /* If we're parsing a constexpr constructor definition, we need
25216 to check that the constructor body is indeed empty. However,
25217 before we get to cp_parser_function_body lot of junk has been
25218 generated, so we can't just check that we have an empty block.
25219 Rather we take a snapshot of the outermost block, and check whether
25220 cp_parser_function_body changed its state. */
25221 if (check_body_p)
25223 list = cur_stmt_list;
25224 if (STATEMENT_LIST_TAIL (list))
25225 last = STATEMENT_LIST_TAIL (list)->stmt;
25227 /* Parse the function-body. */
25228 cp_parser_function_body (parser, in_function_try_block);
25229 if (check_body_p)
25230 check_constexpr_ctor_body (last, list, /*complain=*/true);
25231 /* Finish the function body. */
25232 finish_function_body (body);
25235 /* Parse an initializer.
25237 initializer:
25238 = initializer-clause
25239 ( expression-list )
25241 Returns an expression representing the initializer. If no
25242 initializer is present, NULL_TREE is returned.
25244 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
25245 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
25246 set to TRUE if there is no initializer present. If there is an
25247 initializer, and it is not a constant-expression, *NON_CONSTANT_P
25248 is set to true; otherwise it is set to false. */
25250 static tree
25251 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
25252 bool* non_constant_p, bool subexpression_p)
25254 cp_token *token;
25255 tree init;
25257 /* Peek at the next token. */
25258 token = cp_lexer_peek_token (parser->lexer);
25260 /* Let our caller know whether or not this initializer was
25261 parenthesized. */
25262 *is_direct_init = (token->type != CPP_EQ);
25263 /* Assume that the initializer is constant. */
25264 *non_constant_p = false;
25266 if (token->type == CPP_EQ)
25268 /* Consume the `='. */
25269 cp_lexer_consume_token (parser->lexer);
25270 /* Parse the initializer-clause. */
25271 init = cp_parser_initializer_clause (parser, non_constant_p);
25273 else if (token->type == CPP_OPEN_PAREN)
25275 vec<tree, va_gc> *vec;
25276 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25277 /*cast_p=*/false,
25278 /*allow_expansion_p=*/true,
25279 non_constant_p);
25280 if (vec == NULL)
25281 return error_mark_node;
25282 init = build_tree_list_vec (vec);
25283 release_tree_vector (vec);
25285 else if (token->type == CPP_OPEN_BRACE)
25287 cp_lexer_set_source_position (parser->lexer);
25288 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25289 init = cp_parser_braced_list (parser, non_constant_p);
25290 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
25292 else
25294 /* Anything else is an error. */
25295 cp_parser_error (parser, "expected initializer");
25296 init = error_mark_node;
25299 if (!subexpression_p && check_for_bare_parameter_packs (init))
25300 init = error_mark_node;
25302 return init;
25305 /* Parse an initializer-clause.
25307 initializer-clause:
25308 assignment-expression
25309 braced-init-list
25311 Returns an expression representing the initializer.
25313 If the `assignment-expression' production is used the value
25314 returned is simply a representation for the expression.
25316 Otherwise, calls cp_parser_braced_list. */
25318 static cp_expr
25319 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
25321 cp_expr initializer;
25323 /* Assume the expression is constant. */
25324 *non_constant_p = false;
25326 /* If it is not a `{', then we are looking at an
25327 assignment-expression. */
25328 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
25330 initializer
25331 = cp_parser_constant_expression (parser,
25332 /*allow_non_constant_p=*/2,
25333 non_constant_p);
25335 else
25336 initializer = cp_parser_braced_list (parser, non_constant_p);
25338 return initializer;
25341 /* Parse a brace-enclosed initializer list.
25343 braced-init-list:
25344 { initializer-list , [opt] }
25345 { designated-initializer-list , [opt] }
25348 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
25349 the elements of the initializer-list (or NULL, if the last
25350 production is used). The TREE_TYPE for the CONSTRUCTOR will be
25351 NULL_TREE. There is no way to detect whether or not the optional
25352 trailing `,' was provided. NON_CONSTANT_P is as for
25353 cp_parser_initializer. */
25355 static cp_expr
25356 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
25358 tree initializer;
25359 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
25361 /* Consume the `{' token. */
25362 matching_braces braces;
25363 braces.require_open (parser);
25364 /* Create a CONSTRUCTOR to represent the braced-initializer. */
25365 initializer = make_node (CONSTRUCTOR);
25366 /* If it's not a `}', then there is a non-trivial initializer. */
25367 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
25369 bool designated;
25370 /* Parse the initializer list. */
25371 CONSTRUCTOR_ELTS (initializer)
25372 = cp_parser_initializer_list (parser, non_constant_p, &designated);
25373 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer) = designated;
25374 /* A trailing `,' token is allowed. */
25375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25376 cp_lexer_consume_token (parser->lexer);
25378 else
25379 *non_constant_p = false;
25380 /* Now, there should be a trailing `}'. */
25381 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
25382 braces.require_close (parser);
25383 TREE_TYPE (initializer) = init_list_type_node;
25385 cp_expr result (initializer);
25386 /* Build a location of the form:
25387 { ... }
25388 ^~~~~~~
25389 with caret==start at the open brace, finish at the close brace. */
25390 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25391 result.set_location (combined_loc);
25392 return result;
25395 /* Consume tokens up to, and including, the next non-nested closing `]'.
25396 Returns true iff we found a closing `]'. */
25398 static bool
25399 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
25401 unsigned square_depth = 0;
25403 while (true)
25405 cp_token * token = cp_lexer_peek_token (parser->lexer);
25407 switch (token->type)
25409 case CPP_PRAGMA_EOL:
25410 if (!parser->lexer->in_pragma)
25411 break;
25412 /* FALLTHRU */
25414 case CPP_EOF:
25415 /* If we've run out of tokens, then there is no closing `]'. */
25416 return false;
25418 case CPP_OPEN_SQUARE:
25419 ++square_depth;
25420 break;
25422 case CPP_CLOSE_SQUARE:
25423 if (!square_depth--)
25425 cp_lexer_consume_token (parser->lexer);
25426 return true;
25428 break;
25430 default:
25431 break;
25434 /* Consume the token. */
25435 cp_lexer_consume_token (parser->lexer);
25439 /* Return true if we are looking at an array-designator, false otherwise. */
25441 static bool
25442 cp_parser_array_designator_p (cp_parser *parser)
25444 /* Consume the `['. */
25445 cp_lexer_consume_token (parser->lexer);
25447 cp_lexer_save_tokens (parser->lexer);
25449 /* Skip tokens until the next token is a closing square bracket.
25450 If we find the closing `]', and the next token is a `=', then
25451 we are looking at an array designator. */
25452 bool array_designator_p
25453 = (cp_parser_skip_to_closing_square_bracket (parser)
25454 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
25456 /* Roll back the tokens we skipped. */
25457 cp_lexer_rollback_tokens (parser->lexer);
25459 return array_designator_p;
25462 /* Parse an initializer-list.
25464 initializer-list:
25465 initializer-clause ... [opt]
25466 initializer-list , initializer-clause ... [opt]
25468 C++20 Extension:
25470 designated-initializer-list:
25471 designated-initializer-clause
25472 designated-initializer-list , designated-initializer-clause
25474 designated-initializer-clause:
25475 designator brace-or-equal-initializer
25477 designator:
25478 . identifier
25480 GNU Extension:
25482 initializer-list:
25483 designation initializer-clause ...[opt]
25484 initializer-list , designation initializer-clause ...[opt]
25486 designation:
25487 . identifier =
25488 identifier :
25489 [ constant-expression ] =
25491 Returns a vec of constructor_elt. The VALUE of each elt is an expression
25492 for the initializer. If the INDEX of the elt is non-NULL, it is the
25493 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
25494 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
25495 are any designators. */
25497 static vec<constructor_elt, va_gc> *
25498 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p,
25499 bool *designated)
25501 vec<constructor_elt, va_gc> *v = NULL;
25502 bool first_p = true;
25503 tree first_designator = NULL_TREE;
25505 /* Assume all of the expressions are constant. */
25506 *non_constant_p = false;
25508 unsigned nelts = 0;
25509 int suppress = suppress_location_wrappers;
25511 /* Parse the rest of the list. */
25512 while (true)
25514 cp_token *token;
25515 tree designator;
25516 tree initializer;
25517 bool clause_non_constant_p;
25518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25520 /* Handle the C++20 syntax, '. id ='. */
25521 if ((cxx_dialect >= cxx20
25522 || cp_parser_allow_gnu_extensions_p (parser))
25523 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
25524 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
25525 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ
25526 || (cp_lexer_peek_nth_token (parser->lexer, 3)->type
25527 == CPP_OPEN_BRACE)))
25529 if (pedantic && cxx_dialect < cxx20)
25530 pedwarn (loc, OPT_Wc__20_extensions,
25531 "C++ designated initializers only available with "
25532 "%<-std=c++20%> or %<-std=gnu++20%>");
25533 /* Consume the `.'. */
25534 cp_lexer_consume_token (parser->lexer);
25535 /* Consume the identifier. */
25536 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25537 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
25538 /* Consume the `='. */
25539 cp_lexer_consume_token (parser->lexer);
25541 /* Also, if the next token is an identifier and the following one is a
25542 colon, we are looking at the GNU designated-initializer
25543 syntax. */
25544 else if (cp_parser_allow_gnu_extensions_p (parser)
25545 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
25546 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
25547 == CPP_COLON))
25549 /* Warn the user that they are using an extension. */
25550 pedwarn (loc, OPT_Wpedantic,
25551 "ISO C++ does not allow GNU designated initializers");
25552 /* Consume the identifier. */
25553 designator = cp_lexer_consume_token (parser->lexer)->u.value;
25554 /* Consume the `:'. */
25555 cp_lexer_consume_token (parser->lexer);
25557 /* Also handle C99 array designators, '[ const ] ='. */
25558 else if (cp_parser_allow_gnu_extensions_p (parser)
25559 && !c_dialect_objc ()
25560 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
25562 /* In C++11, [ could start a lambda-introducer. */
25563 bool non_const = false;
25565 cp_parser_parse_tentatively (parser);
25567 if (!cp_parser_array_designator_p (parser))
25569 cp_parser_simulate_error (parser);
25570 designator = NULL_TREE;
25572 else
25574 designator = cp_parser_constant_expression (parser, true,
25575 &non_const);
25576 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
25577 cp_parser_require (parser, CPP_EQ, RT_EQ);
25580 if (!cp_parser_parse_definitely (parser))
25581 designator = NULL_TREE;
25582 else if (non_const
25583 && (!require_potential_rvalue_constant_expression
25584 (designator)))
25585 designator = NULL_TREE;
25586 if (designator)
25587 /* Warn the user that they are using an extension. */
25588 pedwarn (loc, OPT_Wpedantic,
25589 "ISO C++ does not allow C99 designated initializers");
25591 else
25592 designator = NULL_TREE;
25594 if (first_p)
25596 first_designator = designator;
25597 first_p = false;
25599 else if (cxx_dialect >= cxx20
25600 && first_designator != error_mark_node
25601 && (!first_designator != !designator))
25603 error_at (loc, "either all initializer clauses should be designated "
25604 "or none of them should be");
25605 first_designator = error_mark_node;
25607 else if (cxx_dialect < cxx20 && !first_designator)
25608 first_designator = designator;
25610 /* Parse the initializer. */
25611 initializer = cp_parser_initializer_clause (parser,
25612 &clause_non_constant_p);
25613 /* If any clause is non-constant, so is the entire initializer. */
25614 if (clause_non_constant_p)
25615 *non_constant_p = true;
25617 /* If we have an ellipsis, this is an initializer pack
25618 expansion. */
25619 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25621 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25623 /* Consume the `...'. */
25624 cp_lexer_consume_token (parser->lexer);
25626 if (designator && cxx_dialect >= cxx20)
25627 error_at (loc,
25628 "%<...%> not allowed in designated initializer list");
25630 /* Turn the initializer into an initializer expansion. */
25631 initializer = make_pack_expansion (initializer);
25634 /* Add it to the vector. */
25635 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
25637 /* If the next token is not a comma, we have reached the end of
25638 the list. */
25639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25640 break;
25642 /* Peek at the next token. */
25643 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25644 /* If the next token is a `}', then we're still done. An
25645 initializer-clause can have a trailing `,' after the
25646 initializer-list and before the closing `}'. */
25647 if (token->type == CPP_CLOSE_BRACE)
25648 break;
25650 /* Suppress location wrappers in a long initializer to save memory
25651 (14179). The cutoff is chosen arbitrarily. */
25652 const unsigned loc_max = 256;
25653 unsigned incr = 1;
25654 if (TREE_CODE (initializer) == CONSTRUCTOR)
25655 /* Look one level down because it's easy. Looking deeper would require
25656 passing down a nelts pointer, and I don't think multi-level massive
25657 initializers are common enough to justify this. */
25658 incr = CONSTRUCTOR_NELTS (initializer);
25659 nelts += incr;
25660 if (nelts >= loc_max && (nelts - incr) < loc_max)
25661 ++suppress_location_wrappers;
25663 /* Consume the `,' token. */
25664 cp_lexer_consume_token (parser->lexer);
25667 /* The same identifier shall not appear in multiple designators
25668 of a designated-initializer-list. */
25669 if (first_designator)
25671 unsigned int i;
25672 tree designator, val;
25673 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25674 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25676 if (IDENTIFIER_MARKED (designator))
25678 error_at (cp_expr_loc_or_input_loc (val),
25679 "%<.%s%> designator used multiple times in "
25680 "the same initializer list",
25681 IDENTIFIER_POINTER (designator));
25682 (*v)[i].index = error_mark_node;
25684 else
25685 IDENTIFIER_MARKED (designator) = 1;
25687 FOR_EACH_CONSTRUCTOR_ELT (v, i, designator, val)
25688 if (designator && TREE_CODE (designator) == IDENTIFIER_NODE)
25689 IDENTIFIER_MARKED (designator) = 0;
25692 suppress_location_wrappers = suppress;
25694 *designated = first_designator != NULL_TREE;
25695 return v;
25698 /* Classes [gram.class] */
25700 /* Parse a class-name.
25702 class-name:
25703 identifier
25704 template-id
25706 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
25707 to indicate that names looked up in dependent types should be
25708 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
25709 keyword has been used to indicate that the name that appears next
25710 is a template. TAG_TYPE indicates the explicit tag given before
25711 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
25712 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
25713 is the class being defined in a class-head. If ENUM_OK is TRUE,
25714 enum-names are also accepted.
25716 Returns the TYPE_DECL representing the class. */
25718 static tree
25719 cp_parser_class_name (cp_parser *parser,
25720 bool typename_keyword_p,
25721 bool template_keyword_p,
25722 enum tag_types tag_type,
25723 bool check_dependency_p,
25724 bool class_head_p,
25725 bool is_declaration,
25726 bool enum_ok)
25728 tree decl;
25729 tree identifier = NULL_TREE;
25731 /* All class-names start with an identifier. */
25732 cp_token *token = cp_lexer_peek_token (parser->lexer);
25733 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
25735 cp_parser_error (parser, "expected class-name");
25736 return error_mark_node;
25739 /* PARSER->SCOPE can be cleared when parsing the template-arguments
25740 to a template-id, so we save it here. Consider object scope too,
25741 so that make_typename_type below can use it (cp_parser_template_name
25742 considers object scope also). This may happen with code like
25744 p->template A<T>::a()
25746 where we first want to look up A<T>::a in the class of the object
25747 expression, as per [basic.lookup.classref]. */
25748 tree scope = parser->scope ? parser->scope : parser->context->object_type;
25749 /* This only checks parser->scope to avoid duplicate errors; if
25750 ->object_type is erroneous, go on to give a parse error. */
25751 if (parser->scope == error_mark_node)
25752 return error_mark_node;
25754 /* Any name names a type if we're following the `typename' keyword
25755 in a qualified name where the enclosing scope is type-dependent. */
25756 const bool typename_p = (typename_keyword_p
25757 && parser->scope
25758 && TYPE_P (parser->scope)
25759 && dependent_scope_p (parser->scope));
25760 /* Handle the common case (an identifier, but not a template-id)
25761 efficiently. */
25762 if (token->type == CPP_NAME
25763 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
25765 cp_token *identifier_token;
25766 bool ambiguous_p;
25768 /* Look for the identifier. */
25769 identifier_token = cp_lexer_peek_token (parser->lexer);
25770 ambiguous_p = identifier_token->error_reported;
25771 identifier = cp_parser_identifier (parser);
25772 /* If the next token isn't an identifier, we are certainly not
25773 looking at a class-name. */
25774 if (identifier == error_mark_node)
25775 decl = error_mark_node;
25776 /* If we know this is a type-name, there's no need to look it
25777 up. */
25778 else if (typename_p)
25779 decl = identifier;
25780 else
25782 tree ambiguous_decls;
25783 /* If we already know that this lookup is ambiguous, then
25784 we've already issued an error message; there's no reason
25785 to check again. */
25786 if (ambiguous_p)
25788 cp_parser_simulate_error (parser);
25789 return error_mark_node;
25791 /* If the next token is a `::', then the name must be a type
25792 name.
25794 [basic.lookup.qual]
25796 During the lookup for a name preceding the :: scope
25797 resolution operator, object, function, and enumerator
25798 names are ignored. */
25799 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25800 tag_type = scope_type;
25801 /* Look up the name. */
25802 decl = cp_parser_lookup_name (parser, identifier,
25803 tag_type,
25804 /*is_template=*/false,
25805 /*is_namespace=*/false,
25806 check_dependency_p,
25807 &ambiguous_decls,
25808 identifier_token->location);
25809 if (ambiguous_decls)
25811 if (cp_parser_parsing_tentatively (parser))
25812 cp_parser_simulate_error (parser);
25813 return error_mark_node;
25817 else
25819 /* Try a template-id. */
25820 decl = cp_parser_template_id (parser, template_keyword_p,
25821 check_dependency_p,
25822 tag_type,
25823 is_declaration);
25824 if (decl == error_mark_node)
25825 return error_mark_node;
25828 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
25830 /* If this is a typename, create a TYPENAME_TYPE. */
25831 if (typename_p && decl != error_mark_node)
25833 decl = make_typename_type (scope, decl, typename_type,
25834 /*complain=*/tf_error);
25835 if (decl != error_mark_node)
25836 decl = TYPE_NAME (decl);
25839 decl = strip_using_decl (decl);
25841 /* Check to see that it is really the name of a class. */
25842 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
25843 && identifier_p (TREE_OPERAND (decl, 0))
25844 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
25845 /* Situations like this:
25847 template <typename T> struct A {
25848 typename T::template X<int>::I i;
25851 are problematic. Is `T::template X<int>' a class-name? The
25852 standard does not seem to be definitive, but there is no other
25853 valid interpretation of the following `::'. Therefore, those
25854 names are considered class-names. */
25856 decl = make_typename_type (scope, decl, tag_type, tf_error);
25857 if (decl != error_mark_node)
25858 decl = TYPE_NAME (decl);
25860 else if (TREE_CODE (decl) != TYPE_DECL
25861 || TREE_TYPE (decl) == error_mark_node
25862 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
25863 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
25864 /* In Objective-C 2.0, a classname followed by '.' starts a
25865 dot-syntax expression, and it's not a type-name. */
25866 || (c_dialect_objc ()
25867 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
25868 && objc_is_class_name (decl)))
25869 decl = error_mark_node;
25871 if (decl == error_mark_node)
25872 cp_parser_error (parser, "expected class-name");
25873 else if (identifier && !parser->scope)
25874 maybe_note_name_used_in_class (identifier, decl);
25876 return decl;
25879 /* Make sure that any member-function parameters are in scope.
25880 For instance, a function's noexcept-specifier can use the function's
25881 parameters:
25883 struct S {
25884 void fn (int p) noexcept(noexcept(p));
25887 so we need to make sure name lookup can find them. This is used
25888 when we delay parsing of the noexcept-specifier. */
25890 static void
25891 inject_parm_decls (tree decl)
25893 begin_scope (sk_function_parms, decl);
25894 tree args = DECL_ARGUMENTS (decl);
25896 do_push_parm_decls (decl, args, /*nonparms=*/NULL);
25898 if (args && is_this_parameter (args))
25900 gcc_checking_assert (current_class_ptr == NULL_TREE);
25901 current_class_ref = cp_build_fold_indirect_ref (args);
25902 current_class_ptr = args;
25906 /* Undo the effects of inject_parm_decls. */
25908 static void
25909 pop_injected_parms (void)
25911 pop_bindings_and_leave_scope ();
25912 current_class_ptr = current_class_ref = NULL_TREE;
25915 /* Parse a class-specifier.
25917 class-specifier:
25918 class-head { member-specification [opt] }
25920 Returns the TREE_TYPE representing the class. */
25922 tree
25923 cp_parser_class_specifier (cp_parser* parser)
25925 auto_timevar tv (TV_PARSE_STRUCT);
25927 tree type;
25928 tree attributes = NULL_TREE;
25929 bool nested_name_specifier_p;
25930 unsigned saved_num_template_parameter_lists;
25931 bool saved_in_function_body;
25932 unsigned char in_statement;
25933 bool in_switch_statement_p;
25934 bool saved_in_unbraced_linkage_specification_p;
25935 tree old_scope = NULL_TREE;
25936 tree scope = NULL_TREE;
25937 cp_token *closing_brace;
25939 push_deferring_access_checks (dk_no_deferred);
25941 /* Parse the class-head. */
25942 type = cp_parser_class_head (parser,
25943 &nested_name_specifier_p);
25944 /* If the class-head was a semantic disaster, skip the entire body
25945 of the class. */
25946 if (!type)
25948 cp_parser_skip_to_end_of_block_or_statement (parser);
25949 pop_deferring_access_checks ();
25950 return error_mark_node;
25953 /* Look for the `{'. */
25954 matching_braces braces;
25955 if (!braces.require_open (parser))
25957 pop_deferring_access_checks ();
25958 return error_mark_node;
25961 cp_ensure_no_omp_declare_simd (parser);
25962 cp_ensure_no_oacc_routine (parser);
25964 /* Issue an error message if type-definitions are forbidden here. */
25965 bool type_definition_ok_p = cp_parser_check_type_definition (parser);
25966 /* Remember that we are defining one more class. */
25967 ++parser->num_classes_being_defined;
25968 /* Inside the class, surrounding template-parameter-lists do not
25969 apply. */
25970 saved_num_template_parameter_lists
25971 = parser->num_template_parameter_lists;
25972 parser->num_template_parameter_lists = 0;
25973 /* We are not in a function body. */
25974 saved_in_function_body = parser->in_function_body;
25975 parser->in_function_body = false;
25976 /* Or in a loop. */
25977 in_statement = parser->in_statement;
25978 parser->in_statement = 0;
25979 /* Or in a switch. */
25980 in_switch_statement_p = parser->in_switch_statement_p;
25981 parser->in_switch_statement_p = false;
25982 /* We are not immediately inside an extern "lang" block. */
25983 saved_in_unbraced_linkage_specification_p
25984 = parser->in_unbraced_linkage_specification_p;
25985 parser->in_unbraced_linkage_specification_p = false;
25987 /* Start the class. */
25988 if (nested_name_specifier_p)
25990 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
25991 /* SCOPE must be a scope nested inside current scope. */
25992 if (is_nested_namespace (current_namespace,
25993 decl_namespace_context (scope)))
25994 old_scope = push_inner_scope (scope);
25995 else
25996 nested_name_specifier_p = false;
25998 type = begin_class_definition (type);
26000 if (type == error_mark_node)
26001 /* If the type is erroneous, skip the entire body of the class. */
26002 cp_parser_skip_to_closing_brace (parser);
26003 else
26004 /* Parse the member-specification. */
26005 cp_parser_member_specification_opt (parser);
26007 /* Look for the trailing `}'. */
26008 closing_brace = braces.require_close (parser);
26009 /* Look for trailing attributes to apply to this class. */
26010 if (cp_parser_allow_gnu_extensions_p (parser))
26011 attributes = cp_parser_gnu_attributes_opt (parser);
26012 if (type != error_mark_node)
26013 type = finish_struct (type, attributes);
26014 if (nested_name_specifier_p)
26015 pop_inner_scope (old_scope, scope);
26017 /* We've finished a type definition. Check for the common syntax
26018 error of forgetting a semicolon after the definition. We need to
26019 be careful, as we can't just check for not-a-semicolon and be done
26020 with it; the user might have typed:
26022 class X { } c = ...;
26023 class X { } *p = ...;
26025 and so forth. Instead, enumerate all the possible tokens that
26026 might follow this production; if we don't see one of them, then
26027 complain and silently insert the semicolon. */
26029 cp_token *token = cp_lexer_peek_token (parser->lexer);
26030 bool want_semicolon = true;
26032 if (cp_next_tokens_can_be_std_attribute_p (parser))
26033 /* Don't try to parse c++11 attributes here. As per the
26034 grammar, that should be a task for
26035 cp_parser_decl_specifier_seq. */
26036 want_semicolon = false;
26038 switch (token->type)
26040 case CPP_NAME:
26041 case CPP_SEMICOLON:
26042 case CPP_MULT:
26043 case CPP_AND:
26044 case CPP_OPEN_PAREN:
26045 case CPP_CLOSE_PAREN:
26046 case CPP_COMMA:
26047 case CPP_SCOPE:
26048 want_semicolon = false;
26049 break;
26051 /* While it's legal for type qualifiers and storage class
26052 specifiers to follow type definitions in the grammar, only
26053 compiler testsuites contain code like that. Assume that if
26054 we see such code, then what we're really seeing is a case
26055 like:
26057 class X { }
26058 const <type> var = ...;
26062 class Y { }
26063 static <type> func (...) ...
26065 i.e. the qualifier or specifier applies to the next
26066 declaration. To do so, however, we need to look ahead one
26067 more token to see if *that* token is a type specifier.
26069 This code could be improved to handle:
26071 class Z { }
26072 static const <type> var = ...; */
26073 case CPP_KEYWORD:
26074 if (keyword_is_decl_specifier (token->keyword))
26076 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
26078 /* Handling user-defined types here would be nice, but very
26079 tricky. */
26080 want_semicolon
26081 = (lookahead->type == CPP_KEYWORD
26082 && keyword_begins_type_specifier (lookahead->keyword));
26084 break;
26085 default:
26086 break;
26089 /* If we don't have a type, then something is very wrong and we
26090 shouldn't try to do anything clever. Likewise for not seeing the
26091 closing brace. */
26092 if (closing_brace && TYPE_P (type) && want_semicolon)
26094 /* Locate the closing brace. */
26095 cp_token_position prev
26096 = cp_lexer_previous_token_position (parser->lexer);
26097 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
26098 location_t loc = prev_token->location;
26100 /* We want to suggest insertion of a ';' immediately *after* the
26101 closing brace, so, if we can, offset the location by 1 column. */
26102 location_t next_loc = loc;
26103 if (!linemap_location_from_macro_expansion_p (line_table, loc))
26104 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
26106 rich_location richloc (line_table, next_loc);
26108 /* If we successfully offset the location, suggest the fix-it. */
26109 if (next_loc != loc)
26110 richloc.add_fixit_insert_before (next_loc, ";");
26112 if (CLASSTYPE_DECLARED_CLASS (type))
26113 error_at (&richloc,
26114 "expected %<;%> after class definition");
26115 else if (TREE_CODE (type) == RECORD_TYPE)
26116 error_at (&richloc,
26117 "expected %<;%> after struct definition");
26118 else if (TREE_CODE (type) == UNION_TYPE)
26119 error_at (&richloc,
26120 "expected %<;%> after union definition");
26121 else
26122 gcc_unreachable ();
26124 /* Unget one token and smash it to look as though we encountered
26125 a semicolon in the input stream. */
26126 cp_lexer_set_token_position (parser->lexer, prev);
26127 token = cp_lexer_peek_token (parser->lexer);
26128 token->type = CPP_SEMICOLON;
26129 token->keyword = RID_MAX;
26133 /* If this class is not itself within the scope of another class,
26134 then we need to parse the bodies of all of the queued function
26135 definitions. Note that the queued functions defined in a class
26136 are not always processed immediately following the
26137 class-specifier for that class. Consider:
26139 struct A {
26140 struct B { void f() { sizeof (A); } };
26143 If `f' were processed before the processing of `A' were
26144 completed, there would be no way to compute the size of `A'.
26145 Note that the nesting we are interested in here is lexical --
26146 not the semantic nesting given by TYPE_CONTEXT. In particular,
26147 for:
26149 struct A { struct B; };
26150 struct A::B { void f() { } };
26152 there is no need to delay the parsing of `A::B::f'. */
26153 if (--parser->num_classes_being_defined == 0)
26155 tree decl;
26156 tree class_type = NULL_TREE;
26157 tree pushed_scope = NULL_TREE;
26158 unsigned ix;
26159 cp_default_arg_entry *e;
26161 if (!type_definition_ok_p || any_erroneous_template_args_p (type))
26163 /* Skip default arguments, NSDMIs, etc, in order to improve
26164 error recovery (c++/71169, c++/71832). */
26165 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26166 vec_safe_truncate (unparsed_nsdmis, 0);
26167 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26170 /* In a first pass, parse default arguments to the functions.
26171 Then, in a second pass, parse the bodies of the functions.
26172 This two-phased approach handles cases like:
26174 struct S {
26175 void f() { g(); }
26176 void g(int i = 3);
26180 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
26182 decl = e->decl;
26183 /* If there are default arguments that have not yet been processed,
26184 take care of them now. */
26185 if (class_type != e->class_type)
26187 if (pushed_scope)
26188 pop_scope (pushed_scope);
26189 class_type = e->class_type;
26190 pushed_scope = push_scope (class_type);
26192 /* Make sure that any template parameters are in scope. */
26193 maybe_begin_member_template_processing (decl);
26194 /* Parse the default argument expressions. */
26195 cp_parser_late_parsing_default_args (parser, decl);
26196 /* Remove any template parameters from the symbol table. */
26197 maybe_end_member_template_processing ();
26199 vec_safe_truncate (unparsed_funs_with_default_args, 0);
26201 /* If there are noexcept-specifiers that have not yet been processed,
26202 take care of them now. Do this before processing NSDMIs as they
26203 may depend on noexcept-specifiers already having been processed. */
26204 tree save_ccp = current_class_ptr;
26205 tree save_ccr = current_class_ref;
26206 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts, ix, decl)
26208 tree ctx = DECL_CONTEXT (decl);
26209 if (class_type != ctx)
26211 if (pushed_scope)
26212 pop_scope (pushed_scope);
26213 class_type = ctx;
26214 pushed_scope = push_scope (class_type);
26217 tree def_parse = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
26218 def_parse = TREE_PURPOSE (def_parse);
26220 /* Make sure that any template parameters are in scope. */
26221 maybe_begin_member_template_processing (decl);
26223 /* Make sure that any member-function parameters are in scope.
26224 This function doesn't expect ccp to be set. */
26225 current_class_ptr = current_class_ref = NULL_TREE;
26226 inject_parm_decls (decl);
26228 /* 'this' is not allowed in static member functions. */
26229 unsigned char local_variables_forbidden_p
26230 = parser->local_variables_forbidden_p;
26231 if (DECL_THIS_STATIC (decl))
26232 parser->local_variables_forbidden_p |= THIS_FORBIDDEN;
26234 /* Now we can parse the noexcept-specifier. */
26235 tree spec = cp_parser_late_noexcept_specifier (parser, def_parse);
26237 if (spec == error_mark_node)
26238 spec = NULL_TREE;
26240 /* Update the fn's type directly -- it might have escaped
26241 beyond this decl :( */
26242 fixup_deferred_exception_variants (TREE_TYPE (decl), spec);
26243 /* Update any instantiations we've already created. We must
26244 keep the new noexcept-specifier wrapped in a DEFERRED_NOEXCEPT
26245 so that maybe_instantiate_noexcept can tsubst the NOEXCEPT_EXPR
26246 in the pattern. */
26247 for (tree i : DEFPARSE_INSTANTIATIONS (def_parse))
26248 DEFERRED_NOEXCEPT_PATTERN (TREE_PURPOSE (i))
26249 = spec ? TREE_PURPOSE (spec) : error_mark_node;
26251 /* Restore the state of local_variables_forbidden_p. */
26252 parser->local_variables_forbidden_p = local_variables_forbidden_p;
26254 /* The finish_struct call above performed various override checking,
26255 but it skipped unparsed noexcept-specifier operands. Now that we
26256 have resolved them, check again. */
26257 noexcept_override_late_checks (type, decl);
26259 /* Remove any member-function parameters from the symbol table. */
26260 pop_injected_parms ();
26262 /* Remove any template parameters from the symbol table. */
26263 maybe_end_member_template_processing ();
26265 vec_safe_truncate (unparsed_noexcepts, 0);
26267 /* Now parse any NSDMIs. */
26268 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
26270 if (class_type != DECL_CONTEXT (decl))
26272 if (pushed_scope)
26273 pop_scope (pushed_scope);
26274 class_type = DECL_CONTEXT (decl);
26275 pushed_scope = push_scope (class_type);
26277 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
26278 cp_parser_late_parsing_nsdmi (parser, decl);
26280 vec_safe_truncate (unparsed_nsdmis, 0);
26281 current_class_ptr = save_ccp;
26282 current_class_ref = save_ccr;
26283 if (pushed_scope)
26284 pop_scope (pushed_scope);
26286 /* Now parse the body of the functions. */
26287 if (flag_openmp)
26289 /* OpenMP UDRs need to be parsed before all other functions. */
26290 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26291 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
26292 cp_parser_late_parsing_for_member (parser, decl);
26293 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26294 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
26295 cp_parser_late_parsing_for_member (parser, decl);
26297 else
26298 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
26299 cp_parser_late_parsing_for_member (parser, decl);
26300 vec_safe_truncate (unparsed_funs_with_definitions, 0);
26303 /* Put back any saved access checks. */
26304 pop_deferring_access_checks ();
26306 /* Restore saved state. */
26307 parser->in_switch_statement_p = in_switch_statement_p;
26308 parser->in_statement = in_statement;
26309 parser->in_function_body = saved_in_function_body;
26310 parser->num_template_parameter_lists
26311 = saved_num_template_parameter_lists;
26312 parser->in_unbraced_linkage_specification_p
26313 = saved_in_unbraced_linkage_specification_p;
26315 return type;
26318 /* Parse a class-head.
26320 class-head:
26321 class-key identifier [opt] base-clause [opt]
26322 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
26323 class-key nested-name-specifier [opt] template-id
26324 base-clause [opt]
26326 class-virt-specifier:
26327 final
26329 GNU Extensions:
26330 class-key attributes identifier [opt] base-clause [opt]
26331 class-key attributes nested-name-specifier identifier base-clause [opt]
26332 class-key attributes nested-name-specifier [opt] template-id
26333 base-clause [opt]
26335 Upon return BASES is initialized to the list of base classes (or
26336 NULL, if there are none) in the same form returned by
26337 cp_parser_base_clause.
26339 Returns the TYPE of the indicated class. Sets
26340 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
26341 involving a nested-name-specifier was used, and FALSE otherwise.
26343 Returns error_mark_node if this is not a class-head.
26345 Returns NULL_TREE if the class-head is syntactically valid, but
26346 semantically invalid in a way that means we should skip the entire
26347 body of the class. */
26349 static tree
26350 cp_parser_class_head (cp_parser* parser,
26351 bool* nested_name_specifier_p)
26353 tree nested_name_specifier;
26354 enum tag_types class_key;
26355 tree id = NULL_TREE;
26356 tree type = NULL_TREE;
26357 tree attributes;
26358 tree bases;
26359 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
26360 bool template_id_p = false;
26361 bool qualified_p = false;
26362 bool invalid_nested_name_p = false;
26363 bool invalid_explicit_specialization_p = false;
26364 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26365 tree pushed_scope = NULL_TREE;
26366 unsigned num_templates;
26367 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
26368 /* Assume no nested-name-specifier will be present. */
26369 *nested_name_specifier_p = false;
26370 /* Assume no template parameter lists will be used in defining the
26371 type. */
26372 num_templates = 0;
26373 parser->colon_corrects_to_scope_p = false;
26375 /* Look for the class-key. */
26376 class_key = cp_parser_class_key (parser);
26377 if (class_key == none_type)
26378 return error_mark_node;
26380 location_t class_head_start_location = input_location;
26382 /* Parse the attributes. */
26383 attributes = cp_parser_attributes_opt (parser);
26385 /* If the next token is `::', that is invalid -- but sometimes
26386 people do try to write:
26388 struct ::S {};
26390 Handle this gracefully by accepting the extra qualifier, and then
26391 issuing an error about it later if this really is a
26392 class-head. If it turns out just to be an elaborated type
26393 specifier, remain silent. */
26394 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
26395 qualified_p = true;
26397 push_deferring_access_checks (dk_no_check);
26399 /* Determine the name of the class. Begin by looking for an
26400 optional nested-name-specifier. */
26401 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
26402 nested_name_specifier
26403 = cp_parser_nested_name_specifier_opt (parser,
26404 /*typename_keyword_p=*/false,
26405 /*check_dependency_p=*/false,
26406 /*type_p=*/true,
26407 /*is_declaration=*/false);
26408 /* If there was a nested-name-specifier, then there *must* be an
26409 identifier. */
26411 cp_token *bad_template_keyword = NULL;
26413 if (nested_name_specifier)
26415 type_start_token = cp_lexer_peek_token (parser->lexer);
26416 /* Although the grammar says `identifier', it really means
26417 `class-name' or `template-name'. You are only allowed to
26418 define a class that has already been declared with this
26419 syntax.
26421 The proposed resolution for Core Issue 180 says that wherever
26422 you see `class T::X' you should treat `X' as a type-name.
26424 It is OK to define an inaccessible class; for example:
26426 class A { class B; };
26427 class A::B {};
26429 We do not know if we will see a class-name, or a
26430 template-name. We look for a class-name first, in case the
26431 class-name is a template-id; if we looked for the
26432 template-name first we would stop after the template-name. */
26433 cp_parser_parse_tentatively (parser);
26434 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26435 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
26436 type = cp_parser_class_name (parser,
26437 /*typename_keyword_p=*/false,
26438 /*template_keyword_p=*/false,
26439 class_type,
26440 /*check_dependency_p=*/false,
26441 /*class_head_p=*/true,
26442 /*is_declaration=*/false);
26443 /* If that didn't work, ignore the nested-name-specifier. */
26444 if (!cp_parser_parse_definitely (parser))
26446 invalid_nested_name_p = true;
26447 type_start_token = cp_lexer_peek_token (parser->lexer);
26448 id = cp_parser_identifier (parser);
26449 if (id == error_mark_node)
26450 id = NULL_TREE;
26452 /* If we could not find a corresponding TYPE, treat this
26453 declaration like an unqualified declaration. */
26454 if (type == error_mark_node)
26455 nested_name_specifier = NULL_TREE;
26456 /* Otherwise, count the number of templates used in TYPE and its
26457 containing scopes. */
26458 else
26459 num_templates = num_template_headers_for_class (TREE_TYPE (type));
26461 /* Otherwise, the identifier is optional. */
26462 else
26464 /* We don't know whether what comes next is a template-id,
26465 an identifier, or nothing at all. */
26466 cp_parser_parse_tentatively (parser);
26467 /* Check for a template-id. */
26468 type_start_token = cp_lexer_peek_token (parser->lexer);
26469 id = cp_parser_template_id (parser,
26470 /*template_keyword_p=*/false,
26471 /*check_dependency_p=*/true,
26472 class_key,
26473 /*is_declaration=*/true);
26474 /* If that didn't work, it could still be an identifier. */
26475 if (!cp_parser_parse_definitely (parser))
26477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26479 type_start_token = cp_lexer_peek_token (parser->lexer);
26480 id = cp_parser_identifier (parser);
26482 else
26483 id = NULL_TREE;
26485 else
26487 template_id_p = true;
26488 ++num_templates;
26492 pop_deferring_access_checks ();
26494 if (id)
26496 cp_parser_check_for_invalid_template_id (parser, id,
26497 class_key,
26498 type_start_token->location);
26500 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
26502 /* If it's not a `:' or a `{' then we can't really be looking at a
26503 class-head, since a class-head only appears as part of a
26504 class-specifier. We have to detect this situation before calling
26505 xref_tag, since that has irreversible side-effects. */
26506 if (!cp_parser_next_token_starts_class_definition_p (parser))
26508 cp_parser_error (parser, "expected %<{%> or %<:%>");
26509 type = error_mark_node;
26510 goto out;
26513 /* At this point, we're going ahead with the class-specifier, even
26514 if some other problem occurs. */
26515 cp_parser_commit_to_tentative_parse (parser);
26516 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
26518 cp_parser_error (parser,
26519 "cannot specify %<override%> for a class");
26520 type = error_mark_node;
26521 goto out;
26523 /* Issue the error about the overly-qualified name now. */
26524 if (qualified_p)
26526 cp_parser_error (parser,
26527 "global qualification of class name is invalid");
26528 type = error_mark_node;
26529 goto out;
26531 else if (invalid_nested_name_p)
26533 cp_parser_error (parser,
26534 "qualified name does not name a class");
26535 type = error_mark_node;
26536 goto out;
26538 else if (nested_name_specifier)
26540 tree scope;
26542 if (bad_template_keyword)
26543 /* [temp.names]: in a qualified-id formed by a class-head-name, the
26544 keyword template shall not appear at the top level. */
26545 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
26546 "keyword %<template%> not allowed in class-head-name");
26548 /* Reject typedef-names in class heads. */
26549 if (!DECL_IMPLICIT_TYPEDEF_P (type))
26551 error_at (type_start_token->location,
26552 "invalid class name in declaration of %qD",
26553 type);
26554 type = NULL_TREE;
26555 goto done;
26558 /* Figure out in what scope the declaration is being placed. */
26559 scope = current_scope ();
26560 /* If that scope does not contain the scope in which the
26561 class was originally declared, the program is invalid. */
26562 if (scope && !is_ancestor (scope, nested_name_specifier))
26564 if (at_namespace_scope_p ())
26565 error_at (type_start_token->location,
26566 "declaration of %qD in namespace %qD which does not "
26567 "enclose %qD",
26568 type, scope, nested_name_specifier);
26569 else
26570 error_at (type_start_token->location,
26571 "declaration of %qD in %qD which does not enclose %qD",
26572 type, scope, nested_name_specifier);
26573 type = NULL_TREE;
26574 goto done;
26576 /* [dcl.meaning]
26578 A declarator-id shall not be qualified except for the
26579 definition of a ... nested class outside of its class
26580 ... [or] the definition or explicit instantiation of a
26581 class member of a namespace outside of its namespace. */
26582 if (scope == nested_name_specifier)
26583 permerror (nested_name_specifier_token_start->location,
26584 "extra qualification not allowed");
26586 /* An explicit-specialization must be preceded by "template <>". If
26587 it is not, try to recover gracefully. */
26588 if (at_namespace_scope_p ()
26589 && parser->num_template_parameter_lists == 0
26590 && !processing_template_parmlist
26591 && template_id_p)
26593 /* Build a location of this form:
26594 struct typename <ARGS>
26595 ^~~~~~~~~~~~~~~~~~~~~~
26596 with caret==start at the start token, and
26597 finishing at the end of the type. */
26598 location_t reported_loc
26599 = make_location (class_head_start_location,
26600 class_head_start_location,
26601 get_finish (type_start_token->location));
26602 rich_location richloc (line_table, reported_loc);
26603 richloc.add_fixit_insert_before (class_head_start_location,
26604 "template <> ");
26605 error_at (&richloc,
26606 "an explicit specialization must be preceded by"
26607 " %<template <>%>");
26608 invalid_explicit_specialization_p = true;
26609 /* Take the same action that would have been taken by
26610 cp_parser_explicit_specialization. */
26611 ++parser->num_template_parameter_lists;
26612 begin_specialization ();
26614 /* There must be no "return" statements between this point and the
26615 end of this function; set "type "to the correct return value and
26616 use "goto done;" to return. */
26617 /* Make sure that the right number of template parameters were
26618 present. */
26619 if (!cp_parser_check_template_parameters (parser, num_templates,
26620 template_id_p,
26621 type_start_token->location,
26622 /*declarator=*/NULL))
26624 /* If something went wrong, there is no point in even trying to
26625 process the class-definition. */
26626 type = NULL_TREE;
26627 goto done;
26630 /* Look up the type. */
26631 if (template_id_p)
26633 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
26634 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
26635 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
26637 error_at (type_start_token->location,
26638 "function template %qD redeclared as a class template", id);
26639 type = error_mark_node;
26641 else
26643 type = TREE_TYPE (id);
26644 type = maybe_process_partial_specialization (type);
26646 /* Check the scope while we still know whether or not we had a
26647 nested-name-specifier. */
26648 if (type != error_mark_node)
26649 check_unqualified_spec_or_inst (type, type_start_token->location);
26651 if (nested_name_specifier)
26652 pushed_scope = push_scope (nested_name_specifier);
26654 else if (nested_name_specifier)
26656 type = TREE_TYPE (type);
26658 /* Given:
26660 template <typename T> struct S { struct T };
26661 template <typename T> struct S<T>::T { };
26663 we will get a TYPENAME_TYPE when processing the definition of
26664 `S::T'. We need to resolve it to the actual type before we
26665 try to define it. */
26666 if (TREE_CODE (type) == TYPENAME_TYPE)
26668 type = resolve_typename_type (type, /*only_current_p=*/false);
26669 if (TREE_CODE (type) == TYPENAME_TYPE)
26671 cp_parser_error (parser, "could not resolve typename type");
26672 type = error_mark_node;
26676 type = maybe_process_partial_specialization (type);
26677 if (type == error_mark_node)
26679 type = NULL_TREE;
26680 goto done;
26683 /* Enter the scope indicated by the nested-name-specifier. */
26684 pushed_scope = push_scope (nested_name_specifier);
26685 /* Get the canonical version of this type. */
26686 type = TYPE_MAIN_DECL (type);
26687 /* Call push_template_decl if it seems like we should be defining a
26688 template either from the template headers or the type we're
26689 defining, so that we diagnose both extra and missing headers. */
26690 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
26691 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
26692 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
26694 type = push_template_decl (type);
26695 if (type == error_mark_node)
26697 type = NULL_TREE;
26698 goto done;
26702 type = TREE_TYPE (type);
26703 *nested_name_specifier_p = true;
26705 else /* The name is not a nested name. */
26707 /* If the class was unnamed, create a dummy name. */
26708 if (!id)
26709 id = make_anon_name ();
26710 TAG_how how = (parser->in_type_id_in_expr_p
26711 ? TAG_how::INNERMOST_NON_CLASS
26712 : TAG_how::CURRENT_ONLY);
26713 type = xref_tag (class_key, id, how,
26714 parser->num_template_parameter_lists);
26717 /* Diagnose class/struct/union mismatches. */
26718 cp_parser_check_class_key (parser, UNKNOWN_LOCATION, class_key, type,
26719 true, true);
26721 /* Indicate whether this class was declared as a `class' or as a
26722 `struct'. */
26723 if (TREE_CODE (type) == RECORD_TYPE)
26724 CLASSTYPE_DECLARED_CLASS (type) = class_key == class_type;
26726 /* If this type was already complete, and we see another definition,
26727 that's an error. Likewise if the type is already being defined:
26728 this can happen, eg, when it's defined from within an expression
26729 (c++/84605). */
26730 if (type != error_mark_node
26731 && (COMPLETE_TYPE_P (type) || TYPE_BEING_DEFINED (type)))
26733 error_at (type_start_token->location, "redefinition of %q#T",
26734 type);
26735 inform (location_of (type), "previous definition of %q#T",
26736 type);
26737 type = NULL_TREE;
26738 goto done;
26740 else if (type == error_mark_node)
26741 type = NULL_TREE;
26743 if (type)
26745 if (current_lambda_expr ()
26746 && uses_parameter_packs (attributes))
26748 /* In a lambda this should work, but doesn't currently. */
26749 sorry ("unexpanded parameter pack in local class in lambda");
26750 attributes = NULL_TREE;
26753 /* Apply attributes now, before any use of the class as a template
26754 argument in its base list. */
26755 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
26756 fixup_attribute_variants (type);
26759 /* Associate constraints with the type. */
26760 if (flag_concepts)
26761 type = associate_classtype_constraints (type);
26763 /* We will have entered the scope containing the class; the names of
26764 base classes should be looked up in that context. For example:
26766 struct A { struct B {}; struct C; };
26767 struct A::C : B {};
26769 is valid. */
26771 /* Get the list of base-classes, if there is one. Defer access checking
26772 until the entire list has been seen, as per [class.access.general]. */
26773 push_deferring_access_checks (dk_deferred);
26774 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26776 if (type)
26777 pushclass (type);
26778 bases = cp_parser_base_clause (parser);
26779 if (type)
26780 popclass ();
26782 else
26783 bases = NULL_TREE;
26785 /* If we're really defining a class, process the base classes.
26786 If they're invalid, fail. */
26787 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26788 xref_basetypes (type, bases);
26790 /* Now that all bases have been seen and attached to the class, check
26791 accessibility of the types named in the base-clause. This must be
26792 done relative to the class scope, so that we accept e.g.
26794 struct A { protected: struct B {}; };
26795 struct C : A::B, A {}; // OK: A::B is accessible via base A
26797 as per [class.access.general]. */
26798 if (type)
26799 pushclass (type);
26800 pop_to_parent_deferring_access_checks ();
26801 if (type)
26802 popclass ();
26804 done:
26805 /* Leave the scope given by the nested-name-specifier. We will
26806 enter the class scope itself while processing the members. */
26807 if (pushed_scope)
26808 pop_scope (pushed_scope);
26810 if (invalid_explicit_specialization_p)
26812 end_specialization ();
26813 --parser->num_template_parameter_lists;
26816 if (type)
26817 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
26818 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
26819 CLASSTYPE_FINAL (type) = 1;
26820 out:
26821 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
26822 return type;
26825 /* Parse a class-key.
26827 class-key:
26828 class
26829 struct
26830 union
26832 Returns the kind of class-key specified, or none_type to indicate
26833 error. */
26835 static enum tag_types
26836 cp_parser_class_key (cp_parser* parser)
26838 cp_token *token;
26839 enum tag_types tag_type;
26841 /* Look for the class-key. */
26842 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
26843 if (!token)
26844 return none_type;
26846 /* Check to see if the TOKEN is a class-key. */
26847 tag_type = cp_parser_token_is_class_key (token);
26848 if (!tag_type)
26849 cp_parser_error (parser, "expected class-key");
26850 return tag_type;
26853 /* Parse a type-parameter-key.
26855 type-parameter-key:
26856 class
26857 typename
26860 static void
26861 cp_parser_type_parameter_key (cp_parser* parser)
26863 /* Look for the type-parameter-key. */
26864 enum tag_types tag_type = none_type;
26865 cp_token *token = cp_lexer_peek_token (parser->lexer);
26866 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
26868 cp_lexer_consume_token (parser->lexer);
26869 if (pedantic && tag_type == typename_type
26870 && cxx_dialect < cxx17)
26871 /* typename is not allowed in a template template parameter
26872 by the standard until C++17. */
26873 pedwarn (token->location, OPT_Wc__17_extensions,
26874 "ISO C++ forbids typename key in template template parameter;"
26875 " use %<-std=c++17%> or %<-std=gnu++17%>");
26877 else
26878 cp_parser_error (parser, "expected %<class%> or %<typename%>");
26880 return;
26883 /* Parse an (optional) member-specification.
26885 member-specification:
26886 member-declaration member-specification [opt]
26887 access-specifier : member-specification [opt] */
26889 static void
26890 cp_parser_member_specification_opt (cp_parser* parser)
26892 while (true)
26894 cp_token *token;
26895 enum rid keyword;
26897 /* Peek at the next token. */
26898 token = cp_lexer_peek_token (parser->lexer);
26899 /* If it's a `}', or EOF then we've seen all the members. */
26900 if (token->type == CPP_CLOSE_BRACE
26901 || token->type == CPP_EOF
26902 || token->type == CPP_PRAGMA_EOL)
26903 break;
26905 /* See if this token is a keyword. */
26906 keyword = token->keyword;
26907 switch (keyword)
26909 case RID_PUBLIC:
26910 case RID_PROTECTED:
26911 case RID_PRIVATE:
26912 /* Consume the access-specifier. */
26913 cp_lexer_consume_token (parser->lexer);
26914 /* Remember which access-specifier is active. */
26915 current_access_specifier = token->u.value;
26916 /* Look for the `:'. */
26917 cp_parser_require (parser, CPP_COLON, RT_COLON);
26918 break;
26920 default:
26921 /* Accept #pragmas at class scope. */
26922 if (token->type == CPP_PRAGMA)
26924 cp_parser_pragma (parser, pragma_member, NULL);
26925 break;
26928 /* Otherwise, the next construction must be a
26929 member-declaration. */
26930 cp_parser_member_declaration (parser);
26935 /* Parse a member-declaration.
26937 member-declaration:
26938 decl-specifier-seq [opt] member-declarator-list [opt] ;
26939 function-definition ; [opt]
26940 :: [opt] nested-name-specifier template [opt] unqualified-id ;
26941 using-declaration
26942 template-declaration
26943 alias-declaration
26945 member-declarator-list:
26946 member-declarator
26947 member-declarator-list , member-declarator
26949 member-declarator:
26950 declarator pure-specifier [opt]
26951 declarator constant-initializer [opt]
26952 identifier [opt] : constant-expression
26954 GNU Extensions:
26956 member-declaration:
26957 __extension__ member-declaration
26959 member-declarator:
26960 declarator attributes [opt] pure-specifier [opt]
26961 declarator attributes [opt] constant-initializer [opt]
26962 identifier [opt] attributes [opt] : constant-expression
26964 C++0x Extensions:
26966 member-declaration:
26967 static_assert-declaration */
26969 static void
26970 cp_parser_member_declaration (cp_parser* parser)
26972 cp_decl_specifier_seq decl_specifiers;
26973 tree prefix_attributes;
26974 tree decl;
26975 int declares_class_or_enum;
26976 bool friend_p;
26977 cp_token *token = NULL;
26978 cp_token *decl_spec_token_start = NULL;
26979 cp_token *initializer_token_start = NULL;
26980 int saved_pedantic;
26981 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
26983 /* Check for the `__extension__' keyword. */
26984 if (cp_parser_extension_opt (parser, &saved_pedantic))
26986 /* Recurse. */
26987 cp_parser_member_declaration (parser);
26988 /* Restore the old value of the PEDANTIC flag. */
26989 pedantic = saved_pedantic;
26991 return;
26994 /* Check for a template-declaration. */
26995 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26997 /* An explicit specialization here is an error condition, and we
26998 expect the specialization handler to detect and report this. */
26999 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
27000 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
27001 cp_parser_explicit_specialization (parser);
27002 else
27003 cp_parser_template_declaration (parser, /*member_p=*/true);
27005 return;
27007 /* Check for a template introduction. */
27008 else if (cp_parser_template_declaration_after_export (parser, true))
27009 return;
27011 /* Check for a using-declaration. */
27012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
27014 if (cxx_dialect < cxx11)
27015 /* Parse the using-declaration. */
27016 cp_parser_using_declaration (parser, /*access_declaration_p=*/false);
27017 else if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_ENUM))
27018 cp_parser_using_enum (parser);
27019 else
27021 tree decl;
27022 bool alias_decl_expected;
27023 cp_parser_parse_tentatively (parser);
27024 decl = cp_parser_alias_declaration (parser);
27025 /* Note that if we actually see the '=' token after the
27026 identifier, cp_parser_alias_declaration commits the
27027 tentative parse. In that case, we really expect an
27028 alias-declaration. Otherwise, we expect a using
27029 declaration. */
27030 alias_decl_expected =
27031 !cp_parser_uncommitted_to_tentative_parse_p (parser);
27032 cp_parser_parse_definitely (parser);
27034 if (alias_decl_expected)
27035 finish_member_declaration (decl);
27036 else
27037 cp_parser_using_declaration (parser,
27038 /*access_declaration_p=*/false);
27040 return;
27043 /* Check for @defs. */
27044 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
27046 tree ivar, member;
27047 tree ivar_chains = cp_parser_objc_defs_expression (parser);
27048 ivar = ivar_chains;
27049 while (ivar)
27051 member = ivar;
27052 ivar = TREE_CHAIN (member);
27053 TREE_CHAIN (member) = NULL_TREE;
27054 finish_member_declaration (member);
27056 return;
27059 /* If the next token is `static_assert' we have a static assertion. */
27060 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
27062 cp_parser_static_assert (parser, /*member_p=*/true);
27063 return;
27066 parser->colon_corrects_to_scope_p = false;
27068 cp_omp_declare_simd_data odsd;
27069 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
27070 goto out;
27072 /* Parse the decl-specifier-seq. */
27073 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
27074 cp_parser_decl_specifier_seq (parser,
27075 (CP_PARSER_FLAGS_OPTIONAL
27076 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
27077 &decl_specifiers,
27078 &declares_class_or_enum);
27080 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
27081 cp_parser_handle_directive_omp_attributes (parser,
27082 &decl_specifiers.attributes,
27083 &odsd, true);
27085 /* Check for an invalid type-name. */
27086 if (!decl_specifiers.any_type_specifiers_p
27087 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27088 goto out;
27089 /* If there is no declarator, then the decl-specifier-seq should
27090 specify a type. */
27091 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27093 /* If there was no decl-specifier-seq, and the next token is a
27094 `;', then we have something like:
27096 struct S { ; };
27098 [class.mem]
27100 Each member-declaration shall declare at least one member
27101 name of the class. */
27102 if (!decl_specifiers.any_specifiers_p)
27104 cp_token *token = cp_lexer_peek_token (parser->lexer);
27105 if (!in_system_header_at (token->location))
27107 gcc_rich_location richloc (token->location);
27108 richloc.add_fixit_remove ();
27109 pedwarn (&richloc, OPT_Wpedantic, "extra %<;%>");
27112 else
27114 /* See if this declaration is a friend. */
27115 friend_p = cp_parser_friend_p (&decl_specifiers);
27116 /* If there were decl-specifiers, check to see if there was
27117 a class-declaration. */
27118 tree type = check_tag_decl (&decl_specifiers,
27119 /*explicit_type_instantiation_p=*/false);
27120 /* Nested classes have already been added to the class, but
27121 a `friend' needs to be explicitly registered. */
27122 if (friend_p)
27124 /* If the `friend' keyword was present, the friend must
27125 be introduced with a class-key. */
27126 if (!declares_class_or_enum && cxx_dialect < cxx11)
27127 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
27128 "in C++03 a class-key must be used "
27129 "when declaring a friend");
27130 /* In this case:
27132 template <typename T> struct A {
27133 friend struct A<T>::B;
27136 A<T>::B will be represented by a TYPENAME_TYPE, and
27137 therefore not recognized by check_tag_decl. */
27138 if (!type)
27140 type = decl_specifiers.type;
27141 if (type && TREE_CODE (type) == TYPE_DECL)
27142 type = TREE_TYPE (type);
27144 /* Warn if an attribute cannot appear here, as per
27145 [dcl.attr.grammar]/5. But not when declares_class_or_enum:
27146 we ignore attributes in elaborated-type-specifiers. */
27147 if (!declares_class_or_enum
27148 && cxx11_attribute_p (decl_specifiers.attributes))
27150 decl_specifiers.attributes = NULL_TREE;
27151 if (warning_at (decl_spec_token_start->location,
27152 OPT_Wattributes, "attribute ignored"))
27153 inform (decl_spec_token_start->location, "an attribute "
27154 "that appertains to a friend declaration that "
27155 "is not a definition is ignored");
27157 if (!type || !TYPE_P (type))
27158 error_at (decl_spec_token_start->location,
27159 "friend declaration does not name a class or "
27160 "function");
27161 else
27162 make_friend_class (current_class_type, type,
27163 /*complain=*/true);
27165 /* If there is no TYPE, an error message will already have
27166 been issued. */
27167 else if (!type || type == error_mark_node)
27169 /* An anonymous aggregate has to be handled specially; such
27170 a declaration really declares a data member (with a
27171 particular type), as opposed to a nested class. */
27172 else if (ANON_AGGR_TYPE_P (type))
27174 /* C++11 9.5/6. */
27175 if (decl_specifiers.storage_class != sc_none)
27176 error_at (decl_spec_token_start->location,
27177 "a storage class on an anonymous aggregate "
27178 "in class scope is not allowed");
27180 /* Remove constructors and such from TYPE, now that we
27181 know it is an anonymous aggregate. */
27182 fixup_anonymous_aggr (type);
27183 /* And make the corresponding data member. */
27184 decl = build_decl (decl_spec_token_start->location,
27185 FIELD_DECL, NULL_TREE, type);
27186 /* Add it to the class. */
27187 finish_member_declaration (decl);
27189 else
27190 cp_parser_check_access_in_redeclaration
27191 (TYPE_NAME (type),
27192 decl_spec_token_start->location);
27195 else
27197 bool assume_semicolon = false;
27199 /* Clear attributes from the decl_specifiers but keep them
27200 around as prefix attributes that apply them to the entity
27201 being declared. */
27202 prefix_attributes = decl_specifiers.attributes;
27203 decl_specifiers.attributes = NULL_TREE;
27204 if (parser->omp_declare_simd
27205 && (parser->omp_declare_simd->attribs[0]
27206 == &decl_specifiers.attributes))
27207 parser->omp_declare_simd->attribs[0] = &prefix_attributes;
27209 /* See if these declarations will be friends. */
27210 friend_p = cp_parser_friend_p (&decl_specifiers);
27212 /* Keep going until we hit the `;' at the end of the
27213 declaration. */
27214 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
27216 tree attributes = NULL_TREE;
27217 tree first_attribute;
27218 tree initializer;
27219 bool named_bitfld = false;
27221 /* Peek at the next token. */
27222 token = cp_lexer_peek_token (parser->lexer);
27224 /* The following code wants to know early if it is a bit-field
27225 or some other declaration. Attributes can appear before
27226 the `:' token. Skip over them without consuming any tokens
27227 to peek if they are followed by `:'. */
27228 if (cp_next_tokens_can_be_attribute_p (parser)
27229 || (token->type == CPP_NAME
27230 && cp_nth_tokens_can_be_attribute_p (parser, 2)
27231 && (named_bitfld = true)))
27233 size_t n
27234 = cp_parser_skip_attributes_opt (parser, 1 + named_bitfld);
27235 token = cp_lexer_peek_nth_token (parser->lexer, n);
27238 /* Check for a bitfield declaration. */
27239 if (token->type == CPP_COLON
27240 || (token->type == CPP_NAME
27241 && token == cp_lexer_peek_token (parser->lexer)
27242 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON)
27243 && (named_bitfld = true)))
27245 tree identifier;
27246 tree width;
27247 tree late_attributes = NULL_TREE;
27248 location_t id_location
27249 = cp_lexer_peek_token (parser->lexer)->location;
27251 if (named_bitfld)
27252 identifier = cp_parser_identifier (parser);
27253 else
27254 identifier = NULL_TREE;
27256 /* Look for attributes that apply to the bitfield. */
27257 attributes = cp_parser_attributes_opt (parser);
27259 /* Consume the `:' token. */
27260 cp_lexer_consume_token (parser->lexer);
27262 /* Get the width of the bitfield. */
27263 width = cp_parser_constant_expression (parser, false, NULL,
27264 cxx_dialect >= cxx11);
27266 /* In C++20 and as extension for C++11 and above we allow
27267 default member initializers for bit-fields. */
27268 initializer = NULL_TREE;
27269 if (cxx_dialect >= cxx11
27270 && (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
27271 || cp_lexer_next_token_is (parser->lexer,
27272 CPP_OPEN_BRACE)))
27274 location_t loc
27275 = cp_lexer_peek_token (parser->lexer)->location;
27276 if (cxx_dialect < cxx20
27277 && identifier != NULL_TREE)
27278 pedwarn (loc, OPT_Wc__20_extensions,
27279 "default member initializers for bit-fields "
27280 "only available with %<-std=c++20%> or "
27281 "%<-std=gnu++20%>");
27283 initializer = cp_parser_save_nsdmi (parser);
27284 if (identifier == NULL_TREE)
27286 error_at (loc, "default member initializer for "
27287 "unnamed bit-field");
27288 initializer = NULL_TREE;
27291 else
27293 /* Look for attributes that apply to the bitfield after
27294 the `:' token and width. This is where GCC used to
27295 parse attributes in the past, pedwarn if there is
27296 a std attribute. */
27297 if (cp_next_tokens_can_be_std_attribute_p (parser))
27298 pedwarn (input_location, OPT_Wpedantic,
27299 "ISO C++ allows bit-field attributes only "
27300 "before the %<:%> token");
27302 late_attributes = cp_parser_attributes_opt (parser);
27305 attributes = attr_chainon (attributes, late_attributes);
27307 /* Remember which attributes are prefix attributes and
27308 which are not. */
27309 first_attribute = attributes;
27310 /* Combine the attributes. */
27311 attributes = attr_chainon (prefix_attributes, attributes);
27313 /* Create the bitfield declaration. */
27314 decl = grokbitfield (identifier
27315 ? make_id_declarator (NULL_TREE,
27316 identifier,
27317 sfk_none,
27318 id_location)
27319 : NULL,
27320 &decl_specifiers,
27321 width, initializer,
27322 attributes);
27324 else
27326 cp_declarator *declarator;
27327 tree asm_specification;
27328 int ctor_dtor_or_conv_p;
27329 bool static_p = (decl_specifiers.storage_class == sc_static);
27330 cp_parser_flags flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
27331 /* We can't delay parsing for friends,
27332 alias-declarations, and typedefs, even though the
27333 standard seems to require it. */
27334 if (!friend_p
27335 && !decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
27336 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
27338 /* Parse the declarator. */
27339 declarator
27340 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27341 flags,
27342 &ctor_dtor_or_conv_p,
27343 /*parenthesized_p=*/NULL,
27344 /*member_p=*/true,
27345 friend_p, static_p);
27347 /* If something went wrong parsing the declarator, make sure
27348 that we at least consume some tokens. */
27349 if (declarator == cp_error_declarator)
27351 /* Skip to the end of the statement. */
27352 cp_parser_skip_to_end_of_statement (parser);
27353 /* If the next token is not a semicolon, that is
27354 probably because we just skipped over the body of
27355 a function. So, we consume a semicolon if
27356 present, but do not issue an error message if it
27357 is not present. */
27358 if (cp_lexer_next_token_is (parser->lexer,
27359 CPP_SEMICOLON))
27360 cp_lexer_consume_token (parser->lexer);
27361 goto out;
27364 /* Handle class-scope non-template C++17 deduction guides. */
27365 cp_parser_maybe_adjust_declarator_for_dguide (parser,
27366 &decl_specifiers,
27367 declarator,
27368 &ctor_dtor_or_conv_p);
27370 if (declares_class_or_enum & 2)
27371 cp_parser_check_for_definition_in_return_type
27372 (declarator, decl_specifiers.type,
27373 decl_specifiers.locations[ds_type_spec]);
27375 /* Look for an asm-specification. */
27376 asm_specification = cp_parser_asm_specification_opt (parser);
27377 /* Look for attributes that apply to the declaration. */
27378 attributes = cp_parser_attributes_opt (parser);
27379 /* Remember which attributes are prefix attributes and
27380 which are not. */
27381 first_attribute = attributes;
27382 /* Combine the attributes. */
27383 attributes = attr_chainon (prefix_attributes, attributes);
27385 /* If it's an `=', then we have a constant-initializer or a
27386 pure-specifier. It is not correct to parse the
27387 initializer before registering the member declaration
27388 since the member declaration should be in scope while
27389 its initializer is processed. However, the rest of the
27390 front end does not yet provide an interface that allows
27391 us to handle this correctly. */
27392 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
27394 /* In [class.mem]:
27396 A pure-specifier shall be used only in the declaration of
27397 a virtual function.
27399 A member-declarator can contain a constant-initializer
27400 only if it declares a static member of integral or
27401 enumeration type.
27403 Therefore, if the DECLARATOR is for a function, we look
27404 for a pure-specifier; otherwise, we look for a
27405 constant-initializer. When we call `grokfield', it will
27406 perform more stringent semantics checks. */
27407 initializer_token_start = cp_lexer_peek_token (parser->lexer);
27408 declarator->init_loc = initializer_token_start->location;
27409 if (function_declarator_p (declarator)
27410 || (decl_specifiers.type
27411 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
27412 && declarator->kind == cdk_id
27413 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
27414 == FUNCTION_TYPE)))
27415 initializer = cp_parser_pure_specifier (parser);
27416 else if (decl_specifiers.storage_class != sc_static)
27417 initializer = cp_parser_save_nsdmi (parser);
27418 else if (cxx_dialect >= cxx11)
27420 bool nonconst;
27421 /* Don't require a constant rvalue in C++11, since we
27422 might want a reference constant. We'll enforce
27423 constancy later. */
27424 cp_lexer_consume_token (parser->lexer);
27425 /* Parse the initializer. */
27426 initializer = cp_parser_initializer_clause (parser,
27427 &nonconst);
27429 else
27430 /* Parse the initializer. */
27431 initializer = cp_parser_constant_initializer (parser);
27433 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
27434 && !function_declarator_p (declarator))
27436 bool x;
27437 declarator->init_loc
27438 = cp_lexer_peek_token (parser->lexer)->location;
27439 if (decl_specifiers.storage_class != sc_static)
27440 initializer = cp_parser_save_nsdmi (parser);
27441 else
27442 initializer = cp_parser_initializer (parser, &x, &x);
27444 /* Detect invalid bit-field cases such as
27446 int *p : 4;
27447 int &&r : 3;
27449 and similar. */
27450 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
27451 /* If there were no type specifiers, it was a
27452 constructor. */
27453 && decl_specifiers.any_type_specifiers_p)
27455 /* This is called for a decent diagnostic only. */
27456 tree d = grokdeclarator (declarator, &decl_specifiers,
27457 BITFIELD, /*initialized=*/false,
27458 &attributes);
27459 if (!error_operand_p (d))
27460 error_at (DECL_SOURCE_LOCATION (d),
27461 "bit-field %qD has non-integral type %qT",
27462 d, TREE_TYPE (d));
27463 cp_parser_skip_to_end_of_statement (parser);
27464 /* Avoid "extra ;" pedwarns. */
27465 if (cp_lexer_next_token_is (parser->lexer,
27466 CPP_SEMICOLON))
27467 cp_lexer_consume_token (parser->lexer);
27468 goto out;
27470 /* Otherwise, there is no initializer. */
27471 else
27472 initializer = NULL_TREE;
27474 /* See if we are probably looking at a function
27475 definition. We are certainly not looking at a
27476 member-declarator. Calling `grokfield' has
27477 side-effects, so we must not do it unless we are sure
27478 that we are looking at a member-declarator. */
27479 if (cp_parser_token_starts_function_definition_p
27480 (cp_lexer_peek_token (parser->lexer)))
27482 /* The grammar does not allow a pure-specifier to be
27483 used when a member function is defined. (It is
27484 possible that this fact is an oversight in the
27485 standard, since a pure function may be defined
27486 outside of the class-specifier. */
27487 if (initializer && initializer_token_start)
27488 error_at (initializer_token_start->location,
27489 "pure-specifier on function-definition");
27490 decl = cp_parser_save_member_function_body (parser,
27491 &decl_specifiers,
27492 declarator,
27493 attributes);
27495 if (parser->fully_implicit_function_template_p)
27496 decl = finish_fully_implicit_template (parser, decl);
27497 /* If the member was not a friend, declare it here. */
27498 if (!friend_p)
27499 finish_member_declaration (decl);
27500 /* Peek at the next token. */
27501 token = cp_lexer_peek_token (parser->lexer);
27502 /* If the next token is a semicolon, consume it. */
27503 if (token->type == CPP_SEMICOLON)
27505 location_t semicolon_loc
27506 = cp_lexer_consume_token (parser->lexer)->location;
27507 gcc_rich_location richloc (semicolon_loc);
27508 richloc.add_fixit_remove ();
27509 warning_at (&richloc, OPT_Wextra_semi,
27510 "extra %<;%> after in-class "
27511 "function definition");
27513 goto out;
27515 else
27516 if (declarator->kind == cdk_function)
27517 declarator->id_loc = token->location;
27519 /* Create the declaration. */
27520 decl = grokfield (declarator, &decl_specifiers,
27521 initializer, /*init_const_expr_p=*/true,
27522 asm_specification, attributes);
27523 if (parser->fully_implicit_function_template_p)
27525 if (friend_p)
27526 finish_fully_implicit_template (parser, 0);
27527 else
27528 decl = finish_fully_implicit_template (parser, decl);
27532 cp_finalize_omp_declare_simd (parser, decl);
27533 cp_finalize_oacc_routine (parser, decl, false);
27535 /* Reset PREFIX_ATTRIBUTES. */
27536 if (attributes != error_mark_node)
27538 while (attributes && TREE_CHAIN (attributes) != first_attribute)
27539 attributes = TREE_CHAIN (attributes);
27540 if (attributes)
27541 TREE_CHAIN (attributes) = NULL_TREE;
27544 /* If there is any qualification still in effect, clear it
27545 now; we will be starting fresh with the next declarator. */
27546 parser->scope = NULL_TREE;
27547 parser->qualifying_scope = NULL_TREE;
27548 parser->object_scope = NULL_TREE;
27549 /* If it's a `,', then there are more declarators. */
27550 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
27552 cp_lexer_consume_token (parser->lexer);
27553 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
27555 cp_token *token = cp_lexer_previous_token (parser->lexer);
27556 gcc_rich_location richloc (token->location);
27557 richloc.add_fixit_remove ();
27558 error_at (&richloc, "stray %<,%> at end of "
27559 "member declaration");
27562 /* If the next token isn't a `;', then we have a parse error. */
27563 else if (cp_lexer_next_token_is_not (parser->lexer,
27564 CPP_SEMICOLON))
27566 /* The next token might be a ways away from where the
27567 actual semicolon is missing. Find the previous token
27568 and use that for our error position. */
27569 cp_token *token = cp_lexer_previous_token (parser->lexer);
27570 gcc_rich_location richloc (token->location);
27571 richloc.add_fixit_insert_after (";");
27572 error_at (&richloc, "expected %<;%> at end of "
27573 "member declaration");
27575 /* Assume that the user meant to provide a semicolon. If
27576 we were to cp_parser_skip_to_end_of_statement, we might
27577 skip to a semicolon inside a member function definition
27578 and issue nonsensical error messages. */
27579 assume_semicolon = true;
27582 if (decl)
27584 /* Add DECL to the list of members. */
27585 if (!friend_p
27586 /* Explicitly include, eg, NSDMIs, for better error
27587 recovery (c++/58650). */
27588 || !DECL_DECLARES_FUNCTION_P (decl))
27589 finish_member_declaration (decl);
27591 if (DECL_DECLARES_FUNCTION_P (decl))
27592 cp_parser_save_default_args (parser, STRIP_TEMPLATE (decl));
27593 else if (TREE_CODE (decl) == FIELD_DECL
27594 && DECL_INITIAL (decl))
27595 /* Add DECL to the queue of NSDMI to be parsed later. */
27596 vec_safe_push (unparsed_nsdmis, decl);
27599 if (assume_semicolon)
27600 goto out;
27604 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27605 out:
27606 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
27607 cp_finalize_omp_declare_simd (parser, &odsd);
27610 /* Parse a pure-specifier.
27612 pure-specifier:
27615 Returns INTEGER_ZERO_NODE if a pure specifier is found.
27616 Otherwise, ERROR_MARK_NODE is returned. */
27618 static tree
27619 cp_parser_pure_specifier (cp_parser* parser)
27621 cp_token *token;
27623 /* Look for the `=' token. */
27624 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27625 return error_mark_node;
27626 /* Look for the `0' token. */
27627 token = cp_lexer_peek_token (parser->lexer);
27629 if (token->type == CPP_EOF
27630 || token->type == CPP_PRAGMA_EOL)
27631 return error_mark_node;
27633 cp_lexer_consume_token (parser->lexer);
27635 /* Accept = default or = delete in c++0x mode. */
27636 if (token->keyword == RID_DEFAULT
27637 || token->keyword == RID_DELETE)
27639 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
27640 return token->u.value;
27643 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
27644 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
27646 cp_parser_error (parser,
27647 "invalid pure specifier (only %<= 0%> is allowed)");
27648 cp_parser_skip_to_end_of_statement (parser);
27649 return error_mark_node;
27651 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
27653 error_at (token->location, "templates may not be %<virtual%>");
27654 return error_mark_node;
27657 return integer_zero_node;
27660 /* Parse a constant-initializer.
27662 constant-initializer:
27663 = constant-expression
27665 Returns a representation of the constant-expression. */
27667 static tree
27668 cp_parser_constant_initializer (cp_parser* parser)
27670 /* Look for the `=' token. */
27671 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
27672 return error_mark_node;
27674 /* It is invalid to write:
27676 struct S { static const int i = { 7 }; };
27679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27681 cp_parser_error (parser,
27682 "a brace-enclosed initializer is not allowed here");
27683 /* Consume the opening brace. */
27684 matching_braces braces;
27685 braces.consume_open (parser);
27686 /* Skip the initializer. */
27687 cp_parser_skip_to_closing_brace (parser);
27688 /* Look for the trailing `}'. */
27689 braces.require_close (parser);
27691 return error_mark_node;
27694 return cp_parser_constant_expression (parser);
27697 /* Derived classes [gram.class.derived] */
27699 /* Parse a base-clause.
27701 base-clause:
27702 : base-specifier-list
27704 base-specifier-list:
27705 base-specifier ... [opt]
27706 base-specifier-list , base-specifier ... [opt]
27708 Returns a TREE_LIST representing the base-classes, in the order in
27709 which they were declared. The representation of each node is as
27710 described by cp_parser_base_specifier.
27712 In the case that no bases are specified, this function will return
27713 NULL_TREE, not ERROR_MARK_NODE. */
27715 static tree
27716 cp_parser_base_clause (cp_parser* parser)
27718 tree bases = NULL_TREE;
27720 /* Look for the `:' that begins the list. */
27721 cp_parser_require (parser, CPP_COLON, RT_COLON);
27723 /* Scan the base-specifier-list. */
27724 while (true)
27726 cp_token *token;
27727 tree base;
27728 bool pack_expansion_p = false;
27730 /* Look for the base-specifier. */
27731 base = cp_parser_base_specifier (parser);
27732 /* Look for the (optional) ellipsis. */
27733 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27735 /* Consume the `...'. */
27736 cp_lexer_consume_token (parser->lexer);
27738 pack_expansion_p = true;
27741 /* Add BASE to the front of the list. */
27742 if (base && base != error_mark_node)
27744 if (pack_expansion_p)
27745 /* Make this a pack expansion type. */
27746 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
27748 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
27750 TREE_CHAIN (base) = bases;
27751 bases = base;
27754 /* Peek at the next token. */
27755 token = cp_lexer_peek_token (parser->lexer);
27756 /* If it's not a comma, then the list is complete. */
27757 if (token->type != CPP_COMMA)
27758 break;
27759 /* Consume the `,'. */
27760 cp_lexer_consume_token (parser->lexer);
27763 /* PARSER->SCOPE may still be non-NULL at this point, if the last
27764 base class had a qualified name. However, the next name that
27765 appears is certainly not qualified. */
27766 parser->scope = NULL_TREE;
27767 parser->qualifying_scope = NULL_TREE;
27768 parser->object_scope = NULL_TREE;
27770 return nreverse (bases);
27773 /* Parse a base-specifier.
27775 base-specifier:
27776 :: [opt] nested-name-specifier [opt] class-name
27777 virtual access-specifier [opt] :: [opt] nested-name-specifier
27778 [opt] class-name
27779 access-specifier virtual [opt] :: [opt] nested-name-specifier
27780 [opt] class-name
27782 Returns a TREE_LIST. The TREE_PURPOSE will be one of
27783 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
27784 indicate the specifiers provided. The TREE_VALUE will be a TYPE
27785 (or the ERROR_MARK_NODE) indicating the type that was specified. */
27787 static tree
27788 cp_parser_base_specifier (cp_parser* parser)
27790 cp_token *token;
27791 bool done = false;
27792 bool virtual_p = false;
27793 bool duplicate_virtual_error_issued_p = false;
27794 bool duplicate_access_error_issued_p = false;
27795 bool class_scope_p, template_p;
27796 tree access = access_default_node;
27797 tree type;
27799 /* Process the optional `virtual' and `access-specifier'. */
27800 while (!done)
27802 /* Peek at the next token. */
27803 token = cp_lexer_peek_token (parser->lexer);
27804 /* Process `virtual'. */
27805 switch (token->keyword)
27807 case RID_VIRTUAL:
27808 /* If `virtual' appears more than once, issue an error. */
27809 if (virtual_p && !duplicate_virtual_error_issued_p)
27811 cp_parser_error (parser,
27812 "%<virtual%> specified more than once in base-specifier");
27813 duplicate_virtual_error_issued_p = true;
27816 virtual_p = true;
27818 /* Consume the `virtual' token. */
27819 cp_lexer_consume_token (parser->lexer);
27821 break;
27823 case RID_PUBLIC:
27824 case RID_PROTECTED:
27825 case RID_PRIVATE:
27826 /* If more than one access specifier appears, issue an
27827 error. */
27828 if (access != access_default_node
27829 && !duplicate_access_error_issued_p)
27831 cp_parser_error (parser,
27832 "more than one access specifier in base-specifier");
27833 duplicate_access_error_issued_p = true;
27836 access = ridpointers[(int) token->keyword];
27838 /* Consume the access-specifier. */
27839 cp_lexer_consume_token (parser->lexer);
27841 break;
27843 default:
27844 done = true;
27845 break;
27848 /* It is not uncommon to see programs mechanically, erroneously, use
27849 the 'typename' keyword to denote (dependent) qualified types
27850 as base classes. */
27851 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
27853 token = cp_lexer_peek_token (parser->lexer);
27854 if (!processing_template_decl)
27855 error_at (token->location,
27856 "keyword %<typename%> not allowed outside of templates");
27857 else
27858 error_at (token->location,
27859 "keyword %<typename%> not allowed in this context "
27860 "(the base class is implicitly a type)");
27861 cp_lexer_consume_token (parser->lexer);
27864 /* Look for the optional `::' operator. */
27865 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
27866 /* Look for the nested-name-specifier. The simplest way to
27867 implement:
27869 [temp.res]
27871 The keyword `typename' is not permitted in a base-specifier or
27872 mem-initializer; in these contexts a qualified name that
27873 depends on a template-parameter is implicitly assumed to be a
27874 type name.
27876 is to pretend that we have seen the `typename' keyword at this
27877 point. */
27878 cp_parser_nested_name_specifier_opt (parser,
27879 /*typename_keyword_p=*/true,
27880 /*check_dependency_p=*/true,
27881 /*type_p=*/true,
27882 /*is_declaration=*/true);
27883 /* If the base class is given by a qualified name, assume that names
27884 we see are type names or templates, as appropriate. */
27885 class_scope_p = (parser->scope && TYPE_P (parser->scope));
27886 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
27888 if (!parser->scope
27889 && cp_lexer_next_token_is_decltype (parser->lexer))
27890 /* DR 950 allows decltype as a base-specifier. */
27891 type = cp_parser_decltype (parser);
27892 else
27894 /* Otherwise, look for the class-name. */
27895 type = cp_parser_class_name (parser,
27896 class_scope_p,
27897 template_p,
27898 typename_type,
27899 /*check_dependency_p=*/true,
27900 /*class_head_p=*/false,
27901 /*is_declaration=*/true);
27902 type = TREE_TYPE (type);
27905 if (type == error_mark_node)
27906 return error_mark_node;
27908 return finish_base_specifier (type, access, virtual_p);
27911 /* Exception handling [gram.exception] */
27913 /* Save the tokens that make up the noexcept-specifier for a member-function.
27914 Returns a DEFERRED_PARSE. */
27916 static tree
27917 cp_parser_save_noexcept (cp_parser *parser)
27919 cp_token *first = parser->lexer->next_token;
27920 /* We want everything up to, including, the final ')'. */
27921 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0);
27922 cp_token *last = parser->lexer->next_token;
27924 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
27925 to carry the information we will need. */
27926 tree expr = make_node (DEFERRED_PARSE);
27927 /* Save away the noexcept-specifier; we will process it when the
27928 class is complete. */
27929 DEFPARSE_TOKENS (expr) = cp_token_cache_new (first, last);
27930 DEFPARSE_INSTANTIATIONS (expr) = nullptr;
27931 expr = build_tree_list (expr, NULL_TREE);
27932 return expr;
27935 /* Used for late processing of noexcept-specifiers of member-functions.
27936 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
27937 we saved for later; parse it now. DECL is the declaration of the
27938 member function. */
27940 static tree
27941 cp_parser_late_noexcept_specifier (cp_parser *parser, tree default_arg)
27943 /* Make sure we've gotten something that hasn't been parsed yet. */
27944 gcc_assert (TREE_CODE (default_arg) == DEFERRED_PARSE);
27946 push_unparsed_function_queues (parser);
27948 /* Push the saved tokens for the noexcept-specifier onto the parser's
27949 lexer stack. */
27950 cp_token_cache *tokens = DEFPARSE_TOKENS (default_arg);
27951 cp_parser_push_lexer_for_tokens (parser, tokens);
27953 /* Parse the cached noexcept-specifier. */
27954 tree parsed_arg
27955 = cp_parser_noexcept_specification_opt (parser,
27956 CP_PARSER_FLAGS_NONE,
27957 /*require_constexpr=*/true,
27958 /*consumed_expr=*/NULL,
27959 /*return_cond=*/false);
27961 /* Revert to the main lexer. */
27962 cp_parser_pop_lexer (parser);
27964 /* Restore the queue. */
27965 pop_unparsed_function_queues (parser);
27967 /* And we're done. */
27968 return parsed_arg;
27971 /* Perform late checking of overriding function with respect to their
27972 noexcept-specifiers. TYPE is the class and FNDECL is the function
27973 that potentially overrides some virtual function with the same
27974 signature. */
27976 static void
27977 noexcept_override_late_checks (tree type, tree fndecl)
27979 tree binfo = TYPE_BINFO (type);
27980 tree base_binfo;
27982 if (DECL_STATIC_FUNCTION_P (fndecl))
27983 return;
27985 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
27987 tree basetype = BINFO_TYPE (base_binfo);
27989 if (!TYPE_POLYMORPHIC_P (basetype))
27990 continue;
27992 tree fn = look_for_overrides_here (basetype, fndecl);
27993 if (fn)
27994 maybe_check_overriding_exception_spec (fndecl, fn);
27998 /* Parse an (optional) noexcept-specification.
28000 noexcept-specification:
28001 noexcept ( constant-expression ) [opt]
28003 If no noexcept-specification is present, returns NULL_TREE.
28004 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
28005 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
28006 there are no parentheses. CONSUMED_EXPR will be set accordingly.
28007 Otherwise, returns a noexcept specification unless RETURN_COND is true,
28008 in which case a boolean condition is returned instead. The parser flags
28009 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
28010 the (member) function is `const'. */
28012 static tree
28013 cp_parser_noexcept_specification_opt (cp_parser* parser,
28014 cp_parser_flags flags,
28015 bool require_constexpr,
28016 bool* consumed_expr,
28017 bool return_cond)
28019 cp_token *token;
28020 const char *saved_message;
28022 /* Peek at the next token. */
28023 token = cp_lexer_peek_token (parser->lexer);
28025 /* Is it a noexcept-specification? */
28026 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
28028 tree expr;
28030 /* [class.mem]/6 says that a noexcept-specifer (within the
28031 member-specification of the class) is a complete-class context of
28032 a class. So, if the noexcept-specifier has the optional expression,
28033 just save the tokens, and reparse this after we're done with the
28034 class. */
28036 if ((flags & CP_PARSER_FLAGS_DELAY_NOEXCEPT)
28037 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN)
28038 /* No need to delay parsing for a number literal or true/false. */
28039 && !((cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
28040 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
28041 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
28042 && at_class_scope_p ()
28043 && TYPE_BEING_DEFINED (current_class_type)
28044 && !LAMBDA_TYPE_P (current_class_type))
28045 return cp_parser_save_noexcept (parser);
28047 cp_lexer_consume_token (parser->lexer);
28049 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
28051 matching_parens parens;
28052 parens.consume_open (parser);
28054 if (require_constexpr)
28056 /* Types may not be defined in an exception-specification. */
28057 saved_message = parser->type_definition_forbidden_message;
28058 parser->type_definition_forbidden_message
28059 = G_("types may not be defined in an exception-specification");
28061 bool non_constant_p;
28062 expr
28063 = cp_parser_constant_expression (parser,
28064 /*allow_non_constant=*/true,
28065 &non_constant_p);
28066 if (non_constant_p
28067 && !require_potential_rvalue_constant_expression (expr))
28069 expr = NULL_TREE;
28070 return_cond = true;
28073 /* Restore the saved message. */
28074 parser->type_definition_forbidden_message = saved_message;
28076 else
28078 expr = cp_parser_expression (parser);
28079 *consumed_expr = true;
28082 parens.require_close (parser);
28084 else
28086 expr = boolean_true_node;
28087 if (!require_constexpr)
28088 *consumed_expr = false;
28091 /* We cannot build a noexcept-spec right away because this will check
28092 that expr is a constexpr. */
28093 if (!return_cond)
28094 return build_noexcept_spec (expr, tf_warning_or_error);
28095 else
28096 return expr;
28098 else
28099 return NULL_TREE;
28102 /* Parse an (optional) exception-specification.
28104 exception-specification:
28105 throw ( type-id-list [opt] )
28107 Returns a TREE_LIST representing the exception-specification. The
28108 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
28109 control parsing. QUALS are qualifiers indicating whether the (member)
28110 function is `const'. */
28112 static tree
28113 cp_parser_exception_specification_opt (cp_parser* parser,
28114 cp_parser_flags flags)
28116 cp_token *token;
28117 tree type_id_list;
28118 const char *saved_message;
28120 /* Peek at the next token. */
28121 token = cp_lexer_peek_token (parser->lexer);
28123 /* Is it a noexcept-specification? */
28124 type_id_list
28125 = cp_parser_noexcept_specification_opt (parser, flags,
28126 /*require_constexpr=*/true,
28127 /*consumed_expr=*/NULL,
28128 /*return_cond=*/false);
28129 if (type_id_list != NULL_TREE)
28130 return type_id_list;
28132 /* If it's not `throw', then there's no exception-specification. */
28133 if (!cp_parser_is_keyword (token, RID_THROW))
28134 return NULL_TREE;
28136 location_t loc = token->location;
28138 /* Consume the `throw'. */
28139 cp_lexer_consume_token (parser->lexer);
28141 /* Look for the `('. */
28142 matching_parens parens;
28143 parens.require_open (parser);
28145 /* Peek at the next token. */
28146 token = cp_lexer_peek_token (parser->lexer);
28147 /* If it's not a `)', then there is a type-id-list. */
28148 if (token->type != CPP_CLOSE_PAREN)
28150 /* Types may not be defined in an exception-specification. */
28151 saved_message = parser->type_definition_forbidden_message;
28152 parser->type_definition_forbidden_message
28153 = G_("types may not be defined in an exception-specification");
28154 /* Parse the type-id-list. */
28155 type_id_list = cp_parser_type_id_list (parser);
28156 /* Restore the saved message. */
28157 parser->type_definition_forbidden_message = saved_message;
28159 if (cxx_dialect >= cxx17)
28161 error_at (loc, "ISO C++17 does not allow dynamic exception "
28162 "specifications");
28163 type_id_list = NULL_TREE;
28165 else if (cxx_dialect >= cxx11)
28166 warning_at (loc, OPT_Wdeprecated,
28167 "dynamic exception specifications are deprecated in "
28168 "C++11");
28170 /* In C++17, throw() is equivalent to noexcept (true). throw()
28171 is deprecated in C++11 and above as well, but is still widely used,
28172 so don't warn about it yet. */
28173 else if (cxx_dialect >= cxx17)
28174 type_id_list = noexcept_true_spec;
28175 else
28176 type_id_list = empty_except_spec;
28178 /* Look for the `)'. */
28179 parens.require_close (parser);
28181 return type_id_list;
28184 /* Parse an (optional) type-id-list.
28186 type-id-list:
28187 type-id ... [opt]
28188 type-id-list , type-id ... [opt]
28190 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
28191 in the order that the types were presented. */
28193 static tree
28194 cp_parser_type_id_list (cp_parser* parser)
28196 tree types = NULL_TREE;
28198 while (true)
28200 cp_token *token;
28201 tree type;
28203 token = cp_lexer_peek_token (parser->lexer);
28205 /* Get the next type-id. */
28206 type = cp_parser_type_id (parser);
28207 /* Check for invalid 'auto'. */
28208 if (flag_concepts && type_uses_auto (type))
28210 error_at (token->location,
28211 "invalid use of %<auto%> in exception-specification");
28212 type = error_mark_node;
28214 /* Parse the optional ellipsis. */
28215 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28217 /* Consume the `...'. */
28218 cp_lexer_consume_token (parser->lexer);
28220 /* Turn the type into a pack expansion expression. */
28221 type = make_pack_expansion (type);
28223 /* Add it to the list. */
28224 types = add_exception_specifier (types, type, /*complain=*/1);
28225 /* Peek at the next token. */
28226 token = cp_lexer_peek_token (parser->lexer);
28227 /* If it is not a `,', we are done. */
28228 if (token->type != CPP_COMMA)
28229 break;
28230 /* Consume the `,'. */
28231 cp_lexer_consume_token (parser->lexer);
28234 return nreverse (types);
28237 /* Parse a try-block.
28239 try-block:
28240 try compound-statement handler-seq */
28242 static tree
28243 cp_parser_try_block (cp_parser* parser)
28245 tree try_block;
28247 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
28248 if (parser->in_function_body
28249 && DECL_DECLARED_CONSTEXPR_P (current_function_decl)
28250 && cxx_dialect < cxx20)
28251 pedwarn (input_location, OPT_Wc__20_extensions,
28252 "%<try%> in %<constexpr%> function only "
28253 "available with %<-std=c++20%> or %<-std=gnu++20%>");
28255 try_block = begin_try_block ();
28256 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
28257 finish_try_block (try_block);
28258 cp_parser_handler_seq (parser);
28259 finish_handler_sequence (try_block);
28261 return try_block;
28264 /* Parse a function-try-block.
28266 function-try-block:
28267 try ctor-initializer [opt] function-body handler-seq */
28269 static void
28270 cp_parser_function_try_block (cp_parser* parser)
28272 tree compound_stmt;
28273 tree try_block;
28275 /* Look for the `try' keyword. */
28276 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
28277 return;
28278 /* Let the rest of the front end know where we are. */
28279 try_block = begin_function_try_block (&compound_stmt);
28280 /* Parse the function-body. */
28281 cp_parser_ctor_initializer_opt_and_function_body
28282 (parser, /*in_function_try_block=*/true);
28283 /* We're done with the `try' part. */
28284 finish_function_try_block (try_block);
28285 /* Parse the handlers. */
28286 cp_parser_handler_seq (parser);
28287 /* We're done with the handlers. */
28288 finish_function_handler_sequence (try_block, compound_stmt);
28291 /* Parse a handler-seq.
28293 handler-seq:
28294 handler handler-seq [opt] */
28296 static void
28297 cp_parser_handler_seq (cp_parser* parser)
28299 while (true)
28301 cp_token *token;
28303 /* Parse the handler. */
28304 cp_parser_handler (parser);
28305 /* Peek at the next token. */
28306 token = cp_lexer_peek_token (parser->lexer);
28307 /* If it's not `catch' then there are no more handlers. */
28308 if (!cp_parser_is_keyword (token, RID_CATCH))
28309 break;
28313 /* Parse a handler.
28315 handler:
28316 catch ( exception-declaration ) compound-statement */
28318 static void
28319 cp_parser_handler (cp_parser* parser)
28321 tree handler;
28322 tree declaration;
28324 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
28325 handler = begin_handler ();
28326 matching_parens parens;
28327 parens.require_open (parser);
28328 declaration = cp_parser_exception_declaration (parser);
28329 finish_handler_parms (declaration, handler);
28330 parens.require_close (parser);
28331 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
28332 finish_handler (handler);
28335 /* Parse an exception-declaration.
28337 exception-declaration:
28338 type-specifier-seq declarator
28339 type-specifier-seq abstract-declarator
28340 type-specifier-seq
28343 Returns a VAR_DECL for the declaration, or NULL_TREE if the
28344 ellipsis variant is used. */
28346 static tree
28347 cp_parser_exception_declaration (cp_parser* parser)
28349 cp_decl_specifier_seq type_specifiers;
28350 cp_declarator *declarator;
28351 const char *saved_message;
28353 /* If it's an ellipsis, it's easy to handle. */
28354 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
28356 /* Consume the `...' token. */
28357 cp_lexer_consume_token (parser->lexer);
28358 return NULL_TREE;
28361 /* Types may not be defined in exception-declarations. */
28362 saved_message = parser->type_definition_forbidden_message;
28363 parser->type_definition_forbidden_message
28364 = G_("types may not be defined in exception-declarations");
28366 /* Parse the type-specifier-seq. */
28367 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
28368 /*is_declaration=*/true,
28369 /*is_trailing_return=*/false,
28370 &type_specifiers);
28371 /* If it's a `)', then there is no declarator. */
28372 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28373 declarator = NULL;
28374 else
28375 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
28376 CP_PARSER_FLAGS_NONE,
28377 /*ctor_dtor_or_conv_p=*/NULL,
28378 /*parenthesized_p=*/NULL,
28379 /*member_p=*/false,
28380 /*friend_p=*/false,
28381 /*static_p=*/false);
28383 /* Restore the saved message. */
28384 parser->type_definition_forbidden_message = saved_message;
28386 if (!type_specifiers.any_specifiers_p)
28387 return error_mark_node;
28389 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
28392 /* Parse a throw-expression.
28394 throw-expression:
28395 throw assignment-expression [opt]
28397 Returns a THROW_EXPR representing the throw-expression. */
28399 static tree
28400 cp_parser_throw_expression (cp_parser* parser)
28402 tree expression;
28403 cp_token* token;
28404 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28406 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
28407 token = cp_lexer_peek_token (parser->lexer);
28408 /* Figure out whether or not there is an assignment-expression
28409 following the "throw" keyword. */
28410 if (token->type == CPP_COMMA
28411 || token->type == CPP_SEMICOLON
28412 || token->type == CPP_CLOSE_PAREN
28413 || token->type == CPP_CLOSE_SQUARE
28414 || token->type == CPP_CLOSE_BRACE
28415 || token->type == CPP_COLON)
28416 expression = NULL_TREE;
28417 else
28418 expression = cp_parser_assignment_expression (parser);
28420 /* Construct a location e.g.:
28421 throw x
28422 ^~~~~~~
28423 with caret == start at the start of the "throw" token, and
28424 the end at the end of the final token we consumed. */
28425 location_t combined_loc = make_location (start_loc, start_loc,
28426 parser->lexer);
28427 expression = build_throw (combined_loc, expression);
28429 return expression;
28432 /* Parse a yield-expression.
28434 yield-expression:
28435 co_yield assignment-expression
28436 co_yield braced-init-list
28438 Returns a CO_YIELD_EXPR representing the yield-expression. */
28440 static tree
28441 cp_parser_yield_expression (cp_parser* parser)
28443 tree expr;
28445 cp_token *token = cp_lexer_peek_token (parser->lexer);
28446 location_t kw_loc = token->location; /* Save for later. */
28448 cp_parser_require_keyword (parser, RID_CO_YIELD, RT_CO_YIELD);
28450 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28452 bool expr_non_constant_p;
28453 cp_lexer_set_source_position (parser->lexer);
28454 /* ??? : probably a moot point? */
28455 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
28456 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
28458 else
28459 expr = cp_parser_assignment_expression (parser);
28461 if (expr == error_mark_node)
28462 return expr;
28464 return finish_co_yield_expr (kw_loc, expr);
28467 /* GNU Extensions */
28469 /* Parse an (optional) asm-specification.
28471 asm-specification:
28472 asm ( string-literal )
28474 If the asm-specification is present, returns a STRING_CST
28475 corresponding to the string-literal. Otherwise, returns
28476 NULL_TREE. */
28478 static tree
28479 cp_parser_asm_specification_opt (cp_parser* parser)
28481 cp_token *token;
28482 tree asm_specification;
28484 /* Peek at the next token. */
28485 token = cp_lexer_peek_token (parser->lexer);
28486 /* If the next token isn't the `asm' keyword, then there's no
28487 asm-specification. */
28488 if (!cp_parser_is_keyword (token, RID_ASM))
28489 return NULL_TREE;
28491 /* Consume the `asm' token. */
28492 cp_lexer_consume_token (parser->lexer);
28493 /* Look for the `('. */
28494 matching_parens parens;
28495 parens.require_open (parser);
28497 /* Look for the string-literal. */
28498 asm_specification = cp_parser_string_literal (parser, false, false);
28500 /* Look for the `)'. */
28501 parens.require_close (parser);
28503 return asm_specification;
28506 /* Parse an asm-operand-list.
28508 asm-operand-list:
28509 asm-operand
28510 asm-operand-list , asm-operand
28512 asm-operand:
28513 string-literal ( expression )
28514 [ string-literal ] string-literal ( expression )
28516 Returns a TREE_LIST representing the operands. The TREE_VALUE of
28517 each node is the expression. The TREE_PURPOSE is itself a
28518 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
28519 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
28520 is a STRING_CST for the string literal before the parenthesis. Returns
28521 ERROR_MARK_NODE if any of the operands are invalid. */
28523 static tree
28524 cp_parser_asm_operand_list (cp_parser* parser)
28526 tree asm_operands = NULL_TREE;
28527 bool invalid_operands = false;
28529 while (true)
28531 tree string_literal;
28532 tree expression;
28533 tree name;
28535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
28537 /* Consume the `[' token. */
28538 cp_lexer_consume_token (parser->lexer);
28539 /* Read the operand name. */
28540 name = cp_parser_identifier (parser);
28541 if (name != error_mark_node)
28542 name = build_string (IDENTIFIER_LENGTH (name),
28543 IDENTIFIER_POINTER (name));
28544 /* Look for the closing `]'. */
28545 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28547 else
28548 name = NULL_TREE;
28549 /* Look for the string-literal. */
28550 string_literal = cp_parser_string_literal (parser, false, false);
28552 /* Look for the `('. */
28553 matching_parens parens;
28554 parens.require_open (parser);
28555 /* Parse the expression. */
28556 expression = cp_parser_expression (parser);
28557 /* Look for the `)'. */
28558 parens.require_close (parser);
28560 if (name == error_mark_node
28561 || string_literal == error_mark_node
28562 || expression == error_mark_node)
28563 invalid_operands = true;
28565 /* Add this operand to the list. */
28566 asm_operands = tree_cons (build_tree_list (name, string_literal),
28567 expression,
28568 asm_operands);
28569 /* If the next token is not a `,', there are no more
28570 operands. */
28571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28572 break;
28573 /* Consume the `,'. */
28574 cp_lexer_consume_token (parser->lexer);
28577 return invalid_operands ? error_mark_node : nreverse (asm_operands);
28580 /* Parse an asm-clobber-list.
28582 asm-clobber-list:
28583 string-literal
28584 asm-clobber-list , string-literal
28586 Returns a TREE_LIST, indicating the clobbers in the order that they
28587 appeared. The TREE_VALUE of each node is a STRING_CST. */
28589 static tree
28590 cp_parser_asm_clobber_list (cp_parser* parser)
28592 tree clobbers = NULL_TREE;
28594 while (true)
28596 tree string_literal;
28598 /* Look for the string literal. */
28599 string_literal = cp_parser_string_literal (parser, false, false);
28600 /* Add it to the list. */
28601 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
28602 /* If the next token is not a `,', then the list is
28603 complete. */
28604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28605 break;
28606 /* Consume the `,' token. */
28607 cp_lexer_consume_token (parser->lexer);
28610 return clobbers;
28613 /* Parse an asm-label-list.
28615 asm-label-list:
28616 identifier
28617 asm-label-list , identifier
28619 Returns a TREE_LIST, indicating the labels in the order that they
28620 appeared. The TREE_VALUE of each node is a label. */
28622 static tree
28623 cp_parser_asm_label_list (cp_parser* parser)
28625 tree labels = NULL_TREE;
28627 while (true)
28629 tree identifier, label, name;
28631 /* Look for the identifier. */
28632 identifier = cp_parser_identifier (parser);
28633 if (!error_operand_p (identifier))
28635 label = lookup_label (identifier);
28636 if (TREE_CODE (label) == LABEL_DECL)
28638 TREE_USED (label) = 1;
28639 check_goto (label);
28640 name = build_string (IDENTIFIER_LENGTH (identifier),
28641 IDENTIFIER_POINTER (identifier));
28642 labels = tree_cons (name, label, labels);
28645 /* If the next token is not a `,', then the list is
28646 complete. */
28647 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28648 break;
28649 /* Consume the `,' token. */
28650 cp_lexer_consume_token (parser->lexer);
28653 return nreverse (labels);
28656 /* Return TRUE iff the next tokens in the stream are possibly the
28657 beginning of a GNU extension attribute. */
28659 static bool
28660 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
28662 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
28665 /* Return TRUE iff the next tokens in the stream are possibly the
28666 beginning of a standard C++-11 attribute specifier. */
28668 static bool
28669 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
28671 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
28674 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28675 beginning of a standard C++-11 attribute specifier. */
28677 static bool
28678 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
28680 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28682 return (cxx_dialect >= cxx11
28683 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
28684 || (token->type == CPP_OPEN_SQUARE
28685 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
28686 && token->type == CPP_OPEN_SQUARE)));
28689 /* Return TRUE iff the next Nth tokens in the stream are possibly the
28690 beginning of a GNU extension attribute. */
28692 static bool
28693 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
28695 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
28697 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
28700 /* Return true iff the next tokens can be the beginning of either a
28701 GNU attribute list, or a standard C++11 attribute sequence. */
28703 static bool
28704 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
28706 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
28707 || cp_next_tokens_can_be_std_attribute_p (parser));
28710 /* Return true iff the next Nth tokens can be the beginning of either
28711 a GNU attribute list, or a standard C++11 attribute sequence. */
28713 static bool
28714 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
28716 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
28717 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
28720 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
28721 of GNU attributes, or return NULL. */
28723 static tree
28724 cp_parser_attributes_opt (cp_parser *parser)
28726 tree attrs = NULL_TREE;
28727 while (true)
28729 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
28730 attrs = attr_chainon (attrs, cp_parser_gnu_attributes_opt (parser));
28731 else if (cp_next_tokens_can_be_std_attribute_p (parser))
28732 attrs = attr_chainon (attrs, cp_parser_std_attribute_spec_seq (parser));
28733 else
28734 break;
28736 return attrs;
28739 /* Parse an (optional) series of attributes.
28741 attributes:
28742 attributes attribute
28744 attribute:
28745 __attribute__ (( attribute-list [opt] ))
28747 The return value is as for cp_parser_gnu_attribute_list. */
28749 static tree
28750 cp_parser_gnu_attributes_opt (cp_parser* parser)
28752 tree attributes = NULL_TREE;
28754 auto cleanup = make_temp_override
28755 (parser->auto_is_implicit_function_template_parm_p, false);
28757 while (true)
28759 cp_token *token;
28760 tree attribute_list;
28761 bool ok = true;
28763 /* Peek at the next token. */
28764 token = cp_lexer_peek_token (parser->lexer);
28765 /* If it's not `__attribute__', then we're done. */
28766 if (token->keyword != RID_ATTRIBUTE)
28767 break;
28769 /* Consume the `__attribute__' keyword. */
28770 cp_lexer_consume_token (parser->lexer);
28771 /* Look for the two `(' tokens. */
28772 matching_parens outer_parens;
28773 if (!outer_parens.require_open (parser))
28774 ok = false;
28775 matching_parens inner_parens;
28776 if (!inner_parens.require_open (parser))
28777 ok = false;
28779 /* Peek at the next token. */
28780 token = cp_lexer_peek_token (parser->lexer);
28781 if (token->type != CPP_CLOSE_PAREN)
28782 /* Parse the attribute-list. */
28783 attribute_list = cp_parser_gnu_attribute_list (parser);
28784 else
28785 /* If the next token is a `)', then there is no attribute
28786 list. */
28787 attribute_list = NULL;
28789 /* Look for the two `)' tokens. */
28790 if (!inner_parens.require_close (parser))
28791 ok = false;
28792 if (!outer_parens.require_close (parser))
28793 ok = false;
28794 if (!ok)
28795 cp_parser_skip_to_end_of_statement (parser);
28797 /* Add these new attributes to the list. */
28798 attributes = attr_chainon (attributes, attribute_list);
28801 return attributes;
28804 /* Parse a GNU attribute-list.
28806 attribute-list:
28807 attribute
28808 attribute-list , attribute
28810 attribute:
28811 identifier
28812 identifier ( identifier )
28813 identifier ( identifier , expression-list )
28814 identifier ( expression-list )
28816 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
28817 to an attribute. The TREE_PURPOSE of each node is the identifier
28818 indicating which attribute is in use. The TREE_VALUE represents
28819 the arguments, if any. */
28821 static tree
28822 cp_parser_gnu_attribute_list (cp_parser* parser, bool exactly_one /* = false */)
28824 tree attribute_list = NULL_TREE;
28825 bool save_translate_strings_p = parser->translate_strings_p;
28827 /* Don't create wrapper nodes within attributes: the
28828 handlers don't know how to handle them. */
28829 auto_suppress_location_wrappers sentinel;
28831 parser->translate_strings_p = false;
28832 while (true)
28834 cp_token *token;
28835 tree identifier;
28836 tree attribute;
28838 /* Look for the identifier. We also allow keywords here; for
28839 example `__attribute__ ((const))' is legal. */
28840 token = cp_lexer_peek_token (parser->lexer);
28841 if (token->type == CPP_NAME
28842 || token->type == CPP_KEYWORD)
28844 tree arguments = NULL_TREE;
28846 /* Consume the token, but save it since we need it for the
28847 SIMD enabled function parsing. */
28848 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
28850 /* Save away the identifier that indicates which attribute
28851 this is. */
28852 identifier = (token->type == CPP_KEYWORD)
28853 /* For keywords, use the canonical spelling, not the
28854 parsed identifier. */
28855 ? ridpointers[(int) token->keyword]
28856 : id_token->u.value;
28858 identifier = canonicalize_attr_name (identifier);
28859 attribute = build_tree_list (identifier, NULL_TREE);
28861 /* Peek at the next token. */
28862 token = cp_lexer_peek_token (parser->lexer);
28863 /* If it's an `(', then parse the attribute arguments. */
28864 if (token->type == CPP_OPEN_PAREN)
28866 vec<tree, va_gc> *vec;
28867 int attr_flag = (attribute_takes_identifier_p (identifier)
28868 ? id_attr : normal_attr);
28869 if (is_attribute_p ("assume", identifier))
28870 attr_flag = assume_attr;
28871 vec = cp_parser_parenthesized_expression_list
28872 (parser, attr_flag, /*cast_p=*/false,
28873 /*allow_expansion_p=*/false,
28874 /*non_constant_p=*/NULL);
28875 if (vec == NULL)
28876 arguments = error_mark_node;
28877 else
28879 arguments = build_tree_list_vec (vec);
28880 release_tree_vector (vec);
28882 /* Save the arguments away. */
28883 TREE_VALUE (attribute) = arguments;
28886 if (arguments != error_mark_node)
28888 /* Add this attribute to the list. */
28889 TREE_CHAIN (attribute) = attribute_list;
28890 attribute_list = attribute;
28893 token = cp_lexer_peek_token (parser->lexer);
28895 /* Unless EXACTLY_ONE is set look for more attributes.
28896 If the next token isn't a `,', we're done. */
28897 if (exactly_one || token->type != CPP_COMMA)
28898 break;
28900 /* Consume the comma and keep going. */
28901 cp_lexer_consume_token (parser->lexer);
28903 parser->translate_strings_p = save_translate_strings_p;
28905 /* We built up the list in reverse order. */
28906 return nreverse (attribute_list);
28909 /* Parse arguments of omp::directive attribute.
28911 ( directive-name ,[opt] clause-list[opt] )
28913 For directive just remember the first/last tokens for subsequent
28914 parsing. */
28916 static void
28917 cp_parser_omp_directive_args (cp_parser *parser, tree attribute)
28919 cp_token *first = cp_lexer_peek_nth_token (parser->lexer, 2);
28920 if (first->type == CPP_CLOSE_PAREN)
28922 cp_lexer_consume_token (parser->lexer);
28923 error_at (first->location, "expected OpenMP directive name");
28924 cp_lexer_consume_token (parser->lexer);
28925 TREE_VALUE (attribute) = NULL_TREE;
28926 return;
28928 size_t n = cp_parser_skip_balanced_tokens (parser, 1);
28929 if (n == 1)
28931 cp_lexer_consume_token (parser->lexer);
28932 error_at (first->location, "expected attribute argument as balanced "
28933 "token sequence");
28934 TREE_VALUE (attribute) = NULL_TREE;
28935 return;
28937 for (n = n - 2; n; --n)
28938 cp_lexer_consume_token (parser->lexer);
28939 cp_token *last = cp_lexer_peek_token (parser->lexer);
28940 cp_lexer_consume_token (parser->lexer);
28941 tree arg = make_node (DEFERRED_PARSE);
28942 DEFPARSE_TOKENS (arg) = cp_token_cache_new (first, last);
28943 DEFPARSE_INSTANTIATIONS (arg) = nullptr;
28944 TREE_VALUE (attribute) = tree_cons (NULL_TREE, arg, TREE_VALUE (attribute));
28947 /* Parse arguments of omp::sequence attribute.
28949 ( omp::[opt] directive-attr [ , omp::[opt] directive-attr ]... ) */
28951 static void
28952 cp_parser_omp_sequence_args (cp_parser *parser, tree attribute)
28954 matching_parens parens;
28955 parens.consume_open (parser);
28958 cp_token *token = cp_lexer_peek_token (parser->lexer);
28959 if (token->type == CPP_NAME
28960 && token->u.value == omp_identifier
28961 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
28963 cp_lexer_consume_token (parser->lexer);
28964 cp_lexer_consume_token (parser->lexer);
28965 token = cp_lexer_peek_token (parser->lexer);
28967 bool directive = false;
28968 const char *p;
28969 if (token->type != CPP_NAME)
28970 p = "";
28971 else
28972 p = IDENTIFIER_POINTER (token->u.value);
28973 if (strcmp (p, "directive") == 0)
28974 directive = true;
28975 else if (strcmp (p, "sequence") != 0)
28977 error_at (token->location, "expected %<directive%> or %<sequence%>");
28978 cp_parser_skip_to_closing_parenthesis (parser,
28979 /*recovering=*/true,
28980 /*or_comma=*/true,
28981 /*consume_paren=*/false);
28982 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28983 break;
28984 cp_lexer_consume_token (parser->lexer);
28986 cp_lexer_consume_token (parser->lexer);
28987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
28988 cp_parser_required_error (parser, RT_OPEN_PAREN, false,
28989 UNKNOWN_LOCATION);
28990 else if (directive)
28991 cp_parser_omp_directive_args (parser, attribute);
28992 else
28993 cp_parser_omp_sequence_args (parser, attribute);
28994 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
28995 break;
28996 cp_lexer_consume_token (parser->lexer);
28998 while (1);
28999 if (!parens.require_close (parser))
29000 cp_parser_skip_to_closing_parenthesis (parser, true, false,
29001 /*consume_paren=*/true);
29004 /* Parse a standard C++11 attribute.
29006 The returned representation is a TREE_LIST which TREE_PURPOSE is
29007 the scoped name of the attribute, and the TREE_VALUE is its
29008 arguments list.
29010 Note that the scoped name of the attribute is itself a TREE_LIST
29011 which TREE_PURPOSE is the namespace of the attribute, and
29012 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
29013 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
29014 and which TREE_PURPOSE is directly the attribute name.
29016 Clients of the attribute code should use get_attribute_namespace
29017 and get_attribute_name to get the actual namespace and name of
29018 attributes, regardless of their being GNU or C++11 attributes.
29020 attribute:
29021 attribute-token attribute-argument-clause [opt]
29023 attribute-token:
29024 identifier
29025 attribute-scoped-token
29027 attribute-scoped-token:
29028 attribute-namespace :: identifier
29030 attribute-namespace:
29031 identifier
29033 attribute-argument-clause:
29034 ( balanced-token-seq )
29036 balanced-token-seq:
29037 balanced-token [opt]
29038 balanced-token-seq balanced-token
29040 balanced-token:
29041 ( balanced-token-seq )
29042 [ balanced-token-seq ]
29043 { balanced-token-seq }. */
29045 static tree
29046 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
29048 tree attribute, attr_id = NULL_TREE, arguments;
29049 cp_token *token;
29051 auto cleanup = make_temp_override
29052 (parser->auto_is_implicit_function_template_parm_p, false);
29054 /* First, parse name of the attribute, a.k.a attribute-token. */
29056 token = cp_lexer_peek_token (parser->lexer);
29057 if (token->type == CPP_NAME)
29058 attr_id = token->u.value;
29059 else if (token->type == CPP_KEYWORD)
29060 attr_id = ridpointers[(int) token->keyword];
29061 else if (token->flags & NAMED_OP)
29062 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29064 if (attr_id == NULL_TREE)
29065 return NULL_TREE;
29067 cp_lexer_consume_token (parser->lexer);
29069 token = cp_lexer_peek_token (parser->lexer);
29070 if (token->type == CPP_SCOPE)
29072 /* We are seeing a scoped attribute token. */
29074 cp_lexer_consume_token (parser->lexer);
29075 if (attr_ns)
29076 error_at (token->location, "attribute using prefix used together "
29077 "with scoped attribute token");
29078 attr_ns = attr_id;
29080 token = cp_lexer_peek_token (parser->lexer);
29081 if (token->type == CPP_NAME)
29082 attr_id = token->u.value;
29083 else if (token->type == CPP_KEYWORD)
29084 attr_id = ridpointers[(int) token->keyword];
29085 else if (token->flags & NAMED_OP)
29086 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
29087 else
29089 error_at (token->location,
29090 "expected an identifier for the attribute name");
29091 return error_mark_node;
29093 cp_lexer_consume_token (parser->lexer);
29095 attr_ns = canonicalize_attr_name (attr_ns);
29096 attr_id = canonicalize_attr_name (attr_id);
29097 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29098 NULL_TREE);
29099 token = cp_lexer_peek_token (parser->lexer);
29101 else if (attr_ns)
29103 attr_ns = canonicalize_attr_name (attr_ns);
29104 attr_id = canonicalize_attr_name (attr_id);
29105 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
29106 NULL_TREE);
29108 else
29110 attr_id = canonicalize_attr_name (attr_id);
29111 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
29112 NULL_TREE);
29114 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
29115 but no longer: we have to be able to tell [[noreturn]] and
29116 __attribute__((noreturn)) apart. */
29117 /* C++14 deprecated attribute is equivalent to GNU's. */
29118 if (is_attribute_p ("deprecated", attr_id))
29119 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29120 /* C++17 fallthrough attribute is equivalent to GNU's. */
29121 else if (is_attribute_p ("fallthrough", attr_id))
29122 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29123 /* C++23 assume attribute is equivalent to GNU's. */
29124 else if (is_attribute_p ("assume", attr_id))
29125 TREE_PURPOSE (TREE_PURPOSE (attribute)) = gnu_identifier;
29126 /* Transactional Memory TS optimize_for_synchronized attribute is
29127 equivalent to GNU transaction_callable. */
29128 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
29129 TREE_PURPOSE (attribute)
29130 = get_identifier ("transaction_callable");
29131 /* Transactional Memory attributes are GNU attributes. */
29132 else if (tm_attr_to_mask (attr_id))
29133 TREE_PURPOSE (attribute) = attr_id;
29136 /* Now parse the optional argument clause of the attribute. */
29138 if (token->type != CPP_OPEN_PAREN)
29140 if ((flag_openmp || flag_openmp_simd)
29141 && attr_ns == omp_identifier
29142 && (is_attribute_p ("directive", attr_id)
29143 || is_attribute_p ("sequence", attr_id)))
29145 error_at (token->location, "%<omp::%E%> attribute requires argument",
29146 attr_id);
29147 return NULL_TREE;
29149 return attribute;
29153 vec<tree, va_gc> *vec;
29154 int attr_flag = normal_attr;
29156 /* Maybe we don't expect to see any arguments for this attribute. */
29157 const attribute_spec *as
29158 = lookup_attribute_spec (TREE_PURPOSE (attribute));
29159 if (as && as->max_length == 0)
29161 error_at (token->location, "%qE attribute does not take any arguments",
29162 attr_id);
29163 cp_parser_skip_to_closing_parenthesis (parser,
29164 /*recovering=*/true,
29165 /*or_comma=*/false,
29166 /*consume_paren=*/true);
29167 return error_mark_node;
29170 if (is_attribute_p ("assume", attr_id)
29171 && (attr_ns == NULL_TREE || attr_ns == gnu_identifier))
29172 /* The assume attribute needs special handling of the argument. */
29173 attr_flag = assume_attr;
29174 else if (attr_ns == gnu_identifier
29175 && attribute_takes_identifier_p (attr_id))
29176 /* A GNU attribute that takes an identifier in parameter. */
29177 attr_flag = id_attr;
29179 /* If this is a fake attribute created to handle -Wno-attributes,
29180 we must skip parsing the arguments. */
29181 if (as == NULL || attribute_ignored_p (as))
29183 if ((flag_openmp || flag_openmp_simd) && attr_ns == omp_identifier)
29185 if (is_attribute_p ("directive", attr_id))
29187 cp_parser_omp_directive_args (parser, attribute);
29188 return attribute;
29190 else if (is_attribute_p ("sequence", attr_id))
29192 TREE_VALUE (TREE_PURPOSE (attribute))
29193 = get_identifier ("directive");
29194 cp_parser_omp_sequence_args (parser, attribute);
29195 TREE_VALUE (attribute) = nreverse (TREE_VALUE (attribute));
29196 return attribute;
29200 /* For unknown attributes, just skip balanced tokens instead of
29201 trying to parse the arguments. */
29202 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1; n; --n)
29203 cp_lexer_consume_token (parser->lexer);
29204 return attribute;
29207 vec = cp_parser_parenthesized_expression_list
29208 (parser, attr_flag, /*cast_p=*/false,
29209 /*allow_expansion_p=*/true,
29210 /*non_constant_p=*/NULL);
29211 if (vec == NULL)
29212 arguments = error_mark_node;
29213 else
29215 if (vec->is_empty ())
29216 /* e.g. [[attr()]]. */
29217 error_at (token->location, "parentheses must be omitted if "
29218 "%qE attribute argument list is empty",
29219 attr_id);
29220 arguments = build_tree_list_vec (vec);
29221 release_tree_vector (vec);
29224 if (arguments == error_mark_node)
29225 attribute = error_mark_node;
29226 else
29227 TREE_VALUE (attribute) = arguments;
29230 return attribute;
29233 /* Warn if the attribute ATTRIBUTE appears more than once in the
29234 attribute-list ATTRIBUTES. This used to be enforced for certain
29235 attributes, but the restriction was removed in P2156. Note that
29236 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
29237 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
29238 found in ATTRIBUTES. */
29240 static bool
29241 cp_parser_check_std_attribute (location_t loc, tree attributes, tree attribute)
29243 static auto alist = { "noreturn", "deprecated", "nodiscard", "maybe_unused",
29244 "likely", "unlikely", "fallthrough",
29245 "no_unique_address" };
29246 if (attributes)
29247 for (const auto &a : alist)
29248 if (is_attribute_p (a, get_attribute_name (attribute))
29249 && is_attribute_namespace_p ("", attribute)
29250 && lookup_attribute ("", a, attributes))
29252 if (!from_macro_expansion_at (loc))
29253 warning_at (loc, OPT_Wattributes, "attribute %qs specified "
29254 "multiple times", a);
29255 return false;
29257 return true;
29260 /* Parse a list of standard C++-11 attributes.
29262 attribute-list:
29263 attribute [opt]
29264 attribute-list , attribute[opt]
29265 attribute ...
29266 attribute-list , attribute ...
29269 static tree
29270 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
29272 tree attributes = NULL_TREE, attribute = NULL_TREE;
29273 cp_token *token = NULL;
29275 while (true)
29277 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29278 attribute = cp_parser_std_attribute (parser, attr_ns);
29279 if (attribute == error_mark_node)
29280 break;
29281 if (attribute != NULL_TREE)
29283 if (cp_parser_check_std_attribute (loc, attributes, attribute))
29285 TREE_CHAIN (attribute) = attributes;
29286 attributes = attribute;
29289 token = cp_lexer_peek_token (parser->lexer);
29290 if (token->type == CPP_ELLIPSIS)
29292 cp_lexer_consume_token (parser->lexer);
29293 if (attribute == NULL_TREE)
29294 error_at (token->location,
29295 "expected attribute before %<...%>");
29296 else
29298 tree pack = make_pack_expansion (TREE_VALUE (attribute));
29299 if (pack == error_mark_node)
29300 return error_mark_node;
29301 TREE_VALUE (attribute) = pack;
29303 token = cp_lexer_peek_token (parser->lexer);
29305 if (token->type != CPP_COMMA)
29306 break;
29307 cp_lexer_consume_token (parser->lexer);
29309 attributes = nreverse (attributes);
29310 return attributes;
29313 /* Parse a standard C++-11 attribute specifier.
29315 attribute-specifier:
29316 [ [ attribute-using-prefix [opt] attribute-list ] ]
29317 alignment-specifier
29319 attribute-using-prefix:
29320 using attribute-namespace :
29322 alignment-specifier:
29323 alignas ( type-id ... [opt] )
29324 alignas ( alignment-expression ... [opt] ). */
29326 static tree
29327 cp_parser_std_attribute_spec (cp_parser *parser)
29329 tree attributes = NULL_TREE;
29330 cp_token *token = cp_lexer_peek_token (parser->lexer);
29332 if (token->type == CPP_OPEN_SQUARE
29333 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
29335 tree attr_ns = NULL_TREE;
29337 cp_lexer_consume_token (parser->lexer);
29338 cp_lexer_consume_token (parser->lexer);
29340 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
29342 token = cp_lexer_peek_nth_token (parser->lexer, 2);
29343 if (token->type == CPP_NAME)
29344 attr_ns = token->u.value;
29345 else if (token->type == CPP_KEYWORD)
29346 attr_ns = ridpointers[(int) token->keyword];
29347 else if (token->flags & NAMED_OP)
29348 attr_ns = get_identifier (cpp_type2name (token->type,
29349 token->flags));
29350 if (attr_ns
29351 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
29353 if (cxx_dialect < cxx17)
29354 pedwarn (input_location, OPT_Wc__17_extensions,
29355 "attribute using prefix only available "
29356 "with %<-std=c++17%> or %<-std=gnu++17%>");
29358 cp_lexer_consume_token (parser->lexer);
29359 cp_lexer_consume_token (parser->lexer);
29360 cp_lexer_consume_token (parser->lexer);
29362 else
29363 attr_ns = NULL_TREE;
29366 attributes = cp_parser_std_attribute_list (parser, attr_ns);
29368 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
29369 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
29370 cp_parser_skip_to_end_of_statement (parser);
29371 else
29372 /* Warn about parsing c++11 attribute in non-c++11 mode, only
29373 when we are sure that we have actually parsed them. */
29374 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29376 else
29378 tree alignas_expr;
29380 /* Look for an alignment-specifier. */
29382 token = cp_lexer_peek_token (parser->lexer);
29384 if (token->type != CPP_KEYWORD
29385 || token->keyword != RID_ALIGNAS)
29386 return NULL_TREE;
29388 cp_lexer_consume_token (parser->lexer);
29389 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
29391 matching_parens parens;
29392 if (!parens.require_open (parser))
29393 return error_mark_node;
29395 cp_parser_parse_tentatively (parser);
29396 alignas_expr = cp_parser_type_id (parser);
29398 if (!cp_parser_parse_definitely (parser))
29400 alignas_expr = cp_parser_assignment_expression (parser);
29401 if (alignas_expr == error_mark_node)
29402 cp_parser_skip_to_end_of_statement (parser);
29403 if (alignas_expr == NULL_TREE
29404 || alignas_expr == error_mark_node)
29405 return alignas_expr;
29408 alignas_expr = cxx_alignas_expr (alignas_expr);
29409 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
29411 /* Handle alignas (pack...). */
29412 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29414 cp_lexer_consume_token (parser->lexer);
29415 alignas_expr = make_pack_expansion (alignas_expr);
29418 /* Something went wrong, so don't build the attribute. */
29419 if (alignas_expr == error_mark_node)
29420 return error_mark_node;
29422 /* Missing ')' means the code cannot possibly be valid; go ahead
29423 and commit to make sure we issue a hard error. */
29424 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
29425 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29426 cp_parser_commit_to_tentative_parse (parser);
29428 if (!parens.require_close (parser))
29429 return error_mark_node;
29431 /* Build the C++-11 representation of an 'aligned'
29432 attribute. */
29433 attributes
29434 = build_tree_list (build_tree_list (gnu_identifier,
29435 aligned_identifier), alignas_expr);
29438 return attributes;
29441 /* Parse a standard C++-11 attribute-specifier-seq.
29443 attribute-specifier-seq:
29444 attribute-specifier-seq [opt] attribute-specifier */
29446 static tree
29447 cp_parser_std_attribute_spec_seq (cp_parser *parser)
29449 tree attr_specs = NULL_TREE;
29450 tree attr_last = NULL_TREE;
29452 /* Don't create wrapper nodes within attributes: the
29453 handlers don't know how to handle them. */
29454 auto_suppress_location_wrappers sentinel;
29456 while (true)
29458 tree attr_spec = cp_parser_std_attribute_spec (parser);
29459 if (attr_spec == NULL_TREE)
29460 break;
29461 if (attr_spec == error_mark_node)
29462 return error_mark_node;
29464 if (attr_last)
29465 TREE_CHAIN (attr_last) = attr_spec;
29466 else
29467 attr_specs = attr_last = attr_spec;
29468 attr_last = tree_last (attr_last);
29471 return attr_specs;
29474 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
29475 return index of the first token after balanced-token, or N on failure. */
29477 static size_t
29478 cp_parser_skip_balanced_tokens (cp_parser *parser, size_t n)
29480 size_t orig_n = n;
29481 int nparens = 0, nbraces = 0, nsquares = 0;
29483 switch (cp_lexer_peek_nth_token (parser->lexer, n++)->type)
29485 case CPP_PRAGMA_EOL:
29486 if (!parser->lexer->in_pragma)
29487 break;
29488 /* FALLTHRU */
29489 case CPP_EOF:
29490 /* Ran out of tokens. */
29491 return orig_n;
29492 case CPP_OPEN_PAREN:
29493 ++nparens;
29494 break;
29495 case CPP_OPEN_BRACE:
29496 ++nbraces;
29497 break;
29498 case CPP_OPEN_SQUARE:
29499 ++nsquares;
29500 break;
29501 case CPP_CLOSE_PAREN:
29502 --nparens;
29503 break;
29504 case CPP_CLOSE_BRACE:
29505 --nbraces;
29506 break;
29507 case CPP_CLOSE_SQUARE:
29508 --nsquares;
29509 break;
29510 default:
29511 break;
29513 while (nparens || nbraces || nsquares);
29514 return n;
29517 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
29518 return index of the first token after the GNU attribute tokens, or N on
29519 failure. */
29521 static size_t
29522 cp_parser_skip_gnu_attributes_opt (cp_parser *parser, size_t n)
29524 while (true)
29526 if (!cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ATTRIBUTE)
29527 || !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN)
29528 || !cp_lexer_nth_token_is (parser->lexer, n + 2, CPP_OPEN_PAREN))
29529 break;
29531 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 2);
29532 if (n2 == n + 2)
29533 break;
29534 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_PAREN))
29535 break;
29536 n = n2 + 1;
29538 return n;
29541 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
29542 next token), return index of the first token after the standard C++11
29543 attribute tokens, or N on failure. */
29545 static size_t
29546 cp_parser_skip_std_attribute_spec_seq (cp_parser *parser, size_t n)
29548 while (true)
29550 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
29551 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE))
29553 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29554 if (n2 == n + 1)
29555 break;
29556 if (!cp_lexer_nth_token_is (parser->lexer, n2, CPP_CLOSE_SQUARE))
29557 break;
29558 n = n2 + 1;
29560 else if (cp_lexer_nth_token_is_keyword (parser->lexer, n, RID_ALIGNAS)
29561 && cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_PAREN))
29563 size_t n2 = cp_parser_skip_balanced_tokens (parser, n + 1);
29564 if (n2 == n + 1)
29565 break;
29566 n = n2;
29568 else
29569 break;
29571 return n;
29574 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
29575 as the next token), return index of the first token after the attribute
29576 tokens, or N on failure. */
29578 static size_t
29579 cp_parser_skip_attributes_opt (cp_parser *parser, size_t n)
29581 if (cp_nth_tokens_can_be_gnu_attribute_p (parser, n))
29582 return cp_parser_skip_gnu_attributes_opt (parser, n);
29583 return cp_parser_skip_std_attribute_spec_seq (parser, n);
29586 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
29587 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
29588 current value of the PEDANTIC flag, regardless of whether or not
29589 the `__extension__' keyword is present. The caller is responsible
29590 for restoring the value of the PEDANTIC flag. */
29592 static bool
29593 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
29595 /* Save the old value of the PEDANTIC flag. */
29596 *saved_pedantic = pedantic;
29598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
29600 /* Consume the `__extension__' token. */
29601 cp_lexer_consume_token (parser->lexer);
29602 /* We're not being pedantic while the `__extension__' keyword is
29603 in effect. */
29604 pedantic = 0;
29606 return true;
29609 return false;
29612 /* Parse a label declaration.
29614 label-declaration:
29615 __label__ label-declarator-seq ;
29617 label-declarator-seq:
29618 identifier , label-declarator-seq
29619 identifier */
29621 static void
29622 cp_parser_label_declaration (cp_parser* parser)
29624 /* Look for the `__label__' keyword. */
29625 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
29627 while (true)
29629 tree identifier;
29631 /* Look for an identifier. */
29632 identifier = cp_parser_identifier (parser);
29633 /* If we failed, stop. */
29634 if (identifier == error_mark_node)
29635 break;
29636 /* Declare it as a label. */
29637 finish_label_decl (identifier);
29638 /* If the next token is a `;', stop. */
29639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29640 break;
29641 /* Look for the `,' separating the label declarations. */
29642 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
29645 /* Look for the final `;'. */
29646 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
29649 // -------------------------------------------------------------------------- //
29650 // Concept definitions
29652 static tree
29653 cp_parser_concept_definition (cp_parser *parser)
29655 /* A concept definition is an unevaluated context. */
29656 cp_unevaluated u;
29658 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT));
29659 cp_lexer_consume_token (parser->lexer);
29661 cp_expr id = cp_parser_identifier (parser);
29662 if (id == error_mark_node)
29664 cp_parser_skip_to_end_of_statement (parser);
29665 cp_parser_consume_semicolon_at_end_of_statement (parser);
29666 return NULL_TREE;
29669 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
29671 cp_parser_skip_to_end_of_statement (parser);
29672 cp_parser_consume_semicolon_at_end_of_statement (parser);
29673 return error_mark_node;
29676 processing_constraint_expression_sentinel parsing_constraint;
29677 tree init = cp_parser_constraint_expression (parser);
29678 if (init == error_mark_node)
29679 cp_parser_skip_to_end_of_statement (parser);
29681 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
29682 but continue as if it were. */
29683 cp_parser_consume_semicolon_at_end_of_statement (parser);
29685 return finish_concept_definition (id, init);
29688 // -------------------------------------------------------------------------- //
29689 // Requires Clause
29691 /* Diagnose an expression that should appear in ()'s within a requires-clause
29692 and suggest where to place those parentheses. */
29694 static void
29695 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc)
29697 error_at (loc, "expression must be enclosed in parentheses");
29700 static void
29701 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc)
29703 gcc_rich_location richloc (loc);
29704 richloc.add_fixit_insert_before ("(");
29705 richloc.add_fixit_insert_after (")");
29706 error_at (&richloc, "expression must be enclosed in parentheses");
29709 /* Characterizes the likely kind of expression intended by a mis-written
29710 primary constraint. */
29711 enum primary_constraint_error
29713 pce_ok,
29714 pce_maybe_operator,
29715 pce_maybe_postfix
29718 /* Returns true if the token(s) following a primary-expression in a
29719 constraint-logical-* expression would require parentheses. */
29721 static primary_constraint_error
29722 cp_parser_constraint_requires_parens (cp_parser *parser, bool lambda_p)
29724 cp_token *token = cp_lexer_peek_token (parser->lexer);
29725 switch (token->type)
29727 default:
29728 return pce_ok;
29730 case CPP_EQ:
29732 /* An equal sign may be part of the definition of a function,
29733 and not an assignment operator, when parsing the expression
29734 for a trailing requires-clause. For example:
29736 template<typename T>
29737 struct S {
29738 S() requires C<T> = default;
29741 Don't try to reparse this a binary operator. */
29742 if (cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DELETE)
29743 || cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_DEFAULT))
29744 return pce_ok;
29746 gcc_fallthrough ();
29749 /* Arithmetic operators. */
29750 case CPP_PLUS:
29751 case CPP_MINUS:
29752 case CPP_MULT:
29753 case CPP_DIV:
29754 case CPP_MOD:
29755 /* Bitwise operators. */
29756 case CPP_AND:
29757 case CPP_OR:
29758 case CPP_XOR:
29759 case CPP_RSHIFT:
29760 case CPP_LSHIFT:
29761 /* Relational operators. */
29762 case CPP_EQ_EQ:
29763 case CPP_NOT_EQ:
29764 case CPP_LESS:
29765 case CPP_GREATER:
29766 case CPP_LESS_EQ:
29767 case CPP_GREATER_EQ:
29768 case CPP_SPACESHIP:
29769 /* Pointer-to-member. */
29770 case CPP_DOT_STAR:
29771 case CPP_DEREF_STAR:
29772 /* Assignment operators. */
29773 case CPP_PLUS_EQ:
29774 case CPP_MINUS_EQ:
29775 case CPP_MULT_EQ:
29776 case CPP_DIV_EQ:
29777 case CPP_MOD_EQ:
29778 case CPP_AND_EQ:
29779 case CPP_OR_EQ:
29780 case CPP_XOR_EQ:
29781 case CPP_RSHIFT_EQ:
29782 case CPP_LSHIFT_EQ:
29783 /* Conditional operator */
29784 case CPP_QUERY:
29785 /* Unenclosed binary or conditional operator. */
29786 return pce_maybe_operator;
29788 case CPP_OPEN_PAREN:
29790 /* A primary constraint that precedes the parameter-list of a
29791 lambda expression is followed by an open paren.
29793 []<typename T> requires C (T a, T b) { ... }
29795 Don't try to re-parse this as a postfix expression. */
29796 if (lambda_p)
29797 return pce_ok;
29799 gcc_fallthrough ();
29801 case CPP_OPEN_SQUARE:
29803 /* A primary-constraint-expression followed by a '[[' is not a
29804 postfix expression. */
29805 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE))
29806 return pce_ok;
29808 gcc_fallthrough ();
29810 case CPP_PLUS_PLUS:
29811 case CPP_MINUS_MINUS:
29812 case CPP_DOT:
29813 /* Unenclosed postfix operator. */
29814 return pce_maybe_postfix;
29816 case CPP_DEREF:
29817 /* A primary constraint that precedes the lambda-declarator of a
29818 lambda expression is followed by trailing return type.
29820 []<typename T> requires C -> void {}
29822 Don't try to re-parse this as a postfix expression in
29823 C++23 and later. In C++20 ( needs to come in between but we
29824 allow it to be omitted with pedwarn. */
29825 if (lambda_p)
29826 return pce_ok;
29827 /* Unenclosed postfix operator. */
29828 return pce_maybe_postfix;
29832 /* Returns true if the next token begins a unary expression, preceded by
29833 an operator or keyword. */
29835 static bool
29836 cp_parser_unary_constraint_requires_parens (cp_parser *parser)
29838 cp_token *token = cp_lexer_peek_token (parser->lexer);
29839 switch (token->type)
29841 case CPP_NOT:
29842 case CPP_PLUS:
29843 case CPP_MINUS:
29844 case CPP_MULT:
29845 case CPP_COMPL:
29846 case CPP_PLUS_PLUS:
29847 case CPP_MINUS_MINUS:
29848 return true;
29850 case CPP_KEYWORD:
29852 switch (token->keyword)
29854 case RID_STATCAST:
29855 case RID_DYNCAST:
29856 case RID_REINTCAST:
29857 case RID_CONSTCAST:
29858 case RID_TYPEID:
29859 case RID_SIZEOF:
29860 case RID_ALIGNOF:
29861 case RID_NOEXCEPT:
29862 case RID_NEW:
29863 case RID_DELETE:
29864 case RID_THROW:
29865 return true;
29867 default:
29868 break;
29872 default:
29873 break;
29876 return false;
29879 /* Parse a primary expression within a constraint. */
29881 static cp_expr
29882 cp_parser_constraint_primary_expression (cp_parser *parser, bool lambda_p)
29884 /* If this looks like a unary expression, parse it as such, but diagnose
29885 it as ill-formed; it requires parens. */
29886 if (cp_parser_unary_constraint_requires_parens (parser))
29888 cp_expr e = cp_parser_assignment_expression (parser, NULL, false, false);
29889 cp_parser_diagnose_ungrouped_constraint_rich (e.get_location());
29890 return e;
29893 cp_lexer_save_tokens (parser->lexer);
29894 cp_id_kind idk;
29895 location_t loc = input_location;
29896 cp_expr expr = cp_parser_primary_expression (parser,
29897 /*address_p=*/false,
29898 /*cast_p=*/false,
29899 /*template_arg_p=*/false,
29900 &idk);
29901 expr.maybe_add_location_wrapper ();
29903 primary_constraint_error pce = pce_ok;
29904 if (expr != error_mark_node)
29906 /* The primary-expression could be part of an unenclosed non-logical
29907 compound expression. */
29908 pce = cp_parser_constraint_requires_parens (parser, lambda_p);
29910 if (pce == pce_ok)
29912 cp_lexer_commit_tokens (parser->lexer);
29913 return finish_constraint_primary_expr (expr);
29916 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
29917 error, but return the expression as if it were valid. */
29918 cp_lexer_rollback_tokens (parser->lexer);
29919 cp_parser_parse_tentatively (parser);
29920 if (pce == pce_maybe_operator)
29921 expr = cp_parser_assignment_expression (parser, NULL, false, false);
29922 else
29923 expr = cp_parser_simple_cast_expression (parser);
29924 if (cp_parser_parse_definitely (parser))
29926 cp_parser_diagnose_ungrouped_constraint_rich (expr.get_location());
29927 return expr;
29930 /* Otherwise, something has gone very wrong, and we can't generate a more
29931 meaningful diagnostic or recover. */
29932 cp_parser_diagnose_ungrouped_constraint_plain (loc);
29933 return error_mark_node;
29936 /* Parse a constraint-logical-and-expression.
29938 constraint-logical-and-expression:
29939 primary-expression
29940 constraint-logical-and-expression '&&' primary-expression */
29942 static cp_expr
29943 cp_parser_constraint_logical_and_expression (cp_parser *parser, bool lambda_p)
29945 cp_expr lhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29946 while (cp_lexer_next_token_is (parser->lexer, CPP_AND_AND))
29948 cp_token *op = cp_lexer_consume_token (parser->lexer);
29949 tree rhs = cp_parser_constraint_primary_expression (parser, lambda_p);
29950 lhs = finish_constraint_and_expr (op->location, lhs, rhs);
29952 return lhs;
29955 /* Parse a constraint-logical-or-expression.
29957 constraint-logical-or-expression:
29958 constraint-logical-and-expression
29959 constraint-logical-or-expression '||' constraint-logical-and-expression */
29961 static cp_expr
29962 cp_parser_constraint_logical_or_expression (cp_parser *parser, bool lambda_p)
29964 cp_expr lhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29965 while (cp_lexer_next_token_is (parser->lexer, CPP_OR_OR))
29967 cp_token *op = cp_lexer_consume_token (parser->lexer);
29968 cp_expr rhs = cp_parser_constraint_logical_and_expression (parser, lambda_p);
29969 lhs = finish_constraint_or_expr (op->location, lhs, rhs);
29971 return lhs;
29974 /* Parse the expression after a requires-clause. This has a different grammar
29975 than that in the concepts TS. */
29977 static tree
29978 cp_parser_requires_clause_expression (cp_parser *parser, bool lambda_p)
29980 processing_constraint_expression_sentinel parsing_constraint;
29981 ++processing_template_decl;
29982 cp_expr expr = cp_parser_constraint_logical_or_expression (parser, lambda_p);
29983 --processing_template_decl;
29984 if (check_for_bare_parameter_packs (expr))
29985 expr = error_mark_node;
29986 return expr;
29989 /* Parse a expression after a requires clause.
29991 constraint-expression:
29992 logical-or-expression
29994 The required logical-or-expression must be a constant expression. Note
29995 that we don't check that the expression is constepxr here. We defer until
29996 we analyze constraints and then, we only check atomic constraints. */
29998 static tree
29999 cp_parser_constraint_expression (cp_parser *parser)
30001 processing_constraint_expression_sentinel parsing_constraint;
30002 ++processing_template_decl;
30003 cp_expr expr = cp_parser_binary_expression (parser, false, true,
30004 PREC_NOT_OPERATOR, NULL);
30005 --processing_template_decl;
30006 if (check_for_bare_parameter_packs (expr))
30007 expr = error_mark_node;
30008 expr.maybe_add_location_wrapper ();
30009 return expr;
30012 /* Optionally parse a requires clause:
30014 requires-clause:
30015 `requires` constraint-logical-or-expression.
30016 [ConceptsTS]
30017 `requires constraint-expression.
30019 LAMBDA_P is true when the requires-clause is parsed before the
30020 parameter-list of a lambda-declarator. */
30022 static tree
30023 cp_parser_requires_clause_opt (cp_parser *parser, bool lambda_p)
30025 /* A requires clause is an unevaluated context. */
30026 cp_unevaluated u;
30028 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30029 if (tok->keyword != RID_REQUIRES)
30031 if (!flag_concepts && tok->type == CPP_NAME
30032 && tok->u.value == ridpointers[RID_REQUIRES])
30034 error_at (cp_lexer_peek_token (parser->lexer)->location,
30035 "%<requires%> only available with "
30036 "%<-std=c++20%> or %<-fconcepts%>");
30037 /* Parse and discard the requires-clause. */
30038 cp_lexer_consume_token (parser->lexer);
30039 cp_parser_constraint_expression (parser);
30041 return NULL_TREE;
30044 cp_token *tok2 = cp_lexer_peek_nth_token (parser->lexer, 2);
30045 if (tok2->type == CPP_OPEN_BRACE)
30047 /* An opening brace following the start of a requires-clause is
30048 ill-formed; the user likely forgot the second `requires' that
30049 would start a requires-expression. */
30050 gcc_rich_location richloc (tok2->location);
30051 richloc.add_fixit_insert_after (tok->location, " requires");
30052 error_at (&richloc, "missing additional %<requires%> to start "
30053 "a requires-expression");
30054 /* Don't consume the `requires', so that it's reused as the start of a
30055 requires-expression. */
30057 else
30058 cp_lexer_consume_token (parser->lexer);
30060 if (!flag_concepts_ts)
30061 return cp_parser_requires_clause_expression (parser, lambda_p);
30062 else
30063 return cp_parser_constraint_expression (parser);
30066 /*---------------------------------------------------------------------------
30067 Requires expressions
30068 ---------------------------------------------------------------------------*/
30070 /* Parse a requires expression
30072 requirement-expression:
30073 'requires' requirement-parameter-list [opt] requirement-body */
30075 static tree
30076 cp_parser_requires_expression (cp_parser *parser)
30078 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30079 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
30081 /* Avoid committing to outer tentative parse. */
30082 tentative_firewall firewall (parser);
30084 /* This is definitely a requires-expression. */
30085 cp_parser_commit_to_tentative_parse (parser);
30087 tree parms, reqs;
30089 /* Local parameters are delared as variables within the scope
30090 of the expression. They are not visible past the end of
30091 the expression. Expressions within the requires-expression
30092 are unevaluated. */
30093 struct scope_sentinel
30095 scope_sentinel ()
30097 ++cp_unevaluated_operand;
30098 begin_scope (sk_function_parms, NULL_TREE);
30099 current_binding_level->requires_expression = true;
30102 ~scope_sentinel ()
30104 pop_bindings_and_leave_scope ();
30105 --cp_unevaluated_operand;
30107 } s;
30109 /* Parse the optional parameter list. */
30110 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30112 parms = cp_parser_requirement_parameter_list (parser);
30113 if (parms == error_mark_node)
30114 return error_mark_node;
30116 else
30117 parms = NULL_TREE;
30119 /* Parse the requirement body. */
30120 ++processing_template_decl;
30121 reqs = cp_parser_requirement_body (parser);
30122 --processing_template_decl;
30123 if (reqs == error_mark_node)
30124 return error_mark_node;
30127 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
30128 the parm chain. */
30129 grokparms (parms, &parms);
30130 loc = make_location (loc, loc, parser->lexer);
30131 tree expr = finish_requires_expr (loc, parms, reqs);
30132 if (!processing_template_decl)
30134 /* Perform semantic processing now to diagnose any invalid types and
30135 expressions. */
30136 int saved_errorcount = errorcount;
30137 tsubst_requires_expr (expr, NULL_TREE, tf_warning_or_error, NULL_TREE);
30138 if (errorcount > saved_errorcount)
30139 return error_mark_node;
30141 return expr;
30144 /* Parse a parameterized requirement.
30146 requirement-parameter-list:
30147 '(' parameter-declaration-clause ')' */
30149 static tree
30150 cp_parser_requirement_parameter_list (cp_parser *parser)
30152 matching_parens parens;
30153 if (!parens.require_open (parser))
30154 return error_mark_node;
30156 tree parms = (cp_parser_parameter_declaration_clause
30157 (parser, CP_PARSER_FLAGS_TYPENAME_OPTIONAL));
30159 if (!parens.require_close (parser))
30160 return error_mark_node;
30162 /* Modify the declared parameters by removing their context
30163 so they don't refer to the enclosing scope and explicitly
30164 indicating that they are constraint variables. */
30165 for (tree parm = parms; parm; parm = TREE_CHAIN (parm))
30167 if (parm == void_list_node || parm == explicit_void_list_node)
30168 break;
30169 tree decl = TREE_VALUE (parm);
30170 if (decl != error_mark_node)
30172 DECL_CONTEXT (decl) = NULL_TREE;
30173 CONSTRAINT_VAR_P (decl) = true;
30177 return parms;
30180 /* Parse the body of a requirement.
30182 requirement-body:
30183 '{' requirement-list '}' */
30184 static tree
30185 cp_parser_requirement_body (cp_parser *parser)
30187 matching_braces braces;
30188 if (!braces.require_open (parser))
30189 return error_mark_node;
30191 tree reqs = cp_parser_requirement_seq (parser);
30193 if (!braces.require_close (parser))
30194 return error_mark_node;
30196 return reqs;
30199 /* Parse a sequence of requirements.
30201 requirement-seq:
30202 requirement
30203 requirement-seq requirement */
30205 static tree
30206 cp_parser_requirement_seq (cp_parser *parser)
30208 tree result = NULL_TREE;
30211 tree req = cp_parser_requirement (parser);
30212 if (req != error_mark_node)
30213 result = tree_cons (NULL_TREE, req, result);
30215 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE)
30216 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF));
30218 /* If there are no valid requirements, this is not a valid expression. */
30219 if (!result)
30220 return error_mark_node;
30222 /* Reverse the order of requirements so they are analyzed in order. */
30223 return nreverse (result);
30226 /* Parse a syntactic requirement or type requirement.
30228 requirement:
30229 simple-requirement
30230 compound-requirement
30231 type-requirement
30232 nested-requirement */
30234 static tree
30235 cp_parser_requirement (cp_parser *parser)
30237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
30238 return cp_parser_compound_requirement (parser);
30239 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
30240 return cp_parser_type_requirement (parser);
30241 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
30242 return cp_parser_nested_requirement (parser);
30243 else
30244 return cp_parser_simple_requirement (parser);
30247 /* Parse a simple requirement.
30249 simple-requirement:
30250 expression ';' */
30252 static tree
30253 cp_parser_simple_requirement (cp_parser *parser)
30255 location_t start = cp_lexer_peek_token (parser->lexer)->location;
30256 cp_expr expr = cp_parser_expression (parser, NULL, false, false);
30257 if (expr == error_mark_node)
30258 cp_parser_skip_to_end_of_statement (parser);
30260 cp_parser_consume_semicolon_at_end_of_statement (parser);
30262 if (!expr || expr == error_mark_node)
30263 return error_mark_node;
30265 /* Sometimes we don't get locations, so use the cached token location
30266 as a reasonable approximation. */
30267 if (expr.get_location() == UNKNOWN_LOCATION)
30268 expr.set_location (start);
30270 for (tree t = expr; ; )
30272 if (TREE_CODE (t) == TRUTH_ANDIF_EXPR
30273 || TREE_CODE (t) == TRUTH_ORIF_EXPR)
30275 t = TREE_OPERAND (t, 0);
30276 continue;
30278 if (concept_check_p (t))
30280 gcc_rich_location richloc (get_start (start));
30281 richloc.add_fixit_insert_before (start, "requires ");
30282 warning_at (&richloc, OPT_Wmissing_requires, "testing "
30283 "if a concept-id is a valid expression; add "
30284 "%<requires%> to check satisfaction");
30286 break;
30289 return finish_simple_requirement (expr.get_location (), expr);
30292 /* Parse a type requirement
30294 type-requirement
30295 nested-name-specifier [opt] required-type-name ';'
30297 required-type-name:
30298 type-name
30299 'template' [opt] simple-template-id */
30301 static tree
30302 cp_parser_type_requirement (cp_parser *parser)
30304 cp_token *start_tok = cp_lexer_consume_token (parser->lexer);
30305 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30307 // Save the scope before parsing name specifiers.
30308 tree saved_scope = parser->scope;
30309 tree saved_object_scope = parser->object_scope;
30310 tree saved_qualifying_scope = parser->qualifying_scope;
30311 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
30312 cp_parser_nested_name_specifier_opt (parser,
30313 /*typename_keyword_p=*/true,
30314 /*check_dependency_p=*/false,
30315 /*type_p=*/true,
30316 /*is_declaration=*/false);
30318 tree type;
30319 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
30321 cp_lexer_consume_token (parser->lexer);
30322 type = cp_parser_template_id (parser,
30323 /*template_keyword_p=*/true,
30324 /*check_dependency=*/false,
30325 /*tag_type=*/none_type,
30326 /*is_declaration=*/false);
30327 type = make_typename_type (parser->scope, type, typename_type,
30328 /*complain=*/tf_error);
30330 else
30331 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
30333 if (TREE_CODE (type) == TYPE_DECL)
30334 type = TREE_TYPE (type);
30336 parser->scope = saved_scope;
30337 parser->object_scope = saved_object_scope;
30338 parser->qualifying_scope = saved_qualifying_scope;
30340 if (type == error_mark_node)
30341 cp_parser_skip_to_end_of_statement (parser);
30343 cp_parser_consume_semicolon_at_end_of_statement (parser);
30345 if (type == error_mark_node)
30346 return error_mark_node;
30348 loc = make_location (loc, start_tok->location, parser->lexer);
30349 return finish_type_requirement (loc, type);
30352 /* Parse a compound requirement
30354 compound-requirement:
30355 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
30357 static tree
30358 cp_parser_compound_requirement (cp_parser *parser)
30360 /* Parse an expression enclosed in '{ }'s. */
30361 matching_braces braces;
30362 if (!braces.require_open (parser))
30363 return error_mark_node;
30365 cp_token *expr_token = cp_lexer_peek_token (parser->lexer);
30367 tree expr = cp_parser_expression (parser, NULL, false, false);
30368 if (expr == error_mark_node)
30369 cp_parser_skip_to_closing_brace (parser);
30371 if (!braces.require_close (parser))
30373 cp_parser_skip_to_end_of_statement (parser);
30374 cp_parser_consume_semicolon_at_end_of_statement (parser);
30375 return error_mark_node;
30378 /* If the expression was invalid, skip the remainder of the requirement. */
30379 if (!expr || expr == error_mark_node)
30381 cp_parser_skip_to_end_of_statement (parser);
30382 cp_parser_consume_semicolon_at_end_of_statement (parser);
30383 return error_mark_node;
30386 /* Parse the optional noexcept. */
30387 bool noexcept_p = false;
30388 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
30390 cp_lexer_consume_token (parser->lexer);
30391 noexcept_p = true;
30394 /* Parse the optional trailing return type. */
30395 tree type = NULL_TREE;
30396 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
30398 cp_lexer_consume_token (parser->lexer);
30399 cp_token *tok = cp_lexer_peek_token (parser->lexer);
30401 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
30402 parser->in_result_type_constraint_p = true;
30403 /* C++20 allows either a type-id or a type-constraint. Parsing
30404 a type-id will subsume the parsing for a type-constraint but
30405 allow for more syntactic forms (e.g., const C<T>*). */
30406 type = cp_parser_trailing_type_id (parser);
30407 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
30408 if (type == error_mark_node)
30409 return error_mark_node;
30411 location_t type_loc = make_location (tok->location, tok->location,
30412 parser->lexer);
30414 /* Check that we haven't written something like 'const C<T>*'. */
30415 if (type_uses_auto (type))
30417 if (!is_auto (type))
30419 error_at (type_loc,
30420 "result type is not a plain type-constraint");
30421 cp_parser_consume_semicolon_at_end_of_statement (parser);
30422 return error_mark_node;
30425 else if (!flag_concepts_ts)
30426 /* P1452R2 removed the trailing-return-type option. */
30427 error_at (type_loc,
30428 "return-type-requirement is not a type-constraint");
30431 location_t loc = make_location (expr_token->location,
30432 braces.open_location (),
30433 parser->lexer);
30435 cp_parser_consume_semicolon_at_end_of_statement (parser);
30437 if (expr == error_mark_node || type == error_mark_node)
30438 return error_mark_node;
30440 return finish_compound_requirement (loc, expr, type, noexcept_p);
30443 /* Parse a nested requirement. This is the same as a requires clause.
30445 nested-requirement:
30446 requires-clause */
30448 static tree
30449 cp_parser_nested_requirement (cp_parser *parser)
30451 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
30452 cp_token *tok = cp_lexer_consume_token (parser->lexer);
30453 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30454 tree req = cp_parser_constraint_expression (parser);
30455 if (req == error_mark_node)
30456 cp_parser_skip_to_end_of_statement (parser);
30457 loc = make_location (loc, tok->location, parser->lexer);
30458 cp_parser_consume_semicolon_at_end_of_statement (parser);
30459 if (req == error_mark_node)
30460 return error_mark_node;
30461 return finish_nested_requirement (loc, req);
30464 /* Support Functions */
30466 /* Return the appropriate prefer_type argument for lookup_name based on
30467 tag_type. */
30469 static inline LOOK_want
30470 prefer_type_arg (tag_types tag_type)
30472 switch (tag_type)
30474 case none_type: return LOOK_want::NORMAL; // No preference.
30475 case scope_type: return LOOK_want::TYPE_NAMESPACE; // Type or namespace.
30476 default: return LOOK_want::TYPE; // Type only.
30480 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
30481 NAME should have one of the representations used for an
30482 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
30483 is returned. If PARSER->SCOPE is a dependent type, then a
30484 SCOPE_REF is returned.
30486 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
30487 returned; the name was already resolved when the TEMPLATE_ID_EXPR
30488 was formed. Abstractly, such entities should not be passed to this
30489 function, because they do not need to be looked up, but it is
30490 simpler to check for this special case here, rather than at the
30491 call-sites.
30493 In cases not explicitly covered above, this function returns a
30494 DECL, OVERLOAD, or baselink representing the result of the lookup.
30495 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
30496 is returned.
30498 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
30499 (e.g., "struct") that was used. In that case bindings that do not
30500 refer to types are ignored.
30502 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
30503 ignored.
30505 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
30506 are ignored.
30508 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
30509 types.
30511 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
30512 TREE_LIST of candidates if name-lookup results in an ambiguity, and
30513 NULL_TREE otherwise. */
30515 static cp_expr
30516 cp_parser_lookup_name (cp_parser *parser, tree name,
30517 enum tag_types tag_type,
30518 bool is_template,
30519 bool is_namespace,
30520 bool check_dependency,
30521 tree *ambiguous_decls,
30522 location_t name_location)
30524 tree decl;
30525 tree object_type = parser->context->object_type;
30527 /* Assume that the lookup will be unambiguous. */
30528 if (ambiguous_decls)
30529 *ambiguous_decls = NULL_TREE;
30531 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
30532 no longer valid. Note that if we are parsing tentatively, and
30533 the parse fails, OBJECT_TYPE will be automatically restored. */
30534 parser->context->object_type = NULL_TREE;
30536 if (name == error_mark_node)
30537 return error_mark_node;
30539 /* A template-id has already been resolved; there is no lookup to
30540 do. */
30541 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
30542 return name;
30543 if (BASELINK_P (name))
30545 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
30546 == TEMPLATE_ID_EXPR);
30547 return name;
30550 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
30551 it should already have been checked to make sure that the name
30552 used matches the type being destroyed. */
30553 if (TREE_CODE (name) == BIT_NOT_EXPR)
30555 tree type;
30557 /* Figure out to which type this destructor applies. */
30558 if (parser->scope)
30559 type = parser->scope;
30560 else if (object_type)
30561 type = object_type;
30562 else
30563 type = current_class_type;
30564 /* If that's not a class type, there is no destructor. */
30565 if (!type || !CLASS_TYPE_P (type))
30566 return error_mark_node;
30568 /* In a non-static member function, check implicit this->. */
30569 if (current_class_ref)
30570 return lookup_destructor (current_class_ref, parser->scope, name,
30571 tf_warning_or_error);
30573 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
30574 lazily_declare_fn (sfk_destructor, type);
30576 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
30577 return dtor;
30579 return error_mark_node;
30582 /* By this point, the NAME should be an ordinary identifier. If
30583 the id-expression was a qualified name, the qualifying scope is
30584 stored in PARSER->SCOPE at this point. */
30585 gcc_assert (identifier_p (name));
30587 /* Perform the lookup. */
30588 if (parser->scope)
30590 bool dependent_p;
30592 if (parser->scope == error_mark_node)
30593 return error_mark_node;
30595 /* If the SCOPE is dependent, the lookup must be deferred until
30596 the template is instantiated -- unless we are explicitly
30597 looking up names in uninstantiated templates. Even then, we
30598 cannot look up the name if the scope is not a class type; it
30599 might, for example, be a template type parameter. */
30600 dependent_p = (TYPE_P (parser->scope)
30601 && dependent_scope_p (parser->scope));
30602 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
30603 && dependent_p)
30604 /* Defer lookup. */
30605 decl = error_mark_node;
30606 else
30608 tree pushed_scope = NULL_TREE;
30610 /* If PARSER->SCOPE is a dependent type, then it must be a
30611 class type, and we must not be checking dependencies;
30612 otherwise, we would have processed this lookup above. So
30613 that PARSER->SCOPE is not considered a dependent base by
30614 lookup_member, we must enter the scope here. */
30615 if (dependent_p)
30616 pushed_scope = push_scope (parser->scope);
30618 /* If the PARSER->SCOPE is a template specialization, it
30619 may be instantiated during name lookup. In that case,
30620 errors may be issued. Even if we rollback the current
30621 tentative parse, those errors are valid. */
30622 decl = lookup_qualified_name (parser->scope, name,
30623 prefer_type_arg (tag_type),
30624 /*complain=*/true);
30626 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
30627 lookup result and the nested-name-specifier nominates a class C:
30628 * if the name specified after the nested-name-specifier, when
30629 looked up in C, is the injected-class-name of C (Clause 9), or
30630 * if the name specified after the nested-name-specifier is the
30631 same as the identifier or the simple-template-id's template-
30632 name in the last component of the nested-name-specifier,
30633 the name is instead considered to name the constructor of
30634 class C. [ Note: for example, the constructor is not an
30635 acceptable lookup result in an elaborated-type-specifier so
30636 the constructor would not be used in place of the
30637 injected-class-name. --end note ] Such a constructor name
30638 shall be used only in the declarator-id of a declaration that
30639 names a constructor or in a using-declaration. */
30640 if (tag_type == none_type
30641 && DECL_SELF_REFERENCE_P (decl)
30642 && same_type_p (DECL_CONTEXT (decl), parser->scope))
30643 decl = lookup_qualified_name (parser->scope, ctor_identifier,
30644 prefer_type_arg (tag_type),
30645 /*complain=*/true);
30647 if (pushed_scope)
30648 pop_scope (pushed_scope);
30651 /* If the scope is a dependent type and either we deferred lookup or
30652 we did lookup but didn't find the name, rememeber the name. */
30653 if (decl == error_mark_node && TYPE_P (parser->scope)
30654 && dependent_type_p (parser->scope))
30656 if (tag_type)
30658 tree type;
30660 /* The resolution to Core Issue 180 says that `struct
30661 A::B' should be considered a type-name, even if `A'
30662 is dependent. */
30663 type = make_typename_type (parser->scope, name, tag_type,
30664 /*complain=*/tf_error);
30665 if (type != error_mark_node)
30666 decl = TYPE_NAME (type);
30668 else if (is_template
30669 && (cp_parser_next_token_ends_template_argument_p (parser)
30670 || cp_lexer_next_token_is (parser->lexer,
30671 CPP_CLOSE_PAREN)))
30672 decl = make_unbound_class_template (parser->scope,
30673 name, NULL_TREE,
30674 /*complain=*/tf_error);
30675 else
30676 decl = build_qualified_name (/*type=*/NULL_TREE,
30677 parser->scope, name,
30678 is_template);
30680 parser->qualifying_scope = parser->scope;
30681 parser->object_scope = NULL_TREE;
30683 else if (object_type)
30685 bool dep = dependent_scope_p (object_type);
30687 /* Look up the name in the scope of the OBJECT_TYPE, unless the
30688 OBJECT_TYPE is not a class. */
30689 if (!dep && CLASS_TYPE_P (object_type))
30690 /* If the OBJECT_TYPE is a template specialization, it may
30691 be instantiated during name lookup. In that case, errors
30692 may be issued. Even if we rollback the current tentative
30693 parse, those errors are valid. */
30694 decl = lookup_member (object_type,
30695 name,
30696 /*protect=*/0,
30697 /*prefer_type=*/tag_type != none_type,
30698 tf_warning_or_error);
30699 else
30700 decl = NULL_TREE;
30702 if (!decl)
30703 /* Look it up in the enclosing context. DR 141: When looking for a
30704 template-name after -> or ., only consider class templates. */
30705 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30706 /* DR 141: When looking in the
30707 current enclosing context for a
30708 template-name after -> or ., only
30709 consider class templates. */
30710 : is_template ? LOOK_want::TYPE
30711 : prefer_type_arg (tag_type));
30713 /* If we did unqualified lookup of a dependent member-qualified name and
30714 found something, do we want to use it? P1787 clarified that we need
30715 to look in the object scope first even if it's dependent, but for now
30716 let's still use it in some cases.
30717 FIXME remember unqualified lookup result to use if member lookup fails
30718 at instantiation time. */
30719 if (decl && dep && is_template)
30721 saved_token_sentinel toks (parser->lexer, STS_ROLLBACK);
30722 /* Only use the unqualified class template lookup if we're actually
30723 looking at a template arg list. */
30724 if (!cp_parser_skip_entire_template_parameter_list (parser))
30725 decl = NULL_TREE;
30728 /* If we know we're looking for a type (e.g. A in p->A::x),
30729 mock up a typename. */
30730 if (!decl && object_type && tag_type != none_type
30731 && dependentish_scope_p (object_type))
30733 tree type = build_typename_type (object_type, name, name,
30734 typename_type);
30735 decl = TYPE_NAME (type);
30738 parser->object_scope = object_type;
30739 parser->qualifying_scope = NULL_TREE;
30741 else
30743 decl = lookup_name (name, is_namespace ? LOOK_want::NAMESPACE
30744 : prefer_type_arg (tag_type));
30745 parser->qualifying_scope = NULL_TREE;
30746 parser->object_scope = NULL_TREE;
30749 /* If the lookup failed, let our caller know. */
30750 if (!decl || decl == error_mark_node)
30751 return error_mark_node;
30753 /* If we have resolved the name of a member declaration, check to
30754 see if the declaration is accessible. When the name resolves to
30755 set of overloaded functions, accessibility is checked when
30756 overload resolution is done. If we have a TREE_LIST, then the lookup
30757 is either ambiguous or it found multiple injected-class-names, the
30758 accessibility of which is trivially satisfied.
30760 During an explicit instantiation, access is not checked at all,
30761 as per [temp.explicit]. */
30762 if (DECL_P (decl))
30763 check_accessibility_of_qualified_id (decl, object_type, parser->scope,
30764 tf_warning_or_error);
30766 /* Pull out the template from an injected-class-name (or multiple). */
30767 if (is_template)
30768 decl = maybe_get_template_decl_from_type_decl (decl);
30770 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
30771 if (TREE_CODE (decl) == TREE_LIST)
30773 if (ambiguous_decls)
30774 *ambiguous_decls = decl;
30775 /* The error message we have to print is too complicated for
30776 cp_parser_error, so we incorporate its actions directly. */
30777 if (!cp_parser_simulate_error (parser))
30779 error_at (name_location, "reference to %qD is ambiguous",
30780 name);
30781 print_candidates (decl);
30783 return error_mark_node;
30786 gcc_assert (DECL_P (decl)
30787 || TREE_CODE (decl) == OVERLOAD
30788 || TREE_CODE (decl) == SCOPE_REF
30789 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
30790 || BASELINK_P (decl));
30792 maybe_record_typedef_use (decl);
30794 return cp_expr (decl, name_location);
30797 /* Like cp_parser_lookup_name, but for use in the typical case where
30798 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
30799 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
30801 static tree
30802 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
30804 return cp_parser_lookup_name (parser, name,
30805 none_type,
30806 /*is_template=*/false,
30807 /*is_namespace=*/false,
30808 /*check_dependency=*/true,
30809 /*ambiguous_decls=*/NULL,
30810 location);
30813 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
30814 the current context, return the TYPE_DECL. If TAG_NAME_P is
30815 true, the DECL indicates the class being defined in a class-head,
30816 or declared in an elaborated-type-specifier.
30818 Otherwise, return DECL. */
30820 static tree
30821 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
30823 /* If the TEMPLATE_DECL is being declared as part of a class-head,
30824 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
30826 struct A {
30827 template <typename T> struct B;
30830 template <typename T> struct A::B {};
30832 Similarly, in an elaborated-type-specifier:
30834 namespace N { struct X{}; }
30836 struct A {
30837 template <typename T> friend struct N::X;
30840 However, if the DECL refers to a class type, and we are in
30841 the scope of the class, then the name lookup automatically
30842 finds the TYPE_DECL created by build_self_reference rather
30843 than a TEMPLATE_DECL. For example, in:
30845 template <class T> struct S {
30846 S s;
30849 there is no need to handle such case. */
30851 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
30852 return DECL_TEMPLATE_RESULT (decl);
30854 return decl;
30857 /* If too many, or too few, template-parameter lists apply to the
30858 declarator, issue an error message. Returns TRUE if all went well,
30859 and FALSE otherwise. */
30861 static bool
30862 cp_parser_check_declarator_template_parameters (cp_parser* parser,
30863 cp_declarator *declarator,
30864 location_t declarator_location)
30866 switch (declarator->kind)
30868 case cdk_id:
30870 unsigned num_templates = 0;
30871 tree scope = declarator->u.id.qualifying_scope;
30872 bool template_id_p = false;
30874 if (scope)
30875 num_templates = num_template_headers_for_class (scope);
30876 else if (TREE_CODE (declarator->u.id.unqualified_name)
30877 == TEMPLATE_ID_EXPR)
30879 /* If the DECLARATOR has the form `X<y>' then it uses one
30880 additional level of template parameters. */
30881 ++num_templates;
30882 template_id_p = true;
30885 return cp_parser_check_template_parameters
30886 (parser, num_templates, template_id_p, declarator_location,
30887 declarator);
30890 case cdk_function:
30891 case cdk_array:
30892 case cdk_pointer:
30893 case cdk_reference:
30894 case cdk_ptrmem:
30895 return (cp_parser_check_declarator_template_parameters
30896 (parser, declarator->declarator, declarator_location));
30898 case cdk_decomp:
30899 case cdk_error:
30900 return true;
30902 default:
30903 gcc_unreachable ();
30905 return false;
30908 /* NUM_TEMPLATES were used in the current declaration. If that is
30909 invalid, return FALSE and issue an error messages. Otherwise,
30910 return TRUE. If DECLARATOR is non-NULL, then we are checking a
30911 declarator and we can print more accurate diagnostics. */
30913 static bool
30914 cp_parser_check_template_parameters (cp_parser* parser,
30915 unsigned num_templates,
30916 bool template_id_p,
30917 location_t location,
30918 cp_declarator *declarator)
30920 /* If there are the same number of template classes and parameter
30921 lists, that's OK. */
30922 if (parser->num_template_parameter_lists == num_templates)
30923 return true;
30924 /* If there are more, but only one more, and the name ends in an identifier,
30925 then we are declaring a primary template. That's OK too. */
30926 if (!template_id_p
30927 && parser->num_template_parameter_lists == num_templates + 1)
30928 return true;
30930 if (cp_parser_simulate_error (parser))
30931 return false;
30933 /* If there are more template classes than parameter lists, we have
30934 something like:
30936 template <class T> void S<T>::R<T>::f (); */
30937 if (parser->num_template_parameter_lists < num_templates)
30939 if (declarator && !current_function_decl)
30940 error_at (location, "specializing member %<%T::%E%> "
30941 "requires %<template<>%> syntax",
30942 declarator->u.id.qualifying_scope,
30943 declarator->u.id.unqualified_name);
30944 else if (declarator)
30945 error_at (location, "invalid declaration of %<%T::%E%>",
30946 declarator->u.id.qualifying_scope,
30947 declarator->u.id.unqualified_name);
30948 else
30949 error_at (location, "too few template-parameter-lists");
30950 return false;
30952 /* Otherwise, there are too many template parameter lists. We have
30953 something like:
30955 template <class T> template <class U> void S::f(); */
30956 error_at (location, "too many template-parameter-lists");
30957 return false;
30960 /* Parse an optional `::' token indicating that the following name is
30961 from the global namespace. If so, PARSER->SCOPE is set to the
30962 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
30963 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
30964 Returns the new value of PARSER->SCOPE, if the `::' token is
30965 present, and NULL_TREE otherwise. */
30967 static tree
30968 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
30970 cp_token *token;
30972 /* Peek at the next token. */
30973 token = cp_lexer_peek_token (parser->lexer);
30974 /* If we're looking at a `::' token then we're starting from the
30975 global namespace, not our current location. */
30976 if (token->type == CPP_SCOPE)
30978 /* Consume the `::' token. */
30979 cp_lexer_consume_token (parser->lexer);
30980 /* Set the SCOPE so that we know where to start the lookup. */
30981 parser->scope = global_namespace;
30982 parser->qualifying_scope = global_namespace;
30983 parser->object_scope = NULL_TREE;
30985 return parser->scope;
30987 else if (!current_scope_valid_p)
30989 parser->scope = NULL_TREE;
30990 parser->qualifying_scope = NULL_TREE;
30991 parser->object_scope = NULL_TREE;
30994 return NULL_TREE;
30997 /* Returns TRUE if the upcoming token sequence is the start of a
30998 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
30999 declarator is preceded by the `friend' specifier. The parser flags FLAGS
31000 is used to control type-specifier parsing. */
31002 static bool
31003 cp_parser_constructor_declarator_p (cp_parser *parser, cp_parser_flags flags,
31004 bool friend_p)
31006 bool constructor_p;
31007 bool outside_class_specifier_p;
31008 tree nested_name_specifier;
31009 cp_token *next_token;
31011 /* The common case is that this is not a constructor declarator, so
31012 try to avoid doing lots of work if at all possible. It's not
31013 valid declare a constructor at function scope. */
31014 if (parser->in_function_body)
31015 return false;
31016 /* And only certain tokens can begin a constructor declarator. */
31017 next_token = cp_lexer_peek_token (parser->lexer);
31018 if (next_token->type != CPP_NAME
31019 && next_token->type != CPP_SCOPE
31020 && next_token->type != CPP_NESTED_NAME_SPECIFIER
31021 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
31022 declarator-id of a constructor or destructor. */
31023 && (next_token->type != CPP_TEMPLATE_ID || cxx_dialect >= cxx20))
31024 return false;
31026 /* Parse tentatively; we are going to roll back all of the tokens
31027 consumed here. */
31028 cp_parser_parse_tentatively (parser);
31029 /* Assume that we are looking at a constructor declarator. */
31030 constructor_p = true;
31032 /* Look for the optional `::' operator. */
31033 cp_parser_global_scope_opt (parser,
31034 /*current_scope_valid_p=*/false);
31035 /* Look for the nested-name-specifier. */
31036 nested_name_specifier
31037 = (cp_parser_nested_name_specifier_opt (parser,
31038 /*typename_keyword_p=*/false,
31039 /*check_dependency_p=*/false,
31040 /*type_p=*/false,
31041 /*is_declaration=*/false));
31043 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
31044 if (nested_name_specifier
31045 && TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
31047 tree s = resolve_typename_type (nested_name_specifier,
31048 /*only_current_p=*/false);
31049 if (TREE_CODE (s) != TYPENAME_TYPE)
31050 nested_name_specifier = s;
31053 outside_class_specifier_p = (!at_class_scope_p ()
31054 || !TYPE_BEING_DEFINED (current_class_type)
31055 || friend_p);
31057 /* Outside of a class-specifier, there must be a
31058 nested-name-specifier. Except in C++17 mode, where we
31059 might be declaring a guiding declaration. */
31060 if (!nested_name_specifier && outside_class_specifier_p
31061 && cxx_dialect < cxx17)
31062 constructor_p = false;
31063 else if (nested_name_specifier == error_mark_node)
31064 constructor_p = false;
31066 /* If we have a class scope, this is easy; DR 147 says that S::S always
31067 names the constructor, and no other qualified name could. */
31068 if (constructor_p && nested_name_specifier
31069 && CLASS_TYPE_P (nested_name_specifier))
31071 tree id = cp_parser_unqualified_id (parser,
31072 /*template_keyword_p=*/false,
31073 /*check_dependency_p=*/false,
31074 /*declarator_p=*/true,
31075 /*optional_p=*/false);
31076 if (is_overloaded_fn (id))
31077 id = DECL_NAME (get_first_fn (id));
31078 if (!constructor_name_p (id, nested_name_specifier))
31079 constructor_p = false;
31081 /* If we still think that this might be a constructor-declarator,
31082 look for a class-name. */
31083 else if (constructor_p)
31085 /* If we have:
31087 template <typename T> struct S {
31088 S();
31091 we must recognize that the nested `S' names a class. */
31092 if (cxx_dialect >= cxx17)
31093 cp_parser_parse_tentatively (parser);
31095 tree type_decl;
31096 type_decl = cp_parser_class_name (parser,
31097 /*typename_keyword_p=*/false,
31098 /*template_keyword_p=*/false,
31099 none_type,
31100 /*check_dependency_p=*/false,
31101 /*class_head_p=*/false,
31102 /*is_declaration=*/false);
31104 if (cxx_dialect >= cxx17
31105 && !cp_parser_parse_definitely (parser))
31107 type_decl = NULL_TREE;
31108 tree tmpl = cp_parser_template_name (parser,
31109 /*template_keyword*/false,
31110 /*check_dependency_p*/false,
31111 /*is_declaration*/false,
31112 none_type,
31113 /*is_identifier*/NULL);
31114 if (DECL_CLASS_TEMPLATE_P (tmpl)
31115 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
31116 /* It's a deduction guide, return true. */;
31117 else
31118 cp_parser_simulate_error (parser);
31121 /* If there was no class-name, then this is not a constructor.
31122 Otherwise, if we are in a class-specifier and we aren't
31123 handling a friend declaration, check that its type matches
31124 current_class_type (c++/38313). Note: error_mark_node
31125 is left alone for error recovery purposes. */
31126 constructor_p = (!cp_parser_error_occurred (parser)
31127 && (outside_class_specifier_p
31128 || type_decl == NULL_TREE
31129 || type_decl == error_mark_node
31130 || same_type_p (current_class_type,
31131 TREE_TYPE (type_decl))));
31133 /* If we're still considering a constructor, we have to see a `(',
31134 to begin the parameter-declaration-clause, followed by either a
31135 `)', an `...', or a decl-specifier. We need to check for a
31136 type-specifier to avoid being fooled into thinking that:
31138 S (f) (int);
31140 is a constructor. (It is actually a function named `f' that
31141 takes one parameter (of type `int') and returns a value of type
31142 `S'. */
31143 if (constructor_p
31144 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31145 constructor_p = false;
31147 if (constructor_p
31148 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
31149 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
31150 /* A parameter declaration begins with a decl-specifier,
31151 which is either the "attribute" keyword, a storage class
31152 specifier, or (usually) a type-specifier. */
31153 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer)
31154 /* GNU attributes can actually appear both at the start of
31155 a parameter and parenthesized declarator.
31156 S (__attribute__((unused)) int);
31157 is a constructor, but
31158 S (__attribute__((unused)) foo) (int);
31159 is a function declaration. [[attribute]] can appear in the
31160 first form too, but not in the second form. */
31161 && !cp_next_tokens_can_be_std_attribute_p (parser))
31163 tree type;
31164 tree pushed_scope = NULL_TREE;
31165 unsigned saved_num_template_parameter_lists;
31167 if (cp_parser_allow_gnu_extensions_p (parser)
31168 && cp_next_tokens_can_be_gnu_attribute_p (parser))
31170 unsigned int n = cp_parser_skip_gnu_attributes_opt (parser, 1);
31171 while (--n)
31172 cp_lexer_consume_token (parser->lexer);
31175 /* Names appearing in the type-specifier should be looked up
31176 in the scope of the class. */
31177 if (current_class_type)
31178 type = NULL_TREE;
31179 else if (type_decl)
31181 type = TREE_TYPE (type_decl);
31182 if (TREE_CODE (type) == TYPENAME_TYPE)
31184 type = resolve_typename_type (type,
31185 /*only_current_p=*/false);
31186 if (TREE_CODE (type) == TYPENAME_TYPE)
31188 cp_parser_abort_tentative_parse (parser);
31189 return false;
31192 pushed_scope = push_scope (type);
31195 /* Inside the constructor parameter list, surrounding
31196 template-parameter-lists do not apply. */
31197 saved_num_template_parameter_lists
31198 = parser->num_template_parameter_lists;
31199 parser->num_template_parameter_lists = 0;
31201 /* Look for the type-specifier. It's not optional, but its typename
31202 might be. Unless this is a friend declaration; we don't want to
31203 treat
31205 friend S (T::fn)(int);
31207 as a constructor, but with P0634, we might assume a type when
31208 looking for the type-specifier. It is actually a function named
31209 `T::fn' that takes one parameter (of type `int') and returns a
31210 value of type `S'. Constructors can be friends, but they must
31211 use a qualified name.
31213 Parse with an empty set of declaration specifiers since we're
31214 trying to match a decl-specifier-seq of the first parameter.
31215 This must be non-null so that cp_parser_simple_type_specifier
31216 will recognize a constrained placeholder type such as:
31217 'C<int> auto' where C is a type concept. */
31218 cp_decl_specifier_seq ctor_specs;
31219 clear_decl_specs (&ctor_specs);
31220 cp_parser_type_specifier (parser,
31221 (friend_p ? CP_PARSER_FLAGS_NONE
31222 : (flags & ~CP_PARSER_FLAGS_OPTIONAL)),
31223 /*decl_specs=*/&ctor_specs,
31224 /*is_declarator=*/true,
31225 /*declares_class_or_enum=*/NULL,
31226 /*is_cv_qualifier=*/NULL);
31228 parser->num_template_parameter_lists
31229 = saved_num_template_parameter_lists;
31231 /* Leave the scope of the class. */
31232 if (pushed_scope)
31233 pop_scope (pushed_scope);
31235 constructor_p = !cp_parser_error_occurred (parser);
31239 /* We did not really want to consume any tokens. */
31240 cp_parser_abort_tentative_parse (parser);
31242 return constructor_p;
31245 /* Parse the definition of the function given by the DECL_SPECIFIERS,
31246 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
31247 they must be performed once we are in the scope of the function.
31249 Returns the function defined. */
31251 static tree
31252 cp_parser_function_definition_from_specifiers_and_declarator
31253 (cp_parser* parser,
31254 cp_decl_specifier_seq *decl_specifiers,
31255 tree attributes,
31256 const cp_declarator *declarator)
31258 tree fn;
31259 bool success_p;
31261 /* Begin the function-definition. */
31262 success_p = start_function (decl_specifiers, declarator, attributes);
31264 /* The things we're about to see are not directly qualified by any
31265 template headers we've seen thus far. */
31266 reset_specialization ();
31268 /* If there were names looked up in the decl-specifier-seq that we
31269 did not check, check them now. We must wait until we are in the
31270 scope of the function to perform the checks, since the function
31271 might be a friend. */
31272 perform_deferred_access_checks (tf_warning_or_error);
31274 if (success_p)
31276 cp_finalize_omp_declare_simd (parser, current_function_decl);
31277 parser->omp_declare_simd = NULL;
31278 cp_finalize_oacc_routine (parser, current_function_decl, true);
31279 parser->oacc_routine = NULL;
31282 if (!success_p)
31284 /* Skip the entire function. */
31285 cp_parser_skip_to_end_of_block_or_statement (parser);
31286 fn = error_mark_node;
31288 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
31290 /* Seen already, skip it. An error message has already been output. */
31291 cp_parser_skip_to_end_of_block_or_statement (parser);
31292 fn = current_function_decl;
31293 current_function_decl = NULL_TREE;
31294 /* If this is a function from a class, pop the nested class. */
31295 if (current_class_name)
31296 pop_nested_class ();
31298 else
31300 auto_timevar tv (DECL_DECLARED_INLINE_P (current_function_decl)
31301 ? TV_PARSE_INLINE : TV_PARSE_FUNC);
31302 fn = cp_parser_function_definition_after_declarator (parser,
31303 /*inline_p=*/false);
31306 return fn;
31309 /* Parse the part of a function-definition that follows the
31310 declarator. INLINE_P is TRUE iff this function is an inline
31311 function defined within a class-specifier.
31313 Returns the function defined. */
31315 static tree
31316 cp_parser_function_definition_after_declarator (cp_parser* parser,
31317 bool inline_p)
31319 tree fn;
31320 bool saved_in_unbraced_linkage_specification_p;
31321 bool saved_in_function_body;
31322 unsigned saved_num_template_parameter_lists;
31323 cp_token *token;
31324 bool fully_implicit_function_template_p
31325 = parser->fully_implicit_function_template_p;
31326 parser->fully_implicit_function_template_p = false;
31327 tree implicit_template_parms
31328 = parser->implicit_template_parms;
31329 parser->implicit_template_parms = 0;
31330 cp_binding_level* implicit_template_scope
31331 = parser->implicit_template_scope;
31332 parser->implicit_template_scope = 0;
31334 saved_in_function_body = parser->in_function_body;
31335 parser->in_function_body = true;
31336 /* If the next token is `return', then the code may be trying to
31337 make use of the "named return value" extension that G++ used to
31338 support. */
31339 token = cp_lexer_peek_token (parser->lexer);
31340 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
31342 /* Consume the `return' keyword. */
31343 cp_lexer_consume_token (parser->lexer);
31344 /* Look for the identifier that indicates what value is to be
31345 returned. */
31346 cp_parser_identifier (parser);
31347 /* Issue an error message. */
31348 error_at (token->location,
31349 "named return values are no longer supported");
31350 /* Skip tokens until we reach the start of the function body. */
31351 while (true)
31353 cp_token *token = cp_lexer_peek_token (parser->lexer);
31354 if (token->type == CPP_OPEN_BRACE
31355 || token->type == CPP_EOF
31356 || token->type == CPP_PRAGMA_EOL)
31357 break;
31358 cp_lexer_consume_token (parser->lexer);
31361 /* The `extern' in `extern "C" void f () { ... }' does not apply to
31362 anything declared inside `f'. */
31363 saved_in_unbraced_linkage_specification_p
31364 = parser->in_unbraced_linkage_specification_p;
31365 parser->in_unbraced_linkage_specification_p = false;
31366 /* Inside the function, surrounding template-parameter-lists do not
31367 apply. */
31368 saved_num_template_parameter_lists
31369 = parser->num_template_parameter_lists;
31370 parser->num_template_parameter_lists = 0;
31372 /* If the next token is `try', `__transaction_atomic', or
31373 `__transaction_relaxed`, then we are looking at either function-try-block
31374 or function-transaction-block. Note that all of these include the
31375 function-body. */
31376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
31377 cp_parser_function_transaction (parser, RID_TRANSACTION_ATOMIC);
31378 else if (cp_lexer_next_token_is_keyword (parser->lexer,
31379 RID_TRANSACTION_RELAXED))
31380 cp_parser_function_transaction (parser, RID_TRANSACTION_RELAXED);
31381 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
31382 cp_parser_function_try_block (parser);
31383 else
31384 cp_parser_ctor_initializer_opt_and_function_body
31385 (parser, /*in_function_try_block=*/false);
31387 /* Finish the function. */
31388 fn = finish_function (inline_p);
31390 if (modules_p ()
31391 && !inline_p
31392 && TYPE_P (DECL_CONTEXT (fn))
31393 && (DECL_DECLARED_INLINE_P (fn)
31394 || processing_template_decl))
31395 set_defining_module (fn);
31397 /* Generate code for it, if necessary. */
31398 expand_or_defer_fn (fn);
31400 /* Restore the saved values. */
31401 parser->in_unbraced_linkage_specification_p
31402 = saved_in_unbraced_linkage_specification_p;
31403 parser->num_template_parameter_lists
31404 = saved_num_template_parameter_lists;
31405 parser->in_function_body = saved_in_function_body;
31407 parser->fully_implicit_function_template_p
31408 = fully_implicit_function_template_p;
31409 parser->implicit_template_parms
31410 = implicit_template_parms;
31411 parser->implicit_template_scope
31412 = implicit_template_scope;
31414 if (parser->fully_implicit_function_template_p)
31415 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
31417 return fn;
31420 /* Parse a template-declaration body (following argument list). */
31422 static void
31423 cp_parser_template_declaration_after_parameters (cp_parser* parser,
31424 tree parameter_list,
31425 bool member_p)
31427 tree decl = NULL_TREE;
31428 bool friend_p = false;
31430 /* We just processed one more parameter list. */
31431 ++parser->num_template_parameter_lists;
31433 /* Get the deferred access checks from the parameter list. These
31434 will be checked once we know what is being declared, as for a
31435 member template the checks must be performed in the scope of the
31436 class containing the member. */
31437 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
31439 /* Tentatively parse for a new template parameter list, which can either be
31440 the template keyword or a template introduction. */
31441 if (cp_parser_template_declaration_after_export (parser, member_p))
31442 /* OK */;
31443 else if (cxx_dialect >= cxx11
31444 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
31445 decl = cp_parser_alias_declaration (parser);
31446 else if (cxx_dialect >= cxx20 /* Implies flag_concept. */
31447 && cp_lexer_next_token_is_keyword (parser->lexer, RID_CONCEPT)
31448 && !cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_BOOL))
31449 /* Allow 'concept bool' to be handled as per the TS. */
31450 decl = cp_parser_concept_definition (parser);
31451 else
31453 cp_token *token = cp_lexer_peek_token (parser->lexer);
31454 decl = cp_parser_single_declaration (parser,
31455 checks,
31456 member_p,
31457 /*explicit_specialization_p=*/false,
31458 &friend_p);
31460 /* If this is a member template declaration, let the front
31461 end know. */
31462 if (member_p && !friend_p && decl)
31464 if (TREE_CODE (decl) == TYPE_DECL)
31465 cp_parser_check_access_in_redeclaration (decl, token->location);
31467 decl = finish_member_template_decl (decl);
31469 else if (friend_p && decl
31470 && DECL_DECLARES_TYPE_P (decl))
31471 make_friend_class (current_class_type, TREE_TYPE (decl),
31472 /*complain=*/true);
31474 /* We are done with the current parameter list. */
31475 --parser->num_template_parameter_lists;
31477 pop_deferring_access_checks ();
31479 /* Finish up. */
31480 finish_template_decl (parameter_list);
31482 /* Check the template arguments for a literal operator template. */
31483 if (decl
31484 && DECL_DECLARES_FUNCTION_P (decl)
31485 && UDLIT_OPER_P (DECL_NAME (decl)))
31487 bool ok = true;
31488 if (parameter_list == NULL_TREE)
31489 ok = false;
31490 else
31492 int num_parms = TREE_VEC_LENGTH (parameter_list);
31493 if (num_parms == 1)
31495 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
31496 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31497 if (TREE_CODE (parm) != PARM_DECL)
31498 ok = false;
31499 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))
31500 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31501 /* OK, C++20 string literal operator template. We don't need
31502 to warn in lower dialects here because we will have already
31503 warned about the template parameter. */;
31504 else if (TREE_TYPE (parm) != char_type_node
31505 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31506 ok = false;
31508 else if (num_parms == 2 && cxx_dialect >= cxx14)
31510 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
31511 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
31512 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
31513 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
31514 if (TREE_CODE (parm) != PARM_DECL
31515 || TREE_TYPE (parm) != TREE_TYPE (type)
31516 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
31517 ok = false;
31518 else
31519 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
31520 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic,
31521 "ISO C++ did not adopt string literal operator templa"
31522 "tes taking an argument pack of characters");
31524 else
31525 ok = false;
31527 if (!ok)
31529 if (cxx_dialect > cxx17)
31530 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31531 "template %qD has invalid parameter list; expected "
31532 "non-type template parameter pack %<<char...>%> or "
31533 "single non-type parameter of class type",
31534 decl);
31535 else
31536 error_at (DECL_SOURCE_LOCATION (decl), "literal operator "
31537 "template %qD has invalid parameter list; expected "
31538 "non-type template parameter pack %<<char...>%>",
31539 decl);
31543 /* Register member declarations. */
31544 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
31545 finish_member_declaration (decl);
31546 /* If DECL is a function template, we must return to parse it later.
31547 (Even though there is no definition, there might be default
31548 arguments that need handling.) */
31549 if (member_p && decl
31550 && DECL_DECLARES_FUNCTION_P (decl))
31551 vec_safe_push (unparsed_funs_with_definitions, decl);
31554 /* Parse a template introduction header for a template-declaration. Returns
31555 false if tentative parse fails. */
31557 static bool
31558 cp_parser_template_introduction (cp_parser* parser, bool member_p)
31560 cp_parser_parse_tentatively (parser);
31562 tree saved_scope = parser->scope;
31563 tree saved_object_scope = parser->object_scope;
31564 tree saved_qualifying_scope = parser->qualifying_scope;
31565 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31567 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
31569 /* In classes don't parse valid unnamed bitfields as invalid
31570 template introductions. */
31571 if (member_p)
31572 parser->colon_corrects_to_scope_p = false;
31574 /* Look for the optional `::' operator. */
31575 cp_parser_global_scope_opt (parser,
31576 /*current_scope_valid_p=*/false);
31577 /* Look for the nested-name-specifier. */
31578 cp_parser_nested_name_specifier_opt (parser,
31579 /*typename_keyword_p=*/false,
31580 /*check_dependency_p=*/true,
31581 /*type_p=*/false,
31582 /*is_declaration=*/false);
31584 cp_token *token = cp_lexer_peek_token (parser->lexer);
31585 tree concept_name = cp_parser_identifier (parser);
31587 /* Look up the concept for which we will be matching
31588 template parameters. */
31589 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
31590 token->location);
31591 parser->scope = saved_scope;
31592 parser->object_scope = saved_object_scope;
31593 parser->qualifying_scope = saved_qualifying_scope;
31594 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31596 if (concept_name == error_mark_node
31597 || (seen_error () && !concept_definition_p (tmpl_decl)))
31598 cp_parser_simulate_error (parser);
31600 /* Look for opening brace for introduction. */
31601 matching_braces braces;
31602 braces.require_open (parser);
31603 location_t open_loc = input_location;
31605 if (!cp_parser_parse_definitely (parser))
31606 return false;
31608 push_deferring_access_checks (dk_deferred);
31610 /* Build vector of placeholder parameters and grab
31611 matching identifiers. */
31612 tree introduction_list = cp_parser_introduction_list (parser);
31614 /* Look for closing brace for introduction. */
31615 if (!braces.require_close (parser))
31616 return true;
31618 /* The introduction-list shall not be empty. */
31619 int nargs = TREE_VEC_LENGTH (introduction_list);
31620 if (nargs == 0)
31622 /* In cp_parser_introduction_list we have already issued an error. */
31623 return true;
31626 if (tmpl_decl == error_mark_node)
31628 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
31629 token->location);
31630 return true;
31633 /* Build and associate the constraint. */
31634 location_t introduction_loc = make_location (open_loc,
31635 start_token->location,
31636 parser->lexer);
31637 tree parms = finish_template_introduction (tmpl_decl,
31638 introduction_list,
31639 introduction_loc);
31640 if (parms && parms != error_mark_node)
31642 if (!flag_concepts_ts)
31643 pedwarn (introduction_loc, 0, "template-introductions"
31644 " are not part of C++20 concepts; use %qs to enable",
31645 "-fconcepts-ts");
31647 cp_parser_template_declaration_after_parameters (parser, parms,
31648 member_p);
31649 return true;
31652 if (parms == NULL_TREE)
31653 error_at (token->location, "no matching concept for template-introduction");
31655 return true;
31658 /* Parse a normal template-declaration following the template keyword. */
31660 static void
31661 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
31663 tree parameter_list;
31664 bool need_lang_pop;
31665 location_t location = input_location;
31667 /* Look for the `<' token. */
31668 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
31669 return;
31670 if (at_class_scope_p () && current_function_decl)
31672 /* 14.5.2.2 [temp.mem]
31674 A local class shall not have member templates. */
31675 error_at (location,
31676 "invalid declaration of member template in local class");
31677 cp_parser_skip_to_end_of_block_or_statement (parser);
31678 return;
31680 /* [temp]
31682 A template ... shall not have C linkage. */
31683 if (current_lang_name == lang_name_c)
31685 error_at (location, "template with C linkage");
31686 maybe_show_extern_c_location ();
31687 /* Give it C++ linkage to avoid confusing other parts of the
31688 front end. */
31689 push_lang_context (lang_name_cplusplus);
31690 need_lang_pop = true;
31692 else
31693 need_lang_pop = false;
31695 /* We cannot perform access checks on the template parameter
31696 declarations until we know what is being declared, just as we
31697 cannot check the decl-specifier list. */
31698 push_deferring_access_checks (dk_deferred);
31700 /* If the next token is `>', then we have an invalid
31701 specialization. Rather than complain about an invalid template
31702 parameter, issue an error message here. */
31703 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
31705 cp_parser_error (parser, "invalid explicit specialization");
31706 begin_specialization ();
31707 parameter_list = NULL_TREE;
31709 else
31711 /* Parse the template parameters. */
31712 parameter_list = cp_parser_template_parameter_list (parser);
31715 /* Look for the `>'. */
31716 cp_parser_require_end_of_template_parameter_list (parser);
31718 /* Manage template requirements */
31719 if (flag_concepts)
31721 tree reqs = get_shorthand_constraints (current_template_parms);
31722 if (tree treqs = cp_parser_requires_clause_opt (parser, false))
31723 reqs = combine_constraint_expressions (reqs, treqs);
31724 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
31727 cp_parser_template_declaration_after_parameters (parser, parameter_list,
31728 member_p);
31730 /* For the erroneous case of a template with C linkage, we pushed an
31731 implicit C++ linkage scope; exit that scope now. */
31732 if (need_lang_pop)
31733 pop_lang_context ();
31736 /* Parse a template-declaration, assuming that the `export' (and
31737 `extern') keywords, if present, has already been scanned. MEMBER_P
31738 is as for cp_parser_template_declaration. */
31740 static bool
31741 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
31743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
31745 cp_lexer_consume_token (parser->lexer);
31746 cp_parser_explicit_template_declaration (parser, member_p);
31747 return true;
31749 else if (flag_concepts)
31750 return cp_parser_template_introduction (parser, member_p);
31752 return false;
31755 /* Perform the deferred access checks from a template-parameter-list.
31756 CHECKS is a TREE_LIST of access checks, as returned by
31757 get_deferred_access_checks. */
31759 static void
31760 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
31762 ++processing_template_parmlist;
31763 perform_access_checks (checks, tf_warning_or_error);
31764 --processing_template_parmlist;
31767 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
31768 `function-definition' sequence that follows a template header.
31769 If MEMBER_P is true, this declaration appears in a class scope.
31771 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
31772 *FRIEND_P is set to TRUE iff the declaration is a friend. */
31774 static tree
31775 cp_parser_single_declaration (cp_parser* parser,
31776 vec<deferred_access_check, va_gc> *checks,
31777 bool member_p,
31778 bool explicit_specialization_p,
31779 bool* friend_p)
31781 int declares_class_or_enum;
31782 tree decl = NULL_TREE;
31783 cp_decl_specifier_seq decl_specifiers;
31784 bool function_definition_p = false;
31785 cp_token *decl_spec_token_start;
31787 /* This function is only used when processing a template
31788 declaration. */
31789 gcc_assert (innermost_scope_kind () == sk_template_parms
31790 || innermost_scope_kind () == sk_template_spec);
31792 /* Defer access checks until we know what is being declared. */
31793 push_deferring_access_checks (dk_deferred);
31795 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
31796 alternative. */
31797 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
31798 cp_parser_decl_specifier_seq (parser,
31799 (CP_PARSER_FLAGS_OPTIONAL
31800 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL),
31801 &decl_specifiers,
31802 &declares_class_or_enum);
31804 cp_omp_declare_simd_data odsd;
31805 if (decl_specifiers.attributes && (flag_openmp || flag_openmp_simd))
31806 cp_parser_handle_directive_omp_attributes (parser,
31807 &decl_specifiers.attributes,
31808 &odsd, true);
31810 if (friend_p)
31811 *friend_p = cp_parser_friend_p (&decl_specifiers);
31813 /* There are no template typedefs. */
31814 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
31816 error_at (decl_spec_token_start->location,
31817 "template declaration of %<typedef%>");
31818 decl = error_mark_node;
31821 /* Gather up the access checks that occurred the
31822 decl-specifier-seq. */
31823 stop_deferring_access_checks ();
31825 /* Check for the declaration of a template class. */
31826 if (declares_class_or_enum)
31828 if (cp_parser_declares_only_class_p (parser)
31829 || (declares_class_or_enum & 2))
31831 decl = shadow_tag (&decl_specifiers);
31833 /* In this case:
31835 struct C {
31836 friend template <typename T> struct A<T>::B;
31839 A<T>::B will be represented by a TYPENAME_TYPE, and
31840 therefore not recognized by shadow_tag. */
31841 if (friend_p && *friend_p
31842 && !decl
31843 && decl_specifiers.type
31844 && TYPE_P (decl_specifiers.type))
31845 decl = decl_specifiers.type;
31847 if (decl && decl != error_mark_node)
31848 decl = TYPE_NAME (decl);
31849 else
31850 decl = error_mark_node;
31852 /* If this is a declaration, but not a definition, associate
31853 any constraints with the type declaration. Constraints
31854 are associated with definitions in cp_parser_class_specifier. */
31855 if (declares_class_or_enum == 1)
31856 associate_classtype_constraints (TREE_TYPE (decl));
31858 /* Perform access checks for template parameters. */
31859 cp_parser_perform_template_parameter_access_checks (checks);
31861 /* Give a helpful diagnostic for
31862 template <class T> struct A { } a;
31863 if we aren't already recovering from an error. */
31864 if (!cp_parser_declares_only_class_p (parser)
31865 && !seen_error ())
31867 error_at (cp_lexer_peek_token (parser->lexer)->location,
31868 "a class template declaration must not declare "
31869 "anything else");
31870 cp_parser_skip_to_end_of_block_or_statement (parser);
31871 goto out;
31876 /* Complain about missing 'typename' or other invalid type names. */
31877 if (!decl_specifiers.any_type_specifiers_p
31878 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
31880 /* cp_parser_parse_and_diagnose_invalid_type_name calls
31881 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
31882 the rest of this declaration. */
31883 decl = error_mark_node;
31884 goto out;
31887 /* If it's not a template class, try for a template function. If
31888 the next token is a `;', then this declaration does not declare
31889 anything. But, if there were errors in the decl-specifiers, then
31890 the error might well have come from an attempted class-specifier.
31891 In that case, there's no need to warn about a missing declarator. */
31892 if (!decl
31893 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
31894 || decl_specifiers.type != error_mark_node))
31896 int flags = CP_PARSER_FLAGS_TYPENAME_OPTIONAL;
31897 /* We don't delay parsing for friends, though CWG 2510 may change
31898 that. */
31899 if (member_p && !(friend_p && *friend_p))
31900 flags |= CP_PARSER_FLAGS_DELAY_NOEXCEPT;
31901 decl = cp_parser_init_declarator (parser,
31902 flags,
31903 &decl_specifiers,
31904 checks,
31905 /*function_definition_allowed_p=*/true,
31906 member_p,
31907 declares_class_or_enum,
31908 &function_definition_p,
31909 NULL, NULL, NULL);
31911 /* 7.1.1-1 [dcl.stc]
31913 A storage-class-specifier shall not be specified in an explicit
31914 specialization... */
31915 if (decl
31916 && explicit_specialization_p
31917 && decl_specifiers.storage_class != sc_none)
31919 error_at (decl_spec_token_start->location,
31920 "explicit template specialization cannot have a storage class");
31921 decl = error_mark_node;
31924 if (decl && VAR_P (decl))
31925 check_template_variable (decl);
31928 /* Look for a trailing `;' after the declaration. */
31929 if (!function_definition_p
31930 && (decl == error_mark_node
31931 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
31932 cp_parser_skip_to_end_of_block_or_statement (parser);
31934 out:
31935 pop_deferring_access_checks ();
31937 /* Clear any current qualification; whatever comes next is the start
31938 of something new. */
31939 parser->scope = NULL_TREE;
31940 parser->qualifying_scope = NULL_TREE;
31941 parser->object_scope = NULL_TREE;
31943 cp_finalize_omp_declare_simd (parser, &odsd);
31945 return decl;
31948 /* Parse a cast-expression that is not the operand of a unary "&". */
31950 static cp_expr
31951 cp_parser_simple_cast_expression (cp_parser *parser)
31953 return cp_parser_cast_expression (parser, /*address_p=*/false,
31954 /*cast_p=*/false, /*decltype*/false, NULL);
31957 /* Parse a functional cast to TYPE. Returns an expression
31958 representing the cast. */
31960 static cp_expr
31961 cp_parser_functional_cast (cp_parser* parser, tree type)
31963 vec<tree, va_gc> *vec;
31964 tree expression_list;
31965 cp_expr cast;
31966 bool nonconst_p;
31968 location_t start_loc = input_location;
31970 if (!type)
31971 type = error_mark_node;
31973 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
31975 cp_lexer_set_source_position (parser->lexer);
31976 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
31977 expression_list = cp_parser_braced_list (parser, &nonconst_p);
31978 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
31979 if (TREE_CODE (type) == TYPE_DECL)
31980 type = TREE_TYPE (type);
31982 cast = finish_compound_literal (type, expression_list,
31983 tf_warning_or_error, fcl_functional);
31984 /* Create a location of the form:
31985 type_name{i, f}
31986 ^~~~~~~~~~~~~~~
31987 with caret == start at the start of the type name,
31988 finishing at the closing brace. */
31989 location_t combined_loc = make_location (start_loc, start_loc,
31990 parser->lexer);
31991 cast.set_location (combined_loc);
31992 return cast;
31996 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
31997 /*cast_p=*/true,
31998 /*allow_expansion_p=*/true,
31999 /*non_constant_p=*/NULL);
32000 if (vec == NULL)
32001 expression_list = error_mark_node;
32002 else
32004 expression_list = build_tree_list_vec (vec);
32005 release_tree_vector (vec);
32008 /* Create a location of the form:
32009 float(i)
32010 ^~~~~~~~
32011 with caret == start at the start of the type name,
32012 finishing at the closing paren. */
32013 location_t combined_loc = make_location (start_loc, start_loc,
32014 parser->lexer);
32015 cast = build_functional_cast (combined_loc, type, expression_list,
32016 tf_warning_or_error);
32018 /* [expr.const]/1: In an integral constant expression "only type
32019 conversions to integral or enumeration type can be used". */
32020 if (TREE_CODE (type) == TYPE_DECL)
32021 type = TREE_TYPE (type);
32022 if (cast != error_mark_node
32023 && !cast_valid_in_integral_constant_expression_p (type)
32024 && cp_parser_non_integral_constant_expression (parser,
32025 NIC_CONSTRUCTOR))
32026 return error_mark_node;
32028 return cast;
32031 /* Save the tokens that make up the body of a member function defined
32032 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
32033 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
32034 specifiers applied to the declaration. Returns the FUNCTION_DECL
32035 for the member function. */
32037 static tree
32038 cp_parser_save_member_function_body (cp_parser* parser,
32039 cp_decl_specifier_seq *decl_specifiers,
32040 cp_declarator *declarator,
32041 tree attributes)
32043 cp_token *first;
32044 cp_token *last;
32045 tree fn;
32046 bool function_try_block = false;
32048 /* Create the FUNCTION_DECL. */
32049 fn = grokmethod (decl_specifiers, declarator, attributes);
32050 cp_finalize_omp_declare_simd (parser, fn);
32051 cp_finalize_oacc_routine (parser, fn, true);
32052 /* If something went badly wrong, bail out now. */
32053 if (fn == error_mark_node)
32055 /* If there's a function-body, skip it. */
32056 if (cp_parser_token_starts_function_definition_p
32057 (cp_lexer_peek_token (parser->lexer)))
32058 cp_parser_skip_to_end_of_block_or_statement (parser);
32059 return error_mark_node;
32062 /* Remember it, if there are default args to post process. */
32063 cp_parser_save_default_args (parser, fn);
32065 /* Save away the tokens that make up the body of the
32066 function. */
32067 first = parser->lexer->next_token;
32069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
32070 cp_lexer_consume_token (parser->lexer);
32071 else if (cp_lexer_next_token_is_keyword (parser->lexer,
32072 RID_TRANSACTION_ATOMIC))
32074 cp_lexer_consume_token (parser->lexer);
32075 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
32076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
32077 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
32078 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
32079 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
32080 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
32081 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
32083 cp_lexer_consume_token (parser->lexer);
32084 cp_lexer_consume_token (parser->lexer);
32085 cp_lexer_consume_token (parser->lexer);
32086 cp_lexer_consume_token (parser->lexer);
32087 cp_lexer_consume_token (parser->lexer);
32089 else
32090 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
32091 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32093 cp_lexer_consume_token (parser->lexer);
32094 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32095 break;
32099 /* Handle function try blocks. */
32100 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
32102 cp_lexer_consume_token (parser->lexer);
32103 function_try_block = true;
32105 /* We can have braced-init-list mem-initializers before the fn body. */
32106 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
32108 cp_lexer_consume_token (parser->lexer);
32109 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
32111 /* cache_group will stop after an un-nested { } pair, too. */
32112 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
32113 break;
32115 /* variadic mem-inits have ... after the ')'. */
32116 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32117 cp_lexer_consume_token (parser->lexer);
32120 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32121 /* Handle function try blocks. */
32122 if (function_try_block)
32123 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
32124 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
32125 last = parser->lexer->next_token;
32127 /* Save away the inline definition; we will process it when the
32128 class is complete. */
32129 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
32130 DECL_PENDING_INLINE_P (fn) = 1;
32132 /* We need to know that this was defined in the class, so that
32133 friend templates are handled correctly. */
32134 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
32136 /* Add FN to the queue of functions to be parsed later. */
32137 vec_safe_push (unparsed_funs_with_definitions, fn);
32139 return fn;
32142 /* Save the tokens that make up the in-class initializer for a non-static
32143 data member. Returns a DEFERRED_PARSE. */
32145 static tree
32146 cp_parser_save_nsdmi (cp_parser* parser)
32148 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
32151 /* Parse a template-argument-list, as well as the trailing ">" (but
32152 not the opening "<"). See cp_parser_template_argument_list for the
32153 return value. */
32155 static tree
32156 cp_parser_enclosed_template_argument_list (cp_parser* parser)
32158 tree arguments;
32159 tree saved_scope;
32160 tree saved_qualifying_scope;
32161 tree saved_object_scope;
32162 bool saved_greater_than_is_operator_p;
32164 /* [temp.names]
32166 When parsing a template-id, the first non-nested `>' is taken as
32167 the end of the template-argument-list rather than a greater-than
32168 operator. */
32169 saved_greater_than_is_operator_p
32170 = parser->greater_than_is_operator_p;
32171 parser->greater_than_is_operator_p = false;
32172 /* Parsing the argument list may modify SCOPE, so we save it
32173 here. */
32174 saved_scope = parser->scope;
32175 saved_qualifying_scope = parser->qualifying_scope;
32176 saved_object_scope = parser->object_scope;
32177 /* We need to evaluate the template arguments, even though this
32178 template-id may be nested within a "sizeof". */
32179 cp_evaluated ev;
32180 /* Parse the template-argument-list itself. */
32181 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
32182 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT)
32183 || cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32184 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32186 arguments = make_tree_vec (0);
32187 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (arguments, 0);
32189 else
32190 arguments = cp_parser_template_argument_list (parser);
32191 /* Look for the `>' that ends the template-argument-list. If we find
32192 a '>>' instead, it's probably just a typo. */
32193 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
32195 if (cxx_dialect != cxx98)
32197 /* In C++0x, a `>>' in a template argument list or cast
32198 expression is considered to be two separate `>'
32199 tokens. So, change the current token to a `>', but don't
32200 consume it: it will be consumed later when the outer
32201 template argument list (or cast expression) is parsed.
32202 Note that this replacement of `>' for `>>' is necessary
32203 even if we are parsing tentatively: in the tentative
32204 case, after calling
32205 cp_parser_enclosed_template_argument_list we will always
32206 throw away all of the template arguments and the first
32207 closing `>', either because the template argument list
32208 was erroneous or because we are replacing those tokens
32209 with a CPP_TEMPLATE_ID token. The second `>' (which will
32210 not have been thrown away) is needed either to close an
32211 outer template argument list or to complete a new-style
32212 cast. */
32213 cp_token *token = cp_lexer_peek_token (parser->lexer);
32214 token->type = CPP_GREATER;
32216 else if (!saved_greater_than_is_operator_p)
32218 /* If we're in a nested template argument list, the '>>' has
32219 to be a typo for '> >'. We emit the error message, but we
32220 continue parsing and we push a '>' as next token, so that
32221 the argument list will be parsed correctly. Note that the
32222 global source location is still on the token before the
32223 '>>', so we need to say explicitly where we want it. */
32224 cp_token *token = cp_lexer_peek_token (parser->lexer);
32225 gcc_rich_location richloc (token->location);
32226 richloc.add_fixit_replace ("> >");
32227 error_at (&richloc, "%<>>%> should be %<> >%> "
32228 "within a nested template argument list");
32230 token->type = CPP_GREATER;
32232 else
32234 /* If this is not a nested template argument list, the '>>'
32235 is a typo for '>'. Emit an error message and continue.
32236 Same deal about the token location, but here we can get it
32237 right by consuming the '>>' before issuing the diagnostic. */
32238 cp_token *token = cp_lexer_consume_token (parser->lexer);
32239 error_at (token->location,
32240 "spurious %<>>%>, use %<>%> to terminate "
32241 "a template argument list");
32244 /* Similarly for >>= and >=. */
32245 else if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER_EQ)
32246 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT_EQ))
32248 cp_token *token = cp_lexer_consume_token (parser->lexer);
32249 gcc_rich_location richloc (token->location);
32250 enum cpp_ttype new_type;
32251 const char *replacement;
32252 if (token->type == CPP_GREATER_EQ)
32254 replacement = "> =";
32255 new_type = CPP_EQ;
32257 else if (!saved_greater_than_is_operator_p)
32259 if (cxx_dialect != cxx98)
32260 replacement = ">> =";
32261 else
32262 replacement = "> > =";
32263 new_type = CPP_GREATER;
32265 else
32267 replacement = "> >=";
32268 new_type = CPP_GREATER_EQ;
32270 richloc.add_fixit_replace (replacement);
32271 error_at (&richloc, "%qs should be %qs to terminate a template "
32272 "argument list",
32273 cpp_type2name (token->type, token->flags), replacement);
32274 token->type = new_type;
32276 else
32277 cp_parser_require_end_of_template_parameter_list (parser);
32278 /* The `>' token might be a greater-than operator again now. */
32279 parser->greater_than_is_operator_p
32280 = saved_greater_than_is_operator_p;
32281 /* Restore the SAVED_SCOPE. */
32282 parser->scope = saved_scope;
32283 parser->qualifying_scope = saved_qualifying_scope;
32284 parser->object_scope = saved_object_scope;
32286 return arguments;
32289 /* MEMBER_FUNCTION is a member function, or a friend. If default
32290 arguments, or the body of the function have not yet been parsed,
32291 parse them now. */
32293 static void
32294 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
32296 auto_timevar tv (TV_PARSE_INMETH);
32298 /* If this member is a template, get the underlying
32299 FUNCTION_DECL. */
32300 if (DECL_FUNCTION_TEMPLATE_P (member_function))
32301 member_function = DECL_TEMPLATE_RESULT (member_function);
32303 /* There should not be any class definitions in progress at this
32304 point; the bodies of members are only parsed outside of all class
32305 definitions. */
32306 gcc_assert (parser->num_classes_being_defined == 0);
32307 /* While we're parsing the member functions we might encounter more
32308 classes. We want to handle them right away, but we don't want
32309 them getting mixed up with functions that are currently in the
32310 queue. */
32311 push_unparsed_function_queues (parser);
32313 /* Make sure that any template parameters are in scope. */
32314 maybe_begin_member_template_processing (member_function);
32316 /* If the body of the function has not yet been parsed, parse it
32317 now. Except if the tokens have been purged (PR c++/39751). */
32318 if (DECL_PENDING_INLINE_P (member_function)
32319 && !DECL_PENDING_INLINE_INFO (member_function)->first->purged_p)
32321 tree function_scope;
32322 cp_token_cache *tokens;
32324 /* The function is no longer pending; we are processing it. */
32325 tokens = DECL_PENDING_INLINE_INFO (member_function);
32326 DECL_PENDING_INLINE_INFO (member_function) = NULL;
32327 DECL_PENDING_INLINE_P (member_function) = 0;
32329 /* If this is a local class, enter the scope of the containing
32330 function. */
32331 function_scope = current_function_decl;
32332 if (function_scope)
32333 push_function_context ();
32335 /* Push the body of the function onto the lexer stack. */
32336 cp_parser_push_lexer_for_tokens (parser, tokens);
32338 /* Let the front end know that we going to be defining this
32339 function. */
32340 start_preparsed_function (member_function, NULL_TREE,
32341 SF_PRE_PARSED | SF_INCLASS_INLINE);
32343 /* #pragma omp declare reduction needs special parsing. */
32344 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
32346 parser->lexer->in_pragma = true;
32347 cp_parser_omp_declare_reduction_exprs (member_function, parser);
32348 finish_function (/*inline_p=*/true);
32349 cp_check_omp_declare_reduction (member_function);
32351 else
32352 /* Now, parse the body of the function. */
32353 cp_parser_function_definition_after_declarator (parser,
32354 /*inline_p=*/true);
32356 /* Leave the scope of the containing function. */
32357 if (function_scope)
32358 pop_function_context ();
32359 cp_parser_pop_lexer (parser);
32362 /* Remove any template parameters from the symbol table. */
32363 maybe_end_member_template_processing ();
32365 /* Restore the queue. */
32366 pop_unparsed_function_queues (parser);
32369 /* If DECL contains any default args, remember it on the unparsed
32370 functions queue. */
32372 static void
32373 cp_parser_save_default_args (cp_parser* parser, tree decl)
32375 tree probe;
32377 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
32378 probe;
32379 probe = TREE_CHAIN (probe))
32380 if (TREE_PURPOSE (probe))
32382 cp_default_arg_entry entry = {current_class_type, decl};
32383 vec_safe_push (unparsed_funs_with_default_args, entry);
32384 break;
32387 /* Remember if there is a noexcept-specifier to post process. */
32388 tree spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl));
32389 if (UNPARSED_NOEXCEPT_SPEC_P (spec))
32390 vec_safe_push (unparsed_noexcepts, decl);
32393 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
32394 which is either a FIELD_DECL or PARM_DECL. Parse it and return
32395 the result. For a PARM_DECL, PARMTYPE is the corresponding type
32396 from the parameter-type-list. */
32398 static tree
32399 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
32400 tree default_arg, tree parmtype)
32402 cp_token_cache *tokens;
32403 tree parsed_arg;
32404 bool dummy;
32406 if (default_arg == error_mark_node)
32407 return error_mark_node;
32409 /* Push the saved tokens for the default argument onto the parser's
32410 lexer stack. */
32411 tokens = DEFPARSE_TOKENS (default_arg);
32412 cp_parser_push_lexer_for_tokens (parser, tokens);
32414 start_lambda_scope (decl);
32416 /* Parse the default argument. */
32417 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
32418 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
32419 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
32421 finish_lambda_scope ();
32423 if (parsed_arg == error_mark_node)
32424 cp_parser_skip_to_end_of_statement (parser);
32426 if (!processing_template_decl)
32428 /* In a non-template class, check conversions now. In a template,
32429 we'll wait and instantiate these as needed. */
32430 if (TREE_CODE (decl) == PARM_DECL)
32431 parsed_arg = check_default_argument (parmtype, parsed_arg,
32432 tf_warning_or_error);
32433 else if (maybe_reject_flexarray_init (decl, parsed_arg))
32434 parsed_arg = error_mark_node;
32435 else
32436 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
32439 /* If the token stream has not been completely used up, then
32440 there was extra junk after the end of the default
32441 argument. */
32442 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
32444 if (TREE_CODE (decl) == PARM_DECL)
32445 cp_parser_error (parser, "expected %<,%>");
32446 else
32447 cp_parser_error (parser, "expected %<;%>");
32450 /* Revert to the main lexer. */
32451 cp_parser_pop_lexer (parser);
32453 return parsed_arg;
32456 /* FIELD is a non-static data member with an initializer which we saved for
32457 later; parse it now. */
32459 static void
32460 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
32462 tree def;
32464 maybe_begin_member_template_processing (field);
32466 push_unparsed_function_queues (parser);
32467 def = cp_parser_late_parse_one_default_arg (parser, field,
32468 DECL_INITIAL (field),
32469 NULL_TREE);
32470 pop_unparsed_function_queues (parser);
32472 maybe_end_member_template_processing ();
32474 DECL_INITIAL (field) = def;
32477 /* FN is a FUNCTION_DECL which may contains a parameter with an
32478 unparsed DEFERRED_PARSE. Parse the default args now. This function
32479 assumes that the current scope is the scope in which the default
32480 argument should be processed. */
32482 static void
32483 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
32485 unsigned char saved_local_variables_forbidden_p;
32487 /* While we're parsing the default args, we might (due to the
32488 statement expression extension) encounter more classes. We want
32489 to handle them right away, but we don't want them getting mixed
32490 up with default args that are currently in the queue. */
32491 push_unparsed_function_queues (parser);
32493 /* Local variable names (and the `this' keyword) may not appear
32494 in a default argument. */
32495 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
32496 parser->local_variables_forbidden_p = LOCAL_VARS_AND_THIS_FORBIDDEN;
32498 push_defarg_context (fn);
32500 begin_scope (sk_function_parms, fn);
32502 /* Gather the PARM_DECLs into a vec so we can keep track of them when
32503 pushdecl clears DECL_CHAIN. */
32504 releasing_vec parms;
32505 for (tree parmdecl = DECL_ARGUMENTS (fn); parmdecl;
32506 parmdecl = DECL_CHAIN (parmdecl))
32507 vec_safe_push (parms, parmdecl);
32509 tree parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
32510 for (int i = 0;
32511 parm && parm != void_list_node;
32512 parm = TREE_CHAIN (parm),
32513 ++i)
32515 tree default_arg = TREE_PURPOSE (parm);
32516 tree parsed_arg;
32518 tree parmdecl = parms[i];
32519 pushdecl (parmdecl);
32521 if (!default_arg)
32522 continue;
32524 if (TREE_CODE (default_arg) != DEFERRED_PARSE)
32525 /* This can happen for a friend declaration for a function
32526 already declared with default arguments. */
32527 continue;
32529 parsed_arg
32530 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
32531 default_arg,
32532 TREE_VALUE (parm));
32533 TREE_PURPOSE (parm) = parsed_arg;
32535 /* Update any instantiations we've already created. */
32536 for (tree copy : DEFPARSE_INSTANTIATIONS (default_arg))
32537 TREE_PURPOSE (copy) = parsed_arg;
32540 pop_bindings_and_leave_scope ();
32542 /* Restore DECL_CHAINs after clobbering by pushdecl. */
32543 parm = NULL_TREE;
32544 for (int i = parms->length () - 1; i >= 0; --i)
32546 DECL_CHAIN (parms[i]) = parm;
32547 parm = parms[i];
32550 pop_defarg_context ();
32552 /* Make sure no default arg is missing. */
32553 check_default_args (fn);
32555 /* Restore the state of local_variables_forbidden_p. */
32556 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
32558 /* Restore the queue. */
32559 pop_unparsed_function_queues (parser);
32562 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
32564 sizeof ... ( identifier )
32566 where the 'sizeof' token has already been consumed. */
32568 static tree
32569 cp_parser_sizeof_pack (cp_parser *parser)
32571 /* Consume the `...'. */
32572 cp_lexer_consume_token (parser->lexer);
32573 maybe_warn_variadic_templates ();
32575 matching_parens parens;
32576 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
32577 if (paren)
32578 parens.consume_open (parser);
32579 else
32580 permerror (cp_lexer_peek_token (parser->lexer)->location,
32581 "%<sizeof...%> argument must be surrounded by parentheses");
32583 cp_token *token = cp_lexer_peek_token (parser->lexer);
32584 tree name = cp_parser_identifier (parser);
32585 if (name == error_mark_node)
32586 return error_mark_node;
32587 /* The name is not qualified. */
32588 parser->scope = NULL_TREE;
32589 parser->qualifying_scope = NULL_TREE;
32590 parser->object_scope = NULL_TREE;
32591 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
32592 if (expr == error_mark_node)
32593 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
32594 token->location);
32595 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
32596 expr = TREE_TYPE (expr);
32597 else if (TREE_CODE (expr) == CONST_DECL)
32598 expr = DECL_INITIAL (expr);
32599 expr = make_pack_expansion (expr);
32600 if (expr != error_mark_node)
32601 PACK_EXPANSION_SIZEOF_P (expr) = true;
32603 if (paren)
32604 parens.require_close (parser);
32606 return expr;
32609 /* Parse the operand of `sizeof' (or a similar operator). Returns
32610 either a TYPE or an expression, depending on the form of the
32611 input. The KEYWORD indicates which kind of expression we have
32612 encountered. */
32614 static tree
32615 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
32617 tree expr = NULL_TREE;
32618 const char *saved_message;
32619 const char *saved_message_arg;
32620 bool saved_integral_constant_expression_p;
32621 bool saved_non_integral_constant_expression_p;
32623 /* If it's a `...', then we are computing the length of a parameter
32624 pack. */
32625 if (keyword == RID_SIZEOF
32626 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
32627 return cp_parser_sizeof_pack (parser);
32629 /* Types cannot be defined in a `sizeof' expression. Save away the
32630 old message. */
32631 saved_message = parser->type_definition_forbidden_message;
32632 saved_message_arg = parser->type_definition_forbidden_message_arg;
32633 parser->type_definition_forbidden_message
32634 = G_("types may not be defined in %qs expressions");
32635 parser->type_definition_forbidden_message_arg
32636 = IDENTIFIER_POINTER (ridpointers[keyword]);
32638 /* The restrictions on constant-expressions do not apply inside
32639 sizeof expressions. */
32640 saved_integral_constant_expression_p
32641 = parser->integral_constant_expression_p;
32642 saved_non_integral_constant_expression_p
32643 = parser->non_integral_constant_expression_p;
32644 parser->integral_constant_expression_p = false;
32646 auto cleanup = make_temp_override
32647 (parser->auto_is_implicit_function_template_parm_p, false);
32649 /* Do not actually evaluate the expression. */
32650 ++cp_unevaluated_operand;
32651 ++c_inhibit_evaluation_warnings;
32652 /* If it's a `(', then we might be looking at the type-id
32653 construction. */
32654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32656 tree type = NULL_TREE;
32658 tentative_firewall firewall (parser);
32660 /* We can't be sure yet whether we're looking at a type-id or an
32661 expression. */
32662 cp_parser_parse_tentatively (parser);
32664 matching_parens parens;
32665 parens.consume_open (parser);
32667 /* Note: as a GNU Extension, compound literals are considered
32668 postfix-expressions as they are in C99, so they are valid
32669 arguments to sizeof. See comment in cp_parser_cast_expression
32670 for details. */
32671 if (cp_parser_compound_literal_p (parser))
32672 cp_parser_simulate_error (parser);
32673 else
32675 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
32676 parser->in_type_id_in_expr_p = true;
32677 /* Look for the type-id. */
32678 type = cp_parser_type_id (parser);
32679 /* Look for the closing `)'. */
32680 parens.require_close (parser);
32681 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
32684 /* If all went well, then we're done. */
32685 if (cp_parser_parse_definitely (parser))
32686 expr = type;
32687 else
32689 /* Commit to the tentative_firewall so we get syntax errors. */
32690 cp_parser_commit_to_tentative_parse (parser);
32692 expr = cp_parser_unary_expression (parser);
32695 else
32696 expr = cp_parser_unary_expression (parser);
32698 /* Go back to evaluating expressions. */
32699 --cp_unevaluated_operand;
32700 --c_inhibit_evaluation_warnings;
32702 /* And restore the old one. */
32703 parser->type_definition_forbidden_message = saved_message;
32704 parser->type_definition_forbidden_message_arg = saved_message_arg;
32705 parser->integral_constant_expression_p
32706 = saved_integral_constant_expression_p;
32707 parser->non_integral_constant_expression_p
32708 = saved_non_integral_constant_expression_p;
32710 return expr;
32713 /* If the current declaration has no declarator, return true. */
32715 static bool
32716 cp_parser_declares_only_class_p (cp_parser *parser)
32718 /* If the next token is a `;' or a `,' then there is no
32719 declarator. */
32720 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
32721 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
32724 /* Update the DECL_SPECS to reflect the storage class indicated by
32725 KEYWORD. */
32727 static void
32728 cp_parser_set_storage_class (cp_parser *parser,
32729 cp_decl_specifier_seq *decl_specs,
32730 enum rid keyword,
32731 cp_token *token)
32733 cp_storage_class storage_class;
32735 switch (keyword)
32737 case RID_AUTO:
32738 storage_class = sc_auto;
32739 break;
32740 case RID_REGISTER:
32741 storage_class = sc_register;
32742 break;
32743 case RID_STATIC:
32744 storage_class = sc_static;
32745 break;
32746 case RID_EXTERN:
32747 storage_class = sc_extern;
32748 break;
32749 case RID_MUTABLE:
32750 storage_class = sc_mutable;
32751 break;
32752 default:
32753 gcc_unreachable ();
32756 if (parser->in_unbraced_linkage_specification_p)
32758 error_at (token->location, "invalid use of %qD in linkage specification",
32759 ridpointers[keyword]);
32760 return;
32762 else if (decl_specs->storage_class != sc_none)
32764 if (decl_specs->conflicting_specifiers_p)
32765 return;
32766 gcc_rich_location richloc (token->location);
32767 richloc.add_location_if_nearby (decl_specs->locations[ds_storage_class]);
32768 if (decl_specs->storage_class == storage_class)
32769 error_at (&richloc, "duplicate %qD specifier", ridpointers[keyword]);
32770 else
32771 error_at (&richloc,
32772 "%qD specifier conflicts with %qs",
32773 ridpointers[keyword],
32774 cp_storage_class_name[decl_specs->storage_class]);
32775 decl_specs->conflicting_specifiers_p = true;
32776 return;
32779 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
32780 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
32781 && decl_specs->gnu_thread_keyword_p)
32783 pedwarn (decl_specs->locations[ds_thread], 0,
32784 "%<__thread%> before %qD", ridpointers[keyword]);
32787 decl_specs->storage_class = storage_class;
32788 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
32790 /* A storage class specifier cannot be applied alongside a typedef
32791 specifier. If there is a typedef specifier present then set
32792 conflicting_specifiers_p which will trigger an error later
32793 on in grokdeclarator. */
32794 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32795 && !decl_specs->conflicting_specifiers_p)
32797 gcc_rich_location richloc (token->location);
32798 richloc.add_location_if_nearby (decl_specs->locations[ds_typedef]);
32799 error_at (&richloc,
32800 "%qD specifier conflicts with %<typedef%>",
32801 ridpointers[keyword]);
32802 decl_specs->conflicting_specifiers_p = true;
32806 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
32807 is true, the type is a class or enum definition. */
32809 static void
32810 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
32811 tree type_spec,
32812 cp_token *token,
32813 bool type_definition_p)
32815 decl_specs->any_specifiers_p = true;
32817 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
32818 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
32819 this is what happened. In system headers, we ignore these
32820 declarations so that G++ can work with system headers that are not
32821 C++-safe. */
32822 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
32823 && !type_definition_p
32824 && (type_spec == boolean_type_node
32825 || type_spec == char8_type_node
32826 || type_spec == char16_type_node
32827 || type_spec == char32_type_node
32828 || type_spec == wchar_type_node)
32829 && (decl_specs->type
32830 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
32831 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
32832 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
32833 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
32835 decl_specs->redefined_builtin_type = type_spec;
32836 set_and_check_decl_spec_loc (decl_specs,
32837 ds_redefined_builtin_type_spec,
32838 token);
32839 if (!decl_specs->type)
32841 decl_specs->type = type_spec;
32842 decl_specs->type_definition_p = false;
32843 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
32846 else if (decl_specs->type)
32847 decl_specs->multiple_types_p = true;
32848 else
32850 decl_specs->type = type_spec;
32851 decl_specs->type_definition_p = type_definition_p;
32852 decl_specs->redefined_builtin_type = NULL_TREE;
32853 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
32857 /* True iff TOKEN is the GNU keyword __thread. */
32859 static bool
32860 token_is__thread (cp_token *token)
32862 gcc_assert (token->keyword == RID_THREAD);
32863 return id_equal (token->u.value, "__thread");
32866 /* Set the location for a declarator specifier and check if it is
32867 duplicated.
32869 DECL_SPECS is the sequence of declarator specifiers onto which to
32870 set the location.
32872 DS is the single declarator specifier to set which location is to
32873 be set onto the existing sequence of declarators.
32875 LOCATION is the location for the declarator specifier to
32876 consider. */
32878 static void
32879 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
32880 cp_decl_spec ds, cp_token *token)
32882 gcc_assert (ds < ds_last);
32884 if (decl_specs == NULL)
32885 return;
32887 location_t location = token->location;
32889 if (decl_specs->locations[ds] == 0)
32891 decl_specs->locations[ds] = location;
32892 if (ds == ds_thread)
32893 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
32895 else
32897 if (ds == ds_long)
32899 if (decl_specs->locations[ds_long_long] != 0)
32900 error_at (location,
32901 "%<long long long%> is too long for GCC");
32902 else
32904 decl_specs->locations[ds_long_long] = location;
32905 pedwarn_cxx98 (location,
32906 OPT_Wlong_long,
32907 "ISO C++ 1998 does not support %<long long%>");
32910 else if (ds == ds_thread)
32912 bool gnu = token_is__thread (token);
32913 gcc_rich_location richloc (location);
32914 if (gnu != decl_specs->gnu_thread_keyword_p)
32916 richloc.add_range (decl_specs->locations[ds_thread]);
32917 error_at (&richloc,
32918 "both %<__thread%> and %<thread_local%> specified");
32920 else
32922 richloc.add_fixit_remove ();
32923 error_at (&richloc, "duplicate %qD", token->u.value);
32926 else
32928 static const char *const decl_spec_names[] = {
32929 "signed",
32930 "unsigned",
32931 "short",
32932 "long",
32933 "const",
32934 "volatile",
32935 "restrict",
32936 "inline",
32937 "virtual",
32938 "explicit",
32939 "friend",
32940 "typedef",
32941 "using",
32942 "constexpr",
32943 "__complex",
32944 "constinit",
32945 "consteval"
32947 gcc_rich_location richloc (location);
32948 richloc.add_fixit_remove ();
32949 error_at (&richloc, "duplicate %qs", decl_spec_names[ds]);
32954 /* Return true iff the declarator specifier DS is present in the
32955 sequence of declarator specifiers DECL_SPECS. */
32957 bool
32958 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
32959 cp_decl_spec ds)
32961 gcc_assert (ds < ds_last);
32963 if (decl_specs == NULL)
32964 return false;
32966 return decl_specs->locations[ds] != 0;
32969 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
32970 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
32972 static bool
32973 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
32975 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
32978 /* Issue an error message indicating that TOKEN_DESC was expected.
32979 If KEYWORD is true, it indicated this function is called by
32980 cp_parser_require_keword and the required token can only be
32981 a indicated keyword.
32983 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
32984 within any error as the location of an "opening" token matching
32985 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
32986 RT_CLOSE_PAREN). */
32988 static void
32989 cp_parser_required_error (cp_parser *parser,
32990 required_token token_desc,
32991 bool keyword,
32992 location_t matching_location)
32994 if (cp_parser_simulate_error (parser))
32995 return;
32997 const char *gmsgid = NULL;
32998 switch (token_desc)
33000 case RT_NEW:
33001 gmsgid = G_("expected %<new%>");
33002 break;
33003 case RT_DELETE:
33004 gmsgid = G_("expected %<delete%>");
33005 break;
33006 case RT_RETURN:
33007 gmsgid = G_("expected %<return%>");
33008 break;
33009 case RT_WHILE:
33010 gmsgid = G_("expected %<while%>");
33011 break;
33012 case RT_EXTERN:
33013 gmsgid = G_("expected %<extern%>");
33014 break;
33015 case RT_STATIC_ASSERT:
33016 gmsgid = G_("expected %<static_assert%>");
33017 break;
33018 case RT_DECLTYPE:
33019 gmsgid = G_("expected %<decltype%>");
33020 break;
33021 case RT_OPERATOR:
33022 gmsgid = G_("expected %<operator%>");
33023 break;
33024 case RT_CLASS:
33025 gmsgid = G_("expected %<class%>");
33026 break;
33027 case RT_TEMPLATE:
33028 gmsgid = G_("expected %<template%>");
33029 break;
33030 case RT_NAMESPACE:
33031 gmsgid = G_("expected %<namespace%>");
33032 break;
33033 case RT_USING:
33034 gmsgid = G_("expected %<using%>");
33035 break;
33036 case RT_ASM:
33037 gmsgid = G_("expected %<asm%>");
33038 break;
33039 case RT_TRY:
33040 gmsgid = G_("expected %<try%>");
33041 break;
33042 case RT_CATCH:
33043 gmsgid = G_("expected %<catch%>");
33044 break;
33045 case RT_THROW:
33046 gmsgid = G_("expected %<throw%>");
33047 break;
33048 case RT_AUTO:
33049 gmsgid = G_("expected %<auto%>");
33050 break;
33051 case RT_LABEL:
33052 gmsgid = G_("expected %<__label__%>");
33053 break;
33054 case RT_AT_TRY:
33055 gmsgid = G_("expected %<@try%>");
33056 break;
33057 case RT_AT_SYNCHRONIZED:
33058 gmsgid = G_("expected %<@synchronized%>");
33059 break;
33060 case RT_AT_THROW:
33061 gmsgid = G_("expected %<@throw%>");
33062 break;
33063 case RT_TRANSACTION_ATOMIC:
33064 gmsgid = G_("expected %<__transaction_atomic%>");
33065 break;
33066 case RT_TRANSACTION_RELAXED:
33067 gmsgid = G_("expected %<__transaction_relaxed%>");
33068 break;
33069 case RT_CO_YIELD:
33070 gmsgid = G_("expected %<co_yield%>");
33071 break;
33072 default:
33073 break;
33076 if (!gmsgid && !keyword)
33078 switch (token_desc)
33080 case RT_SEMICOLON:
33081 gmsgid = G_("expected %<;%>");
33082 break;
33083 case RT_OPEN_PAREN:
33084 gmsgid = G_("expected %<(%>");
33085 break;
33086 case RT_CLOSE_BRACE:
33087 gmsgid = G_("expected %<}%>");
33088 break;
33089 case RT_OPEN_BRACE:
33090 gmsgid = G_("expected %<{%>");
33091 break;
33092 case RT_CLOSE_SQUARE:
33093 gmsgid = G_("expected %<]%>");
33094 break;
33095 case RT_OPEN_SQUARE:
33096 gmsgid = G_("expected %<[%>");
33097 break;
33098 case RT_COMMA:
33099 gmsgid = G_("expected %<,%>");
33100 break;
33101 case RT_SCOPE:
33102 gmsgid = G_("expected %<::%>");
33103 break;
33104 case RT_LESS:
33105 gmsgid = G_("expected %<<%>");
33106 break;
33107 case RT_GREATER:
33108 gmsgid = G_("expected %<>%>");
33109 break;
33110 case RT_EQ:
33111 gmsgid = G_("expected %<=%>");
33112 break;
33113 case RT_ELLIPSIS:
33114 gmsgid = G_("expected %<...%>");
33115 break;
33116 case RT_MULT:
33117 gmsgid = G_("expected %<*%>");
33118 break;
33119 case RT_COMPL:
33120 gmsgid = G_("expected %<~%>");
33121 break;
33122 case RT_COLON:
33123 gmsgid = G_("expected %<:%>");
33124 break;
33125 case RT_COLON_SCOPE:
33126 gmsgid = G_("expected %<:%> or %<::%>");
33127 break;
33128 case RT_CLOSE_PAREN:
33129 gmsgid = G_("expected %<)%>");
33130 break;
33131 case RT_COMMA_CLOSE_PAREN:
33132 gmsgid = G_("expected %<,%> or %<)%>");
33133 break;
33134 case RT_PRAGMA_EOL:
33135 gmsgid = G_("expected end of line");
33136 break;
33137 case RT_NAME:
33138 gmsgid = G_("expected identifier");
33139 break;
33140 case RT_SELECT:
33141 gmsgid = G_("expected selection-statement");
33142 break;
33143 case RT_ITERATION:
33144 gmsgid = G_("expected iteration-statement");
33145 break;
33146 case RT_JUMP:
33147 gmsgid = G_("expected jump-statement");
33148 break;
33149 case RT_CLASS_KEY:
33150 gmsgid = G_("expected class-key");
33151 break;
33152 case RT_CLASS_TYPENAME_TEMPLATE:
33153 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
33154 break;
33155 default:
33156 gcc_unreachable ();
33160 if (gmsgid)
33161 cp_parser_error_1 (parser, gmsgid, token_desc, matching_location);
33165 /* If the next token is of the indicated TYPE, consume it. Otherwise,
33166 issue an error message indicating that TOKEN_DESC was expected.
33168 Returns the token consumed, if the token had the appropriate type.
33169 Otherwise, returns NULL.
33171 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
33172 within any error as the location of an "opening" token matching
33173 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
33174 RT_CLOSE_PAREN). */
33176 static cp_token *
33177 cp_parser_require (cp_parser* parser,
33178 enum cpp_ttype type,
33179 required_token token_desc,
33180 location_t matching_location)
33182 if (cp_lexer_next_token_is (parser->lexer, type))
33183 return cp_lexer_consume_token (parser->lexer);
33184 else
33186 /* Output the MESSAGE -- unless we're parsing tentatively. */
33187 if (!cp_parser_simulate_error (parser))
33188 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
33189 matching_location);
33190 return NULL;
33194 /* Skip an entire parameter list from start to finish. The next token must
33195 be the initial "<" of the parameter list. Returns true on success and
33196 false otherwise. */
33198 static bool
33199 cp_parser_skip_entire_template_parameter_list (cp_parser* parser)
33201 /* Consume the "<" because cp_parser_skip_to_end_of_template_parameter_list
33202 requires it. */
33203 cp_lexer_consume_token (parser->lexer);
33204 return cp_parser_skip_to_end_of_template_parameter_list (parser);
33207 /* Ensure we are at the end of a template parameter list. If we are, return.
33208 If we are not, something has gone wrong, in which case issue an error and
33209 skip to end of the parameter list. */
33211 static void
33212 cp_parser_require_end_of_template_parameter_list (cp_parser* parser)
33214 /* Are we ready, yet? If not, issue error message. */
33215 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
33216 return;
33218 cp_parser_skip_to_end_of_template_parameter_list (parser);
33221 /* You should only call this function from inside a template parameter list
33222 (i.e. the current token should at least be the initial "<" of the
33223 parameter list). If you are skipping the entire list, it may be better to
33224 use cp_parser_skip_entire_template_parameter_list.
33226 Tokens are skipped until the final ">" is found, or if we see
33227 '{', '}', ';', or if we find an unbalanced ')' or ']'.
33229 Returns true if we successfully reached the end, and false if
33230 something unexpected happened (e.g. end of file). */
33232 static bool
33233 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
33235 /* Current level of '< ... >'. */
33236 unsigned level = 0;
33237 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
33238 unsigned nesting_depth = 0;
33240 /* Skip tokens until the desired token is found. */
33241 while (true)
33243 /* Peek at the next token. */
33244 switch (cp_lexer_peek_token (parser->lexer)->type)
33246 case CPP_LESS:
33247 if (!nesting_depth)
33248 ++level;
33249 break;
33251 case CPP_RSHIFT:
33252 if (cxx_dialect == cxx98)
33253 /* C++0x views the `>>' operator as two `>' tokens, but
33254 C++98 does not. */
33255 break;
33256 else if (!nesting_depth && level-- == 0)
33258 /* We've hit a `>>' where the first `>' closes the
33259 template argument list, and the second `>' is
33260 spurious. Just consume the `>>' and stop; we've
33261 already produced at least one error. */
33262 cp_lexer_consume_token (parser->lexer);
33263 return false;
33265 /* Fall through for C++0x, so we handle the second `>' in
33266 the `>>'. */
33267 gcc_fallthrough ();
33269 case CPP_GREATER:
33270 if (!nesting_depth && level-- == 0)
33272 /* We've reached the token we want, consume it and stop. */
33273 cp_lexer_consume_token (parser->lexer);
33274 return true;
33276 break;
33278 case CPP_OPEN_PAREN:
33279 case CPP_OPEN_SQUARE:
33280 ++nesting_depth;
33281 break;
33283 case CPP_CLOSE_PAREN:
33284 case CPP_CLOSE_SQUARE:
33285 if (nesting_depth-- == 0)
33286 return false;
33287 break;
33289 case CPP_EOF:
33290 case CPP_PRAGMA_EOL:
33291 case CPP_SEMICOLON:
33292 case CPP_OPEN_BRACE:
33293 case CPP_CLOSE_BRACE:
33294 /* The '>' was probably forgotten, don't look further. */
33295 return false;
33297 default:
33298 break;
33301 /* Consume this token. */
33302 cp_lexer_consume_token (parser->lexer);
33306 /* If the next token is the indicated keyword, consume it. Otherwise,
33307 issue an error message indicating that TOKEN_DESC was expected.
33309 Returns the token consumed, if the token had the appropriate type.
33310 Otherwise, returns NULL. */
33312 static cp_token *
33313 cp_parser_require_keyword (cp_parser* parser,
33314 enum rid keyword,
33315 required_token token_desc)
33317 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
33319 if (token && token->keyword != keyword)
33321 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
33322 UNKNOWN_LOCATION);
33323 return NULL;
33326 return token;
33329 /* Returns TRUE iff TOKEN is a token that can begin the body of a
33330 function-definition. */
33332 static bool
33333 cp_parser_token_starts_function_definition_p (cp_token* token)
33335 return (/* An ordinary function-body begins with an `{'. */
33336 token->type == CPP_OPEN_BRACE
33337 /* A ctor-initializer begins with a `:'. */
33338 || token->type == CPP_COLON
33339 /* A function-try-block begins with `try'. */
33340 || token->keyword == RID_TRY
33341 /* A function-transaction-block begins with `__transaction_atomic'
33342 or `__transaction_relaxed'. */
33343 || token->keyword == RID_TRANSACTION_ATOMIC
33344 || token->keyword == RID_TRANSACTION_RELAXED
33345 /* The named return value extension begins with `return'. */
33346 || token->keyword == RID_RETURN);
33349 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
33350 definition. */
33352 static bool
33353 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
33355 cp_token *token;
33357 token = cp_lexer_peek_token (parser->lexer);
33358 return (token->type == CPP_OPEN_BRACE
33359 || (token->type == CPP_COLON
33360 && !parser->colon_doesnt_start_class_def_p));
33363 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
33364 C++0x) ending a template-argument. */
33366 static bool
33367 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
33369 cp_token *token;
33371 token = cp_lexer_peek_token (parser->lexer);
33372 return (token->type == CPP_COMMA
33373 || token->type == CPP_GREATER
33374 || token->type == CPP_ELLIPSIS
33375 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)
33376 /* For better diagnostics, treat >>= like that too, that
33377 shouldn't appear non-nested in template arguments. */
33378 || token->type == CPP_RSHIFT_EQ);
33381 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
33382 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
33384 static bool
33385 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
33386 size_t n)
33388 cp_token *token;
33390 token = cp_lexer_peek_nth_token (parser->lexer, n);
33391 if (token->type == CPP_LESS)
33392 return true;
33393 /* Check for the sequence `<::' in the original code. It would be lexed as
33394 `[:', where `[' is a digraph, and there is no whitespace before
33395 `:'. */
33396 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
33398 cp_token *token2;
33399 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
33400 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
33401 return true;
33403 return false;
33406 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
33407 or none_type otherwise. */
33409 static enum tag_types
33410 cp_parser_token_is_class_key (cp_token* token)
33412 switch (token->keyword)
33414 case RID_CLASS:
33415 return class_type;
33416 case RID_STRUCT:
33417 return record_type;
33418 case RID_UNION:
33419 return union_type;
33421 default:
33422 return none_type;
33426 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
33427 or none_type otherwise or if the token is null. */
33429 static enum tag_types
33430 cp_parser_token_is_type_parameter_key (cp_token* token)
33432 if (!token)
33433 return none_type;
33435 switch (token->keyword)
33437 case RID_CLASS:
33438 return class_type;
33439 case RID_TYPENAME:
33440 return typename_type;
33442 default:
33443 return none_type;
33447 /* Diagnose redundant enum-keys. */
33449 static void
33450 cp_parser_maybe_warn_enum_key (cp_parser *parser, location_t key_loc,
33451 tree type, rid scoped_key)
33453 if (!warn_redundant_tags)
33454 return;
33456 tree type_decl = TYPE_MAIN_DECL (type);
33457 tree name = DECL_NAME (type_decl);
33458 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
33459 push_deferring_access_checks (dk_no_check);
33460 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33461 pop_deferring_access_checks ();
33463 /* The enum-key is redundant for uses of the TYPE that are not
33464 declarations and for which name lookup returns just the type
33465 itself. */
33466 if (decl != type_decl)
33467 return;
33469 if (scoped_key != RID_CLASS
33470 && scoped_key != RID_STRUCT
33471 && current_lang_name != lang_name_cplusplus
33472 && current_namespace == global_namespace)
33474 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
33475 enum tag in shared C/C++ code in files (such as headers) included
33476 in the main source file. */
33477 const line_map_ordinary *map = NULL;
33478 linemap_resolve_location (line_table, key_loc,
33479 LRK_MACRO_DEFINITION_LOCATION,
33480 &map);
33481 if (!MAIN_FILE_P (map))
33482 return;
33485 gcc_rich_location richloc (key_loc);
33486 richloc.add_fixit_remove (key_loc);
33487 warning_at (&richloc, OPT_Wredundant_tags,
33488 "redundant enum-key %<enum%s%> in reference to %q#T",
33489 (scoped_key == RID_CLASS ? " class"
33490 : scoped_key == RID_STRUCT ? " struct" : ""), type);
33493 /* Describes the set of declarations of a struct, class, or class template
33494 or its specializations. Used for -Wmismatched-tags. */
33496 class class_decl_loc_t
33498 public:
33500 class_decl_loc_t ()
33501 : locvec (), idxdef (), def_class_key ()
33503 locvec.create (4);
33506 /* Constructs an object for a single declaration of a class with
33507 CLASS_KEY at the current location in the current function (or
33508 at another scope). KEY_REDUNDANT is true if the class-key may
33509 be omitted in the current context without an ambiguity with
33510 another symbol with the same name.
33511 DEF_P is true for a class declaration that is a definition.
33512 CURLOC is the associated location. */
33513 class_decl_loc_t (tag_types class_key, bool key_redundant, bool def_p,
33514 location_t curloc = input_location)
33515 : locvec (), idxdef (def_p ? 0 : UINT_MAX), def_class_key (class_key)
33517 locvec.create (4);
33518 class_key_loc_t ckl (current_function_decl, curloc, class_key,
33519 key_redundant);
33520 locvec.quick_push (ckl);
33523 /* Copy, assign, and destroy the object. Necessary because LOCVEC
33524 isn't safely copyable and assignable and doesn't release storage
33525 on its own. */
33526 class_decl_loc_t (const class_decl_loc_t &rhs)
33527 : locvec (rhs.locvec.copy ()), idxdef (rhs.idxdef),
33528 def_class_key (rhs.def_class_key)
33531 class_decl_loc_t& operator= (const class_decl_loc_t &rhs)
33533 if (this == &rhs)
33534 return *this;
33535 locvec.release ();
33536 locvec = rhs.locvec.copy ();
33537 idxdef = rhs.idxdef;
33538 def_class_key = rhs.def_class_key;
33539 return *this;
33542 ~class_decl_loc_t ()
33544 locvec.release ();
33547 /* Issues -Wmismatched-tags for a single class. */
33548 void diag_mismatched_tags (tree);
33550 /* Issues -Wmismatched-tags for all classes. */
33551 static void diag_mismatched_tags ();
33553 /* Adds TYPE_DECL to the collection of class decls and diagnoses
33554 redundant tags (if -Wredundant-tags is enabled). */
33555 static void add (cp_parser *, location_t, tag_types, tree, bool, bool);
33557 /* Either adds this decl to the collection of class decls
33558 or diagnoses it, whichever is appropriate. */
33559 void add_or_diag_mismatched_tag (tree, tag_types, bool, bool);
33561 private:
33563 tree function (unsigned i) const
33565 return locvec[i].func;
33568 location_t location (unsigned i) const
33570 return locvec[i].loc;
33573 bool key_redundant (unsigned i) const
33575 return locvec[i].key_redundant;
33578 tag_types class_key (unsigned i) const
33580 return locvec[i].class_key;
33583 /* True if a definition for the class has been seen. */
33584 bool def_p () const
33586 return idxdef < locvec.length ();
33589 /* The location of a single mention of a class type with the given
33590 class-key. */
33591 struct class_key_loc_t
33593 class_key_loc_t (tree func, location_t loc, tag_types key, bool redundant)
33594 : func (func), loc (loc), class_key (key), key_redundant (redundant)
33597 /* The function the type is mentioned in. */
33598 tree func;
33599 /* The exact location. */
33600 location_t loc;
33601 /* The class-key used in the mention of the type. */
33602 tag_types class_key;
33603 /* True when the class-key could be omitted at this location
33604 without an ambiguity with another symbol of the same name. */
33605 bool key_redundant;
33607 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
33608 vec <class_key_loc_t> locvec;
33609 /* LOCVEC index of the definition or UINT_MAX if none exists. */
33610 unsigned idxdef;
33611 /* The class-key the class was last declared with or none_type when
33612 it has been declared with a mismatched key. */
33613 tag_types def_class_key;
33615 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
33616 description above. */
33617 typedef hash_map<tree_decl_hash, class_decl_loc_t> class_to_loc_map_t;
33618 static class_to_loc_map_t class2loc;
33621 class_decl_loc_t::class_to_loc_map_t class_decl_loc_t::class2loc;
33623 /* Issue an error message if the CLASS_KEY does not match the TYPE.
33624 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33625 is set for a declaration of class TYPE and clear for a reference to
33626 it that is not a declaration of it. */
33628 static void
33629 cp_parser_check_class_key (cp_parser *parser, location_t key_loc,
33630 tag_types class_key, tree type, bool def_p,
33631 bool decl_p)
33633 if (type == error_mark_node)
33634 return;
33636 bool seen_as_union = TREE_CODE (type) == UNION_TYPE;
33637 if (seen_as_union != (class_key == union_type))
33639 if (permerror (input_location, "%qs tag used in naming %q#T",
33640 class_key == union_type ? "union"
33641 : class_key == record_type ? "struct" : "class",
33642 type))
33643 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
33644 "%q#T was previously declared here", type);
33645 return;
33648 if (!warn_mismatched_tags && !warn_redundant_tags)
33649 return;
33651 /* Only consider the true class-keys below and ignore typename_type,
33652 etc. that are not C++ class-keys. */
33653 if (class_key != class_type
33654 && class_key != record_type
33655 && class_key != union_type)
33656 return;
33658 class_decl_loc_t::add (parser, key_loc, class_key, type, def_p, decl_p);
33661 /* Returns the template or specialization of one to which the RECORD_TYPE
33662 TYPE corresponds. */
33664 static tree
33665 specialization_of (tree type)
33667 tree ret = type;
33669 /* Determine the template or its partial specialization to which TYPE
33670 corresponds. */
33671 if (tree spec = most_specialized_partial_spec (type, tf_none))
33672 if (spec != error_mark_node)
33673 ret = TREE_TYPE (TREE_VALUE (spec));
33675 if (ret == type)
33676 ret = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type);
33678 return TYPE_MAIN_DECL (ret);
33682 /* Adds the class TYPE to the collection of class decls and diagnoses
33683 redundant tags (if -Wredundant-tags is enabled).
33684 DEF_P is expected to be set for a definition of class TYPE. DECL_P
33685 is set for a (likely, based on syntactic context) declaration of class
33686 TYPE and clear for a reference to it that is not a declaration of it. */
33688 void
33689 class_decl_loc_t::add (cp_parser *parser, location_t key_loc,
33690 tag_types class_key, tree type, bool def_p, bool decl_p)
33692 tree type_decl = TYPE_MAIN_DECL (type);
33693 tree name = DECL_NAME (type_decl);
33694 /* Look up the NAME to see if it unambiguously refers to the TYPE
33695 and set KEY_REDUNDANT if so. */
33696 push_deferring_access_checks (dk_no_check);
33697 tree decl = cp_parser_lookup_name_simple (parser, name, input_location);
33698 pop_deferring_access_checks ();
33700 /* The class-key is redundant for uses of the CLASS_TYPE that are
33701 neither definitions of it nor declarations, and for which name
33702 lookup returns just the type itself. */
33703 bool key_redundant = (!def_p && !decl_p
33704 && (decl == type_decl
33705 || TREE_CODE (decl) == TEMPLATE_DECL
33706 || (CLASS_TYPE_P (type)
33707 && TYPE_BEING_DEFINED (type))));
33709 if (key_redundant
33710 && class_key != class_type
33711 && current_lang_name != lang_name_cplusplus
33712 && current_namespace == global_namespace)
33714 /* Avoid issuing the diagnostic for apparently redundant struct
33715 and union class-keys in shared C/C++ code in files (such as
33716 headers) included in the main source file. */
33717 const line_map_ordinary *map = NULL;
33718 linemap_resolve_location (line_table, key_loc,
33719 LRK_MACRO_DEFINITION_LOCATION,
33720 &map);
33721 if (!MAIN_FILE_P (map))
33722 key_redundant = false;
33725 /* Set if a declaration of TYPE has previously been seen or if it must
33726 exist in a precompiled header. */
33727 bool exist;
33728 class_decl_loc_t *rdl = &class2loc.get_or_insert (type_decl, &exist);
33729 if (!exist)
33731 tree type = TREE_TYPE (type_decl);
33732 if (def_p || !COMPLETE_TYPE_P (type))
33734 /* TYPE_DECL is the first declaration or definition of the type
33735 (outside precompiled headers -- see below). Just create
33736 a new entry for it and return unless it's a declaration
33737 involving a template that may need to be diagnosed by
33738 -Wredundant-tags. */
33739 *rdl = class_decl_loc_t (class_key, false, def_p);
33740 if (TREE_CODE (decl) != TEMPLATE_DECL)
33741 return;
33743 else
33745 /* TYPE was previously defined in some unknown precompiled header.
33746 Simply add a record of its definition at an unknown location and
33747 proceed below to add a reference to it at the current location.
33748 (Declarations in precompiled headers that are not definitions
33749 are ignored.) */
33750 tag_types def_key
33751 = CLASSTYPE_DECLARED_CLASS (type) ? class_type : record_type;
33752 location_t def_loc = DECL_SOURCE_LOCATION (type_decl);
33753 *rdl = class_decl_loc_t (def_key, false, true, def_loc);
33754 exist = true;
33758 /* A prior declaration of TYPE_DECL has been seen. */
33760 if (key_redundant)
33762 gcc_rich_location richloc (key_loc);
33763 richloc.add_fixit_remove (key_loc);
33764 warning_at (&richloc, OPT_Wredundant_tags,
33765 "redundant class-key %qs in reference to %q#T",
33766 class_key == union_type ? "union"
33767 : class_key == record_type ? "struct" : "class",
33768 type);
33771 if (!exist)
33772 /* Do nothing if this is the first declaration of the type. */
33773 return;
33775 if (rdl->idxdef != UINT_MAX && rdl->def_class_key == class_key)
33776 /* Do nothing if the class-key in this declaration matches
33777 the definition. */
33778 return;
33780 rdl->add_or_diag_mismatched_tag (type_decl, class_key, key_redundant,
33781 def_p);
33784 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
33785 of class decls or diagnoses it, whichever is appropriate. */
33787 void
33788 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl,
33789 tag_types class_key,
33790 bool redundant,
33791 bool def_p)
33793 /* Reset the CLASS_KEY associated with this type on mismatch.
33794 This is an optimization that lets the diagnostic code skip
33795 over classes that use the same class-key in all declarations. */
33796 if (def_class_key != class_key)
33797 def_class_key = none_type;
33799 /* Set IDXDEF to the index of the vector corresponding to
33800 the definition. */
33801 if (def_p)
33802 idxdef = locvec.length ();
33804 /* Append a record of this declaration to the vector. */
33805 class_key_loc_t ckl (current_function_decl, input_location, class_key,
33806 redundant);
33807 locvec.safe_push (ckl);
33809 if (idxdef == UINT_MAX)
33810 return;
33812 /* As a space optimization diagnose declarations of a class
33813 whose definition has been seen and purge the LOCVEC of
33814 all entries except the definition. */
33815 diag_mismatched_tags (type_decl);
33816 if (idxdef)
33818 class_decl_loc_t::class_key_loc_t ent = locvec[idxdef];
33819 locvec.release ();
33820 locvec.reserve (2);
33821 locvec.safe_push (ent);
33822 idxdef = 0;
33824 else
33825 /* Pop the entry pushed above for this declaration. */
33826 locvec.pop ();
33829 /* Issues -Wmismatched-tags for a single class. */
33831 void
33832 class_decl_loc_t::diag_mismatched_tags (tree type_decl)
33834 if (!warn_mismatched_tags)
33835 return;
33837 /* Number of uses of the class. */
33838 const unsigned ndecls = locvec.length ();
33840 /* The class (or template) declaration guiding the decisions about
33841 the diagnostic. For ordinary classes it's the same as THIS. For
33842 uses of instantiations of templates other than their declarations
33843 it points to the record for the declaration of the corresponding
33844 primary template or partial specialization. */
33845 class_decl_loc_t *cdlguide = this;
33847 tree type = TREE_TYPE (type_decl);
33848 if (CLASS_TYPE_P (type) && CLASSTYPE_IMPLICIT_INSTANTIATION (type))
33850 /* For implicit instantiations of a primary template look up
33851 the primary or partial specialization and use it as
33852 the expected class-key rather than using the class-key of
33853 the first reference to the instantiation. The primary must
33854 be (and inevitably is) at index zero. */
33855 tree spec = specialization_of (type);
33856 cdlguide = class2loc.get (spec);
33857 gcc_assert (cdlguide != NULL);
33859 else
33861 /* Skip declarations that consistently use the same class-key. */
33862 if (def_class_key != none_type)
33863 return;
33866 /* Set if a definition for the class has been seen. */
33867 const bool def_p = cdlguide->def_p ();
33869 /* The index of the declaration whose class-key this declaration
33870 is expected to match. It's either the class-key of the class
33871 definition if one exists or the first declaration otherwise. */
33872 const unsigned idxguide = def_p ? cdlguide->idxdef : 0;
33874 /* The class-key the class is expected to be declared with: it's
33875 either the key used in its definition or the first declaration
33876 if no definition has been provided.
33877 For implicit instantiations of a primary template it's
33878 the class-key used to declare the primary with. The primary
33879 must be at index zero. */
33880 const tag_types xpect_key = cdlguide->class_key (idxguide);
33882 unsigned idx = 0;
33883 /* Advance IDX to the first declaration that either is not
33884 a definition or that doesn't match the first declaration
33885 if no definition is provided. */
33886 while (class_key (idx) == xpect_key)
33887 if (++idx == ndecls)
33888 return;
33890 /* Save the current function before changing it below. */
33891 tree save_func = current_function_decl;
33892 /* Set the function declaration to print in diagnostic context. */
33893 current_function_decl = function (idx);
33895 const char *xmatchkstr = xpect_key == record_type ? "class" : "struct";
33896 const char *xpectkstr = xpect_key == record_type ? "struct" : "class";
33898 location_t loc = location (idx);
33899 bool key_redundant_p = key_redundant (idx);
33900 auto_diagnostic_group d;
33901 /* Issue a warning for the first mismatched declaration.
33902 Avoid using "%#qT" since the class-key for the same type will
33903 be the same regardless of which one was used in the declaraion. */
33904 if (warning_at (loc, OPT_Wmismatched_tags,
33905 "%qT declared with a mismatched class-key %qs",
33906 type_decl, xmatchkstr))
33908 /* Suggest how to avoid the warning for each instance since
33909 the guidance may be different depending on context. */
33910 inform (loc,
33911 (key_redundant_p
33912 ? G_("remove the class-key or replace it with %qs")
33913 : G_("replace the class-key with %qs")),
33914 xpectkstr);
33916 /* Also point to the first declaration or definition that guided
33917 the decision to issue the warning above. */
33918 inform (cdlguide->location (idxguide),
33919 (def_p
33920 ? G_("%qT defined as %qs here")
33921 : G_("%qT first declared as %qs here")),
33922 type_decl, xpectkstr);
33925 /* Issue warnings for the remaining inconsistent declarations. */
33926 for (unsigned i = idx + 1; i != ndecls; ++i)
33928 tag_types clskey = class_key (i);
33929 /* Skip over the declarations that match either the definition
33930 if one was provided or the first declaration. */
33931 if (clskey == xpect_key)
33932 continue;
33934 loc = location (i);
33935 key_redundant_p = key_redundant (i);
33936 /* Set the function declaration to print in diagnostic context. */
33937 current_function_decl = function (i);
33938 if (warning_at (loc, OPT_Wmismatched_tags,
33939 "%qT declared with a mismatched class-key %qs",
33940 type_decl, xmatchkstr))
33941 /* Suggest how to avoid the warning for each instance since
33942 the guidance may be different depending on context. */
33943 inform (loc,
33944 (key_redundant_p
33945 ? G_("remove the class-key or replace it with %qs")
33946 : G_("replace the class-key with %qs")),
33947 xpectkstr);
33950 /* Restore the current function in case it was replaced above. */
33951 current_function_decl = save_func;
33954 /* Issues -Wmismatched-tags for all classes. Called at the end
33955 of processing a translation unit, after declarations of all class
33956 types and their uses have been recorded. */
33958 void
33959 class_decl_loc_t::diag_mismatched_tags ()
33961 /* CLASS2LOC should be empty if both -Wmismatched-tags and
33962 -Wredundant-tags are disabled. */
33963 gcc_assert (warn_mismatched_tags
33964 || warn_redundant_tags
33965 || class2loc.is_empty ());
33967 /* Save the current function before changing on return. It should
33968 be null at this point. */
33969 temp_override<tree> cleanup (current_function_decl);
33971 if (warn_mismatched_tags)
33973 /* Iterate over the collected class/struct/template declarations. */
33974 typedef class_to_loc_map_t::iterator iter_t;
33975 for (iter_t it = class2loc.begin (); it != class2loc.end (); ++it)
33977 tree type_decl = (*it).first;
33978 class_decl_loc_t &recloc = (*it).second;
33979 recloc.diag_mismatched_tags (type_decl);
33983 class2loc.empty ();
33986 /* Issue an error message if DECL is redeclared with different
33987 access than its original declaration [class.access.spec/3].
33988 This applies to nested classes, nested class templates and
33989 enumerations [class.mem/1]. */
33991 static void
33992 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
33994 if (!decl
33995 || (!CLASS_TYPE_P (TREE_TYPE (decl))
33996 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
33997 return;
33999 if ((TREE_PRIVATE (decl)
34000 != (current_access_specifier == access_private_node))
34001 || (TREE_PROTECTED (decl)
34002 != (current_access_specifier == access_protected_node)))
34003 error_at (location, "%qD redeclared with different access", decl);
34006 /* Look for the `template' keyword, as a syntactic disambiguator.
34007 Return TRUE iff it is present, in which case it will be
34008 consumed. */
34010 static bool
34011 cp_parser_optional_template_keyword (cp_parser *parser)
34013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
34015 /* In C++98 the `template' keyword can only be used within templates;
34016 outside templates the parser can always figure out what is a
34017 template and what is not. In C++11, per the resolution of DR 468,
34018 `template' is allowed in cases where it is not strictly necessary. */
34019 if (!processing_template_decl
34020 && pedantic && cxx_dialect == cxx98)
34022 cp_token *token = cp_lexer_peek_token (parser->lexer);
34023 pedwarn (token->location, OPT_Wpedantic,
34024 "in C++98 %<template%> (as a disambiguator) is only "
34025 "allowed within templates");
34026 /* If this part of the token stream is rescanned, the same
34027 error message would be generated. So, we purge the token
34028 from the stream. */
34029 cp_lexer_purge_token (parser->lexer);
34030 return false;
34032 else
34034 /* Consume the `template' keyword. */
34035 cp_lexer_consume_token (parser->lexer);
34036 return true;
34039 return false;
34042 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
34043 set PARSER->SCOPE, and perform other related actions. */
34045 static void
34046 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
34048 struct tree_check *check_value;
34050 /* Get the stored value. */
34051 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
34052 /* Set the scope from the stored value. */
34053 parser->scope = saved_checks_value (check_value);
34054 parser->qualifying_scope = check_value->qualifying_scope;
34055 parser->object_scope = parser->context->object_type;
34056 parser->context->object_type = NULL_TREE;
34059 /* Consume tokens up through a non-nested END token. Returns TRUE if we
34060 encounter the end of a block before what we were looking for. */
34062 static bool
34063 cp_parser_cache_group (cp_parser *parser,
34064 enum cpp_ttype end,
34065 unsigned depth)
34067 while (true)
34069 cp_token *token = cp_lexer_peek_token (parser->lexer);
34071 /* Abort a parenthesized expression if we encounter a semicolon. */
34072 if ((end == CPP_CLOSE_PAREN || depth == 0)
34073 && token->type == CPP_SEMICOLON)
34074 return true;
34075 /* If we've reached the end of the file, stop. */
34076 if (token->type == CPP_EOF
34077 || (end != CPP_PRAGMA_EOL
34078 && token->type == CPP_PRAGMA_EOL))
34079 return true;
34080 if (token->type == CPP_CLOSE_BRACE && depth == 0)
34081 /* We've hit the end of an enclosing block, so there's been some
34082 kind of syntax error. */
34083 return true;
34085 /* Consume the token. */
34086 cp_lexer_consume_token (parser->lexer);
34087 /* See if it starts a new group. */
34088 if (token->type == CPP_OPEN_BRACE)
34090 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
34091 /* In theory this should probably check end == '}', but
34092 cp_parser_save_member_function_body needs it to exit
34093 after either '}' or ')' when called with ')'. */
34094 if (depth == 0)
34095 return false;
34097 else if (token->type == CPP_OPEN_PAREN)
34099 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
34100 if (depth == 0 && end == CPP_CLOSE_PAREN)
34101 return false;
34103 else if (token->type == CPP_PRAGMA)
34104 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
34105 else if (token->type == end)
34106 return false;
34110 /* Like above, for caching a default argument or NSDMI. Both of these are
34111 terminated by a non-nested comma, but it can be unclear whether or not a
34112 comma is nested in a template argument list unless we do more parsing.
34113 In order to handle this ambiguity, when we encounter a ',' after a '<'
34114 we try to parse what follows as a parameter-declaration-list (in the
34115 case of a default argument) or a member-declarator (in the case of an
34116 NSDMI). If that succeeds, then we stop caching. */
34118 static tree
34119 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
34121 unsigned depth = 0;
34122 int maybe_template_id = 0;
34123 cp_token *first_token;
34124 cp_token *token;
34125 tree default_argument;
34127 /* Add tokens until we have processed the entire default
34128 argument. We add the range [first_token, token). */
34129 first_token = cp_lexer_peek_token (parser->lexer);
34130 if (first_token->type == CPP_OPEN_BRACE)
34132 /* For list-initialization, this is straightforward. */
34133 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
34134 token = cp_lexer_peek_token (parser->lexer);
34136 else while (true)
34138 bool done = false;
34140 /* Peek at the next token. */
34141 token = cp_lexer_peek_token (parser->lexer);
34142 /* What we do depends on what token we have. */
34143 switch (token->type)
34145 /* In valid code, a default argument must be
34146 immediately followed by a `,' `)', or `...'. */
34147 case CPP_COMMA:
34148 if (depth == 0 && maybe_template_id)
34150 /* If we've seen a '<', we might be in a
34151 template-argument-list. Until Core issue 325 is
34152 resolved, we don't know how this situation ought
34153 to be handled, so try to DTRT. We check whether
34154 what comes after the comma is a valid parameter
34155 declaration list. If it is, then the comma ends
34156 the default argument; otherwise the default
34157 argument continues. */
34158 bool error = false;
34159 cp_token *peek;
34161 /* Set ITALP so cp_parser_parameter_declaration_list
34162 doesn't decide to commit to this parse. */
34163 bool saved_italp = parser->in_template_argument_list_p;
34164 parser->in_template_argument_list_p = true;
34166 cp_parser_parse_tentatively (parser);
34168 if (nsdmi)
34170 /* Parse declarators until we reach a non-comma or
34171 somthing that cannot be an initializer.
34172 Just checking whether we're looking at a single
34173 declarator is insufficient. Consider:
34174 int var = tuple<T,U>::x;
34175 The template parameter 'U' looks exactly like a
34176 declarator. */
34179 int ctor_dtor_or_conv_p;
34180 cp_lexer_consume_token (parser->lexer);
34181 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
34182 CP_PARSER_FLAGS_NONE,
34183 &ctor_dtor_or_conv_p,
34184 /*parenthesized_p=*/NULL,
34185 /*member_p=*/true,
34186 /*friend_p=*/false,
34187 /*static_p=*/false);
34188 peek = cp_lexer_peek_token (parser->lexer);
34189 if (cp_parser_error_occurred (parser))
34190 break;
34192 while (peek->type == CPP_COMMA);
34193 /* If we met an '=' or ';' then the original comma
34194 was the end of the NSDMI. Otherwise assume
34195 we're still in the NSDMI. */
34196 error = (peek->type != CPP_EQ
34197 && peek->type != CPP_SEMICOLON);
34199 else
34201 cp_lexer_consume_token (parser->lexer);
34202 begin_scope (sk_function_parms, NULL_TREE);
34203 tree t = cp_parser_parameter_declaration_list
34204 (parser, CP_PARSER_FLAGS_NONE,
34205 /*pending_decls*/nullptr);
34206 if (t == error_mark_node)
34207 error = true;
34208 pop_bindings_and_leave_scope ();
34210 if (!cp_parser_error_occurred (parser) && !error)
34211 done = true;
34212 cp_parser_abort_tentative_parse (parser);
34214 parser->in_template_argument_list_p = saved_italp;
34215 break;
34217 /* FALLTHRU */
34218 case CPP_CLOSE_PAREN:
34219 case CPP_ELLIPSIS:
34220 /* If we run into a non-nested `;', `}', or `]',
34221 then the code is invalid -- but the default
34222 argument is certainly over. */
34223 case CPP_SEMICOLON:
34224 case CPP_CLOSE_BRACE:
34225 case CPP_CLOSE_SQUARE:
34226 if (depth == 0
34227 /* Handle correctly int n = sizeof ... ( p ); */
34228 && token->type != CPP_ELLIPSIS)
34229 done = true;
34230 /* Update DEPTH, if necessary. */
34231 else if (token->type == CPP_CLOSE_PAREN
34232 || token->type == CPP_CLOSE_BRACE
34233 || token->type == CPP_CLOSE_SQUARE)
34234 --depth;
34235 break;
34237 case CPP_OPEN_PAREN:
34238 case CPP_OPEN_SQUARE:
34239 case CPP_OPEN_BRACE:
34240 ++depth;
34241 break;
34243 case CPP_LESS:
34244 if (depth == 0)
34245 /* This might be the comparison operator, or it might
34246 start a template argument list. */
34247 ++maybe_template_id;
34248 break;
34250 case CPP_RSHIFT:
34251 if (cxx_dialect == cxx98)
34252 break;
34253 /* Fall through for C++0x, which treats the `>>'
34254 operator like two `>' tokens in certain
34255 cases. */
34256 gcc_fallthrough ();
34258 case CPP_GREATER:
34259 if (depth == 0)
34261 /* This might be an operator, or it might close a
34262 template argument list. But if a previous '<'
34263 started a template argument list, this will have
34264 closed it, so we can't be in one anymore. */
34265 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
34266 if (maybe_template_id < 0)
34267 maybe_template_id = 0;
34269 break;
34271 /* If we run out of tokens, issue an error message. */
34272 case CPP_EOF:
34273 case CPP_PRAGMA_EOL:
34274 error_at (token->location, "file ends in default argument");
34275 return error_mark_node;
34277 case CPP_NAME:
34278 case CPP_SCOPE:
34279 /* In these cases, we should look for template-ids.
34280 For example, if the default argument is
34281 `X<int, double>()', we need to do name lookup to
34282 figure out whether or not `X' is a template; if
34283 so, the `,' does not end the default argument.
34285 That is not yet done. */
34286 break;
34288 default:
34289 break;
34292 /* If we've reached the end, stop. */
34293 if (done)
34294 break;
34296 /* Add the token to the token block. */
34297 token = cp_lexer_consume_token (parser->lexer);
34300 /* Create a DEFERRED_PARSE to represent the unparsed default
34301 argument. */
34302 default_argument = make_node (DEFERRED_PARSE);
34303 DEFPARSE_TOKENS (default_argument)
34304 = cp_token_cache_new (first_token, token);
34305 DEFPARSE_INSTANTIATIONS (default_argument) = NULL;
34307 return default_argument;
34310 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
34312 location_t
34313 defparse_location (tree default_argument)
34315 cp_token_cache *tokens = DEFPARSE_TOKENS (default_argument);
34316 location_t start = tokens->first->location;
34317 location_t end = tokens->last->location;
34318 return make_location (start, start, end);
34321 /* Begin parsing tentatively. We always save tokens while parsing
34322 tentatively so that if the tentative parsing fails we can restore the
34323 tokens. */
34325 static void
34326 cp_parser_parse_tentatively (cp_parser* parser)
34328 /* Enter a new parsing context. */
34329 parser->context = cp_parser_context_new (parser->context);
34330 /* Begin saving tokens. */
34331 cp_lexer_save_tokens (parser->lexer);
34332 /* In order to avoid repetitive access control error messages,
34333 access checks are queued up until we are no longer parsing
34334 tentatively. */
34335 push_deferring_access_checks (dk_deferred);
34338 /* Commit to the currently active tentative parse. */
34340 static void
34341 cp_parser_commit_to_tentative_parse (cp_parser* parser)
34343 cp_parser_context *context;
34344 cp_lexer *lexer;
34346 /* Mark all of the levels as committed. */
34347 lexer = parser->lexer;
34348 for (context = parser->context; context->next; context = context->next)
34350 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34351 break;
34352 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34353 while (!cp_lexer_saving_tokens (lexer))
34354 lexer = lexer->next;
34355 cp_lexer_commit_tokens (lexer);
34359 /* Commit to the topmost currently active tentative parse.
34361 Note that this function shouldn't be called when there are
34362 irreversible side-effects while in a tentative state. For
34363 example, we shouldn't create a permanent entry in the symbol
34364 table, or issue an error message that might not apply if the
34365 tentative parse is aborted. */
34367 static void
34368 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
34370 cp_parser_context *context = parser->context;
34371 cp_lexer *lexer = parser->lexer;
34373 if (context)
34375 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
34376 return;
34377 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
34379 while (!cp_lexer_saving_tokens (lexer))
34380 lexer = lexer->next;
34381 cp_lexer_commit_tokens (lexer);
34385 /* Abort the currently active tentative parse. All consumed tokens
34386 will be rolled back, and no diagnostics will be issued. */
34388 static void
34389 cp_parser_abort_tentative_parse (cp_parser* parser)
34391 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
34392 || errorcount > 0);
34393 cp_parser_simulate_error (parser);
34394 /* Now, pretend that we want to see if the construct was
34395 successfully parsed. */
34396 cp_parser_parse_definitely (parser);
34399 /* Stop parsing tentatively. If a parse error has occurred, restore the
34400 token stream. Otherwise, commit to the tokens we have consumed.
34401 Returns true if no error occurred; false otherwise. */
34403 static bool
34404 cp_parser_parse_definitely (cp_parser* parser)
34406 bool error_occurred;
34407 cp_parser_context *context;
34409 /* Remember whether or not an error occurred, since we are about to
34410 destroy that information. */
34411 error_occurred = cp_parser_error_occurred (parser);
34412 /* Remove the topmost context from the stack. */
34413 context = parser->context;
34414 parser->context = context->next;
34415 /* If no parse errors occurred, commit to the tentative parse. */
34416 if (!error_occurred)
34418 /* Commit to the tokens read tentatively, unless that was
34419 already done. */
34420 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
34421 cp_lexer_commit_tokens (parser->lexer);
34423 pop_to_parent_deferring_access_checks ();
34425 /* Otherwise, if errors occurred, roll back our state so that things
34426 are just as they were before we began the tentative parse. */
34427 else
34429 cp_lexer_rollback_tokens (parser->lexer);
34430 pop_deferring_access_checks ();
34432 /* Add the context to the front of the free list. */
34433 context->next = cp_parser_context_free_list;
34434 cp_parser_context_free_list = context;
34436 return !error_occurred;
34439 /* Returns true if we are parsing tentatively and are not committed to
34440 this tentative parse. */
34442 static bool
34443 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
34445 return (cp_parser_parsing_tentatively (parser)
34446 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
34449 /* Returns nonzero iff an error has occurred during the most recent
34450 tentative parse. */
34452 static bool
34453 cp_parser_error_occurred (cp_parser* parser)
34455 return (cp_parser_parsing_tentatively (parser)
34456 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
34459 /* Returns nonzero if GNU extensions are allowed. */
34461 static bool
34462 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
34464 return parser->allow_gnu_extensions_p;
34467 /* Objective-C++ Productions */
34470 /* Parse an Objective-C expression, which feeds into a primary-expression
34471 above.
34473 objc-expression:
34474 objc-message-expression
34475 objc-string-literal
34476 objc-encode-expression
34477 objc-protocol-expression
34478 objc-selector-expression
34480 Returns a tree representation of the expression. */
34482 static cp_expr
34483 cp_parser_objc_expression (cp_parser* parser)
34485 /* Try to figure out what kind of declaration is present. */
34486 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
34488 switch (kwd->type)
34490 case CPP_OPEN_SQUARE:
34491 return cp_parser_objc_message_expression (parser);
34493 case CPP_OBJC_STRING:
34494 kwd = cp_lexer_consume_token (parser->lexer);
34495 return objc_build_string_object (kwd->u.value);
34497 case CPP_KEYWORD:
34498 switch (kwd->keyword)
34500 case RID_AT_ENCODE:
34501 return cp_parser_objc_encode_expression (parser);
34503 case RID_AT_PROTOCOL:
34504 return cp_parser_objc_protocol_expression (parser);
34506 case RID_AT_SELECTOR:
34507 return cp_parser_objc_selector_expression (parser);
34509 default:
34510 break;
34512 /* FALLTHRU */
34513 default:
34514 error_at (kwd->location,
34515 "misplaced %<@%D%> Objective-C++ construct",
34516 kwd->u.value);
34517 cp_parser_skip_to_end_of_block_or_statement (parser);
34520 return error_mark_node;
34523 /* Parse an Objective-C message expression.
34525 objc-message-expression:
34526 [ objc-message-receiver objc-message-args ]
34528 Returns a representation of an Objective-C message. */
34530 static tree
34531 cp_parser_objc_message_expression (cp_parser* parser)
34533 tree receiver, messageargs;
34535 parser->objective_c_message_context_p = true;
34536 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34537 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
34538 receiver = cp_parser_objc_message_receiver (parser);
34539 messageargs = cp_parser_objc_message_args (parser);
34540 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
34541 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
34543 tree result = objc_build_message_expr (receiver, messageargs);
34545 /* Construct a location e.g.
34546 [self func1:5]
34547 ^~~~~~~~~~~~~~
34548 ranging from the '[' to the ']', with the caret at the start. */
34549 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
34550 protected_set_expr_location (result, combined_loc);
34552 parser->objective_c_message_context_p = false;
34553 return result;
34556 /* Parse an objc-message-receiver.
34558 objc-message-receiver:
34559 expression
34560 simple-type-specifier
34562 Returns a representation of the type or expression. */
34564 static tree
34565 cp_parser_objc_message_receiver (cp_parser* parser)
34567 tree rcv;
34569 /* An Objective-C message receiver may be either (1) a type
34570 or (2) an expression. */
34571 cp_parser_parse_tentatively (parser);
34572 rcv = cp_parser_expression (parser);
34574 /* If that worked out, fine. */
34575 if (cp_parser_parse_definitely (parser))
34576 return rcv;
34578 cp_parser_parse_tentatively (parser);
34579 rcv = cp_parser_simple_type_specifier (parser,
34580 /*decl_specs=*/NULL,
34581 CP_PARSER_FLAGS_NONE);
34583 if (cp_parser_parse_definitely (parser))
34584 return objc_get_class_reference (rcv);
34586 cp_parser_error (parser, "objective-c++ message receiver expected");
34587 return error_mark_node;
34590 /* Parse the arguments and selectors comprising an Objective-C message.
34592 objc-message-args:
34593 objc-selector
34594 objc-selector-args
34595 objc-selector-args , objc-comma-args
34597 objc-selector-args:
34598 objc-selector [opt] : assignment-expression
34599 objc-selector-args objc-selector [opt] : assignment-expression
34601 objc-comma-args:
34602 assignment-expression
34603 objc-comma-args , assignment-expression
34605 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
34606 selector arguments and TREE_VALUE containing a list of comma
34607 arguments. */
34609 static tree
34610 cp_parser_objc_message_args (cp_parser* parser)
34612 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
34613 bool maybe_unary_selector_p = true;
34614 cp_token *token = cp_lexer_peek_token (parser->lexer);
34616 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
34618 tree selector = NULL_TREE, arg;
34620 if (token->type != CPP_COLON)
34621 selector = cp_parser_objc_selector (parser);
34623 /* Detect if we have a unary selector. */
34624 if (maybe_unary_selector_p
34625 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
34626 return build_tree_list (selector, NULL_TREE);
34628 maybe_unary_selector_p = false;
34629 cp_parser_require (parser, CPP_COLON, RT_COLON);
34630 arg = cp_parser_assignment_expression (parser);
34632 sel_args
34633 = chainon (sel_args,
34634 build_tree_list (selector, arg));
34636 token = cp_lexer_peek_token (parser->lexer);
34639 /* Handle non-selector arguments, if any. */
34640 while (token->type == CPP_COMMA)
34642 tree arg;
34644 cp_lexer_consume_token (parser->lexer);
34645 arg = cp_parser_assignment_expression (parser);
34647 addl_args
34648 = chainon (addl_args,
34649 build_tree_list (NULL_TREE, arg));
34651 token = cp_lexer_peek_token (parser->lexer);
34654 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
34656 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
34657 return build_tree_list (error_mark_node, error_mark_node);
34660 return build_tree_list (sel_args, addl_args);
34663 /* Parse an Objective-C encode expression.
34665 objc-encode-expression:
34666 @encode objc-typename
34668 Returns an encoded representation of the type argument. */
34670 static cp_expr
34671 cp_parser_objc_encode_expression (cp_parser* parser)
34673 tree type;
34674 cp_token *token;
34675 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34677 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
34678 matching_parens parens;
34679 parens.require_open (parser);
34680 token = cp_lexer_peek_token (parser->lexer);
34681 type = complete_type (cp_parser_type_id (parser));
34682 parens.require_close (parser);
34684 if (!type)
34686 error_at (token->location,
34687 "%<@encode%> must specify a type as an argument");
34688 return error_mark_node;
34691 /* This happens if we find @encode(T) (where T is a template
34692 typename or something dependent on a template typename) when
34693 parsing a template. In that case, we can't compile it
34694 immediately, but we rather create an AT_ENCODE_EXPR which will
34695 need to be instantiated when the template is used.
34697 if (dependent_type_p (type))
34699 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
34700 TREE_READONLY (value) = 1;
34701 return value;
34705 /* Build a location of the form:
34706 @encode(int)
34707 ^~~~~~~~~~~~
34708 with caret==start at the @ token, finishing at the close paren. */
34709 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34711 return cp_expr (objc_build_encode_expr (type), combined_loc);
34714 /* Parse an Objective-C @defs expression. */
34716 static tree
34717 cp_parser_objc_defs_expression (cp_parser *parser)
34719 tree name;
34721 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
34722 matching_parens parens;
34723 parens.require_open (parser);
34724 name = cp_parser_identifier (parser);
34725 parens.require_close (parser);
34727 return objc_get_class_ivars (name);
34730 /* Parse an Objective-C protocol expression.
34732 objc-protocol-expression:
34733 @protocol ( identifier )
34735 Returns a representation of the protocol expression. */
34737 static tree
34738 cp_parser_objc_protocol_expression (cp_parser* parser)
34740 tree proto;
34741 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
34743 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
34744 matching_parens parens;
34745 parens.require_open (parser);
34746 proto = cp_parser_identifier (parser);
34747 parens.require_close (parser);
34749 /* Build a location of the form:
34750 @protocol(prot)
34751 ^~~~~~~~~~~~~~~
34752 with caret==start at the @ token, finishing at the close paren. */
34753 location_t combined_loc = make_location (start_loc, start_loc, parser->lexer);
34754 tree result = objc_build_protocol_expr (proto);
34755 protected_set_expr_location (result, combined_loc);
34756 return result;
34759 /* Parse an Objective-C selector expression.
34761 objc-selector-expression:
34762 @selector ( objc-method-signature )
34764 objc-method-signature:
34765 objc-selector
34766 objc-selector-seq
34768 objc-selector-seq:
34769 objc-selector :
34770 objc-selector-seq objc-selector :
34772 Returns a representation of the method selector. */
34774 static tree
34775 cp_parser_objc_selector_expression (cp_parser* parser)
34777 tree sel_seq = NULL_TREE;
34778 bool maybe_unary_selector_p = true;
34779 cp_token *token;
34780 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34782 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
34783 matching_parens parens;
34784 parens.require_open (parser);
34785 token = cp_lexer_peek_token (parser->lexer);
34787 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
34788 || token->type == CPP_SCOPE)
34790 tree selector = NULL_TREE;
34792 if (token->type != CPP_COLON
34793 || token->type == CPP_SCOPE)
34794 selector = cp_parser_objc_selector (parser);
34796 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
34797 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
34799 /* Detect if we have a unary selector. */
34800 if (maybe_unary_selector_p)
34802 sel_seq = selector;
34803 goto finish_selector;
34805 else
34807 cp_parser_error (parser, "expected %<:%>");
34810 maybe_unary_selector_p = false;
34811 token = cp_lexer_consume_token (parser->lexer);
34813 if (token->type == CPP_SCOPE)
34815 sel_seq
34816 = chainon (sel_seq,
34817 build_tree_list (selector, NULL_TREE));
34818 sel_seq
34819 = chainon (sel_seq,
34820 build_tree_list (NULL_TREE, NULL_TREE));
34822 else
34823 sel_seq
34824 = chainon (sel_seq,
34825 build_tree_list (selector, NULL_TREE));
34827 token = cp_lexer_peek_token (parser->lexer);
34830 finish_selector:
34831 parens.require_close (parser);
34834 /* Build a location of the form:
34835 @selector(func)
34836 ^~~~~~~~~~~~~~~
34837 with caret==start at the @ token, finishing at the close paren. */
34838 location_t combined_loc = make_location (loc, loc, parser->lexer);
34839 tree result = objc_build_selector_expr (combined_loc, sel_seq);
34840 /* TODO: objc_build_selector_expr doesn't always honor the location. */
34841 protected_set_expr_location (result, combined_loc);
34842 return result;
34845 /* Parse a list of identifiers.
34847 objc-identifier-list:
34848 identifier
34849 objc-identifier-list , identifier
34851 Returns a TREE_LIST of identifier nodes. */
34853 static tree
34854 cp_parser_objc_identifier_list (cp_parser* parser)
34856 tree identifier;
34857 tree list;
34858 cp_token *sep;
34860 identifier = cp_parser_identifier (parser);
34861 if (identifier == error_mark_node)
34862 return error_mark_node;
34864 list = build_tree_list (NULL_TREE, identifier);
34865 sep = cp_lexer_peek_token (parser->lexer);
34867 while (sep->type == CPP_COMMA)
34869 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
34870 identifier = cp_parser_identifier (parser);
34871 if (identifier == error_mark_node)
34872 return list;
34874 list = chainon (list, build_tree_list (NULL_TREE,
34875 identifier));
34876 sep = cp_lexer_peek_token (parser->lexer);
34879 return list;
34882 /* Parse an Objective-C alias declaration.
34884 objc-alias-declaration:
34885 @compatibility_alias identifier identifier ;
34887 This function registers the alias mapping with the Objective-C front end.
34888 It returns nothing. */
34890 static void
34891 cp_parser_objc_alias_declaration (cp_parser* parser)
34893 tree alias, orig;
34895 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
34896 alias = cp_parser_identifier (parser);
34897 orig = cp_parser_identifier (parser);
34898 objc_declare_alias (alias, orig);
34899 cp_parser_consume_semicolon_at_end_of_statement (parser);
34902 /* Parse an Objective-C class forward-declaration.
34904 objc-class-declaration:
34905 @class objc-identifier-list ;
34907 The function registers the forward declarations with the Objective-C
34908 front end. It returns nothing. */
34910 static void
34911 cp_parser_objc_class_declaration (cp_parser* parser)
34913 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
34914 while (true)
34916 tree id;
34918 id = cp_parser_identifier (parser);
34919 if (id == error_mark_node)
34920 break;
34922 objc_declare_class (id);
34924 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
34925 cp_lexer_consume_token (parser->lexer);
34926 else
34927 break;
34929 cp_parser_consume_semicolon_at_end_of_statement (parser);
34932 /* Parse a list of Objective-C protocol references.
34934 objc-protocol-refs-opt:
34935 objc-protocol-refs [opt]
34937 objc-protocol-refs:
34938 < objc-identifier-list >
34940 Returns a TREE_LIST of identifiers, if any. */
34942 static tree
34943 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
34945 tree protorefs = NULL_TREE;
34947 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
34949 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
34950 protorefs = cp_parser_objc_identifier_list (parser);
34951 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
34954 return protorefs;
34957 /* Parse a Objective-C visibility specification. */
34959 static void
34960 cp_parser_objc_visibility_spec (cp_parser* parser)
34962 cp_token *vis = cp_lexer_peek_token (parser->lexer);
34964 switch (vis->keyword)
34966 case RID_AT_PRIVATE:
34967 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
34968 break;
34969 case RID_AT_PROTECTED:
34970 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
34971 break;
34972 case RID_AT_PUBLIC:
34973 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
34974 break;
34975 case RID_AT_PACKAGE:
34976 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
34977 break;
34978 default:
34979 return;
34982 /* Eat '@private'/'@protected'/'@public'. */
34983 cp_lexer_consume_token (parser->lexer);
34986 /* Parse an Objective-C method type. Return 'true' if it is a class
34987 (+) method, and 'false' if it is an instance (-) method. */
34989 static inline bool
34990 cp_parser_objc_method_type (cp_parser* parser)
34992 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
34993 return true;
34994 else
34995 return false;
34998 /* Parse an Objective-C protocol qualifier. */
35000 static tree
35001 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
35003 tree quals = NULL_TREE, node;
35004 cp_token *token = cp_lexer_peek_token (parser->lexer);
35006 node = token->u.value;
35008 while (node && identifier_p (node)
35009 && (node == ridpointers [(int) RID_IN]
35010 || node == ridpointers [(int) RID_OUT]
35011 || node == ridpointers [(int) RID_INOUT]
35012 || node == ridpointers [(int) RID_BYCOPY]
35013 || node == ridpointers [(int) RID_BYREF]
35014 || node == ridpointers [(int) RID_ONEWAY]))
35016 quals = tree_cons (NULL_TREE, node, quals);
35017 cp_lexer_consume_token (parser->lexer);
35018 token = cp_lexer_peek_token (parser->lexer);
35019 node = token->u.value;
35022 return quals;
35025 /* Parse an Objective-C typename. */
35027 static tree
35028 cp_parser_objc_typename (cp_parser* parser)
35030 tree type_name = NULL_TREE;
35032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35034 tree proto_quals, cp_type = NULL_TREE;
35036 matching_parens parens;
35037 parens.consume_open (parser); /* Eat '('. */
35038 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
35040 /* An ObjC type name may consist of just protocol qualifiers, in which
35041 case the type shall default to 'id'. */
35042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35044 cp_type = cp_parser_type_id (parser);
35046 /* If the type could not be parsed, an error has already
35047 been produced. For error recovery, behave as if it had
35048 not been specified, which will use the default type
35049 'id'. */
35050 if (cp_type == error_mark_node)
35052 cp_type = NULL_TREE;
35053 /* We need to skip to the closing parenthesis as
35054 cp_parser_type_id() does not seem to do it for
35055 us. */
35056 cp_parser_skip_to_closing_parenthesis (parser,
35057 /*recovering=*/true,
35058 /*or_comma=*/false,
35059 /*consume_paren=*/false);
35063 parens.require_close (parser);
35064 type_name = build_tree_list (proto_quals, cp_type);
35067 return type_name;
35070 /* Check to see if TYPE refers to an Objective-C selector name. */
35072 static bool
35073 cp_parser_objc_selector_p (enum cpp_ttype type)
35075 return (type == CPP_NAME || type == CPP_KEYWORD
35076 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
35077 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
35078 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
35079 || type == CPP_XOR || type == CPP_XOR_EQ);
35082 /* Parse an Objective-C selector. */
35084 static tree
35085 cp_parser_objc_selector (cp_parser* parser)
35087 cp_token *token = cp_lexer_consume_token (parser->lexer);
35089 if (!cp_parser_objc_selector_p (token->type))
35091 error_at (token->location, "invalid Objective-C++ selector name");
35092 return error_mark_node;
35095 /* C++ operator names are allowed to appear in ObjC selectors. */
35096 switch (token->type)
35098 case CPP_AND_AND: return get_identifier ("and");
35099 case CPP_AND_EQ: return get_identifier ("and_eq");
35100 case CPP_AND: return get_identifier ("bitand");
35101 case CPP_OR: return get_identifier ("bitor");
35102 case CPP_COMPL: return get_identifier ("compl");
35103 case CPP_NOT: return get_identifier ("not");
35104 case CPP_NOT_EQ: return get_identifier ("not_eq");
35105 case CPP_OR_OR: return get_identifier ("or");
35106 case CPP_OR_EQ: return get_identifier ("or_eq");
35107 case CPP_XOR: return get_identifier ("xor");
35108 case CPP_XOR_EQ: return get_identifier ("xor_eq");
35109 default: return token->u.value;
35113 /* Parse an Objective-C params list. */
35115 static tree
35116 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
35118 tree params = NULL_TREE;
35119 bool maybe_unary_selector_p = true;
35120 cp_token *token = cp_lexer_peek_token (parser->lexer);
35122 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
35124 tree selector = NULL_TREE, type_name, identifier;
35125 tree parm_attr = NULL_TREE;
35127 if (token->keyword == RID_ATTRIBUTE)
35128 break;
35130 if (token->type != CPP_COLON)
35131 selector = cp_parser_objc_selector (parser);
35133 /* Detect if we have a unary selector. */
35134 if (maybe_unary_selector_p
35135 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
35137 params = selector; /* Might be followed by attributes. */
35138 break;
35141 maybe_unary_selector_p = false;
35142 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
35144 /* Something went quite wrong. There should be a colon
35145 here, but there is not. Stop parsing parameters. */
35146 break;
35148 type_name = cp_parser_objc_typename (parser);
35149 /* New ObjC allows attributes on parameters too. */
35150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
35151 parm_attr = cp_parser_attributes_opt (parser);
35152 identifier = cp_parser_identifier (parser);
35154 params
35155 = chainon (params,
35156 objc_build_keyword_decl (selector,
35157 type_name,
35158 identifier,
35159 parm_attr));
35161 token = cp_lexer_peek_token (parser->lexer);
35164 if (params == NULL_TREE)
35166 cp_parser_error (parser, "objective-c++ method declaration is expected");
35167 return error_mark_node;
35170 /* We allow tail attributes for the method. */
35171 if (token->keyword == RID_ATTRIBUTE)
35173 *attributes = cp_parser_attributes_opt (parser);
35174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35175 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35176 return params;
35177 cp_parser_error (parser,
35178 "method attributes must be specified at the end");
35179 return error_mark_node;
35182 if (params == NULL_TREE)
35184 cp_parser_error (parser, "objective-c++ method declaration is expected");
35185 return error_mark_node;
35187 return params;
35190 /* Parse the non-keyword Objective-C params. */
35192 static tree
35193 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
35194 tree* attributes)
35196 tree params = make_node (TREE_LIST);
35197 cp_token *token = cp_lexer_peek_token (parser->lexer);
35198 *ellipsisp = false; /* Initially, assume no ellipsis. */
35200 while (token->type == CPP_COMMA)
35202 cp_parameter_declarator *parmdecl;
35203 tree parm;
35205 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35206 token = cp_lexer_peek_token (parser->lexer);
35208 if (token->type == CPP_ELLIPSIS)
35210 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
35211 *ellipsisp = true;
35212 token = cp_lexer_peek_token (parser->lexer);
35213 break;
35216 /* TODO: parse attributes for tail parameters. */
35217 parmdecl = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35218 false, NULL);
35219 parm = grokdeclarator (parmdecl->declarator,
35220 &parmdecl->decl_specifiers,
35221 PARM, /*initialized=*/0,
35222 /*attrlist=*/NULL);
35224 chainon (params, build_tree_list (NULL_TREE, parm));
35225 token = cp_lexer_peek_token (parser->lexer);
35228 /* We allow tail attributes for the method. */
35229 if (token->keyword == RID_ATTRIBUTE)
35231 if (*attributes == NULL_TREE)
35233 *attributes = cp_parser_attributes_opt (parser);
35234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
35235 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35236 return params;
35238 else
35239 /* We have an error, but parse the attributes, so that we can
35240 carry on. */
35241 *attributes = cp_parser_attributes_opt (parser);
35243 cp_parser_error (parser,
35244 "method attributes must be specified at the end");
35245 return error_mark_node;
35248 return params;
35251 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
35253 static void
35254 cp_parser_objc_interstitial_code (cp_parser* parser)
35256 cp_token *token = cp_lexer_peek_token (parser->lexer);
35258 /* If the next token is `extern' and the following token is a string
35259 literal, then we have a linkage specification. */
35260 if (token->keyword == RID_EXTERN
35261 && cp_parser_is_pure_string_literal
35262 (cp_lexer_peek_nth_token (parser->lexer, 2)))
35263 cp_parser_linkage_specification (parser, NULL_TREE);
35264 /* Handle #pragma, if any. */
35265 else if (token->type == CPP_PRAGMA)
35266 cp_parser_pragma (parser, pragma_objc_icode, NULL);
35267 /* Allow stray semicolons. */
35268 else if (token->type == CPP_SEMICOLON)
35269 cp_lexer_consume_token (parser->lexer);
35270 /* Mark methods as optional or required, when building protocols. */
35271 else if (token->keyword == RID_AT_OPTIONAL)
35273 cp_lexer_consume_token (parser->lexer);
35274 objc_set_method_opt (true);
35276 else if (token->keyword == RID_AT_REQUIRED)
35278 cp_lexer_consume_token (parser->lexer);
35279 objc_set_method_opt (false);
35281 else if (token->keyword == RID_NAMESPACE)
35282 cp_parser_namespace_definition (parser);
35283 /* Other stray characters must generate errors. */
35284 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
35286 cp_lexer_consume_token (parser->lexer);
35287 error ("stray %qs between Objective-C++ methods",
35288 token->type == CPP_OPEN_BRACE ? "{" : "}");
35290 /* Finally, try to parse a block-declaration, or a function-definition. */
35291 else
35292 cp_parser_block_declaration (parser, /*statement_p=*/false);
35295 /* Parse a method signature. */
35297 static tree
35298 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
35300 tree rettype, kwdparms, optparms;
35301 bool ellipsis = false;
35302 bool is_class_method;
35304 is_class_method = cp_parser_objc_method_type (parser);
35305 rettype = cp_parser_objc_typename (parser);
35306 *attributes = NULL_TREE;
35307 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
35308 if (kwdparms == error_mark_node)
35309 return error_mark_node;
35310 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
35311 if (optparms == error_mark_node)
35312 return error_mark_node;
35314 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
35317 static bool
35318 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
35320 tree tattr;
35321 cp_lexer_save_tokens (parser->lexer);
35322 tattr = cp_parser_attributes_opt (parser);
35323 gcc_assert (tattr) ;
35325 /* If the attributes are followed by a method introducer, this is not allowed.
35326 Dump the attributes and flag the situation. */
35327 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
35328 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
35329 return true;
35331 /* Otherwise, the attributes introduce some interstitial code, possibly so
35332 rewind to allow that check. */
35333 cp_lexer_rollback_tokens (parser->lexer);
35334 return false;
35337 /* Parse an Objective-C method prototype list. */
35339 static void
35340 cp_parser_objc_method_prototype_list (cp_parser* parser)
35342 cp_token *token = cp_lexer_peek_token (parser->lexer);
35344 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
35346 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35348 tree attributes, sig;
35349 bool is_class_method;
35350 if (token->type == CPP_PLUS)
35351 is_class_method = true;
35352 else
35353 is_class_method = false;
35354 sig = cp_parser_objc_method_signature (parser, &attributes);
35355 if (sig == error_mark_node)
35357 cp_parser_skip_to_end_of_block_or_statement (parser);
35358 token = cp_lexer_peek_token (parser->lexer);
35359 continue;
35361 objc_add_method_declaration (is_class_method, sig, attributes);
35362 cp_parser_consume_semicolon_at_end_of_statement (parser);
35364 else if (token->keyword == RID_AT_PROPERTY)
35365 cp_parser_objc_at_property_declaration (parser);
35366 else if (token->keyword == RID_ATTRIBUTE
35367 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35368 warning_at (cp_lexer_peek_token (parser->lexer)->location,
35369 OPT_Wattributes,
35370 "prefix attributes are ignored for methods");
35371 else
35372 /* Allow for interspersed non-ObjC++ code. */
35373 cp_parser_objc_interstitial_code (parser);
35375 token = cp_lexer_peek_token (parser->lexer);
35378 if (token->type != CPP_EOF)
35379 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35380 else
35381 cp_parser_error (parser, "expected %<@end%>");
35383 objc_finish_interface ();
35386 /* Parse an Objective-C method definition list. */
35388 static void
35389 cp_parser_objc_method_definition_list (cp_parser* parser)
35391 for (;;)
35393 cp_token *token = cp_lexer_peek_token (parser->lexer);
35395 if (token->keyword == RID_AT_END)
35397 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35398 break;
35400 else if (token->type == CPP_EOF)
35402 cp_parser_error (parser, "expected %<@end%>");
35403 break;
35405 else if (token->type == CPP_PLUS || token->type == CPP_MINUS)
35407 bool is_class_method = token->type == CPP_PLUS;
35409 push_deferring_access_checks (dk_deferred);
35410 tree attribute;
35411 tree sig = cp_parser_objc_method_signature (parser, &attribute);
35412 if (sig == error_mark_node)
35413 cp_parser_skip_to_end_of_block_or_statement (parser);
35414 else
35416 objc_start_method_definition (is_class_method, sig,
35417 attribute, NULL_TREE);
35419 /* For historical reasons, we accept an optional semicolon. */
35420 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35421 cp_lexer_consume_token (parser->lexer);
35423 perform_deferred_access_checks (tf_warning_or_error);
35424 stop_deferring_access_checks ();
35425 tree meth
35426 = cp_parser_function_definition_after_declarator (parser, false);
35427 pop_deferring_access_checks ();
35428 objc_finish_method_definition (meth);
35431 /* The following case will be removed once @synthesize is
35432 completely implemented. */
35433 else if (token->keyword == RID_AT_PROPERTY)
35434 cp_parser_objc_at_property_declaration (parser);
35435 else if (token->keyword == RID_AT_SYNTHESIZE)
35436 cp_parser_objc_at_synthesize_declaration (parser);
35437 else if (token->keyword == RID_AT_DYNAMIC)
35438 cp_parser_objc_at_dynamic_declaration (parser);
35439 else if (token->keyword == RID_ATTRIBUTE
35440 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
35441 warning_at (token->location, OPT_Wattributes,
35442 "prefix attributes are ignored for methods");
35443 else
35444 /* Allow for interspersed non-ObjC++ code. */
35445 cp_parser_objc_interstitial_code (parser);
35448 objc_finish_implementation ();
35451 /* Parse Objective-C ivars. */
35453 static void
35454 cp_parser_objc_class_ivars (cp_parser* parser)
35456 cp_token *token = cp_lexer_peek_token (parser->lexer);
35458 if (token->type != CPP_OPEN_BRACE)
35459 return; /* No ivars specified. */
35461 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
35462 token = cp_lexer_peek_token (parser->lexer);
35464 while (token->type != CPP_CLOSE_BRACE
35465 && token->keyword != RID_AT_END && token->type != CPP_EOF)
35467 cp_decl_specifier_seq declspecs;
35468 int decl_class_or_enum_p;
35469 tree prefix_attributes;
35471 cp_parser_objc_visibility_spec (parser);
35473 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35474 break;
35476 cp_parser_decl_specifier_seq (parser,
35477 CP_PARSER_FLAGS_OPTIONAL,
35478 &declspecs,
35479 &decl_class_or_enum_p);
35481 /* auto, register, static, extern, mutable. */
35482 if (declspecs.storage_class != sc_none)
35484 cp_parser_error (parser, "invalid type for instance variable");
35485 declspecs.storage_class = sc_none;
35488 /* thread_local. */
35489 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
35491 cp_parser_error (parser, "invalid type for instance variable");
35492 declspecs.locations[ds_thread] = 0;
35495 /* typedef. */
35496 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
35498 cp_parser_error (parser, "invalid type for instance variable");
35499 declspecs.locations[ds_typedef] = 0;
35502 prefix_attributes = declspecs.attributes;
35503 declspecs.attributes = NULL_TREE;
35505 /* Keep going until we hit the `;' at the end of the
35506 declaration. */
35507 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35509 tree width = NULL_TREE, attributes, first_attribute, decl;
35510 cp_declarator *declarator = NULL;
35511 int ctor_dtor_or_conv_p;
35513 /* Check for a (possibly unnamed) bitfield declaration. */
35514 token = cp_lexer_peek_token (parser->lexer);
35515 if (token->type == CPP_COLON)
35516 goto eat_colon;
35518 if (token->type == CPP_NAME
35519 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
35520 == CPP_COLON))
35522 /* Get the name of the bitfield. */
35523 declarator = make_id_declarator (NULL_TREE,
35524 cp_parser_identifier (parser),
35525 sfk_none, token->location);
35527 eat_colon:
35528 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35529 /* Get the width of the bitfield. */
35530 width
35531 = cp_parser_constant_expression (parser);
35533 else
35535 /* Parse the declarator. */
35536 declarator
35537 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
35538 CP_PARSER_FLAGS_NONE,
35539 &ctor_dtor_or_conv_p,
35540 /*parenthesized_p=*/NULL,
35541 /*member_p=*/false,
35542 /*friend_p=*/false,
35543 /*static_p=*/false);
35546 /* Look for attributes that apply to the ivar. */
35547 attributes = cp_parser_attributes_opt (parser);
35548 /* Remember which attributes are prefix attributes and
35549 which are not. */
35550 first_attribute = attributes;
35551 /* Combine the attributes. */
35552 attributes = attr_chainon (prefix_attributes, attributes);
35554 if (width)
35555 /* Create the bitfield declaration. */
35556 decl = grokbitfield (declarator, &declspecs,
35557 width, NULL_TREE, attributes);
35558 else
35559 decl = grokfield (declarator, &declspecs,
35560 NULL_TREE, /*init_const_expr_p=*/false,
35561 NULL_TREE, attributes);
35563 /* Add the instance variable. */
35564 if (decl != error_mark_node && decl != NULL_TREE)
35565 objc_add_instance_variable (decl);
35567 /* Reset PREFIX_ATTRIBUTES. */
35568 if (attributes != error_mark_node)
35570 while (attributes && TREE_CHAIN (attributes) != first_attribute)
35571 attributes = TREE_CHAIN (attributes);
35572 if (attributes)
35573 TREE_CHAIN (attributes) = NULL_TREE;
35576 token = cp_lexer_peek_token (parser->lexer);
35578 if (token->type == CPP_COMMA)
35580 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
35581 continue;
35583 break;
35586 cp_parser_consume_semicolon_at_end_of_statement (parser);
35587 token = cp_lexer_peek_token (parser->lexer);
35590 if (token->keyword == RID_AT_END)
35591 cp_parser_error (parser, "expected %<}%>");
35593 /* Do not consume the RID_AT_END, so it will be read again as terminating
35594 the @interface of @implementation. */
35595 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
35596 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
35598 /* For historical reasons, we accept an optional semicolon. */
35599 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35600 cp_lexer_consume_token (parser->lexer);
35603 /* Parse an Objective-C protocol declaration. */
35605 static void
35606 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
35608 tree proto, protorefs;
35609 cp_token *tok;
35611 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
35612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35614 tok = cp_lexer_peek_token (parser->lexer);
35615 error_at (tok->location, "identifier expected after %<@protocol%>");
35616 cp_parser_consume_semicolon_at_end_of_statement (parser);
35617 return;
35620 /* See if we have a forward declaration or a definition. */
35621 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
35623 /* Try a forward declaration first. */
35624 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
35626 while (true)
35628 tree id;
35630 id = cp_parser_identifier (parser);
35631 if (id == error_mark_node)
35632 break;
35634 objc_declare_protocol (id, attributes);
35636 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
35637 cp_lexer_consume_token (parser->lexer);
35638 else
35639 break;
35641 cp_parser_consume_semicolon_at_end_of_statement (parser);
35644 /* Ok, we got a full-fledged definition (or at least should). */
35645 else
35647 proto = cp_parser_identifier (parser);
35648 protorefs = cp_parser_objc_protocol_refs_opt (parser);
35649 objc_start_protocol (proto, protorefs, attributes);
35650 cp_parser_objc_method_prototype_list (parser);
35654 /* Parse an Objective-C superclass or category. */
35656 static void
35657 cp_parser_objc_superclass_or_category (cp_parser *parser,
35658 bool iface_p,
35659 tree *super,
35660 tree *categ, bool *is_class_extension)
35662 cp_token *next = cp_lexer_peek_token (parser->lexer);
35664 *super = *categ = NULL_TREE;
35665 *is_class_extension = false;
35666 if (next->type == CPP_COLON)
35668 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
35669 *super = cp_parser_identifier (parser);
35671 else if (next->type == CPP_OPEN_PAREN)
35673 matching_parens parens;
35674 parens.consume_open (parser); /* Eat '('. */
35676 /* If there is no category name, and this is an @interface, we
35677 have a class extension. */
35678 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35680 *categ = NULL_TREE;
35681 *is_class_extension = true;
35683 else
35684 *categ = cp_parser_identifier (parser);
35686 parens.require_close (parser);
35690 /* Parse an Objective-C class interface. */
35692 static void
35693 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
35695 tree name, super, categ, protos;
35696 bool is_class_extension;
35698 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
35699 location_t nam_loc = cp_lexer_peek_token (parser->lexer)->location;
35700 name = cp_parser_identifier (parser);
35701 if (name == error_mark_node)
35703 /* It's hard to recover because even if valid @interface stuff
35704 is to follow, we can't compile it (or validate it) if we
35705 don't even know which class it refers to. Let's assume this
35706 was a stray '@interface' token in the stream and skip it.
35708 return;
35710 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
35711 &is_class_extension);
35712 protos = cp_parser_objc_protocol_refs_opt (parser);
35714 /* We have either a class or a category on our hands. */
35715 if (categ || is_class_extension)
35716 objc_start_category_interface (name, categ, protos, attributes);
35717 else
35719 objc_start_class_interface (name, nam_loc, super, protos, attributes);
35720 /* Handle instance variable declarations, if any. */
35721 cp_parser_objc_class_ivars (parser);
35722 objc_continue_interface ();
35725 cp_parser_objc_method_prototype_list (parser);
35728 /* Parse an Objective-C class implementation. */
35730 static void
35731 cp_parser_objc_class_implementation (cp_parser* parser)
35733 tree name, super, categ;
35734 bool is_class_extension;
35736 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
35737 name = cp_parser_identifier (parser);
35738 if (name == error_mark_node)
35740 /* It's hard to recover because even if valid @implementation
35741 stuff is to follow, we can't compile it (or validate it) if
35742 we don't even know which class it refers to. Let's assume
35743 this was a stray '@implementation' token in the stream and
35744 skip it.
35746 return;
35748 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
35749 &is_class_extension);
35751 /* We have either a class or a category on our hands. */
35752 if (categ)
35753 objc_start_category_implementation (name, categ);
35754 else
35756 objc_start_class_implementation (name, super);
35757 /* Handle instance variable declarations, if any. */
35758 cp_parser_objc_class_ivars (parser);
35759 objc_continue_implementation ();
35762 cp_parser_objc_method_definition_list (parser);
35765 /* Consume the @end token and finish off the implementation. */
35767 static void
35768 cp_parser_objc_end_implementation (cp_parser* parser)
35770 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
35771 objc_finish_implementation ();
35774 /* Parse an Objective-C declaration. */
35776 static void
35777 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
35779 /* Try to figure out what kind of declaration is present. */
35780 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
35782 if (attributes)
35783 switch (kwd->keyword)
35785 case RID_AT_ALIAS:
35786 case RID_AT_CLASS:
35787 case RID_AT_END:
35788 error_at (kwd->location, "attributes may not be specified before"
35789 " the %<@%D%> Objective-C++ keyword",
35790 kwd->u.value);
35791 attributes = NULL;
35792 break;
35793 case RID_AT_IMPLEMENTATION:
35794 warning_at (kwd->location, OPT_Wattributes,
35795 "prefix attributes are ignored before %<@%D%>",
35796 kwd->u.value);
35797 attributes = NULL;
35798 default:
35799 break;
35802 switch (kwd->keyword)
35804 case RID_AT_ALIAS:
35805 cp_parser_objc_alias_declaration (parser);
35806 break;
35807 case RID_AT_CLASS:
35808 cp_parser_objc_class_declaration (parser);
35809 break;
35810 case RID_AT_PROTOCOL:
35811 cp_parser_objc_protocol_declaration (parser, attributes);
35812 break;
35813 case RID_AT_INTERFACE:
35814 cp_parser_objc_class_interface (parser, attributes);
35815 break;
35816 case RID_AT_IMPLEMENTATION:
35817 cp_parser_objc_class_implementation (parser);
35818 break;
35819 case RID_AT_END:
35820 cp_parser_objc_end_implementation (parser);
35821 break;
35822 default:
35823 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
35824 kwd->u.value);
35825 cp_parser_skip_to_end_of_block_or_statement (parser);
35829 /* Parse an Objective-C try-catch-finally statement.
35831 objc-try-catch-finally-stmt:
35832 @try compound-statement objc-catch-clause-seq [opt]
35833 objc-finally-clause [opt]
35835 objc-catch-clause-seq:
35836 objc-catch-clause objc-catch-clause-seq [opt]
35838 objc-catch-clause:
35839 @catch ( objc-exception-declaration ) compound-statement
35841 objc-finally-clause:
35842 @finally compound-statement
35844 objc-exception-declaration:
35845 parameter-declaration
35846 '...'
35848 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
35850 Returns NULL_TREE.
35852 PS: This function is identical to c_parser_objc_try_catch_finally_statement
35853 for C. Keep them in sync. */
35855 static tree
35856 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
35858 location_t location;
35859 tree stmt;
35861 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
35862 location = cp_lexer_peek_token (parser->lexer)->location;
35863 objc_maybe_warn_exceptions (location);
35864 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
35865 node, lest it get absorbed into the surrounding block. */
35866 stmt = push_stmt_list ();
35867 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35868 objc_begin_try_stmt (location, pop_stmt_list (stmt));
35870 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
35872 cp_parameter_declarator *parm;
35873 tree parameter_declaration = error_mark_node;
35874 bool seen_open_paren = false;
35875 matching_parens parens;
35877 cp_lexer_consume_token (parser->lexer);
35878 if (parens.require_open (parser))
35879 seen_open_paren = true;
35880 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
35882 /* We have "@catch (...)" (where the '...' are literally
35883 what is in the code). Skip the '...'.
35884 parameter_declaration is set to NULL_TREE, and
35885 objc_being_catch_clauses() knows that that means
35886 '...'. */
35887 cp_lexer_consume_token (parser->lexer);
35888 parameter_declaration = NULL_TREE;
35890 else
35892 /* We have "@catch (NSException *exception)" or something
35893 like that. Parse the parameter declaration. */
35894 parm = cp_parser_parameter_declaration (parser, CP_PARSER_FLAGS_NONE,
35895 false, NULL);
35896 if (parm == NULL)
35897 parameter_declaration = error_mark_node;
35898 else
35899 parameter_declaration = grokdeclarator (parm->declarator,
35900 &parm->decl_specifiers,
35901 PARM, /*initialized=*/0,
35902 /*attrlist=*/NULL);
35904 if (seen_open_paren)
35905 parens.require_close (parser);
35906 else
35908 /* If there was no open parenthesis, we are recovering from
35909 an error, and we are trying to figure out what mistake
35910 the user has made. */
35912 /* If there is an immediate closing parenthesis, the user
35913 probably forgot the opening one (ie, they typed "@catch
35914 NSException *e)". Parse the closing parenthesis and keep
35915 going. */
35916 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
35917 cp_lexer_consume_token (parser->lexer);
35919 /* If these is no immediate closing parenthesis, the user
35920 probably doesn't know that parenthesis are required at
35921 all (ie, they typed "@catch NSException *e"). So, just
35922 forget about the closing parenthesis and keep going. */
35924 objc_begin_catch_clause (parameter_declaration);
35925 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35926 objc_finish_catch_clause ();
35928 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
35930 cp_lexer_consume_token (parser->lexer);
35931 location = cp_lexer_peek_token (parser->lexer)->location;
35932 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
35933 node, lest it get absorbed into the surrounding block. */
35934 stmt = push_stmt_list ();
35935 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35936 objc_build_finally_clause (location, pop_stmt_list (stmt));
35939 return objc_finish_try_stmt ();
35942 /* Parse an Objective-C synchronized statement.
35944 objc-synchronized-stmt:
35945 @synchronized ( expression ) compound-statement
35947 Returns NULL_TREE. */
35949 static tree
35950 cp_parser_objc_synchronized_statement (cp_parser *parser)
35952 location_t location;
35953 tree lock, stmt;
35955 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
35957 location = cp_lexer_peek_token (parser->lexer)->location;
35958 objc_maybe_warn_exceptions (location);
35959 matching_parens parens;
35960 parens.require_open (parser);
35961 lock = cp_parser_expression (parser);
35962 parens.require_close (parser);
35964 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
35965 node, lest it get absorbed into the surrounding block. */
35966 stmt = push_stmt_list ();
35967 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
35969 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
35972 /* Parse an Objective-C throw statement.
35974 objc-throw-stmt:
35975 @throw assignment-expression [opt] ;
35977 Returns a constructed '@throw' statement. */
35979 static tree
35980 cp_parser_objc_throw_statement (cp_parser *parser)
35982 tree expr = NULL_TREE;
35983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35985 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
35987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35988 expr = cp_parser_expression (parser);
35990 cp_parser_consume_semicolon_at_end_of_statement (parser);
35992 return objc_build_throw_stmt (loc, expr);
35995 /* Parse an Objective-C statement. */
35997 static tree
35998 cp_parser_objc_statement (cp_parser * parser)
36000 /* Try to figure out what kind of declaration is present. */
36001 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
36003 switch (kwd->keyword)
36005 case RID_AT_TRY:
36006 return cp_parser_objc_try_catch_finally_statement (parser);
36007 case RID_AT_SYNCHRONIZED:
36008 return cp_parser_objc_synchronized_statement (parser);
36009 case RID_AT_THROW:
36010 return cp_parser_objc_throw_statement (parser);
36011 default:
36012 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
36013 kwd->u.value);
36014 cp_parser_skip_to_end_of_block_or_statement (parser);
36017 return error_mark_node;
36020 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
36021 look ahead to see if an objc keyword follows the attributes. This
36022 is to detect the use of prefix attributes on ObjC @interface and
36023 @protocol. */
36025 static bool
36026 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
36028 cp_lexer_save_tokens (parser->lexer);
36029 tree addon = cp_parser_attributes_opt (parser);
36030 if (addon
36031 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
36033 cp_lexer_commit_tokens (parser->lexer);
36034 if (*attrib)
36035 TREE_CHAIN (*attrib) = addon;
36036 else
36037 *attrib = addon;
36038 return true;
36040 cp_lexer_rollback_tokens (parser->lexer);
36041 return false;
36044 /* This routine is a minimal replacement for
36045 c_parser_struct_declaration () used when parsing the list of
36046 types/names or ObjC++ properties. For example, when parsing the
36047 code
36049 @property (readonly) int a, b, c;
36051 this function is responsible for parsing "int a, int b, int c" and
36052 returning the declarations as CHAIN of DECLs.
36054 TODO: Share this code with cp_parser_objc_class_ivars. It's very
36055 similar parsing. */
36056 static tree
36057 cp_parser_objc_struct_declaration (cp_parser *parser)
36059 tree decls = NULL_TREE;
36060 cp_decl_specifier_seq declspecs;
36061 int decl_class_or_enum_p;
36062 tree prefix_attributes;
36064 cp_parser_decl_specifier_seq (parser,
36065 CP_PARSER_FLAGS_NONE,
36066 &declspecs,
36067 &decl_class_or_enum_p);
36069 if (declspecs.type == error_mark_node)
36070 return error_mark_node;
36072 /* auto, register, static, extern, mutable. */
36073 if (declspecs.storage_class != sc_none)
36075 cp_parser_error (parser, "invalid type for property");
36076 declspecs.storage_class = sc_none;
36079 /* thread_local. */
36080 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
36082 cp_parser_error (parser, "invalid type for property");
36083 declspecs.locations[ds_thread] = 0;
36086 /* typedef. */
36087 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
36089 cp_parser_error (parser, "invalid type for property");
36090 declspecs.locations[ds_typedef] = 0;
36093 prefix_attributes = declspecs.attributes;
36094 declspecs.attributes = NULL_TREE;
36096 /* Keep going until we hit the `;' at the end of the declaration. */
36097 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
36099 tree attributes, first_attribute, decl;
36100 cp_declarator *declarator;
36101 cp_token *token;
36103 /* Parse the declarator. */
36104 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
36105 CP_PARSER_FLAGS_NONE,
36106 NULL, NULL, false, false, false);
36108 /* Look for attributes that apply to the ivar. */
36109 attributes = cp_parser_attributes_opt (parser);
36110 /* Remember which attributes are prefix attributes and
36111 which are not. */
36112 first_attribute = attributes;
36113 /* Combine the attributes. */
36114 attributes = attr_chainon (prefix_attributes, attributes);
36116 decl = grokfield (declarator, &declspecs,
36117 NULL_TREE, /*init_const_expr_p=*/false,
36118 NULL_TREE, attributes);
36120 if (decl == error_mark_node || decl == NULL_TREE)
36121 return error_mark_node;
36123 /* Reset PREFIX_ATTRIBUTES. */
36124 if (attributes != error_mark_node)
36126 while (attributes && TREE_CHAIN (attributes) != first_attribute)
36127 attributes = TREE_CHAIN (attributes);
36128 if (attributes)
36129 TREE_CHAIN (attributes) = NULL_TREE;
36132 DECL_CHAIN (decl) = decls;
36133 decls = decl;
36135 token = cp_lexer_peek_token (parser->lexer);
36136 if (token->type == CPP_COMMA)
36138 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
36139 continue;
36141 else
36142 break;
36144 return decls;
36147 /* Parse an Objective-C @property declaration. The syntax is:
36149 objc-property-declaration:
36150 '@property' objc-property-attributes[opt] struct-declaration ;
36152 objc-property-attributes:
36153 '(' objc-property-attribute-list ')'
36155 objc-property-attribute-list:
36156 objc-property-attribute
36157 objc-property-attribute-list, objc-property-attribute
36159 objc-property-attribute
36160 'getter' = identifier
36161 'setter' = identifier
36162 'readonly'
36163 'readwrite'
36164 'assign'
36165 'retain'
36166 'copy'
36167 'nonatomic'
36169 For example:
36170 @property NSString *name;
36171 @property (readonly) id object;
36172 @property (retain, nonatomic, getter=getTheName) id name;
36173 @property int a, b, c;
36175 PS: This function is identical to
36176 c_parser_objc_at_property_declaration for C. Keep them in sync. */
36177 static void
36178 cp_parser_objc_at_property_declaration (cp_parser *parser)
36180 /* Parse the optional attribute list.
36182 A list of parsed, but not verified, attributes. */
36183 auto_delete_vec<property_attribute_info> prop_attr_list;
36184 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36186 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
36188 /* Parse the optional attribute list... */
36189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36191 /* Eat the '('. */
36192 matching_parens parens;
36193 location_t attr_start = cp_lexer_peek_token (parser->lexer)->location;
36194 parens.consume_open (parser);
36195 bool syntax_error = false;
36197 /* Allow empty @property attribute lists, but with a warning. */
36198 location_t attr_end = cp_lexer_peek_token (parser->lexer)->location;
36199 location_t attr_comb;
36200 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
36202 attr_comb = make_location (attr_end, attr_start, attr_end);
36203 warning_at (attr_comb, OPT_Wattributes,
36204 "empty property attribute list");
36206 else
36207 while (true)
36209 cp_token *token = cp_lexer_peek_token (parser->lexer);
36210 attr_start = token->location;
36211 attr_end = get_finish (token->location);
36212 attr_comb = make_location (attr_start, attr_start, attr_end);
36214 if (token->type == CPP_CLOSE_PAREN || token->type == CPP_COMMA)
36216 warning_at (attr_comb, OPT_Wattributes,
36217 "missing property attribute");
36218 if (token->type == CPP_CLOSE_PAREN)
36219 break;
36220 cp_lexer_consume_token (parser->lexer);
36221 continue;
36224 tree attr_name = NULL_TREE;
36225 if (identifier_p (token->u.value))
36226 attr_name = token->u.value;
36228 enum rid keyword;
36229 if (token->type == CPP_NAME)
36230 keyword = C_RID_CODE (token->u.value);
36231 else if (token->type == CPP_KEYWORD
36232 && token->keyword == RID_CLASS)
36233 /* Account for accepting the 'class' keyword in this context. */
36234 keyword = RID_CLASS;
36235 else
36236 keyword = RID_MAX; /* By definition, an unknown property. */
36237 cp_lexer_consume_token (parser->lexer);
36239 enum objc_property_attribute_kind prop_kind
36240 = objc_prop_attr_kind_for_rid (keyword);
36241 property_attribute_info *prop
36242 = new property_attribute_info (attr_name, attr_comb, prop_kind);
36243 prop_attr_list.safe_push (prop);
36245 tree meth_name;
36246 switch (prop->prop_kind)
36248 default: break;
36249 case OBJC_PROPERTY_ATTR_UNKNOWN:
36250 if (attr_name)
36251 error_at (attr_start, "unknown property attribute %qE",
36252 attr_name);
36253 else
36254 error_at (attr_start, "unknown property attribute");
36255 prop->parse_error = syntax_error = true;
36256 break;
36258 case OBJC_PROPERTY_ATTR_GETTER:
36259 case OBJC_PROPERTY_ATTR_SETTER:
36260 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
36262 attr_comb = make_location (attr_end, attr_start, attr_end);
36263 error_at (attr_comb, "expected %<=%> after Objective-C %qE",
36264 attr_name);
36265 prop->parse_error = syntax_error = true;
36266 break;
36269 token = cp_lexer_peek_token (parser->lexer);
36270 attr_end = token->location;
36271 cp_lexer_consume_token (parser->lexer); /* eat the = */
36273 if (!cp_parser_objc_selector_p
36274 (cp_lexer_peek_token (parser->lexer)->type))
36276 attr_comb = make_location (attr_end, attr_start, attr_end);
36277 error_at (attr_comb, "expected %qE selector name",
36278 attr_name);
36279 prop->parse_error = syntax_error = true;
36280 break;
36283 /* Get the end of the method name, and consume the name. */
36284 token = cp_lexer_peek_token (parser->lexer);
36285 attr_end = get_finish (token->location);
36286 /* Because method names may contain C++ keywords, we have a
36287 routine to fetch them (this also consumes the token). */
36288 meth_name = cp_parser_objc_selector (parser);
36290 if (prop->prop_kind == OBJC_PROPERTY_ATTR_SETTER)
36292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
36294 attr_comb = make_location (attr_end, attr_start,
36295 attr_end);
36296 error_at (attr_comb, "setter method names must"
36297 " terminate with %<:%>");
36298 prop->parse_error = syntax_error = true;
36300 else
36302 attr_end = get_finish (cp_lexer_peek_token
36303 (parser->lexer)->location);
36304 cp_lexer_consume_token (parser->lexer);
36306 attr_comb = make_location (attr_start, attr_start,
36307 attr_end);
36309 else
36310 attr_comb = make_location (attr_start, attr_start,
36311 attr_end);
36312 prop->ident = meth_name;
36313 /* Updated location including all that was successfully
36314 parsed. */
36315 prop->prop_loc = attr_comb;
36316 break;
36319 /* If we see a comma here, then keep going - even if we already
36320 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
36321 this makes a more useful output and avoid spurious warnings
36322 about missing attributes that are, in fact, specified after the
36323 one with the syntax error. */
36324 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36325 cp_lexer_consume_token (parser->lexer);
36326 else
36327 break;
36330 if (syntax_error || !parens.require_close (parser))
36331 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
36332 /*or_comma=*/false,
36333 /*consume_paren=*/true);
36336 /* 'properties' is the list of properties that we read. Usually a
36337 single one, but maybe more (eg, in "@property int a, b, c;" there
36338 are three).
36339 TODO: Update this parsing so that it accepts (erroneous) bitfields so
36340 that we can issue a meaningful and consistent (between C/C++) error
36341 message from objc_add_property_declaration (). */
36342 tree properties = cp_parser_objc_struct_declaration (parser);
36344 if (properties == error_mark_node)
36345 cp_parser_skip_to_end_of_statement (parser);
36346 else if (properties == NULL_TREE)
36347 cp_parser_error (parser, "expected identifier");
36348 else
36350 /* Comma-separated properties are chained together in reverse order;
36351 add them one by one. */
36352 properties = nreverse (properties);
36353 for (; properties; properties = TREE_CHAIN (properties))
36354 objc_add_property_declaration (loc, copy_node (properties),
36355 prop_attr_list);
36358 cp_parser_consume_semicolon_at_end_of_statement (parser);
36361 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
36363 objc-synthesize-declaration:
36364 @synthesize objc-synthesize-identifier-list ;
36366 objc-synthesize-identifier-list:
36367 objc-synthesize-identifier
36368 objc-synthesize-identifier-list, objc-synthesize-identifier
36370 objc-synthesize-identifier
36371 identifier
36372 identifier = identifier
36374 For example:
36375 @synthesize MyProperty;
36376 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
36378 PS: This function is identical to c_parser_objc_at_synthesize_declaration
36379 for C. Keep them in sync.
36381 static void
36382 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
36384 tree list = NULL_TREE;
36385 location_t loc;
36386 loc = cp_lexer_peek_token (parser->lexer)->location;
36388 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
36389 while (true)
36391 tree property, ivar;
36392 property = cp_parser_identifier (parser);
36393 if (property == error_mark_node)
36395 cp_parser_consume_semicolon_at_end_of_statement (parser);
36396 return;
36398 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
36400 cp_lexer_consume_token (parser->lexer);
36401 ivar = cp_parser_identifier (parser);
36402 if (ivar == error_mark_node)
36404 cp_parser_consume_semicolon_at_end_of_statement (parser);
36405 return;
36408 else
36409 ivar = NULL_TREE;
36410 list = chainon (list, build_tree_list (ivar, property));
36411 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36412 cp_lexer_consume_token (parser->lexer);
36413 else
36414 break;
36416 cp_parser_consume_semicolon_at_end_of_statement (parser);
36417 objc_add_synthesize_declaration (loc, list);
36420 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
36422 objc-dynamic-declaration:
36423 @dynamic identifier-list ;
36425 For example:
36426 @dynamic MyProperty;
36427 @dynamic MyProperty, AnotherProperty;
36429 PS: This function is identical to c_parser_objc_at_dynamic_declaration
36430 for C. Keep them in sync.
36432 static void
36433 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
36435 tree list = NULL_TREE;
36436 location_t loc;
36437 loc = cp_lexer_peek_token (parser->lexer)->location;
36439 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
36440 while (true)
36442 tree property;
36443 property = cp_parser_identifier (parser);
36444 if (property == error_mark_node)
36446 cp_parser_consume_semicolon_at_end_of_statement (parser);
36447 return;
36449 list = chainon (list, build_tree_list (NULL, property));
36450 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36451 cp_lexer_consume_token (parser->lexer);
36452 else
36453 break;
36455 cp_parser_consume_semicolon_at_end_of_statement (parser);
36456 objc_add_dynamic_declaration (loc, list);
36460 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
36462 /* Returns name of the next clause.
36463 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
36464 the token is not consumed. Otherwise appropriate pragma_omp_clause is
36465 returned and the token is consumed. */
36467 static pragma_omp_clause
36468 cp_parser_omp_clause_name (cp_parser *parser)
36470 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
36472 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
36473 result = PRAGMA_OACC_CLAUSE_AUTO;
36474 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
36475 result = PRAGMA_OMP_CLAUSE_IF;
36476 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
36477 result = PRAGMA_OMP_CLAUSE_DEFAULT;
36478 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
36479 result = PRAGMA_OACC_CLAUSE_DELETE;
36480 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
36481 result = PRAGMA_OMP_CLAUSE_PRIVATE;
36482 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
36483 result = PRAGMA_OMP_CLAUSE_FOR;
36484 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36486 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36487 const char *p = IDENTIFIER_POINTER (id);
36489 switch (p[0])
36491 case 'a':
36492 if (!strcmp ("affinity", p))
36493 result = PRAGMA_OMP_CLAUSE_AFFINITY;
36494 else if (!strcmp ("aligned", p))
36495 result = PRAGMA_OMP_CLAUSE_ALIGNED;
36496 else if (!strcmp ("allocate", p))
36497 result = PRAGMA_OMP_CLAUSE_ALLOCATE;
36498 else if (!strcmp ("async", p))
36499 result = PRAGMA_OACC_CLAUSE_ASYNC;
36500 else if (!strcmp ("attach", p))
36501 result = PRAGMA_OACC_CLAUSE_ATTACH;
36502 break;
36503 case 'b':
36504 if (!strcmp ("bind", p))
36505 result = PRAGMA_OMP_CLAUSE_BIND;
36506 break;
36507 case 'c':
36508 if (!strcmp ("collapse", p))
36509 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
36510 else if (!strcmp ("copy", p))
36511 result = PRAGMA_OACC_CLAUSE_COPY;
36512 else if (!strcmp ("copyin", p))
36513 result = PRAGMA_OMP_CLAUSE_COPYIN;
36514 else if (!strcmp ("copyout", p))
36515 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36516 else if (!strcmp ("copyprivate", p))
36517 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
36518 else if (!strcmp ("create", p))
36519 result = PRAGMA_OACC_CLAUSE_CREATE;
36520 break;
36521 case 'd':
36522 if (!strcmp ("defaultmap", p))
36523 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
36524 else if (!strcmp ("depend", p))
36525 result = PRAGMA_OMP_CLAUSE_DEPEND;
36526 else if (!strcmp ("detach", p))
36527 result = PRAGMA_OACC_CLAUSE_DETACH;
36528 else if (!strcmp ("device", p))
36529 result = PRAGMA_OMP_CLAUSE_DEVICE;
36530 else if (!strcmp ("deviceptr", p))
36531 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
36532 else if (!strcmp ("device_resident", p))
36533 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
36534 else if (!strcmp ("device_type", p))
36535 result = PRAGMA_OMP_CLAUSE_DEVICE_TYPE;
36536 else if (!strcmp ("dist_schedule", p))
36537 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
36538 else if (!strcmp ("doacross", p))
36539 result = PRAGMA_OMP_CLAUSE_DOACROSS;
36540 break;
36541 case 'e':
36542 if (!strcmp ("enter", p))
36543 result = PRAGMA_OMP_CLAUSE_ENTER;
36544 break;
36545 case 'f':
36546 if (!strcmp ("filter", p))
36547 result = PRAGMA_OMP_CLAUSE_FILTER;
36548 else if (!strcmp ("final", p))
36549 result = PRAGMA_OMP_CLAUSE_FINAL;
36550 else if (!strcmp ("finalize", p))
36551 result = PRAGMA_OACC_CLAUSE_FINALIZE;
36552 else if (!strcmp ("firstprivate", p))
36553 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
36554 else if (!strcmp ("from", p))
36555 result = PRAGMA_OMP_CLAUSE_FROM;
36556 break;
36557 case 'g':
36558 if (!strcmp ("gang", p))
36559 result = PRAGMA_OACC_CLAUSE_GANG;
36560 else if (!strcmp ("grainsize", p))
36561 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
36562 break;
36563 case 'h':
36564 if (!strcmp ("has_device_addr", p))
36565 result = PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR;
36566 else if (!strcmp ("hint", p))
36567 result = PRAGMA_OMP_CLAUSE_HINT;
36568 else if (!strcmp ("host", p))
36569 result = PRAGMA_OACC_CLAUSE_HOST;
36570 break;
36571 case 'i':
36572 if (!strcmp ("if_present", p))
36573 result = PRAGMA_OACC_CLAUSE_IF_PRESENT;
36574 else if (!strcmp ("in_reduction", p))
36575 result = PRAGMA_OMP_CLAUSE_IN_REDUCTION;
36576 else if (!strcmp ("inbranch", p))
36577 result = PRAGMA_OMP_CLAUSE_INBRANCH;
36578 else if (!strcmp ("independent", p))
36579 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
36580 else if (!strcmp ("is_device_ptr", p))
36581 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
36582 break;
36583 case 'l':
36584 if (!strcmp ("lastprivate", p))
36585 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
36586 else if (!strcmp ("linear", p))
36587 result = PRAGMA_OMP_CLAUSE_LINEAR;
36588 else if (!strcmp ("link", p))
36589 result = PRAGMA_OMP_CLAUSE_LINK;
36590 break;
36591 case 'm':
36592 if (!strcmp ("map", p))
36593 result = PRAGMA_OMP_CLAUSE_MAP;
36594 else if (!strcmp ("mergeable", p))
36595 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
36596 break;
36597 case 'n':
36598 if (!strcmp ("no_create", p))
36599 result = PRAGMA_OACC_CLAUSE_NO_CREATE;
36600 else if (!strcmp ("nogroup", p))
36601 result = PRAGMA_OMP_CLAUSE_NOGROUP;
36602 else if (!strcmp ("nohost", p))
36603 result = PRAGMA_OACC_CLAUSE_NOHOST;
36604 else if (!strcmp ("nontemporal", p))
36605 result = PRAGMA_OMP_CLAUSE_NONTEMPORAL;
36606 else if (!strcmp ("notinbranch", p))
36607 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
36608 else if (!strcmp ("nowait", p))
36609 result = PRAGMA_OMP_CLAUSE_NOWAIT;
36610 else if (!strcmp ("num_gangs", p))
36611 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
36612 else if (!strcmp ("num_tasks", p))
36613 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
36614 else if (!strcmp ("num_teams", p))
36615 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
36616 else if (!strcmp ("num_threads", p))
36617 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
36618 else if (!strcmp ("num_workers", p))
36619 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
36620 break;
36621 case 'o':
36622 if (!strcmp ("ordered", p))
36623 result = PRAGMA_OMP_CLAUSE_ORDERED;
36624 else if (!strcmp ("order", p))
36625 result = PRAGMA_OMP_CLAUSE_ORDER;
36626 break;
36627 case 'p':
36628 if (!strcmp ("parallel", p))
36629 result = PRAGMA_OMP_CLAUSE_PARALLEL;
36630 else if (!strcmp ("present", p))
36631 result = PRAGMA_OACC_CLAUSE_PRESENT;
36632 else if (!strcmp ("present_or_copy", p)
36633 || !strcmp ("pcopy", p))
36634 result = PRAGMA_OACC_CLAUSE_COPY;
36635 else if (!strcmp ("present_or_copyin", p)
36636 || !strcmp ("pcopyin", p))
36637 result = PRAGMA_OACC_CLAUSE_COPYIN;
36638 else if (!strcmp ("present_or_copyout", p)
36639 || !strcmp ("pcopyout", p))
36640 result = PRAGMA_OACC_CLAUSE_COPYOUT;
36641 else if (!strcmp ("present_or_create", p)
36642 || !strcmp ("pcreate", p))
36643 result = PRAGMA_OACC_CLAUSE_CREATE;
36644 else if (!strcmp ("priority", p))
36645 result = PRAGMA_OMP_CLAUSE_PRIORITY;
36646 else if (!strcmp ("proc_bind", p))
36647 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
36648 break;
36649 case 'r':
36650 if (!strcmp ("reduction", p))
36651 result = PRAGMA_OMP_CLAUSE_REDUCTION;
36652 break;
36653 case 's':
36654 if (!strcmp ("safelen", p))
36655 result = PRAGMA_OMP_CLAUSE_SAFELEN;
36656 else if (!strcmp ("schedule", p))
36657 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
36658 else if (!strcmp ("sections", p))
36659 result = PRAGMA_OMP_CLAUSE_SECTIONS;
36660 else if (!strcmp ("self", p)) /* "self" is a synonym for "host". */
36661 result = PRAGMA_OACC_CLAUSE_HOST;
36662 else if (!strcmp ("seq", p))
36663 result = PRAGMA_OACC_CLAUSE_SEQ;
36664 else if (!strcmp ("shared", p))
36665 result = PRAGMA_OMP_CLAUSE_SHARED;
36666 else if (!strcmp ("simd", p))
36667 result = PRAGMA_OMP_CLAUSE_SIMD;
36668 else if (!strcmp ("simdlen", p))
36669 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
36670 break;
36671 case 't':
36672 if (!strcmp ("task_reduction", p))
36673 result = PRAGMA_OMP_CLAUSE_TASK_REDUCTION;
36674 else if (!strcmp ("taskgroup", p))
36675 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
36676 else if (!strcmp ("thread_limit", p))
36677 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
36678 else if (!strcmp ("threads", p))
36679 result = PRAGMA_OMP_CLAUSE_THREADS;
36680 else if (!strcmp ("tile", p))
36681 result = PRAGMA_OACC_CLAUSE_TILE;
36682 else if (!strcmp ("to", p))
36683 result = PRAGMA_OMP_CLAUSE_TO;
36684 break;
36685 case 'u':
36686 if (!strcmp ("uniform", p))
36687 result = PRAGMA_OMP_CLAUSE_UNIFORM;
36688 else if (!strcmp ("untied", p))
36689 result = PRAGMA_OMP_CLAUSE_UNTIED;
36690 else if (!strcmp ("use_device", p))
36691 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
36692 else if (!strcmp ("use_device_addr", p))
36693 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR;
36694 else if (!strcmp ("use_device_ptr", p))
36695 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
36696 break;
36697 case 'v':
36698 if (!strcmp ("vector", p))
36699 result = PRAGMA_OACC_CLAUSE_VECTOR;
36700 else if (!strcmp ("vector_length", p))
36701 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
36702 break;
36703 case 'w':
36704 if (!strcmp ("wait", p))
36705 result = PRAGMA_OACC_CLAUSE_WAIT;
36706 else if (!strcmp ("worker", p))
36707 result = PRAGMA_OACC_CLAUSE_WORKER;
36708 break;
36712 if (result != PRAGMA_OMP_CLAUSE_NONE)
36713 cp_lexer_consume_token (parser->lexer);
36715 return result;
36718 /* Validate that a clause of the given type does not already exist. */
36720 static void
36721 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
36722 const char *name, location_t location)
36724 if (omp_find_clause (clauses, code))
36725 error_at (location, "too many %qs clauses", name);
36728 /* OpenMP 2.5:
36729 variable-list:
36730 identifier
36731 variable-list , identifier
36733 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
36734 colon). An opening parenthesis will have been consumed by the caller.
36736 If KIND is nonzero, create the appropriate node and install the decl
36737 in OMP_CLAUSE_DECL and add the node to the head of the list.
36739 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
36740 return the list created.
36742 COLON can be NULL if only closing parenthesis should end the list,
36743 or pointer to bool which will receive false if the list is terminated
36744 by closing parenthesis or true if the list is terminated by colon.
36746 The optional ALLOW_DEREF argument is true if list items can use the deref
36747 (->) operator. */
36749 struct omp_dim
36751 tree low_bound, length;
36752 location_t loc;
36753 bool no_colon;
36754 omp_dim (tree lb, tree len, location_t lo, bool nc)
36755 : low_bound (lb), length (len), loc (lo), no_colon (nc) {}
36758 static tree
36759 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
36760 tree list, bool *colon,
36761 bool allow_deref = false)
36763 auto_vec<omp_dim> dims;
36764 bool array_section_p;
36765 cp_token *token;
36766 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36767 if (colon)
36769 parser->colon_corrects_to_scope_p = false;
36770 *colon = false;
36772 while (1)
36774 tree name, decl;
36776 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36777 cp_parser_parse_tentatively (parser);
36778 token = cp_lexer_peek_token (parser->lexer);
36779 if (kind != 0
36780 && cp_parser_is_keyword (token, RID_THIS))
36782 decl = finish_this_expr ();
36783 if (TREE_CODE (decl) == NON_LVALUE_EXPR
36784 || CONVERT_EXPR_P (decl))
36785 decl = TREE_OPERAND (decl, 0);
36786 cp_lexer_consume_token (parser->lexer);
36788 else if (cp_parser_is_keyword (token, RID_FUNCTION_NAME)
36789 || cp_parser_is_keyword (token, RID_PRETTY_FUNCTION_NAME)
36790 || cp_parser_is_keyword (token, RID_C99_FUNCTION_NAME))
36792 cp_id_kind idk;
36793 decl = cp_parser_primary_expression (parser, false, false, false,
36794 &idk);
36796 else if (kind == OMP_CLAUSE_DEPEND
36797 && cp_parser_is_keyword (token, RID_OMP_ALL_MEMORY)
36798 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
36799 || cp_lexer_nth_token_is (parser->lexer, 2,
36800 CPP_CLOSE_PAREN)))
36802 decl = ridpointers[RID_OMP_ALL_MEMORY];
36803 cp_lexer_consume_token (parser->lexer);
36805 else
36807 name = cp_parser_id_expression (parser, /*template_p=*/false,
36808 /*check_dependency_p=*/true,
36809 /*template_p=*/NULL,
36810 /*declarator_p=*/false,
36811 /*optional_p=*/false);
36812 if (name == error_mark_node)
36814 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36815 && cp_parser_simulate_error (parser))
36816 goto depend_lvalue;
36817 goto skip_comma;
36820 if (identifier_p (name))
36821 decl = cp_parser_lookup_name_simple (parser, name, token->location);
36822 else
36823 decl = name;
36824 if (decl == error_mark_node)
36826 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36827 && cp_parser_simulate_error (parser))
36828 goto depend_lvalue;
36829 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
36830 token->location);
36833 if (outer_automatic_var_p (decl))
36834 decl = process_outer_var_ref (decl, tf_warning_or_error);
36835 if (decl == error_mark_node)
36837 else if (kind != 0)
36839 switch (kind)
36841 case OMP_CLAUSE__CACHE_:
36842 /* The OpenACC cache directive explicitly only allows "array
36843 elements or subarrays". */
36844 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
36846 error_at (token->location, "expected %<[%>");
36847 decl = error_mark_node;
36848 break;
36850 /* FALLTHROUGH. */
36851 case OMP_CLAUSE_MAP:
36852 case OMP_CLAUSE_FROM:
36853 case OMP_CLAUSE_TO:
36854 start_component_ref:
36855 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36856 || (allow_deref
36857 && cp_lexer_next_token_is (parser->lexer, CPP_DEREF)))
36859 cpp_ttype ttype
36860 = cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36861 ? CPP_DOT : CPP_DEREF;
36862 location_t loc
36863 = cp_lexer_peek_token (parser->lexer)->location;
36864 cp_id_kind idk = CP_ID_KIND_NONE;
36865 cp_lexer_consume_token (parser->lexer);
36866 decl = convert_from_reference (decl);
36867 decl = (cp_parser_postfix_dot_deref_expression
36868 (parser, ttype, cp_expr (decl, token->location),
36869 false, &idk, loc));
36871 /* FALLTHROUGH. */
36872 case OMP_CLAUSE_AFFINITY:
36873 case OMP_CLAUSE_DEPEND:
36874 case OMP_CLAUSE_REDUCTION:
36875 case OMP_CLAUSE_IN_REDUCTION:
36876 case OMP_CLAUSE_TASK_REDUCTION:
36877 case OMP_CLAUSE_HAS_DEVICE_ADDR:
36878 array_section_p = false;
36879 dims.truncate (0);
36880 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
36882 location_t loc = UNKNOWN_LOCATION;
36883 tree low_bound = NULL_TREE, length = NULL_TREE;
36884 bool no_colon = false;
36886 parser->colon_corrects_to_scope_p = false;
36887 cp_lexer_consume_token (parser->lexer);
36888 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36890 loc = cp_lexer_peek_token (parser->lexer)->location;
36891 low_bound = cp_parser_expression (parser);
36892 /* Later handling is not prepared to see through these. */
36893 gcc_checking_assert (!location_wrapper_p (low_bound));
36895 if (!colon)
36896 parser->colon_corrects_to_scope_p
36897 = saved_colon_corrects_to_scope_p;
36898 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
36900 length = integer_one_node;
36901 no_colon = true;
36903 else
36905 /* Look for `:'. */
36906 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36908 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36909 && cp_parser_simulate_error (parser))
36910 goto depend_lvalue;
36911 goto skip_comma;
36913 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36914 cp_parser_commit_to_tentative_parse (parser);
36915 else
36916 array_section_p = true;
36917 if (!cp_lexer_next_token_is (parser->lexer,
36918 CPP_CLOSE_SQUARE))
36920 length = cp_parser_expression (parser);
36921 /* Later handling is not prepared to see through these. */
36922 gcc_checking_assert (!location_wrapper_p (length));
36925 /* Look for the closing `]'. */
36926 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
36927 RT_CLOSE_SQUARE))
36929 if ((kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36930 && cp_parser_simulate_error (parser))
36931 goto depend_lvalue;
36932 goto skip_comma;
36935 dims.safe_push (omp_dim (low_bound, length, loc, no_colon));
36938 if ((kind == OMP_CLAUSE_MAP
36939 || kind == OMP_CLAUSE_FROM
36940 || kind == OMP_CLAUSE_TO)
36941 && !array_section_p
36942 && (cp_lexer_next_token_is (parser->lexer, CPP_DOT)
36943 || (allow_deref
36944 && cp_lexer_next_token_is (parser->lexer,
36945 CPP_DEREF))))
36947 for (unsigned i = 0; i < dims.length (); i++)
36949 gcc_assert (dims[i].length == integer_one_node);
36950 decl = build_array_ref (dims[i].loc,
36951 decl, dims[i].low_bound);
36953 goto start_component_ref;
36955 else
36956 for (unsigned i = 0; i < dims.length (); i++)
36957 decl = tree_cons (dims[i].low_bound, dims[i].length, decl);
36959 break;
36960 default:
36961 break;
36964 if (kind == OMP_CLAUSE_DEPEND || kind == OMP_CLAUSE_AFFINITY)
36966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
36967 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
36968 && cp_parser_simulate_error (parser))
36970 depend_lvalue:
36971 cp_parser_abort_tentative_parse (parser);
36972 decl = cp_parser_assignment_expression (parser, NULL,
36973 false, false);
36975 else
36976 cp_parser_parse_definitely (parser);
36979 tree u = build_omp_clause (token->location, kind);
36980 OMP_CLAUSE_DECL (u) = decl;
36981 OMP_CLAUSE_CHAIN (u) = list;
36982 list = u;
36984 else
36985 list = tree_cons (decl, NULL_TREE, list);
36987 get_comma:
36988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
36989 break;
36990 cp_lexer_consume_token (parser->lexer);
36993 if (colon)
36994 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36996 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
36998 *colon = true;
36999 cp_parser_require (parser, CPP_COLON, RT_COLON);
37000 return list;
37003 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37005 int ending;
37007 /* Try to resync to an unnested comma. Copied from
37008 cp_parser_parenthesized_expression_list. */
37009 skip_comma:
37010 if (colon)
37011 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37012 ending = cp_parser_skip_to_closing_parenthesis (parser,
37013 /*recovering=*/true,
37014 /*or_comma=*/true,
37015 /*consume_paren=*/true);
37016 if (ending < 0)
37017 goto get_comma;
37020 return list;
37023 /* Similarly, but expect leading and trailing parenthesis. This is a very
37024 common case for omp clauses. */
37026 static tree
37027 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list,
37028 bool allow_deref = false)
37030 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37031 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL,
37032 allow_deref);
37033 return list;
37036 /* OpenACC 2.0:
37037 copy ( variable-list )
37038 copyin ( variable-list )
37039 copyout ( variable-list )
37040 create ( variable-list )
37041 delete ( variable-list )
37042 present ( variable-list )
37044 OpenACC 2.6:
37045 no_create ( variable-list )
37046 attach ( variable-list )
37047 detach ( variable-list ) */
37049 static tree
37050 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
37051 tree list)
37053 enum gomp_map_kind kind;
37054 switch (c_kind)
37056 case PRAGMA_OACC_CLAUSE_ATTACH:
37057 kind = GOMP_MAP_ATTACH;
37058 break;
37059 case PRAGMA_OACC_CLAUSE_COPY:
37060 kind = GOMP_MAP_TOFROM;
37061 break;
37062 case PRAGMA_OACC_CLAUSE_COPYIN:
37063 kind = GOMP_MAP_TO;
37064 break;
37065 case PRAGMA_OACC_CLAUSE_COPYOUT:
37066 kind = GOMP_MAP_FROM;
37067 break;
37068 case PRAGMA_OACC_CLAUSE_CREATE:
37069 kind = GOMP_MAP_ALLOC;
37070 break;
37071 case PRAGMA_OACC_CLAUSE_DELETE:
37072 kind = GOMP_MAP_RELEASE;
37073 break;
37074 case PRAGMA_OACC_CLAUSE_DETACH:
37075 kind = GOMP_MAP_DETACH;
37076 break;
37077 case PRAGMA_OACC_CLAUSE_DEVICE:
37078 kind = GOMP_MAP_FORCE_TO;
37079 break;
37080 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
37081 kind = GOMP_MAP_DEVICE_RESIDENT;
37082 break;
37083 case PRAGMA_OACC_CLAUSE_HOST:
37084 kind = GOMP_MAP_FORCE_FROM;
37085 break;
37086 case PRAGMA_OACC_CLAUSE_LINK:
37087 kind = GOMP_MAP_LINK;
37088 break;
37089 case PRAGMA_OACC_CLAUSE_NO_CREATE:
37090 kind = GOMP_MAP_IF_PRESENT;
37091 break;
37092 case PRAGMA_OACC_CLAUSE_PRESENT:
37093 kind = GOMP_MAP_FORCE_PRESENT;
37094 break;
37095 default:
37096 gcc_unreachable ();
37098 tree nl, c;
37099 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list, true);
37101 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
37102 OMP_CLAUSE_SET_MAP_KIND (c, kind);
37104 return nl;
37107 /* OpenACC 2.0:
37108 deviceptr ( variable-list ) */
37110 static tree
37111 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
37113 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37114 tree vars, t;
37116 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
37117 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
37118 variable-list must only allow for pointer variables. */
37119 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
37120 for (t = vars; t; t = TREE_CHAIN (t))
37122 tree v = TREE_PURPOSE (t);
37123 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
37124 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
37125 OMP_CLAUSE_DECL (u) = v;
37126 OMP_CLAUSE_CHAIN (u) = list;
37127 list = u;
37130 return list;
37133 /* OpenACC 2.5:
37134 auto
37135 finalize
37136 independent
37137 nohost
37138 seq */
37140 static tree
37141 cp_parser_oacc_simple_clause (location_t loc, enum omp_clause_code code,
37142 tree list)
37144 check_no_duplicate_clause (list, code, omp_clause_code_name[code], loc);
37146 tree c = build_omp_clause (loc, code);
37147 OMP_CLAUSE_CHAIN (c) = list;
37149 return c;
37152 /* OpenACC:
37153 num_gangs ( expression )
37154 num_workers ( expression )
37155 vector_length ( expression ) */
37157 static tree
37158 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
37159 const char *str, tree list)
37161 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37163 matching_parens parens;
37164 if (!parens.require_open (parser))
37165 return list;
37167 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
37169 if (t == error_mark_node
37170 || !parens.require_close (parser))
37172 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37173 /*or_comma=*/false,
37174 /*consume_paren=*/true);
37175 return list;
37178 check_no_duplicate_clause (list, code, str, loc);
37180 tree c = build_omp_clause (loc, code);
37181 OMP_CLAUSE_OPERAND (c, 0) = t;
37182 OMP_CLAUSE_CHAIN (c) = list;
37183 return c;
37186 /* OpenACC:
37188 gang [( gang-arg-list )]
37189 worker [( [num:] int-expr )]
37190 vector [( [length:] int-expr )]
37192 where gang-arg is one of:
37194 [num:] int-expr
37195 static: size-expr
37197 and size-expr may be:
37200 int-expr
37203 static tree
37204 cp_parser_oacc_shape_clause (cp_parser *parser, location_t loc,
37205 omp_clause_code kind,
37206 const char *str, tree list)
37208 const char *id = "num";
37209 cp_lexer *lexer = parser->lexer;
37210 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
37212 if (kind == OMP_CLAUSE_VECTOR)
37213 id = "length";
37215 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
37217 matching_parens parens;
37218 parens.consume_open (parser);
37222 cp_token *next = cp_lexer_peek_token (lexer);
37223 int idx = 0;
37225 /* Gang static argument. */
37226 if (kind == OMP_CLAUSE_GANG
37227 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
37229 cp_lexer_consume_token (lexer);
37231 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37232 goto cleanup_error;
37234 idx = 1;
37235 if (ops[idx] != NULL)
37237 cp_parser_error (parser, "too many %<static%> arguments");
37238 goto cleanup_error;
37241 /* Check for the '*' argument. */
37242 if (cp_lexer_next_token_is (lexer, CPP_MULT)
37243 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37244 || cp_lexer_nth_token_is (parser->lexer, 2,
37245 CPP_CLOSE_PAREN)))
37247 cp_lexer_consume_token (lexer);
37248 ops[idx] = integer_minus_one_node;
37250 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
37252 cp_lexer_consume_token (lexer);
37253 continue;
37255 else break;
37258 /* Worker num: argument and vector length: arguments. */
37259 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
37260 && id_equal (next->u.value, id)
37261 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
37263 cp_lexer_consume_token (lexer); /* id */
37264 cp_lexer_consume_token (lexer); /* ':' */
37267 /* Now collect the actual argument. */
37268 if (ops[idx] != NULL_TREE)
37270 cp_parser_error (parser, "unexpected argument");
37271 goto cleanup_error;
37274 tree expr = cp_parser_assignment_expression (parser, NULL, false,
37275 false);
37276 if (expr == error_mark_node)
37277 goto cleanup_error;
37279 mark_exp_read (expr);
37280 ops[idx] = expr;
37282 if (kind == OMP_CLAUSE_GANG
37283 && cp_lexer_next_token_is (lexer, CPP_COMMA))
37285 cp_lexer_consume_token (lexer);
37286 continue;
37288 break;
37290 while (1);
37292 if (!parens.require_close (parser))
37293 goto cleanup_error;
37296 check_no_duplicate_clause (list, kind, str, loc);
37298 c = build_omp_clause (loc, kind);
37300 if (ops[1])
37301 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
37303 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
37304 OMP_CLAUSE_CHAIN (c) = list;
37306 return c;
37308 cleanup_error:
37309 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37310 return list;
37313 /* OpenACC 2.0:
37314 tile ( size-expr-list ) */
37316 static tree
37317 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
37319 tree c, expr = error_mark_node;
37320 tree tile = NULL_TREE;
37322 /* Collapse and tile are mutually exclusive. (The spec doesn't say
37323 so, but the spec authors never considered such a case and have
37324 differing opinions on what it might mean, including 'not
37325 allowed'.) */
37326 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
37327 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
37328 clause_loc);
37330 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37331 return list;
37335 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
37336 return list;
37338 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
37339 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
37340 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
37342 cp_lexer_consume_token (parser->lexer);
37343 expr = integer_zero_node;
37345 else
37346 expr = cp_parser_constant_expression (parser);
37348 tile = tree_cons (NULL_TREE, expr, tile);
37350 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
37352 /* Consume the trailing ')'. */
37353 cp_lexer_consume_token (parser->lexer);
37355 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
37356 tile = nreverse (tile);
37357 OMP_CLAUSE_TILE_LIST (c) = tile;
37358 OMP_CLAUSE_CHAIN (c) = list;
37359 return c;
37362 /* OpenACC 2.0
37363 Parse wait clause or directive parameters. */
37365 static tree
37366 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
37368 vec<tree, va_gc> *args;
37369 tree t, args_tree;
37371 args = cp_parser_parenthesized_expression_list (parser, non_attr,
37372 /*cast_p=*/false,
37373 /*allow_expansion_p=*/true,
37374 /*non_constant_p=*/NULL);
37376 if (args == NULL || args->length () == 0)
37378 if (args != NULL)
37380 cp_parser_error (parser, "expected integer expression list");
37381 release_tree_vector (args);
37383 return list;
37386 args_tree = build_tree_list_vec (args);
37388 release_tree_vector (args);
37390 for (t = args_tree; t; t = TREE_CHAIN (t))
37392 tree targ = TREE_VALUE (t);
37394 if (targ != error_mark_node)
37396 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
37397 error ("%<wait%> expression must be integral");
37398 else
37400 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
37402 targ = mark_rvalue_use (targ);
37403 OMP_CLAUSE_DECL (c) = targ;
37404 OMP_CLAUSE_CHAIN (c) = list;
37405 list = c;
37410 return list;
37413 /* OpenACC:
37414 wait [( int-expr-list )] */
37416 static tree
37417 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
37419 location_t location = cp_lexer_peek_token (parser->lexer)->location;
37421 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37422 list = cp_parser_oacc_wait_list (parser, location, list);
37423 else
37425 tree c = build_omp_clause (location, OMP_CLAUSE_WAIT);
37427 OMP_CLAUSE_DECL (c) = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
37428 OMP_CLAUSE_CHAIN (c) = list;
37429 list = c;
37432 return list;
37435 /* OpenMP 3.0:
37436 collapse ( constant-expression ) */
37438 static tree
37439 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
37441 tree c, num;
37442 location_t loc;
37443 HOST_WIDE_INT n;
37445 loc = cp_lexer_peek_token (parser->lexer)->location;
37446 matching_parens parens;
37447 if (!parens.require_open (parser))
37448 return list;
37450 num = cp_parser_constant_expression (parser);
37452 if (!parens.require_close (parser))
37453 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37454 /*or_comma=*/false,
37455 /*consume_paren=*/true);
37457 if (num == error_mark_node)
37458 return list;
37459 num = fold_non_dependent_expr (num);
37460 if (!tree_fits_shwi_p (num)
37461 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
37462 || (n = tree_to_shwi (num)) <= 0
37463 || (int) n != n)
37465 error_at (loc, "collapse argument needs positive constant integer expression");
37466 return list;
37469 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
37470 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
37471 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
37472 OMP_CLAUSE_CHAIN (c) = list;
37473 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
37475 return c;
37478 /* OpenMP 2.5:
37479 default ( none | shared )
37481 OpenMP 5.1:
37482 default ( private | firstprivate )
37484 OpenACC:
37485 default ( none | present ) */
37487 static tree
37488 cp_parser_omp_clause_default (cp_parser *parser, tree list,
37489 location_t location, bool is_oacc)
37491 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
37492 tree c;
37494 matching_parens parens;
37495 if (!parens.require_open (parser))
37496 return list;
37497 if (!is_oacc && cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
37499 kind = OMP_CLAUSE_DEFAULT_PRIVATE;
37500 cp_lexer_consume_token (parser->lexer);
37502 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37504 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37505 const char *p = IDENTIFIER_POINTER (id);
37507 switch (p[0])
37509 case 'n':
37510 if (strcmp ("none", p) != 0)
37511 goto invalid_kind;
37512 kind = OMP_CLAUSE_DEFAULT_NONE;
37513 break;
37515 case 'p':
37516 if (strcmp ("present", p) != 0 || !is_oacc)
37517 goto invalid_kind;
37518 kind = OMP_CLAUSE_DEFAULT_PRESENT;
37519 break;
37521 case 'f':
37522 if (strcmp ("firstprivate", p) != 0 || is_oacc)
37523 goto invalid_kind;
37524 kind = OMP_CLAUSE_DEFAULT_FIRSTPRIVATE;
37525 break;
37527 case 's':
37528 if (strcmp ("shared", p) != 0 || is_oacc)
37529 goto invalid_kind;
37530 kind = OMP_CLAUSE_DEFAULT_SHARED;
37531 break;
37533 default:
37534 goto invalid_kind;
37537 cp_lexer_consume_token (parser->lexer);
37539 else
37541 invalid_kind:
37542 if (is_oacc)
37543 cp_parser_error (parser, "expected %<none%> or %<present%>");
37544 else
37545 cp_parser_error (parser, "expected %<none%>, %<shared%>, "
37546 "%<private%> or %<firstprivate%>");
37549 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
37550 || !parens.require_close (parser))
37551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37552 /*or_comma=*/false,
37553 /*consume_paren=*/true);
37555 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
37556 return list;
37558 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
37559 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
37560 OMP_CLAUSE_CHAIN (c) = list;
37561 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
37563 return c;
37566 /* OpenMP 3.1:
37567 final ( expression ) */
37569 static tree
37570 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
37572 tree t, c;
37574 matching_parens parens;
37575 if (!parens.require_open (parser))
37576 return list;
37578 t = cp_parser_assignment_expression (parser);
37580 if (t == error_mark_node
37581 || !parens.require_close (parser))
37582 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37583 /*or_comma=*/false,
37584 /*consume_paren=*/true);
37586 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
37588 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
37589 OMP_CLAUSE_FINAL_EXPR (c) = t;
37590 OMP_CLAUSE_CHAIN (c) = list;
37592 return c;
37595 /* OpenMP 2.5:
37596 if ( expression )
37598 OpenMP 4.5:
37599 if ( directive-name-modifier : expression )
37601 directive-name-modifier:
37602 parallel | task | taskloop | target data | target | target update
37603 | target enter data | target exit data
37605 OpenMP 5.0:
37606 directive-name-modifier:
37607 ... | simd | cancel */
37609 static tree
37610 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
37611 bool is_omp)
37613 tree t, c;
37614 enum tree_code if_modifier = ERROR_MARK;
37616 matching_parens parens;
37617 if (!parens.require_open (parser))
37618 return list;
37620 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37622 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37623 const char *p = IDENTIFIER_POINTER (id);
37624 int n = 2;
37626 if (strcmp ("cancel", p) == 0)
37627 if_modifier = VOID_CST;
37628 else if (strcmp ("parallel", p) == 0)
37629 if_modifier = OMP_PARALLEL;
37630 else if (strcmp ("simd", p) == 0)
37631 if_modifier = OMP_SIMD;
37632 else if (strcmp ("task", p) == 0)
37633 if_modifier = OMP_TASK;
37634 else if (strcmp ("taskloop", p) == 0)
37635 if_modifier = OMP_TASKLOOP;
37636 else if (strcmp ("target", p) == 0)
37638 if_modifier = OMP_TARGET;
37639 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
37641 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
37642 p = IDENTIFIER_POINTER (id);
37643 if (strcmp ("data", p) == 0)
37644 if_modifier = OMP_TARGET_DATA;
37645 else if (strcmp ("update", p) == 0)
37646 if_modifier = OMP_TARGET_UPDATE;
37647 else if (strcmp ("enter", p) == 0)
37648 if_modifier = OMP_TARGET_ENTER_DATA;
37649 else if (strcmp ("exit", p) == 0)
37650 if_modifier = OMP_TARGET_EXIT_DATA;
37651 if (if_modifier != OMP_TARGET)
37652 n = 3;
37653 else
37655 location_t loc
37656 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
37657 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
37658 "or %<exit%>");
37659 if_modifier = ERROR_MARK;
37661 if (if_modifier == OMP_TARGET_ENTER_DATA
37662 || if_modifier == OMP_TARGET_EXIT_DATA)
37664 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
37666 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
37667 p = IDENTIFIER_POINTER (id);
37668 if (strcmp ("data", p) == 0)
37669 n = 4;
37671 if (n != 4)
37673 location_t loc
37674 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
37675 error_at (loc, "expected %<data%>");
37676 if_modifier = ERROR_MARK;
37681 if (if_modifier != ERROR_MARK)
37683 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
37685 while (n-- > 0)
37686 cp_lexer_consume_token (parser->lexer);
37688 else
37690 if (n > 2)
37692 location_t loc
37693 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
37694 error_at (loc, "expected %<:%>");
37696 if_modifier = ERROR_MARK;
37701 t = cp_parser_assignment_expression (parser);
37703 if (t == error_mark_node
37704 || !parens.require_close (parser))
37705 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37706 /*or_comma=*/false,
37707 /*consume_paren=*/true);
37709 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
37710 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
37712 if (if_modifier != ERROR_MARK
37713 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37715 const char *p = NULL;
37716 switch (if_modifier)
37718 case VOID_CST: p = "cancel"; break;
37719 case OMP_PARALLEL: p = "parallel"; break;
37720 case OMP_SIMD: p = "simd"; break;
37721 case OMP_TASK: p = "task"; break;
37722 case OMP_TASKLOOP: p = "taskloop"; break;
37723 case OMP_TARGET_DATA: p = "target data"; break;
37724 case OMP_TARGET: p = "target"; break;
37725 case OMP_TARGET_UPDATE: p = "target update"; break;
37726 case OMP_TARGET_ENTER_DATA: p = "target enter data"; break;
37727 case OMP_TARGET_EXIT_DATA: p = "target exit data"; break;
37728 default: gcc_unreachable ();
37730 error_at (location, "too many %<if%> clauses with %qs modifier",
37732 return list;
37734 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
37736 if (!is_omp)
37737 error_at (location, "too many %<if%> clauses");
37738 else
37739 error_at (location, "too many %<if%> clauses without modifier");
37740 return list;
37742 else if (if_modifier == ERROR_MARK
37743 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
37745 error_at (location, "if any %<if%> clause has modifier, then all "
37746 "%<if%> clauses have to use modifier");
37747 return list;
37751 c = build_omp_clause (location, OMP_CLAUSE_IF);
37752 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
37753 OMP_CLAUSE_IF_EXPR (c) = t;
37754 OMP_CLAUSE_CHAIN (c) = list;
37756 return c;
37759 /* OpenMP 3.1:
37760 mergeable */
37762 static tree
37763 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
37764 tree list, location_t location)
37766 tree c;
37768 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
37769 location);
37771 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
37772 OMP_CLAUSE_CHAIN (c) = list;
37773 return c;
37776 /* OpenMP 2.5:
37777 nowait */
37779 static tree
37780 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
37781 tree list, location_t location)
37783 tree c;
37785 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
37787 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
37788 OMP_CLAUSE_CHAIN (c) = list;
37789 return c;
37792 /* OpenMP 2.5:
37793 num_threads ( expression ) */
37795 static tree
37796 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
37797 location_t location)
37799 tree t, c;
37801 matching_parens parens;
37802 if (!parens.require_open (parser))
37803 return list;
37805 t = cp_parser_assignment_expression (parser);
37807 if (t == error_mark_node
37808 || !parens.require_close (parser))
37809 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37810 /*or_comma=*/false,
37811 /*consume_paren=*/true);
37813 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
37814 "num_threads", location);
37816 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
37817 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
37818 OMP_CLAUSE_CHAIN (c) = list;
37820 return c;
37823 /* OpenMP 4.5:
37824 num_tasks ( expression )
37826 OpenMP 5.1:
37827 num_tasks ( strict : expression ) */
37829 static tree
37830 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
37831 location_t location)
37833 tree t, c;
37835 matching_parens parens;
37836 if (!parens.require_open (parser))
37837 return list;
37839 bool strict = false;
37840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37841 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37843 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37844 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37846 strict = true;
37847 cp_lexer_consume_token (parser->lexer);
37848 cp_lexer_consume_token (parser->lexer);
37852 t = cp_parser_assignment_expression (parser);
37854 if (t == error_mark_node
37855 || !parens.require_close (parser))
37856 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37857 /*or_comma=*/false,
37858 /*consume_paren=*/true);
37860 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
37861 "num_tasks", location);
37863 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
37864 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
37865 OMP_CLAUSE_NUM_TASKS_STRICT (c) = strict;
37866 OMP_CLAUSE_CHAIN (c) = list;
37868 return c;
37871 /* OpenMP 4.5:
37872 grainsize ( expression )
37874 OpenMP 5.1:
37875 grainsize ( strict : expression ) */
37877 static tree
37878 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
37879 location_t location)
37881 tree t, c;
37883 matching_parens parens;
37884 if (!parens.require_open (parser))
37885 return list;
37887 bool strict = false;
37888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
37889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
37891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37892 if (!strcmp (IDENTIFIER_POINTER (id), "strict"))
37894 strict = true;
37895 cp_lexer_consume_token (parser->lexer);
37896 cp_lexer_consume_token (parser->lexer);
37900 t = cp_parser_assignment_expression (parser);
37902 if (t == error_mark_node
37903 || !parens.require_close (parser))
37904 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37905 /*or_comma=*/false,
37906 /*consume_paren=*/true);
37908 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
37909 "grainsize", location);
37911 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
37912 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
37913 OMP_CLAUSE_GRAINSIZE_STRICT (c) = strict;
37914 OMP_CLAUSE_CHAIN (c) = list;
37916 return c;
37919 /* OpenMP 4.5:
37920 priority ( expression ) */
37922 static tree
37923 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
37924 location_t location)
37926 tree t, c;
37928 matching_parens parens;
37929 if (!parens.require_open (parser))
37930 return list;
37932 t = cp_parser_assignment_expression (parser);
37934 if (t == error_mark_node
37935 || !parens.require_close (parser))
37936 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37937 /*or_comma=*/false,
37938 /*consume_paren=*/true);
37940 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
37941 "priority", location);
37943 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
37944 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
37945 OMP_CLAUSE_CHAIN (c) = list;
37947 return c;
37950 /* OpenMP 4.5:
37951 hint ( expression ) */
37953 static tree
37954 cp_parser_omp_clause_hint (cp_parser *parser, tree list, location_t location)
37956 tree t, c;
37958 matching_parens parens;
37959 if (!parens.require_open (parser))
37960 return list;
37962 t = cp_parser_assignment_expression (parser);
37964 if (t != error_mark_node)
37966 t = fold_non_dependent_expr (t);
37967 if (!value_dependent_expression_p (t)
37968 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
37969 || !tree_fits_shwi_p (t)
37970 || tree_int_cst_sgn (t) == -1))
37971 error_at (location, "expected constant integer expression with "
37972 "valid sync-hint value");
37974 if (t == error_mark_node
37975 || !parens.require_close (parser))
37976 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
37977 /*or_comma=*/false,
37978 /*consume_paren=*/true);
37979 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
37981 c = build_omp_clause (location, OMP_CLAUSE_HINT);
37982 OMP_CLAUSE_HINT_EXPR (c) = t;
37983 OMP_CLAUSE_CHAIN (c) = list;
37985 return c;
37988 /* OpenMP 5.1:
37989 filter ( integer-expression ) */
37991 static tree
37992 cp_parser_omp_clause_filter (cp_parser *parser, tree list, location_t location)
37994 tree t, c;
37996 matching_parens parens;
37997 if (!parens.require_open (parser))
37998 return list;
38000 t = cp_parser_assignment_expression (parser);
38002 if (t == error_mark_node
38003 || !parens.require_close (parser))
38004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38005 /*or_comma=*/false,
38006 /*consume_paren=*/true);
38007 check_no_duplicate_clause (list, OMP_CLAUSE_FILTER, "filter", location);
38009 c = build_omp_clause (location, OMP_CLAUSE_FILTER);
38010 OMP_CLAUSE_FILTER_EXPR (c) = t;
38011 OMP_CLAUSE_CHAIN (c) = list;
38013 return c;
38016 /* OpenMP 4.5:
38017 defaultmap ( tofrom : scalar )
38019 OpenMP 5.0:
38020 defaultmap ( implicit-behavior [ : variable-category ] ) */
38022 static tree
38023 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
38024 location_t location)
38026 tree c, id;
38027 const char *p;
38028 enum omp_clause_defaultmap_kind behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38029 enum omp_clause_defaultmap_kind category
38030 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED;
38032 matching_parens parens;
38033 if (!parens.require_open (parser))
38034 return list;
38036 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
38037 p = "default";
38038 else if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38040 invalid_behavior:
38041 cp_parser_error (parser, "expected %<alloc%>, %<to%>, %<from%>, "
38042 "%<tofrom%>, %<firstprivate%>, %<none%> "
38043 "or %<default%>");
38044 goto out_err;
38046 else
38048 id = cp_lexer_peek_token (parser->lexer)->u.value;
38049 p = IDENTIFIER_POINTER (id);
38052 switch (p[0])
38054 case 'a':
38055 if (strcmp ("alloc", p) == 0)
38056 behavior = OMP_CLAUSE_DEFAULTMAP_ALLOC;
38057 else
38058 goto invalid_behavior;
38059 break;
38061 case 'd':
38062 if (strcmp ("default", p) == 0)
38063 behavior = OMP_CLAUSE_DEFAULTMAP_DEFAULT;
38064 else
38065 goto invalid_behavior;
38066 break;
38068 case 'f':
38069 if (strcmp ("firstprivate", p) == 0)
38070 behavior = OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE;
38071 else if (strcmp ("from", p) == 0)
38072 behavior = OMP_CLAUSE_DEFAULTMAP_FROM;
38073 else
38074 goto invalid_behavior;
38075 break;
38077 case 'n':
38078 if (strcmp ("none", p) == 0)
38079 behavior = OMP_CLAUSE_DEFAULTMAP_NONE;
38080 else
38081 goto invalid_behavior;
38082 break;
38084 case 't':
38085 if (strcmp ("tofrom", p) == 0)
38086 behavior = OMP_CLAUSE_DEFAULTMAP_TOFROM;
38087 else if (strcmp ("to", p) == 0)
38088 behavior = OMP_CLAUSE_DEFAULTMAP_TO;
38089 else
38090 goto invalid_behavior;
38091 break;
38093 default:
38094 goto invalid_behavior;
38096 cp_lexer_consume_token (parser->lexer);
38098 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38100 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38101 goto out_err;
38103 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38105 invalid_category:
38106 cp_parser_error (parser, "expected %<scalar%>, %<aggregate%> or "
38107 "%<pointer%>");
38108 goto out_err;
38110 id = cp_lexer_peek_token (parser->lexer)->u.value;
38111 p = IDENTIFIER_POINTER (id);
38113 switch (p[0])
38115 case 'a':
38116 if (strcmp ("aggregate", p) == 0)
38117 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE;
38118 else
38119 goto invalid_category;
38120 break;
38122 case 'p':
38123 if (strcmp ("pointer", p) == 0)
38124 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER;
38125 else
38126 goto invalid_category;
38127 break;
38129 case 's':
38130 if (strcmp ("scalar", p) == 0)
38131 category = OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR;
38132 else
38133 goto invalid_category;
38134 break;
38136 default:
38137 goto invalid_category;
38140 cp_lexer_consume_token (parser->lexer);
38142 if (!parens.require_close (parser))
38143 goto out_err;
38145 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
38146 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEFAULTMAP
38147 && (category == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
38148 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c) == category
38149 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c)
38150 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)))
38152 enum omp_clause_defaultmap_kind cat = category;
38153 location_t loc = OMP_CLAUSE_LOCATION (c);
38154 if (cat == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED)
38155 cat = OMP_CLAUSE_DEFAULTMAP_CATEGORY (c);
38156 p = NULL;
38157 switch (cat)
38159 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED:
38160 p = NULL;
38161 break;
38162 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE:
38163 p = "aggregate";
38164 break;
38165 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER:
38166 p = "pointer";
38167 break;
38168 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR:
38169 p = "scalar";
38170 break;
38171 default:
38172 gcc_unreachable ();
38174 if (p)
38175 error_at (loc, "too many %<defaultmap%> clauses with %qs category",
38177 else
38178 error_at (loc, "too many %<defaultmap%> clauses with unspecified "
38179 "category");
38180 break;
38183 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
38184 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c, behavior, category);
38185 OMP_CLAUSE_CHAIN (c) = list;
38186 return c;
38188 out_err:
38189 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38190 /*or_comma=*/false,
38191 /*consume_paren=*/true);
38192 return list;
38195 /* OpenMP 5.0:
38196 order ( concurrent )
38198 OpenMP 5.1:
38199 order ( order-modifier : concurrent )
38201 order-modifier:
38202 reproducible
38203 unconstrained */
38205 static tree
38206 cp_parser_omp_clause_order (cp_parser *parser, tree list, location_t location)
38208 tree c, id;
38209 const char *p;
38210 bool unconstrained = false;
38211 bool reproducible = false;
38213 matching_parens parens;
38214 if (!parens.require_open (parser))
38215 return list;
38217 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38218 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38220 id = cp_lexer_peek_token (parser->lexer)->u.value;
38221 p = IDENTIFIER_POINTER (id);
38222 if (strcmp (p, "unconstrained") == 0)
38223 unconstrained = true;
38224 else if (strcmp (p, "reproducible") == 0)
38225 reproducible = true;
38226 else
38228 cp_parser_error (parser, "expected %<reproducible%> or "
38229 "%<unconstrained%>");
38230 goto out_err;
38232 cp_lexer_consume_token (parser->lexer);
38233 cp_lexer_consume_token (parser->lexer);
38235 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38237 cp_parser_error (parser, "expected %<concurrent%>");
38238 goto out_err;
38240 else
38242 id = cp_lexer_peek_token (parser->lexer)->u.value;
38243 p = IDENTIFIER_POINTER (id);
38245 if (strcmp (p, "concurrent") != 0)
38247 cp_parser_error (parser, "expected %<concurrent%>");
38248 goto out_err;
38250 cp_lexer_consume_token (parser->lexer);
38251 if (!parens.require_close (parser))
38252 goto out_err;
38254 check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location);
38255 c = build_omp_clause (location, OMP_CLAUSE_ORDER);
38256 OMP_CLAUSE_ORDER_UNCONSTRAINED (c) = unconstrained;
38257 OMP_CLAUSE_ORDER_REPRODUCIBLE (c) = reproducible;
38258 OMP_CLAUSE_CHAIN (c) = list;
38259 return c;
38261 out_err:
38262 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38263 /*or_comma=*/false,
38264 /*consume_paren=*/true);
38265 return list;
38268 /* OpenMP 5.0:
38269 bind ( teams | parallel | thread ) */
38271 static tree
38272 cp_parser_omp_clause_bind (cp_parser *parser, tree list,
38273 location_t location)
38275 tree c;
38276 const char *p;
38277 enum omp_clause_bind_kind kind = OMP_CLAUSE_BIND_THREAD;
38279 matching_parens parens;
38280 if (!parens.require_open (parser))
38281 return list;
38283 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38285 invalid:
38286 cp_parser_error (parser,
38287 "expected %<teams%>, %<parallel%> or %<thread%>");
38288 goto out_err;
38290 else
38292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38293 p = IDENTIFIER_POINTER (id);
38295 if (strcmp (p, "teams") == 0)
38296 kind = OMP_CLAUSE_BIND_TEAMS;
38297 else if (strcmp (p, "parallel") == 0)
38298 kind = OMP_CLAUSE_BIND_PARALLEL;
38299 else if (strcmp (p, "thread") != 0)
38300 goto invalid;
38301 cp_lexer_consume_token (parser->lexer);
38302 if (!parens.require_close (parser))
38303 goto out_err;
38305 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
38306 c = build_omp_clause (location, OMP_CLAUSE_BIND);
38307 OMP_CLAUSE_BIND_KIND (c) = kind;
38308 OMP_CLAUSE_CHAIN (c) = list;
38309 return c;
38311 out_err:
38312 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38313 /*or_comma=*/false,
38314 /*consume_paren=*/true);
38315 return list;
38318 /* OpenMP 2.5:
38319 ordered
38321 OpenMP 4.5:
38322 ordered ( constant-expression ) */
38324 static tree
38325 cp_parser_omp_clause_ordered (cp_parser *parser,
38326 tree list, location_t location)
38328 tree c, num = NULL_TREE;
38329 HOST_WIDE_INT n;
38331 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
38332 "ordered", location);
38334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
38336 matching_parens parens;
38337 parens.consume_open (parser);
38339 num = cp_parser_constant_expression (parser);
38341 if (!parens.require_close (parser))
38342 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38343 /*or_comma=*/false,
38344 /*consume_paren=*/true);
38346 if (num == error_mark_node)
38347 return list;
38348 num = fold_non_dependent_expr (num);
38349 if (!tree_fits_shwi_p (num)
38350 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
38351 || (n = tree_to_shwi (num)) <= 0
38352 || (int) n != n)
38354 error_at (location,
38355 "ordered argument needs positive constant integer "
38356 "expression");
38357 return list;
38361 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
38362 OMP_CLAUSE_ORDERED_EXPR (c) = num;
38363 OMP_CLAUSE_CHAIN (c) = list;
38364 return c;
38367 /* OpenMP 2.5:
38368 reduction ( reduction-operator : variable-list )
38370 reduction-operator:
38371 One of: + * - & ^ | && ||
38373 OpenMP 3.1:
38375 reduction-operator:
38376 One of: + * - & ^ | && || min max
38378 OpenMP 4.0:
38380 reduction-operator:
38381 One of: + * - & ^ | && ||
38382 id-expression
38384 OpenMP 5.0:
38385 reduction ( reduction-modifier, reduction-operator : variable-list )
38386 in_reduction ( reduction-operator : variable-list )
38387 task_reduction ( reduction-operator : variable-list ) */
38389 static tree
38390 cp_parser_omp_clause_reduction (cp_parser *parser, enum omp_clause_code kind,
38391 bool is_omp, tree list)
38393 enum tree_code code = ERROR_MARK;
38394 tree nlist, c, id = NULL_TREE;
38395 bool task = false;
38396 bool inscan = false;
38398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38399 return list;
38401 if (kind == OMP_CLAUSE_REDUCTION && is_omp)
38403 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT)
38404 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38406 cp_lexer_consume_token (parser->lexer);
38407 cp_lexer_consume_token (parser->lexer);
38409 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38410 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA))
38412 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38413 const char *p = IDENTIFIER_POINTER (id);
38414 if (strcmp (p, "task") == 0)
38415 task = true;
38416 else if (strcmp (p, "inscan") == 0)
38417 inscan = true;
38418 if (task || inscan)
38420 cp_lexer_consume_token (parser->lexer);
38421 cp_lexer_consume_token (parser->lexer);
38426 switch (cp_lexer_peek_token (parser->lexer)->type)
38428 case CPP_PLUS: code = PLUS_EXPR; break;
38429 case CPP_MULT: code = MULT_EXPR; break;
38430 case CPP_MINUS: code = MINUS_EXPR; break;
38431 case CPP_AND: code = BIT_AND_EXPR; break;
38432 case CPP_XOR: code = BIT_XOR_EXPR; break;
38433 case CPP_OR: code = BIT_IOR_EXPR; break;
38434 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
38435 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
38436 default: break;
38439 if (code != ERROR_MARK)
38440 cp_lexer_consume_token (parser->lexer);
38441 else
38443 bool saved_colon_corrects_to_scope_p;
38444 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38445 parser->colon_corrects_to_scope_p = false;
38446 id = cp_parser_id_expression (parser, /*template_p=*/false,
38447 /*check_dependency_p=*/true,
38448 /*template_p=*/NULL,
38449 /*declarator_p=*/false,
38450 /*optional_p=*/false);
38451 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38452 if (identifier_p (id))
38454 const char *p = IDENTIFIER_POINTER (id);
38456 if (strcmp (p, "min") == 0)
38457 code = MIN_EXPR;
38458 else if (strcmp (p, "max") == 0)
38459 code = MAX_EXPR;
38460 else if (id == ovl_op_identifier (false, PLUS_EXPR))
38461 code = PLUS_EXPR;
38462 else if (id == ovl_op_identifier (false, MULT_EXPR))
38463 code = MULT_EXPR;
38464 else if (id == ovl_op_identifier (false, MINUS_EXPR))
38465 code = MINUS_EXPR;
38466 else if (id == ovl_op_identifier (false, BIT_AND_EXPR))
38467 code = BIT_AND_EXPR;
38468 else if (id == ovl_op_identifier (false, BIT_IOR_EXPR))
38469 code = BIT_IOR_EXPR;
38470 else if (id == ovl_op_identifier (false, BIT_XOR_EXPR))
38471 code = BIT_XOR_EXPR;
38472 else if (id == ovl_op_identifier (false, TRUTH_ANDIF_EXPR))
38473 code = TRUTH_ANDIF_EXPR;
38474 else if (id == ovl_op_identifier (false, TRUTH_ORIF_EXPR))
38475 code = TRUTH_ORIF_EXPR;
38476 id = omp_reduction_id (code, id, NULL_TREE);
38477 tree scope = parser->scope;
38478 if (scope)
38479 id = build_qualified_name (NULL_TREE, scope, id, false);
38480 parser->scope = NULL_TREE;
38481 parser->qualifying_scope = NULL_TREE;
38482 parser->object_scope = NULL_TREE;
38484 else
38486 error ("invalid reduction-identifier");
38487 resync_fail:
38488 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38489 /*or_comma=*/false,
38490 /*consume_paren=*/true);
38491 return list;
38495 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
38496 goto resync_fail;
38498 nlist = cp_parser_omp_var_list_no_open (parser, kind, list,
38499 NULL);
38500 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38502 OMP_CLAUSE_REDUCTION_CODE (c) = code;
38503 if (task)
38504 OMP_CLAUSE_REDUCTION_TASK (c) = 1;
38505 else if (inscan)
38506 OMP_CLAUSE_REDUCTION_INSCAN (c) = 1;
38507 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
38510 return nlist;
38513 /* OpenMP 2.5:
38514 schedule ( schedule-kind )
38515 schedule ( schedule-kind , expression )
38517 schedule-kind:
38518 static | dynamic | guided | runtime | auto
38520 OpenMP 4.5:
38521 schedule ( schedule-modifier : schedule-kind )
38522 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
38524 schedule-modifier:
38525 simd
38526 monotonic
38527 nonmonotonic */
38529 static tree
38530 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
38532 tree c, t;
38533 int modifiers = 0, nmodifiers = 0;
38535 matching_parens parens;
38536 if (!parens.require_open (parser))
38537 return list;
38539 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
38541 location_t comma = UNKNOWN_LOCATION;
38542 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38544 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38545 const char *p = IDENTIFIER_POINTER (id);
38546 if (strcmp ("simd", p) == 0)
38547 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
38548 else if (strcmp ("monotonic", p) == 0)
38549 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
38550 else if (strcmp ("nonmonotonic", p) == 0)
38551 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
38552 else
38553 break;
38554 comma = UNKNOWN_LOCATION;
38555 cp_lexer_consume_token (parser->lexer);
38556 if (nmodifiers++ == 0
38557 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38559 comma = cp_lexer_peek_token (parser->lexer)->location;
38560 cp_lexer_consume_token (parser->lexer);
38562 else
38564 cp_parser_require (parser, CPP_COLON, RT_COLON);
38565 break;
38568 if (comma != UNKNOWN_LOCATION)
38569 error_at (comma, "expected %<:%>");
38571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
38573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38574 const char *p = IDENTIFIER_POINTER (id);
38576 switch (p[0])
38578 case 'd':
38579 if (strcmp ("dynamic", p) != 0)
38580 goto invalid_kind;
38581 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
38582 break;
38584 case 'g':
38585 if (strcmp ("guided", p) != 0)
38586 goto invalid_kind;
38587 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
38588 break;
38590 case 'r':
38591 if (strcmp ("runtime", p) != 0)
38592 goto invalid_kind;
38593 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
38594 break;
38596 default:
38597 goto invalid_kind;
38600 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
38601 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
38602 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
38603 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
38604 else
38605 goto invalid_kind;
38606 cp_lexer_consume_token (parser->lexer);
38608 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
38609 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38610 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
38611 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
38613 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
38614 "specified");
38615 modifiers = 0;
38618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38620 cp_token *token;
38621 cp_lexer_consume_token (parser->lexer);
38623 token = cp_lexer_peek_token (parser->lexer);
38624 t = cp_parser_assignment_expression (parser);
38626 if (t == error_mark_node)
38627 goto resync_fail;
38628 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
38629 error_at (token->location, "schedule %<runtime%> does not take "
38630 "a %<chunk_size%> parameter");
38631 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
38632 error_at (token->location, "schedule %<auto%> does not take "
38633 "a %<chunk_size%> parameter");
38634 else
38635 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
38637 if (!parens.require_close (parser))
38638 goto resync_fail;
38640 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
38641 goto resync_fail;
38643 OMP_CLAUSE_SCHEDULE_KIND (c)
38644 = (enum omp_clause_schedule_kind)
38645 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
38647 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
38648 OMP_CLAUSE_CHAIN (c) = list;
38649 return c;
38651 invalid_kind:
38652 cp_parser_error (parser, "invalid schedule kind");
38653 resync_fail:
38654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38655 /*or_comma=*/false,
38656 /*consume_paren=*/true);
38657 return list;
38660 /* OpenMP 3.0:
38661 untied */
38663 static tree
38664 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
38665 tree list, location_t location)
38667 tree c;
38669 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
38671 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
38672 OMP_CLAUSE_CHAIN (c) = list;
38673 return c;
38676 /* OpenMP 4.0:
38677 inbranch
38678 notinbranch */
38680 static tree
38681 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
38682 tree list, location_t location)
38684 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38685 tree c = build_omp_clause (location, code);
38686 OMP_CLAUSE_CHAIN (c) = list;
38687 return c;
38690 /* OpenMP 4.0:
38691 parallel
38693 sections
38694 taskgroup */
38696 static tree
38697 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
38698 enum omp_clause_code code,
38699 tree list, location_t location)
38701 tree c = build_omp_clause (location, code);
38702 OMP_CLAUSE_CHAIN (c) = list;
38703 return c;
38706 /* OpenMP 4.5:
38707 nogroup */
38709 static tree
38710 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
38711 tree list, location_t location)
38713 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
38714 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
38715 OMP_CLAUSE_CHAIN (c) = list;
38716 return c;
38719 /* OpenMP 4.5:
38720 simd
38721 threads */
38723 static tree
38724 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
38725 enum omp_clause_code code,
38726 tree list, location_t location)
38728 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
38729 tree c = build_omp_clause (location, code);
38730 OMP_CLAUSE_CHAIN (c) = list;
38731 return c;
38734 /* OpenMP 4.0:
38735 num_teams ( expression )
38737 OpenMP 5.1:
38738 num_teams ( expression : expression ) */
38740 static tree
38741 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
38742 location_t location)
38744 tree upper, lower = NULL_TREE, c;
38746 matching_parens parens;
38747 if (!parens.require_open (parser))
38748 return list;
38750 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38751 parser->colon_corrects_to_scope_p = false;
38752 upper = cp_parser_assignment_expression (parser);
38753 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38755 if (upper != error_mark_node
38756 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38758 lower = upper;
38759 cp_lexer_consume_token (parser->lexer);
38760 upper = cp_parser_assignment_expression (parser);
38763 if (upper == error_mark_node
38764 || !parens.require_close (parser))
38765 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38766 /*or_comma=*/false,
38767 /*consume_paren=*/true);
38769 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
38770 "num_teams", location);
38772 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
38773 OMP_CLAUSE_NUM_TEAMS_UPPER_EXPR (c) = upper;
38774 OMP_CLAUSE_NUM_TEAMS_LOWER_EXPR (c) = lower;
38775 OMP_CLAUSE_CHAIN (c) = list;
38777 return c;
38780 /* OpenMP 4.0:
38781 thread_limit ( expression ) */
38783 static tree
38784 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
38785 location_t location)
38787 tree t, c;
38789 matching_parens parens;
38790 if (!parens.require_open (parser))
38791 return list;
38793 t = cp_parser_assignment_expression (parser);
38795 if (t == error_mark_node
38796 || !parens.require_close (parser))
38797 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38798 /*or_comma=*/false,
38799 /*consume_paren=*/true);
38801 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
38802 "thread_limit", location);
38804 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
38805 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
38806 OMP_CLAUSE_CHAIN (c) = list;
38808 return c;
38811 /* OpenMP 4.0:
38812 aligned ( variable-list )
38813 aligned ( variable-list : constant-expression ) */
38815 static tree
38816 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
38818 tree nlist, c, alignment = NULL_TREE;
38819 bool colon;
38821 matching_parens parens;
38822 if (!parens.require_open (parser))
38823 return list;
38825 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
38826 &colon);
38828 if (colon)
38830 alignment = cp_parser_constant_expression (parser);
38832 if (!parens.require_close (parser))
38833 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
38834 /*or_comma=*/false,
38835 /*consume_paren=*/true);
38837 if (alignment == error_mark_node)
38838 alignment = NULL_TREE;
38841 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38842 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
38844 return nlist;
38847 /* OpenMP 5.0:
38848 allocate ( variable-list )
38849 allocate ( expression : variable-list )
38851 OpenMP 5.1:
38852 allocate ( allocator-modifier : variable-list )
38853 allocate ( allocator-modifier , allocator-modifier : variable-list )
38855 allocator-modifier:
38856 allocator ( expression )
38857 align ( expression ) */
38859 static tree
38860 cp_parser_omp_clause_allocate (cp_parser *parser, tree list)
38862 tree nlist, c, allocator = NULL_TREE, align = NULL_TREE;
38863 bool colon, has_modifiers = false;
38865 matching_parens parens;
38866 if (!parens.require_open (parser))
38867 return list;
38869 cp_parser_parse_tentatively (parser);
38870 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38871 parser->colon_corrects_to_scope_p = false;
38872 for (int mod = 0; mod < 2; mod++)
38873 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38874 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
38876 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38877 const char *p = IDENTIFIER_POINTER (id);
38878 if (strcmp (p, "allocator") != 0 && strcmp (p, "align") != 0)
38879 break;
38880 cp_lexer_consume_token (parser->lexer);
38881 matching_parens parens2;
38882 if (!parens2.require_open (parser))
38883 break;
38884 if (strcmp (p, "allocator") == 0)
38886 if (allocator != NULL_TREE)
38887 break;
38888 allocator = cp_parser_assignment_expression (parser);
38890 else
38892 if (align != NULL_TREE)
38893 break;
38894 align = cp_parser_assignment_expression (parser);
38896 if (!parens2.require_close (parser))
38897 break;
38898 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38900 has_modifiers = true;
38901 break;
38903 if (mod != 0 || cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
38904 break;
38905 cp_lexer_consume_token (parser->lexer);
38907 else
38908 break;
38909 if (!has_modifiers)
38911 cp_parser_abort_tentative_parse (parser);
38912 align = NULL_TREE;
38913 allocator = NULL_TREE;
38914 cp_parser_parse_tentatively (parser);
38915 allocator = cp_parser_assignment_expression (parser);
38917 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38918 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
38920 cp_parser_parse_definitely (parser);
38921 cp_lexer_consume_token (parser->lexer);
38922 if (allocator == error_mark_node)
38923 allocator = NULL_TREE;
38924 if (align == error_mark_node)
38925 align = NULL_TREE;
38927 else
38929 cp_parser_abort_tentative_parse (parser);
38930 allocator = NULL_TREE;
38931 align = NULL_TREE;
38934 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALLOCATE, list,
38935 &colon);
38937 if (allocator || align)
38938 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38940 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
38941 OMP_CLAUSE_ALLOCATE_ALIGN (c) = align;
38944 return nlist;
38947 /* OpenMP 2.5:
38948 lastprivate ( variable-list )
38950 OpenMP 5.0:
38951 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
38953 static tree
38954 cp_parser_omp_clause_lastprivate (cp_parser *parser, tree list)
38956 bool conditional = false;
38958 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38959 return list;
38961 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
38962 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
38964 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
38965 const char *p = IDENTIFIER_POINTER (id);
38967 if (strcmp ("conditional", p) == 0)
38969 conditional = true;
38970 cp_lexer_consume_token (parser->lexer);
38971 cp_lexer_consume_token (parser->lexer);
38975 tree nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LASTPRIVATE,
38976 list, NULL);
38978 if (conditional)
38979 for (tree c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
38980 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c) = 1;
38981 return nlist;
38984 /* OpenMP 4.0:
38985 linear ( variable-list )
38986 linear ( variable-list : expression )
38988 OpenMP 4.5:
38989 linear ( modifier ( variable-list ) )
38990 linear ( modifier ( variable-list ) : expression )
38992 modifier:
38995 uval
38997 OpenMP 5.2:
38998 linear ( variable-list : modifiers-list )
39000 modifiers:
39003 uval
39004 step ( expression ) */
39006 static tree
39007 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
39008 bool declare_simd)
39010 tree nlist, c, step = integer_one_node;
39011 bool colon;
39012 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
39013 bool old_linear_modifier = false;
39015 matching_parens parens;
39016 if (!parens.require_open (parser))
39017 return list;
39019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39022 const char *p = IDENTIFIER_POINTER (id);
39024 if (strcmp ("ref", p) == 0)
39025 kind = OMP_CLAUSE_LINEAR_REF;
39026 else if (strcmp ("val", p) == 0)
39027 kind = OMP_CLAUSE_LINEAR_VAL;
39028 else if (strcmp ("uval", p) == 0)
39029 kind = OMP_CLAUSE_LINEAR_UVAL;
39030 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39032 cp_lexer_consume_token (parser->lexer);
39033 old_linear_modifier = true;
39035 else
39036 kind = OMP_CLAUSE_LINEAR_DEFAULT;
39039 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
39040 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
39041 &colon);
39042 else
39044 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
39045 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
39046 if (colon)
39047 cp_parser_require (parser, CPP_COLON, RT_COLON);
39048 else if (!parens.require_close (parser))
39049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39050 /*or_comma=*/false,
39051 /*consume_paren=*/true);
39054 if (colon)
39056 bool has_modifiers = false;
39057 if (kind == OMP_CLAUSE_LINEAR_DEFAULT
39058 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39060 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39061 const char *p = IDENTIFIER_POINTER (id);
39062 size_t pos = 0;
39063 if (strcmp ("ref", p) == 0
39064 || strcmp ("val", p) == 0
39065 || strcmp ("uval", p) == 0)
39066 pos = 2;
39067 else if (strcmp ("step", p) == 0
39068 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
39070 pos = cp_parser_skip_balanced_tokens (parser, 2);
39071 if (pos == 2)
39072 pos = 0;
39074 if (pos != 0
39075 && (cp_lexer_nth_token_is (parser->lexer, pos, CPP_COMMA)
39076 || cp_lexer_nth_token_is (parser->lexer, pos,
39077 CPP_CLOSE_PAREN)))
39078 has_modifiers = true;
39081 step = NULL_TREE;
39082 if (has_modifiers)
39084 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39086 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39087 const char *p = IDENTIFIER_POINTER (id);
39088 enum omp_clause_linear_kind nkind = OMP_CLAUSE_LINEAR_DEFAULT;
39089 if (strcmp ("ref", p) == 0)
39090 nkind = OMP_CLAUSE_LINEAR_REF;
39091 else if (strcmp ("val", p) == 0)
39092 nkind = OMP_CLAUSE_LINEAR_VAL;
39093 else if (strcmp ("uval", p) == 0)
39094 nkind = OMP_CLAUSE_LINEAR_UVAL;
39095 if (nkind != OMP_CLAUSE_LINEAR_DEFAULT)
39097 if (kind != OMP_CLAUSE_LINEAR_DEFAULT)
39098 error_at (cp_lexer_peek_token (parser->lexer)->location,
39099 "multiple linear modifiers");
39100 kind = nkind;
39101 cp_lexer_consume_token (parser->lexer);
39103 else if (strcmp ("step", p) == 0)
39105 location_t step_loc
39106 = cp_lexer_peek_token (parser->lexer)->location;
39107 cp_lexer_consume_token (parser->lexer);
39108 matching_parens parens2;
39109 if (parens2.require_open (parser))
39111 if (step)
39112 error_at (step_loc, "multiple %<step%> modifiers");
39113 if (declare_simd
39114 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39115 && cp_lexer_nth_token_is (parser->lexer, 2,
39116 CPP_CLOSE_PAREN))
39118 cp_token *token
39119 = cp_lexer_peek_token (parser->lexer);
39120 location_t tok_loc = token->location;
39121 cp_parser_parse_tentatively (parser);
39122 step = cp_parser_id_expression (parser, false, true,
39123 NULL, false, false);
39124 if (step != error_mark_node)
39125 step = cp_parser_lookup_name_simple (parser, step,
39126 tok_loc);
39127 if (step == error_mark_node)
39129 step = NULL_TREE;
39130 cp_parser_abort_tentative_parse (parser);
39132 else if (!cp_parser_parse_definitely (parser))
39133 step = NULL_TREE;
39135 if (!step)
39136 step = cp_parser_assignment_expression (parser);
39137 if (!parens2.require_close (parser))
39138 cp_parser_skip_to_closing_parenthesis (parser, true,
39139 false, true);
39141 else
39142 break;
39144 else
39145 break;
39146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39148 cp_lexer_consume_token (parser->lexer);
39149 continue;
39151 break;
39153 if (!step)
39154 step = integer_one_node;
39156 else if (declare_simd
39157 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39158 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
39160 cp_token *token = cp_lexer_peek_token (parser->lexer);
39161 cp_parser_parse_tentatively (parser);
39162 step = cp_parser_id_expression (parser, /*template_p=*/false,
39163 /*check_dependency_p=*/true,
39164 /*template_p=*/NULL,
39165 /*declarator_p=*/false,
39166 /*optional_p=*/false);
39167 if (step != error_mark_node)
39168 step = cp_parser_lookup_name_simple (parser, step, token->location);
39169 if (step == error_mark_node)
39171 step = NULL_TREE;
39172 cp_parser_abort_tentative_parse (parser);
39174 else if (!cp_parser_parse_definitely (parser))
39175 step = NULL_TREE;
39177 if (!step)
39178 step = cp_parser_assignment_expression (parser);
39180 if (!parens.require_close (parser))
39181 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39182 /*or_comma=*/false,
39183 /*consume_paren=*/true);
39185 if (step == error_mark_node)
39186 return list;
39189 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39191 OMP_CLAUSE_LINEAR_STEP (c) = step;
39192 OMP_CLAUSE_LINEAR_KIND (c) = kind;
39193 OMP_CLAUSE_LINEAR_OLD_LINEAR_MODIFIER (c) = old_linear_modifier;
39196 return nlist;
39199 /* OpenMP 4.0:
39200 safelen ( constant-expression ) */
39202 static tree
39203 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
39204 location_t location)
39206 tree t, c;
39208 matching_parens parens;
39209 if (!parens.require_open (parser))
39210 return list;
39212 t = cp_parser_constant_expression (parser);
39214 if (t == error_mark_node
39215 || !parens.require_close (parser))
39216 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39217 /*or_comma=*/false,
39218 /*consume_paren=*/true);
39220 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
39222 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
39223 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
39224 OMP_CLAUSE_CHAIN (c) = list;
39226 return c;
39229 /* OpenMP 4.0:
39230 simdlen ( constant-expression ) */
39232 static tree
39233 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
39234 location_t location)
39236 tree t, c;
39238 matching_parens parens;
39239 if (!parens.require_open (parser))
39240 return list;
39242 t = cp_parser_constant_expression (parser);
39244 if (t == error_mark_node
39245 || !parens.require_close (parser))
39246 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39247 /*or_comma=*/false,
39248 /*consume_paren=*/true);
39250 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
39252 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
39253 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
39254 OMP_CLAUSE_CHAIN (c) = list;
39256 return c;
39259 /* OpenMP 4.5:
39260 vec:
39261 identifier [+/- integer]
39262 vec , identifier [+/- integer]
39265 static tree
39266 cp_parser_omp_clause_doacross_sink (cp_parser *parser, location_t clause_loc,
39267 tree list, bool depend_p)
39269 tree vec = NULL;
39271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39273 cp_parser_error (parser, "expected identifier");
39274 return list;
39277 if (!depend_p)
39279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39280 if (strcmp (IDENTIFIER_POINTER (id), "omp_cur_iteration") == 0
39281 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_MINUS)
39282 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_NUMBER)
39283 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_PAREN))
39285 tree val = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
39286 if (integer_onep (val))
39288 cp_lexer_consume_token (parser->lexer);
39289 cp_lexer_consume_token (parser->lexer);
39290 cp_lexer_consume_token (parser->lexer);
39291 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
39292 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
39293 OMP_CLAUSE_CHAIN (u) = list;
39294 return u;
39299 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39301 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
39302 tree t, identifier = cp_parser_identifier (parser);
39303 tree addend = NULL;
39305 if (identifier == error_mark_node)
39306 t = error_mark_node;
39307 else
39309 t = cp_parser_lookup_name_simple
39310 (parser, identifier,
39311 cp_lexer_peek_token (parser->lexer)->location);
39312 if (t == error_mark_node)
39313 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
39314 id_loc);
39317 bool neg = false;
39318 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
39319 neg = true;
39320 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
39322 addend = integer_zero_node;
39323 goto add_to_vector;
39325 cp_lexer_consume_token (parser->lexer);
39327 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
39329 cp_parser_error (parser, "expected integer");
39330 return list;
39333 addend = cp_lexer_peek_token (parser->lexer)->u.value;
39334 if (TREE_CODE (addend) != INTEGER_CST)
39336 cp_parser_error (parser, "expected integer");
39337 return list;
39339 cp_lexer_consume_token (parser->lexer);
39341 add_to_vector:
39342 if (t != error_mark_node)
39344 vec = tree_cons (addend, t, vec);
39345 if (neg)
39346 OMP_CLAUSE_DOACROSS_SINK_NEGATIVE (vec) = 1;
39349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
39350 || !cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
39351 break;
39353 cp_lexer_consume_token (parser->lexer);
39356 if (vec)
39358 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DOACROSS);
39359 OMP_CLAUSE_DOACROSS_KIND (u) = OMP_CLAUSE_DOACROSS_SINK;
39360 OMP_CLAUSE_DOACROSS_DEPEND (u) = depend_p;
39361 OMP_CLAUSE_DECL (u) = nreverse (vec);
39362 OMP_CLAUSE_CHAIN (u) = list;
39363 return u;
39365 return list;
39368 /* OpenMP 5.0:
39369 detach ( event-handle ) */
39371 static tree
39372 cp_parser_omp_clause_detach (cp_parser *parser, tree list)
39374 matching_parens parens;
39376 if (!parens.require_open (parser))
39377 return list;
39379 cp_token *token;
39380 tree name, decl;
39382 token = cp_lexer_peek_token (parser->lexer);
39383 name = cp_parser_id_expression (parser, /*template_p=*/false,
39384 /*check_dependency_p=*/true,
39385 /*template_p=*/NULL,
39386 /*declarator_p=*/false,
39387 /*optional_p=*/false);
39388 if (name == error_mark_node)
39389 decl = error_mark_node;
39390 else
39392 if (identifier_p (name))
39393 decl = cp_parser_lookup_name_simple (parser, name, token->location);
39394 else
39395 decl = name;
39396 if (decl == error_mark_node)
39397 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
39398 token->location);
39401 if (decl == error_mark_node
39402 || !parens.require_close (parser))
39403 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39404 /*or_comma=*/false,
39405 /*consume_paren=*/true);
39407 tree u = build_omp_clause (token->location, OMP_CLAUSE_DETACH);
39408 OMP_CLAUSE_DECL (u) = decl;
39409 OMP_CLAUSE_CHAIN (u) = list;
39411 return u;
39414 /* OpenMP 5.0:
39415 iterators ( iterators-definition )
39417 iterators-definition:
39418 iterator-specifier
39419 iterator-specifier , iterators-definition
39421 iterator-specifier:
39422 identifier = range-specification
39423 iterator-type identifier = range-specification
39425 range-specification:
39426 begin : end
39427 begin : end : step */
39429 static tree
39430 cp_parser_omp_iterators (cp_parser *parser)
39432 tree ret = NULL_TREE, *last = &ret;
39433 cp_lexer_consume_token (parser->lexer);
39435 matching_parens parens;
39436 if (!parens.require_open (parser))
39437 return error_mark_node;
39439 bool saved_colon_corrects_to_scope_p
39440 = parser->colon_corrects_to_scope_p;
39441 bool saved_colon_doesnt_start_class_def_p
39442 = parser->colon_doesnt_start_class_def_p;
39446 tree iter_type;
39447 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39448 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_EQ))
39449 iter_type = integer_type_node;
39450 else
39452 const char *saved_message
39453 = parser->type_definition_forbidden_message;
39454 parser->type_definition_forbidden_message
39455 = G_("types may not be defined in iterator type");
39457 iter_type = cp_parser_type_id (parser);
39459 parser->type_definition_forbidden_message = saved_message;
39462 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
39463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39465 cp_parser_error (parser, "expected identifier");
39466 break;
39469 tree id = cp_parser_identifier (parser);
39470 if (id == error_mark_node)
39471 break;
39473 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
39474 break;
39476 parser->colon_corrects_to_scope_p = false;
39477 parser->colon_doesnt_start_class_def_p = true;
39478 tree begin = cp_parser_assignment_expression (parser);
39480 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39481 break;
39483 tree end = cp_parser_assignment_expression (parser);
39485 tree step = integer_one_node;
39486 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
39488 cp_lexer_consume_token (parser->lexer);
39489 step = cp_parser_assignment_expression (parser);
39492 tree iter_var = build_decl (loc, VAR_DECL, id, iter_type);
39493 DECL_ARTIFICIAL (iter_var) = 1;
39494 DECL_CONTEXT (iter_var) = current_function_decl;
39495 pushdecl (iter_var);
39497 *last = make_tree_vec (6);
39498 TREE_VEC_ELT (*last, 0) = iter_var;
39499 TREE_VEC_ELT (*last, 1) = begin;
39500 TREE_VEC_ELT (*last, 2) = end;
39501 TREE_VEC_ELT (*last, 3) = step;
39502 last = &TREE_CHAIN (*last);
39504 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39506 cp_lexer_consume_token (parser->lexer);
39507 continue;
39509 break;
39511 while (1);
39513 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39514 parser->colon_doesnt_start_class_def_p
39515 = saved_colon_doesnt_start_class_def_p;
39517 if (!parens.require_close (parser))
39518 cp_parser_skip_to_closing_parenthesis (parser,
39519 /*recovering=*/true,
39520 /*or_comma=*/false,
39521 /*consume_paren=*/true);
39523 return ret ? ret : error_mark_node;
39526 /* OpenMP 5.0:
39527 affinity ( [aff-modifier :] variable-list )
39528 aff-modifier:
39529 iterator ( iterators-definition ) */
39531 static tree
39532 cp_parser_omp_clause_affinity (cp_parser *parser, tree list)
39534 tree nlist, c, iterators = NULL_TREE;
39536 matching_parens parens;
39537 if (!parens.require_open (parser))
39538 return list;
39540 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39542 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39543 const char *p = IDENTIFIER_POINTER (id);
39544 bool parse_iter = ((strcmp ("iterator", p) == 0)
39545 && (cp_lexer_nth_token_is (parser->lexer, 2,
39546 CPP_OPEN_PAREN)));
39547 if (parse_iter)
39549 size_t n = cp_parser_skip_balanced_tokens (parser, 2);
39550 parse_iter = cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON);
39552 if (parse_iter)
39554 begin_scope (sk_omp, NULL);
39555 iterators = cp_parser_omp_iterators (parser);
39556 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39558 if (iterators)
39559 poplevel (0, 1, 0);
39560 cp_parser_skip_to_closing_parenthesis (parser,
39561 /*recovering=*/true,
39562 /*or_comma=*/false,
39563 /*consume_paren=*/true);
39564 return list;
39568 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_AFFINITY,
39569 list, NULL);
39570 if (iterators)
39572 tree block = poplevel (1, 1, 0);
39573 if (iterators != error_mark_node)
39575 TREE_VEC_ELT (iterators, 5) = block;
39576 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39577 OMP_CLAUSE_DECL (c) = build_tree_list (iterators,
39578 OMP_CLAUSE_DECL (c));
39581 return nlist;
39584 /* OpenMP 4.0:
39585 depend ( depend-kind : variable-list )
39587 depend-kind:
39588 in | out | inout
39590 OpenMP 4.5:
39591 depend ( source )
39593 depend ( sink : vec )
39595 OpenMP 5.0:
39596 depend ( depend-modifier , depend-kind: variable-list )
39598 depend-kind:
39599 in | out | inout | mutexinoutset | depobj
39601 depend-modifier:
39602 iterator ( iterators-definition ) */
39604 static tree
39605 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
39607 tree nlist, c, iterators = NULL_TREE;
39608 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_LAST;
39609 enum omp_clause_doacross_kind dkind = OMP_CLAUSE_DOACROSS_LAST;
39611 matching_parens parens;
39612 if (!parens.require_open (parser))
39613 return list;
39617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39618 goto invalid_kind;
39620 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39621 const char *p = IDENTIFIER_POINTER (id);
39623 if (strcmp ("iterator", p) == 0 && iterators == NULL_TREE)
39625 begin_scope (sk_omp, NULL);
39626 iterators = cp_parser_omp_iterators (parser);
39627 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
39628 continue;
39630 if (strcmp ("in", p) == 0)
39631 kind = OMP_CLAUSE_DEPEND_IN;
39632 else if (strcmp ("inout", p) == 0)
39633 kind = OMP_CLAUSE_DEPEND_INOUT;
39634 else if (strcmp ("inoutset", p) == 0)
39635 kind = OMP_CLAUSE_DEPEND_INOUTSET;
39636 else if (strcmp ("mutexinoutset", p) == 0)
39637 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
39638 else if (strcmp ("out", p) == 0)
39639 kind = OMP_CLAUSE_DEPEND_OUT;
39640 else if (strcmp ("depobj", p) == 0)
39641 kind = OMP_CLAUSE_DEPEND_DEPOBJ;
39642 else if (strcmp ("sink", p) == 0)
39643 dkind = OMP_CLAUSE_DOACROSS_SINK;
39644 else if (strcmp ("source", p) == 0)
39645 dkind = OMP_CLAUSE_DOACROSS_SOURCE;
39646 else
39647 goto invalid_kind;
39648 break;
39650 while (1);
39652 cp_lexer_consume_token (parser->lexer);
39654 if (iterators
39655 && (dkind == OMP_CLAUSE_DOACROSS_SOURCE
39656 || dkind == OMP_CLAUSE_DOACROSS_SINK))
39658 poplevel (0, 1, 0);
39659 error_at (loc, "%<iterator%> modifier incompatible with %qs",
39660 dkind == OMP_CLAUSE_DOACROSS_SOURCE ? "source" : "sink");
39661 iterators = NULL_TREE;
39664 if (dkind == OMP_CLAUSE_DOACROSS_SOURCE)
39666 c = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
39667 OMP_CLAUSE_DOACROSS_KIND (c) = dkind;
39668 OMP_CLAUSE_DOACROSS_DEPEND (c) = 1;
39669 OMP_CLAUSE_DECL (c) = NULL_TREE;
39670 OMP_CLAUSE_CHAIN (c) = list;
39671 if (!parens.require_close (parser))
39672 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39673 /*or_comma=*/false,
39674 /*consume_paren=*/true);
39675 return c;
39678 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39679 goto resync_fail;
39681 if (dkind == OMP_CLAUSE_DOACROSS_SINK)
39683 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, true);
39684 if (!parens.require_close (parser))
39685 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39686 /*or_comma=*/false,
39687 /*consume_paren=*/true);
39689 else
39691 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
39692 list, NULL);
39694 if (iterators)
39696 tree block = poplevel (1, 1, 0);
39697 if (iterators == error_mark_node)
39698 iterators = NULL_TREE;
39699 else
39700 TREE_VEC_ELT (iterators, 5) = block;
39703 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39705 OMP_CLAUSE_DEPEND_KIND (c) = kind;
39706 if (iterators)
39707 OMP_CLAUSE_DECL (c)
39708 = build_tree_list (iterators, OMP_CLAUSE_DECL (c));
39711 return nlist;
39713 invalid_kind:
39714 cp_parser_error (parser, "invalid depend kind");
39715 resync_fail:
39716 if (iterators)
39717 poplevel (0, 1, 0);
39718 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39719 /*or_comma=*/false,
39720 /*consume_paren=*/true);
39721 return list;
39724 /* OpenMP 5.2:
39725 doacross ( source : )
39726 doacross ( source : omp_cur_iteration )
39728 doacross ( sink : vec )
39729 doacross ( sink : omp_cur_iteration - logical_iteration ) */
39731 static tree
39732 cp_parser_omp_clause_doacross (cp_parser *parser, tree list, location_t loc)
39734 tree nlist;
39735 enum omp_clause_doacross_kind kind = OMP_CLAUSE_DOACROSS_LAST;
39737 matching_parens parens;
39738 if (!parens.require_open (parser))
39739 return list;
39741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
39743 invalid_kind:
39744 cp_parser_error (parser, "invalid doacross kind");
39745 resync_fail:
39746 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39747 /*or_comma=*/false,
39748 /*consume_paren=*/true);
39749 return list;
39752 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39753 const char *p = IDENTIFIER_POINTER (id);
39755 if (strcmp ("sink", p) == 0)
39756 kind = OMP_CLAUSE_DOACROSS_SINK;
39757 else if (strcmp ("source", p) == 0)
39758 kind = OMP_CLAUSE_DOACROSS_SOURCE;
39759 else
39760 goto invalid_kind;
39762 cp_lexer_consume_token (parser->lexer);
39764 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
39765 goto resync_fail;
39767 if (kind == OMP_CLAUSE_DOACROSS_SOURCE)
39769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
39771 id = cp_lexer_peek_token (parser->lexer)->u.value;
39772 p = IDENTIFIER_POINTER (id);
39773 if (strcmp (p, "omp_cur_iteration") == 0)
39774 cp_lexer_consume_token (parser->lexer);
39776 nlist = build_omp_clause (loc, OMP_CLAUSE_DOACROSS);
39777 OMP_CLAUSE_DOACROSS_KIND (nlist) = OMP_CLAUSE_DOACROSS_SOURCE;
39778 OMP_CLAUSE_DECL (nlist) = NULL_TREE;
39779 OMP_CLAUSE_CHAIN (nlist) = list;
39781 else
39782 nlist = cp_parser_omp_clause_doacross_sink (parser, loc, list, false);
39784 if (!parens.require_close (parser))
39785 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39786 /*or_comma=*/false,
39787 /*consume_paren=*/true);
39788 return nlist;
39791 /* OpenMP 4.0:
39792 map ( map-kind : variable-list )
39793 map ( variable-list )
39795 map-kind:
39796 alloc | to | from | tofrom
39798 OpenMP 4.5:
39799 map-kind:
39800 alloc | to | from | tofrom | release | delete
39802 map ( always [,] map-kind: variable-list )
39804 OpenMP 5.0:
39805 map ( [map-type-modifier[,] ...] map-kind: variable-list )
39807 map-type-modifier:
39808 always | close */
39810 static tree
39811 cp_parser_omp_clause_map (cp_parser *parser, tree list)
39813 tree nlist, c;
39814 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
39816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
39817 return list;
39819 int pos = 1;
39820 int map_kind_pos = 0;
39821 while (cp_lexer_peek_nth_token (parser->lexer, pos)->type == CPP_NAME
39822 || cp_lexer_peek_nth_token (parser->lexer, pos)->keyword == RID_DELETE)
39824 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COLON)
39826 map_kind_pos = pos;
39827 break;
39830 if (cp_lexer_peek_nth_token (parser->lexer, pos + 1)->type == CPP_COMMA)
39831 pos++;
39832 pos++;
39835 bool always_modifier = false;
39836 bool close_modifier = false;
39837 for (int pos = 1; pos < map_kind_pos; ++pos)
39839 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39840 if (tok->type == CPP_COMMA)
39842 cp_lexer_consume_token (parser->lexer);
39843 continue;
39846 const char *p = IDENTIFIER_POINTER (tok->u.value);
39847 if (strcmp ("always", p) == 0)
39849 if (always_modifier)
39851 cp_parser_error (parser, "too many %<always%> modifiers");
39852 cp_parser_skip_to_closing_parenthesis (parser,
39853 /*recovering=*/true,
39854 /*or_comma=*/false,
39855 /*consume_paren=*/true);
39856 return list;
39858 always_modifier = true;
39860 else if (strcmp ("close", p) == 0)
39862 if (close_modifier)
39864 cp_parser_error (parser, "too many %<close%> modifiers");
39865 cp_parser_skip_to_closing_parenthesis (parser,
39866 /*recovering=*/true,
39867 /*or_comma=*/false,
39868 /*consume_paren=*/true);
39869 return list;
39871 close_modifier = true;
39873 else
39875 cp_parser_error (parser, "%<#pragma omp target%> with "
39876 "modifier other than %<always%> or "
39877 "%<close%> on %<map%> clause");
39878 cp_parser_skip_to_closing_parenthesis (parser,
39879 /*recovering=*/true,
39880 /*or_comma=*/false,
39881 /*consume_paren=*/true);
39882 return list;
39885 cp_lexer_consume_token (parser->lexer);
39888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39889 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
39892 const char *p = IDENTIFIER_POINTER (id);
39894 if (strcmp ("alloc", p) == 0)
39895 kind = GOMP_MAP_ALLOC;
39896 else if (strcmp ("to", p) == 0)
39897 kind = always_modifier ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
39898 else if (strcmp ("from", p) == 0)
39899 kind = always_modifier ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
39900 else if (strcmp ("tofrom", p) == 0)
39901 kind = always_modifier ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
39902 else if (strcmp ("release", p) == 0)
39903 kind = GOMP_MAP_RELEASE;
39904 else
39906 cp_parser_error (parser, "invalid map kind");
39907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39908 /*or_comma=*/false,
39909 /*consume_paren=*/true);
39910 return list;
39912 cp_lexer_consume_token (parser->lexer);
39913 cp_lexer_consume_token (parser->lexer);
39915 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
39916 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
39918 kind = GOMP_MAP_DELETE;
39919 cp_lexer_consume_token (parser->lexer);
39920 cp_lexer_consume_token (parser->lexer);
39923 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
39924 NULL, true);
39926 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
39927 OMP_CLAUSE_SET_MAP_KIND (c, kind);
39929 return nlist;
39932 /* OpenMP 4.0:
39933 device ( expression )
39935 OpenMP 5.0:
39936 device ( [device-modifier :] integer-expression )
39938 device-modifier:
39939 ancestor | device_num */
39941 static tree
39942 cp_parser_omp_clause_device (cp_parser *parser, tree list,
39943 location_t location)
39945 tree t, c;
39946 bool ancestor = false;
39948 matching_parens parens;
39949 if (!parens.require_open (parser))
39950 return list;
39952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
39953 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_COLON))
39955 cp_token *tok = cp_lexer_peek_token (parser->lexer);
39956 const char *p = IDENTIFIER_POINTER (tok->u.value);
39957 if (strcmp ("ancestor", p) == 0)
39959 ancestor = true;
39961 /* A requires directive with the reverse_offload clause must be
39962 specified. */
39963 if ((omp_requires_mask & OMP_REQUIRES_REVERSE_OFFLOAD) == 0)
39965 error_at (tok->location, "%<ancestor%> device modifier not "
39966 "preceded by %<requires%> directive "
39967 "with %<reverse_offload%> clause");
39968 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39969 return list;
39972 else if (strcmp ("device_num", p) == 0)
39974 else
39976 error_at (tok->location, "expected %<ancestor%> or %<device_num%>");
39977 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
39978 return list;
39980 cp_lexer_consume_token (parser->lexer);
39981 cp_lexer_consume_token (parser->lexer);
39984 t = cp_parser_assignment_expression (parser);
39986 if (t == error_mark_node
39987 || !parens.require_close (parser))
39988 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
39989 /*or_comma=*/false,
39990 /*consume_paren=*/true);
39992 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
39993 "device", location);
39995 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
39996 OMP_CLAUSE_DEVICE_ID (c) = t;
39997 OMP_CLAUSE_CHAIN (c) = list;
39998 OMP_CLAUSE_DEVICE_ANCESTOR (c) = ancestor;
40000 return c;
40003 /* OpenMP 4.0:
40004 dist_schedule ( static )
40005 dist_schedule ( static , expression ) */
40007 static tree
40008 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
40009 location_t location)
40011 tree c, t;
40013 matching_parens parens;
40014 if (!parens.require_open (parser))
40015 return list;
40017 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
40019 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
40020 goto invalid_kind;
40021 cp_lexer_consume_token (parser->lexer);
40023 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40025 cp_lexer_consume_token (parser->lexer);
40027 t = cp_parser_assignment_expression (parser);
40029 if (t == error_mark_node)
40030 goto resync_fail;
40031 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
40033 if (!parens.require_close (parser))
40034 goto resync_fail;
40036 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40037 goto resync_fail;
40039 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
40040 "dist_schedule", location); */
40041 if (omp_find_clause (list, OMP_CLAUSE_DIST_SCHEDULE))
40042 warning_at (location, 0, "too many %qs clauses", "dist_schedule");
40043 OMP_CLAUSE_CHAIN (c) = list;
40044 return c;
40046 invalid_kind:
40047 cp_parser_error (parser, "invalid dist_schedule kind");
40048 resync_fail:
40049 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40050 /*or_comma=*/false,
40051 /*consume_paren=*/true);
40052 return list;
40055 /* OpenMP 4.0:
40056 proc_bind ( proc-bind-kind )
40058 proc-bind-kind:
40059 primary | master | close | spread
40060 where OpenMP 5.1 added 'primary' and deprecated the alias 'master'. */
40062 static tree
40063 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
40064 location_t location)
40066 tree c;
40067 enum omp_clause_proc_bind_kind kind;
40069 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40070 return list;
40072 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40074 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40075 const char *p = IDENTIFIER_POINTER (id);
40077 if (strcmp ("primary", p) == 0)
40078 kind = OMP_CLAUSE_PROC_BIND_PRIMARY;
40079 else if (strcmp ("master", p) == 0)
40080 kind = OMP_CLAUSE_PROC_BIND_MASTER;
40081 else if (strcmp ("close", p) == 0)
40082 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
40083 else if (strcmp ("spread", p) == 0)
40084 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
40085 else
40086 goto invalid_kind;
40088 else
40089 goto invalid_kind;
40091 cp_lexer_consume_token (parser->lexer);
40092 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40093 goto resync_fail;
40095 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
40096 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
40097 location);
40098 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
40099 OMP_CLAUSE_CHAIN (c) = list;
40100 return c;
40102 invalid_kind:
40103 cp_parser_error (parser, "invalid depend kind");
40104 resync_fail:
40105 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40106 /*or_comma=*/false,
40107 /*consume_paren=*/true);
40108 return list;
40111 /* OpenMP 5.0:
40112 device_type ( host | nohost | any ) */
40114 static tree
40115 cp_parser_omp_clause_device_type (cp_parser *parser, tree list,
40116 location_t location)
40118 tree c;
40119 enum omp_clause_device_type_kind kind;
40121 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
40122 return list;
40124 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40126 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40127 const char *p = IDENTIFIER_POINTER (id);
40129 if (strcmp ("host", p) == 0)
40130 kind = OMP_CLAUSE_DEVICE_TYPE_HOST;
40131 else if (strcmp ("nohost", p) == 0)
40132 kind = OMP_CLAUSE_DEVICE_TYPE_NOHOST;
40133 else if (strcmp ("any", p) == 0)
40134 kind = OMP_CLAUSE_DEVICE_TYPE_ANY;
40135 else
40136 goto invalid_kind;
40138 else
40139 goto invalid_kind;
40141 cp_lexer_consume_token (parser->lexer);
40142 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
40143 goto resync_fail;
40145 c = build_omp_clause (location, OMP_CLAUSE_DEVICE_TYPE);
40146 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
40147 location);
40148 OMP_CLAUSE_DEVICE_TYPE_KIND (c) = kind;
40149 OMP_CLAUSE_CHAIN (c) = list;
40150 return c;
40152 invalid_kind:
40153 cp_parser_error (parser, "invalid depend kind");
40154 resync_fail:
40155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40156 /*or_comma=*/false,
40157 /*consume_paren=*/true);
40158 return list;
40161 /* OpenACC:
40162 async [( int-expr )] */
40164 static tree
40165 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
40167 tree c, t;
40168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
40170 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
40172 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
40174 matching_parens parens;
40175 parens.consume_open (parser);
40177 t = cp_parser_assignment_expression (parser);
40178 if (t == error_mark_node
40179 || !parens.require_close (parser))
40180 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
40181 /*or_comma=*/false,
40182 /*consume_paren=*/true);
40185 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
40187 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
40188 OMP_CLAUSE_ASYNC_EXPR (c) = t;
40189 OMP_CLAUSE_CHAIN (c) = list;
40190 list = c;
40192 return list;
40195 /* Parse all OpenACC clauses. The set clauses allowed by the directive
40196 is a bitmask in MASK. Return the list of clauses found. */
40198 static tree
40199 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
40200 const char *where, cp_token *pragma_tok,
40201 bool finish_p = true)
40203 tree clauses = NULL;
40204 bool first = true;
40206 /* Don't create location wrapper nodes within OpenACC clauses. */
40207 auto_suppress_location_wrappers sentinel;
40209 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40211 location_t here;
40212 pragma_omp_clause c_kind;
40213 omp_clause_code code;
40214 const char *c_name;
40215 tree prev = clauses;
40217 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40218 cp_lexer_consume_token (parser->lexer);
40220 here = cp_lexer_peek_token (parser->lexer)->location;
40221 c_kind = cp_parser_omp_clause_name (parser);
40223 switch (c_kind)
40225 case PRAGMA_OACC_CLAUSE_ASYNC:
40226 clauses = cp_parser_oacc_clause_async (parser, clauses);
40227 c_name = "async";
40228 break;
40229 case PRAGMA_OACC_CLAUSE_AUTO:
40230 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_AUTO,
40231 clauses);
40232 c_name = "auto";
40233 break;
40234 case PRAGMA_OACC_CLAUSE_ATTACH:
40235 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40236 c_name = "attach";
40237 break;
40238 case PRAGMA_OACC_CLAUSE_COLLAPSE:
40239 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
40240 c_name = "collapse";
40241 break;
40242 case PRAGMA_OACC_CLAUSE_COPY:
40243 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40244 c_name = "copy";
40245 break;
40246 case PRAGMA_OACC_CLAUSE_COPYIN:
40247 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40248 c_name = "copyin";
40249 break;
40250 case PRAGMA_OACC_CLAUSE_COPYOUT:
40251 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40252 c_name = "copyout";
40253 break;
40254 case PRAGMA_OACC_CLAUSE_CREATE:
40255 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40256 c_name = "create";
40257 break;
40258 case PRAGMA_OACC_CLAUSE_DELETE:
40259 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40260 c_name = "delete";
40261 break;
40262 case PRAGMA_OMP_CLAUSE_DEFAULT:
40263 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
40264 c_name = "default";
40265 break;
40266 case PRAGMA_OACC_CLAUSE_DETACH:
40267 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40268 c_name = "detach";
40269 break;
40270 case PRAGMA_OACC_CLAUSE_DEVICE:
40271 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40272 c_name = "device";
40273 break;
40274 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
40275 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
40276 c_name = "deviceptr";
40277 break;
40278 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
40279 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40280 c_name = "device_resident";
40281 break;
40282 case PRAGMA_OACC_CLAUSE_FINALIZE:
40283 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_FINALIZE,
40284 clauses);
40285 c_name = "finalize";
40286 break;
40287 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
40288 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40289 clauses);
40290 c_name = "firstprivate";
40291 break;
40292 case PRAGMA_OACC_CLAUSE_GANG:
40293 c_name = "gang";
40294 clauses = cp_parser_oacc_shape_clause (parser, here, OMP_CLAUSE_GANG,
40295 c_name, clauses);
40296 break;
40297 case PRAGMA_OACC_CLAUSE_HOST:
40298 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40299 c_name = "host";
40300 break;
40301 case PRAGMA_OACC_CLAUSE_IF:
40302 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
40303 c_name = "if";
40304 break;
40305 case PRAGMA_OACC_CLAUSE_IF_PRESENT:
40306 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_IF_PRESENT,
40307 clauses);
40308 c_name = "if_present";
40309 break;
40310 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
40311 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_INDEPENDENT,
40312 clauses);
40313 c_name = "independent";
40314 break;
40315 case PRAGMA_OACC_CLAUSE_LINK:
40316 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40317 c_name = "link";
40318 break;
40319 case PRAGMA_OACC_CLAUSE_NO_CREATE:
40320 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40321 c_name = "no_create";
40322 break;
40323 case PRAGMA_OACC_CLAUSE_NOHOST:
40324 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_NOHOST,
40325 clauses);
40326 c_name = "nohost";
40327 break;
40328 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
40329 code = OMP_CLAUSE_NUM_GANGS;
40330 c_name = "num_gangs";
40331 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40332 clauses);
40333 break;
40334 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
40335 c_name = "num_workers";
40336 code = OMP_CLAUSE_NUM_WORKERS;
40337 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40338 clauses);
40339 break;
40340 case PRAGMA_OACC_CLAUSE_PRESENT:
40341 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
40342 c_name = "present";
40343 break;
40344 case PRAGMA_OACC_CLAUSE_PRIVATE:
40345 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40346 clauses);
40347 c_name = "private";
40348 break;
40349 case PRAGMA_OACC_CLAUSE_REDUCTION:
40350 clauses
40351 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40352 false, clauses);
40353 c_name = "reduction";
40354 break;
40355 case PRAGMA_OACC_CLAUSE_SEQ:
40356 clauses = cp_parser_oacc_simple_clause (here, OMP_CLAUSE_SEQ,
40357 clauses);
40358 c_name = "seq";
40359 break;
40360 case PRAGMA_OACC_CLAUSE_TILE:
40361 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
40362 c_name = "tile";
40363 break;
40364 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
40365 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40366 clauses);
40367 c_name = "use_device";
40368 break;
40369 case PRAGMA_OACC_CLAUSE_VECTOR:
40370 c_name = "vector";
40371 clauses = cp_parser_oacc_shape_clause (parser, here,
40372 OMP_CLAUSE_VECTOR,
40373 c_name, clauses);
40374 break;
40375 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
40376 c_name = "vector_length";
40377 code = OMP_CLAUSE_VECTOR_LENGTH;
40378 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
40379 clauses);
40380 break;
40381 case PRAGMA_OACC_CLAUSE_WAIT:
40382 clauses = cp_parser_oacc_clause_wait (parser, clauses);
40383 c_name = "wait";
40384 break;
40385 case PRAGMA_OACC_CLAUSE_WORKER:
40386 c_name = "worker";
40387 clauses = cp_parser_oacc_shape_clause (parser, here,
40388 OMP_CLAUSE_WORKER,
40389 c_name, clauses);
40390 break;
40391 default:
40392 cp_parser_error (parser, "expected %<#pragma acc%> clause");
40393 goto saw_error;
40396 first = false;
40398 if (((mask >> c_kind) & 1) == 0)
40400 /* Remove the invalid clause(s) from the list to avoid
40401 confusing the rest of the compiler. */
40402 clauses = prev;
40403 error_at (here, "%qs is not valid for %qs", c_name, where);
40407 saw_error:
40408 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40410 if (finish_p)
40411 return finish_omp_clauses (clauses, C_ORT_ACC);
40413 return clauses;
40416 /* Parse all OpenMP clauses. The set clauses allowed by the directive
40417 is a bitmask in MASK. Return the list of clauses found.
40418 FINISH_P set if finish_omp_clauses should be called.
40419 NESTED non-zero if clauses should be terminated by closing paren instead
40420 of end of pragma. If it is 2, additionally commas are required in between
40421 the clauses. */
40423 static tree
40424 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
40425 const char *where, cp_token *pragma_tok,
40426 bool finish_p = true, int nested = 0)
40428 tree clauses = NULL;
40429 bool first = true;
40430 cp_token *token = NULL;
40432 /* Don't create location wrapper nodes within OpenMP clauses. */
40433 auto_suppress_location_wrappers sentinel;
40435 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
40437 pragma_omp_clause c_kind;
40438 const char *c_name;
40439 tree prev = clauses;
40441 if (nested && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
40442 break;
40444 if (!first
40445 /* OpenMP 5.1 allows optional comma in between directive-name and
40446 clauses everywhere, but as we aren't done with OpenMP 5.0
40447 implementation yet, let's allow it for now only in C++11
40448 attributes. */
40449 || (parser->lexer->in_omp_attribute_pragma && nested != 2))
40451 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
40452 cp_lexer_consume_token (parser->lexer);
40453 else if (nested == 2)
40454 error_at (cp_lexer_peek_token (parser->lexer)->location,
40455 "clauses in %<simd%> trait should be separated "
40456 "by %<,%>");
40459 token = cp_lexer_peek_token (parser->lexer);
40460 c_kind = cp_parser_omp_clause_name (parser);
40462 switch (c_kind)
40464 case PRAGMA_OMP_CLAUSE_BIND:
40465 clauses = cp_parser_omp_clause_bind (parser, clauses,
40466 token->location);
40467 c_name = "bind";
40468 break;
40469 case PRAGMA_OMP_CLAUSE_COLLAPSE:
40470 clauses = cp_parser_omp_clause_collapse (parser, clauses,
40471 token->location);
40472 c_name = "collapse";
40473 break;
40474 case PRAGMA_OMP_CLAUSE_COPYIN:
40475 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
40476 c_name = "copyin";
40477 break;
40478 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
40479 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
40480 clauses);
40481 c_name = "copyprivate";
40482 break;
40483 case PRAGMA_OMP_CLAUSE_DEFAULT:
40484 clauses = cp_parser_omp_clause_default (parser, clauses,
40485 token->location, false);
40486 c_name = "default";
40487 break;
40488 case PRAGMA_OMP_CLAUSE_FILTER:
40489 clauses = cp_parser_omp_clause_filter (parser, clauses,
40490 token->location);
40491 c_name = "filter";
40492 break;
40493 case PRAGMA_OMP_CLAUSE_FINAL:
40494 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
40495 c_name = "final";
40496 break;
40497 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
40498 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
40499 clauses);
40500 c_name = "firstprivate";
40501 break;
40502 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
40503 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
40504 token->location);
40505 c_name = "grainsize";
40506 break;
40507 case PRAGMA_OMP_CLAUSE_HINT:
40508 clauses = cp_parser_omp_clause_hint (parser, clauses,
40509 token->location);
40510 c_name = "hint";
40511 break;
40512 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
40513 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
40514 token->location);
40515 c_name = "defaultmap";
40516 break;
40517 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
40518 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
40519 clauses);
40520 c_name = "use_device_ptr";
40521 break;
40522 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR:
40523 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_ADDR,
40524 clauses);
40525 c_name = "use_device_addr";
40526 break;
40527 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
40528 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
40529 clauses);
40530 c_name = "is_device_ptr";
40531 break;
40532 case PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR:
40533 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_HAS_DEVICE_ADDR,
40534 clauses);
40535 c_name = "has_device_addr";
40536 break;
40537 case PRAGMA_OMP_CLAUSE_IF:
40538 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
40539 true);
40540 c_name = "if";
40541 break;
40542 case PRAGMA_OMP_CLAUSE_IN_REDUCTION:
40543 clauses
40544 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_IN_REDUCTION,
40545 true, clauses);
40546 c_name = "in_reduction";
40547 break;
40548 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
40549 clauses = cp_parser_omp_clause_lastprivate (parser, clauses);
40550 c_name = "lastprivate";
40551 break;
40552 case PRAGMA_OMP_CLAUSE_MERGEABLE:
40553 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
40554 token->location);
40555 c_name = "mergeable";
40556 break;
40557 case PRAGMA_OMP_CLAUSE_NOWAIT:
40558 clauses = cp_parser_omp_clause_nowait (parser, clauses,
40559 token->location);
40560 c_name = "nowait";
40561 break;
40562 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
40563 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
40564 token->location);
40565 c_name = "num_tasks";
40566 break;
40567 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
40568 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
40569 token->location);
40570 c_name = "num_threads";
40571 break;
40572 case PRAGMA_OMP_CLAUSE_ORDER:
40573 clauses = cp_parser_omp_clause_order (parser, clauses,
40574 token->location);
40575 c_name = "order";
40576 break;
40577 case PRAGMA_OMP_CLAUSE_ORDERED:
40578 clauses = cp_parser_omp_clause_ordered (parser, clauses,
40579 token->location);
40580 c_name = "ordered";
40581 break;
40582 case PRAGMA_OMP_CLAUSE_PRIORITY:
40583 clauses = cp_parser_omp_clause_priority (parser, clauses,
40584 token->location);
40585 c_name = "priority";
40586 break;
40587 case PRAGMA_OMP_CLAUSE_PRIVATE:
40588 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
40589 clauses);
40590 c_name = "private";
40591 break;
40592 case PRAGMA_OMP_CLAUSE_REDUCTION:
40593 clauses
40594 = cp_parser_omp_clause_reduction (parser, OMP_CLAUSE_REDUCTION,
40595 true, clauses);
40596 c_name = "reduction";
40597 break;
40598 case PRAGMA_OMP_CLAUSE_SCHEDULE:
40599 clauses = cp_parser_omp_clause_schedule (parser, clauses,
40600 token->location);
40601 c_name = "schedule";
40602 break;
40603 case PRAGMA_OMP_CLAUSE_SHARED:
40604 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
40605 clauses);
40606 c_name = "shared";
40607 break;
40608 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION:
40609 clauses
40610 = cp_parser_omp_clause_reduction (parser,
40611 OMP_CLAUSE_TASK_REDUCTION,
40612 true, clauses);
40613 c_name = "task_reduction";
40614 break;
40615 case PRAGMA_OMP_CLAUSE_UNTIED:
40616 clauses = cp_parser_omp_clause_untied (parser, clauses,
40617 token->location);
40618 c_name = "untied";
40619 break;
40620 case PRAGMA_OMP_CLAUSE_INBRANCH:
40621 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
40622 clauses, token->location);
40623 c_name = "inbranch";
40624 break;
40625 case PRAGMA_OMP_CLAUSE_NONTEMPORAL:
40626 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_NONTEMPORAL,
40627 clauses);
40628 c_name = "nontemporal";
40629 break;
40630 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
40631 clauses = cp_parser_omp_clause_branch (parser,
40632 OMP_CLAUSE_NOTINBRANCH,
40633 clauses, token->location);
40634 c_name = "notinbranch";
40635 break;
40636 case PRAGMA_OMP_CLAUSE_PARALLEL:
40637 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
40638 clauses, token->location);
40639 c_name = "parallel";
40640 if (!first)
40642 clause_not_first:
40643 error_at (token->location, "%qs must be the first clause of %qs",
40644 c_name, where);
40645 clauses = prev;
40647 break;
40648 case PRAGMA_OMP_CLAUSE_FOR:
40649 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
40650 clauses, token->location);
40651 c_name = "for";
40652 if (!first)
40653 goto clause_not_first;
40654 break;
40655 case PRAGMA_OMP_CLAUSE_SECTIONS:
40656 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
40657 clauses, token->location);
40658 c_name = "sections";
40659 if (!first)
40660 goto clause_not_first;
40661 break;
40662 case PRAGMA_OMP_CLAUSE_TASKGROUP:
40663 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
40664 clauses, token->location);
40665 c_name = "taskgroup";
40666 if (!first)
40667 goto clause_not_first;
40668 break;
40669 case PRAGMA_OMP_CLAUSE_LINK:
40670 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
40671 c_name = "link";
40672 break;
40673 case PRAGMA_OMP_CLAUSE_TO:
40674 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
40676 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
40677 clauses);
40678 for (tree c = nl; c != clauses; c = OMP_CLAUSE_CHAIN (c))
40679 OMP_CLAUSE_ENTER_TO (c) = 1;
40680 clauses = nl;
40682 else
40683 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses,
40684 true);
40685 c_name = "to";
40686 break;
40687 case PRAGMA_OMP_CLAUSE_FROM:
40688 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses,
40689 true);
40690 c_name = "from";
40691 break;
40692 case PRAGMA_OMP_CLAUSE_UNIFORM:
40693 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
40694 clauses);
40695 c_name = "uniform";
40696 break;
40697 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
40698 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
40699 token->location);
40700 c_name = "num_teams";
40701 break;
40702 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
40703 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
40704 token->location);
40705 c_name = "thread_limit";
40706 break;
40707 case PRAGMA_OMP_CLAUSE_ALIGNED:
40708 clauses = cp_parser_omp_clause_aligned (parser, clauses);
40709 c_name = "aligned";
40710 break;
40711 case PRAGMA_OMP_CLAUSE_ALLOCATE:
40712 clauses = cp_parser_omp_clause_allocate (parser, clauses);
40713 c_name = "allocate";
40714 break;
40715 case PRAGMA_OMP_CLAUSE_LINEAR:
40717 bool declare_simd = false;
40718 if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
40719 declare_simd = true;
40720 clauses = cp_parser_omp_clause_linear (parser, clauses, declare_simd);
40722 c_name = "linear";
40723 break;
40724 case PRAGMA_OMP_CLAUSE_AFFINITY:
40725 clauses = cp_parser_omp_clause_affinity (parser, clauses);
40726 c_name = "affinity";
40727 break;
40728 case PRAGMA_OMP_CLAUSE_DEPEND:
40729 clauses = cp_parser_omp_clause_depend (parser, clauses,
40730 token->location);
40731 c_name = "depend";
40732 break;
40733 case PRAGMA_OMP_CLAUSE_DOACROSS:
40734 clauses = cp_parser_omp_clause_doacross (parser, clauses,
40735 token->location);
40736 c_name = "doacross";
40737 break;
40738 case PRAGMA_OMP_CLAUSE_DETACH:
40739 clauses = cp_parser_omp_clause_detach (parser, clauses);
40740 c_name = "detach";
40741 break;
40742 case PRAGMA_OMP_CLAUSE_MAP:
40743 clauses = cp_parser_omp_clause_map (parser, clauses);
40744 c_name = "map";
40745 break;
40746 case PRAGMA_OMP_CLAUSE_DEVICE:
40747 clauses = cp_parser_omp_clause_device (parser, clauses,
40748 token->location);
40749 c_name = "device";
40750 break;
40751 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
40752 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
40753 token->location);
40754 c_name = "dist_schedule";
40755 break;
40756 case PRAGMA_OMP_CLAUSE_PROC_BIND:
40757 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
40758 token->location);
40759 c_name = "proc_bind";
40760 break;
40761 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE:
40762 clauses = cp_parser_omp_clause_device_type (parser, clauses,
40763 token->location);
40764 c_name = "device_type";
40765 break;
40766 case PRAGMA_OMP_CLAUSE_SAFELEN:
40767 clauses = cp_parser_omp_clause_safelen (parser, clauses,
40768 token->location);
40769 c_name = "safelen";
40770 break;
40771 case PRAGMA_OMP_CLAUSE_SIMDLEN:
40772 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
40773 token->location);
40774 c_name = "simdlen";
40775 break;
40776 case PRAGMA_OMP_CLAUSE_NOGROUP:
40777 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
40778 token->location);
40779 c_name = "nogroup";
40780 break;
40781 case PRAGMA_OMP_CLAUSE_THREADS:
40782 clauses
40783 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
40784 clauses, token->location);
40785 c_name = "threads";
40786 break;
40787 case PRAGMA_OMP_CLAUSE_SIMD:
40788 clauses
40789 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
40790 clauses, token->location);
40791 c_name = "simd";
40792 break;
40793 case PRAGMA_OMP_CLAUSE_ENTER:
40794 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
40795 clauses);
40796 c_name = "enter";
40797 break;
40798 default:
40799 cp_parser_error (parser, "expected %<#pragma omp%> clause");
40800 goto saw_error;
40803 first = false;
40805 if (((mask >> c_kind) & 1) == 0)
40807 /* Remove the invalid clause(s) from the list to avoid
40808 confusing the rest of the compiler. */
40809 clauses = prev;
40810 error_at (token->location, "%qs is not valid for %qs", c_name, where);
40813 saw_error:
40814 if (!nested)
40815 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
40816 if (finish_p)
40818 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
40819 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
40820 else
40821 return finish_omp_clauses (clauses, C_ORT_OMP);
40823 return clauses;
40826 /* OpenMP 2.5:
40827 structured-block:
40828 statement
40830 In practice, we're also interested in adding the statement to an
40831 outer node. So it is convenient if we work around the fact that
40832 cp_parser_statement calls add_stmt. */
40834 static unsigned
40835 cp_parser_begin_omp_structured_block (cp_parser *parser)
40837 unsigned save = parser->in_statement;
40839 /* Only move the values to IN_OMP_BLOCK if they weren't false.
40840 This preserves the "not within loop or switch" style error messages
40841 for nonsense cases like
40842 void foo() {
40843 #pragma omp single
40844 break;
40847 if (parser->in_statement)
40848 parser->in_statement = IN_OMP_BLOCK;
40850 return save;
40853 static void
40854 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
40856 parser->in_statement = save;
40859 static tree
40860 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
40862 tree stmt = begin_omp_structured_block ();
40863 unsigned int save = cp_parser_begin_omp_structured_block (parser);
40865 parser->omp_attrs_forbidden_p = true;
40866 cp_parser_statement (parser, NULL_TREE, false, if_p);
40868 cp_parser_end_omp_structured_block (parser, save);
40869 return finish_omp_structured_block (stmt);
40872 /* OpenMP 5.0:
40873 # pragma omp allocate (list) [allocator(allocator)] */
40875 static void
40876 cp_parser_omp_allocate (cp_parser *parser, cp_token *pragma_tok)
40878 tree allocator = NULL_TREE;
40879 location_t loc = pragma_tok->location;
40880 tree nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_ALLOCATE, NULL_TREE);
40882 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
40883 if (parser->lexer->in_omp_attribute_pragma
40884 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
40885 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
40886 cp_lexer_consume_token (parser->lexer);
40888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
40890 matching_parens parens;
40891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
40892 const char *p = IDENTIFIER_POINTER (id);
40893 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
40894 cp_lexer_consume_token (parser->lexer);
40895 if (strcmp (p, "allocator") != 0)
40896 error_at (cloc, "expected %<allocator%>");
40897 else if (parens.require_open (parser))
40899 allocator = cp_parser_assignment_expression (parser);
40900 if (allocator == error_mark_node)
40901 allocator = NULL_TREE;
40902 parens.require_close (parser);
40905 cp_parser_require_pragma_eol (parser, pragma_tok);
40907 if (allocator)
40908 for (tree c = nl; c != NULL_TREE; c = OMP_CLAUSE_CHAIN (c))
40909 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c) = allocator;
40911 sorry_at (loc, "%<#pragma omp allocate%> not yet supported");
40914 /* OpenMP 2.5:
40915 # pragma omp atomic new-line
40916 expression-stmt
40918 expression-stmt:
40919 x binop= expr | x++ | ++x | x-- | --x
40920 binop:
40921 +, *, -, /, &, ^, |, <<, >>
40923 where x is an lvalue expression with scalar type.
40925 OpenMP 3.1:
40926 # pragma omp atomic new-line
40927 update-stmt
40929 # pragma omp atomic read new-line
40930 read-stmt
40932 # pragma omp atomic write new-line
40933 write-stmt
40935 # pragma omp atomic update new-line
40936 update-stmt
40938 # pragma omp atomic capture new-line
40939 capture-stmt
40941 # pragma omp atomic capture new-line
40942 capture-block
40944 read-stmt:
40945 v = x
40946 write-stmt:
40947 x = expr
40948 update-stmt:
40949 expression-stmt | x = x binop expr
40950 capture-stmt:
40951 v = expression-stmt
40952 capture-block:
40953 { v = x; update-stmt; } | { update-stmt; v = x; }
40955 OpenMP 4.0:
40956 update-stmt:
40957 expression-stmt | x = x binop expr | x = expr binop x
40958 capture-stmt:
40959 v = update-stmt
40960 capture-block:
40961 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
40963 OpenMP 5.1:
40964 # pragma omp atomic compare new-line
40965 conditional-update-atomic
40967 # pragma omp atomic compare capture new-line
40968 conditional-update-capture-atomic
40970 conditional-update-atomic:
40971 cond-expr-stmt | cond-update-stmt
40972 cond-expr-stmt:
40973 x = expr ordop x ? expr : x;
40974 x = x ordop expr ? expr : x;
40975 x = x == e ? d : x;
40976 cond-update-stmt:
40977 if (expr ordop x) { x = expr; }
40978 if (x ordop expr) { x = expr; }
40979 if (x == e) { x = d; }
40980 ordop:
40981 <, >
40982 conditional-update-capture-atomic:
40983 v = cond-expr-stmt
40984 { v = x; cond-expr-stmt }
40985 { cond-expr-stmt v = x; }
40986 { v = x; cond-update-stmt }
40987 { cond-update-stmt v = x; }
40988 if (x == e) { x = d; } else { v = x; }
40989 { r = x == e; if (r) { x = d; } }
40990 { r = x == e; if (r) { x = d; } else { v = x; } }
40992 where x, r and v are lvalue expressions with scalar type,
40993 expr, e and d are expressions with scalar type and e might be
40994 the same as v. */
40996 static void
40997 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok, bool openacc)
40999 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
41000 tree rhs1 = NULL_TREE, orig_lhs, r = NULL_TREE;
41001 location_t loc = pragma_tok->location;
41002 enum tree_code code = ERROR_MARK, opcode = NOP_EXPR;
41003 enum omp_memory_order memory_order = OMP_MEMORY_ORDER_UNSPECIFIED;
41004 bool structured_block = false;
41005 bool first = true;
41006 tree clauses = NULL_TREE;
41007 bool capture = false;
41008 bool compare = false;
41009 bool weak = false;
41010 enum omp_memory_order fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41011 bool no_semicolon = false;
41012 bool extra_scope = false;
41014 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
41016 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41017 if ((!first || parser->lexer->in_omp_attribute_pragma)
41018 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41019 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41020 cp_lexer_consume_token (parser->lexer);
41022 first = false;
41024 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41026 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41027 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
41028 const char *p = IDENTIFIER_POINTER (id);
41029 enum tree_code new_code = ERROR_MARK;
41030 enum omp_memory_order new_memory_order
41031 = OMP_MEMORY_ORDER_UNSPECIFIED;
41032 bool new_capture = false;
41033 bool new_compare = false;
41034 bool new_weak = false;
41035 enum omp_memory_order new_fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41037 if (!strcmp (p, "read"))
41038 new_code = OMP_ATOMIC_READ;
41039 else if (!strcmp (p, "write"))
41040 new_code = NOP_EXPR;
41041 else if (!strcmp (p, "update"))
41042 new_code = OMP_ATOMIC;
41043 else if (openacc && !strcmp (p, "capture"))
41044 new_code = OMP_ATOMIC_CAPTURE_NEW;
41045 else if (openacc)
41047 p = NULL;
41048 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41049 "or %<capture%> clause");
41051 else if (!strcmp (p, "capture"))
41052 new_capture = true;
41053 else if (!strcmp (p, "compare"))
41054 new_compare = true;
41055 else if (!strcmp (p, "weak"))
41056 new_weak = true;
41057 else if (!strcmp (p, "fail"))
41059 matching_parens parens;
41061 cp_lexer_consume_token (parser->lexer);
41062 if (!parens.require_open (parser))
41063 continue;
41065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41067 id = cp_lexer_peek_token (parser->lexer)->u.value;
41068 const char *q = IDENTIFIER_POINTER (id);
41070 if (!strcmp (q, "seq_cst"))
41071 new_fail = OMP_MEMORY_ORDER_SEQ_CST;
41072 else if (!strcmp (q, "acquire"))
41073 new_fail = OMP_MEMORY_ORDER_ACQUIRE;
41074 else if (!strcmp (q, "relaxed"))
41075 new_fail = OMP_MEMORY_ORDER_RELAXED;
41078 if (new_fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41080 cp_lexer_consume_token (parser->lexer);
41081 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41082 error_at (cloc, "too many %qs clauses", "fail");
41083 else
41084 fail = new_fail;
41086 else
41087 cp_parser_error (parser, "expected %<seq_cst%>, %<acquire%> "
41088 "or %<relaxed%>");
41089 if (new_fail == OMP_MEMORY_ORDER_UNSPECIFIED
41090 || !parens.require_close (parser))
41091 cp_parser_skip_to_closing_parenthesis (parser,
41092 /*recovering=*/true,
41093 /*or_comma=*/false,
41094 /*consume_paren=*/true);
41095 continue;
41097 else if (!strcmp (p, "seq_cst"))
41098 new_memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41099 else if (!strcmp (p, "acq_rel"))
41100 new_memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41101 else if (!strcmp (p, "release"))
41102 new_memory_order = OMP_MEMORY_ORDER_RELEASE;
41103 else if (!strcmp (p, "acquire"))
41104 new_memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41105 else if (!strcmp (p, "relaxed"))
41106 new_memory_order = OMP_MEMORY_ORDER_RELAXED;
41107 else if (!strcmp (p, "hint"))
41109 cp_lexer_consume_token (parser->lexer);
41110 clauses = cp_parser_omp_clause_hint (parser, clauses, cloc);
41111 continue;
41113 else
41115 p = NULL;
41116 error_at (cloc, "expected %<read%>, %<write%>, %<update%>, "
41117 "%<capture%>, %<compare%>, %<weak%>, %<fail%>, "
41118 "%<seq_cst%>, %<acq_rel%>, %<release%>, "
41119 "%<relaxed%> or %<hint%> clause");
41121 if (p)
41123 if (new_code != ERROR_MARK)
41125 /* OpenACC permits 'update capture'. */
41126 if (openacc
41127 && code == OMP_ATOMIC
41128 && new_code == OMP_ATOMIC_CAPTURE_NEW)
41129 code = new_code;
41130 else if (code != ERROR_MARK)
41131 error_at (cloc, "too many atomic clauses");
41132 else
41133 code = new_code;
41135 else if (new_memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41137 if (memory_order != OMP_MEMORY_ORDER_UNSPECIFIED)
41138 error_at (cloc, "too many memory order clauses");
41139 else
41140 memory_order = new_memory_order;
41142 else if (new_capture)
41144 if (capture)
41145 error_at (cloc, "too many %qs clauses", "capture");
41146 else
41147 capture = true;
41149 else if (new_compare)
41151 if (compare)
41152 error_at (cloc, "too many %qs clauses", "compare");
41153 else
41154 compare = true;
41156 else if (new_weak)
41158 if (weak)
41159 error_at (cloc, "too many %qs clauses", "weak");
41160 else
41161 weak = true;
41163 cp_lexer_consume_token (parser->lexer);
41164 continue;
41167 break;
41169 cp_parser_require_pragma_eol (parser, pragma_tok);
41171 if (code == ERROR_MARK)
41172 code = OMP_ATOMIC;
41173 if (capture)
41175 if (code != OMP_ATOMIC)
41176 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41177 "clauses", "capture");
41178 else
41179 code = OMP_ATOMIC_CAPTURE_NEW;
41181 if (compare && code != OMP_ATOMIC && code != OMP_ATOMIC_CAPTURE_NEW)
41183 error_at (loc, "%qs clause is incompatible with %<read%> or %<write%> "
41184 "clauses", "compare");
41185 compare = false;
41187 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED && !compare)
41189 error_at (loc, "%qs clause requires %qs clause", "fail", "compare");
41190 fail = OMP_MEMORY_ORDER_UNSPECIFIED;
41192 if (weak && !compare)
41194 error_at (loc, "%qs clause requires %qs clause", "weak", "compare");
41195 weak = false;
41197 if (openacc)
41198 memory_order = OMP_MEMORY_ORDER_RELAXED;
41199 else if (memory_order == OMP_MEMORY_ORDER_UNSPECIFIED)
41201 omp_requires_mask
41202 = (enum omp_requires) (omp_requires_mask
41203 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED);
41204 switch ((enum omp_memory_order)
41205 (omp_requires_mask & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER))
41207 case OMP_MEMORY_ORDER_UNSPECIFIED:
41208 case OMP_MEMORY_ORDER_RELAXED:
41209 memory_order = OMP_MEMORY_ORDER_RELAXED;
41210 break;
41211 case OMP_MEMORY_ORDER_SEQ_CST:
41212 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41213 break;
41214 case OMP_MEMORY_ORDER_ACQ_REL:
41215 switch (code)
41217 case OMP_ATOMIC_READ:
41218 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41219 break;
41220 case NOP_EXPR: /* atomic write */
41221 memory_order = OMP_MEMORY_ORDER_RELEASE;
41222 break;
41223 default:
41224 memory_order = OMP_MEMORY_ORDER_ACQ_REL;
41225 break;
41227 break;
41228 default:
41229 gcc_unreachable ();
41232 else
41233 switch (code)
41235 case OMP_ATOMIC_READ:
41236 if (memory_order == OMP_MEMORY_ORDER_RELEASE)
41238 error_at (loc, "%<#pragma omp atomic read%> incompatible with "
41239 "%<release%> clause");
41240 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41242 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41243 memory_order = OMP_MEMORY_ORDER_ACQUIRE;
41244 break;
41245 case NOP_EXPR: /* atomic write */
41246 if (memory_order == OMP_MEMORY_ORDER_ACQUIRE)
41248 error_at (loc, "%<#pragma omp atomic write%> incompatible with "
41249 "%<acquire%> clause");
41250 memory_order = OMP_MEMORY_ORDER_SEQ_CST;
41252 else if (memory_order == OMP_MEMORY_ORDER_ACQ_REL)
41253 memory_order = OMP_MEMORY_ORDER_RELEASE;
41254 break;
41255 default:
41256 break;
41258 if (fail != OMP_MEMORY_ORDER_UNSPECIFIED)
41259 memory_order
41260 = (enum omp_memory_order) (memory_order
41261 | (fail << OMP_FAIL_MEMORY_ORDER_SHIFT));
41263 switch (code)
41265 case OMP_ATOMIC_READ:
41266 case NOP_EXPR: /* atomic write */
41267 v = cp_parser_unary_expression (parser);
41268 if (v == error_mark_node)
41269 goto saw_error;
41270 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41271 goto saw_error;
41272 if (code == NOP_EXPR)
41273 lhs = cp_parser_expression (parser);
41274 else
41275 lhs = cp_parser_unary_expression (parser);
41276 if (lhs == error_mark_node)
41277 goto saw_error;
41278 if (code == NOP_EXPR)
41280 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
41281 opcode. */
41282 code = OMP_ATOMIC;
41283 rhs = lhs;
41284 lhs = v;
41285 v = NULL_TREE;
41287 goto done;
41288 case OMP_ATOMIC_CAPTURE_NEW:
41289 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
41291 cp_lexer_consume_token (parser->lexer);
41292 structured_block = true;
41294 else if (compare
41295 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41296 break;
41297 else
41299 v = cp_parser_unary_expression (parser);
41300 if (v == error_mark_node)
41301 goto saw_error;
41302 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41303 goto saw_error;
41304 if (compare
41305 && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41307 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41308 error_at (eloc, "expected expression");
41309 goto saw_error;
41312 default:
41313 break;
41316 restart:
41317 if (compare && cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
41319 cp_lexer_consume_token (parser->lexer);
41321 matching_parens parens;
41322 if (!parens.require_open (parser))
41323 goto saw_error;
41324 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
41325 tree cmp_expr;
41326 if (r)
41327 cmp_expr = cp_parser_unary_expression (parser);
41328 else
41329 cmp_expr = cp_parser_binary_expression (parser, false, true,
41330 PREC_NOT_OPERATOR, NULL);
41331 if (!parens.require_close (parser))
41332 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
41333 if (cmp_expr == error_mark_node)
41334 goto saw_error;
41335 if (r)
41337 if (!cp_tree_equal (cmp_expr, r))
41338 goto bad_if;
41339 cmp_expr = rhs;
41340 rhs = NULL_TREE;
41341 gcc_assert (TREE_CODE (cmp_expr) == EQ_EXPR);
41343 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41345 else if (!structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41347 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41348 "expected %<==%> comparison in %<if%> condition");
41349 goto saw_error;
41351 else if (TREE_CODE (cmp_expr) != GT_EXPR
41352 && TREE_CODE (cmp_expr) != LT_EXPR)
41354 error_at (EXPR_LOC_OR_LOC (cmp_expr, eloc),
41355 "expected %<==%>, %<<%> or %<>%> comparison in %<if%> "
41356 "condition");
41357 goto saw_error;
41359 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41360 goto saw_error;
41362 extra_scope = true;
41363 eloc = cp_lexer_peek_token (parser->lexer)->location;
41364 lhs = cp_parser_unary_expression (parser);
41365 orig_lhs = lhs;
41366 if (lhs == error_mark_node)
41367 goto saw_error;
41368 if (!cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41370 cp_parser_error (parser, "expected %<=%>");
41371 goto saw_error;
41373 cp_lexer_consume_token (parser->lexer);
41374 eloc = cp_lexer_peek_token (parser->lexer)->location;
41375 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41376 rhs1 = cp_parser_expression (parser);
41377 else
41378 rhs1 = cp_parser_simple_cast_expression (parser);
41380 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41381 goto saw_error;
41383 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41384 goto saw_error;
41386 extra_scope = false;
41387 no_semicolon = true;
41389 if (cp_tree_equal (TREE_OPERAND (cmp_expr, 0), lhs))
41391 if (TREE_CODE (cmp_expr) == EQ_EXPR)
41393 opcode = COND_EXPR;
41394 rhs = TREE_OPERAND (cmp_expr, 1);
41396 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), rhs1))
41398 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41399 ? MIN_EXPR : MAX_EXPR);
41400 rhs = rhs1;
41401 rhs1 = TREE_OPERAND (cmp_expr, 0);
41403 else
41404 goto bad_if;
41406 else if (TREE_CODE (cmp_expr) == EQ_EXPR)
41407 goto bad_if;
41408 else if (cp_tree_equal (TREE_OPERAND (cmp_expr, 1), lhs)
41409 && cp_tree_equal (TREE_OPERAND (cmp_expr, 0), rhs1))
41411 opcode = (TREE_CODE (cmp_expr) == GT_EXPR
41412 ? MAX_EXPR : MIN_EXPR);
41413 rhs = rhs1;
41414 rhs1 = TREE_OPERAND (cmp_expr, 1);
41416 else
41418 bad_if:
41419 cp_parser_error (parser,
41420 "invalid form of %<#pragma omp atomic compare%>");
41421 goto saw_error;
41424 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
41426 if (code != OMP_ATOMIC_CAPTURE_NEW
41427 || (structured_block && r == NULL_TREE)
41428 || TREE_CODE (cmp_expr) != EQ_EXPR)
41430 eloc = cp_lexer_peek_token (parser->lexer)->location;
41431 error_at (eloc, "unexpected %<else%>");
41432 goto saw_error;
41435 cp_lexer_consume_token (parser->lexer);
41437 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
41438 goto saw_error;
41440 extra_scope = true;
41441 v = cp_parser_unary_expression (parser);
41442 if (v == error_mark_node)
41443 goto saw_error;
41444 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41445 goto saw_error;
41447 tree expr = cp_parser_simple_cast_expression (parser);
41449 if (!cp_tree_equal (expr, lhs))
41450 goto bad_if;
41452 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41453 goto saw_error;
41455 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
41456 goto saw_error;
41458 extra_scope = false;
41459 code = OMP_ATOMIC_CAPTURE_OLD;
41460 if (r == NULL_TREE)
41461 /* Signal to c_finish_omp_atomic that in
41462 if (x == e) { x = d; } else { v = x; }
41463 case the store to v should be conditional. */
41464 r = void_list_node;
41466 else if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41468 cp_parser_error (parser, "expected %<else%>");
41469 goto saw_error;
41471 else if (code == OMP_ATOMIC_CAPTURE_NEW
41472 && r != NULL_TREE
41473 && v == NULL_TREE)
41474 code = OMP_ATOMIC;
41475 goto stmt_done;
41477 lhs = cp_parser_unary_expression (parser);
41478 orig_lhs = lhs;
41479 switch (TREE_CODE (lhs))
41481 case ERROR_MARK:
41482 goto saw_error;
41484 case POSTINCREMENT_EXPR:
41485 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41486 code = OMP_ATOMIC_CAPTURE_OLD;
41487 /* FALLTHROUGH */
41488 case PREINCREMENT_EXPR:
41489 lhs = TREE_OPERAND (lhs, 0);
41490 opcode = PLUS_EXPR;
41491 rhs = integer_one_node;
41492 if (compare)
41493 goto invalid_compare;
41494 break;
41496 case POSTDECREMENT_EXPR:
41497 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
41498 code = OMP_ATOMIC_CAPTURE_OLD;
41499 /* FALLTHROUGH */
41500 case PREDECREMENT_EXPR:
41501 lhs = TREE_OPERAND (lhs, 0);
41502 opcode = MINUS_EXPR;
41503 rhs = integer_one_node;
41504 if (compare)
41505 goto invalid_compare;
41506 break;
41508 case COMPOUND_EXPR:
41509 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
41510 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
41511 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
41512 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
41513 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
41514 (TREE_OPERAND (lhs, 1), 0), 0)))
41515 == BOOLEAN_TYPE)
41516 /* Undo effects of boolean_increment for post {in,de}crement. */
41517 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
41518 /* FALLTHRU */
41519 case MODIFY_EXPR:
41520 if (TREE_CODE (lhs) == MODIFY_EXPR
41521 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
41523 /* Undo effects of boolean_increment. */
41524 if (integer_onep (TREE_OPERAND (lhs, 1)))
41526 /* This is pre or post increment. */
41527 rhs = TREE_OPERAND (lhs, 1);
41528 lhs = TREE_OPERAND (lhs, 0);
41529 opcode = NOP_EXPR;
41530 if (code == OMP_ATOMIC_CAPTURE_NEW
41531 && !structured_block
41532 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
41533 code = OMP_ATOMIC_CAPTURE_OLD;
41534 if (compare)
41535 goto invalid_compare;
41536 break;
41539 /* FALLTHRU */
41540 default:
41541 if (compare && !cp_lexer_next_token_is (parser->lexer, CPP_EQ))
41543 cp_parser_error (parser, "expected %<=%>");
41544 goto saw_error;
41546 switch (cp_lexer_peek_token (parser->lexer)->type)
41548 case CPP_MULT_EQ:
41549 opcode = MULT_EXPR;
41550 break;
41551 case CPP_DIV_EQ:
41552 opcode = TRUNC_DIV_EXPR;
41553 break;
41554 case CPP_PLUS_EQ:
41555 opcode = PLUS_EXPR;
41556 break;
41557 case CPP_MINUS_EQ:
41558 opcode = MINUS_EXPR;
41559 break;
41560 case CPP_LSHIFT_EQ:
41561 opcode = LSHIFT_EXPR;
41562 break;
41563 case CPP_RSHIFT_EQ:
41564 opcode = RSHIFT_EXPR;
41565 break;
41566 case CPP_AND_EQ:
41567 opcode = BIT_AND_EXPR;
41568 break;
41569 case CPP_OR_EQ:
41570 opcode = BIT_IOR_EXPR;
41571 break;
41572 case CPP_XOR_EQ:
41573 opcode = BIT_XOR_EXPR;
41574 break;
41575 case CPP_EQ:
41576 enum cp_parser_prec oprec;
41577 cp_token *token;
41578 cp_lexer_consume_token (parser->lexer);
41579 cp_parser_parse_tentatively (parser);
41580 rhs1 = cp_parser_simple_cast_expression (parser);
41581 if (rhs1 == error_mark_node)
41583 cp_parser_abort_tentative_parse (parser);
41584 cp_parser_simple_cast_expression (parser);
41585 goto saw_error;
41587 token = cp_lexer_peek_token (parser->lexer);
41588 if (token->type != CPP_SEMICOLON
41589 && (!compare || token->type != CPP_QUERY)
41590 && !cp_tree_equal (lhs, rhs1))
41592 cp_parser_abort_tentative_parse (parser);
41593 cp_parser_parse_tentatively (parser);
41594 rhs = cp_parser_binary_expression (parser, false, true,
41595 PREC_NOT_OPERATOR, NULL);
41596 if (rhs == error_mark_node)
41598 cp_parser_abort_tentative_parse (parser);
41599 cp_parser_binary_expression (parser, false, true,
41600 PREC_NOT_OPERATOR, NULL);
41601 goto saw_error;
41603 switch (TREE_CODE (rhs))
41605 case MULT_EXPR:
41606 case TRUNC_DIV_EXPR:
41607 case RDIV_EXPR:
41608 case PLUS_EXPR:
41609 case MINUS_EXPR:
41610 case LSHIFT_EXPR:
41611 case RSHIFT_EXPR:
41612 case BIT_AND_EXPR:
41613 case BIT_IOR_EXPR:
41614 case BIT_XOR_EXPR:
41615 if (compare)
41616 break;
41617 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
41619 if (cp_parser_parse_definitely (parser))
41621 opcode = TREE_CODE (rhs);
41622 rhs1 = TREE_OPERAND (rhs, 0);
41623 rhs = TREE_OPERAND (rhs, 1);
41624 goto stmt_done;
41626 else
41627 goto saw_error;
41629 break;
41630 case EQ_EXPR:
41631 if (!compare
41632 || code != OMP_ATOMIC_CAPTURE_NEW
41633 || !structured_block
41634 || v
41635 || r)
41636 break;
41637 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
41638 && cp_lexer_nth_token_is_keyword (parser->lexer,
41639 2, RID_IF))
41641 if (cp_parser_parse_definitely (parser))
41643 r = lhs;
41644 lhs = NULL_TREE;
41645 rhs1 = NULL_TREE;
41646 cp_lexer_consume_token (parser->lexer);
41647 goto restart;
41650 break;
41651 case GT_EXPR:
41652 case LT_EXPR:
41653 if (compare
41654 && cp_lexer_next_token_is (parser->lexer, CPP_QUERY)
41655 && cp_tree_equal (lhs, TREE_OPERAND (rhs, 1))
41656 && cp_parser_parse_definitely (parser))
41658 opcode = TREE_CODE (rhs);
41659 rhs1 = TREE_OPERAND (rhs, 0);
41660 rhs = TREE_OPERAND (rhs, 1);
41661 cond_expr:
41662 cp_lexer_consume_token (parser->lexer);
41663 bool saved_colon_corrects_to_scope_p
41664 = parser->colon_corrects_to_scope_p;
41665 parser->colon_corrects_to_scope_p = false;
41666 tree e1 = cp_parser_expression (parser);
41667 parser->colon_corrects_to_scope_p
41668 = saved_colon_corrects_to_scope_p;
41669 cp_parser_require (parser, CPP_COLON, RT_COLON);
41670 tree e2 = cp_parser_simple_cast_expression (parser);
41671 if (cp_tree_equal (lhs, e2))
41673 if (cp_tree_equal (lhs, rhs1))
41675 if (opcode == EQ_EXPR)
41677 opcode = COND_EXPR;
41678 rhs1 = e1;
41679 goto stmt_done;
41681 if (cp_tree_equal (rhs, e1))
41683 opcode
41684 = opcode == GT_EXPR ? MIN_EXPR : MAX_EXPR;
41685 rhs = e1;
41686 goto stmt_done;
41689 else
41691 gcc_assert (opcode != EQ_EXPR);
41692 if (cp_tree_equal (rhs1, e1))
41694 opcode
41695 = opcode == GT_EXPR ? MAX_EXPR : MIN_EXPR;
41696 rhs1 = rhs;
41697 rhs = e1;
41698 goto stmt_done;
41702 cp_parser_error (parser,
41703 "invalid form of "
41704 "%<#pragma omp atomic compare%>");
41705 goto saw_error;
41707 break;
41708 default:
41709 break;
41711 cp_parser_abort_tentative_parse (parser);
41712 if (structured_block
41713 && code == OMP_ATOMIC_CAPTURE_OLD
41714 && !compare)
41716 rhs = cp_parser_expression (parser);
41717 if (rhs == error_mark_node)
41718 goto saw_error;
41719 opcode = NOP_EXPR;
41720 rhs1 = NULL_TREE;
41721 goto stmt_done;
41723 cp_parser_error (parser,
41724 "invalid form of %<#pragma omp atomic%>");
41725 goto saw_error;
41727 if (!cp_parser_parse_definitely (parser))
41728 goto saw_error;
41729 switch (token->type)
41731 case CPP_SEMICOLON:
41732 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
41734 code = OMP_ATOMIC_CAPTURE_OLD;
41735 v = lhs;
41736 lhs = NULL_TREE;
41737 lhs1 = rhs1;
41738 rhs1 = NULL_TREE;
41739 cp_lexer_consume_token (parser->lexer);
41740 goto restart;
41742 else if (structured_block && !compare)
41744 opcode = NOP_EXPR;
41745 rhs = rhs1;
41746 rhs1 = NULL_TREE;
41747 goto stmt_done;
41749 cp_parser_error (parser,
41750 "invalid form of %<#pragma omp atomic%>");
41751 goto saw_error;
41752 case CPP_MULT:
41753 opcode = MULT_EXPR;
41754 break;
41755 case CPP_DIV:
41756 opcode = TRUNC_DIV_EXPR;
41757 break;
41758 case CPP_PLUS:
41759 opcode = PLUS_EXPR;
41760 break;
41761 case CPP_MINUS:
41762 opcode = MINUS_EXPR;
41763 break;
41764 case CPP_LSHIFT:
41765 opcode = LSHIFT_EXPR;
41766 break;
41767 case CPP_RSHIFT:
41768 opcode = RSHIFT_EXPR;
41769 break;
41770 case CPP_AND:
41771 opcode = BIT_AND_EXPR;
41772 break;
41773 case CPP_OR:
41774 opcode = BIT_IOR_EXPR;
41775 break;
41776 case CPP_XOR:
41777 opcode = BIT_XOR_EXPR;
41778 break;
41779 case CPP_EQ_EQ:
41780 opcode = EQ_EXPR;
41781 break;
41782 case CPP_GREATER:
41783 opcode = GT_EXPR;
41784 break;
41785 case CPP_LESS:
41786 opcode = LT_EXPR;
41787 break;
41788 default:
41789 cp_parser_error (parser,
41790 "invalid operator for %<#pragma omp atomic%>");
41791 goto saw_error;
41793 if (compare
41794 && TREE_CODE_CLASS (opcode) != tcc_comparison)
41796 cp_parser_error (parser,
41797 "invalid form of "
41798 "%<#pragma omp atomic compare%>");
41799 goto saw_error;
41801 oprec = TOKEN_PRECEDENCE (token);
41802 gcc_assert (oprec != PREC_NOT_OPERATOR);
41803 if (commutative_tree_code (opcode))
41804 oprec = (enum cp_parser_prec) (oprec - 1);
41805 cp_lexer_consume_token (parser->lexer);
41806 rhs = cp_parser_binary_expression (parser, false, false,
41807 oprec, NULL);
41808 if (rhs == error_mark_node)
41809 goto saw_error;
41810 if (compare)
41812 if (!cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
41814 cp_parser_error (parser,
41815 "invalid form of "
41816 "%<#pragma omp atomic compare%>");
41817 goto saw_error;
41819 goto cond_expr;
41821 goto stmt_done;
41822 default:
41823 cp_parser_error (parser,
41824 "invalid operator for %<#pragma omp atomic%>");
41825 goto saw_error;
41827 cp_lexer_consume_token (parser->lexer);
41829 rhs = cp_parser_expression (parser);
41830 if (rhs == error_mark_node)
41831 goto saw_error;
41832 break;
41834 stmt_done:
41835 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW && r == NULL_TREE)
41837 if (!no_semicolon
41838 && !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
41839 goto saw_error;
41840 no_semicolon = false;
41841 v = cp_parser_unary_expression (parser);
41842 if (v == error_mark_node)
41843 goto saw_error;
41844 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
41845 goto saw_error;
41846 lhs1 = cp_parser_unary_expression (parser);
41847 if (lhs1 == error_mark_node)
41848 goto saw_error;
41850 if (structured_block)
41852 if (!no_semicolon)
41853 cp_parser_consume_semicolon_at_end_of_statement (parser);
41854 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
41856 done:
41857 if (weak && opcode != COND_EXPR)
41859 error_at (loc, "%<weak%> clause requires atomic equality comparison");
41860 weak = false;
41862 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
41863 finish_omp_atomic (pragma_tok->location, code, opcode, lhs, rhs, v, lhs1,
41864 rhs1, r, clauses, memory_order, weak);
41865 if (!structured_block && !no_semicolon)
41866 cp_parser_consume_semicolon_at_end_of_statement (parser);
41867 return;
41869 invalid_compare:
41870 error ("invalid form of %<pragma omp atomic compare%>");
41871 /* FALLTHRU */
41872 saw_error:
41873 cp_parser_skip_to_end_of_block_or_statement (parser);
41874 if (extra_scope && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41875 cp_lexer_consume_token (parser->lexer);
41876 if (structured_block)
41878 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41879 cp_lexer_consume_token (parser->lexer);
41880 else if (code == OMP_ATOMIC_CAPTURE_NEW)
41882 cp_parser_skip_to_end_of_block_or_statement (parser);
41883 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
41884 cp_lexer_consume_token (parser->lexer);
41890 /* OpenMP 2.5:
41891 # pragma omp barrier new-line */
41893 static void
41894 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
41896 cp_parser_require_pragma_eol (parser, pragma_tok);
41897 finish_omp_barrier ();
41900 /* OpenMP 2.5:
41901 # pragma omp critical [(name)] new-line
41902 structured-block
41904 OpenMP 4.5:
41905 # pragma omp critical [(name) [hint(expression)]] new-line
41906 structured-block */
41908 #define OMP_CRITICAL_CLAUSE_MASK \
41909 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
41911 static tree
41912 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
41914 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
41916 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
41918 matching_parens parens;
41919 parens.consume_open (parser);
41921 name = cp_parser_identifier (parser);
41923 if (name == error_mark_node
41924 || !parens.require_close (parser))
41925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41926 /*or_comma=*/false,
41927 /*consume_paren=*/true);
41928 if (name == error_mark_node)
41929 name = NULL;
41931 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
41932 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
41933 cp_lexer_consume_token (parser->lexer);
41936 clauses = cp_parser_omp_all_clauses (parser, OMP_CRITICAL_CLAUSE_MASK,
41937 "#pragma omp critical", pragma_tok);
41939 stmt = cp_parser_omp_structured_block (parser, if_p);
41940 return c_finish_omp_critical (input_location, stmt, name, clauses);
41943 /* OpenMP 5.0:
41944 # pragma omp depobj ( depobj ) depobj-clause new-line
41946 depobj-clause:
41947 depend (dependence-type : locator)
41948 destroy
41949 update (dependence-type)
41951 dependence-type:
41954 inout
41955 mutexinout */
41957 static void
41958 cp_parser_omp_depobj (cp_parser *parser, cp_token *pragma_tok)
41960 location_t loc = pragma_tok->location;
41961 matching_parens parens;
41962 if (!parens.require_open (parser))
41964 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
41965 return;
41968 tree depobj = cp_parser_assignment_expression (parser);
41970 if (!parens.require_close (parser))
41971 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
41972 /*or_comma=*/false,
41973 /*consume_paren=*/true);
41975 tree clause = NULL_TREE;
41976 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INVALID;
41977 location_t c_loc = cp_lexer_peek_token (parser->lexer)->location;
41978 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
41979 if (parser->lexer->in_omp_attribute_pragma
41980 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
41981 cp_lexer_consume_token (parser->lexer);
41982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
41984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
41985 const char *p = IDENTIFIER_POINTER (id);
41987 cp_lexer_consume_token (parser->lexer);
41988 if (!strcmp ("depend", p))
41990 /* Don't create location wrapper nodes within the depend clause. */
41991 auto_suppress_location_wrappers sentinel;
41992 clause = cp_parser_omp_clause_depend (parser, NULL_TREE, c_loc);
41993 if (clause)
41994 clause = finish_omp_clauses (clause, C_ORT_OMP);
41995 if (!clause)
41996 clause = error_mark_node;
41998 else if (!strcmp ("destroy", p))
41999 kind = OMP_CLAUSE_DEPEND_LAST;
42000 else if (!strcmp ("update", p))
42002 matching_parens c_parens;
42003 if (c_parens.require_open (parser))
42005 location_t c2_loc
42006 = cp_lexer_peek_token (parser->lexer)->location;
42007 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42009 tree id2 = cp_lexer_peek_token (parser->lexer)->u.value;
42010 const char *p2 = IDENTIFIER_POINTER (id2);
42012 cp_lexer_consume_token (parser->lexer);
42013 if (!strcmp ("in", p2))
42014 kind = OMP_CLAUSE_DEPEND_IN;
42015 else if (!strcmp ("out", p2))
42016 kind = OMP_CLAUSE_DEPEND_OUT;
42017 else if (!strcmp ("inout", p2))
42018 kind = OMP_CLAUSE_DEPEND_INOUT;
42019 else if (!strcmp ("mutexinoutset", p2))
42020 kind = OMP_CLAUSE_DEPEND_MUTEXINOUTSET;
42021 else if (!strcmp ("inoutset", p2))
42022 kind = OMP_CLAUSE_DEPEND_INOUTSET;
42024 if (kind == OMP_CLAUSE_DEPEND_INVALID)
42026 clause = error_mark_node;
42027 error_at (c2_loc, "expected %<in%>, %<out%>, %<inout%>, "
42028 "%<mutexinoutset%> or %<inoutset%>");
42030 if (!c_parens.require_close (parser))
42031 cp_parser_skip_to_closing_parenthesis (parser,
42032 /*recovering=*/true,
42033 /*or_comma=*/false,
42034 /*consume_paren=*/true);
42036 else
42037 clause = error_mark_node;
42040 if (!clause && kind == OMP_CLAUSE_DEPEND_INVALID)
42042 clause = error_mark_node;
42043 error_at (c_loc, "expected %<depend%>, %<destroy%> or %<update%> clause");
42045 cp_parser_require_pragma_eol (parser, pragma_tok);
42047 finish_omp_depobj (loc, depobj, kind, clause);
42051 /* OpenMP 2.5:
42052 # pragma omp flush flush-vars[opt] new-line
42054 flush-vars:
42055 ( variable-list )
42057 OpenMP 5.0:
42058 # pragma omp flush memory-order-clause new-line */
42060 static void
42061 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
42063 enum memmodel mo = MEMMODEL_LAST;
42064 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
42065 if (parser->lexer->in_omp_attribute_pragma
42066 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
42067 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
42068 cp_lexer_consume_token (parser->lexer);
42069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42072 const char *p = IDENTIFIER_POINTER (id);
42073 if (!strcmp (p, "seq_cst"))
42074 mo = MEMMODEL_SEQ_CST;
42075 else if (!strcmp (p, "acq_rel"))
42076 mo = MEMMODEL_ACQ_REL;
42077 else if (!strcmp (p, "release"))
42078 mo = MEMMODEL_RELEASE;
42079 else if (!strcmp (p, "acquire"))
42080 mo = MEMMODEL_ACQUIRE;
42081 else
42082 error_at (cp_lexer_peek_token (parser->lexer)->location,
42083 "expected %<seq_cst%>, %<acq_rel%>, %<release%> or "
42084 "%<acquire%>");
42085 cp_lexer_consume_token (parser->lexer);
42087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
42089 if (mo != MEMMODEL_LAST)
42090 error_at (cp_lexer_peek_token (parser->lexer)->location,
42091 "%<flush%> list specified together with memory order "
42092 "clause");
42093 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
42095 cp_parser_require_pragma_eol (parser, pragma_tok);
42097 finish_omp_flush (mo);
42100 /* Helper function, to parse omp for increment expression. */
42102 static tree
42103 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
42105 tree cond = cp_parser_binary_expression (parser, false, true,
42106 PREC_NOT_OPERATOR, NULL);
42107 if (cond == error_mark_node
42108 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
42110 cp_parser_skip_to_end_of_statement (parser);
42111 return error_mark_node;
42114 switch (TREE_CODE (cond))
42116 case GT_EXPR:
42117 case GE_EXPR:
42118 case LT_EXPR:
42119 case LE_EXPR:
42120 break;
42121 case NE_EXPR:
42122 if (code != OACC_LOOP)
42123 break;
42124 gcc_fallthrough ();
42125 default:
42126 return error_mark_node;
42129 /* If decl is an iterator, preserve LHS and RHS of the relational
42130 expr until finish_omp_for. */
42131 if (decl
42132 && (type_dependent_expression_p (decl)
42133 || CLASS_TYPE_P (TREE_TYPE (decl))))
42134 return cond;
42136 return build_x_binary_op (cp_expr_loc_or_input_loc (cond),
42137 TREE_CODE (cond),
42138 TREE_OPERAND (cond, 0), ERROR_MARK,
42139 TREE_OPERAND (cond, 1), ERROR_MARK,
42140 NULL_TREE, /*overload=*/NULL, tf_warning_or_error);
42143 /* Helper function, to parse omp for increment expression. */
42145 static tree
42146 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
42148 cp_token *token = cp_lexer_peek_token (parser->lexer);
42149 enum tree_code op;
42150 tree lhs, rhs;
42151 cp_id_kind idk;
42152 bool decl_first;
42154 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42156 op = (token->type == CPP_PLUS_PLUS
42157 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
42158 cp_lexer_consume_token (parser->lexer);
42159 lhs = cp_parser_simple_cast_expression (parser);
42160 if (lhs != decl
42161 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42162 return error_mark_node;
42163 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42166 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
42167 if (lhs != decl
42168 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
42169 return error_mark_node;
42171 token = cp_lexer_peek_token (parser->lexer);
42172 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
42174 op = (token->type == CPP_PLUS_PLUS
42175 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
42176 cp_lexer_consume_token (parser->lexer);
42177 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
42180 op = cp_parser_assignment_operator_opt (parser);
42181 if (op == ERROR_MARK)
42182 return error_mark_node;
42184 if (op != NOP_EXPR)
42186 rhs = cp_parser_assignment_expression (parser);
42187 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
42188 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42191 lhs = cp_parser_binary_expression (parser, false, false,
42192 PREC_ADDITIVE_EXPRESSION, NULL);
42193 token = cp_lexer_peek_token (parser->lexer);
42194 decl_first = (lhs == decl
42195 || (processing_template_decl && cp_tree_equal (lhs, decl)));
42196 if (decl_first)
42197 lhs = NULL_TREE;
42198 if (token->type != CPP_PLUS
42199 && token->type != CPP_MINUS)
42200 return error_mark_node;
42204 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
42205 cp_lexer_consume_token (parser->lexer);
42206 rhs = cp_parser_binary_expression (parser, false, false,
42207 PREC_ADDITIVE_EXPRESSION, NULL);
42208 token = cp_lexer_peek_token (parser->lexer);
42209 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
42211 if (lhs == NULL_TREE)
42213 if (op == PLUS_EXPR)
42214 lhs = rhs;
42215 else
42216 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
42217 NULL_TREE, tf_warning_or_error);
42219 else
42220 lhs = build_x_binary_op (input_location, op,
42221 lhs, ERROR_MARK,
42222 rhs, ERROR_MARK,
42223 NULL_TREE, NULL, tf_warning_or_error);
42226 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
42228 if (!decl_first)
42230 if ((rhs != decl
42231 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
42232 || op == MINUS_EXPR)
42233 return error_mark_node;
42234 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
42236 else
42237 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
42239 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
42242 /* Parse the initialization statement of an OpenMP for loop.
42244 Return true if the resulting construct should have an
42245 OMP_CLAUSE_PRIVATE added to it. */
42247 static tree
42248 cp_parser_omp_for_loop_init (cp_parser *parser,
42249 tree &this_pre_body,
42250 releasing_vec &for_block,
42251 tree &init,
42252 tree &orig_init,
42253 tree &decl,
42254 tree &real_decl)
42256 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
42257 return NULL_TREE;
42259 tree add_private_clause = NULL_TREE;
42261 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
42263 init-expr:
42264 var = lb
42265 integer-type var = lb
42266 random-access-iterator-type var = lb
42267 pointer-type var = lb
42269 cp_decl_specifier_seq type_specifiers;
42271 /* First, try to parse as an initialized declaration. See
42272 cp_parser_condition, from whence the bulk of this is copied. */
42274 cp_parser_parse_tentatively (parser);
42275 cp_parser_type_specifier_seq (parser, CP_PARSER_FLAGS_NONE,
42276 /*is_declaration=*/true,
42277 /*is_trailing_return=*/false,
42278 &type_specifiers);
42279 if (cp_parser_parse_definitely (parser))
42281 /* If parsing a type specifier seq succeeded, then this
42282 MUST be a initialized declaration. */
42283 tree asm_specification, attributes;
42284 cp_declarator *declarator;
42286 declarator = cp_parser_declarator (parser,
42287 CP_PARSER_DECLARATOR_NAMED,
42288 CP_PARSER_FLAGS_NONE,
42289 /*ctor_dtor_or_conv_p=*/NULL,
42290 /*parenthesized_p=*/NULL,
42291 /*member_p=*/false,
42292 /*friend_p=*/false,
42293 /*static_p=*/false);
42294 attributes = cp_parser_attributes_opt (parser);
42295 asm_specification = cp_parser_asm_specification_opt (parser);
42297 if (declarator == cp_error_declarator)
42298 cp_parser_skip_to_end_of_statement (parser);
42300 else
42302 tree pushed_scope, auto_node;
42304 decl = start_decl (declarator, &type_specifiers,
42305 SD_INITIALIZED, attributes,
42306 /*prefix_attributes=*/NULL_TREE,
42307 &pushed_scope);
42309 auto_node = type_uses_auto (TREE_TYPE (decl));
42310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
42312 if (cp_lexer_next_token_is (parser->lexer,
42313 CPP_OPEN_PAREN))
42314 error ("parenthesized initialization is not allowed in "
42315 "OpenMP %<for%> loop");
42316 else
42317 /* Trigger an error. */
42318 cp_parser_require (parser, CPP_EQ, RT_EQ);
42320 init = error_mark_node;
42321 cp_parser_skip_to_end_of_statement (parser);
42323 else if (CLASS_TYPE_P (TREE_TYPE (decl))
42324 || type_dependent_expression_p (decl)
42325 || auto_node)
42327 bool is_direct_init, is_non_constant_init;
42329 init = cp_parser_initializer (parser,
42330 &is_direct_init,
42331 &is_non_constant_init);
42333 if (auto_node)
42335 TREE_TYPE (decl)
42336 = do_auto_deduction (TREE_TYPE (decl), init,
42337 auto_node);
42339 if (!CLASS_TYPE_P (TREE_TYPE (decl))
42340 && !type_dependent_expression_p (decl))
42341 goto non_class;
42344 cp_finish_decl (decl, init, !is_non_constant_init,
42345 asm_specification,
42346 LOOKUP_ONLYCONVERTING);
42347 orig_init = init;
42348 if (CLASS_TYPE_P (TREE_TYPE (decl)))
42350 vec_safe_push (for_block, this_pre_body);
42351 init = NULL_TREE;
42353 else
42355 init = pop_stmt_list (this_pre_body);
42356 if (init && TREE_CODE (init) == STATEMENT_LIST)
42358 tree_stmt_iterator i = tsi_start (init);
42359 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
42360 while (!tsi_end_p (i))
42362 tree t = tsi_stmt (i);
42363 if (TREE_CODE (t) == DECL_EXPR
42364 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
42366 tsi_delink (&i);
42367 vec_safe_push (for_block, t);
42368 continue;
42370 break;
42372 if (tsi_one_before_end_p (i))
42374 tree t = tsi_stmt (i);
42375 tsi_delink (&i);
42376 free_stmt_list (init);
42377 init = t;
42381 this_pre_body = NULL_TREE;
42383 else
42385 /* Consume '='. */
42386 cp_lexer_consume_token (parser->lexer);
42387 init = cp_parser_assignment_expression (parser);
42389 non_class:
42390 if (TYPE_REF_P (TREE_TYPE (decl)))
42391 init = error_mark_node;
42392 else
42393 cp_finish_decl (decl, NULL_TREE,
42394 /*init_const_expr_p=*/false,
42395 asm_specification,
42396 LOOKUP_ONLYCONVERTING);
42399 if (pushed_scope)
42400 pop_scope (pushed_scope);
42403 else
42405 cp_id_kind idk;
42406 /* If parsing a type specifier sequence failed, then
42407 this MUST be a simple expression. */
42408 cp_parser_parse_tentatively (parser);
42409 decl = cp_parser_primary_expression (parser, false, false,
42410 false, &idk);
42411 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
42412 if (!cp_parser_error_occurred (parser)
42413 && decl
42414 && (TREE_CODE (decl) == COMPONENT_REF
42415 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
42417 cp_parser_abort_tentative_parse (parser);
42418 cp_parser_parse_tentatively (parser);
42419 cp_token *token = cp_lexer_peek_token (parser->lexer);
42420 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
42421 /*check_dependency_p=*/true,
42422 /*template_p=*/NULL,
42423 /*declarator_p=*/false,
42424 /*optional_p=*/false);
42425 if (name != error_mark_node
42426 && last_tok == cp_lexer_peek_token (parser->lexer))
42428 decl = cp_parser_lookup_name_simple (parser, name,
42429 token->location);
42430 if (TREE_CODE (decl) == FIELD_DECL)
42431 add_private_clause = omp_privatize_field (decl, false);
42433 cp_parser_abort_tentative_parse (parser);
42434 cp_parser_parse_tentatively (parser);
42435 decl = cp_parser_primary_expression (parser, false, false,
42436 false, &idk);
42438 if (!cp_parser_error_occurred (parser)
42439 && decl
42440 && DECL_P (decl)
42441 && CLASS_TYPE_P (TREE_TYPE (decl)))
42443 tree rhs;
42445 cp_parser_parse_definitely (parser);
42446 cp_parser_require (parser, CPP_EQ, RT_EQ);
42447 rhs = cp_parser_assignment_expression (parser);
42448 orig_init = rhs;
42449 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
42450 decl, NOP_EXPR,
42451 rhs, NULL_TREE,
42452 tf_warning_or_error));
42453 if (!add_private_clause)
42454 add_private_clause = decl;
42456 else
42458 decl = NULL;
42459 cp_parser_abort_tentative_parse (parser);
42460 init = cp_parser_expression (parser);
42461 if (init)
42463 if (TREE_CODE (init) == MODIFY_EXPR
42464 || TREE_CODE (init) == MODOP_EXPR)
42465 real_decl = TREE_OPERAND (init, 0);
42469 return add_private_clause;
42472 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
42474 void
42475 cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
42476 tree &decl, tree &orig_decl, tree &init,
42477 tree &orig_init, tree &cond, tree &incr)
42479 tree begin, end, range_temp_decl = NULL_TREE;
42480 tree iter_type, begin_expr, end_expr;
42482 if (processing_template_decl)
42484 if (check_for_bare_parameter_packs (init))
42485 init = error_mark_node;
42486 if (!type_dependent_expression_p (init)
42487 /* do_auto_deduction doesn't mess with template init-lists. */
42488 && !BRACE_ENCLOSED_INITIALIZER_P (init))
42490 tree d = decl;
42491 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
42493 tree v = DECL_VALUE_EXPR (decl);
42494 if (TREE_CODE (v) == ARRAY_REF
42495 && VAR_P (TREE_OPERAND (v, 0))
42496 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42497 d = TREE_OPERAND (v, 0);
42499 do_range_for_auto_deduction (d, init);
42501 cond = global_namespace;
42502 incr = NULL_TREE;
42503 orig_init = init;
42504 if (this_pre_body)
42505 this_pre_body = pop_stmt_list (this_pre_body);
42506 return;
42509 init = mark_lvalue_use (init);
42511 if (decl == error_mark_node || init == error_mark_node)
42512 /* If an error happened previously do nothing or else a lot of
42513 unhelpful errors would be issued. */
42514 begin_expr = end_expr = iter_type = error_mark_node;
42515 else
42517 tree range_temp;
42519 if (VAR_P (init)
42520 && array_of_runtime_bound_p (TREE_TYPE (init)))
42521 /* Can't bind a reference to an array of runtime bound. */
42522 range_temp = init;
42523 else
42525 range_temp = build_range_temp (init);
42526 DECL_NAME (range_temp) = NULL_TREE;
42527 pushdecl (range_temp);
42528 cp_finish_decl (range_temp, init,
42529 /*is_constant_init*/false, NULL_TREE,
42530 LOOKUP_ONLYCONVERTING);
42531 range_temp_decl = range_temp;
42532 range_temp = convert_from_reference (range_temp);
42534 iter_type = cp_parser_perform_range_for_lookup (range_temp,
42535 &begin_expr, &end_expr);
42538 tree end_iter_type = iter_type;
42539 if (cxx_dialect >= cxx17)
42540 end_iter_type = cv_unqualified (TREE_TYPE (end_expr));
42541 end = build_decl (input_location, VAR_DECL, NULL_TREE, end_iter_type);
42542 TREE_USED (end) = 1;
42543 DECL_ARTIFICIAL (end) = 1;
42544 pushdecl (end);
42545 cp_finish_decl (end, end_expr,
42546 /*is_constant_init*/false, NULL_TREE,
42547 LOOKUP_ONLYCONVERTING);
42549 /* The new for initialization statement. */
42550 begin = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
42551 TREE_USED (begin) = 1;
42552 DECL_ARTIFICIAL (begin) = 1;
42553 pushdecl (begin);
42554 orig_init = init;
42555 if (CLASS_TYPE_P (iter_type))
42556 init = NULL_TREE;
42557 else
42559 init = begin_expr;
42560 begin_expr = NULL_TREE;
42562 cp_finish_decl (begin, begin_expr,
42563 /*is_constant_init*/false, NULL_TREE,
42564 LOOKUP_ONLYCONVERTING);
42566 /* The new for condition. */
42567 if (CLASS_TYPE_P (iter_type))
42568 cond = build2 (NE_EXPR, boolean_type_node, begin, end);
42569 else
42570 cond = build_x_binary_op (input_location, NE_EXPR,
42571 begin, ERROR_MARK,
42572 end, ERROR_MARK,
42573 NULL_TREE, NULL, tf_warning_or_error);
42575 /* The new increment expression. */
42576 if (CLASS_TYPE_P (iter_type))
42577 incr = build2 (PREINCREMENT_EXPR, iter_type, begin, NULL_TREE);
42578 else
42579 incr = finish_unary_op_expr (input_location,
42580 PREINCREMENT_EXPR, begin,
42581 tf_warning_or_error);
42583 orig_decl = decl;
42584 decl = begin;
42585 if (for_block)
42587 vec_safe_push (for_block, this_pre_body);
42588 this_pre_body = NULL_TREE;
42591 tree decomp_first_name = NULL_TREE;
42592 unsigned decomp_cnt = 0;
42593 if (orig_decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (orig_decl))
42595 tree v = DECL_VALUE_EXPR (orig_decl);
42596 if (TREE_CODE (v) == ARRAY_REF
42597 && VAR_P (TREE_OPERAND (v, 0))
42598 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
42600 tree d = orig_decl;
42601 orig_decl = TREE_OPERAND (v, 0);
42602 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
42603 decomp_first_name = d;
42607 tree auto_node = type_uses_auto (TREE_TYPE (orig_decl));
42608 if (auto_node)
42610 tree t = build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42611 NULL_TREE, tf_none);
42612 if (!error_operand_p (t))
42613 TREE_TYPE (orig_decl) = do_auto_deduction (TREE_TYPE (orig_decl),
42614 t, auto_node);
42617 tree v = make_tree_vec (decomp_cnt + 3);
42618 TREE_VEC_ELT (v, 0) = range_temp_decl;
42619 TREE_VEC_ELT (v, 1) = end;
42620 TREE_VEC_ELT (v, 2) = orig_decl;
42621 for (unsigned i = 0; i < decomp_cnt; i++)
42623 TREE_VEC_ELT (v, i + 3) = decomp_first_name;
42624 decomp_first_name = DECL_CHAIN (decomp_first_name);
42626 orig_decl = tree_cons (NULL_TREE, NULL_TREE, v);
42629 /* Helper for cp_parser_omp_for_loop, finalize part of range for
42630 inside of the collapsed body. */
42632 void
42633 cp_finish_omp_range_for (tree orig, tree begin)
42635 gcc_assert (TREE_CODE (orig) == TREE_LIST
42636 && TREE_CODE (TREE_CHAIN (orig)) == TREE_VEC);
42637 tree decl = TREE_VEC_ELT (TREE_CHAIN (orig), 2);
42638 tree decomp_first_name = NULL_TREE;
42639 unsigned int decomp_cnt = 0;
42641 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42643 decomp_first_name = TREE_VEC_ELT (TREE_CHAIN (orig), 3);
42644 decomp_cnt = TREE_VEC_LENGTH (TREE_CHAIN (orig)) - 3;
42645 cp_maybe_mangle_decomp (decl, decomp_first_name, decomp_cnt);
42648 /* The declaration is initialized with *__begin inside the loop body. */
42649 cp_finish_decl (decl,
42650 build_x_indirect_ref (input_location, begin, RO_UNARY_STAR,
42651 NULL_TREE, tf_warning_or_error),
42652 /*is_constant_init*/false, NULL_TREE,
42653 LOOKUP_ONLYCONVERTING);
42654 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
42655 cp_finish_decomp (decl, decomp_first_name, decomp_cnt);
42658 /* Return true if next tokens contain a standard attribute that contains
42659 omp::directive (DIRECTIVE). */
42661 static bool
42662 cp_parser_omp_section_scan (cp_parser *parser, const char *directive,
42663 bool tentative)
42665 size_t n = cp_parser_skip_attributes_opt (parser, 1), i;
42666 if (n < 10)
42667 return false;
42668 for (i = 5; i < n - 4; i++)
42669 if (cp_lexer_nth_token_is (parser->lexer, i, CPP_NAME)
42670 && cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_OPEN_PAREN)
42671 && cp_lexer_nth_token_is (parser->lexer, i + 2, CPP_NAME))
42673 tree first = cp_lexer_peek_nth_token (parser->lexer, i)->u.value;
42674 tree second = cp_lexer_peek_nth_token (parser->lexer, i + 2)->u.value;
42675 if (strcmp (IDENTIFIER_POINTER (first), "directive"))
42676 continue;
42677 if (strcmp (IDENTIFIER_POINTER (second), directive) == 0)
42678 break;
42680 if (i == n - 4)
42681 return false;
42682 cp_parser_parse_tentatively (parser);
42683 location_t first_loc = cp_lexer_peek_token (parser->lexer)->location;
42684 location_t last_loc
42685 = cp_lexer_peek_nth_token (parser->lexer, n - 1)->location;
42686 location_t middle_loc = UNKNOWN_LOCATION;
42687 tree std_attrs = cp_parser_std_attribute_spec_seq (parser);
42688 int cnt = 0;
42689 bool seen = false;
42690 for (tree attr = std_attrs; attr; attr = TREE_CHAIN (attr))
42691 if (get_attribute_namespace (attr) == omp_identifier
42692 && is_attribute_p ("directive", get_attribute_name (attr)))
42694 for (tree a = TREE_VALUE (attr); a; a = TREE_CHAIN (a))
42696 tree d = TREE_VALUE (a);
42697 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
42698 cp_token *first = DEFPARSE_TOKENS (d)->first;
42699 cnt++;
42700 if (first->type == CPP_NAME
42701 && strcmp (IDENTIFIER_POINTER (first->u.value),
42702 directive) == 0)
42704 seen = true;
42705 if (middle_loc == UNKNOWN_LOCATION)
42706 middle_loc = first->location;
42710 if (!seen || tentative)
42712 cp_parser_abort_tentative_parse (parser);
42713 return seen;
42715 if (cnt != 1 || TREE_CHAIN (std_attrs))
42717 error_at (make_location (first_loc, last_loc, middle_loc),
42718 "%<[[omp::directive(%s)]]%> must be the only specified "
42719 "attribute on a statement", directive);
42720 cp_parser_abort_tentative_parse (parser);
42721 return false;
42723 if (!cp_parser_parse_definitely (parser))
42724 return false;
42725 cp_parser_handle_statement_omp_attributes (parser, std_attrs);
42726 return true;
42729 /* Parse an OpenMP structured block sequence. KIND is the corresponding
42730 separating directive. */
42732 static tree
42733 cp_parser_omp_structured_block_sequence (cp_parser *parser,
42734 enum pragma_kind kind)
42736 tree stmt = begin_omp_structured_block ();
42737 unsigned int save = cp_parser_begin_omp_structured_block (parser);
42739 cp_parser_statement (parser, NULL_TREE, false, NULL);
42740 while (true)
42742 cp_token *token = cp_lexer_peek_token (parser->lexer);
42744 if (token->type == CPP_CLOSE_BRACE
42745 || token->type == CPP_EOF
42746 || token->type == CPP_PRAGMA_EOL
42747 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END)
42748 || (kind != PRAGMA_NONE
42749 && cp_parser_pragma_kind (token) == kind))
42750 break;
42752 if (kind != PRAGMA_NONE
42753 && cp_parser_omp_section_scan (parser,
42754 kind == PRAGMA_OMP_SCAN
42755 ? "scan" : "section", false))
42756 break;
42758 cp_parser_statement (parser, NULL_TREE, false, NULL);
42761 cp_parser_end_omp_structured_block (parser, save);
42762 return finish_omp_structured_block (stmt);
42766 /* OpenMP 5.0:
42768 scan-loop-body:
42769 { structured-block scan-directive structured-block } */
42771 static void
42772 cp_parser_omp_scan_loop_body (cp_parser *parser)
42774 tree substmt, clauses = NULL_TREE;
42776 matching_braces braces;
42777 if (!braces.require_open (parser))
42778 return;
42780 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_OMP_SCAN);
42781 substmt = build2 (OMP_SCAN, void_type_node, substmt, NULL_TREE);
42782 add_stmt (substmt);
42784 cp_token *tok = cp_lexer_peek_token (parser->lexer);
42785 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SCAN)
42787 enum omp_clause_code clause = OMP_CLAUSE_ERROR;
42789 cp_lexer_consume_token (parser->lexer);
42791 if (parser->lexer->in_omp_attribute_pragma
42792 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
42793 cp_lexer_consume_token (parser->lexer);
42795 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
42797 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
42798 const char *p = IDENTIFIER_POINTER (id);
42799 if (strcmp (p, "inclusive") == 0)
42800 clause = OMP_CLAUSE_INCLUSIVE;
42801 else if (strcmp (p, "exclusive") == 0)
42802 clause = OMP_CLAUSE_EXCLUSIVE;
42804 if (clause != OMP_CLAUSE_ERROR)
42806 cp_lexer_consume_token (parser->lexer);
42807 clauses = cp_parser_omp_var_list (parser, clause, NULL_TREE);
42809 else
42810 cp_parser_error (parser, "expected %<inclusive%> or "
42811 "%<exclusive%> clause");
42813 cp_parser_require_pragma_eol (parser, tok);
42815 else
42816 error ("expected %<#pragma omp scan%>");
42818 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
42819 substmt = cp_parser_omp_structured_block_sequence (parser, PRAGMA_NONE);
42820 substmt = build2_loc (tok->location, OMP_SCAN, void_type_node, substmt,
42821 clauses);
42822 add_stmt (substmt);
42824 braces.require_close (parser);
42827 /* Parse the restricted form of the for statement allowed by OpenMP. */
42829 static tree
42830 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
42831 tree *cclauses, bool *if_p)
42833 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
42834 tree orig_decl;
42835 tree real_decl, initv, condv, incrv, declv, orig_declv;
42836 tree this_pre_body, cl, ordered_cl = NULL_TREE;
42837 location_t loc_first;
42838 bool collapse_err = false;
42839 int i, collapse = 1, ordered = 0, count, nbraces = 0;
42840 releasing_vec for_block;
42841 auto_vec<tree, 4> orig_inits;
42842 bool tiling = false;
42843 bool inscan = false;
42845 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
42846 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
42847 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
42848 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
42850 tiling = true;
42851 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
42853 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
42854 && OMP_CLAUSE_ORDERED_EXPR (cl))
42856 ordered_cl = cl;
42857 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
42859 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
42860 && OMP_CLAUSE_REDUCTION_INSCAN (cl)
42861 && (code == OMP_SIMD || code == OMP_FOR))
42862 inscan = true;
42864 if (ordered && ordered < collapse)
42866 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42867 "%<ordered%> clause parameter is less than %<collapse%>");
42868 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
42869 = build_int_cst (NULL_TREE, collapse);
42870 ordered = collapse;
42873 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
42874 count = ordered ? ordered : collapse;
42876 declv = make_tree_vec (count);
42877 initv = make_tree_vec (count);
42878 condv = make_tree_vec (count);
42879 incrv = make_tree_vec (count);
42880 orig_declv = NULL_TREE;
42882 loc_first = cp_lexer_peek_token (parser->lexer)->location;
42884 for (i = 0; i < count; i++)
42886 int bracecount = 0;
42887 tree add_private_clause = NULL_TREE;
42888 location_t loc;
42890 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
42892 if (!collapse_err)
42893 cp_parser_error (parser, "for statement expected");
42894 return NULL;
42896 loc = cp_lexer_consume_token (parser->lexer)->location;
42898 /* Don't create location wrapper nodes within an OpenMP "for"
42899 statement. */
42900 auto_suppress_location_wrappers sentinel;
42902 matching_parens parens;
42903 if (!parens.require_open (parser))
42904 return NULL;
42906 init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
42907 this_pre_body = push_stmt_list ();
42909 if (code != OACC_LOOP && cxx_dialect >= cxx11)
42911 /* Save tokens so that we can put them back. */
42912 cp_lexer_save_tokens (parser->lexer);
42914 /* Look for ':' that is not nested in () or {}. */
42915 bool is_range_for
42916 = (cp_parser_skip_to_closing_parenthesis_1 (parser,
42917 /*recovering=*/false,
42918 CPP_COLON,
42919 /*consume_paren=*/
42920 false) == -1);
42922 /* Roll back the tokens we skipped. */
42923 cp_lexer_rollback_tokens (parser->lexer);
42925 if (is_range_for)
42927 bool saved_colon_corrects_to_scope_p
42928 = parser->colon_corrects_to_scope_p;
42930 /* A colon is used in range-based for. */
42931 parser->colon_corrects_to_scope_p = false;
42933 /* Parse the declaration. */
42934 cp_parser_simple_declaration (parser,
42935 /*function_definition_allowed_p=*/
42936 false, &decl);
42937 parser->colon_corrects_to_scope_p
42938 = saved_colon_corrects_to_scope_p;
42940 cp_parser_require (parser, CPP_COLON, RT_COLON);
42942 init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
42943 false, 0, true);
42945 cp_convert_omp_range_for (this_pre_body, for_block, decl,
42946 orig_decl, init, orig_init,
42947 cond, incr);
42948 if (this_pre_body)
42950 if (pre_body)
42952 tree t = pre_body;
42953 pre_body = push_stmt_list ();
42954 add_stmt (t);
42955 add_stmt (this_pre_body);
42956 pre_body = pop_stmt_list (pre_body);
42958 else
42959 pre_body = this_pre_body;
42962 if (ordered_cl)
42963 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
42964 "%<ordered%> clause with parameter on "
42965 "range-based %<for%> loop");
42967 goto parse_close_paren;
42971 add_private_clause
42972 = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
42973 init, orig_init, decl, real_decl);
42975 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
42976 if (this_pre_body)
42978 this_pre_body = pop_stmt_list (this_pre_body);
42979 if (pre_body)
42981 tree t = pre_body;
42982 pre_body = push_stmt_list ();
42983 add_stmt (t);
42984 add_stmt (this_pre_body);
42985 pre_body = pop_stmt_list (pre_body);
42987 else
42988 pre_body = this_pre_body;
42991 if (decl)
42992 real_decl = decl;
42993 if (cclauses != NULL
42994 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
42995 && real_decl != NULL_TREE
42996 && code != OMP_LOOP)
42998 tree *c;
42999 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
43000 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
43001 && OMP_CLAUSE_DECL (*c) == real_decl)
43003 error_at (loc, "iteration variable %qD"
43004 " should not be firstprivate", real_decl);
43005 *c = OMP_CLAUSE_CHAIN (*c);
43007 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
43008 && OMP_CLAUSE_DECL (*c) == real_decl)
43010 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
43011 tree l = *c;
43012 *c = OMP_CLAUSE_CHAIN (*c);
43013 if (code == OMP_SIMD)
43015 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43016 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
43018 else
43020 OMP_CLAUSE_CHAIN (l) = clauses;
43021 clauses = l;
43023 add_private_clause = NULL_TREE;
43025 else
43027 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
43028 && OMP_CLAUSE_DECL (*c) == real_decl)
43029 add_private_clause = NULL_TREE;
43030 c = &OMP_CLAUSE_CHAIN (*c);
43034 if (add_private_clause)
43036 tree c;
43037 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
43039 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
43040 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
43041 && OMP_CLAUSE_DECL (c) == decl)
43042 break;
43043 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
43044 && OMP_CLAUSE_DECL (c) == decl)
43045 error_at (loc, "iteration variable %qD "
43046 "should not be firstprivate",
43047 decl);
43048 else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
43049 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
43050 && OMP_CLAUSE_DECL (c) == decl)
43051 error_at (loc, "iteration variable %qD should not be reduction",
43052 decl);
43054 if (c == NULL)
43056 if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
43057 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
43058 else if (code != OMP_SIMD)
43059 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
43060 else
43061 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
43062 OMP_CLAUSE_DECL (c) = add_private_clause;
43063 c = finish_omp_clauses (c, C_ORT_OMP);
43064 if (c)
43066 OMP_CLAUSE_CHAIN (c) = clauses;
43067 clauses = c;
43068 /* For linear, signal that we need to fill up
43069 the so far unknown linear step. */
43070 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
43071 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
43076 cond = NULL;
43077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
43078 cond = cp_parser_omp_for_cond (parser, decl, code);
43079 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
43081 incr = NULL;
43082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
43084 /* If decl is an iterator, preserve the operator on decl
43085 until finish_omp_for. */
43086 if (real_decl
43087 && ((processing_template_decl
43088 && (TREE_TYPE (real_decl) == NULL_TREE
43089 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
43090 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
43091 incr = cp_parser_omp_for_incr (parser, real_decl);
43092 else
43093 incr = cp_parser_expression (parser);
43094 protected_set_expr_location_if_unset (incr, input_location);
43097 parse_close_paren:
43098 if (!parens.require_close (parser))
43099 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
43100 /*or_comma=*/false,
43101 /*consume_paren=*/true);
43103 TREE_VEC_ELT (declv, i) = decl;
43104 TREE_VEC_ELT (initv, i) = init;
43105 TREE_VEC_ELT (condv, i) = cond;
43106 TREE_VEC_ELT (incrv, i) = incr;
43107 if (orig_init)
43109 orig_inits.safe_grow_cleared (i + 1, true);
43110 orig_inits[i] = orig_init;
43112 if (orig_decl)
43114 if (!orig_declv)
43115 orig_declv = copy_node (declv);
43116 TREE_VEC_ELT (orig_declv, i) = orig_decl;
43118 else if (orig_declv)
43119 TREE_VEC_ELT (orig_declv, i) = decl;
43121 if (i == count - 1)
43122 break;
43124 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
43125 in between the collapsed for loops to be still considered perfectly
43126 nested. Hopefully the final version clarifies this.
43127 For now handle (multiple) {'s and empty statements. */
43128 cp_parser_parse_tentatively (parser);
43129 for (;;)
43131 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43132 break;
43133 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
43135 cp_lexer_consume_token (parser->lexer);
43136 bracecount++;
43138 else if (bracecount
43139 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43140 cp_lexer_consume_token (parser->lexer);
43141 else
43143 loc = cp_lexer_peek_token (parser->lexer)->location;
43144 error_at (loc, "not enough for loops to collapse");
43145 collapse_err = true;
43146 cp_parser_abort_tentative_parse (parser);
43147 declv = NULL_TREE;
43148 break;
43152 if (declv)
43154 cp_parser_parse_definitely (parser);
43155 nbraces += bracecount;
43159 if (nbraces)
43160 if_p = NULL;
43162 /* Note that we saved the original contents of this flag when we entered
43163 the structured block, and so we don't need to re-save it here. */
43164 parser->in_statement = IN_OMP_FOR;
43166 /* Note that the grammar doesn't call for a structured block here,
43167 though the loop as a whole is a structured block. */
43168 if (orig_declv)
43170 body = begin_omp_structured_block ();
43171 for (i = 0; i < count; i++)
43172 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
43173 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
43174 TREE_VEC_ELT (declv, i));
43176 else
43177 body = push_stmt_list ();
43178 if (inscan)
43179 cp_parser_omp_scan_loop_body (parser);
43180 else
43181 cp_parser_statement (parser, NULL_TREE, false, if_p);
43182 if (orig_declv)
43183 body = finish_omp_structured_block (body);
43184 else
43185 body = pop_stmt_list (body);
43187 if (declv == NULL_TREE)
43188 ret = NULL_TREE;
43189 else
43190 ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
43191 incrv, body, pre_body, &orig_inits, clauses);
43193 while (nbraces)
43195 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
43197 cp_lexer_consume_token (parser->lexer);
43198 nbraces--;
43200 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
43201 cp_lexer_consume_token (parser->lexer);
43202 else
43204 if (!collapse_err)
43206 error_at (cp_lexer_peek_token (parser->lexer)->location,
43207 "collapsed loops not perfectly nested");
43209 collapse_err = true;
43210 cp_parser_statement_seq_opt (parser, NULL);
43211 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
43212 break;
43216 while (!for_block->is_empty ())
43218 tree t = for_block->pop ();
43219 if (TREE_CODE (t) == STATEMENT_LIST)
43220 add_stmt (pop_stmt_list (t));
43221 else
43222 add_stmt (t);
43225 return ret;
43228 /* Helper function for OpenMP parsing, split clauses and call
43229 finish_omp_clauses on each of the set of clauses afterwards. */
43231 static void
43232 cp_omp_split_clauses (location_t loc, enum tree_code code,
43233 omp_clause_mask mask, tree clauses, tree *cclauses)
43235 int i;
43236 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
43237 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
43238 if (cclauses[i])
43239 cclauses[i] = finish_omp_clauses (cclauses[i],
43240 i == C_OMP_CLAUSE_SPLIT_TARGET
43241 ? C_ORT_OMP_TARGET : C_ORT_OMP);
43244 /* OpenMP 5.0:
43245 #pragma omp loop loop-clause[optseq] new-line
43246 for-loop */
43248 #define OMP_LOOP_CLAUSE_MASK \
43249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
43254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43256 static tree
43257 cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
43258 char *p_name, omp_clause_mask mask, tree *cclauses,
43259 bool *if_p)
43261 tree clauses, sb, ret;
43262 unsigned int save;
43263 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43265 strcat (p_name, " loop");
43266 mask |= OMP_LOOP_CLAUSE_MASK;
43268 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43269 cclauses == NULL);
43270 if (cclauses)
43272 cp_omp_split_clauses (loc, OMP_LOOP, mask, clauses, cclauses);
43273 clauses = cclauses[C_OMP_CLAUSE_SPLIT_LOOP];
43276 keep_next_level (true);
43277 sb = begin_omp_structured_block ();
43278 save = cp_parser_begin_omp_structured_block (parser);
43280 ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
43282 cp_parser_end_omp_structured_block (parser, save);
43283 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43285 return ret;
43288 /* OpenMP 4.0:
43289 #pragma omp simd simd-clause[optseq] new-line
43290 for-loop */
43292 #define OMP_SIMD_CLAUSE_MASK \
43293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
43294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
43295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
43297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
43303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43305 static tree
43306 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
43307 char *p_name, omp_clause_mask mask, tree *cclauses,
43308 bool *if_p)
43310 tree clauses, sb, ret;
43311 unsigned int save;
43312 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43314 strcat (p_name, " simd");
43315 mask |= OMP_SIMD_CLAUSE_MASK;
43317 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43318 cclauses == NULL);
43319 if (cclauses)
43321 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
43322 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
43325 keep_next_level (true);
43326 sb = begin_omp_structured_block ();
43327 save = cp_parser_begin_omp_structured_block (parser);
43329 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
43331 cp_parser_end_omp_structured_block (parser, save);
43332 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43334 return ret;
43337 /* OpenMP 2.5:
43338 #pragma omp for for-clause[optseq] new-line
43339 for-loop
43341 OpenMP 4.0:
43342 #pragma omp for simd for-simd-clause[optseq] new-line
43343 for-loop */
43345 #define OMP_FOR_CLAUSE_MASK \
43346 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
43350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
43352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
43353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
43354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
43358 static tree
43359 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
43360 char *p_name, omp_clause_mask mask, tree *cclauses,
43361 bool *if_p)
43363 tree clauses, sb, ret;
43364 unsigned int save;
43365 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43367 strcat (p_name, " for");
43368 mask |= OMP_FOR_CLAUSE_MASK;
43369 /* parallel for{, simd} disallows nowait clause, but for
43370 target {teams distribute ,}parallel for{, simd} it should be accepted. */
43371 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
43372 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43373 /* Composite distribute parallel for{, simd} disallows ordered clause. */
43374 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43375 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
43377 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43379 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43380 const char *p = IDENTIFIER_POINTER (id);
43382 if (strcmp (p, "simd") == 0)
43384 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43385 if (cclauses == NULL)
43386 cclauses = cclauses_buf;
43388 cp_lexer_consume_token (parser->lexer);
43389 if (!flag_openmp) /* flag_openmp_simd */
43390 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43391 cclauses, if_p);
43392 sb = begin_omp_structured_block ();
43393 save = cp_parser_begin_omp_structured_block (parser);
43394 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
43395 cclauses, if_p);
43396 cp_parser_end_omp_structured_block (parser, save);
43397 tree body = finish_omp_structured_block (sb);
43398 if (ret == NULL)
43399 return ret;
43400 ret = make_node (OMP_FOR);
43401 TREE_TYPE (ret) = void_type_node;
43402 OMP_FOR_BODY (ret) = body;
43403 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43404 SET_EXPR_LOCATION (ret, loc);
43405 add_stmt (ret);
43406 return ret;
43409 if (!flag_openmp) /* flag_openmp_simd */
43411 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43412 return NULL_TREE;
43415 /* Composite distribute parallel for disallows linear clause. */
43416 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43417 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
43419 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43420 cclauses == NULL);
43421 if (cclauses)
43423 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
43424 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
43427 keep_next_level (true);
43428 sb = begin_omp_structured_block ();
43429 save = cp_parser_begin_omp_structured_block (parser);
43431 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
43433 cp_parser_end_omp_structured_block (parser, save);
43434 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
43436 return ret;
43439 static tree cp_parser_omp_taskloop (cp_parser *, cp_token *, char *,
43440 omp_clause_mask, tree *, bool *);
43442 /* OpenMP 2.5:
43443 # pragma omp master new-line
43444 structured-block */
43446 static tree
43447 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok,
43448 char *p_name, omp_clause_mask mask, tree *cclauses,
43449 bool *if_p)
43451 tree clauses, sb, ret;
43452 unsigned int save;
43453 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43455 strcat (p_name, " master");
43457 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43459 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43460 const char *p = IDENTIFIER_POINTER (id);
43462 if (strcmp (p, "taskloop") == 0)
43464 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43465 if (cclauses == NULL)
43466 cclauses = cclauses_buf;
43468 cp_lexer_consume_token (parser->lexer);
43469 if (!flag_openmp) /* flag_openmp_simd */
43470 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43471 cclauses, if_p);
43472 sb = begin_omp_structured_block ();
43473 save = cp_parser_begin_omp_structured_block (parser);
43474 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43475 cclauses, if_p);
43476 cp_parser_end_omp_structured_block (parser, save);
43477 tree body = finish_omp_structured_block (sb);
43478 if (ret == NULL)
43479 return ret;
43480 ret = c_finish_omp_master (loc, body);
43481 OMP_MASTER_COMBINED (ret) = 1;
43482 return ret;
43485 if (!flag_openmp) /* flag_openmp_simd */
43487 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43488 return NULL_TREE;
43491 if (cclauses)
43493 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43494 false);
43495 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43497 else
43498 cp_parser_require_pragma_eol (parser, pragma_tok);
43500 return c_finish_omp_master (loc,
43501 cp_parser_omp_structured_block (parser, if_p));
43504 /* OpenMP 5.1:
43505 # pragma omp masked masked-clauses new-line
43506 structured-block */
43508 #define OMP_MASKED_CLAUSE_MASK \
43509 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FILTER)
43511 static tree
43512 cp_parser_omp_masked (cp_parser *parser, cp_token *pragma_tok,
43513 char *p_name, omp_clause_mask mask, tree *cclauses,
43514 bool *if_p)
43516 tree clauses, sb, ret;
43517 unsigned int save;
43518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43520 strcat (p_name, " masked");
43521 mask |= OMP_MASKED_CLAUSE_MASK;
43523 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43525 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43526 const char *p = IDENTIFIER_POINTER (id);
43528 if (strcmp (p, "taskloop") == 0)
43530 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43531 if (cclauses == NULL)
43532 cclauses = cclauses_buf;
43534 cp_lexer_consume_token (parser->lexer);
43535 if (!flag_openmp) /* flag_openmp_simd */
43536 return cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43537 cclauses, if_p);
43538 sb = begin_omp_structured_block ();
43539 save = cp_parser_begin_omp_structured_block (parser);
43540 ret = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask,
43541 cclauses, if_p);
43542 cp_parser_end_omp_structured_block (parser, save);
43543 tree body = finish_omp_structured_block (sb);
43544 if (ret == NULL)
43545 return ret;
43546 ret = c_finish_omp_masked (loc, body,
43547 cclauses[C_OMP_CLAUSE_SPLIT_MASKED]);
43548 OMP_MASKED_COMBINED (ret) = 1;
43549 return ret;
43552 if (!flag_openmp) /* flag_openmp_simd */
43554 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43555 return NULL_TREE;
43558 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43559 cclauses == NULL);
43560 if (cclauses)
43562 cp_omp_split_clauses (loc, OMP_MASTER, mask, clauses, cclauses);
43563 clauses = cclauses[C_OMP_CLAUSE_SPLIT_MASKED];
43566 return c_finish_omp_masked (loc,
43567 cp_parser_omp_structured_block (parser, if_p),
43568 clauses);
43571 /* OpenMP 2.5:
43572 # pragma omp ordered new-line
43573 structured-block
43575 OpenMP 4.5:
43576 # pragma omp ordered ordered-clauses new-line
43577 structured-block */
43579 #define OMP_ORDERED_CLAUSE_MASK \
43580 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
43581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
43583 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
43584 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
43585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DOACROSS))
43587 static bool
43588 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
43589 enum pragma_context context, bool *if_p)
43591 location_t loc = pragma_tok->location;
43592 int n = 1;
43594 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
43595 if (parser->lexer->in_omp_attribute_pragma
43596 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
43597 n = 2;
43599 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_NAME))
43601 tree id = cp_lexer_peek_nth_token (parser->lexer, n)->u.value;
43602 const char *p = IDENTIFIER_POINTER (id);
43604 if (strcmp (p, "depend") == 0 || strcmp (p, "doacross") == 0)
43606 if (!flag_openmp) /* flag_openmp_simd */
43608 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43609 return false;
43611 if (context == pragma_stmt)
43613 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
43614 "%qs clause may only be used in compound "
43615 "statements", p);
43616 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43617 return true;
43619 tree clauses
43620 = cp_parser_omp_all_clauses (parser,
43621 OMP_ORDERED_DEPEND_CLAUSE_MASK,
43622 "#pragma omp ordered", pragma_tok);
43623 c_finish_omp_ordered (loc, clauses, NULL_TREE);
43624 return false;
43628 tree clauses
43629 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
43630 "#pragma omp ordered", pragma_tok);
43632 if (!flag_openmp /* flag_openmp_simd */
43633 && omp_find_clause (clauses, OMP_CLAUSE_SIMD) == NULL_TREE)
43634 return false;
43636 c_finish_omp_ordered (loc, clauses,
43637 cp_parser_omp_structured_block (parser, if_p));
43638 return true;
43641 /* OpenMP 2.5:
43643 section-scope:
43644 { section-sequence }
43646 section-sequence:
43647 section-directive[opt] structured-block
43648 section-sequence section-directive structured-block */
43650 static tree
43651 cp_parser_omp_sections_scope (cp_parser *parser)
43653 tree stmt, substmt;
43654 bool error_suppress = false;
43655 cp_token *tok;
43657 matching_braces braces;
43658 if (!braces.require_open (parser))
43659 return NULL_TREE;
43661 stmt = push_stmt_list ();
43663 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
43664 != PRAGMA_OMP_SECTION
43665 && !cp_parser_omp_section_scan (parser, "section", true))
43667 substmt = cp_parser_omp_structured_block_sequence (parser,
43668 PRAGMA_OMP_SECTION);
43669 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43670 add_stmt (substmt);
43673 while (1)
43675 tok = cp_lexer_peek_token (parser->lexer);
43676 if (tok->type == CPP_CLOSE_BRACE)
43677 break;
43678 if (tok->type == CPP_EOF)
43679 break;
43681 if (cp_parser_omp_section_scan (parser, "section", false))
43682 tok = cp_lexer_peek_token (parser->lexer);
43683 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
43685 cp_lexer_consume_token (parser->lexer);
43686 cp_parser_require_pragma_eol (parser, tok);
43687 error_suppress = false;
43689 else if (!error_suppress)
43691 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
43692 error_suppress = true;
43695 substmt = cp_parser_omp_structured_block_sequence (parser,
43696 PRAGMA_OMP_SECTION);
43697 substmt = build1 (OMP_SECTION, void_type_node, substmt);
43698 add_stmt (substmt);
43700 braces.require_close (parser);
43702 substmt = pop_stmt_list (stmt);
43704 stmt = make_node (OMP_SECTIONS);
43705 TREE_TYPE (stmt) = void_type_node;
43706 OMP_SECTIONS_BODY (stmt) = substmt;
43708 add_stmt (stmt);
43709 return stmt;
43712 /* OpenMP 2.5:
43713 # pragma omp sections sections-clause[optseq] newline
43714 sections-scope */
43716 #define OMP_SECTIONS_CLAUSE_MASK \
43717 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43724 static tree
43725 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
43726 char *p_name, omp_clause_mask mask, tree *cclauses)
43728 tree clauses, ret;
43729 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43731 strcat (p_name, " sections");
43732 mask |= OMP_SECTIONS_CLAUSE_MASK;
43733 if (cclauses)
43734 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
43736 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43737 cclauses == NULL);
43738 if (cclauses)
43740 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
43741 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
43744 ret = cp_parser_omp_sections_scope (parser);
43745 if (ret)
43746 OMP_SECTIONS_CLAUSES (ret) = clauses;
43748 return ret;
43751 /* OpenMP 2.5:
43752 # pragma omp parallel parallel-clause[optseq] new-line
43753 structured-block
43754 # pragma omp parallel for parallel-for-clause[optseq] new-line
43755 structured-block
43756 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
43757 structured-block
43759 OpenMP 4.0:
43760 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
43761 structured-block */
43763 #define OMP_PARALLEL_CLAUSE_MASK \
43764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
43770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
43772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
43775 static tree
43776 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
43777 char *p_name, omp_clause_mask mask, tree *cclauses,
43778 bool *if_p)
43780 tree stmt, clauses, block;
43781 unsigned int save;
43782 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
43784 strcat (p_name, " parallel");
43785 mask |= OMP_PARALLEL_CLAUSE_MASK;
43786 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
43787 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
43788 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
43789 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
43791 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
43793 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43794 if (cclauses == NULL)
43795 cclauses = cclauses_buf;
43797 cp_lexer_consume_token (parser->lexer);
43798 if (!flag_openmp) /* flag_openmp_simd */
43799 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43800 if_p);
43801 block = begin_omp_parallel ();
43802 save = cp_parser_begin_omp_structured_block (parser);
43803 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
43804 if_p);
43805 cp_parser_end_omp_structured_block (parser, save);
43806 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43807 block);
43808 if (ret == NULL_TREE)
43809 return ret;
43810 OMP_PARALLEL_COMBINED (stmt) = 1;
43811 return stmt;
43813 /* When combined with distribute, parallel has to be followed by for.
43814 #pragma omp target parallel is allowed though. */
43815 else if (cclauses
43816 && (mask & (OMP_CLAUSE_MASK_1
43817 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
43819 error_at (loc, "expected %<for%> after %qs", p_name);
43820 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43821 return NULL_TREE;
43823 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
43825 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
43826 const char *p = IDENTIFIER_POINTER (id);
43827 if (cclauses == NULL && strcmp (p, "masked") == 0)
43829 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43830 cclauses = cclauses_buf;
43832 cp_lexer_consume_token (parser->lexer);
43833 if (!flag_openmp) /* flag_openmp_simd */
43834 return cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43835 cclauses, if_p);
43836 block = begin_omp_parallel ();
43837 save = cp_parser_begin_omp_structured_block (parser);
43838 tree ret = cp_parser_omp_masked (parser, pragma_tok, p_name, mask,
43839 cclauses, if_p);
43840 cp_parser_end_omp_structured_block (parser, save);
43841 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43842 block);
43843 if (ret == NULL_TREE)
43844 return ret;
43845 /* masked does have just filter clause, but during gimplification
43846 isn't represented by a gimplification omp context, so for
43847 #pragma omp parallel masked don't set OMP_PARALLEL_COMBINED,
43848 so that
43849 #pragma omp parallel masked
43850 #pragma omp taskloop simd lastprivate (x)
43851 isn't confused with
43852 #pragma omp parallel masked taskloop simd lastprivate (x) */
43853 if (OMP_MASKED_COMBINED (ret))
43854 OMP_PARALLEL_COMBINED (stmt) = 1;
43855 return stmt;
43857 else if (cclauses == NULL && strcmp (p, "master") == 0)
43859 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43860 cclauses = cclauses_buf;
43862 cp_lexer_consume_token (parser->lexer);
43863 if (!flag_openmp) /* flag_openmp_simd */
43864 return cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43865 cclauses, if_p);
43866 block = begin_omp_parallel ();
43867 save = cp_parser_begin_omp_structured_block (parser);
43868 tree ret = cp_parser_omp_master (parser, pragma_tok, p_name, mask,
43869 cclauses, if_p);
43870 cp_parser_end_omp_structured_block (parser, save);
43871 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43872 block);
43873 if (ret == NULL_TREE)
43874 return ret;
43875 /* master doesn't have any clauses and during gimplification
43876 isn't represented by a gimplification omp context, so for
43877 #pragma omp parallel master don't set OMP_PARALLEL_COMBINED,
43878 so that
43879 #pragma omp parallel master
43880 #pragma omp taskloop simd lastprivate (x)
43881 isn't confused with
43882 #pragma omp parallel master taskloop simd lastprivate (x) */
43883 if (OMP_MASTER_COMBINED (ret))
43884 OMP_PARALLEL_COMBINED (stmt) = 1;
43885 return stmt;
43887 else if (strcmp (p, "loop") == 0)
43889 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43890 if (cclauses == NULL)
43891 cclauses = cclauses_buf;
43893 cp_lexer_consume_token (parser->lexer);
43894 if (!flag_openmp) /* flag_openmp_simd */
43895 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43896 cclauses, if_p);
43897 block = begin_omp_parallel ();
43898 save = cp_parser_begin_omp_structured_block (parser);
43899 tree ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
43900 cclauses, if_p);
43901 cp_parser_end_omp_structured_block (parser, save);
43902 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43903 block);
43904 if (ret == NULL_TREE)
43905 return ret;
43906 OMP_PARALLEL_COMBINED (stmt) = 1;
43907 return stmt;
43909 else if (!flag_openmp) /* flag_openmp_simd */
43911 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43912 return NULL_TREE;
43914 else if (cclauses == NULL && strcmp (p, "sections") == 0)
43916 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
43917 cclauses = cclauses_buf;
43919 cp_lexer_consume_token (parser->lexer);
43920 block = begin_omp_parallel ();
43921 save = cp_parser_begin_omp_structured_block (parser);
43922 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
43923 cp_parser_end_omp_structured_block (parser, save);
43924 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
43925 block);
43926 OMP_PARALLEL_COMBINED (stmt) = 1;
43927 return stmt;
43930 else if (!flag_openmp) /* flag_openmp_simd */
43932 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
43933 return NULL_TREE;
43936 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
43937 cclauses == NULL);
43938 if (cclauses)
43940 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
43941 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
43944 block = begin_omp_parallel ();
43945 save = cp_parser_begin_omp_structured_block (parser);
43946 parser->omp_attrs_forbidden_p = true;
43947 cp_parser_statement (parser, NULL_TREE, false, if_p);
43948 cp_parser_end_omp_structured_block (parser, save);
43949 stmt = finish_omp_parallel (clauses, block);
43950 return stmt;
43953 /* OpenMP 2.5:
43954 # pragma omp single single-clause[optseq] new-line
43955 structured-block */
43957 #define OMP_SINGLE_CLAUSE_MASK \
43958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
43961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43964 static tree
43965 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43967 tree stmt = make_node (OMP_SINGLE);
43968 TREE_TYPE (stmt) = void_type_node;
43969 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43971 OMP_SINGLE_CLAUSES (stmt)
43972 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
43973 "#pragma omp single", pragma_tok);
43974 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
43976 return add_stmt (stmt);
43979 /* OpenMP 5.1:
43980 # pragma omp scope scope-clause[optseq] new-line
43981 structured-block */
43983 #define OMP_SCOPE_CLAUSE_MASK \
43984 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
43990 static tree
43991 cp_parser_omp_scope (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
43993 tree stmt = make_node (OMP_SCOPE);
43994 TREE_TYPE (stmt) = void_type_node;
43995 SET_EXPR_LOCATION (stmt, pragma_tok->location);
43997 OMP_SCOPE_CLAUSES (stmt)
43998 = cp_parser_omp_all_clauses (parser, OMP_SCOPE_CLAUSE_MASK,
43999 "#pragma omp scope", pragma_tok);
44000 OMP_SCOPE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44002 return add_stmt (stmt);
44005 /* OpenMP 3.0:
44006 # pragma omp task task-clause[optseq] new-line
44007 structured-block */
44009 #define OMP_TASK_CLAUSE_MASK \
44010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
44012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
44013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
44017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
44018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
44020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH) \
44023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_AFFINITY))
44025 static tree
44026 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44028 tree clauses, block;
44029 unsigned int save;
44031 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
44032 "#pragma omp task", pragma_tok);
44033 block = begin_omp_task ();
44034 save = cp_parser_begin_omp_structured_block (parser);
44035 parser->omp_attrs_forbidden_p = true;
44036 cp_parser_statement (parser, NULL_TREE, false, if_p);
44037 cp_parser_end_omp_structured_block (parser, save);
44038 return finish_omp_task (clauses, block);
44041 /* OpenMP 3.0:
44042 # pragma omp taskwait new-line
44044 OpenMP 5.0:
44045 # pragma omp taskwait taskwait-clause[opt] new-line */
44047 #define OMP_TASKWAIT_CLAUSE_MASK \
44048 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44051 static void
44052 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
44054 tree clauses
44055 = cp_parser_omp_all_clauses (parser, OMP_TASKWAIT_CLAUSE_MASK,
44056 "#pragma omp taskwait", pragma_tok);
44058 if (clauses)
44060 tree stmt = make_node (OMP_TASK);
44061 TREE_TYPE (stmt) = void_node;
44062 OMP_TASK_CLAUSES (stmt) = clauses;
44063 OMP_TASK_BODY (stmt) = NULL_TREE;
44064 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44065 add_stmt (stmt);
44067 else
44068 finish_omp_taskwait ();
44071 /* OpenMP 3.1:
44072 # pragma omp taskyield new-line */
44074 static void
44075 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
44077 cp_parser_require_pragma_eol (parser, pragma_tok);
44078 finish_omp_taskyield ();
44081 /* OpenMP 4.0:
44082 # pragma omp taskgroup new-line
44083 structured-block
44085 OpenMP 5.0:
44086 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
44088 #define OMP_TASKGROUP_CLAUSE_MASK \
44089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
44092 static tree
44093 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44095 tree clauses
44096 = cp_parser_omp_all_clauses (parser, OMP_TASKGROUP_CLAUSE_MASK,
44097 "#pragma omp taskgroup", pragma_tok);
44098 return c_finish_omp_taskgroup (input_location,
44099 cp_parser_omp_structured_block (parser,
44100 if_p),
44101 clauses);
44105 /* OpenMP 2.5:
44106 # pragma omp threadprivate (variable-list) */
44108 static void
44109 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
44111 tree vars;
44113 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
44114 cp_parser_require_pragma_eol (parser, pragma_tok);
44116 finish_omp_threadprivate (vars);
44119 /* OpenMP 4.0:
44120 # pragma omp cancel cancel-clause[optseq] new-line */
44122 #define OMP_CANCEL_CLAUSE_MASK \
44123 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
44127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
44129 static void
44130 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
44132 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
44133 "#pragma omp cancel", pragma_tok);
44134 finish_omp_cancel (clauses);
44137 /* OpenMP 4.0:
44138 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
44140 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
44141 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
44142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
44143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
44144 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
44146 static bool
44147 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
44148 enum pragma_context context)
44150 tree clauses;
44151 bool point_seen = false;
44153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44155 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44156 const char *p = IDENTIFIER_POINTER (id);
44158 if (strcmp (p, "point") == 0)
44160 cp_lexer_consume_token (parser->lexer);
44161 point_seen = true;
44164 if (!point_seen)
44166 cp_parser_error (parser, "expected %<point%>");
44167 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44168 return false;
44171 if (context != pragma_compound)
44173 if (context == pragma_stmt)
44174 error_at (pragma_tok->location,
44175 "%<#pragma %s%> may only be used in compound statements",
44176 "omp cancellation point");
44177 else
44178 cp_parser_error (parser, "expected declaration specifiers");
44179 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44180 return true;
44183 clauses = cp_parser_omp_all_clauses (parser,
44184 OMP_CANCELLATION_POINT_CLAUSE_MASK,
44185 "#pragma omp cancellation point",
44186 pragma_tok);
44187 finish_omp_cancellation_point (clauses);
44188 return true;
44191 /* OpenMP 4.0:
44192 #pragma omp distribute distribute-clause[optseq] new-line
44193 for-loop */
44195 #define OMP_DISTRIBUTE_CLAUSE_MASK \
44196 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
44199 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
44200 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
44202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
44204 static tree
44205 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
44206 char *p_name, omp_clause_mask mask, tree *cclauses,
44207 bool *if_p)
44209 tree clauses, sb, ret;
44210 unsigned int save;
44211 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44213 strcat (p_name, " distribute");
44214 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
44216 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44218 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44219 const char *p = IDENTIFIER_POINTER (id);
44220 bool simd = false;
44221 bool parallel = false;
44223 if (strcmp (p, "simd") == 0)
44224 simd = true;
44225 else
44226 parallel = strcmp (p, "parallel") == 0;
44227 if (parallel || simd)
44229 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44230 if (cclauses == NULL)
44231 cclauses = cclauses_buf;
44232 cp_lexer_consume_token (parser->lexer);
44233 if (!flag_openmp) /* flag_openmp_simd */
44235 if (simd)
44236 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44237 cclauses, if_p);
44238 else
44239 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44240 cclauses, if_p);
44242 sb = begin_omp_structured_block ();
44243 save = cp_parser_begin_omp_structured_block (parser);
44244 if (simd)
44245 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
44246 cclauses, if_p);
44247 else
44248 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
44249 cclauses, if_p);
44250 cp_parser_end_omp_structured_block (parser, save);
44251 tree body = finish_omp_structured_block (sb);
44252 if (ret == NULL)
44253 return ret;
44254 ret = make_node (OMP_DISTRIBUTE);
44255 TREE_TYPE (ret) = void_type_node;
44256 OMP_FOR_BODY (ret) = body;
44257 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44258 SET_EXPR_LOCATION (ret, loc);
44259 add_stmt (ret);
44260 return ret;
44263 if (!flag_openmp) /* flag_openmp_simd */
44265 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44266 return NULL_TREE;
44269 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44270 cclauses == NULL);
44271 if (cclauses)
44273 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
44274 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
44277 keep_next_level (true);
44278 sb = begin_omp_structured_block ();
44279 save = cp_parser_begin_omp_structured_block (parser);
44281 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
44283 cp_parser_end_omp_structured_block (parser, save);
44284 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
44286 return ret;
44289 /* OpenMP 4.0:
44290 # pragma omp teams teams-clause[optseq] new-line
44291 structured-block */
44293 #define OMP_TEAMS_CLAUSE_MASK \
44294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
44297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
44298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
44299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
44303 static tree
44304 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
44305 char *p_name, omp_clause_mask mask, tree *cclauses,
44306 bool *if_p)
44308 tree clauses, sb, ret;
44309 unsigned int save;
44310 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
44312 strcat (p_name, " teams");
44313 mask |= OMP_TEAMS_CLAUSE_MASK;
44315 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44317 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44318 const char *p = IDENTIFIER_POINTER (id);
44319 if (strcmp (p, "distribute") == 0)
44321 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44322 if (cclauses == NULL)
44323 cclauses = cclauses_buf;
44325 cp_lexer_consume_token (parser->lexer);
44326 if (!flag_openmp) /* flag_openmp_simd */
44327 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44328 cclauses, if_p);
44329 keep_next_level (true);
44330 sb = begin_omp_structured_block ();
44331 save = cp_parser_begin_omp_structured_block (parser);
44332 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
44333 cclauses, if_p);
44334 cp_parser_end_omp_structured_block (parser, save);
44335 tree body = finish_omp_structured_block (sb);
44336 if (ret == NULL)
44337 return ret;
44338 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44339 ret = make_node (OMP_TEAMS);
44340 TREE_TYPE (ret) = void_type_node;
44341 OMP_TEAMS_CLAUSES (ret) = clauses;
44342 OMP_TEAMS_BODY (ret) = body;
44343 OMP_TEAMS_COMBINED (ret) = 1;
44344 SET_EXPR_LOCATION (ret, loc);
44345 return add_stmt (ret);
44347 else if (strcmp (p, "loop") == 0)
44349 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
44350 if (cclauses == NULL)
44351 cclauses = cclauses_buf;
44353 cp_lexer_consume_token (parser->lexer);
44354 if (!flag_openmp) /* flag_openmp_simd */
44355 return cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44356 cclauses, if_p);
44357 keep_next_level (true);
44358 sb = begin_omp_structured_block ();
44359 save = cp_parser_begin_omp_structured_block (parser);
44360 ret = cp_parser_omp_loop (parser, pragma_tok, p_name, mask,
44361 cclauses, if_p);
44362 cp_parser_end_omp_structured_block (parser, save);
44363 tree body = finish_omp_structured_block (sb);
44364 if (ret == NULL)
44365 return ret;
44366 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44367 ret = make_node (OMP_TEAMS);
44368 TREE_TYPE (ret) = void_type_node;
44369 OMP_TEAMS_CLAUSES (ret) = clauses;
44370 OMP_TEAMS_BODY (ret) = body;
44371 OMP_TEAMS_COMBINED (ret) = 1;
44372 SET_EXPR_LOCATION (ret, loc);
44373 return add_stmt (ret);
44376 if (!flag_openmp) /* flag_openmp_simd */
44378 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44379 return NULL_TREE;
44382 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
44383 cclauses == NULL);
44384 if (cclauses)
44386 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
44387 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44390 tree stmt = make_node (OMP_TEAMS);
44391 TREE_TYPE (stmt) = void_type_node;
44392 OMP_TEAMS_CLAUSES (stmt) = clauses;
44393 keep_next_level (true);
44394 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44395 SET_EXPR_LOCATION (stmt, loc);
44397 return add_stmt (stmt);
44400 /* OpenMP 4.0:
44401 # pragma omp target data target-data-clause[optseq] new-line
44402 structured-block */
44404 #define OMP_TARGET_DATA_CLAUSE_MASK \
44405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
44409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
44411 static tree
44412 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44414 if (flag_openmp)
44415 omp_requires_mask
44416 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44418 tree clauses
44419 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
44420 "#pragma omp target data", pragma_tok);
44421 c_omp_adjust_map_clauses (clauses, false);
44422 int map_seen = 0;
44423 for (tree *pc = &clauses; *pc;)
44425 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44426 switch (OMP_CLAUSE_MAP_KIND (*pc))
44428 case GOMP_MAP_TO:
44429 case GOMP_MAP_ALWAYS_TO:
44430 case GOMP_MAP_FROM:
44431 case GOMP_MAP_ALWAYS_FROM:
44432 case GOMP_MAP_TOFROM:
44433 case GOMP_MAP_ALWAYS_TOFROM:
44434 case GOMP_MAP_ALLOC:
44435 map_seen = 3;
44436 break;
44437 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44438 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44439 case GOMP_MAP_ALWAYS_POINTER:
44440 case GOMP_MAP_ATTACH_DETACH:
44441 break;
44442 default:
44443 map_seen |= 1;
44444 error_at (OMP_CLAUSE_LOCATION (*pc),
44445 "%<#pragma omp target data%> with map-type other "
44446 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
44447 "on %<map%> clause");
44448 *pc = OMP_CLAUSE_CHAIN (*pc);
44449 continue;
44451 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_PTR
44452 || OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_USE_DEVICE_ADDR)
44453 map_seen = 3;
44454 pc = &OMP_CLAUSE_CHAIN (*pc);
44457 if (map_seen != 3)
44459 if (map_seen == 0)
44460 error_at (pragma_tok->location,
44461 "%<#pragma omp target data%> must contain at least "
44462 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
44463 "clause");
44464 return NULL_TREE;
44467 tree stmt = make_node (OMP_TARGET_DATA);
44468 TREE_TYPE (stmt) = void_type_node;
44469 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
44471 keep_next_level (true);
44472 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
44474 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44475 return add_stmt (stmt);
44478 /* OpenMP 4.5:
44479 # pragma omp target enter data target-enter-data-clause[optseq] new-line
44480 structured-block */
44482 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
44483 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44489 static bool
44490 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
44491 enum pragma_context context)
44493 bool data_seen = false;
44494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44496 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44497 const char *p = IDENTIFIER_POINTER (id);
44499 if (strcmp (p, "data") == 0)
44501 cp_lexer_consume_token (parser->lexer);
44502 data_seen = true;
44505 if (!data_seen)
44507 cp_parser_error (parser, "expected %<data%>");
44508 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44509 return false;
44512 if (context == pragma_stmt)
44514 error_at (pragma_tok->location,
44515 "%<#pragma %s%> may only be used in compound statements",
44516 "omp target enter data");
44517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44518 return true;
44521 if (flag_openmp)
44522 omp_requires_mask
44523 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44525 tree clauses
44526 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
44527 "#pragma omp target enter data", pragma_tok);
44528 c_omp_adjust_map_clauses (clauses, false);
44529 int map_seen = 0;
44530 for (tree *pc = &clauses; *pc;)
44532 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44533 switch (OMP_CLAUSE_MAP_KIND (*pc))
44535 case GOMP_MAP_TO:
44536 case GOMP_MAP_ALWAYS_TO:
44537 case GOMP_MAP_ALLOC:
44538 map_seen = 3;
44539 break;
44540 case GOMP_MAP_TOFROM:
44541 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_TO);
44542 map_seen = 3;
44543 break;
44544 case GOMP_MAP_ALWAYS_TOFROM:
44545 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_TO);
44546 map_seen = 3;
44547 break;
44548 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44549 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44550 case GOMP_MAP_ALWAYS_POINTER:
44551 case GOMP_MAP_ATTACH_DETACH:
44552 break;
44553 default:
44554 map_seen |= 1;
44555 error_at (OMP_CLAUSE_LOCATION (*pc),
44556 "%<#pragma omp target enter data%> with map-type other "
44557 "than %<to%>, %<tofrom%> or %<alloc%> on %<map%> clause");
44558 *pc = OMP_CLAUSE_CHAIN (*pc);
44559 continue;
44561 pc = &OMP_CLAUSE_CHAIN (*pc);
44564 if (map_seen != 3)
44566 if (map_seen == 0)
44567 error_at (pragma_tok->location,
44568 "%<#pragma omp target enter data%> must contain at least "
44569 "one %<map%> clause");
44570 return true;
44573 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
44574 TREE_TYPE (stmt) = void_type_node;
44575 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
44576 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44577 add_stmt (stmt);
44578 return true;
44581 /* OpenMP 4.5:
44582 # pragma omp target exit data target-enter-data-clause[optseq] new-line
44583 structured-block */
44585 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
44586 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44592 static bool
44593 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
44594 enum pragma_context context)
44596 bool data_seen = false;
44597 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44599 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44600 const char *p = IDENTIFIER_POINTER (id);
44602 if (strcmp (p, "data") == 0)
44604 cp_lexer_consume_token (parser->lexer);
44605 data_seen = true;
44608 if (!data_seen)
44610 cp_parser_error (parser, "expected %<data%>");
44611 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44612 return false;
44615 if (context == pragma_stmt)
44617 error_at (pragma_tok->location,
44618 "%<#pragma %s%> may only be used in compound statements",
44619 "omp target exit data");
44620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44621 return true;
44624 if (flag_openmp)
44625 omp_requires_mask
44626 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44628 tree clauses
44629 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
44630 "#pragma omp target exit data", pragma_tok);
44631 c_omp_adjust_map_clauses (clauses, false);
44632 int map_seen = 0;
44633 for (tree *pc = &clauses; *pc;)
44635 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
44636 switch (OMP_CLAUSE_MAP_KIND (*pc))
44638 case GOMP_MAP_FROM:
44639 case GOMP_MAP_ALWAYS_FROM:
44640 case GOMP_MAP_RELEASE:
44641 case GOMP_MAP_DELETE:
44642 map_seen = 3;
44643 break;
44644 case GOMP_MAP_TOFROM:
44645 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_FROM);
44646 map_seen = 3;
44647 break;
44648 case GOMP_MAP_ALWAYS_TOFROM:
44649 OMP_CLAUSE_SET_MAP_KIND (*pc, GOMP_MAP_ALWAYS_FROM);
44650 map_seen = 3;
44651 break;
44652 case GOMP_MAP_FIRSTPRIVATE_POINTER:
44653 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
44654 case GOMP_MAP_ALWAYS_POINTER:
44655 case GOMP_MAP_ATTACH_DETACH:
44656 break;
44657 default:
44658 map_seen |= 1;
44659 error_at (OMP_CLAUSE_LOCATION (*pc),
44660 "%<#pragma omp target exit data%> with map-type other "
44661 "than %<from%>, %<tofrom%>, %<release%> or %<delete%> "
44662 "on %<map%> clause");
44663 *pc = OMP_CLAUSE_CHAIN (*pc);
44664 continue;
44666 pc = &OMP_CLAUSE_CHAIN (*pc);
44669 if (map_seen != 3)
44671 if (map_seen == 0)
44672 error_at (pragma_tok->location,
44673 "%<#pragma omp target exit data%> must contain at least "
44674 "one %<map%> clause");
44675 return true;
44678 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
44679 TREE_TYPE (stmt) = void_type_node;
44680 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
44681 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44682 add_stmt (stmt);
44683 return true;
44686 /* OpenMP 4.0:
44687 # pragma omp target update target-update-clause[optseq] new-line */
44689 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
44690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
44691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
44692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
44697 static bool
44698 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
44699 enum pragma_context context)
44701 if (context == pragma_stmt)
44703 error_at (pragma_tok->location,
44704 "%<#pragma %s%> may only be used in compound statements",
44705 "omp target update");
44706 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44707 return true;
44710 tree clauses
44711 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
44712 "#pragma omp target update", pragma_tok);
44713 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
44714 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
44716 error_at (pragma_tok->location,
44717 "%<#pragma omp target update%> must contain at least one "
44718 "%<from%> or %<to%> clauses");
44719 return true;
44722 if (flag_openmp)
44723 omp_requires_mask
44724 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44726 tree stmt = make_node (OMP_TARGET_UPDATE);
44727 TREE_TYPE (stmt) = void_type_node;
44728 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
44729 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44730 add_stmt (stmt);
44731 return true;
44734 /* OpenMP 4.0:
44735 # pragma omp target target-clause[optseq] new-line
44736 structured-block */
44738 #define OMP_TARGET_CLAUSE_MASK \
44739 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
44740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
44741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
44742 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
44743 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
44744 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
44745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
44746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
44747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
44748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
44749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
44750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR)\
44751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HAS_DEVICE_ADDR))
44753 static bool
44754 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
44755 enum pragma_context context, bool *if_p)
44757 if (flag_openmp)
44758 omp_requires_mask
44759 = (enum omp_requires) (omp_requires_mask | OMP_REQUIRES_TARGET_USED);
44761 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
44763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
44764 const char *p = IDENTIFIER_POINTER (id);
44765 enum tree_code ccode = ERROR_MARK;
44767 if (strcmp (p, "teams") == 0)
44768 ccode = OMP_TEAMS;
44769 else if (strcmp (p, "parallel") == 0)
44770 ccode = OMP_PARALLEL;
44771 else if (strcmp (p, "simd") == 0)
44772 ccode = OMP_SIMD;
44773 if (ccode != ERROR_MARK)
44775 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
44776 char p_name[sizeof ("#pragma omp target teams distribute "
44777 "parallel for simd")];
44779 cp_lexer_consume_token (parser->lexer);
44780 strcpy (p_name, "#pragma omp target");
44781 if (!flag_openmp) /* flag_openmp_simd */
44783 tree stmt;
44784 switch (ccode)
44786 case OMP_TEAMS:
44787 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
44788 OMP_TARGET_CLAUSE_MASK,
44789 cclauses, if_p);
44790 break;
44791 case OMP_PARALLEL:
44792 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44793 OMP_TARGET_CLAUSE_MASK,
44794 cclauses, if_p);
44795 break;
44796 case OMP_SIMD:
44797 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
44798 OMP_TARGET_CLAUSE_MASK,
44799 cclauses, if_p);
44800 break;
44801 default:
44802 gcc_unreachable ();
44804 return stmt != NULL_TREE;
44806 keep_next_level (true);
44807 tree sb = begin_omp_structured_block (), ret;
44808 unsigned save = cp_parser_begin_omp_structured_block (parser);
44809 switch (ccode)
44811 case OMP_TEAMS:
44812 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
44813 OMP_TARGET_CLAUSE_MASK, cclauses,
44814 if_p);
44815 break;
44816 case OMP_PARALLEL:
44817 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
44818 OMP_TARGET_CLAUSE_MASK, cclauses,
44819 if_p);
44820 break;
44821 case OMP_SIMD:
44822 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
44823 OMP_TARGET_CLAUSE_MASK, cclauses,
44824 if_p);
44825 break;
44826 default:
44827 gcc_unreachable ();
44829 cp_parser_end_omp_structured_block (parser, save);
44830 tree body = finish_omp_structured_block (sb);
44831 if (ret == NULL_TREE)
44832 return false;
44833 if (ccode == OMP_TEAMS && !processing_template_decl)
44834 /* For combined target teams, ensure the num_teams and
44835 thread_limit clause expressions are evaluated on the host,
44836 before entering the target construct. */
44837 for (tree c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
44838 c; c = OMP_CLAUSE_CHAIN (c))
44839 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
44840 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
44841 for (int i = 0;
44842 i <= (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS); ++i)
44843 if (OMP_CLAUSE_OPERAND (c, i)
44844 && TREE_CODE (OMP_CLAUSE_OPERAND (c, i)) != INTEGER_CST)
44846 tree expr = OMP_CLAUSE_OPERAND (c, i);
44847 expr = force_target_expr (TREE_TYPE (expr), expr,
44848 tf_none);
44849 if (expr == error_mark_node)
44850 continue;
44851 tree tmp = TARGET_EXPR_SLOT (expr);
44852 add_stmt (expr);
44853 OMP_CLAUSE_OPERAND (c, i) = expr;
44854 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
44855 OMP_CLAUSE_FIRSTPRIVATE);
44856 OMP_CLAUSE_DECL (tc) = tmp;
44857 OMP_CLAUSE_CHAIN (tc)
44858 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
44859 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
44861 c_omp_adjust_map_clauses (cclauses[C_OMP_CLAUSE_SPLIT_TARGET], true);
44862 finish_omp_target (pragma_tok->location,
44863 cclauses[C_OMP_CLAUSE_SPLIT_TARGET], body, true);
44864 return true;
44866 else if (!flag_openmp) /* flag_openmp_simd */
44868 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44869 return false;
44871 else if (strcmp (p, "data") == 0)
44873 cp_lexer_consume_token (parser->lexer);
44874 cp_parser_omp_target_data (parser, pragma_tok, if_p);
44875 return true;
44877 else if (strcmp (p, "enter") == 0)
44879 cp_lexer_consume_token (parser->lexer);
44880 return cp_parser_omp_target_enter_data (parser, pragma_tok, context);
44882 else if (strcmp (p, "exit") == 0)
44884 cp_lexer_consume_token (parser->lexer);
44885 return cp_parser_omp_target_exit_data (parser, pragma_tok, context);
44887 else if (strcmp (p, "update") == 0)
44889 cp_lexer_consume_token (parser->lexer);
44890 return cp_parser_omp_target_update (parser, pragma_tok, context);
44893 if (!flag_openmp) /* flag_openmp_simd */
44895 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
44896 return false;
44899 tree clauses = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
44900 "#pragma omp target", pragma_tok,
44901 false);
44902 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
44903 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
44905 tree nc = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
44906 OMP_CLAUSE_DECL (nc) = OMP_CLAUSE_DECL (c);
44907 OMP_CLAUSE_SET_MAP_KIND (nc, GOMP_MAP_ALWAYS_TOFROM);
44908 OMP_CLAUSE_CHAIN (nc) = OMP_CLAUSE_CHAIN (c);
44909 OMP_CLAUSE_CHAIN (c) = nc;
44911 clauses = finish_omp_clauses (clauses, C_ORT_OMP_TARGET);
44913 c_omp_adjust_map_clauses (clauses, true);
44914 keep_next_level (true);
44915 tree body = cp_parser_omp_structured_block (parser, if_p);
44917 finish_omp_target (pragma_tok->location, clauses, body, false);
44918 return true;
44921 /* OpenACC 2.0:
44922 # pragma acc cache (variable-list) new-line
44925 static tree
44926 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
44928 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
44929 clauses. */
44930 auto_suppress_location_wrappers sentinel;
44932 tree stmt, clauses;
44934 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
44935 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
44937 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
44939 stmt = make_node (OACC_CACHE);
44940 TREE_TYPE (stmt) = void_type_node;
44941 OACC_CACHE_CLAUSES (stmt) = clauses;
44942 SET_EXPR_LOCATION (stmt, pragma_tok->location);
44943 add_stmt (stmt);
44945 return stmt;
44948 /* OpenACC 2.0:
44949 # pragma acc data oacc-data-clause[optseq] new-line
44950 structured-block */
44952 #define OACC_DATA_CLAUSE_MASK \
44953 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
44954 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
44955 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
44956 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
44957 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
44958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
44959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
44960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
44962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
44964 static tree
44965 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44967 tree stmt, clauses, block;
44968 unsigned int save;
44970 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
44971 "#pragma acc data", pragma_tok);
44973 block = begin_omp_parallel ();
44974 save = cp_parser_begin_omp_structured_block (parser);
44975 cp_parser_statement (parser, NULL_TREE, false, if_p);
44976 cp_parser_end_omp_structured_block (parser, save);
44977 stmt = finish_oacc_data (clauses, block);
44978 return stmt;
44981 /* OpenACC 2.0:
44982 # pragma acc host_data <clauses> new-line
44983 structured-block */
44985 #define OACC_HOST_DATA_CLAUSE_MASK \
44986 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
44987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
44988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
44990 static tree
44991 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
44993 tree stmt, clauses, block;
44994 unsigned int save;
44996 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
44997 "#pragma acc host_data", pragma_tok);
44999 block = begin_omp_parallel ();
45000 save = cp_parser_begin_omp_structured_block (parser);
45001 cp_parser_statement (parser, NULL_TREE, false, if_p);
45002 cp_parser_end_omp_structured_block (parser, save);
45003 stmt = finish_oacc_host_data (clauses, block);
45004 return stmt;
45007 /* OpenACC 2.0:
45008 # pragma acc declare oacc-data-clause[optseq] new-line
45011 #define OACC_DECLARE_CLAUSE_MASK \
45012 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
45018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
45019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
45021 static tree
45022 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
45024 tree clauses, stmt;
45025 bool error = false;
45026 bool found_in_scope = global_bindings_p ();
45028 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
45029 "#pragma acc declare", pragma_tok, true);
45032 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45034 error_at (pragma_tok->location,
45035 "no valid clauses specified in %<#pragma acc declare%>");
45036 return NULL_TREE;
45039 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
45041 location_t loc = OMP_CLAUSE_LOCATION (t);
45042 tree decl = OMP_CLAUSE_DECL (t);
45043 if (!DECL_P (decl))
45045 error_at (loc, "array section in %<#pragma acc declare%>");
45046 error = true;
45047 continue;
45049 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
45050 switch (OMP_CLAUSE_MAP_KIND (t))
45052 case GOMP_MAP_FIRSTPRIVATE_POINTER:
45053 case GOMP_MAP_ALLOC:
45054 case GOMP_MAP_TO:
45055 case GOMP_MAP_FORCE_DEVICEPTR:
45056 case GOMP_MAP_DEVICE_RESIDENT:
45057 break;
45059 case GOMP_MAP_LINK:
45060 if (!global_bindings_p ()
45061 && (TREE_STATIC (decl)
45062 || !DECL_EXTERNAL (decl)))
45064 error_at (loc,
45065 "%qD must be a global variable in "
45066 "%<#pragma acc declare link%>",
45067 decl);
45068 error = true;
45069 continue;
45071 break;
45073 default:
45074 if (global_bindings_p ())
45076 error_at (loc, "invalid OpenACC clause at file scope");
45077 error = true;
45078 continue;
45080 if (DECL_EXTERNAL (decl))
45082 error_at (loc,
45083 "invalid use of %<extern%> variable %qD "
45084 "in %<#pragma acc declare%>", decl);
45085 error = true;
45086 continue;
45088 else if (TREE_PUBLIC (decl))
45090 error_at (loc,
45091 "invalid use of %<global%> variable %qD "
45092 "in %<#pragma acc declare%>", decl);
45093 error = true;
45094 continue;
45096 break;
45099 if (!found_in_scope)
45100 /* This seems to ignore the existence of cleanup scopes?
45101 What is the meaning for local extern decls? The local
45102 extern is in this scope, but it is referring to a decl that
45103 is namespace scope. */
45104 for (tree d = current_binding_level->names; d; d = TREE_CHAIN (d))
45105 if (d == decl)
45107 found_in_scope = true;
45108 break;
45110 if (!found_in_scope)
45112 error_at (loc,
45113 "%qD must be a variable declared in the same scope as "
45114 "%<#pragma acc declare%>", decl);
45115 error = true;
45116 continue;
45119 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
45120 || lookup_attribute ("omp declare target link",
45121 DECL_ATTRIBUTES (decl)))
45123 error_at (loc, "variable %qD used more than once with "
45124 "%<#pragma acc declare%>", decl);
45125 error = true;
45126 continue;
45129 if (!error)
45131 tree id;
45133 if (DECL_LOCAL_DECL_P (decl))
45134 /* We need to mark the aliased decl, as that is the entity
45135 that is being referred to. This won't work for
45136 dependent variables, but it didn't work for them before
45137 DECL_LOCAL_DECL_P was a thing either. But then
45138 dependent local extern variable decls are as rare as
45139 hen's teeth. */
45140 if (auto alias = DECL_LOCAL_DECL_ALIAS (decl))
45141 if (alias != error_mark_node)
45142 decl = alias;
45144 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
45145 id = get_identifier ("omp declare target link");
45146 else
45147 id = get_identifier ("omp declare target");
45149 DECL_ATTRIBUTES (decl)
45150 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
45151 if (current_binding_level->kind == sk_namespace)
45153 symtab_node *node = symtab_node::get (decl);
45154 if (node != NULL)
45156 node->offloadable = 1;
45157 if (ENABLE_OFFLOADING)
45159 g->have_offload = true;
45160 if (is_a <varpool_node *> (node))
45161 vec_safe_push (offload_vars, decl);
45168 if (error || current_binding_level->kind == sk_namespace)
45169 return NULL_TREE;
45171 stmt = make_node (OACC_DECLARE);
45172 TREE_TYPE (stmt) = void_type_node;
45173 OACC_DECLARE_CLAUSES (stmt) = clauses;
45174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45176 add_stmt (stmt);
45178 return NULL_TREE;
45181 /* OpenACC 2.0:
45182 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
45186 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
45188 LOC is the location of the #pragma token.
45191 #define OACC_ENTER_DATA_CLAUSE_MASK \
45192 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45194 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45199 #define OACC_EXIT_DATA_CLAUSE_MASK \
45200 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45201 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45202 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45203 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
45204 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
45205 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
45206 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45208 static tree
45209 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
45210 bool enter)
45212 location_t loc = pragma_tok->location;
45213 tree stmt, clauses;
45214 const char *p = "";
45216 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45217 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45219 if (strcmp (p, "data") != 0)
45221 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
45222 enter ? "enter" : "exit");
45223 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
45224 return NULL_TREE;
45227 cp_lexer_consume_token (parser->lexer);
45229 if (enter)
45230 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
45231 "#pragma acc enter data", pragma_tok);
45232 else
45233 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
45234 "#pragma acc exit data", pragma_tok);
45236 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45238 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
45239 enter ? "enter" : "exit");
45240 return NULL_TREE;
45243 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
45244 TREE_TYPE (stmt) = void_type_node;
45245 OMP_STANDALONE_CLAUSES (stmt) = clauses;
45246 SET_EXPR_LOCATION (stmt, loc);
45247 add_stmt (stmt);
45248 return stmt;
45251 /* OpenACC 2.0:
45252 # pragma acc loop oacc-loop-clause[optseq] new-line
45253 structured-block */
45255 #define OACC_LOOP_CLAUSE_MASK \
45256 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
45257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
45260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
45261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
45262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
45263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
45264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
45265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
45267 static tree
45268 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
45269 omp_clause_mask mask, tree *cclauses, bool *if_p)
45271 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
45273 strcat (p_name, " loop");
45274 mask |= OACC_LOOP_CLAUSE_MASK;
45276 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
45277 cclauses == NULL);
45278 if (cclauses)
45280 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
45281 if (*cclauses)
45282 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
45283 if (clauses)
45284 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
45287 tree block = begin_omp_structured_block ();
45288 int save = cp_parser_begin_omp_structured_block (parser);
45289 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
45290 cp_parser_end_omp_structured_block (parser, save);
45291 add_stmt (finish_omp_structured_block (block));
45293 return stmt;
45296 /* OpenACC 2.0:
45297 # pragma acc kernels oacc-kernels-clause[optseq] new-line
45298 structured-block
45302 # pragma acc parallel oacc-parallel-clause[optseq] new-line
45303 structured-block
45305 OpenACC 2.6:
45307 # pragma acc serial oacc-serial-clause[optseq] new-line
45310 #define OACC_KERNELS_CLAUSE_MASK \
45311 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45327 #define OACC_PARALLEL_CLAUSE_MASK \
45328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
45340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
45341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
45345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45347 #define OACC_SERIAL_CLAUSE_MASK \
45348 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
45350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
45351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
45352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
45353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
45354 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
45355 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
45356 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45357 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
45358 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
45359 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
45360 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
45361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
45362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
45364 static tree
45365 cp_parser_oacc_compute (cp_parser *parser, cp_token *pragma_tok,
45366 char *p_name, bool *if_p)
45368 omp_clause_mask mask;
45369 enum tree_code code;
45370 switch (cp_parser_pragma_kind (pragma_tok))
45372 case PRAGMA_OACC_KERNELS:
45373 strcat (p_name, " kernels");
45374 mask = OACC_KERNELS_CLAUSE_MASK;
45375 code = OACC_KERNELS;
45376 break;
45377 case PRAGMA_OACC_PARALLEL:
45378 strcat (p_name, " parallel");
45379 mask = OACC_PARALLEL_CLAUSE_MASK;
45380 code = OACC_PARALLEL;
45381 break;
45382 case PRAGMA_OACC_SERIAL:
45383 strcat (p_name, " serial");
45384 mask = OACC_SERIAL_CLAUSE_MASK;
45385 code = OACC_SERIAL;
45386 break;
45387 default:
45388 gcc_unreachable ();
45391 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45393 const char *p
45394 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45395 if (strcmp (p, "loop") == 0)
45397 cp_lexer_consume_token (parser->lexer);
45398 tree block = begin_omp_parallel ();
45399 tree clauses;
45400 tree stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask,
45401 &clauses, if_p);
45402 protected_set_expr_location (stmt, pragma_tok->location);
45403 return finish_omp_construct (code, block, clauses);
45407 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
45409 tree block = begin_omp_parallel ();
45410 unsigned int save = cp_parser_begin_omp_structured_block (parser);
45411 cp_parser_statement (parser, NULL_TREE, false, if_p);
45412 cp_parser_end_omp_structured_block (parser, save);
45413 return finish_omp_construct (code, block, clauses);
45416 /* OpenACC 2.0:
45417 # pragma acc update oacc-update-clause[optseq] new-line
45420 #define OACC_UPDATE_CLAUSE_MASK \
45421 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
45422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
45423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
45424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
45425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
45426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
45428 static tree
45429 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
45431 tree stmt, clauses;
45433 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
45434 "#pragma acc update", pragma_tok);
45436 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
45438 error_at (pragma_tok->location,
45439 "%<#pragma acc update%> must contain at least one "
45440 "%<device%> or %<host%> or %<self%> clause");
45441 return NULL_TREE;
45444 stmt = make_node (OACC_UPDATE);
45445 TREE_TYPE (stmt) = void_type_node;
45446 OACC_UPDATE_CLAUSES (stmt) = clauses;
45447 SET_EXPR_LOCATION (stmt, pragma_tok->location);
45448 add_stmt (stmt);
45449 return stmt;
45452 /* OpenACC 2.0:
45453 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
45455 LOC is the location of the #pragma token.
45458 #define OACC_WAIT_CLAUSE_MASK \
45459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
45461 static tree
45462 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
45464 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
45465 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
45467 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
45468 list = cp_parser_oacc_wait_list (parser, loc, list);
45470 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
45471 "#pragma acc wait", pragma_tok);
45473 stmt = c_finish_oacc_wait (loc, list, clauses);
45474 stmt = finish_expr_stmt (stmt);
45476 return stmt;
45479 /* OpenMP 4.0:
45480 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
45482 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
45483 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
45484 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
45485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
45486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
45487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
45488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
45490 static void
45491 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
45492 enum pragma_context context,
45493 bool variant_p)
45495 bool first_p = parser->omp_declare_simd == NULL;
45496 cp_omp_declare_simd_data data;
45497 if (first_p)
45499 data.error_seen = false;
45500 data.fndecl_seen = false;
45501 data.variant_p = variant_p;
45502 data.tokens = vNULL;
45503 data.attribs[0] = NULL;
45504 data.attribs[1] = NULL;
45505 data.loc = UNKNOWN_LOCATION;
45506 /* It is safe to take the address of a local variable; it will only be
45507 used while this scope is live. */
45508 parser->omp_declare_simd = &data;
45510 else if (parser->omp_declare_simd->variant_p != variant_p)
45512 error_at (pragma_tok->location,
45513 "%<#pragma omp declare %s%> followed by "
45514 "%<#pragma omp declare %s%>",
45515 parser->omp_declare_simd->variant_p ? "variant" : "simd",
45516 parser->omp_declare_simd->variant_p ? "simd" : "variant");
45517 parser->omp_declare_simd->error_seen = true;
45520 /* Store away all pragma tokens. */
45521 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
45522 cp_lexer_consume_token (parser->lexer);
45523 cp_parser_require_pragma_eol (parser, pragma_tok);
45524 struct cp_token_cache *cp
45525 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
45526 parser->omp_declare_simd->tokens.safe_push (cp);
45528 if (first_p)
45530 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
45531 cp_parser_pragma (parser, context, NULL);
45532 switch (context)
45534 case pragma_external:
45535 cp_parser_declaration (parser, NULL_TREE);
45536 break;
45537 case pragma_member:
45538 cp_parser_member_declaration (parser);
45539 break;
45540 case pragma_objc_icode:
45541 cp_parser_block_declaration (parser, /*statement_p=*/false);
45542 break;
45543 default:
45544 cp_parser_declaration_statement (parser);
45545 break;
45547 if (parser->omp_declare_simd
45548 && !parser->omp_declare_simd->error_seen
45549 && !parser->omp_declare_simd->fndecl_seen)
45550 error_at (pragma_tok->location,
45551 "%<#pragma omp declare %s%> not immediately followed by "
45552 "function declaration or definition",
45553 parser->omp_declare_simd->variant_p ? "variant" : "simd");
45554 data.tokens.release ();
45555 parser->omp_declare_simd = NULL;
45559 static const char *const omp_construct_selectors[] = {
45560 "simd", "target", "teams", "parallel", "for", NULL };
45561 static const char *const omp_device_selectors[] = {
45562 "kind", "isa", "arch", NULL };
45563 static const char *const omp_implementation_selectors[] = {
45564 "vendor", "extension", "atomic_default_mem_order", "unified_address",
45565 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
45566 static const char *const omp_user_selectors[] = {
45567 "condition", NULL };
45569 /* OpenMP 5.0:
45571 trait-selector:
45572 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
45574 trait-score:
45575 score(score-expression) */
45577 static tree
45578 cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
45580 tree ret = NULL_TREE;
45583 tree selector;
45584 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45585 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45586 selector = cp_lexer_peek_token (parser->lexer)->u.value;
45587 else
45589 cp_parser_error (parser, "expected trait selector name");
45590 return error_mark_node;
45593 tree properties = NULL_TREE;
45594 const char *const *selectors = NULL;
45595 bool allow_score = true;
45596 bool allow_user = false;
45597 int property_limit = 0;
45598 enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
45599 CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
45600 CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
45601 switch (IDENTIFIER_POINTER (set)[0])
45603 case 'c': /* construct */
45604 selectors = omp_construct_selectors;
45605 allow_score = false;
45606 property_limit = 1;
45607 property_kind = CTX_PROPERTY_SIMD;
45608 break;
45609 case 'd': /* device */
45610 selectors = omp_device_selectors;
45611 allow_score = false;
45612 allow_user = true;
45613 property_limit = 3;
45614 property_kind = CTX_PROPERTY_NAME_LIST;
45615 break;
45616 case 'i': /* implementation */
45617 selectors = omp_implementation_selectors;
45618 allow_user = true;
45619 property_limit = 3;
45620 property_kind = CTX_PROPERTY_NAME_LIST;
45621 break;
45622 case 'u': /* user */
45623 selectors = omp_user_selectors;
45624 property_limit = 1;
45625 property_kind = CTX_PROPERTY_EXPR;
45626 break;
45627 default:
45628 gcc_unreachable ();
45630 for (int i = 0; ; i++)
45632 if (selectors[i] == NULL)
45634 if (allow_user)
45636 property_kind = CTX_PROPERTY_USER;
45637 break;
45639 else
45641 error ("selector %qs not allowed for context selector "
45642 "set %qs", IDENTIFIER_POINTER (selector),
45643 IDENTIFIER_POINTER (set));
45644 cp_lexer_consume_token (parser->lexer);
45645 return error_mark_node;
45648 if (i == property_limit)
45649 property_kind = CTX_PROPERTY_NONE;
45650 if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
45651 break;
45653 if (property_kind == CTX_PROPERTY_NAME_LIST
45654 && IDENTIFIER_POINTER (set)[0] == 'i'
45655 && strcmp (IDENTIFIER_POINTER (selector),
45656 "atomic_default_mem_order") == 0)
45657 property_kind = CTX_PROPERTY_ID;
45659 cp_lexer_consume_token (parser->lexer);
45661 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
45663 if (property_kind == CTX_PROPERTY_NONE)
45665 error ("selector %qs does not accept any properties",
45666 IDENTIFIER_POINTER (selector));
45667 return error_mark_node;
45670 matching_parens parens;
45671 parens.consume_open (parser);
45673 cp_token *token = cp_lexer_peek_token (parser->lexer);
45674 if (allow_score
45675 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
45676 && strcmp (IDENTIFIER_POINTER (token->u.value), "score") == 0
45677 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
45679 cp_lexer_save_tokens (parser->lexer);
45680 cp_lexer_consume_token (parser->lexer);
45681 cp_lexer_consume_token (parser->lexer);
45682 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
45683 true)
45684 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
45686 cp_lexer_rollback_tokens (parser->lexer);
45687 cp_lexer_consume_token (parser->lexer);
45689 matching_parens parens2;
45690 parens2.require_open (parser);
45691 tree score = cp_parser_constant_expression (parser);
45692 if (!parens2.require_close (parser))
45693 cp_parser_skip_to_closing_parenthesis (parser, true,
45694 false, true);
45695 cp_parser_require (parser, CPP_COLON, RT_COLON);
45696 if (score != error_mark_node)
45698 score = fold_non_dependent_expr (score);
45699 if (value_dependent_expression_p (score))
45700 properties = tree_cons (get_identifier (" score"),
45701 score, properties);
45702 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score))
45703 || TREE_CODE (score) != INTEGER_CST)
45704 error_at (token->location, "score argument must be "
45705 "constant integer expression");
45706 else if (tree_int_cst_sgn (score) < 0)
45707 error_at (token->location, "score argument must be "
45708 "non-negative");
45709 else
45710 properties = tree_cons (get_identifier (" score"),
45711 score, properties);
45714 else
45715 cp_lexer_rollback_tokens (parser->lexer);
45717 token = cp_lexer_peek_token (parser->lexer);
45720 switch (property_kind)
45722 tree t;
45723 case CTX_PROPERTY_USER:
45726 t = cp_parser_constant_expression (parser);
45727 if (t != error_mark_node)
45729 t = fold_non_dependent_expr (t);
45730 if (TREE_CODE (t) == STRING_CST)
45731 properties = tree_cons (NULL_TREE, t, properties);
45732 else if (!value_dependent_expression_p (t)
45733 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45734 || !tree_fits_shwi_p (t)))
45735 error_at (token->location, "property must be "
45736 "constant integer expression or string "
45737 "literal");
45738 else
45739 properties = tree_cons (NULL_TREE, t, properties);
45741 else
45742 return error_mark_node;
45744 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45745 cp_lexer_consume_token (parser->lexer);
45746 else
45747 break;
45749 while (1);
45750 break;
45751 case CTX_PROPERTY_ID:
45752 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45753 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45755 tree prop = cp_lexer_peek_token (parser->lexer)->u.value;
45756 cp_lexer_consume_token (parser->lexer);
45757 properties = tree_cons (prop, NULL_TREE, properties);
45759 else
45761 cp_parser_error (parser, "expected identifier");
45762 return error_mark_node;
45764 break;
45765 case CTX_PROPERTY_NAME_LIST:
45768 tree prop = NULL_TREE, value = NULL_TREE;
45769 if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
45770 || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45772 prop = cp_lexer_peek_token (parser->lexer)->u.value;
45773 cp_lexer_consume_token (parser->lexer);
45775 else if (cp_lexer_next_token_is (parser->lexer, CPP_STRING))
45776 value = cp_parser_string_literal (parser, false, false);
45777 else
45779 cp_parser_error (parser, "expected identifier or "
45780 "string literal");
45781 return error_mark_node;
45784 properties = tree_cons (prop, value, properties);
45786 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45787 cp_lexer_consume_token (parser->lexer);
45788 else
45789 break;
45791 while (1);
45792 break;
45793 case CTX_PROPERTY_EXPR:
45794 t = cp_parser_constant_expression (parser);
45795 if (t != error_mark_node)
45797 t = fold_non_dependent_expr (t);
45798 if (!value_dependent_expression_p (t)
45799 && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
45800 || !tree_fits_shwi_p (t)))
45801 error_at (token->location, "property must be "
45802 "constant integer expression");
45803 else
45804 properties = tree_cons (NULL_TREE, t, properties);
45806 else
45807 return error_mark_node;
45808 break;
45809 case CTX_PROPERTY_SIMD:
45810 if (!has_parms_p)
45812 error_at (token->location, "properties for %<simd%> "
45813 "selector may not be specified in "
45814 "%<metadirective%>");
45815 return error_mark_node;
45817 properties
45818 = cp_parser_omp_all_clauses (parser,
45819 OMP_DECLARE_SIMD_CLAUSE_MASK,
45820 "simd", NULL, true, 2);
45821 break;
45822 default:
45823 gcc_unreachable ();
45826 if (!parens.require_close (parser))
45827 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
45829 properties = nreverse (properties);
45831 else if (property_kind == CTX_PROPERTY_NAME_LIST
45832 || property_kind == CTX_PROPERTY_ID
45833 || property_kind == CTX_PROPERTY_EXPR)
45835 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
45836 return error_mark_node;
45839 ret = tree_cons (selector, properties, ret);
45841 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45842 cp_lexer_consume_token (parser->lexer);
45843 else
45844 break;
45846 while (1);
45848 return nreverse (ret);
45851 /* OpenMP 5.0:
45853 trait-set-selector[,trait-set-selector[,...]]
45855 trait-set-selector:
45856 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
45858 trait-set-selector-name:
45859 constructor
45860 device
45861 implementation
45862 user */
45864 static tree
45865 cp_parser_omp_context_selector_specification (cp_parser *parser,
45866 bool has_parms_p)
45868 tree ret = NULL_TREE;
45871 const char *setp = "";
45872 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45873 setp
45874 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45875 switch (setp[0])
45877 case 'c':
45878 if (strcmp (setp, "construct") == 0)
45879 setp = NULL;
45880 break;
45881 case 'd':
45882 if (strcmp (setp, "device") == 0)
45883 setp = NULL;
45884 break;
45885 case 'i':
45886 if (strcmp (setp, "implementation") == 0)
45887 setp = NULL;
45888 break;
45889 case 'u':
45890 if (strcmp (setp, "user") == 0)
45891 setp = NULL;
45892 break;
45893 default:
45894 break;
45896 if (setp)
45898 cp_parser_error (parser, "expected %<construct%>, %<device%>, "
45899 "%<implementation%> or %<user%>");
45900 return error_mark_node;
45903 tree set = cp_lexer_peek_token (parser->lexer)->u.value;
45904 cp_lexer_consume_token (parser->lexer);
45906 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
45907 return error_mark_node;
45909 matching_braces braces;
45910 if (!braces.require_open (parser))
45911 return error_mark_node;
45913 tree selectors
45914 = cp_parser_omp_context_selector (parser, set, has_parms_p);
45915 if (selectors == error_mark_node)
45917 cp_parser_skip_to_closing_brace (parser);
45918 ret = error_mark_node;
45920 else if (ret != error_mark_node)
45921 ret = tree_cons (set, selectors, ret);
45923 braces.require_close (parser);
45925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
45926 cp_lexer_consume_token (parser->lexer);
45927 else
45928 break;
45930 while (1);
45932 if (ret == error_mark_node)
45933 return ret;
45934 return nreverse (ret);
45937 /* Assumption clauses:
45938 OpenMP 5.1
45939 absent (directive-name-list)
45940 contains (directive-name-list)
45941 holds (expression)
45942 no_openmp
45943 no_openmp_routines
45944 no_parallelism */
45946 static void
45947 cp_parser_omp_assumption_clauses (cp_parser *parser, cp_token *pragma_tok,
45948 bool is_assume)
45950 bool first = true;
45951 bool no_openmp = false;
45952 bool no_openmp_routines = false;
45953 bool no_parallelism = false;
45954 bitmap_head absent_head, contains_head;
45956 bitmap_obstack_initialize (NULL);
45957 bitmap_initialize (&absent_head, &bitmap_default_obstack);
45958 bitmap_initialize (&contains_head, &bitmap_default_obstack);
45960 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
45961 error_at (cp_lexer_peek_token (parser->lexer)->location,
45962 "expected at least one assumption clause");
45964 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
45966 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
45967 if ((!first || parser->lexer->in_omp_attribute_pragma)
45968 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
45969 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
45970 cp_lexer_consume_token (parser->lexer);
45972 first = false;
45974 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
45975 break;
45977 const char *p
45978 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
45979 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
45981 if (!strcmp (p, "no_openmp"))
45983 cp_lexer_consume_token (parser->lexer);
45984 if (no_openmp)
45985 error_at (cloc, "too many %qs clauses", "no_openmp");
45986 no_openmp = true;
45988 else if (!strcmp (p, "no_openmp_routines"))
45990 cp_lexer_consume_token (parser->lexer);
45991 if (no_openmp_routines)
45992 error_at (cloc, "too many %qs clauses", "no_openmp_routines");
45993 no_openmp_routines = true;
45995 else if (!strcmp (p, "no_parallelism"))
45997 cp_lexer_consume_token (parser->lexer);
45998 if (no_parallelism)
45999 error_at (cloc, "too many %qs clauses", "no_parallelism");
46000 no_parallelism = true;
46002 else if (!strcmp (p, "holds"))
46004 cp_lexer_consume_token (parser->lexer);
46005 matching_parens parens;
46006 if (parens.require_open (parser))
46008 location_t eloc = cp_lexer_peek_token (parser->lexer)->location;
46009 tree t = cp_parser_assignment_expression (parser);
46010 if (!type_dependent_expression_p (t))
46011 t = contextual_conv_bool (t, tf_warning_or_error);
46012 if (is_assume && !error_operand_p (t))
46014 t = build_call_expr_internal_loc (eloc, IFN_ASSUME,
46015 void_type_node, 1, t);
46016 finish_expr_stmt (t);
46018 if (!parens.require_close (parser))
46019 cp_parser_skip_to_closing_parenthesis (parser,
46020 /*recovering=*/true,
46021 /*or_comma=*/false,
46022 /*consume_paren=*/true);
46025 else if (!strcmp (p, "absent") || !strcmp (p, "contains"))
46027 cp_lexer_consume_token (parser->lexer);
46028 matching_parens parens;
46029 if (parens.require_open (parser))
46033 const char *directive[3] = {};
46034 int i;
46035 location_t dloc
46036 = cp_lexer_peek_token (parser->lexer)->location;
46037 for (i = 0; i < 3; i++)
46039 tree id;
46040 if (cp_lexer_nth_token_is (parser->lexer, i + 1, CPP_NAME))
46041 id = cp_lexer_peek_nth_token (parser->lexer,
46042 i + 1)->u.value;
46043 else if (cp_lexer_nth_token_is (parser->lexer, i + 1,
46044 CPP_KEYWORD))
46046 enum rid rid
46047 = cp_lexer_peek_nth_token (parser->lexer,
46048 i + 1)->keyword;
46049 id = ridpointers[rid];
46051 else
46052 break;
46053 directive[i] = IDENTIFIER_POINTER (id);
46055 if (i == 0)
46056 error_at (dloc, "expected directive name");
46057 else
46059 const struct c_omp_directive *dir
46060 = c_omp_categorize_directive (directive[0],
46061 directive[1],
46062 directive[2]);
46063 if (dir == NULL
46064 || dir->kind == C_OMP_DIR_DECLARATIVE
46065 || dir->kind == C_OMP_DIR_INFORMATIONAL
46066 || dir->id == PRAGMA_OMP_END
46067 || (!dir->second && directive[1])
46068 || (!dir->third && directive[2]))
46069 error_at (dloc, "unknown OpenMP directive name in "
46070 "%qs clause argument", p);
46071 else
46073 int id = dir - c_omp_directives;
46074 if (bitmap_bit_p (p[0] == 'a' ? &contains_head
46075 : &absent_head, id))
46076 error_at (dloc, "%<%s%s%s%s%s%> directive "
46077 "mentioned in both %<absent%> and "
46078 "%<contains%> clauses",
46079 directive[0],
46080 directive[1] ? " " : "",
46081 directive[1] ? directive[1] : "",
46082 directive[2] ? " " : "",
46083 directive[2] ? directive[2] : "");
46084 else if (!bitmap_set_bit (p[0] == 'a'
46085 ? &absent_head
46086 : &contains_head, id))
46087 error_at (dloc, "%<%s%s%s%s%s%> directive "
46088 "mentioned multiple times in %qs "
46089 "clauses",
46090 directive[0],
46091 directive[1] ? " " : "",
46092 directive[1] ? directive[1] : "",
46093 directive[2] ? " " : "",
46094 directive[2] ? directive[2] : "", p);
46096 for (; i; --i)
46097 cp_lexer_consume_token (parser->lexer);
46099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
46100 cp_lexer_consume_token (parser->lexer);
46101 else
46102 break;
46104 while (1);
46105 if (!parens.require_close (parser))
46106 cp_parser_skip_to_closing_parenthesis (parser,
46107 /*recovering=*/true,
46108 /*or_comma=*/false,
46109 /*consume_paren=*/true);
46112 else if (startswith (p, "ext_"))
46114 warning_at (cloc, 0, "unknown assumption clause %qs", p);
46115 cp_lexer_consume_token (parser->lexer);
46116 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46117 for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
46118 n; --n)
46119 cp_lexer_consume_token (parser->lexer);
46121 else
46123 cp_lexer_consume_token (parser->lexer);
46124 error_at (cloc, "expected assumption clause");
46125 break;
46128 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46131 /* OpenMP 5.1
46132 # pragma omp assume clauses[optseq] new-line */
46134 static void
46135 cp_parser_omp_assume (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
46137 cp_parser_omp_assumption_clauses (parser, pragma_tok, true);
46138 add_stmt (cp_parser_omp_structured_block (parser, if_p));
46141 /* OpenMP 5.1
46142 # pragma omp assumes clauses[optseq] new-line */
46144 static bool
46145 cp_parser_omp_assumes (cp_parser *parser, cp_token *pragma_tok)
46147 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
46148 return false;
46151 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
46152 that into "omp declare variant base" attribute. */
46154 static tree
46155 cp_finish_omp_declare_variant (cp_parser *parser, cp_token *pragma_tok,
46156 tree attrs)
46158 matching_parens parens;
46159 if (!parens.require_open (parser))
46161 fail:
46162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46163 return attrs;
46166 bool template_p;
46167 cp_id_kind idk = CP_ID_KIND_NONE;
46168 cp_token *varid_token = cp_lexer_peek_token (parser->lexer);
46169 cp_expr varid
46170 = cp_parser_id_expression (parser, /*template_keyword_p=*/false,
46171 /*check_dependency_p=*/true,
46172 /*template_p=*/&template_p,
46173 /*declarator_p=*/false,
46174 /*optional_p=*/false);
46175 parens.require_close (parser);
46177 tree variant;
46178 if (TREE_CODE (varid) == TEMPLATE_ID_EXPR
46179 || TREE_CODE (varid) == TYPE_DECL
46180 || varid == error_mark_node)
46181 variant = varid;
46182 else if (varid_token->type == CPP_NAME && varid_token->error_reported)
46183 variant = NULL_TREE;
46184 else
46186 tree ambiguous_decls;
46187 variant = cp_parser_lookup_name (parser, varid, none_type,
46188 template_p, /*is_namespace=*/false,
46189 /*check_dependency=*/true,
46190 &ambiguous_decls,
46191 varid.get_location ());
46192 if (ambiguous_decls)
46193 variant = NULL_TREE;
46195 if (variant == NULL_TREE)
46196 variant = error_mark_node;
46197 else if (TREE_CODE (variant) != SCOPE_REF)
46199 const char *error_msg;
46200 variant
46201 = finish_id_expression (varid, variant, parser->scope,
46202 &idk, false, true,
46203 &parser->non_integral_constant_expression_p,
46204 template_p, true, false, false, &error_msg,
46205 varid.get_location ());
46206 if (error_msg)
46207 cp_parser_error (parser, error_msg);
46209 location_t caret_loc = get_pure_location (varid.get_location ());
46210 location_t start_loc = get_start (varid_token->location);
46211 location_t finish_loc = get_finish (varid.get_location ());
46212 location_t varid_loc = make_location (caret_loc, start_loc, finish_loc);
46214 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46215 if (parser->lexer->in_omp_attribute_pragma
46216 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46217 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46218 cp_lexer_consume_token (parser->lexer);
46220 const char *clause = "";
46221 location_t match_loc = cp_lexer_peek_token (parser->lexer)->location;
46222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46223 clause = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
46224 if (strcmp (clause, "match"))
46226 cp_parser_error (parser, "expected %<match%>");
46227 goto fail;
46230 cp_lexer_consume_token (parser->lexer);
46232 if (!parens.require_open (parser))
46233 goto fail;
46235 tree ctx = cp_parser_omp_context_selector_specification (parser, true);
46236 if (ctx == error_mark_node)
46237 goto fail;
46238 ctx = omp_check_context_selector (match_loc, ctx);
46239 if (ctx != error_mark_node && variant != error_mark_node)
46241 tree match_loc_node = maybe_wrap_with_location (integer_zero_node,
46242 match_loc);
46243 tree loc_node = maybe_wrap_with_location (integer_zero_node, varid_loc);
46244 loc_node = tree_cons (match_loc_node,
46245 build_int_cst (integer_type_node, idk),
46246 build_tree_list (loc_node, integer_zero_node));
46247 attrs = tree_cons (get_identifier ("omp declare variant base"),
46248 tree_cons (variant, ctx, loc_node), attrs);
46249 if (processing_template_decl)
46250 ATTR_IS_DEPENDENT (attrs) = 1;
46253 parens.require_close (parser);
46254 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46255 return attrs;
46259 /* Finalize #pragma omp declare simd clauses after direct declarator has
46260 been parsed, and put that into "omp declare simd" attribute. */
46262 static tree
46263 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
46265 struct cp_token_cache *ce;
46266 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
46267 int i;
46269 if (!data->error_seen && data->fndecl_seen)
46271 error ("%<#pragma omp declare %s%> not immediately followed by "
46272 "a single function declaration or definition",
46273 data->variant_p ? "variant" : "simd");
46274 data->error_seen = true;
46276 if (data->error_seen)
46277 return attrs;
46279 FOR_EACH_VEC_ELT (data->tokens, i, ce)
46281 tree c, cl;
46283 cp_parser_push_lexer_for_tokens (parser, ce);
46284 parser->lexer->in_pragma = true;
46285 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
46286 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46287 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46288 const char *kind = IDENTIFIER_POINTER (id);
46289 cp_lexer_consume_token (parser->lexer);
46290 if (strcmp (kind, "simd") == 0)
46292 /* For now only in C++ attributes, do it always for OpenMP 5.1.
46293 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46294 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46295 cp_lexer_consume_token (parser->lexer); */
46297 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
46298 "#pragma omp declare simd",
46299 pragma_tok);
46300 if (cl)
46301 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
46302 c = build_tree_list (get_identifier ("omp declare simd"), cl);
46303 TREE_CHAIN (c) = attrs;
46304 if (processing_template_decl)
46305 ATTR_IS_DEPENDENT (c) = 1;
46306 attrs = c;
46308 else
46310 gcc_assert (strcmp (kind, "variant") == 0);
46311 attrs
46312 = cp_finish_omp_declare_variant (parser, pragma_tok, attrs);
46314 cp_parser_pop_lexer (parser);
46317 cp_lexer *lexer = NULL;
46318 for (int i = 0; i < 2; i++)
46320 if (data->attribs[i] == NULL)
46321 continue;
46322 for (tree *pa = data->attribs[i]; *pa; )
46323 if (get_attribute_namespace (*pa) == omp_identifier
46324 && is_attribute_p ("directive", get_attribute_name (*pa)))
46326 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a))
46328 tree d = TREE_VALUE (a);
46329 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE);
46330 cp_token *first = DEFPARSE_TOKENS (d)->first;
46331 cp_token *last = DEFPARSE_TOKENS (d)->last;
46332 const char *directive[3] = {};
46333 for (int j = 0; j < 3; j++)
46335 tree id = NULL_TREE;
46336 if (first + j == last)
46337 break;
46338 if (first[j].type == CPP_NAME)
46339 id = first[j].u.value;
46340 else if (first[j].type == CPP_KEYWORD)
46341 id = ridpointers[(int) first[j].keyword];
46342 else
46343 break;
46344 directive[j] = IDENTIFIER_POINTER (id);
46346 const c_omp_directive *dir = NULL;
46347 if (directive[0])
46348 dir = c_omp_categorize_directive (directive[0], directive[1],
46349 directive[2]);
46350 if (dir == NULL)
46352 error_at (first->location,
46353 "unknown OpenMP directive name in "
46354 "%<omp::directive%> attribute argument");
46355 continue;
46357 if (dir->id != PRAGMA_OMP_DECLARE
46358 || (strcmp (directive[1], "simd") != 0
46359 && strcmp (directive[1], "variant") != 0))
46361 error_at (first->location,
46362 "OpenMP directive other than %<declare simd%> "
46363 "or %<declare variant%> appertains to a "
46364 "declaration");
46365 continue;
46368 if (parser->omp_attrs_forbidden_p)
46370 error_at (first->location,
46371 "mixing OpenMP directives with attribute and "
46372 "pragma syntax on the same statement");
46373 parser->omp_attrs_forbidden_p = false;
46376 if (!flag_openmp && strcmp (directive[1], "simd") != 0)
46377 continue;
46378 if (lexer == NULL)
46380 lexer = cp_lexer_alloc ();
46381 lexer->debugging_p = parser->lexer->debugging_p;
46383 vec_safe_reserve (lexer->buffer, (last - first) + 2);
46384 cp_token tok = {};
46385 tok.type = CPP_PRAGMA;
46386 tok.keyword = RID_MAX;
46387 tok.u.value = build_int_cst (NULL, PRAGMA_OMP_DECLARE);
46388 tok.location = first->location;
46389 lexer->buffer->quick_push (tok);
46390 while (++first < last)
46391 lexer->buffer->quick_push (*first);
46392 tok = {};
46393 tok.type = CPP_PRAGMA_EOL;
46394 tok.keyword = RID_MAX;
46395 tok.location = last->location;
46396 lexer->buffer->quick_push (tok);
46397 tok = {};
46398 tok.type = CPP_EOF;
46399 tok.keyword = RID_MAX;
46400 tok.location = last->location;
46401 lexer->buffer->quick_push (tok);
46402 lexer->next = parser->lexer;
46403 lexer->next_token = lexer->buffer->address ();
46404 lexer->last_token = lexer->next_token
46405 + lexer->buffer->length ()
46406 - 1;
46407 lexer->in_omp_attribute_pragma = true;
46408 parser->lexer = lexer;
46409 /* Move the current source position to that of the first token
46410 in the new lexer. */
46411 cp_lexer_set_source_position_from_token (lexer->next_token);
46413 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
46414 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46415 const char *kind = IDENTIFIER_POINTER (id);
46416 cp_lexer_consume_token (parser->lexer);
46418 tree c, cl;
46419 if (strcmp (kind, "simd") == 0)
46421 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46422 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46423 cp_lexer_consume_token (parser->lexer);
46425 omp_clause_mask mask = OMP_DECLARE_SIMD_CLAUSE_MASK;
46426 cl = cp_parser_omp_all_clauses (parser, mask,
46427 "#pragma omp declare simd",
46428 pragma_tok);
46429 if (cl)
46430 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
46431 c = build_tree_list (get_identifier ("omp declare simd"),
46432 cl);
46433 TREE_CHAIN (c) = attrs;
46434 if (processing_template_decl)
46435 ATTR_IS_DEPENDENT (c) = 1;
46436 attrs = c;
46438 else
46440 gcc_assert (strcmp (kind, "variant") == 0);
46441 attrs
46442 = cp_finish_omp_declare_variant (parser, pragma_tok,
46443 attrs);
46445 gcc_assert (parser->lexer != lexer);
46446 vec_safe_truncate (lexer->buffer, 0);
46448 *pa = TREE_CHAIN (*pa);
46450 else
46451 pa = &TREE_CHAIN (*pa);
46453 if (lexer)
46454 cp_lexer_destroy (lexer);
46456 data->fndecl_seen = true;
46457 return attrs;
46460 /* Helper for cp_parser_omp_declare_target, handle one to or link clause
46461 on #pragma omp declare target. Return false if errors were reported. */
46463 static bool
46464 handle_omp_declare_target_clause (tree c, tree t, int device_type)
46466 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
46467 tree at2 = lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t));
46468 tree id;
46469 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
46471 id = get_identifier ("omp declare target link");
46472 std::swap (at1, at2);
46474 else
46475 id = get_identifier ("omp declare target");
46476 if (at2)
46478 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ENTER)
46479 error_at (OMP_CLAUSE_LOCATION (c),
46480 "%qD specified both in declare target %<link%> and %qs"
46481 " clauses", t, OMP_CLAUSE_ENTER_TO (c) ? "to" : "enter");
46482 else
46483 error_at (OMP_CLAUSE_LOCATION (c),
46484 "%qD specified both in declare target %<link%> and "
46485 "%<to%> or %<enter%> clauses", t);
46486 return false;
46488 if (!at1)
46490 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46491 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
46492 return true;
46494 symtab_node *node = symtab_node::get (t);
46495 if (node != NULL)
46497 node->offloadable = 1;
46498 if (ENABLE_OFFLOADING)
46500 g->have_offload = true;
46501 if (is_a <varpool_node *> (node))
46502 vec_safe_push (offload_vars, t);
46506 if (TREE_CODE (t) != FUNCTION_DECL)
46507 return true;
46508 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0)
46510 tree at3 = lookup_attribute ("omp declare target host",
46511 DECL_ATTRIBUTES (t));
46512 if (at3 == NULL_TREE)
46514 id = get_identifier ("omp declare target host");
46515 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46518 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0)
46520 tree at3 = lookup_attribute ("omp declare target nohost",
46521 DECL_ATTRIBUTES (t));
46522 if (at3 == NULL_TREE)
46524 id = get_identifier ("omp declare target nohost");
46525 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
46528 return true;
46531 /* OpenMP 4.0:
46532 # pragma omp declare target new-line
46533 declarations and definitions
46534 # pragma omp end declare target new-line
46536 OpenMP 4.5:
46537 # pragma omp declare target ( extended-list ) new-line
46539 # pragma omp declare target declare-target-clauses[seq] new-line */
46541 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
46542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
46543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ENTER) \
46544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
46545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
46547 static void
46548 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
46550 tree clauses = NULL_TREE;
46551 int device_type = 0;
46552 bool only_device_type = true;
46553 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
46554 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46555 || (parser->lexer->in_omp_attribute_pragma
46556 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46557 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME)))
46558 clauses
46559 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
46560 "#pragma omp declare target", pragma_tok);
46561 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
46563 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_ENTER,
46564 clauses);
46565 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
46566 cp_parser_require_pragma_eol (parser, pragma_tok);
46568 else
46570 cp_omp_declare_target_attr a
46571 = { parser->lexer->in_omp_attribute_pragma, -1 };
46572 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
46573 cp_parser_require_pragma_eol (parser, pragma_tok);
46574 return;
46576 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46577 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
46578 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
46579 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46581 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
46582 continue;
46583 tree t = OMP_CLAUSE_DECL (c);
46584 only_device_type = false;
46585 if (!handle_omp_declare_target_clause (c, t, device_type))
46586 continue;
46587 if (VAR_OR_FUNCTION_DECL_P (t)
46588 && DECL_LOCAL_DECL_P (t)
46589 && DECL_LANG_SPECIFIC (t)
46590 && DECL_LOCAL_DECL_ALIAS (t)
46591 && DECL_LOCAL_DECL_ALIAS (t) != error_mark_node)
46592 handle_omp_declare_target_clause (c, DECL_LOCAL_DECL_ALIAS (t),
46593 device_type);
46595 if (device_type && only_device_type)
46596 error_at (OMP_CLAUSE_LOCATION (clauses),
46597 "directive with only %<device_type%> clause");
46600 /* OpenMP 5.1
46601 # pragma omp begin assumes clauses[optseq] new-line
46603 # pragma omp begin declare target clauses[optseq] new-line */
46605 #define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \
46606 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE)
46608 static void
46609 cp_parser_omp_begin (cp_parser *parser, cp_token *pragma_tok)
46611 const char *p = "";
46612 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
46613 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46615 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46616 p = IDENTIFIER_POINTER (id);
46618 if (strcmp (p, "declare") == 0)
46620 cp_lexer_consume_token (parser->lexer);
46621 p = "";
46622 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46624 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46625 p = IDENTIFIER_POINTER (id);
46627 if (strcmp (p, "target") == 0)
46629 cp_lexer_consume_token (parser->lexer);
46630 tree clauses
46631 = cp_parser_omp_all_clauses (parser,
46632 OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK,
46633 "#pragma omp begin declare target",
46634 pragma_tok);
46635 int device_type = 0;
46636 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
46637 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE)
46638 device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c);
46639 cp_omp_declare_target_attr a
46640 = { in_omp_attribute_pragma, device_type };
46641 vec_safe_push (scope_chain->omp_declare_target_attribute, a);
46643 else
46645 cp_parser_error (parser, "expected %<target%>");
46646 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46649 else if (strcmp (p, "assumes") == 0)
46651 cp_lexer_consume_token (parser->lexer);
46652 cp_parser_omp_assumption_clauses (parser, pragma_tok, false);
46653 cp_omp_begin_assumes_data a = { in_omp_attribute_pragma };
46654 vec_safe_push (scope_chain->omp_begin_assumes, a);
46656 else
46658 cp_parser_error (parser, "expected %<declare target%> or %<assumes%>");
46659 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46663 /* OpenMP 4.0:
46664 # pragma omp end declare target new-line
46666 OpenMP 5.1:
46667 # pragma omp end assumes new-line */
46669 static void
46670 cp_parser_omp_end (cp_parser *parser, cp_token *pragma_tok)
46672 const char *p = "";
46673 bool in_omp_attribute_pragma = parser->lexer->in_omp_attribute_pragma;
46674 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46676 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46677 p = IDENTIFIER_POINTER (id);
46679 if (strcmp (p, "declare") == 0)
46681 cp_lexer_consume_token (parser->lexer);
46682 p = "";
46683 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46685 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46686 p = IDENTIFIER_POINTER (id);
46688 if (strcmp (p, "target") == 0)
46689 cp_lexer_consume_token (parser->lexer);
46690 else
46692 cp_parser_error (parser, "expected %<target%>");
46693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46694 return;
46696 cp_parser_require_pragma_eol (parser, pragma_tok);
46697 if (!vec_safe_length (scope_chain->omp_declare_target_attribute))
46698 error_at (pragma_tok->location,
46699 "%<#pragma omp end declare target%> without corresponding "
46700 "%<#pragma omp declare target%> or "
46701 "%<#pragma omp begin declare target%>");
46702 else
46704 cp_omp_declare_target_attr
46705 a = scope_chain->omp_declare_target_attribute->pop ();
46706 if (a.attr_syntax != in_omp_attribute_pragma)
46708 if (a.attr_syntax)
46709 error_at (pragma_tok->location,
46710 "%qs in attribute syntax terminated "
46711 "with %qs in pragma syntax",
46712 a.device_type >= 0 ? "begin declare target"
46713 : "declare target",
46714 "end declare target");
46715 else
46716 error_at (pragma_tok->location,
46717 "%qs in pragma syntax terminated "
46718 "with %qs in attribute syntax",
46719 a.device_type >= 0 ? "begin declare target"
46720 : "declare target",
46721 "end declare target");
46725 else if (strcmp (p, "assumes") == 0)
46727 cp_lexer_consume_token (parser->lexer);
46728 cp_parser_require_pragma_eol (parser, pragma_tok);
46729 if (!vec_safe_length (scope_chain->omp_begin_assumes))
46730 error_at (pragma_tok->location,
46731 "%qs without corresponding %qs",
46732 "#pragma omp end assumes", "#pragma omp begin assumes");
46733 else
46735 cp_omp_begin_assumes_data
46736 a = scope_chain->omp_begin_assumes->pop ();
46737 if (a.attr_syntax != in_omp_attribute_pragma)
46739 if (a.attr_syntax)
46740 error_at (pragma_tok->location,
46741 "%qs in attribute syntax terminated "
46742 "with %qs in pragma syntax",
46743 "begin assumes", "end assumes");
46744 else
46745 error_at (pragma_tok->location,
46746 "%qs in pragma syntax terminated "
46747 "with %qs in attribute syntax",
46748 "begin assumes", "end assumes");
46752 else
46754 cp_parser_error (parser, "expected %<declare%> or %<assumes%>");
46755 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
46756 return;
46760 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
46761 expression and optional initializer clause of
46762 #pragma omp declare reduction. We store the expression(s) as
46763 either 3, 6 or 7 special statements inside of the artificial function's
46764 body. The first two statements are DECL_EXPRs for the artificial
46765 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
46766 expression that uses those variables.
46767 If there was any INITIALIZER clause, this is followed by further statements,
46768 the fourth and fifth statements are DECL_EXPRs for the artificial
46769 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
46770 constructor variant (first token after open paren is not omp_priv),
46771 then the sixth statement is a statement with the function call expression
46772 that uses the OMP_PRIV and optionally OMP_ORIG variable.
46773 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
46774 to initialize the OMP_PRIV artificial variable and there is seventh
46775 statement, a DECL_EXPR of the OMP_PRIV statement again. */
46777 static bool
46778 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
46780 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
46781 gcc_assert (TYPE_REF_P (type));
46782 type = TREE_TYPE (type);
46783 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
46784 DECL_ARTIFICIAL (omp_out) = 1;
46785 pushdecl (omp_out);
46786 add_decl_expr (omp_out);
46787 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
46788 DECL_ARTIFICIAL (omp_in) = 1;
46789 pushdecl (omp_in);
46790 add_decl_expr (omp_in);
46791 tree combiner;
46792 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
46794 keep_next_level (true);
46795 tree block = begin_omp_structured_block ();
46796 combiner = cp_parser_expression (parser);
46797 finish_expr_stmt (combiner);
46798 block = finish_omp_structured_block (block);
46799 if (processing_template_decl)
46800 block = build_stmt (input_location, EXPR_STMT, block);
46801 add_stmt (block);
46803 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
46804 return false;
46806 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
46807 if (parser->lexer->in_omp_attribute_pragma
46808 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
46809 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
46810 cp_lexer_consume_token (parser->lexer);
46812 const char *p = "";
46813 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46815 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46816 p = IDENTIFIER_POINTER (id);
46819 if (strcmp (p, "initializer") == 0)
46821 cp_lexer_consume_token (parser->lexer);
46822 matching_parens parens;
46823 if (!parens.require_open (parser))
46824 return false;
46826 p = "";
46827 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
46829 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
46830 p = IDENTIFIER_POINTER (id);
46833 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
46834 DECL_ARTIFICIAL (omp_priv) = 1;
46835 pushdecl (omp_priv);
46836 add_decl_expr (omp_priv);
46837 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
46838 DECL_ARTIFICIAL (omp_orig) = 1;
46839 pushdecl (omp_orig);
46840 add_decl_expr (omp_orig);
46842 keep_next_level (true);
46843 block = begin_omp_structured_block ();
46845 bool ctor = false;
46846 if (strcmp (p, "omp_priv") == 0)
46848 bool is_direct_init, is_non_constant_init;
46849 ctor = true;
46850 cp_lexer_consume_token (parser->lexer);
46851 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
46852 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
46853 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46854 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
46855 == CPP_CLOSE_PAREN
46856 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
46857 == CPP_CLOSE_PAREN))
46859 finish_omp_structured_block (block);
46860 error ("invalid initializer clause");
46861 return false;
46863 initializer = cp_parser_initializer (parser, &is_direct_init,
46864 &is_non_constant_init);
46865 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
46866 NULL_TREE, LOOKUP_ONLYCONVERTING);
46868 else
46870 cp_parser_parse_tentatively (parser);
46871 /* Don't create location wrapper nodes here. */
46872 auto_suppress_location_wrappers sentinel;
46873 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
46874 /*check_dependency_p=*/true,
46875 /*template_p=*/NULL,
46876 /*declarator_p=*/false,
46877 /*optional_p=*/false);
46878 vec<tree, va_gc> *args;
46879 if (fn_name == error_mark_node
46880 || cp_parser_error_occurred (parser)
46881 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
46882 || ((args = cp_parser_parenthesized_expression_list
46883 (parser, non_attr, /*cast_p=*/false,
46884 /*allow_expansion_p=*/true,
46885 /*non_constant_p=*/NULL)),
46886 cp_parser_error_occurred (parser)))
46888 finish_omp_structured_block (block);
46889 cp_parser_abort_tentative_parse (parser);
46890 cp_parser_error (parser, "expected id-expression (arguments)");
46891 return false;
46893 unsigned int i;
46894 tree arg;
46895 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
46896 if (arg == omp_priv
46897 || (TREE_CODE (arg) == ADDR_EXPR
46898 && TREE_OPERAND (arg, 0) == omp_priv))
46899 break;
46900 cp_parser_abort_tentative_parse (parser);
46901 if (arg == NULL_TREE)
46902 error ("one of the initializer call arguments should be %<omp_priv%>"
46903 " or %<&omp_priv%>");
46904 initializer = cp_parser_postfix_expression (parser, false, false, false,
46905 false, NULL);
46906 finish_expr_stmt (initializer);
46909 block = finish_omp_structured_block (block);
46910 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
46911 if (processing_template_decl)
46912 block = build_stmt (input_location, EXPR_STMT, block);
46913 add_stmt (block);
46915 if (ctor)
46916 add_decl_expr (omp_orig);
46918 if (!parens.require_close (parser))
46919 return false;
46922 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
46923 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
46924 UNKNOWN_LOCATION);
46926 return true;
46929 /* OpenMP 4.0
46930 #pragma omp declare reduction (reduction-id : typename-list : expression) \
46931 initializer-clause[opt] new-line
46933 initializer-clause:
46934 initializer (omp_priv initializer)
46935 initializer (function-name (argument-list)) */
46937 static void
46938 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
46939 enum pragma_context)
46941 auto_vec<tree> types;
46942 enum tree_code reduc_code = ERROR_MARK;
46943 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
46944 unsigned int i;
46945 cp_token *first_token;
46946 cp_token_cache *cp;
46947 int errs;
46948 void *p;
46950 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
46951 p = obstack_alloc (&declarator_obstack, 0);
46953 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
46954 goto fail;
46956 switch (cp_lexer_peek_token (parser->lexer)->type)
46958 case CPP_PLUS:
46959 reduc_code = PLUS_EXPR;
46960 break;
46961 case CPP_MULT:
46962 reduc_code = MULT_EXPR;
46963 break;
46964 case CPP_MINUS:
46965 reduc_code = MINUS_EXPR;
46966 break;
46967 case CPP_AND:
46968 reduc_code = BIT_AND_EXPR;
46969 break;
46970 case CPP_XOR:
46971 reduc_code = BIT_XOR_EXPR;
46972 break;
46973 case CPP_OR:
46974 reduc_code = BIT_IOR_EXPR;
46975 break;
46976 case CPP_AND_AND:
46977 reduc_code = TRUTH_ANDIF_EXPR;
46978 break;
46979 case CPP_OR_OR:
46980 reduc_code = TRUTH_ORIF_EXPR;
46981 break;
46982 case CPP_NAME:
46983 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
46984 break;
46985 default:
46986 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
46987 "%<|%>, %<&&%>, %<||%> or identifier");
46988 goto fail;
46991 if (reduc_code != ERROR_MARK)
46992 cp_lexer_consume_token (parser->lexer);
46994 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
46995 if (reduc_id == error_mark_node)
46996 goto fail;
46998 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
46999 goto fail;
47001 /* Types may not be defined in declare reduction type list. */
47002 const char *saved_message;
47003 saved_message = parser->type_definition_forbidden_message;
47004 parser->type_definition_forbidden_message
47005 = G_("types may not be defined in declare reduction type list");
47006 bool saved_colon_corrects_to_scope_p;
47007 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
47008 parser->colon_corrects_to_scope_p = false;
47009 bool saved_colon_doesnt_start_class_def_p;
47010 saved_colon_doesnt_start_class_def_p
47011 = parser->colon_doesnt_start_class_def_p;
47012 parser->colon_doesnt_start_class_def_p = true;
47014 while (true)
47016 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47017 type = cp_parser_type_id (parser);
47018 if (type == error_mark_node)
47020 else if (ARITHMETIC_TYPE_P (type)
47021 && (orig_reduc_id == NULL_TREE
47022 || (TREE_CODE (type) != COMPLEX_TYPE
47023 && (id_equal (orig_reduc_id, "min")
47024 || id_equal (orig_reduc_id, "max")))))
47025 error_at (loc, "predeclared arithmetic type %qT in "
47026 "%<#pragma omp declare reduction%>", type);
47027 else if (FUNC_OR_METHOD_TYPE_P (type)
47028 || TREE_CODE (type) == ARRAY_TYPE)
47029 error_at (loc, "function or array type %qT in "
47030 "%<#pragma omp declare reduction%>", type);
47031 else if (TYPE_REF_P (type))
47032 error_at (loc, "reference type %qT in "
47033 "%<#pragma omp declare reduction%>", type);
47034 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
47035 error_at (loc, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
47036 "type %qT in %<#pragma omp declare reduction%>", type);
47037 else
47038 types.safe_push (type);
47040 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
47041 cp_lexer_consume_token (parser->lexer);
47042 else
47043 break;
47046 /* Restore the saved message. */
47047 parser->type_definition_forbidden_message = saved_message;
47048 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
47049 parser->colon_doesnt_start_class_def_p
47050 = saved_colon_doesnt_start_class_def_p;
47052 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
47053 || types.is_empty ())
47055 fail:
47056 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47057 goto done;
47060 first_token = cp_lexer_peek_token (parser->lexer);
47061 cp = NULL;
47062 errs = errorcount;
47063 FOR_EACH_VEC_ELT (types, i, type)
47065 tree fntype
47066 = build_function_type_list (void_type_node,
47067 cp_build_reference_type (type, false),
47068 NULL_TREE);
47069 tree this_reduc_id = reduc_id;
47070 if (!dependent_type_p (type))
47071 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
47072 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
47073 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
47074 DECL_ARTIFICIAL (fndecl) = 1;
47075 DECL_EXTERNAL (fndecl) = 1;
47076 DECL_DECLARED_INLINE_P (fndecl) = 1;
47077 DECL_IGNORED_P (fndecl) = 1;
47078 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
47079 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
47080 DECL_ATTRIBUTES (fndecl)
47081 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
47082 DECL_ATTRIBUTES (fndecl));
47083 bool block_scope = false;
47084 if (current_function_decl)
47086 block_scope = true;
47087 DECL_CONTEXT (fndecl) = current_function_decl;
47088 DECL_LOCAL_DECL_P (fndecl) = true;
47091 if (processing_template_decl)
47092 fndecl = push_template_decl (fndecl);
47094 if (block_scope)
47096 if (!processing_template_decl)
47097 pushdecl (fndecl);
47099 else if (current_class_type)
47101 if (cp == NULL)
47103 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47104 cp_lexer_consume_token (parser->lexer);
47105 cp = cp_token_cache_new (first_token,
47106 cp_lexer_peek_nth_token (parser->lexer,
47107 2));
47109 DECL_STATIC_FUNCTION_P (fndecl) = 1;
47110 finish_member_declaration (fndecl);
47111 DECL_PENDING_INLINE_INFO (fndecl) = cp;
47112 DECL_PENDING_INLINE_P (fndecl) = 1;
47113 vec_safe_push (unparsed_funs_with_definitions, fndecl);
47114 continue;
47116 else
47118 DECL_CONTEXT (fndecl) = current_namespace;
47119 tree d = pushdecl (fndecl);
47120 /* We should never meet a matched duplicate decl. */
47121 gcc_checking_assert (d == error_mark_node || d == fndecl);
47124 tree block = NULL_TREE;
47125 if (!block_scope)
47126 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
47127 else
47128 block = begin_omp_structured_block ();
47129 if (cp)
47131 cp_parser_push_lexer_for_tokens (parser, cp);
47132 parser->lexer->in_pragma = true;
47135 bool ok = cp_parser_omp_declare_reduction_exprs (fndecl, parser);
47137 if (cp)
47138 cp_parser_pop_lexer (parser);
47139 if (!block_scope)
47140 finish_function (/*inline_p=*/false);
47141 else
47143 DECL_CONTEXT (fndecl) = current_function_decl;
47144 if (DECL_TEMPLATE_INFO (fndecl))
47145 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl)) = current_function_decl;
47147 if (!ok)
47148 goto fail;
47150 if (block_scope)
47152 block = finish_omp_structured_block (block);
47153 if (TREE_CODE (block) == BIND_EXPR)
47154 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
47155 else if (TREE_CODE (block) == STATEMENT_LIST)
47156 DECL_SAVED_TREE (fndecl) = block;
47157 if (processing_template_decl)
47158 add_decl_expr (fndecl);
47161 cp_check_omp_declare_reduction (fndecl);
47162 if (cp == NULL && types.length () > 1)
47163 cp = cp_token_cache_new (first_token,
47164 cp_lexer_peek_nth_token (parser->lexer, 2));
47165 if (errs != errorcount)
47166 break;
47169 cp_parser_require_pragma_eol (parser, pragma_tok);
47171 done:
47172 /* Free any declarators allocated. */
47173 obstack_free (&declarator_obstack, p);
47176 /* OpenMP 4.0
47177 #pragma omp declare simd declare-simd-clauses[optseq] new-line
47178 #pragma omp declare reduction (reduction-id : typename-list : expression) \
47179 initializer-clause[opt] new-line
47180 #pragma omp declare target new-line
47182 OpenMP 5.0
47183 #pragma omp declare variant (identifier) match (context-selector) */
47185 static bool
47186 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
47187 enum pragma_context context)
47189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47191 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47192 const char *p = IDENTIFIER_POINTER (id);
47194 if (strcmp (p, "simd") == 0)
47196 cp_lexer_consume_token (parser->lexer);
47197 cp_parser_omp_declare_simd (parser, pragma_tok,
47198 context, false);
47199 return true;
47201 if (flag_openmp && strcmp (p, "variant") == 0)
47203 cp_lexer_consume_token (parser->lexer);
47204 cp_parser_omp_declare_simd (parser, pragma_tok,
47205 context, true);
47206 return true;
47208 cp_ensure_no_omp_declare_simd (parser);
47209 if (strcmp (p, "reduction") == 0)
47211 cp_lexer_consume_token (parser->lexer);
47212 cp_parser_omp_declare_reduction (parser, pragma_tok,
47213 context);
47214 return false;
47216 if (!flag_openmp) /* flag_openmp_simd */
47218 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47219 return false;
47221 if (strcmp (p, "target") == 0)
47223 cp_lexer_consume_token (parser->lexer);
47224 cp_parser_omp_declare_target (parser, pragma_tok);
47225 return false;
47228 cp_parser_error (parser, "expected %<simd%>, %<reduction%>, "
47229 "%<target%> or %<variant%>");
47230 cp_parser_require_pragma_eol (parser, pragma_tok);
47231 return false;
47234 /* OpenMP 5.0
47235 #pragma omp requires clauses[optseq] new-line */
47237 static bool
47238 cp_parser_omp_requires (cp_parser *parser, cp_token *pragma_tok)
47240 bool first = true;
47241 enum omp_requires new_req = (enum omp_requires) 0;
47243 location_t loc = pragma_tok->location;
47244 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47246 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
47247 if ((!first || parser->lexer->in_omp_attribute_pragma)
47248 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47249 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47250 cp_lexer_consume_token (parser->lexer);
47252 first = false;
47254 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47256 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47257 const char *p = IDENTIFIER_POINTER (id);
47258 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47259 enum omp_requires this_req = (enum omp_requires) 0;
47261 if (!strcmp (p, "unified_address"))
47262 this_req = OMP_REQUIRES_UNIFIED_ADDRESS;
47263 else if (!strcmp (p, "unified_shared_memory"))
47264 this_req = OMP_REQUIRES_UNIFIED_SHARED_MEMORY;
47265 else if (!strcmp (p, "dynamic_allocators"))
47266 this_req = OMP_REQUIRES_DYNAMIC_ALLOCATORS;
47267 else if (!strcmp (p, "reverse_offload"))
47268 this_req = OMP_REQUIRES_REVERSE_OFFLOAD;
47269 else if (!strcmp (p, "atomic_default_mem_order"))
47271 cp_lexer_consume_token (parser->lexer);
47273 matching_parens parens;
47274 if (parens.require_open (parser))
47276 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47278 id = cp_lexer_peek_token (parser->lexer)->u.value;
47279 p = IDENTIFIER_POINTER (id);
47281 if (!strcmp (p, "seq_cst"))
47282 this_req
47283 = (enum omp_requires) OMP_MEMORY_ORDER_SEQ_CST;
47284 else if (!strcmp (p, "relaxed"))
47285 this_req
47286 = (enum omp_requires) OMP_MEMORY_ORDER_RELAXED;
47287 else if (!strcmp (p, "acq_rel"))
47288 this_req
47289 = (enum omp_requires) OMP_MEMORY_ORDER_ACQ_REL;
47291 if (this_req == 0)
47293 error_at (cp_lexer_peek_token (parser->lexer)->location,
47294 "expected %<seq_cst%>, %<relaxed%> or "
47295 "%<acq_rel%>");
47296 switch (cp_lexer_peek_token (parser->lexer)->type)
47298 case CPP_EOF:
47299 case CPP_PRAGMA_EOL:
47300 case CPP_CLOSE_PAREN:
47301 break;
47302 default:
47303 if (cp_lexer_nth_token_is (parser->lexer, 2,
47304 CPP_CLOSE_PAREN))
47305 cp_lexer_consume_token (parser->lexer);
47306 break;
47309 else
47310 cp_lexer_consume_token (parser->lexer);
47312 if (!parens.require_close (parser))
47313 cp_parser_skip_to_closing_parenthesis (parser,
47314 /*recovering=*/true,
47315 /*or_comma=*/false,
47316 /*consume_paren=*/
47317 true);
47319 if (this_req == 0)
47321 cp_parser_require_pragma_eol (parser, pragma_tok);
47322 return false;
47325 p = NULL;
47327 else
47329 error_at (cloc, "expected %<unified_address%>, "
47330 "%<unified_shared_memory%>, "
47331 "%<dynamic_allocators%>, "
47332 "%<reverse_offload%> "
47333 "or %<atomic_default_mem_order%> clause");
47334 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47335 return false;
47337 if (p)
47338 cp_lexer_consume_token (parser->lexer);
47339 if (this_req)
47341 if ((this_req & ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47343 if ((this_req & new_req) != 0)
47344 error_at (cloc, "too many %qs clauses", p);
47345 if (this_req != OMP_REQUIRES_DYNAMIC_ALLOCATORS
47346 && (omp_requires_mask & OMP_REQUIRES_TARGET_USED) != 0)
47347 error_at (cloc, "%qs clause used lexically after first "
47348 "target construct or offloading API", p);
47350 else if ((new_req & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47352 error_at (cloc, "too many %qs clauses",
47353 "atomic_default_mem_order");
47354 this_req = (enum omp_requires) 0;
47356 else if ((omp_requires_mask
47357 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER) != 0)
47359 error_at (cloc, "more than one %<atomic_default_mem_order%>"
47360 " clause in a single compilation unit");
47361 this_req
47362 = (enum omp_requires)
47363 (omp_requires_mask
47364 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER);
47366 else if ((omp_requires_mask
47367 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED) != 0)
47368 error_at (cloc, "%<atomic_default_mem_order%> clause used "
47369 "lexically after first %<atomic%> construct "
47370 "without memory order clause");
47371 new_req = (enum omp_requires) (new_req | this_req);
47372 omp_requires_mask
47373 = (enum omp_requires) (omp_requires_mask | this_req);
47374 continue;
47377 break;
47379 cp_parser_require_pragma_eol (parser, pragma_tok);
47381 if (new_req == 0)
47382 error_at (loc, "%<pragma omp requires%> requires at least one clause");
47383 return false;
47387 /* OpenMP 5.1:
47388 #pragma omp nothing new-line */
47390 static void
47391 cp_parser_omp_nothing (cp_parser *parser, cp_token *pragma_tok)
47393 cp_parser_require_pragma_eol (parser, pragma_tok);
47397 /* OpenMP 5.1
47398 #pragma omp error clauses[optseq] new-line */
47400 static bool
47401 cp_parser_omp_error (cp_parser *parser, cp_token *pragma_tok,
47402 enum pragma_context context)
47404 int at_compilation = -1;
47405 int severity_fatal = -1;
47406 tree message = NULL_TREE;
47407 bool first = true;
47408 bool bad = false;
47409 location_t loc = pragma_tok->location;
47411 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47413 /* For now only in C++ attributes, do it always for OpenMP 5.1. */
47414 if ((!first || parser->lexer->in_omp_attribute_pragma)
47415 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
47416 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
47417 cp_lexer_consume_token (parser->lexer);
47419 first = false;
47421 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
47422 break;
47424 const char *p
47425 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
47426 location_t cloc = cp_lexer_peek_token (parser->lexer)->location;
47427 static const char *args[] = {
47428 "execution", "compilation", "warning", "fatal"
47430 int *v = NULL;
47431 int idx = 0, n = -1;
47432 tree m = NULL_TREE;
47434 if (!strcmp (p, "at"))
47435 v = &at_compilation;
47436 else if (!strcmp (p, "severity"))
47438 v = &severity_fatal;
47439 idx += 2;
47441 else if (strcmp (p, "message"))
47443 error_at (cloc,
47444 "expected %<at%>, %<severity%> or %<message%> clause");
47445 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47446 return false;
47449 cp_lexer_consume_token (parser->lexer);
47451 matching_parens parens;
47452 if (parens.require_open (parser))
47454 if (v == NULL)
47456 m = cp_parser_assignment_expression (parser);
47457 if (type_dependent_expression_p (m))
47458 m = build1 (IMPLICIT_CONV_EXPR, const_string_type_node, m);
47459 else
47460 m = perform_implicit_conversion_flags (const_string_type_node, m,
47461 tf_warning_or_error,
47462 LOOKUP_NORMAL);
47464 else
47466 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47468 tree val = cp_lexer_peek_token (parser->lexer)->u.value;
47469 const char *q = IDENTIFIER_POINTER (val);
47471 if (!strcmp (q, args[idx]))
47472 n = 0;
47473 else if (!strcmp (q, args[idx + 1]))
47474 n = 1;
47476 if (n == -1)
47478 error_at (cp_lexer_peek_token (parser->lexer)->location,
47479 "expected %qs or %qs", args[idx], args[idx + 1]);
47480 bad = true;
47481 switch (cp_lexer_peek_token (parser->lexer)->type)
47483 case CPP_EOF:
47484 case CPP_PRAGMA_EOL:
47485 case CPP_CLOSE_PAREN:
47486 break;
47487 default:
47488 if (cp_lexer_nth_token_is (parser->lexer, 2,
47489 CPP_CLOSE_PAREN))
47490 cp_lexer_consume_token (parser->lexer);
47491 break;
47494 else
47495 cp_lexer_consume_token (parser->lexer);
47498 if (!parens.require_close (parser))
47499 cp_parser_skip_to_closing_parenthesis (parser,
47500 /*recovering=*/true,
47501 /*or_comma=*/false,
47502 /*consume_paren=*/
47503 true);
47505 if (v == NULL)
47507 if (message)
47509 error_at (cloc, "too many %qs clauses", p);
47510 bad = true;
47512 else
47513 message = m;
47515 else if (n != -1)
47517 if (*v != -1)
47519 error_at (cloc, "too many %qs clauses", p);
47520 bad = true;
47522 else
47523 *v = n;
47526 else
47527 bad = true;
47529 cp_parser_require_pragma_eol (parser, pragma_tok);
47530 if (bad)
47531 return true;
47533 if (at_compilation == -1)
47534 at_compilation = 1;
47535 if (severity_fatal == -1)
47536 severity_fatal = 1;
47537 if (!at_compilation)
47539 if (context != pragma_compound)
47541 error_at (loc, "%<#pragma omp error%> with %<at(execution)%> clause "
47542 "may only be used in compound statements");
47543 return true;
47545 tree fndecl
47546 = builtin_decl_explicit (severity_fatal ? BUILT_IN_GOMP_ERROR
47547 : BUILT_IN_GOMP_WARNING);
47548 if (!message)
47549 message = build_zero_cst (const_string_type_node);
47550 tree stmt = build_call_expr_loc (loc, fndecl, 2, message,
47551 build_all_ones_cst (size_type_node));
47552 add_stmt (stmt);
47553 return true;
47556 if (in_discarded_stmt)
47557 return false;
47559 const char *msg = NULL;
47560 if (message)
47562 msg = c_getstr (fold_for_warn (message));
47563 if (msg == NULL)
47564 msg = _("<message unknown at compile time>");
47566 if (msg)
47567 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
47568 "%<pragma omp error%> encountered: %s", msg);
47569 else
47570 emit_diagnostic (severity_fatal ? DK_ERROR : DK_WARNING, loc, 0,
47571 "%<pragma omp error%> encountered");
47572 return false;
47575 /* OpenMP 4.5:
47576 #pragma omp taskloop taskloop-clause[optseq] new-line
47577 for-loop
47579 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
47580 for-loop */
47582 #define OMP_TASKLOOP_CLAUSE_MASK \
47583 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
47584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
47585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
47586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
47587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
47588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
47589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
47590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
47591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
47592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
47593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
47594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
47595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
47596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
47597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
47598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
47599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
47601 static tree
47602 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
47603 char *p_name, omp_clause_mask mask, tree *cclauses,
47604 bool *if_p)
47606 tree clauses, sb, ret;
47607 unsigned int save;
47608 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
47610 strcat (p_name, " taskloop");
47611 mask |= OMP_TASKLOOP_CLAUSE_MASK;
47612 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
47613 clause. */
47614 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS)) != 0)
47615 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION);
47617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
47619 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
47620 const char *p = IDENTIFIER_POINTER (id);
47622 if (strcmp (p, "simd") == 0)
47624 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
47625 if (cclauses == NULL)
47626 cclauses = cclauses_buf;
47628 cp_lexer_consume_token (parser->lexer);
47629 if (!flag_openmp) /* flag_openmp_simd */
47630 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47631 cclauses, if_p);
47632 sb = begin_omp_structured_block ();
47633 save = cp_parser_begin_omp_structured_block (parser);
47634 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
47635 cclauses, if_p);
47636 cp_parser_end_omp_structured_block (parser, save);
47637 tree body = finish_omp_structured_block (sb);
47638 if (ret == NULL)
47639 return ret;
47640 ret = make_node (OMP_TASKLOOP);
47641 TREE_TYPE (ret) = void_type_node;
47642 OMP_FOR_BODY (ret) = body;
47643 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
47644 SET_EXPR_LOCATION (ret, loc);
47645 add_stmt (ret);
47646 return ret;
47649 if (!flag_openmp) /* flag_openmp_simd */
47651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47652 return NULL_TREE;
47655 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
47656 cclauses == NULL);
47657 if (cclauses)
47659 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
47660 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
47663 keep_next_level (true);
47664 sb = begin_omp_structured_block ();
47665 save = cp_parser_begin_omp_structured_block (parser);
47667 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
47668 if_p);
47670 cp_parser_end_omp_structured_block (parser, save);
47671 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
47673 return ret;
47677 /* OpenACC 2.0:
47678 # pragma acc routine oacc-routine-clause[optseq] new-line
47679 function-definition
47681 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
47684 #define OACC_ROUTINE_CLAUSE_MASK \
47685 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
47686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
47687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
47688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
47689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NOHOST) )
47691 /* Parse the OpenACC routine pragma. This has an optional '( name )'
47692 component, which must resolve to a declared namespace-scope
47693 function. The clauses are either processed directly (for a named
47694 function), or defered until the immediatley following declaration
47695 is parsed. */
47697 static void
47698 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
47699 enum pragma_context context)
47701 gcc_checking_assert (context == pragma_external);
47702 /* The checking for "another pragma following this one" in the "no optional
47703 '( name )'" case makes sure that we dont re-enter. */
47704 gcc_checking_assert (parser->oacc_routine == NULL);
47706 cp_oacc_routine_data data;
47707 data.error_seen = false;
47708 data.fndecl_seen = false;
47709 data.tokens = vNULL;
47710 data.clauses = NULL_TREE;
47711 data.loc = pragma_tok->location;
47712 /* It is safe to take the address of a local variable; it will only be
47713 used while this scope is live. */
47714 parser->oacc_routine = &data;
47716 /* Look for optional '( name )'. */
47717 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
47719 matching_parens parens;
47720 parens.consume_open (parser); /* '(' */
47722 /* We parse the name as an id-expression. If it resolves to
47723 anything other than a non-overloaded function at namespace
47724 scope, it's an error. */
47725 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
47726 tree name = cp_parser_id_expression (parser,
47727 /*template_keyword_p=*/false,
47728 /*check_dependency_p=*/false,
47729 /*template_p=*/NULL,
47730 /*declarator_p=*/false,
47731 /*optional_p=*/false);
47732 tree decl = (identifier_p (name)
47733 ? cp_parser_lookup_name_simple (parser, name, name_loc)
47734 : name);
47735 if (name != error_mark_node && decl == error_mark_node)
47736 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
47738 if (decl == error_mark_node
47739 || !parens.require_close (parser))
47741 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
47742 parser->oacc_routine = NULL;
47743 return;
47746 data.clauses
47747 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47748 "#pragma acc routine",
47749 cp_lexer_peek_token (parser->lexer));
47750 /* The clauses are in reverse order; fix that to make later diagnostic
47751 emission easier. */
47752 data.clauses = nreverse (data.clauses);
47754 if (decl && is_overloaded_fn (decl)
47755 && (TREE_CODE (decl) != FUNCTION_DECL
47756 || DECL_FUNCTION_TEMPLATE_P (decl)))
47758 error_at (name_loc,
47759 "%<#pragma acc routine%> names a set of overloads");
47760 parser->oacc_routine = NULL;
47761 return;
47764 /* Perhaps we should use the same rule as declarations in different
47765 namespaces? */
47766 if (!DECL_NAMESPACE_SCOPE_P (decl))
47768 error_at (name_loc,
47769 "%qD does not refer to a namespace scope function", decl);
47770 parser->oacc_routine = NULL;
47771 return;
47774 if (TREE_CODE (decl) != FUNCTION_DECL)
47776 error_at (name_loc, "%qD does not refer to a function", decl);
47777 parser->oacc_routine = NULL;
47778 return;
47781 cp_finalize_oacc_routine (parser, decl, false);
47782 parser->oacc_routine = NULL;
47784 else /* No optional '( name )'. */
47786 /* Store away all pragma tokens. */
47787 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
47788 cp_lexer_consume_token (parser->lexer);
47789 cp_parser_require_pragma_eol (parser, pragma_tok);
47790 struct cp_token_cache *cp
47791 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
47792 parser->oacc_routine->tokens.safe_push (cp);
47794 /* Emit a helpful diagnostic if there's another pragma following this
47795 one. */
47796 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
47798 cp_ensure_no_oacc_routine (parser);
47799 data.tokens.release ();
47800 /* ..., and then just keep going. */
47801 return;
47804 /* We only have to consider the pragma_external case here. */
47805 cp_parser_declaration (parser, NULL_TREE);
47806 if (parser->oacc_routine
47807 && !parser->oacc_routine->fndecl_seen)
47808 cp_ensure_no_oacc_routine (parser);
47809 else
47810 parser->oacc_routine = NULL;
47811 data.tokens.release ();
47815 /* Finalize #pragma acc routine clauses after direct declarator has
47816 been parsed. */
47818 static tree
47819 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
47821 struct cp_token_cache *ce;
47822 cp_oacc_routine_data *data = parser->oacc_routine;
47824 if (!data->error_seen && data->fndecl_seen)
47826 error_at (data->loc,
47827 "%<#pragma acc routine%> not immediately followed by "
47828 "a single function declaration or definition");
47829 data->error_seen = true;
47831 if (data->error_seen)
47832 return attrs;
47834 gcc_checking_assert (data->tokens.length () == 1);
47835 ce = data->tokens[0];
47837 cp_parser_push_lexer_for_tokens (parser, ce);
47838 parser->lexer->in_pragma = true;
47839 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
47841 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
47842 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
47843 parser->oacc_routine->clauses
47844 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
47845 "#pragma acc routine", pragma_tok);
47846 /* The clauses are in reverse order; fix that to make later diagnostic
47847 emission easier. */
47848 parser->oacc_routine->clauses = nreverse (parser->oacc_routine->clauses);
47849 cp_parser_pop_lexer (parser);
47850 /* Later, cp_finalize_oacc_routine will process the clauses. */
47851 parser->oacc_routine->fndecl_seen = true;
47853 return attrs;
47856 /* Apply any saved OpenACC routine clauses to a just-parsed
47857 declaration. */
47859 static void
47860 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
47862 if (UNLIKELY (parser->oacc_routine != NULL))
47864 /* Keep going if we're in error reporting mode. */
47865 if (parser->oacc_routine->error_seen
47866 || fndecl == error_mark_node)
47867 return;
47869 if (TREE_CODE (fndecl) != FUNCTION_DECL)
47871 if (parser->oacc_routine->fndecl_seen)
47873 error_at (parser->oacc_routine->loc,
47874 "%<#pragma acc routine%> not immediately followed by"
47875 " a single function declaration or definition");
47876 parser->oacc_routine = NULL;
47877 return;
47880 cp_ensure_no_oacc_routine (parser);
47881 return;
47884 int compatible
47885 = oacc_verify_routine_clauses (fndecl, &parser->oacc_routine->clauses,
47886 parser->oacc_routine->loc,
47887 "#pragma acc routine");
47888 if (compatible < 0)
47890 parser->oacc_routine = NULL;
47891 return;
47893 if (compatible > 0)
47896 else
47898 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
47900 error_at (parser->oacc_routine->loc,
47901 TREE_USED (fndecl)
47902 ? G_("%<#pragma acc routine%> must be applied before"
47903 " use")
47904 : G_("%<#pragma acc routine%> must be applied before"
47905 " definition"));
47906 parser->oacc_routine = NULL;
47907 return;
47910 /* Set the routine's level of parallelism. */
47911 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
47912 oacc_replace_fn_attrib (fndecl, dims);
47914 /* Add an "omp declare target" attribute. */
47915 DECL_ATTRIBUTES (fndecl)
47916 = tree_cons (get_identifier ("omp declare target"),
47917 parser->oacc_routine->clauses,
47918 DECL_ATTRIBUTES (fndecl));
47923 /* Main entry point to OpenMP statement pragmas. */
47925 static void
47926 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
47928 tree stmt;
47929 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
47930 omp_clause_mask mask (0);
47932 switch (cp_parser_pragma_kind (pragma_tok))
47934 case PRAGMA_OACC_ATOMIC:
47935 cp_parser_omp_atomic (parser, pragma_tok, true);
47936 return;
47937 case PRAGMA_OACC_CACHE:
47938 stmt = cp_parser_oacc_cache (parser, pragma_tok);
47939 break;
47940 case PRAGMA_OACC_DATA:
47941 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
47942 break;
47943 case PRAGMA_OACC_ENTER_DATA:
47944 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
47945 break;
47946 case PRAGMA_OACC_EXIT_DATA:
47947 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
47948 break;
47949 case PRAGMA_OACC_HOST_DATA:
47950 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
47951 break;
47952 case PRAGMA_OACC_KERNELS:
47953 case PRAGMA_OACC_PARALLEL:
47954 case PRAGMA_OACC_SERIAL:
47955 strcpy (p_name, "#pragma acc");
47956 stmt = cp_parser_oacc_compute (parser, pragma_tok, p_name, if_p);
47957 break;
47958 case PRAGMA_OACC_LOOP:
47959 strcpy (p_name, "#pragma acc");
47960 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
47961 if_p);
47962 break;
47963 case PRAGMA_OACC_UPDATE:
47964 stmt = cp_parser_oacc_update (parser, pragma_tok);
47965 break;
47966 case PRAGMA_OACC_WAIT:
47967 stmt = cp_parser_oacc_wait (parser, pragma_tok);
47968 break;
47969 case PRAGMA_OMP_ALLOCATE:
47970 cp_parser_omp_allocate (parser, pragma_tok);
47971 return;
47972 case PRAGMA_OMP_ATOMIC:
47973 cp_parser_omp_atomic (parser, pragma_tok, false);
47974 return;
47975 case PRAGMA_OMP_CRITICAL:
47976 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
47977 break;
47978 case PRAGMA_OMP_DISTRIBUTE:
47979 strcpy (p_name, "#pragma omp");
47980 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
47981 if_p);
47982 break;
47983 case PRAGMA_OMP_FOR:
47984 strcpy (p_name, "#pragma omp");
47985 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
47986 if_p);
47987 break;
47988 case PRAGMA_OMP_LOOP:
47989 strcpy (p_name, "#pragma omp");
47990 stmt = cp_parser_omp_loop (parser, pragma_tok, p_name, mask, NULL,
47991 if_p);
47992 break;
47993 case PRAGMA_OMP_MASKED:
47994 strcpy (p_name, "#pragma omp");
47995 stmt = cp_parser_omp_masked (parser, pragma_tok, p_name, mask, NULL,
47996 if_p);
47997 break;
47998 case PRAGMA_OMP_MASTER:
47999 strcpy (p_name, "#pragma omp");
48000 stmt = cp_parser_omp_master (parser, pragma_tok, p_name, mask, NULL,
48001 if_p);
48002 break;
48003 case PRAGMA_OMP_PARALLEL:
48004 strcpy (p_name, "#pragma omp");
48005 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
48006 if_p);
48007 break;
48008 case PRAGMA_OMP_SCOPE:
48009 stmt = cp_parser_omp_scope (parser, pragma_tok, if_p);
48010 break;
48011 case PRAGMA_OMP_SECTIONS:
48012 strcpy (p_name, "#pragma omp");
48013 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
48014 break;
48015 case PRAGMA_OMP_SIMD:
48016 strcpy (p_name, "#pragma omp");
48017 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
48018 if_p);
48019 break;
48020 case PRAGMA_OMP_SINGLE:
48021 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
48022 break;
48023 case PRAGMA_OMP_TASK:
48024 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
48025 break;
48026 case PRAGMA_OMP_TASKGROUP:
48027 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
48028 break;
48029 case PRAGMA_OMP_TASKLOOP:
48030 strcpy (p_name, "#pragma omp");
48031 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
48032 if_p);
48033 break;
48034 case PRAGMA_OMP_TEAMS:
48035 strcpy (p_name, "#pragma omp");
48036 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
48037 if_p);
48038 break;
48039 case PRAGMA_OMP_ASSUME:
48040 cp_parser_omp_assume (parser, pragma_tok, if_p);
48041 return;
48042 default:
48043 gcc_unreachable ();
48046 protected_set_expr_location (stmt, pragma_tok->location);
48049 /* Transactional Memory parsing routines. */
48051 /* Parse a transaction attribute.
48053 txn-attribute:
48054 attribute
48055 [ [ identifier ] ]
48057 We use this instead of cp_parser_attributes_opt for transactions to avoid
48058 the pedwarn in C++98 mode. */
48060 static tree
48061 cp_parser_txn_attribute_opt (cp_parser *parser)
48063 cp_token *token;
48064 tree attr_name, attr = NULL;
48066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
48067 return cp_parser_attributes_opt (parser);
48069 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
48070 return NULL_TREE;
48071 cp_lexer_consume_token (parser->lexer);
48072 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
48073 goto error1;
48075 token = cp_lexer_peek_token (parser->lexer);
48076 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
48078 token = cp_lexer_consume_token (parser->lexer);
48080 attr_name = (token->type == CPP_KEYWORD
48081 /* For keywords, use the canonical spelling,
48082 not the parsed identifier. */
48083 ? ridpointers[(int) token->keyword]
48084 : token->u.value);
48085 attr = build_tree_list (attr_name, NULL_TREE);
48087 else
48088 cp_parser_error (parser, "expected identifier");
48090 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48091 error1:
48092 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
48093 return attr;
48096 /* Parse a __transaction_atomic or __transaction_relaxed statement.
48098 transaction-statement:
48099 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
48100 compound-statement
48101 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
48104 static tree
48105 cp_parser_transaction (cp_parser *parser, cp_token *token)
48107 unsigned char old_in = parser->in_transaction;
48108 unsigned char this_in = 1, new_in;
48109 enum rid keyword = token->keyword;
48110 tree stmt, attrs, noex;
48112 cp_lexer_consume_token (parser->lexer);
48114 if (keyword == RID_TRANSACTION_RELAXED
48115 || keyword == RID_SYNCHRONIZED)
48116 this_in |= TM_STMT_ATTR_RELAXED;
48117 else
48119 attrs = cp_parser_txn_attribute_opt (parser);
48120 if (attrs)
48121 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48124 /* Parse a noexcept specification. */
48125 if (keyword == RID_ATOMIC_NOEXCEPT)
48126 noex = boolean_true_node;
48127 else if (keyword == RID_ATOMIC_CANCEL)
48129 /* cancel-and-throw is unimplemented. */
48130 sorry ("%<atomic_cancel%>");
48131 noex = NULL_TREE;
48133 else
48134 noex = cp_parser_noexcept_specification_opt (parser,
48135 CP_PARSER_FLAGS_NONE,
48136 /*require_constexpr=*/true,
48137 /*consumed_expr=*/NULL,
48138 /*return_cond=*/true);
48140 /* Keep track if we're in the lexical scope of an outer transaction. */
48141 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
48143 stmt = begin_transaction_stmt (token->location, NULL, this_in);
48145 parser->in_transaction = new_in;
48146 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
48147 parser->in_transaction = old_in;
48149 finish_transaction_stmt (stmt, NULL, this_in, noex);
48151 return stmt;
48154 /* Parse a __transaction_atomic or __transaction_relaxed expression.
48156 transaction-expression:
48157 __transaction_atomic txn-noexcept-spec[opt] ( expression )
48158 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
48161 static tree
48162 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
48164 unsigned char old_in = parser->in_transaction;
48165 unsigned char this_in = 1;
48166 cp_token *token;
48167 tree expr, noex;
48168 bool noex_expr;
48169 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
48171 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
48172 || keyword == RID_TRANSACTION_RELAXED);
48174 if (!flag_tm)
48175 error_at (loc,
48176 keyword == RID_TRANSACTION_RELAXED
48177 ? G_("%<__transaction_relaxed%> without transactional memory "
48178 "support enabled")
48179 : G_("%<__transaction_atomic%> without transactional memory "
48180 "support enabled"));
48182 token = cp_parser_require_keyword (parser, keyword,
48183 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
48184 : RT_TRANSACTION_RELAXED));
48185 gcc_assert (token != NULL);
48187 if (keyword == RID_TRANSACTION_RELAXED)
48188 this_in |= TM_STMT_ATTR_RELAXED;
48190 /* Set this early. This might mean that we allow transaction_cancel in
48191 an expression that we find out later actually has to be a constexpr.
48192 However, we expect that cxx_constant_value will be able to deal with
48193 this; also, if the noexcept has no constexpr, then what we parse next
48194 really is a transaction's body. */
48195 parser->in_transaction = this_in;
48197 /* Parse a noexcept specification. */
48198 noex = cp_parser_noexcept_specification_opt (parser,
48199 CP_PARSER_FLAGS_NONE,
48200 /*require_constexpr=*/false,
48201 &noex_expr,
48202 /*return_cond=*/true);
48204 if (!noex || !noex_expr
48205 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
48207 matching_parens parens;
48208 parens.require_open (parser);
48210 expr = cp_parser_expression (parser);
48211 expr = finish_parenthesized_expr (expr);
48213 parens.require_close (parser);
48215 else
48217 /* The only expression that is available got parsed for the noexcept
48218 already. noexcept is true then. */
48219 expr = noex;
48220 noex = boolean_true_node;
48223 expr = build_transaction_expr (token->location, expr, this_in, noex);
48224 parser->in_transaction = old_in;
48226 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
48227 return error_mark_node;
48229 return (flag_tm ? expr : error_mark_node);
48232 /* Parse a function-transaction-block.
48234 function-transaction-block:
48235 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
48236 function-body
48237 __transaction_atomic txn-attribute[opt] function-try-block
48238 __transaction_relaxed ctor-initializer[opt] function-body
48239 __transaction_relaxed function-try-block
48242 static void
48243 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
48245 unsigned char old_in = parser->in_transaction;
48246 unsigned char new_in = 1;
48247 tree compound_stmt, stmt, attrs;
48248 cp_token *token;
48250 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
48251 || keyword == RID_TRANSACTION_RELAXED);
48252 token = cp_parser_require_keyword (parser, keyword,
48253 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
48254 : RT_TRANSACTION_RELAXED));
48255 gcc_assert (token != NULL);
48257 if (keyword == RID_TRANSACTION_RELAXED)
48258 new_in |= TM_STMT_ATTR_RELAXED;
48259 else
48261 attrs = cp_parser_txn_attribute_opt (parser);
48262 if (attrs)
48263 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
48266 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
48268 parser->in_transaction = new_in;
48270 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
48271 cp_parser_function_try_block (parser);
48272 else
48273 cp_parser_ctor_initializer_opt_and_function_body
48274 (parser, /*in_function_try_block=*/false);
48276 parser->in_transaction = old_in;
48278 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
48281 /* Parse a __transaction_cancel statement.
48283 cancel-statement:
48284 __transaction_cancel txn-attribute[opt] ;
48285 __transaction_cancel txn-attribute[opt] throw-expression ;
48287 ??? Cancel and throw is not yet implemented. */
48289 static tree
48290 cp_parser_transaction_cancel (cp_parser *parser)
48292 cp_token *token;
48293 bool is_outer = false;
48294 tree stmt, attrs;
48296 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
48297 RT_TRANSACTION_CANCEL);
48298 gcc_assert (token != NULL);
48300 attrs = cp_parser_txn_attribute_opt (parser);
48301 if (attrs)
48302 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
48304 /* ??? Parse cancel-and-throw here. */
48306 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
48308 if (!flag_tm)
48310 error_at (token->location, "%<__transaction_cancel%> without "
48311 "transactional memory support enabled");
48312 return error_mark_node;
48314 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
48316 error_at (token->location, "%<__transaction_cancel%> within a "
48317 "%<__transaction_relaxed%>");
48318 return error_mark_node;
48320 else if (is_outer)
48322 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
48323 && !is_tm_may_cancel_outer (current_function_decl))
48325 error_at (token->location, "outer %<__transaction_cancel%> not "
48326 "within outer %<__transaction_atomic%>");
48327 error_at (token->location,
48328 " or a %<transaction_may_cancel_outer%> function");
48329 return error_mark_node;
48332 else if (parser->in_transaction == 0)
48334 error_at (token->location, "%<__transaction_cancel%> not within "
48335 "%<__transaction_atomic%>");
48336 return error_mark_node;
48339 stmt = build_tm_abort_call (token->location, is_outer);
48340 add_stmt (stmt);
48342 return stmt;
48346 /* Special handling for the first token or line in the file. The first
48347 thing in the file might be #pragma GCC pch_preprocess, which loads a
48348 PCH file, which is a GC collection point. So we need to handle this
48349 first pragma without benefit of an existing lexer structure.
48351 Always returns one token to the caller in *FIRST_TOKEN. This is
48352 either the true first token of the file, or the first token after
48353 the initial pragma. */
48355 static void
48356 cp_parser_initial_pragma (cp_token *first_token)
48358 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
48359 return;
48361 cp_lexer_get_preprocessor_token (0, first_token);
48363 tree name = NULL;
48364 if (first_token->type == CPP_STRING)
48366 name = first_token->u.value;
48368 cp_lexer_get_preprocessor_token (0, first_token);
48371 /* Skip to the end of the pragma. */
48372 if (first_token->type != CPP_PRAGMA_EOL)
48374 error_at (first_token->location,
48375 "malformed %<#pragma GCC pch_preprocess%>");
48377 cp_lexer_get_preprocessor_token (0, first_token);
48378 while (first_token->type != CPP_PRAGMA_EOL);
48381 /* Now actually load the PCH file. */
48382 if (name)
48383 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
48385 /* Read one more token to return to our caller. We have to do this
48386 after reading the PCH file in, since its pointers have to be
48387 live. */
48388 cp_lexer_get_preprocessor_token (0, first_token);
48391 /* Parse a pragma GCC ivdep. */
48393 static bool
48394 cp_parser_pragma_ivdep (cp_parser *parser, cp_token *pragma_tok)
48396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48397 return true;
48400 /* Parse a pragma GCC unroll. */
48402 static unsigned short
48403 cp_parser_pragma_unroll (cp_parser *parser, cp_token *pragma_tok)
48405 location_t location = cp_lexer_peek_token (parser->lexer)->location;
48406 tree expr = cp_parser_constant_expression (parser);
48407 unsigned short unroll;
48408 expr = maybe_constant_value (expr);
48409 HOST_WIDE_INT lunroll = 0;
48410 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr))
48411 || TREE_CODE (expr) != INTEGER_CST
48412 || (lunroll = tree_to_shwi (expr)) < 0
48413 || lunroll >= USHRT_MAX)
48415 error_at (location, "%<#pragma GCC unroll%> requires an"
48416 " assignment-expression that evaluates to a non-negative"
48417 " integral constant less than %u", USHRT_MAX);
48418 unroll = 0;
48420 else
48422 unroll = (unsigned short)lunroll;
48423 if (unroll == 0)
48424 unroll = 1;
48426 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48427 return unroll;
48430 /* Normal parsing of a pragma token. Here we can (and must) use the
48431 regular lexer. */
48433 static bool
48434 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
48436 cp_token *pragma_tok;
48437 unsigned int id;
48438 tree stmt;
48439 bool ret = false;
48441 pragma_tok = cp_lexer_consume_token (parser->lexer);
48442 gcc_assert (pragma_tok->type == CPP_PRAGMA);
48443 parser->lexer->in_pragma = true;
48445 id = cp_parser_pragma_kind (pragma_tok);
48446 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
48447 cp_ensure_no_omp_declare_simd (parser);
48448 switch (id)
48450 case PRAGMA_GCC_PCH_PREPROCESS:
48451 error_at (pragma_tok->location,
48452 "%<#pragma GCC pch_preprocess%> must be first");
48453 break;
48455 case PRAGMA_OMP_BARRIER:
48456 switch (context)
48458 case pragma_compound:
48459 cp_parser_omp_barrier (parser, pragma_tok);
48460 return false;
48461 case pragma_stmt:
48462 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48463 "used in compound statements", "omp barrier");
48464 ret = true;
48465 break;
48466 default:
48467 goto bad_stmt;
48469 break;
48471 case PRAGMA_OMP_DEPOBJ:
48472 switch (context)
48474 case pragma_compound:
48475 cp_parser_omp_depobj (parser, pragma_tok);
48476 return false;
48477 case pragma_stmt:
48478 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48479 "used in compound statements", "omp depobj");
48480 ret = true;
48481 break;
48482 default:
48483 goto bad_stmt;
48485 break;
48487 case PRAGMA_OMP_FLUSH:
48488 switch (context)
48490 case pragma_compound:
48491 cp_parser_omp_flush (parser, pragma_tok);
48492 return false;
48493 case pragma_stmt:
48494 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
48495 "used in compound statements", "omp flush");
48496 ret = true;
48497 break;
48498 default:
48499 goto bad_stmt;
48501 break;
48503 case PRAGMA_OMP_TASKWAIT:
48504 switch (context)
48506 case pragma_compound:
48507 cp_parser_omp_taskwait (parser, pragma_tok);
48508 return false;
48509 case pragma_stmt:
48510 error_at (pragma_tok->location,
48511 "%<#pragma %s%> may only be used in compound statements",
48512 "omp taskwait");
48513 ret = true;
48514 break;
48515 default:
48516 goto bad_stmt;
48518 break;
48520 case PRAGMA_OMP_TASKYIELD:
48521 switch (context)
48523 case pragma_compound:
48524 cp_parser_omp_taskyield (parser, pragma_tok);
48525 return false;
48526 case pragma_stmt:
48527 error_at (pragma_tok->location,
48528 "%<#pragma %s%> may only be used in compound statements",
48529 "omp taskyield");
48530 ret = true;
48531 break;
48532 default:
48533 goto bad_stmt;
48535 break;
48537 case PRAGMA_OMP_CANCEL:
48538 switch (context)
48540 case pragma_compound:
48541 cp_parser_omp_cancel (parser, pragma_tok);
48542 return false;
48543 case pragma_stmt:
48544 error_at (pragma_tok->location,
48545 "%<#pragma %s%> may only be used in compound statements",
48546 "omp cancel");
48547 ret = true;
48548 break;
48549 default:
48550 goto bad_stmt;
48552 break;
48554 case PRAGMA_OMP_CANCELLATION_POINT:
48555 return cp_parser_omp_cancellation_point (parser, pragma_tok, context);
48557 case PRAGMA_OMP_THREADPRIVATE:
48558 cp_parser_omp_threadprivate (parser, pragma_tok);
48559 return false;
48561 case PRAGMA_OMP_DECLARE:
48562 return cp_parser_omp_declare (parser, pragma_tok, context);
48564 case PRAGMA_OACC_DECLARE:
48565 cp_parser_oacc_declare (parser, pragma_tok);
48566 return false;
48568 case PRAGMA_OACC_ENTER_DATA:
48569 if (context == pragma_stmt)
48571 error_at (pragma_tok->location,
48572 "%<#pragma %s%> may only be used in compound statements",
48573 "acc enter data");
48574 ret = true;
48575 break;
48577 else if (context != pragma_compound)
48578 goto bad_stmt;
48579 cp_parser_omp_construct (parser, pragma_tok, if_p);
48580 return true;
48582 case PRAGMA_OACC_EXIT_DATA:
48583 if (context == pragma_stmt)
48585 error_at (pragma_tok->location,
48586 "%<#pragma %s%> may only be used in compound statements",
48587 "acc exit data");
48588 ret = true;
48589 break;
48591 else if (context != pragma_compound)
48592 goto bad_stmt;
48593 cp_parser_omp_construct (parser, pragma_tok, if_p);
48594 return true;
48596 case PRAGMA_OACC_ROUTINE:
48597 if (context != pragma_external)
48599 error_at (pragma_tok->location,
48600 "%<#pragma acc routine%> must be at file scope");
48601 ret = true;
48602 break;
48604 cp_parser_oacc_routine (parser, pragma_tok, context);
48605 return false;
48607 case PRAGMA_OACC_UPDATE:
48608 if (context == pragma_stmt)
48610 error_at (pragma_tok->location,
48611 "%<#pragma %s%> may only be used in compound statements",
48612 "acc update");
48613 ret = true;
48614 break;
48616 else if (context != pragma_compound)
48617 goto bad_stmt;
48618 cp_parser_omp_construct (parser, pragma_tok, if_p);
48619 return true;
48621 case PRAGMA_OACC_WAIT:
48622 if (context == pragma_stmt)
48624 error_at (pragma_tok->location,
48625 "%<#pragma %s%> may only be used in compound statements",
48626 "acc wait");
48627 ret = true;
48628 break;
48630 else if (context != pragma_compound)
48631 goto bad_stmt;
48632 cp_parser_omp_construct (parser, pragma_tok, if_p);
48633 return true;
48634 case PRAGMA_OMP_ALLOCATE:
48635 cp_parser_omp_allocate (parser, pragma_tok);
48636 return false;
48637 case PRAGMA_OACC_ATOMIC:
48638 case PRAGMA_OACC_CACHE:
48639 case PRAGMA_OACC_DATA:
48640 case PRAGMA_OACC_HOST_DATA:
48641 case PRAGMA_OACC_KERNELS:
48642 case PRAGMA_OACC_LOOP:
48643 case PRAGMA_OACC_PARALLEL:
48644 case PRAGMA_OACC_SERIAL:
48645 case PRAGMA_OMP_ASSUME:
48646 case PRAGMA_OMP_ATOMIC:
48647 case PRAGMA_OMP_CRITICAL:
48648 case PRAGMA_OMP_DISTRIBUTE:
48649 case PRAGMA_OMP_FOR:
48650 case PRAGMA_OMP_LOOP:
48651 case PRAGMA_OMP_MASKED:
48652 case PRAGMA_OMP_MASTER:
48653 case PRAGMA_OMP_PARALLEL:
48654 case PRAGMA_OMP_SCOPE:
48655 case PRAGMA_OMP_SECTIONS:
48656 case PRAGMA_OMP_SIMD:
48657 case PRAGMA_OMP_SINGLE:
48658 case PRAGMA_OMP_TASK:
48659 case PRAGMA_OMP_TASKGROUP:
48660 case PRAGMA_OMP_TASKLOOP:
48661 case PRAGMA_OMP_TEAMS:
48662 if (context != pragma_stmt && context != pragma_compound)
48663 goto bad_stmt;
48664 stmt = push_omp_privatization_clauses (false);
48665 cp_parser_omp_construct (parser, pragma_tok, if_p);
48666 pop_omp_privatization_clauses (stmt);
48667 return true;
48669 case PRAGMA_OMP_REQUIRES:
48670 if (context != pragma_external)
48672 error_at (pragma_tok->location,
48673 "%<#pragma omp requires%> may only be used at file or "
48674 "namespace scope");
48675 ret = true;
48676 break;
48678 return cp_parser_omp_requires (parser, pragma_tok);
48680 case PRAGMA_OMP_ASSUMES:
48681 if (context != pragma_external)
48683 error_at (pragma_tok->location,
48684 "%<#pragma omp assumes%> may only be used at file or "
48685 "namespace scope");
48686 ret = true;
48687 break;
48689 return cp_parser_omp_assumes (parser, pragma_tok);
48691 case PRAGMA_OMP_NOTHING:
48692 cp_parser_omp_nothing (parser, pragma_tok);
48693 return false;
48695 case PRAGMA_OMP_ERROR:
48696 return cp_parser_omp_error (parser, pragma_tok, context);
48698 case PRAGMA_OMP_ORDERED:
48699 if (context != pragma_stmt && context != pragma_compound)
48700 goto bad_stmt;
48701 stmt = push_omp_privatization_clauses (false);
48702 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
48703 pop_omp_privatization_clauses (stmt);
48704 return ret;
48706 case PRAGMA_OMP_TARGET:
48707 if (context != pragma_stmt && context != pragma_compound)
48708 goto bad_stmt;
48709 stmt = push_omp_privatization_clauses (false);
48710 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
48711 pop_omp_privatization_clauses (stmt);
48712 return ret;
48714 case PRAGMA_OMP_BEGIN:
48715 cp_parser_omp_begin (parser, pragma_tok);
48716 return false;
48718 case PRAGMA_OMP_END:
48719 cp_parser_omp_end (parser, pragma_tok);
48720 return false;
48722 case PRAGMA_OMP_SCAN:
48723 error_at (pragma_tok->location,
48724 "%<#pragma omp scan%> may only be used in "
48725 "a loop construct with %<inscan%> %<reduction%> clause");
48726 break;
48728 case PRAGMA_OMP_SECTION:
48729 error_at (pragma_tok->location,
48730 "%<#pragma omp section%> may only be used in "
48731 "%<#pragma omp sections%> construct");
48732 break;
48734 case PRAGMA_IVDEP:
48736 if (context == pragma_external)
48738 error_at (pragma_tok->location,
48739 "%<#pragma GCC ivdep%> must be inside a function");
48740 break;
48742 const bool ivdep = cp_parser_pragma_ivdep (parser, pragma_tok);
48743 unsigned short unroll;
48744 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48745 if (tok->type == CPP_PRAGMA
48746 && cp_parser_pragma_kind (tok) == PRAGMA_UNROLL)
48748 tok = cp_lexer_consume_token (parser->lexer);
48749 unroll = cp_parser_pragma_unroll (parser, tok);
48750 tok = cp_lexer_peek_token (the_parser->lexer);
48752 else
48753 unroll = 0;
48754 if (tok->type != CPP_KEYWORD
48755 || (tok->keyword != RID_FOR
48756 && tok->keyword != RID_WHILE
48757 && tok->keyword != RID_DO))
48759 cp_parser_error (parser, "for, while or do statement expected");
48760 return false;
48762 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48763 return true;
48766 case PRAGMA_UNROLL:
48768 if (context == pragma_external)
48770 error_at (pragma_tok->location,
48771 "%<#pragma GCC unroll%> must be inside a function");
48772 break;
48774 const unsigned short unroll
48775 = cp_parser_pragma_unroll (parser, pragma_tok);
48776 bool ivdep;
48777 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48778 if (tok->type == CPP_PRAGMA
48779 && cp_parser_pragma_kind (tok) == PRAGMA_IVDEP)
48781 tok = cp_lexer_consume_token (parser->lexer);
48782 ivdep = cp_parser_pragma_ivdep (parser, tok);
48783 tok = cp_lexer_peek_token (the_parser->lexer);
48785 else
48786 ivdep = false;
48787 if (tok->type != CPP_KEYWORD
48788 || (tok->keyword != RID_FOR
48789 && tok->keyword != RID_WHILE
48790 && tok->keyword != RID_DO))
48792 cp_parser_error (parser, "for, while or do statement expected");
48793 return false;
48795 cp_parser_iteration_statement (parser, if_p, ivdep, unroll);
48796 return true;
48799 default:
48800 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
48801 c_invoke_pragma_handler (id);
48802 break;
48804 bad_stmt:
48805 cp_parser_error (parser, "expected declaration specifiers");
48806 break;
48809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
48810 return ret;
48813 /* The interface the pragma parsers have to the lexer. */
48815 enum cpp_ttype
48816 pragma_lex (tree *value, location_t *loc)
48818 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
48819 enum cpp_ttype ret = tok->type;
48821 *value = tok->u.value;
48822 if (loc)
48823 *loc = tok->location;
48825 if (ret == CPP_PRAGMA_EOL)
48826 ret = CPP_EOF;
48827 else if (ret == CPP_STRING)
48828 *value = cp_parser_string_literal (the_parser, false, false);
48829 else
48831 if (ret == CPP_KEYWORD)
48832 ret = CPP_NAME;
48833 cp_lexer_consume_token (the_parser->lexer);
48836 return ret;
48840 /* External interface. */
48842 /* Parse one entire translation unit. */
48844 void
48845 c_parse_file (void)
48847 static bool already_called = false;
48849 if (already_called)
48850 fatal_error (input_location,
48851 "multi-source compilation not implemented for C++");
48852 already_called = true;
48854 /* cp_lexer_new_main is called before doing any GC allocation
48855 because tokenization might load a PCH file. */
48856 cp_lexer_new_main ();
48858 cp_parser_translation_unit (the_parser);
48859 class_decl_loc_t::diag_mismatched_tags ();
48861 the_parser = NULL;
48863 finish_translation_unit ();
48866 /* Create an identifier for a generic parameter type (a synthesized
48867 template parameter implied by `auto' or a concept identifier). */
48869 static GTY(()) int generic_parm_count;
48870 static tree
48871 make_generic_type_name ()
48873 char buf[32];
48874 sprintf (buf, "auto:%d", ++generic_parm_count);
48875 return get_identifier (buf);
48878 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
48879 (creating a new template parameter list if necessary). Returns the newly
48880 created template type parm. */
48882 static tree
48883 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
48885 /* A requires-clause is not a function and cannot have placeholders. */
48886 if (current_binding_level->requires_expression)
48888 error ("placeholder type not allowed in this context");
48889 return error_mark_node;
48892 gcc_assert (current_binding_level->kind == sk_function_parms);
48894 /* We are either continuing a function template that already contains implicit
48895 template parameters, creating a new fully-implicit function template, or
48896 extending an existing explicit function template with implicit template
48897 parameters. */
48899 cp_binding_level *const entry_scope = current_binding_level;
48901 bool become_template = false;
48902 cp_binding_level *parent_scope = 0;
48904 if (parser->implicit_template_scope)
48906 gcc_assert (parser->implicit_template_parms);
48908 current_binding_level = parser->implicit_template_scope;
48910 else
48912 /* Roll back to the existing template parameter scope (in the case of
48913 extending an explicit function template) or introduce a new template
48914 parameter scope ahead of the function parameter scope (or class scope
48915 in the case of out-of-line member definitions). The function scope is
48916 added back after template parameter synthesis below. */
48918 cp_binding_level *scope = entry_scope;
48920 while (scope->kind == sk_function_parms)
48922 parent_scope = scope;
48923 scope = scope->level_chain;
48925 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
48927 /* If not defining a class, then any class scope is a scope level in
48928 an out-of-line member definition. In this case simply wind back
48929 beyond the first such scope to inject the template parameter list.
48930 Otherwise wind back to the class being defined. The latter can
48931 occur in class member friend declarations such as:
48933 class A {
48934 void foo (auto);
48936 class B {
48937 friend void A::foo (auto);
48940 The template parameter list synthesized for the friend declaration
48941 must be injected in the scope of 'B'. This can also occur in
48942 erroneous cases such as:
48944 struct A {
48945 struct B {
48946 void foo (auto);
48948 void B::foo (auto) {}
48951 Here the attempted definition of 'B::foo' within 'A' is ill-formed
48952 but, nevertheless, the template parameter list synthesized for the
48953 declarator should be injected into the scope of 'A' as if the
48954 ill-formed template was specified explicitly. */
48956 while (scope->kind == sk_class && !scope->defining_class_p)
48958 parent_scope = scope;
48959 scope = scope->level_chain;
48963 current_binding_level = scope;
48965 if (scope->kind != sk_template_parms
48966 || !function_being_declared_is_template_p (parser))
48968 /* Introduce a new template parameter list for implicit template
48969 parameters. */
48971 become_template = true;
48973 parser->implicit_template_scope
48974 = begin_scope (sk_template_parms, NULL);
48976 ++processing_template_decl;
48978 parser->fully_implicit_function_template_p = true;
48979 ++parser->num_template_parameter_lists;
48981 else
48983 /* Synthesize implicit template parameters at the end of the explicit
48984 template parameter list. */
48986 gcc_assert (current_template_parms);
48988 parser->implicit_template_scope = scope;
48990 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
48991 parser->implicit_template_parms
48992 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
48996 /* Synthesize a new template parameter and track the current template
48997 parameter chain with implicit_template_parms. */
48999 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
49000 tree synth_id = make_generic_type_name ();
49001 tree synth_tmpl_parm;
49002 bool non_type = false;
49004 /* Synthesize the type template parameter. */
49005 gcc_assert(!proto || TREE_CODE (proto) == TYPE_DECL);
49006 synth_tmpl_parm = finish_template_type_parm (class_type_node, synth_id);
49008 if (become_template)
49009 current_template_parms = tree_cons (size_int (current_template_depth + 1),
49010 NULL_TREE, current_template_parms);
49012 /* Attach the constraint to the parm before processing. */
49013 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
49014 TREE_TYPE (node) = constr;
49015 tree new_parm
49016 = process_template_parm (parser->implicit_template_parms,
49017 input_location,
49018 node,
49019 /*non_type=*/non_type,
49020 /*param_pack=*/false);
49022 /* Mark the synthetic declaration "virtual". This is used when
49023 comparing template-heads to determine if whether an abbreviated
49024 function template is equivalent to an explicit template.
49026 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
49027 if (TREE_VALUE (new_parm) != error_mark_node)
49028 DECL_VIRTUAL_P (TREE_VALUE (new_parm)) = true;
49030 // Chain the new parameter to the list of implicit parameters.
49031 if (parser->implicit_template_parms)
49032 parser->implicit_template_parms
49033 = TREE_CHAIN (parser->implicit_template_parms);
49034 else
49035 parser->implicit_template_parms = new_parm;
49037 tree new_decl = get_local_decls ();
49038 if (non_type)
49039 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
49040 new_decl = DECL_INITIAL (new_decl);
49042 /* If creating a fully implicit function template, start the new implicit
49043 template parameter list with this synthesized type, otherwise grow the
49044 current template parameter list. */
49046 if (become_template)
49048 parent_scope->level_chain = current_binding_level;
49050 tree new_parms = make_tree_vec (1);
49051 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
49052 TREE_VALUE (current_template_parms) = new_parms;
49054 else
49056 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
49057 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
49058 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
49059 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
49062 /* If the new parameter was constrained, we need to add that to the
49063 constraints in the template parameter list. */
49064 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
49066 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
49067 reqs = combine_constraint_expressions (reqs, req);
49068 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
49071 current_binding_level = entry_scope;
49073 return new_decl;
49076 /* Finish the declaration of a fully implicit function template. Such a
49077 template has no explicit template parameter list so has not been through the
49078 normal template head and tail processing. synthesize_implicit_template_parm
49079 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
49080 provided if the declaration is a class member such that its template
49081 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
49082 form is returned. Otherwise NULL_TREE is returned. */
49084 static tree
49085 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
49087 gcc_assert (parser->fully_implicit_function_template_p);
49089 if (member_decl_opt && member_decl_opt != error_mark_node
49090 && DECL_VIRTUAL_P (member_decl_opt))
49092 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
49093 "implicit templates may not be %<virtual%>");
49094 DECL_VIRTUAL_P (member_decl_opt) = false;
49097 if (member_decl_opt)
49098 member_decl_opt = finish_member_template_decl (member_decl_opt);
49099 end_template_decl ();
49101 parser->fully_implicit_function_template_p = false;
49102 parser->implicit_template_parms = 0;
49103 parser->implicit_template_scope = 0;
49104 --parser->num_template_parameter_lists;
49106 return member_decl_opt;
49109 /* Like finish_fully_implicit_template, but to be used in error
49110 recovery, rearranging scopes so that we restore the state we had
49111 before synthesize_implicit_template_parm inserted the implement
49112 template parms scope. */
49114 static void
49115 abort_fully_implicit_template (cp_parser *parser)
49117 cp_binding_level *return_to_scope = current_binding_level;
49119 if (parser->implicit_template_scope
49120 && return_to_scope != parser->implicit_template_scope)
49122 cp_binding_level *child = return_to_scope;
49123 for (cp_binding_level *scope = child->level_chain;
49124 scope != parser->implicit_template_scope;
49125 scope = child->level_chain)
49126 child = scope;
49127 child->level_chain = parser->implicit_template_scope->level_chain;
49128 parser->implicit_template_scope->level_chain = return_to_scope;
49129 current_binding_level = parser->implicit_template_scope;
49131 else
49132 return_to_scope = return_to_scope->level_chain;
49134 finish_fully_implicit_template (parser, NULL);
49136 gcc_assert (current_binding_level == return_to_scope);
49139 /* Helper function for diagnostics that have complained about things
49140 being used with 'extern "C"' linkage.
49142 Attempt to issue a note showing where the 'extern "C"' linkage began. */
49144 void
49145 maybe_show_extern_c_location (void)
49147 if (the_parser->innermost_linkage_specification_location != UNKNOWN_LOCATION)
49148 inform (the_parser->innermost_linkage_specification_location,
49149 "%<extern \"C\"%> linkage started here");
49152 #include "gt-cp-parser.h"